[PATCH] D140915: [clangd] Fix getQueryScopes for using-directive with inline namespace

Tom Praschan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 3 11:58:47 PST 2023


tom-anders created this revision.
tom-anders added reviewers: nridge, kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
tom-anders requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

For example, in the folliwong code

  #include <string>
  
  using namespace std::string_literals;
  
  int main() {
      strin^ // Completes `string` instead of `std::string`
  }

The using declaration would make completion drop the std namespace, even though it shouldn't.

printNamespaceScope() skips inline namespaces, so to fix this use
printQualifiedName() instead

See https://github.com/clangd/clangd/issues/1451


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140915

Files:
  clang-tools-extra/clangd/CodeComplete.cpp
  clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp


Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -1700,9 +1700,11 @@
       namespace ns1 {}
       namespace ns2 {} // ignore
       namespace ns3 { namespace nns3 {} }
+      namespace ns4 { inline namespace ns4_inline {} }
       namespace foo {
       using namespace ns1;
       using namespace ns3::nns3;
+      using namespace ns4::ns4_inline;
       }
       namespace ns {
       void f() {
@@ -1711,10 +1713,11 @@
       }
   )cpp");
 
-  EXPECT_THAT(Requests,
-              ElementsAre(Field(
-                  &FuzzyFindRequest::Scopes,
-                  UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::"))));
+  EXPECT_THAT(
+      Requests,
+      ElementsAre(Field(&FuzzyFindRequest::Scopes,
+                        UnorderedElementsAre("foo::", "ns1::", "ns3::nns3::",
+                                             "ns4::ns4_inline::"))));
 }
 
 TEST(CompletionTest, UnresolvedQualifierIdQuery) {
Index: clang-tools-extra/clangd/CodeComplete.cpp
===================================================================
--- clang-tools-extra/clangd/CodeComplete.cpp
+++ clang-tools-extra/clangd/CodeComplete.cpp
@@ -670,8 +670,12 @@
   for (auto *Context : CCContext.getVisitedContexts()) {
     if (isa<TranslationUnitDecl>(Context))
       Scopes.AccessibleScopes.push_back(""); // global namespace
-    else if (isa<NamespaceDecl>(Context))
-      Scopes.AccessibleScopes.push_back(printNamespaceScope(*Context));
+    else if (const auto *ND = dyn_cast<NamespaceDecl>(Context)) {
+      if (ND->isInlineNamespace())
+        Scopes.AccessibleScopes.push_back(printQualifiedName(*ND) + "::");
+      else
+        Scopes.AccessibleScopes.push_back(printNamespaceScope(*ND));
+    }
   }
 
   const CXXScopeSpec *SemaSpecifier =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140915.486046.patch
Type: text/x-patch
Size: 1956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230103/c5995cdf/attachment-0001.bin>


More information about the cfe-commits mailing list