Merge remote-tracking branch 'origin/swift-4.1-branch' into stable
diff --git a/lib/Tooling/Refactor/ASTSlice.cpp b/lib/Tooling/Refactor/ASTSlice.cpp
index 5447571..ebf6abc 100644
--- a/lib/Tooling/Refactor/ASTSlice.cpp
+++ b/lib/Tooling/Refactor/ASTSlice.cpp
@@ -246,8 +246,7 @@
   const Stmt *Parent = NodeTree[Index + 1].getStmtOrNull();
   if (!Parent)
     return Same;
-
-  const auto Next = std::make_pair(Parent, Index + 1);
+  auto Next = std::make_pair(Parent, Index + 1);
   // The entire pseudo expression is selected when just its syntactic
   // form is selected.
   if (isa<Expr>(S)) {
@@ -256,6 +255,18 @@
         return Next;
     }
   }
+
+  // Look through the implicit casts in the parents.
+  unsigned ParentIndex = Index + 1;
+  for (; ParentIndex <= NodeTree.size() && isa<ImplicitCastExpr>(Parent);
+       ++ParentIndex) {
+    const Stmt *NewParent = NodeTree[ParentIndex + 1].getStmtOrNull();
+    if (!NewParent)
+      break;
+    Parent = NewParent;
+  }
+  Next = std::make_pair(Parent, ParentIndex);
+
   // The entire ObjC string literal is selected when just its string
   // literal is selected.
   if (isa<StringLiteral>(S) && isa<ObjCStringLiteral>(Parent))
@@ -271,7 +282,7 @@
       if (Call->getCalleeDecl() == DRE->getDecl())
         return Next;
     }
-  }
+    }
   return Same;
 }
 
diff --git a/test/Refactor/Extract/extract-expression.cpp b/test/Refactor/Extract/extract-expression.cpp
index 56b26df..3cef26c 100644
--- a/test/Refactor/Extract/extract-expression.cpp
+++ b/test/Refactor/Extract/extract-expression.cpp
@@ -58,3 +58,11 @@
 // CHECK5-NEXT: "extracted(r)" [[@LINE-3]]:3 -> [[@LINE-3]]:21
 
 // RUN: clang-refactor-test perform -action extract -selected=%s:55:3-55:21 %s | FileCheck --check-prefix=CHECK5 %s
+;
+void extractFunctionCall() {
+  sumArea(0, 1);
+}
+// CHECK6: "static int extracted() {\nreturn sumArea(0, 1);\n}\n\n" [[@LINE-3]]:1 -> [[@LINE-3]]:1
+// CHECK6-NEXT: "extracted()" [[@LINE-3]]:3 -> [[@LINE-3]]:16
+
+// RUN: clang-refactor-test perform -action extract -selected=%s:63:3-63:10 %s | FileCheck --check-prefix=CHECK6 %s