[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / unittests / SemanticHighlightingTests.cpp
blob6eee6ec30ac024b3ad32e5b1b09a999d41402de3
1 //==- SemanticHighlightingTests.cpp - SemanticHighlighting tests-*- C++ -* -==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "Annotations.h"
10 #include "Config.h"
11 #include "Protocol.h"
12 #include "SemanticHighlighting.h"
13 #include "SourceCode.h"
14 #include "TestTU.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"
22 #include <algorithm>
24 namespace clang {
25 namespace clangd {
26 namespace {
28 using testing::IsEmpty;
29 using testing::SizeIs;
31 /// Annotates the input code with provided semantic highlightings. Results look
32 /// something like:
33 /// class $Class[[X]] {
34 /// $Primitive[[int]] $Field[[a]] = 0;
35 /// };
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;
41 }));
43 std::string Buf;
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);
52 bool hasDef =
53 T.Modifiers & (1 << uint32_t(HighlightingModifier::Definition));
54 bool hasDecl =
55 T.Modifiers & (1 << uint32_t(HighlightingModifier::Declaration));
56 EXPECT_TRUE(!hasDef || hasDecl);
58 OS << Input.substr(NextChar, StartOffset - NextChar);
59 OS << '$' << T.Kind;
60 for (unsigned I = 0;
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) << "]]";
69 NextChar = EndOffset;
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>>
78 AdditionalFiles = {},
79 uint32_t ModifierMask = -1,
80 std::vector<std::string> AdditionalArgs = {}) {
81 Annotations Test(Code);
82 TestTU TU;
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();
93 auto Actual =
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[] = {
109 R"cpp(
110 struct $Class_def[[AS]] {
111 double $Field_decl[[SomeMember]];
113 struct {
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[[)]];
122 )cpp",
123 R"cpp(
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]]();
132 )cpp",
133 R"cpp(
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]]();
157 $Class[[B]]();
159 )cpp",
160 R"cpp(
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]];
174 )cpp",
175 R"cpp(
176 namespace $Namespace_decl[[abc]] {
177 namespace {}
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]];
197 )cpp",
198 R"cpp(
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]]();
211 $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;
223 )cpp",
224 R"cpp(
225 struct $Class_def[[AA]] {
226 int $Field_decl[[A]];
228 int $Variable_def[[B]];
229 $Class[[AA]] $Variable_def[[A]]{$Variable[[B]]};
230 )cpp",
231 R"cpp(
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]];
246 )cpp",
247 R"cpp(
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]] ...);
272 )cpp",
273 R"cpp(
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[[>]];
278 )cpp",
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.
282 R"cpp(
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]];
295 )cpp",
296 R"cpp(
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;
304 )cpp",
305 R"cpp(
306 enum $Enum_decl[[En]] {
307 $EnumConstant_decl_readonly[[EC]],
309 class $Class_def[[Foo]] {};
310 class $Class_def[[Bar]] {
311 public:
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]]),
318 $Field[[I]] (123) {}
320 class $Class_def[[Bar2]] : public $Class[[Bar]] {
321 $Class_def_constrDestr[[Bar2]]() : $Class[[Bar]]($Class[[Foo]](), $EnumConstant_readonly[[EC]]) {}
323 )cpp",
324 R"cpp(
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{};
337 )cpp",
338 R"cpp(
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]];
364 )cpp",
365 R"cpp(
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]];
391 )cpp",
392 // Tokens that share a source range but have conflicting Kinds are not
393 // highlighted.
394 R"cpp(
395 #define $Macro_decl[[DEF_MULTIPLE]](X) namespace X { class X { int X; }; }
396 #define $Macro_decl[[DEF_CLASS]](T) class T {};
397 // Preamble ends.
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]]);
433 )cpp",
434 R"cpp(
435 #define $Macro_decl[[fail]](expr) expr
436 #define $Macro_decl[[assert]](COND) if (!(COND)) { $Macro[[fail]]("assertion failed" #COND); }
437 // Preamble ends.
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]]());
445 )cpp",
446 // highlighting all macro references
447 R"cpp(
448 #ifndef $Macro[[name]]
449 #define $Macro_decl[[name]]
450 #endif
452 #define $Macro_decl[[test]]
453 #undef $Macro[[test]]
454 #ifdef $Macro[[test]]
455 #endif
457 #if defined($Macro[[test]])
458 #endif
459 )cpp",
460 R"cpp(
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[[++]];
475 )cpp",
476 R"cpp(
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]]);
494 )cpp",
495 R"cpp(
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]]);
503 )cpp",
504 R"cpp(
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]]);
514 )cpp",
515 R"cpp(
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]]());
527 )cpp",
528 R"cpp(
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]]);
532 )cpp",
533 R"cpp(
534 template $Bracket[[<]]class $TemplateParameter_def[[T]]$Bracket[[>]]
535 void $Function_def[[foo]]($TemplateParameter[[T]] $Parameter_def[[P]]) {
536 $Parameter[[P]].$Unknown_dependentName[[Field]];
538 )cpp",
539 R"cpp(
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]];
546 )cpp",
547 // Highlighting the using decl as the underlying using shadow decl.
548 R"cpp(
549 void $Function_decl[[foo]]();
550 using ::$Function[[foo]];
551 )cpp",
552 // Highlighting of template template arguments.
553 R"cpp(
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[[>]]
558 *$Field_decl[[t]];
560 )cpp",
561 // Inactive code highlighting
562 R"cpp(
563 // Code in the preamble.
564 // Inactive lines get an empty InactiveCode token at the beginning.
565 #ifdef $Macro[[test]]
566 $InactiveCode[[int Inactive1;]]
567 #endif
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]];
580 #else
581 $InactiveCode[[int Inactive3;]]
582 #endif
584 #ifndef $Macro[[test]]
585 int $Variable_def[[Active2]];
586 #endif
588 #ifdef $Macro[[test]]
589 $InactiveCode[[int Inactive4;]]
590 #else
591 int $Variable_def[[Active3]];
592 #endif
593 )cpp",
594 // Argument to 'sizeof...'
595 R"cpp(
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]]);
601 )cpp",
602 // More dependent types
603 R"cpp(
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]];
617 )cpp",
618 // Dependent name with heuristic target
619 R"cpp(
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]]();
638 )cpp",
639 // Concepts
640 R"cpp(
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]]() {}
660 )cpp",
661 // Dependent template name
662 R"cpp(
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]]
667 $Bracket[[>]];
668 )cpp",
669 R"cpp(
670 class $Class_def_abstract[[Abstract]] {
671 public:
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]]();
679 )cpp",
680 R"cpp(
681 <:[deprecated]:> int $Variable_def_deprecated[[x]];
682 )cpp",
683 R"cpp(
684 // ObjC: Classes and methods
685 @class $Class_decl[[Forward]];
687 @interface $Class_def[[Foo]]
688 @end
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]];
693 @end
694 @implementation $Class_def[[Bar]]
695 -(id) $Method_def[[x]]:(int)$Parameter_def[[a]] $Method_def[[y]]:(int)$Parameter_def[[b]] {
696 return self;
698 +(instancetype)$StaticMethod_def_static[[sharedInstance]] { return 0; }
699 +(void) $StaticMethod_def_static[[explode]] {}
700 @end
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]]];
706 )cpp",
707 R"cpp(
708 // ObjC: Protocols
709 @protocol $Interface_def[[Protocol]]
710 @end
711 @protocol $Interface_def[[Protocol2]] <$Interface[[Protocol]]>
712 @end
713 @interface $Class_def[[Klass]] <$Interface[[Protocol]]>
714 @end
715 id<$Interface[[Protocol]]> $Variable_def[[x]];
716 )cpp",
717 R"cpp(
718 // ObjC: Categories
719 @interface $Class_def[[Foo]]
720 @end
721 @interface $Class[[Foo]]($Namespace_def[[Bar]])
722 @end
723 @implementation $Class[[Foo]]($Namespace_def[[Bar]])
724 @end
725 )cpp",
726 R"cpp(
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]];
733 @end
734 @implementation $Class_def[[Foo]]
735 @synthesize someProperty = _someProperty;
736 - (int)$Method_def[[otherMethod]] {
737 return 0;
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;
744 @end
745 )cpp",
746 // Member imported from dependent base
747 R"cpp(
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]];
759 )cpp",
760 // Modifier for variables passed as non-const references
761 R"cpp(
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,
787 int*, const int*,
788 int&, const int&,
789 int*&, const int*&, const int* const &,
790 int**, int**&, int** const &,
791 int = 123) {
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]]);
834 )cpp",
835 // init-captures
836 R"cpp(
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[[)]];
842 )cpp",
843 // Enum base specifier
844 R"cpp(
845 using $Primitive_decl[[MyTypedef]] = int;
846 enum $Enum_decl[[MyEnum]] : $Primitive[[MyTypedef]] {};
847 )cpp",
848 // Enum base specifier
849 R"cpp(
850 typedef int $Primitive_decl[[MyTypedef]];
851 enum $Enum_decl[[MyEnum]] : $Primitive[[MyTypedef]] {};
852 )cpp",
853 // Using enum
854 R"cpp(
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]];
860 )cpp",
861 // Issue 1096
862 R"cpp(
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]]);
873 )cpp",
874 // Predefined identifiers
875 R"cpp(
876 void $Function_def[[Foo]]() {
877 const char *$LocalVariable_def_readonly[[s]] = $LocalVariable_readonly_static[[__func__]];
879 )cpp",
880 // override and final
881 R"cpp(
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]]; };
885 )cpp",
886 // Issue 1222: readonly modifier for generic parameter
887 R"cpp(
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]];
896 )cpp",
897 // Explicit template specialization
898 R"cpp(
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]]);
909 )cpp",
910 // operator calls in template
911 R"cpp(
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]]; }
916 )cpp",
917 // new and delete
918 R"cpp(
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]];
926 )cpp",
927 // explicit operator invocation
928 R"cpp(
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]]);
936 )cpp",
937 R"cpp(
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]]);
947 )cpp",
948 // Brackets support: Nested template instantiations.
949 R"cpp(
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]];
965 )cpp",
966 // Brackets support: One of the brackets is a macro.
967 R"cpp(
968 #define $Macro_decl[[LESS]] <
969 template $Macro[[LESS]] typename $TemplateParameter_def[[T]] > class $Class_def[[A]] {};
970 )cpp",
971 // Brackets support: Specializations
972 R"cpp(
973 template $Bracket[[<]]typename $TemplateParameter_def[[T]]$Bracket[[>]] class $Class_def[[S]] {
974 public:
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]]);
1008 )cpp",
1009 // Brackets support: Function calls
1010 R"cpp(
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[[>]]();
1028 )cpp",
1029 // Brackets support: Concepts
1030 R"cpp(
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]] {};
1039 )cpp",
1040 // Labels
1041 R"cpp(
1042 bool $Function_def[[funcWithGoto]](bool $Parameter_def[[b]]) {
1043 if ($Parameter[[b]])
1044 goto $Label[[return_true]];
1045 return false;
1046 $Label_decl[[return_true]]:
1047 return true;
1049 )cpp",
1050 // no crash
1051 R"cpp(
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
1060 )cpp",
1061 R"cpp(
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]];
1070 )cpp",
1071 // Recursive UsingValueDecl
1072 R"cpp(
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]];
1078 )cpp"};
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]] {
1086 #include "imp.h"
1088 )cpp",
1089 {{"imp.h", R"cpp(
1090 int someMethod();
1091 void otherMethod();
1092 )cpp"}},
1093 ~ScopeModifierMask);
1095 // A separate test for macros in headers.
1096 checkHighlightings(R"cpp(
1097 #include "imp.h"
1098 $Macro[[DEFINE_Y]]
1099 $Macro[[DXYZ_Y]](A);
1100 )cpp",
1101 {{"imp.h", R"cpp(
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)
1106 )cpp"}},
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]];
1113 @end
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]];
1118 )cpp",
1119 {{"SystemSDK/SYSObject.h", R"cpp(
1120 @interface SYSObject
1121 @property(nonatomic, assign) int value;
1122 + (instancetype)new;
1123 @end
1124 )cpp"}},
1125 ~ScopeModifierMask, {"-isystemSystemSDK/"});
1128 TEST(SemanticHighlighting, ScopeModifiers) {
1129 const char *TestCases[] = {
1130 R"cpp(
1131 static int $Variable_fileScope[[x]];
1132 namespace $Namespace_globalScope[[ns]] {
1133 class $Class_globalScope[[x]];
1135 namespace {
1136 void $Function_fileScope[[foo]]();
1138 )cpp",
1139 R"cpp(
1140 void $Function_globalScope[[foo]](int $Parameter_functionScope[[y]]) {
1141 int $LocalVariable_functionScope[[z]];
1143 )cpp",
1144 R"cpp(
1145 // Lambdas are considered functions, not classes.
1146 auto $Variable_fileScope[[x]] = [$LocalVariable_functionScope[[m]](42)] {
1147 return $LocalVariable_functionScope[[m]];
1149 )cpp",
1150 R"cpp(
1151 // Classes in functions are classes.
1152 void $Function_globalScope[[foo]]() {
1153 class $Class_functionScope[[X]] {
1154 int $Field_classScope[[x]];
1157 )cpp",
1158 R"cpp(
1159 template $Bracket[[<]]int $TemplateParameter_classScope[[T]]$Bracket[[>]]
1160 class $Class_globalScope[[X]] {
1162 )cpp",
1163 R"cpp(
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]];
1168 )cpp",
1169 R"cpp(
1170 #define $Macro_globalScope[[X]] 1
1171 int $Variable_globalScope[[Y]] = $Macro_globalScope[[X]];
1172 )cpp",
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);
1191 return Results;
1194 TEST(SemanticHighlighting, toSemanticTokens) {
1195 auto Tokens = tokens(R"(
1196 [[blah]]
1198 $Function[[big]] [[bang]]
1199 )");
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]]
1230 )"),
1231 /*Code=*/"");
1232 EXPECT_THAT(diffTokens(Before, Before), IsEmpty());
1234 auto After = toSemanticTokens(tokens(R"(
1235 [[foo]] [[hello]] [[world]] [[baz]]
1236 [[one]] [[two]] [[three]]
1237 )"),
1238 /*Code=*/"");
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));
1246 // hello
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);
1250 // world
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);
1254 // baz
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(
1262 [[fo
1264 o]] [[bar]])cpp";
1265 auto Toks = toSemanticTokens(tokens(AnnotatedCode),
1266 Annotations(AnnotatedCode).code());
1267 ASSERT_THAT(Toks, SizeIs(4));
1268 // foo
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);
1279 // bar
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;
1288 )cpp";
1289 Config Cfg;
1290 Cfg.SemanticTokens.DisabledKinds = {"Operator"};
1291 Cfg.SemanticTokens.DisabledModifiers = {"Declaration", "Definition"};
1292 WithContextValue WithCfg(Config::Key, std::move(Cfg));
1293 checkHighlightings(AnnotatedCode, {}, ~ScopeModifierMask);
1295 } // namespace
1296 } // namespace clangd
1297 } // namespace clang