diff --git a/clang-tools-extra/clangd/AST.h b/clang-tools-extra/clangd/AST.h --- a/clang-tools-extra/clangd/AST.h +++ b/clang-tools-extra/clangd/AST.h @@ -75,7 +75,7 @@ /// could change these semantics in the future by reimplementing this funcure /// (e.g. avoid USR for macros). llvm::Optional getSymbolID(const llvm::StringRef MacroName, - const MacroInfo *MI, + SourceLocation DefLoc, const SourceManager &SM); /// Returns a QualType as string. The result doesn't contain unwritten scopes diff --git a/clang-tools-extra/clangd/AST.cpp b/clang-tools-extra/clangd/AST.cpp --- a/clang-tools-extra/clangd/AST.cpp +++ b/clang-tools-extra/clangd/AST.cpp @@ -290,12 +290,10 @@ } llvm::Optional getSymbolID(const llvm::StringRef MacroName, - const MacroInfo *MI, + SourceLocation DefLoc, const SourceManager &SM) { - if (MI == nullptr) - return None; llvm::SmallString<128> USR; - if (index::generateUSRForMacro(MacroName, MI->getDefinitionLoc(), SM, USR)) + if (index::generateUSRForMacro(MacroName, DefLoc, SM, USR)) return None; return SymbolID(USR); } diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -503,9 +503,12 @@ return clang::clangd::getSymbolID(R.Declaration); } case CodeCompletionResult::RK_Macro: - return clang::clangd::getSymbolID(R.Macro->getName(), R.MacroDefInfo, SM); + if (!R.MacroDefInfo) + return llvm::None; + return clang::clangd::getSymbolID(R.Macro->getName(), + R.MacroDefInfo->getDefinitionLoc(), SM); case CodeCompletionResult::RK_Keyword: - return None; + return llvm::None; } llvm_unreachable("unknown CodeCompletionResult kind"); } diff --git a/clang-tools-extra/clangd/CollectMacros.cpp b/clang-tools-extra/clangd/CollectMacros.cpp --- a/clang-tools-extra/clangd/CollectMacros.cpp +++ b/clang-tools-extra/clangd/CollectMacros.cpp @@ -7,8 +7,10 @@ //===----------------------------------------------------------------------===// #include "CollectMacros.h" +#include "index/SymbolID.h" #include "clang/Basic/SourceLocation.h" #include "clang/Lex/Lexer.h" +#include "llvm/ADT/Optional.h" namespace clang { namespace clangd { @@ -25,7 +27,10 @@ Out.Names.insert(Name); auto Range = halfOpenToRange( SM, CharSourceRange::getCharRange(Loc, MacroNameTok.getEndLoc())); - if (auto SID = getSymbolID(Name, MI, SM)) + llvm::Optional SID; + if (MI) + SID = getSymbolID(Name, MI->getDefinitionLoc(), SM); + if (SID) Out.MacroRefs[*SID].push_back(Range); else Out.UnknownMacros.push_back(Range); diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -753,9 +753,10 @@ } RefsRequest Req; - if (Macro) { + if (Macro && Macro->Info) { // Handle references to macro. - if (auto MacroSID = getSymbolID(Macro->Name, Macro->Info, SM)) { + if (auto MacroSID = + getSymbolID(Macro->Name, Macro->Info->getDefinitionLoc(), SM)) { // Collect macro references from main file. const auto &IDToRefs = AST.getMacros().MacroRefs; auto Refs = IDToRefs.find(*MacroSID); diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -387,7 +387,7 @@ if (SM.isWrittenInBuiltinFile(DefLoc)) return true; - auto ID = getSymbolID(Name->getName(), MI, SM); + auto ID = getSymbolID(Name->getName(), DefLoc, SM); if (!ID) return true; @@ -510,14 +510,16 @@ // First, drop header guards. We can't identify these until EOF. for (const IdentifierInfo *II : IndexedMacros) { if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo()) - if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager())) + if (auto ID = getSymbolID(II->getName(), MI->getDefinitionLoc(), + PP->getSourceManager())) if (MI->isUsedForHeaderGuard()) Symbols.erase(*ID); } // Now increment refcounts. for (const IdentifierInfo *II : ReferencedMacros) { if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo()) - if (auto ID = getSymbolID(II->getName(), MI, PP->getSourceManager())) + if (auto ID = getSymbolID(II->getName(), MI->getDefinitionLoc(), + PP->getSourceManager())) IncRef(*ID); } } diff --git a/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp b/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp --- a/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp +++ b/clang-tools-extra/clangd/unittests/CollectMacrosTests.cpp @@ -92,8 +92,9 @@ const auto *Id = syntax::spelledIdentifierTouching(*Loc, AST.getTokens()); ASSERT_TRUE(Id); auto Macro = locateMacroAt(*Id, PP); - assert(Macro); - auto SID = getSymbolID(Macro->Name, Macro->Info, SM); + assert(Macro && Macro->Info); + auto SID = getSymbolID(Macro->Name, Macro->Info->getDefinitionLoc(), SM); + assert(SID); EXPECT_THAT(ExpectedRefs, UnorderedElementsAreArray(ActualMacroRefs.MacroRefs[*SID]))