[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-09-06 Thread Timm Bäder via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb93d2d37e709: [clang][Interp] Handle SourceLocExprs 
(authored by tbaeder).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/lib/AST/Interp/Program.cpp
  clang/test/AST/Interp/builtin-functions.cpp

Index: clang/test/AST/Interp/builtin-functions.cpp
===
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -221,3 +221,31 @@
 namespace fabs {
   static_assert(__builtin_fabs(-14.0) == 14.0, "");
 }
+
+namespace std {
+struct source_location {
+  struct __impl {
+unsigned int _M_line;
+const char *_M_file_name;
+signed char _M_column;
+const char *_M_function_name;
+  };
+  using BuiltinT = decltype(__builtin_source_location()); // OK.
+};
+}
+
+namespace SourceLocation {
+  constexpr auto A = __builtin_source_location();
+  static_assert(A->_M_line == __LINE__ -1, "");
+  static_assert(A->_M_column == 22, "");
+  static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
+  static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
+
+  static_assert(__builtin_LINE() == __LINE__, "");
+
+  struct Foo {
+int a = __builtin_LINE();
+  };
+
+  static_assert(Foo{}.a == __LINE__, "");
+}
Index: clang/lib/AST/Interp/Program.cpp
===
--- clang/lib/AST/Interp/Program.cpp
+++ clang/lib/AST/Interp/Program.cpp
@@ -161,9 +161,12 @@
   const Expr *Init) {
   assert(!getGlobal(VD));
   bool IsStatic, IsExtern;
-  if (auto *Var = dyn_cast(VD)) {
+  if (const auto *Var = dyn_cast(VD)) {
 IsStatic = Context::shouldBeGloballyIndexed(VD);
 IsExtern = !Var->getAnyInitializer();
+  } else if (isa(VD)) {
+IsStatic = true;
+IsExtern = false;
   } else {
 IsStatic = false;
 IsExtern = true;
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -35,6 +35,7 @@
 template  class DeclScope;
 template  class OptionScope;
 template  class ArrayIndexScope;
+template  class SourceLocScope;
 
 /// Compilation context for expressions.
 template 
@@ -102,6 +103,7 @@
   bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
   bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
   bool VisitCXXConstructExpr(const CXXConstructExpr *E);
+  bool VisitSourceLocExpr(const SourceLocExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -154,6 +156,8 @@
 
   /// Creates and initializes a variable from the given decl.
   bool visitVarDecl(const VarDecl *VD);
+  /// Visit an APValue.
+  bool visitAPValue(const APValue , PrimType ValType, const Expr *E);
 
   /// Visits an expression and converts it to a boolean.
   bool visitBool(const Expr *E);
@@ -210,6 +214,7 @@
   friend class DeclScope;
   friend class OptionScope;
   friend class ArrayIndexScope;
+  friend class SourceLocScope;
 
   /// Emits a zero initializer.
   bool visitZeroInitializer(QualType QT, const Expr *E);
@@ -239,12 +244,14 @@
   llvm::function_ref Indirect);
 
   /// Emits an APSInt constant.
+  bool emitConst(const llvm::APSInt , PrimType Ty, const Expr *E);
   bool emitConst(const llvm::APSInt , const Expr *E);
   bool emitConst(const llvm::APInt , const Expr *E) {
 return emitConst(static_cast(Value), E);
   }
 
   /// Emits an integer constant.
+  template  bool emitConst(T Value, PrimType Ty, const Expr *E);
   template  bool emitConst(T Value, const Expr *E);
 
   /// Returns the CXXRecordDecl for the type of the given expression,
@@ -285,6 +292,9 @@
   /// Current argument index. Needed to emit ArrayInitIndexExpr.
   std::optional ArrayIndex;
 
+  /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
+  const Expr *SourceLocDefaultExpr = nullptr;
+
   /// Flag indicating if return value is to be discarded.
   bool DiscardResult = false;
 
@@ -444,6 +454,28 @@
   std::optional OldArrayIndex;
 };
 
+template  class SourceLocScope final {
+public:
+  SourceLocScope(ByteCodeExprGen *Ctx, const Expr *DefaultExpr)
+  : Ctx(Ctx) {
+assert(DefaultExpr);
+// We only switch if the current SourceLocDefaultExpr is null.
+if (!Ctx->SourceLocDefaultExpr) {
+  Enabled = true;
+  Ctx->SourceLocDefaultExpr = DefaultExpr;
+}
+  }
+
+  ~SourceLocScope() {
+if (Enabled)
+  Ctx->SourceLocDefaultExpr = nullptr;
+  }
+
+private:
+  ByteCodeExprGen *Ctx;
+  bool Enabled = false;
+};
+
 } // namespace interp
 } // namespace clang
 
Index: 

[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-09-05 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

I do in https://reviews.llvm.org/D156045, but that depends on other commits 
(not sure which exactly right now though).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-09-05 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added a comment.

So you can enable some of the sema source location tests?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-09-05 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

Ping


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-20 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder updated this revision to Diff 551854.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/lib/AST/Interp/Program.cpp
  clang/test/AST/Interp/builtin-functions.cpp

Index: clang/test/AST/Interp/builtin-functions.cpp
===
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -221,3 +221,31 @@
 namespace fabs {
   static_assert(__builtin_fabs(-14.0) == 14.0, "");
 }
+
+namespace std {
+struct source_location {
+  struct __impl {
+unsigned int _M_line;
+const char *_M_file_name;
+signed char _M_column;
+const char *_M_function_name;
+  };
+  using BuiltinT = decltype(__builtin_source_location()); // OK.
+};
+}
+
+namespace SourceLocation {
+  constexpr auto A = __builtin_source_location();
+  static_assert(A->_M_line == __LINE__ -1, "");
+  static_assert(A->_M_column == 22, "");
+  static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
+  static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
+
+  static_assert(__builtin_LINE() == __LINE__, "");
+
+  struct Foo {
+int a = __builtin_LINE();
+  };
+
+  static_assert(Foo{}.a == __LINE__, "");
+}
Index: clang/lib/AST/Interp/Program.cpp
===
--- clang/lib/AST/Interp/Program.cpp
+++ clang/lib/AST/Interp/Program.cpp
@@ -161,9 +161,12 @@
   const Expr *Init) {
   assert(!getGlobal(VD));
   bool IsStatic, IsExtern;
-  if (auto *Var = dyn_cast(VD)) {
+  if (const auto *Var = dyn_cast(VD)) {
 IsStatic = Context::shouldBeGloballyIndexed(VD);
 IsExtern = !Var->getAnyInitializer();
+  } else if (isa(VD)) {
+IsStatic = true;
+IsExtern = false;
   } else {
 IsStatic = false;
 IsExtern = true;
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -35,6 +35,7 @@
 template  class DeclScope;
 template  class OptionScope;
 template  class ArrayIndexScope;
+template  class SourceLocScope;
 
 /// Compilation context for expressions.
 template 
@@ -102,6 +103,7 @@
   bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
   bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
   bool VisitCXXConstructExpr(const CXXConstructExpr *E);
+  bool VisitSourceLocExpr(const SourceLocExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -154,6 +156,8 @@
 
   /// Creates and initializes a variable from the given decl.
   bool visitVarDecl(const VarDecl *VD);
+  /// Visit an APValue.
+  bool visitAPValue(const APValue , PrimType ValType, const Expr *E);
 
   /// Visits an expression and converts it to a boolean.
   bool visitBool(const Expr *E);
@@ -210,6 +214,7 @@
   friend class DeclScope;
   friend class OptionScope;
   friend class ArrayIndexScope;
+  friend class SourceLocScope;
 
   /// Emits a zero initializer.
   bool visitZeroInitializer(QualType QT, const Expr *E);
@@ -238,12 +243,14 @@
   llvm::function_ref Indirect);
 
   /// Emits an APSInt constant.
+  bool emitConst(const llvm::APSInt , PrimType Ty, const Expr *E);
   bool emitConst(const llvm::APSInt , const Expr *E);
   bool emitConst(const llvm::APInt , const Expr *E) {
 return emitConst(static_cast(Value), E);
   }
 
   /// Emits an integer constant.
+  template  bool emitConst(T Value, PrimType Ty, const Expr *E);
   template  bool emitConst(T Value, const Expr *E);
 
   /// Returns the CXXRecordDecl for the type of the given expression,
@@ -284,6 +291,9 @@
   /// Current argument index. Needed to emit ArrayInitIndexExpr.
   std::optional ArrayIndex;
 
+  /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
+  const Expr *SourceLocDefaultExpr = nullptr;
+
   /// Flag indicating if return value is to be discarded.
   bool DiscardResult = false;
 
@@ -443,6 +453,28 @@
   std::optional OldArrayIndex;
 };
 
+template  class SourceLocScope final {
+public:
+  SourceLocScope(ByteCodeExprGen *Ctx, const Expr *DefaultExpr)
+  : Ctx(Ctx) {
+assert(DefaultExpr);
+// We only switch if the current SourceLocDefaultExpr is null.
+if (!Ctx->SourceLocDefaultExpr) {
+  Enabled = true;
+  Ctx->SourceLocDefaultExpr = DefaultExpr;
+}
+  }
+
+  ~SourceLocScope() {
+if (Enabled)
+  Ctx->SourceLocDefaultExpr = nullptr;
+  }
+
+private:
+  ByteCodeExprGen *Ctx;
+  bool Enabled = false;
+};
+
 } // namespace interp
 } // namespace clang
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1456,6 +1456,62 @@
   return 

