From fd5894c8584d9ec141dba7984315e439a59088f2 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Thu, 21 Feb 2019 11:22:58 +0000 Subject: [PATCH] [CodeComplete] Collect visited contexts when scope specifier is invalid. 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 | 15 ++++++++++++++- unittests/Sema/CodeCompleteTest.cpp | 10 +++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index a042abb6f5..cebf97a09a 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -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 diff --git a/unittests/Sema/CodeCompleteTest.cpp b/unittests/Sema/CodeCompleteTest.cpp index 3bec79dc9a..6bd6074a66 100644 --- a/unittests/Sema/CodeCompleteTest.cpp +++ b/unittests/Sema/CodeCompleteTest.cpp @@ -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) { -- 2.50.1