[CodeComplete] Ensure object is the same in compareOverloads()
authorIlya Biryukov <ibiryukov@google.com>
Fri, 4 Oct 2019 08:10:27 +0000 (08:10 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Fri, 4 Oct 2019 08:10:27 +0000 (08:10 +0000)
Summary:
This fixes a regression that led to size() not being available in clangd
when completing 'deque().^' and using libc++.

Reviewers: sammccall

Reviewed By: sammccall

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

Tags: #clang

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

llvm-svn: 373710

clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
clang/lib/Sema/SemaCodeComplete.cpp
clang/test/CodeCompletion/member-access-qualifiers.cpp [new file with mode: 0644]

index 6e4322e..4cfb1a2 100644 (file)
@@ -2546,6 +2546,25 @@ TEST(CompletionTest, NamespaceDoubleInsertion) {
               UnorderedElementsAre(AllOf(Qualifier(""), Named("ABCDE"))));
 }
 
+TEST(CompletionTest, DerivedMethodsAreAlwaysVisible) {
+  // Despite the fact that base method matches the ref-qualifier better,
+  // completion results should only include the derived method.
+  auto Completions = completions(R"cpp(
+    struct deque_base {
+      float size();
+      double size() const;
+    };
+    struct deque : deque_base {
+        int size() const;
+    };
+
+    auto x = deque().^
+  )cpp")
+                         .Completions;
+  EXPECT_THAT(Completions,
+              ElementsAre(AllOf(ReturnType("int"), Named("size"))));
+}
+
 TEST(NoCompileCompletionTest, Basic) {
   auto Results = completionsNoCompile(R"cpp(
     void func() {
index e4bbee8..f24c3b2 100644 (file)
@@ -1185,6 +1185,9 @@ static OverloadCompare compareOverloads(const CXXMethodDecl &Candidate,
                                         const CXXMethodDecl &Incumbent,
                                         const Qualifiers &ObjectQuals,
                                         ExprValueKind ObjectKind) {
+  // Base/derived shadowing is handled elsewhere.
+  if (Candidate.getDeclContext() != Incumbent.getDeclContext())
+    return OverloadCompare::BothViable;
   if (Candidate.isVariadic() != Incumbent.isVariadic() ||
       Candidate.getNumParams() != Incumbent.getNumParams() ||
       Candidate.getMinRequiredArguments() !=
diff --git a/clang/test/CodeCompletion/member-access-qualifiers.cpp b/clang/test/CodeCompletion/member-access-qualifiers.cpp
new file mode 100644 (file)
index 0000000..93af02c
--- /dev/null
@@ -0,0 +1,13 @@
+struct deque_base {
+  int &size();
+  const int &size() const;
+};
+
+struct deque : private deque_base {
+    int size() const;
+};
+
+auto x = deque().
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:10:18 %s -o - | FileCheck %s
+// CHECK: COMPLETION: size : [#int#]size()[# const#]
+// CHECK: COMPLETION: size (Hidden,InBase,Inaccessible) : [#int &#]deque_base::size()