[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-20 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

All the dependencies have been pushed.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-18 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D155627#4598032 , @tbaeder wrote:

> In D155627#4596321 , @aaron.ballman 
> wrote:
>
>> In D155627#4595089 , @tbaeder 
>> wrote:
>>
>>> Having https://reviews.llvm.org/D144457 and 
>>> https://reviews.llvm.org/D144457 approved would make rebasing easier :)
>>
>> I can look into https://reviews.llvm.org/D144457, but then having to do 
>> https://reviews.llvm.org/D144457 right after is going to be tough. (I 
>> suspect there may be a copy pasta here.) ;-)
>
> But once you've reviewed https://reviews.llvm.org/D144457, reviewing 
> https://reviews.llvm.org/D144457 shouldn't be that hard? :)

I dunno, I'm skeptical.

> I'm pretty sure the second link should've been 
> https://reviews.llvm.org/D152132.

Oh, so now we're moving the goalposts too? ;-) I'll check that one out next, 
thanks for clarifying!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-18 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

In D155627#4596321 , @aaron.ballman 
wrote:

> In D155627#4595089 , @tbaeder wrote:
>
>> Having https://reviews.llvm.org/D144457 and https://reviews.llvm.org/D144457 
>> approved would make rebasing easier :)
>
> I can look into https://reviews.llvm.org/D144457, but then having to do 
> https://reviews.llvm.org/D144457 right after is going to be tough. (I suspect 
> there may be a copy pasta here.) ;-)

But once you've reviewed https://reviews.llvm.org/D144457, reviewing 
https://reviews.llvm.org/D144457 shouldn't be that hard? :)

I'm pretty sure the second link should've been https://reviews.llvm.org/D152132.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-17 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

In D155627#4595089 , @tbaeder wrote:

> Having https://reviews.llvm.org/D144457 and https://reviews.llvm.org/D144457 
> approved would make rebasing easier :)

I can look into https://reviews.llvm.org/D144457, but then having to do 
https://reviews.llvm.org/D144457 right after is going to be tough. (I suspect 
there may be a copy pasta here.) ;-)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-17 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

Having https://reviews.llvm.org/D144457 and https://reviews.llvm.org/D144457 
approved would make rebasing easier :)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-08-17 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added a comment.

@tbaeder Maybe you could land the dependencies of this patch so that we can 
progress it. Thanks!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-28 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added a comment.

FYI i think this is fine but I'm waiting for progress on the PRs that would 
allow us to enable more tests.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-22 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

@cor3ntin Ok, I need...

- https://reviews.llvm.org/D155707
- https://reviews.llvm.org/D156027
- https://reviews.llvm.org/D156042

to enable the existing tests with at least one of the RUN lines.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-22 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder updated this revision to Diff 543249.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/lib/AST/Interp/Program.cpp
  clang/test/AST/Interp/builtin-functions.cpp

Index: clang/test/AST/Interp/builtin-functions.cpp
===
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -133,3 +133,31 @@
   _Complex double CD = __arithmetic_fence(CD);
 #endif
 }
