[clang-tools-extra] f5a2ef8 - [clangd] Fix the code action `RemoveUsingNamespace`

Tom Praschan via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 6 08:32:17 PST 2022


Author: v1nh1shungry
Date: 2022-11-06T18:31:20+01:00
New Revision: f5a2ef80fa47d657877d5be314ce29ff7195d887

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

LOG: [clangd] Fix the code action `RemoveUsingNamespace`

Avoid adding qualifiers before C++ operators declared in a non-class context

Reviewed By: tom-anders

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
    clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
index 8df7a448c4383..93fdbb9486cc7 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
@@ -155,6 +155,13 @@ Expected<Tweak::Effect> RemoveUsingNamespace::apply(const Selection &Inputs) {
             if (!visibleContext(T->getDeclContext())
                      ->Equals(TargetDirective->getNominatedNamespace()))
               return;
+            // Avoid adding qualifiers before operators, e.g.
+            //   using namespace std;
+            //   cout << "foo"; // Must not changed to std::cout std:: << "foo"
+            // FIXME: User-defined literals are not handled
+            if (T->isInIdentifierNamespace(
+                    Decl::IdentifierNamespace::IDNS_NonMemberOperator))
+              return;
           }
           SourceLocation Loc = Ref.NameLoc;
           if (Loc.isMacroID()) {

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
index 59788e75d1698..3449c6475e3fc 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
@@ -226,6 +226,29 @@ TEST_F(RemoveUsingNamespaceTest, All) {
       int main() {
         std::vector V;
       }
+    )cpp"},
+      {// Does not qualify operators declared in a non-class context
+       R"cpp(
+      namespace ns {
+      struct Foo {};
+      void operator+(const Foo &, int) {}
+      }
+      using namespace n^s;
+      int main() {
+        Foo foo;
+        foo + 10;
+      }
+    )cpp",
+       R"cpp(
+      namespace ns {
+      struct Foo {};
+      void operator+(const Foo &, int) {}
+      }
+      
+      int main() {
+        ns::Foo foo;
+        foo + 10;
+      }
     )cpp"}};
   for (auto C : Cases)
     EXPECT_EQ(C.second, apply(C.first)) << C.first;


        


More information about the cfe-commits mailing list