[clang] 6d73cca - [clang][Interp] Lazily visit unknown global declarations

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 23:50:30 PDT 2023


Author: Timm Bäder
Date: 2023-09-15T08:50:21+02:00
New Revision: 6d73cca1864b78cbc3944eb92b6916df36061eb9

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

LOG: [clang][Interp] Lazily visit unknown global declarations

In C, we don't get a evaluateAsInitializer() call for all global
declarations, yet we have to handle DeclRefExpr pointing to them.

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

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 4fa12a66bbf767f..63c184db2be38f7 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2459,6 +2459,17 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
     return this->emitGetPtrThisField(Offset, E);
   }
 
+  // Lazily visit global declarations we haven't seen yet.
+  // This happens in C.
+  if (!Ctx.getLangOpts().CPlusPlus) {
+    if (const auto *VD = dyn_cast<VarDecl>(D);
+        VD && VD->hasGlobalStorage() && VD->getAnyInitializer()) {
+      if (!this->visitVarDecl(VD))
+        return false;
+      // Retry.
+      return this->VisitDeclRefExpr(E);
+    }
+  }
   return false;
 }
 

diff  --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index fe794bb014e7c4b..24d0300baa44218 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -1,12 +1,23 @@
 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -pedantic -verify=pedantic-expected %s
 // RUN: %clang_cc1 -verify=ref %s
+// RUN: %clang_cc1 -pedantic -verify=pedantic-ref %s
 
 /// expected-no-diagnostics
-/// ref-no-diagnostics
 
 _Static_assert(1, "");
 _Static_assert(0 != 1, "");
-_Static_assert(1.0 == 1.0, "");
+_Static_assert(1.0 == 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
+                                // pedantic-expected-warning {{not an integer constant expression}}
 _Static_assert( (5 > 4) + (3 > 2) == 2, "");
 
+/// FIXME: Should also be rejected in the new interpreter
 int a = (1 == 1 ? 5 : 3);
+_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}}
+
+const int b = 3;
+_Static_assert(b == 3, ""); // pedantic-ref-warning {{not an integer constant expression}} \
+                            // pedantic-expected-warning {{not an integer constant expression}}
+


        


More information about the cfe-commits mailing list