[clang] b8f5e3c - [clang][bytecode] Diagnose PointerToIntegral casts (#137444)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 22:11:33 PDT 2025
Author: Timm Baeder
Date: 2025-04-26T07:11:30+02:00
New Revision: b8f5e3c573577a388f2674a88901faf760bca8a6
URL: https://github.com/llvm/llvm-project/commit/b8f5e3c573577a388f2674a88901faf760bca8a6
DIFF: https://github.com/llvm/llvm-project/commit/b8f5e3c573577a388f2674a88901faf760bca8a6.diff
LOG: [clang][bytecode] Diagnose PointerToIntegral casts (#137444)
Always emit the diagnostic, but permit the cast.
Added:
Modified:
clang/lib/AST/ByteCode/Interp.h
clang/test/AST/ByteCode/cxx11.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index e2f2e735dc838..63a1f3e8033b7 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2362,8 +2362,12 @@ template <PrimType Name, class T = typename PrimConv<Name>::T>
bool CastPointerIntegral(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();
+ S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
+ << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
+ << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
+
if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth()))
- return false;
+ return Invalid(S, OpPC);
S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
return true;
diff --git a/clang/test/AST/ByteCode/cxx11.cpp b/clang/test/AST/ByteCode/cxx11.cpp
index 18806801bdd33..88e195c36b583 100644
--- a/clang/test/AST/ByteCode/cxx11.cpp
+++ b/clang/test/AST/ByteCode/cxx11.cpp
@@ -221,3 +221,15 @@ namespace PseudoDtor {
static_assert(f() == 0, ""); // both-error {{constant expression}} \
// expected-note {{in call to}}
}
+
+namespace IntToPtrCast {
+ typedef __INTPTR_TYPE__ intptr_t;
+
+ constexpr intptr_t f(intptr_t x) {
+ return (((x) >> 21) * 8);
+ }
+
+ extern "C" int foo;
+ 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}}
+}
More information about the cfe-commits
mailing list