Skip to content

Commit 030ea6d

Browse files
committed
[clang][Interp] Only check toplevel declarations
1 parent db791b2 commit 030ea6d

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,7 +3460,7 @@ bool Compiler<Emitter>::visitDecl(const VarDecl *VD, bool ConstantContext) {
34603460
}
34613461

34623462
// Create and initialize the variable.
3463-
if (!this->visitVarDecl(VD))
3463+
if (!this->visitVarDecl(VD, /*Toplevel=*/true))
34643464
return false;
34653465

34663466
// Get a pointer to the variable
@@ -3507,7 +3507,7 @@ bool Compiler<Emitter>::visitDecl(const VarDecl *VD, bool ConstantContext) {
35073507
}
35083508

35093509
template <class Emitter>
3510-
VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD) {
3510+
VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD, bool Toplevel) {
35113511
// We don't know what to do with these, so just return false.
35123512
if (VD->getType().isNull())
35133513
return false;
@@ -3521,7 +3521,7 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD) {
35213521
std::optional<PrimType> VarT = classify(VD->getType());
35223522

35233523
auto checkDecl = [&]() -> bool {
3524-
bool NeedsOp = VD->isLocalVarDecl() && VD->isStaticLocal();
3524+
bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
35253525
return !NeedsOp || this->emitCheckDecl(VD, VD);
35263526
};
35273527

@@ -4991,7 +4991,7 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
49914991
if ((VD->hasGlobalStorage() || VD->isLocalVarDecl() ||
49924992
VD->isStaticDataMember()) &&
49934993
typeShouldBeVisited(VD->getType())) {
4994-
auto VarState = this->visitVarDecl(VD);
4994+
auto VarState = this->visitVarDecl(VD, true);
49954995
if (VarState.notCreated())
49964996
return true;
49974997
if (!VarState)
@@ -5004,7 +5004,7 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
50045004
if (const auto *VD = dyn_cast<VarDecl>(D);
50055005
VD && VD->getAnyInitializer() &&
50065006
VD->getType().isConstant(Ctx.getASTContext()) && !VD->isWeak()) {
5007-
auto VarState = this->visitVarDecl(VD);
5007+
auto VarState = this->visitVarDecl(VD, true);
50085008
if (VarState.notCreated())
50095009
return true;
50105010
if (!VarState)

clang/lib/AST/Interp/Compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
260260
/// intact.
261261
bool delegate(const Expr *E);
262262
/// Creates and initializes a variable from the given decl.
263-
VarCreationState visitVarDecl(const VarDecl *VD);
263+
VarCreationState visitVarDecl(const VarDecl *VD, bool Toplevel = false);
264264
/// Visit an APValue.
265265
bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E);
266266
bool visitAPValueInitializer(const APValue &Val, const Expr *E);

clang/test/AST/Interp/c.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,11 @@ void SuperSpecialFunc(void) {
293293
const int SuperSpecialCase = 10;
294294
_Static_assert((sizeof(SuperSpecialCase) == 12 && SuperSpecialCase == 3) || SuperSpecialCase == 10, ""); // pedantic-warning {{GNU extension}}
295295
}
296+
297+
298+
void T1(void) {
299+
static int *y[1] = {({ static int _x = 20; (void*)0;})}; // all-error {{initializer element is not a compile-time constant}} \
300+
// pedantic-warning {{use of GNU statement expression extension}}
301+
}
302+
303+

0 commit comments

Comments
 (0)