[clang] d6ccc29 - [clang][bytecode] Take AccessKinds into account in diagnoseNonConstVa… (#204824)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 19 07:22:41 PDT 2026
Author: Timm Baeder
Date: 2026-06-19T16:22:37+02:00
New Revision: d6ccc29cb3a79631818e30f2f523fc6a91718f8a
URL: https://github.com/llvm/llvm-project/commit/d6ccc29cb3a79631818e30f2f523fc6a91718f8a
DIFF: https://github.com/llvm/llvm-project/commit/d6ccc29cb3a79631818e30f2f523fc6a91718f8a.diff
LOG: [clang][bytecode] Take AccessKinds into account in diagnoseNonConstVa… (#204824)
…riable
And diagnose it as a modification and not a read if applicable.
Added:
Modified:
clang/lib/AST/ByteCode/Interp.cpp
clang/test/AST/ByteCode/cxx20.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 60914a2da111a..b022d71ae1e49 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -97,9 +97,10 @@ static void noteValueLocation(InterpState &S, const Block *B) {
}
static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
- const ValueDecl *VD);
+ const ValueDecl *VD,
+ AccessKinds AK = AK_Read);
static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
- const ValueDecl *D) {
+ const ValueDecl *D, AccessKinds AK = AK_Read) {
// This function tries pretty hard to produce a good diagnostic. Just skip
// that if nobody will see it anyway.
if (!S.diagnosing())
@@ -129,7 +130,7 @@ static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
}
if (!D->getType().isConstQualified()) {
- diagnoseNonConstVariable(S, OpPC, D);
+ diagnoseNonConstVariable(S, OpPC, D, AK);
} else if (const auto *VD = dyn_cast<VarDecl>(D)) {
if (!VD->getAnyInitializer()) {
diagnoseMissingInitializer(S, OpPC, VD);
@@ -143,8 +144,13 @@ static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
return false;
}
+static bool isModification(AccessKinds AK) {
+ return AK == AK_Assign || AK == AK_Increment || AK == AK_Decrement ||
+ AK == AK_Construct || AK == AK_Destroy;
+}
+
static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
- const ValueDecl *VD) {
+ const ValueDecl *VD, AccessKinds AK) {
if (!S.diagnosing())
return;
@@ -168,8 +174,12 @@ static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
return;
if (VD->getType()->isIntegralOrEnumerationType()) {
- S.FFDiag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
- S.Note(VD->getLocation(), diag::note_declared_at);
+ if (isModification(AK)) {
+ S.FFDiag(Loc, diag::note_constexpr_modify_global);
+ } else {
+ S.FFDiag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
+ S.Note(VD->getLocation(), diag::note_declared_at);
+ }
return;
}
@@ -1266,7 +1276,7 @@ bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK) {
return false;
if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement)
- return diagnoseUnknownDecl(S, OpPC, D);
+ return diagnoseUnknownDecl(S, OpPC, D, AK);
if (AK == AK_Destroy || S.getLangOpts().CPlusPlus14) {
const SourceInfo &E = S.Current->getSource(OpPC);
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index a6409d4a2c268..7ff70076ee6e4 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -1,6 +1,11 @@
// RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,expected -fcxx-exceptions %s -DNEW_INTERP -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,ref -fcxx-exceptions %s
+
+int x;
+static_assert(++x, "test"); // both-error {{not an integral constant expression}} \
+ // both-note {{cannot modify an object that is visible outside that expression}}
+
void test_alignas_operand() {
alignas(8) char dummy;
static_assert(__alignof(dummy) == 8);
More information about the cfe-commits
mailing list