Skip to content

Commit 7aa3716

Browse files
[clangd] Avoid using CompletionItemKind.Text for macro completions from the index (#88236)
This was fixed in clangd/clangd#1484 for Sema completions but the fix did not apply to index completions. Fixes clangd/clangd#2002
1 parent 5fb8215 commit 7aa3716

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ const CodeCompleteOptions::CodeCompletionRankingModel
8989

9090
namespace {
9191

92-
CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
92+
// Note: changes to this function should also be reflected in the
93+
// CodeCompletionResult overload where appropriate.
94+
CompletionItemKind
95+
toCompletionItemKind(index::SymbolKind Kind,
96+
const llvm::StringRef *Signature = nullptr) {
9397
using SK = index::SymbolKind;
9498
switch (Kind) {
9599
case SK::Unknown:
@@ -99,7 +103,10 @@ CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
99103
case SK::NamespaceAlias:
100104
return CompletionItemKind::Module;
101105
case SK::Macro:
102-
return CompletionItemKind::Text;
106+
// Use macro signature (if provided) to tell apart function-like and
107+
// object-like macros.
108+
return Signature && Signature->contains('(') ? CompletionItemKind::Function
109+
: CompletionItemKind::Constant;
103110
case SK::Enum:
104111
return CompletionItemKind::Enum;
105112
case SK::Struct:
@@ -150,6 +157,8 @@ CompletionItemKind toCompletionItemKind(index::SymbolKind Kind) {
150157
llvm_unreachable("Unhandled clang::index::SymbolKind.");
151158
}
152159

160+
// Note: changes to this function should also be reflected in the
161+
// index::SymbolKind overload where appropriate.
153162
CompletionItemKind toCompletionItemKind(const CodeCompletionResult &Res,
154163
CodeCompletionContext::Kind CtxKind) {
155164
if (Res.Declaration)
@@ -379,7 +388,8 @@ struct CodeCompletionBuilder {
379388
if (Completion.Scope.empty())
380389
Completion.Scope = std::string(C.IndexResult->Scope);
381390
if (Completion.Kind == CompletionItemKind::Missing)
382-
Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind);
391+
Completion.Kind = toCompletionItemKind(C.IndexResult->SymInfo.Kind,
392+
&C.IndexResult->Signature);
383393
if (Completion.Name.empty())
384394
Completion.Name = std::string(C.IndexResult->Name);
385395
if (Completion.FilterText.empty())

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ TEST(CompletionTest, Kinds) {
671671
#define MACRO 10
672672
int X = ^
673673
)cpp",
674-
{func("indexFunction"), var("indexVariable"), cls("indexClass")});
674+
{func("indexFunction"), var("indexVariable"), cls("indexClass"),
675+
macro("indexObjMacro"), macro("indexFuncMacro", "(x, y)")});
675676
EXPECT_THAT(Results.Completions,
676677
AllOf(has("function", CompletionItemKind::Function),
677678
has("variable", CompletionItemKind::Variable),
@@ -680,7 +681,9 @@ TEST(CompletionTest, Kinds) {
680681
has("MACRO", CompletionItemKind::Constant),
681682
has("indexFunction", CompletionItemKind::Function),
682683
has("indexVariable", CompletionItemKind::Variable),
683-
has("indexClass", CompletionItemKind::Class)));
684+
has("indexClass", CompletionItemKind::Class),
685+
has("indexObjMacro", CompletionItemKind::Constant),
686+
has("indexFuncMacro", CompletionItemKind::Function)));
684687

685688
Results = completions("nam^");
686689
EXPECT_THAT(Results.Completions,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle,
3838
// Helpers to produce fake index symbols for memIndex() or completions().
3939
// USRFormat is a regex replacement string for the unqualified part of the USR.
4040
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind,
41-
llvm::StringRef USRFormat) {
41+
llvm::StringRef USRFormat, llvm::StringRef Signature) {
4242
Symbol Sym;
4343
std::string USR = "c:"; // We synthesize a few simple cases of USRs by hand!
4444
size_t Pos = QName.rfind("::");
@@ -55,6 +55,7 @@ Symbol sym(llvm::StringRef QName, index::SymbolKind Kind,
5555
Sym.SymInfo.Kind = Kind;
5656
Sym.Flags |= Symbol::IndexedForCodeCompletion;
5757
Sym.Origin = SymbolOrigin::Static;
58+
Sym.Signature = Signature;
5859
return Sym;
5960
}
6061

@@ -86,6 +87,10 @@ Symbol conceptSym(llvm::StringRef Name) {
8687
return sym(Name, index::SymbolKind::Concept, "@CT@\\0");
8788
}
8889

90+
Symbol macro(llvm::StringRef Name, llvm::StringRef ArgList) {
91+
return sym(Name, index::SymbolKind::Macro, "@macro@\\0", ArgList);
92+
}
93+
8994
Symbol objcSym(llvm::StringRef Name, index::SymbolKind Kind,
9095
llvm::StringRef USRPrefix) {
9196
Symbol Sym;

clang-tools-extra/clangd/unittests/TestIndex.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Symbol symbol(llvm::StringRef QName);
2020
// Helpers to produce fake index symbols with proper SymbolID.
2121
// USRFormat is a regex replacement string for the unqualified part of the USR.
2222
Symbol sym(llvm::StringRef QName, index::SymbolKind Kind,
23-
llvm::StringRef USRFormat);
23+
llvm::StringRef USRFormat, llvm::StringRef Signature = {});
2424
// Creats a function symbol assuming no function arg.
2525
Symbol func(llvm::StringRef Name);
2626
// Creates a class symbol.
@@ -35,6 +35,8 @@ Symbol var(llvm::StringRef Name);
3535
Symbol ns(llvm::StringRef Name);
3636
// Create a C++20 concept symbol.
3737
Symbol conceptSym(llvm::StringRef Name);
38+
// Create a macro symbol.
39+
Symbol macro(llvm::StringRef Name, llvm::StringRef ArgList = {});
3840

3941
// Create an Objective-C symbol.
4042
Symbol objcSym(llvm::StringRef Name, index::SymbolKind Kind,

0 commit comments

Comments
 (0)