Skip to content
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
38 changes: 33 additions & 5 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2613,18 +2613,46 @@ bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
const CXXReinterpretCastExpr *E) {
const Expr *SubExpr = E->getSubExpr();

bool Fatal = false;
std::optional<PrimType> FromT = classify(SubExpr);
std::optional<PrimType> ToT = classify(E);

if (!FromT || !ToT)
Fatal = true;
else
Fatal = (ToT != FromT);
return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);

if (FromT == PT_Ptr || ToT == PT_Ptr) {
// Both types could be PT_Ptr because their expressions are glvalues.
std::optional<PrimType> PointeeFromT;
if (SubExpr->getType()->isPointerOrReferenceType())
PointeeFromT = classify(SubExpr->getType()->getPointeeType());
else
PointeeFromT = classify(SubExpr->getType());

std::optional<PrimType> PointeeToT;
if (E->getType()->isPointerOrReferenceType())
PointeeToT = classify(E->getType()->getPointeeType());
else
PointeeToT = classify(E->getType());

bool Fatal = true;
if (PointeeToT && PointeeFromT) {
if (isIntegralType(*PointeeFromT) && isIntegralType(*PointeeToT))
Fatal = false;
}

if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
return false;

if (E->getCastKind() == CK_LValueBitCast)
return this->delegate(SubExpr);
return this->VisitCastExpr(E);
}

// Try to actually do the cast.
bool Fatal = (ToT != FromT);
if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
return false;

return this->delegate(SubExpr);
return this->VisitCastExpr(E);
}

template <class Emitter>
Expand Down
3 changes: 3 additions & 0 deletions clang/test/AST/ByteCode/invalid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ namespace Casts {
B b;
(void)*reinterpret_cast<void*>(&b); // both-error {{indirection not permitted on operand of type 'void *'}}
}

/// Just make sure this doesn't crash.
float PR9558 = reinterpret_cast<const float&>("asd");
}