+
+namespace std {
+struct source_location {
+  struct __impl {
+unsigned int _M_line;
+const char *_M_file_name;
+signed char _M_column;
+const char *_M_function_name;
+  };
+  using BuiltinT = decltype(__builtin_source_location()); // OK.
+};
+}
+
+namespace SourceLocation {
+  constexpr auto A = __builtin_source_location();
+  static_assert(A->_M_line == __LINE__ -1, "");
+  static_assert(A->_M_column == 22, "");
+  static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
+  static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
+
+  static_assert(__builtin_LINE() == __LINE__, "");
+
+  struct Foo {
+int a = __builtin_LINE();
+  };
+
+  static_assert(Foo{}.a == __LINE__, "");
+}
Index: clang/lib/AST/Interp/Program.cpp
===
--- clang/lib/AST/Interp/Program.cpp
+++ clang/lib/AST/Interp/Program.cpp
@@ -161,9 +161,12 @@
   const Expr *Init) {
   assert(!getGlobal(VD));
   bool IsStatic, IsExtern;
-  if (auto *Var = dyn_cast(VD)) {
+  if (const auto *Var = dyn_cast(VD)) {
 IsStatic = Context::shouldBeGloballyIndexed(VD);
 IsExtern = !Var->getAnyInitializer();
+  } else if (isa(VD)) {
+IsStatic = true;
+IsExtern = false;
   } else {
 IsStatic = false;
 IsExtern = true;
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -35,6 +35,7 @@
 template  class DeclScope;
 template  class OptionScope;
 template  class ArrayIndexScope;
+template  class SourceLocScope;
 
 /// Compilation context for expressions.
 template 
@@ -100,6 +101,7 @@
   bool VisitPredefinedExpr(const PredefinedExpr *E);
   bool VisitCXXThrowExpr(const CXXThrowExpr *E);
   bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
+  bool VisitSourceLocExpr(const SourceLocExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -148,6 +150,8 @@
   bool visitRecordInitializer(const Expr *Initializer);
   /// Creates and initializes a variable from the given decl.
   bool visitVarDecl(const VarDecl *VD);
+  /// Visit an APValue.
+  bool visitAPValue(const APValue , PrimType ValType, const Expr *E);
 
   /// Visits an expression and converts it to a boolean.
   bool visitBool(const Expr *E);
@@ -207,6 +211,7 @@
   friend class DeclScope;
   friend class OptionScope;
   friend class ArrayIndexScope;
+  friend class SourceLocScope;
 
   /// Emits a zero initializer.
   bool visitZeroInitializer(QualType QT, const Expr *E);
@@ -235,12 +240,14 @@
   llvm::function_ref Indirect);
 
   /// Emits an APSInt constant.
+  bool emitConst(const llvm::APSInt , PrimType Ty, const Expr *E);
   bool emitConst(const llvm::APSInt , const Expr *E);
   bool emitConst(const llvm::APInt , const Expr *E) {
 return emitConst(static_cast(Value), E);
   }
 
   /// Emits an integer constant.
+  template  bool emitConst(T Value, PrimType Ty, const Expr *E);
   template  bool emitConst(T Value, const Expr *E);
 
   /// Returns the CXXRecordDecl for the type of the given expression,
@@ -281,6 +288,9 @@
   /// Current argument index. Needed to emit ArrayInitIndexExpr.
   std::optional ArrayIndex;
 
+  /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
+  const Expr *SourceLocDefaultExpr = nullptr;
+
   /// Flag indicating if return value is to be discarded.
   bool DiscardResult = false;
 };
@@ -436,6 +446,28 @@
   std::optional OldArrayIndex;
 };
 
+template  class SourceLocScope final {
+public:
+  SourceLocScope(ByteCodeExprGen *Ctx, const Expr *DefaultExpr)
+  : Ctx(Ctx) {
+assert(DefaultExpr);
+// We only switch if the current SourceLocDefaultExpr is null.
+if (!Ctx->SourceLocDefaultExpr) {
+  Enabled = true;
+  Ctx->SourceLocDefaultExpr = DefaultExpr;
+}
+  }
+
+  ~SourceLocScope() {
+if (Enabled)
+  Ctx->SourceLocDefaultExpr = nullptr;
+  }
+
+private:
+  ByteCodeExprGen *Ctx;
+  bool Enabled = false;
+};
+
 } // namespace interp
 } // namespace clang
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ 

[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-22 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder updated this revision to Diff 543212.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/lib/AST/Interp/Program.cpp
  clang/test/AST/Interp/builtin-functions.cpp

Index: clang/test/AST/Interp/builtin-functions.cpp
===
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -133,3 +133,31 @@
   _Complex double CD = __arithmetic_fence(CD);
 #endif
 }
