[PATCH] D22128: Make CastExpr::getSubExprAsWritten look through implicit temporary under CK_ConstructorConversion

Stephan Bergmann via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 8 04:14:00 PDT 2016


sberg created this revision.
sberg added a reviewer: rsmith.
sberg added a subscriber: cfe-commits.
Herald added a subscriber: aemerson.

With

> struct S1 {};
> struct S2 { operator S1(); };
> S1 f(S2 s) { return static_cast<S1>(s); }

the static_cast expr is

> CXXStaticCastExpr 0x... 'struct S1' static_cast<struct S1> <ConstructorConversion>
> `-CXXConstructExpr 0x... 'struct S1' 'void (struct S1 &&) noexcept' elidable
>   `-MaterializeTemporaryExpr 0x... 'struct S1' xvalue
>     `-ImplicitCastExpr 0x... 'struct S1' <UserDefinedConversion>
>       `-CXXMemberCallExpr 0x... 'struct S1'
>         `-MemberExpr 0x... '<bound member function type>' .operator S1 0x...
>           `-DeclRefExpr 0x... 'struct S2' lvalue ParmVar 0x... 's' 'struct S2'

getSubExprAsWritten used to return the MaterializeTemporaryExpr (of type S1) under the CXXConstructExpr, instead of unwinding further to the DeclRefExpr (of type S2) at the bottom.

http://reviews.llvm.org/D22128

Files:
  lib/AST/Expr.cpp

Index: lib/AST/Expr.cpp
===================================================================
--- lib/AST/Expr.cpp
+++ lib/AST/Expr.cpp
@@ -1602,25 +1602,32 @@
   llvm_unreachable("Unhandled cast kind!");
 }
 
+namespace {
+  Expr *skipImplicitTemporary(Expr *expr) {
+    // Skip through reference binding to temporary.
+    if (MaterializeTemporaryExpr *Materialize
+                                  = dyn_cast<MaterializeTemporaryExpr>(expr))
+      expr = Materialize->GetTemporaryExpr();
+
+    // Skip any temporary bindings; they're implicit.
+    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(expr))
+      expr = Binder->getSubExpr();
+
+    return expr;
+  }
+}
+
 Expr *CastExpr::getSubExprAsWritten() {
   Expr *SubExpr = nullptr;
   CastExpr *E = this;
   do {
-    SubExpr = E->getSubExpr();
+    SubExpr = skipImplicitTemporary(E->getSubExpr());
 
-    // Skip through reference binding to temporary.
-    if (MaterializeTemporaryExpr *Materialize 
-                                  = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
-      SubExpr = Materialize->GetTemporaryExpr();
-        
-    // Skip any temporary bindings; they're implicit.
-    if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
-      SubExpr = Binder->getSubExpr();
-    
     // Conversions by constructor and conversion functions have a
     // subexpression describing the call; strip it off.
     if (E->getCastKind() == CK_ConstructorConversion)
-      SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
+      SubExpr =
+        skipImplicitTemporary(cast<CXXConstructExpr>(SubExpr)->getArg(0));
     else if (E->getCastKind() == CK_UserDefinedConversion) {
       assert((isa<CXXMemberCallExpr>(SubExpr) ||
               isa<BlockExpr>(SubExpr)) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22128.63197.patch
Type: text/x-patch
Size: 1794 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160708/fa1d570b/attachment.bin>


More information about the cfe-commits mailing list