[PATCH] D55250: [clangd] Enhance macro hover to see full definition

Marc-Andre Laperle via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 4 18:45:19 PST 2018


malaperle updated this revision to Diff 176754.
malaperle added a comment.

Clang-format


Repository:
  rCTE Clang Tools Extra

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55250/new/

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.176754.patch
Type: text/x-patch
Size: 2488 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181205/05c30684/attachment-0001.bin>


More information about the cfe-commits mailing list