diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -980,6 +980,12 @@ if (SM.getLocForStartOfFile(SM.getFileID(Loc)) != Loc) Loc = Loc.getLocWithOffset(-1); MacroDefinition MacroDef = PP.getMacroDefinitionAtLoc(IdentifierInfo, Loc); + // If the definition was not found before the current token location then + // it is possible the current token is a macro definition itself. + if (!MacroDef) { + Loc = SpelledTok.location().getLocWithOffset(1); + MacroDef = PP.getMacroDefinitionAtLoc(IdentifierInfo, Loc); + } if (auto *MI = MacroDef.getMacroInfo()) return DefinedMacro{ IdentifierInfo->getName(), MI, diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -1308,6 +1308,17 @@ HI.Kind = index::SymbolKind::Macro; HI.Definition = "#define MACRO 0"; }}, + { + R"cpp(// Macro + namespace { + #define [[MA^CRO]] 0 + } + )cpp", + [](HoverInfo &HI) { + HI.Name = "MACRO"; + HI.Kind = index::SymbolKind::Macro; + HI.Definition = "#define MACRO 0"; + }}, { R"cpp(// Macro #define MACRO 0