Skip to content

[clang][bytecode] Diagnose dynamic_cast before C++20 #137442

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
11 changes: 11 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3011,6 +3011,17 @@ bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
return this->VisitCastExpr(E);
}

template <class Emitter>
bool Compiler<Emitter>::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {

if (!Ctx.getLangOpts().CPlusPlus20) {
if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E))
return false;
}

return this->VisitCastExpr(E);
}

template <class Emitter>
bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
assert(E->getType()->isBooleanType());
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
bool VisitPredefinedExpr(const PredefinedExpr *E);
bool VisitCXXThrowExpr(const CXXThrowExpr *E);
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E);
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
bool VisitCXXConstructExpr(const CXXConstructExpr *E);
bool VisitSourceLocExpr(const SourceLocExpr *E);
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,11 @@ inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
S.FFDiag(E);

return false;
} else if (Kind == CastKind::Dynamic) {
assert(!S.getLangOpts().CPlusPlus20);
S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
<< diag::ConstexprInvalidCastKind::Dynamic;
return true;
}

return false;
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/ByteCode/PrimType.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ inline constexpr bool isPtrType(PrimType T) {
enum class CastKind : uint8_t {
Reinterpret,
Volatile,
Dynamic,
};

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
Expand All @@ -67,6 +68,9 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
case interp::CastKind::Volatile:
OS << "volatile";
break;
case interp::CastKind::Dynamic:
OS << "dynamic";
break;
}
return OS;
}
Expand Down
13 changes: 11 additions & 2 deletions clang/test/AST/ByteCode/cxx11-pedantic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected -std=c++11 -triple x86_64-linux -pedantic %s
// RUN: %clang_cc1 -verify=both,ref -std=c++11 -triple x86_64-linux -pedantic %s
// RUN: %clang_cc1 -verify=both,expected -std=c++11 -triple x86_64-linux -pedantic %s -fexperimental-new-constant-interpreter
// RUN: %clang_cc1 -verify=both,ref -std=c++11 -triple x86_64-linux -pedantic %s

struct T { int n; };
const T t = { 42 }; // both-note 2{{declared here}}
Expand All @@ -11,3 +11,12 @@ struct S {

static_assert(t.n == 42, ""); // both-error {{expression is not an integral constant expression}} \
// both-note {{read of non-constexpr variable 't' is not allowed}}

namespace DynamicCast {
struct S { int n; };
constexpr S s { 16 };
struct T {
int n : dynamic_cast<const S*>(&s)->n; // both-warning {{constant expression}} \
// both-note {{dynamic_cast}}
};
}
Loading