Skip to content

Commit c7ea4c1

Browse files
authored
[clang][bytecode] Revisit global variables separately (#123358)
Call `EvaluateAsInitializer()` explicitly here, so we don't abort the evaluation of the `DeflRefExpr` just because the initializer of that global variable failed.
1 parent ebfdd38 commit c7ea4c1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6210,8 +6210,20 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
62106210
return revisit(VD);
62116211

62126212
if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
6213-
typeShouldBeVisited(VD->getType()))
6213+
typeShouldBeVisited(VD->getType())) {
6214+
if (const Expr *Init = VD->getAnyInitializer();
6215+
Init && !Init->isValueDependent()) {
6216+
// Whether or not the evaluation is successul doesn't really matter
6217+
// here -- we will create a global variable in any case, and that
6218+
// will have the state of initializer evaluation attached.
6219+
APValue V;
6220+
SmallVector<PartialDiagnosticAt> Notes;
6221+
(void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
6222+
true);
6223+
return this->visitDeclRef(D, E);
6224+
}
62146225
return revisit(VD);
6226+
}
62156227

62166228
// FIXME: The evaluateValue() check here is a little ridiculous, since
62176229
// it will ultimately call into Context::evaluateAsInitializer(). In

clang/test/AST/ByteCode/cxx98.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ struct PR65784s{
5959
int *ptr;
6060
} const PR65784[] = {(int *)""};
6161
PR65784s PR65784f() { return *PR65784; }
62+
63+
const int b = 1 / 0; // both-warning {{division by zero is undefined}} \
64+
// both-note {{declared here}}
65+
_Static_assert(b, ""); // both-error {{not an integral constant expression}} \
66+
// both-note {{initializer of 'b' is not a constant expression}}

0 commit comments

Comments
 (0)