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 @@ -31,6 +31,14 @@ std::vector collectTokens() { Tokens.clear(); TraverseAST(Ctx); + // Initializer lists can give duplicates of tokens, therefore all tokens + // must be deduplicated. + llvm::sort(Tokens, + [](const HighlightingToken &Lhs, const HighlightingToken &Rhs) { + return std::tie(Lhs.R, Lhs.Kind) < std::tie(Rhs.R, Rhs.Kind); + }); + auto Last = std::unique(Tokens.begin(), Tokens.end()); + Tokens.erase(Last, Tokens.end()); return Tokens; } 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,13 @@ $Variable[[AA]].$Field[[E]].$Field[[C]]; $Class[[A]]::$Variable[[S]] = 90; } + )cpp", + R"cpp( + struct $Class[[AA]] { + int $Field[[A]]; + } + int $Variable[[B]]; + $Class[[AA]] $Variable[[A]]{$Variable[[B]]}; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase);