Index: clangd/XRefs.cpp =================================================================== --- clangd/XRefs.cpp +++ clangd/XRefs.cpp @@ -565,12 +565,28 @@ } /// Generate a \p Hover object given the macro \p MacroInf. -static Hover getHoverContents(StringRef MacroName) { - Hover H; - - H.contents.value = "#define "; - H.contents.value += MacroName; +static Hover getHoverContents(MacroDecl Decl, ASTContext &ASTCtx) { + SourceManager &SM = ASTCtx.getSourceManager(); + StringRef Definition = Decl.Name; + + // Try to get the full definition, not just the name + SourceLocation StartLoc = Decl.Info->getDefinitionLoc(); + SourceLocation EndLoc = Decl.Info->getDefinitionEndLoc(); + if (EndLoc.isValid()) { + EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM, ASTCtx.getLangOpts()); + bool Invalid; + StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid); + if (!Invalid) { + unsigned StartOffset = SM.getFileOffset(StartLoc); + unsigned EndOffset = SM.getFileOffset(EndLoc); + if (EndOffset <= Buffer.size() && StartOffset < EndOffset) + Definition = Buffer.substr(StartOffset, EndOffset - StartOffset); + } + } + Hover H; + H.contents.kind = MarkupKind::Markdown; + H.contents.value = formatv("```C++\n#define {0}\n```", Definition); return H; } @@ -694,7 +710,7 @@ auto Symbols = getSymbolAtPosition(AST, SourceLocationBeg); if (!Symbols.Macros.empty()) - return getHoverContents(Symbols.Macros[0].Name); + return getHoverContents(Symbols.Macros[0], AST.getASTContext()); if (!Symbols.Decls.empty()) return getHoverContents(Symbols.Decls[0].D); Index: unittests/clangd/XRefsTests.cpp =================================================================== --- unittests/clangd/XRefsTests.cpp +++ unittests/clangd/XRefsTests.cpp @@ -616,7 +616,27 @@ #define MACRO 2 #undef macro )cpp", - "#define MACRO", + "```C++\n#define MACRO 1\n```", + }, + { + R"cpp(// Macro + #define MACRO 0 + #define MACRO2 ^MACRO + )cpp", + "```C++\n#define MACRO 0\n```", + }, + { + R"cpp(// Macro + #define MACRO {\ + return 0;\ + } + int main() ^MACRO + )cpp", + R"cpp(```C++ +#define MACRO {\ + return 0;\ + } +```)cpp", }, { R"cpp(// Forward class declaration