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;
 }


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;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to