[clang-tools-extra] r322185 - [clangd] Remove duplicates from code completion

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 10 05:51:09 PST 2018


Author: ibiryukov
Date: Wed Jan 10 05:51:09 2018
New Revision: 322185

URL: http://llvm.org/viewvc/llvm-project?rev=322185&view=rev
Log:
[clangd] Remove duplicates from code completion

Summary:
This patch removes hidden items from code completion.
Items can be hidden, e.g., by other items in the child scopes.
This patch addresses a particular problem of a duplicate completion
item for the class in the following example:

    struct Adapter { void method(); };
    void Adapter::method() {
       Adapter^
    }

We should probably investigate if there are other duplicates in
completion and remove them, possibly adding assertions that it never
happens.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: cfe-commits, klimek

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

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=322185&r1=322184&r2=322185&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original)
+++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Wed Jan 10 05:51:09 2018
@@ -294,6 +294,13 @@ public:
     std::priority_queue<CompletionCandidate> Candidates;
     for (unsigned I = 0; I < NumResults; ++I) {
       auto &Result = Results[I];
+      // We drop hidden items, as they cannot be found by the lookup after
+      // inserting the corresponding completion item and only produce noise and
+      // duplicates in the completion list. However, there is one exception. If
+      // Result has a Qualifier which is non-informative, we can refer to an
+      // item by adding that qualifier, so we don't filter out this item.
+      if (Result.Hidden && (!Result.Qualifier || Result.QualifierIsInformative))
+        continue;
       if (!ClangdOpts.IncludeIneligibleResults &&
           (Result.Availability == CXAvailability_NotAvailable ||
            Result.Availability == CXAvailability_NotAccessible))

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=322185&r1=322184&r2=322185&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp Wed Jan 10 05:51:09 2018
@@ -592,6 +592,22 @@ TEST(CompletionTest, ASTIndexMultiFile)
                                             Doc("Doooc"), Detail("void"))));
 }
 
+TEST(CompletionTest, NoDuplicates) {
+  auto Items = completions(R"cpp(
+struct Adapter {
+  void method();
+};
+
+void Adapter::method() {
+  Adapter^
+}
+  )cpp")
+                   .items;
+
+  // Make sure there are no duplicate entries of 'Adapter'.
+  EXPECT_THAT(Items, ElementsAre(Named("Adapter"), Named("~Adapter")));
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang




More information about the cfe-commits mailing list