[PATCH] D141757: [clangd] allow extracting to variable for complete lambda expressions

Julian Schmidt via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 14 05:16:09 PST 2023


5chmidti updated this revision to Diff 489232.
5chmidti added a comment.

Add change to release notes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141757/new/

https://reviews.llvm.org/D141757

Files:
  clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
  clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
  clang-tools-extra/docs/ReleaseNotes.rst


Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -78,6 +78,8 @@
 Miscellaneous
 ^^^^^^^^^^^^^
 
+- The extract variable tweak gained support for extracting complete lambda expressions to a variable.
+
 Improvements to clang-doc
 -------------------------
 
Index: clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
@@ -98,6 +98,7 @@
       return [[t]].bar([[t]].z);
     }
     void v() { return; }
+    template <typename T> void callable_sink(T) {}
     // function default argument
     void f(int b = [[1]]) {
       // empty selection
@@ -131,6 +132,21 @@
       goto label;
       label:
         a = [[1]];
+
+      // lambdas
+      callable_sink([][[(){}]]);
+
+      // captures
+      int x = 0;
+      callable_sink([ [[=]] ](){});
+      callable_sink([ [[&]] ](){});
+      callable_sink([ [[x]] ](){});
+      callable_sink([ [[&x] ]](){});
+      callable_sink([y = [[x]] ](){});
+      callable_sink([ [[y = x]] ](){});
+
+      // default args
+      callable_sink([](int x = [[10]]){});
     }
   )cpp";
   EXPECT_UNAVAILABLE(UnavailableCases);
@@ -282,6 +298,19 @@
                  void f() {
                    auto placeholder = S(2) + S(3) + S(4); S x = S(1) + placeholder + S(5);
                  })cpp"},
+      // Complete lambda expressions
+      {R"cpp(template <typename T> void f(T) {}
+                void f2() {
+                  f([[ [](){ return 42; }]]);
+                }
+                )cpp",
+       R"cpp(template <typename T> void f(T) {}
+                void f2() {
+                  auto placeholder = [](){ return 42; }; f( placeholder);
+                }
+                )cpp"},
+      {R"cpp(void f() { auto x = [[ [](){ return 42; }]]; })cpp",
+       R"cpp(void f() { auto placeholder = [](){ return 42; }; auto x =  placeholder; })cpp"},
       // Don't try to analyze across macro boundaries
       // FIXME: it'd be nice to do this someday (in a safe way)
       {R"cpp(#define ECHO(X) X
Index: clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
@@ -152,10 +152,11 @@
   auto CanExtractOutside =
       [](const SelectionTree::Node *InsertionPoint) -> bool {
     if (const clang::Stmt *Stmt = InsertionPoint->ASTNode.get<clang::Stmt>()) {
-      // Allow all expressions except LambdaExpr since we don't want to extract
-      // from the captures/default arguments of a lambda
+      // Allow all expressions except partial LambdaExpr selections since we
+      // don't want to extract from the captures/default arguments of a lambda
       if (isa<clang::Expr>(Stmt))
-        return !isa<LambdaExpr>(Stmt);
+        return !(isa<LambdaExpr>(Stmt) &&
+                 InsertionPoint->Selected != SelectionTree::Complete);
       // We don't yet allow extraction from switch/case stmt as we would need to
       // jump over the switch stmt even if there is a CompoundStmt inside the
       // switch. And there are other Stmts which we don't care about (e.g.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141757.489232.patch
Type: text/x-patch
Size: 3519 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230114/09c6a606/attachment.bin>


More information about the cfe-commits mailing list