Skip to content

Commit 2fab235

Browse files
committed
[CodeComplete] Report location of opening parens for signature help
Summary: Used in clangd. Reviewers: sammccall Reviewed By: sammccall Subscribers: ioeric, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D51436 llvm-svn: 341063
1 parent 9c16d80 commit 2fab235

File tree

11 files changed

+91
-39
lines changed

11 files changed

+91
-39
lines changed

clang/include/clang/Sema/CodeCompleteConsumer.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,13 @@ class CodeCompleteConsumer {
11141114
/// \param Candidates an array of overload candidates.
11151115
///
11161116
/// \param NumCandidates the number of overload candidates
1117+
///
1118+
/// \param OpenParLoc location of the opening parenthesis of the argument
1119+
/// list.
11171120
virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
11181121
OverloadCandidate *Candidates,
1119-
unsigned NumCandidates) {}
1122+
unsigned NumCandidates,
1123+
SourceLocation OpenParLoc) {}
11201124
//@}
11211125

11221126
/// Retrieve the allocator that will be used to allocate
@@ -1166,7 +1170,8 @@ class PrintingCodeCompleteConsumer : public CodeCompleteConsumer {
11661170

11671171
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
11681172
OverloadCandidate *Candidates,
1169-
unsigned NumCandidates) override;
1173+
unsigned NumCandidates,
1174+
SourceLocation OpenParLoc) override;
11701175

11711176
bool isResultFilteredOut(StringRef Filter, CodeCompletionResult Results) override;
11721177

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10230,9 +10230,11 @@ class Sema {
1023010230
const VirtSpecifiers *VS = nullptr);
1023110231
void CodeCompleteBracketDeclarator(Scope *S);
1023210232
void CodeCompleteCase(Scope *S);
10233-
void CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args);
10233+
void CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args,
10234+
SourceLocation OpenParLoc);
1023410235
void CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc,
10235-
ArrayRef<Expr *> Args);
10236+
ArrayRef<Expr *> Args,
10237+
SourceLocation OpenParLoc);
1023610238
void CodeCompleteInitializer(Scope *S, Decl *D);
1023710239
void CodeCompleteReturn(Scope *S);
1023810240
void CodeCompleteAfterIf(Scope *S);

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,8 +1911,10 @@ namespace {
19111911

19121912
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
19131913
OverloadCandidate *Candidates,
1914-
unsigned NumCandidates) override {
1915-
Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates);
1914+
unsigned NumCandidates,
1915+
SourceLocation OpenParLoc) override {
1916+
Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates,
1917+
OpenParLoc);
19161918
}
19171919

