Restrict lvalue-to-rvalue conversions in CGExprConstant.
authorEli Friedman <efriedma@quicinc.com>
Wed, 13 Apr 2022 00:13:06 +0000 (17:13 -0700)
committerEli Friedman <efriedma@quicinc.com>
Wed, 13 Apr 2022 19:34:57 +0000 (12:34 -0700)
We were generating wrong code for cxx20-consteval-crash.cpp: instead of
loading a value of a variable, we were using its address as the
initializer.

Found while adding code to verify the size of constant initializers.

Differential Revision: https://reviews.llvm.org/D123648

clang/lib/CodeGen/CGExprConstant.cpp
clang/test/CodeGenCXX/cxx20-consteval-crash.cpp

index 6397ff5..92122f0 100644 (file)
@@ -1092,7 +1092,16 @@ public:
                                                              destAS, destTy);
     }
 
-    case CK_LValueToRValue:
+    case CK_LValueToRValue: {
+      // We don't really support doing lvalue-to-rvalue conversions here; any
+      // interesting conversions should be done in Evaluate().  But as a
+      // special case, allow compound literals to support the gcc extension
+      // allowing "struct x {int x;} x = (struct x) {};".
+      if (auto *E = dyn_cast<CompoundLiteralExpr>(subExpr->IgnoreParens()))
+        return Visit(E->getInitializer(), destType);
+      return nullptr;
+    }
+
     case CK_AtomicToNonAtomic:
     case CK_NonAtomicToAtomic:
     case CK_NoOp:
index c8ca6a5..c7f0377 100644 (file)
@@ -9,7 +9,7 @@ auto x2 = X();
 
 // CHECK: @_ZN7PR507872x_E = external global i32, align 4
 // CHECK-NEXT: @_ZN7PR507872x1E = constant i32* @_ZN7PR507872x_E, align 8
-// CHECK-NEXT: @_ZN7PR507872x2E = global i32* @_ZN7PR507872x_E, align 4
+// CHECK-NEXT: @_ZN7PR507872x2E = global i32 0, align 4
 }
 
 namespace PR51484 {
@@ -18,7 +18,7 @@ struct X { int val; };
 consteval X g() { return {0}; }
 void f() { g(); }
 
-// CHECK: define dso_local void @_ZN7PR514841fEv() #0 {
+// CHECK: define dso_local void @_ZN7PR514841fEv() #1 {
 // CHECK: entry:
 // CHECK-NOT: call i32 @_ZN7PR514841gEv()
 // CHECK:  ret void