[clang] b504870 - [clang][Interp] Fix lvalue CompoundLiteralExprs

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 26 07:40:01 PST 2024


Author: Timm Bäder
Date: 2024-02-26T16:39:43+01:00
New Revision: b5048700fc31f3bf6dd32ace7730815d4cfef411

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

LOG: [clang][Interp] Fix lvalue CompoundLiteralExprs

We need to leave a pointer on the stack for them, even if their
type is primitive.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/c.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index e96afb1078cc79..a71b6e82817e4a 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1679,13 +1679,24 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr(
 
   std::optional<PrimType> T = classify(E->getType());
   if (E->isFileScope()) {
+    // Avoid creating a variable if this is a primitive RValue anyway.
+    if (T && !E->isLValue())
+      return this->delegate(Init);
+
     if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
-      if (classify(E->getType()))
-        return this->visit(Init);
       if (!this->emitGetPtrGlobal(*GlobalIndex, E))
         return false;
+
+      if (T) {
+        if (!this->visit(Init))
+          return false;
+        return this->emitInitGlobal(*T, *GlobalIndex, E);
+      }
+
       return this->visitInitializer(Init);
     }
+
+    return false;
   }
 
   // Otherwise, use a local variable.

diff  --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index b67fe93417058e..a6244c3af202a1 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -21,6 +21,9 @@ _Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant exp
                            // pedantic-expected-warning {{not an integer constant expression}}
 _Static_assert(!!1, "");
 
+_Static_assert(!(_Bool){(void*)0}, ""); // pedantic-ref-warning {{not an integer constant expression}} \
+                                        // pedantic-expected-warning {{not an integer constant expression}}
+
 int a = (1 == 1 ? 5 : 3);
 _Static_assert(a == 5, ""); // all-error {{not an integral constant expression}}
 


        


More information about the cfe-commits mailing list