nridge created this revision. nridge added reviewers: kadircet, hokein. Herald added a subscriber: arphaman. Herald added a project: All. nridge requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang-tools-extra.
Fixes https://github.com/clangd/clangd/issues/1484 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D144703 Files: clang-tools-extra/clangd/CodeComplete.cpp clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -627,7 +627,7 @@ has("variable", CompletionItemKind::Variable), has("int", CompletionItemKind::Keyword), has("Struct", CompletionItemKind::Struct), - has("MACRO", CompletionItemKind::Text), + has("MACRO", CompletionItemKind::Variable), has("indexFunction", CompletionItemKind::Function), has("indexVariable", CompletionItemKind::Variable), has("indexClass", CompletionItemKind::Class))); @@ -3789,7 +3789,7 @@ kind(CompletionItemKind::Constructor)))); EXPECT_THAT(completions(Context + "MAC^(2)", {}, Opts).Completions, Contains(AllOf(labeled("MACRO(x)"), snippetSuffix(""), - kind(CompletionItemKind::Text)))); + kind(CompletionItemKind::Function)))); } TEST(CompletionTest, NoCrashDueToMacroOrdering) { Index: clang-tools-extra/clangd/CodeComplete.cpp =================================================================== --- clang-tools-extra/clangd/CodeComplete.cpp +++ clang-tools-extra/clangd/CodeComplete.cpp @@ -150,22 +150,24 @@ llvm_unreachable("Unhandled clang::index::SymbolKind."); } -CompletionItemKind -toCompletionItemKind(CodeCompletionResult::ResultKind ResKind, - const NamedDecl *Decl, - CodeCompletionContext::Kind CtxKind) { - if (Decl) - return toCompletionItemKind(index::getSymbolInfo(Decl).Kind); +CompletionItemKind toCompletionItemKind(const CodeCompletionResult &Res, + CodeCompletionContext::Kind CtxKind) { + if (Res.Declaration) + return toCompletionItemKind(index::getSymbolInfo(Res.Declaration).Kind); if (CtxKind == CodeCompletionContext::CCC_IncludedFile) return CompletionItemKind::File; - switch (ResKind) { + switch (Res.Kind) { case CodeCompletionResult::RK_Declaration: llvm_unreachable("RK_Declaration without Decl"); case CodeCompletionResult::RK_Keyword: return CompletionItemKind::Keyword; case CodeCompletionResult::RK_Macro: - return CompletionItemKind::Text; // unfortunately, there's no 'Macro' - // completion items in LSP. + // There is no 'Macro' kind in LSP. + // Avoid using 'Text' to avoid confusion with client-side word-based + // completion proposals. + return Res.MacroDefInfo && Res.MacroDefInfo->isFunctionLike() + ? CompletionItemKind::Function + : CompletionItemKind::Variable; case CodeCompletionResult::RK_Pattern: return CompletionItemKind::Snippet; } @@ -337,8 +339,7 @@ Completion.Scope = std::string( splitQualifiedName(printQualifiedName(*ND)).first); } - Completion.Kind = toCompletionItemKind( - C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind); + Completion.Kind = toCompletionItemKind(*C.SemaResult, ContextKind); // Sema could provide more info on whether the completion was a file or // folder. if (Completion.Kind == CompletionItemKind::File &&
Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -627,7 +627,7 @@ has("variable", CompletionItemKind::Variable), has("int", CompletionItemKind::Keyword), has("Struct", CompletionItemKind::Struct), - has("MACRO", CompletionItemKind::Text), + has("MACRO", CompletionItemKind::Variable), has("indexFunction", CompletionItemKind::Function), has("indexVariable", CompletionItemKind::Variable), has("indexClass", CompletionItemKind::Class))); @@ -3789,7 +3789,7 @@ kind(CompletionItemKind::Constructor)))); EXPECT_THAT(completions(Context + "MAC^(2)", {}, Opts).Completions, Contains(AllOf(labeled("MACRO(x)"), snippetSuffix(""), - kind(CompletionItemKind::Text)))); + kind(CompletionItemKind::Function)))); } TEST(CompletionTest, NoCrashDueToMacroOrdering) { Index: clang-tools-extra/clangd/CodeComplete.cpp =================================================================== --- clang-tools-extra/clangd/CodeComplete.cpp +++ clang-tools-extra/clangd/CodeComplete.cpp @@ -150,22 +150,24 @@ llvm_unreachable("Unhandled clang::index::SymbolKind."); } -CompletionItemKind -toCompletionItemKind(CodeCompletionResult::ResultKind ResKind, - const NamedDecl *Decl, - CodeCompletionContext::Kind CtxKind) { - if (Decl) - return toCompletionItemKind(index::getSymbolInfo(Decl).Kind); +CompletionItemKind toCompletionItemKind(const CodeCompletionResult &Res, + CodeCompletionContext::Kind CtxKind) { + if (Res.Declaration) + return toCompletionItemKind(index::getSymbolInfo(Res.Declaration).Kind); if (CtxKind == CodeCompletionContext::CCC_IncludedFile) return CompletionItemKind::File; - switch (ResKind) { + switch (Res.Kind) { case CodeCompletionResult::RK_Declaration: llvm_unreachable("RK_Declaration without Decl"); case CodeCompletionResult::RK_Keyword: return CompletionItemKind::Keyword; case CodeCompletionResult::RK_Macro: - return CompletionItemKind::Text; // unfortunately, there's no 'Macro' - // completion items in LSP. + // There is no 'Macro' kind in LSP. + // Avoid using 'Text' to avoid confusion with client-side word-based + // completion proposals. + return Res.MacroDefInfo && Res.MacroDefInfo->isFunctionLike() + ? CompletionItemKind::Function + : CompletionItemKind::Variable; case CodeCompletionResult::RK_Pattern: return CompletionItemKind::Snippet; } @@ -337,8 +339,7 @@ Completion.Scope = std::string( splitQualifiedName(printQualifiedName(*ND)).first); } - Completion.Kind = toCompletionItemKind( - C.SemaResult->Kind, C.SemaResult->Declaration, ContextKind); + Completion.Kind = toCompletionItemKind(*C.SemaResult, ContextKind); // Sema could provide more info on whether the completion was a file or // folder. if (Completion.Kind == CompletionItemKind::File &&
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits