diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -555,7 +555,7 @@ }}, {"signatureHelpProvider", llvm::json::Object{ - {"triggerCharacters", {"(", ",", ")"}}, + {"triggerCharacters", {"(", ")", "{", "}", ","}}, }}, {"declarationProvider", true}, {"definitionProvider", true}, diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -923,7 +923,8 @@ void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, - SourceLocation OpenParLoc) override { + SourceLocation OpenParLoc, + bool Braced) override { assert(!OpenParLoc.isInvalid()); SourceManager &SrcMgr = S.getSourceManager(); OpenParLoc = SrcMgr.getFileLoc(OpenParLoc); @@ -951,8 +952,11 @@ // long in some cases (e.g. when 'T' is substituted with 'std::string', we // would get 'std::basic_string'). if (auto *Func = Candidate.getFunction()) { - if (auto *Pattern = Func->getTemplateInstantiationPattern()) - Candidate = OverloadCandidate(Pattern); + if (auto *Pattern = Func->getTemplateInstantiationPattern()) { + auto NewCandidate = OverloadCandidate(Pattern); + NewCandidate.IsInitListConstructor = Candidate.IsInitListConstructor; + Candidate = NewCandidate; + } } if (static_cast(I) == SigHelp.activeSignature) { // The activeParameter in LSP relates to the activeSignature. There is @@ -963,8 +967,9 @@ paramIndexForArg(Candidate, SigHelp.activeParameter); } - const auto *CCS = Candidate.CreateSignatureString( - CurrentArg, S, *Allocator, CCTUInfo, true); + const auto *CCS = + Candidate.CreateSignatureString(CurrentArg, S, *Allocator, CCTUInfo, + /*IncludeBriefComment=*/true, Braced); assert(CCS && "Expected the CodeCompletionString to be non-null"); ScoredSignatures.push_back(processOverloadCandidate( Candidate, *CCS, @@ -1162,7 +1167,8 @@ void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, - SourceLocation OpenParLoc) override { + SourceLocation OpenParLoc, + bool Braced) override { assert(CurrentArg <= (unsigned)std::numeric_limits::max() && "too many arguments"); diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -1212,6 +1212,10 @@ std::string Text; std::pair Offsets; }; +llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, + const ExpectedParameter &P) { + return OS << P.Text; +} MATCHER_P(ParamsAre, P, "") { if (P.size() != arg.parameters.size()) return false; @@ -1260,6 +1264,35 @@ EXPECT_EQ(0, Results.activeParameter); } +TEST(SignatureHelpTest, Constructors) { + std::string Top = R"cpp( + struct S { + S(int); + S(const S &) = delete; + }; + )cpp"; + + auto CheckParenInit = [&](std::string Init) { + EXPECT_THAT(signatures(Top + Init).signatures, + UnorderedElementsAre(Sig("S([[int]])"))) + << Init; + }; + CheckParenInit("S s(^);"); + CheckParenInit("auto s = S(^);"); + CheckParenInit("auto s = new S(^);"); + + auto CheckBracedInit = [&](std::string Init) { + EXPECT_THAT(signatures(Top + Init).signatures, + UnorderedElementsAre(Sig("S{[[int]]}"))) + << Init; + }; + CheckBracedInit("S s{^};"); + CheckBracedInit("S s = {^};"); + CheckBracedInit("auto s = S{^};"); + CheckBracedInit("auto s = new S{^};"); + CheckBracedInit("int x(S); int i = x({^});"); +} + TEST(SignatureHelpTest, OverloadInitListRegression) { auto Results = signatures(R"cpp( struct A {int x;}; diff --git a/clang/include/clang/Sema/CodeCompleteConsumer.h b/clang/include/clang/Sema/CodeCompleteConsumer.h --- a/clang/include/clang/Sema/CodeCompleteConsumer.h +++ b/clang/include/clang/Sema/CodeCompleteConsumer.h @@ -1048,6 +1048,10 @@ /// Determine the kind of overload candidate. CandidateKind getKind() const { return Kind; } + /// True if this candidate converts args to a std::initializer_list, as in + /// std::vector X = {1, 2, 3}; + bool IsInitListConstructor; + /// Retrieve the function overload candidate or the templated /// function declaration for a function template. FunctionDecl *getFunction() const; @@ -1064,11 +1068,11 @@ /// Create a new code-completion string that describes the function /// signature of this overload candidate. - CodeCompletionString *CreateSignatureString(unsigned CurrentArg, - Sema &S, - CodeCompletionAllocator &Allocator, - CodeCompletionTUInfo &CCTUInfo, - bool IncludeBriefComments) const; + CodeCompletionString * + CreateSignatureString(unsigned CurrentArg, Sema &S, + CodeCompletionAllocator &Allocator, + CodeCompletionTUInfo &CCTUInfo, + bool IncludeBriefComments, bool Braced) const; }; CodeCompleteConsumer(const CodeCompleteOptions &CodeCompleteOpts) @@ -1142,7 +1146,8 @@ virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, - SourceLocation OpenParLoc) {} + SourceLocation OpenParLoc, + bool Braced) {} //@} /// Retrieve the allocator that will be used to allocate @@ -1193,7 +1198,8 @@ void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, - SourceLocation OpenParLoc) override; + SourceLocation OpenParLoc, + bool Braced) override; bool isResultFilteredOut(StringRef Filter, CodeCompletionResult Results) override; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -12539,13 +12539,12 @@ QualType ProduceConstructorSignatureHelp(Scope *S, QualType Type, SourceLocation Loc, ArrayRef Args, - SourceLocation OpenParLoc); - QualType ProduceCtorInitMemberSignatureHelp(Scope *S, Decl *ConstructorDecl, - CXXScopeSpec SS, - ParsedType TemplateTypeTy, - ArrayRef ArgExprs, - IdentifierInfo *II, - SourceLocation OpenParLoc); + SourceLocation OpenParLoc, + bool Braced); + QualType ProduceCtorInitMemberSignatureHelp( + Scope *S, Decl *ConstructorDecl, CXXScopeSpec SS, + ParsedType TemplateTypeTy, ArrayRef ArgExprs, IdentifierInfo *II, + SourceLocation OpenParLoc, bool Braced); void CodeCompleteInitializer(Scope *S, Decl *D); /// Trigger code completion for a record of \p BaseType. \p InitExprs are /// expressions in the initializer list seen so far and \p D is the current diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -1922,9 +1922,10 @@ void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, - SourceLocation OpenParLoc) override { + SourceLocation OpenParLoc, + bool Braced) override { Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates, - OpenParLoc); + OpenParLoc, Braced); } CodeCompletionAllocator &getAllocator() override { diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -2420,7 +2420,8 @@ auto RunSignatureHelp = [&]() { QualType PreferredType = Actions.ProduceConstructorSignatureHelp( getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), - ThisDecl->getLocation(), Exprs, T.getOpenLocation()); + ThisDecl->getLocation(), Exprs, T.getOpenLocation(), + /*Braced=*/false); CalledSignatureHelp = true; return PreferredType; }; @@ -2440,7 +2441,8 @@ if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) { Actions.ProduceConstructorSignatureHelp( getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), - ThisDecl->getLocation(), Exprs, T.getOpenLocation()); + ThisDecl->getLocation(), Exprs, T.getOpenLocation(), + /*Braced=*/false); CalledSignatureHelp = true; } Actions.ActOnInitializerError(ThisDecl); diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -3740,8 +3740,8 @@ if (TemplateTypeTy.isInvalid()) return QualType(); QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp( - getCurScope(), ConstructorDecl, SS, TemplateTypeTy.get(), ArgExprs, II, - T.getOpenLocation()); + getCurScope(), ConstructorDecl, SS, TemplateTypeTy.get(), ArgExprs, + II, T.getOpenLocation(), /*Braced=*/false); CalledSignatureHelp = true; return PreferredType; }; diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp --- a/clang/lib/Parse/ParseExprCXX.cpp +++ b/clang/lib/Parse/ParseExprCXX.cpp @@ -1878,7 +1878,7 @@ if (TypeRep) PreferredType = Actions.ProduceConstructorSignatureHelp( getCurScope(), TypeRep.get()->getCanonicalTypeInternal(), - DS.getEndLoc(), Exprs, T.getOpenLocation()); + DS.getEndLoc(), Exprs, T.getOpenLocation(), /*Braced=*/false); CalledSignatureHelp = true; return PreferredType; }; @@ -3168,7 +3168,8 @@ if (TypeRep) PreferredType = Actions.ProduceConstructorSignatureHelp( getCurScope(), TypeRep.get()->getCanonicalTypeInternal(), - DeclaratorInfo.getEndLoc(), ConstructorArgs, ConstructorLParen); + DeclaratorInfo.getEndLoc(), ConstructorArgs, ConstructorLParen, + /*Braced=*/false); CalledSignatureHelp = true; return PreferredType; }; diff --git a/clang/lib/Parse/ParseInit.cpp b/clang/lib/Parse/ParseInit.cpp --- a/clang/lib/Parse/ParseInit.cpp +++ b/clang/lib/Parse/ParseInit.cpp @@ -459,12 +459,22 @@ Actions, EnterExpressionEvaluationContext::InitList); bool InitExprsOk = true; - DesignatorCompletionInfo DesignatorCompletion{ - InitExprs, - PreferredType.get(T.getOpenLocation()), + QualType LikelyType = PreferredType.get(T.getOpenLocation()); + DesignatorCompletionInfo DesignatorCompletion{InitExprs, LikelyType}; + bool CalledSignatureHelp = false; + auto RunSignatureHelp = [&] { + QualType PreferredType; + if (!LikelyType.isNull()) + PreferredType = Actions.ProduceConstructorSignatureHelp( + getCurScope(), LikelyType->getCanonicalTypeInternal(), + T.getOpenLocation(), InitExprs, T.getOpenLocation(), /*Braced=*/true); + CalledSignatureHelp = true; + return PreferredType; }; while (1) { + PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp); + // Handle Microsoft __if_exists/if_not_exists if necessary. if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) { @@ -530,7 +540,6 @@ return ExprError(); // an error occurred. } - // Return true if a comma (or closing brace) is necessary after the // __if_exists/if_not_exists statement. bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs, diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp --- a/clang/lib/Parse/ParseOpenMP.cpp +++ b/clang/lib/Parse/ParseOpenMP.cpp @@ -470,7 +470,7 @@ auto RunSignatureHelp = [this, OmpPrivParm, LParLoc, &Exprs]() { QualType PreferredType = Actions.ProduceConstructorSignatureHelp( getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(), - OmpPrivParm->getLocation(), Exprs, LParLoc); + OmpPrivParm->getLocation(), Exprs, LParLoc, /*Braced=*/false); CalledSignatureHelp = true; return PreferredType; }; diff --git a/clang/lib/Sema/CodeCompleteConsumer.cpp b/clang/lib/Sema/CodeCompleteConsumer.cpp --- a/clang/lib/Sema/CodeCompleteConsumer.cpp +++ b/clang/lib/Sema/CodeCompleteConsumer.cpp @@ -645,7 +645,7 @@ void PrintingCodeCompleteConsumer::ProcessOverloadCandidates( Sema &SemaRef, unsigned CurrentArg, OverloadCandidate *Candidates, - unsigned NumCandidates, SourceLocation OpenParLoc) { + unsigned NumCandidates, SourceLocation OpenParLoc, bool Braced) { OS << "OPENING_PAREN_LOC: "; OpenParLoc.print(OS, SemaRef.getSourceManager()); OS << "\n"; @@ -653,7 +653,7 @@ for (unsigned I = 0; I != NumCandidates; ++I) { if (CodeCompletionString *CCS = Candidates[I].CreateSignatureString( CurrentArg, SemaRef, getAllocator(), CCTUInfo, - includeBriefComments())) { + includeBriefComments(), Braced)) { OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n"; } } diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -3761,7 +3761,8 @@ CodeCompletionString * CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( unsigned CurrentArg, Sema &S, CodeCompletionAllocator &Allocator, - CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments) const { + CodeCompletionTUInfo &CCTUInfo, bool IncludeBriefComments, + bool Braced) const { PrintingPolicy Policy = getCompletionPrintingPolicy(S); // Show signatures of constructors as they are declared: // vector(int n) rather than vector(int n) @@ -3780,9 +3781,11 @@ const FunctionType *FT = getFunctionType(); Result.AddResultTypeChunk(Result.getAllocator().CopyString( FT->getReturnType().getAsString(Policy))); - Result.AddChunk(CodeCompletionString::CK_LeftParen); + Result.AddChunk(Braced ? CodeCompletionString::CK_LeftBrace + : CodeCompletionString::CK_LeftParen); Result.AddChunk(CodeCompletionString::CK_CurrentParameter, "..."); - Result.AddChunk(CodeCompletionString::CK_RightParen); + Result.AddChunk(Braced ? CodeCompletionString::CK_RightBrace + : CodeCompletionString::CK_RightParen); return Result.TakeString(); } @@ -3802,10 +3805,12 @@ Proto->getReturnType().getAsString(Policy))); } - Result.AddChunk(CodeCompletionString::CK_LeftParen); + Result.AddChunk(Braced ? CodeCompletionString::CK_LeftBrace + : CodeCompletionString::CK_LeftParen); AddOverloadParameterChunks(S.getASTContext(), Policy, FDecl, Proto, Result, CurrentArg); - Result.AddChunk(CodeCompletionString::CK_RightParen); + Result.AddChunk(Braced ? CodeCompletionString::CK_RightBrace + : CodeCompletionString::CK_RightParen); return Result.TakeString(); } @@ -5860,15 +5865,15 @@ return ParamType; } -static QualType -ProduceSignatureHelp(Sema &SemaRef, Scope *S, - MutableArrayRef Candidates, - unsigned CurrentArg, SourceLocation OpenParLoc) { +static QualType ProduceSignatureHelp( + Sema &SemaRef, Scope *S, MutableArrayRef Candidates, + unsigned CurrentArg, SourceLocation OpenParLoc, bool Braced) { if (Candidates.empty()) return QualType(); if (SemaRef.getPreprocessor().isCodeCompletionReached()) SemaRef.CodeCompleter->ProcessOverloadCandidates( - SemaRef, CurrentArg, Candidates.data(), Candidates.size(), OpenParLoc); + SemaRef, CurrentArg, Candidates.data(), Candidates.size(), OpenParLoc, + Braced); return getParamType(SemaRef, Candidates, CurrentArg); } @@ -5970,15 +5975,16 @@ } } mergeCandidatesWithResults(*this, Results, CandidateSet, Loc, Args.size()); - QualType ParamType = - ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); + QualType ParamType = ProduceSignatureHelp(*this, S, Results, Args.size(), + OpenParLoc, /*Braced=*/false); return !CandidateSet.empty() ? ParamType : QualType(); } QualType Sema::ProduceConstructorSignatureHelp(Scope *S, QualType Type, SourceLocation Loc, ArrayRef Args, - SourceLocation OpenParLoc) { + SourceLocation OpenParLoc, + bool Braced) { if (!CodeCompleter) return QualType(); @@ -5987,6 +5993,10 @@ isCompleteType(Loc, Type) ? Type->getAsCXXRecordDecl() : nullptr; if (!RD) return Type; + // FIXME: we don't support signature help for aggregate initialization, so + // don't offer a confusing partial list (e.g. the copy constructor). + if (Braced && RD->isAggregate()) + return Type; // FIXME: Provide support for member initializers. // FIXME: Provide support for variadic template constructors. @@ -5995,12 +6005,20 @@ for (NamedDecl *C : LookupConstructors(RD)) { if (auto *FD = dyn_cast(C)) { + // FIXME: we can't yet provide correct signature help for initializer + // list constructors, so skip them entirely. + if (Braced && LangOpts.CPlusPlus && isInitListConstructor(FD)) + continue; AddOverloadCandidate(FD, DeclAccessPair::make(FD, C->getAccess()), Args, CandidateSet, /*SuppressUserConversions=*/false, /*PartialOverloading=*/true, /*AllowExplicit*/ true); } else if (auto *FTD = dyn_cast(C)) { + if (Braced && LangOpts.CPlusPlus && + isInitListConstructor(FTD->getTemplatedDecl())) + continue; + AddTemplateOverloadCandidate( FTD, DeclAccessPair::make(FTD, C->getAccess()), /*ExplicitTemplateArgs=*/nullptr, Args, CandidateSet, @@ -6011,12 +6029,14 @@ SmallVector Results; mergeCandidatesWithResults(*this, Results, CandidateSet, Loc, Args.size()); - return ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc); + return ProduceSignatureHelp(*this, S, Results, Args.size(), OpenParLoc, + Braced); } QualType Sema::ProduceCtorInitMemberSignatureHelp( Scope *S, Decl *ConstructorDecl, CXXScopeSpec SS, ParsedType TemplateTypeTy, - ArrayRef ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc) { + ArrayRef ArgExprs, IdentifierInfo *II, SourceLocation OpenParLoc, + bool Braced) { if (!CodeCompleter) return QualType(); @@ -6029,7 +6049,7 @@ Constructor->getParent(), SS, TemplateTypeTy, II)) return ProduceConstructorSignatureHelp(getCurScope(), MemberDecl->getType(), MemberDecl->getLocation(), ArgExprs, - OpenParLoc); + OpenParLoc, Braced); return QualType(); } diff --git a/clang/test/CodeCompletion/ctor-signature.cpp b/clang/test/CodeCompletion/ctor-signature.cpp --- a/clang/test/CodeCompletion/ctor-signature.cpp +++ b/clang/test/CodeCompletion/ctor-signature.cpp @@ -15,3 +15,40 @@ // CHECK-CC2: OVERLOAD: Foo(<#const Foo &#>) // CHECK-CC2: OVERLOAD: Foo(<#Foo &&#> } + +namespace std { +template struct initializer_list {}; +} // namespace std + +struct Bar { + // CHECK-BRACED: OVERLOAD: Bar{<#int#>} + Bar(int); + // CHECK-BRACED: OVERLOAD: Bar{<#double#>, double} + Bar(double, double); + // FIXME: no support for init-list constructors yet. + // CHECK-BRACED-NOT: OVERLOAD: {{.*}}char + Bar(std::initializer_list C); + // CHECK-BRACED: OVERLOAD: Bar{<#const Bar &#>} + // CHECK-BRACED: OVERLOAD: Bar{<#T *Pointer#>} + template Bar(T *Pointer); +}; + +auto b1 = Bar{}; +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:36:15 %s | FileCheck -check-prefix=CHECK-BRACED %s +Bar b2{}; +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:38:8 %s | FileCheck -check-prefix=CHECK-BRACED %s +static int consumeBar(Bar) { return 0; } +int b3 = consumeBar({}); +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:41:22 %s | FileCheck -check-prefix=CHECK-BRACED %s + +struct Aggregate { + // FIXME: no support for aggregates yet. + // CHECK-AGGREGATE-NOT: OVERLOAD: Aggregate{<#const Aggregate &#>} + // CHECK-AGGREGATE-NOT: OVERLOAD: {{.*}}first + int first; + int second; +}; + +Aggregate a{}; +// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:52:13 %s | FileCheck -check-prefix=CHECK-AGGREGATE %s + diff --git a/clang/tools/libclang/CIndexCodeCompletion.cpp b/clang/tools/libclang/CIndexCodeCompletion.cpp --- a/clang/tools/libclang/CIndexCodeCompletion.cpp +++ b/clang/tools/libclang/CIndexCodeCompletion.cpp @@ -656,14 +656,15 @@ void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, OverloadCandidate *Candidates, unsigned NumCandidates, - SourceLocation OpenParLoc) override { + SourceLocation OpenParLoc, + bool Braced) override { StoredResults.reserve(StoredResults.size() + NumCandidates); for (unsigned I = 0; I != NumCandidates; ++I) { - CodeCompletionString *StoredCompletion - = Candidates[I].CreateSignatureString(CurrentArg, S, getAllocator(), + CodeCompletionString *StoredCompletion = + Candidates[I].CreateSignatureString(CurrentArg, S, getAllocator(), getCodeCompletionTUInfo(), - includeBriefComments()); - + includeBriefComments(), Braced); + CXCompletionResult R; R.CursorKind = CXCursor_OverloadCandidate; R.CompletionString = StoredCompletion;