[clang] [clang][bytecode] Do not diagnose volatile reads in CPCE mode (PR #140546)
via cfe-commits
cfe-commits at lists.llvm.org
Mon May 19 07:19:20 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
This matches the diagnostic output of the current interpreter.
---
Full diff: https://github.com/llvm/llvm-project/pull/140546.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.h (+8-7)
- (modified) clang/test/AST/ByteCode/cxx11.cpp (+10)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 9f1a6302eb856..bfc6797d13412 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2936,13 +2936,14 @@ inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
<< static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
return !Fatal;
} else if (Kind == CastKind::Volatile) {
- // FIXME: Technically not a cast.
- const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
- if (S.getLangOpts().CPlusPlus)
- S.FFDiag(E, diag::note_constexpr_access_volatile_type)
- << AK_Read << E->getSubExpr()->getType();
- else
- S.FFDiag(E);
+ if (!S.checkingPotentialConstantExpression()) {
+ const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
+ if (S.getLangOpts().CPlusPlus)
+ S.FFDiag(E, diag::note_constexpr_access_volatile_type)
+ << AK_Read << E->getSubExpr()->getType();
+ else
+ S.FFDiag(E);
+ }
return false;
} else if (Kind == CastKind::Dynamic) {
diff --git a/clang/test/AST/ByteCode/cxx11.cpp b/clang/test/AST/ByteCode/cxx11.cpp
index 88e195c36b583..2a1bda675075c 100644
--- a/clang/test/AST/ByteCode/cxx11.cpp
+++ b/clang/test/AST/ByteCode/cxx11.cpp
@@ -233,3 +233,13 @@ namespace IntToPtrCast {
constexpr intptr_t i = f((intptr_t)&foo - 10); // both-error{{constexpr variable 'i' must be initialized by a constant expression}} \
// both-note{{reinterpret_cast}}
}
+
+namespace Volatile {
+ constexpr int f(volatile int &&r) {
+ return r; // both-note {{read of volatile-qualified type 'volatile int'}}
+ }
+ struct S {
+ int j : f(0); // both-error {{constant expression}} \
+ // both-note {{in call to 'f(0)'}}
+ };
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/140546
More information about the cfe-commits
mailing list