diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -160,9 +160,15 @@ } void addToken(SourceLocation Loc, HighlightingKind Kind) { - if (Loc.isMacroID()) - // FIXME: skip tokens inside macros for now. - return; + if(Loc.isMacroID()) { + // If the location is not an argument it might be from a macro of the form + // "#define VAR var". In that case this would highlight "var" in the macro + // definition and if VAR is used for functions and variables there would + // be conflicts and duplicates. So skip those for now. + if (!SM.isMacroArgExpansion(Loc)) + return; + Loc = SM.getSpellingLoc(Loc); + } auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc); if (!R) { diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -166,6 +166,42 @@ $Variable[[AA]].$Field[[E]].$Field[[C]]; $Class[[A]]::$Variable[[S]] = 90; } + )cpp", + R"cpp( + #define DEF_CLASS(T) class T {}; + DEF_CLASS($Class[[A]]) + + #define MACRO_CONCAT(X, V, T) T foo##X = V + #define DEF_VAR(X, V) int X = V + #define DEF_VAR_T(T, X, V) T X = V + #define DEF_VAR_REV(V, X) DEF_VAR(X, V) + #define CPY(X) X + #define DEF_VAR_TYPE(X, Y) X Y + #define SOME_NAME variable + #define SOME_NAME_SET variable2 = 123 + #define INC_VAR(X) X += 2 + void $Function[[foo]]() { + DEF_VAR($Variable[[X]], 123); + DEF_VAR_REV(908, $Variable[[XY]]); + int CPY( $Variable[[XX]] ); + DEF_VAR_TYPE($Class[[A]], $Variable[[AA]]); + double SOME_NAME; + int SOME_NAME_SET; + $Variable[[variable]] = 20.1; + MACRO_CONCAT(var, 2, float); + DEF_VAR_T($Class[[A]], CPY(CPY($Variable[[Nested]])), + CPY($Class[[A]]())); + INC_VAR($Variable[[variable]]); + } + void SOME_NAME(); + DEF_VAR($Variable[[XYZ]], 567); + DEF_VAR_REV(756, $Variable[[AB]]); + + #define CALL_FN(F) F(); + #define DEF_FN(F) void F () + DEF_FN($Function[[g]]) { + CALL_FN($Function[[foo]]); + } )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase);