[clang] 4d7f4a7 - [clang][Interp] Only lazily visit constant globals

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 5 00:37:47 PDT 2023


Author: Timm Bäder
Date: 2023-10-05T09:37:37+02:00
New Revision: 4d7f4a7c82cb742dd9711cce1cfbe574ac08c068

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

LOG: [clang][Interp] Only lazily visit constant globals

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

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 f4ab30a5c8e7238..00d3b469d9049f6 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2536,7 +2536,8 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
   // This happens in C.
   if (!Ctx.getLangOpts().CPlusPlus) {
     if (const auto *VD = dyn_cast<VarDecl>(D);
-        VD && VD->hasGlobalStorage() && VD->getAnyInitializer()) {
+        VD && VD->hasGlobalStorage() && VD->getAnyInitializer() &&
+        VD->getType().isConstQualified()) {
       if (!this->visitVarDecl(VD))
         return false;
       // Retry.

diff  --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index c0ec5f8339dd1d7..47058b8a93203b6 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -14,11 +14,15 @@ _Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant exp
                            // pedantic-expected-warning {{not an integer constant expression}}
 _Static_assert(!!1, "");
 
-/// FIXME: Should also be rejected in the new interpreter
-int a = (1 == 1 ? 5 : 3);
+int a = (1 == 1 ? 5 : 3); // expected-note {{declared here}} \
+                          // pedantic-expected-note {{declared here}}
 _Static_assert(a == 5, ""); // ref-error {{not an integral constant expression}} \
                             // pedantic-ref-error {{not an integral constant expression}} \
-                            // pedantic-expected-warning {{not an integer constant expression}}
+                            // expected-error {{not an integral constant expression}} \
+                            // expected-note {{read of non-const variable}} \
+                            // pedantic-expected-error {{not an integral constant expression}} \
+                            // pedantic-expected-note {{read of non-const variable}}
+
 
 const int b = 3;
 _Static_assert(b == 3, ""); // pedantic-ref-warning {{not an integer constant expression}} \


        


More information about the cfe-commits mailing list