[clang] 1210738 - [clang][Interp] Handle CXXScalarValueInitExprs (#67147)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 10 03:45:14 PDT 2023


Author: Timm Baeder
Date: 2023-10-10T12:45:09+02:00
New Revision: 12107386ca13f206daf67b06bfe394ba4b2758fb

URL: https://github.com/llvm/llvm-project/commit/12107386ca13f206daf67b06bfe394ba4b2758fb
DIFF: https://github.com/llvm/llvm-project/commit/12107386ca13f206daf67b06bfe394ba4b2758fb.diff

LOG: [clang][Interp] Handle CXXScalarValueInitExprs (#67147)

Handle those by just zero-initializing the result.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/lib/AST/Interp/ByteCodeExprGen.h
    clang/test/AST/Interp/literals.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 02b35583636bf13..9bd040a208b3f95 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1531,6 +1531,12 @@ bool ByteCodeExprGen<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
   return this->emitOffsetOf(T, E, E);
 }
 
+template <class Emitter>
+bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
+    const CXXScalarValueInitExpr *E) {
+  return this->visitZeroInitializer(E->getType(), E);
+}
+
 template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
   if (E->containsErrors())
     return false;

diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.h b/clang/lib/AST/Interp/ByteCodeExprGen.h
index 47a3f75f13459d0..7cfe4d9251c5f05 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.h
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -106,6 +106,7 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
   bool VisitCXXConstructExpr(const CXXConstructExpr *E);
   bool VisitSourceLocExpr(const SourceLocExpr *E);
   bool VisitOffsetOfExpr(const OffsetOfExpr *E);
+  bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
 
 protected:
   bool visitExpr(const Expr *E) override;

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index ceda59405ea9105..a83dcd234111587 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -31,6 +31,33 @@ static_assert(b, "");
 constexpr int one = true;
 static_assert(one == 1, "");
 
+constexpr bool b2 = bool();
+static_assert(!b2, "");
+
+namespace ScalarTypes {
+  constexpr int ScalarInitInt = int();
+  static_assert(ScalarInitInt == 0, "");
+  constexpr float ScalarInitFloat = float();
+  static_assert(ScalarInitFloat == 0.0f, "");
+
+  static_assert(decltype(nullptr)() == nullptr, "");
+
+  template<typename T>
+  constexpr T getScalar() { return T(); }
+
+  static_assert(getScalar<const int>() == 0, "");
+  static_assert(getScalar<const double>() == 0.0, "");
+
+  static_assert(getScalar<void*>() == nullptr, "");
+  static_assert(getScalar<void(*)(void)>() == nullptr, "");
+
+  enum E {
+    First = 0,
+  };
+  static_assert(getScalar<E>() == First, "");
+  /// FIXME: Member pointers.
+}
+
 namespace IntegralCasts {
   constexpr int i = 12;
   constexpr unsigned int ui = i;


        


More information about the cfe-commits mailing list