]> granicus.if.org Git - clang/commitdiff
[refactor][extract] avoid extracting expressions from types in functions
authorAlex Lorenz <arphaman@gmail.com>
Tue, 14 Nov 2017 18:59:01 +0000 (18:59 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Tue, 14 Nov 2017 18:59:01 +0000 (18:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318169 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Tooling/Refactoring/ASTSelection.cpp
test/Refactor/Extract/ExtractExprIntoFunction.cpp

index ab2be1551219ca74df7aa42a2bb78e2421c6f540..4f1168becf8a2948b0badd59fd9bac863df03a8b 100644 (file)
@@ -383,10 +383,12 @@ bool CodeRangeASTSelection::isInFunctionLikeBodyOfCode() const {
     if (const auto *D = Node.get<Decl>()) {
       if (isFunctionLikeDeclaration(D))
         return IsPrevCompound;
-      // FIXME (Alex L): We should return false on top-level decls in functions
-      // e.g. we don't want to extract:
+      // Stop the search at any type declaration to avoid returning true for
+      // expressions in type declarations in functions, like:
       // function foo() { struct X {
       //   int m = /*selection:*/ 1 + 2 /*selection end*/; }; };
+      if (isa<TypeDecl>(D))
+        return false;
     }
     IsPrevCompound = Node.get<CompoundStmt>() != nullptr;
   }
index b4f0b59543875316d930c7ea482bc50c785473fb..0fccc9298597ea403540bc87e39a63f53e5840ed 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: clang-refactor extract -selection=test:%s %s -- -std=c++11 2>&1 | grep -v CHECK | FileCheck %s
+// RUN: clang-refactor extract -selection=test:%s %s -- -std=c++14 2>&1 | grep -v CHECK | FileCheck %s
 
 
 void simpleExtractNoCaptures() {
@@ -42,7 +42,21 @@ struct OutOfBodyStuff {
   void foo(int x =/*range out_of_body_expr=->+0:58*/1 + 2);
 };
 
-// CHECK: 3 'out_of_body_expr' results:
+auto inFunctionOutOfBody() -> decltype(/*range out_of_body_expr=->+0:79*/1 + 2) {
+  struct OutOfBodyStuff {
+    int FieldInit = /*range out_of_body_expr=->+0:60*/1 + 2;
+
+    void foo(int x =/*range out_of_body_expr=->+0:60*/1 + 2);
+  };
+  enum E {
+    X = /*range out_of_body_expr=->+0:48*/1 + 2
+  };
+  int x = 0;
+  using T = decltype(/*range out_of_body_expr=->+0:61*/x + 3);
+  return x;
+}
+
+// CHECK: 8 'out_of_body_expr' results:
 // CHECK: the selected code is not a part of a function's / method's body
 
 void simpleExpressionNoExtraction() {