+
+namespace std {
+struct source_location {
+  struct __impl {
+unsigned int _M_line;
+const char *_M_file_name;
+signed char _M_column;
+const char *_M_function_name;
+  };
+  using BuiltinT = decltype(__builtin_source_location()); // OK.
+};
+}
+
+namespace SourceLocation {
+  constexpr auto A = __builtin_source_location();
+  static_assert(A->_M_line == 150, "");
+  static_assert(A->_M_column == 22, "");
+  static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
+  static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
+
+  static_assert(__builtin_LINE() == 156, "");
+
+  struct Foo {
+int a = __builtin_LINE();
+  };
+
+  static_assert(Foo{}.a == 162, "");
+}
Index: clang/lib/AST/Interp/Program.cpp
===
--- clang/lib/AST/Interp/Program.cpp
+++ clang/lib/AST/Interp/Program.cpp
@@ -161,9 +161,12 @@
   const Expr *Init) {
   assert(!getGlobal(VD));
   bool IsStatic, IsExtern;
-  if (auto *Var = dyn_cast(VD)) {
+  if (const auto *Var = dyn_cast(VD)) {
 IsStatic = Context::shouldBeGloballyIndexed(VD);
 IsExtern = !Var->getAnyInitializer();
+  } else if (isa(VD)) {
+IsStatic = true;
+IsExtern = false;
   } else {
 IsStatic = false;
 IsExtern = true;
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -35,6 +35,7 @@
 template  class DeclScope;
 template  class OptionScope;
 template  class ArrayIndexScope;
+template  class SourceLocScope;
 
 /// Compilation context for expressions.
 template 
@@ -100,6 +101,7 @@
   bool VisitPredefinedExpr(const PredefinedExpr *E);
   bool VisitCXXThrowExpr(const CXXThrowExpr *E);
   bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
+  bool VisitSourceLocExpr(const SourceLocExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -148,6 +150,8 @@
   bool visitRecordInitializer(const Expr *Initializer);
   /// Creates and initializes a variable from the given decl.
   bool visitVarDecl(const VarDecl *VD);
+  /// Visit an APValue.
+  bool visitAPValue(const APValue , PrimType ValType, const Expr *E);
 
   /// Visits an expression and converts it to a boolean.
   bool visitBool(const Expr *E);
@@ -207,6 +211,7 @@
   friend class DeclScope;
   friend class OptionScope;
   friend class ArrayIndexScope;
+  friend class SourceLocScope;
 
   /// Emits a zero initializer.
   bool visitZeroInitializer(QualType QT, const Expr *E);
@@ -235,12 +240,14 @@
   llvm::function_ref Indirect);
 
   /// Emits an APSInt constant.
+  bool emitConst(const llvm::APSInt , PrimType Ty, const Expr *E);
   bool emitConst(const llvm::APSInt , const Expr *E);
   bool emitConst(const llvm::APInt , const Expr *E) {
 return emitConst(static_cast(Value), E);
   }
 
   /// Emits an integer constant.
+  template  bool emitConst(T Value, PrimType Ty, const Expr *E);
   template  bool emitConst(T Value, const Expr *E);
 
   /// Returns the CXXRecordDecl for the type of the given expression,
@@ -281,6 +288,9 @@
   /// Current argument index. Needed to emit ArrayInitIndexExpr.
   std::optional ArrayIndex;
 
+  /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
+  const Expr *SourceLocDefaultExpr = nullptr;
+
   /// Flag indicating if return value is to be discarded.
   bool DiscardResult = false;
 };
@@ -436,6 +446,28 @@
   std::optional OldArrayIndex;
 };
 
+template  class SourceLocScope final {
+public:
+  SourceLocScope(ByteCodeExprGen *Ctx, const Expr *DefaultExpr)
+  : Ctx(Ctx) {
+assert(DefaultExpr);
+// We only switch if the current SourceLocDefaultExpr is null.
+if (!Ctx->SourceLocDefaultExpr) {
+  Enabled = true;
+  Ctx->SourceLocDefaultExpr = DefaultExpr;
+}
+  }
+
+  ~SourceLocScope() {
+if (Enabled)
+  Ctx->SourceLocDefaultExpr = nullptr;
+  }
+
+private:
+  ByteCodeExprGen *Ctx;
+  bool Enabled = false;
+};
+
 } // namespace interp
 } // namespace clang
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1138,6 +1138,62 

[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-20 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder updated this revision to Diff 542420.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/lib/AST/Interp/Program.cpp
  clang/test/AST/Interp/builtin-functions.cpp

Index: clang/test/AST/Interp/builtin-functions.cpp
===
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -133,3 +133,31 @@
   _Complex double CD = __arithmetic_fence(CD);
 #endif
 }
+
+namespace std {
+struct source_location {
+  struct __impl {
+unsigned int _M_line;
+const char *_M_file_name;
+signed char _M_column;
+const char *_M_function_name;
+  };
+  using BuiltinT = decltype(__builtin_source_location()); // OK.
+};
+}
+
+namespace SourceLocation {
+  constexpr auto A = __builtin_source_location();
+  static_assert(A->_M_line == 150, "");
+  static_assert(A->_M_column == 22, "");
+  static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
+  static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
+
+  static_assert(__builtin_LINE() == 156, "");
+
+  struct Foo {
+int a = __builtin_LINE();
+  };
+
+  static_assert(Foo{}.a == 162, "");
+}
Index: clang/lib/AST/Interp/Program.cpp
===
--- clang/lib/AST/Interp/Program.cpp
+++ clang/lib/AST/Interp/Program.cpp
@@ -161,9 +161,12 @@
   const Expr *Init) {
   assert(!getGlobal(VD));
   bool IsStatic, IsExtern;
-  if (auto *Var = dyn_cast(VD)) {
+  if (const auto *Var = dyn_cast(VD)) {
 IsStatic = Context::shouldBeGloballyIndexed(VD);
 IsExtern = !Var->getAnyInitializer();
+  } else if (isa(VD)) {
+IsStatic = true;
+IsExtern = false;
   } else {
 IsStatic = false;
 IsExtern = true;
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -35,6 +35,7 @@
 template  class DeclScope;
 template  class OptionScope;
 template  class ArrayIndexScope;
+template  class SourceLocScope;
 
 /// Compilation context for expressions.
 template 
@@ -100,6 +101,7 @@
   bool VisitPredefinedExpr(const PredefinedExpr *E);
   bool VisitCXXThrowExpr(const CXXThrowExpr *E);
   bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
+  bool VisitSourceLocExpr(const SourceLocExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -148,6 +150,8 @@
   bool visitRecordInitializer(const Expr *Initializer);
   /// Creates and initializes a variable from the given decl.
   bool visitVarDecl(const VarDecl *VD);
+  /// Visit an APValue.
+  bool visitAPValue(const APValue , PrimType ValType, const Expr *E);
 
   /// Visits an expression and converts it to a boolean.
   bool visitBool(const Expr *E);
@@ -207,6 +211,7 @@
   friend class DeclScope;
   friend class OptionScope;
   friend class ArrayIndexScope;
+  friend class SourceLocScope;
 
   /// Emits a zero initializer.
   bool visitZeroInitializer(QualType QT, const Expr *E);
@@ -235,12 +240,14 @@
   llvm::function_ref Indirect);
 
   /// Emits an APSInt constant.
+  bool emitConst(const llvm::APSInt , PrimType Ty, const Expr *E);
   bool emitConst(const llvm::APSInt , const Expr *E);
   bool emitConst(const llvm::APInt , const Expr *E) {
 return emitConst(static_cast(Value), E);
   }
 
   /// Emits an integer constant.
+  template  bool emitConst(T Value, PrimType Ty, const Expr *E);
   template  bool emitConst(T Value, const Expr *E);
 
   /// Returns the CXXRecordDecl for the type of the given expression,
@@ -281,6 +288,9 @@
   /// Current argument index. Needed to emit ArrayInitIndexExpr.
   std::optional ArrayIndex;
 
+  /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
+  const Expr *SourceLocDefaultExpr = nullptr;
+
   /// Flag indicating if return value is to be discarded.
   bool DiscardResult = false;
 };
@@ -436,6 +446,25 @@
   std::optional OldArrayIndex;
 };
 
+template  class SourceLocScope final {
+public:
+  SourceLocScope(ByteCodeExprGen *Ctx, const Expr *DefaultExpr)
+  : Ctx(Ctx) {
+assert(DefaultExpr);
+// We only switch if the current SourceLocDefaultExpr is null.
+if (!Ctx->SourceLocDefaultExpr) {
+  OldDefaultExpr = nullptr;
+  Ctx->SourceLocDefaultExpr = DefaultExpr;
+}
+  }
+
+  ~SourceLocScope() { Ctx->SourceLocDefaultExpr = OldDefaultExpr; }
+
+private:
+  ByteCodeExprGen *Ctx;
+  const Expr *OldDefaultExpr;
+};
+
 } // namespace interp
 } // namespace clang
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1138,6 +1138,62 @@
   

