[PATCH] D156794: [clang][Interp] Lazily visit unknown global declarations

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 07:00:59 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156794

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


Index: clang/test/AST/Interp/c.c
===================================================================
--- clang/test/AST/Interp/c.c
+++ clang/test/AST/Interp/c.c
@@ -2,11 +2,15 @@
 // RUN: %clang_cc1 -verify=ref %s
 
 /// expected-no-diagnostics
-/// ref-no-diagnostics
 
 _Static_assert(1, "");
 _Static_assert(0 != 1, "");
 _Static_assert(1.0 == 1.0, "");
 _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}}
+
+const int b = 3;
+_Static_assert(b == 3, "");
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2169,6 +2169,14 @@
     return this->emitGetPtrThisField(Offset, E);
   }
 
+  // Lazily visit global declarations we haven't seen yet.
+  // This happens in C.
+  if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->hasGlobalStorage()) {
+    if (!this->visitVarDecl(VD))
+      return false;
+    // Retry.
+    return this->VisitDeclRefExpr(E);
+  }
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156794.546038.patch
Type: text/x-patch
Size: 1208 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230801/e1329c87/attachment.bin>


More information about the cfe-commits mailing list