1 | //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "FormatTestBase.h" |
10 | |
11 | #define DEBUG_TYPE "format-test" |
12 | |
13 | namespace clang { |
14 | namespace format { |
15 | namespace test { |
16 | namespace { |
17 | |
18 | class FormatTest : public test::FormatTestBase {}; |
19 | |
20 | TEST_F(FormatTest, MessUp) { |
21 | EXPECT_EQ("1 2 3", messUp( "1 2 3")); |
22 | EXPECT_EQ("1 2 3", messUp( "1\n2\n3")); |
23 | EXPECT_EQ("a\n//b\nc", messUp( "a\n//b\nc")); |
24 | EXPECT_EQ("a\n#b\nc", messUp( "a\n#b\nc")); |
25 | EXPECT_EQ("a\n#b c d\ne", messUp( "a\n#b\\\nc\\\nd\ne")); |
26 | } |
27 | |
28 | TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { |
29 | EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); |
30 | } |
31 | |
32 | TEST_F(FormatTest, LLVMStyleOverride) { |
33 | EXPECT_EQ(FormatStyle::LK_Proto, |
34 | getLLVMStyle(FormatStyle::LK_Proto).Language); |
35 | } |
36 | |
37 | //===----------------------------------------------------------------------===// |
38 | // Basic function tests. |
39 | //===----------------------------------------------------------------------===// |
40 | |
41 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); } |
42 | |
43 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
44 | verifyFormat("int i;", " int i;"); |
45 | verifyFormat("\nint i;", " \n\t \v \f int i;"); |
46 | verifyFormat("int i;\nint j;", " int i; int j;"); |
47 | verifyFormat("int i;\nint j;", " int i;\n int j;"); |
48 | |
49 | auto Style = getLLVMStyle(); |
50 | Style.KeepEmptyLines.AtStartOfFile = false; |
51 | verifyFormat("int i;", " \n\t \v \f int i;", Style); |
52 | } |
53 | |
54 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
55 | verifyFormat("int i;", "int\ni;"); |
56 | } |
57 | |
58 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
59 | verifyFormat("{\n" |
60 | " {\n" |
61 | " {\n" |
62 | " }\n" |
63 | " }\n" |
64 | "}", |
65 | "{{{}}}"); |
66 | } |
67 | |
68 | TEST_F(FormatTest, FormatsNestedCall) { |
69 | verifyFormat("Method(f1, f2(f3));"); |
70 | verifyFormat("Method(f1(f2, f3()));"); |
71 | verifyFormat("Method(f1(f2, (f3())));"); |
72 | } |
73 | |
74 | TEST_F(FormatTest, NestedNameSpecifiers) { |
75 | verifyFormat("vector<::Type> v;"); |
76 | verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); |
77 | verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); |
78 | verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); |
79 | verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); |
80 | verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); |
81 | verifyFormat("bool a = 2 < ::SomeFunction();"); |
82 | verifyFormat("ALWAYS_INLINE ::std::string getName();"); |
83 | verifyFormat("some::string getName();"); |
84 | } |
85 | |
86 | TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { |
87 | verifyFormat("if (a) {\n" |
88 | " f();\n" |
89 | "}", |
90 | "if(a){f();}"); |
91 | EXPECT_EQ(4, ReplacementCount); |
92 | verifyNoChange("if (a) {\n" |
93 | " f();\n" |
94 | "}"); |
95 | EXPECT_EQ(0, ReplacementCount); |
96 | verifyNoChange("/*\r\n" |
97 | "\r\n" |
98 | "*/"); |
99 | EXPECT_EQ(0, ReplacementCount); |
100 | } |
101 | |
102 | TEST_F(FormatTest, RemovesEmptyLines) { |
103 | verifyFormat("class C {\n" |
104 | " int i;\n" |
105 | "};", |
106 | "class C {\n" |
107 | " int i;\n" |
108 | "\n" |
109 | "};"); |
110 | |
111 | // Don't remove empty lines at the start of namespaces or extern "C" blocks. |
112 | verifyFormat("namespace N {\n" |
113 | "\n" |
114 | "int i;\n" |
115 | "}", |
116 | "namespace N {\n" |
117 | "\n" |
118 | "int i;\n" |
119 | "}", |
120 | getGoogleStyle()); |
121 | verifyFormat("/* something */ namespace N {\n" |
122 | "\n" |
123 | "int i;\n" |
124 | "}", |
125 | "/* something */ namespace N {\n" |
126 | "\n" |
127 | "int i;\n" |
128 | "}", |
129 | getGoogleStyle()); |
130 | verifyFormat("inline namespace N {\n" |
131 | "\n" |
132 | "int i;\n" |
133 | "}", |
134 | "inline namespace N {\n" |
135 | "\n" |
136 | "int i;\n" |
137 | "}", |
138 | getGoogleStyle()); |
139 | verifyFormat("/* something */ inline namespace N {\n" |
140 | "\n" |
141 | "int i;\n" |
142 | "}", |
143 | "/* something */ inline namespace N {\n" |
144 | "\n" |
145 | "int i;\n" |
146 | "}", |
147 | getGoogleStyle()); |
148 | verifyFormat("export namespace N {\n" |
149 | "\n" |
150 | "int i;\n" |
151 | "}", |
152 | "export namespace N {\n" |
153 | "\n" |
154 | "int i;\n" |
155 | "}", |
156 | getGoogleStyle()); |
157 | verifyFormat("extern /**/ \"C\" /**/ {\n" |
158 | "\n" |
159 | "int i;\n" |
160 | "}", |
161 | "extern /**/ \"C\" /**/ {\n" |
162 | "\n" |
163 | "int i;\n" |
164 | "}", |
165 | getGoogleStyle()); |
166 | |
167 | auto CustomStyle = getLLVMStyle(); |
168 | CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; |
169 | CustomStyle.BraceWrapping.AfterNamespace = true; |
170 | CustomStyle.KeepEmptyLines.AtStartOfBlock = false; |
171 | verifyFormat("namespace N\n" |
172 | "{\n" |
173 | "\n" |
174 | "int i;\n" |
175 | "}", |
176 | "namespace N\n" |
177 | "{\n" |
178 | "\n" |
179 | "\n" |
180 | "int i;\n" |
181 | "}", |
182 | CustomStyle); |
183 | verifyFormat("/* something */ namespace N\n" |
184 | "{\n" |
185 | "\n" |
186 | "int i;\n" |
187 | "}", |
188 | "/* something */ namespace N {\n" |
189 | "\n" |
190 | "\n" |
191 | "int i;\n" |
192 | "}", |
193 | CustomStyle); |
194 | verifyFormat("inline namespace N\n" |
195 | "{\n" |
196 | "\n" |
197 | "int i;\n" |
198 | "}", |
199 | "inline namespace N\n" |
200 | "{\n" |
201 | "\n" |
202 | "\n" |
203 | "int i;\n" |
204 | "}", |
205 | CustomStyle); |
206 | verifyFormat("/* something */ inline namespace N\n" |
207 | "{\n" |
208 | "\n" |
209 | "int i;\n" |
210 | "}", |
211 | "/* something */ inline namespace N\n" |
212 | "{\n" |
213 | "\n" |
214 | "int i;\n" |
215 | "}", |
216 | CustomStyle); |
217 | verifyFormat("export namespace N\n" |
218 | "{\n" |
219 | "\n" |
220 | "int i;\n" |
221 | "}", |
222 | "export namespace N\n" |
223 | "{\n" |
224 | "\n" |
225 | "int i;\n" |
226 | "}", |
227 | CustomStyle); |
228 | verifyFormat("namespace a\n" |
229 | "{\n" |
230 | "namespace b\n" |
231 | "{\n" |
232 | "\n" |
233 | "class AA {};\n" |
234 | "\n" |
235 | "} // namespace b\n" |
236 | "} // namespace a", |
237 | "namespace a\n" |
238 | "{\n" |
239 | "namespace b\n" |
240 | "{\n" |
241 | "\n" |
242 | "\n" |
243 | "class AA {};\n" |
244 | "\n" |
245 | "\n" |
246 | "}\n" |
247 | "}", |
248 | CustomStyle); |
249 | verifyFormat("namespace A /* comment */\n" |
250 | "{\n" |
251 | "class B {}\n" |
252 | "} // namespace A", |
253 | "namespace A /* comment */ { class B {} }", CustomStyle); |
254 | verifyFormat("namespace A\n" |
255 | "{ /* comment */\n" |
256 | "class B {}\n" |
257 | "} // namespace A", |
258 | "namespace A {/* comment */ class B {} }", CustomStyle); |
259 | verifyFormat("namespace A\n" |
260 | "{ /* comment */\n" |
261 | "\n" |
262 | "class B {}\n" |
263 | "\n" |
264 | "" |
265 | "} // namespace A", |
266 | "namespace A { /* comment */\n" |
267 | "\n" |
268 | "\n" |
269 | "class B {}\n" |
270 | "\n" |
271 | "\n" |
272 | "}", |
273 | CustomStyle); |
274 | verifyFormat("namespace A /* comment */\n" |
275 | "{\n" |
276 | "\n" |
277 | "class B {}\n" |
278 | "\n" |
279 | "} // namespace A", |
280 | "namespace A/* comment */ {\n" |
281 | "\n" |
282 | "\n" |
283 | "class B {}\n" |
284 | "\n" |
285 | "\n" |
286 | "}", |
287 | CustomStyle); |
288 | |
289 | // ...but do keep inlining and removing empty lines for non-block extern "C" |
290 | // functions. |
291 | verifyGoogleFormat("extern \"C\" int f() { return 42; }"); |
292 | verifyFormat("extern \"C\" int f() {\n" |
293 | " int i = 42;\n" |
294 | " return i;\n" |
295 | "}", |
296 | "extern \"C\" int f() {\n" |
297 | "\n" |
298 | " int i = 42;\n" |
299 | " return i;\n" |
300 | "}", |
301 | getGoogleStyle()); |
302 | |
303 | // Remove empty lines at the beginning and end of blocks. |
304 | verifyFormat("void f() {\n" |
305 | "\n" |
306 | " if (a) {\n" |
307 | "\n" |
308 | " f();\n" |
309 | " }\n" |
310 | "}", |
311 | "void f() {\n" |
312 | "\n" |
313 | " if (a) {\n" |
314 | "\n" |
315 | " f();\n" |
316 | "\n" |
317 | " }\n" |
318 | "\n" |
319 | "}"); |
320 | verifyFormat("void f() {\n" |
321 | " if (a) {\n" |
322 | " f();\n" |
323 | " }\n" |
324 | "}", |
325 | "void f() {\n" |
326 | "\n" |
327 | " if (a) {\n" |
328 | "\n" |
329 | " f();\n" |
330 | "\n" |
331 | " }\n" |
332 | "\n" |
333 | "}", |
334 | getGoogleStyle()); |
335 | |
336 | // Don't remove empty lines in more complex control statements. |
337 | verifyFormat("void f() {\n" |
338 | " if (a) {\n" |
339 | " f();\n" |
340 | "\n" |
341 | " } else if (b) {\n" |
342 | " f();\n" |
343 | " }\n" |
344 | "}", |
345 | "void f() {\n" |
346 | " if (a) {\n" |
347 | " f();\n" |
348 | "\n" |
349 | " } else if (b) {\n" |
350 | " f();\n" |
351 | "\n" |
352 | " }\n" |
353 | "\n" |
354 | "}"); |
355 | |
356 | // Don't remove empty lines before namespace endings. |
357 | FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); |
358 | LLVMWithNoNamespaceFix.FixNamespaceComments = false; |
359 | verifyNoChange("namespace {\n" |
360 | "int i;\n" |
361 | "\n" |
362 | "}", |
363 | LLVMWithNoNamespaceFix); |
364 | verifyFormat("namespace {\n" |
365 | "int i;\n" |
366 | "}", |
367 | LLVMWithNoNamespaceFix); |
368 | verifyNoChange("namespace {\n" |
369 | "int i;\n" |
370 | "\n" |
371 | "};", |
372 | LLVMWithNoNamespaceFix); |
373 | verifyFormat("namespace {\n" |
374 | "int i;\n" |
375 | "};", |
376 | LLVMWithNoNamespaceFix); |
377 | verifyNoChange("namespace {\n" |
378 | "int i;\n" |
379 | "\n" |
380 | "}"); |
381 | verifyFormat("namespace {\n" |
382 | "int i;\n" |
383 | "\n" |
384 | "} // namespace", |
385 | "namespace {\n" |
386 | "int i;\n" |
387 | "\n" |
388 | "} // namespace"); |
389 | |
390 | FormatStyle Style = getLLVMStyle(); |
391 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
392 | Style.MaxEmptyLinesToKeep = 2; |
393 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
394 | Style.BraceWrapping.AfterClass = true; |
395 | Style.BraceWrapping.AfterFunction = true; |
396 | Style.KeepEmptyLines.AtStartOfBlock = false; |
397 | |
398 | verifyFormat("class Foo\n" |
399 | "{\n" |
400 | " Foo() {}\n" |
401 | "\n" |
402 | " void funk() {}\n" |
403 | "};", |
404 | "class Foo\n" |
405 | "{\n" |
406 | " Foo()\n" |
407 | " {\n" |
408 | " }\n" |
409 | "\n" |
410 | " void funk() {}\n" |
411 | "};", |
412 | Style); |
413 | } |
414 | |
415 | TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { |
416 | verifyFormat("x = (a) and (b);"); |
417 | verifyFormat("x = (a) or (b);"); |
418 | verifyFormat("x = (a) bitand (b);"); |
419 | verifyFormat("x = (a) bitor (b);"); |
420 | verifyFormat("x = (a) not_eq (b);"); |
421 | verifyFormat("x = (a) and_eq (b);"); |
422 | verifyFormat("x = (a) or_eq (b);"); |
423 | verifyFormat("x = (a) xor (b);"); |
424 | } |
425 | |
426 | TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { |
427 | verifyFormat("x = compl(a);"); |
428 | verifyFormat("x = not(a);"); |
429 | verifyFormat("x = bitand(a);"); |
430 | // Unary operator must not be merged with the next identifier |
431 | verifyFormat("x = compl a;"); |
432 | verifyFormat("x = not a;"); |
433 | verifyFormat("x = bitand a;"); |
434 | } |
435 | |
436 | //===----------------------------------------------------------------------===// |
437 | // Tests for control statements. |
438 | //===----------------------------------------------------------------------===// |
439 | |
440 | TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { |
441 | verifyFormat("if (true)\n f();\ng();"); |
442 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
443 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
444 | verifyFormat("if constexpr (true)\n" |
445 | " f();\ng();"); |
446 | verifyFormat("if CONSTEXPR (true)\n" |
447 | " f();\ng();"); |
448 | verifyFormat("if constexpr (a)\n" |
449 | " if constexpr (b)\n" |
450 | " if constexpr (c)\n" |
451 | " g();\n" |
452 | "h();"); |
453 | verifyFormat("if CONSTEXPR (a)\n" |
454 | " if CONSTEXPR (b)\n" |
455 | " if CONSTEXPR (c)\n" |
456 | " g();\n" |
457 | "h();"); |
458 | verifyFormat("if constexpr (a)\n" |
459 | " if constexpr (b) {\n" |
460 | " f();\n" |
461 | " }\n" |
462 | "g();"); |
463 | verifyFormat("if CONSTEXPR (a)\n" |
464 | " if CONSTEXPR (b) {\n" |
465 | " f();\n" |
466 | " }\n" |
467 | "g();"); |
468 | |
469 | verifyFormat("if consteval {\n}"); |
470 | verifyFormat("if !consteval {\n}"); |
471 | verifyFormat("if not consteval {\n}"); |
472 | verifyFormat("if consteval {\n} else {\n}"); |
473 | verifyFormat("if !consteval {\n} else {\n}"); |
474 | verifyFormat("if consteval {\n" |
475 | " f();\n" |
476 | "}"); |
477 | verifyFormat("if !consteval {\n" |
478 | " f();\n" |
479 | "}"); |
480 | verifyFormat("if consteval {\n" |
481 | " f();\n" |
482 | "} else {\n" |
483 | " g();\n" |
484 | "}"); |
485 | verifyFormat("if CONSTEVAL {\n" |
486 | " f();\n" |
487 | "}"); |
488 | verifyFormat("if !CONSTEVAL {\n" |
489 | " f();\n" |
490 | "}"); |
491 | |
492 | verifyFormat("if (a)\n" |
493 | " g();"); |
494 | verifyFormat("if (a) {\n" |
495 | " g()\n" |
496 | "};"); |
497 | verifyFormat("if (a)\n" |
498 | " g();\n" |
499 | "else\n" |
500 | " g();"); |
501 | verifyFormat("if (a) {\n" |
502 | " g();\n" |
503 | "} else\n" |
504 | " g();"); |
505 | verifyFormat("if (a)\n" |
506 | " g();\n" |
507 | "else {\n" |
508 | " g();\n" |
509 | "}"); |
510 | verifyFormat("if (a) {\n" |
511 | " g();\n" |
512 | "} else {\n" |
513 | " g();\n" |
514 | "}"); |
515 | verifyFormat("if (a)\n" |
516 | " g();\n" |
517 | "else if (b)\n" |
518 | " g();\n" |
519 | "else\n" |
520 | " g();"); |
521 | verifyFormat("if (a) {\n" |
522 | " g();\n" |
523 | "} else if (b)\n" |
524 | " g();\n" |
525 | "else\n" |
526 | " g();"); |
527 | verifyFormat("if (a)\n" |
528 | " g();\n" |
529 | "else if (b) {\n" |
530 | " g();\n" |
531 | "} else\n" |
532 | " g();"); |
533 | verifyFormat("if (a)\n" |
534 | " g();\n" |
535 | "else if (b)\n" |
536 | " g();\n" |
537 | "else {\n" |
538 | " g();\n" |
539 | "}"); |
540 | verifyFormat("if (a)\n" |
541 | " g();\n" |
542 | "else if (b) {\n" |
543 | " g();\n" |
544 | "} else {\n" |
545 | " g();\n" |
546 | "}"); |
547 | verifyFormat("if (a) {\n" |
548 | " g();\n" |
549 | "} else if (b) {\n" |
550 | " g();\n" |
551 | "} else {\n" |
552 | " g();\n" |
553 | "}"); |
554 | |
555 | FormatStyle AllowsMergedIf = getLLVMStyle(); |
556 | AllowsMergedIf.IfMacros.push_back(x: "MYIF"); |
557 | AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
558 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = |
559 | FormatStyle::SIS_WithoutElse; |
560 | verifyFormat("if (a)\n" |
561 | " // comment\n" |
562 | " f();", |
563 | AllowsMergedIf); |
564 | verifyFormat("{\n" |
565 | " if (a)\n" |
566 | " label:\n" |
567 | " f();\n" |
568 | "}", |
569 | AllowsMergedIf); |
570 | verifyFormat("#define A \\\n" |
571 | " if (a) \\\n" |
572 | " label: \\\n" |
573 | " f()", |
574 | AllowsMergedIf); |
575 | verifyFormat("if (a)\n" |
576 | " ;", |
577 | AllowsMergedIf); |
578 | verifyFormat("if (a)\n" |
579 | " if (b) return;", |
580 | AllowsMergedIf); |
581 | |
582 | verifyFormat("if (a) // Can't merge this\n" |
583 | " f();", |
584 | AllowsMergedIf); |
585 | verifyFormat("if (a) /* still don't merge */\n" |
586 | " f();", |
587 | AllowsMergedIf); |
588 | verifyFormat("if (a) { // Never merge this\n" |
589 | " f();\n" |
590 | "}", |
591 | AllowsMergedIf); |
592 | verifyFormat("if (a) { /* Never merge this */\n" |
593 | " f();\n" |
594 | "}", |
595 | AllowsMergedIf); |
596 | verifyFormat("MYIF (a)\n" |
597 | " // comment\n" |
598 | " f();", |
599 | AllowsMergedIf); |
600 | verifyFormat("{\n" |
601 | " MYIF (a)\n" |
602 | " label:\n" |
603 | " f();\n" |
604 | "}", |
605 | AllowsMergedIf); |
606 | verifyFormat("#define A \\\n" |
607 | " MYIF (a) \\\n" |
608 | " label: \\\n" |
609 | " f()", |
610 | AllowsMergedIf); |
611 | verifyFormat("MYIF (a)\n" |
612 | " ;", |
613 | AllowsMergedIf); |
614 | verifyFormat("MYIF (a)\n" |
615 | " MYIF (b) return;", |
616 | AllowsMergedIf); |
617 | |
618 | verifyFormat("MYIF (a) // Can't merge this\n" |
619 | " f();", |
620 | AllowsMergedIf); |
621 | verifyFormat("MYIF (a) /* still don't merge */\n" |
622 | " f();", |
623 | AllowsMergedIf); |
624 | verifyFormat("MYIF (a) { // Never merge this\n" |
625 | " f();\n" |
626 | "}", |
627 | AllowsMergedIf); |
628 | verifyFormat("MYIF (a) { /* Never merge this */\n" |
629 | " f();\n" |
630 | "}", |
631 | AllowsMergedIf); |
632 | |
633 | AllowsMergedIf.ColumnLimit = 14; |
634 | // Where line-lengths matter, a 2-letter synonym that maintains line length. |
635 | // Not IF to avoid any confusion that IF is somehow special. |
636 | AllowsMergedIf.IfMacros.push_back(x: "FI"); |
637 | verifyFormat("if (a) return;", AllowsMergedIf); |
638 | verifyFormat("if (aaaaaaaaa)\n" |
639 | " return;", |
640 | AllowsMergedIf); |
641 | verifyFormat("FI (a) return;", AllowsMergedIf); |
642 | verifyFormat("FI (aaaaaaaaa)\n" |
643 | " return;", |
644 | AllowsMergedIf); |
645 | |
646 | AllowsMergedIf.ColumnLimit = 13; |
647 | verifyFormat("if (a)\n return;", AllowsMergedIf); |
648 | verifyFormat("FI (a)\n return;", AllowsMergedIf); |
649 | |
650 | FormatStyle AllowsMergedIfElse = getLLVMStyle(); |
651 | AllowsMergedIfElse.IfMacros.push_back(x: "MYIF"); |
652 | AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = |
653 | FormatStyle::SIS_AllIfsAndElse; |
654 | verifyFormat("if (a)\n" |
655 | " // comment\n" |
656 | " f();\n" |
657 | "else\n" |
658 | " // comment\n" |
659 | " f();", |
660 | AllowsMergedIfElse); |
661 | verifyFormat("{\n" |
662 | " if (a)\n" |
663 | " label:\n" |
664 | " f();\n" |
665 | " else\n" |
666 | " label:\n" |
667 | " f();\n" |
668 | "}", |
669 | AllowsMergedIfElse); |
670 | verifyFormat("if (a)\n" |
671 | " ;\n" |
672 | "else\n" |
673 | " ;", |
674 | AllowsMergedIfElse); |
675 | verifyFormat("if (a) {\n" |
676 | "} else {\n" |
677 | "}", |
678 | AllowsMergedIfElse); |
679 | verifyFormat("if (a) return;\n" |
680 | "else if (b) return;\n" |
681 | "else return;", |
682 | AllowsMergedIfElse); |
683 | verifyFormat("if (a) {\n" |
684 | "} else return;", |
685 | AllowsMergedIfElse); |
686 | verifyFormat("if (a) {\n" |
687 | "} else if (b) return;\n" |
688 | "else return;", |
689 | AllowsMergedIfElse); |
690 | verifyFormat("if (a) return;\n" |
691 | "else if (b) {\n" |
692 | "} else return;", |
693 | AllowsMergedIfElse); |
694 | verifyFormat("if (a)\n" |
695 | " if (b) return;\n" |
696 | " else return;", |
697 | AllowsMergedIfElse); |
698 | verifyFormat("if constexpr (a)\n" |
699 | " if constexpr (b) return;\n" |
700 | " else if constexpr (c) return;\n" |
701 | " else return;", |
702 | AllowsMergedIfElse); |
703 | verifyFormat("MYIF (a)\n" |
704 | " // comment\n" |
705 | " f();\n" |
706 | "else\n" |
707 | " // comment\n" |
708 | " f();", |
709 | AllowsMergedIfElse); |
710 | verifyFormat("{\n" |
711 | " MYIF (a)\n" |
712 | " label:\n" |
713 | " f();\n" |
714 | " else\n" |
715 | " label:\n" |
716 | " f();\n" |
717 | "}", |
718 | AllowsMergedIfElse); |
719 | verifyFormat("MYIF (a)\n" |
720 | " ;\n" |
721 | "else\n" |
722 | " ;", |
723 | AllowsMergedIfElse); |
724 | verifyFormat("MYIF (a) {\n" |
725 | "} else {\n" |
726 | "}", |
727 | AllowsMergedIfElse); |
728 | verifyFormat("MYIF (a) return;\n" |
729 | "else MYIF (b) return;\n" |
730 | "else return;", |
731 | AllowsMergedIfElse); |
732 | verifyFormat("MYIF (a) {\n" |
733 | "} else return;", |
734 | AllowsMergedIfElse); |
735 | verifyFormat("MYIF (a) {\n" |
736 | "} else MYIF (b) return;\n" |
737 | "else return;", |
738 | AllowsMergedIfElse); |
739 | verifyFormat("MYIF (a) return;\n" |
740 | "else MYIF (b) {\n" |
741 | "} else return;", |
742 | AllowsMergedIfElse); |
743 | verifyFormat("MYIF (a)\n" |
744 | " MYIF (b) return;\n" |
745 | " else return;", |
746 | AllowsMergedIfElse); |
747 | verifyFormat("MYIF constexpr (a)\n" |
748 | " MYIF constexpr (b) return;\n" |
749 | " else MYIF constexpr (c) return;\n" |
750 | " else return;", |
751 | AllowsMergedIfElse); |
752 | } |
753 | |
754 | TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { |
755 | FormatStyle AllowsMergedIf = getLLVMStyle(); |
756 | AllowsMergedIf.IfMacros.push_back(x: "MYIF"); |
757 | AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
758 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = |
759 | FormatStyle::SIS_WithoutElse; |
760 | verifyFormat("if (a)\n" |
761 | " f();\n" |
762 | "else {\n" |
763 | " g();\n" |
764 | "}", |
765 | AllowsMergedIf); |
766 | verifyFormat("if (a)\n" |
767 | " f();\n" |
768 | "else\n" |
769 | " g();", |
770 | AllowsMergedIf); |
771 | |
772 | verifyFormat("if (a) g();", AllowsMergedIf); |
773 | verifyFormat("if (a) {\n" |
774 | " g()\n" |
775 | "};", |
776 | AllowsMergedIf); |
777 | verifyFormat("if (a)\n" |
778 | " g();\n" |
779 | "else\n" |
780 | " g();", |
781 | AllowsMergedIf); |
782 | verifyFormat("if (a) {\n" |
783 | " g();\n" |
784 | "} else\n" |
785 | " g();", |
786 | AllowsMergedIf); |
787 | verifyFormat("if (a)\n" |
788 | " g();\n" |
789 | "else {\n" |
790 | " g();\n" |
791 | "}", |
792 | AllowsMergedIf); |
793 | verifyFormat("if (a) {\n" |
794 | " g();\n" |
795 | "} else {\n" |
796 | " g();\n" |
797 | "}", |
798 | AllowsMergedIf); |
799 | verifyFormat("if (a)\n" |
800 | " g();\n" |
801 | "else if (b)\n" |
802 | " g();\n" |
803 | "else\n" |
804 | " g();", |
805 | AllowsMergedIf); |
806 | verifyFormat("if (a) {\n" |
807 | " g();\n" |
808 | "} else if (b)\n" |
809 | " g();\n" |
810 | "else\n" |
811 | " g();", |
812 | AllowsMergedIf); |
813 | verifyFormat("if (a)\n" |
814 | " g();\n" |
815 | "else if (b) {\n" |
816 | " g();\n" |
817 | "} else\n" |
818 | " g();", |
819 | AllowsMergedIf); |
820 | verifyFormat("if (a)\n" |
821 | " g();\n" |
822 | "else if (b)\n" |
823 | " g();\n" |
824 | "else {\n" |
825 | " g();\n" |
826 | "}", |
827 | AllowsMergedIf); |
828 | verifyFormat("if (a)\n" |
829 | " g();\n" |
830 | "else if (b) {\n" |
831 | " g();\n" |
832 | "} else {\n" |
833 | " g();\n" |
834 | "}", |
835 | AllowsMergedIf); |
836 | verifyFormat("if (a) {\n" |
837 | " g();\n" |
838 | "} else if (b) {\n" |
839 | " g();\n" |
840 | "} else {\n" |
841 | " g();\n" |
842 | "}", |
843 | AllowsMergedIf); |
844 | verifyFormat("MYIF (a)\n" |
845 | " f();\n" |
846 | "else {\n" |
847 | " g();\n" |
848 | "}", |
849 | AllowsMergedIf); |
850 | verifyFormat("MYIF (a)\n" |
851 | " f();\n" |
852 | "else\n" |
853 | " g();", |
854 | AllowsMergedIf); |
855 | |
856 | verifyFormat("MYIF (a) g();", AllowsMergedIf); |
857 | verifyFormat("MYIF (a) {\n" |
858 | " g()\n" |
859 | "};", |
860 | AllowsMergedIf); |
861 | verifyFormat("MYIF (a)\n" |
862 | " g();\n" |
863 | "else\n" |
864 | " g();", |
865 | AllowsMergedIf); |
866 | verifyFormat("MYIF (a) {\n" |
867 | " g();\n" |
868 | "} else\n" |
869 | " g();", |
870 | AllowsMergedIf); |
871 | verifyFormat("MYIF (a)\n" |
872 | " g();\n" |
873 | "else {\n" |
874 | " g();\n" |
875 | "}", |
876 | AllowsMergedIf); |
877 | verifyFormat("MYIF (a) {\n" |
878 | " g();\n" |
879 | "} else {\n" |
880 | " g();\n" |
881 | "}", |
882 | AllowsMergedIf); |
883 | verifyFormat("MYIF (a)\n" |
884 | " g();\n" |
885 | "else MYIF (b)\n" |
886 | " g();\n" |
887 | "else\n" |
888 | " g();", |
889 | AllowsMergedIf); |
890 | verifyFormat("MYIF (a)\n" |
891 | " g();\n" |
892 | "else if (b)\n" |
893 | " g();\n" |
894 | "else\n" |
895 | " g();", |
896 | AllowsMergedIf); |
897 | verifyFormat("MYIF (a) {\n" |
898 | " g();\n" |
899 | "} else MYIF (b)\n" |
900 | " g();\n" |
901 | "else\n" |
902 | " g();", |
903 | AllowsMergedIf); |
904 | verifyFormat("MYIF (a) {\n" |
905 | " g();\n" |
906 | "} else if (b)\n" |
907 | " g();\n" |
908 | "else\n" |
909 | " g();", |
910 | AllowsMergedIf); |
911 | verifyFormat("MYIF (a)\n" |
912 | " g();\n" |
913 | "else MYIF (b) {\n" |
914 | " g();\n" |
915 | "} else\n" |
916 | " g();", |
917 | AllowsMergedIf); |
918 | verifyFormat("MYIF (a)\n" |
919 | " g();\n" |
920 | "else if (b) {\n" |
921 | " g();\n" |
922 | "} else\n" |
923 | " g();", |
924 | AllowsMergedIf); |
925 | verifyFormat("MYIF (a)\n" |
926 | " g();\n" |
927 | "else MYIF (b)\n" |
928 | " g();\n" |
929 | "else {\n" |
930 | " g();\n" |
931 | "}", |
932 | AllowsMergedIf); |
933 | verifyFormat("MYIF (a)\n" |
934 | " g();\n" |
935 | "else if (b)\n" |
936 | " g();\n" |
937 | "else {\n" |
938 | " g();\n" |
939 | "}", |
940 | AllowsMergedIf); |
941 | verifyFormat("MYIF (a)\n" |
942 | " g();\n" |
943 | "else MYIF (b) {\n" |
944 | " g();\n" |
945 | "} else {\n" |
946 | " g();\n" |
947 | "}", |
948 | AllowsMergedIf); |
949 | verifyFormat("MYIF (a)\n" |
950 | " g();\n" |
951 | "else if (b) {\n" |
952 | " g();\n" |
953 | "} else {\n" |
954 | " g();\n" |
955 | "}", |
956 | AllowsMergedIf); |
957 | verifyFormat("MYIF (a) {\n" |
958 | " g();\n" |
959 | "} else MYIF (b) {\n" |
960 | " g();\n" |
961 | "} else {\n" |
962 | " g();\n" |
963 | "}", |
964 | AllowsMergedIf); |
965 | verifyFormat("MYIF (a) {\n" |
966 | " g();\n" |
967 | "} else if (b) {\n" |
968 | " g();\n" |
969 | "} else {\n" |
970 | " g();\n" |
971 | "}", |
972 | AllowsMergedIf); |
973 | |
974 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = |
975 | FormatStyle::SIS_OnlyFirstIf; |
976 | |
977 | verifyFormat("if (a) f();\n" |
978 | "else {\n" |
979 | " g();\n" |
980 | "}", |
981 | AllowsMergedIf); |
982 | verifyFormat("if (a) f();\n" |
983 | "else {\n" |
984 | " if (a) f();\n" |
985 | " else {\n" |
986 | " g();\n" |
987 | " }\n" |
988 | " g();\n" |
989 | "}", |
990 | AllowsMergedIf); |
991 | |
992 | verifyFormat("if (a) g();", AllowsMergedIf); |
993 | verifyFormat("if (a) {\n" |
994 | " g()\n" |
995 | "};", |
996 | AllowsMergedIf); |
997 | verifyFormat("if (a) g();\n" |
998 | "else\n" |
999 | " g();", |
1000 | AllowsMergedIf); |
1001 | verifyFormat("if (a) {\n" |
1002 | " g();\n" |
1003 | "} else\n" |
1004 | " g();", |
1005 | AllowsMergedIf); |
1006 | verifyFormat("if (a) g();\n" |
1007 | "else {\n" |
1008 | " g();\n" |
1009 | "}", |
1010 | AllowsMergedIf); |
1011 | verifyFormat("if (a) {\n" |
1012 | " g();\n" |
1013 | "} else {\n" |
1014 | " g();\n" |
1015 | "}", |
1016 | AllowsMergedIf); |
1017 | verifyFormat("if (a) g();\n" |
1018 | "else if (b)\n" |
1019 | " g();\n" |
1020 | "else\n" |
1021 | " g();", |
1022 | AllowsMergedIf); |
1023 | verifyFormat("if (a) {\n" |
1024 | " g();\n" |
1025 | "} else if (b)\n" |
1026 | " g();\n" |
1027 | "else\n" |
1028 | " g();", |
1029 | AllowsMergedIf); |
1030 | verifyFormat("if (a) g();\n" |
1031 | "else if (b) {\n" |
1032 | " g();\n" |
1033 | "} else\n" |
1034 | " g();", |
1035 | AllowsMergedIf); |
1036 | verifyFormat("if (a) g();\n" |
1037 | "else if (b)\n" |
1038 | " g();\n" |
1039 | "else {\n" |
1040 | " g();\n" |
1041 | "}", |
1042 | AllowsMergedIf); |
1043 | verifyFormat("if (a) g();\n" |
1044 | "else if (b) {\n" |
1045 | " g();\n" |
1046 | "} else {\n" |
1047 | " g();\n" |
1048 | "}", |
1049 | AllowsMergedIf); |
1050 | verifyFormat("if (a) {\n" |
1051 | " g();\n" |
1052 | "} else if (b) {\n" |
1053 | " g();\n" |
1054 | "} else {\n" |
1055 | " g();\n" |
1056 | "}", |
1057 | AllowsMergedIf); |
1058 | verifyFormat("MYIF (a) f();\n" |
1059 | "else {\n" |
1060 | " g();\n" |
1061 | "}", |
1062 | AllowsMergedIf); |
1063 | verifyFormat("MYIF (a) f();\n" |
1064 | "else {\n" |
1065 | " if (a) f();\n" |
1066 | " else {\n" |
1067 | " g();\n" |
1068 | " }\n" |
1069 | " g();\n" |
1070 | "}", |
1071 | AllowsMergedIf); |
1072 | |
1073 | verifyFormat("MYIF (a) g();", AllowsMergedIf); |
1074 | verifyFormat("MYIF (a) {\n" |
1075 | " g()\n" |
1076 | "};", |
1077 | AllowsMergedIf); |
1078 | verifyFormat("MYIF (a) g();\n" |
1079 | "else\n" |
1080 | " g();", |
1081 | AllowsMergedIf); |
1082 | verifyFormat("MYIF (a) {\n" |
1083 | " g();\n" |
1084 | "} else\n" |
1085 | " g();", |
1086 | AllowsMergedIf); |
1087 | verifyFormat("MYIF (a) g();\n" |
1088 | "else {\n" |
1089 | " g();\n" |
1090 | "}", |
1091 | AllowsMergedIf); |
1092 | verifyFormat("MYIF (a) {\n" |
1093 | " g();\n" |
1094 | "} else {\n" |
1095 | " g();\n" |
1096 | "}", |
1097 | AllowsMergedIf); |
1098 | verifyFormat("MYIF (a) g();\n" |
1099 | "else MYIF (b)\n" |
1100 | " g();\n" |
1101 | "else\n" |
1102 | " g();", |
1103 | AllowsMergedIf); |
1104 | verifyFormat("MYIF (a) g();\n" |
1105 | "else if (b)\n" |
1106 | " g();\n" |
1107 | "else\n" |
1108 | " g();", |
1109 | AllowsMergedIf); |
1110 | verifyFormat("MYIF (a) {\n" |
1111 | " g();\n" |
1112 | "} else MYIF (b)\n" |
1113 | " g();\n" |
1114 | "else\n" |
1115 | " g();", |
1116 | AllowsMergedIf); |
1117 | verifyFormat("MYIF (a) {\n" |
1118 | " g();\n" |
1119 | "} else if (b)\n" |
1120 | " g();\n" |
1121 | "else\n" |
1122 | " g();", |
1123 | AllowsMergedIf); |
1124 | verifyFormat("MYIF (a) g();\n" |
1125 | "else MYIF (b) {\n" |
1126 | " g();\n" |
1127 | "} else\n" |
1128 | " g();", |
1129 | AllowsMergedIf); |
1130 | verifyFormat("MYIF (a) g();\n" |
1131 | "else if (b) {\n" |
1132 | " g();\n" |
1133 | "} else\n" |
1134 | " g();", |
1135 | AllowsMergedIf); |
1136 | verifyFormat("MYIF (a) g();\n" |
1137 | "else MYIF (b)\n" |
1138 | " g();\n" |
1139 | "else {\n" |
1140 | " g();\n" |
1141 | "}", |
1142 | AllowsMergedIf); |
1143 | verifyFormat("MYIF (a) g();\n" |
1144 | "else if (b)\n" |
1145 | " g();\n" |
1146 | "else {\n" |
1147 | " g();\n" |
1148 | "}", |
1149 | AllowsMergedIf); |
1150 | verifyFormat("MYIF (a) g();\n" |
1151 | "else MYIF (b) {\n" |
1152 | " g();\n" |
1153 | "} else {\n" |
1154 | " g();\n" |
1155 | "}", |
1156 | AllowsMergedIf); |
1157 | verifyFormat("MYIF (a) g();\n" |
1158 | "else if (b) {\n" |
1159 | " g();\n" |
1160 | "} else {\n" |
1161 | " g();\n" |
1162 | "}", |
1163 | AllowsMergedIf); |
1164 | verifyFormat("MYIF (a) {\n" |
1165 | " g();\n" |
1166 | "} else MYIF (b) {\n" |
1167 | " g();\n" |
1168 | "} else {\n" |
1169 | " g();\n" |
1170 | "}", |
1171 | AllowsMergedIf); |
1172 | verifyFormat("MYIF (a) {\n" |
1173 | " g();\n" |
1174 | "} else if (b) {\n" |
1175 | " g();\n" |
1176 | "} else {\n" |
1177 | " g();\n" |
1178 | "}", |
1179 | AllowsMergedIf); |
1180 | |
1181 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = |
1182 | FormatStyle::SIS_AllIfsAndElse; |
1183 | |
1184 | verifyFormat("if (a) f();\n" |
1185 | "else {\n" |
1186 | " g();\n" |
1187 | "}", |
1188 | AllowsMergedIf); |
1189 | verifyFormat("if (a) f();\n" |
1190 | "else {\n" |
1191 | " if (a) f();\n" |
1192 | " else {\n" |
1193 | " g();\n" |
1194 | " }\n" |
1195 | " g();\n" |
1196 | "}", |
1197 | AllowsMergedIf); |
1198 | |
1199 | verifyFormat("if (a) g();", AllowsMergedIf); |
1200 | verifyFormat("if (a) {\n" |
1201 | " g()\n" |
1202 | "};", |
1203 | AllowsMergedIf); |
1204 | verifyFormat("if (a) g();\n" |
1205 | "else g();", |
1206 | AllowsMergedIf); |
1207 | verifyFormat("if (a) {\n" |
1208 | " g();\n" |
1209 | "} else g();", |
1210 | AllowsMergedIf); |
1211 | verifyFormat("if (a) g();\n" |
1212 | "else {\n" |
1213 | " g();\n" |
1214 | "}", |
1215 | AllowsMergedIf); |
1216 | verifyFormat("if (a) {\n" |
1217 | " g();\n" |
1218 | "} else {\n" |
1219 | " g();\n" |
1220 | "}", |
1221 | AllowsMergedIf); |
1222 | verifyFormat("if (a) g();\n" |
1223 | "else if (b) g();\n" |
1224 | "else g();", |
1225 | AllowsMergedIf); |
1226 | verifyFormat("if (a) {\n" |
1227 | " g();\n" |
1228 | "} else if (b) g();\n" |
1229 | "else g();", |
1230 | AllowsMergedIf); |
1231 | verifyFormat("if (a) g();\n" |
1232 | "else if (b) {\n" |
1233 | " g();\n" |
1234 | "} else g();", |
1235 | AllowsMergedIf); |
1236 | verifyFormat("if (a) g();\n" |
1237 | "else if (b) g();\n" |
1238 | "else {\n" |
1239 | " g();\n" |
1240 | "}", |
1241 | AllowsMergedIf); |
1242 | verifyFormat("if (a) g();\n" |
1243 | "else if (b) {\n" |
1244 | " g();\n" |
1245 | "} else {\n" |
1246 | " g();\n" |
1247 | "}", |
1248 | AllowsMergedIf); |
1249 | verifyFormat("if (a) {\n" |
1250 | " g();\n" |
1251 | "} else if (b) {\n" |
1252 | " g();\n" |
1253 | "} else {\n" |
1254 | " g();\n" |
1255 | "}", |
1256 | AllowsMergedIf); |
1257 | verifyFormat("MYIF (a) f();\n" |
1258 | "else {\n" |
1259 | " g();\n" |
1260 | "}", |
1261 | AllowsMergedIf); |
1262 | verifyFormat("MYIF (a) f();\n" |
1263 | "else {\n" |
1264 | " if (a) f();\n" |
1265 | " else {\n" |
1266 | " g();\n" |
1267 | " }\n" |
1268 | " g();\n" |
1269 | "}", |
1270 | AllowsMergedIf); |
1271 | |
1272 | verifyFormat("MYIF (a) g();", AllowsMergedIf); |
1273 | verifyFormat("MYIF (a) {\n" |
1274 | " g()\n" |
1275 | "};", |
1276 | AllowsMergedIf); |
1277 | verifyFormat("MYIF (a) g();\n" |
1278 | "else g();", |
1279 | AllowsMergedIf); |
1280 | verifyFormat("MYIF (a) {\n" |
1281 | " g();\n" |
1282 | "} else g();", |
1283 | AllowsMergedIf); |
1284 | verifyFormat("MYIF (a) g();\n" |
1285 | "else {\n" |
1286 | " g();\n" |
1287 | "}", |
1288 | AllowsMergedIf); |
1289 | verifyFormat("MYIF (a) {\n" |
1290 | " g();\n" |
1291 | "} else {\n" |
1292 | " g();\n" |
1293 | "}", |
1294 | AllowsMergedIf); |
1295 | verifyFormat("MYIF (a) g();\n" |
1296 | "else MYIF (b) g();\n" |
1297 | "else g();", |
1298 | AllowsMergedIf); |
1299 | verifyFormat("MYIF (a) g();\n" |
1300 | "else if (b) g();\n" |
1301 | "else g();", |
1302 | AllowsMergedIf); |
1303 | verifyFormat("MYIF (a) {\n" |
1304 | " g();\n" |
1305 | "} else MYIF (b) g();\n" |
1306 | "else g();", |
1307 | AllowsMergedIf); |
1308 | verifyFormat("MYIF (a) {\n" |
1309 | " g();\n" |
1310 | "} else if (b) g();\n" |
1311 | "else g();", |
1312 | AllowsMergedIf); |
1313 | verifyFormat("MYIF (a) g();\n" |
1314 | "else MYIF (b) {\n" |
1315 | " g();\n" |
1316 | "} else g();", |
1317 | AllowsMergedIf); |
1318 | verifyFormat("MYIF (a) g();\n" |
1319 | "else if (b) {\n" |
1320 | " g();\n" |
1321 | "} else g();", |
1322 | AllowsMergedIf); |
1323 | verifyFormat("MYIF (a) g();\n" |
1324 | "else MYIF (b) g();\n" |
1325 | "else {\n" |
1326 | " g();\n" |
1327 | "}", |
1328 | AllowsMergedIf); |
1329 | verifyFormat("MYIF (a) g();\n" |
1330 | "else if (b) g();\n" |
1331 | "else {\n" |
1332 | " g();\n" |
1333 | "}", |
1334 | AllowsMergedIf); |
1335 | verifyFormat("MYIF (a) g();\n" |
1336 | "else MYIF (b) {\n" |
1337 | " g();\n" |
1338 | "} else {\n" |
1339 | " g();\n" |
1340 | "}", |
1341 | AllowsMergedIf); |
1342 | verifyFormat("MYIF (a) g();\n" |
1343 | "else if (b) {\n" |
1344 | " g();\n" |
1345 | "} else {\n" |
1346 | " g();\n" |
1347 | "}", |
1348 | AllowsMergedIf); |
1349 | verifyFormat("MYIF (a) {\n" |
1350 | " g();\n" |
1351 | "} else MYIF (b) {\n" |
1352 | " g();\n" |
1353 | "} else {\n" |
1354 | " g();\n" |
1355 | "}", |
1356 | AllowsMergedIf); |
1357 | verifyFormat("MYIF (a) {\n" |
1358 | " g();\n" |
1359 | "} else if (b) {\n" |
1360 | " g();\n" |
1361 | "} else {\n" |
1362 | " g();\n" |
1363 | "}", |
1364 | AllowsMergedIf); |
1365 | } |
1366 | |
1367 | TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { |
1368 | verifyFormat("while (true)\n" |
1369 | " ;"); |
1370 | verifyFormat("for (;;)\n" |
1371 | " ;"); |
1372 | |
1373 | FormatStyle AllowsMergedLoops = getLLVMStyle(); |
1374 | AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; |
1375 | |
1376 | verifyFormat("while (true) continue;", AllowsMergedLoops); |
1377 | verifyFormat("for (;;) continue;", AllowsMergedLoops); |
1378 | verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); |
1379 | verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); |
1380 | verifyFormat("while (true);", AllowsMergedLoops); |
1381 | verifyFormat("for (;;);", AllowsMergedLoops); |
1382 | verifyFormat("for (;;)\n" |
1383 | " for (;;) continue;", |
1384 | AllowsMergedLoops); |
1385 | verifyFormat("for (;;)\n" |
1386 | " while (true) continue;", |
1387 | AllowsMergedLoops); |
1388 | verifyFormat("while (true)\n" |
1389 | " for (;;) continue;", |
1390 | AllowsMergedLoops); |
1391 | verifyFormat("BOOST_FOREACH (int &v, vec)\n" |
1392 | " for (;;) continue;", |
1393 | AllowsMergedLoops); |
1394 | verifyFormat("for (;;)\n" |
1395 | " BOOST_FOREACH (int &v, vec) continue;", |
1396 | AllowsMergedLoops); |
1397 | verifyFormat("for (;;) // Can't merge this\n" |
1398 | " continue;", |
1399 | AllowsMergedLoops); |
1400 | verifyFormat("for (;;) /* still don't merge */\n" |
1401 | " continue;", |
1402 | AllowsMergedLoops); |
1403 | verifyFormat("do a++;\n" |
1404 | "while (true);", |
1405 | AllowsMergedLoops); |
1406 | verifyFormat("do /* Don't merge */\n" |
1407 | " a++;\n" |
1408 | "while (true);", |
1409 | AllowsMergedLoops); |
1410 | verifyFormat("do // Don't merge\n" |
1411 | " a++;\n" |
1412 | "while (true);", |
1413 | AllowsMergedLoops); |
1414 | verifyFormat("do\n" |
1415 | " // Don't merge\n" |
1416 | " a++;\n" |
1417 | "while (true);", |
1418 | AllowsMergedLoops); |
1419 | |
1420 | // Without braces labels are interpreted differently. |
1421 | verifyFormat("{\n" |
1422 | " do\n" |
1423 | " label:\n" |
1424 | " a++;\n" |
1425 | " while (true);\n" |
1426 | "}", |
1427 | AllowsMergedLoops); |
1428 | |
1429 | // Don't merge if there are comments before the null statement. |
1430 | verifyFormat("while (1) //\n" |
1431 | " ;", |
1432 | AllowsMergedLoops); |
1433 | verifyFormat("for (;;) /**/\n" |
1434 | " ;", |
1435 | AllowsMergedLoops); |
1436 | verifyFormat("while (true) /**/\n" |
1437 | " ;", |
1438 | "while (true) /**/;", AllowsMergedLoops); |
1439 | } |
1440 | |
1441 | TEST_F(FormatTest, FormatShortBracedStatements) { |
1442 | FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); |
1443 | EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false); |
1444 | EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine, |
1445 | FormatStyle::SIS_Never); |
1446 | EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false); |
1447 | EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false); |
1448 | verifyFormat("for (;;) {\n" |
1449 | " f();\n" |
1450 | "}"); |
1451 | verifyFormat("/*comment*/ for (;;) {\n" |
1452 | " f();\n" |
1453 | "}"); |
1454 | verifyFormat("BOOST_FOREACH (int v, vec) {\n" |
1455 | " f();\n" |
1456 | "}"); |
1457 | verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n" |
1458 | " f();\n" |
1459 | "}"); |
1460 | verifyFormat("while (true) {\n" |
1461 | " f();\n" |
1462 | "}"); |
1463 | verifyFormat("/*comment*/ while (true) {\n" |
1464 | " f();\n" |
1465 | "}"); |
1466 | verifyFormat("if (true) {\n" |
1467 | " f();\n" |
1468 | "}"); |
1469 | verifyFormat("/*comment*/ if (true) {\n" |
1470 | " f();\n" |
1471 | "}"); |
1472 | |
1473 | AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = |
1474 | FormatStyle::SBS_Empty; |
1475 | AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = |
1476 | FormatStyle::SIS_WithoutElse; |
1477 | verifyFormat("if (true) {}", AllowSimpleBracedStatements); |
1478 | verifyFormat("if (i) break;", AllowSimpleBracedStatements); |
1479 | verifyFormat("if (i > 0) {\n" |
1480 | " return i;\n" |
1481 | "}", |
1482 | AllowSimpleBracedStatements); |
1483 | |
1484 | AllowSimpleBracedStatements.IfMacros.push_back(x: "MYIF"); |
1485 | // Where line-lengths matter, a 2-letter synonym that maintains line length. |
1486 | // Not IF to avoid any confusion that IF is somehow special. |
1487 | AllowSimpleBracedStatements.IfMacros.push_back(x: "FI"); |
1488 | AllowSimpleBracedStatements.ColumnLimit = 40; |
1489 | AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = |
1490 | FormatStyle::SBS_Always; |
1491 | AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; |
1492 | AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; |
1493 | AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; |
1494 | AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; |
1495 | |
1496 | verifyFormat("if (true) {}", AllowSimpleBracedStatements); |
1497 | verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); |
1498 | verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); |
1499 | verifyFormat("if consteval {}", AllowSimpleBracedStatements); |
1500 | verifyFormat("if !consteval {}", AllowSimpleBracedStatements); |
1501 | verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements); |
1502 | verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); |
1503 | verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); |
1504 | verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); |
1505 | verifyFormat("while (true) {}", AllowSimpleBracedStatements); |
1506 | verifyFormat("for (;;) {}", AllowSimpleBracedStatements); |
1507 | verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); |
1508 | verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); |
1509 | verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); |
1510 | verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements); |
1511 | verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements); |
1512 | verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); |
1513 | verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); |
1514 | verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); |
1515 | verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements); |
1516 | verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements); |
1517 | verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); |
1518 | verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); |
1519 | verifyFormat("if (true) { fffffffffffffffffffffff(); }", |
1520 | AllowSimpleBracedStatements); |
1521 | verifyFormat("if (true) {\n" |
1522 | " ffffffffffffffffffffffff();\n" |
1523 | "}", |
1524 | AllowSimpleBracedStatements); |
1525 | verifyFormat("if (true) {\n" |
1526 | " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" |
1527 | "}", |
1528 | AllowSimpleBracedStatements); |
1529 | verifyFormat("if (true) { //\n" |
1530 | " f();\n" |
1531 | "}", |
1532 | AllowSimpleBracedStatements); |
1533 | verifyFormat("if (true) {\n" |
1534 | " f();\n" |
1535 | " f();\n" |
1536 | "}", |
1537 | AllowSimpleBracedStatements); |
1538 | verifyFormat("if (true) {\n" |
1539 | " f();\n" |
1540 | "} else {\n" |
1541 | " f();\n" |
1542 | "}", |
1543 | AllowSimpleBracedStatements); |
1544 | verifyFormat("FI (true) { fffffffffffffffffffffff(); }", |
1545 | AllowSimpleBracedStatements); |
1546 | verifyFormat("MYIF (true) {\n" |
1547 | " ffffffffffffffffffffffff();\n" |
1548 | "}", |
1549 | AllowSimpleBracedStatements); |
1550 | verifyFormat("MYIF (true) {\n" |
1551 | " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" |
1552 | "}", |
1553 | AllowSimpleBracedStatements); |
1554 | verifyFormat("MYIF (true) { //\n" |
1555 | " f();\n" |
1556 | "}", |
1557 | AllowSimpleBracedStatements); |
1558 | verifyFormat("MYIF (true) {\n" |
1559 | " f();\n" |
1560 | " f();\n" |
1561 | "}", |
1562 | AllowSimpleBracedStatements); |
1563 | verifyFormat("MYIF (true) {\n" |
1564 | " f();\n" |
1565 | "} else {\n" |
1566 | " f();\n" |
1567 | "}", |
1568 | AllowSimpleBracedStatements); |
1569 | |
1570 | verifyFormat("struct A2 {\n" |
1571 | " int X;\n" |
1572 | "};", |
1573 | AllowSimpleBracedStatements); |
1574 | verifyFormat("typedef struct A2 {\n" |
1575 | " int X;\n" |
1576 | "} A2_t;", |
1577 | AllowSimpleBracedStatements); |
1578 | verifyFormat("template <int> struct A2 {\n" |
1579 | " struct B {};\n" |
1580 | "};", |
1581 | AllowSimpleBracedStatements); |
1582 | |
1583 | AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = |
1584 | FormatStyle::SIS_Never; |
1585 | verifyFormat("if (true) {}", AllowSimpleBracedStatements); |
1586 | verifyFormat("if (true) {\n" |
1587 | " f();\n" |
1588 | "}", |
1589 | AllowSimpleBracedStatements); |
1590 | verifyFormat("if (true) {\n" |
1591 | " f();\n" |
1592 | "} else {\n" |
1593 | " f();\n" |
1594 | "}", |
1595 | AllowSimpleBracedStatements); |
1596 | verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); |
1597 | verifyFormat("MYIF (true) {\n" |
1598 | " f();\n" |
1599 | "}", |
1600 | AllowSimpleBracedStatements); |
1601 | verifyFormat("MYIF (true) {\n" |
1602 | " f();\n" |
1603 | "} else {\n" |
1604 | " f();\n" |
1605 | "}", |
1606 | AllowSimpleBracedStatements); |
1607 | |
1608 | AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; |
1609 | verifyFormat("while (true) {}", AllowSimpleBracedStatements); |
1610 | verifyFormat("while (true) {\n" |
1611 | " f();\n" |
1612 | "}", |
1613 | AllowSimpleBracedStatements); |
1614 | verifyFormat("for (;;) {}", AllowSimpleBracedStatements); |
1615 | verifyFormat("for (;;) {\n" |
1616 | " f();\n" |
1617 | "}", |
1618 | AllowSimpleBracedStatements); |
1619 | verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); |
1620 | verifyFormat("BOOST_FOREACH (int v, vec) {\n" |
1621 | " f();\n" |
1622 | "}", |
1623 | AllowSimpleBracedStatements); |
1624 | |
1625 | AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = |
1626 | FormatStyle::SIS_WithoutElse; |
1627 | AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; |
1628 | AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = |
1629 | FormatStyle::BWACS_Always; |
1630 | |
1631 | verifyFormat("if (true) {}", AllowSimpleBracedStatements); |
1632 | verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); |
1633 | verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); |
1634 | verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); |
1635 | verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); |
1636 | verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); |
1637 | verifyFormat("while (true) {}", AllowSimpleBracedStatements); |
1638 | verifyFormat("for (;;) {}", AllowSimpleBracedStatements); |
1639 | verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); |
1640 | verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); |
1641 | verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); |
1642 | verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); |
1643 | verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); |
1644 | verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); |
1645 | verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); |
1646 | verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); |
1647 | verifyFormat("if (true) { fffffffffffffffffffffff(); }", |
1648 | AllowSimpleBracedStatements); |
1649 | verifyFormat("if (true)\n" |
1650 | "{\n" |
1651 | " ffffffffffffffffffffffff();\n" |
1652 | "}", |
1653 | AllowSimpleBracedStatements); |
1654 | verifyFormat("if (true)\n" |
1655 | "{\n" |
1656 | " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" |
1657 | "}", |
1658 | AllowSimpleBracedStatements); |
1659 | verifyFormat("if (true)\n" |
1660 | "{ //\n" |
1661 | " f();\n" |
1662 | "}", |
1663 | AllowSimpleBracedStatements); |
1664 | verifyFormat("if (true)\n" |
1665 | "{\n" |
1666 | " f();\n" |
1667 | " f();\n" |
1668 | "}", |
1669 | AllowSimpleBracedStatements); |
1670 | verifyFormat("if (true)\n" |
1671 | "{\n" |
1672 | " f();\n" |
1673 | "} else\n" |
1674 | "{\n" |
1675 | " f();\n" |
1676 | "}", |
1677 | AllowSimpleBracedStatements); |
1678 | verifyFormat("FI (true) { fffffffffffffffffffffff(); }", |
1679 | AllowSimpleBracedStatements); |
1680 | verifyFormat("MYIF (true)\n" |
1681 | "{\n" |
1682 | " ffffffffffffffffffffffff();\n" |
1683 | "}", |
1684 | AllowSimpleBracedStatements); |
1685 | verifyFormat("MYIF (true)\n" |
1686 | "{\n" |
1687 | " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" |
1688 | "}", |
1689 | AllowSimpleBracedStatements); |
1690 | verifyFormat("MYIF (true)\n" |
1691 | "{ //\n" |
1692 | " f();\n" |
1693 | "}", |
1694 | AllowSimpleBracedStatements); |
1695 | verifyFormat("MYIF (true)\n" |
1696 | "{\n" |
1697 | " f();\n" |
1698 | " f();\n" |
1699 | "}", |
1700 | AllowSimpleBracedStatements); |
1701 | verifyFormat("MYIF (true)\n" |
1702 | "{\n" |
1703 | " f();\n" |
1704 | "} else\n" |
1705 | "{\n" |
1706 | " f();\n" |
1707 | "}", |
1708 | AllowSimpleBracedStatements); |
1709 | |
1710 | AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = |
1711 | FormatStyle::SIS_Never; |
1712 | verifyFormat("if (true) {}", AllowSimpleBracedStatements); |
1713 | verifyFormat("if (true)\n" |
1714 | "{\n" |
1715 | " f();\n" |
1716 | "}", |
1717 | AllowSimpleBracedStatements); |
1718 | verifyFormat("if (true)\n" |
1719 | "{\n" |
1720 | " f();\n" |
1721 | "} else\n" |
1722 | "{\n" |
1723 | " f();\n" |
1724 | "}", |
1725 | AllowSimpleBracedStatements); |
1726 | verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); |
1727 | verifyFormat("MYIF (true)\n" |
1728 | "{\n" |
1729 | " f();\n" |
1730 | "}", |
1731 | AllowSimpleBracedStatements); |
1732 | verifyFormat("MYIF (true)\n" |
1733 | "{\n" |
1734 | " f();\n" |
1735 | "} else\n" |
1736 | "{\n" |
1737 | " f();\n" |
1738 | "}", |
1739 | AllowSimpleBracedStatements); |
1740 | |
1741 | AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; |
1742 | verifyFormat("while (true) {}", AllowSimpleBracedStatements); |
1743 | verifyFormat("while (true)\n" |
1744 | "{\n" |
1745 | " f();\n" |
1746 | "}", |
1747 | AllowSimpleBracedStatements); |
1748 | verifyFormat("for (;;) {}", AllowSimpleBracedStatements); |
1749 | verifyFormat("for (;;)\n" |
1750 | "{\n" |
1751 | " f();\n" |
1752 | "}", |
1753 | AllowSimpleBracedStatements); |
1754 | verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); |
1755 | verifyFormat("BOOST_FOREACH (int v, vec)\n" |
1756 | "{\n" |
1757 | " f();\n" |
1758 | "}", |
1759 | AllowSimpleBracedStatements); |
1760 | |
1761 | FormatStyle Style = getLLVMStyle(); |
1762 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
1763 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
1764 | |
1765 | verifyFormat("while (i > 0)\n" |
1766 | "{\n" |
1767 | " --i;\n" |
1768 | "}", |
1769 | Style); |
1770 | |
1771 | verifyFormat("if (a)\n" |
1772 | "{\n" |
1773 | " ++b;\n" |
1774 | "}", |
1775 | Style); |
1776 | |
1777 | verifyFormat("if (a)\n" |
1778 | "{\n" |
1779 | " b = 1;\n" |
1780 | "} else\n" |
1781 | "{\n" |
1782 | " b = 0;\n" |
1783 | "}", |
1784 | Style); |
1785 | |
1786 | verifyFormat("if (a)\n" |
1787 | "{\n" |
1788 | " b = 1;\n" |
1789 | "} else if (c)\n" |
1790 | "{\n" |
1791 | " b = 2;\n" |
1792 | "} else\n" |
1793 | "{\n" |
1794 | " b = 0;\n" |
1795 | "}", |
1796 | Style); |
1797 | |
1798 | Style.BraceWrapping.BeforeElse = true; |
1799 | |
1800 | verifyFormat("if (a)\n" |
1801 | "{\n" |
1802 | " b = 1;\n" |
1803 | "}\n" |
1804 | "else\n" |
1805 | "{\n" |
1806 | " b = 0;\n" |
1807 | "}", |
1808 | Style); |
1809 | |
1810 | verifyFormat("if (a)\n" |
1811 | "{\n" |
1812 | " b = 1;\n" |
1813 | "}\n" |
1814 | "else if (c)\n" |
1815 | "{\n" |
1816 | " b = 2;\n" |
1817 | "}\n" |
1818 | "else\n" |
1819 | "{\n" |
1820 | " b = 0;\n" |
1821 | "}", |
1822 | Style); |
1823 | } |
1824 | |
1825 | TEST_F(FormatTest, UnderstandsMacros) { |
1826 | verifyFormat("#define A (parentheses)"); |
1827 | verifyFormat("/* comment */ #define A (parentheses)"); |
1828 | verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); |
1829 | // Even the partial code should never be merged. |
1830 | verifyNoChange("/* comment */ #define A (parentheses)\n" |
1831 | "#"); |
1832 | verifyFormat("/* comment */ #define A (parentheses)\n" |
1833 | "#\n"); |
1834 | verifyFormat("/* comment */ #define A (parentheses)\n" |
1835 | "#define B (parentheses)"); |
1836 | verifyFormat("#define true ((int)1)"); |
1837 | verifyFormat("#define and(x)"); |
1838 | verifyFormat("#define if(x) x"); |
1839 | verifyFormat("#define return(x) (x)"); |
1840 | verifyFormat("#define while(x) for (; x;)"); |
1841 | verifyFormat("#define xor(x) (^(x))"); |
1842 | verifyFormat("#define __except(x)"); |
1843 | verifyFormat("#define __try(x)"); |
1844 | |
1845 | // https://llvm.org/PR54348. |
1846 | verifyFormat( |
1847 | "#define A" |
1848 | " " |
1849 | "\\\n" |
1850 | " class & {}"); |
1851 | |
1852 | FormatStyle Style = getLLVMStyle(); |
1853 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
1854 | Style.BraceWrapping.AfterFunction = true; |
1855 | // Test that a macro definition never gets merged with the following |
1856 | // definition. |
1857 | // FIXME: The AAA macro definition probably should not be split into 3 lines. |
1858 | verifyFormat("#define AAA " |
1859 | " \\\n" |
1860 | " N " |
1861 | " \\\n" |
1862 | " {\n" |
1863 | "#define BBB }", |
1864 | Style); |
1865 | // verifyFormat("#define AAA N { //", Style); |
1866 | |
1867 | verifyFormat("MACRO(return)"); |
1868 | verifyFormat("MACRO(co_await)"); |
1869 | verifyFormat("MACRO(co_return)"); |
1870 | verifyFormat("MACRO(co_yield)"); |
1871 | verifyFormat("MACRO(return, something)"); |
1872 | verifyFormat("MACRO(co_return, something)"); |
1873 | verifyFormat("MACRO(something##something)"); |
1874 | verifyFormat("MACRO(return##something)"); |
1875 | verifyFormat("MACRO(co_return##something)"); |
1876 | |
1877 | verifyFormat("#define A x:"); |
1878 | |
1879 | verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n" |
1880 | " { \\\n" |
1881 | " #Bar \\\n" |
1882 | " }"); |
1883 | verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n" |
1884 | " { #Bar }"); |
1885 | } |
1886 | |
1887 | TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { |
1888 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
1889 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
1890 | Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; |
1891 | Style.BreakBeforeBraces = FormatStyle::BS_Allman; |
1892 | verifyFormat("#define A \\\n" |
1893 | " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" |
1894 | " { \\\n" |
1895 | " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" |
1896 | " }\n" |
1897 | "X;", |
1898 | "#define A \\\n" |
1899 | " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" |
1900 | " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" |
1901 | " }\n" |
1902 | "X;", |
1903 | Style); |
1904 | } |
1905 | |
1906 | TEST_F(FormatTest, ParseIfElse) { |
1907 | verifyFormat("if (true)\n" |
1908 | " if (true)\n" |
1909 | " if (true)\n" |
1910 | " f();\n" |
1911 | " else\n" |
1912 | " g();\n" |
1913 | " else\n" |
1914 | " h();\n" |
1915 | "else\n" |
1916 | " i();"); |
1917 | verifyFormat("if (true)\n" |
1918 | " if (true)\n" |
1919 | " if (true) {\n" |
1920 | " if (true)\n" |
1921 | " f();\n" |
1922 | " } else {\n" |
1923 | " g();\n" |
1924 | " }\n" |
1925 | " else\n" |
1926 | " h();\n" |
1927 | "else {\n" |
1928 | " i();\n" |
1929 | "}"); |
1930 | verifyFormat("if (true)\n" |
1931 | " if constexpr (true)\n" |
1932 | " if (true) {\n" |
1933 | " if constexpr (true)\n" |
1934 | " f();\n" |
1935 | " } else {\n" |
1936 | " g();\n" |
1937 | " }\n" |
1938 | " else\n" |
1939 | " h();\n" |
1940 | "else {\n" |
1941 | " i();\n" |
1942 | "}"); |
1943 | verifyFormat("if (true)\n" |
1944 | " if CONSTEXPR (true)\n" |
1945 | " if (true) {\n" |
1946 | " if CONSTEXPR (true)\n" |
1947 | " f();\n" |
1948 | " } else {\n" |
1949 | " g();\n" |
1950 | " }\n" |
1951 | " else\n" |
1952 | " h();\n" |
1953 | "else {\n" |
1954 | " i();\n" |
1955 | "}"); |
1956 | verifyFormat("void f() {\n" |
1957 | " if (a) {\n" |
1958 | " } else {\n" |
1959 | " }\n" |
1960 | "}"); |
1961 | } |
1962 | |
1963 | TEST_F(FormatTest, ElseIf) { |
1964 | verifyFormat("if (a) {\n} else if (b) {\n}"); |
1965 | verifyFormat("if (a)\n" |
1966 | " f();\n" |
1967 | "else if (b)\n" |
1968 | " g();\n" |
1969 | "else\n" |
1970 | " h();"); |
1971 | verifyFormat("if (a)\n" |
1972 | " f();\n" |
1973 | "else // comment\n" |
1974 | " if (b) {\n" |
1975 | " g();\n" |
1976 | " h();\n" |
1977 | " }"); |
1978 | verifyFormat("if constexpr (a)\n" |
1979 | " f();\n" |
1980 | "else if constexpr (b)\n" |
1981 | " g();\n" |
1982 | "else\n" |
1983 | " h();"); |
1984 | verifyFormat("if CONSTEXPR (a)\n" |
1985 | " f();\n" |
1986 | "else if CONSTEXPR (b)\n" |
1987 | " g();\n" |
1988 | "else\n" |
1989 | " h();"); |
1990 | verifyFormat("if (a) {\n" |
1991 | " f();\n" |
1992 | "}\n" |
1993 | "// or else ..\n" |
1994 | "else {\n" |
1995 | " g()\n" |
1996 | "}"); |
1997 | |
1998 | verifyFormat("if (a) {\n" |
1999 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
2000 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
2001 | "}"); |
2002 | verifyFormat("if (a) {\n" |
2003 | "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
2004 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
2005 | "}"); |
2006 | verifyFormat("if (a) {\n" |
2007 | "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
2008 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
2009 | "}"); |
2010 | verifyFormat("if (a) {\n" |
2011 | "} else if (\n" |
2012 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
2013 | "}", |
2014 | getLLVMStyleWithColumns(62)); |
2015 | verifyFormat("if (a) {\n" |
2016 | "} else if constexpr (\n" |
2017 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
2018 | "}", |
2019 | getLLVMStyleWithColumns(62)); |
2020 | verifyFormat("if (a) {\n" |
2021 | "} else if CONSTEXPR (\n" |
2022 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
2023 | "}", |
2024 | getLLVMStyleWithColumns(62)); |
2025 | } |
2026 | |
2027 | TEST_F(FormatTest, SeparatePointerReferenceAlignment) { |
2028 | FormatStyle Style = getLLVMStyle(); |
2029 | EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); |
2030 | EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer); |
2031 | verifyFormat("int *f1(int *a, int &b, int &&c);", Style); |
2032 | verifyFormat("int &f2(int &&c, int *a, int &b);", Style); |
2033 | verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); |
2034 | verifyFormat("int *f1(int &a) const &;", Style); |
2035 | verifyFormat("int *f1(int &a) const & = 0;", Style); |
2036 | verifyFormat("int *a = f1();", Style); |
2037 | verifyFormat("int &b = f2();", Style); |
2038 | verifyFormat("int &&c = f3();", Style); |
2039 | verifyFormat("int f3() { return sizeof(Foo &); }", Style); |
2040 | verifyFormat("int f4() { return sizeof(Foo &&); }", Style); |
2041 | verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); |
2042 | verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); |
2043 | verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); |
2044 | verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); |
2045 | verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); |
2046 | verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); |
2047 | verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); |
2048 | verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); |
2049 | verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); |
2050 | verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); |
2051 | verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); |
2052 | verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); |
2053 | verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); |
2054 | verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); |
2055 | verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); |
2056 | verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); |
2057 | verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); |
2058 | verifyFormat("for (f(); int &c : {1, 2, 3})", Style); |
2059 | verifyFormat( |
2060 | "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n" |
2061 | " res2 = [](int &a) { return 0000000000000; };", |
2062 | Style); |
2063 | |
2064 | Style.AlignConsecutiveDeclarations.Enabled = true; |
2065 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; |
2066 | verifyFormat("Const unsigned int *c;\n" |
2067 | "const unsigned int *d;\n" |
2068 | "Const unsigned int &e;\n" |
2069 | "const unsigned int &f;\n" |
2070 | "int *f1(int *a, int &b, int &&c);\n" |
2071 | "double *(*f2)(int *a, double &&b);\n" |
2072 | "const unsigned &&g;\n" |
2073 | "Const unsigned h;", |
2074 | Style); |
2075 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; |
2076 | verifyFormat("Const unsigned int *c;\n" |
2077 | "const unsigned int *d;\n" |
2078 | "Const unsigned int &e;\n" |
2079 | "const unsigned int &f;\n" |
2080 | "int *f1(int *a, int &b, int &&c);\n" |
2081 | "double *(*f2)(int *a, double &&b);\n" |
2082 | "const unsigned &&g;\n" |
2083 | "Const unsigned h;", |
2084 | Style); |
2085 | |
2086 | Style.PointerAlignment = FormatStyle::PAS_Left; |
2087 | verifyFormat("int* f1(int* a, int& b, int&& c);", Style); |
2088 | verifyFormat("int& f2(int&& c, int* a, int& b);", Style); |
2089 | verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); |
2090 | verifyFormat("int* f1(int& a) const& = 0;", Style); |
2091 | verifyFormat("int* a = f1();", Style); |
2092 | verifyFormat("int& b = f2();", Style); |
2093 | verifyFormat("int&& c = f3();", Style); |
2094 | verifyFormat("int f3() { return sizeof(Foo&); }", Style); |
2095 | verifyFormat("int f4() { return sizeof(Foo&&); }", Style); |
2096 | verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style); |
2097 | verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style); |
2098 | verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); |
2099 | verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); |
2100 | verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); |
2101 | verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); |
2102 | verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); |
2103 | verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); |
2104 | verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); |
2105 | verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); |
2106 | verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); |
2107 | verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); |
2108 | verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); |
2109 | verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); |
2110 | verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); |
2111 | verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); |
2112 | verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); |
2113 | verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); |
2114 | verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); |
2115 | verifyFormat("for (f(); int& c : {1, 2, 3})", Style); |
2116 | verifyFormat( |
2117 | "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n" |
2118 | " res2 = [](int& a) { return 0000000000000; };", |
2119 | Style); |
2120 | verifyFormat("[](decltype(foo)& Bar) {}", Style); |
2121 | |
2122 | Style.AlignConsecutiveDeclarations.Enabled = true; |
2123 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; |
2124 | verifyFormat("Const unsigned int* c;\n" |
2125 | "const unsigned int* d;\n" |
2126 | "Const unsigned int& e;\n" |
2127 | "const unsigned int& f;\n" |
2128 | "int* f1(int* a, int& b, int&& c);\n" |
2129 | "double* (*f2)(int* a, double&& b);\n" |
2130 | "const unsigned&& g;\n" |
2131 | "Const unsigned h;", |
2132 | Style); |
2133 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; |
2134 | verifyFormat("Const unsigned int* c;\n" |
2135 | "const unsigned int* d;\n" |
2136 | "Const unsigned int& e;\n" |
2137 | "const unsigned int& f;\n" |
2138 | "int* f1(int* a, int& b, int&& c);\n" |
2139 | "double* (*f2)(int* a, double&& b);\n" |
2140 | "const unsigned&& g;\n" |
2141 | "Const unsigned h;", |
2142 | Style); |
2143 | |
2144 | Style.PointerAlignment = FormatStyle::PAS_Right; |
2145 | Style.ReferenceAlignment = FormatStyle::RAS_Left; |
2146 | verifyFormat("int *f1(int *a, int& b, int&& c);", Style); |
2147 | verifyFormat("int& f2(int&& c, int *a, int& b);", Style); |
2148 | verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); |
2149 | verifyFormat("int *a = f1();", Style); |
2150 | verifyFormat("int& b = f2();", Style); |
2151 | verifyFormat("int&& c = f3();", Style); |
2152 | verifyFormat("int f3() { return sizeof(Foo&); }", Style); |
2153 | verifyFormat("int f4() { return sizeof(Foo&&); }", Style); |
2154 | verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style); |
2155 | verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style); |
2156 | verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); |
2157 | verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); |
2158 | verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); |
2159 | |
2160 | Style.AlignConsecutiveDeclarations.Enabled = true; |
2161 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; |
2162 | verifyFormat("Const unsigned int *c;\n" |
2163 | "const unsigned int *d;\n" |
2164 | "Const unsigned int& e;\n" |
2165 | "const unsigned int& f;\n" |
2166 | "int *f1(int *a, int& b, int&& c);\n" |
2167 | "double *(*f2)(int *a, double&& b);\n" |
2168 | "const unsigned&& g;\n" |
2169 | "Const unsigned h;", |
2170 | Style); |
2171 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; |
2172 | verifyFormat("Const unsigned int *c;\n" |
2173 | "const unsigned int *d;\n" |
2174 | "Const unsigned int& e;\n" |
2175 | "const unsigned int& f;\n" |
2176 | "int *f1(int *a, int& b, int&& c);\n" |
2177 | "double *(*f2)(int *a, double&& b);\n" |
2178 | "const unsigned&& g;\n" |
2179 | "Const unsigned h;", |
2180 | Style); |
2181 | |
2182 | Style.PointerAlignment = FormatStyle::PAS_Left; |
2183 | Style.ReferenceAlignment = FormatStyle::RAS_Middle; |
2184 | verifyFormat("int* f1(int* a, int & b, int && c);", Style); |
2185 | verifyFormat("int & f2(int && c, int* a, int & b);", Style); |
2186 | verifyFormat("int && f3(int & b, int && c, int* a);", Style); |
2187 | verifyFormat("int* a = f1();", Style); |
2188 | verifyFormat("int & b = f2();", Style); |
2189 | verifyFormat("int && c = f3();", Style); |
2190 | verifyFormat("int f3() { return sizeof(Foo &); }", Style); |
2191 | verifyFormat("int f4() { return sizeof(Foo &&); }", Style); |
2192 | verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); |
2193 | verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); |
2194 | verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); |
2195 | verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); |
2196 | verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); |
2197 | verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); |
2198 | verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); |
2199 | verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); |
2200 | verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); |
2201 | verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); |
2202 | verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); |
2203 | verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); |
2204 | verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); |
2205 | verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); |
2206 | verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); |
2207 | verifyFormat("for (f(); int & c : {1, 2, 3})", Style); |
2208 | verifyFormat( |
2209 | "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n" |
2210 | " res2 = [](int & a) { return 0000000000000; };", |
2211 | Style); |
2212 | |
2213 | Style.AlignConsecutiveDeclarations.Enabled = true; |
2214 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; |
2215 | verifyFormat("Const unsigned int* c;\n" |
2216 | "const unsigned int* d;\n" |
2217 | "Const unsigned int & e;\n" |
2218 | "const unsigned int & f;\n" |
2219 | "int* f1(int* a, int & b, int && c);\n" |
2220 | "double* (*f2)(int* a, double && b);\n" |
2221 | "const unsigned && g;\n" |
2222 | "Const unsigned h;", |
2223 | Style); |
2224 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; |
2225 | verifyFormat("Const unsigned int* c;\n" |
2226 | "const unsigned int* d;\n" |
2227 | "Const unsigned int & e;\n" |
2228 | "const unsigned int & f;\n" |
2229 | "int* f1(int* a, int & b, int && c);\n" |
2230 | "double* (*f2)(int* a, double && b);\n" |
2231 | "const unsigned && g;\n" |
2232 | "Const unsigned h;", |
2233 | Style); |
2234 | |
2235 | Style.PointerAlignment = FormatStyle::PAS_Middle; |
2236 | Style.ReferenceAlignment = FormatStyle::RAS_Right; |
2237 | verifyFormat("int * f1(int * a, int &b, int &&c);", Style); |
2238 | verifyFormat("int &f2(int &&c, int * a, int &b);", Style); |
2239 | verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); |
2240 | verifyFormat("int * a = f1();", Style); |
2241 | verifyFormat("int &b = f2();", Style); |
2242 | verifyFormat("int &&c = f3();", Style); |
2243 | verifyFormat("int f3() { return sizeof(Foo &); }", Style); |
2244 | verifyFormat("int f4() { return sizeof(Foo &&); }", Style); |
2245 | verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); |
2246 | verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); |
2247 | verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); |
2248 | verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); |
2249 | verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); |
2250 | |
2251 | Style.AlignConsecutiveDeclarations.Enabled = true; |
2252 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; |
2253 | verifyFormat("Const unsigned int * c;\n" |
2254 | "const unsigned int * d;\n" |
2255 | "Const unsigned int &e;\n" |
2256 | "const unsigned int &f;\n" |
2257 | "int * f1(int * a, int &b, int &&c);\n" |
2258 | "double * (*f2)(int * a, double &&b);\n" |
2259 | "const unsigned &&g;\n" |
2260 | "Const unsigned h;", |
2261 | Style); |
2262 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; |
2263 | verifyFormat("Const unsigned int * c;\n" |
2264 | "const unsigned int * d;\n" |
2265 | "Const unsigned int &e;\n" |
2266 | "const unsigned int &f;\n" |
2267 | "int * f1(int * a, int &b, int &&c);\n" |
2268 | "double * (*f2)(int * a, double &&b);\n" |
2269 | "const unsigned &&g;\n" |
2270 | "Const unsigned h;", |
2271 | Style); |
2272 | |
2273 | // FIXME: we don't handle this yet, so output may be arbitrary until it's |
2274 | // specifically handled |
2275 | // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); |
2276 | } |
2277 | |
2278 | TEST_F(FormatTest, FormatsForLoop) { |
2279 | verifyFormat( |
2280 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
2281 | " ++VeryVeryLongLoopVariable)\n" |
2282 | " ;"); |
2283 | verifyFormat("for (;;)\n" |
2284 | " f();"); |
2285 | verifyFormat("for (;;) {\n}"); |
2286 | verifyFormat("for (;;) {\n" |
2287 | " f();\n" |
2288 | "}"); |
2289 | verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); |
2290 | |
2291 | verifyFormat( |
2292 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
2293 | " E = UnwrappedLines.end();\n" |
2294 | " I != E; ++I) {\n}"); |
2295 | |
2296 | verifyFormat( |
2297 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
2298 | " ++IIIII) {\n}"); |
2299 | verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" |
2300 | " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" |
2301 | " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); |
2302 | verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" |
2303 | " I = FD->getDeclsInPrototypeScope().begin(),\n" |
2304 | " E = FD->getDeclsInPrototypeScope().end();\n" |
2305 | " I != E; ++I) {\n}"); |
2306 | verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" |
2307 | " I = Container.begin(),\n" |
2308 | " E = Container.end();\n" |
2309 | " I != E; ++I) {\n}", |
2310 | getLLVMStyleWithColumns(76)); |
2311 | |
2312 | verifyFormat( |
2313 | "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" |
2314 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" |
2315 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
2316 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
2317 | " ++aaaaaaaaaaa) {\n}"); |
2318 | verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
2319 | " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" |
2320 | " ++i) {\n}"); |
2321 | verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" |
2322 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
2323 | "}"); |
2324 | verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" |
2325 | " aaaaaaaaaa);\n" |
2326 | " iter; ++iter) {\n" |
2327 | "}"); |
2328 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
2329 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
2330 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" |
2331 | " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); |
2332 | |
2333 | // These should not be formatted as Objective-C for-in loops. |
2334 | verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); |
2335 | verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); |
2336 | verifyFormat("Foo *x;\nfor (x in y) {\n}"); |
2337 | verifyFormat( |
2338 | "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); |
2339 | |
2340 | FormatStyle NoBinPacking = getLLVMStyle(); |
2341 | NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
2342 | verifyFormat("for (int aaaaaaaaaaa = 1;\n" |
2343 | " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" |
2344 | " aaaaaaaaaaaaaaaa,\n" |
2345 | " aaaaaaaaaaaaaaaa,\n" |
2346 | " aaaaaaaaaaaaaaaa);\n" |
2347 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
2348 | "}", |
2349 | NoBinPacking); |
2350 | verifyFormat( |
2351 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
2352 | " E = UnwrappedLines.end();\n" |
2353 | " I != E;\n" |
2354 | " ++I) {\n}", |
2355 | NoBinPacking); |
2356 | |
2357 | FormatStyle AlignLeft = getLLVMStyle(); |
2358 | AlignLeft.PointerAlignment = FormatStyle::PAS_Left; |
2359 | verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); |
2360 | } |
2361 | |
2362 | TEST_F(FormatTest, RangeBasedForLoops) { |
2363 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
2364 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
2365 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" |
2366 | " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); |
2367 | verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" |
2368 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
2369 | verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" |
2370 | " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); |
2371 | } |
2372 | |
2373 | TEST_F(FormatTest, ForEachLoops) { |
2374 | FormatStyle Style = getLLVMStyle(); |
2375 | EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); |
2376 | EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); |
2377 | verifyFormat("void f() {\n" |
2378 | " for (;;) {\n" |
2379 | " }\n" |
2380 | " foreach (Item *item, itemlist) {\n" |
2381 | " }\n" |
2382 | " Q_FOREACH (Item *item, itemlist) {\n" |
2383 | " }\n" |
2384 | " BOOST_FOREACH (Item *item, itemlist) {\n" |
2385 | " }\n" |
2386 | " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" |
2387 | "}", |
2388 | Style); |
2389 | verifyFormat("void f() {\n" |
2390 | " for (;;)\n" |
2391 | " int j = 1;\n" |
2392 | " Q_FOREACH (int v, vec)\n" |
2393 | " v *= 2;\n" |
2394 | " for (;;) {\n" |
2395 | " int j = 1;\n" |
2396 | " }\n" |
2397 | " Q_FOREACH (int v, vec) {\n" |
2398 | " v *= 2;\n" |
2399 | " }\n" |
2400 | "}", |
2401 | Style); |
2402 | |
2403 | FormatStyle ShortBlocks = getLLVMStyle(); |
2404 | ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
2405 | EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); |
2406 | verifyFormat("void f() {\n" |
2407 | " for (;;)\n" |
2408 | " int j = 1;\n" |
2409 | " Q_FOREACH (int &v, vec)\n" |
2410 | " v *= 2;\n" |
2411 | " for (;;) {\n" |
2412 | " int j = 1;\n" |
2413 | " }\n" |
2414 | " Q_FOREACH (int &v, vec) {\n" |
2415 | " int j = 1;\n" |
2416 | " }\n" |
2417 | "}", |
2418 | ShortBlocks); |
2419 | |
2420 | FormatStyle ShortLoops = getLLVMStyle(); |
2421 | ShortLoops.AllowShortLoopsOnASingleLine = true; |
2422 | EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); |
2423 | verifyFormat("void f() {\n" |
2424 | " for (;;) int j = 1;\n" |
2425 | " Q_FOREACH (int &v, vec) int j = 1;\n" |
2426 | " for (;;) {\n" |
2427 | " int j = 1;\n" |
2428 | " }\n" |
2429 | " Q_FOREACH (int &v, vec) {\n" |
2430 | " int j = 1;\n" |
2431 | " }\n" |
2432 | "}", |
2433 | ShortLoops); |
2434 | |
2435 | FormatStyle ShortBlocksAndLoops = getLLVMStyle(); |
2436 | ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
2437 | ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; |
2438 | verifyFormat("void f() {\n" |
2439 | " for (;;) int j = 1;\n" |
2440 | " Q_FOREACH (int &v, vec) int j = 1;\n" |
2441 | " for (;;) { int j = 1; }\n" |
2442 | " Q_FOREACH (int &v, vec) { int j = 1; }\n" |
2443 | "}", |
2444 | ShortBlocksAndLoops); |
2445 | |
2446 | Style.SpaceBeforeParens = |
2447 | FormatStyle::SBPO_ControlStatementsExceptControlMacros; |
2448 | verifyFormat("void f() {\n" |
2449 | " for (;;) {\n" |
2450 | " }\n" |
2451 | " foreach(Item *item, itemlist) {\n" |
2452 | " }\n" |
2453 | " Q_FOREACH(Item *item, itemlist) {\n" |
2454 | " }\n" |
2455 | " BOOST_FOREACH(Item *item, itemlist) {\n" |
2456 | " }\n" |
2457 | " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" |
2458 | "}", |
2459 | Style); |
2460 | |
2461 | // As function-like macros. |
2462 | verifyFormat("#define foreach(x, y)\n" |
2463 | "#define Q_FOREACH(x, y)\n" |
2464 | "#define BOOST_FOREACH(x, y)\n" |
2465 | "#define UNKNOWN_FOREACH(x, y)"); |
2466 | |
2467 | // Not as function-like macros. |
2468 | verifyFormat("#define foreach (x, y)\n" |
2469 | "#define Q_FOREACH (x, y)\n" |
2470 | "#define BOOST_FOREACH (x, y)\n" |
2471 | "#define UNKNOWN_FOREACH (x, y)"); |
2472 | |
2473 | // handle microsoft non standard extension |
2474 | verifyFormat("for each (char c in x->MyStringProperty)"); |
2475 | } |
2476 | |
2477 | TEST_F(FormatTest, FormatsWhileLoop) { |
2478 | verifyFormat("while (true) {\n}"); |
2479 | verifyFormat("while (true)\n" |
2480 | " f();"); |
2481 | verifyFormat("while () {\n}"); |
2482 | verifyFormat("while () {\n" |
2483 | " f();\n" |
2484 | "}"); |
2485 | } |
2486 | |
2487 | TEST_F(FormatTest, FormatsDoWhile) { |
2488 | verifyFormat("do {\n" |
2489 | " do_something();\n" |
2490 | "} while (something());"); |
2491 | verifyFormat("do\n" |
2492 | " do_something();\n" |
2493 | "while (something());"); |
2494 | } |
2495 | |
2496 | TEST_F(FormatTest, FormatsSwitchStatement) { |
2497 | verifyFormat("switch (x) {\n" |
2498 | "case 1:\n" |
2499 | " f();\n" |
2500 | " break;\n" |
2501 | "case kFoo:\n" |
2502 | "case ns::kBar:\n" |
2503 | "case kBaz:\n" |
2504 | " break;\n" |
2505 | "default:\n" |
2506 | " g();\n" |
2507 | " break;\n" |
2508 | "}"); |
2509 | verifyFormat("switch (x) {\n" |
2510 | "case 1: {\n" |
2511 | " f();\n" |
2512 | " break;\n" |
2513 | "}\n" |
2514 | "case 2: {\n" |
2515 | " break;\n" |
2516 | "}\n" |
2517 | "}"); |
2518 | verifyFormat("switch (x) {\n" |
2519 | "case 1: {\n" |
2520 | " f();\n" |
2521 | " {\n" |
2522 | " g();\n" |
2523 | " h();\n" |
2524 | " }\n" |
2525 | " break;\n" |
2526 | "}\n" |
2527 | "}"); |
2528 | verifyFormat("switch (x) {\n" |
2529 | "case 1: {\n" |
2530 | " f();\n" |
2531 | " if (foo) {\n" |
2532 | " g();\n" |
2533 | " h();\n" |
2534 | " }\n" |
2535 | " break;\n" |
2536 | "}\n" |
2537 | "}"); |
2538 | verifyFormat("switch (x) {\n" |
2539 | "case 1: {\n" |
2540 | " f();\n" |
2541 | " g();\n" |
2542 | "} break;\n" |
2543 | "}"); |
2544 | verifyFormat("switch (test)\n" |
2545 | " ;"); |
2546 | verifyFormat("switch (x) {\n" |
2547 | "default: {\n" |
2548 | " // Do nothing.\n" |
2549 | "}\n" |
2550 | "}"); |
2551 | verifyFormat("switch (x) {\n" |
2552 | "// comment\n" |
2553 | "// if 1, do f()\n" |
2554 | "case 1:\n" |
2555 | " f();\n" |
2556 | "}"); |
2557 | verifyFormat("switch (x) {\n" |
2558 | "case 1:\n" |
2559 | " // Do amazing stuff\n" |
2560 | " {\n" |
2561 | " f();\n" |
2562 | " g();\n" |
2563 | " }\n" |
2564 | " break;\n" |
2565 | "}"); |
2566 | verifyFormat("#define A \\\n" |
2567 | " switch (x) { \\\n" |
2568 | " case a: \\\n" |
2569 | " foo = b; \\\n" |
2570 | " }", |
2571 | getLLVMStyleWithColumns(20)); |
2572 | verifyFormat("#define OPERATION_CASE(name) \\\n" |
2573 | " case OP_name: \\\n" |
2574 | " return operations::Operation##name", |
2575 | getLLVMStyleWithColumns(40)); |
2576 | verifyFormat("switch (x) {\n" |
2577 | "case 1:;\n" |
2578 | "default:;\n" |
2579 | " int i;\n" |
2580 | "}"); |
2581 | |
2582 | verifyGoogleFormat("switch (x) {\n" |
2583 | " case 1:\n" |
2584 | " f();\n" |
2585 | " break;\n" |
2586 | " case kFoo:\n" |
2587 | " case ns::kBar:\n" |
2588 | " case kBaz:\n" |
2589 | " break;\n" |
2590 | " default:\n" |
2591 | " g();\n" |
2592 | " break;\n" |
2593 | "}"); |
2594 | verifyGoogleFormat("switch (x) {\n" |
2595 | " case 1: {\n" |
2596 | " f();\n" |
2597 | " break;\n" |
2598 | " }\n" |
2599 | "}"); |
2600 | verifyGoogleFormat("switch (test)\n" |
2601 | " ;"); |
2602 | |
2603 | verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" |
2604 | " case OP_name: \\\n" |
2605 | " return operations::Operation##name"); |
2606 | verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" |
2607 | " // Get the correction operation class.\n" |
2608 | " switch (OpCode) {\n" |
2609 | " CASE(Add);\n" |
2610 | " CASE(Subtract);\n" |
2611 | " default:\n" |
2612 | " return operations::Unknown;\n" |
2613 | " }\n" |
2614 | "#undef OPERATION_CASE\n" |
2615 | "}"); |
2616 | verifyFormat("DEBUG({\n" |
2617 | " switch (x) {\n" |
2618 | " case A:\n" |
2619 | " f();\n" |
2620 | " break;\n" |
2621 | " // fallthrough\n" |
2622 | " case B:\n" |
2623 | " g();\n" |
2624 | " break;\n" |
2625 | " }\n" |
2626 | "});"); |
2627 | verifyNoChange("DEBUG({\n" |
2628 | " switch (x) {\n" |
2629 | " case A:\n" |
2630 | " f();\n" |
2631 | " break;\n" |
2632 | " // On B:\n" |
2633 | " case B:\n" |
2634 | " g();\n" |
2635 | " break;\n" |
2636 | " }\n" |
2637 | "});"); |
2638 | verifyFormat("switch (n) {\n" |
2639 | "case 0: {\n" |
2640 | " return false;\n" |
2641 | "}\n" |
2642 | "default: {\n" |
2643 | " return true;\n" |
2644 | "}\n" |
2645 | "}", |
2646 | "switch (n)\n" |
2647 | "{\n" |
2648 | "case 0: {\n" |
2649 | " return false;\n" |
2650 | "}\n" |
2651 | "default: {\n" |
2652 | " return true;\n" |
2653 | "}\n" |
2654 | "}"); |
2655 | verifyFormat("switch (a) {\n" |
2656 | "case (b):\n" |
2657 | " return;\n" |
2658 | "}"); |
2659 | |
2660 | verifyFormat("switch (a) {\n" |
2661 | "case some_namespace::\n" |
2662 | " some_constant:\n" |
2663 | " return;\n" |
2664 | "}", |
2665 | getLLVMStyleWithColumns(34)); |
2666 | |
2667 | verifyFormat("switch (a) {\n" |
2668 | "[[likely]] case 1:\n" |
2669 | " return;\n" |
2670 | "}"); |
2671 | verifyFormat("switch (a) {\n" |
2672 | "[[likely]] [[other::likely]] case 1:\n" |
2673 | " return;\n" |
2674 | "}"); |
2675 | verifyFormat("switch (x) {\n" |
2676 | "case 1:\n" |
2677 | " return;\n" |
2678 | "[[likely]] case 2:\n" |
2679 | " return;\n" |
2680 | "}"); |
2681 | verifyFormat("switch (a) {\n" |
2682 | "case 1:\n" |
2683 | "[[likely]] case 2:\n" |
2684 | " return;\n" |
2685 | "}"); |
2686 | FormatStyle Attributes = getLLVMStyle(); |
2687 | Attributes.AttributeMacros.push_back(x: "LIKELY"); |
2688 | Attributes.AttributeMacros.push_back(x: "OTHER_LIKELY"); |
2689 | verifyFormat("switch (a) {\n" |
2690 | "LIKELY case b:\n" |
2691 | " return;\n" |
2692 | "}", |
2693 | Attributes); |
2694 | verifyFormat("switch (a) {\n" |
2695 | "LIKELY OTHER_LIKELY() case b:\n" |
2696 | " return;\n" |
2697 | "}", |
2698 | Attributes); |
2699 | verifyFormat("switch (a) {\n" |
2700 | "case 1:\n" |
2701 | " return;\n" |
2702 | "LIKELY case 2:\n" |
2703 | " return;\n" |
2704 | "}", |
2705 | Attributes); |
2706 | verifyFormat("switch (a) {\n" |
2707 | "case 1:\n" |
2708 | "LIKELY case 2:\n" |
2709 | " return;\n" |
2710 | "}", |
2711 | Attributes); |
2712 | |
2713 | FormatStyle Style = getLLVMStyle(); |
2714 | Style.IndentCaseLabels = true; |
2715 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; |
2716 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
2717 | Style.BraceWrapping.AfterCaseLabel = true; |
2718 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
2719 | verifyFormat("switch (n)\n" |
2720 | "{\n" |
2721 | " case 0:\n" |
2722 | " {\n" |
2723 | " return false;\n" |
2724 | " }\n" |
2725 | " default:\n" |
2726 | " {\n" |
2727 | " return true;\n" |
2728 | " }\n" |
2729 | "}", |
2730 | "switch (n) {\n" |
2731 | " case 0: {\n" |
2732 | " return false;\n" |
2733 | " }\n" |
2734 | " default: {\n" |
2735 | " return true;\n" |
2736 | " }\n" |
2737 | "}", |
2738 | Style); |
2739 | Style.BraceWrapping.AfterCaseLabel = false; |
2740 | verifyFormat("switch (n)\n" |
2741 | "{\n" |
2742 | " case 0: {\n" |
2743 | " return false;\n" |
2744 | " }\n" |
2745 | " default: {\n" |
2746 | " return true;\n" |
2747 | " }\n" |
2748 | "}", |
2749 | "switch (n) {\n" |
2750 | " case 0:\n" |
2751 | " {\n" |
2752 | " return false;\n" |
2753 | " }\n" |
2754 | " default:\n" |
2755 | " {\n" |
2756 | " return true;\n" |
2757 | " }\n" |
2758 | "}", |
2759 | Style); |
2760 | Style.IndentCaseLabels = false; |
2761 | Style.IndentCaseBlocks = true; |
2762 | verifyFormat("switch (n)\n" |
2763 | "{\n" |
2764 | "case 0:\n" |
2765 | " {\n" |
2766 | " return false;\n" |
2767 | " }\n" |
2768 | "case 1:\n" |
2769 | " break;\n" |
2770 | "default:\n" |
2771 | " {\n" |
2772 | " return true;\n" |
2773 | " }\n" |
2774 | "}", |
2775 | "switch (n) {\n" |
2776 | "case 0: {\n" |
2777 | " return false;\n" |
2778 | "}\n" |
2779 | "case 1:\n" |
2780 | " break;\n" |
2781 | "default: {\n" |
2782 | " return true;\n" |
2783 | "}\n" |
2784 | "}", |
2785 | Style); |
2786 | Style.IndentCaseLabels = true; |
2787 | Style.IndentCaseBlocks = true; |
2788 | verifyFormat("switch (n)\n" |
2789 | "{\n" |
2790 | " case 0:\n" |
2791 | " {\n" |
2792 | " return false;\n" |
2793 | " }\n" |
2794 | " case 1:\n" |
2795 | " break;\n" |
2796 | " default:\n" |
2797 | " {\n" |
2798 | " return true;\n" |
2799 | " }\n" |
2800 | "}", |
2801 | "switch (n) {\n" |
2802 | "case 0: {\n" |
2803 | " return false;\n" |
2804 | "}\n" |
2805 | "case 1:\n" |
2806 | " break;\n" |
2807 | "default: {\n" |
2808 | " return true;\n" |
2809 | "}\n" |
2810 | "}", |
2811 | Style); |
2812 | } |
2813 | |
2814 | TEST_F(FormatTest, CaseRanges) { |
2815 | verifyFormat("switch (x) {\n" |
2816 | "case 'A' ... 'Z':\n" |
2817 | "case 1 ... 5:\n" |
2818 | "case a ... b:\n" |
2819 | " break;\n" |
2820 | "}"); |
2821 | } |
2822 | |
2823 | TEST_F(FormatTest, ShortEnums) { |
2824 | FormatStyle Style = getLLVMStyle(); |
2825 | EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine); |
2826 | EXPECT_FALSE(Style.BraceWrapping.AfterEnum); |
2827 | verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); |
2828 | verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); |
2829 | Style.AllowShortEnumsOnASingleLine = false; |
2830 | verifyFormat("enum {\n" |
2831 | " A,\n" |
2832 | " B,\n" |
2833 | " C\n" |
2834 | "} ShortEnum1, ShortEnum2;", |
2835 | Style); |
2836 | verifyFormat("typedef enum {\n" |
2837 | " A,\n" |
2838 | " B,\n" |
2839 | " C\n" |
2840 | "} ShortEnum1, ShortEnum2;", |
2841 | Style); |
2842 | verifyFormat("enum {\n" |
2843 | " A,\n" |
2844 | "} ShortEnum1, ShortEnum2;", |
2845 | Style); |
2846 | verifyFormat("typedef enum {\n" |
2847 | " A,\n" |
2848 | "} ShortEnum1, ShortEnum2;", |
2849 | Style); |
2850 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
2851 | Style.BraceWrapping.AfterEnum = true; |
2852 | verifyFormat("enum\n" |
2853 | "{\n" |
2854 | " A,\n" |
2855 | " B,\n" |
2856 | " C\n" |
2857 | "} ShortEnum1, ShortEnum2;", |
2858 | Style); |
2859 | verifyFormat("typedef enum\n" |
2860 | "{\n" |
2861 | " A,\n" |
2862 | " B,\n" |
2863 | " C\n" |
2864 | "} ShortEnum1, ShortEnum2;", |
2865 | Style); |
2866 | } |
2867 | |
2868 | TEST_F(FormatTest, ShortCompoundRequirement) { |
2869 | constexpr StringRef Code("template <typename T>\n" |
2870 | "concept c = requires(T x) {\n" |
2871 | " { x + 1 } -> std::same_as<int>;\n" |
2872 | "};"); |
2873 | |
2874 | FormatStyle Style = getLLVMStyle(); |
2875 | EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine); |
2876 | verifyFormat(Code, Style); |
2877 | verifyFormat("template <typename T>\n" |
2878 | "concept c = requires(T x) {\n" |
2879 | " { x + 1 } -> std::same_as<int>;\n" |
2880 | " { x + 2 } -> std::same_as<int>;\n" |
2881 | "};", |
2882 | Style); |
2883 | |
2884 | Style.AllowShortCompoundRequirementOnASingleLine = false; |
2885 | verifyFormat("template <typename T>\n" |
2886 | "concept c = requires(T x) {\n" |
2887 | " {\n" |
2888 | " x + 1\n" |
2889 | " } -> std::same_as<int>;\n" |
2890 | "};", |
2891 | Code, Style); |
2892 | verifyFormat("template <typename T>\n" |
2893 | "concept c = requires(T x) {\n" |
2894 | " {\n" |
2895 | " x + 1\n" |
2896 | " } -> std::same_as<int>;\n" |
2897 | " {\n" |
2898 | " x + 2\n" |
2899 | " } -> std::same_as<int>;\n" |
2900 | "};", |
2901 | Style); |
2902 | |
2903 | Style.AllowShortCompoundRequirementOnASingleLine = true; |
2904 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
2905 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; |
2906 | verifyFormat(Code, Style); |
2907 | } |
2908 | |
2909 | TEST_F(FormatTest, ShortCaseLabels) { |
2910 | FormatStyle Style = getLLVMStyle(); |
2911 | Style.AllowShortCaseLabelsOnASingleLine = true; |
2912 | verifyFormat("switch (a) {\n" |
2913 | "case 1: x = 1; break;\n" |
2914 | "case 2: return;\n" |
2915 | "case 3:\n" |
2916 | "case 4:\n" |
2917 | "case 5: return;\n" |
2918 | "case 6: // comment\n" |
2919 | " return;\n" |
2920 | "case 7:\n" |
2921 | " // comment\n" |
2922 | " return;\n" |
2923 | "case 8:\n" |
2924 | " x = 8; // comment\n" |
2925 | " break;\n" |
2926 | "default: y = 1; break;\n" |
2927 | "}", |
2928 | Style); |
2929 | verifyFormat("switch (a) {\n" |
2930 | "case 0: return; // comment\n" |
2931 | "case 1: break; // comment\n" |
2932 | "case 2: return;\n" |
2933 | "// comment\n" |
2934 | "case 3: return;\n" |
2935 | "// comment 1\n" |
2936 | "// comment 2\n" |
2937 | "// comment 3\n" |
2938 | "case 4: break; /* comment */\n" |
2939 | "case 5:\n" |
2940 | " // comment\n" |
2941 | " break;\n" |
2942 | "case 6: /* comment */ x = 1; break;\n" |
2943 | "case 7: x = /* comment */ 1; break;\n" |
2944 | "case 8:\n" |
2945 | " x = 1; /* comment */\n" |
2946 | " break;\n" |
2947 | "case 9:\n" |
2948 | " break; // comment line 1\n" |
2949 | " // comment line 2\n" |
2950 | "}", |
2951 | Style); |
2952 | verifyFormat("switch (a) {\n" |
2953 | "case 1:\n" |
2954 | " x = 8;\n" |
2955 | " // fall through\n" |
2956 | "case 2: x = 8;\n" |
2957 | "// comment\n" |
2958 | "case 3:\n" |
2959 | " return; /* comment line 1\n" |
2960 | " * comment line 2 */\n" |
2961 | "case 4: i = 8;\n" |
2962 | "// something else\n" |
2963 | "#if FOO\n" |
2964 | "case 5: break;\n" |
2965 | "#endif\n" |
2966 | "}", |
2967 | "switch (a) {\n" |
2968 | "case 1: x = 8;\n" |
2969 | " // fall through\n" |
2970 | "case 2:\n" |
2971 | " x = 8;\n" |
2972 | "// comment\n" |
2973 | "case 3:\n" |
2974 | " return; /* comment line 1\n" |
2975 | " * comment line 2 */\n" |
2976 | "case 4:\n" |
2977 | " i = 8;\n" |
2978 | "// something else\n" |
2979 | "#if FOO\n" |
2980 | "case 5: break;\n" |
2981 | "#endif\n" |
2982 | "}", |
2983 | Style); |
2984 | verifyFormat("switch (a) {\n" |
2985 | "case 0:\n" |
2986 | " return; // long long long long long long long long long long " |
2987 | "long long comment\n" |
2988 | " // line\n" |
2989 | "}", |
2990 | "switch (a) {\n" |
2991 | "case 0: return; // long long long long long long long long " |
2992 | "long long long long comment line\n" |
2993 | "}", |
2994 | Style); |
2995 | verifyFormat("switch (a) {\n" |
2996 | "case 0:\n" |
2997 | " return; /* long long long long long long long long long long " |
2998 | "long long comment\n" |
2999 | " line */\n" |
3000 | "}", |
3001 | "switch (a) {\n" |
3002 | "case 0: return; /* long long long long long long long long " |
3003 | "long long long long comment line */\n" |
3004 | "}", |
3005 | Style); |
3006 | verifyFormat("switch (a) {\n" |
3007 | "#if FOO\n" |
3008 | "case 0: return 0;\n" |
3009 | "#endif\n" |
3010 | "}", |
3011 | Style); |
3012 | verifyFormat("switch (a) {\n" |
3013 | "case 1: {\n" |
3014 | "}\n" |
3015 | "case 2: {\n" |
3016 | " return;\n" |
3017 | "}\n" |
3018 | "case 3: {\n" |
3019 | " x = 1;\n" |
3020 | " return;\n" |
3021 | "}\n" |
3022 | "case 4:\n" |
3023 | " if (x)\n" |
3024 | " return;\n" |
3025 | "}", |
3026 | Style); |
3027 | Style.ColumnLimit = 21; |
3028 | verifyFormat("#define X \\\n" |
3029 | " case 0: break;\n" |
3030 | "#include \"f\"", |
3031 | Style); |
3032 | verifyFormat("switch (a) {\n" |
3033 | "case 1: x = 1; break;\n" |
3034 | "case 2: return;\n" |
3035 | "case 3:\n" |
3036 | "case 4:\n" |
3037 | "case 5: return;\n" |
3038 | "default:\n" |
3039 | " y = 1;\n" |
3040 | " break;\n" |
3041 | "}", |
3042 | Style); |
3043 | Style.ColumnLimit = 80; |
3044 | Style.AllowShortCaseLabelsOnASingleLine = false; |
3045 | Style.IndentCaseLabels = true; |
3046 | verifyFormat("switch (n) {\n" |
3047 | " default /*comments*/:\n" |
3048 | " return true;\n" |
3049 | " case 0:\n" |
3050 | " return false;\n" |
3051 | "}", |
3052 | "switch (n) {\n" |
3053 | "default/*comments*/:\n" |
3054 | " return true;\n" |
3055 | "case 0:\n" |
3056 | " return false;\n" |
3057 | "}", |
3058 | Style); |
3059 | Style.AllowShortCaseLabelsOnASingleLine = true; |
3060 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
3061 | Style.BraceWrapping.AfterCaseLabel = true; |
3062 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
3063 | verifyFormat("switch (n)\n" |
3064 | "{\n" |
3065 | " case 0:\n" |
3066 | " {\n" |
3067 | " return false;\n" |
3068 | " }\n" |
3069 | " default:\n" |
3070 | " {\n" |
3071 | " return true;\n" |
3072 | " }\n" |
3073 | "}", |
3074 | "switch (n) {\n" |
3075 | " case 0: {\n" |
3076 | " return false;\n" |
3077 | " }\n" |
3078 | " default:\n" |
3079 | " {\n" |
3080 | " return true;\n" |
3081 | " }\n" |
3082 | "}", |
3083 | Style); |
3084 | } |
3085 | |
3086 | TEST_F(FormatTest, FormatsLabels) { |
3087 | verifyFormat("void f() {\n" |
3088 | " some_code();\n" |
3089 | "test_label:\n" |
3090 | " some_other_code();\n" |
3091 | " {\n" |
3092 | " some_more_code();\n" |
3093 | " another_label:\n" |
3094 | " some_more_code();\n" |
3095 | " }\n" |
3096 | "}"); |
3097 | verifyFormat("{\n" |
3098 | " some_code();\n" |
3099 | "test_label:\n" |
3100 | " some_other_code();\n" |
3101 | "}"); |
3102 | verifyFormat("{\n" |
3103 | " some_code();\n" |
3104 | "test_label:;\n" |
3105 | " int i = 0;\n" |
3106 | "}"); |
3107 | verifyFormat("{\n" |
3108 | " some_code();\n" |
3109 | "test_label: { some_other_code(); }\n" |
3110 | "}"); |
3111 | verifyFormat("{\n" |
3112 | " some_code();\n" |
3113 | "test_label: {\n" |
3114 | " some_other_code();\n" |
3115 | " some_other_code();\n" |
3116 | "}\n" |
3117 | "}"); |
3118 | verifyFormat("{\n" |
3119 | "L0:\n" |
3120 | "[[foo]] L1:\n" |
3121 | "[[bar]] [[baz]] L2:\n" |
3122 | " g();\n" |
3123 | "}"); |
3124 | verifyFormat("{\n" |
3125 | "[[foo]] L1: {\n" |
3126 | "[[bar]] [[baz]] L2:\n" |
3127 | " g();\n" |
3128 | "}\n" |
3129 | "}"); |
3130 | verifyFormat("{\n" |
3131 | "[[foo]] L1:\n" |
3132 | " f();\n" |
3133 | " {\n" |
3134 | " [[bar]] [[baz]] L2:\n" |
3135 | " g();\n" |
3136 | " }\n" |
3137 | "}"); |
3138 | |
3139 | FormatStyle Style = getLLVMStyle(); |
3140 | Style.IndentGotoLabels = false; |
3141 | verifyFormat("void f() {\n" |
3142 | " some_code();\n" |
3143 | "test_label:\n" |
3144 | " some_other_code();\n" |
3145 | " {\n" |
3146 | " some_more_code();\n" |
3147 | "another_label:\n" |
3148 | " some_more_code();\n" |
3149 | " }\n" |
3150 | "}", |
3151 | Style); |
3152 | verifyFormat("{\n" |
3153 | " some_code();\n" |
3154 | "test_label:\n" |
3155 | " some_other_code();\n" |
3156 | "}", |
3157 | Style); |
3158 | verifyFormat("{\n" |
3159 | " some_code();\n" |
3160 | "test_label:;\n" |
3161 | " int i = 0;\n" |
3162 | "}", |
3163 | Style); |
3164 | verifyFormat("{\n" |
3165 | " some_code();\n" |
3166 | "test_label: { some_other_code(); }\n" |
3167 | "}", |
3168 | Style); |
3169 | verifyFormat("{\n" |
3170 | "[[foo]] L1:\n" |
3171 | " f();\n" |
3172 | " {\n" |
3173 | "[[bar]] [[baz]] L2:\n" |
3174 | " g();\n" |
3175 | " }\n" |
3176 | "}", |
3177 | Style); |
3178 | |
3179 | Style.ColumnLimit = 15; |
3180 | verifyFormat("#define FOO \\\n" |
3181 | "label: \\\n" |
3182 | " break;", |
3183 | Style); |
3184 | |
3185 | // The opening brace may either be on the same unwrapped line as the colon or |
3186 | // on a separate one. The formatter should recognize both. |
3187 | Style = getLLVMStyle(); |
3188 | Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman; |
3189 | verifyFormat("{\n" |
3190 | " some_code();\n" |
3191 | "test_label:\n" |
3192 | "{\n" |
3193 | " some_other_code();\n" |
3194 | "}\n" |
3195 | "}", |
3196 | Style); |
3197 | verifyFormat("{\n" |
3198 | "[[foo]] L1:\n" |
3199 | "{\n" |
3200 | "[[bar]] [[baz]] L2:\n" |
3201 | " g();\n" |
3202 | "}\n" |
3203 | "}", |
3204 | Style); |
3205 | } |
3206 | |
3207 | TEST_F(FormatTest, MultiLineControlStatements) { |
3208 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20); |
3209 | Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; |
3210 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; |
3211 | // Short lines should keep opening brace on same line. |
3212 | verifyFormat("if (foo) {\n" |
3213 | " bar();\n" |
3214 | "}", |
3215 | "if(foo){bar();}", Style); |
3216 | verifyFormat("if (foo) {\n" |
3217 | " bar();\n" |
3218 | "} else {\n" |
3219 | " baz();\n" |
3220 | "}", |
3221 | "if(foo){bar();}else{baz();}", Style); |
3222 | verifyFormat("if (foo && bar) {\n" |
3223 | " baz();\n" |
3224 | "}", |
3225 | "if(foo&&bar){baz();}", Style); |
3226 | verifyFormat("if (foo) {\n" |
3227 | " bar();\n" |
3228 | "} else if (baz) {\n" |
3229 | " quux();\n" |
3230 | "}", |
3231 | "if(foo){bar();}else if(baz){quux();}", Style); |
3232 | verifyFormat("if (foo) {\n" |
3233 | " bar();\n" |
3234 | "} else if (baz) {\n" |
3235 | " quux();\n" |
3236 | "} else {\n" |
3237 | " foobar();\n" |
3238 | "}", |
3239 | "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style); |
3240 | verifyFormat("for (;;) {\n" |
3241 | " foo();\n" |
3242 | "}", |
3243 | "for(;;){foo();}"); |
3244 | verifyFormat("while (1) {\n" |
3245 | " foo();\n" |
3246 | "}", |
3247 | "while(1){foo();}", Style); |
3248 | verifyFormat("switch (foo) {\n" |
3249 | "case bar:\n" |
3250 | " return;\n" |
3251 | "}", |
3252 | "switch(foo){case bar:return;}", Style); |
3253 | verifyFormat("try {\n" |
3254 | " foo();\n" |
3255 | "} catch (...) {\n" |
3256 | " bar();\n" |
3257 | "}", |
3258 | "try{foo();}catch(...){bar();}", Style); |
3259 | verifyFormat("do {\n" |
3260 | " foo();\n" |
3261 | "} while (bar &&\n" |
3262 | " baz);", |
3263 | "do{foo();}while(bar&&baz);", Style); |
3264 | // Long lines should put opening brace on new line. |
3265 | verifyFormat("void f() {\n" |
3266 | " if (a1 && a2 &&\n" |
3267 | " a3)\n" |
3268 | " {\n" |
3269 | " quux();\n" |
3270 | " }\n" |
3271 | "}", |
3272 | "void f(){if(a1&&a2&&a3){quux();}}", Style); |
3273 | verifyFormat("if (foo && bar &&\n" |
3274 | " baz)\n" |
3275 | "{\n" |
3276 | " quux();\n" |
3277 | "}", |
3278 | "if(foo&&bar&&baz){quux();}", Style); |
3279 | verifyFormat("if (foo && bar &&\n" |
3280 | " baz)\n" |
3281 | "{\n" |
3282 | " quux();\n" |
3283 | "}", |
3284 | "if (foo && bar &&\n" |
3285 | " baz) {\n" |
3286 | " quux();\n" |
3287 | "}", |
3288 | Style); |
3289 | verifyFormat("if (foo) {\n" |
3290 | " bar();\n" |
3291 | "} else if (baz ||\n" |
3292 | " quux)\n" |
3293 | "{\n" |
3294 | " foobar();\n" |
3295 | "}", |
3296 | "if(foo){bar();}else if(baz||quux){foobar();}", Style); |
3297 | verifyFormat("if (foo) {\n" |
3298 | " bar();\n" |
3299 | "} else if (baz ||\n" |
3300 | " quux)\n" |
3301 | "{\n" |
3302 | " foobar();\n" |
3303 | "} else {\n" |
3304 | " barbaz();\n" |
3305 | "}", |
3306 | "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", |
3307 | Style); |
3308 | verifyFormat("for (int i = 0;\n" |
3309 | " i < 10; ++i)\n" |
3310 | "{\n" |
3311 | " foo();\n" |
3312 | "}", |
3313 | "for(int i=0;i<10;++i){foo();}", Style); |
3314 | verifyFormat("foreach (int i,\n" |
3315 | " list)\n" |
3316 | "{\n" |
3317 | " foo();\n" |
3318 | "}", |
3319 | "foreach(int i, list){foo();}", Style); |
3320 | Style.ColumnLimit = |
3321 | 40; // to concentrate at brace wrapping, not line wrap due to column limit |
3322 | verifyFormat("foreach (int i, list) {\n" |
3323 | " foo();\n" |
3324 | "}", |
3325 | "foreach(int i, list){foo();}", Style); |
3326 | Style.ColumnLimit = |
3327 | 20; // to concentrate at brace wrapping, not line wrap due to column limit |
3328 | verifyFormat("while (foo || bar ||\n" |
3329 | " baz)\n" |
3330 | "{\n" |
3331 | " quux();\n" |
3332 | "}", |
3333 | "while(foo||bar||baz){quux();}", Style); |
3334 | verifyFormat("switch (\n" |
3335 | " foo = barbaz)\n" |
3336 | "{\n" |
3337 | "case quux:\n" |
3338 | " return;\n" |
3339 | "}", |
3340 | "switch(foo=barbaz){case quux:return;}", Style); |
3341 | verifyFormat("try {\n" |
3342 | " foo();\n" |
3343 | "} catch (\n" |
3344 | " Exception &bar)\n" |
3345 | "{\n" |
3346 | " baz();\n" |
3347 | "}", |
3348 | "try{foo();}catch(Exception&bar){baz();}", Style); |
3349 | Style.ColumnLimit = |
3350 | 40; // to concentrate at brace wrapping, not line wrap due to column limit |
3351 | verifyFormat("try {\n" |
3352 | " foo();\n" |
3353 | "} catch (Exception &bar) {\n" |
3354 | " baz();\n" |
3355 | "}", |
3356 | "try{foo();}catch(Exception&bar){baz();}", Style); |
3357 | Style.ColumnLimit = |
3358 | 20; // to concentrate at brace wrapping, not line wrap due to column limit |
3359 | |
3360 | Style.BraceWrapping.BeforeElse = true; |
3361 | verifyFormat("if (foo) {\n" |
3362 | " bar();\n" |
3363 | "}\n" |
3364 | "else if (baz ||\n" |
3365 | " quux)\n" |
3366 | "{\n" |
3367 | " foobar();\n" |
3368 | "}\n" |
3369 | "else {\n" |
3370 | " barbaz();\n" |
3371 | "}", |
3372 | "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", |
3373 | Style); |
3374 | |
3375 | Style.BraceWrapping.BeforeCatch = true; |
3376 | verifyFormat("try {\n" |
3377 | " foo();\n" |
3378 | "}\n" |
3379 | "catch (...) {\n" |
3380 | " baz();\n" |
3381 | "}", |
3382 | "try{foo();}catch(...){baz();}", Style); |
3383 | |
3384 | Style.BraceWrapping.AfterFunction = true; |
3385 | Style.BraceWrapping.AfterStruct = false; |
3386 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; |
3387 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
3388 | Style.ColumnLimit = 80; |
3389 | verifyFormat("void shortfunction() { bar(); }", Style); |
3390 | verifyFormat("struct T shortfunction() { return bar(); }", Style); |
3391 | verifyFormat("struct T {};", Style); |
3392 | |
3393 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
3394 | verifyFormat("void shortfunction()\n" |
3395 | "{\n" |
3396 | " bar();\n" |
3397 | "}", |
3398 | Style); |
3399 | verifyFormat("struct T shortfunction()\n" |
3400 | "{\n" |
3401 | " return bar();\n" |
3402 | "}", |
3403 | Style); |
3404 | verifyFormat("struct T {};", Style); |
3405 | |
3406 | Style.BraceWrapping.AfterFunction = false; |
3407 | Style.BraceWrapping.AfterStruct = true; |
3408 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
3409 | verifyFormat("void shortfunction() { bar(); }", Style); |
3410 | verifyFormat("struct T shortfunction() { return bar(); }", Style); |
3411 | verifyFormat("struct T\n" |
3412 | "{\n" |
3413 | "};", |
3414 | Style); |
3415 | |
3416 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
3417 | verifyFormat("void shortfunction() {\n" |
3418 | " bar();\n" |
3419 | "}", |
3420 | Style); |
3421 | verifyFormat("struct T shortfunction() {\n" |
3422 | " return bar();\n" |
3423 | "}", |
3424 | Style); |
3425 | verifyFormat("struct T\n" |
3426 | "{\n" |
3427 | "};", |
3428 | Style); |
3429 | |
3430 | Style = getLLVMStyle(); |
3431 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
3432 | Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; |
3433 | Style.AllowShortLoopsOnASingleLine = true; |
3434 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
3435 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; |
3436 | verifyFormat("if (true) { return; }", Style); |
3437 | verifyFormat("while (true) { return; }", Style); |
3438 | // Failing test in https://reviews.llvm.org/D114521#3151727 |
3439 | verifyFormat("for (;;) { bar(); }", Style); |
3440 | } |
3441 | |
3442 | TEST_F(FormatTest, BeforeWhile) { |
3443 | FormatStyle Style = getLLVMStyle(); |
3444 | Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; |
3445 | |
3446 | verifyFormat("do {\n" |
3447 | " foo();\n" |
3448 | "} while (1);", |
3449 | Style); |
3450 | Style.BraceWrapping.BeforeWhile = true; |
3451 | verifyFormat("do {\n" |
3452 | " foo();\n" |
3453 | "}\n" |
3454 | "while (1);", |
3455 | Style); |
3456 | } |
3457 | |
3458 | //===----------------------------------------------------------------------===// |
3459 | // Tests for classes, namespaces, etc. |
3460 | //===----------------------------------------------------------------------===// |
3461 | |
3462 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
3463 | verifyFormat("class A {};"); |
3464 | } |
3465 | |
3466 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
3467 | verifyFormat("class A {\n" |
3468 | "public:\n" |
3469 | "public: // comment\n" |
3470 | "protected:\n" |
3471 | "private:\n" |
3472 | " void f() {}\n" |
3473 | "};"); |
3474 | verifyFormat("export class A {\n" |
3475 | "public:\n" |
3476 | "public: // comment\n" |
3477 | "protected:\n" |
3478 | "private:\n" |
3479 | " void f() {}\n" |
3480 | "};"); |
3481 | verifyGoogleFormat("class A {\n" |
3482 | " public:\n" |
3483 | " protected:\n" |
3484 | " private:\n" |
3485 | " void f() {}\n" |
3486 | "};"); |
3487 | verifyGoogleFormat("export class A {\n" |
3488 | " public:\n" |
3489 | " protected:\n" |
3490 | " private:\n" |
3491 | " void f() {}\n" |
3492 | "};"); |
3493 | verifyFormat("class A {\n" |
3494 | "public slots:\n" |
3495 | " void f1() {}\n" |
3496 | "public Q_SLOTS:\n" |
3497 | " void f2() {}\n" |
3498 | "protected slots:\n" |
3499 | " void f3() {}\n" |
3500 | "protected Q_SLOTS:\n" |
3501 | " void f4() {}\n" |
3502 | "private slots:\n" |
3503 | " void f5() {}\n" |
3504 | "private Q_SLOTS:\n" |
3505 | " void f6() {}\n" |
3506 | "signals:\n" |
3507 | " void g1();\n" |
3508 | "Q_SIGNALS:\n" |
3509 | " void g2();\n" |
3510 | "};"); |
3511 | |
3512 | // Don't interpret 'signals' the wrong way. |
3513 | verifyFormat("signals.set();"); |
3514 | verifyFormat("for (Signals signals : f()) {\n}"); |
3515 | verifyFormat("{\n" |
3516 | " signals.set(); // This needs indentation.\n" |
3517 | "}"); |
3518 | verifyFormat("void f() {\n" |
3519 | "label:\n" |
3520 | " signals.baz();\n" |
3521 | "}"); |
3522 | |
3523 | const auto Style = getLLVMStyle(Language: FormatStyle::LK_C); |
3524 | verifyFormat("private[1];", Style); |
3525 | verifyFormat("testArray[public] = 1;"); |
3526 | verifyFormat("public();", Style); |
3527 | verifyFormat("myFunc(public);"); |
3528 | verifyFormat("std::vector<int> testVec = {private};"); |
3529 | verifyFormat("private.p = 1;", Style); |
3530 | verifyFormat("void function(private...) {};"); |
3531 | verifyFormat("if (private && public)"); |
3532 | verifyFormat("private &= true;", Style); |
3533 | verifyFormat("int x = private * public;"); |
3534 | verifyFormat("public *= private;", Style); |
3535 | verifyFormat("int x = public + private;"); |
3536 | verifyFormat("private++;", Style); |
3537 | verifyFormat("++private;"); |
3538 | verifyFormat("public += private;", Style); |
3539 | verifyFormat("public = public - private;", Style); |
3540 | verifyFormat("public->foo();", Style); |
3541 | verifyFormat("private--;", Style); |
3542 | verifyFormat("--private;"); |
3543 | verifyFormat("public -= 1;", Style); |
3544 | verifyFormat("if (!private && !public)"); |
3545 | verifyFormat("public != private;", Style); |
3546 | verifyFormat("int x = public / private;"); |
3547 | verifyFormat("public /= 2;", Style); |
3548 | verifyFormat("public = public % 2;", Style); |
3549 | verifyFormat("public %= 2;", Style); |
3550 | verifyFormat("if (public < private)"); |
3551 | verifyFormat("public << private;", Style); |
3552 | verifyFormat("public <<= private;", Style); |
3553 | verifyFormat("if (public > private)"); |
3554 | verifyFormat("public >> private;", Style); |
3555 | verifyFormat("public >>= private;", Style); |
3556 | verifyFormat("public ^ private;", Style); |
3557 | verifyFormat("public ^= private;", Style); |
3558 | verifyFormat("public | private;", Style); |
3559 | verifyFormat("public |= private;", Style); |
3560 | verifyFormat("auto x = private ? 1 : 2;"); |
3561 | verifyFormat("if (public == private)"); |
3562 | verifyFormat("void foo(public, private)"); |
3563 | |
3564 | verifyFormat("class A {\n" |
3565 | "public:\n" |
3566 | " std::unique_ptr<int *[]> b() { return nullptr; }\n" |
3567 | "\n" |
3568 | "private:\n" |
3569 | " int c;\n" |
3570 | "};\n" |
3571 | "class B {\n" |
3572 | "public:\n" |
3573 | " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n" |
3574 | "\n" |
3575 | "private:\n" |
3576 | " int c;\n" |
3577 | "};"); |
3578 | } |
3579 | |
3580 | TEST_F(FormatTest, SeparatesLogicalBlocks) { |
3581 | verifyFormat("class A {\n" |
3582 | "public:\n" |
3583 | " void f();\n" |
3584 | "\n" |
3585 | "private:\n" |
3586 | " void g() {}\n" |
3587 | " // test\n" |
3588 | "protected:\n" |
3589 | " int h;\n" |
3590 | "};", |
3591 | "class A {\n" |
3592 | "public:\n" |
3593 | "void f();\n" |
3594 | "private:\n" |
3595 | "void g() {}\n" |
3596 | "// test\n" |
3597 | "protected:\n" |
3598 | "int h;\n" |
3599 | "};"); |
3600 | verifyFormat("class A {\n" |
3601 | "protected:\n" |
3602 | "public:\n" |
3603 | " void f();\n" |
3604 | "};", |
3605 | "class A {\n" |
3606 | "protected:\n" |
3607 | "\n" |
3608 | "public:\n" |
3609 | "\n" |
3610 | " void f();\n" |
3611 | "};"); |
3612 | |
3613 | // Even ensure proper spacing inside macros. |
3614 | verifyFormat("#define B \\\n" |
3615 | " class A { \\\n" |
3616 | " protected: \\\n" |
3617 | " public: \\\n" |
3618 | " void f(); \\\n" |
3619 | " };", |
3620 | "#define B \\\n" |
3621 | " class A { \\\n" |
3622 | " protected: \\\n" |
3623 | " \\\n" |
3624 | " public: \\\n" |
3625 | " \\\n" |
3626 | " void f(); \\\n" |
3627 | " };", |
3628 | getGoogleStyle()); |
3629 | // But don't remove empty lines after macros ending in access specifiers. |
3630 | verifyFormat("#define A private:\n" |
3631 | "\n" |
3632 | "int i;", |
3633 | "#define A private:\n" |
3634 | "\n" |
3635 | "int i;"); |
3636 | } |
3637 | |
3638 | TEST_F(FormatTest, FormatsClasses) { |
3639 | verifyFormat("class A : public B {};"); |
3640 | verifyFormat("class A : public ::B {};"); |
3641 | |
3642 | verifyFormat( |
3643 | "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
3644 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); |
3645 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
3646 | " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
3647 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); |
3648 | verifyFormat( |
3649 | "class A : public B, public C, public D, public E, public F {};"); |
3650 | verifyFormat("class AAAAAAAAAAAA : public B,\n" |
3651 | " public C,\n" |
3652 | " public D,\n" |
3653 | " public E,\n" |
3654 | " public F,\n" |
3655 | " public G {};"); |
3656 | |
3657 | verifyFormat("class\n" |
3658 | " ReallyReallyLongClassName {\n" |
3659 | " int i;\n" |
3660 | "};", |
3661 | getLLVMStyleWithColumns(32)); |
3662 | verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" |
3663 | " aaaaaaaaaaaaaaaa> {};"); |
3664 | verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" |
3665 | " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" |
3666 | " aaaaaaaaaaaaaaaaaaaaaa> {};"); |
3667 | verifyFormat("template <class R, class C>\n" |
3668 | "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" |
3669 | " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); |
3670 | verifyFormat("class ::A::B {};"); |
3671 | } |
3672 | |
3673 | TEST_F(FormatTest, BreakInheritanceStyle) { |
3674 | FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); |
3675 | StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = |
3676 | FormatStyle::BILS_BeforeComma; |
3677 | verifyFormat("class MyClass : public X {};", |
3678 | StyleWithInheritanceBreakBeforeComma); |
3679 | verifyFormat("class MyClass\n" |
3680 | " : public X\n" |
3681 | " , public Y {};", |
3682 | StyleWithInheritanceBreakBeforeComma); |
3683 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" |
3684 | " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" |
3685 | " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", |
3686 | StyleWithInheritanceBreakBeforeComma); |
3687 | verifyFormat("struct aaaaaaaaaaaaa\n" |
3688 | " : public aaaaaaaaaaaaaaaaaaa< // break\n" |
3689 | " aaaaaaaaaaaaaaaa> {};", |
3690 | StyleWithInheritanceBreakBeforeComma); |
3691 | |
3692 | FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); |
3693 | StyleWithInheritanceBreakAfterColon.BreakInheritanceList = |
3694 | FormatStyle::BILS_AfterColon; |
3695 | verifyFormat("class MyClass : public X {};", |
3696 | StyleWithInheritanceBreakAfterColon); |
3697 | verifyFormat("class MyClass : public X, public Y {};", |
3698 | StyleWithInheritanceBreakAfterColon); |
3699 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" |
3700 | " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
3701 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", |
3702 | StyleWithInheritanceBreakAfterColon); |
3703 | verifyFormat("struct aaaaaaaaaaaaa :\n" |
3704 | " public aaaaaaaaaaaaaaaaaaa< // break\n" |
3705 | " aaaaaaaaaaaaaaaa> {};", |
3706 | StyleWithInheritanceBreakAfterColon); |
3707 | |
3708 | FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); |
3709 | StyleWithInheritanceBreakAfterComma.BreakInheritanceList = |
3710 | FormatStyle::BILS_AfterComma; |
3711 | verifyFormat("class MyClass : public X {};", |
3712 | StyleWithInheritanceBreakAfterComma); |
3713 | verifyFormat("class MyClass : public X,\n" |
3714 | " public Y {};", |
3715 | StyleWithInheritanceBreakAfterComma); |
3716 | verifyFormat( |
3717 | "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
3718 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " |
3719 | "{};", |
3720 | StyleWithInheritanceBreakAfterComma); |
3721 | verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" |
3722 | " aaaaaaaaaaaaaaaa> {};", |
3723 | StyleWithInheritanceBreakAfterComma); |
3724 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" |
3725 | " : public OnceBreak,\n" |
3726 | " public AlwaysBreak,\n" |
3727 | " EvenBasesFitInOneLine {};", |
3728 | StyleWithInheritanceBreakAfterComma); |
3729 | } |
3730 | |
3731 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) { |
3732 | verifyFormat("class A {\n} a, b;"); |
3733 | verifyFormat("struct A {\n} a, b;"); |
3734 | verifyFormat("union A {\n} a, b;"); |
3735 | |
3736 | verifyFormat("constexpr class A {\n} a, b;"); |
3737 | verifyFormat("constexpr struct A {\n} a, b;"); |
3738 | verifyFormat("constexpr union A {\n} a, b;"); |
3739 | |
3740 | verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace"); |
3741 | verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace"); |
3742 | verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace"); |
3743 | |
3744 | verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace"); |
3745 | verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace"); |
3746 | verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace"); |
3747 | |
3748 | verifyFormat("namespace ns {\n" |
3749 | "class {\n" |
3750 | "} a, b;\n" |
3751 | "} // namespace ns"); |
3752 | verifyFormat("namespace ns {\n" |
3753 | "const class {\n" |
3754 | "} a, b;\n" |
3755 | "} // namespace ns"); |
3756 | verifyFormat("namespace ns {\n" |
3757 | "constexpr class C {\n" |
3758 | "} a, b;\n" |
3759 | "} // namespace ns"); |
3760 | verifyFormat("namespace ns {\n" |
3761 | "class { /* comment */\n" |
3762 | "} a, b;\n" |
3763 | "} // namespace ns"); |
3764 | verifyFormat("namespace ns {\n" |
3765 | "const class { /* comment */\n" |
3766 | "} a, b;\n" |
3767 | "} // namespace ns"); |
3768 | } |
3769 | |
3770 | TEST_F(FormatTest, FormatsEnum) { |
3771 | verifyFormat("enum {\n" |
3772 | " Zero,\n" |
3773 | " One = 1,\n" |
3774 | " Two = One + 1,\n" |
3775 | " Three = (One + Two),\n" |
3776 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
3777 | " Five = (One, Two, Three, Four, 5)\n" |
3778 | "};"); |
3779 | verifyGoogleFormat("enum {\n" |
3780 | " Zero,\n" |
3781 | " One = 1,\n" |
3782 | " Two = One + 1,\n" |
3783 | " Three = (One + Two),\n" |
3784 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
3785 | " Five = (One, Two, Three, Four, 5)\n" |
3786 | "};"); |
3787 | verifyFormat("enum Enum {};"); |
3788 | verifyFormat("enum {};"); |
3789 | verifyFormat("enum X E {} d;"); |
3790 | verifyFormat("enum __attribute__((...)) E {} d;"); |
3791 | verifyFormat("enum __declspec__((...)) E {} d;"); |
3792 | verifyFormat("enum [[nodiscard]] E {} d;"); |
3793 | verifyFormat("enum {\n" |
3794 | " Bar = Foo<int, int>::value\n" |
3795 | "};", |
3796 | getLLVMStyleWithColumns(30)); |
3797 | |
3798 | verifyFormat("enum ShortEnum { A, B, C };"); |
3799 | verifyGoogleFormat("enum ShortEnum { A, B, C };"); |
3800 | |
3801 | verifyFormat("enum KeepEmptyLines {\n" |
3802 | " ONE,\n" |
3803 | "\n" |
3804 | " TWO,\n" |
3805 | "\n" |
3806 | " THREE\n" |
3807 | "}", |
3808 | "enum KeepEmptyLines {\n" |
3809 | " ONE,\n" |
3810 | "\n" |
3811 | " TWO,\n" |
3812 | "\n" |
3813 | "\n" |
3814 | " THREE\n" |
3815 | "}"); |
3816 | verifyFormat("enum E { // comment\n" |
3817 | " ONE,\n" |
3818 | " TWO\n" |
3819 | "};\n" |
3820 | "int i;"); |
3821 | |
3822 | FormatStyle EightIndent = getLLVMStyle(); |
3823 | EightIndent.IndentWidth = 8; |
3824 | verifyFormat("enum {\n" |
3825 | " VOID,\n" |
3826 | " CHAR,\n" |
3827 | " SHORT,\n" |
3828 | " INT,\n" |
3829 | " LONG,\n" |
3830 | " SIGNED,\n" |
3831 | " UNSIGNED,\n" |
3832 | " BOOL,\n" |
3833 | " FLOAT,\n" |
3834 | " DOUBLE,\n" |
3835 | " COMPLEX\n" |
3836 | "};", |
3837 | EightIndent); |
3838 | |
3839 | verifyFormat("enum [[nodiscard]] E {\n" |
3840 | " ONE,\n" |
3841 | " TWO,\n" |
3842 | "};"); |
3843 | verifyFormat("enum [[nodiscard]] E {\n" |
3844 | " // Comment 1\n" |
3845 | " ONE,\n" |
3846 | " // Comment 2\n" |
3847 | " TWO,\n" |
3848 | "};"); |
3849 | verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n" |
3850 | " // Comment 1\n" |
3851 | " ONE,\n" |
3852 | " // Comment 2\n" |
3853 | " TWO\n" |
3854 | "};"); |
3855 | verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n" |
3856 | " // Comment 1\n" |
3857 | " ONE,\n" |
3858 | " // Comment 2\n" |
3859 | " TWO\n" |
3860 | "};"); |
3861 | verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n" |
3862 | " A,\n" |
3863 | " // bar\n" |
3864 | " B\n" |
3865 | "};", |
3866 | "enum [[clang::enum_extensibility(open)]] E{// foo\n" |
3867 | " A,\n" |
3868 | " // bar\n" |
3869 | " B};"); |
3870 | |
3871 | // Not enums. |
3872 | verifyFormat("enum X f() {\n" |
3873 | " a();\n" |
3874 | " return 42;\n" |
3875 | "}"); |
3876 | verifyFormat("enum X Type::f() {\n" |
3877 | " a();\n" |
3878 | " return 42;\n" |
3879 | "}"); |
3880 | verifyFormat("enum ::X f() {\n" |
3881 | " a();\n" |
3882 | " return 42;\n" |
3883 | "}"); |
3884 | verifyFormat("enum ns::X f() {\n" |
3885 | " a();\n" |
3886 | " return 42;\n" |
3887 | "}"); |
3888 | } |
3889 | |
3890 | TEST_F(FormatTest, FormatsEnumsWithErrors) { |
3891 | verifyFormat("enum Type {\n" |
3892 | " One = 0; // These semicolons should be commas.\n" |
3893 | " Two = 1;\n" |
3894 | "};"); |
3895 | verifyFormat("namespace n {\n" |
3896 | "enum Type {\n" |
3897 | " One,\n" |
3898 | " Two, // missing };\n" |
3899 | " int i;\n" |
3900 | "}\n" |
3901 | "void g() {}"); |
3902 | } |
3903 | |
3904 | TEST_F(FormatTest, FormatsEnumStruct) { |
3905 | verifyFormat("enum struct {\n" |
3906 | " Zero,\n" |
3907 | " One = 1,\n" |
3908 | " Two = One + 1,\n" |
3909 | " Three = (One + Two),\n" |
3910 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
3911 | " Five = (One, Two, Three, Four, 5)\n" |
3912 | "};"); |
3913 | verifyFormat("enum struct Enum {};"); |
3914 | verifyFormat("enum struct {};"); |
3915 | verifyFormat("enum struct X E {} d;"); |
3916 | verifyFormat("enum struct __attribute__((...)) E {} d;"); |
3917 | verifyFormat("enum struct __declspec__((...)) E {} d;"); |
3918 | verifyFormat("enum struct [[nodiscard]] E {} d;"); |
3919 | verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); |
3920 | |
3921 | verifyFormat("enum struct [[nodiscard]] E {\n" |
3922 | " ONE,\n" |
3923 | " TWO,\n" |
3924 | "};"); |
3925 | verifyFormat("enum struct [[nodiscard]] E {\n" |
3926 | " // Comment 1\n" |
3927 | " ONE,\n" |
3928 | " // Comment 2\n" |
3929 | " TWO,\n" |
3930 | "};"); |
3931 | } |
3932 | |
3933 | TEST_F(FormatTest, FormatsEnumClass) { |
3934 | verifyFormat("enum class {\n" |
3935 | " Zero,\n" |
3936 | " One = 1,\n" |
3937 | " Two = One + 1,\n" |
3938 | " Three = (One + Two),\n" |
3939 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
3940 | " Five = (One, Two, Three, Four, 5)\n" |
3941 | "};"); |
3942 | verifyFormat("enum class Enum {};"); |
3943 | verifyFormat("enum class {};"); |
3944 | verifyFormat("enum class X E {} d;"); |
3945 | verifyFormat("enum class __attribute__((...)) E {} d;"); |
3946 | verifyFormat("enum class __declspec__((...)) E {} d;"); |
3947 | verifyFormat("enum class [[nodiscard]] E {} d;"); |
3948 | verifyFormat("enum class X f() {\n a();\n return 42;\n}"); |
3949 | |
3950 | verifyFormat("enum class [[nodiscard]] E {\n" |
3951 | " ONE,\n" |
3952 | " TWO,\n" |
3953 | "};"); |
3954 | verifyFormat("enum class [[nodiscard]] E {\n" |
3955 | " // Comment 1\n" |
3956 | " ONE,\n" |
3957 | " // Comment 2\n" |
3958 | " TWO,\n" |
3959 | "};"); |
3960 | } |
3961 | |
3962 | TEST_F(FormatTest, FormatsEnumTypes) { |
3963 | verifyFormat("enum X : int {\n" |
3964 | " A, // Force multiple lines.\n" |
3965 | " B\n" |
3966 | "};"); |
3967 | verifyFormat("enum X : int { A, B };"); |
3968 | verifyFormat("enum X : std::uint32_t { A, B };"); |
3969 | } |
3970 | |
3971 | TEST_F(FormatTest, FormatsTypedefEnum) { |
3972 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
3973 | verifyFormat("typedef enum {} EmptyEnum;"); |
3974 | verifyFormat("typedef enum { A, B, C } ShortEnum;"); |
3975 | verifyFormat("typedef enum {\n" |
3976 | " ZERO = 0,\n" |
3977 | " ONE = 1,\n" |
3978 | " TWO = 2,\n" |
3979 | " THREE = 3\n" |
3980 | "} LongEnum;", |
3981 | Style); |
3982 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
3983 | Style.BraceWrapping.AfterEnum = true; |
3984 | verifyFormat("typedef enum {} EmptyEnum;"); |
3985 | verifyFormat("typedef enum { A, B, C } ShortEnum;"); |
3986 | verifyFormat("typedef enum\n" |
3987 | "{\n" |
3988 | " ZERO = 0,\n" |
3989 | " ONE = 1,\n" |
3990 | " TWO = 2,\n" |
3991 | " THREE = 3\n" |
3992 | "} LongEnum;", |
3993 | Style); |
3994 | } |
3995 | |
3996 | TEST_F(FormatTest, FormatsNSEnums) { |
3997 | verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); |
3998 | verifyGoogleFormat( |
3999 | "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); |
4000 | verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" |
4001 | " // Information about someDecentlyLongValue.\n" |
4002 | " someDecentlyLongValue,\n" |
4003 | " // Information about anotherDecentlyLongValue.\n" |
4004 | " anotherDecentlyLongValue,\n" |
4005 | " // Information about aThirdDecentlyLongValue.\n" |
4006 | " aThirdDecentlyLongValue\n" |
4007 | "};"); |
4008 | verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" |
4009 | " // Information about someDecentlyLongValue.\n" |
4010 | " someDecentlyLongValue,\n" |
4011 | " // Information about anotherDecentlyLongValue.\n" |
4012 | " anotherDecentlyLongValue,\n" |
4013 | " // Information about aThirdDecentlyLongValue.\n" |
4014 | " aThirdDecentlyLongValue\n" |
4015 | "};"); |
4016 | verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" |
4017 | " a = 1,\n" |
4018 | " b = 2,\n" |
4019 | " c = 3,\n" |
4020 | "};"); |
4021 | verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" |
4022 | " a = 1,\n" |
4023 | " b = 2,\n" |
4024 | " c = 3,\n" |
4025 | "};"); |
4026 | verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" |
4027 | " a = 1,\n" |
4028 | " b = 2,\n" |
4029 | " c = 3,\n" |
4030 | "};"); |
4031 | verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" |
4032 | " a = 1,\n" |
4033 | " b = 2,\n" |
4034 | " c = 3,\n" |
4035 | "};"); |
4036 | } |
4037 | |
4038 | TEST_F(FormatTest, FormatsBitfields) { |
4039 | verifyFormat("struct Bitfields {\n" |
4040 | " unsigned sClass : 8;\n" |
4041 | " unsigned ValueKind : 2;\n" |
4042 | "};"); |
4043 | verifyFormat("struct A {\n" |
4044 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" |
4045 | " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" |
4046 | "};"); |
4047 | verifyFormat("struct MyStruct {\n" |
4048 | " uchar data;\n" |
4049 | " uchar : 8;\n" |
4050 | " uchar : 8;\n" |
4051 | " uchar other;\n" |
4052 | "};"); |
4053 | FormatStyle Style = getLLVMStyle(); |
4054 | Style.BitFieldColonSpacing = FormatStyle::BFCS_None; |
4055 | verifyFormat("struct Bitfields {\n" |
4056 | " unsigned sClass:8;\n" |
4057 | " unsigned ValueKind:2;\n" |
4058 | " uchar other;\n" |
4059 | "};", |
4060 | Style); |
4061 | verifyFormat("struct A {\n" |
4062 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" |
4063 | " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" |
4064 | "};", |
4065 | Style); |
4066 | Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; |
4067 | verifyFormat("struct Bitfields {\n" |
4068 | " unsigned sClass :8;\n" |
4069 | " unsigned ValueKind :2;\n" |
4070 | " uchar other;\n" |
4071 | "};", |
4072 | Style); |
4073 | Style.BitFieldColonSpacing = FormatStyle::BFCS_After; |
4074 | verifyFormat("struct Bitfields {\n" |
4075 | " unsigned sClass: 8;\n" |
4076 | " unsigned ValueKind: 2;\n" |
4077 | " uchar other;\n" |
4078 | "};", |
4079 | Style); |
4080 | } |
4081 | |
4082 | TEST_F(FormatTest, FormatsNamespaces) { |
4083 | FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); |
4084 | LLVMWithNoNamespaceFix.FixNamespaceComments = false; |
4085 | |
4086 | verifyFormat("namespace some_namespace {\n" |
4087 | "class A {};\n" |
4088 | "void f() { f(); }\n" |
4089 | "}", |
4090 | LLVMWithNoNamespaceFix); |
4091 | verifyFormat("#define M(x) x##x\n" |
4092 | "namespace M(x) {\n" |
4093 | "class A {};\n" |
4094 | "void f() { f(); }\n" |
4095 | "}", |
4096 | LLVMWithNoNamespaceFix); |
4097 | verifyFormat("#define M(x) x##x\n" |
4098 | "namespace N::inline M(x) {\n" |
4099 | "class A {};\n" |
4100 | "void f() { f(); }\n" |
4101 | "}", |
4102 | LLVMWithNoNamespaceFix); |
4103 | verifyFormat("#define M(x) x##x\n" |
4104 | "namespace M(x)::inline N {\n" |
4105 | "class A {};\n" |
4106 | "void f() { f(); }\n" |
4107 | "}", |
4108 | LLVMWithNoNamespaceFix); |
4109 | verifyFormat("#define M(x) x##x\n" |
4110 | "namespace N::M(x) {\n" |
4111 | "class A {};\n" |
4112 | "void f() { f(); }\n" |
4113 | "}", |
4114 | LLVMWithNoNamespaceFix); |
4115 | verifyFormat("#define M(x) x##x\n" |
4116 | "namespace M::N(x) {\n" |
4117 | "class A {};\n" |
4118 | "void f() { f(); }\n" |
4119 | "}", |
4120 | LLVMWithNoNamespaceFix); |
4121 | verifyFormat("namespace N::inline D {\n" |
4122 | "class A {};\n" |
4123 | "void f() { f(); }\n" |
4124 | "}", |
4125 | LLVMWithNoNamespaceFix); |
4126 | verifyFormat("namespace N::inline D::E {\n" |
4127 | "class A {};\n" |
4128 | "void f() { f(); }\n" |
4129 | "}", |
4130 | LLVMWithNoNamespaceFix); |
4131 | verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" |
4132 | "class A {};\n" |
4133 | "void f() { f(); }\n" |
4134 | "}", |
4135 | LLVMWithNoNamespaceFix); |
4136 | verifyFormat("/* something */ namespace some_namespace {\n" |
4137 | "class A {};\n" |
4138 | "void f() { f(); }\n" |
4139 | "}", |
4140 | LLVMWithNoNamespaceFix); |
4141 | verifyFormat("namespace {\n" |
4142 | "class A {};\n" |
4143 | "void f() { f(); }\n" |
4144 | "}", |
4145 | LLVMWithNoNamespaceFix); |
4146 | verifyFormat("/* something */ namespace {\n" |
4147 | "class A {};\n" |
4148 | "void f() { f(); }\n" |
4149 | "}", |
4150 | LLVMWithNoNamespaceFix); |
4151 | verifyFormat("inline namespace X {\n" |
4152 | "class A {};\n" |
4153 | "void f() { f(); }\n" |
4154 | "}", |
4155 | LLVMWithNoNamespaceFix); |
4156 | verifyFormat("/* something */ inline namespace X {\n" |
4157 | "class A {};\n" |
4158 | "void f() { f(); }\n" |
4159 | "}", |
4160 | LLVMWithNoNamespaceFix); |
4161 | verifyFormat("export namespace X {\n" |
4162 | "class A {};\n" |
4163 | "void f() { f(); }\n" |
4164 | "}", |
4165 | LLVMWithNoNamespaceFix); |
4166 | verifyFormat("using namespace some_namespace;\n" |
4167 | "class A {};\n" |
4168 | "void f() { f(); }", |
4169 | LLVMWithNoNamespaceFix); |
4170 | |
4171 | // This code is more common than we thought; if we |
4172 | // layout this correctly the semicolon will go into |
4173 | // its own line, which is undesirable. |
4174 | verifyFormat("namespace {};", LLVMWithNoNamespaceFix); |
4175 | verifyFormat("namespace {\n" |
4176 | "class A {};\n" |
4177 | "};", |
4178 | LLVMWithNoNamespaceFix); |
4179 | |
4180 | verifyFormat("namespace {\n" |
4181 | "int SomeVariable = 0; // comment\n" |
4182 | "} // namespace", |
4183 | LLVMWithNoNamespaceFix); |
4184 | verifyFormat("#ifndef HEADER_GUARD\n" |
4185 | "#define HEADER_GUARD\n" |
4186 | "namespace my_namespace {\n" |
4187 | "int i;\n" |
4188 | "} // my_namespace\n" |
4189 | "#endif // HEADER_GUARD", |
4190 | "#ifndef HEADER_GUARD\n" |
4191 | " #define HEADER_GUARD\n" |
4192 | " namespace my_namespace {\n" |
4193 | "int i;\n" |
4194 | "} // my_namespace\n" |
4195 | "#endif // HEADER_GUARD", |
4196 | LLVMWithNoNamespaceFix); |
4197 | |
4198 | verifyFormat("namespace A::B {\n" |
4199 | "class C {};\n" |
4200 | "}", |
4201 | LLVMWithNoNamespaceFix); |
4202 | |
4203 | FormatStyle Style = getLLVMStyle(); |
4204 | Style.NamespaceIndentation = FormatStyle::NI_All; |
4205 | verifyFormat("namespace out {\n" |
4206 | " int i;\n" |
4207 | " namespace in {\n" |
4208 | " int i;\n" |
4209 | " } // namespace in\n" |
4210 | "} // namespace out", |
4211 | "namespace out {\n" |
4212 | "int i;\n" |
4213 | "namespace in {\n" |
4214 | "int i;\n" |
4215 | "} // namespace in\n" |
4216 | "} // namespace out", |
4217 | Style); |
4218 | |
4219 | FormatStyle ShortInlineFunctions = getLLVMStyle(); |
4220 | ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; |
4221 | ShortInlineFunctions.AllowShortFunctionsOnASingleLine = |
4222 | FormatStyle::SFS_Inline; |
4223 | verifyFormat("namespace {\n" |
4224 | " void f() {\n" |
4225 | " return;\n" |
4226 | " }\n" |
4227 | "} // namespace", |
4228 | ShortInlineFunctions); |
4229 | verifyFormat("namespace { /* comment */\n" |
4230 | " void f() {\n" |
4231 | " return;\n" |
4232 | " }\n" |
4233 | "} // namespace", |
4234 | ShortInlineFunctions); |
4235 | verifyFormat("namespace { // comment\n" |
4236 | " void f() {\n" |
4237 | " return;\n" |
4238 | " }\n" |
4239 | "} // namespace", |
4240 | ShortInlineFunctions); |
4241 | verifyFormat("namespace {\n" |
4242 | " int some_int;\n" |
4243 | " void f() {\n" |
4244 | " return;\n" |
4245 | " }\n" |
4246 | "} // namespace", |
4247 | ShortInlineFunctions); |
4248 | verifyFormat("namespace interface {\n" |
4249 | " void f() {\n" |
4250 | " return;\n" |
4251 | " }\n" |
4252 | "} // namespace interface", |
4253 | ShortInlineFunctions); |
4254 | verifyFormat("namespace {\n" |
4255 | " class X {\n" |
4256 | " void f() { return; }\n" |
4257 | " };\n" |
4258 | "} // namespace", |
4259 | ShortInlineFunctions); |
4260 | verifyFormat("namespace {\n" |
4261 | " class X { /* comment */\n" |
4262 | " void f() { return; }\n" |
4263 | " };\n" |
4264 | "} // namespace", |
4265 | ShortInlineFunctions); |
4266 | verifyFormat("namespace {\n" |
4267 | " class X { // comment\n" |
4268 | " void f() { return; }\n" |
4269 | " };\n" |
4270 | "} // namespace", |
4271 | ShortInlineFunctions); |
4272 | verifyFormat("namespace {\n" |
4273 | " struct X {\n" |
4274 | " void f() { return; }\n" |
4275 | " };\n" |
4276 | "} // namespace", |
4277 | ShortInlineFunctions); |
4278 | verifyFormat("namespace {\n" |
4279 | " union X {\n" |
4280 | " void f() { return; }\n" |
4281 | " };\n" |
4282 | "} // namespace", |
4283 | ShortInlineFunctions); |
4284 | verifyFormat("extern \"C\" {\n" |
4285 | "void f() {\n" |
4286 | " return;\n" |
4287 | "}\n" |
4288 | "} // namespace", |
4289 | ShortInlineFunctions); |
4290 | verifyFormat("namespace {\n" |
4291 | " class X {\n" |
4292 | " void f() { return; }\n" |
4293 | " } x;\n" |
4294 | "} // namespace", |
4295 | ShortInlineFunctions); |
4296 | verifyFormat("namespace {\n" |
4297 | " [[nodiscard]] class X {\n" |
4298 | " void f() { return; }\n" |
4299 | " };\n" |
4300 | "} // namespace", |
4301 | ShortInlineFunctions); |
4302 | verifyFormat("namespace {\n" |
4303 | " static class X {\n" |
4304 | " void f() { return; }\n" |
4305 | " } x;\n" |
4306 | "} // namespace", |
4307 | ShortInlineFunctions); |
4308 | verifyFormat("namespace {\n" |
4309 | " constexpr class X {\n" |
4310 | " void f() { return; }\n" |
4311 | " } x;\n" |
4312 | "} // namespace", |
4313 | ShortInlineFunctions); |
4314 | |
4315 | ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; |
4316 | verifyFormat("extern \"C\" {\n" |
4317 | " void f() {\n" |
4318 | " return;\n" |
4319 | " }\n" |
4320 | "} // namespace", |
4321 | ShortInlineFunctions); |
4322 | |
4323 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
4324 | verifyFormat("namespace out {\n" |
4325 | "int i;\n" |
4326 | "namespace in {\n" |
4327 | " int i;\n" |
4328 | "} // namespace in\n" |
4329 | "} // namespace out", |
4330 | "namespace out {\n" |
4331 | "int i;\n" |
4332 | "namespace in {\n" |
4333 | "int i;\n" |
4334 | "} // namespace in\n" |
4335 | "} // namespace out", |
4336 | Style); |
4337 | |
4338 | Style.NamespaceIndentation = FormatStyle::NI_None; |
4339 | verifyFormat("template <class T>\n" |
4340 | "concept a_concept = X<>;\n" |
4341 | "namespace B {\n" |
4342 | "struct b_struct {};\n" |
4343 | "} // namespace B", |
4344 | Style); |
4345 | verifyFormat("template <int I>\n" |
4346 | "constexpr void foo()\n" |
4347 | " requires(I == 42)\n" |
4348 | "{}\n" |
4349 | "namespace ns {\n" |
4350 | "void foo() {}\n" |
4351 | "} // namespace ns", |
4352 | Style); |
4353 | |
4354 | FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle(); |
4355 | LLVMWithCompactInnerNamespace.CompactNamespaces = true; |
4356 | LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner; |
4357 | verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n" |
4358 | "// block for debug mode\n" |
4359 | "#ifndef NDEBUG\n" |
4360 | "#endif\n" |
4361 | "}}} // namespace ns1::ns2::ns3", |
4362 | LLVMWithCompactInnerNamespace); |
4363 | } |
4364 | |
4365 | TEST_F(FormatTest, NamespaceMacros) { |
4366 | FormatStyle Style = getLLVMStyle(); |
4367 | Style.NamespaceMacros.push_back(x: "TESTSUITE"); |
4368 | |
4369 | verifyFormat("TESTSUITE(A) {\n" |
4370 | "int foo();\n" |
4371 | "} // TESTSUITE(A)", |
4372 | Style); |
4373 | |
4374 | verifyFormat("TESTSUITE(A, B) {\n" |
4375 | "int foo();\n" |
4376 | "} // TESTSUITE(A)", |
4377 | Style); |
4378 | |
4379 | // Properly indent according to NamespaceIndentation style |
4380 | Style.NamespaceIndentation = FormatStyle::NI_All; |
4381 | verifyFormat("TESTSUITE(A) {\n" |
4382 | " int foo();\n" |
4383 | "} // TESTSUITE(A)", |
4384 | Style); |
4385 | verifyFormat("TESTSUITE(A) {\n" |
4386 | " namespace B {\n" |
4387 | " int foo();\n" |
4388 | " } // namespace B\n" |
4389 | "} // TESTSUITE(A)", |
4390 | Style); |
4391 | verifyFormat("namespace A {\n" |
4392 | " TESTSUITE(B) {\n" |
4393 | " int foo();\n" |
4394 | " } // TESTSUITE(B)\n" |
4395 | "} // namespace A", |
4396 | Style); |
4397 | |
4398 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
4399 | verifyFormat("TESTSUITE(A) {\n" |
4400 | "TESTSUITE(B) {\n" |
4401 | " int foo();\n" |
4402 | "} // TESTSUITE(B)\n" |
4403 | "} // TESTSUITE(A)", |
4404 | Style); |
4405 | verifyFormat("TESTSUITE(A) {\n" |
4406 | "namespace B {\n" |
4407 | " int foo();\n" |
4408 | "} // namespace B\n" |
4409 | "} // TESTSUITE(A)", |
4410 | Style); |
4411 | verifyFormat("namespace A {\n" |
4412 | "TESTSUITE(B) {\n" |
4413 | " int foo();\n" |
4414 | "} // TESTSUITE(B)\n" |
4415 | "} // namespace A", |
4416 | Style); |
4417 | |
4418 | // Properly merge namespace-macros blocks in CompactNamespaces mode |
4419 | Style.NamespaceIndentation = FormatStyle::NI_None; |
4420 | Style.CompactNamespaces = true; |
4421 | verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" |
4422 | "}} // TESTSUITE(A::B)", |
4423 | Style); |
4424 | |
4425 | verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" |
4426 | "}} // TESTSUITE(out::in)", |
4427 | "TESTSUITE(out) {\n" |
4428 | "TESTSUITE(in) {\n" |
4429 | "} // TESTSUITE(in)\n" |
4430 | "} // TESTSUITE(out)", |
4431 | Style); |
4432 | |
4433 | verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" |
4434 | "}} // TESTSUITE(out::in)", |
4435 | "TESTSUITE(out) {\n" |
4436 | "TESTSUITE(in) {\n" |
4437 | "} // TESTSUITE(in)\n" |
4438 | "} // TESTSUITE(out)", |
4439 | Style); |
4440 | |
4441 | // Do not merge different namespaces/macros |
4442 | verifyFormat("namespace out {\n" |
4443 | "TESTSUITE(in) {\n" |
4444 | "} // TESTSUITE(in)\n" |
4445 | "} // namespace out", |
4446 | Style); |
4447 | verifyFormat("TESTSUITE(out) {\n" |
4448 | "namespace in {\n" |
4449 | "} // namespace in\n" |
4450 | "} // TESTSUITE(out)", |
4451 | Style); |
4452 | Style.NamespaceMacros.push_back(x: "FOOBAR"); |
4453 | verifyFormat("TESTSUITE(out) {\n" |
4454 | "FOOBAR(in) {\n" |
4455 | "} // FOOBAR(in)\n" |
4456 | "} // TESTSUITE(out)", |
4457 | Style); |
4458 | } |
4459 | |
4460 | TEST_F(FormatTest, FormatsCompactNamespaces) { |
4461 | FormatStyle Style = getLLVMStyle(); |
4462 | Style.CompactNamespaces = true; |
4463 | Style.NamespaceMacros.push_back(x: "TESTSUITE"); |
4464 | |
4465 | verifyFormat("namespace A { namespace B {\n" |
4466 | "}} // namespace A::B", |
4467 | Style); |
4468 | |
4469 | verifyFormat("namespace out { namespace in {\n" |
4470 | "}} // namespace out::in", |
4471 | "namespace out {\n" |
4472 | "namespace in {\n" |
4473 | "} // namespace in\n" |
4474 | "} // namespace out", |
4475 | Style); |
4476 | |
4477 | // Only namespaces which have both consecutive opening and end get compacted |
4478 | verifyFormat("namespace out {\n" |
4479 | "namespace in1 {\n" |
4480 | "} // namespace in1\n" |
4481 | "namespace in2 {\n" |
4482 | "} // namespace in2\n" |
4483 | "} // namespace out", |
4484 | Style); |
4485 | |
4486 | verifyFormat("namespace out {\n" |
4487 | "int i;\n" |
4488 | "namespace in {\n" |
4489 | "int j;\n" |
4490 | "} // namespace in\n" |
4491 | "int k;\n" |
4492 | "} // namespace out", |
4493 | "namespace out { int i;\n" |
4494 | "namespace in { int j; } // namespace in\n" |
4495 | "int k; } // namespace out", |
4496 | Style); |
4497 | |
4498 | Style.ColumnLimit = 41; |
4499 | verifyFormat("namespace A { namespace B { namespace C {\n" |
4500 | "}}} // namespace A::B::C", |
4501 | "namespace A { namespace B {\n" |
4502 | "namespace C {\n" |
4503 | "}} // namespace B::C\n" |
4504 | "} // namespace A", |
4505 | Style); |
4506 | |
4507 | Style.ColumnLimit = 40; |
4508 | verifyFormat("namespace aaaaaaaaaa {\n" |
4509 | "namespace bbbbbbbbbb {\n" |
4510 | "}} // namespace aaaaaaaaaa::bbbbbbbbbb", |
4511 | "namespace aaaaaaaaaa {\n" |
4512 | "namespace bbbbbbbbbb {\n" |
4513 | "} // namespace bbbbbbbbbb\n" |
4514 | "} // namespace aaaaaaaaaa", |
4515 | Style); |
4516 | |
4517 | verifyFormat("namespace aaaaaa { namespace bbbbbb {\n" |
4518 | "namespace cccccc {\n" |
4519 | "}}} // namespace aaaaaa::bbbbbb::cccccc", |
4520 | "namespace aaaaaa {\n" |
4521 | "namespace bbbbbb {\n" |
4522 | "namespace cccccc {\n" |
4523 | "} // namespace cccccc\n" |
4524 | "} // namespace bbbbbb\n" |
4525 | "} // namespace aaaaaa", |
4526 | Style); |
4527 | |
4528 | verifyFormat("namespace a { namespace b {\n" |
4529 | "namespace c {\n" |
4530 | "}}} // namespace a::b::c", |
4531 | Style); |
4532 | |
4533 | Style.ColumnLimit = 80; |
4534 | |
4535 | // Extra semicolon after 'inner' closing brace prevents merging |
4536 | verifyFormat("namespace out { namespace in {\n" |
4537 | "}; } // namespace out::in", |
4538 | "namespace out {\n" |
4539 | "namespace in {\n" |
4540 | "}; // namespace in\n" |
4541 | "} // namespace out", |
4542 | Style); |
4543 | |
4544 | // Extra semicolon after 'outer' closing brace is conserved |
4545 | verifyFormat("namespace out { namespace in {\n" |
4546 | "}}; // namespace out::in", |
4547 | "namespace out {\n" |
4548 | "namespace in {\n" |
4549 | "} // namespace in\n" |
4550 | "}; // namespace out", |
4551 | Style); |
4552 | |
4553 | Style.NamespaceIndentation = FormatStyle::NI_All; |
4554 | verifyFormat("namespace out { namespace in {\n" |
4555 | " int i;\n" |
4556 | "}} // namespace out::in", |
4557 | "namespace out {\n" |
4558 | "namespace in {\n" |
4559 | "int i;\n" |
4560 | "} // namespace in\n" |
4561 | "} // namespace out", |
4562 | Style); |
4563 | verifyFormat("namespace out { namespace mid {\n" |
4564 | " namespace in {\n" |
4565 | " int j;\n" |
4566 | " } // namespace in\n" |
4567 | " int k;\n" |
4568 | "}} // namespace out::mid", |
4569 | "namespace out { namespace mid {\n" |
4570 | "namespace in { int j; } // namespace in\n" |
4571 | "int k; }} // namespace out::mid", |
4572 | Style); |
4573 | |
4574 | verifyFormat("namespace A { namespace B { namespace C {\n" |
4575 | " int i;\n" |
4576 | "}}} // namespace A::B::C\n" |
4577 | "int main() {\n" |
4578 | " if (true)\n" |
4579 | " return 0;\n" |
4580 | "}", |
4581 | "namespace A { namespace B {\n" |
4582 | "namespace C {\n" |
4583 | " int i;\n" |
4584 | "}} // namespace B::C\n" |
4585 | "} // namespace A\n" |
4586 | "int main() {\n" |
4587 | " if (true)\n" |
4588 | " return 0;\n" |
4589 | "}", |
4590 | Style); |
4591 | |
4592 | verifyFormat("namespace A { namespace B { namespace C {\n" |
4593 | "#ifdef FOO\n" |
4594 | " int i;\n" |
4595 | "#endif\n" |
4596 | "}}} // namespace A::B::C\n" |
4597 | "int main() {\n" |
4598 | " if (true)\n" |
4599 | " return 0;\n" |
4600 | "}", |
4601 | "namespace A { namespace B {\n" |
4602 | "namespace C {\n" |
4603 | "#ifdef FOO\n" |
4604 | " int i;\n" |
4605 | "#endif\n" |
4606 | "}} // namespace B::C\n" |
4607 | "} // namespace A\n" |
4608 | "int main() {\n" |
4609 | " if (true)\n" |
4610 | " return 0;\n" |
4611 | "}", |
4612 | Style); |
4613 | |
4614 | Style.NamespaceIndentation = FormatStyle::NI_Inner; |
4615 | verifyFormat("namespace out { namespace in {\n" |
4616 | " int i;\n" |
4617 | "}} // namespace out::in", |
4618 | "namespace out {\n" |
4619 | "namespace in {\n" |
4620 | "int i;\n" |
4621 | "} // namespace in\n" |
4622 | "} // namespace out", |
4623 | Style); |
4624 | verifyFormat("namespace out { namespace mid { namespace in {\n" |
4625 | " int i;\n" |
4626 | "}}} // namespace out::mid::in", |
4627 | "namespace out {\n" |
4628 | "namespace mid {\n" |
4629 | "namespace in {\n" |
4630 | "int i;\n" |
4631 | "} // namespace in\n" |
4632 | "} // namespace mid\n" |
4633 | "} // namespace out", |
4634 | Style); |
4635 | |
4636 | Style.CompactNamespaces = true; |
4637 | Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; |
4638 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
4639 | Style.BraceWrapping.BeforeLambdaBody = true; |
4640 | verifyFormat("namespace out { namespace in {\n" |
4641 | "}} // namespace out::in", |
4642 | Style); |
4643 | verifyFormat("namespace out { namespace in {\n" |
4644 | "}} // namespace out::in", |
4645 | "namespace out {\n" |
4646 | "namespace in {\n" |
4647 | "} // namespace in\n" |
4648 | "} // namespace out", |
4649 | Style); |
4650 | } |
4651 | |
4652 | TEST_F(FormatTest, FormatsExternC) { |
4653 | verifyFormat("extern \"C\" {\nint a;"); |
4654 | verifyFormat("extern \"C\" {}"); |
4655 | verifyFormat("extern \"C\" {\n" |
4656 | "int foo();\n" |
4657 | "}"); |
4658 | verifyFormat("extern \"C\" int foo() {}"); |
4659 | verifyFormat("extern \"C\" int foo();"); |
4660 | verifyFormat("extern \"C\" int foo() {\n" |
4661 | " int i = 42;\n" |
4662 | " return i;\n" |
4663 | "}"); |
4664 | verifyFormat( |
4665 | "extern \"C\" char const *const\n" |
4666 | " OpenCL_source_OpenCLRunTime_test_attribute_opencl_unroll_hint;"); |
4667 | |
4668 | FormatStyle Style = getLLVMStyle(); |
4669 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
4670 | Style.BraceWrapping.AfterFunction = true; |
4671 | verifyFormat("extern \"C\" int foo() {}", Style); |
4672 | verifyFormat("extern \"C\" int foo();", Style); |
4673 | verifyFormat("extern \"C\" int foo()\n" |
4674 | "{\n" |
4675 | " int i = 42;\n" |
4676 | " return i;\n" |
4677 | "}", |
4678 | Style); |
4679 | |
4680 | Style.BraceWrapping.AfterExternBlock = true; |
4681 | Style.BraceWrapping.SplitEmptyRecord = false; |
4682 | verifyFormat("extern \"C\"\n" |
4683 | "{}", |
4684 | Style); |
4685 | verifyFormat("extern \"C\"\n" |
4686 | "{\n" |
4687 | " int foo();\n" |
4688 | "}", |
4689 | Style); |
4690 | } |
4691 | |
4692 | TEST_F(FormatTest, IndentExternBlockStyle) { |
4693 | FormatStyle Style = getLLVMStyle(); |
4694 | Style.IndentWidth = 2; |
4695 | |
4696 | Style.IndentExternBlock = FormatStyle::IEBS_Indent; |
4697 | verifyFormat("extern \"C\" { /*9*/\n" |
4698 | "}", |
4699 | Style); |
4700 | verifyFormat("extern \"C\" {\n" |
4701 | " int foo10();\n" |
4702 | "}", |
4703 | Style); |
4704 | |
4705 | Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; |
4706 | verifyFormat("extern \"C\" { /*11*/\n" |
4707 | "}", |
4708 | Style); |
4709 | verifyFormat("extern \"C\" {\n" |
4710 | "int foo12();\n" |
4711 | "}", |
4712 | Style); |
4713 | |
4714 | Style.BreakBeforeBraces = FormatStyle::BS_Allman; |
4715 | verifyFormat("extern \"C\"\n" |
4716 | "{\n" |
4717 | "int i;\n" |
4718 | "}", |
4719 | Style); |
4720 | |
4721 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
4722 | Style.BraceWrapping.AfterExternBlock = true; |
4723 | Style.IndentExternBlock = FormatStyle::IEBS_Indent; |
4724 | verifyFormat("extern \"C\"\n" |
4725 | "{ /*13*/\n" |
4726 | "}", |
4727 | Style); |
4728 | verifyFormat("extern \"C\"\n{\n" |
4729 | " int foo14();\n" |
4730 | "}", |
4731 | Style); |
4732 | |
4733 | Style.BraceWrapping.AfterExternBlock = false; |
4734 | Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; |
4735 | verifyFormat("extern \"C\" { /*15*/\n" |
4736 | "}", |
4737 | Style); |
4738 | verifyFormat("extern \"C\" {\n" |
4739 | "int foo16();\n" |
4740 | "}", |
4741 | Style); |
4742 | |
4743 | Style.BraceWrapping.AfterExternBlock = true; |
4744 | verifyFormat("extern \"C\"\n" |
4745 | "{ /*13*/\n" |
4746 | "}", |
4747 | Style); |
4748 | verifyFormat("extern \"C\"\n" |
4749 | "{\n" |
4750 | "int foo14();\n" |
4751 | "}", |
4752 | Style); |
4753 | |
4754 | Style.IndentExternBlock = FormatStyle::IEBS_Indent; |
4755 | verifyFormat("extern \"C\"\n" |
4756 | "{ /*13*/\n" |
4757 | "}", |
4758 | Style); |
4759 | verifyFormat("extern \"C\"\n" |
4760 | "{\n" |
4761 | " int foo14();\n" |
4762 | "}", |
4763 | Style); |
4764 | } |
4765 | |
4766 | TEST_F(FormatTest, FormatsInlineASM) { |
4767 | verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); |
4768 | verifyFormat("asm(\"nop\" ::: \"memory\");"); |
4769 | verifyFormat( |
4770 | "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" |
4771 | " \"cpuid\\n\\t\"\n" |
4772 | " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" |
4773 | " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" |
4774 | " : \"a\"(value));"); |
4775 | verifyFormat( |
4776 | "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" |
4777 | " __asm {\n" |
4778 | " mov edx,[that] // vtable in edx\n" |
4779 | " mov eax,methodIndex\n" |
4780 | " call [edx][eax*4] // stdcall\n" |
4781 | " }\n" |
4782 | "}", |
4783 | "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" |
4784 | " __asm {\n" |
4785 | " mov edx,[that] // vtable in edx\n" |
4786 | " mov eax,methodIndex\n" |
4787 | " call [edx][eax*4] // stdcall\n" |
4788 | " }\n" |
4789 | "}"); |
4790 | verifyNoChange("_asm {\n" |
4791 | " xor eax, eax;\n" |
4792 | " cpuid;\n" |
4793 | "}"); |
4794 | verifyFormat("void function() {\n" |
4795 | " // comment\n" |
4796 | " asm(\"\");\n" |
4797 | "}"); |
4798 | verifyFormat("__asm {\n" |
4799 | "}\n" |
4800 | "int i;", |
4801 | "__asm {\n" |
4802 | "}\n" |
4803 | "int i;"); |
4804 | |
4805 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 0); |
4806 | const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"}; |
4807 | const StringRef Code2{"asm(\"xyz\"\n" |
4808 | " : \"=a\"(a), \"=d\"(b)\n" |
4809 | " : \"a\"(data));"}; |
4810 | const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n" |
4811 | " : \"a\"(data));"}; |
4812 | |
4813 | Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline; |
4814 | verifyFormat(Code1, Style); |
4815 | verifyNoChange(Code2, Style); |
4816 | verifyNoChange(Code3, Style); |
4817 | |
4818 | Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always; |
4819 | verifyFormat(Code2, Code1, Style); |
4820 | verifyNoChange(Code2, Style); |
4821 | verifyFormat(Code2, Code3, Style); |
4822 | } |
4823 | |
4824 | TEST_F(FormatTest, FormatTryCatch) { |
4825 | verifyFormat("try {\n" |
4826 | " throw a * b;\n" |
4827 | "} catch (int a) {\n" |
4828 | " // Do nothing.\n" |
4829 | "} catch (...) {\n" |
4830 | " exit(42);\n" |
4831 | "}"); |
4832 | |
4833 | // Function-level try statements. |
4834 | verifyFormat("int f() try { return 4; } catch (...) {\n" |
4835 | " return 5;\n" |
4836 | "}"); |
4837 | verifyFormat("class A {\n" |
4838 | " int a;\n" |
4839 | " A() try : a(0) {\n" |
4840 | " } catch (...) {\n" |
4841 | " throw;\n" |
4842 | " }\n" |
4843 | "};"); |
4844 | verifyFormat("class A {\n" |
4845 | " int a;\n" |
4846 | " A() try : a(0), b{1} {\n" |
4847 | " } catch (...) {\n" |
4848 | " throw;\n" |
4849 | " }\n" |
4850 | "};"); |
4851 | verifyFormat("class A {\n" |
4852 | " int a;\n" |
4853 | " A() try : a(0), b{1}, c{2} {\n" |
4854 | " } catch (...) {\n" |
4855 | " throw;\n" |
4856 | " }\n" |
4857 | "};"); |
4858 | verifyFormat("class A {\n" |
4859 | " int a;\n" |
4860 | " A() try : a(0), b{1}, c{2} {\n" |
4861 | " { // New scope.\n" |
4862 | " }\n" |
4863 | " } catch (...) {\n" |
4864 | " throw;\n" |
4865 | " }\n" |
4866 | "};"); |
4867 | |
4868 | // Incomplete try-catch blocks. |
4869 | verifyIncompleteFormat("try {} catch ("); |
4870 | } |
4871 | |
4872 | TEST_F(FormatTest, FormatTryAsAVariable) { |
4873 | verifyFormat("int try;"); |
4874 | verifyFormat("int try, size;"); |
4875 | verifyFormat("try = foo();"); |
4876 | verifyFormat("if (try < size) {\n return true;\n}"); |
4877 | |
4878 | verifyFormat("int catch;"); |
4879 | verifyFormat("int catch, size;"); |
4880 | verifyFormat("catch = foo();"); |
4881 | verifyFormat("if (catch < size) {\n return true;\n}"); |
4882 | |
4883 | FormatStyle Style = getLLVMStyle(); |
4884 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
4885 | Style.BraceWrapping.AfterFunction = true; |
4886 | Style.BraceWrapping.BeforeCatch = true; |
4887 | verifyFormat("try {\n" |
4888 | " int bar = 1;\n" |
4889 | "}\n" |
4890 | "catch (...) {\n" |
4891 | " int bar = 1;\n" |
4892 | "}", |
4893 | Style); |
4894 | verifyFormat("#if NO_EX\n" |
4895 | "try\n" |
4896 | "#endif\n" |
4897 | "{\n" |
4898 | "}\n" |
4899 | "#if NO_EX\n" |
4900 | "catch (...) {\n" |
4901 | "}", |
4902 | Style); |
4903 | verifyFormat("try /* abc */ {\n" |
4904 | " int bar = 1;\n" |
4905 | "}\n" |
4906 | "catch (...) {\n" |
4907 | " int bar = 1;\n" |
4908 | "}", |
4909 | Style); |
4910 | verifyFormat("try\n" |
4911 | "// abc\n" |
4912 | "{\n" |
4913 | " int bar = 1;\n" |
4914 | "}\n" |
4915 | "catch (...) {\n" |
4916 | " int bar = 1;\n" |
4917 | "}", |
4918 | Style); |
4919 | } |
4920 | |
4921 | TEST_F(FormatTest, FormatSEHTryCatch) { |
4922 | verifyFormat("__try {\n" |
4923 | " int a = b * c;\n" |
4924 | "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" |
4925 | " // Do nothing.\n" |
4926 | "}"); |
4927 | |
4928 | verifyFormat("__try {\n" |
4929 | " int a = b * c;\n" |
4930 | "} __finally {\n" |
4931 | " // Do nothing.\n" |
4932 | "}"); |
4933 | |
4934 | verifyFormat("DEBUG({\n" |
4935 | " __try {\n" |
4936 | " } __finally {\n" |
4937 | " }\n" |
4938 | "});"); |
4939 | } |
4940 | |
4941 | TEST_F(FormatTest, IncompleteTryCatchBlocks) { |
4942 | verifyFormat("try {\n" |
4943 | " f();\n" |
4944 | "} catch {\n" |
4945 | " g();\n" |
4946 | "}"); |
4947 | verifyFormat("try {\n" |
4948 | " f();\n" |
4949 | "} catch (A a) MACRO(x) {\n" |
4950 | " g();\n" |
4951 | "} catch (B b) MACRO(x) {\n" |
4952 | " g();\n" |
4953 | "}"); |
4954 | } |
4955 | |
4956 | TEST_F(FormatTest, FormatTryCatchBraceStyles) { |
4957 | FormatStyle Style = getLLVMStyle(); |
4958 | for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, |
4959 | FormatStyle::BS_WebKit}) { |
4960 | Style.BreakBeforeBraces = BraceStyle; |
4961 | verifyFormat("try {\n" |
4962 | " // something\n" |
4963 | "} catch (...) {\n" |
4964 | " // something\n" |
4965 | "}", |
4966 | Style); |
4967 | } |
4968 | Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
4969 | verifyFormat("try {\n" |
4970 | " // something\n" |
4971 | "}\n" |
4972 | "catch (...) {\n" |
4973 | " // something\n" |
4974 | "}", |
4975 | Style); |
4976 | verifyFormat("__try {\n" |
4977 | " // something\n" |
4978 | "}\n" |
4979 | "__finally {\n" |
4980 | " // something\n" |
4981 | "}", |
4982 | Style); |
4983 | verifyFormat("@try {\n" |
4984 | " // something\n" |
4985 | "}\n" |
4986 | "@finally {\n" |
4987 | " // something\n" |
4988 | "}", |
4989 | Style); |
4990 | Style.BreakBeforeBraces = FormatStyle::BS_Allman; |
4991 | verifyFormat("try\n" |
4992 | "{\n" |
4993 | " // something\n" |
4994 | "}\n" |
4995 | "catch (...)\n" |
4996 | "{\n" |
4997 | " // something\n" |
4998 | "}", |
4999 | Style); |
5000 | Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; |
5001 | verifyFormat("try\n" |
5002 | " {\n" |
5003 | " // something white\n" |
5004 | " }\n" |
5005 | "catch (...)\n" |
5006 | " {\n" |
5007 | " // something white\n" |
5008 | " }", |
5009 | Style); |
5010 | Style.BreakBeforeBraces = FormatStyle::BS_GNU; |
5011 | verifyFormat("try\n" |
5012 | " {\n" |
5013 | " // something\n" |
5014 | " }\n" |
5015 | "catch (...)\n" |
5016 | " {\n" |
5017 | " // something\n" |
5018 | " }", |
5019 | Style); |
5020 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
5021 | Style.BraceWrapping.BeforeCatch = true; |
5022 | verifyFormat("try {\n" |
5023 | " // something\n" |
5024 | "}\n" |
5025 | "catch (...) {\n" |
5026 | " // something\n" |
5027 | "}", |
5028 | Style); |
5029 | } |
5030 | |
5031 | TEST_F(FormatTest, StaticInitializers) { |
5032 | verifyFormat("static SomeClass SC = {1, 'a'};"); |
5033 | |
5034 | verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" |
5035 | " 100000000, " |
5036 | "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); |
5037 | |
5038 | // Here, everything other than the "}" would fit on a line. |
5039 | verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" |
5040 | " 10000000000000000000000000};"); |
5041 | verifyFormat("S s = {a,\n" |
5042 | "\n" |
5043 | " b};", |
5044 | "S s = {\n" |
5045 | " a,\n" |
5046 | "\n" |
5047 | " b\n" |
5048 | "};"); |
5049 | |
5050 | // FIXME: This would fit into the column limit if we'd fit "{ {" on the first |
5051 | // line. However, the formatting looks a bit off and this probably doesn't |
5052 | // happen often in practice. |
5053 | verifyFormat("static int Variable[1] = {\n" |
5054 | " {1000000000000000000000000000000000000}};", |
5055 | getLLVMStyleWithColumns(40)); |
5056 | } |
5057 | |
5058 | TEST_F(FormatTest, DesignatedInitializers) { |
5059 | verifyFormat("const struct A a = {.a = 1, .b = 2};"); |
5060 | verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" |
5061 | " .bbbbbbbbbb = 2,\n" |
5062 | " .cccccccccc = 3,\n" |
5063 | " .dddddddddd = 4,\n" |
5064 | " .eeeeeeeeee = 5};"); |
5065 | verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" |
5066 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" |
5067 | " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" |
5068 | " .ccccccccccccccccccccccccccc = 3,\n" |
5069 | " .ddddddddddddddddddddddddddd = 4,\n" |
5070 | " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); |
5071 | |
5072 | verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); |
5073 | |
5074 | verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); |
5075 | verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" |
5076 | " [2] = bbbbbbbbbb,\n" |
5077 | " [3] = cccccccccc,\n" |
5078 | " [4] = dddddddddd,\n" |
5079 | " [5] = eeeeeeeeee};"); |
5080 | verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" |
5081 | " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
5082 | " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
5083 | " [3] = cccccccccccccccccccccccccccccccccccccc,\n" |
5084 | " [4] = dddddddddddddddddddddddddddddddddddddd,\n" |
5085 | " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); |
5086 | |
5087 | verifyFormat("for (const TestCase &test_case : {\n" |
5088 | " TestCase{\n" |
5089 | " .a = 1,\n" |
5090 | " .b = 1,\n" |
5091 | " },\n" |
5092 | " TestCase{\n" |
5093 | " .a = 2,\n" |
5094 | " .b = 2,\n" |
5095 | " },\n" |
5096 | " }) {\n" |
5097 | "}"); |
5098 | } |
5099 | |
5100 | TEST_F(FormatTest, BracedInitializerIndentWidth) { |
5101 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
5102 | Style.BinPackArguments = true; |
5103 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
5104 | Style.BracedInitializerIndentWidth = 6; |
5105 | |
5106 | // Non-initializing braces are unaffected by BracedInitializerIndentWidth. |
5107 | verifyFormat("enum class {\n" |
5108 | " One,\n" |
5109 | " Two,\n" |
5110 | "};", |
5111 | Style); |
5112 | verifyFormat("class Foo {\n" |
5113 | " Foo() {}\n" |
5114 | " void bar();\n" |
5115 | "};", |
5116 | Style); |
5117 | verifyFormat("void foo() {\n" |
5118 | " auto bar = baz;\n" |
5119 | " return baz;\n" |
5120 | "};", |
5121 | Style); |
5122 | verifyFormat("auto foo = [&] {\n" |
5123 | " auto bar = baz;\n" |
5124 | " return baz;\n" |
5125 | "};", |
5126 | Style); |
5127 | verifyFormat("{\n" |
5128 | " auto bar = baz;\n" |
5129 | " return baz;\n" |
5130 | "};", |
5131 | Style); |
5132 | // Non-brace initialization is unaffected by BracedInitializerIndentWidth. |
5133 | verifyFormat("SomeClass clazz(\n" |
5134 | " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n" |
5135 | " \"zzzzzzzzzzzzzzzzzz\");", |
5136 | Style); |
5137 | |
5138 | // The following types of initialization are all affected by |
5139 | // BracedInitializerIndentWidth. Aggregate initialization. |
5140 | verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" |
5141 | " 10000000, 20000000};", |
5142 | Style); |
5143 | verifyFormat("SomeStruct s{\n" |
5144 | " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n" |
5145 | " \"zzzzzzzzzzzzzzzz\"};", |
5146 | Style); |
5147 | // Designated initializers. |
5148 | verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" |
5149 | " [0] = 10000000, [1] = 20000000};", |
5150 | Style); |
5151 | verifyFormat("SomeStruct s{\n" |
5152 | " .foo = \"xxxxxxxxxxxxx\",\n" |
5153 | " .bar = \"yyyyyyyyyyyyy\",\n" |
5154 | " .baz = \"zzzzzzzzzzzzz\"};", |
5155 | Style); |
5156 | // List initialization. |
5157 | verifyFormat("SomeStruct s{\n" |
5158 | " \"xxxxxxxxxxxxx\",\n" |
5159 | " \"yyyyyyyyyyyyy\",\n" |
5160 | " \"zzzzzzzzzzzzz\",\n" |
5161 | "};", |
5162 | Style); |
5163 | verifyFormat("SomeStruct{\n" |
5164 | " \"xxxxxxxxxxxxx\",\n" |
5165 | " \"yyyyyyyyyyyyy\",\n" |
5166 | " \"zzzzzzzzzzzzz\",\n" |
5167 | "};", |
5168 | Style); |
5169 | verifyFormat("new SomeStruct{\n" |
5170 | " \"xxxxxxxxxxxxx\",\n" |
5171 | " \"yyyyyyyyyyyyy\",\n" |
5172 | " \"zzzzzzzzzzzzz\",\n" |
5173 | "};", |
5174 | Style); |
5175 | // Member initializer. |
5176 | verifyFormat("class SomeClass {\n" |
5177 | " SomeStruct s{\n" |
5178 | " \"xxxxxxxxxxxxx\",\n" |
5179 | " \"yyyyyyyyyyyyy\",\n" |
5180 | " \"zzzzzzzzzzzzz\",\n" |
5181 | " };\n" |
5182 | "};", |
5183 | Style); |
5184 | // Constructor member initializer. |
5185 | verifyFormat("SomeClass::SomeClass : strct{\n" |
5186 | " \"xxxxxxxxxxxxx\",\n" |
5187 | " \"yyyyyyyyyyyyy\",\n" |
5188 | " \"zzzzzzzzzzzzz\",\n" |
5189 | " } {}", |
5190 | Style); |
5191 | // Copy initialization. |
5192 | verifyFormat("SomeStruct s = SomeStruct{\n" |
5193 | " \"xxxxxxxxxxxxx\",\n" |
5194 | " \"yyyyyyyyyyyyy\",\n" |
5195 | " \"zzzzzzzzzzzzz\",\n" |
5196 | "};", |
5197 | Style); |
5198 | // Copy list initialization. |
5199 | verifyFormat("SomeStruct s = {\n" |
5200 | " \"xxxxxxxxxxxxx\",\n" |
5201 | " \"yyyyyyyyyyyyy\",\n" |
5202 | " \"zzzzzzzzzzzzz\",\n" |
5203 | "};", |
5204 | Style); |
5205 | // Assignment operand initialization. |
5206 | verifyFormat("s = {\n" |
5207 | " \"xxxxxxxxxxxxx\",\n" |
5208 | " \"yyyyyyyyyyyyy\",\n" |
5209 | " \"zzzzzzzzzzzzz\",\n" |
5210 | "};", |
5211 | Style); |
5212 | // Returned object initialization. |
5213 | verifyFormat("return {\n" |
5214 | " \"xxxxxxxxxxxxx\",\n" |
5215 | " \"yyyyyyyyyyyyy\",\n" |
5216 | " \"zzzzzzzzzzzzz\",\n" |
5217 | "};", |
5218 | Style); |
5219 | // Initializer list. |
5220 | verifyFormat("auto initializerList = {\n" |
5221 | " \"xxxxxxxxxxxxx\",\n" |
5222 | " \"yyyyyyyyyyyyy\",\n" |
5223 | " \"zzzzzzzzzzzzz\",\n" |
5224 | "};", |
5225 | Style); |
5226 | // Function parameter initialization. |
5227 | verifyFormat("func({\n" |
5228 | " \"xxxxxxxxxxxxx\",\n" |
5229 | " \"yyyyyyyyyyyyy\",\n" |
5230 | " \"zzzzzzzzzzzzz\",\n" |
5231 | "});", |
5232 | Style); |
5233 | // Nested init lists. |
5234 | verifyFormat("SomeStruct s = {\n" |
5235 | " {{init1, init2, init3, init4, init5},\n" |
5236 | " {init1, init2, init3, init4, init5}}};", |
5237 | Style); |
5238 | verifyFormat("SomeStruct s = {\n" |
5239 | " {{\n" |
5240 | " .init1 = 1,\n" |
5241 | " .init2 = 2,\n" |
5242 | " .init3 = 3,\n" |
5243 | " .init4 = 4,\n" |
5244 | " .init5 = 5,\n" |
5245 | " },\n" |
5246 | " {init1, init2, init3, init4, init5}}};", |
5247 | Style); |
5248 | verifyFormat("SomeArrayT a[3] = {\n" |
5249 | " {\n" |
5250 | " foo,\n" |
5251 | " bar,\n" |
5252 | " },\n" |
5253 | " {\n" |
5254 | " foo,\n" |
5255 | " bar,\n" |
5256 | " },\n" |
5257 | " SomeArrayT{},\n" |
5258 | "};", |
5259 | Style); |
5260 | verifyFormat("SomeArrayT a[3] = {\n" |
5261 | " {foo},\n" |
5262 | " {\n" |
5263 | " {\n" |
5264 | " init1,\n" |
5265 | " init2,\n" |
5266 | " init3,\n" |
5267 | " },\n" |
5268 | " {\n" |
5269 | " init1,\n" |
5270 | " init2,\n" |
5271 | " init3,\n" |
5272 | " },\n" |
5273 | " },\n" |
5274 | " {baz},\n" |
5275 | "};", |
5276 | Style); |
5277 | |
5278 | // Aligning after open braces unaffected by BracedInitializerIndentWidth. |
5279 | Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; |
5280 | verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n" |
5281 | " \"zzzzzzzzzzzzz\"};", |
5282 | Style); |
5283 | } |
5284 | |
5285 | TEST_F(FormatTest, NestedStaticInitializers) { |
5286 | verifyFormat("static A x = {{{}}};"); |
5287 | verifyFormat("static A x = {{{init1, init2, init3, init4},\n" |
5288 | " {init1, init2, init3, init4}}};", |
5289 | getLLVMStyleWithColumns(50)); |
5290 | |
5291 | verifyFormat("somes Status::global_reps[3] = {\n" |
5292 | " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" |
5293 | " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" |
5294 | " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", |
5295 | getLLVMStyleWithColumns(60)); |
5296 | verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" |
5297 | " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" |
5298 | " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" |
5299 | " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); |
5300 | verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" |
5301 | " {rect.fRight - rect.fLeft, rect.fBottom - " |
5302 | "rect.fTop}};"); |
5303 | |
5304 | verifyFormat( |
5305 | "SomeArrayOfSomeType a = {\n" |
5306 | " {{1, 2, 3},\n" |
5307 | " {1, 2, 3},\n" |
5308 | " {111111111111111111111111111111, 222222222222222222222222222222,\n" |
5309 | " 333333333333333333333333333333},\n" |
5310 | " {1, 2, 3},\n" |
5311 | " {1, 2, 3}}};"); |
5312 | verifyFormat( |
5313 | "SomeArrayOfSomeType a = {\n" |
5314 | " {{1, 2, 3}},\n" |
5315 | " {{1, 2, 3}},\n" |
5316 | " {{111111111111111111111111111111, 222222222222222222222222222222,\n" |
5317 | " 333333333333333333333333333333}},\n" |
5318 | " {{1, 2, 3}},\n" |
5319 | " {{1, 2, 3}}};"); |
5320 | |
5321 | verifyFormat("struct {\n" |
5322 | " unsigned bit;\n" |
5323 | " const char *const name;\n" |
5324 | "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" |
5325 | " {kOsWin, \"Windows\"},\n" |
5326 | " {kOsLinux, \"Linux\"},\n" |
5327 | " {kOsCrOS, \"Chrome OS\"}};"); |
5328 | verifyFormat("struct {\n" |
5329 | " unsigned bit;\n" |
5330 | " const char *const name;\n" |
5331 | "} kBitsToOs[] = {\n" |
5332 | " {kOsMac, \"Mac\"},\n" |
5333 | " {kOsWin, \"Windows\"},\n" |
5334 | " {kOsLinux, \"Linux\"},\n" |
5335 | " {kOsCrOS, \"Chrome OS\"},\n" |
5336 | "};"); |
5337 | } |
5338 | |
5339 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
5340 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
5341 | " \\\n" |
5342 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
5343 | } |
5344 | |
5345 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
5346 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
5347 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
5348 | |
5349 | // Do break defaulted and deleted functions. |
5350 | verifyFormat("virtual void ~Deeeeeeeestructor() =\n" |
5351 | " default;", |
5352 | getLLVMStyleWithColumns(40)); |
5353 | verifyFormat("virtual void ~Deeeeeeeestructor() =\n" |
5354 | " delete;", |
5355 | getLLVMStyleWithColumns(40)); |
5356 | } |
5357 | |
5358 | TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { |
5359 | verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", |
5360 | getLLVMStyleWithColumns(40)); |
5361 | verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", |
5362 | getLLVMStyleWithColumns(40)); |
5363 | verifyFormat("#define Q \\\n" |
5364 | " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" |
5365 | " \"aaaaaaaa.cpp\"", |
5366 | "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", |
5367 | getLLVMStyleWithColumns(40)); |
5368 | } |
5369 | |
5370 | TEST_F(FormatTest, UnderstandsLinePPDirective) { |
5371 | verifyFormat("# 123 \"A string literal\"", |
5372 | " # 123 \"A string literal\""); |
5373 | } |
5374 | |
5375 | TEST_F(FormatTest, LayoutUnknownPPDirective) { |
5376 | verifyFormat("#;"); |
5377 | verifyFormat("#\n;\n;\n;"); |
5378 | } |
5379 | |
5380 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
5381 | verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); |
5382 | verifyFormat("#define A B", "# \\\n define \\\n A \\\n B", |
5383 | getLLVMStyleWithColumns(12)); |
5384 | } |
5385 | |
5386 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
5387 | verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); |
5388 | verifyFormat("#define A B", "# \\\n define \\\n A \\\n B"); |
5389 | } |
5390 | |
5391 | TEST_F(FormatTest, DoesntRemoveUnknownTokens) { |
5392 | verifyFormat("#define A \\x20"); |
5393 | verifyFormat("#define A \\ x20"); |
5394 | verifyFormat("#define A \\ x20", "#define A \\ x20"); |
5395 | verifyFormat("#define A ''"); |
5396 | verifyFormat("#define A ''qqq"); |
5397 | verifyFormat("#define A `qqq"); |
5398 | verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); |
5399 | verifyFormat("const char *c = STRINGIFY(\n" |
5400 | "\\na : b);", |
5401 | "const char * c = STRINGIFY(\n" |
5402 | "\\na : b);"); |
5403 | |
5404 | verifyFormat("a\r\\"); |
5405 | verifyFormat("a\v\\"); |
5406 | verifyFormat("a\f\\"); |
5407 | } |
5408 | |
5409 | TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { |
5410 | FormatStyle style = getChromiumStyle(Language: FormatStyle::LK_Cpp); |
5411 | style.IndentWidth = 4; |
5412 | style.PPIndentWidth = 1; |
5413 | |
5414 | style.IndentPPDirectives = FormatStyle::PPDIS_None; |
5415 | verifyFormat("#ifdef __linux__\n" |
5416 | "void foo() {\n" |
5417 | " int x = 0;\n" |
5418 | "}\n" |
5419 | "#define FOO\n" |
5420 | "#endif\n" |
5421 | "void bar() {\n" |
5422 | " int y = 0;\n" |
5423 | "}", |
5424 | style); |
5425 | |
5426 | style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; |
5427 | verifyFormat("#ifdef __linux__\n" |
5428 | "void foo() {\n" |
5429 | " int x = 0;\n" |
5430 | "}\n" |
5431 | "# define FOO foo\n" |
5432 | "#endif\n" |
5433 | "void bar() {\n" |
5434 | " int y = 0;\n" |
5435 | "}", |
5436 | style); |
5437 | |
5438 | style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; |
5439 | verifyFormat("#ifdef __linux__\n" |
5440 | "void foo() {\n" |
5441 | " int x = 0;\n" |
5442 | "}\n" |
5443 | " #define FOO foo\n" |
5444 | "#endif\n" |
5445 | "void bar() {\n" |
5446 | " int y = 0;\n" |
5447 | "}", |
5448 | style); |
5449 | verifyFormat("#if 1\n" |
5450 | " // some comments\n" |
5451 | " // another\n" |
5452 | " #define foo 1\n" |
5453 | "// not a define comment\n" |
5454 | "void bar() {\n" |
5455 | " // comment\n" |
5456 | " int y = 0;\n" |
5457 | "}", |
5458 | "#if 1\n" |
5459 | "// some comments\n" |
5460 | "// another\n" |
5461 | "#define foo 1\n" |
5462 | "// not a define comment\n" |
5463 | "void bar() {\n" |
5464 | " // comment\n" |
5465 | " int y = 0;\n" |
5466 | "}", |
5467 | style); |
5468 | |
5469 | style.IndentPPDirectives = FormatStyle::PPDIS_None; |
5470 | verifyFormat("#ifdef foo\n" |
5471 | "#define bar() \\\n" |
5472 | " if (A) { \\\n" |
5473 | " B(); \\\n" |
5474 | " } \\\n" |
5475 | " C();\n" |
5476 | "#endif", |
5477 | style); |
5478 | verifyFormat("if (emacs) {\n" |
5479 | "#ifdef is\n" |
5480 | "#define lit \\\n" |
5481 | " if (af) { \\\n" |
5482 | " return duh(); \\\n" |
5483 | " }\n" |
5484 | "#endif\n" |
5485 | "}", |
5486 | style); |
5487 | verifyFormat("#if abc\n" |
5488 | "#ifdef foo\n" |
5489 | "#define bar() \\\n" |
5490 | " if (A) { \\\n" |
5491 | " if (B) { \\\n" |
5492 | " C(); \\\n" |
5493 | " } \\\n" |
5494 | " } \\\n" |
5495 | " D();\n" |
5496 | "#endif\n" |
5497 | "#endif", |
5498 | style); |
5499 | verifyFormat("#ifndef foo\n" |
5500 | "#define foo\n" |
5501 | "if (emacs) {\n" |
5502 | "#ifdef is\n" |
5503 | "#define lit \\\n" |
5504 | " if (af) { \\\n" |
5505 | " return duh(); \\\n" |
5506 | " }\n" |
5507 | "#endif\n" |
5508 | "}\n" |
5509 | "#endif", |
5510 | style); |
5511 | verifyFormat("#if 1\n" |
5512 | "#define X \\\n" |
5513 | " { \\\n" |
5514 | " x; \\\n" |
5515 | " x; \\\n" |
5516 | " }\n" |
5517 | "#endif", |
5518 | style); |
5519 | verifyFormat("#define X \\\n" |
5520 | " { \\\n" |
5521 | " x; \\\n" |
5522 | " x; \\\n" |
5523 | " }", |
5524 | style); |
5525 | |
5526 | style.PPIndentWidth = 2; |
5527 | verifyFormat("#ifdef foo\n" |
5528 | "#define bar() \\\n" |
5529 | " if (A) { \\\n" |
5530 | " B(); \\\n" |
5531 | " } \\\n" |
5532 | " C();\n" |
5533 | "#endif", |
5534 | style); |
5535 | style.IndentWidth = 8; |
5536 | verifyFormat("#ifdef foo\n" |
5537 | "#define bar() \\\n" |
5538 | " if (A) { \\\n" |
5539 | " B(); \\\n" |
5540 | " } \\\n" |
5541 | " C();\n" |
5542 | "#endif", |
5543 | style); |
5544 | |
5545 | style.IndentWidth = 1; |
5546 | style.PPIndentWidth = 4; |
5547 | verifyFormat("#if 1\n" |
5548 | "#define X \\\n" |
5549 | " { \\\n" |
5550 | " x; \\\n" |
5551 | " x; \\\n" |
5552 | " }\n" |
5553 | "#endif", |
5554 | style); |
5555 | verifyFormat("#define X \\\n" |
5556 | " { \\\n" |
5557 | " x; \\\n" |
5558 | " x; \\\n" |
5559 | " }", |
5560 | style); |
5561 | |
5562 | style.IndentWidth = 4; |
5563 | style.PPIndentWidth = 1; |
5564 | style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; |
5565 | verifyFormat("#ifdef foo\n" |
5566 | "# define bar() \\\n" |
5567 | " if (A) { \\\n" |
5568 | " B(); \\\n" |
5569 | " } \\\n" |
5570 | " C();\n" |
5571 | "#endif", |
5572 | style); |
5573 | verifyFormat("#if abc\n" |
5574 | "# ifdef foo\n" |
5575 | "# define bar() \\\n" |
5576 | " if (A) { \\\n" |
5577 | " if (B) { \\\n" |
5578 | " C(); \\\n" |
5579 | " } \\\n" |
5580 | " } \\\n" |
5581 | " D();\n" |
5582 | "# endif\n" |
5583 | "#endif", |
5584 | style); |
5585 | verifyFormat("#ifndef foo\n" |
5586 | "#define foo\n" |
5587 | "if (emacs) {\n" |
5588 | "#ifdef is\n" |
5589 | "# define lit \\\n" |
5590 | " if (af) { \\\n" |
5591 | " return duh(); \\\n" |
5592 | " }\n" |
5593 | "#endif\n" |
5594 | "}\n" |
5595 | "#endif", |
5596 | style); |
5597 | verifyFormat("#define X \\\n" |
5598 | " { \\\n" |
5599 | " x; \\\n" |
5600 | " x; \\\n" |
5601 | " }", |
5602 | style); |
5603 | |
5604 | style.PPIndentWidth = 2; |
5605 | style.IndentWidth = 8; |
5606 | verifyFormat("#ifdef foo\n" |
5607 | "# define bar() \\\n" |
5608 | " if (A) { \\\n" |
5609 | " B(); \\\n" |
5610 | " } \\\n" |
5611 | " C();\n" |
5612 | "#endif", |
5613 | style); |
5614 | |
5615 | style.PPIndentWidth = 4; |
5616 | style.IndentWidth = 1; |
5617 | verifyFormat("#define X \\\n" |
5618 | " { \\\n" |
5619 | " x; \\\n" |
5620 | " x; \\\n" |
5621 | " }", |
5622 | style); |
5623 | |
5624 | style.IndentWidth = 4; |
5625 | style.PPIndentWidth = 1; |
5626 | style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; |
5627 | verifyFormat("if (emacs) {\n" |
5628 | "#ifdef is\n" |
5629 | " #define lit \\\n" |
5630 | " if (af) { \\\n" |
5631 | " return duh(); \\\n" |
5632 | " }\n" |
5633 | "#endif\n" |
5634 | "}", |
5635 | style); |
5636 | verifyFormat("#if abc\n" |
5637 | " #ifdef foo\n" |
5638 | " #define bar() \\\n" |
5639 | " if (A) { \\\n" |
5640 | " B(); \\\n" |
5641 | " } \\\n" |
5642 | " C();\n" |
5643 | " #endif\n" |
5644 | "#endif", |
5645 | style); |
5646 | verifyFormat("#if 1\n" |
5647 | " #define X \\\n" |
5648 | " { \\\n" |
5649 | " x; \\\n" |
5650 | " x; \\\n" |
5651 | " }\n" |
5652 | "#endif", |
5653 | style); |
5654 | |
5655 | style.PPIndentWidth = 2; |
5656 | verifyFormat("#ifdef foo\n" |
5657 | " #define bar() \\\n" |
5658 | " if (A) { \\\n" |
5659 | " B(); \\\n" |
5660 | " } \\\n" |
5661 | " C();\n" |
5662 | "#endif", |
5663 | style); |
5664 | |
5665 | style.PPIndentWidth = 4; |
5666 | style.IndentWidth = 1; |
5667 | verifyFormat("#if 1\n" |
5668 | " #define X \\\n" |
5669 | " { \\\n" |
5670 | " x; \\\n" |
5671 | " x; \\\n" |
5672 | " }\n" |
5673 | "#endif", |
5674 | style); |
5675 | } |
5676 | |
5677 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
5678 | verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); |
5679 | verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); |
5680 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
5681 | // FIXME: We never break before the macro name. |
5682 | verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); |
5683 | |
5684 | verifyFormat("#define A A\n#define A A"); |
5685 | verifyFormat("#define A(X) A\n#define A A"); |
5686 | |
5687 | verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); |
5688 | verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); |
5689 | } |
5690 | |
5691 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
5692 | verifyFormat("// somecomment\n" |
5693 | "#include \"a.h\"\n" |
5694 | "#define A( \\\n" |
5695 | " A, B)\n" |
5696 | "#include \"b.h\"\n" |
5697 | "// somecomment", |
5698 | " // somecomment\n" |
5699 | " #include \"a.h\"\n" |
5700 | "#define A(A,\\\n" |
5701 | " B)\n" |
5702 | " #include \"b.h\"\n" |
5703 | " // somecomment", |
5704 | getLLVMStyleWithColumns(13)); |
5705 | } |
5706 | |
5707 | TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); } |
5708 | |
5709 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
5710 | verifyFormat("#define A \\\n" |
5711 | " c; \\\n" |
5712 | " e;\n" |
5713 | "f;", |
5714 | "#define A c; e;\n" |
5715 | "f;", |
5716 | getLLVMStyleWithColumns(14)); |
5717 | } |
5718 | |
5719 | TEST_F(FormatTest, LayoutRemainingTokens) { |
5720 | verifyFormat("{\n" |
5721 | "}"); |
5722 | } |
5723 | |
5724 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
5725 | verifyFormat("int x,\n" |
5726 | "#define A\n" |
5727 | " y;", |
5728 | "int x,\n#define A\ny;"); |
5729 | } |
5730 | |
5731 | TEST_F(FormatTest, HashInMacroDefinition) { |
5732 | verifyFormat("#define A(c) L#c"); |
5733 | verifyFormat("#define A(c) u#c"); |
5734 | verifyFormat("#define A(c) U#c"); |
5735 | verifyFormat("#define A(c) u8#c"); |
5736 | verifyFormat("#define A(c) LR#c"); |
5737 | verifyFormat("#define A(c) uR#c"); |
5738 | verifyFormat("#define A(c) UR#c"); |
5739 | verifyFormat("#define A(c) u8R#c"); |
5740 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
5741 | verifyFormat("#define A \\\n" |
5742 | " { \\\n" |
5743 | " f(#c); \\\n" |
5744 | " }", |
5745 | getLLVMStyleWithColumns(11)); |
5746 | |
5747 | verifyFormat("#define A(X) \\\n" |
5748 | " void function##X()", |
5749 | getLLVMStyleWithColumns(22)); |
5750 | |
5751 | verifyFormat("#define A(a, b, c) \\\n" |
5752 | " void a##b##c()", |
5753 | getLLVMStyleWithColumns(22)); |
5754 | |
5755 | verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); |
5756 | |
5757 | verifyFormat("{\n" |
5758 | " {\n" |
5759 | "#define GEN_ID(_x) char *_x{#_x}\n" |
5760 | " GEN_ID(one);\n" |
5761 | " }\n" |
5762 | "}"); |
5763 | } |
5764 | |
5765 | TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { |
5766 | verifyFormat("#define A (x)"); |
5767 | verifyFormat("#define A(x)"); |
5768 | |
5769 | FormatStyle Style = getLLVMStyle(); |
5770 | Style.SpaceBeforeParens = FormatStyle::SBPO_Never; |
5771 | verifyFormat("#define true ((foo)1)", Style); |
5772 | Style.SpaceBeforeParens = FormatStyle::SBPO_Always; |
5773 | verifyFormat("#define false((foo)0)", Style); |
5774 | } |
5775 | |
5776 | TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { |
5777 | verifyFormat("#define A b;", |
5778 | "#define A \\\n" |
5779 | " \\\n" |
5780 | " b;", |
5781 | getLLVMStyleWithColumns(25)); |
5782 | verifyNoChange("#define A \\\n" |
5783 | " \\\n" |
5784 | " a; \\\n" |
5785 | " b;", |
5786 | getLLVMStyleWithColumns(11)); |
5787 | verifyNoChange("#define A \\\n" |
5788 | " a; \\\n" |
5789 | " \\\n" |
5790 | " b;", |
5791 | getLLVMStyleWithColumns(11)); |
5792 | } |
5793 | |
5794 | TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { |
5795 | verifyIncompleteFormat("#define A :"); |
5796 | verifyFormat("#define SOMECASES \\\n" |
5797 | " case 1: \\\n" |
5798 | " case 2", |
5799 | getLLVMStyleWithColumns(20)); |
5800 | verifyFormat("#define MACRO(a) \\\n" |
5801 | " if (a) \\\n" |
5802 | " f(); \\\n" |
5803 | " else \\\n" |
5804 | " g()", |
5805 | getLLVMStyleWithColumns(18)); |
5806 | verifyFormat("#define A template <typename T>"); |
5807 | verifyIncompleteFormat("#define STR(x) #x\n" |
5808 | "f(STR(this_is_a_string_literal{));"); |
5809 | verifyFormat("#pragma omp threadprivate( \\\n" |
5810 | " y)), // expected-warning", |
5811 | getLLVMStyleWithColumns(28)); |
5812 | verifyFormat("#d, = };"); |
5813 | verifyFormat("#if \"a"); |
5814 | verifyIncompleteFormat("({\n" |
5815 | "#define b \\\n" |
5816 | " } \\\n" |
5817 | " a\n" |
5818 | "a", |
5819 | getLLVMStyleWithColumns(15)); |
5820 | verifyFormat("#define A \\\n" |
5821 | " { \\\n" |
5822 | " {\n" |
5823 | "#define B \\\n" |
5824 | " } \\\n" |
5825 | " }", |
5826 | getLLVMStyleWithColumns(15)); |
5827 | verifyNoCrash(Code: "#if a\na(\n#else\n#endif\n{a"); |
5828 | verifyNoCrash(Code: "a={0,1\n#if a\n#else\n;\n#endif\n}"); |
5829 | verifyNoCrash(Code: "#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); |
5830 | verifyNoCrash(Code: "#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); |
5831 | verifyNoCrash(Code: "#else\n" |
5832 | "#else\n" |
5833 | "#endif\n" |
5834 | "#endif"); |
5835 | verifyNoCrash(Code: "#else\n" |
5836 | "#if X\n" |
5837 | "#endif\n" |
5838 | "#endif"); |
5839 | verifyNoCrash(Code: "#else\n" |
5840 | "#endif\n" |
5841 | "#if X\n" |
5842 | "#endif"); |
5843 | verifyNoCrash(Code: "#if X\n" |
5844 | "#else\n" |
5845 | "#else\n" |
5846 | "#endif\n" |
5847 | "#endif"); |
5848 | verifyNoCrash(Code: "#if X\n" |
5849 | "#elif Y\n" |
5850 | "#elif Y\n" |
5851 | "#endif\n" |
5852 | "#endif"); |
5853 | verifyNoCrash(Code: "#endif\n" |
5854 | "#endif"); |
5855 | verifyNoCrash(Code: "#endif\n" |
5856 | "#else"); |
5857 | verifyNoCrash(Code: "#endif\n" |
5858 | "#elif Y"); |
5859 | } |
5860 | |
5861 | TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { |
5862 | verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. |
5863 | verifyFormat("class A : public QObject {\n" |
5864 | " Q_OBJECT\n" |
5865 | "\n" |
5866 | " A() {}\n" |
5867 | "};", |
5868 | "class A : public QObject {\n" |
5869 | " Q_OBJECT\n" |
5870 | "\n" |
5871 | " A() {\n}\n" |
5872 | "} ;"); |
5873 | verifyFormat("MACRO\n" |
5874 | "/*static*/ int i;", |
5875 | "MACRO\n" |
5876 | " /*static*/ int i;"); |
5877 | verifyFormat("SOME_MACRO\n" |
5878 | "namespace {\n" |
5879 | "void f();\n" |
5880 | "} // namespace", |
5881 | "SOME_MACRO\n" |
5882 | " namespace {\n" |
5883 | "void f( );\n" |
5884 | "} // namespace"); |
5885 | // Only if the identifier contains at least 5 characters. |
5886 | verifyFormat("HTTP f();", "HTTP\nf();"); |
5887 | verifyNoChange("MACRO\nf();"); |
5888 | // Only if everything is upper case. |
5889 | verifyFormat("class A : public QObject {\n" |
5890 | " Q_Object A() {}\n" |
5891 | "};", |
5892 | "class A : public QObject {\n" |
5893 | " Q_Object\n" |
5894 | " A() {\n}\n" |
5895 | "} ;"); |
5896 | |
5897 | // Only if the next line can actually start an unwrapped line. |
5898 | verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n" |
5899 | "<< SomeThing;"); |
5900 | |
5901 | verifyFormat("GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)(foo);", |
5902 | "GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)\n" |
5903 | "(foo);", |
5904 | getLLVMStyleWithColumns(60)); |
5905 | |
5906 | verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " |
5907 | "(n, buffers))", |
5908 | getChromiumStyle(FormatStyle::LK_Cpp)); |
5909 | |
5910 | // See PR41483 |
5911 | verifyNoChange("/**/ FOO(a)\n" |
5912 | "FOO(b)"); |
5913 | } |
5914 | |
5915 | TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { |
5916 | verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" |
5917 | "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" |
5918 | "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" |
5919 | "class X {};\n" |
5920 | "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" |
5921 | "int *createScopDetectionPass() { return 0; }", |
5922 | " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" |
5923 | " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" |
5924 | " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" |
5925 | " class X {};\n" |
5926 | " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" |
5927 | " int *createScopDetectionPass() { return 0; }"); |
5928 | // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as |
5929 | // braces, so that inner block is indented one level more. |
5930 | verifyFormat("int q() {\n" |
5931 | " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" |
5932 | " IPC_MESSAGE_HANDLER(xxx, qqq)\n" |
5933 | " IPC_END_MESSAGE_MAP()\n" |
5934 | "}", |
5935 | "int q() {\n" |
5936 | " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" |
5937 | " IPC_MESSAGE_HANDLER(xxx, qqq)\n" |
5938 | " IPC_END_MESSAGE_MAP()\n" |
5939 | "}"); |
5940 | |
5941 | // Same inside macros. |
5942 | verifyFormat("#define LIST(L) \\\n" |
5943 | " L(A) \\\n" |
5944 | " L(B) \\\n" |
5945 | " L(C)", |
5946 | "#define LIST(L) \\\n" |
5947 | " L(A) \\\n" |
5948 | " L(B) \\\n" |
5949 | " L(C)", |
5950 | getGoogleStyle()); |
5951 | |
5952 | // These must not be recognized as macros. |
5953 | verifyFormat("int q() {\n" |
5954 | " f(x);\n" |
5955 | " f(x) {}\n" |
5956 | " f(x)->g();\n" |
5957 | " f(x)->*g();\n" |
5958 | " f(x).g();\n" |
5959 | " f(x) = x;\n" |
5960 | " f(x) += x;\n" |
5961 | " f(x) -= x;\n" |
5962 | " f(x) *= x;\n" |
5963 | " f(x) /= x;\n" |
5964 | " f(x) %= x;\n" |
5965 | " f(x) &= x;\n" |
5966 | " f(x) |= x;\n" |
5967 | " f(x) ^= x;\n" |
5968 | " f(x) >>= x;\n" |
5969 | " f(x) <<= x;\n" |
5970 | " f(x)[y].z();\n" |
5971 | " LOG(INFO) << x;\n" |
5972 | " ifstream(x) >> x;\n" |
5973 | "}", |
5974 | "int q() {\n" |
5975 | " f(x)\n;\n" |
5976 | " f(x)\n {}\n" |
5977 | " f(x)\n->g();\n" |
5978 | " f(x)\n->*g();\n" |
5979 | " f(x)\n.g();\n" |
5980 | " f(x)\n = x;\n" |
5981 | " f(x)\n += x;\n" |
5982 | " f(x)\n -= x;\n" |
5983 | " f(x)\n *= x;\n" |
5984 | " f(x)\n /= x;\n" |
5985 | " f(x)\n %= x;\n" |
5986 | " f(x)\n &= x;\n" |
5987 | " f(x)\n |= x;\n" |
5988 | " f(x)\n ^= x;\n" |
5989 | " f(x)\n >>= x;\n" |
5990 | " f(x)\n <<= x;\n" |
5991 | " f(x)\n[y].z();\n" |
5992 | " LOG(INFO)\n << x;\n" |
5993 | " ifstream(x)\n >> x;\n" |
5994 | "}"); |
5995 | verifyFormat("int q() {\n" |
5996 | " F(x)\n" |
5997 | " if (1) {\n" |
5998 | " }\n" |
5999 | " F(x)\n" |
6000 | " while (1) {\n" |
6001 | " }\n" |
6002 | " F(x)\n" |
6003 | " G(x);\n" |
6004 | " F(x)\n" |
6005 | " try {\n" |
6006 | " Q();\n" |
6007 | " } catch (...) {\n" |
6008 | " }\n" |
6009 | "}", |
6010 | "int q() {\n" |
6011 | "F(x)\n" |
6012 | "if (1) {}\n" |
6013 | "F(x)\n" |
6014 | "while (1) {}\n" |
6015 | "F(x)\n" |
6016 | "G(x);\n" |
6017 | "F(x)\n" |
6018 | "try { Q(); } catch (...) {}\n" |
6019 | "}"); |
6020 | verifyFormat("class A {\n" |
6021 | " A() : t(0) {}\n" |
6022 | " A(int i) noexcept() : {}\n" |
6023 | " A(X x)\n"// FIXME: function-level try blocks are broken. |
6024 | " try : t(0) {\n" |
6025 | " } catch (...) {\n" |
6026 | " }\n" |
6027 | "};", |
6028 | "class A {\n" |
6029 | " A()\n : t(0) {}\n" |
6030 | " A(int i)\n noexcept() : {}\n" |
6031 | " A(X x)\n" |
6032 | " try : t(0) {} catch (...) {}\n" |
6033 | "};"); |
6034 | FormatStyle Style = getLLVMStyle(); |
6035 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
6036 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
6037 | Style.BraceWrapping.AfterFunction = true; |
6038 | verifyFormat("void f()\n" |
6039 | "try\n" |
6040 | "{\n" |
6041 | "}", |
6042 | "void f() try {\n" |
6043 | "}", |
6044 | Style); |
6045 | verifyFormat("class SomeClass {\n" |
6046 | "public:\n" |
6047 | " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" |
6048 | "};", |
6049 | "class SomeClass {\n" |
6050 | "public:\n" |
6051 | " SomeClass()\n" |
6052 | " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" |
6053 | "};"); |
6054 | verifyFormat("class SomeClass {\n" |
6055 | "public:\n" |
6056 | " SomeClass()\n" |
6057 | " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" |
6058 | "};", |
6059 | "class SomeClass {\n" |
6060 | "public:\n" |
6061 | " SomeClass()\n" |
6062 | " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" |
6063 | "};", |
6064 | getLLVMStyleWithColumns(40)); |
6065 | |
6066 | verifyFormat("MACRO(>)"); |
6067 | |
6068 | // Some macros contain an implicit semicolon. |
6069 | Style = getLLVMStyle(); |
6070 | Style.StatementMacros.push_back(x: "FOO"); |
6071 | verifyFormat("FOO(a) int b = 0;"); |
6072 | verifyFormat("FOO(a)\n" |
6073 | "int b = 0;", |
6074 | Style); |
6075 | verifyFormat("FOO(a);\n" |
6076 | "int b = 0;", |
6077 | Style); |
6078 | verifyFormat("FOO(argc, argv, \"4.0.2\")\n" |
6079 | "int b = 0;", |
6080 | Style); |
6081 | verifyFormat("FOO()\n" |
6082 | "int b = 0;", |
6083 | Style); |
6084 | verifyFormat("FOO\n" |
6085 | "int b = 0;", |
6086 | Style); |
6087 | verifyFormat("void f() {\n" |
6088 | " FOO(a)\n" |
6089 | " return a;\n" |
6090 | "}", |
6091 | Style); |
6092 | verifyFormat("FOO(a)\n" |
6093 | "FOO(b)", |
6094 | Style); |
6095 | verifyFormat("int a = 0;\n" |
6096 | "FOO(b)\n" |
6097 | "int c = 0;", |
6098 | Style); |
6099 | verifyFormat("int a = 0;\n" |
6100 | "int x = FOO(a)\n" |
6101 | "int b = 0;", |
6102 | Style); |
6103 | verifyFormat("void foo(int a) { FOO(a) }\n" |
6104 | "uint32_t bar() {}", |
6105 | Style); |
6106 | } |
6107 | |
6108 | TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { |
6109 | FormatStyle ZeroColumn = getLLVMStyleWithColumns(ColumnLimit: 0); |
6110 | |
6111 | verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", |
6112 | ZeroColumn); |
6113 | } |
6114 | |
6115 | TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { |
6116 | verifyFormat("#define A \\\n" |
6117 | " f({ \\\n" |
6118 | " g(); \\\n" |
6119 | " });", |
6120 | getLLVMStyleWithColumns(11)); |
6121 | } |
6122 | |
6123 | TEST_F(FormatTest, IndentPreprocessorDirectives) { |
6124 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
6125 | Style.IndentPPDirectives = FormatStyle::PPDIS_None; |
6126 | verifyFormat("#ifdef _WIN32\n" |
6127 | "#define A 0\n" |
6128 | "#ifdef VAR2\n" |
6129 | "#define B 1\n" |
6130 | "#include <someheader.h>\n" |
6131 | "#define MACRO \\\n" |
6132 | " some_very_long_func_aaaaaaaaaa();\n" |
6133 | "#endif\n" |
6134 | "#else\n" |
6135 | "#define A 1\n" |
6136 | "#endif", |
6137 | Style); |
6138 | Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; |
6139 | verifyFormat("#if 1\n" |
6140 | "# define __STR(x) #x\n" |
6141 | "#endif", |
6142 | Style); |
6143 | verifyFormat("#ifdef _WIN32\n" |
6144 | "# define A 0\n" |
6145 | "# ifdef VAR2\n" |
6146 | "# define B 1\n" |
6147 | "# include <someheader.h>\n" |
6148 | "# define MACRO \\\n" |
6149 | " some_very_long_func_aaaaaaaaaa();\n" |
6150 | "# endif\n" |
6151 | "#else\n" |
6152 | "# define A 1\n" |
6153 | "#endif", |
6154 | Style); |
6155 | verifyFormat("#if A\n" |
6156 | "# define MACRO \\\n" |
6157 | " void a(int x) { \\\n" |
6158 | " b(); \\\n" |
6159 | " c(); \\\n" |
6160 | " d(); \\\n" |
6161 | " e(); \\\n" |
6162 | " f(); \\\n" |
6163 | " }\n" |
6164 | "#endif", |
6165 | Style); |
6166 | // Comments before include guard. |
6167 | verifyFormat("// file comment\n" |
6168 | "// file comment\n" |
6169 | "#ifndef HEADER_H\n" |
6170 | "#define HEADER_H\n" |
6171 | "code();\n" |
6172 | "#endif", |
6173 | Style); |
6174 | // Test with include guards. |
6175 | verifyFormat("#ifndef HEADER_H\n" |
6176 | "#define HEADER_H\n" |
6177 | "code();\n" |
6178 | "#endif", |
6179 | Style); |
6180 | // Include guards must have a #define with the same variable immediately |
6181 | // after #ifndef. |
6182 | verifyFormat("#ifndef NOT_GUARD\n" |
6183 | "# define FOO\n" |
6184 | "code();\n" |
6185 | "#endif", |
6186 | Style); |
6187 | |
6188 | // Include guards must cover the entire file. |
6189 | verifyFormat("code();\n" |
6190 | "code();\n" |
6191 | "#ifndef NOT_GUARD\n" |
6192 | "# define NOT_GUARD\n" |
6193 | "code();\n" |
6194 | "#endif", |
6195 | Style); |
6196 | verifyFormat("#ifndef NOT_GUARD\n" |
6197 | "# define NOT_GUARD\n" |
6198 | "code();\n" |
6199 | "#endif\n" |
6200 | "code();", |
6201 | Style); |
6202 | // Test with trailing blank lines. |
6203 | verifyFormat("#ifndef HEADER_H\n" |
6204 | "#define HEADER_H\n" |
6205 | "code();\n" |
6206 | "#endif", |
6207 | Style); |
6208 | // Include guards don't have #else. |
6209 | verifyFormat("#ifndef NOT_GUARD\n" |
6210 | "# define NOT_GUARD\n" |
6211 | "code();\n" |
6212 | "#else\n" |
6213 | "#endif", |
6214 | Style); |
6215 | verifyFormat("#ifndef NOT_GUARD\n" |
6216 | "# define NOT_GUARD\n" |
6217 | "code();\n" |
6218 | "#elif FOO\n" |
6219 | "#endif", |
6220 | Style); |
6221 | // Non-identifier #define after potential include guard. |
6222 | verifyFormat("#ifndef FOO\n" |
6223 | "# define 1\n" |
6224 | "#endif", |
6225 | Style); |
6226 | // #if closes past last non-preprocessor line. |
6227 | verifyFormat("#ifndef FOO\n" |
6228 | "#define FOO\n" |
6229 | "#if 1\n" |
6230 | "int i;\n" |
6231 | "# define A 0\n" |
6232 | "#endif\n" |
6233 | "#endif", |
6234 | Style); |
6235 | // Don't crash if there is an #elif directive without a condition. |
6236 | verifyFormat("#if 1\n" |
6237 | "int x;\n" |
6238 | "#elif\n" |
6239 | "int y;\n" |
6240 | "#else\n" |
6241 | "int z;\n" |
6242 | "#endif", |
6243 | Style); |
6244 | // FIXME: This doesn't handle the case where there's code between the |
6245 | // #ifndef and #define but all other conditions hold. This is because when |
6246 | // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the |
6247 | // previous code line yet, so we can't detect it. |
6248 | verifyFormat("#ifndef NOT_GUARD\n" |
6249 | "code();\n" |
6250 | "#define NOT_GUARD\n" |
6251 | "code();\n" |
6252 | "#endif", |
6253 | "#ifndef NOT_GUARD\n" |
6254 | "code();\n" |
6255 | "# define NOT_GUARD\n" |
6256 | "code();\n" |
6257 | "#endif", |
6258 | Style); |
6259 | // FIXME: This doesn't handle cases where legitimate preprocessor lines may |
6260 | // be outside an include guard. Examples are #pragma once and |
6261 | // #pragma GCC diagnostic, or anything else that does not change the meaning |
6262 | // of the file if it's included multiple times. |
6263 | verifyFormat("#ifdef WIN32\n" |
6264 | "# pragma once\n" |
6265 | "#endif\n" |
6266 | "#ifndef HEADER_H\n" |
6267 | "# define HEADER_H\n" |
6268 | "code();\n" |
6269 | "#endif", |
6270 | "#ifdef WIN32\n" |
6271 | "# pragma once\n" |
6272 | "#endif\n" |
6273 | "#ifndef HEADER_H\n" |
6274 | "#define HEADER_H\n" |
6275 | "code();\n" |
6276 | "#endif", |
6277 | Style); |
6278 | // FIXME: This does not detect when there is a single non-preprocessor line |
6279 | // in front of an include-guard-like structure where other conditions hold |
6280 | // because ScopedLineState hides the line. |
6281 | verifyFormat("code();\n" |
6282 | "#ifndef HEADER_H\n" |
6283 | "#define HEADER_H\n" |
6284 | "code();\n" |
6285 | "#endif", |
6286 | "code();\n" |
6287 | "#ifndef HEADER_H\n" |
6288 | "# define HEADER_H\n" |
6289 | "code();\n" |
6290 | "#endif", |
6291 | Style); |
6292 | // Keep comments aligned with #, otherwise indent comments normally. These |
6293 | // tests cannot use verifyFormat because messUp manipulates leading |
6294 | // whitespace. |
6295 | { |
6296 | const char *Expected = "" |
6297 | "void f() {\n" |
6298 | "#if 1\n" |
6299 | "// Preprocessor aligned.\n" |
6300 | "# define A 0\n" |
6301 | " // Code. Separated by blank line.\n" |
6302 | "\n" |
6303 | "# define B 0\n" |
6304 | " // Code. Not aligned with #\n" |
6305 | "# define C 0\n" |
6306 | "#endif"; |
6307 | const char *ToFormat = "" |
6308 | "void f() {\n" |
6309 | "#if 1\n" |
6310 | "// Preprocessor aligned.\n" |
6311 | "# define A 0\n" |
6312 | "// Code. Separated by blank line.\n" |
6313 | "\n" |
6314 | "# define B 0\n" |
6315 | " // Code. Not aligned with #\n" |
6316 | "# define C 0\n" |
6317 | "#endif"; |
6318 | verifyFormat(Expected, ToFormat, Style); |
6319 | verifyNoChange(Expected, Style); |
6320 | } |
6321 | // Keep block quotes aligned. |
6322 | { |
6323 | const char *Expected = "" |
6324 | "void f() {\n" |
6325 | "#if 1\n" |
6326 | "/* Preprocessor aligned. */\n" |
6327 | "# define A 0\n" |
6328 | " /* Code. Separated by blank line. */\n" |
6329 | "\n" |
6330 | "# define B 0\n" |
6331 | " /* Code. Not aligned with # */\n" |
6332 | "# define C 0\n" |
6333 | "#endif"; |
6334 | const char *ToFormat = "" |
6335 | "void f() {\n" |
6336 | "#if 1\n" |
6337 | "/* Preprocessor aligned. */\n" |
6338 | "# define A 0\n" |
6339 | "/* Code. Separated by blank line. */\n" |
6340 | "\n" |
6341 | "# define B 0\n" |
6342 | " /* Code. Not aligned with # */\n" |
6343 | "# define C 0\n" |
6344 | "#endif"; |
6345 | verifyFormat(Expected, ToFormat, Style); |
6346 | verifyNoChange(Expected, Style); |
6347 | } |
6348 | // Keep comments aligned with un-indented directives. |
6349 | { |
6350 | const char *Expected = "" |
6351 | "void f() {\n" |
6352 | "// Preprocessor aligned.\n" |
6353 | "#define A 0\n" |
6354 | " // Code. Separated by blank line.\n" |
6355 | "\n" |
6356 | "#define B 0\n" |
6357 | " // Code. Not aligned with #\n" |
6358 | "#define C 0\n"; |
6359 | const char *ToFormat = "" |
6360 | "void f() {\n" |
6361 | "// Preprocessor aligned.\n" |
6362 | "#define A 0\n" |
6363 | "// Code. Separated by blank line.\n" |
6364 | "\n" |
6365 | "#define B 0\n" |
6366 | " // Code. Not aligned with #\n" |
6367 | "#define C 0\n"; |
6368 | verifyFormat(Expected, ToFormat, Style); |
6369 | verifyNoChange(Expected, Style); |
6370 | } |
6371 | // Test AfterHash with tabs. |
6372 | { |
6373 | FormatStyle Tabbed = Style; |
6374 | Tabbed.UseTab = FormatStyle::UT_Always; |
6375 | Tabbed.IndentWidth = 8; |
6376 | Tabbed.TabWidth = 8; |
6377 | verifyFormat("#ifdef _WIN32\n" |
6378 | "#\tdefine A 0\n" |
6379 | "#\tifdef VAR2\n" |
6380 | "#\t\tdefine B 1\n" |
6381 | "#\t\tinclude <someheader.h>\n" |
6382 | "#\t\tdefine MACRO \\\n" |
6383 | "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" |
6384 | "#\tendif\n" |
6385 | "#else\n" |
6386 | "#\tdefine A 1\n" |
6387 | "#endif", |
6388 | Tabbed); |
6389 | } |
6390 | |
6391 | // Regression test: Multiline-macro inside include guards. |
6392 | verifyFormat("#ifndef HEADER_H\n" |
6393 | "#define HEADER_H\n" |
6394 | "#define A() \\\n" |
6395 | " int i; \\\n" |
6396 | " int j;\n" |
6397 | "#endif // HEADER_H", |
6398 | getLLVMStyleWithColumns(20)); |
6399 | |
6400 | Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; |
6401 | // Basic before hash indent tests |
6402 | verifyFormat("#ifdef _WIN32\n" |
6403 | " #define A 0\n" |
6404 | " #ifdef VAR2\n" |
6405 | " #define B 1\n" |
6406 | " #include <someheader.h>\n" |
6407 | " #define MACRO \\\n" |
6408 | " some_very_long_func_aaaaaaaaaa();\n" |
6409 | " #endif\n" |
6410 | "#else\n" |
6411 | " #define A 1\n" |
6412 | "#endif", |
6413 | Style); |
6414 | verifyFormat("#if A\n" |
6415 | " #define MACRO \\\n" |
6416 | " void a(int x) { \\\n" |
6417 | " b(); \\\n" |
6418 | " c(); \\\n" |
6419 | " d(); \\\n" |
6420 | " e(); \\\n" |
6421 | " f(); \\\n" |
6422 | " }\n" |
6423 | "#endif", |
6424 | Style); |
6425 | // Keep comments aligned with indented directives. These |
6426 | // tests cannot use verifyFormat because messUp manipulates leading |
6427 | // whitespace. |
6428 | { |
6429 | const char *Expected = "void f() {\n" |
6430 | "// Aligned to preprocessor.\n" |
6431 | "#if 1\n" |
6432 | " // Aligned to code.\n" |
6433 | " int a;\n" |
6434 | " #if 1\n" |
6435 | " // Aligned to preprocessor.\n" |
6436 | " #define A 0\n" |
6437 | " // Aligned to code.\n" |
6438 | " int b;\n" |
6439 | " #endif\n" |
6440 | "#endif\n" |
6441 | "}"; |
6442 | const char *ToFormat = "void f() {\n" |
6443 | "// Aligned to preprocessor.\n" |
6444 | "#if 1\n" |
6445 | "// Aligned to code.\n" |
6446 | "int a;\n" |
6447 | "#if 1\n" |
6448 | "// Aligned to preprocessor.\n" |
6449 | "#define A 0\n" |
6450 | "// Aligned to code.\n" |
6451 | "int b;\n" |
6452 | "#endif\n" |
6453 | "#endif\n" |
6454 | "}"; |
6455 | verifyFormat(Expected, ToFormat, Style); |
6456 | verifyNoChange(Expected, Style); |
6457 | } |
6458 | { |
6459 | const char *Expected = "void f() {\n" |
6460 | "/* Aligned to preprocessor. */\n" |
6461 | "#if 1\n" |
6462 | " /* Aligned to code. */\n" |
6463 | " int a;\n" |
6464 | " #if 1\n" |
6465 | " /* Aligned to preprocessor. */\n" |
6466 | " #define A 0\n" |
6467 | " /* Aligned to code. */\n" |
6468 | " int b;\n" |
6469 | " #endif\n" |
6470 | "#endif\n" |
6471 | "}"; |
6472 | const char *ToFormat = "void f() {\n" |
6473 | "/* Aligned to preprocessor. */\n" |
6474 | "#if 1\n" |
6475 | "/* Aligned to code. */\n" |
6476 | "int a;\n" |
6477 | "#if 1\n" |
6478 | "/* Aligned to preprocessor. */\n" |
6479 | "#define A 0\n" |
6480 | "/* Aligned to code. */\n" |
6481 | "int b;\n" |
6482 | "#endif\n" |
6483 | "#endif\n" |
6484 | "}"; |
6485 | verifyFormat(Expected, ToFormat, Style); |
6486 | verifyNoChange(Expected, Style); |
6487 | } |
6488 | |
6489 | // Test single comment before preprocessor |
6490 | verifyFormat("// Comment\n" |
6491 | "\n" |
6492 | "#if 1\n" |
6493 | "#endif", |
6494 | Style); |
6495 | |
6496 | verifyFormat("#ifndef ABCDE\n" |
6497 | " #define ABCDE 0\n" |
6498 | "#endif\n" |
6499 | "\n" |
6500 | "#define FGHIJK", |
6501 | "#ifndef ABCDE\n" |
6502 | "#define ABCDE 0\n" |
6503 | "#endif\n" |
6504 | "\n" |
6505 | "#define FGHIJK", |
6506 | Style); |
6507 | } |
6508 | |
6509 | TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) { |
6510 | FormatStyle Style = getLLVMStyle(); |
6511 | Style.AlignConsecutiveAssignments.Enabled = true; |
6512 | Style.AlignConsecutiveDeclarations.Enabled = true; |
6513 | |
6514 | // Test with just #if blocks. |
6515 | verifyFormat("void f1() {\n" |
6516 | "#if 1\n" |
6517 | " int foo = 1;\n" |
6518 | " int foobar = 2;\n" |
6519 | "#endif\n" |
6520 | "}\n" |
6521 | "#if 1\n" |
6522 | "int baz = 3;\n" |
6523 | "#endif\n" |
6524 | "void f2() {\n" |
6525 | "#if 1\n" |
6526 | " char *foobarbaz = \"foobarbaz\";\n" |
6527 | " int quux = 4;\n" |
6528 | "}", |
6529 | Style); |
6530 | |
6531 | // Test with just #else blocks. |
6532 | verifyFormat("void f1() {\n" |
6533 | "#if 1\n" |
6534 | "#else\n" |
6535 | " int foo = 1;\n" |
6536 | " int foobar = 2;\n" |
6537 | "#endif\n" |
6538 | "}\n" |
6539 | "#if 1\n" |
6540 | "#else\n" |
6541 | "int baz = 3;\n" |
6542 | "#endif\n" |
6543 | "void f2() {\n" |
6544 | "#if 1\n" |
6545 | "#else\n" |
6546 | " char *foobarbaz = \"foobarbaz\";\n" |
6547 | " int quux = 4;\n" |
6548 | "}", |
6549 | Style); |
6550 | verifyFormat("auto foo = [] { return; };\n" |
6551 | "#if FOO\n" |
6552 | "#else\n" |
6553 | "count = bar;\n" |
6554 | "mbid = bid;\n" |
6555 | "#endif", |
6556 | Style); |
6557 | |
6558 | // Test with a mix of #if and #else blocks. |
6559 | verifyFormat("void f1() {\n" |
6560 | "#if 1\n" |
6561 | "#else\n" |
6562 | " int foo = 1;\n" |
6563 | " int foobar = 2;\n" |
6564 | "#endif\n" |
6565 | "}\n" |
6566 | "#if 1\n" |
6567 | "int baz = 3;\n" |
6568 | "#endif\n" |
6569 | "void f2() {\n" |
6570 | "#if 1\n" |
6571 | "#else\n" |
6572 | " // prevent alignment with #else in f1\n" |
6573 | " char *foobarbaz = \"foobarbaz\";\n" |
6574 | " int quux = 4;\n" |
6575 | "}", |
6576 | Style); |
6577 | |
6578 | // Test with nested #if and #else blocks. |
6579 | verifyFormat("void f1() {\n" |
6580 | "#if 1\n" |
6581 | "#else\n" |
6582 | "#if 2\n" |
6583 | "#else\n" |
6584 | " int foo = 1;\n" |
6585 | " int foobar = 2;\n" |
6586 | "#endif\n" |
6587 | "#endif\n" |
6588 | "}\n" |
6589 | "#if 1\n" |
6590 | "#else\n" |
6591 | "#if 2\n" |
6592 | "int baz = 3;\n" |
6593 | "#endif\n" |
6594 | "#endif\n" |
6595 | "void f2() {\n" |
6596 | "#if 1\n" |
6597 | "#if 2\n" |
6598 | "#else\n" |
6599 | " // prevent alignment with #else in f1\n" |
6600 | " char *foobarbaz = \"foobarbaz\";\n" |
6601 | " int quux = 4;\n" |
6602 | "#endif\n" |
6603 | "#endif\n" |
6604 | "}", |
6605 | Style); |
6606 | |
6607 | verifyFormat("#if FOO\n" |
6608 | "int a = 1;\n" |
6609 | "#else\n" |
6610 | "int ab = 2;\n" |
6611 | "#endif\n" |
6612 | "#ifdef BAR\n" |
6613 | "int abc = 3;\n" |
6614 | "#elifdef BAZ\n" |
6615 | "int abcd = 4;\n" |
6616 | "#endif", |
6617 | Style); |
6618 | |
6619 | verifyFormat("void f() {\n" |
6620 | " if (foo) {\n" |
6621 | "#if FOO\n" |
6622 | " int a = 1;\n" |
6623 | "#else\n" |
6624 | " bool a = true;\n" |
6625 | "#endif\n" |
6626 | " int abc = 3;\n" |
6627 | "#ifndef BAR\n" |
6628 | " int abcd = 4;\n" |
6629 | "#elif BAZ\n" |
6630 | " bool abcd = true;\n" |
6631 | "#endif\n" |
6632 | " }\n" |
6633 | "}", |
6634 | Style); |
6635 | |
6636 | verifyFormat("void f() {\n" |
6637 | "#if FOO\n" |
6638 | " a = 1;\n" |
6639 | "#else\n" |
6640 | " ab = 2;\n" |
6641 | "#endif\n" |
6642 | "}\n" |
6643 | "void g() {\n" |
6644 | "#if BAR\n" |
6645 | " abc = 3;\n" |
6646 | "#elifndef BAZ\n" |
6647 | " abcd = 4;\n" |
6648 | "#endif\n" |
6649 | "}", |
6650 | Style); |
6651 | } |
6652 | |
6653 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
6654 | verifyFormat("{\n" |
6655 | " {\n" |
6656 | " a #c;\n" |
6657 | " }\n" |
6658 | "}"); |
6659 | } |
6660 | |
6661 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
6662 | verifyFormat("#define A \\\n { \\\n {\nint i;", |
6663 | "#define A { {\nint i;", getLLVMStyleWithColumns(11)); |
6664 | verifyFormat("#define A \\\n } \\\n }\nint i;", |
6665 | "#define A } }\nint i;", getLLVMStyleWithColumns(11)); |
6666 | } |
6667 | |
6668 | TEST_F(FormatTest, EscapedNewlines) { |
6669 | FormatStyle Narrow = getLLVMStyleWithColumns(ColumnLimit: 11); |
6670 | verifyFormat("#define A \\\n int i; \\\n int j;", |
6671 | "#define A \\\nint i;\\\n int j;", Narrow); |
6672 | verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;"); |
6673 | verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();"); |
6674 | verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */"); |
6675 | verifyNoChange("<a\n\\\\\n>"); |
6676 | |
6677 | FormatStyle AlignLeft = getLLVMStyle(); |
6678 | AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
6679 | verifyFormat("#define MACRO(x) \\\n" |
6680 | "private: \\\n" |
6681 | " int x(int a);", |
6682 | AlignLeft); |
6683 | |
6684 | // CRLF line endings |
6685 | verifyFormat("#define A \\\r\n int i; \\\r\n int j;", |
6686 | "#define A \\\r\nint i;\\\r\n int j;", Narrow); |
6687 | verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;"); |
6688 | verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();"); |
6689 | verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */"); |
6690 | verifyNoChange("<a\r\n\\\\\r\n>"); |
6691 | verifyFormat("#define MACRO(x) \\\r\n" |
6692 | "private: \\\r\n" |
6693 | " int x(int a);", |
6694 | AlignLeft); |
6695 | |
6696 | constexpr StringRef Code{"#define A \\\n" |
6697 | " int a123; \\\n" |
6698 | " int a; \\\n" |
6699 | " int a1234;"}; |
6700 | verifyFormat(Code, AlignLeft); |
6701 | |
6702 | constexpr StringRef Code2{"#define A \\\n" |
6703 | " int a123; \\\n" |
6704 | " int a; \\\n" |
6705 | " int a1234;"}; |
6706 | auto LastLine = getLLVMStyle(); |
6707 | LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine; |
6708 | verifyFormat(Code2, LastLine); |
6709 | |
6710 | LastLine.ColumnLimit = 13; |
6711 | verifyFormat(Code, LastLine); |
6712 | |
6713 | LastLine.ColumnLimit = 0; |
6714 | verifyFormat(Code2, LastLine); |
6715 | |
6716 | FormatStyle DontAlign = getLLVMStyle(); |
6717 | DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; |
6718 | DontAlign.MaxEmptyLinesToKeep = 3; |
6719 | // FIXME: can't use verifyFormat here because the newline before |
6720 | // "public:" is not inserted the first time it's reformatted |
6721 | verifyNoChange("#define A \\\n" |
6722 | " class Foo { \\\n" |
6723 | " void bar(); \\\n" |
6724 | "\\\n" |
6725 | "\\\n" |
6726 | "\\\n" |
6727 | " public: \\\n" |
6728 | " void baz(); \\\n" |
6729 | " };", |
6730 | DontAlign); |
6731 | } |
6732 | |
6733 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
6734 | verifyFormat("#define A \\\n" |
6735 | " int v( \\\n" |
6736 | " a); \\\n" |
6737 | " int i;", |
6738 | getLLVMStyleWithColumns(11)); |
6739 | } |
6740 | |
6741 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
6742 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
6743 | " \\\n" |
6744 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
6745 | "\n" |
6746 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
6747 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);", |
6748 | " #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
6749 | "\\\n" |
6750 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
6751 | " \n" |
6752 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
6753 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);"); |
6754 | } |
6755 | |
6756 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
6757 | verifyFormat("int\n" |
6758 | "#define A\n" |
6759 | " a;", |
6760 | "int\n#define A\na;"); |
6761 | verifyFormat("functionCallTo(\n" |
6762 | " someOtherFunction(\n" |
6763 | " withSomeParameters, whichInSequence,\n" |
6764 | " areLongerThanALine(andAnotherCall,\n" |
6765 | "#define A B\n" |
6766 | " withMoreParamters,\n" |
6767 | " whichStronglyInfluenceTheLayout),\n" |
6768 | " andMoreParameters),\n" |
6769 | " trailing);", |
6770 | getLLVMStyleWithColumns(69)); |
6771 | verifyFormat("Foo::Foo()\n" |
6772 | "#ifdef BAR\n" |
6773 | " : baz(0)\n" |
6774 | "#endif\n" |
6775 | "{\n" |
6776 | "}"); |
6777 | verifyFormat("void f() {\n" |
6778 | " if (true)\n" |
6779 | "#ifdef A\n" |
6780 | " f(42);\n" |
6781 | " x();\n" |
6782 | "#else\n" |
6783 | " g();\n" |
6784 | " x();\n" |
6785 | "#endif\n" |
6786 | "}"); |
6787 | verifyFormat("void f(param1, param2,\n" |
6788 | " param3,\n" |
6789 | "#ifdef A\n" |
6790 | " param4(param5,\n" |
6791 | "#ifdef A1\n" |
6792 | " param6,\n" |
6793 | "#ifdef A2\n" |
6794 | " param7),\n" |
6795 | "#else\n" |
6796 | " param8),\n" |
6797 | " param9,\n" |
6798 | "#endif\n" |
6799 | " param10,\n" |
6800 | "#endif\n" |
6801 | " param11)\n" |
6802 | "#else\n" |
6803 | " param12)\n" |
6804 | "#endif\n" |
6805 | "{\n" |
6806 | " x();\n" |
6807 | "}", |
6808 | getLLVMStyleWithColumns(28)); |
6809 | verifyFormat("#if 1\n" |
6810 | "int i;"); |
6811 | verifyFormat("#if 1\n" |
6812 | "#endif\n" |
6813 | "#if 1\n" |
6814 | "#else\n" |
6815 | "#endif"); |
6816 | verifyFormat("DEBUG({\n" |
6817 | " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
6818 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" |
6819 | "});\n" |
6820 | "#if a\n" |
6821 | "#else\n" |
6822 | "#endif"); |
6823 | |
6824 | verifyIncompleteFormat("void f(\n" |
6825 | "#if A\n" |
6826 | ");\n" |
6827 | "#else\n" |
6828 | "#endif"); |
6829 | |
6830 | // Verify that indentation is correct when there is an `#if 0` with an |
6831 | // `#else`. |
6832 | verifyFormat("#if 0\n" |
6833 | "{\n" |
6834 | "#else\n" |
6835 | "{\n" |
6836 | "#endif\n" |
6837 | " x;\n" |
6838 | "}"); |
6839 | |
6840 | verifyFormat("#if 0\n" |
6841 | "#endif\n" |
6842 | "#if X\n" |
6843 | "int something_fairly_long; // Align here please\n" |
6844 | "#endif // Should be aligned"); |
6845 | |
6846 | verifyFormat("#if 0\n" |
6847 | "#endif\n" |
6848 | "#if X\n" |
6849 | "#else // Align\n" |
6850 | ";\n" |
6851 | "#endif // Align"); |
6852 | |
6853 | verifyFormat("void SomeFunction(int param1,\n" |
6854 | " template <\n" |
6855 | "#ifdef A\n" |
6856 | "#if 0\n" |
6857 | "#endif\n" |
6858 | " MyType<Some>>\n" |
6859 | "#else\n" |
6860 | " Type1, Type2>\n" |
6861 | "#endif\n" |
6862 | " param2,\n" |
6863 | " param3) {\n" |
6864 | " f();\n" |
6865 | "}"); |
6866 | |
6867 | verifyFormat("#ifdef __cplusplus\n" |
6868 | "extern \"C\"\n" |
6869 | "#endif\n" |
6870 | " void f();"); |
6871 | } |
6872 | |
6873 | TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { |
6874 | verifyFormat("#endif\n" |
6875 | "#if B"); |
6876 | } |
6877 | |
6878 | TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { |
6879 | FormatStyle SingleLine = getLLVMStyle(); |
6880 | SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; |
6881 | verifyFormat("#if 0\n" |
6882 | "#elif 1\n" |
6883 | "#endif\n" |
6884 | "void foo() {\n" |
6885 | " if (test) foo2();\n" |
6886 | "}", |
6887 | SingleLine); |
6888 | } |
6889 | |
6890 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
6891 | verifyFormat("functionCall({ int i; });"); |
6892 | verifyFormat("functionCall({\n" |
6893 | " int i;\n" |
6894 | " int j;\n" |
6895 | "});"); |
6896 | verifyFormat("functionCall(\n" |
6897 | " {\n" |
6898 | " int i;\n" |
6899 | " int j;\n" |
6900 | " },\n" |
6901 | " aaaa, bbbb, cccc);"); |
6902 | verifyFormat("functionA(functionB({\n" |
6903 | " int i;\n" |
6904 | " int j;\n" |
6905 | " }),\n" |
6906 | " aaaa, bbbb, cccc);"); |
6907 | verifyFormat("functionCall(\n" |
6908 | " {\n" |
6909 | " int i;\n" |
6910 | " int j;\n" |
6911 | " },\n" |
6912 | " aaaa, bbbb, // comment\n" |
6913 | " cccc);"); |
6914 | verifyFormat("functionA(functionB({\n" |
6915 | " int i;\n" |
6916 | " int j;\n" |
6917 | " }),\n" |
6918 | " aaaa, bbbb, // comment\n" |
6919 | " cccc);"); |
6920 | verifyFormat("functionCall(aaaa, bbbb, { int i; });"); |
6921 | verifyFormat("functionCall(aaaa, bbbb, {\n" |
6922 | " int i;\n" |
6923 | " int j;\n" |
6924 | "});"); |
6925 | verifyFormat( |
6926 | "Aaa(\n"// FIXME: There shouldn't be a linebreak here. |
6927 | " {\n" |
6928 | " int i; // break\n" |
6929 | " },\n" |
6930 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
6931 | " ccccccccccccccccc));"); |
6932 | verifyFormat("DEBUG({\n" |
6933 | " if (a)\n" |
6934 | " f();\n" |
6935 | "});"); |
6936 | } |
6937 | |
6938 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
6939 | verifyFormat("SOME_MACRO { int i; }\n" |
6940 | "int i;", |
6941 | " SOME_MACRO {int i;} int i;"); |
6942 | } |
6943 | |
6944 | TEST_F(FormatTest, LayoutNestedBlocks) { |
6945 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
6946 | " struct s {\n" |
6947 | " int i;\n" |
6948 | " };\n" |
6949 | " s kBitsToOs[] = {{10}};\n" |
6950 | " for (int i = 0; i < 10; ++i)\n" |
6951 | " return;\n" |
6952 | "}"); |
6953 | verifyFormat("call(parameter, {\n" |
6954 | " something();\n" |
6955 | " // Comment using all columns.\n" |
6956 | " somethingelse();\n" |
6957 | "});", |
6958 | getLLVMStyleWithColumns(40)); |
6959 | verifyFormat("DEBUG( //\n" |
6960 | " { f(); }, a);"); |
6961 | verifyFormat("DEBUG( //\n" |
6962 | " {\n" |
6963 | " f(); //\n" |
6964 | " },\n" |
6965 | " a);"); |
6966 | |
6967 | verifyFormat("call(parameter, {\n" |
6968 | " something();\n" |
6969 | " // Comment too\n" |
6970 | " // looooooooooong.\n" |
6971 | " somethingElse();\n" |
6972 | "});", |
6973 | "call(parameter, {\n" |
6974 | " something();\n" |
6975 | " // Comment too looooooooooong.\n" |
6976 | " somethingElse();\n" |
6977 | "});", |
6978 | getLLVMStyleWithColumns(29)); |
6979 | verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });"); |
6980 | verifyFormat("DEBUG({ // comment\n" |
6981 | " int i;\n" |
6982 | "});", |
6983 | "DEBUG({ // comment\n" |
6984 | "int i;\n" |
6985 | "});"); |
6986 | verifyFormat("DEBUG({\n" |
6987 | " int i;\n" |
6988 | "\n" |
6989 | " // comment\n" |
6990 | " int j;\n" |
6991 | "});", |
6992 | "DEBUG({\n" |
6993 | " int i;\n" |
6994 | "\n" |
6995 | " // comment\n" |
6996 | " int j;\n" |
6997 | "});"); |
6998 | |
6999 | verifyFormat("DEBUG({\n" |
7000 | " if (a)\n" |
7001 | " return;\n" |
7002 | "});"); |
7003 | verifyGoogleFormat("DEBUG({\n" |
7004 | " if (a) return;\n" |
7005 | "});"); |
7006 | FormatStyle Style = getGoogleStyle(); |
7007 | Style.ColumnLimit = 45; |
7008 | verifyFormat("Debug(\n" |
7009 | " aaaaa,\n" |
7010 | " {\n" |
7011 | " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" |
7012 | " },\n" |
7013 | " a);", |
7014 | Style); |
7015 | |
7016 | verifyFormat("SomeFunction({MACRO({ return output; }), b});"); |
7017 | |
7018 | verifyNoCrash(Code: "^{v^{a}}"); |
7019 | } |
7020 | |
7021 | TEST_F(FormatTest, FormatNestedBlocksInMacros) { |
7022 | verifyFormat("#define MACRO() \\\n" |
7023 | " Debug(aaa, /* force line break */ \\\n" |
7024 | " { \\\n" |
7025 | " int i; \\\n" |
7026 | " int j; \\\n" |
7027 | " })", |
7028 | "#define MACRO() Debug(aaa, /* force line break */ \\\n" |
7029 | " { int i; int j; })", |
7030 | getGoogleStyle()); |
7031 | |
7032 | verifyFormat("#define A \\\n" |
7033 | " [] { \\\n" |
7034 | " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" |
7035 | " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" |
7036 | " }", |
7037 | "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" |
7038 | "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", |
7039 | getGoogleStyle()); |
7040 | } |
7041 | |
7042 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
7043 | verifyFormat("enum E {};"); |
7044 | verifyFormat("enum E {}"); |
7045 | FormatStyle Style = getLLVMStyle(); |
7046 | Style.SpaceInEmptyBlock = true; |
7047 | verifyFormat("void f() { }", "void f() {}", Style); |
7048 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; |
7049 | verifyFormat("{ }", Style); |
7050 | verifyFormat("while (true) { }", "while (true) {}", Style); |
7051 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
7052 | Style.BraceWrapping.BeforeElse = false; |
7053 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
7054 | verifyFormat("if (a)\n" |
7055 | "{\n" |
7056 | "} else if (b)\n" |
7057 | "{\n" |
7058 | "} else\n" |
7059 | "{ }", |
7060 | Style); |
7061 | Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; |
7062 | verifyFormat("if (a) {\n" |
7063 | "} else if (b) {\n" |
7064 | "} else {\n" |
7065 | "}", |
7066 | Style); |
7067 | Style.BraceWrapping.BeforeElse = true; |
7068 | verifyFormat("if (a) { }\n" |
7069 | "else if (b) { }\n" |
7070 | "else { }", |
7071 | Style); |
7072 | |
7073 | Style = getLLVMStyle(Language: FormatStyle::LK_CSharp); |
7074 | Style.SpaceInEmptyBlock = true; |
7075 | verifyFormat("Event += () => { };", Style); |
7076 | } |
7077 | |
7078 | TEST_F(FormatTest, FormatBeginBlockEndMacros) { |
7079 | FormatStyle Style = getLLVMStyle(); |
7080 | Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; |
7081 | Style.MacroBlockEnd = "^[A-Z_]+_END$"; |
7082 | verifyFormat("FOO_BEGIN\n" |
7083 | " FOO_ENTRY\n" |
7084 | "FOO_END", |
7085 | Style); |
7086 | verifyFormat("FOO_BEGIN\n" |
7087 | " NESTED_FOO_BEGIN\n" |
7088 | " NESTED_FOO_ENTRY\n" |
7089 | " NESTED_FOO_END\n" |
7090 | "FOO_END", |
7091 | Style); |
7092 | verifyFormat("FOO_BEGIN(Foo, Bar)\n" |
7093 | " int x;\n" |
7094 | " x = 1;\n" |
7095 | "FOO_END(Baz)", |
7096 | Style); |
7097 | |
7098 | Style.RemoveBracesLLVM = true; |
7099 | verifyNoCrash(Code: "for (;;)\n" |
7100 | " FOO_BEGIN\n" |
7101 | " foo();\n" |
7102 | " FOO_END", |
7103 | Style); |
7104 | } |
7105 | |
7106 | //===----------------------------------------------------------------------===// |
7107 | // Line break tests. |
7108 | //===----------------------------------------------------------------------===// |
7109 | |
7110 | TEST_F(FormatTest, PreventConfusingIndents) { |
7111 | verifyFormat( |
7112 | "void f() {\n" |
7113 | " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
7114 | " parameter, parameter, parameter)),\n" |
7115 | " SecondLongCall(parameter));\n" |
7116 | "}"); |
7117 | verifyFormat( |
7118 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7119 | " aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7120 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
7121 | " aaaaaaaaaaaaaaaaaaaaaaaa);"); |
7122 | verifyFormat( |
7123 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7124 | " [aaaaaaaaaaaaaaaaaaaaaaaa\n" |
7125 | " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" |
7126 | " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); |
7127 | verifyFormat( |
7128 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" |
7129 | " aaaaaaaaaaaaaaaaaaaaaaaa<\n" |
7130 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" |
7131 | " aaaaaaaaaaaaaaaaaaaaaaaa>;"); |
7132 | verifyFormat("int a = bbbb && ccc &&\n" |
7133 | " fffff(\n" |
7134 | "#define A Just forcing a new line\n" |
7135 | " ddd);"); |
7136 | } |
7137 | |
7138 | TEST_F(FormatTest, LineBreakingInBinaryExpressions) { |
7139 | verifyFormat( |
7140 | "bool aaaaaaa =\n" |
7141 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" |
7142 | " bbbbbbbb();"); |
7143 | verifyFormat( |
7144 | "bool aaaaaaa =\n" |
7145 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" |
7146 | " bbbbbbbb();"); |
7147 | |
7148 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" |
7149 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" |
7150 | " ccccccccc == ddddddddddd;"); |
7151 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" |
7152 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" |
7153 | " ccccccccc == ddddddddddd;"); |
7154 | verifyFormat( |
7155 | "bool aaaaaaaaaaaaaaaaaaaaa =\n" |
7156 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" |
7157 | " ccccccccc == ddddddddddd;"); |
7158 | |
7159 | verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" |
7160 | " aaaaaa) &&\n" |
7161 | " bbbbbb && cccccc;"); |
7162 | verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" |
7163 | " aaaaaa) >>\n" |
7164 | " bbbbbb;"); |
7165 | verifyFormat("aa = Whitespaces.addUntouchableComment(\n" |
7166 | " SourceMgr.getSpellingColumnNumber(\n" |
7167 | " TheLine.Last->FormatTok.Tok.getLocation()) -\n" |
7168 | " 1);"); |
7169 | |
7170 | verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
7171 | " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" |
7172 | " cccccc) {\n}"); |
7173 | verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
7174 | " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" |
7175 | " cccccc) {\n}"); |
7176 | verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
7177 | " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" |
7178 | " cccccc) {\n}"); |
7179 | verifyFormat("b = a &&\n" |
7180 | " // Comment\n" |
7181 | " b.c && d;"); |
7182 | |
7183 | // If the LHS of a comparison is not a binary expression itself, the |
7184 | // additional linebreak confuses many people. |
7185 | verifyFormat( |
7186 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7187 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" |
7188 | "}"); |
7189 | verifyFormat( |
7190 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7191 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" |
7192 | "}"); |
7193 | verifyFormat( |
7194 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" |
7195 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" |
7196 | "}"); |
7197 | verifyFormat( |
7198 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7199 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" |
7200 | "}"); |
7201 | // Even explicit parentheses stress the precedence enough to make the |
7202 | // additional break unnecessary. |
7203 | verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7204 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" |
7205 | "}"); |
7206 | // This cases is borderline, but with the indentation it is still readable. |
7207 | verifyFormat( |
7208 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7209 | " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7210 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
7211 | "}", |
7212 | getLLVMStyleWithColumns(75)); |
7213 | |
7214 | // If the LHS is a binary expression, we should still use the additional break |
7215 | // as otherwise the formatting hides the operator precedence. |
7216 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7217 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
7218 | " 5) {\n" |
7219 | "}"); |
7220 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7221 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" |
7222 | " 5) {\n" |
7223 | "}"); |
7224 | |
7225 | FormatStyle OnePerLine = getLLVMStyle(); |
7226 | OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
7227 | verifyFormat( |
7228 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
7229 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
7230 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", |
7231 | OnePerLine); |
7232 | |
7233 | verifyFormat("int i = someFunction(aaaaaaa, 0)\n" |
7234 | " .aaa(aaaaaaaaaaaaa) *\n" |
7235 | " aaaaaaa +\n" |
7236 | " aaaaaaa;", |
7237 | getLLVMStyleWithColumns(40)); |
7238 | } |
7239 | |
7240 | TEST_F(FormatTest, ExpressionIndentation) { |
7241 | verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7242 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7243 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
7244 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
7245 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" |
7246 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" |
7247 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
7248 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" |
7249 | " ccccccccccccccccccccccccccccccccccccccccc;"); |
7250 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
7251 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7252 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
7253 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); |
7254 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7255 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
7256 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
7257 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); |
7258 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
7259 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
7260 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7261 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); |
7262 | verifyFormat("if () {\n" |
7263 | "} else if (aaaaa && bbbbb > // break\n" |
7264 | " ccccc) {\n" |
7265 | "}"); |
7266 | verifyFormat("if () {\n" |
7267 | "} else if constexpr (aaaaa && bbbbb > // break\n" |
7268 | " ccccc) {\n" |
7269 | "}"); |
7270 | verifyFormat("if () {\n" |
7271 | "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" |
7272 | " ccccc) {\n" |
7273 | "}"); |
7274 | verifyFormat("if () {\n" |
7275 | "} else if (aaaaa &&\n" |
7276 | " bbbbb > // break\n" |
7277 | " ccccc &&\n" |
7278 | " ddddd) {\n" |
7279 | "}"); |
7280 | |
7281 | // Presence of a trailing comment used to change indentation of b. |
7282 | verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" |
7283 | " b;\n" |
7284 | "return aaaaaaaaaaaaaaaaaaa +\n" |
7285 | " b; //", |
7286 | getLLVMStyleWithColumns(30)); |
7287 | } |
7288 | |
7289 | TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { |
7290 | // Not sure what the best system is here. Like this, the LHS can be found |
7291 | // immediately above an operator (everything with the same or a higher |
7292 | // indent). The RHS is aligned right of the operator and so compasses |
7293 | // everything until something with the same indent as the operator is found. |
7294 | // FIXME: Is this a good system? |
7295 | FormatStyle Style = getLLVMStyle(); |
7296 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
7297 | verifyFormat( |
7298 | "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7299 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7300 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7301 | " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7302 | " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7303 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7304 | " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7305 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7306 | " > ccccccccccccccccccccccccccccccccccccccccc;", |
7307 | Style); |
7308 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7309 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7310 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7311 | " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", |
7312 | Style); |
7313 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7314 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7315 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7316 | " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", |
7317 | Style); |
7318 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7319 | " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7320 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7321 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", |
7322 | Style); |
7323 | verifyFormat("if () {\n" |
7324 | "} else if (aaaaa\n" |
7325 | " && bbbbb // break\n" |
7326 | " > ccccc) {\n" |
7327 | "}", |
7328 | Style); |
7329 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7330 | " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", |
7331 | Style); |
7332 | verifyFormat("return (a)\n" |
7333 | " // comment\n" |
7334 | " + b;", |
7335 | Style); |
7336 | verifyFormat( |
7337 | "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7338 | " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7339 | " + cc;", |
7340 | Style); |
7341 | |
7342 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7343 | " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
7344 | Style); |
7345 | |
7346 | // Forced by comments. |
7347 | verifyFormat( |
7348 | "unsigned ContentSize =\n" |
7349 | " sizeof(int16_t) // DWARF ARange version number\n" |
7350 | " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" |
7351 | " + sizeof(int8_t) // Pointer Size (in bytes)\n" |
7352 | " + sizeof(int8_t); // Segment Size (in bytes)"); |
7353 | |
7354 | verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" |
7355 | " == boost::fusion::at_c<1>(iiii).second;", |
7356 | Style); |
7357 | |
7358 | Style.ColumnLimit = 60; |
7359 | verifyFormat("zzzzzzzzzz\n" |
7360 | " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7361 | " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", |
7362 | Style); |
7363 | |
7364 | Style.ColumnLimit = 80; |
7365 | Style.IndentWidth = 4; |
7366 | Style.TabWidth = 4; |
7367 | Style.UseTab = FormatStyle::UT_Always; |
7368 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
7369 | Style.AlignOperands = FormatStyle::OAS_DontAlign; |
7370 | verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" |
7371 | "\t&& (someOtherLongishConditionPart1\n" |
7372 | "\t\t|| someOtherEvenLongerNestedConditionPart2);", |
7373 | "return someVeryVeryLongConditionThatBarelyFitsOnALine && " |
7374 | "(someOtherLongishConditionPart1 || " |
7375 | "someOtherEvenLongerNestedConditionPart2);", |
7376 | Style); |
7377 | |
7378 | Style = getLLVMStyleWithColumns(ColumnLimit: 20); |
7379 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
7380 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
7381 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
7382 | Style.ContinuationIndentWidth = 2; |
7383 | verifyFormat("struct Foo {\n" |
7384 | " Foo(\n" |
7385 | " int arg1,\n" |
7386 | " int arg2)\n" |
7387 | " : Base(\n" |
7388 | " arg1,\n" |
7389 | " arg2) {}\n" |
7390 | "};", |
7391 | Style); |
7392 | verifyFormat("return abc\n" |
7393 | " ? foo(\n" |
7394 | " a,\n" |
7395 | " b,\n" |
7396 | " bar(\n" |
7397 | " abc))\n" |
7398 | " : g(abc);", |
7399 | Style); |
7400 | } |
7401 | |
7402 | TEST_F(FormatTest, ExpressionIndentationStrictAlign) { |
7403 | FormatStyle Style = getLLVMStyle(); |
7404 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
7405 | Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; |
7406 | |
7407 | verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7408 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7409 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7410 | " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7411 | " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7412 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7413 | " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7414 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7415 | " > ccccccccccccccccccccccccccccccccccccccccc;", |
7416 | Style); |
7417 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7418 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7419 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7420 | " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", |
7421 | Style); |
7422 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7423 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7424 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7425 | " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", |
7426 | Style); |
7427 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7428 | " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7429 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7430 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", |
7431 | Style); |
7432 | verifyFormat("if () {\n" |
7433 | "} else if (aaaaa\n" |
7434 | " && bbbbb // break\n" |
7435 | " > ccccc) {\n" |
7436 | "}", |
7437 | Style); |
7438 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7439 | " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", |
7440 | Style); |
7441 | verifyFormat("return (a)\n" |
7442 | " // comment\n" |
7443 | " + b;", |
7444 | Style); |
7445 | verifyFormat( |
7446 | "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7447 | " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7448 | " + cc;", |
7449 | Style); |
7450 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
7451 | " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" |
7452 | " : 3333333333333333;", |
7453 | Style); |
7454 | verifyFormat( |
7455 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
7456 | " : ccccccccccccccc ? dddddddddddddddddd\n" |
7457 | " : eeeeeeeeeeeeeeeeee)\n" |
7458 | " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" |
7459 | " : 3333333333333333;", |
7460 | Style); |
7461 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7462 | " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
7463 | Style); |
7464 | |
7465 | verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" |
7466 | " == boost::fusion::at_c<1>(iiii).second;", |
7467 | Style); |
7468 | |
7469 | Style.ColumnLimit = 60; |
7470 | verifyFormat("zzzzzzzzzzzzz\n" |
7471 | " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7472 | " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", |
7473 | Style); |
7474 | |
7475 | // Forced by comments. |
7476 | Style.ColumnLimit = 80; |
7477 | verifyFormat( |
7478 | "unsigned ContentSize\n" |
7479 | " = sizeof(int16_t) // DWARF ARange version number\n" |
7480 | " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" |
7481 | " + sizeof(int8_t) // Pointer Size (in bytes)\n" |
7482 | " + sizeof(int8_t); // Segment Size (in bytes)", |
7483 | Style); |
7484 | |
7485 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
7486 | verifyFormat( |
7487 | "unsigned ContentSize =\n" |
7488 | " sizeof(int16_t) // DWARF ARange version number\n" |
7489 | " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" |
7490 | " + sizeof(int8_t) // Pointer Size (in bytes)\n" |
7491 | " + sizeof(int8_t); // Segment Size (in bytes)", |
7492 | Style); |
7493 | |
7494 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
7495 | verifyFormat( |
7496 | "unsigned ContentSize =\n" |
7497 | " sizeof(int16_t) // DWARF ARange version number\n" |
7498 | " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" |
7499 | " + sizeof(int8_t) // Pointer Size (in bytes)\n" |
7500 | " + sizeof(int8_t); // Segment Size (in bytes)", |
7501 | Style); |
7502 | } |
7503 | |
7504 | TEST_F(FormatTest, EnforcedOperatorWraps) { |
7505 | // Here we'd like to wrap after the || operators, but a comment is forcing an |
7506 | // earlier wrap. |
7507 | verifyFormat("bool x = aaaaa //\n" |
7508 | " || bbbbb\n" |
7509 | " //\n" |
7510 | " || cccc;"); |
7511 | } |
7512 | |
7513 | TEST_F(FormatTest, NoOperandAlignment) { |
7514 | FormatStyle Style = getLLVMStyle(); |
7515 | Style.AlignOperands = FormatStyle::OAS_DontAlign; |
7516 | verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" |
7517 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
7518 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
7519 | Style); |
7520 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
7521 | verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7522 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7523 | " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7524 | " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7525 | " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7526 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7527 | " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7528 | " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7529 | " > ccccccccccccccccccccccccccccccccccccccccc;", |
7530 | Style); |
7531 | |
7532 | verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7533 | " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7534 | " + cc;", |
7535 | Style); |
7536 | verifyFormat("int a = aa\n" |
7537 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
7538 | " * cccccccccccccccccccccccccccccccccccc;", |
7539 | Style); |
7540 | |
7541 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
7542 | verifyFormat("return (a > b\n" |
7543 | " // comment1\n" |
7544 | " // comment2\n" |
7545 | " || c);", |
7546 | Style); |
7547 | } |
7548 | |
7549 | TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { |
7550 | FormatStyle Style = getLLVMStyle(); |
7551 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
7552 | verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
7553 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
7554 | " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", |
7555 | Style); |
7556 | } |
7557 | |
7558 | TEST_F(FormatTest, AllowBinPackingInsideArguments) { |
7559 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
7560 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
7561 | Style.BinPackArguments = false; |
7562 | verifyFormat("void test() {\n" |
7563 | " someFunction(\n" |
7564 | " this + argument + is + quite\n" |
7565 | " + long + so + it + gets + wrapped\n" |
7566 | " + but + remains + bin - packed);\n" |
7567 | "}", |
7568 | Style); |
7569 | verifyFormat("void test() {\n" |
7570 | " someFunction(arg1,\n" |
7571 | " this + argument + is\n" |
7572 | " + quite + long + so\n" |
7573 | " + it + gets + wrapped\n" |
7574 | " + but + remains + bin\n" |
7575 | " - packed,\n" |
7576 | " arg3);\n" |
7577 | "}", |
7578 | Style); |
7579 | verifyFormat("void test() {\n" |
7580 | " someFunction(\n" |
7581 | " arg1,\n" |
7582 | " this + argument + has\n" |
7583 | " + anotherFunc(nested,\n" |
7584 | " calls + whose\n" |
7585 | " + arguments\n" |
7586 | " + are + also\n" |
7587 | " + wrapped,\n" |
7588 | " in + addition)\n" |
7589 | " + to + being + bin - packed,\n" |
7590 | " arg3);\n" |
7591 | "}", |
7592 | Style); |
7593 | |
7594 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
7595 | verifyFormat("void test() {\n" |
7596 | " someFunction(\n" |
7597 | " arg1,\n" |
7598 | " this + argument + has +\n" |
7599 | " anotherFunc(nested,\n" |
7600 | " calls + whose +\n" |
7601 | " arguments +\n" |
7602 | " are + also +\n" |
7603 | " wrapped,\n" |
7604 | " in + addition) +\n" |
7605 | " to + being + bin - packed,\n" |
7606 | " arg3);\n" |
7607 | "}", |
7608 | Style); |
7609 | } |
7610 | |
7611 | TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) { |
7612 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 45); |
7613 | EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None); |
7614 | verifyFormat("bool b =\n" |
7615 | " is_default_constructible_v<hash<T>> and\n" |
7616 | " is_copy_constructible_v<hash<T>> and\n" |
7617 | " is_move_constructible_v<hash<T>> and\n" |
7618 | " is_copy_assignable_v<hash<T>> and\n" |
7619 | " is_move_assignable_v<hash<T>> and\n" |
7620 | " is_destructible_v<hash<T>> and\n" |
7621 | " is_swappable_v<hash<T>> and\n" |
7622 | " is_callable_v<hash<T>(T)>;", |
7623 | Style); |
7624 | |
7625 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
7626 | verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" |
7627 | " and is_copy_constructible_v<hash<T>>\n" |
7628 | " and is_move_constructible_v<hash<T>>\n" |
7629 | " and is_copy_assignable_v<hash<T>>\n" |
7630 | " and is_move_assignable_v<hash<T>>\n" |
7631 | " and is_destructible_v<hash<T>>\n" |
7632 | " and is_swappable_v<hash<T>>\n" |
7633 | " and is_callable_v<hash<T>(T)>;", |
7634 | Style); |
7635 | |
7636 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
7637 | verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" |
7638 | " and is_copy_constructible_v<hash<T>>\n" |
7639 | " and is_move_constructible_v<hash<T>>\n" |
7640 | " and is_copy_assignable_v<hash<T>>\n" |
7641 | " and is_move_assignable_v<hash<T>>\n" |
7642 | " and is_destructible_v<hash<T>>\n" |
7643 | " and is_swappable_v<hash<T>>\n" |
7644 | " and is_callable_v<hash<T>(T)>;", |
7645 | Style); |
7646 | } |
7647 | |
7648 | TEST_F(FormatTest, ConstructorInitializers) { |
7649 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
7650 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", |
7651 | getLLVMStyleWithColumns(45)); |
7652 | verifyFormat("Constructor()\n" |
7653 | " : Inttializer(FitsOnTheLine) {}", |
7654 | getLLVMStyleWithColumns(44)); |
7655 | verifyFormat("Constructor()\n" |
7656 | " : Inttializer(FitsOnTheLine) {}", |
7657 | getLLVMStyleWithColumns(43)); |
7658 | |
7659 | verifyFormat("template <typename T>\n" |
7660 | "Constructor() : Initializer(FitsOnTheLine) {}", |
7661 | getLLVMStyleWithColumns(45)); |
7662 | |
7663 | verifyFormat( |
7664 | "SomeClass::Constructor()\n" |
7665 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
7666 | |
7667 | verifyFormat( |
7668 | "SomeClass::Constructor()\n" |
7669 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
7670 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); |
7671 | verifyFormat( |
7672 | "SomeClass::Constructor()\n" |
7673 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
7674 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
7675 | verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7676 | " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
7677 | " : aaaaaaaaaa(aaaaaa) {}"); |
7678 | |
7679 | verifyFormat("Constructor()\n" |
7680 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
7681 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7682 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
7683 | " aaaaaaaaaaaaaaaaaaaaaaa() {}"); |
7684 | |
7685 | verifyFormat("Constructor()\n" |
7686 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7687 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
7688 | |
7689 | verifyFormat("Constructor(int Parameter = 0)\n" |
7690 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" |
7691 | " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); |
7692 | verifyFormat("Constructor()\n" |
7693 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" |
7694 | "}", |
7695 | getLLVMStyleWithColumns(60)); |
7696 | verifyFormat("Constructor()\n" |
7697 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7698 | " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); |
7699 | |
7700 | // Here a line could be saved by splitting the second initializer onto two |
7701 | // lines, but that is not desirable. |
7702 | verifyFormat("Constructor()\n" |
7703 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
7704 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
7705 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
7706 | |
7707 | FormatStyle OnePerLine = getLLVMStyle(); |
7708 | OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; |
7709 | verifyFormat("MyClass::MyClass()\n" |
7710 | " : a(a),\n" |
7711 | " b(b),\n" |
7712 | " c(c) {}", |
7713 | OnePerLine); |
7714 | verifyFormat("MyClass::MyClass()\n" |
7715 | " : a(a), // comment\n" |
7716 | " b(b),\n" |
7717 | " c(c) {}", |
7718 | OnePerLine); |
7719 | verifyFormat("MyClass::MyClass(int a)\n" |
7720 | " : b(a), // comment\n" |
7721 | " c(a + 1) { // lined up\n" |
7722 | "}", |
7723 | OnePerLine); |
7724 | verifyFormat("Constructor()\n" |
7725 | " : a(b, b, b) {}", |
7726 | OnePerLine); |
7727 | OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7728 | OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; |
7729 | verifyFormat("SomeClass::Constructor()\n" |
7730 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
7731 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
7732 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
7733 | OnePerLine); |
7734 | verifyFormat("SomeClass::Constructor()\n" |
7735 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
7736 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
7737 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
7738 | OnePerLine); |
7739 | verifyFormat("MyClass::MyClass(int var)\n" |
7740 | " : some_var_(var), // 4 space indent\n" |
7741 | " some_other_var_(var + 1) { // lined up\n" |
7742 | "}", |
7743 | OnePerLine); |
7744 | verifyFormat("Constructor()\n" |
7745 | " : aaaaa(aaaaaa),\n" |
7746 | " aaaaa(aaaaaa),\n" |
7747 | " aaaaa(aaaaaa),\n" |
7748 | " aaaaa(aaaaaa),\n" |
7749 | " aaaaa(aaaaaa) {}", |
7750 | OnePerLine); |
7751 | verifyFormat("Constructor()\n" |
7752 | " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" |
7753 | " aaaaaaaaaaaaaaaaaaaaaa) {}", |
7754 | OnePerLine); |
7755 | OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
7756 | verifyFormat( |
7757 | "Constructor()\n" |
7758 | " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
7759 | " aaaaaaaaaaa().aaa(),\n" |
7760 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
7761 | OnePerLine); |
7762 | OnePerLine.ColumnLimit = 60; |
7763 | verifyFormat("Constructor()\n" |
7764 | " : aaaaaaaaaaaaaaaaaaaa(a),\n" |
7765 | " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", |
7766 | OnePerLine); |
7767 | |
7768 | verifyFormat("Constructor()\n" |
7769 | " : // Comment forcing unwanted break.\n" |
7770 | " aaaa(aaaa) {}", |
7771 | "Constructor() :\n" |
7772 | " // Comment forcing unwanted break.\n" |
7773 | " aaaa(aaaa) {}"); |
7774 | } |
7775 | |
7776 | TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { |
7777 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
7778 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
7779 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
7780 | |
7781 | for (int i = 0; i < 4; ++i) { |
7782 | // Test all combinations of parameters that should not have an effect. |
7783 | Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; |
7784 | Style.AllowAllArgumentsOnNextLine = i & 2; |
7785 | |
7786 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7787 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
7788 | verifyFormat("Constructor()\n" |
7789 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7790 | Style); |
7791 | verifyFormat("Constructor() : a(a), b(b) {}", Style); |
7792 | |
7793 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7794 | verifyFormat("Constructor()\n" |
7795 | " : aaaaaaaaaaaaaaaaaaaa(a)\n" |
7796 | " , bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7797 | Style); |
7798 | verifyFormat("Constructor() : a(a), b(b) {}", Style); |
7799 | |
7800 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
7801 | verifyFormat("Constructor()\n" |
7802 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7803 | Style); |
7804 | verifyFormat("Constructor()\n" |
7805 | " : a(a), b(b) {}", |
7806 | Style); |
7807 | verifyFormat("Constructor()\n" |
7808 | " : aaaaaaaaaaaaaaaaaaaa(a)\n" |
7809 | " , bbbbbbbbbbbbbbbbbbbbb(b)\n" |
7810 | " , cccccccccccccccccccccc(c) {}", |
7811 | Style); |
7812 | |
7813 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; |
7814 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7815 | verifyFormat("Constructor()\n" |
7816 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7817 | Style); |
7818 | |
7819 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7820 | verifyFormat("Constructor()\n" |
7821 | " : aaaaaaaaaaaaaaaaaaaa(a),\n" |
7822 | " bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7823 | Style); |
7824 | |
7825 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
7826 | verifyFormat("Constructor()\n" |
7827 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7828 | Style); |
7829 | verifyFormat("Constructor()\n" |
7830 | " : a(a), b(b) {}", |
7831 | Style); |
7832 | verifyFormat("Constructor()\n" |
7833 | " : aaaaaaaaaaaaaaaaaaaa(a),\n" |
7834 | " bbbbbbbbbbbbbbbbbbbbb(b),\n" |
7835 | " cccccccccccccccccccccc(c) {}", |
7836 | Style); |
7837 | |
7838 | Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; |
7839 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7840 | verifyFormat("Constructor() :\n" |
7841 | " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7842 | Style); |
7843 | |
7844 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7845 | verifyFormat("Constructor() :\n" |
7846 | " aaaaaaaaaaaaaaaaaa(a),\n" |
7847 | " bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7848 | Style); |
7849 | |
7850 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
7851 | verifyFormat("Constructor() :\n" |
7852 | " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7853 | Style); |
7854 | verifyFormat("Constructor() :\n" |
7855 | " a(a), b(b) {}", |
7856 | Style); |
7857 | verifyFormat("Constructor() :\n" |
7858 | " aaaaaaaaaaaaaaaaaaaa(a),\n" |
7859 | " bbbbbbbbbbbbbbbbbbbbb(b),\n" |
7860 | " cccccccccccccccccccccc(c) {}", |
7861 | Style); |
7862 | } |
7863 | |
7864 | // Test interactions between AllowAllParametersOfDeclarationOnNextLine and |
7865 | // AllowAllConstructorInitializersOnNextLine in all |
7866 | // BreakConstructorInitializers modes |
7867 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
7868 | Style.AllowAllParametersOfDeclarationOnNextLine = true; |
7869 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7870 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7871 | " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" |
7872 | " : aaaaaaaaaaaaaaaaaaaa(a)\n" |
7873 | " , bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7874 | Style); |
7875 | |
7876 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7877 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7878 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7879 | " int bbbbbbbbbbbbb,\n" |
7880 | " int cccccccccccccccc)\n" |
7881 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7882 | Style); |
7883 | |
7884 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
7885 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7886 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7887 | " int bbbbbbbbbbbbb,\n" |
7888 | " int cccccccccccccccc)\n" |
7889 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7890 | Style); |
7891 | |
7892 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
7893 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7894 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7895 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7896 | " int bbbbbbbbbbbbb)\n" |
7897 | " : aaaaaaaaaaaaaaaaaaaa(a)\n" |
7898 | " , bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7899 | Style); |
7900 | |
7901 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; |
7902 | |
7903 | Style.AllowAllParametersOfDeclarationOnNextLine = true; |
7904 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7905 | " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" |
7906 | " : aaaaaaaaaaaaaaaaaaaa(a),\n" |
7907 | " bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7908 | Style); |
7909 | |
7910 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7911 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7912 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7913 | " int bbbbbbbbbbbbb,\n" |
7914 | " int cccccccccccccccc)\n" |
7915 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7916 | Style); |
7917 | |
7918 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
7919 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7920 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7921 | " int bbbbbbbbbbbbb,\n" |
7922 | " int cccccccccccccccc)\n" |
7923 | " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7924 | Style); |
7925 | |
7926 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
7927 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7928 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7929 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7930 | " int bbbbbbbbbbbbb)\n" |
7931 | " : aaaaaaaaaaaaaaaaaaaa(a),\n" |
7932 | " bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7933 | Style); |
7934 | |
7935 | Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; |
7936 | Style.AllowAllParametersOfDeclarationOnNextLine = true; |
7937 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7938 | " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" |
7939 | " aaaaaaaaaaaaaaaaaaaa(a),\n" |
7940 | " bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7941 | Style); |
7942 | |
7943 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
7944 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7945 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7946 | " int bbbbbbbbbbbbb,\n" |
7947 | " int cccccccccccccccc) :\n" |
7948 | " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7949 | Style); |
7950 | |
7951 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
7952 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7953 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7954 | " int bbbbbbbbbbbbb,\n" |
7955 | " int cccccccccccccccc) :\n" |
7956 | " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7957 | Style); |
7958 | |
7959 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
7960 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7961 | verifyFormat("SomeClassWithALongName::Constructor(\n" |
7962 | " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7963 | " int bbbbbbbbbbbbb) :\n" |
7964 | " aaaaaaaaaaaaaaaaaaaa(a),\n" |
7965 | " bbbbbbbbbbbbbbbbbbbbb(b) {}", |
7966 | Style); |
7967 | |
7968 | Style = getLLVMStyleWithColumns(ColumnLimit: 0); |
7969 | Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
7970 | verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style); |
7971 | verifyNoChange("Foo(Bar bar, Baz baz)\n" |
7972 | " : bar(bar), baz(baz) {}", |
7973 | Style); |
7974 | } |
7975 | |
7976 | TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { |
7977 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
7978 | Style.BinPackArguments = false; |
7979 | for (int i = 0; i < 4; ++i) { |
7980 | // Test all combinations of parameters that should not have an effect. |
7981 | Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; |
7982 | Style.PackConstructorInitializers = |
7983 | i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; |
7984 | |
7985 | Style.AllowAllArgumentsOnNextLine = true; |
7986 | verifyFormat("void foo() {\n" |
7987 | " FunctionCallWithReallyLongName(\n" |
7988 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" |
7989 | "}", |
7990 | Style); |
7991 | Style.AllowAllArgumentsOnNextLine = false; |
7992 | verifyFormat("void foo() {\n" |
7993 | " FunctionCallWithReallyLongName(\n" |
7994 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
7995 | " bbbbbbbbbbbb);\n" |
7996 | "}", |
7997 | Style); |
7998 | |
7999 | Style.AllowAllArgumentsOnNextLine = true; |
8000 | verifyFormat("void foo() {\n" |
8001 | " auto VariableWithReallyLongName = {\n" |
8002 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" |
8003 | "}", |
8004 | Style); |
8005 | Style.AllowAllArgumentsOnNextLine = false; |
8006 | verifyFormat("void foo() {\n" |
8007 | " auto VariableWithReallyLongName = {\n" |
8008 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8009 | " bbbbbbbbbbbb};\n" |
8010 | "}", |
8011 | Style); |
8012 | } |
8013 | |
8014 | // This parameter should not affect declarations. |
8015 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
8016 | Style.AllowAllArgumentsOnNextLine = false; |
8017 | Style.AllowAllParametersOfDeclarationOnNextLine = true; |
8018 | verifyFormat("void FunctionCallWithReallyLongName(\n" |
8019 | " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", |
8020 | Style); |
8021 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
8022 | verifyFormat("void FunctionCallWithReallyLongName(\n" |
8023 | " int aaaaaaaaaaaaaaaaaaaaaaa,\n" |
8024 | " int bbbbbbbbbbbb);", |
8025 | Style); |
8026 | } |
8027 | |
8028 | TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { |
8029 | // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign |
8030 | // and BAS_Align. |
8031 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 35); |
8032 | StringRef Input = "functionCall(paramA, paramB, paramC);\n" |
8033 | "void functionDecl(int A, int B, int C);"; |
8034 | Style.AllowAllArgumentsOnNextLine = false; |
8035 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
8036 | verifyFormat(StringRef("functionCall(paramA, paramB,\n" |
8037 | " paramC);\n" |
8038 | "void functionDecl(int A, int B,\n" |
8039 | " int C);"), |
8040 | Input, Style); |
8041 | Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; |
8042 | verifyFormat(StringRef("functionCall(paramA, paramB,\n" |
8043 | " paramC);\n" |
8044 | "void functionDecl(int A, int B,\n" |
8045 | " int C);"), |
8046 | Input, Style); |
8047 | // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over |
8048 | // AllowAllArgumentsOnNextLine. |
8049 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
8050 | verifyFormat(StringRef("functionCall(\n" |
8051 | " paramA, paramB, paramC);\n" |
8052 | "void functionDecl(\n" |
8053 | " int A, int B, int C);"), |
8054 | Input, Style); |
8055 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
8056 | verifyFormat("functionCall(\n" |
8057 | " paramA, paramB, paramC\n" |
8058 | ");\n" |
8059 | "void functionDecl(\n" |
8060 | " int A, int B, int C\n" |
8061 | ");", |
8062 | Input, Style); |
8063 | |
8064 | // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the |
8065 | // first argument. |
8066 | Style.AllowAllArgumentsOnNextLine = true; |
8067 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
8068 | verifyFormat(StringRef("functionCall(\n" |
8069 | " paramA, paramB, paramC);\n" |
8070 | "void functionDecl(\n" |
8071 | " int A, int B, int C);"), |
8072 | Input, Style); |
8073 | // It wouldn't fit on one line with aligned parameters so this setting |
8074 | // doesn't change anything for BAS_Align. |
8075 | Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; |
8076 | verifyFormat(StringRef("functionCall(paramA, paramB,\n" |
8077 | " paramC);\n" |
8078 | "void functionDecl(int A, int B,\n" |
8079 | " int C);"), |
8080 | Input, Style); |
8081 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
8082 | verifyFormat(StringRef("functionCall(\n" |
8083 | " paramA, paramB, paramC);\n" |
8084 | "void functionDecl(\n" |
8085 | " int A, int B, int C);"), |
8086 | Input, Style); |
8087 | } |
8088 | |
8089 | TEST_F(FormatTest, BreakFunctionDefinitionParameters) { |
8090 | StringRef Input = "void functionDecl(paramA, paramB, paramC);\n" |
8091 | "void emptyFunctionDefinition() {}\n" |
8092 | "void functionDefinition(int A, int B, int C) {}\n" |
8093 | "Class::Class(int A, int B) : m_A(A), m_B(B) {}"; |
8094 | verifyFormat(Input); |
8095 | |
8096 | FormatStyle Style = getLLVMStyle(); |
8097 | EXPECT_FALSE(Style.BreakFunctionDefinitionParameters); |
8098 | Style.BreakFunctionDefinitionParameters = true; |
8099 | verifyFormat("void functionDecl(paramA, paramB, paramC);\n" |
8100 | "void emptyFunctionDefinition() {}\n" |
8101 | "void functionDefinition(\n" |
8102 | " int A, int B, int C) {}\n" |
8103 | "Class::Class(\n" |
8104 | " int A, int B)\n" |
8105 | " : m_A(A), m_B(B) {}", |
8106 | Input, Style); |
8107 | |
8108 | // Test the style where all parameters are on their own lines. |
8109 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
8110 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
8111 | verifyFormat("void functionDecl(paramA, paramB, paramC);\n" |
8112 | "void emptyFunctionDefinition() {}\n" |
8113 | "void functionDefinition(\n" |
8114 | " int A,\n" |
8115 | " int B,\n" |
8116 | " int C) {}\n" |
8117 | "Class::Class(\n" |
8118 | " int A,\n" |
8119 | " int B)\n" |
8120 | " : m_A(A), m_B(B) {}", |
8121 | Input, Style); |
8122 | } |
8123 | |
8124 | TEST_F(FormatTest, BreakBeforeInlineASMColon) { |
8125 | FormatStyle Style = getLLVMStyle(); |
8126 | Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never; |
8127 | /* Test the behaviour with long lines */ |
8128 | Style.ColumnLimit = 40; |
8129 | verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" |
8130 | " : : val);", |
8131 | Style); |
8132 | verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" |
8133 | " : val1 : val2);", |
8134 | Style); |
8135 | verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" |
8136 | " \"cpuid\\n\\t\"\n" |
8137 | " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n" |
8138 | " : \"=a\" : \"a\");", |
8139 | Style); |
8140 | Style.ColumnLimit = 80; |
8141 | verifyFormat("asm volatile(\"string\", : : val);", Style); |
8142 | verifyFormat("asm volatile(\"string\", : val1 : val2);", Style); |
8143 | |
8144 | Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always; |
8145 | verifyFormat("asm volatile(\"string\",\n" |
8146 | " :\n" |
8147 | " : val);", |
8148 | Style); |
8149 | verifyFormat("asm volatile(\"string\",\n" |
8150 | " : val1\n" |
8151 | " : val2);", |
8152 | Style); |
8153 | /* Test the behaviour with long lines */ |
8154 | Style.ColumnLimit = 40; |
8155 | verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" |
8156 | " \"cpuid\\n\\t\"\n" |
8157 | " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" |
8158 | " : \"=a\"(*rEAX)\n" |
8159 | " : \"a\"(value));", |
8160 | Style); |
8161 | verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" |
8162 | " \"cpuid\\n\\t\"\n" |
8163 | " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" |
8164 | " :\n" |
8165 | " : \"a\"(value));", |
8166 | Style); |
8167 | verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" |
8168 | " :\n" |
8169 | " : val);", |
8170 | Style); |
8171 | verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" |
8172 | " : val1\n" |
8173 | " : val2);", |
8174 | Style); |
8175 | } |
8176 | |
8177 | TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { |
8178 | FormatStyle Style = getLLVMStyle(); |
8179 | Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; |
8180 | |
8181 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
8182 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", |
8183 | getStyleWithColumns(Style, 45)); |
8184 | verifyFormat("Constructor() :\n" |
8185 | " Initializer(FitsOnTheLine) {}", |
8186 | getStyleWithColumns(Style, 44)); |
8187 | verifyFormat("Constructor() :\n" |
8188 | " Initializer(FitsOnTheLine) {}", |
8189 | getStyleWithColumns(Style, 43)); |
8190 | |
8191 | verifyFormat("template <typename T>\n" |
8192 | "Constructor() : Initializer(FitsOnTheLine) {}", |
8193 | getStyleWithColumns(Style, 50)); |
8194 | verifyFormat( |
8195 | "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n" |
8196 | " int mooooooooooooore) noexcept :\n" |
8197 | " Super{some, arguments}, Member{5}, Member2{2} {}", |
8198 | Style); |
8199 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
8200 | verifyFormat( |
8201 | "SomeClass::Constructor() :\n" |
8202 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", |
8203 | Style); |
8204 | verifyFormat( |
8205 | "SomeClass::Constructor() : // NOLINT\n" |
8206 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", |
8207 | Style); |
8208 | |
8209 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
8210 | verifyFormat( |
8211 | "SomeClass::Constructor() :\n" |
8212 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", |
8213 | Style); |
8214 | verifyFormat( |
8215 | "SomeClass::Constructor() : // NOLINT\n" |
8216 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", |
8217 | Style); |
8218 | |
8219 | Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; |
8220 | verifyFormat( |
8221 | "SomeClass::Constructor() :\n" |
8222 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", |
8223 | Style); |
8224 | |
8225 | verifyFormat( |
8226 | "SomeClass::Constructor() :\n" |
8227 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
8228 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
8229 | Style); |
8230 | verifyFormat( |
8231 | "SomeClass::Constructor() :\n" |
8232 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
8233 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", |
8234 | Style); |
8235 | verifyFormat( |
8236 | "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8237 | " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}", |
8238 | Style); |
8239 | |
8240 | verifyFormat("Constructor() :\n" |
8241 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
8242 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8243 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
8244 | " aaaaaaaaaaaaaaaaaaaaaaa() {}", |
8245 | Style); |
8246 | |
8247 | verifyFormat("Constructor() :\n" |
8248 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8249 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
8250 | Style); |
8251 | |
8252 | verifyFormat("Constructor(int Parameter = 0) :\n" |
8253 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" |
8254 | " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", |
8255 | Style); |
8256 | verifyFormat("Constructor() :\n" |
8257 | " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" |
8258 | "}", |
8259 | getStyleWithColumns(Style, 60)); |
8260 | verifyFormat("Constructor() :\n" |
8261 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8262 | " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", |
8263 | Style); |
8264 | |
8265 | // Here a line could be saved by splitting the second initializer onto two |
8266 | // lines, but that is not desirable. |
8267 | verifyFormat("Constructor() :\n" |
8268 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
8269 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
8270 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
8271 | Style); |
8272 | |
8273 | FormatStyle OnePerLine = Style; |
8274 | OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; |
8275 | verifyFormat("SomeClass::Constructor() :\n" |
8276 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
8277 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
8278 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
8279 | OnePerLine); |
8280 | verifyFormat("SomeClass::Constructor() :\n" |
8281 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
8282 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
8283 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
8284 | OnePerLine); |
8285 | verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n" |
8286 | " i(i), // comment\n" |
8287 | " j(j) {}", |
8288 | OnePerLine); |
8289 | verifyFormat("MyClass::MyClass(int var) :\n" |
8290 | " some_var_(var), // 4 space indent\n" |
8291 | " some_other_var_(var + 1) { // lined up\n" |
8292 | "}", |
8293 | OnePerLine); |
8294 | verifyFormat("Constructor() :\n" |
8295 | " aaaaa(aaaaaa),\n" |
8296 | " aaaaa(aaaaaa),\n" |
8297 | " aaaaa(aaaaaa),\n" |
8298 | " aaaaa(aaaaaa),\n" |
8299 | " aaaaa(aaaaaa) {}", |
8300 | OnePerLine); |
8301 | verifyFormat("Constructor() :\n" |
8302 | " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" |
8303 | " aaaaaaaaaaaaaaaaaaaaaa) {}", |
8304 | OnePerLine); |
8305 | OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
8306 | verifyFormat("Constructor() :\n" |
8307 | " aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8308 | " aaaaaaaaaaa().aaa(),\n" |
8309 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
8310 | OnePerLine); |
8311 | OnePerLine.ColumnLimit = 60; |
8312 | verifyFormat("Constructor() :\n" |
8313 | " aaaaaaaaaaaaaaaaaaaa(a),\n" |
8314 | " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", |
8315 | OnePerLine); |
8316 | |
8317 | verifyFormat("Constructor() :\n" |
8318 | " // Comment forcing unwanted break.\n" |
8319 | " aaaa(aaaa) {}", |
8320 | Style); |
8321 | verifyFormat("Constructor() : // NOLINT\n" |
8322 | " aaaa(aaaa) {}", |
8323 | Style); |
8324 | verifyFormat("Constructor() : // A very long trailing comment that cannot fit" |
8325 | " on a single\n" |
8326 | " // line.\n" |
8327 | " aaaa(aaaa) {}", |
8328 | "Constructor() : // A very long trailing comment that cannot fit" |
8329 | " on a single line.\n" |
8330 | " aaaa(aaaa) {}", |
8331 | Style); |
8332 | |
8333 | Style.ColumnLimit = 0; |
8334 | verifyNoChange("SomeClass::Constructor() :\n" |
8335 | " a(a) {}", |
8336 | Style); |
8337 | verifyNoChange("SomeClass::Constructor() noexcept :\n" |
8338 | " a(a) {}", |
8339 | Style); |
8340 | verifyNoChange("SomeClass::Constructor() :\n" |
8341 | " a(a), b(b), c(c) {}", |
8342 | Style); |
8343 | verifyNoChange("SomeClass::Constructor() :\n" |
8344 | " a(a) {\n" |
8345 | " foo();\n" |
8346 | " bar();\n" |
8347 | "}", |
8348 | Style); |
8349 | verifyFormat("struct Foo {\n" |
8350 | " int x;\n" |
8351 | " Foo() : x(0) {}\n" |
8352 | "};", |
8353 | "struct Foo {\n" |
8354 | " int x;\n" |
8355 | " Foo():x(0) {}\n" |
8356 | "};", |
8357 | Style); |
8358 | |
8359 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
8360 | verifyNoChange("SomeClass::Constructor() :\n" |
8361 | " a(a), b(b), c(c) {\n" |
8362 | "}", |
8363 | Style); |
8364 | verifyNoChange("SomeClass::Constructor() :\n" |
8365 | " a(a) {\n" |
8366 | "}", |
8367 | Style); |
8368 | |
8369 | Style.ColumnLimit = 80; |
8370 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
8371 | Style.ConstructorInitializerIndentWidth = 2; |
8372 | verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); |
8373 | verifyFormat("SomeClass::Constructor() :\n" |
8374 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8375 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", |
8376 | Style); |
8377 | |
8378 | // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as |
8379 | // well |
8380 | Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; |
8381 | verifyFormat( |
8382 | "class SomeClass\n" |
8383 | " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8384 | " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", |
8385 | Style); |
8386 | Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; |
8387 | verifyFormat( |
8388 | "class SomeClass\n" |
8389 | " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8390 | " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", |
8391 | Style); |
8392 | Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; |
8393 | verifyFormat( |
8394 | "class SomeClass :\n" |
8395 | " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8396 | " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", |
8397 | Style); |
8398 | Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; |
8399 | verifyFormat( |
8400 | "class SomeClass\n" |
8401 | " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8402 | " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", |
8403 | Style); |
8404 | } |
8405 | |
8406 | #ifndef EXPENSIVE_CHECKS |
8407 | // Expensive checks enables libstdc++ checking which includes validating the |
8408 | // state of ranges used in std::priority_queue - this blows out the |
8409 | // runtime/scalability of the function and makes this test unacceptably slow. |
8410 | TEST_F(FormatTest, MemoizationTests) { |
8411 | // This breaks if the memoization lookup does not take \c Indent and |
8412 | // \c LastSpace into account. |
8413 | verifyFormat( |
8414 | "extern CFRunLoopTimerRef\n" |
8415 | "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" |
8416 | " CFTimeInterval interval, CFOptionFlags flags,\n" |
8417 | " CFIndex order, CFRunLoopTimerCallBack callout,\n" |
8418 | " CFRunLoopTimerContext *context) {}"); |
8419 | |
8420 | // Deep nesting somewhat works around our memoization. |
8421 | verifyFormat( |
8422 | "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
8423 | " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
8424 | " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
8425 | " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
8426 | " aaaaa())))))))))))))))))))))))))))))))))))))));", |
8427 | getLLVMStyleWithColumns(65)); |
8428 | verifyFormat( |
8429 | "aaaaa(\n" |
8430 | " aaaaa,\n" |
8431 | " aaaaa(\n" |
8432 | " aaaaa,\n" |
8433 | " aaaaa(\n" |
8434 | " aaaaa,\n" |
8435 | " aaaaa(\n" |
8436 | " aaaaa,\n" |
8437 | " aaaaa(\n" |
8438 | " aaaaa,\n" |
8439 | " aaaaa(\n" |
8440 | " aaaaa,\n" |
8441 | " aaaaa(\n" |
8442 | " aaaaa,\n" |
8443 | " aaaaa(\n" |
8444 | " aaaaa,\n" |
8445 | " aaaaa(\n" |
8446 | " aaaaa,\n" |
8447 | " aaaaa(\n" |
8448 | " aaaaa,\n" |
8449 | " aaaaa(\n" |
8450 | " aaaaa,\n" |
8451 | " aaaaa(\n" |
8452 | " aaaaa,\n" |
8453 | " aaaaa))))))))))));", |
8454 | getLLVMStyleWithColumns(65)); |
8455 | verifyFormat( |
8456 | "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n" |
8457 | " a),\n" |
8458 | " a),\n" |
8459 | " a),\n" |
8460 | " a),\n" |
8461 | " a),\n" |
8462 | " a),\n" |
8463 | " a),\n" |
8464 | " a),\n" |
8465 | " a),\n" |
8466 | " a),\n" |
8467 | " a),\n" |
8468 | " a),\n" |
8469 | " a),\n" |
8470 | " a),\n" |
8471 | " a),\n" |
8472 | " a),\n" |
8473 | " a)", |
8474 | getLLVMStyleWithColumns(65)); |
8475 | |
8476 | // This test takes VERY long when memoization is broken. |
8477 | FormatStyle OnePerLine = getLLVMStyle(); |
8478 | OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
8479 | OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
8480 | std::string input = "Constructor()\n" |
8481 | " : aaaa(a,\n"; |
8482 | for (unsigned i = 0, e = 80; i != e; ++i) |
8483 | input += " a,\n"; |
8484 | input += " a) {}"; |
8485 | verifyFormat(input, OnePerLine); |
8486 | OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
8487 | verifyFormat(input, OnePerLine); |
8488 | } |
8489 | #endif |
8490 | |
8491 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
8492 | verifyFormat( |
8493 | "void f() {\n" |
8494 | " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
8495 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
8496 | " f();\n" |
8497 | "}"); |
8498 | verifyFormat("if (Intervals[i].getRange().getFirst() <\n" |
8499 | " Intervals[i - 1].getRange().getLast()) {\n}"); |
8500 | } |
8501 | |
8502 | TEST_F(FormatTest, BreaksFunctionDeclarations) { |
8503 | // Principially, we break function declarations in a certain order: |
8504 | // 1) break amongst arguments. |
8505 | verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" |
8506 | " Cccccccccccccc cccccccccccccc);"); |
8507 | verifyFormat("template <class TemplateIt>\n" |
8508 | "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" |
8509 | " TemplateIt *stop) {}"); |
8510 | |
8511 | // 2) break after return type. |
8512 | verifyGoogleFormat( |
8513 | "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8514 | "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); |
8515 | |
8516 | // 3) break after (. |
8517 | verifyGoogleFormat( |
8518 | "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" |
8519 | " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); |
8520 | |
8521 | // 4) break before after nested name specifiers. |
8522 | verifyGoogleFormat( |
8523 | "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8524 | "SomeClasssssssssssssssssssssssssssssssssssssss::\n" |
8525 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); |
8526 | |
8527 | // However, there are exceptions, if a sufficient amount of lines can be |
8528 | // saved. |
8529 | // FIXME: The precise cut-offs wrt. the number of saved lines might need some |
8530 | // more adjusting. |
8531 | verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" |
8532 | " Cccccccccccccc cccccccccc,\n" |
8533 | " Cccccccccccccc cccccccccc,\n" |
8534 | " Cccccccccccccc cccccccccc,\n" |
8535 | " Cccccccccccccc cccccccccc);"); |
8536 | verifyGoogleFormat( |
8537 | "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8538 | "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
8539 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
8540 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); |
8541 | verifyFormat( |
8542 | "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" |
8543 | " Cccccccccccccc cccccccccc,\n" |
8544 | " Cccccccccccccc cccccccccc,\n" |
8545 | " Cccccccccccccc cccccccccc,\n" |
8546 | " Cccccccccccccc cccccccccc,\n" |
8547 | " Cccccccccccccc cccccccccc,\n" |
8548 | " Cccccccccccccc cccccccccc);"); |
8549 | verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" |
8550 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
8551 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
8552 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
8553 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); |
8554 | |
8555 | // Break after multi-line parameters. |
8556 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8557 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8558 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8559 | " bbbb bbbb);"); |
8560 | verifyFormat("void SomeLoooooooooooongFunction(\n" |
8561 | " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" |
8562 | " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8563 | " int bbbbbbbbbbbbb);"); |
8564 | |
8565 | // Treat overloaded operators like other functions. |
8566 | verifyFormat("SomeLoooooooooooooooooooooooooogType\n" |
8567 | "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); |
8568 | verifyFormat("SomeLoooooooooooooooooooooooooogType\n" |
8569 | "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); |
8570 | verifyFormat("SomeLoooooooooooooooooooooooooogType\n" |
8571 | "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); |
8572 | verifyGoogleFormat( |
8573 | "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" |
8574 | " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); |
8575 | verifyGoogleFormat( |
8576 | "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" |
8577 | " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); |
8578 | |
8579 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8580 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); |
8581 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" |
8582 | "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); |
8583 | verifyGoogleFormat( |
8584 | "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" |
8585 | "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8586 | " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); |
8587 | verifyGoogleFormat("template <typename T>\n" |
8588 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8589 | "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" |
8590 | " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); |
8591 | |
8592 | verifyFormat("extern \"C\" //\n" |
8593 | " void f();"); |
8594 | |
8595 | FormatStyle Style = getLLVMStyle(); |
8596 | Style.PointerAlignment = FormatStyle::PAS_Left; |
8597 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8598 | " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", |
8599 | Style); |
8600 | verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" |
8601 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
8602 | Style); |
8603 | } |
8604 | |
8605 | TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { |
8606 | // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: |
8607 | // Prefer keeping `::` followed by `operator` together. |
8608 | verifyFormat("const aaaa::bbbbbbb &\n" |
8609 | "ccccccccc::operator++() {\n" |
8610 | " stuff();\n" |
8611 | "}", |
8612 | "const aaaa::bbbbbbb\n" |
8613 | "&ccccccccc::operator++() { stuff(); }", |
8614 | getLLVMStyleWithColumns(40)); |
8615 | } |
8616 | |
8617 | TEST_F(FormatTest, TrailingReturnType) { |
8618 | verifyFormat("auto foo() -> int;"); |
8619 | // correct trailing return type spacing |
8620 | verifyFormat("auto operator->() -> int;"); |
8621 | verifyFormat("auto operator++(int) -> int;"); |
8622 | |
8623 | verifyFormat("struct S {\n" |
8624 | " auto bar() const -> int;\n" |
8625 | "};"); |
8626 | verifyFormat("template <size_t Order, typename T>\n" |
8627 | "auto load_img(const std::string &filename)\n" |
8628 | " -> alias::tensor<Order, T, mem::tag::cpu> {}"); |
8629 | verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" |
8630 | " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); |
8631 | verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); |
8632 | verifyFormat("template <typename T>\n" |
8633 | "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" |
8634 | " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); |
8635 | |
8636 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
8637 | verifyFormat("#define MAKE_DEF(NAME) \\\n" |
8638 | " auto NAME() -> int { return 42; }", |
8639 | Style); |
8640 | |
8641 | // Not trailing return types. |
8642 | verifyFormat("void f() { auto a = b->c(); }"); |
8643 | verifyFormat("auto a = p->foo();"); |
8644 | verifyFormat("int a = p->foo();"); |
8645 | verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); |
8646 | } |
8647 | |
8648 | TEST_F(FormatTest, DeductionGuides) { |
8649 | verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); |
8650 | verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); |
8651 | verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); |
8652 | verifyFormat( |
8653 | "template <class... T>\n" |
8654 | "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); |
8655 | verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); |
8656 | verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); |
8657 | verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); |
8658 | verifyFormat("template <class T> A() -> A<(3 < 2)>;"); |
8659 | verifyFormat("template <class T> A() -> A<((3) < (2))>;"); |
8660 | verifyFormat("template <class T> x() -> x<1>;"); |
8661 | verifyFormat("template <class T> explicit x(T &) -> x<1>;"); |
8662 | |
8663 | verifyFormat("A(const char *) -> A<string &>;"); |
8664 | verifyFormat("A() -> A<int>;"); |
8665 | |
8666 | // Ensure not deduction guides. |
8667 | verifyFormat("c()->f<int>();"); |
8668 | verifyFormat("x()->foo<1>;"); |
8669 | verifyFormat("x = p->foo<3>();"); |
8670 | verifyFormat("x()->x<1>();"); |
8671 | } |
8672 | |
8673 | TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { |
8674 | // Avoid breaking before trailing 'const' or other trailing annotations, if |
8675 | // they are not function-like. |
8676 | FormatStyle Style = getGoogleStyleWithColumns(ColumnLimit: 47); |
8677 | verifyFormat("void someLongFunction(\n" |
8678 | " int someLoooooooooooooongParameter) const {\n}", |
8679 | getLLVMStyleWithColumns(47)); |
8680 | verifyFormat("LoooooongReturnType\n" |
8681 | "someLoooooooongFunction() const {}", |
8682 | getLLVMStyleWithColumns(47)); |
8683 | verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" |
8684 | " const {}", |
8685 | Style); |
8686 | verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" |
8687 | " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); |
8688 | verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" |
8689 | " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); |
8690 | verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" |
8691 | " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); |
8692 | verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" |
8693 | " aaaaaaaaaaa aaaaa) const override;"); |
8694 | verifyGoogleFormat( |
8695 | "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
8696 | " const override;"); |
8697 | |
8698 | // Even if the first parameter has to be wrapped. |
8699 | verifyFormat("void someLongFunction(\n" |
8700 | " int someLongParameter) const {}", |
8701 | getLLVMStyleWithColumns(46)); |
8702 | verifyFormat("void someLongFunction(\n" |
8703 | " int someLongParameter) const {}", |
8704 | Style); |
8705 | verifyFormat("void someLongFunction(\n" |
8706 | " int someLongParameter) override {}", |
8707 | Style); |
8708 | verifyFormat("void someLongFunction(\n" |
8709 | " int someLongParameter) OVERRIDE {}", |
8710 | Style); |
8711 | verifyFormat("void someLongFunction(\n" |
8712 | " int someLongParameter) final {}", |
8713 | Style); |
8714 | verifyFormat("void someLongFunction(\n" |
8715 | " int someLongParameter) FINAL {}", |
8716 | Style); |
8717 | verifyFormat("void someLongFunction(\n" |
8718 | " int parameter) const override {}", |
8719 | Style); |
8720 | |
8721 | Style.BreakBeforeBraces = FormatStyle::BS_Allman; |
8722 | verifyFormat("void someLongFunction(\n" |
8723 | " int someLongParameter) const\n" |
8724 | "{\n" |
8725 | "}", |
8726 | Style); |
8727 | |
8728 | Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; |
8729 | verifyFormat("void someLongFunction(\n" |
8730 | " int someLongParameter) const\n" |
8731 | " {\n" |
8732 | " }", |
8733 | Style); |
8734 | |
8735 | // Unless these are unknown annotations. |
8736 | verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" |
8737 | " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
8738 | " LONG_AND_UGLY_ANNOTATION;"); |
8739 | |
8740 | // Breaking before function-like trailing annotations is fine to keep them |
8741 | // close to their arguments. |
8742 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
8743 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); |
8744 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
8745 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); |
8746 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
8747 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); |
8748 | verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" |
8749 | " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); |
8750 | verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); |
8751 | |
8752 | verifyFormat( |
8753 | "void aaaaaaaaaaaaaaaaaa()\n" |
8754 | " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" |
8755 | " aaaaaaaaaaaaaaaaaaaaaaaaa));"); |
8756 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8757 | " __attribute__((unused));"); |
8758 | |
8759 | Style = getGoogleStyle(); |
8760 | |
8761 | verifyFormat( |
8762 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8763 | " GUARDED_BY(aaaaaaaaaaaa);", |
8764 | Style); |
8765 | verifyFormat( |
8766 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8767 | " GUARDED_BY(aaaaaaaaaaaa);", |
8768 | Style); |
8769 | verifyFormat( |
8770 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" |
8771 | " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
8772 | Style); |
8773 | verifyFormat( |
8774 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" |
8775 | " aaaaaaaaaaaaaaaaaaaaaaaaa;", |
8776 | Style); |
8777 | |
8778 | verifyFormat( |
8779 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8780 | " ABSL_GUARDED_BY(aaaaaaaaaaaa);", |
8781 | Style); |
8782 | verifyFormat( |
8783 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8784 | " ABSL_GUARDED_BY(aaaaaaaaaaaa);", |
8785 | Style); |
8786 | verifyFormat( |
8787 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n" |
8788 | " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
8789 | Style); |
8790 | verifyFormat( |
8791 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n" |
8792 | " aaaaaaaaaaaaaaaaaaaaaaaaa;", |
8793 | Style); |
8794 | } |
8795 | |
8796 | TEST_F(FormatTest, FunctionAnnotations) { |
8797 | verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" |
8798 | "int OldFunction(const string ¶meter) {}"); |
8799 | verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" |
8800 | "string OldFunction(const string ¶meter) {}"); |
8801 | verifyFormat("template <typename T>\n" |
8802 | "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" |
8803 | "string OldFunction(const string ¶meter) {}"); |
8804 | |
8805 | // Not function annotations. |
8806 | verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
8807 | " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); |
8808 | verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" |
8809 | " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); |
8810 | verifyFormat("MACRO(abc).function() // wrap\n" |
8811 | " << abc;"); |
8812 | verifyFormat("MACRO(abc)->function() // wrap\n" |
8813 | " << abc;"); |
8814 | verifyFormat("MACRO(abc)::function() // wrap\n" |
8815 | " << abc;"); |
8816 | verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0)); |
8817 | } |
8818 | |
8819 | TEST_F(FormatTest, BreaksDesireably) { |
8820 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
8821 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
8822 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
8823 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8824 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
8825 | "}"); |
8826 | |
8827 | verifyFormat( |
8828 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8829 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
8830 | |
8831 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8832 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8833 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
8834 | |
8835 | verifyFormat( |
8836 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
8837 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8838 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
8839 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8840 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
8841 | |
8842 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
8843 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
8844 | |
8845 | verifyFormat( |
8846 | "void f() {\n" |
8847 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
8848 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
8849 | "}"); |
8850 | verifyFormat( |
8851 | "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8852 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
8853 | verifyFormat( |
8854 | "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8855 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
8856 | verifyFormat( |
8857 | "aaaaaa(aaa,\n" |
8858 | " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8859 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
8860 | " aaaa);"); |
8861 | verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
8862 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8863 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
8864 | |
8865 | // Indent consistently independent of call expression and unary operator. |
8866 | verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" |
8867 | " dddddddddddddddddddddddddddddd));"); |
8868 | verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" |
8869 | " dddddddddddddddddddddddddddddd));"); |
8870 | verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" |
8871 | " dddddddddddddddddddddddddddddd));"); |
8872 | |
8873 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
8874 | // taking into account the StopAt value. |
8875 | verifyFormat( |
8876 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
8877 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
8878 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
8879 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
8880 | |
8881 | verifyFormat("{\n {\n {\n" |
8882 | " Annotation.SpaceRequiredBefore =\n" |
8883 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
8884 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
8885 | " }\n }\n}"); |
8886 | |
8887 | // Break on an outer level if there was a break on an inner level. |
8888 | verifyFormat("f(g(h(a, // comment\n" |
8889 | " b, c),\n" |
8890 | " d, e),\n" |
8891 | " x, y);", |
8892 | "f(g(h(a, // comment\n" |
8893 | " b, c), d, e), x, y);"); |
8894 | |
8895 | // Prefer breaking similar line breaks. |
8896 | verifyFormat( |
8897 | "const int kTrackingOptions = NSTrackingMouseMoved |\n" |
8898 | " NSTrackingMouseEnteredAndExited |\n" |
8899 | " NSTrackingActiveAlways;"); |
8900 | } |
8901 | |
8902 | TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { |
8903 | FormatStyle NoBinPacking = getGoogleStyle(); |
8904 | NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
8905 | NoBinPacking.BinPackArguments = true; |
8906 | verifyFormat("void f() {\n" |
8907 | " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" |
8908 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
8909 | "}", |
8910 | NoBinPacking); |
8911 | verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" |
8912 | " int aaaaaaaaaaaaaaaaaaaa,\n" |
8913 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
8914 | NoBinPacking); |
8915 | |
8916 | NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; |
8917 | verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8918 | " vector<int> bbbbbbbbbbbbbbb);", |
8919 | NoBinPacking); |
8920 | // FIXME: This behavior difference is probably not wanted. However, currently |
8921 | // we cannot distinguish BreakBeforeParameter being set because of the wrapped |
8922 | // template arguments from BreakBeforeParameter being set because of the |
8923 | // one-per-line formatting. |
8924 | verifyFormat( |
8925 | "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
8926 | " aaaaaaaaaa> aaaaaaaaaa);", |
8927 | NoBinPacking); |
8928 | verifyFormat( |
8929 | "void fffffffffff(\n" |
8930 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" |
8931 | " aaaaaaaaaa);"); |
8932 | } |
8933 | |
8934 | TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { |
8935 | FormatStyle NoBinPacking = getGoogleStyle(); |
8936 | NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
8937 | NoBinPacking.BinPackArguments = false; |
8938 | verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" |
8939 | " aaaaaaaaaaaaaaaaaaaa,\n" |
8940 | " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", |
8941 | NoBinPacking); |
8942 | verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" |
8943 | " aaaaaaaaaaaaa,\n" |
8944 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", |
8945 | NoBinPacking); |
8946 | verifyFormat( |
8947 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
8948 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8949 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
8950 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8951 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", |
8952 | NoBinPacking); |
8953 | verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
8954 | " .aaaaaaaaaaaaaaaaaa();", |
8955 | NoBinPacking); |
8956 | verifyFormat("void f() {\n" |
8957 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
8958 | " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" |
8959 | "}", |
8960 | NoBinPacking); |
8961 | |
8962 | verifyFormat( |
8963 | "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
8964 | " aaaaaaaaaaaa,\n" |
8965 | " aaaaaaaaaaaa);", |
8966 | NoBinPacking); |
8967 | verifyFormat( |
8968 | "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" |
8969 | " ddddddddddddddddddddddddddddd),\n" |
8970 | " test);", |
8971 | NoBinPacking); |
8972 | |
8973 | verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
8974 | " aaaaaaaaaaaaaaaaaaaaaaa,\n" |
8975 | " aaaaaaaaaaaaaaaaaaaaaaa>\n" |
8976 | " aaaaaaaaaaaaaaaaaa;", |
8977 | NoBinPacking); |
8978 | verifyFormat("a(\"a\"\n" |
8979 | " \"a\",\n" |
8980 | " a);"); |
8981 | |
8982 | NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; |
8983 | verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" |
8984 | " aaaaaaaaa,\n" |
8985 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
8986 | NoBinPacking); |
8987 | verifyFormat( |
8988 | "void f() {\n" |
8989 | " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
8990 | " .aaaaaaa();\n" |
8991 | "}", |
8992 | NoBinPacking); |
8993 | verifyFormat( |
8994 | "template <class SomeType, class SomeOtherType>\n" |
8995 | "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", |
8996 | NoBinPacking); |
8997 | } |
8998 | |
8999 | TEST_F(FormatTest, FormatsDeclarationBreakAlways) { |
9000 | FormatStyle BreakAlways = getGoogleStyle(); |
9001 | BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; |
9002 | verifyFormat("void f(int a,\n" |
9003 | " int b);", |
9004 | BreakAlways); |
9005 | verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9006 | " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
9007 | " int cccccccccccccccccccccccc);", |
9008 | BreakAlways); |
9009 | |
9010 | // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set |
9011 | // to BPPS_AlwaysOnePerLine. |
9012 | BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
9013 | verifyFormat( |
9014 | "void someLongFunctionName(\n" |
9015 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9016 | " int b);", |
9017 | BreakAlways); |
9018 | BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
9019 | verifyFormat( |
9020 | "void someLongFunctionName(\n" |
9021 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9022 | " int b\n" |
9023 | ");", |
9024 | BreakAlways); |
9025 | } |
9026 | |
9027 | TEST_F(FormatTest, FormatsDefinitionBreakAlways) { |
9028 | FormatStyle BreakAlways = getGoogleStyle(); |
9029 | BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; |
9030 | verifyFormat("void f(int a,\n" |
9031 | " int b) {\n" |
9032 | " f(a, b);\n" |
9033 | "}", |
9034 | BreakAlways); |
9035 | |
9036 | // Ensure BinPackArguments interact correctly when BinPackParameters is set to |
9037 | // BPPS_AlwaysOnePerLine. |
9038 | verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9039 | " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
9040 | " int cccccccccccccccccccccccc) {\n" |
9041 | " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
9042 | " cccccccccccccccccccccccc);\n" |
9043 | "}", |
9044 | BreakAlways); |
9045 | BreakAlways.BinPackArguments = false; |
9046 | verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9047 | " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
9048 | " int cccccccccccccccccccccccc) {\n" |
9049 | " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9050 | " bbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
9051 | " cccccccccccccccccccccccc);\n" |
9052 | "}", |
9053 | BreakAlways); |
9054 | |
9055 | // Ensure BreakFunctionDefinitionParameters interacts correctly when |
9056 | // BinPackParameters is set to BPPS_AlwaysOnePerLine. |
9057 | BreakAlways.BreakFunctionDefinitionParameters = true; |
9058 | verifyFormat("void f(\n" |
9059 | " int a,\n" |
9060 | " int b) {\n" |
9061 | " f(a, b);\n" |
9062 | "}", |
9063 | BreakAlways); |
9064 | BreakAlways.BreakFunctionDefinitionParameters = false; |
9065 | |
9066 | // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set |
9067 | // to BPPS_AlwaysOnePerLine. |
9068 | BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
9069 | verifyFormat( |
9070 | "void someLongFunctionName(\n" |
9071 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9072 | " int b) {\n" |
9073 | " someLongFunctionName(\n" |
9074 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n" |
9075 | "}", |
9076 | BreakAlways); |
9077 | BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
9078 | verifyFormat( |
9079 | "void someLongFunctionName(\n" |
9080 | " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9081 | " int b\n" |
9082 | ") {\n" |
9083 | " someLongFunctionName(\n" |
9084 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n" |
9085 | " );\n" |
9086 | "}", |
9087 | BreakAlways); |
9088 | } |
9089 | |
9090 | TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { |
9091 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 15); |
9092 | Style.ExperimentalAutoDetectBinPacking = true; |
9093 | verifyFormat("aaa(aaaa,\n" |
9094 | " aaaa,\n" |
9095 | " aaaa);\n" |
9096 | "aaa(aaaa,\n" |
9097 | " aaaa,\n" |
9098 | " aaaa);", |
9099 | "aaa(aaaa,\n"// one-per-line |
9100 | " aaaa,\n" |
9101 | " aaaa );\n" |
9102 | "aaa(aaaa, aaaa, aaaa);", // inconclusive |
9103 | Style); |
9104 | verifyFormat("aaa(aaaa, aaaa,\n" |
9105 | " aaaa);\n" |
9106 | "aaa(aaaa, aaaa,\n" |
9107 | " aaaa);", |
9108 | "aaa(aaaa, aaaa,\n"// bin-packed |
9109 | " aaaa );\n" |
9110 | "aaa(aaaa, aaaa, aaaa);", // inconclusive |
9111 | Style); |
9112 | } |
9113 | |
9114 | TEST_F(FormatTest, IndentExportBlock) { |
9115 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80); |
9116 | Style.IndentExportBlock = true; |
9117 | verifyFormat("export {\n" |
9118 | " int x;\n" |
9119 | " int y;\n" |
9120 | "}", |
9121 | "export {\n" |
9122 | "int x;\n" |
9123 | "int y;\n" |
9124 | "}", |
9125 | Style); |
9126 | |
9127 | Style.IndentExportBlock = false; |
9128 | verifyFormat("export {\n" |
9129 | "int x;\n" |
9130 | "int y;\n" |
9131 | "}", |
9132 | "export {\n" |
9133 | " int x;\n" |
9134 | " int y;\n" |
9135 | "}", |
9136 | Style); |
9137 | } |
9138 | |
9139 | TEST_F(FormatTest, ShortExportBlocks) { |
9140 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80); |
9141 | Style.IndentExportBlock = false; |
9142 | |
9143 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; |
9144 | verifyFormat("export {\n" |
9145 | "}", |
9146 | Style); |
9147 | |
9148 | verifyFormat("export {\n" |
9149 | "int x;\n" |
9150 | "}", |
9151 | Style); |
9152 | |
9153 | verifyFormat("export {\n" |
9154 | "int x;\n" |
9155 | "}", |
9156 | "export\n" |
9157 | "{\n" |
9158 | "int x;\n" |
9159 | "}", |
9160 | Style); |
9161 | |
9162 | verifyFormat("export {\n" |
9163 | "}", |
9164 | "export {}", Style); |
9165 | |
9166 | verifyFormat("export {\n" |
9167 | "int x;\n" |
9168 | "}", |
9169 | "export { int x; }", Style); |
9170 | |
9171 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
9172 | verifyFormat("export {}", |
9173 | "export {\n" |
9174 | "}", |
9175 | Style); |
9176 | |
9177 | verifyFormat("export { int x; }", |
9178 | "export {\n" |
9179 | "int x;\n" |
9180 | "}", |
9181 | Style); |
9182 | |
9183 | verifyFormat("export { int x; }", |
9184 | "export\n" |
9185 | "{\n" |
9186 | "int x;\n" |
9187 | "}", |
9188 | Style); |
9189 | |
9190 | verifyFormat("export {}", |
9191 | "export {\n" |
9192 | "}", |
9193 | Style); |
9194 | |
9195 | verifyFormat("export { int x; }", |
9196 | "export {\n" |
9197 | "int x;\n" |
9198 | "}", |
9199 | Style); |
9200 | |
9201 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; |
9202 | verifyFormat("export {}", |
9203 | "export {\n" |
9204 | "}", |
9205 | Style); |
9206 | |
9207 | verifyFormat("export {\n" |
9208 | "int x;\n" |
9209 | "}", |
9210 | Style); |
9211 | |
9212 | verifyFormat("export {\n" |
9213 | "int x;\n" |
9214 | "}", |
9215 | "export\n" |
9216 | "{\n" |
9217 | "int x;\n" |
9218 | "}", |
9219 | Style); |
9220 | |
9221 | verifyFormat("export {}", Style); |
9222 | |
9223 | verifyFormat("export {\n" |
9224 | "int x;\n" |
9225 | "}", |
9226 | "export { int x; }", Style); |
9227 | } |
9228 | |
9229 | TEST_F(FormatTest, FormatsBuilderPattern) { |
9230 | verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" |
9231 | " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" |
9232 | " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" |
9233 | " .StartsWith(\".init\", ORDER_INIT)\n" |
9234 | " .StartsWith(\".fini\", ORDER_FINI)\n" |
9235 | " .StartsWith(\".hash\", ORDER_HASH)\n" |
9236 | " .Default(ORDER_TEXT);"); |
9237 | |
9238 | verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" |
9239 | " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); |
9240 | verifyFormat("aaaaaaa->aaaaaaa\n" |
9241 | " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9242 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9243 | " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); |
9244 | verifyFormat( |
9245 | "aaaaaaa->aaaaaaa\n" |
9246 | " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9247 | " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); |
9248 | verifyFormat( |
9249 | "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" |
9250 | " aaaaaaaaaaaaaa);"); |
9251 | verifyFormat( |
9252 | "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" |
9253 | " aaaaaa->aaaaaaaaaaaa()\n" |
9254 | " ->aaaaaaaaaaaaaaaa(\n" |
9255 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9256 | " ->aaaaaaaaaaaaaaaaa();"); |
9257 | verifyGoogleFormat( |
9258 | "void f() {\n" |
9259 | " someo->Add((new util::filetools::Handler(dir))\n" |
9260 | " ->OnEvent1(NewPermanentCallback(\n" |
9261 | " this, &HandlerHolderClass::EventHandlerCBA))\n" |
9262 | " ->OnEvent2(NewPermanentCallback(\n" |
9263 | " this, &HandlerHolderClass::EventHandlerCBB))\n" |
9264 | " ->OnEvent3(NewPermanentCallback(\n" |
9265 | " this, &HandlerHolderClass::EventHandlerCBC))\n" |
9266 | " ->OnEvent5(NewPermanentCallback(\n" |
9267 | " this, &HandlerHolderClass::EventHandlerCBD))\n" |
9268 | " ->OnEvent6(NewPermanentCallback(\n" |
9269 | " this, &HandlerHolderClass::EventHandlerCBE)));\n" |
9270 | "}"); |
9271 | |
9272 | verifyFormat( |
9273 | "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); |
9274 | verifyFormat("aaaaaaaaaaaaaaa()\n" |
9275 | " .aaaaaaaaaaaaaaa()\n" |
9276 | " .aaaaaaaaaaaaaaa()\n" |
9277 | " .aaaaaaaaaaaaaaa()\n" |
9278 | " .aaaaaaaaaaaaaaa();"); |
9279 | verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" |
9280 | " .aaaaaaaaaaaaaaa()\n" |
9281 | " .aaaaaaaaaaaaaaa()\n" |
9282 | " .aaaaaaaaaaaaaaa();"); |
9283 | verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" |
9284 | " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" |
9285 | " .aaaaaaaaaaaaaaa();"); |
9286 | verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9287 | " ->aaaaaaaaaaaaaae(0)\n" |
9288 | " ->aaaaaaaaaaaaaaa();"); |
9289 | |
9290 | // Don't linewrap after very short segments. |
9291 | verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9292 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9293 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
9294 | verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9295 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9296 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
9297 | verifyFormat("aaa()\n" |
9298 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9299 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9300 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
9301 | |
9302 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" |
9303 | " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
9304 | " .has<bbbbbbbbbbbbbbbbbbbbb>();"); |
9305 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" |
9306 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" |
9307 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); |
9308 | |
9309 | // Prefer not to break after empty parentheses. |
9310 | verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" |
9311 | " First->LastNewlineOffset);"); |
9312 | |
9313 | // Prefer not to create "hanging" indents. |
9314 | verifyFormat( |
9315 | "return !soooooooooooooome_map\n" |
9316 | " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9317 | " .second;"); |
9318 | verifyFormat( |
9319 | "return aaaaaaaaaaaaaaaa\n" |
9320 | " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" |
9321 | " .aaaa(aaaaaaaaaaaaaa);"); |
9322 | // No hanging indent here. |
9323 | verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" |
9324 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9325 | verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" |
9326 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9327 | verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" |
9328 | " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
9329 | getLLVMStyleWithColumns(60)); |
9330 | verifyFormat("aaaaaaaaaaaaaaaaaa\n" |
9331 | " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" |
9332 | " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
9333 | getLLVMStyleWithColumns(59)); |
9334 | verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9335 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9336 | " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9337 | |
9338 | // Dont break if only closing statements before member call |
9339 | verifyFormat("test() {\n" |
9340 | " ([]() -> {\n" |
9341 | " int b = 32;\n" |
9342 | " return 3;\n" |
9343 | " }).foo();\n" |
9344 | "}"); |
9345 | verifyFormat("test() {\n" |
9346 | " (\n" |
9347 | " []() -> {\n" |
9348 | " int b = 32;\n" |
9349 | " return 3;\n" |
9350 | " },\n" |
9351 | " foo, bar)\n" |
9352 | " .foo();\n" |
9353 | "}"); |
9354 | verifyFormat("test() {\n" |
9355 | " ([]() -> {\n" |
9356 | " int b = 32;\n" |
9357 | " return 3;\n" |
9358 | " })\n" |
9359 | " .foo()\n" |
9360 | " .bar();\n" |
9361 | "}"); |
9362 | verifyFormat("test() {\n" |
9363 | " ([]() -> {\n" |
9364 | " int b = 32;\n" |
9365 | " return 3;\n" |
9366 | " })\n" |
9367 | " .foo(\"aaaaaaaaaaaaaaaaa\"\n" |
9368 | " \"bbbb\");\n" |
9369 | "}", |
9370 | getLLVMStyleWithColumns(30)); |
9371 | } |
9372 | |
9373 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
9374 | verifyFormat( |
9375 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
9376 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
9377 | verifyFormat( |
9378 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" |
9379 | " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); |
9380 | |
9381 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
9382 | " ccccccccccccccccccccccccc) {\n}"); |
9383 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" |
9384 | " ccccccccccccccccccccccccc) {\n}"); |
9385 | |
9386 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
9387 | " ccccccccccccccccccccccccc) {\n}"); |
9388 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" |
9389 | " ccccccccccccccccccccccccc) {\n}"); |
9390 | |
9391 | verifyFormat( |
9392 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
9393 | " ccccccccccccccccccccccccc) {\n}"); |
9394 | verifyFormat( |
9395 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" |
9396 | " ccccccccccccccccccccccccc) {\n}"); |
9397 | |
9398 | verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" |
9399 | " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" |
9400 | " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" |
9401 | " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); |
9402 | verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" |
9403 | " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" |
9404 | " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" |
9405 | " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); |
9406 | |
9407 | verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" |
9408 | " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" |
9409 | " aaaaaaaaaaaaaaa != aa) {\n}"); |
9410 | verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" |
9411 | " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" |
9412 | " aaaaaaaaaaaaaaa != aa) {\n}"); |
9413 | } |
9414 | |
9415 | TEST_F(FormatTest, BreaksAfterAssignments) { |
9416 | verifyFormat( |
9417 | "unsigned Cost =\n" |
9418 | " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" |
9419 | " SI->getPointerAddressSpaceee());"); |
9420 | verifyFormat( |
9421 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
9422 | " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); |
9423 | |
9424 | verifyFormat( |
9425 | "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" |
9426 | " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); |
9427 | verifyFormat("unsigned OriginalStartColumn =\n" |
9428 | " SourceMgr.getSpellingColumnNumber(\n" |
9429 | " Current.FormatTok.getStartOfNonWhitespace()) -\n" |
9430 | " 1;"); |
9431 | } |
9432 | |
9433 | TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { |
9434 | FormatStyle Style = getLLVMStyle(); |
9435 | verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
9436 | " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", |
9437 | Style); |
9438 | |
9439 | Style.PenaltyBreakAssignment = 20; |
9440 | verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" |
9441 | " cccccccccccccccccccccccccc;", |
9442 | Style); |
9443 | } |
9444 | |
9445 | TEST_F(FormatTest, AlignsAfterAssignments) { |
9446 | verifyFormat( |
9447 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9448 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9449 | verifyFormat( |
9450 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9451 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9452 | verifyFormat( |
9453 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9454 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9455 | verifyFormat( |
9456 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9457 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9458 | verifyFormat( |
9459 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9460 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9461 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
9462 | } |
9463 | |
9464 | TEST_F(FormatTest, AlignsAfterReturn) { |
9465 | verifyFormat( |
9466 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9467 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9468 | verifyFormat( |
9469 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9470 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9471 | verifyFormat( |
9472 | "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" |
9473 | " aaaaaaaaaaaaaaaaaaaaaa();"); |
9474 | verifyFormat( |
9475 | "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" |
9476 | " aaaaaaaaaaaaaaaaaaaaaa());"); |
9477 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9478 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9479 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9480 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" |
9481 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9482 | verifyFormat("return\n" |
9483 | " // true if code is one of a or b.\n" |
9484 | " code == a || code == b;"); |
9485 | } |
9486 | |
9487 | TEST_F(FormatTest, AlignsAfterOpenBracket) { |
9488 | verifyFormat( |
9489 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" |
9490 | " aaaaaaaaa aaaaaaa) {}"); |
9491 | verifyFormat( |
9492 | "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" |
9493 | " aaaaaaaaaaa aaaaaaaaa);"); |
9494 | verifyFormat( |
9495 | "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" |
9496 | " aaaaaaaaaaaaaaaaaaaaa));"); |
9497 | FormatStyle Style = getLLVMStyle(); |
9498 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
9499 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9500 | " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", |
9501 | Style); |
9502 | verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" |
9503 | " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", |
9504 | Style); |
9505 | verifyFormat("SomeLongVariableName->someFunction(\n" |
9506 | " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", |
9507 | Style); |
9508 | verifyFormat( |
9509 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" |
9510 | " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
9511 | Style); |
9512 | verifyFormat( |
9513 | "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" |
9514 | " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
9515 | Style); |
9516 | verifyFormat( |
9517 | "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" |
9518 | " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", |
9519 | Style); |
9520 | |
9521 | verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" |
9522 | " ccccccc(aaaaaaaaaaaaaaaaa, //\n" |
9523 | " b));", |
9524 | Style); |
9525 | |
9526 | Style.ColumnLimit = 30; |
9527 | verifyFormat("for (int foo = 0; foo < FOO;\n" |
9528 | " ++foo) {\n" |
9529 | " bar(foo);\n" |
9530 | "}", |
9531 | Style); |
9532 | Style.ColumnLimit = 80; |
9533 | |
9534 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
9535 | Style.BinPackArguments = false; |
9536 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
9537 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9538 | " aaaaaaaaaaa aaaaaaaa,\n" |
9539 | " aaaaaaaaa aaaaaaa,\n" |
9540 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", |
9541 | Style); |
9542 | verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" |
9543 | " aaaaaaaaaaa aaaaaaaaa,\n" |
9544 | " aaaaaaaaaaa aaaaaaaaa,\n" |
9545 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
9546 | Style); |
9547 | verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" |
9548 | " aaaaaaaaaaaaaaa,\n" |
9549 | " aaaaaaaaaaaaaaaaaaaaa,\n" |
9550 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", |
9551 | Style); |
9552 | verifyFormat( |
9553 | "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" |
9554 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", |
9555 | Style); |
9556 | verifyFormat( |
9557 | "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" |
9558 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", |
9559 | Style); |
9560 | verifyFormat( |
9561 | "aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9562 | " aaaaaaaaaaaaaaaaaaaaa(\n" |
9563 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" |
9564 | " aaaaaaaaaaaaaaaa);", |
9565 | Style); |
9566 | verifyFormat( |
9567 | "aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9568 | " aaaaaaaaaaaaaaaaaaaaa(\n" |
9569 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" |
9570 | " aaaaaaaaaaaaaaaa);", |
9571 | Style); |
9572 | verifyFormat( |
9573 | "fooooooooooo(new BARRRRRRRRR(\n" |
9574 | " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", |
9575 | Style); |
9576 | verifyFormat( |
9577 | "fooooooooooo(::new BARRRRRRRRR(\n" |
9578 | " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", |
9579 | Style); |
9580 | verifyFormat( |
9581 | "fooooooooooo(new FOO::BARRRR(\n" |
9582 | " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", |
9583 | Style); |
9584 | |
9585 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
9586 | Style.BinPackArguments = false; |
9587 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
9588 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9589 | " aaaaaaaaaaa aaaaaaaa,\n" |
9590 | " aaaaaaaaa aaaaaaa,\n" |
9591 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9592 | ") {}", |
9593 | Style); |
9594 | verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" |
9595 | " aaaaaaaaaaa aaaaaaaaa,\n" |
9596 | " aaaaaaaaaaa aaaaaaaaa,\n" |
9597 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9598 | ");", |
9599 | Style); |
9600 | verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" |
9601 | " aaaaaaaaaaaaaaa,\n" |
9602 | " aaaaaaaaaaaaaaaaaaaaa,\n" |
9603 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9604 | "));", |
9605 | Style); |
9606 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" |
9607 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" |
9608 | "));", |
9609 | Style); |
9610 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" |
9611 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" |
9612 | "));", |
9613 | Style); |
9614 | verifyFormat( |
9615 | "aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9616 | " aaaaaaaaaaaaaaaaaaaaa(\n" |
9617 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" |
9618 | " ),\n" |
9619 | " aaaaaaaaaaaaaaaa\n" |
9620 | ");", |
9621 | Style); |
9622 | verifyFormat( |
9623 | "aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9624 | " aaaaaaaaaaaaaaaaaaaaa(\n" |
9625 | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" |
9626 | " ) &&\n" |
9627 | " aaaaaaaaaaaaaaaa\n" |
9628 | ");", |
9629 | Style); |
9630 | verifyFormat("void foo(\n" |
9631 | " void (*foobarpntr)(\n" |
9632 | " aaaaaaaaaaaaaaaaaa *,\n" |
9633 | " bbbbbbbbbbbbbb *,\n" |
9634 | " cccccccccccccccccccc *,\n" |
9635 | " dddddddddddddddddd *\n" |
9636 | " )\n" |
9637 | ");", |
9638 | Style); |
9639 | verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n" |
9640 | " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" |
9641 | "};", |
9642 | Style); |
9643 | |
9644 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9645 | " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n" |
9646 | ") const {\n" |
9647 | " return true;\n" |
9648 | "}", |
9649 | Style); |
9650 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9651 | " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n" |
9652 | ") const;", |
9653 | Style); |
9654 | verifyFormat("void aaaaaaaaa(\n" |
9655 | " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n" |
9656 | ") const noexcept -> std::vector<of_very_long_type>;", |
9657 | Style); |
9658 | verifyFormat( |
9659 | "x = aaaaaaaaaaaaaaa(\n" |
9660 | " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n" |
9661 | ");", |
9662 | Style); |
9663 | Style.ColumnLimit = 60; |
9664 | verifyFormat("auto lambda =\n" |
9665 | " [&b](\n" |
9666 | " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9667 | " ) {};", |
9668 | Style); |
9669 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9670 | " &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" |
9671 | ");", |
9672 | Style); |
9673 | } |
9674 | |
9675 | TEST_F(FormatTest, ParenthesesAndOperandAlignment) { |
9676 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
9677 | verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" |
9678 | " bbbbbbbbbbbbbbbbbbbbbb);", |
9679 | Style); |
9680 | Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; |
9681 | Style.AlignOperands = FormatStyle::OAS_DontAlign; |
9682 | verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" |
9683 | " bbbbbbbbbbbbbbbbbbbbbb);", |
9684 | Style); |
9685 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
9686 | Style.AlignOperands = FormatStyle::OAS_Align; |
9687 | verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" |
9688 | " bbbbbbbbbbbbbbbbbbbbbb);", |
9689 | Style); |
9690 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
9691 | Style.AlignOperands = FormatStyle::OAS_DontAlign; |
9692 | verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" |
9693 | " bbbbbbbbbbbbbbbbbbbbbb);", |
9694 | Style); |
9695 | } |
9696 | |
9697 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
9698 | verifyFormat( |
9699 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9700 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9701 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9702 | verifyFormat( |
9703 | "aaaa(aaaaaaaaaa, aaaaaaaa,\n" |
9704 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9705 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9706 | verifyFormat( |
9707 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9708 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9709 | verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" |
9710 | " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9711 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9712 | verifyFormat( |
9713 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" |
9714 | " : aaaaaaaaaaaaa);"); |
9715 | verifyFormat( |
9716 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9717 | " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9718 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9719 | " aaaaaaaaaaaaa);"); |
9720 | verifyFormat( |
9721 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9722 | " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9723 | " aaaaaaaaaaaaa);"); |
9724 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9725 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9726 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9727 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9728 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9729 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9730 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9731 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9732 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
9733 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9734 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
9735 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9736 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
9737 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9738 | " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
9739 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
9740 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
9741 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9742 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9743 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9744 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" |
9745 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9746 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9747 | " : aaaaaaaaaaaaaaaa;"); |
9748 | verifyFormat( |
9749 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9750 | " ? aaaaaaaaaaaaaaa\n" |
9751 | " : aaaaaaaaaaaaaaa;"); |
9752 | verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" |
9753 | " aaaaaaaaa\n" |
9754 | " ? b\n" |
9755 | " : c);"); |
9756 | verifyFormat("return aaaa == bbbb\n" |
9757 | " // comment\n" |
9758 | " ? aaaa\n" |
9759 | " : bbbb;"); |
9760 | verifyFormat("unsigned Indent =\n" |
9761 | " format(TheLine.First,\n" |
9762 | " IndentForLevel[TheLine.Level] >= 0\n" |
9763 | " ? IndentForLevel[TheLine.Level]\n" |
9764 | " : TheLine * 2,\n" |
9765 | " TheLine.InPPDirective, PreviousEndOfLineColumn);", |
9766 | getLLVMStyleWithColumns(60)); |
9767 | verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" |
9768 | " ? aaaaaaaaaaaaaaa\n" |
9769 | " : bbbbbbbbbbbbbbb //\n" |
9770 | " ? ccccccccccccccc\n" |
9771 | " : ddddddddddddddd;"); |
9772 | verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" |
9773 | " ? aaaaaaaaaaaaaaa\n" |
9774 | " : (bbbbbbbbbbbbbbb //\n" |
9775 | " ? ccccccccccccccc\n" |
9776 | " : ddddddddddddddd);"); |
9777 | verifyFormat( |
9778 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9779 | " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
9780 | " aaaaaaaaaaaaaaaaaaaaa +\n" |
9781 | " aaaaaaaaaaaaaaaaaaaaa\n" |
9782 | " : aaaaaaaaaa;"); |
9783 | verifyFormat( |
9784 | "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9785 | " : aaaaaaaaaaaaaaaaaaaaaa\n" |
9786 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
9787 | |
9788 | FormatStyle NoBinPacking = getLLVMStyle(); |
9789 | NoBinPacking.BinPackArguments = false; |
9790 | verifyFormat( |
9791 | "void f() {\n" |
9792 | " g(aaa,\n" |
9793 | " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" |
9794 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9795 | " ? aaaaaaaaaaaaaaa\n" |
9796 | " : aaaaaaaaaaaaaaa);\n" |
9797 | "}", |
9798 | NoBinPacking); |
9799 | verifyFormat( |
9800 | "void f() {\n" |
9801 | " g(aaa,\n" |
9802 | " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" |
9803 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9804 | " ?: aaaaaaaaaaaaaaa);\n" |
9805 | "}", |
9806 | NoBinPacking); |
9807 | |
9808 | verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" |
9809 | " // comment.\n" |
9810 | " ccccccccccccccccccccccccccccccccccccccc\n" |
9811 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9812 | " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); |
9813 | |
9814 | // Assignments in conditional expressions. Apparently not uncommon :-(. |
9815 | verifyFormat("return a != b\n" |
9816 | " // comment\n" |
9817 | " ? a = b\n" |
9818 | " : a = b;"); |
9819 | verifyFormat("return a != b\n" |
9820 | " // comment\n" |
9821 | " ? a = a != b\n" |
9822 | " // comment\n" |
9823 | " ? a = b\n" |
9824 | " : a\n" |
9825 | " : a;"); |
9826 | verifyFormat("return a != b\n" |
9827 | " // comment\n" |
9828 | " ? a\n" |
9829 | " : a = a != b\n" |
9830 | " // comment\n" |
9831 | " ? a = b\n" |
9832 | " : a;"); |
9833 | |
9834 | // Chained conditionals |
9835 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 70); |
9836 | Style.AlignOperands = FormatStyle::OAS_Align; |
9837 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
9838 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9839 | " : 3333333333333333;", |
9840 | Style); |
9841 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
9842 | " : bbbbbbbbbb ? 2222222222222222\n" |
9843 | " : 3333333333333333;", |
9844 | Style); |
9845 | verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" |
9846 | " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" |
9847 | " : 3333333333333333;", |
9848 | Style); |
9849 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
9850 | " : bbbbbbbbbbbbbb ? 222222\n" |
9851 | " : 333333;", |
9852 | Style); |
9853 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
9854 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9855 | " : cccccccccccccc ? 3333333333333333\n" |
9856 | " : 4444444444444444;", |
9857 | Style); |
9858 | verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" |
9859 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9860 | " : 3333333333333333;", |
9861 | Style); |
9862 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
9863 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9864 | " : (aaa ? bbb : ccc);", |
9865 | Style); |
9866 | verifyFormat( |
9867 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9868 | " : cccccccccccccccccc)\n" |
9869 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9870 | " : 3333333333333333;", |
9871 | Style); |
9872 | verifyFormat( |
9873 | "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9874 | " : cccccccccccccccccc)\n" |
9875 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9876 | " : 3333333333333333;", |
9877 | Style); |
9878 | verifyFormat( |
9879 | "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9880 | " : dddddddddddddddddd)\n" |
9881 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9882 | " : 3333333333333333;", |
9883 | Style); |
9884 | verifyFormat( |
9885 | "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9886 | " : dddddddddddddddddd)\n" |
9887 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9888 | " : 3333333333333333;", |
9889 | Style); |
9890 | verifyFormat( |
9891 | "return aaaaaaaaa ? 1111111111111111\n" |
9892 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9893 | " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9894 | " : dddddddddddddddddd)", |
9895 | Style); |
9896 | verifyFormat( |
9897 | "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" |
9898 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9899 | " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9900 | " : cccccccccccccccccc);", |
9901 | Style); |
9902 | verifyFormat( |
9903 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9904 | " : ccccccccccccccc ? dddddddddddddddddd\n" |
9905 | " : eeeeeeeeeeeeeeeeee)\n" |
9906 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9907 | " : 3333333333333333;", |
9908 | Style); |
9909 | verifyFormat( |
9910 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9911 | " : ccccccccccccccc ? dddddddddddddddddd\n" |
9912 | " : eeeeeeeeeeeeeeeeee)\n" |
9913 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9914 | " : 3333333333333333;", |
9915 | Style); |
9916 | verifyFormat( |
9917 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9918 | " : cccccccccccc ? dddddddddddddddddd\n" |
9919 | " : eeeeeeeeeeeeeeeeee)\n" |
9920 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9921 | " : 3333333333333333;", |
9922 | Style); |
9923 | verifyFormat( |
9924 | "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9925 | " : cccccccccccccccccc\n" |
9926 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9927 | " : 3333333333333333;", |
9928 | Style); |
9929 | verifyFormat( |
9930 | "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9931 | " : cccccccccccccccc ? dddddddddddddddddd\n" |
9932 | " : eeeeeeeeeeeeeeeeee\n" |
9933 | " : bbbbbbbbbbbbbb ? 2222222222222222\n" |
9934 | " : 3333333333333333;", |
9935 | Style); |
9936 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" |
9937 | " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9938 | " : cccccccccccccccccc ? dddddddddddddddddd\n" |
9939 | " : eeeeeeeeeeeeeeeeee)\n" |
9940 | " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" |
9941 | " : 3333333333333333;", |
9942 | Style); |
9943 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" |
9944 | " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" |
9945 | " : cccccccccccccccc ? dddddddddddddddddd\n" |
9946 | " : eeeeeeeeeeeeeeeeee\n" |
9947 | " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" |
9948 | " : 3333333333333333;", |
9949 | Style); |
9950 | |
9951 | Style.AlignOperands = FormatStyle::OAS_DontAlign; |
9952 | Style.BreakBeforeTernaryOperators = false; |
9953 | // FIXME: Aligning the question marks is weird given DontAlign. |
9954 | // Consider disabling this alignment in this case. Also check whether this |
9955 | // will render the adjustment from https://reviews.llvm.org/D82199 |
9956 | // unnecessary. |
9957 | verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" |
9958 | " bbbb ? cccccccccccccccccc :\n" |
9959 | " ddddd;", |
9960 | Style); |
9961 | |
9962 | verifyFormat( |
9963 | "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" |
9964 | " /*\n" |
9965 | " */\n" |
9966 | " function() {\n" |
9967 | " try {\n" |
9968 | " return JJJJJJJJJJJJJJ(\n" |
9969 | " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" |
9970 | " }\n" |
9971 | " } :\n" |
9972 | " function() {};", |
9973 | "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" |
9974 | " /*\n" |
9975 | " */\n" |
9976 | " function() {\n" |
9977 | " try {\n" |
9978 | " return JJJJJJJJJJJJJJ(\n" |
9979 | " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" |
9980 | " }\n" |
9981 | " } :\n" |
9982 | " function() {};", |
9983 | getGoogleStyle(FormatStyle::LK_JavaScript)); |
9984 | } |
9985 | |
9986 | TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { |
9987 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 70); |
9988 | Style.BreakBeforeTernaryOperators = false; |
9989 | verifyFormat( |
9990 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
9991 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
9992 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
9993 | Style); |
9994 | verifyFormat( |
9995 | "aaaa(aaaaaaaaaa, aaaaaaaa,\n" |
9996 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
9997 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
9998 | Style); |
9999 | verifyFormat( |
10000 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
10001 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
10002 | Style); |
10003 | verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" |
10004 | " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
10005 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
10006 | Style); |
10007 | verifyFormat( |
10008 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" |
10009 | " aaaaaaaaaaaaa);", |
10010 | Style); |
10011 | verifyFormat( |
10012 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10013 | " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
10014 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10015 | " aaaaaaaaaaaaa);", |
10016 | Style); |
10017 | verifyFormat( |
10018 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10019 | " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10020 | " aaaaaaaaaaaaa);", |
10021 | Style); |
10022 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10023 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10024 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" |
10025 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10026 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
10027 | Style); |
10028 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10029 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10030 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10031 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" |
10032 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10033 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
10034 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
10035 | Style); |
10036 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10037 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" |
10038 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10039 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
10040 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
10041 | Style); |
10042 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10043 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
10044 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
10045 | Style); |
10046 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" |
10047 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10048 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
10049 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
10050 | Style); |
10051 | verifyFormat( |
10052 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10053 | " aaaaaaaaaaaaaaa :\n" |
10054 | " aaaaaaaaaaaaaaa;", |
10055 | Style); |
10056 | verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" |
10057 | " aaaaaaaaa ?\n" |
10058 | " b :\n" |
10059 | " c);", |
10060 | Style); |
10061 | verifyFormat("unsigned Indent =\n" |
10062 | " format(TheLine.First,\n" |
10063 | " IndentForLevel[TheLine.Level] >= 0 ?\n" |
10064 | " IndentForLevel[TheLine.Level] :\n" |
10065 | " TheLine * 2,\n" |
10066 | " TheLine.InPPDirective, PreviousEndOfLineColumn);", |
10067 | Style); |
10068 | verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" |
10069 | " aaaaaaaaaaaaaaa :\n" |
10070 | " bbbbbbbbbbbbbbb ? //\n" |
10071 | " ccccccccccccccc :\n" |
10072 | " ddddddddddddddd;", |
10073 | Style); |
10074 | verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" |
10075 | " aaaaaaaaaaaaaaa :\n" |
10076 | " (bbbbbbbbbbbbbbb ? //\n" |
10077 | " ccccccccccccccc :\n" |
10078 | " ddddddddddddddd);", |
10079 | Style); |
10080 | verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10081 | " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" |
10082 | " ccccccccccccccccccccccccccc;", |
10083 | Style); |
10084 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
10085 | " aaaaa :\n" |
10086 | " bbbbbbbbbbbbbbb + cccccccccccccccc;", |
10087 | Style); |
10088 | |
10089 | // Chained conditionals |
10090 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" |
10091 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10092 | " 3333333333333333;", |
10093 | Style); |
10094 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" |
10095 | " bbbbbbbbbb ? 2222222222222222 :\n" |
10096 | " 3333333333333333;", |
10097 | Style); |
10098 | verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" |
10099 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10100 | " 3333333333333333;", |
10101 | Style); |
10102 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" |
10103 | " bbbbbbbbbbbbbbbb ? 222222 :\n" |
10104 | " 333333;", |
10105 | Style); |
10106 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" |
10107 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10108 | " cccccccccccccccc ? 3333333333333333 :\n" |
10109 | " 4444444444444444;", |
10110 | Style); |
10111 | verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" |
10112 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10113 | " 3333333333333333;", |
10114 | Style); |
10115 | verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" |
10116 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10117 | " (aaa ? bbb : ccc);", |
10118 | Style); |
10119 | verifyFormat( |
10120 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10121 | " cccccccccccccccccc) :\n" |
10122 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10123 | " 3333333333333333;", |
10124 | Style); |
10125 | verifyFormat( |
10126 | "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10127 | " cccccccccccccccccc) :\n" |
10128 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10129 | " 3333333333333333;", |
10130 | Style); |
10131 | verifyFormat( |
10132 | "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10133 | " dddddddddddddddddd) :\n" |
10134 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10135 | " 3333333333333333;", |
10136 | Style); |
10137 | verifyFormat( |
10138 | "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10139 | " dddddddddddddddddd) :\n" |
10140 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10141 | " 3333333333333333;", |
10142 | Style); |
10143 | verifyFormat( |
10144 | "return aaaaaaaaa ? 1111111111111111 :\n" |
10145 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10146 | " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10147 | " dddddddddddddddddd)", |
10148 | Style); |
10149 | verifyFormat( |
10150 | "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" |
10151 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10152 | " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10153 | " cccccccccccccccccc);", |
10154 | Style); |
10155 | verifyFormat( |
10156 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10157 | " ccccccccccccccccc ? dddddddddddddddddd :\n" |
10158 | " eeeeeeeeeeeeeeeeee) :\n" |
10159 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10160 | " 3333333333333333;", |
10161 | Style); |
10162 | verifyFormat( |
10163 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10164 | " ccccccccccccc ? dddddddddddddddddd :\n" |
10165 | " eeeeeeeeeeeeeeeeee) :\n" |
10166 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10167 | " 3333333333333333;", |
10168 | Style); |
10169 | verifyFormat( |
10170 | "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10171 | " ccccccccccccccccc ? dddddddddddddddddd :\n" |
10172 | " eeeeeeeeeeeeeeeeee) :\n" |
10173 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10174 | " 3333333333333333;", |
10175 | Style); |
10176 | verifyFormat( |
10177 | "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10178 | " cccccccccccccccccc :\n" |
10179 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10180 | " 3333333333333333;", |
10181 | Style); |
10182 | verifyFormat( |
10183 | "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10184 | " cccccccccccccccccc ? dddddddddddddddddd :\n" |
10185 | " eeeeeeeeeeeeeeeeee :\n" |
10186 | " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10187 | " 3333333333333333;", |
10188 | Style); |
10189 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" |
10190 | " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10191 | " cccccccccccccccccc ? dddddddddddddddddd :\n" |
10192 | " eeeeeeeeeeeeeeeeee) :\n" |
10193 | " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10194 | " 3333333333333333;", |
10195 | Style); |
10196 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" |
10197 | " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" |
10198 | " cccccccccccccccccccc ? dddddddddddddddddd :\n" |
10199 | " eeeeeeeeeeeeeeeeee :\n" |
10200 | " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" |
10201 | " 3333333333333333;", |
10202 | Style); |
10203 | } |
10204 | |
10205 | TEST_F(FormatTest, DeclarationsOfMultipleVariables) { |
10206 | verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" |
10207 | " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); |
10208 | verifyFormat("bool a = true, b = false;"); |
10209 | |
10210 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
10211 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" |
10212 | " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" |
10213 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); |
10214 | verifyFormat( |
10215 | "bool aaaaaaaaaaaaaaaaaaaaa =\n" |
10216 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" |
10217 | " d = e && f;"); |
10218 | verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" |
10219 | " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); |
10220 | verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" |
10221 | " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); |
10222 | verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" |
10223 | " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); |
10224 | |
10225 | FormatStyle Style = getGoogleStyle(); |
10226 | Style.PointerAlignment = FormatStyle::PAS_Left; |
10227 | Style.DerivePointerAlignment = false; |
10228 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10229 | " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" |
10230 | " *b = bbbbbbbbbbbbbbbbbbb;", |
10231 | Style); |
10232 | verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" |
10233 | " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", |
10234 | Style); |
10235 | verifyFormat("vector<int*> a, b;", Style); |
10236 | verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); |
10237 | verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); |
10238 | verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); |
10239 | verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", |
10240 | Style); |
10241 | verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", |
10242 | Style); |
10243 | verifyFormat( |
10244 | "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", |
10245 | Style); |
10246 | |
10247 | verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style); |
10248 | verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style); |
10249 | verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style); |
10250 | verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style); |
10251 | verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}", |
10252 | Style); |
10253 | } |
10254 | |
10255 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
10256 | verifyFormat("arr[foo ? bar : baz];"); |
10257 | verifyFormat("f()[foo ? bar : baz];"); |
10258 | verifyFormat("(a + b)[foo ? bar : baz];"); |
10259 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
10260 | } |
10261 | |
10262 | TEST_F(FormatTest, AlignsStringLiterals) { |
10263 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
10264 | " \"short literal\");"); |
10265 | verifyFormat( |
10266 | "looooooooooooooooooooooooongFunction(\n" |
10267 | " \"short literal\"\n" |
10268 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
10269 | verifyFormat("someFunction(\"Always break between multi-line\"\n" |
10270 | " \" string literals\",\n" |
10271 | " also, other, parameters);"); |
10272 | verifyFormat("fun + \"1243\" /* comment */\n" |
10273 | " \"5678\";", |
10274 | "fun + \"1243\" /* comment */\n" |
10275 | " \"5678\";", |
10276 | getLLVMStyleWithColumns(28)); |
10277 | verifyFormat( |
10278 | "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
10279 | " \"aaaaaaaaaaaaaaaaaaaaa\"\n" |
10280 | " \"aaaaaaaaaaaaaaaa\";", |
10281 | "aaaaaa =" |
10282 | "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " |
10283 | "aaaaaaaaaaaaaaaaaaaaa\" " |
10284 | "\"aaaaaaaaaaaaaaaa\";"); |
10285 | verifyFormat("a = a + \"a\"\n" |
10286 | " \"a\"\n" |
10287 | " \"a\";"); |
10288 | verifyFormat("f(\"a\", \"b\"\n" |
10289 | " \"c\");"); |
10290 | |
10291 | verifyFormat( |
10292 | "#define LL_FORMAT \"ll\"\n" |
10293 | "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" |
10294 | " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); |
10295 | |
10296 | verifyFormat("#define A(X) \\\n" |
10297 | " \"aaaaa\" #X \"bbbbbb\" \\\n" |
10298 | " \"ccccc\"", |
10299 | getLLVMStyleWithColumns(23)); |
10300 | verifyFormat("#define A \"def\"\n" |
10301 | "f(\"abc\" A \"ghi\"\n" |
10302 | " \"jkl\");"); |
10303 | |
10304 | verifyFormat("f(L\"a\"\n" |
10305 | " L\"b\");"); |
10306 | verifyFormat("#define A(X) \\\n" |
10307 | " L\"aaaaa\" #X L\"bbbbbb\" \\\n" |
10308 | " L\"ccccc\"", |
10309 | getLLVMStyleWithColumns(25)); |
10310 | |
10311 | verifyFormat("f(@\"a\"\n" |
10312 | " @\"b\");"); |
10313 | verifyFormat("NSString s = @\"a\"\n" |
10314 | " @\"b\"\n" |
10315 | " @\"c\";"); |
10316 | verifyFormat("NSString s = @\"a\"\n" |
10317 | " \"b\"\n" |
10318 | " \"c\";"); |
10319 | } |
10320 | |
10321 | TEST_F(FormatTest, ReturnTypeBreakingStyle) { |
10322 | FormatStyle Style = getLLVMStyle(); |
10323 | Style.ColumnLimit = 60; |
10324 | |
10325 | // No declarations or definitions should be moved to own line. |
10326 | Style.BreakAfterReturnType = FormatStyle::RTBS_None; |
10327 | verifyFormat("class A {\n" |
10328 | " int f() { return 1; }\n" |
10329 | " int g();\n" |
10330 | " long\n" |
10331 | " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" |
10332 | "};\n" |
10333 | "int f() { return 1; }\n" |
10334 | "int g();\n" |
10335 | "int foooooooooooooooooooooooooooo::\n" |
10336 | " baaaaaaaaaaaaaaaaaaaaar();", |
10337 | Style); |
10338 | |
10339 | // It is now allowed to break after a short return type if necessary. |
10340 | Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic; |
10341 | verifyFormat("class A {\n" |
10342 | " int f() { return 1; }\n" |
10343 | " int g();\n" |
10344 | " long\n" |
10345 | " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" |
10346 | "};\n" |
10347 | "int f() { return 1; }\n" |
10348 | "int g();\n" |
10349 | "int\n" |
10350 | "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", |
10351 | Style); |
10352 | |
10353 | // It now must never break after a short return type. |
10354 | Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType; |
10355 | verifyFormat("class A {\n" |
10356 | " int f() { return 1; }\n" |
10357 | " int g();\n" |
10358 | " long foooooooooooooooooooooooooooo::\n" |
10359 | " baaaaaaaaaaaaaaaaaaaar();\n" |
10360 | "};\n" |
10361 | "int f() { return 1; }\n" |
10362 | "int g();\n" |
10363 | "int foooooooooooooooooooooooooooo::\n" |
10364 | " baaaaaaaaaaaaaaaaaaaaar();", |
10365 | Style); |
10366 | |
10367 | // All declarations and definitions should have the return type moved to its |
10368 | // own line. |
10369 | Style.BreakAfterReturnType = FormatStyle::RTBS_All; |
10370 | Style.TypenameMacros = {"LIST"}; |
10371 | verifyFormat("SomeType\n" |
10372 | "funcdecl(LIST(uint64_t));", |
10373 | Style); |
10374 | verifyFormat("class E {\n" |
10375 | " int\n" |
10376 | " f() {\n" |
10377 | " return 1;\n" |
10378 | " }\n" |
10379 | " int\n" |
10380 | " g();\n" |
10381 | " long\n" |
10382 | " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" |
10383 | "};\n" |
10384 | "int\n" |
10385 | "f() {\n" |
10386 | " return 1;\n" |
10387 | "}\n" |
10388 | "int\n" |
10389 | "g();\n" |
10390 | "int\n" |
10391 | "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", |
10392 | Style); |
10393 | |
10394 | // Top-level definitions, and no kinds of declarations should have the |
10395 | // return type moved to its own line. |
10396 | Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; |
10397 | verifyFormat("class B {\n" |
10398 | " int f() { return 1; }\n" |
10399 | " int g();\n" |
10400 | "};\n" |
10401 | "int\n" |
10402 | "f() {\n" |
10403 | " return 1;\n" |
10404 | "}\n" |
10405 | "int g();", |
10406 | Style); |
10407 | |
10408 | // Top-level definitions and declarations should have the return type moved |
10409 | // to its own line. |
10410 | Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; |
10411 | verifyFormat("class C {\n" |
10412 | " int f() { return 1; }\n" |
10413 | " int g();\n" |
10414 | "};\n" |
10415 | "int\n" |
10416 | "f() {\n" |
10417 | " return 1;\n" |
10418 | "}\n" |
10419 | "int\n" |
10420 | "g();\n" |
10421 | "int\n" |
10422 | "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", |
10423 | Style); |
10424 | |
10425 | // All definitions should have the return type moved to its own line, but no |
10426 | // kinds of declarations. |
10427 | Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; |
10428 | verifyFormat("class D {\n" |
10429 | " int\n" |
10430 | " f() {\n" |
10431 | " return 1;\n" |
10432 | " }\n" |
10433 | " int g();\n" |
10434 | "};\n" |
10435 | "int\n" |
10436 | "f() {\n" |
10437 | " return 1;\n" |
10438 | "}\n" |
10439 | "int g();", |
10440 | Style); |
10441 | verifyFormat("const char *\n" |
10442 | "f(void) {\n"// Break here. |
10443 | " return \"\";\n" |
10444 | "}\n" |
10445 | "const char *bar(void);", // No break here. |
10446 | Style); |
10447 | verifyFormat("template <class T>\n" |
10448 | "T *\n" |
10449 | "f(T &c) {\n"// Break here. |
10450 | " return NULL;\n" |
10451 | "}\n" |
10452 | "template <class T> T *f(T &c);", // No break here. |
10453 | Style); |
10454 | verifyFormat("class C {\n" |
10455 | " int\n" |
10456 | " operator+() {\n" |
10457 | " return 1;\n" |
10458 | " }\n" |
10459 | " int\n" |
10460 | " operator()() {\n" |
10461 | " return 1;\n" |
10462 | " }\n" |
10463 | "};", |
10464 | Style); |
10465 | verifyFormat("void\n" |
10466 | "A::operator()() {}\n" |
10467 | "void\n" |
10468 | "A::operator>>() {}\n" |
10469 | "void\n" |
10470 | "A::operator+() {}\n" |
10471 | "void\n" |
10472 | "A::operator*() {}\n" |
10473 | "void\n" |
10474 | "A::operator->() {}\n" |
10475 | "void\n" |
10476 | "A::operator&() {}\n" |
10477 | "void\n" |
10478 | "A::operator&&() {}\n" |
10479 | "void\n" |
10480 | "A::operator[]() {}\n" |
10481 | "void\n" |
10482 | "A::operator!() {}\n" |
10483 | "void\n" |
10484 | "A::operator<Foo> *() {}\n" |
10485 | "void\n" |
10486 | "A::operator<Foo> &() {}\n", |
10487 | Style); |
10488 | verifyFormat("constexpr auto\n" |
10489 | "operator()() const -> reference {}\n" |
10490 | "constexpr auto\n" |
10491 | "operator>>() const -> reference {}\n" |
10492 | "constexpr auto\n" |
10493 | "operator+() const -> reference {}\n" |
10494 | "constexpr auto\n" |
10495 | "operator*() const -> reference {}\n" |
10496 | "constexpr auto\n" |
10497 | "operator->() const -> reference {}\n" |
10498 | "constexpr auto\n" |
10499 | "operator++() const -> reference {}\n" |
10500 | "constexpr auto\n" |
10501 | "operator void *() const -> reference {}\n" |
10502 | "constexpr auto\n" |
10503 | "operator void **() const -> reference {}\n" |
10504 | "constexpr auto\n" |
10505 | "operator void *() const -> reference {}\n" |
10506 | "constexpr auto\n" |
10507 | "operator void &() const -> reference {}\n" |
10508 | "constexpr auto\n" |
10509 | "operator&&() const -> reference {}\n" |
10510 | "constexpr auto\n" |
10511 | "operator char *() const -> reference {}\n" |
10512 | "constexpr auto\n" |
10513 | "operator!() const -> reference {}\n" |
10514 | "constexpr auto\n" |
10515 | "operator[]() const -> reference {}", |
10516 | Style); |
10517 | verifyFormat("void *operator new(std::size_t s);", // No break here. |
10518 | Style); |
10519 | verifyFormat("void *\n" |
10520 | "operator new(std::size_t s) {}", |
10521 | Style); |
10522 | verifyFormat("void *\n" |
10523 | "operator delete[](void *ptr) {}", |
10524 | Style); |
10525 | Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
10526 | verifyFormat("const char *\n" |
10527 | "f(void)\n"// Break here. |
10528 | "{\n" |
10529 | " return \"\";\n" |
10530 | "}\n" |
10531 | "const char *bar(void);", // No break here. |
10532 | Style); |
10533 | verifyFormat("template <class T>\n" |
10534 | "T *\n"// Problem here: no line break |
10535 | "f(T &c)\n"// Break here. |
10536 | "{\n" |
10537 | " return NULL;\n" |
10538 | "}\n" |
10539 | "template <class T> T *f(T &c);", // No break here. |
10540 | Style); |
10541 | verifyFormat("int\n" |
10542 | "foo(A<bool> a)\n" |
10543 | "{\n" |
10544 | " return a;\n" |
10545 | "}", |
10546 | Style); |
10547 | verifyFormat("int\n" |
10548 | "foo(A<8> a)\n" |
10549 | "{\n" |
10550 | " return a;\n" |
10551 | "}", |
10552 | Style); |
10553 | verifyFormat("int\n" |
10554 | "foo(A<B<bool>, 8> a)\n" |
10555 | "{\n" |
10556 | " return a;\n" |
10557 | "}", |
10558 | Style); |
10559 | verifyFormat("int\n" |
10560 | "foo(A<B<8>, bool> a)\n" |
10561 | "{\n" |
10562 | " return a;\n" |
10563 | "}", |
10564 | Style); |
10565 | verifyFormat("int\n" |
10566 | "foo(A<B<bool>, bool> a)\n" |
10567 | "{\n" |
10568 | " return a;\n" |
10569 | "}", |
10570 | Style); |
10571 | verifyFormat("int\n" |
10572 | "foo(A<B<8>, 8> a)\n" |
10573 | "{\n" |
10574 | " return a;\n" |
10575 | "}", |
10576 | Style); |
10577 | |
10578 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
10579 | Style.BraceWrapping.AfterFunction = true; |
10580 | verifyFormat("int f(i);\n"// No break here. |
10581 | "int\n"// Break here. |
10582 | "f(i)\n" |
10583 | "{\n" |
10584 | " return i + 1;\n" |
10585 | "}\n" |
10586 | "int\n"// Break here. |
10587 | "f(i)\n" |
10588 | "{\n" |
10589 | " return i + 1;\n" |
10590 | "};", |
10591 | Style); |
10592 | verifyFormat("int f(a, b, c);\n"// No break here. |
10593 | "int\n"// Break here. |
10594 | "f(a, b, c)\n"// Break here. |
10595 | "short a, b;\n" |
10596 | "float c;\n" |
10597 | "{\n" |
10598 | " return a + b < c;\n" |
10599 | "}\n" |
10600 | "int\n"// Break here. |
10601 | "f(a, b, c)\n"// Break here. |
10602 | "short a, b;\n" |
10603 | "float c;\n" |
10604 | "{\n" |
10605 | " return a + b < c;\n" |
10606 | "};", |
10607 | Style); |
10608 | verifyFormat("byte *\n"// Break here. |
10609 | "f(a)\n"// Break here. |
10610 | "byte a[];\n" |
10611 | "{\n" |
10612 | " return a;\n" |
10613 | "}", |
10614 | Style); |
10615 | verifyFormat("byte *\n" |
10616 | "f(a)\n" |
10617 | "byte /* K&R C */ a[];\n" |
10618 | "{\n" |
10619 | " return a;\n" |
10620 | "}\n" |
10621 | "byte *\n" |
10622 | "g(p)\n" |
10623 | "byte /* K&R C */ *p;\n" |
10624 | "{\n" |
10625 | " return p;\n" |
10626 | "}", |
10627 | Style); |
10628 | verifyFormat("bool f(int a, int) override;\n" |
10629 | "Bar g(int a, Bar) final;\n" |
10630 | "Bar h(a, Bar) final;", |
10631 | Style); |
10632 | verifyFormat("int\n" |
10633 | "f(a)", |
10634 | Style); |
10635 | verifyFormat("bool\n" |
10636 | "f(size_t = 0, bool b = false)\n" |
10637 | "{\n" |
10638 | " return !b;\n" |
10639 | "}", |
10640 | Style); |
10641 | |
10642 | // The return breaking style doesn't affect: |
10643 | // * function and object definitions with attribute-like macros |
10644 | verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" |
10645 | " ABSL_GUARDED_BY(mutex) = {};", |
10646 | getGoogleStyleWithColumns(40)); |
10647 | verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" |
10648 | " ABSL_GUARDED_BY(mutex); // comment", |
10649 | getGoogleStyleWithColumns(40)); |
10650 | verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" |
10651 | " ABSL_GUARDED_BY(mutex1)\n" |
10652 | " ABSL_GUARDED_BY(mutex2);", |
10653 | getGoogleStyleWithColumns(40)); |
10654 | verifyFormat("Tttttt f(int a, int b)\n" |
10655 | " ABSL_GUARDED_BY(mutex1)\n" |
10656 | " ABSL_GUARDED_BY(mutex2);", |
10657 | getGoogleStyleWithColumns(40)); |
10658 | // * typedefs |
10659 | verifyGoogleFormat("typedef ATTR(X) char x;"); |
10660 | |
10661 | Style = getGNUStyle(); |
10662 | |
10663 | // Test for comments at the end of function declarations. |
10664 | verifyFormat("void\n" |
10665 | "foo (int a, /*abc*/ int b) // def\n" |
10666 | "{\n" |
10667 | "}", |
10668 | Style); |
10669 | |
10670 | verifyFormat("void\n" |
10671 | "foo (int a, /* abc */ int b) /* def */\n" |
10672 | "{\n" |
10673 | "}", |
10674 | Style); |
10675 | |
10676 | // Definitions that should not break after return type |
10677 | verifyFormat("void foo (int a, int b); // def", Style); |
10678 | verifyFormat("void foo (int a, int b); /* def */", Style); |
10679 | verifyFormat("void foo (int a, int b);", Style); |
10680 | } |
10681 | |
10682 | TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { |
10683 | FormatStyle NoBreak = getLLVMStyle(); |
10684 | NoBreak.AlwaysBreakBeforeMultilineStrings = false; |
10685 | FormatStyle Break = getLLVMStyle(); |
10686 | Break.AlwaysBreakBeforeMultilineStrings = true; |
10687 | verifyFormat("aaaa = \"bbbb\"\n" |
10688 | " \"cccc\";", |
10689 | NoBreak); |
10690 | verifyFormat("aaaa =\n" |
10691 | " \"bbbb\"\n" |
10692 | " \"cccc\";", |
10693 | Break); |
10694 | verifyFormat("aaaa(\"bbbb\"\n" |
10695 | " \"cccc\");", |
10696 | NoBreak); |
10697 | verifyFormat("aaaa(\n" |
10698 | " \"bbbb\"\n" |
10699 | " \"cccc\");", |
10700 | Break); |
10701 | verifyFormat("aaaa(qqq, \"bbbb\"\n" |
10702 | " \"cccc\");", |
10703 | NoBreak); |
10704 | verifyFormat("aaaa(qqq,\n" |
10705 | " \"bbbb\"\n" |
10706 | " \"cccc\");", |
10707 | Break); |
10708 | verifyFormat("aaaa(qqq,\n" |
10709 | " L\"bbbb\"\n" |
10710 | " L\"cccc\");", |
10711 | Break); |
10712 | verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" |
10713 | " \"bbbb\"));", |
10714 | Break); |
10715 | verifyFormat("string s = someFunction(\n" |
10716 | " \"abc\"\n" |
10717 | " \"abc\");", |
10718 | Break); |
10719 | |
10720 | // As we break before unary operators, breaking right after them is bad. |
10721 | verifyFormat("string foo = abc ? \"x\"\n" |
10722 | " \"blah blah blah blah blah blah\"\n" |
10723 | " : \"y\";", |
10724 | Break); |
10725 | |
10726 | // Don't break if there is no column gain. |
10727 | verifyFormat("f(\"aaaa\"\n" |
10728 | " \"bbbb\");", |
10729 | Break); |
10730 | |
10731 | // Treat literals with escaped newlines like multi-line string literals. |
10732 | verifyNoChange("x = \"a\\\n" |
10733 | "b\\\n" |
10734 | "c\";", |
10735 | NoBreak); |
10736 | verifyFormat("xxxx =\n" |
10737 | " \"a\\\n" |
10738 | "b\\\n" |
10739 | "c\";", |
10740 | "xxxx = \"a\\\n" |
10741 | "b\\\n" |
10742 | "c\";", |
10743 | Break); |
10744 | |
10745 | verifyFormat("NSString *const kString =\n" |
10746 | " @\"aaaa\"\n" |
10747 | " @\"bbbb\";", |
10748 | "NSString *const kString = @\"aaaa\"\n" |
10749 | "@\"bbbb\";", |
10750 | Break); |
10751 | |
10752 | Break.ColumnLimit = 0; |
10753 | verifyFormat("const char *hello = \"hello llvm\";", Break); |
10754 | } |
10755 | |
10756 | TEST_F(FormatTest, AlignsPipes) { |
10757 | verifyFormat( |
10758 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10759 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10760 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10761 | verifyFormat( |
10762 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
10763 | " << aaaaaaaaaaaaaaaaaaaa;"); |
10764 | verifyFormat( |
10765 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10766 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10767 | verifyFormat( |
10768 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" |
10769 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10770 | verifyFormat( |
10771 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
10772 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
10773 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
10774 | verifyFormat( |
10775 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10776 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
10777 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10778 | verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10779 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10780 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
10781 | " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
10782 | verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" |
10783 | " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); |
10784 | verifyFormat( |
10785 | "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10786 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
10787 | verifyFormat( |
10788 | "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" |
10789 | " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
10790 | |
10791 | verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" |
10792 | " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); |
10793 | verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10794 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10795 | " aaaaaaaaaaaaaaaaaaaaa)\n" |
10796 | " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10797 | verifyFormat("LOG_IF(aaa == //\n" |
10798 | " bbb)\n" |
10799 | " << a << b;"); |
10800 | |
10801 | // But sometimes, breaking before the first "<<" is desirable. |
10802 | verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" |
10803 | " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); |
10804 | verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" |
10805 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10806 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10807 | verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" |
10808 | " << BEF << IsTemplate << Description << E->getType();"); |
10809 | verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" |
10810 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10811 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
10812 | verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" |
10813 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10814 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
10815 | " << aaa;"); |
10816 | |
10817 | verifyFormat( |
10818 | "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10819 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
10820 | |
10821 | // Incomplete string literal. |
10822 | verifyFormat("llvm::errs() << \"\n" |
10823 | " << a;", |
10824 | "llvm::errs() << \"\n<<a;"); |
10825 | |
10826 | verifyFormat("void f() {\n" |
10827 | " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" |
10828 | " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" |
10829 | "}"); |
10830 | |
10831 | // Handle 'endl'. |
10832 | verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" |
10833 | " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); |
10834 | verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); |
10835 | |
10836 | // Handle '\n'. |
10837 | verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" |
10838 | " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); |
10839 | verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" |
10840 | " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); |
10841 | verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" |
10842 | " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); |
10843 | verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); |
10844 | } |
10845 | |
10846 | TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { |
10847 | verifyFormat("return out << \"somepacket = {\\n\"\n" |
10848 | " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" |
10849 | " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" |
10850 | " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" |
10851 | " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" |
10852 | " << \"}\";"); |
10853 | |
10854 | verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" |
10855 | " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" |
10856 | " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); |
10857 | verifyFormat( |
10858 | "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" |
10859 | " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" |
10860 | " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" |
10861 | " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" |
10862 | " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); |
10863 | verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" |
10864 | " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
10865 | verifyFormat( |
10866 | "void f() {\n" |
10867 | " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" |
10868 | " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
10869 | "}"); |
10870 | |
10871 | // Breaking before the first "<<" is generally not desirable. |
10872 | verifyFormat( |
10873 | "llvm::errs()\n" |
10874 | " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10875 | " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10876 | " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10877 | " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
10878 | getLLVMStyleWithColumns(70)); |
10879 | verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" |
10880 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10881 | " << \"aaaaaaaaaaaaaaaaaaa: \"\n" |
10882 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10883 | " << \"aaaaaaaaaaaaaaaaaaa: \"\n" |
10884 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", |
10885 | getLLVMStyleWithColumns(70)); |
10886 | |
10887 | verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" |
10888 | " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" |
10889 | " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); |
10890 | verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" |
10891 | " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" |
10892 | " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); |
10893 | verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" |
10894 | " (aaaa + aaaa);", |
10895 | getLLVMStyleWithColumns(40)); |
10896 | verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" |
10897 | " (aaaaaaa + aaaaa));", |
10898 | getLLVMStyleWithColumns(40)); |
10899 | verifyFormat( |
10900 | "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" |
10901 | " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" |
10902 | " bbbbbbbbbbbbbbbbbbbbbbb);"); |
10903 | } |
10904 | |
10905 | TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) { |
10906 | verifyFormat("QStringList() << \"foo\" << \"bar\";"); |
10907 | |
10908 | verifyNoChange("QStringList() << \"foo\"\n" |
10909 | " << \"bar\";"); |
10910 | |
10911 | verifyFormat("log_error(log, \"foo\" << \"bar\");", |
10912 | "log_error(log, \"foo\"\n" |
10913 | " << \"bar\");"); |
10914 | } |
10915 | |
10916 | TEST_F(FormatTest, UnderstandsEquals) { |
10917 | verifyFormat( |
10918 | "aaaaaaaaaaaaaaaaa =\n" |
10919 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
10920 | verifyFormat( |
10921 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
10922 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
10923 | verifyFormat( |
10924 | "if (a) {\n" |
10925 | " f();\n" |
10926 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
10927 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
10928 | "}"); |
10929 | |
10930 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
10931 | " 100000000 + 10000000) {\n}"); |
10932 | } |
10933 | |
10934 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
10935 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
10936 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
10937 | |
10938 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
10939 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
10940 | |
10941 | verifyFormat( |
10942 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
10943 | " Parameter2);"); |
10944 | |
10945 | verifyFormat( |
10946 | "ShortObject->shortFunction(\n" |
10947 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
10948 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
10949 | |
10950 | verifyFormat("loooooooooooooongFunction(\n" |
10951 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
10952 | |
10953 | verifyFormat( |
10954 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
10955 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
10956 | |
10957 | verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" |
10958 | " .WillRepeatedly(Return(SomeValue));"); |
10959 | verifyFormat("void f() {\n" |
10960 | " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" |
10961 | " .Times(2)\n" |
10962 | " .WillRepeatedly(Return(SomeValue));\n" |
10963 | "}"); |
10964 | verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" |
10965 | " ccccccccccccccccccccccc);"); |
10966 | verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10967 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
10968 | " .aaaaa(aaaaa),\n" |
10969 | " aaaaaaaaaaaaaaaaaaaaa);"); |
10970 | verifyFormat("void f() {\n" |
10971 | " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10972 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" |
10973 | "}"); |
10974 | verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10975 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
10976 | " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10977 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10978 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
10979 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10980 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10981 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
10982 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" |
10983 | "}"); |
10984 | |
10985 | // Here, it is not necessary to wrap at "." or "->". |
10986 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
10987 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
10988 | verifyFormat( |
10989 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
10990 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
10991 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));"); |
10992 | |
10993 | verifyFormat( |
10994 | "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
10995 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); |
10996 | verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" |
10997 | " aaaaaaaaa()->aaaaaa()->aaaaa());"); |
10998 | verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" |
10999 | " aaaaaaaaa()->aaaaaa()->aaaaa());"); |
11000 | |
11001 | verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
11002 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
11003 | " .a();"); |
11004 | |
11005 | FormatStyle NoBinPacking = getLLVMStyle(); |
11006 | NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
11007 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
11008 | " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
11009 | " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" |
11010 | " aaaaaaaaaaaaaaaaaaa,\n" |
11011 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
11012 | NoBinPacking); |
11013 | |
11014 | // If there is a subsequent call, change to hanging indentation. |
11015 | verifyFormat( |
11016 | "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11017 | " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" |
11018 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
11019 | verifyFormat( |
11020 | "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11021 | " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); |
11022 | verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11023 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
11024 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
11025 | verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11026 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
11027 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); |
11028 | } |
11029 | |
11030 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
11031 | verifyFormat("template <typename T>\n" |
11032 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
11033 | verifyFormat("template <typename T>\n" |
11034 | "// T should be one of {A, B}.\n" |
11035 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
11036 | verifyFormat( |
11037 | "template <typename T>\n" |
11038 | "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); |
11039 | verifyFormat("template <typename T>\n" |
11040 | "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
11041 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
11042 | verifyFormat( |
11043 | "template <typename T>\n" |
11044 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
11045 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
11046 | verifyFormat( |
11047 | "template <typename T>\n" |
11048 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
11049 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
11050 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
11051 | verifyFormat("template <typename T>\n" |
11052 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11053 | " int aaaaaaaaaaaaaaaaaaaaaa);"); |
11054 | verifyFormat( |
11055 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
11056 | " typename T4 = char>\n" |
11057 | "void f();"); |
11058 | verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" |
11059 | " template <typename> class cccccccccccccccccccccc,\n" |
11060 | " typename ddddddddddddd>\n" |
11061 | "class C {};"); |
11062 | verifyFormat( |
11063 | "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" |
11064 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
11065 | |
11066 | verifyFormat("void f() {\n" |
11067 | " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" |
11068 | " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" |
11069 | "}"); |
11070 | |
11071 | verifyFormat("template <typename T> class C {};"); |
11072 | verifyFormat("template <typename T> void f();"); |
11073 | verifyFormat("template <typename T> void f() {}"); |
11074 | verifyFormat( |
11075 | "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" |
11076 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
11077 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" |
11078 | " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" |
11079 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
11080 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" |
11081 | " bbbbbbbbbbbbbbbbbbbbbbbb);", |
11082 | getLLVMStyleWithColumns(72)); |
11083 | verifyFormat("static_cast<A< //\n" |
11084 | " B> *>(\n" |
11085 | "\n" |
11086 | ");", |
11087 | "static_cast<A<//\n" |
11088 | " B>*>(\n" |
11089 | "\n" |
11090 | " );"); |
11091 | verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11092 | " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); |
11093 | |
11094 | FormatStyle AlwaysBreak = getLLVMStyle(); |
11095 | AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; |
11096 | verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); |
11097 | verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); |
11098 | verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); |
11099 | verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
11100 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" |
11101 | " ccccccccccccccccccccccccccccccccccccccccccccccc);"); |
11102 | verifyFormat("template <template <typename> class Fooooooo,\n" |
11103 | " template <typename> class Baaaaaaar>\n" |
11104 | "struct C {};", |
11105 | AlwaysBreak); |
11106 | verifyFormat("template <typename T> // T can be A, B or C.\n" |
11107 | "struct C {};", |
11108 | AlwaysBreak); |
11109 | verifyFormat("template <typename T>\n" |
11110 | "C(T) noexcept;", |
11111 | AlwaysBreak); |
11112 | verifyFormat("template <typename T>\n" |
11113 | "ClassName(T) noexcept;", |
11114 | AlwaysBreak); |
11115 | verifyFormat("template <typename T>\n" |
11116 | "POOR_NAME(T) noexcept;", |
11117 | AlwaysBreak); |
11118 | verifyFormat("template <enum E> class A {\n" |
11119 | "public:\n" |
11120 | " E *f();\n" |
11121 | "};"); |
11122 | |
11123 | FormatStyle NeverBreak = getLLVMStyle(); |
11124 | NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No; |
11125 | verifyFormat("template <typename T> class C {};", NeverBreak); |
11126 | verifyFormat("template <typename T> void f();", NeverBreak); |
11127 | verifyFormat("template <typename T> void f() {}", NeverBreak); |
11128 | verifyFormat("template <typename T> C(T) noexcept;", NeverBreak); |
11129 | verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak); |
11130 | verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak); |
11131 | verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " |
11132 | "bbbbbbbbbbbbbbbbbbbb) {}", |
11133 | NeverBreak); |
11134 | verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
11135 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" |
11136 | " ccccccccccccccccccccccccccccccccccccccccccccccc);", |
11137 | NeverBreak); |
11138 | verifyFormat("template <template <typename> class Fooooooo,\n" |
11139 | " template <typename> class Baaaaaaar>\n" |
11140 | "struct C {};", |
11141 | NeverBreak); |
11142 | verifyFormat("template <typename T> // T can be A, B or C.\n" |
11143 | "struct C {};", |
11144 | NeverBreak); |
11145 | verifyFormat("template <enum E> class A {\n" |
11146 | "public:\n" |
11147 | " E *f();\n" |
11148 | "};", |
11149 | NeverBreak); |
11150 | NeverBreak.PenaltyBreakTemplateDeclaration = 100; |
11151 | verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " |
11152 | "bbbbbbbbbbbbbbbbbbbb) {}", |
11153 | NeverBreak); |
11154 | |
11155 | auto Style = getLLVMStyle(); |
11156 | Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave; |
11157 | |
11158 | verifyNoChange("template <typename T>\n" |
11159 | "class C {};", |
11160 | Style); |
11161 | verifyFormat("template <typename T> class C {};", Style); |
11162 | |
11163 | verifyNoChange("template <typename T>\n" |
11164 | "void f();", |
11165 | Style); |
11166 | verifyFormat("template <typename T> void f();", Style); |
11167 | |
11168 | verifyNoChange("template <typename T>\n" |
11169 | "void f() {}", |
11170 | Style); |
11171 | verifyFormat("template <typename T> void f() {}", Style); |
11172 | |
11173 | verifyNoChange("template <typename T>\n" |
11174 | "// T can be A, B or C.\n" |
11175 | "struct C {};", |
11176 | Style); |
11177 | verifyFormat("template <typename T> // T can be A, B or C.\n" |
11178 | "struct C {};", |
11179 | Style); |
11180 | |
11181 | verifyNoChange("template <typename T>\n" |
11182 | "C(T) noexcept;", |
11183 | Style); |
11184 | verifyFormat("template <typename T> C(T) noexcept;", Style); |
11185 | |
11186 | verifyNoChange("template <enum E>\n" |
11187 | "class A {\n" |
11188 | "public:\n" |
11189 | " E *f();\n" |
11190 | "};", |
11191 | Style); |
11192 | verifyFormat("template <enum E> class A {\n" |
11193 | "public:\n" |
11194 | " E *f();\n" |
11195 | "};", |
11196 | Style); |
11197 | |
11198 | verifyNoChange("template <auto x>\n" |
11199 | "constexpr int simple(int) {\n" |
11200 | " char c;\n" |
11201 | " return 1;\n" |
11202 | "}", |
11203 | Style); |
11204 | verifyFormat("template <auto x> constexpr int simple(int) {\n" |
11205 | " char c;\n" |
11206 | " return 1;\n" |
11207 | "}", |
11208 | Style); |
11209 | |
11210 | Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; |
11211 | verifyNoChange("template <auto x>\n" |
11212 | "requires(x > 1)\n" |
11213 | "constexpr int with_req(int) {\n" |
11214 | " return 1;\n" |
11215 | "}", |
11216 | Style); |
11217 | verifyFormat("template <auto x> requires(x > 1)\n" |
11218 | "constexpr int with_req(int) {\n" |
11219 | " return 1;\n" |
11220 | "}", |
11221 | Style); |
11222 | } |
11223 | |
11224 | TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { |
11225 | FormatStyle Style = getGoogleStyle(Language: FormatStyle::LK_Cpp); |
11226 | Style.ColumnLimit = 60; |
11227 | verifyFormat("// Baseline - no comments.\n" |
11228 | "template <\n" |
11229 | " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" |
11230 | "void f() {}", |
11231 | Style); |
11232 | |
11233 | verifyFormat("template <\n" |
11234 | " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" |
11235 | "void f() {}", |
11236 | "template <\n" |
11237 | " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" |
11238 | "void f() {}", |
11239 | Style); |
11240 | |
11241 | verifyFormat( |
11242 | "template <\n" |
11243 | " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" |
11244 | "void f() {}", |
11245 | "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" |
11246 | "void f() {}", |
11247 | Style); |
11248 | |
11249 | verifyFormat("template <\n" |
11250 | " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" |
11251 | " // multiline\n" |
11252 | "void f() {}", |
11253 | "template <\n" |
11254 | " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" |
11255 | " // multiline\n" |
11256 | "void f() {}", |
11257 | Style); |
11258 | |
11259 | verifyFormat( |
11260 | "template <typename aaaaaaaaaa<\n" |
11261 | " bbbbbbbbbbbb>::value> // trailing loooong\n" |
11262 | "void f() {}", |
11263 | "template <\n" |
11264 | " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" |
11265 | "void f() {}", |
11266 | Style); |
11267 | } |
11268 | |
11269 | TEST_F(FormatTest, BreakBeforeTemplateCloser) { |
11270 | auto Style = getLLVMStyle(); |
11271 | // Begin with tests covering the case where there is no constraint on the |
11272 | // column limit. |
11273 | Style.ColumnLimit = 0; |
11274 | Style.BreakBeforeTemplateCloser = true; |
11275 | // BreakBeforeTemplateCloser should NOT force template declarations onto |
11276 | // multiple lines. |
11277 | verifyFormat("template <typename Foo>\n" |
11278 | "void foo() {}", |
11279 | Style); |
11280 | verifyFormat("template <typename Foo, typename Bar>\n" |
11281 | "void foo() {}", |
11282 | Style); |
11283 | // It should add a line break before > if not already present: |
11284 | verifyFormat("template <\n" |
11285 | " typename Foo\n" |
11286 | ">\n" |
11287 | "void foo() {}", |
11288 | "template <\n" |
11289 | " typename Foo>\n" |
11290 | "void foo() {}", |
11291 | Style); |
11292 | verifyFormat("template <\n" |
11293 | " typename Foo,\n" |
11294 | " typename Bar\n" |
11295 | ">\n" |
11296 | "void foo() {}", |
11297 | "template <\n" |
11298 | " typename Foo,\n" |
11299 | " typename Bar>\n" |
11300 | "void foo() {}", |
11301 | Style); |
11302 | // When within an indent scope, the > should be placed accordingly: |
11303 | verifyFormat("struct Baz {\n" |
11304 | " template <\n" |
11305 | " typename Foo,\n" |
11306 | " typename Bar\n" |
11307 | " >\n" |
11308 | " void foo() {}\n" |
11309 | "};", |
11310 | "struct Baz {\n" |
11311 | " template <\n" |
11312 | " typename Foo,\n" |
11313 | " typename Bar>\n" |
11314 | " void foo() {}\n" |
11315 | "};", |
11316 | Style); |
11317 | |
11318 | // Test from https://github.com/llvm/llvm-project/issues/80049: |
11319 | verifyFormat( |
11320 | "using type = std::remove_cv_t<\n" |
11321 | " add_common_cv_reference<\n" |
11322 | " std::common_type_t<std::decay_t<T0>, std::decay_t<T1>>,\n" |
11323 | " T0,\n" |
11324 | " T1\n" |
11325 | " >\n" |
11326 | ">;", |
11327 | "using type = std::remove_cv_t<\n" |
11328 | " add_common_cv_reference<\n" |
11329 | " std::common_type_t<std::decay_t<T0>, std::decay_t<T1>>,\n" |
11330 | " T0,\n" |
11331 | " T1>>;", |
11332 | Style); |
11333 | |
11334 | // Test lambda goes to next line: |
11335 | verifyFormat("void foo() {\n" |
11336 | " auto lambda = []<\n" |
11337 | " typename T\n" |
11338 | " >(T t) {\n" |
11339 | " };\n" |
11340 | "}", |
11341 | "void foo() {\n" |
11342 | " auto lambda = []<\n" |
11343 | " typename T>(T t){\n" |
11344 | " };\n" |
11345 | "}", |
11346 | Style); |
11347 | // With no column limit, two parameters can go on the same line: |
11348 | verifyFormat("void foo() {\n" |
11349 | " auto lambda = []<\n" |
11350 | " typename T, typename Foo\n" |
11351 | " >(T t) {\n" |
11352 | " };\n" |
11353 | "}", |
11354 | "void foo() {\n" |
11355 | " auto lambda = []<\n" |
11356 | " typename T, typename Foo>(T t){\n" |
11357 | " };\n" |
11358 | "}", |
11359 | Style); |
11360 | // Or on different lines: |
11361 | verifyFormat("void foo() {\n" |
11362 | " auto lambda = []<\n" |
11363 | " typename T,\n" |
11364 | " typename Foo\n" |
11365 | " >(T t) {\n" |
11366 | " };\n" |
11367 | "}", |
11368 | "void foo() {\n" |
11369 | " auto lambda = []<\n" |
11370 | " typename T,\n" |
11371 | " typename Foo>(T t){\n" |
11372 | " };\n" |
11373 | "}", |
11374 | Style); |
11375 | |
11376 | // Test template usage goes to next line too: |
11377 | verifyFormat("void foo() {\n" |
11378 | " myFunc<\n" |
11379 | " T\n" |
11380 | " >();\n" |
11381 | "}", |
11382 | "void foo() {\n" |
11383 | " myFunc<\n" |
11384 | " T>();\n" |
11385 | "}", |
11386 | Style); |
11387 | |
11388 | // Now test that it handles the cases when the column limit forces wrapping. |
11389 | Style.ColumnLimit = 40; |
11390 | // The typename goes on the first line if it fits: |
11391 | verifyFormat("template <typename Fooooooooooooooooooo,\n" |
11392 | " typename Bar>\n" |
11393 | "void foo() {}", |
11394 | Style); |
11395 | verifyFormat("template <typename Foo,\n" |
11396 | " typename Barrrrrrrrrrrrrrrrrr>\n" |
11397 | "void foo() {}", |
11398 | Style); |
11399 | // Long names should be split in one step: |
11400 | verifyFormat("template <\n" |
11401 | " typename Foo,\n" |
11402 | " typename Barrrrrrrrrrrrrrrrrrr\n" |
11403 | ">\n" |
11404 | "void foo() {}", |
11405 | "template <typename Foo, typename Barrrrrrrrrrrrrrrrrrr>\n" |
11406 | "void foo() {}", |
11407 | Style); |
11408 | verifyFormat("template <\n" |
11409 | " typename Foooooooooooooooooooo,\n" |
11410 | " typename Bar\n" |
11411 | ">\n" |
11412 | "void foo() {}", |
11413 | "template <typename Foooooooooooooooooooo, typename Bar>\n" |
11414 | "void foo() {}", |
11415 | Style); |
11416 | // Even when there is only one long name: |
11417 | verifyFormat("template <\n" |
11418 | " typename Foooooooooooooooooooo\n" |
11419 | ">\n" |
11420 | "void foo() {}", |
11421 | "template <typename Foooooooooooooooooooo>\n" |
11422 | "void foo() {}", |
11423 | Style); |
11424 | // Test lambda goes to next line if the type is looong: |
11425 | verifyFormat("void foo() {\n" |
11426 | " auto lambda =\n" |
11427 | " []<\n" |
11428 | " typename Loooooooooooooooooooooooooooooooooong\n" |
11429 | " >(T t) {};\n" |
11430 | " auto lambda =\n" |
11431 | " [looooooooooooooong]<\n" |
11432 | " typename Loooooooooooooooooooooooooooooooooong\n" |
11433 | " >(T t) {};\n" |
11434 | " auto lambda =\n" |
11435 | " []<\n" |
11436 | " typename T,\n" |
11437 | " typename Loooooooooooooooooooooooooooooooooong\n" |
11438 | " >(T t) {};\n" |
11439 | // Nested: |
11440 | " auto lambda =\n" |
11441 | " []<\n" |
11442 | " template <typename, typename>\n" |
11443 | " typename Looooooooooooooooooong\n" |
11444 | " >(T t) {};\n" |
11445 | // Same idea, the "T" is now short rather than Looong: |
11446 | " auto lambda =\n" |
11447 | " []<template <typename, typename>\n" |
11448 | " typename T>(T t) {};\n" |
11449 | // Nested with long capture forces the style to block indent: |
11450 | " auto lambda =\n" |
11451 | " [loooooooooooooooooooong]<\n" |
11452 | " template <typename, typename>\n" |
11453 | " typename Looooooooooooooooooong\n" |
11454 | " >(T t) {};\n" |
11455 | // But *now* it stays block indented even when T is short: |
11456 | " auto lambda =\n" |
11457 | " [loooooooooooooooooooong]<\n" |
11458 | " template <typename, typename>\n" |
11459 | " typename T\n" |
11460 | " >(T t) {};\n" |
11461 | // Nested, with long name and long captures: |
11462 | " auto lambda =\n" |
11463 | " [loooooooooooooooooooong]<\n" |
11464 | " template <\n" |
11465 | " typename Foooooooooooooooo,\n" |
11466 | " typename\n" |
11467 | " >\n" |
11468 | " typename T\n" |
11469 | " >(T t) {};\n" |
11470 | // Allow the nested template to be on the same line: |
11471 | " auto lambda =\n" |
11472 | " [loooooooooooooooooooong]<\n" |
11473 | " template <typename Fooooooooo,\n" |
11474 | " typename>\n" |
11475 | " typename T\n" |
11476 | " >(T t) {};\n" |
11477 | "}", |
11478 | Style); |
11479 | |
11480 | // Test template usage goes to next line if the type is looong: |
11481 | verifyFormat("void foo() {\n" |
11482 | " myFunc<\n" |
11483 | " Looooooooooooooooooooooooong\n" |
11484 | " >();\n" |
11485 | "}", |
11486 | Style); |
11487 | // Even a single type in the middle is enough to force it to block indent |
11488 | // style: |
11489 | verifyFormat("void foo() {\n" |
11490 | " myFunc<\n" |
11491 | " Foo, Foo, Foo,\n" |
11492 | " Foooooooooooooooooooooooooooooo,\n" |
11493 | " Foo, Foo, Foo, Foo\n" |
11494 | " >();\n" |
11495 | "}", |
11496 | Style); |
11497 | } |
11498 | |
11499 | TEST_F(FormatTest, WrapsTemplateParameters) { |
11500 | FormatStyle Style = getLLVMStyle(); |
11501 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
11502 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
11503 | verifyFormat( |
11504 | "template <typename... a> struct q {};\n" |
11505 | "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" |
11506 | " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" |
11507 | " y;", |
11508 | Style); |
11509 | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; |
11510 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
11511 | verifyFormat( |
11512 | "template <typename... a> struct r {};\n" |
11513 | "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" |
11514 | " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" |
11515 | " y;", |
11516 | Style); |
11517 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
11518 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
11519 | verifyFormat("template <typename... a> struct s {};\n" |
11520 | "extern s<\n" |
11521 | " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " |
11522 | "aaaaaaaaaaaaaaaaaaaaaa,\n" |
11523 | " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " |
11524 | "aaaaaaaaaaaaaaaaaaaaaa>\n" |
11525 | " y;", |
11526 | Style); |
11527 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
11528 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
11529 | verifyFormat("template <typename... a> struct t {};\n" |
11530 | "extern t<\n" |
11531 | " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " |
11532 | "aaaaaaaaaaaaaaaaaaaaaa,\n" |
11533 | " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " |
11534 | "aaaaaaaaaaaaaaaaaaaaaa>\n" |
11535 | " y;", |
11536 | Style); |
11537 | } |
11538 | |
11539 | TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { |
11540 | verifyFormat( |
11541 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
11542 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
11543 | verifyFormat( |
11544 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
11545 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11546 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); |
11547 | |
11548 | // FIXME: Should we have the extra indent after the second break? |
11549 | verifyFormat( |
11550 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
11551 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
11552 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
11553 | |
11554 | verifyFormat( |
11555 | "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" |
11556 | " cccccccccccccccccccccccccccccccccccccccccccccc());"); |
11557 | |
11558 | // Breaking at nested name specifiers is generally not desirable. |
11559 | verifyFormat( |
11560 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11561 | " aaaaaaaaaaaaaaaaaaaaaaa);"); |
11562 | |
11563 | verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" |
11564 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
11565 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
11566 | " aaaaaaaaaaaaaaaaaaaaa);", |
11567 | getLLVMStyleWithColumns(74)); |
11568 | |
11569 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
11570 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
11571 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
11572 | |
11573 | verifyFormat( |
11574 | "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n" |
11575 | " AndAnotherLongClassNameToShowTheIssue() {}\n" |
11576 | "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n" |
11577 | " ~AndAnotherLongClassNameToShowTheIssue() {}"); |
11578 | } |
11579 | |
11580 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
11581 | verifyFormat("A<int> a;"); |
11582 | verifyFormat("A<A<A<int>>> a;"); |
11583 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
11584 | verifyFormat("bool x = a < 1 || 2 > a;"); |
11585 | verifyFormat("bool x = 5 < f<int>();"); |
11586 | verifyFormat("bool x = f<int>() > 5;"); |
11587 | verifyFormat("bool x = 5 < a<int>::x;"); |
11588 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
11589 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
11590 | |
11591 | verifyGoogleFormat("A<A<int>> a;"); |
11592 | verifyGoogleFormat("A<A<A<int>>> a;"); |
11593 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
11594 | verifyGoogleFormat("A<A<int> > a;"); |
11595 | verifyGoogleFormat("A<A<A<int> > > a;"); |
11596 | verifyGoogleFormat("A<A<A<A<int> > > > a;"); |
11597 | verifyGoogleFormat("A<::A<int>> a;"); |
11598 | verifyGoogleFormat("A<::A> a;"); |
11599 | verifyGoogleFormat("A< ::A> a;"); |
11600 | verifyGoogleFormat("A< ::A<int> > a;"); |
11601 | verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle()); |
11602 | verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle()); |
11603 | verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle()); |
11604 | verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle()); |
11605 | verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};", |
11606 | getGoogleStyle()); |
11607 | |
11608 | verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); |
11609 | |
11610 | // template closer followed by a token that starts with > or = |
11611 | verifyFormat("bool b = a<1> > 1;"); |
11612 | verifyFormat("bool b = a<1> >= 1;"); |
11613 | verifyFormat("int i = a<1> >> 1;"); |
11614 | FormatStyle Style = getLLVMStyle(); |
11615 | Style.SpaceBeforeAssignmentOperators = false; |
11616 | verifyFormat("bool b= a<1> == 1;", Style); |
11617 | verifyFormat("a<int> = 1;", Style); |
11618 | verifyFormat("a<int> >>= 1;", Style); |
11619 | |
11620 | verifyFormat("test < a | b >> c;"); |
11621 | verifyFormat("test<test<a | b>> c;"); |
11622 | verifyFormat("test >> a >> b;"); |
11623 | verifyFormat("test << a >> b;"); |
11624 | |
11625 | verifyFormat("f<int>();"); |
11626 | verifyFormat("template <typename T> void f() {}"); |
11627 | verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); |
11628 | verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " |
11629 | "sizeof(char)>::type>;"); |
11630 | verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); |
11631 | verifyFormat("f(a.operator()<A>());"); |
11632 | verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
11633 | " .template operator()<A>());", |
11634 | getLLVMStyleWithColumns(35)); |
11635 | verifyFormat("bool_constant<a && noexcept(f())>;"); |
11636 | verifyFormat("bool_constant<a || noexcept(f())>;"); |
11637 | |
11638 | verifyFormat("if (std::tuple_size_v<T> > 0)"); |
11639 | |
11640 | // Not template parameters. |
11641 | verifyFormat("return a < b && c > d;"); |
11642 | verifyFormat("a < 0 ? b : a > 0 ? c : d;"); |
11643 | verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;"); |
11644 | verifyFormat("void f() {\n" |
11645 | " while (a < b && c > d) {\n" |
11646 | " }\n" |
11647 | "}"); |
11648 | verifyFormat("template <typename... Types>\n" |
11649 | "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); |
11650 | |
11651 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
11652 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", |
11653 | getLLVMStyleWithColumns(60)); |
11654 | verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); |
11655 | verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); |
11656 | verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); |
11657 | verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); |
11658 | |
11659 | verifyFormat("#define FOO(typeName, realClass) \\\n" |
11660 | " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}", |
11661 | getLLVMStyleWithColumns(60)); |
11662 | } |
11663 | |
11664 | TEST_F(FormatTest, UnderstandsShiftOperators) { |
11665 | verifyFormat("if (i < x >> 1)"); |
11666 | verifyFormat("while (i < x >> 1)"); |
11667 | verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); |
11668 | verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); |
11669 | verifyFormat( |
11670 | "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); |
11671 | verifyFormat("Foo.call<Bar<Function>>()"); |
11672 | verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); |
11673 | verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " |
11674 | "++i, v = v >> 1)"); |
11675 | verifyFormat("if (w<u<v<x>>, 1>::t)"); |
11676 | } |
11677 | |
11678 | TEST_F(FormatTest, BitshiftOperatorWidth) { |
11679 | verifyFormat("int a = 1 << 2; /* foo\n" |
11680 | " bar */", |
11681 | "int a=1<<2; /* foo\n" |
11682 | " bar */"); |
11683 | |
11684 | verifyFormat("int b = 256 >> 1; /* foo\n" |
11685 | " bar */", |
11686 | "int b =256>>1 ; /* foo\n" |
11687 | " bar */"); |
11688 | } |
11689 | |
11690 | TEST_F(FormatTest, UnderstandsBinaryOperators) { |
11691 | verifyFormat("COMPARE(a, ==, b);"); |
11692 | verifyFormat("auto s = sizeof...(Ts) - 1;"); |
11693 | } |
11694 | |
11695 | TEST_F(FormatTest, UnderstandsPointersToMembers) { |
11696 | verifyFormat("int A::*x;"); |
11697 | verifyFormat("int (S::*func)(void *);"); |
11698 | verifyFormat("void f() { int (S::*func)(void *); }"); |
11699 | verifyFormat("typedef bool *(Class::*Member)() const;"); |
11700 | verifyFormat("void f() {\n" |
11701 | " (a->*f)();\n" |
11702 | " a->*x;\n" |
11703 | " (a.*f)();\n" |
11704 | " ((*a).*f)();\n" |
11705 | " a.*x;\n" |
11706 | "}"); |
11707 | verifyFormat("void f() {\n" |
11708 | " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" |
11709 | " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" |
11710 | "}"); |
11711 | verifyFormat( |
11712 | "(aaaaaaaaaa->*bbbbbbb)(\n" |
11713 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
11714 | |
11715 | FormatStyle Style = getLLVMStyle(); |
11716 | EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); |
11717 | verifyFormat("typedef bool *(Class::*Member)() const;", Style); |
11718 | verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style); |
11719 | |
11720 | Style.PointerAlignment = FormatStyle::PAS_Left; |
11721 | verifyFormat("typedef bool* (Class::*Member)() const;", Style); |
11722 | verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style); |
11723 | |
11724 | Style.PointerAlignment = FormatStyle::PAS_Middle; |
11725 | verifyFormat("typedef bool * (Class::*Member)() const;", Style); |
11726 | verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style); |
11727 | } |
11728 | |
11729 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
11730 | verifyFormat("int a = -2;"); |
11731 | verifyFormat("f(-1, -2, -3);"); |
11732 | verifyFormat("a[-1] = 5;"); |
11733 | verifyFormat("int a = 5 + -2;"); |
11734 | verifyFormat("if (i == -1) {\n}"); |
11735 | verifyFormat("if (i != -1) {\n}"); |
11736 | verifyFormat("if (i > -1) {\n}"); |
11737 | verifyFormat("if (i < -1) {\n}"); |
11738 | verifyFormat("++(a->f());"); |
11739 | verifyFormat("--(a->f());"); |
11740 | verifyFormat("(a->f())++;"); |
11741 | verifyFormat("a[42]++;"); |
11742 | verifyFormat("if (!(a->f())) {\n}"); |
11743 | verifyFormat("if (!+i) {\n}"); |
11744 | verifyFormat("~&a;"); |
11745 | verifyFormat("for (x = 0; -10 < x; --x) {\n}"); |
11746 | verifyFormat("sizeof -x"); |
11747 | verifyFormat("sizeof +x"); |
11748 | verifyFormat("sizeof *x"); |
11749 | verifyFormat("sizeof &x"); |
11750 | verifyFormat("delete +x;"); |
11751 | verifyFormat("co_await +x;"); |
11752 | verifyFormat("case *x:"); |
11753 | verifyFormat("case &x:"); |
11754 | |
11755 | verifyFormat("a-- > b;"); |
11756 | verifyFormat("b ? -a : c;"); |
11757 | verifyFormat("n * sizeof char16;"); |
11758 | verifyGoogleFormat("n * alignof char16;"); |
11759 | verifyFormat("sizeof(char);"); |
11760 | verifyGoogleFormat("alignof(char);"); |
11761 | |
11762 | verifyFormat("return -1;"); |
11763 | verifyFormat("throw -1;"); |
11764 | verifyFormat("switch (a) {\n" |
11765 | "case -1:\n" |
11766 | " break;\n" |
11767 | "}"); |
11768 | verifyFormat("#define X -1"); |
11769 | verifyFormat("#define X -kConstant"); |
11770 | |
11771 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); |
11772 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); |
11773 | |
11774 | verifyFormat("int a = /* confusing comment */ -1;"); |
11775 | // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. |
11776 | verifyFormat("int a = i /* confusing comment */++;"); |
11777 | |
11778 | verifyFormat("co_yield -1;"); |
11779 | verifyFormat("co_return -1;"); |
11780 | |
11781 | // Check that * is not treated as a binary operator when we set |
11782 | // PointerAlignment as PAS_Left after a keyword and not a declaration. |
11783 | FormatStyle PASLeftStyle = getLLVMStyle(); |
11784 | PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; |
11785 | verifyFormat("co_return *a;", PASLeftStyle); |
11786 | verifyFormat("co_await *a;", PASLeftStyle); |
11787 | verifyFormat("co_yield *a", PASLeftStyle); |
11788 | verifyFormat("return *a;", PASLeftStyle); |
11789 | } |
11790 | |
11791 | TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { |
11792 | verifyFormat("if (!aaaaaaaaaa( // break\n" |
11793 | " aaaaa)) {\n" |
11794 | "}"); |
11795 | verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" |
11796 | " aaaaa));"); |
11797 | verifyFormat("*aaa = aaaaaaa( // break\n" |
11798 | " bbbbbb);"); |
11799 | } |
11800 | |
11801 | TEST_F(FormatTest, UnderstandsOverloadedOperators) { |
11802 | verifyFormat("bool operator<();"); |
11803 | verifyFormat("bool operator>();"); |
11804 | verifyFormat("bool operator=();"); |
11805 | verifyFormat("bool operator==();"); |
11806 | verifyFormat("bool operator!=();"); |
11807 | verifyFormat("int operator+();"); |
11808 | verifyFormat("int operator++();"); |
11809 | verifyFormat("int operator++(int) volatile noexcept;"); |
11810 | verifyFormat("bool operator,();"); |
11811 | verifyFormat("bool operator();"); |
11812 | verifyFormat("bool operator()();"); |
11813 | verifyFormat("bool operator[]();"); |
11814 | verifyFormat("operator bool();"); |
11815 | verifyFormat("operator int();"); |
11816 | verifyFormat("operator void *();"); |
11817 | verifyFormat("operator SomeType<int>();"); |
11818 | verifyFormat("operator SomeType<int, int>();"); |
11819 | verifyFormat("operator SomeType<SomeType<int>>();"); |
11820 | verifyFormat("operator< <>();"); |
11821 | verifyFormat("operator<< <>();"); |
11822 | verifyFormat("< <>"); |
11823 | |
11824 | verifyFormat("void *operator new(std::size_t size);"); |
11825 | verifyFormat("void *operator new[](std::size_t size);"); |
11826 | verifyFormat("void operator delete(void *ptr);"); |
11827 | verifyFormat("void operator delete[](void *ptr);"); |
11828 | verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" |
11829 | "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); |
11830 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" |
11831 | " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); |
11832 | |
11833 | verifyFormat( |
11834 | "ostream &operator<<(ostream &OutputStream,\n" |
11835 | " SomeReallyLongType WithSomeReallyLongValue);"); |
11836 | verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" |
11837 | " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" |
11838 | " return left.group < right.group;\n" |
11839 | "}"); |
11840 | verifyFormat("SomeType &operator=(const SomeType &S);"); |
11841 | verifyFormat("f.template operator()<int>();"); |
11842 | |
11843 | verifyGoogleFormat("operator void*();"); |
11844 | verifyGoogleFormat("operator SomeType<SomeType<int>>();"); |
11845 | verifyGoogleFormat("operator ::A();"); |
11846 | |
11847 | verifyFormat("using A::operator+;"); |
11848 | verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" |
11849 | "int i;"); |
11850 | |
11851 | // Calling an operator as a member function. |
11852 | verifyFormat("void f() { a.operator*(); }"); |
11853 | verifyFormat("void f() { a.operator*(b & b); }"); |
11854 | verifyFormat("void f() { a->operator&(a * b); }"); |
11855 | verifyFormat("void f() { NS::a.operator+(*b * *b); }"); |
11856 | verifyFormat("void f() { operator*(a & a); }"); |
11857 | verifyFormat("void f() { operator&(a, b * b); }"); |
11858 | |
11859 | verifyFormat("void f() { return operator()(x) * b; }"); |
11860 | verifyFormat("void f() { return operator[](x) * b; }"); |
11861 | verifyFormat("void f() { return operator\"\"_a(x) * b; }"); |
11862 | verifyFormat("void f() { return operator\"\" _a(x) * b; }"); |
11863 | verifyFormat("void f() { return operator\"\"s(x) * b; }"); |
11864 | verifyFormat("void f() { return operator\"\" s(x) * b; }"); |
11865 | verifyFormat("void f() { return operator\"\"if(x) * b; }"); |
11866 | |
11867 | verifyFormat("::operator delete(foo);"); |
11868 | verifyFormat("::operator new(n * sizeof(foo));"); |
11869 | verifyFormat("foo() { ::operator delete(foo); }"); |
11870 | verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); |
11871 | } |
11872 | |
11873 | TEST_F(FormatTest, SpaceBeforeTemplateCloser) { |
11874 | verifyFormat("C<&operator- > minus;"); |
11875 | verifyFormat("C<&operator> > gt;"); |
11876 | verifyFormat("C<&operator>= > ge;"); |
11877 | verifyFormat("C<&operator<= > le;"); |
11878 | verifyFormat("C<&operator< <X>> lt;"); |
11879 | } |
11880 | |
11881 | TEST_F(FormatTest, UnderstandsFunctionRefQualification) { |
11882 | verifyFormat("void A::b() && {}"); |
11883 | verifyFormat("void A::b() && noexcept {}"); |
11884 | verifyFormat("Deleted &operator=(const Deleted &) & = default;"); |
11885 | verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); |
11886 | verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;"); |
11887 | verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); |
11888 | verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); |
11889 | verifyFormat("Deleted &operator=(const Deleted &) &;"); |
11890 | verifyFormat("Deleted &operator=(const Deleted &) &&;"); |
11891 | verifyFormat("SomeType MemberFunction(const Deleted &) &;"); |
11892 | verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); |
11893 | verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); |
11894 | verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); |
11895 | verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); |
11896 | verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}"); |
11897 | verifyFormat("void Fn(T const &) const &;"); |
11898 | verifyFormat("void Fn(T const volatile &&) const volatile &&;"); |
11899 | verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;"); |
11900 | verifyGoogleFormat("template <typename T>\n" |
11901 | "void F(T) && = delete;"); |
11902 | verifyFormat("template <typename T> void operator=(T) &;"); |
11903 | verifyFormat("template <typename T> void operator=(T) const &;"); |
11904 | verifyFormat("template <typename T> void operator=(T) & noexcept;"); |
11905 | verifyFormat("template <typename T> void operator=(T) & = default;"); |
11906 | verifyFormat("template <typename T> void operator=(T) &&;"); |
11907 | verifyFormat("template <typename T> void operator=(T) && = delete;"); |
11908 | verifyFormat("template <typename T> void operator=(T) & {}"); |
11909 | verifyFormat("template <typename T> void operator=(T) && {}"); |
11910 | |
11911 | FormatStyle AlignLeft = getLLVMStyle(); |
11912 | AlignLeft.PointerAlignment = FormatStyle::PAS_Left; |
11913 | verifyFormat("void A::b() && {}", AlignLeft); |
11914 | verifyFormat("void A::b() && noexcept {}", AlignLeft); |
11915 | verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); |
11916 | verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;", |
11917 | AlignLeft); |
11918 | verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", |
11919 | AlignLeft); |
11920 | verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); |
11921 | verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); |
11922 | verifyFormat("auto Function(T t) & -> void {}", AlignLeft); |
11923 | verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); |
11924 | verifyFormat("auto Function(T) & -> void {}", AlignLeft); |
11925 | verifyFormat("auto Function(T) & -> void;", AlignLeft); |
11926 | verifyFormat("void Fn(T const&) const&;", AlignLeft); |
11927 | verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); |
11928 | verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;", |
11929 | AlignLeft); |
11930 | verifyFormat("template <typename T> void operator=(T) &;", AlignLeft); |
11931 | verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft); |
11932 | verifyFormat("template <typename T> void operator=(T) & noexcept;", |
11933 | AlignLeft); |
11934 | verifyFormat("template <typename T> void operator=(T) & = default;", |
11935 | AlignLeft); |
11936 | verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft); |
11937 | verifyFormat("template <typename T> void operator=(T) && = delete;", |
11938 | AlignLeft); |
11939 | verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft); |
11940 | verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft); |
11941 | verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft); |
11942 | |
11943 | FormatStyle AlignMiddle = getLLVMStyle(); |
11944 | AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle; |
11945 | verifyFormat("void A::b() && {}", AlignMiddle); |
11946 | verifyFormat("void A::b() && noexcept {}", AlignMiddle); |
11947 | verifyFormat("Deleted & operator=(const Deleted &) & = default;", |
11948 | AlignMiddle); |
11949 | verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;", |
11950 | AlignMiddle); |
11951 | verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", |
11952 | AlignMiddle); |
11953 | verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle); |
11954 | verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle); |
11955 | verifyFormat("auto Function(T t) & -> void {}", AlignMiddle); |
11956 | verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle); |
11957 | verifyFormat("auto Function(T) & -> void {}", AlignMiddle); |
11958 | verifyFormat("auto Function(T) & -> void;", AlignMiddle); |
11959 | verifyFormat("void Fn(T const &) const &;", AlignMiddle); |
11960 | verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle); |
11961 | verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;", |
11962 | AlignMiddle); |
11963 | verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle); |
11964 | verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle); |
11965 | verifyFormat("template <typename T> void operator=(T) & noexcept;", |
11966 | AlignMiddle); |
11967 | verifyFormat("template <typename T> void operator=(T) & = default;", |
11968 | AlignMiddle); |
11969 | verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle); |
11970 | verifyFormat("template <typename T> void operator=(T) && = delete;", |
11971 | AlignMiddle); |
11972 | verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle); |
11973 | verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle); |
11974 | |
11975 | FormatStyle Spaces = getLLVMStyle(); |
11976 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
11977 | Spaces.SpacesInParensOptions = {}; |
11978 | Spaces.SpacesInParensOptions.InCStyleCasts = true; |
11979 | verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); |
11980 | verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); |
11981 | verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); |
11982 | verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); |
11983 | |
11984 | Spaces.SpacesInParensOptions.InCStyleCasts = false; |
11985 | Spaces.SpacesInParensOptions.Other = true; |
11986 | verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); |
11987 | verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", |
11988 | Spaces); |
11989 | verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); |
11990 | verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); |
11991 | |
11992 | FormatStyle BreakTemplate = getLLVMStyle(); |
11993 | BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; |
11994 | |
11995 | verifyFormat("struct f {\n" |
11996 | " template <class T>\n" |
11997 | " int &foo(const std::string &str) & noexcept {}\n" |
11998 | "};", |
11999 | BreakTemplate); |
12000 | |
12001 | verifyFormat("struct f {\n" |
12002 | " template <class T>\n" |
12003 | " int &foo(const std::string &str) && noexcept {}\n" |
12004 | "};", |
12005 | BreakTemplate); |
12006 | |
12007 | verifyFormat("struct f {\n" |
12008 | " template <class T>\n" |
12009 | " int &foo(const std::string &str) const & noexcept {}\n" |
12010 | "};", |
12011 | BreakTemplate); |
12012 | |
12013 | verifyFormat("struct f {\n" |
12014 | " template <class T>\n" |
12015 | " int &foo(const std::string &str) const & noexcept {}\n" |
12016 | "};", |
12017 | BreakTemplate); |
12018 | |
12019 | verifyFormat("struct f {\n" |
12020 | " template <class T>\n" |
12021 | " auto foo(const std::string &str) && noexcept -> int & {}\n" |
12022 | "};", |
12023 | BreakTemplate); |
12024 | |
12025 | FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); |
12026 | AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; |
12027 | AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; |
12028 | |
12029 | verifyFormat("struct f {\n" |
12030 | " template <class T>\n" |
12031 | " int& foo(const std::string& str) & noexcept {}\n" |
12032 | "};", |
12033 | AlignLeftBreakTemplate); |
12034 | |
12035 | verifyFormat("struct f {\n" |
12036 | " template <class T>\n" |
12037 | " int& foo(const std::string& str) && noexcept {}\n" |
12038 | "};", |
12039 | AlignLeftBreakTemplate); |
12040 | |
12041 | verifyFormat("struct f {\n" |
12042 | " template <class T>\n" |
12043 | " int& foo(const std::string& str) const& noexcept {}\n" |
12044 | "};", |
12045 | AlignLeftBreakTemplate); |
12046 | |
12047 | verifyFormat("struct f {\n" |
12048 | " template <class T>\n" |
12049 | " int& foo(const std::string& str) const&& noexcept {}\n" |
12050 | "};", |
12051 | AlignLeftBreakTemplate); |
12052 | |
12053 | verifyFormat("struct f {\n" |
12054 | " template <class T>\n" |
12055 | " auto foo(const std::string& str) && noexcept -> int& {}\n" |
12056 | "};", |
12057 | AlignLeftBreakTemplate); |
12058 | |
12059 | // The `&` in `Type&` should not be confused with a trailing `&` of |
12060 | // DEPRECATED(reason) member function. |
12061 | verifyFormat("struct f {\n" |
12062 | " template <class T>\n" |
12063 | " DEPRECATED(reason)\n" |
12064 | " Type &foo(arguments) {}\n" |
12065 | "};", |
12066 | BreakTemplate); |
12067 | |
12068 | verifyFormat("struct f {\n" |
12069 | " template <class T>\n" |
12070 | " DEPRECATED(reason)\n" |
12071 | " Type& foo(arguments) {}\n" |
12072 | "};", |
12073 | AlignLeftBreakTemplate); |
12074 | |
12075 | verifyFormat("void (*foopt)(int) = &func;"); |
12076 | |
12077 | FormatStyle DerivePointerAlignment = getLLVMStyle(); |
12078 | DerivePointerAlignment.DerivePointerAlignment = true; |
12079 | // There's always a space between the function and its trailing qualifiers. |
12080 | // This isn't evidence for PAS_Right (or for PAS_Left). |
12081 | std::string Prefix = "void a() &;\n" |
12082 | "void b() &;\n"; |
12083 | verifyFormat(Prefix + "int* x;", DerivePointerAlignment); |
12084 | verifyFormat(Prefix + "int *x;", DerivePointerAlignment); |
12085 | // Same if the function is an overloaded operator, and with &&. |
12086 | Prefix = "void operator()() &&;\n" |
12087 | "void operator()() &&;\n"; |
12088 | verifyFormat(Prefix + "int* x;", DerivePointerAlignment); |
12089 | verifyFormat(Prefix + "int *x;", DerivePointerAlignment); |
12090 | // However a space between cv-qualifiers and ref-qualifiers *is* evidence. |
12091 | Prefix = "void a() const &;\n" |
12092 | "void b() const &;\n"; |
12093 | verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment); |
12094 | } |
12095 | |
12096 | TEST_F(FormatTest, PointerAlignmentFallback) { |
12097 | FormatStyle Style = getLLVMStyle(); |
12098 | Style.DerivePointerAlignment = true; |
12099 | |
12100 | const StringRef Code("int* p;\n" |
12101 | "int *q;\n" |
12102 | "int * r;"); |
12103 | |
12104 | EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); |
12105 | verifyFormat("int *p;\n" |
12106 | "int *q;\n" |
12107 | "int *r;", |
12108 | Code, Style); |
12109 | |
12110 | Style.PointerAlignment = FormatStyle::PAS_Left; |
12111 | verifyFormat("int* p;\n" |
12112 | "int* q;\n" |
12113 | "int* r;", |
12114 | Code, Style); |
12115 | |
12116 | Style.PointerAlignment = FormatStyle::PAS_Middle; |
12117 | verifyFormat("int * p;\n" |
12118 | "int * q;\n" |
12119 | "int * r;", |
12120 | Code, Style); |
12121 | } |
12122 | |
12123 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
12124 | verifyFormat("A(void *p) : a(new (p) int) {}"); |
12125 | verifyFormat("void f() {\n" |
12126 | " A *a = new A;\n" |
12127 | " A *a = new (placement) A;\n" |
12128 | " delete a;\n" |
12129 | " delete (A *)a;\n" |
12130 | "}"); |
12131 | verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" |
12132 | " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); |
12133 | verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
12134 | " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" |
12135 | " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); |
12136 | verifyFormat("delete[] h->p;"); |
12137 | verifyFormat("delete[] (void *)p;"); |
12138 | |
12139 | verifyFormat("void operator delete(void *foo) ATTRIB;"); |
12140 | verifyFormat("void operator new(void *foo) ATTRIB;"); |
12141 | verifyFormat("void operator delete[](void *foo) ATTRIB;"); |
12142 | verifyFormat("void operator delete(void *ptr) noexcept;"); |
12143 | |
12144 | verifyFormat("void new(link p);\n" |
12145 | "void delete(link p);", |
12146 | "void new (link p);\n" |
12147 | "void delete (link p);", |
12148 | getLLVMStyle(FormatStyle::LK_C)); |
12149 | |
12150 | verifyFormat("{\n" |
12151 | " p->new();\n" |
12152 | "}\n" |
12153 | "{\n" |
12154 | " p->delete();\n" |
12155 | "}", |
12156 | "{\n" |
12157 | " p->new ();\n" |
12158 | "}\n" |
12159 | "{\n" |
12160 | " p->delete ();\n" |
12161 | "}"); |
12162 | |
12163 | FormatStyle AfterPlacementOperator = getLLVMStyle(); |
12164 | AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
12165 | EXPECT_TRUE( |
12166 | AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator); |
12167 | verifyFormat("new (buf) int;", AfterPlacementOperator); |
12168 | verifyFormat("struct A {\n" |
12169 | " int *a;\n" |
12170 | " A(int *p) : a(new (p) int) {\n" |
12171 | " new (p) int;\n" |
12172 | " int *b = new (p) int;\n" |
12173 | " int *c = new (p) int(3);\n" |
12174 | " delete (b);\n" |
12175 | " }\n" |
12176 | "};", |
12177 | AfterPlacementOperator); |
12178 | verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator); |
12179 | verifyFormat("delete (int *)p;", AfterPlacementOperator); |
12180 | |
12181 | AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator = |
12182 | false; |
12183 | verifyFormat("new(buf) int;", AfterPlacementOperator); |
12184 | verifyFormat("struct A {\n" |
12185 | " int *a;\n" |
12186 | " A(int *p) : a(new(p) int) {\n" |
12187 | " new(p) int;\n" |
12188 | " int *b = new(p) int;\n" |
12189 | " int *c = new(p) int(3);\n" |
12190 | " delete(b);\n" |
12191 | " }\n" |
12192 | "};", |
12193 | AfterPlacementOperator); |
12194 | verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator); |
12195 | verifyFormat("delete (int *)p;", AfterPlacementOperator); |
12196 | } |
12197 | |
12198 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
12199 | verifyFormat("int *f(int *a) {}"); |
12200 | verifyFormat("int main(int argc, char **argv) {}"); |
12201 | verifyFormat("Test::Test(int b) : a(b * b) {}"); |
12202 | verifyIndependentOfContext("f(a, *a);"); |
12203 | verifyFormat("void g() { f(*a); }"); |
12204 | verifyIndependentOfContext("int a = b * 10;"); |
12205 | verifyIndependentOfContext("int a = 10 * b;"); |
12206 | verifyIndependentOfContext("int a = b * c;"); |
12207 | verifyIndependentOfContext("int a += b * c;"); |
12208 | verifyIndependentOfContext("int a -= b * c;"); |
12209 | verifyIndependentOfContext("int a *= b * c;"); |
12210 | verifyIndependentOfContext("int a /= b * c;"); |
12211 | verifyIndependentOfContext("int a = *b;"); |
12212 | verifyIndependentOfContext("int a = *b * c;"); |
12213 | verifyIndependentOfContext("int a = b * *c;"); |
12214 | verifyIndependentOfContext("int a = b * (10);"); |
12215 | verifyIndependentOfContext("S << b * (10);"); |
12216 | verifyIndependentOfContext("return 10 * b;"); |
12217 | verifyIndependentOfContext("return *b * *c;"); |
12218 | verifyIndependentOfContext("return a & ~b;"); |
12219 | verifyIndependentOfContext("f(b ? *c : *d);"); |
12220 | verifyIndependentOfContext("int a = b ? *c : *d;"); |
12221 | verifyIndependentOfContext("*b = a;"); |
12222 | verifyIndependentOfContext("a * ~b;"); |
12223 | verifyIndependentOfContext("a * !b;"); |
12224 | verifyIndependentOfContext("a * +b;"); |
12225 | verifyIndependentOfContext("a * -b;"); |
12226 | verifyIndependentOfContext("a * ++b;"); |
12227 | verifyIndependentOfContext("a * --b;"); |
12228 | verifyIndependentOfContext("a[4] * b;"); |
12229 | verifyIndependentOfContext("a[a * a] = 1;"); |
12230 | verifyIndependentOfContext("f() * b;"); |
12231 | verifyIndependentOfContext("a * [self dostuff];"); |
12232 | verifyIndependentOfContext("int x = a * (a + b);"); |
12233 | verifyIndependentOfContext("(a *)(a + b);"); |
12234 | verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); |
12235 | verifyIndependentOfContext("int *pa = (int *)&a;"); |
12236 | verifyIndependentOfContext("return sizeof(int **);"); |
12237 | verifyIndependentOfContext("return sizeof(int ******);"); |
12238 | verifyIndependentOfContext("return (int **&)a;"); |
12239 | verifyIndependentOfContext("f((*PointerToArray)[10]);"); |
12240 | verifyFormat("void f(Type (*parameter)[10]) {}"); |
12241 | verifyFormat("void f(Type (¶meter)[10]) {}"); |
12242 | verifyGoogleFormat("return sizeof(int**);"); |
12243 | verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); |
12244 | verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); |
12245 | verifyFormat("auto a = [](int **&, int ***) {};"); |
12246 | verifyFormat("auto PointerBinding = [](const char *S) {};"); |
12247 | verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); |
12248 | verifyFormat("[](const decltype(*a) &value) {}"); |
12249 | verifyFormat("[](const typeof(*a) &value) {}"); |
12250 | verifyFormat("[](const _Atomic(a *) &value) {}"); |
12251 | verifyFormat("[](const __underlying_type(a) &value) {}"); |
12252 | verifyFormat("decltype(a * b) F();"); |
12253 | verifyFormat("typeof(a * b) F();"); |
12254 | verifyFormat("#define MACRO() [](A *a) { return 1; }"); |
12255 | verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); |
12256 | verifyIndependentOfContext("typedef void (*f)(int *a);"); |
12257 | verifyIndependentOfContext("typedef void (*f)(Type *a);"); |
12258 | verifyIndependentOfContext("int i{a * b};"); |
12259 | verifyIndependentOfContext("aaa && aaa->f();"); |
12260 | verifyIndependentOfContext("int x = ~*p;"); |
12261 | verifyFormat("Constructor() : a(a), area(width * height) {}"); |
12262 | verifyFormat("Constructor() : a(a), area(a, width * height) {}"); |
12263 | verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); |
12264 | verifyFormat("void f() { f(a, c * d); }"); |
12265 | verifyFormat("void f() { f(new a(), c * d); }"); |
12266 | verifyFormat("void f(const MyOverride &override);"); |
12267 | verifyFormat("void f(const MyFinal &final);"); |
12268 | verifyIndependentOfContext("bool a = f() && override.f();"); |
12269 | verifyIndependentOfContext("bool a = f() && final.f();"); |
12270 | |
12271 | verifyIndependentOfContext("InvalidRegions[*R] = 0;"); |
12272 | |
12273 | verifyIndependentOfContext("A<int *> a;"); |
12274 | verifyIndependentOfContext("A<int **> a;"); |
12275 | verifyIndependentOfContext("A<int *, int *> a;"); |
12276 | verifyIndependentOfContext("A<int *[]> a;"); |
12277 | verifyIndependentOfContext( |
12278 | "const char *const p = reinterpret_cast<const char *const>(q);"); |
12279 | verifyIndependentOfContext("A<int **, int **> a;"); |
12280 | verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); |
12281 | verifyFormat("for (char **a = b; *a; ++a) {\n}"); |
12282 | verifyFormat("for (; a && b;) {\n}"); |
12283 | verifyFormat("bool foo = true && [] { return false; }();"); |
12284 | |
12285 | verifyFormat( |
12286 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
12287 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
12288 | |
12289 | verifyGoogleFormat("int const* a = &b;"); |
12290 | verifyGoogleFormat("**outparam = 1;"); |
12291 | verifyGoogleFormat("*outparam = a * b;"); |
12292 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
12293 | verifyGoogleFormat("A<int*> a;"); |
12294 | verifyGoogleFormat("A<int**> a;"); |
12295 | verifyGoogleFormat("A<int*, int*> a;"); |
12296 | verifyGoogleFormat("A<int**, int**> a;"); |
12297 | verifyGoogleFormat("f(b ? *c : *d);"); |
12298 | verifyGoogleFormat("int a = b ? *c : *d;"); |
12299 | verifyGoogleFormat("Type* t = **x;"); |
12300 | verifyGoogleFormat("Type* t = *++*x;"); |
12301 | verifyGoogleFormat("*++*x;"); |
12302 | verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); |
12303 | verifyGoogleFormat("Type* t = x++ * y;"); |
12304 | verifyGoogleFormat( |
12305 | "const char* const p = reinterpret_cast<const char* const>(q);"); |
12306 | verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); |
12307 | verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); |
12308 | verifyGoogleFormat("template <typename T>\n" |
12309 | "void f(int i = 0, SomeType** temps = NULL);"); |
12310 | |
12311 | FormatStyle Left = getLLVMStyle(); |
12312 | Left.PointerAlignment = FormatStyle::PAS_Left; |
12313 | verifyFormat("x = *a(x) = *a(y);", Left); |
12314 | verifyFormat("for (;; *a = b) {\n}", Left); |
12315 | verifyFormat("return *this += 1;", Left); |
12316 | verifyFormat("throw *x;", Left); |
12317 | verifyFormat("delete *x;", Left); |
12318 | verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); |
12319 | verifyFormat("[](const decltype(*a)* ptr) {}", Left); |
12320 | verifyFormat("[](const typeof(*a)* ptr) {}", Left); |
12321 | verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); |
12322 | verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); |
12323 | verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); |
12324 | verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); |
12325 | verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); |
12326 | verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); |
12327 | |
12328 | verifyIndependentOfContext("a = *(x + y);"); |
12329 | verifyIndependentOfContext("a = &(x + y);"); |
12330 | verifyIndependentOfContext("*(x + y).call();"); |
12331 | verifyIndependentOfContext("&(x + y)->call();"); |
12332 | verifyFormat("void f() { &(*I).first; }"); |
12333 | |
12334 | verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); |
12335 | verifyFormat("f(* /* confusing comment */ foo);"); |
12336 | verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); |
12337 | verifyFormat("void foo(int * // this is the first paramters\n" |
12338 | " ,\n" |
12339 | " int second);"); |
12340 | verifyFormat("double term = a * // first\n" |
12341 | " b;"); |
12342 | verifyFormat( |
12343 | "int *MyValues = {\n" |
12344 | " *A, // Operator detection might be confused by the '{'\n" |
12345 | " *BB // Operator detection might be confused by previous comment\n" |
12346 | "};"); |
12347 | |
12348 | verifyIndependentOfContext("if (int *a = &b)"); |
12349 | verifyIndependentOfContext("if (int &a = *b)"); |
12350 | verifyIndependentOfContext("if (a & b[i])"); |
12351 | verifyIndependentOfContext("if constexpr (a & b[i])"); |
12352 | verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); |
12353 | verifyIndependentOfContext("if (a * (b * c))"); |
12354 | verifyIndependentOfContext("if constexpr (a * (b * c))"); |
12355 | verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); |
12356 | verifyIndependentOfContext("if (a::b::c::d & b[i])"); |
12357 | verifyIndependentOfContext("if (*b[i])"); |
12358 | verifyIndependentOfContext("if (int *a = (&b))"); |
12359 | verifyIndependentOfContext("while (int *a = &b)"); |
12360 | verifyIndependentOfContext("while (a * (b * c))"); |
12361 | verifyIndependentOfContext("size = sizeof *a;"); |
12362 | verifyIndependentOfContext("if (a && (b = c))"); |
12363 | verifyFormat("void f() {\n" |
12364 | " for (const int &v : Values) {\n" |
12365 | " }\n" |
12366 | "}"); |
12367 | verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); |
12368 | verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); |
12369 | verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); |
12370 | |
12371 | verifyFormat("#define A (!a * b)"); |
12372 | verifyFormat("#define MACRO \\\n" |
12373 | " int *i = a * b; \\\n" |
12374 | " void f(a *b);", |
12375 | getLLVMStyleWithColumns(19)); |
12376 | |
12377 | verifyIndependentOfContext("A = new SomeType *[Length];"); |
12378 | verifyIndependentOfContext("A = new SomeType *[Length]();"); |
12379 | verifyIndependentOfContext("T **t = new T *;"); |
12380 | verifyIndependentOfContext("T **t = new T *();"); |
12381 | verifyGoogleFormat("A = new SomeType*[Length]();"); |
12382 | verifyGoogleFormat("A = new SomeType*[Length];"); |
12383 | verifyGoogleFormat("T** t = new T*;"); |
12384 | verifyGoogleFormat("T** t = new T*();"); |
12385 | |
12386 | verifyFormat("STATIC_ASSERT((a & b) == 0);"); |
12387 | verifyFormat("STATIC_ASSERT(0 == (a & b));"); |
12388 | verifyFormat("template <bool a, bool b> " |
12389 | "typename t::if<x && y>::type f() {}"); |
12390 | verifyFormat("template <int *y> f() {}"); |
12391 | verifyFormat("vector<int *> v;"); |
12392 | verifyFormat("vector<int *const> v;"); |
12393 | verifyFormat("vector<int *const **const *> v;"); |
12394 | verifyFormat("vector<int *volatile> v;"); |
12395 | verifyFormat("vector<a *_Nonnull> v;"); |
12396 | verifyFormat("vector<a *_Nullable> v;"); |
12397 | verifyFormat("vector<a *_Null_unspecified> v;"); |
12398 | verifyGoogleFormat("vector<a* absl_nonnull> v;"); |
12399 | verifyGoogleFormat("vector<a* absl_nullable> v;"); |
12400 | verifyGoogleFormat("vector<a* absl_nullability_unknown> v;"); |
12401 | verifyFormat("vector<a *__ptr32> v;"); |
12402 | verifyFormat("vector<a *__ptr64> v;"); |
12403 | verifyFormat("vector<a *__capability> v;"); |
12404 | FormatStyle TypeMacros = getLLVMStyle(); |
12405 | TypeMacros.TypenameMacros = {"LIST"}; |
12406 | verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); |
12407 | verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); |
12408 | verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); |
12409 | verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); |
12410 | verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication |
12411 | |
12412 | FormatStyle CustomQualifier = getLLVMStyle(); |
12413 | // Add identifiers that should not be parsed as a qualifier by default. |
12414 | CustomQualifier.AttributeMacros.push_back(x: "__my_qualifier"); |
12415 | CustomQualifier.AttributeMacros.push_back(x: "_My_qualifier"); |
12416 | CustomQualifier.AttributeMacros.push_back(x: "my_other_qualifier"); |
12417 | verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); |
12418 | verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); |
12419 | verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); |
12420 | verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); |
12421 | verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); |
12422 | verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); |
12423 | verifyFormat("vector<a * _NotAQualifier> v;"); |
12424 | verifyFormat("vector<a * __not_a_qualifier> v;"); |
12425 | verifyFormat("vector<a * b> v;"); |
12426 | verifyFormat("foo<b && false>();"); |
12427 | verifyFormat("foo<b & 1>();"); |
12428 | verifyFormat("foo<b & (1)>();"); |
12429 | verifyFormat("foo<b & (~0)>();"); |
12430 | verifyFormat("foo<b & (true)>();"); |
12431 | verifyFormat("foo<b & ((1))>();"); |
12432 | verifyFormat("foo<b & (/*comment*/ 1)>();"); |
12433 | verifyFormat("decltype(*::std::declval<const T &>()) void F();"); |
12434 | verifyFormat("typeof(*::std::declval<const T &>()) void F();"); |
12435 | verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); |
12436 | verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); |
12437 | verifyFormat( |
12438 | "template <class T, class = typename std::enable_if<\n" |
12439 | " std::is_integral<T>::value &&\n" |
12440 | " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" |
12441 | "void F();", |
12442 | getLLVMStyleWithColumns(70)); |
12443 | verifyFormat("template <class T,\n" |
12444 | " class = typename std::enable_if<\n" |
12445 | " std::is_integral<T>::value &&\n" |
12446 | " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" |
12447 | " class U>\n" |
12448 | "void F();", |
12449 | getLLVMStyleWithColumns(70)); |
12450 | verifyFormat( |
12451 | "template <class T,\n" |
12452 | " class = typename ::std::enable_if<\n" |
12453 | " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" |
12454 | "void F();", |
12455 | getGoogleStyleWithColumns(68)); |
12456 | |
12457 | FormatStyle Style = getLLVMStyle(); |
12458 | Style.PointerAlignment = FormatStyle::PAS_Left; |
12459 | verifyFormat("struct {\n" |
12460 | "}* ptr;", |
12461 | Style); |
12462 | verifyFormat("union {\n" |
12463 | "}* ptr;", |
12464 | Style); |
12465 | verifyFormat("class {\n" |
12466 | "}* ptr;", |
12467 | Style); |
12468 | // Don't confuse a multiplication after a brace-initialized expression with |
12469 | // a class pointer. |
12470 | verifyFormat("int i = int{42} * 34;", Style); |
12471 | verifyFormat("struct {\n" |
12472 | "}&& ptr = {};", |
12473 | Style); |
12474 | verifyFormat("union {\n" |
12475 | "}&& ptr = {};", |
12476 | Style); |
12477 | verifyFormat("class {\n" |
12478 | "}&& ptr = {};", |
12479 | Style); |
12480 | verifyFormat("bool b = 3 == int{3} && true;"); |
12481 | |
12482 | Style.PointerAlignment = FormatStyle::PAS_Middle; |
12483 | verifyFormat("struct {\n" |
12484 | "} * ptr;", |
12485 | Style); |
12486 | verifyFormat("union {\n" |
12487 | "} * ptr;", |
12488 | Style); |
12489 | verifyFormat("class {\n" |
12490 | "} * ptr;", |
12491 | Style); |
12492 | verifyFormat("struct {\n" |
12493 | "} && ptr = {};", |
12494 | Style); |
12495 | verifyFormat("union {\n" |
12496 | "} && ptr = {};", |
12497 | Style); |
12498 | verifyFormat("class {\n" |
12499 | "} && ptr = {};", |
12500 | Style); |
12501 | |
12502 | Style.PointerAlignment = FormatStyle::PAS_Right; |
12503 | verifyFormat("struct {\n" |
12504 | "} *ptr;", |
12505 | Style); |
12506 | verifyFormat("union {\n" |
12507 | "} *ptr;", |
12508 | Style); |
12509 | verifyFormat("class {\n" |
12510 | "} *ptr;", |
12511 | Style); |
12512 | verifyFormat("struct {\n" |
12513 | "} &&ptr = {};", |
12514 | Style); |
12515 | verifyFormat("union {\n" |
12516 | "} &&ptr = {};", |
12517 | Style); |
12518 | verifyFormat("class {\n" |
12519 | "} &&ptr = {};", |
12520 | Style); |
12521 | |
12522 | Style.PointerAlignment = FormatStyle::PAS_Left; |
12523 | verifyFormat("delete[] *ptr;", Style); |
12524 | verifyFormat("delete[] **ptr;", Style); |
12525 | verifyFormat("delete[] *(ptr);", Style); |
12526 | |
12527 | verifyIndependentOfContext("MACRO(int *i);"); |
12528 | verifyIndependentOfContext("MACRO(auto *a);"); |
12529 | verifyIndependentOfContext("MACRO(const A *a);"); |
12530 | verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); |
12531 | verifyIndependentOfContext("MACRO(decltype(A) *a);"); |
12532 | verifyIndependentOfContext("MACRO(typeof(A) *a);"); |
12533 | verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); |
12534 | verifyIndependentOfContext("MACRO(A *const a);"); |
12535 | verifyIndependentOfContext("MACRO(A *restrict a);"); |
12536 | verifyIndependentOfContext("MACRO(A *__restrict__ a);"); |
12537 | verifyIndependentOfContext("MACRO(A *__restrict a);"); |
12538 | verifyIndependentOfContext("MACRO(A *volatile a);"); |
12539 | verifyIndependentOfContext("MACRO(A *__volatile a);"); |
12540 | verifyIndependentOfContext("MACRO(A *__volatile__ a);"); |
12541 | verifyIndependentOfContext("MACRO(A *_Nonnull a);"); |
12542 | verifyIndependentOfContext("MACRO(A *_Nullable a);"); |
12543 | verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); |
12544 | |
12545 | Style = getGoogleStyle(); |
12546 | verifyIndependentOfContext("MACRO(A* absl_nonnull a);", Style); |
12547 | verifyIndependentOfContext("MACRO(A* absl_nullable a);", Style); |
12548 | verifyIndependentOfContext("MACRO(A* absl_nullability_unknown a);", Style); |
12549 | |
12550 | verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); |
12551 | verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); |
12552 | verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); |
12553 | verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); |
12554 | verifyIndependentOfContext("MACRO(A *__ptr32 a);"); |
12555 | verifyIndependentOfContext("MACRO(A *__ptr64 a);"); |
12556 | verifyIndependentOfContext("MACRO(A *__capability);"); |
12557 | verifyIndependentOfContext("MACRO(A &__capability);"); |
12558 | verifyFormat("MACRO(A *__my_qualifier);"); // type declaration |
12559 | verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication |
12560 | // If we add __my_qualifier to AttributeMacros it should always be parsed as |
12561 | // a type declaration: |
12562 | verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); |
12563 | verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); |
12564 | // Also check that TypenameMacros prevents parsing it as multiplication: |
12565 | verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication |
12566 | verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type |
12567 | |
12568 | verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); |
12569 | verifyFormat("void f() { f(float{1}, a * a); }"); |
12570 | verifyFormat("void f() { f(float(1), a * a); }"); |
12571 | |
12572 | verifyFormat("f((void (*)(int))g);"); |
12573 | verifyFormat("f((void (&)(int))g);"); |
12574 | verifyFormat("f((void (^)(int))g);"); |
12575 | |
12576 | // FIXME: Is there a way to make this work? |
12577 | // verifyIndependentOfContext("MACRO(A *a);"); |
12578 | verifyFormat("MACRO(A &B);"); |
12579 | verifyFormat("MACRO(A *B);"); |
12580 | verifyFormat("void f() { MACRO(A * B); }"); |
12581 | verifyFormat("void f() { MACRO(A & B); }"); |
12582 | |
12583 | // This lambda was mis-formatted after D88956 (treating it as a binop): |
12584 | verifyFormat("auto x = [](const decltype(x) &ptr) {};"); |
12585 | verifyFormat("auto x = [](const decltype(x) *ptr) {};"); |
12586 | verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); |
12587 | verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); |
12588 | |
12589 | verifyFormat("DatumHandle const *operator->() const { return input_; }"); |
12590 | verifyFormat("return options != nullptr && operator==(*options);"); |
12591 | |
12592 | verifyFormat("#define OP(x) \\\n" |
12593 | " ostream &operator<<(ostream &s, const A &a) { \\\n" |
12594 | " return s << a.DebugString(); \\\n" |
12595 | " }", |
12596 | "#define OP(x) \\\n" |
12597 | " ostream &operator<<(ostream &s, const A &a) { \\\n" |
12598 | " return s << a.DebugString(); \\\n" |
12599 | " }", |
12600 | getLLVMStyleWithColumns(50)); |
12601 | |
12602 | verifyFormat("#define FOO \\\n" |
12603 | " void foo() { \\\n" |
12604 | " operator+(a * b); \\\n" |
12605 | " }", |
12606 | getLLVMStyleWithColumns(25)); |
12607 | |
12608 | // FIXME: We cannot handle this case yet; we might be able to figure out that |
12609 | // foo<x> d > v; doesn't make sense. |
12610 | verifyFormat("foo<a<b && c> d> v;"); |
12611 | |
12612 | FormatStyle PointerMiddle = getLLVMStyle(); |
12613 | PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; |
12614 | verifyFormat("delete *x;", PointerMiddle); |
12615 | verifyFormat("int * x;", PointerMiddle); |
12616 | verifyFormat("int *[] x;", PointerMiddle); |
12617 | verifyFormat("template <int * y> f() {}", PointerMiddle); |
12618 | verifyFormat("int * f(int * a) {}", PointerMiddle); |
12619 | verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); |
12620 | verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); |
12621 | verifyFormat("A<int *> a;", PointerMiddle); |
12622 | verifyFormat("A<int **> a;", PointerMiddle); |
12623 | verifyFormat("A<int *, int *> a;", PointerMiddle); |
12624 | verifyFormat("A<int *[]> a;", PointerMiddle); |
12625 | verifyFormat("A = new SomeType *[Length]();", PointerMiddle); |
12626 | verifyFormat("A = new SomeType *[Length];", PointerMiddle); |
12627 | verifyFormat("T ** t = new T *;", PointerMiddle); |
12628 | |
12629 | // Member function reference qualifiers aren't binary operators. |
12630 | verifyFormat("string // break\n" |
12631 | "operator()() & {}"); |
12632 | verifyFormat("string // break\n" |
12633 | "operator()() && {}"); |
12634 | verifyGoogleFormat("template <typename T>\n" |
12635 | "auto x() & -> int {}"); |
12636 | |
12637 | // Should be binary operators when used as an argument expression (overloaded |
12638 | // operator invoked as a member function). |
12639 | verifyFormat("void f() { a.operator()(a * a); }"); |
12640 | verifyFormat("void f() { a->operator()(a & a); }"); |
12641 | verifyFormat("void f() { a.operator()(*a & *a); }"); |
12642 | verifyFormat("void f() { a->operator()(*a * *a); }"); |
12643 | |
12644 | verifyFormat("int operator()(T (&&)[N]) { return 1; }"); |
12645 | verifyFormat("int operator()(T (&)[N]) { return 0; }"); |
12646 | |
12647 | verifyFormat("val1 & val2;"); |
12648 | verifyFormat("val1 & val2 & val3;"); |
12649 | verifyFormat("class c {\n" |
12650 | " void func(type &a) { a & member; }\n" |
12651 | " anotherType &member;\n" |
12652 | "}"); |
12653 | } |
12654 | |
12655 | TEST_F(FormatTest, UnderstandsAttributes) { |
12656 | verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); |
12657 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" |
12658 | "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); |
12659 | verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); |
12660 | FormatStyle AfterType = getLLVMStyle(); |
12661 | AfterType.BreakAfterReturnType = FormatStyle::RTBS_All; |
12662 | verifyFormat("__attribute__((nodebug)) void\n" |
12663 | "foo() {}", |
12664 | AfterType); |
12665 | verifyFormat("__unused void\n" |
12666 | "foo() {}", |
12667 | AfterType); |
12668 | |
12669 | FormatStyle CustomAttrs = getLLVMStyle(); |
12670 | CustomAttrs.AttributeMacros.push_back(x: "my_attr_name"); |
12671 | verifyFormat("void MyGoodOldFunction(\n" |
12672 | " void *const long_enough = nullptr,\n" |
12673 | " void *my_attr_name even_longeeeeeeeeeeeeeeeeer = nullptr);", |
12674 | CustomAttrs); |
12675 | |
12676 | CustomAttrs.AttributeMacros.push_back(x: "__unused"); |
12677 | CustomAttrs.AttributeMacros.push_back(x: "__attr1"); |
12678 | CustomAttrs.AttributeMacros.push_back(x: "__attr2"); |
12679 | CustomAttrs.AttributeMacros.push_back(x: "no_underscore_attr"); |
12680 | verifyFormat("vector<SomeType *__attribute((foo))> v;"); |
12681 | verifyFormat("vector<SomeType *__attribute__((foo))> v;"); |
12682 | verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); |
12683 | // Check that it is parsed as a multiplication without AttributeMacros and |
12684 | // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. |
12685 | verifyFormat("vector<SomeType * __attr1> v;"); |
12686 | verifyFormat("vector<SomeType __attr1 *> v;"); |
12687 | verifyFormat("vector<SomeType __attr1 *const> v;"); |
12688 | verifyFormat("vector<SomeType __attr1 * __attr2> v;"); |
12689 | verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); |
12690 | verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); |
12691 | verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); |
12692 | verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); |
12693 | verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); |
12694 | verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); |
12695 | verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); |
12696 | verifyFormat("__attr1 ::qualified_type f();", CustomAttrs); |
12697 | verifyFormat("__attr1() ::qualified_type f();", CustomAttrs); |
12698 | verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs); |
12699 | |
12700 | // Check that these are not parsed as function declarations: |
12701 | CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
12702 | CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; |
12703 | verifyFormat("SomeType s(InitValue);", CustomAttrs); |
12704 | verifyFormat("SomeType s{InitValue};", CustomAttrs); |
12705 | verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); |
12706 | verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); |
12707 | verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); |
12708 | verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); |
12709 | verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); |
12710 | verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); |
12711 | verifyGoogleFormat("SomeType* absl_nonnull s(InitValue);"); |
12712 | verifyGoogleFormat("SomeType* absl_nonnull s{InitValue};"); |
12713 | verifyGoogleFormat("SomeType* absl_nullable s(InitValue);"); |
12714 | verifyGoogleFormat("SomeType* absl_nullable s{InitValue};"); |
12715 | verifyGoogleFormat("SomeType* absl_nullability_unknown s(InitValue);"); |
12716 | verifyGoogleFormat("SomeType* absl_nullability_unknown s{InitValue};"); |
12717 | |
12718 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
12719 | Style.AttributeMacros.push_back(x: "my_fancy_attr"); |
12720 | Style.PointerAlignment = FormatStyle::PAS_Left; |
12721 | verifyFormat("void foo(const MyLongTypeNameeeeeeeeeeeee* my_fancy_attr\n" |
12722 | " testttttttttt);", |
12723 | Style); |
12724 | } |
12725 | |
12726 | TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { |
12727 | // Check that qualifiers on pointers don't break parsing of casts. |
12728 | verifyFormat("x = (foo *const)*v;"); |
12729 | verifyFormat("x = (foo *volatile)*v;"); |
12730 | verifyFormat("x = (foo *restrict)*v;"); |
12731 | verifyFormat("x = (foo *__attribute__((foo)))*v;"); |
12732 | verifyFormat("x = (foo *_Nonnull)*v;"); |
12733 | verifyFormat("x = (foo *_Nullable)*v;"); |
12734 | verifyFormat("x = (foo *_Null_unspecified)*v;"); |
12735 | verifyGoogleFormat("x = (foo* absl_nonnull)*v;"); |
12736 | verifyGoogleFormat("x = (foo* absl_nullable)*v;"); |
12737 | verifyGoogleFormat("x = (foo* absl_nullability_unknown)*v;"); |
12738 | verifyFormat("x = (foo *[[clang::attr]])*v;"); |
12739 | verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); |
12740 | verifyFormat("x = (foo *__ptr32)*v;"); |
12741 | verifyFormat("x = (foo *__ptr64)*v;"); |
12742 | verifyFormat("x = (foo *__capability)*v;"); |
12743 | |
12744 | // Check that we handle multiple trailing qualifiers and skip them all to |
12745 | // determine that the expression is a cast to a pointer type. |
12746 | FormatStyle LongPointerRight = getLLVMStyleWithColumns(ColumnLimit: 999); |
12747 | FormatStyle LongPointerLeft = getLLVMStyleWithColumns(ColumnLimit: 999); |
12748 | LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; |
12749 | StringRef AllQualifiers = |
12750 | "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " |
12751 | "_Nullable [[clang::attr]] __ptr32 __ptr64 __capability"; |
12752 | verifyFormat(("x = (foo *"+ AllQualifiers + ")*v;").str(), LongPointerRight); |
12753 | verifyFormat(("x = (foo* "+ AllQualifiers + ")*v;").str(), LongPointerLeft); |
12754 | |
12755 | // Also check that address-of is not parsed as a binary bitwise-and: |
12756 | verifyFormat("x = (foo *const)&v;"); |
12757 | verifyFormat(("x = (foo *"+ AllQualifiers + ")&v;").str(), LongPointerRight); |
12758 | verifyFormat(("x = (foo* "+ AllQualifiers + ")&v;").str(), LongPointerLeft); |
12759 | |
12760 | // Check custom qualifiers: |
12761 | FormatStyle CustomQualifier = getLLVMStyleWithColumns(ColumnLimit: 999); |
12762 | CustomQualifier.AttributeMacros.push_back(x: "__my_qualifier"); |
12763 | verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. |
12764 | verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); |
12765 | verifyFormat(("x = (foo *"+ AllQualifiers + " __my_qualifier)*v;").str(), |
12766 | CustomQualifier); |
12767 | verifyFormat(("x = (foo *"+ AllQualifiers + " __my_qualifier)&v;").str(), |
12768 | CustomQualifier); |
12769 | |
12770 | // Check that unknown identifiers result in binary operator parsing: |
12771 | verifyFormat("x = (foo * __unknown_qualifier) * v;"); |
12772 | verifyFormat("x = (foo * __unknown_qualifier) & v;"); |
12773 | } |
12774 | |
12775 | TEST_F(FormatTest, UnderstandsSquareAttributes) { |
12776 | verifyFormat("SomeType s [[unused]] (InitValue);"); |
12777 | verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); |
12778 | verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); |
12779 | verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); |
12780 | verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;"); |
12781 | verifyFormat("void f() [[deprecated(\"so sorry\")]];"); |
12782 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
12783 | " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); |
12784 | verifyFormat("[[nodiscard]] bool f() { return false; }"); |
12785 | verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); |
12786 | verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); |
12787 | verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); |
12788 | verifyFormat("[[nodiscard]] ::qualified_type f();"); |
12789 | |
12790 | // Make sure we do not mistake attributes for array subscripts. |
12791 | verifyFormat("int a() {}\n" |
12792 | "[[unused]] int b() {}"); |
12793 | verifyFormat("NSArray *arr;\n" |
12794 | "arr[[Foo() bar]];"); |
12795 | |
12796 | // On the other hand, we still need to correctly find array subscripts. |
12797 | verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); |
12798 | |
12799 | // Make sure that we do not mistake Objective-C method inside array literals |
12800 | // as attributes, even if those method names are also keywords. |
12801 | verifyFormat("@[ [foo bar] ];"); |
12802 | verifyFormat("@[ [NSArray class] ];"); |
12803 | verifyFormat("@[ [foo enum] ];"); |
12804 | |
12805 | verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); |
12806 | |
12807 | // Make sure we do not parse attributes as lambda introducers. |
12808 | FormatStyle MultiLineFunctions = getLLVMStyle(); |
12809 | MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
12810 | verifyFormat("[[unused]] int b() {\n" |
12811 | " return 42;\n" |
12812 | "}", |
12813 | MultiLineFunctions); |
12814 | } |
12815 | |
12816 | TEST_F(FormatTest, AttributeClass) { |
12817 | FormatStyle Style = getChromiumStyle(Language: FormatStyle::LK_Cpp); |
12818 | verifyFormat("class S {\n" |
12819 | " S(S&&) = default;\n" |
12820 | "};", |
12821 | Style); |
12822 | verifyFormat("class [[nodiscard]] S {\n" |
12823 | " S(S&&) = default;\n" |
12824 | "};", |
12825 | Style); |
12826 | verifyFormat("class __attribute((maybeunused)) S {\n" |
12827 | " S(S&&) = default;\n" |
12828 | "};", |
12829 | Style); |
12830 | verifyFormat("struct S {\n" |
12831 | " S(S&&) = default;\n" |
12832 | "};", |
12833 | Style); |
12834 | verifyFormat("struct [[nodiscard]] S {\n" |
12835 | " S(S&&) = default;\n" |
12836 | "};", |
12837 | Style); |
12838 | } |
12839 | |
12840 | TEST_F(FormatTest, AttributesAfterMacro) { |
12841 | FormatStyle Style = getLLVMStyle(); |
12842 | verifyFormat("MACRO;\n" |
12843 | "__attribute__((maybe_unused)) int foo() {\n" |
12844 | " //...\n" |
12845 | "}"); |
12846 | |
12847 | verifyFormat("MACRO;\n" |
12848 | "[[nodiscard]] int foo() {\n" |
12849 | " //...\n" |
12850 | "}"); |
12851 | |
12852 | verifyNoChange("MACRO\n\n" |
12853 | "__attribute__((maybe_unused)) int foo() {\n" |
12854 | " //...\n" |
12855 | "}"); |
12856 | |
12857 | verifyNoChange("MACRO\n\n" |
12858 | "[[nodiscard]] int foo() {\n" |
12859 | " //...\n" |
12860 | "}"); |
12861 | } |
12862 | |
12863 | TEST_F(FormatTest, AttributePenaltyBreaking) { |
12864 | FormatStyle Style = getLLVMStyle(); |
12865 | verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" |
12866 | " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", |
12867 | Style); |
12868 | verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" |
12869 | " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", |
12870 | Style); |
12871 | verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " |
12872 | "shared_ptr<ALongTypeName> &C d) {\n}", |
12873 | Style); |
12874 | } |
12875 | |
12876 | TEST_F(FormatTest, UnderstandsEllipsis) { |
12877 | FormatStyle Style = getLLVMStyle(); |
12878 | verifyFormat("int printf(const char *fmt, ...);"); |
12879 | verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); |
12880 | verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); |
12881 | |
12882 | verifyFormat("template <int *...PP> a;", Style); |
12883 | |
12884 | Style.PointerAlignment = FormatStyle::PAS_Left; |
12885 | verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); |
12886 | |
12887 | verifyFormat("template <int*... PP> a;", Style); |
12888 | |
12889 | Style.PointerAlignment = FormatStyle::PAS_Middle; |
12890 | verifyFormat("template <int *... PP> a;", Style); |
12891 | } |
12892 | |
12893 | TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { |
12894 | verifyFormat("int *a;\n" |
12895 | "int *a;\n" |
12896 | "int *a;", |
12897 | "int *a;\n" |
12898 | "int* a;\n" |
12899 | "int *a;", |
12900 | getGoogleStyle()); |
12901 | verifyFormat("int* a;\n" |
12902 | "int* a;\n" |
12903 | "int* a;", |
12904 | "int* a;\n" |
12905 | "int* a;\n" |
12906 | "int *a;", |
12907 | getGoogleStyle()); |
12908 | verifyFormat("int *a;\n" |
12909 | "int *a;\n" |
12910 | "int *a;", |
12911 | "int *a;\n" |
12912 | "int * a;\n" |
12913 | "int * a;", |
12914 | getGoogleStyle()); |
12915 | verifyFormat("auto x = [] {\n" |
12916 | " int *a;\n" |
12917 | " int *a;\n" |
12918 | " int *a;\n" |
12919 | "};", |
12920 | "auto x=[]{int *a;\n" |
12921 | "int * a;\n" |
12922 | "int * a;};", |
12923 | getGoogleStyle()); |
12924 | } |
12925 | |
12926 | TEST_F(FormatTest, UnderstandsRvalueReferences) { |
12927 | verifyFormat("int f(int &&a) {}"); |
12928 | verifyFormat("int f(int a, char &&b) {}"); |
12929 | verifyFormat("void f() { int &&a = b; }"); |
12930 | verifyGoogleFormat("int f(int a, char&& b) {}"); |
12931 | verifyGoogleFormat("void f() { int&& a = b; }"); |
12932 | |
12933 | verifyIndependentOfContext("A<int &&> a;"); |
12934 | verifyIndependentOfContext("A<int &&, int &&> a;"); |
12935 | verifyGoogleFormat("A<int&&> a;"); |
12936 | verifyGoogleFormat("A<int&&, int&&> a;"); |
12937 | |
12938 | // Not rvalue references: |
12939 | verifyFormat("template <bool B, bool C> class A {\n" |
12940 | " static_assert(B && C, \"Something is wrong\");\n" |
12941 | "};"); |
12942 | verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);"); |
12943 | verifyFormat("template <typename T> struct S {\n" |
12944 | " explicit(Bar<T> && Foo<T>) S(const S &);\n" |
12945 | "};"); |
12946 | verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); |
12947 | verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); |
12948 | verifyFormat("#define A(a, b) (a && b)"); |
12949 | } |
12950 | |
12951 | TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { |
12952 | verifyFormat("void f() {\n" |
12953 | " x[aaaaaaaaa -\n" |
12954 | " b] = 23;\n" |
12955 | "}", |
12956 | getLLVMStyleWithColumns(15)); |
12957 | } |
12958 | |
12959 | TEST_F(FormatTest, FormatsCasts) { |
12960 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
12961 | verifyFormat("static_cast<Type *>(P);"); |
12962 | verifyFormat("static_cast<Type &>(Fun)(Args);"); |
12963 | verifyFormat("static_cast<Type &>(*Fun)(Args);"); |
12964 | verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;"); |
12965 | // Check that static_cast<...>(...) does not require the next token to be on |
12966 | // the same line. |
12967 | verifyFormat("some_loooong_output << something_something__ << " |
12968 | "static_cast<const void *>(R)\n" |
12969 | " << something;"); |
12970 | verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); |
12971 | verifyFormat("const_cast<Type &>(*Fun)(Args);"); |
12972 | verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); |
12973 | verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); |
12974 | verifyFormat("Type *A = (Type *)P;"); |
12975 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
12976 | verifyFormat("int a = (int)(2.0f);"); |
12977 | verifyFormat("int a = (int)2.0f;"); |
12978 | verifyFormat("x[(int32)y];"); |
12979 | verifyFormat("x = (int32)y;"); |
12980 | verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); |
12981 | verifyFormat("int a = (int)*b;"); |
12982 | verifyFormat("int a = (int)2.0f;"); |
12983 | verifyFormat("int a = (int)~0;"); |
12984 | verifyFormat("int a = (int)++a;"); |
12985 | verifyFormat("int a = (int)sizeof(int);"); |
12986 | verifyFormat("int a = (int)+2;"); |
12987 | verifyFormat("my_int a = (my_int)2.0f;"); |
12988 | verifyFormat("my_int a = (my_int)sizeof(int);"); |
12989 | verifyFormat("return (my_int)aaa;"); |
12990 | verifyFormat("throw (my_int)aaa;"); |
12991 | verifyFormat("#define x ((int)-1)"); |
12992 | verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); |
12993 | verifyFormat("#define p(q) ((int *)&q)"); |
12994 | verifyFormat("fn(a)(b) + 1;"); |
12995 | |
12996 | verifyFormat("void f() { my_int a = (my_int)*b; }"); |
12997 | verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); |
12998 | verifyFormat("my_int a = (my_int)~0;"); |
12999 | verifyFormat("my_int a = (my_int)++a;"); |
13000 | verifyFormat("my_int a = (my_int)-2;"); |
13001 | verifyFormat("my_int a = (my_int)1;"); |
13002 | verifyFormat("my_int a = (my_int *)1;"); |
13003 | verifyFormat("my_int a = (const my_int)-1;"); |
13004 | verifyFormat("my_int a = (const my_int *)-1;"); |
13005 | verifyFormat("my_int a = (my_int)(my_int)-1;"); |
13006 | verifyFormat("my_int a = (ns::my_int)-2;"); |
13007 | verifyFormat("case (my_int)ONE:"); |
13008 | verifyFormat("auto x = (X)this;"); |
13009 | // Casts in Obj-C style calls used to not be recognized as such. |
13010 | verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];"); |
13011 | |
13012 | // FIXME: single value wrapped with paren will be treated as cast. |
13013 | verifyFormat("void f(int i = (kValue)*kMask) {}"); |
13014 | |
13015 | verifyFormat("{\n" |
13016 | " (void)F;\n" |
13017 | "}"); |
13018 | |
13019 | // Don't break after a cast's |
13020 | verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
13021 | " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" |
13022 | " bbbbbbbbbbbbbbbbbbbbbb);"); |
13023 | |
13024 | verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); |
13025 | verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); |
13026 | verifyFormat("#define CONF_BOOL(x) (bool)(x)"); |
13027 | verifyFormat("bool *y = (bool *)(void *)(x);"); |
13028 | verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); |
13029 | verifyFormat("bool *y = (bool *)(void *)(int)(x);"); |
13030 | verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); |
13031 | verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); |
13032 | |
13033 | // These are not casts. |
13034 | verifyFormat("void f(int *) {}"); |
13035 | verifyFormat("f(foo)->b;"); |
13036 | verifyFormat("f(foo).b;"); |
13037 | verifyFormat("f(foo)(b);"); |
13038 | verifyFormat("f(foo)[b];"); |
13039 | verifyFormat("[](foo) { return 4; }(bar);"); |
13040 | verifyFormat("(*funptr)(foo)[4];"); |
13041 | verifyFormat("funptrs[4](foo)[4];"); |
13042 | verifyFormat("void f(int *);"); |
13043 | verifyFormat("void f(int *) = 0;"); |
13044 | verifyFormat("void f(SmallVector<int>) {}"); |
13045 | verifyFormat("void f(SmallVector<int>);"); |
13046 | verifyFormat("void f(SmallVector<int>) = 0;"); |
13047 | verifyFormat("void f(int i = (kA * kB) & kMask) {}"); |
13048 | verifyFormat("int a = sizeof(int) * b;"); |
13049 | verifyGoogleFormat("int a = alignof(int) * b;"); |
13050 | verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); |
13051 | verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); |
13052 | verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); |
13053 | |
13054 | // These are not casts, but at some point were confused with casts. |
13055 | verifyFormat("virtual void foo(int *) override;"); |
13056 | verifyFormat("virtual void foo(char &) const;"); |
13057 | verifyFormat("virtual void foo(int *a, char *) const;"); |
13058 | verifyFormat("int a = sizeof(int *) + b;"); |
13059 | verifyGoogleFormat("int a = alignof(int *) + b;"); |
13060 | verifyFormat("bool b = f(g<int>) && c;"); |
13061 | verifyFormat("typedef void (*f)(int i) func;"); |
13062 | verifyFormat("void operator++(int) noexcept;"); |
13063 | verifyFormat("void operator++(int &) noexcept;"); |
13064 | verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " |
13065 | "&) noexcept;"); |
13066 | verifyFormat( |
13067 | "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); |
13068 | verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); |
13069 | verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); |
13070 | verifyFormat("void operator delete(nothrow_t &) noexcept;"); |
13071 | verifyFormat("void operator delete(foo &) noexcept;"); |
13072 | verifyFormat("void operator delete(foo) noexcept;"); |
13073 | verifyFormat("void operator delete(int) noexcept;"); |
13074 | verifyFormat("void operator delete(int &) noexcept;"); |
13075 | verifyFormat("void operator delete(int &) volatile noexcept;"); |
13076 | verifyFormat("void operator delete(int &) const"); |
13077 | verifyFormat("void operator delete(int &) = default"); |
13078 | verifyFormat("void operator delete(int &) = delete"); |
13079 | verifyFormat("void operator delete(int &) [[noreturn]]"); |
13080 | verifyFormat("void operator delete(int &) throw();"); |
13081 | verifyFormat("void operator delete(int &) throw(int);"); |
13082 | verifyFormat("auto operator delete(int &) -> int;"); |
13083 | verifyFormat("auto operator delete(int &) override"); |
13084 | verifyFormat("auto operator delete(int &) final"); |
13085 | |
13086 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" |
13087 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
13088 | // FIXME: The indentation here is not ideal. |
13089 | verifyFormat( |
13090 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
13091 | " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" |
13092 | " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); |
13093 | } |
13094 | |
13095 | TEST_F(FormatTest, FormatsFunctionTypes) { |
13096 | verifyFormat("A<bool()> a;"); |
13097 | verifyFormat("A<SomeType()> a;"); |
13098 | verifyFormat("A<void (*)(int, std::string)> a;"); |
13099 | verifyFormat("A<void *(int)>;"); |
13100 | verifyFormat("void *(*a)(int *, SomeType *);"); |
13101 | verifyFormat("int (*func)(void *);"); |
13102 | verifyFormat("void f() { int (*func)(void *); }"); |
13103 | verifyFormat("template <class CallbackClass>\n" |
13104 | "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); |
13105 | |
13106 | verifyGoogleFormat("A<void*(int*, SomeType*)>;"); |
13107 | verifyGoogleFormat("void* (*a)(int);"); |
13108 | verifyGoogleFormat( |
13109 | "template <class CallbackClass>\n" |
13110 | "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); |
13111 | |
13112 | // Other constructs can look somewhat like function types: |
13113 | verifyFormat("A<sizeof(*x)> a;"); |
13114 | verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); |
13115 | verifyFormat("some_var = function(*some_pointer_var)[0];"); |
13116 | verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); |
13117 | verifyFormat("int x = f(&h)();"); |
13118 | verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); |
13119 | verifyFormat("std::function<\n" |
13120 | " LooooooooooongTemplatedType<\n" |
13121 | " SomeType>*(\n" |
13122 | " LooooooooooooooooongType type)>\n" |
13123 | " function;", |
13124 | getGoogleStyleWithColumns(40)); |
13125 | } |
13126 | |
13127 | TEST_F(FormatTest, FormatsPointersToArrayTypes) { |
13128 | verifyFormat("A (*foo_)[6];"); |
13129 | verifyFormat("vector<int> (*foo_)[6];"); |
13130 | } |
13131 | |
13132 | TEST_F(FormatTest, BreaksLongVariableDeclarations) { |
13133 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" |
13134 | " LoooooooooooooooooooooooooooooooooooooooongVariable;"); |
13135 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" |
13136 | " LoooooooooooooooooooooooooooooooooooooooongVariable;"); |
13137 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" |
13138 | " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); |
13139 | |
13140 | // Different ways of ()-initializiation. |
13141 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" |
13142 | " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); |
13143 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" |
13144 | " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); |
13145 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" |
13146 | " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); |
13147 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" |
13148 | " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); |
13149 | |
13150 | // Lambdas should not confuse the variable declaration heuristic. |
13151 | verifyFormat("LooooooooooooooooongType\n" |
13152 | " variable(nullptr, [](A *a) {});", |
13153 | getLLVMStyleWithColumns(40)); |
13154 | } |
13155 | |
13156 | TEST_F(FormatTest, BreaksLongDeclarations) { |
13157 | verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" |
13158 | " AnotherNameForTheLongType;"); |
13159 | verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" |
13160 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
13161 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" |
13162 | "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); |
13163 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" |
13164 | "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); |
13165 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" |
13166 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13167 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" |
13168 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13169 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" |
13170 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13171 | verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" |
13172 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13173 | verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" |
13174 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13175 | verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" |
13176 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13177 | verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" |
13178 | "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); |
13179 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" |
13180 | "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); |
13181 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" |
13182 | "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); |
13183 | FormatStyle Indented = getLLVMStyle(); |
13184 | Indented.IndentWrappedFunctionNames = true; |
13185 | verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" |
13186 | " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", |
13187 | Indented); |
13188 | verifyFormat( |
13189 | "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" |
13190 | " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", |
13191 | Indented); |
13192 | verifyFormat( |
13193 | "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" |
13194 | " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", |
13195 | Indented); |
13196 | verifyFormat( |
13197 | "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" |
13198 | " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", |
13199 | Indented); |
13200 | |
13201 | // FIXME: Without the comment, this breaks after "(". |
13202 | verifyGoogleFormat( |
13203 | "LoooooooooooooooooooooooooooooooooooooooongType // break\n" |
13204 | " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); |
13205 | |
13206 | verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" |
13207 | " int LoooooooooooooooooooongParam2) {}"); |
13208 | verifyFormat( |
13209 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
13210 | " SourceLocation L, IdentifierIn *II,\n" |
13211 | " Type *T) {}"); |
13212 | verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" |
13213 | "ReallyReaaallyLongFunctionName(\n" |
13214 | " const std::string &SomeParameter,\n" |
13215 | " const SomeType<string, SomeOtherTemplateParameter>\n" |
13216 | " &ReallyReallyLongParameterName,\n" |
13217 | " const SomeType<string, SomeOtherTemplateParameter>\n" |
13218 | " &AnotherLongParameterName) {}"); |
13219 | verifyFormat("template <typename A>\n" |
13220 | "SomeLoooooooooooooooooooooongType<\n" |
13221 | " typename some_namespace::SomeOtherType<A>::Type>\n" |
13222 | "Function() {}"); |
13223 | |
13224 | verifyGoogleFormat( |
13225 | "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" |
13226 | " aaaaaaaaaaaaaaaaaaaaaaa;"); |
13227 | verifyGoogleFormat( |
13228 | "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" |
13229 | " SourceLocation L) {}"); |
13230 | verifyGoogleFormat( |
13231 | "some_namespace::LongReturnType\n" |
13232 | "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" |
13233 | " int first_long_parameter, int second_parameter) {}"); |
13234 | |
13235 | verifyGoogleFormat("template <typename T>\n" |
13236 | "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" |
13237 | "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); |
13238 | verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
13239 | " int aaaaaaaaaaaaaaaaaaaaaaa);"); |
13240 | |
13241 | verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" |
13242 | " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
13243 | " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
13244 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
13245 | " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" |
13246 | " aaaaaaaaaaaaaaaaaaaaaaaa);"); |
13247 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
13248 | " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" |
13249 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" |
13250 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
13251 | |
13252 | verifyFormat("template <typename T> // Templates on own line.\n" |
13253 | "static int // Some comment.\n" |
13254 | "MyFunction(int a);"); |
13255 | } |
13256 | |
13257 | TEST_F(FormatTest, FormatsAccessModifiers) { |
13258 | FormatStyle Style = getLLVMStyle(); |
13259 | EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, |
13260 | FormatStyle::ELBAMS_LogicalBlock); |
13261 | verifyFormat("struct foo {\n" |
13262 | "private:\n" |
13263 | " void f() {}\n" |
13264 | "\n" |
13265 | "private:\n" |
13266 | " int i;\n" |
13267 | "\n" |
13268 | "protected:\n" |
13269 | " int j;\n" |
13270 | "};", |
13271 | Style); |
13272 | verifyFormat("struct foo {\n" |
13273 | "private:\n" |
13274 | " void f() {}\n" |
13275 | "\n" |
13276 | "private:\n" |
13277 | " int i;\n" |
13278 | "\n" |
13279 | "protected:\n" |
13280 | " int j;\n" |
13281 | "};", |
13282 | "struct foo {\n" |
13283 | "private:\n" |
13284 | " void f() {}\n" |
13285 | "private:\n" |
13286 | " int i;\n" |
13287 | "protected:\n" |
13288 | " int j;\n" |
13289 | "};", |
13290 | Style); |
13291 | verifyFormat("struct foo { /* comment */\n" |
13292 | "private:\n" |
13293 | " int i;\n" |
13294 | " // comment\n" |
13295 | "private:\n" |
13296 | " int j;\n" |
13297 | "};", |
13298 | Style); |
13299 | verifyFormat("struct foo {\n" |
13300 | "#ifdef FOO\n" |
13301 | "#endif\n" |
13302 | "private:\n" |
13303 | " int i;\n" |
13304 | "#ifdef FOO\n" |
13305 | "private:\n" |
13306 | "#endif\n" |
13307 | " int j;\n" |
13308 | "};", |
13309 | Style); |
13310 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; |
13311 | verifyFormat("struct foo {\n" |
13312 | "private:\n" |
13313 | " void f() {}\n" |
13314 | "private:\n" |
13315 | " int i;\n" |
13316 | "protected:\n" |
13317 | " int j;\n" |
13318 | "};", |
13319 | Style); |
13320 | verifyFormat("struct foo {\n" |
13321 | "private:\n" |
13322 | " void f() {}\n" |
13323 | "private:\n" |
13324 | " int i;\n" |
13325 | "protected:\n" |
13326 | " int j;\n" |
13327 | "};", |
13328 | "struct foo {\n" |
13329 | "\n" |
13330 | "private:\n" |
13331 | " void f() {}\n" |
13332 | "\n" |
13333 | "private:\n" |
13334 | " int i;\n" |
13335 | "\n" |
13336 | "protected:\n" |
13337 | " int j;\n" |
13338 | "};", |
13339 | Style); |
13340 | verifyFormat("struct foo { /* comment */\n" |
13341 | "private:\n" |
13342 | " int i;\n" |
13343 | " // comment\n" |
13344 | "private:\n" |
13345 | " int j;\n" |
13346 | "};", |
13347 | "struct foo { /* comment */\n" |
13348 | "\n" |
13349 | "private:\n" |
13350 | " int i;\n" |
13351 | " // comment\n" |
13352 | "\n" |
13353 | "private:\n" |
13354 | " int j;\n" |
13355 | "};", |
13356 | Style); |
13357 | verifyFormat("struct foo {\n" |
13358 | "#ifdef FOO\n" |
13359 | "#endif\n" |
13360 | "private:\n" |
13361 | " int i;\n" |
13362 | "#ifdef FOO\n" |
13363 | "private:\n" |
13364 | "#endif\n" |
13365 | " int j;\n" |
13366 | "};", |
13367 | "struct foo {\n" |
13368 | "#ifdef FOO\n" |
13369 | "#endif\n" |
13370 | "\n" |
13371 | "private:\n" |
13372 | " int i;\n" |
13373 | "#ifdef FOO\n" |
13374 | "\n" |
13375 | "private:\n" |
13376 | "#endif\n" |
13377 | " int j;\n" |
13378 | "};", |
13379 | Style); |
13380 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; |
13381 | verifyFormat("struct foo {\n" |
13382 | "private:\n" |
13383 | " void f() {}\n" |
13384 | "\n" |
13385 | "private:\n" |
13386 | " int i;\n" |
13387 | "\n" |
13388 | "protected:\n" |
13389 | " int j;\n" |
13390 | "};", |
13391 | Style); |
13392 | verifyFormat("struct foo {\n" |
13393 | "private:\n" |
13394 | " void f() {}\n" |
13395 | "\n" |
13396 | "private:\n" |
13397 | " int i;\n" |
13398 | "\n" |
13399 | "protected:\n" |
13400 | " int j;\n" |
13401 | "};", |
13402 | "struct foo {\n" |
13403 | "private:\n" |
13404 | " void f() {}\n" |
13405 | "private:\n" |
13406 | " int i;\n" |
13407 | "protected:\n" |
13408 | " int j;\n" |
13409 | "};", |
13410 | Style); |
13411 | verifyFormat("struct foo { /* comment */\n" |
13412 | "private:\n" |
13413 | " int i;\n" |
13414 | " // comment\n" |
13415 | "\n" |
13416 | "private:\n" |
13417 | " int j;\n" |
13418 | "};", |
13419 | Style); |
13420 | verifyFormat("struct foo {\n" |
13421 | "#ifdef FOO\n" |
13422 | "#endif\n" |
13423 | "\n" |
13424 | "private:\n" |
13425 | " int i;\n" |
13426 | "#ifdef FOO\n" |
13427 | "\n" |
13428 | "private:\n" |
13429 | "#endif\n" |
13430 | " int j;\n" |
13431 | "};", |
13432 | "struct foo {\n" |
13433 | "#ifdef FOO\n" |
13434 | "#endif\n" |
13435 | "private:\n" |
13436 | " int i;\n" |
13437 | "#ifdef FOO\n" |
13438 | "private:\n" |
13439 | "#endif\n" |
13440 | " int j;\n" |
13441 | "};", |
13442 | Style); |
13443 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; |
13444 | verifyNoChange("struct foo {\n" |
13445 | "\n" |
13446 | "private:\n" |
13447 | " void f() {}\n" |
13448 | "\n" |
13449 | "private:\n" |
13450 | " int i;\n" |
13451 | "\n" |
13452 | "protected:\n" |
13453 | " int j;\n" |
13454 | "};", |
13455 | Style); |
13456 | verifyFormat("struct foo {\n" |
13457 | "private:\n" |
13458 | " void f() {}\n" |
13459 | "private:\n" |
13460 | " int i;\n" |
13461 | "protected:\n" |
13462 | " int j;\n" |
13463 | "};", |
13464 | Style); |
13465 | verifyNoChange("struct foo { /* comment */\n" |
13466 | "\n" |
13467 | "private:\n" |
13468 | " int i;\n" |
13469 | " // comment\n" |
13470 | "\n" |
13471 | "private:\n" |
13472 | " int j;\n" |
13473 | "};", |
13474 | Style); |
13475 | verifyFormat("struct foo { /* comment */\n" |
13476 | "private:\n" |
13477 | " int i;\n" |
13478 | " // comment\n" |
13479 | "private:\n" |
13480 | " int j;\n" |
13481 | "};", |
13482 | Style); |
13483 | verifyNoChange("struct foo {\n" |
13484 | "#ifdef FOO\n" |
13485 | "#endif\n" |
13486 | "\n" |
13487 | "private:\n" |
13488 | " int i;\n" |
13489 | "#ifdef FOO\n" |
13490 | "\n" |
13491 | "private:\n" |
13492 | "#endif\n" |
13493 | " int j;\n" |
13494 | "};", |
13495 | Style); |
13496 | verifyFormat("struct foo {\n" |
13497 | "#ifdef FOO\n" |
13498 | "#endif\n" |
13499 | "private:\n" |
13500 | " int i;\n" |
13501 | "#ifdef FOO\n" |
13502 | "private:\n" |
13503 | "#endif\n" |
13504 | " int j;\n" |
13505 | "};", |
13506 | Style); |
13507 | Style.AttributeMacros.push_back(x: "FOO"); |
13508 | Style.AttributeMacros.push_back(x: "BAR"); |
13509 | verifyFormat("struct foo {\n" |
13510 | "FOO private:\n" |
13511 | " int i;\n" |
13512 | "BAR(x) protected:\n" |
13513 | " int j;\n" |
13514 | "};", |
13515 | Style); |
13516 | |
13517 | FormatStyle NoEmptyLines = getLLVMStyle(); |
13518 | NoEmptyLines.MaxEmptyLinesToKeep = 0; |
13519 | verifyFormat("struct foo {\n" |
13520 | "private:\n" |
13521 | " void f() {}\n" |
13522 | "\n" |
13523 | "private:\n" |
13524 | " int i;\n" |
13525 | "\n" |
13526 | "public:\n" |
13527 | "protected:\n" |
13528 | " int j;\n" |
13529 | "};", |
13530 | NoEmptyLines); |
13531 | |
13532 | NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; |
13533 | verifyFormat("struct foo {\n" |
13534 | "private:\n" |
13535 | " void f() {}\n" |
13536 | "private:\n" |
13537 | " int i;\n" |
13538 | "public:\n" |
13539 | "protected:\n" |
13540 | " int j;\n" |
13541 | "};", |
13542 | NoEmptyLines); |
13543 | |
13544 | NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; |
13545 | verifyFormat("struct foo {\n" |
13546 | "private:\n" |
13547 | " void f() {}\n" |
13548 | "\n" |
13549 | "private:\n" |
13550 | " int i;\n" |
13551 | "\n" |
13552 | "public:\n" |
13553 | "\n" |
13554 | "protected:\n" |
13555 | " int j;\n" |
13556 | "};", |
13557 | NoEmptyLines); |
13558 | } |
13559 | |
13560 | TEST_F(FormatTest, FormatsAfterAccessModifiers) { |
13561 | |
13562 | FormatStyle Style = getLLVMStyle(); |
13563 | EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); |
13564 | verifyFormat("struct foo {\n" |
13565 | "private:\n" |
13566 | " void f() {}\n" |
13567 | "\n" |
13568 | "private:\n" |
13569 | " int i;\n" |
13570 | "\n" |
13571 | "protected:\n" |
13572 | " int j;\n" |
13573 | "};", |
13574 | Style); |
13575 | |
13576 | // Check if lines are removed. |
13577 | verifyFormat("struct foo {\n" |
13578 | "private:\n" |
13579 | " void f() {}\n" |
13580 | "\n" |
13581 | "private:\n" |
13582 | " int i;\n" |
13583 | "\n" |
13584 | "protected:\n" |
13585 | " int j;\n" |
13586 | "};", |
13587 | "struct foo {\n" |
13588 | "private:\n" |
13589 | "\n" |
13590 | " void f() {}\n" |
13591 | "\n" |
13592 | "private:\n" |
13593 | "\n" |
13594 | " int i;\n" |
13595 | "\n" |
13596 | "protected:\n" |
13597 | "\n" |
13598 | " int j;\n" |
13599 | "};", |
13600 | Style); |
13601 | |
13602 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
13603 | verifyFormat("struct foo {\n" |
13604 | "private:\n" |
13605 | "\n" |
13606 | " void f() {}\n" |
13607 | "\n" |
13608 | "private:\n" |
13609 | "\n" |
13610 | " int i;\n" |
13611 | "\n" |
13612 | "protected:\n" |
13613 | "\n" |
13614 | " int j;\n" |
13615 | "};", |
13616 | Style); |
13617 | |
13618 | // Check if lines are added. |
13619 | verifyFormat("struct foo {\n" |
13620 | "private:\n" |
13621 | "\n" |
13622 | " void f() {}\n" |
13623 | "\n" |
13624 | "private:\n" |
13625 | "\n" |
13626 | " int i;\n" |
13627 | "\n" |
13628 | "protected:\n" |
13629 | "\n" |
13630 | " int j;\n" |
13631 | "};", |
13632 | "struct foo {\n" |
13633 | "private:\n" |
13634 | " void f() {}\n" |
13635 | "\n" |
13636 | "private:\n" |
13637 | " int i;\n" |
13638 | "\n" |
13639 | "protected:\n" |
13640 | " int j;\n" |
13641 | "};", |
13642 | Style); |
13643 | |
13644 | // Leave tests rely on the code layout, test::messUp can not be used. |
13645 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; |
13646 | Style.MaxEmptyLinesToKeep = 0u; |
13647 | verifyFormat("struct foo {\n" |
13648 | "private:\n" |
13649 | " void f() {}\n" |
13650 | "\n" |
13651 | "private:\n" |
13652 | " int i;\n" |
13653 | "\n" |
13654 | "protected:\n" |
13655 | " int j;\n" |
13656 | "};", |
13657 | Style); |
13658 | |
13659 | // Check if MaxEmptyLinesToKeep is respected. |
13660 | verifyFormat("struct foo {\n" |
13661 | "private:\n" |
13662 | " void f() {}\n" |
13663 | "\n" |
13664 | "private:\n" |
13665 | " int i;\n" |
13666 | "\n" |
13667 | "protected:\n" |
13668 | " int j;\n" |
13669 | "};", |
13670 | "struct foo {\n" |
13671 | "private:\n" |
13672 | "\n\n\n" |
13673 | " void f() {}\n" |
13674 | "\n" |
13675 | "private:\n" |
13676 | "\n\n\n" |
13677 | " int i;\n" |
13678 | "\n" |
13679 | "protected:\n" |
13680 | "\n\n\n" |
13681 | " int j;\n" |
13682 | "};", |
13683 | Style); |
13684 | |
13685 | Style.MaxEmptyLinesToKeep = 1u; |
13686 | verifyNoChange("struct foo {\n" |
13687 | "private:\n" |
13688 | "\n" |
13689 | " void f() {}\n" |
13690 | "\n" |
13691 | "private:\n" |
13692 | "\n" |
13693 | " int i;\n" |
13694 | "\n" |
13695 | "protected:\n" |
13696 | "\n" |
13697 | " int j;\n" |
13698 | "};", |
13699 | Style); |
13700 | // Check if no lines are kept. |
13701 | verifyFormat("struct foo {\n" |
13702 | "private:\n" |
13703 | " void f() {}\n" |
13704 | "\n" |
13705 | "private:\n" |
13706 | " int i;\n" |
13707 | "\n" |
13708 | "protected:\n" |
13709 | " int j;\n" |
13710 | "};", |
13711 | Style); |
13712 | // Check if MaxEmptyLinesToKeep is respected. |
13713 | verifyFormat("struct foo {\n" |
13714 | "private:\n" |
13715 | "\n" |
13716 | " void f() {}\n" |
13717 | "\n" |
13718 | "private:\n" |
13719 | "\n" |
13720 | " int i;\n" |
13721 | "\n" |
13722 | "protected:\n" |
13723 | "\n" |
13724 | " int j;\n" |
13725 | "};", |
13726 | "struct foo {\n" |
13727 | "private:\n" |
13728 | "\n\n\n" |
13729 | " void f() {}\n" |
13730 | "\n" |
13731 | "private:\n" |
13732 | "\n\n\n" |
13733 | " int i;\n" |
13734 | "\n" |
13735 | "protected:\n" |
13736 | "\n\n\n" |
13737 | " int j;\n" |
13738 | "};", |
13739 | Style); |
13740 | |
13741 | Style.MaxEmptyLinesToKeep = 10u; |
13742 | verifyNoChange("struct foo {\n" |
13743 | "private:\n" |
13744 | "\n\n\n" |
13745 | " void f() {}\n" |
13746 | "\n" |
13747 | "private:\n" |
13748 | "\n\n\n" |
13749 | " int i;\n" |
13750 | "\n" |
13751 | "protected:\n" |
13752 | "\n\n\n" |
13753 | " int j;\n" |
13754 | "};", |
13755 | Style); |
13756 | |
13757 | // Test with comments. |
13758 | Style = getLLVMStyle(); |
13759 | verifyFormat("struct foo {\n" |
13760 | "private:\n" |
13761 | " // comment\n" |
13762 | " void f() {}\n" |
13763 | "\n" |
13764 | "private: /* comment */\n" |
13765 | " int i;\n" |
13766 | "};", |
13767 | Style); |
13768 | verifyFormat("struct foo {\n" |
13769 | "private:\n" |
13770 | " // comment\n" |
13771 | " void f() {}\n" |
13772 | "\n" |
13773 | "private: /* comment */\n" |
13774 | " int i;\n" |
13775 | "};", |
13776 | "struct foo {\n" |
13777 | "private:\n" |
13778 | "\n" |
13779 | " // comment\n" |
13780 | " void f() {}\n" |
13781 | "\n" |
13782 | "private: /* comment */\n" |
13783 | "\n" |
13784 | " int i;\n" |
13785 | "};", |
13786 | Style); |
13787 | |
13788 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
13789 | verifyFormat("struct foo {\n" |
13790 | "private:\n" |
13791 | "\n" |
13792 | " // comment\n" |
13793 | " void f() {}\n" |
13794 | "\n" |
13795 | "private: /* comment */\n" |
13796 | "\n" |
13797 | " int i;\n" |
13798 | "};", |
13799 | "struct foo {\n" |
13800 | "private:\n" |
13801 | " // comment\n" |
13802 | " void f() {}\n" |
13803 | "\n" |
13804 | "private: /* comment */\n" |
13805 | " int i;\n" |
13806 | "};", |
13807 | Style); |
13808 | verifyFormat("struct foo {\n" |
13809 | "private:\n" |
13810 | "\n" |
13811 | " // comment\n" |
13812 | " void f() {}\n" |
13813 | "\n" |
13814 | "private: /* comment */\n" |
13815 | "\n" |
13816 | " int i;\n" |
13817 | "};", |
13818 | Style); |
13819 | |
13820 | // Test with preprocessor defines. |
13821 | Style = getLLVMStyle(); |
13822 | verifyFormat("struct foo {\n" |
13823 | "private:\n" |
13824 | "#ifdef FOO\n" |
13825 | "#endif\n" |
13826 | " void f() {}\n" |
13827 | "};", |
13828 | Style); |
13829 | verifyFormat("struct foo {\n" |
13830 | "private:\n" |
13831 | "#ifdef FOO\n" |
13832 | "#endif\n" |
13833 | " void f() {}\n" |
13834 | "};", |
13835 | "struct foo {\n" |
13836 | "private:\n" |
13837 | "\n" |
13838 | "#ifdef FOO\n" |
13839 | "#endif\n" |
13840 | " void f() {}\n" |
13841 | "};", |
13842 | Style); |
13843 | verifyNoChange("struct foo {\n" |
13844 | "#ifdef FOO\n" |
13845 | "#else\n" |
13846 | "private:\n" |
13847 | "\n" |
13848 | "#endif\n" |
13849 | "};", |
13850 | Style); |
13851 | verifyFormat("struct foo {\n" |
13852 | "#ifdef FOO\n" |
13853 | "#else\n" |
13854 | "private:\n" |
13855 | "\n" |
13856 | "#endif\n" |
13857 | "};", |
13858 | "struct foo {\n" |
13859 | "#ifdef FOO\n" |
13860 | "#else\n" |
13861 | "private:\n" |
13862 | "\n" |
13863 | "\n" |
13864 | "#endif\n" |
13865 | "};", |
13866 | Style); |
13867 | verifyFormat("struct foo {\n" |
13868 | "#ifdef FOO\n" |
13869 | "private:\n" |
13870 | "#else\n" |
13871 | "#endif\n" |
13872 | "};", |
13873 | "struct foo {\n" |
13874 | "#ifdef FOO\n" |
13875 | "private:\n" |
13876 | "\n" |
13877 | "\n" |
13878 | "#else\n" |
13879 | "#endif\n" |
13880 | "};", |
13881 | Style); |
13882 | verifyFormat("struct foo {\n" |
13883 | "#if 0\n" |
13884 | "#else\n" |
13885 | "#endif\n" |
13886 | "#ifdef FOO\n" |
13887 | "private:\n" |
13888 | "#endif\n" |
13889 | "};", |
13890 | "struct foo {\n" |
13891 | "#if 0\n" |
13892 | "#else\n" |
13893 | "#endif\n" |
13894 | "#ifdef FOO\n" |
13895 | "private:\n" |
13896 | "\n" |
13897 | "\n" |
13898 | "#endif\n" |
13899 | "};", |
13900 | Style); |
13901 | |
13902 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
13903 | verifyFormat("struct foo {\n" |
13904 | "private:\n" |
13905 | "\n" |
13906 | "#ifdef FOO\n" |
13907 | "#endif\n" |
13908 | " void f() {}\n" |
13909 | "};", |
13910 | "struct foo {\n" |
13911 | "private:\n" |
13912 | "#ifdef FOO\n" |
13913 | "#endif\n" |
13914 | " void f() {}\n" |
13915 | "};", |
13916 | Style); |
13917 | verifyFormat("struct foo {\n" |
13918 | "private:\n" |
13919 | "\n" |
13920 | "#ifdef FOO\n" |
13921 | "#endif\n" |
13922 | " void f() {}\n" |
13923 | "};", |
13924 | Style); |
13925 | } |
13926 | |
13927 | TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { |
13928 | // Combined tests of EmptyLineAfterAccessModifier and |
13929 | // EmptyLineBeforeAccessModifier. |
13930 | FormatStyle Style = getLLVMStyle(); |
13931 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; |
13932 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
13933 | verifyFormat("struct foo {\n" |
13934 | "private:\n" |
13935 | "\n" |
13936 | "protected:\n" |
13937 | "};", |
13938 | Style); |
13939 | |
13940 | Style.MaxEmptyLinesToKeep = 10u; |
13941 | // Both remove all new lines. |
13942 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; |
13943 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; |
13944 | verifyFormat("struct foo {\n" |
13945 | "private:\n" |
13946 | "protected:\n" |
13947 | "};", |
13948 | "struct foo {\n" |
13949 | "private:\n" |
13950 | "\n\n\n" |
13951 | "protected:\n" |
13952 | "};", |
13953 | Style); |
13954 | |
13955 | // Leave tests rely on the code layout, test::messUp can not be used. |
13956 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; |
13957 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; |
13958 | Style.MaxEmptyLinesToKeep = 10u; |
13959 | verifyNoChange("struct foo {\n" |
13960 | "private:\n" |
13961 | "\n\n\n" |
13962 | "protected:\n" |
13963 | "};", |
13964 | Style); |
13965 | Style.MaxEmptyLinesToKeep = 3u; |
13966 | verifyNoChange("struct foo {\n" |
13967 | "private:\n" |
13968 | "\n\n\n" |
13969 | "protected:\n" |
13970 | "};", |
13971 | Style); |
13972 | Style.MaxEmptyLinesToKeep = 1u; |
13973 | verifyNoChange("struct foo {\n" |
13974 | "private:\n" |
13975 | "\n\n\n" |
13976 | "protected:\n" |
13977 | "};", |
13978 | Style); // Based on new lines in original document and not |
13979 | // on the setting. |
13980 | |
13981 | Style.MaxEmptyLinesToKeep = 10u; |
13982 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; |
13983 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; |
13984 | // Newlines are kept if they are greater than zero, |
13985 | // test::messUp removes all new lines which changes the logic |
13986 | verifyNoChange("struct foo {\n" |
13987 | "private:\n" |
13988 | "\n\n\n" |
13989 | "protected:\n" |
13990 | "};", |
13991 | Style); |
13992 | |
13993 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; |
13994 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
13995 | // test::messUp removes all new lines which changes the logic |
13996 | verifyNoChange("struct foo {\n" |
13997 | "private:\n" |
13998 | "\n\n\n" |
13999 | "protected:\n" |
14000 | "};", |
14001 | Style); |
14002 | |
14003 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; |
14004 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; |
14005 | verifyNoChange("struct foo {\n" |
14006 | "private:\n" |
14007 | "\n\n\n" |
14008 | "protected:\n" |
14009 | "};", |
14010 | Style); // test::messUp removes all new lines which changes |
14011 | // the logic. |
14012 | |
14013 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; |
14014 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; |
14015 | verifyFormat("struct foo {\n" |
14016 | "private:\n" |
14017 | "protected:\n" |
14018 | "};", |
14019 | "struct foo {\n" |
14020 | "private:\n" |
14021 | "\n\n\n" |
14022 | "protected:\n" |
14023 | "};", |
14024 | Style); |
14025 | |
14026 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; |
14027 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; |
14028 | verifyNoChange("struct foo {\n" |
14029 | "private:\n" |
14030 | "\n\n\n" |
14031 | "protected:\n" |
14032 | "};", |
14033 | Style); // test::messUp removes all new lines which changes |
14034 | // the logic. |
14035 | |
14036 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; |
14037 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
14038 | verifyFormat("struct foo {\n" |
14039 | "private:\n" |
14040 | "protected:\n" |
14041 | "};", |
14042 | "struct foo {\n" |
14043 | "private:\n" |
14044 | "\n\n\n" |
14045 | "protected:\n" |
14046 | "};", |
14047 | Style); |
14048 | |
14049 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; |
14050 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; |
14051 | verifyFormat("struct foo {\n" |
14052 | "private:\n" |
14053 | "protected:\n" |
14054 | "};", |
14055 | "struct foo {\n" |
14056 | "private:\n" |
14057 | "\n\n\n" |
14058 | "protected:\n" |
14059 | "};", |
14060 | Style); |
14061 | |
14062 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; |
14063 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; |
14064 | verifyFormat("struct foo {\n" |
14065 | "private:\n" |
14066 | "protected:\n" |
14067 | "};", |
14068 | "struct foo {\n" |
14069 | "private:\n" |
14070 | "\n\n\n" |
14071 | "protected:\n" |
14072 | "};", |
14073 | Style); |
14074 | |
14075 | Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; |
14076 | Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; |
14077 | verifyFormat("struct foo {\n" |
14078 | "private:\n" |
14079 | "protected:\n" |
14080 | "};", |
14081 | "struct foo {\n" |
14082 | "private:\n" |
14083 | "\n\n\n" |
14084 | "protected:\n" |
14085 | "};", |
14086 | Style); |
14087 | } |
14088 | |
14089 | TEST_F(FormatTest, FormatsArrays) { |
14090 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" |
14091 | " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); |
14092 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" |
14093 | " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); |
14094 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
14095 | " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); |
14096 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
14097 | " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); |
14098 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
14099 | " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); |
14100 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
14101 | " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" |
14102 | " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); |
14103 | verifyFormat( |
14104 | "llvm::outs() << \"aaaaaaaaaaaa: \"\n" |
14105 | " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" |
14106 | " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); |
14107 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" |
14108 | " .aaaaaaaaaaaaaaaaaaaaaa();"); |
14109 | |
14110 | verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" |
14111 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); |
14112 | verifyFormat( |
14113 | "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" |
14114 | " .aaaaaaa[0]\n" |
14115 | " .aaaaaaaaaaaaaaaaaaaaaa();"); |
14116 | verifyFormat("a[::b::c];"); |
14117 | |
14118 | verifyFormat("{\n" |
14119 | " (*a)[0] = 1;\n" |
14120 | "}"); |
14121 | |
14122 | verifyNoCrash(Code: "a[,Y?)]", Style: getLLVMStyleWithColumns(ColumnLimit: 10)); |
14123 | |
14124 | FormatStyle NoColumnLimit = getLLVMStyleWithColumns(ColumnLimit: 0); |
14125 | verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); |
14126 | } |
14127 | |
14128 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
14129 | verifyFormat("(a)->b();"); |
14130 | verifyFormat("--a;"); |
14131 | } |
14132 | |
14133 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
14134 | verifyFormat("#include <string>\n" |
14135 | "#include <a/b/c.h>\n" |
14136 | "#include \"a/b/string\"\n" |
14137 | "#include \"string.h\"\n" |
14138 | "#include \"string.h\"\n" |
14139 | "#include <a-a>\n" |
14140 | "#include < path with space >\n" |
14141 | "#include_next <test.h>" |
14142 | "#include \"abc.h\" // this is included for ABC\n" |
14143 | "#include \"some long include\" // with a comment\n" |
14144 | "#include \"some very long include path\"\n" |
14145 | "#include <some/very/long/include/path>", |
14146 | getLLVMStyleWithColumns(35)); |
14147 | verifyFormat("#include \"a.h\"", "#include \"a.h\""); |
14148 | verifyFormat("#include <a>", "#include<a>"); |
14149 | |
14150 | verifyFormat("#import <string>"); |
14151 | verifyFormat("#import <a/b/c.h>"); |
14152 | verifyFormat("#import \"a/b/string\""); |
14153 | verifyFormat("#import \"string.h\""); |
14154 | verifyFormat("#import \"string.h\""); |
14155 | verifyFormat("#if __has_include(<strstream>)\n" |
14156 | "#include <strstream>\n" |
14157 | "#endif"); |
14158 | |
14159 | verifyFormat("#define MY_IMPORT <a/b>"); |
14160 | |
14161 | verifyFormat("#if __has_include(<a/b>)"); |
14162 | verifyFormat("#if __has_include_next(<a/b>)"); |
14163 | verifyFormat("#define F __has_include(<a/b>)"); |
14164 | verifyFormat("#define F __has_include_next(<a/b>)"); |
14165 | |
14166 | // Protocol buffer definition or missing "#". |
14167 | verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", |
14168 | getLLVMStyleWithColumns(30)); |
14169 | |
14170 | FormatStyle Style = getLLVMStyle(); |
14171 | Style.AlwaysBreakBeforeMultilineStrings = true; |
14172 | Style.ColumnLimit = 0; |
14173 | verifyFormat("#import \"abc.h\"", Style); |
14174 | |
14175 | // But 'import' might also be a regular C++ namespace. |
14176 | verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
14177 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
14178 | verifyFormat("import::Bar foo(val ? 2 : 1);"); |
14179 | } |
14180 | |
14181 | //===----------------------------------------------------------------------===// |
14182 | // Error recovery tests. |
14183 | //===----------------------------------------------------------------------===// |
14184 | |
14185 | TEST_F(FormatTest, IncompleteParameterLists) { |
14186 | FormatStyle NoBinPacking = getLLVMStyle(); |
14187 | NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
14188 | verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" |
14189 | " double *min_x,\n" |
14190 | " double *max_x,\n" |
14191 | " double *min_y,\n" |
14192 | " double *max_y,\n" |
14193 | " double *min_z,\n" |
14194 | " double *max_z, ) {}", |
14195 | NoBinPacking); |
14196 | } |
14197 | |
14198 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
14199 | verifyFormat("void f() { return; }\n42"); |
14200 | verifyFormat("void f() {\n" |
14201 | " if (0)\n" |
14202 | " return;\n" |
14203 | "}\n" |
14204 | "42"); |
14205 | verifyFormat("void f() { return }\n42"); |
14206 | verifyFormat("void f() {\n" |
14207 | " if (0)\n" |
14208 | " return\n" |
14209 | "}\n" |
14210 | "42"); |
14211 | } |
14212 | |
14213 | TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { |
14214 | verifyFormat("void f() { return }", "void f ( ) { return }"); |
14215 | verifyFormat("void f() {\n" |
14216 | " if (a)\n" |
14217 | " return\n" |
14218 | "}", |
14219 | "void f ( ) { if ( a ) return }"); |
14220 | verifyFormat("namespace N {\n" |
14221 | "void f()\n" |
14222 | "}", |
14223 | "namespace N { void f() }"); |
14224 | verifyFormat("namespace N {\n" |
14225 | "void f() {}\n" |
14226 | "void g()\n" |
14227 | "} // namespace N", |
14228 | "namespace N { void f( ) { } void g( ) }"); |
14229 | } |
14230 | |
14231 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
14232 | verifyFormat("int aaaaaaaa =\n" |
14233 | " // Overlylongcomment\n" |
14234 | " b;", |
14235 | getLLVMStyleWithColumns(20)); |
14236 | verifyFormat("function(\n" |
14237 | " ShortArgument,\n" |
14238 | " LoooooooooooongArgument);", |
14239 | getLLVMStyleWithColumns(20)); |
14240 | } |
14241 | |
14242 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
14243 | verifyFormat("public:"); |
14244 | verifyFormat("class A {\n" |
14245 | "public\n" |
14246 | " void f() {}\n" |
14247 | "};"); |
14248 | verifyFormat("public\n" |
14249 | "int qwerty;"); |
14250 | verifyFormat("public\n" |
14251 | "B {}"); |
14252 | verifyFormat("public\n" |
14253 | "{\n" |
14254 | "}"); |
14255 | verifyFormat("public\n" |
14256 | "B { int x; }"); |
14257 | } |
14258 | |
14259 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
14260 | verifyFormat("{"); |
14261 | verifyFormat("#})"); |
14262 | verifyNoCrash(Code: "(/**/[:!] ?[)."); |
14263 | verifyNoCrash(Code: "struct X {\n" |
14264 | " operator iunt(\n" |
14265 | "};"); |
14266 | verifyNoCrash(Code: "struct Foo {\n" |
14267 | " operator foo(bar\n" |
14268 | "};"); |
14269 | verifyNoCrash(Code: "decltype( {\n" |
14270 | " {"); |
14271 | } |
14272 | |
14273 | TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { |
14274 | // Found by oss-fuzz: |
14275 | // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 |
14276 | FormatStyle Style = getGoogleStyle(Language: FormatStyle::LK_Cpp); |
14277 | Style.ColumnLimit = 60; |
14278 | verifyNoCrash( |
14279 | Code: "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" |
14280 | "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" |
14281 | "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", |
14282 | Style); |
14283 | } |
14284 | |
14285 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
14286 | verifyFormat("do {\n}"); |
14287 | verifyFormat("do {\n}\n" |
14288 | "f();"); |
14289 | verifyFormat("do {\n}\n" |
14290 | "wheeee(fun);"); |
14291 | verifyFormat("do {\n" |
14292 | " f();\n" |
14293 | "}"); |
14294 | } |
14295 | |
14296 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
14297 | verifyFormat("if {\n foo;\n foo();\n}"); |
14298 | verifyFormat("switch {\n foo;\n foo();\n}"); |
14299 | verifyIncompleteFormat("for {\n foo;\n foo();\n}"); |
14300 | verifyIncompleteFormat("ERROR: for target;"); |
14301 | verifyFormat("while {\n foo;\n foo();\n}"); |
14302 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
14303 | } |
14304 | |
14305 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
14306 | verifyIncompleteFormat("namespace {\n" |
14307 | "class Foo { Foo (\n" |
14308 | "};\n" |
14309 | "} // namespace"); |
14310 | } |
14311 | |
14312 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
14313 | verifyFormat("{\n" |
14314 | " {\n" |
14315 | " }", |
14316 | "{\n" |
14317 | "{\n" |
14318 | "}"); |
14319 | verifyFormat("{\n" |
14320 | " {\n" |
14321 | " }", |
14322 | "{\n" |
14323 | " {\n" |
14324 | "}"); |
14325 | verifyFormat("{\n" |
14326 | " {\n" |
14327 | " }"); |
14328 | verifyFormat("{\n" |
14329 | " {\n" |
14330 | " }\n" |
14331 | "}\n" |
14332 | "}", |
14333 | "{\n" |
14334 | " {\n" |
14335 | " }\n" |
14336 | " }\n" |
14337 | "}"); |
14338 | |
14339 | verifyFormat("{\n" |
14340 | " {\n" |
14341 | " breakme(\n" |
14342 | " qwe);\n" |
14343 | " }", |
14344 | "{\n" |
14345 | " {\n" |
14346 | " breakme(qwe);\n" |
14347 | "}", |
14348 | getLLVMStyleWithColumns(10)); |
14349 | } |
14350 | |
14351 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
14352 | verifyFormat("int x = {\n" |
14353 | " avariable,\n" |
14354 | " b(alongervariable)};", |
14355 | getLLVMStyleWithColumns(25)); |
14356 | } |
14357 | |
14358 | TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { |
14359 | verifyFormat("return (a)(b){1, 2, 3};"); |
14360 | } |
14361 | |
14362 | TEST_F(FormatTest, LayoutCxx11BraceInitializers) { |
14363 | verifyFormat("vector<int> x{1, 2, 3, 4};"); |
14364 | verifyFormat("vector<int> x{\n" |
14365 | " 1,\n" |
14366 | " 2,\n" |
14367 | " 3,\n" |
14368 | " 4,\n" |
14369 | "};"); |
14370 | verifyFormat("vector<T> x{{}, {}, {}, {}};"); |
14371 | verifyFormat("f({1, 2});"); |
14372 | verifyFormat("auto v = Foo{-1};"); |
14373 | verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); |
14374 | verifyFormat("Class::Class : member{1, 2, 3} {}"); |
14375 | verifyFormat("new vector<int>{1, 2, 3};"); |
14376 | verifyFormat("new int[3]{1, 2, 3};"); |
14377 | verifyFormat("new int{1};"); |
14378 | verifyFormat("return {arg1, arg2};"); |
14379 | verifyFormat("return {arg1, SomeType{parameter}};"); |
14380 | verifyFormat("int count = set<int>{f(), g(), h()}.size();"); |
14381 | verifyFormat("new T{arg1, arg2};"); |
14382 | verifyFormat("f(MyMap[{composite, key}]);"); |
14383 | verifyFormat("class Class {\n" |
14384 | " T member = {arg1, arg2};\n" |
14385 | "};"); |
14386 | verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); |
14387 | verifyFormat("const struct A a = {.a = 1, .b = 2};"); |
14388 | verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); |
14389 | verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); |
14390 | verifyFormat("int a = std::is_integral<int>{} + 0;"); |
14391 | |
14392 | verifyFormat("int foo(int i) { return fo1{}(i); }"); |
14393 | verifyFormat("int foo(int i) { return fo1{}(i); }"); |
14394 | verifyFormat("auto i = decltype(x){};"); |
14395 | verifyFormat("auto i = typeof(x){};"); |
14396 | verifyFormat("auto i = _Atomic(x){};"); |
14397 | verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); |
14398 | verifyFormat("Node n{1, Node{1000}, //\n" |
14399 | " 2};"); |
14400 | verifyFormat("Aaaa aaaaaaa{\n" |
14401 | " {\n" |
14402 | " aaaa,\n" |
14403 | " },\n" |
14404 | "};"); |
14405 | verifyFormat("class C : public D {\n" |
14406 | " SomeClass SC{2};\n" |
14407 | "};"); |
14408 | verifyFormat("class C : public A {\n" |
14409 | " class D : public B {\n" |
14410 | " void f() { int i{2}; }\n" |
14411 | " };\n" |
14412 | "};"); |
14413 | verifyFormat("#define A {a, a},"); |
14414 | // Don't confuse braced list initializers with compound statements. |
14415 | verifyFormat( |
14416 | "class A {\n" |
14417 | " A() : a{} {}\n" |
14418 | " A() : Base<int>{} {}\n" |
14419 | " A() : Base<Foo<int>>{} {}\n" |
14420 | " A(int b) : b(b) {}\n" |
14421 | " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" |
14422 | " int a, b;\n" |
14423 | " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" |
14424 | " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " |
14425 | "{}\n" |
14426 | "};"); |
14427 | |
14428 | // Avoid breaking between equal sign and opening brace |
14429 | FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); |
14430 | AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; |
14431 | verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" |
14432 | " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" |
14433 | " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" |
14434 | " {\"ccccccccccccccccccccc\", 2}};", |
14435 | AvoidBreakingFirstArgument); |
14436 | |
14437 | // Binpacking only if there is no trailing comma |
14438 | verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" |
14439 | " cccccccccc, dddddddddd};", |
14440 | getLLVMStyleWithColumns(50)); |
14441 | verifyFormat("const Aaaaaa aaaaa = {\n" |
14442 | " aaaaaaaaaaa,\n" |
14443 | " bbbbbbbbbbb,\n" |
14444 | " ccccccccccc,\n" |
14445 | " ddddddddddd,\n" |
14446 | "};", |
14447 | getLLVMStyleWithColumns(50)); |
14448 | |
14449 | // Cases where distinguising braced lists and blocks is hard. |
14450 | verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); |
14451 | verifyFormat("void f() {\n" |
14452 | " return; // comment\n" |
14453 | "}\n" |
14454 | "SomeType t;"); |
14455 | verifyFormat("void f() {\n" |
14456 | " if (a) {\n" |
14457 | " f();\n" |
14458 | " }\n" |
14459 | "}\n" |
14460 | "SomeType t;"); |
14461 | |
14462 | // In combination with BinPackArguments = false. |
14463 | FormatStyle NoBinPacking = getLLVMStyle(); |
14464 | NoBinPacking.BinPackArguments = false; |
14465 | verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" |
14466 | " bbbbb,\n" |
14467 | " ccccc,\n" |
14468 | " ddddd,\n" |
14469 | " eeeee,\n" |
14470 | " ffffff,\n" |
14471 | " ggggg,\n" |
14472 | " hhhhhh,\n" |
14473 | " iiiiii,\n" |
14474 | " jjjjjj,\n" |
14475 | " kkkkkk};", |
14476 | NoBinPacking); |
14477 | verifyFormat("const Aaaaaa aaaaa = {\n" |
14478 | " aaaaa,\n" |
14479 | " bbbbb,\n" |
14480 | " ccccc,\n" |
14481 | " ddddd,\n" |
14482 | " eeeee,\n" |
14483 | " ffffff,\n" |
14484 | " ggggg,\n" |
14485 | " hhhhhh,\n" |
14486 | " iiiiii,\n" |
14487 | " jjjjjj,\n" |
14488 | " kkkkkk,\n" |
14489 | "};", |
14490 | NoBinPacking); |
14491 | verifyFormat( |
14492 | "const Aaaaaa aaaaa = {\n" |
14493 | " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" |
14494 | " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" |
14495 | " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" |
14496 | "};", |
14497 | NoBinPacking); |
14498 | |
14499 | NoBinPacking.BinPackLongBracedList = false; |
14500 | verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" |
14501 | " bbbbb,\n" |
14502 | " ccccc,\n" |
14503 | " ddddd,\n" |
14504 | " eeeee,\n" |
14505 | " ffffff,\n" |
14506 | " ggggg,\n" |
14507 | " hhhhhh,\n" |
14508 | " iiiiii,\n" |
14509 | " jjjjjj,\n" |
14510 | " kkkkkk,\n" |
14511 | " aaaaa,\n" |
14512 | " bbbbb,\n" |
14513 | " ccccc,\n" |
14514 | " ddddd,\n" |
14515 | " eeeee,\n" |
14516 | " ffffff,\n" |
14517 | " ggggg,\n" |
14518 | " hhhhhh,\n" |
14519 | " iiiiii};", |
14520 | NoBinPacking); |
14521 | verifyFormat("const Aaaaaa aaaaa = {\n" |
14522 | " aaaaa,\n" |
14523 | " bbbbb,\n" |
14524 | " ccccc,\n" |
14525 | " ddddd,\n" |
14526 | " eeeee,\n" |
14527 | " ffffff,\n" |
14528 | " ggggg,\n" |
14529 | " hhhhhh,\n" |
14530 | " iiiiii,\n" |
14531 | " jjjjjj,\n" |
14532 | " kkkkkk,\n" |
14533 | " aaaaa,\n" |
14534 | " bbbbb,\n" |
14535 | " ccccc,\n" |
14536 | " ddddd,\n" |
14537 | " eeeee,\n" |
14538 | " ffffff,\n" |
14539 | " ggggg,\n" |
14540 | " hhhhhh,\n" |
14541 | "};", |
14542 | NoBinPacking); |
14543 | |
14544 | NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
14545 | verifyFormat("static uint8 CddDp83848Reg[] = {\n" |
14546 | " CDDDP83848_BMCR_REGISTER,\n" |
14547 | " CDDDP83848_BMSR_REGISTER,\n" |
14548 | " CDDDP83848_RBR_REGISTER};", |
14549 | "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" |
14550 | " CDDDP83848_BMSR_REGISTER,\n" |
14551 | " CDDDP83848_RBR_REGISTER};", |
14552 | NoBinPacking); |
14553 | |
14554 | // FIXME: The alignment of these trailing comments might be bad. Then again, |
14555 | // this might be utterly useless in real code. |
14556 | verifyFormat("Constructor::Constructor()\n" |
14557 | " : some_value{ //\n" |
14558 | " aaaaaaa, //\n" |
14559 | " bbbbbbb} {}"); |
14560 | |
14561 | // In braced lists, the first comment is always assumed to belong to the |
14562 | // first element. Thus, it can be moved to the next or previous line as |
14563 | // appropriate. |
14564 | verifyFormat("function({// First element:\n" |
14565 | " 1,\n" |
14566 | " // Second element:\n" |
14567 | " 2});", |
14568 | "function({\n" |
14569 | " // First element:\n" |
14570 | " 1,\n" |
14571 | " // Second element:\n" |
14572 | " 2});"); |
14573 | verifyFormat("std::vector<int> MyNumbers{\n" |
14574 | " // First element:\n" |
14575 | " 1,\n" |
14576 | " // Second element:\n" |
14577 | " 2};", |
14578 | "std::vector<int> MyNumbers{// First element:\n" |
14579 | " 1,\n" |
14580 | " // Second element:\n" |
14581 | " 2};", |
14582 | getLLVMStyleWithColumns(30)); |
14583 | // A trailing comma should still lead to an enforced line break and no |
14584 | // binpacking. |
14585 | verifyFormat("vector<int> SomeVector = {\n" |
14586 | " // aaa\n" |
14587 | " 1,\n" |
14588 | " 2,\n" |
14589 | "};", |
14590 | "vector<int> SomeVector = { // aaa\n" |
14591 | " 1, 2, };"); |
14592 | |
14593 | // C++11 brace initializer list l-braces should not be treated any differently |
14594 | // when breaking before lambda bodies is enabled |
14595 | FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); |
14596 | BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; |
14597 | BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; |
14598 | BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; |
14599 | verifyFormat( |
14600 | "std::runtime_error{\n" |
14601 | " \"Long string which will force a break onto the next line...\"};", |
14602 | BreakBeforeLambdaBody); |
14603 | |
14604 | FormatStyle ExtraSpaces = getLLVMStyle(); |
14605 | ExtraSpaces.Cpp11BracedListStyle = false; |
14606 | ExtraSpaces.ColumnLimit = 75; |
14607 | verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); |
14608 | verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); |
14609 | verifyFormat("f({ 1, 2 });", ExtraSpaces); |
14610 | verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); |
14611 | verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); |
14612 | verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); |
14613 | verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); |
14614 | verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); |
14615 | verifyFormat("return { arg1, arg2 };", ExtraSpaces); |
14616 | verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); |
14617 | verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); |
14618 | verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); |
14619 | verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); |
14620 | verifyFormat("class Class {\n" |
14621 | " T member = { arg1, arg2 };\n" |
14622 | "};", |
14623 | ExtraSpaces); |
14624 | verifyFormat( |
14625 | "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
14626 | " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" |
14627 | " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
14628 | " bbbbbbbbbbbbbbbbbbbb, bbbbb };", |
14629 | ExtraSpaces); |
14630 | verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); |
14631 | verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", |
14632 | ExtraSpaces); |
14633 | verifyFormat( |
14634 | "someFunction(OtherParam,\n" |
14635 | " BracedList{ // comment 1 (Forcing interesting break)\n" |
14636 | " param1, param2,\n" |
14637 | " // comment 2\n" |
14638 | " param3, param4 });", |
14639 | ExtraSpaces); |
14640 | verifyFormat( |
14641 | "std::this_thread::sleep_for(\n" |
14642 | " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", |
14643 | ExtraSpaces); |
14644 | verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" |
14645 | " aaaaaaa,\n" |
14646 | " aaaaaaaaaa,\n" |
14647 | " aaaaa,\n" |
14648 | " aaaaaaaaaaaaaaa,\n" |
14649 | " aaa,\n" |
14650 | " aaaaaaaaaa,\n" |
14651 | " a,\n" |
14652 | " aaaaaaaaaaaaaaaaaaaaa,\n" |
14653 | " aaaaaaaaaaaa,\n" |
14654 | " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" |
14655 | " aaaaaaa,\n" |
14656 | " a};"); |
14657 | verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); |
14658 | verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); |
14659 | verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); |
14660 | |
14661 | // Avoid breaking between initializer/equal sign and opening brace |
14662 | ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; |
14663 | verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" |
14664 | " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" |
14665 | " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" |
14666 | " { \"ccccccccccccccccccccc\", 2 }\n" |
14667 | "};", |
14668 | ExtraSpaces); |
14669 | verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" |
14670 | " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" |
14671 | " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" |
14672 | " { \"ccccccccccccccccccccc\", 2 }\n" |
14673 | "};", |
14674 | ExtraSpaces); |
14675 | |
14676 | FormatStyle SpaceBeforeBrace = getLLVMStyle(); |
14677 | SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; |
14678 | verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); |
14679 | verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); |
14680 | |
14681 | FormatStyle SpaceBetweenBraces = getLLVMStyle(); |
14682 | SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; |
14683 | SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; |
14684 | SpaceBetweenBraces.SpacesInParensOptions.Other = true; |
14685 | SpaceBetweenBraces.SpacesInSquareBrackets = true; |
14686 | verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); |
14687 | verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); |
14688 | verifyFormat("vector< int > x{ // comment 1\n" |
14689 | " 1, 2, 3, 4 };", |
14690 | SpaceBetweenBraces); |
14691 | SpaceBetweenBraces.ColumnLimit = 20; |
14692 | verifyFormat("vector< int > x{\n" |
14693 | " 1, 2, 3, 4 };", |
14694 | "vector<int>x{1,2,3,4};", SpaceBetweenBraces); |
14695 | SpaceBetweenBraces.ColumnLimit = 24; |
14696 | verifyFormat("vector< int > x{ 1, 2,\n" |
14697 | " 3, 4 };", |
14698 | "vector<int>x{1,2,3,4};", SpaceBetweenBraces); |
14699 | verifyFormat("vector< int > x{\n" |
14700 | " 1,\n" |
14701 | " 2,\n" |
14702 | " 3,\n" |
14703 | " 4,\n" |
14704 | "};", |
14705 | "vector<int>x{1,2,3,4,};", SpaceBetweenBraces); |
14706 | verifyFormat("vector< int > x{};", SpaceBetweenBraces); |
14707 | SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; |
14708 | SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true; |
14709 | verifyFormat("vector< int > x{ };", SpaceBetweenBraces); |
14710 | } |
14711 | |
14712 | TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { |
14713 | verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14714 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14715 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14716 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14717 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14718 | " 1, 22, 333, 4444, 55555, 666666, 7777777};"); |
14719 | verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" |
14720 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14721 | " 1, 22, 333, 4444, 55555, //\n" |
14722 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14723 | " 1, 22, 333, 4444, 55555, 666666, 7777777};"); |
14724 | verifyFormat( |
14725 | "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14726 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14727 | " 1, 22, 333, 4444, 55555, 666666, // comment\n" |
14728 | " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" |
14729 | " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" |
14730 | " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" |
14731 | " 7777777};"); |
14732 | verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" |
14733 | " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" |
14734 | " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); |
14735 | verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" |
14736 | " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" |
14737 | " // Separating comment.\n" |
14738 | " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); |
14739 | verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" |
14740 | " // Leading comment\n" |
14741 | " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" |
14742 | " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); |
14743 | verifyFormat("vector<int> x = {1, 1, 1, 1,\n" |
14744 | " 1, 1, 1, 1};", |
14745 | getLLVMStyleWithColumns(39)); |
14746 | verifyFormat("vector<int> x = {1, 1, 1, 1,\n" |
14747 | " 1, 1, 1, 1};", |
14748 | getLLVMStyleWithColumns(38)); |
14749 | verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" |
14750 | " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", |
14751 | getLLVMStyleWithColumns(43)); |
14752 | verifyFormat( |
14753 | "static unsigned SomeValues[10][3] = {\n" |
14754 | " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" |
14755 | " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); |
14756 | verifyFormat("static auto fields = new vector<string>{\n" |
14757 | " \"aaaaaaaaaaaaa\",\n" |
14758 | " \"aaaaaaaaaaaaa\",\n" |
14759 | " \"aaaaaaaaaaaa\",\n" |
14760 | " \"aaaaaaaaaaaaaa\",\n" |
14761 | " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" |
14762 | " \"aaaaaaaaaaaa\",\n" |
14763 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" |
14764 | "};"); |
14765 | verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); |
14766 | verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" |
14767 | " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" |
14768 | " 3, cccccccccccccccccccccc};", |
14769 | getLLVMStyleWithColumns(60)); |
14770 | |
14771 | // Trailing commas. |
14772 | verifyFormat("vector<int> x = {\n" |
14773 | " 1, 1, 1, 1, 1, 1, 1, 1,\n" |
14774 | "};", |
14775 | getLLVMStyleWithColumns(39)); |
14776 | verifyFormat("vector<int> x = {\n" |
14777 | " 1, 1, 1, 1, 1, 1, 1, 1, //\n" |
14778 | "};", |
14779 | getLLVMStyleWithColumns(39)); |
14780 | verifyFormat("vector<int> x = {1, 1, 1, 1,\n" |
14781 | " 1, 1, 1, 1,\n" |
14782 | " /**/ /**/};", |
14783 | getLLVMStyleWithColumns(39)); |
14784 | |
14785 | // Trailing comment in the first line. |
14786 | verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" |
14787 | " 1111111111, 2222222222, 33333333333, 4444444444, //\n" |
14788 | " 111111111, 222222222, 3333333333, 444444444, //\n" |
14789 | " 11111111, 22222222, 333333333, 44444444};"); |
14790 | // Trailing comment in the last line. |
14791 | verifyFormat("int aaaaa[] = {\n" |
14792 | " 1, 2, 3, // comment\n" |
14793 | " 4, 5, 6 // comment\n" |
14794 | "};"); |
14795 | |
14796 | // With nested lists, we should either format one item per line or all nested |
14797 | // lists one on line. |
14798 | // FIXME: For some nested lists, we can do better. |
14799 | verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" |
14800 | " {aaaaaaaaaaaaaaaaaaa},\n" |
14801 | " {aaaaaaaaaaaaaaaaaaaaa},\n" |
14802 | " {aaaaaaaaaaaaaaaaa}};", |
14803 | getLLVMStyleWithColumns(60)); |
14804 | verifyFormat( |
14805 | "SomeStruct my_struct_array = {\n" |
14806 | " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" |
14807 | " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" |
14808 | " {aaa, aaa},\n" |
14809 | " {aaa, aaa},\n" |
14810 | " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" |
14811 | " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" |
14812 | " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); |
14813 | |
14814 | // No column layout should be used here. |
14815 | verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" |
14816 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); |
14817 | |
14818 | verifyNoCrash(Code: "a<,"); |
14819 | |
14820 | // No braced initializer here. |
14821 | verifyFormat("void f() {\n" |
14822 | " struct Dummy {};\n" |
14823 | " f(v);\n" |
14824 | "}"); |
14825 | verifyFormat("void foo() {\n" |
14826 | " { // asdf\n" |
14827 | " {\n" |
14828 | " int a;\n" |
14829 | " }\n" |
14830 | " }\n" |
14831 | " {\n" |
14832 | " {\n" |
14833 | " int b;\n" |
14834 | " }\n" |
14835 | " }\n" |
14836 | "}"); |
14837 | verifyFormat("namespace n {\n" |
14838 | "void foo() {\n" |
14839 | " {\n" |
14840 | " {\n" |
14841 | " statement();\n" |
14842 | " if (false) {\n" |
14843 | " }\n" |
14844 | " }\n" |
14845 | " }\n" |
14846 | " {\n" |
14847 | " }\n" |
14848 | "}\n" |
14849 | "} // namespace n"); |
14850 | |
14851 | // Long lists should be formatted in columns even if they are nested. |
14852 | verifyFormat( |
14853 | "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14854 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14855 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14856 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14857 | " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" |
14858 | " 1, 22, 333, 4444, 55555, 666666, 7777777});"); |
14859 | |
14860 | // Allow "single-column" layout even if that violates the column limit. There |
14861 | // isn't going to be a better way. |
14862 | verifyFormat("std::vector<int> a = {\n" |
14863 | " aaaaaaaa,\n" |
14864 | " aaaaaaaa,\n" |
14865 | " aaaaaaaa,\n" |
14866 | " aaaaaaaa,\n" |
14867 | " aaaaaaaaaa,\n" |
14868 | " aaaaaaaa,\n" |
14869 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", |
14870 | getLLVMStyleWithColumns(30)); |
14871 | verifyFormat("vector<int> aaaa = {\n" |
14872 | " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
14873 | " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
14874 | " aaaaaa.aaaaaaa,\n" |
14875 | " aaaaaa.aaaaaaa,\n" |
14876 | " aaaaaa.aaaaaaa,\n" |
14877 | " aaaaaa.aaaaaaa,\n" |
14878 | "};"); |
14879 | |
14880 | // Don't create hanging lists. |
14881 | verifyFormat("someFunction(Param, {List1, List2,\n" |
14882 | " List3});", |
14883 | getLLVMStyleWithColumns(35)); |
14884 | verifyFormat("someFunction(Param, Param,\n" |
14885 | " {List1, List2,\n" |
14886 | " List3});", |
14887 | getLLVMStyleWithColumns(35)); |
14888 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" |
14889 | " aaaaaaaaaaaaaaaaaaaaaaa);"); |
14890 | |
14891 | // No possible column formats, don't want the optimal paths penalized. |
14892 | verifyFormat( |
14893 | "waarudo::unit desk = {\n" |
14894 | " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};"); |
14895 | verifyFormat("SomeType something1([](const Input &i) -> Output { return " |
14896 | "Output{1, 2}; },\n" |
14897 | " [](const Input &i) -> Output { return " |
14898 | "Output{1, 2}; });"); |
14899 | FormatStyle NoBinPacking = getLLVMStyle(); |
14900 | NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
14901 | verifyFormat("waarudo::unit desk = {\n" |
14902 | " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, " |
14903 | "1, 1} * w::m; }};", |
14904 | NoBinPacking); |
14905 | } |
14906 | |
14907 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
14908 | FormatStyle DoNotMerge = getLLVMStyle(); |
14909 | DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
14910 | |
14911 | verifyFormat("void f() { return 42; }"); |
14912 | verifyFormat("void f() {\n" |
14913 | " return 42;\n" |
14914 | "}", |
14915 | DoNotMerge); |
14916 | verifyFormat("void f() {\n" |
14917 | " // Comment\n" |
14918 | "}"); |
14919 | verifyFormat("{\n" |
14920 | "#error {\n" |
14921 | " int a;\n" |
14922 | "}"); |
14923 | verifyFormat("{\n" |
14924 | " int a;\n" |
14925 | "#error {\n" |
14926 | "}"); |
14927 | verifyFormat("void f() {} // comment"); |
14928 | verifyFormat("void f() { int a; } // comment"); |
14929 | verifyFormat("void f() {\n" |
14930 | "} // comment", |
14931 | DoNotMerge); |
14932 | verifyFormat("void f() {\n" |
14933 | " int a;\n" |
14934 | "} // comment", |
14935 | DoNotMerge); |
14936 | verifyFormat("void f() {\n" |
14937 | "} // comment", |
14938 | getLLVMStyleWithColumns(15)); |
14939 | |
14940 | verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); |
14941 | verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); |
14942 | |
14943 | verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); |
14944 | verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); |
14945 | verifyGoogleFormat("class C {\n" |
14946 | " C()\n" |
14947 | " : iiiiiiii(nullptr),\n" |
14948 | " kkkkkkk(nullptr),\n" |
14949 | " mmmmmmm(nullptr),\n" |
14950 | " nnnnnnn(nullptr) {}\n" |
14951 | "};"); |
14952 | |
14953 | FormatStyle NoColumnLimit = getLLVMStyleWithColumns(ColumnLimit: 0); |
14954 | verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit); |
14955 | verifyFormat("class C {\n" |
14956 | " A() : b(0) {}\n" |
14957 | "};", |
14958 | "class C{A():b(0){}};", NoColumnLimit); |
14959 | verifyFormat("A()\n" |
14960 | " : b(0) {\n" |
14961 | "}", |
14962 | "A()\n:b(0)\n{\n}", NoColumnLimit); |
14963 | |
14964 | FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit; |
14965 | NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom; |
14966 | NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true; |
14967 | verifyFormat("class C {\n" |
14968 | "#pragma foo\n" |
14969 | " int foo { return 0; }\n" |
14970 | "};", |
14971 | NoColumnLimitWrapAfterFunction); |
14972 | verifyFormat("class C {\n" |
14973 | "#pragma foo\n" |
14974 | " void foo {}\n" |
14975 | "};", |
14976 | NoColumnLimitWrapAfterFunction); |
14977 | |
14978 | FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; |
14979 | DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = |
14980 | FormatStyle::SFS_None; |
14981 | verifyFormat("A() : b(0) {\n" |
14982 | "}", |
14983 | DoNotMergeNoColumnLimit); |
14984 | verifyNoChange("A()\n" |
14985 | " : b(0) {\n" |
14986 | "}", |
14987 | DoNotMergeNoColumnLimit); |
14988 | verifyFormat("A()\n" |
14989 | " : b(0) {\n" |
14990 | "}", |
14991 | "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit); |
14992 | |
14993 | verifyFormat("#define A \\\n" |
14994 | " void f() { \\\n" |
14995 | " int i; \\\n" |
14996 | " }", |
14997 | getLLVMStyleWithColumns(20)); |
14998 | verifyFormat("#define A \\\n" |
14999 | " void f() { int i; }", |
15000 | getLLVMStyleWithColumns(21)); |
15001 | verifyFormat("#define A \\\n" |
15002 | " void f() { \\\n" |
15003 | " int i; \\\n" |
15004 | " } \\\n" |
15005 | " int j;", |
15006 | getLLVMStyleWithColumns(22)); |
15007 | verifyFormat("#define A \\\n" |
15008 | " void f() { int i; } \\\n" |
15009 | " int j;", |
15010 | getLLVMStyleWithColumns(23)); |
15011 | |
15012 | verifyFormat( |
15013 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
15014 | " aaaaaaaaaaaaaaaaaa,\n" |
15015 | " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}"); |
15016 | |
15017 | constexpr StringRef Code{"void foo() { /* Empty */ }"}; |
15018 | verifyFormat(Code); |
15019 | verifyFormat(Code, "void foo() { /* Empty */\n" |
15020 | "}"); |
15021 | verifyFormat(Code, "void foo() {\n" |
15022 | "/* Empty */\n" |
15023 | "}"); |
15024 | } |
15025 | |
15026 | TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { |
15027 | FormatStyle MergeEmptyOnly = getLLVMStyle(); |
15028 | MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; |
15029 | verifyFormat("class C {\n" |
15030 | " int f() {}\n" |
15031 | "};", |
15032 | MergeEmptyOnly); |
15033 | verifyFormat("class C {\n" |
15034 | " int f() {\n" |
15035 | " return 42;\n" |
15036 | " }\n" |
15037 | "};", |
15038 | MergeEmptyOnly); |
15039 | verifyFormat("int f() {}", MergeEmptyOnly); |
15040 | verifyFormat("int f() {\n" |
15041 | " return 42;\n" |
15042 | "}", |
15043 | MergeEmptyOnly); |
15044 | |
15045 | // Also verify behavior when BraceWrapping.AfterFunction = true |
15046 | MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; |
15047 | MergeEmptyOnly.BraceWrapping.AfterFunction = true; |
15048 | verifyFormat("int f() {}", MergeEmptyOnly); |
15049 | verifyFormat("class C {\n" |
15050 | " int f() {}\n" |
15051 | "};", |
15052 | MergeEmptyOnly); |
15053 | } |
15054 | |
15055 | TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { |
15056 | FormatStyle MergeInlineOnly = getLLVMStyle(); |
15057 | MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
15058 | verifyFormat("class C {\n" |
15059 | " int f() { return 42; }\n" |
15060 | "};", |
15061 | MergeInlineOnly); |
15062 | verifyFormat("int f() {\n" |
15063 | " return 42;\n" |
15064 | "}", |
15065 | MergeInlineOnly); |
15066 | |
15067 | // SFS_Inline implies SFS_Empty |
15068 | verifyFormat("class C {\n" |
15069 | " int f() {}\n" |
15070 | "};", |
15071 | MergeInlineOnly); |
15072 | verifyFormat("int f() {}", MergeInlineOnly); |
15073 | // https://llvm.org/PR54147 |
15074 | verifyFormat("auto lambda = []() {\n" |
15075 | " // comment\n" |
15076 | " f();\n" |
15077 | " g();\n" |
15078 | "};", |
15079 | MergeInlineOnly); |
15080 | |
15081 | verifyFormat("class C {\n" |
15082 | "#ifdef A\n" |
15083 | " int f() { return 42; }\n" |
15084 | "#endif\n" |
15085 | "};", |
15086 | MergeInlineOnly); |
15087 | |
15088 | verifyFormat("struct S {\n" |
15089 | "// comment\n" |
15090 | "#ifdef FOO\n" |
15091 | " int foo() { bar(); }\n" |
15092 | "#endif\n" |
15093 | "};", |
15094 | MergeInlineOnly); |
15095 | |
15096 | MergeInlineOnly.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
15097 | verifyFormat("#define Foo \\\n" |
15098 | " struct S { \\\n" |
15099 | " void foo() { return; } \\\n" |
15100 | " }", |
15101 | MergeInlineOnly); |
15102 | |
15103 | // Also verify behavior when BraceWrapping.AfterFunction = true |
15104 | MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; |
15105 | MergeInlineOnly.BraceWrapping.AfterFunction = true; |
15106 | verifyFormat("class C {\n" |
15107 | " int f() { return 42; }\n" |
15108 | "};", |
15109 | MergeInlineOnly); |
15110 | verifyFormat("int f()\n" |
15111 | "{\n" |
15112 | " return 42;\n" |
15113 | "}", |
15114 | MergeInlineOnly); |
15115 | |
15116 | // SFS_Inline implies SFS_Empty |
15117 | verifyFormat("int f() {}", MergeInlineOnly); |
15118 | verifyFormat("class C {\n" |
15119 | " int f() {}\n" |
15120 | "};", |
15121 | MergeInlineOnly); |
15122 | |
15123 | MergeInlineOnly.BraceWrapping.AfterClass = true; |
15124 | MergeInlineOnly.BraceWrapping.AfterStruct = true; |
15125 | verifyFormat("class C\n" |
15126 | "{\n" |
15127 | " int f() { return 42; }\n" |
15128 | "};", |
15129 | MergeInlineOnly); |
15130 | verifyFormat("struct C\n" |
15131 | "{\n" |
15132 | " int f() { return 42; }\n" |
15133 | "};", |
15134 | MergeInlineOnly); |
15135 | verifyFormat("int f()\n" |
15136 | "{\n" |
15137 | " return 42;\n" |
15138 | "}", |
15139 | MergeInlineOnly); |
15140 | verifyFormat("int f() {}", MergeInlineOnly); |
15141 | verifyFormat("class C\n" |
15142 | "{\n" |
15143 | " int f() { return 42; }\n" |
15144 | "};", |
15145 | MergeInlineOnly); |
15146 | verifyFormat("struct C\n" |
15147 | "{\n" |
15148 | " int f() { return 42; }\n" |
15149 | "};", |
15150 | MergeInlineOnly); |
15151 | verifyFormat("struct C\n" |
15152 | "// comment\n" |
15153 | "/* comment */\n" |
15154 | "// comment\n" |
15155 | "{\n" |
15156 | " int f() { return 42; }\n" |
15157 | "};", |
15158 | MergeInlineOnly); |
15159 | verifyFormat("/* comment */ struct C\n" |
15160 | "{\n" |
15161 | " int f() { return 42; }\n" |
15162 | "};", |
15163 | MergeInlineOnly); |
15164 | } |
15165 | |
15166 | TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { |
15167 | FormatStyle MergeInlineOnly = getLLVMStyle(); |
15168 | MergeInlineOnly.AllowShortFunctionsOnASingleLine = |
15169 | FormatStyle::SFS_InlineOnly; |
15170 | verifyFormat("class C {\n" |
15171 | " int f() { return 42; }\n" |
15172 | "};", |
15173 | MergeInlineOnly); |
15174 | verifyFormat("int f() {\n" |
15175 | " return 42;\n" |
15176 | "}", |
15177 | MergeInlineOnly); |
15178 | |
15179 | // SFS_InlineOnly does not imply SFS_Empty |
15180 | verifyFormat("class C {\n" |
15181 | " int f() {}\n" |
15182 | "};", |
15183 | MergeInlineOnly); |
15184 | verifyFormat("int f() {\n" |
15185 | "}", |
15186 | MergeInlineOnly); |
15187 | |
15188 | MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; |
15189 | verifyFormat("class Foo\n" |
15190 | " {\n" |
15191 | " void f() { foo(); }\n" |
15192 | " };", |
15193 | MergeInlineOnly); |
15194 | |
15195 | // Also verify behavior when BraceWrapping.AfterFunction = true |
15196 | MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; |
15197 | MergeInlineOnly.BraceWrapping.AfterFunction = true; |
15198 | verifyFormat("class C {\n" |
15199 | " int f() { return 42; }\n" |
15200 | "};", |
15201 | MergeInlineOnly); |
15202 | verifyFormat("int f()\n" |
15203 | "{\n" |
15204 | " return 42;\n" |
15205 | "}", |
15206 | MergeInlineOnly); |
15207 | |
15208 | // SFS_InlineOnly does not imply SFS_Empty |
15209 | verifyFormat("int f()\n" |
15210 | "{\n" |
15211 | "}", |
15212 | MergeInlineOnly); |
15213 | verifyFormat("class C {\n" |
15214 | " int f() {}\n" |
15215 | "};", |
15216 | MergeInlineOnly); |
15217 | } |
15218 | |
15219 | TEST_F(FormatTest, SplitEmptyFunction) { |
15220 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
15221 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
15222 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15223 | Style.BraceWrapping.AfterFunction = true; |
15224 | Style.BraceWrapping.SplitEmptyFunction = false; |
15225 | |
15226 | verifyFormat("int f()\n" |
15227 | "{}", |
15228 | Style); |
15229 | verifyFormat("int f()\n" |
15230 | "{\n" |
15231 | " return 42;\n" |
15232 | "}", |
15233 | Style); |
15234 | verifyFormat("int f()\n" |
15235 | "{\n" |
15236 | " // some comment\n" |
15237 | "}", |
15238 | Style); |
15239 | |
15240 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; |
15241 | verifyFormat("int f() {}", Style); |
15242 | verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" |
15243 | "{}", |
15244 | Style); |
15245 | verifyFormat("int f()\n" |
15246 | "{\n" |
15247 | " return 0;\n" |
15248 | "}", |
15249 | Style); |
15250 | |
15251 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
15252 | verifyFormat("class Foo {\n" |
15253 | " int f() {}\n" |
15254 | "};", |
15255 | Style); |
15256 | verifyFormat("class Foo {\n" |
15257 | " int f() { return 0; }\n" |
15258 | "};", |
15259 | Style); |
15260 | verifyFormat("class Foo {\n" |
15261 | " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" |
15262 | " {}\n" |
15263 | "};", |
15264 | Style); |
15265 | verifyFormat("class Foo {\n" |
15266 | " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" |
15267 | " {\n" |
15268 | " return 0;\n" |
15269 | " }\n" |
15270 | "};", |
15271 | Style); |
15272 | |
15273 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
15274 | verifyFormat("int f() {}", Style); |
15275 | verifyFormat("int f() { return 0; }", Style); |
15276 | verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" |
15277 | "{}", |
15278 | Style); |
15279 | verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" |
15280 | "{\n" |
15281 | " return 0;\n" |
15282 | "}", |
15283 | Style); |
15284 | } |
15285 | |
15286 | TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { |
15287 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
15288 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
15289 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15290 | Style.BraceWrapping.AfterFunction = true; |
15291 | Style.BraceWrapping.SplitEmptyFunction = true; |
15292 | Style.BraceWrapping.SplitEmptyRecord = false; |
15293 | |
15294 | verifyFormat("class C {};", Style); |
15295 | verifyFormat("struct C {};", Style); |
15296 | verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
15297 | " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" |
15298 | "{\n" |
15299 | "}", |
15300 | Style); |
15301 | verifyFormat("class C {\n" |
15302 | " C()\n" |
15303 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" |
15304 | " bbbbbbbbbbbbbbbbbbb()\n" |
15305 | " {\n" |
15306 | " }\n" |
15307 | " void\n" |
15308 | " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
15309 | " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" |
15310 | " {\n" |
15311 | " }\n" |
15312 | "};", |
15313 | Style); |
15314 | } |
15315 | |
15316 | TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { |
15317 | FormatStyle Style = getLLVMStyle(); |
15318 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
15319 | verifyFormat("#ifdef A\n" |
15320 | "int f() {}\n" |
15321 | "#else\n" |
15322 | "int g() {}\n" |
15323 | "#endif", |
15324 | Style); |
15325 | } |
15326 | |
15327 | TEST_F(FormatTest, SplitEmptyClass) { |
15328 | FormatStyle Style = getLLVMStyle(); |
15329 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15330 | Style.BraceWrapping.AfterClass = true; |
15331 | Style.BraceWrapping.SplitEmptyRecord = false; |
15332 | |
15333 | verifyFormat("class Foo\n" |
15334 | "{};", |
15335 | Style); |
15336 | verifyFormat("/* something */ class Foo\n" |
15337 | "{};", |
15338 | Style); |
15339 | verifyFormat("template <typename X> class Foo\n" |
15340 | "{};", |
15341 | Style); |
15342 | verifyFormat("class Foo\n" |
15343 | "{\n" |
15344 | " Foo();\n" |
15345 | "};", |
15346 | Style); |
15347 | verifyFormat("typedef class Foo\n" |
15348 | "{\n" |
15349 | "} Foo_t;", |
15350 | Style); |
15351 | |
15352 | Style.BraceWrapping.SplitEmptyRecord = true; |
15353 | Style.BraceWrapping.AfterStruct = true; |
15354 | verifyFormat("class rep\n" |
15355 | "{\n" |
15356 | "};", |
15357 | Style); |
15358 | verifyFormat("struct rep\n" |
15359 | "{\n" |
15360 | "};", |
15361 | Style); |
15362 | verifyFormat("template <typename T> class rep\n" |
15363 | "{\n" |
15364 | "};", |
15365 | Style); |
15366 | verifyFormat("template <typename T> struct rep\n" |
15367 | "{\n" |
15368 | "};", |
15369 | Style); |
15370 | verifyFormat("class rep\n" |
15371 | "{\n" |
15372 | " int x;\n" |
15373 | "};", |
15374 | Style); |
15375 | verifyFormat("struct rep\n" |
15376 | "{\n" |
15377 | " int x;\n" |
15378 | "};", |
15379 | Style); |
15380 | verifyFormat("template <typename T> class rep\n" |
15381 | "{\n" |
15382 | " int x;\n" |
15383 | "};", |
15384 | Style); |
15385 | verifyFormat("template <typename T> struct rep\n" |
15386 | "{\n" |
15387 | " int x;\n" |
15388 | "};", |
15389 | Style); |
15390 | verifyFormat("template <typename T> class rep // Foo\n" |
15391 | "{\n" |
15392 | " int x;\n" |
15393 | "};", |
15394 | Style); |
15395 | verifyFormat("template <typename T> struct rep // Bar\n" |
15396 | "{\n" |
15397 | " int x;\n" |
15398 | "};", |
15399 | Style); |
15400 | |
15401 | verifyFormat("template <typename T> class rep<T>\n" |
15402 | "{\n" |
15403 | " int x;\n" |
15404 | "};", |
15405 | Style); |
15406 | |
15407 | verifyFormat("template <typename T> class rep<std::complex<T>>\n" |
15408 | "{\n" |
15409 | " int x;\n" |
15410 | "};", |
15411 | Style); |
15412 | verifyFormat("template <typename T> class rep<std::complex<T>>\n" |
15413 | "{\n" |
15414 | "};", |
15415 | Style); |
15416 | |
15417 | verifyFormat("#include \"stdint.h\"\n" |
15418 | "namespace rep {}", |
15419 | Style); |
15420 | verifyFormat("#include <stdint.h>\n" |
15421 | "namespace rep {}", |
15422 | Style); |
15423 | verifyFormat("#include <stdint.h>\n" |
15424 | "namespace rep {}", |
15425 | "#include <stdint.h>\n" |
15426 | "namespace rep {\n" |
15427 | "\n" |
15428 | "\n" |
15429 | "}", |
15430 | Style); |
15431 | } |
15432 | |
15433 | TEST_F(FormatTest, SplitEmptyStruct) { |
15434 | FormatStyle Style = getLLVMStyle(); |
15435 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15436 | Style.BraceWrapping.AfterStruct = true; |
15437 | Style.BraceWrapping.SplitEmptyRecord = false; |
15438 | |
15439 | verifyFormat("struct Foo\n" |
15440 | "{};", |
15441 | Style); |
15442 | verifyFormat("/* something */ struct Foo\n" |
15443 | "{};", |
15444 | Style); |
15445 | verifyFormat("template <typename X> struct Foo\n" |
15446 | "{};", |
15447 | Style); |
15448 | verifyFormat("struct Foo\n" |
15449 | "{\n" |
15450 | " Foo();\n" |
15451 | "};", |
15452 | Style); |
15453 | verifyFormat("typedef struct Foo\n" |
15454 | "{\n" |
15455 | "} Foo_t;", |
15456 | Style); |
15457 | // typedef struct Bar {} Bar_t; |
15458 | } |
15459 | |
15460 | TEST_F(FormatTest, SplitEmptyUnion) { |
15461 | FormatStyle Style = getLLVMStyle(); |
15462 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15463 | Style.BraceWrapping.AfterUnion = true; |
15464 | Style.BraceWrapping.SplitEmptyRecord = false; |
15465 | |
15466 | verifyFormat("union Foo\n" |
15467 | "{};", |
15468 | Style); |
15469 | verifyFormat("/* something */ union Foo\n" |
15470 | "{};", |
15471 | Style); |
15472 | verifyFormat("union Foo\n" |
15473 | "{\n" |
15474 | " A,\n" |
15475 | "};", |
15476 | Style); |
15477 | verifyFormat("typedef union Foo\n" |
15478 | "{\n" |
15479 | "} Foo_t;", |
15480 | Style); |
15481 | } |
15482 | |
15483 | TEST_F(FormatTest, SplitEmptyNamespace) { |
15484 | FormatStyle Style = getLLVMStyle(); |
15485 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15486 | Style.BraceWrapping.AfterNamespace = true; |
15487 | Style.BraceWrapping.SplitEmptyNamespace = false; |
15488 | |
15489 | verifyFormat("namespace Foo\n" |
15490 | "{};", |
15491 | Style); |
15492 | verifyFormat("/* something */ namespace Foo\n" |
15493 | "{};", |
15494 | Style); |
15495 | verifyFormat("inline namespace Foo\n" |
15496 | "{};", |
15497 | Style); |
15498 | verifyFormat("/* something */ inline namespace Foo\n" |
15499 | "{};", |
15500 | Style); |
15501 | verifyFormat("export namespace Foo\n" |
15502 | "{};", |
15503 | Style); |
15504 | verifyFormat("namespace Foo\n" |
15505 | "{\n" |
15506 | "void Bar();\n" |
15507 | "};", |
15508 | Style); |
15509 | } |
15510 | |
15511 | TEST_F(FormatTest, NeverMergeShortRecords) { |
15512 | FormatStyle Style = getLLVMStyle(); |
15513 | |
15514 | verifyFormat("class Foo {\n" |
15515 | " Foo();\n" |
15516 | "};", |
15517 | Style); |
15518 | verifyFormat("typedef class Foo {\n" |
15519 | " Foo();\n" |
15520 | "} Foo_t;", |
15521 | Style); |
15522 | verifyFormat("struct Foo {\n" |
15523 | " Foo();\n" |
15524 | "};", |
15525 | Style); |
15526 | verifyFormat("typedef struct Foo {\n" |
15527 | " Foo();\n" |
15528 | "} Foo_t;", |
15529 | Style); |
15530 | verifyFormat("union Foo {\n" |
15531 | " A,\n" |
15532 | "};", |
15533 | Style); |
15534 | verifyFormat("typedef union Foo {\n" |
15535 | " A,\n" |
15536 | "} Foo_t;", |
15537 | Style); |
15538 | verifyFormat("namespace Foo {\n" |
15539 | "void Bar();\n" |
15540 | "};", |
15541 | Style); |
15542 | |
15543 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
15544 | Style.BraceWrapping.AfterClass = true; |
15545 | Style.BraceWrapping.AfterStruct = true; |
15546 | Style.BraceWrapping.AfterUnion = true; |
15547 | Style.BraceWrapping.AfterNamespace = true; |
15548 | verifyFormat("class Foo\n" |
15549 | "{\n" |
15550 | " Foo();\n" |
15551 | "};", |
15552 | Style); |
15553 | verifyFormat("typedef class Foo\n" |
15554 | "{\n" |
15555 | " Foo();\n" |
15556 | "} Foo_t;", |
15557 | Style); |
15558 | verifyFormat("struct Foo\n" |
15559 | "{\n" |
15560 | " Foo();\n" |
15561 | "};", |
15562 | Style); |
15563 | verifyFormat("typedef struct Foo\n" |
15564 | "{\n" |
15565 | " Foo();\n" |
15566 | "} Foo_t;", |
15567 | Style); |
15568 | verifyFormat("union Foo\n" |
15569 | "{\n" |
15570 | " A,\n" |
15571 | "};", |
15572 | Style); |
15573 | verifyFormat("typedef union Foo\n" |
15574 | "{\n" |
15575 | " A,\n" |
15576 | "} Foo_t;", |
15577 | Style); |
15578 | verifyFormat("namespace Foo\n" |
15579 | "{\n" |
15580 | "void Bar();\n" |
15581 | "};", |
15582 | Style); |
15583 | } |
15584 | |
15585 | TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { |
15586 | // Elaborate type variable declarations. |
15587 | verifyFormat("struct foo a = {bar};\nint n;"); |
15588 | verifyFormat("class foo a = {bar};\nint n;"); |
15589 | verifyFormat("union foo a = {bar};\nint n;"); |
15590 | |
15591 | // Elaborate types inside function definitions. |
15592 | verifyFormat("struct foo f() {}\nint n;"); |
15593 | verifyFormat("class foo f() {}\nint n;"); |
15594 | verifyFormat("union foo f() {}\nint n;"); |
15595 | |
15596 | // Templates. |
15597 | verifyFormat("template <class X> void f() {}\nint n;"); |
15598 | verifyFormat("template <struct X> void f() {}\nint n;"); |
15599 | verifyFormat("template <union X> void f() {}\nint n;"); |
15600 | |
15601 | // Actual definitions... |
15602 | verifyFormat("struct {\n} n;"); |
15603 | verifyFormat( |
15604 | "template <template <class T, class Y>, class Z> class X {\n} n;"); |
15605 | verifyFormat("union Z {\n int n;\n} x;"); |
15606 | verifyFormat("class MACRO Z {\n} n;"); |
15607 | verifyFormat("class MACRO(X) Z {\n} n;"); |
15608 | verifyFormat("class __attribute__((X)) Z {\n} n;"); |
15609 | verifyFormat("class __declspec(X) Z {\n} n;"); |
15610 | verifyFormat("class A##B##C {\n} n;"); |
15611 | verifyFormat("class alignas(16) Z {\n} n;"); |
15612 | verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); |
15613 | verifyFormat("class MACROA MACRO(X) Z {\n} n;"); |
15614 | |
15615 | // Redefinition from nested context: |
15616 | verifyFormat("class A::B::C {\n} n;"); |
15617 | |
15618 | // Template definitions. |
15619 | verifyFormat( |
15620 | "template <typename F>\n" |
15621 | "Matcher(const Matcher<F> &Other,\n" |
15622 | " typename enable_if_c<is_base_of<F, T>::value &&\n" |
15623 | " !is_same<F, T>::value>::type * = 0)\n" |
15624 | " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); |
15625 | |
15626 | // FIXME: This is still incorrectly handled at the formatter side. |
15627 | verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); |
15628 | verifyFormat("int i = SomeFunction(a<b, a> b);"); |
15629 | |
15630 | verifyFormat("class A<int> f() {}\n" |
15631 | "int n;"); |
15632 | verifyFormat("template <typename T> class A<T> f() {}\n" |
15633 | "int n;"); |
15634 | |
15635 | verifyFormat("template <> class Foo<int> F() {\n" |
15636 | "} n;"); |
15637 | |
15638 | // Elaborate types where incorrectly parsing the structural element would |
15639 | // break the indent. |
15640 | verifyFormat("if (true)\n" |
15641 | " class X x;\n" |
15642 | "else\n" |
15643 | " f();"); |
15644 | |
15645 | // This is simply incomplete. Formatting is not important, but must not crash. |
15646 | verifyFormat("class A:"); |
15647 | } |
15648 | |
15649 | TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { |
15650 | verifyNoChange("#error Leave all white!!!!! space* alone!"); |
15651 | verifyNoChange("#warning Leave all white!!!!! space* alone!"); |
15652 | verifyFormat("#error 1", " # error 1"); |
15653 | verifyFormat("#warning 1", " # warning 1"); |
15654 | } |
15655 | |
15656 | TEST_F(FormatTest, FormatHashIfExpressions) { |
15657 | verifyFormat("#if AAAA && BBBB"); |
15658 | verifyFormat("#if (AAAA && BBBB)"); |
15659 | verifyFormat("#elif (AAAA && BBBB)"); |
15660 | // FIXME: Come up with a better indentation for #elif. |
15661 | verifyFormat( |
15662 | "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" |
15663 | " defined(BBBBBBBB)\n" |
15664 | "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" |
15665 | " defined(BBBBBBBB)\n" |
15666 | "#endif", |
15667 | getLLVMStyleWithColumns(65)); |
15668 | } |
15669 | |
15670 | TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { |
15671 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
15672 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = |
15673 | FormatStyle::SIS_WithoutElse; |
15674 | verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); |
15675 | verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); |
15676 | verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); |
15677 | verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf); |
15678 | FormatStyle ShortMergedIf = AllowsMergedIf; |
15679 | ShortMergedIf.ColumnLimit = 25; |
15680 | verifyFormat("#define A \\\n" |
15681 | " if (true) return 42;", |
15682 | ShortMergedIf); |
15683 | verifyFormat("#define A \\\n" |
15684 | " f(); \\\n" |
15685 | " if (true)\n" |
15686 | "#define B", |
15687 | ShortMergedIf); |
15688 | verifyFormat("#define A \\\n" |
15689 | " f(); \\\n" |
15690 | " if (true)\n" |
15691 | "g();", |
15692 | ShortMergedIf); |
15693 | verifyFormat("{\n" |
15694 | "#ifdef A\n" |
15695 | " // Comment\n" |
15696 | " if (true) continue;\n" |
15697 | "#endif\n" |
15698 | " // Comment\n" |
15699 | " if (true) continue;\n" |
15700 | "}", |
15701 | ShortMergedIf); |
15702 | ShortMergedIf.ColumnLimit = 33; |
15703 | verifyFormat("#define A \\\n" |
15704 | " if constexpr (true) return 42;", |
15705 | ShortMergedIf); |
15706 | verifyFormat("#define A \\\n" |
15707 | " if CONSTEXPR (true) return 42;", |
15708 | ShortMergedIf); |
15709 | ShortMergedIf.ColumnLimit = 29; |
15710 | verifyFormat("#define A \\\n" |
15711 | " if (aaaaaaaaaa) return 1; \\\n" |
15712 | " return 2;", |
15713 | ShortMergedIf); |
15714 | ShortMergedIf.ColumnLimit = 28; |
15715 | verifyFormat("#define A \\\n" |
15716 | " if (aaaaaaaaaa) \\\n" |
15717 | " return 1; \\\n" |
15718 | " return 2;", |
15719 | ShortMergedIf); |
15720 | verifyFormat("#define A \\\n" |
15721 | " if constexpr (aaaaaaa) \\\n" |
15722 | " return 1; \\\n" |
15723 | " return 2;", |
15724 | ShortMergedIf); |
15725 | verifyFormat("#define A \\\n" |
15726 | " if CONSTEXPR (aaaaaaa) \\\n" |
15727 | " return 1; \\\n" |
15728 | " return 2;", |
15729 | ShortMergedIf); |
15730 | |
15731 | verifyFormat("//\n" |
15732 | "#define a \\\n" |
15733 | " if \\\n" |
15734 | " 0", |
15735 | getChromiumStyle(FormatStyle::LK_Cpp)); |
15736 | } |
15737 | |
15738 | TEST_F(FormatTest, FormatStarDependingOnContext) { |
15739 | verifyFormat("void f(int *a);"); |
15740 | verifyFormat("void f() { f(fint * b); }"); |
15741 | verifyFormat("class A {\n void f(int *a);\n};"); |
15742 | verifyFormat("class A {\n int *a;\n};"); |
15743 | verifyFormat("namespace a {\n" |
15744 | "namespace b {\n" |
15745 | "class A {\n" |
15746 | " void f() {}\n" |
15747 | " int *a;\n" |
15748 | "};\n" |
15749 | "} // namespace b\n" |
15750 | "} // namespace a"); |
15751 | } |
15752 | |
15753 | TEST_F(FormatTest, SpecialTokensAtEndOfLine) { |
15754 | verifyFormat("while"); |
15755 | verifyFormat("operator"); |
15756 | } |
15757 | |
15758 | TEST_F(FormatTest, SkipsDeeplyNestedLines) { |
15759 | // This code would be painfully slow to format if we didn't skip it. |
15760 | std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"// 20x |
15761 | "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" |
15762 | "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" |
15763 | "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" |
15764 | "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" |
15765 | "A(1, 1)\n" |
15766 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"// 10x |
15767 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15768 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15769 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15770 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15771 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15772 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15773 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15774 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" |
15775 | ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); |
15776 | // Deeply nested part is untouched, rest is formatted. |
15777 | EXPECT_EQ(std::string("int i;") + Code + "int j;", |
15778 | format(std::string("int i;") + Code + "int j;", |
15779 | getLLVMStyle(), SC_ExpectIncomplete)); |
15780 | } |
15781 | |
15782 | //===----------------------------------------------------------------------===// |
15783 | // Objective-C tests. |
15784 | //===----------------------------------------------------------------------===// |
15785 | |
15786 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
15787 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
15788 | verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;", |
15789 | "-(NSUInteger)indexOfObject:(id)anObject;"); |
15790 | verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;"); |
15791 | verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;"); |
15792 | verifyFormat("- (NSInteger)Method3:(id)anObject;", |
15793 | "-(NSInteger)Method3:(id)anObject;"); |
15794 | verifyFormat("- (NSInteger)Method4:(id)anObject;", |
15795 | "-(NSInteger)Method4:(id)anObject;"); |
15796 | verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
15797 | "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"); |
15798 | verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"); |
15799 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject " |
15800 | "forAllCells:(BOOL)flag;"); |
15801 | |
15802 | // Very long objectiveC method declaration. |
15803 | verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" |
15804 | " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); |
15805 | verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" |
15806 | " inRange:(NSRange)range\n" |
15807 | " outRange:(NSRange)out_range\n" |
15808 | " outRange1:(NSRange)out_range1\n" |
15809 | " outRange2:(NSRange)out_range2\n" |
15810 | " outRange3:(NSRange)out_range3\n" |
15811 | " outRange4:(NSRange)out_range4\n" |
15812 | " outRange5:(NSRange)out_range5\n" |
15813 | " outRange6:(NSRange)out_range6\n" |
15814 | " outRange7:(NSRange)out_range7\n" |
15815 | " outRange8:(NSRange)out_range8\n" |
15816 | " outRange9:(NSRange)out_range9;"); |
15817 | |
15818 | // When the function name has to be wrapped. |
15819 | FormatStyle Style = getLLVMStyle(); |
15820 | // ObjC ignores IndentWrappedFunctionNames when wrapping methods |
15821 | // and always indents instead. |
15822 | Style.IndentWrappedFunctionNames = false; |
15823 | verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" |
15824 | " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" |
15825 | " anotherName:(NSString)bbbbbbbbbbbbbb {\n" |
15826 | "}", |
15827 | Style); |
15828 | Style.IndentWrappedFunctionNames = true; |
15829 | verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" |
15830 | " veryLooooooooooongName:(NSString)cccccccccccccc\n" |
15831 | " anotherName:(NSString)dddddddddddddd {\n" |
15832 | "}", |
15833 | Style); |
15834 | |
15835 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
15836 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
15837 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
15838 | // protocol lists (but not for template classes): |
15839 | // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
15840 | |
15841 | verifyFormat("- (int (*)())foo:(int (*)())f;"); |
15842 | verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); |
15843 | |
15844 | // If there's no return type (very rare in practice!), LLVM and Google style |
15845 | // agree. |
15846 | verifyFormat("- foo;"); |
15847 | verifyFormat("- foo:(int)f;"); |
15848 | verifyGoogleFormat("- foo:(int)foo;"); |
15849 | } |
15850 | |
15851 | TEST_F(FormatTest, BreaksStringLiterals) { |
15852 | // FIXME: unstable test case |
15853 | EXPECT_EQ("\"some text \"\n" |
15854 | "\"other\";", |
15855 | format("\"some text other\";", getLLVMStyleWithColumns(12))); |
15856 | // FIXME: unstable test case |
15857 | EXPECT_EQ("\"some text \"\n" |
15858 | "\"other\";", |
15859 | format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); |
15860 | verifyFormat("#define A \\\n" |
15861 | " \"some \" \\\n" |
15862 | " \"text \" \\\n" |
15863 | " \"other\";", |
15864 | "#define A \"some text other\";", getLLVMStyleWithColumns(12)); |
15865 | verifyFormat("#define A \\\n" |
15866 | " \"so \" \\\n" |
15867 | " \"text \" \\\n" |
15868 | " \"other\";", |
15869 | "#define A \"so text other\";", getLLVMStyleWithColumns(12)); |
15870 | |
15871 | verifyFormat("\"some text\"", getLLVMStyleWithColumns(1)); |
15872 | verifyFormat("\"some text\"", getLLVMStyleWithColumns(11)); |
15873 | // FIXME: unstable test case |
15874 | EXPECT_EQ("\"some \"\n" |
15875 | "\"text\"", |
15876 | format("\"some text\"", getLLVMStyleWithColumns(10))); |
15877 | // FIXME: unstable test case |
15878 | EXPECT_EQ("\"some \"\n" |
15879 | "\"text\"", |
15880 | format("\"some text\"", getLLVMStyleWithColumns(7))); |
15881 | // FIXME: unstable test case |
15882 | EXPECT_EQ("\"some\"\n" |
15883 | "\" tex\"\n" |
15884 | "\"t\"", |
15885 | format("\"some text\"", getLLVMStyleWithColumns(6))); |
15886 | // FIXME: unstable test case |
15887 | EXPECT_EQ("\"some\"\n" |
15888 | "\" tex\"\n" |
15889 | "\" and\"", |
15890 | format("\"some tex and\"", getLLVMStyleWithColumns(6))); |
15891 | // FIXME: unstable test case |
15892 | EXPECT_EQ("\"some\"\n" |
15893 | "\"/tex\"\n" |
15894 | "\"/and\"", |
15895 | format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); |
15896 | |
15897 | verifyFormat("variable =\n" |
15898 | " \"long string \"\n" |
15899 | " \"literal\";", |
15900 | "variable = \"long string literal\";", |
15901 | getLLVMStyleWithColumns(20)); |
15902 | |
15903 | verifyFormat("variable = f(\n" |
15904 | " \"long string \"\n" |
15905 | " \"literal\",\n" |
15906 | " short,\n" |
15907 | " loooooooooooooooooooong);", |
15908 | "variable = f(\"long string literal\", short, " |
15909 | "loooooooooooooooooooong);", |
15910 | getLLVMStyleWithColumns(20)); |
15911 | |
15912 | verifyFormat("f(g(\"long string \"\n" |
15913 | " \"literal\"),\n" |
15914 | " b);", |
15915 | "f(g(\"long string literal\"), b);", |
15916 | getLLVMStyleWithColumns(20)); |
15917 | verifyFormat("f(g(\"long string \"\n" |
15918 | " \"literal\",\n" |
15919 | " a),\n" |
15920 | " b);", |
15921 | "f(g(\"long string literal\", a), b);", |
15922 | getLLVMStyleWithColumns(20)); |
15923 | verifyFormat("f(\"one two\".split(\n" |
15924 | " variable));", |
15925 | "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)); |
15926 | verifyFormat("f(\"one two three four five six \"\n" |
15927 | " \"seven\".split(\n" |
15928 | " really_looooong_variable));", |
15929 | "f(\"one two three four five six seven\"." |
15930 | "split(really_looooong_variable));", |
15931 | getLLVMStyleWithColumns(33)); |
15932 | |
15933 | verifyFormat("f(\"some \"\n" |
15934 | " \"text\",\n" |
15935 | " other);", |
15936 | "f(\"some text\", other);", getLLVMStyleWithColumns(10)); |
15937 | |
15938 | // Only break as a last resort. |
15939 | verifyFormat( |
15940 | "aaaaaaaaaaaaaaaaaaaa(\n" |
15941 | " aaaaaaaaaaaaaaaaaaaa,\n" |
15942 | " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); |
15943 | |
15944 | // FIXME: unstable test case |
15945 | EXPECT_EQ("\"splitmea\"\n" |
15946 | "\"trandomp\"\n" |
15947 | "\"oint\"", |
15948 | format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); |
15949 | |
15950 | // FIXME: unstable test case |
15951 | EXPECT_EQ("\"split/\"\n" |
15952 | "\"pathat/\"\n" |
15953 | "\"slashes\"", |
15954 | format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); |
15955 | |
15956 | // FIXME: unstable test case |
15957 | EXPECT_EQ("\"split/\"\n" |
15958 | "\"pathat/\"\n" |
15959 | "\"slashes\"", |
15960 | format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); |
15961 | // FIXME: unstable test case |
15962 | EXPECT_EQ("\"split at \"\n" |
15963 | "\"spaces/at/\"\n" |
15964 | "\"slashes.at.any$\"\n" |
15965 | "\"non-alphanumeric%\"\n" |
15966 | "\"1111111111characte\"\n" |
15967 | "\"rs\"", |
15968 | format("\"split at " |
15969 | "spaces/at/" |
15970 | "slashes.at." |
15971 | "any$non-" |
15972 | "alphanumeric%" |
15973 | "1111111111characte" |
15974 | "rs\"", |
15975 | getLLVMStyleWithColumns(20))); |
15976 | |
15977 | // Verify that splitting the strings understands |
15978 | // Style::AlwaysBreakBeforeMultilineStrings. |
15979 | verifyFormat("aaaaaaaaaaaa(\n" |
15980 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" |
15981 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", |
15982 | "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " |
15983 | "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " |
15984 | "aaaaaaaaaaaaaaaaaaaaaa\");", |
15985 | getGoogleStyle()); |
15986 | verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
15987 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", |
15988 | "return \"aaaaaaaaaaaaaaaaaaaaaa " |
15989 | "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " |
15990 | "aaaaaaaaaaaaaaaaaaaaaa\";", |
15991 | getGoogleStyle()); |
15992 | verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
15993 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", |
15994 | "llvm::outs() << " |
15995 | "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" |
15996 | "aaaaaaaaaaaaaaaaaaa\";"); |
15997 | verifyFormat("ffff(\n" |
15998 | " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
15999 | " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", |
16000 | "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " |
16001 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", |
16002 | getGoogleStyle()); |
16003 | |
16004 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 12); |
16005 | Style.BreakStringLiterals = false; |
16006 | verifyFormat("\"some text other\";", Style); |
16007 | |
16008 | FormatStyle AlignLeft = getLLVMStyleWithColumns(ColumnLimit: 12); |
16009 | AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
16010 | verifyFormat("#define A \\\n" |
16011 | " \"some \" \\\n" |
16012 | " \"text \" \\\n" |
16013 | " \"other\";", |
16014 | "#define A \"some text other\";", AlignLeft); |
16015 | } |
16016 | |
16017 | TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { |
16018 | verifyFormat("C a = \"some more \"\n" |
16019 | " \"text\";", |
16020 | "C a = \"some more text\";", getLLVMStyleWithColumns(18)); |
16021 | } |
16022 | |
16023 | TEST_F(FormatTest, FullyRemoveEmptyLines) { |
16024 | FormatStyle NoEmptyLines = getLLVMStyleWithColumns(ColumnLimit: 80); |
16025 | NoEmptyLines.MaxEmptyLinesToKeep = 0; |
16026 | verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);", |
16027 | NoEmptyLines); |
16028 | } |
16029 | |
16030 | TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { |
16031 | // FIXME: unstable test case |
16032 | EXPECT_EQ( |
16033 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
16034 | "(\n" |
16035 | " \"x\t\");", |
16036 | format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
16037 | "aaaaaaa(" |
16038 | "\"x\t\");")); |
16039 | } |
16040 | |
16041 | TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { |
16042 | // FIXME: unstable test case |
16043 | EXPECT_EQ( |
16044 | "u8\"utf8 string \"\n" |
16045 | "u8\"literal\";", |
16046 | format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); |
16047 | // FIXME: unstable test case |
16048 | EXPECT_EQ( |
16049 | "u\"utf16 string \"\n" |
16050 | "u\"literal\";", |
16051 | format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); |
16052 | // FIXME: unstable test case |
16053 | EXPECT_EQ( |
16054 | "U\"utf32 string \"\n" |
16055 | "U\"literal\";", |
16056 | format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); |
16057 | // FIXME: unstable test case |
16058 | EXPECT_EQ("L\"wide string \"\n" |
16059 | "L\"literal\";", |
16060 | format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); |
16061 | verifyFormat("@\"NSString \"\n" |
16062 | "@\"literal\";", |
16063 | "@\"NSString literal\";", getGoogleStyleWithColumns(19)); |
16064 | verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); |
16065 | |
16066 | // This input makes clang-format try to split the incomplete unicode escape |
16067 | // sequence, which used to lead to a crasher. |
16068 | verifyNoCrash( |
16069 | Code: "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
16070 | Style: getLLVMStyleWithColumns(ColumnLimit: 60)); |
16071 | } |
16072 | |
16073 | TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { |
16074 | FormatStyle Style = getGoogleStyleWithColumns(ColumnLimit: 15); |
16075 | verifyFormat("R\"x(raw literal)x\";", Style); |
16076 | verifyFormat("uR\"x(raw literal)x\";", Style); |
16077 | verifyFormat("LR\"x(raw literal)x\";", Style); |
16078 | verifyFormat("UR\"x(raw literal)x\";", Style); |
16079 | verifyFormat("u8R\"x(raw literal)x\";", Style); |
16080 | } |
16081 | |
16082 | TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { |
16083 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20); |
16084 | // FIXME: unstable test case |
16085 | EXPECT_EQ( |
16086 | "_T(\"aaaaaaaaaaaaaa\")\n" |
16087 | "_T(\"aaaaaaaaaaaaaa\")\n" |
16088 | "_T(\"aaaaaaaaaaaa\")", |
16089 | format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); |
16090 | verifyFormat("f(x,\n" |
16091 | " _T(\"aaaaaaaaaaaa\")\n" |
16092 | " _T(\"aaa\"),\n" |
16093 | " z);", |
16094 | "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style); |
16095 | |
16096 | // FIXME: Handle embedded spaces in one iteration. |
16097 | // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" |
16098 | // "_T(\"aaaaaaaaaaaaa\")\n" |
16099 | // "_T(\"aaaaaaaaaaaaa\")\n" |
16100 | // "_T(\"a\")", |
16101 | // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", |
16102 | // getLLVMStyleWithColumns(20))); |
16103 | verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", |
16104 | " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style); |
16105 | verifyFormat("f(\n" |
16106 | "#if !TEST\n" |
16107 | " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" |
16108 | "#endif\n" |
16109 | ");", |
16110 | "f(\n" |
16111 | "#if !TEST\n" |
16112 | "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" |
16113 | "#endif\n" |
16114 | ");"); |
16115 | verifyFormat("f(\n" |
16116 | "\n" |
16117 | " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", |
16118 | "f(\n" |
16119 | "\n" |
16120 | "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"); |
16121 | // Regression test for accessing tokens past the end of a vector in the |
16122 | // TokenLexer. |
16123 | verifyNoCrash(Code: R"(_T( |
16124 | " |
16125 | ) |
16126 | )"); |
16127 | } |
16128 | |
16129 | TEST_F(FormatTest, BreaksStringLiteralOperands) { |
16130 | // In a function call with two operands, the second can be broken with no line |
16131 | // break before it. |
16132 | verifyFormat("func(a, \"long long \"\n" |
16133 | " \"long long\");", |
16134 | "func(a, \"long long long long\");", |
16135 | getLLVMStyleWithColumns(24)); |
16136 | // In a function call with three operands, the second must be broken with a |
16137 | // line break before it. |
16138 | verifyFormat("func(a,\n" |
16139 | " \"long long long \"\n" |
16140 | " \"long\",\n" |
16141 | " c);", |
16142 | "func(a, \"long long long long\", c);", |
16143 | getLLVMStyleWithColumns(24)); |
16144 | // In a function call with three operands, the third must be broken with a |
16145 | // line break before it. |
16146 | verifyFormat("func(a, b,\n" |
16147 | " \"long long long \"\n" |
16148 | " \"long\");", |
16149 | "func(a, b, \"long long long long\");", |
16150 | getLLVMStyleWithColumns(24)); |
16151 | // In a function call with three operands, both the second and the third must |
16152 | // be broken with a line break before them. |
16153 | verifyFormat("func(a,\n" |
16154 | " \"long long long \"\n" |
16155 | " \"long\",\n" |
16156 | " \"long long long \"\n" |
16157 | " \"long\");", |
16158 | "func(a, \"long long long long\", \"long long long long\");", |
16159 | getLLVMStyleWithColumns(24)); |
16160 | // In a chain of << with two operands, the second can be broken with no line |
16161 | // break before it. |
16162 | verifyFormat("a << \"line line \"\n" |
16163 | " \"line\";", |
16164 | "a << \"line line line\";", getLLVMStyleWithColumns(20)); |
16165 | // In a chain of << with three operands, the second can be broken with no line |
16166 | // break before it. |
16167 | verifyFormat("abcde << \"line \"\n" |
16168 | " \"line line\"\n" |
16169 | " << c;", |
16170 | "abcde << \"line line line\" << c;", |
16171 | getLLVMStyleWithColumns(20)); |
16172 | // In a chain of << with three operands, the third must be broken with a line |
16173 | // break before it. |
16174 | verifyFormat("a << b\n" |
16175 | " << \"line line \"\n" |
16176 | " \"line\";", |
16177 | "a << b << \"line line line\";", getLLVMStyleWithColumns(20)); |
16178 | // In a chain of << with three operands, the second can be broken with no line |
16179 | // break before it and the third must be broken with a line break before it. |
16180 | verifyFormat("abcd << \"line line \"\n" |
16181 | " \"line\"\n" |
16182 | " << \"line line \"\n" |
16183 | " \"line\";", |
16184 | "abcd << \"line line line\" << \"line line line\";", |
16185 | getLLVMStyleWithColumns(20)); |
16186 | // In a chain of binary operators with two operands, the second can be broken |
16187 | // with no line break before it. |
16188 | verifyFormat("abcd + \"line line \"\n" |
16189 | " \"line line\";", |
16190 | "abcd + \"line line line line\";", getLLVMStyleWithColumns(20)); |
16191 | // In a chain of binary operators with three operands, the second must be |
16192 | // broken with a line break before it. |
16193 | verifyFormat("abcd +\n" |
16194 | " \"line line \"\n" |
16195 | " \"line line\" +\n" |
16196 | " e;", |
16197 | "abcd + \"line line line line\" + e;", |
16198 | getLLVMStyleWithColumns(20)); |
16199 | // In a function call with two operands, with AlignAfterOpenBracket enabled, |
16200 | // the first must be broken with a line break before it. |
16201 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 25); |
16202 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
16203 | verifyFormat("someFunction(\n" |
16204 | " \"long long long \"\n" |
16205 | " \"long\",\n" |
16206 | " a);", |
16207 | "someFunction(\"long long long long\", a);", Style); |
16208 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
16209 | verifyFormat("someFunction(\n" |
16210 | " \"long long long \"\n" |
16211 | " \"long\",\n" |
16212 | " a\n" |
16213 | ");", |
16214 | Style); |
16215 | } |
16216 | |
16217 | TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { |
16218 | verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" |
16219 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" |
16220 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", |
16221 | "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" |
16222 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" |
16223 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"); |
16224 | } |
16225 | |
16226 | TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { |
16227 | verifyFormat("f(g(R\"x(raw literal)x\", a), b);", |
16228 | "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()); |
16229 | verifyFormat("fffffffffff(g(R\"x(\n" |
16230 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16231 | ")x\",\n" |
16232 | " a),\n" |
16233 | " b);", |
16234 | "fffffffffff(g(R\"x(\n" |
16235 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16236 | ")x\", a), b);", |
16237 | getGoogleStyleWithColumns(20)); |
16238 | verifyFormat("fffffffffff(\n" |
16239 | " g(R\"x(qqq\n" |
16240 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16241 | ")x\",\n" |
16242 | " a),\n" |
16243 | " b);", |
16244 | "fffffffffff(g(R\"x(qqq\n" |
16245 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16246 | ")x\", a), b);", |
16247 | getGoogleStyleWithColumns(20)); |
16248 | |
16249 | verifyNoChange("fffffffffff(R\"x(\n" |
16250 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16251 | ")x\");", |
16252 | getGoogleStyleWithColumns(20)); |
16253 | verifyFormat("fffffffffff(R\"x(\n" |
16254 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16255 | ")x\" + bbbbbb);", |
16256 | "fffffffffff(R\"x(\n" |
16257 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16258 | ")x\" + bbbbbb);", |
16259 | getGoogleStyleWithColumns(20)); |
16260 | verifyFormat("fffffffffff(\n" |
16261 | " R\"x(\n" |
16262 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16263 | ")x\" +\n" |
16264 | " bbbbbb);", |
16265 | "fffffffffff(\n" |
16266 | " R\"x(\n" |
16267 | "multiline raw string literal xxxxxxxxxxxxxx\n" |
16268 | ")x\" + bbbbbb);", |
16269 | getGoogleStyleWithColumns(20)); |
16270 | verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);", |
16271 | "fffffffffff(\n" |
16272 | " R\"(single line raw string)\" + bbbbbb);"); |
16273 | } |
16274 | |
16275 | TEST_F(FormatTest, SkipsUnknownStringLiterals) { |
16276 | verifyFormat("string a = \"unterminated;"); |
16277 | verifyFormat("function(\"unterminated,\n" |
16278 | " OtherParameter);", |
16279 | "function( \"unterminated,\n" |
16280 | " OtherParameter);"); |
16281 | } |
16282 | |
16283 | TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { |
16284 | FormatStyle Style = getLLVMStyle(); |
16285 | Style.Standard = FormatStyle::LS_Cpp03; |
16286 | verifyFormat("#define x(_a) printf(\"foo\" _a);", |
16287 | "#define x(_a) printf(\"foo\"_a);", Style); |
16288 | } |
16289 | |
16290 | TEST_F(FormatTest, CppLexVersion) { |
16291 | FormatStyle Style = getLLVMStyle(); |
16292 | // Formatting of x * y differs if x is a type. |
16293 | verifyFormat("void foo() { MACRO(a * b); }", Style); |
16294 | verifyFormat("void foo() { MACRO(int *b); }", Style); |
16295 | |
16296 | // LLVM style uses latest lexer. |
16297 | verifyFormat("void foo() { MACRO(char8_t *b); }", Style); |
16298 | Style.Standard = FormatStyle::LS_Cpp17; |
16299 | // But in c++17, char8_t isn't a keyword. |
16300 | verifyFormat("void foo() { MACRO(char8_t * b); }", Style); |
16301 | } |
16302 | |
16303 | TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } |
16304 | |
16305 | TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { |
16306 | verifyFormat("someFunction(\"aaabbbcccd\"\n" |
16307 | " \"ddeeefff\");", |
16308 | "someFunction(\"aaabbbcccdddeeefff\");", |
16309 | getLLVMStyleWithColumns(25)); |
16310 | verifyFormat("someFunction1234567890(\n" |
16311 | " \"aaabbbcccdddeeefff\");", |
16312 | "someFunction1234567890(\"aaabbbcccdddeeefff\");", |
16313 | getLLVMStyleWithColumns(26)); |
16314 | verifyFormat("someFunction1234567890(\n" |
16315 | " \"aaabbbcccdddeeeff\"\n" |
16316 | " \"f\");", |
16317 | "someFunction1234567890(\"aaabbbcccdddeeefff\");", |
16318 | getLLVMStyleWithColumns(25)); |
16319 | verifyFormat("someFunction1234567890(\n" |
16320 | " \"aaabbbcccdddeeeff\"\n" |
16321 | " \"f\");", |
16322 | "someFunction1234567890(\"aaabbbcccdddeeefff\");", |
16323 | getLLVMStyleWithColumns(24)); |
16324 | verifyFormat("someFunction(\n" |
16325 | " \"aaabbbcc ddde \"\n" |
16326 | " \"efff\");", |
16327 | "someFunction(\"aaabbbcc ddde efff\");", |
16328 | getLLVMStyleWithColumns(25)); |
16329 | verifyFormat("someFunction(\"aaabbbccc \"\n" |
16330 | " \"ddeeefff\");", |
16331 | "someFunction(\"aaabbbccc ddeeefff\");", |
16332 | getLLVMStyleWithColumns(25)); |
16333 | verifyFormat("someFunction1234567890(\n" |
16334 | " \"aaabb \"\n" |
16335 | " \"cccdddeeefff\");", |
16336 | "someFunction1234567890(\"aaabb cccdddeeefff\");", |
16337 | getLLVMStyleWithColumns(25)); |
16338 | verifyFormat("#define A \\\n" |
16339 | " string s = \\\n" |
16340 | " \"123456789\" \\\n" |
16341 | " \"0\"; \\\n" |
16342 | " int i;", |
16343 | "#define A string s = \"1234567890\"; int i;", |
16344 | getLLVMStyleWithColumns(20)); |
16345 | verifyFormat("someFunction(\n" |
16346 | " \"aaabbbcc \"\n" |
16347 | " \"dddeeefff\");", |
16348 | "someFunction(\"aaabbbcc dddeeefff\");", |
16349 | getLLVMStyleWithColumns(25)); |
16350 | } |
16351 | |
16352 | TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { |
16353 | verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3)); |
16354 | verifyFormat("\"\\\"", getLLVMStyleWithColumns(2)); |
16355 | // FIXME: unstable test case |
16356 | EXPECT_EQ("\"test\"\n" |
16357 | "\"\\n\"", |
16358 | format("\"test\\n\"", getLLVMStyleWithColumns(7))); |
16359 | // FIXME: unstable test case |
16360 | EXPECT_EQ("\"tes\\\\\"\n" |
16361 | "\"n\"", |
16362 | format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); |
16363 | // FIXME: unstable test case |
16364 | EXPECT_EQ("\"\\\\\\\\\"\n" |
16365 | "\"\\n\"", |
16366 | format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); |
16367 | verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7)); |
16368 | // FIXME: unstable test case |
16369 | EXPECT_EQ("\"\\uff01\"\n" |
16370 | "\"test\"", |
16371 | format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); |
16372 | verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)); |
16373 | // FIXME: unstable test case |
16374 | EXPECT_EQ("\"\\x000000000001\"\n" |
16375 | "\"next\"", |
16376 | format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); |
16377 | verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)); |
16378 | verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7)); |
16379 | // FIXME: unstable test case |
16380 | EXPECT_EQ("\"test\"\n" |
16381 | "\"\\000000\"\n" |
16382 | "\"000001\"", |
16383 | format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); |
16384 | // FIXME: unstable test case |
16385 | EXPECT_EQ("\"test\\000\"\n" |
16386 | "\"00000000\"\n" |
16387 | "\"1\"", |
16388 | format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); |
16389 | } |
16390 | |
16391 | TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { |
16392 | verifyFormat("void f() {\n" |
16393 | " return g() {}\n" |
16394 | " void h() {}"); |
16395 | verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" |
16396 | "g();\n" |
16397 | "}"); |
16398 | } |
16399 | |
16400 | TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { |
16401 | verifyFormat( |
16402 | "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); |
16403 | } |
16404 | |
16405 | TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { |
16406 | verifyFormat("class X {\n" |
16407 | " void f() {\n" |
16408 | " }\n" |
16409 | "};", |
16410 | getLLVMStyleWithColumns(12)); |
16411 | } |
16412 | |
16413 | TEST_F(FormatTest, ConfigurableIndentWidth) { |
16414 | FormatStyle EightIndent = getLLVMStyleWithColumns(ColumnLimit: 18); |
16415 | EightIndent.IndentWidth = 8; |
16416 | EightIndent.ContinuationIndentWidth = 8; |
16417 | verifyFormat("void f() {\n" |
16418 | " someFunction();\n" |
16419 | " if (true) {\n" |
16420 | " f();\n" |
16421 | " }\n" |
16422 | "}", |
16423 | EightIndent); |
16424 | verifyFormat("class X {\n" |
16425 | " void f() {\n" |
16426 | " }\n" |
16427 | "};", |
16428 | EightIndent); |
16429 | verifyFormat("int x[] = {\n" |
16430 | " call(),\n" |
16431 | " call()};", |
16432 | EightIndent); |
16433 | } |
16434 | |
16435 | TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { |
16436 | verifyFormat("double\n" |
16437 | "f();", |
16438 | getLLVMStyleWithColumns(8)); |
16439 | } |
16440 | |
16441 | TEST_F(FormatTest, ConfigurableUseOfTab) { |
16442 | FormatStyle Tab = getLLVMStyleWithColumns(ColumnLimit: 42); |
16443 | Tab.IndentWidth = 8; |
16444 | Tab.UseTab = FormatStyle::UT_Always; |
16445 | Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
16446 | |
16447 | verifyFormat("if (aaaaaaaa && // q\n" |
16448 | " bb)\t\t// w\n" |
16449 | "\t;", |
16450 | "if (aaaaaaaa &&// q\n" |
16451 | "bb)// w\n" |
16452 | ";", |
16453 | Tab); |
16454 | verifyFormat("if (aaa && bbb) // w\n" |
16455 | "\t;", |
16456 | "if(aaa&&bbb)// w\n" |
16457 | ";", |
16458 | Tab); |
16459 | |
16460 | verifyFormat("class X {\n" |
16461 | "\tvoid f() {\n" |
16462 | "\t\tsomeFunction(parameter1,\n" |
16463 | "\t\t\t parameter2);\n" |
16464 | "\t}\n" |
16465 | "};", |
16466 | Tab); |
16467 | verifyFormat("#define A \\\n" |
16468 | "\tvoid f() { \\\n" |
16469 | "\t\tsomeFunction( \\\n" |
16470 | "\t\t parameter1, \\\n" |
16471 | "\t\t parameter2); \\\n" |
16472 | "\t}", |
16473 | Tab); |
16474 | verifyFormat("int a;\t // x\n" |
16475 | "int bbbbbbbb; // x", |
16476 | Tab); |
16477 | |
16478 | FormatStyle TabAlignment = Tab; |
16479 | TabAlignment.AlignConsecutiveDeclarations.Enabled = true; |
16480 | TabAlignment.PointerAlignment = FormatStyle::PAS_Left; |
16481 | verifyFormat("unsigned long long big;\n" |
16482 | "char*\t\t ptr;", |
16483 | TabAlignment); |
16484 | TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; |
16485 | verifyFormat("unsigned long long big;\n" |
16486 | "char *\t\t ptr;", |
16487 | TabAlignment); |
16488 | TabAlignment.PointerAlignment = FormatStyle::PAS_Right; |
16489 | verifyFormat("unsigned long long big;\n" |
16490 | "char\t\t *ptr;", |
16491 | TabAlignment); |
16492 | |
16493 | Tab.TabWidth = 4; |
16494 | Tab.IndentWidth = 8; |
16495 | verifyFormat("class TabWidth4Indent8 {\n" |
16496 | "\t\tvoid f() {\n" |
16497 | "\t\t\t\tsomeFunction(parameter1,\n" |
16498 | "\t\t\t\t\t\t\t parameter2);\n" |
16499 | "\t\t}\n" |
16500 | "};", |
16501 | Tab); |
16502 | |
16503 | Tab.TabWidth = 4; |
16504 | Tab.IndentWidth = 4; |
16505 | verifyFormat("class TabWidth4Indent4 {\n" |
16506 | "\tvoid f() {\n" |
16507 | "\t\tsomeFunction(parameter1,\n" |
16508 | "\t\t\t\t\t parameter2);\n" |
16509 | "\t}\n" |
16510 | "};", |
16511 | Tab); |
16512 | |
16513 | Tab.TabWidth = 8; |
16514 | Tab.IndentWidth = 4; |
16515 | verifyFormat("class TabWidth8Indent4 {\n" |
16516 | " void f() {\n" |
16517 | "\tsomeFunction(parameter1,\n" |
16518 | "\t\t parameter2);\n" |
16519 | " }\n" |
16520 | "};", |
16521 | Tab); |
16522 | |
16523 | Tab.TabWidth = 8; |
16524 | Tab.IndentWidth = 8; |
16525 | verifyFormat("/*\n" |
16526 | "\t a\t\tcomment\n" |
16527 | "\t in multiple lines\n" |
16528 | " */", |
16529 | " /*\t \t \n" |
16530 | " \t \t a\t\tcomment\t \t\n" |
16531 | " \t \t in multiple lines\t\n" |
16532 | " \t */", |
16533 | Tab); |
16534 | |
16535 | TabAlignment.UseTab = FormatStyle::UT_ForIndentation; |
16536 | TabAlignment.PointerAlignment = FormatStyle::PAS_Left; |
16537 | verifyFormat("void f() {\n" |
16538 | "\tunsigned long long big;\n" |
16539 | "\tchar* ptr;\n" |
16540 | "}", |
16541 | TabAlignment); |
16542 | TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; |
16543 | verifyFormat("void f() {\n" |
16544 | "\tunsigned long long big;\n" |
16545 | "\tchar * ptr;\n" |
16546 | "}", |
16547 | TabAlignment); |
16548 | TabAlignment.PointerAlignment = FormatStyle::PAS_Right; |
16549 | verifyFormat("void f() {\n" |
16550 | "\tunsigned long long big;\n" |
16551 | "\tchar *ptr;\n" |
16552 | "}", |
16553 | TabAlignment); |
16554 | |
16555 | Tab.UseTab = FormatStyle::UT_ForIndentation; |
16556 | verifyFormat("{\n" |
16557 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16558 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16559 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16560 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16561 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16562 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16563 | "};", |
16564 | Tab); |
16565 | verifyFormat("enum AA {\n" |
16566 | "\ta1, // Force multiple lines\n" |
16567 | "\ta2,\n" |
16568 | "\ta3\n" |
16569 | "};", |
16570 | Tab); |
16571 | verifyFormat("if (aaaaaaaa && // q\n" |
16572 | " bb) // w\n" |
16573 | "\t;", |
16574 | "if (aaaaaaaa &&// q\n" |
16575 | "bb)// w\n" |
16576 | ";", |
16577 | Tab); |
16578 | verifyFormat("class X {\n" |
16579 | "\tvoid f() {\n" |
16580 | "\t\tsomeFunction(parameter1,\n" |
16581 | "\t\t parameter2);\n" |
16582 | "\t}\n" |
16583 | "};", |
16584 | Tab); |
16585 | verifyFormat("{\n" |
16586 | "\tQ(\n" |
16587 | "\t {\n" |
16588 | "\t\t int a;\n" |
16589 | "\t\t someFunction(aaaaaaaa,\n" |
16590 | "\t\t bbbbbbb);\n" |
16591 | "\t },\n" |
16592 | "\t p);\n" |
16593 | "}", |
16594 | Tab); |
16595 | verifyFormat("{\n" |
16596 | "\t/* aaaa\n" |
16597 | "\t bbbb */\n" |
16598 | "}", |
16599 | "{\n" |
16600 | "/* aaaa\n" |
16601 | " bbbb */\n" |
16602 | "}", |
16603 | Tab); |
16604 | verifyFormat("{\n" |
16605 | "\t/*\n" |
16606 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16607 | "\t bbbbbbbbbbbbb\n" |
16608 | "\t*/\n" |
16609 | "}", |
16610 | "{\n" |
16611 | "/*\n" |
16612 | " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16613 | "*/\n" |
16614 | "}", |
16615 | Tab); |
16616 | verifyFormat("{\n" |
16617 | "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16618 | "\t// bbbbbbbbbbbbb\n" |
16619 | "}", |
16620 | "{\n" |
16621 | "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16622 | "}", |
16623 | Tab); |
16624 | verifyFormat("{\n" |
16625 | "\t/*\n" |
16626 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16627 | "\t bbbbbbbbbbbbb\n" |
16628 | "\t*/\n" |
16629 | "}", |
16630 | "{\n" |
16631 | "\t/*\n" |
16632 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16633 | "\t*/\n" |
16634 | "}", |
16635 | Tab); |
16636 | verifyNoChange("{\n" |
16637 | "\t/*\n" |
16638 | "\n" |
16639 | "\t*/\n" |
16640 | "}", |
16641 | Tab); |
16642 | verifyNoChange("{\n" |
16643 | "\t/*\n" |
16644 | " asdf\n" |
16645 | "\t*/\n" |
16646 | "}", |
16647 | Tab); |
16648 | |
16649 | verifyFormat("void f() {\n" |
16650 | "\treturn true ? aaaaaaaaaaaaaaaaaa\n" |
16651 | "\t : bbbbbbbbbbbbbbbbbb\n" |
16652 | "}", |
16653 | Tab); |
16654 | FormatStyle TabNoBreak = Tab; |
16655 | TabNoBreak.BreakBeforeTernaryOperators = false; |
16656 | verifyFormat("void f() {\n" |
16657 | "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" |
16658 | "\t bbbbbbbbbbbbbbbbbb\n" |
16659 | "}", |
16660 | TabNoBreak); |
16661 | verifyFormat("void f() {\n" |
16662 | "\treturn true ?\n" |
16663 | "\t aaaaaaaaaaaaaaaaaaaa :\n" |
16664 | "\t bbbbbbbbbbbbbbbbbbbb\n" |
16665 | "}", |
16666 | TabNoBreak); |
16667 | |
16668 | Tab.UseTab = FormatStyle::UT_Never; |
16669 | verifyFormat("/*\n" |
16670 | " a\t\tcomment\n" |
16671 | " in multiple lines\n" |
16672 | " */", |
16673 | " /*\t \t \n" |
16674 | " \t \t a\t\tcomment\t \t\n" |
16675 | " \t \t in multiple lines\t\n" |
16676 | " \t */", |
16677 | Tab); |
16678 | verifyFormat("/* some\n" |
16679 | " comment */", |
16680 | " \t \t /* some\n" |
16681 | " \t \t comment */", |
16682 | Tab); |
16683 | verifyFormat("int a; /* some\n" |
16684 | " comment */", |
16685 | " \t \t int a; /* some\n" |
16686 | " \t \t comment */", |
16687 | Tab); |
16688 | |
16689 | verifyFormat("int a; /* some\n" |
16690 | "comment */", |
16691 | " \t \t int\ta; /* some\n" |
16692 | " \t \t comment */", |
16693 | Tab); |
16694 | verifyFormat("f(\"\t\t\"); /* some\n" |
16695 | " comment */", |
16696 | " \t \t f(\"\t\t\"); /* some\n" |
16697 | " \t \t comment */", |
16698 | Tab); |
16699 | verifyFormat("{\n" |
16700 | " /*\n" |
16701 | " * Comment\n" |
16702 | " */\n" |
16703 | " int i;\n" |
16704 | "}", |
16705 | "{\n" |
16706 | "\t/*\n" |
16707 | "\t * Comment\n" |
16708 | "\t */\n" |
16709 | "\t int i;\n" |
16710 | "}", |
16711 | Tab); |
16712 | |
16713 | Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; |
16714 | Tab.TabWidth = 8; |
16715 | Tab.IndentWidth = 8; |
16716 | verifyFormat("if (aaaaaaaa && // q\n" |
16717 | " bb) // w\n" |
16718 | "\t;", |
16719 | "if (aaaaaaaa &&// q\n" |
16720 | "bb)// w\n" |
16721 | ";", |
16722 | Tab); |
16723 | verifyFormat("if (aaa && bbb) // w\n" |
16724 | "\t;", |
16725 | "if(aaa&&bbb)// w\n" |
16726 | ";", |
16727 | Tab); |
16728 | verifyFormat("class X {\n" |
16729 | "\tvoid f() {\n" |
16730 | "\t\tsomeFunction(parameter1,\n" |
16731 | "\t\t\t parameter2);\n" |
16732 | "\t}\n" |
16733 | "};", |
16734 | Tab); |
16735 | verifyFormat("#define A \\\n" |
16736 | "\tvoid f() { \\\n" |
16737 | "\t\tsomeFunction( \\\n" |
16738 | "\t\t parameter1, \\\n" |
16739 | "\t\t parameter2); \\\n" |
16740 | "\t}", |
16741 | Tab); |
16742 | Tab.TabWidth = 4; |
16743 | Tab.IndentWidth = 8; |
16744 | verifyFormat("class TabWidth4Indent8 {\n" |
16745 | "\t\tvoid f() {\n" |
16746 | "\t\t\t\tsomeFunction(parameter1,\n" |
16747 | "\t\t\t\t\t\t\t parameter2);\n" |
16748 | "\t\t}\n" |
16749 | "};", |
16750 | Tab); |
16751 | Tab.TabWidth = 4; |
16752 | Tab.IndentWidth = 4; |
16753 | verifyFormat("class TabWidth4Indent4 {\n" |
16754 | "\tvoid f() {\n" |
16755 | "\t\tsomeFunction(parameter1,\n" |
16756 | "\t\t\t\t\t parameter2);\n" |
16757 | "\t}\n" |
16758 | "};", |
16759 | Tab); |
16760 | Tab.TabWidth = 8; |
16761 | Tab.IndentWidth = 4; |
16762 | verifyFormat("class TabWidth8Indent4 {\n" |
16763 | " void f() {\n" |
16764 | "\tsomeFunction(parameter1,\n" |
16765 | "\t\t parameter2);\n" |
16766 | " }\n" |
16767 | "};", |
16768 | Tab); |
16769 | Tab.TabWidth = 8; |
16770 | Tab.IndentWidth = 8; |
16771 | verifyFormat("/*\n" |
16772 | "\t a\t\tcomment\n" |
16773 | "\t in multiple lines\n" |
16774 | " */", |
16775 | " /*\t \t \n" |
16776 | " \t \t a\t\tcomment\t \t\n" |
16777 | " \t \t in multiple lines\t\n" |
16778 | " \t */", |
16779 | Tab); |
16780 | verifyFormat("{\n" |
16781 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16782 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16783 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16784 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16785 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16786 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
16787 | "};", |
16788 | Tab); |
16789 | verifyFormat("enum AA {\n" |
16790 | "\ta1, // Force multiple lines\n" |
16791 | "\ta2,\n" |
16792 | "\ta3\n" |
16793 | "};", |
16794 | Tab); |
16795 | verifyFormat("if (aaaaaaaa && // q\n" |
16796 | " bb) // w\n" |
16797 | "\t;", |
16798 | "if (aaaaaaaa &&// q\n" |
16799 | "bb)// w\n" |
16800 | ";", |
16801 | Tab); |
16802 | verifyFormat("class X {\n" |
16803 | "\tvoid f() {\n" |
16804 | "\t\tsomeFunction(parameter1,\n" |
16805 | "\t\t\t parameter2);\n" |
16806 | "\t}\n" |
16807 | "};", |
16808 | Tab); |
16809 | verifyFormat("{\n" |
16810 | "\tQ(\n" |
16811 | "\t {\n" |
16812 | "\t\t int a;\n" |
16813 | "\t\t someFunction(aaaaaaaa,\n" |
16814 | "\t\t\t\t bbbbbbb);\n" |
16815 | "\t },\n" |
16816 | "\t p);\n" |
16817 | "}", |
16818 | Tab); |
16819 | verifyFormat("{\n" |
16820 | "\t/* aaaa\n" |
16821 | "\t bbbb */\n" |
16822 | "}", |
16823 | "{\n" |
16824 | "/* aaaa\n" |
16825 | " bbbb */\n" |
16826 | "}", |
16827 | Tab); |
16828 | verifyFormat("{\n" |
16829 | "\t/*\n" |
16830 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16831 | "\t bbbbbbbbbbbbb\n" |
16832 | "\t*/\n" |
16833 | "}", |
16834 | "{\n" |
16835 | "/*\n" |
16836 | " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16837 | "*/\n" |
16838 | "}", |
16839 | Tab); |
16840 | verifyFormat("{\n" |
16841 | "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16842 | "\t// bbbbbbbbbbbbb\n" |
16843 | "}", |
16844 | "{\n" |
16845 | "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16846 | "}", |
16847 | Tab); |
16848 | verifyFormat("{\n" |
16849 | "\t/*\n" |
16850 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16851 | "\t bbbbbbbbbbbbb\n" |
16852 | "\t*/\n" |
16853 | "}", |
16854 | "{\n" |
16855 | "\t/*\n" |
16856 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16857 | "\t*/\n" |
16858 | "}", |
16859 | Tab); |
16860 | verifyNoChange("{\n" |
16861 | "\t/*\n" |
16862 | "\n" |
16863 | "\t*/\n" |
16864 | "}", |
16865 | Tab); |
16866 | verifyNoChange("{\n" |
16867 | "\t/*\n" |
16868 | " asdf\n" |
16869 | "\t*/\n" |
16870 | "}", |
16871 | Tab); |
16872 | verifyFormat("/* some\n" |
16873 | " comment */", |
16874 | " \t \t /* some\n" |
16875 | " \t \t comment */", |
16876 | Tab); |
16877 | verifyFormat("int a; /* some\n" |
16878 | " comment */", |
16879 | " \t \t int a; /* some\n" |
16880 | " \t \t comment */", |
16881 | Tab); |
16882 | verifyFormat("int a; /* some\n" |
16883 | "comment */", |
16884 | " \t \t int\ta; /* some\n" |
16885 | " \t \t comment */", |
16886 | Tab); |
16887 | verifyFormat("f(\"\t\t\"); /* some\n" |
16888 | " comment */", |
16889 | " \t \t f(\"\t\t\"); /* some\n" |
16890 | " \t \t comment */", |
16891 | Tab); |
16892 | verifyFormat("{\n" |
16893 | "\t/*\n" |
16894 | "\t * Comment\n" |
16895 | "\t */\n" |
16896 | "\tint i;\n" |
16897 | "}", |
16898 | "{\n" |
16899 | "\t/*\n" |
16900 | "\t * Comment\n" |
16901 | "\t */\n" |
16902 | "\t int i;\n" |
16903 | "}", |
16904 | Tab); |
16905 | Tab.TabWidth = 2; |
16906 | Tab.IndentWidth = 2; |
16907 | verifyFormat("{\n" |
16908 | "\t/* aaaa\n" |
16909 | "\t\t bbbb */\n" |
16910 | "}", |
16911 | "{\n" |
16912 | "/* aaaa\n" |
16913 | "\t bbbb */\n" |
16914 | "}", |
16915 | Tab); |
16916 | verifyFormat("{\n" |
16917 | "\t/*\n" |
16918 | "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
16919 | "\t\tbbbbbbbbbbbbb\n" |
16920 | "\t*/\n" |
16921 | "}", |
16922 | "{\n" |
16923 | "/*\n" |
16924 | "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
16925 | "*/\n" |
16926 | "}", |
16927 | Tab); |
16928 | Tab.AlignConsecutiveAssignments.Enabled = true; |
16929 | Tab.AlignConsecutiveDeclarations.Enabled = true; |
16930 | Tab.TabWidth = 4; |
16931 | Tab.IndentWidth = 4; |
16932 | verifyFormat("class Assign {\n" |
16933 | "\tvoid f() {\n" |
16934 | "\t\tint x = 123;\n" |
16935 | "\t\tint random = 4;\n" |
16936 | "\t\tstd::string alphabet =\n" |
16937 | "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" |
16938 | "\t}\n" |
16939 | "};", |
16940 | Tab); |
16941 | |
16942 | Tab.UseTab = FormatStyle::UT_AlignWithSpaces; |
16943 | Tab.TabWidth = 8; |
16944 | Tab.IndentWidth = 8; |
16945 | verifyFormat("if (aaaaaaaa && // q\n" |
16946 | " bb) // w\n" |
16947 | "\t;", |
16948 | "if (aaaaaaaa &&// q\n" |
16949 | "bb)// w\n" |
16950 | ";", |
16951 | Tab); |
16952 | verifyFormat("if (aaa && bbb) // w\n" |
16953 | "\t;", |
16954 | "if(aaa&&bbb)// w\n" |
16955 | ";", |
16956 | Tab); |
16957 | verifyFormat("class X {\n" |
16958 | "\tvoid f() {\n" |
16959 | "\t\tsomeFunction(parameter1,\n" |
16960 | "\t\t parameter2);\n" |
16961 | "\t}\n" |
16962 | "};", |
16963 | Tab); |
16964 | verifyFormat("#define A \\\n" |
16965 | "\tvoid f() { \\\n" |
16966 | "\t\tsomeFunction( \\\n" |
16967 | "\t\t parameter1, \\\n" |
16968 | "\t\t parameter2); \\\n" |
16969 | "\t}", |
16970 | Tab); |
16971 | Tab.TabWidth = 4; |
16972 | Tab.IndentWidth = 8; |
16973 | verifyFormat("class TabWidth4Indent8 {\n" |
16974 | "\t\tvoid f() {\n" |
16975 | "\t\t\t\tsomeFunction(parameter1,\n" |
16976 | "\t\t\t\t parameter2);\n" |
16977 | "\t\t}\n" |
16978 | "};", |
16979 | Tab); |
16980 | Tab.TabWidth = 4; |
16981 | Tab.IndentWidth = 4; |
16982 | verifyFormat("class TabWidth4Indent4 {\n" |
16983 | "\tvoid f() {\n" |
16984 | "\t\tsomeFunction(parameter1,\n" |
16985 | "\t\t parameter2);\n" |
16986 | "\t}\n" |
16987 | "};", |
16988 | Tab); |
16989 | Tab.TabWidth = 8; |
16990 | Tab.IndentWidth = 4; |
16991 | verifyFormat("class TabWidth8Indent4 {\n" |
16992 | " void f() {\n" |
16993 | "\tsomeFunction(parameter1,\n" |
16994 | "\t parameter2);\n" |
16995 | " }\n" |
16996 | "};", |
16997 | Tab); |
16998 | Tab.TabWidth = 8; |
16999 | Tab.IndentWidth = 8; |
17000 | verifyFormat("/*\n" |
17001 | " a\t\tcomment\n" |
17002 | " in multiple lines\n" |
17003 | " */", |
17004 | " /*\t \t \n" |
17005 | " \t \t a\t\tcomment\t \t\n" |
17006 | " \t \t in multiple lines\t\n" |
17007 | " \t */", |
17008 | Tab); |
17009 | verifyFormat("{\n" |
17010 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
17011 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
17012 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
17013 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
17014 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
17015 | "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" |
17016 | "};", |
17017 | Tab); |
17018 | verifyFormat("enum AA {\n" |
17019 | "\ta1, // Force multiple lines\n" |
17020 | "\ta2,\n" |
17021 | "\ta3\n" |
17022 | "};", |
17023 | Tab); |
17024 | verifyFormat("if (aaaaaaaa && // q\n" |
17025 | " bb) // w\n" |
17026 | "\t;", |
17027 | "if (aaaaaaaa &&// q\n" |
17028 | "bb)// w\n" |
17029 | ";", |
17030 | Tab); |
17031 | verifyFormat("class X {\n" |
17032 | "\tvoid f() {\n" |
17033 | "\t\tsomeFunction(parameter1,\n" |
17034 | "\t\t parameter2);\n" |
17035 | "\t}\n" |
17036 | "};", |
17037 | Tab); |
17038 | verifyFormat("{\n" |
17039 | "\tQ(\n" |
17040 | "\t {\n" |
17041 | "\t\t int a;\n" |
17042 | "\t\t someFunction(aaaaaaaa,\n" |
17043 | "\t\t bbbbbbb);\n" |
17044 | "\t },\n" |
17045 | "\t p);\n" |
17046 | "}", |
17047 | Tab); |
17048 | verifyFormat("{\n" |
17049 | "\t/* aaaa\n" |
17050 | "\t bbbb */\n" |
17051 | "}", |
17052 | "{\n" |
17053 | "/* aaaa\n" |
17054 | " bbbb */\n" |
17055 | "}", |
17056 | Tab); |
17057 | verifyFormat("{\n" |
17058 | "\t/*\n" |
17059 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
17060 | "\t bbbbbbbbbbbbb\n" |
17061 | "\t*/\n" |
17062 | "}", |
17063 | "{\n" |
17064 | "/*\n" |
17065 | " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
17066 | "*/\n" |
17067 | "}", |
17068 | Tab); |
17069 | verifyFormat("{\n" |
17070 | "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
17071 | "\t// bbbbbbbbbbbbb\n" |
17072 | "}", |
17073 | "{\n" |
17074 | "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
17075 | "}", |
17076 | Tab); |
17077 | verifyFormat("{\n" |
17078 | "\t/*\n" |
17079 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
17080 | "\t bbbbbbbbbbbbb\n" |
17081 | "\t*/\n" |
17082 | "}", |
17083 | "{\n" |
17084 | "\t/*\n" |
17085 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
17086 | "\t*/\n" |
17087 | "}", |
17088 | Tab); |
17089 | verifyNoChange("{\n" |
17090 | "\t/*\n" |
17091 | "\n" |
17092 | "\t*/\n" |
17093 | "}", |
17094 | Tab); |
17095 | verifyNoChange("{\n" |
17096 | "\t/*\n" |
17097 | " asdf\n" |
17098 | "\t*/\n" |
17099 | "}", |
17100 | Tab); |
17101 | verifyFormat("/* some\n" |
17102 | " comment */", |
17103 | " \t \t /* some\n" |
17104 | " \t \t comment */", |
17105 | Tab); |
17106 | verifyFormat("int a; /* some\n" |
17107 | " comment */", |
17108 | " \t \t int a; /* some\n" |
17109 | " \t \t comment */", |
17110 | Tab); |
17111 | verifyFormat("int a; /* some\n" |
17112 | "comment */", |
17113 | " \t \t int\ta; /* some\n" |
17114 | " \t \t comment */", |
17115 | Tab); |
17116 | verifyFormat("f(\"\t\t\"); /* some\n" |
17117 | " comment */", |
17118 | " \t \t f(\"\t\t\"); /* some\n" |
17119 | " \t \t comment */", |
17120 | Tab); |
17121 | verifyFormat("{\n" |
17122 | "\t/*\n" |
17123 | "\t * Comment\n" |
17124 | "\t */\n" |
17125 | "\tint i;\n" |
17126 | "}", |
17127 | "{\n" |
17128 | "\t/*\n" |
17129 | "\t * Comment\n" |
17130 | "\t */\n" |
17131 | "\t int i;\n" |
17132 | "}", |
17133 | Tab); |
17134 | Tab.TabWidth = 2; |
17135 | Tab.IndentWidth = 2; |
17136 | verifyFormat("{\n" |
17137 | "\t/* aaaa\n" |
17138 | "\t bbbb */\n" |
17139 | "}", |
17140 | "{\n" |
17141 | "/* aaaa\n" |
17142 | " bbbb */\n" |
17143 | "}", |
17144 | Tab); |
17145 | verifyFormat("{\n" |
17146 | "\t/*\n" |
17147 | "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
17148 | "\t bbbbbbbbbbbbb\n" |
17149 | "\t*/\n" |
17150 | "}", |
17151 | "{\n" |
17152 | "/*\n" |
17153 | " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" |
17154 | "*/\n" |
17155 | "}", |
17156 | Tab); |
17157 | Tab.AlignConsecutiveAssignments.Enabled = true; |
17158 | Tab.AlignConsecutiveDeclarations.Enabled = true; |
17159 | Tab.TabWidth = 4; |
17160 | Tab.IndentWidth = 4; |
17161 | verifyFormat("class Assign {\n" |
17162 | "\tvoid f() {\n" |
17163 | "\t\tint x = 123;\n" |
17164 | "\t\tint random = 4;\n" |
17165 | "\t\tstd::string alphabet =\n" |
17166 | "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" |
17167 | "\t}\n" |
17168 | "};", |
17169 | Tab); |
17170 | Tab.AlignOperands = FormatStyle::OAS_Align; |
17171 | verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" |
17172 | " cccccccccccccccccccc;", |
17173 | Tab); |
17174 | // no alignment |
17175 | verifyFormat("int aaaaaaaaaa =\n" |
17176 | "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", |
17177 | Tab); |
17178 | verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" |
17179 | " : bbbbbbbbbbbbbb ? 222222222222222\n" |
17180 | " : 333333333333333;", |
17181 | Tab); |
17182 | Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
17183 | Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; |
17184 | verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" |
17185 | " + cccccccccccccccccccc;", |
17186 | Tab); |
17187 | } |
17188 | |
17189 | TEST_F(FormatTest, ZeroTabWidth) { |
17190 | FormatStyle Tab = getLLVMStyleWithColumns(ColumnLimit: 42); |
17191 | Tab.IndentWidth = 8; |
17192 | Tab.UseTab = FormatStyle::UT_Never; |
17193 | Tab.TabWidth = 0; |
17194 | verifyFormat("void a() {\n" |
17195 | " // line starts with '\t'\n" |
17196 | "};", |
17197 | "void a(){\n" |
17198 | "\t// line starts with '\t'\n" |
17199 | "};", |
17200 | Tab); |
17201 | |
17202 | verifyFormat("void a() {\n" |
17203 | " // line starts with '\t'\n" |
17204 | "};", |
17205 | "void a(){\n" |
17206 | "\t\t// line starts with '\t'\n" |
17207 | "};", |
17208 | Tab); |
17209 | |
17210 | Tab.UseTab = FormatStyle::UT_ForIndentation; |
17211 | verifyFormat("void a() {\n" |
17212 | " // line starts with '\t'\n" |
17213 | "};", |
17214 | "void a(){\n" |
17215 | "\t// line starts with '\t'\n" |
17216 | "};", |
17217 | Tab); |
17218 | |
17219 | verifyFormat("void a() {\n" |
17220 | " // line starts with '\t'\n" |
17221 | "};", |
17222 | "void a(){\n" |
17223 | "\t\t// line starts with '\t'\n" |
17224 | "};", |
17225 | Tab); |
17226 | |
17227 | Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; |
17228 | verifyFormat("void a() {\n" |
17229 | " // line starts with '\t'\n" |
17230 | "};", |
17231 | "void a(){\n" |
17232 | "\t// line starts with '\t'\n" |
17233 | "};", |
17234 | Tab); |
17235 | |
17236 | verifyFormat("void a() {\n" |
17237 | " // line starts with '\t'\n" |
17238 | "};", |
17239 | "void a(){\n" |
17240 | "\t\t// line starts with '\t'\n" |
17241 | "};", |
17242 | Tab); |
17243 | |
17244 | Tab.UseTab = FormatStyle::UT_AlignWithSpaces; |
17245 | verifyFormat("void a() {\n" |
17246 | " // line starts with '\t'\n" |
17247 | "};", |
17248 | "void a(){\n" |
17249 | "\t// line starts with '\t'\n" |
17250 | "};", |
17251 | Tab); |
17252 | |
17253 | verifyFormat("void a() {\n" |
17254 | " // line starts with '\t'\n" |
17255 | "};", |
17256 | "void a(){\n" |
17257 | "\t\t// line starts with '\t'\n" |
17258 | "};", |
17259 | Tab); |
17260 | |
17261 | Tab.UseTab = FormatStyle::UT_Always; |
17262 | verifyFormat("void a() {\n" |
17263 | "// line starts with '\t'\n" |
17264 | "};", |
17265 | "void a(){\n" |
17266 | "\t// line starts with '\t'\n" |
17267 | "};", |
17268 | Tab); |
17269 | |
17270 | verifyFormat("void a() {\n" |
17271 | "// line starts with '\t'\n" |
17272 | "};", |
17273 | "void a(){\n" |
17274 | "\t\t// line starts with '\t'\n" |
17275 | "};", |
17276 | Tab); |
17277 | } |
17278 | |
17279 | TEST_F(FormatTest, CalculatesOriginalColumn) { |
17280 | verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" |
17281 | "q\"; /* some\n" |
17282 | " comment */", |
17283 | " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" |
17284 | "q\"; /* some\n" |
17285 | " comment */"); |
17286 | verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" |
17287 | "/* some\n" |
17288 | " comment */", |
17289 | "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" |
17290 | " /* some\n" |
17291 | " comment */"); |
17292 | verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" |
17293 | "qqq\n" |
17294 | "/* some\n" |
17295 | " comment */", |
17296 | "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" |
17297 | "qqq\n" |
17298 | " /* some\n" |
17299 | " comment */"); |
17300 | verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" |
17301 | "wwww; /* some\n" |
17302 | " comment */", |
17303 | " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" |
17304 | "wwww; /* some\n" |
17305 | " comment */"); |
17306 | } |
17307 | |
17308 | TEST_F(FormatTest, SpaceAfterOperatorKeyword) { |
17309 | auto SpaceAfterOperatorKeyword = getLLVMStyle(); |
17310 | SpaceAfterOperatorKeyword.SpaceAfterOperatorKeyword = true; |
17311 | verifyFormat("bool operator ++(int a);", SpaceAfterOperatorKeyword); |
17312 | } |
17313 | |
17314 | TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { |
17315 | FormatStyle NoSpace = getLLVMStyle(); |
17316 | NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; |
17317 | |
17318 | verifyFormat("while(true)\n" |
17319 | " continue;", |
17320 | NoSpace); |
17321 | verifyFormat("for(;;)\n" |
17322 | " continue;", |
17323 | NoSpace); |
17324 | verifyFormat("if(true)\n" |
17325 | " f();\n" |
17326 | "else if(true)\n" |
17327 | " f();", |
17328 | NoSpace); |
17329 | verifyFormat("do {\n" |
17330 | " do_something();\n" |
17331 | "} while(something());", |
17332 | NoSpace); |
17333 | verifyFormat("switch(x) {\n" |
17334 | "default:\n" |
17335 | " break;\n" |
17336 | "}", |
17337 | NoSpace); |
17338 | verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); |
17339 | verifyFormat("size_t x = sizeof(x);", NoSpace); |
17340 | verifyFormat("auto f(int x) -> decltype(x);", NoSpace); |
17341 | verifyFormat("auto f(int x) -> typeof(x);", NoSpace); |
17342 | verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); |
17343 | verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); |
17344 | verifyFormat("int f(T x) noexcept(x.create());", NoSpace); |
17345 | verifyFormat("alignas(128) char a[128];", NoSpace); |
17346 | verifyFormat("size_t x = alignof(MyType);", NoSpace); |
17347 | verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); |
17348 | verifyFormat("int f() throw(Deprecated);", NoSpace); |
17349 | verifyFormat("typedef void (*cb)(int);", NoSpace); |
17350 | verifyFormat("T A::operator()();", NoSpace); |
17351 | verifyFormat("X A::operator++(T);", NoSpace); |
17352 | verifyFormat("auto lambda = []() { return 0; };", NoSpace); |
17353 | verifyFormat("#if (foo || bar) && baz\n" |
17354 | "#elif ((a || b) && c) || d\n" |
17355 | "#endif", |
17356 | NoSpace); |
17357 | |
17358 | FormatStyle Space = getLLVMStyle(); |
17359 | Space.SpaceBeforeParens = FormatStyle::SBPO_Always; |
17360 | |
17361 | verifyFormat("int f ();", Space); |
17362 | verifyFormat("bool operator< ();", Space); |
17363 | verifyFormat("bool operator> ();", Space); |
17364 | verifyFormat("void f (int a, T b) {\n" |
17365 | " while (true)\n" |
17366 | " continue;\n" |
17367 | "}", |
17368 | Space); |
17369 | verifyFormat("if (true)\n" |
17370 | " f ();\n" |
17371 | "else if (true)\n" |
17372 | " f ();", |
17373 | Space); |
17374 | verifyFormat("do {\n" |
17375 | " do_something ();\n" |
17376 | "} while (something ());", |
17377 | Space); |
17378 | verifyFormat("switch (x) {\n" |
17379 | "default:\n" |
17380 | " break;\n" |
17381 | "}", |
17382 | Space); |
17383 | verifyFormat("A::A () : a (1) {}", Space); |
17384 | verifyFormat("void f () __attribute__ ((asdf));", Space); |
17385 | verifyFormat("*(&a + 1);\n" |
17386 | "&((&a)[1]);\n" |
17387 | "a[(b + c) * d];\n" |
17388 | "(((a + 1) * 2) + 3) * 4;", |
17389 | Space); |
17390 | verifyFormat("#define A(x) x", Space); |
17391 | verifyFormat("#define A (x) x", Space); |
17392 | verifyFormat("#if defined(x)\n" |
17393 | "#endif", |
17394 | Space); |
17395 | verifyFormat("auto i = std::make_unique<int> (5);", Space); |
17396 | verifyFormat("size_t x = sizeof (x);", Space); |
17397 | verifyFormat("auto f (int x) -> decltype (x);", Space); |
17398 | verifyFormat("auto f (int x) -> typeof (x);", Space); |
17399 | verifyFormat("auto f (int x) -> _Atomic (x);", Space); |
17400 | verifyFormat("auto f (int x) -> __underlying_type (x);", Space); |
17401 | verifyFormat("int f (T x) noexcept (x.create ());", Space); |
17402 | verifyFormat("alignas (128) char a[128];", Space); |
17403 | verifyFormat("size_t x = alignof (MyType);", Space); |
17404 | verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); |
17405 | verifyFormat("int f () throw (Deprecated);", Space); |
17406 | verifyFormat("typedef void (*cb) (int);", Space); |
17407 | verifyFormat("T A::operator() ();", Space); |
17408 | verifyFormat("X A::operator++ (T);", Space); |
17409 | verifyFormat("auto lambda = [] () { return 0; };", Space); |
17410 | verifyFormat("int x = int (y);", Space); |
17411 | verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space); |
17412 | verifyFormat("__builtin_LINE ()", Space); |
17413 | verifyFormat("__builtin_UNKNOWN ()", Space); |
17414 | |
17415 | FormatStyle SomeSpace = getLLVMStyle(); |
17416 | SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; |
17417 | |
17418 | verifyFormat("[]() -> float {}", SomeSpace); |
17419 | verifyFormat("[] (auto foo) {}", SomeSpace); |
17420 | verifyFormat("[foo]() -> int {}", SomeSpace); |
17421 | verifyFormat("int f();", SomeSpace); |
17422 | verifyFormat("void f (int a, T b) {\n" |
17423 | " while (true)\n" |
17424 | " continue;\n" |
17425 | "}", |
17426 | SomeSpace); |
17427 | verifyFormat("if (true)\n" |
17428 | " f();\n" |
17429 | "else if (true)\n" |
17430 | " f();", |
17431 | SomeSpace); |
17432 | verifyFormat("do {\n" |
17433 | " do_something();\n" |
17434 | "} while (something());", |
17435 | SomeSpace); |
17436 | verifyFormat("switch (x) {\n" |
17437 | "default:\n" |
17438 | " break;\n" |
17439 | "}", |
17440 | SomeSpace); |
17441 | verifyFormat("A::A() : a (1) {}", SomeSpace); |
17442 | verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); |
17443 | verifyFormat("*(&a + 1);\n" |
17444 | "&((&a)[1]);\n" |
17445 | "a[(b + c) * d];\n" |
17446 | "(((a + 1) * 2) + 3) * 4;", |
17447 | SomeSpace); |
17448 | verifyFormat("#define A(x) x", SomeSpace); |
17449 | verifyFormat("#define A (x) x", SomeSpace); |
17450 | verifyFormat("#if defined(x)\n" |
17451 | "#endif", |
17452 | SomeSpace); |
17453 | verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); |
17454 | verifyFormat("size_t x = sizeof (x);", SomeSpace); |
17455 | verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); |
17456 | verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); |
17457 | verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); |
17458 | verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); |
17459 | verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); |
17460 | verifyFormat("alignas (128) char a[128];", SomeSpace); |
17461 | verifyFormat("size_t x = alignof (MyType);", SomeSpace); |
17462 | verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", |
17463 | SomeSpace); |
17464 | verifyFormat("int f() throw (Deprecated);", SomeSpace); |
17465 | verifyFormat("typedef void (*cb) (int);", SomeSpace); |
17466 | verifyFormat("T A::operator()();", SomeSpace); |
17467 | verifyFormat("X A::operator++ (T);", SomeSpace); |
17468 | verifyFormat("int x = int (y);", SomeSpace); |
17469 | verifyFormat("auto lambda = []() { return 0; };", SomeSpace); |
17470 | |
17471 | FormatStyle SpaceControlStatements = getLLVMStyle(); |
17472 | SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17473 | SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; |
17474 | |
17475 | verifyFormat("while (true)\n" |
17476 | " continue;", |
17477 | SpaceControlStatements); |
17478 | verifyFormat("if (true)\n" |
17479 | " f();\n" |
17480 | "else if (true)\n" |
17481 | " f();", |
17482 | SpaceControlStatements); |
17483 | verifyFormat("for (;;) {\n" |
17484 | " do_something();\n" |
17485 | "}", |
17486 | SpaceControlStatements); |
17487 | verifyFormat("do {\n" |
17488 | " do_something();\n" |
17489 | "} while (something());", |
17490 | SpaceControlStatements); |
17491 | verifyFormat("switch (x) {\n" |
17492 | "default:\n" |
17493 | " break;\n" |
17494 | "}", |
17495 | SpaceControlStatements); |
17496 | |
17497 | FormatStyle SpaceFuncDecl = getLLVMStyle(); |
17498 | SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17499 | SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; |
17500 | |
17501 | verifyFormat("int f ();", SpaceFuncDecl); |
17502 | verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); |
17503 | verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl); |
17504 | verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); |
17505 | verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); |
17506 | verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl); |
17507 | verifyFormat("#define A(x) x", SpaceFuncDecl); |
17508 | verifyFormat("#define A (x) x", SpaceFuncDecl); |
17509 | verifyFormat("#if defined(x)\n" |
17510 | "#endif", |
17511 | SpaceFuncDecl); |
17512 | verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); |
17513 | verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); |
17514 | verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); |
17515 | verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); |
17516 | verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); |
17517 | verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); |
17518 | verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); |
17519 | verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); |
17520 | verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); |
17521 | verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", |
17522 | SpaceFuncDecl); |
17523 | verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); |
17524 | verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); |
17525 | verifyFormat("T A::operator()();", SpaceFuncDecl); |
17526 | verifyFormat("X A::operator++(T);", SpaceFuncDecl); |
17527 | verifyFormat("T A::operator()() {}", SpaceFuncDecl); |
17528 | verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); |
17529 | verifyFormat("int x = int(y);", SpaceFuncDecl); |
17530 | verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", |
17531 | SpaceFuncDecl); |
17532 | |
17533 | FormatStyle SpaceFuncDef = getLLVMStyle(); |
17534 | SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17535 | SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; |
17536 | |
17537 | verifyFormat("int f();", SpaceFuncDef); |
17538 | verifyFormat("void f (int a, T b) {}", SpaceFuncDef); |
17539 | verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef); |
17540 | verifyFormat("A::A () : a(1) {}", SpaceFuncDef); |
17541 | verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); |
17542 | verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef); |
17543 | verifyFormat("#define A(x) x", SpaceFuncDef); |
17544 | verifyFormat("#define A (x) x", SpaceFuncDef); |
17545 | verifyFormat("#if defined(x)\n" |
17546 | "#endif", |
17547 | SpaceFuncDef); |
17548 | verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); |
17549 | verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); |
17550 | verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); |
17551 | verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); |
17552 | verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); |
17553 | verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); |
17554 | verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); |
17555 | verifyFormat("alignas(128) char a[128];", SpaceFuncDef); |
17556 | verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); |
17557 | verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", |
17558 | SpaceFuncDef); |
17559 | verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); |
17560 | verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); |
17561 | verifyFormat("T A::operator()();", SpaceFuncDef); |
17562 | verifyFormat("X A::operator++(T);", SpaceFuncDef); |
17563 | verifyFormat("T A::operator()() {}", SpaceFuncDef); |
17564 | verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); |
17565 | verifyFormat("int x = int(y);", SpaceFuncDef); |
17566 | verifyFormat("void foo::bar () {}", SpaceFuncDef); |
17567 | verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}", |
17568 | SpaceFuncDef); |
17569 | |
17570 | FormatStyle SpaceIfMacros = getLLVMStyle(); |
17571 | SpaceIfMacros.IfMacros.clear(); |
17572 | SpaceIfMacros.IfMacros.push_back(x: "MYIF"); |
17573 | SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17574 | SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; |
17575 | verifyFormat("MYIF (a)\n return;", SpaceIfMacros); |
17576 | verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); |
17577 | verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); |
17578 | |
17579 | FormatStyle SpaceForeachMacros = getLLVMStyle(); |
17580 | EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, |
17581 | FormatStyle::SBS_Never); |
17582 | EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); |
17583 | SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17584 | SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; |
17585 | verifyFormat("for (;;) {\n" |
17586 | "}", |
17587 | SpaceForeachMacros); |
17588 | verifyFormat("foreach (Item *item, itemlist) {\n" |
17589 | "}", |
17590 | SpaceForeachMacros); |
17591 | verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" |
17592 | "}", |
17593 | SpaceForeachMacros); |
17594 | verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" |
17595 | "}", |
17596 | SpaceForeachMacros); |
17597 | verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); |
17598 | |
17599 | FormatStyle SomeSpace2 = getLLVMStyle(); |
17600 | SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17601 | SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; |
17602 | verifyFormat("[]() -> float {}", SomeSpace2); |
17603 | verifyFormat("[] (auto foo) {}", SomeSpace2); |
17604 | verifyFormat("[foo]() -> int {}", SomeSpace2); |
17605 | verifyFormat("int f();", SomeSpace2); |
17606 | verifyFormat("void f (int a, T b) {\n" |
17607 | " while (true)\n" |
17608 | " continue;\n" |
17609 | "}", |
17610 | SomeSpace2); |
17611 | verifyFormat("if (true)\n" |
17612 | " f();\n" |
17613 | "else if (true)\n" |
17614 | " f();", |
17615 | SomeSpace2); |
17616 | verifyFormat("do {\n" |
17617 | " do_something();\n" |
17618 | "} while (something());", |
17619 | SomeSpace2); |
17620 | verifyFormat("switch (x) {\n" |
17621 | "default:\n" |
17622 | " break;\n" |
17623 | "}", |
17624 | SomeSpace2); |
17625 | verifyFormat("A::A() : a (1) {}", SomeSpace2); |
17626 | verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); |
17627 | verifyFormat("*(&a + 1);\n" |
17628 | "&((&a)[1]);\n" |
17629 | "a[(b + c) * d];\n" |
17630 | "(((a + 1) * 2) + 3) * 4;", |
17631 | SomeSpace2); |
17632 | verifyFormat("#define A(x) x", SomeSpace2); |
17633 | verifyFormat("#define A (x) x", SomeSpace2); |
17634 | verifyFormat("#if defined(x)\n" |
17635 | "#endif", |
17636 | SomeSpace2); |
17637 | verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); |
17638 | verifyFormat("size_t x = sizeof (x);", SomeSpace2); |
17639 | verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); |
17640 | verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); |
17641 | verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); |
17642 | verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); |
17643 | verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); |
17644 | verifyFormat("alignas (128) char a[128];", SomeSpace2); |
17645 | verifyFormat("size_t x = alignof (MyType);", SomeSpace2); |
17646 | verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", |
17647 | SomeSpace2); |
17648 | verifyFormat("int f() throw (Deprecated);", SomeSpace2); |
17649 | verifyFormat("typedef void (*cb) (int);", SomeSpace2); |
17650 | verifyFormat("T A::operator()();", SomeSpace2); |
17651 | verifyFormat("X A::operator++ (T);", SomeSpace2); |
17652 | verifyFormat("int x = int (y);", SomeSpace2); |
17653 | verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); |
17654 | |
17655 | FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); |
17656 | SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17657 | SpaceAfterOverloadedOperator.SpaceBeforeParensOptions |
17658 | .AfterOverloadedOperator = true; |
17659 | |
17660 | verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); |
17661 | verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); |
17662 | verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); |
17663 | verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); |
17664 | |
17665 | SpaceAfterOverloadedOperator.SpaceBeforeParensOptions |
17666 | .AfterOverloadedOperator = false; |
17667 | |
17668 | verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); |
17669 | verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); |
17670 | verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); |
17671 | verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); |
17672 | |
17673 | auto SpaceAfterRequires = getLLVMStyle(); |
17674 | SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom; |
17675 | EXPECT_FALSE( |
17676 | SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause); |
17677 | EXPECT_FALSE( |
17678 | SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression); |
17679 | verifyFormat("void f(auto x)\n" |
17680 | " requires requires(int i) { x + i; }\n" |
17681 | "{}", |
17682 | SpaceAfterRequires); |
17683 | verifyFormat("void f(auto x)\n" |
17684 | " requires(requires(int i) { x + i; })\n" |
17685 | "{}", |
17686 | SpaceAfterRequires); |
17687 | verifyFormat("if (requires(int i) { x + i; })\n" |
17688 | " return;", |
17689 | SpaceAfterRequires); |
17690 | verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); |
17691 | verifyFormat("template <typename T>\n" |
17692 | " requires(Foo<T>)\n" |
17693 | "class Bar;", |
17694 | SpaceAfterRequires); |
17695 | |
17696 | SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; |
17697 | verifyFormat("void f(auto x)\n" |
17698 | " requires requires(int i) { x + i; }\n" |
17699 | "{}", |
17700 | SpaceAfterRequires); |
17701 | verifyFormat("void f(auto x)\n" |
17702 | " requires (requires(int i) { x + i; })\n" |
17703 | "{}", |
17704 | SpaceAfterRequires); |
17705 | verifyFormat("if (requires(int i) { x + i; })\n" |
17706 | " return;", |
17707 | SpaceAfterRequires); |
17708 | verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); |
17709 | verifyFormat("template <typename T>\n" |
17710 | " requires (Foo<T>)\n" |
17711 | "class Bar;", |
17712 | SpaceAfterRequires); |
17713 | |
17714 | SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false; |
17715 | SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true; |
17716 | verifyFormat("void f(auto x)\n" |
17717 | " requires requires (int i) { x + i; }\n" |
17718 | "{}", |
17719 | SpaceAfterRequires); |
17720 | verifyFormat("void f(auto x)\n" |
17721 | " requires(requires (int i) { x + i; })\n" |
17722 | "{}", |
17723 | SpaceAfterRequires); |
17724 | verifyFormat("if (requires (int i) { x + i; })\n" |
17725 | " return;", |
17726 | SpaceAfterRequires); |
17727 | verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); |
17728 | verifyFormat("template <typename T>\n" |
17729 | " requires(Foo<T>)\n" |
17730 | "class Bar;", |
17731 | SpaceAfterRequires); |
17732 | |
17733 | SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; |
17734 | verifyFormat("void f(auto x)\n" |
17735 | " requires requires (int i) { x + i; }\n" |
17736 | "{}", |
17737 | SpaceAfterRequires); |
17738 | verifyFormat("void f(auto x)\n" |
17739 | " requires (requires (int i) { x + i; })\n" |
17740 | "{}", |
17741 | SpaceAfterRequires); |
17742 | verifyFormat("if (requires (int i) { x + i; })\n" |
17743 | " return;", |
17744 | SpaceAfterRequires); |
17745 | verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); |
17746 | verifyFormat("template <typename T>\n" |
17747 | " requires (Foo<T>)\n" |
17748 | "class Bar;", |
17749 | SpaceAfterRequires); |
17750 | } |
17751 | |
17752 | TEST_F(FormatTest, SpaceAfterLogicalNot) { |
17753 | FormatStyle Spaces = getLLVMStyle(); |
17754 | Spaces.SpaceAfterLogicalNot = true; |
17755 | |
17756 | verifyFormat("bool x = ! y", Spaces); |
17757 | verifyFormat("if (! isFailure())", Spaces); |
17758 | verifyFormat("if (! (a && b))", Spaces); |
17759 | verifyFormat("\"Error!\"", Spaces); |
17760 | verifyFormat("! ! x", Spaces); |
17761 | } |
17762 | |
17763 | TEST_F(FormatTest, ConfigurableSpacesInParens) { |
17764 | FormatStyle Spaces = getLLVMStyle(); |
17765 | |
17766 | verifyFormat("do_something(::globalVar);", Spaces); |
17767 | verifyFormat("call(x, y, z);", Spaces); |
17768 | verifyFormat("call();", Spaces); |
17769 | verifyFormat("std::function<void(int, int)> callback;", Spaces); |
17770 | verifyFormat("void inFunction() { std::function<void(int, int)> fct; }", |
17771 | Spaces); |
17772 | verifyFormat("while ((bool)1)\n" |
17773 | " continue;", |
17774 | Spaces); |
17775 | verifyFormat("for (;;)\n" |
17776 | " continue;", |
17777 | Spaces); |
17778 | verifyFormat("if (true)\n" |
17779 | " f();\n" |
17780 | "else if (true)\n" |
17781 | " f();", |
17782 | Spaces); |
17783 | verifyFormat("do {\n" |
17784 | " do_something((int)i);\n" |
17785 | "} while (something());", |
17786 | Spaces); |
17787 | verifyFormat("switch (x) {\n" |
17788 | "default:\n" |
17789 | " break;\n" |
17790 | "}", |
17791 | Spaces); |
17792 | verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); |
17793 | verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); |
17794 | verifyFormat("void f() __attribute__((asdf));", Spaces); |
17795 | verifyFormat("x = (int32)y;", Spaces); |
17796 | verifyFormat("y = ((int (*)(int))foo)(x);", Spaces); |
17797 | verifyFormat("decltype(x) y = 42;", Spaces); |
17798 | verifyFormat("decltype((x)) y = z;", Spaces); |
17799 | verifyFormat("decltype((foo())) a = foo();", Spaces); |
17800 | verifyFormat("decltype((bar(10))) a = bar(11);", Spaces); |
17801 | verifyFormat("if ((x - y) && (a ^ b))\n" |
17802 | " f();", |
17803 | Spaces); |
17804 | verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n" |
17805 | " foo(i);", |
17806 | Spaces); |
17807 | verifyFormat("switch (x / (y + z)) {\n" |
17808 | "default:\n" |
17809 | " break;\n" |
17810 | "}", |
17811 | Spaces); |
17812 | |
17813 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
17814 | Spaces.SpacesInParensOptions = {}; |
17815 | Spaces.SpacesInParensOptions.Other = true; |
17816 | |
17817 | EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements); |
17818 | verifyFormat("if (a)\n" |
17819 | " return;", |
17820 | Spaces); |
17821 | |
17822 | Spaces.SpacesInParensOptions.InConditionalStatements = true; |
17823 | verifyFormat("do_something( ::globalVar );", Spaces); |
17824 | verifyFormat("call( x, y, z );", Spaces); |
17825 | verifyFormat("call();", Spaces); |
17826 | verifyFormat("std::function<void( int, int )> callback;", Spaces); |
17827 | verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", |
17828 | Spaces); |
17829 | verifyFormat("while ( (bool)1 )\n" |
17830 | " continue;", |
17831 | Spaces); |
17832 | verifyFormat("for ( ;; )\n" |
17833 | " continue;", |
17834 | Spaces); |
17835 | verifyFormat("if ( true )\n" |
17836 | " f();\n" |
17837 | "else if ( true )\n" |
17838 | " f();", |
17839 | Spaces); |
17840 | verifyFormat("do {\n" |
17841 | " do_something( (int)i );\n" |
17842 | "} while ( something() );", |
17843 | Spaces); |
17844 | verifyFormat("switch ( x ) {\n" |
17845 | "default:\n" |
17846 | " break;\n" |
17847 | "}", |
17848 | Spaces); |
17849 | verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces); |
17850 | verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces); |
17851 | verifyFormat("void f() __attribute__( ( asdf ) );", Spaces); |
17852 | verifyFormat("x = (int32)y;", Spaces); |
17853 | verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces); |
17854 | verifyFormat("decltype( x ) y = 42;", Spaces); |
17855 | verifyFormat("decltype( ( x ) ) y = z;", Spaces); |
17856 | verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces); |
17857 | verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces); |
17858 | verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n" |
17859 | " f();", |
17860 | Spaces); |
17861 | verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n" |
17862 | " foo( i );", |
17863 | Spaces); |
17864 | verifyFormat("switch ( x / ( y + z ) ) {\n" |
17865 | "default:\n" |
17866 | " break;\n" |
17867 | "}", |
17868 | Spaces); |
17869 | |
17870 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
17871 | Spaces.SpacesInParensOptions = {}; |
17872 | Spaces.SpacesInParensOptions.InCStyleCasts = true; |
17873 | verifyFormat("Type *A = ( Type * )P;", Spaces); |
17874 | verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); |
17875 | verifyFormat("x = ( int32 )y;", Spaces); |
17876 | verifyFormat("throw ( int32 )x;", Spaces); |
17877 | verifyFormat("int a = ( int )(2.0f);", Spaces); |
17878 | verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); |
17879 | verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); |
17880 | verifyFormat("#define x (( int )-1)", Spaces); |
17881 | verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces); |
17882 | |
17883 | // Run the first set of tests again with: |
17884 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
17885 | Spaces.SpacesInParensOptions = {}; |
17886 | Spaces.SpacesInParensOptions.InEmptyParentheses = true; |
17887 | Spaces.SpacesInParensOptions.InCStyleCasts = true; |
17888 | verifyFormat("call(x, y, z);", Spaces); |
17889 | verifyFormat("call( );", Spaces); |
17890 | verifyFormat("std::function<void(int, int)> callback;", Spaces); |
17891 | verifyFormat("while (( bool )1)\n" |
17892 | " continue;", |
17893 | Spaces); |
17894 | verifyFormat("for (;;)\n" |
17895 | " continue;", |
17896 | Spaces); |
17897 | verifyFormat("if (true)\n" |
17898 | " f( );\n" |
17899 | "else if (true)\n" |
17900 | " f( );", |
17901 | Spaces); |
17902 | verifyFormat("do {\n" |
17903 | " do_something(( int )i);\n" |
17904 | "} while (something( ));", |
17905 | Spaces); |
17906 | verifyFormat("switch (x) {\n" |
17907 | "default:\n" |
17908 | " break;\n" |
17909 | "}", |
17910 | Spaces); |
17911 | verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); |
17912 | verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); |
17913 | verifyFormat("void f( ) __attribute__((asdf));", Spaces); |
17914 | verifyFormat("x = ( int32 )y;", Spaces); |
17915 | verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces); |
17916 | verifyFormat("decltype(x) y = 42;", Spaces); |
17917 | verifyFormat("decltype((x)) y = z;", Spaces); |
17918 | verifyFormat("decltype((foo( ))) a = foo( );", Spaces); |
17919 | verifyFormat("decltype((bar(10))) a = bar(11);", Spaces); |
17920 | verifyFormat("if ((x - y) && (a ^ b))\n" |
17921 | " f( );", |
17922 | Spaces); |
17923 | verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n" |
17924 | " foo(i);", |
17925 | Spaces); |
17926 | verifyFormat("switch (x / (y + z)) {\n" |
17927 | "default:\n" |
17928 | " break;\n" |
17929 | "}", |
17930 | Spaces); |
17931 | |
17932 | // Run the first set of tests again with: |
17933 | Spaces.SpaceAfterCStyleCast = true; |
17934 | verifyFormat("call(x, y, z);", Spaces); |
17935 | verifyFormat("call( );", Spaces); |
17936 | verifyFormat("std::function<void(int, int)> callback;", Spaces); |
17937 | verifyFormat("while (( bool ) 1)\n" |
17938 | " continue;", |
17939 | Spaces); |
17940 | verifyFormat("for (;;)\n" |
17941 | " continue;", |
17942 | Spaces); |
17943 | verifyFormat("if (true)\n" |
17944 | " f( );\n" |
17945 | "else if (true)\n" |
17946 | " f( );", |
17947 | Spaces); |
17948 | verifyFormat("do {\n" |
17949 | " do_something(( int ) i);\n" |
17950 | "} while (something( ));", |
17951 | Spaces); |
17952 | verifyFormat("switch (x) {\n" |
17953 | "default:\n" |
17954 | " break;\n" |
17955 | "}", |
17956 | Spaces); |
17957 | verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); |
17958 | verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); |
17959 | verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); |
17960 | verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); |
17961 | verifyFormat("bool *y = ( bool * ) (x);", Spaces); |
17962 | verifyFormat("throw ( int32 ) x;", Spaces); |
17963 | verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); |
17964 | verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); |
17965 | verifyFormat("void f( ) __attribute__((asdf));", Spaces); |
17966 | |
17967 | // Run subset of tests again with: |
17968 | Spaces.SpacesInParensOptions.InCStyleCasts = false; |
17969 | Spaces.SpaceAfterCStyleCast = true; |
17970 | verifyFormat("while ((bool) 1)\n" |
17971 | " continue;", |
17972 | Spaces); |
17973 | verifyFormat("do {\n" |
17974 | " do_something((int) i);\n" |
17975 | "} while (something( ));", |
17976 | Spaces); |
17977 | |
17978 | verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); |
17979 | verifyFormat("size_t idx = (size_t) a;", Spaces); |
17980 | verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); |
17981 | verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); |
17982 | verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); |
17983 | verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); |
17984 | verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); |
17985 | verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); |
17986 | verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); |
17987 | verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); |
17988 | verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); |
17989 | verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); |
17990 | verifyFormat("throw (int32) x;", Spaces); |
17991 | verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); |
17992 | verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); |
17993 | verifyFormat("void f( ) __attribute__((asdf));", Spaces); |
17994 | |
17995 | Spaces.ColumnLimit = 80; |
17996 | Spaces.IndentWidth = 4; |
17997 | Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
17998 | verifyFormat("void foo( ) {\n" |
17999 | " size_t foo = (*(function))(\n" |
18000 | " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " |
18001 | "BarrrrrrrrrrrrLong,\n" |
18002 | " FoooooooooLooooong);\n" |
18003 | "}", |
18004 | Spaces); |
18005 | Spaces.SpaceAfterCStyleCast = false; |
18006 | verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); |
18007 | verifyFormat("size_t idx = (size_t)a;", Spaces); |
18008 | verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); |
18009 | verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); |
18010 | verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); |
18011 | verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); |
18012 | verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); |
18013 | |
18014 | verifyFormat("void foo( ) {\n" |
18015 | " size_t foo = (*(function))(\n" |
18016 | " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " |
18017 | "BarrrrrrrrrrrrLong,\n" |
18018 | " FoooooooooLooooong);\n" |
18019 | "}", |
18020 | Spaces); |
18021 | |
18022 | Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
18023 | verifyFormat("void foo( ) {\n" |
18024 | " size_t foo = (*(function))(\n" |
18025 | " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " |
18026 | "BarrrrrrrrrrrrLong,\n" |
18027 | " FoooooooooLooooong\n" |
18028 | " );\n" |
18029 | "}", |
18030 | Spaces); |
18031 | verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); |
18032 | verifyFormat("size_t idx = (size_t)a;", Spaces); |
18033 | verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); |
18034 | verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); |
18035 | verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); |
18036 | verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); |
18037 | verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); |
18038 | |
18039 | // Check ExceptDoubleParentheses spaces |
18040 | Spaces.IndentWidth = 2; |
18041 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
18042 | Spaces.SpacesInParensOptions = {}; |
18043 | Spaces.SpacesInParensOptions.Other = true; |
18044 | Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true; |
18045 | verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces); |
18046 | verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces); |
18047 | verifyFormat("void f() __attribute__(( asdf ));", Spaces); |
18048 | verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces); |
18049 | verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces); |
18050 | verifyFormat("class __declspec( dllimport ) X {};", Spaces); |
18051 | verifyFormat("class __declspec(( dllimport )) X {};", Spaces); |
18052 | verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces); |
18053 | verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces); |
18054 | verifyFormat("decltype( x ) y = 42;", Spaces); |
18055 | verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces); |
18056 | verifyFormat("if (( i = j ))\n" |
18057 | " do_something( i );", |
18058 | Spaces); |
18059 | |
18060 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
18061 | Spaces.SpacesInParensOptions = {}; |
18062 | Spaces.SpacesInParensOptions.InConditionalStatements = true; |
18063 | Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true; |
18064 | verifyFormat("while ( (bool)1 )\n" |
18065 | " continue;", |
18066 | Spaces); |
18067 | verifyFormat("while ((i = j))\n" |
18068 | " continue;", |
18069 | Spaces); |
18070 | verifyFormat("do {\n" |
18071 | " do_something((int)i);\n" |
18072 | "} while ( something() );", |
18073 | Spaces); |
18074 | verifyFormat("do {\n" |
18075 | " do_something((int)i);\n" |
18076 | "} while ((i = i + 1));", |
18077 | Spaces); |
18078 | verifyFormat("if ( (x - y) && (a ^ b) )\n" |
18079 | " f();", |
18080 | Spaces); |
18081 | verifyFormat("if ((i = j))\n" |
18082 | " do_something(i);", |
18083 | Spaces); |
18084 | verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n" |
18085 | " foo(i);", |
18086 | Spaces); |
18087 | verifyFormat("switch ( x / (y + z) ) {\n" |
18088 | "default:\n" |
18089 | " break;\n" |
18090 | "}", |
18091 | Spaces); |
18092 | verifyFormat("if constexpr ((a = b))\n" |
18093 | " c;", |
18094 | Spaces); |
18095 | } |
18096 | |
18097 | TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { |
18098 | verifyFormat("int a[5];"); |
18099 | verifyFormat("a[3] += 42;"); |
18100 | |
18101 | FormatStyle Spaces = getLLVMStyle(); |
18102 | Spaces.SpacesInSquareBrackets = true; |
18103 | // Not lambdas. |
18104 | verifyFormat("int a[ 5 ];", Spaces); |
18105 | verifyFormat("a[ 3 ] += 42;", Spaces); |
18106 | verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); |
18107 | verifyFormat("double &operator[](int i) { return 0; }\n" |
18108 | "int i;", |
18109 | Spaces); |
18110 | verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); |
18111 | verifyFormat("int i = a[ a ][ a ]->f();", Spaces); |
18112 | verifyFormat("int i = (*b)[ a ]->f();", Spaces); |
18113 | // Lambdas. |
18114 | verifyFormat("int c = []() -> int { return 2; }();", Spaces); |
18115 | verifyFormat("return [ i, args... ] {};", Spaces); |
18116 | verifyFormat("int foo = [ &bar ]() {};", Spaces); |
18117 | verifyFormat("int foo = [ = ]() {};", Spaces); |
18118 | verifyFormat("int foo = [ & ]() {};", Spaces); |
18119 | verifyFormat("int foo = [ =, &bar ]() {};", Spaces); |
18120 | verifyFormat("int foo = [ &bar, = ]() {};", Spaces); |
18121 | } |
18122 | |
18123 | TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { |
18124 | FormatStyle NoSpaceStyle = getLLVMStyle(); |
18125 | verifyFormat("int a[5];", NoSpaceStyle); |
18126 | verifyFormat("a[3] += 42;", NoSpaceStyle); |
18127 | |
18128 | verifyFormat("int a[1];", NoSpaceStyle); |
18129 | verifyFormat("int 1 [a];", NoSpaceStyle); |
18130 | verifyFormat("int a[1][2];", NoSpaceStyle); |
18131 | verifyFormat("a[7] = 5;", NoSpaceStyle); |
18132 | verifyFormat("int a = (f())[23];", NoSpaceStyle); |
18133 | verifyFormat("f([] {})", NoSpaceStyle); |
18134 | |
18135 | FormatStyle Space = getLLVMStyle(); |
18136 | Space.SpaceBeforeSquareBrackets = true; |
18137 | verifyFormat("int c = []() -> int { return 2; }();", Space); |
18138 | verifyFormat("return [i, args...] {};", Space); |
18139 | |
18140 | verifyFormat("int a [5];", Space); |
18141 | verifyFormat("a [3] += 42;", Space); |
18142 | verifyFormat("constexpr char hello []{\"hello\"};", Space); |
18143 | verifyFormat("double &operator[](int i) { return 0; }\n" |
18144 | "int i;", |
18145 | Space); |
18146 | verifyFormat("std::unique_ptr<int []> foo() {}", Space); |
18147 | verifyFormat("int i = a [a][a]->f();", Space); |
18148 | verifyFormat("int i = (*b) [a]->f();", Space); |
18149 | |
18150 | verifyFormat("int a [1];", Space); |
18151 | verifyFormat("int 1 [a];", Space); |
18152 | verifyFormat("int a [1][2];", Space); |
18153 | verifyFormat("a [7] = 5;", Space); |
18154 | verifyFormat("int a = (f()) [23];", Space); |
18155 | verifyFormat("f([] {})", Space); |
18156 | } |
18157 | |
18158 | TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { |
18159 | verifyFormat("int a = 5;"); |
18160 | verifyFormat("a += 42;"); |
18161 | verifyFormat("a or_eq 8;"); |
18162 | |
18163 | auto Spaces = getLLVMStyle(Language: FormatStyle::LK_C); |
18164 | verifyFormat("xor = foo;", Spaces); |
18165 | |
18166 | Spaces.Language = FormatStyle::LK_Cpp; |
18167 | Spaces.SpaceBeforeAssignmentOperators = false; |
18168 | verifyFormat("int a= 5;", Spaces); |
18169 | verifyFormat("a+= 42;", Spaces); |
18170 | verifyFormat("a or_eq 8;", Spaces); |
18171 | verifyFormat("xor= foo;", Spaces); |
18172 | } |
18173 | |
18174 | TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { |
18175 | verifyFormat("class Foo : public Bar {};"); |
18176 | verifyFormat("Foo::Foo() : foo(1) {}"); |
18177 | verifyFormat("for (auto a : b) {\n}"); |
18178 | verifyFormat("int x = a ? b : c;"); |
18179 | verifyFormat("{\n" |
18180 | "label0:\n" |
18181 | " int x = 0;\n" |
18182 | "}"); |
18183 | verifyFormat("switch (x) {\n" |
18184 | "case 1:\n" |
18185 | "default:\n" |
18186 | "}"); |
18187 | verifyFormat("switch (allBraces) {\n" |
18188 | "case 1: {\n" |
18189 | " break;\n" |
18190 | "}\n" |
18191 | "case 2: {\n" |
18192 | " [[fallthrough]];\n" |
18193 | "}\n" |
18194 | "default: {\n" |
18195 | " break;\n" |
18196 | "}\n" |
18197 | "}"); |
18198 | |
18199 | FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(ColumnLimit: 30); |
18200 | CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; |
18201 | verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); |
18202 | verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); |
18203 | verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); |
18204 | verifyFormat("int x = a ? b : c;", CtorInitializerStyle); |
18205 | verifyFormat("{\n" |
18206 | "label1:\n" |
18207 | " int x = 0;\n" |
18208 | "}", |
18209 | CtorInitializerStyle); |
18210 | verifyFormat("switch (x) {\n" |
18211 | "case 1:\n" |
18212 | "default:\n" |
18213 | "}", |
18214 | CtorInitializerStyle); |
18215 | verifyFormat("switch (allBraces) {\n" |
18216 | "case 1: {\n" |
18217 | " break;\n" |
18218 | "}\n" |
18219 | "case 2: {\n" |
18220 | " [[fallthrough]];\n" |
18221 | "}\n" |
18222 | "default: {\n" |
18223 | " break;\n" |
18224 | "}\n" |
18225 | "}", |
18226 | CtorInitializerStyle); |
18227 | CtorInitializerStyle.BreakConstructorInitializers = |
18228 | FormatStyle::BCIS_AfterColon; |
18229 | verifyFormat("Fooooooooooo::Fooooooooooo():\n" |
18230 | " aaaaaaaaaaaaaaaa(1),\n" |
18231 | " bbbbbbbbbbbbbbbb(2) {}", |
18232 | CtorInitializerStyle); |
18233 | CtorInitializerStyle.BreakConstructorInitializers = |
18234 | FormatStyle::BCIS_BeforeComma; |
18235 | verifyFormat("Fooooooooooo::Fooooooooooo()\n" |
18236 | " : aaaaaaaaaaaaaaaa(1)\n" |
18237 | " , bbbbbbbbbbbbbbbb(2) {}", |
18238 | CtorInitializerStyle); |
18239 | CtorInitializerStyle.BreakConstructorInitializers = |
18240 | FormatStyle::BCIS_BeforeColon; |
18241 | verifyFormat("Fooooooooooo::Fooooooooooo()\n" |
18242 | " : aaaaaaaaaaaaaaaa(1),\n" |
18243 | " bbbbbbbbbbbbbbbb(2) {}", |
18244 | CtorInitializerStyle); |
18245 | CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; |
18246 | verifyFormat("Fooooooooooo::Fooooooooooo()\n" |
18247 | ": aaaaaaaaaaaaaaaa(1),\n" |
18248 | " bbbbbbbbbbbbbbbb(2) {}", |
18249 | CtorInitializerStyle); |
18250 | |
18251 | FormatStyle InheritanceStyle = getLLVMStyleWithColumns(ColumnLimit: 30); |
18252 | InheritanceStyle.SpaceBeforeInheritanceColon = false; |
18253 | verifyFormat("class Foo: public Bar {};", InheritanceStyle); |
18254 | verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); |
18255 | verifyFormat("for (auto a : b) {\n}", InheritanceStyle); |
18256 | verifyFormat("int x = a ? b : c;", InheritanceStyle); |
18257 | verifyFormat("{\n" |
18258 | "label2:\n" |
18259 | " int x = 0;\n" |
18260 | "}", |
18261 | InheritanceStyle); |
18262 | verifyFormat("switch (x) {\n" |
18263 | "case 1:\n" |
18264 | "default:\n" |
18265 | "}", |
18266 | InheritanceStyle); |
18267 | verifyFormat("switch (allBraces) {\n" |
18268 | "case 1: {\n" |
18269 | " break;\n" |
18270 | "}\n" |
18271 | "case 2: {\n" |
18272 | " [[fallthrough]];\n" |
18273 | "}\n" |
18274 | "default: {\n" |
18275 | " break;\n" |
18276 | "}\n" |
18277 | "}", |
18278 | InheritanceStyle); |
18279 | InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; |
18280 | verifyFormat("class Foooooooooooooooooooooo\n" |
18281 | " : public aaaaaaaaaaaaaaaaaa,\n" |
18282 | " public bbbbbbbbbbbbbbbbbb {\n" |
18283 | "}", |
18284 | InheritanceStyle); |
18285 | InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; |
18286 | verifyFormat("class Foooooooooooooooooooooo:\n" |
18287 | " public aaaaaaaaaaaaaaaaaa,\n" |
18288 | " public bbbbbbbbbbbbbbbbbb {\n" |
18289 | "}", |
18290 | InheritanceStyle); |
18291 | InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; |
18292 | verifyFormat("class Foooooooooooooooooooooo\n" |
18293 | " : public aaaaaaaaaaaaaaaaaa\n" |
18294 | " , public bbbbbbbbbbbbbbbbbb {\n" |
18295 | "}", |
18296 | InheritanceStyle); |
18297 | InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; |
18298 | verifyFormat("class Foooooooooooooooooooooo\n" |
18299 | " : public aaaaaaaaaaaaaaaaaa,\n" |
18300 | " public bbbbbbbbbbbbbbbbbb {\n" |
18301 | "}", |
18302 | InheritanceStyle); |
18303 | InheritanceStyle.ConstructorInitializerIndentWidth = 0; |
18304 | verifyFormat("class Foooooooooooooooooooooo\n" |
18305 | ": public aaaaaaaaaaaaaaaaaa,\n" |
18306 | " public bbbbbbbbbbbbbbbbbb {}", |
18307 | InheritanceStyle); |
18308 | |
18309 | FormatStyle ForLoopStyle = getLLVMStyle(); |
18310 | ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; |
18311 | verifyFormat("class Foo : public Bar {};", ForLoopStyle); |
18312 | verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); |
18313 | verifyFormat("for (auto a: b) {\n}", ForLoopStyle); |
18314 | verifyFormat("int x = a ? b : c;", ForLoopStyle); |
18315 | verifyFormat("{\n" |
18316 | "label2:\n" |
18317 | " int x = 0;\n" |
18318 | "}", |
18319 | ForLoopStyle); |
18320 | verifyFormat("switch (x) {\n" |
18321 | "case 1:\n" |
18322 | "default:\n" |
18323 | "}", |
18324 | ForLoopStyle); |
18325 | verifyFormat("switch (allBraces) {\n" |
18326 | "case 1: {\n" |
18327 | " break;\n" |
18328 | "}\n" |
18329 | "case 2: {\n" |
18330 | " [[fallthrough]];\n" |
18331 | "}\n" |
18332 | "default: {\n" |
18333 | " break;\n" |
18334 | "}\n" |
18335 | "}", |
18336 | ForLoopStyle); |
18337 | |
18338 | FormatStyle CaseStyle = getLLVMStyle(); |
18339 | CaseStyle.SpaceBeforeCaseColon = true; |
18340 | verifyFormat("class Foo : public Bar {};", CaseStyle); |
18341 | verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); |
18342 | verifyFormat("for (auto a : b) {\n}", CaseStyle); |
18343 | verifyFormat("int x = a ? b : c;", CaseStyle); |
18344 | verifyFormat("switch (x) {\n" |
18345 | "case 1 :\n" |
18346 | "default :\n" |
18347 | "}", |
18348 | CaseStyle); |
18349 | verifyFormat("switch (allBraces) {\n" |
18350 | "case 1 : {\n" |
18351 | " break;\n" |
18352 | "}\n" |
18353 | "case 2 : {\n" |
18354 | " [[fallthrough]];\n" |
18355 | "}\n" |
18356 | "default : {\n" |
18357 | " break;\n" |
18358 | "}\n" |
18359 | "}", |
18360 | CaseStyle); |
18361 | // Goto labels should not be affected. |
18362 | verifyFormat("switch (x) {\n" |
18363 | "goto_label:\n" |
18364 | "default :\n" |
18365 | "}", |
18366 | CaseStyle); |
18367 | verifyFormat("switch (x) {\n" |
18368 | "goto_label: { break; }\n" |
18369 | "default : {\n" |
18370 | " break;\n" |
18371 | "}\n" |
18372 | "}", |
18373 | CaseStyle); |
18374 | |
18375 | FormatStyle NoSpaceStyle = getLLVMStyle(); |
18376 | EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); |
18377 | NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; |
18378 | NoSpaceStyle.SpaceBeforeInheritanceColon = false; |
18379 | NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; |
18380 | verifyFormat("class Foo: public Bar {};", NoSpaceStyle); |
18381 | verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); |
18382 | verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); |
18383 | verifyFormat("int x = a ? b : c;", NoSpaceStyle); |
18384 | verifyFormat("{\n" |
18385 | "label3:\n" |
18386 | " int x = 0;\n" |
18387 | "}", |
18388 | NoSpaceStyle); |
18389 | verifyFormat("switch (x) {\n" |
18390 | "case 1:\n" |
18391 | "default:\n" |
18392 | "}", |
18393 | NoSpaceStyle); |
18394 | verifyFormat("switch (allBraces) {\n" |
18395 | "case 1: {\n" |
18396 | " break;\n" |
18397 | "}\n" |
18398 | "case 2: {\n" |
18399 | " [[fallthrough]];\n" |
18400 | "}\n" |
18401 | "default: {\n" |
18402 | " break;\n" |
18403 | "}\n" |
18404 | "}", |
18405 | NoSpaceStyle); |
18406 | |
18407 | FormatStyle InvertedSpaceStyle = getLLVMStyle(); |
18408 | InvertedSpaceStyle.SpaceBeforeCaseColon = true; |
18409 | InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; |
18410 | InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; |
18411 | InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; |
18412 | verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); |
18413 | verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); |
18414 | verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); |
18415 | verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); |
18416 | verifyFormat("{\n" |
18417 | "label3:\n" |
18418 | " int x = 0;\n" |
18419 | "}", |
18420 | InvertedSpaceStyle); |
18421 | verifyFormat("switch (x) {\n" |
18422 | "case 1 :\n" |
18423 | "case 2 : {\n" |
18424 | " break;\n" |
18425 | "}\n" |
18426 | "default :\n" |
18427 | " break;\n" |
18428 | "}", |
18429 | InvertedSpaceStyle); |
18430 | verifyFormat("switch (allBraces) {\n" |
18431 | "case 1 : {\n" |
18432 | " break;\n" |
18433 | "}\n" |
18434 | "case 2 : {\n" |
18435 | " [[fallthrough]];\n" |
18436 | "}\n" |
18437 | "default : {\n" |
18438 | " break;\n" |
18439 | "}\n" |
18440 | "}", |
18441 | InvertedSpaceStyle); |
18442 | } |
18443 | |
18444 | TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { |
18445 | FormatStyle Style = getLLVMStyle(); |
18446 | |
18447 | Style.PointerAlignment = FormatStyle::PAS_Left; |
18448 | Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; |
18449 | verifyFormat("void* const* x = NULL;", Style); |
18450 | |
18451 | #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ |
18452 | do { \ |
18453 | Style.PointerAlignment = FormatStyle::Pointers; \ |
18454 | Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ |
18455 | verifyFormat(Code, Style); \ |
18456 | } while (false) |
18457 | |
18458 | verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); |
18459 | verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); |
18460 | verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); |
18461 | |
18462 | verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); |
18463 | verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); |
18464 | verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); |
18465 | |
18466 | verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); |
18467 | verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); |
18468 | verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); |
18469 | |
18470 | verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); |
18471 | verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); |
18472 | verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); |
18473 | |
18474 | verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); |
18475 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, |
18476 | SAPQ_Default); |
18477 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, |
18478 | SAPQ_Default); |
18479 | |
18480 | verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); |
18481 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, |
18482 | SAPQ_Before); |
18483 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, |
18484 | SAPQ_Before); |
18485 | |
18486 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); |
18487 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); |
18488 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, |
18489 | SAPQ_After); |
18490 | |
18491 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); |
18492 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); |
18493 | verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); |
18494 | |
18495 | #undef verifyQualifierSpaces |
18496 | |
18497 | FormatStyle Spaces = getLLVMStyle(); |
18498 | Spaces.AttributeMacros.push_back(x: "qualified"); |
18499 | Spaces.PointerAlignment = FormatStyle::PAS_Right; |
18500 | Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; |
18501 | verifyFormat("SomeType *volatile *a = NULL;", Spaces); |
18502 | verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); |
18503 | verifyFormat("std::vector<SomeType *const *> x;", Spaces); |
18504 | verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); |
18505 | verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); |
18506 | Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; |
18507 | verifyFormat("SomeType * volatile *a = NULL;", Spaces); |
18508 | verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); |
18509 | verifyFormat("std::vector<SomeType * const *> x;", Spaces); |
18510 | verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); |
18511 | verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); |
18512 | |
18513 | // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. |
18514 | Spaces.PointerAlignment = FormatStyle::PAS_Left; |
18515 | Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; |
18516 | verifyFormat("SomeType* volatile* a = NULL;", Spaces); |
18517 | verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); |
18518 | verifyFormat("std::vector<SomeType* const*> x;", Spaces); |
18519 | verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); |
18520 | verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); |
18521 | // However, setting it to SAPQ_After should add spaces after __attribute, etc. |
18522 | Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; |
18523 | verifyFormat("SomeType* volatile * a = NULL;", Spaces); |
18524 | verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); |
18525 | verifyFormat("std::vector<SomeType* const *> x;", Spaces); |
18526 | verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); |
18527 | verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); |
18528 | |
18529 | // PAS_Middle should not have any noticeable changes even for SAPQ_Both |
18530 | Spaces.PointerAlignment = FormatStyle::PAS_Middle; |
18531 | Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; |
18532 | verifyFormat("SomeType * volatile * a = NULL;", Spaces); |
18533 | verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); |
18534 | verifyFormat("std::vector<SomeType * const *> x;", Spaces); |
18535 | verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); |
18536 | verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); |
18537 | } |
18538 | |
18539 | TEST_F(FormatTest, AlignConsecutiveMacros) { |
18540 | FormatStyle Style = getLLVMStyle(); |
18541 | Style.AlignConsecutiveAssignments.Enabled = true; |
18542 | Style.AlignConsecutiveDeclarations.Enabled = true; |
18543 | |
18544 | verifyFormat("#define a 3\n" |
18545 | "#define bbbb 4\n" |
18546 | "#define ccc (5)", |
18547 | Style); |
18548 | |
18549 | verifyFormat("#define f(x) (x * x)\n" |
18550 | "#define fff(x, y, z) (x * y + z)\n" |
18551 | "#define ffff(x, y) (x - y)", |
18552 | Style); |
18553 | |
18554 | verifyFormat("#define foo(x, y) (x + y)\n" |
18555 | "#define bar (5, 6)(2 + 2)", |
18556 | Style); |
18557 | |
18558 | verifyFormat("#define a 3\n" |
18559 | "#define bbbb 4\n" |
18560 | "#define ccc (5)\n" |
18561 | "#define f(x) (x * x)\n" |
18562 | "#define fff(x, y, z) (x * y + z)\n" |
18563 | "#define ffff(x, y) (x - y)", |
18564 | Style); |
18565 | |
18566 | Style.AlignConsecutiveMacros.Enabled = true; |
18567 | verifyFormat("#define a 3\n" |
18568 | "#define bbbb 4\n" |
18569 | "#define ccc (5)", |
18570 | Style); |
18571 | |
18572 | verifyFormat("#define true 1\n" |
18573 | "#define false 0", |
18574 | Style); |
18575 | |
18576 | verifyFormat("#define f(x) (x * x)\n" |
18577 | "#define fff(x, y, z) (x * y + z)\n" |
18578 | "#define ffff(x, y) (x - y)", |
18579 | Style); |
18580 | |
18581 | verifyFormat("#define foo(x, y) (x + y)\n" |
18582 | "#define bar (5, 6)(2 + 2)", |
18583 | Style); |
18584 | |
18585 | verifyFormat("#define a 3\n" |
18586 | "#define bbbb 4\n" |
18587 | "#define ccc (5)\n" |
18588 | "#define f(x) (x * x)\n" |
18589 | "#define fff(x, y, z) (x * y + z)\n" |
18590 | "#define ffff(x, y) (x - y)", |
18591 | Style); |
18592 | |
18593 | verifyFormat("#define a 5\n" |
18594 | "#define foo(x, y) (x + y)\n" |
18595 | "#define CCC (6)\n" |
18596 | "auto lambda = []() {\n" |
18597 | " auto ii = 0;\n" |
18598 | " float j = 0;\n" |
18599 | " return 0;\n" |
18600 | "};\n" |
18601 | "int i = 0;\n" |
18602 | "float i2 = 0;\n" |
18603 | "auto v = type{\n" |
18604 | " i = 1, //\n" |
18605 | " (i = 2), //\n" |
18606 | " i = 3 //\n" |
18607 | "};", |
18608 | Style); |
18609 | |
18610 | Style.AlignConsecutiveMacros.Enabled = false; |
18611 | Style.ColumnLimit = 20; |
18612 | |
18613 | verifyFormat("#define a \\\n" |
18614 | " \"aabbbbbbbbbbbb\"\n" |
18615 | "#define D \\\n" |
18616 | " \"aabbbbbbbbbbbb\" \\\n" |
18617 | " \"ccddeeeeeeeee\"\n" |
18618 | "#define B \\\n" |
18619 | " \"QQQQQQQQQQQQQ\" \\\n" |
18620 | " \"FFFFFFFFFFFFF\" \\\n" |
18621 | " \"LLLLLLLL\"", |
18622 | Style); |
18623 | |
18624 | Style.AlignConsecutiveMacros.Enabled = true; |
18625 | verifyFormat("#define a \\\n" |
18626 | " \"aabbbbbbbbbbbb\"\n" |
18627 | "#define D \\\n" |
18628 | " \"aabbbbbbbbbbbb\" \\\n" |
18629 | " \"ccddeeeeeeeee\"\n" |
18630 | "#define B \\\n" |
18631 | " \"QQQQQQQQQQQQQ\" \\\n" |
18632 | " \"FFFFFFFFFFFFF\" \\\n" |
18633 | " \"LLLLLLLL\"", |
18634 | Style); |
18635 | |
18636 | // Test across comments |
18637 | Style.MaxEmptyLinesToKeep = 10; |
18638 | Style.ReflowComments = FormatStyle::RCS_Never; |
18639 | Style.AlignConsecutiveMacros.AcrossComments = true; |
18640 | verifyFormat("#define a 3\n" |
18641 | "// line comment\n" |
18642 | "#define bbbb 4\n" |
18643 | "#define ccc (5)", |
18644 | "#define a 3\n" |
18645 | "// line comment\n" |
18646 | "#define bbbb 4\n" |
18647 | "#define ccc (5)", |
18648 | Style); |
18649 | |
18650 | verifyFormat("#define a 3\n" |
18651 | "/* block comment */\n" |
18652 | "#define bbbb 4\n" |
18653 | "#define ccc (5)", |
18654 | "#define a 3\n" |
18655 | "/* block comment */\n" |
18656 | "#define bbbb 4\n" |
18657 | "#define ccc (5)", |
18658 | Style); |
18659 | |
18660 | verifyFormat("#define a 3\n" |
18661 | "/* multi-line *\n" |
18662 | " * block comment */\n" |
18663 | "#define bbbb 4\n" |
18664 | "#define ccc (5)", |
18665 | "#define a 3\n" |
18666 | "/* multi-line *\n" |
18667 | " * block comment */\n" |
18668 | "#define bbbb 4\n" |
18669 | "#define ccc (5)", |
18670 | Style); |
18671 | |
18672 | verifyFormat("#define a 3\n" |
18673 | "// multi-line line comment\n" |
18674 | "//\n" |
18675 | "#define bbbb 4\n" |
18676 | "#define ccc (5)", |
18677 | "#define a 3\n" |
18678 | "// multi-line line comment\n" |
18679 | "//\n" |
18680 | "#define bbbb 4\n" |
18681 | "#define ccc (5)", |
18682 | Style); |
18683 | |
18684 | verifyFormat("#define a 3\n" |
18685 | "// empty lines still break.\n" |
18686 | "\n" |
18687 | "#define bbbb 4\n" |
18688 | "#define ccc (5)", |
18689 | "#define a 3\n" |
18690 | "// empty lines still break.\n" |
18691 | "\n" |
18692 | "#define bbbb 4\n" |
18693 | "#define ccc (5)", |
18694 | Style); |
18695 | |
18696 | // Test across empty lines |
18697 | Style.AlignConsecutiveMacros.AcrossComments = false; |
18698 | Style.AlignConsecutiveMacros.AcrossEmptyLines = true; |
18699 | verifyFormat("#define a 3\n" |
18700 | "\n" |
18701 | "#define bbbb 4\n" |
18702 | "#define ccc (5)", |
18703 | "#define a 3\n" |
18704 | "\n" |
18705 | "#define bbbb 4\n" |
18706 | "#define ccc (5)", |
18707 | Style); |
18708 | |
18709 | verifyFormat("#define a 3\n" |
18710 | "\n" |
18711 | "\n" |
18712 | "\n" |
18713 | "#define bbbb 4\n" |
18714 | "#define ccc (5)", |
18715 | "#define a 3\n" |
18716 | "\n" |
18717 | "\n" |
18718 | "\n" |
18719 | "#define bbbb 4\n" |
18720 | "#define ccc (5)", |
18721 | Style); |
18722 | |
18723 | verifyFormat("#define a 3\n" |
18724 | "// comments should break alignment\n" |
18725 | "//\n" |
18726 | "#define bbbb 4\n" |
18727 | "#define ccc (5)", |
18728 | "#define a 3\n" |
18729 | "// comments should break alignment\n" |
18730 | "//\n" |
18731 | "#define bbbb 4\n" |
18732 | "#define ccc (5)", |
18733 | Style); |
18734 | |
18735 | // Test across empty lines and comments |
18736 | Style.AlignConsecutiveMacros.AcrossComments = true; |
18737 | verifyFormat("#define a 3\n" |
18738 | "\n" |
18739 | "// line comment\n" |
18740 | "#define bbbb 4\n" |
18741 | "#define ccc (5)", |
18742 | Style); |
18743 | |
18744 | verifyFormat("#define a 3\n" |
18745 | "\n" |
18746 | "\n" |
18747 | "/* multi-line *\n" |
18748 | " * block comment */\n" |
18749 | "\n" |
18750 | "\n" |
18751 | "#define bbbb 4\n" |
18752 | "#define ccc (5)", |
18753 | "#define a 3\n" |
18754 | "\n" |
18755 | "\n" |
18756 | "/* multi-line *\n" |
18757 | " * block comment */\n" |
18758 | "\n" |
18759 | "\n" |
18760 | "#define bbbb 4\n" |
18761 | "#define ccc (5)", |
18762 | Style); |
18763 | |
18764 | verifyFormat("#define a 3\n" |
18765 | "\n" |
18766 | "\n" |
18767 | "/* multi-line *\n" |
18768 | " * block comment */\n" |
18769 | "\n" |
18770 | "\n" |
18771 | "#define bbbb 4\n" |
18772 | "#define ccc (5)", |
18773 | "#define a 3\n" |
18774 | "\n" |
18775 | "\n" |
18776 | "/* multi-line *\n" |
18777 | " * block comment */\n" |
18778 | "\n" |
18779 | "\n" |
18780 | "#define bbbb 4\n" |
18781 | "#define ccc (5)", |
18782 | Style); |
18783 | } |
18784 | |
18785 | TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { |
18786 | FormatStyle Alignment = getLLVMStyle(); |
18787 | Alignment.AlignConsecutiveMacros.Enabled = true; |
18788 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
18789 | Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; |
18790 | |
18791 | Alignment.MaxEmptyLinesToKeep = 10; |
18792 | /* Test alignment across empty lines */ |
18793 | verifyFormat("int a = 5;\n" |
18794 | "\n" |
18795 | "int oneTwoThree = 123;", |
18796 | "int a = 5;\n" |
18797 | "\n" |
18798 | "int oneTwoThree= 123;", |
18799 | Alignment); |
18800 | verifyFormat("int a = 5;\n" |
18801 | "int one = 1;\n" |
18802 | "\n" |
18803 | "int oneTwoThree = 123;", |
18804 | "int a = 5;\n" |
18805 | "int one = 1;\n" |
18806 | "\n" |
18807 | "int oneTwoThree = 123;", |
18808 | Alignment); |
18809 | verifyFormat("int a = 5;\n" |
18810 | "int one = 1;\n" |
18811 | "\n" |
18812 | "int oneTwoThree = 123;\n" |
18813 | "int oneTwo = 12;", |
18814 | "int a = 5;\n" |
18815 | "int one = 1;\n" |
18816 | "\n" |
18817 | "int oneTwoThree = 123;\n" |
18818 | "int oneTwo = 12;", |
18819 | Alignment); |
18820 | |
18821 | /* Test across comments */ |
18822 | verifyFormat("int a = 5;\n" |
18823 | "/* block comment */\n" |
18824 | "int oneTwoThree = 123;", |
18825 | "int a = 5;\n" |
18826 | "/* block comment */\n" |
18827 | "int oneTwoThree=123;", |
18828 | Alignment); |
18829 | |
18830 | verifyFormat("int a = 5;\n" |
18831 | "// line comment\n" |
18832 | "int oneTwoThree = 123;", |
18833 | "int a = 5;\n" |
18834 | "// line comment\n" |
18835 | "int oneTwoThree=123;", |
18836 | Alignment); |
18837 | |
18838 | /* Test across comments and newlines */ |
18839 | verifyFormat("int a = 5;\n" |
18840 | "\n" |
18841 | "/* block comment */\n" |
18842 | "int oneTwoThree = 123;", |
18843 | "int a = 5;\n" |
18844 | "\n" |
18845 | "/* block comment */\n" |
18846 | "int oneTwoThree=123;", |
18847 | Alignment); |
18848 | |
18849 | verifyFormat("int a = 5;\n" |
18850 | "\n" |
18851 | "// line comment\n" |
18852 | "int oneTwoThree = 123;", |
18853 | "int a = 5;\n" |
18854 | "\n" |
18855 | "// line comment\n" |
18856 | "int oneTwoThree=123;", |
18857 | Alignment); |
18858 | } |
18859 | |
18860 | TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { |
18861 | FormatStyle Alignment = getLLVMStyle(); |
18862 | Alignment.AlignConsecutiveDeclarations.Enabled = true; |
18863 | Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true; |
18864 | Alignment.AlignConsecutiveDeclarations.AcrossComments = true; |
18865 | |
18866 | Alignment.MaxEmptyLinesToKeep = 10; |
18867 | /* Test alignment across empty lines */ |
18868 | verifyFormat("int a = 5;\n" |
18869 | "\n" |
18870 | "float const oneTwoThree = 123;", |
18871 | "int a = 5;\n" |
18872 | "\n" |
18873 | "float const oneTwoThree = 123;", |
18874 | Alignment); |
18875 | verifyFormat("int a = 5;\n" |
18876 | "float const one = 1;\n" |
18877 | "\n" |
18878 | "int oneTwoThree = 123;", |
18879 | "int a = 5;\n" |
18880 | "float const one = 1;\n" |
18881 | "\n" |
18882 | "int oneTwoThree = 123;", |
18883 | Alignment); |
18884 | |
18885 | /* Test across comments */ |
18886 | verifyFormat("float const a = 5;\n" |
18887 | "/* block comment */\n" |
18888 | "int oneTwoThree = 123;", |
18889 | "float const a = 5;\n" |
18890 | "/* block comment */\n" |
18891 | "int oneTwoThree=123;", |
18892 | Alignment); |
18893 | |
18894 | verifyFormat("float const a = 5;\n" |
18895 | "// line comment\n" |
18896 | "int oneTwoThree = 123;", |
18897 | "float const a = 5;\n" |
18898 | "// line comment\n" |
18899 | "int oneTwoThree=123;", |
18900 | Alignment); |
18901 | |
18902 | /* Test across comments and newlines */ |
18903 | verifyFormat("float const a = 5;\n" |
18904 | "\n" |
18905 | "/* block comment */\n" |
18906 | "int oneTwoThree = 123;", |
18907 | "float const a = 5;\n" |
18908 | "\n" |
18909 | "/* block comment */\n" |
18910 | "int oneTwoThree=123;", |
18911 | Alignment); |
18912 | |
18913 | verifyFormat("float const a = 5;\n" |
18914 | "\n" |
18915 | "// line comment\n" |
18916 | "int oneTwoThree = 123;", |
18917 | "float const a = 5;\n" |
18918 | "\n" |
18919 | "// line comment\n" |
18920 | "int oneTwoThree=123;", |
18921 | Alignment); |
18922 | } |
18923 | |
18924 | TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { |
18925 | FormatStyle Alignment = getLLVMStyle(); |
18926 | Alignment.AlignConsecutiveBitFields.Enabled = true; |
18927 | Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true; |
18928 | Alignment.AlignConsecutiveBitFields.AcrossComments = true; |
18929 | |
18930 | Alignment.MaxEmptyLinesToKeep = 10; |
18931 | /* Test alignment across empty lines */ |
18932 | verifyFormat("int a : 5;\n" |
18933 | "\n" |
18934 | "int longbitfield : 6;", |
18935 | "int a : 5;\n" |
18936 | "\n" |
18937 | "int longbitfield : 6;", |
18938 | Alignment); |
18939 | verifyFormat("int a : 5;\n" |
18940 | "int one : 1;\n" |
18941 | "\n" |
18942 | "int longbitfield : 6;", |
18943 | "int a : 5;\n" |
18944 | "int one : 1;\n" |
18945 | "\n" |
18946 | "int longbitfield : 6;", |
18947 | Alignment); |
18948 | |
18949 | /* Test across comments */ |
18950 | verifyFormat("int a : 5;\n" |
18951 | "/* block comment */\n" |
18952 | "int longbitfield : 6;", |
18953 | "int a : 5;\n" |
18954 | "/* block comment */\n" |
18955 | "int longbitfield : 6;", |
18956 | Alignment); |
18957 | verifyFormat("int a : 5;\n" |
18958 | "int one : 1;\n" |
18959 | "// line comment\n" |
18960 | "int longbitfield : 6;", |
18961 | "int a : 5;\n" |
18962 | "int one : 1;\n" |
18963 | "// line comment\n" |
18964 | "int longbitfield : 6;", |
18965 | Alignment); |
18966 | |
18967 | /* Test across comments and newlines */ |
18968 | verifyFormat("int a : 5;\n" |
18969 | "/* block comment */\n" |
18970 | "\n" |
18971 | "int longbitfield : 6;", |
18972 | "int a : 5;\n" |
18973 | "/* block comment */\n" |
18974 | "\n" |
18975 | "int longbitfield : 6;", |
18976 | Alignment); |
18977 | verifyFormat("int a : 5;\n" |
18978 | "int one : 1;\n" |
18979 | "\n" |
18980 | "// line comment\n" |
18981 | "\n" |
18982 | "int longbitfield : 6;", |
18983 | "int a : 5;\n" |
18984 | "int one : 1;\n" |
18985 | "\n" |
18986 | "// line comment \n" |
18987 | "\n" |
18988 | "int longbitfield : 6;", |
18989 | Alignment); |
18990 | } |
18991 | |
18992 | TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { |
18993 | FormatStyle Alignment = getLLVMStyle(); |
18994 | Alignment.AlignConsecutiveMacros.Enabled = true; |
18995 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
18996 | Alignment.AlignConsecutiveAssignments.AcrossComments = true; |
18997 | |
18998 | Alignment.MaxEmptyLinesToKeep = 10; |
18999 | /* Test alignment across empty lines */ |
19000 | verifyFormat("int a = 5;\n" |
19001 | "\n" |
19002 | "int oneTwoThree = 123;", |
19003 | "int a = 5;\n" |
19004 | "\n" |
19005 | "int oneTwoThree= 123;", |
19006 | Alignment); |
19007 | verifyFormat("int a = 5;\n" |
19008 | "int one = 1;\n" |
19009 | "\n" |
19010 | "int oneTwoThree = 123;", |
19011 | "int a = 5;\n" |
19012 | "int one = 1;\n" |
19013 | "\n" |
19014 | "int oneTwoThree = 123;", |
19015 | Alignment); |
19016 | |
19017 | /* Test across comments */ |
19018 | verifyFormat("int a = 5;\n" |
19019 | "/* block comment */\n" |
19020 | "int oneTwoThree = 123;", |
19021 | "int a = 5;\n" |
19022 | "/* block comment */\n" |
19023 | "int oneTwoThree=123;", |
19024 | Alignment); |
19025 | |
19026 | verifyFormat("int a = 5;\n" |
19027 | "// line comment\n" |
19028 | "int oneTwoThree = 123;", |
19029 | "int a = 5;\n" |
19030 | "// line comment\n" |
19031 | "int oneTwoThree=123;", |
19032 | Alignment); |
19033 | |
19034 | verifyFormat("int a = 5;\n" |
19035 | "/*\n" |
19036 | " * multi-line block comment\n" |
19037 | " */\n" |
19038 | "int oneTwoThree = 123;", |
19039 | "int a = 5;\n" |
19040 | "/*\n" |
19041 | " * multi-line block comment\n" |
19042 | " */\n" |
19043 | "int oneTwoThree=123;", |
19044 | Alignment); |
19045 | |
19046 | verifyFormat("int a = 5;\n" |
19047 | "//\n" |
19048 | "// multi-line line comment\n" |
19049 | "//\n" |
19050 | "int oneTwoThree = 123;", |
19051 | "int a = 5;\n" |
19052 | "//\n" |
19053 | "// multi-line line comment\n" |
19054 | "//\n" |
19055 | "int oneTwoThree=123;", |
19056 | Alignment); |
19057 | |
19058 | /* Test across comments and newlines */ |
19059 | verifyFormat("int a = 5;\n" |
19060 | "\n" |
19061 | "/* block comment */\n" |
19062 | "int oneTwoThree = 123;", |
19063 | "int a = 5;\n" |
19064 | "\n" |
19065 | "/* block comment */\n" |
19066 | "int oneTwoThree=123;", |
19067 | Alignment); |
19068 | |
19069 | verifyFormat("int a = 5;\n" |
19070 | "\n" |
19071 | "// line comment\n" |
19072 | "int oneTwoThree = 123;", |
19073 | "int a = 5;\n" |
19074 | "\n" |
19075 | "// line comment\n" |
19076 | "int oneTwoThree=123;", |
19077 | Alignment); |
19078 | } |
19079 | |
19080 | TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { |
19081 | FormatStyle Alignment = getLLVMStyle(); |
19082 | Alignment.AlignConsecutiveMacros.Enabled = true; |
19083 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
19084 | Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; |
19085 | Alignment.AlignConsecutiveAssignments.AcrossComments = true; |
19086 | verifyFormat("int a = 5;\n" |
19087 | "int oneTwoThree = 123;", |
19088 | Alignment); |
19089 | verifyFormat("int a = method();\n" |
19090 | "int oneTwoThree = 133;", |
19091 | Alignment); |
19092 | verifyFormat("a &= 5;\n" |
19093 | "bcd *= 5;\n" |
19094 | "ghtyf += 5;\n" |
19095 | "dvfvdb -= 5;\n" |
19096 | "a /= 5;\n" |
19097 | "vdsvsv %= 5;\n" |
19098 | "sfdbddfbdfbb ^= 5;\n" |
19099 | "dvsdsv |= 5;\n" |
19100 | "int dsvvdvsdvvv = 123;", |
19101 | Alignment); |
19102 | verifyFormat("int i = 1, j = 10;\n" |
19103 | "something = 2000;", |
19104 | Alignment); |
19105 | verifyFormat("something = 2000;\n" |
19106 | "int i = 1, j = 10;", |
19107 | Alignment); |
19108 | verifyFormat("something = 2000;\n" |
19109 | "another = 911;\n" |
19110 | "int i = 1, j = 10;\n" |
19111 | "oneMore = 1;\n" |
19112 | "i = 2;", |
19113 | Alignment); |
19114 | verifyFormat("int a = 5;\n" |
19115 | "int one = 1;\n" |
19116 | "method();\n" |
19117 | "int oneTwoThree = 123;\n" |
19118 | "int oneTwo = 12;", |
19119 | Alignment); |
19120 | verifyFormat("int oneTwoThree = 123;\n" |
19121 | "int oneTwo = 12;\n" |
19122 | "method();", |
19123 | Alignment); |
19124 | verifyFormat("int oneTwoThree = 123; // comment\n" |
19125 | "int oneTwo = 12; // comment", |
19126 | Alignment); |
19127 | |
19128 | // Bug 25167 |
19129 | /* Uncomment when fixed |
19130 | verifyFormat("#if A\n" |
19131 | "#else\n" |
19132 | "int aaaaaaaa = 12;\n" |
19133 | "#endif\n" |
19134 | "#if B\n" |
19135 | "#else\n" |
19136 | "int a = 12;\n" |
19137 | "#endif", |
19138 | Alignment); |
19139 | verifyFormat("enum foo {\n" |
19140 | "#if A\n" |
19141 | "#else\n" |
19142 | " aaaaaaaa = 12;\n" |
19143 | "#endif\n" |
19144 | "#if B\n" |
19145 | "#else\n" |
19146 | " a = 12;\n" |
19147 | "#endif\n" |
19148 | "};", |
19149 | Alignment); |
19150 | */ |
19151 | |
19152 | Alignment.MaxEmptyLinesToKeep = 10; |
19153 | /* Test alignment across empty lines */ |
19154 | verifyFormat("int a = 5;\n" |
19155 | "\n" |
19156 | "int oneTwoThree = 123;", |
19157 | "int a = 5;\n" |
19158 | "\n" |
19159 | "int oneTwoThree= 123;", |
19160 | Alignment); |
19161 | verifyFormat("int a = 5;\n" |
19162 | "int one = 1;\n" |
19163 | "\n" |
19164 | "int oneTwoThree = 123;", |
19165 | "int a = 5;\n" |
19166 | "int one = 1;\n" |
19167 | "\n" |
19168 | "int oneTwoThree = 123;", |
19169 | Alignment); |
19170 | verifyFormat("int a = 5;\n" |
19171 | "int one = 1;\n" |
19172 | "\n" |
19173 | "int oneTwoThree = 123;\n" |
19174 | "int oneTwo = 12;", |
19175 | "int a = 5;\n" |
19176 | "int one = 1;\n" |
19177 | "\n" |
19178 | "int oneTwoThree = 123;\n" |
19179 | "int oneTwo = 12;", |
19180 | Alignment); |
19181 | |
19182 | /* Test across comments */ |
19183 | verifyFormat("int a = 5;\n" |
19184 | "/* block comment */\n" |
19185 | "int oneTwoThree = 123;", |
19186 | "int a = 5;\n" |
19187 | "/* block comment */\n" |
19188 | "int oneTwoThree=123;", |
19189 | Alignment); |
19190 | |
19191 | verifyFormat("int a = 5;\n" |
19192 | "// line comment\n" |
19193 | "int oneTwoThree = 123;", |
19194 | "int a = 5;\n" |
19195 | "// line comment\n" |
19196 | "int oneTwoThree=123;", |
19197 | Alignment); |
19198 | |
19199 | /* Test across comments and newlines */ |
19200 | verifyFormat("int a = 5;\n" |
19201 | "\n" |
19202 | "/* block comment */\n" |
19203 | "int oneTwoThree = 123;", |
19204 | "int a = 5;\n" |
19205 | "\n" |
19206 | "/* block comment */\n" |
19207 | "int oneTwoThree=123;", |
19208 | Alignment); |
19209 | |
19210 | verifyFormat("int a = 5;\n" |
19211 | "\n" |
19212 | "// line comment\n" |
19213 | "int oneTwoThree = 123;", |
19214 | "int a = 5;\n" |
19215 | "\n" |
19216 | "// line comment\n" |
19217 | "int oneTwoThree=123;", |
19218 | Alignment); |
19219 | |
19220 | verifyFormat("int a = 5;\n" |
19221 | "//\n" |
19222 | "// multi-line line comment\n" |
19223 | "//\n" |
19224 | "int oneTwoThree = 123;", |
19225 | "int a = 5;\n" |
19226 | "//\n" |
19227 | "// multi-line line comment\n" |
19228 | "//\n" |
19229 | "int oneTwoThree=123;", |
19230 | Alignment); |
19231 | |
19232 | verifyFormat("int a = 5;\n" |
19233 | "/*\n" |
19234 | " * multi-line block comment\n" |
19235 | " */\n" |
19236 | "int oneTwoThree = 123;", |
19237 | "int a = 5;\n" |
19238 | "/*\n" |
19239 | " * multi-line block comment\n" |
19240 | " */\n" |
19241 | "int oneTwoThree=123;", |
19242 | Alignment); |
19243 | |
19244 | verifyFormat("int a = 5;\n" |
19245 | "\n" |
19246 | "/* block comment */\n" |
19247 | "\n" |
19248 | "\n" |
19249 | "\n" |
19250 | "int oneTwoThree = 123;", |
19251 | "int a = 5;\n" |
19252 | "\n" |
19253 | "/* block comment */\n" |
19254 | "\n" |
19255 | "\n" |
19256 | "\n" |
19257 | "int oneTwoThree=123;", |
19258 | Alignment); |
19259 | |
19260 | verifyFormat("int a = 5;\n" |
19261 | "\n" |
19262 | "// line comment\n" |
19263 | "\n" |
19264 | "\n" |
19265 | "\n" |
19266 | "int oneTwoThree = 123;", |
19267 | "int a = 5;\n" |
19268 | "\n" |
19269 | "// line comment\n" |
19270 | "\n" |
19271 | "\n" |
19272 | "\n" |
19273 | "int oneTwoThree=123;", |
19274 | Alignment); |
19275 | |
19276 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; |
19277 | verifyFormat("#define A \\\n" |
19278 | " int aaaa = 12; \\\n" |
19279 | " int b = 23; \\\n" |
19280 | " int ccc = 234; \\\n" |
19281 | " int dddddddddd = 2345;", |
19282 | Alignment); |
19283 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
19284 | verifyFormat("#define A \\\n" |
19285 | " int aaaa = 12; \\\n" |
19286 | " int b = 23; \\\n" |
19287 | " int ccc = 234; \\\n" |
19288 | " int dddddddddd = 2345;", |
19289 | Alignment); |
19290 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; |
19291 | verifyFormat("#define A " |
19292 | " \\\n" |
19293 | " int aaaa = 12; " |
19294 | " \\\n" |
19295 | " int b = 23; " |
19296 | " \\\n" |
19297 | " int ccc = 234; " |
19298 | " \\\n" |
19299 | " int dddddddddd = 2345;", |
19300 | Alignment); |
19301 | verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " |
19302 | "k = 4, int l = 5,\n" |
19303 | " int m = 6) {\n" |
19304 | " int j = 10;\n" |
19305 | " otherThing = 1;\n" |
19306 | "}", |
19307 | Alignment); |
19308 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
19309 | " int i = 1;\n" |
19310 | " int j = 2;\n" |
19311 | " int big = 10000;\n" |
19312 | "}", |
19313 | Alignment); |
19314 | verifyFormat("class C {\n" |
19315 | "public:\n" |
19316 | " int i = 1;\n" |
19317 | " virtual void f() = 0;\n" |
19318 | "};", |
19319 | Alignment); |
19320 | verifyFormat("int i = 1;\n" |
19321 | "if (SomeType t = getSomething()) {\n" |
19322 | "}\n" |
19323 | "int j = 2;\n" |
19324 | "int big = 10000;", |
19325 | Alignment); |
19326 | verifyFormat("int j = 7;\n" |
19327 | "for (int k = 0; k < N; ++k) {\n" |
19328 | "}\n" |
19329 | "int j = 2;\n" |
19330 | "int big = 10000;\n" |
19331 | "}", |
19332 | Alignment); |
19333 | Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
19334 | verifyFormat("int i = 1;\n" |
19335 | "LooooooooooongType loooooooooooooooooooooongVariable\n" |
19336 | " = someLooooooooooooooooongFunction();\n" |
19337 | "int j = 2;", |
19338 | Alignment); |
19339 | Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
19340 | verifyFormat("int i = 1;\n" |
19341 | "LooooooooooongType loooooooooooooooooooooongVariable =\n" |
19342 | " someLooooooooooooooooongFunction();\n" |
19343 | "int j = 2;", |
19344 | Alignment); |
19345 | |
19346 | verifyFormat("auto lambda = []() {\n" |
19347 | " auto i = 0;\n" |
19348 | " return 0;\n" |
19349 | "};\n" |
19350 | "int i = 0;\n" |
19351 | "auto v = type{\n" |
19352 | " i = 1, //\n" |
19353 | " (i = 2), //\n" |
19354 | " i = 3 //\n" |
19355 | "};", |
19356 | Alignment); |
19357 | |
19358 | verifyFormat( |
19359 | "int i = 1;\n" |
19360 | "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" |
19361 | " loooooooooooooooooooooongParameterB);\n" |
19362 | "int j = 2;", |
19363 | Alignment); |
19364 | |
19365 | verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" |
19366 | " typename B = very_long_type_name_1,\n" |
19367 | " typename T_2 = very_long_type_name_2>\n" |
19368 | "auto foo() {}", |
19369 | Alignment); |
19370 | verifyFormat("int a, b = 1;\n" |
19371 | "int c = 2;\n" |
19372 | "int dd = 3;", |
19373 | Alignment); |
19374 | verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" |
19375 | "float b[1][] = {{3.f}};", |
19376 | Alignment); |
19377 | verifyFormat("for (int i = 0; i < 1; i++)\n" |
19378 | " int x = 1;", |
19379 | Alignment); |
19380 | verifyFormat("for (i = 0; i < 1; i++)\n" |
19381 | " x = 1;\n" |
19382 | "y = 1;", |
19383 | Alignment); |
19384 | |
19385 | Alignment.ReflowComments = FormatStyle::RCS_Always; |
19386 | Alignment.ColumnLimit = 50; |
19387 | verifyFormat("int x = 0;\n" |
19388 | "int yy = 1; /// specificlennospace\n" |
19389 | "int zzz = 2;", |
19390 | "int x = 0;\n" |
19391 | "int yy = 1; ///specificlennospace\n" |
19392 | "int zzz = 2;", |
19393 | Alignment); |
19394 | } |
19395 | |
19396 | TEST_F(FormatTest, AlignCompoundAssignments) { |
19397 | FormatStyle Alignment = getLLVMStyle(); |
19398 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
19399 | Alignment.AlignConsecutiveAssignments.AlignCompound = true; |
19400 | Alignment.AlignConsecutiveAssignments.PadOperators = false; |
19401 | verifyFormat("sfdbddfbdfbb = 5;\n" |
19402 | "dvsdsv = 5;\n" |
19403 | "int dsvvdvsdvvv = 123;", |
19404 | Alignment); |
19405 | verifyFormat("sfdbddfbdfbb ^= 5;\n" |
19406 | "dvsdsv |= 5;\n" |
19407 | "int dsvvdvsdvvv = 123;", |
19408 | Alignment); |
19409 | verifyFormat("sfdbddfbdfbb ^= 5;\n" |
19410 | "dvsdsv <<= 5;\n" |
19411 | "int dsvvdvsdvvv = 123;", |
19412 | Alignment); |
19413 | verifyFormat("int xxx = 5;\n" |
19414 | "xxx = 5;\n" |
19415 | "{\n" |
19416 | " int yyy = 6;\n" |
19417 | " yyy = 6;\n" |
19418 | "}", |
19419 | Alignment); |
19420 | verifyFormat("int xxx = 5;\n" |
19421 | "xxx += 5;\n" |
19422 | "{\n" |
19423 | " int yyy = 6;\n" |
19424 | " yyy += 6;\n" |
19425 | "}", |
19426 | Alignment); |
19427 | // Test that `<=` is not treated as a compound assignment. |
19428 | verifyFormat("aa &= 5;\n" |
19429 | "b <= 10;\n" |
19430 | "c = 15;", |
19431 | Alignment); |
19432 | Alignment.AlignConsecutiveAssignments.PadOperators = true; |
19433 | verifyFormat("sfdbddfbdfbb = 5;\n" |
19434 | "dvsdsv = 5;\n" |
19435 | "int dsvvdvsdvvv = 123;", |
19436 | Alignment); |
19437 | verifyFormat("sfdbddfbdfbb ^= 5;\n" |
19438 | "dvsdsv |= 5;\n" |
19439 | "int dsvvdvsdvvv = 123;", |
19440 | Alignment); |
19441 | verifyFormat("sfdbddfbdfbb ^= 5;\n" |
19442 | "dvsdsv <<= 5;\n" |
19443 | "int dsvvdvsdvvv = 123;", |
19444 | Alignment); |
19445 | verifyFormat("a += 5;\n" |
19446 | "one = 1;\n" |
19447 | "\n" |
19448 | "oneTwoThree = 123;", |
19449 | "a += 5;\n" |
19450 | "one = 1;\n" |
19451 | "\n" |
19452 | "oneTwoThree = 123;", |
19453 | Alignment); |
19454 | verifyFormat("a += 5;\n" |
19455 | "one = 1;\n" |
19456 | "//\n" |
19457 | "oneTwoThree = 123;", |
19458 | "a += 5;\n" |
19459 | "one = 1;\n" |
19460 | "//\n" |
19461 | "oneTwoThree = 123;", |
19462 | Alignment); |
19463 | Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; |
19464 | verifyFormat("a += 5;\n" |
19465 | "one = 1;\n" |
19466 | "\n" |
19467 | "oneTwoThree = 123;", |
19468 | "a += 5;\n" |
19469 | "one = 1;\n" |
19470 | "\n" |
19471 | "oneTwoThree = 123;", |
19472 | Alignment); |
19473 | verifyFormat("a += 5;\n" |
19474 | "one = 1;\n" |
19475 | "//\n" |
19476 | "oneTwoThree = 123;", |
19477 | "a += 5;\n" |
19478 | "one = 1;\n" |
19479 | "//\n" |
19480 | "oneTwoThree = 123;", |
19481 | Alignment); |
19482 | Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false; |
19483 | Alignment.AlignConsecutiveAssignments.AcrossComments = true; |
19484 | verifyFormat("a += 5;\n" |
19485 | "one = 1;\n" |
19486 | "\n" |
19487 | "oneTwoThree = 123;", |
19488 | "a += 5;\n" |
19489 | "one = 1;\n" |
19490 | "\n" |
19491 | "oneTwoThree = 123;", |
19492 | Alignment); |
19493 | verifyFormat("a += 5;\n" |
19494 | "one = 1;\n" |
19495 | "//\n" |
19496 | "oneTwoThree = 123;", |
19497 | "a += 5;\n" |
19498 | "one = 1;\n" |
19499 | "//\n" |
19500 | "oneTwoThree = 123;", |
19501 | Alignment); |
19502 | Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; |
19503 | verifyFormat("a += 5;\n" |
19504 | "one >>= 1;\n" |
19505 | "\n" |
19506 | "oneTwoThree = 123;", |
19507 | "a += 5;\n" |
19508 | "one >>= 1;\n" |
19509 | "\n" |
19510 | "oneTwoThree = 123;", |
19511 | Alignment); |
19512 | verifyFormat("a += 5;\n" |
19513 | "one = 1;\n" |
19514 | "//\n" |
19515 | "oneTwoThree <<= 123;", |
19516 | "a += 5;\n" |
19517 | "one = 1;\n" |
19518 | "//\n" |
19519 | "oneTwoThree <<= 123;", |
19520 | Alignment); |
19521 | } |
19522 | |
19523 | TEST_F(FormatTest, AlignConsecutiveAssignments) { |
19524 | FormatStyle Alignment = getLLVMStyle(); |
19525 | Alignment.AlignConsecutiveMacros.Enabled = true; |
19526 | verifyFormat("int a = 5;\n" |
19527 | "int oneTwoThree = 123;", |
19528 | Alignment); |
19529 | verifyFormat("int a = 5;\n" |
19530 | "int oneTwoThree = 123;", |
19531 | Alignment); |
19532 | |
19533 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
19534 | verifyFormat("int a = 5;\n" |
19535 | "int oneTwoThree = 123;", |
19536 | Alignment); |
19537 | verifyFormat("int a = method();\n" |
19538 | "int oneTwoThree = 133;", |
19539 | Alignment); |
19540 | verifyFormat("aa <= 5;\n" |
19541 | "a &= 5;\n" |
19542 | "bcd *= 5;\n" |
19543 | "ghtyf += 5;\n" |
19544 | "dvfvdb -= 5;\n" |
19545 | "a /= 5;\n" |
19546 | "vdsvsv %= 5;\n" |
19547 | "sfdbddfbdfbb ^= 5;\n" |
19548 | "dvsdsv |= 5;\n" |
19549 | "int dsvvdvsdvvv = 123;", |
19550 | Alignment); |
19551 | verifyFormat("int i = 1, j = 10;\n" |
19552 | "something = 2000;", |
19553 | Alignment); |
19554 | verifyFormat("something = 2000;\n" |
19555 | "int i = 1, j = 10;", |
19556 | Alignment); |
19557 | verifyFormat("something = 2000;\n" |
19558 | "another = 911;\n" |
19559 | "int i = 1, j = 10;\n" |
19560 | "oneMore = 1;\n" |
19561 | "i = 2;", |
19562 | Alignment); |
19563 | verifyFormat("int a = 5;\n" |
19564 | "int one = 1;\n" |
19565 | "method();\n" |
19566 | "int oneTwoThree = 123;\n" |
19567 | "int oneTwo = 12;", |
19568 | Alignment); |
19569 | verifyFormat("int oneTwoThree = 123;\n" |
19570 | "int oneTwo = 12;\n" |
19571 | "method();", |
19572 | Alignment); |
19573 | verifyFormat("int oneTwoThree = 123; // comment\n" |
19574 | "int oneTwo = 12; // comment", |
19575 | Alignment); |
19576 | verifyFormat("int f() = default;\n" |
19577 | "int &operator() = default;\n" |
19578 | "int &operator=() {", |
19579 | Alignment); |
19580 | verifyFormat("int f() = delete;\n" |
19581 | "int &operator() = delete;\n" |
19582 | "int &operator=() {", |
19583 | Alignment); |
19584 | verifyFormat("int f() = default; // comment\n" |
19585 | "int &operator() = default; // comment\n" |
19586 | "int &operator=() {", |
19587 | Alignment); |
19588 | verifyFormat("int f() = default;\n" |
19589 | "int &operator() = default;\n" |
19590 | "int &operator==() {", |
19591 | Alignment); |
19592 | verifyFormat("int f() = default;\n" |
19593 | "int &operator() = default;\n" |
19594 | "int &operator<=() {", |
19595 | Alignment); |
19596 | verifyFormat("int f() = default;\n" |
19597 | "int &operator() = default;\n" |
19598 | "int &operator!=() {", |
19599 | Alignment); |
19600 | verifyFormat("int f() = default;\n" |
19601 | "int &operator() = default;\n" |
19602 | "int &operator=();", |
19603 | Alignment); |
19604 | verifyFormat("int f() = delete;\n" |
19605 | "int &operator() = delete;\n" |
19606 | "int &operator=();", |
19607 | Alignment); |
19608 | verifyFormat("/* long long padding */ int f() = default;\n" |
19609 | "int &operator() = default;\n" |
19610 | "int &operator/**/ =();", |
19611 | Alignment); |
19612 | // https://llvm.org/PR33697 |
19613 | FormatStyle AlignmentWithPenalty = getLLVMStyle(); |
19614 | AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true; |
19615 | AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; |
19616 | verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" |
19617 | " void f() = delete;\n" |
19618 | " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" |
19619 | " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" |
19620 | "};", |
19621 | AlignmentWithPenalty); |
19622 | |
19623 | // Bug 25167 |
19624 | /* Uncomment when fixed |
19625 | verifyFormat("#if A\n" |
19626 | "#else\n" |
19627 | "int aaaaaaaa = 12;\n" |
19628 | "#endif\n" |
19629 | "#if B\n" |
19630 | "#else\n" |
19631 | "int a = 12;\n" |
19632 | "#endif", |
19633 | Alignment); |
19634 | verifyFormat("enum foo {\n" |
19635 | "#if A\n" |
19636 | "#else\n" |
19637 | " aaaaaaaa = 12;\n" |
19638 | "#endif\n" |
19639 | "#if B\n" |
19640 | "#else\n" |
19641 | " a = 12;\n" |
19642 | "#endif\n" |
19643 | "};", |
19644 | Alignment); |
19645 | */ |
19646 | |
19647 | verifyFormat("int a = 5;\n" |
19648 | "\n" |
19649 | "int oneTwoThree = 123;", |
19650 | "int a = 5;\n" |
19651 | "\n" |
19652 | "int oneTwoThree= 123;", |
19653 | Alignment); |
19654 | verifyFormat("int a = 5;\n" |
19655 | "int one = 1;\n" |
19656 | "\n" |
19657 | "int oneTwoThree = 123;", |
19658 | "int a = 5;\n" |
19659 | "int one = 1;\n" |
19660 | "\n" |
19661 | "int oneTwoThree = 123;", |
19662 | Alignment); |
19663 | verifyFormat("int a = 5;\n" |
19664 | "int one = 1;\n" |
19665 | "\n" |
19666 | "int oneTwoThree = 123;\n" |
19667 | "int oneTwo = 12;", |
19668 | "int a = 5;\n" |
19669 | "int one = 1;\n" |
19670 | "\n" |
19671 | "int oneTwoThree = 123;\n" |
19672 | "int oneTwo = 12;", |
19673 | Alignment); |
19674 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; |
19675 | verifyFormat("#define A \\\n" |
19676 | " int aaaa = 12; \\\n" |
19677 | " int b = 23; \\\n" |
19678 | " int ccc = 234; \\\n" |
19679 | " int dddddddddd = 2345;", |
19680 | Alignment); |
19681 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
19682 | verifyFormat("#define A \\\n" |
19683 | " int aaaa = 12; \\\n" |
19684 | " int b = 23; \\\n" |
19685 | " int ccc = 234; \\\n" |
19686 | " int dddddddddd = 2345;", |
19687 | Alignment); |
19688 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; |
19689 | verifyFormat("#define A " |
19690 | " \\\n" |
19691 | " int aaaa = 12; " |
19692 | " \\\n" |
19693 | " int b = 23; " |
19694 | " \\\n" |
19695 | " int ccc = 234; " |
19696 | " \\\n" |
19697 | " int dddddddddd = 2345;", |
19698 | Alignment); |
19699 | verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " |
19700 | "k = 4, int l = 5,\n" |
19701 | " int m = 6) {\n" |
19702 | " int j = 10;\n" |
19703 | " otherThing = 1;\n" |
19704 | "}", |
19705 | Alignment); |
19706 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
19707 | " int i = 1;\n" |
19708 | " int j = 2;\n" |
19709 | " int big = 10000;\n" |
19710 | "}", |
19711 | Alignment); |
19712 | verifyFormat("class C {\n" |
19713 | "public:\n" |
19714 | " int i = 1;\n" |
19715 | " virtual void f() = 0;\n" |
19716 | "};", |
19717 | Alignment); |
19718 | verifyFormat("int i = 1;\n" |
19719 | "if (SomeType t = getSomething()) {\n" |
19720 | "}\n" |
19721 | "int j = 2;\n" |
19722 | "int big = 10000;", |
19723 | Alignment); |
19724 | verifyFormat("int j = 7;\n" |
19725 | "for (int k = 0; k < N; ++k) {\n" |
19726 | "}\n" |
19727 | "int j = 2;\n" |
19728 | "int big = 10000;\n" |
19729 | "}", |
19730 | Alignment); |
19731 | Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
19732 | verifyFormat("int i = 1;\n" |
19733 | "LooooooooooongType loooooooooooooooooooooongVariable\n" |
19734 | " = someLooooooooooooooooongFunction();\n" |
19735 | "int j = 2;", |
19736 | Alignment); |
19737 | Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
19738 | verifyFormat("int i = 1;\n" |
19739 | "LooooooooooongType loooooooooooooooooooooongVariable =\n" |
19740 | " someLooooooooooooooooongFunction();\n" |
19741 | "int j = 2;", |
19742 | Alignment); |
19743 | |
19744 | verifyFormat("auto lambda = []() {\n" |
19745 | " auto i = 0;\n" |
19746 | " return 0;\n" |
19747 | "};\n" |
19748 | "int i = 0;\n" |
19749 | "auto v = type{\n" |
19750 | " i = 1, //\n" |
19751 | " (i = 2), //\n" |
19752 | " i = 3 //\n" |
19753 | "};", |
19754 | Alignment); |
19755 | |
19756 | verifyFormat( |
19757 | "int i = 1;\n" |
19758 | "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" |
19759 | " loooooooooooooooooooooongParameterB);\n" |
19760 | "int j = 2;", |
19761 | Alignment); |
19762 | |
19763 | verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" |
19764 | " typename B = very_long_type_name_1,\n" |
19765 | " typename T_2 = very_long_type_name_2>\n" |
19766 | "auto foo() {}", |
19767 | Alignment); |
19768 | verifyFormat("int a, b = 1;\n" |
19769 | "int c = 2;\n" |
19770 | "int dd = 3;", |
19771 | Alignment); |
19772 | verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" |
19773 | "float b[1][] = {{3.f}};", |
19774 | Alignment); |
19775 | verifyFormat("for (int i = 0; i < 1; i++)\n" |
19776 | " int x = 1;", |
19777 | Alignment); |
19778 | verifyFormat("for (i = 0; i < 1; i++)\n" |
19779 | " x = 1;\n" |
19780 | "y = 1;", |
19781 | Alignment); |
19782 | |
19783 | EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always); |
19784 | Alignment.ColumnLimit = 50; |
19785 | verifyFormat("int x = 0;\n" |
19786 | "int yy = 1; /// specificlennospace\n" |
19787 | "int zzz = 2;", |
19788 | "int x = 0;\n" |
19789 | "int yy = 1; ///specificlennospace\n" |
19790 | "int zzz = 2;", |
19791 | Alignment); |
19792 | |
19793 | verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" |
19794 | "auto b = [] {\n" |
19795 | " f();\n" |
19796 | " return;\n" |
19797 | "};", |
19798 | Alignment); |
19799 | verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" |
19800 | "auto b = g([] {\n" |
19801 | " f();\n" |
19802 | " return;\n" |
19803 | "});", |
19804 | Alignment); |
19805 | verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" |
19806 | "auto b = g(param, [] {\n" |
19807 | " f();\n" |
19808 | " return;\n" |
19809 | "});", |
19810 | Alignment); |
19811 | verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" |
19812 | "auto b = [] {\n" |
19813 | " if (condition) {\n" |
19814 | " return;\n" |
19815 | " }\n" |
19816 | "};", |
19817 | Alignment); |
19818 | |
19819 | verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
19820 | " ccc ? aaaaa : bbbbb,\n" |
19821 | " dddddddddddddddddddddddddd);", |
19822 | Alignment); |
19823 | // FIXME: https://llvm.org/PR53497 |
19824 | // verifyFormat("auto aaaaaaaaaaaa = f();\n" |
19825 | // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
19826 | // " ccc ? aaaaa : bbbbb,\n" |
19827 | // " dddddddddddddddddddddddddd);", |
19828 | // Alignment); |
19829 | |
19830 | // Confirm proper handling of AlignConsecutiveAssignments with |
19831 | // BinPackArguments. |
19832 | // See https://llvm.org/PR55360 |
19833 | Alignment = getLLVMStyleWithColumns(ColumnLimit: 50); |
19834 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
19835 | Alignment.BinPackArguments = false; |
19836 | verifyFormat("int a_long_name = 1;\n" |
19837 | "auto b = B({a_long_name, a_long_name},\n" |
19838 | " {a_longer_name_for_wrap,\n" |
19839 | " a_longer_name_for_wrap});", |
19840 | Alignment); |
19841 | verifyFormat("int a_long_name = 1;\n" |
19842 | "auto b = B{{a_long_name, a_long_name},\n" |
19843 | " {a_longer_name_for_wrap,\n" |
19844 | " a_longer_name_for_wrap}};", |
19845 | Alignment); |
19846 | |
19847 | Alignment = getLLVMStyleWithColumns(ColumnLimit: 60); |
19848 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
19849 | verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n" |
19850 | "using I = std::conditional_t<II::value >= 0,\n" |
19851 | " std::ic<int, II::value + 1>,\n" |
19852 | " std::ic<int, -1>>;", |
19853 | Alignment); |
19854 | verifyFormat("SomeName = Foo;\n" |
19855 | "X = func<Type, Type>(looooooooooooooooooooooooong,\n" |
19856 | " arrrrrrrrrrg);", |
19857 | Alignment); |
19858 | |
19859 | Alignment.ColumnLimit = 80; |
19860 | Alignment.SpacesInAngles = FormatStyle::SIAS_Always; |
19861 | verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n" |
19862 | "ptr = reinterpret_cast< void ** >(ptr[0]);", |
19863 | Alignment); |
19864 | verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n" |
19865 | "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));", |
19866 | Alignment); |
19867 | |
19868 | Alignment.SpacesInParens = FormatStyle::SIPO_Custom; |
19869 | Alignment.SpacesInParensOptions.InCStyleCasts = true; |
19870 | verifyFormat("void **ptr = ( void ** )unkn;\n" |
19871 | "ptr = ( void ** )ptr[0];", |
19872 | Alignment); |
19873 | verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n" |
19874 | "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);", |
19875 | Alignment); |
19876 | } |
19877 | |
19878 | TEST_F(FormatTest, AlignConsecutiveBitFields) { |
19879 | FormatStyle Alignment = getLLVMStyle(); |
19880 | Alignment.AlignConsecutiveBitFields.Enabled = true; |
19881 | verifyFormat("int const a : 5;\n" |
19882 | "int oneTwoThree : 23;", |
19883 | Alignment); |
19884 | |
19885 | // Initializers are allowed starting with c++2a |
19886 | verifyFormat("int const a : 5 = 1;\n" |
19887 | "int oneTwoThree : 23 = 0;", |
19888 | Alignment); |
19889 | |
19890 | Alignment.AlignConsecutiveDeclarations.Enabled = true; |
19891 | verifyFormat("int const a : 5;\n" |
19892 | "int oneTwoThree : 23;", |
19893 | Alignment); |
19894 | |
19895 | verifyFormat("int const a : 5; // comment\n" |
19896 | "int oneTwoThree : 23; // comment", |
19897 | Alignment); |
19898 | |
19899 | verifyFormat("int const a : 5 = 1;\n" |
19900 | "int oneTwoThree : 23 = 0;", |
19901 | Alignment); |
19902 | |
19903 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
19904 | verifyFormat("int const a : 5 = 1;\n" |
19905 | "int oneTwoThree : 23 = 0;", |
19906 | Alignment); |
19907 | verifyFormat("int const a : 5 = {1};\n" |
19908 | "int oneTwoThree : 23 = 0;", |
19909 | Alignment); |
19910 | |
19911 | Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; |
19912 | verifyFormat("int const a :5;\n" |
19913 | "int oneTwoThree:23;", |
19914 | Alignment); |
19915 | |
19916 | Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; |
19917 | verifyFormat("int const a :5;\n" |
19918 | "int oneTwoThree :23;", |
19919 | Alignment); |
19920 | |
19921 | Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; |
19922 | verifyFormat("int const a : 5;\n" |
19923 | "int oneTwoThree: 23;", |
19924 | Alignment); |
19925 | |
19926 | // Known limitations: ':' is only recognized as a bitfield colon when |
19927 | // followed by a number. |
19928 | /* |
19929 | verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" |
19930 | "int a : 5;", |
19931 | Alignment); |
19932 | */ |
19933 | } |
19934 | |
19935 | TEST_F(FormatTest, AlignConsecutiveDeclarations) { |
19936 | FormatStyle Alignment = getLLVMStyle(); |
19937 | Alignment.AlignConsecutiveMacros.Enabled = true; |
19938 | Alignment.PointerAlignment = FormatStyle::PAS_Right; |
19939 | verifyFormat("float const a = 5;\n" |
19940 | "int oneTwoThree = 123;", |
19941 | Alignment); |
19942 | verifyFormat("int a = 5;\n" |
19943 | "float const oneTwoThree = 123;", |
19944 | Alignment); |
19945 | |
19946 | Alignment.AlignConsecutiveDeclarations.Enabled = true; |
19947 | verifyFormat("float const a = 5;\n" |
19948 | "int oneTwoThree = 123;", |
19949 | Alignment); |
19950 | verifyFormat("int a = method();\n" |
19951 | "float const oneTwoThree = 133;", |
19952 | Alignment); |
19953 | verifyFormat("int i = 1, j = 10;\n" |
19954 | "something = 2000;", |
19955 | Alignment); |
19956 | verifyFormat("something = 2000;\n" |
19957 | "int i = 1, j = 10;", |
19958 | Alignment); |
19959 | verifyFormat("float something = 2000;\n" |
19960 | "double another = 911;\n" |
19961 | "int i = 1, j = 10;\n" |
19962 | "const int *oneMore = 1;\n" |
19963 | "unsigned i = 2;", |
19964 | Alignment); |
19965 | verifyFormat("float a = 5;\n" |
19966 | "int one = 1;\n" |
19967 | "method();\n" |
19968 | "const double oneTwoThree = 123;\n" |
19969 | "const unsigned int oneTwo = 12;", |
19970 | Alignment); |
19971 | verifyFormat("int oneTwoThree{0}; // comment\n" |
19972 | "unsigned oneTwo; // comment", |
19973 | Alignment); |
19974 | verifyFormat("unsigned int *a;\n" |
19975 | "int *b;\n" |
19976 | "unsigned int Const *c;\n" |
19977 | "unsigned int const *d;\n" |
19978 | "unsigned int Const &e;\n" |
19979 | "unsigned int const &f;", |
19980 | Alignment); |
19981 | verifyFormat("Const unsigned int *c;\n" |
19982 | "const unsigned int *d;\n" |
19983 | "Const unsigned int &e;\n" |
19984 | "const unsigned int &f;\n" |
19985 | "const unsigned g;\n" |
19986 | "Const unsigned h;", |
19987 | Alignment); |
19988 | verifyFormat("float const a = 5;\n" |
19989 | "\n" |
19990 | "int oneTwoThree = 123;", |
19991 | "float const a = 5;\n" |
19992 | "\n" |
19993 | "int oneTwoThree= 123;", |
19994 | Alignment); |
19995 | verifyFormat("float a = 5;\n" |
19996 | "int one = 1;\n" |
19997 | "\n" |
19998 | "unsigned oneTwoThree = 123;", |
19999 | "float a = 5;\n" |
20000 | "int one = 1;\n" |
20001 | "\n" |
20002 | "unsigned oneTwoThree = 123;", |
20003 | Alignment); |
20004 | verifyFormat("float a = 5;\n" |
20005 | "int one = 1;\n" |
20006 | "\n" |
20007 | "unsigned oneTwoThree = 123;\n" |
20008 | "int oneTwo = 12;", |
20009 | "float a = 5;\n" |
20010 | "int one = 1;\n" |
20011 | "\n" |
20012 | "unsigned oneTwoThree = 123;\n" |
20013 | "int oneTwo = 12;", |
20014 | Alignment); |
20015 | // Function prototype alignment |
20016 | verifyFormat("int a();\n" |
20017 | "double b();", |
20018 | Alignment); |
20019 | verifyFormat("int a(int x);\n" |
20020 | "double b();", |
20021 | Alignment); |
20022 | verifyFormat("int a(const Test & = Test());\n" |
20023 | "int a1(int &foo, const Test & = Test());\n" |
20024 | "int a2(int &foo, const Test &name = Test());\n" |
20025 | "double b();", |
20026 | Alignment); |
20027 | verifyFormat("struct Test {\n" |
20028 | " Test(const Test &) = default;\n" |
20029 | " ~Test() = default;\n" |
20030 | " Test &operator=(const Test &) = default;\n" |
20031 | "};", |
20032 | Alignment); |
20033 | unsigned OldColumnLimit = Alignment.ColumnLimit; |
20034 | // We need to set ColumnLimit to zero, in order to stress nested alignments, |
20035 | // otherwise the function parameters will be re-flowed onto a single line. |
20036 | Alignment.ColumnLimit = 0; |
20037 | verifyFormat("int a(int x,\n" |
20038 | " float y);\n" |
20039 | "double b(int x,\n" |
20040 | " double y);", |
20041 | "int a(int x,\n" |
20042 | " float y);\n" |
20043 | "double b(int x,\n" |
20044 | " double y);", |
20045 | Alignment); |
20046 | // This ensures that function parameters of function declarations are |
20047 | // correctly indented when their owning functions are indented. |
20048 | // The failure case here is for 'double y' to not be indented enough. |
20049 | verifyFormat("double a(int x);\n" |
20050 | "int b(int y,\n" |
20051 | " double z);", |
20052 | "double a(int x);\n" |
20053 | "int b(int y,\n" |
20054 | " double z);", |
20055 | Alignment); |
20056 | // Set ColumnLimit low so that we induce wrapping immediately after |
20057 | // the function name and opening paren. |
20058 | Alignment.ColumnLimit = 13; |
20059 | verifyFormat("int function(\n" |
20060 | " int x,\n" |
20061 | " bool y);", |
20062 | Alignment); |
20063 | // Set ColumnLimit low so that we break the argument list in multiple lines. |
20064 | Alignment.ColumnLimit = 35; |
20065 | verifyFormat("int a3(SomeTypeName1 &x,\n" |
20066 | " SomeTypeName2 &y,\n" |
20067 | " const Test & = Test());\n" |
20068 | "double b();", |
20069 | Alignment); |
20070 | Alignment.ColumnLimit = OldColumnLimit; |
20071 | // Ensure function pointers don't screw up recursive alignment |
20072 | verifyFormat("int a(int x, void (*fp)(int y));\n" |
20073 | "double b();", |
20074 | Alignment); |
20075 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
20076 | verifyFormat("struct Test {\n" |
20077 | " Test(const Test &) = default;\n" |
20078 | " ~Test() = default;\n" |
20079 | " Test &operator=(const Test &) = default;\n" |
20080 | "};", |
20081 | Alignment); |
20082 | // Ensure recursive alignment is broken by function braces, so that the |
20083 | // "a = 1" does not align with subsequent assignments inside the function |
20084 | // body. |
20085 | verifyFormat("int func(int a = 1) {\n" |
20086 | " int b = 2;\n" |
20087 | " int cc = 3;\n" |
20088 | "}", |
20089 | Alignment); |
20090 | verifyFormat("float something = 2000;\n" |
20091 | "double another = 911;\n" |
20092 | "int i = 1, j = 10;\n" |
20093 | "const int *oneMore = 1;\n" |
20094 | "unsigned i = 2;", |
20095 | Alignment); |
20096 | verifyFormat("int oneTwoThree = {0}; // comment\n" |
20097 | "unsigned oneTwo = 0; // comment", |
20098 | Alignment); |
20099 | // Make sure that scope is correctly tracked, in the absence of braces |
20100 | verifyFormat("for (int i = 0; i < n; i++)\n" |
20101 | " j = i;\n" |
20102 | "double x = 1;", |
20103 | Alignment); |
20104 | verifyFormat("if (int i = 0)\n" |
20105 | " j = i;\n" |
20106 | "double x = 1;", |
20107 | Alignment); |
20108 | // Ensure operator[] and operator() are comprehended |
20109 | verifyFormat("struct test {\n" |
20110 | " long long int foo();\n" |
20111 | " int operator[](int a);\n" |
20112 | " double bar();\n" |
20113 | "};", |
20114 | Alignment); |
20115 | verifyFormat("struct test {\n" |
20116 | " long long int foo();\n" |
20117 | " int operator()(int a);\n" |
20118 | " double bar();\n" |
20119 | "};", |
20120 | Alignment); |
20121 | // http://llvm.org/PR52914 |
20122 | verifyFormat("char *a[] = {\"a\", // comment\n" |
20123 | " \"bb\"};\n" |
20124 | "int bbbbbbb = 0;", |
20125 | Alignment); |
20126 | // http://llvm.org/PR68079 |
20127 | verifyFormat("using Fn = int (A::*)();\n" |
20128 | "using RFn = int (A::*)() &;\n" |
20129 | "using RRFn = int (A::*)() &&;", |
20130 | Alignment); |
20131 | verifyFormat("using Fn = int (A::*)();\n" |
20132 | "using RFn = int *(A::*)() &;\n" |
20133 | "using RRFn = double (A::*)() &&;", |
20134 | Alignment); |
20135 | |
20136 | // PAS_Right |
20137 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20138 | " int const i = 1;\n" |
20139 | " int *j = 2;\n" |
20140 | " int big = 10000;\n" |
20141 | "\n" |
20142 | " unsigned oneTwoThree = 123;\n" |
20143 | " int oneTwo = 12;\n" |
20144 | " method();\n" |
20145 | " float k = 2;\n" |
20146 | " int ll = 10000;\n" |
20147 | "}", |
20148 | "void SomeFunction(int parameter= 0) {\n" |
20149 | " int const i= 1;\n" |
20150 | " int *j=2;\n" |
20151 | " int big = 10000;\n" |
20152 | "\n" |
20153 | "unsigned oneTwoThree =123;\n" |
20154 | "int oneTwo = 12;\n" |
20155 | " method();\n" |
20156 | "float k= 2;\n" |
20157 | "int ll=10000;\n" |
20158 | "}", |
20159 | Alignment); |
20160 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20161 | " int const i = 1;\n" |
20162 | " int **j = 2, ***k;\n" |
20163 | " int &k = i;\n" |
20164 | " int &&l = i + j;\n" |
20165 | " int big = 10000;\n" |
20166 | "\n" |
20167 | " unsigned oneTwoThree = 123;\n" |
20168 | " int oneTwo = 12;\n" |
20169 | " method();\n" |
20170 | " float k = 2;\n" |
20171 | " int ll = 10000;\n" |
20172 | "}", |
20173 | "void SomeFunction(int parameter= 0) {\n" |
20174 | " int const i= 1;\n" |
20175 | " int **j=2,***k;\n" |
20176 | "int &k=i;\n" |
20177 | "int &&l=i+j;\n" |
20178 | " int big = 10000;\n" |
20179 | "\n" |
20180 | "unsigned oneTwoThree =123;\n" |
20181 | "int oneTwo = 12;\n" |
20182 | " method();\n" |
20183 | "float k= 2;\n" |
20184 | "int ll=10000;\n" |
20185 | "}", |
20186 | Alignment); |
20187 | // variables are aligned at their name, pointers are at the right most |
20188 | // position |
20189 | verifyFormat("int *a;\n" |
20190 | "int **b;\n" |
20191 | "int ***c;\n" |
20192 | "int foobar;", |
20193 | Alignment); |
20194 | |
20195 | // PAS_Left |
20196 | FormatStyle AlignmentLeft = Alignment; |
20197 | AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; |
20198 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20199 | " int const i = 1;\n" |
20200 | " int* j = 2;\n" |
20201 | " int big = 10000;\n" |
20202 | "\n" |
20203 | " unsigned oneTwoThree = 123;\n" |
20204 | " int oneTwo = 12;\n" |
20205 | " method();\n" |
20206 | " float k = 2;\n" |
20207 | " int ll = 10000;\n" |
20208 | "}", |
20209 | "void SomeFunction(int parameter= 0) {\n" |
20210 | " int const i= 1;\n" |
20211 | " int *j=2;\n" |
20212 | " int big = 10000;\n" |
20213 | "\n" |
20214 | "unsigned oneTwoThree =123;\n" |
20215 | "int oneTwo = 12;\n" |
20216 | " method();\n" |
20217 | "float k= 2;\n" |
20218 | "int ll=10000;\n" |
20219 | "}", |
20220 | AlignmentLeft); |
20221 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20222 | " int const i = 1;\n" |
20223 | " int** j = 2;\n" |
20224 | " int& k = i;\n" |
20225 | " int&& l = i + j;\n" |
20226 | " int big = 10000;\n" |
20227 | "\n" |
20228 | " unsigned oneTwoThree = 123;\n" |
20229 | " int oneTwo = 12;\n" |
20230 | " method();\n" |
20231 | " float k = 2;\n" |
20232 | " int ll = 10000;\n" |
20233 | "}", |
20234 | "void SomeFunction(int parameter= 0) {\n" |
20235 | " int const i= 1;\n" |
20236 | " int **j=2;\n" |
20237 | "int &k=i;\n" |
20238 | "int &&l=i+j;\n" |
20239 | " int big = 10000;\n" |
20240 | "\n" |
20241 | "unsigned oneTwoThree =123;\n" |
20242 | "int oneTwo = 12;\n" |
20243 | " method();\n" |
20244 | "float k= 2;\n" |
20245 | "int ll=10000;\n" |
20246 | "}", |
20247 | AlignmentLeft); |
20248 | // variables are aligned at their name, pointers are at the left most position |
20249 | verifyFormat("int* a;\n" |
20250 | "int** b;\n" |
20251 | "int*** c;\n" |
20252 | "int foobar;", |
20253 | AlignmentLeft); |
20254 | |
20255 | verifyFormat("int a(SomeType& foo, const Test& = Test());\n" |
20256 | "double b();", |
20257 | AlignmentLeft); |
20258 | |
20259 | auto Style = AlignmentLeft; |
20260 | Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; |
20261 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
20262 | verifyFormat("int function_name(const wchar_t* title,\n" |
20263 | " int x = 0,\n" |
20264 | " long extraStyle = 0,\n" |
20265 | " bool readOnly = false,\n" |
20266 | " FancyClassType* module = nullptr);", |
20267 | Style); |
20268 | |
20269 | // PAS_Middle |
20270 | FormatStyle AlignmentMiddle = Alignment; |
20271 | AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; |
20272 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20273 | " int const i = 1;\n" |
20274 | " int * j = 2;\n" |
20275 | " int big = 10000;\n" |
20276 | "\n" |
20277 | " unsigned oneTwoThree = 123;\n" |
20278 | " int oneTwo = 12;\n" |
20279 | " method();\n" |
20280 | " float k = 2;\n" |
20281 | " int ll = 10000;\n" |
20282 | "}", |
20283 | "void SomeFunction(int parameter= 0) {\n" |
20284 | " int const i= 1;\n" |
20285 | " int *j=2;\n" |
20286 | " int big = 10000;\n" |
20287 | "\n" |
20288 | "unsigned oneTwoThree =123;\n" |
20289 | "int oneTwo = 12;\n" |
20290 | " method();\n" |
20291 | "float k= 2;\n" |
20292 | "int ll=10000;\n" |
20293 | "}", |
20294 | AlignmentMiddle); |
20295 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20296 | " int const i = 1;\n" |
20297 | " int ** j = 2, ***k;\n" |
20298 | " int & k = i;\n" |
20299 | " int && l = i + j;\n" |
20300 | " int big = 10000;\n" |
20301 | "\n" |
20302 | " unsigned oneTwoThree = 123;\n" |
20303 | " int oneTwo = 12;\n" |
20304 | " method();\n" |
20305 | " float k = 2;\n" |
20306 | " int ll = 10000;\n" |
20307 | "}", |
20308 | "void SomeFunction(int parameter= 0) {\n" |
20309 | " int const i= 1;\n" |
20310 | " int **j=2,***k;\n" |
20311 | "int &k=i;\n" |
20312 | "int &&l=i+j;\n" |
20313 | " int big = 10000;\n" |
20314 | "\n" |
20315 | "unsigned oneTwoThree =123;\n" |
20316 | "int oneTwo = 12;\n" |
20317 | " method();\n" |
20318 | "float k= 2;\n" |
20319 | "int ll=10000;\n" |
20320 | "}", |
20321 | AlignmentMiddle); |
20322 | // variables are aligned at their name, pointers are in the middle |
20323 | verifyFormat("int * a;\n" |
20324 | "int * b;\n" |
20325 | "int *** c;\n" |
20326 | "int foobar;", |
20327 | AlignmentMiddle); |
20328 | |
20329 | verifyFormat("int a(SomeType & foo, const Test & = Test());\n" |
20330 | "double b();", |
20331 | AlignmentMiddle); |
20332 | |
20333 | Alignment.AlignConsecutiveAssignments.Enabled = false; |
20334 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; |
20335 | verifyFormat("#define A \\\n" |
20336 | " int aaaa = 12; \\\n" |
20337 | " float b = 23; \\\n" |
20338 | " const int ccc = 234; \\\n" |
20339 | " unsigned dddddddddd = 2345;", |
20340 | Alignment); |
20341 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
20342 | verifyFormat("#define A \\\n" |
20343 | " int aaaa = 12; \\\n" |
20344 | " float b = 23; \\\n" |
20345 | " const int ccc = 234; \\\n" |
20346 | " unsigned dddddddddd = 2345;", |
20347 | Alignment); |
20348 | Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; |
20349 | Alignment.ColumnLimit = 30; |
20350 | verifyFormat("#define A \\\n" |
20351 | " int aaaa = 12; \\\n" |
20352 | " float b = 23; \\\n" |
20353 | " const int ccc = 234; \\\n" |
20354 | " int dddddddddd = 2345;", |
20355 | Alignment); |
20356 | Alignment.ColumnLimit = 80; |
20357 | verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " |
20358 | "k = 4, int l = 5,\n" |
20359 | " int m = 6) {\n" |
20360 | " const int j = 10;\n" |
20361 | " otherThing = 1;\n" |
20362 | "}", |
20363 | Alignment); |
20364 | verifyFormat("void SomeFunction(int parameter = 0) {\n" |
20365 | " int const i = 1;\n" |
20366 | " int *j = 2;\n" |
20367 | " int big = 10000;\n" |
20368 | "}", |
20369 | Alignment); |
20370 | verifyFormat("class C {\n" |
20371 | "public:\n" |
20372 | " int i = 1;\n" |
20373 | " virtual void f() = 0;\n" |
20374 | "};", |
20375 | Alignment); |
20376 | verifyFormat("float i = 1;\n" |
20377 | "if (SomeType t = getSomething()) {\n" |
20378 | "}\n" |
20379 | "const unsigned j = 2;\n" |
20380 | "int big = 10000;", |
20381 | Alignment); |
20382 | verifyFormat("float j = 7;\n" |
20383 | "for (int k = 0; k < N; ++k) {\n" |
20384 | "}\n" |
20385 | "unsigned j = 2;\n" |
20386 | "int big = 10000;\n" |
20387 | "}", |
20388 | Alignment); |
20389 | Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
20390 | verifyFormat("float i = 1;\n" |
20391 | "LooooooooooongType loooooooooooooooooooooongVariable\n" |
20392 | " = someLooooooooooooooooongFunction();\n" |
20393 | "int j = 2;", |
20394 | Alignment); |
20395 | Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; |
20396 | verifyFormat("int i = 1;\n" |
20397 | "LooooooooooongType loooooooooooooooooooooongVariable =\n" |
20398 | " someLooooooooooooooooongFunction();\n" |
20399 | "int j = 2;", |
20400 | Alignment); |
20401 | |
20402 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
20403 | verifyFormat("auto lambda = []() {\n" |
20404 | " auto ii = 0;\n" |
20405 | " float j = 0;\n" |
20406 | " return 0;\n" |
20407 | "};\n" |
20408 | "int i = 0;\n" |
20409 | "float i2 = 0;\n" |
20410 | "auto v = type{\n" |
20411 | " i = 1, //\n" |
20412 | " (i = 2), //\n" |
20413 | " i = 3 //\n" |
20414 | "};", |
20415 | Alignment); |
20416 | Alignment.AlignConsecutiveAssignments.Enabled = false; |
20417 | |
20418 | verifyFormat( |
20419 | "int i = 1;\n" |
20420 | "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" |
20421 | " loooooooooooooooooooooongParameterB);\n" |
20422 | "int j = 2;", |
20423 | Alignment); |
20424 | |
20425 | // Test interactions with ColumnLimit and AlignConsecutiveAssignments: |
20426 | // We expect declarations and assignments to align, as long as it doesn't |
20427 | // exceed the column limit, starting a new alignment sequence whenever it |
20428 | // happens. |
20429 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
20430 | Alignment.ColumnLimit = 30; |
20431 | verifyFormat("float ii = 1;\n" |
20432 | "unsigned j = 2;\n" |
20433 | "int someVerylongVariable = 1;\n" |
20434 | "AnotherLongType ll = 123456;\n" |
20435 | "VeryVeryLongType k = 2;\n" |
20436 | "int myvar = 1;", |
20437 | Alignment); |
20438 | Alignment.ColumnLimit = 80; |
20439 | Alignment.AlignConsecutiveAssignments.Enabled = false; |
20440 | |
20441 | verifyFormat( |
20442 | "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" |
20443 | " typename LongType, typename B>\n" |
20444 | "auto foo() {}", |
20445 | Alignment); |
20446 | verifyFormat("float a, b = 1;\n" |
20447 | "int c = 2;\n" |
20448 | "int dd = 3;", |
20449 | Alignment); |
20450 | verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" |
20451 | "float b[1][] = {{3.f}};", |
20452 | Alignment); |
20453 | Alignment.AlignConsecutiveAssignments.Enabled = true; |
20454 | verifyFormat("float a, b = 1;\n" |
20455 | "int c = 2;\n" |
20456 | "int dd = 3;", |
20457 | Alignment); |
20458 | verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" |
20459 | "float b[1][] = {{3.f}};", |
20460 | Alignment); |
20461 | Alignment.AlignConsecutiveAssignments.Enabled = false; |
20462 | |
20463 | Alignment.ColumnLimit = 30; |
20464 | Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
20465 | verifyFormat("void foo(float a,\n" |
20466 | " float b,\n" |
20467 | " int c,\n" |
20468 | " uint32_t *d) {\n" |
20469 | " int *e = 0;\n" |
20470 | " float f = 0;\n" |
20471 | " double g = 0;\n" |
20472 | "}\n" |
20473 | "void bar(ino_t a,\n" |
20474 | " int b,\n" |
20475 | " uint32_t *c,\n" |
20476 | " bool d) {}", |
20477 | Alignment); |
20478 | Alignment.BinPackParameters = FormatStyle::BPPS_BinPack; |
20479 | Alignment.ColumnLimit = 80; |
20480 | |
20481 | // Bug 33507 |
20482 | Alignment.PointerAlignment = FormatStyle::PAS_Middle; |
20483 | verifyFormat( |
20484 | "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" |
20485 | " static const Version verVs2017;\n" |
20486 | " return true;\n" |
20487 | "});", |
20488 | Alignment); |
20489 | Alignment.PointerAlignment = FormatStyle::PAS_Right; |
20490 | |
20491 | // See llvm.org/PR35641 |
20492 | Alignment.AlignConsecutiveDeclarations.Enabled = true; |
20493 | verifyFormat("int func() { //\n" |
20494 | " int b;\n" |
20495 | " unsigned c;\n" |
20496 | "}", |
20497 | Alignment); |
20498 | |
20499 | // See PR37175 |
20500 | Style = getMozillaStyle(); |
20501 | Style.AlignConsecutiveDeclarations.Enabled = true; |
20502 | verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" |
20503 | "foo(int a);", |
20504 | "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style); |
20505 | |
20506 | Alignment.PointerAlignment = FormatStyle::PAS_Left; |
20507 | verifyFormat("unsigned int* a;\n" |
20508 | "int* b;\n" |
20509 | "unsigned int Const* c;\n" |
20510 | "unsigned int const* d;\n" |
20511 | "unsigned int Const& e;\n" |
20512 | "unsigned int const& f;", |
20513 | Alignment); |
20514 | verifyFormat("Const unsigned int* c;\n" |
20515 | "const unsigned int* d;\n" |
20516 | "Const unsigned int& e;\n" |
20517 | "const unsigned int& f;\n" |
20518 | "const unsigned g;\n" |
20519 | "Const unsigned h;", |
20520 | Alignment); |
20521 | |
20522 | Alignment.PointerAlignment = FormatStyle::PAS_Middle; |
20523 | verifyFormat("unsigned int * a;\n" |
20524 | "int * b;\n" |
20525 | "unsigned int Const * c;\n" |
20526 | "unsigned int const * d;\n" |
20527 | "unsigned int Const & e;\n" |
20528 | "unsigned int const & f;", |
20529 | Alignment); |
20530 | verifyFormat("Const unsigned int * c;\n" |
20531 | "const unsigned int * d;\n" |
20532 | "Const unsigned int & e;\n" |
20533 | "const unsigned int & f;\n" |
20534 | "const unsigned g;\n" |
20535 | "Const unsigned h;", |
20536 | Alignment); |
20537 | |
20538 | // See PR46529 |
20539 | FormatStyle BracedAlign = getLLVMStyle(); |
20540 | BracedAlign.AlignConsecutiveDeclarations.Enabled = true; |
20541 | verifyFormat("const auto result{[]() {\n" |
20542 | " const auto something = 1;\n" |
20543 | " return 2;\n" |
20544 | "}};", |
20545 | BracedAlign); |
20546 | verifyFormat("int foo{[]() {\n" |
20547 | " int bar{0};\n" |
20548 | " return 0;\n" |
20549 | "}()};", |
20550 | BracedAlign); |
20551 | BracedAlign.Cpp11BracedListStyle = false; |
20552 | verifyFormat("const auto result{ []() {\n" |
20553 | " const auto something = 1;\n" |
20554 | " return 2;\n" |
20555 | "} };", |
20556 | BracedAlign); |
20557 | verifyFormat("int foo{ []() {\n" |
20558 | " int bar{ 0 };\n" |
20559 | " return 0;\n" |
20560 | "}() };", |
20561 | BracedAlign); |
20562 | |
20563 | Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false; |
20564 | verifyFormat("unsigned int f1(void);\n" |
20565 | "void f2(void);\n" |
20566 | "size_t f3(void);", |
20567 | Alignment); |
20568 | } |
20569 | |
20570 | TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) { |
20571 | FormatStyle Alignment = getLLVMStyle(); |
20572 | Alignment.AllowShortCaseLabelsOnASingleLine = true; |
20573 | Alignment.AlignConsecutiveShortCaseStatements.Enabled = true; |
20574 | |
20575 | verifyFormat("switch (level) {\n" |
20576 | "case log::info: return \"info\";\n" |
20577 | "case log::warning: return \"warning\";\n" |
20578 | "default: return \"default\";\n" |
20579 | "}", |
20580 | Alignment); |
20581 | |
20582 | verifyFormat("switch (level) {\n" |
20583 | "case log::info: return \"info\";\n" |
20584 | "case log::warning: return \"warning\";\n" |
20585 | "}", |
20586 | "switch (level) {\n" |
20587 | "case log::info: return \"info\";\n" |
20588 | "case log::warning:\n" |
20589 | " return \"warning\";\n" |
20590 | "}", |
20591 | Alignment); |
20592 | |
20593 | // Empty case statements push out the alignment, but non-short case labels |
20594 | // don't. |
20595 | verifyFormat("switch (level) {\n" |
20596 | "case log::info: return \"info\";\n" |
20597 | "case log::critical:\n" |
20598 | "case log::warning:\n" |
20599 | "case log::severe: return \"severe\";\n" |
20600 | "case log::extra_severe:\n" |
20601 | " // comment\n" |
20602 | " return \"extra_severe\";\n" |
20603 | "}", |
20604 | Alignment); |
20605 | |
20606 | // Verify comments and empty lines break the alignment. |
20607 | verifyNoChange("switch (level) {\n" |
20608 | "case log::info: return \"info\";\n" |
20609 | "case log::warning: return \"warning\";\n" |
20610 | "// comment\n" |
20611 | "case log::critical: return \"critical\";\n" |
20612 | "default: return \"default\";\n" |
20613 | "\n" |
20614 | "case log::severe: return \"severe\";\n" |
20615 | "}", |
20616 | Alignment); |
20617 | |
20618 | // Empty case statements don't break the alignment, and potentially push it |
20619 | // out. |
20620 | verifyFormat("switch (level) {\n" |
20621 | "case log::info: return \"info\";\n" |
20622 | "case log::warning:\n" |
20623 | "case log::critical:\n" |
20624 | "default: return \"default\";\n" |
20625 | "}", |
20626 | Alignment); |
20627 | |
20628 | // Implicit fallthrough cases can be aligned with either a comment or |
20629 | // [[fallthrough]] |
20630 | verifyFormat("switch (level) {\n" |
20631 | "case log::info: return \"info\";\n" |
20632 | "case log::warning: // fallthrough\n" |
20633 | "case log::error: return \"error\";\n" |
20634 | "case log::critical: /*fallthrough*/\n" |
20635 | "case log::severe: return \"severe\";\n" |
20636 | "case log::diag: [[fallthrough]];\n" |
20637 | "default: return \"default\";\n" |
20638 | "}", |
20639 | Alignment); |
20640 | |
20641 | // Verify trailing comment that needs a reflow also gets aligned properly. |
20642 | verifyFormat("switch (level) {\n" |
20643 | "case log::info: return \"info\";\n" |
20644 | "case log::warning: // fallthrough\n" |
20645 | "case log::error: return \"error\";\n" |
20646 | "}", |
20647 | "switch (level) {\n" |
20648 | "case log::info: return \"info\";\n" |
20649 | "case log::warning: //fallthrough\n" |
20650 | "case log::error: return \"error\";\n" |
20651 | "}", |
20652 | Alignment); |
20653 | |
20654 | // Verify adjacent non-short case statements don't change the alignment, and |
20655 | // properly break the set of consecutive statements. |
20656 | verifyFormat("switch (level) {\n" |
20657 | "case log::critical:\n" |
20658 | " // comment\n" |
20659 | " return \"critical\";\n" |
20660 | "case log::info: return \"info\";\n" |
20661 | "case log::warning: return \"warning\";\n" |
20662 | "default:\n" |
20663 | " // comment\n" |
20664 | " return \"\";\n" |
20665 | "case log::error: return \"error\";\n" |
20666 | "case log::severe: return \"severe\";\n" |
20667 | "case log::extra_critical:\n" |
20668 | " // comment\n" |
20669 | " return \"extra critical\";\n" |
20670 | "}", |
20671 | Alignment); |
20672 | |
20673 | Alignment.SpaceBeforeCaseColon = true; |
20674 | verifyFormat("switch (level) {\n" |
20675 | "case log::info : return \"info\";\n" |
20676 | "case log::warning : return \"warning\";\n" |
20677 | "default : return \"default\";\n" |
20678 | "}", |
20679 | Alignment); |
20680 | Alignment.SpaceBeforeCaseColon = false; |
20681 | |
20682 | // Make sure we don't incorrectly align correctly across nested switch cases. |
20683 | verifyFormat("switch (level) {\n" |
20684 | "case log::info: return \"info\";\n" |
20685 | "case log::warning: return \"warning\";\n" |
20686 | "case log::other:\n" |
20687 | " switch (sublevel) {\n" |
20688 | " case log::info: return \"info\";\n" |
20689 | " case log::warning: return \"warning\";\n" |
20690 | " }\n" |
20691 | " break;\n" |
20692 | "case log::error: return \"error\";\n" |
20693 | "default: return \"default\";\n" |
20694 | "}", |
20695 | "switch (level) {\n" |
20696 | "case log::info: return \"info\";\n" |
20697 | "case log::warning: return \"warning\";\n" |
20698 | "case log::other: switch (sublevel) {\n" |
20699 | " case log::info: return \"info\";\n" |
20700 | " case log::warning: return \"warning\";\n" |
20701 | "}\n" |
20702 | "break;\n" |
20703 | "case log::error: return \"error\";\n" |
20704 | "default: return \"default\";\n" |
20705 | "}", |
20706 | Alignment); |
20707 | |
20708 | Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true; |
20709 | |
20710 | verifyFormat("switch (level) {\n" |
20711 | "case log::info: return \"info\";\n" |
20712 | "\n" |
20713 | "case log::warning: return \"warning\";\n" |
20714 | "}", |
20715 | "switch (level) {\n" |
20716 | "case log::info: return \"info\";\n" |
20717 | "\n" |
20718 | "case log::warning: return \"warning\";\n" |
20719 | "}", |
20720 | Alignment); |
20721 | |
20722 | Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true; |
20723 | |
20724 | verifyNoChange("switch (level) {\n" |
20725 | "case log::info: return \"info\";\n" |
20726 | "\n" |
20727 | "/* block comment */\n" |
20728 | "\n" |
20729 | "// line comment\n" |
20730 | "case log::warning: return \"warning\";\n" |
20731 | "}", |
20732 | Alignment); |
20733 | |
20734 | Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false; |
20735 | |
20736 | verifyFormat("switch (level) {\n" |
20737 | "case log::info: return \"info\";\n" |
20738 | "//\n" |
20739 | "case log::warning: return \"warning\";\n" |
20740 | "}", |
20741 | Alignment); |
20742 | |
20743 | Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true; |
20744 | |
20745 | verifyFormat("switch (level) {\n" |
20746 | "case log::info : return \"info\";\n" |
20747 | "case log::warning: return \"warning\";\n" |
20748 | "default : return \"default\";\n" |
20749 | "}", |
20750 | Alignment); |
20751 | |
20752 | // With AlignCaseColons, empty case statements don't break alignment of |
20753 | // consecutive case statements (and are aligned). |
20754 | verifyFormat("switch (level) {\n" |
20755 | "case log::info : return \"info\";\n" |
20756 | "case log::warning :\n" |
20757 | "case log::critical:\n" |
20758 | "default : return \"default\";\n" |
20759 | "}", |
20760 | Alignment); |
20761 | |
20762 | // Final non-short case labels shouldn't have their colon aligned |
20763 | verifyFormat("switch (level) {\n" |
20764 | "case log::info : return \"info\";\n" |
20765 | "case log::warning :\n" |
20766 | "case log::critical:\n" |
20767 | "case log::severe : return \"severe\";\n" |
20768 | "default:\n" |
20769 | " // comment\n" |
20770 | " return \"default\";\n" |
20771 | "}", |
20772 | Alignment); |
20773 | |
20774 | // Verify adjacent non-short case statements break the set of consecutive |
20775 | // alignments and aren't aligned with adjacent non-short case statements if |
20776 | // AlignCaseColons is set. |
20777 | verifyFormat("switch (level) {\n" |
20778 | "case log::critical:\n" |
20779 | " // comment\n" |
20780 | " return \"critical\";\n" |
20781 | "case log::info : return \"info\";\n" |
20782 | "case log::warning: return \"warning\";\n" |
20783 | "default:\n" |
20784 | " // comment\n" |
20785 | " return \"\";\n" |
20786 | "case log::error : return \"error\";\n" |
20787 | "case log::severe: return \"severe\";\n" |
20788 | "case log::extra_critical:\n" |
20789 | " // comment\n" |
20790 | " return \"extra critical\";\n" |
20791 | "}", |
20792 | Alignment); |
20793 | |
20794 | Alignment.SpaceBeforeCaseColon = true; |
20795 | verifyFormat("switch (level) {\n" |
20796 | "case log::info : return \"info\";\n" |
20797 | "case log::warning : return \"warning\";\n" |
20798 | "case log::error :\n" |
20799 | "default : return \"default\";\n" |
20800 | "}", |
20801 | Alignment); |
20802 | } |
20803 | |
20804 | TEST_F(FormatTest, AlignWithLineBreaks) { |
20805 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 120); |
20806 | |
20807 | EXPECT_EQ(Style.AlignConsecutiveAssignments, |
20808 | FormatStyle::AlignConsecutiveStyle( |
20809 | {/*Enabled=*/false, /*AcrossEmptyLines=*/false, |
20810 | /*AcrossComments=*/false, /*AlignCompound=*/false, |
20811 | /*AlignFunctionDeclarations=*/false, |
20812 | /*AlignFunctionPointers=*/false, |
20813 | /*PadOperators=*/true})); |
20814 | EXPECT_EQ(Style.AlignConsecutiveDeclarations, |
20815 | FormatStyle::AlignConsecutiveStyle( |
20816 | {/*Enabled=*/false, /*AcrossEmptyLines=*/false, |
20817 | /*AcrossComments=*/false, /*AlignCompound=*/false, |
20818 | /*AlignFunctionDeclarations=*/true, |
20819 | /*AlignFunctionPointers=*/false, |
20820 | /*PadOperators=*/false})); |
20821 | verifyFormat("void foo() {\n" |
20822 | " int myVar = 5;\n" |
20823 | " double x = 3.14;\n" |
20824 | " auto str = \"Hello \"\n" |
20825 | " \"World\";\n" |
20826 | " auto s = \"Hello \"\n" |
20827 | " \"Again\";\n" |
20828 | "}", |
20829 | Style); |
20830 | |
20831 | // clang-format off |
20832 | verifyFormat("void foo() {\n" |
20833 | " const int capacityBefore = Entries.capacity();\n" |
20834 | " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20835 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20836 | " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20837 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20838 | "}", |
20839 | Style); |
20840 | // clang-format on |
20841 | |
20842 | Style.AlignConsecutiveAssignments.Enabled = true; |
20843 | verifyFormat("void foo() {\n" |
20844 | " int myVar = 5;\n" |
20845 | " double x = 3.14;\n" |
20846 | " auto str = \"Hello \"\n" |
20847 | " \"World\";\n" |
20848 | " auto s = \"Hello \"\n" |
20849 | " \"Again\";\n" |
20850 | "}", |
20851 | Style); |
20852 | |
20853 | // clang-format off |
20854 | verifyFormat("void foo() {\n" |
20855 | " const int capacityBefore = Entries.capacity();\n" |
20856 | " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20857 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20858 | " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20859 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20860 | "}", |
20861 | Style); |
20862 | // clang-format on |
20863 | |
20864 | Style.AlignConsecutiveAssignments.Enabled = false; |
20865 | Style.AlignConsecutiveDeclarations.Enabled = true; |
20866 | verifyFormat("void foo() {\n" |
20867 | " int myVar = 5;\n" |
20868 | " double x = 3.14;\n" |
20869 | " auto str = \"Hello \"\n" |
20870 | " \"World\";\n" |
20871 | " auto s = \"Hello \"\n" |
20872 | " \"Again\";\n" |
20873 | "}", |
20874 | Style); |
20875 | |
20876 | // clang-format off |
20877 | verifyFormat("void foo() {\n" |
20878 | " const int capacityBefore = Entries.capacity();\n" |
20879 | " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20880 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20881 | " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20882 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20883 | "}", |
20884 | Style); |
20885 | // clang-format on |
20886 | |
20887 | Style.AlignConsecutiveAssignments.Enabled = true; |
20888 | Style.AlignConsecutiveDeclarations.Enabled = true; |
20889 | |
20890 | verifyFormat("void foo() {\n" |
20891 | " int myVar = 5;\n" |
20892 | " double x = 3.14;\n" |
20893 | " auto str = \"Hello \"\n" |
20894 | " \"World\";\n" |
20895 | " auto s = \"Hello \"\n" |
20896 | " \"Again\";\n" |
20897 | "}", |
20898 | Style); |
20899 | |
20900 | // clang-format off |
20901 | verifyFormat("void foo() {\n" |
20902 | " const int capacityBefore = Entries.capacity();\n" |
20903 | " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20904 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20905 | " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" |
20906 | " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" |
20907 | "}", |
20908 | Style); |
20909 | // clang-format on |
20910 | |
20911 | Style = getLLVMStyleWithColumns(ColumnLimit: 20); |
20912 | Style.AlignConsecutiveAssignments.Enabled = true; |
20913 | Style.IndentWidth = 4; |
20914 | |
20915 | verifyFormat("void foo() {\n" |
20916 | " int i1 = 1;\n" |
20917 | " int j = 0;\n" |
20918 | " int k = bar(\n" |
20919 | " argument1,\n" |
20920 | " argument2);\n" |
20921 | "}", |
20922 | Style); |
20923 | |
20924 | verifyFormat("unsigned i = 0;\n" |
20925 | "int a[] = {\n" |
20926 | " 1234567890,\n" |
20927 | " -1234567890};", |
20928 | Style); |
20929 | |
20930 | Style.ColumnLimit = 120; |
20931 | |
20932 | // clang-format off |
20933 | verifyFormat("void SomeFunc() {\n" |
20934 | " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" |
20935 | " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" |
20936 | " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" |
20937 | " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" |
20938 | " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" |
20939 | " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" |
20940 | "}", |
20941 | Style); |
20942 | // clang-format on |
20943 | |
20944 | Style.BinPackArguments = false; |
20945 | |
20946 | // clang-format off |
20947 | verifyFormat("void SomeFunc() {\n" |
20948 | " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" |
20949 | " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" |
20950 | " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" |
20951 | " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" |
20952 | " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" |
20953 | " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" |
20954 | "}", |
20955 | Style); |
20956 | // clang-format on |
20957 | } |
20958 | |
20959 | TEST_F(FormatTest, AlignWithInitializerPeriods) { |
20960 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
20961 | |
20962 | verifyFormat("void foo1(void) {\n" |
20963 | " BYTE p[1] = 1;\n" |
20964 | " A B = {.one_foooooooooooooooo = 2,\n" |
20965 | " .two_fooooooooooooo = 3,\n" |
20966 | " .three_fooooooooooooo = 4};\n" |
20967 | " BYTE payload = 2;\n" |
20968 | "}", |
20969 | Style); |
20970 | |
20971 | Style.AlignConsecutiveAssignments.Enabled = true; |
20972 | Style.AlignConsecutiveDeclarations.Enabled = false; |
20973 | verifyFormat("void foo2(void) {\n" |
20974 | " BYTE p[1] = 1;\n" |
20975 | " A B = {.one_foooooooooooooooo = 2,\n" |
20976 | " .two_fooooooooooooo = 3,\n" |
20977 | " .three_fooooooooooooo = 4};\n" |
20978 | " BYTE payload = 2;\n" |
20979 | "}", |
20980 | Style); |
20981 | |
20982 | Style.AlignConsecutiveAssignments.Enabled = false; |
20983 | Style.AlignConsecutiveDeclarations.Enabled = true; |
20984 | verifyFormat("void foo3(void) {\n" |
20985 | " BYTE p[1] = 1;\n" |
20986 | " A B = {.one_foooooooooooooooo = 2,\n" |
20987 | " .two_fooooooooooooo = 3,\n" |
20988 | " .three_fooooooooooooo = 4};\n" |
20989 | " BYTE payload = 2;\n" |
20990 | "}", |
20991 | Style); |
20992 | |
20993 | Style.AlignConsecutiveAssignments.Enabled = true; |
20994 | Style.AlignConsecutiveDeclarations.Enabled = true; |
20995 | verifyFormat("void foo4(void) {\n" |
20996 | " BYTE p[1] = 1;\n" |
20997 | " A B = {.one_foooooooooooooooo = 2,\n" |
20998 | " .two_fooooooooooooo = 3,\n" |
20999 | " .three_fooooooooooooo = 4};\n" |
21000 | " BYTE payload = 2;\n" |
21001 | "}", |
21002 | Style); |
21003 | } |
21004 | |
21005 | TEST_F(FormatTest, LinuxBraceBreaking) { |
21006 | FormatStyle LinuxBraceStyle = getLLVMStyle(); |
21007 | LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; |
21008 | verifyFormat("namespace a\n" |
21009 | "{\n" |
21010 | "class A\n" |
21011 | "{\n" |
21012 | " void f()\n" |
21013 | " {\n" |
21014 | " if (true) {\n" |
21015 | " a();\n" |
21016 | " b();\n" |
21017 | " } else {\n" |
21018 | " a();\n" |
21019 | " }\n" |
21020 | " }\n" |
21021 | " void g() { return; }\n" |
21022 | "};\n" |
21023 | "struct B {\n" |
21024 | " int x;\n" |
21025 | "};\n" |
21026 | "} // namespace a", |
21027 | LinuxBraceStyle); |
21028 | verifyFormat("enum X {\n" |
21029 | " Y = 0,\n" |
21030 | "}", |
21031 | LinuxBraceStyle); |
21032 | verifyFormat("struct S {\n" |
21033 | " int Type;\n" |
21034 | " union {\n" |
21035 | " int x;\n" |
21036 | " double y;\n" |
21037 | " } Value;\n" |
21038 | " class C\n" |
21039 | " {\n" |
21040 | " MyFavoriteType Value;\n" |
21041 | " } Class;\n" |
21042 | "}", |
21043 | LinuxBraceStyle); |
21044 | } |
21045 | |
21046 | TEST_F(FormatTest, MozillaBraceBreaking) { |
21047 | FormatStyle MozillaBraceStyle = getLLVMStyle(); |
21048 | MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; |
21049 | MozillaBraceStyle.FixNamespaceComments = false; |
21050 | verifyFormat("namespace a {\n" |
21051 | "class A\n" |
21052 | "{\n" |
21053 | " void f()\n" |
21054 | " {\n" |
21055 | " if (true) {\n" |
21056 | " a();\n" |
21057 | " b();\n" |
21058 | " }\n" |
21059 | " }\n" |
21060 | " void g() { return; }\n" |
21061 | "};\n" |
21062 | "enum E\n" |
21063 | "{\n" |
21064 | " A,\n" |
21065 | " // foo\n" |
21066 | " B,\n" |
21067 | " C\n" |
21068 | "};\n" |
21069 | "struct B\n" |
21070 | "{\n" |
21071 | " int x;\n" |
21072 | "};\n" |
21073 | "}", |
21074 | MozillaBraceStyle); |
21075 | verifyFormat("struct S\n" |
21076 | "{\n" |
21077 | " int Type;\n" |
21078 | " union\n" |
21079 | " {\n" |
21080 | " int x;\n" |
21081 | " double y;\n" |
21082 | " } Value;\n" |
21083 | " class C\n" |
21084 | " {\n" |
21085 | " MyFavoriteType Value;\n" |
21086 | " } Class;\n" |
21087 | "}", |
21088 | MozillaBraceStyle); |
21089 | } |
21090 | |
21091 | TEST_F(FormatTest, StroustrupBraceBreaking) { |
21092 | FormatStyle StroustrupBraceStyle = getLLVMStyle(); |
21093 | StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; |
21094 | verifyFormat("namespace a {\n" |
21095 | "class A {\n" |
21096 | " void f()\n" |
21097 | " {\n" |
21098 | " if (true) {\n" |
21099 | " a();\n" |
21100 | " b();\n" |
21101 | " }\n" |
21102 | " }\n" |
21103 | " void g() { return; }\n" |
21104 | "};\n" |
21105 | "struct B {\n" |
21106 | " int x;\n" |
21107 | "};\n" |
21108 | "} // namespace a", |
21109 | StroustrupBraceStyle); |
21110 | |
21111 | verifyFormat("void foo()\n" |
21112 | "{\n" |
21113 | " if (a) {\n" |
21114 | " a();\n" |
21115 | " }\n" |
21116 | " else {\n" |
21117 | " b();\n" |
21118 | " }\n" |
21119 | "}", |
21120 | StroustrupBraceStyle); |
21121 | |
21122 | verifyFormat("#ifdef _DEBUG\n" |
21123 | "int foo(int i = 0)\n" |
21124 | "#else\n" |
21125 | "int foo(int i = 5)\n" |
21126 | "#endif\n" |
21127 | "{\n" |
21128 | " return i;\n" |
21129 | "}", |
21130 | StroustrupBraceStyle); |
21131 | |
21132 | verifyFormat("void foo() {}\n" |
21133 | "void bar()\n" |
21134 | "#ifdef _DEBUG\n" |
21135 | "{\n" |
21136 | " foo();\n" |
21137 | "}\n" |
21138 | "#else\n" |
21139 | "{\n" |
21140 | "}\n" |
21141 | "#endif", |
21142 | StroustrupBraceStyle); |
21143 | |
21144 | verifyFormat("void foobar() { int i = 5; }\n" |
21145 | "#ifdef _DEBUG\n" |
21146 | "void bar() {}\n" |
21147 | "#else\n" |
21148 | "void bar() { foobar(); }\n" |
21149 | "#endif", |
21150 | StroustrupBraceStyle); |
21151 | } |
21152 | |
21153 | TEST_F(FormatTest, AllmanBraceBreaking) { |
21154 | FormatStyle AllmanBraceStyle = getLLVMStyle(); |
21155 | AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; |
21156 | |
21157 | verifyFormat("namespace a\n" |
21158 | "{\n" |
21159 | "void f();\n" |
21160 | "void g();\n" |
21161 | "} // namespace a", |
21162 | "namespace a\n" |
21163 | "{\n" |
21164 | "void f();\n" |
21165 | "void g();\n" |
21166 | "}", |
21167 | AllmanBraceStyle); |
21168 | |
21169 | verifyFormat("namespace a\n" |
21170 | "{\n" |
21171 | "class A\n" |
21172 | "{\n" |
21173 | " void f()\n" |
21174 | " {\n" |
21175 | " if (true)\n" |
21176 | " {\n" |
21177 | " a();\n" |
21178 | " b();\n" |
21179 | " }\n" |
21180 | " }\n" |
21181 | " void g() { return; }\n" |
21182 | "};\n" |
21183 | "struct B\n" |
21184 | "{\n" |
21185 | " int x;\n" |
21186 | "};\n" |
21187 | "union C\n" |
21188 | "{\n" |
21189 | "};\n" |
21190 | "} // namespace a", |
21191 | AllmanBraceStyle); |
21192 | |
21193 | verifyFormat("void f()\n" |
21194 | "{\n" |
21195 | " if (true)\n" |
21196 | " {\n" |
21197 | " a();\n" |
21198 | " }\n" |
21199 | " else if (false)\n" |
21200 | " {\n" |
21201 | " b();\n" |
21202 | " }\n" |
21203 | " else\n" |
21204 | " {\n" |
21205 | " c();\n" |
21206 | " }\n" |
21207 | "}", |
21208 | AllmanBraceStyle); |
21209 | |
21210 | verifyFormat("void f()\n" |
21211 | "{\n" |
21212 | " for (int i = 0; i < 10; ++i)\n" |
21213 | " {\n" |
21214 | " a();\n" |
21215 | " }\n" |
21216 | " while (false)\n" |
21217 | " {\n" |
21218 | " b();\n" |
21219 | " }\n" |
21220 | " do\n" |
21221 | " {\n" |
21222 | " c();\n" |
21223 | " } while (false)\n" |
21224 | "}", |
21225 | AllmanBraceStyle); |
21226 | |
21227 | verifyFormat("void f(int a)\n" |
21228 | "{\n" |
21229 | " switch (a)\n" |
21230 | " {\n" |
21231 | " case 0:\n" |
21232 | " break;\n" |
21233 | " case 1:\n" |
21234 | " {\n" |
21235 | " break;\n" |
21236 | " }\n" |
21237 | " case 2:\n" |
21238 | " {\n" |
21239 | " }\n" |
21240 | " break;\n" |
21241 | " default:\n" |
21242 | " break;\n" |
21243 | " }\n" |
21244 | "}", |
21245 | AllmanBraceStyle); |
21246 | |
21247 | verifyFormat("enum X\n" |
21248 | "{\n" |
21249 | " Y = 0,\n" |
21250 | "}", |
21251 | AllmanBraceStyle); |
21252 | verifyFormat("enum X\n" |
21253 | "{\n" |
21254 | " Y = 0\n" |
21255 | "}", |
21256 | AllmanBraceStyle); |
21257 | |
21258 | verifyFormat("@interface BSApplicationController ()\n" |
21259 | "{\n" |
21260 | "@private\n" |
21261 | " id _extraIvar;\n" |
21262 | "}\n" |
21263 | "@end", |
21264 | AllmanBraceStyle); |
21265 | |
21266 | verifyFormat("#ifdef _DEBUG\n" |
21267 | "int foo(int i = 0)\n" |
21268 | "#else\n" |
21269 | "int foo(int i = 5)\n" |
21270 | "#endif\n" |
21271 | "{\n" |
21272 | " return i;\n" |
21273 | "}", |
21274 | AllmanBraceStyle); |
21275 | |
21276 | verifyFormat("void foo() {}\n" |
21277 | "void bar()\n" |
21278 | "#ifdef _DEBUG\n" |
21279 | "{\n" |
21280 | " foo();\n" |
21281 | "}\n" |
21282 | "#else\n" |
21283 | "{\n" |
21284 | "}\n" |
21285 | "#endif", |
21286 | AllmanBraceStyle); |
21287 | |
21288 | verifyFormat("void foobar() { int i = 5; }\n" |
21289 | "#ifdef _DEBUG\n" |
21290 | "void bar() {}\n" |
21291 | "#else\n" |
21292 | "void bar() { foobar(); }\n" |
21293 | "#endif", |
21294 | AllmanBraceStyle); |
21295 | |
21296 | EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, |
21297 | FormatStyle::SLS_All); |
21298 | |
21299 | verifyFormat("[](int i) { return i + 2; };\n" |
21300 | "[](int i, int j)\n" |
21301 | "{\n" |
21302 | " auto x = i + j;\n" |
21303 | " auto y = i * j;\n" |
21304 | " return x ^ y;\n" |
21305 | "};\n" |
21306 | "void foo()\n" |
21307 | "{\n" |
21308 | " auto shortLambda = [](int i) { return i + 2; };\n" |
21309 | " auto longLambda = [](int i, int j)\n" |
21310 | " {\n" |
21311 | " auto x = i + j;\n" |
21312 | " auto y = i * j;\n" |
21313 | " return x ^ y;\n" |
21314 | " };\n" |
21315 | "}", |
21316 | AllmanBraceStyle); |
21317 | |
21318 | AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; |
21319 | |
21320 | verifyFormat("[](int i)\n" |
21321 | "{\n" |
21322 | " return i + 2;\n" |
21323 | "};\n" |
21324 | "[](int i, int j)\n" |
21325 | "{\n" |
21326 | " auto x = i + j;\n" |
21327 | " auto y = i * j;\n" |
21328 | " return x ^ y;\n" |
21329 | "};\n" |
21330 | "void foo()\n" |
21331 | "{\n" |
21332 | " auto shortLambda = [](int i)\n" |
21333 | " {\n" |
21334 | " return i + 2;\n" |
21335 | " };\n" |
21336 | " auto longLambda = [](int i, int j)\n" |
21337 | " {\n" |
21338 | " auto x = i + j;\n" |
21339 | " auto y = i * j;\n" |
21340 | " return x ^ y;\n" |
21341 | " };\n" |
21342 | "}", |
21343 | AllmanBraceStyle); |
21344 | |
21345 | // Reset |
21346 | AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; |
21347 | |
21348 | // This shouldn't affect ObjC blocks.. |
21349 | verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" |
21350 | " // ...\n" |
21351 | " int i;\n" |
21352 | "}];", |
21353 | AllmanBraceStyle); |
21354 | verifyFormat("void (^block)(void) = ^{\n" |
21355 | " // ...\n" |
21356 | " int i;\n" |
21357 | "};", |
21358 | AllmanBraceStyle); |
21359 | // .. or dict literals. |
21360 | verifyFormat("void f()\n" |
21361 | "{\n" |
21362 | " // ...\n" |
21363 | " [object someMethod:@{@\"a\" : @\"b\"}];\n" |
21364 | "}", |
21365 | AllmanBraceStyle); |
21366 | verifyFormat("void f()\n" |
21367 | "{\n" |
21368 | " // ...\n" |
21369 | " [object someMethod:@{a : @\"b\"}];\n" |
21370 | "}", |
21371 | AllmanBraceStyle); |
21372 | verifyFormat("int f()\n" |
21373 | "{ // comment\n" |
21374 | " return 42;\n" |
21375 | "}", |
21376 | AllmanBraceStyle); |
21377 | |
21378 | AllmanBraceStyle.ColumnLimit = 19; |
21379 | verifyFormat("void f() { int i; }", AllmanBraceStyle); |
21380 | AllmanBraceStyle.ColumnLimit = 18; |
21381 | verifyFormat("void f()\n" |
21382 | "{\n" |
21383 | " int i;\n" |
21384 | "}", |
21385 | AllmanBraceStyle); |
21386 | AllmanBraceStyle.ColumnLimit = 80; |
21387 | |
21388 | FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; |
21389 | BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = |
21390 | FormatStyle::SIS_WithoutElse; |
21391 | BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; |
21392 | verifyFormat("void f(bool b)\n" |
21393 | "{\n" |
21394 | " if (b)\n" |
21395 | " {\n" |
21396 | " return;\n" |
21397 | " }\n" |
21398 | "}", |
21399 | BreakBeforeBraceShortIfs); |
21400 | verifyFormat("void f(bool b)\n" |
21401 | "{\n" |
21402 | " if constexpr (b)\n" |
21403 | " {\n" |
21404 | " return;\n" |
21405 | " }\n" |
21406 | "}", |
21407 | BreakBeforeBraceShortIfs); |
21408 | verifyFormat("void f(bool b)\n" |
21409 | "{\n" |
21410 | " if CONSTEXPR (b)\n" |
21411 | " {\n" |
21412 | " return;\n" |
21413 | " }\n" |
21414 | "}", |
21415 | BreakBeforeBraceShortIfs); |
21416 | verifyFormat("void f(bool b)\n" |
21417 | "{\n" |
21418 | " if (b) return;\n" |
21419 | "}", |
21420 | BreakBeforeBraceShortIfs); |
21421 | verifyFormat("void f(bool b)\n" |
21422 | "{\n" |
21423 | " if constexpr (b) return;\n" |
21424 | "}", |
21425 | BreakBeforeBraceShortIfs); |
21426 | verifyFormat("void f(bool b)\n" |
21427 | "{\n" |
21428 | " if CONSTEXPR (b) return;\n" |
21429 | "}", |
21430 | BreakBeforeBraceShortIfs); |
21431 | verifyFormat("void f(bool b)\n" |
21432 | "{\n" |
21433 | " while (b)\n" |
21434 | " {\n" |
21435 | " return;\n" |
21436 | " }\n" |
21437 | "}", |
21438 | BreakBeforeBraceShortIfs); |
21439 | } |
21440 | |
21441 | TEST_F(FormatTest, WhitesmithsBraceBreaking) { |
21442 | FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(ColumnLimit: 0); |
21443 | WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; |
21444 | |
21445 | // Make a few changes to the style for testing purposes |
21446 | WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = |
21447 | FormatStyle::SFS_Empty; |
21448 | WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; |
21449 | |
21450 | // FIXME: this test case can't decide whether there should be a blank line |
21451 | // after the ~D() line or not. It adds one if one doesn't exist in the test |
21452 | // and it removes the line if one exists. |
21453 | /* |
21454 | verifyFormat("class A;\n" |
21455 | "namespace B\n" |
21456 | " {\n" |
21457 | "class C;\n" |
21458 | "// Comment\n" |
21459 | "class D\n" |
21460 | " {\n" |
21461 | "public:\n" |
21462 | " D();\n" |
21463 | " ~D() {}\n" |
21464 | "private:\n" |
21465 | " enum E\n" |
21466 | " {\n" |
21467 | " F\n" |
21468 | " }\n" |
21469 | " };\n" |
21470 | " } // namespace B", |
21471 | WhitesmithsBraceStyle); |
21472 | */ |
21473 | |
21474 | WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; |
21475 | verifyFormat("namespace a\n" |
21476 | " {\n" |
21477 | "class A\n" |
21478 | " {\n" |
21479 | " void f()\n" |
21480 | " {\n" |
21481 | " if (true)\n" |
21482 | " {\n" |
21483 | " a();\n" |
21484 | " b();\n" |
21485 | " }\n" |
21486 | " }\n" |
21487 | " void g()\n" |
21488 | " {\n" |
21489 | " return;\n" |
21490 | " }\n" |
21491 | " };\n" |
21492 | "struct B\n" |
21493 | " {\n" |
21494 | " int x;\n" |
21495 | " };\n" |
21496 | " } // namespace a", |
21497 | WhitesmithsBraceStyle); |
21498 | |
21499 | verifyFormat("namespace a\n" |
21500 | " {\n" |
21501 | "namespace b\n" |
21502 | " {\n" |
21503 | "class A\n" |
21504 | " {\n" |
21505 | " void f()\n" |
21506 | " {\n" |
21507 | " if (true)\n" |
21508 | " {\n" |
21509 | " a();\n" |
21510 | " b();\n" |
21511 | " }\n" |
21512 | " }\n" |
21513 | " void g()\n" |
21514 | " {\n" |
21515 | " return;\n" |
21516 | " }\n" |
21517 | " };\n" |
21518 | "struct B\n" |
21519 | " {\n" |
21520 | " int x;\n" |
21521 | " };\n" |
21522 | " } // namespace b\n" |
21523 | " } // namespace a", |
21524 | WhitesmithsBraceStyle); |
21525 | |
21526 | WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; |
21527 | verifyFormat("namespace a\n" |
21528 | " {\n" |
21529 | "namespace b\n" |
21530 | " {\n" |
21531 | " class A\n" |
21532 | " {\n" |
21533 | " void f()\n" |
21534 | " {\n" |
21535 | " if (true)\n" |
21536 | " {\n" |
21537 | " a();\n" |
21538 | " b();\n" |
21539 | " }\n" |
21540 | " }\n" |
21541 | " void g()\n" |
21542 | " {\n" |
21543 | " return;\n" |
21544 | " }\n" |
21545 | " };\n" |
21546 | " struct B\n" |
21547 | " {\n" |
21548 | " int x;\n" |
21549 | " };\n" |
21550 | " } // namespace b\n" |
21551 | " } // namespace a", |
21552 | WhitesmithsBraceStyle); |
21553 | |
21554 | WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; |
21555 | verifyFormat("namespace a\n" |
21556 | " {\n" |
21557 | " namespace b\n" |
21558 | " {\n" |
21559 | " class A\n" |
21560 | " {\n" |
21561 | " void f()\n" |
21562 | " {\n" |
21563 | " if (true)\n" |
21564 | " {\n" |
21565 | " a();\n" |
21566 | " b();\n" |
21567 | " }\n" |
21568 | " }\n" |
21569 | " void g()\n" |
21570 | " {\n" |
21571 | " return;\n" |
21572 | " }\n" |
21573 | " };\n" |
21574 | " struct B\n" |
21575 | " {\n" |
21576 | " int x;\n" |
21577 | " };\n" |
21578 | " } // namespace b\n" |
21579 | " } // namespace a", |
21580 | WhitesmithsBraceStyle); |
21581 | |
21582 | verifyFormat("void f()\n" |
21583 | " {\n" |
21584 | " if (true)\n" |
21585 | " {\n" |
21586 | " a();\n" |
21587 | " }\n" |
21588 | " else if (false)\n" |
21589 | " {\n" |
21590 | " b();\n" |
21591 | " }\n" |
21592 | " else\n" |
21593 | " {\n" |
21594 | " c();\n" |
21595 | " }\n" |
21596 | " }", |
21597 | WhitesmithsBraceStyle); |
21598 | |
21599 | verifyFormat("void f()\n" |
21600 | " {\n" |
21601 | " for (int i = 0; i < 10; ++i)\n" |
21602 | " {\n" |
21603 | " a();\n" |
21604 | " }\n" |
21605 | " while (false)\n" |
21606 | " {\n" |
21607 | " b();\n" |
21608 | " }\n" |
21609 | " do\n" |
21610 | " {\n" |
21611 | " c();\n" |
21612 | " } while (false)\n" |
21613 | " }", |
21614 | WhitesmithsBraceStyle); |
21615 | |
21616 | WhitesmithsBraceStyle.IndentCaseLabels = true; |
21617 | verifyFormat("void switchTest1(int a)\n" |
21618 | " {\n" |
21619 | " switch (a)\n" |
21620 | " {\n" |
21621 | " case 2:\n" |
21622 | " {\n" |
21623 | " }\n" |
21624 | " break;\n" |
21625 | " }\n" |
21626 | " }", |
21627 | WhitesmithsBraceStyle); |
21628 | |
21629 | verifyFormat("void switchTest2(int a)\n" |
21630 | " {\n" |
21631 | " switch (a)\n" |
21632 | " {\n" |
21633 | " case 0:\n" |
21634 | " break;\n" |
21635 | " case 1:\n" |
21636 | " {\n" |
21637 | " break;\n" |
21638 | " }\n" |
21639 | " case 2:\n" |
21640 | " {\n" |
21641 | " }\n" |
21642 | " break;\n" |
21643 | " default:\n" |
21644 | " break;\n" |
21645 | " }\n" |
21646 | " }", |
21647 | WhitesmithsBraceStyle); |
21648 | |
21649 | verifyFormat("void switchTest3(int a)\n" |
21650 | " {\n" |
21651 | " switch (a)\n" |
21652 | " {\n" |
21653 | " case 0:\n" |
21654 | " {\n" |
21655 | " foo(x);\n" |
21656 | " }\n" |
21657 | " break;\n" |
21658 | " default:\n" |
21659 | " {\n" |
21660 | " foo(1);\n" |
21661 | " }\n" |
21662 | " break;\n" |
21663 | " }\n" |
21664 | " }", |
21665 | WhitesmithsBraceStyle); |
21666 | |
21667 | WhitesmithsBraceStyle.IndentCaseLabels = false; |
21668 | |
21669 | verifyFormat("void switchTest4(int a)\n" |
21670 | " {\n" |
21671 | " switch (a)\n" |
21672 | " {\n" |
21673 | " case 2:\n" |
21674 | " {\n" |
21675 | " }\n" |
21676 | " break;\n" |
21677 | " }\n" |
21678 | " }", |
21679 | WhitesmithsBraceStyle); |
21680 | |
21681 | verifyFormat("void switchTest5(int a)\n" |
21682 | " {\n" |
21683 | " switch (a)\n" |
21684 | " {\n" |
21685 | " case 0:\n" |
21686 | " break;\n" |
21687 | " case 1:\n" |
21688 | " {\n" |
21689 | " foo();\n" |
21690 | " break;\n" |
21691 | " }\n" |
21692 | " case 2:\n" |
21693 | " {\n" |
21694 | " }\n" |
21695 | " break;\n" |
21696 | " default:\n" |
21697 | " break;\n" |
21698 | " }\n" |
21699 | " }", |
21700 | WhitesmithsBraceStyle); |
21701 | |
21702 | verifyFormat("void switchTest6(int a)\n" |
21703 | " {\n" |
21704 | " switch (a)\n" |
21705 | " {\n" |
21706 | " case 0:\n" |
21707 | " {\n" |
21708 | " foo(x);\n" |
21709 | " }\n" |
21710 | " break;\n" |
21711 | " default:\n" |
21712 | " {\n" |
21713 | " foo(1);\n" |
21714 | " }\n" |
21715 | " break;\n" |
21716 | " }\n" |
21717 | " }", |
21718 | WhitesmithsBraceStyle); |
21719 | |
21720 | verifyFormat("enum X\n" |
21721 | " {\n" |
21722 | " Y = 0, // testing\n" |
21723 | " }", |
21724 | WhitesmithsBraceStyle); |
21725 | |
21726 | verifyFormat("enum X\n" |
21727 | " {\n" |
21728 | " Y = 0\n" |
21729 | " }", |
21730 | WhitesmithsBraceStyle); |
21731 | verifyFormat("enum X\n" |
21732 | " {\n" |
21733 | " Y = 0,\n" |
21734 | " Z = 1\n" |
21735 | " };", |
21736 | WhitesmithsBraceStyle); |
21737 | |
21738 | verifyFormat("@interface BSApplicationController ()\n" |
21739 | " {\n" |
21740 | "@private\n" |
21741 | " id _extraIvar;\n" |
21742 | " }\n" |
21743 | "@end", |
21744 | WhitesmithsBraceStyle); |
21745 | |
21746 | verifyFormat("#ifdef _DEBUG\n" |
21747 | "int foo(int i = 0)\n" |
21748 | "#else\n" |
21749 | "int foo(int i = 5)\n" |
21750 | "#endif\n" |
21751 | " {\n" |
21752 | " return i;\n" |
21753 | " }", |
21754 | WhitesmithsBraceStyle); |
21755 | |
21756 | verifyFormat("void foo() {}\n" |
21757 | "void bar()\n" |
21758 | "#ifdef _DEBUG\n" |
21759 | " {\n" |
21760 | " foo();\n" |
21761 | " }\n" |
21762 | "#else\n" |
21763 | " {\n" |
21764 | " }\n" |
21765 | "#endif", |
21766 | WhitesmithsBraceStyle); |
21767 | |
21768 | verifyFormat("void foobar()\n" |
21769 | " {\n" |
21770 | " int i = 5;\n" |
21771 | " }\n" |
21772 | "#ifdef _DEBUG\n" |
21773 | "void bar() {}\n" |
21774 | "#else\n" |
21775 | "void bar()\n" |
21776 | " {\n" |
21777 | " foobar();\n" |
21778 | " }\n" |
21779 | "#endif", |
21780 | WhitesmithsBraceStyle); |
21781 | |
21782 | // This shouldn't affect ObjC blocks.. |
21783 | verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" |
21784 | " // ...\n" |
21785 | " int i;\n" |
21786 | "}];", |
21787 | WhitesmithsBraceStyle); |
21788 | verifyFormat("void (^block)(void) = ^{\n" |
21789 | " // ...\n" |
21790 | " int i;\n" |
21791 | "};", |
21792 | WhitesmithsBraceStyle); |
21793 | // .. or dict literals. |
21794 | verifyFormat("void f()\n" |
21795 | " {\n" |
21796 | " [object someMethod:@{@\"a\" : @\"b\"}];\n" |
21797 | " }", |
21798 | WhitesmithsBraceStyle); |
21799 | |
21800 | verifyFormat("int f()\n" |
21801 | " { // comment\n" |
21802 | " return 42;\n" |
21803 | " }", |
21804 | WhitesmithsBraceStyle); |
21805 | |
21806 | FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; |
21807 | BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = |
21808 | FormatStyle::SIS_OnlyFirstIf; |
21809 | BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; |
21810 | verifyFormat("void f(bool b)\n" |
21811 | " {\n" |
21812 | " if (b)\n" |
21813 | " {\n" |
21814 | " return;\n" |
21815 | " }\n" |
21816 | " }", |
21817 | BreakBeforeBraceShortIfs); |
21818 | verifyFormat("void f(bool b)\n" |
21819 | " {\n" |
21820 | " if (b) return;\n" |
21821 | " }", |
21822 | BreakBeforeBraceShortIfs); |
21823 | verifyFormat("void f(bool b)\n" |
21824 | " {\n" |
21825 | " while (b)\n" |
21826 | " {\n" |
21827 | " return;\n" |
21828 | " }\n" |
21829 | " }", |
21830 | BreakBeforeBraceShortIfs); |
21831 | } |
21832 | |
21833 | TEST_F(FormatTest, GNUBraceBreaking) { |
21834 | FormatStyle GNUBraceStyle = getLLVMStyle(); |
21835 | GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; |
21836 | verifyFormat("namespace a\n" |
21837 | "{\n" |
21838 | "class A\n" |
21839 | "{\n" |
21840 | " void f()\n" |
21841 | " {\n" |
21842 | " int a;\n" |
21843 | " {\n" |
21844 | " int b;\n" |
21845 | " }\n" |
21846 | " if (true)\n" |
21847 | " {\n" |
21848 | " a();\n" |
21849 | " b();\n" |
21850 | " }\n" |
21851 | " }\n" |
21852 | " void g() { return; }\n" |
21853 | "}\n" |
21854 | "} // namespace a", |
21855 | GNUBraceStyle); |
21856 | |
21857 | verifyFormat("void f()\n" |
21858 | "{\n" |
21859 | " if (true)\n" |
21860 | " {\n" |
21861 | " a();\n" |
21862 | " }\n" |
21863 | " else if (false)\n" |
21864 | " {\n" |
21865 | " b();\n" |
21866 | " }\n" |
21867 | " else\n" |
21868 | " {\n" |
21869 | " c();\n" |
21870 | " }\n" |
21871 | "}", |
21872 | GNUBraceStyle); |
21873 | |
21874 | verifyFormat("void f()\n" |
21875 | "{\n" |
21876 | " for (int i = 0; i < 10; ++i)\n" |
21877 | " {\n" |
21878 | " a();\n" |
21879 | " }\n" |
21880 | " while (false)\n" |
21881 | " {\n" |
21882 | " b();\n" |
21883 | " }\n" |
21884 | " do\n" |
21885 | " {\n" |
21886 | " c();\n" |
21887 | " }\n" |
21888 | " while (false);\n" |
21889 | "}", |
21890 | GNUBraceStyle); |
21891 | |
21892 | verifyFormat("void f(int a)\n" |
21893 | "{\n" |
21894 | " switch (a)\n" |
21895 | " {\n" |
21896 | " case 0:\n" |
21897 | " break;\n" |
21898 | " case 1:\n" |
21899 | " {\n" |
21900 | " break;\n" |
21901 | " }\n" |
21902 | " case 2:\n" |
21903 | " {\n" |
21904 | " }\n" |
21905 | " break;\n" |
21906 | " default:\n" |
21907 | " break;\n" |
21908 | " }\n" |
21909 | "}", |
21910 | GNUBraceStyle); |
21911 | |
21912 | verifyFormat("enum X\n" |
21913 | "{\n" |
21914 | " Y = 0,\n" |
21915 | "}", |
21916 | GNUBraceStyle); |
21917 | |
21918 | verifyFormat("@interface BSApplicationController ()\n" |
21919 | "{\n" |
21920 | "@private\n" |
21921 | " id _extraIvar;\n" |
21922 | "}\n" |
21923 | "@end", |
21924 | GNUBraceStyle); |
21925 | |
21926 | verifyFormat("#ifdef _DEBUG\n" |
21927 | "int foo(int i = 0)\n" |
21928 | "#else\n" |
21929 | "int foo(int i = 5)\n" |
21930 | "#endif\n" |
21931 | "{\n" |
21932 | " return i;\n" |
21933 | "}", |
21934 | GNUBraceStyle); |
21935 | |
21936 | verifyFormat("void foo() {}\n" |
21937 | "void bar()\n" |
21938 | "#ifdef _DEBUG\n" |
21939 | "{\n" |
21940 | " foo();\n" |
21941 | "}\n" |
21942 | "#else\n" |
21943 | "{\n" |
21944 | "}\n" |
21945 | "#endif", |
21946 | GNUBraceStyle); |
21947 | |
21948 | verifyFormat("void foobar() { int i = 5; }\n" |
21949 | "#ifdef _DEBUG\n" |
21950 | "void bar() {}\n" |
21951 | "#else\n" |
21952 | "void bar() { foobar(); }\n" |
21953 | "#endif", |
21954 | GNUBraceStyle); |
21955 | } |
21956 | |
21957 | TEST_F(FormatTest, WebKitBraceBreaking) { |
21958 | FormatStyle WebKitBraceStyle = getLLVMStyle(); |
21959 | WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; |
21960 | WebKitBraceStyle.FixNamespaceComments = false; |
21961 | verifyFormat("namespace a {\n" |
21962 | "class A {\n" |
21963 | " void f()\n" |
21964 | " {\n" |
21965 | " if (true) {\n" |
21966 | " a();\n" |
21967 | " b();\n" |
21968 | " }\n" |
21969 | " }\n" |
21970 | " void g() { return; }\n" |
21971 | "};\n" |
21972 | "enum E {\n" |
21973 | " A,\n" |
21974 | " // foo\n" |
21975 | " B,\n" |
21976 | " C\n" |
21977 | "};\n" |
21978 | "struct B {\n" |
21979 | " int x;\n" |
21980 | "};\n" |
21981 | "}", |
21982 | WebKitBraceStyle); |
21983 | verifyFormat("struct S {\n" |
21984 | " int Type;\n" |
21985 | " union {\n" |
21986 | " int x;\n" |
21987 | " double y;\n" |
21988 | " } Value;\n" |
21989 | " class C {\n" |
21990 | " MyFavoriteType Value;\n" |
21991 | " } Class;\n" |
21992 | "};", |
21993 | WebKitBraceStyle); |
21994 | } |
21995 | |
21996 | TEST_F(FormatTest, CatchExceptionReferenceBinding) { |
21997 | verifyFormat("void f() {\n" |
21998 | " try {\n" |
21999 | " } catch (const Exception &e) {\n" |
22000 | " }\n" |
22001 | "}"); |
22002 | } |
22003 | |
22004 | TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { |
22005 | auto Style = getLLVMStyle(); |
22006 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; |
22007 | verifyNoCrash(Code: "f({\n" |
22008 | "table({}, table({{\"\", false}}, {}))\n" |
22009 | "});", |
22010 | Style); |
22011 | |
22012 | Style.AlignConsecutiveAssignments.Enabled = true; |
22013 | Style.AlignConsecutiveDeclarations.Enabled = true; |
22014 | verifyFormat("struct test demo[] = {\n" |
22015 | " {56, 23, \"hello\"},\n" |
22016 | " {-1, 93463, \"world\"},\n" |
22017 | " { 7, 5, \"!!\"}\n" |
22018 | "};", |
22019 | Style); |
22020 | |
22021 | verifyFormat("struct test demo[] = {\n" |
22022 | " {56, 23, \"hello\"}, // first line\n" |
22023 | " {-1, 93463, \"world\"}, // second line\n" |
22024 | " { 7, 5, \"!!\"} // third line\n" |
22025 | "};", |
22026 | Style); |
22027 | |
22028 | verifyFormat("struct test demo[4] = {\n" |
22029 | " { 56, 23, 21, \"oh\"}, // first line\n" |
22030 | " { -1, 93463, 22, \"my\"}, // second line\n" |
22031 | " { 7, 5, 1, \"goodness\"} // third line\n" |
22032 | " {234, 5, 1, \"gracious\"} // fourth line\n" |
22033 | "};", |
22034 | Style); |
22035 | |
22036 | verifyFormat("struct test demo[3] = {\n" |
22037 | " {56, 23, \"hello\"},\n" |
22038 | " {-1, 93463, \"world\"},\n" |
22039 | " { 7, 5, \"!!\"}\n" |
22040 | "};", |
22041 | Style); |
22042 | |
22043 | verifyFormat("struct test demo[3] = {\n" |
22044 | " {int{56}, 23, \"hello\"},\n" |
22045 | " {int{-1}, 93463, \"world\"},\n" |
22046 | " { int{7}, 5, \"!!\"}\n" |
22047 | "};", |
22048 | Style); |
22049 | |
22050 | verifyFormat("struct test demo[] = {\n" |
22051 | " {56, 23, \"hello\"},\n" |
22052 | " {-1, 93463, \"world\"},\n" |
22053 | " { 7, 5, \"!!\"},\n" |
22054 | "};", |
22055 | Style); |
22056 | |
22057 | verifyFormat("test demo[] = {\n" |
22058 | " {56, 23, \"hello\"},\n" |
22059 | " {-1, 93463, \"world\"},\n" |
22060 | " { 7, 5, \"!!\"},\n" |
22061 | "};", |
22062 | Style); |
22063 | |
22064 | verifyFormat("demo = std::array<struct test, 3>{\n" |
22065 | " test{56, 23, \"hello\"},\n" |
22066 | " test{-1, 93463, \"world\"},\n" |
22067 | " test{ 7, 5, \"!!\"},\n" |
22068 | "};", |
22069 | Style); |
22070 | |
22071 | verifyFormat("test demo[] = {\n" |
22072 | " {56, 23, \"hello\"},\n" |
22073 | "#if X\n" |
22074 | " {-1, 93463, \"world\"},\n" |
22075 | "#endif\n" |
22076 | " { 7, 5, \"!!\"}\n" |
22077 | "};", |
22078 | Style); |
22079 | |
22080 | verifyFormat( |
22081 | "test demo[] = {\n" |
22082 | " { 7, 23,\n" |
22083 | " \"hello world i am a very long line that really, in any\"\n" |
22084 | " \"just world, ought to be split over multiple lines\"},\n" |
22085 | " {-1, 93463, \"world\"},\n" |
22086 | " {56, 5, \"!!\"}\n" |
22087 | "};", |
22088 | Style); |
22089 | |
22090 | verifyNoCrash(Code: "Foo f[] = {\n" |
22091 | " [0] = { 1, },\n" |
22092 | " [i] { 1, },\n" |
22093 | "};", |
22094 | Style); |
22095 | verifyNoCrash(Code: "Foo foo[] = {\n" |
22096 | " [0] = {1, 1},\n" |
22097 | " [1] { 1, 1, },\n" |
22098 | " [2] { 1, 1, },\n" |
22099 | "};", |
22100 | Style); |
22101 | verifyNoCrash(Code: "test arr[] = {\n" |
22102 | "#define FOO(i) {i, i},\n" |
22103 | "SOME_GENERATOR(FOO)\n" |
22104 | "{2, 2}\n" |
22105 | "};", |
22106 | Style); |
22107 | |
22108 | verifyFormat("return GradForUnaryCwise(g, {\n" |
22109 | " {{\"sign\"}, \"Sign\", " |
22110 | " {\"x\", \"dy\"}},\n" |
22111 | " { {\"dx\"}, \"Mul\", {\"dy\"" |
22112 | ", \"sign\"}},\n" |
22113 | "});", |
22114 | Style); |
22115 | |
22116 | Style.Cpp11BracedListStyle = false; |
22117 | verifyFormat("struct test demo[] = {\n" |
22118 | " { 56, 23, \"hello\" },\n" |
22119 | " { -1, 93463, \"world\" },\n" |
22120 | " { 7, 5, \"!!\" }\n" |
22121 | "};", |
22122 | Style); |
22123 | Style.Cpp11BracedListStyle = true; |
22124 | |
22125 | Style.ColumnLimit = 0; |
22126 | verifyFormat( |
22127 | "test demo[] = {\n" |
22128 | " {56, 23, \"hello world i am a very long line that really, " |
22129 | "in any just world, ought to be split over multiple lines\"},\n" |
22130 | " {-1, 93463, " |
22131 | " \"world\"},\n" |
22132 | " { 7, 5, " |
22133 | " \"!!\"},\n" |
22134 | "};", |
22135 | "test demo[] = {{56, 23, \"hello world i am a very long line " |
22136 | "that really, in any just world, ought to be split over multiple " |
22137 | "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", |
22138 | Style); |
22139 | |
22140 | Style.ColumnLimit = 80; |
22141 | verifyFormat("test demo[] = {\n" |
22142 | " {56, 23, /* a comment */ \"hello\"},\n" |
22143 | " {-1, 93463, \"world\"},\n" |
22144 | " { 7, 5, \"!!\"}\n" |
22145 | "};", |
22146 | Style); |
22147 | |
22148 | verifyFormat("test demo[] = {\n" |
22149 | " {56, 23, \"hello\"},\n" |
22150 | " {-1, 93463, \"world\" /* comment here */},\n" |
22151 | " { 7, 5, \"!!\"}\n" |
22152 | "};", |
22153 | Style); |
22154 | |
22155 | verifyFormat("test demo[] = {\n" |
22156 | " {56, /* a comment */ 23, \"hello\"},\n" |
22157 | " {-1, 93463, \"world\"},\n" |
22158 | " { 7, 5, \"!!\"}\n" |
22159 | "};", |
22160 | Style); |
22161 | |
22162 | Style.ColumnLimit = 20; |
22163 | verifyFormat("demo = std::array<\n" |
22164 | " struct test, 3>{\n" |
22165 | " test{\n" |
22166 | " 56, 23,\n" |
22167 | " \"hello \"\n" |
22168 | " \"world i \"\n" |
22169 | " \"am a very \"\n" |
22170 | " \"long line \"\n" |
22171 | " \"that \"\n" |
22172 | " \"really, \"\n" |
22173 | " \"in any \"\n" |
22174 | " \"just \"\n" |
22175 | " \"world, \"\n" |
22176 | " \"ought to \"\n" |
22177 | " \"be split \"\n" |
22178 | " \"over \"\n" |
22179 | " \"multiple \"\n" |
22180 | " \"lines\"},\n" |
22181 | " test{-1, 93463,\n" |
22182 | " \"world\"},\n" |
22183 | " test{ 7, 5,\n" |
22184 | " \"!!\" },\n" |
22185 | "};", |
22186 | "demo = std::array<struct test, 3>{test{56, 23, \"hello world " |
22187 | "i am a very long line that really, in any just world, ought " |
22188 | "to be split over multiple lines\"},test{-1, 93463, \"world\"}," |
22189 | "test{7, 5, \"!!\"},};", |
22190 | Style); |
22191 | // This caused a core dump by enabling Alignment in the LLVMStyle globally |
22192 | Style = getLLVMStyleWithColumns(ColumnLimit: 50); |
22193 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; |
22194 | verifyFormat("static A x = {\n" |
22195 | " {{init1, init2, init3, init4},\n" |
22196 | " {init1, init2, init3, init4}}\n" |
22197 | "};", |
22198 | Style); |
22199 | // TODO: Fix the indentations below when this option is fully functional. |
22200 | #if 0 |
22201 | verifyFormat("int a[][] = {\n" |
22202 | " {\n" |
22203 | " {0, 2}, //\n" |
22204 | " {1, 2} //\n" |
22205 | " }\n" |
22206 | "};", |
22207 | Style); |
22208 | #endif |
22209 | Style.ColumnLimit = 100; |
22210 | verifyFormat( |
22211 | "test demo[] = {\n" |
22212 | " {56, 23,\n" |
22213 | " \"hello world i am a very long line that really, in any just world" |
22214 | ", ought to be split over \"\n" |
22215 | " \"multiple lines\" },\n" |
22216 | " {-1, 93463, \"world\"},\n" |
22217 | " { 7, 5, \"!!\"},\n" |
22218 | "};", |
22219 | "test demo[] = {{56, 23, \"hello world i am a very long line " |
22220 | "that really, in any just world, ought to be split over multiple " |
22221 | "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", |
22222 | Style); |
22223 | |
22224 | Style = getLLVMStyleWithColumns(ColumnLimit: 50); |
22225 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; |
22226 | verifyFormat("struct test demo[] = {\n" |
22227 | " {56, 23, \"hello\"},\n" |
22228 | " {-1, 93463, \"world\"},\n" |
22229 | " { 7, 5, \"!!\"}\n" |
22230 | "};\n" |
22231 | "static A x = {\n" |
22232 | " {{init1, init2, init3, init4},\n" |
22233 | " {init1, init2, init3, init4}}\n" |
22234 | "};", |
22235 | Style); |
22236 | Style.ColumnLimit = 100; |
22237 | Style.AlignConsecutiveAssignments.AcrossComments = true; |
22238 | Style.AlignConsecutiveDeclarations.AcrossComments = true; |
22239 | verifyFormat("struct test demo[] = {\n" |
22240 | " {56, 23, \"hello\"},\n" |
22241 | " {-1, 93463, \"world\"},\n" |
22242 | " { 7, 5, \"!!\"}\n" |
22243 | "};\n" |
22244 | "struct test demo[4] = {\n" |
22245 | " { 56, 23, 21, \"oh\"}, // first line\n" |
22246 | " { -1, 93463, 22, \"my\"}, // second line\n" |
22247 | " { 7, 5, 1, \"goodness\"} // third line\n" |
22248 | " {234, 5, 1, \"gracious\"} // fourth line\n" |
22249 | "};", |
22250 | Style); |
22251 | verifyFormat( |
22252 | "test demo[] = {\n" |
22253 | " {56,\n" |
22254 | " \"hello world i am a very long line that really, in any just world" |
22255 | ", ought to be split over \"\n" |
22256 | " \"multiple lines\", 23},\n" |
22257 | " {-1, \"world\", 93463},\n" |
22258 | " { 7, \"!!\", 5},\n" |
22259 | "};", |
22260 | "test demo[] = {{56, \"hello world i am a very long line " |
22261 | "that really, in any just world, ought to be split over multiple " |
22262 | "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", |
22263 | Style); |
22264 | } |
22265 | |
22266 | TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { |
22267 | auto Style = getLLVMStyle(); |
22268 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; |
22269 | /* FIXME: This case gets misformatted. |
22270 | verifyFormat("auto foo = Items{\n" |
22271 | " Section{0, bar(), },\n" |
22272 | " Section{1, boo() }\n" |
22273 | "};", |
22274 | Style); |
22275 | */ |
22276 | verifyFormat("auto foo = Items{\n" |
22277 | " Section{\n" |
22278 | " 0, bar(),\n" |
22279 | " }\n" |
22280 | "};", |
22281 | Style); |
22282 | verifyFormat("struct test demo[] = {\n" |
22283 | " {56, 23, \"hello\"},\n" |
22284 | " {-1, 93463, \"world\"},\n" |
22285 | " {7, 5, \"!!\" }\n" |
22286 | "};", |
22287 | Style); |
22288 | verifyFormat("struct test demo[] = {\n" |
22289 | " {56, 23, \"hello\"}, // first line\n" |
22290 | " {-1, 93463, \"world\"}, // second line\n" |
22291 | " {7, 5, \"!!\" } // third line\n" |
22292 | "};", |
22293 | Style); |
22294 | verifyFormat("struct test demo[4] = {\n" |
22295 | " {56, 23, 21, \"oh\" }, // first line\n" |
22296 | " {-1, 93463, 22, \"my\" }, // second line\n" |
22297 | " {7, 5, 1, \"goodness\"} // third line\n" |
22298 | " {234, 5, 1, \"gracious\"} // fourth line\n" |
22299 | "};", |
22300 | Style); |
22301 | verifyFormat("struct test demo[3] = {\n" |
22302 | " {56, 23, \"hello\"},\n" |
22303 | " {-1, 93463, \"world\"},\n" |
22304 | " {7, 5, \"!!\" }\n" |
22305 | "};", |
22306 | Style); |
22307 | |
22308 | verifyFormat("struct test demo[3] = {\n" |
22309 | " {int{56}, 23, \"hello\"},\n" |
22310 | " {int{-1}, 93463, \"world\"},\n" |
22311 | " {int{7}, 5, \"!!\" }\n" |
22312 | "};", |
22313 | Style); |
22314 | verifyFormat("struct test demo[] = {\n" |
22315 | " {56, 23, \"hello\"},\n" |
22316 | " {-1, 93463, \"world\"},\n" |
22317 | " {7, 5, \"!!\" },\n" |
22318 | "};", |
22319 | Style); |
22320 | verifyFormat("test demo[] = {\n" |
22321 | " {56, 23, \"hello\"},\n" |
22322 | " {-1, 93463, \"world\"},\n" |
22323 | " {7, 5, \"!!\" },\n" |
22324 | "};", |
22325 | Style); |
22326 | verifyFormat("demo = std::array<struct test, 3>{\n" |
22327 | " test{56, 23, \"hello\"},\n" |
22328 | " test{-1, 93463, \"world\"},\n" |
22329 | " test{7, 5, \"!!\" },\n" |
22330 | "};", |
22331 | Style); |
22332 | verifyFormat("test demo[] = {\n" |
22333 | " {56, 23, \"hello\"},\n" |
22334 | "#if X\n" |
22335 | " {-1, 93463, \"world\"},\n" |
22336 | "#endif\n" |
22337 | " {7, 5, \"!!\" }\n" |
22338 | "};", |
22339 | Style); |
22340 | verifyFormat( |
22341 | "test demo[] = {\n" |
22342 | " {7, 23,\n" |
22343 | " \"hello world i am a very long line that really, in any\"\n" |
22344 | " \"just world, ought to be split over multiple lines\"},\n" |
22345 | " {-1, 93463, \"world\" },\n" |
22346 | " {56, 5, \"!!\" }\n" |
22347 | "};", |
22348 | Style); |
22349 | |
22350 | verifyNoCrash(Code: "Foo f[] = {\n" |
22351 | " [0] = { 1, },\n" |
22352 | " [i] { 1, },\n" |
22353 | "};", |
22354 | Style); |
22355 | verifyNoCrash(Code: "Foo foo[] = {\n" |
22356 | " [0] = {1, 1},\n" |
22357 | " [1] { 1, 1, },\n" |
22358 | " [2] { 1, 1, },\n" |
22359 | "};", |
22360 | Style); |
22361 | verifyNoCrash(Code: "test arr[] = {\n" |
22362 | "#define FOO(i) {i, i},\n" |
22363 | "SOME_GENERATOR(FOO)\n" |
22364 | "{2, 2}\n" |
22365 | "};", |
22366 | Style); |
22367 | |
22368 | verifyFormat("return GradForUnaryCwise(g, {\n" |
22369 | " {{\"sign\"}, \"Sign\", {\"x\", " |
22370 | "\"dy\"} },\n" |
22371 | " {{\"dx\"}, \"Mul\", " |
22372 | "{\"dy\", \"sign\"}},\n" |
22373 | "});", |
22374 | Style); |
22375 | |
22376 | Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; |
22377 | verifyFormat("#define FOO \\\n" |
22378 | " int foo[][2] = { \\\n" |
22379 | " {0, 1} \\\n" |
22380 | " };", |
22381 | Style); |
22382 | |
22383 | Style.Cpp11BracedListStyle = false; |
22384 | verifyFormat("struct test demo[] = {\n" |
22385 | " { 56, 23, \"hello\" },\n" |
22386 | " { -1, 93463, \"world\" },\n" |
22387 | " { 7, 5, \"!!\" }\n" |
22388 | "};", |
22389 | Style); |
22390 | Style.Cpp11BracedListStyle = true; |
22391 | |
22392 | Style.ColumnLimit = 0; |
22393 | verifyFormat( |
22394 | "test demo[] = {\n" |
22395 | " {56, 23, \"hello world i am a very long line that really, in any " |
22396 | "just world, ought to be split over multiple lines\"},\n" |
22397 | " {-1, 93463, \"world\" " |
22398 | " },\n" |
22399 | " {7, 5, \"!!\" " |
22400 | " },\n" |
22401 | "};", |
22402 | "test demo[] = {{56, 23, \"hello world i am a very long line " |
22403 | "that really, in any just world, ought to be split over multiple " |
22404 | "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", |
22405 | Style); |
22406 | |
22407 | Style.ColumnLimit = 80; |
22408 | verifyFormat("test demo[] = {\n" |
22409 | " {56, 23, /* a comment */ \"hello\"},\n" |
22410 | " {-1, 93463, \"world\" },\n" |
22411 | " {7, 5, \"!!\" }\n" |
22412 | "};", |
22413 | Style); |
22414 | |
22415 | verifyFormat("test demo[] = {\n" |
22416 | " {56, 23, \"hello\" },\n" |
22417 | " {-1, 93463, \"world\" /* comment here */},\n" |
22418 | " {7, 5, \"!!\" }\n" |
22419 | "};", |
22420 | Style); |
22421 | |
22422 | verifyFormat("test demo[] = {\n" |
22423 | " {56, /* a comment */ 23, \"hello\"},\n" |
22424 | " {-1, 93463, \"world\"},\n" |
22425 | " {7, 5, \"!!\" }\n" |
22426 | "};", |
22427 | Style); |
22428 | verifyFormat("Foo foo = {\n" |
22429 | " // comment\n" |
22430 | " {1, 2}\n" |
22431 | "};", |
22432 | Style); |
22433 | |
22434 | Style.ColumnLimit = 20; |
22435 | // FIXME: unstable test case |
22436 | EXPECT_EQ( |
22437 | "demo = std::array<\n" |
22438 | " struct test, 3>{\n" |
22439 | " test{\n" |
22440 | " 56, 23,\n" |
22441 | " \"hello \"\n" |
22442 | " \"world i \"\n" |
22443 | " \"am a very \"\n" |
22444 | " \"long line \"\n" |
22445 | " \"that \"\n" |
22446 | " \"really, \"\n" |
22447 | " \"in any \"\n" |
22448 | " \"just \"\n" |
22449 | " \"world, \"\n" |
22450 | " \"ought to \"\n" |
22451 | " \"be split \"\n" |
22452 | " \"over \"\n" |
22453 | " \"multiple \"\n" |
22454 | " \"lines\"},\n" |
22455 | " test{-1, 93463,\n" |
22456 | " \"world\"},\n" |
22457 | " test{7, 5,\n" |
22458 | " \"!!\" },\n" |
22459 | "};", |
22460 | format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " |
22461 | "i am a very long line that really, in any just world, ought " |
22462 | "to be split over multiple lines\"},test{-1, 93463, \"world\"}," |
22463 | "test{7, 5, \"!!\"},};", |
22464 | Style)); |
22465 | |
22466 | // This caused a core dump by enabling Alignment in the LLVMStyle globally |
22467 | Style = getLLVMStyleWithColumns(ColumnLimit: 50); |
22468 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; |
22469 | verifyFormat("static A x = {\n" |
22470 | " {{init1, init2, init3, init4},\n" |
22471 | " {init1, init2, init3, init4}}\n" |
22472 | "};", |
22473 | Style); |
22474 | Style.ColumnLimit = 100; |
22475 | verifyFormat( |
22476 | "test demo[] = {\n" |
22477 | " {56, 23,\n" |
22478 | " \"hello world i am a very long line that really, in any just world" |
22479 | ", ought to be split over \"\n" |
22480 | " \"multiple lines\" },\n" |
22481 | " {-1, 93463, \"world\"},\n" |
22482 | " {7, 5, \"!!\" },\n" |
22483 | "};", |
22484 | "test demo[] = {{56, 23, \"hello world i am a very long line " |
22485 | "that really, in any just world, ought to be split over multiple " |
22486 | "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", |
22487 | Style); |
22488 | |
22489 | Style.ColumnLimit = 25; |
22490 | verifyNoCrash(Code: "Type foo{\n" |
22491 | " {\n" |
22492 | " 1, // A\n" |
22493 | " 2, // B\n" |
22494 | " 3, // C\n" |
22495 | " },\n" |
22496 | " \"hello\",\n" |
22497 | "};", |
22498 | Style); |
22499 | verifyNoCrash(Code: "Type object[X][Y] = {\n" |
22500 | " {{val}, {val}, {val}},\n" |
22501 | " {{val}, {val}, // some comment\n" |
22502 | " {val}}\n" |
22503 | "};", |
22504 | Style); |
22505 | |
22506 | Style.ColumnLimit = 120; |
22507 | verifyNoCrash( |
22508 | Code: "T v[] {\n" |
22509 | " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, " |
22510 | "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, " |
22511 | "\"00000000000000000000000000000000000000000000000000000000" |
22512 | "00000000000000000000000000000000000000000000000000000000\" },\n" |
22513 | "};", |
22514 | Style); |
22515 | |
22516 | Style.SpacesInParens = FormatStyle::SIPO_Custom; |
22517 | Style.SpacesInParensOptions.Other = true; |
22518 | verifyFormat("Foo foo[] = {\n" |
22519 | " {1, 1},\n" |
22520 | " {1, 1},\n" |
22521 | "};", |
22522 | Style); |
22523 | } |
22524 | |
22525 | TEST_F(FormatTest, UnderstandsPragmas) { |
22526 | verifyFormat("#pragma omp reduction(| : var)"); |
22527 | verifyFormat("#pragma omp reduction(+ : var)"); |
22528 | |
22529 | verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " |
22530 | "(including parentheses).", |
22531 | "#pragma mark Any non-hyphenated or hyphenated string " |
22532 | "(including parentheses)."); |
22533 | |
22534 | verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " |
22535 | "(including parentheses).", |
22536 | "#pragma mark Any non-hyphenated or hyphenated string " |
22537 | "(including parentheses)."); |
22538 | |
22539 | verifyFormat("#pragma comment(linker, \\\n" |
22540 | " \"argument\" \\\n" |
22541 | " \"argument\"", |
22542 | "#pragma comment(linker, \\\n" |
22543 | " \"argument\" \\\n" |
22544 | " \"argument\"", |
22545 | getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32)); |
22546 | } |
22547 | |
22548 | TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { |
22549 | verifyFormat("#pragma omp target map(to : var)"); |
22550 | verifyFormat("#pragma omp target map(to : var[ : N])"); |
22551 | verifyFormat("#pragma omp target map(to : var[0 : N])"); |
22552 | verifyFormat("#pragma omp target map(always, to : var[0 : N])"); |
22553 | |
22554 | verifyFormat( |
22555 | "#pragma omp target \\\n" |
22556 | " reduction(+ : var) \\\n" |
22557 | " map(to : A[0 : N]) \\\n" |
22558 | " map(to : B[0 : N]) \\\n" |
22559 | " map(from : C[0 : N]) \\\n" |
22560 | " firstprivate(i) \\\n" |
22561 | " firstprivate(j) \\\n" |
22562 | " firstprivate(k)", |
22563 | "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) " |
22564 | "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)", |
22565 | getLLVMStyleWithColumns(26)); |
22566 | } |
22567 | |
22568 | TEST_F(FormatTest, UnderstandPragmaOption) { |
22569 | verifyFormat("#pragma option -C -A"); |
22570 | |
22571 | verifyFormat("#pragma option -C -A", "#pragma option -C -A"); |
22572 | } |
22573 | |
22574 | TEST_F(FormatTest, UnderstandPragmaRegion) { |
22575 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 0); |
22576 | verifyFormat("#pragma region TEST(FOO : BAR)", Style); |
22577 | verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style); |
22578 | } |
22579 | |
22580 | TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { |
22581 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20); |
22582 | |
22583 | // See PR41213 |
22584 | verifyFormat("/*\n" |
22585 | " *\t9012345\n" |
22586 | " * /8901\n" |
22587 | " */", |
22588 | "/*\n" |
22589 | " *\t9012345 /8901\n" |
22590 | " */", |
22591 | Style); |
22592 | verifyFormat("/*\n" |
22593 | " *345678\n" |
22594 | " *\t/8901\n" |
22595 | " */", |
22596 | "/*\n" |
22597 | " *345678\t/8901\n" |
22598 | " */", |
22599 | Style); |
22600 | |
22601 | verifyFormat("int a; // the\n" |
22602 | " // comment", |
22603 | Style); |
22604 | verifyNoChange("int a; /* first line\n" |
22605 | " * second\n" |
22606 | " * line third\n" |
22607 | " * line\n" |
22608 | " */", |
22609 | Style); |
22610 | verifyFormat("int a; // first line\n" |
22611 | " // second\n" |
22612 | " // line third\n" |
22613 | " // line", |
22614 | "int a; // first line\n" |
22615 | " // second line\n" |
22616 | " // third line", |
22617 | Style); |
22618 | |
22619 | Style.PenaltyExcessCharacter = 90; |
22620 | verifyFormat("int a; // the comment", Style); |
22621 | verifyFormat("int a; // the comment\n" |
22622 | " // aaa", |
22623 | "int a; // the comment aaa", Style); |
22624 | verifyNoChange("int a; /* first line\n" |
22625 | " * second line\n" |
22626 | " * third line\n" |
22627 | " */", |
22628 | Style); |
22629 | verifyFormat("int a; // first line\n" |
22630 | " // second line\n" |
22631 | " // third line", |
22632 | Style); |
22633 | // FIXME: Investigate why this is not getting the same layout as the test |
22634 | // above. |
22635 | verifyFormat("int a; /* first line\n" |
22636 | " * second line\n" |
22637 | " * third line\n" |
22638 | " */", |
22639 | "int a; /* first line second line third line" |
22640 | "\n*/", |
22641 | Style); |
22642 | |
22643 | verifyFormat("// foo bar baz bazfoo\n" |
22644 | "// foo bar foo bar", |
22645 | "// foo bar baz bazfoo\n" |
22646 | "// foo bar foo bar", |
22647 | Style); |
22648 | verifyFormat("// foo bar baz bazfoo\n" |
22649 | "// foo bar foo bar", |
22650 | "// foo bar baz bazfoo\n" |
22651 | "// foo bar foo bar", |
22652 | Style); |
22653 | |
22654 | // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the |
22655 | // next one. |
22656 | verifyFormat("// foo bar baz bazfoo\n" |
22657 | "// bar foo bar", |
22658 | "// foo bar baz bazfoo bar\n" |
22659 | "// foo bar", |
22660 | Style); |
22661 | |
22662 | // FIXME: unstable test case |
22663 | EXPECT_EQ("// foo bar baz bazfoo\n" |
22664 | "// foo bar baz bazfoo\n" |
22665 | "// bar foo bar", |
22666 | format("// foo bar baz bazfoo\n" |
22667 | "// foo bar baz bazfoo bar\n" |
22668 | "// foo bar", |
22669 | Style)); |
22670 | |
22671 | // FIXME: unstable test case |
22672 | EXPECT_EQ("// foo bar baz bazfoo\n" |
22673 | "// foo bar baz bazfoo\n" |
22674 | "// bar foo bar", |
22675 | format("// foo bar baz bazfoo\n" |
22676 | "// foo bar baz bazfoo bar\n" |
22677 | "// foo bar", |
22678 | Style)); |
22679 | |
22680 | // Make sure we do not keep protruding characters if strict mode reflow is |
22681 | // cheaper than keeping protruding characters. |
22682 | Style.ColumnLimit = 21; |
22683 | verifyFormat("// foo foo foo foo\n" |
22684 | "// foo foo foo foo\n" |
22685 | "// foo foo foo foo", |
22686 | "// foo foo foo foo foo foo foo foo foo foo foo foo", Style); |
22687 | |
22688 | verifyFormat("int a = /* long block\n" |
22689 | " comment */\n" |
22690 | " 42;", |
22691 | "int a = /* long block comment */ 42;", Style); |
22692 | } |
22693 | |
22694 | TEST_F(FormatTest, BreakPenaltyAfterLParen) { |
22695 | FormatStyle Style = getLLVMStyle(); |
22696 | Style.ColumnLimit = 8; |
22697 | Style.PenaltyExcessCharacter = 15; |
22698 | verifyFormat("int foo(\n" |
22699 | " int aaaaaaaaaaaaaaaaaaaaaaaa);", |
22700 | Style); |
22701 | Style.PenaltyBreakOpenParenthesis = 200; |
22702 | verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", |
22703 | "int foo(\n" |
22704 | " int aaaaaaaaaaaaaaaaaaaaaaaa);", |
22705 | Style); |
22706 | } |
22707 | |
22708 | TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { |
22709 | FormatStyle Style = getLLVMStyle(); |
22710 | Style.ColumnLimit = 5; |
22711 | Style.PenaltyExcessCharacter = 150; |
22712 | verifyFormat("foo((\n" |
22713 | " int)aaaaaaaaaaaaaaaaaaaaaaaa);", |
22714 | |
22715 | Style); |
22716 | Style.PenaltyBreakOpenParenthesis = 100'000; |
22717 | verifyFormat("foo((int)\n" |
22718 | " aaaaaaaaaaaaaaaaaaaaaaaa);", |
22719 | "foo((\n" |
22720 | "int)aaaaaaaaaaaaaaaaaaaaaaaa);", |
22721 | Style); |
22722 | } |
22723 | |
22724 | TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { |
22725 | FormatStyle Style = getLLVMStyle(); |
22726 | Style.ColumnLimit = 4; |
22727 | Style.PenaltyExcessCharacter = 100; |
22728 | verifyFormat("for (\n" |
22729 | " int iiiiiiiiiiiiiiiii =\n" |
22730 | " 0;\n" |
22731 | " iiiiiiiiiiiiiiiii <\n" |
22732 | " 2;\n" |
22733 | " iiiiiiiiiiiiiiiii++) {\n" |
22734 | "}", |
22735 | |
22736 | Style); |
22737 | Style.PenaltyBreakOpenParenthesis = 1250; |
22738 | verifyFormat("for (int iiiiiiiiiiiiiiiii =\n" |
22739 | " 0;\n" |
22740 | " iiiiiiiiiiiiiiiii <\n" |
22741 | " 2;\n" |
22742 | " iiiiiiiiiiiiiiiii++) {\n" |
22743 | "}", |
22744 | "for (\n" |
22745 | " int iiiiiiiiiiiiiiiii =\n" |
22746 | " 0;\n" |
22747 | " iiiiiiiiiiiiiiiii <\n" |
22748 | " 2;\n" |
22749 | " iiiiiiiiiiiiiiiii++) {\n" |
22750 | "}", |
22751 | Style); |
22752 | } |
22753 | |
22754 | TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) { |
22755 | auto Style = getLLVMStyle(); |
22756 | EXPECT_EQ(Style.PenaltyBreakBeforeMemberAccess, 150u); |
22757 | |
22758 | Style.ColumnLimit = 60; |
22759 | Style.PenaltyBreakBeforeMemberAccess = 110; |
22760 | verifyFormat("aaaaaaaa.aaaaaaaa.bbbbbbbb()\n" |
22761 | " .ccccccccccccccccccccc(dddddddd);\n" |
22762 | "aaaaaaaa.aaaaaaaa\n" |
22763 | " .bbbbbbbb(cccccccccccccccccccccccccccccccc);", |
22764 | Style); |
22765 | |
22766 | Style.ColumnLimit = 13; |
22767 | verifyFormat("foo->bar\n" |
22768 | " .b(a);", |
22769 | Style); |
22770 | } |
22771 | |
22772 | TEST_F(FormatTest, BreakPenaltyScopeResolution) { |
22773 | FormatStyle Style = getLLVMStyle(); |
22774 | Style.ColumnLimit = 20; |
22775 | Style.PenaltyExcessCharacter = 100; |
22776 | verifyFormat("unsigned long\n" |
22777 | "foo::bar();", |
22778 | Style); |
22779 | Style.PenaltyBreakScopeResolution = 10; |
22780 | verifyFormat("unsigned long foo::\n" |
22781 | " bar();", |
22782 | Style); |
22783 | } |
22784 | |
22785 | TEST_F(FormatTest, WorksFor8bitEncodings) { |
22786 | // FIXME: unstable test case |
22787 | EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" |
22788 | "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" |
22789 | "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" |
22790 | "\"\xef\xee\xf0\xf3...\"", |
22791 | format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " |
22792 | "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " |
22793 | "\xef\xee\xf0\xf3...\"", |
22794 | getLLVMStyleWithColumns(12))); |
22795 | } |
22796 | |
22797 | TEST_F(FormatTest, HandlesUTF8BOM) { |
22798 | verifyFormat("\xef\xbb\xbf"); |
22799 | verifyFormat("\xef\xbb\xbf#include <iostream>"); |
22800 | verifyFormat("\xef\xbb\xbf\n#include <iostream>"); |
22801 | |
22802 | auto Style = getLLVMStyle(); |
22803 | Style.KeepEmptyLines.AtStartOfFile = false; |
22804 | verifyFormat("\xef\xbb\xbf#include <iostream>", |
22805 | "\xef\xbb\xbf\n#include <iostream>", Style); |
22806 | } |
22807 | |
22808 | // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. |
22809 | #if !defined(_MSC_VER) |
22810 | |
22811 | TEST_F(FormatTest, CountsUTF8CharactersProperly) { |
22812 | verifyFormat("\"Однажды в студёную зимнюю пору...\"", |
22813 | getLLVMStyleWithColumns(35)); |
22814 | verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", |
22815 | getLLVMStyleWithColumns(31)); |
22816 | verifyFormat("// Однажды в студёную зимнюю пору...", |
22817 | getLLVMStyleWithColumns(36)); |
22818 | verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); |
22819 | verifyFormat("/* Однажды в студёную зимнюю пору... */", |
22820 | getLLVMStyleWithColumns(39)); |
22821 | verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", |
22822 | getLLVMStyleWithColumns(35)); |
22823 | } |
22824 | |
22825 | TEST_F(FormatTest, SplitsUTF8Strings) { |
22826 | // Non-printable characters' width is currently considered to be the length in |
22827 | // bytes in UTF8. The characters can be displayed in very different manner |
22828 | // (zero-width, single width with a substitution glyph, expanded to their code |
22829 | // (e.g. "<8d>"), so there's no single correct way to handle them. |
22830 | // FIXME: unstable test case |
22831 | EXPECT_EQ("\"aaaaÄ\"\n" |
22832 | "\"\xc2\x8d\";", |
22833 | format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); |
22834 | // FIXME: unstable test case |
22835 | EXPECT_EQ("\"aaaaaaaÄ\"\n" |
22836 | "\"\xc2\x8d\";", |
22837 | format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); |
22838 | // FIXME: unstable test case |
22839 | EXPECT_EQ("\"Однажды, в \"\n" |
22840 | "\"студёную \"\n" |
22841 | "\"зимнюю \"\n" |
22842 | "\"пору,\"", |
22843 | format("\"Однажды, в студёную зимнюю пору,\"", |
22844 | getLLVMStyleWithColumns(13))); |
22845 | // FIXME: unstable test case |
22846 | EXPECT_EQ( |
22847 | "\"一 二 三 \"\n" |
22848 | "\"四 五六 \"\n" |
22849 | "\"七 八 九 \"\n" |
22850 | "\"十\"", |
22851 | format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); |
22852 | // FIXME: unstable test case |
22853 | EXPECT_EQ("\"一\t\"\n" |
22854 | "\"二 \t\"\n" |
22855 | "\"三 四 \"\n" |
22856 | "\"五\t\"\n" |
22857 | "\"六 \t\"\n" |
22858 | "\"七 \"\n" |
22859 | "\"八九十\tqq\"", |
22860 | format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", |
22861 | getLLVMStyleWithColumns(11))); |
22862 | |
22863 | // UTF8 character in an escape sequence. |
22864 | // FIXME: unstable test case |
22865 | EXPECT_EQ("\"aaaaaa\"\n" |
22866 | "\"\\\xC2\x8D\"", |
22867 | format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); |
22868 | } |
22869 | |
22870 | TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { |
22871 | verifyFormat("const char *sssss =\n" |
22872 | " \"一二三四五六七八\\\n" |
22873 | " 九 十\";", |
22874 | "const char *sssss = \"一二三四五六七八\\\n" |
22875 | " 九 十\";", |
22876 | getLLVMStyleWithColumns(30)); |
22877 | } |
22878 | |
22879 | TEST_F(FormatTest, SplitsUTF8LineComments) { |
22880 | verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)); |
22881 | verifyFormat("// Я из лесу\n" |
22882 | "// вышел; был\n" |
22883 | "// сильный\n" |
22884 | "// мороз.", |
22885 | "// Я из лесу вышел; был сильный мороз.", |
22886 | getLLVMStyleWithColumns(13)); |
22887 | verifyFormat("// 一二三\n" |
22888 | "// 四五六七\n" |
22889 | "// 八 九\n" |
22890 | "// 十", |
22891 | "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)); |
22892 | } |
22893 | |
22894 | TEST_F(FormatTest, SplitsUTF8BlockComments) { |
22895 | verifyFormat("/* Гляжу,\n" |
22896 | " * поднимается\n" |
22897 | " * медленно в\n" |
22898 | " * гору\n" |
22899 | " * Лошадка,\n" |
22900 | " * везущая\n" |
22901 | " * хворосту\n" |
22902 | " * воз. */", |
22903 | "/* Гляжу, поднимается медленно в гору\n" |
22904 | " * Лошадка, везущая хворосту воз. */", |
22905 | getLLVMStyleWithColumns(13)); |
22906 | verifyFormat("/* 一二三\n" |
22907 | " * 四五六七\n" |
22908 | " * 八 九\n" |
22909 | " * 十 */", |
22910 | "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)); |
22911 | verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n" |
22912 | " * 𝕓𝕪𝕥𝕖\n" |
22913 | " * 𝖀𝕿𝕱-𝟠 */", |
22914 | "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)); |
22915 | } |
22916 | |
22917 | #endif // _MSC_VER |
22918 | |
22919 | TEST_F(FormatTest, ConstructorInitializerIndentWidth) { |
22920 | FormatStyle Style = getLLVMStyle(); |
22921 | |
22922 | Style.ConstructorInitializerIndentWidth = 4; |
22923 | verifyFormat( |
22924 | "SomeClass::Constructor()\n" |
22925 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
22926 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
22927 | Style); |
22928 | |
22929 | Style.ConstructorInitializerIndentWidth = 2; |
22930 | verifyFormat( |
22931 | "SomeClass::Constructor()\n" |
22932 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
22933 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
22934 | Style); |
22935 | |
22936 | Style.ConstructorInitializerIndentWidth = 0; |
22937 | verifyFormat( |
22938 | "SomeClass::Constructor()\n" |
22939 | ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
22940 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
22941 | Style); |
22942 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
22943 | verifyFormat( |
22944 | "SomeLongTemplateVariableName<\n" |
22945 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", |
22946 | Style); |
22947 | verifyFormat("bool smaller = 1 < " |
22948 | "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" |
22949 | " " |
22950 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
22951 | Style); |
22952 | |
22953 | Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; |
22954 | verifyFormat("SomeClass::Constructor() :\n" |
22955 | "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" |
22956 | "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", |
22957 | Style); |
22958 | } |
22959 | |
22960 | TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { |
22961 | FormatStyle Style = getLLVMStyle(); |
22962 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
22963 | Style.ConstructorInitializerIndentWidth = 4; |
22964 | verifyFormat("SomeClass::Constructor()\n" |
22965 | " : a(a)\n" |
22966 | " , b(b)\n" |
22967 | " , c(c) {}", |
22968 | Style); |
22969 | verifyFormat("SomeClass::Constructor()\n" |
22970 | " : a(a) {}", |
22971 | Style); |
22972 | |
22973 | Style.ColumnLimit = 0; |
22974 | verifyFormat("SomeClass::Constructor()\n" |
22975 | " : a(a) {}", |
22976 | Style); |
22977 | verifyFormat("SomeClass::Constructor() noexcept\n" |
22978 | " : a(a) {}", |
22979 | Style); |
22980 | verifyFormat("SomeClass::Constructor()\n" |
22981 | " : a(a)\n" |
22982 | " , b(b)\n" |
22983 | " , c(c) {}", |
22984 | Style); |
22985 | verifyFormat("SomeClass::Constructor()\n" |
22986 | " : a(a) {\n" |
22987 | " foo();\n" |
22988 | " bar();\n" |
22989 | "}", |
22990 | Style); |
22991 | |
22992 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
22993 | verifyFormat("SomeClass::Constructor()\n" |
22994 | " : a(a)\n" |
22995 | " , b(b)\n" |
22996 | " , c(c) {\n}", |
22997 | Style); |
22998 | verifyFormat("SomeClass::Constructor()\n" |
22999 | " : a(a) {\n}", |
23000 | Style); |
23001 | |
23002 | Style.ColumnLimit = 80; |
23003 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
23004 | Style.ConstructorInitializerIndentWidth = 2; |
23005 | verifyFormat("SomeClass::Constructor()\n" |
23006 | " : a(a)\n" |
23007 | " , b(b)\n" |
23008 | " , c(c) {}", |
23009 | Style); |
23010 | |
23011 | Style.ConstructorInitializerIndentWidth = 0; |
23012 | verifyFormat("SomeClass::Constructor()\n" |
23013 | ": a(a)\n" |
23014 | ", b(b)\n" |
23015 | ", c(c) {}", |
23016 | Style); |
23017 | |
23018 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
23019 | Style.ConstructorInitializerIndentWidth = 4; |
23020 | verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); |
23021 | verifyFormat( |
23022 | "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)", |
23023 | Style); |
23024 | verifyFormat( |
23025 | "SomeClass::Constructor()\n" |
23026 | " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", |
23027 | Style); |
23028 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
23029 | verifyFormat("SomeClass::Constructor()\n" |
23030 | " : aaaaaaaa(aaaaaaaa) {}", |
23031 | Style); |
23032 | verifyFormat("SomeClass::Constructor()\n" |
23033 | " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)", |
23034 | Style); |
23035 | verifyFormat( |
23036 | "SomeClass::Constructor()\n" |
23037 | " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", |
23038 | Style); |
23039 | |
23040 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; |
23041 | Style.ConstructorInitializerIndentWidth = 4; |
23042 | Style.ColumnLimit = 60; |
23043 | verifyFormat("SomeClass::Constructor()\n" |
23044 | " : aaaaaaaa(aaaaaaaa)\n" |
23045 | " , aaaaaaaa(aaaaaaaa)\n" |
23046 | " , aaaaaaaa(aaaaaaaa) {}", |
23047 | Style); |
23048 | Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; |
23049 | verifyFormat("SomeClass::Constructor()\n" |
23050 | " : aaaaaaaa(aaaaaaaa)\n" |
23051 | " , aaaaaaaa(aaaaaaaa)\n" |
23052 | " , aaaaaaaa(aaaaaaaa) {}", |
23053 | Style); |
23054 | } |
23055 | |
23056 | TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { |
23057 | FormatStyle Style = getLLVMStyle(); |
23058 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; |
23059 | Style.ConstructorInitializerIndentWidth = 4; |
23060 | verifyFormat("SomeClass::Constructor()\n" |
23061 | " : a{a}\n" |
23062 | " , b{b} {}", |
23063 | Style); |
23064 | verifyFormat("SomeClass::Constructor()\n" |
23065 | " : a{a}\n" |
23066 | "#if CONDITION\n" |
23067 | " , b{b}\n" |
23068 | "#endif\n" |
23069 | "{\n}", |
23070 | Style); |
23071 | Style.ConstructorInitializerIndentWidth = 2; |
23072 | verifyFormat("SomeClass::Constructor()\n" |
23073 | "#if CONDITION\n" |
23074 | " : a{a}\n" |
23075 | "#endif\n" |
23076 | " , b{b}\n" |
23077 | " , c{c} {\n}", |
23078 | Style); |
23079 | Style.ConstructorInitializerIndentWidth = 0; |
23080 | verifyFormat("SomeClass::Constructor()\n" |
23081 | ": a{a}\n" |
23082 | "#ifdef CONDITION\n" |
23083 | ", b{b}\n" |
23084 | "#else\n" |
23085 | ", c{c}\n" |
23086 | "#endif\n" |
23087 | ", d{d} {\n}", |
23088 | Style); |
23089 | Style.ConstructorInitializerIndentWidth = 4; |
23090 | verifyFormat("SomeClass::Constructor()\n" |
23091 | " : a{a}\n" |
23092 | "#if WINDOWS\n" |
23093 | "#if DEBUG\n" |
23094 | " , b{0}\n" |
23095 | "#else\n" |
23096 | " , b{1}\n" |
23097 | "#endif\n" |
23098 | "#else\n" |
23099 | "#if DEBUG\n" |
23100 | " , b{2}\n" |
23101 | "#else\n" |
23102 | " , b{3}\n" |
23103 | "#endif\n" |
23104 | "#endif\n" |
23105 | "{\n}", |
23106 | Style); |
23107 | verifyFormat("SomeClass::Constructor()\n" |
23108 | " : a{a}\n" |
23109 | "#if WINDOWS\n" |
23110 | " , b{0}\n" |
23111 | "#if DEBUG\n" |
23112 | " , c{0}\n" |
23113 | "#else\n" |
23114 | " , c{1}\n" |
23115 | "#endif\n" |
23116 | "#else\n" |
23117 | "#if DEBUG\n" |
23118 | " , c{2}\n" |
23119 | "#else\n" |
23120 | " , c{3}\n" |
23121 | "#endif\n" |
23122 | " , b{1}\n" |
23123 | "#endif\n" |
23124 | "{\n}", |
23125 | Style); |
23126 | } |
23127 | |
23128 | TEST_F(FormatTest, Destructors) { |
23129 | verifyFormat("void F(int &i) { i.~int(); }"); |
23130 | verifyFormat("void F(int &i) { i->~int(); }"); |
23131 | } |
23132 | |
23133 | TEST_F(FormatTest, FormatsWithWebKitStyle) { |
23134 | FormatStyle Style = getWebKitStyle(); |
23135 | |
23136 | // Don't indent in outer namespaces. |
23137 | verifyFormat("namespace outer {\n" |
23138 | "int i;\n" |
23139 | "namespace inner {\n" |
23140 | " int i;\n" |
23141 | "} // namespace inner\n" |
23142 | "} // namespace outer\n" |
23143 | "namespace other_outer {\n" |
23144 | "int i;\n" |
23145 | "}", |
23146 | Style); |
23147 | |
23148 | // Don't indent case labels. |
23149 | verifyFormat("switch (variable) {\n" |
23150 | "case 1:\n" |
23151 | "case 2:\n" |
23152 | " doSomething();\n" |
23153 | " break;\n" |
23154 | "default:\n" |
23155 | " ++variable;\n" |
23156 | "}", |
23157 | Style); |
23158 | |
23159 | // Wrap before binary operators. |
23160 | verifyFormat( |
23161 | "void f()\n" |
23162 | "{\n" |
23163 | " if (aaaaaaaaaaaaaaaa\n" |
23164 | " && bbbbbbbbbbbbbbbbbbbbbbbb\n" |
23165 | " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" |
23166 | " return;\n" |
23167 | "}", |
23168 | "void f() {\n" |
23169 | "if (aaaaaaaaaaaaaaaa\n" |
23170 | "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" |
23171 | "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" |
23172 | "return;\n" |
23173 | "}", |
23174 | Style); |
23175 | |
23176 | // Allow functions on a single line. |
23177 | verifyFormat("void f() { return; }", Style); |
23178 | |
23179 | // Allow empty blocks on a single line and insert a space in empty blocks. |
23180 | verifyFormat("void f() { }", "void f() {}", Style); |
23181 | verifyFormat("while (true) { }", "while (true) {}", Style); |
23182 | // However, don't merge non-empty short loops. |
23183 | verifyFormat("while (true) {\n" |
23184 | " continue;\n" |
23185 | "}", |
23186 | "while (true) { continue; }", Style); |
23187 | |
23188 | // Constructor initializers are formatted one per line with the "," on the |
23189 | // new line. |
23190 | verifyFormat("Constructor()\n" |
23191 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
23192 | " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" |
23193 | " aaaaaaaaaaaaaa)\n" |
23194 | " , aaaaaaaaaaaaaaaaaaaaaaa()\n" |
23195 | "{\n" |
23196 | "}", |
23197 | Style); |
23198 | verifyFormat("SomeClass::Constructor()\n" |
23199 | " : a(a)\n" |
23200 | "{\n" |
23201 | "}", |
23202 | Style); |
23203 | verifyFormat("SomeClass::Constructor()\n" |
23204 | " : a(a)\n" |
23205 | "{\n" |
23206 | "}", |
23207 | "SomeClass::Constructor():a(a){}", Style); |
23208 | verifyFormat("SomeClass::Constructor()\n" |
23209 | " : a(a)\n" |
23210 | " , b(b)\n" |
23211 | " , c(c)\n" |
23212 | "{\n" |
23213 | "}", |
23214 | Style); |
23215 | verifyFormat("SomeClass::Constructor()\n" |
23216 | " : a(a)\n" |
23217 | "{\n" |
23218 | " foo();\n" |
23219 | " bar();\n" |
23220 | "}", |
23221 | Style); |
23222 | |
23223 | // Access specifiers should be aligned left. |
23224 | verifyFormat("class C {\n" |
23225 | "public:\n" |
23226 | " int i;\n" |
23227 | "};", |
23228 | Style); |
23229 | |
23230 | // Do not align comments. |
23231 | verifyFormat("int a; // Do not\n" |
23232 | "double b; // align comments.", |
23233 | Style); |
23234 | |
23235 | // Do not align operands. |
23236 | verifyFormat("ASSERT(aaaa\n" |
23237 | " || bbbb);", |
23238 | "ASSERT ( aaaa\n||bbbb);", Style); |
23239 | |
23240 | // Accept input's line breaks. |
23241 | verifyFormat("if (aaaaaaaaaaaaaaa\n" |
23242 | " || bbbbbbbbbbbbbbb) {\n" |
23243 | " i++;\n" |
23244 | "}", |
23245 | "if (aaaaaaaaaaaaaaa\n" |
23246 | "|| bbbbbbbbbbbbbbb) { i++; }", |
23247 | Style); |
23248 | verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" |
23249 | " i++;\n" |
23250 | "}", |
23251 | "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style); |
23252 | |
23253 | // Don't automatically break all macro definitions (llvm.org/PR17842). |
23254 | verifyFormat("#define aNumber 10", Style); |
23255 | // However, generally keep the line breaks that the user authored. |
23256 | verifyFormat("#define aNumber \\\n" |
23257 | " 10", |
23258 | "#define aNumber \\\n" |
23259 | " 10", |
23260 | Style); |
23261 | |
23262 | // Keep empty and one-element array literals on a single line. |
23263 | verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" |
23264 | " copyItems:YES];", |
23265 | "NSArray*a=[[NSArray alloc] initWithArray:@[]\n" |
23266 | "copyItems:YES];", |
23267 | Style); |
23268 | verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" |
23269 | " copyItems:YES];", |
23270 | "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" |
23271 | " copyItems:YES];", |
23272 | Style); |
23273 | // FIXME: This does not seem right, there should be more indentation before |
23274 | // the array literal's entries. Nested blocks have the same problem. |
23275 | verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n" |
23276 | " @\"a\",\n" |
23277 | " @\"a\"\n" |
23278 | "]\n" |
23279 | " copyItems:YES];", |
23280 | "NSArray* a = [[NSArray alloc] initWithArray:@[\n" |
23281 | " @\"a\",\n" |
23282 | " @\"a\"\n" |
23283 | " ]\n" |
23284 | " copyItems:YES];", |
23285 | Style); |
23286 | verifyFormat( |
23287 | "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" |
23288 | " copyItems:YES];", |
23289 | "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" |
23290 | " copyItems:YES];", |
23291 | Style); |
23292 | |
23293 | verifyFormat("[self.a b:c c:d];", Style); |
23294 | verifyFormat("[self.a b:c\n" |
23295 | " c:d];", |
23296 | "[self.a b:c\n" |
23297 | "c:d];", |
23298 | Style); |
23299 | } |
23300 | |
23301 | TEST_F(FormatTest, FormatsLambdas) { |
23302 | verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();"); |
23303 | verifyFormat( |
23304 | "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();"); |
23305 | verifyFormat("int c = [&] { [=] { return b++; }(); }();"); |
23306 | verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();"); |
23307 | verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();"); |
23308 | verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}"); |
23309 | verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}"); |
23310 | verifyFormat("auto c = [a = [b = 42] {}] {};"); |
23311 | verifyFormat("auto c = [a = &i + 10, b = [] {}] {};"); |
23312 | verifyFormat("int x = f(*+[] {});"); |
23313 | verifyFormat("void f() {\n" |
23314 | " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" |
23315 | "}"); |
23316 | verifyFormat("void f() {\n" |
23317 | " other(x.begin(), //\n" |
23318 | " x.end(), //\n" |
23319 | " [&](int, int) { return 1; });\n" |
23320 | "}"); |
23321 | verifyFormat("void f() {\n" |
23322 | " other.other.other.other.other(\n" |
23323 | " x.begin(), x.end(),\n" |
23324 | " [something, rather](int, int, int, int, int, int, int) { " |
23325 | "return 1; });\n" |
23326 | "}"); |
23327 | verifyFormat( |
23328 | "void f() {\n" |
23329 | " other.other.other.other.other(\n" |
23330 | " x.begin(), x.end(),\n" |
23331 | " [something, rather](int, int, int, int, int, int, int) {\n" |
23332 | " //\n" |
23333 | " });\n" |
23334 | "}"); |
23335 | verifyFormat("SomeFunction([]() { // A cool function...\n" |
23336 | " return 43;\n" |
23337 | "});"); |
23338 | verifyFormat("SomeFunction([]() {\n" |
23339 | "#define A a\n" |
23340 | " return 43;\n" |
23341 | "});", |
23342 | "SomeFunction([](){\n" |
23343 | "#define A a\n" |
23344 | "return 43;\n" |
23345 | "});"); |
23346 | verifyFormat("void f() {\n" |
23347 | " SomeFunction([](decltype(x), A *a) {});\n" |
23348 | " SomeFunction([](typeof(x), A *a) {});\n" |
23349 | " SomeFunction([](_Atomic(x), A *a) {});\n" |
23350 | " SomeFunction([](__underlying_type(x), A *a) {});\n" |
23351 | "}"); |
23352 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
23353 | " [](const aaaaaaaaaa &a) { return a; });"); |
23354 | verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" |
23355 | " SomeOtherFunctioooooooooooooooooooooooooon();\n" |
23356 | "});"); |
23357 | verifyFormat("Constructor()\n" |
23358 | " : Field([] { // comment\n" |
23359 | " int i;\n" |
23360 | " }) {}"); |
23361 | verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" |
23362 | " return some_parameter.size();\n" |
23363 | "};"); |
23364 | verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" |
23365 | " [](const string &s) { return s; };"); |
23366 | verifyFormat("int i = aaaaaa ? 1 //\n" |
23367 | " : [] {\n" |
23368 | " return 2; //\n" |
23369 | " }();"); |
23370 | verifyFormat("llvm::errs() << \"number of twos is \"\n" |
23371 | " << std::count_if(v.begin(), v.end(), [](int x) {\n" |
23372 | " return x == 2; // force break\n" |
23373 | " });"); |
23374 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
23375 | " [=](int iiiiiiiiiiii) {\n" |
23376 | " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" |
23377 | " aaaaaaaaaaaaaaaaaaaaaaa;\n" |
23378 | " });", |
23379 | getLLVMStyleWithColumns(60)); |
23380 | |
23381 | verifyFormat("SomeFunction({[&] {\n" |
23382 | " // comment\n" |
23383 | " },\n" |
23384 | " [&] {\n" |
23385 | " // comment\n" |
23386 | " }});"); |
23387 | verifyFormat("SomeFunction({[&] {\n" |
23388 | " // comment\n" |
23389 | "}});"); |
23390 | verifyFormat( |
23391 | "virtual aaaaaaaaaaaaaaaa(\n" |
23392 | " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" |
23393 | " aaaaa aaaaaaaaa);"); |
23394 | |
23395 | // Lambdas with return types. |
23396 | verifyFormat("int c = []() -> int { return 2; }();"); |
23397 | verifyFormat("int c = []() -> int * { return 2; }();"); |
23398 | verifyFormat("int c = []() -> vector<int> { return {2}; }();"); |
23399 | verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); |
23400 | verifyFormat("foo([]() noexcept -> int {});"); |
23401 | verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); |
23402 | verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); |
23403 | verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); |
23404 | verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); |
23405 | verifyFormat("[a, a]() -> a<1> {};"); |
23406 | verifyFormat("[]() -> foo<5 + 2> { return {}; };"); |
23407 | verifyFormat("[]() -> foo<5 - 2> { return {}; };"); |
23408 | verifyFormat("[]() -> foo<5 / 2> { return {}; };"); |
23409 | verifyFormat("[]() -> foo<5 * 2> { return {}; };"); |
23410 | verifyFormat("[]() -> foo<5 % 2> { return {}; };"); |
23411 | verifyFormat("[]() -> foo<5 << 2> { return {}; };"); |
23412 | verifyFormat("[]() -> foo<!5> { return {}; };"); |
23413 | verifyFormat("[]() -> foo<~5> { return {}; };"); |
23414 | verifyFormat("[]() -> foo<5 | 2> { return {}; };"); |
23415 | verifyFormat("[]() -> foo<5 || 2> { return {}; };"); |
23416 | verifyFormat("[]() -> foo<5 & 2> { return {}; };"); |
23417 | verifyFormat("[]() -> foo<5 && 2> { return {}; };"); |
23418 | verifyFormat("[]() -> foo<5 == 2> { return {}; };"); |
23419 | verifyFormat("[]() -> foo<5 != 2> { return {}; };"); |
23420 | verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); |
23421 | verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); |
23422 | verifyFormat("[]() -> foo<5 < 2> { return {}; };"); |
23423 | verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); |
23424 | verifyFormat("namespace bar {\n" |
23425 | "// broken:\n" |
23426 | "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" |
23427 | "} // namespace bar"); |
23428 | verifyFormat("namespace bar {\n" |
23429 | "// broken:\n" |
23430 | "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" |
23431 | "} // namespace bar"); |
23432 | verifyFormat("namespace bar {\n" |
23433 | "// broken:\n" |
23434 | "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" |
23435 | "} // namespace bar"); |
23436 | verifyFormat("namespace bar {\n" |
23437 | "// broken:\n" |
23438 | "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" |
23439 | "} // namespace bar"); |
23440 | verifyFormat("namespace bar {\n" |
23441 | "// broken:\n" |
23442 | "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" |
23443 | "} // namespace bar"); |
23444 | verifyFormat("namespace bar {\n" |
23445 | "// broken:\n" |
23446 | "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" |
23447 | "} // namespace bar"); |
23448 | verifyFormat("namespace bar {\n" |
23449 | "// broken:\n" |
23450 | "auto foo{[]() -> foo<!5> { return {}; }};\n" |
23451 | "} // namespace bar"); |
23452 | verifyFormat("namespace bar {\n" |
23453 | "// broken:\n" |
23454 | "auto foo{[]() -> foo<~5> { return {}; }};\n" |
23455 | "} // namespace bar"); |
23456 | verifyFormat("namespace bar {\n" |
23457 | "// broken:\n" |
23458 | "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" |
23459 | "} // namespace bar"); |
23460 | verifyFormat("namespace bar {\n" |
23461 | "// broken:\n" |
23462 | "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" |
23463 | "} // namespace bar"); |
23464 | verifyFormat("namespace bar {\n" |
23465 | "// broken:\n" |
23466 | "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" |
23467 | "} // namespace bar"); |
23468 | verifyFormat("namespace bar {\n" |
23469 | "// broken:\n" |
23470 | "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" |
23471 | "} // namespace bar"); |
23472 | verifyFormat("namespace bar {\n" |
23473 | "// broken:\n" |
23474 | "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" |
23475 | "} // namespace bar"); |
23476 | verifyFormat("namespace bar {\n" |
23477 | "// broken:\n" |
23478 | "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" |
23479 | "} // namespace bar"); |
23480 | verifyFormat("namespace bar {\n" |
23481 | "// broken:\n" |
23482 | "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" |
23483 | "} // namespace bar"); |
23484 | verifyFormat("namespace bar {\n" |
23485 | "// broken:\n" |
23486 | "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" |
23487 | "} // namespace bar"); |
23488 | verifyFormat("namespace bar {\n" |
23489 | "// broken:\n" |
23490 | "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" |
23491 | "} // namespace bar"); |
23492 | verifyFormat("namespace bar {\n" |
23493 | "// broken:\n" |
23494 | "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" |
23495 | "} // namespace bar"); |
23496 | verifyFormat("[]() -> a<1> {};"); |
23497 | verifyFormat("[]() -> a<1> { ; };"); |
23498 | verifyFormat("[]() -> a<1> { ; }();"); |
23499 | verifyFormat("[a, a]() -> a<true> {};"); |
23500 | verifyFormat("[]() -> a<true> {};"); |
23501 | verifyFormat("[]() -> a<true> { ; };"); |
23502 | verifyFormat("[]() -> a<true> { ; }();"); |
23503 | verifyFormat("[a, a]() -> a<false> {};"); |
23504 | verifyFormat("[]() -> a<false> {};"); |
23505 | verifyFormat("[]() -> a<false> { ; };"); |
23506 | verifyFormat("[]() -> a<false> { ; }();"); |
23507 | verifyFormat("auto foo{[]() -> foo<false> { ; }};"); |
23508 | verifyFormat("namespace bar {\n" |
23509 | "auto foo{[]() -> foo<false> { ; }};\n" |
23510 | "} // namespace bar"); |
23511 | verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" |
23512 | " int j) -> int {\n" |
23513 | " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" |
23514 | "};"); |
23515 | verifyFormat( |
23516 | "aaaaaaaaaaaaaaaaaaaaaa(\n" |
23517 | " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" |
23518 | " return aaaaaaaaaaaaaaaaa;\n" |
23519 | " });", |
23520 | getLLVMStyleWithColumns(70)); |
23521 | verifyFormat("[]() //\n" |
23522 | " -> int {\n" |
23523 | " return 1; //\n" |
23524 | "};"); |
23525 | verifyFormat("[]() -> Void<T...> {};"); |
23526 | verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); |
23527 | verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); |
23528 | verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); |
23529 | verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); |
23530 | verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); |
23531 | verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});"); |
23532 | verifyFormat("return int{[x = x]() { return x; }()};"); |
23533 | |
23534 | // Lambdas with explicit template argument lists. |
23535 | verifyFormat( |
23536 | "auto L = []<template <typename> class T, class U>(T<U> &&a) {};"); |
23537 | verifyFormat("auto L = []<class T>(T) {\n" |
23538 | " {\n" |
23539 | " f();\n" |
23540 | " g();\n" |
23541 | " }\n" |
23542 | "};"); |
23543 | verifyFormat("auto L = []<class... T>(T...) {\n" |
23544 | " {\n" |
23545 | " f();\n" |
23546 | " g();\n" |
23547 | " }\n" |
23548 | "};"); |
23549 | verifyFormat("auto L = []<typename... T>(T...) {\n" |
23550 | " {\n" |
23551 | " f();\n" |
23552 | " g();\n" |
23553 | " }\n" |
23554 | "};"); |
23555 | verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" |
23556 | " {\n" |
23557 | " f();\n" |
23558 | " g();\n" |
23559 | " }\n" |
23560 | "};"); |
23561 | verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" |
23562 | " {\n" |
23563 | " f();\n" |
23564 | " g();\n" |
23565 | " }\n" |
23566 | "};"); |
23567 | verifyFormat("auto L = []<int... T>(T...) {\n" |
23568 | " {\n" |
23569 | " f();\n" |
23570 | " g();\n" |
23571 | " }\n" |
23572 | "};"); |
23573 | verifyFormat("auto L = []<Foo... T>(T...) {\n" |
23574 | " {\n" |
23575 | " f();\n" |
23576 | " g();\n" |
23577 | " }\n" |
23578 | "};"); |
23579 | |
23580 | // Lambdas that fit on a single line within an argument list are not forced |
23581 | // onto new lines. |
23582 | verifyFormat("SomeFunction([] {});"); |
23583 | verifyFormat("SomeFunction(0, [] {});"); |
23584 | verifyFormat("SomeFunction([] {}, 0);"); |
23585 | verifyFormat("SomeFunction(0, [] {}, 0);"); |
23586 | verifyFormat("SomeFunction([] { return 0; }, 0);"); |
23587 | verifyFormat("SomeFunction(a, [] { return 0; }, b);"); |
23588 | verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });"); |
23589 | verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);"); |
23590 | verifyFormat("auto loooooooooooooooooooooooooooong =\n" |
23591 | " SomeFunction([] { return 0; }, [] { return 0; }, b);"); |
23592 | // Exceeded column limit. We need to break. |
23593 | verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n" |
23594 | " [] { return anotherLooooooooooonoooooooongName; }, [] { " |
23595 | "return 0; }, b);"); |
23596 | |
23597 | // Multiple multi-line lambdas in the same parentheses change indentation |
23598 | // rules. These lambdas are always forced to start on new lines. |
23599 | verifyFormat("SomeFunction(\n" |
23600 | " []() {\n" |
23601 | " //\n" |
23602 | " },\n" |
23603 | " []() {\n" |
23604 | " //\n" |
23605 | " });"); |
23606 | |
23607 | // A multi-line lambda passed as arg0 is always pushed to the next line. |
23608 | verifyFormat("SomeFunction(\n" |
23609 | " [this] {\n" |
23610 | " //\n" |
23611 | " },\n" |
23612 | " 1);"); |
23613 | |
23614 | // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like |
23615 | // the arg0 case above. |
23616 | auto Style = getGoogleStyle(); |
23617 | Style.BinPackArguments = false; |
23618 | verifyFormat("SomeFunction(\n" |
23619 | " a,\n" |
23620 | " [this] {\n" |
23621 | " //\n" |
23622 | " },\n" |
23623 | " b);", |
23624 | Style); |
23625 | verifyFormat("SomeFunction(\n" |
23626 | " a,\n" |
23627 | " [this] {\n" |
23628 | " //\n" |
23629 | " },\n" |
23630 | " b);"); |
23631 | |
23632 | // A lambda with a very long line forces arg0 to be pushed out irrespective of |
23633 | // the BinPackArguments value (as long as the code is wide enough). |
23634 | verifyFormat( |
23635 | "something->SomeFunction(\n" |
23636 | " a,\n" |
23637 | " [this] {\n" |
23638 | " " |
23639 | "D0000000000000000000000000000000000000000000000000000000000001();\n" |
23640 | " },\n" |
23641 | " b);"); |
23642 | |
23643 | // A multi-line lambda is pulled up as long as the introducer fits on the |
23644 | // previous line and there are no further args. |
23645 | verifyFormat("function(1, [this, that] {\n" |
23646 | " //\n" |
23647 | "});"); |
23648 | verifyFormat("function([this, that] {\n" |
23649 | " //\n" |
23650 | "});"); |
23651 | // FIXME: this format is not ideal and we should consider forcing the first |
23652 | // arg onto its own line. |
23653 | verifyFormat("function(a, b, c, //\n" |
23654 | " d, [this, that] {\n" |
23655 | " //\n" |
23656 | " });"); |
23657 | |
23658 | // Multiple lambdas are treated correctly even when there is a short arg0. |
23659 | verifyFormat("SomeFunction(\n" |
23660 | " 1,\n" |
23661 | " [this] {\n" |
23662 | " //\n" |
23663 | " },\n" |
23664 | " [this] {\n" |
23665 | " //\n" |
23666 | " },\n" |
23667 | " 1);"); |
23668 | |
23669 | // More complex introducers. |
23670 | verifyFormat("return [i, args...] {};"); |
23671 | |
23672 | // Not lambdas. |
23673 | verifyFormat("constexpr char hello[]{\"hello\"};"); |
23674 | verifyFormat("double &operator[](int i) { return 0; }\n" |
23675 | "int i;"); |
23676 | verifyFormat("std::unique_ptr<int[]> foo() {}"); |
23677 | verifyFormat("int i = a[a][a]->f();"); |
23678 | verifyFormat("int i = (*b)[a]->f();"); |
23679 | |
23680 | // Other corner cases. |
23681 | verifyFormat("void f() {\n" |
23682 | " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" |
23683 | " );\n" |
23684 | "}"); |
23685 | verifyFormat("auto k = *[](int *j) { return j; }(&i);"); |
23686 | |
23687 | // Lambdas created through weird macros. |
23688 | verifyFormat("void f() {\n" |
23689 | " MACRO((const AA &a) { return 1; });\n" |
23690 | " MACRO((AA &a) { return 1; });\n" |
23691 | "}"); |
23692 | |
23693 | verifyFormat("if (blah_blah(whatever, whatever, [] {\n" |
23694 | " doo_dah();\n" |
23695 | " doo_dah();\n" |
23696 | " })) {\n" |
23697 | "}"); |
23698 | verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" |
23699 | " doo_dah();\n" |
23700 | " doo_dah();\n" |
23701 | " })) {\n" |
23702 | "}"); |
23703 | verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" |
23704 | " doo_dah();\n" |
23705 | " doo_dah();\n" |
23706 | " })) {\n" |
23707 | "}"); |
23708 | verifyFormat("auto lambda = []() {\n" |
23709 | " int a = 2\n" |
23710 | "#if A\n" |
23711 | " + 2\n" |
23712 | "#endif\n" |
23713 | " ;\n" |
23714 | "};"); |
23715 | |
23716 | // Lambdas with complex multiline introducers. |
23717 | verifyFormat( |
23718 | "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
23719 | " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" |
23720 | " -> ::std::unordered_set<\n" |
23721 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" |
23722 | " //\n" |
23723 | " });"); |
23724 | |
23725 | FormatStyle LLVMStyle = getLLVMStyleWithColumns(ColumnLimit: 60); |
23726 | verifyFormat("very_long_function_name_yes_it_is_really_long(\n" |
23727 | " [](auto n) noexcept [[back_attr]]\n" |
23728 | " -> std::unordered_map<very_long_type_name_A,\n" |
23729 | " very_long_type_name_B> {\n" |
23730 | " really_do_something();\n" |
23731 | " });", |
23732 | LLVMStyle); |
23733 | verifyFormat("very_long_function_name_yes_it_is_really_long(\n" |
23734 | " [](auto n) constexpr\n" |
23735 | " -> std::unordered_map<very_long_type_name_A,\n" |
23736 | " very_long_type_name_B> {\n" |
23737 | " really_do_something();\n" |
23738 | " });", |
23739 | LLVMStyle); |
23740 | |
23741 | FormatStyle DoNotMerge = getLLVMStyle(); |
23742 | DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; |
23743 | verifyFormat("auto c = []() {\n" |
23744 | " return b;\n" |
23745 | "};", |
23746 | "auto c = []() { return b; };", DoNotMerge); |
23747 | verifyFormat("auto c = []() {\n" |
23748 | "};", |
23749 | " auto c = []() {};", DoNotMerge); |
23750 | |
23751 | FormatStyle MergeEmptyOnly = getLLVMStyle(); |
23752 | MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; |
23753 | verifyFormat("auto c = []() {\n" |
23754 | " return b;\n" |
23755 | "};", |
23756 | "auto c = []() {\n" |
23757 | " return b;\n" |
23758 | " };", |
23759 | MergeEmptyOnly); |
23760 | verifyFormat("auto c = []() {};", |
23761 | "auto c = []() {\n" |
23762 | "};", |
23763 | MergeEmptyOnly); |
23764 | |
23765 | FormatStyle MergeInline = getLLVMStyle(); |
23766 | MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; |
23767 | verifyFormat("auto c = []() {\n" |
23768 | " return b;\n" |
23769 | "};", |
23770 | "auto c = []() { return b; };", MergeInline); |
23771 | verifyFormat("function([]() { return b; })", MergeInline); |
23772 | verifyFormat("function([]() { return b; }, a)", MergeInline); |
23773 | verifyFormat("function(a, []() { return b; })", MergeInline); |
23774 | verifyFormat("auto guard = foo{[&] { exit_status = true; }};", MergeInline); |
23775 | |
23776 | // Check option "BraceWrapping.BeforeLambdaBody" and different state of |
23777 | // AllowShortLambdasOnASingleLine |
23778 | FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); |
23779 | LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; |
23780 | LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; |
23781 | LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = |
23782 | FormatStyle::ShortLambdaStyle::SLS_None; |
23783 | verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" |
23784 | " []()\n" |
23785 | " {\n" |
23786 | " return 17;\n" |
23787 | " });", |
23788 | LLVMWithBeforeLambdaBody); |
23789 | verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" |
23790 | " []()\n" |
23791 | " {\n" |
23792 | " });", |
23793 | LLVMWithBeforeLambdaBody); |
23794 | verifyFormat("auto fct_SLS_None = []()\n" |
23795 | "{\n" |
23796 | " return 17;\n" |
23797 | "};", |
23798 | LLVMWithBeforeLambdaBody); |
23799 | verifyFormat("TwoNestedLambdas_SLS_None(\n" |
23800 | " []()\n" |
23801 | " {\n" |
23802 | " return Call(\n" |
23803 | " []()\n" |
23804 | " {\n" |
23805 | " return 17;\n" |
23806 | " });\n" |
23807 | " });", |
23808 | LLVMWithBeforeLambdaBody); |
23809 | verifyFormat("void Fct() {\n" |
23810 | " return {[]()\n" |
23811 | " {\n" |
23812 | " return 17;\n" |
23813 | " }};\n" |
23814 | "}", |
23815 | LLVMWithBeforeLambdaBody); |
23816 | |
23817 | LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = |
23818 | FormatStyle::ShortLambdaStyle::SLS_Empty; |
23819 | verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" |
23820 | " []()\n" |
23821 | " {\n" |
23822 | " return 17;\n" |
23823 | " });", |
23824 | LLVMWithBeforeLambdaBody); |
23825 | verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", |
23826 | LLVMWithBeforeLambdaBody); |
23827 | verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" |
23828 | "ongFunctionName_SLS_Empty(\n" |
23829 | " []() {});", |
23830 | LLVMWithBeforeLambdaBody); |
23831 | verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" |
23832 | " []()\n" |
23833 | " {\n" |
23834 | " return 17;\n" |
23835 | " });", |
23836 | LLVMWithBeforeLambdaBody); |
23837 | verifyFormat("auto fct_SLS_Empty = []()\n" |
23838 | "{\n" |
23839 | " return 17;\n" |
23840 | "};", |
23841 | LLVMWithBeforeLambdaBody); |
23842 | verifyFormat("TwoNestedLambdas_SLS_Empty(\n" |
23843 | " []()\n" |
23844 | " {\n" |
23845 | " return Call([]() {});\n" |
23846 | " });", |
23847 | LLVMWithBeforeLambdaBody); |
23848 | verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" |
23849 | " []()\n" |
23850 | " {\n" |
23851 | " return Call([]() {});\n" |
23852 | " });", |
23853 | LLVMWithBeforeLambdaBody); |
23854 | verifyFormat( |
23855 | "FctWithLongLineInLambda_SLS_Empty(\n" |
23856 | " []()\n" |
23857 | " {\n" |
23858 | " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" |
23859 | " AndShouldNotBeConsiderAsInline,\n" |
23860 | " LambdaBodyMustBeBreak);\n" |
23861 | " });", |
23862 | LLVMWithBeforeLambdaBody); |
23863 | |
23864 | LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = |
23865 | FormatStyle::ShortLambdaStyle::SLS_Inline; |
23866 | verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", |
23867 | LLVMWithBeforeLambdaBody); |
23868 | verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", |
23869 | LLVMWithBeforeLambdaBody); |
23870 | verifyFormat("auto fct_SLS_Inline = []()\n" |
23871 | "{\n" |
23872 | " return 17;\n" |
23873 | "};", |
23874 | LLVMWithBeforeLambdaBody); |
23875 | verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " |
23876 | "17; }); });", |
23877 | LLVMWithBeforeLambdaBody); |
23878 | verifyFormat( |
23879 | "FctWithLongLineInLambda_SLS_Inline(\n" |
23880 | " []()\n" |
23881 | " {\n" |
23882 | " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" |
23883 | " AndShouldNotBeConsiderAsInline,\n" |
23884 | " LambdaBodyMustBeBreak);\n" |
23885 | " });", |
23886 | LLVMWithBeforeLambdaBody); |
23887 | verifyFormat("FctWithMultipleParams_SLS_Inline(" |
23888 | "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" |
23889 | " []() { return 17; });", |
23890 | LLVMWithBeforeLambdaBody); |
23891 | verifyFormat( |
23892 | "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", |
23893 | LLVMWithBeforeLambdaBody); |
23894 | |
23895 | LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = |
23896 | FormatStyle::ShortLambdaStyle::SLS_All; |
23897 | verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", |
23898 | LLVMWithBeforeLambdaBody); |
23899 | verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", |
23900 | LLVMWithBeforeLambdaBody); |
23901 | verifyFormat("auto fct_SLS_All = []() { return 17; };", |
23902 | LLVMWithBeforeLambdaBody); |
23903 | verifyFormat("FctWithOneParam_SLS_All(\n" |
23904 | " []()\n" |
23905 | " {\n" |
23906 | " // A cool function...\n" |
23907 | " return 43;\n" |
23908 | " });", |
23909 | LLVMWithBeforeLambdaBody); |
23910 | verifyFormat("FctWithMultipleParams_SLS_All(" |
23911 | "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" |
23912 | " []() { return 17; });", |
23913 | LLVMWithBeforeLambdaBody); |
23914 | verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", |
23915 | LLVMWithBeforeLambdaBody); |
23916 | verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", |
23917 | LLVMWithBeforeLambdaBody); |
23918 | verifyFormat( |
23919 | "FctWithLongLineInLambda_SLS_All(\n" |
23920 | " []()\n" |
23921 | " {\n" |
23922 | " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" |
23923 | " AndShouldNotBeConsiderAsInline,\n" |
23924 | " LambdaBodyMustBeBreak);\n" |
23925 | " });", |
23926 | LLVMWithBeforeLambdaBody); |
23927 | verifyFormat( |
23928 | "auto fct_SLS_All = []()\n" |
23929 | "{\n" |
23930 | " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" |
23931 | " AndShouldNotBeConsiderAsInline,\n" |
23932 | " LambdaBodyMustBeBreak);\n" |
23933 | "};", |
23934 | LLVMWithBeforeLambdaBody); |
23935 | LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
23936 | verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", |
23937 | LLVMWithBeforeLambdaBody); |
23938 | verifyFormat( |
23939 | "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" |
23940 | " FirstParam,\n" |
23941 | " SecondParam,\n" |
23942 | " ThirdParam,\n" |
23943 | " FourthParam);", |
23944 | LLVMWithBeforeLambdaBody); |
23945 | verifyFormat("FctWithLongLineInLambda_SLS_All(\n" |
23946 | " []() { return " |
23947 | "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" |
23948 | " FirstParam,\n" |
23949 | " SecondParam,\n" |
23950 | " ThirdParam,\n" |
23951 | " FourthParam);", |
23952 | LLVMWithBeforeLambdaBody); |
23953 | verifyFormat( |
23954 | "FctWithLongLineInLambda_SLS_All(FirstParam,\n" |
23955 | " SecondParam,\n" |
23956 | " ThirdParam,\n" |
23957 | " FourthParam,\n" |
23958 | " []() { return SomeValueNotSoLong; });", |
23959 | LLVMWithBeforeLambdaBody); |
23960 | verifyFormat("FctWithLongLineInLambda_SLS_All(\n" |
23961 | " []()\n" |
23962 | " {\n" |
23963 | " return " |
23964 | "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" |
23965 | "eConsiderAsInline;\n" |
23966 | " });", |
23967 | LLVMWithBeforeLambdaBody); |
23968 | verifyFormat( |
23969 | "FctWithLongLineInLambda_SLS_All(\n" |
23970 | " []()\n" |
23971 | " {\n" |
23972 | " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" |
23973 | " AndShouldNotBeConsiderAsInline,\n" |
23974 | " LambdaBodyMustBeBreak);\n" |
23975 | " });", |
23976 | LLVMWithBeforeLambdaBody); |
23977 | verifyFormat("FctWithTwoParams_SLS_All(\n" |
23978 | " []()\n" |
23979 | " {\n" |
23980 | " // A cool function...\n" |
23981 | " return 43;\n" |
23982 | " },\n" |
23983 | " 87);", |
23984 | LLVMWithBeforeLambdaBody); |
23985 | verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", |
23986 | LLVMWithBeforeLambdaBody); |
23987 | verifyFormat( |
23988 | "FctWithTwoParams_SLS_All(\n" |
23989 | " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });", |
23990 | LLVMWithBeforeLambdaBody); |
23991 | verifyFormat( |
23992 | "FctWithTwoParams_SLS_All(\n" |
23993 | " 87,\n" |
23994 | " []()\n" |
23995 | " {\n" |
23996 | " return " |
23997 | "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n" |
23998 | " });", |
23999 | LLVMWithBeforeLambdaBody); |
24000 | verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", |
24001 | LLVMWithBeforeLambdaBody); |
24002 | verifyFormat( |
24003 | "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", |
24004 | LLVMWithBeforeLambdaBody); |
24005 | verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " |
24006 | "}); }, x);", |
24007 | LLVMWithBeforeLambdaBody); |
24008 | verifyFormat("TwoNestedLambdas_SLS_All(\n" |
24009 | " []()\n" |
24010 | " {\n" |
24011 | " // A cool function...\n" |
24012 | " return Call([]() { return 17; });\n" |
24013 | " });", |
24014 | LLVMWithBeforeLambdaBody); |
24015 | verifyFormat("TwoNestedLambdas_SLS_All(\n" |
24016 | " []()\n" |
24017 | " {\n" |
24018 | " return Call(\n" |
24019 | " []()\n" |
24020 | " {\n" |
24021 | " // A cool function...\n" |
24022 | " return 17;\n" |
24023 | " });\n" |
24024 | " });", |
24025 | LLVMWithBeforeLambdaBody); |
24026 | |
24027 | LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = |
24028 | FormatStyle::ShortLambdaStyle::SLS_None; |
24029 | |
24030 | verifyFormat("auto select = [this]() -> const Library::Object *\n" |
24031 | "{\n" |
24032 | " return MyAssignment::SelectFromList(this);\n" |
24033 | "};", |
24034 | LLVMWithBeforeLambdaBody); |
24035 | |
24036 | verifyFormat("auto select = [this]() -> const Library::Object &\n" |
24037 | "{\n" |
24038 | " return MyAssignment::SelectFromList(this);\n" |
24039 | "};", |
24040 | LLVMWithBeforeLambdaBody); |
24041 | |
24042 | verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" |
24043 | "{\n" |
24044 | " return MyAssignment::SelectFromList(this);\n" |
24045 | "};", |
24046 | LLVMWithBeforeLambdaBody); |
24047 | |
24048 | verifyFormat("namespace test {\n" |
24049 | "class Test {\n" |
24050 | "public:\n" |
24051 | " Test() = default;\n" |
24052 | "};\n" |
24053 | "} // namespace test", |
24054 | LLVMWithBeforeLambdaBody); |
24055 | |
24056 | // Lambdas with different indentation styles. |
24057 | Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
24058 | verifyFormat("Result doSomething(Promise promise) {\n" |
24059 | " return promise.then(\n" |
24060 | " [this, obj = std::move(s)](int bar) mutable {\n" |
24061 | " return someObject.startAsyncAction().then(\n" |
24062 | " [this, &obj](Result result) mutable {\n" |
24063 | " result.processMore();\n" |
24064 | " });\n" |
24065 | " });\n" |
24066 | "}", |
24067 | Style); |
24068 | Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; |
24069 | verifyFormat("Result doSomething(Promise promise) {\n" |
24070 | " return promise.then(\n" |
24071 | " [this, obj = std::move(s)](int bar) mutable {\n" |
24072 | " return obj.startAsyncAction().then(\n" |
24073 | " [this, &obj](Result result) mutable {\n" |
24074 | " result.processMore();\n" |
24075 | " });\n" |
24076 | " });\n" |
24077 | "}", |
24078 | Style); |
24079 | verifyFormat("Result doSomething(Promise promise) {\n" |
24080 | " return promise.then([this, obj = std::move(s)] {\n" |
24081 | " return obj.startAsyncAction().then(\n" |
24082 | " [this, &obj](Result result) mutable {\n" |
24083 | " result.processMore();\n" |
24084 | " });\n" |
24085 | " });\n" |
24086 | "}", |
24087 | Style); |
24088 | verifyFormat("void test() {\n" |
24089 | " ([]() -> auto {\n" |
24090 | " int b = 32;\n" |
24091 | " return 3;\n" |
24092 | " }).foo();\n" |
24093 | "}", |
24094 | Style); |
24095 | verifyFormat("void test() {\n" |
24096 | " []() -> auto {\n" |
24097 | " int b = 32;\n" |
24098 | " return 3;\n" |
24099 | " }\n" |
24100 | "}", |
24101 | Style); |
24102 | verifyFormat("void test() {\n" |
24103 | " std::sort(v.begin(), v.end(),\n" |
24104 | " [](const auto &foo, const auto &bar) {\n" |
24105 | " return foo.baz < bar.baz;\n" |
24106 | " });\n" |
24107 | "};", |
24108 | Style); |
24109 | verifyFormat("void test() {\n" |
24110 | " (\n" |
24111 | " []() -> auto {\n" |
24112 | " int b = 32;\n" |
24113 | " return 3;\n" |
24114 | " }, foo, bar)\n" |
24115 | " .foo();\n" |
24116 | "}", |
24117 | Style); |
24118 | verifyFormat("void test() {\n" |
24119 | " ([]() -> auto {\n" |
24120 | " int b = 32;\n" |
24121 | " return 3;\n" |
24122 | " })\n" |
24123 | " .foo()\n" |
24124 | " .bar();\n" |
24125 | "}", |
24126 | Style); |
24127 | verifyFormat("#define A \\\n" |
24128 | " [] { \\\n" |
24129 | " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" |
24130 | " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" |
24131 | " }", |
24132 | Style); |
24133 | verifyFormat("#define SORT(v) \\\n" |
24134 | " std::sort(v.begin(), v.end(), \\\n" |
24135 | " [](const auto &foo, const auto &bar) { \\\n" |
24136 | " return foo.baz < bar.baz; \\\n" |
24137 | " });", |
24138 | Style); |
24139 | verifyFormat("void foo() {\n" |
24140 | " aFunction(1, b(c(foo, bar, baz, [](d) {\n" |
24141 | " auto f = e(d);\n" |
24142 | " return f;\n" |
24143 | " })));\n" |
24144 | "}", |
24145 | Style); |
24146 | verifyFormat("void foo() {\n" |
24147 | " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n" |
24148 | " auto f = e(foo, [&] {\n" |
24149 | " auto g = h();\n" |
24150 | " return g;\n" |
24151 | " }, qux, [&] -> Bar {\n" |
24152 | " auto i = j();\n" |
24153 | " return i;\n" |
24154 | " });\n" |
24155 | " return f;\n" |
24156 | " })));\n" |
24157 | "}", |
24158 | Style); |
24159 | verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" |
24160 | " AnotherLongClassName baz)\n" |
24161 | " : baz{baz}, func{[&] {\n" |
24162 | " auto qux = bar;\n" |
24163 | " return aFunkyFunctionCall(qux);\n" |
24164 | " }} {}", |
24165 | Style); |
24166 | verifyFormat("void foo() {\n" |
24167 | " class Foo {\n" |
24168 | " public:\n" |
24169 | " Foo()\n" |
24170 | " : qux{[](int quux) {\n" |
24171 | " auto tmp = quux;\n" |
24172 | " return tmp;\n" |
24173 | " }} {}\n" |
24174 | "\n" |
24175 | " private:\n" |
24176 | " std::function<void(int quux)> qux;\n" |
24177 | " };\n" |
24178 | "}", |
24179 | Style); |
24180 | Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; |
24181 | verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" |
24182 | " AnotherLongClassName baz) :\n" |
24183 | " baz{baz}, func{[&] {\n" |
24184 | " auto qux = bar;\n" |
24185 | " return aFunkyFunctionCall(qux);\n" |
24186 | " }} {}", |
24187 | Style); |
24188 | Style.PackConstructorInitializers = FormatStyle::PCIS_Never; |
24189 | verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" |
24190 | " AnotherLongClassName baz) :\n" |
24191 | " baz{baz},\n" |
24192 | " func{[&] {\n" |
24193 | " auto qux = bar;\n" |
24194 | " return aFunkyFunctionCall(qux);\n" |
24195 | " }} {}", |
24196 | Style); |
24197 | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; |
24198 | // FIXME: The following test should pass, but fails at the time of writing. |
24199 | #if 0 |
24200 | // As long as all the non-lambda arguments fit on a single line, AlwaysBreak |
24201 | // doesn't force an initial line break, even if lambdas span multiple lines. |
24202 | verifyFormat("void foo() {\n" |
24203 | " aFunction(\n" |
24204 | " [](d) -> Foo {\n" |
24205 | " auto f = e(d);\n" |
24206 | " return f;\n" |
24207 | " }, foo, Bar{}, [] {\n" |
24208 | " auto g = h();\n" |
24209 | " return g;\n" |
24210 | " }, baz);\n" |
24211 | "}", |
24212 | Style); |
24213 | #endif |
24214 | // A long non-lambda argument forces arguments to span multiple lines and thus |
24215 | // forces an initial line break when using AlwaysBreak. |
24216 | verifyFormat("void foo() {\n" |
24217 | " aFunction(\n" |
24218 | " 1,\n" |
24219 | " [](d) -> Foo {\n" |
24220 | " auto f = e(d);\n" |
24221 | " return f;\n" |
24222 | " }, foo, Bar{},\n" |
24223 | " [] {\n" |
24224 | " auto g = h();\n" |
24225 | " return g;\n" |
24226 | " }, bazzzzz,\n" |
24227 | " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n" |
24228 | "}", |
24229 | Style); |
24230 | Style.BinPackArguments = false; |
24231 | verifyFormat("void foo() {\n" |
24232 | " aFunction(\n" |
24233 | " 1,\n" |
24234 | " [](d) -> Foo {\n" |
24235 | " auto f = e(d);\n" |
24236 | " return f;\n" |
24237 | " },\n" |
24238 | " foo,\n" |
24239 | " Bar{},\n" |
24240 | " [] {\n" |
24241 | " auto g = h();\n" |
24242 | " return g;\n" |
24243 | " },\n" |
24244 | " bazzzzz,\n" |
24245 | " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n" |
24246 | "}", |
24247 | Style); |
24248 | Style.BinPackArguments = true; |
24249 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
24250 | Style.BraceWrapping.BeforeLambdaBody = true; |
24251 | verifyFormat("void foo() {\n" |
24252 | " aFunction(\n" |
24253 | " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n" |
24254 | " {\n" |
24255 | " auto f = e(\n" |
24256 | " [&]\n" |
24257 | " {\n" |
24258 | " auto g = h();\n" |
24259 | " return g;\n" |
24260 | " }, qux, [&] -> Bar\n" |
24261 | " {\n" |
24262 | " auto i = j();\n" |
24263 | " return i;\n" |
24264 | " });\n" |
24265 | " return f;\n" |
24266 | " })));\n" |
24267 | "}", |
24268 | Style); |
24269 | } |
24270 | |
24271 | TEST_F(FormatTest, LambdaWithLineComments) { |
24272 | FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); |
24273 | LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; |
24274 | LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; |
24275 | LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = |
24276 | FormatStyle::ShortLambdaStyle::SLS_All; |
24277 | |
24278 | verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); |
24279 | verifyFormat("auto k = []() // comment\n" |
24280 | "{ return; }", |
24281 | LLVMWithBeforeLambdaBody); |
24282 | verifyFormat("auto k = []() /* comment */ { return; }", |
24283 | LLVMWithBeforeLambdaBody); |
24284 | verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", |
24285 | LLVMWithBeforeLambdaBody); |
24286 | verifyFormat("auto k = []() // X\n" |
24287 | "{ return; }", |
24288 | LLVMWithBeforeLambdaBody); |
24289 | verifyFormat( |
24290 | "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" |
24291 | "{ return; }", |
24292 | LLVMWithBeforeLambdaBody); |
24293 | |
24294 | LLVMWithBeforeLambdaBody.ColumnLimit = 0; |
24295 | |
24296 | verifyFormat("foo([]()\n" |
24297 | " {\n" |
24298 | " bar(); //\n" |
24299 | " return 1; // comment\n" |
24300 | " }());", |
24301 | "foo([]() {\n" |
24302 | " bar(); //\n" |
24303 | " return 1; // comment\n" |
24304 | "}());", |
24305 | LLVMWithBeforeLambdaBody); |
24306 | verifyFormat("foo(\n" |
24307 | " 1, MACRO {\n" |
24308 | " baz();\n" |
24309 | " bar(); // comment\n" |
24310 | " },\n" |
24311 | " []() {});", |
24312 | "foo(\n" |
24313 | " 1, MACRO { baz(); bar(); // comment\n" |
24314 | " }, []() {}\n" |
24315 | ");", |
24316 | LLVMWithBeforeLambdaBody); |
24317 | } |
24318 | |
24319 | TEST_F(FormatTest, EmptyLinesInLambdas) { |
24320 | verifyFormat("auto lambda = []() {\n" |
24321 | " x(); //\n" |
24322 | "};", |
24323 | "auto lambda = []() {\n" |
24324 | "\n" |
24325 | " x(); //\n" |
24326 | "\n" |
24327 | "};"); |
24328 | } |
24329 | |
24330 | TEST_F(FormatTest, LambdaBracesInGNU) { |
24331 | auto Style = getGNUStyle(); |
24332 | EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature); |
24333 | |
24334 | constexpr StringRef Code("auto x = [&] ()\n" |
24335 | " {\n" |
24336 | " for (int i = 0; i < y; ++i)\n" |
24337 | " return 97;\n" |
24338 | " };"); |
24339 | verifyFormat(Code, Style); |
24340 | |
24341 | Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; |
24342 | verifyFormat(Code, Style); |
24343 | verifyFormat("for_each_thread ([] (thread_info *thread)\n" |
24344 | " {\n" |
24345 | " /* Lambda body. */\n" |
24346 | " });", |
24347 | "for_each_thread([](thread_info *thread) {\n" |
24348 | " /* Lambda body. */\n" |
24349 | "});", |
24350 | Style); |
24351 | verifyFormat("iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info)\n" |
24352 | " {\n" |
24353 | " /* Lambda body. */\n" |
24354 | " });", |
24355 | "iterate_over_lwps(scope_ptid, [=](struct lwp_info *info) {\n" |
24356 | " /* Lambda body. */\n" |
24357 | "});", |
24358 | Style); |
24359 | } |
24360 | |
24361 | TEST_F(FormatTest, FormatsBlocks) { |
24362 | FormatStyle ShortBlocks = getLLVMStyle(); |
24363 | ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
24364 | verifyFormat("int (^Block)(int, int);", ShortBlocks); |
24365 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); |
24366 | verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); |
24367 | verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); |
24368 | verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); |
24369 | verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); |
24370 | |
24371 | verifyFormat("foo(^{ bar(); });", ShortBlocks); |
24372 | verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); |
24373 | verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); |
24374 | |
24375 | verifyFormat("[operation setCompletionBlock:^{\n" |
24376 | " [self onOperationDone];\n" |
24377 | "}];"); |
24378 | verifyFormat("int i = {[operation setCompletionBlock:^{\n" |
24379 | " [self onOperationDone];\n" |
24380 | "}]};"); |
24381 | verifyFormat("[operation setCompletionBlock:^(int *i) {\n" |
24382 | " f();\n" |
24383 | "}];"); |
24384 | verifyFormat("int a = [operation block:^int(int *i) {\n" |
24385 | " return 1;\n" |
24386 | "}];"); |
24387 | verifyFormat("[myObject doSomethingWith:arg1\n" |
24388 | " aaa:^int(int *a) {\n" |
24389 | " return 1;\n" |
24390 | " }\n" |
24391 | " bbb:f(a * bbbbbbbb)];"); |
24392 | |
24393 | verifyFormat("[operation setCompletionBlock:^{\n" |
24394 | " [self.delegate newDataAvailable];\n" |
24395 | "}];", |
24396 | getLLVMStyleWithColumns(60)); |
24397 | verifyFormat("dispatch_async(_fileIOQueue, ^{\n" |
24398 | " NSString *path = [self sessionFilePath];\n" |
24399 | " if (path) {\n" |
24400 | " // ...\n" |
24401 | " }\n" |
24402 | "});"); |
24403 | verifyFormat("[[SessionService sharedService]\n" |
24404 | " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" |
24405 | " if (window) {\n" |
24406 | " [self windowDidLoad:window];\n" |
24407 | " } else {\n" |
24408 | " [self errorLoadingWindow];\n" |
24409 | " }\n" |
24410 | " }];"); |
24411 | verifyFormat("void (^largeBlock)(void) = ^{\n" |
24412 | " // ...\n" |
24413 | "};", |
24414 | getLLVMStyleWithColumns(40)); |
24415 | verifyFormat("[[SessionService sharedService]\n" |
24416 | " loadWindowWithCompletionBlock: //\n" |
24417 | " ^(SessionWindow *window) {\n" |
24418 | " if (window) {\n" |
24419 | " [self windowDidLoad:window];\n" |
24420 | " } else {\n" |
24421 | " [self errorLoadingWindow];\n" |
24422 | " }\n" |
24423 | " }];", |
24424 | getLLVMStyleWithColumns(60)); |
24425 | verifyFormat("[myObject doSomethingWith:arg1\n" |
24426 | " firstBlock:^(Foo *a) {\n" |
24427 | " // ...\n" |
24428 | " int i;\n" |
24429 | " }\n" |
24430 | " secondBlock:^(Bar *b) {\n" |
24431 | " // ...\n" |
24432 | " int i;\n" |
24433 | " }\n" |
24434 | " thirdBlock:^Foo(Bar *b) {\n" |
24435 | " // ...\n" |
24436 | " int i;\n" |
24437 | " }];"); |
24438 | verifyFormat("[myObject doSomethingWith:arg1\n" |
24439 | " firstBlock:-1\n" |
24440 | " secondBlock:^(Bar *b) {\n" |
24441 | " // ...\n" |
24442 | " int i;\n" |
24443 | " }];"); |
24444 | |
24445 | verifyFormat("f(^{\n" |
24446 | " @autoreleasepool {\n" |
24447 | " if (a) {\n" |
24448 | " g();\n" |
24449 | " }\n" |
24450 | " }\n" |
24451 | "});"); |
24452 | verifyFormat("Block b = ^int *(A *a, B *b) {\n" |
24453 | "};"); |
24454 | verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" |
24455 | "};"); |
24456 | |
24457 | FormatStyle FourIndent = getLLVMStyle(); |
24458 | FourIndent.ObjCBlockIndentWidth = 4; |
24459 | verifyFormat("[operation setCompletionBlock:^{\n" |
24460 | " [self onOperationDone];\n" |
24461 | "}];", |
24462 | FourIndent); |
24463 | } |
24464 | |
24465 | TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { |
24466 | FormatStyle ZeroColumn = getLLVMStyleWithColumns(ColumnLimit: 0); |
24467 | |
24468 | verifyFormat("[[SessionService sharedService] " |
24469 | "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" |
24470 | " if (window) {\n" |
24471 | " [self windowDidLoad:window];\n" |
24472 | " } else {\n" |
24473 | " [self errorLoadingWindow];\n" |
24474 | " }\n" |
24475 | "}];", |
24476 | ZeroColumn); |
24477 | verifyFormat("[[SessionService sharedService]\n" |
24478 | " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" |
24479 | " if (window) {\n" |
24480 | " [self windowDidLoad:window];\n" |
24481 | " } else {\n" |
24482 | " [self errorLoadingWindow];\n" |
24483 | " }\n" |
24484 | " }];", |
24485 | "[[SessionService sharedService]\n" |
24486 | "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" |
24487 | " if (window) {\n" |
24488 | " [self windowDidLoad:window];\n" |
24489 | " } else {\n" |
24490 | " [self errorLoadingWindow];\n" |
24491 | " }\n" |
24492 | "}];", |
24493 | ZeroColumn); |
24494 | verifyFormat("[myObject doSomethingWith:arg1\n" |
24495 | " firstBlock:^(Foo *a) {\n" |
24496 | " // ...\n" |
24497 | " int i;\n" |
24498 | " }\n" |
24499 | " secondBlock:^(Bar *b) {\n" |
24500 | " // ...\n" |
24501 | " int i;\n" |
24502 | " }\n" |
24503 | " thirdBlock:^Foo(Bar *b) {\n" |
24504 | " // ...\n" |
24505 | " int i;\n" |
24506 | " }];", |
24507 | ZeroColumn); |
24508 | verifyFormat("f(^{\n" |
24509 | " @autoreleasepool {\n" |
24510 | " if (a) {\n" |
24511 | " g();\n" |
24512 | " }\n" |
24513 | " }\n" |
24514 | "});", |
24515 | ZeroColumn); |
24516 | verifyFormat("void (^largeBlock)(void) = ^{\n" |
24517 | " // ...\n" |
24518 | "};", |
24519 | ZeroColumn); |
24520 | |
24521 | ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; |
24522 | verifyFormat("void (^largeBlock)(void) = ^{ int i; };", |
24523 | "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); |
24524 | ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; |
24525 | verifyFormat("void (^largeBlock)(void) = ^{\n" |
24526 | " int i;\n" |
24527 | "};", |
24528 | "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); |
24529 | } |
24530 | |
24531 | TEST_F(FormatTest, SupportsCRLF) { |
24532 | verifyFormat("int a;\r\n" |
24533 | "int b;\r\n" |
24534 | "int c;", |
24535 | "int a;\r\n" |
24536 | " int b;\r\n" |
24537 | " int c;"); |
24538 | verifyFormat("int a;\r\n" |
24539 | "int b;\r\n" |
24540 | "int c;\r\n", |
24541 | "int a;\r\n" |
24542 | " int b;\n" |
24543 | " int c;\r\n"); |
24544 | verifyFormat("int a;\n" |
24545 | "int b;\n" |
24546 | "int c;", |
24547 | "int a;\r\n" |
24548 | " int b;\n" |
24549 | " int c;"); |
24550 | // FIXME: unstable test case |
24551 | EXPECT_EQ("\"aaaaaaa \"\r\n" |
24552 | "\"bbbbbbb\";\r\n", |
24553 | format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); |
24554 | verifyFormat("#define A \\\r\n" |
24555 | " b; \\\r\n" |
24556 | " c; \\\r\n" |
24557 | " d;", |
24558 | "#define A \\\r\n" |
24559 | " b; \\\r\n" |
24560 | " c; d; ", |
24561 | getGoogleStyle()); |
24562 | |
24563 | verifyNoChange("/*\r\n" |
24564 | "multi line block comments\r\n" |
24565 | "should not introduce\r\n" |
24566 | "an extra carriage return\r\n" |
24567 | "*/"); |
24568 | verifyFormat("/*\r\n" |
24569 | "\r\n" |
24570 | "*/", |
24571 | "/*\r\n" |
24572 | " \r\r\r\n" |
24573 | "*/"); |
24574 | |
24575 | FormatStyle style = getLLVMStyle(); |
24576 | |
24577 | EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF); |
24578 | verifyFormat("union FooBarBazQux {\n" |
24579 | " int foo;\n" |
24580 | " int bar;\n" |
24581 | " int baz;\n" |
24582 | "};", |
24583 | "union FooBarBazQux {\r\n" |
24584 | " int foo;\n" |
24585 | " int bar;\r\n" |
24586 | " int baz;\n" |
24587 | "};", |
24588 | style); |
24589 | style.LineEnding = FormatStyle::LE_DeriveCRLF; |
24590 | verifyFormat("union FooBarBazQux {\r\n" |
24591 | " int foo;\r\n" |
24592 | " int bar;\r\n" |
24593 | " int baz;\r\n" |
24594 | "};", |
24595 | "union FooBarBazQux {\r\n" |
24596 | " int foo;\n" |
24597 | " int bar;\r\n" |
24598 | " int baz;\n" |
24599 | "};", |
24600 | style); |
24601 | |
24602 | style.LineEnding = FormatStyle::LE_LF; |
24603 | verifyFormat("union FooBarBazQux {\n" |
24604 | " int foo;\n" |
24605 | " int bar;\n" |
24606 | " int baz;\n" |
24607 | " int qux;\n" |
24608 | "};", |
24609 | "union FooBarBazQux {\r\n" |
24610 | " int foo;\n" |
24611 | " int bar;\r\n" |
24612 | " int baz;\n" |
24613 | " int qux;\r\n" |
24614 | "};", |
24615 | style); |
24616 | style.LineEnding = FormatStyle::LE_CRLF; |
24617 | verifyFormat("union FooBarBazQux {\r\n" |
24618 | " int foo;\r\n" |
24619 | " int bar;\r\n" |
24620 | " int baz;\r\n" |
24621 | " int qux;\r\n" |
24622 | "};", |
24623 | "union FooBarBazQux {\r\n" |
24624 | " int foo;\n" |
24625 | " int bar;\r\n" |
24626 | " int baz;\n" |
24627 | " int qux;\n" |
24628 | "};", |
24629 | style); |
24630 | |
24631 | style.LineEnding = FormatStyle::LE_DeriveLF; |
24632 | verifyFormat("union FooBarBazQux {\r\n" |
24633 | " int foo;\r\n" |
24634 | " int bar;\r\n" |
24635 | " int baz;\r\n" |
24636 | " int qux;\r\n" |
24637 | "};", |
24638 | "union FooBarBazQux {\r\n" |
24639 | " int foo;\n" |
24640 | " int bar;\r\n" |
24641 | " int baz;\n" |
24642 | " int qux;\r\n" |
24643 | "};", |
24644 | style); |
24645 | style.LineEnding = FormatStyle::LE_DeriveCRLF; |
24646 | verifyFormat("union FooBarBazQux {\n" |
24647 | " int foo;\n" |
24648 | " int bar;\n" |
24649 | " int baz;\n" |
24650 | " int qux;\n" |
24651 | "};", |
24652 | "union FooBarBazQux {\r\n" |
24653 | " int foo;\n" |
24654 | " int bar;\r\n" |
24655 | " int baz;\n" |
24656 | " int qux;\n" |
24657 | "};", |
24658 | style); |
24659 | } |
24660 | |
24661 | TEST_F(FormatTest, MunchSemicolonAfterBlocks) { |
24662 | verifyFormat("MY_CLASS(C) {\n" |
24663 | " int i;\n" |
24664 | " int j;\n" |
24665 | "};"); |
24666 | } |
24667 | |
24668 | TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { |
24669 | FormatStyle TwoIndent = getLLVMStyleWithColumns(ColumnLimit: 15); |
24670 | TwoIndent.ContinuationIndentWidth = 2; |
24671 | |
24672 | verifyFormat("int i =\n" |
24673 | " longFunction(\n" |
24674 | " arg);", |
24675 | "int i = longFunction(arg);", TwoIndent); |
24676 | |
24677 | FormatStyle SixIndent = getLLVMStyleWithColumns(ColumnLimit: 20); |
24678 | SixIndent.ContinuationIndentWidth = 6; |
24679 | |
24680 | verifyFormat("int i =\n" |
24681 | " longFunction(\n" |
24682 | " arg);", |
24683 | "int i = longFunction(arg);", SixIndent); |
24684 | } |
24685 | |
24686 | TEST_F(FormatTest, WrappedClosingParenthesisIndent) { |
24687 | FormatStyle Style = getLLVMStyle(); |
24688 | verifyFormat("int Foo::getter(\n" |
24689 | " //\n" |
24690 | ") const {\n" |
24691 | " return foo;\n" |
24692 | "}", |
24693 | Style); |
24694 | verifyFormat("void Foo::setter(\n" |
24695 | " //\n" |
24696 | ") {\n" |
24697 | " foo = 1;\n" |
24698 | "}", |
24699 | Style); |
24700 | } |
24701 | |
24702 | TEST_F(FormatTest, SpacesInAngles) { |
24703 | FormatStyle Spaces = getLLVMStyle(); |
24704 | Spaces.SpacesInAngles = FormatStyle::SIAS_Always; |
24705 | |
24706 | verifyFormat("vector< ::std::string > x1;", Spaces); |
24707 | verifyFormat("Foo< int, Bar > x2;", Spaces); |
24708 | verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); |
24709 | |
24710 | verifyFormat("static_cast< int >(arg);", Spaces); |
24711 | verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); |
24712 | verifyFormat("f< int, float >();", Spaces); |
24713 | verifyFormat("template <> g() {}", Spaces); |
24714 | verifyFormat("template < std::vector< int > > f() {}", Spaces); |
24715 | verifyFormat("std::function< void(int, int) > fct;", Spaces); |
24716 | verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", |
24717 | Spaces); |
24718 | |
24719 | Spaces.Standard = FormatStyle::LS_Cpp03; |
24720 | Spaces.SpacesInAngles = FormatStyle::SIAS_Always; |
24721 | verifyFormat("A< A< int > >();", Spaces); |
24722 | |
24723 | Spaces.SpacesInAngles = FormatStyle::SIAS_Never; |
24724 | verifyFormat("A<A<int> >();", Spaces); |
24725 | |
24726 | Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; |
24727 | verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", |
24728 | Spaces); |
24729 | verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", |
24730 | Spaces); |
24731 | |
24732 | verifyFormat("A<A<int> >();", Spaces); |
24733 | verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); |
24734 | verifyFormat("A< A< int > >();", Spaces); |
24735 | |
24736 | Spaces.Standard = FormatStyle::LS_Cpp11; |
24737 | Spaces.SpacesInAngles = FormatStyle::SIAS_Always; |
24738 | verifyFormat("A< A< int > >();", Spaces); |
24739 | |
24740 | Spaces.SpacesInAngles = FormatStyle::SIAS_Never; |
24741 | verifyFormat("vector<::std::string> x4;", Spaces); |
24742 | verifyFormat("vector<int> x5;", Spaces); |
24743 | verifyFormat("Foo<int, Bar> x6;", Spaces); |
24744 | verifyFormat("Foo<::int, ::Bar> x7;", Spaces); |
24745 | |
24746 | verifyFormat("A<A<int>>();", Spaces); |
24747 | |
24748 | Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; |
24749 | verifyFormat("vector<::std::string> x4;", Spaces); |
24750 | verifyFormat("vector< ::std::string > x4;", Spaces); |
24751 | verifyFormat("vector<int> x5;", Spaces); |
24752 | verifyFormat("vector< int > x5;", Spaces); |
24753 | verifyFormat("Foo<int, Bar> x6;", Spaces); |
24754 | verifyFormat("Foo< int, Bar > x6;", Spaces); |
24755 | verifyFormat("Foo<::int, ::Bar> x7;", Spaces); |
24756 | verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); |
24757 | |
24758 | verifyFormat("A<A<int>>();", Spaces); |
24759 | verifyFormat("A< A< int > >();", Spaces); |
24760 | verifyFormat("A<A<int > >();", Spaces); |
24761 | verifyFormat("A< A< int>>();", Spaces); |
24762 | |
24763 | Spaces.SpacesInAngles = FormatStyle::SIAS_Always; |
24764 | verifyFormat("// clang-format off\n" |
24765 | "foo<<<1, 1>>>();\n" |
24766 | "// clang-format on", |
24767 | Spaces); |
24768 | verifyFormat("// clang-format off\n" |
24769 | "foo< < <1, 1> > >();\n" |
24770 | "// clang-format on", |
24771 | Spaces); |
24772 | } |
24773 | |
24774 | TEST_F(FormatTest, SpaceAfterTemplateKeyword) { |
24775 | FormatStyle Style = getLLVMStyle(); |
24776 | Style.SpaceAfterTemplateKeyword = false; |
24777 | verifyFormat("template<int> void foo();", Style); |
24778 | } |
24779 | |
24780 | TEST_F(FormatTest, TripleAngleBrackets) { |
24781 | verifyFormat("f<<<1, 1>>>();"); |
24782 | verifyFormat("f<<<1, 1, 1, s>>>();"); |
24783 | verifyFormat("f<<<a, b, c, d>>>();"); |
24784 | verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();"); |
24785 | verifyFormat("f<param><<<1, 1>>>();"); |
24786 | verifyFormat("f<1><<<1, 1>>>();"); |
24787 | verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();"); |
24788 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
24789 | "aaaaaaaaaaa<<<\n 1, 1>>>();"); |
24790 | verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" |
24791 | " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); |
24792 | } |
24793 | |
24794 | TEST_F(FormatTest, MergeLessLessAtEnd) { |
24795 | verifyFormat("<<"); |
24796 | verifyFormat("< < <", "\\\n<<<"); |
24797 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
24798 | "aaallvm::outs() <<"); |
24799 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
24800 | "aaaallvm::outs()\n <<"); |
24801 | } |
24802 | |
24803 | TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { |
24804 | std::string code = "#if A\n" |
24805 | "#if B\n" |
24806 | "a.\n" |
24807 | "#endif\n" |
24808 | " a = 1;\n" |
24809 | "#else\n" |
24810 | "#endif\n" |
24811 | "#if C\n" |
24812 | "#else\n" |
24813 | "#endif\n"; |
24814 | verifyFormat(code); |
24815 | } |
24816 | |
24817 | TEST_F(FormatTest, HandleConflictMarkers) { |
24818 | // Git/SVN conflict markers. |
24819 | verifyFormat("int a;\n" |
24820 | "void f() {\n" |
24821 | " callme(some(parameter1,\n" |
24822 | "<<<<<<< text by the vcs\n" |
24823 | " parameter2),\n" |
24824 | "||||||| text by the vcs\n" |
24825 | " parameter2),\n" |
24826 | " parameter3,\n" |
24827 | "======= text by the vcs\n" |
24828 | " parameter2, parameter3),\n" |
24829 | ">>>>>>> text by the vcs\n" |
24830 | " otherparameter);", |
24831 | "int a;\n" |
24832 | "void f() {\n" |
24833 | " callme(some(parameter1,\n" |
24834 | "<<<<<<< text by the vcs\n" |
24835 | " parameter2),\n" |
24836 | "||||||| text by the vcs\n" |
24837 | " parameter2),\n" |
24838 | " parameter3,\n" |
24839 | "======= text by the vcs\n" |
24840 | " parameter2,\n" |
24841 | " parameter3),\n" |
24842 | ">>>>>>> text by the vcs\n" |
24843 | " otherparameter);"); |
24844 | |
24845 | // Perforce markers. |
24846 | verifyFormat("void f() {\n" |
24847 | " function(\n" |
24848 | ">>>> text by the vcs\n" |
24849 | " parameter,\n" |
24850 | "==== text by the vcs\n" |
24851 | " parameter,\n" |
24852 | "==== text by the vcs\n" |
24853 | " parameter,\n" |
24854 | "<<<< text by the vcs\n" |
24855 | " parameter);", |
24856 | "void f() {\n" |
24857 | " function(\n" |
24858 | ">>>> text by the vcs\n" |
24859 | " parameter,\n" |
24860 | "==== text by the vcs\n" |
24861 | " parameter,\n" |
24862 | "==== text by the vcs\n" |
24863 | " parameter,\n" |
24864 | "<<<< text by the vcs\n" |
24865 | " parameter);"); |
24866 | |
24867 | verifyNoChange("<<<<<<<\n" |
24868 | "|||||||\n" |
24869 | "=======\n" |
24870 | ">>>>>>>"); |
24871 | |
24872 | verifyNoChange("<<<<<<<\n" |
24873 | "|||||||\n" |
24874 | "int i;\n" |
24875 | "=======\n" |
24876 | ">>>>>>>"); |
24877 | |
24878 | // FIXME: Handle parsing of macros around conflict markers correctly: |
24879 | verifyFormat("#define Macro \\\n" |
24880 | "<<<<<<<\n" |
24881 | "Something \\\n" |
24882 | "|||||||\n" |
24883 | "Else \\\n" |
24884 | "=======\n" |
24885 | "Other \\\n" |
24886 | ">>>>>>>\n" |
24887 | " End int i;", |
24888 | "#define Macro \\\n" |
24889 | "<<<<<<<\n" |
24890 | " Something \\\n" |
24891 | "|||||||\n" |
24892 | " Else \\\n" |
24893 | "=======\n" |
24894 | " Other \\\n" |
24895 | ">>>>>>>\n" |
24896 | " End\n" |
24897 | "int i;"); |
24898 | |
24899 | verifyFormat(R"(==== |
24900 | #ifdef A |
24901 | a |
24902 | #else |
24903 | b |
24904 | #endif |
24905 | )"); |
24906 | } |
24907 | |
24908 | TEST_F(FormatTest, DisableRegions) { |
24909 | verifyFormat("int i;\n" |
24910 | "// clang-format off\n" |
24911 | " int j;\n" |
24912 | "// clang-format on\n" |
24913 | "int k;", |
24914 | " int i;\n" |
24915 | " // clang-format off\n" |
24916 | " int j;\n" |
24917 | " // clang-format on\n" |
24918 | " int k;"); |
24919 | verifyFormat("int i;\n" |
24920 | "/* clang-format off */\n" |
24921 | " int j;\n" |
24922 | "/* clang-format on */\n" |
24923 | "int k;", |
24924 | " int i;\n" |
24925 | " /* clang-format off */\n" |
24926 | " int j;\n" |
24927 | " /* clang-format on */\n" |
24928 | " int k;"); |
24929 | |
24930 | // Don't reflow comments within disabled regions. |
24931 | verifyFormat("// clang-format off\n" |
24932 | "// long long long long long long line\n" |
24933 | "/* clang-format on */\n" |
24934 | "/* long long long\n" |
24935 | " * long long long\n" |
24936 | " * line */\n" |
24937 | "int i;\n" |
24938 | "/* clang-format off */\n" |
24939 | "/* long long long long long long line */", |
24940 | "// clang-format off\n" |
24941 | "// long long long long long long line\n" |
24942 | "/* clang-format on */\n" |
24943 | "/* long long long long long long line */\n" |
24944 | "int i;\n" |
24945 | "/* clang-format off */\n" |
24946 | "/* long long long long long long line */", |
24947 | getLLVMStyleWithColumns(20)); |
24948 | |
24949 | verifyFormat("int *i;\n" |
24950 | "// clang-format off:\n" |
24951 | "int* j;\n" |
24952 | "// clang-format on: 1\n" |
24953 | "int *k;", |
24954 | "int* i;\n" |
24955 | "// clang-format off:\n" |
24956 | "int* j;\n" |
24957 | "// clang-format on: 1\n" |
24958 | "int* k;"); |
24959 | |
24960 | verifyFormat("int *i;\n" |
24961 | "// clang-format off:0\n" |
24962 | "int* j;\n" |
24963 | "// clang-format only\n" |
24964 | "int* k;", |
24965 | "int* i;\n" |
24966 | "// clang-format off:0\n" |
24967 | "int* j;\n" |
24968 | "// clang-format only\n" |
24969 | "int* k;"); |
24970 | |
24971 | verifyNoChange("// clang-format off\n" |
24972 | "#if 0\n" |
24973 | " #if SHOULD_STAY_INDENTED\n" |
24974 | " #endif\n" |
24975 | "#endif\n" |
24976 | "// clang-format on"); |
24977 | } |
24978 | |
24979 | TEST_F(FormatTest, OneLineFormatOffRegex) { |
24980 | auto Style = getLLVMStyle(); |
24981 | Style.OneLineFormatOffRegex = "// format off$"; |
24982 | |
24983 | verifyFormat(" // format off\n" |
24984 | " int i ;\n" |
24985 | "int j;", |
24986 | " // format off\n" |
24987 | " int i ;\n" |
24988 | " int j ;", |
24989 | Style); |
24990 | verifyFormat("// format off?\n" |
24991 | "int i;", |
24992 | " // format off?\n" |
24993 | " int i ;", |
24994 | Style); |
24995 | verifyFormat("f(\"// format off\");", " f(\"// format off\") ;", Style); |
24996 | |
24997 | verifyFormat("int i;\n" |
24998 | " // format off\n" |
24999 | " int j ;\n" |
25000 | "int k;", |
25001 | " int i ;\n" |
25002 | " // format off\n" |
25003 | " int j ;\n" |
25004 | " int k ;", |
25005 | Style); |
25006 | |
25007 | verifyFormat(" // format off\n" |
25008 | "\n" |
25009 | "int i;", |
25010 | " // format off\n" |
25011 | " \n" |
25012 | " int i ;", |
25013 | Style); |
25014 | |
25015 | verifyFormat("int i;\n" |
25016 | " int j ; // format off\n" |
25017 | "int k;", |
25018 | " int i ;\n" |
25019 | " int j ; // format off\n" |
25020 | " int k ;", |
25021 | Style); |
25022 | |
25023 | verifyFormat("// clang-format off\n" |
25024 | " int i ;\n" |
25025 | " int j ; // format off\n" |
25026 | " int k ;\n" |
25027 | "// clang-format on\n" |
25028 | "f();", |
25029 | " // clang-format off\n" |
25030 | " int i ;\n" |
25031 | " int j ; // format off\n" |
25032 | " int k ;\n" |
25033 | " // clang-format on\n" |
25034 | " f() ;", |
25035 | Style); |
25036 | |
25037 | Style.OneLineFormatOffRegex = "^/\\* format off \\*/"; |
25038 | verifyFormat("int i;\n" |
25039 | " /* format off */ int j ;\n" |
25040 | "int k;", |
25041 | " int i ;\n" |
25042 | " /* format off */ int j ;\n" |
25043 | " int k ;", |
25044 | Style); |
25045 | verifyFormat("f(\"/* format off */\");", " f(\"/* format off */\") ;", Style); |
25046 | |
25047 | Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; |
25048 | verifyFormat("#define A \\\n" |
25049 | " do { \\\n" |
25050 | " /* format off */\\\n" |
25051 | " f() ; \\\n" |
25052 | " g(); \\\n" |
25053 | " } while (0)", |
25054 | "# define A\\\n" |
25055 | " do{ \\\n" |
25056 | " /* format off */\\\n" |
25057 | " f() ; \\\n" |
25058 | " g() ;\\\n" |
25059 | " } while (0 )", |
25060 | Style); |
25061 | |
25062 | Style.ColumnLimit = 50; |
25063 | Style.OneLineFormatOffRegex = "^LogErrorPrint$"; |
25064 | verifyFormat(" myproject::LogErrorPrint(logger, \"Don't split me!\");\n" |
25065 | "myproject::MyLogErrorPrinter(myLogger,\n" |
25066 | " \"Split me!\");", |
25067 | " myproject::LogErrorPrint(logger, \"Don't split me!\");\n" |
25068 | " myproject::MyLogErrorPrinter(myLogger, \"Split me!\");", |
25069 | Style); |
25070 | |
25071 | Style.OneLineFormatOffRegex = "//(< clang-format off| NO_TRANSLATION)$"; |
25072 | verifyNoChange( |
25073 | " int i ; //< clang-format off\n" |
25074 | " msg = sprintf(\"Long string with placeholders.\"); // NO_TRANSLATION", |
25075 | Style); |
25076 | } |
25077 | |
25078 | TEST_F(FormatTest, DoNotCrashOnInvalidInput) { |
25079 | format(Code: "? ) ="); |
25080 | verifyNoCrash(Code: "#define a\\\n /**/}"); |
25081 | verifyNoCrash(Code: " tst %o5 ! are we doing the gray case?\n" |
25082 | "LY52: ! [internal]"); |
25083 | } |
25084 | |
25085 | TEST_F(FormatTest, FormatsTableGenCode) { |
25086 | FormatStyle Style = getLLVMStyle(); |
25087 | Style.Language = FormatStyle::LK_TableGen; |
25088 | verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); |
25089 | } |
25090 | |
25091 | TEST_F(FormatTest, ArrayOfTemplates) { |
25092 | verifyFormat("auto a = new unique_ptr<int>[10];", |
25093 | "auto a = new unique_ptr<int > [ 10];"); |
25094 | |
25095 | FormatStyle Spaces = getLLVMStyle(); |
25096 | Spaces.SpacesInSquareBrackets = true; |
25097 | verifyFormat("auto a = new unique_ptr<int>[ 10 ];", |
25098 | "auto a = new unique_ptr<int > [10];", Spaces); |
25099 | } |
25100 | |
25101 | TEST_F(FormatTest, ArrayAsTemplateType) { |
25102 | verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;", |
25103 | "auto a = unique_ptr < Foo < Bar>[ 10]> ;"); |
25104 | |
25105 | FormatStyle Spaces = getLLVMStyle(); |
25106 | Spaces.SpacesInSquareBrackets = true; |
25107 | verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", |
25108 | "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces); |
25109 | } |
25110 | |
25111 | TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } |
25112 | |
25113 | TEST_F(FormatTest, FormatSortsUsingDeclarations) { |
25114 | verifyFormat("using std::cin;\n" |
25115 | "using std::cout;", |
25116 | "using std::cout;\n" |
25117 | "using std::cin;", |
25118 | getGoogleStyle()); |
25119 | } |
25120 | |
25121 | TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { |
25122 | FormatStyle Style = getLLVMStyle(); |
25123 | Style.Standard = FormatStyle::LS_Cpp03; |
25124 | // cpp03 recognize this string as identifier u8 and literal character 'a' |
25125 | verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style); |
25126 | } |
25127 | |
25128 | TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { |
25129 | // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers |
25130 | // all modes, including C++11, C++14 and C++17 |
25131 | verifyFormat("auto c = u8'a';"); |
25132 | } |
25133 | |
25134 | TEST_F(FormatTest, DoNotFormatLikelyXml) { |
25135 | verifyGoogleFormat("<!-- ;> -->"); |
25136 | verifyNoChange(" <!-- >; -->", getGoogleStyle()); |
25137 | } |
25138 | |
25139 | TEST_F(FormatTest, StructuredBindings) { |
25140 | // Structured bindings is a C++17 feature. |
25141 | // all modes, including C++11, C++14 and C++17 |
25142 | verifyFormat("auto [a, b] = f();"); |
25143 | verifyFormat("auto [a, b] = f();", "auto[a, b] = f();"); |
25144 | verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();"); |
25145 | verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();"); |
25146 | verifyFormat("auto const volatile [a, b] = f();", |
25147 | "auto const volatile[a, b] = f();"); |
25148 | verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();"); |
25149 | verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();"); |
25150 | verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();"); |
25151 | verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();"); |
25152 | verifyFormat("auto const volatile &&[a, b] = f();", |
25153 | "auto const volatile &&[a, b] = f();"); |
25154 | verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();"); |
25155 | verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();"); |
25156 | verifyFormat("const auto volatile &&[a, b] = f();", |
25157 | "const auto volatile &&[a, b] = f();"); |
25158 | verifyFormat("volatile const auto &&[a, b] = f();", |
25159 | "volatile const auto &&[a, b] = f();"); |
25160 | verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();"); |
25161 | |
25162 | // Make sure we don't mistake structured bindings for lambdas. |
25163 | FormatStyle PointerMiddle = getLLVMStyle(); |
25164 | PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; |
25165 | verifyGoogleFormat("auto [a1, b]{A * i};"); |
25166 | verifyFormat("auto [a2, b]{A * i};"); |
25167 | verifyFormat("auto [a3, b]{A * i};", PointerMiddle); |
25168 | verifyGoogleFormat("auto const [a1, b]{A * i};"); |
25169 | verifyFormat("auto const [a2, b]{A * i};"); |
25170 | verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); |
25171 | verifyGoogleFormat("auto const& [a1, b]{A * i};"); |
25172 | verifyFormat("auto const &[a2, b]{A * i};"); |
25173 | verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); |
25174 | verifyGoogleFormat("auto const&& [a1, b]{A * i};"); |
25175 | verifyFormat("auto const &&[a2, b]{A * i};"); |
25176 | verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); |
25177 | |
25178 | verifyFormat("for (const auto &&[a, b] : some_range) {\n}", |
25179 | "for (const auto && [a, b] : some_range) {\n}"); |
25180 | verifyFormat("for (const auto &[a, b] : some_range) {\n}", |
25181 | "for (const auto & [a, b] : some_range) {\n}"); |
25182 | verifyFormat("for (const auto [a, b] : some_range) {\n}", |
25183 | "for (const auto[a, b] : some_range) {\n}"); |
25184 | verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);"); |
25185 | verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);"); |
25186 | verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);"); |
25187 | verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);"); |
25188 | verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);"); |
25189 | verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};"); |
25190 | verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};"); |
25191 | verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};"); |
25192 | |
25193 | FormatStyle Spaces = getLLVMStyle(); |
25194 | Spaces.SpacesInSquareBrackets = true; |
25195 | verifyFormat("auto [ a, b ] = f();", Spaces); |
25196 | verifyFormat("auto &&[ a, b ] = f();", Spaces); |
25197 | verifyFormat("auto &[ a, b ] = f();", Spaces); |
25198 | verifyFormat("auto const &&[ a, b ] = f();", Spaces); |
25199 | verifyFormat("auto const &[ a, b ] = f();", Spaces); |
25200 | } |
25201 | |
25202 | TEST_F(FormatTest, FileAndCode) { |
25203 | EXPECT_EQ(FormatStyle::LK_C, guessLanguage("foo.c", "")); |
25204 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); |
25205 | EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); |
25206 | EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); |
25207 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); |
25208 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25209 | guessLanguage("foo.h", "@interface Foo\n@end")); |
25210 | EXPECT_EQ( |
25211 | FormatStyle::LK_ObjC, |
25212 | guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); |
25213 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25214 | guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); |
25215 | EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); |
25216 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); |
25217 | EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end")); |
25218 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25219 | guessLanguage("foo.h", "int DoStuff(CGRect rect);")); |
25220 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25221 | guessLanguage( |
25222 | "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));")); |
25223 | EXPECT_EQ( |
25224 | FormatStyle::LK_Cpp, |
25225 | guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); |
25226 | // Only one of the two preprocessor regions has ObjC-like code. |
25227 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25228 | guessLanguage("foo.h", "#if A\n" |
25229 | "#define B() C\n" |
25230 | "#else\n" |
25231 | "#define B() [NSString a:@\"\"]\n" |
25232 | "#endif")); |
25233 | } |
25234 | |
25235 | TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { |
25236 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); |
25237 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25238 | guessLanguage("foo.h", "array[[calculator getIndex]];")); |
25239 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25240 | guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); |
25241 | EXPECT_EQ( |
25242 | FormatStyle::LK_Cpp, |
25243 | guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); |
25244 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25245 | guessLanguage("foo.h", "[[noreturn foo] bar];")); |
25246 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25247 | guessLanguage("foo.h", "[[clang::fallthrough]];")); |
25248 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25249 | guessLanguage("foo.h", "[[clang:fallthrough] foo];")); |
25250 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25251 | guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); |
25252 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25253 | guessLanguage("foo.h", "[[using clang: fallthrough]];")); |
25254 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25255 | guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); |
25256 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25257 | guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); |
25258 | EXPECT_EQ( |
25259 | FormatStyle::LK_Cpp, |
25260 | guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); |
25261 | EXPECT_EQ( |
25262 | FormatStyle::LK_Cpp, |
25263 | guessLanguage("foo.h", |
25264 | "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); |
25265 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); |
25266 | } |
25267 | |
25268 | TEST_F(FormatTest, GuessLanguageWithCaret) { |
25269 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); |
25270 | EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); |
25271 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25272 | guessLanguage("foo.h", "int(^)(char, float);")); |
25273 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25274 | guessLanguage("foo.h", "int(^foo)(char, float);")); |
25275 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25276 | guessLanguage("foo.h", "int(^foo[10])(char, float);")); |
25277 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25278 | guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); |
25279 | EXPECT_EQ( |
25280 | FormatStyle::LK_ObjC, |
25281 | guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); |
25282 | } |
25283 | |
25284 | TEST_F(FormatTest, GuessLanguageWithPragmas) { |
25285 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25286 | guessLanguage("foo.h", "__pragma(warning(disable:))")); |
25287 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25288 | guessLanguage("foo.h", "#pragma(warning(disable:))")); |
25289 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25290 | guessLanguage("foo.h", "_Pragma(warning(disable:))")); |
25291 | } |
25292 | |
25293 | TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { |
25294 | // ASM symbolic names are identifiers that must be surrounded by [] without |
25295 | // space in between: |
25296 | // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands |
25297 | |
25298 | // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. |
25299 | verifyFormat(R"(// |
25300 | asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); |
25301 | )"); |
25302 | |
25303 | // A list of several ASM symbolic names. |
25304 | verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); |
25305 | |
25306 | // ASM symbolic names in inline ASM with inputs and outputs. |
25307 | verifyFormat(R"(// |
25308 | asm("cmoveq %1, %2, %[result]" |
25309 | : [result] "=r"(result) |
25310 | : "r"(test), "r"(new), "[result]"(old)); |
25311 | )"); |
25312 | |
25313 | // ASM symbolic names in inline ASM with no outputs. |
25314 | verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); |
25315 | } |
25316 | |
25317 | TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { |
25318 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25319 | guessLanguage("foo.h", "void f() {\n" |
25320 | " asm (\"mov %[e], %[d]\"\n" |
25321 | " : [d] \"=rm\" (d)\n" |
25322 | " [e] \"rm\" (*e));\n" |
25323 | "}")); |
25324 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25325 | guessLanguage("foo.h", "void f() {\n" |
25326 | " _asm (\"mov %[e], %[d]\"\n" |
25327 | " : [d] \"=rm\" (d)\n" |
25328 | " [e] \"rm\" (*e));\n" |
25329 | "}")); |
25330 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25331 | guessLanguage("foo.h", "void f() {\n" |
25332 | " __asm (\"mov %[e], %[d]\"\n" |
25333 | " : [d] \"=rm\" (d)\n" |
25334 | " [e] \"rm\" (*e));\n" |
25335 | "}")); |
25336 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25337 | guessLanguage("foo.h", "void f() {\n" |
25338 | " __asm__ (\"mov %[e], %[d]\"\n" |
25339 | " : [d] \"=rm\" (d)\n" |
25340 | " [e] \"rm\" (*e));\n" |
25341 | "}")); |
25342 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25343 | guessLanguage("foo.h", "void f() {\n" |
25344 | " asm (\"mov %[e], %[d]\"\n" |
25345 | " : [d] \"=rm\" (d),\n" |
25346 | " [e] \"rm\" (*e));\n" |
25347 | "}")); |
25348 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25349 | guessLanguage("foo.h", "void f() {\n" |
25350 | " asm volatile (\"mov %[e], %[d]\"\n" |
25351 | " : [d] \"=rm\" (d)\n" |
25352 | " [e] \"rm\" (*e));\n" |
25353 | "}")); |
25354 | } |
25355 | |
25356 | TEST_F(FormatTest, GuessLanguageWithChildLines) { |
25357 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25358 | guessLanguage("foo.h", "#define FOO ({ std::string s; })")); |
25359 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25360 | guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); |
25361 | EXPECT_EQ( |
25362 | FormatStyle::LK_Cpp, |
25363 | guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); |
25364 | EXPECT_EQ( |
25365 | FormatStyle::LK_ObjC, |
25366 | guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); |
25367 | } |
25368 | |
25369 | TEST_F(FormatTest, GetLanguageByComment) { |
25370 | EXPECT_EQ(FormatStyle::LK_C, |
25371 | guessLanguage("foo.h", "// clang-format Language: C\n" |
25372 | "int i;")); |
25373 | EXPECT_EQ(FormatStyle::LK_Cpp, |
25374 | guessLanguage("foo.h", "// clang-format Language: Cpp\n" |
25375 | "int DoStuff(CGRect rect);")); |
25376 | EXPECT_EQ(FormatStyle::LK_ObjC, |
25377 | guessLanguage("foo.h", "// clang-format Language: ObjC\n" |
25378 | "int i;")); |
25379 | } |
25380 | |
25381 | TEST_F(FormatTest, TypenameMacros) { |
25382 | std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; |
25383 | |
25384 | // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 |
25385 | FormatStyle Google = getGoogleStyleWithColumns(ColumnLimit: 0); |
25386 | Google.TypenameMacros = TypenameMacros; |
25387 | verifyFormat("struct foo {\n" |
25388 | " int bar;\n" |
25389 | " TAILQ_ENTRY(a) bleh;\n" |
25390 | "};", |
25391 | Google); |
25392 | |
25393 | FormatStyle Macros = getLLVMStyle(); |
25394 | Macros.TypenameMacros = TypenameMacros; |
25395 | |
25396 | verifyFormat("STACK_OF(int) a;", Macros); |
25397 | verifyFormat("STACK_OF(int) *a;", Macros); |
25398 | verifyFormat("STACK_OF(int const *) *a;", Macros); |
25399 | verifyFormat("STACK_OF(int *const) *a;", Macros); |
25400 | verifyFormat("STACK_OF(int, string) a;", Macros); |
25401 | verifyFormat("STACK_OF(LIST(int)) a;", Macros); |
25402 | verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); |
25403 | verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); |
25404 | verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); |
25405 | verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); |
25406 | verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); |
25407 | |
25408 | Macros.PointerAlignment = FormatStyle::PAS_Left; |
25409 | verifyFormat("STACK_OF(int)* a;", Macros); |
25410 | verifyFormat("STACK_OF(int*)* a;", Macros); |
25411 | verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); |
25412 | verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); |
25413 | verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); |
25414 | } |
25415 | |
25416 | TEST_F(FormatTest, AtomicQualifier) { |
25417 | // Check that we treate _Atomic as a type and not a function call |
25418 | FormatStyle Google = getGoogleStyleWithColumns(ColumnLimit: 0); |
25419 | verifyFormat("struct foo {\n" |
25420 | " int a1;\n" |
25421 | " _Atomic(a) a2;\n" |
25422 | " _Atomic(_Atomic(int) *const) a3;\n" |
25423 | "};", |
25424 | Google); |
25425 | verifyFormat("_Atomic(uint64_t) a;"); |
25426 | verifyFormat("_Atomic(uint64_t) *a;"); |
25427 | verifyFormat("_Atomic(uint64_t const *) *a;"); |
25428 | verifyFormat("_Atomic(uint64_t *const) *a;"); |
25429 | verifyFormat("_Atomic(const uint64_t *) *a;"); |
25430 | verifyFormat("_Atomic(uint64_t) a;"); |
25431 | verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); |
25432 | verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); |
25433 | verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); |
25434 | verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); |
25435 | |
25436 | verifyFormat("_Atomic(uint64_t) *s(InitValue);"); |
25437 | verifyFormat("_Atomic(uint64_t) *s{InitValue};"); |
25438 | FormatStyle Style = getLLVMStyle(); |
25439 | Style.PointerAlignment = FormatStyle::PAS_Left; |
25440 | verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); |
25441 | verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); |
25442 | verifyFormat("_Atomic(int)* a;", Style); |
25443 | verifyFormat("_Atomic(int*)* a;", Style); |
25444 | verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); |
25445 | |
25446 | Style.SpacesInParens = FormatStyle::SIPO_Custom; |
25447 | Style.SpacesInParensOptions.InCStyleCasts = true; |
25448 | verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); |
25449 | Style.SpacesInParensOptions.InCStyleCasts = false; |
25450 | Style.SpacesInParensOptions.Other = true; |
25451 | verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); |
25452 | verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); |
25453 | } |
25454 | |
25455 | TEST_F(FormatTest, C11Generic) { |
25456 | verifyFormat("_Generic(x, int: 1, default: 0)"); |
25457 | verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)"); |
25458 | verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);"); |
25459 | verifyFormat("_Generic(x, int: f1, const int: f2)();"); |
25460 | verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);"); |
25461 | |
25462 | verifyFormat("_Generic(x,\n" |
25463 | " float: f,\n" |
25464 | " default: d,\n" |
25465 | " long double: ld,\n" |
25466 | " float _Complex: fc,\n" |
25467 | " double _Complex: dc,\n" |
25468 | " long double _Complex: ldc)"); |
25469 | |
25470 | verifyFormat("while (_Generic(x, //\n" |
25471 | " long: x)(x) > x) {\n" |
25472 | "}"); |
25473 | verifyFormat("while (_Generic(x, //\n" |
25474 | " long: x)(x)) {\n" |
25475 | "}"); |
25476 | verifyFormat("x(_Generic(x, //\n" |
25477 | " long: x)(x));"); |
25478 | |
25479 | FormatStyle Style = getLLVMStyle(); |
25480 | Style.ColumnLimit = 40; |
25481 | verifyFormat("#define LIMIT_MAX(T) \\\n" |
25482 | " _Generic(((T)0), \\\n" |
25483 | " unsigned int: UINT_MAX, \\\n" |
25484 | " unsigned long: ULONG_MAX, \\\n" |
25485 | " unsigned long long: ULLONG_MAX)", |
25486 | Style); |
25487 | verifyFormat("_Generic(x,\n" |
25488 | " struct A: 1,\n" |
25489 | " void (*)(void): 2);", |
25490 | Style); |
25491 | |
25492 | Style.ContinuationIndentWidth = 2; |
25493 | verifyFormat("_Generic(x,\n" |
25494 | " struct A: 1,\n" |
25495 | " void (*)(void): 2);", |
25496 | Style); |
25497 | } |
25498 | |
25499 | TEST_F(FormatTest, AmbersandInLamda) { |
25500 | // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 |
25501 | FormatStyle AlignStyle = getLLVMStyle(); |
25502 | AlignStyle.PointerAlignment = FormatStyle::PAS_Left; |
25503 | verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); |
25504 | AlignStyle.PointerAlignment = FormatStyle::PAS_Right; |
25505 | verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); |
25506 | } |
25507 | |
25508 | TEST_F(FormatTest, TrailingReturnTypeAuto) { |
25509 | FormatStyle Style = getLLVMStyle(); |
25510 | verifyFormat("[]() -> auto { return Val; }", Style); |
25511 | verifyFormat("[]() -> auto * { return Val; }", Style); |
25512 | verifyFormat("[]() -> auto & { return Val; }", Style); |
25513 | verifyFormat("auto foo() -> auto { return Val; }", Style); |
25514 | verifyFormat("auto foo() -> auto * { return Val; }", Style); |
25515 | verifyFormat("auto foo() -> auto & { return Val; }", Style); |
25516 | } |
25517 | |
25518 | TEST_F(FormatTest, SpacesInConditionalStatement) { |
25519 | FormatStyle Spaces = getLLVMStyle(); |
25520 | Spaces.IfMacros.clear(); |
25521 | Spaces.IfMacros.push_back(x: "MYIF"); |
25522 | Spaces.SpacesInParens = FormatStyle::SIPO_Custom; |
25523 | Spaces.SpacesInParensOptions.InConditionalStatements = true; |
25524 | verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); |
25525 | verifyFormat("if ( !a )\n return;", Spaces); |
25526 | verifyFormat("if ( a )\n return;", Spaces); |
25527 | verifyFormat("if constexpr ( a )\n return;", Spaces); |
25528 | verifyFormat("MYIF ( a )\n return;", Spaces); |
25529 | verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); |
25530 | verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); |
25531 | verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); |
25532 | verifyFormat("while ( a )\n return;", Spaces); |
25533 | verifyFormat("while ( (a && b) )\n return;", Spaces); |
25534 | verifyFormat("do {\n} while ( 1 != 0 );", Spaces); |
25535 | verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); |
25536 | // Check that space on the left of "::" is inserted as expected at beginning |
25537 | // of condition. |
25538 | verifyFormat("while ( ::func() )\n return;", Spaces); |
25539 | |
25540 | // Check impact of ControlStatementsExceptControlMacros is honored. |
25541 | Spaces.SpaceBeforeParens = |
25542 | FormatStyle::SBPO_ControlStatementsExceptControlMacros; |
25543 | verifyFormat("MYIF( a )\n return;", Spaces); |
25544 | verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); |
25545 | verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); |
25546 | } |
25547 | |
25548 | TEST_F(FormatTest, AlternativeOperators) { |
25549 | // Test case for ensuring alternate operators are not |
25550 | // combined with their right most neighbour. |
25551 | verifyFormat("int a and b;"); |
25552 | verifyFormat("int a and_eq b;"); |
25553 | verifyFormat("int a bitand b;"); |
25554 | verifyFormat("int a bitor b;"); |
25555 | verifyFormat("int a compl b;"); |
25556 | verifyFormat("int a not b;"); |
25557 | verifyFormat("int a not_eq b;"); |
25558 | verifyFormat("int a or b;"); |
25559 | verifyFormat("int a xor b;"); |
25560 | verifyFormat("int a xor_eq b;"); |
25561 | verifyFormat("return this not_eq bitand other;"); |
25562 | verifyFormat("bool operator not_eq(const X bitand other)"); |
25563 | |
25564 | verifyFormat("int a and 5;"); |
25565 | verifyFormat("int a and_eq 5;"); |
25566 | verifyFormat("int a bitand 5;"); |
25567 | verifyFormat("int a bitor 5;"); |
25568 | verifyFormat("int a compl 5;"); |
25569 | verifyFormat("int a not 5;"); |
25570 | verifyFormat("int a not_eq 5;"); |
25571 | verifyFormat("int a or 5;"); |
25572 | verifyFormat("int a xor 5;"); |
25573 | verifyFormat("int a xor_eq 5;"); |
25574 | |
25575 | verifyFormat("int a compl(5);"); |
25576 | verifyFormat("int a not(5);"); |
25577 | |
25578 | verifyFormat("compl foo();"); // ~foo(); |
25579 | verifyFormat("foo() <%%>"); // foo() {} |
25580 | verifyFormat("void foo() <%%>"); // void foo() {} |
25581 | verifyFormat("int a<:1:>;"); // int a[1]; |
25582 | verifyFormat("%:define ABC abc"); // #define ABC abc |
25583 | verifyFormat("%:%:"); // ## |
25584 | |
25585 | verifyFormat("return not ::f();"); |
25586 | verifyFormat("return not *foo;"); |
25587 | |
25588 | verifyFormat("a = v(not;);\n" |
25589 | "c = v(not x);\n" |
25590 | "d = v(not 1);\n" |
25591 | "e = v(not 123.f);"); |
25592 | |
25593 | verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n" |
25594 | " V(and) \\\n" |
25595 | " V(not) \\\n" |
25596 | " V(other)", |
25597 | getLLVMStyleWithColumns(40)); |
25598 | } |
25599 | |
25600 | TEST_F(FormatTest, STLWhileNotDefineChed) { |
25601 | verifyFormat("#if defined(while)\n" |
25602 | "#define while EMIT WARNING C4005\n" |
25603 | "#endif // while"); |
25604 | } |
25605 | |
25606 | TEST_F(FormatTest, OperatorSpacing) { |
25607 | FormatStyle Style = getLLVMStyle(); |
25608 | Style.PointerAlignment = FormatStyle::PAS_Right; |
25609 | verifyFormat("Foo::operator*();", Style); |
25610 | verifyFormat("Foo::operator void *();", Style); |
25611 | verifyFormat("Foo::operator void **();", Style); |
25612 | verifyFormat("Foo::operator void *&();", Style); |
25613 | verifyFormat("Foo::operator void *&&();", Style); |
25614 | verifyFormat("Foo::operator void const *();", Style); |
25615 | verifyFormat("Foo::operator void const **();", Style); |
25616 | verifyFormat("Foo::operator void const *&();", Style); |
25617 | verifyFormat("Foo::operator void const *&&();", Style); |
25618 | verifyFormat("Foo::operator()(void *);", Style); |
25619 | verifyFormat("Foo::operator*(void *);", Style); |
25620 | verifyFormat("Foo::operator*();", Style); |
25621 | verifyFormat("Foo::operator**();", Style); |
25622 | verifyFormat("Foo::operator&();", Style); |
25623 | verifyFormat("Foo::operator<int> *();", Style); |
25624 | verifyFormat("Foo::operator<Foo> *();", Style); |
25625 | verifyFormat("Foo::operator<int> **();", Style); |
25626 | verifyFormat("Foo::operator<Foo> **();", Style); |
25627 | verifyFormat("Foo::operator<int> &();", Style); |
25628 | verifyFormat("Foo::operator<Foo> &();", Style); |
25629 | verifyFormat("Foo::operator<int> &&();", Style); |
25630 | verifyFormat("Foo::operator<Foo> &&();", Style); |
25631 | verifyFormat("Foo::operator<int> *&();", Style); |
25632 | verifyFormat("Foo::operator<Foo> *&();", Style); |
25633 | verifyFormat("Foo::operator<int> *&&();", Style); |
25634 | verifyFormat("Foo::operator<Foo> *&&();", Style); |
25635 | verifyFormat("operator*(int (*)(), class Foo);", Style); |
25636 | |
25637 | verifyFormat("Foo::operator&();", Style); |
25638 | verifyFormat("Foo::operator void &();", Style); |
25639 | verifyFormat("Foo::operator void const &();", Style); |
25640 | verifyFormat("Foo::operator()(void &);", Style); |
25641 | verifyFormat("Foo::operator&(void &);", Style); |
25642 | verifyFormat("Foo::operator&();", Style); |
25643 | verifyFormat("operator&(int (&)(), class Foo);", Style); |
25644 | verifyFormat("operator&&(int (&)(), class Foo);", Style); |
25645 | |
25646 | verifyFormat("Foo::operator&&();", Style); |
25647 | verifyFormat("Foo::operator**();", Style); |
25648 | verifyFormat("Foo::operator void &&();", Style); |
25649 | verifyFormat("Foo::operator void const &&();", Style); |
25650 | verifyFormat("Foo::operator()(void &&);", Style); |
25651 | verifyFormat("Foo::operator&&(void &&);", Style); |
25652 | verifyFormat("Foo::operator&&();", Style); |
25653 | verifyFormat("operator&&(int (&&)(), class Foo);", Style); |
25654 | verifyFormat("operator const nsTArrayRight<E> &()", Style); |
25655 | verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", |
25656 | Style); |
25657 | verifyFormat("operator void **()", Style); |
25658 | verifyFormat("operator const FooRight<Object> &()", Style); |
25659 | verifyFormat("operator const FooRight<Object> *()", Style); |
25660 | verifyFormat("operator const FooRight<Object> **()", Style); |
25661 | verifyFormat("operator const FooRight<Object> *&()", Style); |
25662 | verifyFormat("operator const FooRight<Object> *&&()", Style); |
25663 | |
25664 | Style.PointerAlignment = FormatStyle::PAS_Left; |
25665 | verifyFormat("Foo::operator*();", Style); |
25666 | verifyFormat("Foo::operator**();", Style); |
25667 | verifyFormat("Foo::operator void*();", Style); |
25668 | verifyFormat("Foo::operator void**();", Style); |
25669 | verifyFormat("Foo::operator void*&();", Style); |
25670 | verifyFormat("Foo::operator void*&&();", Style); |
25671 | verifyFormat("Foo::operator void const*();", Style); |
25672 | verifyFormat("Foo::operator void const**();", Style); |
25673 | verifyFormat("Foo::operator void const*&();", Style); |
25674 | verifyFormat("Foo::operator void const*&&();", Style); |
25675 | verifyFormat("Foo::operator/*comment*/ void*();", Style); |
25676 | verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); |
25677 | verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); |
25678 | verifyFormat("Foo::operator()(void*);", Style); |
25679 | verifyFormat("Foo::operator*(void*);", Style); |
25680 | verifyFormat("Foo::operator*();", Style); |
25681 | verifyFormat("Foo::operator<int>*();", Style); |
25682 | verifyFormat("Foo::operator<Foo>*();", Style); |
25683 | verifyFormat("Foo::operator<int>**();", Style); |
25684 | verifyFormat("Foo::operator<Foo>**();", Style); |
25685 | verifyFormat("Foo::operator<Foo>*&();", Style); |
25686 | verifyFormat("Foo::operator<int>&();", Style); |
25687 | verifyFormat("Foo::operator<Foo>&();", Style); |
25688 | verifyFormat("Foo::operator<int>&&();", Style); |
25689 | verifyFormat("Foo::operator<Foo>&&();", Style); |
25690 | verifyFormat("Foo::operator<int>*&();", Style); |
25691 | verifyFormat("Foo::operator<Foo>*&();", Style); |
25692 | verifyFormat("operator*(int (*)(), class Foo);", Style); |
25693 | |
25694 | verifyFormat("Foo::operator&();", Style); |
25695 | verifyFormat("Foo::operator void&();", Style); |
25696 | verifyFormat("Foo::operator void const&();", Style); |
25697 | verifyFormat("Foo::operator/*comment*/ void&();", Style); |
25698 | verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); |
25699 | verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); |
25700 | verifyFormat("Foo::operator()(void&);", Style); |
25701 | verifyFormat("Foo::operator&(void&);", Style); |
25702 | verifyFormat("Foo::operator&();", Style); |
25703 | verifyFormat("operator&(int (&)(), class Foo);", Style); |
25704 | verifyFormat("operator&(int (&&)(), class Foo);", Style); |
25705 | verifyFormat("operator&&(int (&&)(), class Foo);", Style); |
25706 | |
25707 | verifyFormat("Foo::operator&&();", Style); |
25708 | verifyFormat("Foo::operator void&&();", Style); |
25709 | verifyFormat("Foo::operator void const&&();", Style); |
25710 | verifyFormat("Foo::operator/*comment*/ void&&();", Style); |
25711 | verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); |
25712 | verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); |
25713 | verifyFormat("Foo::operator()(void&&);", Style); |
25714 | verifyFormat("Foo::operator&&(void&&);", Style); |
25715 | verifyFormat("Foo::operator&&();", Style); |
25716 | verifyFormat("operator&&(int (&&)(), class Foo);", Style); |
25717 | verifyFormat("operator const nsTArrayLeft<E>&()", Style); |
25718 | verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", |
25719 | Style); |
25720 | verifyFormat("operator void**()", Style); |
25721 | verifyFormat("operator const FooLeft<Object>&()", Style); |
25722 | verifyFormat("operator const FooLeft<Object>*()", Style); |
25723 | verifyFormat("operator const FooLeft<Object>**()", Style); |
25724 | verifyFormat("operator const FooLeft<Object>*&()", Style); |
25725 | verifyFormat("operator const FooLeft<Object>*&&()", Style); |
25726 | |
25727 | // PR45107 |
25728 | verifyFormat("operator Vector<String>&();", Style); |
25729 | verifyFormat("operator const Vector<String>&();", Style); |
25730 | verifyFormat("operator foo::Bar*();", Style); |
25731 | verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); |
25732 | verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", |
25733 | Style); |
25734 | |
25735 | Style.PointerAlignment = FormatStyle::PAS_Middle; |
25736 | verifyFormat("Foo::operator*();", Style); |
25737 | verifyFormat("Foo::operator void *();", Style); |
25738 | verifyFormat("Foo::operator()(void *);", Style); |
25739 | verifyFormat("Foo::operator*(void *);", Style); |
25740 | verifyFormat("Foo::operator*();", Style); |
25741 | verifyFormat("operator*(int (*)(), class Foo);", Style); |
25742 | |
25743 | verifyFormat("Foo::operator&();", Style); |
25744 | verifyFormat("Foo::operator void &();", Style); |
25745 | verifyFormat("Foo::operator void const &();", Style); |
25746 | verifyFormat("Foo::operator()(void &);", Style); |
25747 | verifyFormat("Foo::operator&(void &);", Style); |
25748 | verifyFormat("Foo::operator&();", Style); |
25749 | verifyFormat("operator&(int (&)(), class Foo);", Style); |
25750 | |
25751 | verifyFormat("Foo::operator&&();", Style); |
25752 | verifyFormat("Foo::operator void &&();", Style); |
25753 | verifyFormat("Foo::operator void const &&();", Style); |
25754 | verifyFormat("Foo::operator()(void &&);", Style); |
25755 | verifyFormat("Foo::operator&&(void &&);", Style); |
25756 | verifyFormat("Foo::operator&&();", Style); |
25757 | verifyFormat("operator&&(int (&&)(), class Foo);", Style); |
25758 | } |
25759 | |
25760 | TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { |
25761 | FormatStyle Style = getLLVMStyle(); |
25762 | // PR46157 |
25763 | verifyFormat("foo(operator+, -42);", Style); |
25764 | verifyFormat("foo(operator++, -42);", Style); |
25765 | verifyFormat("foo(operator--, -42);", Style); |
25766 | verifyFormat("foo(-42, operator--);", Style); |
25767 | verifyFormat("foo(-42, operator, );", Style); |
25768 | verifyFormat("foo(operator, , -42);", Style); |
25769 | } |
25770 | |
25771 | TEST_F(FormatTest, WhitespaceSensitiveMacros) { |
25772 | FormatStyle Style = getLLVMStyle(); |
25773 | Style.WhitespaceSensitiveMacros.push_back(x: "FOO"); |
25774 | |
25775 | // Newlines are important here. |
25776 | verifyNoChange("FOO(1+2 )\n", Style); |
25777 | verifyNoChange("FOO(a:b:c)\n", Style); |
25778 | |
25779 | // Don't use the helpers here, since 'mess up' will change the whitespace |
25780 | // and these are all whitespace sensitive by definition |
25781 | verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style); |
25782 | verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style); |
25783 | verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style); |
25784 | verifyNoChange("FOO(String-ized&Messy+But,: :\n" |
25785 | " Still=Intentional);", |
25786 | Style); |
25787 | Style.AlignConsecutiveAssignments.Enabled = true; |
25788 | verifyNoChange("FOO(String-ized=&Messy+But,: :\n" |
25789 | " Still=Intentional);", |
25790 | Style); |
25791 | |
25792 | Style.ColumnLimit = 21; |
25793 | verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style); |
25794 | } |
25795 | |
25796 | TEST_F(FormatTest, SkipMacroDefinitionBody) { |
25797 | auto Style = getLLVMStyle(); |
25798 | Style.SkipMacroDefinitionBody = true; |
25799 | |
25800 | verifyFormat("#define A", "#define A", Style); |
25801 | verifyFormat("#define A a aa", "#define A a aa", Style); |
25802 | verifyNoChange("#define A b", Style); |
25803 | verifyNoChange("#define A ( args )", Style); |
25804 | verifyNoChange("#define A ( args ) = func ( args )", Style); |
25805 | verifyNoChange("#define A ( args ) { int a = 1 ; }", Style); |
25806 | verifyNoChange("#define A ( args ) \\\n" |
25807 | " {\\\n" |
25808 | " int a = 1 ;\\\n" |
25809 | "}", |
25810 | Style); |
25811 | |
25812 | verifyNoChange("#define A x:", Style); |
25813 | verifyNoChange("#define A a. b", Style); |
25814 | |
25815 | // Surrounded with formatted code. |
25816 | verifyFormat("int a;\n" |
25817 | "#define A a\n" |
25818 | "int a;", |
25819 | "int a ;\n" |
25820 | "#define A a\n" |
25821 | "int a ;", |
25822 | Style); |
25823 | |
25824 | // Columns are not broken when a limit is set. |
25825 | Style.ColumnLimit = 10; |
25826 | verifyFormat("#define A a a a a", " # define A a a a a ", Style); |
25827 | verifyNoChange("#define A a a a a", Style); |
25828 | |
25829 | Style.ColumnLimit = 15; |
25830 | verifyFormat("#define A // a\n" |
25831 | " // very\n" |
25832 | " // long\n" |
25833 | " // comment", |
25834 | "#define A //a very long comment", Style); |
25835 | Style.ColumnLimit = 0; |
25836 | |
25837 | // Multiline definition. |
25838 | verifyNoChange("#define A \\\n" |
25839 | "Line one with spaces . \\\n" |
25840 | " Line two.", |
25841 | Style); |
25842 | verifyNoChange("#define A \\\n" |
25843 | "a a \\\n" |
25844 | "a \\\n" |
25845 | "a", |
25846 | Style); |
25847 | Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; |
25848 | verifyNoChange("#define A \\\n" |
25849 | "a a \\\n" |
25850 | "a \\\n" |
25851 | "a", |
25852 | Style); |
25853 | Style.AlignEscapedNewlines = FormatStyle::ENAS_Right; |
25854 | verifyNoChange("#define A \\\n" |
25855 | "a a \\\n" |
25856 | "a \\\n" |
25857 | "a", |
25858 | Style); |
25859 | |
25860 | // Adjust indendations but don't change the definition. |
25861 | Style.IndentPPDirectives = FormatStyle::PPDIS_None; |
25862 | verifyNoChange("#if A\n" |
25863 | "#define A a\n" |
25864 | "#endif", |
25865 | Style); |
25866 | verifyFormat("#if A\n" |
25867 | "#define A a\n" |
25868 | "#endif", |
25869 | "#if A\n" |
25870 | " #define A a\n" |
25871 | "#endif", |
25872 | Style); |
25873 | Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; |
25874 | verifyNoChange("#if A\n" |
25875 | "# define A a\n" |
25876 | "#endif", |
25877 | Style); |
25878 | verifyFormat("#if A\n" |
25879 | "# define A a\n" |
25880 | "#endif", |
25881 | "#if A\n" |
25882 | " #define A a\n" |
25883 | "#endif", |
25884 | Style); |
25885 | Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; |
25886 | verifyNoChange("#if A\n" |
25887 | " #define A a\n" |
25888 | "#endif", |
25889 | Style); |
25890 | verifyFormat("#if A\n" |
25891 | " #define A a\n" |
25892 | "#endif", |
25893 | "#if A\n" |
25894 | " # define A a\n" |
25895 | "#endif", |
25896 | Style); |
25897 | |
25898 | Style.IndentPPDirectives = FormatStyle::PPDIS_None; |
25899 | // SkipMacroDefinitionBody should not affect other PP directives |
25900 | verifyFormat("#if !defined(A)\n" |
25901 | "#define A a\n" |
25902 | "#endif", |
25903 | "#if ! defined ( A )\n" |
25904 | " #define A a\n" |
25905 | "#endif", |
25906 | Style); |
25907 | |
25908 | // With comments. |
25909 | verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a", |
25910 | Style); |
25911 | verifyNoChange("/* */ #define A a // a a", Style); |
25912 | |
25913 | verifyFormat("int a; // a\n" |
25914 | "#define A // a\n" |
25915 | "int aaa; // a", |
25916 | "int a; // a\n" |
25917 | "#define A // a\n" |
25918 | "int aaa; // a", |
25919 | Style); |
25920 | |
25921 | verifyNoChange( |
25922 | "#define MACRO_WITH_COMMENTS() \\\n" |
25923 | " public: \\\n" |
25924 | " /* Documentation parsed by Doxygen for the following method. */ \\\n" |
25925 | " static MyType getClassTypeId(); \\\n" |
25926 | " /** Normal comment for the following method. */ \\\n" |
25927 | " virtual MyType getTypeId() const;", |
25928 | Style); |
25929 | |
25930 | // multiline macro definitions |
25931 | verifyNoChange("#define A a\\\n" |
25932 | " A a \\\n " |
25933 | " A a", |
25934 | Style); |
25935 | } |
25936 | |
25937 | TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { |
25938 | // These tests are not in NamespaceEndCommentsFixerTest because that doesn't |
25939 | // test its interaction with line wrapping |
25940 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80); |
25941 | verifyFormat("namespace {\n" |
25942 | "int i;\n" |
25943 | "int j;\n" |
25944 | "} // namespace", |
25945 | Style); |
25946 | |
25947 | verifyFormat("namespace AAA {\n" |
25948 | "int i;\n" |
25949 | "int j;\n" |
25950 | "} // namespace AAA", |
25951 | Style); |
25952 | |
25953 | verifyFormat("namespace Averyveryveryverylongnamespace {\n" |
25954 | "int i;\n" |
25955 | "int j;\n" |
25956 | "} // namespace Averyveryveryverylongnamespace", |
25957 | "namespace Averyveryveryverylongnamespace {\n" |
25958 | "int i;\n" |
25959 | "int j;\n" |
25960 | "}", |
25961 | Style); |
25962 | |
25963 | verifyFormat( |
25964 | "namespace " |
25965 | "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" |
25966 | " went::mad::now {\n" |
25967 | "int i;\n" |
25968 | "int j;\n" |
25969 | "} // namespace\n" |
25970 | " // " |
25971 | "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" |
25972 | "went::mad::now", |
25973 | "namespace " |
25974 | "would::it::save::you::a::lot::of::time::if_::i::" |
25975 | "just::gave::up::and_::went::mad::now {\n" |
25976 | "int i;\n" |
25977 | "int j;\n" |
25978 | "}", |
25979 | Style); |
25980 | |
25981 | // This used to duplicate the comment again and again on subsequent runs |
25982 | verifyFormat( |
25983 | "namespace " |
25984 | "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" |
25985 | " went::mad::now {\n" |
25986 | "int i;\n" |
25987 | "int j;\n" |
25988 | "} // namespace\n" |
25989 | " // " |
25990 | "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" |
25991 | "went::mad::now", |
25992 | "namespace " |
25993 | "would::it::save::you::a::lot::of::time::if_::i::" |
25994 | "just::gave::up::and_::went::mad::now {\n" |
25995 | "int i;\n" |
25996 | "int j;\n" |
25997 | "} // namespace\n" |
25998 | " // " |
25999 | "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" |
26000 | "and_::went::mad::now", |
26001 | Style); |
26002 | } |
26003 | |
26004 | TEST_F(FormatTest, LikelyUnlikely) { |
26005 | FormatStyle Style = getLLVMStyle(); |
26006 | |
26007 | verifyFormat("if (argc > 5) [[unlikely]] {\n" |
26008 | " return 29;\n" |
26009 | "}", |
26010 | Style); |
26011 | |
26012 | verifyFormat("if (argc > 5) [[likely]] {\n" |
26013 | " return 29;\n" |
26014 | "}", |
26015 | Style); |
26016 | |
26017 | verifyFormat("if (argc > 5) [[unlikely]] {\n" |
26018 | " return 29;\n" |
26019 | "} else [[likely]] {\n" |
26020 | " return 42;\n" |
26021 | "}", |
26022 | Style); |
26023 | |
26024 | verifyFormat("if (argc > 5) [[unlikely]] {\n" |
26025 | " return 29;\n" |
26026 | "} else if (argc > 10) [[likely]] {\n" |
26027 | " return 99;\n" |
26028 | "} else {\n" |
26029 | " return 42;\n" |
26030 | "}", |
26031 | Style); |
26032 | |
26033 | verifyFormat("if (argc > 5) [[gnu::unused]] {\n" |
26034 | " return 29;\n" |
26035 | "}", |
26036 | Style); |
26037 | |
26038 | verifyFormat("if (argc > 5) [[unlikely]]\n" |
26039 | " return 29;", |
26040 | Style); |
26041 | verifyFormat("if (argc > 5) [[likely]]\n" |
26042 | " return 29;", |
26043 | Style); |
26044 | |
26045 | verifyFormat("while (limit > 0) [[unlikely]] {\n" |
26046 | " --limit;\n" |
26047 | "}", |
26048 | Style); |
26049 | verifyFormat("for (auto &limit : limits) [[likely]] {\n" |
26050 | " --limit;\n" |
26051 | "}", |
26052 | Style); |
26053 | |
26054 | verifyFormat("for (auto &limit : limits) [[unlikely]]\n" |
26055 | " --limit;", |
26056 | Style); |
26057 | verifyFormat("while (limit > 0) [[likely]]\n" |
26058 | " --limit;", |
26059 | Style); |
26060 | |
26061 | Style.AttributeMacros.push_back(x: "UNLIKELY"); |
26062 | Style.AttributeMacros.push_back(x: "LIKELY"); |
26063 | verifyFormat("if (argc > 5) UNLIKELY\n" |
26064 | " return 29;", |
26065 | Style); |
26066 | |
26067 | verifyFormat("if (argc > 5) UNLIKELY {\n" |
26068 | " return 29;\n" |
26069 | "}", |
26070 | Style); |
26071 | verifyFormat("if (argc > 5) UNLIKELY {\n" |
26072 | " return 29;\n" |
26073 | "} else [[likely]] {\n" |
26074 | " return 42;\n" |
26075 | "}", |
26076 | Style); |
26077 | verifyFormat("if (argc > 5) UNLIKELY {\n" |
26078 | " return 29;\n" |
26079 | "} else LIKELY {\n" |
26080 | " return 42;\n" |
26081 | "}", |
26082 | Style); |
26083 | verifyFormat("if (argc > 5) [[unlikely]] {\n" |
26084 | " return 29;\n" |
26085 | "} else LIKELY {\n" |
26086 | " return 42;\n" |
26087 | "}", |
26088 | Style); |
26089 | |
26090 | verifyFormat("for (auto &limit : limits) UNLIKELY {\n" |
26091 | " --limit;\n" |
26092 | "}", |
26093 | Style); |
26094 | verifyFormat("while (limit > 0) LIKELY {\n" |
26095 | " --limit;\n" |
26096 | "}", |
26097 | Style); |
26098 | |
26099 | verifyFormat("while (limit > 0) UNLIKELY\n" |
26100 | " --limit;", |
26101 | Style); |
26102 | verifyFormat("for (auto &limit : limits) LIKELY\n" |
26103 | " --limit;", |
26104 | Style); |
26105 | } |
26106 | |
26107 | TEST_F(FormatTest, PenaltyIndentedWhitespace) { |
26108 | verifyFormat("Constructor()\n" |
26109 | " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
26110 | " aaaa(aaaaaaaaaaaaaaaaaa, " |
26111 | "aaaaaaaaaaaaaaaaaat))"); |
26112 | verifyFormat("Constructor()\n" |
26113 | " : aaaaaaaaaaaaa(aaaaaa), " |
26114 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); |
26115 | |
26116 | FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); |
26117 | StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; |
26118 | verifyFormat("Constructor()\n" |
26119 | " : aaaaaa(aaaaaa),\n" |
26120 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
26121 | " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", |
26122 | StyleWithWhitespacePenalty); |
26123 | verifyFormat("Constructor()\n" |
26124 | " : aaaaaaaaaaaaa(aaaaaa), " |
26125 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", |
26126 | StyleWithWhitespacePenalty); |
26127 | } |
26128 | |
26129 | TEST_F(FormatTest, LLVMDefaultStyle) { |
26130 | FormatStyle Style = getLLVMStyle(); |
26131 | verifyFormat("extern \"C\" {\n" |
26132 | "int foo();\n" |
26133 | "}", |
26134 | Style); |
26135 | } |
26136 | TEST_F(FormatTest, GNUDefaultStyle) { |
26137 | FormatStyle Style = getGNUStyle(); |
26138 | verifyFormat("extern \"C\"\n" |
26139 | "{\n" |
26140 | " int foo ();\n" |
26141 | "}", |
26142 | Style); |
26143 | } |
26144 | TEST_F(FormatTest, MozillaDefaultStyle) { |
26145 | FormatStyle Style = getMozillaStyle(); |
26146 | verifyFormat("extern \"C\"\n" |
26147 | "{\n" |
26148 | " int foo();\n" |
26149 | "}", |
26150 | Style); |
26151 | } |
26152 | TEST_F(FormatTest, GoogleDefaultStyle) { |
26153 | FormatStyle Style = getGoogleStyle(); |
26154 | verifyFormat("extern \"C\" {\n" |
26155 | "int foo();\n" |
26156 | "}", |
26157 | Style); |
26158 | } |
26159 | TEST_F(FormatTest, ChromiumDefaultStyle) { |
26160 | FormatStyle Style = getChromiumStyle(Language: FormatStyle::LK_Cpp); |
26161 | verifyFormat("extern \"C\" {\n" |
26162 | "int foo();\n" |
26163 | "}", |
26164 | Style); |
26165 | } |
26166 | TEST_F(FormatTest, MicrosoftDefaultStyle) { |
26167 | FormatStyle Style = getMicrosoftStyle(Language: FormatStyle::LK_Cpp); |
26168 | verifyFormat("extern \"C\"\n" |
26169 | "{\n" |
26170 | " int foo();\n" |
26171 | "}", |
26172 | Style); |
26173 | } |
26174 | TEST_F(FormatTest, WebKitDefaultStyle) { |
26175 | FormatStyle Style = getWebKitStyle(); |
26176 | verifyFormat("extern \"C\" {\n" |
26177 | "int foo();\n" |
26178 | "}", |
26179 | Style); |
26180 | } |
26181 | |
26182 | TEST_F(FormatTest, Concepts) { |
26183 | EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations, |
26184 | FormatStyle::BBCDS_Always); |
26185 | |
26186 | // The default in LLVM style is REI_OuterScope, but these tests were written |
26187 | // when the default was REI_Keyword. |
26188 | FormatStyle Style = getLLVMStyle(); |
26189 | Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; |
26190 | |
26191 | verifyFormat("template <typename T>\n" |
26192 | "concept True = true;"); |
26193 | |
26194 | verifyFormat("template <typename T>\n" |
26195 | "concept C = ((false || foo()) && C2<T>) ||\n" |
26196 | " (std::trait<T>::value && Baz) || sizeof(T) >= 6;", |
26197 | getLLVMStyleWithColumns(60)); |
26198 | |
26199 | verifyFormat("template <typename T>\n" |
26200 | "concept DelayedCheck = true && requires(T t) { t.bar(); } && " |
26201 | "sizeof(T) <= 8;"); |
26202 | |
26203 | verifyFormat("template <typename T>\n" |
26204 | "concept DelayedCheck = true && requires(T t) {\n" |
26205 | " t.bar();\n" |
26206 | " t.baz();\n" |
26207 | " } && sizeof(T) <= 8;", |
26208 | Style); |
26209 | |
26210 | verifyFormat("template <typename T>\n" |
26211 | "concept DelayedCheck = true && requires(T t) { // Comment\n" |
26212 | " t.bar();\n" |
26213 | " t.baz();\n" |
26214 | " } && sizeof(T) <= 8;", |
26215 | Style); |
26216 | |
26217 | verifyFormat("template <typename T>\n" |
26218 | "concept DelayedCheck = false || requires(T t) { t.bar(); } && " |
26219 | "sizeof(T) <= 8;"); |
26220 | |
26221 | verifyFormat("template <typename T>\n" |
26222 | "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;"); |
26223 | |
26224 | verifyFormat("template <typename T>\n" |
26225 | "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);"); |
26226 | |
26227 | verifyFormat("template <typename T>\n" |
26228 | "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;"); |
26229 | |
26230 | verifyFormat("template <typename T>\n" |
26231 | "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " |
26232 | "&& sizeof(T) <= 8;"); |
26233 | |
26234 | verifyFormat("template <typename T>\n" |
26235 | "concept DelayedCheck =\n" |
26236 | " static_cast<bool>(0) || requires(T t) { t.bar(); } && " |
26237 | "sizeof(T) <= 8;"); |
26238 | |
26239 | verifyFormat("template <typename T>\n" |
26240 | "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } " |
26241 | "&& sizeof(T) <= 8;"); |
26242 | |
26243 | verifyFormat( |
26244 | "template <typename T>\n" |
26245 | "concept DelayedCheck =\n" |
26246 | " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;"); |
26247 | |
26248 | verifyFormat("template <typename T>\n" |
26249 | "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } " |
26250 | "&& sizeof(T) <= 8;"); |
26251 | |
26252 | verifyFormat("template <typename T>\n" |
26253 | "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && " |
26254 | "sizeof(T) <= 8;"); |
26255 | |
26256 | verifyFormat("template <typename T>\n" |
26257 | "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n" |
26258 | " requires(T t) {\n" |
26259 | " t.bar();\n" |
26260 | " t.baz();\n" |
26261 | " } && sizeof(T) <= 8 && !(4 < 3);", |
26262 | getLLVMStyleWithColumns(60)); |
26263 | |
26264 | verifyFormat("template <typename T>\n" |
26265 | "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;"); |
26266 | |
26267 | verifyFormat("template <typename T>\n" |
26268 | "concept C = foo();"); |
26269 | |
26270 | verifyFormat("template <typename T>\n" |
26271 | "concept C = foo(T());"); |
26272 | |
26273 | verifyFormat("template <typename T>\n" |
26274 | "concept C = foo(T{});"); |
26275 | |
26276 | verifyFormat("template <typename T>\n" |
26277 | "concept Size = V<sizeof(T)>::Value > 5;"); |
26278 | |
26279 | verifyFormat("template <typename T>\n" |
26280 | "concept True = S<T>::Value;"); |
26281 | |
26282 | verifyFormat("template <S T>\n" |
26283 | "concept True = T.field;"); |
26284 | |
26285 | verifyFormat( |
26286 | "template <typename T>\n" |
26287 | "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n" |
26288 | " sizeof(T) <= 8;"); |
26289 | |
26290 | // FIXME: This is misformatted because the fake l paren starts at bool, not at |
26291 | // the lambda l square. |
26292 | verifyFormat("template <typename T>\n" |
26293 | "concept C = [] -> bool { return true; }() && requires(T t) { " |
26294 | "t.bar(); } &&\n" |
26295 | " sizeof(T) <= 8;"); |
26296 | |
26297 | verifyFormat( |
26298 | "template <typename T>\n" |
26299 | "concept C = decltype([]() { return std::true_type{}; }())::value &&\n" |
26300 | " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); |
26301 | |
26302 | verifyFormat("template <typename T>\n" |
26303 | "concept C = decltype([]() { return std::true_type{}; " |
26304 | "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;", |
26305 | getLLVMStyleWithColumns(120)); |
26306 | |
26307 | verifyFormat("template <typename T>\n" |
26308 | "concept C = decltype([]() -> std::true_type { return {}; " |
26309 | "}())::value &&\n" |
26310 | " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); |
26311 | |
26312 | verifyFormat("template <typename T>\n" |
26313 | "concept C = true;\n" |
26314 | "Foo Bar;"); |
26315 | |
26316 | verifyFormat("template <typename T>\n" |
26317 | "concept Hashable = requires(T a) {\n" |
26318 | " { std::hash<T>{}(a) } -> " |
26319 | "std::convertible_to<std::size_t>;\n" |
26320 | " };", |
26321 | Style); |
26322 | |
26323 | verifyFormat( |
26324 | "template <typename T>\n" |
26325 | "concept EqualityComparable = requires(T a, T b) {\n" |
26326 | " { a == b } -> std::same_as<bool>;\n" |
26327 | " };", |
26328 | Style); |
26329 | |
26330 | verifyFormat( |
26331 | "template <typename T>\n" |
26332 | "concept EqualityComparable = requires(T a, T b) {\n" |
26333 | " { a == b } -> std::same_as<bool>;\n" |
26334 | " { a != b } -> std::same_as<bool>;\n" |
26335 | " };", |
26336 | Style); |
26337 | |
26338 | verifyFormat("template <typename T>\n" |
26339 | "concept WeakEqualityComparable = requires(T a, T b) {\n" |
26340 | " { a == b };\n" |
26341 | " { a != b };\n" |
26342 | " };", |
26343 | Style); |
26344 | |
26345 | verifyFormat("template <typename T>\n" |
26346 | "concept HasSizeT = requires { typename T::size_t; };"); |
26347 | |
26348 | verifyFormat("template <typename T>\n" |
26349 | "concept Semiregular =\n" |
26350 | " DefaultConstructible<T> && CopyConstructible<T> && " |
26351 | "CopyAssignable<T> &&\n" |
26352 | " requires(T a, std::size_t n) {\n" |
26353 | " requires Same<T *, decltype(&a)>;\n" |
26354 | " { a.~T() } noexcept;\n" |
26355 | " requires Same<T *, decltype(new T)>;\n" |
26356 | " requires Same<T *, decltype(new T[n])>;\n" |
26357 | " { delete new T; };\n" |
26358 | " { delete new T[n]; };\n" |
26359 | " };", |
26360 | Style); |
26361 | |
26362 | verifyFormat("template <typename T>\n" |
26363 | "concept Semiregular =\n" |
26364 | " requires(T a, std::size_t n) {\n" |
26365 | " requires Same<T *, decltype(&a)>;\n" |
26366 | " { a.~T() } noexcept;\n" |
26367 | " requires Same<T *, decltype(new T)>;\n" |
26368 | " requires Same<T *, decltype(new T[n])>;\n" |
26369 | " { delete new T; };\n" |
26370 | " { delete new T[n]; };\n" |
26371 | " { new T } -> std::same_as<T *>;\n" |
26372 | " } && DefaultConstructible<T> && CopyConstructible<T> && " |
26373 | "CopyAssignable<T>;", |
26374 | Style); |
26375 | |
26376 | verifyFormat( |
26377 | "template <typename T>\n" |
26378 | "concept Semiregular =\n" |
26379 | " DefaultConstructible<T> && requires(T a, std::size_t n) {\n" |
26380 | " requires Same<T *, decltype(&a)>;\n" |
26381 | " { a.~T() } noexcept;\n" |
26382 | " requires Same<T *, decltype(new T)>;\n" |
26383 | " requires Same<T *, decltype(new " |
26384 | "T[n])>;\n" |
26385 | " { delete new T; };\n" |
26386 | " { delete new T[n]; };\n" |
26387 | " } && CopyConstructible<T> && " |
26388 | "CopyAssignable<T>;", |
26389 | Style); |
26390 | |
26391 | verifyFormat("template <typename T>\n" |
26392 | "concept Two = requires(T t) {\n" |
26393 | " { t.foo() } -> std::same_as<Bar>;\n" |
26394 | " } && requires(T &&t) {\n" |
26395 | " { t.foo() } -> std::same_as<Bar &&>;\n" |
26396 | " };", |
26397 | Style); |
26398 | |
26399 | verifyFormat( |
26400 | "template <typename T>\n" |
26401 | "concept C = requires(T x) {\n" |
26402 | " { *x } -> std::convertible_to<typename T::inner>;\n" |
26403 | " { x + 1 } noexcept -> std::same_as<int>;\n" |
26404 | " { x * 1 } -> std::convertible_to<T>;\n" |
26405 | " };", |
26406 | Style); |
26407 | |
26408 | verifyFormat("template <typename T>\n" |
26409 | "concept C = requires(T x) {\n" |
26410 | " {\n" |
26411 | " long_long_long_function_call(1, 2, 3, 4, 5)\n" |
26412 | " } -> long_long_concept_name<T>;\n" |
26413 | " {\n" |
26414 | " long_long_long_function_call(1, 2, 3, 4, 5)\n" |
26415 | " } noexcept -> long_long_concept_name<T>;\n" |
26416 | " };", |
26417 | Style); |
26418 | |
26419 | verifyFormat( |
26420 | "template <typename T, typename U = T>\n" |
26421 | "concept Swappable = requires(T &&t, U &&u) {\n" |
26422 | " swap(std::forward<T>(t), std::forward<U>(u));\n" |
26423 | " swap(std::forward<U>(u), std::forward<T>(t));\n" |
26424 | " };", |
26425 | Style); |
26426 | |
26427 | verifyFormat("template <typename T, typename U>\n" |
26428 | "concept Common = requires(T &&t, U &&u) {\n" |
26429 | " typename CommonType<T, U>;\n" |
26430 | " { CommonType<T, U>(std::forward<T>(t)) };\n" |
26431 | " };", |
26432 | Style); |
26433 | |
26434 | verifyFormat("template <typename T, typename U>\n" |
26435 | "concept Common = requires(T &&t, U &&u) {\n" |
26436 | " typename CommonType<T, U>;\n" |
26437 | " { CommonType<T, U>{std::forward<T>(t)} };\n" |
26438 | " };", |
26439 | Style); |
26440 | |
26441 | verifyFormat( |
26442 | "template <typename T>\n" |
26443 | "concept C = requires(T t) {\n" |
26444 | " requires Bar<T> && Foo<T>;\n" |
26445 | " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" |
26446 | " };", |
26447 | Style); |
26448 | |
26449 | verifyFormat("template <typename T>\n" |
26450 | "concept HasFoo = requires(T t) {\n" |
26451 | " { t.foo() };\n" |
26452 | " t.foo();\n" |
26453 | " };\n" |
26454 | "template <typename T>\n" |
26455 | "concept HasBar = requires(T t) {\n" |
26456 | " { t.bar() };\n" |
26457 | " t.bar();\n" |
26458 | " };", |
26459 | Style); |
26460 | |
26461 | verifyFormat("template <typename T>\n" |
26462 | "concept Large = sizeof(T) > 10;"); |
26463 | |
26464 | verifyFormat("template <typename T, typename U>\n" |
26465 | "concept FooableWith = requires(T t, U u) {\n" |
26466 | " typename T::foo_type;\n" |
26467 | " { t.foo(u) } -> typename T::foo_type;\n" |
26468 | " t++;\n" |
26469 | " };\n" |
26470 | "void doFoo(FooableWith<int> auto t) { t.foo(3); }", |
26471 | Style); |
26472 | |
26473 | verifyFormat("template <typename T>\n" |
26474 | "concept Context = is_specialization_of_v<context, T>;"); |
26475 | |
26476 | verifyFormat("template <typename T>\n" |
26477 | "concept Node = std::is_object_v<T>;"); |
26478 | |
26479 | verifyFormat("template <class T>\n" |
26480 | "concept integral = __is_integral(T);"); |
26481 | |
26482 | verifyFormat("template <class T>\n" |
26483 | "concept is2D = __array_extent(T, 1) == 2;"); |
26484 | |
26485 | verifyFormat("template <class T>\n" |
26486 | "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)"); |
26487 | |
26488 | verifyFormat("template <class T, class T2>\n" |
26489 | "concept Same = __is_same_as<T, T2>;"); |
26490 | |
26491 | verifyFormat( |
26492 | "template <class _InIt, class _OutIt>\n" |
26493 | "concept _Can_reread_dest =\n" |
26494 | " std::forward_iterator<_OutIt> &&\n" |
26495 | " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;"); |
26496 | |
26497 | Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed; |
26498 | |
26499 | verifyFormat( |
26500 | "template <typename T>\n" |
26501 | "concept C = requires(T t) {\n" |
26502 | " requires Bar<T> && Foo<T>;\n" |
26503 | " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" |
26504 | " };", |
26505 | Style); |
26506 | |
26507 | verifyFormat("template <typename T>\n" |
26508 | "concept HasFoo = requires(T t) {\n" |
26509 | " { t.foo() };\n" |
26510 | " t.foo();\n" |
26511 | " };\n" |
26512 | "template <typename T>\n" |
26513 | "concept HasBar = requires(T t) {\n" |
26514 | " { t.bar() };\n" |
26515 | " t.bar();\n" |
26516 | " };", |
26517 | Style); |
26518 | |
26519 | verifyFormat("template <typename T> concept True = true;", Style); |
26520 | |
26521 | verifyFormat("template <typename T>\n" |
26522 | "concept C = decltype([]() -> std::true_type { return {}; " |
26523 | "}())::value &&\n" |
26524 | " requires(T t) { t.bar(); } && sizeof(T) <= 8;", |
26525 | Style); |
26526 | |
26527 | verifyFormat("template <typename T>\n" |
26528 | "concept Semiregular =\n" |
26529 | " DefaultConstructible<T> && CopyConstructible<T> && " |
26530 | "CopyAssignable<T> &&\n" |
26531 | " requires(T a, std::size_t n) {\n" |
26532 | " requires Same<T *, decltype(&a)>;\n" |
26533 | " { a.~T() } noexcept;\n" |
26534 | " requires Same<T *, decltype(new T)>;\n" |
26535 | " requires Same<T *, decltype(new T[n])>;\n" |
26536 | " { delete new T; };\n" |
26537 | " { delete new T[n]; };\n" |
26538 | " };", |
26539 | Style); |
26540 | |
26541 | Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never; |
26542 | |
26543 | verifyFormat("template <typename T> concept C =\n" |
26544 | " requires(T t) {\n" |
26545 | " requires Bar<T> && Foo<T>;\n" |
26546 | " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" |
26547 | " };", |
26548 | Style); |
26549 | |
26550 | verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n" |
26551 | " { t.foo() };\n" |
26552 | " t.foo();\n" |
26553 | " };\n" |
26554 | "template <typename T> concept HasBar = requires(T t) {\n" |
26555 | " { t.bar() };\n" |
26556 | " t.bar();\n" |
26557 | " };", |
26558 | Style); |
26559 | |
26560 | verifyFormat("template <typename T> concept True = true;", Style); |
26561 | |
26562 | verifyFormat( |
26563 | "template <typename T> concept C =\n" |
26564 | " decltype([]() -> std::true_type { return {}; }())::value &&\n" |
26565 | " requires(T t) { t.bar(); } && sizeof(T) <= 8;", |
26566 | Style); |
26567 | |
26568 | verifyFormat("template <typename T> concept Semiregular =\n" |
26569 | " DefaultConstructible<T> && CopyConstructible<T> && " |
26570 | "CopyAssignable<T> &&\n" |
26571 | " requires(T a, std::size_t n) {\n" |
26572 | " requires Same<T *, decltype(&a)>;\n" |
26573 | " { a.~T() } noexcept;\n" |
26574 | " requires Same<T *, decltype(new T)>;\n" |
26575 | " requires Same<T *, decltype(new T[n])>;\n" |
26576 | " { delete new T; };\n" |
26577 | " { delete new T[n]; };\n" |
26578 | " };", |
26579 | Style); |
26580 | |
26581 | // The following tests are invalid C++, we just want to make sure we don't |
26582 | // assert. |
26583 | verifyNoCrash(Code: "template <typename T>\n" |
26584 | "concept C = requires C2<T>;"); |
26585 | |
26586 | verifyNoCrash(Code: "template <typename T>\n" |
26587 | "concept C = 5 + 4;"); |
26588 | |
26589 | verifyNoCrash(Code: "template <typename T>\n" |
26590 | "concept C = class X;"); |
26591 | |
26592 | verifyNoCrash(Code: "template <typename T>\n" |
26593 | "concept C = [] && true;"); |
26594 | |
26595 | verifyNoCrash(Code: "template <typename T>\n" |
26596 | "concept C = [] && requires(T t) { typename T::size_type; };"); |
26597 | } |
26598 | |
26599 | TEST_F(FormatTest, RequiresClausesPositions) { |
26600 | auto Style = getLLVMStyle(); |
26601 | EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine); |
26602 | EXPECT_EQ(Style.IndentRequiresClause, true); |
26603 | |
26604 | // The default in LLVM style is REI_OuterScope, but these tests were written |
26605 | // when the default was REI_Keyword. |
26606 | Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; |
26607 | |
26608 | verifyFormat("template <typename T>\n" |
26609 | " requires(Foo<T> && std::trait<T>)\n" |
26610 | "struct Bar;", |
26611 | Style); |
26612 | |
26613 | verifyFormat("template <typename T>\n" |
26614 | " requires(Foo<T> && std::trait<T>)\n" |
26615 | "class Bar {\n" |
26616 | "public:\n" |
26617 | " Bar(T t);\n" |
26618 | " bool baz();\n" |
26619 | "};", |
26620 | Style); |
26621 | |
26622 | verifyFormat( |
26623 | "template <typename T>\n" |
26624 | " requires requires(T &&t) {\n" |
26625 | " typename T::I;\n" |
26626 | " requires(F<typename T::I> && std::trait<typename T::I>);\n" |
26627 | " }\n" |
26628 | "Bar(T) -> Bar<typename T::I>;", |
26629 | Style); |
26630 | |
26631 | verifyFormat("template <typename T>\n" |
26632 | " requires(Foo<T> && std::trait<T>)\n" |
26633 | "constexpr T MyGlobal;", |
26634 | Style); |
26635 | |
26636 | verifyFormat("template <typename T>\n" |
26637 | " requires Foo<T> && requires(T t) {\n" |
26638 | " { t.baz() } -> std::same_as<bool>;\n" |
26639 | " requires std::same_as<T::Factor, int>;\n" |
26640 | " }\n" |
26641 | "inline int bar(T t) {\n" |
26642 | " return t.baz() ? T::Factor : 5;\n" |
26643 | "}", |
26644 | Style); |
26645 | |
26646 | verifyFormat("template <typename T>\n" |
26647 | "inline int bar(T t)\n" |
26648 | " requires Foo<T> && requires(T t) {\n" |
26649 | " { t.baz() } -> std::same_as<bool>;\n" |
26650 | " requires std::same_as<T::Factor, int>;\n" |
26651 | " }\n" |
26652 | "{\n" |
26653 | " return t.baz() ? T::Factor : 5;\n" |
26654 | "}", |
26655 | Style); |
26656 | |
26657 | verifyFormat("template <typename T>\n" |
26658 | " requires F<T>\n" |
26659 | "int bar(T t) {\n" |
26660 | " return 5;\n" |
26661 | "}", |
26662 | Style); |
26663 | |
26664 | verifyFormat("template <typename T>\n" |
26665 | "int bar(T t)\n" |
26666 | " requires F<T>\n" |
26667 | "{\n" |
26668 | " return 5;\n" |
26669 | "}", |
26670 | Style); |
26671 | |
26672 | verifyFormat("template <typename T>\n" |
26673 | "int S::bar(T t) &&\n" |
26674 | " requires F<T>\n" |
26675 | "{\n" |
26676 | " return 5;\n" |
26677 | "}", |
26678 | Style); |
26679 | |
26680 | verifyFormat("template <typename T>\n" |
26681 | "int bar(T t)\n" |
26682 | " requires F<T>;", |
26683 | Style); |
26684 | |
26685 | Style.IndentRequiresClause = false; |
26686 | verifyFormat("template <typename T>\n" |
26687 | "requires F<T>\n" |
26688 | "int bar(T t) {\n" |
26689 | " return 5;\n" |
26690 | "}", |
26691 | Style); |
26692 | |
26693 | verifyFormat("template <typename T>\n" |
26694 | "int S::bar(T t) &&\n" |
26695 | "requires F<T>\n" |
26696 | "{\n" |
26697 | " return 5;\n" |
26698 | "}", |
26699 | Style); |
26700 | |
26701 | verifyFormat("template <typename T>\n" |
26702 | "int bar(T t)\n" |
26703 | "requires F<T>\n" |
26704 | "{\n" |
26705 | " return 5;\n" |
26706 | "}", |
26707 | Style); |
26708 | |
26709 | Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace; |
26710 | Style.IndentRequiresClause = true; |
26711 | |
26712 | verifyFormat("template <typename T>\n" |
26713 | " requires(Foo<T> && std::trait<T>)\n" |
26714 | "struct Bar;", |
26715 | Style); |
26716 | |
26717 | verifyFormat("template <typename T>\n" |
26718 | " requires(Foo<T> && std::trait<T>)\n" |
26719 | "class Bar {\n" |
26720 | "public:\n" |
26721 | " Bar(T t);\n" |
26722 | " bool baz();\n" |
26723 | "};", |
26724 | Style); |
26725 | |
26726 | verifyFormat( |
26727 | "template <typename T>\n" |
26728 | " requires requires(T &&t) {\n" |
26729 | " typename T::I;\n" |
26730 | " requires(F<typename T::I> && std::trait<typename T::I>);\n" |
26731 | " }\n" |
26732 | "Bar(T) -> Bar<typename T::I>;", |
26733 | Style); |
26734 | |
26735 | verifyFormat("template <typename T>\n" |
26736 | " requires(Foo<T> && std::trait<T>)\n" |
26737 | "constexpr T MyGlobal;", |
26738 | Style); |
26739 | |
26740 | verifyFormat("template <typename T>\n" |
26741 | " requires Foo<T> && requires(T t) {\n" |
26742 | " { t.baz() } -> std::same_as<bool>;\n" |
26743 | " requires std::same_as<T::Factor, int>;\n" |
26744 | " }\n" |
26745 | "inline int bar(T t) {\n" |
26746 | " return t.baz() ? T::Factor : 5;\n" |
26747 | "}", |
26748 | Style); |
26749 | |
26750 | verifyFormat("template <typename T>\n" |
26751 | "inline int bar(T t)\n" |
26752 | " requires Foo<T> && requires(T t) {\n" |
26753 | " { t.baz() } -> std::same_as<bool>;\n" |
26754 | " requires std::same_as<T::Factor, int>;\n" |
26755 | " } {\n" |
26756 | " return t.baz() ? T::Factor : 5;\n" |
26757 | "}", |
26758 | Style); |
26759 | |
26760 | verifyFormat("template <typename T>\n" |
26761 | " requires F<T>\n" |
26762 | "int bar(T t) {\n" |
26763 | " return 5;\n" |
26764 | "}", |
26765 | Style); |
26766 | |
26767 | verifyFormat("template <typename T>\n" |
26768 | "int bar(T t)\n" |
26769 | " requires F<T> {\n" |
26770 | " return 5;\n" |
26771 | "}", |
26772 | Style); |
26773 | |
26774 | verifyFormat("template <typename T>\n" |
26775 | "int S::bar(T t) &&\n" |
26776 | " requires F<T> {\n" |
26777 | " return 5;\n" |
26778 | "}", |
26779 | Style); |
26780 | |
26781 | verifyFormat("template <typename T>\n" |
26782 | "int bar(T t)\n" |
26783 | " requires F<T>;", |
26784 | Style); |
26785 | |
26786 | verifyFormat("template <typename T>\n" |
26787 | "int bar(T t)\n" |
26788 | " requires F<T> {}", |
26789 | Style); |
26790 | |
26791 | Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine; |
26792 | Style.IndentRequiresClause = false; |
26793 | verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n" |
26794 | "template <typename T> requires Foo<T> void bar() {}\n" |
26795 | "template <typename T> void bar() requires Foo<T> {}\n" |
26796 | "template <typename T> void bar() requires Foo<T>;\n" |
26797 | "template <typename T> void S::bar() && requires Foo<T> {}\n" |
26798 | "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;", |
26799 | Style); |
26800 | |
26801 | auto ColumnStyle = Style; |
26802 | ColumnStyle.ColumnLimit = 40; |
26803 | verifyFormat("template <typename AAAAAAA>\n" |
26804 | "requires Foo<T> struct Bar {};\n" |
26805 | "template <typename AAAAAAA>\n" |
26806 | "requires Foo<T> void bar() {}\n" |
26807 | "template <typename AAAAAAA>\n" |
26808 | "void bar() requires Foo<T> {}\n" |
26809 | "template <typename T>\n" |
26810 | "void S::bar() && requires Foo<T> {}\n" |
26811 | "template <typename AAAAAAA>\n" |
26812 | "requires Foo<T> Baz(T) -> Baz<T>;", |
26813 | ColumnStyle); |
26814 | |
26815 | verifyFormat("template <typename T>\n" |
26816 | "requires Foo<AAAAAAA> struct Bar {};\n" |
26817 | "template <typename T>\n" |
26818 | "requires Foo<AAAAAAA> void bar() {}\n" |
26819 | "template <typename T>\n" |
26820 | "void bar() requires Foo<AAAAAAA> {}\n" |
26821 | "template <typename T>\n" |
26822 | "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;", |
26823 | ColumnStyle); |
26824 | |
26825 | verifyFormat("template <typename AAAAAAA>\n" |
26826 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26827 | "struct Bar {};\n" |
26828 | "template <typename AAAAAAA>\n" |
26829 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26830 | "void bar() {}\n" |
26831 | "template <typename AAAAAAA>\n" |
26832 | "void bar()\n" |
26833 | " requires Foo<AAAAAAAAAAAAAAAA> {}\n" |
26834 | "template <typename AAAAAAA>\n" |
26835 | "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" |
26836 | "template <typename AAAAAAA>\n" |
26837 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26838 | "Bar(T) -> Bar<T>;", |
26839 | ColumnStyle); |
26840 | |
26841 | Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; |
26842 | ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; |
26843 | |
26844 | verifyFormat("template <typename T>\n" |
26845 | "requires Foo<T> struct Bar {};\n" |
26846 | "template <typename T>\n" |
26847 | "requires Foo<T> void bar() {}\n" |
26848 | "template <typename T>\n" |
26849 | "void bar()\n" |
26850 | "requires Foo<T> {}\n" |
26851 | "template <typename T>\n" |
26852 | "void bar()\n" |
26853 | "requires Foo<T>;\n" |
26854 | "template <typename T>\n" |
26855 | "void S::bar() &&\n" |
26856 | "requires Foo<T> {}\n" |
26857 | "template <typename T>\n" |
26858 | "requires Foo<T> Bar(T) -> Bar<T>;", |
26859 | Style); |
26860 | |
26861 | verifyFormat("template <typename AAAAAAA>\n" |
26862 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26863 | "struct Bar {};\n" |
26864 | "template <typename AAAAAAA>\n" |
26865 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26866 | "void bar() {}\n" |
26867 | "template <typename AAAAAAA>\n" |
26868 | "void bar()\n" |
26869 | "requires Foo<AAAAAAAAAAAAAAAA> {}\n" |
26870 | "template <typename AAAAAAA>\n" |
26871 | "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" |
26872 | "template <typename AAAAAAA>\n" |
26873 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26874 | "Bar(T) -> Bar<T>;", |
26875 | ColumnStyle); |
26876 | |
26877 | Style.IndentRequiresClause = true; |
26878 | ColumnStyle.IndentRequiresClause = true; |
26879 | |
26880 | verifyFormat("template <typename T>\n" |
26881 | " requires Foo<T> struct Bar {};\n" |
26882 | "template <typename T>\n" |
26883 | " requires Foo<T> void bar() {}\n" |
26884 | "template <typename T>\n" |
26885 | "void bar()\n" |
26886 | " requires Foo<T> {}\n" |
26887 | "template <typename T>\n" |
26888 | "void S::bar() &&\n" |
26889 | " requires Foo<T> {}\n" |
26890 | "template <typename T>\n" |
26891 | " requires Foo<T> Bar(T) -> Bar<T>;", |
26892 | Style); |
26893 | |
26894 | verifyFormat("template <typename AAAAAAA>\n" |
26895 | " requires Foo<AAAAAAAAAAAAAAAA>\n" |
26896 | "struct Bar {};\n" |
26897 | "template <typename AAAAAAA>\n" |
26898 | " requires Foo<AAAAAAAAAAAAAAAA>\n" |
26899 | "void bar() {}\n" |
26900 | "template <typename AAAAAAA>\n" |
26901 | "void bar()\n" |
26902 | " requires Foo<AAAAAAAAAAAAAAAA> {}\n" |
26903 | "template <typename AAAAAAA>\n" |
26904 | " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n" |
26905 | "template <typename AAAAAAA>\n" |
26906 | " requires Foo<AAAAAAAAAAAAAAAA>\n" |
26907 | "Bar(T) -> Bar<T>;", |
26908 | ColumnStyle); |
26909 | |
26910 | Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; |
26911 | ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; |
26912 | |
26913 | verifyFormat("template <typename T> requires Foo<T>\n" |
26914 | "struct Bar {};\n" |
26915 | "template <typename T> requires Foo<T>\n" |
26916 | "void bar() {}\n" |
26917 | "template <typename T>\n" |
26918 | "void bar() requires Foo<T>\n" |
26919 | "{}\n" |
26920 | "template <typename T> void bar() requires Foo<T>;\n" |
26921 | "template <typename T>\n" |
26922 | "void S::bar() && requires Foo<T>\n" |
26923 | "{}\n" |
26924 | "template <typename T> requires Foo<T>\n" |
26925 | "Bar(T) -> Bar<T>;", |
26926 | Style); |
26927 | |
26928 | verifyFormat("template <typename AAAAAAA>\n" |
26929 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26930 | "struct Bar {};\n" |
26931 | "template <typename AAAAAAA>\n" |
26932 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26933 | "void bar() {}\n" |
26934 | "template <typename AAAAAAA>\n" |
26935 | "void bar()\n" |
26936 | " requires Foo<AAAAAAAAAAAAAAAA>\n" |
26937 | "{}\n" |
26938 | "template <typename AAAAAAA>\n" |
26939 | "requires Foo<AAAAAAAA>\n" |
26940 | "Bar(T) -> Bar<T>;\n" |
26941 | "template <typename AAAAAAA>\n" |
26942 | "requires Foo<AAAAAAAAAAAAAAAA>\n" |
26943 | "Bar(T) -> Bar<T>;", |
26944 | ColumnStyle); |
26945 | } |
26946 | |
26947 | TEST_F(FormatTest, RequiresClauses) { |
26948 | verifyFormat("struct [[nodiscard]] zero_t {\n" |
26949 | " template <class T>\n" |
26950 | " requires requires { number_zero_v<T>; }\n" |
26951 | " [[nodiscard]] constexpr operator T() const {\n" |
26952 | " return number_zero_v<T>;\n" |
26953 | " }\n" |
26954 | "};"); |
26955 | |
26956 | verifyFormat("template <class T>\n" |
26957 | " requires(std::same_as<int, T>)\n" |
26958 | "decltype(auto) fun() {}"); |
26959 | |
26960 | auto Style = getLLVMStyle(); |
26961 | |
26962 | verifyFormat( |
26963 | "template <typename T>\n" |
26964 | " requires is_default_constructible_v<hash<T>> and\n" |
26965 | " is_copy_constructible_v<hash<T>> and\n" |
26966 | " is_move_constructible_v<hash<T>> and\n" |
26967 | " is_copy_assignable_v<hash<T>> and " |
26968 | "is_move_assignable_v<hash<T>> and\n" |
26969 | " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n" |
26970 | " is_callable_v<hash<T>(T)> and\n" |
26971 | " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n" |
26972 | " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n" |
26973 | " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n" |
26974 | "struct S {};", |
26975 | Style); |
26976 | |
26977 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; |
26978 | verifyFormat( |
26979 | "template <typename T>\n" |
26980 | " requires is_default_constructible_v<hash<T>>\n" |
26981 | " and is_copy_constructible_v<hash<T>>\n" |
26982 | " and is_move_constructible_v<hash<T>>\n" |
26983 | " and is_copy_assignable_v<hash<T>> and " |
26984 | "is_move_assignable_v<hash<T>>\n" |
26985 | " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n" |
26986 | " and is_callable_v<hash<T>(T)>\n" |
26987 | " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n" |
26988 | " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n" |
26989 | " and is_same_v<size_t, decltype(hash<T>(declval<const T " |
26990 | "&>()))>\n" |
26991 | "struct S {};", |
26992 | Style); |
26993 | |
26994 | Style = getLLVMStyle(); |
26995 | Style.ConstructorInitializerIndentWidth = 4; |
26996 | Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; |
26997 | Style.PackConstructorInitializers = FormatStyle::PCIS_Never; |
26998 | verifyFormat("constexpr Foo(Foo const &other)\n" |
26999 | " requires std::is_copy_constructible<T>\n" |
27000 | " : value{other.value} {\n" |
27001 | " do_magic();\n" |
27002 | " do_more_magic();\n" |
27003 | "}", |
27004 | Style); |
27005 | |
27006 | // Not a clause, but we once hit an assert. |
27007 | verifyFormat("#if 0\n" |
27008 | "#else\n" |
27009 | "foo();\n" |
27010 | "#endif\n" |
27011 | "bar(requires);"); |
27012 | |
27013 | verifyNoCrash(Code: "template <class T>\n" |
27014 | " requires(requires { std::declval<T>()"); |
27015 | } |
27016 | |
27017 | TEST_F(FormatTest, RequiresExpressionIndentation) { |
27018 | auto Style = getLLVMStyle(); |
27019 | EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope); |
27020 | |
27021 | verifyFormat("template <typename T>\n" |
27022 | "concept C = requires(T t) {\n" |
27023 | " typename T::value;\n" |
27024 | " requires requires(typename T::value v) {\n" |
27025 | " { t == v } -> std::same_as<bool>;\n" |
27026 | " };\n" |
27027 | "};", |
27028 | Style); |
27029 | |
27030 | verifyFormat("template <typename T>\n" |
27031 | "void bar(T)\n" |
27032 | " requires Foo<T> && requires(T t) {\n" |
27033 | " { t.foo() } -> std::same_as<int>;\n" |
27034 | " } && requires(T t) {\n" |
27035 | " { t.bar() } -> std::same_as<bool>;\n" |
27036 | " --t;\n" |
27037 | " };", |
27038 | Style); |
27039 | |
27040 | verifyFormat("template <typename T>\n" |
27041 | " requires Foo<T> &&\n" |
27042 | " requires(T t) {\n" |
27043 | " { t.foo() } -> std::same_as<int>;\n" |
27044 | " } && requires(T t) {\n" |
27045 | " { t.bar() } -> std::same_as<bool>;\n" |
27046 | " --t;\n" |
27047 | " }\n" |
27048 | "void bar(T);", |
27049 | Style); |
27050 | |
27051 | verifyFormat("template <typename T> void f() {\n" |
27052 | " if constexpr (requires(T t) {\n" |
27053 | " { t.bar() } -> std::same_as<bool>;\n" |
27054 | " }) {\n" |
27055 | " }\n" |
27056 | "}", |
27057 | Style); |
27058 | |
27059 | verifyFormat("template <typename T> void f() {\n" |
27060 | " if constexpr (condition && requires(T t) {\n" |
27061 | " { t.bar() } -> std::same_as<bool>;\n" |
27062 | " }) {\n" |
27063 | " }\n" |
27064 | "}", |
27065 | Style); |
27066 | |
27067 | verifyFormat("template <typename T> struct C {\n" |
27068 | " void f()\n" |
27069 | " requires requires(T t) {\n" |
27070 | " { t.bar() } -> std::same_as<bool>;\n" |
27071 | " };\n" |
27072 | "};", |
27073 | Style); |
27074 | |
27075 | Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; |
27076 | |
27077 | verifyFormat("template <typename T>\n" |
27078 | "concept C = requires(T t) {\n" |
27079 | " typename T::value;\n" |
27080 | " requires requires(typename T::value v) {\n" |
27081 | " { t == v } -> std::same_as<bool>;\n" |
27082 | " };\n" |
27083 | " };", |
27084 | Style); |
27085 | |
27086 | verifyFormat( |
27087 | "template <typename T>\n" |
27088 | "void bar(T)\n" |
27089 | " requires Foo<T> && requires(T t) {\n" |
27090 | " { t.foo() } -> std::same_as<int>;\n" |
27091 | " } && requires(T t) {\n" |
27092 | " { t.bar() } -> std::same_as<bool>;\n" |
27093 | " --t;\n" |
27094 | " };", |
27095 | Style); |
27096 | |
27097 | verifyFormat("template <typename T>\n" |
27098 | " requires Foo<T> &&\n" |
27099 | " requires(T t) {\n" |
27100 | " { t.foo() } -> std::same_as<int>;\n" |
27101 | " } && requires(T t) {\n" |
27102 | " { t.bar() } -> std::same_as<bool>;\n" |
27103 | " --t;\n" |
27104 | " }\n" |
27105 | "void bar(T);", |
27106 | Style); |
27107 | |
27108 | verifyFormat("template <typename T> void f() {\n" |
27109 | " if constexpr (requires(T t) {\n" |
27110 | " { t.bar() } -> std::same_as<bool>;\n" |
27111 | " }) {\n" |
27112 | " }\n" |
27113 | "}", |
27114 | Style); |
27115 | |
27116 | verifyFormat( |
27117 | "template <typename T> void f() {\n" |
27118 | " if constexpr (condition && requires(T t) {\n" |
27119 | " { t.bar() } -> std::same_as<bool>;\n" |
27120 | " }) {\n" |
27121 | " }\n" |
27122 | "}", |
27123 | Style); |
27124 | |
27125 | verifyFormat("template <typename T> struct C {\n" |
27126 | " void f()\n" |
27127 | " requires requires(T t) {\n" |
27128 | " { t.bar() } -> std::same_as<bool>;\n" |
27129 | " };\n" |
27130 | "};", |
27131 | Style); |
27132 | } |
27133 | |
27134 | TEST_F(FormatTest, StatementAttributeLikeMacros) { |
27135 | FormatStyle Style = getLLVMStyle(); |
27136 | StringRef Source = "void Foo::slot() {\n" |
27137 | " unsigned char MyChar = 'x';\n" |
27138 | " emit signal(MyChar);\n" |
27139 | " Q_EMIT signal(MyChar);\n" |
27140 | "}"; |
27141 | |
27142 | verifyFormat(Source, Style); |
27143 | |
27144 | Style.AlignConsecutiveDeclarations.Enabled = true; |
27145 | verifyFormat("void Foo::slot() {\n" |
27146 | " unsigned char MyChar = 'x';\n" |
27147 | " emit signal(MyChar);\n" |
27148 | " Q_EMIT signal(MyChar);\n" |
27149 | "}", |
27150 | Source, Style); |
27151 | |
27152 | Style.StatementAttributeLikeMacros.push_back(x: "emit"); |
27153 | verifyFormat(Source, Style); |
27154 | |
27155 | Style.StatementAttributeLikeMacros = {}; |
27156 | verifyFormat("void Foo::slot() {\n" |
27157 | " unsigned char MyChar = 'x';\n" |
27158 | " emit signal(MyChar);\n" |
27159 | " Q_EMIT signal(MyChar);\n" |
27160 | "}", |
27161 | Source, Style); |
27162 | } |
27163 | |
27164 | TEST_F(FormatTest, IndentAccessModifiers) { |
27165 | FormatStyle Style = getLLVMStyle(); |
27166 | Style.IndentAccessModifiers = true; |
27167 | // Members are *two* levels below the record; |
27168 | // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. |
27169 | verifyFormat("class C {\n" |
27170 | " int i;\n" |
27171 | "};", |
27172 | Style); |
27173 | verifyFormat("union C {\n" |
27174 | " int i;\n" |
27175 | " unsigned u;\n" |
27176 | "};", |
27177 | Style); |
27178 | // Access modifiers should be indented one level below the record. |
27179 | verifyFormat("class C {\n" |
27180 | " public:\n" |
27181 | " int i;\n" |
27182 | "};", |
27183 | Style); |
27184 | verifyFormat("class C {\n" |
27185 | " public /* comment */:\n" |
27186 | " int i;\n" |
27187 | "};", |
27188 | Style); |
27189 | verifyFormat("struct S {\n" |
27190 | " private:\n" |
27191 | " class C {\n" |
27192 | " int j;\n" |
27193 | "\n" |
27194 | " public:\n" |
27195 | " C();\n" |
27196 | " };\n" |
27197 | "\n" |
27198 | " public:\n" |
27199 | " int i;\n" |
27200 | "};", |
27201 | Style); |
27202 | // Enumerations are not records and should be unaffected. |
27203 | Style.AllowShortEnumsOnASingleLine = false; |
27204 | verifyFormat("enum class E {\n" |
27205 | " A,\n" |
27206 | " B\n" |
27207 | "};", |
27208 | Style); |
27209 | // Test with a different indentation width; |
27210 | // also proves that the result is Style.AccessModifierOffset agnostic. |
27211 | Style.IndentWidth = 3; |
27212 | verifyFormat("class C {\n" |
27213 | " public:\n" |
27214 | " int i;\n" |
27215 | "};", |
27216 | Style); |
27217 | verifyFormat("class C {\n" |
27218 | " public /**/:\n" |
27219 | " int i;\n" |
27220 | "};", |
27221 | Style); |
27222 | Style.AttributeMacros.push_back(x: "FOO"); |
27223 | verifyFormat("class C {\n" |
27224 | " FOO public:\n" |
27225 | " int i;\n" |
27226 | "};", |
27227 | Style); |
27228 | } |
27229 | |
27230 | TEST_F(FormatTest, LimitlessStringsAndComments) { |
27231 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 0); |
27232 | constexpr StringRef Code = |
27233 | "/**\n" |
27234 | " * This is a multiline comment with quite some long lines, at least for " |
27235 | "the LLVM Style.\n" |
27236 | " * We will redo this with strings and line comments. Just to check if " |
27237 | "everything is working.\n" |
27238 | " */\n" |
27239 | "bool foo() {\n" |
27240 | " /* Single line multi line comment. */\n" |
27241 | " const std::string String = \"This is a multiline string with quite " |
27242 | "some long lines, at least for the LLVM Style.\"\n" |
27243 | " \"We already did it with multi line " |
27244 | "comments, and we will do it with line comments. Just to check if " |
27245 | "everything is working.\";\n" |
27246 | " // This is a line comment (block) with quite some long lines, at " |
27247 | "least for the LLVM Style.\n" |
27248 | " // We already did this with multi line comments and strings. Just to " |
27249 | "check if everything is working.\n" |
27250 | " const std::string SmallString = \"Hello World\";\n" |
27251 | " // Small line comment\n" |
27252 | " return String.size() > SmallString.size();\n" |
27253 | "}"; |
27254 | verifyNoChange(Code, Style); |
27255 | } |
27256 | |
27257 | TEST_F(FormatTest, FormatDecayCopy) { |
27258 | // error cases from unit tests |
27259 | verifyFormat("foo(auto())"); |
27260 | verifyFormat("foo(auto{})"); |
27261 | verifyFormat("foo(auto({}))"); |
27262 | verifyFormat("foo(auto{{}})"); |
27263 | |
27264 | verifyFormat("foo(auto(1))"); |
27265 | verifyFormat("foo(auto{1})"); |
27266 | verifyFormat("foo(new auto(1))"); |
27267 | verifyFormat("foo(new auto{1})"); |
27268 | verifyFormat("decltype(auto(1)) x;"); |
27269 | verifyFormat("decltype(auto{1}) x;"); |
27270 | verifyFormat("auto(x);"); |
27271 | verifyFormat("auto{x};"); |
27272 | verifyFormat("new auto{x};"); |
27273 | verifyFormat("auto{x} = y;"); |
27274 | verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly |
27275 | // the user's own fault |
27276 | verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is |
27277 | // clearly the user's own fault |
27278 | verifyFormat("auto (*p)() = f;"); |
27279 | } |
27280 | |
27281 | TEST_F(FormatTest, Cpp20ModulesSupport) { |
27282 | FormatStyle Style = getLLVMStyle(); |
27283 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; |
27284 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
27285 | |
27286 | verifyFormat("export import foo;", Style); |
27287 | verifyFormat("export import foo:bar;", Style); |
27288 | verifyFormat("export import foo.bar;", Style); |
27289 | verifyFormat("export import foo.bar:baz;", Style); |
27290 | verifyFormat("export import :bar;", Style); |
27291 | verifyFormat("export module foo:bar;", Style); |
27292 | verifyFormat("export module foo;", Style); |
27293 | verifyFormat("export module foo.bar;", Style); |
27294 | verifyFormat("export module foo.bar:baz;", Style); |
27295 | verifyFormat("export import <string_view>;", Style); |
27296 | verifyFormat("export import <Foo/Bar>;", Style); |
27297 | |
27298 | verifyFormat("export type_name var;", Style); |
27299 | verifyFormat("template <class T> export using A = B<T>;", Style); |
27300 | verifyFormat("export using A = B;", Style); |
27301 | verifyFormat("export int func() {\n" |
27302 | " foo();\n" |
27303 | "}", |
27304 | Style); |
27305 | verifyFormat("export struct {\n" |
27306 | " int foo;\n" |
27307 | "};", |
27308 | Style); |
27309 | verifyFormat("export {\n" |
27310 | " int foo;\n" |
27311 | "};", |
27312 | Style); |
27313 | verifyFormat("export export char const *hello() { return \"hello\"; }"); |
27314 | |
27315 | verifyFormat("import bar;", Style); |
27316 | verifyFormat("import foo.bar;", Style); |
27317 | verifyFormat("import foo:bar;", Style); |
27318 | verifyFormat("import :bar;", Style); |
27319 | verifyFormat("import /* module partition */ :bar;", Style); |
27320 | verifyFormat("import <ctime>;", Style); |
27321 | verifyFormat("import \"header\";", Style); |
27322 | |
27323 | verifyFormat("module foo;", Style); |
27324 | verifyFormat("module foo:bar;", Style); |
27325 | verifyFormat("module foo.bar;", Style); |
27326 | verifyFormat("module;", Style); |
27327 | |
27328 | verifyFormat("export namespace hi {\n" |
27329 | "const char *sayhi();\n" |
27330 | "}", |
27331 | Style); |
27332 | |
27333 | verifyFormat("module :private;", Style); |
27334 | verifyFormat("import <foo/bar.h>;", Style); |
27335 | verifyFormat("import foo...bar;", Style); |
27336 | verifyFormat("import ..........;", Style); |
27337 | verifyFormat("module foo:private;", Style); |
27338 | verifyFormat("import a", Style); |
27339 | verifyFormat("module a", Style); |
27340 | verifyFormat("export import a", Style); |
27341 | verifyFormat("export module a", Style); |
27342 | |
27343 | verifyFormat("import", Style); |
27344 | verifyFormat("module", Style); |
27345 | verifyFormat("export", Style); |
27346 | |
27347 | verifyFormat("import /* not keyword */ = val ? 2 : 1;"); |
27348 | } |
27349 | |
27350 | TEST_F(FormatTest, CoroutineForCoawait) { |
27351 | FormatStyle Style = getLLVMStyle(); |
27352 | verifyFormat("for co_await (auto x : range())\n ;"); |
27353 | verifyFormat("for (auto i : arr) {\n" |
27354 | "}", |
27355 | Style); |
27356 | verifyFormat("for co_await (auto i : arr) {\n" |
27357 | "}", |
27358 | Style); |
27359 | verifyFormat("for co_await (auto i : foo(T{})) {\n" |
27360 | "}", |
27361 | Style); |
27362 | } |
27363 | |
27364 | TEST_F(FormatTest, CoroutineCoAwait) { |
27365 | verifyFormat("int x = co_await foo();"); |
27366 | verifyFormat("int x = (co_await foo());"); |
27367 | verifyFormat("co_await (42);"); |
27368 | verifyFormat("void operator co_await(int);"); |
27369 | verifyFormat("void operator co_await(a);"); |
27370 | verifyFormat("co_await a;"); |
27371 | verifyFormat("co_await missing_await_resume{};"); |
27372 | verifyFormat("co_await a; // comment"); |
27373 | verifyFormat("void test0() { co_await a; }"); |
27374 | verifyFormat("co_await co_await co_await foo();"); |
27375 | verifyFormat("co_await foo().bar();"); |
27376 | verifyFormat("co_await [this]() -> Task { co_return x; }"); |
27377 | verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " |
27378 | "foo(); }(x, y);"); |
27379 | |
27380 | FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
27381 | verifyFormat("co_await [this](int a, int b) -> Task {\n" |
27382 | " co_return co_await foo();\n" |
27383 | "}(x, y);", |
27384 | Style); |
27385 | verifyFormat("co_await;"); |
27386 | } |
27387 | |
27388 | TEST_F(FormatTest, CoroutineCoYield) { |
27389 | verifyFormat("int x = co_yield foo();"); |
27390 | verifyFormat("int x = (co_yield foo());"); |
27391 | verifyFormat("co_yield (42);"); |
27392 | verifyFormat("co_yield {42};"); |
27393 | verifyFormat("co_yield 42;"); |
27394 | verifyFormat("co_yield n++;"); |
27395 | verifyFormat("co_yield ++n;"); |
27396 | verifyFormat("co_yield;"); |
27397 | } |
27398 | |
27399 | TEST_F(FormatTest, CoroutineCoReturn) { |
27400 | verifyFormat("co_return (42);"); |
27401 | verifyFormat("co_return;"); |
27402 | verifyFormat("co_return {};"); |
27403 | verifyFormat("co_return x;"); |
27404 | verifyFormat("co_return co_await foo();"); |
27405 | verifyFormat("co_return co_yield foo();"); |
27406 | } |
27407 | |
27408 | TEST_F(FormatTest, EmptyShortBlock) { |
27409 | auto Style = getLLVMStyle(); |
27410 | Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; |
27411 | |
27412 | verifyFormat("try {\n" |
27413 | " doA();\n" |
27414 | "} catch (Exception &e) {\n" |
27415 | " e.printStackTrace();\n" |
27416 | "}", |
27417 | Style); |
27418 | |
27419 | verifyFormat("try {\n" |
27420 | " doA();\n" |
27421 | "} catch (Exception &e) {}", |
27422 | Style); |
27423 | } |
27424 | |
27425 | TEST_F(FormatTest, ShortTemplatedArgumentLists) { |
27426 | auto Style = getLLVMStyle(); |
27427 | |
27428 | verifyFormat("template <> struct S : Template<int (*)[]> {};", Style); |
27429 | verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style); |
27430 | verifyFormat("struct Y : X<[] { return 0; }> {};", Style); |
27431 | verifyFormat("struct Y<[] { return 0; }> {};", Style); |
27432 | |
27433 | verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); |
27434 | verifyFormat("template <int N> struct Foo<char[N]> {};", Style); |
27435 | } |
27436 | |
27437 | TEST_F(FormatTest, MultilineLambdaInConditional) { |
27438 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 70); |
27439 | verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n" |
27440 | " ;\n" |
27441 | " return 5;\n" |
27442 | "}()\n" |
27443 | " : 2;", |
27444 | Style); |
27445 | verifyFormat( |
27446 | "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n" |
27447 | " ;\n" |
27448 | " return 5;\n" |
27449 | "}();", |
27450 | Style); |
27451 | |
27452 | Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
27453 | verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n" |
27454 | " ? []() {\n" |
27455 | " ;\n" |
27456 | " return 5;\n" |
27457 | " }()\n" |
27458 | " : 2;", |
27459 | Style); |
27460 | verifyFormat("auto aLengthyIdentifier =\n" |
27461 | " oneExpressionSoThatWeBreak ? 2 : []() {\n" |
27462 | " ;\n" |
27463 | " return 5;\n" |
27464 | " }();", |
27465 | Style); |
27466 | |
27467 | Style = getLLVMStyleWithColumns(ColumnLimit: 40); |
27468 | verifyFormat("auto aLengthyIdentifier =\n" |
27469 | " oneExpressionSoThatWeBreak ? []() {\n" |
27470 | " ;\n" |
27471 | " return 5;\n" |
27472 | " }()\n" |
27473 | " : 2;", |
27474 | Style); |
27475 | verifyFormat("auto aLengthyIdentifier =\n" |
27476 | " oneExpressionSoThatWeBreak\n" |
27477 | " ? 2\n" |
27478 | " : []() {\n" |
27479 | " ;\n" |
27480 | " return 5;\n" |
27481 | " };", |
27482 | Style); |
27483 | } |
27484 | |
27485 | TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { |
27486 | auto Style = getLLVMStyle(); |
27487 | |
27488 | StringRef Short = "functionCall(paramA, paramB, paramC);\n" |
27489 | "void functionDecl(int a, int b, int c);"; |
27490 | |
27491 | StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " |
27492 | "paramF, paramG, paramH, paramI);\n" |
27493 | "void functionDecl(int argumentA, int argumentB, int " |
27494 | "argumentC, int argumentD, int argumentE);"; |
27495 | |
27496 | verifyFormat(Short, Style); |
27497 | |
27498 | StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " |
27499 | "paramF, paramG, paramH,\n" |
27500 | " paramI);\n" |
27501 | "void functionDecl(int argumentA, int argumentB, int " |
27502 | "argumentC, int argumentD,\n" |
27503 | " int argumentE);"; |
27504 | |
27505 | verifyFormat(NoBreak, Medium, Style); |
27506 | verifyFormat(NoBreak, |
27507 | "functionCall(\n" |
27508 | " paramA,\n" |
27509 | " paramB,\n" |
27510 | " paramC,\n" |
27511 | " paramD,\n" |
27512 | " paramE,\n" |
27513 | " paramF,\n" |
27514 | " paramG,\n" |
27515 | " paramH,\n" |
27516 | " paramI\n" |
27517 | ");\n" |
27518 | "void functionDecl(\n" |
27519 | " int argumentA,\n" |
27520 | " int argumentB,\n" |
27521 | " int argumentC,\n" |
27522 | " int argumentD,\n" |
27523 | " int argumentE\n" |
27524 | ");", |
27525 | Style); |
27526 | |
27527 | verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" |
27528 | " nestedLongFunctionCall(argument1, " |
27529 | "argument2, argument3,\n" |
27530 | " argument4, " |
27531 | "argument5));", |
27532 | Style); |
27533 | |
27534 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
27535 | |
27536 | verifyFormat(Short, Style); |
27537 | verifyFormat( |
27538 | "functionCall(\n" |
27539 | " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " |
27540 | "paramI\n" |
27541 | ");\n" |
27542 | "void functionDecl(\n" |
27543 | " int argumentA, int argumentB, int argumentC, int argumentD, int " |
27544 | "argumentE\n" |
27545 | ");", |
27546 | Medium, Style); |
27547 | |
27548 | Style.AllowAllArgumentsOnNextLine = false; |
27549 | Style.AllowAllParametersOfDeclarationOnNextLine = false; |
27550 | |
27551 | verifyFormat(Short, Style); |
27552 | verifyFormat( |
27553 | "functionCall(\n" |
27554 | " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " |
27555 | "paramI\n" |
27556 | ");\n" |
27557 | "void functionDecl(\n" |
27558 | " int argumentA, int argumentB, int argumentC, int argumentD, int " |
27559 | "argumentE\n" |
27560 | ");", |
27561 | Medium, Style); |
27562 | |
27563 | Style.BinPackArguments = false; |
27564 | Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; |
27565 | |
27566 | verifyFormat(Short, Style); |
27567 | |
27568 | verifyFormat("functionCall(\n" |
27569 | " paramA,\n" |
27570 | " paramB,\n" |
27571 | " paramC,\n" |
27572 | " paramD,\n" |
27573 | " paramE,\n" |
27574 | " paramF,\n" |
27575 | " paramG,\n" |
27576 | " paramH,\n" |
27577 | " paramI\n" |
27578 | ");\n" |
27579 | "void functionDecl(\n" |
27580 | " int argumentA,\n" |
27581 | " int argumentB,\n" |
27582 | " int argumentC,\n" |
27583 | " int argumentD,\n" |
27584 | " int argumentE\n" |
27585 | ");", |
27586 | Medium, Style); |
27587 | |
27588 | verifyFormat("outerFunctionCall(\n" |
27589 | " nestedFunctionCall(argument1),\n" |
27590 | " nestedLongFunctionCall(\n" |
27591 | " argument1,\n" |
27592 | " argument2,\n" |
27593 | " argument3,\n" |
27594 | " argument4,\n" |
27595 | " argument5\n" |
27596 | " )\n" |
27597 | ");", |
27598 | Style); |
27599 | |
27600 | verifyFormat("int a = (int)b;", Style); |
27601 | verifyFormat("int a = (int)b;", |
27602 | "int a = (\n" |
27603 | " int\n" |
27604 | ") b;", |
27605 | Style); |
27606 | |
27607 | verifyFormat("return (true);", Style); |
27608 | verifyFormat("return (true);", |
27609 | "return (\n" |
27610 | " true\n" |
27611 | ");", |
27612 | Style); |
27613 | |
27614 | verifyFormat("void foo();", Style); |
27615 | verifyFormat("void foo();", |
27616 | "void foo(\n" |
27617 | ");", |
27618 | Style); |
27619 | |
27620 | verifyFormat("void foo() {}", Style); |
27621 | verifyFormat("void foo() {}", |
27622 | "void foo(\n" |
27623 | ") {\n" |
27624 | "}", |
27625 | Style); |
27626 | |
27627 | verifyFormat("auto string = std::string();", Style); |
27628 | verifyFormat("auto string = std::string();", |
27629 | "auto string = std::string(\n" |
27630 | ");", |
27631 | Style); |
27632 | |
27633 | verifyFormat("void (*functionPointer)() = nullptr;", Style); |
27634 | verifyFormat("void (*functionPointer)() = nullptr;", |
27635 | "void (\n" |
27636 | " *functionPointer\n" |
27637 | ")\n" |
27638 | "(\n" |
27639 | ") = nullptr;", |
27640 | Style); |
27641 | } |
27642 | |
27643 | TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { |
27644 | auto Style = getLLVMStyle(); |
27645 | |
27646 | verifyFormat("if (foo()) {\n" |
27647 | " return;\n" |
27648 | "}", |
27649 | Style); |
27650 | |
27651 | verifyFormat("if (quiteLongArg !=\n" |
27652 | " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg " |
27653 | "comment\n" |
27654 | " return;\n" |
27655 | "}", |
27656 | Style); |
27657 | |
27658 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
27659 | |
27660 | verifyFormat("if (foo()) {\n" |
27661 | " return;\n" |
27662 | "}", |
27663 | Style); |
27664 | |
27665 | verifyFormat("if (quiteLongArg !=\n" |
27666 | " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg " |
27667 | "comment\n" |
27668 | " return;\n" |
27669 | "}", |
27670 | Style); |
27671 | |
27672 | verifyFormat("void foo() {\n" |
27673 | " if (camelCaseName < alsoLongName ||\n" |
27674 | " anotherEvenLongerName <=\n" |
27675 | " thisReallyReallyReallyReallyReallyReallyLongerName ||" |
27676 | "\n" |
27677 | " otherName < thisLastName) {\n" |
27678 | " return;\n" |
27679 | " } else if (quiteLongName < alsoLongName ||\n" |
27680 | " anotherEvenLongerName <=\n" |
27681 | " thisReallyReallyReallyReallyReallyReallyLonger" |
27682 | "Name ||\n" |
27683 | " otherName < thisLastName) {\n" |
27684 | " return;\n" |
27685 | " }\n" |
27686 | "}", |
27687 | Style); |
27688 | |
27689 | Style.ContinuationIndentWidth = 2; |
27690 | verifyFormat("void foo() {\n" |
27691 | " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n" |
27692 | " ontoMultipleLines && whenFormattedCorrectly) {\n" |
27693 | " if (false) {\n" |
27694 | " return;\n" |
27695 | " } else if (thisIsRatherALongIfClause && " |
27696 | "thatIExpectToBeBroken ||\n" |
27697 | " ontoMultipleLines && whenFormattedCorrectly) {\n" |
27698 | " return;\n" |
27699 | " }\n" |
27700 | " }\n" |
27701 | "}", |
27702 | Style); |
27703 | } |
27704 | |
27705 | TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { |
27706 | auto Style = getLLVMStyle(); |
27707 | |
27708 | verifyFormat("for (int i = 0; i < 5; ++i) {\n" |
27709 | " doSomething();\n" |
27710 | "}", |
27711 | Style); |
27712 | |
27713 | verifyFormat("for (int myReallyLongCountVariable = 0; " |
27714 | "myReallyLongCountVariable < count;\n" |
27715 | " myReallyLongCountVariable++) {\n" |
27716 | " doSomething();\n" |
27717 | "}", |
27718 | Style); |
27719 | |
27720 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
27721 | |
27722 | verifyFormat("for (int i = 0; i < 5; ++i) {\n" |
27723 | " doSomething();\n" |
27724 | "}", |
27725 | Style); |
27726 | |
27727 | verifyFormat("for (int myReallyLongCountVariable = 0; " |
27728 | "myReallyLongCountVariable < count;\n" |
27729 | " myReallyLongCountVariable++) {\n" |
27730 | " doSomething();\n" |
27731 | "}", |
27732 | Style); |
27733 | } |
27734 | |
27735 | TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) { |
27736 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
27737 | Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; |
27738 | // Aggregate initialization. |
27739 | verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" |
27740 | " 10000000, 20000000\n" |
27741 | "};", |
27742 | Style); |
27743 | verifyFormat("SomeStruct s{\n" |
27744 | " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n" |
27745 | " \"zzzzzzzzzzzzzzzz\"\n" |
27746 | "};", |
27747 | Style); |
27748 | // Designated initializers. |
27749 | verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" |
27750 | " [0] = 10000000, [1] = 20000000\n" |
27751 | "};", |
27752 | Style); |
27753 | verifyFormat("SomeStruct s{\n" |
27754 | " .foo = \"xxxxxxxxxxxxx\",\n" |
27755 | " .bar = \"yyyyyyyyyyyyy\",\n" |
27756 | " .baz = \"zzzzzzzzzzzzz\"\n" |
27757 | "};", |
27758 | Style); |
27759 | // List initialization. |
27760 | verifyFormat("SomeStruct s{\n" |
27761 | " \"xxxxxxxxxxxxx\",\n" |
27762 | " \"yyyyyyyyyyyyy\",\n" |
27763 | " \"zzzzzzzzzzzzz\",\n" |
27764 | "};", |
27765 | Style); |
27766 | verifyFormat("SomeStruct{\n" |
27767 | " \"xxxxxxxxxxxxx\",\n" |
27768 | " \"yyyyyyyyyyyyy\",\n" |
27769 | " \"zzzzzzzzzzzzz\",\n" |
27770 | "};", |
27771 | Style); |
27772 | verifyFormat("new SomeStruct{\n" |
27773 | " \"xxxxxxxxxxxxx\",\n" |
27774 | " \"yyyyyyyyyyyyy\",\n" |
27775 | " \"zzzzzzzzzzzzz\",\n" |
27776 | "};", |
27777 | Style); |
27778 | // Member initializer. |
27779 | verifyFormat("class SomeClass {\n" |
27780 | " SomeStruct s{\n" |
27781 | " \"xxxxxxxxxxxxx\",\n" |
27782 | " \"yyyyyyyyyyyyy\",\n" |
27783 | " \"zzzzzzzzzzzzz\",\n" |
27784 | " };\n" |
27785 | "};", |
27786 | Style); |
27787 | // Constructor member initializer. |
27788 | verifyFormat("SomeClass::SomeClass : strct{\n" |
27789 | " \"xxxxxxxxxxxxx\",\n" |
27790 | " \"yyyyyyyyyyyyy\",\n" |
27791 | " \"zzzzzzzzzzzzz\",\n" |
27792 | " } {}", |
27793 | Style); |
27794 | // Copy initialization. |
27795 | verifyFormat("SomeStruct s = SomeStruct{\n" |
27796 | " \"xxxxxxxxxxxxx\",\n" |
27797 | " \"yyyyyyyyyyyyy\",\n" |
27798 | " \"zzzzzzzzzzzzz\",\n" |
27799 | "};", |
27800 | Style); |
27801 | // Copy list initialization. |
27802 | verifyFormat("SomeStruct s = {\n" |
27803 | " \"xxxxxxxxxxxxx\",\n" |
27804 | " \"yyyyyyyyyyyyy\",\n" |
27805 | " \"zzzzzzzzzzzzz\",\n" |
27806 | "};", |
27807 | Style); |
27808 | // Assignment operand initialization. |
27809 | verifyFormat("s = {\n" |
27810 | " \"xxxxxxxxxxxxx\",\n" |
27811 | " \"yyyyyyyyyyyyy\",\n" |
27812 | " \"zzzzzzzzzzzzz\",\n" |
27813 | "};", |
27814 | Style); |
27815 | // Returned object initialization. |
27816 | verifyFormat("return {\n" |
27817 | " \"xxxxxxxxxxxxx\",\n" |
27818 | " \"yyyyyyyyyyyyy\",\n" |
27819 | " \"zzzzzzzzzzzzz\",\n" |
27820 | "};", |
27821 | Style); |
27822 | // Initializer list. |
27823 | verifyFormat("auto initializerList = {\n" |
27824 | " \"xxxxxxxxxxxxx\",\n" |
27825 | " \"yyyyyyyyyyyyy\",\n" |
27826 | " \"zzzzzzzzzzzzz\",\n" |
27827 | "};", |
27828 | Style); |
27829 | // Function parameter initialization. |
27830 | verifyFormat("func({\n" |
27831 | " \"xxxxxxxxxxxxx\",\n" |
27832 | " \"yyyyyyyyyyyyy\",\n" |
27833 | " \"zzzzzzzzzzzzz\",\n" |
27834 | "});", |
27835 | Style); |
27836 | // Nested init lists. |
27837 | verifyFormat("SomeStruct s = {\n" |
27838 | " {{init1, init2, init3, init4, init5},\n" |
27839 | " {init1, init2, init3, init4, init5}}\n" |
27840 | "};", |
27841 | Style); |
27842 | verifyFormat("SomeStruct s = {\n" |
27843 | " {{\n" |
27844 | " .init1 = 1,\n" |
27845 | " .init2 = 2,\n" |
27846 | " .init3 = 3,\n" |
27847 | " .init4 = 4,\n" |
27848 | " .init5 = 5,\n" |
27849 | " },\n" |
27850 | " {init1, init2, init3, init4, init5}}\n" |
27851 | "};", |
27852 | Style); |
27853 | verifyFormat("SomeArrayT a[3] = {\n" |
27854 | " {\n" |
27855 | " foo,\n" |
27856 | " bar,\n" |
27857 | " },\n" |
27858 | " {\n" |
27859 | " foo,\n" |
27860 | " bar,\n" |
27861 | " },\n" |
27862 | " SomeArrayT{},\n" |
27863 | "};", |
27864 | Style); |
27865 | verifyFormat("SomeArrayT a[3] = {\n" |
27866 | " {foo},\n" |
27867 | " {\n" |
27868 | " {\n" |
27869 | " init1,\n" |
27870 | " init2,\n" |
27871 | " init3,\n" |
27872 | " },\n" |
27873 | " {\n" |
27874 | " init1,\n" |
27875 | " init2,\n" |
27876 | " init3,\n" |
27877 | " },\n" |
27878 | " },\n" |
27879 | " {baz},\n" |
27880 | "};", |
27881 | Style); |
27882 | } |
27883 | |
27884 | TEST_F(FormatTest, UnderstandsDigraphs) { |
27885 | verifyFormat("int arr<:5:> = {};"); |
27886 | verifyFormat("int arr[5] = <%%>;"); |
27887 | verifyFormat("int arr<:::qualified_variable:> = {};"); |
27888 | verifyFormat("int arr[::qualified_variable] = <%%>;"); |
27889 | verifyFormat("%:include <header>"); |
27890 | verifyFormat("%:define A x##y"); |
27891 | verifyFormat("#define A x%:%:y"); |
27892 | } |
27893 | |
27894 | TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) { |
27895 | auto Style = getLLVMStyle(); |
27896 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; |
27897 | Style.AlignConsecutiveAssignments.Enabled = true; |
27898 | Style.AlignConsecutiveDeclarations.Enabled = true; |
27899 | |
27900 | // The AlignArray code is incorrect for non square Arrays and can cause |
27901 | // crashes, these tests assert that the array is not changed but will |
27902 | // also act as regression tests for when it is properly fixed |
27903 | verifyFormat("struct test demo[] = {\n" |
27904 | " {1, 2},\n" |
27905 | " {3, 4, 5},\n" |
27906 | " {6, 7, 8}\n" |
27907 | "};", |
27908 | Style); |
27909 | verifyFormat("struct test demo[] = {\n" |
27910 | " {1, 2, 3, 4, 5},\n" |
27911 | " {3, 4, 5},\n" |
27912 | " {6, 7, 8}\n" |
27913 | "};", |
27914 | Style); |
27915 | verifyFormat("struct test demo[] = {\n" |
27916 | " {1, 2, 3, 4, 5},\n" |
27917 | " {3, 4, 5},\n" |
27918 | " {6, 7, 8, 9, 10, 11, 12}\n" |
27919 | "};", |
27920 | Style); |
27921 | verifyFormat("struct test demo[] = {\n" |
27922 | " {1, 2, 3},\n" |
27923 | " {3, 4, 5},\n" |
27924 | " {6, 7, 8, 9, 10, 11, 12}\n" |
27925 | "};", |
27926 | Style); |
27927 | |
27928 | verifyFormat("S{\n" |
27929 | " {},\n" |
27930 | " {},\n" |
27931 | " {a, b}\n" |
27932 | "};", |
27933 | Style); |
27934 | verifyFormat("S{\n" |
27935 | " {},\n" |
27936 | " {},\n" |
27937 | " {a, b},\n" |
27938 | "};", |
27939 | Style); |
27940 | verifyFormat("void foo() {\n" |
27941 | " auto thing = test{\n" |
27942 | " {\n" |
27943 | " {13}, {something}, // A\n" |
27944 | " }\n" |
27945 | " };\n" |
27946 | "}", |
27947 | "void foo() {\n" |
27948 | " auto thing = test{\n" |
27949 | " {\n" |
27950 | " {13},\n" |
27951 | " {something}, // A\n" |
27952 | " }\n" |
27953 | " };\n" |
27954 | "}", |
27955 | Style); |
27956 | } |
27957 | |
27958 | TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) { |
27959 | auto Style = getLLVMStyle(); |
27960 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; |
27961 | Style.AlignConsecutiveAssignments.Enabled = true; |
27962 | Style.AlignConsecutiveDeclarations.Enabled = true; |
27963 | |
27964 | // The AlignArray code is incorrect for non square Arrays and can cause |
27965 | // crashes, these tests assert that the array is not changed but will |
27966 | // also act as regression tests for when it is properly fixed |
27967 | verifyFormat("struct test demo[] = {\n" |
27968 | " {1, 2},\n" |
27969 | " {3, 4, 5},\n" |
27970 | " {6, 7, 8}\n" |
27971 | "};", |
27972 | Style); |
27973 | verifyFormat("struct test demo[] = {\n" |
27974 | " {1, 2, 3, 4, 5},\n" |
27975 | " {3, 4, 5},\n" |
27976 | " {6, 7, 8}\n" |
27977 | "};", |
27978 | Style); |
27979 | verifyFormat("struct test demo[] = {\n" |
27980 | " {1, 2, 3, 4, 5},\n" |
27981 | " {3, 4, 5},\n" |
27982 | " {6, 7, 8, 9, 10, 11, 12}\n" |
27983 | "};", |
27984 | Style); |
27985 | verifyFormat("struct test demo[] = {\n" |
27986 | " {1, 2, 3},\n" |
27987 | " {3, 4, 5},\n" |
27988 | " {6, 7, 8, 9, 10, 11, 12}\n" |
27989 | "};", |
27990 | Style); |
27991 | |
27992 | verifyFormat("S{\n" |
27993 | " {},\n" |
27994 | " {},\n" |
27995 | " {a, b}\n" |
27996 | "};", |
27997 | Style); |
27998 | verifyFormat("S{\n" |
27999 | " {},\n" |
28000 | " {},\n" |
28001 | " {a, b},\n" |
28002 | "};", |
28003 | Style); |
28004 | verifyFormat("void foo() {\n" |
28005 | " auto thing = test{\n" |
28006 | " {\n" |
28007 | " {13}, {something}, // A\n" |
28008 | " }\n" |
28009 | " };\n" |
28010 | "}", |
28011 | "void foo() {\n" |
28012 | " auto thing = test{\n" |
28013 | " {\n" |
28014 | " {13},\n" |
28015 | " {something}, // A\n" |
28016 | " }\n" |
28017 | " };\n" |
28018 | "}", |
28019 | Style); |
28020 | } |
28021 | |
28022 | TEST_F(FormatTest, FormatsVariableTemplates) { |
28023 | verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;"); |
28024 | verifyFormat("template <typename T> " |
28025 | "inline bool var = is_integral_v<T> && is_signed_v<T>;"); |
28026 | } |
28027 | |
28028 | TEST_F(FormatTest, RemoveSemicolon) { |
28029 | FormatStyle Style = getLLVMStyle(); |
28030 | Style.RemoveSemicolon = true; |
28031 | |
28032 | verifyFormat("int max(int a, int b) { return a > b ? a : b; }", |
28033 | "int max(int a, int b) { return a > b ? a : b; };", Style); |
28034 | |
28035 | verifyFormat("int max(int a, int b) { return a > b ? a : b; }", |
28036 | "int max(int a, int b) { return a > b ? a : b; };;", Style); |
28037 | |
28038 | verifyFormat("class Foo {\n" |
28039 | " int getSomething() const { return something; }\n" |
28040 | "};", |
28041 | "class Foo {\n" |
28042 | " int getSomething() const { return something; };\n" |
28043 | "};", |
28044 | Style); |
28045 | |
28046 | verifyFormat("class Foo {\n" |
28047 | " int getSomething() const { return something; }\n" |
28048 | "};", |
28049 | "class Foo {\n" |
28050 | " int getSomething() const { return something; };;\n" |
28051 | "};", |
28052 | Style); |
28053 | |
28054 | verifyFormat("for (;;) {\n" |
28055 | "}", |
28056 | Style); |
28057 | |
28058 | verifyFormat("class [[deprecated(\"\")]] C {\n" |
28059 | " int i;\n" |
28060 | "};", |
28061 | Style); |
28062 | |
28063 | verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n" |
28064 | " int i;\n" |
28065 | "};", |
28066 | Style); |
28067 | |
28068 | verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style); |
28069 | |
28070 | verifyFormat("void main() {}", "void main() {};", Style); |
28071 | |
28072 | verifyFormat("struct Foo {\n" |
28073 | " Foo() {}\n" |
28074 | " ~Foo() {}\n" |
28075 | "};", |
28076 | "struct Foo {\n" |
28077 | " Foo() {};\n" |
28078 | " ~Foo() {};\n" |
28079 | "};", |
28080 | Style); |
28081 | |
28082 | // We can't (and probably shouldn't) support the following. |
28083 | #if 0 |
28084 | verifyFormat("void foo() {} //\n" |
28085 | "int bar;", |
28086 | "void foo() {}; //\n" |
28087 | "; int bar;", |
28088 | Style); |
28089 | #endif |
28090 | |
28091 | verifyFormat("auto sgf = [] {\n" |
28092 | " ogl = {\n" |
28093 | " a, b, c, d, e,\n" |
28094 | " };\n" |
28095 | "};", |
28096 | Style); |
28097 | |
28098 | Style.TypenameMacros.push_back(x: "STRUCT"); |
28099 | verifyFormat("STRUCT(T, B) { int i; };", Style); |
28100 | } |
28101 | |
28102 | TEST_F(FormatTest, EnumTrailingComma) { |
28103 | constexpr StringRef Code("enum : int { /**/ };\n" |
28104 | "enum {\n" |
28105 | " a,\n" |
28106 | " b,\n" |
28107 | " c, //\n" |
28108 | "};\n" |
28109 | "enum Color { red, green, blue /**/ };"); |
28110 | verifyFormat(Code); |
28111 | |
28112 | auto Style = getLLVMStyle(); |
28113 | Style.EnumTrailingComma = FormatStyle::ETC_Insert; |
28114 | verifyFormat("enum : int { /**/ };\n" |
28115 | "enum {\n" |
28116 | " a,\n" |
28117 | " b,\n" |
28118 | " c, //\n" |
28119 | "};\n" |
28120 | "enum Color { red, green, blue, /**/ };", |
28121 | Code, Style); |
28122 | |
28123 | Style.EnumTrailingComma = FormatStyle::ETC_Remove; |
28124 | verifyFormat("enum : int { /**/ };\n" |
28125 | "enum {\n" |
28126 | " a,\n" |
28127 | " b,\n" |
28128 | " c //\n" |
28129 | "};\n" |
28130 | "enum Color { red, green, blue /**/ };", |
28131 | Code, Style); |
28132 | |
28133 | EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine); |
28134 | Style.AllowShortEnumsOnASingleLine = false; |
28135 | |
28136 | constexpr StringRef Input("enum {\n" |
28137 | " //\n" |
28138 | " a,\n" |
28139 | " /**/\n" |
28140 | " b,\n" |
28141 | "};"); |
28142 | verifyFormat(Input, Input, Style, {tooling::Range(12, 3)}); // line 3 |
28143 | verifyFormat("enum {\n" |
28144 | " //\n" |
28145 | " a,\n" |
28146 | " /**/\n" |
28147 | " b\n" |
28148 | "};", |
28149 | Input, Style, {tooling::Range(24, 3)}); // line 5 |
28150 | |
28151 | Style.EnumTrailingComma = FormatStyle::ETC_Insert; |
28152 | verifyFormat("enum class MyEnum_E {\n" |
28153 | " MY_ENUM = 0U,\n" |
28154 | "};", |
28155 | "enum class MyEnum_E {\n" |
28156 | " MY_ENUM = 0U\n" |
28157 | "};", |
28158 | Style); |
28159 | } |
28160 | |
28161 | TEST_F(FormatTest, BreakAfterAttributes) { |
28162 | constexpr StringRef Code("[[maybe_unused]] const int i;\n" |
28163 | "[[foo([[]])]] [[maybe_unused]]\n" |
28164 | "int j;\n" |
28165 | "[[maybe_unused]]\n" |
28166 | "foo<int> k;\n" |
28167 | "[[nodiscard]] inline int f(int &i);\n" |
28168 | "[[foo([[]])]] [[nodiscard]]\n" |
28169 | "int g(int &i);\n" |
28170 | "[[nodiscard]]\n" |
28171 | "inline int f(int &i) {\n" |
28172 | " i = 1;\n" |
28173 | " return 0;\n" |
28174 | "}\n" |
28175 | "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n" |
28176 | " i = 0;\n" |
28177 | " return 1;\n" |
28178 | "}"); |
28179 | |
28180 | FormatStyle Style = getLLVMStyle(); |
28181 | EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave); |
28182 | verifyNoChange(Code, Style); |
28183 | |
28184 | Style.BreakAfterAttributes = FormatStyle::ABS_Never; |
28185 | verifyFormat("[[maybe_unused]] const int i;\n" |
28186 | "[[foo([[]])]] [[maybe_unused]] int j;\n" |
28187 | "[[maybe_unused]] foo<int> k;\n" |
28188 | "[[nodiscard]] inline int f(int &i);\n" |
28189 | "[[foo([[]])]] [[nodiscard]] int g(int &i);\n" |
28190 | "[[nodiscard]] inline int f(int &i) {\n" |
28191 | " i = 1;\n" |
28192 | " return 0;\n" |
28193 | "}\n" |
28194 | "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n" |
28195 | " i = 0;\n" |
28196 | " return 1;\n" |
28197 | "}", |
28198 | Code, Style); |
28199 | |
28200 | Style.BreakAfterAttributes = FormatStyle::ABS_Always; |
28201 | verifyFormat("[[maybe_unused]]\n" |
28202 | "const int i;\n" |
28203 | "[[foo([[]])]] [[maybe_unused]]\n" |
28204 | "int j;\n" |
28205 | "[[maybe_unused]]\n" |
28206 | "foo<int> k;\n" |
28207 | "[[nodiscard]]\n" |
28208 | "inline int f(int &i);\n" |
28209 | "[[foo([[]])]] [[nodiscard]]\n" |
28210 | "int g(int &i);\n" |
28211 | "[[nodiscard]]\n" |
28212 | "inline int f(int &i) {\n" |
28213 | " i = 1;\n" |
28214 | " return 0;\n" |
28215 | "}\n" |
28216 | "[[foo([[]])]] [[nodiscard]]\n" |
28217 | "int g(int &i) {\n" |
28218 | " i = 0;\n" |
28219 | " return 1;\n" |
28220 | "}", |
28221 | Code, Style); |
28222 | |
28223 | constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n" |
28224 | " f();\n" |
28225 | "else\n" |
28226 | " g();\n" |
28227 | "[[foo([[]])]]\n" |
28228 | "switch (b) {\n" |
28229 | "[[unlikely]] case 1:\n" |
28230 | " ++b;\n" |
28231 | " break;\n" |
28232 | "[[likely]]\n" |
28233 | "default:\n" |
28234 | " return;\n" |
28235 | "}\n" |
28236 | "[[unlikely]] for (; c > 0; --c)\n" |
28237 | " h();\n" |
28238 | "[[likely]]\n" |
28239 | "while (d > 0)\n" |
28240 | " --d;"); |
28241 | |
28242 | Style.BreakAfterAttributes = FormatStyle::ABS_Leave; |
28243 | verifyNoChange(CtrlStmtCode, Style); |
28244 | |
28245 | Style.BreakAfterAttributes = FormatStyle::ABS_Never; |
28246 | verifyFormat("[[likely]] if (a)\n" |
28247 | " f();\n" |
28248 | "else\n" |
28249 | " g();\n" |
28250 | "[[foo([[]])]] switch (b) {\n" |
28251 | "[[unlikely]] case 1:\n" |
28252 | " ++b;\n" |
28253 | " break;\n" |
28254 | "[[likely]] default:\n" |
28255 | " return;\n" |
28256 | "}\n" |
28257 | "[[unlikely]] for (; c > 0; --c)\n" |
28258 | " h();\n" |
28259 | "[[likely]] while (d > 0)\n" |
28260 | " --d;", |
28261 | CtrlStmtCode, Style); |
28262 | |
28263 | Style.BreakAfterAttributes = FormatStyle::ABS_Always; |
28264 | verifyFormat("[[likely]]\n" |
28265 | "if (a)\n" |
28266 | " f();\n" |
28267 | "else\n" |
28268 | " g();\n" |
28269 | "[[foo([[]])]]\n" |
28270 | "switch (b) {\n" |
28271 | "[[unlikely]]\n" |
28272 | "case 1:\n" |
28273 | " ++b;\n" |
28274 | " break;\n" |
28275 | "[[likely]]\n" |
28276 | "default:\n" |
28277 | " return;\n" |
28278 | "}\n" |
28279 | "[[unlikely]]\n" |
28280 | "for (; c > 0; --c)\n" |
28281 | " h();\n" |
28282 | "[[likely]]\n" |
28283 | "while (d > 0)\n" |
28284 | " --d;", |
28285 | CtrlStmtCode, Style); |
28286 | |
28287 | verifyFormat("[[nodiscard]]\n" |
28288 | "operator bool();\n" |
28289 | "[[nodiscard]]\n" |
28290 | "operator bool() {\n" |
28291 | " return true;\n" |
28292 | "}", |
28293 | "[[nodiscard]] operator bool();\n" |
28294 | "[[nodiscard]] operator bool() { return true; }", |
28295 | Style); |
28296 | |
28297 | constexpr StringRef CtorDtorCode("struct Foo {\n" |
28298 | " [[deprecated]] Foo();\n" |
28299 | " [[deprecated]] Foo() {}\n" |
28300 | " [[deprecated]] ~Foo();\n" |
28301 | " [[deprecated]] ~Foo() {}\n" |
28302 | " [[deprecated]] void f();\n" |
28303 | " [[deprecated]] void f() {}\n" |
28304 | "};\n" |
28305 | "[[deprecated]] Bar::Bar() {}\n" |
28306 | "[[deprecated]] Bar::~Bar() {}\n" |
28307 | "[[deprecated]] void g() {}"); |
28308 | verifyFormat("struct Foo {\n" |
28309 | " [[deprecated]]\n" |
28310 | " Foo();\n" |
28311 | " [[deprecated]]\n" |
28312 | " Foo() {}\n" |
28313 | " [[deprecated]]\n" |
28314 | " ~Foo();\n" |
28315 | " [[deprecated]]\n" |
28316 | " ~Foo() {}\n" |
28317 | " [[deprecated]]\n" |
28318 | " void f();\n" |
28319 | " [[deprecated]]\n" |
28320 | " void f() {}\n" |
28321 | "};\n" |
28322 | "[[deprecated]]\n" |
28323 | "Bar::Bar() {}\n" |
28324 | "[[deprecated]]\n" |
28325 | "Bar::~Bar() {}\n" |
28326 | "[[deprecated]]\n" |
28327 | "void g() {}", |
28328 | CtorDtorCode, Style); |
28329 | |
28330 | Style.BreakBeforeBraces = FormatStyle::BS_Linux; |
28331 | verifyFormat("struct Foo {\n" |
28332 | " [[deprecated]]\n" |
28333 | " Foo();\n" |
28334 | " [[deprecated]]\n" |
28335 | " Foo()\n" |
28336 | " {\n" |
28337 | " }\n" |
28338 | " [[deprecated]]\n" |
28339 | " ~Foo();\n" |
28340 | " [[deprecated]]\n" |
28341 | " ~Foo()\n" |
28342 | " {\n" |
28343 | " }\n" |
28344 | " [[deprecated]]\n" |
28345 | " void f();\n" |
28346 | " [[deprecated]]\n" |
28347 | " void f()\n" |
28348 | " {\n" |
28349 | " }\n" |
28350 | "};\n" |
28351 | "[[deprecated]]\n" |
28352 | "Bar::Bar()\n" |
28353 | "{\n" |
28354 | "}\n" |
28355 | "[[deprecated]]\n" |
28356 | "Bar::~Bar()\n" |
28357 | "{\n" |
28358 | "}\n" |
28359 | "[[deprecated]]\n" |
28360 | "void g()\n" |
28361 | "{\n" |
28362 | "}", |
28363 | CtorDtorCode, Style); |
28364 | |
28365 | verifyFormat("struct Foo {\n" |
28366 | " [[maybe_unused]]\n" |
28367 | " void operator+();\n" |
28368 | "};\n" |
28369 | "[[nodiscard]]\n" |
28370 | "Foo &operator-(Foo &);", |
28371 | Style); |
28372 | |
28373 | Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left; |
28374 | verifyFormat("[[nodiscard]]\n" |
28375 | "Foo& operator-(Foo&);", |
28376 | Style); |
28377 | } |
28378 | |
28379 | TEST_F(FormatTest, InsertNewlineAtEOF) { |
28380 | FormatStyle Style = getLLVMStyle(); |
28381 | Style.InsertNewlineAtEOF = true; |
28382 | |
28383 | verifyNoChange("int i;\n", Style); |
28384 | verifyFormat("int i;\n", "int i;", Style); |
28385 | |
28386 | constexpr StringRef Code{"namespace {\n" |
28387 | "int i;\n" |
28388 | "} // namespace"}; |
28389 | verifyFormat(Code.str() + '\n', Code, Style, |
28390 | {tooling::Range(19, 13)}); // line 3 |
28391 | } |
28392 | |
28393 | TEST_F(FormatTest, KeepEmptyLinesAtEOF) { |
28394 | FormatStyle Style = getLLVMStyle(); |
28395 | Style.KeepEmptyLines.AtEndOfFile = true; |
28396 | |
28397 | const StringRef Code{"int i;\n\n"}; |
28398 | verifyNoChange(Code, Style); |
28399 | verifyFormat(Code, "int i;\n\n\n", Style); |
28400 | } |
28401 | |
28402 | TEST_F(FormatTest, SpaceAfterUDL) { |
28403 | verifyFormat("auto c = (4s).count();"); |
28404 | verifyFormat("auto x = 5s .count() == 5;"); |
28405 | } |
28406 | |
28407 | TEST_F(FormatTest, InterfaceAsClassMemberName) { |
28408 | verifyFormat("class Foo {\n" |
28409 | " int interface;\n" |
28410 | " Foo::Foo(int iface) : interface{iface} {}\n" |
28411 | "}"); |
28412 | } |
28413 | |
28414 | TEST_F(FormatTest, PreprocessorOverlappingRegions) { |
28415 | verifyFormat("#ifdef\n\n" |
28416 | "#else\n" |
28417 | "#endif", |
28418 | "#ifdef \n" |
28419 | " \n" |
28420 | "\n" |
28421 | "#else \n" |
28422 | "#endif ", |
28423 | getGoogleStyle()); |
28424 | } |
28425 | |
28426 | TEST_F(FormatTest, RemoveParentheses) { |
28427 | FormatStyle Style = getLLVMStyle(); |
28428 | EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave); |
28429 | |
28430 | Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses; |
28431 | verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style); |
28432 | verifyFormat("int x __attribute__((aligned(16))) = 0;", Style); |
28433 | verifyFormat("decltype((foo->bar)) baz;", Style); |
28434 | verifyFormat("class __declspec(dllimport) X {};", |
28435 | "class __declspec((dllimport)) X {};", Style); |
28436 | verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style); |
28437 | verifyFormat("while (a)\n" |
28438 | " b;", |
28439 | "while (((a)))\n" |
28440 | " b;", |
28441 | Style); |
28442 | verifyFormat("while ((a = b))\n" |
28443 | " c;", |
28444 | "while (((a = b)))\n" |
28445 | " c;", |
28446 | Style); |
28447 | verifyFormat("if (a)\n" |
28448 | " b;", |
28449 | "if (((a)))\n" |
28450 | " b;", |
28451 | Style); |
28452 | verifyFormat("if constexpr ((a = b))\n" |
28453 | " c;", |
28454 | "if constexpr (((a = b)))\n" |
28455 | " c;", |
28456 | Style); |
28457 | verifyFormat("if (({ a; }))\n" |
28458 | " b;", |
28459 | "if ((({ a; })))\n" |
28460 | " b;", |
28461 | Style); |
28462 | verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));", |
28463 | "static_assert(((std::is_constructible_v<T, Args &&> && ...)));", |
28464 | Style); |
28465 | verifyFormat("foo((a, b));", "foo(((a, b)));", Style); |
28466 | verifyFormat("foo((a, b));", "foo(((a), b));", Style); |
28467 | verifyFormat("foo((a, b));", "foo((a, (b)));", Style); |
28468 | verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style); |
28469 | verifyFormat("(..., (hash_a = hash_combine(hash_a, hash_b)));", |
28470 | "(..., ((hash_a = hash_combine(hash_a, hash_b))));", Style); |
28471 | verifyFormat("((hash_a = hash_combine(hash_a, hash_b)), ...);", |
28472 | "(((hash_a = hash_combine(hash_a, hash_b))), ...);", Style); |
28473 | verifyFormat("return (0);", "return (((0)));", Style); |
28474 | verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style); |
28475 | verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));", |
28476 | "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", |
28477 | Style); |
28478 | verifyFormat("MOCK_METHOD(void, Function, (), override);", |
28479 | "MOCK_METHOD(void, Function, (), (override));", Style); |
28480 | |
28481 | Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement; |
28482 | verifyFormat("#define Return0 return (0);", Style); |
28483 | verifyFormat("return 0;", "return (0);", Style); |
28484 | verifyFormat("co_return 0;", "co_return ((0));", Style); |
28485 | verifyFormat("return 0;", "return (((0)));", Style); |
28486 | verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style); |
28487 | verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);", |
28488 | "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", |
28489 | Style); |
28490 | verifyFormat("inline decltype(auto) f() {\n" |
28491 | " if (a) {\n" |
28492 | " return (a);\n" |
28493 | " }\n" |
28494 | " return (b);\n" |
28495 | "}", |
28496 | "inline decltype(auto) f() {\n" |
28497 | " if (a) {\n" |
28498 | " return ((a));\n" |
28499 | " }\n" |
28500 | " return ((b));\n" |
28501 | "}", |
28502 | Style); |
28503 | verifyFormat("auto g() {\n" |
28504 | " decltype(auto) x = [] {\n" |
28505 | " auto y = [] {\n" |
28506 | " if (a) {\n" |
28507 | " return a;\n" |
28508 | " }\n" |
28509 | " return b;\n" |
28510 | " };\n" |
28511 | " if (c) {\n" |
28512 | " return (c);\n" |
28513 | " }\n" |
28514 | " return (d);\n" |
28515 | " };\n" |
28516 | " if (e) {\n" |
28517 | " return e;\n" |
28518 | " }\n" |
28519 | " return f;\n" |
28520 | "}", |
28521 | "auto g() {\n" |
28522 | " decltype(auto) x = [] {\n" |
28523 | " auto y = [] {\n" |
28524 | " if (a) {\n" |
28525 | " return ((a));\n" |
28526 | " }\n" |
28527 | " return ((b));\n" |
28528 | " };\n" |
28529 | " if (c) {\n" |
28530 | " return ((c));\n" |
28531 | " }\n" |
28532 | " return ((d));\n" |
28533 | " };\n" |
28534 | " if (e) {\n" |
28535 | " return ((e));\n" |
28536 | " }\n" |
28537 | " return ((f));\n" |
28538 | "}", |
28539 | Style); |
28540 | |
28541 | Style.ColumnLimit = 25; |
28542 | verifyFormat("return (a + b) - (c + d);", |
28543 | "return (((a + b)) -\n" |
28544 | " ((c + d)));", |
28545 | Style); |
28546 | } |
28547 | |
28548 | TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) { |
28549 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 35); |
28550 | |
28551 | EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never); |
28552 | verifyFormat("void foo(int arg1,\n" |
28553 | " double arg2) noexcept;", |
28554 | Style); |
28555 | |
28556 | // The following line does not fit within the 35 column limit, but that's what |
28557 | // happens with no break allowed. |
28558 | verifyFormat("void bar(int arg1, double arg2) noexcept(\n" |
28559 | " noexcept(baz(arg1)) &&\n" |
28560 | " noexcept(baz(arg2)));", |
28561 | Style); |
28562 | |
28563 | verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;", |
28564 | Style); |
28565 | |
28566 | Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always; |
28567 | verifyFormat("void foo(int arg1,\n" |
28568 | " double arg2) noexcept;", |
28569 | Style); |
28570 | |
28571 | verifyFormat("void bar(int arg1, double arg2)\n" |
28572 | " noexcept(noexcept(baz(arg1)) &&\n" |
28573 | " noexcept(baz(arg2)));", |
28574 | Style); |
28575 | |
28576 | verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n" |
28577 | " noexcept;", |
28578 | Style); |
28579 | |
28580 | Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen; |
28581 | verifyFormat("void foo(int arg1,\n" |
28582 | " double arg2) noexcept;", |
28583 | Style); |
28584 | |
28585 | verifyFormat("void bar(int arg1, double arg2)\n" |
28586 | " noexcept(noexcept(baz(arg1)) &&\n" |
28587 | " noexcept(baz(arg2)));", |
28588 | Style); |
28589 | |
28590 | verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;", |
28591 | Style); |
28592 | } |
28593 | |
28594 | TEST_F(FormatTest, PPBranchesInBracedInit) { |
28595 | verifyFormat("A a_{kFlag1,\n" |
28596 | "#if BUILD_FLAG\n" |
28597 | " kFlag2,\n" |
28598 | "#else\n" |
28599 | " kFlag3,\n" |
28600 | "#endif\n" |
28601 | " kFlag4};", |
28602 | "A a_{\n" |
28603 | " kFlag1,\n" |
28604 | "#if BUILD_FLAG\n" |
28605 | " kFlag2,\n" |
28606 | "#else\n" |
28607 | " kFlag3,\n" |
28608 | "#endif\n" |
28609 | " kFlag4\n" |
28610 | "};"); |
28611 | } |
28612 | |
28613 | TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) { |
28614 | verifyFormat("{\n" |
28615 | " char *a[] = {\n" |
28616 | " /* abc */ \"abc\",\n" |
28617 | "#if FOO\n" |
28618 | " /* xyz */ \"xyz\",\n" |
28619 | "#endif\n" |
28620 | " /* last */ \"last\"};\n" |
28621 | "}", |
28622 | getLLVMStyleWithColumns(30)); |
28623 | } |
28624 | |
28625 | TEST_F(FormatTest, BreakAdjacentStringLiterals) { |
28626 | constexpr StringRef Code{ |
28627 | "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"}; |
28628 | |
28629 | verifyFormat("return \"Code\"\n" |
28630 | " \"\\0\\52\\26\\55\\55\\0\"\n" |
28631 | " \"x013\"\n" |
28632 | " \"\\02\\xBA\";", |
28633 | Code); |
28634 | |
28635 | auto Style = getLLVMStyle(); |
28636 | Style.BreakAdjacentStringLiterals = false; |
28637 | verifyFormat(Code, Style); |
28638 | } |
28639 | |
28640 | TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) { |
28641 | verifyFormat( |
28642 | "int rus; // А теперь комментарии, например, на русском, 2-байта\n" |
28643 | "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n" |
28644 | " // уже отодвинут. Перенос, при этом, отрабатывает верно"); |
28645 | |
28646 | auto Style = getLLVMStyle(); |
28647 | Style.ColumnLimit = 15; |
28648 | verifyNoChange("#define test \\\n" |
28649 | " /* 测试 */ \\\n" |
28650 | " \"aa\" \\\n" |
28651 | " \"bb\"", |
28652 | Style); |
28653 | |
28654 | Style.ColumnLimit = 25; |
28655 | verifyFormat("struct foo {\n" |
28656 | " int iiiiii; ///< iiiiii\n" |
28657 | " int b; ///< ыыы\n" |
28658 | " int c; ///< ыыыы\n" |
28659 | "};", |
28660 | Style); |
28661 | |
28662 | Style.ColumnLimit = 35; |
28663 | verifyFormat("#define SENSOR_DESC_1 \\\n" |
28664 | " \"{\" \\\n" |
28665 | " \"unit_of_measurement: \\\"°C\\\",\" \\\n" |
28666 | " \"}\"", |
28667 | Style); |
28668 | |
28669 | Style.ColumnLimit = 80; |
28670 | Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; |
28671 | verifyFormat("Languages languages = {\n" |
28672 | " Language{{'e', 'n'}, U\"Test English\" },\n" |
28673 | " Language{{'l', 'v'}, U\"Test Latviešu\"},\n" |
28674 | " Language{{'r', 'u'}, U\"Test Русский\" },\n" |
28675 | "};", |
28676 | Style); |
28677 | } |
28678 | |
28679 | TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) { |
28680 | verifyFormat("return .5;"); |
28681 | verifyFormat("return not '5';"); |
28682 | verifyFormat("return sizeof \"5\";"); |
28683 | } |
28684 | |
28685 | TEST_F(FormatTest, BreakBinaryOperations) { |
28686 | auto Style = getLLVMStyleWithColumns(ColumnLimit: 60); |
28687 | EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never); |
28688 | |
28689 | // Logical operations |
28690 | verifyFormat("if (condition1 && condition2) {\n" |
28691 | "}", |
28692 | Style); |
28693 | |
28694 | verifyFormat("if (condition1 && condition2 &&\n" |
28695 | " (condition3 || condition4) && condition5 &&\n" |
28696 | " condition6) {\n" |
28697 | "}", |
28698 | Style); |
28699 | |
28700 | verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n" |
28701 | " loooooooooooooooooooooongcondition2) {\n" |
28702 | "}", |
28703 | Style); |
28704 | |
28705 | // Arithmetic |
28706 | verifyFormat("const int result = lhs + rhs;", Style); |
28707 | |
28708 | verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n" |
28709 | " loooooooooooooooooooooongop3;", |
28710 | Style); |
28711 | |
28712 | verifyFormat("result = longOperand1 + longOperand2 -\n" |
28713 | " (longOperand3 + longOperand4) -\n" |
28714 | " longOperand5 * longOperand6;", |
28715 | Style); |
28716 | |
28717 | verifyFormat("const int result =\n" |
28718 | " operand1 + operand2 - (operand3 + operand4);", |
28719 | Style); |
28720 | |
28721 | // Check operator>> special case. |
28722 | verifyFormat("std::cin >> longOperand_1 >> longOperand_2 >>\n" |
28723 | " longOperand_3_;", |
28724 | Style); |
28725 | |
28726 | Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine; |
28727 | |
28728 | // Logical operations |
28729 | verifyFormat("if (condition1 && condition2) {\n" |
28730 | "}", |
28731 | Style); |
28732 | |
28733 | verifyFormat("if (condition1 && // comment\n" |
28734 | " condition2 &&\n" |
28735 | " (condition3 || condition4) && // comment\n" |
28736 | " condition5 &&\n" |
28737 | " condition6) {\n" |
28738 | "}", |
28739 | Style); |
28740 | |
28741 | verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n" |
28742 | " loooooooooooooooooooooongcondition2) {\n" |
28743 | "}", |
28744 | Style); |
28745 | |
28746 | // Arithmetic |
28747 | verifyFormat("const int result = lhs + rhs;", Style); |
28748 | |
28749 | verifyFormat("result = loooooooooooooooooooooongop1 +\n" |
28750 | " loooooooooooooooooooooongop2 +\n" |
28751 | " loooooooooooooooooooooongop3;", |
28752 | Style); |
28753 | |
28754 | verifyFormat("const int result =\n" |
28755 | " operand1 + operand2 - (operand3 + operand4);", |
28756 | Style); |
28757 | |
28758 | verifyFormat("result = longOperand1 +\n" |
28759 | " longOperand2 -\n" |
28760 | " (longOperand3 + longOperand4) -\n" |
28761 | " longOperand5 +\n" |
28762 | " longOperand6;", |
28763 | Style); |
28764 | |
28765 | verifyFormat("result = operand1 +\n" |
28766 | " operand2 -\n" |
28767 | " operand3 +\n" |
28768 | " operand4 -\n" |
28769 | " operand5 +\n" |
28770 | " operand6;", |
28771 | Style); |
28772 | |
28773 | // Ensure mixed precedence operations are handled properly |
28774 | verifyFormat("result = op1 + op2 * op3 - op4;", Style); |
28775 | |
28776 | verifyFormat("result = operand1 +\n" |
28777 | " operand2 /\n" |
28778 | " operand3 +\n" |
28779 | " operand4 /\n" |
28780 | " operand5 *\n" |
28781 | " operand6;", |
28782 | Style); |
28783 | |
28784 | verifyFormat("result = operand1 *\n" |
28785 | " operand2 -\n" |
28786 | " operand3 *\n" |
28787 | " operand4 -\n" |
28788 | " operand5 +\n" |
28789 | " operand6;", |
28790 | Style); |
28791 | |
28792 | verifyFormat("result = operand1 *\n" |
28793 | " (operand2 - operand3 * operand4) -\n" |
28794 | " operand5 +\n" |
28795 | " operand6;", |
28796 | Style); |
28797 | |
28798 | verifyFormat("result = operand1.member *\n" |
28799 | " (operand2.member() - operand3->mem * operand4) -\n" |
28800 | " operand5.member() +\n" |
28801 | " operand6->member;", |
28802 | Style); |
28803 | |
28804 | // Check operator>> special case. |
28805 | verifyFormat("std::cin >>\n" |
28806 | " longOperand_1 >>\n" |
28807 | " longOperand_2 >>\n" |
28808 | " longOperand_3_;", |
28809 | Style); |
28810 | |
28811 | Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence; |
28812 | verifyFormat("result = op1 + op2 * op3 - op4;", Style); |
28813 | |
28814 | verifyFormat("result = operand1 +\n" |
28815 | " operand2 / operand3 +\n" |
28816 | " operand4 / operand5 * operand6;", |
28817 | Style); |
28818 | |
28819 | verifyFormat("result = operand1 * operand2 -\n" |
28820 | " operand3 * operand4 -\n" |
28821 | " operand5 +\n" |
28822 | " operand6;", |
28823 | Style); |
28824 | |
28825 | verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n" |
28826 | " operand5 +\n" |
28827 | " operand6;", |
28828 | Style); |
28829 | |
28830 | verifyFormat("std::uint32_t a = byte_buffer[0] |\n" |
28831 | " byte_buffer[1] << 8 |\n" |
28832 | " byte_buffer[2] << 16 |\n" |
28833 | " byte_buffer[3] << 24;", |
28834 | Style); |
28835 | |
28836 | // Check operator>> special case. |
28837 | verifyFormat("std::cin >>\n" |
28838 | " longOperand_1 >>\n" |
28839 | " longOperand_2 >>\n" |
28840 | " longOperand_3_;", |
28841 | Style); |
28842 | |
28843 | Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine; |
28844 | Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; |
28845 | |
28846 | // Logical operations |
28847 | verifyFormat("if (condition1 && condition2) {\n" |
28848 | "}", |
28849 | Style); |
28850 | |
28851 | verifyFormat("if (loooooooooooooooooooooongcondition1\n" |
28852 | " && loooooooooooooooooooooongcondition2) {\n" |
28853 | "}", |
28854 | Style); |
28855 | |
28856 | // Arithmetic |
28857 | verifyFormat("const int result = lhs + rhs;", Style); |
28858 | |
28859 | verifyFormat("result = loooooooooooooooooooooongop1\n" |
28860 | " + loooooooooooooooooooooongop2\n" |
28861 | " + loooooooooooooooooooooongop3;", |
28862 | Style); |
28863 | |
28864 | verifyFormat("const int result =\n" |
28865 | " operand1 + operand2 - (operand3 + operand4);", |
28866 | Style); |
28867 | |
28868 | verifyFormat("result = longOperand1\n" |
28869 | " + longOperand2\n" |
28870 | " - (longOperand3 + longOperand4)\n" |
28871 | " - longOperand5\n" |
28872 | " + longOperand6;", |
28873 | Style); |
28874 | |
28875 | verifyFormat("result = operand1\n" |
28876 | " + operand2\n" |
28877 | " - operand3\n" |
28878 | " + operand4\n" |
28879 | " - operand5\n" |
28880 | " + operand6;", |
28881 | Style); |
28882 | |
28883 | // Ensure mixed precedence operations are handled properly |
28884 | verifyFormat("result = op1 + op2 * op3 - op4;", Style); |
28885 | |
28886 | verifyFormat("result = operand1\n" |
28887 | " + operand2\n" |
28888 | " / operand3\n" |
28889 | " + operand4\n" |
28890 | " / operand5\n" |
28891 | " * operand6;", |
28892 | Style); |
28893 | |
28894 | verifyFormat("result = operand1\n" |
28895 | " * operand2\n" |
28896 | " - operand3\n" |
28897 | " * operand4\n" |
28898 | " - operand5\n" |
28899 | " + operand6;", |
28900 | Style); |
28901 | |
28902 | verifyFormat("result = operand1\n" |
28903 | " * (operand2 - operand3 * operand4)\n" |
28904 | " - operand5\n" |
28905 | " + operand6;", |
28906 | Style); |
28907 | |
28908 | verifyFormat("std::uint32_t a = byte_buffer[0]\n" |
28909 | " | byte_buffer[1]\n" |
28910 | " << 8\n" |
28911 | " | byte_buffer[2]\n" |
28912 | " << 16\n" |
28913 | " | byte_buffer[3]\n" |
28914 | " << 24;", |
28915 | Style); |
28916 | |
28917 | // Check operator>> special case. |
28918 | verifyFormat("std::cin\n" |
28919 | " >> longOperand_1\n" |
28920 | " >> longOperand_2\n" |
28921 | " >> longOperand_3_;", |
28922 | Style); |
28923 | |
28924 | Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence; |
28925 | verifyFormat("result = op1 + op2 * op3 - op4;", Style); |
28926 | |
28927 | verifyFormat("result = operand1\n" |
28928 | " + operand2 / operand3\n" |
28929 | " + operand4 / operand5 * operand6;", |
28930 | Style); |
28931 | |
28932 | verifyFormat("result = operand1 * operand2\n" |
28933 | " - operand3 * operand4\n" |
28934 | " - operand5\n" |
28935 | " + operand6;", |
28936 | Style); |
28937 | |
28938 | verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n" |
28939 | " - operand5\n" |
28940 | " + operand6;", |
28941 | Style); |
28942 | |
28943 | verifyFormat("std::uint32_t a = byte_buffer[0]\n" |
28944 | " | byte_buffer[1] << 8\n" |
28945 | " | byte_buffer[2] << 16\n" |
28946 | " | byte_buffer[3] << 24;", |
28947 | Style); |
28948 | |
28949 | // Check operator>> special case. |
28950 | verifyFormat("std::cin\n" |
28951 | " >> longOperand_1\n" |
28952 | " >> longOperand_2\n" |
28953 | " >> longOperand_3_;", |
28954 | Style); |
28955 | } |
28956 | |
28957 | TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) { |
28958 | auto Style = getLLVMStyle(); |
28959 | Style.RemoveEmptyLinesInUnwrappedLines = true; |
28960 | |
28961 | verifyFormat("int c = a + b;", |
28962 | "int c\n" |
28963 | "\n" |
28964 | " = a + b;", |
28965 | Style); |
28966 | |
28967 | verifyFormat("enum : unsigned { AA = 0, BB } myEnum;", |
28968 | "enum : unsigned\n" |
28969 | "\n" |
28970 | "{\n" |
28971 | " AA = 0,\n" |
28972 | " BB\n" |
28973 | "} myEnum;", |
28974 | Style); |
28975 | |
28976 | verifyFormat("class B : public E {\n" |
28977 | "private:\n" |
28978 | "};", |
28979 | "class B : public E\n" |
28980 | "\n" |
28981 | "{\n" |
28982 | "private:\n" |
28983 | "};", |
28984 | Style); |
28985 | |
28986 | verifyFormat( |
28987 | "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};", |
28988 | "struct AAAAAAAAAAAAAAA test[3] = {{56,\n" |
28989 | "\n" |
28990 | " 23, \"hello\"},\n" |
28991 | " {7, 5, \"!!\"}};", |
28992 | Style); |
28993 | |
28994 | verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);", |
28995 | "int myFunction(\n" |
28996 | "\n" |
28997 | " int aaaaaaaaaaaaa,\n" |
28998 | "\n" |
28999 | " int ccccccccccccc, int d);", |
29000 | Style); |
29001 | |
29002 | verifyFormat("switch (e) {\n" |
29003 | "case 1:\n" |
29004 | " return e;\n" |
29005 | "case 2:\n" |
29006 | " return 2;\n" |
29007 | "}", |
29008 | "switch (\n" |
29009 | "\n" |
29010 | " e) {\n" |
29011 | "case 1:\n" |
29012 | " return e;\n" |
29013 | "case 2:\n" |
29014 | " return 2;\n" |
29015 | "}", |
29016 | Style); |
29017 | |
29018 | verifyFormat("while (true) {\n" |
29019 | "}", |
29020 | "while (\n" |
29021 | "\n" |
29022 | " true) {\n" |
29023 | "}", |
29024 | Style); |
29025 | |
29026 | verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n" |
29027 | " std::map<int, std::string> *outputMap);", |
29028 | "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n" |
29029 | "\n" |
29030 | " (std::map<int, std::string> *outputMap);", |
29031 | Style); |
29032 | } |
29033 | |
29034 | TEST_F(FormatTest, KeepFormFeed) { |
29035 | auto Style = getLLVMStyle(); |
29036 | Style.KeepFormFeed = true; |
29037 | |
29038 | constexpr StringRef NoFormFeed{"int i;\n" |
29039 | "\n" |
29040 | "void f();"}; |
29041 | verifyFormat(NoFormFeed, |
29042 | "int i;\n" |
29043 | " \f\n" |
29044 | "void f();", |
29045 | Style); |
29046 | verifyFormat(NoFormFeed, |
29047 | "int i;\n" |
29048 | "\n" |
29049 | "\fvoid f();", |
29050 | Style); |
29051 | verifyFormat(NoFormFeed, |
29052 | "\fint i;\n" |
29053 | "\n" |
29054 | "void f();", |
29055 | Style); |
29056 | verifyFormat(NoFormFeed, |
29057 | "int i;\n" |
29058 | "\n" |
29059 | "void f();\f", |
29060 | Style); |
29061 | |
29062 | constexpr StringRef FormFeed{"int i;\n" |
29063 | "\f\n" |
29064 | "void f();"}; |
29065 | verifyNoChange(FormFeed, Style); |
29066 | |
29067 | Style.LineEnding = FormatStyle::LE_LF; |
29068 | verifyFormat(FormFeed, |
29069 | "int i;\r\n" |
29070 | "\f\r\n" |
29071 | "void f();", |
29072 | Style); |
29073 | |
29074 | constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n" |
29075 | "\f\n" |
29076 | "\n" |
29077 | "void f();"}; |
29078 | Style.MaxEmptyLinesToKeep = 2; |
29079 | verifyFormat(FormFeedBeforeEmptyLine, |
29080 | "int i;\n" |
29081 | "\n" |
29082 | "\f\n" |
29083 | "void f();", |
29084 | Style); |
29085 | verifyFormat(FormFeedBeforeEmptyLine, |
29086 | "int i;\n" |
29087 | "\f\n" |
29088 | "\f\n" |
29089 | "void f();", |
29090 | Style); |
29091 | } |
29092 | |
29093 | TEST_F(FormatTest, ShortNamespacesOption) { |
29094 | auto Style = getLLVMStyle(); |
29095 | Style.AllowShortNamespacesOnASingleLine = true; |
29096 | Style.CompactNamespaces = true; |
29097 | Style.FixNamespaceComments = false; |
29098 | |
29099 | // Basic functionality. |
29100 | verifyFormat("namespace foo { class bar; }", Style); |
29101 | verifyFormat("namespace foo::bar { class baz; }", Style); |
29102 | verifyFormat("namespace { class bar; }", Style); |
29103 | verifyFormat("namespace foo {\n" |
29104 | "class bar;\n" |
29105 | "class baz;\n" |
29106 | "}", |
29107 | Style); |
29108 | |
29109 | // Trailing comments prevent merging. |
29110 | verifyFormat("namespace foo { namespace baz {\n" |
29111 | "class qux;\n" |
29112 | "} // comment\n" |
29113 | "}", |
29114 | Style); |
29115 | |
29116 | // Make sure code doesn't walk too far on unbalanced code. |
29117 | verifyFormat("namespace foo {", Style); |
29118 | verifyFormat("namespace foo {\n" |
29119 | "class baz;", |
29120 | Style); |
29121 | verifyFormat("namespace foo {\n" |
29122 | "namespace bar { class baz; }", |
29123 | Style); |
29124 | |
29125 | // Nested namespaces. |
29126 | verifyFormat("namespace foo { namespace bar { class baz; } }", Style); |
29127 | |
29128 | // Without CompactNamespaces, we won't merge consecutive namespace |
29129 | // declarations. |
29130 | Style.CompactNamespaces = false; |
29131 | verifyFormat("namespace foo {\n" |
29132 | "namespace bar { class baz; }\n" |
29133 | "}", |
29134 | Style); |
29135 | |
29136 | verifyFormat("namespace foo {\n" |
29137 | "namespace bar { class baz; }\n" |
29138 | "namespace qux { class quux; }\n" |
29139 | "}", |
29140 | Style); |
29141 | |
29142 | Style.CompactNamespaces = true; |
29143 | |
29144 | // Varying inner content. |
29145 | verifyFormat("namespace foo {\n" |
29146 | "int f() { return 5; }\n" |
29147 | "}", |
29148 | Style); |
29149 | verifyFormat("namespace foo { template <T> struct bar; }", Style); |
29150 | verifyFormat("namespace foo { constexpr int num = 42; }", Style); |
29151 | |
29152 | // Validate nested namespace wrapping scenarios around the ColumnLimit. |
29153 | Style.ColumnLimit = 64; |
29154 | |
29155 | // Validate just under the ColumnLimit. |
29156 | verifyFormat( |
29157 | "namespace foo { namespace bar { namespace baz { class qux; } } }", |
29158 | Style); |
29159 | |
29160 | // Validate just over the ColumnLimit. |
29161 | verifyFormat("namespace foo { namespace baar { namespace baaz {\n" |
29162 | "class quux;\n" |
29163 | "}}}", |
29164 | Style); |
29165 | |
29166 | verifyFormat( |
29167 | "namespace foo { namespace bar { namespace baz { namespace qux {\n" |
29168 | "class quux;\n" |
29169 | "}}}}", |
29170 | Style); |
29171 | |
29172 | // Validate that the ColumnLimit logic accounts for trailing content as well. |
29173 | verifyFormat("namespace foo { namespace bar { class qux; } } // extra", |
29174 | Style); |
29175 | |
29176 | verifyFormat("namespace foo { namespace bar { namespace baz {\n" |
29177 | "class qux;\n" |
29178 | "}}} // extra", |
29179 | Style); |
29180 | |
29181 | // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing |
29182 | // namespace comment from 'FixNamespaceComments', as it's not really necessary |
29183 | // in this scenario, but the two options work at very different layers of the |
29184 | // formatter, so I'm not sure how to make them interact. |
29185 | // |
29186 | // As it stands, the trailing comment will be added and likely make the line |
29187 | // too long to fit within the ColumnLimit, reducing the how likely the line |
29188 | // will still fit on a single line. The recommendation for now is to use the |
29189 | // concatenated namespace syntax instead. e.g. 'namespace foo::bar' |
29190 | Style.FixNamespaceComments = true; |
29191 | verifyFormat( |
29192 | "namespace foo { namespace bar { namespace baz {\n" |
29193 | "class qux;\n" |
29194 | "}}} // namespace foo::bar::baz", |
29195 | "namespace foo { namespace bar { namespace baz { class qux; } } }", |
29196 | Style); |
29197 | Style.FixNamespaceComments = false; |
29198 | |
29199 | Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
29200 | Style.BraceWrapping.AfterNamespace = true; |
29201 | verifyFormat("namespace foo { class bar; }", Style); |
29202 | verifyFormat("namespace foo { namespace bar { class baz; } }", Style); |
29203 | verifyFormat("namespace foo\n" |
29204 | "{ // comment\n" |
29205 | "class bar;\n" |
29206 | "}", |
29207 | Style); |
29208 | verifyFormat("namespace foo { class bar; }", |
29209 | "namespace foo {\n" |
29210 | "class bar;\n" |
29211 | "}", |
29212 | Style); |
29213 | verifyFormat("namespace foo\n" |
29214 | "{\n" |
29215 | "namespace bar\n" |
29216 | "{ // comment\n" |
29217 | "class baz;\n" |
29218 | "}\n" |
29219 | "}", |
29220 | Style); |
29221 | verifyFormat("namespace foo // comment\n" |
29222 | "{\n" |
29223 | "class baz;\n" |
29224 | "}", |
29225 | Style); |
29226 | } |
29227 | |
29228 | TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { |
29229 | auto Style = getLLVMStyle(); |
29230 | Style.FixNamespaceComments = false; |
29231 | Style.MaxEmptyLinesToKeep = 2; |
29232 | Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; |
29233 | |
29234 | // Empty namespace. |
29235 | verifyFormat("namespace N {}", Style); |
29236 | |
29237 | // Single namespace. |
29238 | verifyFormat("namespace N {\n" |
29239 | "int f1(int a) { return 2 * a; }\n" |
29240 | "}", |
29241 | "namespace N {\n" |
29242 | "\n" |
29243 | "\n" |
29244 | "int f1(int a) { return 2 * a; }\n" |
29245 | "\n" |
29246 | "\n" |
29247 | "}", |
29248 | Style); |
29249 | |
29250 | // Nested namespace. |
29251 | verifyFormat("namespace N1 {\n" |
29252 | "namespace N2 {\n" |
29253 | "int a = 1;\n" |
29254 | "}\n" |
29255 | "}", |
29256 | "namespace N1 {\n" |
29257 | "\n" |
29258 | "\n" |
29259 | "namespace N2 {\n" |
29260 | "\n" |
29261 | "int a = 1;\n" |
29262 | "\n" |
29263 | "}\n" |
29264 | "\n" |
29265 | "\n" |
29266 | "}", |
29267 | Style); |
29268 | |
29269 | Style.CompactNamespaces = true; |
29270 | |
29271 | verifyFormat("namespace N1 { namespace N2 {\n" |
29272 | "int a = 1;\n" |
29273 | "}}", |
29274 | "namespace N1 { namespace N2 {\n" |
29275 | "\n" |
29276 | "\n" |
29277 | "int a = 1;\n" |
29278 | "\n" |
29279 | "\n" |
29280 | "}}", |
29281 | Style); |
29282 | } |
29283 | |
29284 | TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { |
29285 | auto Style = getLLVMStyle(); |
29286 | Style.FixNamespaceComments = false; |
29287 | Style.MaxEmptyLinesToKeep = 2; |
29288 | Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; |
29289 | |
29290 | // Empty namespace. |
29291 | verifyFormat("namespace N {}", Style); |
29292 | |
29293 | // Single namespace. |
29294 | verifyFormat("namespace N {\n" |
29295 | "\n" |
29296 | "int f1(int a) { return 2 * a; }\n" |
29297 | "\n" |
29298 | "}", |
29299 | "namespace N {\n" |
29300 | "int f1(int a) { return 2 * a; }\n" |
29301 | "}", |
29302 | Style); |
29303 | |
29304 | // Nested namespace. |
29305 | verifyFormat("namespace N1 {\n" |
29306 | "namespace N2 {\n" |
29307 | "\n" |
29308 | "int a = 1;\n" |
29309 | "\n" |
29310 | "}\n" |
29311 | "}", |
29312 | "namespace N1 {\n" |
29313 | "namespace N2 {\n" |
29314 | "int a = 1;\n" |
29315 | "}\n" |
29316 | "}", |
29317 | Style); |
29318 | |
29319 | verifyFormat("namespace N1 {\n" |
29320 | "\n" |
29321 | "namespace N2 {\n" |
29322 | "\n" |
29323 | "\n" |
29324 | "int a = 1;\n" |
29325 | "\n" |
29326 | "\n" |
29327 | "}\n" |
29328 | "\n" |
29329 | "}", |
29330 | "namespace N1 {\n" |
29331 | "\n" |
29332 | "namespace N2 {\n" |
29333 | "\n" |
29334 | "\n" |
29335 | "\n" |
29336 | "int a = 1;\n" |
29337 | "\n" |
29338 | "\n" |
29339 | "\n" |
29340 | "}\n" |
29341 | "\n" |
29342 | "}", |
29343 | Style); |
29344 | |
29345 | Style.CompactNamespaces = true; |
29346 | |
29347 | verifyFormat("namespace N1 { namespace N2 {\n" |
29348 | "\n" |
29349 | "int a = 1;\n" |
29350 | "\n" |
29351 | "}}", |
29352 | "namespace N1 { namespace N2 {\n" |
29353 | "int a = 1;\n" |
29354 | "}}", |
29355 | Style); |
29356 | } |
29357 | |
29358 | TEST_F(FormatTest, BreakBeforeClassName) { |
29359 | verifyFormat("class ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_NULLABILITY_COMPATIBLE\n" |
29360 | " ArenaSafeUniquePtr {};"); |
29361 | } |
29362 | |
29363 | } // namespace |
29364 | } // namespace test |
29365 | } // namespace format |
29366 | } // namespace clang |
29367 |