1 //===- unittest/Format/TokenAnnotatorTest.cpp - Formatting unit tests -----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Format/Format.h"
11 #include "FormatTestUtils.h"
12 #include "TestLexer.h"
13 #include "gtest/gtest.h"
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();
27 class TokenAnnotatorTest
: public ::testing::Test
{
29 TokenList
annotate(llvm::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_TOKEN(FormatTok, Kind, Type) \
47 EXPECT_TOKEN_KIND(FormatTok, Kind); \
48 EXPECT_TOKEN_TYPE(FormatTok, Type); \
51 TEST_F(TokenAnnotatorTest
, UnderstandsUsesOfStarAndAmp
) {
52 auto Tokens
= annotate("auto x = [](const decltype(x) &ptr) {};");
53 EXPECT_EQ(Tokens
.size(), 18u) << Tokens
;
54 EXPECT_TOKEN(Tokens
[7], tok::kw_decltype
, TT_Unknown
);
55 EXPECT_TOKEN(Tokens
[8], tok::l_paren
, TT_TypeDeclarationParen
);
56 EXPECT_TOKEN(Tokens
[9], tok::identifier
, TT_Unknown
);
57 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_TypeDeclarationParen
);
58 EXPECT_TOKEN(Tokens
[11], tok::amp
, TT_PointerOrReference
);
60 Tokens
= annotate("auto x = [](const decltype(x) *ptr) {};");
61 EXPECT_EQ(Tokens
.size(), 18u) << Tokens
;
62 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_TypeDeclarationParen
);
63 EXPECT_TOKEN(Tokens
[11], tok::star
, TT_PointerOrReference
);
65 Tokens
= annotate("#define lambda [](const decltype(x) &ptr) {}");
66 EXPECT_EQ(Tokens
.size(), 17u) << Tokens
;
67 EXPECT_TOKEN(Tokens
[7], tok::kw_decltype
, TT_Unknown
);
68 EXPECT_TOKEN(Tokens
[8], tok::l_paren
, TT_TypeDeclarationParen
);
69 EXPECT_TOKEN(Tokens
[9], tok::identifier
, TT_Unknown
);
70 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_TypeDeclarationParen
);
71 EXPECT_TOKEN(Tokens
[11], tok::amp
, TT_PointerOrReference
);
73 Tokens
= annotate("#define lambda [](const decltype(x) *ptr) {}");
74 EXPECT_EQ(Tokens
.size(), 17u) << Tokens
;
75 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_TypeDeclarationParen
);
76 EXPECT_TOKEN(Tokens
[11], tok::star
, TT_PointerOrReference
);
78 Tokens
= annotate("void f() {\n"
79 " while (p < a && *p == 'a')\n"
82 EXPECT_EQ(Tokens
.size(), 21u) << Tokens
;
83 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_BinaryOperator
);
84 EXPECT_TOKEN(Tokens
[11], tok::star
, TT_UnaryOperator
);
86 Tokens
= annotate("case *x:");
87 EXPECT_EQ(Tokens
.size(), 5u) << Tokens
;
88 EXPECT_TOKEN(Tokens
[1], tok::star
, TT_UnaryOperator
);
89 Tokens
= annotate("case &x:");
90 EXPECT_EQ(Tokens
.size(), 5u) << Tokens
;
91 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_UnaryOperator
);
93 Tokens
= annotate("bool b = 3 == int{3} && true;");
94 EXPECT_EQ(Tokens
.size(), 13u) << Tokens
;
95 EXPECT_TOKEN(Tokens
[9], tok::ampamp
, TT_BinaryOperator
);
97 Tokens
= annotate("struct {\n"
99 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
100 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_PointerOrReference
);
101 Tokens
= annotate("union {\n"
103 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
104 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_PointerOrReference
);
105 Tokens
= annotate("class {\n"
107 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
108 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_PointerOrReference
);
110 Tokens
= annotate("struct {\n"
112 EXPECT_EQ(Tokens
.size(), 10u) << Tokens
;
113 EXPECT_TOKEN(Tokens
[3], tok::ampamp
, TT_PointerOrReference
);
114 Tokens
= annotate("union {\n"
116 EXPECT_EQ(Tokens
.size(), 10u) << Tokens
;
117 EXPECT_TOKEN(Tokens
[3], tok::ampamp
, TT_PointerOrReference
);
118 Tokens
= annotate("class {\n"
120 EXPECT_EQ(Tokens
.size(), 10u) << Tokens
;
121 EXPECT_TOKEN(Tokens
[3], tok::ampamp
, TT_PointerOrReference
);
122 Tokens
= annotate("int i = int{42} * 2;");
123 EXPECT_EQ(Tokens
.size(), 11u) << Tokens
;
124 EXPECT_TOKEN(Tokens
[7], tok::star
, TT_BinaryOperator
);
126 Tokens
= annotate("delete[] *ptr;");
127 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
128 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_UnaryOperator
);
129 Tokens
= annotate("delete[] **ptr;");
130 EXPECT_EQ(Tokens
.size(), 8u) << Tokens
;
131 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_UnaryOperator
);
132 EXPECT_TOKEN(Tokens
[4], tok::star
, TT_UnaryOperator
);
133 Tokens
= annotate("delete[] *(ptr);");
134 EXPECT_EQ(Tokens
.size(), 9u) << Tokens
;
135 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_UnaryOperator
);
137 Tokens
= annotate("void f() { void (*fnptr)(char* foo); }");
138 EXPECT_EQ(Tokens
.size(), 18u) << Tokens
;
139 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_FunctionTypeLParen
);
140 // FIXME: The star of a function pointer probably makes more sense as
141 // TT_PointerOrReference.
142 EXPECT_TOKEN(Tokens
[7], tok::star
, TT_UnaryOperator
);
143 EXPECT_TOKEN(Tokens
[12], tok::star
, TT_PointerOrReference
);
145 Tokens
= annotate("void f() { void (*fnptr)(t* foo); }");
146 EXPECT_EQ(Tokens
.size(), 18u) << Tokens
;
147 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_FunctionTypeLParen
);
148 EXPECT_TOKEN(Tokens
[7], tok::star
, TT_UnaryOperator
);
149 EXPECT_TOKEN(Tokens
[12], tok::star
, TT_PointerOrReference
);
151 Tokens
= annotate("int f3() { return sizeof(Foo&); }");
152 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
153 EXPECT_TOKEN(Tokens
[9], tok::amp
, TT_PointerOrReference
);
155 Tokens
= annotate("int f4() { return sizeof(Foo&&); }");
156 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
157 EXPECT_TOKEN(Tokens
[9], tok::ampamp
, TT_PointerOrReference
);
159 Tokens
= annotate("void f5() { int f6(Foo&, Bar&); }");
160 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
161 EXPECT_TOKEN(Tokens
[9], tok::amp
, TT_PointerOrReference
);
162 EXPECT_TOKEN(Tokens
[12], tok::amp
, TT_PointerOrReference
);
164 Tokens
= annotate("void f7() { int f8(Foo&&, Bar&&); }");
165 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
166 EXPECT_TOKEN(Tokens
[9], tok::ampamp
, TT_PointerOrReference
);
167 EXPECT_TOKEN(Tokens
[12], tok::ampamp
, TT_PointerOrReference
);
169 Tokens
= annotate("Type1 &val1 = val2;");
170 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
171 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_PointerOrReference
);
173 Tokens
= annotate("Type1 *val1 = &val2;");
174 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
175 EXPECT_TOKEN(Tokens
[1], tok::star
, TT_PointerOrReference
);
176 EXPECT_TOKEN(Tokens
[4], tok::amp
, TT_UnaryOperator
);
178 Tokens
= annotate("val1 & val2;");
179 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
180 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
182 Tokens
= annotate("val1 & val2.member;");
183 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
184 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
186 Tokens
= annotate("val1 & val2.*member;");
187 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
188 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
190 Tokens
= annotate("val1.*member & val2;");
191 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
192 EXPECT_TOKEN(Tokens
[3], tok::amp
, TT_BinaryOperator
);
194 Tokens
= annotate("val1 & val2->*member;");
195 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
196 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
198 Tokens
= annotate("val1->member & val2;");
199 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
200 EXPECT_TOKEN(Tokens
[3], tok::amp
, TT_BinaryOperator
);
202 Tokens
= annotate("val1 & val2 & val3;");
203 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
204 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
205 EXPECT_TOKEN(Tokens
[3], tok::amp
, TT_BinaryOperator
);
207 Tokens
= annotate("val1 & val2 // comment\n"
209 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
210 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
211 EXPECT_TOKEN(Tokens
[4], tok::amp
, TT_BinaryOperator
);
214 annotate("val1 & val2.member & val3.member() & val4 & val5->member;");
215 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
216 EXPECT_TOKEN(Tokens
[1], tok::amp
, TT_BinaryOperator
);
217 EXPECT_TOKEN(Tokens
[5], tok::amp
, TT_BinaryOperator
);
218 EXPECT_TOKEN(Tokens
[11], tok::amp
, TT_BinaryOperator
);
219 EXPECT_TOKEN(Tokens
[13], tok::amp
, TT_BinaryOperator
);
221 Tokens
= annotate("class c {\n"
222 " void func(type &a) { a & member; }\n"
223 " anotherType &member;\n"
225 ASSERT_EQ(Tokens
.size(), 22u) << Tokens
;
226 EXPECT_TOKEN(Tokens
[7], tok::amp
, TT_PointerOrReference
);
227 EXPECT_TOKEN(Tokens
[12], tok::amp
, TT_BinaryOperator
);
228 EXPECT_TOKEN(Tokens
[17], tok::amp
, TT_PointerOrReference
);
230 Tokens
= annotate("struct S {\n"
231 " auto Mem = C & D;\n"
233 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
234 EXPECT_TOKEN(Tokens
[7], tok::amp
, TT_BinaryOperator
);
237 annotate("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
238 ASSERT_EQ(Tokens
.size(), 23u) << Tokens
;
239 EXPECT_TOKEN(Tokens
[15], tok::ampamp
, TT_BinaryOperator
);
241 Tokens
= annotate("template <typename T> struct S {\n"
242 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
244 ASSERT_EQ(Tokens
.size(), 30u) << Tokens
;
245 EXPECT_TOKEN(Tokens
[14], tok::ampamp
, TT_BinaryOperator
);
247 Tokens
= annotate("template <bool B = C && D> struct S {};");
248 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
249 EXPECT_TOKEN(Tokens
[6], tok::ampamp
, TT_BinaryOperator
);
251 Tokens
= annotate("template <typename T, bool B = C && D> struct S {};");
252 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
253 EXPECT_TOKEN(Tokens
[9], tok::ampamp
, TT_BinaryOperator
);
255 Tokens
= annotate("template <typename T, typename U = T&&> struct S {};");
256 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
257 EXPECT_TOKEN(Tokens
[9], tok::ampamp
, TT_PointerOrReference
);
259 Tokens
= annotate("template <typename T = int (*)(int)> struct S {};");
260 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
261 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_FunctionTypeLParen
);
262 EXPECT_TOKEN(Tokens
[7], tok::star
, TT_PointerOrReference
);
264 Tokens
= annotate("Foo<A && B> a = {};");
265 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
266 EXPECT_TOKEN(Tokens
[3], tok::ampamp
, TT_BinaryOperator
);
268 Tokens
= annotate("Foo<A &&> a = {};");
269 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
270 EXPECT_TOKEN(Tokens
[3], tok::ampamp
, TT_PointerOrReference
);
272 Tokens
= annotate("template <enable_if_t<foo && !bar>* = nullptr> void f();");
273 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
274 EXPECT_TOKEN(Tokens
[5], tok::ampamp
, TT_BinaryOperator
);
277 annotate("auto foo() noexcept(noexcept(bar()) && "
278 "trait<std::decay_t<decltype(bar())>> && noexcept(baz())) {}");
279 EXPECT_EQ(Tokens
.size(), 38u) << Tokens
;
280 EXPECT_TOKEN(Tokens
[12], tok::ampamp
, TT_BinaryOperator
);
281 EXPECT_TOKEN(Tokens
[27], tok::ampamp
, TT_BinaryOperator
);
283 Tokens
= annotate("foo = *i < *j && *j > *k;");
284 EXPECT_EQ(Tokens
.size(), 15u) << Tokens
;
285 EXPECT_TOKEN(Tokens
[4], tok::less
, TT_BinaryOperator
);
286 EXPECT_TOKEN(Tokens
[7], tok::ampamp
, TT_BinaryOperator
);
287 EXPECT_TOKEN(Tokens
[10], tok::greater
, TT_BinaryOperator
);
289 FormatStyle Style
= getLLVMStyle();
290 Style
.TypeNames
.push_back("MYI");
291 Tokens
= annotate("if (MYI *p{nullptr})", Style
);
292 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
293 EXPECT_TOKEN(Tokens
[2], tok::identifier
, TT_TypeName
);
294 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_PointerOrReference
);
296 Style
.TypeNames
.push_back("Class");
297 Tokens
= annotate("if (Class *obj {getObj()})", Style
);
298 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
299 EXPECT_TOKEN(Tokens
[2], tok::identifier
, TT_TypeName
);
300 EXPECT_TOKEN(Tokens
[3], tok::star
, TT_PointerOrReference
);
303 TEST_F(TokenAnnotatorTest
, UnderstandsUsesOfPlusAndMinus
) {
304 auto Tokens
= annotate("x - 0");
305 ASSERT_EQ(Tokens
.size(), 4u) << Tokens
;
306 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_BinaryOperator
);
307 Tokens
= annotate("0 + 0");
308 ASSERT_EQ(Tokens
.size(), 4u) << Tokens
;
309 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_BinaryOperator
);
310 Tokens
= annotate("x + +0");
311 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
312 EXPECT_TOKEN(Tokens
[2], tok::plus
, TT_UnaryOperator
);
313 Tokens
= annotate("x ? -0 : +0");
314 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
315 EXPECT_TOKEN(Tokens
[2], tok::minus
, TT_UnaryOperator
);
316 EXPECT_TOKEN(Tokens
[5], tok::plus
, TT_UnaryOperator
);
317 Tokens
= annotate("(-0)");
318 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
319 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
320 Tokens
= annotate("0, -0");
321 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
322 EXPECT_TOKEN(Tokens
[2], tok::minus
, TT_UnaryOperator
);
323 Tokens
= annotate("for (; -1;) {\n}");
324 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
325 EXPECT_TOKEN(Tokens
[3], tok::minus
, TT_UnaryOperator
);
326 Tokens
= annotate("x = -1;");
327 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
328 EXPECT_TOKEN(Tokens
[2], tok::minus
, TT_UnaryOperator
);
329 Tokens
= annotate("x[-1]");
330 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
331 EXPECT_TOKEN(Tokens
[2], tok::minus
, TT_UnaryOperator
);
332 Tokens
= annotate("x = {-1};");
333 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
334 EXPECT_TOKEN(Tokens
[3], tok::minus
, TT_UnaryOperator
);
335 Tokens
= annotate("case -x:");
336 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
337 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
338 Tokens
= annotate("co_await -x;");
339 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
340 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
341 Tokens
= annotate("co_return -x;");
342 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
343 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
344 Tokens
= annotate("co_yield -x;");
345 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
346 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
347 Tokens
= annotate("delete -x;");
348 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
349 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
350 Tokens
= annotate("return -x;");
351 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
352 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
353 Tokens
= annotate("throw -x;");
354 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
355 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
356 Tokens
= annotate("sizeof -x");
357 ASSERT_EQ(Tokens
.size(), 4u) << Tokens
;
358 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
359 Tokens
= annotate("co_await +x;");
360 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
361 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
362 Tokens
= annotate("co_return +x;");
363 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
364 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
365 Tokens
= annotate("co_yield +x;");
366 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
367 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
368 Tokens
= annotate("delete +x;");
369 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
370 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
371 Tokens
= annotate("return +x;");
372 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
373 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
374 Tokens
= annotate("throw +x;");
375 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
376 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
377 Tokens
= annotate("sizeof +x");
378 ASSERT_EQ(Tokens
.size(), 4u) << Tokens
;
379 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
380 Tokens
= annotate("(int)-x");
381 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
382 EXPECT_TOKEN(Tokens
[3], tok::minus
, TT_UnaryOperator
);
383 Tokens
= annotate("(-x)");
384 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
385 EXPECT_TOKEN(Tokens
[1], tok::minus
, TT_UnaryOperator
);
386 Tokens
= annotate("!+x");
387 ASSERT_EQ(Tokens
.size(), 4u) << Tokens
;
388 EXPECT_TOKEN(Tokens
[0], tok::exclaim
, TT_UnaryOperator
);
389 EXPECT_TOKEN(Tokens
[1], tok::plus
, TT_UnaryOperator
);
392 TEST_F(TokenAnnotatorTest
, UnderstandsClasses
) {
393 auto Tokens
= annotate("class C {};");
394 EXPECT_EQ(Tokens
.size(), 6u) << Tokens
;
395 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_ClassLBrace
);
396 EXPECT_TOKEN(Tokens
[3], tok::r_brace
, TT_ClassRBrace
);
398 Tokens
= annotate("const class C {} c;");
399 EXPECT_EQ(Tokens
.size(), 8u) << Tokens
;
400 EXPECT_TOKEN(Tokens
[3], tok::l_brace
, TT_ClassLBrace
);
401 EXPECT_TOKEN(Tokens
[4], tok::r_brace
, TT_ClassRBrace
);
403 Tokens
= annotate("const class {} c;");
404 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
405 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_ClassLBrace
);
406 EXPECT_TOKEN(Tokens
[3], tok::r_brace
, TT_ClassRBrace
);
408 Tokens
= annotate("class [[deprecated(\"\")]] C { int i; };");
409 EXPECT_EQ(Tokens
.size(), 17u) << Tokens
;
410 EXPECT_TOKEN(Tokens
[10], tok::l_brace
, TT_ClassLBrace
);
411 EXPECT_TOKEN(Tokens
[14], tok::r_brace
, TT_ClassRBrace
);
414 TEST_F(TokenAnnotatorTest
, UnderstandsStructs
) {
415 auto Tokens
= annotate("struct S {};");
416 EXPECT_EQ(Tokens
.size(), 6u) << Tokens
;
417 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_StructLBrace
);
418 EXPECT_TOKEN(Tokens
[3], tok::r_brace
, TT_StructRBrace
);
420 Tokens
= annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
421 EXPECT_EQ(Tokens
.size(), 15u) << Tokens
;
422 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_StructLBrace
);
423 EXPECT_TOKEN(Tokens
[12], tok::r_brace
, TT_StructRBrace
);
425 Tokens
= annotate("struct [[deprecated]] [[nodiscard]] C { int i; };");
426 EXPECT_EQ(Tokens
.size(), 19u) << Tokens
;
427 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_StructLBrace
);
428 EXPECT_TOKEN(Tokens
[16], tok::r_brace
, TT_StructRBrace
);
430 Tokens
= annotate("template <typename T> struct S<const T[N]> {};");
431 EXPECT_EQ(Tokens
.size(), 18u) << Tokens
;
432 EXPECT_TOKEN(Tokens
[7], tok::less
, TT_TemplateOpener
);
433 EXPECT_TOKEN(Tokens
[10], tok::l_square
, TT_ArraySubscriptLSquare
);
434 EXPECT_TOKEN(Tokens
[13], tok::greater
, TT_TemplateCloser
);
435 EXPECT_TOKEN(Tokens
[14], tok::l_brace
, TT_StructLBrace
);
436 EXPECT_TOKEN(Tokens
[15], tok::r_brace
, TT_StructRBrace
);
438 Tokens
= annotate("template <typename T> struct S<T const[N]> {};");
439 EXPECT_EQ(Tokens
.size(), 18u) << Tokens
;
440 EXPECT_TOKEN(Tokens
[7], tok::less
, TT_TemplateOpener
);
441 EXPECT_TOKEN(Tokens
[10], tok::l_square
, TT_ArraySubscriptLSquare
);
442 EXPECT_TOKEN(Tokens
[13], tok::greater
, TT_TemplateCloser
);
443 EXPECT_TOKEN(Tokens
[14], tok::l_brace
, TT_StructLBrace
);
444 EXPECT_TOKEN(Tokens
[15], tok::r_brace
, TT_StructRBrace
);
446 Tokens
= annotate("template <typename T, unsigned n> struct S<T const[n]> {\n"
447 " void f(T const (&a)[n]);\n"
449 EXPECT_EQ(Tokens
.size(), 35u) << Tokens
;
450 EXPECT_TOKEN(Tokens
[10], tok::less
, TT_TemplateOpener
);
451 EXPECT_TOKEN(Tokens
[13], tok::l_square
, TT_ArraySubscriptLSquare
);
452 EXPECT_TOKEN(Tokens
[16], tok::greater
, TT_TemplateCloser
);
453 EXPECT_TOKEN(Tokens
[17], tok::l_brace
, TT_StructLBrace
);
454 EXPECT_TOKEN(Tokens
[23], tok::l_paren
, TT_FunctionTypeLParen
);
455 EXPECT_TOKEN(Tokens
[24], tok::amp
, TT_UnaryOperator
);
456 EXPECT_TOKEN(Tokens
[27], tok::l_square
, TT_ArraySubscriptLSquare
);
457 EXPECT_TOKEN(Tokens
[32], tok::r_brace
, TT_StructRBrace
);
460 TEST_F(TokenAnnotatorTest
, UnderstandsUnions
) {
461 auto Tokens
= annotate("union U {};");
462 EXPECT_EQ(Tokens
.size(), 6u) << Tokens
;
463 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_UnionLBrace
);
464 EXPECT_TOKEN(Tokens
[3], tok::r_brace
, TT_UnionRBrace
);
466 Tokens
= annotate("union U { void f() { return; } };");
467 EXPECT_EQ(Tokens
.size(), 14u) << Tokens
;
468 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_UnionLBrace
);
469 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_FunctionLBrace
);
470 EXPECT_TOKEN(Tokens
[11], tok::r_brace
, TT_UnionRBrace
);
473 TEST_F(TokenAnnotatorTest
, UnderstandsEnums
) {
474 auto Tokens
= annotate("enum E {};");
475 EXPECT_EQ(Tokens
.size(), 6u) << Tokens
;
476 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_EnumLBrace
);
477 EXPECT_TOKEN(Tokens
[3], tok::r_brace
, TT_EnumRBrace
);
480 TEST_F(TokenAnnotatorTest
, UnderstandsDefaultedAndDeletedFunctions
) {
481 auto Tokens
= annotate("auto operator<=>(const T &) const & = default;");
482 EXPECT_EQ(Tokens
.size(), 14u) << Tokens
;
483 EXPECT_TOKEN(Tokens
[9], tok::amp
, TT_PointerOrReference
);
485 Tokens
= annotate("template <typename T> void F(T) && = delete;");
486 EXPECT_EQ(Tokens
.size(), 15u) << Tokens
;
487 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_PointerOrReference
);
490 TEST_F(TokenAnnotatorTest
, UnderstandsVariables
) {
492 annotate("inline bool var = is_integral_v<int> && is_signed_v<int>;");
493 EXPECT_EQ(Tokens
.size(), 15u) << Tokens
;
494 EXPECT_TOKEN(Tokens
[8], tok::ampamp
, TT_BinaryOperator
);
497 TEST_F(TokenAnnotatorTest
, UnderstandsVariableTemplates
) {
499 annotate("template <typename T> "
500 "inline bool var = is_integral_v<int> && is_signed_v<int>;");
501 EXPECT_EQ(Tokens
.size(), 20u) << Tokens
;
502 EXPECT_TOKEN(Tokens
[13], tok::ampamp
, TT_BinaryOperator
);
505 TEST_F(TokenAnnotatorTest
, UnderstandsTemplatesInMacros
) {
507 annotate("#define FOO(typeName) \\\n"
508 " { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }");
509 ASSERT_EQ(Tokens
.size(), 27u) << Tokens
;
510 EXPECT_TOKEN(Tokens
[11], tok::less
, TT_TemplateOpener
);
511 EXPECT_TOKEN(Tokens
[13], tok::greater
, TT_TemplateCloser
);
512 EXPECT_TOKEN(Tokens
[17], tok::less
, TT_TemplateOpener
);
513 EXPECT_TOKEN(Tokens
[19], tok::greater
, TT_TemplateCloser
);
516 TEST_F(TokenAnnotatorTest
, UnderstandsGreaterAfterTemplateCloser
) {
517 auto Tokens
= annotate("if (std::tuple_size_v<T> > 0)");
518 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
519 EXPECT_TOKEN(Tokens
[5], tok::less
, TT_TemplateOpener
);
520 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_TemplateCloser
);
521 EXPECT_TOKEN(Tokens
[8], tok::greater
, TT_BinaryOperator
);
524 TEST_F(TokenAnnotatorTest
, UnderstandsNonTemplateAngleBrackets
) {
525 auto Tokens
= annotate("return a < b && c > d;");
526 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
527 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_BinaryOperator
);
528 EXPECT_TOKEN(Tokens
[6], tok::greater
, TT_BinaryOperator
);
530 Tokens
= annotate("a < 0 ? b : a > 0 ? c : d;");
531 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
532 EXPECT_TOKEN(Tokens
[1], tok::less
, TT_BinaryOperator
);
533 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_BinaryOperator
);
535 Tokens
= annotate("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
536 ASSERT_EQ(Tokens
.size(), 27u) << Tokens
;
537 EXPECT_TOKEN(Tokens
[7], tok::less
, TT_BinaryOperator
);
538 EXPECT_TOKEN(Tokens
[20], tok::greater
, TT_BinaryOperator
);
541 TEST_F(TokenAnnotatorTest
, UnderstandsWhitespaceSensitiveMacros
) {
542 FormatStyle Style
= getLLVMStyle();
543 Style
.WhitespaceSensitiveMacros
.push_back("FOO");
545 auto Tokens
= annotate("FOO(1+2 )", Style
);
546 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
547 EXPECT_TOKEN(Tokens
[0], tok::identifier
, TT_UntouchableMacroFunc
);
549 Tokens
= annotate("FOO(a:b:c)", Style
);
550 EXPECT_EQ(Tokens
.size(), 9u) << Tokens
;
551 EXPECT_TOKEN(Tokens
[0], tok::identifier
, TT_UntouchableMacroFunc
);
554 TEST_F(TokenAnnotatorTest
, UnderstandsDelete
) {
555 auto Tokens
= annotate("delete (void *)p;");
556 EXPECT_EQ(Tokens
.size(), 8u) << Tokens
;
557 EXPECT_TOKEN(Tokens
[4], tok::r_paren
, TT_CastRParen
);
559 Tokens
= annotate("delete[] (void *)p;");
560 EXPECT_EQ(Tokens
.size(), 10u) << Tokens
;
561 EXPECT_TOKEN(Tokens
[6], tok::r_paren
, TT_CastRParen
);
563 Tokens
= annotate("delete[] /*comment*/ (void *)p;");
564 EXPECT_EQ(Tokens
.size(), 11u) << Tokens
;
565 EXPECT_TOKEN(Tokens
[7], tok::r_paren
, TT_CastRParen
);
567 Tokens
= annotate("delete[/*comment*/] (void *)p;");
568 EXPECT_EQ(Tokens
.size(), 11u) << Tokens
;
569 EXPECT_TOKEN(Tokens
[7], tok::r_paren
, TT_CastRParen
);
571 Tokens
= annotate("delete/*comment*/[] (void *)p;");
572 EXPECT_EQ(Tokens
.size(), 11u) << Tokens
;
573 EXPECT_TOKEN(Tokens
[7], tok::r_paren
, TT_CastRParen
);
576 TEST_F(TokenAnnotatorTest
, UnderstandsCasts
) {
577 auto Tokens
= annotate("(void)p;");
578 EXPECT_EQ(Tokens
.size(), 6u) << Tokens
;
579 EXPECT_TOKEN(Tokens
[2], tok::r_paren
, TT_CastRParen
);
581 Tokens
= annotate("auto x = (Foo)p;");
582 EXPECT_EQ(Tokens
.size(), 9u) << Tokens
;
583 EXPECT_TOKEN(Tokens
[5], tok::r_paren
, TT_CastRParen
);
585 Tokens
= annotate("(std::vector<int>)p;");
586 EXPECT_EQ(Tokens
.size(), 11u) << Tokens
;
587 EXPECT_TOKEN(Tokens
[7], tok::r_paren
, TT_CastRParen
);
589 Tokens
= annotate("return (Foo)p;");
590 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
591 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_CastRParen
);
593 Tokens
= annotate("throw (Foo)p;");
594 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
595 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_CastRParen
);
597 Tokens
= annotate("#define FOO(x) (((uint64_t)(x) * BAR) / 100)");
598 EXPECT_EQ(Tokens
.size(), 21u) << Tokens
;
599 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_CastRParen
);
600 EXPECT_TOKEN(Tokens
[13], tok::r_paren
, TT_Unknown
);
601 EXPECT_TOKEN(Tokens
[14], tok::star
, TT_BinaryOperator
);
603 Tokens
= annotate("return (Foo) & 10;");
604 EXPECT_EQ(Tokens
.size(), 8u) << Tokens
;
605 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_Unknown
);
606 EXPECT_TOKEN(Tokens
[4], tok::amp
, TT_BinaryOperator
);
609 TEST_F(TokenAnnotatorTest
, UnderstandsDynamicExceptionSpecifier
) {
610 auto Tokens
= annotate("void f() throw(int);");
611 EXPECT_EQ(Tokens
.size(), 10u) << Tokens
;
612 EXPECT_TOKEN(Tokens
[4], tok::kw_throw
, TT_Unknown
);
615 TEST_F(TokenAnnotatorTest
, UnderstandsFunctionRefQualifiers
) {
616 auto Tokens
= annotate("void f() &;");
617 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
618 EXPECT_TOKEN(Tokens
[4], tok::amp
, TT_PointerOrReference
);
620 Tokens
= annotate("void operator=(T) &&;");
621 EXPECT_EQ(Tokens
.size(), 9u) << Tokens
;
622 EXPECT_TOKEN(Tokens
[6], tok::ampamp
, TT_PointerOrReference
);
624 Tokens
= annotate("template <typename T> void f() &;");
625 EXPECT_EQ(Tokens
.size(), 12u) << Tokens
;
626 EXPECT_TOKEN(Tokens
[9], tok::amp
, TT_PointerOrReference
);
628 Tokens
= annotate("template <typename T> void operator=(T) &;");
629 EXPECT_EQ(Tokens
.size(), 14u) << Tokens
;
630 EXPECT_TOKEN(Tokens
[11], tok::amp
, TT_PointerOrReference
);
633 TEST_F(TokenAnnotatorTest
, UnderstandsOverloadedOperators
) {
634 auto Tokens
= annotate("x.operator+()");
635 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
636 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
637 EXPECT_TOKEN(Tokens
[3], tok::plus
, TT_OverloadedOperator
);
638 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
639 Tokens
= annotate("x.operator=()");
640 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
641 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
642 EXPECT_TOKEN(Tokens
[3], tok::equal
, TT_OverloadedOperator
);
643 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
644 Tokens
= annotate("x.operator+=()");
645 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
646 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
647 EXPECT_TOKEN(Tokens
[3], tok::plusequal
, TT_OverloadedOperator
);
648 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
649 Tokens
= annotate("x.operator,()");
650 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
651 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
652 EXPECT_TOKEN(Tokens
[3], tok::comma
, TT_OverloadedOperator
);
653 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
654 Tokens
= annotate("x.operator()()");
655 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
656 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
657 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_OverloadedOperator
);
658 EXPECT_TOKEN(Tokens
[4], tok::r_paren
, TT_OverloadedOperator
);
659 EXPECT_TOKEN(Tokens
[5], tok::l_paren
, TT_OverloadedOperatorLParen
);
660 Tokens
= annotate("x.operator[]()");
661 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
662 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
663 // EXPECT_TOKEN(Tokens[3], tok::l_square, TT_OverloadedOperator);
664 // EXPECT_TOKEN(Tokens[4], tok::r_square, TT_OverloadedOperator);
665 EXPECT_TOKEN(Tokens
[5], tok::l_paren
, TT_OverloadedOperatorLParen
);
666 Tokens
= annotate("x.operator\"\"_a()");
667 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
668 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
669 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_OverloadedOperator
);
670 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
671 Tokens
= annotate("x.operator\"\" _a()");
672 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
673 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
674 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_OverloadedOperator
);
675 EXPECT_TOKEN(Tokens
[5], tok::l_paren
, TT_OverloadedOperatorLParen
);
676 Tokens
= annotate("x.operator\"\"if()");
677 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
678 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
679 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_OverloadedOperator
);
680 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
681 Tokens
= annotate("x.operator\"\"s()");
682 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
683 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
684 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_OverloadedOperator
);
685 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
686 Tokens
= annotate("x.operator\"\" s()");
687 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
688 EXPECT_TOKEN(Tokens
[2], tok::kw_operator
, TT_Unknown
);
689 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_OverloadedOperator
);
690 EXPECT_TOKEN(Tokens
[5], tok::l_paren
, TT_OverloadedOperatorLParen
);
692 Tokens
= annotate("int operator+(int);");
693 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
694 EXPECT_TOKEN(Tokens
[1], tok::kw_operator
, TT_FunctionDeclarationName
);
695 EXPECT_TOKEN(Tokens
[2], tok::plus
, TT_OverloadedOperator
);
696 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_OverloadedOperatorLParen
);
697 Tokens
= annotate("auto operator=(T&) {}");
698 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
699 EXPECT_TOKEN(Tokens
[1], tok::kw_operator
, TT_FunctionDeclarationName
);
700 EXPECT_TOKEN(Tokens
[2], tok::equal
, TT_OverloadedOperator
);
701 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_OverloadedOperatorLParen
);
702 Tokens
= annotate("auto operator()() {}");
703 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
704 EXPECT_TOKEN(Tokens
[1], tok::kw_operator
, TT_FunctionDeclarationName
);
705 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_OverloadedOperator
);
706 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_OverloadedOperator
);
707 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_OverloadedOperatorLParen
);
709 Tokens
= annotate("class Foo {\n"
710 " int operator+(a* b);\n"
712 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
713 EXPECT_TOKEN(Tokens
[4], tok::kw_operator
, TT_FunctionDeclarationName
);
714 EXPECT_TOKEN(Tokens
[5], tok::plus
, TT_OverloadedOperator
);
715 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_OverloadedOperatorLParen
);
716 EXPECT_TOKEN(Tokens
[8], tok::star
, TT_PointerOrReference
);
718 Tokens
= annotate("class Foo {\n"
719 " int c = operator+(a * b);\n"
721 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
722 EXPECT_TOKEN(Tokens
[6], tok::kw_operator
, TT_Unknown
);
723 EXPECT_TOKEN(Tokens
[7], tok::plus
, TT_OverloadedOperator
);
724 EXPECT_TOKEN(Tokens
[8], tok::l_paren
, TT_OverloadedOperatorLParen
);
725 EXPECT_TOKEN(Tokens
[10], tok::star
, TT_BinaryOperator
);
727 Tokens
= annotate("void foo() {\n"
728 " operator+(a * b);\n"
730 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
731 EXPECT_TOKEN(Tokens
[5], tok::kw_operator
, TT_Unknown
);
732 EXPECT_TOKEN(Tokens
[6], tok::plus
, TT_OverloadedOperator
);
733 EXPECT_TOKEN(Tokens
[7], tok::l_paren
, TT_OverloadedOperatorLParen
);
734 EXPECT_TOKEN(Tokens
[9], tok::star
, TT_BinaryOperator
);
736 Tokens
= annotate("return operator+(a * b, c & d) + operator+(a && b && c);");
737 ASSERT_EQ(Tokens
.size(), 24u) << Tokens
;
738 EXPECT_TOKEN(Tokens
[1], tok::kw_operator
, TT_Unknown
);
739 EXPECT_TOKEN(Tokens
[2], tok::plus
, TT_OverloadedOperator
);
740 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_OverloadedOperatorLParen
);
741 EXPECT_TOKEN(Tokens
[5], tok::star
, TT_BinaryOperator
);
742 EXPECT_TOKEN(Tokens
[9], tok::amp
, TT_BinaryOperator
);
743 EXPECT_TOKEN(Tokens
[13], tok::kw_operator
, TT_Unknown
);
744 EXPECT_TOKEN(Tokens
[14], tok::plus
, TT_OverloadedOperator
);
745 EXPECT_TOKEN(Tokens
[15], tok::l_paren
, TT_OverloadedOperatorLParen
);
746 EXPECT_TOKEN(Tokens
[17], tok::ampamp
, TT_BinaryOperator
);
747 EXPECT_TOKEN(Tokens
[19], tok::ampamp
, TT_BinaryOperator
);
749 Tokens
= annotate("class Foo {\n"
751 " operator+(a * b);\n"
754 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
755 EXPECT_TOKEN(Tokens
[8], tok::kw_operator
, TT_Unknown
);
756 EXPECT_TOKEN(Tokens
[9], tok::plus
, TT_OverloadedOperator
);
757 EXPECT_TOKEN(Tokens
[10], tok::l_paren
, TT_OverloadedOperatorLParen
);
758 EXPECT_TOKEN(Tokens
[12], tok::star
, TT_BinaryOperator
);
760 Tokens
= annotate("std::vector<Foo> operator()(Foo &foo);");
761 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
762 EXPECT_TOKEN(Tokens
[3], tok::less
, TT_TemplateOpener
);
763 EXPECT_TOKEN(Tokens
[5], tok::greater
, TT_TemplateCloser
);
764 EXPECT_TOKEN(Tokens
[6], tok::kw_operator
, TT_FunctionDeclarationName
);
765 EXPECT_TOKEN(Tokens
[7], tok::l_paren
, TT_OverloadedOperator
);
766 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_OverloadedOperator
);
767 EXPECT_TOKEN(Tokens
[9], tok::l_paren
, TT_OverloadedOperatorLParen
);
768 EXPECT_TOKEN(Tokens
[11], tok::amp
, TT_PointerOrReference
);
770 Tokens
= annotate("decltype(auto) operator()(T &x);");
771 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
772 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_TypeDeclarationParen
);
773 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_TypeDeclarationParen
);
774 EXPECT_TOKEN(Tokens
[4], tok::kw_operator
, TT_FunctionDeclarationName
);
775 EXPECT_TOKEN(Tokens
[5], tok::l_paren
, TT_OverloadedOperator
);
776 EXPECT_TOKEN(Tokens
[6], tok::r_paren
, TT_OverloadedOperator
);
777 EXPECT_TOKEN(Tokens
[7], tok::l_paren
, TT_OverloadedOperatorLParen
);
778 EXPECT_TOKEN(Tokens
[9], tok::amp
, TT_PointerOrReference
);
781 TEST_F(TokenAnnotatorTest
, OverloadedOperatorInTemplate
) {
785 } Operators
[] = {{"+", tok::plus
},
801 {"+=", tok::plusequal
},
802 {"-=", tok::minusequal
},
803 {"*=", tok::starequal
},
804 {"/=", tok::slashequal
},
805 {"%=", tok::percentequal
},
806 {"^=", tok::caretequal
},
807 {"&=", tok::ampequal
},
808 {"|=", tok::pipeequal
},
809 {"<<", tok::lessless
},
810 {">>", tok::greatergreater
},
811 {">>=", tok::greatergreaterequal
},
812 {"<<=", tok::lesslessequal
},
813 {"==", tok::equalequal
},
814 {"!=", tok::exclaimequal
},
815 {"<=", tok::lessequal
},
816 {">=", tok::greaterequal
},
817 {"<=>", tok::spaceship
},
819 {"||", tok::pipepipe
},
820 {"++", tok::plusplus
},
821 {"--", tok::minusminus
},
823 {"->*", tok::arrowstar
},
826 for (const auto &Operator
: Operators
) {
827 std::string
Input("C<&operator");
828 Input
+= Operator
.Text
;
830 auto Tokens
= annotate(std::string(Input
));
831 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
832 EXPECT_TOKEN(Tokens
[1], tok::less
, TT_TemplateOpener
);
833 EXPECT_TOKEN(Tokens
[4], Operator
.Kind
, TT_OverloadedOperator
);
834 EXPECT_TOKEN(Tokens
[5], tok::greater
, TT_TemplateCloser
);
837 auto Tokens
= annotate("C<&operator< <X>> lt;");
838 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
839 EXPECT_TOKEN(Tokens
[1], tok::less
, TT_TemplateOpener
);
840 EXPECT_TOKEN(Tokens
[4], tok::less
, TT_OverloadedOperator
);
841 EXPECT_TOKEN(Tokens
[5], tok::less
, TT_TemplateOpener
);
842 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_TemplateCloser
);
843 EXPECT_TOKEN(Tokens
[8], tok::greater
, TT_TemplateCloser
);
846 TEST_F(TokenAnnotatorTest
, UnderstandsRequiresClausesAndConcepts
) {
847 auto Tokens
= annotate("template <typename T>\n"
848 "concept C = (Foo && Bar) && (Bar && Baz);");
850 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
851 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_BinaryOperator
);
852 EXPECT_TOKEN(Tokens
[13], tok::ampamp
, TT_BinaryOperator
);
853 EXPECT_TOKEN(Tokens
[16], tok::ampamp
, TT_BinaryOperator
);
855 Tokens
= annotate("template <typename T>\n"
856 "concept C = Foo && !Bar;");
858 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
859 EXPECT_TOKEN(Tokens
[9], tok::ampamp
, TT_BinaryOperator
);
860 EXPECT_TOKEN(Tokens
[10], tok::exclaim
, TT_UnaryOperator
);
862 Tokens
= annotate("template <typename T>\n"
863 "concept C = requires(T t) {\n"
865 "} && Bar<T> && Baz<T>;");
866 ASSERT_EQ(Tokens
.size(), 35u) << Tokens
;
867 EXPECT_TOKEN(Tokens
[8], tok::kw_requires
, TT_RequiresExpression
);
868 EXPECT_TOKEN(Tokens
[9], tok::l_paren
, TT_RequiresExpressionLParen
);
869 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_RequiresExpressionLBrace
);
870 EXPECT_TOKEN(Tokens
[23], tok::ampamp
, TT_BinaryOperator
);
871 EXPECT_TOKEN(Tokens
[28], tok::ampamp
, TT_BinaryOperator
);
873 Tokens
= annotate("template<typename T>\n"
874 "requires C1<T> && (C21<T> || C22<T> && C2e<T>) && C3<T>\n"
876 ASSERT_EQ(Tokens
.size(), 36u) << Tokens
;
877 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
878 EXPECT_TOKEN(Tokens
[6], tok::identifier
, TT_Unknown
);
879 EXPECT_EQ(Tokens
[6]->FakeLParens
.size(), 1u);
880 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_BinaryOperator
);
881 EXPECT_TOKEN(Tokens
[16], tok::pipepipe
, TT_BinaryOperator
);
882 EXPECT_TOKEN(Tokens
[21], tok::ampamp
, TT_BinaryOperator
);
883 EXPECT_TOKEN(Tokens
[27], tok::ampamp
, TT_BinaryOperator
);
884 EXPECT_TOKEN(Tokens
[31], tok::greater
, TT_TemplateCloser
);
885 EXPECT_EQ(Tokens
[31]->FakeRParens
, 1u);
886 EXPECT_TRUE(Tokens
[31]->ClosesRequiresClause
);
889 annotate("template<typename T>\n"
890 "requires (C1<T> && (C21<T> || C22<T> && C2e<T>) && C3<T>)\n"
892 ASSERT_EQ(Tokens
.size(), 38u) << Tokens
;
893 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
894 EXPECT_TOKEN(Tokens
[7], tok::identifier
, TT_Unknown
);
895 EXPECT_EQ(Tokens
[7]->FakeLParens
.size(), 1u);
896 EXPECT_TOKEN(Tokens
[11], tok::ampamp
, TT_BinaryOperator
);
897 EXPECT_TOKEN(Tokens
[17], tok::pipepipe
, TT_BinaryOperator
);
898 EXPECT_TOKEN(Tokens
[22], tok::ampamp
, TT_BinaryOperator
);
899 EXPECT_TOKEN(Tokens
[28], tok::ampamp
, TT_BinaryOperator
);
900 EXPECT_TOKEN(Tokens
[32], tok::greater
, TT_TemplateCloser
);
901 EXPECT_EQ(Tokens
[32]->FakeRParens
, 1u);
902 EXPECT_TOKEN(Tokens
[33], tok::r_paren
, TT_Unknown
);
903 EXPECT_TRUE(Tokens
[33]->ClosesRequiresClause
);
905 Tokens
= annotate("template <typename T>\n"
906 "void foo(T) noexcept requires Bar<T>;");
907 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
908 EXPECT_TOKEN(Tokens
[11], tok::kw_requires
, TT_RequiresClause
);
910 Tokens
= annotate("template <typename T>\n"
911 "requires Bar<T> || Baz<T>\n"
912 "auto foo(T) -> int;");
913 ASSERT_EQ(Tokens
.size(), 24u) << Tokens
;
914 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
915 EXPECT_EQ(Tokens
[11]->FakeLParens
.size(), 0u);
916 EXPECT_TRUE(Tokens
[14]->ClosesRequiresClause
);
917 EXPECT_TOKEN(Tokens
[20], tok::arrow
, TT_TrailingReturnArrow
);
919 Tokens
= annotate("template <typename T>\n"
921 "bool foo(T) { return false; }");
922 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
923 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
924 EXPECT_TRUE(Tokens
[9]->ClosesRequiresClause
);
925 EXPECT_TOKEN(Tokens
[11], tok::identifier
, TT_FunctionDeclarationName
);
927 Tokens
= annotate("template <typename T>\n"
929 "decltype(auto) foo(T) { return false; }");
930 ASSERT_EQ(Tokens
.size(), 24u) << Tokens
;
931 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
932 EXPECT_TRUE(Tokens
[9]->ClosesRequiresClause
);
933 EXPECT_TOKEN(Tokens
[14], tok::identifier
, TT_FunctionDeclarationName
);
935 Tokens
= annotate("template <typename T>\n"
937 " void foo() const requires Bar<T>;\n"
938 " void bar() const & requires Baz<T>;\n"
939 " void bar() && requires Baz2<T>;\n"
940 " void baz() const & noexcept requires Baz<T>;\n"
941 " void baz() && noexcept requires Baz2<T>;\n"
944 "void S::bar() const & requires Baz<T> { }");
945 ASSERT_EQ(Tokens
.size(), 85u) << Tokens
;
946 EXPECT_TOKEN(Tokens
[13], tok::kw_requires
, TT_RequiresClause
);
947 EXPECT_TOKEN(Tokens
[24], tok::amp
, TT_PointerOrReference
);
948 EXPECT_TOKEN(Tokens
[25], tok::kw_requires
, TT_RequiresClause
);
949 EXPECT_TOKEN(Tokens
[35], tok::ampamp
, TT_PointerOrReference
);
950 EXPECT_TOKEN(Tokens
[36], tok::kw_requires
, TT_RequiresClause
);
951 EXPECT_TOKEN(Tokens
[47], tok::amp
, TT_PointerOrReference
);
952 EXPECT_TOKEN(Tokens
[49], tok::kw_requires
, TT_RequiresClause
);
953 EXPECT_TOKEN(Tokens
[59], tok::ampamp
, TT_PointerOrReference
);
954 EXPECT_TOKEN(Tokens
[61], tok::kw_requires
, TT_RequiresClause
);
955 EXPECT_TOKEN(Tokens
[76], tok::amp
, TT_PointerOrReference
);
956 EXPECT_TOKEN(Tokens
[77], tok::kw_requires
, TT_RequiresClause
);
958 Tokens
= annotate("void Class::member() && requires(Constant) {}");
959 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
960 EXPECT_TOKEN(Tokens
[7], tok::kw_requires
, TT_RequiresClause
);
962 Tokens
= annotate("void Class::member() && requires(Constant<T>) {}");
963 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
964 EXPECT_TOKEN(Tokens
[7], tok::kw_requires
, TT_RequiresClause
);
967 annotate("void Class::member() && requires(Namespace::Constant<T>) {}");
968 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
969 EXPECT_TOKEN(Tokens
[7], tok::kw_requires
, TT_RequiresClause
);
971 Tokens
= annotate("void Class::member() && requires(typename "
972 "Namespace::Outer<T>::Inner::Constant) {}");
973 ASSERT_EQ(Tokens
.size(), 24u) << Tokens
;
974 EXPECT_TOKEN(Tokens
[7], tok::kw_requires
, TT_RequiresClause
);
976 Tokens
= annotate("struct [[nodiscard]] zero_t {\n"
977 " template<class T>\n"
978 " requires requires { number_zero_v<T>; }\n"
979 " [[nodiscard]] constexpr operator T() const { "
980 "return number_zero_v<T>; }\n"
982 ASSERT_EQ(Tokens
.size(), 44u) << Tokens
;
983 EXPECT_TOKEN(Tokens
[13], tok::kw_requires
, TT_RequiresClause
);
984 EXPECT_TOKEN(Tokens
[14], tok::kw_requires
, TT_RequiresExpression
);
985 EXPECT_TOKEN(Tokens
[15], tok::l_brace
, TT_RequiresExpressionLBrace
);
986 EXPECT_TOKEN(Tokens
[21], tok::r_brace
, TT_Unknown
);
987 EXPECT_EQ(Tokens
[21]->MatchingParen
, Tokens
[15]);
988 EXPECT_TRUE(Tokens
[21]->ClosesRequiresClause
);
991 annotate("template <class A, class B> concept C ="
992 "std::same_as<std::iter_value_t<A>, std::iter_value_t<B>>;");
993 ASSERT_EQ(Tokens
.size(), 31u) << Tokens
;
994 EXPECT_TOKEN(Tokens
[8], tok::kw_concept
, TT_Unknown
);
995 EXPECT_TOKEN(Tokens
[14], tok::less
, TT_TemplateOpener
);
996 EXPECT_TOKEN(Tokens
[18], tok::less
, TT_TemplateOpener
);
997 EXPECT_TOKEN(Tokens
[20], tok::greater
, TT_TemplateCloser
);
998 EXPECT_TOKEN(Tokens
[25], tok::less
, TT_TemplateOpener
);
999 EXPECT_TOKEN(Tokens
[27], tok::greater
, TT_TemplateCloser
);
1000 EXPECT_TOKEN(Tokens
[28], tok::greater
, TT_TemplateCloser
);
1002 Tokens
= annotate("auto bar() -> int requires(is_integral_v<T>) {}");
1003 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1004 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1006 Tokens
= annotate("auto bar() -> void requires(is_integral_v<T>) {}");
1007 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1008 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1010 Tokens
= annotate("auto bar() -> MyType requires(is_integral_v<T>) {}");
1011 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1012 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1015 annotate("auto bar() -> SOME_MACRO_TYPE requires(is_integral_v<T>) {}");
1016 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1017 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1020 annotate("auto bar() -> qualified::type requires(is_integral_v<T>) {}");
1021 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1022 EXPECT_TOKEN(Tokens
[8], tok::kw_requires
, TT_RequiresClause
);
1025 annotate("auto bar() -> Template<type> requires(is_integral_v<T>) {}");
1026 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
1027 EXPECT_TOKEN(Tokens
[9], tok::kw_requires
, TT_RequiresClause
);
1029 Tokens
= annotate("void foo() requires((A<T>) && C) {}");
1030 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1031 EXPECT_TOKEN(Tokens
[4], tok::kw_requires
, TT_RequiresClause
);
1032 EXPECT_TOKEN(Tokens
[12], tok::ampamp
, TT_BinaryOperator
);
1034 Tokens
= annotate("void foo() requires(((A<T>) && C)) {}");
1035 ASSERT_EQ(Tokens
.size(), 20u) << Tokens
;
1036 EXPECT_TOKEN(Tokens
[4], tok::kw_requires
, TT_RequiresClause
);
1037 EXPECT_TOKEN(Tokens
[13], tok::ampamp
, TT_BinaryOperator
);
1039 Tokens
= annotate("void foo() requires([](T&&){}(t)) {}");
1040 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
1041 EXPECT_TOKEN(Tokens
[4], tok::kw_requires
, TT_RequiresClause
);
1042 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_PointerOrReference
);
1044 Tokens
= annotate("void foo() requires([](T&& u){}(t)) {}");
1045 ASSERT_EQ(Tokens
.size(), 22u) << Tokens
;
1046 EXPECT_TOKEN(Tokens
[4], tok::kw_requires
, TT_RequiresClause
);
1047 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_PointerOrReference
);
1049 Tokens
= annotate("void f() & requires(true) {}");
1050 EXPECT_EQ(Tokens
.size(), 12u) << Tokens
;
1051 EXPECT_TOKEN(Tokens
[4], tok::amp
, TT_PointerOrReference
);
1052 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
1054 Tokens
= annotate("void f() & requires(C<true, true>) {}");
1055 EXPECT_EQ(Tokens
.size(), 17u) << Tokens
;
1056 EXPECT_TOKEN(Tokens
[4], tok::amp
, TT_PointerOrReference
);
1057 EXPECT_TOKEN(Tokens
[5], tok::kw_requires
, TT_RequiresClause
);
1060 TEST_F(TokenAnnotatorTest
, UnderstandsRequiresExpressions
) {
1061 auto Tokens
= annotate("bool b = requires(int i) { i + 5; };");
1062 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1063 EXPECT_TOKEN(Tokens
[3], tok::kw_requires
, TT_RequiresExpression
);
1064 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_RequiresExpressionLParen
);
1065 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_RequiresExpressionLBrace
);
1067 Tokens
= annotate("if (requires(int i) { i + 5; }) return;");
1068 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
1069 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1070 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1071 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_RequiresExpressionLBrace
);
1073 Tokens
= annotate("if (func() && requires(int i) { i + 5; }) return;");
1074 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
1075 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresExpression
);
1076 EXPECT_TOKEN(Tokens
[7], tok::l_paren
, TT_RequiresExpressionLParen
);
1077 EXPECT_TOKEN(Tokens
[11], tok::l_brace
, TT_RequiresExpressionLBrace
);
1079 Tokens
= annotate("foo(requires(const T t) {});");
1080 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1081 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1082 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1083 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_RequiresExpressionLBrace
);
1085 Tokens
= annotate("foo(requires(const int t) {});");
1086 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1087 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1088 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1089 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_RequiresExpressionLBrace
);
1091 Tokens
= annotate("foo(requires(const T t) {});");
1092 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1093 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1094 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1095 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_RequiresExpressionLBrace
);
1097 Tokens
= annotate("foo(requires(int const* volatile t) {});");
1098 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1099 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1100 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1101 EXPECT_TOKEN(Tokens
[6], tok::star
, TT_PointerOrReference
);
1102 EXPECT_TOKEN(Tokens
[10], tok::l_brace
, TT_RequiresExpressionLBrace
);
1104 Tokens
= annotate("foo(requires(T const* volatile t) {});");
1105 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1106 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1107 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1108 EXPECT_TOKEN(Tokens
[6], tok::star
, TT_PointerOrReference
);
1109 EXPECT_TOKEN(Tokens
[10], tok::l_brace
, TT_RequiresExpressionLBrace
);
1111 Tokens
= annotate("foo(requires(T& t) {});");
1112 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1113 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1114 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1115 EXPECT_TOKEN(Tokens
[5], tok::amp
, TT_PointerOrReference
);
1116 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_RequiresExpressionLBrace
);
1118 Tokens
= annotate("foo(requires(T&& t) {});");
1119 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1120 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1121 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1122 EXPECT_TOKEN(Tokens
[5], tok::ampamp
, TT_PointerOrReference
);
1123 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_RequiresExpressionLBrace
);
1125 Tokens
= annotate("bool foo = requires(T& t) {};");
1126 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1127 EXPECT_TOKEN(Tokens
[3], tok::kw_requires
, TT_RequiresExpression
);
1128 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_RequiresExpressionLParen
);
1129 EXPECT_TOKEN(Tokens
[6], tok::amp
, TT_PointerOrReference
);
1130 EXPECT_TOKEN(Tokens
[9], tok::l_brace
, TT_RequiresExpressionLBrace
);
1132 Tokens
= annotate("bool foo = requires(T&& t) {};");
1133 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1134 EXPECT_TOKEN(Tokens
[3], tok::kw_requires
, TT_RequiresExpression
);
1135 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_RequiresExpressionLParen
);
1136 EXPECT_TOKEN(Tokens
[6], tok::ampamp
, TT_PointerOrReference
);
1137 EXPECT_TOKEN(Tokens
[9], tok::l_brace
, TT_RequiresExpressionLBrace
);
1140 annotate("foo(requires(const typename Outer<T>::Inner * const t) {});");
1141 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
1142 EXPECT_TOKEN(Tokens
[2], tok::kw_requires
, TT_RequiresExpression
);
1143 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_RequiresExpressionLParen
);
1144 EXPECT_TOKEN(Tokens
[12], tok::star
, TT_PointerOrReference
);
1145 EXPECT_TOKEN(Tokens
[16], tok::l_brace
, TT_RequiresExpressionLBrace
);
1147 Tokens
= annotate("template <typename T>\n"
1148 "concept C = requires(T T) {\n"
1149 " requires Bar<T> && Foo<T>;\n"
1151 ASSERT_EQ(Tokens
.size(), 28u) << Tokens
;
1152 EXPECT_TOKEN(Tokens
[8], tok::kw_requires
, TT_RequiresExpression
);
1153 EXPECT_TOKEN(Tokens
[9], tok::l_paren
, TT_RequiresExpressionLParen
);
1154 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_RequiresExpressionLBrace
);
1155 EXPECT_TOKEN(Tokens
[14], tok::kw_requires
,
1156 TT_RequiresClauseInARequiresExpression
);
1158 Tokens
= annotate("template <typename T>\n"
1159 "concept C = requires(T T) {\n"
1160 " { t.func() } -> std::same_as<int>;"
1161 " requires Bar<T> && Foo<T>;\n"
1163 ASSERT_EQ(Tokens
.size(), 43u) << Tokens
;
1164 EXPECT_TOKEN(Tokens
[8], tok::kw_requires
, TT_RequiresExpression
);
1165 EXPECT_TOKEN(Tokens
[9], tok::l_paren
, TT_RequiresExpressionLParen
);
1166 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_RequiresExpressionLBrace
);
1167 EXPECT_TOKEN(Tokens
[29], tok::kw_requires
,
1168 TT_RequiresClauseInARequiresExpression
);
1170 // Invalid Code, but we don't want to crash. See http://llvm.org/PR54350.
1171 Tokens
= annotate("bool r10 = requires (struct new_struct { int x; } s) { "
1172 "requires true; };");
1173 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
1174 EXPECT_TOKEN(Tokens
[3], tok::kw_requires
, TT_RequiresExpression
);
1175 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_RequiresExpressionLParen
);
1176 EXPECT_TOKEN(Tokens
[14], tok::l_brace
, TT_RequiresExpressionLBrace
);
1178 Tokens
= annotate("bool foo = requires(C<true, true> c) {\n"
1181 ASSERT_EQ(Tokens
.size(), 25u) << Tokens
;
1182 EXPECT_TOKEN(Tokens
[3], tok::kw_requires
, TT_RequiresExpression
);
1183 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_RequiresExpressionLParen
);
1184 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_RequiresExpressionLBrace
);
1187 TEST_F(TokenAnnotatorTest
, UnderstandsPragmaRegion
) {
1188 // Everything after #pragma region should be ImplicitStringLiteral
1189 auto Tokens
= annotate("#pragma region Foo(Bar: Hello)");
1190 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1191 EXPECT_TOKEN(Tokens
[5], tok::identifier
, TT_ImplicitStringLiteral
);
1192 EXPECT_TOKEN(Tokens
[6], tok::colon
, TT_ImplicitStringLiteral
);
1193 EXPECT_TOKEN(Tokens
[7], tok::identifier
, TT_ImplicitStringLiteral
);
1195 // Make sure it's annotated correctly inside a function as well
1196 Tokens
= annotate("void test(){\n#pragma region Foo(Bar: Hello)\n}");
1197 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1198 EXPECT_TOKEN(Tokens
[10], tok::identifier
, TT_ImplicitStringLiteral
);
1199 EXPECT_TOKEN(Tokens
[11], tok::colon
, TT_ImplicitStringLiteral
);
1200 EXPECT_TOKEN(Tokens
[12], tok::identifier
, TT_ImplicitStringLiteral
);
1203 TEST_F(TokenAnnotatorTest
, RequiresDoesNotChangeParsingOfTheRest
) {
1204 const char *BaseCode
= nullptr;
1205 const char *ConstrainedCode
= nullptr;
1206 auto BaseTokenCount
= 0u;
1207 auto RequiresTokenCount
= 0u;
1208 auto PrefixTokenCount
= 0u;
1210 auto TestRequires
= [&](int Line
) {
1211 const auto BaseTokens
= annotate(BaseCode
);
1212 const auto ConstrainedTokens
= annotate(ConstrainedCode
);
1214 #define LINE " (Line " << Line << ')'
1216 ASSERT_EQ(BaseTokens
.size(), BaseTokenCount
) << BaseTokens
<< LINE
;
1217 ASSERT_EQ(ConstrainedTokens
.size(), BaseTokenCount
+ RequiresTokenCount
)
1220 for (auto I
= 0u; I
< BaseTokenCount
; ++I
) {
1223 *ConstrainedTokens
[I
< PrefixTokenCount
? I
: I
+ RequiresTokenCount
])
1230 BaseCode
= "template<typename T>\n"
1232 ConstrainedCode
= "template<typename T>\n"
1233 " requires Foo<T>\n"
1235 BaseTokenCount
= 11;
1236 RequiresTokenCount
= 5;
1237 PrefixTokenCount
= 5;
1238 TestRequires(__LINE__
);
1240 BaseCode
= "template<typename T>\n"
1242 ConstrainedCode
= "template<typename T>\n"
1243 " requires Foo<T>\n"
1246 TestRequires(__LINE__
);
1248 BaseCode
= "template<typename T>\n"
1253 ConstrainedCode
= "template<typename T>\n"
1254 " requires Foo<T>\n"
1259 BaseTokenCount
= 21;
1260 TestRequires(__LINE__
);
1262 BaseCode
= "template<typename T>\n"
1263 "Bar(T) -> Bar<T>;";
1264 ConstrainedCode
= "template<typename T>\n"
1265 " requires Foo<T>\n"
1266 "Bar(T) -> Bar<T>;";
1267 BaseTokenCount
= 16;
1268 TestRequires(__LINE__
);
1270 BaseCode
= "template<typename T>\n"
1272 ConstrainedCode
= "template<typename T>\n"
1273 " requires Foo<T>\n"
1275 BaseTokenCount
= 11;
1276 TestRequires(__LINE__
);
1278 BaseCode
= "template<typename T>\n"
1280 " auto bar = baz();\n"
1281 " return bar + T{};\n"
1283 ConstrainedCode
= "template<typename T>\n"
1284 " requires Foo<T>\n"
1286 " auto bar = baz();\n"
1287 " return bar + T{};\n"
1289 BaseTokenCount
= 26;
1290 TestRequires(__LINE__
);
1292 BaseCode
= "template<typename T>\n"
1294 ConstrainedCode
= "template<typename T>\n"
1295 "T foo() requires Foo<T>;";
1296 BaseTokenCount
= 11;
1297 PrefixTokenCount
= 9;
1298 TestRequires(__LINE__
);
1300 BaseCode
= "template<typename T>\n"
1302 " auto bar = baz();\n"
1303 " return bar + T{};\n"
1305 ConstrainedCode
= "template<typename T>\n"
1306 "T foo() requires Foo<T> {\n"
1307 " auto bar = baz();\n"
1308 " return bar + T{};\n"
1310 BaseTokenCount
= 26;
1311 TestRequires(__LINE__
);
1313 BaseCode
= "template<typename T>\n"
1315 ConstrainedCode
= "template<typename T>\n"
1316 " requires(Foo<T>)\n"
1318 BaseTokenCount
= 11;
1319 RequiresTokenCount
= 7;
1320 PrefixTokenCount
= 5;
1321 TestRequires(__LINE__
);
1323 BaseCode
= "template<typename T>\n"
1324 "Bar(T) -> Bar<typename T::I>;";
1325 ConstrainedCode
= "template<typename T>\n"
1326 " requires requires(T &&t) {\n"
1329 "Bar(T) -> Bar<typename T::I>;";
1330 BaseTokenCount
= 19;
1331 RequiresTokenCount
= 14;
1332 PrefixTokenCount
= 5;
1333 TestRequires(__LINE__
);
1335 BaseCode
= "struct [[nodiscard]] zero_t {\n"
1336 " template<class T>\n"
1337 " [[nodiscard]] constexpr operator T() const { return v<T>; }\n"
1340 "struct [[nodiscard]] zero_t {\n"
1341 " template<class T>\n"
1342 " requires requires { v<T>; }\n"
1343 " [[nodiscard]] constexpr operator T() const { return v<T>; }\n"
1345 BaseTokenCount
= 35;
1346 RequiresTokenCount
= 9;
1347 PrefixTokenCount
= 13;
1348 TestRequires(__LINE__
);
1350 BaseCode
= "constexpr Foo(Foo const &other)\n"
1351 " : value{other.value} {\n"
1353 " do_more_magic();\n"
1355 ConstrainedCode
= "constexpr Foo(Foo const &other)\n"
1356 " requires std::is_copy_constructible<T>\n"
1357 " : value{other.value} {\n"
1359 " do_more_magic();\n"
1361 BaseTokenCount
= 26;
1362 RequiresTokenCount
= 7;
1363 PrefixTokenCount
= 8;
1364 TestRequires(__LINE__
);
1366 BaseCode
= "constexpr Foo(Foo const &other)\n"
1367 " : value{other.value} {\n"
1369 " do_more_magic();\n"
1371 ConstrainedCode
= "constexpr Foo(Foo const &other)\n"
1372 " requires (std::is_copy_constructible<T>)\n"
1373 " : value{other.value} {\n"
1375 " do_more_magic();\n"
1377 RequiresTokenCount
= 9;
1378 TestRequires(__LINE__
);
1380 BaseCode
= "template<typename T>\n"
1384 ConstrainedCode
= "template<typename T>\n"
1389 BaseTokenCount
= 16;
1390 RequiresTokenCount
= 4;
1391 PrefixTokenCount
= 5;
1392 TestRequires(__LINE__
);
1395 TEST_F(TokenAnnotatorTest
, UnderstandsAsm
) {
1396 auto Tokens
= annotate("__asm{\n"
1399 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
1400 EXPECT_TOKEN(Tokens
[0], tok::kw_asm
, TT_Unknown
);
1401 EXPECT_TOKEN(Tokens
[1], tok::l_brace
, TT_InlineASMBrace
);
1402 EXPECT_TOKEN(Tokens
[4], tok::r_brace
, TT_InlineASMBrace
);
1405 TEST_F(TokenAnnotatorTest
, UnderstandsObjCBlock
) {
1406 auto Tokens
= annotate("int (^)() = ^ ()\n"
1407 " external_source_symbol() { //\n"
1410 ASSERT_EQ(Tokens
.size(), 21u) << Tokens
;
1411 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ObjCBlockLParen
);
1412 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_ObjCBlockLBrace
);
1414 Tokens
= annotate("int *p = ^int*(){ //\n"
1415 " return nullptr;\n"
1417 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
1418 EXPECT_TOKEN(Tokens
[9], tok::l_brace
, TT_ObjCBlockLBrace
);
1421 TEST_F(TokenAnnotatorTest
, UnderstandsObjCMethodExpr
) {
1422 auto Tokens
= annotate("void f() {\n"
1424 " BOOL a = [b.c n] > 1;\n"
1426 EXPECT_EQ(Tokens
.size(), 20u) << Tokens
;
1427 EXPECT_TOKEN(Tokens
[9], tok::l_square
, TT_ObjCMethodExpr
);
1428 EXPECT_TOKEN(Tokens
[15], tok::greater
, TT_BinaryOperator
);
1431 TEST_F(TokenAnnotatorTest
, UnderstandsLambdas
) {
1432 auto Tokens
= annotate("[]() constexpr {}");
1433 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1434 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1435 EXPECT_TOKEN(Tokens
[5], tok::l_brace
, TT_LambdaLBrace
);
1437 Tokens
= annotate("[]() consteval {}");
1438 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1439 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1440 EXPECT_TOKEN(Tokens
[5], tok::l_brace
, TT_LambdaLBrace
);
1442 Tokens
= annotate("[]() mutable {}");
1443 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1444 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1445 EXPECT_TOKEN(Tokens
[5], tok::l_brace
, TT_LambdaLBrace
);
1447 Tokens
= annotate("[]() static {}");
1448 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1449 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1450 EXPECT_TOKEN(Tokens
[5], tok::l_brace
, TT_LambdaLBrace
);
1452 Tokens
= annotate("[]() -> auto {}");
1453 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
1454 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1455 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_TrailingReturnArrow
);
1456 EXPECT_TOKEN(Tokens
[6], tok::l_brace
, TT_LambdaLBrace
);
1458 Tokens
= annotate("[]() -> auto & {}");
1459 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1460 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1461 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_TrailingReturnArrow
);
1462 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_LambdaLBrace
);
1464 Tokens
= annotate("[]() -> auto * {}");
1465 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1466 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1467 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_TrailingReturnArrow
);
1468 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_LambdaLBrace
);
1470 Tokens
= annotate("[] {}");
1471 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
1472 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1473 EXPECT_TOKEN(Tokens
[2], tok::l_brace
, TT_LambdaLBrace
);
1475 Tokens
= annotate("[] noexcept {}");
1476 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
1477 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1478 EXPECT_TOKEN(Tokens
[3], tok::l_brace
, TT_LambdaLBrace
);
1480 Tokens
= annotate("[] -> auto {}");
1481 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
1482 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1483 EXPECT_TOKEN(Tokens
[2], tok::arrow
, TT_TrailingReturnArrow
);
1484 EXPECT_TOKEN(Tokens
[4], tok::l_brace
, TT_LambdaLBrace
);
1486 Tokens
= annotate("[] <typename T> () {}");
1487 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1488 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1489 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1490 EXPECT_TOKEN(Tokens
[8], tok::l_brace
, TT_LambdaLBrace
);
1492 Tokens
= annotate("[] <typename T> {}");
1493 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
1494 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1495 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1496 EXPECT_TOKEN(Tokens
[6], tok::l_brace
, TT_LambdaLBrace
);
1498 Tokens
= annotate("[] <typename... T> () {}");
1499 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1500 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1501 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1502 EXPECT_TOKEN(Tokens
[9], tok::l_brace
, TT_LambdaLBrace
);
1504 Tokens
= annotate("[] <typename... T> {}");
1505 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1506 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1507 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1508 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_LambdaLBrace
);
1510 Tokens
= annotate("[] <int... T> () {}");
1511 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1512 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1513 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1514 EXPECT_TOKEN(Tokens
[9], tok::l_brace
, TT_LambdaLBrace
);
1516 Tokens
= annotate("[] <int... T> {}");
1517 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1518 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1519 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1520 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_LambdaLBrace
);
1522 Tokens
= annotate("[] <Foo... T> () {}");
1523 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1524 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1525 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1526 EXPECT_TOKEN(Tokens
[9], tok::l_brace
, TT_LambdaLBrace
);
1528 Tokens
= annotate("[] <Foo... T> {}");
1529 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1530 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1531 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1532 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_LambdaLBrace
);
1534 // Lambdas with a requires-clause
1535 Tokens
= annotate("[] <typename T> (T t) requires Bar<T> {}");
1536 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1537 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1538 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1539 EXPECT_TOKEN(Tokens
[10], tok::kw_requires
, TT_RequiresClause
);
1540 EXPECT_TRUE(Tokens
[14]->ClosesRequiresClause
);
1541 EXPECT_TOKEN(Tokens
[15], tok::l_brace
, TT_LambdaLBrace
);
1543 Tokens
= annotate("[] <typename T> (T &&t) requires Bar<T> {}");
1544 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
1545 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1546 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1547 EXPECT_TOKEN(Tokens
[8], tok::ampamp
, TT_PointerOrReference
);
1548 EXPECT_TOKEN(Tokens
[11], tok::kw_requires
, TT_RequiresClause
);
1549 EXPECT_TRUE(Tokens
[15]->ClosesRequiresClause
);
1550 EXPECT_TOKEN(Tokens
[16], tok::l_brace
, TT_LambdaLBrace
);
1552 Tokens
= annotate("[] <typename T> (T t) requires Foo<T> || Bar<T> {}");
1553 ASSERT_EQ(Tokens
.size(), 23u) << Tokens
;
1554 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1555 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1556 EXPECT_TOKEN(Tokens
[10], tok::kw_requires
, TT_RequiresClause
);
1557 EXPECT_TRUE(Tokens
[19]->ClosesRequiresClause
);
1558 EXPECT_TOKEN(Tokens
[20], tok::l_brace
, TT_LambdaLBrace
);
1560 Tokens
= annotate("[] <typename T> (T t) -> T requires Bar<T> {}");
1561 ASSERT_EQ(Tokens
.size(), 20u) << Tokens
;
1562 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1563 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1564 EXPECT_TOKEN(Tokens
[10], tok::arrow
, TT_TrailingReturnArrow
);
1565 EXPECT_TOKEN(Tokens
[12], tok::kw_requires
, TT_RequiresClause
);
1566 EXPECT_TRUE(Tokens
[16]->ClosesRequiresClause
);
1567 EXPECT_TOKEN(Tokens
[17], tok::l_brace
, TT_LambdaLBrace
);
1569 Tokens
= annotate("[] <typename T> requires Bar<T> (T t) {}");
1570 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1571 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1572 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1573 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1574 EXPECT_TRUE(Tokens
[10]->ClosesRequiresClause
);
1575 EXPECT_TOKEN(Tokens
[15], tok::l_brace
, TT_LambdaLBrace
);
1577 Tokens
= annotate("[] <typename T> requires Bar<T> (T &&t) {}");
1578 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
1579 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1580 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1581 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1582 EXPECT_TRUE(Tokens
[10]->ClosesRequiresClause
);
1583 EXPECT_TOKEN(Tokens
[13], tok::ampamp
, TT_PointerOrReference
);
1584 EXPECT_TOKEN(Tokens
[16], tok::l_brace
, TT_LambdaLBrace
);
1586 Tokens
= annotate("[] <typename T> requires Foo<T> || Bar<T> (T t) {}");
1587 ASSERT_EQ(Tokens
.size(), 23u) << Tokens
;
1588 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1589 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1590 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1591 EXPECT_TRUE(Tokens
[15]->ClosesRequiresClause
);
1592 EXPECT_TOKEN(Tokens
[20], tok::l_brace
, TT_LambdaLBrace
);
1594 Tokens
= annotate("[] <typename T> requires true (T&& t) {}");
1595 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1596 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1597 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1598 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1599 EXPECT_TRUE(Tokens
[7]->ClosesRequiresClause
);
1600 EXPECT_TOKEN(Tokens
[10], tok::ampamp
, TT_PointerOrReference
);
1601 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_LambdaLBrace
);
1603 Tokens
= annotate("[] <typename T> requires Bar<T> {}");
1604 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
1605 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1606 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1607 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1608 EXPECT_TRUE(Tokens
[10]->ClosesRequiresClause
);
1609 EXPECT_TOKEN(Tokens
[11], tok::l_brace
, TT_LambdaLBrace
);
1611 Tokens
= annotate("[] <typename T> requires Bar<T> noexcept {}");
1612 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1613 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1614 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1615 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1616 EXPECT_TRUE(Tokens
[10]->ClosesRequiresClause
);
1617 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_LambdaLBrace
);
1619 Tokens
= annotate("[] <typename T> requires Bar<T> -> T {}");
1620 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1621 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1622 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1623 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1624 EXPECT_TRUE(Tokens
[10]->ClosesRequiresClause
);
1625 EXPECT_TOKEN(Tokens
[11], tok::arrow
, TT_TrailingReturnArrow
);
1626 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_LambdaLBrace
);
1628 Tokens
= annotate("[] <typename T> requires Foo<T> (T t) requires Bar<T> {}");
1629 ASSERT_EQ(Tokens
.size(), 23u) << Tokens
;
1630 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1631 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1632 EXPECT_TOKEN(Tokens
[6], tok::kw_requires
, TT_RequiresClause
);
1633 EXPECT_TRUE(Tokens
[10]->ClosesRequiresClause
);
1634 EXPECT_TOKEN(Tokens
[15], tok::kw_requires
, TT_RequiresClause
);
1635 EXPECT_TRUE(Tokens
[19]->ClosesRequiresClause
);
1636 EXPECT_TOKEN(Tokens
[20], tok::l_brace
, TT_LambdaLBrace
);
1638 Tokens
= annotate("[] <typename T = int> (T t) {}");
1639 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1640 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1641 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1642 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_TemplateCloser
);
1643 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_LambdaLBrace
);
1645 Tokens
= annotate("[] <int I = 0> (T t) {}");
1646 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1647 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1648 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1649 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_TemplateCloser
);
1650 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_LambdaLBrace
);
1652 Tokens
= annotate("[] <bool b = false> (T t) {}");
1653 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1654 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1655 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1656 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_TemplateCloser
);
1657 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_LambdaLBrace
);
1659 Tokens
= annotate("[] <bool b = true && false> (T&& t) {}");
1660 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1661 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1662 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1663 EXPECT_TOKEN(Tokens
[7], tok::ampamp
, TT_BinaryOperator
);
1664 EXPECT_TOKEN(Tokens
[9], tok::greater
, TT_TemplateCloser
);
1665 EXPECT_TOKEN(Tokens
[12], tok::ampamp
, TT_PointerOrReference
);
1666 EXPECT_TOKEN(Tokens
[15], tok::l_brace
, TT_LambdaLBrace
);
1668 Tokens
= annotate("[] <typename T = int> requires Foo<T> (T t) {}");
1669 ASSERT_EQ(Tokens
.size(), 20u) << Tokens
;
1670 EXPECT_TOKEN(Tokens
[0], tok::l_square
, TT_LambdaLSquare
);
1671 EXPECT_TOKEN(Tokens
[2], tok::less
, TT_TemplateOpener
);
1672 EXPECT_TOKEN(Tokens
[7], tok::greater
, TT_TemplateCloser
);
1673 EXPECT_TOKEN(Tokens
[8], tok::kw_requires
, TT_RequiresClause
);
1674 EXPECT_TOKEN(Tokens
[17], tok::l_brace
, TT_LambdaLBrace
);
1677 TEST_F(TokenAnnotatorTest
, UnderstandsFunctionAnnotations
) {
1678 auto Tokens
= annotate("template <typename T>\n"
1679 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
1680 "string OldFunction(const string ¶meter) {}");
1681 ASSERT_EQ(Tokens
.size(), 20u) << Tokens
;
1682 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_FunctionAnnotationRParen
);
1684 Tokens
= annotate("template <typename T>\n"
1686 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1687 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_Unknown
);
1690 TEST_F(TokenAnnotatorTest
, UnderstandsFunctionDeclarationNames
) {
1691 auto Tokens
= annotate("void f [[noreturn]] ();");
1692 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1693 EXPECT_TOKEN(Tokens
[1], tok::identifier
, TT_FunctionDeclarationName
);
1695 Tokens
= annotate("void f [[noreturn]] () {}");
1696 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1697 EXPECT_TOKEN(Tokens
[1], tok::identifier
, TT_FunctionDeclarationName
);
1699 Tokens
= annotate("#define FOO Foo::\n"
1701 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1702 EXPECT_TOKEN(Tokens
[6], tok::identifier
, TT_FunctionDeclarationName
);
1704 Tokens
= annotate("struct Foo {\n"
1707 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
1708 EXPECT_TOKEN(Tokens
[3], tok::identifier
, TT_Unknown
);
1709 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_FunctionTypeLParen
);
1712 TEST_F(TokenAnnotatorTest
, UnderstandsCtorAndDtorDeclNames
) {
1713 auto Tokens
= annotate("class Foo { public: Foo(); };");
1714 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1715 EXPECT_TOKEN(Tokens
[5], tok::identifier
, TT_CtorDtorDeclName
);
1717 Tokens
= annotate("class Foo { public: ~Foo(); };");
1718 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1719 EXPECT_TOKEN(Tokens
[6], tok::identifier
, TT_CtorDtorDeclName
);
1721 Tokens
= annotate("struct Foo { [[deprecated]] Foo() {} };");
1722 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1723 EXPECT_TOKEN(Tokens
[8], tok::identifier
, TT_CtorDtorDeclName
);
1724 EXPECT_TOKEN(Tokens
[11], tok::l_brace
, TT_FunctionLBrace
);
1726 Tokens
= annotate("struct Foo { [[deprecated]] ~Foo() {} };");
1727 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
1728 EXPECT_TOKEN(Tokens
[9], tok::identifier
, TT_CtorDtorDeclName
);
1729 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_FunctionLBrace
);
1731 Tokens
= annotate("struct Foo { Foo() [[deprecated]] {} };");
1732 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1733 EXPECT_TOKEN(Tokens
[3], tok::identifier
, TT_CtorDtorDeclName
);
1734 EXPECT_TOKEN(Tokens
[11], tok::l_brace
, TT_FunctionLBrace
);
1736 Tokens
= annotate("struct Foo { ~Foo() [[deprecated]] {} };");
1737 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
1738 EXPECT_TOKEN(Tokens
[4], tok::identifier
, TT_CtorDtorDeclName
);
1739 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_FunctionLBrace
);
1741 Tokens
= annotate("struct Foo { [[deprecated]] explicit Foo() {} };");
1742 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
1743 EXPECT_TOKEN(Tokens
[9], tok::identifier
, TT_CtorDtorDeclName
);
1744 EXPECT_TOKEN(Tokens
[12], tok::l_brace
, TT_FunctionLBrace
);
1746 Tokens
= annotate("struct Foo { virtual [[deprecated]] ~Foo() {} };");
1747 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1748 EXPECT_TOKEN(Tokens
[10], tok::identifier
, TT_CtorDtorDeclName
);
1749 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_FunctionLBrace
);
1751 Tokens
= annotate("Foo::Foo() {}");
1752 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1753 EXPECT_TOKEN(Tokens
[2], tok::identifier
, TT_CtorDtorDeclName
);
1754 EXPECT_TOKEN(Tokens
[5], tok::l_brace
, TT_FunctionLBrace
);
1756 Tokens
= annotate("Foo::~Foo() {}");
1757 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
1758 EXPECT_TOKEN(Tokens
[3], tok::identifier
, TT_CtorDtorDeclName
);
1759 EXPECT_TOKEN(Tokens
[6], tok::l_brace
, TT_FunctionLBrace
);
1761 Tokens
= annotate("struct Test {\n"
1767 ASSERT_EQ(Tokens
.size(), 25u) << Tokens
;
1768 EXPECT_TOKEN(Tokens
[3], tok::identifier
, TT_CtorDtorDeclName
);
1769 EXPECT_TOKEN(Tokens
[14], tok::identifier
, TT_Unknown
);
1772 TEST_F(TokenAnnotatorTest
, UnderstandsC11GenericSelection
) {
1773 auto Tokens
= annotate("_Generic(x, int: 1, default: 0)");
1774 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1775 EXPECT_TOKEN(Tokens
[0], tok::kw__Generic
, TT_Unknown
);
1776 EXPECT_TOKEN(Tokens
[5], tok::colon
, TT_GenericSelectionColon
);
1777 EXPECT_TOKEN(Tokens
[9], tok::colon
, TT_GenericSelectionColon
);
1780 TEST_F(TokenAnnotatorTest
, UnderstandsTrailingReturnArrow
) {
1781 auto Tokens
= annotate("auto f() -> int;");
1782 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1783 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_TrailingReturnArrow
);
1785 Tokens
= annotate("auto operator->() -> int;");
1786 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
1787 EXPECT_TOKEN(Tokens
[2], tok::arrow
, TT_OverloadedOperator
);
1788 EXPECT_TOKEN(Tokens
[5], tok::arrow
, TT_TrailingReturnArrow
);
1790 Tokens
= annotate("auto operator++(int) -> int;");
1791 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1792 EXPECT_TOKEN(Tokens
[6], tok::arrow
, TT_TrailingReturnArrow
);
1794 Tokens
= annotate("auto operator=() -> int;");
1795 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
1796 EXPECT_TOKEN(Tokens
[5], tok::arrow
, TT_TrailingReturnArrow
);
1798 Tokens
= annotate("auto operator=(int) -> int;");
1799 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1800 EXPECT_TOKEN(Tokens
[6], tok::arrow
, TT_TrailingReturnArrow
);
1802 Tokens
= annotate("auto foo() -> auto { return Val; }");
1803 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1804 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_TrailingReturnArrow
);
1806 Tokens
= annotate("struct S { auto bar() const -> int; };");
1807 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
1808 EXPECT_TOKEN(Tokens
[8], tok::arrow
, TT_TrailingReturnArrow
);
1810 // Not trailing return arrows
1811 Tokens
= annotate("auto a = b->c;");
1812 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1813 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_Unknown
);
1815 Tokens
= annotate("auto a = (b)->c;");
1816 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1817 EXPECT_TOKEN(Tokens
[6], tok::arrow
, TT_Unknown
);
1819 Tokens
= annotate("auto a = b()->c;");
1820 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1821 EXPECT_TOKEN(Tokens
[6], tok::arrow
, TT_Unknown
);
1823 Tokens
= annotate("auto a = b->c();");
1824 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
1825 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_Unknown
);
1827 Tokens
= annotate("decltype(auto) a = b()->c;");
1828 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1829 EXPECT_TOKEN(Tokens
[9], tok::arrow
, TT_Unknown
);
1831 Tokens
= annotate("void f() { auto a = b->c(); }");
1832 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1833 EXPECT_TOKEN(Tokens
[9], tok::arrow
, TT_Unknown
);
1835 Tokens
= annotate("void f() { auto a = b()->c; }");
1836 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
1837 EXPECT_TOKEN(Tokens
[11], tok::arrow
, TT_Unknown
);
1839 Tokens
= annotate("#define P(ptr) auto p = (ptr)->p");
1840 ASSERT_EQ(Tokens
.size(), 15u) << Tokens
;
1841 EXPECT_TOKEN(Tokens
[12], tok::arrow
, TT_Unknown
);
1843 Tokens
= annotate("void f() FOO(foo->bar);");
1844 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
1845 EXPECT_TOKEN(Tokens
[7], tok::arrow
, TT_Unknown
);
1848 Tokens
= annotate("auto f() -> int { auto a = b()->c; }");
1849 ASSERT_EQ(Tokens
.size(), 18u) << Tokens
;
1850 EXPECT_TOKEN(Tokens
[4], tok::arrow
, TT_TrailingReturnArrow
);
1851 EXPECT_TOKEN(Tokens
[13], tok::arrow
, TT_Unknown
);
1854 TEST_F(TokenAnnotatorTest
, UnderstandsAttributeMacros
) {
1855 // '__attribute__' has special handling.
1856 auto Tokens
= annotate("__attribute__(X) void Foo(void);");
1857 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1858 EXPECT_TOKEN(Tokens
[0], tok::kw___attribute
, TT_Unknown
);
1859 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_AttributeLParen
);
1860 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_AttributeRParen
);
1862 // Generic macro has no special handling in this location.
1863 Tokens
= annotate("A(X) void Foo(void);");
1864 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1865 EXPECT_TOKEN(Tokens
[0], tok::identifier
, TT_Unknown
);
1866 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_Unknown
);
1868 // Add a custom AttributeMacro. Test that it has the same behavior.
1869 FormatStyle Style
= getLLVMStyle();
1870 Style
.AttributeMacros
.push_back("A");
1872 // An "AttributeMacro" gets annotated like '__attribute__'.
1873 Tokens
= annotate("A(X) void Foo(void);", Style
);
1874 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1875 EXPECT_TOKEN(Tokens
[0], tok::identifier
, TT_AttributeMacro
);
1876 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_AttributeLParen
);
1877 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_AttributeRParen
);
1880 TEST_F(TokenAnnotatorTest
, UnderstandsAttributeMacrosOnObjCDecl
) {
1881 // '__attribute__' has special handling.
1882 auto Tokens
= annotate("__attribute__(X) @interface Foo");
1883 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1884 EXPECT_TOKEN(Tokens
[0], tok::kw___attribute
, TT_Unknown
);
1885 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_AttributeLParen
);
1886 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_AttributeRParen
);
1888 // Generic macro has no special handling in this location.
1889 Tokens
= annotate("A(X) @interface Foo");
1890 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1891 // Note: Don't check token-type as a random token in this position is hard to
1893 EXPECT_TOKEN_KIND(Tokens
[0], tok::identifier
);
1894 EXPECT_TOKEN_KIND(Tokens
[1], tok::l_paren
);
1896 // Add a custom AttributeMacro. Test that it has the same behavior.
1897 FormatStyle Style
= getLLVMStyle();
1898 Style
.AttributeMacros
.push_back("A");
1900 // An "AttributeMacro" gets annotated like '__attribute__'.
1901 Tokens
= annotate("A(X) @interface Foo", Style
);
1902 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
1903 EXPECT_TOKEN(Tokens
[0], tok::identifier
, TT_AttributeMacro
);
1904 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_AttributeLParen
);
1905 EXPECT_TOKEN(Tokens
[3], tok::r_paren
, TT_AttributeRParen
);
1908 TEST_F(TokenAnnotatorTest
, UnderstandsAttributeMacrosOnObjCMethodDecl
) {
1909 // '__attribute__' has special handling.
1910 auto Tokens
= annotate("- (id)init __attribute__(X);");
1911 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1912 EXPECT_TOKEN(Tokens
[5], tok::kw___attribute
, TT_Unknown
);
1913 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_AttributeLParen
);
1914 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_AttributeRParen
);
1916 // Generic macro has no special handling in this location.
1917 Tokens
= annotate("- (id)init A(X);");
1918 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1919 // Note: Don't check token-type as a random token in this position is hard to
1921 EXPECT_TOKEN_KIND(Tokens
[5], tok::identifier
);
1922 EXPECT_TOKEN_KIND(Tokens
[6], tok::l_paren
);
1924 // Add a custom AttributeMacro. Test that it has the same behavior.
1925 FormatStyle Style
= getLLVMStyle();
1926 Style
.AttributeMacros
.push_back("A");
1928 // An "AttributeMacro" gets annotated like '__attribute__'.
1929 Tokens
= annotate("- (id)init A(X);", Style
);
1930 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
1931 EXPECT_TOKEN(Tokens
[5], tok::identifier
, TT_AttributeMacro
);
1932 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_AttributeLParen
);
1933 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_AttributeRParen
);
1936 TEST_F(TokenAnnotatorTest
, UnderstandsAttributeMacrosOnObjCProperty
) {
1937 // '__attribute__' has special handling.
1938 auto Tokens
= annotate("@property(weak) id delegate __attribute__(X);");
1939 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1940 EXPECT_TOKEN(Tokens
[7], tok::kw___attribute
, TT_Unknown
);
1941 EXPECT_TOKEN(Tokens
[8], tok::l_paren
, TT_AttributeLParen
);
1942 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_AttributeRParen
);
1944 // Generic macro has no special handling in this location.
1945 Tokens
= annotate("@property(weak) id delegate A(X);");
1946 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1947 // Note: Don't check token-type as a random token in this position is hard to
1949 EXPECT_TOKEN_KIND(Tokens
[7], tok::identifier
);
1950 EXPECT_TOKEN_KIND(Tokens
[8], tok::l_paren
);
1952 // Add a custom AttributeMacro. Test that it has the same behavior.
1953 FormatStyle Style
= getLLVMStyle();
1954 Style
.AttributeMacros
.push_back("A");
1956 // An "AttributeMacro" gets annotated like '__attribute__'.
1957 Tokens
= annotate("@property(weak) id delegate A(X);", Style
);
1958 ASSERT_EQ(Tokens
.size(), 13u) << Tokens
;
1959 EXPECT_TOKEN(Tokens
[7], tok::identifier
, TT_AttributeMacro
);
1960 EXPECT_TOKEN(Tokens
[8], tok::l_paren
, TT_AttributeLParen
);
1961 EXPECT_TOKEN(Tokens
[10], tok::r_paren
, TT_AttributeRParen
);
1964 TEST_F(TokenAnnotatorTest
, UnderstandsVerilogOperators
) {
1965 auto Annotate
= [this](llvm::StringRef Code
) {
1966 return annotate(Code
, getLLVMStyle(FormatStyle::LK_Verilog
));
1968 // Test that unary operators get labeled as such and that operators like '++'
1970 tok::TokenKind Unary
[] = {tok::plus
, tok::minus
, tok::exclaim
,
1971 tok::tilde
, tok::amp
, tok::pipe
,
1972 tok::caret
, tok::plusplus
, tok::minusminus
};
1973 for (auto Kind
: Unary
) {
1975 Annotate(std::string("x = ") + tok::getPunctuatorSpelling(Kind
) + "x;");
1976 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
1977 EXPECT_TOKEN(Tokens
[2], Kind
, TT_UnaryOperator
);
1979 // Operators formed by joining two operators like '^~'. For some of these
1980 // joined operators, we don't have a separate type, so we only test for their
1982 std::pair
<prec::Level
, std::string
> JoinedBinary
[] = {
1983 {prec::Comma
, "<->"}, {prec::Assignment
, "+="},
1984 {prec::Assignment
, "-="}, {prec::Assignment
, "*="},
1985 {prec::Assignment
, "/="}, {prec::Assignment
, "%="},
1986 {prec::Assignment
, "&="}, {prec::Assignment
, "^="},
1987 {prec::Assignment
, "<<="}, {prec::Assignment
, ">>="},
1988 {prec::Assignment
, "<<<="}, {prec::Assignment
, ">>>="},
1989 {prec::LogicalOr
, "||"}, {prec::LogicalAnd
, "&&"},
1990 {prec::Equality
, "=="}, {prec::Equality
, "!="},
1991 {prec::Equality
, "==="}, {prec::Equality
, "!=="},
1992 {prec::Equality
, "==?"}, {prec::Equality
, "!=?"},
1993 {prec::ExclusiveOr
, "~^"}, {prec::ExclusiveOr
, "^~"},
1995 for (auto Operator
: JoinedBinary
) {
1996 auto Tokens
= Annotate(std::string("x = x ") + Operator
.second
+ " x;");
1997 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
1998 EXPECT_TOKEN_TYPE(Tokens
[3], TT_BinaryOperator
);
1999 EXPECT_TOKEN_PRECEDENCE(Tokens
[3], Operator
.first
);
2001 // '~^' and '^~' can be unary as well as binary operators.
2002 auto Tokens
= Annotate("x = ~^x;");
2003 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
2004 EXPECT_TOKEN_TYPE(Tokens
[2], TT_UnaryOperator
);
2005 Tokens
= Annotate("x = ^~x;");
2006 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
2007 EXPECT_TOKEN_TYPE(Tokens
[2], TT_UnaryOperator
);
2008 // The unary operators '~&' and '~|' can only be unary operators. The current
2009 // implementation treats each of them as separate unary '~' and '&' or '|'
2010 // operators, which is enough for formatting purposes. In FormatTestVerilog,
2011 // there is a test that there is no space in between. And even if a new line
2012 // is inserted between the '~' and '|', the semantic meaning is the same as
2013 // the joined operator, so the CanBreakBefore property doesn't need to be
2014 // false for the second operator.
2015 Tokens
= Annotate("x = ~&x;");
2016 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2017 EXPECT_TOKEN(Tokens
[2], tok::tilde
, TT_UnaryOperator
);
2018 EXPECT_TOKEN(Tokens
[3], tok::amp
, TT_UnaryOperator
);
2019 Tokens
= Annotate("x = ~|x;");
2020 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2021 EXPECT_TOKEN(Tokens
[2], tok::tilde
, TT_UnaryOperator
);
2022 EXPECT_TOKEN(Tokens
[3], tok::pipe
, TT_UnaryOperator
);
2023 // Test for block label colons.
2024 Tokens
= Annotate("begin : x\n"
2026 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2027 EXPECT_TOKEN(Tokens
[1], tok::colon
, TT_VerilogBlockLabelColon
);
2028 EXPECT_TOKEN(Tokens
[4], tok::colon
, TT_VerilogBlockLabelColon
);
2029 // Test that the dimension colon is annotated correctly.
2030 Tokens
= Annotate("var [1 : 0] x;");
2031 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2032 EXPECT_TOKEN(Tokens
[3], tok::colon
, TT_BitFieldColon
);
2033 Tokens
= Annotate("extern function [1 : 0] x;");
2034 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2035 EXPECT_TOKEN(Tokens
[4], tok::colon
, TT_BitFieldColon
);
2036 Tokens
= Annotate("module test\n"
2037 " (input wire [7 : 0] a[7 : 0]);\n"
2039 ASSERT_EQ(Tokens
.size(), 20u) << Tokens
;
2040 EXPECT_TOKEN(Tokens
[4], tok::identifier
, TT_VerilogDimensionedTypeName
);
2041 EXPECT_TOKEN(Tokens
[7], tok::colon
, TT_BitFieldColon
);
2042 EXPECT_TOKEN(Tokens
[13], tok::colon
, TT_BitFieldColon
);
2043 // Test case labels and ternary operators.
2044 Tokens
= Annotate("case (x)\n"
2048 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2049 EXPECT_TOKEN(Tokens
[5], tok::colon
, TT_CaseLabelColon
);
2050 Tokens
= Annotate("case (x)\n"
2054 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
2055 EXPECT_TOKEN(Tokens
[5], tok::question
, TT_ConditionalExpr
);
2056 EXPECT_TOKEN(Tokens
[7], tok::colon
, TT_ConditionalExpr
);
2057 EXPECT_TOKEN(Tokens
[9], tok::colon
, TT_CaseLabelColon
);
2058 // Non-blocking assignments.
2059 Tokens
= Annotate("a <= b;");
2060 ASSERT_EQ(Tokens
.size(), 5u) << Tokens
;
2061 EXPECT_TOKEN(Tokens
[1], tok::lessequal
, TT_BinaryOperator
);
2062 EXPECT_TOKEN_PRECEDENCE(Tokens
[1], prec::Assignment
);
2063 Tokens
= Annotate("if (a <= b) break;");
2064 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2065 EXPECT_TOKEN(Tokens
[3], tok::lessequal
, TT_BinaryOperator
);
2066 EXPECT_TOKEN_PRECEDENCE(Tokens
[3], prec::Relational
);
2067 Tokens
= Annotate("a <= b <= a;");
2068 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2069 EXPECT_TOKEN(Tokens
[1], tok::lessequal
, TT_BinaryOperator
);
2070 EXPECT_TOKEN_PRECEDENCE(Tokens
[1], prec::Assignment
);
2071 EXPECT_TOKEN(Tokens
[3], tok::lessequal
, TT_BinaryOperator
);
2072 EXPECT_TOKEN_PRECEDENCE(Tokens
[3], prec::Relational
);
2074 // Port lists in module instantiation.
2075 Tokens
= Annotate("module_x instance_1(port_1), instance_2(port_2);");
2076 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
2077 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_VerilogInstancePortLParen
);
2078 EXPECT_TOKEN(Tokens
[7], tok::l_paren
, TT_VerilogInstancePortLParen
);
2079 Tokens
= Annotate("module_x #(parameter) instance_1(port_1), "
2080 "instance_2(port_2);");
2081 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
2082 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_VerilogInstancePortLParen
);
2083 EXPECT_TOKEN(Tokens
[6], tok::l_paren
, TT_VerilogInstancePortLParen
);
2084 EXPECT_TOKEN(Tokens
[11], tok::l_paren
, TT_VerilogInstancePortLParen
);
2086 // Condition parentheses.
2087 Tokens
= Annotate("assert (x);");
2088 ASSERT_EQ(Tokens
.size(), 6u) << Tokens
;
2089 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ConditionLParen
);
2090 Tokens
= Annotate("assert #0 (x);");
2091 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2092 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_ConditionLParen
);
2093 Tokens
= Annotate("assert final (x);");
2094 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2095 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_ConditionLParen
);
2096 Tokens
= Annotate("foreach (x[x]) continue;");
2097 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2098 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ConditionLParen
);
2099 Tokens
= Annotate("repeat (x[x]) continue;");
2100 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2101 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ConditionLParen
);
2102 Tokens
= Annotate("case (x) endcase;");
2103 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2104 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ConditionLParen
);
2106 // Sensitivity list. The TT_Unknown type is clearly not binding for the
2107 // future, please adapt if those tokens get annotated. This test is only here
2108 // to prevent the comma from being annotated as TT_VerilogInstancePortComma.
2109 Tokens
= Annotate("always @(posedge x, posedge y);");
2110 ASSERT_EQ(Tokens
.size(), 11u) << Tokens
;
2111 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_Unknown
);
2112 EXPECT_TOKEN(Tokens
[5], tok::comma
, TT_Unknown
);
2113 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_Unknown
);
2115 // String literals in concatenation.
2116 Tokens
= Annotate("x = {\"\"};");
2117 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2118 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_StringInConcatenation
);
2119 Tokens
= Annotate("x = {\"\", \"\"};");
2120 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2121 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_StringInConcatenation
);
2122 EXPECT_TOKEN(Tokens
[5], tok::string_literal
, TT_StringInConcatenation
);
2123 Tokens
= Annotate("x = '{{\"\"}};");
2124 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2125 EXPECT_TOKEN(Tokens
[5], tok::string_literal
, TT_StringInConcatenation
);
2126 // Cases where the string should not be annotated that type. Fix the
2127 // `TT_Unknown` if needed in the future.
2128 Tokens
= Annotate("x = {\"\" == \"\"};");
2129 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2130 EXPECT_TOKEN(Tokens
[3], tok::string_literal
, TT_Unknown
);
2131 EXPECT_TOKEN(Tokens
[5], tok::string_literal
, TT_Unknown
);
2132 Tokens
= Annotate("x = {(\"\")};");
2133 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2134 EXPECT_TOKEN(Tokens
[4], tok::string_literal
, TT_Unknown
);
2135 Tokens
= Annotate("x = '{\"\"};");
2136 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2137 EXPECT_TOKEN(Tokens
[4], tok::string_literal
, TT_Unknown
);
2140 TEST_F(TokenAnnotatorTest
, UnderstandConstructors
) {
2141 auto Tokens
= annotate("Class::Class() : BaseClass(), Member() {}");
2143 // The TT_Unknown is clearly not binding for the future, please adapt if those
2144 // tokens get annotated.
2145 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
2146 EXPECT_TOKEN(Tokens
[5], tok::colon
, TT_CtorInitializerColon
);
2147 EXPECT_TOKEN(Tokens
[6], tok::identifier
, TT_Unknown
);
2148 EXPECT_TOKEN(Tokens
[7], tok::l_paren
, TT_Unknown
);
2149 EXPECT_TOKEN(Tokens
[8], tok::r_paren
, TT_Unknown
);
2150 EXPECT_TOKEN(Tokens
[9], tok::comma
, TT_CtorInitializerComma
);
2151 EXPECT_TOKEN(Tokens
[10], tok::identifier
, TT_Unknown
);
2152 EXPECT_TOKEN(Tokens
[11], tok::l_paren
, TT_Unknown
);
2153 EXPECT_TOKEN(Tokens
[12], tok::r_paren
, TT_Unknown
);
2154 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_FunctionLBrace
);
2156 Tokens
= annotate("Class::Class() : BaseClass{}, Member{} {}");
2157 ASSERT_EQ(Tokens
.size(), 16u) << Tokens
;
2158 EXPECT_TOKEN(Tokens
[5], tok::colon
, TT_CtorInitializerColon
);
2159 EXPECT_TOKEN(Tokens
[6], tok::identifier
, TT_Unknown
);
2160 EXPECT_TOKEN(Tokens
[7], tok::l_brace
, TT_Unknown
);
2161 EXPECT_TOKEN(Tokens
[8], tok::r_brace
, TT_Unknown
);
2162 EXPECT_TOKEN(Tokens
[9], tok::comma
, TT_CtorInitializerComma
);
2163 EXPECT_TOKEN(Tokens
[10], tok::identifier
, TT_Unknown
);
2164 EXPECT_TOKEN(Tokens
[11], tok::l_brace
, TT_Unknown
);
2165 EXPECT_TOKEN(Tokens
[12], tok::r_brace
, TT_Unknown
);
2166 EXPECT_TOKEN(Tokens
[13], tok::l_brace
, TT_FunctionLBrace
);
2169 TEST_F(TokenAnnotatorTest
, UnderstandsConditionParens
) {
2170 auto Tokens
= annotate("if (x) {}");
2171 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2172 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ConditionLParen
);
2173 Tokens
= annotate("if constexpr (x) {}");
2174 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2175 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_ConditionLParen
);
2176 Tokens
= annotate("if CONSTEXPR (x) {}");
2177 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2178 EXPECT_TOKEN(Tokens
[2], tok::l_paren
, TT_ConditionLParen
);
2179 Tokens
= annotate("if (x) {} else if (x) {}");
2180 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
2181 EXPECT_TOKEN(Tokens
[1], tok::l_paren
, TT_ConditionLParen
);
2182 EXPECT_TOKEN(Tokens
[8], tok::l_paren
, TT_ConditionLParen
);
2185 TEST_F(TokenAnnotatorTest
, CSharpNullableTypes
) {
2186 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_CSharp
);
2188 auto Tokens
= annotate("int? a;", Style
);
2189 EXPECT_EQ(Tokens
.size(), 5u) << Tokens
;
2190 EXPECT_TOKEN(Tokens
[1], tok::question
, TT_CSharpNullable
);
2192 Tokens
= annotate("int? a = 1;", Style
);
2193 EXPECT_EQ(Tokens
.size(), 7u) << Tokens
;
2194 EXPECT_TOKEN(Tokens
[1], tok::question
, TT_CSharpNullable
);
2196 Tokens
= annotate("int?)", Style
);
2197 EXPECT_EQ(Tokens
.size(), 4u) << Tokens
;
2198 EXPECT_TOKEN(Tokens
[1], tok::question
, TT_CSharpNullable
);
2200 Tokens
= annotate("int?>", Style
);
2201 EXPECT_EQ(Tokens
.size(), 4u) << Tokens
;
2202 EXPECT_TOKEN(Tokens
[1], tok::question
, TT_CSharpNullable
);
2204 Tokens
= annotate("cond? id : id2", Style
);
2205 EXPECT_EQ(Tokens
.size(), 6u) << Tokens
;
2206 EXPECT_TOKEN(Tokens
[1], tok::question
, TT_ConditionalExpr
);
2208 Tokens
= annotate("cond ? cond2 ? : id1 : id2", Style
);
2209 EXPECT_EQ(Tokens
.size(), 9u) << Tokens
;
2210 EXPECT_TOKEN(Tokens
[1], tok::question
, TT_ConditionalExpr
);
2213 TEST_F(TokenAnnotatorTest
, UnderstandsLabels
) {
2214 auto Tokens
= annotate("{ x: break; }");
2215 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2216 EXPECT_TOKEN(Tokens
[2], tok::colon
, TT_GotoLabelColon
);
2217 Tokens
= annotate("{ case x: break; }");
2218 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2219 EXPECT_TOKEN(Tokens
[3], tok::colon
, TT_CaseLabelColon
);
2220 Tokens
= annotate("{ x: { break; } }");
2221 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2222 EXPECT_TOKEN(Tokens
[2], tok::colon
, TT_GotoLabelColon
);
2223 Tokens
= annotate("{ case x: { break; } }");
2224 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2225 EXPECT_TOKEN(Tokens
[3], tok::colon
, TT_CaseLabelColon
);
2228 TEST_F(TokenAnnotatorTest
, UnderstandsNestedBlocks
) {
2229 // The closing braces are not annotated. It doesn't seem to cause a problem.
2230 // So we only test for the opening braces.
2231 auto Tokens
= annotate("{\n"
2237 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
2238 EXPECT_BRACE_KIND(Tokens
[0], BK_Block
);
2239 EXPECT_BRACE_KIND(Tokens
[1], BK_Block
);
2240 EXPECT_BRACE_KIND(Tokens
[2], BK_Block
);
2241 EXPECT_BRACE_KIND(Tokens
[10], BK_Block
);
2244 TEST_F(TokenAnnotatorTest
, UnderstandDesignatedInitializers
) {
2245 auto Tokens
= annotate("SomeStruct { .a = 1 };");
2246 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2247 EXPECT_BRACE_KIND(Tokens
[1], BK_BracedInit
);
2248 EXPECT_TOKEN(Tokens
[2], tok::period
, TT_DesignatedInitializerPeriod
);
2250 Tokens
= annotate("SomeStruct { .a = 1, .b = 2 };");
2251 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
2252 EXPECT_BRACE_KIND(Tokens
[1], BK_BracedInit
);
2253 EXPECT_TOKEN(Tokens
[2], tok::period
, TT_DesignatedInitializerPeriod
);
2254 EXPECT_TOKEN(Tokens
[7], tok::period
, TT_DesignatedInitializerPeriod
);
2256 Tokens
= annotate("SomeStruct {\n"
2262 ASSERT_EQ(Tokens
.size(), 19u) << Tokens
;
2263 EXPECT_BRACE_KIND(Tokens
[1], BK_BracedInit
);
2264 EXPECT_TOKEN(Tokens
[5], tok::period
, TT_DesignatedInitializerPeriod
);
2265 EXPECT_TOKEN(Tokens
[12], tok::period
, TT_DesignatedInitializerPeriod
);
2267 Tokens
= annotate("SomeStruct {\n"
2273 ASSERT_EQ(Tokens
.size(), 20u) << Tokens
;
2274 EXPECT_BRACE_KIND(Tokens
[1], BK_BracedInit
);
2275 EXPECT_TOKEN(Tokens
[6], tok::period
, TT_DesignatedInitializerPeriod
);
2276 EXPECT_TOKEN(Tokens
[13], tok::period
, TT_DesignatedInitializerPeriod
);
2279 TEST_F(TokenAnnotatorTest
, UnderstandsJavaScript
) {
2280 auto Annotate
= [this](llvm::StringRef Code
) {
2281 return annotate(Code
, getLLVMStyle(FormatStyle::LK_JavaScript
));
2285 auto Tokens
= Annotate("var x = {'x' : 1, 'y' : 2};");
2286 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
2287 EXPECT_TOKEN(Tokens
[3], tok::l_brace
, TT_DictLiteral
);
2288 EXPECT_TOKEN(Tokens
[4], tok::string_literal
, TT_SelectorName
);
2289 EXPECT_TOKEN(Tokens
[5], tok::colon
, TT_DictLiteral
);
2290 EXPECT_TOKEN(Tokens
[8], tok::string_literal
, TT_SelectorName
);
2291 EXPECT_TOKEN(Tokens
[9], tok::colon
, TT_DictLiteral
);
2292 // Change when we need to annotate these.
2293 EXPECT_BRACE_KIND(Tokens
[3], BK_Unknown
);
2294 EXPECT_BRACE_KIND(Tokens
[11], BK_Unknown
);
2295 EXPECT_TOKEN(Tokens
[11], tok::r_brace
, TT_Unknown
);
2298 TEST_F(TokenAnnotatorTest
, UnderstandsAttributes
) {
2299 auto Tokens
= annotate("bool foo __attribute__((unused));");
2300 ASSERT_EQ(Tokens
.size(), 10u) << Tokens
;
2301 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_AttributeLParen
);
2302 EXPECT_TOKEN(Tokens
[4], tok::l_paren
, TT_Unknown
);
2303 EXPECT_TOKEN(Tokens
[6], tok::r_paren
, TT_Unknown
);
2304 EXPECT_TOKEN(Tokens
[7], tok::r_paren
, TT_AttributeRParen
);
2306 Tokens
= annotate("bool foo __declspec(dllimport);");
2307 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2308 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_AttributeLParen
);
2309 EXPECT_TOKEN(Tokens
[5], tok::r_paren
, TT_AttributeRParen
);
2311 FormatStyle Style
= getLLVMStyle();
2312 Style
.AttributeMacros
.push_back("FOO");
2313 Tokens
= annotate("bool foo FOO(unused);", Style
);
2314 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2315 EXPECT_TOKEN(Tokens
[2], tok::identifier
, TT_AttributeMacro
);
2316 EXPECT_TOKEN(Tokens
[3], tok::l_paren
, TT_AttributeLParen
);
2317 EXPECT_TOKEN(Tokens
[5], tok::r_paren
, TT_AttributeRParen
);
2320 TEST_F(TokenAnnotatorTest
, UnderstandsControlStatements
) {
2321 auto Tokens
= annotate("while (true) {}");
2322 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2323 EXPECT_TOKEN(Tokens
[4], tok::l_brace
, TT_ControlStatementLBrace
);
2324 EXPECT_TOKEN(Tokens
[5], tok::r_brace
, TT_ControlStatementRBrace
);
2326 Tokens
= annotate("for (;;) {}");
2327 ASSERT_EQ(Tokens
.size(), 8u) << Tokens
;
2328 EXPECT_TOKEN(Tokens
[5], tok::l_brace
, TT_ControlStatementLBrace
);
2329 EXPECT_TOKEN(Tokens
[6], tok::r_brace
, TT_ControlStatementRBrace
);
2331 Tokens
= annotate("do {} while (true);");
2332 ASSERT_EQ(Tokens
.size(), 9u) << Tokens
;
2333 EXPECT_TOKEN(Tokens
[1], tok::l_brace
, TT_ControlStatementLBrace
);
2334 EXPECT_TOKEN(Tokens
[2], tok::r_brace
, TT_ControlStatementRBrace
);
2336 Tokens
= annotate("if (true) {} else if (false) {} else {}");
2337 ASSERT_EQ(Tokens
.size(), 17u) << Tokens
;
2338 EXPECT_TOKEN(Tokens
[4], tok::l_brace
, TT_ControlStatementLBrace
);
2339 EXPECT_TOKEN(Tokens
[5], tok::r_brace
, TT_ControlStatementRBrace
);
2340 EXPECT_TOKEN(Tokens
[11], tok::l_brace
, TT_ControlStatementLBrace
);
2341 EXPECT_TOKEN(Tokens
[12], tok::r_brace
, TT_ControlStatementRBrace
);
2342 EXPECT_TOKEN(Tokens
[14], tok::l_brace
, TT_ElseLBrace
);
2343 EXPECT_TOKEN(Tokens
[15], tok::r_brace
, TT_ElseRBrace
);
2345 Tokens
= annotate("switch (foo) {}");
2346 ASSERT_EQ(Tokens
.size(), 7u) << Tokens
;
2347 EXPECT_TOKEN(Tokens
[4], tok::l_brace
, TT_ControlStatementLBrace
);
2348 EXPECT_TOKEN(Tokens
[5], tok::r_brace
, TT_ControlStatementRBrace
);
2351 TEST_F(TokenAnnotatorTest
, UnderstandsDoWhile
) {
2352 auto Tokens
= annotate("do { ++i; } while ( i > 5 );");
2353 ASSERT_EQ(Tokens
.size(), 14u) << Tokens
;
2354 EXPECT_TOKEN(Tokens
[6], tok::kw_while
, TT_DoWhile
);
2356 Tokens
= annotate("do ++i; while ( i > 5 );");
2357 ASSERT_EQ(Tokens
.size(), 12u) << Tokens
;
2358 EXPECT_TOKEN(Tokens
[4], tok::kw_while
, TT_DoWhile
);
2362 } // namespace format
2363 } // namespace clang