Author: ibiryukov Date: Tue May 28 08:21:03 2019 New Revision: 361838 URL: http://llvm.org/viewvc/llvm-project?rev=361838&view=rev Log: [CodeComplete] Set preferred type for qualified-id
Reviewers: kadircet Reviewed By: kadircet Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62514 Modified: cfe/trunk/include/clang/Sema/Sema.h cfe/trunk/lib/Parse/ParseExprCXX.cpp cfe/trunk/lib/Sema/SemaCodeComplete.cpp cfe/trunk/unittests/Sema/CodeCompleteTest.cpp Modified: cfe/trunk/include/clang/Sema/Sema.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=361838&r1=361837&r2=361838&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/Sema.h (original) +++ cfe/trunk/include/clang/Sema/Sema.h Tue May 28 08:21:03 2019 @@ -10646,8 +10646,8 @@ public: void CodeCompleteInitializer(Scope *S, Decl *D); void CodeCompleteAfterIf(Scope *S); - void CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, - bool EnteringContext, QualType BaseType); + void CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, bool EnteringContext, + QualType BaseType, QualType PreferredType); void CodeCompleteUsing(Scope *S); void CodeCompleteUsingDirective(Scope *S); void CodeCompleteNamespaceDecl(Scope *S); Modified: cfe/trunk/lib/Parse/ParseExprCXX.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExprCXX.cpp?rev=361838&r1=361837&r2=361838&view=diff ============================================================================== --- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original) +++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Tue May 28 08:21:03 2019 @@ -232,13 +232,16 @@ bool Parser::ParseOptionalCXXScopeSpecif HasScopeSpecifier = true; } + // Preferred type might change when parsing qualifiers, we need the original. + auto SavedType = PreferredType; while (true) { if (HasScopeSpecifier) { if (Tok.is(tok::code_completion)) { // Code completion for a nested-name-specifier, where the code // completion token follows the '::'. Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext, - ObjectType.get()); + ObjectType.get(), + SavedType.get(SS.getBeginLoc())); // Include code completion token into the range of the scope otherwise // when we try to annotate the scope tokens the dangling code completion // token will cause assertion in Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=361838&r1=361837&r2=361838&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original) +++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Tue May 28 08:21:03 2019 @@ -5215,7 +5215,8 @@ void Sema::CodeCompleteAfterIf(Scope *S) } void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS, - bool EnteringContext, QualType BaseType) { + bool EnteringContext, QualType BaseType, + QualType PreferredType) { if (SS.isEmpty() || !CodeCompleter) return; @@ -5224,13 +5225,15 @@ void Sema::CodeCompleteQualifiedId(Scope // it can be useful for global code completion which have information about // contexts/symbols that are not in the AST. if (SS.isInvalid()) { - CodeCompletionContext CC(CodeCompletionContext::CCC_Symbol); + CodeCompletionContext CC(CodeCompletionContext::CCC_Symbol, PreferredType); CC.setCXXScopeSpecifier(SS); // 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 (!PreferredType.isNull()) + DummyResults.setPreferredType(PreferredType); if (S->getEntity()) { CodeCompletionDeclConsumer Consumer(DummyResults, S->getEntity(), BaseType); @@ -5253,9 +5256,12 @@ void Sema::CodeCompleteQualifiedId(Scope if (!isDependentScopeSpecifier(SS) && RequireCompleteDeclContext(SS, Ctx)) return; - ResultBuilder Results(*this, CodeCompleter->getAllocator(), - CodeCompleter->getCodeCompletionTUInfo(), - CodeCompletionContext::CCC_Symbol); + ResultBuilder Results( + *this, CodeCompleter->getAllocator(), + CodeCompleter->getCodeCompletionTUInfo(), + CodeCompletionContext(CodeCompletionContext::CCC_Symbol, PreferredType)); + if (!PreferredType.isNull()) + Results.setPreferredType(PreferredType); Results.EnterNewScope(); // The "template" keyword can follow "::" in the grammar, but only Modified: cfe/trunk/unittests/Sema/CodeCompleteTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Sema/CodeCompleteTest.cpp?rev=361838&r1=361837&r2=361838&view=diff ============================================================================== --- cfe/trunk/unittests/Sema/CodeCompleteTest.cpp (original) +++ cfe/trunk/unittests/Sema/CodeCompleteTest.cpp Tue May 28 08:21:03 2019 @@ -454,5 +454,31 @@ TEST(PreferredTypeTest, FunctionArgument } )cpp"; EXPECT_THAT(collectPreferredTypes(Code), Each("volatile double *")); + + Code = R"cpp( + namespace ns { + struct vector { + }; + } + void accepts_vector(ns::vector); + + void test() { + accepts_vector(^::^ns::^vector()); + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("ns::vector")); + + Code = R"cpp( + template <class T> + struct vector { using self = vector; }; + + void accepts_vector(vector<int>); + int foo(int); + + void test() { + accepts_vector(^::^vector<decltype(foo(1))>::^self); + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("vector<int>")); } } // namespace _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits