]> granicus.if.org Git - clang/commitdiff
[CodeComplete] Collect visited contexts when scope specifier is invalid.
authorEric Liu <ioeric@google.com>
Thu, 21 Feb 2019 11:22:58 +0000 (11:22 +0000)
committerEric Liu <ioeric@google.com>
Thu, 21 Feb 2019 11:22:58 +0000 (11:22 +0000)
Summary:
This will allow completion consumers to guess the specified scope by
putting together scopes in the context with the specified scope (e.g. when the
specified namespace is not imported yet).

Reviewers: ilya-biryukov

Subscribers: jdoerfert, cfe-commits

Tags: #clang

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@354570 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaCodeComplete.cpp
unittests/Sema/CodeCompleteTest.cpp

index a042abb6f5f8951ed47a4276682eac709e982068..cebf97a09abab863475878d156b90a5a238e828a 100644 (file)
@@ -5061,7 +5061,20 @@ void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
   if (SS.isInvalid()) {
     CodeCompletionContext CC(CodeCompletionContext::CCC_Symbol);
     CC.setCXXScopeSpecifier(SS);
-    HandleCodeCompleteResults(this, CodeCompleter, CC, nullptr, 0);
+    // As SS is invalid, we try to collect accessible contexts from the current
+    // scope with a dummy lookup so that the completion consumer can try to
+    // guess what the specified scope is.
+    ResultBuilder DummyResults(*this, CodeCompleter->getAllocator(),
+                               CodeCompleter->getCodeCompletionTUInfo(), CC);
+    if (S->getEntity()) {
+      CodeCompletionDeclConsumer Consumer(DummyResults, S->getEntity(),
+                                          BaseType);
+      LookupVisibleDecls(S, LookupOrdinaryName, Consumer,
+                         /*IncludeGlobalScope=*/false,
+                         /*LoadExternal=*/false);
+    }
+    HandleCodeCompleteResults(this, CodeCompleter,
+                              DummyResults.getCompletionContext(), nullptr, 0);
     return;
   }
   // Always pretend to enter a context to ensure that a dependent type
index 3bec79dc9ad03bfa16ae39365f8efd25b3b1d519..6bd6074a6637f2865a997e7157d2d686a9d70e72 100644 (file)
@@ -173,12 +173,16 @@ TEST(SemaCodeCompleteTest, VisitedNSForValidQualifiedId) {
                                               "foo::(anonymous)"));
 }
 
-TEST(SemaCodeCompleteTest, VisitedNSForInvalideQualifiedId) {
+TEST(SemaCodeCompleteTest, VisitedNSForInvalidQualifiedId) {
   auto VisitedNS = runCodeCompleteOnCode(R"cpp(
-     namespace ns { foo::^ }
+     namespace na {}
+     namespace ns1 {
+     using namespace na;
+     foo::^
+     }
   )cpp")
                        .VisitedNamespaces;
-  EXPECT_TRUE(VisitedNS.empty());
+  EXPECT_THAT(VisitedNS, UnorderedElementsAre("ns1", "na"));
 }
 
 TEST(SemaCodeCompleteTest, VisitedNSWithoutQualifier) {