Skip to content

Commit 43c292c

Browse files
committed
[clangd] Report position of opening paren in singature help
Summary: Only accessible via the C++ API at the moment. Reviewers: sammccall Reviewed By: sammccall Subscribers: ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D51437 llvm-svn: 341065
1 parent e11f221 commit 43c292c

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,17 @@ class SignatureHelpCollector final : public CodeCompleteConsumer {
794794

795795
void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
796796
OverloadCandidate *Candidates,
797-
unsigned NumCandidates) override {
797+
unsigned NumCandidates,
798+
SourceLocation OpenParLoc) override {
799+
assert(!OpenParLoc.isInvalid());
800+
SourceManager &SrcMgr = S.getSourceManager();
801+
OpenParLoc = SrcMgr.getFileLoc(OpenParLoc);
802+
if (SrcMgr.isInMainFile(OpenParLoc))
803+
SigHelp.argListStart = sourceLocToPosition(SrcMgr, OpenParLoc);
804+
else
805+
elog("Location oustide main file in signature help: {0}",
806+
OpenParLoc.printToString(SrcMgr));
807+
798808
std::vector<ScoredSignature> ScoredSignatures;
799809
SigHelp.signatures.reserve(NumCandidates);
800810
ScoredSignatures.reserve(NumCandidates);

clang-tools-extra/clangd/Protocol.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,13 @@ struct SignatureHelp {
828828

829829
/// The active parameter of the active signature.
830830
int activeParameter = 0;
831+
832+
/// Position of the start of the argument list, including opening paren. e.g.
833+
/// foo("first arg", "second arg",
834+
/// ^-argListStart ^-cursor
835+
/// This is a clangd-specific extension, it is only available via C++ API and
836+
/// not currently serialized for the LSP.
837+
Position argListStart;
831838
};
832839
llvm::json::Value toJSON(const SignatureHelp &);
833840

clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,7 @@ TEST(CompletionTest, IgnoreCompleteInExcludedPPBranchWithRecoveryContext) {
825825

826826
EXPECT_TRUE(Results.Completions.empty());
827827
}
828-
829-
SignatureHelp signatures(StringRef Text,
828+
SignatureHelp signatures(StringRef Text, Position Point,
830829
std::vector<Symbol> IndexSymbols = {}) {
831830
std::unique_ptr<SymbolIndex> Index;
832831
if (!IndexSymbols.empty())
@@ -840,9 +839,14 @@ SignatureHelp signatures(StringRef Text,
840839

841840
ClangdServer Server(CDB, FS, DiagConsumer, Opts);
842841
auto File = testPath("foo.cpp");
842+
runAddDocument(Server, File, Text);
843+
return cantFail(runSignatureHelp(Server, File, Point));
844+
}
845+
846+
SignatureHelp signatures(StringRef Text,
847+
std::vector<Symbol> IndexSymbols = {}) {
843848
Annotations Test(Text);
844-
runAddDocument(Server, File, Test.code());
845-
return cantFail(runSignatureHelp(Server, File, Test.point()));
849+
return signatures(Test.code(), Test.point(), std::move(IndexSymbols));
846850
}
847851

848852
MATCHER_P(ParamsAre, P, "") {
@@ -907,6 +911,54 @@ TEST(SignatureHelpTest, ActiveArg) {
907911
EXPECT_EQ(1, Results.activeParameter);
908912
}
909913

914+
TEST(SignatureHelpTest, OpeningParen) {
915+
llvm::StringLiteral Tests[] = {// Recursive function call.
916+
R"cpp(
917+
int foo(int a, int b, int c);
918+
int main() {
919+
foo(foo $p^( foo(10, 10, 10), ^ )));
920+
})cpp",
921+
// Functional type cast.
922+
R"cpp(
923+
struct Foo {
924+
Foo(int a, int b, int c);
925+
};
926+
int main() {
927+
Foo $p^( 10, ^ );
928+
})cpp",
929+
// New expression.
930+
R"cpp(
931+
struct Foo {
932+
Foo(int a, int b, int c);
933+
};
934+
int main() {
935+
new Foo $p^( 10, ^ );
936+
})cpp",
937+
// Macro expansion.
938+
R"cpp(
939+
int foo(int a, int b, int c);
940+
#define FOO foo(
941+
942+
int main() {
943+
// Macro expansions.
944+
$p^FOO 10, ^ );
945+
})cpp",
946+
// Macro arguments.
947+
R"cpp(
948+
int foo(int a, int b, int c);
949+
int main() {
950+
#define ID(X) X
951+
ID(foo $p^( foo(10), ^ ))
952+
})cpp"};
953+
954+
for (auto Test : Tests) {
955+
Annotations Code(Test);
956+
EXPECT_EQ(signatures(Code.code(), Code.point()).argListStart,
957+
Code.point("p"))
958+
<< "Test source:" << Test;
959+
}
960+
}
961+
910962
class IndexRequestCollector : public SymbolIndex {
911963
public:
912964
bool

0 commit comments

Comments
 (0)