[GlobalISel] Add G_ABDS and G_ABDU instructions (#118122)
[llvm-project.git] / clang / unittests / Format / TokenAnnotatorTest.cpp
blob9db3187ac44e70ff7a8c8ec0d80dc9cbc655c970
1 //===- unittest/Format/TokenAnnotatorTest.cpp - Formatting unit tests -----===//
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 "clang/Format/Format.h"
11 #include "FormatTestUtils.h"
12 #include "TestLexer.h"
13 #include "gtest/gtest.h"
15 namespace clang {
16 namespace format {
18 // Not really the equality, but everything we need.
19 static bool operator==(const FormatToken &LHS,
20 const FormatToken &RHS) noexcept {
21 return LHS.Tok.getKind() == RHS.Tok.getKind() &&
22 LHS.getType() == RHS.getType();
25 namespace {
27 class TokenAnnotatorTest : public testing::Test {
28 protected:
29 TokenList annotate(StringRef Code,
30 const FormatStyle &Style = getLLVMStyle()) {
31 return TestLexer(Allocator, Buffers, Style).annotate(Code);
33 llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
34 std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
37 #define EXPECT_TOKEN_KIND(FormatTok, Kind) \
38 EXPECT_EQ((FormatTok)->Tok.getKind(), Kind) << *(FormatTok)
39 #define EXPECT_TOKEN_TYPE(FormatTok, Type) \
40 EXPECT_EQ((FormatTok)->getType(), Type) << *(FormatTok)
41 #define EXPECT_TOKEN_PRECEDENCE(FormatTok, Prec) \
42 EXPECT_EQ((FormatTok)->getPrecedence(), Prec) << *(FormatTok)
43 #define EXPECT_BRACE_KIND(FormatTok, Kind) \
44 EXPECT_EQ(FormatTok->getBlockKind(), Kind) << *(FormatTok)
45 #define EXPECT_SPLIT_PENALTY(FormatTok, Penalty) \
46 EXPECT_EQ(FormatTok->SplitPenalty, Penalty) << *(FormatTok)
47 #define EXPECT_TOKEN(FormatTok, Kind, Type) \
48 do { \
49 EXPECT_TOKEN_KIND(FormatTok, Kind); \
50 EXPECT_TOKEN_TYPE(FormatTok, Type); \
51 } while (false)
53 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
54 auto Tokens = annotate("auto x = [](const decltype(x) &ptr) {};");
55 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
56 EXPECT_TOKEN(Tokens[7], tok::kw_decltype, TT_Unknown);
57 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_TypeDeclarationParen);
58 EXPECT_TOKEN(Tokens[9], tok::identifier, TT_Unknown);
59 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_TypeDeclarationParen);
60 EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
62 Tokens = annotate("auto x = [](const decltype(x) *ptr) {};");
63 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
64 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_TypeDeclarationParen);
65 EXPECT_TOKEN(Tokens[11], tok::star, TT_PointerOrReference);
67 Tokens = annotate("#define lambda [](const decltype(x) &ptr) {}");
68 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
69 EXPECT_TOKEN(Tokens[7], tok::kw_decltype, TT_Unknown);
70 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_TypeDeclarationParen);
71 EXPECT_TOKEN(Tokens[9], tok::identifier, TT_Unknown);
72 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_TypeDeclarationParen);
73 EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
75 Tokens = annotate("#define lambda [](const decltype(x) *ptr) {}");
76 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
77 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_TypeDeclarationParen);
78 EXPECT_TOKEN(Tokens[11], tok::star, TT_PointerOrReference);
80 Tokens = annotate("void f() {\n"
81 " while (p < a && *p == 'a')\n"
82 " p++;\n"
83 "}");
84 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
85 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_BinaryOperator);
86 EXPECT_TOKEN(Tokens[11], tok::star, TT_UnaryOperator);
88 Tokens = annotate("case *x:");
89 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
90 EXPECT_TOKEN(Tokens[1], tok::star, TT_UnaryOperator);
91 Tokens = annotate("case &x:");
92 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
93 EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);
95 Tokens = annotate("bool b = 3 == int{3} && true;");
96 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
97 EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);
99 Tokens = annotate("struct {\n"
100 "} *ptr;");
101 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
102 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
103 Tokens = annotate("union {\n"
104 "} *ptr;");
105 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
106 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
107 Tokens = annotate("class {\n"
108 "} *ptr;");
109 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
110 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
112 Tokens = annotate("struct {\n"
113 "} &&ptr = {};");
114 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
115 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
116 Tokens = annotate("union {\n"
117 "} &&ptr = {};");
118 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
119 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
120 Tokens = annotate("class {\n"
121 "} &&ptr = {};");
122 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
123 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
124 Tokens = annotate("int i = int{42} * 2;");
125 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
126 EXPECT_TOKEN(Tokens[7], tok::star, TT_BinaryOperator);
128 Tokens = annotate("delete[] *ptr;");
129 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
130 EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator);
131 Tokens = annotate("delete[] **ptr;");
132 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
133 EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator);
134 EXPECT_TOKEN(Tokens[4], tok::star, TT_UnaryOperator);
135 Tokens = annotate("delete[] *(ptr);");
136 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
137 EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator);
139 Tokens = annotate("void f() { void (*fnptr)(char* foo); }");
140 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
141 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen);
142 // FIXME: The star of a function pointer probably makes more sense as
143 // TT_PointerOrReference.
144 EXPECT_TOKEN(Tokens[7], tok::star, TT_UnaryOperator);
145 EXPECT_TOKEN(Tokens[12], tok::star, TT_PointerOrReference);
147 Tokens = annotate("void f() { void (*fnptr)(t* foo); }");
148 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
149 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen);
150 EXPECT_TOKEN(Tokens[7], tok::star, TT_UnaryOperator);
151 EXPECT_TOKEN(Tokens[12], tok::star, TT_PointerOrReference);
153 Tokens = annotate("int f3() { return sizeof(Foo&); }");
154 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
155 EXPECT_TOKEN(Tokens[9], tok::amp, TT_PointerOrReference);
157 Tokens = annotate("int f4() { return sizeof(Foo&&); }");
158 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
159 EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference);
161 Tokens = annotate("void f5() { int f6(Foo&, Bar&); }");
162 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
163 EXPECT_TOKEN(Tokens[9], tok::amp, TT_PointerOrReference);
164 EXPECT_TOKEN(Tokens[12], tok::amp, TT_PointerOrReference);
166 Tokens = annotate("void f7() { int f8(Foo&&, Bar&&); }");
167 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
168 EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference);
169 EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_PointerOrReference);
171 Tokens = annotate("Type1 &val1 = val2;");
172 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
173 EXPECT_TOKEN(Tokens[1], tok::amp, TT_PointerOrReference);
175 Tokens = annotate("Type1 *val1 = &val2;");
176 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
177 EXPECT_TOKEN(Tokens[1], tok::star, TT_PointerOrReference);
178 EXPECT_TOKEN(Tokens[4], tok::amp, TT_UnaryOperator);
180 Tokens = annotate("val1 & val2;");
181 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
182 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
184 Tokens = annotate("val1 & val2.member;");
185 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
186 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
188 Tokens = annotate("val1 & val2.*member;");
189 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
190 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
192 Tokens = annotate("val1.*member & val2;");
193 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
194 EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator);
196 Tokens = annotate("val1 & val2->*member;");
197 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
198 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
200 Tokens = annotate("val1->member & val2;");
201 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
202 EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator);
204 Tokens = annotate("val1 & val2 & val3;");
205 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
206 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
207 EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator);
209 Tokens = annotate("val1 & val2 // comment\n"
210 " & val3;");
211 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
212 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
213 EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator);
215 Tokens =
216 annotate("val1 & val2.member & val3.member() & val4 & val5->member;");
217 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
218 EXPECT_TOKEN(Tokens[1], tok::amp, TT_BinaryOperator);
219 EXPECT_TOKEN(Tokens[5], tok::amp, TT_BinaryOperator);
220 EXPECT_TOKEN(Tokens[11], tok::amp, TT_BinaryOperator);
221 EXPECT_TOKEN(Tokens[13], tok::amp, TT_BinaryOperator);
223 Tokens = annotate("class c {\n"
224 " void func(type &a) { a & member; }\n"
225 " anotherType &member;\n"
226 "}");
227 ASSERT_EQ(Tokens.size(), 22u) << Tokens;
228 EXPECT_TOKEN(Tokens[7], tok::amp, TT_PointerOrReference);
229 EXPECT_TOKEN(Tokens[12], tok::amp, TT_BinaryOperator);
230 EXPECT_TOKEN(Tokens[17], tok::amp, TT_PointerOrReference);
232 Tokens = annotate("struct S {\n"
233 " auto Mem = C & D;\n"
234 "}");
235 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
236 EXPECT_TOKEN(Tokens[7], tok::amp, TT_BinaryOperator);
238 Tokens =
239 annotate("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
240 ASSERT_EQ(Tokens.size(), 23u) << Tokens;
241 EXPECT_TOKEN(Tokens[15], tok::ampamp, TT_BinaryOperator);
243 Tokens = annotate("template <typename T> struct S {\n"
244 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
245 "};");
246 ASSERT_EQ(Tokens.size(), 30u) << Tokens;
247 EXPECT_TOKEN(Tokens[14], tok::ampamp, TT_BinaryOperator);
249 Tokens = annotate("template <bool B = C && D> struct S {};");
250 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
251 EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_BinaryOperator);
253 Tokens = annotate("template <typename T, bool B = C && D> struct S {};");
254 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
255 EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);
257 Tokens = annotate("template <typename T, typename U = T&&> struct S {};");
258 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
259 EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference);
261 Tokens = annotate("template <typename T = int (*)(int)> struct S {};");
262 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
263 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen);
264 EXPECT_TOKEN(Tokens[7], tok::star, TT_PointerOrReference);
266 Tokens = annotate("Foo<A && B> a = {};");
267 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
268 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
270 Tokens = annotate("Foo<A &&> a = {};");
271 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
272 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);
274 Tokens = annotate("template <typename T>\n"
275 "enable_if_t<is_integral_v<T>, bool> // comment\n"
276 "operator~(T &a);");
277 ASSERT_EQ(Tokens.size(), 24u) << Tokens;
278 EXPECT_TOKEN(Tokens[19], tok::amp, TT_PointerOrReference);
280 Tokens = annotate("template <enable_if_t<foo && !bar>* = nullptr> void f();");
281 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
282 EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
284 Tokens =
285 annotate("auto foo() noexcept(noexcept(bar()) && "
286 "trait<std::decay_t<decltype(bar())>> && noexcept(baz())) {}");
287 ASSERT_EQ(Tokens.size(), 38u) << Tokens;
288 EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);
289 EXPECT_TOKEN(Tokens[27], tok::ampamp, TT_BinaryOperator);
291 Tokens = annotate("foo = *i < *j && *j > *k;");
292 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
293 EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
294 EXPECT_TOKEN(Tokens[7], tok::ampamp, TT_BinaryOperator);
295 EXPECT_TOKEN(Tokens[10], tok::greater, TT_BinaryOperator);
297 Tokens = annotate("if (Foo *foo; bar)");
298 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
299 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
301 Tokens = annotate("if (Foo **foo(); bar)");
302 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
303 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
304 EXPECT_TOKEN(Tokens[4], tok::star, TT_PointerOrReference);
306 Tokens = annotate("if (Foo *&foo{a}; bar)");
307 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
308 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
309 EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
311 FormatStyle Style = getLLVMStyle();
312 Style.TypeNames.push_back("MYI");
313 Tokens = annotate("if (MYI *p{nullptr})", Style);
314 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
315 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
316 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
318 Style.TypeNames.push_back("Class");
319 Tokens = annotate("if (Class *obj {getObj()})", Style);
320 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
321 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
322 EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
324 Tokens = annotate("class Foo {\n"
325 " void operator<() {}\n"
326 " Foo &f;\n"
327 "};");
328 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
329 EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
330 EXPECT_TOKEN(Tokens[5], tok::less, TT_OverloadedOperator);
331 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
332 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
333 EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
335 Tokens = annotate("if (new && num) {\n"
336 " new = 1;\n"
337 "}\n"
338 "if (!delete && num) {\n"
339 " delete = 1;\n"
340 "}");
341 ASSERT_EQ(Tokens.size(), 26u) << Tokens;
342 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
343 EXPECT_TOKEN(Tokens[16], tok::ampamp, TT_BinaryOperator);
345 Tokens = annotate("#define FOO \\\n"
346 " void foo() { f(a * b); }");
347 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
348 EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator);
350 Tokens = annotate("for (int i; Foo *&foo : foos)");
351 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
352 EXPECT_TOKEN(Tokens[6], tok::star, TT_PointerOrReference);
353 EXPECT_TOKEN(Tokens[7], tok::amp, TT_PointerOrReference);
354 EXPECT_TOKEN(Tokens[9], tok::colon, TT_RangeBasedForLoopColon);
356 Tokens = annotate("#define FOO auto Foo = [] { f(a * b); };");
357 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
358 EXPECT_TOKEN(Tokens[12], tok::star, TT_BinaryOperator);
360 Tokens = annotate("namespace {\n"
361 "#define FOO(x) void foo(a##x *b);\n"
362 "}");
363 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
364 EXPECT_TOKEN(Tokens[14], tok::star, TT_PointerOrReference);
366 Tokens = annotate("Thingy kConfig = {\n"
367 " 1,\n"
368 " (uint16_t)(kScale * height_pixels),\n"
369 "};");
370 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
371 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_CastRParen);
372 EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator);
375 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
376 auto Tokens = annotate("x - 0");
377 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
378 EXPECT_TOKEN(Tokens[1], tok::minus, TT_BinaryOperator);
379 Tokens = annotate("0 + 0");
380 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
381 EXPECT_TOKEN(Tokens[1], tok::plus, TT_BinaryOperator);
382 Tokens = annotate("x + +0");
383 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
384 EXPECT_TOKEN(Tokens[2], tok::plus, TT_UnaryOperator);
385 Tokens = annotate("x ? -0 : +0");
386 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
387 EXPECT_TOKEN(Tokens[2], tok::minus, TT_UnaryOperator);
388 EXPECT_TOKEN(Tokens[5], tok::plus, TT_UnaryOperator);
389 Tokens = annotate("(-0)");
390 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
391 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
392 Tokens = annotate("0, -0");
393 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
394 EXPECT_TOKEN(Tokens[2], tok::minus, TT_UnaryOperator);
395 Tokens = annotate("for (; -1;) {\n}");
396 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
397 EXPECT_TOKEN(Tokens[3], tok::minus, TT_UnaryOperator);
398 Tokens = annotate("x = -1;");
399 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
400 EXPECT_TOKEN(Tokens[2], tok::minus, TT_UnaryOperator);
401 Tokens = annotate("x[-1]");
402 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
403 EXPECT_TOKEN(Tokens[2], tok::minus, TT_UnaryOperator);
404 Tokens = annotate("x = {-1};");
405 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
406 EXPECT_TOKEN(Tokens[3], tok::minus, TT_UnaryOperator);
407 Tokens = annotate("case -x:");
408 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
409 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
410 Tokens = annotate("co_await -x;");
411 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
412 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
413 Tokens = annotate("co_return -x;");
414 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
415 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
416 Tokens = annotate("co_yield -x;");
417 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
418 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
419 Tokens = annotate("delete -x;");
420 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
421 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
422 Tokens = annotate("return -x;");
423 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
424 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
425 Tokens = annotate("throw -x;");
426 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
427 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
428 Tokens = annotate("sizeof -x");
429 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
430 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
431 Tokens = annotate("co_await +x;");
432 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
433 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
434 Tokens = annotate("co_return +x;");
435 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
436 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
437 Tokens = annotate("co_yield +x;");
438 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
439 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
440 Tokens = annotate("delete +x;");
441 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
442 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
443 Tokens = annotate("return +x;");
444 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
445 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
446 Tokens = annotate("throw +x;");
447 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
448 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
449 Tokens = annotate("sizeof +x");
450 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
451 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
452 Tokens = annotate("(int)-x");
453 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
454 EXPECT_TOKEN(Tokens[3], tok::minus, TT_UnaryOperator);
455 Tokens = annotate("(-x)");
456 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
457 EXPECT_TOKEN(Tokens[1], tok::minus, TT_UnaryOperator);
458 Tokens = annotate("!+x");
459 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
460 EXPECT_TOKEN(Tokens[0], tok::exclaim, TT_UnaryOperator);
461 EXPECT_TOKEN(Tokens[1], tok::plus, TT_UnaryOperator);
464 TEST_F(TokenAnnotatorTest, UnderstandsClasses) {
465 auto Tokens = annotate("class C {};");
466 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
467 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_ClassLBrace);
468 EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_ClassRBrace);
470 Tokens = annotate("const class C {} c;");
471 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
472 EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_ClassLBrace);
473 EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_ClassRBrace);
475 Tokens = annotate("const class {} c;");
476 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
477 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_ClassLBrace);
478 EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_ClassRBrace);
480 Tokens = annotate("class [[deprecated(\"\")]] C { int i; };");
481 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
482 EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_ClassLBrace);
483 EXPECT_TOKEN(Tokens[14], tok::r_brace, TT_ClassRBrace);
486 TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
487 auto Tokens = annotate("struct S {};");
488 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
489 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
490 EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_StructRBrace);
492 Tokens = annotate("struct macro(a) S {};");
493 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
494 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
495 EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_StructRBrace);
497 Tokens = annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
498 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
499 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
500 EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_StructRBrace);
502 Tokens = annotate("struct [[deprecated]] [[nodiscard]] C { int i; };");
503 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
504 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_StructLBrace);
505 EXPECT_TOKEN(Tokens[16], tok::r_brace, TT_StructRBrace);
507 Tokens = annotate("struct macro(a) S {\n"
508 " void f(T &t);\n"
509 "};");
510 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
511 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
512 EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
513 EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
515 Tokens = annotate("template <typename T> struct S<const T[N]> {};");
516 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
517 EXPECT_TOKEN(Tokens[7], tok::less, TT_TemplateOpener);
518 EXPECT_TOKEN(Tokens[10], tok::l_square, TT_ArraySubscriptLSquare);
519 EXPECT_TOKEN(Tokens[13], tok::greater, TT_TemplateCloser);
520 EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_StructLBrace);
521 EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
523 Tokens = annotate("template <typename T> struct S<T const[N]> {};");
524 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
525 EXPECT_TOKEN(Tokens[7], tok::less, TT_TemplateOpener);
526 EXPECT_TOKEN(Tokens[10], tok::l_square, TT_ArraySubscriptLSquare);
527 EXPECT_TOKEN(Tokens[13], tok::greater, TT_TemplateCloser);
528 EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_StructLBrace);
529 EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
531 Tokens = annotate("template <typename T, unsigned n> struct S<T const[n]> {\n"
532 " void f(T const (&a)[n]);\n"
533 "};");
534 ASSERT_EQ(Tokens.size(), 35u) << Tokens;
535 EXPECT_TOKEN(Tokens[10], tok::less, TT_TemplateOpener);
536 EXPECT_TOKEN(Tokens[13], tok::l_square, TT_ArraySubscriptLSquare);
537 EXPECT_TOKEN(Tokens[16], tok::greater, TT_TemplateCloser);
538 EXPECT_TOKEN(Tokens[17], tok::l_brace, TT_StructLBrace);
539 EXPECT_TOKEN(Tokens[23], tok::l_paren, TT_FunctionTypeLParen);
540 EXPECT_TOKEN(Tokens[24], tok::amp, TT_UnaryOperator);
541 EXPECT_TOKEN(Tokens[27], tok::l_square, TT_ArraySubscriptLSquare);
542 EXPECT_TOKEN(Tokens[32], tok::r_brace, TT_StructRBrace);
544 Tokens = annotate("template <typename T, enum E e> struct S {};");
545 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
546 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_StructLBrace);
548 auto Style = getLLVMStyle();
549 Style.AttributeMacros.push_back("EXPORT");
550 Tokens = annotate("struct EXPORT StructName {};", Style);
551 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
552 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_AttributeMacro);
553 EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_StructLBrace);
554 EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_StructRBrace);
557 TEST_F(TokenAnnotatorTest, UnderstandsUnions) {
558 auto Tokens = annotate("union U {};");
559 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
560 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_UnionLBrace);
561 EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_UnionRBrace);
563 Tokens = annotate("union U { void f() { return; } };");
564 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
565 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_UnionLBrace);
566 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_FunctionLBrace);
567 EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_UnionRBrace);
570 TEST_F(TokenAnnotatorTest, UnderstandsEnums) {
571 auto Tokens = annotate("enum E {};");
572 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
573 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_EnumLBrace);
574 EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_EnumRBrace);
577 TEST_F(TokenAnnotatorTest, UnderstandsDefaultedAndDeletedFunctions) {
578 auto Tokens = annotate("auto operator<=>(const T &) const & = default;");
579 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
580 EXPECT_TOKEN(Tokens[9], tok::amp, TT_PointerOrReference);
582 Tokens = annotate("template <typename T> void F(T) && = delete;");
583 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
584 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);
587 TEST_F(TokenAnnotatorTest, UnderstandsVariables) {
588 auto Tokens =
589 annotate("inline bool var = is_integral_v<int> && is_signed_v<int>;");
590 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
591 EXPECT_TOKEN(Tokens[8], tok::ampamp, TT_BinaryOperator);
594 TEST_F(TokenAnnotatorTest, UnderstandsVariableTemplates) {
595 auto Tokens =
596 annotate("template <typename T> "
597 "inline bool var = is_integral_v<int> && is_signed_v<int>;");
598 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
599 EXPECT_TOKEN(Tokens[13], tok::ampamp, TT_BinaryOperator);
602 TEST_F(TokenAnnotatorTest, UnderstandsTemplatesInMacros) {
603 auto Tokens =
604 annotate("#define FOO(typeName) \\\n"
605 " { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }");
606 ASSERT_EQ(Tokens.size(), 27u) << Tokens;
607 EXPECT_TOKEN(Tokens[11], tok::less, TT_TemplateOpener);
608 EXPECT_TOKEN(Tokens[13], tok::greater, TT_TemplateCloser);
609 EXPECT_TOKEN(Tokens[17], tok::less, TT_TemplateOpener);
610 EXPECT_TOKEN(Tokens[19], tok::greater, TT_TemplateCloser);
613 TEST_F(TokenAnnotatorTest, UnderstandsGreaterAfterTemplateCloser) {
614 auto Tokens = annotate("if (std::tuple_size_v<T> > 0)");
615 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
616 EXPECT_TOKEN(Tokens[5], tok::less, TT_TemplateOpener);
617 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
618 EXPECT_TOKEN(Tokens[8], tok::greater, TT_BinaryOperator);
621 TEST_F(TokenAnnotatorTest, UnderstandsTernaryInTemplate) {
622 // IsExpression = false
623 auto Tokens = annotate("foo<true ? 1 : 2>();");
624 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
625 EXPECT_TOKEN(Tokens[1], tok::less, TT_TemplateOpener);
626 EXPECT_TOKEN(Tokens[3], tok::question, TT_ConditionalExpr);
627 EXPECT_TOKEN(Tokens[5], tok::colon, TT_ConditionalExpr);
628 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
630 // IsExpression = true
632 Tokens = annotate("return foo<true ? 1 : 2>();");
633 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
634 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
635 EXPECT_TOKEN(Tokens[4], tok::question, TT_ConditionalExpr);
636 EXPECT_TOKEN(Tokens[6], tok::colon, TT_ConditionalExpr);
637 EXPECT_TOKEN(Tokens[8], tok::greater, TT_TemplateCloser);
639 Tokens = annotate("return foo<true ? 1 : 2>{};");
640 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
641 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
642 EXPECT_TOKEN(Tokens[4], tok::question, TT_ConditionalExpr);
643 EXPECT_TOKEN(Tokens[6], tok::colon, TT_ConditionalExpr);
644 EXPECT_TOKEN(Tokens[8], tok::greater, TT_TemplateCloser);
647 TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) {
648 auto Tokens = annotate("return a < b && c > d;");
649 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
650 EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
651 EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);
653 Tokens = annotate("a < 0 ? b : a > 0 ? c : d;");
654 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
655 EXPECT_TOKEN(Tokens[1], tok::less, TT_BinaryOperator);
656 EXPECT_TOKEN(Tokens[7], tok::greater, TT_BinaryOperator);
658 Tokens = annotate("return A < B ? true : A > B;");
659 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
660 EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
661 EXPECT_TOKEN(Tokens[8], tok::greater, TT_BinaryOperator);
663 Tokens = annotate("return A < B ? true : A > B ? false : false;");
664 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
665 EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
666 EXPECT_TOKEN(Tokens[8], tok::greater, TT_BinaryOperator);
668 Tokens = annotate("return checklower ? a < b : a > b;");
669 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
670 EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
671 EXPECT_TOKEN(Tokens[8], tok::greater, TT_BinaryOperator);
673 Tokens = annotate("return A < B ^ A > B;");
674 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
675 EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
676 EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);
678 Tokens = annotate("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
679 ASSERT_EQ(Tokens.size(), 27u) << Tokens;
680 EXPECT_TOKEN(Tokens[7], tok::less, TT_BinaryOperator);
681 EXPECT_TOKEN(Tokens[20], tok::greater, TT_BinaryOperator);
684 TEST_F(TokenAnnotatorTest, UnderstandsTemplateTemplateParameters) {
685 auto Tokens = annotate("template <template <typename...> typename X,\n"
686 " template <typename...> class Y,\n"
687 " typename... T>\n"
688 "class A {};");
689 ASSERT_EQ(Tokens.size(), 28u) << Tokens;
690 EXPECT_TOKEN(Tokens[1], tok::less, TT_TemplateOpener);
691 EXPECT_TOKEN(Tokens[3], tok::less, TT_TemplateOpener);
692 EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
693 EXPECT_FALSE(Tokens[6]->ClosesTemplateDeclaration);
694 EXPECT_TOKEN(Tokens[11], tok::less, TT_TemplateOpener);
695 EXPECT_TOKEN(Tokens[14], tok::greater, TT_TemplateCloser);
696 EXPECT_FALSE(Tokens[14]->ClosesTemplateDeclaration);
697 EXPECT_TOKEN(Tokens[21], tok::greater, TT_TemplateCloser);
698 EXPECT_TRUE(Tokens[21]->ClosesTemplateDeclaration);
701 TEST_F(TokenAnnotatorTest, UnderstandsWhitespaceSensitiveMacros) {
702 FormatStyle Style = getLLVMStyle();
703 Style.WhitespaceSensitiveMacros.push_back("FOO");
705 auto Tokens = annotate("FOO(1+2 )", Style);
706 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
707 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_UntouchableMacroFunc);
709 Tokens = annotate("FOO(a:b:c)", Style);
710 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
711 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_UntouchableMacroFunc);
714 TEST_F(TokenAnnotatorTest, UnderstandsDelete) {
715 auto Tokens = annotate("delete (void *)p;");
716 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
717 EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
719 Tokens = annotate("delete[] (void *)p;");
720 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
721 EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_CastRParen);
723 Tokens = annotate("delete[] /*comment*/ (void *)p;");
724 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
725 EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
727 Tokens = annotate("delete[/*comment*/] (void *)p;");
728 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
729 EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
731 Tokens = annotate("delete/*comment*/[] (void *)p;");
732 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
733 EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
736 TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
737 auto Tokens = annotate("(void)p;");
738 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
739 EXPECT_TOKEN(Tokens[2], tok::r_paren, TT_CastRParen);
741 Tokens = annotate("(uint32_t)&&label;");
742 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
743 EXPECT_TOKEN(Tokens[2], tok::r_paren, TT_CastRParen);
744 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_UnaryOperator);
745 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
747 Tokens = annotate("auto x = (Foo)p;");
748 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
749 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
751 Tokens = annotate("(std::vector<int>)p;");
752 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
753 EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
755 Tokens = annotate("return (Foo)p;");
756 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
757 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_CastRParen);
759 Tokens = annotate("throw (Foo)p;");
760 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
761 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_CastRParen);
763 Tokens = annotate("#define FOO(x) (((uint64_t)(x) * BAR) / 100)");
764 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
765 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
766 EXPECT_TOKEN(Tokens[13], tok::r_paren, TT_Unknown);
767 EXPECT_TOKEN(Tokens[14], tok::star, TT_BinaryOperator);
769 Tokens = annotate("#define foo(i) ((i) - bar)");
770 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
771 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown);
772 EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator);
774 Tokens = annotate("return (Foo) & 10;");
775 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
776 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_Unknown);
777 EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator);
779 Tokens = annotate("return (struct foo){};");
780 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
781 EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
783 Tokens = annotate("#define FOO(bar) foo((uint64_t)&bar)");
784 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
785 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
786 EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
788 Tokens = annotate("#define FOO(bar) foo((Foo) & bar)");
789 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
790 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_Unknown);
791 EXPECT_TOKEN(Tokens[11], tok::amp, TT_BinaryOperator);
793 Tokens = annotate("func((void (*)())&a);");
794 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
795 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
796 EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference);
797 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_CastRParen);
798 EXPECT_TOKEN(Tokens[10], tok::amp, TT_UnaryOperator);
800 Tokens = annotate("int result = ((int)a) - b;");
801 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
802 EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_CastRParen);
803 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
804 EXPECT_TOKEN(Tokens[9], tok::minus, TT_BinaryOperator);
806 Tokens = annotate("return (double)(foo(30)) - 15;");
807 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
808 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_CastRParen);
809 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_Unknown);
810 EXPECT_TOKEN(Tokens[10], tok::minus, TT_BinaryOperator);
812 Tokens = annotate("return (::Type)(1 + 2);");
813 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
814 EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
816 Tokens = annotate("return (Namespace::Class)(1 + 2);");
817 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
818 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
820 Tokens = annotate("return (Foo (*)(void *, Bar, ...))&foo;");
821 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
822 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen);
823 EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen);
824 EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator);
826 auto Style = getLLVMStyle();
827 Style.TypeNames.push_back("Foo");
828 Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
829 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
830 EXPECT_TOKEN(Tokens[9], tok::identifier, TT_TypeName);
831 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
832 EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
835 TEST_F(TokenAnnotatorTest, UnderstandsDynamicExceptionSpecifier) {
836 auto Tokens = annotate("void f() throw(int);");
837 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
838 EXPECT_TOKEN(Tokens[4], tok::kw_throw, TT_Unknown);
841 TEST_F(TokenAnnotatorTest, UnderstandsFunctionRefQualifiers) {
842 auto Tokens = annotate("void f() &;");
843 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
844 EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
846 Tokens = annotate("void operator=(T) &&;");
847 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
848 EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_PointerOrReference);
850 Tokens = annotate("template <typename T> void f() &;");
851 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
852 EXPECT_TOKEN(Tokens[9], tok::amp, TT_PointerOrReference);
854 Tokens = annotate("template <typename T> void operator=(T) &;");
855 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
856 EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
859 TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
860 auto Tokens = annotate("x.operator+()");
861 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
862 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
863 EXPECT_TOKEN(Tokens[3], tok::plus, TT_OverloadedOperator);
864 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
865 Tokens = annotate("x.operator=()");
866 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
867 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
868 EXPECT_TOKEN(Tokens[3], tok::equal, TT_OverloadedOperator);
869 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
870 Tokens = annotate("x.operator+=()");
871 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
872 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
873 EXPECT_TOKEN(Tokens[3], tok::plusequal, TT_OverloadedOperator);
874 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
875 Tokens = annotate("x.operator,()");
876 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
877 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
878 EXPECT_TOKEN(Tokens[3], tok::comma, TT_OverloadedOperator);
879 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
880 Tokens = annotate("x.operator()()");
881 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
882 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
883 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperator);
884 EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_OverloadedOperator);
885 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen);
886 Tokens = annotate("x.operator[]()");
887 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
888 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
889 // EXPECT_TOKEN(Tokens[3], tok::l_square, TT_OverloadedOperator);
890 // EXPECT_TOKEN(Tokens[4], tok::r_square, TT_OverloadedOperator);
891 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen);
892 Tokens = annotate("x.operator\"\"_a()");
893 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
894 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
895 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator);
896 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
897 Tokens = annotate("x.operator\"\" _a()");
898 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
899 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
900 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator);
901 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen);
902 Tokens = annotate("x.operator\"\"if()");
903 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
904 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
905 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator);
906 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
907 Tokens = annotate("x.operator\"\"s()");
908 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
909 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
910 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator);
911 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
912 Tokens = annotate("x.operator\"\" s()");
913 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
914 EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
915 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator);
916 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen);
918 Tokens = annotate("int operator+(int);");
919 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
920 EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName);
921 EXPECT_TOKEN(Tokens[2], tok::plus, TT_OverloadedOperator);
922 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperatorLParen);
923 Tokens = annotate("auto operator=(T&) {}");
924 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
925 EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName);
926 EXPECT_TOKEN(Tokens[2], tok::equal, TT_OverloadedOperator);
927 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperatorLParen);
928 Tokens = annotate("auto operator()() {}");
929 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
930 EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName);
931 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_OverloadedOperator);
932 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_OverloadedOperator);
933 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
935 Tokens = annotate("class Foo {\n"
936 " int operator+(a* b);\n"
937 "}");
938 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
939 EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
940 EXPECT_TOKEN(Tokens[5], tok::plus, TT_OverloadedOperator);
941 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
942 EXPECT_TOKEN(Tokens[8], tok::star, TT_PointerOrReference);
944 Tokens = annotate("class Foo {\n"
945 " int c = operator+(a * b);\n"
946 "}");
947 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
948 EXPECT_TOKEN(Tokens[6], tok::kw_operator, TT_Unknown);
949 EXPECT_TOKEN(Tokens[7], tok::plus, TT_OverloadedOperator);
950 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_OverloadedOperatorLParen);
951 EXPECT_TOKEN(Tokens[10], tok::star, TT_BinaryOperator);
953 Tokens = annotate("void foo() {\n"
954 " operator+(a * b);\n"
955 "}");
956 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
957 EXPECT_TOKEN(Tokens[5], tok::kw_operator, TT_Unknown);
958 EXPECT_TOKEN(Tokens[6], tok::plus, TT_OverloadedOperator);
959 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_OverloadedOperatorLParen);
960 EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator);
962 Tokens = annotate("return operator+(a * b, c & d) + operator+(a && b && c);");
963 ASSERT_EQ(Tokens.size(), 24u) << Tokens;
964 EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_Unknown);
965 EXPECT_TOKEN(Tokens[2], tok::plus, TT_OverloadedOperator);
966 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperatorLParen);
967 EXPECT_TOKEN(Tokens[5], tok::star, TT_BinaryOperator);
968 EXPECT_TOKEN(Tokens[9], tok::amp, TT_BinaryOperator);
969 EXPECT_TOKEN(Tokens[13], tok::kw_operator, TT_Unknown);
970 EXPECT_TOKEN(Tokens[14], tok::plus, TT_OverloadedOperator);
971 EXPECT_TOKEN(Tokens[15], tok::l_paren, TT_OverloadedOperatorLParen);
972 EXPECT_TOKEN(Tokens[17], tok::ampamp, TT_BinaryOperator);
973 EXPECT_TOKEN(Tokens[19], tok::ampamp, TT_BinaryOperator);
975 Tokens = annotate("class Foo {\n"
976 " void foo() {\n"
977 " operator+(a * b);\n"
978 " }\n"
979 "}");
980 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
981 EXPECT_TOKEN(Tokens[8], tok::kw_operator, TT_Unknown);
982 EXPECT_TOKEN(Tokens[9], tok::plus, TT_OverloadedOperator);
983 EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_OverloadedOperatorLParen);
984 EXPECT_TOKEN(Tokens[12], tok::star, TT_BinaryOperator);
986 Tokens = annotate("std::vector<Foo> operator()(Foo &foo);");
987 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
988 EXPECT_TOKEN(Tokens[3], tok::less, TT_TemplateOpener);
989 EXPECT_TOKEN(Tokens[5], tok::greater, TT_TemplateCloser);
990 EXPECT_TOKEN(Tokens[6], tok::kw_operator, TT_FunctionDeclarationName);
991 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_OverloadedOperator);
992 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_OverloadedOperator);
993 EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_OverloadedOperatorLParen);
994 EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
996 Tokens = annotate("decltype(auto) operator()(T &x);");
997 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
998 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_TypeDeclarationParen);
999 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_TypeDeclarationParen);
1000 EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
1001 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperator);
1002 EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_OverloadedOperator);
1003 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_OverloadedOperatorLParen);
1004 EXPECT_TOKEN(Tokens[9], tok::amp, TT_PointerOrReference);
1006 Tokens = annotate("friend ostream& ::operator<<(ostream& lhs, foo& rhs);");
1007 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1008 EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
1009 EXPECT_TOKEN(Tokens[5], tok::lessless, TT_OverloadedOperator);
1010 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
1011 EXPECT_TOKEN(Tokens[8], tok::amp, TT_PointerOrReference);
1012 EXPECT_TOKEN(Tokens[12], tok::amp, TT_PointerOrReference);
1015 TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) {
1016 struct {
1017 const char *Text;
1018 tok::TokenKind Kind;
1019 } Operators[] = {{"+", tok::plus},
1020 {"-", tok::minus},
1021 // FIXME:
1022 // {"*", tok::star},
1023 {"/", tok::slash},
1024 {"%", tok::percent},
1025 {"^", tok::caret},
1026 // FIXME:
1027 // {"&", tok::amp},
1028 {"|", tok::pipe},
1029 {"~", tok::tilde},
1030 {"!", tok::exclaim},
1031 {"=", tok::equal},
1032 // FIXME:
1033 // {"<", tok::less},
1034 {">", tok::greater},
1035 {"+=", tok::plusequal},
1036 {"-=", tok::minusequal},
1037 {"*=", tok::starequal},
1038 {"/=", tok::slashequal},
1039 {"%=", tok::percentequal},
1040 {"^=", tok::caretequal},
1041 {"&=", tok::ampequal},
1042 {"|=", tok::pipeequal},
1043 {"<<", tok::lessless},
1044 {">>", tok::greatergreater},
1045 {">>=", tok::greatergreaterequal},
1046 {"<<=", tok::lesslessequal},
1047 {"==", tok::equalequal},
1048 {"!=", tok::exclaimequal},
1049 {"<=", tok::lessequal},
1050 {">=", tok::greaterequal},
1051 {"<=>", tok::spaceship},
1052 {"&&", tok::ampamp},
1053 {"||", tok::pipepipe},
1054 {"++", tok::plusplus},
1055 {"--", tok::minusminus},
1056 {",", tok::comma},
1057 {"->*", tok::arrowstar},
1058 {"->", tok::arrow}};
1060 for (const auto &Operator : Operators) {
1061 std::string Input("C<&operator");
1062 Input += Operator.Text;
1063 Input += " > a;";
1064 auto Tokens = annotate(std::string(Input));
1065 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
1066 EXPECT_TOKEN(Tokens[1], tok::less, TT_TemplateOpener);
1067 EXPECT_TOKEN(Tokens[4], Operator.Kind, TT_OverloadedOperator);
1068 EXPECT_TOKEN(Tokens[5], tok::greater, TT_TemplateCloser);
1071 auto Tokens = annotate("C<&operator< <X>> lt;");
1072 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1073 EXPECT_TOKEN(Tokens[1], tok::less, TT_TemplateOpener);
1074 EXPECT_TOKEN(Tokens[4], tok::less, TT_OverloadedOperator);
1075 EXPECT_TOKEN(Tokens[5], tok::less, TT_TemplateOpener);
1076 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
1077 EXPECT_TOKEN(Tokens[8], tok::greater, TT_TemplateCloser);
1080 TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
1081 auto Tokens = annotate("template <typename T>\n"
1082 "concept C = (Foo && Bar) && (Bar && Baz);");
1084 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1085 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_BinaryOperator);
1086 EXPECT_TOKEN(Tokens[13], tok::ampamp, TT_BinaryOperator);
1087 EXPECT_TOKEN(Tokens[16], tok::ampamp, TT_BinaryOperator);
1089 Tokens = annotate("template <typename T>\n"
1090 "concept C = Foo && !Bar;");
1092 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
1093 EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);
1094 EXPECT_TOKEN(Tokens[10], tok::exclaim, TT_UnaryOperator);
1096 Tokens = annotate("template <typename T>\n"
1097 "concept C = requires(T t) {\n"
1098 " { t.foo() };\n"
1099 "} && Bar<T> && Baz<T>;");
1100 ASSERT_EQ(Tokens.size(), 35u) << Tokens;
1101 EXPECT_TOKEN(Tokens[8], tok::kw_requires, TT_RequiresExpression);
1102 EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_RequiresExpressionLParen);
1103 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
1104 EXPECT_TOKEN(Tokens[23], tok::ampamp, TT_BinaryOperator);
1105 EXPECT_TOKEN(Tokens[28], tok::ampamp, TT_BinaryOperator);
1107 Tokens = annotate("template<typename T>\n"
1108 "requires C1<T> && (C21<T> || C22<T> && C2e<T>) && C3<T>\n"
1109 "struct Foo;");
1110 ASSERT_EQ(Tokens.size(), 36u) << Tokens;
1111 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1112 EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown);
1113 EXPECT_EQ(Tokens[6]->FakeLParens.size(), 1u);
1114 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_BinaryOperator);
1115 EXPECT_TOKEN(Tokens[16], tok::pipepipe, TT_BinaryOperator);
1116 EXPECT_TOKEN(Tokens[21], tok::ampamp, TT_BinaryOperator);
1117 EXPECT_TOKEN(Tokens[27], tok::ampamp, TT_BinaryOperator);
1118 EXPECT_TOKEN(Tokens[31], tok::greater, TT_TemplateCloser);
1119 EXPECT_EQ(Tokens[31]->FakeRParens, 1u);
1120 EXPECT_TRUE(Tokens[31]->ClosesRequiresClause);
1122 Tokens =
1123 annotate("template<typename T>\n"
1124 "requires (C1<T> && (C21<T> || C22<T> && C2e<T>) && C3<T>)\n"
1125 "struct Foo;");
1126 ASSERT_EQ(Tokens.size(), 38u) << Tokens;
1127 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1128 EXPECT_TOKEN(Tokens[7], tok::identifier, TT_Unknown);
1129 EXPECT_EQ(Tokens[7]->FakeLParens.size(), 1u);
1130 EXPECT_TOKEN(Tokens[11], tok::ampamp, TT_BinaryOperator);
1131 EXPECT_TOKEN(Tokens[17], tok::pipepipe, TT_BinaryOperator);
1132 EXPECT_TOKEN(Tokens[22], tok::ampamp, TT_BinaryOperator);
1133 EXPECT_TOKEN(Tokens[28], tok::ampamp, TT_BinaryOperator);
1134 EXPECT_TOKEN(Tokens[32], tok::greater, TT_TemplateCloser);
1135 EXPECT_EQ(Tokens[32]->FakeRParens, 1u);
1136 EXPECT_TOKEN(Tokens[33], tok::r_paren, TT_Unknown);
1137 EXPECT_TRUE(Tokens[33]->ClosesRequiresClause);
1139 Tokens = annotate("template <typename T>\n"
1140 "void foo(T) noexcept requires Bar<T>;");
1141 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1142 EXPECT_TOKEN(Tokens[11], tok::kw_requires, TT_RequiresClause);
1144 Tokens = annotate("template <typename T>\n"
1145 "requires Bar<T> || Baz<T>\n"
1146 "auto foo(T) -> int;");
1147 ASSERT_EQ(Tokens.size(), 24u) << Tokens;
1148 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1149 EXPECT_EQ(Tokens[11]->FakeLParens.size(), 0u);
1150 EXPECT_TRUE(Tokens[14]->ClosesRequiresClause);
1151 EXPECT_TOKEN(Tokens[20], tok::arrow, TT_TrailingReturnArrow);
1153 Tokens = annotate("template <typename T>\n"
1154 "requires Bar<T>\n"
1155 "bool foo(T) { return false; }");
1156 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1157 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1158 EXPECT_TRUE(Tokens[9]->ClosesRequiresClause);
1159 EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName);
1160 EXPECT_TOKEN(Tokens[12], tok::l_paren, TT_FunctionDeclarationLParen);
1162 Tokens = annotate("template <typename T>\n"
1163 "requires Bar<T>\n"
1164 "decltype(auto) foo(T) { return false; }");
1165 ASSERT_EQ(Tokens.size(), 24u) << Tokens;
1166 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1167 EXPECT_TRUE(Tokens[9]->ClosesRequiresClause);
1168 EXPECT_TOKEN(Tokens[14], tok::identifier, TT_FunctionDeclarationName);
1169 EXPECT_TOKEN(Tokens[15], tok::l_paren, TT_FunctionDeclarationLParen);
1171 Tokens = annotate("template <typename T>\n"
1172 "struct S {\n"
1173 " void foo() const requires Bar<T>;\n"
1174 " void bar() const & requires Baz<T>;\n"
1175 " void bar() && requires Baz2<T>;\n"
1176 " void baz() const & noexcept requires Baz<T>;\n"
1177 " void baz() && noexcept requires Baz2<T>;\n"
1178 "};\n"
1179 "\n"
1180 "void S::bar() const & requires Baz<T> { }");
1181 ASSERT_EQ(Tokens.size(), 85u) << Tokens;
1182 EXPECT_TOKEN(Tokens[13], tok::kw_requires, TT_RequiresClause);
1183 EXPECT_TOKEN(Tokens[24], tok::amp, TT_PointerOrReference);
1184 EXPECT_TOKEN(Tokens[25], tok::kw_requires, TT_RequiresClause);
1185 EXPECT_TOKEN(Tokens[35], tok::ampamp, TT_PointerOrReference);
1186 EXPECT_TOKEN(Tokens[36], tok::kw_requires, TT_RequiresClause);
1187 EXPECT_TOKEN(Tokens[47], tok::amp, TT_PointerOrReference);
1188 EXPECT_TOKEN(Tokens[49], tok::kw_requires, TT_RequiresClause);
1189 EXPECT_TOKEN(Tokens[59], tok::ampamp, TT_PointerOrReference);
1190 EXPECT_TOKEN(Tokens[61], tok::kw_requires, TT_RequiresClause);
1191 EXPECT_TOKEN(Tokens[76], tok::amp, TT_PointerOrReference);
1192 EXPECT_TOKEN(Tokens[77], tok::kw_requires, TT_RequiresClause);
1194 Tokens = annotate("void Class::member() && requires(Constant) {}");
1195 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
1196 EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause);
1198 Tokens = annotate("void Class::member() && requires(Constant<T>) {}");
1199 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1200 EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause);
1202 Tokens =
1203 annotate("void Class::member() && requires(Namespace::Constant<T>) {}");
1204 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1205 EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause);
1207 Tokens = annotate("void Class::member() && requires(typename "
1208 "Namespace::Outer<T>::Inner::Constant) {}");
1209 ASSERT_EQ(Tokens.size(), 24u) << Tokens;
1210 EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause);
1212 Tokens = annotate("struct [[nodiscard]] zero_t {\n"
1213 " template<class T>\n"
1214 " requires requires { number_zero_v<T>; }\n"
1215 " [[nodiscard]] constexpr operator T() const { "
1216 "return number_zero_v<T>; }\n"
1217 "};");
1218 ASSERT_EQ(Tokens.size(), 44u) << Tokens;
1219 EXPECT_TOKEN(Tokens[13], tok::kw_requires, TT_RequiresClause);
1220 EXPECT_TOKEN(Tokens[14], tok::kw_requires, TT_RequiresExpression);
1221 EXPECT_TOKEN(Tokens[15], tok::l_brace, TT_RequiresExpressionLBrace);
1222 EXPECT_TOKEN(Tokens[21], tok::r_brace, TT_Unknown);
1223 EXPECT_EQ(Tokens[21]->MatchingParen, Tokens[15]);
1224 EXPECT_TRUE(Tokens[21]->ClosesRequiresClause);
1226 Tokens =
1227 annotate("template <class A, class B> concept C ="
1228 "std::same_as<std::iter_value_t<A>, std::iter_value_t<B>>;");
1229 ASSERT_EQ(Tokens.size(), 31u) << Tokens;
1230 EXPECT_TOKEN(Tokens[8], tok::kw_concept, TT_Unknown);
1231 EXPECT_TOKEN(Tokens[14], tok::less, TT_TemplateOpener);
1232 EXPECT_TOKEN(Tokens[18], tok::less, TT_TemplateOpener);
1233 EXPECT_TOKEN(Tokens[20], tok::greater, TT_TemplateCloser);
1234 EXPECT_TOKEN(Tokens[25], tok::less, TT_TemplateOpener);
1235 EXPECT_TOKEN(Tokens[27], tok::greater, TT_TemplateCloser);
1236 EXPECT_TOKEN(Tokens[28], tok::greater, TT_TemplateCloser);
1238 Tokens = annotate("auto bar() -> int requires(is_integral_v<T>) {}");
1239 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1240 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1242 Tokens = annotate("auto bar() -> void requires(is_integral_v<T>) {}");
1243 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1244 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1246 Tokens = annotate("auto bar() -> MyType requires(is_integral_v<T>) {}");
1247 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1248 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1250 Tokens =
1251 annotate("auto bar() -> SOME_MACRO_TYPE requires(is_integral_v<T>) {}");
1252 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1253 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1255 Tokens =
1256 annotate("auto bar() -> qualified::type requires(is_integral_v<T>) {}");
1257 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1258 EXPECT_TOKEN(Tokens[8], tok::kw_requires, TT_RequiresClause);
1260 Tokens =
1261 annotate("auto bar() -> Template<type> requires(is_integral_v<T>) {}");
1262 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1263 EXPECT_TOKEN(Tokens[9], tok::kw_requires, TT_RequiresClause);
1265 Tokens = annotate("void foo() requires((A<T>) && C) {}");
1266 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1267 EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
1268 EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);
1270 Tokens = annotate("void foo() requires(((A<T>) && C)) {}");
1271 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
1272 EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
1273 EXPECT_TOKEN(Tokens[13], tok::ampamp, TT_BinaryOperator);
1275 Tokens = annotate("void foo() requires([](T&&){}(t)) {}");
1276 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1277 EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
1278 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);
1280 Tokens = annotate("void foo() requires([](T&& u){}(t)) {}");
1281 ASSERT_EQ(Tokens.size(), 22u) << Tokens;
1282 EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
1283 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);
1285 Tokens = annotate("void f() & requires(true) {}");
1286 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1287 EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
1288 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1290 Tokens = annotate("void f() & requires(C<true, true>) {}");
1291 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1292 EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
1293 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1295 Tokens = annotate("template <typename T>\n"
1296 "concept C = (!Foo<T>) && Bar;");
1297 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1298 EXPECT_TOKEN(Tokens[15], tok::ampamp, TT_BinaryOperator);
1300 Tokens = annotate("void f() & requires(C<decltype(x)>) {}");
1301 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1302 EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
1303 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1305 Tokens = annotate("auto f() -> int& requires(C<decltype(x)>) {}");
1306 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
1307 EXPECT_TOKEN(Tokens[6], tok::amp, TT_PointerOrReference);
1308 EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause);
1310 Tokens = annotate("bool x = t && requires(decltype(t) x) { x.foo(); };");
1311 ASSERT_EQ(Tokens.size(), 23u) << Tokens;
1312 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresExpression);
1314 Tokens = annotate("bool x = t && requires(Foo<decltype(t)> x) { x.foo(); };");
1315 ASSERT_EQ(Tokens.size(), 26u) << Tokens;
1316 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresExpression);
1318 Tokens = annotate("bool x = t && requires(Foo<C1 || C2> x) { x.foo(); };");
1319 ASSERT_EQ(Tokens.size(), 25u) << Tokens;
1320 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresExpression);
1322 // Second function definition is required due to lookahead
1323 Tokens = annotate("void f() &\n"
1324 " requires(n == 1)\n"
1325 "{}\n"
1326 "void g();");
1327 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1328 EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
1329 EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1332 TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
1333 auto Tokens = annotate("bool b = requires(int i) { i + 5; };");
1334 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1335 EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
1336 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
1337 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
1339 Tokens = annotate("if (requires(int i) { i + 5; }) return;");
1340 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1341 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1342 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1343 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_RequiresExpressionLBrace);
1345 Tokens = annotate("if (func() && requires(int i) { i + 5; }) return;");
1346 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1347 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresExpression);
1348 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_RequiresExpressionLParen);
1349 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_RequiresExpressionLBrace);
1351 Tokens = annotate("foo(requires(const T t) {});");
1352 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1353 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1354 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1355 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
1357 Tokens = annotate("foo(requires(const int t) {});");
1358 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1359 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1360 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1361 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
1363 Tokens = annotate("foo(requires(const T t) {});");
1364 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1365 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1366 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1367 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
1369 Tokens = annotate("foo(requires(int const* volatile t) {});");
1370 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
1371 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1372 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1373 EXPECT_TOKEN(Tokens[6], tok::star, TT_PointerOrReference);
1374 EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_RequiresExpressionLBrace);
1376 Tokens = annotate("foo(requires(T const* volatile t) {});");
1377 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
1378 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1379 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1380 EXPECT_TOKEN(Tokens[6], tok::star, TT_PointerOrReference);
1381 EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_RequiresExpressionLBrace);
1383 Tokens = annotate("foo(requires(T& t) {});");
1384 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1385 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1386 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1387 EXPECT_TOKEN(Tokens[5], tok::amp, TT_PointerOrReference);
1388 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
1390 Tokens = annotate("foo(requires(T&& t) {});");
1391 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1392 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1393 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1394 EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_PointerOrReference);
1395 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
1397 Tokens = annotate("bool foo = requires(T& t) {};");
1398 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1399 EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
1400 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
1401 EXPECT_TOKEN(Tokens[6], tok::amp, TT_PointerOrReference);
1402 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_RequiresExpressionLBrace);
1404 Tokens = annotate("bool foo = requires(T&& t) {};");
1405 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1406 EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
1407 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
1408 EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_PointerOrReference);
1409 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_RequiresExpressionLBrace);
1411 Tokens =
1412 annotate("foo(requires(const typename Outer<T>::Inner * const t) {});");
1413 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1414 EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
1415 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_RequiresExpressionLParen);
1416 EXPECT_TOKEN(Tokens[12], tok::star, TT_PointerOrReference);
1417 EXPECT_TOKEN(Tokens[16], tok::l_brace, TT_RequiresExpressionLBrace);
1419 Tokens = annotate("template <typename T>\n"
1420 "concept C = requires(T T) {\n"
1421 " requires Bar<T> && Foo<T>;\n"
1422 "};");
1423 ASSERT_EQ(Tokens.size(), 28u) << Tokens;
1424 EXPECT_TOKEN(Tokens[8], tok::kw_requires, TT_RequiresExpression);
1425 EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_RequiresExpressionLParen);
1426 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
1427 EXPECT_TOKEN(Tokens[14], tok::kw_requires,
1428 TT_RequiresClauseInARequiresExpression);
1430 Tokens = annotate("template <typename T>\n"
1431 "concept C = requires(T T) {\n"
1432 " { t.func() } -> std::same_as<int>;"
1433 " requires Bar<T> && Foo<T>;\n"
1434 "};");
1435 ASSERT_EQ(Tokens.size(), 43u) << Tokens;
1436 EXPECT_TOKEN(Tokens[8], tok::kw_requires, TT_RequiresExpression);
1437 EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_RequiresExpressionLParen);
1438 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
1439 EXPECT_TOKEN(Tokens[29], tok::kw_requires,
1440 TT_RequiresClauseInARequiresExpression);
1442 // Invalid Code, but we don't want to crash. See http://llvm.org/PR54350.
1443 Tokens = annotate("bool r10 = requires (struct new_struct { int x; } s) { "
1444 "requires true; };");
1445 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1446 EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
1447 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
1448 EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_RequiresExpressionLBrace);
1450 Tokens = annotate("bool foo = requires(C<true, true> c) {\n"
1451 " { c.foo(); }\n"
1452 "};");
1453 ASSERT_EQ(Tokens.size(), 25u) << Tokens;
1454 EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
1455 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
1456 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
1459 TEST_F(TokenAnnotatorTest, UnderstandsPragmaRegion) {
1460 // Everything after #pragma region should be ImplicitStringLiteral
1461 auto Tokens = annotate("#pragma region Foo(Bar: Hello)");
1462 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1463 EXPECT_TOKEN(Tokens[5], tok::identifier, TT_ImplicitStringLiteral);
1464 EXPECT_TOKEN(Tokens[6], tok::colon, TT_ImplicitStringLiteral);
1465 EXPECT_TOKEN(Tokens[7], tok::identifier, TT_ImplicitStringLiteral);
1467 // Make sure it's annotated correctly inside a function as well
1468 Tokens = annotate("void test(){\n#pragma region Foo(Bar: Hello)\n}");
1469 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1470 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_ImplicitStringLiteral);
1471 EXPECT_TOKEN(Tokens[11], tok::colon, TT_ImplicitStringLiteral);
1472 EXPECT_TOKEN(Tokens[12], tok::identifier, TT_ImplicitStringLiteral);
1475 TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) {
1476 const char *BaseCode = nullptr;
1477 const char *ConstrainedCode = nullptr;
1478 auto BaseTokenCount = 0u;
1479 auto RequiresTokenCount = 0u;
1480 auto PrefixTokenCount = 0u;
1482 auto TestRequires = [&](int Line) {
1483 const auto BaseTokens = annotate(BaseCode);
1484 const auto ConstrainedTokens = annotate(ConstrainedCode);
1486 #define LINE " (Line " << Line << ')'
1488 ASSERT_EQ(BaseTokens.size(), BaseTokenCount) << BaseTokens << LINE;
1489 ASSERT_EQ(ConstrainedTokens.size(), BaseTokenCount + RequiresTokenCount)
1490 << LINE;
1492 for (auto I = 0u; I < BaseTokenCount; ++I) {
1493 EXPECT_EQ(
1494 *BaseTokens[I],
1495 *ConstrainedTokens[I < PrefixTokenCount ? I : I + RequiresTokenCount])
1496 << I << LINE;
1499 #undef LINE
1502 BaseCode = "template<typename T>\n"
1503 "T Pi = 3.14;";
1504 ConstrainedCode = "template<typename T>\n"
1505 " requires Foo<T>\n"
1506 "T Pi = 3.14;";
1507 BaseTokenCount = 11;
1508 RequiresTokenCount = 5;
1509 PrefixTokenCount = 5;
1510 TestRequires(__LINE__);
1512 BaseCode = "template<typename T>\n"
1513 "struct Bar;";
1514 ConstrainedCode = "template<typename T>\n"
1515 " requires Foo<T>\n"
1516 "struct Bar;";
1517 BaseTokenCount = 9;
1518 TestRequires(__LINE__);
1520 BaseCode = "template<typename T>\n"
1521 "struct Bar {\n"
1522 " T foo();\n"
1523 " T bar();\n"
1524 "};";
1525 ConstrainedCode = "template<typename T>\n"
1526 " requires Foo<T>\n"
1527 "struct Bar {\n"
1528 " T foo();\n"
1529 " T bar();\n"
1530 "};";
1531 BaseTokenCount = 21;
1532 TestRequires(__LINE__);
1534 BaseCode = "template<typename T>\n"
1535 "Bar(T) -> Bar<T>;";
1536 ConstrainedCode = "template<typename T>\n"
1537 " requires Foo<T>\n"
1538 "Bar(T) -> Bar<T>;";
1539 BaseTokenCount = 16;
1540 TestRequires(__LINE__);
1542 BaseCode = "template<typename T>\n"
1543 "T foo();";
1544 ConstrainedCode = "template<typename T>\n"
1545 " requires Foo<T>\n"
1546 "T foo();";
1547 BaseTokenCount = 11;
1548 TestRequires(__LINE__);
1550 BaseCode = "template<typename T>\n"
1551 "T foo() {\n"
1552 " auto bar = baz();\n"
1553 " return bar + T{};\n"
1554 "}";
1555 ConstrainedCode = "template<typename T>\n"
1556 " requires Foo<T>\n"
1557 "T foo() {\n"
1558 " auto bar = baz();\n"
1559 " return bar + T{};\n"
1560 "}";
1561 BaseTokenCount = 26;
1562 TestRequires(__LINE__);
1564 BaseCode = "template<typename T>\n"
1565 "T foo();";
1566 ConstrainedCode = "template<typename T>\n"
1567 "T foo() requires Foo<T>;";
1568 BaseTokenCount = 11;
1569 PrefixTokenCount = 9;
1570 TestRequires(__LINE__);
1572 BaseCode = "template<typename T>\n"
1573 "T foo() {\n"
1574 " auto bar = baz();\n"
1575 " return bar + T{};\n"
1576 "}";
1577 ConstrainedCode = "template<typename T>\n"
1578 "T foo() requires Foo<T> {\n"
1579 " auto bar = baz();\n"
1580 " return bar + T{};\n"
1581 "}";
1582 BaseTokenCount = 26;
1583 TestRequires(__LINE__);
1585 BaseCode = "template<typename T>\n"
1586 "T foo();";
1587 ConstrainedCode = "template<typename T>\n"
1588 " requires(Foo<T>)\n"
1589 "T foo();";
1590 BaseTokenCount = 11;
1591 RequiresTokenCount = 7;
1592 PrefixTokenCount = 5;
1593 TestRequires(__LINE__);
1595 BaseCode = "template<typename T>\n"
1596 "Bar(T) -> Bar<typename T::I>;";
1597 ConstrainedCode = "template<typename T>\n"
1598 " requires requires(T &&t) {\n"
1599 " typename T::I;\n"
1600 " }\n"
1601 "Bar(T) -> Bar<typename T::I>;";
1602 BaseTokenCount = 19;
1603 RequiresTokenCount = 14;
1604 PrefixTokenCount = 5;
1605 TestRequires(__LINE__);
1607 BaseCode = "struct [[nodiscard]] zero_t {\n"
1608 " template<class T>\n"
1609 " [[nodiscard]] constexpr operator T() const { return v<T>; }\n"
1610 "};";
1611 ConstrainedCode =
1612 "struct [[nodiscard]] zero_t {\n"
1613 " template<class T>\n"
1614 " requires requires { v<T>; }\n"
1615 " [[nodiscard]] constexpr operator T() const { return v<T>; }\n"
1616 "};";
1617 BaseTokenCount = 35;
1618 RequiresTokenCount = 9;
1619 PrefixTokenCount = 13;
1620 TestRequires(__LINE__);
1622 BaseCode = "constexpr Foo(Foo const &other)\n"
1623 " : value{other.value} {\n"
1624 " do_magic();\n"
1625 " do_more_magic();\n"
1626 "}";
1627 ConstrainedCode = "constexpr Foo(Foo const &other)\n"
1628 " requires std::is_copy_constructible<T>\n"
1629 " : value{other.value} {\n"
1630 " do_magic();\n"
1631 " do_more_magic();\n"
1632 "}";
1633 BaseTokenCount = 26;
1634 RequiresTokenCount = 7;
1635 PrefixTokenCount = 8;
1636 TestRequires(__LINE__);
1638 BaseCode = "constexpr Foo(Foo const &other)\n"
1639 " : value{other.value} {\n"
1640 " do_magic();\n"
1641 " do_more_magic();\n"
1642 "}";
1643 ConstrainedCode = "constexpr Foo(Foo const &other)\n"
1644 " requires (std::is_copy_constructible<T>)\n"
1645 " : value{other.value} {\n"
1646 " do_magic();\n"
1647 " do_more_magic();\n"
1648 "}";
1649 RequiresTokenCount = 9;
1650 TestRequires(__LINE__);
1652 BaseCode = "template<typename T>\n"
1653 "ANNOTATE(\"S\"\n"
1654 " \"S\")\n"
1655 "void foo();";
1656 ConstrainedCode = "template<typename T>\n"
1657 " requires(true)\n"
1658 "ANNOTATE(\"S\"\n"
1659 " \"S\")\n"
1660 "void foo();";
1661 BaseTokenCount = 16;
1662 RequiresTokenCount = 4;
1663 PrefixTokenCount = 5;
1664 TestRequires(__LINE__);
1667 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
1668 auto Tokens = annotate("__asm{\n"
1669 "\"a\":\n"
1670 ": x\n"
1671 ":};");
1672 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1673 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1674 EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
1675 EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
1676 EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
1677 EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
1678 EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
1680 Tokens = annotate("__asm(\n"
1681 "\"a\":\n"
1682 ": x\n"
1683 ":);");
1684 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1685 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1686 EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
1687 EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
1688 EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
1690 Tokens = annotate("asm volatile (\n"
1691 "\"a_label:\"\n"
1692 ":\n"
1693 ": x\n"
1694 ":);");
1695 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1696 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1697 EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
1698 EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
1699 EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
1701 Tokens = annotate("__asm__(\n"
1702 "\"a_label:\"\n"
1703 ": x\n"
1704 ":\n"
1705 ": y);");
1706 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1707 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1708 EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
1709 EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
1710 EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
1712 Tokens = annotate("__asm volatile (\n"
1713 "\"a_label:\"\n"
1714 "\"a b c(%%x)\"\n"
1715 ":\n"
1716 ": x\n"
1717 ":);");
1718 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1719 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1720 EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
1721 EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
1722 EXPECT_TOKEN(Tokens[8], tok::colon, TT_InlineASMColon);
1724 Tokens = annotate("asm(\n"
1725 "\"insn\"\n"
1726 ": \"=r\" (var1), \"=&r\" (value)\n"
1727 ":\n"
1728 ": \"memory\");");
1729 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1730 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1731 EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
1732 EXPECT_TOKEN(Tokens[13], tok::colon, TT_InlineASMColon);
1733 EXPECT_TOKEN(Tokens[14], tok::colon, TT_InlineASMColon);
1735 Tokens = annotate("__asm__ volatile (\n"
1736 "\"ldr r1, [r0, %%[sym]]\"\n"
1737 ":\n"
1738 ": [sym] \"J\" (aaaaa(aaaa, aaaa))\n"
1739 ");");
1740 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1741 EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
1742 EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
1743 EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
1744 EXPECT_TOKEN(Tokens[6], tok::l_square, TT_InlineASMSymbolicNameLSquare);
1747 TEST_F(TokenAnnotatorTest, UnderstandsObjCBlock) {
1748 auto Tokens = annotate("int (^)() = ^ ()\n"
1749 " external_source_symbol() { //\n"
1750 " return 1;\n"
1751 "};");
1752 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
1753 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ObjCBlockLParen);
1754 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_ObjCBlockLBrace);
1756 Tokens = annotate("int *p = ^int*(){ //\n"
1757 " return nullptr;\n"
1758 "}();");
1759 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1760 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_ObjCBlockLBrace);
1762 Tokens = annotate("id (^block)(Foo *a) = ^id _Nullable(Foo *_Nullable a) {\n"
1763 " return a;\n"
1764 "};");
1765 ASSERT_EQ(Tokens.size(), 27u) << Tokens;
1766 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown); // Not CtorDtorDeclName.
1767 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ObjCBlockLParen);
1770 TEST_F(TokenAnnotatorTest, UnderstandsObjCMethodExpr) {
1771 auto Tokens = annotate("void f() {\n"
1772 " //\n"
1773 " BOOL a = [b.c n] > 1;\n"
1774 "}");
1775 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
1776 EXPECT_TOKEN(Tokens[9], tok::l_square, TT_ObjCMethodExpr);
1777 EXPECT_TOKEN(Tokens[15], tok::greater, TT_BinaryOperator);
1780 TEST_F(TokenAnnotatorTest, UnderstandsLambdas) {
1781 auto Tokens = annotate("[]() constexpr {}");
1782 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1783 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1784 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1785 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace);
1787 Tokens = annotate("[]() consteval {}");
1788 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1789 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1790 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1791 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace);
1793 Tokens = annotate("[]() mutable {}");
1794 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1795 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1796 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1797 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace);
1799 Tokens = annotate("[]() static {}");
1800 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1801 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1802 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1803 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace);
1805 Tokens = annotate("[]() -> auto {}");
1806 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
1807 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1808 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1809 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_LambdaArrow);
1810 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_LambdaLBrace);
1812 Tokens = annotate("[]() -> auto & {}");
1813 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1814 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1815 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1816 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_LambdaArrow);
1817 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_LambdaLBrace);
1819 Tokens = annotate("[]() -> auto * {}");
1820 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1821 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1822 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_LambdaDefinitionLParen);
1823 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_LambdaArrow);
1824 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_LambdaLBrace);
1826 Tokens = annotate("[] {}");
1827 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
1828 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1829 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_LambdaLBrace);
1831 Tokens = annotate("[] noexcept {}");
1832 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
1833 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1834 EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_LambdaLBrace);
1836 Tokens = annotate("[] -> auto {}");
1837 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
1838 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1839 EXPECT_TOKEN(Tokens[2], tok::arrow, TT_LambdaArrow);
1840 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_LambdaLBrace);
1842 Tokens = annotate("[] -> struct S { return {}; }");
1843 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1844 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1845 EXPECT_TOKEN(Tokens[2], tok::arrow, TT_LambdaArrow);
1846 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_LambdaLBrace);
1848 Tokens = annotate("foo([&](u32 bar) __attribute__((attr)) -> void {});");
1849 ASSERT_EQ(Tokens.size(), 22u) << Tokens;
1850 EXPECT_TOKEN(Tokens[2], tok::l_square, TT_LambdaLSquare);
1851 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_LambdaDefinitionLParen);
1852 EXPECT_TOKEN(Tokens[15], tok::arrow, TT_LambdaArrow);
1853 EXPECT_TOKEN(Tokens[17], tok::l_brace, TT_LambdaLBrace);
1855 Tokens = annotate("[] <typename T> () {}");
1856 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1857 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1858 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1859 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_LambdaDefinitionLParen);
1860 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_LambdaLBrace);
1862 Tokens = annotate("[] <typename T> {}");
1863 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
1864 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1865 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1866 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_LambdaLBrace);
1868 Tokens = annotate("[] <typename... T> () {}");
1869 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1870 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1871 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1872 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_LambdaDefinitionLParen);
1873 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_LambdaLBrace);
1875 Tokens = annotate("[] <typename... T> {}");
1876 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1877 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1878 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1879 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_LambdaLBrace);
1881 Tokens = annotate("[] <int... T> () {}");
1882 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1883 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1884 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1885 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_LambdaDefinitionLParen);
1886 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_LambdaLBrace);
1888 Tokens = annotate("[] <int... T> {}");
1889 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1890 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1891 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1892 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_LambdaLBrace);
1894 Tokens = annotate("[] <Foo... T> () {}");
1895 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1896 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1897 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1898 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_LambdaDefinitionLParen);
1899 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_LambdaLBrace);
1901 Tokens = annotate("[] <Foo... T> {}");
1902 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
1903 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1904 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1905 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_LambdaLBrace);
1907 // Lambdas with a requires-clause
1908 Tokens = annotate("[] <typename T> (T t) requires Bar<T> {}");
1909 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1910 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1911 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1912 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_LambdaDefinitionLParen);
1913 EXPECT_TOKEN(Tokens[10], tok::kw_requires, TT_RequiresClause);
1914 EXPECT_TRUE(Tokens[14]->ClosesRequiresClause);
1915 EXPECT_TOKEN(Tokens[15], tok::l_brace, TT_LambdaLBrace);
1917 Tokens = annotate("[] <typename T> (T &&t) requires Bar<T> {}");
1918 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1919 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1920 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1921 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_LambdaDefinitionLParen);
1922 EXPECT_TOKEN(Tokens[8], tok::ampamp, TT_PointerOrReference);
1923 EXPECT_TOKEN(Tokens[11], tok::kw_requires, TT_RequiresClause);
1924 EXPECT_TRUE(Tokens[15]->ClosesRequiresClause);
1925 EXPECT_TOKEN(Tokens[16], tok::l_brace, TT_LambdaLBrace);
1927 Tokens = annotate("[] <typename T> (T t) requires Foo<T> || Bar<T> {}");
1928 ASSERT_EQ(Tokens.size(), 23u) << Tokens;
1929 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1930 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1931 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_LambdaDefinitionLParen);
1932 EXPECT_TOKEN(Tokens[10], tok::kw_requires, TT_RequiresClause);
1933 EXPECT_TRUE(Tokens[19]->ClosesRequiresClause);
1934 EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_LambdaLBrace);
1936 Tokens = annotate("[] <typename T> (T t) -> T requires Bar<T> {}");
1937 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
1938 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1939 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1940 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_LambdaDefinitionLParen);
1941 EXPECT_TOKEN(Tokens[10], tok::arrow, TT_LambdaArrow);
1942 EXPECT_TOKEN(Tokens[12], tok::kw_requires, TT_RequiresClause);
1943 EXPECT_TRUE(Tokens[16]->ClosesRequiresClause);
1944 EXPECT_TOKEN(Tokens[17], tok::l_brace, TT_LambdaLBrace);
1946 Tokens = annotate("[] <typename T> requires Bar<T> (T t) {}");
1947 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1948 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1949 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1950 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1951 EXPECT_TRUE(Tokens[10]->ClosesRequiresClause);
1952 // FIXME:
1953 // EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_LambdaDefinitionLParen);
1954 EXPECT_TOKEN(Tokens[15], tok::l_brace, TT_LambdaLBrace);
1956 Tokens = annotate("[] <typename T> requires Bar<T> (T &&t) {}");
1957 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1958 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1959 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1960 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1961 EXPECT_TRUE(Tokens[10]->ClosesRequiresClause);
1962 // FIXME:
1963 // EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_LambdaDefinitionLParen);
1964 EXPECT_TOKEN(Tokens[13], tok::ampamp, TT_PointerOrReference);
1965 EXPECT_TOKEN(Tokens[16], tok::l_brace, TT_LambdaLBrace);
1967 Tokens = annotate("[] <typename T> requires Foo<T> || Bar<T> (T t) {}");
1968 ASSERT_EQ(Tokens.size(), 23u) << Tokens;
1969 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1970 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1971 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1972 EXPECT_TRUE(Tokens[15]->ClosesRequiresClause);
1973 // FIXME:
1974 // EXPECT_TOKEN(Tokens[16], tok::l_paren, TT_LambdaDefinitionLParen);
1975 EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_LambdaLBrace);
1977 Tokens = annotate("[] <typename T> requires true (T&& t) {}");
1978 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1979 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1980 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1981 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1982 EXPECT_TRUE(Tokens[7]->ClosesRequiresClause);
1983 // FIXME:
1984 // EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_LambdaDefinitionLParen);
1985 EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);
1986 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_LambdaLBrace);
1988 Tokens = annotate("[] <typename T> requires Bar<T> {}");
1989 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
1990 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1991 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
1992 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
1993 EXPECT_TRUE(Tokens[10]->ClosesRequiresClause);
1994 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_LambdaLBrace);
1996 Tokens = annotate("[] <typename T> requires Bar<T> noexcept {}");
1997 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
1998 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
1999 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2000 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
2001 EXPECT_TRUE(Tokens[10]->ClosesRequiresClause);
2002 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_LambdaLBrace);
2004 Tokens = annotate("[] <typename T> requires Bar<T> -> T {}");
2005 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2006 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2007 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2008 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
2009 EXPECT_TRUE(Tokens[10]->ClosesRequiresClause);
2010 EXPECT_TOKEN(Tokens[11], tok::arrow, TT_LambdaArrow);
2011 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_LambdaLBrace);
2013 Tokens = annotate("[] <typename T> requires Foo<T> (T t) requires Bar<T> {}");
2014 ASSERT_EQ(Tokens.size(), 23u) << Tokens;
2015 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2016 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2017 EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
2018 EXPECT_TRUE(Tokens[10]->ClosesRequiresClause);
2019 // FIXME:
2020 // EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_LambdaDefinitionLParen);
2021 EXPECT_TOKEN(Tokens[15], tok::kw_requires, TT_RequiresClause);
2022 EXPECT_TRUE(Tokens[19]->ClosesRequiresClause);
2023 EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_LambdaLBrace);
2025 Tokens = annotate("[] <typename T = int> (T t) {}");
2026 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
2027 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2028 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2029 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
2030 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_LambdaDefinitionLParen);
2031 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_LambdaLBrace);
2033 Tokens = annotate("[] <int I = 0> (T t) {}");
2034 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
2035 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2036 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2037 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
2038 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_LambdaDefinitionLParen);
2039 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_LambdaLBrace);
2041 Tokens = annotate("[] <bool b = false> (T t) {}");
2042 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
2043 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2044 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2045 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
2046 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_LambdaDefinitionLParen);
2047 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_LambdaLBrace);
2049 Tokens = annotate("[] <bool b = true && false> (T&& t) {}");
2050 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
2051 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2052 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2053 EXPECT_TOKEN(Tokens[7], tok::ampamp, TT_BinaryOperator);
2054 EXPECT_TOKEN(Tokens[9], tok::greater, TT_TemplateCloser);
2055 EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_LambdaDefinitionLParen);
2056 EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_PointerOrReference);
2057 EXPECT_TOKEN(Tokens[15], tok::l_brace, TT_LambdaLBrace);
2059 Tokens = annotate("[] <typename T = int> requires Foo<T> (T t) {}");
2060 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
2061 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);
2062 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
2063 EXPECT_TOKEN(Tokens[7], tok::greater, TT_TemplateCloser);
2064 EXPECT_TOKEN(Tokens[8], tok::kw_requires, TT_RequiresClause);
2065 // FIXME:
2066 // EXPECT_TOKEN(Tokens[13], tok::l_paren, TT_LambdaDefinitionLParen);
2067 EXPECT_TOKEN(Tokens[17], tok::l_brace, TT_LambdaLBrace);
2070 TEST_F(TokenAnnotatorTest, UnderstandsFunctionAnnotations) {
2071 auto Tokens = annotate("template <typename T>\n"
2072 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
2073 "string OldFunction(const string &parameter) {}");
2074 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
2075 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_FunctionAnnotationRParen);
2077 Tokens = annotate("template <typename T>\n"
2078 "A(T) noexcept;");
2079 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2080 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
2082 Tokens = annotate("FOO(bar)();");
2083 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2084 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_Unknown);
2087 TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
2088 auto Tokens = annotate("void f [[noreturn]] ();");
2089 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2090 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2091 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
2093 Tokens = annotate("void f [[noreturn]] () {}");
2094 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2095 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2096 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
2098 Tokens = annotate("#define FOO Foo::\n"
2099 "FOO Foo();");
2100 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2101 EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
2102 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
2104 Tokens = annotate("struct Foo {\n"
2105 " Bar (*func)();\n"
2106 "};");
2107 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2108 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
2109 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
2111 Tokens = annotate("void instanceof();");
2112 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2113 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2114 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
2116 Tokens = annotate("int iso_time(time_t);");
2117 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2118 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2119 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
2121 auto Style = getLLVMStyle();
2122 Style.TypeNames.push_back("MyType");
2123 Tokens = annotate("int iso_time(MyType);", Style);
2124 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2125 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
2126 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
2127 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_TypeName);
2130 TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) {
2131 auto Tokens = annotate("class Foo { public: Foo(); };");
2132 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2133 EXPECT_TOKEN(Tokens[5], tok::identifier, TT_CtorDtorDeclName);
2134 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
2136 Tokens = annotate("class Foo { public: ~Foo(); };");
2137 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2138 EXPECT_TOKEN(Tokens[6], tok::identifier, TT_CtorDtorDeclName);
2139 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
2141 Tokens = annotate("struct Foo { [[deprecated]] Foo() {} };");
2142 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2143 EXPECT_TOKEN(Tokens[8], tok::identifier, TT_CtorDtorDeclName);
2144 EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_FunctionDeclarationLParen);
2145 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
2147 Tokens = annotate("struct Foo { [[deprecated]] ~Foo() {} };");
2148 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
2149 EXPECT_TOKEN(Tokens[9], tok::identifier, TT_CtorDtorDeclName);
2150 EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_FunctionDeclarationLParen);
2151 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
2153 Tokens = annotate("struct Foo { Foo() [[deprecated]] {} };");
2154 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2155 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
2156 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
2157 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
2159 Tokens = annotate("struct Foo { ~Foo() [[deprecated]] {} };");
2160 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
2161 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_CtorDtorDeclName);
2162 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_FunctionDeclarationLParen);
2163 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
2165 Tokens = annotate("struct Foo { [[deprecated]] explicit Foo() {} };");
2166 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
2167 EXPECT_TOKEN(Tokens[9], tok::identifier, TT_CtorDtorDeclName);
2168 EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_FunctionDeclarationLParen);
2169 EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
2171 Tokens = annotate("struct Foo { virtual [[deprecated]] ~Foo() {} };");
2172 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
2173 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
2174 EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
2175 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
2177 Tokens = annotate("Foo::Foo() {}");
2178 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2179 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
2180 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
2181 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
2183 Tokens = annotate("Foo::~Foo() {}");
2184 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2185 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
2186 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
2187 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
2189 Tokens = annotate("struct Test {\n"
2190 " Test()\n"
2191 " : l([] {\n"
2192 " Short::foo();\n"
2193 " }) {}\n"
2194 "};");
2195 ASSERT_EQ(Tokens.size(), 25u) << Tokens;
2196 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
2197 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
2198 EXPECT_TOKEN(Tokens[14], tok::identifier, TT_Unknown);
2201 TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) {
2202 auto Tokens = annotate("_Generic(x, int: 1, default: 0)");
2203 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2204 EXPECT_TOKEN(Tokens[0], tok::kw__Generic, TT_Unknown);
2205 EXPECT_TOKEN(Tokens[5], tok::colon, TT_GenericSelectionColon);
2206 EXPECT_TOKEN(Tokens[9], tok::colon, TT_GenericSelectionColon);
2209 TEST_F(TokenAnnotatorTest, UnderstandsTrailingReturnArrow) {
2210 auto Tokens = annotate("auto f() -> int;");
2211 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2212 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_TrailingReturnArrow);
2214 Tokens = annotate("auto operator->() -> int;");
2215 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2216 EXPECT_TOKEN(Tokens[2], tok::arrow, TT_OverloadedOperator);
2217 EXPECT_TOKEN(Tokens[5], tok::arrow, TT_TrailingReturnArrow);
2219 Tokens = annotate("auto operator++(int) -> int;");
2220 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2221 EXPECT_TOKEN(Tokens[6], tok::arrow, TT_TrailingReturnArrow);
2223 Tokens = annotate("auto operator=() -> int;");
2224 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2225 EXPECT_TOKEN(Tokens[5], tok::arrow, TT_TrailingReturnArrow);
2227 Tokens = annotate("auto operator=(int) -> int;");
2228 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2229 EXPECT_TOKEN(Tokens[6], tok::arrow, TT_TrailingReturnArrow);
2231 Tokens = annotate("auto foo() -> auto { return Val; }");
2232 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2233 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_TrailingReturnArrow);
2235 Tokens = annotate("struct S { auto bar() const -> int; };");
2236 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2237 EXPECT_TOKEN(Tokens[8], tok::arrow, TT_TrailingReturnArrow);
2239 // Not trailing return arrows
2240 Tokens = annotate("auto a = b->c;");
2241 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2242 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_Unknown);
2244 Tokens = annotate("auto a = (b)->c;");
2245 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2246 EXPECT_TOKEN(Tokens[6], tok::arrow, TT_Unknown);
2248 Tokens = annotate("auto a = b()->c;");
2249 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2250 EXPECT_TOKEN(Tokens[6], tok::arrow, TT_Unknown);
2252 Tokens = annotate("auto a = b->c();");
2253 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2254 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_Unknown);
2256 Tokens = annotate("decltype(auto) a = b()->c;");
2257 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2258 EXPECT_TOKEN(Tokens[9], tok::arrow, TT_Unknown);
2260 Tokens = annotate("void f() { auto a = b->c(); }");
2261 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2262 EXPECT_TOKEN(Tokens[9], tok::arrow, TT_Unknown);
2264 Tokens = annotate("void f() { auto a = b()->c; }");
2265 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2266 EXPECT_TOKEN(Tokens[11], tok::arrow, TT_Unknown);
2268 Tokens = annotate("#define P(ptr) auto p = (ptr)->p");
2269 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
2270 EXPECT_TOKEN(Tokens[12], tok::arrow, TT_Unknown);
2272 Tokens = annotate("void f() FOO(foo->bar);");
2273 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2274 EXPECT_TOKEN(Tokens[7], tok::arrow, TT_Unknown);
2276 Tokens = annotate("__attribute__((cold)) C() : Base(obj->func()) {}");
2277 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
2278 EXPECT_TOKEN(Tokens[13], tok::arrow, TT_Unknown);
2280 auto Style = getLLVMStyle();
2281 Style.StatementAttributeLikeMacros.push_back("emit");
2282 Tokens = annotate("emit foo()->bar;", Style);
2283 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2284 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_StatementAttributeLikeMacro);
2285 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_Unknown);
2287 // Mixed
2288 Tokens = annotate("auto f() -> int { auto a = b()->c; }");
2289 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
2290 EXPECT_TOKEN(Tokens[4], tok::arrow, TT_TrailingReturnArrow);
2291 EXPECT_TOKEN(Tokens[13], tok::arrow, TT_Unknown);
2294 TEST_F(TokenAnnotatorTest, UnderstandHashInMacro) {
2295 auto Tokens = annotate("#define Foo(Bar) \\\n"
2296 " { \\\n"
2297 " #Bar \\\n"
2298 " }");
2299 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2300 EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
2301 EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
2303 Tokens = annotate("#define Foo(Bar) \\\n"
2304 " { #Bar }");
2305 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2306 EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
2307 EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
2309 Tokens = annotate("#define FOO(typeName, realClass) \\\n"
2310 " {#typeName, foo<Foo>(new foo<realClass>(#typeName))}");
2311 ASSERT_EQ(Tokens.size(), 29u) << Tokens;
2312 EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
2313 EXPECT_BRACE_KIND(Tokens[27], BK_BracedInit);
2316 TEST_F(TokenAnnotatorTest, UnderstandsAttributeMacros) {
2317 // '__attribute__' has special handling.
2318 auto Tokens = annotate("__attribute__((X)) void Foo(void);");
2319 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2320 EXPECT_TOKEN(Tokens[0], tok::kw___attribute, TT_Unknown);
2321 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_AttributeLParen);
2322 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
2324 // Generic macro has no special handling in this location.
2325 Tokens = annotate("A(X) void Foo(void);");
2326 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2327 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
2328 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_Unknown);
2330 // Add a custom AttributeMacro. Test that it has the same behavior.
2331 FormatStyle Style = getLLVMStyle();
2332 Style.AttributeMacros.push_back("A");
2334 // An "AttributeMacro" gets annotated like '__attribute__'.
2335 Tokens = annotate("A(X) void Foo(void);", Style);
2336 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2337 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_AttributeMacro);
2338 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_AttributeLParen);
2339 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_AttributeRParen);
2342 TEST_F(TokenAnnotatorTest, UnderstandsAttributeMacrosOnObjCDecl) {
2343 // '__attribute__' has special handling.
2344 auto Tokens = annotate("__attribute__((X)) @interface Foo");
2345 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2346 EXPECT_TOKEN(Tokens[0], tok::kw___attribute, TT_Unknown);
2347 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_AttributeLParen);
2348 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
2350 // Generic macro has no special handling in this location.
2351 Tokens = annotate("A(X) @interface Foo");
2352 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2353 // Note: Don't check token-type as a random token in this position is hard to
2354 // reason about.
2355 EXPECT_TOKEN_KIND(Tokens[0], tok::identifier);
2356 EXPECT_TOKEN_KIND(Tokens[1], tok::l_paren);
2358 // Add a custom AttributeMacro. Test that it has the same behavior.
2359 FormatStyle Style = getLLVMStyle();
2360 Style.AttributeMacros.push_back("A");
2362 // An "AttributeMacro" gets annotated like '__attribute__'.
2363 Tokens = annotate("A(X) @interface Foo", Style);
2364 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2365 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_AttributeMacro);
2366 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_AttributeLParen);
2367 EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_AttributeRParen);
2370 TEST_F(TokenAnnotatorTest, UnderstandsAttributeMacrosOnObjCMethodDecl) {
2371 // '__attribute__' has special handling.
2372 auto Tokens = annotate("- (id)init __attribute__((X));");
2373 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2374 EXPECT_TOKEN(Tokens[5], tok::kw___attribute, TT_Unknown);
2375 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_AttributeLParen);
2376 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_AttributeRParen);
2378 // Generic macro has no special handling in this location.
2379 Tokens = annotate("- (id)init A(X);");
2380 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2381 // Note: Don't check token-type as a random token in this position is hard to
2382 // reason about.
2383 EXPECT_TOKEN_KIND(Tokens[5], tok::identifier);
2384 EXPECT_TOKEN_KIND(Tokens[6], tok::l_paren);
2386 // Add a custom AttributeMacro. Test that it has the same behavior.
2387 FormatStyle Style = getLLVMStyle();
2388 Style.AttributeMacros.push_back("A");
2390 // An "AttributeMacro" gets annotated like '__attribute__'.
2391 Tokens = annotate("- (id)init A(X);", Style);
2392 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2393 EXPECT_TOKEN(Tokens[5], tok::identifier, TT_AttributeMacro);
2394 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_AttributeLParen);
2395 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_AttributeRParen);
2398 TEST_F(TokenAnnotatorTest, UnderstandsAttributeMacrosOnObjCProperty) {
2399 // '__attribute__' has special handling.
2400 auto Tokens = annotate("@property(weak) id delegate __attribute__((X));");
2401 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
2402 EXPECT_TOKEN(Tokens[7], tok::kw___attribute, TT_Unknown);
2403 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_AttributeLParen);
2404 EXPECT_TOKEN(Tokens[12], tok::r_paren, TT_AttributeRParen);
2406 // Generic macro has no special handling in this location.
2407 Tokens = annotate("@property(weak) id delegate A(X);");
2408 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2409 // Note: Don't check token-type as a random token in this position is hard to
2410 // reason about.
2411 EXPECT_TOKEN_KIND(Tokens[7], tok::identifier);
2412 EXPECT_TOKEN_KIND(Tokens[8], tok::l_paren);
2414 // Add a custom AttributeMacro. Test that it has the same behavior.
2415 FormatStyle Style = getLLVMStyle();
2416 Style.AttributeMacros.push_back("A");
2418 // An "AttributeMacro" gets annotated like '__attribute__'.
2419 Tokens = annotate("@property(weak) id delegate A(X);", Style);
2420 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
2421 EXPECT_TOKEN(Tokens[7], tok::identifier, TT_AttributeMacro);
2422 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_AttributeLParen);
2423 EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_AttributeRParen);
2426 TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) {
2427 auto Annotate = [this](StringRef Code) {
2428 return annotate(Code, getLLVMStyle(FormatStyle::LK_Verilog));
2430 // Test that unary operators get labeled as such and that operators like '++'
2431 // don't get split.
2432 tok::TokenKind Unary[] = {tok::plus, tok::minus, tok::exclaim,
2433 tok::tilde, tok::amp, tok::pipe,
2434 tok::caret, tok::plusplus, tok::minusminus};
2435 for (auto Kind : Unary) {
2436 auto Tokens =
2437 Annotate(std::string("x = ") + tok::getPunctuatorSpelling(Kind) + "x;");
2438 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2439 EXPECT_TOKEN(Tokens[2], Kind, TT_UnaryOperator);
2441 // Operators formed by joining two operators like '^~'. For some of these
2442 // joined operators, we don't have a separate type, so we only test for their
2443 // precedence.
2444 std::pair<prec::Level, std::string> JoinedBinary[] = {
2445 {prec::Comma, "->"}, {prec::Comma, "<->"},
2446 {prec::Assignment, "+="}, {prec::Assignment, "-="},
2447 {prec::Assignment, "*="}, {prec::Assignment, "/="},
2448 {prec::Assignment, "%="}, {prec::Assignment, "&="},
2449 {prec::Assignment, "^="}, {prec::Assignment, "<<="},
2450 {prec::Assignment, ">>="}, {prec::Assignment, "<<<="},
2451 {prec::Assignment, ">>>="}, {prec::LogicalOr, "||"},
2452 {prec::LogicalAnd, "&&"}, {prec::Equality, "=="},
2453 {prec::Equality, "!="}, {prec::Equality, "==="},
2454 {prec::Equality, "!=="}, {prec::Equality, "==?"},
2455 {prec::Equality, "!=?"}, {prec::ExclusiveOr, "~^"},
2456 {prec::ExclusiveOr, "^~"},
2458 for (auto Operator : JoinedBinary) {
2459 auto Tokens = Annotate(std::string("x = x ") + Operator.second + " x;");
2460 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2461 EXPECT_TOKEN_TYPE(Tokens[3], TT_BinaryOperator);
2462 EXPECT_TOKEN_PRECEDENCE(Tokens[3], Operator.first);
2464 // '~^' and '^~' can be unary as well as binary operators.
2465 auto Tokens = Annotate("x = ~^x;");
2466 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2467 EXPECT_TOKEN_TYPE(Tokens[2], TT_UnaryOperator);
2468 Tokens = Annotate("x = ^~x;");
2469 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2470 EXPECT_TOKEN_TYPE(Tokens[2], TT_UnaryOperator);
2471 // The unary operators '~&' and '~|' can only be unary operators. The current
2472 // implementation treats each of them as separate unary '~' and '&' or '|'
2473 // operators, which is enough for formatting purposes. In FormatTestVerilog,
2474 // there is a test that there is no space in between. And even if a new line
2475 // is inserted between the '~' and '|', the semantic meaning is the same as
2476 // the joined operator, so the CanBreakBefore property doesn't need to be
2477 // false for the second operator.
2478 Tokens = Annotate("x = ~&x;");
2479 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2480 EXPECT_TOKEN(Tokens[2], tok::tilde, TT_UnaryOperator);
2481 EXPECT_TOKEN(Tokens[3], tok::amp, TT_UnaryOperator);
2482 Tokens = Annotate("x = ~|x;");
2483 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2484 EXPECT_TOKEN(Tokens[2], tok::tilde, TT_UnaryOperator);
2485 EXPECT_TOKEN(Tokens[3], tok::pipe, TT_UnaryOperator);
2486 // Test for block label colons.
2487 Tokens = Annotate("begin : x\n"
2488 "end : x");
2489 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2490 EXPECT_TOKEN(Tokens[1], tok::colon, TT_VerilogBlockLabelColon);
2491 EXPECT_TOKEN(Tokens[4], tok::colon, TT_VerilogBlockLabelColon);
2492 // Test that the dimension colon is annotated correctly.
2493 Tokens = Annotate("var [1 : 0] x;");
2494 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2495 EXPECT_TOKEN(Tokens[3], tok::colon, TT_BitFieldColon);
2496 Tokens = Annotate("extern function [1 : 0] x;");
2497 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2498 EXPECT_TOKEN(Tokens[4], tok::colon, TT_BitFieldColon);
2499 Tokens = Annotate("module test\n"
2500 " (input wire [7 : 0] a[7 : 0]);\n"
2501 "endmodule");
2502 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
2503 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_VerilogDimensionedTypeName);
2504 EXPECT_TOKEN(Tokens[7], tok::colon, TT_BitFieldColon);
2505 EXPECT_TOKEN(Tokens[13], tok::colon, TT_BitFieldColon);
2506 // Test case labels and ternary operators.
2507 Tokens = Annotate("case (x)\n"
2508 " x:\n"
2509 " x;\n"
2510 "endcase");
2511 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2512 EXPECT_TOKEN(Tokens[5], tok::colon, TT_CaseLabelColon);
2513 Tokens = Annotate("case (x)\n"
2514 " x ? x : x:\n"
2515 " x;\n"
2516 "endcase");
2517 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2518 EXPECT_TOKEN(Tokens[5], tok::question, TT_ConditionalExpr);
2519 EXPECT_TOKEN(Tokens[7], tok::colon, TT_ConditionalExpr);
2520 EXPECT_TOKEN(Tokens[9], tok::colon, TT_CaseLabelColon);
2521 // Non-blocking assignments.
2522 Tokens = Annotate("a <= b;");
2523 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
2524 EXPECT_TOKEN(Tokens[1], tok::lessequal, TT_BinaryOperator);
2525 EXPECT_TOKEN_PRECEDENCE(Tokens[1], prec::Assignment);
2526 Tokens = Annotate("if (a <= b) break;");
2527 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2528 EXPECT_TOKEN(Tokens[3], tok::lessequal, TT_BinaryOperator);
2529 EXPECT_TOKEN_PRECEDENCE(Tokens[3], prec::Relational);
2530 Tokens = Annotate("a <= b <= a;");
2531 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2532 EXPECT_TOKEN(Tokens[1], tok::lessequal, TT_BinaryOperator);
2533 EXPECT_TOKEN_PRECEDENCE(Tokens[1], prec::Assignment);
2534 EXPECT_TOKEN(Tokens[3], tok::lessequal, TT_BinaryOperator);
2535 EXPECT_TOKEN_PRECEDENCE(Tokens[3], prec::Relational);
2537 // Port lists in module instantiation.
2538 Tokens = Annotate("module_x instance_1(port_1), instance_2(port_2);");
2539 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2540 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_VerilogInstancePortLParen);
2541 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_VerilogInstancePortLParen);
2542 Tokens = Annotate("module_x #(parameter) instance_1(port_1), "
2543 "instance_2(port_2);");
2544 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2545 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_VerilogInstancePortLParen);
2546 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_VerilogInstancePortLParen);
2547 EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_VerilogInstancePortLParen);
2549 // Condition parentheses.
2550 Tokens = Annotate("assert (x);");
2551 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2552 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ConditionLParen);
2553 Tokens = Annotate("assert #0 (x);");
2554 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2555 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_ConditionLParen);
2556 Tokens = Annotate("assert final (x);");
2557 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2558 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_ConditionLParen);
2559 Tokens = Annotate("foreach (x[x]) continue;");
2560 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2561 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ConditionLParen);
2562 Tokens = Annotate("repeat (x[x]) continue;");
2563 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2564 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ConditionLParen);
2565 Tokens = Annotate("case (x) endcase;");
2566 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2567 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ConditionLParen);
2569 // Sensitivity list. The TT_Unknown type is clearly not binding for the
2570 // future, please adapt if those tokens get annotated. This test is only here
2571 // to prevent the comma from being annotated as TT_VerilogInstancePortComma.
2572 Tokens = Annotate("always @(posedge x, posedge y);");
2573 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2574 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_Unknown);
2575 EXPECT_TOKEN(Tokens[5], tok::comma, TT_Unknown);
2576 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
2578 // String literals in concatenation.
2579 Tokens = Annotate("x = {\"\"};");
2580 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2581 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_StringInConcatenation);
2582 Tokens = Annotate("x = {\"\", \"\"};");
2583 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2584 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_StringInConcatenation);
2585 EXPECT_TOKEN(Tokens[5], tok::string_literal, TT_StringInConcatenation);
2586 Tokens = Annotate("x = '{{\"\"}};");
2587 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2588 EXPECT_TOKEN(Tokens[5], tok::string_literal, TT_StringInConcatenation);
2589 // Cases where the string should not be annotated that type. Fix the
2590 // `TT_Unknown` if needed in the future.
2591 Tokens = Annotate("x = {\"\" == \"\"};");
2592 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2593 EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_Unknown);
2594 EXPECT_TOKEN(Tokens[5], tok::string_literal, TT_Unknown);
2595 Tokens = Annotate("x = {(\"\")};");
2596 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2597 EXPECT_TOKEN(Tokens[4], tok::string_literal, TT_Unknown);
2598 Tokens = Annotate("x = '{\"\"};");
2599 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2600 EXPECT_TOKEN(Tokens[4], tok::string_literal, TT_Unknown);
2602 // Module headers.
2603 Tokens = Annotate("module x();\n"
2604 "endmodule");
2605 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2606 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_VerilogMultiLineListLParen);
2607 Tokens = Annotate("function automatic `x x();\n"
2608 "endmodule");
2609 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2610 EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_VerilogMultiLineListLParen);
2611 Tokens = Annotate("function automatic x``x x();\n"
2612 "endmodule");
2613 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2614 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_VerilogMultiLineListLParen);
2615 Tokens = Annotate("function automatic x::x x();\n"
2616 "endmodule");
2617 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
2618 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_VerilogMultiLineListLParen);
2621 TEST_F(TokenAnnotatorTest, UnderstandTableGenTokens) {
2622 auto Style = getLLVMStyle(FormatStyle::LK_TableGen);
2623 ASSERT_TRUE(Style.isTableGen());
2625 TestLexer Lexer(Allocator, Buffers, Style);
2626 AdditionalKeywords Keywords(Lexer.IdentTable);
2627 auto Annotate = [&Lexer](StringRef Code) { return Lexer.annotate(Code); };
2629 // Additional keywords representation test.
2630 auto Tokens = Annotate("def foo : Bar<1>;");
2631 ASSERT_TRUE(Keywords.isTableGenKeyword(*Tokens[0]));
2632 ASSERT_TRUE(Keywords.isTableGenDefinition(*Tokens[0]));
2633 ASSERT_TRUE(Tokens[0]->is(Keywords.kw_def));
2634 ASSERT_TRUE(Tokens[1]->is(TT_StartOfName));
2636 // Code, the multiline string token.
2637 Tokens = Annotate("[{ code is multiline string }]");
2638 ASSERT_EQ(Tokens.size(), 2u) << Tokens;
2639 EXPECT_TOKEN(Tokens[0], tok::string_literal, TT_TableGenMultiLineString);
2640 EXPECT_FALSE(Tokens[0]->IsMultiline);
2641 // Case with multiple lines.
2642 Tokens = Annotate("[{ It can break\n"
2643 " across lines and the line breaks\n"
2644 " are retained in \n"
2645 " the string. }]");
2646 ASSERT_EQ(Tokens.size(), 2u) << Tokens;
2647 EXPECT_TOKEN(Tokens[0], tok::string_literal, TT_TableGenMultiLineString);
2648 EXPECT_EQ(Tokens[0]->ColumnWidth, sizeof("[{ It can break\n") - 1);
2649 EXPECT_TRUE(Tokens[0]->IsMultiline);
2650 EXPECT_EQ(Tokens[0]->LastLineColumnWidth, sizeof(" the string. }]") - 1);
2652 // Numeric literals.
2653 Tokens = Annotate("1234");
2654 EXPECT_TOKEN(Tokens[0], tok::numeric_constant, TT_Unknown);
2655 Tokens = Annotate("-1");
2656 EXPECT_TOKEN(Tokens[0], tok::numeric_constant, TT_Unknown);
2657 Tokens = Annotate("+1234");
2658 EXPECT_TOKEN(Tokens[0], tok::numeric_constant, TT_Unknown);
2659 Tokens = Annotate("0b0110");
2660 EXPECT_TOKEN(Tokens[0], tok::numeric_constant, TT_Unknown);
2661 Tokens = Annotate("0x1abC");
2662 EXPECT_TOKEN(Tokens[0], tok::numeric_constant, TT_Unknown);
2664 // Identifier tokens. In TableGen, identifiers can begin with a number.
2665 // In ambiguous cases, the lexer tries to lex it as a number.
2666 // Even if the try fails, it does not fall back to identifier lexing and
2667 // regard as an error.
2668 // The ambiguity is not documented. The result of those tests are based on the
2669 // implementation of llvm::TGLexer::LexToken.
2670 // This is invalid syntax of number, but not an identifier.
2671 Tokens = Annotate("0x1234x");
2672 EXPECT_TOKEN(Tokens[0], tok::numeric_constant, TT_Unknown);
2673 Tokens = Annotate("identifier");
2674 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
2675 // Identifier beginning with a number.
2676 Tokens = Annotate("0x");
2677 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
2678 Tokens = Annotate("2dVector");
2679 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
2680 Tokens = Annotate("01234Vector");
2681 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
2683 // Structured statements.
2684 Tokens = Annotate("class Foo {}");
2685 EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_FunctionLBrace);
2686 Tokens = Annotate("def Def: Foo {}");
2687 EXPECT_TOKEN(Tokens[2], tok::colon, TT_InheritanceColon);
2688 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2689 Tokens = Annotate("if cond then {} else {}");
2690 EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_ControlStatementLBrace);
2691 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_ElseLBrace);
2692 Tokens = Annotate("defset Foo Def2 = {}");
2693 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
2695 // Bang Operators.
2696 Tokens = Annotate("!foreach");
2697 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TableGenBangOperator);
2698 Tokens = Annotate("!if");
2699 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TableGenBangOperator);
2700 Tokens = Annotate("!cond");
2701 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TableGenCondOperator);
2703 auto AnnotateValue = [this, &Style](StringRef Code) {
2704 // Values are annotated only in specific context.
2705 auto Result = annotate(("def X { let V = " + Code + "; }").str(), Style);
2706 return decltype(Result){Result.begin() + 6, Result.end() - 3};
2708 // Both of bang/cond operators.
2709 Tokens = AnnotateValue("!cond(!eq(x, 0): 1, true: x)");
2710 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
2711 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TableGenCondOperator);
2712 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TableGenBangOperator);
2713 EXPECT_TOKEN(Tokens[8], tok::colon, TT_TableGenCondOperatorColon);
2714 EXPECT_TOKEN(Tokens[10], tok::comma, TT_TableGenCondOperatorComma);
2715 EXPECT_TOKEN(Tokens[12], tok::colon, TT_TableGenCondOperatorColon);
2716 // DAGArg values with operator identifier
2717 Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
2718 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2719 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpener);
2720 EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColon);
2721 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown); // $src1
2722 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListComma);
2723 EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColon);
2724 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2725 // List literal
2726 Tokens = AnnotateValue("[1, 2, 3]");
2727 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2728 EXPECT_TOKEN(Tokens[0], tok::l_square, TT_TableGenListOpener);
2729 EXPECT_TOKEN(Tokens[6], tok::r_square, TT_TableGenListCloser);
2730 // Suffixes of values
2731 Tokens = AnnotateValue("valid.field");
2732 ASSERT_EQ(Tokens.size(), 3u) << Tokens;
2733 EXPECT_TOKEN(Tokens[1], tok::period, TT_TableGenValueSuffix);
2734 // Code
2735 Tokens = AnnotateValue("[{ code is multiline string }]");
2736 ASSERT_EQ(Tokens.size(), 1u) << Tokens;
2737 EXPECT_TOKEN(Tokens[0], tok::string_literal, TT_TableGenMultiLineString);
2739 // The definition
2740 Tokens = annotate("def Def : Parent<Child> {}", Style);
2741 ASSERT_EQ(Tokens.size(), 10u) << Tokens; // This contains eof.
2742 // We use inheritance colon and function brace. They are enough.
2743 EXPECT_TOKEN(Tokens[2], tok::colon, TT_InheritanceColon);
2744 EXPECT_TOKEN(Tokens[4], tok::less, TT_TemplateOpener);
2745 EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
2746 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_FunctionLBrace);
2748 // DAGArg breaking options. They use different token types depending on what
2749 // is specified.
2750 Style.TableGenBreakInsideDAGArg = FormatStyle::DAS_BreakElements;
2752 // When TableGenBreakInsideDAGArg is DAS_BreakElements and
2753 // TableGenBreakingDAGArgOperators is not specified, it makes all the DAGArg
2754 // elements to have line break.
2755 Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
2756 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2757 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpenerToBreak);
2758 EXPECT_TOKEN(Tokens[1], tok::identifier,
2759 TT_TableGenDAGArgOperatorID); // ins
2760 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListCommaToBreak);
2761 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2763 Tokens = AnnotateValue("(other type1:$src1, type2:$src2)");
2764 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2765 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpenerToBreak);
2766 EXPECT_TOKEN(Tokens[1], tok::identifier,
2767 TT_TableGenDAGArgOperatorID); // other
2768 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListCommaToBreak);
2769 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2771 // For non-identifier operators, breaks after the operator.
2772 Tokens = AnnotateValue("(!cast<Type>(\"Name\") type1:$src1, type2:$src2)");
2773 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2774 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpenerToBreak);
2775 EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_TableGenDAGArgOperatorToBreak);
2776 EXPECT_TOKEN(Tokens[11], tok::comma, TT_TableGenDAGArgListCommaToBreak);
2777 EXPECT_TOKEN(Tokens[15], tok::r_paren, TT_TableGenDAGArgCloser);
2779 Style.TableGenBreakInsideDAGArg = FormatStyle::DAS_BreakAll;
2781 // When TableGenBreakInsideDAGArg is DAS_BreakAll and
2782 // TableGenBreakingDAGArgOperators is not specified, it makes all the DAGArg
2783 // to have line break inside it.
2784 Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
2785 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2786 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpenerToBreak);
2787 EXPECT_TOKEN(Tokens[1], tok::identifier,
2788 TT_TableGenDAGArgOperatorToBreak); // ins
2789 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListCommaToBreak);
2790 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2792 Tokens = AnnotateValue("(other type1:$src1, type2:$src2)");
2793 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2794 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpenerToBreak);
2795 EXPECT_TOKEN(Tokens[1], tok::identifier,
2796 TT_TableGenDAGArgOperatorToBreak); // other
2797 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListCommaToBreak);
2798 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2800 // If TableGenBreakingDAGArgOperators is specified, it is limited to the
2801 // specified operators.
2802 Style.TableGenBreakingDAGArgOperators = {"ins", "outs"};
2803 Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
2804 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2805 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpenerToBreak);
2806 EXPECT_TOKEN(Tokens[1], tok::identifier,
2807 TT_TableGenDAGArgOperatorToBreak); // ins
2808 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListCommaToBreak);
2809 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2811 Tokens = AnnotateValue("(other type1:$src1, type2:$src2)");
2812 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2813 EXPECT_TOKEN(Tokens[0], tok::l_paren, TT_TableGenDAGArgOpener);
2814 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown); // other
2815 EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListComma);
2816 EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2818 // If TableGenBreakingDAGArgOperators is enabled, it uses
2819 // TT_TableGenDAGArgListColonToAlign to annotate the colon to align.
2820 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled = true;
2821 Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
2822 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2823 EXPECT_TOKEN(Tokens[1], tok::identifier,
2824 TT_TableGenDAGArgOperatorToBreak); // ins
2825 EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColonToAlign);
2826 EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColonToAlign);
2828 Tokens = AnnotateValue("(other type1:$src1, type2:$src2)");
2829 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2830 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown); // other
2831 EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColon);
2832 EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColon);
2835 TEST_F(TokenAnnotatorTest, UnderstandConstructors) {
2836 auto Tokens = annotate("Class::Class() : BaseClass(), Member() {}");
2838 // The TT_Unknown is clearly not binding for the future, please adapt if those
2839 // tokens get annotated.
2840 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2841 EXPECT_TOKEN(Tokens[5], tok::colon, TT_CtorInitializerColon);
2842 EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown);
2843 EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_Unknown);
2844 EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
2845 EXPECT_TOKEN(Tokens[9], tok::comma, TT_CtorInitializerComma);
2846 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_Unknown);
2847 EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_Unknown);
2848 EXPECT_TOKEN(Tokens[12], tok::r_paren, TT_Unknown);
2849 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
2850 EXPECT_BRACE_KIND(Tokens[13], BK_Block);
2852 Tokens = annotate("Class::Class() : BaseClass{}, Member{} {}");
2853 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
2854 EXPECT_TOKEN(Tokens[5], tok::colon, TT_CtorInitializerColon);
2855 EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown);
2856 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_Unknown);
2857 EXPECT_TOKEN(Tokens[8], tok::r_brace, TT_Unknown);
2858 EXPECT_TOKEN(Tokens[9], tok::comma, TT_CtorInitializerComma);
2859 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_Unknown);
2860 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_Unknown);
2861 EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_Unknown);
2862 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
2863 EXPECT_BRACE_KIND(Tokens[13], BK_Block);
2865 Tokens = annotate("class Class {\n"
2866 " Class() : BaseClass() {\n"
2867 "#if 0\n"
2868 " // comment\n"
2869 "#endif\n"
2870 " }\n"
2871 " Class f();\n"
2872 "}");
2873 ASSERT_EQ(Tokens.size(), 25u) << Tokens;
2874 EXPECT_TOKEN(Tokens[6], tok::colon, TT_CtorInitializerColon);
2875 EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_FunctionLBrace);
2876 EXPECT_BRACE_KIND(Tokens[10], BK_Block);
2879 TEST_F(TokenAnnotatorTest, UnderstandsConditionParens) {
2880 auto Tokens = annotate("if (x) {}");
2881 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2882 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ConditionLParen);
2883 Tokens = annotate("if constexpr (x) {}");
2884 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2885 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_ConditionLParen);
2886 Tokens = annotate("if CONSTEXPR (x) {}");
2887 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2888 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_ConditionLParen);
2889 Tokens = annotate("if (x) {} else if (x) {}");
2890 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2891 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ConditionLParen);
2892 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_ConditionLParen);
2895 TEST_F(TokenAnnotatorTest, CSharpNullableTypes) {
2896 FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
2898 auto Tokens = annotate("int? a;", Style);
2899 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
2900 EXPECT_TOKEN(Tokens[1], tok::question, TT_CSharpNullable);
2902 Tokens = annotate("int? a = 1;", Style);
2903 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2904 EXPECT_TOKEN(Tokens[1], tok::question, TT_CSharpNullable);
2906 Tokens = annotate("int?)", Style);
2907 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
2908 EXPECT_TOKEN(Tokens[1], tok::question, TT_CSharpNullable);
2910 Tokens = annotate("int?>", Style);
2911 ASSERT_EQ(Tokens.size(), 4u) << Tokens;
2912 EXPECT_TOKEN(Tokens[1], tok::question, TT_CSharpNullable);
2914 Tokens = annotate("cond? id : id2", Style);
2915 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2916 EXPECT_TOKEN(Tokens[1], tok::question, TT_ConditionalExpr);
2918 Tokens = annotate("cond ? cond2 ? : id1 : id2", Style);
2919 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2920 EXPECT_TOKEN(Tokens[1], tok::question, TT_ConditionalExpr);
2923 TEST_F(TokenAnnotatorTest, UnderstandsLabels) {
2924 auto Tokens = annotate("{ x: break; }");
2925 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2926 EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2928 Tokens = annotate("{ case x: break; }");
2929 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2930 EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2932 Tokens = annotate("{ x: { break; } }");
2933 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2934 EXPECT_TOKEN(Tokens[2], tok::colon, TT_GotoLabelColon);
2936 Tokens = annotate("{ case x: { break; } }");
2937 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2938 EXPECT_TOKEN(Tokens[3], tok::colon, TT_CaseLabelColon);
2940 Tokens = annotate("#define FOO label:");
2941 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2942 EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
2944 Tokens = annotate("#define FOO \\\n"
2945 "label: \\\n"
2946 " break;");
2947 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2948 EXPECT_TOKEN(Tokens[4], tok::colon, TT_GotoLabelColon);
2951 TEST_F(TokenAnnotatorTest, UnderstandsNestedBlocks) {
2952 // The closing braces are not annotated. It doesn't seem to cause a problem.
2953 // So we only test for the opening braces.
2954 auto Tokens = annotate("{\n"
2955 " {\n"
2956 " { int a = 0; }\n"
2957 " }\n"
2958 " {}\n"
2959 "}");
2960 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2961 EXPECT_BRACE_KIND(Tokens[0], BK_Block);
2962 EXPECT_BRACE_KIND(Tokens[1], BK_Block);
2963 EXPECT_BRACE_KIND(Tokens[2], BK_Block);
2964 EXPECT_BRACE_KIND(Tokens[10], BK_Block);
2967 TEST_F(TokenAnnotatorTest, UnderstandDesignatedInitializers) {
2968 auto Tokens = annotate("SomeStruct { .a = 1 };");
2969 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2970 EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit);
2971 EXPECT_TOKEN(Tokens[2], tok::period, TT_DesignatedInitializerPeriod);
2973 Tokens = annotate("SomeStruct { .a = 1, .b = 2 };");
2974 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2975 EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit);
2976 EXPECT_TOKEN(Tokens[2], tok::period, TT_DesignatedInitializerPeriod);
2977 EXPECT_TOKEN(Tokens[7], tok::period, TT_DesignatedInitializerPeriod);
2979 Tokens = annotate("SomeStruct {\n"
2980 "#ifdef FOO\n"
2981 " .a = 1,\n"
2982 "#endif\n"
2983 " .b = 2\n"
2984 "};");
2985 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
2986 EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit);
2987 EXPECT_TOKEN(Tokens[5], tok::period, TT_DesignatedInitializerPeriod);
2988 EXPECT_TOKEN(Tokens[12], tok::period, TT_DesignatedInitializerPeriod);
2990 Tokens = annotate("SomeStruct {\n"
2991 "#if defined FOO\n"
2992 " .a = 1,\n"
2993 "#endif\n"
2994 " .b = 2\n"
2995 "};");
2996 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
2997 EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit);
2998 EXPECT_TOKEN(Tokens[6], tok::period, TT_DesignatedInitializerPeriod);
2999 EXPECT_TOKEN(Tokens[13], tok::period, TT_DesignatedInitializerPeriod);
3001 Tokens = annotate("Foo foo[] = {[0]{}};");
3002 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
3003 EXPECT_TOKEN(Tokens[6], tok::l_square, TT_DesignatedInitializerLSquare);
3004 EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
3007 TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
3008 auto Annotate = [this](StringRef Code) {
3009 return annotate(Code, getLLVMStyle(FormatStyle::LK_JavaScript));
3012 // Dictionary.
3013 auto Tokens = Annotate("var x = {'x' : 1, 'y' : 2};");
3014 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
3015 EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_DictLiteral);
3016 EXPECT_TOKEN(Tokens[4], tok::string_literal, TT_SelectorName);
3017 EXPECT_TOKEN(Tokens[5], tok::colon, TT_DictLiteral);
3018 EXPECT_TOKEN(Tokens[8], tok::string_literal, TT_SelectorName);
3019 EXPECT_TOKEN(Tokens[9], tok::colon, TT_DictLiteral);
3020 // Change when we need to annotate these.
3021 EXPECT_BRACE_KIND(Tokens[3], BK_Unknown);
3022 EXPECT_BRACE_KIND(Tokens[11], BK_Unknown);
3023 EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_Unknown);
3026 TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
3027 auto Tokens = annotate("bool foo __attribute__((unused));");
3028 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
3029 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
3030 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
3031 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_Unknown);
3032 EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_Unknown);
3033 EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_AttributeRParen);
3035 Tokens = annotate("bool foo __declspec(dllimport);");
3036 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3037 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
3038 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
3040 Tokens = annotate("bool __attribute__((unused)) foo;");
3041 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
3042 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
3043 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
3044 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_Unknown);
3045 EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_AttributeRParen);
3046 EXPECT_TOKEN(Tokens[7], tok::identifier, TT_StartOfName);
3048 Tokens = annotate("void __attribute__((x)) Foo();");
3049 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3050 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
3051 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
3052 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_Unknown);
3053 EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_AttributeRParen);
3054 EXPECT_TOKEN(Tokens[7], tok::identifier, TT_FunctionDeclarationName);
3055 EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_FunctionDeclarationLParen);
3057 FormatStyle Style = getLLVMStyle();
3058 Style.AttributeMacros.push_back("FOO");
3059 Tokens = annotate("bool foo FOO(unused);", Style);
3060 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3061 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_AttributeMacro);
3062 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
3063 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
3066 TEST_F(TokenAnnotatorTest, UnderstandsControlStatements) {
3067 auto Tokens = annotate("while (true) {}");
3068 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3069 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
3070 EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
3072 Tokens = annotate("for (;;) {}");
3073 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3074 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_ControlStatementLBrace);
3075 EXPECT_TOKEN(Tokens[6], tok::r_brace, TT_ControlStatementRBrace);
3077 Tokens = annotate("do {} while (true);");
3078 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3079 EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_ControlStatementLBrace);
3080 EXPECT_TOKEN(Tokens[2], tok::r_brace, TT_ControlStatementRBrace);
3082 Tokens = annotate("if (true) {} else if (false) {} else {}");
3083 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
3084 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
3085 EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
3086 EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_ControlStatementLBrace);
3087 EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_ControlStatementRBrace);
3088 EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_ElseLBrace);
3089 EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_ElseRBrace);
3091 Tokens = annotate("switch (foo) {}");
3092 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3093 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
3094 EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
3097 TEST_F(TokenAnnotatorTest, UnderstandsDoWhile) {
3098 auto Tokens = annotate("do { ++i; } while ( i > 5 );");
3099 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
3100 EXPECT_TOKEN(Tokens[6], tok::kw_while, TT_DoWhile);
3102 Tokens = annotate("do ++i; while ( i > 5 );");
3103 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3104 EXPECT_TOKEN(Tokens[4], tok::kw_while, TT_DoWhile);
3107 TEST_F(TokenAnnotatorTest, StartOfName) {
3108 auto Tokens = annotate("#pragma clang diagnostic push");
3109 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3110 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
3111 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
3112 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
3114 Tokens = annotate("#pragma clang diagnostic ignored \"-Wzero-length-array\"");
3115 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3116 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
3117 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
3118 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
3120 Tokens = annotate("#define FOO Foo foo");
3121 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3122 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
3123 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
3124 EXPECT_TOKEN(Tokens[4], tok::identifier, TT_StartOfName);
3126 Tokens = annotate("@interface NSCoder (TestCoder)");
3127 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3128 EXPECT_TOKEN(Tokens[0], tok::at, TT_ObjCDecl);
3129 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_StartOfName);
3131 auto Style = getLLVMStyle();
3132 Style.StatementAttributeLikeMacros.push_back("emit");
3133 Tokens = annotate("emit foo = 0;", Style);
3134 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3135 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_StatementAttributeLikeMacro);
3136 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown);
3139 TEST_F(TokenAnnotatorTest, BraceKind) {
3140 auto Tokens = annotate("void f() {};");
3141 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3142 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3143 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3144 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
3145 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3146 EXPECT_BRACE_KIND(Tokens[5], BK_Block);
3148 Tokens = annotate("class Foo<int> f() {}");
3149 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
3150 EXPECT_TOKEN(Tokens[5], tok::identifier, TT_FunctionDeclarationName);
3151 EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
3152 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
3153 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3154 EXPECT_BRACE_KIND(Tokens[9], BK_Block);
3156 Tokens = annotate("template <typename T> class Foo<T> f() {}");
3157 ASSERT_EQ(Tokens.size(), 16u) << Tokens;
3158 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
3159 EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
3160 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
3161 EXPECT_BRACE_KIND(Tokens[13], BK_Block);
3162 EXPECT_BRACE_KIND(Tokens[14], BK_Block);
3164 Tokens = annotate("void f() override {};");
3165 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3166 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3167 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3168 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
3169 EXPECT_BRACE_KIND(Tokens[5], BK_Block);
3170 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3172 Tokens = annotate("void f() noexcept(false) {};");
3173 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3174 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3175 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3176 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
3177 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3178 EXPECT_BRACE_KIND(Tokens[9], BK_Block);
3180 Tokens = annotate("auto f() -> void {};");
3181 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
3182 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3183 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3184 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
3185 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3186 EXPECT_BRACE_KIND(Tokens[7], BK_Block);
3188 Tokens = annotate("void f() { /**/ };");
3189 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3190 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3191 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3192 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
3193 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3194 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3196 Tokens = annotate("void f() { //\n"
3197 "};");
3198 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3199 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3200 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3201 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
3202 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3203 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3205 Tokens = annotate("void f() {\n"
3206 " //\n"
3207 "};");
3208 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3209 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
3210 EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
3211 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
3212 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3213 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3215 Tokens = annotate("struct Foo {\n"
3216 " Foo() {};\n"
3217 " ~Foo() {};\n"
3218 "};");
3219 ASSERT_EQ(Tokens.size(), 19u) << Tokens;
3220 EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
3221 EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
3222 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
3223 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3224 EXPECT_BRACE_KIND(Tokens[7], BK_Block);
3225 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
3226 EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
3227 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
3228 EXPECT_BRACE_KIND(Tokens[13], BK_Block);
3229 EXPECT_BRACE_KIND(Tokens[14], BK_Block);
3231 Tokens = annotate("{\n"
3232 " char *a[] = {\n"
3233 " /* abc */ \"abc\",\n"
3234 "#if FOO\n"
3235 " /* xyz */ \"xyz\",\n"
3236 "#endif\n"
3237 " /* last */ \"last\"};\n"
3238 "}");
3239 ASSERT_EQ(Tokens.size(), 25u) << Tokens;
3240 EXPECT_BRACE_KIND(Tokens[0], BK_Block);
3241 EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
3242 EXPECT_BRACE_KIND(Tokens[21], BK_BracedInit);
3244 Tokens =
3245 annotate("#define SCOP_STAT(NAME, DESC) \\\n"
3246 " {\"polly\", #NAME, \"Number of rejected regions: \" DESC}");
3247 ASSERT_EQ(Tokens.size(), 18u) << Tokens;
3248 EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
3249 EXPECT_BRACE_KIND(Tokens[16], BK_BracedInit);
3251 Tokens = annotate("struct {};");
3252 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
3253 EXPECT_BRACE_KIND(Tokens[1], BK_Block);
3254 EXPECT_BRACE_KIND(Tokens[2], BK_Block);
3256 Tokens = annotate("struct : Base {};");
3257 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3258 EXPECT_BRACE_KIND(Tokens[3], BK_Block);
3259 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3261 Tokens = annotate("struct Foo {};");
3262 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3263 EXPECT_BRACE_KIND(Tokens[2], BK_Block);
3264 EXPECT_BRACE_KIND(Tokens[3], BK_Block);
3266 Tokens = annotate("struct ::Foo {};");
3267 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3268 EXPECT_BRACE_KIND(Tokens[3], BK_Block);
3269 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3271 Tokens = annotate("struct NS::Foo {};");
3272 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3273 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3274 EXPECT_BRACE_KIND(Tokens[5], BK_Block);
3276 Tokens = annotate("struct Foo<int> {};");
3277 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3278 EXPECT_BRACE_KIND(Tokens[5], BK_Block);
3279 EXPECT_BRACE_KIND(Tokens[6], BK_Block);
3281 Tokens = annotate("struct Foo<int>::Bar {};");
3282 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
3283 EXPECT_BRACE_KIND(Tokens[7], BK_Block);
3284 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3286 Tokens = annotate("struct Foo<int> : Base {};");
3287 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
3288 EXPECT_BRACE_KIND(Tokens[7], BK_Block);
3289 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3291 Tokens = annotate("struct Foo final {};");
3292 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3293 EXPECT_BRACE_KIND(Tokens[3], BK_Block);
3294 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3296 Tokens = annotate("struct [[foo]] [[bar]] Foo final : Base1, Base2 {};");
3297 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
3298 EXPECT_BRACE_KIND(Tokens[17], BK_Block);
3299 EXPECT_BRACE_KIND(Tokens[18], BK_Block);
3301 Tokens = annotate("struct Foo x{};");
3302 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3303 EXPECT_BRACE_KIND(Tokens[3], BK_BracedInit);
3304 EXPECT_BRACE_KIND(Tokens[4], BK_BracedInit);
3306 Tokens = annotate("struct ::Foo x{};");
3307 ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3308 EXPECT_BRACE_KIND(Tokens[4], BK_BracedInit);
3309 EXPECT_BRACE_KIND(Tokens[5], BK_BracedInit);
3311 Tokens = annotate("struct NS::Foo x{};");
3312 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3313 EXPECT_BRACE_KIND(Tokens[5], BK_BracedInit);
3314 EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
3316 Tokens = annotate("struct Foo<int> x{};");
3317 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
3318 EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
3319 EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
3321 Tokens = annotate("#ifdef DEBUG_ENABLED\n"
3322 "#else\n"
3323 "#endif\n"
3324 "class RenderingServer : Object {\n"
3325 "#ifndef DISABLE_DEPRECATED\n"
3326 " enum Features {\n"
3327 " FEATURE_SHADERS,\n"
3328 " FEATURE_MULTITHREADED,\n"
3329 " };\n"
3330 "#endif\n"
3331 "};");
3332 ASSERT_EQ(Tokens.size(), 29u) << Tokens;
3333 EXPECT_BRACE_KIND(Tokens[11], BK_Block);
3334 EXPECT_BRACE_KIND(Tokens[17], BK_Block);
3335 EXPECT_BRACE_KIND(Tokens[22], BK_Block);
3336 EXPECT_BRACE_KIND(Tokens[26], BK_Block);
3338 Tokens = annotate("{\n"
3339 "#define M(x) \\\n"
3340 " return {#x};\n"
3341 "}");
3342 ASSERT_EQ(Tokens.size(), 15u) << Tokens;
3343 EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_BlockLBrace);
3344 EXPECT_BRACE_KIND(Tokens[0], BK_Block);
3345 EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
3346 EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
3347 EXPECT_BRACE_KIND(Tokens[13], BK_Block);
3349 Tokens = annotate("a = class extends goog.a {};",
3350 getGoogleStyle(FormatStyle::LK_JavaScript));
3351 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
3352 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_ClassLBrace);
3353 EXPECT_BRACE_KIND(Tokens[7], BK_Block);
3354 EXPECT_TOKEN(Tokens[8], tok::r_brace, TT_ClassRBrace);
3355 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3357 Tokens = annotate("a = class Foo extends goog.a {};",
3358 getGoogleStyle(FormatStyle::LK_JavaScript));
3359 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3360 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_ClassLBrace);
3361 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3362 EXPECT_TOKEN(Tokens[9], tok::r_brace, TT_ClassRBrace);
3363 EXPECT_BRACE_KIND(Tokens[9], BK_Block);
3365 Tokens = annotate("#define FOO(X) \\\n"
3366 " struct X##_tag_ {};");
3367 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
3368 EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_StructLBrace);
3369 EXPECT_BRACE_KIND(Tokens[10], BK_Block);
3370 EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_StructRBrace);
3371 EXPECT_BRACE_KIND(Tokens[11], BK_Block);
3373 Tokens = annotate("#define MACRO \\\n"
3374 " struct hash<type> { \\\n"
3375 " void f() { return; } \\\n"
3376 " };");
3377 ASSERT_EQ(Tokens.size(), 20u) << Tokens;
3378 EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
3379 EXPECT_BRACE_KIND(Tokens[8], BK_Block);
3380 EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
3381 EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
3382 EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
3383 EXPECT_BRACE_KIND(Tokens[13], BK_Block);
3384 EXPECT_BRACE_KIND(Tokens[16], BK_Block);
3385 EXPECT_TOKEN(Tokens[17], tok::r_brace, TT_StructRBrace);
3386 EXPECT_BRACE_KIND(Tokens[17], BK_Block);
3388 Tokens = annotate("#define MEMBER(NAME) NAME{\"\"}");
3389 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
3390 EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
3391 EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
3394 TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {
3395 auto Tokens = annotate("auto foo() -> enum En {}");
3396 ASSERT_EQ(Tokens.size(), 10u) << Tokens;
3397 EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_FunctionLBrace);
3400 TEST_F(TokenAnnotatorTest, BlockLBrace) {
3401 auto Tokens = annotate("{\n"
3402 " {\n"
3403 " foo();\n"
3404 " }\n"
3405 "}");
3406 ASSERT_EQ(Tokens.size(), 9u) << Tokens;
3407 EXPECT_TOKEN(Tokens[0], tok::l_brace, TT_BlockLBrace);
3408 EXPECT_BRACE_KIND(Tokens[0], BK_Block);
3409 EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_BlockLBrace);
3410 EXPECT_BRACE_KIND(Tokens[1], BK_Block);
3412 Tokens = annotate("void bar() {\n"
3413 " {\n"
3414 " foo();\n"
3415 " }\n"
3416 "}");
3417 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
3418 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
3419 EXPECT_BRACE_KIND(Tokens[4], BK_Block);
3420 EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_BlockLBrace);
3421 EXPECT_BRACE_KIND(Tokens[5], BK_Block);
3423 Tokens = annotate("[foo bar:{{0, 1}} baz:baz];",
3424 getLLVMStyle(FormatStyle::LK_ObjC));
3425 ASSERT_EQ(Tokens.size(), 17u) << Tokens;
3426 EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown); // Not TT_BlockLBrace.
3427 EXPECT_BRACE_KIND(Tokens[4], BK_Unknown); // Not BK_Block.
3428 EXPECT_BRACE_KIND(Tokens[5], BK_BracedInit);
3429 EXPECT_BRACE_KIND(Tokens[9], BK_Unknown); // Not BK_Block.
3430 EXPECT_BRACE_KIND(Tokens[10], BK_Unknown); // Not BK_Block.
3433 TEST_F(TokenAnnotatorTest, SwitchExpression) {
3434 auto Style = getLLVMStyle(FormatStyle::LK_Java);
3435 auto Tokens = annotate("i = switch (day) {\n"
3436 " case THURSDAY, SATURDAY -> 8;\n"
3437 " case WEDNESDAY -> 9;\n"
3438 " default -> 1;\n"
3439 "};",
3440 Style);
3441 ASSERT_EQ(Tokens.size(), 26u) << Tokens;
3442 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_SwitchExpressionLBrace);
3443 EXPECT_TOKEN(Tokens[7], tok::kw_case, TT_SwitchExpressionLabel);
3444 EXPECT_TOKEN(Tokens[11], tok::arrow, TT_CaseLabelArrow);
3445 EXPECT_TOKEN(Tokens[14], tok::kw_case, TT_SwitchExpressionLabel);
3446 EXPECT_TOKEN(Tokens[16], tok::arrow, TT_CaseLabelArrow);
3447 EXPECT_TOKEN(Tokens[19], tok::kw_default, TT_SwitchExpressionLabel);
3448 EXPECT_TOKEN(Tokens[20], tok::arrow, TT_CaseLabelArrow);
3451 TEST_F(TokenAnnotatorTest, CppAltOperatorKeywords) {
3452 auto Tokens = annotate("a = b and c;");
3453 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3454 EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
3456 Tokens = annotate("a = b and_eq c;");
3457 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3458 EXPECT_TOKEN(Tokens[3], tok::ampequal, TT_BinaryOperator);
3460 Tokens = annotate("a = b bitand c;");
3461 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3462 EXPECT_TOKEN(Tokens[3], tok::amp, TT_BinaryOperator);
3464 Tokens = annotate("a = b bitor c;");
3465 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3466 EXPECT_TOKEN(Tokens[3], tok::pipe, TT_BinaryOperator);
3468 Tokens = annotate("a = b compl c;");
3469 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3470 EXPECT_TOKEN(Tokens[3], tok::tilde, TT_UnaryOperator);
3472 Tokens = annotate("a = b not c;");
3473 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3474 EXPECT_TOKEN(Tokens[3], tok::exclaim, TT_UnaryOperator);
3476 Tokens = annotate("a = b not_eq c;");
3477 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3478 EXPECT_TOKEN(Tokens[3], tok::exclaimequal, TT_BinaryOperator);
3480 Tokens = annotate("a = b or c;");
3481 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3482 EXPECT_TOKEN(Tokens[3], tok::pipepipe, TT_BinaryOperator);
3484 Tokens = annotate("a = b or_eq c;");
3485 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3486 EXPECT_TOKEN(Tokens[3], tok::pipeequal, TT_BinaryOperator);
3488 Tokens = annotate("a = b xor c;");
3489 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3490 EXPECT_TOKEN(Tokens[3], tok::caret, TT_BinaryOperator);
3492 Tokens = annotate("a = b xor_eq c;");
3493 ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3494 EXPECT_TOKEN(Tokens[3], tok::caretequal, TT_BinaryOperator);
3496 Tokens = annotate("xor = foo;");
3497 ASSERT_EQ(Tokens.size(), 5u) << Tokens;
3498 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_Unknown);
3500 Tokens = annotate("int xor = foo;");
3501 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3502 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
3505 TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
3506 auto Tokens =
3507 annotate("Foo::Foo(int x, int y) try\n"
3508 " : foo{[] -> std::string { return {}; }(), x}, bar{y} {\n"
3509 "} catch (...) {\n"
3510 "}");
3511 ASSERT_EQ(Tokens.size(), 45u) << Tokens;
3512 EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
3513 EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
3514 EXPECT_TOKEN(Tokens[11], tok::colon, TT_CtorInitializerColon);
3515 EXPECT_TOKEN(Tokens[14], tok::l_square, TT_LambdaLSquare);
3516 EXPECT_TOKEN(Tokens[16], tok::arrow, TT_LambdaArrow);
3517 EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_LambdaLBrace);
3518 EXPECT_TOKEN(Tokens[31], tok::comma, TT_CtorInitializerComma);
3519 EXPECT_TOKEN(Tokens[36], tok::l_brace, TT_FunctionLBrace);
3522 TEST_F(TokenAnnotatorTest, TypenameMacro) {
3523 auto Style = getLLVMStyle();
3524 Style.TypenameMacros.push_back("STRUCT");
3526 auto Tokens = annotate("STRUCT(T, B) { int i; };", Style);
3527 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
3528 EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TypenameMacro);
3529 EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_TypeDeclarationParen);
3530 EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_TypeDeclarationParen);
3531 EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_Unknown);
3534 TEST_F(TokenAnnotatorTest, GNULanguageStandard) {
3535 auto Style = getGNUStyle();
3536 EXPECT_EQ(Style.Standard, FormatStyle::LS_Latest);
3538 auto Tokens = annotate("return 1 <=> 2;", Style);
3539 ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3540 EXPECT_TOKEN(Tokens[2], tok::spaceship, TT_BinaryOperator);
3543 TEST_F(TokenAnnotatorTest, SplitPenalty) {
3544 auto Style = getLLVMStyle();
3545 Style.ColumnLimit = 20;
3547 auto Tokens = annotate("class foo {\n"
3548 " auto bar()\n"
3549 " -> bool;\n"
3550 "};",
3551 Style);
3552 ASSERT_EQ(Tokens.size(), 13u) << Tokens;
3553 EXPECT_TOKEN(Tokens[7], tok::arrow, TT_TrailingReturnArrow);
3554 EXPECT_SPLIT_PENALTY(Tokens[7], 23u);
3557 TEST_F(TokenAnnotatorTest, TemplateName) {
3558 constexpr StringRef Code{"return Foo < A || B > (C ^ D);"};
3560 auto Tokens = annotate(Code);
3561 ASSERT_EQ(Tokens.size(), 14u) << Tokens;
3562 EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
3563 EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);
3565 auto Style = getLLVMStyle();
3566 Style.TemplateNames.push_back("Foo");
3568 Tokens = annotate(Code, Style);
3569 EXPECT_TOKEN(Tokens[1], tok::identifier, TT_TemplateName);
3570 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
3571 EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
3574 TEST_F(TokenAnnotatorTest, TemplateInstantiation) {
3575 auto Tokens = annotate("return FixedInt<N | M>();");
3576 ASSERT_EQ(Tokens.size(), 11u) << Tokens;
3577 EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
3578 EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
3580 Tokens = annotate("return std::conditional_t<T::value == U::value, T, U>{};");
3581 ASSERT_EQ(Tokens.size(), 21u) << Tokens;
3582 EXPECT_TOKEN(Tokens[4], tok::less, TT_TemplateOpener);
3583 EXPECT_TOKEN(Tokens[16], tok::greater, TT_TemplateCloser);
3585 Tokens =
3586 annotate("auto x{std::conditional_t<T::value == U::value, T, U>{}};");
3587 ASSERT_EQ(Tokens.size(), 24u) << Tokens;
3588 EXPECT_TOKEN(Tokens[6], tok::less, TT_TemplateOpener);
3589 EXPECT_TOKEN(Tokens[18], tok::greater, TT_TemplateCloser);
3592 TEST_F(TokenAnnotatorTest, SwitchInMacroArgument) {
3593 auto Tokens = annotate("FOOBAR(switch);\n"
3594 "void f() {}");
3595 ASSERT_EQ(Tokens.size(), 12u) << Tokens;
3596 EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_FunctionLBrace);
3599 } // namespace
3600 } // namespace format
3601 } // namespace clang