[PATCH] D87030: Adapt CastExpr::getSubExprAsWritten to ConstantExpr

Stephan Bergmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 2 07:31:02 PDT 2020


sberg created this revision.
sberg added a reviewer: rsmith.
Herald added a project: clang.
sberg requested review of this revision.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87030

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


Index: clang/unittests/Tooling/CastExprTest.cpp
===================================================================
--- clang/unittests/Tooling/CastExprTest.cpp
+++ clang/unittests/Tooling/CastExprTest.cpp
@@ -34,4 +34,23 @@
                     "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);
+}
 }
Index: clang/lib/AST/Expr.cpp
===================================================================
--- clang/lib/AST/Expr.cpp
+++ clang/lib/AST/Expr.cpp
@@ -1823,8 +1823,8 @@
     // Conversions by constructor and conversion functions have a
     // subexpression describing the call; strip it off.
     if (E->getCastKind() == CK_ConstructorConversion)
-      SubExpr =
-        skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr)->getArg(0));
+      SubExpr = skipImplicitTemporary(
+          cast<CXXConstructExpr>(SubExpr->IgnoreImplicit())->getArg(0));
     else if (E->getCastKind() == CK_UserDefinedConversion) {
       assert((isa<CXXMemberCallExpr>(SubExpr) ||
               isa<BlockExpr>(SubExpr)) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87030.289443.patch
Type: text/x-patch
Size: 1790 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200902/01e983f0/attachment.bin>


More information about the cfe-commits mailing list