nridge created this revision. nridge added a reviewer: kadircet. Herald added subscribers: usaxena95, arphaman. nridge requested review of this revision. Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov. Herald added a project: clang-tools-extra.
Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D110130 Files: clang-tools-extra/clangd/SemanticHighlighting.cpp clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -729,6 +729,14 @@ } }; )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. @@ -797,7 +805,7 @@ )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", Index: clang-tools-extra/clangd/SemanticHighlighting.cpp =================================================================== --- clang-tools-extra/clangd/SemanticHighlighting.cpp +++ clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -514,13 +514,20 @@ 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); + SourceLocation StartLoc = D->getTypeSpecStartLoc(); + // The AutoType may not have a corresponding token, e.g. in the case of + // init-captures, so there's nothing to color. + // Detect this case by checking if the type specifier's location + // is the same as the location of the declared name itself. + if (StartLoc != D->getLocation()) { + 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; }
Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -729,6 +729,14 @@ } }; )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. @@ -797,7 +805,7 @@ )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", Index: clang-tools-extra/clangd/SemanticHighlighting.cpp =================================================================== --- clang-tools-extra/clangd/SemanticHighlighting.cpp +++ clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -514,13 +514,20 @@ 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); + SourceLocation StartLoc = D->getTypeSpecStartLoc(); + // The AutoType may not have a corresponding token, e.g. in the case of + // init-captures, so there's nothing to color. + // Detect this case by checking if the type specifier's location + // is the same as the location of the declared name itself. + if (StartLoc != D->getLocation()) { + 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; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits