Skip to content

Commit 7f3ac51

Browse files
committed
[clang][Interp] Only accept constant variables in c++98
1 parent 73324cb commit 7f3ac51

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

clang/lib/AST/Interp/Interp.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
302302

303303
QualType T = VD->getType();
304304
if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11)
305-
return T->isSignedIntegerOrEnumerationType() || T->isUnsignedIntegerOrEnumerationType();
305+
return (T->isSignedIntegerOrEnumerationType() ||
306+
T->isUnsignedIntegerOrEnumerationType()) &&
307+
T.isConstQualified();
306308

307309
if (T.isConstQualified())
308310
return true;
@@ -316,12 +318,10 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
316318
return false;
317319
};
318320

319-
if (const auto *D = Desc->asValueDecl()) {
320-
if (const auto *VD = dyn_cast<VarDecl>(D);
321-
VD && VD->hasGlobalStorage() && !IsConstType(VD)) {
322-
diagnoseNonConstVariable(S, OpPC, VD);
323-
return S.inConstantContext();
324-
}
321+
if (const auto *D = Desc->asVarDecl();
322+
D && D->hasGlobalStorage() && !IsConstType(D)) {
323+
diagnoseNonConstVariable(S, OpPC, D);
324+
return S.inConstantContext();
325325
}
326326

327327
return true;

clang/test/AST/Interp/cxx98.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ struct C0 {
4545
};
4646
const int c0_test = C0::Data<int*>;
4747
_Static_assert(c0_test == 0, "");
48+
49+
50+
int a = 0; // both-note {{declared here}}
51+
_Static_assert(a == 0, ""); // both-error {{static assertion expression is not an integral constant expression}} \
52+
// both-note {{read of non-const variable 'a' is not allowed in a constant expression}}

0 commit comments

Comments
 (0)