[clang] [clang][Interp] Properly abort on reads from non-const variables (PR #102426)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 7 23:49:42 PDT 2024
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/102426
>From fb78d6e1b2b546b0901a4b1ca97ff8eae3cc95cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 8 Aug 2024 08:01:18 +0200
Subject: [PATCH] [clang][Interp] Properly abort on reads from non-const
variables
We need to return false here in any case. Use isConstant() to capure
the weird OpenCL cases.
---
clang/lib/AST/Interp/Interp.cpp | 7 ++++---
clang/test/AST/Interp/bitfields.cpp | 19 ++++++++++++-------
2 files changed, 16 insertions(+), 10 deletions(-)
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