1 //==- SemanticHighlightingTests.cpp - SemanticHighlighting tests-*- C++ -* -==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "Annotations.h"
12 #include "SemanticHighlighting.h"
13 #include "SourceCode.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/ScopedPrinter.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "gmock/gmock.h"
28 using testing::IsEmpty
;
29 using testing::SizeIs
;
31 /// Annotates the input code with provided semantic highlightings. Results look
33 /// class $Class[[X]] {
34 /// $Primitive[[int]] $Field[[a]] = 0;
36 std::string
annotate(llvm::StringRef Input
,
37 llvm::ArrayRef
<HighlightingToken
> Tokens
) {
38 assert(llvm::is_sorted(
39 Tokens
, [](const HighlightingToken
&L
, const HighlightingToken
&R
) {
40 return L
.R
.start
< R
.R
.start
;
44 llvm::raw_string_ostream
OS(Buf
);
45 unsigned NextChar
= 0;
46 for (auto &T
: Tokens
) {
47 unsigned StartOffset
= llvm::cantFail(positionToOffset(Input
, T
.R
.start
));
48 unsigned EndOffset
= llvm::cantFail(positionToOffset(Input
, T
.R
.end
));
49 assert(StartOffset
<= EndOffset
);
50 assert(NextChar
<= StartOffset
);
53 T
.Modifiers
& (1 << uint32_t(HighlightingModifier::Definition
));
55 T
.Modifiers
& (1 << uint32_t(HighlightingModifier::Declaration
));
56 EXPECT_TRUE(!hasDef
|| hasDecl
);
58 OS
<< Input
.substr(NextChar
, StartOffset
- NextChar
);
61 I
<= static_cast<uint32_t>(HighlightingModifier::LastModifier
); ++I
) {
62 if (T
.Modifiers
& (1 << I
)) {
63 // _decl_def is common and redundant, just print _def instead.
64 if (I
!= uint32_t(HighlightingModifier::Declaration
) || !hasDef
)
65 OS
<< '_' << static_cast<HighlightingModifier
>(I
);
68 OS
<< "[[" << Input
.substr(StartOffset
, EndOffset
- StartOffset
) << "]]";
71 OS
<< Input
.substr(NextChar
);
72 return std::move(OS
.str());
75 void checkHighlightings(llvm::StringRef Code
,
76 std::vector
<std::pair
</*FileName*/ llvm::StringRef
,
77 /*FileContent*/ llvm::StringRef
>>
79 uint32_t ModifierMask
= -1,
80 std::vector
<std::string
> AdditionalArgs
= {}) {
81 Annotations
Test(Code
);
83 TU
.Code
= std::string(Test
.code());
85 TU
.ExtraArgs
.push_back("-std=c++20");
86 TU
.ExtraArgs
.push_back("-xobjective-c++");
87 TU
.ExtraArgs
.insert(std::end(TU
.ExtraArgs
), std::begin(AdditionalArgs
),
88 std::end(AdditionalArgs
));
90 for (auto File
: AdditionalFiles
)
91 TU
.AdditionalFiles
.insert({File
.first
, std::string(File
.second
)});
92 auto AST
= TU
.build();
94 getSemanticHighlightings(AST
, /*IncludeInactiveRegionTokens=*/true);
95 for (auto &Token
: Actual
)
96 Token
.Modifiers
&= ModifierMask
;
98 EXPECT_EQ(Code
, annotate(Test
.code(), Actual
));
101 constexpr static uint32_t ScopeModifierMask
=
102 1 << unsigned(HighlightingModifier::FunctionScope
) |
103 1 << unsigned(HighlightingModifier::ClassScope
) |
104 1 << unsigned(HighlightingModifier::FileScope
) |
105 1 << unsigned(HighlightingModifier::GlobalScope
);
107 TEST(SemanticHighlighting
, GetsCorrectTokens
) {
108 const char *TestCases
[] = {
110 struct $Class_def[[AS]] {
111 double $Field_decl[[SomeMember]];
114 } $Variable_def[[S]];
115 void $Function_def[[foo]](int $Parameter_def[[A]], $Class[[AS]] $Parameter_def[[As]]) {
116 $Primitive_deduced_defaultLibrary[[auto]] $LocalVariable_def[[VeryLongVariableName]] = 12312;
117 $Class[[AS]] $LocalVariable_def[[AA]];
118 $Primitive_deduced_defaultLibrary[[auto]] $LocalVariable_def[[L]] = $LocalVariable[[AA]].$Field[[SomeMember]] $Operator[[+]] $Parameter[[A]];
119 auto $LocalVariable_def[[FN]] = [ $LocalVariable[[AA]]](int $Parameter_def[[A]]) -> void {};
120 $LocalVariable[[FN]]$Operator_userDefined[[(]]12312$Operator_userDefined[[)]];
124 void $Function_decl[[foo]](int);
125 void $Function_decl[[Gah]]();
126 void $Function_def[[foo]]() {
127 auto $LocalVariable_def[[Bou]] = $Function[[Gah]];
129 struct $Class_def[[A]] {
130 void $Method_decl[[abc]]();
134 namespace $Namespace_decl[[abc]] {
135 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
136 struct $Class_def[[A]] {
137 $TemplateParameter[[T]] $Field_decl[[t]];
140 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
141 struct $Class_def[[C]] : $Namespace[[abc]]::$Class[[A]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]] {
142 typename $TemplateParameter[[T]]::$Type_dependentName[[A]]* $Field_decl[[D]];
144 $Namespace[[abc]]::$Class[[A]]$Bracket[[<]]int$Bracket[[>]] $Variable_def[[AA]];
145 typedef $Namespace[[abc]]::$Class[[A]]$Bracket[[<]]int$Bracket[[>]] $Class_decl[[AAA]];
146 struct $Class_def[[B]] {
147 $Class_decl_constrDestr[[B]]();
148 ~$Class_decl_constrDestr[[B]]();
149 void operator$Operator_decl[[<<]]($Class[[B]]);
150 $Class[[AAA]] $Field_decl[[AA]];
152 $Class[[B]]::$Class_def_constrDestr[[B]]() {}
153 $Class[[B]]::~$Class_def_constrDestr[[B]]() {}
154 void $Function_def[[f]] () {
155 $Class[[B]] $LocalVariable_def[[BB]] = $Class[[B]]();
156 $LocalVariable[[BB]].~$Class_constrDestr[[B]]();
161 enum class $Enum_decl[[E]] {
162 $EnumConstant_decl_readonly[[A]],
163 $EnumConstant_decl_readonly[[B]],
165 enum $Enum_decl[[EE]] {
166 $EnumConstant_decl_readonly[[Hi]],
168 struct $Class_def[[A]] {
169 $Enum[[E]] $Field_decl[[EEE]];
170 $Enum[[EE]] $Field_decl[[EEEE]];
172 int $Variable_def[[I]] = $EnumConstant_readonly[[Hi]];
173 $Enum[[E]] $Variable_def[[L]] = $Enum[[E]]::$EnumConstant_readonly[[B]];
176 namespace $Namespace_decl[[abc]] {
178 namespace $Namespace_decl[[bcd]] {
179 struct $Class_def[[A]] {};
180 namespace $Namespace_decl[[cde]] {
181 struct $Class_def[[A]] {
182 enum class $Enum_decl[[B]] {
183 $EnumConstant_decl_readonly[[Hi]],
189 using namespace $Namespace[[abc]]::$Namespace[[bcd]];
190 namespace $Namespace_decl[[vwz]] =
191 $Namespace[[abc]]::$Namespace[[bcd]]::$Namespace[[cde]];
192 $Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable_def[[AA]];
193 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]] $Variable_def[[AAA]] =
194 $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant_readonly[[Hi]];
195 ::$Namespace[[vwz]]::$Class[[A]] $Variable_def[[B]];
196 ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable_def[[BB]];
199 struct $Class_def[[D]] {
200 double $Field_decl[[C]];
202 struct $Class_def[[A]] {
203 double $Field_decl[[B]];
204 $Class[[D]] $Field_decl[[E]];
205 static double $StaticField_decl_static[[S]];
206 static void $StaticMethod_def_static[[bar]]() {}
207 void $Method_def[[foo]]() {
208 $Field[[B]] $Operator[[=]] 123;
209 this->$Field[[B]] $Operator[[=]] 156;
210 this->$Method[[foo]]();
212 $StaticMethod_static[[bar]]();
213 $StaticField_static[[S]] $Operator[[=]] 90.1;
216 void $Function_def[[foo]]() {
217 $Class[[A]] $LocalVariable_def[[AA]];
218 $LocalVariable[[AA]].$Field[[B]] $Operator[[+=]] 2;
219 $LocalVariable[[AA]].$Method[[foo]]();
220 $LocalVariable[[AA]].$Field[[E]].$Field[[C]];
221 $Class[[A]]::$StaticField_static[[S]] $Operator[[=]] 90;
225 struct $Class_def[[AA]] {
226 int $Field_decl[[A]];
228 int $Variable_def[[B]];
229 $Class[[AA]] $Variable_def[[A]]{$Variable[[B]]};
232 namespace $Namespace_decl[[a]] {
233 struct $Class_def[[A]] {};
234 typedef char $Primitive_decl[[C]];
236 typedef $Namespace[[a]]::$Class[[A]] $Class_decl[[B]];
237 using $Class_decl[[BB]] = $Namespace[[a]]::$Class[[A]];
238 enum class $Enum_decl[[E]] {};
239 typedef $Enum[[E]] $Enum_decl[[C]];
240 typedef $Enum[[C]] $Enum_decl[[CC]];
241 using $Enum_decl[[CD]] = $Enum[[CC]];
242 $Enum[[CC]] $Function_decl[[f]]($Class[[B]]);
243 $Enum[[CD]] $Function_decl[[f]]($Class[[BB]]);
244 typedef $Namespace[[a]]::$Primitive[[C]] $Primitive_decl[[PC]];
245 typedef float $Primitive_decl[[F]];
248 template$Bracket[[<]]typename $TemplateParameter_def[[T]], typename = void$Bracket[[>]]
249 class $Class_def[[A]] {
250 $TemplateParameter[[T]] $Field_decl[[AA]];
251 $TemplateParameter[[T]] $Method_decl[[foo]]();
253 template$Bracket[[<]]class $TemplateParameter_def[[TT]]$Bracket[[>]]
254 class $Class_def[[B]] {
255 $Class[[A]]$Bracket[[<]]$TemplateParameter[[TT]]$Bracket[[>]] $Field_decl[[AA]];
257 template$Bracket[[<]]class $TemplateParameter_def[[TT]], class $TemplateParameter_def[[GG]]$Bracket[[>]]
258 class $Class_def[[BB]] {};
259 template$Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
260 class $Class_def[[BB]]$Bracket[[<]]$TemplateParameter[[T]], int$Bracket[[>]] {};
261 template$Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
262 class $Class_def[[BB]]$Bracket[[<]]$TemplateParameter[[T]], $TemplateParameter[[T]]*$Bracket[[>]] {};
264 template$Bracket[[<]]template$Bracket[[<]]class$Bracket[[>]] class $TemplateParameter_def[[T]], class $TemplateParameter_def[[C]]$Bracket[[>]]
265 $TemplateParameter[[T]]$Bracket[[<]]$TemplateParameter[[C]]$Bracket[[>]] $Function_decl[[f]]();
267 template$Bracket[[<]]typename$Bracket[[>]]
268 class $Class_def[[Foo]] {};
270 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
271 void $Function_decl[[foo]]($TemplateParameter[[T]] ...);
274 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
275 struct $Class_def[[Tmpl]] {$TemplateParameter[[T]] $Field_decl[[x]] = 0;};
276 extern template struct $Class_def[[Tmpl]]$Bracket[[<]]float$Bracket[[>]];
277 template struct $Class_def[[Tmpl]]$Bracket[[<]]double$Bracket[[>]];
279 // This test is to guard against highlightings disappearing when using
280 // conversion operators as their behaviour in the clang AST differ from
281 // other CXXMethodDecls.
283 class $Class_def[[Foo]] {};
284 struct $Class_def[[Bar]] {
285 explicit operator $Class[[Foo]]*() const;
286 explicit operator int() const;
287 operator $Class[[Foo]]();
289 void $Function_def[[f]]() {
290 $Class[[Bar]] $LocalVariable_def[[B]];
291 $Class[[Foo]] $LocalVariable_def[[F]] = $LocalVariable[[B]];
292 $Class[[Foo]] *$LocalVariable_def[[FP]] = ($Class[[Foo]]*)$LocalVariable[[B]];
293 int $LocalVariable_def[[I]] = (int)$LocalVariable[[B]];
297 struct $Class_def[[B]] {};
298 struct $Class_def[[A]] {
299 $Class[[B]] $Field_decl[[BB]];
300 $Class[[A]] &operator$Operator_decl[[=]]($Class[[A]] &&$Parameter_def[[O]]);
303 $Class[[A]] &$Class[[A]]::operator$Operator_def[[=]]($Class[[A]] &&$Parameter_def[[O]]) = default;
306 enum $Enum_decl[[En]] {
307 $EnumConstant_decl_readonly[[EC]],
309 class $Class_def[[Foo]] {};
310 class $Class_def[[Bar]] {
312 $Class[[Foo]] $Field_decl[[Fo]];
313 $Enum[[En]] $Field_decl[[E]];
314 int $Field_decl[[I]];
315 $Class_def_constrDestr[[Bar]] ($Class[[Foo]] $Parameter_def[[F]],
316 $Enum[[En]] $Parameter_def[[E]])
317 : $Field[[Fo]] ($Parameter[[F]]), $Field[[E]] ($Parameter[[E]]),
320 class $Class_def[[Bar2]] : public $Class[[Bar]] {
321 $Class_def_constrDestr[[Bar2]]() : $Class[[Bar]]($Class[[Foo]](), $EnumConstant_readonly[[EC]]) {}
325 enum $Enum_decl[[E]] {
326 $EnumConstant_decl_readonly[[E]],
328 class $Class_def[[Foo]] {};
329 $Enum_deduced[[auto]] $Variable_def[[AE]] = $Enum[[E]]::$EnumConstant_readonly[[E]];
330 $Class_deduced[[auto]] $Variable_def[[AF]] = $Class[[Foo]]();
331 $Class_deduced[[decltype]](auto) $Variable_def[[AF2]] = $Class[[Foo]]();
332 $Class_deduced[[auto]] *$Variable_def[[AFP]] = $Operator[[&]]$Variable[[AF]];
333 $Enum_deduced[[auto]] &$Variable_def[[AER]] = $Variable[[AE]];
334 $Primitive_deduced_defaultLibrary[[auto]] $Variable_def[[Form]] = 10.2 $Operator[[+]] 2 $Operator[[*]] 4;
335 $Primitive_deduced_defaultLibrary[[decltype]]($Variable[[Form]]) $Variable_def[[F]] = 10;
336 auto $Variable_def[[Fun]] = []()->void{};
339 class $Class_def[[G]] {};
340 template$Bracket[[<]]$Class[[G]] *$TemplateParameter_def_readonly[[U]]$Bracket[[>]]
341 class $Class_def[[GP]] {};
342 template$Bracket[[<]]$Class[[G]] &$TemplateParameter_def_readonly[[U]]$Bracket[[>]]
343 class $Class_def[[GR]] {};
344 template$Bracket[[<]]int *$TemplateParameter_def_readonly[[U]]$Bracket[[>]]
345 class $Class_def[[IP]] {
346 void $Method_def[[f]]() {
347 $Operator[[*]]$TemplateParameter_readonly[[U]] $Operator[[+=]] 5;
350 template$Bracket[[<]]unsigned $TemplateParameter_def_readonly[[U]] = 2$Bracket[[>]]
351 class $Class_def[[Foo]] {
352 void $Method_def[[f]]() {
353 for(int $LocalVariable_def[[I]] = 0;
354 $LocalVariable[[I]] $Operator[[<]] $TemplateParameter_readonly[[U]];) {}
358 $Class[[G]] $Variable_def[[L]];
359 void $Function_def[[f]]() {
360 $Class[[Foo]]$Bracket[[<]]123$Bracket[[>]] $LocalVariable_def[[F]];
361 $Class[[GP]]$Bracket[[<]]$Operator[[&]]$Variable[[L]]$Bracket[[>]] $LocalVariable_def[[LL]];
362 $Class[[GR]]$Bracket[[<]]$Variable[[L]]$Bracket[[>]] $LocalVariable_def[[LLL]];
366 template$Bracket[[<]]typename $TemplateParameter_def[[T]],
367 void ($TemplateParameter[[T]]::*$TemplateParameter_def_readonly[[method]])(int)$Bracket[[>]]
368 struct $Class_def[[G]] {
369 void $Method_def[[foo]](
370 $TemplateParameter[[T]] *$Parameter_def[[O]]) {
371 ($Parameter[[O]]$Operator_userDefined[[->*]]$TemplateParameter_readonly[[method]])(10);
375 struct $Class_def[[F]] {
376 void $Method_decl[[f]](int);
378 template$Bracket[[<]]void (*$TemplateParameter_def_readonly[[Func]])()$Bracket[[>]]
379 struct $Class_def[[A]] {
380 void $Method_def[[f]]() {
381 ($Operator[[*]]$TemplateParameter_readonly[[Func]])();
385 void $Function_def[[foo]]() {
386 $Class[[F]] $LocalVariable_def[[FF]];
387 $Class[[G]]$Bracket[[<]]$Class[[F]], $Operator[[&]]$Class[[F]]::$Method[[f]]$Bracket[[>]] $LocalVariable_def[[GG]];
388 $LocalVariable[[GG]].$Method[[foo]]($Operator[[&]]$LocalVariable_usedAsMutablePointer[[FF]]);
389 $Class[[A]]$Bracket[[<]]$Function[[foo]]$Bracket[[>]] $LocalVariable_def[[AA]];
392 // Tokens that share a source range but have conflicting Kinds are not
395 #define $Macro_decl[[DEF_MULTIPLE]](X) namespace X { class X { int X; }; }
396 #define $Macro_decl[[DEF_CLASS]](T) class T {};
398 $Macro[[DEF_MULTIPLE]](XYZ);
399 $Macro[[DEF_MULTIPLE]](XYZW);
400 $Macro[[DEF_CLASS]]($Class_def[[A]])
401 #define $Macro_decl[[MACRO_CONCAT]](X, V, T) T foo##X = V
402 #define $Macro_decl[[DEF_VAR]](X, V) int X = V
403 #define $Macro_decl[[DEF_VAR_T]](T, X, V) T X = V
404 #define $Macro_decl[[DEF_VAR_REV]](V, X) $Macro[[DEF_VAR]](X, V)
405 #define $Macro_decl[[CPY]](X) X
406 #define $Macro_decl[[DEF_VAR_TYPE]](X, Y) X Y
407 #define $Macro_decl[[SOME_NAME]] variable
408 #define $Macro_decl[[SOME_NAME_SET]] variable2 = 123
409 #define $Macro_decl[[INC_VAR]](X) X += 2
410 void $Function_def[[foo]]() {
411 $Macro[[DEF_VAR]]($LocalVariable_def[[X]], 123);
412 $Macro[[DEF_VAR_REV]](908, $LocalVariable_def[[XY]]);
413 int $Macro[[CPY]]( $LocalVariable_def[[XX]] );
414 $Macro[[DEF_VAR_TYPE]]($Class[[A]], $LocalVariable_def[[AA]]);
415 double $Macro[[SOME_NAME]];
416 int $Macro[[SOME_NAME_SET]];
417 $LocalVariable[[variable]] $Operator[[=]] 20.1;
418 $Macro[[MACRO_CONCAT]](var, 2, float);
419 $Macro[[DEF_VAR_T]]($Class[[A]], $Macro[[CPY]](
420 $Macro[[CPY]]($LocalVariable_def[[Nested]])),
421 $Macro[[CPY]]($Class[[A]]()));
422 $Macro[[INC_VAR]]($LocalVariable[[variable]]);
424 void $Macro[[SOME_NAME]]();
425 $Macro[[DEF_VAR]]($Variable_def[[MMMMM]], 567);
426 $Macro[[DEF_VAR_REV]](756, $Variable_def[[AB]]);
428 #define $Macro_decl[[CALL_FN]](F) F();
429 #define $Macro_decl[[DEF_FN]](F) void F ()
430 $Macro[[DEF_FN]]($Function_def[[g]]) {
431 $Macro[[CALL_FN]]($Function[[foo]]);
435 #define $Macro_decl[[fail]](expr) expr
436 #define $Macro_decl[[assert]](COND) if (!(COND)) { $Macro[[fail]]("assertion failed
" #COND); }
438 int $Variable_def[[x]];
439 int $Variable_def[[y]];
440 int $Function_decl[[f]]();
441 void $Function_def[[foo]]() {
442 $Macro[[assert]]($Variable[[x]] $Operator[[!=]] $Variable[[y]]);
443 $Macro[[assert]]($Variable[[x]] $Operator[[!=]] $Function[[f]]());
446 // highlighting all macro references
448 #ifndef $Macro[[name]]
449 #define $Macro_decl[[name]]
452 #define $Macro_decl[[test]]
453 #undef $Macro[[test]]
454 #ifdef $Macro[[test]]
457 #if defined($Macro[[test]])
461 struct $Class_def[[S]] {
462 float $Field_decl[[Value]];
463 $Class[[S]] *$Field_decl[[Next]];
465 $Class[[S]] $Variable_def[[Global]][2] = {$Class[[S]](), $Class[[S]]()};
466 auto [$Variable_decl[[G1]], $Variable_decl[[G2]]] = $Variable[[Global]];
467 void $Function_def[[f]]($Class[[S]] $Parameter_def[[P]]) {
468 int $LocalVariable_def[[A]][2] = {1,2};
469 auto [$LocalVariable_decl[[B1]], $LocalVariable_decl[[B2]]] = $LocalVariable[[A]];
470 auto [$LocalVariable_decl[[G1]], $LocalVariable_decl[[G2]]] = $Variable[[Global]];
471 $Class_deduced[[auto]] [$LocalVariable_decl[[P1]], $LocalVariable_decl[[P2]]] = $Parameter[[P]];
472 // Highlights references to BindingDecls.
473 $LocalVariable[[B1]]$Operator[[++]];
477 template$Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
478 class $Class_def[[A]] {
479 using $TemplateParameter_decl[[TemplateParam1]] = $TemplateParameter[[T]];
480 typedef $TemplateParameter[[T]] $TemplateParameter_decl[[TemplateParam2]];
481 using $Primitive_decl[[IntType]] = int;
483 using $Typedef_decl[[Pointer]] = $TemplateParameter[[T]] *;
484 using $Typedef_decl[[LVReference]] = $TemplateParameter[[T]] &;
485 using $Typedef_decl[[RVReference]] = $TemplateParameter[[T]]&&;
486 using $Typedef_decl[[Array]] = $TemplateParameter[[T]]*[3];
487 using $Typedef_decl[[MemberPointer]] = int ($Class[[A]]::*)(int);
489 // Use various previously defined typedefs in a function type.
490 void $Method_decl[[func]](
491 $Typedef[[Pointer]], $Typedef[[LVReference]], $Typedef[[RVReference]],
492 $Typedef[[Array]], $Typedef[[MemberPointer]]);
496 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
497 void $Function_decl[[phase1]]($TemplateParameter[[T]]);
498 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
499 void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) {
500 $Function[[phase1]]($Parameter[[P]]);
501 $Unknown_dependentName[[phase2]]($Parameter[[P]]);
505 class $Class_def[[A]] {
506 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
507 void $Method_decl[[bar]]($TemplateParameter[[T]]);
510 template $Bracket[[<]]class $TemplateParameter_def[[U]]$Bracket[[>]]
511 void $Function_def[[foo]]($TemplateParameter[[U]] $Parameter_def[[P]]) {
512 $Class[[A]]().$Method[[bar]]($Parameter[[P]]);
516 struct $Class_def[[A]] {
517 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
518 static void $StaticMethod_decl_static[[foo]]($TemplateParameter[[T]]);
521 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
522 struct $Class_def[[B]] {
523 void $Method_def[[bar]]() {
524 $Class[[A]]::$StaticMethod_static[[foo]]($TemplateParameter[[T]]());
529 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
530 void $Function_decl[[foo]](typename $TemplateParameter[[T]]::$Type_dependentName[[Type]]
531 = $TemplateParameter[[T]]::$Unknown_dependentName[[val]]);
534 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
535 void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) {
536 $Parameter[[P]].$Unknown_dependentName[[Field]];
540 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
541 class $Class_def[[A]] {
542 int $Method_def[[foo]]() {
543 return $TemplateParameter[[T]]::$Unknown_dependentName[[Field]];
547 // Highlighting the using decl as the underlying using shadow decl.
549 void $Function_decl[[foo]]();
550 using ::$Function[[foo]];
552 // Highlighting of template template arguments.
554 template $Bracket[[<]]template $Bracket[[<]]class$Bracket[[>]] class $TemplateParameter_def[[TT]],
555 template $Bracket[[<]]class$Bracket[[>]] class ...$TemplateParameter_def[[TTs]]$Bracket[[>]]
556 struct $Class_def[[Foo]] {
557 $Class[[Foo]]$Bracket[[<]]$TemplateParameter[[TT]], $TemplateParameter[[TTs]]...$Bracket[[>]]
561 // Inactive code highlighting
563 // Code in the preamble.
564 // Inactive lines get an empty InactiveCode token at the beginning.
565 #ifdef $Macro[[test]]
566 $InactiveCode[[int Inactive1;]]
569 // A declaration to cause the preamble to end.
570 int $Variable_def[[EndPreamble]];
572 // Code after the preamble.
573 // Code inside inactive blocks does not get regular highlightings
574 // because it's not part of the AST.
575 #define $Macro_decl[[test2]]
576 #if defined($Macro[[test]])
577 $InactiveCode[[int Inactive2;]]
578 #elif defined($Macro[[test2]])
579 int $Variable_def[[Active1]];
581 $InactiveCode[[int Inactive3;]]
584 #ifndef $Macro[[test]]
585 int $Variable_def[[Active2]];
588 #ifdef $Macro[[test]]
589 $InactiveCode[[int Inactive4;]]
591 int $Variable_def[[Active3]];
594 // Argument to 'sizeof...'
596 template $Bracket[[<]]typename... $TemplateParameter_def[[Elements]]$Bracket[[>]]
597 struct $Class_def[[TupleSize]] {
598 static const int $StaticField_decl_readonly_static[[size]] =
599 sizeof...($TemplateParameter[[Elements]]);
602 // More dependent types
604 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
605 struct $Class_def[[Waldo]] {
606 using $Typedef_decl[[Location1]] = typename $TemplateParameter[[T]]
607 ::$Type_dependentName[[Resolver]]::$Type_dependentName[[Location]];
608 using $Typedef_decl[[Location2]] = typename $TemplateParameter[[T]]
609 ::template $Type_dependentName[[Resolver]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]
610 ::$Type_dependentName[[Location]];
611 using $Typedef_decl[[Location3]] = typename $TemplateParameter[[T]]
612 ::$Type_dependentName[[Resolver]]
613 ::template $Type_dependentName[[Location]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]];
614 static const int $StaticField_decl_readonly_static[[Value]] = $TemplateParameter[[T]]
615 ::$Type_dependentName[[Resolver]]::$Unknown_dependentName[[Value]];
618 // Dependent name with heuristic target
620 template $Bracket[[<]]typename$Bracket[[>]]
621 struct $Class_def[[Foo]] {
622 int $Field_decl[[Waldo]];
623 void $Method_def[[bar]]() {
624 $Class[[Foo]]().$Field_dependentName[[Waldo]];
626 template $Bracket[[<]]typename $TemplateParameter_def[[U]]$Bracket[[>]]
627 void $Method_def[[bar1]]() {
628 $Class[[Foo]]$Bracket[[<]]$TemplateParameter[[U]]$Bracket[[>]]().$Field_dependentName[[Waldo]];
631 void $Method_decl[[Overload]]();
632 void $Method_decl_readonly[[Overload]]() const;
634 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
635 void $Function_def[[baz]]($Class[[Foo]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]] $Parameter_def[[o]]) {
636 $Parameter[[o]].$Method_readonly_dependentName[[Overload]]();
641 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
642 concept $Concept_decl[[Fooable]] =
643 requires($TemplateParameter[[T]] $Parameter_def[[F]]) {
644 $Parameter[[F]].$Unknown_dependentName[[foo]]();
646 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
647 requires $Concept[[Fooable]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]
648 void $Function_def[[bar]]($TemplateParameter[[T]] $Parameter_def[[F]]) {
649 $Parameter[[F]].$Unknown_dependentName[[foo]]();
652 struct $Class_def[[F]] {
653 void $Method_def[[foo]]() {};
655 $Concept[[Fooable]] $Class_deduced[[auto]] $Variable_def[[f]] = $Class[[F]]();
657 void $Function_def[[Bar]]($Concept[[Fooable]] $TemplateParameter[[auto]] $Parameter_def[[x]]) {}
659 template$Bracket[[<]]$Concept[[Fooable]] auto $TemplateParameter_def_readonly[[x]]$Bracket[[>]] void $Function_def[[Boo]]() {}
661 // Dependent template name
663 template $Bracket[[<]]template $Bracket[[<]]typename$Bracket[[>]] class$Bracket[[>]] struct $Class_def[[A]] {};
664 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
665 using $Typedef_decl[[W]] = $Class[[A]]$Bracket[[<]]
666 $TemplateParameter[[T]]::template $Class_dependentName[[Waldo]]
670 class $Class_def_abstract[[Abstract]] {
672 virtual void $Method_decl_abstract_virtual[[pure]]() = 0;
673 virtual void $Method_decl_virtual[[impl]]();
675 void $Function_def[[foo]]($Class_abstract[[Abstract]]* $Parameter_def[[A]]) {
676 $Parameter[[A]]->$Method_abstract_virtual[[pure]]();
677 $Parameter[[A]]->$Method_virtual[[impl]]();
681 <:[deprecated]:> int $Variable_def_deprecated[[x]];
684 // ObjC: Classes and methods
685 @class $Class_decl[[Forward]];
687 @interface $Class_def[[Foo]]
689 @interface $Class_def[[Bar]] : $Class[[Foo]]
690 -(id) $Method_decl[[x]]:(int)$Parameter_def[[a]] $Method_decl[[y]]:(int)$Parameter_def[[b]];
691 +(instancetype)$StaticMethod_decl_static[[sharedInstance]];
692 +(void) $StaticMethod_decl_static[[explode]];
694 @implementation $Class_def[[Bar]]
695 -(id) $Method_def[[x]]:(int)$Parameter_def[[a]] $Method_def[[y]]:(int)$Parameter_def[[b]] {
698 +(instancetype)$StaticMethod_def_static[[sharedInstance]] { return 0; }
699 +(void) $StaticMethod_def_static[[explode]] {}
702 void $Function_def[[m]]($Class[[Bar]] *$Parameter_def[[b]]) {
703 [$Parameter[[b]] $Method[[x]]:1 $Method[[y]]:2];
704 [$Class[[Bar]] $StaticMethod_static[[explode]]];
709 @protocol $Interface_def[[Protocol]]
711 @protocol $Interface_def[[Protocol2]] <$Interface[[Protocol]]>
713 @interface $Class_def[[Klass]] <$Interface[[Protocol]]>
715 id<$Interface[[Protocol]]> $Variable_def[[x]];
719 @interface $Class_def[[Foo]]
721 @interface $Class[[Foo]]($Namespace_def[[Bar]])
723 @implementation $Class[[Foo]]($Namespace_def[[Bar]])
727 // ObjC: Properties and Ivars.
728 @interface $Class_def[[Foo]] {
729 int $Field_decl[[_someProperty]];
731 @property(nonatomic, assign) int $Field_decl[[someProperty]];
732 @property(readonly, class) $Class[[Foo]] *$Field_decl_readonly_static[[sharedInstance]];
734 @implementation $Class_def[[Foo]]
735 @synthesize someProperty = _someProperty;
736 - (int)$Method_def[[otherMethod]] {
739 - (int)$Method_def[[doSomething]] {
740 $Class[[Foo]].$Field_static[[sharedInstance]].$Field[[someProperty]] $Operator[[=]] 1;
741 self.$Field[[someProperty]] $Operator[[=]] self.$Field[[someProperty]] $Operator[[+]] self.$Field[[otherMethod]] $Operator[[+]] 1;
742 self->$Field[[_someProperty]] $Operator[[=]] $Field[[_someProperty]] $Operator[[+]] 1;
746 // Member imported from dependent base
748 template $Bracket[[<]]typename$Bracket[[>]] struct $Class_def[[Base]] {
749 int $Field_decl[[member]];
751 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
752 struct $Class_def[[Derived]] : $Class[[Base]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]] {
753 using $Class[[Base]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]::$Field_dependentName[[member]];
755 void $Method_def[[method]]() {
756 (void)$Field_dependentName[[member]];
760 // Modifier for variables passed as non-const references
762 struct $Class_def[[ClassWithOp]] {
763 void operator$Operator_decl[[(]]$Operator_decl[[)]](int);
764 void operator$Operator_decl[[(]]$Operator_decl[[)]](int, int &);
765 void operator$Operator_decl[[(]]$Operator_decl[[)]](int, int, const int &);
766 int &operator$Operator_decl[[[]]$Operator_decl[[]]](int &);
767 int operator$Operator_decl[[[]]$Operator_decl[[]]](int) const;
769 struct $Class_def[[ClassWithStaticMember]] {
770 static inline int $StaticField_def_static[[j]] = 0;
772 struct $Class_def[[ClassWithRefMembers]] {
773 $Class_def_constrDestr[[ClassWithRefMembers]](int $Parameter_def[[i]])
774 : $Field[[i1]]($Parameter[[i]]),
775 $Field_readonly[[i2]]($Parameter[[i]]),
776 $Field[[i3]]($Parameter_usedAsMutableReference[[i]]),
777 $Field_readonly[[i4]]($Class[[ClassWithStaticMember]]::$StaticField_static[[j]]),
778 $Field[[i5]]($Class[[ClassWithStaticMember]]::$StaticField_static_usedAsMutableReference[[j]])
780 int $Field_decl[[i1]];
781 const int &$Field_decl_readonly[[i2]];
782 int &$Field_decl[[i3]];
783 const int &$Field_decl_readonly[[i4]];
784 int &$Field_decl[[i5]];
786 void $Function_def[[fun]](int, const int,
789 int*&, const int*&, const int* const &,
790 int**, int**&, int** const &,
792 int $LocalVariable_def[[val]];
793 int* $LocalVariable_def[[ptr]];
794 const int* $LocalVariable_def_readonly[[constPtr]];
795 int** $LocalVariable_def[[array]];
796 $Function[[fun]]($LocalVariable[[val]], $LocalVariable[[val]],
797 $LocalVariable_usedAsMutablePointer[[ptr]], $LocalVariable_readonly[[constPtr]],
798 $LocalVariable_usedAsMutableReference[[val]], $LocalVariable[[val]],
800 $LocalVariable_usedAsMutableReference[[ptr]],
801 $LocalVariable_readonly_usedAsMutableReference[[constPtr]],
802 $LocalVariable_readonly[[constPtr]],
804 $LocalVariable_usedAsMutablePointer[[array]], $LocalVariable_usedAsMutableReference[[array]],
805 $LocalVariable[[array]]
807 [](int){}$Operator_userDefined[[(]]$LocalVariable[[val]]$Operator_userDefined[[)]];
808 [](int&){}$Operator_userDefined[[(]]$LocalVariable_usedAsMutableReference[[val]]$Operator_userDefined[[)]];
809 [](const int&){}$Operator_userDefined[[(]]$LocalVariable[[val]]$Operator_userDefined[[)]];
810 $Class[[ClassWithOp]] $LocalVariable_def[[c]];
811 const $Class[[ClassWithOp]] $LocalVariable_def_readonly[[c2]];
812 $LocalVariable[[c]]$Operator_userDefined[[(]]$LocalVariable[[val]]$Operator_userDefined[[)]];
813 $LocalVariable[[c]]$Operator_userDefined[[(]]0, $LocalVariable_usedAsMutableReference[[val]]$Operator_userDefined[[)]];
814 $LocalVariable[[c]]$Operator_userDefined[[(]]0, 0, $LocalVariable[[val]]$Operator_userDefined[[)]];
815 $LocalVariable[[c]]$Operator_userDefined[[[]]$LocalVariable_usedAsMutableReference[[val]]$Operator_userDefined[[]]];
816 $LocalVariable_readonly[[c2]]$Operator_userDefined[[[]]$LocalVariable[[val]]$Operator_userDefined[[]]];
818 struct $Class_def[[S]] {
819 $Class_def_constrDestr[[S]](int&) {
820 $Class[[S]] $LocalVariable_def[[s1]]($Field_usedAsMutableReference[[field]]);
821 $Class[[S]] $LocalVariable_def[[s2]]($LocalVariable[[s1]].$Field_usedAsMutableReference[[field]]);
823 $Class[[S]] $LocalVariable_def[[s3]]($StaticField_static_usedAsMutableReference[[staticField]]);
824 $Class[[S]] $LocalVariable_def[[s4]]($Class[[S]]::$StaticField_static_usedAsMutableReference[[staticField]]);
826 int $Field_decl[[field]];
827 static int $StaticField_decl_static[[staticField]];
829 template $Bracket[[<]]typename $TemplateParameter_def[[X]]$Bracket[[>]]
830 void $Function_def[[foo]]($TemplateParameter[[X]]& $Parameter_def[[x]]) {
831 // We do not support dependent types, so this one should *not* get the modifier.
832 $Function[[foo]]($Parameter[[x]]);
837 void $Function_def[[foo]]() {
838 int $LocalVariable_def[[a]], $LocalVariable_def[[b]];
839 [ $LocalVariable_def[[c]] = $LocalVariable[[a]],
840 $LocalVariable_def[[d]]($LocalVariable[[b]]) ]() {}$Operator_userDefined[[(]]$Operator_userDefined[[)]];
843 // Enum base specifier
845 using $Primitive_decl[[MyTypedef]] = int;
846 enum $Enum_decl[[MyEnum]] : $Primitive[[MyTypedef]] {};
848 // Enum base specifier
850 typedef int $Primitive_decl[[MyTypedef]];
851 enum $Enum_decl[[MyEnum]] : $Primitive[[MyTypedef]] {};
855 enum class $Enum_decl[[Color]] { $EnumConstant_decl_readonly[[Black]] };
856 namespace $Namespace_decl[[ns]] {
857 using enum $Enum[[Color]];
858 $Enum[[Color]] $Variable_def[[ModelT]] = $EnumConstant[[Black]];
863 void $Function_decl[[Foo]]();
864 // Use <: :> digraphs for deprecated attribute to avoid conflict with annotation syntax
865 <:<:deprecated:>:> void $Function_decl_deprecated[[Foo]](int* $Parameter_def[[x]]);
866 void $Function_decl[[Foo]](int $Parameter_def[[x]]);
867 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
868 void $Function_def[[Bar]]($TemplateParameter[[T]] $Parameter_def[[x]]) {
869 $Function_deprecated[[Foo]]($Parameter[[x]]);
870 $Function_deprecated[[Foo]]($Parameter[[x]]);
871 $Function_deprecated[[Foo]]($Parameter[[x]]);
874 // Predefined identifiers
876 void $Function_def[[Foo]]() {
877 const char *$LocalVariable_def_readonly[[s]] = $LocalVariable_readonly_static[[__func__]];
880 // override and final
882 class $Class_def_abstract[[Base]] { virtual void $Method_decl_abstract_virtual[[m]]() = 0; };
883 class $Class_def[[override]] : public $Class_abstract[[Base]] { void $Method_decl_virtual[[m]]() $Modifier[[override]]; };
884 class $Class_def[[final]] : public $Class[[override]] { void $Method_decl_virtual[[m]]() $Modifier[[override]] $Modifier[[final]]; };
886 // Issue 1222: readonly modifier for generic parameter
888 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
889 auto $Function_def[[foo]](const $TemplateParameter[[T]] $Parameter_def_readonly[[template_type]],
890 const $TemplateParameter[[auto]] $Parameter_def_readonly[[auto_type]],
891 const int $Parameter_def_readonly[[explicit_type]]) {
892 return $Parameter_readonly[[template_type]]
893 $Operator_userDefined[[+]] $Parameter_readonly[[auto_type]]
894 $Operator_userDefined[[+]] $Parameter_readonly[[explicit_type]];
897 // Explicit template specialization
899 struct $Class_def[[Base]]{};
900 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
901 struct $Class_def[[S]] : public $Class[[Base]] {};
902 template $Bracket[[<]]$Bracket[[>]]
903 struct $Class_def[[S]]$Bracket[[<]]void$Bracket[[>]] : public $Class[[Base]] {};
905 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
906 $TemplateParameter[[T]] $Variable_def[[x]] = {};
907 template $Bracket[[<]]$Bracket[[>]]
908 int $Variable_def[[x]]$Bracket[[<]]int$Bracket[[>]] = (int)sizeof($Class[[Base]]);
910 // operator calls in template
912 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] class $Class_def[[C]] {
913 bool $Method_def[[compare]]($TemplateParameter[[T]] $Parameter_def[[t1]], $TemplateParameter[[T]] $Parameter_def[[t2]]) { return $Parameter[[t1]] $Operator_userDefined[[==]] $Parameter[[t2]]; }
914 $TemplateParameter[[T]] $Method_def[[deref]]($TemplateParameter[[T]] *$Parameter_def[[t]]) { return $Operator_userDefined[[*]]$Parameter[[t]]; }
919 struct $Class_def[[S]] { int *$Field_decl[[a]]; };
920 void $Function_def[[f]]() {
921 $Class[[S]] *$LocalVariable_def[[s]] = $Operator[[new]] $Class[[S]];
922 $LocalVariable[[s]]->$Field[[a]] $Operator[[=]] $Operator[[new]] int[10];
923 $Operator[[delete]][] $LocalVariable[[s]]->$Field[[a]];
924 $Operator[[delete]] $LocalVariable[[s]];
927 // explicit operator invocation
929 struct $Class_def[[S]] {
930 $Class[[S]] operator$Operator_decl[[+]](const $Class[[S]] &$Parameter_def_readonly[[other]]);
932 void $Function_def[[f]]() {
933 $Class[[S]] $LocalVariable_def[[s]];
934 $Class[[S]] $LocalVariable_def[[s2]] = $LocalVariable[[s]].operator$Operator_userDefined[[+]]($LocalVariable[[s]]);
938 // Brackets support: C++ casts
939 void $Function_def[[f]]() {
940 struct $Class_def[[B]] { virtual ~$Class_decl_constrDestr[[B]](); };
941 struct $Class_def[[D]] : public $Class[[B]] {};
942 $Class[[B]] $LocalVariable_def[[b]];
943 int $LocalVariable_def[[i]] = static_cast$Bracket[[<]]int$Bracket[[>]](3.5);
944 void *$LocalVariable_def[[p]] = reinterpret_cast$Bracket[[<]]void *$Bracket[[>]](0);
945 $Class[[D]] &$LocalVariable_def[[d]] = dynamic_cast$Bracket[[<]]$Class[[D]] &$Bracket[[>]]($LocalVariable[[b]]);
948 // Brackets support: Nested template instantiations.
950 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] struct $Class_def[[S]] {};
951 void $Function_def[[f]]() {
952 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]$Bracket[[>]] $LocalVariable_def[[s1]];
953 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]\
954 $Bracket[[>]] $LocalVariable_def[[s2]];
955 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]$Bracket[[>]]$Bracket[[>]] $LocalVariable_def[[s3]];
956 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]\
957 $Bracket[[>]]$Bracket[[>]] $LocalVariable_def[[s4]];
958 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]\
959 $Bracket[[>]]$Bracket[[>]] $LocalVariable_def[[s5]];
960 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]\
961 $Bracket[[>]]$Bracket[[>]] $LocalVariable_def[[s6]];
962 $Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]$Class[[S]]$Bracket[[<]]int$Bracket[[>]]$Bracket[[>]]\
963 $Bracket[[>]] $LocalVariable_def[[s7]];
966 // Brackets support: One of the brackets is a macro.
968 #define $Macro_decl[[LESS]] <
969 template $Macro[[LESS]] typename $TemplateParameter_def[[T]] > class $Class_def[[A]] {};
971 // Brackets support: Specializations
973 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] class $Class_def[[S]] {
975 template $Bracket[[<]]typename $TemplateParameter_def[[U]]$Bracket[[>]] class $Class_decl[[Nested]];
977 template $Bracket[[<]]$Bracket[[>]] class $Class_def[[S]]$Bracket[[<]]int$Bracket[[>]] {};
978 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
979 class $Class_def[[S]]$Bracket[[<]]$TemplateParameter[[T]]*$Bracket[[>]] {};
980 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
981 template $Bracket[[<]]typename $TemplateParameter_def[[U]]$Bracket[[>]]
982 class $Class[[S]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]::$Class_def[[Nested]] {};
983 template $Bracket[[<]]$Bracket[[>]]
984 template $Bracket[[<]]$Bracket[[>]]
985 class $Class[[S]]$Bracket[[<]]float$Bracket[[>]]::$Class_def[[Nested]]$Bracket[[<]]float$Bracket[[>]] {};
986 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] void $Function_decl[[foo]]();
987 void $Function_def[[bar]]() {
988 $Function[[foo]]$Bracket[[<]]int$Bracket[[>]]();
990 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] constexpr int $Variable_def_readonly[[V]] = 42;
991 constexpr int $Variable_def_readonly[[Y]] = $Variable_readonly[[V]]$Bracket[[<]]char$Bracket[[>]];
992 template $Bracket[[<]]$Bracket[[>]]
993 constexpr int $Variable_def_readonly[[V]]$Bracket[[<]]int$Bracket[[>]] = 5;
994 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
995 constexpr int $Variable_def_readonly[[V]]$Bracket[[<]]$TemplateParameter[[T]]*$Bracket[[>]] = 6;
996 template $Bracket[[<]]typename$Bracket[[>]]
997 class $Class_def[[A]] {
998 enum class $Enum_decl[[E]];
1000 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
1001 enum class $Class[[A]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]::$Enum_decl[[E]] {$EnumConstant_decl_readonly[[X]], $EnumConstant_decl_readonly[[Y]], $EnumConstant_decl_readonly[[Z]]};
1002 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
1003 class $Class_def[[B]] {
1004 template $Bracket[[<]]class $TemplateParameter_def[[U]]$Bracket[[>]] void $Method_def[[foo]]($TemplateParameter[[U]]) { }
1005 template$Bracket[[<]]$Bracket[[>]] void $Method_def[[foo]]$Bracket[[<]]int$Bracket[[>]](int) { }
1006 friend void $Function_decl[[foo]]$Bracket[[<]]$Bracket[[>]]($TemplateParameter[[T]]);
1009 // Brackets support: Function calls
1011 template $Bracket[[<]]typename$Bracket[[>]] void $Function_decl[[free]]();
1012 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
1013 struct $Class_def[[A]] {
1014 template $Bracket[[<]]typename$Bracket[[>]] void $Method_decl[[mem]]();
1016 void $Function_def[[foo]]() {
1017 $Class[[A]]$Bracket[[<]]int$Bracket[[>]] $LocalVariable_def[[a]];
1018 $LocalVariable[[a]].$Method[[mem]]$Bracket[[<]]int$Bracket[[>]]();
1020 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
1021 void $Function_def[[bar]]() {
1022 $Function[[free]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]();
1023 $Class[[A]]$Bracket[[<]]int$Bracket[[>]] $LocalVariable_def[[a]];
1024 $LocalVariable[[a]].$Method[[mem]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]();
1025 $Class[[A]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]] $LocalVariable_def[[b]];
1026 $LocalVariable[[b]].template $Method_dependentName[[mem]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]();
1029 // Brackets support: Concepts
1031 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
1032 concept $Concept_decl[[C]] = true;
1033 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] requires $Concept[[C]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]
1034 class $Class_def[[Z]] {};
1035 template $Bracket[[<]]typename, typename$Bracket[[>]]
1036 concept $Concept_decl[[C2]] = true;
1037 template $Bracket[[<]]$Concept[[C2]]$Bracket[[<]]int$Bracket[[>]] $TemplateParameter_def[[A]]$Bracket[[>]]
1038 class $Class_def[[B]] {};
1042 bool $Function_def[[funcWithGoto]](bool $Parameter_def[[b]]) {
1043 if ($Parameter[[b]])
1044 goto $Label[[return_true]];
1046 $Label_decl[[return_true]]:
1052 struct $Class_def[[Foo]] {
1053 void $Method_decl[[foo]]();
1056 void $Function_def[[s]]($Class[[Foo]] $Parameter_def[[f]]) {
1057 auto $LocalVariable_def[[k]] = $Operator[[&]]$Class[[Foo]]::$Method[[foo]];
1058 ($Parameter[[f]]$Operator[[.*]]$LocalVariable[[k]])(); // no crash on VisitCXXMemberCallExpr
1062 template$Bracket[[<]]typename$Bracket[[>]]
1063 class $Class_def[[Foo]] {};
1065 template$Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]]
1066 void $Function_def[[k]]() {
1067 auto $LocalVariable_def[[s]] = $Operator[[new]] $Class[[Foo]]$Bracket[[<]]$TemplateParameter[[T]]$Bracket[[>]]();
1068 $Operator[[delete]] $LocalVariable[[s]];
1071 // Recursive UsingValueDecl
1073 template $Bracket[[<]]int$Bracket[[>]] class $Class_def[[X]] {
1074 template $Bracket[[<]]int$Bracket[[>]] class $Class_def[[Y]] {
1075 using $Class[[Y]]$Bracket[[<]]0$Bracket[[>]]::$Unknown_dependentName[[xxx]];
1079 for (const auto &TestCase
: TestCases
)
1080 // Mask off scope modifiers to keep the tests manageable.
1081 // They're tested separately.
1082 checkHighlightings(TestCase
, {}, ~ScopeModifierMask
);
1084 checkHighlightings(R
"cpp(
1085 class $Class_def[[A]] {
1093 ~ScopeModifierMask
);
1095 // A separate test for macros in headers.
1096 checkHighlightings(R
"cpp(
1099 $Macro[[DXYZ_Y]](A);
1102 #define DXYZ(X) class X {};
1103 #define DXYZ_Y(Y) DXYZ(x##Y)
1104 #define DEFINE(X) int X;
1105 #define DEFINE_Y DEFINE(Y)
1107 ~ScopeModifierMask
);
1109 checkHighlightings(R
"cpp(
1110 #include "SYSObject
.h
"
1111 @interface $Class_defaultLibrary[[SYSObject]] ($Namespace_def[[UserCategory]])
1112 @property(nonatomic, readonly) int $Field_decl_readonly[[user_property]];
1114 int $Function_def[[somethingUsingSystemSymbols]]() {
1115 $Class_defaultLibrary[[SYSObject]] *$LocalVariable_def[[obj]] = [$Class_defaultLibrary[[SYSObject]] $StaticMethod_static_defaultLibrary[[new]]];
1116 return $LocalVariable[[obj]].$Field_defaultLibrary[[value]] $Operator[[+]] $LocalVariable[[obj]].$Field_readonly[[user_property]];
1119 {{"SystemSDK/SYSObject.h", R
"cpp(
1120 @interface SYSObject
1121 @property(nonatomic, assign) int value;
1122 + (instancetype)new;
1125 ~ScopeModifierMask
, {"-isystemSystemSDK/"});
1128 TEST(SemanticHighlighting
, ScopeModifiers
) {
1129 const char *TestCases
[] = {
1131 static int $Variable_fileScope[[x]];
1132 namespace $Namespace_globalScope[[ns]] {
1133 class $Class_globalScope[[x]];
1136 void $Function_fileScope[[foo]]();
1140 void $Function_globalScope[[foo]](int $Parameter_functionScope[[y]]) {
1141 int $LocalVariable_functionScope[[z]];
1145 // Lambdas are considered functions, not classes.
1146 auto $Variable_fileScope[[x]] = [$LocalVariable_functionScope[[m]](42)] {
1147 return $LocalVariable_functionScope[[m]];
1151 // Classes in functions are classes.
1152 void $Function_globalScope[[foo]]() {
1153 class $Class_functionScope[[X]] {
1154 int $Field_classScope[[x]];
1159 template $Bracket[[<]]int $TemplateParameter_classScope[[T]]$Bracket[[>]]
1160 class $Class_globalScope[[X]] {
1164 // No useful scope for template parameters of variable templates.
1165 template $Bracket[[<]]typename $TemplateParameter[[A]]$Bracket[[>]]
1166 unsigned $Variable_globalScope[[X]] =
1167 $TemplateParameter[[A]]::$Unknown_classScope[[x]];
1170 #define $Macro_globalScope[[X]] 1
1171 int $Variable_globalScope[[Y]] = $Macro_globalScope[[X]];
1175 for (const char *Test
: TestCases
)
1176 checkHighlightings(Test
, {}, ScopeModifierMask
);
1179 // Ranges are highlighted as variables, unless highlighted as $Function etc.
1180 std::vector
<HighlightingToken
> tokens(llvm::StringRef MarkedText
) {
1181 Annotations
A(MarkedText
);
1182 std::vector
<HighlightingToken
> Results
;
1183 for (const Range
& R
: A
.ranges())
1184 Results
.push_back({HighlightingKind::Variable
, 0, R
});
1185 for (unsigned I
= 0; I
< static_cast<unsigned>(HighlightingKind::LastKind
); ++I
) {
1186 HighlightingKind Kind
= static_cast<HighlightingKind
>(I
);
1187 for (const Range
& R
: A
.ranges(llvm::to_string(Kind
)))
1188 Results
.push_back({Kind
, 0, R
});
1190 llvm::sort(Results
);
1194 TEST(SemanticHighlighting
, toSemanticTokens
) {
1195 auto Tokens
= tokens(R
"(
1198 $Function[[big]] [[bang]]
1200 Tokens
.front().Modifiers
|= unsigned(HighlightingModifier::Declaration
);
1201 Tokens
.front().Modifiers
|= unsigned(HighlightingModifier::Readonly
);
1202 auto Results
= toSemanticTokens(Tokens
, /*Code=*/"");
1204 ASSERT_THAT(Results
, SizeIs(3));
1205 EXPECT_EQ(Results
[0].tokenType
, unsigned(HighlightingKind::Variable
));
1206 EXPECT_EQ(Results
[0].tokenModifiers
,
1207 unsigned(HighlightingModifier::Declaration
) |
1208 unsigned(HighlightingModifier::Readonly
));
1209 EXPECT_EQ(Results
[0].deltaLine
, 1u);
1210 EXPECT_EQ(Results
[0].deltaStart
, 1u);
1211 EXPECT_EQ(Results
[0].length
, 4u);
1213 EXPECT_EQ(Results
[1].tokenType
, unsigned(HighlightingKind::Function
));
1214 EXPECT_EQ(Results
[1].tokenModifiers
, 0u);
1215 EXPECT_EQ(Results
[1].deltaLine
, 2u);
1216 EXPECT_EQ(Results
[1].deltaStart
, 4u);
1217 EXPECT_EQ(Results
[1].length
, 3u);
1219 EXPECT_EQ(Results
[2].tokenType
, unsigned(HighlightingKind::Variable
));
1220 EXPECT_EQ(Results
[1].tokenModifiers
, 0u);
1221 EXPECT_EQ(Results
[2].deltaLine
, 0u);
1222 EXPECT_EQ(Results
[2].deltaStart
, 4u);
1223 EXPECT_EQ(Results
[2].length
, 4u);
1226 TEST(SemanticHighlighting
, diffSemanticTokens
) {
1227 auto Before
= toSemanticTokens(tokens(R
"(
1228 [[foo]] [[bar]] [[baz]]
1229 [[one]] [[two]] [[three]]
1232 EXPECT_THAT(diffTokens(Before
, Before
), IsEmpty());
1234 auto After
= toSemanticTokens(tokens(R
"(
1235 [[foo]] [[hello]] [[world]] [[baz]]
1236 [[one]] [[two]] [[three]]
1240 // Replace [bar, baz] with [hello, world, baz]
1241 auto Diff
= diffTokens(Before
, After
);
1242 ASSERT_THAT(Diff
, SizeIs(1));
1243 EXPECT_EQ(1u, Diff
.front().startToken
);
1244 EXPECT_EQ(2u, Diff
.front().deleteTokens
);
1245 ASSERT_THAT(Diff
.front().tokens
, SizeIs(3));
1247 EXPECT_EQ(0u, Diff
.front().tokens
[0].deltaLine
);
1248 EXPECT_EQ(4u, Diff
.front().tokens
[0].deltaStart
);
1249 EXPECT_EQ(5u, Diff
.front().tokens
[0].length
);
1251 EXPECT_EQ(0u, Diff
.front().tokens
[1].deltaLine
);
1252 EXPECT_EQ(6u, Diff
.front().tokens
[1].deltaStart
);
1253 EXPECT_EQ(5u, Diff
.front().tokens
[1].length
);
1255 EXPECT_EQ(0u, Diff
.front().tokens
[2].deltaLine
);
1256 EXPECT_EQ(6u, Diff
.front().tokens
[2].deltaStart
);
1257 EXPECT_EQ(3u, Diff
.front().tokens
[2].length
);
1260 TEST(SemanticHighlighting
, MultilineTokens
) {
1261 llvm::StringRef AnnotatedCode
= R
"cpp(
1265 auto Toks
= toSemanticTokens(tokens(AnnotatedCode
),
1266 Annotations(AnnotatedCode
).code());
1267 ASSERT_THAT(Toks
, SizeIs(4));
1269 EXPECT_EQ(Toks
[0].deltaLine
, 1u);
1270 EXPECT_EQ(Toks
[0].deltaStart
, 2u);
1271 EXPECT_EQ(Toks
[0].length
, 2u);
1272 EXPECT_EQ(Toks
[1].deltaLine
, 1u);
1273 EXPECT_EQ(Toks
[1].deltaStart
, 0u);
1274 EXPECT_EQ(Toks
[1].length
, 1u);
1275 EXPECT_EQ(Toks
[2].deltaLine
, 1u);
1276 EXPECT_EQ(Toks
[2].deltaStart
, 0u);
1277 EXPECT_EQ(Toks
[2].length
, 1u);
1280 EXPECT_EQ(Toks
[3].deltaLine
, 0u);
1281 EXPECT_EQ(Toks
[3].deltaStart
, 2u);
1282 EXPECT_EQ(Toks
[3].length
, 3u);
1285 TEST(SemanticHighlighting
, WithHighlightingFilter
) {
1286 llvm::StringRef AnnotatedCode
= R
"cpp(
1287 int *$Variable[[x]] = new int;
1290 Cfg
.SemanticTokens
.DisabledKinds
= {"Operator"};
1291 Cfg
.SemanticTokens
.DisabledModifiers
= {"Declaration", "Definition"};
1292 WithContextValue
WithCfg(Config::Key
, std::move(Cfg
));
1293 checkHighlightings(AnnotatedCode
, {}, ~ScopeModifierMask
);
1296 } // namespace clangd
1297 } // namespace clang