[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-20 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added inline comments.



Comment at: clang/test/AST/Interp/builtin-functions.cpp:153
+  static_assert(__builtin_LINE() == 152, "");
+}

cor3ntin wrote:
> tbaeder wrote:
> > tbaeder wrote:
> > > cor3ntin wrote:
> > > > what is missing to enable the existing source location tests with the 
> > > > new intterpreter?
> > > There is a crash that I need to investigate first, //but// 
> > > `SemaCXX/source_location.cpp` also uses stuff like 
> > > `static_assert(noexcept(...));` that I don't handle yet.
> > Okay, after my last two patches I uploaded, the only problem missing is the 
> > broken source locations for initializers.
> I think it might be worth doing in this patch (unless it requires a lot of 
> work)
I was wrong and there's some other work needed. Namely, I need to redo how we 
do initializers. I will definitely tackel that this quarter and dial down the 
work on new features.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-19 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added inline comments.



Comment at: clang/test/AST/Interp/builtin-functions.cpp:153
+  static_assert(__builtin_LINE() == 152, "");
+}

tbaeder wrote:
> tbaeder wrote:
> > cor3ntin wrote:
> > > what is missing to enable the existing source location tests with the new 
> > > intterpreter?
> > There is a crash that I need to investigate first, //but// 
> > `SemaCXX/source_location.cpp` also uses stuff like 
> > `static_assert(noexcept(...));` that I don't handle yet.
> Okay, after my last two patches I uploaded, the only problem missing is the 
> broken source locations for initializers.
I think it might be worth doing in this patch (unless it requires a lot of work)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-19 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added inline comments.



Comment at: clang/test/AST/Interp/builtin-functions.cpp:153
+  static_assert(__builtin_LINE() == 152, "");
+}

tbaeder wrote:
> cor3ntin wrote:
> > what is missing to enable the existing source location tests with the new 
> > intterpreter?
> There is a crash that I need to investigate first, //but// 
> `SemaCXX/source_location.cpp` also uses stuff like 
> `static_assert(noexcept(...));` that I don't handle yet.
Okay, after my last two patches I uploaded, the only problem missing is the 
broken source locations for initializers.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-19 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder added a comment.

In D155627#4513433 , @cor3ntin wrote:

> Note that you are probably need something like `SourceLocExprScopeGuard` to 
> make source_location actually work, , it might be worth doing in this patch.
> The idea is to remember the location of a call when evaluating default 
> arguments / default member initializers

Yeah I saw, that's what the FIXME comment is about.




Comment at: clang/test/AST/Interp/builtin-functions.cpp:153
+  static_assert(__builtin_LINE() == 152, "");
+}

cor3ntin wrote:
> what is missing to enable the existing source location tests with the new 
> intterpreter?
There is a crash that I need to investigate first, //but// 
`SemaCXX/source_location.cpp` also uses stuff like 
`static_assert(noexcept(...));` that I don't handle yet.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-19 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added inline comments.



Comment at: clang/test/AST/Interp/builtin-functions.cpp:153
+  static_assert(__builtin_LINE() == 152, "");
+}

what is missing to enable the existing source location tests with the new 
intterpreter?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-19 Thread Corentin Jabot via Phabricator via cfe-commits
cor3ntin added a comment.

Note that you are probably need something like `SourceLocExprScopeGuard` to 
make source_location actually work, , it might be worth doing in this patch.
The idea is to remember the location of a call when evaluating default 
arguments / default member initializers


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155627/new/

https://reviews.llvm.org/D155627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155627: [clang][Interp] Handle SourceLocExprs

2023-07-18 Thread Timm Bäder via Phabricator via cfe-commits
tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is a bit awkward, since we get an `APValue` from the expression, which we 
then need to re-integrate into our bytecode/primtype based interpreter.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155627

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/ByteCodeExprGen.h
  clang/lib/AST/Interp/Program.cpp
  clang/test/AST/Interp/builtin-functions.cpp

