[PATCH] D53926: [clangd] Only add global scope to completion query scopes for TU context.

Eric Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 31 03:59:36 PDT 2018


ioeric created this revision.
ioeric added a reviewer: sammccall.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.

This avoids duplicated scopes in the query e.g. when anonymous namespace
is present.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D53926

Files:
  clangd/CodeComplete.cpp
  unittests/clangd/CodeCompleteTests.cpp


Index: unittests/clangd/CodeCompleteTests.cpp
===================================================================
--- unittests/clangd/CodeCompleteTests.cpp
+++ unittests/clangd/CodeCompleteTests.cpp
@@ -1142,6 +1142,18 @@
                                           UnorderedElementsAre(""))));
 }
 
+TEST(CompletionTest, NoDuplicatedGlobalScope) {
+  auto Requests = captureIndexRequests(R"cpp(
+      namespace {  void f(); }
+      namespace ns {
+      ^
+      } // namespace ns
+  )cpp");
+
+  EXPECT_THAT(Requests, ElementsAre(Field(&FuzzyFindRequest::Scopes,
+                                          ElementsAre("ns::", ""))));
+}
+
 TEST(CompletionTest, NoIndexCompletionsInsideClasses) {
   auto Completions = completions(
       R"cpp(
Index: clangd/CodeComplete.cpp
===================================================================
--- clangd/CodeComplete.cpp
+++ clangd/CodeComplete.cpp
@@ -560,10 +560,13 @@
   auto GetAllAccessibleScopes = [](CodeCompletionContext &CCContext) {
     SpecifiedScope Info;
     for (auto *Context : CCContext.getVisitedContexts()) {
-      if (isa<TranslationUnitDecl>(Context))
+      if (isa<TranslationUnitDecl>(Context)) {
         Info.AccessibleScopes.push_back(""); // global namespace
-      else if (isa<NamespaceDecl>(Context))
-        Info.AccessibleScopes.push_back(printNamespaceScope(*Context));
+      } else if (isa<NamespaceDecl>(Context)) {
+        auto S = printNamespaceScope(*Context);
+        if (!S.empty())
+          Info.AccessibleScopes.push_back(std::move(S));
+      }
     }
     return Info;
   };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53926.171887.patch
Type: text/x-patch
Size: 1583 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181031/98cf8044/attachment.bin>


More information about the cfe-commits mailing list