Skip to content

Commit 51cdd51

Browse files
committed
[clangd] Added highlightings for template parameters and specializations.
Summary: Template parameters and specializations were not being highlighted before. This adds highlightings to those types of tokens by adding two Visit* methods. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64855 llvm-svn: 366420
1 parent c38e3ef commit 51cdd51

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

clang-tools-extra/clangd/SemanticHighlighting.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ class HighlightingTokenCollector
9999
return true;
100100
}
101101

102+
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc &TL) {
103+
// TemplateTypeParmTypeLoc does not have a TagDecl in its type ptr.
104+
addToken(TL.getBeginLoc(), TL.getDecl());
105+
return true;
106+
}
107+
108+
bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc &TL) {
109+
if (const TemplateDecl *TD =
110+
TL.getTypePtr()->getTemplateName().getAsTemplateDecl())
111+
addToken(TL.getBeginLoc(), TD);
112+
return true;
113+
}
114+
102115
bool VisitTypeLoc(TypeLoc &TL) {
103116
// This check is for not getting two entries when there are anonymous
104117
// structs. It also makes us not highlight certain namespace qualifiers
@@ -135,6 +148,10 @@ class HighlightingTokenCollector
135148
// We highlight class decls, constructor decls and destructor decls as
136149
// `Class` type. The destructor decls are handled in `VisitTypeLoc` (we will
137150
// visit a TypeLoc where the underlying Type is a CXXRecordDecl).
151+
if (isa<ClassTemplateDecl>(D)) {
152+
addToken(Loc, HighlightingKind::Class);
153+
return;
154+
}
138155
if (isa<RecordDecl>(D)) {
139156
addToken(Loc, HighlightingKind::Class);
140157
return;
@@ -175,6 +192,14 @@ class HighlightingTokenCollector
175192
addToken(Loc, HighlightingKind::Namespace);
176193
return;
177194
}
195+
if (isa<TemplateTemplateParmDecl>(D)) {
196+
addToken(Loc, HighlightingKind::TemplateParameter);
197+
return;
198+
}
199+
if (isa<TemplateTypeParmDecl>(D)) {
200+
addToken(Loc, HighlightingKind::TemplateParameter);
201+
return;
202+
}
178203
}
179204

180205
void addToken(SourceLocation Loc, HighlightingKind Kind) {
@@ -297,6 +322,8 @@ llvm::StringRef toTextMateScope(HighlightingKind Kind) {
297322
return "variable.other.enummember.cpp";
298323
case HighlightingKind::Namespace:
299324
return "entity.name.namespace.cpp";
325+
case HighlightingKind::TemplateParameter:
326+
return "entity.name.type.template.cpp";
300327
case HighlightingKind::NumKinds:
301328
llvm_unreachable("must not pass NumKinds to the function");
302329
}

clang-tools-extra/clangd/SemanticHighlighting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum class HighlightingKind {
3232
Enum,
3333
EnumConstant,
3434
Namespace,
35+
TemplateParameter,
3536

3637
NumKinds,
3738
};

clang-tools-extra/clangd/test/semantic-highlighting.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
# CHECK-NEXT: ],
2828
# CHECK-NEXT: [
2929
# CHECK-NEXT: "entity.name.namespace.cpp"
30+
# CHECK-NEXT: ],
31+
# CHECK-NEXT: [
32+
# CHECK-NEXT: "entity.name.type.template.cpp"
3033
# CHECK-NEXT: ]
3134
# CHECK-NEXT: ]
3235
# CHECK-NEXT: },

clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void checkHighlightings(llvm::StringRef Code) {
4040
{HighlightingKind::Namespace, "Namespace"},
4141
{HighlightingKind::EnumConstant, "EnumConstant"},
4242
{HighlightingKind::Field, "Field"},
43-
{HighlightingKind::Method, "Method"}};
43+
{HighlightingKind::Method, "Method"},
44+
{HighlightingKind::TemplateParameter, "TemplateParameter"}};
4445
std::vector<HighlightingToken> ExpectedTokens;
4546
for (const auto &KindString : KindToString) {
4647
std::vector<HighlightingToken> Toks = makeHighlightingTokens(
@@ -80,14 +81,14 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
8081
)cpp",
8182
R"cpp(
8283
namespace $Namespace[[abc]] {
83-
template<typename T>
84+
template<typename $TemplateParameter[[T]]>
8485
struct $Class[[A]] {
85-
T $Field[[t]];
86+
$TemplateParameter[[T]] $Field[[t]];
8687
};
8788
}
88-
template<typename T>
89-
struct $Class[[C]] : $Namespace[[abc]]::A<T> {
90-
typename T::A* $Field[[D]];
89+
template<typename $TemplateParameter[[T]]>
90+
struct $Class[[C]] : $Namespace[[abc]]::$Class[[A]]<$TemplateParameter[[T]]> {
91+
typename $TemplateParameter[[T]]::A* $Field[[D]];
9192
};
9293
$Namespace[[abc]]::$Class[[A]]<int> $Variable[[AA]];
9394
typedef $Namespace[[abc]]::$Class[[A]]<int> $Class[[AAA]];
@@ -186,6 +187,32 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
186187
using $Enum[[CD]] = $Enum[[CC]];
187188
$Enum[[CC]] $Function[[f]]($Class[[B]]);
188189
$Enum[[CD]] $Function[[f]]($Class[[BB]]);
190+
)cpp",
191+
R"cpp(
192+
template<typename $TemplateParameter[[T]], typename = void>
193+
class $Class[[A]] {
194+
$TemplateParameter[[T]] $Field[[AA]];
195+
$TemplateParameter[[T]] $Method[[foo]]();
196+
};
197+
template<class $TemplateParameter[[TT]]>
198+
class $Class[[B]] {
199+
$Class[[A]]<$TemplateParameter[[TT]]> $Field[[AA]];
200+
};
201+
template<class $TemplateParameter[[TT]], class $TemplateParameter[[GG]]>
202+
class $Class[[BB]] {};
203+
template<class $TemplateParameter[[T]]>
204+
class $Class[[BB]]<$TemplateParameter[[T]], int> {};
205+
template<class $TemplateParameter[[T]]>
206+
class $Class[[BB]]<$TemplateParameter[[T]], $TemplateParameter[[T]]*> {};
207+
208+
template<template<class> class $TemplateParameter[[T]], class $TemplateParameter[[C]]>
209+
$TemplateParameter[[T]]<$TemplateParameter[[C]]> $Function[[f]]();
210+
211+
template<typename>
212+
class $Class[[Foo]] {};
213+
214+
template<typename $TemplateParameter[[T]]>
215+
void $Function[[foo]]($TemplateParameter[[T]] ...);
189216
)cpp"};
190217
for (const auto &TestCase : TestCases) {
191218
checkHighlightings(TestCase);

0 commit comments

Comments
 (0)