Skip to content

Commit 876cbf9

Browse files
authored
[clang][Interp] Properly abort on reads from non-const variables (#102426)
We need to return false here in any case. Use isConstant() to capure the weird OpenCL cases.
1 parent 2289785 commit 876cbf9

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

clang/lib/AST/Interp/Interp.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,11 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
301301
assert(Desc);
302302

303303
auto IsConstType = [&S](const VarDecl *VD) -> bool {
304-
if (VD->isConstexpr())
304+
QualType T = VD->getType();
305+
306+
if (T.isConstant(S.getCtx()))
305307
return true;
306308

307-
QualType T = VD->getType();
308309
if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11)
309310
return (T->isSignedIntegerOrEnumerationType() ||
310311
T->isUnsignedIntegerOrEnumerationType()) &&
@@ -325,7 +326,7 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
325326
if (const auto *D = Desc->asVarDecl();
326327
D && D->hasGlobalStorage() && D != S.EvaluatingDecl && !IsConstType(D)) {
327328
diagnoseNonConstVariable(S, OpPC, D);
328-
return S.inConstantContext();
329+
return false;
329330
}
330331

331332
return true;

clang/test/AST/Interp/bitfields.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s
2-
// RUN: %clang_cc1 -verify=ref -Wno-bitfield-constant-conversion %s
3-
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s
4-
// RUN: %clang_cc1 -std=c++20 -verify=ref -Wno-bitfield-constant-conversion %s
5-
6-
// expected-no-diagnostics
7-
// ref-no-diagnostics
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify=expected,both %s
2+
// RUN: %clang_cc1 -verify=ref,both -Wno-bitfield-constant-conversion %s
3+
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify=expected,both %s
4+
// RUN: %clang_cc1 -std=c++20 -verify=ref,both -Wno-bitfield-constant-conversion %s
85

96
namespace Basic {
107
struct A {
@@ -123,3 +120,11 @@ namespace test0 {
123120
c.onebit = int_source();
124121
}
125122
}
123+
124+
namespace NonConstBitWidth {
125+
int n3 = 37; // both-note {{declared here}}
126+
struct S {
127+
int l : n3; // both-error {{constant expression}} \
128+
// both-note {{read of non-const variable}}
129+
};
130+
}

0 commit comments

Comments
 (0)