Author: Adam Czachorowski
Date: 2020-05-06T15:50:54+02:00
New Revision: 319787315dd65f05aea562760cf57ac486bd2812

URL: 
https://github.com/llvm/llvm-project/commit/319787315dd65f05aea562760cf57ac486bd2812
DIFF: 
https://github.com/llvm/llvm-project/commit/319787315dd65f05aea562760cf57ac486bd2812.diff

LOG: [clangd] Do not offer "Add using" tweak in header files.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, 
cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
    clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index 836ac9ac57ab..f5eee85193ce 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -175,6 +175,12 @@ findInsertionPoint(const Tweak::Selection &Inputs,
 
 bool AddUsing::prepare(const Selection &Inputs) {
   auto &SM = Inputs.AST->getSourceManager();
+
+  // Do not suggest "using" in header files. That way madness lies.
+  if (isHeaderFile(SM.getFileEntryForID(SM.getMainFileID())->getName(),
+                   Inputs.AST->getLangOpts()))
+    return false;
+
   auto *Node = Inputs.ASTSelection.commonAncestor();
   if (Node == nullptr)
     return false;

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp 
b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index b93cca23e6b6..e85d2624d265 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -2463,6 +2463,13 @@ class cc {
   // test that we don't crash.
   EXPECT_UNAVAILABLE(Header +
                      "template<typename TT> using foo = one::tt<T^T>;");
+
+  // Check that we do not trigger in header files.
+  FileName = "test.h";
+  ExtraArgs.push_back("-xc++-header"); // .h file is treated a C by default.
+  EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }");
+  FileName = "test.hpp";
+  EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }");
 }
 
 TEST_F(AddUsingTest, Apply) {


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to