Skip to content

[clang][bytecode] Diagnose PointerToIntegral casts #137444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2360,8 +2360,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;
Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/ByteCode/cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}
Loading