Index: clang/test/AST/Interp/builtin-functions.cpp
===
--- clang/test/AST/Interp/builtin-functions.cpp
+++ clang/test/AST/Interp/builtin-functions.cpp
@@ -129,3 +129,25 @@
   _Complex double CD = __arithmetic_fence(CD);
 #endif
 }
+
+namespace std {
+struct source_location {
+  struct __impl {
+unsigned int _M_line;
+const char *_M_file_name;
+signed char _M_column;
+const char *_M_function_name;
+  };
+  using BuiltinT = decltype(__builtin_source_location()); // OK.
+};
+}
+
+namespace SourceLocation {
+  constexpr auto A = __builtin_source_location();
+  static_assert(A->_M_line == 146, "");
+  static_assert(A->_M_column == 22, "");
+  static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, "");
+  static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, "");
+
+  static_assert(__builtin_LINE() == 152, "");
+}
Index: clang/lib/AST/Interp/Program.cpp
===
--- clang/lib/AST/Interp/Program.cpp
+++ clang/lib/AST/Interp/Program.cpp
@@ -161,9 +161,12 @@
   const Expr *Init) {
   assert(!getGlobal(VD));
   bool IsStatic, IsExtern;
-  if (auto *Var = dyn_cast(VD)) {
+  if (const auto *Var = dyn_cast(VD)) {
 IsStatic = Context::shouldBeGloballyIndexed(VD);
 IsExtern = !Var->getAnyInitializer();
+  } else if (isa(VD)) {
+IsStatic = true;
+IsExtern = false;
   } else {
 IsStatic = false;
 IsExtern = true;
Index: clang/lib/AST/Interp/ByteCodeExprGen.h
===
--- clang/lib/AST/Interp/ByteCodeExprGen.h
+++ clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -100,6 +100,7 @@
   bool VisitPredefinedExpr(const PredefinedExpr *E);
   bool VisitCXXThrowExpr(const CXXThrowExpr *E);
   bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
+  bool VisitSourceLocExpr(const SourceLocExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;
@@ -148,6 +149,8 @@
   bool visitRecordInitializer(const Expr *Initializer);
   /// Creates and initializes a variable from the given decl.
   bool visitVarDecl(const VarDecl *VD);
+  /// Visit an APValue.
+  bool visitAPValue(const APValue , PrimType ValType, const Expr *E);
 
   /// Visits an expression and converts it to a boolean.
   bool visitBool(const Expr *E);
@@ -235,12 +238,14 @@
   llvm::function_ref Indirect);
 
   /// Emits an APSInt constant.
+  bool emitConst(const llvm::APSInt , PrimType Ty, const Expr *E);
   bool emitConst(const llvm::APSInt , const Expr *E);
   bool emitConst(const llvm::APInt , const Expr *E) {
 return emitConst(static_cast(Value), E);
   }
 
   /// Emits an integer constant.
+  template  bool emitConst(T Value, PrimType Ty, const Expr *E);
   template  bool emitConst(T Value, const Expr *E);
 
   /// Returns the CXXRecordDecl for the type of the given expression,
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1136,6 +1136,61 @@
   return this->emitInvalidCast(CastKind::Reinterpret, E);
 }
 
+template 
+bool ByteCodeExprGen::VisitSourceLocExpr(const SourceLocExpr *E) {
+  if (DiscardResult)
+return true;
+
+  // FIXME: Supply default expression.
+  const APValue Val = E->EvaluateInContext(Ctx.getASTContext(), nullptr);
+  // Things like __builtin_LINE().
+  if (E->getType()->isIntegerType()) {
+assert(Val.isInt());
+const APSInt  = Val.getInt();
+return this->emitConst(I, E);
+  }
+  // Otherwise, the APValue is an LValue, with only one element.
+  // Theoretically, we don't need the APValue at all of course.
+  assert(E->getType()->isPointerType());
+  assert(Val.isLValue());
+  const APValue::LValueBase  = Val.getLValueBase();
+  if (const Expr *LValueExpr = Base.dyn_cast())
+return this->visit(LValueExpr);
+
+  // Otherwise, we have a decl (which is the case for
+  // __builtin_source_location).
+  assert(Base.is());
+  assert(Val.getLValuePath().size() == 0);
+  const auto *BaseDecl = Base.dyn_cast();
+  assert(BaseDecl);
+
+  auto *UGCD = cast(BaseDecl);
+
+  std::optional GlobalIndex = P.getOrCreateGlobal(UGCD);
+  if