Skip to content

Commit

Permalink
Adapt CastExpr::getSubExprAsWritten to ConstantExpr
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D87030
  • Loading branch information
stbergmann committed Jan 12, 2021
1 parent 1cc5235 commit 215ed9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/AST/Expr.cpp
Expand Up @@ -1765,7 +1765,7 @@ Expr *CastExpr::getSubExprAsWritten() {
// subexpression describing the call; strip it off.
if (E->getCastKind() == CK_ConstructorConversion)
SubExpr =
skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr)->getArg(0));
skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr->IgnoreImplicit())->getArg(0));
else if (E->getCastKind() == CK_UserDefinedConversion) {
assert((isa<CXXMemberCallExpr>(SubExpr) ||
isa<BlockExpr>(SubExpr)) &&
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Tooling/CastExprTest.cpp
Expand Up @@ -34,4 +34,24 @@ TEST(CastExprTest, GetSubExprAsWrittenThroughMaterializedTemporary) {
"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);
}

}

0 comments on commit 215ed9b

Please sign in to comment.