Adapt CastExpr::getSubExprAsWritten to ConstantExpr

Differential Revision: https://reviews.llvm.org/D87030
This commit is contained in:
Stephan Bergmann 2020-09-02 16:04:56 +02:00
parent 1cc5235712
commit 215ed9b33c
2 changed files with 21 additions and 1 deletions

View File

@ -1765,7 +1765,7 @@ Expr *CastExpr::getSubExprAsWritten() {
// subexpression describing the call; strip it off. // subexpression describing the call; strip it off.
if (E->getCastKind() == CK_ConstructorConversion) if (E->getCastKind() == CK_ConstructorConversion)
SubExpr = SubExpr =
skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr)->getArg(0)); skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr->IgnoreImplicit())->getArg(0));
else if (E->getCastKind() == CK_UserDefinedConversion) { else if (E->getCastKind() == CK_UserDefinedConversion) {
assert((isa<CXXMemberCallExpr>(SubExpr) || assert((isa<CXXMemberCallExpr>(SubExpr) ||
isa<BlockExpr>(SubExpr)) && isa<BlockExpr>(SubExpr)) &&

View File

@ -34,4 +34,24 @@ TEST(CastExprTest, GetSubExprAsWrittenThroughMaterializedTemporary) {
"S1 f(S2 s) { return static_cast<S1>(s); }\n"); "S1 f(S2 s) { return static_cast<S1>(s); }\n");
} }
// Verify that getSubExprAsWritten looks through a ConstantExpr in a scenario
// like
//
// CXXFunctionalCastExpr functional cast to struct S <ConstructorConversion>
// `-ConstantExpr 'S'
// |-value: Struct
// `-CXXConstructExpr 'S' 'void (int)'
// `-IntegerLiteral 'int' 0
TEST(CastExprTest, GetSubExprAsWrittenThroughConstantExpr) {
CastExprVisitor Visitor;
Visitor.OnExplicitCast = [](ExplicitCastExpr *Expr) {
auto *Sub = Expr->getSubExprAsWritten();
EXPECT_TRUE(isa<IntegerLiteral>(Sub))
<< "Expected IntegerLiteral, but saw " << Sub->getStmtClassName();
};
Visitor.runOver("struct S { consteval S(int) {} };\n"
"S f() { return S(0); }\n",
CastExprVisitor::Lang_CXX2a);
}
} }