[PATCH] D92847: [clangd] ExpandAutoType: Do not offer code action on lambdas.

Adam Czachorowski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 8 07:36:53 PST 2020


adamcz created this revision.
Herald added subscribers: usaxena95, kadircet, arphaman.
adamcz requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

We can't expand lambda types anyway. Now we simply not offer the code
action instead of showing it and then returning an error in apply().


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92847

Files:
  clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
  clang-tools-extra/clangd/test/check-fail.test
  clang-tools-extra/clangd/unittests/TweakTests.cpp


Index: clang-tools-extra/clangd/unittests/TweakTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -559,8 +559,7 @@
   EXPECT_THAT(apply("au^to x = &ns::Func;"),
               StartsWith("fail: Could not expand type of function pointer"));
   // lambda types are not replaced
-  EXPECT_THAT(apply("au^to x = []{};"),
-              StartsWith("fail: Could not expand type of lambda expression"));
+  EXPECT_UNAVAILABLE("au^to x = []{};");
   // inline namespaces
   EXPECT_EQ(apply("au^to x = inl_ns::Visible();"),
             "Visible x = inl_ns::Visible();");
Index: clang-tools-extra/clangd/test/check-fail.test
===================================================================
--- clang-tools-extra/clangd/test/check-fail.test
+++ clang-tools-extra/clangd/test/check-fail.test
@@ -11,4 +11,5 @@
 // CHECK: All checks completed, 2 errors
 
 #include "missing.h"
-auto x = []{};
+void fun();
+auto x = fun;
Index: clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -74,6 +74,31 @@
           CachedLocation = Result;
       }
     }
+
+    if (!CachedLocation)
+      return false;
+
+    // apply() below will use RecursiveASTVisitor to figure out the deduced
+    // type. That takes longer, but will handle all cases.
+    // This scan of parents here only handles one case (RecordDecl) and we only
+    // do it to prevent offering this tweak on lambda types (common use case for
+    // 'auto'), since it would fail in apply() anyway.
+    //
+    // In the future, We could re-use this node we found here in apply(), but a
+    // better approach would be to fix getDeducedType() to not require walking
+    // the entire file and just run in in prepare() instead.
+    for (auto *It = Node; It; It = It->Parent) {
+      if (auto *DD = It->ASTNode.get<DeclaratorDecl>()) {
+        if (DD->getTypeSourceInfo() &&
+            DD->getTypeSourceInfo()->getTypeLoc().getBeginLoc() ==
+                CachedLocation->getBeginLoc()) {
+          if (auto *RD = DD->getType()->getAsRecordDecl())
+            if (RD->isLambda())
+              return false;
+          break;
+        }
+      }
+    }
   }
   return (bool) CachedLocation;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92847.310205.patch
Type: text/x-patch
Size: 2497 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201208/f00f0eb7/attachment.bin>


More information about the cfe-commits mailing list