Skip to content

Commit

Permalink
[clang][Interp] Handle invalid CXXCtorInitializer expressions
Browse files Browse the repository at this point in the history
Their type might be a null type, in which case we need to abort here.
  • Loading branch information
tbaederr committed Feb 9, 2024
1 parent bc6955f commit 0d9decc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/AST/Interp/ByteCodeStmtGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ bool ByteCodeStmtGen<Emitter>::visitFunc(const FunctionDecl *F) {

auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
const Expr *InitExpr) -> bool {
// We don't know what to do with these, so just return false.
if (InitExpr->getType().isNull())
return false;

if (std::optional<PrimType> T = this->classify(InitExpr)) {
if (!this->visit(InitExpr))
return false;
Expand Down
11 changes: 11 additions & 0 deletions clang/test/AST/Interp/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1228,3 +1228,14 @@ namespace InheritedConstructor {
constexpr S s(1);
}
}

namespace InvalidCtorInitializer {
struct X {
int Y;
constexpr X() // expected-note {{declared here}}
: Y(fo_o_()) {} // both-error {{use of undeclared identifier 'fo_o_'}}
};
// no crash on evaluating the constexpr ctor.
constexpr int Z = X().Y; // both-error {{constexpr variable 'Z' must be initialized by a constant expression}} \
// expected-note {{undefined constructor 'X'}}
}

0 comments on commit 0d9decc

Please sign in to comment.