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,
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,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits