[PATCH] D85253: [clangd] Show correct hover tooltip for non-preamble macro definition.

Ilya Golovenko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 4 15:35:30 PDT 2020


ilya-golovenko created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman, jkorous.
Herald added a project: clang.
ilya-golovenko requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

Incorrect definition is shown in tooltip when hovering over a non-preamble macro definition.
In the example below the definition will have 'namespace ns {}' instead of correct '#define FOO 1'.

namespace ns {
#define FOO 1
}


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85253

Files:
  clang-tools-extra/clangd/SourceCode.cpp
  clang-tools-extra/clangd/unittests/HoverTests.cpp


Index: clang-tools-extra/clangd/unittests/HoverTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ 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
Index: clang-tools-extra/clangd/SourceCode.cpp
===================================================================
--- clang-tools-extra/clangd/SourceCode.cpp
+++ 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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85253.283043.patch
Type: text/x-patch
Size: 1531 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200804/c96bd217/attachment.bin>


More information about the cfe-commits mailing list