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

Tom Praschan via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 9 11:05:16 PST 2022


Author: v1nh1shungry
Date: 2022-11-09T21:04:21+01:00
New Revision: 82ca918b5755f3c442e247563dc011f7b310e446

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

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

Avoid adding qualifiers before user-defined literals

Reviewed By: tom-anders

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

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 93fdbb9486cc7..f4228ae0be1f8 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
@@ -155,12 +155,23 @@ Expected<Tweak::Effect> RemoveUsingNamespace::apply(const Selection &Inputs) {
             if (!visibleContext(T->getDeclContext())
                      ->Equals(TargetDirective->getNominatedNamespace()))
               return;
+            auto Kind = T->getDeclName().getNameKind();
             // 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))
+            if (Kind == DeclarationName::CXXOperatorName)
+              return;
+            // Avoid adding qualifiers before user-defined literals, e.g.
+            //   using namespace std;
+            //   auto s = "foo"s; // Must not changed to auto s = "foo" std::s;
+            // FIXME: Add a using-directive for user-defined literals
+            // declared in an inline namespace, e.g.
+            //   using namespace s^td;
+            //   int main() { cout << "foo"s; }
+            // change to
+            //   using namespace std::literals;
+            //   int main() { std::cout << "foo"s; }
+            if (Kind == DeclarationName::NameKind::CXXLiteralOperatorName)
               return;
           }
           SourceLocation Loc = Ref.NameLoc;

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
index 3449c6475e3fc..03a46d954fcd4 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
@@ -249,6 +249,21 @@ TEST_F(RemoveUsingNamespaceTest, All) {
         ns::Foo foo;
         foo + 10;
       }
+    )cpp"},
+      {// Does not qualify user-defined literals
+       R"cpp(
+      namespace ns {
+      long double operator "" _w(long double);
+      }
+      using namespace n^s;
+      int main() { 1.5_w; }
+    )cpp",
+       R"cpp(
+      namespace ns {
+      long double operator "" _w(long double);
+      }
+      
+      int main() { 1.5_w; }
     )cpp"}};
   for (auto C : Cases)
     EXPECT_EQ(C.second, apply(C.first)) << C.first;


        


More information about the cfe-commits mailing list