19181920
CodeCompletionAllocator &getAllocator() override {

clang/lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
23042304
auto ConstructorCompleter = [&, ThisVarDecl] {
23052305
Actions.CodeCompleteConstructor(
23062306
getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
2307-
ThisDecl->getLocation(), Exprs);
2307+
ThisDecl->getLocation(), Exprs, T.getOpenLocation());
23082308
};
23092309
if (ThisVarDecl) {
23102310
// ParseExpressionList can sometimes succeed even when ThisDecl is not

clang/lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,16 +1650,18 @@ Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
16501650
CommaLocsTy CommaLocs;
16511651

16521652
if (Tok.is(tok::code_completion)) {
1653-
Actions.CodeCompleteCall(getCurScope(), LHS.get(), None);
1653+
Actions.CodeCompleteCall(getCurScope(), LHS.get(), None,
1654+
PT.getOpenLocation());
16541655
cutOffParsing();
16551656
return ExprError();
16561657
}
16571658

16581659
if (OpKind == tok::l_paren || !LHS.isInvalid()) {
16591660
if (Tok.isNot(tok::r_paren)) {
16601661
if (ParseExpressionList(ArgExprs, CommaLocs, [&] {
1661-
Actions.CodeCompleteCall(getCurScope(), LHS.get(), ArgExprs);
1662-
})) {
1662+
Actions.CodeCompleteCall(getCurScope(), LHS.get(), ArgExprs,
1663+
PT.getOpenLocation());
1664+
})) {
16631665
(void)Actions.CorrectDelayedTyposInExpr(LHS);
16641666
LHS = ExprError();
16651667
} else if (LHS.isInvalid()) {

clang/lib/Parse/ParseExprCXX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
16871687
if (ParseExpressionList(Exprs, CommaLocs, [&] {
16881688
Actions.CodeCompleteConstructor(
16891689
getCurScope(), TypeRep.get()->getCanonicalTypeInternal(),
1690-
DS.getEndLoc(), Exprs);
1690+
DS.getEndLoc(), Exprs, T.getOpenLocation());
16911691
})) {
16921692
SkipUntil(tok::r_paren, StopAtSemi);
16931693
return ExprError();
@@ -2821,7 +2821,7 @@ Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) {
28212821
DeclaratorInfo).get();
28222822
Actions.CodeCompleteConstructor(
28232823
getCurScope(), TypeRep.get()->getCanonicalTypeInternal(),
2824-
DeclaratorInfo.getEndLoc(), ConstructorArgs);
2824+
DeclaratorInfo.getEndLoc(), ConstructorArgs, ConstructorLParen);
28252825
})) {
28262826
SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch);
28272827
return ExprError();

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,14 @@ void Parser::ParseOpenMPReductionInitializerForDecl(VarDecl *OmpPrivParm) {
415415
ExprVector Exprs;
416416
CommaLocsTy CommaLocs;
417417

418-
if (ParseExpressionList(Exprs, CommaLocs, [this, OmpPrivParm, &Exprs] {
419-
Actions.CodeCompleteConstructor(
420-
getCurScope(), OmpPrivParm->getType()->getCanonicalTypeInternal(),
421-
OmpPrivParm->getLocation(), Exprs);
422-
})) {
418+
SourceLocation LParLoc = T.getOpenLocation();
419+
if (ParseExpressionList(
420+
Exprs, CommaLocs, [this, OmpPrivParm, LParLoc, &Exprs] {
421+
Actions.CodeCompleteConstructor(
422+
getCurScope(),
423+
OmpPrivParm->getType()->getCanonicalTypeInternal(),
424+
OmpPrivParm->getLocation(), Exprs, LParLoc);
425+
})) {
423426
Actions.ActOnInitializerError(OmpPrivParm);
424427
SkipUntil(tok::r_paren, tok::annot_pragma_openmp_end, StopBeforeMatch);
425428
} else {

clang/lib/Sema/CodeCompleteConsumer.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
#include "clang/AST/DeclarationName.h"
2121
#include "clang/AST/Type.h"
2222
#include "clang/Basic/IdentifierTable.h"
23-
#include "clang/Sema/Sema.h"
2423
#include "clang/Lex/Preprocessor.h"
24+
#include "clang/Sema/Sema.h"
2525
#include "llvm/ADT/SmallString.h"
2626
#include "llvm/ADT/SmallVector.h"
2727
#include "llvm/ADT/StringRef.h"
2828
#include "llvm/ADT/Twine.h"
2929
#include "llvm/Support/Casting.h"
3030
#include "llvm/Support/Compiler.h"
3131
#include "llvm/Support/ErrorHandling.h"
32+
#include "llvm/Support/FormatVariadic.h"
3233
#include "llvm/Support/raw_ostream.h"
3334
#include <algorithm>
3435
#include <cassert>
@@ -624,16 +625,17 @@ static std::string getOverloadAsString(const CodeCompletionString &CCS) {
624625
return OS.str();
625626
}
626627

627-
void
628-
PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef,
629-
unsigned CurrentArg,
630-
OverloadCandidate *Candidates,
631-
unsigned NumCandidates) {
628+
void PrintingCodeCompleteConsumer::ProcessOverloadCandidates(
629+
Sema &SemaRef, unsigned CurrentArg, OverloadCandidate *Candidates,
630+
unsigned NumCandidates, SourceLocation OpenParLoc) {
631+
OS << "OPENING_PAREN_LOC: ";
632+
OpenParLoc.print(OS, SemaRef.getSourceManager());
633+
OS << "\n";
634+
632635
for (unsigned I = 0; I != NumCandidates; ++I) {
633-
if (CodeCompletionString *CCS
634-
= Candidates[I].CreateSignatureString(CurrentArg, SemaRef,
635-
getAllocator(), CCTUInfo,
636-
includeBriefComments())) {
636+
if (CodeCompletionString *CCS = Candidates[I].CreateSignatureString(
637+
CurrentArg, SemaRef, getAllocator(), CCTUInfo,
638+
includeBriefComments())) {
637639
OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
638640
}
639641
}

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4435,10 +4435,11 @@ static QualType getParamType(Sema &SemaRef,
44354435
return ParamType;
44364436
}
44374437

4438-
static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S,
4439-
MutableArrayRef<ResultCandidate> Candidates,
4440-
unsigned CurrentArg,
4441-
bool CompleteExpressionWithCurrentArg = true) {
4438+
static void
4439+
CodeCompleteOverloadResults(Sema &SemaRef, Scope *S,
4440+
MutableArrayRef<ResultCandidate> Candidates,
4441+
unsigned CurrentArg, SourceLocation OpenParLoc,
4442+
bool CompleteExpressionWithCurrentArg = true) {
44424443
QualType ParamType;
44434444
if (CompleteExpressionWithCurrentArg)
44444445
ParamType = getParamType(SemaRef, Candidates, CurrentArg);
@@ -4449,12 +4450,12 @@ static void CodeCompleteOverloadResults(Sema &SemaRef, Scope *S,
44494450
SemaRef.CodeCompleteExpression(S, ParamType);
44504451

44514452
if (!Candidates.empty())
4452-
SemaRef.CodeCompleter->ProcessOverloadCandidates(SemaRef, CurrentArg,
4453-
Candidates.data(),
4454-
Candidates.size());
4453+
SemaRef.CodeCompleter->ProcessOverloadCandidates(
4454+
SemaRef, CurrentArg, Candidates.data(), Candidates.size(), OpenParLoc);
44554455
}
44564456

4457-
void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) {
4457+
void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args,
4458+
SourceLocation OpenParLoc) {
44584459
if (!CodeCompleter)
44594460
return;
44604461

@@ -4552,12 +4553,13 @@ void Sema::CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args) {
45524553
}
45534554

45544555
mergeCandidatesWithResults(*this, Results, CandidateSet, Loc);
4555-
CodeCompleteOverloadResults(*this, S, Results, Args.size(),
4556+
CodeCompleteOverloadResults(*this, S, Results, Args.size(), OpenParLoc,
45564557
!CandidateSet.empty());
45574558
}
45584559

45594560
void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc,
4560-
ArrayRef<Expr *> Args) {
4561+
ArrayRef<Expr *> Args,
4562+
SourceLocation OpenParLoc) {
45614563
if (!CodeCompleter)
45624564
return;
45634565

@@ -4592,7 +4594,7 @@ void Sema::CodeCompleteConstructor(Scope *S, QualType Type, SourceLocation Loc,
45924594

45934595
SmallVector<ResultCandidate, 8> Results;
45944596
mergeCandidatesWithResults(*this, Results, CandidateSet, Loc);
4595-
CodeCompleteOverloadResults(*this, S, Results, Args.size());
4597+
CodeCompleteOverloadResults(*this, S, Results, Args.size(), OpenParLoc);
45964598
}
45974599

45984600
void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
void foo(int a, int b);
2+
void foo(int a, int b, int c);
3+
4+
void test() {
5+
foo(10, );
6+
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:5:10 %s -o - \
7+
// RUN: | FileCheck -check-prefix=CHECK-CC1 %s
8+
// CHECK-CC1: OPENING_PAREN_LOC: {{.*}}paren_locs.cpp:5:6
9+
10+
#define FOO foo(
11+
FOO 10, );
12+
#undef FOO
13+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:11:10 %s -o - \
14+
// RUN: | FileCheck -check-prefix=CHECK-CC2 %s
15+
// CHECK-CC2: OPENING_PAREN_LOC: {{.*}}paren_locs.cpp:11:3
16+
17+
struct Foo {
18+
Foo(int a, int b);
19+
Foo(int a, int b, int c);
20+
};
21+
Foo a(10, );
22+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:21:12 %s -o - \
23+
// RUN: | FileCheck -check-prefix=CHECK-CC3 %s
24+
// CHECK-CC3: OPENING_PAREN_LOC: {{.*}}paren_locs.cpp:21:8
25+
Foo(10, );
26+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:25:10 %s -o - \
27+
// RUN: | FileCheck -check-prefix=CHECK-CC4 %s
28+
// CHECK-CC4: OPENING_PAREN_LOC: {{.*}}paren_locs.cpp:25:6
29+
new Foo(10, );
30+
// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:29:15 %s -o - \
31+
// RUN: | FileCheck -check-prefix=CHECK-CC5 %s
32+
// CHECK-CC5: OPENING_PAREN_LOC: {{.*}}paren_locs.cpp:29:10
33+
}

clang/tools/libclang/CIndexCodeCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,8 @@ namespace {
653653

654654
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
655655
OverloadCandidate *Candidates,
656-
unsigned NumCandidates) override {
656+
unsigned NumCandidates,
657+
SourceLocation OpenParLoc) override {
657658
StoredResults.reserve(StoredResults.size() + NumCandidates);
658659
for (unsigned I = 0; I != NumCandidates; ++I) {
659660
CodeCompletionString *StoredCompletion

0 commit comments

Comments
 (0)