Skip to content

Commit 8e2dbab

Browse files
authored
[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.
1 parent 5fdf07d commit 8e2dbab

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang/lib/AST/ByteCode/Program.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,18 @@ std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
204204
IsStatic = false;
205205
IsExtern = true;
206206
}
207+
208+
// Register all previous declarations as well. For extern blocks, just replace
209+
// the index with the new variable.
207210
if (auto Idx = createGlobal(VD, VD->getType(), IsStatic, IsExtern, Init)) {
208-
for (const Decl *P = VD; P; P = P->getPreviousDecl())
211+
for (const Decl *P = VD; P; P = P->getPreviousDecl()) {
212+
if (P != VD) {
213+
unsigned PIdx = GlobalIndices[P];
214+
if (Globals[PIdx]->block()->isExtern())
215+
Globals[PIdx] = Globals[*Idx];
216+
}
209217
GlobalIndices[P] = *Idx;
218+
}
210219
return *Idx;
211220
}
212221
return std::nullopt;

clang/test/AST/ByteCode/extern.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected %s
2+
// RUN: %clang_cc1 -verify=both,ref %s
3+
4+
5+
// both-no-diagnostics
6+
7+
extern const int E;
8+
constexpr int getE() {
9+
return E;
10+
}
11+
const int E = 10;
12+
static_assert(getE() == 10);
13+

0 commit comments

Comments
 (0)