r318169 - [refactor][extract] avoid extracting expressions from types in functions

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 10:59:02 PST 2017


Author: arphaman
Date: Tue Nov 14 10:59:01 2017
New Revision: 318169

URL: http://llvm.org/viewvc/llvm-project?rev=318169&view=rev
Log:
[refactor][extract] avoid extracting expressions from types in functions

Modified:
    cfe/trunk/lib/Tooling/Refactoring/ASTSelection.cpp
    cfe/trunk/test/Refactor/Extract/ExtractExprIntoFunction.cpp

Modified: cfe/trunk/lib/Tooling/Refactoring/ASTSelection.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/ASTSelection.cpp?rev=318169&r1=318168&r2=318169&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Refactoring/ASTSelection.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/ASTSelection.cpp Tue Nov 14 10:59:01 2017
@@ -383,10 +383,12 @@ bool CodeRangeASTSelection::isInFunction
     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;
   }

Modified: cfe/trunk/test/Refactor/Extract/ExtractExprIntoFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Refactor/Extract/ExtractExprIntoFunction.cpp?rev=318169&r1=318168&r2=318169&view=diff
==============================================================================
--- cfe/trunk/test/Refactor/Extract/ExtractExprIntoFunction.cpp (original)
+++ cfe/trunk/test/Refactor/Extract/ExtractExprIntoFunction.cpp Tue Nov 14 10:59:01 2017
@@ -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() {




More information about the cfe-commits mailing list