Index: clangd/AST.h =================================================================== --- clangd/AST.h +++ clangd/AST.h @@ -37,6 +37,11 @@ /// Gets the symbol ID for a declaration, if possible. llvm::Optional getSymbolID(const Decl *D); +/// Gets the symbol ID for a macro, if possible. +llvm::Optional getSymbolID(const IdentifierInfo &II, + const MacroInfo *MI, + const SourceManager &SM); + } // namespace clangd } // namespace clang Index: clangd/AST.cpp =================================================================== --- clangd/AST.cpp +++ clangd/AST.cpp @@ -61,5 +61,16 @@ return SymbolID(USR); } +llvm::Optional getSymbolID(const IdentifierInfo &II, + const MacroInfo *MI, + const SourceManager &SM) { + if (MI == nullptr) + return None; + llvm::SmallString<128> USR; + if (index::generateUSRForMacro(II.getName(), MI->getDefinitionLoc(), SM, USR)) + return None; + return SymbolID(USR); +} + } // namespace clangd } // namespace clang Index: clangd/CodeComplete.cpp =================================================================== --- clangd/CodeComplete.cpp +++ clangd/CodeComplete.cpp @@ -505,14 +505,15 @@ }; // Determine the symbol ID for a Sema code completion result, if possible. -llvm::Optional getSymbolID(const CodeCompletionResult &R) { +llvm::Optional getSymbolID(const CodeCompletionResult &R, + const SourceManager &SM) { switch (R.Kind) { case CodeCompletionResult::RK_Declaration: case CodeCompletionResult::RK_Pattern: { return clang::clangd::getSymbolID(R.Declaration); } case CodeCompletionResult::RK_Macro: - // FIXME: Macros do have USRs, but the CCR doesn't contain enough info. + return clang::clangd::getSymbolID(*R.Macro, R.MacroDefInfo, SM); case CodeCompletionResult::RK_Keyword: return None; } @@ -1435,7 +1436,8 @@ llvm::DenseSet UsedIndexResults; auto CorrespondingIndexResult = [&](const CodeCompletionResult &SemaResult) -> const Symbol * { - if (auto SymID = getSymbolID(SemaResult)) { + if (auto SymID = + getSymbolID(SemaResult, Recorder->CCSema->getSourceManager())) { auto I = IndexResults.find(*SymID); if (I != IndexResults.end()) { UsedIndexResults.insert(&*I); Index: clangd/index/SymbolCollector.cpp =================================================================== --- clangd/index/SymbolCollector.cpp +++ clangd/index/SymbolCollector.cpp @@ -389,14 +389,16 @@ if (index::generateUSRForMacro(Name->getName(), MI->getDefinitionLoc(), SM, USR)) return true; - SymbolID ID(USR); + auto ID = getSymbolID(*Name, MI, SM); + if (!ID) + return true; // Only collect one instance in case there are multiple. - if (Symbols.find(ID) != nullptr) + if (Symbols.find(*ID) != nullptr) return true; Symbol S; - S.ID = std::move(ID); + S.ID = std::move(*ID); S.Name = Name->getName(); S.IsIndexedForCodeCompletion = true; S.SymInfo = index::getSymbolInfoForMacro(*MI); Index: unittests/clangd/CodeCompleteTests.cpp =================================================================== --- unittests/clangd/CodeCompleteTests.cpp +++ unittests/clangd/CodeCompleteTests.cpp @@ -1882,6 +1882,16 @@ AllOf(Named("Func"), HasInclude("\"foo.h\""), Not(InsertInclude())))); } +TEST(CompletionTest, MergeMacrosFromIndexAndSema) { + Symbol Sym; + Sym.ID = SymbolID("c:foo.cpp@8@macro@Clangd_Macro_Test"); + Sym.SymInfo.Kind = index::SymbolKind::Macro; + Sym.IsIndexedForCodeCompletion = true; + EXPECT_THAT(completions("#define Clangd_Macro_Test\nClangd_Macro_T^", {Sym}) + .Completions, + UnorderedElementsAre(Named("Clangd_Macro_Test"))); +} + } // namespace } // namespace clangd } // namespace clang