[clang-tools-extra] r346224 - [clangd] Deduplicate query scopes.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 6 03:17:41 PST 2018


Author: ioeric
Date: Tue Nov  6 03:17:40 2018
New Revision: 346224

URL: http://llvm.org/viewvc/llvm-project?rev=346224&view=rev
Log:
[clangd] Deduplicate query scopes.

Summary:
For example, when anonymous namespace is present, duplicated namespaces might be
generated for the enclosing namespace.

Reviewers: ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

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

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

Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeComplete.cpp?rev=346224&r1=346223&r2=346224&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original)
+++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Tue Nov  6 03:17:40 2018
@@ -541,16 +541,14 @@ struct SpecifiedScope {
   // Set if the qualifier is not fully resolved by Sema.
   Optional<std::string> UnresolvedQualifier;
 
-  // Construct scopes being queried in indexes.
+  // Construct scopes being queried in indexes. The results are deduplicated.
   // This method format the scopes to match the index request representation.
   std::vector<std::string> scopesForIndexQuery() {
-    std::vector<std::string> Results;
-    for (StringRef AS : AccessibleScopes) {
-      Results.push_back(AS);
-      if (UnresolvedQualifier)
-        Results.back() += *UnresolvedQualifier;
-    }
-    return Results;
+    std::set<std::string> Results;
+    for (StringRef AS : AccessibleScopes)
+      Results.insert(
+          ((UnresolvedQualifier ? *UnresolvedQualifier : "") + AS).str());
+    return {Results.begin(), Results.end()};
   }
 };
 

Modified: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp?rev=346224&r1=346223&r2=346224&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp Tue Nov  6 03:17:40 2018
@@ -1142,6 +1142,23 @@ TEST(CompletionTest, GlobalQualifiedQuer
                                           UnorderedElementsAre(""))));
 }
 
+TEST(CompletionTest, NoDuplicatedQueryScopes) {
+  auto Requests = captureIndexRequests(R"cpp(
+      namespace {}
+
+      namespace na {
+      namespace {}
+      namespace nb {
+      ^
+      } // namespace nb
+      } // namespace na
+  )cpp");
+
+  EXPECT_THAT(Requests,
+              ElementsAre(Field(&FuzzyFindRequest::Scopes,
+                                UnorderedElementsAre("na::", "na::nb::", ""))));
+}
+
 TEST(CompletionTest, NoIndexCompletionsInsideClasses) {
   auto Completions = completions(
       R"cpp(




More information about the cfe-commits mailing list