[clang] 8e2dbab - [clang][bytecode] Fix defining extern variables (#108940)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 17 06:21:32 PDT 2024


Author: Timm Baeder
Date: 2024-09-17T15:21:28+02:00
New Revision: 8e2dbab24276a8521d241463b4161c78bc4d39d2

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

LOG: [clang][bytecode] Fix defining extern variables (#108940)

At the point of defintion of the variable, a function might already
refert to the variable by its index. Replace the index with the new one.

Added: 
    clang/test/AST/ByteCode/extern.cpp

Modified: 
    clang/lib/AST/ByteCode/Program.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index bd5860beabaecd..79c645257306e0 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -204,9 +204,18 @@ std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
     IsStatic = false;
     IsExtern = true;
   }
+
+  // Register all previous declarations as well. For extern blocks, just replace
+  // the index with the new variable.
   if (auto Idx = createGlobal(VD, VD->getType(), IsStatic, IsExtern, Init)) {
-    for (const Decl *P = VD; P; P = P->getPreviousDecl())
+    for (const Decl *P = VD; P; P = P->getPreviousDecl()) {
+      if (P != VD) {
+        unsigned PIdx = GlobalIndices[P];
+        if (Globals[PIdx]->block()->isExtern())
+          Globals[PIdx] = Globals[*Idx];
+      }
       GlobalIndices[P] = *Idx;
+    }
     return *Idx;
   }
   return std::nullopt;

diff  --git a/clang/test/AST/ByteCode/extern.cpp b/clang/test/AST/ByteCode/extern.cpp
new file mode 100644
index 00000000000000..a616269911a7ec
--- /dev/null
+++ b/clang/test/AST/ByteCode/extern.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected %s
+// RUN: %clang_cc1 -verify=both,ref %s
+
+
+// both-no-diagnostics
+
+extern const int E;
+constexpr int getE() {
+  return E;
+}
+const int E = 10;
+static_assert(getE() == 10);
+


        


More information about the cfe-commits mailing list