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 @@ -14,6 +14,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/Type.h" #include namespace clang { @@ -123,12 +124,6 @@ return true; } - bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) { - // TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr. - addToken(TL.getBeginLoc(), TL.getDecl()); - return true; - } - bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) { if (const TemplateDecl *TD = TL.getTypePtr()->getTemplateName().getAsTemplateDecl()) @@ -179,9 +174,17 @@ void addType(SourceLocation Loc, const Type *TP) { if (!TP) return; + if (TP->isPointerType() || TP->isLValueReferenceType()) + // When highlighting dependant template types the type can be a pointer or + // reference of a template type. To highlight that type we need to get to + // the underlying type. + addType(Loc, TP->getPointeeType().getTypePtr()); if (TP->isBuiltinType()) // Builtins must be special cased as they do not have a TagDecl. addToken(Loc, HighlightingKind::Primitive); + if (const TemplateTypeParmType *TD = dyn_cast(TP)) + // TemplateTypeParmType does not have a TagDecl. + addToken(Loc, TD->getDecl()); if (const TagDecl *TD = TP->getAsTagDecl()) addToken(Loc, TD); } 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 @@ -427,6 +427,14 @@ assert($Variable[[x]] != $Variable[[y]]); assert($Variable[[x]] != $Function[[f]]()); } + )cpp", + R"cpp( + template + class $Class[[A]] { + using $TemplateParameter[[D]] = $TemplateParameter[[T]]; + using $TemplateParameter[[DD]] = $TemplateParameter[[T]] *; + using $TemplateParameter[[DDD]] = $TemplateParameter[[T]] &; + }; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase);