[PATCH] D141760: [clangd] fix extract function tweak signature spelling out lambda types

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


5chmidti created this revision.
5chmidti added reviewers: sammccall, nridge.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
5chmidti requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Fixes the signature of the extracted function when the signature contains a lambda.
Previously spelled out the type (i.e. loc, ...) of the lambda, now specifies auto.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141760

Files:
  clang-tools-extra/clangd/AST.cpp
  clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.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
 ^^^^^^^^^^^^^
 
+- Fixed extracted function signatures spelling out lambda types.
+
 Improvements to clang-doc
 -------------------------
 
Index: clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
+++ clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
@@ -190,6 +190,32 @@
     }]]
   )cpp";
   EXPECT_EQ(apply(CompoundFailInput), "unavailable");
+
+  std::string LambdaInput = R"cpp(
+auto bar() {
+  int x = 10;
+  auto lambda = [x]() { return x; };
+  [[int y = 42;
+  int z = lambda();]]
+
+  return lambda();
+}
+  )cpp";
+
+  std::string LambdaOutput = R"cpp(
+void extracted(auto &lambda) {
+int y = 42;
+  int z = lambda();
+}
+auto bar() {
+  int x = 10;
+  auto lambda = [x]() { return x; };
+  extracted(lambda);
+
+  return lambda();
+}
+  )cpp";
+  EXPECT_EQ(apply(LambdaInput), LambdaOutput);
 }
 
 TEST_F(ExtractFunctionTest, DifferentHeaderSourceTest) {
Index: clang-tools-extra/clangd/AST.cpp
===================================================================
--- clang-tools-extra/clangd/AST.cpp
+++ clang-tools-extra/clangd/AST.cpp
@@ -430,6 +430,11 @@
   PrintCB PCB(&CurContext);
   PP.Callbacks = &PCB;
 
+  if (const auto *const RDecl = QT->getAsCXXRecordDecl();
+      RDecl && RDecl->isLambda()) {
+    return "auto";
+  }
+
   QT.print(OS, PP, Placeholder);
   return OS.str();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141760.489236.patch
Type: text/x-patch
Size: 1717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230114/bb9a83b4/attachment.bin>


More information about the cfe-commits mailing list