diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h b/clang-tools-extra/clangd/SemanticHighlighting.h --- a/clang-tools-extra/clangd/SemanticHighlighting.h +++ b/clang-tools-extra/clangd/SemanticHighlighting.h @@ -51,6 +51,7 @@ Macro, Modifier, Operator, + Bracket, // This one is different from the other kinds as it's a line style // rather than a token style. 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 @@ -369,6 +369,46 @@ return addToken(*Range, Kind); } + // Most of this function works around + // https://github.com/clangd/clangd/issues/871. + void addAngleBracketTokens(SourceLocation LLoc, SourceLocation RLoc) { + if (!LLoc.isValid() || !RLoc.isValid()) + return; + + auto LRange = getRangeForSourceLocation(LLoc); + if (!LRange) + return; + + // RLoc might be pointing at a virtual buffer when it's part of a `>>` + // token. + RLoc = SourceMgr.getFileLoc(RLoc); + // Make sure token is part of the main file. + RLoc = getHighlightableSpellingToken(RLoc, SourceMgr); + if (!RLoc.isValid()) + return; + + const auto *RTok = TB.spelledTokenAt(RLoc); + // Handle `>>`. RLoc is always pointing at the right location, just change + // the end to be offset by 1. + // We'll either point at the beginning of `>>`, hence get a proper spelled + // or point in the middle of `>>` hence get no spelled tok. + if (!RTok || RTok->kind() == tok::greatergreater) { + Position Begin = sourceLocToPosition(SourceMgr, RLoc); + Position End = sourceLocToPosition(SourceMgr, RLoc.getLocWithOffset(1)); + addToken(*LRange, HighlightingKind::Bracket); + addToken({Begin, End}, HighlightingKind::Bracket); + return; + } + + // Easy case, we have the `>` token directly available. + if (RTok->kind() == tok::greater) { + if (auto RRange = getRangeForSourceLocation(RLoc)) { + addToken(*LRange, HighlightingKind::Bracket); + addToken(*RRange, HighlightingKind::Bracket); + } + } + } + HighlightingToken &addToken(Range R, HighlightingKind Kind) { HighlightingToken HT; HT.R = std::move(R); @@ -559,6 +599,12 @@ return Base::TraverseConstructorInitializer(Init); } + bool TraverseTypeConstraint(const TypeConstraint *C) { + if (auto *Args = C->getTemplateArgsAsWritten()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + return Base::TraverseTypeConstraint(C); + } + bool VisitPredefinedExpr(PredefinedExpr *E) { H.addToken(E->getLocation(), HighlightingKind::LocalVariable) .addModifier(HighlightingModifier::Static) @@ -567,6 +613,77 @@ return true; } + bool VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) { + if (auto *Args = E->getTemplateArgsAsWritten()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + return true; + } + + bool VisitTemplateDecl(TemplateDecl *D) { + if (auto *TPL = D->getTemplateParameters()) + H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); + return true; + } + + bool VisitTagDecl(TagDecl *D) { + for (unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) { + if (auto *TPL = D->getTemplateParameterList(i)) + H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); + } + return true; + } + + bool VisitClassTemplatePartialSpecializationDecl( + ClassTemplatePartialSpecializationDecl *D) { + if (auto *TPL = D->getTemplateParameters()) + H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); + if (auto *Args = D->getTemplateArgsAsWritten()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + return true; + } + + bool VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) { + if (auto *Args = D->getTemplateArgsInfo()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + return true; + } + + bool VisitVarTemplatePartialSpecializationDecl( + VarTemplatePartialSpecializationDecl *D) { + if (auto *TPL = D->getTemplateParameters()) + H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); + if (auto *Args = D->getTemplateArgsAsWritten()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + return true; + } + + bool VisitClassScopeFunctionSpecializationDecl( + ClassScopeFunctionSpecializationDecl *D) { + if (auto *Args = D->getTemplateArgsAsWritten()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + return true; + } + + bool VisitDeclRefExpr(DeclRefExpr *E) { + H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); + return true; + } + bool VisitMemberExpr(MemberExpr *E) { + H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); + return true; + } + + bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) { + H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc()); + return true; + } + + bool VisitAutoTypeLoc(AutoTypeLoc L) { + if (L.isConstrained()) + H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc()); + return true; + } + bool VisitFunctionDecl(FunctionDecl *D) { if (D->isOverloadedOperator()) { const auto AddOpDeclToken = [&](SourceLocation Loc) { @@ -581,6 +698,10 @@ if (Kind == OO_Call || Kind == OO_Subscript) AddOpDeclToken(Range.getEnd()); } + if (auto *Args = D->getTemplateSpecializationArgsAsWritten()) + H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc()); + if (auto *I = D->getDependentSpecializationInfo()) + H.addAngleBracketTokens(I->getLAngleLoc(), I->getRAngleLoc()); return true; } @@ -632,6 +753,12 @@ return true; } + bool VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { + const auto &B = E->getAngleBrackets(); + H.addAngleBracketTokens(B.getBegin(), B.getEnd()); + return true; + } + bool VisitCallExpr(CallExpr *E) { // Highlighting parameters passed by non-const reference does not really // make sense for literals... @@ -757,6 +884,10 @@ } bool VisitDeclaratorDecl(DeclaratorDecl *D) { + for (unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) { + if (auto *TPL = D->getTemplateParameterList(i)) + H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc()); + } auto *AT = D->getType()->getContainedAutoType(); if (!AT) return true; @@ -858,6 +989,7 @@ } bool VisitOverloadExpr(OverloadExpr *E) { + H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); if (!E->decls().empty()) return true; // handled by findExplicitReferences. auto &Tok = H.addToken(E->getNameLoc(), HighlightingKind::Unknown) @@ -872,6 +1004,7 @@ H.addToken(E->getMemberNameInfo().getLoc(), HighlightingKind::Unknown) .addModifier(HighlightingModifier::DependentName) .addModifier(HighlightingModifier::ClassScope); + H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); return true; } @@ -879,6 +1012,7 @@ H.addToken(E->getNameInfo().getLoc(), HighlightingKind::Unknown) .addModifier(HighlightingModifier::DependentName) .addModifier(HighlightingModifier::ClassScope); + H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc()); return true; } @@ -906,6 +1040,7 @@ H.addToken(L.getTemplateNameLoc(), HighlightingKind::Type) .addModifier(HighlightingModifier::DependentName) .addModifier(HighlightingModifier::ClassScope); + H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc()); return true; } @@ -1074,6 +1209,8 @@ return OS << "Modifier"; case HighlightingKind::Operator: return OS << "Operator"; + case HighlightingKind::Bracket: + return OS << "Bracket"; case HighlightingKind::InactiveCode: return OS << "InactiveCode"; } @@ -1212,6 +1349,8 @@ return "modifier"; case HighlightingKind::Operator: return "operator"; + case HighlightingKind::Bracket: + return "bracket"; case HighlightingKind::InactiveCode: return "comment"; } 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 @@ -130,17 +130,17 @@ )cpp", R"cpp( namespace $Namespace_decl[[abc]] { - template + template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] struct $Class_def[[A]] { $TemplateParameter[[T]] $Field_decl[[t]]; }; } - template - struct $Class_def[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> { + template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] + struct $Class_def[[C]] : $Namespace[[abc]]::$Class[[A]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]] { typename $TemplateParameter[[T]]::$Type_dependentName[[A]]* $Field_decl[[D]]; }; - $Namespace[[abc]]::$Class[[A]] $Variable_def[[AA]]; - typedef $Namespace[[abc]]::$Class[[A]] $Class_decl[[AAA]]; + $Namespace[[abc]]::$Class[[A]]$Bracket[[<]]int$Bracket[[>]] $Variable_def[[AA]]; + typedef $Namespace[[abc]]::$Class[[A]]$Bracket[[<]]int$Bracket[[>]] $Class_decl[[AAA]]; struct $Class_def[[B]] { $Class_decl_constrDestr[[B]](); ~$Class_decl_constrDestr[[B]](); @@ -243,36 +243,36 @@ typedef float $Primitive_decl[[F]]; )cpp", R"cpp( - template + template$Bracket[[<]]typename $TemplateParameter_def[[T]], typename = void$Bracket[[>]] class $Class_def[[A]] { $TemplateParameter[[T]] $Field_decl[[AA]]; $TemplateParameter[[T]] $Method_decl[[foo]](); }; - template + template$Bracket[[<]]class $TemplateParameter_def[[TT]]$Bracket[[>]] class $Class_def[[B]] { - $Class[[A]]<$TemplateParameter[[TT]]> $Field_decl[[AA]]; + $Class[[A]]$Bracket[[<]]$TemplateParameter[[TT]]$Bracket[[>]] $Field_decl[[AA]]; }; - template + template$Bracket[[<]]class $TemplateParameter_def[[TT]], class $TemplateParameter_def[[GG]]$Bracket[[>]] class $Class_def[[BB]] {}; - template - class $Class_def[[BB]]<$TemplateParameter[[T]], int> {}; - template - class $Class_def[[BB]]<$TemplateParameter[[T]], $TemplateParameter[[T]]*> {}; + template$Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] + class $Class_def[[BB]]$Bracket[[<]]$TemplateParameter[[T]], int$Bracket[[>]] {}; + template$Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] + class $Class_def[[BB]]$Bracket[[<]]$TemplateParameter[[T]], $TemplateParameter[[T]]*$Bracket[[>]] {}; - template class $TemplateParameter_def[[T]], class $TemplateParameter_def[[C]]> - $TemplateParameter[[T]]<$TemplateParameter[[C]]> $Function_decl[[f]](); + template$Bracket[[<]]template$Bracket[[<]]class$Bracket[[>]] class $TemplateParameter_def[[T]], class $TemplateParameter_def[[C]]$Bracket[[>]] + $TemplateParameter[[T]]$Bracket[[<]]$TemplateParameter[[C]]$Bracket[[>]] $Function_decl[[f]](); - template + template$Bracket[[<]]typename$Bracket[[>]] class $Class_def[[Foo]] {}; - template + template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] void $Function_decl[[foo]]($TemplateParameter[[T]] ...); )cpp", R"cpp( - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] struct $Class_def[[Tmpl]] {$TemplateParameter[[T]] $Field_decl[[x]] = 0;}; - extern template struct $Class_def[[Tmpl]]; - template struct $Class_def[[Tmpl]]; + extern template struct $Class_def[[Tmpl]]$Bracket[[<]]float$Bracket[[>]]; + template struct $Class_def[[Tmpl]]$Bracket[[<]]double$Bracket[[>]]; )cpp", // This test is to guard against highlightings disappearing when using // conversion operators as their behaviour in the clang AST differ from @@ -335,17 +335,17 @@ )cpp", R"cpp( class $Class_def[[G]] {}; - template<$Class[[G]] *$TemplateParameter_def_readonly[[U]]> + template$Bracket[[<]]$Class[[G]] *$TemplateParameter_def_readonly[[U]]$Bracket[[>]] class $Class_def[[GP]] {}; - template<$Class[[G]] &$TemplateParameter_def_readonly[[U]]> + template$Bracket[[<]]$Class[[G]] &$TemplateParameter_def_readonly[[U]]$Bracket[[>]] class $Class_def[[GR]] {}; - template + template$Bracket[[<]]int *$TemplateParameter_def_readonly[[U]]$Bracket[[>]] class $Class_def[[IP]] { void $Method_def[[f]]() { $Operator[[*]]$TemplateParameter_readonly[[U]] $Operator[[+=]] 5; } }; - template + template$Bracket[[<]]unsigned $TemplateParameter_def_readonly[[U]] = 2$Bracket[[>]] class $Class_def[[Foo]] { void $Method_def[[f]]() { for(int $LocalVariable_def[[I]] = 0; @@ -355,14 +355,14 @@ $Class[[G]] $Variable_def[[L]]; void $Function_def[[f]]() { - $Class[[Foo]]<123> $LocalVariable_def[[F]]; - $Class[[GP]]<$Operator[[&]]$Variable[[L]]> $LocalVariable_def[[LL]]; - $Class[[GR]]<$Variable[[L]]> $LocalVariable_def[[LLL]]; + $Class[[Foo]]$Bracket[[<]]123$Bracket[[>]] $LocalVariable_def[[F]]; + $Class[[GP]]$Bracket[[<]]$Operator[[&]]$Variable[[L]]$Bracket[[>]] $LocalVariable_def[[LL]]; + $Class[[GR]]$Bracket[[<]]$Variable[[L]]$Bracket[[>]] $LocalVariable_def[[LLL]]; } )cpp", R"cpp( - template + template$Bracket[[<]]typename $TemplateParameter_def[[T]], + void ($TemplateParameter[[T]]::*$TemplateParameter_def_readonly[[method]])(int)$Bracket[[>]] struct $Class_def[[G]] { void $Method_def[[foo]]( $TemplateParameter[[T]] *$Parameter_def[[O]]) { @@ -373,7 +373,7 @@ struct $Class_def[[F]] { void $Method_decl[[f]](int); }; - template + template$Bracket[[<]]void (*$TemplateParameter_def_readonly[[Func]])()$Bracket[[>]] struct $Class_def[[A]] { void $Method_def[[f]]() { ($Operator[[*]]$TemplateParameter_readonly[[Func]])(); @@ -382,9 +382,9 @@ void $Function_def[[foo]]() { $Class[[F]] $LocalVariable_def[[FF]]; - $Class[[G]]<$Class[[F]], $Operator[[&]]$Class[[F]]::$Method[[f]]> $LocalVariable_def[[GG]]; + $Class[[G]]$Bracket[[<]]$Class[[F]], $Operator[[&]]$Class[[F]]::$Method[[f]]$Bracket[[>]] $LocalVariable_def[[GG]]; $LocalVariable[[GG]].$Method[[foo]]($Operator[[&]]$LocalVariable_usedAsMutablePointer[[FF]]); - $Class[[A]]<$Function[[foo]]> $LocalVariable_def[[AA]]; + $Class[[A]]$Bracket[[<]]$Function[[foo]]$Bracket[[>]] $LocalVariable_def[[AA]]; } )cpp", // Tokens that share a source range but have conflicting Kinds are not @@ -472,7 +472,7 @@ } )cpp", R"cpp( - template + template$Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] class $Class_def[[A]] { using $TemplateParameter_decl[[TemplateParam1]] = $TemplateParameter[[T]]; typedef $TemplateParameter[[T]] $TemplateParameter_decl[[TemplateParam2]]; @@ -491,9 +491,9 @@ }; )cpp", R"cpp( - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] void $Function_decl[[phase1]]($TemplateParameter[[T]]); - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) { $Function[[phase1]]($Parameter[[P]]); $Unknown_dependentName[[phase2]]($Parameter[[P]]); @@ -501,22 +501,22 @@ )cpp", R"cpp( class $Class_def[[A]] { - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] void $Method_decl[[bar]]($TemplateParameter[[T]]); }; - template + template $Bracket[[<]]class $TemplateParameter_def[[U]]$Bracket[[>]] void $Function_def[[foo]]($TemplateParameter[[U]] $Parameter_def[[P]]) { $Class[[A]]().$Method[[bar]]($Parameter[[P]]); } )cpp", R"cpp( struct $Class_def[[A]] { - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] static void $StaticMethod_decl_static[[foo]]($TemplateParameter[[T]]); }; - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] struct $Class_def[[B]] { void $Method_def[[bar]]() { $Class[[A]]::$StaticMethod_static[[foo]]($TemplateParameter[[T]]()); @@ -524,18 +524,18 @@ }; )cpp", R"cpp( - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] void $Function_decl[[foo]](typename $TemplateParameter[[T]]::$Type_dependentName[[Type]] = $TemplateParameter[[T]]::$Unknown_dependentName[[val]]); )cpp", R"cpp( - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) { $Parameter[[P]].$Unknown_dependentName[[Field]]; } )cpp", R"cpp( - template + template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]] class $Class_def[[A]] { int $Method_def[[foo]]() { return $TemplateParameter[[T]]::$Unknown_dependentName[[Field]]; @@ -549,10 +549,10 @@ )cpp", // Highlighting of template template arguments. R"cpp( - template