Skip to content

[clang][Interp] reinterpret casts aren't always fatal #101900

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
Aug 5, 2024
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
9 changes: 6 additions & 3 deletions clang/lib/AST/Interp/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (CE->getType()->isAtomicType()) {
if (!this->discard(SubExpr))
return false;
return this->emitInvalidCast(CastKind::Reinterpret, CE);
return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, CE);
}

if (DiscardResult)
Expand Down Expand Up @@ -2465,10 +2465,13 @@ bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
template <class Emitter>
bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
const CXXReinterpretCastExpr *E) {
if (!this->discard(E->getSubExpr()))
const Expr *SubExpr = E->getSubExpr();

bool TypesMatch = classify(E) == classify(SubExpr);
if (!this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/!TypesMatch, E))
return false;

return this->emitInvalidCast(CastKind::Reinterpret, E);
return this->delegate(SubExpr);
}

template <class Emitter>
Expand Down
9 changes: 6 additions & 3 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2787,13 +2787,16 @@ inline bool Unsupported(InterpState &S, CodePtr OpPC) {
inline bool Error(InterpState &S, CodePtr OpPC) { return false; }

/// Same here, but only for casts.
inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind) {
inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
bool Fatal) {
const SourceLocation &Loc = S.Current->getLocation(OpPC);

// FIXME: Support diagnosing other invalid cast kinds.
if (Kind == CastKind::Reinterpret)
S.FFDiag(Loc, diag::note_constexpr_invalid_cast)
if (Kind == CastKind::Reinterpret) {
S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
<< static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
return !Fatal;
}
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/Interp/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def Invalid : Opcode {}
def Unsupported : Opcode {}
def Error : Opcode {}
def InvalidCast : Opcode {
let Args = [ArgCastKind];
let Args = [ArgCastKind, ArgBool];
}

def InvalidDeclRef : Opcode {
Expand Down
13 changes: 13 additions & 0 deletions clang/test/AST/Interp/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ namespace BaseClassOffsets {
// CHECK: @_ZN16BaseClassOffsets1bE = global ptr getelementptr (i8, ptr @_ZN16BaseClassOffsets1cE, i64 4), align 8
B* b = &c;
}

namespace reinterpretcast {
const unsigned int n = 1234;
extern const int &s = reinterpret_cast<const int&>(n);
// CHECK: @_ZN15reinterpretcastL1nE = internal constant i32 1234, align 4
// CHECK: @_ZN15reinterpretcast1sE = constant ptr @_ZN15reinterpretcastL1nE, align 8

void *f1(unsigned long l) {
return reinterpret_cast<void *>(l);
}
// CHECK: define {{.*}} ptr @_ZN15reinterpretcast2f1Em
// CHECK: inttoptr
}
Loading