Skip to content

Commit 6d73cca

Browse files
committed
[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
1 parent 7b03a55 commit 6d73cca

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,17 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
24592459
return this->emitGetPtrThisField(Offset, E);
24602460
}
24612461

2462+
// Lazily visit global declarations we haven't seen yet.
2463+
// This happens in C.
2464+
if (!Ctx.getLangOpts().CPlusPlus) {
2465+
if (const auto *VD = dyn_cast<VarDecl>(D);
2466+
VD && VD->hasGlobalStorage() && VD->getAnyInitializer()) {
2467+
if (!this->visitVarDecl(VD))
2468+
return false;
2469+
// Retry.
2470+
return this->VisitDeclRefExpr(E);
2471+
}
2472+
}
24622473
return false;
24632474
}
24642475

clang/test/AST/Interp/c.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
2+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -pedantic -verify=pedantic-expected %s
23
// RUN: %clang_cc1 -verify=ref %s
4+
// RUN: %clang_cc1 -pedantic -verify=pedantic-ref %s
35

46
/// expected-no-diagnostics
5-
/// ref-no-diagnostics
67

78
_Static_assert(1, "");
89
_Static_assert(0 != 1, "");
9-
_Static_assert(1.0 == 1.0, "");
10+
_Static_assert(1.0 == 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
11+
// pedantic-expected-warning {{not an integer constant expression}}
1012
_Static_assert( (5 > 4) + (3 > 2) == 2, "");
1113

14+
/// FIXME: Should also be rejected in the new interpreter
1215
int a = (1 == 1 ? 5 : 3);
16+
_Static_assert(a == 5, ""); // ref-error {{not an integral constant expression}} \
17+
// pedantic-ref-error {{not an integral constant expression}} \
18+
// pedantic-expected-warning {{not an integer constant expression}}
19+
20+
const int b = 3;
21+
_Static_assert(b == 3, ""); // pedantic-ref-warning {{not an integer constant expression}} \
22+
// pedantic-expected-warning {{not an integer constant expression}}
23+

0 commit comments

Comments
 (0)