[clang] 876cbf9 - [clang][Interp] Properly abort on reads from non-const variables (#102426)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 8 00:36:07 PDT 2024
Author: Timm Baeder
Date: 2024-08-08T09:36:03+02:00
New Revision: 876cbf9a127b0a18374dcf91c969c7ba152e048c
URL: https://github.com/llvm/llvm-project/commit/876cbf9a127b0a18374dcf91c969c7ba152e048c
DIFF: https://github.com/llvm/llvm-project/commit/876cbf9a127b0a18374dcf91c969c7ba152e048c.diff
LOG: [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.
Added:
Modified:
clang/lib/AST/Interp/Interp.cpp
clang/test/AST/Interp/bitfields.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 0f72b860ddad77..85cb8ff2db974e 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -301,10 +301,11 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
assert(Desc);
auto IsConstType = [&S](const VarDecl *VD) -> bool {
- if (VD->isConstexpr())
+ QualType T = VD->getType();
+
+ if (T.isConstant(S.getCtx()))
return true;
- QualType T = VD->getType();
if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11)
return (T->isSignedIntegerOrEnumerationType() ||
T->isUnsignedIntegerOrEnumerationType()) &&
@@ -325,7 +326,7 @@ bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
if (const auto *D = Desc->asVarDecl();
D && D->hasGlobalStorage() && D != S.EvaluatingDecl && !IsConstType(D)) {
diagnoseNonConstVariable(S, OpPC, D);
- return S.inConstantContext();
+ return false;
}
return true;
diff --git a/clang/test/AST/Interp/bitfields.cpp b/clang/test/AST/Interp/bitfields.cpp
index 5fc34bb1229d99..df8d5678287d37 100644
--- a/clang/test/AST/Interp/bitfields.cpp
+++ b/clang/test/AST/Interp/bitfields.cpp
@@ -1,10 +1,7 @@
-// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s
-// RUN: %clang_cc1 -verify=ref -Wno-bitfield-constant-conversion %s
-// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s
-// RUN: %clang_cc1 -std=c++20 -verify=ref -Wno-bitfield-constant-conversion %s
-
-// expected-no-diagnostics
-// ref-no-diagnostics
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify=expected,both %s
+// RUN: %clang_cc1 -verify=ref,both -Wno-bitfield-constant-conversion %s
+// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify=expected,both %s
+// RUN: %clang_cc1 -std=c++20 -verify=ref,both -Wno-bitfield-constant-conversion %s
namespace Basic {
struct A {
@@ -123,3 +120,11 @@ namespace test0 {
c.onebit = int_source();
}
}
+
+namespace NonConstBitWidth {
+ int n3 = 37; // both-note {{declared here}}
+ struct S {
+ int l : n3; // both-error {{constant expression}} \
+ // both-note {{read of non-const variable}}
+ };
+}
More information about the cfe-commits
mailing list