[PATCH] D123648: Restrict lvalue-to-rvalue conversions in CGExprConstant.

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 12 17:32:42 PDT 2022


efriedma created this revision.
efriedma added reviewers: aaron.ballman, rjmccall, rsmith.
Herald added a project: All.
efriedma requested review of this revision.
Herald added a project: clang.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123648

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


Index: clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
===================================================================
--- clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
+++ clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
@@ -9,7 +9,7 @@
 
 // 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 @@
 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
Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1092,7 +1092,16 @@
                                                              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:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123648.422370.patch
Type: text/x-patch
Size: 1703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220413/028c5a59/attachment.bin>


More information about the cfe-commits mailing list