Author: Nathan Ridge Date: 2021-09-23T03:52:57-04:00 New Revision: 4223195de3eaaa9c221d1f178899f221be6264c5
URL: https://github.com/llvm/llvm-project/commit/4223195de3eaaa9c221d1f178899f221be6264c5 DIFF: https://github.com/llvm/llvm-project/commit/4223195de3eaaa9c221d1f178899f221be6264c5.diff LOG: [clangd] Ensure lambda init-capture gets semantic token Prior to this patch, CollectExtraHighlightings would incorrectly produce a token for the init-capture's type which overlapped the name and resulted in both being dropped. Fixes https://github.com/clangd/clangd/issues/868 Differential Revision: https://reviews.llvm.org/D110130 Added: Modified: clang-tools-extra/clangd/SemanticHighlighting.cpp clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index d55bd9e459d53..76e810f5a3b83 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -601,16 +601,24 @@ class CollectExtraHighlightings auto *AT = D->getType()->getContainedAutoType(); if (!AT) return true; - if (auto K = kindForType(AT->getDeducedType().getTypePtrOrNull(), - H.getResolver())) { - auto &Tok = H.addToken(D->getTypeSpecStartLoc(), *K) - .addModifier(HighlightingModifier::Deduced); - const Type *Deduced = AT->getDeducedType().getTypePtrOrNull(); - if (auto Mod = scopeModifier(Deduced)) - Tok.addModifier(*Mod); - if (isDefaultLibrary(Deduced)) - Tok.addModifier(HighlightingModifier::DefaultLibrary); - } + auto K = + kindForType(AT->getDeducedType().getTypePtrOrNull(), H.getResolver()); + if (!K) + return true; + SourceLocation StartLoc = D->getTypeSpecStartLoc(); + // The AutoType may not have a corresponding token, e.g. in the case of + // init-captures. In this case, StartLoc overlaps with the location + // of the decl itself, and producing a token for the type here would result + // in both it and the token for the decl being dropped due to conflict. + if (StartLoc == D->getLocation()) + return true; + auto &Tok = + H.addToken(StartLoc, *K).addModifier(HighlightingModifier::Deduced); + const Type *Deduced = AT->getDeducedType().getTypePtrOrNull(); + if (auto Mod = scopeModifier(Deduced)) + Tok.addModifier(*Mod); + if (isDefaultLibrary(Deduced)) + Tok.addModifier(HighlightingModifier::DefaultLibrary); return true; } diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 1a9220e911e32..4653490f5b036 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -772,6 +772,14 @@ sizeof...($TemplateParameter[[Elements]]); $Function[[foo]]($Parameter[[x]]); } )cpp", + // init-captures + R"cpp( + void $Function_decl[[foo]]() { + int $LocalVariable_decl[[a]], $LocalVariable_decl[[b]]; + [ $LocalVariable_decl[[c]] = $LocalVariable[[a]], + $LocalVariable_decl[[d]]($LocalVariable[[b]]) ]() {}(); + } + )cpp", }; for (const auto &TestCase : TestCases) // Mask off scope modifiers to keep the tests manageable. @@ -840,7 +848,7 @@ TEST(SemanticHighlighting, ScopeModifiers) { )cpp", R"cpp( // Lambdas are considered functions, not classes. - auto $Variable_fileScope[[x]] = [m(42)] { // FIXME: annotate capture + auto $Variable_fileScope[[x]] = [$LocalVariable_functionScope[[m]](42)] { return $LocalVariable_functionScope[[m]]; }; )cpp", _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits