From 2658cb65bdc51f3569b9494c13024a9504ef7fb0 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Tue, 13 Mar 2018 12:30:59 +0000 Subject: [PATCH] [clangd] Fix irrelevant declaratations in goto definition (on macros). Summary: DeclrationAndMacrosFinder will find some declarations (not macro!) that are referened inside the macro somehow, isSearchedLocation() is not sufficient, we don't know whether the searched source location is macro or not. Reviewers: ilya-biryukov Subscribers: klimek, jkorous-apple, ioeric, cfe-commits Differential Revision: https://reviews.llvm.org/D44293 llvm-svn: 327387 --- clang-tools-extra/clangd/XRefs.cpp | 10 ++++++++++ clang-tools-extra/unittests/clangd/XRefsTests.cpp | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp index 0f9b8a5..df3bf6f 100644 --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -127,6 +127,16 @@ private: MacroInfo *MacroInf = MacroDef.getMacroInfo(); if (MacroInf) { MacroInfos.push_back(MacroDecl{IdentifierInfo->getName(), MacroInf}); + // Clear all collected delcarations if this is a macro search. + // + // In theory, there should be no declarataions being collected when we + // search a source location that refers to a macro. + // The occurrence location returned by `handleDeclOccurence` is + // limited (FID, Offset are from expansion location), we will collect + // all declarations inside the macro. + // + // FIXME: Avoid adding decls from inside macros in handlDeclOccurence. + Decls.clear(); } } } diff --git a/clang-tools-extra/unittests/clangd/XRefsTests.cpp b/clang-tools-extra/unittests/clangd/XRefsTests.cpp index a7fdc84..0052d42 100644 --- a/clang-tools-extra/unittests/clangd/XRefsTests.cpp +++ b/clang-tools-extra/unittests/clangd/XRefsTests.cpp @@ -213,6 +213,15 @@ TEST(GoToDefinition, All) { #undef macro )cpp", + R"cpp(// Macro + class TTT { public: int a; }; + #define [[FF(S) if (int b = S.a) {}]] + void f() { + TTT t; + F^F(t); + } + )cpp", + R"cpp(// Forward class declaration class Foo; class [[Foo]] {}; -- 2.7.4