[PATCH] D55250: [clangd] Enhance macro hover to see full definition
Marc-Andre Laperle via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 3 21:51:12 PST 2018
malaperle created this revision.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, ioeric, ilya-biryukov.
Signed-off-by: Marc-Andre Laperle <malaperle at gmail.com>
Repository:
rCTE Clang Tools Extra
https://reviews.llvm.org/D55250
Files:
clangd/XRefs.cpp
unittests/clangd/XRefsTests.cpp
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
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);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55250.176548.patch
Type: text/x-patch
Size: 2488 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181204/28996f2b/attachment-0001.bin>
More information about the cfe-commits
mailing list