[clang-tools-extra] 3c5bed7 - [clangd] ExpandAutoType: Do not offer code action on lambdas.

Adam Czachorowski via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 8 11:08:20 PST 2020


Author: Adam Czachorowski
Date: 2020-12-08T20:03:16+01:00
New Revision: 3c5bed734f9e02bd3bc4fbd1e0acc53506823ebf

URL: https://github.com/llvm/llvm-project/commit/3c5bed734f9e02bd3bc4fbd1e0acc53506823ebf
DIFF: https://github.com/llvm/llvm-project/commit/3c5bed734f9e02bd3bc4fbd1e0acc53506823ebf.diff

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

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().

Differential Revision: https://reviews.llvm.org/D92847

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index 61f68a688252..6d38eb1de82a 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -63,6 +63,25 @@ bool isStructuredBindingType(const SelectionTree::Node *N) {
   return N && N->ASTNode.get<DecompositionDecl>();
 }
 
+// Returns true iff Node is a lambda, and thus should not be expanded. Loc is
+// the location of the auto type.
+bool isDeducedAsLambda(const SelectionTree::Node *Node, SourceLocation Loc) {
+  // getDeducedType() does a traversal, which we want to avoid in prepare().
+  // But at least check this isn't auto x = []{...};, which can't ever be
+  // expanded.
+  // (It would be nice if we had an efficient getDeducedType(), instead).
+  for (const auto *It = Node; It; It = It->Parent) {
+    if (const auto *DD = It->ASTNode.get<DeclaratorDecl>()) {
+      if (DD->getTypeSourceInfo() &&
+          DD->getTypeSourceInfo()->getTypeLoc().getBeginLoc() == Loc) {
+        if (auto *RD = DD->getType()->getAsRecordDecl())
+          return RD->isLambda();
+      }
+    }
+  }
+  return false;
+}
+
 bool ExpandAutoType::prepare(const Selection& Inputs) {
   CachedLocation = llvm::None;
   if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
@@ -70,11 +89,13 @@ bool ExpandAutoType::prepare(const Selection& Inputs) {
       if (const AutoTypeLoc Result = TypeNode->getAs<AutoTypeLoc>()) {
         // Code in apply() does handle 'decltype(auto)' yet.
         if (!Result.getTypePtr()->isDecltypeAuto() &&
-            !isStructuredBindingType(Node))
+            !isStructuredBindingType(Node) &&
+            !isDeducedAsLambda(Node, Result.getBeginLoc()))
           CachedLocation = Result;
       }
     }
   }
+
   return (bool) CachedLocation;
 }
 

diff  --git a/clang-tools-extra/clangd/test/check-fail.test b/clang-tools-extra/clangd/test/check-fail.test
index 0ee777f02cc5..dd50b59b2c79 100644
--- a/clang-tools-extra/clangd/test/check-fail.test
+++ b/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;

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index 85edd92d27da..0dcf8feb786f 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -559,8 +559,7 @@ TEST_F(ExpandAutoTypeTest, Test) {
   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();");


        


More information about the cfe-commits mailing list