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 @@ -15,6 +15,8 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/RecursiveASTVisitor.h" +#include "clang/AST/Type.h" +#include "clang/AST/TypeLoc.h" #include namespace clang { @@ -124,17 +126,16 @@ bool VisitTypedefNameDecl(TypedefNameDecl *TD) { if (const auto *TSI = TD->getTypeSourceInfo()) - addType(TD->getLocation(), TSI->getTypeLoc().getTypePtr()); + addType(TD->getLocation(), TSI->getTypeLoc().getType().getTypePtr()); return true; } - bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) { - // TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr. - addToken(TL.getBeginLoc(), TL.getDecl()); + bool VisitTypedefTypeLoc(TypedefTypeLoc TL) { + addType(TL.getBeginLoc(), TL.getTypePtr()); return true; } - bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) { + bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { if (const TemplateDecl *TD = TL.getTypePtr()->getTemplateName().getAsTemplateDecl()) addToken(TL.getBeginLoc(), TD); @@ -187,6 +188,9 @@ 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 also do 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 @@ -266,7 +266,9 @@ )cpp", R"cpp( namespace $Namespace[[a]] { - struct $Class[[A]] {}; + struct $Class[[A]] { + $Primitive[[void]] $Method[[foo]]($Class[[A]]*); + }; typedef $Primitive[[char]] $Primitive[[C]]; } typedef $Namespace[[a]]::$Class[[A]] $Class[[B]]; @@ -474,7 +476,7 @@ $Macro[[assert]]($Variable[[x]] != $Function[[f]]()); } )cpp", - R"cpp( + R"cpp( struct $Class[[S]] { $Primitive[[float]] $Field[[Value]]; $Class[[S]] *$Field[[Next]]; @@ -488,6 +490,21 @@ // Highlights references to BindingDecls. $Variable[[B1]]++; } + )cpp", + R"cpp( + template + class $Class[[A]] { + using $TemplateParameter[[TemplateParam1]] = $TemplateParameter[[T]]; + typedef $TemplateParameter[[T]] $TemplateParameter[[TemplateParam2]]; + using $Primitive[[IntType]] = $Primitive[[int]]; + + // These typedefs are not yet highlighted, their types are complicated. + using Pointer = $TemplateParameter[[T]] *; + using LVReference = $TemplateParameter[[T]] &; + using RVReference = $TemplateParameter[[T]]&&; + using Array = $TemplateParameter[[T]]*[3]; + using MemberPointer = $Primitive[[int]] (A::*)($Primitive[[int]]); + }; )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase);