[clang] 215ed9b - Adapt CastExpr::getSubExprAsWritten to ConstantExpr

Stephan Bergmann via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 12 00:41:13 PST 2021


Author: Stephan Bergmann
Date: 2021-01-12T09:41:03+01:00
New Revision: 215ed9b33ccbe9858aeb65b357bd1111cff354be

URL: https://github.com/llvm/llvm-project/commit/215ed9b33ccbe9858aeb65b357bd1111cff354be
DIFF: https://github.com/llvm/llvm-project/commit/215ed9b33ccbe9858aeb65b357bd1111cff354be.diff

LOG: Adapt CastExpr::getSubExprAsWritten to ConstantExpr

Differential Revision: https://reviews.llvm.org/D87030

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp
    clang/unittests/Tooling/CastExprTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index a274bf37a407..94b6adf489fe 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -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)) &&

diff  --git a/clang/unittests/Tooling/CastExprTest.cpp b/clang/unittests/Tooling/CastExprTest.cpp
index a9e78d2155b4..cda963a6a897 100644
--- a/clang/unittests/Tooling/CastExprTest.cpp
+++ b/clang/unittests/Tooling/CastExprTest.cpp
@@ -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);
+}
+
 }


        


More information about the cfe-commits mailing list