Debian Codegraph @

clang/unittests/Format/FormatTest.cpp

raw
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("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("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("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("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("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("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(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 Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2088 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2089 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2090 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2091 verifyFormat("int* f1(int& a) const& = 0;", Style);
2092 verifyFormat("int* a = f1();", Style);
2093 verifyFormat("int& b = f2();", Style);
2094 verifyFormat("int&& c = f3();", Style);
2095 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2096 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2097 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2098 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2099 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2100 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2101 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2102 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2103 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2104 verifyFormat("for (int a = 0, b = 0; const int& 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 = 0; const Foo* c : {1, 2, 3})", Style);
2107 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2108 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2109 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2110 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2111 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2112 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2113 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2114 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2115 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2116 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2117 verifyFormat(
2118 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2119 " res2 = [](int& a) { return 0000000000000; };",
2120 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 = false;
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("LIKELY");
2688 Attributes.AttributeMacros.push_back("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 FormatStyle Style = getLLVMStyle();
2870 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2871 verifyFormat("template <typename T>\n"
2872 "concept c = requires(T x) {\n"
2873 " { x + 1 } -> std::same_as<int>;\n"
2874 "};",
2875 Style);
2876 verifyFormat("template <typename T>\n"
2877 "concept c = requires(T x) {\n"
2878 " { x + 1 } -> std::same_as<int>;\n"
2879 " { x + 2 } -> std::same_as<int>;\n"
2880 "};",
2881 Style);
2882 Style.AllowShortCompoundRequirementOnASingleLine = false;
2883 verifyFormat("template <typename T>\n"
2884 "concept c = requires(T x) {\n"
2885 " {\n"
2886 " x + 1\n"
2887 " } -> std::same_as<int>;\n"
2888 "};",
2889 Style);
2890 verifyFormat("template <typename T>\n"
2891 "concept c = requires(T x) {\n"
2892 " {\n"
2893 " x + 1\n"
2894 " } -> std::same_as<int>;\n"
2895 " {\n"
2896 " x + 2\n"
2897 " } -> std::same_as<int>;\n"
2898 "};",
2899 Style);
2900 }
2901
2902 TEST_F(FormatTest, ShortCaseLabels) {
2903 FormatStyle Style = getLLVMStyle();
2904 Style.AllowShortCaseLabelsOnASingleLine = true;
2905 verifyFormat("switch (a) {\n"
2906 "case 1: x = 1; break;\n"
2907 "case 2: return;\n"
2908 "case 3:\n"
2909 "case 4:\n"
2910 "case 5: return;\n"
2911 "case 6: // comment\n"
2912 " return;\n"
2913 "case 7:\n"
2914 " // comment\n"
2915 " return;\n"
2916 "case 8:\n"
2917 " x = 8; // comment\n"
2918 " break;\n"
2919 "default: y = 1; break;\n"
2920 "}",
2921 Style);
2922 verifyFormat("switch (a) {\n"
2923 "case 0: return; // comment\n"
2924 "case 1: break; // comment\n"
2925 "case 2: return;\n"
2926 "// comment\n"
2927 "case 3: return;\n"
2928 "// comment 1\n"
2929 "// comment 2\n"
2930 "// comment 3\n"
2931 "case 4: break; /* comment */\n"
2932 "case 5:\n"
2933 " // comment\n"
2934 " break;\n"
2935 "case 6: /* comment */ x = 1; break;\n"
2936 "case 7: x = /* comment */ 1; break;\n"
2937 "case 8:\n"
2938 " x = 1; /* comment */\n"
2939 " break;\n"
2940 "case 9:\n"
2941 " break; // comment line 1\n"
2942 " // comment line 2\n"
2943 "}",
2944 Style);
2945 verifyFormat("switch (a) {\n"
2946 "case 1:\n"
2947 " x = 8;\n"
2948 " // fall through\n"
2949 "case 2: x = 8;\n"
2950 "// comment\n"
2951 "case 3:\n"
2952 " return; /* comment line 1\n"
2953 " * comment line 2 */\n"
2954 "case 4: i = 8;\n"
2955 "// something else\n"
2956 "#if FOO\n"
2957 "case 5: break;\n"
2958 "#endif\n"
2959 "}",
2960 "switch (a) {\n"
2961 "case 1: x = 8;\n"
2962 " // fall through\n"
2963 "case 2:\n"
2964 " x = 8;\n"
2965 "// comment\n"
2966 "case 3:\n"
2967 " return; /* comment line 1\n"
2968 " * comment line 2 */\n"
2969 "case 4:\n"
2970 " i = 8;\n"
2971 "// something else\n"
2972 "#if FOO\n"
2973 "case 5: break;\n"
2974 "#endif\n"
2975 "}",
2976 Style);
2977 verifyFormat("switch (a) {\n"
2978 "case 0:\n"
2979 " return; // long long long long long long long long long long "
2980 "long long comment\n"
2981 " // line\n"
2982 "}",
2983 "switch (a) {\n"
2984 "case 0: return; // long long long long long long long long "
2985 "long long long long comment line\n"
2986 "}",
2987 Style);
2988 verifyFormat("switch (a) {\n"
2989 "case 0:\n"
2990 " return; /* long long long long long long long long long long "
2991 "long long comment\n"
2992 " line */\n"
2993 "}",
2994 "switch (a) {\n"
2995 "case 0: return; /* long long long long long long long long "
2996 "long long long long comment line */\n"
2997 "}",
2998 Style);
2999 verifyFormat("switch (a) {\n"
3000 "#if FOO\n"
3001 "case 0: return 0;\n"
3002 "#endif\n"
3003 "}",
3004 Style);
3005 verifyFormat("switch (a) {\n"
3006 "case 1: {\n"
3007 "}\n"
3008 "case 2: {\n"
3009 " return;\n"
3010 "}\n"
3011 "case 3: {\n"
3012 " x = 1;\n"
3013 " return;\n"
3014 "}\n"
3015 "case 4:\n"
3016 " if (x)\n"
3017 " return;\n"
3018 "}",
3019 Style);
3020 Style.ColumnLimit = 21;
3021 verifyFormat("#define X \\\n"
3022 " case 0: break;\n"
3023 "#include \"f\"",
3024 Style);
3025 verifyFormat("switch (a) {\n"
3026 "case 1: x = 1; break;\n"
3027 "case 2: return;\n"
3028 "case 3:\n"
3029 "case 4:\n"
3030 "case 5: return;\n"
3031 "default:\n"
3032 " y = 1;\n"
3033 " break;\n"
3034 "}",
3035 Style);
3036 Style.ColumnLimit = 80;
3037 Style.AllowShortCaseLabelsOnASingleLine = false;
3038 Style.IndentCaseLabels = true;
3039 verifyFormat("switch (n) {\n"
3040 " default /*comments*/:\n"
3041 " return true;\n"
3042 " case 0:\n"
3043 " return false;\n"
3044 "}",
3045 "switch (n) {\n"
3046 "default/*comments*/:\n"
3047 " return true;\n"
3048 "case 0:\n"
3049 " return false;\n"
3050 "}",
3051 Style);
3052 Style.AllowShortCaseLabelsOnASingleLine = true;
3053 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3054 Style.BraceWrapping.AfterCaseLabel = true;
3055 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3056 verifyFormat("switch (n)\n"
3057 "{\n"
3058 " case 0:\n"
3059 " {\n"
3060 " return false;\n"
3061 " }\n"
3062 " default:\n"
3063 " {\n"
3064 " return true;\n"
3065 " }\n"
3066 "}",
3067 "switch (n) {\n"
3068 " case 0: {\n"
3069 " return false;\n"
3070 " }\n"
3071 " default:\n"
3072 " {\n"
3073 " return true;\n"
3074 " }\n"
3075 "}",
3076 Style);
3077 }
3078
3079 TEST_F(FormatTest, FormatsLabels) {
3080 verifyFormat("void f() {\n"
3081 " some_code();\n"
3082 "test_label:\n"
3083 " some_other_code();\n"
3084 " {\n"
3085 " some_more_code();\n"
3086 " another_label:\n"
3087 " some_more_code();\n"
3088 " }\n"
3089 "}");
3090 verifyFormat("{\n"
3091 " some_code();\n"
3092 "test_label:\n"
3093 " some_other_code();\n"
3094 "}");
3095 verifyFormat("{\n"
3096 " some_code();\n"
3097 "test_label:;\n"
3098 " int i = 0;\n"
3099 "}");
3100 verifyFormat("{\n"
3101 " some_code();\n"
3102 "test_label: { some_other_code(); }\n"
3103 "}");
3104 verifyFormat("{\n"
3105 " some_code();\n"
3106 "test_label: {\n"
3107 " some_other_code();\n"
3108 " some_other_code();\n"
3109 "}\n"
3110 "}");
3111 verifyFormat("{\n"
3112 "L0:\n"
3113 "[[foo]] L1:\n"
3114 "[[bar]] [[baz]] L2:\n"
3115 " g();\n"
3116 "}");
3117 verifyFormat("{\n"
3118 "[[foo]] L1: {\n"
3119 "[[bar]] [[baz]] L2:\n"
3120 " g();\n"
3121 "}\n"
3122 "}");
3123 verifyFormat("{\n"
3124 "[[foo]] L1:\n"
3125 " f();\n"
3126 " {\n"
3127 " [[bar]] [[baz]] L2:\n"
3128 " g();\n"
3129 " }\n"
3130 "}");
3131
3132 FormatStyle Style = getLLVMStyle();
3133 Style.IndentGotoLabels = false;
3134 verifyFormat("void f() {\n"
3135 " some_code();\n"
3136 "test_label:\n"
3137 " some_other_code();\n"
3138 " {\n"
3139 " some_more_code();\n"
3140 "another_label:\n"
3141 " some_more_code();\n"
3142 " }\n"
3143 "}",
3144 Style);
3145 verifyFormat("{\n"
3146 " some_code();\n"
3147 "test_label:\n"
3148 " some_other_code();\n"
3149 "}",
3150 Style);
3151 verifyFormat("{\n"
3152 " some_code();\n"
3153 "test_label:;\n"
3154 " int i = 0;\n"
3155 "}",
3156 Style);
3157 verifyFormat("{\n"
3158 " some_code();\n"
3159 "test_label: { some_other_code(); }\n"
3160 "}",
3161 Style);
3162 verifyFormat("{\n"
3163 "[[foo]] L1:\n"
3164 " f();\n"
3165 " {\n"
3166 "[[bar]] [[baz]] L2:\n"
3167 " g();\n"
3168 " }\n"
3169 "}",
3170 Style);
3171
3172 Style.ColumnLimit = 15;
3173 verifyFormat("#define FOO \\\n"
3174 "label: \\\n"
3175 " break;",
3176 Style);
3177
3178 // The opening brace may either be on the same unwrapped line as the colon or
3179 // on a separate one. The formatter should recognize both.
3180 Style = getLLVMStyle();
3181 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman;
3182 verifyFormat("{\n"
3183 " some_code();\n"
3184 "test_label:\n"
3185 "{\n"
3186 " some_other_code();\n"
3187 "}\n"
3188 "}",
3189 Style);
3190 verifyFormat("{\n"
3191 "[[foo]] L1:\n"
3192 "{\n"
3193 "[[bar]] [[baz]] L2:\n"
3194 " g();\n"
3195 "}\n"
3196 "}",
3197 Style);
3198 }
3199
3200 TEST_F(FormatTest, MultiLineControlStatements) {
3201 FormatStyle Style = getLLVMStyleWithColumns(20);
3202 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3203 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3204 // Short lines should keep opening brace on same line.
3205 verifyFormat("if (foo) {\n"
3206 " bar();\n"
3207 "}",
3208 "if(foo){bar();}", Style);
3209 verifyFormat("if (foo) {\n"
3210 " bar();\n"
3211 "} else {\n"
3212 " baz();\n"
3213 "}",
3214 "if(foo){bar();}else{baz();}", Style);
3215 verifyFormat("if (foo && bar) {\n"
3216 " baz();\n"
3217 "}",
3218 "if(foo&&bar){baz();}", Style);
3219 verifyFormat("if (foo) {\n"
3220 " bar();\n"
3221 "} else if (baz) {\n"
3222 " quux();\n"
3223 "}",
3224 "if(foo){bar();}else if(baz){quux();}", Style);
3225 verifyFormat("if (foo) {\n"
3226 " bar();\n"
3227 "} else if (baz) {\n"
3228 " quux();\n"
3229 "} else {\n"
3230 " foobar();\n"
3231 "}",
3232 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3233 verifyFormat("for (;;) {\n"
3234 " foo();\n"
3235 "}",
3236 "for(;;){foo();}");
3237 verifyFormat("while (1) {\n"
3238 " foo();\n"
3239 "}",
3240 "while(1){foo();}", Style);
3241 verifyFormat("switch (foo) {\n"
3242 "case bar:\n"
3243 " return;\n"
3244 "}",
3245 "switch(foo){case bar:return;}", Style);
3246 verifyFormat("try {\n"
3247 " foo();\n"
3248 "} catch (...) {\n"
3249 " bar();\n"
3250 "}",
3251 "try{foo();}catch(...){bar();}", Style);
3252 verifyFormat("do {\n"
3253 " foo();\n"
3254 "} while (bar &&\n"
3255 " baz);",
3256 "do{foo();}while(bar&&baz);", Style);
3257 // Long lines should put opening brace on new line.
3258 verifyFormat("void f() {\n"
3259 " if (a1 && a2 &&\n"
3260 " a3)\n"
3261 " {\n"
3262 " quux();\n"
3263 " }\n"
3264 "}",
3265 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3266 verifyFormat("if (foo && bar &&\n"
3267 " baz)\n"
3268 "{\n"
3269 " quux();\n"
3270 "}",
3271 "if(foo&&bar&&baz){quux();}", Style);
3272 verifyFormat("if (foo && bar &&\n"
3273 " baz)\n"
3274 "{\n"
3275 " quux();\n"
3276 "}",
3277 "if (foo && bar &&\n"
3278 " baz) {\n"
3279 " quux();\n"
3280 "}",
3281 Style);
3282 verifyFormat("if (foo) {\n"
3283 " bar();\n"
3284 "} else if (baz ||\n"
3285 " quux)\n"
3286 "{\n"
3287 " foobar();\n"
3288 "}",
3289 "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3290 verifyFormat("if (foo) {\n"
3291 " bar();\n"
3292 "} else if (baz ||\n"
3293 " quux)\n"
3294 "{\n"
3295 " foobar();\n"
3296 "} else {\n"
3297 " barbaz();\n"
3298 "}",
3299 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3300 Style);
3301 verifyFormat("for (int i = 0;\n"
3302 " i < 10; ++i)\n"
3303 "{\n"
3304 " foo();\n"
3305 "}",
3306 "for(int i=0;i<10;++i){foo();}", Style);
3307 verifyFormat("foreach (int i,\n"
3308 " list)\n"
3309 "{\n"
3310 " foo();\n"
3311 "}",
3312 "foreach(int i, list){foo();}", Style);
3313 Style.ColumnLimit =
3314 40; // to concentrate at brace wrapping, not line wrap due to column limit
3315 verifyFormat("foreach (int i, list) {\n"
3316 " foo();\n"
3317 "}",
3318 "foreach(int i, list){foo();}", Style);
3319 Style.ColumnLimit =
3320 20; // to concentrate at brace wrapping, not line wrap due to column limit
3321 verifyFormat("while (foo || bar ||\n"
3322 " baz)\n"
3323 "{\n"
3324 " quux();\n"
3325 "}",
3326 "while(foo||bar||baz){quux();}", Style);
3327 verifyFormat("switch (\n"
3328 " foo = barbaz)\n"
3329 "{\n"
3330 "case quux:\n"
3331 " return;\n"
3332 "}",
3333 "switch(foo=barbaz){case quux:return;}", Style);
3334 verifyFormat("try {\n"
3335 " foo();\n"
3336 "} catch (\n"
3337 " Exception &bar)\n"
3338 "{\n"
3339 " baz();\n"
3340 "}",
3341 "try{foo();}catch(Exception&bar){baz();}", Style);
3342 Style.ColumnLimit =
3343 40; // to concentrate at brace wrapping, not line wrap due to column limit
3344 verifyFormat("try {\n"
3345 " foo();\n"
3346 "} catch (Exception &bar) {\n"
3347 " baz();\n"
3348 "}",
3349 "try{foo();}catch(Exception&bar){baz();}", Style);
3350 Style.ColumnLimit =
3351 20; // to concentrate at brace wrapping, not line wrap due to column limit
3352
3353 Style.BraceWrapping.BeforeElse = true;
3354 verifyFormat("if (foo) {\n"
3355 " bar();\n"
3356 "}\n"
3357 "else if (baz ||\n"
3358 " quux)\n"
3359 "{\n"
3360 " foobar();\n"
3361 "}\n"
3362 "else {\n"
3363 " barbaz();\n"
3364 "}",
3365 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3366 Style);
3367
3368 Style.BraceWrapping.BeforeCatch = true;
3369 verifyFormat("try {\n"
3370 " foo();\n"
3371 "}\n"
3372 "catch (...) {\n"
3373 " baz();\n"
3374 "}",
3375 "try{foo();}catch(...){baz();}", Style);
3376
3377 Style.BraceWrapping.AfterFunction = true;
3378 Style.BraceWrapping.AfterStruct = false;
3379 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3380 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3381 Style.ColumnLimit = 80;
3382 verifyFormat("void shortfunction() { bar(); }", Style);
3383 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3384 verifyFormat("struct T {};", Style);
3385
3386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3387 verifyFormat("void shortfunction()\n"
3388 "{\n"
3389 " bar();\n"
3390 "}",
3391 Style);
3392 verifyFormat("struct T shortfunction()\n"
3393 "{\n"
3394 " return bar();\n"
3395 "}",
3396 Style);
3397 verifyFormat("struct T {};", Style);
3398
3399 Style.BraceWrapping.AfterFunction = false;
3400 Style.BraceWrapping.AfterStruct = true;
3401 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3402 verifyFormat("void shortfunction() { bar(); }", Style);
3403 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3404 verifyFormat("struct T\n"
3405 "{\n"
3406 "};",
3407 Style);
3408
3409 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3410 verifyFormat("void shortfunction() {\n"
3411 " bar();\n"
3412 "}",
3413 Style);
3414 verifyFormat("struct T shortfunction() {\n"
3415 " return bar();\n"
3416 "}",
3417 Style);
3418 verifyFormat("struct T\n"
3419 "{\n"
3420 "};",
3421 Style);
3422 }
3423
3424 TEST_F(FormatTest, BeforeWhile) {
3425 FormatStyle Style = getLLVMStyle();
3426 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3427
3428 verifyFormat("do {\n"
3429 " foo();\n"
3430 "} while (1);",
3431 Style);
3432 Style.BraceWrapping.BeforeWhile = true;
3433 verifyFormat("do {\n"
3434 " foo();\n"
3435 "}\n"
3436 "while (1);",
3437 Style);
3438 }
3439
3440 //===----------------------------------------------------------------------===//
3441 // Tests for classes, namespaces, etc.
3442 //===----------------------------------------------------------------------===//
3443
3444 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3445 verifyFormat("class A {};");
3446 }
3447
3448 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3449 verifyFormat("class A {\n"
3450 "public:\n"
3451 "public: // comment\n"
3452 "protected:\n"
3453 "private:\n"
3454 " void f() {}\n"
3455 "};");
3456 verifyFormat("export class A {\n"
3457 "public:\n"
3458 "public: // comment\n"
3459 "protected:\n"
3460 "private:\n"
3461 " void f() {}\n"
3462 "};");
3463 verifyGoogleFormat("class A {\n"
3464 " public:\n"
3465 " protected:\n"
3466 " private:\n"
3467 " void f() {}\n"
3468 "};");
3469 verifyGoogleFormat("export class A {\n"
3470 " public:\n"
3471 " protected:\n"
3472 " private:\n"
3473 " void f() {}\n"
3474 "};");
3475 verifyFormat("class A {\n"
3476 "public slots:\n"
3477 " void f1() {}\n"
3478 "public Q_SLOTS:\n"
3479 " void f2() {}\n"
3480 "protected slots:\n"
3481 " void f3() {}\n"
3482 "protected Q_SLOTS:\n"
3483 " void f4() {}\n"
3484 "private slots:\n"
3485 " void f5() {}\n"
3486 "private Q_SLOTS:\n"
3487 " void f6() {}\n"
3488 "signals:\n"
3489 " void g1();\n"
3490 "Q_SIGNALS:\n"
3491 " void g2();\n"
3492 "};");
3493
3494 // Don't interpret 'signals' the wrong way.
3495 verifyFormat("signals.set();");
3496 verifyFormat("for (Signals signals : f()) {\n}");
3497 verifyFormat("{\n"
3498 " signals.set(); // This needs indentation.\n"
3499 "}");
3500 verifyFormat("void f() {\n"
3501 "label:\n"
3502 " signals.baz();\n"
3503 "}");
3504 verifyFormat("private[1];");
3505 verifyFormat("testArray[public] = 1;");
3506 verifyFormat("public();");
3507 verifyFormat("myFunc(public);");
3508 verifyFormat("std::vector<int> testVec = {private};");
3509 verifyFormat("private.p = 1;");
3510 verifyFormat("void function(private...) {};");
3511 verifyFormat("if (private && public)");
3512 verifyFormat("private &= true;");
3513 verifyFormat("int x = private * public;");
3514 verifyFormat("public *= private;");
3515 verifyFormat("int x = public + private;");
3516 verifyFormat("private++;");
3517 verifyFormat("++private;");
3518 verifyFormat("public += private;");
3519 verifyFormat("public = public - private;");
3520 verifyFormat("public->foo();");
3521 verifyFormat("private--;");
3522 verifyFormat("--private;");
3523 verifyFormat("public -= 1;");
3524 verifyFormat("if (!private && !public)");
3525 verifyFormat("public != private;");
3526 verifyFormat("int x = public / private;");
3527 verifyFormat("public /= 2;");
3528 verifyFormat("public = public % 2;");
3529 verifyFormat("public %= 2;");
3530 verifyFormat("if (public < private)");
3531 verifyFormat("public << private;");
3532 verifyFormat("public <<= private;");
3533 verifyFormat("if (public > private)");
3534 verifyFormat("public >> private;");
3535 verifyFormat("public >>= private;");
3536 verifyFormat("public ^ private;");
3537 verifyFormat("public ^= private;");
3538 verifyFormat("public | private;");
3539 verifyFormat("public |= private;");
3540 verifyFormat("auto x = private ? 1 : 2;");
3541 verifyFormat("if (public == private)");
3542 verifyFormat("void foo(public, private)");
3543 verifyFormat("public::foo();");
3544
3545 verifyFormat("class A {\n"
3546 "public:\n"
3547 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3548 "\n"
3549 "private:\n"
3550 " int c;\n"
3551 "};\n"
3552 "class B {\n"
3553 "public:\n"
3554 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3555 "\n"
3556 "private:\n"
3557 " int c;\n"
3558 "};");
3559 }
3560
3561 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3562 verifyFormat("class A {\n"
3563 "public:\n"
3564 " void f();\n"
3565 "\n"
3566 "private:\n"
3567 " void g() {}\n"
3568 " // test\n"
3569 "protected:\n"
3570 " int h;\n"
3571 "};",
3572 "class A {\n"
3573 "public:\n"
3574 "void f();\n"
3575 "private:\n"
3576 "void g() {}\n"
3577 "// test\n"
3578 "protected:\n"
3579 "int h;\n"
3580 "};");
3581 verifyFormat("class A {\n"
3582 "protected:\n"
3583 "public:\n"
3584 " void f();\n"
3585 "};",
3586 "class A {\n"
3587 "protected:\n"
3588 "\n"
3589 "public:\n"
3590 "\n"
3591 " void f();\n"
3592 "};");
3593
3594 // Even ensure proper spacing inside macros.
3595 verifyFormat("#define B \\\n"
3596 " class A { \\\n"
3597 " protected: \\\n"
3598 " public: \\\n"
3599 " void f(); \\\n"
3600 " };",
3601 "#define B \\\n"
3602 " class A { \\\n"
3603 " protected: \\\n"
3604 " \\\n"
3605 " public: \\\n"
3606 " \\\n"
3607 " void f(); \\\n"
3608 " };",
3609 getGoogleStyle());
3610 // But don't remove empty lines after macros ending in access specifiers.
3611 verifyFormat("#define A private:\n"
3612 "\n"
3613 "int i;",
3614 "#define A private:\n"
3615 "\n"
3616 "int i;");
3617 }
3618
3619 TEST_F(FormatTest, FormatsClasses) {
3620 verifyFormat("class A : public B {};");
3621 verifyFormat("class A : public ::B {};");
3622
3623 verifyFormat(
3624 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3625 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3626 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3627 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3628 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3629 verifyFormat(
3630 "class A : public B, public C, public D, public E, public F {};");
3631 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3632 " public C,\n"
3633 " public D,\n"
3634 " public E,\n"
3635 " public F,\n"
3636 " public G {};");
3637
3638 verifyFormat("class\n"
3639 " ReallyReallyLongClassName {\n"
3640 " int i;\n"
3641 "};",
3642 getLLVMStyleWithColumns(32));
3643 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3644 " aaaaaaaaaaaaaaaa> {};");
3645 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3646 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3647 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3648 verifyFormat("template <class R, class C>\n"
3649 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3650 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3651 verifyFormat("class ::A::B {};");
3652 }
3653
3654 TEST_F(FormatTest, BreakInheritanceStyle) {
3655 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3656 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3657 FormatStyle::BILS_BeforeComma;
3658 verifyFormat("class MyClass : public X {};",
3659 StyleWithInheritanceBreakBeforeComma);
3660 verifyFormat("class MyClass\n"
3661 " : public X\n"
3662 " , public Y {};",
3663 StyleWithInheritanceBreakBeforeComma);
3664 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3665 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3666 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3667 StyleWithInheritanceBreakBeforeComma);
3668 verifyFormat("struct aaaaaaaaaaaaa\n"
3669 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3670 " aaaaaaaaaaaaaaaa> {};",
3671 StyleWithInheritanceBreakBeforeComma);
3672
3673 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3674 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3675 FormatStyle::BILS_AfterColon;
3676 verifyFormat("class MyClass : public X {};",
3677 StyleWithInheritanceBreakAfterColon);
3678 verifyFormat("class MyClass : public X, public Y {};",
3679 StyleWithInheritanceBreakAfterColon);
3680 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3681 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3682 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3683 StyleWithInheritanceBreakAfterColon);
3684 verifyFormat("struct aaaaaaaaaaaaa :\n"
3685 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3686 " aaaaaaaaaaaaaaaa> {};",
3687 StyleWithInheritanceBreakAfterColon);
3688
3689 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3690 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3691 FormatStyle::BILS_AfterComma;
3692 verifyFormat("class MyClass : public X {};",
3693 StyleWithInheritanceBreakAfterComma);
3694 verifyFormat("class MyClass : public X,\n"
3695 " public Y {};",
3696 StyleWithInheritanceBreakAfterComma);
3697 verifyFormat(
3698 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3699 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3700 "{};",
3701 StyleWithInheritanceBreakAfterComma);
3702 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3703 " aaaaaaaaaaaaaaaa> {};",
3704 StyleWithInheritanceBreakAfterComma);
3705 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3706 " : public OnceBreak,\n"
3707 " public AlwaysBreak,\n"
3708 " EvenBasesFitInOneLine {};",
3709 StyleWithInheritanceBreakAfterComma);
3710 }
3711
3712 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3713 verifyFormat("class A {\n} a, b;");
3714 verifyFormat("struct A {\n} a, b;");
3715 verifyFormat("union A {\n} a, b;");
3716
3717 verifyFormat("constexpr class A {\n} a, b;");
3718 verifyFormat("constexpr struct A {\n} a, b;");
3719 verifyFormat("constexpr union A {\n} a, b;");
3720
3721 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3722 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3723 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3724
3725 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3726 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3727 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3728
3729 verifyFormat("namespace ns {\n"
3730 "class {\n"
3731 "} a, b;\n"
3732 "} // namespace ns");
3733 verifyFormat("namespace ns {\n"
3734 "const class {\n"
3735 "} a, b;\n"
3736 "} // namespace ns");
3737 verifyFormat("namespace ns {\n"
3738 "constexpr class C {\n"
3739 "} a, b;\n"
3740 "} // namespace ns");
3741 verifyFormat("namespace ns {\n"
3742 "class { /* comment */\n"
3743 "} a, b;\n"
3744 "} // namespace ns");
3745 verifyFormat("namespace ns {\n"
3746 "const class { /* comment */\n"
3747 "} a, b;\n"
3748 "} // namespace ns");
3749 }
3750
3751 TEST_F(FormatTest, FormatsEnum) {
3752 verifyFormat("enum {\n"
3753 " Zero,\n"
3754 " One = 1,\n"
3755 " Two = One + 1,\n"
3756 " Three = (One + Two),\n"
3757 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3758 " Five = (One, Two, Three, Four, 5)\n"
3759 "};");
3760 verifyGoogleFormat("enum {\n"
3761 " Zero,\n"
3762 " One = 1,\n"
3763 " Two = One + 1,\n"
3764 " Three = (One + Two),\n"
3765 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3766 " Five = (One, Two, Three, Four, 5)\n"
3767 "};");
3768 verifyFormat("enum Enum {};");
3769 verifyFormat("enum {};");
3770 verifyFormat("enum X E {} d;");
3771 verifyFormat("enum __attribute__((...)) E {} d;");
3772 verifyFormat("enum __declspec__((...)) E {} d;");
3773 verifyFormat("enum [[nodiscard]] E {} d;");
3774 verifyFormat("enum {\n"
3775 " Bar = Foo<int, int>::value\n"
3776 "};",
3777 getLLVMStyleWithColumns(30));
3778
3779 verifyFormat("enum ShortEnum { A, B, C };");
3780 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3781
3782 verifyFormat("enum KeepEmptyLines {\n"
3783 " ONE,\n"
3784 "\n"
3785 " TWO,\n"
3786 "\n"
3787 " THREE\n"
3788 "}",
3789 "enum KeepEmptyLines {\n"
3790 " ONE,\n"
3791 "\n"
3792 " TWO,\n"
3793 "\n"
3794 "\n"
3795 " THREE\n"
3796 "}");
3797 verifyFormat("enum E { // comment\n"
3798 " ONE,\n"
3799 " TWO\n"
3800 "};\n"
3801 "int i;");
3802
3803 FormatStyle EightIndent = getLLVMStyle();
3804 EightIndent.IndentWidth = 8;
3805 verifyFormat("enum {\n"
3806 " VOID,\n"
3807 " CHAR,\n"
3808 " SHORT,\n"
3809 " INT,\n"
3810 " LONG,\n"
3811 " SIGNED,\n"
3812 " UNSIGNED,\n"
3813 " BOOL,\n"
3814 " FLOAT,\n"
3815 " DOUBLE,\n"
3816 " COMPLEX\n"
3817 "};",
3818 EightIndent);
3819
3820 verifyFormat("enum [[nodiscard]] E {\n"
3821 " ONE,\n"
3822 " TWO,\n"
3823 "};");
3824 verifyFormat("enum [[nodiscard]] E {\n"
3825 " // Comment 1\n"
3826 " ONE,\n"
3827 " // Comment 2\n"
3828 " TWO,\n"
3829 "};");
3830 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3831 " // Comment 1\n"
3832 " ONE,\n"
3833 " // Comment 2\n"
3834 " TWO\n"
3835 "};");
3836 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3837 " // Comment 1\n"
3838 " ONE,\n"
3839 " // Comment 2\n"
3840 " TWO\n"
3841 "};");
3842 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3843 " A,\n"
3844 " // bar\n"
3845 " B\n"
3846 "};",
3847 "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3848 " A,\n"
3849 " // bar\n"
3850 " B};");
3851
3852 // Not enums.
3853 verifyFormat("enum X f() {\n"
3854 " a();\n"
3855 " return 42;\n"
3856 "}");
3857 verifyFormat("enum X Type::f() {\n"
3858 " a();\n"
3859 " return 42;\n"
3860 "}");
3861 verifyFormat("enum ::X f() {\n"
3862 " a();\n"
3863 " return 42;\n"
3864 "}");
3865 verifyFormat("enum ns::X f() {\n"
3866 " a();\n"
3867 " return 42;\n"
3868 "}");
3869 }
3870
3871 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3872 verifyFormat("enum Type {\n"
3873 " One = 0; // These semicolons should be commas.\n"
3874 " Two = 1;\n"
3875 "};");
3876 verifyFormat("namespace n {\n"
3877 "enum Type {\n"
3878 " One,\n"
3879 " Two, // missing };\n"
3880 " int i;\n"
3881 "}\n"
3882 "void g() {}");
3883 }
3884
3885 TEST_F(FormatTest, FormatsEnumStruct) {
3886 verifyFormat("enum struct {\n"
3887 " Zero,\n"
3888 " One = 1,\n"
3889 " Two = One + 1,\n"
3890 " Three = (One + Two),\n"
3891 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3892 " Five = (One, Two, Three, Four, 5)\n"
3893 "};");
3894 verifyFormat("enum struct Enum {};");
3895 verifyFormat("enum struct {};");
3896 verifyFormat("enum struct X E {} d;");
3897 verifyFormat("enum struct __attribute__((...)) E {} d;");
3898 verifyFormat("enum struct __declspec__((...)) E {} d;");
3899 verifyFormat("enum struct [[nodiscard]] E {} d;");
3900 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3901
3902 verifyFormat("enum struct [[nodiscard]] E {\n"
3903 " ONE,\n"
3904 " TWO,\n"
3905 "};");
3906 verifyFormat("enum struct [[nodiscard]] E {\n"
3907 " // Comment 1\n"
3908 " ONE,\n"
3909 " // Comment 2\n"
3910 " TWO,\n"
3911 "};");
3912 }
3913
3914 TEST_F(FormatTest, FormatsEnumClass) {
3915 verifyFormat("enum class {\n"
3916 " Zero,\n"
3917 " One = 1,\n"
3918 " Two = One + 1,\n"
3919 " Three = (One + Two),\n"
3920 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3921 " Five = (One, Two, Three, Four, 5)\n"
3922 "};");
3923 verifyFormat("enum class Enum {};");
3924 verifyFormat("enum class {};");
3925 verifyFormat("enum class X E {} d;");
3926 verifyFormat("enum class __attribute__((...)) E {} d;");
3927 verifyFormat("enum class __declspec__((...)) E {} d;");
3928 verifyFormat("enum class [[nodiscard]] E {} d;");
3929 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3930
3931 verifyFormat("enum class [[nodiscard]] E {\n"
3932 " ONE,\n"
3933 " TWO,\n"
3934 "};");
3935 verifyFormat("enum class [[nodiscard]] E {\n"
3936 " // Comment 1\n"
3937 " ONE,\n"
3938 " // Comment 2\n"
3939 " TWO,\n"
3940 "};");
3941 }
3942
3943 TEST_F(FormatTest, FormatsEnumTypes) {
3944 verifyFormat("enum X : int {\n"
3945 " A, // Force multiple lines.\n"
3946 " B\n"
3947 "};");
3948 verifyFormat("enum X : int { A, B };");
3949 verifyFormat("enum X : std::uint32_t { A, B };");
3950 }
3951
3952 TEST_F(FormatTest, FormatsTypedefEnum) {
3953 FormatStyle Style = getLLVMStyleWithColumns(40);
3954 verifyFormat("typedef enum {} EmptyEnum;");
3955 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3956 verifyFormat("typedef enum {\n"
3957 " ZERO = 0,\n"
3958 " ONE = 1,\n"
3959 " TWO = 2,\n"
3960 " THREE = 3\n"
3961 "} LongEnum;",
3962 Style);
3963 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3964 Style.BraceWrapping.AfterEnum = true;
3965 verifyFormat("typedef enum {} EmptyEnum;");
3966 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3967 verifyFormat("typedef enum\n"
3968 "{\n"
3969 " ZERO = 0,\n"
3970 " ONE = 1,\n"
3971 " TWO = 2,\n"
3972 " THREE = 3\n"
3973 "} LongEnum;",
3974 Style);
3975 }
3976
3977 TEST_F(FormatTest, FormatsNSEnums) {
3978 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3979 verifyGoogleFormat(
3980 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3981 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3982 " // Information about someDecentlyLongValue.\n"
3983 " someDecentlyLongValue,\n"
3984 " // Information about anotherDecentlyLongValue.\n"
3985 " anotherDecentlyLongValue,\n"
3986 " // Information about aThirdDecentlyLongValue.\n"
3987 " aThirdDecentlyLongValue\n"
3988 "};");
3989 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3990 " // Information about someDecentlyLongValue.\n"
3991 " someDecentlyLongValue,\n"
3992 " // Information about anotherDecentlyLongValue.\n"
3993 " anotherDecentlyLongValue,\n"
3994 " // Information about aThirdDecentlyLongValue.\n"
3995 " aThirdDecentlyLongValue\n"
3996 "};");
3997 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3998 " a = 1,\n"
3999 " b = 2,\n"
4000 " c = 3,\n"
4001 "};");
4002 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
4003 " a = 1,\n"
4004 " b = 2,\n"
4005 " c = 3,\n"
4006 "};");
4007 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
4008 " a = 1,\n"
4009 " b = 2,\n"
4010 " c = 3,\n"
4011 "};");
4012 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4013 " a = 1,\n"
4014 " b = 2,\n"
4015 " c = 3,\n"
4016 "};");
4017 }
4018
4019 TEST_F(FormatTest, FormatsBitfields) {
4020 verifyFormat("struct Bitfields {\n"
4021 " unsigned sClass : 8;\n"
4022 " unsigned ValueKind : 2;\n"
4023 "};");
4024 verifyFormat("struct A {\n"
4025 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4026 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4027 "};");
4028 verifyFormat("struct MyStruct {\n"
4029 " uchar data;\n"
4030 " uchar : 8;\n"
4031 " uchar : 8;\n"
4032 " uchar other;\n"
4033 "};");
4034 FormatStyle Style = getLLVMStyle();
4035 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
4036 verifyFormat("struct Bitfields {\n"
4037 " unsigned sClass:8;\n"
4038 " unsigned ValueKind:2;\n"
4039 " uchar other;\n"
4040 "};",
4041 Style);
4042 verifyFormat("struct A {\n"
4043 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4044 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4045 "};",
4046 Style);
4047 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
4048 verifyFormat("struct Bitfields {\n"
4049 " unsigned sClass :8;\n"
4050 " unsigned ValueKind :2;\n"
4051 " uchar other;\n"
4052 "};",
4053 Style);
4054 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
4055 verifyFormat("struct Bitfields {\n"
4056 " unsigned sClass: 8;\n"
4057 " unsigned ValueKind: 2;\n"
4058 " uchar other;\n"
4059 "};",
4060 Style);
4061 }
4062
4063 TEST_F(FormatTest, FormatsNamespaces) {
4064 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
4065 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
4066
4067 verifyFormat("namespace some_namespace {\n"
4068 "class A {};\n"
4069 "void f() { f(); }\n"
4070 "}",
4071 LLVMWithNoNamespaceFix);
4072 verifyFormat("#define M(x) x##x\n"
4073 "namespace M(x) {\n"
4074 "class A {};\n"
4075 "void f() { f(); }\n"
4076 "}",
4077 LLVMWithNoNamespaceFix);
4078 verifyFormat("#define M(x) x##x\n"
4079 "namespace N::inline M(x) {\n"
4080 "class A {};\n"
4081 "void f() { f(); }\n"
4082 "}",
4083 LLVMWithNoNamespaceFix);
4084 verifyFormat("#define M(x) x##x\n"
4085 "namespace M(x)::inline N {\n"
4086 "class A {};\n"
4087 "void f() { f(); }\n"
4088 "}",
4089 LLVMWithNoNamespaceFix);
4090 verifyFormat("#define M(x) x##x\n"
4091 "namespace N::M(x) {\n"
4092 "class A {};\n"
4093 "void f() { f(); }\n"
4094 "}",
4095 LLVMWithNoNamespaceFix);
4096 verifyFormat("#define M(x) x##x\n"
4097 "namespace M::N(x) {\n"
4098 "class A {};\n"
4099 "void f() { f(); }\n"
4100 "}",
4101 LLVMWithNoNamespaceFix);
4102 verifyFormat("namespace N::inline D {\n"
4103 "class A {};\n"
4104 "void f() { f(); }\n"
4105 "}",
4106 LLVMWithNoNamespaceFix);
4107 verifyFormat("namespace N::inline D::E {\n"
4108 "class A {};\n"
4109 "void f() { f(); }\n"
4110 "}",
4111 LLVMWithNoNamespaceFix);
4112 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4113 "class A {};\n"
4114 "void f() { f(); }\n"
4115 "}",
4116 LLVMWithNoNamespaceFix);
4117 verifyFormat("/* something */ namespace some_namespace {\n"
4118 "class A {};\n"
4119 "void f() { f(); }\n"
4120 "}",
4121 LLVMWithNoNamespaceFix);
4122 verifyFormat("namespace {\n"
4123 "class A {};\n"
4124 "void f() { f(); }\n"
4125 "}",
4126 LLVMWithNoNamespaceFix);
4127 verifyFormat("/* something */ namespace {\n"
4128 "class A {};\n"
4129 "void f() { f(); }\n"
4130 "}",
4131 LLVMWithNoNamespaceFix);
4132 verifyFormat("inline namespace X {\n"
4133 "class A {};\n"
4134 "void f() { f(); }\n"
4135 "}",
4136 LLVMWithNoNamespaceFix);
4137 verifyFormat("/* something */ inline namespace X {\n"
4138 "class A {};\n"
4139 "void f() { f(); }\n"
4140 "}",
4141 LLVMWithNoNamespaceFix);
4142 verifyFormat("export namespace X {\n"
4143 "class A {};\n"
4144 "void f() { f(); }\n"
4145 "}",
4146 LLVMWithNoNamespaceFix);
4147 verifyFormat("using namespace some_namespace;\n"
4148 "class A {};\n"
4149 "void f() { f(); }",
4150 LLVMWithNoNamespaceFix);
4151
4152 // This code is more common than we thought; if we
4153 // layout this correctly the semicolon will go into
4154 // its own line, which is undesirable.
4155 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4156 verifyFormat("namespace {\n"
4157 "class A {};\n"
4158 "};",
4159 LLVMWithNoNamespaceFix);
4160
4161 verifyFormat("namespace {\n"
4162 "int SomeVariable = 0; // comment\n"
4163 "} // namespace",
4164 LLVMWithNoNamespaceFix);
4165 verifyFormat("#ifndef HEADER_GUARD\n"
4166 "#define HEADER_GUARD\n"
4167 "namespace my_namespace {\n"
4168 "int i;\n"
4169 "} // my_namespace\n"
4170 "#endif // HEADER_GUARD",
4171 "#ifndef HEADER_GUARD\n"
4172 " #define HEADER_GUARD\n"
4173 " namespace my_namespace {\n"
4174 "int i;\n"
4175 "} // my_namespace\n"
4176 "#endif // HEADER_GUARD",
4177 LLVMWithNoNamespaceFix);
4178
4179 verifyFormat("namespace A::B {\n"
4180 "class C {};\n"
4181 "}",
4182 LLVMWithNoNamespaceFix);
4183
4184 FormatStyle Style = getLLVMStyle();
4185 Style.NamespaceIndentation = FormatStyle::NI_All;
4186 verifyFormat("namespace out {\n"
4187 " int i;\n"
4188 " namespace in {\n"
4189 " int i;\n"
4190 " } // namespace in\n"
4191 "} // namespace out",
4192 "namespace out {\n"
4193 "int i;\n"
4194 "namespace in {\n"
4195 "int i;\n"
4196 "} // namespace in\n"
4197 "} // namespace out",
4198 Style);
4199
4200 FormatStyle ShortInlineFunctions = getLLVMStyle();
4201 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4202 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4203 FormatStyle::SFS_Inline;
4204 verifyFormat("namespace {\n"
4205 " void f() {\n"
4206 " return;\n"
4207 " }\n"
4208 "} // namespace",
4209 ShortInlineFunctions);
4210 verifyFormat("namespace { /* comment */\n"
4211 " void f() {\n"
4212 " return;\n"
4213 " }\n"
4214 "} // namespace",
4215 ShortInlineFunctions);
4216 verifyFormat("namespace { // comment\n"
4217 " void f() {\n"
4218 " return;\n"
4219 " }\n"
4220 "} // namespace",
4221 ShortInlineFunctions);
4222 verifyFormat("namespace {\n"
4223 " int some_int;\n"
4224 " void f() {\n"
4225 " return;\n"
4226 " }\n"
4227 "} // namespace",
4228 ShortInlineFunctions);
4229 verifyFormat("namespace interface {\n"
4230 " void f() {\n"
4231 " return;\n"
4232 " }\n"
4233 "} // namespace interface",
4234 ShortInlineFunctions);
4235 verifyFormat("namespace {\n"
4236 " class X {\n"
4237 " void f() { return; }\n"
4238 " };\n"
4239 "} // namespace",
4240 ShortInlineFunctions);
4241 verifyFormat("namespace {\n"
4242 " class X { /* comment */\n"
4243 " void f() { return; }\n"
4244 " };\n"
4245 "} // namespace",
4246 ShortInlineFunctions);
4247 verifyFormat("namespace {\n"
4248 " class X { // comment\n"
4249 " void f() { return; }\n"
4250 " };\n"
4251 "} // namespace",
4252 ShortInlineFunctions);
4253 verifyFormat("namespace {\n"
4254 " struct X {\n"
4255 " void f() { return; }\n"
4256 " };\n"
4257 "} // namespace",
4258 ShortInlineFunctions);
4259 verifyFormat("namespace {\n"
4260 " union X {\n"
4261 " void f() { return; }\n"
4262 " };\n"
4263 "} // namespace",
4264 ShortInlineFunctions);
4265 verifyFormat("extern \"C\" {\n"
4266 "void f() {\n"
4267 " return;\n"
4268 "}\n"
4269 "} // namespace",
4270 ShortInlineFunctions);
4271 verifyFormat("namespace {\n"
4272 " class X {\n"
4273 " void f() { return; }\n"
4274 " } x;\n"
4275 "} // namespace",
4276 ShortInlineFunctions);
4277 verifyFormat("namespace {\n"
4278 " [[nodiscard]] class X {\n"
4279 " void f() { return; }\n"
4280 " };\n"
4281 "} // namespace",
4282 ShortInlineFunctions);
4283 verifyFormat("namespace {\n"
4284 " static class X {\n"
4285 " void f() { return; }\n"
4286 " } x;\n"
4287 "} // namespace",
4288 ShortInlineFunctions);
4289 verifyFormat("namespace {\n"
4290 " constexpr class X {\n"
4291 " void f() { return; }\n"
4292 " } x;\n"
4293 "} // namespace",
4294 ShortInlineFunctions);
4295
4296 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4297 verifyFormat("extern \"C\" {\n"
4298 " void f() {\n"
4299 " return;\n"
4300 " }\n"
4301 "} // namespace",
4302 ShortInlineFunctions);
4303
4304 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4305 verifyFormat("namespace out {\n"
4306 "int i;\n"
4307 "namespace in {\n"
4308 " int i;\n"
4309 "} // namespace in\n"
4310 "} // namespace out",
4311 "namespace out {\n"
4312 "int i;\n"
4313 "namespace in {\n"
4314 "int i;\n"
4315 "} // namespace in\n"
4316 "} // namespace out",
4317 Style);
4318
4319 Style.NamespaceIndentation = FormatStyle::NI_None;
4320 verifyFormat("template <class T>\n"
4321 "concept a_concept = X<>;\n"
4322 "namespace B {\n"
4323 "struct b_struct {};\n"
4324 "} // namespace B",
4325 Style);
4326 verifyFormat("template <int I>\n"
4327 "constexpr void foo()\n"
4328 " requires(I == 42)\n"
4329 "{}\n"
4330 "namespace ns {\n"
4331 "void foo() {}\n"
4332 "} // namespace ns",
4333 Style);
4334
4335 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4336 LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4337 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4338 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4339 "// block for debug mode\n"
4340 "#ifndef NDEBUG\n"
4341 "#endif\n"
4342 "}}} // namespace ns1::ns2::ns3",
4343 LLVMWithCompactInnerNamespace);
4344 }
4345
4346 TEST_F(FormatTest, NamespaceMacros) {
4347 FormatStyle Style = getLLVMStyle();
4348 Style.NamespaceMacros.push_back("TESTSUITE");
4349
4350 verifyFormat("TESTSUITE(A) {\n"
4351 "int foo();\n"
4352 "} // TESTSUITE(A)",
4353 Style);
4354
4355 verifyFormat("TESTSUITE(A, B) {\n"
4356 "int foo();\n"
4357 "} // TESTSUITE(A)",
4358 Style);
4359
4360 // Properly indent according to NamespaceIndentation style
4361 Style.NamespaceIndentation = FormatStyle::NI_All;
4362 verifyFormat("TESTSUITE(A) {\n"
4363 " int foo();\n"
4364 "} // TESTSUITE(A)",
4365 Style);
4366 verifyFormat("TESTSUITE(A) {\n"
4367 " namespace B {\n"
4368 " int foo();\n"
4369 " } // namespace B\n"
4370 "} // TESTSUITE(A)",
4371 Style);
4372 verifyFormat("namespace A {\n"
4373 " TESTSUITE(B) {\n"
4374 " int foo();\n"
4375 " } // TESTSUITE(B)\n"
4376 "} // namespace A",
4377 Style);
4378
4379 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4380 verifyFormat("TESTSUITE(A) {\n"
4381 "TESTSUITE(B) {\n"
4382 " int foo();\n"
4383 "} // TESTSUITE(B)\n"
4384 "} // TESTSUITE(A)",
4385 Style);
4386 verifyFormat("TESTSUITE(A) {\n"
4387 "namespace B {\n"
4388 " int foo();\n"
4389 "} // namespace B\n"
4390 "} // TESTSUITE(A)",
4391 Style);
4392 verifyFormat("namespace A {\n"
4393 "TESTSUITE(B) {\n"
4394 " int foo();\n"
4395 "} // TESTSUITE(B)\n"
4396 "} // namespace A",
4397 Style);
4398
4399 // Properly merge namespace-macros blocks in CompactNamespaces mode
4400 Style.NamespaceIndentation = FormatStyle::NI_None;
4401 Style.CompactNamespaces = true;
4402 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4403 "}} // TESTSUITE(A::B)",
4404 Style);
4405
4406 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4407 "}} // TESTSUITE(out::in)",
4408 "TESTSUITE(out) {\n"
4409 "TESTSUITE(in) {\n"
4410 "} // TESTSUITE(in)\n"
4411 "} // TESTSUITE(out)",
4412 Style);
4413
4414 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4415 "}} // TESTSUITE(out::in)",
4416 "TESTSUITE(out) {\n"
4417 "TESTSUITE(in) {\n"
4418 "} // TESTSUITE(in)\n"
4419 "} // TESTSUITE(out)",
4420 Style);
4421
4422 // Do not merge different namespaces/macros
4423 verifyFormat("namespace out {\n"
4424 "TESTSUITE(in) {\n"
4425 "} // TESTSUITE(in)\n"
4426 "} // namespace out",
4427 Style);
4428 verifyFormat("TESTSUITE(out) {\n"
4429 "namespace in {\n"
4430 "} // namespace in\n"
4431 "} // TESTSUITE(out)",
4432 Style);
4433 Style.NamespaceMacros.push_back("FOOBAR");
4434 verifyFormat("TESTSUITE(out) {\n"
4435 "FOOBAR(in) {\n"
4436 "} // FOOBAR(in)\n"
4437 "} // TESTSUITE(out)",
4438 Style);
4439 }
4440
4441 TEST_F(FormatTest, FormatsCompactNamespaces) {
4442 FormatStyle Style = getLLVMStyle();
4443 Style.CompactNamespaces = true;
4444 Style.NamespaceMacros.push_back("TESTSUITE");
4445
4446 verifyFormat("namespace A { namespace B {\n"
4447 "}} // namespace A::B",
4448 Style);
4449
4450 verifyFormat("namespace out { namespace in {\n"
4451 "}} // namespace out::in",
4452 "namespace out {\n"
4453 "namespace in {\n"
4454 "} // namespace in\n"
4455 "} // namespace out",
4456 Style);
4457
4458 // Only namespaces which have both consecutive opening and end get compacted
4459 verifyFormat("namespace out {\n"
4460 "namespace in1 {\n"
4461 "} // namespace in1\n"
4462 "namespace in2 {\n"
4463 "} // namespace in2\n"
4464 "} // namespace out",
4465 Style);
4466
4467 verifyFormat("namespace out {\n"
4468 "int i;\n"
4469 "namespace in {\n"
4470 "int j;\n"
4471 "} // namespace in\n"
4472 "int k;\n"
4473 "} // namespace out",
4474 "namespace out { int i;\n"
4475 "namespace in { int j; } // namespace in\n"
4476 "int k; } // namespace out",
4477 Style);
4478
4479 verifyFormat("namespace A { namespace B { namespace C {\n"
4480 "}}} // namespace A::B::C",
4481 "namespace A { namespace B {\n"
4482 "namespace C {\n"
4483 "}} // namespace B::C\n"
4484 "} // namespace A",
4485 Style);
4486
4487 Style.ColumnLimit = 40;
4488 verifyFormat("namespace aaaaaaaaaa {\n"
4489 "namespace bbbbbbbbbb {\n"
4490 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4491 "namespace aaaaaaaaaa {\n"
4492 "namespace bbbbbbbbbb {\n"
4493 "} // namespace bbbbbbbbbb\n"
4494 "} // namespace aaaaaaaaaa",
4495 Style);
4496
4497 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4498 "namespace cccccc {\n"
4499 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4500 "namespace aaaaaa {\n"
4501 "namespace bbbbbb {\n"
4502 "namespace cccccc {\n"
4503 "} // namespace cccccc\n"
4504 "} // namespace bbbbbb\n"
4505 "} // namespace aaaaaa",
4506 Style);
4507 Style.ColumnLimit = 80;
4508
4509 // Extra semicolon after 'inner' closing brace prevents merging
4510 verifyFormat("namespace out { namespace in {\n"
4511 "}; } // namespace out::in",
4512 "namespace out {\n"
4513 "namespace in {\n"
4514 "}; // namespace in\n"
4515 "} // namespace out",
4516 Style);
4517
4518 // Extra semicolon after 'outer' closing brace is conserved
4519 verifyFormat("namespace out { namespace in {\n"
4520 "}}; // namespace out::in",
4521 "namespace out {\n"
4522 "namespace in {\n"
4523 "} // namespace in\n"
4524 "}; // namespace out",
4525 Style);
4526
4527 Style.NamespaceIndentation = FormatStyle::NI_All;
4528 verifyFormat("namespace out { namespace in {\n"
4529 " int i;\n"
4530 "}} // namespace out::in",
4531 "namespace out {\n"
4532 "namespace in {\n"
4533 "int i;\n"
4534 "} // namespace in\n"
4535 "} // namespace out",
4536 Style);
4537 verifyFormat("namespace out { namespace mid {\n"
4538 " namespace in {\n"
4539 " int j;\n"
4540 " } // namespace in\n"
4541 " int k;\n"
4542 "}} // namespace out::mid",
4543 "namespace out { namespace mid {\n"
4544 "namespace in { int j; } // namespace in\n"
4545 "int k; }} // namespace out::mid",
4546 Style);
4547
4548 verifyFormat("namespace A { namespace B { namespace C {\n"
4549 " int i;\n"
4550 "}}} // namespace A::B::C\n"
4551 "int main() {\n"
4552 " if (true)\n"
4553 " return 0;\n"
4554 "}",
4555 "namespace A { namespace B {\n"
4556 "namespace C {\n"
4557 " int i;\n"
4558 "}} // namespace B::C\n"
4559 "} // namespace A\n"
4560 "int main() {\n"
4561 " if (true)\n"
4562 " return 0;\n"
4563 "}",
4564 Style);
4565
4566 verifyFormat("namespace A { namespace B { namespace C {\n"
4567 "#ifdef FOO\n"
4568 " int i;\n"
4569 "#endif\n"
4570 "}}} // namespace A::B::C\n"
4571 "int main() {\n"
4572 " if (true)\n"
4573 " return 0;\n"
4574 "}",
4575 "namespace A { namespace B {\n"
4576 "namespace C {\n"
4577 "#ifdef FOO\n"
4578 " int i;\n"
4579 "#endif\n"
4580 "}} // namespace B::C\n"
4581 "} // namespace A\n"
4582 "int main() {\n"
4583 " if (true)\n"
4584 " return 0;\n"
4585 "}",
4586 Style);
4587
4588 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4589 verifyFormat("namespace out { namespace in {\n"
4590 " int i;\n"
4591 "}} // namespace out::in",
4592 "namespace out {\n"
4593 "namespace in {\n"
4594 "int i;\n"
4595 "} // namespace in\n"
4596 "} // namespace out",
4597 Style);
4598 verifyFormat("namespace out { namespace mid { namespace in {\n"
4599 " int i;\n"
4600 "}}} // namespace out::mid::in",
4601 "namespace out {\n"
4602 "namespace mid {\n"
4603 "namespace in {\n"
4604 "int i;\n"
4605 "} // namespace in\n"
4606 "} // namespace mid\n"
4607 "} // namespace out",
4608 Style);
4609
4610 Style.CompactNamespaces = true;
4611 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4612 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4613 Style.BraceWrapping.BeforeLambdaBody = true;
4614 verifyFormat("namespace out { namespace in {\n"
4615 "}} // namespace out::in",
4616 Style);
4617 verifyFormat("namespace out { namespace in {\n"
4618 "}} // namespace out::in",
4619 "namespace out {\n"
4620 "namespace in {\n"
4621 "} // namespace in\n"
4622 "} // namespace out",
4623 Style);
4624 }
4625
4626 TEST_F(FormatTest, FormatsExternC) {
4627 verifyFormat("extern \"C\" {\nint a;");
4628 verifyFormat("extern \"C\" {}");
4629 verifyFormat("extern \"C\" {\n"
4630 "int foo();\n"
4631 "}");
4632 verifyFormat("extern \"C\" int foo() {}");
4633 verifyFormat("extern \"C\" int foo();");
4634 verifyFormat("extern \"C\" int foo() {\n"
4635 " int i = 42;\n"
4636 " return i;\n"
4637 "}");
4638
4639 FormatStyle Style = getLLVMStyle();
4640 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4641 Style.BraceWrapping.AfterFunction = true;
4642 verifyFormat("extern \"C\" int foo() {}", Style);
4643 verifyFormat("extern \"C\" int foo();", Style);
4644 verifyFormat("extern \"C\" int foo()\n"
4645 "{\n"
4646 " int i = 42;\n"
4647 " return i;\n"
4648 "}",
4649 Style);
4650
4651 Style.BraceWrapping.AfterExternBlock = true;
4652 Style.BraceWrapping.SplitEmptyRecord = false;
4653 verifyFormat("extern \"C\"\n"
4654 "{}",
4655 Style);
4656 verifyFormat("extern \"C\"\n"
4657 "{\n"
4658 " int foo();\n"
4659 "}",
4660 Style);
4661 }
4662
4663 TEST_F(FormatTest, IndentExternBlockStyle) {
4664 FormatStyle Style = getLLVMStyle();
4665 Style.IndentWidth = 2;
4666
4667 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4668 verifyFormat("extern \"C\" { /*9*/\n"
4669 "}",
4670 Style);
4671 verifyFormat("extern \"C\" {\n"
4672 " int foo10();\n"
4673 "}",
4674 Style);
4675
4676 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4677 verifyFormat("extern \"C\" { /*11*/\n"
4678 "}",
4679 Style);
4680 verifyFormat("extern \"C\" {\n"
4681 "int foo12();\n"
4682 "}",
4683 Style);
4684
4685 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4686 verifyFormat("extern \"C\"\n"
4687 "{\n"
4688 "int i;\n"
4689 "}",
4690 Style);
4691
4692 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4693 Style.BraceWrapping.AfterExternBlock = true;
4694 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4695 verifyFormat("extern \"C\"\n"
4696 "{ /*13*/\n"
4697 "}",
4698 Style);
4699 verifyFormat("extern \"C\"\n{\n"
4700 " int foo14();\n"
4701 "}",
4702 Style);
4703
4704 Style.BraceWrapping.AfterExternBlock = false;
4705 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4706 verifyFormat("extern \"C\" { /*15*/\n"
4707 "}",
4708 Style);
4709 verifyFormat("extern \"C\" {\n"
4710 "int foo16();\n"
4711 "}",
4712 Style);
4713
4714 Style.BraceWrapping.AfterExternBlock = true;
4715 verifyFormat("extern \"C\"\n"
4716 "{ /*13*/\n"
4717 "}",
4718 Style);
4719 verifyFormat("extern \"C\"\n"
4720 "{\n"
4721 "int foo14();\n"
4722 "}",
4723 Style);
4724
4725 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4726 verifyFormat("extern \"C\"\n"
4727 "{ /*13*/\n"
4728 "}",
4729 Style);
4730 verifyFormat("extern \"C\"\n"
4731 "{\n"
4732 " int foo14();\n"
4733 "}",
4734 Style);
4735 }
4736
4737 TEST_F(FormatTest, FormatsInlineASM) {
4738 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4739 verifyFormat("asm(\"nop\" ::: \"memory\");");
4740 verifyFormat(
4741 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4742 " \"cpuid\\n\\t\"\n"
4743 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4744 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4745 " : \"a\"(value));");
4746 verifyFormat(
4747 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4748 " __asm {\n"
4749 " mov edx,[that] // vtable in edx\n"
4750 " mov eax,methodIndex\n"
4751 " call [edx][eax*4] // stdcall\n"
4752 " }\n"
4753 "}",
4754 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4755 " __asm {\n"
4756 " mov edx,[that] // vtable in edx\n"
4757 " mov eax,methodIndex\n"
4758 " call [edx][eax*4] // stdcall\n"
4759 " }\n"
4760 "}");
4761 verifyNoChange("_asm {\n"
4762 " xor eax, eax;\n"
4763 " cpuid;\n"
4764 "}");
4765 verifyFormat("void function() {\n"
4766 " // comment\n"
4767 " asm(\"\");\n"
4768 "}");
4769 verifyFormat("__asm {\n"
4770 "}\n"
4771 "int i;",
4772 "__asm {\n"
4773 "}\n"
4774 "int i;");
4775
4776 auto Style = getLLVMStyleWithColumns(0);
4777 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4778 const StringRef Code2{"asm(\"xyz\"\n"
4779 " : \"=a\"(a), \"=d\"(b)\n"
4780 " : \"a\"(data));"};
4781 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4782 " : \"a\"(data));"};
4783
4784 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4785 verifyFormat(Code1, Style);
4786 verifyNoChange(Code2, Style);
4787 verifyNoChange(Code3, Style);
4788
4789 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4790 verifyFormat(Code2, Code1, Style);
4791 verifyNoChange(Code2, Style);
4792 verifyFormat(Code2, Code3, Style);
4793 }
4794
4795 TEST_F(FormatTest, FormatTryCatch) {
4796 verifyFormat("try {\n"
4797 " throw a * b;\n"
4798 "} catch (int a) {\n"
4799 " // Do nothing.\n"
4800 "} catch (...) {\n"
4801 " exit(42);\n"
4802 "}");
4803
4804 // Function-level try statements.
4805 verifyFormat("int f() try { return 4; } catch (...) {\n"
4806 " return 5;\n"
4807 "}");
4808 verifyFormat("class A {\n"
4809 " int a;\n"
4810 " A() try : a(0) {\n"
4811 " } catch (...) {\n"
4812 " throw;\n"
4813 " }\n"
4814 "};");
4815 verifyFormat("class A {\n"
4816 " int a;\n"
4817 " A() try : a(0), b{1} {\n"
4818 " } catch (...) {\n"
4819 " throw;\n"
4820 " }\n"
4821 "};");
4822 verifyFormat("class A {\n"
4823 " int a;\n"
4824 " A() try : a(0), b{1}, c{2} {\n"
4825 " } catch (...) {\n"
4826 " throw;\n"
4827 " }\n"
4828 "};");
4829 verifyFormat("class A {\n"
4830 " int a;\n"
4831 " A() try : a(0), b{1}, c{2} {\n"
4832 " { // New scope.\n"
4833 " }\n"
4834 " } catch (...) {\n"
4835 " throw;\n"
4836 " }\n"
4837 "};");
4838
4839 // Incomplete try-catch blocks.
4840 verifyIncompleteFormat("try {} catch (");
4841 }
4842
4843 TEST_F(FormatTest, FormatTryAsAVariable) {
4844 verifyFormat("int try;");
4845 verifyFormat("int try, size;");
4846 verifyFormat("try = foo();");
4847 verifyFormat("if (try < size) {\n return true;\n}");
4848
4849 verifyFormat("int catch;");
4850 verifyFormat("int catch, size;");
4851 verifyFormat("catch = foo();");
4852 verifyFormat("if (catch < size) {\n return true;\n}");
4853
4854 FormatStyle Style = getLLVMStyle();
4855 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4856 Style.BraceWrapping.AfterFunction = true;
4857 Style.BraceWrapping.BeforeCatch = true;
4858 verifyFormat("try {\n"
4859 " int bar = 1;\n"
4860 "}\n"
4861 "catch (...) {\n"
4862 " int bar = 1;\n"
4863 "}",
4864 Style);
4865 verifyFormat("#if NO_EX\n"
4866 "try\n"
4867 "#endif\n"
4868 "{\n"
4869 "}\n"
4870 "#if NO_EX\n"
4871 "catch (...) {\n"
4872 "}",
4873 Style);
4874 verifyFormat("try /* abc */ {\n"
4875 " int bar = 1;\n"
4876 "}\n"
4877 "catch (...) {\n"
4878 " int bar = 1;\n"
4879 "}",
4880 Style);
4881 verifyFormat("try\n"
4882 "// abc\n"
4883 "{\n"
4884 " int bar = 1;\n"
4885 "}\n"
4886 "catch (...) {\n"
4887 " int bar = 1;\n"
4888 "}",
4889 Style);
4890 }
4891
4892 TEST_F(FormatTest, FormatSEHTryCatch) {
4893 verifyFormat("__try {\n"
4894 " int a = b * c;\n"
4895 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4896 " // Do nothing.\n"
4897 "}");
4898
4899 verifyFormat("__try {\n"
4900 " int a = b * c;\n"
4901 "} __finally {\n"
4902 " // Do nothing.\n"
4903 "}");
4904
4905 verifyFormat("DEBUG({\n"
4906 " __try {\n"
4907 " } __finally {\n"
4908 " }\n"
4909 "});");
4910 }
4911
4912 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4913 verifyFormat("try {\n"
4914 " f();\n"
4915 "} catch {\n"
4916 " g();\n"
4917 "}");
4918 verifyFormat("try {\n"
4919 " f();\n"
4920 "} catch (A a) MACRO(x) {\n"
4921 " g();\n"
4922 "} catch (B b) MACRO(x) {\n"
4923 " g();\n"
4924 "}");
4925 }
4926
4927 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4928 FormatStyle Style = getLLVMStyle();
4929 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4930 FormatStyle::BS_WebKit}) {
4931 Style.BreakBeforeBraces = BraceStyle;
4932 verifyFormat("try {\n"
4933 " // something\n"
4934 "} catch (...) {\n"
4935 " // something\n"
4936 "}",
4937 Style);
4938 }
4939 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4940 verifyFormat("try {\n"
4941 " // something\n"
4942 "}\n"
4943 "catch (...) {\n"
4944 " // something\n"
4945 "}",
4946 Style);
4947 verifyFormat("__try {\n"
4948 " // something\n"
4949 "}\n"
4950 "__finally {\n"
4951 " // something\n"
4952 "}",
4953 Style);
4954 verifyFormat("@try {\n"
4955 " // something\n"
4956 "}\n"
4957 "@finally {\n"
4958 " // something\n"
4959 "}",
4960 Style);
4961 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4962 verifyFormat("try\n"
4963 "{\n"
4964 " // something\n"
4965 "}\n"
4966 "catch (...)\n"
4967 "{\n"
4968 " // something\n"
4969 "}",
4970 Style);
4971 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4972 verifyFormat("try\n"
4973 " {\n"
4974 " // something white\n"
4975 " }\n"
4976 "catch (...)\n"
4977 " {\n"
4978 " // something white\n"
4979 " }",
4980 Style);
4981 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4982 verifyFormat("try\n"
4983 " {\n"
4984 " // something\n"
4985 " }\n"
4986 "catch (...)\n"
4987 " {\n"
4988 " // something\n"
4989 " }",
4990 Style);
4991 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4992 Style.BraceWrapping.BeforeCatch = true;
4993 verifyFormat("try {\n"
4994 " // something\n"
4995 "}\n"
4996 "catch (...) {\n"
4997 " // something\n"
4998 "}",
4999 Style);
5000 }
5001
5002 TEST_F(FormatTest, StaticInitializers) {
5003 verifyFormat("static SomeClass SC = {1, 'a'};");
5004
5005 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
5006 " 100000000, "
5007 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
5008
5009 // Here, everything other than the "}" would fit on a line.
5010 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
5011 " 10000000000000000000000000};");
5012 verifyFormat("S s = {a,\n"
5013 "\n"
5014 " b};",
5015 "S s = {\n"
5016 " a,\n"
5017 "\n"
5018 " b\n"
5019 "};");
5020
5021 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5022 // line. However, the formatting looks a bit off and this probably doesn't
5023 // happen often in practice.
5024 verifyFormat("static int Variable[1] = {\n"
5025 " {1000000000000000000000000000000000000}};",
5026 getLLVMStyleWithColumns(40));
5027 }
5028
5029 TEST_F(FormatTest, DesignatedInitializers) {
5030 verifyFormat("const struct A a = {.a = 1, .b = 2};");
5031 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5032 " .bbbbbbbbbb = 2,\n"
5033 " .cccccccccc = 3,\n"
5034 " .dddddddddd = 4,\n"
5035 " .eeeeeeeeee = 5};");
5036 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5038 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5039 " .ccccccccccccccccccccccccccc = 3,\n"
5040 " .ddddddddddddddddddddddddddd = 4,\n"
5041 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5042
5043 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5044
5045 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5046 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5047 " [2] = bbbbbbbbbb,\n"
5048 " [3] = cccccccccc,\n"
5049 " [4] = dddddddddd,\n"
5050 " [5] = eeeeeeeeee};");
5051 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5052 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5054 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5055 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5056 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5057
5058 verifyFormat("for (const TestCase &test_case : {\n"
5059 " TestCase{\n"
5060 " .a = 1,\n"
5061 " .b = 1,\n"
5062 " },\n"
5063 " TestCase{\n"
5064 " .a = 2,\n"
5065 " .b = 2,\n"
5066 " },\n"
5067 " }) {\n"
5068 "}");
5069 }
5070
5071 TEST_F(FormatTest, BracedInitializerIndentWidth) {
5072 auto Style = getLLVMStyleWithColumns(60);
5073 Style.BinPackArguments = true;
5074 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5075 Style.BracedInitializerIndentWidth = 6;
5076
5077 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5078 verifyFormat("enum class {\n"
5079 " One,\n"
5080 " Two,\n"
5081 "};",
5082 Style);
5083 verifyFormat("class Foo {\n"
5084 " Foo() {}\n"
5085 " void bar();\n"
5086 "};",
5087 Style);
5088 verifyFormat("void foo() {\n"
5089 " auto bar = baz;\n"
5090 " return baz;\n"
5091 "};",
5092 Style);
5093 verifyFormat("auto foo = [&] {\n"
5094 " auto bar = baz;\n"
5095 " return baz;\n"
5096 "};",
5097 Style);
5098 verifyFormat("{\n"
5099 " auto bar = baz;\n"
5100 " return baz;\n"
5101 "};",
5102 Style);
5103 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5104 verifyFormat("SomeClass clazz(\n"
5105 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5106 " \"zzzzzzzzzzzzzzzzzz\");",
5107 Style);
5108
5109 // The following types of initialization are all affected by
5110 // BracedInitializerIndentWidth. Aggregate initialization.
5111 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5112 " 10000000, 20000000};",
5113 Style);
5114 verifyFormat("SomeStruct s{\n"
5115 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5116 " \"zzzzzzzzzzzzzzzz\"};",
5117 Style);
5118 // Designated initializers.
5119 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5120 " [0] = 10000000, [1] = 20000000};",
5121 Style);
5122 verifyFormat("SomeStruct s{\n"
5123 " .foo = \"xxxxxxxxxxxxx\",\n"
5124 " .bar = \"yyyyyyyyyyyyy\",\n"
5125 " .baz = \"zzzzzzzzzzzzz\"};",
5126 Style);
5127 // List initialization.
5128 verifyFormat("SomeStruct s{\n"
5129 " \"xxxxxxxxxxxxx\",\n"
5130 " \"yyyyyyyyyyyyy\",\n"
5131 " \"zzzzzzzzzzzzz\",\n"
5132 "};",
5133 Style);
5134 verifyFormat("SomeStruct{\n"
5135 " \"xxxxxxxxxxxxx\",\n"
5136 " \"yyyyyyyyyyyyy\",\n"
5137 " \"zzzzzzzzzzzzz\",\n"
5138 "};",
5139 Style);
5140 verifyFormat("new SomeStruct{\n"
5141 " \"xxxxxxxxxxxxx\",\n"
5142 " \"yyyyyyyyyyyyy\",\n"
5143 " \"zzzzzzzzzzzzz\",\n"
5144 "};",
5145 Style);
5146 // Member initializer.
5147 verifyFormat("class SomeClass {\n"
5148 " SomeStruct s{\n"
5149 " \"xxxxxxxxxxxxx\",\n"
5150 " \"yyyyyyyyyyyyy\",\n"
5151 " \"zzzzzzzzzzzzz\",\n"
5152 " };\n"
5153 "};",
5154 Style);
5155 // Constructor member initializer.
5156 verifyFormat("SomeClass::SomeClass : strct{\n"
5157 " \"xxxxxxxxxxxxx\",\n"
5158 " \"yyyyyyyyyyyyy\",\n"
5159 " \"zzzzzzzzzzzzz\",\n"
5160 " } {}",
5161 Style);
5162 // Copy initialization.
5163 verifyFormat("SomeStruct s = SomeStruct{\n"
5164 " \"xxxxxxxxxxxxx\",\n"
5165 " \"yyyyyyyyyyyyy\",\n"
5166 " \"zzzzzzzzzzzzz\",\n"
5167 "};",
5168 Style);
5169 // Copy list initialization.
5170 verifyFormat("SomeStruct s = {\n"
5171 " \"xxxxxxxxxxxxx\",\n"
5172 " \"yyyyyyyyyyyyy\",\n"
5173 " \"zzzzzzzzzzzzz\",\n"
5174 "};",
5175 Style);
5176 // Assignment operand initialization.
5177 verifyFormat("s = {\n"
5178 " \"xxxxxxxxxxxxx\",\n"
5179 " \"yyyyyyyyyyyyy\",\n"
5180 " \"zzzzzzzzzzzzz\",\n"
5181 "};",
5182 Style);
5183 // Returned object initialization.
5184 verifyFormat("return {\n"
5185 " \"xxxxxxxxxxxxx\",\n"
5186 " \"yyyyyyyyyyyyy\",\n"
5187 " \"zzzzzzzzzzzzz\",\n"
5188 "};",
5189 Style);
5190 // Initializer list.
5191 verifyFormat("auto initializerList = {\n"
5192 " \"xxxxxxxxxxxxx\",\n"
5193 " \"yyyyyyyyyyyyy\",\n"
5194 " \"zzzzzzzzzzzzz\",\n"
5195 "};",
5196 Style);
5197 // Function parameter initialization.
5198 verifyFormat("func({\n"
5199 " \"xxxxxxxxxxxxx\",\n"
5200 " \"yyyyyyyyyyyyy\",\n"
5201 " \"zzzzzzzzzzzzz\",\n"
5202 "});",
5203 Style);
5204 // Nested init lists.
5205 verifyFormat("SomeStruct s = {\n"
5206 " {{init1, init2, init3, init4, init5},\n"
5207 " {init1, init2, init3, init4, init5}}};",
5208 Style);
5209 verifyFormat("SomeStruct s = {\n"
5210 " {{\n"
5211 " .init1 = 1,\n"
5212 " .init2 = 2,\n"
5213 " .init3 = 3,\n"
5214 " .init4 = 4,\n"
5215 " .init5 = 5,\n"
5216 " },\n"
5217 " {init1, init2, init3, init4, init5}}};",
5218 Style);
5219 verifyFormat("SomeArrayT a[3] = {\n"
5220 " {\n"
5221 " foo,\n"
5222 " bar,\n"
5223 " },\n"
5224 " {\n"
5225 " foo,\n"
5226 " bar,\n"
5227 " },\n"
5228 " SomeArrayT{},\n"
5229 "};",
5230 Style);
5231 verifyFormat("SomeArrayT a[3] = {\n"
5232 " {foo},\n"
5233 " {\n"
5234 " {\n"
5235 " init1,\n"
5236 " init2,\n"
5237 " init3,\n"
5238 " },\n"
5239 " {\n"
5240 " init1,\n"
5241 " init2,\n"
5242 " init3,\n"
5243 " },\n"
5244 " },\n"
5245 " {baz},\n"
5246 "};",
5247 Style);
5248
5249 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5250 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5251 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5252 " \"zzzzzzzzzzzzz\"};",
5253 Style);
5254 }
5255
5256 TEST_F(FormatTest, NestedStaticInitializers) {
5257 verifyFormat("static A x = {{{}}};");
5258 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5259 " {init1, init2, init3, init4}}};",
5260 getLLVMStyleWithColumns(50));
5261
5262 verifyFormat("somes Status::global_reps[3] = {\n"
5263 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5264 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5265 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5266 getLLVMStyleWithColumns(60));
5267 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5268 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5269 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5270 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5271 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5272 " {rect.fRight - rect.fLeft, rect.fBottom - "
5273 "rect.fTop}};");
5274
5275 verifyFormat(
5276 "SomeArrayOfSomeType a = {\n"
5277 " {{1, 2, 3},\n"
5278 " {1, 2, 3},\n"
5279 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5280 " 333333333333333333333333333333},\n"
5281 " {1, 2, 3},\n"
5282 " {1, 2, 3}}};");
5283 verifyFormat(
5284 "SomeArrayOfSomeType a = {\n"
5285 " {{1, 2, 3}},\n"
5286 " {{1, 2, 3}},\n"
5287 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5288 " 333333333333333333333333333333}},\n"
5289 " {{1, 2, 3}},\n"
5290 " {{1, 2, 3}}};");
5291
5292 verifyFormat("struct {\n"
5293 " unsigned bit;\n"
5294 " const char *const name;\n"
5295 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5296 " {kOsWin, \"Windows\"},\n"
5297 " {kOsLinux, \"Linux\"},\n"
5298 " {kOsCrOS, \"Chrome OS\"}};");
5299 verifyFormat("struct {\n"
5300 " unsigned bit;\n"
5301 " const char *const name;\n"
5302 "} kBitsToOs[] = {\n"
5303 " {kOsMac, \"Mac\"},\n"
5304 " {kOsWin, \"Windows\"},\n"
5305 " {kOsLinux, \"Linux\"},\n"
5306 " {kOsCrOS, \"Chrome OS\"},\n"
5307 "};");
5308 }
5309
5310 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5311 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5312 " \\\n"
5313 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5314 }
5315
5316 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5317 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5318 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5319
5320 // Do break defaulted and deleted functions.
5321 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5322 " default;",
5323 getLLVMStyleWithColumns(40));
5324 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5325 " delete;",
5326 getLLVMStyleWithColumns(40));
5327 }
5328
5329 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5330 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5331 getLLVMStyleWithColumns(40));
5332 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5333 getLLVMStyleWithColumns(40));
5334 verifyFormat("#define Q \\\n"
5335 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5336 " \"aaaaaaaa.cpp\"",
5337 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5338 getLLVMStyleWithColumns(40));
5339 }
5340
5341 TEST_F(FormatTest, UnderstandsLinePPDirective) {
5342 verifyFormat("# 123 \"A string literal\"",
5343 " # 123 \"A string literal\"");
5344 }
5345
5346 TEST_F(FormatTest, LayoutUnknownPPDirective) {
5347 verifyFormat("#;");
5348 verifyFormat("#\n;\n;\n;");
5349 }
5350
5351 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5352 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5353 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5354 getLLVMStyleWithColumns(12));
5355 }
5356
5357 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5358 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5359 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5360 }
5361
5362 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5363 verifyFormat("#define A \\x20");
5364 verifyFormat("#define A \\ x20");
5365 verifyFormat("#define A \\ x20", "#define A \\ x20");
5366 verifyFormat("#define A ''");
5367 verifyFormat("#define A ''qqq");
5368 verifyFormat("#define A `qqq");
5369 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5370 verifyFormat("const char *c = STRINGIFY(\n"
5371 "\\na : b);",
5372 "const char * c = STRINGIFY(\n"
5373 "\\na : b);");
5374
5375 verifyFormat("a\r\\");
5376 verifyFormat("a\v\\");
5377 verifyFormat("a\f\\");
5378 }
5379
5380 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5381 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5382 style.IndentWidth = 4;
5383 style.PPIndentWidth = 1;
5384
5385 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5386 verifyFormat("#ifdef __linux__\n"
5387 "void foo() {\n"
5388 " int x = 0;\n"
5389 "}\n"
5390 "#define FOO\n"
5391 "#endif\n"
5392 "void bar() {\n"
5393 " int y = 0;\n"
5394 "}",
5395 style);
5396
5397 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5398 verifyFormat("#ifdef __linux__\n"
5399 "void foo() {\n"
5400 " int x = 0;\n"
5401 "}\n"
5402 "# define FOO foo\n"
5403 "#endif\n"
5404 "void bar() {\n"
5405 " int y = 0;\n"
5406 "}",
5407 style);
5408
5409 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5410 verifyFormat("#ifdef __linux__\n"
5411 "void foo() {\n"
5412 " int x = 0;\n"
5413 "}\n"
5414 " #define FOO foo\n"
5415 "#endif\n"
5416 "void bar() {\n"
5417 " int y = 0;\n"
5418 "}",
5419 style);
5420 verifyFormat("#if 1\n"
5421 " // some comments\n"
5422 " // another\n"
5423 " #define foo 1\n"
5424 "// not a define comment\n"
5425 "void bar() {\n"
5426 " // comment\n"
5427 " int y = 0;\n"
5428 "}",
5429 "#if 1\n"
5430 "// some comments\n"
5431 "// another\n"
5432 "#define foo 1\n"
5433 "// not a define comment\n"
5434 "void bar() {\n"
5435 " // comment\n"
5436 " int y = 0;\n"
5437 "}",
5438 style);
5439
5440 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5441 verifyFormat("#ifdef foo\n"
5442 "#define bar() \\\n"
5443 " if (A) { \\\n"
5444 " B(); \\\n"
5445 " } \\\n"
5446 " C();\n"
5447 "#endif",
5448 style);
5449 verifyFormat("if (emacs) {\n"
5450 "#ifdef is\n"
5451 "#define lit \\\n"
5452 " if (af) { \\\n"
5453 " return duh(); \\\n"
5454 " }\n"
5455 "#endif\n"
5456 "}",
5457 style);
5458 verifyFormat("#if abc\n"
5459 "#ifdef foo\n"
5460 "#define bar() \\\n"
5461 " if (A) { \\\n"
5462 " if (B) { \\\n"
5463 " C(); \\\n"
5464 " } \\\n"
5465 " } \\\n"
5466 " D();\n"
5467 "#endif\n"
5468 "#endif",
5469 style);
5470 verifyFormat("#ifndef foo\n"
5471 "#define foo\n"
5472 "if (emacs) {\n"
5473 "#ifdef is\n"
5474 "#define lit \\\n"
5475 " if (af) { \\\n"
5476 " return duh(); \\\n"
5477 " }\n"
5478 "#endif\n"
5479 "}\n"
5480 "#endif",
5481 style);
5482 verifyFormat("#if 1\n"
5483 "#define X \\\n"
5484 " { \\\n"
5485 " x; \\\n"
5486 " x; \\\n"
5487 " }\n"
5488 "#endif",
5489 style);
5490 verifyFormat("#define X \\\n"
5491 " { \\\n"
5492 " x; \\\n"
5493 " x; \\\n"
5494 " }",
5495 style);
5496
5497 style.PPIndentWidth = 2;
5498 verifyFormat("#ifdef foo\n"
5499 "#define bar() \\\n"
5500 " if (A) { \\\n"
5501 " B(); \\\n"
5502 " } \\\n"
5503 " C();\n"
5504 "#endif",
5505 style);
5506 style.IndentWidth = 8;
5507 verifyFormat("#ifdef foo\n"
5508 "#define bar() \\\n"
5509 " if (A) { \\\n"
5510 " B(); \\\n"
5511 " } \\\n"
5512 " C();\n"
5513 "#endif",
5514 style);
5515
5516 style.IndentWidth = 1;
5517 style.PPIndentWidth = 4;
5518 verifyFormat("#if 1\n"
5519 "#define X \\\n"
5520 " { \\\n"
5521 " x; \\\n"
5522 " x; \\\n"
5523 " }\n"
5524 "#endif",
5525 style);
5526 verifyFormat("#define X \\\n"
5527 " { \\\n"
5528 " x; \\\n"
5529 " x; \\\n"
5530 " }",
5531 style);
5532
5533 style.IndentWidth = 4;
5534 style.PPIndentWidth = 1;
5535 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
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 verifyFormat("#if abc\n"
5545 "# ifdef foo\n"
5546 "# define bar() \\\n"
5547 " if (A) { \\\n"
5548 " if (B) { \\\n"
5549 " C(); \\\n"
5550 " } \\\n"
5551 " } \\\n"
5552 " D();\n"
5553 "# endif\n"
5554 "#endif",
5555 style);
5556 verifyFormat("#ifndef foo\n"
5557 "#define foo\n"
5558 "if (emacs) {\n"
5559 "#ifdef is\n"
5560 "# define lit \\\n"
5561 " if (af) { \\\n"
5562 " return duh(); \\\n"
5563 " }\n"
5564 "#endif\n"
5565 "}\n"
5566 "#endif",
5567 style);
5568 verifyFormat("#define X \\\n"
5569 " { \\\n"
5570 " x; \\\n"
5571 " x; \\\n"
5572 " }",
5573 style);
5574
5575 style.PPIndentWidth = 2;
5576 style.IndentWidth = 8;
5577 verifyFormat("#ifdef foo\n"
5578 "# define bar() \\\n"
5579 " if (A) { \\\n"
5580 " B(); \\\n"
5581 " } \\\n"
5582 " C();\n"
5583 "#endif",
5584 style);
5585
5586 style.PPIndentWidth = 4;
5587 style.IndentWidth = 1;
5588 verifyFormat("#define X \\\n"
5589 " { \\\n"
5590 " x; \\\n"
5591 " x; \\\n"
5592 " }",
5593 style);
5594
5595 style.IndentWidth = 4;
5596 style.PPIndentWidth = 1;
5597 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5598 verifyFormat("if (emacs) {\n"
5599 "#ifdef is\n"
5600 " #define lit \\\n"
5601 " if (af) { \\\n"
5602 " return duh(); \\\n"
5603 " }\n"
5604 "#endif\n"
5605 "}",
5606 style);
5607 verifyFormat("#if abc\n"
5608 " #ifdef foo\n"
5609 " #define bar() \\\n"
5610 " if (A) { \\\n"
5611 " B(); \\\n"
5612 " } \\\n"
5613 " C();\n"
5614 " #endif\n"
5615 "#endif",
5616 style);
5617 verifyFormat("#if 1\n"
5618 " #define X \\\n"
5619 " { \\\n"
5620 " x; \\\n"
5621 " x; \\\n"
5622 " }\n"
5623 "#endif",
5624 style);
5625
5626 style.PPIndentWidth = 2;
5627 verifyFormat("#ifdef foo\n"
5628 " #define bar() \\\n"
5629 " if (A) { \\\n"
5630 " B(); \\\n"
5631 " } \\\n"
5632 " C();\n"
5633 "#endif",
5634 style);
5635
5636 style.PPIndentWidth = 4;
5637 style.IndentWidth = 1;
5638 verifyFormat("#if 1\n"
5639 " #define X \\\n"
5640 " { \\\n"
5641 " x; \\\n"
5642 " x; \\\n"
5643 " }\n"
5644 "#endif",
5645 style);
5646 }
5647
5648 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5649 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5650 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5651 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5652 // FIXME: We never break before the macro name.
5653 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5654
5655 verifyFormat("#define A A\n#define A A");
5656 verifyFormat("#define A(X) A\n#define A A");
5657
5658 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5659 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5660 }
5661
5662 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5663 verifyFormat("// somecomment\n"
5664 "#include \"a.h\"\n"
5665 "#define A( \\\n"
5666 " A, B)\n"
5667 "#include \"b.h\"\n"
5668 "// somecomment",
5669 " // somecomment\n"
5670 " #include \"a.h\"\n"
5671 "#define A(A,\\\n"
5672 " B)\n"
5673 " #include \"b.h\"\n"
5674 " // somecomment",
5675 getLLVMStyleWithColumns(13));
5676 }
5677
5678 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5679
5680 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5681 verifyFormat("#define A \\\n"
5682 " c; \\\n"
5683 " e;\n"
5684 "f;",
5685 "#define A c; e;\n"
5686 "f;",
5687 getLLVMStyleWithColumns(14));
5688 }
5689
5690 TEST_F(FormatTest, LayoutRemainingTokens) {
5691 verifyFormat("{\n"
5692 "}");
5693 }
5694
5695 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5696 verifyFormat("int x,\n"
5697 "#define A\n"
5698 " y;",
5699 "int x,\n#define A\ny;");
5700 }
5701
5702 TEST_F(FormatTest, HashInMacroDefinition) {
5703 verifyFormat("#define A(c) L#c");
5704 verifyFormat("#define A(c) u#c");
5705 verifyFormat("#define A(c) U#c");
5706 verifyFormat("#define A(c) u8#c");
5707 verifyFormat("#define A(c) LR#c");
5708 verifyFormat("#define A(c) uR#c");
5709 verifyFormat("#define A(c) UR#c");
5710 verifyFormat("#define A(c) u8R#c");
5711 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5712 verifyFormat("#define A \\\n"
5713 " { \\\n"
5714 " f(#c); \\\n"
5715 " }",
5716 getLLVMStyleWithColumns(11));
5717
5718 verifyFormat("#define A(X) \\\n"
5719 " void function##X()",
5720 getLLVMStyleWithColumns(22));
5721
5722 verifyFormat("#define A(a, b, c) \\\n"
5723 " void a##b##c()",
5724 getLLVMStyleWithColumns(22));
5725
5726 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5727
5728 #if 0
5729 // FIXME: The correct format is:
5730 verifyFormat("{\n"
5731 " {\n"
5732 "#define GEN_ID(_x) char *_x{#_x}\n"
5733 " GEN_ID(one);\n"
5734 " }\n"
5735 "}");
5736 #endif
5737 verifyFormat("{\n"
5738 " {\n"
5739 "#define GEN_ID(_x) \\\n"
5740 " char *_x { #_x }\n"
5741 " GEN_ID(one);\n"
5742 " }\n"
5743 "}",
5744 getGoogleStyle());
5745 }
5746
5747 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5748 verifyFormat("#define A (x)");
5749 verifyFormat("#define A(x)");
5750
5751 FormatStyle Style = getLLVMStyle();
5752 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5753 verifyFormat("#define true ((foo)1)", Style);
5754 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5755 verifyFormat("#define false((foo)0)", Style);
5756 }
5757
5758 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5759 verifyFormat("#define A b;",
5760 "#define A \\\n"
5761 " \\\n"
5762 " b;",
5763 getLLVMStyleWithColumns(25));
5764 verifyNoChange("#define A \\\n"
5765 " \\\n"
5766 " a; \\\n"
5767 " b;",
5768 getLLVMStyleWithColumns(11));
5769 verifyNoChange("#define A \\\n"
5770 " a; \\\n"
5771 " \\\n"
5772 " b;",
5773 getLLVMStyleWithColumns(11));
5774 }
5775
5776 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5777 verifyIncompleteFormat("#define A :");
5778 verifyFormat("#define SOMECASES \\\n"
5779 " case 1: \\\n"
5780 " case 2",
5781 getLLVMStyleWithColumns(20));
5782 verifyFormat("#define MACRO(a) \\\n"
5783 " if (a) \\\n"
5784 " f(); \\\n"
5785 " else \\\n"
5786 " g()",
5787 getLLVMStyleWithColumns(18));
5788 verifyFormat("#define A template <typename T>");
5789 verifyIncompleteFormat("#define STR(x) #x\n"
5790 "f(STR(this_is_a_string_literal{));");
5791 verifyFormat("#pragma omp threadprivate( \\\n"
5792 " y)), // expected-warning",
5793 getLLVMStyleWithColumns(28));
5794 verifyFormat("#d, = };");
5795 verifyFormat("#if \"a");
5796 verifyIncompleteFormat("({\n"
5797 "#define b \\\n"
5798 " } \\\n"
5799 " a\n"
5800 "a",
5801 getLLVMStyleWithColumns(15));
5802 verifyFormat("#define A \\\n"
5803 " { \\\n"
5804 " {\n"
5805 "#define B \\\n"
5806 " } \\\n"
5807 " }",
5808 getLLVMStyleWithColumns(15));
5809 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5810 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5811 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5812 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5813 verifyNoCrash("#else\n"
5814 "#else\n"
5815 "#endif\n"
5816 "#endif");
5817 verifyNoCrash("#else\n"
5818 "#if X\n"
5819 "#endif\n"
5820 "#endif");
5821 verifyNoCrash("#else\n"
5822 "#endif\n"
5823 "#if X\n"
5824 "#endif");
5825 verifyNoCrash("#if X\n"
5826 "#else\n"
5827 "#else\n"
5828 "#endif\n"
5829 "#endif");
5830 verifyNoCrash("#if X\n"
5831 "#elif Y\n"
5832 "#elif Y\n"
5833 "#endif\n"
5834 "#endif");
5835 verifyNoCrash("#endif\n"
5836 "#endif");
5837 verifyNoCrash("#endif\n"
5838 "#else");
5839 verifyNoCrash("#endif\n"
5840 "#elif Y");
5841 }
5842
5843 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5844 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5845 verifyFormat("class A : public QObject {\n"
5846 " Q_OBJECT\n"
5847 "\n"
5848 " A() {}\n"
5849 "};",
5850 "class A : public QObject {\n"
5851 " Q_OBJECT\n"
5852 "\n"
5853 " A() {\n}\n"
5854 "} ;");
5855 verifyFormat("MACRO\n"
5856 "/*static*/ int i;",
5857 "MACRO\n"
5858 " /*static*/ int i;");
5859 verifyFormat("SOME_MACRO\n"
5860 "namespace {\n"
5861 "void f();\n"
5862 "} // namespace",
5863 "SOME_MACRO\n"
5864 " namespace {\n"
5865 "void f( );\n"
5866 "} // namespace");
5867 // Only if the identifier contains at least 5 characters.
5868 verifyFormat("HTTP f();", "HTTP\nf();");
5869 verifyNoChange("MACRO\nf();");
5870 // Only if everything is upper case.
5871 verifyFormat("class A : public QObject {\n"
5872 " Q_Object A() {}\n"
5873 "};",
5874 "class A : public QObject {\n"
5875 " Q_Object\n"
5876 " A() {\n}\n"
5877 "} ;");
5878
5879 // Only if the next line can actually start an unwrapped line.
5880 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5881 "<< SomeThing;");
5882
5883 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5884 "(n, buffers))",
5885 getChromiumStyle(FormatStyle::LK_Cpp));
5886
5887 // See PR41483
5888 verifyNoChange("/**/ FOO(a)\n"
5889 "FOO(b)");
5890 }
5891
5892 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5893 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5894 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5895 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5896 "class X {};\n"
5897 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5898 "int *createScopDetectionPass() { return 0; }",
5899 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5900 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5901 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5902 " class X {};\n"
5903 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5904 " int *createScopDetectionPass() { return 0; }");
5905 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5906 // braces, so that inner block is indented one level more.
5907 verifyFormat("int q() {\n"
5908 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5909 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5910 " IPC_END_MESSAGE_MAP()\n"
5911 "}",
5912 "int q() {\n"
5913 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5914 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5915 " IPC_END_MESSAGE_MAP()\n"
5916 "}");
5917
5918 // Same inside macros.
5919 verifyFormat("#define LIST(L) \\\n"
5920 " L(A) \\\n"
5921 " L(B) \\\n"
5922 " L(C)",
5923 "#define LIST(L) \\\n"
5924 " L(A) \\\n"
5925 " L(B) \\\n"
5926 " L(C)",
5927 getGoogleStyle());
5928
5929 // These must not be recognized as macros.
5930 verifyFormat("int q() {\n"
5931 " f(x);\n"
5932 " f(x) {}\n"
5933 " f(x)->g();\n"
5934 " f(x)->*g();\n"
5935 " f(x).g();\n"
5936 " f(x) = x;\n"
5937 " f(x) += x;\n"
5938 " f(x) -= x;\n"
5939 " f(x) *= x;\n"
5940 " f(x) /= x;\n"
5941 " f(x) %= x;\n"
5942 " f(x) &= x;\n"
5943 " f(x) |= x;\n"
5944 " f(x) ^= x;\n"
5945 " f(x) >>= x;\n"
5946 " f(x) <<= x;\n"
5947 " f(x)[y].z();\n"
5948 " LOG(INFO) << x;\n"
5949 " ifstream(x) >> x;\n"
5950 "}",
5951 "int q() {\n"
5952 " f(x)\n;\n"
5953 " f(x)\n {}\n"
5954 " f(x)\n->g();\n"
5955 " f(x)\n->*g();\n"
5956 " f(x)\n.g();\n"
5957 " f(x)\n = x;\n"
5958 " f(x)\n += x;\n"
5959 " f(x)\n -= x;\n"
5960 " f(x)\n *= x;\n"
5961 " f(x)\n /= x;\n"
5962 " f(x)\n %= x;\n"
5963 " f(x)\n &= x;\n"
5964 " f(x)\n |= x;\n"
5965 " f(x)\n ^= x;\n"
5966 " f(x)\n >>= x;\n"
5967 " f(x)\n <<= x;\n"
5968 " f(x)\n[y].z();\n"
5969 " LOG(INFO)\n << x;\n"
5970 " ifstream(x)\n >> x;\n"
5971 "}");
5972 verifyFormat("int q() {\n"
5973 " F(x)\n"
5974 " if (1) {\n"
5975 " }\n"
5976 " F(x)\n"
5977 " while (1) {\n"
5978 " }\n"
5979 " F(x)\n"
5980 " G(x);\n"
5981 " F(x)\n"
5982 " try {\n"
5983 " Q();\n"
5984 " } catch (...) {\n"
5985 " }\n"
5986 "}",
5987 "int q() {\n"
5988 "F(x)\n"
5989 "if (1) {}\n"
5990 "F(x)\n"
5991 "while (1) {}\n"
5992 "F(x)\n"
5993 "G(x);\n"
5994 "F(x)\n"
5995 "try { Q(); } catch (...) {}\n"
5996 "}");
5997 verifyFormat("class A {\n"
5998 " A() : t(0) {}\n"
5999 " A(int i) noexcept() : {}\n"
6000 " A(X x)\n" // FIXME: function-level try blocks are broken.
6001 " try : t(0) {\n"
6002 " } catch (...) {\n"
6003 " }\n"
6004 "};",
6005 "class A {\n"
6006 " A()\n : t(0) {}\n"
6007 " A(int i)\n noexcept() : {}\n"
6008 " A(X x)\n"
6009 " try : t(0) {} catch (...) {}\n"
6010 "};");
6011 FormatStyle Style = getLLVMStyle();
6012 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6013 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6014 Style.BraceWrapping.AfterFunction = true;
6015 verifyFormat("void f()\n"
6016 "try\n"
6017 "{\n"
6018 "}",
6019 "void f() try {\n"
6020 "}",
6021 Style);
6022 verifyFormat("class SomeClass {\n"
6023 "public:\n"
6024 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6025 "};",
6026 "class SomeClass {\n"
6027 "public:\n"
6028 " SomeClass()\n"
6029 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6030 "};");
6031 verifyFormat("class SomeClass {\n"
6032 "public:\n"
6033 " SomeClass()\n"
6034 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6035 "};",
6036 "class SomeClass {\n"
6037 "public:\n"
6038 " SomeClass()\n"
6039 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6040 "};",
6041 getLLVMStyleWithColumns(40));
6042
6043 verifyFormat("MACRO(>)");
6044
6045 // Some macros contain an implicit semicolon.
6046 Style = getLLVMStyle();
6047 Style.StatementMacros.push_back("FOO");
6048 verifyFormat("FOO(a) int b = 0;");
6049 verifyFormat("FOO(a)\n"
6050 "int b = 0;",
6051 Style);
6052 verifyFormat("FOO(a);\n"
6053 "int b = 0;",
6054 Style);
6055 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6056 "int b = 0;",
6057 Style);
6058 verifyFormat("FOO()\n"
6059 "int b = 0;",
6060 Style);
6061 verifyFormat("FOO\n"
6062 "int b = 0;",
6063 Style);
6064 verifyFormat("void f() {\n"
6065 " FOO(a)\n"
6066 " return a;\n"
6067 "}",
6068 Style);
6069 verifyFormat("FOO(a)\n"
6070 "FOO(b)",
6071 Style);
6072 verifyFormat("int a = 0;\n"
6073 "FOO(b)\n"
6074 "int c = 0;",
6075 Style);
6076 verifyFormat("int a = 0;\n"
6077 "int x = FOO(a)\n"
6078 "int b = 0;",
6079 Style);
6080 verifyFormat("void foo(int a) { FOO(a) }\n"
6081 "uint32_t bar() {}",
6082 Style);
6083 }
6084
6085 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6086 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
6087
6088 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6089 ZeroColumn);
6090 }
6091
6092 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6093 verifyFormat("#define A \\\n"
6094 " f({ \\\n"
6095 " g(); \\\n"
6096 " });",
6097 getLLVMStyleWithColumns(11));
6098 }
6099
6100 TEST_F(FormatTest, IndentPreprocessorDirectives) {
6101 FormatStyle Style = getLLVMStyleWithColumns(40);
6102 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6103 verifyFormat("#ifdef _WIN32\n"
6104 "#define A 0\n"
6105 "#ifdef VAR2\n"
6106 "#define B 1\n"
6107 "#include <someheader.h>\n"
6108 "#define MACRO \\\n"
6109 " some_very_long_func_aaaaaaaaaa();\n"
6110 "#endif\n"
6111 "#else\n"
6112 "#define A 1\n"
6113 "#endif",
6114 Style);
6115 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6116 verifyFormat("#if 1\n"
6117 "# define __STR(x) #x\n"
6118 "#endif",
6119 Style);
6120 verifyFormat("#ifdef _WIN32\n"
6121 "# define A 0\n"
6122 "# ifdef VAR2\n"
6123 "# define B 1\n"
6124 "# include <someheader.h>\n"
6125 "# define MACRO \\\n"
6126 " some_very_long_func_aaaaaaaaaa();\n"
6127 "# endif\n"
6128 "#else\n"
6129 "# define A 1\n"
6130 "#endif",
6131 Style);
6132 verifyFormat("#if A\n"
6133 "# define MACRO \\\n"
6134 " void a(int x) { \\\n"
6135 " b(); \\\n"
6136 " c(); \\\n"
6137 " d(); \\\n"
6138 " e(); \\\n"
6139 " f(); \\\n"
6140 " }\n"
6141 "#endif",
6142 Style);
6143 // Comments before include guard.
6144 verifyFormat("// file comment\n"
6145 "// file comment\n"
6146 "#ifndef HEADER_H\n"
6147 "#define HEADER_H\n"
6148 "code();\n"
6149 "#endif",
6150 Style);
6151 // Test with include guards.
6152 verifyFormat("#ifndef HEADER_H\n"
6153 "#define HEADER_H\n"
6154 "code();\n"
6155 "#endif",
6156 Style);
6157 // Include guards must have a #define with the same variable immediately
6158 // after #ifndef.
6159 verifyFormat("#ifndef NOT_GUARD\n"
6160 "# define FOO\n"
6161 "code();\n"
6162 "#endif",
6163 Style);
6164
6165 // Include guards must cover the entire file.
6166 verifyFormat("code();\n"
6167 "code();\n"
6168 "#ifndef NOT_GUARD\n"
6169 "# define NOT_GUARD\n"
6170 "code();\n"
6171 "#endif",
6172 Style);
6173 verifyFormat("#ifndef NOT_GUARD\n"
6174 "# define NOT_GUARD\n"
6175 "code();\n"
6176 "#endif\n"
6177 "code();",
6178 Style);
6179 // Test with trailing blank lines.
6180 verifyFormat("#ifndef HEADER_H\n"
6181 "#define HEADER_H\n"
6182 "code();\n"
6183 "#endif",
6184 Style);
6185 // Include guards don't have #else.
6186 verifyFormat("#ifndef NOT_GUARD\n"
6187 "# define NOT_GUARD\n"
6188 "code();\n"
6189 "#else\n"
6190 "#endif",
6191 Style);
6192 verifyFormat("#ifndef NOT_GUARD\n"
6193 "# define NOT_GUARD\n"
6194 "code();\n"
6195 "#elif FOO\n"
6196 "#endif",
6197 Style);
6198 // Non-identifier #define after potential include guard.
6199 verifyFormat("#ifndef FOO\n"
6200 "# define 1\n"
6201 "#endif",
6202 Style);
6203 // #if closes past last non-preprocessor line.
6204 verifyFormat("#ifndef FOO\n"
6205 "#define FOO\n"
6206 "#if 1\n"
6207 "int i;\n"
6208 "# define A 0\n"
6209 "#endif\n"
6210 "#endif",
6211 Style);
6212 // Don't crash if there is an #elif directive without a condition.
6213 verifyFormat("#if 1\n"
6214 "int x;\n"
6215 "#elif\n"
6216 "int y;\n"
6217 "#else\n"
6218 "int z;\n"
6219 "#endif",
6220 Style);
6221 // FIXME: This doesn't handle the case where there's code between the
6222 // #ifndef and #define but all other conditions hold. This is because when
6223 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6224 // previous code line yet, so we can't detect it.
6225 verifyFormat("#ifndef NOT_GUARD\n"
6226 "code();\n"
6227 "#define NOT_GUARD\n"
6228 "code();\n"
6229 "#endif",
6230 "#ifndef NOT_GUARD\n"
6231 "code();\n"
6232 "# define NOT_GUARD\n"
6233 "code();\n"
6234 "#endif",
6235 Style);
6236 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6237 // be outside an include guard. Examples are #pragma once and
6238 // #pragma GCC diagnostic, or anything else that does not change the meaning
6239 // of the file if it's included multiple times.
6240 verifyFormat("#ifdef WIN32\n"
6241 "# pragma once\n"
6242 "#endif\n"
6243 "#ifndef HEADER_H\n"
6244 "# define HEADER_H\n"
6245 "code();\n"
6246 "#endif",
6247 "#ifdef WIN32\n"
6248 "# pragma once\n"
6249 "#endif\n"
6250 "#ifndef HEADER_H\n"
6251 "#define HEADER_H\n"
6252 "code();\n"
6253 "#endif",
6254 Style);
6255 // FIXME: This does not detect when there is a single non-preprocessor line
6256 // in front of an include-guard-like structure where other conditions hold
6257 // because ScopedLineState hides the line.
6258 verifyFormat("code();\n"
6259 "#ifndef HEADER_H\n"
6260 "#define HEADER_H\n"
6261 "code();\n"
6262 "#endif",
6263 "code();\n"
6264 "#ifndef HEADER_H\n"
6265 "# define HEADER_H\n"
6266 "code();\n"
6267 "#endif",
6268 Style);
6269 // Keep comments aligned with #, otherwise indent comments normally. These
6270 // tests cannot use verifyFormat because messUp manipulates leading
6271 // whitespace.
6272 {
6273 const char *Expected = ""
6274 "void f() {\n"
6275 "#if 1\n"
6276 "// Preprocessor aligned.\n"
6277 "# define A 0\n"
6278 " // Code. Separated by blank line.\n"
6279 "\n"
6280 "# define B 0\n"
6281 " // Code. Not aligned with #\n"
6282 "# define C 0\n"
6283 "#endif";
6284 const char *ToFormat = ""
6285 "void f() {\n"
6286 "#if 1\n"
6287 "// Preprocessor aligned.\n"
6288 "# define A 0\n"
6289 "// Code. Separated by blank line.\n"
6290 "\n"
6291 "# define B 0\n"
6292 " // Code. Not aligned with #\n"
6293 "# define C 0\n"
6294 "#endif";
6295 verifyFormat(Expected, ToFormat, Style);
6296 verifyNoChange(Expected, Style);
6297 }
6298 // Keep block quotes aligned.
6299 {
6300 const char *Expected = ""
6301 "void f() {\n"
6302 "#if 1\n"
6303 "/* Preprocessor aligned. */\n"
6304 "# define A 0\n"
6305 " /* Code. Separated by blank line. */\n"
6306 "\n"
6307 "# define B 0\n"
6308 " /* Code. Not aligned with # */\n"
6309 "# define C 0\n"
6310 "#endif";
6311 const char *ToFormat = ""
6312 "void f() {\n"
6313 "#if 1\n"
6314 "/* Preprocessor aligned. */\n"
6315 "# define A 0\n"
6316 "/* Code. Separated by blank line. */\n"
6317 "\n"
6318 "# define B 0\n"
6319 " /* Code. Not aligned with # */\n"
6320 "# define C 0\n"
6321 "#endif";
6322 verifyFormat(Expected, ToFormat, Style);
6323 verifyNoChange(Expected, Style);
6324 }
6325 // Keep comments aligned with un-indented directives.
6326 {
6327 const char *Expected = ""
6328 "void f() {\n"
6329 "// Preprocessor aligned.\n"
6330 "#define A 0\n"
6331 " // Code. Separated by blank line.\n"
6332 "\n"
6333 "#define B 0\n"
6334 " // Code. Not aligned with #\n"
6335 "#define C 0\n";
6336 const char *ToFormat = ""
6337 "void f() {\n"
6338 "// Preprocessor aligned.\n"
6339 "#define A 0\n"
6340 "// Code. Separated by blank line.\n"
6341 "\n"
6342 "#define B 0\n"
6343 " // Code. Not aligned with #\n"
6344 "#define C 0\n";
6345 verifyFormat(Expected, ToFormat, Style);
6346 verifyNoChange(Expected, Style);
6347 }
6348 // Test AfterHash with tabs.
6349 {
6350 FormatStyle Tabbed = Style;
6351 Tabbed.UseTab = FormatStyle::UT_Always;
6352 Tabbed.IndentWidth = 8;
6353 Tabbed.TabWidth = 8;
6354 verifyFormat("#ifdef _WIN32\n"
6355 "#\tdefine A 0\n"
6356 "#\tifdef VAR2\n"
6357 "#\t\tdefine B 1\n"
6358 "#\t\tinclude <someheader.h>\n"
6359 "#\t\tdefine MACRO \\\n"
6360 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6361 "#\tendif\n"
6362 "#else\n"
6363 "#\tdefine A 1\n"
6364 "#endif",
6365 Tabbed);
6366 }
6367
6368 // Regression test: Multiline-macro inside include guards.
6369 verifyFormat("#ifndef HEADER_H\n"
6370 "#define HEADER_H\n"
6371 "#define A() \\\n"
6372 " int i; \\\n"
6373 " int j;\n"
6374 "#endif // HEADER_H",
6375 getLLVMStyleWithColumns(20));
6376
6377 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6378 // Basic before hash indent tests
6379 verifyFormat("#ifdef _WIN32\n"
6380 " #define A 0\n"
6381 " #ifdef VAR2\n"
6382 " #define B 1\n"
6383 " #include <someheader.h>\n"
6384 " #define MACRO \\\n"
6385 " some_very_long_func_aaaaaaaaaa();\n"
6386 " #endif\n"
6387 "#else\n"
6388 " #define A 1\n"
6389 "#endif",
6390 Style);
6391 verifyFormat("#if A\n"
6392 " #define MACRO \\\n"
6393 " void a(int x) { \\\n"
6394 " b(); \\\n"
6395 " c(); \\\n"
6396 " d(); \\\n"
6397 " e(); \\\n"
6398 " f(); \\\n"
6399 " }\n"
6400 "#endif",
6401 Style);
6402 // Keep comments aligned with indented directives. These
6403 // tests cannot use verifyFormat because messUp manipulates leading
6404 // whitespace.
6405 {
6406 const char *Expected = "void f() {\n"
6407 "// Aligned to preprocessor.\n"
6408 "#if 1\n"
6409 " // Aligned to code.\n"
6410 " int a;\n"
6411 " #if 1\n"
6412 " // Aligned to preprocessor.\n"
6413 " #define A 0\n"
6414 " // Aligned to code.\n"
6415 " int b;\n"
6416 " #endif\n"
6417 "#endif\n"
6418 "}";
6419 const char *ToFormat = "void f() {\n"
6420 "// Aligned to preprocessor.\n"
6421 "#if 1\n"
6422 "// Aligned to code.\n"
6423 "int a;\n"
6424 "#if 1\n"
6425 "// Aligned to preprocessor.\n"
6426 "#define A 0\n"
6427 "// Aligned to code.\n"
6428 "int b;\n"
6429 "#endif\n"
6430 "#endif\n"
6431 "}";
6432 verifyFormat(Expected, ToFormat, Style);
6433 verifyNoChange(Expected, Style);
6434 }
6435 {
6436 const char *Expected = "void f() {\n"
6437 "/* Aligned to preprocessor. */\n"
6438 "#if 1\n"
6439 " /* Aligned to code. */\n"
6440 " int a;\n"
6441 " #if 1\n"
6442 " /* Aligned to preprocessor. */\n"
6443 " #define A 0\n"
6444 " /* Aligned to code. */\n"
6445 " int b;\n"
6446 " #endif\n"
6447 "#endif\n"
6448 "}";
6449 const char *ToFormat = "void f() {\n"
6450 "/* Aligned to preprocessor. */\n"
6451 "#if 1\n"
6452 "/* Aligned to code. */\n"
6453 "int a;\n"
6454 "#if 1\n"
6455 "/* Aligned to preprocessor. */\n"
6456 "#define A 0\n"
6457 "/* Aligned to code. */\n"
6458 "int b;\n"
6459 "#endif\n"
6460 "#endif\n"
6461 "}";
6462 verifyFormat(Expected, ToFormat, Style);
6463 verifyNoChange(Expected, Style);
6464 }
6465
6466 // Test single comment before preprocessor
6467 verifyFormat("// Comment\n"
6468 "\n"
6469 "#if 1\n"
6470 "#endif",
6471 Style);
6472 }
6473
6474 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6475 FormatStyle Style = getLLVMStyle();
6476 Style.AlignConsecutiveAssignments.Enabled = true;
6477 Style.AlignConsecutiveDeclarations.Enabled = true;
6478
6479 // Test with just #if blocks.
6480 verifyFormat("void f1() {\n"
6481 "#if 1\n"
6482 " int foo = 1;\n"
6483 " int foobar = 2;\n"
6484 "#endif\n"
6485 "}\n"
6486 "#if 1\n"
6487 "int baz = 3;\n"
6488 "#endif\n"
6489 "void f2() {\n"
6490 "#if 1\n"
6491 " char *foobarbaz = \"foobarbaz\";\n"
6492 " int quux = 4;\n"
6493 "}",
6494 Style);
6495
6496 // Test with just #else blocks.
6497 verifyFormat("void f1() {\n"
6498 "#if 1\n"
6499 "#else\n"
6500 " int foo = 1;\n"
6501 " int foobar = 2;\n"
6502 "#endif\n"
6503 "}\n"
6504 "#if 1\n"
6505 "#else\n"
6506 "int baz = 3;\n"
6507 "#endif\n"
6508 "void f2() {\n"
6509 "#if 1\n"
6510 "#else\n"
6511 " char *foobarbaz = \"foobarbaz\";\n"
6512 " int quux = 4;\n"
6513 "}",
6514 Style);
6515 verifyFormat("auto foo = [] { return; };\n"
6516 "#if FOO\n"
6517 "#else\n"
6518 "count = bar;\n"
6519 "mbid = bid;\n"
6520 "#endif",
6521 Style);
6522
6523 // Test with a mix of #if and #else blocks.
6524 verifyFormat("void f1() {\n"
6525 "#if 1\n"
6526 "#else\n"
6527 " int foo = 1;\n"
6528 " int foobar = 2;\n"
6529 "#endif\n"
6530 "}\n"
6531 "#if 1\n"
6532 "int baz = 3;\n"
6533 "#endif\n"
6534 "void f2() {\n"
6535 "#if 1\n"
6536 "#else\n"
6537 " // prevent alignment with #else in f1\n"
6538 " char *foobarbaz = \"foobarbaz\";\n"
6539 " int quux = 4;\n"
6540 "}",
6541 Style);
6542
6543 // Test with nested #if and #else blocks.
6544 verifyFormat("void f1() {\n"
6545 "#if 1\n"
6546 "#else\n"
6547 "#if 2\n"
6548 "#else\n"
6549 " int foo = 1;\n"
6550 " int foobar = 2;\n"
6551 "#endif\n"
6552 "#endif\n"
6553 "}\n"
6554 "#if 1\n"
6555 "#else\n"
6556 "#if 2\n"
6557 "int baz = 3;\n"
6558 "#endif\n"
6559 "#endif\n"
6560 "void f2() {\n"
6561 "#if 1\n"
6562 "#if 2\n"
6563 "#else\n"
6564 " // prevent alignment with #else in f1\n"
6565 " char *foobarbaz = \"foobarbaz\";\n"
6566 " int quux = 4;\n"
6567 "#endif\n"
6568 "#endif\n"
6569 "}",
6570 Style);
6571
6572 verifyFormat("#if FOO\n"
6573 "int a = 1;\n"
6574 "#else\n"
6575 "int ab = 2;\n"
6576 "#endif\n"
6577 "#ifdef BAR\n"
6578 "int abc = 3;\n"
6579 "#elifdef BAZ\n"
6580 "int abcd = 4;\n"
6581 "#endif",
6582 Style);
6583
6584 verifyFormat("void f() {\n"
6585 " if (foo) {\n"
6586 "#if FOO\n"
6587 " int a = 1;\n"
6588 "#else\n"
6589 " bool a = true;\n"
6590 "#endif\n"
6591 " int abc = 3;\n"
6592 "#ifndef BAR\n"
6593 " int abcd = 4;\n"
6594 "#elif BAZ\n"
6595 " bool abcd = true;\n"
6596 "#endif\n"
6597 " }\n"
6598 "}",
6599 Style);
6600
6601 verifyFormat("void f() {\n"
6602 "#if FOO\n"
6603 " a = 1;\n"
6604 "#else\n"
6605 " ab = 2;\n"
6606 "#endif\n"
6607 "}\n"
6608 "void g() {\n"
6609 "#if BAR\n"
6610 " abc = 3;\n"
6611 "#elifndef BAZ\n"
6612 " abcd = 4;\n"
6613 "#endif\n"
6614 "}",
6615 Style);
6616 }
6617
6618 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6619 verifyFormat("{\n"
6620 " {\n"
6621 " a #c;\n"
6622 " }\n"
6623 "}");
6624 }
6625
6626 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6627 verifyFormat("#define A \\\n { \\\n {\nint i;",
6628 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6629 verifyFormat("#define A \\\n } \\\n }\nint i;",
6630 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6631 }
6632
6633 TEST_F(FormatTest, EscapedNewlines) {
6634 FormatStyle Narrow = getLLVMStyleWithColumns(11);
6635 verifyFormat("#define A \\\n int i; \\\n int j;",
6636 "#define A \\\nint i;\\\n int j;", Narrow);
6637 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6638 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6639 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6640 verifyNoChange("<a\n\\\\\n>");
6641
6642 FormatStyle AlignLeft = getLLVMStyle();
6643 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6644 verifyFormat("#define MACRO(x) \\\n"
6645 "private: \\\n"
6646 " int x(int a);",
6647 AlignLeft);
6648
6649 // CRLF line endings
6650 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6651 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6652 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6653 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6654 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6655 verifyNoChange("<a\r\n\\\\\r\n>");
6656 verifyFormat("#define MACRO(x) \\\r\n"
6657 "private: \\\r\n"
6658 " int x(int a);",
6659 AlignLeft);
6660
6661 constexpr StringRef Code{"#define A \\\n"
6662 " int a123; \\\n"
6663 " int a; \\\n"
6664 " int a1234;"};
6665 verifyFormat(Code, AlignLeft);
6666
6667 constexpr StringRef Code2{"#define A \\\n"
6668 " int a123; \\\n"
6669 " int a; \\\n"
6670 " int a1234;"};
6671 auto LastLine = getLLVMStyle();
6672 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6673 verifyFormat(Code2, LastLine);
6674
6675 LastLine.ColumnLimit = 13;
6676 verifyFormat(Code, LastLine);
6677
6678 LastLine.ColumnLimit = 0;
6679 verifyFormat(Code2, LastLine);
6680
6681 FormatStyle DontAlign = getLLVMStyle();
6682 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6683 DontAlign.MaxEmptyLinesToKeep = 3;
6684 // FIXME: can't use verifyFormat here because the newline before
6685 // "public:" is not inserted the first time it's reformatted
6686 verifyNoChange("#define A \\\n"
6687 " class Foo { \\\n"
6688 " void bar(); \\\n"
6689 "\\\n"
6690 "\\\n"
6691 "\\\n"
6692 " public: \\\n"
6693 " void baz(); \\\n"
6694 " };",
6695 DontAlign);
6696 }
6697
6698 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6699 verifyFormat("#define A \\\n"
6700 " int v( \\\n"
6701 " a); \\\n"
6702 " int i;",
6703 getLLVMStyleWithColumns(11));
6704 }
6705
6706 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6707 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6708 " \\\n"
6709 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6710 "\n"
6711 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6712 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6713 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6714 "\\\n"
6715 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6716 " \n"
6717 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6718 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6719 }
6720
6721 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6722 verifyFormat("int\n"
6723 "#define A\n"
6724 " a;",
6725 "int\n#define A\na;");
6726 verifyFormat("functionCallTo(\n"
6727 " someOtherFunction(\n"
6728 " withSomeParameters, whichInSequence,\n"
6729 " areLongerThanALine(andAnotherCall,\n"
6730 "#define A B\n"
6731 " withMoreParamters,\n"
6732 " whichStronglyInfluenceTheLayout),\n"
6733 " andMoreParameters),\n"
6734 " trailing);",
6735 getLLVMStyleWithColumns(69));
6736 verifyFormat("Foo::Foo()\n"
6737 "#ifdef BAR\n"
6738 " : baz(0)\n"
6739 "#endif\n"
6740 "{\n"
6741 "}");
6742 verifyFormat("void f() {\n"
6743 " if (true)\n"
6744 "#ifdef A\n"
6745 " f(42);\n"
6746 " x();\n"
6747 "#else\n"
6748 " g();\n"
6749 " x();\n"
6750 "#endif\n"
6751 "}");
6752 verifyFormat("void f(param1, param2,\n"
6753 " param3,\n"
6754 "#ifdef A\n"
6755 " param4(param5,\n"
6756 "#ifdef A1\n"
6757 " param6,\n"
6758 "#ifdef A2\n"
6759 " param7),\n"
6760 "#else\n"
6761 " param8),\n"
6762 " param9,\n"
6763 "#endif\n"
6764 " param10,\n"
6765 "#endif\n"
6766 " param11)\n"
6767 "#else\n"
6768 " param12)\n"
6769 "#endif\n"
6770 "{\n"
6771 " x();\n"
6772 "}",
6773 getLLVMStyleWithColumns(28));
6774 verifyFormat("#if 1\n"
6775 "int i;");
6776 verifyFormat("#if 1\n"
6777 "#endif\n"
6778 "#if 1\n"
6779 "#else\n"
6780 "#endif");
6781 verifyFormat("DEBUG({\n"
6782 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6784 "});\n"
6785 "#if a\n"
6786 "#else\n"
6787 "#endif");
6788
6789 verifyIncompleteFormat("void f(\n"
6790 "#if A\n"
6791 ");\n"
6792 "#else\n"
6793 "#endif");
6794
6795 // Verify that indentation is correct when there is an `#if 0` with an
6796 // `#else`.
6797 verifyFormat("#if 0\n"
6798 "{\n"
6799 "#else\n"
6800 "{\n"
6801 "#endif\n"
6802 " x;\n"
6803 "}");
6804
6805 verifyFormat("#if 0\n"
6806 "#endif\n"
6807 "#if X\n"
6808 "int something_fairly_long; // Align here please\n"
6809 "#endif // Should be aligned");
6810
6811 verifyFormat("#if 0\n"
6812 "#endif\n"
6813 "#if X\n"
6814 "#else // Align\n"
6815 ";\n"
6816 "#endif // Align");
6817
6818 verifyFormat("void SomeFunction(int param1,\n"
6819 " template <\n"
6820 "#ifdef A\n"
6821 "#if 0\n"
6822 "#endif\n"
6823 " MyType<Some>>\n"
6824 "#else\n"
6825 " Type1, Type2>\n"
6826 "#endif\n"
6827 " param2,\n"
6828 " param3) {\n"
6829 " f();\n"
6830 "}");
6831 }
6832
6833 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6834 verifyFormat("#endif\n"
6835 "#if B");
6836 }
6837
6838 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6839 FormatStyle SingleLine = getLLVMStyle();
6840 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6841 verifyFormat("#if 0\n"
6842 "#elif 1\n"
6843 "#endif\n"
6844 "void foo() {\n"
6845 " if (test) foo2();\n"
6846 "}",
6847 SingleLine);
6848 }
6849
6850 TEST_F(FormatTest, LayoutBlockInsideParens) {
6851 verifyFormat("functionCall({ int i; });");
6852 verifyFormat("functionCall({\n"
6853 " int i;\n"
6854 " int j;\n"
6855 "});");
6856 verifyFormat("functionCall(\n"
6857 " {\n"
6858 " int i;\n"
6859 " int j;\n"
6860 " },\n"
6861 " aaaa, bbbb, cccc);");
6862 verifyFormat("functionA(functionB({\n"
6863 " int i;\n"
6864 " int j;\n"
6865 " }),\n"
6866 " aaaa, bbbb, cccc);");
6867 verifyFormat("functionCall(\n"
6868 " {\n"
6869 " int i;\n"
6870 " int j;\n"
6871 " },\n"
6872 " aaaa, bbbb, // comment\n"
6873 " cccc);");
6874 verifyFormat("functionA(functionB({\n"
6875 " int i;\n"
6876 " int j;\n"
6877 " }),\n"
6878 " aaaa, bbbb, // comment\n"
6879 " cccc);");
6880 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6881 verifyFormat("functionCall(aaaa, bbbb, {\n"
6882 " int i;\n"
6883 " int j;\n"
6884 "});");
6885 verifyFormat(
6886 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6887 " {\n"
6888 " int i; // break\n"
6889 " },\n"
6890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6891 " ccccccccccccccccc));");
6892 verifyFormat("DEBUG({\n"
6893 " if (a)\n"
6894 " f();\n"
6895 "});");
6896 }
6897
6898 TEST_F(FormatTest, LayoutBlockInsideStatement) {
6899 verifyFormat("SOME_MACRO { int i; }\n"
6900 "int i;",
6901 " SOME_MACRO {int i;} int i;");
6902 }
6903
6904 TEST_F(FormatTest, LayoutNestedBlocks) {
6905 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6906 " struct s {\n"
6907 " int i;\n"
6908 " };\n"
6909 " s kBitsToOs[] = {{10}};\n"
6910 " for (int i = 0; i < 10; ++i)\n"
6911 " return;\n"
6912 "}");
6913 verifyFormat("call(parameter, {\n"
6914 " something();\n"
6915 " // Comment using all columns.\n"
6916 " somethingelse();\n"
6917 "});",
6918 getLLVMStyleWithColumns(40));
6919 verifyFormat("DEBUG( //\n"
6920 " { f(); }, a);");
6921 verifyFormat("DEBUG( //\n"
6922 " {\n"
6923 " f(); //\n"
6924 " },\n"
6925 " a);");
6926
6927 verifyFormat("call(parameter, {\n"
6928 " something();\n"
6929 " // Comment too\n"
6930 " // looooooooooong.\n"
6931 " somethingElse();\n"
6932 "});",
6933 "call(parameter, {\n"
6934 " something();\n"
6935 " // Comment too looooooooooong.\n"
6936 " somethingElse();\n"
6937 "});",
6938 getLLVMStyleWithColumns(29));
6939 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6940 verifyFormat("DEBUG({ // comment\n"
6941 " int i;\n"
6942 "});",
6943 "DEBUG({ // comment\n"
6944 "int i;\n"
6945 "});");
6946 verifyFormat("DEBUG({\n"
6947 " int i;\n"
6948 "\n"
6949 " // comment\n"
6950 " int j;\n"
6951 "});",
6952 "DEBUG({\n"
6953 " int i;\n"
6954 "\n"
6955 " // comment\n"
6956 " int j;\n"
6957 "});");
6958
6959 verifyFormat("DEBUG({\n"
6960 " if (a)\n"
6961 " return;\n"
6962 "});");
6963 verifyGoogleFormat("DEBUG({\n"
6964 " if (a) return;\n"
6965 "});");
6966 FormatStyle Style = getGoogleStyle();
6967 Style.ColumnLimit = 45;
6968 verifyFormat("Debug(\n"
6969 " aaaaa,\n"
6970 " {\n"
6971 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6972 " },\n"
6973 " a);",
6974 Style);
6975
6976 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6977
6978 verifyNoCrash("^{v^{a}}");
6979 }
6980
6981 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6982 verifyFormat("#define MACRO() \\\n"
6983 " Debug(aaa, /* force line break */ \\\n"
6984 " { \\\n"
6985 " int i; \\\n"
6986 " int j; \\\n"
6987 " })",
6988 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6989 " { int i; int j; })",
6990 getGoogleStyle());
6991
6992 verifyFormat("#define A \\\n"
6993 " [] { \\\n"
6994 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6995 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6996 " }",
6997 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6998 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6999 getGoogleStyle());
7000 }
7001
7002 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
7003 verifyFormat("enum E {};");
7004 verifyFormat("enum E {}");
7005 FormatStyle Style = getLLVMStyle();
7006 Style.SpaceInEmptyBlock = true;
7007 verifyFormat("void f() { }", "void f() {}", Style);
7008 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
7009 verifyFormat("{ }", Style);
7010 verifyFormat("while (true) { }", "while (true) {}", Style);
7011 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7012 Style.BraceWrapping.BeforeElse = false;
7013 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
7014 verifyFormat("if (a)\n"
7015 "{\n"
7016 "} else if (b)\n"
7017 "{\n"
7018 "} else\n"
7019 "{ }",
7020 Style);
7021 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
7022 verifyFormat("if (a) {\n"
7023 "} else if (b) {\n"
7024 "} else {\n"
7025 "}",
7026 Style);
7027 Style.BraceWrapping.BeforeElse = true;
7028 verifyFormat("if (a) { }\n"
7029 "else if (b) { }\n"
7030 "else { }",
7031 Style);
7032
7033 Style = getLLVMStyle(FormatStyle::LK_CSharp);
7034 Style.SpaceInEmptyBlock = true;
7035 verifyFormat("Event += () => { };", Style);
7036 }
7037
7038 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
7039 FormatStyle Style = getLLVMStyle();
7040 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
7041 Style.MacroBlockEnd = "^[A-Z_]+_END$";
7042 verifyFormat("FOO_BEGIN\n"
7043 " FOO_ENTRY\n"
7044 "FOO_END",
7045 Style);
7046 verifyFormat("FOO_BEGIN\n"
7047 " NESTED_FOO_BEGIN\n"
7048 " NESTED_FOO_ENTRY\n"
7049 " NESTED_FOO_END\n"
7050 "FOO_END",
7051 Style);
7052 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7053 " int x;\n"
7054 " x = 1;\n"
7055 "FOO_END(Baz)",
7056 Style);
7057
7058 Style.RemoveBracesLLVM = true;
7059 verifyNoCrash("for (;;)\n"
7060 " FOO_BEGIN\n"
7061 " foo();\n"
7062 " FOO_END",
7063 Style);
7064 }
7065
7066 //===----------------------------------------------------------------------===//
7067 // Line break tests.
7068 //===----------------------------------------------------------------------===//
7069
7070 TEST_F(FormatTest, PreventConfusingIndents) {
7071 verifyFormat(
7072 "void f() {\n"
7073 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7074 " parameter, parameter, parameter)),\n"
7075 " SecondLongCall(parameter));\n"
7076 "}");
7077 verifyFormat(
7078 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7079 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7081 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7082 verifyFormat(
7083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7084 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7085 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7086 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7087 verifyFormat(
7088 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7089 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7091 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7092 verifyFormat("int a = bbbb && ccc &&\n"
7093 " fffff(\n"
7094 "#define A Just forcing a new line\n"
7095 " ddd);");
7096 }
7097
7098 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7099 verifyFormat(
7100 "bool aaaaaaa =\n"
7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7102 " bbbbbbbb();");
7103 verifyFormat(
7104 "bool aaaaaaa =\n"
7105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7106 " bbbbbbbb();");
7107
7108 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7110 " ccccccccc == ddddddddddd;");
7111 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7113 " ccccccccc == ddddddddddd;");
7114 verifyFormat(
7115 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7117 " ccccccccc == ddddddddddd;");
7118
7119 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7120 " aaaaaa) &&\n"
7121 " bbbbbb && cccccc;");
7122 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7123 " aaaaaa) >>\n"
7124 " bbbbbb;");
7125 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7126 " SourceMgr.getSpellingColumnNumber(\n"
7127 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7128 " 1);");
7129
7130 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7131 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7132 " cccccc) {\n}");
7133 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7134 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7135 " cccccc) {\n}");
7136 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7137 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7138 " cccccc) {\n}");
7139 verifyFormat("b = a &&\n"
7140 " // Comment\n"
7141 " b.c && d;");
7142
7143 // If the LHS of a comparison is not a binary expression itself, the
7144 // additional linebreak confuses many people.
7145 verifyFormat(
7146 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7148 "}");
7149 verifyFormat(
7150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7152 "}");
7153 verifyFormat(
7154 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7156 "}");
7157 verifyFormat(
7158 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7160 "}");
7161 // Even explicit parentheses stress the precedence enough to make the
7162 // additional break unnecessary.
7163 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7165 "}");
7166 // This cases is borderline, but with the indentation it is still readable.
7167 verifyFormat(
7168 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7169 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7171 "}",
7172 getLLVMStyleWithColumns(75));
7173
7174 // If the LHS is a binary expression, we should still use the additional break
7175 // as otherwise the formatting hides the operator precedence.
7176 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7178 " 5) {\n"
7179 "}");
7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7182 " 5) {\n"
7183 "}");
7184
7185 FormatStyle OnePerLine = getLLVMStyle();
7186 OnePerLine.BinPackParameters = false;
7187 verifyFormat(
7188 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7191 OnePerLine);
7192
7193 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7194 " .aaa(aaaaaaaaaaaaa) *\n"
7195 " aaaaaaa +\n"
7196 " aaaaaaa;",
7197 getLLVMStyleWithColumns(40));
7198 }
7199
7200 TEST_F(FormatTest, ExpressionIndentation) {
7201 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7205 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7206 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7209 " ccccccccccccccccccccccccccccccccccccccccc;");
7210 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7213 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7214 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7221 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7222 verifyFormat("if () {\n"
7223 "} else if (aaaaa && bbbbb > // break\n"
7224 " ccccc) {\n"
7225 "}");
7226 verifyFormat("if () {\n"
7227 "} else if constexpr (aaaaa && bbbbb > // break\n"
7228 " ccccc) {\n"
7229 "}");
7230 verifyFormat("if () {\n"
7231 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7232 " ccccc) {\n"
7233 "}");
7234 verifyFormat("if () {\n"
7235 "} else if (aaaaa &&\n"
7236 " bbbbb > // break\n"
7237 " ccccc &&\n"
7238 " ddddd) {\n"
7239 "}");
7240
7241 // Presence of a trailing comment used to change indentation of b.
7242 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7243 " b;\n"
7244 "return aaaaaaaaaaaaaaaaaaa +\n"
7245 " b; //",
7246 getLLVMStyleWithColumns(30));
7247 }
7248
7249 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7250 // Not sure what the best system is here. Like this, the LHS can be found
7251 // immediately above an operator (everything with the same or a higher
7252 // indent). The RHS is aligned right of the operator and so compasses
7253 // everything until something with the same indent as the operator is found.
7254 // FIXME: Is this a good system?
7255 FormatStyle Style = getLLVMStyle();
7256 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7257 verifyFormat(
7258 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7259 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7260 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7261 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7262 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7264 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7265 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7266 " > ccccccccccccccccccccccccccccccccccccccccc;",
7267 Style);
7268 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7269 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7270 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7271 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7272 Style);
7273 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7274 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7275 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7276 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7277 Style);
7278 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7279 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7281 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7282 Style);
7283 verifyFormat("if () {\n"
7284 "} else if (aaaaa\n"
7285 " && bbbbb // break\n"
7286 " > ccccc) {\n"
7287 "}",
7288 Style);
7289 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7290 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7291 Style);
7292 verifyFormat("return (a)\n"
7293 " // comment\n"
7294 " + b;",
7295 Style);
7296 verifyFormat(
7297 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7298 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7299 " + cc;",
7300 Style);
7301
7302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7303 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7304 Style);
7305
7306 // Forced by comments.
7307 verifyFormat(
7308 "unsigned ContentSize =\n"
7309 " sizeof(int16_t) // DWARF ARange version number\n"
7310 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7311 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7312 " + sizeof(int8_t); // Segment Size (in bytes)");
7313
7314 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7315 " == boost::fusion::at_c<1>(iiii).second;",
7316 Style);
7317
7318 Style.ColumnLimit = 60;
7319 verifyFormat("zzzzzzzzzz\n"
7320 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7321 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7322 Style);
7323
7324 Style.ColumnLimit = 80;
7325 Style.IndentWidth = 4;
7326 Style.TabWidth = 4;
7327 Style.UseTab = FormatStyle::UT_Always;
7328 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7329 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7330 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7331 "\t&& (someOtherLongishConditionPart1\n"
7332 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7333 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7334 "(someOtherLongishConditionPart1 || "
7335 "someOtherEvenLongerNestedConditionPart2);",
7336 Style);
7337
7338 Style = getLLVMStyleWithColumns(20);
7339 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7340 Style.BinPackParameters = false;
7341 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7342 Style.ContinuationIndentWidth = 2;
7343 verifyFormat("struct Foo {\n"
7344 " Foo(\n"
7345 " int arg1,\n"
7346 " int arg2)\n"
7347 " : Base(\n"
7348 " arg1,\n"
7349 " arg2) {}\n"
7350 "};",
7351 Style);
7352 verifyFormat("return abc\n"
7353 " ? foo(\n"
7354 " a,\n"
7355 " b,\n"
7356 " bar(\n"
7357 " abc))\n"
7358 " : g(abc);",
7359 Style);
7360 }
7361
7362 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7363 FormatStyle Style = getLLVMStyle();
7364 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7365 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7366
7367 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7368 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7369 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7370 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7372 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7373 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7374 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7375 " > ccccccccccccccccccccccccccccccccccccccccc;",
7376 Style);
7377 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7378 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7379 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7380 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7381 Style);
7382 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7383 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7384 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7385 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7386 Style);
7387 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7388 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7389 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7390 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7391 Style);
7392 verifyFormat("if () {\n"
7393 "} else if (aaaaa\n"
7394 " && bbbbb // break\n"
7395 " > ccccc) {\n"
7396 "}",
7397 Style);
7398 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7399 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7400 Style);
7401 verifyFormat("return (a)\n"
7402 " // comment\n"
7403 " + b;",
7404 Style);
7405 verifyFormat(
7406 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7407 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7408 " + cc;",
7409 Style);
7410 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7411 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7412 " : 3333333333333333;",
7413 Style);
7414 verifyFormat(
7415 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7416 " : ccccccccccccccc ? dddddddddddddddddd\n"
7417 " : eeeeeeeeeeeeeeeeee)\n"
7418 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7419 " : 3333333333333333;",
7420 Style);
7421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7422 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7423 Style);
7424
7425 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7426 " == boost::fusion::at_c<1>(iiii).second;",
7427 Style);
7428
7429 Style.ColumnLimit = 60;
7430 verifyFormat("zzzzzzzzzzzzz\n"
7431 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7432 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7433 Style);
7434
7435 // Forced by comments.
7436 Style.ColumnLimit = 80;
7437 verifyFormat(
7438 "unsigned ContentSize\n"
7439 " = sizeof(int16_t) // DWARF ARange version number\n"
7440 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7441 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7442 " + sizeof(int8_t); // Segment Size (in bytes)",
7443 Style);
7444
7445 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7446 verifyFormat(
7447 "unsigned ContentSize =\n"
7448 " sizeof(int16_t) // DWARF ARange version number\n"
7449 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7450 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7451 " + sizeof(int8_t); // Segment Size (in bytes)",
7452 Style);
7453
7454 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7455 verifyFormat(
7456 "unsigned ContentSize =\n"
7457 " sizeof(int16_t) // DWARF ARange version number\n"
7458 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7459 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7460 " + sizeof(int8_t); // Segment Size (in bytes)",
7461 Style);
7462 }
7463
7464 TEST_F(FormatTest, EnforcedOperatorWraps) {
7465 // Here we'd like to wrap after the || operators, but a comment is forcing an
7466 // earlier wrap.
7467 verifyFormat("bool x = aaaaa //\n"
7468 " || bbbbb\n"
7469 " //\n"
7470 " || cccc;");
7471 }
7472
7473 TEST_F(FormatTest, NoOperandAlignment) {
7474 FormatStyle Style = getLLVMStyle();
7475 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7476 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7479 Style);
7480 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7481 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7482 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7483 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7484 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7485 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7486 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7487 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7488 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489 " > ccccccccccccccccccccccccccccccccccccccccc;",
7490 Style);
7491
7492 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7493 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7494 " + cc;",
7495 Style);
7496 verifyFormat("int a = aa\n"
7497 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7498 " * cccccccccccccccccccccccccccccccccccc;",
7499 Style);
7500
7501 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7502 verifyFormat("return (a > b\n"
7503 " // comment1\n"
7504 " // comment2\n"
7505 " || c);",
7506 Style);
7507 }
7508
7509 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7510 FormatStyle Style = getLLVMStyle();
7511 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7512 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7515 Style);
7516 }
7517
7518 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7519 FormatStyle Style = getLLVMStyleWithColumns(40);
7520 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7521 Style.BinPackArguments = false;
7522 verifyFormat("void test() {\n"
7523 " someFunction(\n"
7524 " this + argument + is + quite\n"
7525 " + long + so + it + gets + wrapped\n"
7526 " + but + remains + bin - packed);\n"
7527 "}",
7528 Style);
7529 verifyFormat("void test() {\n"
7530 " someFunction(arg1,\n"
7531 " this + argument + is\n"
7532 " + quite + long + so\n"
7533 " + it + gets + wrapped\n"
7534 " + but + remains + bin\n"
7535 " - packed,\n"
7536 " arg3);\n"
7537 "}",
7538 Style);
7539 verifyFormat("void test() {\n"
7540 " someFunction(\n"
7541 " arg1,\n"
7542 " this + argument + has\n"
7543 " + anotherFunc(nested,\n"
7544 " calls + whose\n"
7545 " + arguments\n"
7546 " + are + also\n"
7547 " + wrapped,\n"
7548 " in + addition)\n"
7549 " + to + being + bin - packed,\n"
7550 " arg3);\n"
7551 "}",
7552 Style);
7553
7554 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7555 verifyFormat("void test() {\n"
7556 " someFunction(\n"
7557 " arg1,\n"
7558 " this + argument + has +\n"
7559 " anotherFunc(nested,\n"
7560 " calls + whose +\n"
7561 " arguments +\n"
7562 " are + also +\n"
7563 " wrapped,\n"
7564 " in + addition) +\n"
7565 " to + being + bin - packed,\n"
7566 " arg3);\n"
7567 "}",
7568 Style);
7569 }
7570
7571 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7572 auto Style = getLLVMStyleWithColumns(45);
7573 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7574 verifyFormat("bool b =\n"
7575 " is_default_constructible_v<hash<T>> and\n"
7576 " is_copy_constructible_v<hash<T>> and\n"
7577 " is_move_constructible_v<hash<T>> and\n"
7578 " is_copy_assignable_v<hash<T>> and\n"
7579 " is_move_assignable_v<hash<T>> and\n"
7580 " is_destructible_v<hash<T>> and\n"
7581 " is_swappable_v<hash<T>> and\n"
7582 " is_callable_v<hash<T>(T)>;",
7583 Style);
7584
7585 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7586 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7587 " and is_copy_constructible_v<hash<T>>\n"
7588 " and is_move_constructible_v<hash<T>>\n"
7589 " and is_copy_assignable_v<hash<T>>\n"
7590 " and is_move_assignable_v<hash<T>>\n"
7591 " and is_destructible_v<hash<T>>\n"
7592 " and is_swappable_v<hash<T>>\n"
7593 " and is_callable_v<hash<T>(T)>;",
7594 Style);
7595
7596 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7597 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7598 " and is_copy_constructible_v<hash<T>>\n"
7599 " and is_move_constructible_v<hash<T>>\n"
7600 " and is_copy_assignable_v<hash<T>>\n"
7601 " and is_move_assignable_v<hash<T>>\n"
7602 " and is_destructible_v<hash<T>>\n"
7603 " and is_swappable_v<hash<T>>\n"
7604 " and is_callable_v<hash<T>(T)>;",
7605 Style);
7606 }
7607
7608 TEST_F(FormatTest, ConstructorInitializers) {
7609 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7610 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7611 getLLVMStyleWithColumns(45));
7612 verifyFormat("Constructor()\n"
7613 " : Inttializer(FitsOnTheLine) {}",
7614 getLLVMStyleWithColumns(44));
7615 verifyFormat("Constructor()\n"
7616 " : Inttializer(FitsOnTheLine) {}",
7617 getLLVMStyleWithColumns(43));
7618
7619 verifyFormat("template <typename T>\n"
7620 "Constructor() : Initializer(FitsOnTheLine) {}",
7621 getLLVMStyleWithColumns(45));
7622
7623 verifyFormat(
7624 "SomeClass::Constructor()\n"
7625 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7626
7627 verifyFormat(
7628 "SomeClass::Constructor()\n"
7629 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7630 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7631 verifyFormat(
7632 "SomeClass::Constructor()\n"
7633 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7634 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7635 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7636 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7637 " : aaaaaaaaaa(aaaaaa) {}");
7638
7639 verifyFormat("Constructor()\n"
7640 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7641 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7642 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7643 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7644
7645 verifyFormat("Constructor()\n"
7646 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7648
7649 verifyFormat("Constructor(int Parameter = 0)\n"
7650 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7651 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7652 verifyFormat("Constructor()\n"
7653 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7654 "}",
7655 getLLVMStyleWithColumns(60));
7656 verifyFormat("Constructor()\n"
7657 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7658 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7659
7660 // Here a line could be saved by splitting the second initializer onto two
7661 // lines, but that is not desirable.
7662 verifyFormat("Constructor()\n"
7663 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7664 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7665 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7666
7667 FormatStyle OnePerLine = getLLVMStyle();
7668 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7669 verifyFormat("MyClass::MyClass()\n"
7670 " : a(a),\n"
7671 " b(b),\n"
7672 " c(c) {}",
7673 OnePerLine);
7674 verifyFormat("MyClass::MyClass()\n"
7675 " : a(a), // comment\n"
7676 " b(b),\n"
7677 " c(c) {}",
7678 OnePerLine);
7679 verifyFormat("MyClass::MyClass(int a)\n"
7680 " : b(a), // comment\n"
7681 " c(a + 1) { // lined up\n"
7682 "}",
7683 OnePerLine);
7684 verifyFormat("Constructor()\n"
7685 " : a(b, b, b) {}",
7686 OnePerLine);
7687 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7688 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7689 verifyFormat("SomeClass::Constructor()\n"
7690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7691 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7692 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7693 OnePerLine);
7694 verifyFormat("SomeClass::Constructor()\n"
7695 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7696 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7697 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7698 OnePerLine);
7699 verifyFormat("MyClass::MyClass(int var)\n"
7700 " : some_var_(var), // 4 space indent\n"
7701 " some_other_var_(var + 1) { // lined up\n"
7702 "}",
7703 OnePerLine);
7704 verifyFormat("Constructor()\n"
7705 " : aaaaa(aaaaaa),\n"
7706 " aaaaa(aaaaaa),\n"
7707 " aaaaa(aaaaaa),\n"
7708 " aaaaa(aaaaaa),\n"
7709 " aaaaa(aaaaaa) {}",
7710 OnePerLine);
7711 verifyFormat("Constructor()\n"
7712 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7713 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7714 OnePerLine);
7715 OnePerLine.BinPackParameters = false;
7716 verifyFormat(
7717 "Constructor()\n"
7718 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7719 " aaaaaaaaaaa().aaa(),\n"
7720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7721 OnePerLine);
7722 OnePerLine.ColumnLimit = 60;
7723 verifyFormat("Constructor()\n"
7724 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7725 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7726 OnePerLine);
7727
7728 verifyFormat("Constructor()\n"
7729 " : // Comment forcing unwanted break.\n"
7730 " aaaa(aaaa) {}",
7731 "Constructor() :\n"
7732 " // Comment forcing unwanted break.\n"
7733 " aaaa(aaaa) {}");
7734 }
7735
7736 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7737 FormatStyle Style = getLLVMStyleWithColumns(60);
7738 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7739 Style.BinPackParameters = false;
7740
7741 for (int i = 0; i < 4; ++i) {
7742 // Test all combinations of parameters that should not have an effect.
7743 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7744 Style.AllowAllArgumentsOnNextLine = i & 2;
7745
7746 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7747 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7748 verifyFormat("Constructor()\n"
7749 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7750 Style);
7751 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7752
7753 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7754 verifyFormat("Constructor()\n"
7755 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7756 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7757 Style);
7758 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7759
7760 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7761 verifyFormat("Constructor()\n"
7762 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7763 Style);
7764 verifyFormat("Constructor()\n"
7765 " : a(a), b(b) {}",
7766 Style);
7767 verifyFormat("Constructor()\n"
7768 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7769 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7770 " , cccccccccccccccccccccc(c) {}",
7771 Style);
7772
7773 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7774 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7775 verifyFormat("Constructor()\n"
7776 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7777 Style);
7778
7779 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7780 verifyFormat("Constructor()\n"
7781 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7782 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7783 Style);
7784
7785 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7786 verifyFormat("Constructor()\n"
7787 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7788 Style);
7789 verifyFormat("Constructor()\n"
7790 " : a(a), b(b) {}",
7791 Style);
7792 verifyFormat("Constructor()\n"
7793 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7794 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7795 " cccccccccccccccccccccc(c) {}",
7796 Style);
7797
7798 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7799 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7800 verifyFormat("Constructor() :\n"
7801 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7802 Style);
7803
7804 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7805 verifyFormat("Constructor() :\n"
7806 " aaaaaaaaaaaaaaaaaa(a),\n"
7807 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7808 Style);
7809
7810 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7811 verifyFormat("Constructor() :\n"
7812 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7813 Style);
7814 verifyFormat("Constructor() :\n"
7815 " a(a), b(b) {}",
7816 Style);
7817 verifyFormat("Constructor() :\n"
7818 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7819 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7820 " cccccccccccccccccccccc(c) {}",
7821 Style);
7822 }
7823
7824 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7825 // AllowAllConstructorInitializersOnNextLine in all
7826 // BreakConstructorInitializers modes
7827 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7828 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7829 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7830 verifyFormat("SomeClassWithALongName::Constructor(\n"
7831 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7832 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7833 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7834 Style);
7835
7836 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7837 verifyFormat("SomeClassWithALongName::Constructor(\n"
7838 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7839 " int bbbbbbbbbbbbb,\n"
7840 " int cccccccccccccccc)\n"
7841 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7842 Style);
7843
7844 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7845 verifyFormat("SomeClassWithALongName::Constructor(\n"
7846 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7847 " int bbbbbbbbbbbbb,\n"
7848 " int cccccccccccccccc)\n"
7849 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7850 Style);
7851
7852 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7853 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7854 verifyFormat("SomeClassWithALongName::Constructor(\n"
7855 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7856 " int bbbbbbbbbbbbb)\n"
7857 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7858 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7859 Style);
7860
7861 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7862
7863 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7864 verifyFormat("SomeClassWithALongName::Constructor(\n"
7865 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7866 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7867 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7868 Style);
7869
7870 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7871 verifyFormat("SomeClassWithALongName::Constructor(\n"
7872 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7873 " int bbbbbbbbbbbbb,\n"
7874 " int cccccccccccccccc)\n"
7875 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7876 Style);
7877
7878 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7879 verifyFormat("SomeClassWithALongName::Constructor(\n"
7880 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7881 " int bbbbbbbbbbbbb,\n"
7882 " int cccccccccccccccc)\n"
7883 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7884 Style);
7885
7886 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7887 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7888 verifyFormat("SomeClassWithALongName::Constructor(\n"
7889 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7890 " int bbbbbbbbbbbbb)\n"
7891 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7892 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7893 Style);
7894
7895 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7896 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7897 verifyFormat("SomeClassWithALongName::Constructor(\n"
7898 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7899 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7900 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7901 Style);
7902
7903 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7904 verifyFormat("SomeClassWithALongName::Constructor(\n"
7905 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7906 " int bbbbbbbbbbbbb,\n"
7907 " int cccccccccccccccc) :\n"
7908 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7909 Style);
7910
7911 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7912 verifyFormat("SomeClassWithALongName::Constructor(\n"
7913 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7914 " int bbbbbbbbbbbbb,\n"
7915 " int cccccccccccccccc) :\n"
7916 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7917 Style);
7918
7919 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7920 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7921 verifyFormat("SomeClassWithALongName::Constructor(\n"
7922 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7923 " int bbbbbbbbbbbbb) :\n"
7924 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7925 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7926 Style);
7927
7928 Style = getLLVMStyleWithColumns(0);
7929 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7930 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7931 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7932 " : bar(bar), baz(baz) {}",
7933 Style);
7934 }
7935
7936 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7937 FormatStyle Style = getLLVMStyleWithColumns(60);
7938 Style.BinPackArguments = false;
7939 for (int i = 0; i < 4; ++i) {
7940 // Test all combinations of parameters that should not have an effect.
7941 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7942 Style.PackConstructorInitializers =
7943 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7944
7945 Style.AllowAllArgumentsOnNextLine = true;
7946 verifyFormat("void foo() {\n"
7947 " FunctionCallWithReallyLongName(\n"
7948 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7949 "}",
7950 Style);
7951 Style.AllowAllArgumentsOnNextLine = false;
7952 verifyFormat("void foo() {\n"
7953 " FunctionCallWithReallyLongName(\n"
7954 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7955 " bbbbbbbbbbbb);\n"
7956 "}",
7957 Style);
7958
7959 Style.AllowAllArgumentsOnNextLine = true;
7960 verifyFormat("void foo() {\n"
7961 " auto VariableWithReallyLongName = {\n"
7962 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7963 "}",
7964 Style);
7965 Style.AllowAllArgumentsOnNextLine = false;
7966 verifyFormat("void foo() {\n"
7967 " auto VariableWithReallyLongName = {\n"
7968 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7969 " bbbbbbbbbbbb};\n"
7970 "}",
7971 Style);
7972 }
7973
7974 // This parameter should not affect declarations.
7975 Style.BinPackParameters = false;
7976 Style.AllowAllArgumentsOnNextLine = false;
7977 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7978 verifyFormat("void FunctionCallWithReallyLongName(\n"
7979 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7980 Style);
7981 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7982 verifyFormat("void FunctionCallWithReallyLongName(\n"
7983 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7984 " int bbbbbbbbbbbb);",
7985 Style);
7986 }
7987
7988 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7989 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7990 // and BAS_Align.
7991 FormatStyle Style = getLLVMStyleWithColumns(35);
7992 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7993 "void functionDecl(int A, int B, int C);";
7994 Style.AllowAllArgumentsOnNextLine = false;
7995 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7996 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7997 " paramC);\n"
7998 "void functionDecl(int A, int B,\n"
7999 " int C);"),
8000 Input, Style);
8001 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8002 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8003 " paramC);\n"
8004 "void functionDecl(int A, int B,\n"
8005 " int C);"),
8006 Input, Style);
8007 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
8008 // AllowAllArgumentsOnNextLine.
8009 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8010 verifyFormat(StringRef("functionCall(\n"
8011 " paramA, paramB, paramC);\n"
8012 "void functionDecl(\n"
8013 " int A, int B, int C);"),
8014 Input, Style);
8015 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8016 verifyFormat("functionCall(\n"
8017 " paramA, paramB, paramC\n"
8018 ");\n"
8019 "void functionDecl(\n"
8020 " int A, int B, int C\n"
8021 ");",
8022 Input, Style);
8023
8024 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8025 // first argument.
8026 Style.AllowAllArgumentsOnNextLine = true;
8027 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8028 verifyFormat(StringRef("functionCall(\n"
8029 " paramA, paramB, paramC);\n"
8030 "void functionDecl(\n"
8031 " int A, int B, int C);"),
8032 Input, Style);
8033 // It wouldn't fit on one line with aligned parameters so this setting
8034 // doesn't change anything for BAS_Align.
8035 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
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_DontAlign;
8042 verifyFormat(StringRef("functionCall(\n"
8043 " paramA, paramB, paramC);\n"
8044 "void functionDecl(\n"
8045 " int A, int B, int C);"),
8046 Input, Style);
8047 }
8048
8049 TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8050 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8051 "void emptyFunctionDefinition() {}\n"
8052 "void functionDefinition(int A, int B, int C) {}\n"
8053 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8054 verifyFormat(Input);
8055
8056 FormatStyle Style = getLLVMStyle();
8057 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8058 Style.BreakFunctionDefinitionParameters = true;
8059 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8060 "void emptyFunctionDefinition() {}\n"
8061 "void functionDefinition(\n"
8062 " int A, int B, int C) {}\n"
8063 "Class::Class(\n"
8064 " int A, int B)\n"
8065 " : m_A(A), m_B(B) {}",
8066 Input, Style);
8067
8068 // Test the style where all parameters are on their own lines.
8069 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8070 Style.BinPackParameters = false;
8071 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8072 "void emptyFunctionDefinition() {}\n"
8073 "void functionDefinition(\n"
8074 " int A,\n"
8075 " int B,\n"
8076 " int C) {}\n"
8077 "Class::Class(\n"
8078 " int A,\n"
8079 " int B)\n"
8080 " : m_A(A), m_B(B) {}",
8081 Input, Style);
8082 }
8083
8084 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8085 FormatStyle Style = getLLVMStyle();
8086 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8087 /* Test the behaviour with long lines */
8088 Style.ColumnLimit = 40;
8089 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8090 " : : val);",
8091 Style);
8092 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8093 " : val1 : val2);",
8094 Style);
8095 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8096 " \"cpuid\\n\\t\"\n"
8097 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8098 " : \"=a\" : \"a\");",
8099 Style);
8100 Style.ColumnLimit = 80;
8101 verifyFormat("asm volatile(\"string\", : : val);", Style);
8102 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8103
8104 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8105 verifyFormat("asm volatile(\"string\",\n"
8106 " :\n"
8107 " : val);",
8108 Style);
8109 verifyFormat("asm volatile(\"string\",\n"
8110 " : val1\n"
8111 " : val2);",
8112 Style);
8113 /* Test the behaviour with long lines */
8114 Style.ColumnLimit = 40;
8115 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8116 " \"cpuid\\n\\t\"\n"
8117 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8118 " : \"=a\"(*rEAX)\n"
8119 " : \"a\"(value));",
8120 Style);
8121 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8122 " \"cpuid\\n\\t\"\n"
8123 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8124 " :\n"
8125 " : \"a\"(value));",
8126 Style);
8127 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8128 " :\n"
8129 " : val);",
8130 Style);
8131 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8132 " : val1\n"
8133 " : val2);",
8134 Style);
8135 }
8136
8137 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8138 FormatStyle Style = getLLVMStyle();
8139 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8140
8141 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8142 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8143 getStyleWithColumns(Style, 45));
8144 verifyFormat("Constructor() :\n"
8145 " Initializer(FitsOnTheLine) {}",
8146 getStyleWithColumns(Style, 44));
8147 verifyFormat("Constructor() :\n"
8148 " Initializer(FitsOnTheLine) {}",
8149 getStyleWithColumns(Style, 43));
8150
8151 verifyFormat("template <typename T>\n"
8152 "Constructor() : Initializer(FitsOnTheLine) {}",
8153 getStyleWithColumns(Style, 50));
8154 verifyFormat(
8155 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8156 " int mooooooooooooore) noexcept :\n"
8157 " Super{some, arguments}, Member{5}, Member2{2} {}",
8158 Style);
8159 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8160 verifyFormat(
8161 "SomeClass::Constructor() :\n"
8162 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8163 Style);
8164 verifyFormat(
8165 "SomeClass::Constructor() : // NOLINT\n"
8166 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8167 Style);
8168
8169 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8170 verifyFormat(
8171 "SomeClass::Constructor() :\n"
8172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8173 Style);
8174 verifyFormat(
8175 "SomeClass::Constructor() : // NOLINT\n"
8176 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8177 Style);
8178
8179 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8180 verifyFormat(
8181 "SomeClass::Constructor() :\n"
8182 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8183 Style);
8184
8185 verifyFormat(
8186 "SomeClass::Constructor() :\n"
8187 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8189 Style);
8190 verifyFormat(
8191 "SomeClass::Constructor() :\n"
8192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8193 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8194 Style);
8195 verifyFormat(
8196 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8197 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8198 Style);
8199
8200 verifyFormat("Constructor() :\n"
8201 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8202 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8204 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8205 Style);
8206
8207 verifyFormat("Constructor() :\n"
8208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8210 Style);
8211
8212 verifyFormat("Constructor(int Parameter = 0) :\n"
8213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8214 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8215 Style);
8216 verifyFormat("Constructor() :\n"
8217 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8218 "}",
8219 getStyleWithColumns(Style, 60));
8220 verifyFormat("Constructor() :\n"
8221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8222 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8223 Style);
8224
8225 // Here a line could be saved by splitting the second initializer onto two
8226 // lines, but that is not desirable.
8227 verifyFormat("Constructor() :\n"
8228 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8229 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8230 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8231 Style);
8232
8233 FormatStyle OnePerLine = Style;
8234 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8235 verifyFormat("SomeClass::Constructor() :\n"
8236 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8237 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8238 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8239 OnePerLine);
8240 verifyFormat("SomeClass::Constructor() :\n"
8241 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8242 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8243 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8244 OnePerLine);
8245 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8246 " i(i), // comment\n"
8247 " j(j) {}",
8248 OnePerLine);
8249 verifyFormat("MyClass::MyClass(int var) :\n"
8250 " some_var_(var), // 4 space indent\n"
8251 " some_other_var_(var + 1) { // lined up\n"
8252 "}",
8253 OnePerLine);
8254 verifyFormat("Constructor() :\n"
8255 " aaaaa(aaaaaa),\n"
8256 " aaaaa(aaaaaa),\n"
8257 " aaaaa(aaaaaa),\n"
8258 " aaaaa(aaaaaa),\n"
8259 " aaaaa(aaaaaa) {}",
8260 OnePerLine);
8261 verifyFormat("Constructor() :\n"
8262 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8263 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8264 OnePerLine);
8265 OnePerLine.BinPackParameters = false;
8266 verifyFormat("Constructor() :\n"
8267 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8268 " aaaaaaaaaaa().aaa(),\n"
8269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8270 OnePerLine);
8271 OnePerLine.ColumnLimit = 60;
8272 verifyFormat("Constructor() :\n"
8273 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8274 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8275 OnePerLine);
8276
8277 verifyFormat("Constructor() :\n"
8278 " // Comment forcing unwanted break.\n"
8279 " aaaa(aaaa) {}",
8280 Style);
8281 verifyFormat("Constructor() : // NOLINT\n"
8282 " aaaa(aaaa) {}",
8283 Style);
8284 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8285 " on a single\n"
8286 " // line.\n"
8287 " aaaa(aaaa) {}",
8288 "Constructor() : // A very long trailing comment that cannot fit"
8289 " on a single line.\n"
8290 " aaaa(aaaa) {}",
8291 Style);
8292
8293 Style.ColumnLimit = 0;
8294 verifyFormat("SomeClass::Constructor() :\n"
8295 " a(a) {}",
8296 Style);
8297 verifyFormat("SomeClass::Constructor() noexcept :\n"
8298 " a(a) {}",
8299 Style);
8300 verifyFormat("SomeClass::Constructor() :\n"
8301 " a(a), b(b), c(c) {}",
8302 Style);
8303 verifyFormat("SomeClass::Constructor() :\n"
8304 " a(a) {\n"
8305 " foo();\n"
8306 " bar();\n"
8307 "}",
8308 Style);
8309
8310 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8311 verifyFormat("SomeClass::Constructor() :\n"
8312 " a(a), b(b), c(c) {\n"
8313 "}",
8314 Style);
8315 verifyFormat("SomeClass::Constructor() :\n"
8316 " a(a) {\n"
8317 "}",
8318 Style);
8319
8320 Style.ColumnLimit = 80;
8321 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8322 Style.ConstructorInitializerIndentWidth = 2;
8323 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8324 verifyFormat("SomeClass::Constructor() :\n"
8325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8326 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8327 Style);
8328
8329 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8330 // well
8331 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8332 verifyFormat(
8333 "class SomeClass\n"
8334 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8335 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8336 Style);
8337 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8338 verifyFormat(
8339 "class SomeClass\n"
8340 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8341 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8342 Style);
8343 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8344 verifyFormat(
8345 "class SomeClass :\n"
8346 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8347 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8348 Style);
8349 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8350 verifyFormat(
8351 "class SomeClass\n"
8352 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8353 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8354 Style);
8355 }
8356
8357 #ifndef EXPENSIVE_CHECKS
8358 // Expensive checks enables libstdc++ checking which includes validating the
8359 // state of ranges used in std::priority_queue - this blows out the
8360 // runtime/scalability of the function and makes this test unacceptably slow.
8361 TEST_F(FormatTest, MemoizationTests) {
8362 // This breaks if the memoization lookup does not take \c Indent and
8363 // \c LastSpace into account.
8364 verifyFormat(
8365 "extern CFRunLoopTimerRef\n"
8366 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8367 " CFTimeInterval interval, CFOptionFlags flags,\n"
8368 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8369 " CFRunLoopTimerContext *context) {}");
8370
8371 // Deep nesting somewhat works around our memoization.
8372 verifyFormat(
8373 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8374 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8375 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8376 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8377 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8378 getLLVMStyleWithColumns(65));
8379 verifyFormat(
8380 "aaaaa(\n"
8381 " aaaaa,\n"
8382 " aaaaa(\n"
8383 " aaaaa,\n"
8384 " aaaaa(\n"
8385 " aaaaa,\n"
8386 " aaaaa(\n"
8387 " aaaaa,\n"
8388 " aaaaa(\n"
8389 " aaaaa,\n"
8390 " aaaaa(\n"
8391 " aaaaa,\n"
8392 " aaaaa(\n"
8393 " aaaaa,\n"
8394 " aaaaa(\n"
8395 " aaaaa,\n"
8396 " aaaaa(\n"
8397 " aaaaa,\n"
8398 " aaaaa(\n"
8399 " aaaaa,\n"
8400 " aaaaa(\n"
8401 " aaaaa,\n"
8402 " aaaaa(\n"
8403 " aaaaa,\n"
8404 " aaaaa))))))))))));",
8405 getLLVMStyleWithColumns(65));
8406 verifyFormat(
8407 "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"
8408 " a),\n"
8409 " a),\n"
8410 " a),\n"
8411 " a),\n"
8412 " a),\n"
8413 " a),\n"
8414 " a),\n"
8415 " a),\n"
8416 " a),\n"
8417 " a),\n"
8418 " a),\n"
8419 " a),\n"
8420 " a),\n"
8421 " a),\n"
8422 " a),\n"
8423 " a),\n"
8424 " a)",
8425 getLLVMStyleWithColumns(65));
8426
8427 // This test takes VERY long when memoization is broken.
8428 FormatStyle OnePerLine = getLLVMStyle();
8429 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8430 OnePerLine.BinPackParameters = false;
8431 std::string input = "Constructor()\n"
8432 " : aaaa(a,\n";
8433 for (unsigned i = 0, e = 80; i != e; ++i)
8434 input += " a,\n";
8435 input += " a) {}";
8436 verifyFormat(input, OnePerLine);
8437 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8438 verifyFormat(input, OnePerLine);
8439 }
8440 #endif
8441
8442 TEST_F(FormatTest, BreaksAsHighAsPossible) {
8443 verifyFormat(
8444 "void f() {\n"
8445 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8446 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8447 " f();\n"
8448 "}");
8449 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8450 " Intervals[i - 1].getRange().getLast()) {\n}");
8451 }
8452
8453 TEST_F(FormatTest, BreaksFunctionDeclarations) {
8454 // Principially, we break function declarations in a certain order:
8455 // 1) break amongst arguments.
8456 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8457 " Cccccccccccccc cccccccccccccc);");
8458 verifyFormat("template <class TemplateIt>\n"
8459 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8460 " TemplateIt *stop) {}");
8461
8462 // 2) break after return type.
8463 verifyGoogleFormat(
8464 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8465 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8466
8467 // 3) break after (.
8468 verifyGoogleFormat(
8469 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8470 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8471
8472 // 4) break before after nested name specifiers.
8473 verifyGoogleFormat(
8474 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8475 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8476 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8477
8478 // However, there are exceptions, if a sufficient amount of lines can be
8479 // saved.
8480 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8481 // more adjusting.
8482 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8483 " Cccccccccccccc cccccccccc,\n"
8484 " Cccccccccccccc cccccccccc,\n"
8485 " Cccccccccccccc cccccccccc,\n"
8486 " Cccccccccccccc cccccccccc);");
8487 verifyGoogleFormat(
8488 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8489 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8490 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8491 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8492 verifyFormat(
8493 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8494 " Cccccccccccccc cccccccccc,\n"
8495 " Cccccccccccccc cccccccccc,\n"
8496 " Cccccccccccccc cccccccccc,\n"
8497 " Cccccccccccccc cccccccccc,\n"
8498 " Cccccccccccccc cccccccccc,\n"
8499 " Cccccccccccccc cccccccccc);");
8500 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8501 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8502 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8503 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8504 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8505
8506 // Break after multi-line parameters.
8507 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8510 " bbbb bbbb);");
8511 verifyFormat("void SomeLoooooooooooongFunction(\n"
8512 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8513 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8514 " int bbbbbbbbbbbbb);");
8515
8516 // Treat overloaded operators like other functions.
8517 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8518 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8519 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8520 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8521 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8522 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8523 verifyGoogleFormat(
8524 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8525 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8526 verifyGoogleFormat(
8527 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8528 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8529 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8530 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8531 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8532 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8533 verifyGoogleFormat(
8534 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8535 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8536 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8537 verifyGoogleFormat("template <typename T>\n"
8538 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8539 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8540 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8541
8542 FormatStyle Style = getLLVMStyle();
8543 Style.PointerAlignment = FormatStyle::PAS_Left;
8544 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8545 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8546 Style);
8547 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8549 Style);
8550 }
8551
8552 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8553 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8554 // Prefer keeping `::` followed by `operator` together.
8555 verifyFormat("const aaaa::bbbbbbb &\n"
8556 "ccccccccc::operator++() {\n"
8557 " stuff();\n"
8558 "}",
8559 "const aaaa::bbbbbbb\n"
8560 "&ccccccccc::operator++() { stuff(); }",
8561 getLLVMStyleWithColumns(40));
8562 }
8563
8564 TEST_F(FormatTest, TrailingReturnType) {
8565 verifyFormat("auto foo() -> int;");
8566 // correct trailing return type spacing
8567 verifyFormat("auto operator->() -> int;");
8568 verifyFormat("auto operator++(int) -> int;");
8569
8570 verifyFormat("struct S {\n"
8571 " auto bar() const -> int;\n"
8572 "};");
8573 verifyFormat("template <size_t Order, typename T>\n"
8574 "auto load_img(const std::string &filename)\n"
8575 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8576 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8577 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8578 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8579 verifyFormat("template <typename T>\n"
8580 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8581 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8582
8583 FormatStyle Style = getLLVMStyleWithColumns(60);
8584 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8585 " auto NAME() -> int { return 42; }",
8586 Style);
8587
8588 // Not trailing return types.
8589 verifyFormat("void f() { auto a = b->c(); }");
8590 verifyFormat("auto a = p->foo();");
8591 verifyFormat("int a = p->foo();");
8592 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8593 }
8594
8595 TEST_F(FormatTest, DeductionGuides) {
8596 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8597 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8598 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8599 verifyFormat(
8600 "template <class... T>\n"
8601 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8602 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8603 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8604 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8605 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8606 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8607 verifyFormat("template <class T> x() -> x<1>;");
8608 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8609
8610 verifyFormat("A(const char *) -> A<string &>;");
8611 verifyFormat("A() -> A<int>;");
8612
8613 // Ensure not deduction guides.
8614 verifyFormat("c()->f<int>();");
8615 verifyFormat("x()->foo<1>;");
8616 verifyFormat("x = p->foo<3>();");
8617 verifyFormat("x()->x<1>();");
8618 }
8619
8620 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8621 // Avoid breaking before trailing 'const' or other trailing annotations, if
8622 // they are not function-like.
8623 FormatStyle Style = getGoogleStyleWithColumns(47);
8624 verifyFormat("void someLongFunction(\n"
8625 " int someLoooooooooooooongParameter) const {\n}",
8626 getLLVMStyleWithColumns(47));
8627 verifyFormat("LoooooongReturnType\n"
8628 "someLoooooooongFunction() const {}",
8629 getLLVMStyleWithColumns(47));
8630 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8631 " const {}",
8632 Style);
8633 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8634 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8635 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8636 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8637 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8638 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8639 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8640 " aaaaaaaaaaa aaaaa) const override;");
8641 verifyGoogleFormat(
8642 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8643 " const override;");
8644
8645 // Even if the first parameter has to be wrapped.
8646 verifyFormat("void someLongFunction(\n"
8647 " int someLongParameter) const {}",
8648 getLLVMStyleWithColumns(46));
8649 verifyFormat("void someLongFunction(\n"
8650 " int someLongParameter) const {}",
8651 Style);
8652 verifyFormat("void someLongFunction(\n"
8653 " int someLongParameter) override {}",
8654 Style);
8655 verifyFormat("void someLongFunction(\n"
8656 " int someLongParameter) OVERRIDE {}",
8657 Style);
8658 verifyFormat("void someLongFunction(\n"
8659 " int someLongParameter) final {}",
8660 Style);
8661 verifyFormat("void someLongFunction(\n"
8662 " int someLongParameter) FINAL {}",
8663 Style);
8664 verifyFormat("void someLongFunction(\n"
8665 " int parameter) const override {}",
8666 Style);
8667
8668 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8669 verifyFormat("void someLongFunction(\n"
8670 " int someLongParameter) const\n"
8671 "{\n"
8672 "}",
8673 Style);
8674
8675 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8676 verifyFormat("void someLongFunction(\n"
8677 " int someLongParameter) const\n"
8678 " {\n"
8679 " }",
8680 Style);
8681
8682 // Unless these are unknown annotations.
8683 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8684 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8685 " LONG_AND_UGLY_ANNOTATION;");
8686
8687 // Breaking before function-like trailing annotations is fine to keep them
8688 // close to their arguments.
8689 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8690 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8691 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8692 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8693 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8694 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8695 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8696 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8697 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8698
8699 verifyFormat(
8700 "void aaaaaaaaaaaaaaaaaa()\n"
8701 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8702 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8703 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8704 " __attribute__((unused));");
8705
8706 Style = getGoogleStyle();
8707
8708 verifyFormat(
8709 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8710 " GUARDED_BY(aaaaaaaaaaaa);",
8711 Style);
8712 verifyFormat(
8713 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8714 " GUARDED_BY(aaaaaaaaaaaa);",
8715 Style);
8716 verifyFormat(
8717 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8718 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8719 Style);
8720 verifyFormat(
8721 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8722 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8723 Style);
8724
8725 verifyFormat(
8726 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8727 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8728 Style);
8729 verifyFormat(
8730 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8731 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8732 Style);
8733 verifyFormat(
8734 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8735 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8736 Style);
8737 verifyFormat(
8738 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8739 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8740 Style);
8741 }
8742
8743 TEST_F(FormatTest, FunctionAnnotations) {
8744 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8745 "int OldFunction(const string &parameter) {}");
8746 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8747 "string OldFunction(const string &parameter) {}");
8748 verifyFormat("template <typename T>\n"
8749 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8750 "string OldFunction(const string &parameter) {}");
8751
8752 // Not function annotations.
8753 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8754 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8755 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8756 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8757 verifyFormat("MACRO(abc).function() // wrap\n"
8758 " << abc;");
8759 verifyFormat("MACRO(abc)->function() // wrap\n"
8760 " << abc;");
8761 verifyFormat("MACRO(abc)::function() // wrap\n"
8762 " << abc;");
8763 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8764 }
8765
8766 TEST_F(FormatTest, BreaksDesireably) {
8767 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8768 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8769 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8772 "}");
8773
8774 verifyFormat(
8775 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8777
8778 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8781
8782 verifyFormat(
8783 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8784 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8786 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8788
8789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8790 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8791
8792 verifyFormat(
8793 "void f() {\n"
8794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8796 "}");
8797 verifyFormat(
8798 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8800 verifyFormat(
8801 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8803 verifyFormat(
8804 "aaaaaa(aaa,\n"
8805 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8807 " aaaa);");
8808 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8811
8812 // Indent consistently independent of call expression and unary operator.
8813 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8814 " dddddddddddddddddddddddddddddd));");
8815 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8816 " dddddddddddddddddddddddddddddd));");
8817 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8818 " dddddddddddddddddddddddddddddd));");
8819
8820 // This test case breaks on an incorrect memoization, i.e. an optimization not
8821 // taking into account the StopAt value.
8822 verifyFormat(
8823 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8824 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8825 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8826 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8827
8828 verifyFormat("{\n {\n {\n"
8829 " Annotation.SpaceRequiredBefore =\n"
8830 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8831 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8832 " }\n }\n}");
8833
8834 // Break on an outer level if there was a break on an inner level.
8835 verifyFormat("f(g(h(a, // comment\n"
8836 " b, c),\n"
8837 " d, e),\n"
8838 " x, y);",
8839 "f(g(h(a, // comment\n"
8840 " b, c), d, e), x, y);");
8841
8842 // Prefer breaking similar line breaks.
8843 verifyFormat(
8844 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8845 " NSTrackingMouseEnteredAndExited |\n"
8846 " NSTrackingActiveAlways;");
8847 }
8848
8849 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8850 FormatStyle NoBinPacking = getGoogleStyle();
8851 NoBinPacking.BinPackParameters = false;
8852 NoBinPacking.BinPackArguments = true;
8853 verifyFormat("void f() {\n"
8854 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8856 "}",
8857 NoBinPacking);
8858 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8859 " int aaaaaaaaaaaaaaaaaaaa,\n"
8860 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8861 NoBinPacking);
8862
8863 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8864 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8865 " vector<int> bbbbbbbbbbbbbbb);",
8866 NoBinPacking);
8867 // FIXME: This behavior difference is probably not wanted. However, currently
8868 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8869 // template arguments from BreakBeforeParameter being set because of the
8870 // one-per-line formatting.
8871 verifyFormat(
8872 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8873 " aaaaaaaaaa> aaaaaaaaaa);",
8874 NoBinPacking);
8875 verifyFormat(
8876 "void fffffffffff(\n"
8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8878 " aaaaaaaaaa);");
8879 }
8880
8881 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8882 FormatStyle NoBinPacking = getGoogleStyle();
8883 NoBinPacking.BinPackParameters = false;
8884 NoBinPacking.BinPackArguments = false;
8885 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8886 " aaaaaaaaaaaaaaaaaaaa,\n"
8887 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8888 NoBinPacking);
8889 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8890 " aaaaaaaaaaaaa,\n"
8891 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8892 NoBinPacking);
8893 verifyFormat(
8894 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8895 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8897 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8899 NoBinPacking);
8900 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8901 " .aaaaaaaaaaaaaaaaaa();",
8902 NoBinPacking);
8903 verifyFormat("void f() {\n"
8904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8905 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8906 "}",
8907 NoBinPacking);
8908
8909 verifyFormat(
8910 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8911 " aaaaaaaaaaaa,\n"
8912 " aaaaaaaaaaaa);",
8913 NoBinPacking);
8914 verifyFormat(
8915 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8916 " ddddddddddddddddddddddddddddd),\n"
8917 " test);",
8918 NoBinPacking);
8919
8920 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8921 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8922 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8923 " aaaaaaaaaaaaaaaaaa;",
8924 NoBinPacking);
8925 verifyFormat("a(\"a\"\n"
8926 " \"a\",\n"
8927 " a);");
8928
8929 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8930 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8931 " aaaaaaaaa,\n"
8932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8933 NoBinPacking);
8934 verifyFormat(
8935 "void f() {\n"
8936 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8937 " .aaaaaaa();\n"
8938 "}",
8939 NoBinPacking);
8940 verifyFormat(
8941 "template <class SomeType, class SomeOtherType>\n"
8942 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8943 NoBinPacking);
8944 }
8945
8946 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
8947 FormatStyle Style = getLLVMStyleWithColumns(15);
8948 Style.ExperimentalAutoDetectBinPacking = true;
8949 verifyFormat("aaa(aaaa,\n"
8950 " aaaa,\n"
8951 " aaaa);\n"
8952 "aaa(aaaa,\n"
8953 " aaaa,\n"
8954 " aaaa);",
8955 "aaa(aaaa,\n" // one-per-line
8956 " aaaa,\n"
8957 " aaaa );\n"
8958 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8959 Style);
8960 verifyFormat("aaa(aaaa, aaaa,\n"
8961 " aaaa);\n"
8962 "aaa(aaaa, aaaa,\n"
8963 " aaaa);",
8964 "aaa(aaaa, aaaa,\n" // bin-packed
8965 " aaaa );\n"
8966 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8967 Style);
8968 }
8969
8970 TEST_F(FormatTest, FormatsBuilderPattern) {
8971 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
8972 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
8973 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
8974 " .StartsWith(\".init\", ORDER_INIT)\n"
8975 " .StartsWith(\".fini\", ORDER_FINI)\n"
8976 " .StartsWith(\".hash\", ORDER_HASH)\n"
8977 " .Default(ORDER_TEXT);");
8978
8979 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
8980 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
8981 verifyFormat("aaaaaaa->aaaaaaa\n"
8982 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8984 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8985 verifyFormat(
8986 "aaaaaaa->aaaaaaa\n"
8987 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8988 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8989 verifyFormat(
8990 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
8991 " aaaaaaaaaaaaaa);");
8992 verifyFormat(
8993 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
8994 " aaaaaa->aaaaaaaaaaaa()\n"
8995 " ->aaaaaaaaaaaaaaaa(\n"
8996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8997 " ->aaaaaaaaaaaaaaaaa();");
8998 verifyGoogleFormat(
8999 "void f() {\n"
9000 " someo->Add((new util::filetools::Handler(dir))\n"
9001 " ->OnEvent1(NewPermanentCallback(\n"
9002 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9003 " ->OnEvent2(NewPermanentCallback(\n"
9004 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9005 " ->OnEvent3(NewPermanentCallback(\n"
9006 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9007 " ->OnEvent5(NewPermanentCallback(\n"
9008 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9009 " ->OnEvent6(NewPermanentCallback(\n"
9010 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9011 "}");
9012
9013 verifyFormat(
9014 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9015 verifyFormat("aaaaaaaaaaaaaaa()\n"
9016 " .aaaaaaaaaaaaaaa()\n"
9017 " .aaaaaaaaaaaaaaa()\n"
9018 " .aaaaaaaaaaaaaaa()\n"
9019 " .aaaaaaaaaaaaaaa();");
9020 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9021 " .aaaaaaaaaaaaaaa()\n"
9022 " .aaaaaaaaaaaaaaa()\n"
9023 " .aaaaaaaaaaaaaaa();");
9024 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9025 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9026 " .aaaaaaaaaaaaaaa();");
9027 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9028 " ->aaaaaaaaaaaaaae(0)\n"
9029 " ->aaaaaaaaaaaaaaa();");
9030
9031 // Don't linewrap after very short segments.
9032 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9033 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9034 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9035 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9036 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9038 verifyFormat("aaa()\n"
9039 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9040 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9041 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9042
9043 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9044 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9045 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9046 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9047 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9049
9050 // Prefer not to break after empty parentheses.
9051 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9052 " First->LastNewlineOffset);");
9053
9054 // Prefer not to create "hanging" indents.
9055 verifyFormat(
9056 "return !soooooooooooooome_map\n"
9057 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9058 " .second;");
9059 verifyFormat(
9060 "return aaaaaaaaaaaaaaaa\n"
9061 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9062 " .aaaa(aaaaaaaaaaaaaa);");
9063 // No hanging indent here.
9064 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9066 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9068 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9069 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9070 getLLVMStyleWithColumns(60));
9071 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9072 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9073 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9074 getLLVMStyleWithColumns(59));
9075 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9077 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9078
9079 // Dont break if only closing statements before member call
9080 verifyFormat("test() {\n"
9081 " ([]() -> {\n"
9082 " int b = 32;\n"
9083 " return 3;\n"
9084 " }).foo();\n"
9085 "}");
9086 verifyFormat("test() {\n"
9087 " (\n"
9088 " []() -> {\n"
9089 " int b = 32;\n"
9090 " return 3;\n"
9091 " },\n"
9092 " foo, bar)\n"
9093 " .foo();\n"
9094 "}");
9095 verifyFormat("test() {\n"
9096 " ([]() -> {\n"
9097 " int b = 32;\n"
9098 " return 3;\n"
9099 " })\n"
9100 " .foo()\n"
9101 " .bar();\n"
9102 "}");
9103 verifyFormat("test() {\n"
9104 " ([]() -> {\n"
9105 " int b = 32;\n"
9106 " return 3;\n"
9107 " })\n"
9108 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9109 " \"bbbb\");\n"
9110 "}",
9111 getLLVMStyleWithColumns(30));
9112 }
9113
9114 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9115 verifyFormat(
9116 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9117 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9118 verifyFormat(
9119 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9120 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9121
9122 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9123 " ccccccccccccccccccccccccc) {\n}");
9124 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9125 " ccccccccccccccccccccccccc) {\n}");
9126
9127 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9128 " ccccccccccccccccccccccccc) {\n}");
9129 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9130 " ccccccccccccccccccccccccc) {\n}");
9131
9132 verifyFormat(
9133 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9134 " ccccccccccccccccccccccccc) {\n}");
9135 verifyFormat(
9136 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9137 " ccccccccccccccccccccccccc) {\n}");
9138
9139 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9140 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9141 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9142 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9143 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9144 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9145 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9146 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9147
9148 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9149 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9150 " aaaaaaaaaaaaaaa != aa) {\n}");
9151 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9152 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9153 " aaaaaaaaaaaaaaa != aa) {\n}");
9154 }
9155
9156 TEST_F(FormatTest, BreaksAfterAssignments) {
9157 verifyFormat(
9158 "unsigned Cost =\n"
9159 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9160 " SI->getPointerAddressSpaceee());");
9161 verifyFormat(
9162 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9163 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9164
9165 verifyFormat(
9166 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9167 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9168 verifyFormat("unsigned OriginalStartColumn =\n"
9169 " SourceMgr.getSpellingColumnNumber(\n"
9170 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9171 " 1;");
9172 }
9173
9174 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9175 FormatStyle Style = getLLVMStyle();
9176 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9177 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9178 Style);
9179
9180 Style.PenaltyBreakAssignment = 20;
9181 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9182 " cccccccccccccccccccccccccc;",
9183 Style);
9184 }
9185
9186 TEST_F(FormatTest, AlignsAfterAssignments) {
9187 verifyFormat(
9188 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9189 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9190 verifyFormat(
9191 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9192 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9193 verifyFormat(
9194 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9195 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9196 verifyFormat(
9197 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9198 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9199 verifyFormat(
9200 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9201 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9202 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9203 }
9204
9205 TEST_F(FormatTest, AlignsAfterReturn) {
9206 verifyFormat(
9207 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9208 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9209 verifyFormat(
9210 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9211 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9212 verifyFormat(
9213 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9214 " aaaaaaaaaaaaaaaaaaaaaa();");
9215 verifyFormat(
9216 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9217 " aaaaaaaaaaaaaaaaaaaaaa());");
9218 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9220 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9223 verifyFormat("return\n"
9224 " // true if code is one of a or b.\n"
9225 " code == a || code == b;");
9226 }
9227
9228 TEST_F(FormatTest, AlignsAfterOpenBracket) {
9229 verifyFormat(
9230 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9231 " aaaaaaaaa aaaaaaa) {}");
9232 verifyFormat(
9233 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9234 " aaaaaaaaaaa aaaaaaaaa);");
9235 verifyFormat(
9236 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9237 " aaaaaaaaaaaaaaaaaaaaa));");
9238 FormatStyle Style = getLLVMStyle();
9239 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9240 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9241 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9242 Style);
9243 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9244 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9245 Style);
9246 verifyFormat("SomeLongVariableName->someFunction(\n"
9247 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9248 Style);
9249 verifyFormat(
9250 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9251 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9252 Style);
9253 verifyFormat(
9254 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9255 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9256 Style);
9257 verifyFormat(
9258 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9259 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9260 Style);
9261
9262 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9263 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9264 " b));",
9265 Style);
9266
9267 Style.ColumnLimit = 30;
9268 verifyFormat("for (int foo = 0; foo < FOO;\n"
9269 " ++foo) {\n"
9270 " bar(foo);\n"
9271 "}",
9272 Style);
9273 Style.ColumnLimit = 80;
9274
9275 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9276 Style.BinPackArguments = false;
9277 Style.BinPackParameters = false;
9278 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9279 " aaaaaaaaaaa aaaaaaaa,\n"
9280 " aaaaaaaaa aaaaaaa,\n"
9281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9282 Style);
9283 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9284 " aaaaaaaaaaa aaaaaaaaa,\n"
9285 " aaaaaaaaaaa aaaaaaaaa,\n"
9286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9287 Style);
9288 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9289 " aaaaaaaaaaaaaaa,\n"
9290 " aaaaaaaaaaaaaaaaaaaaa,\n"
9291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9292 Style);
9293 verifyFormat(
9294 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9295 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9296 Style);
9297 verifyFormat(
9298 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9299 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9300 Style);
9301 verifyFormat(
9302 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9303 " aaaaaaaaaaaaaaaaaaaaa(\n"
9304 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9305 " aaaaaaaaaaaaaaaa);",
9306 Style);
9307 verifyFormat(
9308 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9309 " aaaaaaaaaaaaaaaaaaaaa(\n"
9310 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9311 " aaaaaaaaaaaaaaaa);",
9312 Style);
9313
9314 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9315 Style.BinPackArguments = false;
9316 Style.BinPackParameters = false;
9317 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9318 " aaaaaaaaaaa aaaaaaaa,\n"
9319 " aaaaaaaaa aaaaaaa,\n"
9320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9321 ") {}",
9322 Style);
9323 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9324 " aaaaaaaaaaa aaaaaaaaa,\n"
9325 " aaaaaaaaaaa aaaaaaaaa,\n"
9326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9327 ");",
9328 Style);
9329 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9330 " aaaaaaaaaaaaaaa,\n"
9331 " aaaaaaaaaaaaaaaaaaaaa,\n"
9332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9333 "));",
9334 Style);
9335 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9336 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9337 "));",
9338 Style);
9339 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9340 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9341 "));",
9342 Style);
9343 verifyFormat(
9344 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9345 " aaaaaaaaaaaaaaaaaaaaa(\n"
9346 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9347 " ),\n"
9348 " aaaaaaaaaaaaaaaa\n"
9349 ");",
9350 Style);
9351 verifyFormat(
9352 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9353 " aaaaaaaaaaaaaaaaaaaaa(\n"
9354 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9355 " ) &&\n"
9356 " aaaaaaaaaaaaaaaa\n"
9357 ");",
9358 Style);
9359 }
9360
9361 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9362 FormatStyle Style = getLLVMStyleWithColumns(40);
9363 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9364 " bbbbbbbbbbbbbbbbbbbbbb);",
9365 Style);
9366 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9367 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9368 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9369 " bbbbbbbbbbbbbbbbbbbbbb);",
9370 Style);
9371 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9372 Style.AlignOperands = FormatStyle::OAS_Align;
9373 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9374 " bbbbbbbbbbbbbbbbbbbbbb);",
9375 Style);
9376 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9377 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9378 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9379 " bbbbbbbbbbbbbbbbbbbbbb);",
9380 Style);
9381 }
9382
9383 TEST_F(FormatTest, BreaksConditionalExpressions) {
9384 verifyFormat(
9385 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9386 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9387 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9388 verifyFormat(
9389 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9390 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9391 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9392 verifyFormat(
9393 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9394 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9395 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9396 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9397 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9398 verifyFormat(
9399 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9400 " : aaaaaaaaaaaaa);");
9401 verifyFormat(
9402 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9403 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9404 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9405 " aaaaaaaaaaaaa);");
9406 verifyFormat(
9407 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9408 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9409 " aaaaaaaaaaaaa);");
9410 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9411 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9413 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9415 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9417 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9419 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9421 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9422 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9424 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9426 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9427 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9428 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9429 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9430 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9432 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9433 " : aaaaaaaaaaaaaaaa;");
9434 verifyFormat(
9435 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9436 " ? aaaaaaaaaaaaaaa\n"
9437 " : aaaaaaaaaaaaaaa;");
9438 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9439 " aaaaaaaaa\n"
9440 " ? b\n"
9441 " : c);");
9442 verifyFormat("return aaaa == bbbb\n"
9443 " // comment\n"
9444 " ? aaaa\n"
9445 " : bbbb;");
9446 verifyFormat("unsigned Indent =\n"
9447 " format(TheLine.First,\n"
9448 " IndentForLevel[TheLine.Level] >= 0\n"
9449 " ? IndentForLevel[TheLine.Level]\n"
9450 " : TheLine * 2,\n"
9451 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9452 getLLVMStyleWithColumns(60));
9453 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9454 " ? aaaaaaaaaaaaaaa\n"
9455 " : bbbbbbbbbbbbbbb //\n"
9456 " ? ccccccccccccccc\n"
9457 " : ddddddddddddddd;");
9458 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9459 " ? aaaaaaaaaaaaaaa\n"
9460 " : (bbbbbbbbbbbbbbb //\n"
9461 " ? ccccccccccccccc\n"
9462 " : ddddddddddddddd);");
9463 verifyFormat(
9464 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9465 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9466 " aaaaaaaaaaaaaaaaaaaaa +\n"
9467 " aaaaaaaaaaaaaaaaaaaaa\n"
9468 " : aaaaaaaaaa;");
9469 verifyFormat(
9470 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9471 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9472 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9473
9474 FormatStyle NoBinPacking = getLLVMStyle();
9475 NoBinPacking.BinPackArguments = false;
9476 verifyFormat(
9477 "void f() {\n"
9478 " g(aaa,\n"
9479 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9481 " ? aaaaaaaaaaaaaaa\n"
9482 " : aaaaaaaaaaaaaaa);\n"
9483 "}",
9484 NoBinPacking);
9485 verifyFormat(
9486 "void f() {\n"
9487 " g(aaa,\n"
9488 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9490 " ?: aaaaaaaaaaaaaaa);\n"
9491 "}",
9492 NoBinPacking);
9493
9494 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9495 " // comment.\n"
9496 " ccccccccccccccccccccccccccccccccccccccc\n"
9497 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9498 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9499
9500 // Assignments in conditional expressions. Apparently not uncommon :-(.
9501 verifyFormat("return a != b\n"
9502 " // comment\n"
9503 " ? a = b\n"
9504 " : a = b;");
9505 verifyFormat("return a != b\n"
9506 " // comment\n"
9507 " ? a = a != b\n"
9508 " // comment\n"
9509 " ? a = b\n"
9510 " : a\n"
9511 " : a;");
9512 verifyFormat("return a != b\n"
9513 " // comment\n"
9514 " ? a\n"
9515 " : a = a != b\n"
9516 " // comment\n"
9517 " ? a = b\n"
9518 " : a;");
9519
9520 // Chained conditionals
9521 FormatStyle Style = getLLVMStyleWithColumns(70);
9522 Style.AlignOperands = FormatStyle::OAS_Align;
9523 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9524 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9525 " : 3333333333333333;",
9526 Style);
9527 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9528 " : bbbbbbbbbb ? 2222222222222222\n"
9529 " : 3333333333333333;",
9530 Style);
9531 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9532 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9533 " : 3333333333333333;",
9534 Style);
9535 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9536 " : bbbbbbbbbbbbbb ? 222222\n"
9537 " : 333333;",
9538 Style);
9539 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9540 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9541 " : cccccccccccccc ? 3333333333333333\n"
9542 " : 4444444444444444;",
9543 Style);
9544 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9545 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9546 " : 3333333333333333;",
9547 Style);
9548 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9549 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9550 " : (aaa ? bbb : ccc);",
9551 Style);
9552 verifyFormat(
9553 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9554 " : cccccccccccccccccc)\n"
9555 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9556 " : 3333333333333333;",
9557 Style);
9558 verifyFormat(
9559 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9560 " : cccccccccccccccccc)\n"
9561 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9562 " : 3333333333333333;",
9563 Style);
9564 verifyFormat(
9565 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9566 " : dddddddddddddddddd)\n"
9567 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9568 " : 3333333333333333;",
9569 Style);
9570 verifyFormat(
9571 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9572 " : dddddddddddddddddd)\n"
9573 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9574 " : 3333333333333333;",
9575 Style);
9576 verifyFormat(
9577 "return aaaaaaaaa ? 1111111111111111\n"
9578 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9579 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9580 " : dddddddddddddddddd)",
9581 Style);
9582 verifyFormat(
9583 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9584 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9585 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9586 " : cccccccccccccccccc);",
9587 Style);
9588 verifyFormat(
9589 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9590 " : ccccccccccccccc ? dddddddddddddddddd\n"
9591 " : eeeeeeeeeeeeeeeeee)\n"
9592 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9593 " : 3333333333333333;",
9594 Style);
9595 verifyFormat(
9596 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9597 " : ccccccccccccccc ? dddddddddddddddddd\n"
9598 " : eeeeeeeeeeeeeeeeee)\n"
9599 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9600 " : 3333333333333333;",
9601 Style);
9602 verifyFormat(
9603 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9604 " : cccccccccccc ? dddddddddddddddddd\n"
9605 " : eeeeeeeeeeeeeeeeee)\n"
9606 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9607 " : 3333333333333333;",
9608 Style);
9609 verifyFormat(
9610 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9611 " : cccccccccccccccccc\n"
9612 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9613 " : 3333333333333333;",
9614 Style);
9615 verifyFormat(
9616 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9617 " : cccccccccccccccc ? dddddddddddddddddd\n"
9618 " : eeeeeeeeeeeeeeeeee\n"
9619 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9620 " : 3333333333333333;",
9621 Style);
9622 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9623 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9624 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9625 " : eeeeeeeeeeeeeeeeee)\n"
9626 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9627 " : 3333333333333333;",
9628 Style);
9629 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9630 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9631 " : cccccccccccccccc ? dddddddddddddddddd\n"
9632 " : eeeeeeeeeeeeeeeeee\n"
9633 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9634 " : 3333333333333333;",
9635 Style);
9636
9637 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9638 Style.BreakBeforeTernaryOperators = false;
9639 // FIXME: Aligning the question marks is weird given DontAlign.
9640 // Consider disabling this alignment in this case. Also check whether this
9641 // will render the adjustment from https://reviews.llvm.org/D82199
9642 // unnecessary.
9643 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9644 " bbbb ? cccccccccccccccccc :\n"
9645 " ddddd;",
9646 Style);
9647
9648 verifyFormat(
9649 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9650 " /*\n"
9651 " */\n"
9652 " function() {\n"
9653 " try {\n"
9654 " return JJJJJJJJJJJJJJ(\n"
9655 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9656 " }\n"
9657 " } :\n"
9658 " function() {};",
9659 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9660 " /*\n"
9661 " */\n"
9662 " function() {\n"
9663 " try {\n"
9664 " return JJJJJJJJJJJJJJ(\n"
9665 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9666 " }\n"
9667 " } :\n"
9668 " function() {};",
9669 getGoogleStyle(FormatStyle::LK_JavaScript));
9670 }
9671
9672 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9673 FormatStyle Style = getLLVMStyleWithColumns(70);
9674 Style.BreakBeforeTernaryOperators = false;
9675 verifyFormat(
9676 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9679 Style);
9680 verifyFormat(
9681 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9682 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9684 Style);
9685 verifyFormat(
9686 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9688 Style);
9689 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9690 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9692 Style);
9693 verifyFormat(
9694 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9695 " aaaaaaaaaaaaa);",
9696 Style);
9697 verifyFormat(
9698 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9699 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9701 " aaaaaaaaaaaaa);",
9702 Style);
9703 verifyFormat(
9704 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9705 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9706 " aaaaaaaaaaaaa);",
9707 Style);
9708 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9713 Style);
9714 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9716 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9720 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9721 Style);
9722 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9726 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9727 Style);
9728 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9729 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9730 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9731 Style);
9732 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9736 Style);
9737 verifyFormat(
9738 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9739 " aaaaaaaaaaaaaaa :\n"
9740 " aaaaaaaaaaaaaaa;",
9741 Style);
9742 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9743 " aaaaaaaaa ?\n"
9744 " b :\n"
9745 " c);",
9746 Style);
9747 verifyFormat("unsigned Indent =\n"
9748 " format(TheLine.First,\n"
9749 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9750 " IndentForLevel[TheLine.Level] :\n"
9751 " TheLine * 2,\n"
9752 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9753 Style);
9754 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9755 " aaaaaaaaaaaaaaa :\n"
9756 " bbbbbbbbbbbbbbb ? //\n"
9757 " ccccccccccccccc :\n"
9758 " ddddddddddddddd;",
9759 Style);
9760 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9761 " aaaaaaaaaaaaaaa :\n"
9762 " (bbbbbbbbbbbbbbb ? //\n"
9763 " ccccccccccccccc :\n"
9764 " ddddddddddddddd);",
9765 Style);
9766 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9767 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9768 " ccccccccccccccccccccccccccc;",
9769 Style);
9770 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9771 " aaaaa :\n"
9772 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9773 Style);
9774
9775 // Chained conditionals
9776 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9777 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9778 " 3333333333333333;",
9779 Style);
9780 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9781 " bbbbbbbbbb ? 2222222222222222 :\n"
9782 " 3333333333333333;",
9783 Style);
9784 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9785 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9786 " 3333333333333333;",
9787 Style);
9788 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9789 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9790 " 333333;",
9791 Style);
9792 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9793 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9794 " cccccccccccccccc ? 3333333333333333 :\n"
9795 " 4444444444444444;",
9796 Style);
9797 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9798 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9799 " 3333333333333333;",
9800 Style);
9801 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9802 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9803 " (aaa ? bbb : ccc);",
9804 Style);
9805 verifyFormat(
9806 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9807 " cccccccccccccccccc) :\n"
9808 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9809 " 3333333333333333;",
9810 Style);
9811 verifyFormat(
9812 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9813 " cccccccccccccccccc) :\n"
9814 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9815 " 3333333333333333;",
9816 Style);
9817 verifyFormat(
9818 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9819 " dddddddddddddddddd) :\n"
9820 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9821 " 3333333333333333;",
9822 Style);
9823 verifyFormat(
9824 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9825 " dddddddddddddddddd) :\n"
9826 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9827 " 3333333333333333;",
9828 Style);
9829 verifyFormat(
9830 "return aaaaaaaaa ? 1111111111111111 :\n"
9831 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9832 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9833 " dddddddddddddddddd)",
9834 Style);
9835 verifyFormat(
9836 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9837 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9838 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9839 " cccccccccccccccccc);",
9840 Style);
9841 verifyFormat(
9842 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9843 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9844 " eeeeeeeeeeeeeeeeee) :\n"
9845 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9846 " 3333333333333333;",
9847 Style);
9848 verifyFormat(
9849 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9850 " ccccccccccccc ? dddddddddddddddddd :\n"
9851 " eeeeeeeeeeeeeeeeee) :\n"
9852 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9853 " 3333333333333333;",
9854 Style);
9855 verifyFormat(
9856 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9857 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9858 " eeeeeeeeeeeeeeeeee) :\n"
9859 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9860 " 3333333333333333;",
9861 Style);
9862 verifyFormat(
9863 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9864 " cccccccccccccccccc :\n"
9865 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9866 " 3333333333333333;",
9867 Style);
9868 verifyFormat(
9869 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9870 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9871 " eeeeeeeeeeeeeeeeee :\n"
9872 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9873 " 3333333333333333;",
9874 Style);
9875 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9876 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9877 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9878 " eeeeeeeeeeeeeeeeee) :\n"
9879 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9880 " 3333333333333333;",
9881 Style);
9882 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9883 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9884 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
9885 " eeeeeeeeeeeeeeeeee :\n"
9886 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9887 " 3333333333333333;",
9888 Style);
9889 }
9890
9891 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
9892 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
9893 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
9894 verifyFormat("bool a = true, b = false;");
9895
9896 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
9898 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
9899 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
9900 verifyFormat(
9901 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
9902 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
9903 " d = e && f;");
9904 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
9905 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
9906 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9907 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
9908 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
9909 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
9910
9911 FormatStyle Style = getGoogleStyle();
9912 Style.PointerAlignment = FormatStyle::PAS_Left;
9913 Style.DerivePointerAlignment = false;
9914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9915 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
9916 " *b = bbbbbbbbbbbbbbbbbbb;",
9917 Style);
9918 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9919 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
9920 Style);
9921 verifyFormat("vector<int*> a, b;", Style);
9922 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
9923 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
9924 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
9925 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
9926 Style);
9927 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
9928 Style);
9929 verifyFormat(
9930 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
9931 Style);
9932
9933 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
9934 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
9935 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
9936 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
9937 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
9938 Style);
9939 }
9940
9941 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
9942 verifyFormat("arr[foo ? bar : baz];");
9943 verifyFormat("f()[foo ? bar : baz];");
9944 verifyFormat("(a + b)[foo ? bar : baz];");
9945 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
9946 }
9947
9948 TEST_F(FormatTest, AlignsStringLiterals) {
9949 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
9950 " \"short literal\");");
9951 verifyFormat(
9952 "looooooooooooooooooooooooongFunction(\n"
9953 " \"short literal\"\n"
9954 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
9955 verifyFormat("someFunction(\"Always break between multi-line\"\n"
9956 " \" string literals\",\n"
9957 " also, other, parameters);");
9958 verifyFormat("fun + \"1243\" /* comment */\n"
9959 " \"5678\";",
9960 "fun + \"1243\" /* comment */\n"
9961 " \"5678\";",
9962 getLLVMStyleWithColumns(28));
9963 verifyFormat(
9964 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
9965 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
9966 " \"aaaaaaaaaaaaaaaa\";",
9967 "aaaaaa ="
9968 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
9969 "aaaaaaaaaaaaaaaaaaaaa\" "
9970 "\"aaaaaaaaaaaaaaaa\";");
9971 verifyFormat("a = a + \"a\"\n"
9972 " \"a\"\n"
9973 " \"a\";");
9974 verifyFormat("f(\"a\", \"b\"\n"
9975 " \"c\");");
9976
9977 verifyFormat(
9978 "#define LL_FORMAT \"ll\"\n"
9979 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
9980 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
9981
9982 verifyFormat("#define A(X) \\\n"
9983 " \"aaaaa\" #X \"bbbbbb\" \\\n"
9984 " \"ccccc\"",
9985 getLLVMStyleWithColumns(23));
9986 verifyFormat("#define A \"def\"\n"
9987 "f(\"abc\" A \"ghi\"\n"
9988 " \"jkl\");");
9989
9990 verifyFormat("f(L\"a\"\n"
9991 " L\"b\");");
9992 verifyFormat("#define A(X) \\\n"
9993 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
9994 " L\"ccccc\"",
9995 getLLVMStyleWithColumns(25));
9996
9997 verifyFormat("f(@\"a\"\n"
9998 " @\"b\");");
9999 verifyFormat("NSString s = @\"a\"\n"
10000 " @\"b\"\n"
10001 " @\"c\";");
10002 verifyFormat("NSString s = @\"a\"\n"
10003 " \"b\"\n"
10004 " \"c\";");
10005 }
10006
10007 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
10008 FormatStyle Style = getLLVMStyle();
10009 Style.ColumnLimit = 60;
10010
10011 // No declarations or definitions should be moved to own line.
10012 Style.BreakAfterReturnType = FormatStyle::RTBS_None;
10013 verifyFormat("class A {\n"
10014 " int f() { return 1; }\n"
10015 " int g();\n"
10016 " long\n"
10017 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10018 "};\n"
10019 "int f() { return 1; }\n"
10020 "int g();\n"
10021 "int foooooooooooooooooooooooooooo::\n"
10022 " baaaaaaaaaaaaaaaaaaaaar();",
10023 Style);
10024
10025 // It is now allowed to break after a short return type if necessary.
10026 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
10027 verifyFormat("class A {\n"
10028 " int f() { return 1; }\n"
10029 " int g();\n"
10030 " long\n"
10031 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10032 "};\n"
10033 "int f() { return 1; }\n"
10034 "int g();\n"
10035 "int\n"
10036 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10037 Style);
10038
10039 // It now must never break after a short return type.
10040 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
10041 verifyFormat("class A {\n"
10042 " int f() { return 1; }\n"
10043 " int g();\n"
10044 " long foooooooooooooooooooooooooooo::\n"
10045 " baaaaaaaaaaaaaaaaaaaar();\n"
10046 "};\n"
10047 "int f() { return 1; }\n"
10048 "int g();\n"
10049 "int foooooooooooooooooooooooooooo::\n"
10050 " baaaaaaaaaaaaaaaaaaaaar();",
10051 Style);
10052
10053 // All declarations and definitions should have the return type moved to its
10054 // own line.
10055 Style.BreakAfterReturnType = FormatStyle::RTBS_All;
10056 Style.TypenameMacros = {"LIST"};
10057 verifyFormat("SomeType\n"
10058 "funcdecl(LIST(uint64_t));",
10059 Style);
10060 verifyFormat("class E {\n"
10061 " int\n"
10062 " f() {\n"
10063 " return 1;\n"
10064 " }\n"
10065 " int\n"
10066 " g();\n"
10067 " long\n"
10068 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10069 "};\n"
10070 "int\n"
10071 "f() {\n"
10072 " return 1;\n"
10073 "}\n"
10074 "int\n"
10075 "g();\n"
10076 "int\n"
10077 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10078 Style);
10079
10080 // Top-level definitions, and no kinds of declarations should have the
10081 // return type moved to its own line.
10082 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10083 verifyFormat("class B {\n"
10084 " int f() { return 1; }\n"
10085 " int g();\n"
10086 "};\n"
10087 "int\n"
10088 "f() {\n"
10089 " return 1;\n"
10090 "}\n"
10091 "int g();",
10092 Style);
10093
10094 // Top-level definitions and declarations should have the return type moved
10095 // to its own line.
10096 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10097 verifyFormat("class C {\n"
10098 " int f() { return 1; }\n"
10099 " int g();\n"
10100 "};\n"
10101 "int\n"
10102 "f() {\n"
10103 " return 1;\n"
10104 "}\n"
10105 "int\n"
10106 "g();\n"
10107 "int\n"
10108 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10109 Style);
10110
10111 // All definitions should have the return type moved to its own line, but no
10112 // kinds of declarations.
10113 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10114 verifyFormat("class D {\n"
10115 " int\n"
10116 " f() {\n"
10117 " return 1;\n"
10118 " }\n"
10119 " int g();\n"
10120 "};\n"
10121 "int\n"
10122 "f() {\n"
10123 " return 1;\n"
10124 "}\n"
10125 "int g();",
10126 Style);
10127 verifyFormat("const char *\n"
10128 "f(void) {\n" // Break here.
10129 " return \"\";\n"
10130 "}\n"
10131 "const char *bar(void);", // No break here.
10132 Style);
10133 verifyFormat("template <class T>\n"
10134 "T *\n"
10135 "f(T &c) {\n" // Break here.
10136 " return NULL;\n"
10137 "}\n"
10138 "template <class T> T *f(T &c);", // No break here.
10139 Style);
10140 verifyFormat("class C {\n"
10141 " int\n"
10142 " operator+() {\n"
10143 " return 1;\n"
10144 " }\n"
10145 " int\n"
10146 " operator()() {\n"
10147 " return 1;\n"
10148 " }\n"
10149 "};",
10150 Style);
10151 verifyFormat("void\n"
10152 "A::operator()() {}\n"
10153 "void\n"
10154 "A::operator>>() {}\n"
10155 "void\n"
10156 "A::operator+() {}\n"
10157 "void\n"
10158 "A::operator*() {}\n"
10159 "void\n"
10160 "A::operator->() {}\n"
10161 "void\n"
10162 "A::operator void *() {}\n"
10163 "void\n"
10164 "A::operator void &() {}\n"
10165 "void\n"
10166 "A::operator void &&() {}\n"
10167 "void\n"
10168 "A::operator char *() {}\n"
10169 "void\n"
10170 "A::operator[]() {}\n"
10171 "void\n"
10172 "A::operator!() {}\n"
10173 "void\n"
10174 "A::operator**() {}\n"
10175 "void\n"
10176 "A::operator<Foo> *() {}\n"
10177 "void\n"
10178 "A::operator<Foo> **() {}\n"
10179 "void\n"
10180 "A::operator<Foo> &() {}\n"
10181 "void\n"
10182 "A::operator void **() {}",
10183 Style);
10184 verifyFormat("constexpr auto\n"
10185 "operator()() const -> reference {}\n"
10186 "constexpr auto\n"
10187 "operator>>() const -> reference {}\n"
10188 "constexpr auto\n"
10189 "operator+() const -> reference {}\n"
10190 "constexpr auto\n"
10191 "operator*() const -> reference {}\n"
10192 "constexpr auto\n"
10193 "operator->() const -> reference {}\n"
10194 "constexpr auto\n"
10195 "operator++() const -> reference {}\n"
10196 "constexpr auto\n"
10197 "operator void *() const -> reference {}\n"
10198 "constexpr auto\n"
10199 "operator void **() const -> reference {}\n"
10200 "constexpr auto\n"
10201 "operator void *() const -> reference {}\n"
10202 "constexpr auto\n"
10203 "operator void &() const -> reference {}\n"
10204 "constexpr auto\n"
10205 "operator void &&() const -> reference {}\n"
10206 "constexpr auto\n"
10207 "operator char *() const -> reference {}\n"
10208 "constexpr auto\n"
10209 "operator!() const -> reference {}\n"
10210 "constexpr auto\n"
10211 "operator[]() const -> reference {}",
10212 Style);
10213 verifyFormat("void *operator new(std::size_t s);", // No break here.
10214 Style);
10215 verifyFormat("void *\n"
10216 "operator new(std::size_t s) {}",
10217 Style);
10218 verifyFormat("void *\n"
10219 "operator delete[](void *ptr) {}",
10220 Style);
10221 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10222 verifyFormat("const char *\n"
10223 "f(void)\n" // Break here.
10224 "{\n"
10225 " return \"\";\n"
10226 "}\n"
10227 "const char *bar(void);", // No break here.
10228 Style);
10229 verifyFormat("template <class T>\n"
10230 "T *\n" // Problem here: no line break
10231 "f(T &c)\n" // Break here.
10232 "{\n"
10233 " return NULL;\n"
10234 "}\n"
10235 "template <class T> T *f(T &c);", // No break here.
10236 Style);
10237 verifyFormat("int\n"
10238 "foo(A<bool> a)\n"
10239 "{\n"
10240 " return a;\n"
10241 "}",
10242 Style);
10243 verifyFormat("int\n"
10244 "foo(A<8> a)\n"
10245 "{\n"
10246 " return a;\n"
10247 "}",
10248 Style);
10249 verifyFormat("int\n"
10250 "foo(A<B<bool>, 8> a)\n"
10251 "{\n"
10252 " return a;\n"
10253 "}",
10254 Style);
10255 verifyFormat("int\n"
10256 "foo(A<B<8>, bool> a)\n"
10257 "{\n"
10258 " return a;\n"
10259 "}",
10260 Style);
10261 verifyFormat("int\n"
10262 "foo(A<B<bool>, bool> a)\n"
10263 "{\n"
10264 " return a;\n"
10265 "}",
10266 Style);
10267 verifyFormat("int\n"
10268 "foo(A<B<8>, 8> a)\n"
10269 "{\n"
10270 " return a;\n"
10271 "}",
10272 Style);
10273
10274 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10275 Style.BraceWrapping.AfterFunction = true;
10276 verifyFormat("int f(i);\n" // No break here.
10277 "int\n" // Break here.
10278 "f(i)\n"
10279 "{\n"
10280 " return i + 1;\n"
10281 "}\n"
10282 "int\n" // Break here.
10283 "f(i)\n"
10284 "{\n"
10285 " return i + 1;\n"
10286 "};",
10287 Style);
10288 verifyFormat("int f(a, b, c);\n" // No break here.
10289 "int\n" // Break here.
10290 "f(a, b, c)\n" // Break here.
10291 "short a, b;\n"
10292 "float c;\n"
10293 "{\n"
10294 " return a + b < c;\n"
10295 "}\n"
10296 "int\n" // Break here.
10297 "f(a, b, c)\n" // Break here.
10298 "short a, b;\n"
10299 "float c;\n"
10300 "{\n"
10301 " return a + b < c;\n"
10302 "};",
10303 Style);
10304 verifyFormat("byte *\n" // Break here.
10305 "f(a)\n" // Break here.
10306 "byte a[];\n"
10307 "{\n"
10308 " return a;\n"
10309 "}",
10310 Style);
10311 verifyFormat("byte *\n"
10312 "f(a)\n"
10313 "byte /* K&R C */ a[];\n"
10314 "{\n"
10315 " return a;\n"
10316 "}\n"
10317 "byte *\n"
10318 "g(p)\n"
10319 "byte /* K&R C */ *p;\n"
10320 "{\n"
10321 " return p;\n"
10322 "}",
10323 Style);
10324 verifyFormat("bool f(int a, int) override;\n"
10325 "Bar g(int a, Bar) final;\n"
10326 "Bar h(a, Bar) final;",
10327 Style);
10328 verifyFormat("int\n"
10329 "f(a)",
10330 Style);
10331 verifyFormat("bool\n"
10332 "f(size_t = 0, bool b = false)\n"
10333 "{\n"
10334 " return !b;\n"
10335 "}",
10336 Style);
10337
10338 // The return breaking style doesn't affect:
10339 // * function and object definitions with attribute-like macros
10340 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10341 " ABSL_GUARDED_BY(mutex) = {};",
10342 getGoogleStyleWithColumns(40));
10343 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10344 " ABSL_GUARDED_BY(mutex); // comment",
10345 getGoogleStyleWithColumns(40));
10346 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10347 " ABSL_GUARDED_BY(mutex1)\n"
10348 " ABSL_GUARDED_BY(mutex2);",
10349 getGoogleStyleWithColumns(40));
10350 verifyFormat("Tttttt f(int a, int b)\n"
10351 " ABSL_GUARDED_BY(mutex1)\n"
10352 " ABSL_GUARDED_BY(mutex2);",
10353 getGoogleStyleWithColumns(40));
10354 // * typedefs
10355 verifyGoogleFormat("typedef ATTR(X) char x;");
10356
10357 Style = getGNUStyle();
10358
10359 // Test for comments at the end of function declarations.
10360 verifyFormat("void\n"
10361 "foo (int a, /*abc*/ int b) // def\n"
10362 "{\n"
10363 "}",
10364 Style);
10365
10366 verifyFormat("void\n"
10367 "foo (int a, /* abc */ int b) /* def */\n"
10368 "{\n"
10369 "}",
10370 Style);
10371
10372 // Definitions that should not break after return type
10373 verifyFormat("void foo (int a, int b); // def", Style);
10374 verifyFormat("void foo (int a, int b); /* def */", Style);
10375 verifyFormat("void foo (int a, int b);", Style);
10376 }
10377
10378 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10379 FormatStyle NoBreak = getLLVMStyle();
10380 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10381 FormatStyle Break = getLLVMStyle();
10382 Break.AlwaysBreakBeforeMultilineStrings = true;
10383 verifyFormat("aaaa = \"bbbb\"\n"
10384 " \"cccc\";",
10385 NoBreak);
10386 verifyFormat("aaaa =\n"
10387 " \"bbbb\"\n"
10388 " \"cccc\";",
10389 Break);
10390 verifyFormat("aaaa(\"bbbb\"\n"
10391 " \"cccc\");",
10392 NoBreak);
10393 verifyFormat("aaaa(\n"
10394 " \"bbbb\"\n"
10395 " \"cccc\");",
10396 Break);
10397 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10398 " \"cccc\");",
10399 NoBreak);
10400 verifyFormat("aaaa(qqq,\n"
10401 " \"bbbb\"\n"
10402 " \"cccc\");",
10403 Break);
10404 verifyFormat("aaaa(qqq,\n"
10405 " L\"bbbb\"\n"
10406 " L\"cccc\");",
10407 Break);
10408 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10409 " \"bbbb\"));",
10410 Break);
10411 verifyFormat("string s = someFunction(\n"
10412 " \"abc\"\n"
10413 " \"abc\");",
10414 Break);
10415
10416 // As we break before unary operators, breaking right after them is bad.
10417 verifyFormat("string foo = abc ? \"x\"\n"
10418 " \"blah blah blah blah blah blah\"\n"
10419 " : \"y\";",
10420 Break);
10421
10422 // Don't break if there is no column gain.
10423 verifyFormat("f(\"aaaa\"\n"
10424 " \"bbbb\");",
10425 Break);
10426
10427 // Treat literals with escaped newlines like multi-line string literals.
10428 verifyNoChange("x = \"a\\\n"
10429 "b\\\n"
10430 "c\";",
10431 NoBreak);
10432 verifyFormat("xxxx =\n"
10433 " \"a\\\n"
10434 "b\\\n"
10435 "c\";",
10436 "xxxx = \"a\\\n"
10437 "b\\\n"
10438 "c\";",
10439 Break);
10440
10441 verifyFormat("NSString *const kString =\n"
10442 " @\"aaaa\"\n"
10443 " @\"bbbb\";",
10444 "NSString *const kString = @\"aaaa\"\n"
10445 "@\"bbbb\";",
10446 Break);
10447
10448 Break.ColumnLimit = 0;
10449 verifyFormat("const char *hello = \"hello llvm\";", Break);
10450 }
10451
10452 TEST_F(FormatTest, AlignsPipes) {
10453 verifyFormat(
10454 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10455 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10456 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10457 verifyFormat(
10458 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10459 " << aaaaaaaaaaaaaaaaaaaa;");
10460 verifyFormat(
10461 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10462 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10463 verifyFormat(
10464 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10465 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10466 verifyFormat(
10467 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10468 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10469 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10470 verifyFormat(
10471 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10472 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10473 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10474 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10477 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10478 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10479 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10480 verifyFormat(
10481 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10483 verifyFormat(
10484 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10485 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10486
10487 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10488 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10489 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10491 " aaaaaaaaaaaaaaaaaaaaa)\n"
10492 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10493 verifyFormat("LOG_IF(aaa == //\n"
10494 " bbb)\n"
10495 " << a << b;");
10496
10497 // But sometimes, breaking before the first "<<" is desirable.
10498 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10499 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10500 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10501 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10502 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10503 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10504 " << BEF << IsTemplate << Description << E->getType();");
10505 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10506 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10508 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10509 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10511 " << aaa;");
10512
10513 verifyFormat(
10514 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10515 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10516
10517 // Incomplete string literal.
10518 verifyFormat("llvm::errs() << \"\n"
10519 " << a;",
10520 "llvm::errs() << \"\n<<a;");
10521
10522 verifyFormat("void f() {\n"
10523 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10524 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10525 "}");
10526
10527 // Handle 'endl'.
10528 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10529 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10530 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10531
10532 // Handle '\n'.
10533 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10534 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10535 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10536 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10537 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10538 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10539 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10540 }
10541
10542 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10543 verifyFormat("return out << \"somepacket = {\\n\"\n"
10544 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10545 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10546 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10547 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10548 " << \"}\";");
10549
10550 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10551 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10552 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10553 verifyFormat(
10554 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10555 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10556 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10557 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10558 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10559 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10560 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10561 verifyFormat(
10562 "void f() {\n"
10563 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10564 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10565 "}");
10566
10567 // Breaking before the first "<<" is generally not desirable.
10568 verifyFormat(
10569 "llvm::errs()\n"
10570 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10571 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10572 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10573 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10574 getLLVMStyleWithColumns(70));
10575 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10576 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10577 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10578 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10579 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10580 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10581 getLLVMStyleWithColumns(70));
10582
10583 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10584 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10585 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10586 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10587 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10588 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10589 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10590 " (aaaa + aaaa);",
10591 getLLVMStyleWithColumns(40));
10592 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10593 " (aaaaaaa + aaaaa));",
10594 getLLVMStyleWithColumns(40));
10595 verifyFormat(
10596 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10597 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10598 " bbbbbbbbbbbbbbbbbbbbbbb);");
10599 }
10600
10601 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10602 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10603
10604 verifyNoChange("QStringList() << \"foo\"\n"
10605 " << \"bar\";");
10606
10607 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10608 "log_error(log, \"foo\"\n"
10609 " << \"bar\");");
10610 }
10611
10612 TEST_F(FormatTest, UnderstandsEquals) {
10613 verifyFormat(
10614 "aaaaaaaaaaaaaaaaa =\n"
10615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10616 verifyFormat(
10617 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10619 verifyFormat(
10620 "if (a) {\n"
10621 " f();\n"
10622 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10624 "}");
10625
10626 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10627 " 100000000 + 10000000) {\n}");
10628 }
10629
10630 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10631 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10632 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10633
10634 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10635 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10636
10637 verifyFormat(
10638 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10639 " Parameter2);");
10640
10641 verifyFormat(
10642 "ShortObject->shortFunction(\n"
10643 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10644 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10645
10646 verifyFormat("loooooooooooooongFunction(\n"
10647 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10648
10649 verifyFormat(
10650 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10651 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10652
10653 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10654 " .WillRepeatedly(Return(SomeValue));");
10655 verifyFormat("void f() {\n"
10656 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10657 " .Times(2)\n"
10658 " .WillRepeatedly(Return(SomeValue));\n"
10659 "}");
10660 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10661 " ccccccccccccccccccccccc);");
10662 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10664 " .aaaaa(aaaaa),\n"
10665 " aaaaaaaaaaaaaaaaaaaaa);");
10666 verifyFormat("void f() {\n"
10667 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10668 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10669 "}");
10670 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10672 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10673 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10674 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10675 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10676 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10677 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10678 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10679 "}");
10680
10681 // Here, it is not necessary to wrap at "." or "->".
10682 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10683 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10684 verifyFormat(
10685 "aaaaaaaaaaa->aaaaaaaaa(\n"
10686 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10687 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10688
10689 verifyFormat(
10690 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10692 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10693 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10694 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10695 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10696
10697 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10699 " .a();");
10700
10701 FormatStyle NoBinPacking = getLLVMStyle();
10702 NoBinPacking.BinPackParameters = false;
10703 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10704 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10705 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10706 " aaaaaaaaaaaaaaaaaaa,\n"
10707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10708 NoBinPacking);
10709
10710 // If there is a subsequent call, change to hanging indentation.
10711 verifyFormat(
10712 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10713 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10714 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10715 verifyFormat(
10716 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10717 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10718 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10720 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10721 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10723 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10724 }
10725
10726 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10727 verifyFormat("template <typename T>\n"
10728 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10729 verifyFormat("template <typename T>\n"
10730 "// T should be one of {A, B}.\n"
10731 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10732 verifyFormat(
10733 "template <typename T>\n"
10734 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10735 verifyFormat("template <typename T>\n"
10736 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10737 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10738 verifyFormat(
10739 "template <typename T>\n"
10740 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10741 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10742 verifyFormat(
10743 "template <typename T>\n"
10744 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10745 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10746 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10747 verifyFormat("template <typename T>\n"
10748 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10749 " int aaaaaaaaaaaaaaaaaaaaaa);");
10750 verifyFormat(
10751 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10752 " typename T4 = char>\n"
10753 "void f();");
10754 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10755 " template <typename> class cccccccccccccccccccccc,\n"
10756 " typename ddddddddddddd>\n"
10757 "class C {};");
10758 verifyFormat(
10759 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10761
10762 verifyFormat("void f() {\n"
10763 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10764 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10765 "}");
10766
10767 verifyFormat("template <typename T> class C {};");
10768 verifyFormat("template <typename T> void f();");
10769 verifyFormat("template <typename T> void f() {}");
10770 verifyFormat(
10771 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10773 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10774 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10777 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10778 getLLVMStyleWithColumns(72));
10779 verifyFormat("static_cast<A< //\n"
10780 " B> *>(\n"
10781 "\n"
10782 ");",
10783 "static_cast<A<//\n"
10784 " B>*>(\n"
10785 "\n"
10786 " );");
10787 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10788 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10789
10790 FormatStyle AlwaysBreak = getLLVMStyle();
10791 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10792 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10793 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10794 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10795 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10796 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10797 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10798 verifyFormat("template <template <typename> class Fooooooo,\n"
10799 " template <typename> class Baaaaaaar>\n"
10800 "struct C {};",
10801 AlwaysBreak);
10802 verifyFormat("template <typename T> // T can be A, B or C.\n"
10803 "struct C {};",
10804 AlwaysBreak);
10805 verifyFormat("template <typename T>\n"
10806 "C(T) noexcept;",
10807 AlwaysBreak);
10808 verifyFormat("template <typename T>\n"
10809 "ClassName(T) noexcept;",
10810 AlwaysBreak);
10811 verifyFormat("template <typename T>\n"
10812 "POOR_NAME(T) noexcept;",
10813 AlwaysBreak);
10814 verifyFormat("template <enum E> class A {\n"
10815 "public:\n"
10816 " E *f();\n"
10817 "};");
10818
10819 FormatStyle NeverBreak = getLLVMStyle();
10820 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
10821 verifyFormat("template <typename T> class C {};", NeverBreak);
10822 verifyFormat("template <typename T> void f();", NeverBreak);
10823 verifyFormat("template <typename T> void f() {}", NeverBreak);
10824 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10825 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10826 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10827 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10828 "bbbbbbbbbbbbbbbbbbbb) {}",
10829 NeverBreak);
10830 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10831 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10832 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10833 NeverBreak);
10834 verifyFormat("template <template <typename> class Fooooooo,\n"
10835 " template <typename> class Baaaaaaar>\n"
10836 "struct C {};",
10837 NeverBreak);
10838 verifyFormat("template <typename T> // T can be A, B or C.\n"
10839 "struct C {};",
10840 NeverBreak);
10841 verifyFormat("template <enum E> class A {\n"
10842 "public:\n"
10843 " E *f();\n"
10844 "};",
10845 NeverBreak);
10846 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10847 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10848 "bbbbbbbbbbbbbbbbbbbb) {}",
10849 NeverBreak);
10850
10851 auto Style = getLLVMStyle();
10852 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
10853
10854 verifyNoChange("template <typename T>\n"
10855 "class C {};",
10856 Style);
10857 verifyFormat("template <typename T> class C {};", Style);
10858
10859 verifyNoChange("template <typename T>\n"
10860 "void f();",
10861 Style);
10862 verifyFormat("template <typename T> void f();", Style);
10863
10864 verifyNoChange("template <typename T>\n"
10865 "void f() {}",
10866 Style);
10867 verifyFormat("template <typename T> void f() {}", Style);
10868
10869 verifyNoChange("template <typename T>\n"
10870 "// T can be A, B or C.\n"
10871 "struct C {};",
10872 Style);
10873 verifyFormat("template <typename T> // T can be A, B or C.\n"
10874 "struct C {};",
10875 Style);
10876
10877 verifyNoChange("template <typename T>\n"
10878 "C(T) noexcept;",
10879 Style);
10880 verifyFormat("template <typename T> C(T) noexcept;", Style);
10881
10882 verifyNoChange("template <enum E>\n"
10883 "class A {\n"
10884 "public:\n"
10885 " E *f();\n"
10886 "};",
10887 Style);
10888 verifyFormat("template <enum E> class A {\n"
10889 "public:\n"
10890 " E *f();\n"
10891 "};",
10892 Style);
10893
10894 verifyNoChange("template <auto x>\n"
10895 "constexpr int simple(int) {\n"
10896 " char c;\n"
10897 " return 1;\n"
10898 "}",
10899 Style);
10900 verifyFormat("template <auto x> constexpr int simple(int) {\n"
10901 " char c;\n"
10902 " return 1;\n"
10903 "}",
10904 Style);
10905
10906 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
10907 verifyNoChange("template <auto x>\n"
10908 "requires(x > 1)\n"
10909 "constexpr int with_req(int) {\n"
10910 " return 1;\n"
10911 "}",
10912 Style);
10913 verifyFormat("template <auto x> requires(x > 1)\n"
10914 "constexpr int with_req(int) {\n"
10915 " return 1;\n"
10916 "}",
10917 Style);
10918 }
10919
10920 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
10921 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
10922 Style.ColumnLimit = 60;
10923 verifyFormat("// Baseline - no comments.\n"
10924 "template <\n"
10925 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10926 "void f() {}",
10927 Style);
10928
10929 verifyFormat("template <\n"
10930 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10931 "void f() {}",
10932 "template <\n"
10933 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10934 "void f() {}",
10935 Style);
10936
10937 verifyFormat(
10938 "template <\n"
10939 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10940 "void f() {}",
10941 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10942 "void f() {}",
10943 Style);
10944
10945 verifyFormat("template <\n"
10946 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10947 " // multiline\n"
10948 "void f() {}",
10949 "template <\n"
10950 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10951 " // multiline\n"
10952 "void f() {}",
10953 Style);
10954
10955 verifyFormat(
10956 "template <typename aaaaaaaaaa<\n"
10957 " bbbbbbbbbbbb>::value> // trailing loooong\n"
10958 "void f() {}",
10959 "template <\n"
10960 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
10961 "void f() {}",
10962 Style);
10963 }
10964
10965 TEST_F(FormatTest, WrapsTemplateParameters) {
10966 FormatStyle Style = getLLVMStyle();
10967 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10968 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10969 verifyFormat(
10970 "template <typename... a> struct q {};\n"
10971 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10972 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10973 " y;",
10974 Style);
10975 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10976 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10977 verifyFormat(
10978 "template <typename... a> struct r {};\n"
10979 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10980 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10981 " y;",
10982 Style);
10983 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10984 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10985 verifyFormat("template <typename... a> struct s {};\n"
10986 "extern s<\n"
10987 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10988 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10989 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10990 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10991 " y;",
10992 Style);
10993 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10994 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10995 verifyFormat("template <typename... a> struct t {};\n"
10996 "extern t<\n"
10997 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10998 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10999 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11000 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11001 " y;",
11002 Style);
11003 }
11004
11005 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
11006 verifyFormat(
11007 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11009 verifyFormat(
11010 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11013
11014 // FIXME: Should we have the extra indent after the second break?
11015 verifyFormat(
11016 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11019
11020 verifyFormat(
11021 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11022 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11023
11024 // Breaking at nested name specifiers is generally not desirable.
11025 verifyFormat(
11026 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11027 " aaaaaaaaaaaaaaaaaaaaaaa);");
11028
11029 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11032 " aaaaaaaaaaaaaaaaaaaaa);",
11033 getLLVMStyleWithColumns(74));
11034
11035 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11038
11039 verifyFormat(
11040 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11041 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11042 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11043 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11044 }
11045
11046 TEST_F(FormatTest, UnderstandsTemplateParameters) {
11047 verifyFormat("A<int> a;");
11048 verifyFormat("A<A<A<int>>> a;");
11049 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11050 verifyFormat("bool x = a < 1 || 2 > a;");
11051 verifyFormat("bool x = 5 < f<int>();");
11052 verifyFormat("bool x = f<int>() > 5;");
11053 verifyFormat("bool x = 5 < a<int>::x;");
11054 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11055 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11056
11057 verifyGoogleFormat("A<A<int>> a;");
11058 verifyGoogleFormat("A<A<A<int>>> a;");
11059 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11060 verifyGoogleFormat("A<A<int> > a;");
11061 verifyGoogleFormat("A<A<A<int> > > a;");
11062 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11063 verifyGoogleFormat("A<::A<int>> a;");
11064 verifyGoogleFormat("A<::A> a;");
11065 verifyGoogleFormat("A< ::A> a;");
11066 verifyGoogleFormat("A< ::A<int> > a;");
11067 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11068 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11069 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11070 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11071 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11072 getGoogleStyle());
11073
11074 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11075
11076 // template closer followed by a token that starts with > or =
11077 verifyFormat("bool b = a<1> > 1;");
11078 verifyFormat("bool b = a<1> >= 1;");
11079 verifyFormat("int i = a<1> >> 1;");
11080 FormatStyle Style = getLLVMStyle();
11081 Style.SpaceBeforeAssignmentOperators = false;
11082 verifyFormat("bool b= a<1> == 1;", Style);
11083 verifyFormat("a<int> = 1;", Style);
11084 verifyFormat("a<int> >>= 1;", Style);
11085
11086 verifyFormat("test < a | b >> c;");
11087 verifyFormat("test<test<a | b>> c;");
11088 verifyFormat("test >> a >> b;");
11089 verifyFormat("test << a >> b;");
11090
11091 verifyFormat("f<int>();");
11092 verifyFormat("template <typename T> void f() {}");
11093 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11094 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11095 "sizeof(char)>::type>;");
11096 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11097 verifyFormat("f(a.operator()<A>());");
11098 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11099 " .template operator()<A>());",
11100 getLLVMStyleWithColumns(35));
11101 verifyFormat("bool_constant<a && noexcept(f())>;");
11102 verifyFormat("bool_constant<a || noexcept(f())>;");
11103
11104 verifyFormat("if (std::tuple_size_v<T> > 0)");
11105
11106 // Not template parameters.
11107 verifyFormat("return a < b && c > d;");
11108 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11109 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11110 verifyFormat("void f() {\n"
11111 " while (a < b && c > d) {\n"
11112 " }\n"
11113 "}");
11114 verifyFormat("template <typename... Types>\n"
11115 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11116
11117 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11119 getLLVMStyleWithColumns(60));
11120 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11121 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11122 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11123 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11124
11125 verifyFormat("#define FOO(typeName, realClass) \\\n"
11126 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11127 getLLVMStyleWithColumns(60));
11128 }
11129
11130 TEST_F(FormatTest, UnderstandsShiftOperators) {
11131 verifyFormat("if (i < x >> 1)");
11132 verifyFormat("while (i < x >> 1)");
11133 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11134 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11135 verifyFormat(
11136 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11137 verifyFormat("Foo.call<Bar<Function>>()");
11138 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11139 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11140 "++i, v = v >> 1)");
11141 verifyFormat("if (w<u<v<x>>, 1>::t)");
11142 }
11143
11144 TEST_F(FormatTest, BitshiftOperatorWidth) {
11145 verifyFormat("int a = 1 << 2; /* foo\n"
11146 " bar */",
11147 "int a=1<<2; /* foo\n"
11148 " bar */");
11149
11150 verifyFormat("int b = 256 >> 1; /* foo\n"
11151 " bar */",
11152 "int b =256>>1 ; /* foo\n"
11153 " bar */");
11154 }
11155
11156 TEST_F(FormatTest, UnderstandsBinaryOperators) {
11157 verifyFormat("COMPARE(a, ==, b);");
11158 verifyFormat("auto s = sizeof...(Ts) - 1;");
11159 }
11160
11161 TEST_F(FormatTest, UnderstandsPointersToMembers) {
11162 verifyFormat("int A::*x;");
11163 verifyFormat("int (S::*func)(void *);");
11164 verifyFormat("void f() { int (S::*func)(void *); }");
11165 verifyFormat("typedef bool *(Class::*Member)() const;");
11166 verifyFormat("void f() {\n"
11167 " (a->*f)();\n"
11168 " a->*x;\n"
11169 " (a.*f)();\n"
11170 " ((*a).*f)();\n"
11171 " a.*x;\n"
11172 "}");
11173 verifyFormat("void f() {\n"
11174 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11175 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11176 "}");
11177 verifyFormat(
11178 "(aaaaaaaaaa->*bbbbbbb)(\n"
11179 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11180
11181 FormatStyle Style = getLLVMStyle();
11182 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11183 verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11184 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
11185
11186 Style.PointerAlignment = FormatStyle::PAS_Left;
11187 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
11188 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11189
11190 Style.PointerAlignment = FormatStyle::PAS_Middle;
11191 verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11192 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11193 }
11194
11195 TEST_F(FormatTest, UnderstandsUnaryOperators) {
11196 verifyFormat("int a = -2;");
11197 verifyFormat("f(-1, -2, -3);");
11198 verifyFormat("a[-1] = 5;");
11199 verifyFormat("int a = 5 + -2;");
11200 verifyFormat("if (i == -1) {\n}");
11201 verifyFormat("if (i != -1) {\n}");
11202 verifyFormat("if (i > -1) {\n}");
11203 verifyFormat("if (i < -1) {\n}");
11204 verifyFormat("++(a->f());");
11205 verifyFormat("--(a->f());");
11206 verifyFormat("(a->f())++;");
11207 verifyFormat("a[42]++;");
11208 verifyFormat("if (!(a->f())) {\n}");
11209 verifyFormat("if (!+i) {\n}");
11210 verifyFormat("~&a;");
11211 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11212 verifyFormat("sizeof -x");
11213 verifyFormat("sizeof +x");
11214 verifyFormat("sizeof *x");
11215 verifyFormat("sizeof &x");
11216 verifyFormat("delete +x;");
11217 verifyFormat("co_await +x;");
11218 verifyFormat("case *x:");
11219 verifyFormat("case &x:");
11220
11221 verifyFormat("a-- > b;");
11222 verifyFormat("b ? -a : c;");
11223 verifyFormat("n * sizeof char16;");
11224 verifyGoogleFormat("n * alignof char16;");
11225 verifyFormat("sizeof(char);");
11226 verifyGoogleFormat("alignof(char);");
11227
11228 verifyFormat("return -1;");
11229 verifyFormat("throw -1;");
11230 verifyFormat("switch (a) {\n"
11231 "case -1:\n"
11232 " break;\n"
11233 "}");
11234 verifyFormat("#define X -1");
11235 verifyFormat("#define X -kConstant");
11236
11237 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11238 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11239
11240 verifyFormat("int a = /* confusing comment */ -1;");
11241 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11242 verifyFormat("int a = i /* confusing comment */++;");
11243
11244 verifyFormat("co_yield -1;");
11245 verifyFormat("co_return -1;");
11246
11247 // Check that * is not treated as a binary operator when we set
11248 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11249 FormatStyle PASLeftStyle = getLLVMStyle();
11250 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11251 verifyFormat("co_return *a;", PASLeftStyle);
11252 verifyFormat("co_await *a;", PASLeftStyle);
11253 verifyFormat("co_yield *a", PASLeftStyle);
11254 verifyFormat("return *a;", PASLeftStyle);
11255 }
11256
11257 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11258 verifyFormat("if (!aaaaaaaaaa( // break\n"
11259 " aaaaa)) {\n"
11260 "}");
11261 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11262 " aaaaa));");
11263 verifyFormat("*aaa = aaaaaaa( // break\n"
11264 " bbbbbb);");
11265 }
11266
11267 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11268 verifyFormat("bool operator<();");
11269 verifyFormat("bool operator>();");
11270 verifyFormat("bool operator=();");
11271 verifyFormat("bool operator==();");
11272 verifyFormat("bool operator!=();");
11273 verifyFormat("int operator+();");
11274 verifyFormat("int operator++();");
11275 verifyFormat("int operator++(int) volatile noexcept;");
11276 verifyFormat("bool operator,();");
11277 verifyFormat("bool operator();");
11278 verifyFormat("bool operator()();");
11279 verifyFormat("bool operator[]();");
11280 verifyFormat("operator bool();");
11281 verifyFormat("operator int();");
11282 verifyFormat("operator void *();");
11283 verifyFormat("operator SomeType<int>();");
11284 verifyFormat("operator SomeType<int, int>();");
11285 verifyFormat("operator SomeType<SomeType<int>>();");
11286 verifyFormat("operator< <>();");
11287 verifyFormat("operator<< <>();");
11288 verifyFormat("< <>");
11289
11290 verifyFormat("void *operator new(std::size_t size);");
11291 verifyFormat("void *operator new[](std::size_t size);");
11292 verifyFormat("void operator delete(void *ptr);");
11293 verifyFormat("void operator delete[](void *ptr);");
11294 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11295 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11296 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11297 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11298
11299 verifyFormat(
11300 "ostream &operator<<(ostream &OutputStream,\n"
11301 " SomeReallyLongType WithSomeReallyLongValue);");
11302 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11303 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11304 " return left.group < right.group;\n"
11305 "}");
11306 verifyFormat("SomeType &operator=(const SomeType &S);");
11307 verifyFormat("f.template operator()<int>();");
11308
11309 verifyGoogleFormat("operator void*();");
11310 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11311 verifyGoogleFormat("operator ::A();");
11312
11313 verifyFormat("using A::operator+;");
11314 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11315 "int i;");
11316
11317 // Calling an operator as a member function.
11318 verifyFormat("void f() { a.operator*(); }");
11319 verifyFormat("void f() { a.operator*(b & b); }");
11320 verifyFormat("void f() { a->operator&(a * b); }");
11321 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11322 verifyFormat("void f() { operator*(a & a); }");
11323 verifyFormat("void f() { operator&(a, b * b); }");
11324
11325 verifyFormat("void f() { return operator()(x) * b; }");
11326 verifyFormat("void f() { return operator[](x) * b; }");
11327 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11328 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11329 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11330 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11331 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11332
11333 verifyFormat("::operator delete(foo);");
11334 verifyFormat("::operator new(n * sizeof(foo));");
11335 verifyFormat("foo() { ::operator delete(foo); }");
11336 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11337 }
11338
11339 TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11340 verifyFormat("C<&operator- > minus;");
11341 verifyFormat("C<&operator> > gt;");
11342 verifyFormat("C<&operator>= > ge;");
11343 verifyFormat("C<&operator<= > le;");
11344 verifyFormat("C<&operator< <X>> lt;");
11345 }
11346
11347 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11348 verifyFormat("void A::b() && {}");
11349 verifyFormat("void A::b() && noexcept {}");
11350 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11351 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11352 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11353 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11354 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11355 verifyFormat("Deleted &operator=(const Deleted &) &;");
11356 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11357 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11358 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11359 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11360 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11361 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11362 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11363 verifyFormat("void Fn(T const &) const &;");
11364 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11365 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11366 verifyGoogleFormat("template <typename T>\n"
11367 "void F(T) && = delete;");
11368 verifyFormat("template <typename T> void operator=(T) &;");
11369 verifyFormat("template <typename T> void operator=(T) const &;");
11370 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11371 verifyFormat("template <typename T> void operator=(T) & = default;");
11372 verifyFormat("template <typename T> void operator=(T) &&;");
11373 verifyFormat("template <typename T> void operator=(T) && = delete;");
11374 verifyFormat("template <typename T> void operator=(T) & {}");
11375 verifyFormat("template <typename T> void operator=(T) && {}");
11376
11377 FormatStyle AlignLeft = getLLVMStyle();
11378 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11379 verifyFormat("void A::b() && {}", AlignLeft);
11380 verifyFormat("void A::b() && noexcept {}", AlignLeft);
11381 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11382 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11383 AlignLeft);
11384 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11385 AlignLeft);
11386 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11387 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11388 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11389 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11390 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11391 verifyFormat("auto Function(T) & -> void;", AlignLeft);
11392 verifyFormat("void Fn(T const&) const&;", AlignLeft);
11393 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11394 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11395 AlignLeft);
11396 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11397 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11398 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11399 AlignLeft);
11400 verifyFormat("template <typename T> void operator=(T) & = default;",
11401 AlignLeft);
11402 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11403 verifyFormat("template <typename T> void operator=(T) && = delete;",
11404 AlignLeft);
11405 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11406 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11407
11408 FormatStyle AlignMiddle = getLLVMStyle();
11409 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11410 verifyFormat("void A::b() && {}", AlignMiddle);
11411 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11412 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11413 AlignMiddle);
11414 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11415 AlignMiddle);
11416 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11417 AlignMiddle);
11418 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11419 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11420 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11421 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11422 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11423 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11424 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11425 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11426 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11427 AlignMiddle);
11428 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11429 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11430 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11431 AlignMiddle);
11432 verifyFormat("template <typename T> void operator=(T) & = default;",
11433 AlignMiddle);
11434 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11435 verifyFormat("template <typename T> void operator=(T) && = delete;",
11436 AlignMiddle);
11437 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11438 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11439
11440 FormatStyle Spaces = getLLVMStyle();
11441 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11442 Spaces.SpacesInParensOptions = {};
11443 Spaces.SpacesInParensOptions.InCStyleCasts = true;
11444 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11445 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11446 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11447 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11448
11449 Spaces.SpacesInParensOptions.InCStyleCasts = false;
11450 Spaces.SpacesInParensOptions.Other = true;
11451 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11452 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11453 Spaces);
11454 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11455 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11456
11457 FormatStyle BreakTemplate = getLLVMStyle();
11458 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11459
11460 verifyFormat("struct f {\n"
11461 " template <class T>\n"
11462 " int &foo(const std::string &str) & noexcept {}\n"
11463 "};",
11464 BreakTemplate);
11465
11466 verifyFormat("struct f {\n"
11467 " template <class T>\n"
11468 " int &foo(const std::string &str) && noexcept {}\n"
11469 "};",
11470 BreakTemplate);
11471
11472 verifyFormat("struct f {\n"
11473 " template <class T>\n"
11474 " int &foo(const std::string &str) const & noexcept {}\n"
11475 "};",
11476 BreakTemplate);
11477
11478 verifyFormat("struct f {\n"
11479 " template <class T>\n"
11480 " int &foo(const std::string &str) const & noexcept {}\n"
11481 "};",
11482 BreakTemplate);
11483
11484 verifyFormat("struct f {\n"
11485 " template <class T>\n"
11486 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11487 "};",
11488 BreakTemplate);
11489
11490 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11491 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11492 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11493
11494 verifyFormat("struct f {\n"
11495 " template <class T>\n"
11496 " int& foo(const std::string& str) & noexcept {}\n"
11497 "};",
11498 AlignLeftBreakTemplate);
11499
11500 verifyFormat("struct f {\n"
11501 " template <class T>\n"
11502 " int& foo(const std::string& str) && noexcept {}\n"
11503 "};",
11504 AlignLeftBreakTemplate);
11505
11506 verifyFormat("struct f {\n"
11507 " template <class T>\n"
11508 " int& foo(const std::string& str) const& noexcept {}\n"
11509 "};",
11510 AlignLeftBreakTemplate);
11511
11512 verifyFormat("struct f {\n"
11513 " template <class T>\n"
11514 " int& foo(const std::string& str) const&& noexcept {}\n"
11515 "};",
11516 AlignLeftBreakTemplate);
11517
11518 verifyFormat("struct f {\n"
11519 " template <class T>\n"
11520 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11521 "};",
11522 AlignLeftBreakTemplate);
11523
11524 // The `&` in `Type&` should not be confused with a trailing `&` of
11525 // DEPRECATED(reason) member function.
11526 verifyFormat("struct f {\n"
11527 " template <class T>\n"
11528 " DEPRECATED(reason)\n"
11529 " Type &foo(arguments) {}\n"
11530 "};",
11531 BreakTemplate);
11532
11533 verifyFormat("struct f {\n"
11534 " template <class T>\n"
11535 " DEPRECATED(reason)\n"
11536 " Type& foo(arguments) {}\n"
11537 "};",
11538 AlignLeftBreakTemplate);
11539
11540 verifyFormat("void (*foopt)(int) = &func;");
11541
11542 FormatStyle DerivePointerAlignment = getLLVMStyle();
11543 DerivePointerAlignment.DerivePointerAlignment = true;
11544 // There's always a space between the function and its trailing qualifiers.
11545 // This isn't evidence for PAS_Right (or for PAS_Left).
11546 std::string Prefix = "void a() &;\n"
11547 "void b() &;\n";
11548 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11549 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11550 // Same if the function is an overloaded operator, and with &&.
11551 Prefix = "void operator()() &&;\n"
11552 "void operator()() &&;\n";
11553 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11554 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11555 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11556 Prefix = "void a() const &;\n"
11557 "void b() const &;\n";
11558 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11559 }
11560
11561 TEST_F(FormatTest, PointerAlignmentFallback) {
11562 FormatStyle Style = getLLVMStyle();
11563 Style.DerivePointerAlignment = true;
11564
11565 const StringRef Code("int* p;\n"
11566 "int *q;\n"
11567 "int * r;");
11568
11569 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11570 verifyFormat("int *p;\n"
11571 "int *q;\n"
11572 "int *r;",
11573 Code, Style);
11574
11575 Style.PointerAlignment = FormatStyle::PAS_Left;
11576 verifyFormat("int* p;\n"
11577 "int* q;\n"
11578 "int* r;",
11579 Code, Style);
11580
11581 Style.PointerAlignment = FormatStyle::PAS_Middle;
11582 verifyFormat("int * p;\n"
11583 "int * q;\n"
11584 "int * r;",
11585 Code, Style);
11586 }
11587
11588 TEST_F(FormatTest, UnderstandsNewAndDelete) {
11589 verifyFormat("void f() {\n"
11590 " A *a = new A;\n"
11591 " A *a = new (placement) A;\n"
11592 " delete a;\n"
11593 " delete (A *)a;\n"
11594 "}");
11595 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11596 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11597 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11598 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11599 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11600 verifyFormat("delete[] h->p;");
11601 verifyFormat("delete[] (void *)p;");
11602
11603 verifyFormat("void operator delete(void *foo) ATTRIB;");
11604 verifyFormat("void operator new(void *foo) ATTRIB;");
11605 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11606 verifyFormat("void operator delete(void *ptr) noexcept;");
11607
11608 verifyFormat("void new(link p);\n"
11609 "void delete(link p);",
11610 "void new (link p);\n"
11611 "void delete (link p);");
11612
11613 verifyFormat("{\n"
11614 " p->new();\n"
11615 "}\n"
11616 "{\n"
11617 " p->delete();\n"
11618 "}",
11619 "{\n"
11620 " p->new ();\n"
11621 "}\n"
11622 "{\n"
11623 " p->delete ();\n"
11624 "}");
11625
11626 FormatStyle AfterPlacementOperator = getLLVMStyle();
11627 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11628 EXPECT_TRUE(
11629 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
11630 verifyFormat("new (buf) int;", AfterPlacementOperator);
11631 verifyFormat("struct A {\n"
11632 " int *a;\n"
11633 " A(int *p) : a(new (p) int) {\n"
11634 " new (p) int;\n"
11635 " int *b = new (p) int;\n"
11636 " int *c = new (p) int(3);\n"
11637 " delete (b);\n"
11638 " }\n"
11639 "};",
11640 AfterPlacementOperator);
11641 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11642
11643 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11644 false;
11645 verifyFormat("new(buf) int;", AfterPlacementOperator);
11646 verifyFormat("struct A {\n"
11647 " int *a;\n"
11648 " A(int *p) : a(new(p) int) {\n"
11649 " new(p) int;\n"
11650 " int *b = new(p) int;\n"
11651 " int *c = new(p) int(3);\n"
11652 " delete(b);\n"
11653 " }\n"
11654 "};",
11655 AfterPlacementOperator);
11656 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11657 }
11658
11659 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11660 verifyFormat("int *f(int *a) {}");
11661 verifyFormat("int main(int argc, char **argv) {}");
11662 verifyFormat("Test::Test(int b) : a(b * b) {}");
11663 verifyIndependentOfContext("f(a, *a);");
11664 verifyFormat("void g() { f(*a); }");
11665 verifyIndependentOfContext("int a = b * 10;");
11666 verifyIndependentOfContext("int a = 10 * b;");
11667 verifyIndependentOfContext("int a = b * c;");
11668 verifyIndependentOfContext("int a += b * c;");
11669 verifyIndependentOfContext("int a -= b * c;");
11670 verifyIndependentOfContext("int a *= b * c;");
11671 verifyIndependentOfContext("int a /= b * c;");
11672 verifyIndependentOfContext("int a = *b;");
11673 verifyIndependentOfContext("int a = *b * c;");
11674 verifyIndependentOfContext("int a = b * *c;");
11675 verifyIndependentOfContext("int a = b * (10);");
11676 verifyIndependentOfContext("S << b * (10);");
11677 verifyIndependentOfContext("return 10 * b;");
11678 verifyIndependentOfContext("return *b * *c;");
11679 verifyIndependentOfContext("return a & ~b;");
11680 verifyIndependentOfContext("f(b ? *c : *d);");
11681 verifyIndependentOfContext("int a = b ? *c : *d;");
11682 verifyIndependentOfContext("*b = a;");
11683 verifyIndependentOfContext("a * ~b;");
11684 verifyIndependentOfContext("a * !b;");
11685 verifyIndependentOfContext("a * +b;");
11686 verifyIndependentOfContext("a * -b;");
11687 verifyIndependentOfContext("a * ++b;");
11688 verifyIndependentOfContext("a * --b;");
11689 verifyIndependentOfContext("a[4] * b;");
11690 verifyIndependentOfContext("a[a * a] = 1;");
11691 verifyIndependentOfContext("f() * b;");
11692 verifyIndependentOfContext("a * [self dostuff];");
11693 verifyIndependentOfContext("int x = a * (a + b);");
11694 verifyIndependentOfContext("(a *)(a + b);");
11695 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11696 verifyIndependentOfContext("int *pa = (int *)&a;");
11697 verifyIndependentOfContext("return sizeof(int **);");
11698 verifyIndependentOfContext("return sizeof(int ******);");
11699 verifyIndependentOfContext("return (int **&)a;");
11700 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11701 verifyFormat("void f(Type (*parameter)[10]) {}");
11702 verifyFormat("void f(Type (&parameter)[10]) {}");
11703 verifyGoogleFormat("return sizeof(int**);");
11704 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11705 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11706 verifyFormat("auto a = [](int **&, int ***) {};");
11707 verifyFormat("auto PointerBinding = [](const char *S) {};");
11708 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11709 verifyFormat("[](const decltype(*a) &value) {}");
11710 verifyFormat("[](const typeof(*a) &value) {}");
11711 verifyFormat("[](const _Atomic(a *) &value) {}");
11712 verifyFormat("[](const __underlying_type(a) &value) {}");
11713 verifyFormat("decltype(a * b) F();");
11714 verifyFormat("typeof(a * b) F();");
11715 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11716 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11717 verifyIndependentOfContext("typedef void (*f)(int *a);");
11718 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11719 verifyIndependentOfContext("int i{a * b};");
11720 verifyIndependentOfContext("aaa && aaa->f();");
11721 verifyIndependentOfContext("int x = ~*p;");
11722 verifyFormat("Constructor() : a(a), area(width * height) {}");
11723 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11724 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11725 verifyFormat("void f() { f(a, c * d); }");
11726 verifyFormat("void f() { f(new a(), c * d); }");
11727 verifyFormat("void f(const MyOverride &override);");
11728 verifyFormat("void f(const MyFinal &final);");
11729 verifyIndependentOfContext("bool a = f() && override.f();");
11730 verifyIndependentOfContext("bool a = f() && final.f();");
11731
11732 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11733
11734 verifyIndependentOfContext("A<int *> a;");
11735 verifyIndependentOfContext("A<int **> a;");
11736 verifyIndependentOfContext("A<int *, int *> a;");
11737 verifyIndependentOfContext("A<int *[]> a;");
11738 verifyIndependentOfContext(
11739 "const char *const p = reinterpret_cast<const char *const>(q);");
11740 verifyIndependentOfContext("A<int **, int **> a;");
11741 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11742 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11743 verifyFormat("for (; a && b;) {\n}");
11744 verifyFormat("bool foo = true && [] { return false; }();");
11745
11746 verifyFormat(
11747 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11749
11750 verifyGoogleFormat("int const* a = &b;");
11751 verifyGoogleFormat("**outparam = 1;");
11752 verifyGoogleFormat("*outparam = a * b;");
11753 verifyGoogleFormat("int main(int argc, char** argv) {}");
11754 verifyGoogleFormat("A<int*> a;");
11755 verifyGoogleFormat("A<int**> a;");
11756 verifyGoogleFormat("A<int*, int*> a;");
11757 verifyGoogleFormat("A<int**, int**> a;");
11758 verifyGoogleFormat("f(b ? *c : *d);");
11759 verifyGoogleFormat("int a = b ? *c : *d;");
11760 verifyGoogleFormat("Type* t = **x;");
11761 verifyGoogleFormat("Type* t = *++*x;");
11762 verifyGoogleFormat("*++*x;");
11763 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11764 verifyGoogleFormat("Type* t = x++ * y;");
11765 verifyGoogleFormat(
11766 "const char* const p = reinterpret_cast<const char* const>(q);");
11767 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11768 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11769 verifyGoogleFormat("template <typename T>\n"
11770 "void f(int i = 0, SomeType** temps = NULL);");
11771
11772 FormatStyle Left = getLLVMStyle();
11773 Left.PointerAlignment = FormatStyle::PAS_Left;
11774 verifyFormat("x = *a(x) = *a(y);", Left);
11775 verifyFormat("for (;; *a = b) {\n}", Left);
11776 verifyFormat("return *this += 1;", Left);
11777 verifyFormat("throw *x;", Left);
11778 verifyFormat("delete *x;", Left);
11779 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
11780 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
11781 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
11782 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
11783 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
11784 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
11785 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
11786 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
11787 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
11788
11789 verifyIndependentOfContext("a = *(x + y);");
11790 verifyIndependentOfContext("a = &(x + y);");
11791 verifyIndependentOfContext("*(x + y).call();");
11792 verifyIndependentOfContext("&(x + y)->call();");
11793 verifyFormat("void f() { &(*I).first; }");
11794
11795 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11796 verifyFormat("f(* /* confusing comment */ foo);");
11797 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11798 verifyFormat("void foo(int * // this is the first paramters\n"
11799 " ,\n"
11800 " int second);");
11801 verifyFormat("double term = a * // first\n"
11802 " b;");
11803 verifyFormat(
11804 "int *MyValues = {\n"
11805 " *A, // Operator detection might be confused by the '{'\n"
11806 " *BB // Operator detection might be confused by previous comment\n"
11807 "};");
11808
11809 verifyIndependentOfContext("if (int *a = &b)");
11810 verifyIndependentOfContext("if (int &a = *b)");
11811 verifyIndependentOfContext("if (a & b[i])");
11812 verifyIndependentOfContext("if constexpr (a & b[i])");
11813 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11814 verifyIndependentOfContext("if (a * (b * c))");
11815 verifyIndependentOfContext("if constexpr (a * (b * c))");
11816 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11817 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11818 verifyIndependentOfContext("if (*b[i])");
11819 verifyIndependentOfContext("if (int *a = (&b))");
11820 verifyIndependentOfContext("while (int *a = &b)");
11821 verifyIndependentOfContext("while (a * (b * c))");
11822 verifyIndependentOfContext("size = sizeof *a;");
11823 verifyIndependentOfContext("if (a && (b = c))");
11824 verifyFormat("void f() {\n"
11825 " for (const int &v : Values) {\n"
11826 " }\n"
11827 "}");
11828 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11829 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11830 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11831
11832 verifyFormat("#define A (!a * b)");
11833 verifyFormat("#define MACRO \\\n"
11834 " int *i = a * b; \\\n"
11835 " void f(a *b);",
11836 getLLVMStyleWithColumns(19));
11837
11838 verifyIndependentOfContext("A = new SomeType *[Length];");
11839 verifyIndependentOfContext("A = new SomeType *[Length]();");
11840 verifyIndependentOfContext("T **t = new T *;");
11841 verifyIndependentOfContext("T **t = new T *();");
11842 verifyGoogleFormat("A = new SomeType*[Length]();");
11843 verifyGoogleFormat("A = new SomeType*[Length];");
11844 verifyGoogleFormat("T** t = new T*;");
11845 verifyGoogleFormat("T** t = new T*();");
11846
11847 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11848 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11849 verifyFormat("template <bool a, bool b> "
11850 "typename t::if<x && y>::type f() {}");
11851 verifyFormat("template <int *y> f() {}");
11852 verifyFormat("vector<int *> v;");
11853 verifyFormat("vector<int *const> v;");
11854 verifyFormat("vector<int *const **const *> v;");
11855 verifyFormat("vector<int *volatile> v;");
11856 verifyFormat("vector<a *_Nonnull> v;");
11857 verifyFormat("vector<a *_Nullable> v;");
11858 verifyFormat("vector<a *_Null_unspecified> v;");
11859 verifyFormat("vector<a *__ptr32> v;");
11860 verifyFormat("vector<a *__ptr64> v;");
11861 verifyFormat("vector<a *__capability> v;");
11862 FormatStyle TypeMacros = getLLVMStyle();
11863 TypeMacros.TypenameMacros = {"LIST"};
11864 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
11865 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
11866 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
11867 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
11868 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
11869
11870 FormatStyle CustomQualifier = getLLVMStyle();
11871 // Add identifiers that should not be parsed as a qualifier by default.
11872 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11873 CustomQualifier.AttributeMacros.push_back("_My_qualifier");
11874 CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
11875 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11876 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
11877 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11878 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
11879 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11880 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
11881 verifyFormat("vector<a * _NotAQualifier> v;");
11882 verifyFormat("vector<a * __not_a_qualifier> v;");
11883 verifyFormat("vector<a * b> v;");
11884 verifyFormat("foo<b && false>();");
11885 verifyFormat("foo<b & 1>();");
11886 verifyFormat("foo<b & (1)>();");
11887 verifyFormat("foo<b & (~0)>();");
11888 verifyFormat("foo<b & (true)>();");
11889 verifyFormat("foo<b & ((1))>();");
11890 verifyFormat("foo<b & (/*comment*/ 1)>();");
11891 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
11892 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
11893 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
11894 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
11895 verifyFormat(
11896 "template <class T, class = typename std::enable_if<\n"
11897 " std::is_integral<T>::value &&\n"
11898 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
11899 "void F();",
11900 getLLVMStyleWithColumns(70));
11901 verifyFormat("template <class T,\n"
11902 " class = typename std::enable_if<\n"
11903 " std::is_integral<T>::value &&\n"
11904 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
11905 " class U>\n"
11906 "void F();",
11907 getLLVMStyleWithColumns(70));
11908 verifyFormat(
11909 "template <class T,\n"
11910 " class = typename ::std::enable_if<\n"
11911 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
11912 "void F();",
11913 getGoogleStyleWithColumns(68));
11914
11915 FormatStyle Style = getLLVMStyle();
11916 Style.PointerAlignment = FormatStyle::PAS_Left;
11917 verifyFormat("struct {\n"
11918 "}* ptr;",
11919 Style);
11920 verifyFormat("union {\n"
11921 "}* ptr;",
11922 Style);
11923 verifyFormat("class {\n"
11924 "}* ptr;",
11925 Style);
11926 // Don't confuse a multiplication after a brace-initialized expression with
11927 // a class pointer.
11928 verifyFormat("int i = int{42} * 34;", Style);
11929 verifyFormat("struct {\n"
11930 "}&& ptr = {};",
11931 Style);
11932 verifyFormat("union {\n"
11933 "}&& ptr = {};",
11934 Style);
11935 verifyFormat("class {\n"
11936 "}&& ptr = {};",
11937 Style);
11938 verifyFormat("bool b = 3 == int{3} && true;");
11939
11940 Style.PointerAlignment = FormatStyle::PAS_Middle;
11941 verifyFormat("struct {\n"
11942 "} * ptr;",
11943 Style);
11944 verifyFormat("union {\n"
11945 "} * ptr;",
11946 Style);
11947 verifyFormat("class {\n"
11948 "} * ptr;",
11949 Style);
11950 verifyFormat("struct {\n"
11951 "} && ptr = {};",
11952 Style);
11953 verifyFormat("union {\n"
11954 "} && ptr = {};",
11955 Style);
11956 verifyFormat("class {\n"
11957 "} && ptr = {};",
11958 Style);
11959
11960 Style.PointerAlignment = FormatStyle::PAS_Right;
11961 verifyFormat("struct {\n"
11962 "} *ptr;",
11963 Style);
11964 verifyFormat("union {\n"
11965 "} *ptr;",
11966 Style);
11967 verifyFormat("class {\n"
11968 "} *ptr;",
11969 Style);
11970 verifyFormat("struct {\n"
11971 "} &&ptr = {};",
11972 Style);
11973 verifyFormat("union {\n"
11974 "} &&ptr = {};",
11975 Style);
11976 verifyFormat("class {\n"
11977 "} &&ptr = {};",
11978 Style);
11979
11980 Style.PointerAlignment = FormatStyle::PAS_Left;
11981 verifyFormat("delete[] *ptr;", Style);
11982 verifyFormat("delete[] **ptr;", Style);
11983 verifyFormat("delete[] *(ptr);", Style);
11984
11985 verifyIndependentOfContext("MACRO(int *i);");
11986 verifyIndependentOfContext("MACRO(auto *a);");
11987 verifyIndependentOfContext("MACRO(const A *a);");
11988 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
11989 verifyIndependentOfContext("MACRO(decltype(A) *a);");
11990 verifyIndependentOfContext("MACRO(typeof(A) *a);");
11991 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
11992 verifyIndependentOfContext("MACRO(A *const a);");
11993 verifyIndependentOfContext("MACRO(A *restrict a);");
11994 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
11995 verifyIndependentOfContext("MACRO(A *__restrict a);");
11996 verifyIndependentOfContext("MACRO(A *volatile a);");
11997 verifyIndependentOfContext("MACRO(A *__volatile a);");
11998 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
11999 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12000 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12001 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12002 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12003 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12004 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12005 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12006 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12007 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12008 verifyIndependentOfContext("MACRO(A *__capability);");
12009 verifyIndependentOfContext("MACRO(A &__capability);");
12010 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12011 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12012 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12013 // a type declaration:
12014 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
12015 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
12016 // Also check that TypenameMacros prevents parsing it as multiplication:
12017 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12018 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
12019
12020 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12021 verifyFormat("void f() { f(float{1}, a * a); }");
12022 verifyFormat("void f() { f(float(1), a * a); }");
12023
12024 verifyFormat("f((void (*)(int))g);");
12025 verifyFormat("f((void (&)(int))g);");
12026 verifyFormat("f((void (^)(int))g);");
12027
12028 // FIXME: Is there a way to make this work?
12029 // verifyIndependentOfContext("MACRO(A *a);");
12030 verifyFormat("MACRO(A &B);");
12031 verifyFormat("MACRO(A *B);");
12032 verifyFormat("void f() { MACRO(A * B); }");
12033 verifyFormat("void f() { MACRO(A & B); }");
12034
12035 // This lambda was mis-formatted after D88956 (treating it as a binop):
12036 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12037 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12038 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12039 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12040
12041 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12042 verifyFormat("return options != nullptr && operator==(*options);");
12043
12044 verifyFormat("#define OP(x) \\\n"
12045 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12046 " return s << a.DebugString(); \\\n"
12047 " }",
12048 "#define OP(x) \\\n"
12049 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12050 " return s << a.DebugString(); \\\n"
12051 " }",
12052 getLLVMStyleWithColumns(50));
12053
12054 verifyFormat("#define FOO \\\n"
12055 " void foo() { \\\n"
12056 " operator+(a * b); \\\n"
12057 " }",
12058 getLLVMStyleWithColumns(25));
12059
12060 // FIXME: We cannot handle this case yet; we might be able to figure out that
12061 // foo<x> d > v; doesn't make sense.
12062 verifyFormat("foo<a<b && c> d> v;");
12063
12064 FormatStyle PointerMiddle = getLLVMStyle();
12065 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12066 verifyFormat("delete *x;", PointerMiddle);
12067 verifyFormat("int * x;", PointerMiddle);
12068 verifyFormat("int *[] x;", PointerMiddle);
12069 verifyFormat("template <int * y> f() {}", PointerMiddle);
12070 verifyFormat("int * f(int * a) {}", PointerMiddle);
12071 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12072 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12073 verifyFormat("A<int *> a;", PointerMiddle);
12074 verifyFormat("A<int **> a;", PointerMiddle);
12075 verifyFormat("A<int *, int *> a;", PointerMiddle);
12076 verifyFormat("A<int *[]> a;", PointerMiddle);
12077 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12078 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12079 verifyFormat("T ** t = new T *;", PointerMiddle);
12080
12081 // Member function reference qualifiers aren't binary operators.
12082 verifyFormat("string // break\n"
12083 "operator()() & {}");
12084 verifyFormat("string // break\n"
12085 "operator()() && {}");
12086 verifyGoogleFormat("template <typename T>\n"
12087 "auto x() & -> int {}");
12088
12089 // Should be binary operators when used as an argument expression (overloaded
12090 // operator invoked as a member function).
12091 verifyFormat("void f() { a.operator()(a * a); }");
12092 verifyFormat("void f() { a->operator()(a & a); }");
12093 verifyFormat("void f() { a.operator()(*a & *a); }");
12094 verifyFormat("void f() { a->operator()(*a * *a); }");
12095
12096 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12097 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12098
12099 verifyFormat("val1 & val2;");
12100 verifyFormat("val1 & val2 & val3;");
12101 verifyFormat("class c {\n"
12102 " void func(type &a) { a & member; }\n"
12103 " anotherType &member;\n"
12104 "}");
12105 }
12106
12107 TEST_F(FormatTest, UnderstandsAttributes) {
12108 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12109 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12110 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12111 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12112 FormatStyle AfterType = getLLVMStyle();
12113 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12114 verifyFormat("__attribute__((nodebug)) void\n"
12115 "foo() {}",
12116 AfterType);
12117 verifyFormat("__unused void\n"
12118 "foo() {}",
12119 AfterType);
12120
12121 FormatStyle CustomAttrs = getLLVMStyle();
12122 CustomAttrs.AttributeMacros.push_back("__unused");
12123 CustomAttrs.AttributeMacros.push_back("__attr1");
12124 CustomAttrs.AttributeMacros.push_back("__attr2");
12125 CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
12126 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12127 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12128 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12129 // Check that it is parsed as a multiplication without AttributeMacros and
12130 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12131 verifyFormat("vector<SomeType * __attr1> v;");
12132 verifyFormat("vector<SomeType __attr1 *> v;");
12133 verifyFormat("vector<SomeType __attr1 *const> v;");
12134 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12135 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12136 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12137 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12138 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12139 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12140 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12141 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12142 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12143 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12144 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12145
12146 // Check that these are not parsed as function declarations:
12147 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12148 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12149 verifyFormat("SomeType s(InitValue);", CustomAttrs);
12150 verifyFormat("SomeType s{InitValue};", CustomAttrs);
12151 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12152 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12153 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12154 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12155 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12156 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12157 }
12158
12159 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12160 // Check that qualifiers on pointers don't break parsing of casts.
12161 verifyFormat("x = (foo *const)*v;");
12162 verifyFormat("x = (foo *volatile)*v;");
12163 verifyFormat("x = (foo *restrict)*v;");
12164 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12165 verifyFormat("x = (foo *_Nonnull)*v;");
12166 verifyFormat("x = (foo *_Nullable)*v;");
12167 verifyFormat("x = (foo *_Null_unspecified)*v;");
12168 verifyFormat("x = (foo *_Nonnull)*v;");
12169 verifyFormat("x = (foo *[[clang::attr]])*v;");
12170 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12171 verifyFormat("x = (foo *__ptr32)*v;");
12172 verifyFormat("x = (foo *__ptr64)*v;");
12173 verifyFormat("x = (foo *__capability)*v;");
12174
12175 // Check that we handle multiple trailing qualifiers and skip them all to
12176 // determine that the expression is a cast to a pointer type.
12177 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
12178 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
12179 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12180 StringRef AllQualifiers =
12181 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12182 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12183 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12184 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12185
12186 // Also check that address-of is not parsed as a binary bitwise-and:
12187 verifyFormat("x = (foo *const)&v;");
12188 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12189 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12190
12191 // Check custom qualifiers:
12192 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
12193 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12194 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12195 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12196 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12197 CustomQualifier);
12198 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12199 CustomQualifier);
12200
12201 // Check that unknown identifiers result in binary operator parsing:
12202 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12203 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12204 }
12205
12206 TEST_F(FormatTest, UnderstandsSquareAttributes) {
12207 verifyFormat("SomeType s [[unused]] (InitValue);");
12208 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12209 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12210 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12211 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12212 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12213 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12214 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12215 verifyFormat("[[nodiscard]] bool f() { return false; }");
12216 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12217 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12218 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12219 verifyFormat("[[nodiscard]] ::qualified_type f();");
12220
12221 // Make sure we do not mistake attributes for array subscripts.
12222 verifyFormat("int a() {}\n"
12223 "[[unused]] int b() {}");
12224 verifyFormat("NSArray *arr;\n"
12225 "arr[[Foo() bar]];");
12226
12227 // On the other hand, we still need to correctly find array subscripts.
12228 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12229
12230 // Make sure that we do not mistake Objective-C method inside array literals
12231 // as attributes, even if those method names are also keywords.
12232 verifyFormat("@[ [foo bar] ];");
12233 verifyFormat("@[ [NSArray class] ];");
12234 verifyFormat("@[ [foo enum] ];");
12235
12236 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12237
12238 // Make sure we do not parse attributes as lambda introducers.
12239 FormatStyle MultiLineFunctions = getLLVMStyle();
12240 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12241 verifyFormat("[[unused]] int b() {\n"
12242 " return 42;\n"
12243 "}",
12244 MultiLineFunctions);
12245 }
12246
12247 TEST_F(FormatTest, AttributeClass) {
12248 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
12249 verifyFormat("class S {\n"
12250 " S(S&&) = default;\n"
12251 "};",
12252 Style);
12253 verifyFormat("class [[nodiscard]] S {\n"
12254 " S(S&&) = default;\n"
12255 "};",
12256 Style);
12257 verifyFormat("class __attribute((maybeunused)) S {\n"
12258 " S(S&&) = default;\n"
12259 "};",
12260 Style);
12261 verifyFormat("struct S {\n"
12262 " S(S&&) = default;\n"
12263 "};",
12264 Style);
12265 verifyFormat("struct [[nodiscard]] S {\n"
12266 " S(S&&) = default;\n"
12267 "};",
12268 Style);
12269 }
12270
12271 TEST_F(FormatTest, AttributesAfterMacro) {
12272 FormatStyle Style = getLLVMStyle();
12273 verifyFormat("MACRO;\n"
12274 "__attribute__((maybe_unused)) int foo() {\n"
12275 " //...\n"
12276 "}");
12277
12278 verifyFormat("MACRO;\n"
12279 "[[nodiscard]] int foo() {\n"
12280 " //...\n"
12281 "}");
12282
12283 verifyNoChange("MACRO\n\n"
12284 "__attribute__((maybe_unused)) int foo() {\n"
12285 " //...\n"
12286 "}");
12287
12288 verifyNoChange("MACRO\n\n"
12289 "[[nodiscard]] int foo() {\n"
12290 " //...\n"
12291 "}");
12292 }
12293
12294 TEST_F(FormatTest, AttributePenaltyBreaking) {
12295 FormatStyle Style = getLLVMStyle();
12296 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12297 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12298 Style);
12299 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12300 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12301 Style);
12302 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12303 "shared_ptr<ALongTypeName> &C d) {\n}",
12304 Style);
12305 }
12306
12307 TEST_F(FormatTest, UnderstandsEllipsis) {
12308 FormatStyle Style = getLLVMStyle();
12309 verifyFormat("int printf(const char *fmt, ...);");
12310 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12311 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12312
12313 verifyFormat("template <int *...PP> a;", Style);
12314
12315 Style.PointerAlignment = FormatStyle::PAS_Left;
12316 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12317
12318 verifyFormat("template <int*... PP> a;", Style);
12319
12320 Style.PointerAlignment = FormatStyle::PAS_Middle;
12321 verifyFormat("template <int *... PP> a;", Style);
12322 }
12323
12324 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12325 verifyFormat("int *a;\n"
12326 "int *a;\n"
12327 "int *a;",
12328 "int *a;\n"
12329 "int* a;\n"
12330 "int *a;",
12331 getGoogleStyle());
12332 verifyFormat("int* a;\n"
12333 "int* a;\n"
12334 "int* a;",
12335 "int* a;\n"
12336 "int* a;\n"
12337 "int *a;",
12338 getGoogleStyle());
12339 verifyFormat("int *a;\n"
12340 "int *a;\n"
12341 "int *a;",
12342 "int *a;\n"
12343 "int * a;\n"
12344 "int * a;",
12345 getGoogleStyle());
12346 verifyFormat("auto x = [] {\n"
12347 " int *a;\n"
12348 " int *a;\n"
12349 " int *a;\n"
12350 "};",
12351 "auto x=[]{int *a;\n"
12352 "int * a;\n"
12353 "int * a;};",
12354 getGoogleStyle());
12355 }
12356
12357 TEST_F(FormatTest, UnderstandsRvalueReferences) {
12358 verifyFormat("int f(int &&a) {}");
12359 verifyFormat("int f(int a, char &&b) {}");
12360 verifyFormat("void f() { int &&a = b; }");
12361 verifyGoogleFormat("int f(int a, char&& b) {}");
12362 verifyGoogleFormat("void f() { int&& a = b; }");
12363
12364 verifyIndependentOfContext("A<int &&> a;");
12365 verifyIndependentOfContext("A<int &&, int &&> a;");
12366 verifyGoogleFormat("A<int&&> a;");
12367 verifyGoogleFormat("A<int&&, int&&> a;");
12368
12369 // Not rvalue references:
12370 verifyFormat("template <bool B, bool C> class A {\n"
12371 " static_assert(B && C, \"Something is wrong\");\n"
12372 "};");
12373 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12374 verifyFormat("template <typename T> struct S {\n"
12375 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12376 "};");
12377 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12378 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12379 verifyFormat("#define A(a, b) (a && b)");
12380 }
12381
12382 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
12383 verifyFormat("void f() {\n"
12384 " x[aaaaaaaaa -\n"
12385 " b] = 23;\n"
12386 "}",
12387 getLLVMStyleWithColumns(15));
12388 }
12389
12390 TEST_F(FormatTest, FormatsCasts) {
12391 verifyFormat("Type *A = static_cast<Type *>(P);");
12392 verifyFormat("static_cast<Type *>(P);");
12393 verifyFormat("static_cast<Type &>(Fun)(Args);");
12394 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12395 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12396 // Check that static_cast<...>(...) does not require the next token to be on
12397 // the same line.
12398 verifyFormat("some_loooong_output << something_something__ << "
12399 "static_cast<const void *>(R)\n"
12400 " << something;");
12401 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12402 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12403 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12404 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12405 verifyFormat("Type *A = (Type *)P;");
12406 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12407 verifyFormat("int a = (int)(2.0f);");
12408 verifyFormat("int a = (int)2.0f;");
12409 verifyFormat("x[(int32)y];");
12410 verifyFormat("x = (int32)y;");
12411 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12412 verifyFormat("int a = (int)*b;");
12413 verifyFormat("int a = (int)2.0f;");
12414 verifyFormat("int a = (int)~0;");
12415 verifyFormat("int a = (int)++a;");
12416 verifyFormat("int a = (int)sizeof(int);");
12417 verifyFormat("int a = (int)+2;");
12418 verifyFormat("my_int a = (my_int)2.0f;");
12419 verifyFormat("my_int a = (my_int)sizeof(int);");
12420 verifyFormat("return (my_int)aaa;");
12421 verifyFormat("throw (my_int)aaa;");
12422 verifyFormat("#define x ((int)-1)");
12423 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12424 verifyFormat("#define p(q) ((int *)&q)");
12425 verifyFormat("fn(a)(b) + 1;");
12426
12427 verifyFormat("void f() { my_int a = (my_int)*b; }");
12428 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12429 verifyFormat("my_int a = (my_int)~0;");
12430 verifyFormat("my_int a = (my_int)++a;");
12431 verifyFormat("my_int a = (my_int)-2;");
12432 verifyFormat("my_int a = (my_int)1;");
12433 verifyFormat("my_int a = (my_int *)1;");
12434 verifyFormat("my_int a = (const my_int)-1;");
12435 verifyFormat("my_int a = (const my_int *)-1;");
12436 verifyFormat("my_int a = (my_int)(my_int)-1;");
12437 verifyFormat("my_int a = (ns::my_int)-2;");
12438 verifyFormat("case (my_int)ONE:");
12439 verifyFormat("auto x = (X)this;");
12440 // Casts in Obj-C style calls used to not be recognized as such.
12441 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12442
12443 // FIXME: single value wrapped with paren will be treated as cast.
12444 verifyFormat("void f(int i = (kValue)*kMask) {}");
12445
12446 verifyFormat("{\n"
12447 " (void)F;\n"
12448 "}");
12449
12450 // Don't break after a cast's
12451 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12452 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12453 " bbbbbbbbbbbbbbbbbbbbbb);");
12454
12455 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12456 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12457 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12458 verifyFormat("bool *y = (bool *)(void *)(x);");
12459 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12460 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12461 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12462 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12463
12464 // These are not casts.
12465 verifyFormat("void f(int *) {}");
12466 verifyFormat("f(foo)->b;");
12467 verifyFormat("f(foo).b;");
12468 verifyFormat("f(foo)(b);");
12469 verifyFormat("f(foo)[b];");
12470 verifyFormat("[](foo) { return 4; }(bar);");
12471 verifyFormat("(*funptr)(foo)[4];");
12472 verifyFormat("funptrs[4](foo)[4];");
12473 verifyFormat("void f(int *);");
12474 verifyFormat("void f(int *) = 0;");
12475 verifyFormat("void f(SmallVector<int>) {}");
12476 verifyFormat("void f(SmallVector<int>);");
12477 verifyFormat("void f(SmallVector<int>) = 0;");
12478 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12479 verifyFormat("int a = sizeof(int) * b;");
12480 verifyGoogleFormat("int a = alignof(int) * b;");
12481 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12482 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12483 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12484
12485 // These are not casts, but at some point were confused with casts.
12486 verifyFormat("virtual void foo(int *) override;");
12487 verifyFormat("virtual void foo(char &) const;");
12488 verifyFormat("virtual void foo(int *a, char *) const;");
12489 verifyFormat("int a = sizeof(int *) + b;");
12490 verifyGoogleFormat("int a = alignof(int *) + b;");
12491 verifyFormat("bool b = f(g<int>) && c;");
12492 verifyFormat("typedef void (*f)(int i) func;");
12493 verifyFormat("void operator++(int) noexcept;");
12494 verifyFormat("void operator++(int &) noexcept;");
12495 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12496 "&) noexcept;");
12497 verifyFormat(
12498 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12499 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12500 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12501 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12502 verifyFormat("void operator delete(foo &) noexcept;");
12503 verifyFormat("void operator delete(foo) noexcept;");
12504 verifyFormat("void operator delete(int) noexcept;");
12505 verifyFormat("void operator delete(int &) noexcept;");
12506 verifyFormat("void operator delete(int &) volatile noexcept;");
12507 verifyFormat("void operator delete(int &) const");
12508 verifyFormat("void operator delete(int &) = default");
12509 verifyFormat("void operator delete(int &) = delete");
12510 verifyFormat("void operator delete(int &) [[noreturn]]");
12511 verifyFormat("void operator delete(int &) throw();");
12512 verifyFormat("void operator delete(int &) throw(int);");
12513 verifyFormat("auto operator delete(int &) -> int;");
12514 verifyFormat("auto operator delete(int &) override");
12515 verifyFormat("auto operator delete(int &) final");
12516
12517 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12518 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12519 // FIXME: The indentation here is not ideal.
12520 verifyFormat(
12521 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12522 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12523 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12524 }
12525
12526 TEST_F(FormatTest, FormatsFunctionTypes) {
12527 verifyFormat("A<bool()> a;");
12528 verifyFormat("A<SomeType()> a;");
12529 verifyFormat("A<void (*)(int, std::string)> a;");
12530 verifyFormat("A<void *(int)>;");
12531 verifyFormat("void *(*a)(int *, SomeType *);");
12532 verifyFormat("int (*func)(void *);");
12533 verifyFormat("void f() { int (*func)(void *); }");
12534 verifyFormat("template <class CallbackClass>\n"
12535 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12536
12537 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12538 verifyGoogleFormat("void* (*a)(int);");
12539 verifyGoogleFormat(
12540 "template <class CallbackClass>\n"
12541 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12542
12543 // Other constructs can look somewhat like function types:
12544 verifyFormat("A<sizeof(*x)> a;");
12545 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12546 verifyFormat("some_var = function(*some_pointer_var)[0];");
12547 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12548 verifyFormat("int x = f(&h)();");
12549 verifyFormat("returnsFunction(&param1, &param2)(param);");
12550 verifyFormat("std::function<\n"
12551 " LooooooooooongTemplatedType<\n"
12552 " SomeType>*(\n"
12553 " LooooooooooooooooongType type)>\n"
12554 " function;",
12555 getGoogleStyleWithColumns(40));
12556 }
12557
12558 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12559 verifyFormat("A (*foo_)[6];");
12560 verifyFormat("vector<int> (*foo_)[6];");
12561 }
12562
12563 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12564 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12565 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12566 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12567 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12568 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12569 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12570
12571 // Different ways of ()-initializiation.
12572 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12573 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12574 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12575 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12576 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12577 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12578 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12579 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12580
12581 // Lambdas should not confuse the variable declaration heuristic.
12582 verifyFormat("LooooooooooooooooongType\n"
12583 " variable(nullptr, [](A *a) {});",
12584 getLLVMStyleWithColumns(40));
12585 }
12586
12587 TEST_F(FormatTest, BreaksLongDeclarations) {
12588 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12589 " AnotherNameForTheLongType;");
12590 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12592 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12593 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12594 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12595 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12596 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12597 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12598 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12599 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12600 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12601 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12602 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12603 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12604 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12605 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12606 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12607 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12608 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12609 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12610 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12611 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12612 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12613 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12614 FormatStyle Indented = getLLVMStyle();
12615 Indented.IndentWrappedFunctionNames = true;
12616 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12617 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12618 Indented);
12619 verifyFormat(
12620 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12621 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12622 Indented);
12623 verifyFormat(
12624 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12625 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12626 Indented);
12627 verifyFormat(
12628 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12629 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12630 Indented);
12631
12632 // FIXME: Without the comment, this breaks after "(".
12633 verifyGoogleFormat(
12634 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12635 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12636
12637 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12638 " int LoooooooooooooooooooongParam2) {}");
12639 verifyFormat(
12640 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12641 " SourceLocation L, IdentifierIn *II,\n"
12642 " Type *T) {}");
12643 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12644 "ReallyReaaallyLongFunctionName(\n"
12645 " const std::string &SomeParameter,\n"
12646 " const SomeType<string, SomeOtherTemplateParameter>\n"
12647 " &ReallyReallyLongParameterName,\n"
12648 " const SomeType<string, SomeOtherTemplateParameter>\n"
12649 " &AnotherLongParameterName) {}");
12650 verifyFormat("template <typename A>\n"
12651 "SomeLoooooooooooooooooooooongType<\n"
12652 " typename some_namespace::SomeOtherType<A>::Type>\n"
12653 "Function() {}");
12654
12655 verifyGoogleFormat(
12656 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12657 " aaaaaaaaaaaaaaaaaaaaaaa;");
12658 verifyGoogleFormat(
12659 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12660 " SourceLocation L) {}");
12661 verifyGoogleFormat(
12662 "some_namespace::LongReturnType\n"
12663 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12664 " int first_long_parameter, int second_parameter) {}");
12665
12666 verifyGoogleFormat("template <typename T>\n"
12667 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12668 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12669 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12670 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12671
12672 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12673 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12674 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12675 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12676 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12677 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12678 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12679 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12682
12683 verifyFormat("template <typename T> // Templates on own line.\n"
12684 "static int // Some comment.\n"
12685 "MyFunction(int a);");
12686 }
12687
12688 TEST_F(FormatTest, FormatsAccessModifiers) {
12689 FormatStyle Style = getLLVMStyle();
12690 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
12691 FormatStyle::ELBAMS_LogicalBlock);
12692 verifyFormat("struct foo {\n"
12693 "private:\n"
12694 " void f() {}\n"
12695 "\n"
12696 "private:\n"
12697 " int i;\n"
12698 "\n"
12699 "protected:\n"
12700 " int j;\n"
12701 "};",
12702 Style);
12703 verifyFormat("struct foo {\n"
12704 "private:\n"
12705 " void f() {}\n"
12706 "\n"
12707 "private:\n"
12708 " int i;\n"
12709 "\n"
12710 "protected:\n"
12711 " int j;\n"
12712 "};",
12713 "struct foo {\n"
12714 "private:\n"
12715 " void f() {}\n"
12716 "private:\n"
12717 " int i;\n"
12718 "protected:\n"
12719 " int j;\n"
12720 "};",
12721 Style);
12722 verifyFormat("struct foo { /* comment */\n"
12723 "private:\n"
12724 " int i;\n"
12725 " // comment\n"
12726 "private:\n"
12727 " int j;\n"
12728 "};",
12729 Style);
12730 verifyFormat("struct foo {\n"
12731 "#ifdef FOO\n"
12732 "#endif\n"
12733 "private:\n"
12734 " int i;\n"
12735 "#ifdef FOO\n"
12736 "private:\n"
12737 "#endif\n"
12738 " int j;\n"
12739 "};",
12740 Style);
12741 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12742 verifyFormat("struct foo {\n"
12743 "private:\n"
12744 " void f() {}\n"
12745 "private:\n"
12746 " int i;\n"
12747 "protected:\n"
12748 " int j;\n"
12749 "};",
12750 Style);
12751 verifyFormat("struct foo {\n"
12752 "private:\n"
12753 " void f() {}\n"
12754 "private:\n"
12755 " int i;\n"
12756 "protected:\n"
12757 " int j;\n"
12758 "};",
12759 "struct foo {\n"
12760 "\n"
12761 "private:\n"
12762 " void f() {}\n"
12763 "\n"
12764 "private:\n"
12765 " int i;\n"
12766 "\n"
12767 "protected:\n"
12768 " int j;\n"
12769 "};",
12770 Style);
12771 verifyFormat("struct foo { /* comment */\n"
12772 "private:\n"
12773 " int i;\n"
12774 " // comment\n"
12775 "private:\n"
12776 " int j;\n"
12777 "};",
12778 "struct foo { /* comment */\n"
12779 "\n"
12780 "private:\n"
12781 " int i;\n"
12782 " // comment\n"
12783 "\n"
12784 "private:\n"
12785 " int j;\n"
12786 "};",
12787 Style);
12788 verifyFormat("struct foo {\n"
12789 "#ifdef FOO\n"
12790 "#endif\n"
12791 "private:\n"
12792 " int i;\n"
12793 "#ifdef FOO\n"
12794 "private:\n"
12795 "#endif\n"
12796 " int j;\n"
12797 "};",
12798 "struct foo {\n"
12799 "#ifdef FOO\n"
12800 "#endif\n"
12801 "\n"
12802 "private:\n"
12803 " int i;\n"
12804 "#ifdef FOO\n"
12805 "\n"
12806 "private:\n"
12807 "#endif\n"
12808 " int j;\n"
12809 "};",
12810 Style);
12811 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12812 verifyFormat("struct foo {\n"
12813 "private:\n"
12814 " void f() {}\n"
12815 "\n"
12816 "private:\n"
12817 " int i;\n"
12818 "\n"
12819 "protected:\n"
12820 " int j;\n"
12821 "};",
12822 Style);
12823 verifyFormat("struct foo {\n"
12824 "private:\n"
12825 " void f() {}\n"
12826 "\n"
12827 "private:\n"
12828 " int i;\n"
12829 "\n"
12830 "protected:\n"
12831 " int j;\n"
12832 "};",
12833 "struct foo {\n"
12834 "private:\n"
12835 " void f() {}\n"
12836 "private:\n"
12837 " int i;\n"
12838 "protected:\n"
12839 " int j;\n"
12840 "};",
12841 Style);
12842 verifyFormat("struct foo { /* comment */\n"
12843 "private:\n"
12844 " int i;\n"
12845 " // comment\n"
12846 "\n"
12847 "private:\n"
12848 " int j;\n"
12849 "};",
12850 Style);
12851 verifyFormat("struct foo {\n"
12852 "#ifdef FOO\n"
12853 "#endif\n"
12854 "\n"
12855 "private:\n"
12856 " int i;\n"
12857 "#ifdef FOO\n"
12858 "\n"
12859 "private:\n"
12860 "#endif\n"
12861 " int j;\n"
12862 "};",
12863 "struct foo {\n"
12864 "#ifdef FOO\n"
12865 "#endif\n"
12866 "private:\n"
12867 " int i;\n"
12868 "#ifdef FOO\n"
12869 "private:\n"
12870 "#endif\n"
12871 " int j;\n"
12872 "};",
12873 Style);
12874 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12875 verifyNoChange("struct foo {\n"
12876 "\n"
12877 "private:\n"
12878 " void f() {}\n"
12879 "\n"
12880 "private:\n"
12881 " int i;\n"
12882 "\n"
12883 "protected:\n"
12884 " int j;\n"
12885 "};",
12886 Style);
12887 verifyFormat("struct foo {\n"
12888 "private:\n"
12889 " void f() {}\n"
12890 "private:\n"
12891 " int i;\n"
12892 "protected:\n"
12893 " int j;\n"
12894 "};",
12895 Style);
12896 verifyNoChange("struct foo { /* comment */\n"
12897 "\n"
12898 "private:\n"
12899 " int i;\n"
12900 " // comment\n"
12901 "\n"
12902 "private:\n"
12903 " int j;\n"
12904 "};",
12905 Style);
12906 verifyFormat("struct foo { /* comment */\n"
12907 "private:\n"
12908 " int i;\n"
12909 " // comment\n"
12910 "private:\n"
12911 " int j;\n"
12912 "};",
12913 Style);
12914 verifyNoChange("struct foo {\n"
12915 "#ifdef FOO\n"
12916 "#endif\n"
12917 "\n"
12918 "private:\n"
12919 " int i;\n"
12920 "#ifdef FOO\n"
12921 "\n"
12922 "private:\n"
12923 "#endif\n"
12924 " int j;\n"
12925 "};",
12926 Style);
12927 verifyFormat("struct foo {\n"
12928 "#ifdef FOO\n"
12929 "#endif\n"
12930 "private:\n"
12931 " int i;\n"
12932 "#ifdef FOO\n"
12933 "private:\n"
12934 "#endif\n"
12935 " int j;\n"
12936 "};",
12937 Style);
12938 Style.AttributeMacros.push_back("FOO");
12939 Style.AttributeMacros.push_back("BAR");
12940 verifyFormat("struct foo {\n"
12941 "FOO private:\n"
12942 " int i;\n"
12943 "BAR(x) protected:\n"
12944 " int j;\n"
12945 "};",
12946 Style);
12947
12948 FormatStyle NoEmptyLines = getLLVMStyle();
12949 NoEmptyLines.MaxEmptyLinesToKeep = 0;
12950 verifyFormat("struct foo {\n"
12951 "private:\n"
12952 " void f() {}\n"
12953 "\n"
12954 "private:\n"
12955 " int i;\n"
12956 "\n"
12957 "public:\n"
12958 "protected:\n"
12959 " int j;\n"
12960 "};",
12961 NoEmptyLines);
12962
12963 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12964 verifyFormat("struct foo {\n"
12965 "private:\n"
12966 " void f() {}\n"
12967 "private:\n"
12968 " int i;\n"
12969 "public:\n"
12970 "protected:\n"
12971 " int j;\n"
12972 "};",
12973 NoEmptyLines);
12974
12975 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12976 verifyFormat("struct foo {\n"
12977 "private:\n"
12978 " void f() {}\n"
12979 "\n"
12980 "private:\n"
12981 " int i;\n"
12982 "\n"
12983 "public:\n"
12984 "\n"
12985 "protected:\n"
12986 " int j;\n"
12987 "};",
12988 NoEmptyLines);
12989 }
12990
12991 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
12992
12993 FormatStyle Style = getLLVMStyle();
12994 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
12995 verifyFormat("struct foo {\n"
12996 "private:\n"
12997 " void f() {}\n"
12998 "\n"
12999 "private:\n"
13000 " int i;\n"
13001 "\n"
13002 "protected:\n"
13003 " int j;\n"
13004 "};",
13005 Style);
13006
13007 // Check if lines are removed.
13008 verifyFormat("struct foo {\n"
13009 "private:\n"
13010 " void f() {}\n"
13011 "\n"
13012 "private:\n"
13013 " int i;\n"
13014 "\n"
13015 "protected:\n"
13016 " int j;\n"
13017 "};",
13018 "struct foo {\n"
13019 "private:\n"
13020 "\n"
13021 " void f() {}\n"
13022 "\n"
13023 "private:\n"
13024 "\n"
13025 " int i;\n"
13026 "\n"
13027 "protected:\n"
13028 "\n"
13029 " int j;\n"
13030 "};",
13031 Style);
13032
13033 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13034 verifyFormat("struct foo {\n"
13035 "private:\n"
13036 "\n"
13037 " void f() {}\n"
13038 "\n"
13039 "private:\n"
13040 "\n"
13041 " int i;\n"
13042 "\n"
13043 "protected:\n"
13044 "\n"
13045 " int j;\n"
13046 "};",
13047 Style);
13048
13049 // Check if lines are added.
13050 verifyFormat("struct foo {\n"
13051 "private:\n"
13052 "\n"
13053 " void f() {}\n"
13054 "\n"
13055 "private:\n"
13056 "\n"
13057 " int i;\n"
13058 "\n"
13059 "protected:\n"
13060 "\n"
13061 " int j;\n"
13062 "};",
13063 "struct foo {\n"
13064 "private:\n"
13065 " void f() {}\n"
13066 "\n"
13067 "private:\n"
13068 " int i;\n"
13069 "\n"
13070 "protected:\n"
13071 " int j;\n"
13072 "};",
13073 Style);
13074
13075 // Leave tests rely on the code layout, test::messUp can not be used.
13076 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13077 Style.MaxEmptyLinesToKeep = 0u;
13078 verifyFormat("struct foo {\n"
13079 "private:\n"
13080 " void f() {}\n"
13081 "\n"
13082 "private:\n"
13083 " int i;\n"
13084 "\n"
13085 "protected:\n"
13086 " int j;\n"
13087 "};",
13088 Style);
13089
13090 // Check if MaxEmptyLinesToKeep is respected.
13091 verifyFormat("struct foo {\n"
13092 "private:\n"
13093 " void f() {}\n"
13094 "\n"
13095 "private:\n"
13096 " int i;\n"
13097 "\n"
13098 "protected:\n"
13099 " int j;\n"
13100 "};",
13101 "struct foo {\n"
13102 "private:\n"
13103 "\n\n\n"
13104 " void f() {}\n"
13105 "\n"
13106 "private:\n"
13107 "\n\n\n"
13108 " int i;\n"
13109 "\n"
13110 "protected:\n"
13111 "\n\n\n"
13112 " int j;\n"
13113 "};",
13114 Style);
13115
13116 Style.MaxEmptyLinesToKeep = 1u;
13117 verifyNoChange("struct foo {\n"
13118 "private:\n"
13119 "\n"
13120 " void f() {}\n"
13121 "\n"
13122 "private:\n"
13123 "\n"
13124 " int i;\n"
13125 "\n"
13126 "protected:\n"
13127 "\n"
13128 " int j;\n"
13129 "};",
13130 Style);
13131 // Check if no lines are kept.
13132 verifyFormat("struct foo {\n"
13133 "private:\n"
13134 " void f() {}\n"
13135 "\n"
13136 "private:\n"
13137 " int i;\n"
13138 "\n"
13139 "protected:\n"
13140 " int j;\n"
13141 "};",
13142 Style);
13143 // Check if MaxEmptyLinesToKeep is respected.
13144 verifyFormat("struct foo {\n"
13145 "private:\n"
13146 "\n"
13147 " void f() {}\n"
13148 "\n"
13149 "private:\n"
13150 "\n"
13151 " int i;\n"
13152 "\n"
13153 "protected:\n"
13154 "\n"
13155 " int j;\n"
13156 "};",
13157 "struct foo {\n"
13158 "private:\n"
13159 "\n\n\n"
13160 " void f() {}\n"
13161 "\n"
13162 "private:\n"
13163 "\n\n\n"
13164 " int i;\n"
13165 "\n"
13166 "protected:\n"
13167 "\n\n\n"
13168 " int j;\n"
13169 "};",
13170 Style);
13171
13172 Style.MaxEmptyLinesToKeep = 10u;
13173 verifyNoChange("struct foo {\n"
13174 "private:\n"
13175 "\n\n\n"
13176 " void f() {}\n"
13177 "\n"
13178 "private:\n"
13179 "\n\n\n"
13180 " int i;\n"
13181 "\n"
13182 "protected:\n"
13183 "\n\n\n"
13184 " int j;\n"
13185 "};",
13186 Style);
13187
13188 // Test with comments.
13189 Style = getLLVMStyle();
13190 verifyFormat("struct foo {\n"
13191 "private:\n"
13192 " // comment\n"
13193 " void f() {}\n"
13194 "\n"
13195 "private: /* comment */\n"
13196 " int i;\n"
13197 "};",
13198 Style);
13199 verifyFormat("struct foo {\n"
13200 "private:\n"
13201 " // comment\n"
13202 " void f() {}\n"
13203 "\n"
13204 "private: /* comment */\n"
13205 " int i;\n"
13206 "};",
13207 "struct foo {\n"
13208 "private:\n"
13209 "\n"
13210 " // comment\n"
13211 " void f() {}\n"
13212 "\n"
13213 "private: /* comment */\n"
13214 "\n"
13215 " int i;\n"
13216 "};",
13217 Style);
13218
13219 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13220 verifyFormat("struct foo {\n"
13221 "private:\n"
13222 "\n"
13223 " // comment\n"
13224 " void f() {}\n"
13225 "\n"
13226 "private: /* comment */\n"
13227 "\n"
13228 " int i;\n"
13229 "};",
13230 "struct foo {\n"
13231 "private:\n"
13232 " // comment\n"
13233 " void f() {}\n"
13234 "\n"
13235 "private: /* comment */\n"
13236 " int i;\n"
13237 "};",
13238 Style);
13239 verifyFormat("struct foo {\n"
13240 "private:\n"
13241 "\n"
13242 " // comment\n"
13243 " void f() {}\n"
13244 "\n"
13245 "private: /* comment */\n"
13246 "\n"
13247 " int i;\n"
13248 "};",
13249 Style);
13250
13251 // Test with preprocessor defines.
13252 Style = getLLVMStyle();
13253 verifyFormat("struct foo {\n"
13254 "private:\n"
13255 "#ifdef FOO\n"
13256 "#endif\n"
13257 " void f() {}\n"
13258 "};",
13259 Style);
13260 verifyFormat("struct foo {\n"
13261 "private:\n"
13262 "#ifdef FOO\n"
13263 "#endif\n"
13264 " void f() {}\n"
13265 "};",
13266 "struct foo {\n"
13267 "private:\n"
13268 "\n"
13269 "#ifdef FOO\n"
13270 "#endif\n"
13271 " void f() {}\n"
13272 "};",
13273 Style);
13274 verifyNoChange("struct foo {\n"
13275 "#ifdef FOO\n"
13276 "#else\n"
13277 "private:\n"
13278 "\n"
13279 "#endif\n"
13280 "};",
13281 Style);
13282 verifyFormat("struct foo {\n"
13283 "#ifdef FOO\n"
13284 "#else\n"
13285 "private:\n"
13286 "\n"
13287 "#endif\n"
13288 "};",
13289 "struct foo {\n"
13290 "#ifdef FOO\n"
13291 "#else\n"
13292 "private:\n"
13293 "\n"
13294 "\n"
13295 "#endif\n"
13296 "};",
13297 Style);
13298 verifyFormat("struct foo {\n"
13299 "#ifdef FOO\n"
13300 "private:\n"
13301 "#else\n"
13302 "#endif\n"
13303 "};",
13304 "struct foo {\n"
13305 "#ifdef FOO\n"
13306 "private:\n"
13307 "\n"
13308 "\n"
13309 "#else\n"
13310 "#endif\n"
13311 "};",
13312 Style);
13313 verifyFormat("struct foo {\n"
13314 "#if 0\n"
13315 "#else\n"
13316 "#endif\n"
13317 "#ifdef FOO\n"
13318 "private:\n"
13319 "#endif\n"
13320 "};",
13321 "struct foo {\n"
13322 "#if 0\n"
13323 "#else\n"
13324 "#endif\n"
13325 "#ifdef FOO\n"
13326 "private:\n"
13327 "\n"
13328 "\n"
13329 "#endif\n"
13330 "};",
13331 Style);
13332
13333 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13334 verifyFormat("struct foo {\n"
13335 "private:\n"
13336 "\n"
13337 "#ifdef FOO\n"
13338 "#endif\n"
13339 " void f() {}\n"
13340 "};",
13341 "struct foo {\n"
13342 "private:\n"
13343 "#ifdef FOO\n"
13344 "#endif\n"
13345 " void f() {}\n"
13346 "};",
13347 Style);
13348 verifyFormat("struct foo {\n"
13349 "private:\n"
13350 "\n"
13351 "#ifdef FOO\n"
13352 "#endif\n"
13353 " void f() {}\n"
13354 "};",
13355 Style);
13356 }
13357
13358 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13359 // Combined tests of EmptyLineAfterAccessModifier and
13360 // EmptyLineBeforeAccessModifier.
13361 FormatStyle Style = getLLVMStyle();
13362 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13363 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13364 verifyFormat("struct foo {\n"
13365 "private:\n"
13366 "\n"
13367 "protected:\n"
13368 "};",
13369 Style);
13370
13371 Style.MaxEmptyLinesToKeep = 10u;
13372 // Both remove all new lines.
13373 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13374 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13375 verifyFormat("struct foo {\n"
13376 "private:\n"
13377 "protected:\n"
13378 "};",
13379 "struct foo {\n"
13380 "private:\n"
13381 "\n\n\n"
13382 "protected:\n"
13383 "};",
13384 Style);
13385
13386 // Leave tests rely on the code layout, test::messUp can not be used.
13387 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13388 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13389 Style.MaxEmptyLinesToKeep = 10u;
13390 verifyNoChange("struct foo {\n"
13391 "private:\n"
13392 "\n\n\n"
13393 "protected:\n"
13394 "};",
13395 Style);
13396 Style.MaxEmptyLinesToKeep = 3u;
13397 verifyNoChange("struct foo {\n"
13398 "private:\n"
13399 "\n\n\n"
13400 "protected:\n"
13401 "};",
13402 Style);
13403 Style.MaxEmptyLinesToKeep = 1u;
13404 verifyNoChange("struct foo {\n"
13405 "private:\n"
13406 "\n\n\n"
13407 "protected:\n"
13408 "};",
13409 Style); // Based on new lines in original document and not
13410 // on the setting.
13411
13412 Style.MaxEmptyLinesToKeep = 10u;
13413 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13414 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13415 // Newlines are kept if they are greater than zero,
13416 // test::messUp removes all new lines which changes the logic
13417 verifyNoChange("struct foo {\n"
13418 "private:\n"
13419 "\n\n\n"
13420 "protected:\n"
13421 "};",
13422 Style);
13423
13424 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13425 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13426 // test::messUp removes all new lines which changes the logic
13427 verifyNoChange("struct foo {\n"
13428 "private:\n"
13429 "\n\n\n"
13430 "protected:\n"
13431 "};",
13432 Style);
13433
13434 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13435 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13436 verifyNoChange("struct foo {\n"
13437 "private:\n"
13438 "\n\n\n"
13439 "protected:\n"
13440 "};",
13441 Style); // test::messUp removes all new lines which changes
13442 // the logic.
13443
13444 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13445 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13446 verifyFormat("struct foo {\n"
13447 "private:\n"
13448 "protected:\n"
13449 "};",
13450 "struct foo {\n"
13451 "private:\n"
13452 "\n\n\n"
13453 "protected:\n"
13454 "};",
13455 Style);
13456
13457 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13458 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13459 verifyNoChange("struct foo {\n"
13460 "private:\n"
13461 "\n\n\n"
13462 "protected:\n"
13463 "};",
13464 Style); // test::messUp removes all new lines which changes
13465 // the logic.
13466
13467 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13468 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13469 verifyFormat("struct foo {\n"
13470 "private:\n"
13471 "protected:\n"
13472 "};",
13473 "struct foo {\n"
13474 "private:\n"
13475 "\n\n\n"
13476 "protected:\n"
13477 "};",
13478 Style);
13479
13480 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13481 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13482 verifyFormat("struct foo {\n"
13483 "private:\n"
13484 "protected:\n"
13485 "};",
13486 "struct foo {\n"
13487 "private:\n"
13488 "\n\n\n"
13489 "protected:\n"
13490 "};",
13491 Style);
13492
13493 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13494 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13495 verifyFormat("struct foo {\n"
13496 "private:\n"
13497 "protected:\n"
13498 "};",
13499 "struct foo {\n"
13500 "private:\n"
13501 "\n\n\n"
13502 "protected:\n"
13503 "};",
13504 Style);
13505
13506 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13507 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13508 verifyFormat("struct foo {\n"
13509 "private:\n"
13510 "protected:\n"
13511 "};",
13512 "struct foo {\n"
13513 "private:\n"
13514 "\n\n\n"
13515 "protected:\n"
13516 "};",
13517 Style);
13518 }
13519
13520 TEST_F(FormatTest, FormatsArrays) {
13521 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13522 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13523 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13524 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13525 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13526 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13527 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13528 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13529 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13530 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13531 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13532 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13533 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13534 verifyFormat(
13535 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13536 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13537 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13538 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13539 " .aaaaaaaaaaaaaaaaaaaaaa();");
13540
13541 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13543 verifyFormat(
13544 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13545 " .aaaaaaa[0]\n"
13546 " .aaaaaaaaaaaaaaaaaaaaaa();");
13547 verifyFormat("a[::b::c];");
13548
13549 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13550
13551 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13552 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13553 }
13554
13555 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13556 verifyFormat("(a)->b();");
13557 verifyFormat("--a;");
13558 }
13559
13560 TEST_F(FormatTest, HandlesIncludeDirectives) {
13561 verifyFormat("#include <string>\n"
13562 "#include <a/b/c.h>\n"
13563 "#include \"a/b/string\"\n"
13564 "#include \"string.h\"\n"
13565 "#include \"string.h\"\n"
13566 "#include <a-a>\n"
13567 "#include < path with space >\n"
13568 "#include_next <test.h>"
13569 "#include \"abc.h\" // this is included for ABC\n"
13570 "#include \"some long include\" // with a comment\n"
13571 "#include \"some very long include path\"\n"
13572 "#include <some/very/long/include/path>",
13573 getLLVMStyleWithColumns(35));
13574 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13575 verifyFormat("#include <a>", "#include<a>");
13576
13577 verifyFormat("#import <string>");
13578 verifyFormat("#import <a/b/c.h>");
13579 verifyFormat("#import \"a/b/string\"");
13580 verifyFormat("#import \"string.h\"");
13581 verifyFormat("#import \"string.h\"");
13582 verifyFormat("#if __has_include(<strstream>)\n"
13583 "#include <strstream>\n"
13584 "#endif");
13585
13586 verifyFormat("#define MY_IMPORT <a/b>");
13587
13588 verifyFormat("#if __has_include(<a/b>)");
13589 verifyFormat("#if __has_include_next(<a/b>)");
13590 verifyFormat("#define F __has_include(<a/b>)");
13591 verifyFormat("#define F __has_include_next(<a/b>)");
13592
13593 // Protocol buffer definition or missing "#".
13594 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13595 getLLVMStyleWithColumns(30));
13596
13597 FormatStyle Style = getLLVMStyle();
13598 Style.AlwaysBreakBeforeMultilineStrings = true;
13599 Style.ColumnLimit = 0;
13600 verifyFormat("#import \"abc.h\"", Style);
13601
13602 // But 'import' might also be a regular C++ namespace.
13603 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13605 verifyFormat("import::Bar foo(val ? 2 : 1);");
13606 }
13607
13608 //===----------------------------------------------------------------------===//
13609 // Error recovery tests.
13610 //===----------------------------------------------------------------------===//
13611
13612 TEST_F(FormatTest, IncompleteParameterLists) {
13613 FormatStyle NoBinPacking = getLLVMStyle();
13614 NoBinPacking.BinPackParameters = false;
13615 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13616 " double *min_x,\n"
13617 " double *max_x,\n"
13618 " double *min_y,\n"
13619 " double *max_y,\n"
13620 " double *min_z,\n"
13621 " double *max_z, ) {}",
13622 NoBinPacking);
13623 }
13624
13625 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13626 verifyFormat("void f() { return; }\n42");
13627 verifyFormat("void f() {\n"
13628 " if (0)\n"
13629 " return;\n"
13630 "}\n"
13631 "42");
13632 verifyFormat("void f() { return }\n42");
13633 verifyFormat("void f() {\n"
13634 " if (0)\n"
13635 " return\n"
13636 "}\n"
13637 "42");
13638 }
13639
13640 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13641 verifyFormat("void f() { return }", "void f ( ) { return }");
13642 verifyFormat("void f() {\n"
13643 " if (a)\n"
13644 " return\n"
13645 "}",
13646 "void f ( ) { if ( a ) return }");
13647 verifyFormat("namespace N {\n"
13648 "void f()\n"
13649 "}",
13650 "namespace N { void f() }");
13651 verifyFormat("namespace N {\n"
13652 "void f() {}\n"
13653 "void g()\n"
13654 "} // namespace N",
13655 "namespace N { void f( ) { } void g( ) }");
13656 }
13657
13658 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13659 verifyFormat("int aaaaaaaa =\n"
13660 " // Overlylongcomment\n"
13661 " b;",
13662 getLLVMStyleWithColumns(20));
13663 verifyFormat("function(\n"
13664 " ShortArgument,\n"
13665 " LoooooooooooongArgument);",
13666 getLLVMStyleWithColumns(20));
13667 }
13668
13669 TEST_F(FormatTest, IncorrectAccessSpecifier) {
13670 verifyFormat("public:");
13671 verifyFormat("class A {\n"
13672 "public\n"
13673 " void f() {}\n"
13674 "};");
13675 verifyFormat("public\n"
13676 "int qwerty;");
13677 verifyFormat("public\n"
13678 "B {}");
13679 verifyFormat("public\n"
13680 "{\n"
13681 "}");
13682 verifyFormat("public\n"
13683 "B { int x; }");
13684 }
13685
13686 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
13687 verifyFormat("{");
13688 verifyFormat("#})");
13689 verifyNoCrash("(/**/[:!] ?[).");
13690 verifyNoCrash("struct X {\n"
13691 " operator iunt(\n"
13692 "};");
13693 verifyNoCrash("struct Foo {\n"
13694 " operator foo(bar\n"
13695 "};");
13696 }
13697
13698 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
13699 // Found by oss-fuzz:
13700 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13701 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
13702 Style.ColumnLimit = 60;
13703 verifyNoCrash(
13704 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13705 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13706 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13707 Style);
13708 }
13709
13710 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
13711 verifyFormat("do {\n}");
13712 verifyFormat("do {\n}\n"
13713 "f();");
13714 verifyFormat("do {\n}\n"
13715 "wheeee(fun);");
13716 verifyFormat("do {\n"
13717 " f();\n"
13718 "}");
13719 }
13720
13721 TEST_F(FormatTest, IncorrectCodeMissingParens) {
13722 verifyFormat("if {\n foo;\n foo();\n}");
13723 verifyFormat("switch {\n foo;\n foo();\n}");
13724 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13725 verifyIncompleteFormat("ERROR: for target;");
13726 verifyFormat("while {\n foo;\n foo();\n}");
13727 verifyFormat("do {\n foo;\n foo();\n} while;");
13728 }
13729
13730 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
13731 verifyIncompleteFormat("namespace {\n"
13732 "class Foo { Foo (\n"
13733 "};\n"
13734 "} // namespace");
13735 }
13736
13737 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13738 verifyFormat("{\n"
13739 " {\n"
13740 " }",
13741 "{\n"
13742 "{\n"
13743 "}");
13744 verifyFormat("{\n"
13745 " {\n"
13746 " }",
13747 "{\n"
13748 " {\n"
13749 "}");
13750 verifyFormat("{\n"
13751 " {\n"
13752 " }");
13753 verifyFormat("{\n"
13754 " {\n"
13755 " }\n"
13756 "}\n"
13757 "}",
13758 "{\n"
13759 " {\n"
13760 " }\n"
13761 " }\n"
13762 "}");
13763
13764 verifyFormat("{\n"
13765 " {\n"
13766 " breakme(\n"
13767 " qwe);\n"
13768 " }",
13769 "{\n"
13770 " {\n"
13771 " breakme(qwe);\n"
13772 "}",
13773 getLLVMStyleWithColumns(10));
13774 }
13775
13776 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
13777 verifyFormat("int x = {\n"
13778 " avariable,\n"
13779 " b(alongervariable)};",
13780 getLLVMStyleWithColumns(25));
13781 }
13782
13783 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
13784 verifyFormat("return (a)(b){1, 2, 3};");
13785 }
13786
13787 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
13788 verifyFormat("vector<int> x{1, 2, 3, 4};");
13789 verifyFormat("vector<int> x{\n"
13790 " 1,\n"
13791 " 2,\n"
13792 " 3,\n"
13793 " 4,\n"
13794 "};");
13795 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13796 verifyFormat("f({1, 2});");
13797 verifyFormat("auto v = Foo{-1};");
13798 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13799 verifyFormat("Class::Class : member{1, 2, 3} {}");
13800 verifyFormat("new vector<int>{1, 2, 3};");
13801 verifyFormat("new int[3]{1, 2, 3};");
13802 verifyFormat("new int{1};");
13803 verifyFormat("return {arg1, arg2};");
13804 verifyFormat("return {arg1, SomeType{parameter}};");
13805 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13806 verifyFormat("new T{arg1, arg2};");
13807 verifyFormat("f(MyMap[{composite, key}]);");
13808 verifyFormat("class Class {\n"
13809 " T member = {arg1, arg2};\n"
13810 "};");
13811 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13812 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13813 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13814 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13815 verifyFormat("int a = std::is_integral<int>{} + 0;");
13816
13817 verifyFormat("int foo(int i) { return fo1{}(i); }");
13818 verifyFormat("int foo(int i) { return fo1{}(i); }");
13819 verifyFormat("auto i = decltype(x){};");
13820 verifyFormat("auto i = typeof(x){};");
13821 verifyFormat("auto i = _Atomic(x){};");
13822 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13823 verifyFormat("Node n{1, Node{1000}, //\n"
13824 " 2};");
13825 verifyFormat("Aaaa aaaaaaa{\n"
13826 " {\n"
13827 " aaaa,\n"
13828 " },\n"
13829 "};");
13830 verifyFormat("class C : public D {\n"
13831 " SomeClass SC{2};\n"
13832 "};");
13833 verifyFormat("class C : public A {\n"
13834 " class D : public B {\n"
13835 " void f() { int i{2}; }\n"
13836 " };\n"
13837 "};");
13838 verifyFormat("#define A {a, a},");
13839 // Don't confuse braced list initializers with compound statements.
13840 verifyFormat(
13841 "class A {\n"
13842 " A() : a{} {}\n"
13843 " A() : Base<int>{} {}\n"
13844 " A() : Base<Foo<int>>{} {}\n"
13845 " A(int b) : b(b) {}\n"
13846 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13847 " int a, b;\n"
13848 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13849 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13850 "{}\n"
13851 "};");
13852
13853 // Avoid breaking between equal sign and opening brace
13854 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
13855 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
13856 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13857 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13858 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13859 " {\"ccccccccccccccccccccc\", 2}};",
13860 AvoidBreakingFirstArgument);
13861
13862 // Binpacking only if there is no trailing comma
13863 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13864 " cccccccccc, dddddddddd};",
13865 getLLVMStyleWithColumns(50));
13866 verifyFormat("const Aaaaaa aaaaa = {\n"
13867 " aaaaaaaaaaa,\n"
13868 " bbbbbbbbbbb,\n"
13869 " ccccccccccc,\n"
13870 " ddddddddddd,\n"
13871 "};",
13872 getLLVMStyleWithColumns(50));
13873
13874 // Cases where distinguising braced lists and blocks is hard.
13875 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13876 verifyFormat("void f() {\n"
13877 " return; // comment\n"
13878 "}\n"
13879 "SomeType t;");
13880 verifyFormat("void f() {\n"
13881 " if (a) {\n"
13882 " f();\n"
13883 " }\n"
13884 "}\n"
13885 "SomeType t;");
13886
13887 // In combination with BinPackArguments = false.
13888 FormatStyle NoBinPacking = getLLVMStyle();
13889 NoBinPacking.BinPackArguments = false;
13890 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
13891 " bbbbb,\n"
13892 " ccccc,\n"
13893 " ddddd,\n"
13894 " eeeee,\n"
13895 " ffffff,\n"
13896 " ggggg,\n"
13897 " hhhhhh,\n"
13898 " iiiiii,\n"
13899 " jjjjjj,\n"
13900 " kkkkkk};",
13901 NoBinPacking);
13902 verifyFormat("const Aaaaaa aaaaa = {\n"
13903 " aaaaa,\n"
13904 " bbbbb,\n"
13905 " ccccc,\n"
13906 " ddddd,\n"
13907 " eeeee,\n"
13908 " ffffff,\n"
13909 " ggggg,\n"
13910 " hhhhhh,\n"
13911 " iiiiii,\n"
13912 " jjjjjj,\n"
13913 " kkkkkk,\n"
13914 "};",
13915 NoBinPacking);
13916 verifyFormat(
13917 "const Aaaaaa aaaaa = {\n"
13918 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
13919 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
13920 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
13921 "};",
13922 NoBinPacking);
13923
13924 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13925 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
13926 " CDDDP83848_BMCR_REGISTER,\n"
13927 " CDDDP83848_BMSR_REGISTER,\n"
13928 " CDDDP83848_RBR_REGISTER};",
13929 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
13930 " CDDDP83848_BMSR_REGISTER,\n"
13931 " CDDDP83848_RBR_REGISTER};",
13932 NoBinPacking);
13933
13934 // FIXME: The alignment of these trailing comments might be bad. Then again,
13935 // this might be utterly useless in real code.
13936 verifyFormat("Constructor::Constructor()\n"
13937 " : some_value{ //\n"
13938 " aaaaaaa, //\n"
13939 " bbbbbbb} {}");
13940
13941 // In braced lists, the first comment is always assumed to belong to the
13942 // first element. Thus, it can be moved to the next or previous line as
13943 // appropriate.
13944 verifyFormat("function({// First element:\n"
13945 " 1,\n"
13946 " // Second element:\n"
13947 " 2});",
13948 "function({\n"
13949 " // First element:\n"
13950 " 1,\n"
13951 " // Second element:\n"
13952 " 2});");
13953 verifyFormat("std::vector<int> MyNumbers{\n"
13954 " // First element:\n"
13955 " 1,\n"
13956 " // Second element:\n"
13957 " 2};",
13958 "std::vector<int> MyNumbers{// First element:\n"
13959 " 1,\n"
13960 " // Second element:\n"
13961 " 2};",
13962 getLLVMStyleWithColumns(30));
13963 // A trailing comma should still lead to an enforced line break and no
13964 // binpacking.
13965 verifyFormat("vector<int> SomeVector = {\n"
13966 " // aaa\n"
13967 " 1,\n"
13968 " 2,\n"
13969 "};",
13970 "vector<int> SomeVector = { // aaa\n"
13971 " 1, 2, };");
13972
13973 // C++11 brace initializer list l-braces should not be treated any differently
13974 // when breaking before lambda bodies is enabled
13975 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
13976 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
13977 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
13978 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
13979 verifyFormat(
13980 "std::runtime_error{\n"
13981 " \"Long string which will force a break onto the next line...\"};",
13982 BreakBeforeLambdaBody);
13983
13984 FormatStyle ExtraSpaces = getLLVMStyle();
13985 ExtraSpaces.Cpp11BracedListStyle = false;
13986 ExtraSpaces.ColumnLimit = 75;
13987 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
13988 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
13989 verifyFormat("f({ 1, 2 });", ExtraSpaces);
13990 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
13991 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
13992 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
13993 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
13994 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
13995 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
13996 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
13997 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
13998 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
13999 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
14000 verifyFormat("class Class {\n"
14001 " T member = { arg1, arg2 };\n"
14002 "};",
14003 ExtraSpaces);
14004 verifyFormat(
14005 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14006 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14007 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14008 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14009 ExtraSpaces);
14010 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
14011 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14012 ExtraSpaces);
14013 verifyFormat(
14014 "someFunction(OtherParam,\n"
14015 " BracedList{ // comment 1 (Forcing interesting break)\n"
14016 " param1, param2,\n"
14017 " // comment 2\n"
14018 " param3, param4 });",
14019 ExtraSpaces);
14020 verifyFormat(
14021 "std::this_thread::sleep_for(\n"
14022 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14023 ExtraSpaces);
14024 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14025 " aaaaaaa,\n"
14026 " aaaaaaaaaa,\n"
14027 " aaaaa,\n"
14028 " aaaaaaaaaaaaaaa,\n"
14029 " aaa,\n"
14030 " aaaaaaaaaa,\n"
14031 " a,\n"
14032 " aaaaaaaaaaaaaaaaaaaaa,\n"
14033 " aaaaaaaaaaaa,\n"
14034 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14035 " aaaaaaa,\n"
14036 " a};");
14037 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
14038 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
14039 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
14040
14041 // Avoid breaking between initializer/equal sign and opening brace
14042 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
14043 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14044 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14045 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14046 " { \"ccccccccccccccccccccc\", 2 }\n"
14047 "};",
14048 ExtraSpaces);
14049 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14050 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14051 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14052 " { \"ccccccccccccccccccccc\", 2 }\n"
14053 "};",
14054 ExtraSpaces);
14055
14056 FormatStyle SpaceBeforeBrace = getLLVMStyle();
14057 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
14058 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
14059 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
14060
14061 FormatStyle SpaceBetweenBraces = getLLVMStyle();
14062 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
14063 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14064 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
14065 SpaceBetweenBraces.SpacesInSquareBrackets = true;
14066 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
14067 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
14068 verifyFormat("vector< int > x{ // comment 1\n"
14069 " 1, 2, 3, 4 };",
14070 SpaceBetweenBraces);
14071 SpaceBetweenBraces.ColumnLimit = 20;
14072 verifyFormat("vector< int > x{\n"
14073 " 1, 2, 3, 4 };",
14074 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14075 SpaceBetweenBraces.ColumnLimit = 24;
14076 verifyFormat("vector< int > x{ 1, 2,\n"
14077 " 3, 4 };",
14078 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14079 verifyFormat("vector< int > x{\n"
14080 " 1,\n"
14081 " 2,\n"
14082 " 3,\n"
14083 " 4,\n"
14084 "};",
14085 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14086 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14087 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14088 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14089 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14090 }
14091
14092 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14093 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14094 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14095 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14096 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14097 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14098 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14099 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14100 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14101 " 1, 22, 333, 4444, 55555, //\n"
14102 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14103 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14104 verifyFormat(
14105 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14106 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14107 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14108 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14109 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14110 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14111 " 7777777};");
14112 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14113 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14114 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14115 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14116 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14117 " // Separating comment.\n"
14118 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14119 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14120 " // Leading comment\n"
14121 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14122 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14123 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14124 " 1, 1, 1, 1};",
14125 getLLVMStyleWithColumns(39));
14126 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14127 " 1, 1, 1, 1};",
14128 getLLVMStyleWithColumns(38));
14129 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14130 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14131 getLLVMStyleWithColumns(43));
14132 verifyFormat(
14133 "static unsigned SomeValues[10][3] = {\n"
14134 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14135 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14136 verifyFormat("static auto fields = new vector<string>{\n"
14137 " \"aaaaaaaaaaaaa\",\n"
14138 " \"aaaaaaaaaaaaa\",\n"
14139 " \"aaaaaaaaaaaa\",\n"
14140 " \"aaaaaaaaaaaaaa\",\n"
14141 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14142 " \"aaaaaaaaaaaa\",\n"
14143 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14144 "};");
14145 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14146 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14147 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14148 " 3, cccccccccccccccccccccc};",
14149 getLLVMStyleWithColumns(60));
14150
14151 // Trailing commas.
14152 verifyFormat("vector<int> x = {\n"
14153 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14154 "};",
14155 getLLVMStyleWithColumns(39));
14156 verifyFormat("vector<int> x = {\n"
14157 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14158 "};",
14159 getLLVMStyleWithColumns(39));
14160 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14161 " 1, 1, 1, 1,\n"
14162 " /**/ /**/};",
14163 getLLVMStyleWithColumns(39));
14164
14165 // Trailing comment in the first line.
14166 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14167 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14168 " 111111111, 222222222, 3333333333, 444444444, //\n"
14169 " 11111111, 22222222, 333333333, 44444444};");
14170 // Trailing comment in the last line.
14171 verifyFormat("int aaaaa[] = {\n"
14172 " 1, 2, 3, // comment\n"
14173 " 4, 5, 6 // comment\n"
14174 "};");
14175
14176 // With nested lists, we should either format one item per line or all nested
14177 // lists one on line.
14178 // FIXME: For some nested lists, we can do better.
14179 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14180 " {aaaaaaaaaaaaaaaaaaa},\n"
14181 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14182 " {aaaaaaaaaaaaaaaaa}};",
14183 getLLVMStyleWithColumns(60));
14184 verifyFormat(
14185 "SomeStruct my_struct_array = {\n"
14186 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14187 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14188 " {aaa, aaa},\n"
14189 " {aaa, aaa},\n"
14190 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14191 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14192 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14193
14194 // No column layout should be used here.
14195 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14196 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14197
14198 verifyNoCrash("a<,");
14199
14200 // No braced initializer here.
14201 verifyFormat("void f() {\n"
14202 " struct Dummy {};\n"
14203 " f(v);\n"
14204 "}");
14205 verifyFormat("void foo() {\n"
14206 " { // asdf\n"
14207 " {\n"
14208 " int a;\n"
14209 " }\n"
14210 " }\n"
14211 " {\n"
14212 " {\n"
14213 " int b;\n"
14214 " }\n"
14215 " }\n"
14216 "}");
14217 verifyFormat("namespace n {\n"
14218 "void foo() {\n"
14219 " {\n"
14220 " {\n"
14221 " statement();\n"
14222 " if (false) {\n"
14223 " }\n"
14224 " }\n"
14225 " }\n"
14226 " {\n"
14227 " }\n"
14228 "}\n"
14229 "} // namespace n");
14230
14231 // Long lists should be formatted in columns even if they are nested.
14232 verifyFormat(
14233 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14234 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14235 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14236 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14237 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14238 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14239
14240 // Allow "single-column" layout even if that violates the column limit. There
14241 // isn't going to be a better way.
14242 verifyFormat("std::vector<int> a = {\n"
14243 " aaaaaaaa,\n"
14244 " aaaaaaaa,\n"
14245 " aaaaaaaa,\n"
14246 " aaaaaaaa,\n"
14247 " aaaaaaaaaa,\n"
14248 " aaaaaaaa,\n"
14249 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14250 getLLVMStyleWithColumns(30));
14251 verifyFormat("vector<int> aaaa = {\n"
14252 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14253 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14254 " aaaaaa.aaaaaaa,\n"
14255 " aaaaaa.aaaaaaa,\n"
14256 " aaaaaa.aaaaaaa,\n"
14257 " aaaaaa.aaaaaaa,\n"
14258 "};");
14259
14260 // Don't create hanging lists.
14261 verifyFormat("someFunction(Param, {List1, List2,\n"
14262 " List3});",
14263 getLLVMStyleWithColumns(35));
14264 verifyFormat("someFunction(Param, Param,\n"
14265 " {List1, List2,\n"
14266 " List3});",
14267 getLLVMStyleWithColumns(35));
14268 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14269 " aaaaaaaaaaaaaaaaaaaaaaa);");
14270
14271 // No possible column formats, don't want the optimal paths penalized.
14272 verifyFormat(
14273 "waarudo::unit desk = {\n"
14274 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14275 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14276 "Output{1, 2}; },\n"
14277 " [](const Input &i) -> Output { return "
14278 "Output{1, 2}; });");
14279 FormatStyle NoBinPacking = getLLVMStyle();
14280 NoBinPacking.BinPackParameters = false;
14281 verifyFormat("waarudo::unit desk = {\n"
14282 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14283 "1, 1} * w::m; }};",
14284 NoBinPacking);
14285 }
14286
14287 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14288 FormatStyle DoNotMerge = getLLVMStyle();
14289 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14290
14291 verifyFormat("void f() { return 42; }");
14292 verifyFormat("void f() {\n"
14293 " return 42;\n"
14294 "}",
14295 DoNotMerge);
14296 verifyFormat("void f() {\n"
14297 " // Comment\n"
14298 "}");
14299 verifyFormat("{\n"
14300 "#error {\n"
14301 " int a;\n"
14302 "}");
14303 verifyFormat("{\n"
14304 " int a;\n"
14305 "#error {\n"
14306 "}");
14307 verifyFormat("void f() {} // comment");
14308 verifyFormat("void f() { int a; } // comment");
14309 verifyFormat("void f() {\n"
14310 "} // comment",
14311 DoNotMerge);
14312 verifyFormat("void f() {\n"
14313 " int a;\n"
14314 "} // comment",
14315 DoNotMerge);
14316 verifyFormat("void f() {\n"
14317 "} // comment",
14318 getLLVMStyleWithColumns(15));
14319
14320 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14321 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14322
14323 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14324 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14325 verifyGoogleFormat("class C {\n"
14326 " C()\n"
14327 " : iiiiiiii(nullptr),\n"
14328 " kkkkkkk(nullptr),\n"
14329 " mmmmmmm(nullptr),\n"
14330 " nnnnnnn(nullptr) {}\n"
14331 "};");
14332
14333 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
14334 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
14335 verifyFormat("class C {\n"
14336 " A() : b(0) {}\n"
14337 "};",
14338 "class C{A():b(0){}};", NoColumnLimit);
14339 verifyFormat("A()\n"
14340 " : b(0) {\n"
14341 "}",
14342 "A()\n:b(0)\n{\n}", NoColumnLimit);
14343
14344 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
14345 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
14346 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
14347 verifyFormat("class C {\n"
14348 "#pragma foo\n"
14349 " int foo { return 0; }\n"
14350 "};",
14351 NoColumnLimitWrapAfterFunction);
14352 verifyFormat("class C {\n"
14353 "#pragma foo\n"
14354 " void foo {}\n"
14355 "};",
14356 NoColumnLimitWrapAfterFunction);
14357
14358 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
14359 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
14360 FormatStyle::SFS_None;
14361 verifyFormat("A()\n"
14362 " : b(0) {\n"
14363 "}",
14364 "A():b(0){}", DoNotMergeNoColumnLimit);
14365 verifyFormat("A()\n"
14366 " : b(0) {\n"
14367 "}",
14368 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
14369
14370 verifyFormat("#define A \\\n"
14371 " void f() { \\\n"
14372 " int i; \\\n"
14373 " }",
14374 getLLVMStyleWithColumns(20));
14375 verifyFormat("#define A \\\n"
14376 " void f() { int i; }",
14377 getLLVMStyleWithColumns(21));
14378 verifyFormat("#define A \\\n"
14379 " void f() { \\\n"
14380 " int i; \\\n"
14381 " } \\\n"
14382 " int j;",
14383 getLLVMStyleWithColumns(22));
14384 verifyFormat("#define A \\\n"
14385 " void f() { int i; } \\\n"
14386 " int j;",
14387 getLLVMStyleWithColumns(23));
14388
14389 verifyFormat(
14390 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14391 " aaaaaaaaaaaaaaaaaa,\n"
14392 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14393
14394 constexpr StringRef Code{"void foo() { /* Empty */ }"};
14395 verifyFormat(Code);
14396 verifyFormat(Code, "void foo() { /* Empty */\n"
14397 "}");
14398 verifyFormat(Code, "void foo() {\n"
14399 "/* Empty */\n"
14400 "}");
14401 }
14402
14403 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
14404 FormatStyle MergeEmptyOnly = getLLVMStyle();
14405 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14406 verifyFormat("class C {\n"
14407 " int f() {}\n"
14408 "};",
14409 MergeEmptyOnly);
14410 verifyFormat("class C {\n"
14411 " int f() {\n"
14412 " return 42;\n"
14413 " }\n"
14414 "};",
14415 MergeEmptyOnly);
14416 verifyFormat("int f() {}", MergeEmptyOnly);
14417 verifyFormat("int f() {\n"
14418 " return 42;\n"
14419 "}",
14420 MergeEmptyOnly);
14421
14422 // Also verify behavior when BraceWrapping.AfterFunction = true
14423 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14424 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
14425 verifyFormat("int f() {}", MergeEmptyOnly);
14426 verifyFormat("class C {\n"
14427 " int f() {}\n"
14428 "};",
14429 MergeEmptyOnly);
14430 }
14431
14432 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
14433 FormatStyle MergeInlineOnly = getLLVMStyle();
14434 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14435 verifyFormat("class C {\n"
14436 " int f() { return 42; }\n"
14437 "};",
14438 MergeInlineOnly);
14439 verifyFormat("int f() {\n"
14440 " return 42;\n"
14441 "}",
14442 MergeInlineOnly);
14443
14444 // SFS_Inline implies SFS_Empty
14445 verifyFormat("class C {\n"
14446 " int f() {}\n"
14447 "};",
14448 MergeInlineOnly);
14449 verifyFormat("int f() {}", MergeInlineOnly);
14450 // https://llvm.org/PR54147
14451 verifyFormat("auto lambda = []() {\n"
14452 " // comment\n"
14453 " f();\n"
14454 " g();\n"
14455 "};",
14456 MergeInlineOnly);
14457
14458 verifyFormat("class C {\n"
14459 "#ifdef A\n"
14460 " int f() { return 42; }\n"
14461 "#endif\n"
14462 "};",
14463 MergeInlineOnly);
14464
14465 verifyFormat("struct S {\n"
14466 "// comment\n"
14467 "#ifdef FOO\n"
14468 " int foo() { bar(); }\n"
14469 "#endif\n"
14470 "};",
14471 MergeInlineOnly);
14472
14473 // Also verify behavior when BraceWrapping.AfterFunction = true
14474 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14475 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14476 verifyFormat("class C {\n"
14477 " int f() { return 42; }\n"
14478 "};",
14479 MergeInlineOnly);
14480 verifyFormat("int f()\n"
14481 "{\n"
14482 " return 42;\n"
14483 "}",
14484 MergeInlineOnly);
14485
14486 // SFS_Inline implies SFS_Empty
14487 verifyFormat("int f() {}", MergeInlineOnly);
14488 verifyFormat("class C {\n"
14489 " int f() {}\n"
14490 "};",
14491 MergeInlineOnly);
14492
14493 MergeInlineOnly.BraceWrapping.AfterClass = true;
14494 MergeInlineOnly.BraceWrapping.AfterStruct = true;
14495 verifyFormat("class C\n"
14496 "{\n"
14497 " int f() { return 42; }\n"
14498 "};",
14499 MergeInlineOnly);
14500 verifyFormat("struct C\n"
14501 "{\n"
14502 " int f() { return 42; }\n"
14503 "};",
14504 MergeInlineOnly);
14505 verifyFormat("int f()\n"
14506 "{\n"
14507 " return 42;\n"
14508 "}",
14509 MergeInlineOnly);
14510 verifyFormat("int f() {}", MergeInlineOnly);
14511 verifyFormat("class C\n"
14512 "{\n"
14513 " int f() { return 42; }\n"
14514 "};",
14515 MergeInlineOnly);
14516 verifyFormat("struct C\n"
14517 "{\n"
14518 " int f() { return 42; }\n"
14519 "};",
14520 MergeInlineOnly);
14521 verifyFormat("struct C\n"
14522 "// comment\n"
14523 "/* comment */\n"
14524 "// comment\n"
14525 "{\n"
14526 " int f() { return 42; }\n"
14527 "};",
14528 MergeInlineOnly);
14529 verifyFormat("/* comment */ struct C\n"
14530 "{\n"
14531 " int f() { return 42; }\n"
14532 "};",
14533 MergeInlineOnly);
14534 }
14535
14536 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14537 FormatStyle MergeInlineOnly = getLLVMStyle();
14538 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14539 FormatStyle::SFS_InlineOnly;
14540 verifyFormat("class C {\n"
14541 " int f() { return 42; }\n"
14542 "};",
14543 MergeInlineOnly);
14544 verifyFormat("int f() {\n"
14545 " return 42;\n"
14546 "}",
14547 MergeInlineOnly);
14548
14549 // SFS_InlineOnly does not imply SFS_Empty
14550 verifyFormat("class C {\n"
14551 " int f() {}\n"
14552 "};",
14553 MergeInlineOnly);
14554 verifyFormat("int f() {\n"
14555 "}",
14556 MergeInlineOnly);
14557
14558 // Also verify behavior when BraceWrapping.AfterFunction = true
14559 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14560 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14561 verifyFormat("class C {\n"
14562 " int f() { return 42; }\n"
14563 "};",
14564 MergeInlineOnly);
14565 verifyFormat("int f()\n"
14566 "{\n"
14567 " return 42;\n"
14568 "}",
14569 MergeInlineOnly);
14570
14571 // SFS_InlineOnly does not imply SFS_Empty
14572 verifyFormat("int f()\n"
14573 "{\n"
14574 "}",
14575 MergeInlineOnly);
14576 verifyFormat("class C {\n"
14577 " int f() {}\n"
14578 "};",
14579 MergeInlineOnly);
14580 }
14581
14582 TEST_F(FormatTest, SplitEmptyFunction) {
14583 FormatStyle Style = getLLVMStyleWithColumns(40);
14584 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14585 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14586 Style.BraceWrapping.AfterFunction = true;
14587 Style.BraceWrapping.SplitEmptyFunction = false;
14588
14589 verifyFormat("int f()\n"
14590 "{}",
14591 Style);
14592 verifyFormat("int f()\n"
14593 "{\n"
14594 " return 42;\n"
14595 "}",
14596 Style);
14597 verifyFormat("int f()\n"
14598 "{\n"
14599 " // some comment\n"
14600 "}",
14601 Style);
14602
14603 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14604 verifyFormat("int f() {}", Style);
14605 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14606 "{}",
14607 Style);
14608 verifyFormat("int f()\n"
14609 "{\n"
14610 " return 0;\n"
14611 "}",
14612 Style);
14613
14614 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14615 verifyFormat("class Foo {\n"
14616 " int f() {}\n"
14617 "};",
14618 Style);
14619 verifyFormat("class Foo {\n"
14620 " int f() { return 0; }\n"
14621 "};",
14622 Style);
14623 verifyFormat("class Foo {\n"
14624 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14625 " {}\n"
14626 "};",
14627 Style);
14628 verifyFormat("class Foo {\n"
14629 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14630 " {\n"
14631 " return 0;\n"
14632 " }\n"
14633 "};",
14634 Style);
14635
14636 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14637 verifyFormat("int f() {}", Style);
14638 verifyFormat("int f() { return 0; }", Style);
14639 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14640 "{}",
14641 Style);
14642 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14643 "{\n"
14644 " return 0;\n"
14645 "}",
14646 Style);
14647 }
14648
14649 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
14650 FormatStyle Style = getLLVMStyleWithColumns(40);
14651 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14652 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14653 Style.BraceWrapping.AfterFunction = true;
14654 Style.BraceWrapping.SplitEmptyFunction = true;
14655 Style.BraceWrapping.SplitEmptyRecord = false;
14656
14657 verifyFormat("class C {};", Style);
14658 verifyFormat("struct C {};", Style);
14659 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14660 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14661 "{\n"
14662 "}",
14663 Style);
14664 verifyFormat("class C {\n"
14665 " C()\n"
14666 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14667 " bbbbbbbbbbbbbbbbbbb()\n"
14668 " {\n"
14669 " }\n"
14670 " void\n"
14671 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14672 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14673 " {\n"
14674 " }\n"
14675 "};",
14676 Style);
14677 }
14678
14679 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
14680 FormatStyle Style = getLLVMStyle();
14681 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14682 verifyFormat("#ifdef A\n"
14683 "int f() {}\n"
14684 "#else\n"
14685 "int g() {}\n"
14686 "#endif",
14687 Style);
14688 }
14689
14690 TEST_F(FormatTest, SplitEmptyClass) {
14691 FormatStyle Style = getLLVMStyle();
14692 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14693 Style.BraceWrapping.AfterClass = true;
14694 Style.BraceWrapping.SplitEmptyRecord = false;
14695
14696 verifyFormat("class Foo\n"
14697 "{};",
14698 Style);
14699 verifyFormat("/* something */ class Foo\n"
14700 "{};",
14701 Style);
14702 verifyFormat("template <typename X> class Foo\n"
14703 "{};",
14704 Style);
14705 verifyFormat("class Foo\n"
14706 "{\n"
14707 " Foo();\n"
14708 "};",
14709 Style);
14710 verifyFormat("typedef class Foo\n"
14711 "{\n"
14712 "} Foo_t;",
14713 Style);
14714
14715 Style.BraceWrapping.SplitEmptyRecord = true;
14716 Style.BraceWrapping.AfterStruct = true;
14717 verifyFormat("class rep\n"
14718 "{\n"
14719 "};",
14720 Style);
14721 verifyFormat("struct rep\n"
14722 "{\n"
14723 "};",
14724 Style);
14725 verifyFormat("template <typename T> class rep\n"
14726 "{\n"
14727 "};",
14728 Style);
14729 verifyFormat("template <typename T> struct rep\n"
14730 "{\n"
14731 "};",
14732 Style);
14733 verifyFormat("class rep\n"
14734 "{\n"
14735 " int x;\n"
14736 "};",
14737 Style);
14738 verifyFormat("struct rep\n"
14739 "{\n"
14740 " int x;\n"
14741 "};",
14742 Style);
14743 verifyFormat("template <typename T> class rep\n"
14744 "{\n"
14745 " int x;\n"
14746 "};",
14747 Style);
14748 verifyFormat("template <typename T> struct rep\n"
14749 "{\n"
14750 " int x;\n"
14751 "};",
14752 Style);
14753 verifyFormat("template <typename T> class rep // Foo\n"
14754 "{\n"
14755 " int x;\n"
14756 "};",
14757 Style);
14758 verifyFormat("template <typename T> struct rep // Bar\n"
14759 "{\n"
14760 " int x;\n"
14761 "};",
14762 Style);
14763
14764 verifyFormat("template <typename T> class rep<T>\n"
14765 "{\n"
14766 " int x;\n"
14767 "};",
14768 Style);
14769
14770 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14771 "{\n"
14772 " int x;\n"
14773 "};",
14774 Style);
14775 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14776 "{\n"
14777 "};",
14778 Style);
14779
14780 verifyFormat("#include \"stdint.h\"\n"
14781 "namespace rep {}",
14782 Style);
14783 verifyFormat("#include <stdint.h>\n"
14784 "namespace rep {}",
14785 Style);
14786 verifyFormat("#include <stdint.h>\n"
14787 "namespace rep {}",
14788 "#include <stdint.h>\n"
14789 "namespace rep {\n"
14790 "\n"
14791 "\n"
14792 "}",
14793 Style);
14794 }
14795
14796 TEST_F(FormatTest, SplitEmptyStruct) {
14797 FormatStyle Style = getLLVMStyle();
14798 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14799 Style.BraceWrapping.AfterStruct = true;
14800 Style.BraceWrapping.SplitEmptyRecord = false;
14801
14802 verifyFormat("struct Foo\n"
14803 "{};",
14804 Style);
14805 verifyFormat("/* something */ struct Foo\n"
14806 "{};",
14807 Style);
14808 verifyFormat("template <typename X> struct Foo\n"
14809 "{};",
14810 Style);
14811 verifyFormat("struct Foo\n"
14812 "{\n"
14813 " Foo();\n"
14814 "};",
14815 Style);
14816 verifyFormat("typedef struct Foo\n"
14817 "{\n"
14818 "} Foo_t;",
14819 Style);
14820 // typedef struct Bar {} Bar_t;
14821 }
14822
14823 TEST_F(FormatTest, SplitEmptyUnion) {
14824 FormatStyle Style = getLLVMStyle();
14825 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14826 Style.BraceWrapping.AfterUnion = true;
14827 Style.BraceWrapping.SplitEmptyRecord = false;
14828
14829 verifyFormat("union Foo\n"
14830 "{};",
14831 Style);
14832 verifyFormat("/* something */ union Foo\n"
14833 "{};",
14834 Style);
14835 verifyFormat("union Foo\n"
14836 "{\n"
14837 " A,\n"
14838 "};",
14839 Style);
14840 verifyFormat("typedef union Foo\n"
14841 "{\n"
14842 "} Foo_t;",
14843 Style);
14844 }
14845
14846 TEST_F(FormatTest, SplitEmptyNamespace) {
14847 FormatStyle Style = getLLVMStyle();
14848 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14849 Style.BraceWrapping.AfterNamespace = true;
14850 Style.BraceWrapping.SplitEmptyNamespace = false;
14851
14852 verifyFormat("namespace Foo\n"
14853 "{};",
14854 Style);
14855 verifyFormat("/* something */ namespace Foo\n"
14856 "{};",
14857 Style);
14858 verifyFormat("inline namespace Foo\n"
14859 "{};",
14860 Style);
14861 verifyFormat("/* something */ inline namespace Foo\n"
14862 "{};",
14863 Style);
14864 verifyFormat("export namespace Foo\n"
14865 "{};",
14866 Style);
14867 verifyFormat("namespace Foo\n"
14868 "{\n"
14869 "void Bar();\n"
14870 "};",
14871 Style);
14872 }
14873
14874 TEST_F(FormatTest, NeverMergeShortRecords) {
14875 FormatStyle Style = getLLVMStyle();
14876
14877 verifyFormat("class Foo {\n"
14878 " Foo();\n"
14879 "};",
14880 Style);
14881 verifyFormat("typedef class Foo {\n"
14882 " Foo();\n"
14883 "} Foo_t;",
14884 Style);
14885 verifyFormat("struct Foo {\n"
14886 " Foo();\n"
14887 "};",
14888 Style);
14889 verifyFormat("typedef struct Foo {\n"
14890 " Foo();\n"
14891 "} Foo_t;",
14892 Style);
14893 verifyFormat("union Foo {\n"
14894 " A,\n"
14895 "};",
14896 Style);
14897 verifyFormat("typedef union Foo {\n"
14898 " A,\n"
14899 "} Foo_t;",
14900 Style);
14901 verifyFormat("namespace Foo {\n"
14902 "void Bar();\n"
14903 "};",
14904 Style);
14905
14906 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14907 Style.BraceWrapping.AfterClass = true;
14908 Style.BraceWrapping.AfterStruct = true;
14909 Style.BraceWrapping.AfterUnion = true;
14910 Style.BraceWrapping.AfterNamespace = true;
14911 verifyFormat("class Foo\n"
14912 "{\n"
14913 " Foo();\n"
14914 "};",
14915 Style);
14916 verifyFormat("typedef class Foo\n"
14917 "{\n"
14918 " Foo();\n"
14919 "} Foo_t;",
14920 Style);
14921 verifyFormat("struct Foo\n"
14922 "{\n"
14923 " Foo();\n"
14924 "};",
14925 Style);
14926 verifyFormat("typedef struct Foo\n"
14927 "{\n"
14928 " Foo();\n"
14929 "} Foo_t;",
14930 Style);
14931 verifyFormat("union Foo\n"
14932 "{\n"
14933 " A,\n"
14934 "};",
14935 Style);
14936 verifyFormat("typedef union Foo\n"
14937 "{\n"
14938 " A,\n"
14939 "} Foo_t;",
14940 Style);
14941 verifyFormat("namespace Foo\n"
14942 "{\n"
14943 "void Bar();\n"
14944 "};",
14945 Style);
14946 }
14947
14948 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
14949 // Elaborate type variable declarations.
14950 verifyFormat("struct foo a = {bar};\nint n;");
14951 verifyFormat("class foo a = {bar};\nint n;");
14952 verifyFormat("union foo a = {bar};\nint n;");
14953
14954 // Elaborate types inside function definitions.
14955 verifyFormat("struct foo f() {}\nint n;");
14956 verifyFormat("class foo f() {}\nint n;");
14957 verifyFormat("union foo f() {}\nint n;");
14958
14959 // Templates.
14960 verifyFormat("template <class X> void f() {}\nint n;");
14961 verifyFormat("template <struct X> void f() {}\nint n;");
14962 verifyFormat("template <union X> void f() {}\nint n;");
14963
14964 // Actual definitions...
14965 verifyFormat("struct {\n} n;");
14966 verifyFormat(
14967 "template <template <class T, class Y>, class Z> class X {\n} n;");
14968 verifyFormat("union Z {\n int n;\n} x;");
14969 verifyFormat("class MACRO Z {\n} n;");
14970 verifyFormat("class MACRO(X) Z {\n} n;");
14971 verifyFormat("class __attribute__((X)) Z {\n} n;");
14972 verifyFormat("class __declspec(X) Z {\n} n;");
14973 verifyFormat("class A##B##C {\n} n;");
14974 verifyFormat("class alignas(16) Z {\n} n;");
14975 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
14976 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
14977
14978 // Redefinition from nested context:
14979 verifyFormat("class A::B::C {\n} n;");
14980
14981 // Template definitions.
14982 verifyFormat(
14983 "template <typename F>\n"
14984 "Matcher(const Matcher<F> &Other,\n"
14985 " typename enable_if_c<is_base_of<F, T>::value &&\n"
14986 " !is_same<F, T>::value>::type * = 0)\n"
14987 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
14988
14989 // FIXME: This is still incorrectly handled at the formatter side.
14990 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
14991 verifyFormat("int i = SomeFunction(a<b, a> b);");
14992
14993 verifyFormat("class A<int> f() {}\n"
14994 "int n;");
14995 verifyFormat("template <typename T> class A<T> f() {}\n"
14996 "int n;");
14997
14998 verifyFormat("template <> class Foo<int> F() {\n"
14999 "} n;");
15000
15001 // Elaborate types where incorrectly parsing the structural element would
15002 // break the indent.
15003 verifyFormat("if (true)\n"
15004 " class X x;\n"
15005 "else\n"
15006 " f();");
15007
15008 // This is simply incomplete. Formatting is not important, but must not crash.
15009 verifyFormat("class A:");
15010 }
15011
15012 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
15013 verifyNoChange("#error Leave all white!!!!! space* alone!");
15014 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15015 verifyFormat("#error 1", " # error 1");
15016 verifyFormat("#warning 1", " # warning 1");
15017 }
15018
15019 TEST_F(FormatTest, FormatHashIfExpressions) {
15020 verifyFormat("#if AAAA && BBBB");
15021 verifyFormat("#if (AAAA && BBBB)");
15022 verifyFormat("#elif (AAAA && BBBB)");
15023 // FIXME: Come up with a better indentation for #elif.
15024 verifyFormat(
15025 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15026 " defined(BBBBBBBB)\n"
15027 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15028 " defined(BBBBBBBB)\n"
15029 "#endif",
15030 getLLVMStyleWithColumns(65));
15031 }
15032
15033 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
15034 FormatStyle AllowsMergedIf = getGoogleStyle();
15035 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
15036 FormatStyle::SIS_WithoutElse;
15037 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
15038 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
15039 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
15040 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
15041 FormatStyle ShortMergedIf = AllowsMergedIf;
15042 ShortMergedIf.ColumnLimit = 25;
15043 verifyFormat("#define A \\\n"
15044 " if (true) return 42;",
15045 ShortMergedIf);
15046 verifyFormat("#define A \\\n"
15047 " f(); \\\n"
15048 " if (true)\n"
15049 "#define B",
15050 ShortMergedIf);
15051 verifyFormat("#define A \\\n"
15052 " f(); \\\n"
15053 " if (true)\n"
15054 "g();",
15055 ShortMergedIf);
15056 verifyFormat("{\n"
15057 "#ifdef A\n"
15058 " // Comment\n"
15059 " if (true) continue;\n"
15060 "#endif\n"
15061 " // Comment\n"
15062 " if (true) continue;\n"
15063 "}",
15064 ShortMergedIf);
15065 ShortMergedIf.ColumnLimit = 33;
15066 verifyFormat("#define A \\\n"
15067 " if constexpr (true) return 42;",
15068 ShortMergedIf);
15069 verifyFormat("#define A \\\n"
15070 " if CONSTEXPR (true) return 42;",
15071 ShortMergedIf);
15072 ShortMergedIf.ColumnLimit = 29;
15073 verifyFormat("#define A \\\n"
15074 " if (aaaaaaaaaa) return 1; \\\n"
15075 " return 2;",
15076 ShortMergedIf);
15077 ShortMergedIf.ColumnLimit = 28;
15078 verifyFormat("#define A \\\n"
15079 " if (aaaaaaaaaa) \\\n"
15080 " return 1; \\\n"
15081 " return 2;",
15082 ShortMergedIf);
15083 verifyFormat("#define A \\\n"
15084 " if constexpr (aaaaaaa) \\\n"
15085 " return 1; \\\n"
15086 " return 2;",
15087 ShortMergedIf);
15088 verifyFormat("#define A \\\n"
15089 " if CONSTEXPR (aaaaaaa) \\\n"
15090 " return 1; \\\n"
15091 " return 2;",
15092 ShortMergedIf);
15093
15094 verifyFormat("//\n"
15095 "#define a \\\n"
15096 " if \\\n"
15097 " 0",
15098 getChromiumStyle(FormatStyle::LK_Cpp));
15099 }
15100
15101 TEST_F(FormatTest, FormatStarDependingOnContext) {
15102 verifyFormat("void f(int *a);");
15103 verifyFormat("void f() { f(fint * b); }");
15104 verifyFormat("class A {\n void f(int *a);\n};");
15105 verifyFormat("class A {\n int *a;\n};");
15106 verifyFormat("namespace a {\n"
15107 "namespace b {\n"
15108 "class A {\n"
15109 " void f() {}\n"
15110 " int *a;\n"
15111 "};\n"
15112 "} // namespace b\n"
15113 "} // namespace a");
15114 }
15115
15116 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15117 verifyFormat("while");
15118 verifyFormat("operator");
15119 }
15120
15121 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15122 // This code would be painfully slow to format if we didn't skip it.
15123 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
15124 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15125 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15126 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15127 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15128 "A(1, 1)\n"
15129 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15130 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15131 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15132 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15133 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15134 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15135 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15136 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15137 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15138 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15139 // Deeply nested part is untouched, rest is formatted.
15140 EXPECT_EQ(std::string("int i;") + Code + "int j;",
15141 format(std::string("int i;") + Code + "int j;",
15142 getLLVMStyle(), SC_ExpectIncomplete));
15143 }
15144
15145 //===----------------------------------------------------------------------===//
15146 // Objective-C tests.
15147 //===----------------------------------------------------------------------===//
15148
15149 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15150 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15151 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15152 "-(NSUInteger)indexOfObject:(id)anObject;");
15153 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15154 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15155 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15156 "-(NSInteger)Method3:(id)anObject;");
15157 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15158 "-(NSInteger)Method4:(id)anObject;");
15159 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15160 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15161 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15162 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15163 "forAllCells:(BOOL)flag;");
15164
15165 // Very long objectiveC method declaration.
15166 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15167 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15168 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15169 " inRange:(NSRange)range\n"
15170 " outRange:(NSRange)out_range\n"
15171 " outRange1:(NSRange)out_range1\n"
15172 " outRange2:(NSRange)out_range2\n"
15173 " outRange3:(NSRange)out_range3\n"
15174 " outRange4:(NSRange)out_range4\n"
15175 " outRange5:(NSRange)out_range5\n"
15176 " outRange6:(NSRange)out_range6\n"
15177 " outRange7:(NSRange)out_range7\n"
15178 " outRange8:(NSRange)out_range8\n"
15179 " outRange9:(NSRange)out_range9;");
15180
15181 // When the function name has to be wrapped.
15182 FormatStyle Style = getLLVMStyle();
15183 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15184 // and always indents instead.
15185 Style.IndentWrappedFunctionNames = false;
15186 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15187 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15188 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15189 "}",
15190 Style);
15191 Style.IndentWrappedFunctionNames = true;
15192 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15193 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15194 " anotherName:(NSString)dddddddddddddd {\n"
15195 "}",
15196 Style);
15197
15198 verifyFormat("- (int)sum:(vector<int>)numbers;");
15199 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15200 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15201 // protocol lists (but not for template classes):
15202 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15203
15204 verifyFormat("- (int (*)())foo:(int (*)())f;");
15205 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15206
15207 // If there's no return type (very rare in practice!), LLVM and Google style
15208 // agree.
15209 verifyFormat("- foo;");
15210 verifyFormat("- foo:(int)f;");
15211 verifyGoogleFormat("- foo:(int)foo;");
15212 }
15213
15214 TEST_F(FormatTest, BreaksStringLiterals) {
15215 // FIXME: unstable test case
15216 EXPECT_EQ("\"some text \"\n"
15217 "\"other\";",
15218 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15219 // FIXME: unstable test case
15220 EXPECT_EQ("\"some text \"\n"
15221 "\"other\";",
15222 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15223 verifyFormat("#define A \\\n"
15224 " \"some \" \\\n"
15225 " \"text \" \\\n"
15226 " \"other\";",
15227 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15228 verifyFormat("#define A \\\n"
15229 " \"so \" \\\n"
15230 " \"text \" \\\n"
15231 " \"other\";",
15232 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15233
15234 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15235 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15236 // FIXME: unstable test case
15237 EXPECT_EQ("\"some \"\n"
15238 "\"text\"",
15239 format("\"some text\"", getLLVMStyleWithColumns(10)));
15240 // FIXME: unstable test case
15241 EXPECT_EQ("\"some \"\n"
15242 "\"text\"",
15243 format("\"some text\"", getLLVMStyleWithColumns(7)));
15244 // FIXME: unstable test case
15245 EXPECT_EQ("\"some\"\n"
15246 "\" tex\"\n"
15247 "\"t\"",
15248 format("\"some text\"", getLLVMStyleWithColumns(6)));
15249 // FIXME: unstable test case
15250 EXPECT_EQ("\"some\"\n"
15251 "\" tex\"\n"
15252 "\" and\"",
15253 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15254 // FIXME: unstable test case
15255 EXPECT_EQ("\"some\"\n"
15256 "\"/tex\"\n"
15257 "\"/and\"",
15258 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15259
15260 verifyFormat("variable =\n"
15261 " \"long string \"\n"
15262 " \"literal\";",
15263 "variable = \"long string literal\";",
15264 getLLVMStyleWithColumns(20));
15265
15266 verifyFormat("variable = f(\n"
15267 " \"long string \"\n"
15268 " \"literal\",\n"
15269 " short,\n"
15270 " loooooooooooooooooooong);",
15271 "variable = f(\"long string literal\", short, "
15272 "loooooooooooooooooooong);",
15273 getLLVMStyleWithColumns(20));
15274
15275 verifyFormat("f(g(\"long string \"\n"
15276 " \"literal\"),\n"
15277 " b);",
15278 "f(g(\"long string literal\"), b);",
15279 getLLVMStyleWithColumns(20));
15280 verifyFormat("f(g(\"long string \"\n"
15281 " \"literal\",\n"
15282 " a),\n"
15283 " b);",
15284 "f(g(\"long string literal\", a), b);",
15285 getLLVMStyleWithColumns(20));
15286 verifyFormat("f(\"one two\".split(\n"
15287 " variable));",
15288 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15289 verifyFormat("f(\"one two three four five six \"\n"
15290 " \"seven\".split(\n"
15291 " really_looooong_variable));",
15292 "f(\"one two three four five six seven\"."
15293 "split(really_looooong_variable));",
15294 getLLVMStyleWithColumns(33));
15295
15296 verifyFormat("f(\"some \"\n"
15297 " \"text\",\n"
15298 " other);",
15299 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15300
15301 // Only break as a last resort.
15302 verifyFormat(
15303 "aaaaaaaaaaaaaaaaaaaa(\n"
15304 " aaaaaaaaaaaaaaaaaaaa,\n"
15305 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15306
15307 // FIXME: unstable test case
15308 EXPECT_EQ("\"splitmea\"\n"
15309 "\"trandomp\"\n"
15310 "\"oint\"",
15311 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15312
15313 // FIXME: unstable test case
15314 EXPECT_EQ("\"split/\"\n"
15315 "\"pathat/\"\n"
15316 "\"slashes\"",
15317 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15318
15319 // FIXME: unstable test case
15320 EXPECT_EQ("\"split/\"\n"
15321 "\"pathat/\"\n"
15322 "\"slashes\"",
15323 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15324 // FIXME: unstable test case
15325 EXPECT_EQ("\"split at \"\n"
15326 "\"spaces/at/\"\n"
15327 "\"slashes.at.any$\"\n"
15328 "\"non-alphanumeric%\"\n"
15329 "\"1111111111characte\"\n"
15330 "\"rs\"",
15331 format("\"split at "
15332 "spaces/at/"
15333 "slashes.at."
15334 "any$non-"
15335 "alphanumeric%"
15336 "1111111111characte"
15337 "rs\"",
15338 getLLVMStyleWithColumns(20)));
15339
15340 // Verify that splitting the strings understands
15341 // Style::AlwaysBreakBeforeMultilineStrings.
15342 verifyFormat("aaaaaaaaaaaa(\n"
15343 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15344 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15345 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15346 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15347 "aaaaaaaaaaaaaaaaaaaaaa\");",
15348 getGoogleStyle());
15349 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15350 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15351 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15352 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15353 "aaaaaaaaaaaaaaaaaaaaaa\";",
15354 getGoogleStyle());
15355 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15356 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15357 "llvm::outs() << "
15358 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15359 "aaaaaaaaaaaaaaaaaaa\";");
15360 verifyFormat("ffff(\n"
15361 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15362 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15363 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15364 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15365 getGoogleStyle());
15366
15367 FormatStyle Style = getLLVMStyleWithColumns(12);
15368 Style.BreakStringLiterals = false;
15369 verifyFormat("\"some text other\";", Style);
15370
15371 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
15372 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15373 verifyFormat("#define A \\\n"
15374 " \"some \" \\\n"
15375 " \"text \" \\\n"
15376 " \"other\";",
15377 "#define A \"some text other\";", AlignLeft);
15378 }
15379
15380 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
15381 verifyFormat("C a = \"some more \"\n"
15382 " \"text\";",
15383 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15384 }
15385
15386 TEST_F(FormatTest, FullyRemoveEmptyLines) {
15387 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
15388 NoEmptyLines.MaxEmptyLinesToKeep = 0;
15389 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15390 NoEmptyLines);
15391 }
15392
15393 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
15394 // FIXME: unstable test case
15395 EXPECT_EQ(
15396 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15397 "(\n"
15398 " \"x\t\");",
15399 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15400 "aaaaaaa("
15401 "\"x\t\");"));
15402 }
15403
15404 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
15405 // FIXME: unstable test case
15406 EXPECT_EQ(
15407 "u8\"utf8 string \"\n"
15408 "u8\"literal\";",
15409 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15410 // FIXME: unstable test case
15411 EXPECT_EQ(
15412 "u\"utf16 string \"\n"
15413 "u\"literal\";",
15414 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15415 // FIXME: unstable test case
15416 EXPECT_EQ(
15417 "U\"utf32 string \"\n"
15418 "U\"literal\";",
15419 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15420 // FIXME: unstable test case
15421 EXPECT_EQ("L\"wide string \"\n"
15422 "L\"literal\";",
15423 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15424 verifyFormat("@\"NSString \"\n"
15425 "@\"literal\";",
15426 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15427 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
15428
15429 // This input makes clang-format try to split the incomplete unicode escape
15430 // sequence, which used to lead to a crasher.
15431 verifyNoCrash(
15432 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15433 getLLVMStyleWithColumns(60));
15434 }
15435
15436 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
15437 FormatStyle Style = getGoogleStyleWithColumns(15);
15438 verifyFormat("R\"x(raw literal)x\";", Style);
15439 verifyFormat("uR\"x(raw literal)x\";", Style);
15440 verifyFormat("LR\"x(raw literal)x\";", Style);
15441 verifyFormat("UR\"x(raw literal)x\";", Style);
15442 verifyFormat("u8R\"x(raw literal)x\";", Style);
15443 }
15444
15445 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
15446 FormatStyle Style = getLLVMStyleWithColumns(20);
15447 // FIXME: unstable test case
15448 EXPECT_EQ(
15449 "_T(\"aaaaaaaaaaaaaa\")\n"
15450 "_T(\"aaaaaaaaaaaaaa\")\n"
15451 "_T(\"aaaaaaaaaaaa\")",
15452 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
15453 verifyFormat("f(x,\n"
15454 " _T(\"aaaaaaaaaaaa\")\n"
15455 " _T(\"aaa\"),\n"
15456 " z);",
15457 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
15458
15459 // FIXME: Handle embedded spaces in one iteration.
15460 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15461 // "_T(\"aaaaaaaaaaaaa\")\n"
15462 // "_T(\"aaaaaaaaaaaaa\")\n"
15463 // "_T(\"a\")",
15464 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15465 // getLLVMStyleWithColumns(20)));
15466 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15467 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
15468 verifyFormat("f(\n"
15469 "#if !TEST\n"
15470 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15471 "#endif\n"
15472 ");",
15473 "f(\n"
15474 "#if !TEST\n"
15475 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15476 "#endif\n"
15477 ");");
15478 verifyFormat("f(\n"
15479 "\n"
15480 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15481 "f(\n"
15482 "\n"
15483 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15484 // Regression test for accessing tokens past the end of a vector in the
15485 // TokenLexer.
15486 verifyNoCrash(R"(_T(
15487 "
15488 )
15489 )");
15490 }
15491
15492 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15493 // In a function call with two operands, the second can be broken with no line
15494 // break before it.
15495 verifyFormat("func(a, \"long long \"\n"
15496 " \"long long\");",
15497 "func(a, \"long long long long\");",
15498 getLLVMStyleWithColumns(24));
15499 // In a function call with three operands, the second must be broken with a
15500 // line break before it.
15501 verifyFormat("func(a,\n"
15502 " \"long long long \"\n"
15503 " \"long\",\n"
15504 " c);",
15505 "func(a, \"long long long long\", c);",
15506 getLLVMStyleWithColumns(24));
15507 // In a function call with three operands, the third must be broken with a
15508 // line break before it.
15509 verifyFormat("func(a, b,\n"
15510 " \"long long long \"\n"
15511 " \"long\");",
15512 "func(a, b, \"long long long long\");",
15513 getLLVMStyleWithColumns(24));
15514 // In a function call with three operands, both the second and the third must
15515 // be broken with a line break before them.
15516 verifyFormat("func(a,\n"
15517 " \"long long long \"\n"
15518 " \"long\",\n"
15519 " \"long long long \"\n"
15520 " \"long\");",
15521 "func(a, \"long long long long\", \"long long long long\");",
15522 getLLVMStyleWithColumns(24));
15523 // In a chain of << with two operands, the second can be broken with no line
15524 // break before it.
15525 verifyFormat("a << \"line line \"\n"
15526 " \"line\";",
15527 "a << \"line line line\";", getLLVMStyleWithColumns(20));
15528 // In a chain of << with three operands, the second can be broken with no line
15529 // break before it.
15530 verifyFormat("abcde << \"line \"\n"
15531 " \"line line\"\n"
15532 " << c;",
15533 "abcde << \"line line line\" << c;",
15534 getLLVMStyleWithColumns(20));
15535 // In a chain of << with three operands, the third must be broken with a line
15536 // break before it.
15537 verifyFormat("a << b\n"
15538 " << \"line line \"\n"
15539 " \"line\";",
15540 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15541 // In a chain of << with three operands, the second can be broken with no line
15542 // break before it and the third must be broken with a line break before it.
15543 verifyFormat("abcd << \"line line \"\n"
15544 " \"line\"\n"
15545 " << \"line line \"\n"
15546 " \"line\";",
15547 "abcd << \"line line line\" << \"line line line\";",
15548 getLLVMStyleWithColumns(20));
15549 // In a chain of binary operators with two operands, the second can be broken
15550 // with no line break before it.
15551 verifyFormat("abcd + \"line line \"\n"
15552 " \"line line\";",
15553 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15554 // In a chain of binary operators with three operands, the second must be
15555 // broken with a line break before it.
15556 verifyFormat("abcd +\n"
15557 " \"line line \"\n"
15558 " \"line line\" +\n"
15559 " e;",
15560 "abcd + \"line line line line\" + e;",
15561 getLLVMStyleWithColumns(20));
15562 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15563 // the first must be broken with a line break before it.
15564 FormatStyle Style = getLLVMStyleWithColumns(25);
15565 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15566 verifyFormat("someFunction(\n"
15567 " \"long long long \"\n"
15568 " \"long\",\n"
15569 " a);",
15570 "someFunction(\"long long long long\", a);", Style);
15571 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15572 verifyFormat("someFunction(\n"
15573 " \"long long long \"\n"
15574 " \"long\",\n"
15575 " a\n"
15576 ");",
15577 Style);
15578 }
15579
15580 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15581 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15584 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
15587 }
15588
15589 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15590 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
15591 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
15592 verifyFormat("fffffffffff(g(R\"x(\n"
15593 "multiline raw string literal xxxxxxxxxxxxxx\n"
15594 ")x\",\n"
15595 " a),\n"
15596 " b);",
15597 "fffffffffff(g(R\"x(\n"
15598 "multiline raw string literal xxxxxxxxxxxxxx\n"
15599 ")x\", a), b);",
15600 getGoogleStyleWithColumns(20));
15601 verifyFormat("fffffffffff(\n"
15602 " g(R\"x(qqq\n"
15603 "multiline raw string literal xxxxxxxxxxxxxx\n"
15604 ")x\",\n"
15605 " a),\n"
15606 " b);",
15607 "fffffffffff(g(R\"x(qqq\n"
15608 "multiline raw string literal xxxxxxxxxxxxxx\n"
15609 ")x\", a), b);",
15610 getGoogleStyleWithColumns(20));
15611
15612 verifyNoChange("fffffffffff(R\"x(\n"
15613 "multiline raw string literal xxxxxxxxxxxxxx\n"
15614 ")x\");",
15615 getGoogleStyleWithColumns(20));
15616 verifyFormat("fffffffffff(R\"x(\n"
15617 "multiline raw string literal xxxxxxxxxxxxxx\n"
15618 ")x\" + bbbbbb);",
15619 "fffffffffff(R\"x(\n"
15620 "multiline raw string literal xxxxxxxxxxxxxx\n"
15621 ")x\" + bbbbbb);",
15622 getGoogleStyleWithColumns(20));
15623 verifyFormat("fffffffffff(\n"
15624 " R\"x(\n"
15625 "multiline raw string literal xxxxxxxxxxxxxx\n"
15626 ")x\" +\n"
15627 " bbbbbb);",
15628 "fffffffffff(\n"
15629 " R\"x(\n"
15630 "multiline raw string literal xxxxxxxxxxxxxx\n"
15631 ")x\" + bbbbbb);",
15632 getGoogleStyleWithColumns(20));
15633 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
15634 "fffffffffff(\n"
15635 " R\"(single line raw string)\" + bbbbbb);");
15636 }
15637
15638 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15639 verifyFormat("string a = \"unterminated;");
15640 verifyFormat("function(\"unterminated,\n"
15641 " OtherParameter);",
15642 "function( \"unterminated,\n"
15643 " OtherParameter);");
15644 }
15645
15646 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15647 FormatStyle Style = getLLVMStyle();
15648 Style.Standard = FormatStyle::LS_Cpp03;
15649 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15650 "#define x(_a) printf(\"foo\"_a);", Style);
15651 }
15652
15653 TEST_F(FormatTest, CppLexVersion) {
15654 FormatStyle Style = getLLVMStyle();
15655 // Formatting of x * y differs if x is a type.
15656 verifyFormat("void foo() { MACRO(a * b); }", Style);
15657 verifyFormat("void foo() { MACRO(int *b); }", Style);
15658
15659 // LLVM style uses latest lexer.
15660 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15661 Style.Standard = FormatStyle::LS_Cpp17;
15662 // But in c++17, char8_t isn't a keyword.
15663 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15664 }
15665
15666 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15667
15668 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15669 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15670 " \"ddeeefff\");",
15671 "someFunction(\"aaabbbcccdddeeefff\");",
15672 getLLVMStyleWithColumns(25));
15673 verifyFormat("someFunction1234567890(\n"
15674 " \"aaabbbcccdddeeefff\");",
15675 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15676 getLLVMStyleWithColumns(26));
15677 verifyFormat("someFunction1234567890(\n"
15678 " \"aaabbbcccdddeeeff\"\n"
15679 " \"f\");",
15680 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15681 getLLVMStyleWithColumns(25));
15682 verifyFormat("someFunction1234567890(\n"
15683 " \"aaabbbcccdddeeeff\"\n"
15684 " \"f\");",
15685 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15686 getLLVMStyleWithColumns(24));
15687 verifyFormat("someFunction(\n"
15688 " \"aaabbbcc ddde \"\n"
15689 " \"efff\");",
15690 "someFunction(\"aaabbbcc ddde efff\");",
15691 getLLVMStyleWithColumns(25));
15692 verifyFormat("someFunction(\"aaabbbccc \"\n"
15693 " \"ddeeefff\");",
15694 "someFunction(\"aaabbbccc ddeeefff\");",
15695 getLLVMStyleWithColumns(25));
15696 verifyFormat("someFunction1234567890(\n"
15697 " \"aaabb \"\n"
15698 " \"cccdddeeefff\");",
15699 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15700 getLLVMStyleWithColumns(25));
15701 verifyFormat("#define A \\\n"
15702 " string s = \\\n"
15703 " \"123456789\" \\\n"
15704 " \"0\"; \\\n"
15705 " int i;",
15706 "#define A string s = \"1234567890\"; int i;",
15707 getLLVMStyleWithColumns(20));
15708 verifyFormat("someFunction(\n"
15709 " \"aaabbbcc \"\n"
15710 " \"dddeeefff\");",
15711 "someFunction(\"aaabbbcc dddeeefff\");",
15712 getLLVMStyleWithColumns(25));
15713 }
15714
15715 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15716 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15717 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15718 // FIXME: unstable test case
15719 EXPECT_EQ("\"test\"\n"
15720 "\"\\n\"",
15721 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15722 // FIXME: unstable test case
15723 EXPECT_EQ("\"tes\\\\\"\n"
15724 "\"n\"",
15725 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15726 // FIXME: unstable test case
15727 EXPECT_EQ("\"\\\\\\\\\"\n"
15728 "\"\\n\"",
15729 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15730 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15731 // FIXME: unstable test case
15732 EXPECT_EQ("\"\\uff01\"\n"
15733 "\"test\"",
15734 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15735 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15736 // FIXME: unstable test case
15737 EXPECT_EQ("\"\\x000000000001\"\n"
15738 "\"next\"",
15739 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15740 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15741 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15742 // FIXME: unstable test case
15743 EXPECT_EQ("\"test\"\n"
15744 "\"\\000000\"\n"
15745 "\"000001\"",
15746 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15747 // FIXME: unstable test case
15748 EXPECT_EQ("\"test\\000\"\n"
15749 "\"00000000\"\n"
15750 "\"1\"",
15751 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15752 }
15753
15754 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15755 verifyFormat("void f() {\n"
15756 " return g() {}\n"
15757 " void h() {}");
15758 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15759 "g();\n"
15760 "}");
15761 }
15762
15763 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15764 verifyFormat(
15765 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15766 }
15767
15768 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15769 verifyFormat("class X {\n"
15770 " void f() {\n"
15771 " }\n"
15772 "};",
15773 getLLVMStyleWithColumns(12));
15774 }
15775
15776 TEST_F(FormatTest, ConfigurableIndentWidth) {
15777 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15778 EightIndent.IndentWidth = 8;
15779 EightIndent.ContinuationIndentWidth = 8;
15780 verifyFormat("void f() {\n"
15781 " someFunction();\n"
15782 " if (true) {\n"
15783 " f();\n"
15784 " }\n"
15785 "}",
15786 EightIndent);
15787 verifyFormat("class X {\n"
15788 " void f() {\n"
15789 " }\n"
15790 "};",
15791 EightIndent);
15792 verifyFormat("int x[] = {\n"
15793 " call(),\n"
15794 " call()};",
15795 EightIndent);
15796 }
15797
15798 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15799 verifyFormat("double\n"
15800 "f();",
15801 getLLVMStyleWithColumns(8));
15802 }
15803
15804 TEST_F(FormatTest, ConfigurableUseOfTab) {
15805 FormatStyle Tab = getLLVMStyleWithColumns(42);
15806 Tab.IndentWidth = 8;
15807 Tab.UseTab = FormatStyle::UT_Always;
15808 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15809
15810 verifyFormat("if (aaaaaaaa && // q\n"
15811 " bb)\t\t// w\n"
15812 "\t;",
15813 "if (aaaaaaaa &&// q\n"
15814 "bb)// w\n"
15815 ";",
15816 Tab);
15817 verifyFormat("if (aaa && bbb) // w\n"
15818 "\t;",
15819 "if(aaa&&bbb)// w\n"
15820 ";",
15821 Tab);
15822
15823 verifyFormat("class X {\n"
15824 "\tvoid f() {\n"
15825 "\t\tsomeFunction(parameter1,\n"
15826 "\t\t\t parameter2);\n"
15827 "\t}\n"
15828 "};",
15829 Tab);
15830 verifyFormat("#define A \\\n"
15831 "\tvoid f() { \\\n"
15832 "\t\tsomeFunction( \\\n"
15833 "\t\t parameter1, \\\n"
15834 "\t\t parameter2); \\\n"
15835 "\t}",
15836 Tab);
15837 verifyFormat("int a;\t // x\n"
15838 "int bbbbbbbb; // x",
15839 Tab);
15840
15841 FormatStyle TabAlignment = Tab;
15842 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
15843 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15844 verifyFormat("unsigned long long big;\n"
15845 "char*\t\t ptr;",
15846 TabAlignment);
15847 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15848 verifyFormat("unsigned long long big;\n"
15849 "char *\t\t ptr;",
15850 TabAlignment);
15851 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15852 verifyFormat("unsigned long long big;\n"
15853 "char\t\t *ptr;",
15854 TabAlignment);
15855
15856 Tab.TabWidth = 4;
15857 Tab.IndentWidth = 8;
15858 verifyFormat("class TabWidth4Indent8 {\n"
15859 "\t\tvoid f() {\n"
15860 "\t\t\t\tsomeFunction(parameter1,\n"
15861 "\t\t\t\t\t\t\t parameter2);\n"
15862 "\t\t}\n"
15863 "};",
15864 Tab);
15865
15866 Tab.TabWidth = 4;
15867 Tab.IndentWidth = 4;
15868 verifyFormat("class TabWidth4Indent4 {\n"
15869 "\tvoid f() {\n"
15870 "\t\tsomeFunction(parameter1,\n"
15871 "\t\t\t\t\t parameter2);\n"
15872 "\t}\n"
15873 "};",
15874 Tab);
15875
15876 Tab.TabWidth = 8;
15877 Tab.IndentWidth = 4;
15878 verifyFormat("class TabWidth8Indent4 {\n"
15879 " void f() {\n"
15880 "\tsomeFunction(parameter1,\n"
15881 "\t\t parameter2);\n"
15882 " }\n"
15883 "};",
15884 Tab);
15885
15886 Tab.TabWidth = 8;
15887 Tab.IndentWidth = 8;
15888 verifyFormat("/*\n"
15889 "\t a\t\tcomment\n"
15890 "\t in multiple lines\n"
15891 " */",
15892 " /*\t \t \n"
15893 " \t \t a\t\tcomment\t \t\n"
15894 " \t \t in multiple lines\t\n"
15895 " \t */",
15896 Tab);
15897
15898 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
15899 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15900 verifyFormat("void f() {\n"
15901 "\tunsigned long long big;\n"
15902 "\tchar* ptr;\n"
15903 "}",
15904 TabAlignment);
15905 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15906 verifyFormat("void f() {\n"
15907 "\tunsigned long long big;\n"
15908 "\tchar * ptr;\n"
15909 "}",
15910 TabAlignment);
15911 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15912 verifyFormat("void f() {\n"
15913 "\tunsigned long long big;\n"
15914 "\tchar *ptr;\n"
15915 "}",
15916 TabAlignment);
15917
15918 Tab.UseTab = FormatStyle::UT_ForIndentation;
15919 verifyFormat("{\n"
15920 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15921 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15922 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15923 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15924 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15925 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15926 "};",
15927 Tab);
15928 verifyFormat("enum AA {\n"
15929 "\ta1, // Force multiple lines\n"
15930 "\ta2,\n"
15931 "\ta3\n"
15932 "};",
15933 Tab);
15934 verifyFormat("if (aaaaaaaa && // q\n"
15935 " bb) // w\n"
15936 "\t;",
15937 "if (aaaaaaaa &&// q\n"
15938 "bb)// w\n"
15939 ";",
15940 Tab);
15941 verifyFormat("class X {\n"
15942 "\tvoid f() {\n"
15943 "\t\tsomeFunction(parameter1,\n"
15944 "\t\t parameter2);\n"
15945 "\t}\n"
15946 "};",
15947 Tab);
15948 verifyFormat("{\n"
15949 "\tQ(\n"
15950 "\t {\n"
15951 "\t\t int a;\n"
15952 "\t\t someFunction(aaaaaaaa,\n"
15953 "\t\t bbbbbbb);\n"
15954 "\t },\n"
15955 "\t p);\n"
15956 "}",
15957 Tab);
15958 verifyFormat("{\n"
15959 "\t/* aaaa\n"
15960 "\t bbbb */\n"
15961 "}",
15962 "{\n"
15963 "/* aaaa\n"
15964 " bbbb */\n"
15965 "}",
15966 Tab);
15967 verifyFormat("{\n"
15968 "\t/*\n"
15969 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15970 "\t bbbbbbbbbbbbb\n"
15971 "\t*/\n"
15972 "}",
15973 "{\n"
15974 "/*\n"
15975 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15976 "*/\n"
15977 "}",
15978 Tab);
15979 verifyFormat("{\n"
15980 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15981 "\t// bbbbbbbbbbbbb\n"
15982 "}",
15983 "{\n"
15984 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15985 "}",
15986 Tab);
15987 verifyFormat("{\n"
15988 "\t/*\n"
15989 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15990 "\t bbbbbbbbbbbbb\n"
15991 "\t*/\n"
15992 "}",
15993 "{\n"
15994 "\t/*\n"
15995 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15996 "\t*/\n"
15997 "}",
15998 Tab);
15999 verifyNoChange("{\n"
16000 "\t/*\n"
16001 "\n"
16002 "\t*/\n"
16003 "}",
16004 Tab);
16005 verifyNoChange("{\n"
16006 "\t/*\n"
16007 " asdf\n"
16008 "\t*/\n"
16009 "}",
16010 Tab);
16011
16012 verifyFormat("void f() {\n"
16013 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16014 "\t : bbbbbbbbbbbbbbbbbb\n"
16015 "}",
16016 Tab);
16017 FormatStyle TabNoBreak = Tab;
16018 TabNoBreak.BreakBeforeTernaryOperators = false;
16019 verifyFormat("void f() {\n"
16020 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16021 "\t bbbbbbbbbbbbbbbbbb\n"
16022 "}",
16023 TabNoBreak);
16024 verifyFormat("void f() {\n"
16025 "\treturn true ?\n"
16026 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16027 "\t bbbbbbbbbbbbbbbbbbbb\n"
16028 "}",
16029 TabNoBreak);
16030
16031 Tab.UseTab = FormatStyle::UT_Never;
16032 verifyFormat("/*\n"
16033 " a\t\tcomment\n"
16034 " in multiple lines\n"
16035 " */",
16036 " /*\t \t \n"
16037 " \t \t a\t\tcomment\t \t\n"
16038 " \t \t in multiple lines\t\n"
16039 " \t */",
16040 Tab);
16041 verifyFormat("/* some\n"
16042 " comment */",
16043 " \t \t /* some\n"
16044 " \t \t comment */",
16045 Tab);
16046 verifyFormat("int a; /* some\n"
16047 " comment */",
16048 " \t \t int a; /* some\n"
16049 " \t \t comment */",
16050 Tab);
16051
16052 verifyFormat("int a; /* some\n"
16053 "comment */",
16054 " \t \t int\ta; /* some\n"
16055 " \t \t comment */",
16056 Tab);
16057 verifyFormat("f(\"\t\t\"); /* some\n"
16058 " comment */",
16059 " \t \t f(\"\t\t\"); /* some\n"
16060 " \t \t comment */",
16061 Tab);
16062 verifyFormat("{\n"
16063 " /*\n"
16064 " * Comment\n"
16065 " */\n"
16066 " int i;\n"
16067 "}",
16068 "{\n"
16069 "\t/*\n"
16070 "\t * Comment\n"
16071 "\t */\n"
16072 "\t int i;\n"
16073 "}",
16074 Tab);
16075
16076 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16077 Tab.TabWidth = 8;
16078 Tab.IndentWidth = 8;
16079 verifyFormat("if (aaaaaaaa && // q\n"
16080 " bb) // w\n"
16081 "\t;",
16082 "if (aaaaaaaa &&// q\n"
16083 "bb)// w\n"
16084 ";",
16085 Tab);
16086 verifyFormat("if (aaa && bbb) // w\n"
16087 "\t;",
16088 "if(aaa&&bbb)// w\n"
16089 ";",
16090 Tab);
16091 verifyFormat("class X {\n"
16092 "\tvoid f() {\n"
16093 "\t\tsomeFunction(parameter1,\n"
16094 "\t\t\t parameter2);\n"
16095 "\t}\n"
16096 "};",
16097 Tab);
16098 verifyFormat("#define A \\\n"
16099 "\tvoid f() { \\\n"
16100 "\t\tsomeFunction( \\\n"
16101 "\t\t parameter1, \\\n"
16102 "\t\t parameter2); \\\n"
16103 "\t}",
16104 Tab);
16105 Tab.TabWidth = 4;
16106 Tab.IndentWidth = 8;
16107 verifyFormat("class TabWidth4Indent8 {\n"
16108 "\t\tvoid f() {\n"
16109 "\t\t\t\tsomeFunction(parameter1,\n"
16110 "\t\t\t\t\t\t\t parameter2);\n"
16111 "\t\t}\n"
16112 "};",
16113 Tab);
16114 Tab.TabWidth = 4;
16115 Tab.IndentWidth = 4;
16116 verifyFormat("class TabWidth4Indent4 {\n"
16117 "\tvoid f() {\n"
16118 "\t\tsomeFunction(parameter1,\n"
16119 "\t\t\t\t\t parameter2);\n"
16120 "\t}\n"
16121 "};",
16122 Tab);
16123 Tab.TabWidth = 8;
16124 Tab.IndentWidth = 4;
16125 verifyFormat("class TabWidth8Indent4 {\n"
16126 " void f() {\n"
16127 "\tsomeFunction(parameter1,\n"
16128 "\t\t parameter2);\n"
16129 " }\n"
16130 "};",
16131 Tab);
16132 Tab.TabWidth = 8;
16133 Tab.IndentWidth = 8;
16134 verifyFormat("/*\n"
16135 "\t a\t\tcomment\n"
16136 "\t in multiple lines\n"
16137 " */",
16138 " /*\t \t \n"
16139 " \t \t a\t\tcomment\t \t\n"
16140 " \t \t in multiple lines\t\n"
16141 " \t */",
16142 Tab);
16143 verifyFormat("{\n"
16144 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16145 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16146 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16147 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16148 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16149 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16150 "};",
16151 Tab);
16152 verifyFormat("enum AA {\n"
16153 "\ta1, // Force multiple lines\n"
16154 "\ta2,\n"
16155 "\ta3\n"
16156 "};",
16157 Tab);
16158 verifyFormat("if (aaaaaaaa && // q\n"
16159 " bb) // w\n"
16160 "\t;",
16161 "if (aaaaaaaa &&// q\n"
16162 "bb)// w\n"
16163 ";",
16164 Tab);
16165 verifyFormat("class X {\n"
16166 "\tvoid f() {\n"
16167 "\t\tsomeFunction(parameter1,\n"
16168 "\t\t\t parameter2);\n"
16169 "\t}\n"
16170 "};",
16171 Tab);
16172 verifyFormat("{\n"
16173 "\tQ(\n"
16174 "\t {\n"
16175 "\t\t int a;\n"
16176 "\t\t someFunction(aaaaaaaa,\n"
16177 "\t\t\t\t bbbbbbb);\n"
16178 "\t },\n"
16179 "\t p);\n"
16180 "}",
16181 Tab);
16182 verifyFormat("{\n"
16183 "\t/* aaaa\n"
16184 "\t bbbb */\n"
16185 "}",
16186 "{\n"
16187 "/* aaaa\n"
16188 " bbbb */\n"
16189 "}",
16190 Tab);
16191 verifyFormat("{\n"
16192 "\t/*\n"
16193 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16194 "\t bbbbbbbbbbbbb\n"
16195 "\t*/\n"
16196 "}",
16197 "{\n"
16198 "/*\n"
16199 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16200 "*/\n"
16201 "}",
16202 Tab);
16203 verifyFormat("{\n"
16204 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16205 "\t// bbbbbbbbbbbbb\n"
16206 "}",
16207 "{\n"
16208 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16209 "}",
16210 Tab);
16211 verifyFormat("{\n"
16212 "\t/*\n"
16213 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16214 "\t bbbbbbbbbbbbb\n"
16215 "\t*/\n"
16216 "}",
16217 "{\n"
16218 "\t/*\n"
16219 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16220 "\t*/\n"
16221 "}",
16222 Tab);
16223 verifyNoChange("{\n"
16224 "\t/*\n"
16225 "\n"
16226 "\t*/\n"
16227 "}",
16228 Tab);
16229 verifyNoChange("{\n"
16230 "\t/*\n"
16231 " asdf\n"
16232 "\t*/\n"
16233 "}",
16234 Tab);
16235 verifyFormat("/* some\n"
16236 " comment */",
16237 " \t \t /* some\n"
16238 " \t \t comment */",
16239 Tab);
16240 verifyFormat("int a; /* some\n"
16241 " comment */",
16242 " \t \t int a; /* some\n"
16243 " \t \t comment */",
16244 Tab);
16245 verifyFormat("int a; /* some\n"
16246 "comment */",
16247 " \t \t int\ta; /* some\n"
16248 " \t \t comment */",
16249 Tab);
16250 verifyFormat("f(\"\t\t\"); /* some\n"
16251 " comment */",
16252 " \t \t f(\"\t\t\"); /* some\n"
16253 " \t \t comment */",
16254 Tab);
16255 verifyFormat("{\n"
16256 "\t/*\n"
16257 "\t * Comment\n"
16258 "\t */\n"
16259 "\tint i;\n"
16260 "}",
16261 "{\n"
16262 "\t/*\n"
16263 "\t * Comment\n"
16264 "\t */\n"
16265 "\t int i;\n"
16266 "}",
16267 Tab);
16268 Tab.TabWidth = 2;
16269 Tab.IndentWidth = 2;
16270 verifyFormat("{\n"
16271 "\t/* aaaa\n"
16272 "\t\t bbbb */\n"
16273 "}",
16274 "{\n"
16275 "/* aaaa\n"
16276 "\t bbbb */\n"
16277 "}",
16278 Tab);
16279 verifyFormat("{\n"
16280 "\t/*\n"
16281 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16282 "\t\tbbbbbbbbbbbbb\n"
16283 "\t*/\n"
16284 "}",
16285 "{\n"
16286 "/*\n"
16287 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16288 "*/\n"
16289 "}",
16290 Tab);
16291 Tab.AlignConsecutiveAssignments.Enabled = true;
16292 Tab.AlignConsecutiveDeclarations.Enabled = true;
16293 Tab.TabWidth = 4;
16294 Tab.IndentWidth = 4;
16295 verifyFormat("class Assign {\n"
16296 "\tvoid f() {\n"
16297 "\t\tint x = 123;\n"
16298 "\t\tint random = 4;\n"
16299 "\t\tstd::string alphabet =\n"
16300 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16301 "\t}\n"
16302 "};",
16303 Tab);
16304
16305 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16306 Tab.TabWidth = 8;
16307 Tab.IndentWidth = 8;
16308 verifyFormat("if (aaaaaaaa && // q\n"
16309 " bb) // w\n"
16310 "\t;",
16311 "if (aaaaaaaa &&// q\n"
16312 "bb)// w\n"
16313 ";",
16314 Tab);
16315 verifyFormat("if (aaa && bbb) // w\n"
16316 "\t;",
16317 "if(aaa&&bbb)// w\n"
16318 ";",
16319 Tab);
16320 verifyFormat("class X {\n"
16321 "\tvoid f() {\n"
16322 "\t\tsomeFunction(parameter1,\n"
16323 "\t\t parameter2);\n"
16324 "\t}\n"
16325 "};",
16326 Tab);
16327 verifyFormat("#define A \\\n"
16328 "\tvoid f() { \\\n"
16329 "\t\tsomeFunction( \\\n"
16330 "\t\t parameter1, \\\n"
16331 "\t\t parameter2); \\\n"
16332 "\t}",
16333 Tab);
16334 Tab.TabWidth = 4;
16335 Tab.IndentWidth = 8;
16336 verifyFormat("class TabWidth4Indent8 {\n"
16337 "\t\tvoid f() {\n"
16338 "\t\t\t\tsomeFunction(parameter1,\n"
16339 "\t\t\t\t parameter2);\n"
16340 "\t\t}\n"
16341 "};",
16342 Tab);
16343 Tab.TabWidth = 4;
16344 Tab.IndentWidth = 4;
16345 verifyFormat("class TabWidth4Indent4 {\n"
16346 "\tvoid f() {\n"
16347 "\t\tsomeFunction(parameter1,\n"
16348 "\t\t parameter2);\n"
16349 "\t}\n"
16350 "};",
16351 Tab);
16352 Tab.TabWidth = 8;
16353 Tab.IndentWidth = 4;
16354 verifyFormat("class TabWidth8Indent4 {\n"
16355 " void f() {\n"
16356 "\tsomeFunction(parameter1,\n"
16357 "\t parameter2);\n"
16358 " }\n"
16359 "};",
16360 Tab);
16361 Tab.TabWidth = 8;
16362 Tab.IndentWidth = 8;
16363 verifyFormat("/*\n"
16364 " a\t\tcomment\n"
16365 " in multiple lines\n"
16366 " */",
16367 " /*\t \t \n"
16368 " \t \t a\t\tcomment\t \t\n"
16369 " \t \t in multiple lines\t\n"
16370 " \t */",
16371 Tab);
16372 verifyFormat("{\n"
16373 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16374 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16375 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16376 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16377 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16378 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16379 "};",
16380 Tab);
16381 verifyFormat("enum AA {\n"
16382 "\ta1, // Force multiple lines\n"
16383 "\ta2,\n"
16384 "\ta3\n"
16385 "};",
16386 Tab);
16387 verifyFormat("if (aaaaaaaa && // q\n"
16388 " bb) // w\n"
16389 "\t;",
16390 "if (aaaaaaaa &&// q\n"
16391 "bb)// w\n"
16392 ";",
16393 Tab);
16394 verifyFormat("class X {\n"
16395 "\tvoid f() {\n"
16396 "\t\tsomeFunction(parameter1,\n"
16397 "\t\t parameter2);\n"
16398 "\t}\n"
16399 "};",
16400 Tab);
16401 verifyFormat("{\n"
16402 "\tQ(\n"
16403 "\t {\n"
16404 "\t\t int a;\n"
16405 "\t\t someFunction(aaaaaaaa,\n"
16406 "\t\t bbbbbbb);\n"
16407 "\t },\n"
16408 "\t p);\n"
16409 "}",
16410 Tab);
16411 verifyFormat("{\n"
16412 "\t/* aaaa\n"
16413 "\t bbbb */\n"
16414 "}",
16415 "{\n"
16416 "/* aaaa\n"
16417 " bbbb */\n"
16418 "}",
16419 Tab);
16420 verifyFormat("{\n"
16421 "\t/*\n"
16422 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16423 "\t bbbbbbbbbbbbb\n"
16424 "\t*/\n"
16425 "}",
16426 "{\n"
16427 "/*\n"
16428 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16429 "*/\n"
16430 "}",
16431 Tab);
16432 verifyFormat("{\n"
16433 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16434 "\t// bbbbbbbbbbbbb\n"
16435 "}",
16436 "{\n"
16437 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16438 "}",
16439 Tab);
16440 verifyFormat("{\n"
16441 "\t/*\n"
16442 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16443 "\t bbbbbbbbbbbbb\n"
16444 "\t*/\n"
16445 "}",
16446 "{\n"
16447 "\t/*\n"
16448 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16449 "\t*/\n"
16450 "}",
16451 Tab);
16452 verifyNoChange("{\n"
16453 "\t/*\n"
16454 "\n"
16455 "\t*/\n"
16456 "}",
16457 Tab);
16458 verifyNoChange("{\n"
16459 "\t/*\n"
16460 " asdf\n"
16461 "\t*/\n"
16462 "}",
16463 Tab);
16464 verifyFormat("/* some\n"
16465 " comment */",
16466 " \t \t /* some\n"
16467 " \t \t comment */",
16468 Tab);
16469 verifyFormat("int a; /* some\n"
16470 " comment */",
16471 " \t \t int a; /* some\n"
16472 " \t \t comment */",
16473 Tab);
16474 verifyFormat("int a; /* some\n"
16475 "comment */",
16476 " \t \t int\ta; /* some\n"
16477 " \t \t comment */",
16478 Tab);
16479 verifyFormat("f(\"\t\t\"); /* some\n"
16480 " comment */",
16481 " \t \t f(\"\t\t\"); /* some\n"
16482 " \t \t comment */",
16483 Tab);
16484 verifyFormat("{\n"
16485 "\t/*\n"
16486 "\t * Comment\n"
16487 "\t */\n"
16488 "\tint i;\n"
16489 "}",
16490 "{\n"
16491 "\t/*\n"
16492 "\t * Comment\n"
16493 "\t */\n"
16494 "\t int i;\n"
16495 "}",
16496 Tab);
16497 Tab.TabWidth = 2;
16498 Tab.IndentWidth = 2;
16499 verifyFormat("{\n"
16500 "\t/* aaaa\n"
16501 "\t bbbb */\n"
16502 "}",
16503 "{\n"
16504 "/* aaaa\n"
16505 " bbbb */\n"
16506 "}",
16507 Tab);
16508 verifyFormat("{\n"
16509 "\t/*\n"
16510 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16511 "\t bbbbbbbbbbbbb\n"
16512 "\t*/\n"
16513 "}",
16514 "{\n"
16515 "/*\n"
16516 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16517 "*/\n"
16518 "}",
16519 Tab);
16520 Tab.AlignConsecutiveAssignments.Enabled = true;
16521 Tab.AlignConsecutiveDeclarations.Enabled = true;
16522 Tab.TabWidth = 4;
16523 Tab.IndentWidth = 4;
16524 verifyFormat("class Assign {\n"
16525 "\tvoid f() {\n"
16526 "\t\tint x = 123;\n"
16527 "\t\tint random = 4;\n"
16528 "\t\tstd::string alphabet =\n"
16529 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16530 "\t}\n"
16531 "};",
16532 Tab);
16533 Tab.AlignOperands = FormatStyle::OAS_Align;
16534 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16535 " cccccccccccccccccccc;",
16536 Tab);
16537 // no alignment
16538 verifyFormat("int aaaaaaaaaa =\n"
16539 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16540 Tab);
16541 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16542 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16543 " : 333333333333333;",
16544 Tab);
16545 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16546 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16547 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16548 " + cccccccccccccccccccc;",
16549 Tab);
16550 }
16551
16552 TEST_F(FormatTest, ZeroTabWidth) {
16553 FormatStyle Tab = getLLVMStyleWithColumns(42);
16554 Tab.IndentWidth = 8;
16555 Tab.UseTab = FormatStyle::UT_Never;
16556 Tab.TabWidth = 0;
16557 verifyFormat("void a() {\n"
16558 " // line starts with '\t'\n"
16559 "};",
16560 "void a(){\n"
16561 "\t// line starts with '\t'\n"
16562 "};",
16563 Tab);
16564
16565 verifyFormat("void a() {\n"
16566 " // line starts with '\t'\n"
16567 "};",
16568 "void a(){\n"
16569 "\t\t// line starts with '\t'\n"
16570 "};",
16571 Tab);
16572
16573 Tab.UseTab = FormatStyle::UT_ForIndentation;
16574 verifyFormat("void a() {\n"
16575 " // line starts with '\t'\n"
16576 "};",
16577 "void a(){\n"
16578 "\t// line starts with '\t'\n"
16579 "};",
16580 Tab);
16581
16582 verifyFormat("void a() {\n"
16583 " // line starts with '\t'\n"
16584 "};",
16585 "void a(){\n"
16586 "\t\t// line starts with '\t'\n"
16587 "};",
16588 Tab);
16589
16590 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16591 verifyFormat("void a() {\n"
16592 " // line starts with '\t'\n"
16593 "};",
16594 "void a(){\n"
16595 "\t// line starts with '\t'\n"
16596 "};",
16597 Tab);
16598
16599 verifyFormat("void a() {\n"
16600 " // line starts with '\t'\n"
16601 "};",
16602 "void a(){\n"
16603 "\t\t// line starts with '\t'\n"
16604 "};",
16605 Tab);
16606
16607 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16608 verifyFormat("void a() {\n"
16609 " // line starts with '\t'\n"
16610 "};",
16611 "void a(){\n"
16612 "\t// line starts with '\t'\n"
16613 "};",
16614 Tab);
16615
16616 verifyFormat("void a() {\n"
16617 " // line starts with '\t'\n"
16618 "};",
16619 "void a(){\n"
16620 "\t\t// line starts with '\t'\n"
16621 "};",
16622 Tab);
16623
16624 Tab.UseTab = FormatStyle::UT_Always;
16625 verifyFormat("void a() {\n"
16626 "// line starts with '\t'\n"
16627 "};",
16628 "void a(){\n"
16629 "\t// line starts with '\t'\n"
16630 "};",
16631 Tab);
16632
16633 verifyFormat("void a() {\n"
16634 "// line starts with '\t'\n"
16635 "};",
16636 "void a(){\n"
16637 "\t\t// line starts with '\t'\n"
16638 "};",
16639 Tab);
16640 }
16641
16642 TEST_F(FormatTest, CalculatesOriginalColumn) {
16643 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16644 "q\"; /* some\n"
16645 " comment */",
16646 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16647 "q\"; /* some\n"
16648 " comment */");
16649 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16650 "/* some\n"
16651 " comment */",
16652 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16653 " /* some\n"
16654 " comment */");
16655 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16656 "qqq\n"
16657 "/* some\n"
16658 " comment */",
16659 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16660 "qqq\n"
16661 " /* some\n"
16662 " comment */");
16663 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16664 "wwww; /* some\n"
16665 " comment */",
16666 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16667 "wwww; /* some\n"
16668 " comment */");
16669 }
16670
16671 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
16672 FormatStyle NoSpace = getLLVMStyle();
16673 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
16674
16675 verifyFormat("while(true)\n"
16676 " continue;",
16677 NoSpace);
16678 verifyFormat("for(;;)\n"
16679 " continue;",
16680 NoSpace);
16681 verifyFormat("if(true)\n"
16682 " f();\n"
16683 "else if(true)\n"
16684 " f();",
16685 NoSpace);
16686 verifyFormat("do {\n"
16687 " do_something();\n"
16688 "} while(something());",
16689 NoSpace);
16690 verifyFormat("switch(x) {\n"
16691 "default:\n"
16692 " break;\n"
16693 "}",
16694 NoSpace);
16695 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
16696 verifyFormat("size_t x = sizeof(x);", NoSpace);
16697 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
16698 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
16699 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
16700 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
16701 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
16702 verifyFormat("alignas(128) char a[128];", NoSpace);
16703 verifyFormat("size_t x = alignof(MyType);", NoSpace);
16704 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
16705 verifyFormat("int f() throw(Deprecated);", NoSpace);
16706 verifyFormat("typedef void (*cb)(int);", NoSpace);
16707 verifyFormat("T A::operator()();", NoSpace);
16708 verifyFormat("X A::operator++(T);", NoSpace);
16709 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
16710 verifyFormat("#if (foo || bar) && baz\n"
16711 "#elif ((a || b) && c) || d\n"
16712 "#endif",
16713 NoSpace);
16714
16715 FormatStyle Space = getLLVMStyle();
16716 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
16717
16718 verifyFormat("int f ();", Space);
16719 verifyFormat("bool operator< ();", Space);
16720 verifyFormat("bool operator> ();", Space);
16721 verifyFormat("void f (int a, T b) {\n"
16722 " while (true)\n"
16723 " continue;\n"
16724 "}",
16725 Space);
16726 verifyFormat("if (true)\n"
16727 " f ();\n"
16728 "else if (true)\n"
16729 " f ();",
16730 Space);
16731 verifyFormat("do {\n"
16732 " do_something ();\n"
16733 "} while (something ());",
16734 Space);
16735 verifyFormat("switch (x) {\n"
16736 "default:\n"
16737 " break;\n"
16738 "}",
16739 Space);
16740 verifyFormat("A::A () : a (1) {}", Space);
16741 verifyFormat("void f () __attribute__ ((asdf));", Space);
16742 verifyFormat("*(&a + 1);\n"
16743 "&((&a)[1]);\n"
16744 "a[(b + c) * d];\n"
16745 "(((a + 1) * 2) + 3) * 4;",
16746 Space);
16747 verifyFormat("#define A(x) x", Space);
16748 verifyFormat("#define A (x) x", Space);
16749 verifyFormat("#if defined(x)\n"
16750 "#endif",
16751 Space);
16752 verifyFormat("auto i = std::make_unique<int> (5);", Space);
16753 verifyFormat("size_t x = sizeof (x);", Space);
16754 verifyFormat("auto f (int x) -> decltype (x);", Space);
16755 verifyFormat("auto f (int x) -> typeof (x);", Space);
16756 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
16757 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
16758 verifyFormat("int f (T x) noexcept (x.create ());", Space);
16759 verifyFormat("alignas (128) char a[128];", Space);
16760 verifyFormat("size_t x = alignof (MyType);", Space);
16761 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
16762 verifyFormat("int f () throw (Deprecated);", Space);
16763 verifyFormat("typedef void (*cb) (int);", Space);
16764 verifyFormat("T A::operator() ();", Space);
16765 verifyFormat("X A::operator++ (T);", Space);
16766 verifyFormat("auto lambda = [] () { return 0; };", Space);
16767 verifyFormat("int x = int (y);", Space);
16768 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
16769 verifyFormat("__builtin_LINE ()", Space);
16770 verifyFormat("__builtin_UNKNOWN ()", Space);
16771
16772 FormatStyle SomeSpace = getLLVMStyle();
16773 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
16774
16775 verifyFormat("[]() -> float {}", SomeSpace);
16776 verifyFormat("[] (auto foo) {}", SomeSpace);
16777 verifyFormat("[foo]() -> int {}", SomeSpace);
16778 verifyFormat("int f();", SomeSpace);
16779 verifyFormat("void f (int a, T b) {\n"
16780 " while (true)\n"
16781 " continue;\n"
16782 "}",
16783 SomeSpace);
16784 verifyFormat("if (true)\n"
16785 " f();\n"
16786 "else if (true)\n"
16787 " f();",
16788 SomeSpace);
16789 verifyFormat("do {\n"
16790 " do_something();\n"
16791 "} while (something());",
16792 SomeSpace);
16793 verifyFormat("switch (x) {\n"
16794 "default:\n"
16795 " break;\n"
16796 "}",
16797 SomeSpace);
16798 verifyFormat("A::A() : a (1) {}", SomeSpace);
16799 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
16800 verifyFormat("*(&a + 1);\n"
16801 "&((&a)[1]);\n"
16802 "a[(b + c) * d];\n"
16803 "(((a + 1) * 2) + 3) * 4;",
16804 SomeSpace);
16805 verifyFormat("#define A(x) x", SomeSpace);
16806 verifyFormat("#define A (x) x", SomeSpace);
16807 verifyFormat("#if defined(x)\n"
16808 "#endif",
16809 SomeSpace);
16810 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
16811 verifyFormat("size_t x = sizeof (x);", SomeSpace);
16812 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
16813 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
16814 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
16815 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
16816 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
16817 verifyFormat("alignas (128) char a[128];", SomeSpace);
16818 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
16819 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16820 SomeSpace);
16821 verifyFormat("int f() throw (Deprecated);", SomeSpace);
16822 verifyFormat("typedef void (*cb) (int);", SomeSpace);
16823 verifyFormat("T A::operator()();", SomeSpace);
16824 verifyFormat("X A::operator++ (T);", SomeSpace);
16825 verifyFormat("int x = int (y);", SomeSpace);
16826 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
16827
16828 FormatStyle SpaceControlStatements = getLLVMStyle();
16829 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16830 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
16831
16832 verifyFormat("while (true)\n"
16833 " continue;",
16834 SpaceControlStatements);
16835 verifyFormat("if (true)\n"
16836 " f();\n"
16837 "else if (true)\n"
16838 " f();",
16839 SpaceControlStatements);
16840 verifyFormat("for (;;) {\n"
16841 " do_something();\n"
16842 "}",
16843 SpaceControlStatements);
16844 verifyFormat("do {\n"
16845 " do_something();\n"
16846 "} while (something());",
16847 SpaceControlStatements);
16848 verifyFormat("switch (x) {\n"
16849 "default:\n"
16850 " break;\n"
16851 "}",
16852 SpaceControlStatements);
16853
16854 FormatStyle SpaceFuncDecl = getLLVMStyle();
16855 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16856 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
16857
16858 verifyFormat("int f ();", SpaceFuncDecl);
16859 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
16860 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
16861 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
16862 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
16863 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
16864 verifyFormat("#define A(x) x", SpaceFuncDecl);
16865 verifyFormat("#define A (x) x", SpaceFuncDecl);
16866 verifyFormat("#if defined(x)\n"
16867 "#endif",
16868 SpaceFuncDecl);
16869 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
16870 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
16871 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
16872 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
16873 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
16874 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
16875 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
16876 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
16877 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
16878 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16879 SpaceFuncDecl);
16880 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
16881 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
16882 verifyFormat("T A::operator()();", SpaceFuncDecl);
16883 verifyFormat("X A::operator++(T);", SpaceFuncDecl);
16884 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
16885 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
16886 verifyFormat("int x = int(y);", SpaceFuncDecl);
16887 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16888 SpaceFuncDecl);
16889
16890 FormatStyle SpaceFuncDef = getLLVMStyle();
16891 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16892 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
16893
16894 verifyFormat("int f();", SpaceFuncDef);
16895 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16896 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
16897 verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
16898 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
16899 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
16900 verifyFormat("#define A(x) x", SpaceFuncDef);
16901 verifyFormat("#define A (x) x", SpaceFuncDef);
16902 verifyFormat("#if defined(x)\n"
16903 "#endif",
16904 SpaceFuncDef);
16905 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
16906 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
16907 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
16908 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
16909 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
16910 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
16911 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
16912 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
16913 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
16914 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16915 SpaceFuncDef);
16916 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
16917 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
16918 verifyFormat("T A::operator()();", SpaceFuncDef);
16919 verifyFormat("X A::operator++(T);", SpaceFuncDef);
16920 verifyFormat("T A::operator()() {}", SpaceFuncDef);
16921 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
16922 verifyFormat("int x = int(y);", SpaceFuncDef);
16923 verifyFormat("void foo::bar () {}", SpaceFuncDef);
16924 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
16925 SpaceFuncDef);
16926
16927 FormatStyle SpaceIfMacros = getLLVMStyle();
16928 SpaceIfMacros.IfMacros.clear();
16929 SpaceIfMacros.IfMacros.push_back("MYIF");
16930 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16931 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
16932 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
16933 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
16934 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
16935
16936 FormatStyle SpaceForeachMacros = getLLVMStyle();
16937 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
16938 FormatStyle::SBS_Never);
16939 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
16940 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16941 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
16942 verifyFormat("for (;;) {\n"
16943 "}",
16944 SpaceForeachMacros);
16945 verifyFormat("foreach (Item *item, itemlist) {\n"
16946 "}",
16947 SpaceForeachMacros);
16948 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
16949 "}",
16950 SpaceForeachMacros);
16951 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
16952 "}",
16953 SpaceForeachMacros);
16954 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
16955
16956 FormatStyle SomeSpace2 = getLLVMStyle();
16957 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16958 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
16959 verifyFormat("[]() -> float {}", SomeSpace2);
16960 verifyFormat("[] (auto foo) {}", SomeSpace2);
16961 verifyFormat("[foo]() -> int {}", SomeSpace2);
16962 verifyFormat("int f();", SomeSpace2);
16963 verifyFormat("void f (int a, T b) {\n"
16964 " while (true)\n"
16965 " continue;\n"
16966 "}",
16967 SomeSpace2);
16968 verifyFormat("if (true)\n"
16969 " f();\n"
16970 "else if (true)\n"
16971 " f();",
16972 SomeSpace2);
16973 verifyFormat("do {\n"
16974 " do_something();\n"
16975 "} while (something());",
16976 SomeSpace2);
16977 verifyFormat("switch (x) {\n"
16978 "default:\n"
16979 " break;\n"
16980 "}",
16981 SomeSpace2);
16982 verifyFormat("A::A() : a (1) {}", SomeSpace2);
16983 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
16984 verifyFormat("*(&a + 1);\n"
16985 "&((&a)[1]);\n"
16986 "a[(b + c) * d];\n"
16987 "(((a + 1) * 2) + 3) * 4;",
16988 SomeSpace2);
16989 verifyFormat("#define A(x) x", SomeSpace2);
16990 verifyFormat("#define A (x) x", SomeSpace2);
16991 verifyFormat("#if defined(x)\n"
16992 "#endif",
16993 SomeSpace2);
16994 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
16995 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
16996 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
16997 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
16998 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
16999 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
17000 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
17001 verifyFormat("alignas (128) char a[128];", SomeSpace2);
17002 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
17003 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17004 SomeSpace2);
17005 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
17006 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
17007 verifyFormat("T A::operator()();", SomeSpace2);
17008 verifyFormat("X A::operator++ (T);", SomeSpace2);
17009 verifyFormat("int x = int (y);", SomeSpace2);
17010 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
17011
17012 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
17013 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17014 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17015 .AfterOverloadedOperator = true;
17016
17017 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
17018 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
17019 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
17020 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17021
17022 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17023 .AfterOverloadedOperator = false;
17024
17025 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
17026 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
17027 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
17028 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17029
17030 auto SpaceAfterRequires = getLLVMStyle();
17031 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17032 EXPECT_FALSE(
17033 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
17034 EXPECT_FALSE(
17035 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
17036 verifyFormat("void f(auto x)\n"
17037 " requires requires(int i) { x + i; }\n"
17038 "{}",
17039 SpaceAfterRequires);
17040 verifyFormat("void f(auto x)\n"
17041 " requires(requires(int i) { x + i; })\n"
17042 "{}",
17043 SpaceAfterRequires);
17044 verifyFormat("if (requires(int i) { x + i; })\n"
17045 " return;",
17046 SpaceAfterRequires);
17047 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17048 verifyFormat("template <typename T>\n"
17049 " requires(Foo<T>)\n"
17050 "class Bar;",
17051 SpaceAfterRequires);
17052
17053 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17054 verifyFormat("void f(auto x)\n"
17055 " requires requires(int i) { x + i; }\n"
17056 "{}",
17057 SpaceAfterRequires);
17058 verifyFormat("void f(auto x)\n"
17059 " requires (requires(int i) { x + i; })\n"
17060 "{}",
17061 SpaceAfterRequires);
17062 verifyFormat("if (requires(int i) { x + i; })\n"
17063 " return;",
17064 SpaceAfterRequires);
17065 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17066 verifyFormat("template <typename T>\n"
17067 " requires (Foo<T>)\n"
17068 "class Bar;",
17069 SpaceAfterRequires);
17070
17071 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
17072 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
17073 verifyFormat("void f(auto x)\n"
17074 " requires requires (int i) { x + i; }\n"
17075 "{}",
17076 SpaceAfterRequires);
17077 verifyFormat("void f(auto x)\n"
17078 " requires(requires (int i) { x + i; })\n"
17079 "{}",
17080 SpaceAfterRequires);
17081 verifyFormat("if (requires (int i) { x + i; })\n"
17082 " return;",
17083 SpaceAfterRequires);
17084 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17085 verifyFormat("template <typename T>\n"
17086 " requires(Foo<T>)\n"
17087 "class Bar;",
17088 SpaceAfterRequires);
17089
17090 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17091 verifyFormat("void f(auto x)\n"
17092 " requires requires (int i) { x + i; }\n"
17093 "{}",
17094 SpaceAfterRequires);
17095 verifyFormat("void f(auto x)\n"
17096 " requires (requires (int i) { x + i; })\n"
17097 "{}",
17098 SpaceAfterRequires);
17099 verifyFormat("if (requires (int i) { x + i; })\n"
17100 " return;",
17101 SpaceAfterRequires);
17102 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17103 verifyFormat("template <typename T>\n"
17104 " requires (Foo<T>)\n"
17105 "class Bar;",
17106 SpaceAfterRequires);
17107 }
17108
17109 TEST_F(FormatTest, SpaceAfterLogicalNot) {
17110 FormatStyle Spaces = getLLVMStyle();
17111 Spaces.SpaceAfterLogicalNot = true;
17112
17113 verifyFormat("bool x = ! y", Spaces);
17114 verifyFormat("if (! isFailure())", Spaces);
17115 verifyFormat("if (! (a && b))", Spaces);
17116 verifyFormat("\"Error!\"", Spaces);
17117 verifyFormat("! ! x", Spaces);
17118 }
17119
17120 TEST_F(FormatTest, ConfigurableSpacesInParens) {
17121 FormatStyle Spaces = getLLVMStyle();
17122
17123 verifyFormat("do_something(::globalVar);", Spaces);
17124 verifyFormat("call(x, y, z);", Spaces);
17125 verifyFormat("call();", Spaces);
17126 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17127 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17128 Spaces);
17129 verifyFormat("while ((bool)1)\n"
17130 " continue;",
17131 Spaces);
17132 verifyFormat("for (;;)\n"
17133 " continue;",
17134 Spaces);
17135 verifyFormat("if (true)\n"
17136 " f();\n"
17137 "else if (true)\n"
17138 " f();",
17139 Spaces);
17140 verifyFormat("do {\n"
17141 " do_something((int)i);\n"
17142 "} while (something());",
17143 Spaces);
17144 verifyFormat("switch (x) {\n"
17145 "default:\n"
17146 " break;\n"
17147 "}",
17148 Spaces);
17149 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17150 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17151 verifyFormat("void f() __attribute__((asdf));", Spaces);
17152 verifyFormat("x = (int32)y;", Spaces);
17153 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces);
17154 verifyFormat("decltype(x) y = 42;", Spaces);
17155 verifyFormat("decltype((x)) y = z;", Spaces);
17156 verifyFormat("decltype((foo())) a = foo();", Spaces);
17157 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17158 verifyFormat("if ((x - y) && (a ^ b))\n"
17159 " f();",
17160 Spaces);
17161 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17162 " foo(i);",
17163 Spaces);
17164 verifyFormat("switch (x / (y + z)) {\n"
17165 "default:\n"
17166 " break;\n"
17167 "}",
17168 Spaces);
17169
17170 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17171 Spaces.SpacesInParensOptions = {};
17172 Spaces.SpacesInParensOptions.Other = true;
17173 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17174 verifyFormat("do_something( ::globalVar );", Spaces);
17175 verifyFormat("call( x, y, z );", Spaces);
17176 verifyFormat("call();", Spaces);
17177 verifyFormat("std::function<void( int, int )> callback;", Spaces);
17178 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17179 Spaces);
17180 verifyFormat("while ( (bool)1 )\n"
17181 " continue;",
17182 Spaces);
17183 verifyFormat("for ( ;; )\n"
17184 " continue;",
17185 Spaces);
17186 verifyFormat("if ( true )\n"
17187 " f();\n"
17188 "else if ( true )\n"
17189 " f();",
17190 Spaces);
17191 verifyFormat("do {\n"
17192 " do_something( (int)i );\n"
17193 "} while ( something() );",
17194 Spaces);
17195 verifyFormat("switch ( x ) {\n"
17196 "default:\n"
17197 " break;\n"
17198 "}",
17199 Spaces);
17200 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17201 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17202 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17203 verifyFormat("x = (int32)y;", Spaces);
17204 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces);
17205 verifyFormat("decltype( x ) y = 42;", Spaces);
17206 verifyFormat("decltype( ( x ) ) y = z;", Spaces);
17207 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces);
17208 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces);
17209 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17210 " f();",
17211 Spaces);
17212 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17213 " foo( i );",
17214 Spaces);
17215 verifyFormat("switch ( x / ( y + z ) ) {\n"
17216 "default:\n"
17217 " break;\n"
17218 "}",
17219 Spaces);
17220
17221 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17222 Spaces.SpacesInParensOptions = {};
17223 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17224 verifyFormat("Type *A = ( Type * )P;", Spaces);
17225 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17226 verifyFormat("x = ( int32 )y;", Spaces);
17227 verifyFormat("throw ( int32 )x;", Spaces);
17228 verifyFormat("int a = ( int )(2.0f);", Spaces);
17229 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17230 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17231 verifyFormat("#define x (( int )-1)", Spaces);
17232 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17233
17234 // Run the first set of tests again with:
17235 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17236 Spaces.SpacesInParensOptions = {};
17237 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17238 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17239 verifyFormat("call(x, y, z);", Spaces);
17240 verifyFormat("call( );", Spaces);
17241 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17242 verifyFormat("while (( bool )1)\n"
17243 " continue;",
17244 Spaces);
17245 verifyFormat("for (;;)\n"
17246 " continue;",
17247 Spaces);
17248 verifyFormat("if (true)\n"
17249 " f( );\n"
17250 "else if (true)\n"
17251 " f( );",
17252 Spaces);
17253 verifyFormat("do {\n"
17254 " do_something(( int )i);\n"
17255 "} while (something( ));",
17256 Spaces);
17257 verifyFormat("switch (x) {\n"
17258 "default:\n"
17259 " break;\n"
17260 "}",
17261 Spaces);
17262 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17263 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17264 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17265 verifyFormat("x = ( int32 )y;", Spaces);
17266 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17267 verifyFormat("decltype(x) y = 42;", Spaces);
17268 verifyFormat("decltype((x)) y = z;", Spaces);
17269 verifyFormat("decltype((foo( ))) a = foo( );", Spaces);
17270 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17271 verifyFormat("if ((x - y) && (a ^ b))\n"
17272 " f( );",
17273 Spaces);
17274 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17275 " foo(i);",
17276 Spaces);
17277 verifyFormat("switch (x / (y + z)) {\n"
17278 "default:\n"
17279 " break;\n"
17280 "}",
17281 Spaces);
17282
17283 // Run the first set of tests again with:
17284 Spaces.SpaceAfterCStyleCast = true;
17285 verifyFormat("call(x, y, z);", Spaces);
17286 verifyFormat("call( );", Spaces);
17287 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17288 verifyFormat("while (( bool ) 1)\n"
17289 " continue;",
17290 Spaces);
17291 verifyFormat("for (;;)\n"
17292 " continue;",
17293 Spaces);
17294 verifyFormat("if (true)\n"
17295 " f( );\n"
17296 "else if (true)\n"
17297 " f( );",
17298 Spaces);
17299 verifyFormat("do {\n"
17300 " do_something(( int ) i);\n"
17301 "} while (something( ));",
17302 Spaces);
17303 verifyFormat("switch (x) {\n"
17304 "default:\n"
17305 " break;\n"
17306 "}",
17307 Spaces);
17308 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
17309 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
17310 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
17311 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
17312 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
17313 verifyFormat("throw ( int32 ) x;", Spaces);
17314 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17315 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17316 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17317
17318 // Run subset of tests again with:
17319 Spaces.SpacesInParensOptions.InCStyleCasts = false;
17320 Spaces.SpaceAfterCStyleCast = true;
17321 verifyFormat("while ((bool) 1)\n"
17322 " continue;",
17323 Spaces);
17324 verifyFormat("do {\n"
17325 " do_something((int) i);\n"
17326 "} while (something( ));",
17327 Spaces);
17328
17329 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
17330 verifyFormat("size_t idx = (size_t) a;", Spaces);
17331 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
17332 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17333 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17334 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17335 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17336 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
17337 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
17338 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
17339 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
17340 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
17341 verifyFormat("throw (int32) x;", Spaces);
17342 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17343 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17344 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17345
17346 Spaces.ColumnLimit = 80;
17347 Spaces.IndentWidth = 4;
17348 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17349 verifyFormat("void foo( ) {\n"
17350 " size_t foo = (*(function))(\n"
17351 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17352 "BarrrrrrrrrrrrLong,\n"
17353 " FoooooooooLooooong);\n"
17354 "}",
17355 Spaces);
17356 Spaces.SpaceAfterCStyleCast = false;
17357 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17358 verifyFormat("size_t idx = (size_t)a;", Spaces);
17359 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17360 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17361 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17362 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17363 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17364
17365 verifyFormat("void foo( ) {\n"
17366 " size_t foo = (*(function))(\n"
17367 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17368 "BarrrrrrrrrrrrLong,\n"
17369 " FoooooooooLooooong);\n"
17370 "}",
17371 Spaces);
17372
17373 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
17374 verifyFormat("void foo( ) {\n"
17375 " size_t foo = (*(function))(\n"
17376 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17377 "BarrrrrrrrrrrrLong,\n"
17378 " FoooooooooLooooong\n"
17379 " );\n"
17380 "}",
17381 Spaces);
17382 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17383 verifyFormat("size_t idx = (size_t)a;", Spaces);
17384 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17385 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17386 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17387 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17388 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17389
17390 // Check ExceptDoubleParentheses spaces
17391 Spaces.IndentWidth = 2;
17392 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17393 Spaces.SpacesInParensOptions = {};
17394 Spaces.SpacesInParensOptions.Other = true;
17395 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17396 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
17397 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces);
17398 verifyFormat("void f() __attribute__(( asdf ));", Spaces);
17399 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces);
17400 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces);
17401 verifyFormat("class __declspec( dllimport ) X {};", Spaces);
17402 verifyFormat("class __declspec(( dllimport )) X {};", Spaces);
17403 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces);
17404 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces);
17405 verifyFormat("decltype( x ) y = 42;", Spaces);
17406 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces);
17407 verifyFormat("if (( i = j ))\n"
17408 " do_something( i );",
17409 Spaces);
17410
17411 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17412 Spaces.SpacesInParensOptions = {};
17413 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17414 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17415 verifyFormat("while ( (bool)1 )\n"
17416 " continue;",
17417 Spaces);
17418 verifyFormat("while ((i = j))\n"
17419 " continue;",
17420 Spaces);
17421 verifyFormat("do {\n"
17422 " do_something((int)i);\n"
17423 "} while ( something() );",
17424 Spaces);
17425 verifyFormat("do {\n"
17426 " do_something((int)i);\n"
17427 "} while ((i = i + 1));",
17428 Spaces);
17429 verifyFormat("if ( (x - y) && (a ^ b) )\n"
17430 " f();",
17431 Spaces);
17432 verifyFormat("if ((i = j))\n"
17433 " do_something(i);",
17434 Spaces);
17435 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
17436 " foo(i);",
17437 Spaces);
17438 verifyFormat("switch ( x / (y + z) ) {\n"
17439 "default:\n"
17440 " break;\n"
17441 "}",
17442 Spaces);
17443 verifyFormat("if constexpr ((a = b))\n"
17444 " c;",
17445 Spaces);
17446 }
17447
17448 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
17449 verifyFormat("int a[5];");
17450 verifyFormat("a[3] += 42;");
17451
17452 FormatStyle Spaces = getLLVMStyle();
17453 Spaces.SpacesInSquareBrackets = true;
17454 // Not lambdas.
17455 verifyFormat("int a[ 5 ];", Spaces);
17456 verifyFormat("a[ 3 ] += 42;", Spaces);
17457 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
17458 verifyFormat("double &operator[](int i) { return 0; }\n"
17459 "int i;",
17460 Spaces);
17461 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
17462 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
17463 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
17464 // Lambdas.
17465 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
17466 verifyFormat("return [ i, args... ] {};", Spaces);
17467 verifyFormat("int foo = [ &bar ]() {};", Spaces);
17468 verifyFormat("int foo = [ = ]() {};", Spaces);
17469 verifyFormat("int foo = [ & ]() {};", Spaces);
17470 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
17471 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
17472 }
17473
17474 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
17475 FormatStyle NoSpaceStyle = getLLVMStyle();
17476 verifyFormat("int a[5];", NoSpaceStyle);
17477 verifyFormat("a[3] += 42;", NoSpaceStyle);
17478
17479 verifyFormat("int a[1];", NoSpaceStyle);
17480 verifyFormat("int 1 [a];", NoSpaceStyle);
17481 verifyFormat("int a[1][2];", NoSpaceStyle);
17482 verifyFormat("a[7] = 5;", NoSpaceStyle);
17483 verifyFormat("int a = (f())[23];", NoSpaceStyle);
17484 verifyFormat("f([] {})", NoSpaceStyle);
17485
17486 FormatStyle Space = getLLVMStyle();
17487 Space.SpaceBeforeSquareBrackets = true;
17488 verifyFormat("int c = []() -> int { return 2; }();", Space);
17489 verifyFormat("return [i, args...] {};", Space);
17490
17491 verifyFormat("int a [5];", Space);
17492 verifyFormat("a [3] += 42;", Space);
17493 verifyFormat("constexpr char hello []{\"hello\"};", Space);
17494 verifyFormat("double &operator[](int i) { return 0; }\n"
17495 "int i;",
17496 Space);
17497 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
17498 verifyFormat("int i = a [a][a]->f();", Space);
17499 verifyFormat("int i = (*b) [a]->f();", Space);
17500
17501 verifyFormat("int a [1];", Space);
17502 verifyFormat("int 1 [a];", Space);
17503 verifyFormat("int a [1][2];", Space);
17504 verifyFormat("a [7] = 5;", Space);
17505 verifyFormat("int a = (f()) [23];", Space);
17506 verifyFormat("f([] {})", Space);
17507 }
17508
17509 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
17510 verifyFormat("int a = 5;");
17511 verifyFormat("a += 42;");
17512 verifyFormat("a or_eq 8;");
17513 verifyFormat("xor = foo;");
17514
17515 FormatStyle Spaces = getLLVMStyle();
17516 Spaces.SpaceBeforeAssignmentOperators = false;
17517 verifyFormat("int a= 5;", Spaces);
17518 verifyFormat("a+= 42;", Spaces);
17519 verifyFormat("a or_eq 8;", Spaces);
17520 verifyFormat("xor= foo;", Spaces);
17521 }
17522
17523 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
17524 verifyFormat("class Foo : public Bar {};");
17525 verifyFormat("Foo::Foo() : foo(1) {}");
17526 verifyFormat("for (auto a : b) {\n}");
17527 verifyFormat("int x = a ? b : c;");
17528 verifyFormat("{\n"
17529 "label0:\n"
17530 " int x = 0;\n"
17531 "}");
17532 verifyFormat("switch (x) {\n"
17533 "case 1:\n"
17534 "default:\n"
17535 "}");
17536 verifyFormat("switch (allBraces) {\n"
17537 "case 1: {\n"
17538 " break;\n"
17539 "}\n"
17540 "case 2: {\n"
17541 " [[fallthrough]];\n"
17542 "}\n"
17543 "default: {\n"
17544 " break;\n"
17545 "}\n"
17546 "}");
17547
17548 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
17549 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
17550 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
17551 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
17552 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
17553 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
17554 verifyFormat("{\n"
17555 "label1:\n"
17556 " int x = 0;\n"
17557 "}",
17558 CtorInitializerStyle);
17559 verifyFormat("switch (x) {\n"
17560 "case 1:\n"
17561 "default:\n"
17562 "}",
17563 CtorInitializerStyle);
17564 verifyFormat("switch (allBraces) {\n"
17565 "case 1: {\n"
17566 " break;\n"
17567 "}\n"
17568 "case 2: {\n"
17569 " [[fallthrough]];\n"
17570 "}\n"
17571 "default: {\n"
17572 " break;\n"
17573 "}\n"
17574 "}",
17575 CtorInitializerStyle);
17576 CtorInitializerStyle.BreakConstructorInitializers =
17577 FormatStyle::BCIS_AfterColon;
17578 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17579 " aaaaaaaaaaaaaaaa(1),\n"
17580 " bbbbbbbbbbbbbbbb(2) {}",
17581 CtorInitializerStyle);
17582 CtorInitializerStyle.BreakConstructorInitializers =
17583 FormatStyle::BCIS_BeforeComma;
17584 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17585 " : aaaaaaaaaaaaaaaa(1)\n"
17586 " , bbbbbbbbbbbbbbbb(2) {}",
17587 CtorInitializerStyle);
17588 CtorInitializerStyle.BreakConstructorInitializers =
17589 FormatStyle::BCIS_BeforeColon;
17590 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17591 " : aaaaaaaaaaaaaaaa(1),\n"
17592 " bbbbbbbbbbbbbbbb(2) {}",
17593 CtorInitializerStyle);
17594 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
17595 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17596 ": aaaaaaaaaaaaaaaa(1),\n"
17597 " bbbbbbbbbbbbbbbb(2) {}",
17598 CtorInitializerStyle);
17599
17600 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
17601 InheritanceStyle.SpaceBeforeInheritanceColon = false;
17602 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
17603 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
17604 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
17605 verifyFormat("int x = a ? b : c;", InheritanceStyle);
17606 verifyFormat("{\n"
17607 "label2:\n"
17608 " int x = 0;\n"
17609 "}",
17610 InheritanceStyle);
17611 verifyFormat("switch (x) {\n"
17612 "case 1:\n"
17613 "default:\n"
17614 "}",
17615 InheritanceStyle);
17616 verifyFormat("switch (allBraces) {\n"
17617 "case 1: {\n"
17618 " break;\n"
17619 "}\n"
17620 "case 2: {\n"
17621 " [[fallthrough]];\n"
17622 "}\n"
17623 "default: {\n"
17624 " break;\n"
17625 "}\n"
17626 "}",
17627 InheritanceStyle);
17628 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
17629 verifyFormat("class Foooooooooooooooooooooo\n"
17630 " : public aaaaaaaaaaaaaaaaaa,\n"
17631 " public bbbbbbbbbbbbbbbbbb {\n"
17632 "}",
17633 InheritanceStyle);
17634 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
17635 verifyFormat("class Foooooooooooooooooooooo:\n"
17636 " public aaaaaaaaaaaaaaaaaa,\n"
17637 " public bbbbbbbbbbbbbbbbbb {\n"
17638 "}",
17639 InheritanceStyle);
17640 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
17641 verifyFormat("class Foooooooooooooooooooooo\n"
17642 " : public aaaaaaaaaaaaaaaaaa\n"
17643 " , public bbbbbbbbbbbbbbbbbb {\n"
17644 "}",
17645 InheritanceStyle);
17646 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17647 verifyFormat("class Foooooooooooooooooooooo\n"
17648 " : public aaaaaaaaaaaaaaaaaa,\n"
17649 " public bbbbbbbbbbbbbbbbbb {\n"
17650 "}",
17651 InheritanceStyle);
17652 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
17653 verifyFormat("class Foooooooooooooooooooooo\n"
17654 ": public aaaaaaaaaaaaaaaaaa,\n"
17655 " public bbbbbbbbbbbbbbbbbb {}",
17656 InheritanceStyle);
17657
17658 FormatStyle ForLoopStyle = getLLVMStyle();
17659 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
17660 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
17661 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
17662 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
17663 verifyFormat("int x = a ? b : c;", ForLoopStyle);
17664 verifyFormat("{\n"
17665 "label2:\n"
17666 " int x = 0;\n"
17667 "}",
17668 ForLoopStyle);
17669 verifyFormat("switch (x) {\n"
17670 "case 1:\n"
17671 "default:\n"
17672 "}",
17673 ForLoopStyle);
17674 verifyFormat("switch (allBraces) {\n"
17675 "case 1: {\n"
17676 " break;\n"
17677 "}\n"
17678 "case 2: {\n"
17679 " [[fallthrough]];\n"
17680 "}\n"
17681 "default: {\n"
17682 " break;\n"
17683 "}\n"
17684 "}",
17685 ForLoopStyle);
17686
17687 FormatStyle CaseStyle = getLLVMStyle();
17688 CaseStyle.SpaceBeforeCaseColon = true;
17689 verifyFormat("class Foo : public Bar {};", CaseStyle);
17690 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
17691 verifyFormat("for (auto a : b) {\n}", CaseStyle);
17692 verifyFormat("int x = a ? b : c;", CaseStyle);
17693 verifyFormat("switch (x) {\n"
17694 "case 1 :\n"
17695 "default :\n"
17696 "}",
17697 CaseStyle);
17698 verifyFormat("switch (allBraces) {\n"
17699 "case 1 : {\n"
17700 " break;\n"
17701 "}\n"
17702 "case 2 : {\n"
17703 " [[fallthrough]];\n"
17704 "}\n"
17705 "default : {\n"
17706 " break;\n"
17707 "}\n"
17708 "}",
17709 CaseStyle);
17710 // Goto labels should not be affected.
17711 verifyFormat("switch (x) {\n"
17712 "goto_label:\n"
17713 "default :\n"
17714 "}",
17715 CaseStyle);
17716 verifyFormat("switch (x) {\n"
17717 "goto_label: { break; }\n"
17718 "default : {\n"
17719 " break;\n"
17720 "}\n"
17721 "}",
17722 CaseStyle);
17723
17724 FormatStyle NoSpaceStyle = getLLVMStyle();
17725 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
17726 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17727 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
17728 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17729 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
17730 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
17731 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
17732 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
17733 verifyFormat("{\n"
17734 "label3:\n"
17735 " int x = 0;\n"
17736 "}",
17737 NoSpaceStyle);
17738 verifyFormat("switch (x) {\n"
17739 "case 1:\n"
17740 "default:\n"
17741 "}",
17742 NoSpaceStyle);
17743 verifyFormat("switch (allBraces) {\n"
17744 "case 1: {\n"
17745 " break;\n"
17746 "}\n"
17747 "case 2: {\n"
17748 " [[fallthrough]];\n"
17749 "}\n"
17750 "default: {\n"
17751 " break;\n"
17752 "}\n"
17753 "}",
17754 NoSpaceStyle);
17755
17756 FormatStyle InvertedSpaceStyle = getLLVMStyle();
17757 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
17758 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17759 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
17760 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17761 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
17762 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
17763 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
17764 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
17765 verifyFormat("{\n"
17766 "label3:\n"
17767 " int x = 0;\n"
17768 "}",
17769 InvertedSpaceStyle);
17770 verifyFormat("switch (x) {\n"
17771 "case 1 :\n"
17772 "case 2 : {\n"
17773 " break;\n"
17774 "}\n"
17775 "default :\n"
17776 " break;\n"
17777 "}",
17778 InvertedSpaceStyle);
17779 verifyFormat("switch (allBraces) {\n"
17780 "case 1 : {\n"
17781 " break;\n"
17782 "}\n"
17783 "case 2 : {\n"
17784 " [[fallthrough]];\n"
17785 "}\n"
17786 "default : {\n"
17787 " break;\n"
17788 "}\n"
17789 "}",
17790 InvertedSpaceStyle);
17791 }
17792
17793 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
17794 FormatStyle Style = getLLVMStyle();
17795
17796 Style.PointerAlignment = FormatStyle::PAS_Left;
17797 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17798 verifyFormat("void* const* x = NULL;", Style);
17799
17800 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17801 do { \
17802 Style.PointerAlignment = FormatStyle::Pointers; \
17803 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17804 verifyFormat(Code, Style); \
17805 } while (false)
17806
17807 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
17808 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
17809 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
17810
17811 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
17812 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
17813 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
17814
17815 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
17816 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
17817 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
17818
17819 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
17820 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
17821 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
17822
17823 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
17824 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17825 SAPQ_Default);
17826 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17827 SAPQ_Default);
17828
17829 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
17830 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17831 SAPQ_Before);
17832 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17833 SAPQ_Before);
17834
17835 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
17836 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
17837 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17838 SAPQ_After);
17839
17840 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
17841 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
17842 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
17843
17844 #undef verifyQualifierSpaces
17845
17846 FormatStyle Spaces = getLLVMStyle();
17847 Spaces.AttributeMacros.push_back("qualified");
17848 Spaces.PointerAlignment = FormatStyle::PAS_Right;
17849 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17850 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
17851 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17852 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
17853 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
17854 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17855 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17856 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
17857 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
17858 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17859 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17860 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17861
17862 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17863 Spaces.PointerAlignment = FormatStyle::PAS_Left;
17864 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17865 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
17866 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
17867 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
17868 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
17869 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17870 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17871 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17872 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
17873 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
17874 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
17875 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
17876 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17877
17878 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
17879 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
17880 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17881 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
17882 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
17883 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17884 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17885 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17886 }
17887
17888 TEST_F(FormatTest, AlignConsecutiveMacros) {
17889 FormatStyle Style = getLLVMStyle();
17890 Style.AlignConsecutiveAssignments.Enabled = true;
17891 Style.AlignConsecutiveDeclarations.Enabled = true;
17892
17893 verifyFormat("#define a 3\n"
17894 "#define bbbb 4\n"
17895 "#define ccc (5)",
17896 Style);
17897
17898 verifyFormat("#define f(x) (x * x)\n"
17899 "#define fff(x, y, z) (x * y + z)\n"
17900 "#define ffff(x, y) (x - y)",
17901 Style);
17902
17903 verifyFormat("#define foo(x, y) (x + y)\n"
17904 "#define bar (5, 6)(2 + 2)",
17905 Style);
17906
17907 verifyFormat("#define a 3\n"
17908 "#define bbbb 4\n"
17909 "#define ccc (5)\n"
17910 "#define f(x) (x * x)\n"
17911 "#define fff(x, y, z) (x * y + z)\n"
17912 "#define ffff(x, y) (x - y)",
17913 Style);
17914
17915 Style.AlignConsecutiveMacros.Enabled = true;
17916 verifyFormat("#define a 3\n"
17917 "#define bbbb 4\n"
17918 "#define ccc (5)",
17919 Style);
17920
17921 verifyFormat("#define true 1\n"
17922 "#define false 0",
17923 Style);
17924
17925 verifyFormat("#define f(x) (x * x)\n"
17926 "#define fff(x, y, z) (x * y + z)\n"
17927 "#define ffff(x, y) (x - y)",
17928 Style);
17929
17930 verifyFormat("#define foo(x, y) (x + y)\n"
17931 "#define bar (5, 6)(2 + 2)",
17932 Style);
17933
17934 verifyFormat("#define a 3\n"
17935 "#define bbbb 4\n"
17936 "#define ccc (5)\n"
17937 "#define f(x) (x * x)\n"
17938 "#define fff(x, y, z) (x * y + z)\n"
17939 "#define ffff(x, y) (x - y)",
17940 Style);
17941
17942 verifyFormat("#define a 5\n"
17943 "#define foo(x, y) (x + y)\n"
17944 "#define CCC (6)\n"
17945 "auto lambda = []() {\n"
17946 " auto ii = 0;\n"
17947 " float j = 0;\n"
17948 " return 0;\n"
17949 "};\n"
17950 "int i = 0;\n"
17951 "float i2 = 0;\n"
17952 "auto v = type{\n"
17953 " i = 1, //\n"
17954 " (i = 2), //\n"
17955 " i = 3 //\n"
17956 "};",
17957 Style);
17958
17959 Style.AlignConsecutiveMacros.Enabled = false;
17960 Style.ColumnLimit = 20;
17961
17962 verifyFormat("#define a \\\n"
17963 " \"aabbbbbbbbbbbb\"\n"
17964 "#define D \\\n"
17965 " \"aabbbbbbbbbbbb\" \\\n"
17966 " \"ccddeeeeeeeee\"\n"
17967 "#define B \\\n"
17968 " \"QQQQQQQQQQQQQ\" \\\n"
17969 " \"FFFFFFFFFFFFF\" \\\n"
17970 " \"LLLLLLLL\"",
17971 Style);
17972
17973 Style.AlignConsecutiveMacros.Enabled = true;
17974 verifyFormat("#define a \\\n"
17975 " \"aabbbbbbbbbbbb\"\n"
17976 "#define D \\\n"
17977 " \"aabbbbbbbbbbbb\" \\\n"
17978 " \"ccddeeeeeeeee\"\n"
17979 "#define B \\\n"
17980 " \"QQQQQQQQQQQQQ\" \\\n"
17981 " \"FFFFFFFFFFFFF\" \\\n"
17982 " \"LLLLLLLL\"",
17983 Style);
17984
17985 // Test across comments
17986 Style.MaxEmptyLinesToKeep = 10;
17987 Style.ReflowComments = false;
17988 Style.AlignConsecutiveMacros.AcrossComments = true;
17989 verifyFormat("#define a 3\n"
17990 "// line comment\n"
17991 "#define bbbb 4\n"
17992 "#define ccc (5)",
17993 "#define a 3\n"
17994 "// line comment\n"
17995 "#define bbbb 4\n"
17996 "#define ccc (5)",
17997 Style);
17998
17999 verifyFormat("#define a 3\n"
18000 "/* block comment */\n"
18001 "#define bbbb 4\n"
18002 "#define ccc (5)",
18003 "#define a 3\n"
18004 "/* block comment */\n"
18005 "#define bbbb 4\n"
18006 "#define ccc (5)",
18007 Style);
18008
18009 verifyFormat("#define a 3\n"
18010 "/* multi-line *\n"
18011 " * block comment */\n"
18012 "#define bbbb 4\n"
18013 "#define ccc (5)",
18014 "#define a 3\n"
18015 "/* multi-line *\n"
18016 " * block comment */\n"
18017 "#define bbbb 4\n"
18018 "#define ccc (5)",
18019 Style);
18020
18021 verifyFormat("#define a 3\n"
18022 "// multi-line line comment\n"
18023 "//\n"
18024 "#define bbbb 4\n"
18025 "#define ccc (5)",
18026 "#define a 3\n"
18027 "// multi-line line comment\n"
18028 "//\n"
18029 "#define bbbb 4\n"
18030 "#define ccc (5)",
18031 Style);
18032
18033 verifyFormat("#define a 3\n"
18034 "// empty lines still break.\n"
18035 "\n"
18036 "#define bbbb 4\n"
18037 "#define ccc (5)",
18038 "#define a 3\n"
18039 "// empty lines still break.\n"
18040 "\n"
18041 "#define bbbb 4\n"
18042 "#define ccc (5)",
18043 Style);
18044
18045 // Test across empty lines
18046 Style.AlignConsecutiveMacros.AcrossComments = false;
18047 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
18048 verifyFormat("#define a 3\n"
18049 "\n"
18050 "#define bbbb 4\n"
18051 "#define ccc (5)",
18052 "#define a 3\n"
18053 "\n"
18054 "#define bbbb 4\n"
18055 "#define ccc (5)",
18056 Style);
18057
18058 verifyFormat("#define a 3\n"
18059 "\n"
18060 "\n"
18061 "\n"
18062 "#define bbbb 4\n"
18063 "#define ccc (5)",
18064 "#define a 3\n"
18065 "\n"
18066 "\n"
18067 "\n"
18068 "#define bbbb 4\n"
18069 "#define ccc (5)",
18070 Style);
18071
18072 verifyFormat("#define a 3\n"
18073 "// comments should break alignment\n"
18074 "//\n"
18075 "#define bbbb 4\n"
18076 "#define ccc (5)",
18077 "#define a 3\n"
18078 "// comments should break alignment\n"
18079 "//\n"
18080 "#define bbbb 4\n"
18081 "#define ccc (5)",
18082 Style);
18083
18084 // Test across empty lines and comments
18085 Style.AlignConsecutiveMacros.AcrossComments = true;
18086 verifyFormat("#define a 3\n"
18087 "\n"
18088 "// line comment\n"
18089 "#define bbbb 4\n"
18090 "#define ccc (5)",
18091 Style);
18092
18093 verifyFormat("#define a 3\n"
18094 "\n"
18095 "\n"
18096 "/* multi-line *\n"
18097 " * block comment */\n"
18098 "\n"
18099 "\n"
18100 "#define bbbb 4\n"
18101 "#define ccc (5)",
18102 "#define a 3\n"
18103 "\n"
18104 "\n"
18105 "/* multi-line *\n"
18106 " * block comment */\n"
18107 "\n"
18108 "\n"
18109 "#define bbbb 4\n"
18110 "#define ccc (5)",
18111 Style);
18112
18113 verifyFormat("#define a 3\n"
18114 "\n"
18115 "\n"
18116 "/* multi-line *\n"
18117 " * block comment */\n"
18118 "\n"
18119 "\n"
18120 "#define bbbb 4\n"
18121 "#define ccc (5)",
18122 "#define a 3\n"
18123 "\n"
18124 "\n"
18125 "/* multi-line *\n"
18126 " * block comment */\n"
18127 "\n"
18128 "\n"
18129 "#define bbbb 4\n"
18130 "#define ccc (5)",
18131 Style);
18132 }
18133
18134 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
18135 FormatStyle Alignment = getLLVMStyle();
18136 Alignment.AlignConsecutiveMacros.Enabled = true;
18137 Alignment.AlignConsecutiveAssignments.Enabled = true;
18138 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18139
18140 Alignment.MaxEmptyLinesToKeep = 10;
18141 /* Test alignment across empty lines */
18142 verifyFormat("int a = 5;\n"
18143 "\n"
18144 "int oneTwoThree = 123;",
18145 "int a = 5;\n"
18146 "\n"
18147 "int oneTwoThree= 123;",
18148 Alignment);
18149 verifyFormat("int a = 5;\n"
18150 "int one = 1;\n"
18151 "\n"
18152 "int oneTwoThree = 123;",
18153 "int a = 5;\n"
18154 "int one = 1;\n"
18155 "\n"
18156 "int oneTwoThree = 123;",
18157 Alignment);
18158 verifyFormat("int a = 5;\n"
18159 "int one = 1;\n"
18160 "\n"
18161 "int oneTwoThree = 123;\n"
18162 "int oneTwo = 12;",
18163 "int a = 5;\n"
18164 "int one = 1;\n"
18165 "\n"
18166 "int oneTwoThree = 123;\n"
18167 "int oneTwo = 12;",
18168 Alignment);
18169
18170 /* Test across comments */
18171 verifyFormat("int a = 5;\n"
18172 "/* block comment */\n"
18173 "int oneTwoThree = 123;",
18174 "int a = 5;\n"
18175 "/* block comment */\n"
18176 "int oneTwoThree=123;",
18177 Alignment);
18178
18179 verifyFormat("int a = 5;\n"
18180 "// line comment\n"
18181 "int oneTwoThree = 123;",
18182 "int a = 5;\n"
18183 "// line comment\n"
18184 "int oneTwoThree=123;",
18185 Alignment);
18186
18187 /* Test across comments and newlines */
18188 verifyFormat("int a = 5;\n"
18189 "\n"
18190 "/* block comment */\n"
18191 "int oneTwoThree = 123;",
18192 "int a = 5;\n"
18193 "\n"
18194 "/* block comment */\n"
18195 "int oneTwoThree=123;",
18196 Alignment);
18197
18198 verifyFormat("int a = 5;\n"
18199 "\n"
18200 "// line comment\n"
18201 "int oneTwoThree = 123;",
18202 "int a = 5;\n"
18203 "\n"
18204 "// line comment\n"
18205 "int oneTwoThree=123;",
18206 Alignment);
18207 }
18208
18209 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18210 FormatStyle Alignment = getLLVMStyle();
18211 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18212 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18213 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18214
18215 Alignment.MaxEmptyLinesToKeep = 10;
18216 /* Test alignment across empty lines */
18217 verifyFormat("int a = 5;\n"
18218 "\n"
18219 "float const oneTwoThree = 123;",
18220 "int a = 5;\n"
18221 "\n"
18222 "float const oneTwoThree = 123;",
18223 Alignment);
18224 verifyFormat("int a = 5;\n"
18225 "float const one = 1;\n"
18226 "\n"
18227 "int oneTwoThree = 123;",
18228 "int a = 5;\n"
18229 "float const one = 1;\n"
18230 "\n"
18231 "int oneTwoThree = 123;",
18232 Alignment);
18233
18234 /* Test across comments */
18235 verifyFormat("float const a = 5;\n"
18236 "/* block comment */\n"
18237 "int oneTwoThree = 123;",
18238 "float const a = 5;\n"
18239 "/* block comment */\n"
18240 "int oneTwoThree=123;",
18241 Alignment);
18242
18243 verifyFormat("float const a = 5;\n"
18244 "// line comment\n"
18245 "int oneTwoThree = 123;",
18246 "float const a = 5;\n"
18247 "// line comment\n"
18248 "int oneTwoThree=123;",
18249 Alignment);
18250
18251 /* Test across comments and newlines */
18252 verifyFormat("float const a = 5;\n"
18253 "\n"
18254 "/* block comment */\n"
18255 "int oneTwoThree = 123;",
18256 "float const a = 5;\n"
18257 "\n"
18258 "/* block comment */\n"
18259 "int oneTwoThree=123;",
18260 Alignment);
18261
18262 verifyFormat("float const a = 5;\n"
18263 "\n"
18264 "// line comment\n"
18265 "int oneTwoThree = 123;",
18266 "float const a = 5;\n"
18267 "\n"
18268 "// line comment\n"
18269 "int oneTwoThree=123;",
18270 Alignment);
18271 }
18272
18273 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18274 FormatStyle Alignment = getLLVMStyle();
18275 Alignment.AlignConsecutiveBitFields.Enabled = true;
18276 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18277 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18278
18279 Alignment.MaxEmptyLinesToKeep = 10;
18280 /* Test alignment across empty lines */
18281 verifyFormat("int a : 5;\n"
18282 "\n"
18283 "int longbitfield : 6;",
18284 "int a : 5;\n"
18285 "\n"
18286 "int longbitfield : 6;",
18287 Alignment);
18288 verifyFormat("int a : 5;\n"
18289 "int one : 1;\n"
18290 "\n"
18291 "int longbitfield : 6;",
18292 "int a : 5;\n"
18293 "int one : 1;\n"
18294 "\n"
18295 "int longbitfield : 6;",
18296 Alignment);
18297
18298 /* Test across comments */
18299 verifyFormat("int a : 5;\n"
18300 "/* block comment */\n"
18301 "int longbitfield : 6;",
18302 "int a : 5;\n"
18303 "/* block comment */\n"
18304 "int longbitfield : 6;",
18305 Alignment);
18306 verifyFormat("int a : 5;\n"
18307 "int one : 1;\n"
18308 "// line comment\n"
18309 "int longbitfield : 6;",
18310 "int a : 5;\n"
18311 "int one : 1;\n"
18312 "// line comment\n"
18313 "int longbitfield : 6;",
18314 Alignment);
18315
18316 /* Test across comments and newlines */
18317 verifyFormat("int a : 5;\n"
18318 "/* block comment */\n"
18319 "\n"
18320 "int longbitfield : 6;",
18321 "int a : 5;\n"
18322 "/* block comment */\n"
18323 "\n"
18324 "int longbitfield : 6;",
18325 Alignment);
18326 verifyFormat("int a : 5;\n"
18327 "int one : 1;\n"
18328 "\n"
18329 "// line comment\n"
18330 "\n"
18331 "int longbitfield : 6;",
18332 "int a : 5;\n"
18333 "int one : 1;\n"
18334 "\n"
18335 "// line comment \n"
18336 "\n"
18337 "int longbitfield : 6;",
18338 Alignment);
18339 }
18340
18341 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
18342 FormatStyle Alignment = getLLVMStyle();
18343 Alignment.AlignConsecutiveMacros.Enabled = true;
18344 Alignment.AlignConsecutiveAssignments.Enabled = true;
18345 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18346
18347 Alignment.MaxEmptyLinesToKeep = 10;
18348 /* Test alignment across empty lines */
18349 verifyFormat("int a = 5;\n"
18350 "\n"
18351 "int oneTwoThree = 123;",
18352 "int a = 5;\n"
18353 "\n"
18354 "int oneTwoThree= 123;",
18355 Alignment);
18356 verifyFormat("int a = 5;\n"
18357 "int one = 1;\n"
18358 "\n"
18359 "int oneTwoThree = 123;",
18360 "int a = 5;\n"
18361 "int one = 1;\n"
18362 "\n"
18363 "int oneTwoThree = 123;",
18364 Alignment);
18365
18366 /* Test across comments */
18367 verifyFormat("int a = 5;\n"
18368 "/* block comment */\n"
18369 "int oneTwoThree = 123;",
18370 "int a = 5;\n"
18371 "/* block comment */\n"
18372 "int oneTwoThree=123;",
18373 Alignment);
18374
18375 verifyFormat("int a = 5;\n"
18376 "// line comment\n"
18377 "int oneTwoThree = 123;",
18378 "int a = 5;\n"
18379 "// line comment\n"
18380 "int oneTwoThree=123;",
18381 Alignment);
18382
18383 verifyFormat("int a = 5;\n"
18384 "/*\n"
18385 " * multi-line block comment\n"
18386 " */\n"
18387 "int oneTwoThree = 123;",
18388 "int a = 5;\n"
18389 "/*\n"
18390 " * multi-line block comment\n"
18391 " */\n"
18392 "int oneTwoThree=123;",
18393 Alignment);
18394
18395 verifyFormat("int a = 5;\n"
18396 "//\n"
18397 "// multi-line line comment\n"
18398 "//\n"
18399 "int oneTwoThree = 123;",
18400 "int a = 5;\n"
18401 "//\n"
18402 "// multi-line line comment\n"
18403 "//\n"
18404 "int oneTwoThree=123;",
18405 Alignment);
18406
18407 /* Test across comments and newlines */
18408 verifyFormat("int a = 5;\n"
18409 "\n"
18410 "/* block comment */\n"
18411 "int oneTwoThree = 123;",
18412 "int a = 5;\n"
18413 "\n"
18414 "/* block comment */\n"
18415 "int oneTwoThree=123;",
18416 Alignment);
18417
18418 verifyFormat("int a = 5;\n"
18419 "\n"
18420 "// line comment\n"
18421 "int oneTwoThree = 123;",
18422 "int a = 5;\n"
18423 "\n"
18424 "// line comment\n"
18425 "int oneTwoThree=123;",
18426 Alignment);
18427 }
18428
18429 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
18430 FormatStyle Alignment = getLLVMStyle();
18431 Alignment.AlignConsecutiveMacros.Enabled = true;
18432 Alignment.AlignConsecutiveAssignments.Enabled = true;
18433 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18434 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18435 verifyFormat("int a = 5;\n"
18436 "int oneTwoThree = 123;",
18437 Alignment);
18438 verifyFormat("int a = method();\n"
18439 "int oneTwoThree = 133;",
18440 Alignment);
18441 verifyFormat("a &= 5;\n"
18442 "bcd *= 5;\n"
18443 "ghtyf += 5;\n"
18444 "dvfvdb -= 5;\n"
18445 "a /= 5;\n"
18446 "vdsvsv %= 5;\n"
18447 "sfdbddfbdfbb ^= 5;\n"
18448 "dvsdsv |= 5;\n"
18449 "int dsvvdvsdvvv = 123;",
18450 Alignment);
18451 verifyFormat("int i = 1, j = 10;\n"
18452 "something = 2000;",
18453 Alignment);
18454 verifyFormat("something = 2000;\n"
18455 "int i = 1, j = 10;",
18456 Alignment);
18457 verifyFormat("something = 2000;\n"
18458 "another = 911;\n"
18459 "int i = 1, j = 10;\n"
18460 "oneMore = 1;\n"
18461 "i = 2;",
18462 Alignment);
18463 verifyFormat("int a = 5;\n"
18464 "int one = 1;\n"
18465 "method();\n"
18466 "int oneTwoThree = 123;\n"
18467 "int oneTwo = 12;",
18468 Alignment);
18469 verifyFormat("int oneTwoThree = 123;\n"
18470 "int oneTwo = 12;\n"
18471 "method();",
18472 Alignment);
18473 verifyFormat("int oneTwoThree = 123; // comment\n"
18474 "int oneTwo = 12; // comment",
18475 Alignment);
18476
18477 // Bug 25167
18478 /* Uncomment when fixed
18479 verifyFormat("#if A\n"
18480 "#else\n"
18481 "int aaaaaaaa = 12;\n"
18482 "#endif\n"
18483 "#if B\n"
18484 "#else\n"
18485 "int a = 12;\n"
18486 "#endif",
18487 Alignment);
18488 verifyFormat("enum foo {\n"
18489 "#if A\n"
18490 "#else\n"
18491 " aaaaaaaa = 12;\n"
18492 "#endif\n"
18493 "#if B\n"
18494 "#else\n"
18495 " a = 12;\n"
18496 "#endif\n"
18497 "};",
18498 Alignment);
18499 */
18500
18501 Alignment.MaxEmptyLinesToKeep = 10;
18502 /* Test alignment across empty lines */
18503 verifyFormat("int a = 5;\n"
18504 "\n"
18505 "int oneTwoThree = 123;",
18506 "int a = 5;\n"
18507 "\n"
18508 "int oneTwoThree= 123;",
18509 Alignment);
18510 verifyFormat("int a = 5;\n"
18511 "int one = 1;\n"
18512 "\n"
18513 "int oneTwoThree = 123;",
18514 "int a = 5;\n"
18515 "int one = 1;\n"
18516 "\n"
18517 "int oneTwoThree = 123;",
18518 Alignment);
18519 verifyFormat("int a = 5;\n"
18520 "int one = 1;\n"
18521 "\n"
18522 "int oneTwoThree = 123;\n"
18523 "int oneTwo = 12;",
18524 "int a = 5;\n"
18525 "int one = 1;\n"
18526 "\n"
18527 "int oneTwoThree = 123;\n"
18528 "int oneTwo = 12;",
18529 Alignment);
18530
18531 /* Test across comments */
18532 verifyFormat("int a = 5;\n"
18533 "/* block comment */\n"
18534 "int oneTwoThree = 123;",
18535 "int a = 5;\n"
18536 "/* block comment */\n"
18537 "int oneTwoThree=123;",
18538 Alignment);
18539
18540 verifyFormat("int a = 5;\n"
18541 "// line comment\n"
18542 "int oneTwoThree = 123;",
18543 "int a = 5;\n"
18544 "// line comment\n"
18545 "int oneTwoThree=123;",
18546 Alignment);
18547
18548 /* Test across comments and newlines */
18549 verifyFormat("int a = 5;\n"
18550 "\n"
18551 "/* block comment */\n"
18552 "int oneTwoThree = 123;",
18553 "int a = 5;\n"
18554 "\n"
18555 "/* block comment */\n"
18556 "int oneTwoThree=123;",
18557 Alignment);
18558
18559 verifyFormat("int a = 5;\n"
18560 "\n"
18561 "// line comment\n"
18562 "int oneTwoThree = 123;",
18563 "int a = 5;\n"
18564 "\n"
18565 "// line comment\n"
18566 "int oneTwoThree=123;",
18567 Alignment);
18568
18569 verifyFormat("int a = 5;\n"
18570 "//\n"
18571 "// multi-line line comment\n"
18572 "//\n"
18573 "int oneTwoThree = 123;",
18574 "int a = 5;\n"
18575 "//\n"
18576 "// multi-line line comment\n"
18577 "//\n"
18578 "int oneTwoThree=123;",
18579 Alignment);
18580
18581 verifyFormat("int a = 5;\n"
18582 "/*\n"
18583 " * multi-line block comment\n"
18584 " */\n"
18585 "int oneTwoThree = 123;",
18586 "int a = 5;\n"
18587 "/*\n"
18588 " * multi-line block comment\n"
18589 " */\n"
18590 "int oneTwoThree=123;",
18591 Alignment);
18592
18593 verifyFormat("int a = 5;\n"
18594 "\n"
18595 "/* block comment */\n"
18596 "\n"
18597 "\n"
18598 "\n"
18599 "int oneTwoThree = 123;",
18600 "int a = 5;\n"
18601 "\n"
18602 "/* block comment */\n"
18603 "\n"
18604 "\n"
18605 "\n"
18606 "int oneTwoThree=123;",
18607 Alignment);
18608
18609 verifyFormat("int a = 5;\n"
18610 "\n"
18611 "// line comment\n"
18612 "\n"
18613 "\n"
18614 "\n"
18615 "int oneTwoThree = 123;",
18616 "int a = 5;\n"
18617 "\n"
18618 "// line comment\n"
18619 "\n"
18620 "\n"
18621 "\n"
18622 "int oneTwoThree=123;",
18623 Alignment);
18624
18625 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18626 verifyFormat("#define A \\\n"
18627 " int aaaa = 12; \\\n"
18628 " int b = 23; \\\n"
18629 " int ccc = 234; \\\n"
18630 " int dddddddddd = 2345;",
18631 Alignment);
18632 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18633 verifyFormat("#define A \\\n"
18634 " int aaaa = 12; \\\n"
18635 " int b = 23; \\\n"
18636 " int ccc = 234; \\\n"
18637 " int dddddddddd = 2345;",
18638 Alignment);
18639 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18640 verifyFormat("#define A "
18641 " \\\n"
18642 " int aaaa = 12; "
18643 " \\\n"
18644 " int b = 23; "
18645 " \\\n"
18646 " int ccc = 234; "
18647 " \\\n"
18648 " int dddddddddd = 2345;",
18649 Alignment);
18650 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18651 "k = 4, int l = 5,\n"
18652 " int m = 6) {\n"
18653 " int j = 10;\n"
18654 " otherThing = 1;\n"
18655 "}",
18656 Alignment);
18657 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18658 " int i = 1;\n"
18659 " int j = 2;\n"
18660 " int big = 10000;\n"
18661 "}",
18662 Alignment);
18663 verifyFormat("class C {\n"
18664 "public:\n"
18665 " int i = 1;\n"
18666 " virtual void f() = 0;\n"
18667 "};",
18668 Alignment);
18669 verifyFormat("int i = 1;\n"
18670 "if (SomeType t = getSomething()) {\n"
18671 "}\n"
18672 "int j = 2;\n"
18673 "int big = 10000;",
18674 Alignment);
18675 verifyFormat("int j = 7;\n"
18676 "for (int k = 0; k < N; ++k) {\n"
18677 "}\n"
18678 "int j = 2;\n"
18679 "int big = 10000;\n"
18680 "}",
18681 Alignment);
18682 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18683 verifyFormat("int i = 1;\n"
18684 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18685 " = someLooooooooooooooooongFunction();\n"
18686 "int j = 2;",
18687 Alignment);
18688 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18689 verifyFormat("int i = 1;\n"
18690 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18691 " someLooooooooooooooooongFunction();\n"
18692 "int j = 2;",
18693 Alignment);
18694
18695 verifyFormat("auto lambda = []() {\n"
18696 " auto i = 0;\n"
18697 " return 0;\n"
18698 "};\n"
18699 "int i = 0;\n"
18700 "auto v = type{\n"
18701 " i = 1, //\n"
18702 " (i = 2), //\n"
18703 " i = 3 //\n"
18704 "};",
18705 Alignment);
18706
18707 verifyFormat(
18708 "int i = 1;\n"
18709 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18710 " loooooooooooooooooooooongParameterB);\n"
18711 "int j = 2;",
18712 Alignment);
18713
18714 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18715 " typename B = very_long_type_name_1,\n"
18716 " typename T_2 = very_long_type_name_2>\n"
18717 "auto foo() {}",
18718 Alignment);
18719 verifyFormat("int a, b = 1;\n"
18720 "int c = 2;\n"
18721 "int dd = 3;",
18722 Alignment);
18723 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18724 "float b[1][] = {{3.f}};",
18725 Alignment);
18726 verifyFormat("for (int i = 0; i < 1; i++)\n"
18727 " int x = 1;",
18728 Alignment);
18729 verifyFormat("for (i = 0; i < 1; i++)\n"
18730 " x = 1;\n"
18731 "y = 1;",
18732 Alignment);
18733
18734 Alignment.ReflowComments = true;
18735 Alignment.ColumnLimit = 50;
18736 verifyFormat("int x = 0;\n"
18737 "int yy = 1; /// specificlennospace\n"
18738 "int zzz = 2;",
18739 "int x = 0;\n"
18740 "int yy = 1; ///specificlennospace\n"
18741 "int zzz = 2;",
18742 Alignment);
18743 }
18744
18745 TEST_F(FormatTest, AlignCompoundAssignments) {
18746 FormatStyle Alignment = getLLVMStyle();
18747 Alignment.AlignConsecutiveAssignments.Enabled = true;
18748 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
18749 Alignment.AlignConsecutiveAssignments.PadOperators = false;
18750 verifyFormat("sfdbddfbdfbb = 5;\n"
18751 "dvsdsv = 5;\n"
18752 "int dsvvdvsdvvv = 123;",
18753 Alignment);
18754 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18755 "dvsdsv |= 5;\n"
18756 "int dsvvdvsdvvv = 123;",
18757 Alignment);
18758 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18759 "dvsdsv <<= 5;\n"
18760 "int dsvvdvsdvvv = 123;",
18761 Alignment);
18762 verifyFormat("int xxx = 5;\n"
18763 "xxx = 5;\n"
18764 "{\n"
18765 " int yyy = 6;\n"
18766 " yyy = 6;\n"
18767 "}",
18768 Alignment);
18769 verifyFormat("int xxx = 5;\n"
18770 "xxx += 5;\n"
18771 "{\n"
18772 " int yyy = 6;\n"
18773 " yyy += 6;\n"
18774 "}",
18775 Alignment);
18776 // Test that `<=` is not treated as a compound assignment.
18777 verifyFormat("aa &= 5;\n"
18778 "b <= 10;\n"
18779 "c = 15;",
18780 Alignment);
18781 Alignment.AlignConsecutiveAssignments.PadOperators = true;
18782 verifyFormat("sfdbddfbdfbb = 5;\n"
18783 "dvsdsv = 5;\n"
18784 "int dsvvdvsdvvv = 123;",
18785 Alignment);
18786 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18787 "dvsdsv |= 5;\n"
18788 "int dsvvdvsdvvv = 123;",
18789 Alignment);
18790 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18791 "dvsdsv <<= 5;\n"
18792 "int dsvvdvsdvvv = 123;",
18793 Alignment);
18794 verifyFormat("a += 5;\n"
18795 "one = 1;\n"
18796 "\n"
18797 "oneTwoThree = 123;",
18798 "a += 5;\n"
18799 "one = 1;\n"
18800 "\n"
18801 "oneTwoThree = 123;",
18802 Alignment);
18803 verifyFormat("a += 5;\n"
18804 "one = 1;\n"
18805 "//\n"
18806 "oneTwoThree = 123;",
18807 "a += 5;\n"
18808 "one = 1;\n"
18809 "//\n"
18810 "oneTwoThree = 123;",
18811 Alignment);
18812 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18813 verifyFormat("a += 5;\n"
18814 "one = 1;\n"
18815 "\n"
18816 "oneTwoThree = 123;",
18817 "a += 5;\n"
18818 "one = 1;\n"
18819 "\n"
18820 "oneTwoThree = 123;",
18821 Alignment);
18822 verifyFormat("a += 5;\n"
18823 "one = 1;\n"
18824 "//\n"
18825 "oneTwoThree = 123;",
18826 "a += 5;\n"
18827 "one = 1;\n"
18828 "//\n"
18829 "oneTwoThree = 123;",
18830 Alignment);
18831 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
18832 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18833 verifyFormat("a += 5;\n"
18834 "one = 1;\n"
18835 "\n"
18836 "oneTwoThree = 123;",
18837 "a += 5;\n"
18838 "one = 1;\n"
18839 "\n"
18840 "oneTwoThree = 123;",
18841 Alignment);
18842 verifyFormat("a += 5;\n"
18843 "one = 1;\n"
18844 "//\n"
18845 "oneTwoThree = 123;",
18846 "a += 5;\n"
18847 "one = 1;\n"
18848 "//\n"
18849 "oneTwoThree = 123;",
18850 Alignment);
18851 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18852 verifyFormat("a += 5;\n"
18853 "one >>= 1;\n"
18854 "\n"
18855 "oneTwoThree = 123;",
18856 "a += 5;\n"
18857 "one >>= 1;\n"
18858 "\n"
18859 "oneTwoThree = 123;",
18860 Alignment);
18861 verifyFormat("a += 5;\n"
18862 "one = 1;\n"
18863 "//\n"
18864 "oneTwoThree <<= 123;",
18865 "a += 5;\n"
18866 "one = 1;\n"
18867 "//\n"
18868 "oneTwoThree <<= 123;",
18869 Alignment);
18870 }
18871
18872 TEST_F(FormatTest, AlignConsecutiveAssignments) {
18873 FormatStyle Alignment = getLLVMStyle();
18874 Alignment.AlignConsecutiveMacros.Enabled = true;
18875 verifyFormat("int a = 5;\n"
18876 "int oneTwoThree = 123;",
18877 Alignment);
18878 verifyFormat("int a = 5;\n"
18879 "int oneTwoThree = 123;",
18880 Alignment);
18881
18882 Alignment.AlignConsecutiveAssignments.Enabled = true;
18883 verifyFormat("int a = 5;\n"
18884 "int oneTwoThree = 123;",
18885 Alignment);
18886 verifyFormat("int a = method();\n"
18887 "int oneTwoThree = 133;",
18888 Alignment);
18889 verifyFormat("aa <= 5;\n"
18890 "a &= 5;\n"
18891 "bcd *= 5;\n"
18892 "ghtyf += 5;\n"
18893 "dvfvdb -= 5;\n"
18894 "a /= 5;\n"
18895 "vdsvsv %= 5;\n"
18896 "sfdbddfbdfbb ^= 5;\n"
18897 "dvsdsv |= 5;\n"
18898 "int dsvvdvsdvvv = 123;",
18899 Alignment);
18900 verifyFormat("int i = 1, j = 10;\n"
18901 "something = 2000;",
18902 Alignment);
18903 verifyFormat("something = 2000;\n"
18904 "int i = 1, j = 10;",
18905 Alignment);
18906 verifyFormat("something = 2000;\n"
18907 "another = 911;\n"
18908 "int i = 1, j = 10;\n"
18909 "oneMore = 1;\n"
18910 "i = 2;",
18911 Alignment);
18912 verifyFormat("int a = 5;\n"
18913 "int one = 1;\n"
18914 "method();\n"
18915 "int oneTwoThree = 123;\n"
18916 "int oneTwo = 12;",
18917 Alignment);
18918 verifyFormat("int oneTwoThree = 123;\n"
18919 "int oneTwo = 12;\n"
18920 "method();",
18921 Alignment);
18922 verifyFormat("int oneTwoThree = 123; // comment\n"
18923 "int oneTwo = 12; // comment",
18924 Alignment);
18925 verifyFormat("int f() = default;\n"
18926 "int &operator() = default;\n"
18927 "int &operator=() {",
18928 Alignment);
18929 verifyFormat("int f() = delete;\n"
18930 "int &operator() = delete;\n"
18931 "int &operator=() {",
18932 Alignment);
18933 verifyFormat("int f() = default; // comment\n"
18934 "int &operator() = default; // comment\n"
18935 "int &operator=() {",
18936 Alignment);
18937 verifyFormat("int f() = default;\n"
18938 "int &operator() = default;\n"
18939 "int &operator==() {",
18940 Alignment);
18941 verifyFormat("int f() = default;\n"
18942 "int &operator() = default;\n"
18943 "int &operator<=() {",
18944 Alignment);
18945 verifyFormat("int f() = default;\n"
18946 "int &operator() = default;\n"
18947 "int &operator!=() {",
18948 Alignment);
18949 verifyFormat("int f() = default;\n"
18950 "int &operator() = default;\n"
18951 "int &operator=();",
18952 Alignment);
18953 verifyFormat("int f() = delete;\n"
18954 "int &operator() = delete;\n"
18955 "int &operator=();",
18956 Alignment);
18957 verifyFormat("/* long long padding */ int f() = default;\n"
18958 "int &operator() = default;\n"
18959 "int &operator/**/ =();",
18960 Alignment);
18961 // https://llvm.org/PR33697
18962 FormatStyle AlignmentWithPenalty = getLLVMStyle();
18963 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
18964 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
18965 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
18966 " void f() = delete;\n"
18967 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
18968 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
18969 "};",
18970 AlignmentWithPenalty);
18971
18972 // Bug 25167
18973 /* Uncomment when fixed
18974 verifyFormat("#if A\n"
18975 "#else\n"
18976 "int aaaaaaaa = 12;\n"
18977 "#endif\n"
18978 "#if B\n"
18979 "#else\n"
18980 "int a = 12;\n"
18981 "#endif",
18982 Alignment);
18983 verifyFormat("enum foo {\n"
18984 "#if A\n"
18985 "#else\n"
18986 " aaaaaaaa = 12;\n"
18987 "#endif\n"
18988 "#if B\n"
18989 "#else\n"
18990 " a = 12;\n"
18991 "#endif\n"
18992 "};",
18993 Alignment);
18994 */
18995
18996 verifyFormat("int a = 5;\n"
18997 "\n"
18998 "int oneTwoThree = 123;",
18999 "int a = 5;\n"
19000 "\n"
19001 "int oneTwoThree= 123;",
19002 Alignment);
19003 verifyFormat("int a = 5;\n"
19004 "int one = 1;\n"
19005 "\n"
19006 "int oneTwoThree = 123;",
19007 "int a = 5;\n"
19008 "int one = 1;\n"
19009 "\n"
19010 "int oneTwoThree = 123;",
19011 Alignment);
19012 verifyFormat("int a = 5;\n"
19013 "int one = 1;\n"
19014 "\n"
19015 "int oneTwoThree = 123;\n"
19016 "int oneTwo = 12;",
19017 "int a = 5;\n"
19018 "int one = 1;\n"
19019 "\n"
19020 "int oneTwoThree = 123;\n"
19021 "int oneTwo = 12;",
19022 Alignment);
19023 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19024 verifyFormat("#define A \\\n"
19025 " int aaaa = 12; \\\n"
19026 " int b = 23; \\\n"
19027 " int ccc = 234; \\\n"
19028 " int dddddddddd = 2345;",
19029 Alignment);
19030 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19031 verifyFormat("#define A \\\n"
19032 " int aaaa = 12; \\\n"
19033 " int b = 23; \\\n"
19034 " int ccc = 234; \\\n"
19035 " int dddddddddd = 2345;",
19036 Alignment);
19037 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19038 verifyFormat("#define A "
19039 " \\\n"
19040 " int aaaa = 12; "
19041 " \\\n"
19042 " int b = 23; "
19043 " \\\n"
19044 " int ccc = 234; "
19045 " \\\n"
19046 " int dddddddddd = 2345;",
19047 Alignment);
19048 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19049 "k = 4, int l = 5,\n"
19050 " int m = 6) {\n"
19051 " int j = 10;\n"
19052 " otherThing = 1;\n"
19053 "}",
19054 Alignment);
19055 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19056 " int i = 1;\n"
19057 " int j = 2;\n"
19058 " int big = 10000;\n"
19059 "}",
19060 Alignment);
19061 verifyFormat("class C {\n"
19062 "public:\n"
19063 " int i = 1;\n"
19064 " virtual void f() = 0;\n"
19065 "};",
19066 Alignment);
19067 verifyFormat("int i = 1;\n"
19068 "if (SomeType t = getSomething()) {\n"
19069 "}\n"
19070 "int j = 2;\n"
19071 "int big = 10000;",
19072 Alignment);
19073 verifyFormat("int j = 7;\n"
19074 "for (int k = 0; k < N; ++k) {\n"
19075 "}\n"
19076 "int j = 2;\n"
19077 "int big = 10000;\n"
19078 "}",
19079 Alignment);
19080 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19081 verifyFormat("int i = 1;\n"
19082 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19083 " = someLooooooooooooooooongFunction();\n"
19084 "int j = 2;",
19085 Alignment);
19086 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19087 verifyFormat("int i = 1;\n"
19088 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19089 " someLooooooooooooooooongFunction();\n"
19090 "int j = 2;",
19091 Alignment);
19092
19093 verifyFormat("auto lambda = []() {\n"
19094 " auto i = 0;\n"
19095 " return 0;\n"
19096 "};\n"
19097 "int i = 0;\n"
19098 "auto v = type{\n"
19099 " i = 1, //\n"
19100 " (i = 2), //\n"
19101 " i = 3 //\n"
19102 "};",
19103 Alignment);
19104
19105 verifyFormat(
19106 "int i = 1;\n"
19107 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19108 " loooooooooooooooooooooongParameterB);\n"
19109 "int j = 2;",
19110 Alignment);
19111
19112 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19113 " typename B = very_long_type_name_1,\n"
19114 " typename T_2 = very_long_type_name_2>\n"
19115 "auto foo() {}",
19116 Alignment);
19117 verifyFormat("int a, b = 1;\n"
19118 "int c = 2;\n"
19119 "int dd = 3;",
19120 Alignment);
19121 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19122 "float b[1][] = {{3.f}};",
19123 Alignment);
19124 verifyFormat("for (int i = 0; i < 1; i++)\n"
19125 " int x = 1;",
19126 Alignment);
19127 verifyFormat("for (i = 0; i < 1; i++)\n"
19128 " x = 1;\n"
19129 "y = 1;",
19130 Alignment);
19131
19132 EXPECT_EQ(Alignment.ReflowComments, true);
19133 Alignment.ColumnLimit = 50;
19134 verifyFormat("int x = 0;\n"
19135 "int yy = 1; /// specificlennospace\n"
19136 "int zzz = 2;",
19137 "int x = 0;\n"
19138 "int yy = 1; ///specificlennospace\n"
19139 "int zzz = 2;",
19140 Alignment);
19141
19142 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19143 "auto b = [] {\n"
19144 " f();\n"
19145 " return;\n"
19146 "};",
19147 Alignment);
19148 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19149 "auto b = g([] {\n"
19150 " f();\n"
19151 " return;\n"
19152 "});",
19153 Alignment);
19154 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19155 "auto b = g(param, [] {\n"
19156 " f();\n"
19157 " return;\n"
19158 "});",
19159 Alignment);
19160 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19161 "auto b = [] {\n"
19162 " if (condition) {\n"
19163 " return;\n"
19164 " }\n"
19165 "};",
19166 Alignment);
19167
19168 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19169 " ccc ? aaaaa : bbbbb,\n"
19170 " dddddddddddddddddddddddddd);",
19171 Alignment);
19172 // FIXME: https://llvm.org/PR53497
19173 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19174 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19175 // " ccc ? aaaaa : bbbbb,\n"
19176 // " dddddddddddddddddddddddddd);",
19177 // Alignment);
19178
19179 // Confirm proper handling of AlignConsecutiveAssignments with
19180 // BinPackArguments.
19181 // See https://llvm.org/PR55360
19182 Alignment = getLLVMStyleWithColumns(50);
19183 Alignment.AlignConsecutiveAssignments.Enabled = true;
19184 Alignment.BinPackArguments = false;
19185 verifyFormat("int a_long_name = 1;\n"
19186 "auto b = B({a_long_name, a_long_name},\n"
19187 " {a_longer_name_for_wrap,\n"
19188 " a_longer_name_for_wrap});",
19189 Alignment);
19190 verifyFormat("int a_long_name = 1;\n"
19191 "auto b = B{{a_long_name, a_long_name},\n"
19192 " {a_longer_name_for_wrap,\n"
19193 " a_longer_name_for_wrap}};",
19194 Alignment);
19195
19196 Alignment = getLLVMStyleWithColumns(60);
19197 Alignment.AlignConsecutiveAssignments.Enabled = true;
19198 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19199 "using I = std::conditional_t<II::value >= 0,\n"
19200 " std::ic<int, II::value + 1>,\n"
19201 " std::ic<int, -1>>;",
19202 Alignment);
19203 verifyFormat("SomeName = Foo;\n"
19204 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19205 " arrrrrrrrrrg);",
19206 Alignment);
19207
19208 Alignment.ColumnLimit = 80;
19209 Alignment.SpacesInAngles = FormatStyle::SIAS_Always;
19210 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19211 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19212 Alignment);
19213 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19214 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19215 Alignment);
19216
19217 Alignment.SpacesInParens = FormatStyle::SIPO_Custom;
19218 Alignment.SpacesInParensOptions.InCStyleCasts = true;
19219 verifyFormat("void **ptr = ( void ** )unkn;\n"
19220 "ptr = ( void ** )ptr[0];",
19221 Alignment);
19222 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19223 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19224 Alignment);
19225 }
19226
19227 TEST_F(FormatTest, AlignConsecutiveBitFields) {
19228 FormatStyle Alignment = getLLVMStyle();
19229 Alignment.AlignConsecutiveBitFields.Enabled = true;
19230 verifyFormat("int const a : 5;\n"
19231 "int oneTwoThree : 23;",
19232 Alignment);
19233
19234 // Initializers are allowed starting with c++2a
19235 verifyFormat("int const a : 5 = 1;\n"
19236 "int oneTwoThree : 23 = 0;",
19237 Alignment);
19238
19239 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19240 verifyFormat("int const a : 5;\n"
19241 "int oneTwoThree : 23;",
19242 Alignment);
19243
19244 verifyFormat("int const a : 5; // comment\n"
19245 "int oneTwoThree : 23; // comment",
19246 Alignment);
19247
19248 verifyFormat("int const a : 5 = 1;\n"
19249 "int oneTwoThree : 23 = 0;",
19250 Alignment);
19251
19252 Alignment.AlignConsecutiveAssignments.Enabled = true;
19253 verifyFormat("int const a : 5 = 1;\n"
19254 "int oneTwoThree : 23 = 0;",
19255 Alignment);
19256 verifyFormat("int const a : 5 = {1};\n"
19257 "int oneTwoThree : 23 = 0;",
19258 Alignment);
19259
19260 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19261 verifyFormat("int const a :5;\n"
19262 "int oneTwoThree:23;",
19263 Alignment);
19264
19265 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19266 verifyFormat("int const a :5;\n"
19267 "int oneTwoThree :23;",
19268 Alignment);
19269
19270 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19271 verifyFormat("int const a : 5;\n"
19272 "int oneTwoThree: 23;",
19273 Alignment);
19274
19275 // Known limitations: ':' is only recognized as a bitfield colon when
19276 // followed by a number.
19277 /*
19278 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19279 "int a : 5;",
19280 Alignment);
19281 */
19282 }
19283
19284 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19285 FormatStyle Alignment = getLLVMStyle();
19286 Alignment.AlignConsecutiveMacros.Enabled = true;
19287 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19288 verifyFormat("float const a = 5;\n"
19289 "int oneTwoThree = 123;",
19290 Alignment);
19291 verifyFormat("int a = 5;\n"
19292 "float const oneTwoThree = 123;",
19293 Alignment);
19294
19295 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19296 verifyFormat("float const a = 5;\n"
19297 "int oneTwoThree = 123;",
19298 Alignment);
19299 verifyFormat("int a = method();\n"
19300 "float const oneTwoThree = 133;",
19301 Alignment);
19302 verifyFormat("int i = 1, j = 10;\n"
19303 "something = 2000;",
19304 Alignment);
19305 verifyFormat("something = 2000;\n"
19306 "int i = 1, j = 10;",
19307 Alignment);
19308 verifyFormat("float something = 2000;\n"
19309 "double another = 911;\n"
19310 "int i = 1, j = 10;\n"
19311 "const int *oneMore = 1;\n"
19312 "unsigned i = 2;",
19313 Alignment);
19314 verifyFormat("float a = 5;\n"
19315 "int one = 1;\n"
19316 "method();\n"
19317 "const double oneTwoThree = 123;\n"
19318 "const unsigned int oneTwo = 12;",
19319 Alignment);
19320 verifyFormat("int oneTwoThree{0}; // comment\n"
19321 "unsigned oneTwo; // comment",
19322 Alignment);
19323 verifyFormat("unsigned int *a;\n"
19324 "int *b;\n"
19325 "unsigned int Const *c;\n"
19326 "unsigned int const *d;\n"
19327 "unsigned int Const &e;\n"
19328 "unsigned int const &f;",
19329 Alignment);
19330 verifyFormat("Const unsigned int *c;\n"
19331 "const unsigned int *d;\n"
19332 "Const unsigned int &e;\n"
19333 "const unsigned int &f;\n"
19334 "const unsigned g;\n"
19335 "Const unsigned h;",
19336 Alignment);
19337 verifyFormat("float const a = 5;\n"
19338 "\n"
19339 "int oneTwoThree = 123;",
19340 "float const a = 5;\n"
19341 "\n"
19342 "int oneTwoThree= 123;",
19343 Alignment);
19344 verifyFormat("float a = 5;\n"
19345 "int one = 1;\n"
19346 "\n"
19347 "unsigned oneTwoThree = 123;",
19348 "float a = 5;\n"
19349 "int one = 1;\n"
19350 "\n"
19351 "unsigned oneTwoThree = 123;",
19352 Alignment);
19353 verifyFormat("float a = 5;\n"
19354 "int one = 1;\n"
19355 "\n"
19356 "unsigned oneTwoThree = 123;\n"
19357 "int oneTwo = 12;",
19358 "float a = 5;\n"
19359 "int one = 1;\n"
19360 "\n"
19361 "unsigned oneTwoThree = 123;\n"
19362 "int oneTwo = 12;",
19363 Alignment);
19364 // Function prototype alignment
19365 verifyFormat("int a();\n"
19366 "double b();",
19367 Alignment);
19368 verifyFormat("int a(int x);\n"
19369 "double b();",
19370 Alignment);
19371 verifyFormat("int a(const Test & = Test());\n"
19372 "int a1(int &foo, const Test & = Test());\n"
19373 "int a2(int &foo, const Test &name = Test());\n"
19374 "double b();",
19375 Alignment);
19376 verifyFormat("struct Test {\n"
19377 " Test(const Test &) = default;\n"
19378 " ~Test() = default;\n"
19379 " Test &operator=(const Test &) = default;\n"
19380 "};",
19381 Alignment);
19382 unsigned OldColumnLimit = Alignment.ColumnLimit;
19383 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19384 // otherwise the function parameters will be re-flowed onto a single line.
19385 Alignment.ColumnLimit = 0;
19386 verifyFormat("int a(int x,\n"
19387 " float y);\n"
19388 "double b(int x,\n"
19389 " double y);",
19390 "int a(int x,\n"
19391 " float y);\n"
19392 "double b(int x,\n"
19393 " double y);",
19394 Alignment);
19395 // This ensures that function parameters of function declarations are
19396 // correctly indented when their owning functions are indented.
19397 // The failure case here is for 'double y' to not be indented enough.
19398 verifyFormat("double a(int x);\n"
19399 "int b(int y,\n"
19400 " double z);",
19401 "double a(int x);\n"
19402 "int b(int y,\n"
19403 " double z);",
19404 Alignment);
19405 // Set ColumnLimit low so that we induce wrapping immediately after
19406 // the function name and opening paren.
19407 Alignment.ColumnLimit = 13;
19408 verifyFormat("int function(\n"
19409 " int x,\n"
19410 " bool y);",
19411 Alignment);
19412 // Set ColumnLimit low so that we break the argument list in multiple lines.
19413 Alignment.ColumnLimit = 35;
19414 verifyFormat("int a3(SomeTypeName1 &x,\n"
19415 " SomeTypeName2 &y,\n"
19416 " const Test & = Test());\n"
19417 "double b();",
19418 Alignment);
19419 Alignment.ColumnLimit = OldColumnLimit;
19420 // Ensure function pointers don't screw up recursive alignment
19421 verifyFormat("int a(int x, void (*fp)(int y));\n"
19422 "double b();",
19423 Alignment);
19424 Alignment.AlignConsecutiveAssignments.Enabled = true;
19425 verifyFormat("struct Test {\n"
19426 " Test(const Test &) = default;\n"
19427 " ~Test() = default;\n"
19428 " Test &operator=(const Test &) = default;\n"
19429 "};",
19430 Alignment);
19431 // Ensure recursive alignment is broken by function braces, so that the
19432 // "a = 1" does not align with subsequent assignments inside the function
19433 // body.
19434 verifyFormat("int func(int a = 1) {\n"
19435 " int b = 2;\n"
19436 " int cc = 3;\n"
19437 "}",
19438 Alignment);
19439 verifyFormat("float something = 2000;\n"
19440 "double another = 911;\n"
19441 "int i = 1, j = 10;\n"
19442 "const int *oneMore = 1;\n"
19443 "unsigned i = 2;",
19444 Alignment);
19445 verifyFormat("int oneTwoThree = {0}; // comment\n"
19446 "unsigned oneTwo = 0; // comment",
19447 Alignment);
19448 // Make sure that scope is correctly tracked, in the absence of braces
19449 verifyFormat("for (int i = 0; i < n; i++)\n"
19450 " j = i;\n"
19451 "double x = 1;",
19452 Alignment);
19453 verifyFormat("if (int i = 0)\n"
19454 " j = i;\n"
19455 "double x = 1;",
19456 Alignment);
19457 // Ensure operator[] and operator() are comprehended
19458 verifyFormat("struct test {\n"
19459 " long long int foo();\n"
19460 " int operator[](int a);\n"
19461 " double bar();\n"
19462 "};",
19463 Alignment);
19464 verifyFormat("struct test {\n"
19465 " long long int foo();\n"
19466 " int operator()(int a);\n"
19467 " double bar();\n"
19468 "};",
19469 Alignment);
19470 // http://llvm.org/PR52914
19471 verifyFormat("char *a[] = {\"a\", // comment\n"
19472 " \"bb\"};\n"
19473 "int bbbbbbb = 0;",
19474 Alignment);
19475 // http://llvm.org/PR68079
19476 verifyFormat("using Fn = int (A::*)();\n"
19477 "using RFn = int (A::*)() &;\n"
19478 "using RRFn = int (A::*)() &&;",
19479 Alignment);
19480 verifyFormat("using Fn = int (A::*)();\n"
19481 "using RFn = int *(A::*)() &;\n"
19482 "using RRFn = double (A::*)() &&;",
19483 Alignment);
19484
19485 // PAS_Right
19486 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19487 " int const i = 1;\n"
19488 " int *j = 2;\n"
19489 " int big = 10000;\n"
19490 "\n"
19491 " unsigned oneTwoThree = 123;\n"
19492 " int oneTwo = 12;\n"
19493 " method();\n"
19494 " float k = 2;\n"
19495 " int ll = 10000;\n"
19496 "}",
19497 "void SomeFunction(int parameter= 0) {\n"
19498 " int const i= 1;\n"
19499 " int *j=2;\n"
19500 " int big = 10000;\n"
19501 "\n"
19502 "unsigned oneTwoThree =123;\n"
19503 "int oneTwo = 12;\n"
19504 " method();\n"
19505 "float k= 2;\n"
19506 "int ll=10000;\n"
19507 "}",
19508 Alignment);
19509 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19510 " int const i = 1;\n"
19511 " int **j = 2, ***k;\n"
19512 " int &k = i;\n"
19513 " int &&l = i + j;\n"
19514 " int big = 10000;\n"
19515 "\n"
19516 " unsigned oneTwoThree = 123;\n"
19517 " int oneTwo = 12;\n"
19518 " method();\n"
19519 " float k = 2;\n"
19520 " int ll = 10000;\n"
19521 "}",
19522 "void SomeFunction(int parameter= 0) {\n"
19523 " int const i= 1;\n"
19524 " int **j=2,***k;\n"
19525 "int &k=i;\n"
19526 "int &&l=i+j;\n"
19527 " int big = 10000;\n"
19528 "\n"
19529 "unsigned oneTwoThree =123;\n"
19530 "int oneTwo = 12;\n"
19531 " method();\n"
19532 "float k= 2;\n"
19533 "int ll=10000;\n"
19534 "}",
19535 Alignment);
19536 // variables are aligned at their name, pointers are at the right most
19537 // position
19538 verifyFormat("int *a;\n"
19539 "int **b;\n"
19540 "int ***c;\n"
19541 "int foobar;",
19542 Alignment);
19543
19544 // PAS_Left
19545 FormatStyle AlignmentLeft = Alignment;
19546 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
19547 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19548 " int const i = 1;\n"
19549 " int* j = 2;\n"
19550 " int big = 10000;\n"
19551 "\n"
19552 " unsigned oneTwoThree = 123;\n"
19553 " int oneTwo = 12;\n"
19554 " method();\n"
19555 " float k = 2;\n"
19556 " int ll = 10000;\n"
19557 "}",
19558 "void SomeFunction(int parameter= 0) {\n"
19559 " int const i= 1;\n"
19560 " int *j=2;\n"
19561 " int big = 10000;\n"
19562 "\n"
19563 "unsigned oneTwoThree =123;\n"
19564 "int oneTwo = 12;\n"
19565 " method();\n"
19566 "float k= 2;\n"
19567 "int ll=10000;\n"
19568 "}",
19569 AlignmentLeft);
19570 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19571 " int const i = 1;\n"
19572 " int** j = 2;\n"
19573 " int& k = i;\n"
19574 " int&& l = i + j;\n"
19575 " int big = 10000;\n"
19576 "\n"
19577 " unsigned oneTwoThree = 123;\n"
19578 " int oneTwo = 12;\n"
19579 " method();\n"
19580 " float k = 2;\n"
19581 " int ll = 10000;\n"
19582 "}",
19583 "void SomeFunction(int parameter= 0) {\n"
19584 " int const i= 1;\n"
19585 " int **j=2;\n"
19586 "int &k=i;\n"
19587 "int &&l=i+j;\n"
19588 " int big = 10000;\n"
19589 "\n"
19590 "unsigned oneTwoThree =123;\n"
19591 "int oneTwo = 12;\n"
19592 " method();\n"
19593 "float k= 2;\n"
19594 "int ll=10000;\n"
19595 "}",
19596 AlignmentLeft);
19597 // variables are aligned at their name, pointers are at the left most position
19598 verifyFormat("int* a;\n"
19599 "int** b;\n"
19600 "int*** c;\n"
19601 "int foobar;",
19602 AlignmentLeft);
19603
19604 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19605 "double b();",
19606 AlignmentLeft);
19607
19608 // PAS_Middle
19609 FormatStyle AlignmentMiddle = Alignment;
19610 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
19611 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19612 " int const i = 1;\n"
19613 " int * j = 2;\n"
19614 " int big = 10000;\n"
19615 "\n"
19616 " unsigned oneTwoThree = 123;\n"
19617 " int oneTwo = 12;\n"
19618 " method();\n"
19619 " float k = 2;\n"
19620 " int ll = 10000;\n"
19621 "}",
19622 "void SomeFunction(int parameter= 0) {\n"
19623 " int const i= 1;\n"
19624 " int *j=2;\n"
19625 " int big = 10000;\n"
19626 "\n"
19627 "unsigned oneTwoThree =123;\n"
19628 "int oneTwo = 12;\n"
19629 " method();\n"
19630 "float k= 2;\n"
19631 "int ll=10000;\n"
19632 "}",
19633 AlignmentMiddle);
19634 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19635 " int const i = 1;\n"
19636 " int ** j = 2, ***k;\n"
19637 " int & k = i;\n"
19638 " int && l = i + j;\n"
19639 " int big = 10000;\n"
19640 "\n"
19641 " unsigned oneTwoThree = 123;\n"
19642 " int oneTwo = 12;\n"
19643 " method();\n"
19644 " float k = 2;\n"
19645 " int ll = 10000;\n"
19646 "}",
19647 "void SomeFunction(int parameter= 0) {\n"
19648 " int const i= 1;\n"
19649 " int **j=2,***k;\n"
19650 "int &k=i;\n"
19651 "int &&l=i+j;\n"
19652 " int big = 10000;\n"
19653 "\n"
19654 "unsigned oneTwoThree =123;\n"
19655 "int oneTwo = 12;\n"
19656 " method();\n"
19657 "float k= 2;\n"
19658 "int ll=10000;\n"
19659 "}",
19660 AlignmentMiddle);
19661 // variables are aligned at their name, pointers are in the middle
19662 verifyFormat("int * a;\n"
19663 "int * b;\n"
19664 "int *** c;\n"
19665 "int foobar;",
19666 AlignmentMiddle);
19667
19668 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19669 "double b();",
19670 AlignmentMiddle);
19671
19672 Alignment.AlignConsecutiveAssignments.Enabled = false;
19673 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19674 verifyFormat("#define A \\\n"
19675 " int aaaa = 12; \\\n"
19676 " float b = 23; \\\n"
19677 " const int ccc = 234; \\\n"
19678 " unsigned dddddddddd = 2345;",
19679 Alignment);
19680 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19681 verifyFormat("#define A \\\n"
19682 " int aaaa = 12; \\\n"
19683 " float b = 23; \\\n"
19684 " const int ccc = 234; \\\n"
19685 " unsigned dddddddddd = 2345;",
19686 Alignment);
19687 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19688 Alignment.ColumnLimit = 30;
19689 verifyFormat("#define A \\\n"
19690 " int aaaa = 12; \\\n"
19691 " float b = 23; \\\n"
19692 " const int ccc = 234; \\\n"
19693 " int dddddddddd = 2345;",
19694 Alignment);
19695 Alignment.ColumnLimit = 80;
19696 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19697 "k = 4, int l = 5,\n"
19698 " int m = 6) {\n"
19699 " const int j = 10;\n"
19700 " otherThing = 1;\n"
19701 "}",
19702 Alignment);
19703 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19704 " int const i = 1;\n"
19705 " int *j = 2;\n"
19706 " int big = 10000;\n"
19707 "}",
19708 Alignment);
19709 verifyFormat("class C {\n"
19710 "public:\n"
19711 " int i = 1;\n"
19712 " virtual void f() = 0;\n"
19713 "};",
19714 Alignment);
19715 verifyFormat("float i = 1;\n"
19716 "if (SomeType t = getSomething()) {\n"
19717 "}\n"
19718 "const unsigned j = 2;\n"
19719 "int big = 10000;",
19720 Alignment);
19721 verifyFormat("float j = 7;\n"
19722 "for (int k = 0; k < N; ++k) {\n"
19723 "}\n"
19724 "unsigned j = 2;\n"
19725 "int big = 10000;\n"
19726 "}",
19727 Alignment);
19728 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19729 verifyFormat("float i = 1;\n"
19730 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19731 " = someLooooooooooooooooongFunction();\n"
19732 "int j = 2;",
19733 Alignment);
19734 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19735 verifyFormat("int i = 1;\n"
19736 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19737 " someLooooooooooooooooongFunction();\n"
19738 "int j = 2;",
19739 Alignment);
19740
19741 Alignment.AlignConsecutiveAssignments.Enabled = true;
19742 verifyFormat("auto lambda = []() {\n"
19743 " auto ii = 0;\n"
19744 " float j = 0;\n"
19745 " return 0;\n"
19746 "};\n"
19747 "int i = 0;\n"
19748 "float i2 = 0;\n"
19749 "auto v = type{\n"
19750 " i = 1, //\n"
19751 " (i = 2), //\n"
19752 " i = 3 //\n"
19753 "};",
19754 Alignment);
19755 Alignment.AlignConsecutiveAssignments.Enabled = false;
19756
19757 verifyFormat(
19758 "int i = 1;\n"
19759 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19760 " loooooooooooooooooooooongParameterB);\n"
19761 "int j = 2;",
19762 Alignment);
19763
19764 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19765 // We expect declarations and assignments to align, as long as it doesn't
19766 // exceed the column limit, starting a new alignment sequence whenever it
19767 // happens.
19768 Alignment.AlignConsecutiveAssignments.Enabled = true;
19769 Alignment.ColumnLimit = 30;
19770 verifyFormat("float ii = 1;\n"
19771 "unsigned j = 2;\n"
19772 "int someVerylongVariable = 1;\n"
19773 "AnotherLongType ll = 123456;\n"
19774 "VeryVeryLongType k = 2;\n"
19775 "int myvar = 1;",
19776 Alignment);
19777 Alignment.ColumnLimit = 80;
19778 Alignment.AlignConsecutiveAssignments.Enabled = false;
19779
19780 verifyFormat(
19781 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19782 " typename LongType, typename B>\n"
19783 "auto foo() {}",
19784 Alignment);
19785 verifyFormat("float a, b = 1;\n"
19786 "int c = 2;\n"
19787 "int dd = 3;",
19788 Alignment);
19789 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19790 "float b[1][] = {{3.f}};",
19791 Alignment);
19792 Alignment.AlignConsecutiveAssignments.Enabled = true;
19793 verifyFormat("float a, b = 1;\n"
19794 "int c = 2;\n"
19795 "int dd = 3;",
19796 Alignment);
19797 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19798 "float b[1][] = {{3.f}};",
19799 Alignment);
19800 Alignment.AlignConsecutiveAssignments.Enabled = false;
19801
19802 Alignment.ColumnLimit = 30;
19803 Alignment.BinPackParameters = false;
19804 verifyFormat("void foo(float a,\n"
19805 " float b,\n"
19806 " int c,\n"
19807 " uint32_t *d) {\n"
19808 " int *e = 0;\n"
19809 " float f = 0;\n"
19810 " double g = 0;\n"
19811 "}\n"
19812 "void bar(ino_t a,\n"
19813 " int b,\n"
19814 " uint32_t *c,\n"
19815 " bool d) {}",
19816 Alignment);
19817 Alignment.BinPackParameters = true;
19818 Alignment.ColumnLimit = 80;
19819
19820 // Bug 33507
19821 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19822 verifyFormat(
19823 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19824 " static const Version verVs2017;\n"
19825 " return true;\n"
19826 "});",
19827 Alignment);
19828 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19829
19830 // See llvm.org/PR35641
19831 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19832 verifyFormat("int func() { //\n"
19833 " int b;\n"
19834 " unsigned c;\n"
19835 "}",
19836 Alignment);
19837
19838 // See PR37175
19839 FormatStyle Style = getMozillaStyle();
19840 Style.AlignConsecutiveDeclarations.Enabled = true;
19841 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19842 "foo(int a);",
19843 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
19844
19845 Alignment.PointerAlignment = FormatStyle::PAS_Left;
19846 verifyFormat("unsigned int* a;\n"
19847 "int* b;\n"
19848 "unsigned int Const* c;\n"
19849 "unsigned int const* d;\n"
19850 "unsigned int Const& e;\n"
19851 "unsigned int const& f;",
19852 Alignment);
19853 verifyFormat("Const unsigned int* c;\n"
19854 "const unsigned int* d;\n"
19855 "Const unsigned int& e;\n"
19856 "const unsigned int& f;\n"
19857 "const unsigned g;\n"
19858 "Const unsigned h;",
19859 Alignment);
19860
19861 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19862 verifyFormat("unsigned int * a;\n"
19863 "int * b;\n"
19864 "unsigned int Const * c;\n"
19865 "unsigned int const * d;\n"
19866 "unsigned int Const & e;\n"
19867 "unsigned int const & f;",
19868 Alignment);
19869 verifyFormat("Const unsigned int * c;\n"
19870 "const unsigned int * d;\n"
19871 "Const unsigned int & e;\n"
19872 "const unsigned int & f;\n"
19873 "const unsigned g;\n"
19874 "Const unsigned h;",
19875 Alignment);
19876
19877 // See PR46529
19878 FormatStyle BracedAlign = getLLVMStyle();
19879 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
19880 verifyFormat("const auto result{[]() {\n"
19881 " const auto something = 1;\n"
19882 " return 2;\n"
19883 "}};",
19884 BracedAlign);
19885 verifyFormat("int foo{[]() {\n"
19886 " int bar{0};\n"
19887 " return 0;\n"
19888 "}()};",
19889 BracedAlign);
19890 BracedAlign.Cpp11BracedListStyle = false;
19891 verifyFormat("const auto result{ []() {\n"
19892 " const auto something = 1;\n"
19893 " return 2;\n"
19894 "} };",
19895 BracedAlign);
19896 verifyFormat("int foo{ []() {\n"
19897 " int bar{ 0 };\n"
19898 " return 0;\n"
19899 "}() };",
19900 BracedAlign);
19901 }
19902
19903 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
19904 FormatStyle Alignment = getLLVMStyle();
19905 Alignment.AllowShortCaseLabelsOnASingleLine = true;
19906 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
19907
19908 verifyFormat("switch (level) {\n"
19909 "case log::info: return \"info\";\n"
19910 "case log::warning: return \"warning\";\n"
19911 "default: return \"default\";\n"
19912 "}",
19913 Alignment);
19914
19915 verifyFormat("switch (level) {\n"
19916 "case log::info: return \"info\";\n"
19917 "case log::warning: return \"warning\";\n"
19918 "}",
19919 "switch (level) {\n"
19920 "case log::info: return \"info\";\n"
19921 "case log::warning:\n"
19922 " return \"warning\";\n"
19923 "}",
19924 Alignment);
19925
19926 // Empty case statements push out the alignment, but non-short case labels
19927 // don't.
19928 verifyFormat("switch (level) {\n"
19929 "case log::info: return \"info\";\n"
19930 "case log::critical:\n"
19931 "case log::warning:\n"
19932 "case log::severe: return \"severe\";\n"
19933 "case log::extra_severe:\n"
19934 " // comment\n"
19935 " return \"extra_severe\";\n"
19936 "}",
19937 Alignment);
19938
19939 // Verify comments and empty lines break the alignment.
19940 verifyNoChange("switch (level) {\n"
19941 "case log::info: return \"info\";\n"
19942 "case log::warning: return \"warning\";\n"
19943 "// comment\n"
19944 "case log::critical: return \"critical\";\n"
19945 "default: return \"default\";\n"
19946 "\n"
19947 "case log::severe: return \"severe\";\n"
19948 "}",
19949 Alignment);
19950
19951 // Empty case statements don't break the alignment, and potentially push it
19952 // out.
19953 verifyFormat("switch (level) {\n"
19954 "case log::info: return \"info\";\n"
19955 "case log::warning:\n"
19956 "case log::critical:\n"
19957 "default: return \"default\";\n"
19958 "}",
19959 Alignment);
19960
19961 // Implicit fallthrough cases can be aligned with either a comment or
19962 // [[fallthrough]]
19963 verifyFormat("switch (level) {\n"
19964 "case log::info: return \"info\";\n"
19965 "case log::warning: // fallthrough\n"
19966 "case log::error: return \"error\";\n"
19967 "case log::critical: /*fallthrough*/\n"
19968 "case log::severe: return \"severe\";\n"
19969 "case log::diag: [[fallthrough]];\n"
19970 "default: return \"default\";\n"
19971 "}",
19972 Alignment);
19973
19974 // Verify trailing comment that needs a reflow also gets aligned properly.
19975 verifyFormat("switch (level) {\n"
19976 "case log::info: return \"info\";\n"
19977 "case log::warning: // fallthrough\n"
19978 "case log::error: return \"error\";\n"
19979 "}",
19980 "switch (level) {\n"
19981 "case log::info: return \"info\";\n"
19982 "case log::warning: //fallthrough\n"
19983 "case log::error: return \"error\";\n"
19984 "}",
19985 Alignment);
19986
19987 // Verify adjacent non-short case statements don't change the alignment, and
19988 // properly break the set of consecutive statements.
19989 verifyFormat("switch (level) {\n"
19990 "case log::critical:\n"
19991 " // comment\n"
19992 " return \"critical\";\n"
19993 "case log::info: return \"info\";\n"
19994 "case log::warning: return \"warning\";\n"
19995 "default:\n"
19996 " // comment\n"
19997 " return \"\";\n"
19998 "case log::error: return \"error\";\n"
19999 "case log::severe: return \"severe\";\n"
20000 "case log::extra_critical:\n"
20001 " // comment\n"
20002 " return \"extra critical\";\n"
20003 "}",
20004 Alignment);
20005
20006 Alignment.SpaceBeforeCaseColon = true;
20007 verifyFormat("switch (level) {\n"
20008 "case log::info : return \"info\";\n"
20009 "case log::warning : return \"warning\";\n"
20010 "default : return \"default\";\n"
20011 "}",
20012 Alignment);
20013 Alignment.SpaceBeforeCaseColon = false;
20014
20015 // Make sure we don't incorrectly align correctly across nested switch cases.
20016 verifyFormat("switch (level) {\n"
20017 "case log::info: return \"info\";\n"
20018 "case log::warning: return \"warning\";\n"
20019 "case log::other:\n"
20020 " switch (sublevel) {\n"
20021 " case log::info: return \"info\";\n"
20022 " case log::warning: return \"warning\";\n"
20023 " }\n"
20024 " break;\n"
20025 "case log::error: return \"error\";\n"
20026 "default: return \"default\";\n"
20027 "}",
20028 "switch (level) {\n"
20029 "case log::info: return \"info\";\n"
20030 "case log::warning: return \"warning\";\n"
20031 "case log::other: switch (sublevel) {\n"
20032 " case log::info: return \"info\";\n"
20033 " case log::warning: return \"warning\";\n"
20034 "}\n"
20035 "break;\n"
20036 "case log::error: return \"error\";\n"
20037 "default: return \"default\";\n"
20038 "}",
20039 Alignment);
20040
20041 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
20042
20043 verifyFormat("switch (level) {\n"
20044 "case log::info: return \"info\";\n"
20045 "\n"
20046 "case log::warning: return \"warning\";\n"
20047 "}",
20048 "switch (level) {\n"
20049 "case log::info: return \"info\";\n"
20050 "\n"
20051 "case log::warning: return \"warning\";\n"
20052 "}",
20053 Alignment);
20054
20055 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
20056
20057 verifyNoChange("switch (level) {\n"
20058 "case log::info: return \"info\";\n"
20059 "\n"
20060 "/* block comment */\n"
20061 "\n"
20062 "// line comment\n"
20063 "case log::warning: return \"warning\";\n"
20064 "}",
20065 Alignment);
20066
20067 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
20068
20069 verifyFormat("switch (level) {\n"
20070 "case log::info: return \"info\";\n"
20071 "//\n"
20072 "case log::warning: return \"warning\";\n"
20073 "}",
20074 Alignment);
20075
20076 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
20077
20078 verifyFormat("switch (level) {\n"
20079 "case log::info : return \"info\";\n"
20080 "case log::warning: return \"warning\";\n"
20081 "default : return \"default\";\n"
20082 "}",
20083 Alignment);
20084
20085 // With AlignCaseColons, empty case statements don't break alignment of
20086 // consecutive case statements (and are aligned).
20087 verifyFormat("switch (level) {\n"
20088 "case log::info : return \"info\";\n"
20089 "case log::warning :\n"
20090 "case log::critical:\n"
20091 "default : return \"default\";\n"
20092 "}",
20093 Alignment);
20094
20095 // Final non-short case labels shouldn't have their colon aligned
20096 verifyFormat("switch (level) {\n"
20097 "case log::info : return \"info\";\n"
20098 "case log::warning :\n"
20099 "case log::critical:\n"
20100 "case log::severe : return \"severe\";\n"
20101 "default:\n"
20102 " // comment\n"
20103 " return \"default\";\n"
20104 "}",
20105 Alignment);
20106
20107 // Verify adjacent non-short case statements break the set of consecutive
20108 // alignments and aren't aligned with adjacent non-short case statements if
20109 // AlignCaseColons is set.
20110 verifyFormat("switch (level) {\n"
20111 "case log::critical:\n"
20112 " // comment\n"
20113 " return \"critical\";\n"
20114 "case log::info : return \"info\";\n"
20115 "case log::warning: return \"warning\";\n"
20116 "default:\n"
20117 " // comment\n"
20118 " return \"\";\n"
20119 "case log::error : return \"error\";\n"
20120 "case log::severe: return \"severe\";\n"
20121 "case log::extra_critical:\n"
20122 " // comment\n"
20123 " return \"extra critical\";\n"
20124 "}",
20125 Alignment);
20126
20127 Alignment.SpaceBeforeCaseColon = true;
20128 verifyFormat("switch (level) {\n"
20129 "case log::info : return \"info\";\n"
20130 "case log::warning : return \"warning\";\n"
20131 "case log::error :\n"
20132 "default : return \"default\";\n"
20133 "}",
20134 Alignment);
20135 }
20136
20137 TEST_F(FormatTest, AlignWithLineBreaks) {
20138 auto Style = getLLVMStyleWithColumns(120);
20139
20140 EXPECT_EQ(Style.AlignConsecutiveAssignments,
20141 FormatStyle::AlignConsecutiveStyle(
20142 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20143 /*AcrossComments=*/false, /*AlignCompound=*/false,
20144 /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
20145 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20146 FormatStyle::AlignConsecutiveStyle({}));
20147 verifyFormat("void foo() {\n"
20148 " int myVar = 5;\n"
20149 " double x = 3.14;\n"
20150 " auto str = \"Hello \"\n"
20151 " \"World\";\n"
20152 " auto s = \"Hello \"\n"
20153 " \"Again\";\n"
20154 "}",
20155 Style);
20156
20157 // clang-format off
20158 verifyFormat("void foo() {\n"
20159 " const int capacityBefore = Entries.capacity();\n"
20160 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20161 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20162 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20163 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20164 "}",
20165 Style);
20166 // clang-format on
20167
20168 Style.AlignConsecutiveAssignments.Enabled = true;
20169 verifyFormat("void foo() {\n"
20170 " int myVar = 5;\n"
20171 " double x = 3.14;\n"
20172 " auto str = \"Hello \"\n"
20173 " \"World\";\n"
20174 " auto s = \"Hello \"\n"
20175 " \"Again\";\n"
20176 "}",
20177 Style);
20178
20179 // clang-format off
20180 verifyFormat("void foo() {\n"
20181 " const int capacityBefore = Entries.capacity();\n"
20182 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20183 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20184 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20185 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20186 "}",
20187 Style);
20188 // clang-format on
20189
20190 Style.AlignConsecutiveAssignments.Enabled = false;
20191 Style.AlignConsecutiveDeclarations.Enabled = true;
20192 verifyFormat("void foo() {\n"
20193 " int myVar = 5;\n"
20194 " double x = 3.14;\n"
20195 " auto str = \"Hello \"\n"
20196 " \"World\";\n"
20197 " auto s = \"Hello \"\n"
20198 " \"Again\";\n"
20199 "}",
20200 Style);
20201
20202 // clang-format off
20203 verifyFormat("void foo() {\n"
20204 " const int capacityBefore = Entries.capacity();\n"
20205 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20206 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20207 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20208 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20209 "}",
20210 Style);
20211 // clang-format on
20212
20213 Style.AlignConsecutiveAssignments.Enabled = true;
20214 Style.AlignConsecutiveDeclarations.Enabled = true;
20215
20216 verifyFormat("void foo() {\n"
20217 " int myVar = 5;\n"
20218 " double x = 3.14;\n"
20219 " auto str = \"Hello \"\n"
20220 " \"World\";\n"
20221 " auto s = \"Hello \"\n"
20222 " \"Again\";\n"
20223 "}",
20224 Style);
20225
20226 // clang-format off
20227 verifyFormat("void foo() {\n"
20228 " const int capacityBefore = Entries.capacity();\n"
20229 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20230 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20231 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20232 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20233 "}",
20234 Style);
20235 // clang-format on
20236
20237 Style = getLLVMStyleWithColumns(20);
20238 Style.AlignConsecutiveAssignments.Enabled = true;
20239 Style.IndentWidth = 4;
20240
20241 verifyFormat("void foo() {\n"
20242 " int i1 = 1;\n"
20243 " int j = 0;\n"
20244 " int k = bar(\n"
20245 " argument1,\n"
20246 " argument2);\n"
20247 "}",
20248 Style);
20249
20250 verifyFormat("unsigned i = 0;\n"
20251 "int a[] = {\n"
20252 " 1234567890,\n"
20253 " -1234567890};",
20254 Style);
20255
20256 Style.ColumnLimit = 120;
20257
20258 // clang-format off
20259 verifyFormat("void SomeFunc() {\n"
20260 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20261 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20262 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20263 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20264 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20265 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20266 "}",
20267 Style);
20268 // clang-format on
20269
20270 Style.BinPackArguments = false;
20271
20272 // clang-format off
20273 verifyFormat("void SomeFunc() {\n"
20274 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20275 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20276 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20277 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20278 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20279 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20280 "}",
20281 Style);
20282 // clang-format on
20283 }
20284
20285 TEST_F(FormatTest, AlignWithInitializerPeriods) {
20286 auto Style = getLLVMStyleWithColumns(60);
20287
20288 verifyFormat("void foo1(void) {\n"
20289 " BYTE p[1] = 1;\n"
20290 " A B = {.one_foooooooooooooooo = 2,\n"
20291 " .two_fooooooooooooo = 3,\n"
20292 " .three_fooooooooooooo = 4};\n"
20293 " BYTE payload = 2;\n"
20294 "}",
20295 Style);
20296
20297 Style.AlignConsecutiveAssignments.Enabled = true;
20298 Style.AlignConsecutiveDeclarations.Enabled = false;
20299 verifyFormat("void foo2(void) {\n"
20300 " BYTE p[1] = 1;\n"
20301 " A B = {.one_foooooooooooooooo = 2,\n"
20302 " .two_fooooooooooooo = 3,\n"
20303 " .three_fooooooooooooo = 4};\n"
20304 " BYTE payload = 2;\n"
20305 "}",
20306 Style);
20307
20308 Style.AlignConsecutiveAssignments.Enabled = false;
20309 Style.AlignConsecutiveDeclarations.Enabled = true;
20310 verifyFormat("void foo3(void) {\n"
20311 " BYTE p[1] = 1;\n"
20312 " A B = {.one_foooooooooooooooo = 2,\n"
20313 " .two_fooooooooooooo = 3,\n"
20314 " .three_fooooooooooooo = 4};\n"
20315 " BYTE payload = 2;\n"
20316 "}",
20317 Style);
20318
20319 Style.AlignConsecutiveAssignments.Enabled = true;
20320 Style.AlignConsecutiveDeclarations.Enabled = true;
20321 verifyFormat("void foo4(void) {\n"
20322 " BYTE p[1] = 1;\n"
20323 " A B = {.one_foooooooooooooooo = 2,\n"
20324 " .two_fooooooooooooo = 3,\n"
20325 " .three_fooooooooooooo = 4};\n"
20326 " BYTE payload = 2;\n"
20327 "}",
20328 Style);
20329 }
20330
20331 TEST_F(FormatTest, LinuxBraceBreaking) {
20332 FormatStyle LinuxBraceStyle = getLLVMStyle();
20333 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
20334 verifyFormat("namespace a\n"
20335 "{\n"
20336 "class A\n"
20337 "{\n"
20338 " void f()\n"
20339 " {\n"
20340 " if (true) {\n"
20341 " a();\n"
20342 " b();\n"
20343 " } else {\n"
20344 " a();\n"
20345 " }\n"
20346 " }\n"
20347 " void g() { return; }\n"
20348 "};\n"
20349 "struct B {\n"
20350 " int x;\n"
20351 "};\n"
20352 "} // namespace a",
20353 LinuxBraceStyle);
20354 verifyFormat("enum X {\n"
20355 " Y = 0,\n"
20356 "}",
20357 LinuxBraceStyle);
20358 verifyFormat("struct S {\n"
20359 " int Type;\n"
20360 " union {\n"
20361 " int x;\n"
20362 " double y;\n"
20363 " } Value;\n"
20364 " class C\n"
20365 " {\n"
20366 " MyFavoriteType Value;\n"
20367 " } Class;\n"
20368 "}",
20369 LinuxBraceStyle);
20370 }
20371
20372 TEST_F(FormatTest, MozillaBraceBreaking) {
20373 FormatStyle MozillaBraceStyle = getLLVMStyle();
20374 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
20375 MozillaBraceStyle.FixNamespaceComments = false;
20376 verifyFormat("namespace a {\n"
20377 "class A\n"
20378 "{\n"
20379 " void f()\n"
20380 " {\n"
20381 " if (true) {\n"
20382 " a();\n"
20383 " b();\n"
20384 " }\n"
20385 " }\n"
20386 " void g() { return; }\n"
20387 "};\n"
20388 "enum E\n"
20389 "{\n"
20390 " A,\n"
20391 " // foo\n"
20392 " B,\n"
20393 " C\n"
20394 "};\n"
20395 "struct B\n"
20396 "{\n"
20397 " int x;\n"
20398 "};\n"
20399 "}",
20400 MozillaBraceStyle);
20401 verifyFormat("struct S\n"
20402 "{\n"
20403 " int Type;\n"
20404 " union\n"
20405 " {\n"
20406 " int x;\n"
20407 " double y;\n"
20408 " } Value;\n"
20409 " class C\n"
20410 " {\n"
20411 " MyFavoriteType Value;\n"
20412 " } Class;\n"
20413 "}",
20414 MozillaBraceStyle);
20415 }
20416
20417 TEST_F(FormatTest, StroustrupBraceBreaking) {
20418 FormatStyle StroustrupBraceStyle = getLLVMStyle();
20419 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
20420 verifyFormat("namespace a {\n"
20421 "class A {\n"
20422 " void f()\n"
20423 " {\n"
20424 " if (true) {\n"
20425 " a();\n"
20426 " b();\n"
20427 " }\n"
20428 " }\n"
20429 " void g() { return; }\n"
20430 "};\n"
20431 "struct B {\n"
20432 " int x;\n"
20433 "};\n"
20434 "} // namespace a",
20435 StroustrupBraceStyle);
20436
20437 verifyFormat("void foo()\n"
20438 "{\n"
20439 " if (a) {\n"
20440 " a();\n"
20441 " }\n"
20442 " else {\n"
20443 " b();\n"
20444 " }\n"
20445 "}",
20446 StroustrupBraceStyle);
20447
20448 verifyFormat("#ifdef _DEBUG\n"
20449 "int foo(int i = 0)\n"
20450 "#else\n"
20451 "int foo(int i = 5)\n"
20452 "#endif\n"
20453 "{\n"
20454 " return i;\n"
20455 "}",
20456 StroustrupBraceStyle);
20457
20458 verifyFormat("void foo() {}\n"
20459 "void bar()\n"
20460 "#ifdef _DEBUG\n"
20461 "{\n"
20462 " foo();\n"
20463 "}\n"
20464 "#else\n"
20465 "{\n"
20466 "}\n"
20467 "#endif",
20468 StroustrupBraceStyle);
20469
20470 verifyFormat("void foobar() { int i = 5; }\n"
20471 "#ifdef _DEBUG\n"
20472 "void bar() {}\n"
20473 "#else\n"
20474 "void bar() { foobar(); }\n"
20475 "#endif",
20476 StroustrupBraceStyle);
20477 }
20478
20479 TEST_F(FormatTest, AllmanBraceBreaking) {
20480 FormatStyle AllmanBraceStyle = getLLVMStyle();
20481 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
20482
20483 verifyFormat("namespace a\n"
20484 "{\n"
20485 "void f();\n"
20486 "void g();\n"
20487 "} // namespace a",
20488 "namespace a\n"
20489 "{\n"
20490 "void f();\n"
20491 "void g();\n"
20492 "}",
20493 AllmanBraceStyle);
20494
20495 verifyFormat("namespace a\n"
20496 "{\n"
20497 "class A\n"
20498 "{\n"
20499 " void f()\n"
20500 " {\n"
20501 " if (true)\n"
20502 " {\n"
20503 " a();\n"
20504 " b();\n"
20505 " }\n"
20506 " }\n"
20507 " void g() { return; }\n"
20508 "};\n"
20509 "struct B\n"
20510 "{\n"
20511 " int x;\n"
20512 "};\n"
20513 "union C\n"
20514 "{\n"
20515 "};\n"
20516 "} // namespace a",
20517 AllmanBraceStyle);
20518
20519 verifyFormat("void f()\n"
20520 "{\n"
20521 " if (true)\n"
20522 " {\n"
20523 " a();\n"
20524 " }\n"
20525 " else if (false)\n"
20526 " {\n"
20527 " b();\n"
20528 " }\n"
20529 " else\n"
20530 " {\n"
20531 " c();\n"
20532 " }\n"
20533 "}",
20534 AllmanBraceStyle);
20535
20536 verifyFormat("void f()\n"
20537 "{\n"
20538 " for (int i = 0; i < 10; ++i)\n"
20539 " {\n"
20540 " a();\n"
20541 " }\n"
20542 " while (false)\n"
20543 " {\n"
20544 " b();\n"
20545 " }\n"
20546 " do\n"
20547 " {\n"
20548 " c();\n"
20549 " } while (false)\n"
20550 "}",
20551 AllmanBraceStyle);
20552
20553 verifyFormat("void f(int a)\n"
20554 "{\n"
20555 " switch (a)\n"
20556 " {\n"
20557 " case 0:\n"
20558 " break;\n"
20559 " case 1:\n"
20560 " {\n"
20561 " break;\n"
20562 " }\n"
20563 " case 2:\n"
20564 " {\n"
20565 " }\n"
20566 " break;\n"
20567 " default:\n"
20568 " break;\n"
20569 " }\n"
20570 "}",
20571 AllmanBraceStyle);
20572
20573 verifyFormat("enum X\n"
20574 "{\n"
20575 " Y = 0,\n"
20576 "}",
20577 AllmanBraceStyle);
20578 verifyFormat("enum X\n"
20579 "{\n"
20580 " Y = 0\n"
20581 "}",
20582 AllmanBraceStyle);
20583
20584 verifyFormat("@interface BSApplicationController ()\n"
20585 "{\n"
20586 "@private\n"
20587 " id _extraIvar;\n"
20588 "}\n"
20589 "@end",
20590 AllmanBraceStyle);
20591
20592 verifyFormat("#ifdef _DEBUG\n"
20593 "int foo(int i = 0)\n"
20594 "#else\n"
20595 "int foo(int i = 5)\n"
20596 "#endif\n"
20597 "{\n"
20598 " return i;\n"
20599 "}",
20600 AllmanBraceStyle);
20601
20602 verifyFormat("void foo() {}\n"
20603 "void bar()\n"
20604 "#ifdef _DEBUG\n"
20605 "{\n"
20606 " foo();\n"
20607 "}\n"
20608 "#else\n"
20609 "{\n"
20610 "}\n"
20611 "#endif",
20612 AllmanBraceStyle);
20613
20614 verifyFormat("void foobar() { int i = 5; }\n"
20615 "#ifdef _DEBUG\n"
20616 "void bar() {}\n"
20617 "#else\n"
20618 "void bar() { foobar(); }\n"
20619 "#endif",
20620 AllmanBraceStyle);
20621
20622 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
20623 FormatStyle::SLS_All);
20624
20625 verifyFormat("[](int i) { return i + 2; };\n"
20626 "[](int i, int j)\n"
20627 "{\n"
20628 " auto x = i + j;\n"
20629 " auto y = i * j;\n"
20630 " return x ^ y;\n"
20631 "};\n"
20632 "void foo()\n"
20633 "{\n"
20634 " auto shortLambda = [](int i) { return i + 2; };\n"
20635 " auto longLambda = [](int i, int j)\n"
20636 " {\n"
20637 " auto x = i + j;\n"
20638 " auto y = i * j;\n"
20639 " return x ^ y;\n"
20640 " };\n"
20641 "}",
20642 AllmanBraceStyle);
20643
20644 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20645
20646 verifyFormat("[](int i)\n"
20647 "{\n"
20648 " return i + 2;\n"
20649 "};\n"
20650 "[](int i, int j)\n"
20651 "{\n"
20652 " auto x = i + j;\n"
20653 " auto y = i * j;\n"
20654 " return x ^ y;\n"
20655 "};\n"
20656 "void foo()\n"
20657 "{\n"
20658 " auto shortLambda = [](int i)\n"
20659 " {\n"
20660 " return i + 2;\n"
20661 " };\n"
20662 " auto longLambda = [](int i, int j)\n"
20663 " {\n"
20664 " auto x = i + j;\n"
20665 " auto y = i * j;\n"
20666 " return x ^ y;\n"
20667 " };\n"
20668 "}",
20669 AllmanBraceStyle);
20670
20671 // Reset
20672 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
20673
20674 // This shouldn't affect ObjC blocks..
20675 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20676 " // ...\n"
20677 " int i;\n"
20678 "}];",
20679 AllmanBraceStyle);
20680 verifyFormat("void (^block)(void) = ^{\n"
20681 " // ...\n"
20682 " int i;\n"
20683 "};",
20684 AllmanBraceStyle);
20685 // .. or dict literals.
20686 verifyFormat("void f()\n"
20687 "{\n"
20688 " // ...\n"
20689 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20690 "}",
20691 AllmanBraceStyle);
20692 verifyFormat("void f()\n"
20693 "{\n"
20694 " // ...\n"
20695 " [object someMethod:@{a : @\"b\"}];\n"
20696 "}",
20697 AllmanBraceStyle);
20698 verifyFormat("int f()\n"
20699 "{ // comment\n"
20700 " return 42;\n"
20701 "}",
20702 AllmanBraceStyle);
20703
20704 AllmanBraceStyle.ColumnLimit = 19;
20705 verifyFormat("void f() { int i; }", AllmanBraceStyle);
20706 AllmanBraceStyle.ColumnLimit = 18;
20707 verifyFormat("void f()\n"
20708 "{\n"
20709 " int i;\n"
20710 "}",
20711 AllmanBraceStyle);
20712 AllmanBraceStyle.ColumnLimit = 80;
20713
20714 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
20715 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20716 FormatStyle::SIS_WithoutElse;
20717 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20718 verifyFormat("void f(bool b)\n"
20719 "{\n"
20720 " if (b)\n"
20721 " {\n"
20722 " return;\n"
20723 " }\n"
20724 "}",
20725 BreakBeforeBraceShortIfs);
20726 verifyFormat("void f(bool b)\n"
20727 "{\n"
20728 " if constexpr (b)\n"
20729 " {\n"
20730 " return;\n"
20731 " }\n"
20732 "}",
20733 BreakBeforeBraceShortIfs);
20734 verifyFormat("void f(bool b)\n"
20735 "{\n"
20736 " if CONSTEXPR (b)\n"
20737 " {\n"
20738 " return;\n"
20739 " }\n"
20740 "}",
20741 BreakBeforeBraceShortIfs);
20742 verifyFormat("void f(bool b)\n"
20743 "{\n"
20744 " if (b) return;\n"
20745 "}",
20746 BreakBeforeBraceShortIfs);
20747 verifyFormat("void f(bool b)\n"
20748 "{\n"
20749 " if constexpr (b) return;\n"
20750 "}",
20751 BreakBeforeBraceShortIfs);
20752 verifyFormat("void f(bool b)\n"
20753 "{\n"
20754 " if CONSTEXPR (b) return;\n"
20755 "}",
20756 BreakBeforeBraceShortIfs);
20757 verifyFormat("void f(bool b)\n"
20758 "{\n"
20759 " while (b)\n"
20760 " {\n"
20761 " return;\n"
20762 " }\n"
20763 "}",
20764 BreakBeforeBraceShortIfs);
20765 }
20766
20767 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
20768 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
20769 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
20770
20771 // Make a few changes to the style for testing purposes
20772 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
20773 FormatStyle::SFS_Empty;
20774 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20775
20776 // FIXME: this test case can't decide whether there should be a blank line
20777 // after the ~D() line or not. It adds one if one doesn't exist in the test
20778 // and it removes the line if one exists.
20779 /*
20780 verifyFormat("class A;\n"
20781 "namespace B\n"
20782 " {\n"
20783 "class C;\n"
20784 "// Comment\n"
20785 "class D\n"
20786 " {\n"
20787 "public:\n"
20788 " D();\n"
20789 " ~D() {}\n"
20790 "private:\n"
20791 " enum E\n"
20792 " {\n"
20793 " F\n"
20794 " }\n"
20795 " };\n"
20796 " } // namespace B",
20797 WhitesmithsBraceStyle);
20798 */
20799
20800 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
20801 verifyFormat("namespace a\n"
20802 " {\n"
20803 "class A\n"
20804 " {\n"
20805 " void f()\n"
20806 " {\n"
20807 " if (true)\n"
20808 " {\n"
20809 " a();\n"
20810 " b();\n"
20811 " }\n"
20812 " }\n"
20813 " void g()\n"
20814 " {\n"
20815 " return;\n"
20816 " }\n"
20817 " };\n"
20818 "struct B\n"
20819 " {\n"
20820 " int x;\n"
20821 " };\n"
20822 " } // namespace a",
20823 WhitesmithsBraceStyle);
20824
20825 verifyFormat("namespace a\n"
20826 " {\n"
20827 "namespace b\n"
20828 " {\n"
20829 "class A\n"
20830 " {\n"
20831 " void f()\n"
20832 " {\n"
20833 " if (true)\n"
20834 " {\n"
20835 " a();\n"
20836 " b();\n"
20837 " }\n"
20838 " }\n"
20839 " void g()\n"
20840 " {\n"
20841 " return;\n"
20842 " }\n"
20843 " };\n"
20844 "struct B\n"
20845 " {\n"
20846 " int x;\n"
20847 " };\n"
20848 " } // namespace b\n"
20849 " } // namespace a",
20850 WhitesmithsBraceStyle);
20851
20852 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
20853 verifyFormat("namespace a\n"
20854 " {\n"
20855 "namespace b\n"
20856 " {\n"
20857 " class A\n"
20858 " {\n"
20859 " void f()\n"
20860 " {\n"
20861 " if (true)\n"
20862 " {\n"
20863 " a();\n"
20864 " b();\n"
20865 " }\n"
20866 " }\n"
20867 " void g()\n"
20868 " {\n"
20869 " return;\n"
20870 " }\n"
20871 " };\n"
20872 " struct B\n"
20873 " {\n"
20874 " int x;\n"
20875 " };\n"
20876 " } // namespace b\n"
20877 " } // namespace a",
20878 WhitesmithsBraceStyle);
20879
20880 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
20881 verifyFormat("namespace a\n"
20882 " {\n"
20883 " namespace b\n"
20884 " {\n"
20885 " class A\n"
20886 " {\n"
20887 " void f()\n"
20888 " {\n"
20889 " if (true)\n"
20890 " {\n"
20891 " a();\n"
20892 " b();\n"
20893 " }\n"
20894 " }\n"
20895 " void g()\n"
20896 " {\n"
20897 " return;\n"
20898 " }\n"
20899 " };\n"
20900 " struct B\n"
20901 " {\n"
20902 " int x;\n"
20903 " };\n"
20904 " } // namespace b\n"
20905 " } // namespace a",
20906 WhitesmithsBraceStyle);
20907
20908 verifyFormat("void f()\n"
20909 " {\n"
20910 " if (true)\n"
20911 " {\n"
20912 " a();\n"
20913 " }\n"
20914 " else if (false)\n"
20915 " {\n"
20916 " b();\n"
20917 " }\n"
20918 " else\n"
20919 " {\n"
20920 " c();\n"
20921 " }\n"
20922 " }",
20923 WhitesmithsBraceStyle);
20924
20925 verifyFormat("void f()\n"
20926 " {\n"
20927 " for (int i = 0; i < 10; ++i)\n"
20928 " {\n"
20929 " a();\n"
20930 " }\n"
20931 " while (false)\n"
20932 " {\n"
20933 " b();\n"
20934 " }\n"
20935 " do\n"
20936 " {\n"
20937 " c();\n"
20938 " } while (false)\n"
20939 " }",
20940 WhitesmithsBraceStyle);
20941
20942 WhitesmithsBraceStyle.IndentCaseLabels = true;
20943 verifyFormat("void switchTest1(int a)\n"
20944 " {\n"
20945 " switch (a)\n"
20946 " {\n"
20947 " case 2:\n"
20948 " {\n"
20949 " }\n"
20950 " break;\n"
20951 " }\n"
20952 " }",
20953 WhitesmithsBraceStyle);
20954
20955 verifyFormat("void switchTest2(int a)\n"
20956 " {\n"
20957 " switch (a)\n"
20958 " {\n"
20959 " case 0:\n"
20960 " break;\n"
20961 " case 1:\n"
20962 " {\n"
20963 " break;\n"
20964 " }\n"
20965 " case 2:\n"
20966 " {\n"
20967 " }\n"
20968 " break;\n"
20969 " default:\n"
20970 " break;\n"
20971 " }\n"
20972 " }",
20973 WhitesmithsBraceStyle);
20974
20975 verifyFormat("void switchTest3(int a)\n"
20976 " {\n"
20977 " switch (a)\n"
20978 " {\n"
20979 " case 0:\n"
20980 " {\n"
20981 " foo(x);\n"
20982 " }\n"
20983 " break;\n"
20984 " default:\n"
20985 " {\n"
20986 " foo(1);\n"
20987 " }\n"
20988 " break;\n"
20989 " }\n"
20990 " }",
20991 WhitesmithsBraceStyle);
20992
20993 WhitesmithsBraceStyle.IndentCaseLabels = false;
20994
20995 verifyFormat("void switchTest4(int a)\n"
20996 " {\n"
20997 " switch (a)\n"
20998 " {\n"
20999 " case 2:\n"
21000 " {\n"
21001 " }\n"
21002 " break;\n"
21003 " }\n"
21004 " }",
21005 WhitesmithsBraceStyle);
21006
21007 verifyFormat("void switchTest5(int a)\n"
21008 " {\n"
21009 " switch (a)\n"
21010 " {\n"
21011 " case 0:\n"
21012 " break;\n"
21013 " case 1:\n"
21014 " {\n"
21015 " foo();\n"
21016 " break;\n"
21017 " }\n"
21018 " case 2:\n"
21019 " {\n"
21020 " }\n"
21021 " break;\n"
21022 " default:\n"
21023 " break;\n"
21024 " }\n"
21025 " }",
21026 WhitesmithsBraceStyle);
21027
21028 verifyFormat("void switchTest6(int a)\n"
21029 " {\n"
21030 " switch (a)\n"
21031 " {\n"
21032 " case 0:\n"
21033 " {\n"
21034 " foo(x);\n"
21035 " }\n"
21036 " break;\n"
21037 " default:\n"
21038 " {\n"
21039 " foo(1);\n"
21040 " }\n"
21041 " break;\n"
21042 " }\n"
21043 " }",
21044 WhitesmithsBraceStyle);
21045
21046 verifyFormat("enum X\n"
21047 " {\n"
21048 " Y = 0, // testing\n"
21049 " }",
21050 WhitesmithsBraceStyle);
21051
21052 verifyFormat("enum X\n"
21053 " {\n"
21054 " Y = 0\n"
21055 " }",
21056 WhitesmithsBraceStyle);
21057 verifyFormat("enum X\n"
21058 " {\n"
21059 " Y = 0,\n"
21060 " Z = 1\n"
21061 " };",
21062 WhitesmithsBraceStyle);
21063
21064 verifyFormat("@interface BSApplicationController ()\n"
21065 " {\n"
21066 "@private\n"
21067 " id _extraIvar;\n"
21068 " }\n"
21069 "@end",
21070 WhitesmithsBraceStyle);
21071
21072 verifyFormat("#ifdef _DEBUG\n"
21073 "int foo(int i = 0)\n"
21074 "#else\n"
21075 "int foo(int i = 5)\n"
21076 "#endif\n"
21077 " {\n"
21078 " return i;\n"
21079 " }",
21080 WhitesmithsBraceStyle);
21081
21082 verifyFormat("void foo() {}\n"
21083 "void bar()\n"
21084 "#ifdef _DEBUG\n"
21085 " {\n"
21086 " foo();\n"
21087 " }\n"
21088 "#else\n"
21089 " {\n"
21090 " }\n"
21091 "#endif",
21092 WhitesmithsBraceStyle);
21093
21094 verifyFormat("void foobar()\n"
21095 " {\n"
21096 " int i = 5;\n"
21097 " }\n"
21098 "#ifdef _DEBUG\n"
21099 "void bar() {}\n"
21100 "#else\n"
21101 "void bar()\n"
21102 " {\n"
21103 " foobar();\n"
21104 " }\n"
21105 "#endif",
21106 WhitesmithsBraceStyle);
21107
21108 // This shouldn't affect ObjC blocks..
21109 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21110 " // ...\n"
21111 " int i;\n"
21112 "}];",
21113 WhitesmithsBraceStyle);
21114 verifyFormat("void (^block)(void) = ^{\n"
21115 " // ...\n"
21116 " int i;\n"
21117 "};",
21118 WhitesmithsBraceStyle);
21119 // .. or dict literals.
21120 verifyFormat("void f()\n"
21121 " {\n"
21122 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21123 " }",
21124 WhitesmithsBraceStyle);
21125
21126 verifyFormat("int f()\n"
21127 " { // comment\n"
21128 " return 42;\n"
21129 " }",
21130 WhitesmithsBraceStyle);
21131
21132 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
21133 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21134 FormatStyle::SIS_OnlyFirstIf;
21135 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21136 verifyFormat("void f(bool b)\n"
21137 " {\n"
21138 " if (b)\n"
21139 " {\n"
21140 " return;\n"
21141 " }\n"
21142 " }",
21143 BreakBeforeBraceShortIfs);
21144 verifyFormat("void f(bool b)\n"
21145 " {\n"
21146 " if (b) return;\n"
21147 " }",
21148 BreakBeforeBraceShortIfs);
21149 verifyFormat("void f(bool b)\n"
21150 " {\n"
21151 " while (b)\n"
21152 " {\n"
21153 " return;\n"
21154 " }\n"
21155 " }",
21156 BreakBeforeBraceShortIfs);
21157 }
21158
21159 TEST_F(FormatTest, GNUBraceBreaking) {
21160 FormatStyle GNUBraceStyle = getLLVMStyle();
21161 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
21162 verifyFormat("namespace a\n"
21163 "{\n"
21164 "class A\n"
21165 "{\n"
21166 " void f()\n"
21167 " {\n"
21168 " int a;\n"
21169 " {\n"
21170 " int b;\n"
21171 " }\n"
21172 " if (true)\n"
21173 " {\n"
21174 " a();\n"
21175 " b();\n"
21176 " }\n"
21177 " }\n"
21178 " void g() { return; }\n"
21179 "}\n"
21180 "} // namespace a",
21181 GNUBraceStyle);
21182
21183 verifyFormat("void f()\n"
21184 "{\n"
21185 " if (true)\n"
21186 " {\n"
21187 " a();\n"
21188 " }\n"
21189 " else if (false)\n"
21190 " {\n"
21191 " b();\n"
21192 " }\n"
21193 " else\n"
21194 " {\n"
21195 " c();\n"
21196 " }\n"
21197 "}",
21198 GNUBraceStyle);
21199
21200 verifyFormat("void f()\n"
21201 "{\n"
21202 " for (int i = 0; i < 10; ++i)\n"
21203 " {\n"
21204 " a();\n"
21205 " }\n"
21206 " while (false)\n"
21207 " {\n"
21208 " b();\n"
21209 " }\n"
21210 " do\n"
21211 " {\n"
21212 " c();\n"
21213 " }\n"
21214 " while (false);\n"
21215 "}",
21216 GNUBraceStyle);
21217
21218 verifyFormat("void f(int a)\n"
21219 "{\n"
21220 " switch (a)\n"
21221 " {\n"
21222 " case 0:\n"
21223 " break;\n"
21224 " case 1:\n"
21225 " {\n"
21226 " break;\n"
21227 " }\n"
21228 " case 2:\n"
21229 " {\n"
21230 " }\n"
21231 " break;\n"
21232 " default:\n"
21233 " break;\n"
21234 " }\n"
21235 "}",
21236 GNUBraceStyle);
21237
21238 verifyFormat("enum X\n"
21239 "{\n"
21240 " Y = 0,\n"
21241 "}",
21242 GNUBraceStyle);
21243
21244 verifyFormat("@interface BSApplicationController ()\n"
21245 "{\n"
21246 "@private\n"
21247 " id _extraIvar;\n"
21248 "}\n"
21249 "@end",
21250 GNUBraceStyle);
21251
21252 verifyFormat("#ifdef _DEBUG\n"
21253 "int foo(int i = 0)\n"
21254 "#else\n"
21255 "int foo(int i = 5)\n"
21256 "#endif\n"
21257 "{\n"
21258 " return i;\n"
21259 "}",
21260 GNUBraceStyle);
21261
21262 verifyFormat("void foo() {}\n"
21263 "void bar()\n"
21264 "#ifdef _DEBUG\n"
21265 "{\n"
21266 " foo();\n"
21267 "}\n"
21268 "#else\n"
21269 "{\n"
21270 "}\n"
21271 "#endif",
21272 GNUBraceStyle);
21273
21274 verifyFormat("void foobar() { int i = 5; }\n"
21275 "#ifdef _DEBUG\n"
21276 "void bar() {}\n"
21277 "#else\n"
21278 "void bar() { foobar(); }\n"
21279 "#endif",
21280 GNUBraceStyle);
21281 }
21282
21283 TEST_F(FormatTest, WebKitBraceBreaking) {
21284 FormatStyle WebKitBraceStyle = getLLVMStyle();
21285 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
21286 WebKitBraceStyle.FixNamespaceComments = false;
21287 verifyFormat("namespace a {\n"
21288 "class A {\n"
21289 " void f()\n"
21290 " {\n"
21291 " if (true) {\n"
21292 " a();\n"
21293 " b();\n"
21294 " }\n"
21295 " }\n"
21296 " void g() { return; }\n"
21297 "};\n"
21298 "enum E {\n"
21299 " A,\n"
21300 " // foo\n"
21301 " B,\n"
21302 " C\n"
21303 "};\n"
21304 "struct B {\n"
21305 " int x;\n"
21306 "};\n"
21307 "}",
21308 WebKitBraceStyle);
21309 verifyFormat("struct S {\n"
21310 " int Type;\n"
21311 " union {\n"
21312 " int x;\n"
21313 " double y;\n"
21314 " } Value;\n"
21315 " class C {\n"
21316 " MyFavoriteType Value;\n"
21317 " } Class;\n"
21318 "};",
21319 WebKitBraceStyle);
21320 }
21321
21322 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
21323 verifyFormat("void f() {\n"
21324 " try {\n"
21325 " } catch (const Exception &e) {\n"
21326 " }\n"
21327 "}");
21328 }
21329
21330 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
21331 auto Style = getLLVMStyle();
21332 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21333 verifyNoCrash("f({\n"
21334 "table({}, table({{\"\", false}}, {}))\n"
21335 "});",
21336 Style);
21337
21338 Style.AlignConsecutiveAssignments.Enabled = true;
21339 Style.AlignConsecutiveDeclarations.Enabled = true;
21340 verifyFormat("struct test demo[] = {\n"
21341 " {56, 23, \"hello\"},\n"
21342 " {-1, 93463, \"world\"},\n"
21343 " { 7, 5, \"!!\"}\n"
21344 "};",
21345 Style);
21346
21347 verifyFormat("struct test demo[] = {\n"
21348 " {56, 23, \"hello\"}, // first line\n"
21349 " {-1, 93463, \"world\"}, // second line\n"
21350 " { 7, 5, \"!!\"} // third line\n"
21351 "};",
21352 Style);
21353
21354 verifyFormat("struct test demo[4] = {\n"
21355 " { 56, 23, 21, \"oh\"}, // first line\n"
21356 " { -1, 93463, 22, \"my\"}, // second line\n"
21357 " { 7, 5, 1, \"goodness\"} // third line\n"
21358 " {234, 5, 1, \"gracious\"} // fourth line\n"
21359 "};",
21360 Style);
21361
21362 verifyFormat("struct test demo[3] = {\n"
21363 " {56, 23, \"hello\"},\n"
21364 " {-1, 93463, \"world\"},\n"
21365 " { 7, 5, \"!!\"}\n"
21366 "};",
21367 Style);
21368
21369 verifyFormat("struct test demo[3] = {\n"
21370 " {int{56}, 23, \"hello\"},\n"
21371 " {int{-1}, 93463, \"world\"},\n"
21372 " { int{7}, 5, \"!!\"}\n"
21373 "};",
21374 Style);
21375
21376 verifyFormat("struct test demo[] = {\n"
21377 " {56, 23, \"hello\"},\n"
21378 " {-1, 93463, \"world\"},\n"
21379 " { 7, 5, \"!!\"},\n"
21380 "};",
21381 Style);
21382
21383 verifyFormat("test demo[] = {\n"
21384 " {56, 23, \"hello\"},\n"
21385 " {-1, 93463, \"world\"},\n"
21386 " { 7, 5, \"!!\"},\n"
21387 "};",
21388 Style);
21389
21390 verifyFormat("demo = std::array<struct test, 3>{\n"
21391 " test{56, 23, \"hello\"},\n"
21392 " test{-1, 93463, \"world\"},\n"
21393 " test{ 7, 5, \"!!\"},\n"
21394 "};",
21395 Style);
21396
21397 verifyFormat("test demo[] = {\n"
21398 " {56, 23, \"hello\"},\n"
21399 "#if X\n"
21400 " {-1, 93463, \"world\"},\n"
21401 "#endif\n"
21402 " { 7, 5, \"!!\"}\n"
21403 "};",
21404 Style);
21405
21406 verifyFormat(
21407 "test demo[] = {\n"
21408 " { 7, 23,\n"
21409 " \"hello world i am a very long line that really, in any\"\n"
21410 " \"just world, ought to be split over multiple lines\"},\n"
21411 " {-1, 93463, \"world\"},\n"
21412 " {56, 5, \"!!\"}\n"
21413 "};",
21414 Style);
21415
21416 verifyNoCrash("Foo f[] = {\n"
21417 " [0] = { 1, },\n"
21418 " [i] { 1, },\n"
21419 "};",
21420 Style);
21421 verifyNoCrash("Foo foo[] = {\n"
21422 " [0] = {1, 1},\n"
21423 " [1] { 1, 1, },\n"
21424 " [2] { 1, 1, },\n"
21425 "};",
21426 Style);
21427 verifyNoCrash("test arr[] = {\n"
21428 "#define FOO(i) {i, i},\n"
21429 "SOME_GENERATOR(FOO)\n"
21430 "{2, 2}\n"
21431 "};",
21432 Style);
21433
21434 verifyFormat("return GradForUnaryCwise(g, {\n"
21435 " {{\"sign\"}, \"Sign\", "
21436 " {\"x\", \"dy\"}},\n"
21437 " { {\"dx\"}, \"Mul\", {\"dy\""
21438 ", \"sign\"}},\n"
21439 "});",
21440 Style);
21441
21442 Style.Cpp11BracedListStyle = false;
21443 verifyFormat("struct test demo[] = {\n"
21444 " { 56, 23, \"hello\" },\n"
21445 " { -1, 93463, \"world\" },\n"
21446 " { 7, 5, \"!!\" }\n"
21447 "};",
21448 Style);
21449 Style.Cpp11BracedListStyle = true;
21450
21451 Style.ColumnLimit = 0;
21452 verifyFormat(
21453 "test demo[] = {\n"
21454 " {56, 23, \"hello world i am a very long line that really, "
21455 "in any just world, ought to be split over multiple lines\"},\n"
21456 " {-1, 93463, "
21457 " \"world\"},\n"
21458 " { 7, 5, "
21459 " \"!!\"},\n"
21460 "};",
21461 "test demo[] = {{56, 23, \"hello world i am a very long line "
21462 "that really, in any just world, ought to be split over multiple "
21463 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21464 Style);
21465
21466 Style.ColumnLimit = 80;
21467 verifyFormat("test demo[] = {\n"
21468 " {56, 23, /* a comment */ \"hello\"},\n"
21469 " {-1, 93463, \"world\"},\n"
21470 " { 7, 5, \"!!\"}\n"
21471 "};",
21472 Style);
21473
21474 verifyFormat("test demo[] = {\n"
21475 " {56, 23, \"hello\"},\n"
21476 " {-1, 93463, \"world\" /* comment here */},\n"
21477 " { 7, 5, \"!!\"}\n"
21478 "};",
21479 Style);
21480
21481 verifyFormat("test demo[] = {\n"
21482 " {56, /* a comment */ 23, \"hello\"},\n"
21483 " {-1, 93463, \"world\"},\n"
21484 " { 7, 5, \"!!\"}\n"
21485 "};",
21486 Style);
21487
21488 Style.ColumnLimit = 20;
21489 verifyFormat("demo = std::array<\n"
21490 " struct test, 3>{\n"
21491 " test{\n"
21492 " 56, 23,\n"
21493 " \"hello \"\n"
21494 " \"world i \"\n"
21495 " \"am a very \"\n"
21496 " \"long line \"\n"
21497 " \"that \"\n"
21498 " \"really, \"\n"
21499 " \"in any \"\n"
21500 " \"just \"\n"
21501 " \"world, \"\n"
21502 " \"ought to \"\n"
21503 " \"be split \"\n"
21504 " \"over \"\n"
21505 " \"multiple \"\n"
21506 " \"lines\"},\n"
21507 " test{-1, 93463,\n"
21508 " \"world\"},\n"
21509 " test{ 7, 5,\n"
21510 " \"!!\" },\n"
21511 "};",
21512 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21513 "i am a very long line that really, in any just world, ought "
21514 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21515 "test{7, 5, \"!!\"},};",
21516 Style);
21517 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21518 Style = getLLVMStyleWithColumns(50);
21519 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21520 verifyFormat("static A x = {\n"
21521 " {{init1, init2, init3, init4},\n"
21522 " {init1, init2, init3, init4}}\n"
21523 "};",
21524 Style);
21525 // TODO: Fix the indentations below when this option is fully functional.
21526 #if 0
21527 verifyFormat("int a[][] = {\n"
21528 " {\n"
21529 " {0, 2}, //\n"
21530 " {1, 2} //\n"
21531 " }\n"
21532 "};",
21533 Style);
21534 #endif
21535 Style.ColumnLimit = 100;
21536 verifyFormat(
21537 "test demo[] = {\n"
21538 " {56, 23,\n"
21539 " \"hello world i am a very long line that really, in any just world"
21540 ", ought to be split over \"\n"
21541 " \"multiple lines\" },\n"
21542 " {-1, 93463, \"world\"},\n"
21543 " { 7, 5, \"!!\"},\n"
21544 "};",
21545 "test demo[] = {{56, 23, \"hello world i am a very long line "
21546 "that really, in any just world, ought to be split over multiple "
21547 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21548 Style);
21549
21550 Style = getLLVMStyleWithColumns(50);
21551 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21552 verifyFormat("struct test demo[] = {\n"
21553 " {56, 23, \"hello\"},\n"
21554 " {-1, 93463, \"world\"},\n"
21555 " { 7, 5, \"!!\"}\n"
21556 "};\n"
21557 "static A x = {\n"
21558 " {{init1, init2, init3, init4},\n"
21559 " {init1, init2, init3, init4}}\n"
21560 "};",
21561 Style);
21562 Style.ColumnLimit = 100;
21563 Style.AlignConsecutiveAssignments.AcrossComments = true;
21564 Style.AlignConsecutiveDeclarations.AcrossComments = true;
21565 verifyFormat("struct test demo[] = {\n"
21566 " {56, 23, \"hello\"},\n"
21567 " {-1, 93463, \"world\"},\n"
21568 " { 7, 5, \"!!\"}\n"
21569 "};\n"
21570 "struct test demo[4] = {\n"
21571 " { 56, 23, 21, \"oh\"}, // first line\n"
21572 " { -1, 93463, 22, \"my\"}, // second line\n"
21573 " { 7, 5, 1, \"goodness\"} // third line\n"
21574 " {234, 5, 1, \"gracious\"} // fourth line\n"
21575 "};",
21576 Style);
21577 verifyFormat(
21578 "test demo[] = {\n"
21579 " {56,\n"
21580 " \"hello world i am a very long line that really, in any just world"
21581 ", ought to be split over \"\n"
21582 " \"multiple lines\", 23},\n"
21583 " {-1, \"world\", 93463},\n"
21584 " { 7, \"!!\", 5},\n"
21585 "};",
21586 "test demo[] = {{56, \"hello world i am a very long line "
21587 "that really, in any just world, ought to be split over multiple "
21588 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21589 Style);
21590 }
21591
21592 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
21593 auto Style = getLLVMStyle();
21594 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21595 /* FIXME: This case gets misformatted.
21596 verifyFormat("auto foo = Items{\n"
21597 " Section{0, bar(), },\n"
21598 " Section{1, boo() }\n"
21599 "};",
21600 Style);
21601 */
21602 verifyFormat("auto foo = Items{\n"
21603 " Section{\n"
21604 " 0, bar(),\n"
21605 " }\n"
21606 "};",
21607 Style);
21608 verifyFormat("struct test demo[] = {\n"
21609 " {56, 23, \"hello\"},\n"
21610 " {-1, 93463, \"world\"},\n"
21611 " {7, 5, \"!!\" }\n"
21612 "};",
21613 Style);
21614 verifyFormat("struct test demo[] = {\n"
21615 " {56, 23, \"hello\"}, // first line\n"
21616 " {-1, 93463, \"world\"}, // second line\n"
21617 " {7, 5, \"!!\" } // third line\n"
21618 "};",
21619 Style);
21620 verifyFormat("struct test demo[4] = {\n"
21621 " {56, 23, 21, \"oh\" }, // first line\n"
21622 " {-1, 93463, 22, \"my\" }, // second line\n"
21623 " {7, 5, 1, \"goodness\"} // third line\n"
21624 " {234, 5, 1, \"gracious\"} // fourth line\n"
21625 "};",
21626 Style);
21627 verifyFormat("struct test demo[3] = {\n"
21628 " {56, 23, \"hello\"},\n"
21629 " {-1, 93463, \"world\"},\n"
21630 " {7, 5, \"!!\" }\n"
21631 "};",
21632 Style);
21633
21634 verifyFormat("struct test demo[3] = {\n"
21635 " {int{56}, 23, \"hello\"},\n"
21636 " {int{-1}, 93463, \"world\"},\n"
21637 " {int{7}, 5, \"!!\" }\n"
21638 "};",
21639 Style);
21640 verifyFormat("struct test demo[] = {\n"
21641 " {56, 23, \"hello\"},\n"
21642 " {-1, 93463, \"world\"},\n"
21643 " {7, 5, \"!!\" },\n"
21644 "};",
21645 Style);
21646 verifyFormat("test demo[] = {\n"
21647 " {56, 23, \"hello\"},\n"
21648 " {-1, 93463, \"world\"},\n"
21649 " {7, 5, \"!!\" },\n"
21650 "};",
21651 Style);
21652 verifyFormat("demo = std::array<struct test, 3>{\n"
21653 " test{56, 23, \"hello\"},\n"
21654 " test{-1, 93463, \"world\"},\n"
21655 " test{7, 5, \"!!\" },\n"
21656 "};",
21657 Style);
21658 verifyFormat("test demo[] = {\n"
21659 " {56, 23, \"hello\"},\n"
21660 "#if X\n"
21661 " {-1, 93463, \"world\"},\n"
21662 "#endif\n"
21663 " {7, 5, \"!!\" }\n"
21664 "};",
21665 Style);
21666 verifyFormat(
21667 "test demo[] = {\n"
21668 " {7, 23,\n"
21669 " \"hello world i am a very long line that really, in any\"\n"
21670 " \"just world, ought to be split over multiple lines\"},\n"
21671 " {-1, 93463, \"world\" },\n"
21672 " {56, 5, \"!!\" }\n"
21673 "};",
21674 Style);
21675
21676 verifyNoCrash("Foo f[] = {\n"
21677 " [0] = { 1, },\n"
21678 " [i] { 1, },\n"
21679 "};",
21680 Style);
21681 verifyNoCrash("Foo foo[] = {\n"
21682 " [0] = {1, 1},\n"
21683 " [1] { 1, 1, },\n"
21684 " [2] { 1, 1, },\n"
21685 "};",
21686 Style);
21687 verifyNoCrash("test arr[] = {\n"
21688 "#define FOO(i) {i, i},\n"
21689 "SOME_GENERATOR(FOO)\n"
21690 "{2, 2}\n"
21691 "};",
21692 Style);
21693
21694 verifyFormat("return GradForUnaryCwise(g, {\n"
21695 " {{\"sign\"}, \"Sign\", {\"x\", "
21696 "\"dy\"} },\n"
21697 " {{\"dx\"}, \"Mul\", "
21698 "{\"dy\", \"sign\"}},\n"
21699 "});",
21700 Style);
21701
21702 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
21703 verifyFormat("#define FOO \\\n"
21704 " int foo[][2] = { \\\n"
21705 " {0, 1} \\\n"
21706 " };",
21707 Style);
21708
21709 Style.Cpp11BracedListStyle = false;
21710 verifyFormat("struct test demo[] = {\n"
21711 " { 56, 23, \"hello\" },\n"
21712 " { -1, 93463, \"world\" },\n"
21713 " { 7, 5, \"!!\" }\n"
21714 "};",
21715 Style);
21716 Style.Cpp11BracedListStyle = true;
21717
21718 Style.ColumnLimit = 0;
21719 verifyFormat(
21720 "test demo[] = {\n"
21721 " {56, 23, \"hello world i am a very long line that really, in any "
21722 "just world, ought to be split over multiple lines\"},\n"
21723 " {-1, 93463, \"world\" "
21724 " },\n"
21725 " {7, 5, \"!!\" "
21726 " },\n"
21727 "};",
21728 "test demo[] = {{56, 23, \"hello world i am a very long line "
21729 "that really, in any just world, ought to be split over multiple "
21730 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21731 Style);
21732
21733 Style.ColumnLimit = 80;
21734 verifyFormat("test demo[] = {\n"
21735 " {56, 23, /* a comment */ \"hello\"},\n"
21736 " {-1, 93463, \"world\" },\n"
21737 " {7, 5, \"!!\" }\n"
21738 "};",
21739 Style);
21740
21741 verifyFormat("test demo[] = {\n"
21742 " {56, 23, \"hello\" },\n"
21743 " {-1, 93463, \"world\" /* comment here */},\n"
21744 " {7, 5, \"!!\" }\n"
21745 "};",
21746 Style);
21747
21748 verifyFormat("test demo[] = {\n"
21749 " {56, /* a comment */ 23, \"hello\"},\n"
21750 " {-1, 93463, \"world\"},\n"
21751 " {7, 5, \"!!\" }\n"
21752 "};",
21753 Style);
21754 verifyFormat("Foo foo = {\n"
21755 " // comment\n"
21756 " {1, 2}\n"
21757 "};",
21758 Style);
21759
21760 Style.ColumnLimit = 20;
21761 // FIXME: unstable test case
21762 EXPECT_EQ(
21763 "demo = std::array<\n"
21764 " struct test, 3>{\n"
21765 " test{\n"
21766 " 56, 23,\n"
21767 " \"hello \"\n"
21768 " \"world i \"\n"
21769 " \"am a very \"\n"
21770 " \"long line \"\n"
21771 " \"that \"\n"
21772 " \"really, \"\n"
21773 " \"in any \"\n"
21774 " \"just \"\n"
21775 " \"world, \"\n"
21776 " \"ought to \"\n"
21777 " \"be split \"\n"
21778 " \"over \"\n"
21779 " \"multiple \"\n"
21780 " \"lines\"},\n"
21781 " test{-1, 93463,\n"
21782 " \"world\"},\n"
21783 " test{7, 5,\n"
21784 " \"!!\" },\n"
21785 "};",
21786 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21787 "i am a very long line that really, in any just world, ought "
21788 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21789 "test{7, 5, \"!!\"},};",
21790 Style));
21791
21792 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21793 Style = getLLVMStyleWithColumns(50);
21794 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21795 verifyFormat("static A x = {\n"
21796 " {{init1, init2, init3, init4},\n"
21797 " {init1, init2, init3, init4}}\n"
21798 "};",
21799 Style);
21800 Style.ColumnLimit = 100;
21801 verifyFormat(
21802 "test demo[] = {\n"
21803 " {56, 23,\n"
21804 " \"hello world i am a very long line that really, in any just world"
21805 ", ought to be split over \"\n"
21806 " \"multiple lines\" },\n"
21807 " {-1, 93463, \"world\"},\n"
21808 " {7, 5, \"!!\" },\n"
21809 "};",
21810 "test demo[] = {{56, 23, \"hello world i am a very long line "
21811 "that really, in any just world, ought to be split over multiple "
21812 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21813 Style);
21814
21815 Style.ColumnLimit = 25;
21816 verifyNoCrash("Type foo{\n"
21817 " {\n"
21818 " 1, // A\n"
21819 " 2, // B\n"
21820 " 3, // C\n"
21821 " },\n"
21822 " \"hello\",\n"
21823 "};",
21824 Style);
21825 verifyNoCrash("Type object[X][Y] = {\n"
21826 " {{val}, {val}, {val}},\n"
21827 " {{val}, {val}, // some comment\n"
21828 " {val}}\n"
21829 "};",
21830 Style);
21831
21832 Style.ColumnLimit = 120;
21833 verifyNoCrash(
21834 "T v[] {\n"
21835 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21836 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21837 "\"00000000000000000000000000000000000000000000000000000000"
21838 "00000000000000000000000000000000000000000000000000000000\" },\n"
21839 "};",
21840 Style);
21841
21842 Style.SpacesInParens = FormatStyle::SIPO_Custom;
21843 Style.SpacesInParensOptions.Other = true;
21844 verifyFormat("Foo foo[] = {\n"
21845 " {1, 1},\n"
21846 " {1, 1},\n"
21847 "};",
21848 Style);
21849 }
21850
21851 TEST_F(FormatTest, UnderstandsPragmas) {
21852 verifyFormat("#pragma omp reduction(| : var)");
21853 verifyFormat("#pragma omp reduction(+ : var)");
21854
21855 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21856 "(including parentheses).",
21857 "#pragma mark Any non-hyphenated or hyphenated string "
21858 "(including parentheses).");
21859
21860 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21861 "(including parentheses).",
21862 "#pragma mark Any non-hyphenated or hyphenated string "
21863 "(including parentheses).");
21864
21865 verifyFormat("#pragma comment(linker, \\\n"
21866 " \"argument\" \\\n"
21867 " \"argument\"",
21868 "#pragma comment(linker, \\\n"
21869 " \"argument\" \\\n"
21870 " \"argument\"",
21871 getStyleWithColumns(
21872 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32));
21873 }
21874
21875 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
21876 verifyFormat("#pragma omp target map(to : var)");
21877 verifyFormat("#pragma omp target map(to : var[ : N])");
21878 verifyFormat("#pragma omp target map(to : var[0 : N])");
21879 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
21880
21881 verifyFormat(
21882 "#pragma omp target \\\n"
21883 " reduction(+ : var) \\\n"
21884 " map(to : A[0 : N]) \\\n"
21885 " map(to : B[0 : N]) \\\n"
21886 " map(from : C[0 : N]) \\\n"
21887 " firstprivate(i) \\\n"
21888 " firstprivate(j) \\\n"
21889 " firstprivate(k)",
21890 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
21891 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
21892 getLLVMStyleWithColumns(26));
21893 }
21894
21895 TEST_F(FormatTest, UnderstandPragmaOption) {
21896 verifyFormat("#pragma option -C -A");
21897
21898 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
21899 }
21900
21901 TEST_F(FormatTest, UnderstandPragmaRegion) {
21902 auto Style = getLLVMStyleWithColumns(0);
21903 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
21904 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
21905 }
21906
21907 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
21908 FormatStyle Style = getLLVMStyleWithColumns(20);
21909
21910 // See PR41213
21911 verifyFormat("/*\n"
21912 " *\t9012345\n"
21913 " * /8901\n"
21914 " */",
21915 "/*\n"
21916 " *\t9012345 /8901\n"
21917 " */",
21918 Style);
21919 verifyFormat("/*\n"
21920 " *345678\n"
21921 " *\t/8901\n"
21922 " */",
21923 "/*\n"
21924 " *345678\t/8901\n"
21925 " */",
21926 Style);
21927
21928 verifyFormat("int a; // the\n"
21929 " // comment",
21930 Style);
21931 verifyNoChange("int a; /* first line\n"
21932 " * second\n"
21933 " * line third\n"
21934 " * line\n"
21935 " */",
21936 Style);
21937 verifyFormat("int a; // first line\n"
21938 " // second\n"
21939 " // line third\n"
21940 " // line",
21941 "int a; // first line\n"
21942 " // second line\n"
21943 " // third line",
21944 Style);
21945
21946 Style.PenaltyExcessCharacter = 90;
21947 verifyFormat("int a; // the comment", Style);
21948 verifyFormat("int a; // the comment\n"
21949 " // aaa",
21950 "int a; // the comment aaa", Style);
21951 verifyNoChange("int a; /* first line\n"
21952 " * second line\n"
21953 " * third line\n"
21954 " */",
21955 Style);
21956 verifyFormat("int a; // first line\n"
21957 " // second line\n"
21958 " // third line",
21959 Style);
21960 // FIXME: Investigate why this is not getting the same layout as the test
21961 // above.
21962 verifyFormat("int a; /* first line\n"
21963 " * second line\n"
21964 " * third line\n"
21965 " */",
21966 "int a; /* first line second line third line"
21967 "\n*/",
21968 Style);
21969
21970 verifyFormat("// foo bar baz bazfoo\n"
21971 "// foo bar foo bar",
21972 "// foo bar baz bazfoo\n"
21973 "// foo bar foo bar",
21974 Style);
21975 verifyFormat("// foo bar baz bazfoo\n"
21976 "// foo bar foo bar",
21977 "// foo bar baz bazfoo\n"
21978 "// foo bar foo bar",
21979 Style);
21980
21981 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
21982 // next one.
21983 verifyFormat("// foo bar baz bazfoo\n"
21984 "// bar foo bar",
21985 "// foo bar baz bazfoo bar\n"
21986 "// foo bar",
21987 Style);
21988
21989 // FIXME: unstable test case
21990 EXPECT_EQ("// foo bar baz bazfoo\n"
21991 "// foo bar baz bazfoo\n"
21992 "// bar foo bar",
21993 format("// foo bar baz bazfoo\n"
21994 "// foo bar baz bazfoo bar\n"
21995 "// foo bar",
21996 Style));
21997
21998 // FIXME: unstable test case
21999 EXPECT_EQ("// foo bar baz bazfoo\n"
22000 "// foo bar baz bazfoo\n"
22001 "// bar foo bar",
22002 format("// foo bar baz bazfoo\n"
22003 "// foo bar baz bazfoo bar\n"
22004 "// foo bar",
22005 Style));
22006
22007 // Make sure we do not keep protruding characters if strict mode reflow is
22008 // cheaper than keeping protruding characters.
22009 Style.ColumnLimit = 21;
22010 verifyFormat("// foo foo foo foo\n"
22011 "// foo foo foo foo\n"
22012 "// foo foo foo foo",
22013 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
22014
22015 verifyFormat("int a = /* long block\n"
22016 " comment */\n"
22017 " 42;",
22018 "int a = /* long block comment */ 42;", Style);
22019 }
22020
22021 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
22022 FormatStyle Style = getLLVMStyle();
22023 Style.ColumnLimit = 8;
22024 Style.PenaltyExcessCharacter = 15;
22025 verifyFormat("int foo(\n"
22026 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22027 Style);
22028 Style.PenaltyBreakOpenParenthesis = 200;
22029 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22030 "int foo(\n"
22031 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22032 Style);
22033 }
22034
22035 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
22036 FormatStyle Style = getLLVMStyle();
22037 Style.ColumnLimit = 5;
22038 Style.PenaltyExcessCharacter = 150;
22039 verifyFormat("foo((\n"
22040 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22041
22042 Style);
22043 Style.PenaltyBreakOpenParenthesis = 100'000;
22044 verifyFormat("foo((int)\n"
22045 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22046 "foo((\n"
22047 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22048 Style);
22049 }
22050
22051 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
22052 FormatStyle Style = getLLVMStyle();
22053 Style.ColumnLimit = 4;
22054 Style.PenaltyExcessCharacter = 100;
22055 verifyFormat("for (\n"
22056 " int iiiiiiiiiiiiiiiii =\n"
22057 " 0;\n"
22058 " iiiiiiiiiiiiiiiii <\n"
22059 " 2;\n"
22060 " iiiiiiiiiiiiiiiii++) {\n"
22061 "}",
22062
22063 Style);
22064 Style.PenaltyBreakOpenParenthesis = 1250;
22065 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22066 " 0;\n"
22067 " iiiiiiiiiiiiiiiii <\n"
22068 " 2;\n"
22069 " iiiiiiiiiiiiiiiii++) {\n"
22070 "}",
22071 "for (\n"
22072 " int iiiiiiiiiiiiiiiii =\n"
22073 " 0;\n"
22074 " iiiiiiiiiiiiiiiii <\n"
22075 " 2;\n"
22076 " iiiiiiiiiiiiiiiii++) {\n"
22077 "}",
22078 Style);
22079 }
22080
22081 TEST_F(FormatTest, BreakPenaltyScopeResolution) {
22082 FormatStyle Style = getLLVMStyle();
22083 Style.ColumnLimit = 20;
22084 Style.PenaltyExcessCharacter = 100;
22085 verifyFormat("unsigned long\n"
22086 "foo::bar();",
22087 Style);
22088 Style.PenaltyBreakScopeResolution = 10;
22089 verifyFormat("unsigned long foo::\n"
22090 " bar();",
22091 Style);
22092 }
22093
22094 TEST_F(FormatTest, WorksFor8bitEncodings) {
22095 // FIXME: unstable test case
22096 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22097 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22098 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22099 "\"\xef\xee\xf0\xf3...\"",
22100 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22101 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22102 "\xef\xee\xf0\xf3...\"",
22103 getLLVMStyleWithColumns(12)));
22104 }
22105
22106 TEST_F(FormatTest, HandlesUTF8BOM) {
22107 verifyFormat("\xef\xbb\xbf");
22108 verifyFormat("\xef\xbb\xbf#include <iostream>");
22109 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22110
22111 auto Style = getLLVMStyle();
22112 Style.KeepEmptyLines.AtStartOfFile = false;
22113 verifyFormat("\xef\xbb\xbf#include <iostream>",
22114 "\xef\xbb\xbf\n#include <iostream>", Style);
22115 }
22116
22117 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22118 #if !defined(_MSC_VER)
22119
22120 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
22121 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22122 getLLVMStyleWithColumns(35));
22123 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22124 getLLVMStyleWithColumns(31));
22125 verifyFormat("// Однажды в студёную зимнюю пору...",
22126 getLLVMStyleWithColumns(36));
22127 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22128 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22129 getLLVMStyleWithColumns(39));
22130 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22131 getLLVMStyleWithColumns(35));
22132 }
22133
22134 TEST_F(FormatTest, SplitsUTF8Strings) {
22135 // Non-printable characters' width is currently considered to be the length in
22136 // bytes in UTF8. The characters can be displayed in very different manner
22137 // (zero-width, single width with a substitution glyph, expanded to their code
22138 // (e.g. "<8d>"), so there's no single correct way to handle them.
22139 // FIXME: unstable test case
22140 EXPECT_EQ("\"aaaaÄ\"\n"
22141 "\"\xc2\x8d\";",
22142 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22143 // FIXME: unstable test case
22144 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22145 "\"\xc2\x8d\";",
22146 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22147 // FIXME: unstable test case
22148 EXPECT_EQ("\"Однажды, в \"\n"
22149 "\"студёную \"\n"
22150 "\"зимнюю \"\n"
22151 "\"пору,\"",
22152 format("\"Однажды, в студёную зимнюю пору,\"",
22153 getLLVMStyleWithColumns(13)));
22154 // FIXME: unstable test case
22155 EXPECT_EQ(
22156 "\"一 二 三 \"\n"
22157 "\"四 五六 \"\n"
22158 "\"七 八 九 \"\n"
22159 "\"十\"",
22160 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22161 // FIXME: unstable test case
22162 EXPECT_EQ("\"一\t\"\n"
22163 "\"二 \t\"\n"
22164 "\"三 四 \"\n"
22165 "\"五\t\"\n"
22166 "\"六 \t\"\n"
22167 "\"七 \"\n"
22168 "\"八九十\tqq\"",
22169 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
22170 getLLVMStyleWithColumns(11)));
22171
22172 // UTF8 character in an escape sequence.
22173 // FIXME: unstable test case
22174 EXPECT_EQ("\"aaaaaa\"\n"
22175 "\"\\\xC2\x8D\"",
22176 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22177 }
22178
22179 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
22180 verifyFormat("const char *sssss =\n"
22181 " \"一二三四五六七八\\\n"
22182 " 九 十\";",
22183 "const char *sssss = \"一二三四五六七八\\\n"
22184 " 九 十\";",
22185 getLLVMStyleWithColumns(30));
22186 }
22187
22188 TEST_F(FormatTest, SplitsUTF8LineComments) {
22189 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22190 verifyFormat("// Я из лесу\n"
22191 "// вышел; был\n"
22192 "// сильный\n"
22193 "// мороз.",
22194 "// Я из лесу вышел; был сильный мороз.",
22195 getLLVMStyleWithColumns(13));
22196 verifyFormat("// 一二三\n"
22197 "// 四五六七\n"
22198 "// 八 九\n"
22199 "// 十",
22200 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22201 }
22202
22203 TEST_F(FormatTest, SplitsUTF8BlockComments) {
22204 verifyFormat("/* Гляжу,\n"
22205 " * поднимается\n"
22206 " * медленно в\n"
22207 " * гору\n"
22208 " * Лошадка,\n"
22209 " * везущая\n"
22210 " * хворосту\n"
22211 " * воз. */",
22212 "/* Гляжу, поднимается медленно в гору\n"
22213 " * Лошадка, везущая хворосту воз. */",
22214 getLLVMStyleWithColumns(13));
22215 verifyFormat("/* 一二三\n"
22216 " * 四五六七\n"
22217 " * 八 九\n"
22218 " * 十 */",
22219 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22220 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22221 " * 𝕓𝕪𝕥𝕖\n"
22222 " * 𝖀𝕿𝕱-𝟠 */",
22223 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22224 }
22225
22226 #endif // _MSC_VER
22227
22228 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22229 FormatStyle Style = getLLVMStyle();
22230
22231 Style.ConstructorInitializerIndentWidth = 4;
22232 verifyFormat(
22233 "SomeClass::Constructor()\n"
22234 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22235 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22236 Style);
22237
22238 Style.ConstructorInitializerIndentWidth = 2;
22239 verifyFormat(
22240 "SomeClass::Constructor()\n"
22241 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22242 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22243 Style);
22244
22245 Style.ConstructorInitializerIndentWidth = 0;
22246 verifyFormat(
22247 "SomeClass::Constructor()\n"
22248 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22249 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22250 Style);
22251 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
22252 verifyFormat(
22253 "SomeLongTemplateVariableName<\n"
22254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22255 Style);
22256 verifyFormat("bool smaller = 1 < "
22257 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22258 " "
22259 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22260 Style);
22261
22262 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22263 verifyFormat("SomeClass::Constructor() :\n"
22264 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22265 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22266 Style);
22267 }
22268
22269 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
22270 FormatStyle Style = getLLVMStyle();
22271 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22272 Style.ConstructorInitializerIndentWidth = 4;
22273 verifyFormat("SomeClass::Constructor()\n"
22274 " : a(a)\n"
22275 " , b(b)\n"
22276 " , c(c) {}",
22277 Style);
22278 verifyFormat("SomeClass::Constructor()\n"
22279 " : a(a) {}",
22280 Style);
22281
22282 Style.ColumnLimit = 0;
22283 verifyFormat("SomeClass::Constructor()\n"
22284 " : a(a) {}",
22285 Style);
22286 verifyFormat("SomeClass::Constructor() noexcept\n"
22287 " : a(a) {}",
22288 Style);
22289 verifyFormat("SomeClass::Constructor()\n"
22290 " : a(a)\n"
22291 " , b(b)\n"
22292 " , c(c) {}",
22293 Style);
22294 verifyFormat("SomeClass::Constructor()\n"
22295 " : a(a) {\n"
22296 " foo();\n"
22297 " bar();\n"
22298 "}",
22299 Style);
22300
22301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22302 verifyFormat("SomeClass::Constructor()\n"
22303 " : a(a)\n"
22304 " , b(b)\n"
22305 " , c(c) {\n}",
22306 Style);
22307 verifyFormat("SomeClass::Constructor()\n"
22308 " : a(a) {\n}",
22309 Style);
22310
22311 Style.ColumnLimit = 80;
22312 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
22313 Style.ConstructorInitializerIndentWidth = 2;
22314 verifyFormat("SomeClass::Constructor()\n"
22315 " : a(a)\n"
22316 " , b(b)\n"
22317 " , c(c) {}",
22318 Style);
22319
22320 Style.ConstructorInitializerIndentWidth = 0;
22321 verifyFormat("SomeClass::Constructor()\n"
22322 ": a(a)\n"
22323 ", b(b)\n"
22324 ", c(c) {}",
22325 Style);
22326
22327 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22328 Style.ConstructorInitializerIndentWidth = 4;
22329 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
22330 verifyFormat(
22331 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22332 Style);
22333 verifyFormat(
22334 "SomeClass::Constructor()\n"
22335 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22336 Style);
22337 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22338 verifyFormat("SomeClass::Constructor()\n"
22339 " : aaaaaaaa(aaaaaaaa) {}",
22340 Style);
22341 verifyFormat("SomeClass::Constructor()\n"
22342 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22343 Style);
22344 verifyFormat(
22345 "SomeClass::Constructor()\n"
22346 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22347 Style);
22348
22349 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22350 Style.ConstructorInitializerIndentWidth = 4;
22351 Style.ColumnLimit = 60;
22352 verifyFormat("SomeClass::Constructor()\n"
22353 " : aaaaaaaa(aaaaaaaa)\n"
22354 " , aaaaaaaa(aaaaaaaa)\n"
22355 " , aaaaaaaa(aaaaaaaa) {}",
22356 Style);
22357 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22358 verifyFormat("SomeClass::Constructor()\n"
22359 " : aaaaaaaa(aaaaaaaa)\n"
22360 " , aaaaaaaa(aaaaaaaa)\n"
22361 " , aaaaaaaa(aaaaaaaa) {}",
22362 Style);
22363 }
22364
22365 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
22366 FormatStyle Style = getLLVMStyle();
22367 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22368 Style.ConstructorInitializerIndentWidth = 4;
22369 verifyFormat("SomeClass::Constructor()\n"
22370 " : a{a}\n"
22371 " , b{b} {}",
22372 Style);
22373 verifyFormat("SomeClass::Constructor()\n"
22374 " : a{a}\n"
22375 "#if CONDITION\n"
22376 " , b{b}\n"
22377 "#endif\n"
22378 "{\n}",
22379 Style);
22380 Style.ConstructorInitializerIndentWidth = 2;
22381 verifyFormat("SomeClass::Constructor()\n"
22382 "#if CONDITION\n"
22383 " : a{a}\n"
22384 "#endif\n"
22385 " , b{b}\n"
22386 " , c{c} {\n}",
22387 Style);
22388 Style.ConstructorInitializerIndentWidth = 0;
22389 verifyFormat("SomeClass::Constructor()\n"
22390 ": a{a}\n"
22391 "#ifdef CONDITION\n"
22392 ", b{b}\n"
22393 "#else\n"
22394 ", c{c}\n"
22395 "#endif\n"
22396 ", d{d} {\n}",
22397 Style);
22398 Style.ConstructorInitializerIndentWidth = 4;
22399 verifyFormat("SomeClass::Constructor()\n"
22400 " : a{a}\n"
22401 "#if WINDOWS\n"
22402 "#if DEBUG\n"
22403 " , b{0}\n"
22404 "#else\n"
22405 " , b{1}\n"
22406 "#endif\n"
22407 "#else\n"
22408 "#if DEBUG\n"
22409 " , b{2}\n"
22410 "#else\n"
22411 " , b{3}\n"
22412 "#endif\n"
22413 "#endif\n"
22414 "{\n}",
22415 Style);
22416 verifyFormat("SomeClass::Constructor()\n"
22417 " : a{a}\n"
22418 "#if WINDOWS\n"
22419 " , b{0}\n"
22420 "#if DEBUG\n"
22421 " , c{0}\n"
22422 "#else\n"
22423 " , c{1}\n"
22424 "#endif\n"
22425 "#else\n"
22426 "#if DEBUG\n"
22427 " , c{2}\n"
22428 "#else\n"
22429 " , c{3}\n"
22430 "#endif\n"
22431 " , b{1}\n"
22432 "#endif\n"
22433 "{\n}",
22434 Style);
22435 }
22436
22437 TEST_F(FormatTest, Destructors) {
22438 verifyFormat("void F(int &i) { i.~int(); }");
22439 verifyFormat("void F(int &i) { i->~int(); }");
22440 }
22441
22442 TEST_F(FormatTest, FormatsWithWebKitStyle) {
22443 FormatStyle Style = getWebKitStyle();
22444
22445 // Don't indent in outer namespaces.
22446 verifyFormat("namespace outer {\n"
22447 "int i;\n"
22448 "namespace inner {\n"
22449 " int i;\n"
22450 "} // namespace inner\n"
22451 "} // namespace outer\n"
22452 "namespace other_outer {\n"
22453 "int i;\n"
22454 "}",
22455 Style);
22456
22457 // Don't indent case labels.
22458 verifyFormat("switch (variable) {\n"
22459 "case 1:\n"
22460 "case 2:\n"
22461 " doSomething();\n"
22462 " break;\n"
22463 "default:\n"
22464 " ++variable;\n"
22465 "}",
22466 Style);
22467
22468 // Wrap before binary operators.
22469 verifyFormat(
22470 "void f()\n"
22471 "{\n"
22472 " if (aaaaaaaaaaaaaaaa\n"
22473 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22474 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22475 " return;\n"
22476 "}",
22477 "void f() {\n"
22478 "if (aaaaaaaaaaaaaaaa\n"
22479 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22480 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22481 "return;\n"
22482 "}",
22483 Style);
22484
22485 // Allow functions on a single line.
22486 verifyFormat("void f() { return; }", Style);
22487
22488 // Allow empty blocks on a single line and insert a space in empty blocks.
22489 verifyFormat("void f() { }", "void f() {}", Style);
22490 verifyFormat("while (true) { }", "while (true) {}", Style);
22491 // However, don't merge non-empty short loops.
22492 verifyFormat("while (true) {\n"
22493 " continue;\n"
22494 "}",
22495 "while (true) { continue; }", Style);
22496
22497 // Constructor initializers are formatted one per line with the "," on the
22498 // new line.
22499 verifyFormat("Constructor()\n"
22500 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22501 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22502 " aaaaaaaaaaaaaa)\n"
22503 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22504 "{\n"
22505 "}",
22506 Style);
22507 verifyFormat("SomeClass::Constructor()\n"
22508 " : a(a)\n"
22509 "{\n"
22510 "}",
22511 Style);
22512 verifyFormat("SomeClass::Constructor()\n"
22513 " : a(a)\n"
22514 "{\n"
22515 "}",
22516 "SomeClass::Constructor():a(a){}", Style);
22517 verifyFormat("SomeClass::Constructor()\n"
22518 " : a(a)\n"
22519 " , b(b)\n"
22520 " , c(c)\n"
22521 "{\n"
22522 "}",
22523 Style);
22524 verifyFormat("SomeClass::Constructor()\n"
22525 " : a(a)\n"
22526 "{\n"
22527 " foo();\n"
22528 " bar();\n"
22529 "}",
22530 Style);
22531
22532 // Access specifiers should be aligned left.
22533 verifyFormat("class C {\n"
22534 "public:\n"
22535 " int i;\n"
22536 "};",
22537 Style);
22538
22539 // Do not align comments.
22540 verifyFormat("int a; // Do not\n"
22541 "double b; // align comments.",
22542 Style);
22543
22544 // Do not align operands.
22545 verifyFormat("ASSERT(aaaa\n"
22546 " || bbbb);",
22547 "ASSERT ( aaaa\n||bbbb);", Style);
22548
22549 // Accept input's line breaks.
22550 verifyFormat("if (aaaaaaaaaaaaaaa\n"
22551 " || bbbbbbbbbbbbbbb) {\n"
22552 " i++;\n"
22553 "}",
22554 "if (aaaaaaaaaaaaaaa\n"
22555 "|| bbbbbbbbbbbbbbb) { i++; }",
22556 Style);
22557 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22558 " i++;\n"
22559 "}",
22560 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
22561
22562 // Don't automatically break all macro definitions (llvm.org/PR17842).
22563 verifyFormat("#define aNumber 10", Style);
22564 // However, generally keep the line breaks that the user authored.
22565 verifyFormat("#define aNumber \\\n"
22566 " 10",
22567 "#define aNumber \\\n"
22568 " 10",
22569 Style);
22570
22571 // Keep empty and one-element array literals on a single line.
22572 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22573 " copyItems:YES];",
22574 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22575 "copyItems:YES];",
22576 Style);
22577 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22578 " copyItems:YES];",
22579 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22580 " copyItems:YES];",
22581 Style);
22582 // FIXME: This does not seem right, there should be more indentation before
22583 // the array literal's entries. Nested blocks have the same problem.
22584 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22585 " @\"a\",\n"
22586 " @\"a\"\n"
22587 "]\n"
22588 " copyItems:YES];",
22589 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22590 " @\"a\",\n"
22591 " @\"a\"\n"
22592 " ]\n"
22593 " copyItems:YES];",
22594 Style);
22595 verifyFormat(
22596 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22597 " copyItems:YES];",
22598 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22599 " copyItems:YES];",
22600 Style);
22601
22602 verifyFormat("[self.a b:c c:d];", Style);
22603 verifyFormat("[self.a b:c\n"
22604 " c:d];",
22605 "[self.a b:c\n"
22606 "c:d];",
22607 Style);
22608 }
22609
22610 TEST_F(FormatTest, FormatsLambdas) {
22611 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22612 verifyFormat(
22613 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22614 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22615 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22616 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22617 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22618 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22619 verifyFormat("auto c = [a = [b = 42] {}] {};");
22620 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22621 verifyFormat("int x = f(*+[] {});");
22622 verifyFormat("void f() {\n"
22623 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22624 "}");
22625 verifyFormat("void f() {\n"
22626 " other(x.begin(), //\n"
22627 " x.end(), //\n"
22628 " [&](int, int) { return 1; });\n"
22629 "}");
22630 verifyFormat("void f() {\n"
22631 " other.other.other.other.other(\n"
22632 " x.begin(), x.end(),\n"
22633 " [something, rather](int, int, int, int, int, int, int) { "
22634 "return 1; });\n"
22635 "}");
22636 verifyFormat(
22637 "void f() {\n"
22638 " other.other.other.other.other(\n"
22639 " x.begin(), x.end(),\n"
22640 " [something, rather](int, int, int, int, int, int, int) {\n"
22641 " //\n"
22642 " });\n"
22643 "}");
22644 verifyFormat("SomeFunction([]() { // A cool function...\n"
22645 " return 43;\n"
22646 "});");
22647 verifyFormat("SomeFunction([]() {\n"
22648 "#define A a\n"
22649 " return 43;\n"
22650 "});",
22651 "SomeFunction([](){\n"
22652 "#define A a\n"
22653 "return 43;\n"
22654 "});");
22655 verifyFormat("void f() {\n"
22656 " SomeFunction([](decltype(x), A *a) {});\n"
22657 " SomeFunction([](typeof(x), A *a) {});\n"
22658 " SomeFunction([](_Atomic(x), A *a) {});\n"
22659 " SomeFunction([](__underlying_type(x), A *a) {});\n"
22660 "}");
22661 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22662 " [](const aaaaaaaaaa &a) { return a; });");
22663 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22664 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
22665 "});");
22666 verifyFormat("Constructor()\n"
22667 " : Field([] { // comment\n"
22668 " int i;\n"
22669 " }) {}");
22670 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22671 " return some_parameter.size();\n"
22672 "};");
22673 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22674 " [](const string &s) { return s; };");
22675 verifyFormat("int i = aaaaaa ? 1 //\n"
22676 " : [] {\n"
22677 " return 2; //\n"
22678 " }();");
22679 verifyFormat("llvm::errs() << \"number of twos is \"\n"
22680 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
22681 " return x == 2; // force break\n"
22682 " });");
22683 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22684 " [=](int iiiiiiiiiiii) {\n"
22685 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22686 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
22687 " });",
22688 getLLVMStyleWithColumns(60));
22689
22690 verifyFormat("SomeFunction({[&] {\n"
22691 " // comment\n"
22692 " },\n"
22693 " [&] {\n"
22694 " // comment\n"
22695 " }});");
22696 verifyFormat("SomeFunction({[&] {\n"
22697 " // comment\n"
22698 "}});");
22699 verifyFormat(
22700 "virtual aaaaaaaaaaaaaaaa(\n"
22701 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22702 " aaaaa aaaaaaaaa);");
22703
22704 // Lambdas with return types.
22705 verifyFormat("int c = []() -> int { return 2; }();");
22706 verifyFormat("int c = []() -> int * { return 2; }();");
22707 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22708 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22709 verifyFormat("foo([]() noexcept -> int {});");
22710 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22711 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22712 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22713 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22714 verifyFormat("[a, a]() -> a<1> {};");
22715 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22716 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22717 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22718 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22719 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22720 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22721 verifyFormat("[]() -> foo<!5> { return {}; };");
22722 verifyFormat("[]() -> foo<~5> { return {}; };");
22723 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22724 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22725 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22726 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22727 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22728 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22729 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22730 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22731 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22732 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22733 verifyFormat("namespace bar {\n"
22734 "// broken:\n"
22735 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22736 "} // namespace bar");
22737 verifyFormat("namespace bar {\n"
22738 "// broken:\n"
22739 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22740 "} // namespace bar");
22741 verifyFormat("namespace bar {\n"
22742 "// broken:\n"
22743 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22744 "} // namespace bar");
22745 verifyFormat("namespace bar {\n"
22746 "// broken:\n"
22747 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22748 "} // namespace bar");
22749 verifyFormat("namespace bar {\n"
22750 "// broken:\n"
22751 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22752 "} // namespace bar");
22753 verifyFormat("namespace bar {\n"
22754 "// broken:\n"
22755 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22756 "} // namespace bar");
22757 verifyFormat("namespace bar {\n"
22758 "// broken:\n"
22759 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22760 "} // namespace bar");
22761 verifyFormat("namespace bar {\n"
22762 "// broken:\n"
22763 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22764 "} // namespace bar");
22765 verifyFormat("namespace bar {\n"
22766 "// broken:\n"
22767 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22768 "} // namespace bar");
22769 verifyFormat("namespace bar {\n"
22770 "// broken:\n"
22771 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22772 "} // namespace bar");
22773 verifyFormat("namespace bar {\n"
22774 "// broken:\n"
22775 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22776 "} // namespace bar");
22777 verifyFormat("namespace bar {\n"
22778 "// broken:\n"
22779 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22780 "} // namespace bar");
22781 verifyFormat("namespace bar {\n"
22782 "// broken:\n"
22783 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22784 "} // namespace bar");
22785 verifyFormat("namespace bar {\n"
22786 "// broken:\n"
22787 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22788 "} // namespace bar");
22789 verifyFormat("namespace bar {\n"
22790 "// broken:\n"
22791 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22792 "} // namespace bar");
22793 verifyFormat("namespace bar {\n"
22794 "// broken:\n"
22795 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22796 "} // namespace bar");
22797 verifyFormat("namespace bar {\n"
22798 "// broken:\n"
22799 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22800 "} // namespace bar");
22801 verifyFormat("namespace bar {\n"
22802 "// broken:\n"
22803 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22804 "} // namespace bar");
22805 verifyFormat("[]() -> a<1> {};");
22806 verifyFormat("[]() -> a<1> { ; };");
22807 verifyFormat("[]() -> a<1> { ; }();");
22808 verifyFormat("[a, a]() -> a<true> {};");
22809 verifyFormat("[]() -> a<true> {};");
22810 verifyFormat("[]() -> a<true> { ; };");
22811 verifyFormat("[]() -> a<true> { ; }();");
22812 verifyFormat("[a, a]() -> a<false> {};");
22813 verifyFormat("[]() -> a<false> {};");
22814 verifyFormat("[]() -> a<false> { ; };");
22815 verifyFormat("[]() -> a<false> { ; }();");
22816 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22817 verifyFormat("namespace bar {\n"
22818 "auto foo{[]() -> foo<false> { ; }};\n"
22819 "} // namespace bar");
22820 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22821 " int j) -> int {\n"
22822 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22823 "};");
22824 verifyFormat(
22825 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22826 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22827 " return aaaaaaaaaaaaaaaaa;\n"
22828 " });",
22829 getLLVMStyleWithColumns(70));
22830 verifyFormat("[]() //\n"
22831 " -> int {\n"
22832 " return 1; //\n"
22833 "};");
22834 verifyFormat("[]() -> Void<T...> {};");
22835 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22836 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22837 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22838 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22839 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22840 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
22841 verifyFormat("return int{[x = x]() { return x; }()};");
22842
22843 // Lambdas with explicit template argument lists.
22844 verifyFormat(
22845 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22846 verifyFormat("auto L = []<class T>(T) {\n"
22847 " {\n"
22848 " f();\n"
22849 " g();\n"
22850 " }\n"
22851 "};");
22852 verifyFormat("auto L = []<class... T>(T...) {\n"
22853 " {\n"
22854 " f();\n"
22855 " g();\n"
22856 " }\n"
22857 "};");
22858 verifyFormat("auto L = []<typename... T>(T...) {\n"
22859 " {\n"
22860 " f();\n"
22861 " g();\n"
22862 " }\n"
22863 "};");
22864 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
22865 " {\n"
22866 " f();\n"
22867 " g();\n"
22868 " }\n"
22869 "};");
22870 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
22871 " {\n"
22872 " f();\n"
22873 " g();\n"
22874 " }\n"
22875 "};");
22876 verifyFormat("auto L = []<int... T>(T...) {\n"
22877 " {\n"
22878 " f();\n"
22879 " g();\n"
22880 " }\n"
22881 "};");
22882 verifyFormat("auto L = []<Foo... T>(T...) {\n"
22883 " {\n"
22884 " f();\n"
22885 " g();\n"
22886 " }\n"
22887 "};");
22888
22889 // Lambdas that fit on a single line within an argument list are not forced
22890 // onto new lines.
22891 verifyFormat("SomeFunction([] {});");
22892 verifyFormat("SomeFunction(0, [] {});");
22893 verifyFormat("SomeFunction([] {}, 0);");
22894 verifyFormat("SomeFunction(0, [] {}, 0);");
22895 verifyFormat("SomeFunction([] { return 0; }, 0);");
22896 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
22897 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
22898 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
22899 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
22900 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
22901 // Exceeded column limit. We need to break.
22902 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
22903 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
22904 "return 0; }, b);");
22905
22906 // Multiple multi-line lambdas in the same parentheses change indentation
22907 // rules. These lambdas are always forced to start on new lines.
22908 verifyFormat("SomeFunction(\n"
22909 " []() {\n"
22910 " //\n"
22911 " },\n"
22912 " []() {\n"
22913 " //\n"
22914 " });");
22915
22916 // A multi-line lambda passed as arg0 is always pushed to the next line.
22917 verifyFormat("SomeFunction(\n"
22918 " [this] {\n"
22919 " //\n"
22920 " },\n"
22921 " 1);");
22922
22923 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
22924 // the arg0 case above.
22925 auto Style = getGoogleStyle();
22926 Style.BinPackArguments = false;
22927 verifyFormat("SomeFunction(\n"
22928 " a,\n"
22929 " [this] {\n"
22930 " //\n"
22931 " },\n"
22932 " b);",
22933 Style);
22934 verifyFormat("SomeFunction(\n"
22935 " a,\n"
22936 " [this] {\n"
22937 " //\n"
22938 " },\n"
22939 " b);");
22940
22941 // A lambda with a very long line forces arg0 to be pushed out irrespective of
22942 // the BinPackArguments value (as long as the code is wide enough).
22943 verifyFormat(
22944 "something->SomeFunction(\n"
22945 " a,\n"
22946 " [this] {\n"
22947 " "
22948 "D0000000000000000000000000000000000000000000000000000000000001();\n"
22949 " },\n"
22950 " b);");
22951
22952 // A multi-line lambda is pulled up as long as the introducer fits on the
22953 // previous line and there are no further args.
22954 verifyFormat("function(1, [this, that] {\n"
22955 " //\n"
22956 "});");
22957 verifyFormat("function([this, that] {\n"
22958 " //\n"
22959 "});");
22960 // FIXME: this format is not ideal and we should consider forcing the first
22961 // arg onto its own line.
22962 verifyFormat("function(a, b, c, //\n"
22963 " d, [this, that] {\n"
22964 " //\n"
22965 " });");
22966
22967 // Multiple lambdas are treated correctly even when there is a short arg0.
22968 verifyFormat("SomeFunction(\n"
22969 " 1,\n"
22970 " [this] {\n"
22971 " //\n"
22972 " },\n"
22973 " [this] {\n"
22974 " //\n"
22975 " },\n"
22976 " 1);");
22977
22978 // More complex introducers.
22979 verifyFormat("return [i, args...] {};");
22980
22981 // Not lambdas.
22982 verifyFormat("constexpr char hello[]{\"hello\"};");
22983 verifyFormat("double &operator[](int i) { return 0; }\n"
22984 "int i;");
22985 verifyFormat("std::unique_ptr<int[]> foo() {}");
22986 verifyFormat("int i = a[a][a]->f();");
22987 verifyFormat("int i = (*b)[a]->f();");
22988
22989 // Other corner cases.
22990 verifyFormat("void f() {\n"
22991 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
22992 " );\n"
22993 "}");
22994 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
22995
22996 // Lambdas created through weird macros.
22997 verifyFormat("void f() {\n"
22998 " MACRO((const AA &a) { return 1; });\n"
22999 " MACRO((AA &a) { return 1; });\n"
23000 "}");
23001
23002 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23003 " doo_dah();\n"
23004 " doo_dah();\n"
23005 " })) {\n"
23006 "}");
23007 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23008 " doo_dah();\n"
23009 " doo_dah();\n"
23010 " })) {\n"
23011 "}");
23012 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23013 " doo_dah();\n"
23014 " doo_dah();\n"
23015 " })) {\n"
23016 "}");
23017 verifyFormat("auto lambda = []() {\n"
23018 " int a = 2\n"
23019 "#if A\n"
23020 " + 2\n"
23021 "#endif\n"
23022 " ;\n"
23023 "};");
23024
23025 // Lambdas with complex multiline introducers.
23026 verifyFormat(
23027 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23028 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23029 " -> ::std::unordered_set<\n"
23030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23031 " //\n"
23032 " });");
23033
23034 FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
23035 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23036 " [](auto n) noexcept [[back_attr]]\n"
23037 " -> std::unordered_map<very_long_type_name_A,\n"
23038 " very_long_type_name_B> {\n"
23039 " really_do_something();\n"
23040 " });",
23041 LLVMStyle);
23042 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23043 " [](auto n) constexpr\n"
23044 " -> std::unordered_map<very_long_type_name_A,\n"
23045 " very_long_type_name_B> {\n"
23046 " really_do_something();\n"
23047 " });",
23048 LLVMStyle);
23049
23050 FormatStyle DoNotMerge = getLLVMStyle();
23051 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
23052 verifyFormat("auto c = []() {\n"
23053 " return b;\n"
23054 "};",
23055 "auto c = []() { return b; };", DoNotMerge);
23056 verifyFormat("auto c = []() {\n"
23057 "};",
23058 " auto c = []() {};", DoNotMerge);
23059
23060 FormatStyle MergeEmptyOnly = getLLVMStyle();
23061 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
23062 verifyFormat("auto c = []() {\n"
23063 " return b;\n"
23064 "};",
23065 "auto c = []() {\n"
23066 " return b;\n"
23067 " };",
23068 MergeEmptyOnly);
23069 verifyFormat("auto c = []() {};",
23070 "auto c = []() {\n"
23071 "};",
23072 MergeEmptyOnly);
23073
23074 FormatStyle MergeInline = getLLVMStyle();
23075 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
23076 verifyFormat("auto c = []() {\n"
23077 " return b;\n"
23078 "};",
23079 "auto c = []() { return b; };", MergeInline);
23080 verifyFormat("function([]() { return b; })", MergeInline);
23081 verifyFormat("function([]() { return b; }, a)", MergeInline);
23082 verifyFormat("function(a, []() { return b; })", MergeInline);
23083
23084 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23085 // AllowShortLambdasOnASingleLine
23086 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23087 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23088 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23089 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23090 FormatStyle::ShortLambdaStyle::SLS_None;
23091 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23092 " []()\n"
23093 " {\n"
23094 " return 17;\n"
23095 " });",
23096 LLVMWithBeforeLambdaBody);
23097 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23098 " []()\n"
23099 " {\n"
23100 " });",
23101 LLVMWithBeforeLambdaBody);
23102 verifyFormat("auto fct_SLS_None = []()\n"
23103 "{\n"
23104 " return 17;\n"
23105 "};",
23106 LLVMWithBeforeLambdaBody);
23107 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23108 " []()\n"
23109 " {\n"
23110 " return Call(\n"
23111 " []()\n"
23112 " {\n"
23113 " return 17;\n"
23114 " });\n"
23115 " });",
23116 LLVMWithBeforeLambdaBody);
23117 verifyFormat("void Fct() {\n"
23118 " return {[]()\n"
23119 " {\n"
23120 " return 17;\n"
23121 " }};\n"
23122 "}",
23123 LLVMWithBeforeLambdaBody);
23124
23125 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23126 FormatStyle::ShortLambdaStyle::SLS_Empty;
23127 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23128 " []()\n"
23129 " {\n"
23130 " return 17;\n"
23131 " });",
23132 LLVMWithBeforeLambdaBody);
23133 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23134 LLVMWithBeforeLambdaBody);
23135 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23136 "ongFunctionName_SLS_Empty(\n"
23137 " []() {});",
23138 LLVMWithBeforeLambdaBody);
23139 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23140 " []()\n"
23141 " {\n"
23142 " return 17;\n"
23143 " });",
23144 LLVMWithBeforeLambdaBody);
23145 verifyFormat("auto fct_SLS_Empty = []()\n"
23146 "{\n"
23147 " return 17;\n"
23148 "};",
23149 LLVMWithBeforeLambdaBody);
23150 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23151 " []()\n"
23152 " {\n"
23153 " return Call([]() {});\n"
23154 " });",
23155 LLVMWithBeforeLambdaBody);
23156 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23157 " []()\n"
23158 " {\n"
23159 " return Call([]() {});\n"
23160 " });",
23161 LLVMWithBeforeLambdaBody);
23162 verifyFormat(
23163 "FctWithLongLineInLambda_SLS_Empty(\n"
23164 " []()\n"
23165 " {\n"
23166 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23167 " AndShouldNotBeConsiderAsInline,\n"
23168 " LambdaBodyMustBeBreak);\n"
23169 " });",
23170 LLVMWithBeforeLambdaBody);
23171
23172 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23173 FormatStyle::ShortLambdaStyle::SLS_Inline;
23174 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23175 LLVMWithBeforeLambdaBody);
23176 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23177 LLVMWithBeforeLambdaBody);
23178 verifyFormat("auto fct_SLS_Inline = []()\n"
23179 "{\n"
23180 " return 17;\n"
23181 "};",
23182 LLVMWithBeforeLambdaBody);
23183 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23184 "17; }); });",
23185 LLVMWithBeforeLambdaBody);
23186 verifyFormat(
23187 "FctWithLongLineInLambda_SLS_Inline(\n"
23188 " []()\n"
23189 " {\n"
23190 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23191 " AndShouldNotBeConsiderAsInline,\n"
23192 " LambdaBodyMustBeBreak);\n"
23193 " });",
23194 LLVMWithBeforeLambdaBody);
23195 verifyFormat("FctWithMultipleParams_SLS_Inline("
23196 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23197 " []() { return 17; });",
23198 LLVMWithBeforeLambdaBody);
23199 verifyFormat(
23200 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23201 LLVMWithBeforeLambdaBody);
23202
23203 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23204 FormatStyle::ShortLambdaStyle::SLS_All;
23205 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23206 LLVMWithBeforeLambdaBody);
23207 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23208 LLVMWithBeforeLambdaBody);
23209 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23210 LLVMWithBeforeLambdaBody);
23211 verifyFormat("FctWithOneParam_SLS_All(\n"
23212 " []()\n"
23213 " {\n"
23214 " // A cool function...\n"
23215 " return 43;\n"
23216 " });",
23217 LLVMWithBeforeLambdaBody);
23218 verifyFormat("FctWithMultipleParams_SLS_All("
23219 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23220 " []() { return 17; });",
23221 LLVMWithBeforeLambdaBody);
23222 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23223 LLVMWithBeforeLambdaBody);
23224 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23225 LLVMWithBeforeLambdaBody);
23226 verifyFormat(
23227 "FctWithLongLineInLambda_SLS_All(\n"
23228 " []()\n"
23229 " {\n"
23230 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23231 " AndShouldNotBeConsiderAsInline,\n"
23232 " LambdaBodyMustBeBreak);\n"
23233 " });",
23234 LLVMWithBeforeLambdaBody);
23235 verifyFormat(
23236 "auto fct_SLS_All = []()\n"
23237 "{\n"
23238 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23239 " AndShouldNotBeConsiderAsInline,\n"
23240 " LambdaBodyMustBeBreak);\n"
23241 "};",
23242 LLVMWithBeforeLambdaBody);
23243 LLVMWithBeforeLambdaBody.BinPackParameters = false;
23244 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23245 LLVMWithBeforeLambdaBody);
23246 verifyFormat(
23247 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23248 " FirstParam,\n"
23249 " SecondParam,\n"
23250 " ThirdParam,\n"
23251 " FourthParam);",
23252 LLVMWithBeforeLambdaBody);
23253 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23254 " []() { return "
23255 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23256 " FirstParam,\n"
23257 " SecondParam,\n"
23258 " ThirdParam,\n"
23259 " FourthParam);",
23260 LLVMWithBeforeLambdaBody);
23261 verifyFormat(
23262 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23263 " SecondParam,\n"
23264 " ThirdParam,\n"
23265 " FourthParam,\n"
23266 " []() { return SomeValueNotSoLong; });",
23267 LLVMWithBeforeLambdaBody);
23268 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23269 " []()\n"
23270 " {\n"
23271 " return "
23272 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23273 "eConsiderAsInline;\n"
23274 " });",
23275 LLVMWithBeforeLambdaBody);
23276 verifyFormat(
23277 "FctWithLongLineInLambda_SLS_All(\n"
23278 " []()\n"
23279 " {\n"
23280 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23281 " AndShouldNotBeConsiderAsInline,\n"
23282 " LambdaBodyMustBeBreak);\n"
23283 " });",
23284 LLVMWithBeforeLambdaBody);
23285 verifyFormat("FctWithTwoParams_SLS_All(\n"
23286 " []()\n"
23287 " {\n"
23288 " // A cool function...\n"
23289 " return 43;\n"
23290 " },\n"
23291 " 87);",
23292 LLVMWithBeforeLambdaBody);
23293 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23294 LLVMWithBeforeLambdaBody);
23295 verifyFormat(
23296 "FctWithTwoParams_SLS_All(\n"
23297 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23298 LLVMWithBeforeLambdaBody);
23299 verifyFormat(
23300 "FctWithTwoParams_SLS_All(\n"
23301 " 87,\n"
23302 " []()\n"
23303 " {\n"
23304 " return "
23305 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23306 " });",
23307 LLVMWithBeforeLambdaBody);
23308 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23309 LLVMWithBeforeLambdaBody);
23310 verifyFormat(
23311 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23312 LLVMWithBeforeLambdaBody);
23313 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23314 "}); }, x);",
23315 LLVMWithBeforeLambdaBody);
23316 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23317 " []()\n"
23318 " {\n"
23319 " // A cool function...\n"
23320 " return Call([]() { return 17; });\n"
23321 " });",
23322 LLVMWithBeforeLambdaBody);
23323 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23324 " []()\n"
23325 " {\n"
23326 " return Call(\n"
23327 " []()\n"
23328 " {\n"
23329 " // A cool function...\n"
23330 " return 17;\n"
23331 " });\n"
23332 " });",
23333 LLVMWithBeforeLambdaBody);
23334
23335 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23336 FormatStyle::ShortLambdaStyle::SLS_None;
23337
23338 verifyFormat("auto select = [this]() -> const Library::Object *\n"
23339 "{\n"
23340 " return MyAssignment::SelectFromList(this);\n"
23341 "};",
23342 LLVMWithBeforeLambdaBody);
23343
23344 verifyFormat("auto select = [this]() -> const Library::Object &\n"
23345 "{\n"
23346 " return MyAssignment::SelectFromList(this);\n"
23347 "};",
23348 LLVMWithBeforeLambdaBody);
23349
23350 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23351 "{\n"
23352 " return MyAssignment::SelectFromList(this);\n"
23353 "};",
23354 LLVMWithBeforeLambdaBody);
23355
23356 verifyFormat("namespace test {\n"
23357 "class Test {\n"
23358 "public:\n"
23359 " Test() = default;\n"
23360 "};\n"
23361 "} // namespace test",
23362 LLVMWithBeforeLambdaBody);
23363
23364 // Lambdas with different indentation styles.
23365 Style = getLLVMStyleWithColumns(60);
23366 verifyFormat("Result doSomething(Promise promise) {\n"
23367 " return promise.then(\n"
23368 " [this, obj = std::move(s)](int bar) mutable {\n"
23369 " return someObject.startAsyncAction().then(\n"
23370 " [this, &obj](Result result) mutable {\n"
23371 " result.processMore();\n"
23372 " });\n"
23373 " });\n"
23374 "}",
23375 Style);
23376 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
23377 verifyFormat("Result doSomething(Promise promise) {\n"
23378 " return promise.then(\n"
23379 " [this, obj = std::move(s)](int bar) mutable {\n"
23380 " return obj.startAsyncAction().then(\n"
23381 " [this, &obj](Result result) mutable {\n"
23382 " result.processMore();\n"
23383 " });\n"
23384 " });\n"
23385 "}",
23386 Style);
23387 verifyFormat("Result doSomething(Promise promise) {\n"
23388 " return promise.then([this, obj = std::move(s)] {\n"
23389 " return obj.startAsyncAction().then(\n"
23390 " [this, &obj](Result result) mutable {\n"
23391 " result.processMore();\n"
23392 " });\n"
23393 " });\n"
23394 "}",
23395 Style);
23396 verifyFormat("void test() {\n"
23397 " ([]() -> auto {\n"
23398 " int b = 32;\n"
23399 " return 3;\n"
23400 " }).foo();\n"
23401 "}",
23402 Style);
23403 verifyFormat("void test() {\n"
23404 " []() -> auto {\n"
23405 " int b = 32;\n"
23406 " return 3;\n"
23407 " }\n"
23408 "}",
23409 Style);
23410 verifyFormat("void test() {\n"
23411 " std::sort(v.begin(), v.end(),\n"
23412 " [](const auto &foo, const auto &bar) {\n"
23413 " return foo.baz < bar.baz;\n"
23414 " });\n"
23415 "};",
23416 Style);
23417 verifyFormat("void test() {\n"
23418 " (\n"
23419 " []() -> auto {\n"
23420 " int b = 32;\n"
23421 " return 3;\n"
23422 " }, foo, bar)\n"
23423 " .foo();\n"
23424 "}",
23425 Style);
23426 verifyFormat("void test() {\n"
23427 " ([]() -> auto {\n"
23428 " int b = 32;\n"
23429 " return 3;\n"
23430 " })\n"
23431 " .foo()\n"
23432 " .bar();\n"
23433 "}",
23434 Style);
23435 verifyFormat("#define A \\\n"
23436 " [] { \\\n"
23437 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
23438 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
23439 " }",
23440 Style);
23441 verifyFormat("#define SORT(v) \\\n"
23442 " std::sort(v.begin(), v.end(), \\\n"
23443 " [](const auto &foo, const auto &bar) { \\\n"
23444 " return foo.baz < bar.baz; \\\n"
23445 " });",
23446 Style);
23447 verifyFormat("void foo() {\n"
23448 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23449 " auto f = e(d);\n"
23450 " return f;\n"
23451 " })));\n"
23452 "}",
23453 Style);
23454 verifyFormat("void foo() {\n"
23455 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23456 " auto f = e(foo, [&] {\n"
23457 " auto g = h();\n"
23458 " return g;\n"
23459 " }, qux, [&] -> Bar {\n"
23460 " auto i = j();\n"
23461 " return i;\n"
23462 " });\n"
23463 " return f;\n"
23464 " })));\n"
23465 "}",
23466 Style);
23467 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23468 " AnotherLongClassName baz)\n"
23469 " : baz{baz}, func{[&] {\n"
23470 " auto qux = bar;\n"
23471 " return aFunkyFunctionCall(qux);\n"
23472 " }} {}",
23473 Style);
23474 verifyFormat("void foo() {\n"
23475 " class Foo {\n"
23476 " public:\n"
23477 " Foo()\n"
23478 " : qux{[](int quux) {\n"
23479 " auto tmp = quux;\n"
23480 " return tmp;\n"
23481 " }} {}\n"
23482 "\n"
23483 " private:\n"
23484 " std::function<void(int quux)> qux;\n"
23485 " };\n"
23486 "}",
23487 Style);
23488 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
23489 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23490 " AnotherLongClassName baz) :\n"
23491 " baz{baz}, func{[&] {\n"
23492 " auto qux = bar;\n"
23493 " return aFunkyFunctionCall(qux);\n"
23494 " }} {}",
23495 Style);
23496 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
23497 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23498 " AnotherLongClassName baz) :\n"
23499 " baz{baz},\n"
23500 " func{[&] {\n"
23501 " auto qux = bar;\n"
23502 " return aFunkyFunctionCall(qux);\n"
23503 " }} {}",
23504 Style);
23505 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
23506 // FIXME: The following test should pass, but fails at the time of writing.
23507 #if 0
23508 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23509 // doesn't force an initial line break, even if lambdas span multiple lines.
23510 verifyFormat("void foo() {\n"
23511 " aFunction(\n"
23512 " [](d) -> Foo {\n"
23513 " auto f = e(d);\n"
23514 " return f;\n"
23515 " }, foo, Bar{}, [] {\n"
23516 " auto g = h();\n"
23517 " return g;\n"
23518 " }, baz);\n"
23519 "}",
23520 Style);
23521 #endif
23522 // A long non-lambda argument forces arguments to span multiple lines and thus
23523 // forces an initial line break when using AlwaysBreak.
23524 verifyFormat("void foo() {\n"
23525 " aFunction(\n"
23526 " 1,\n"
23527 " [](d) -> Foo {\n"
23528 " auto f = e(d);\n"
23529 " return f;\n"
23530 " }, foo, Bar{},\n"
23531 " [] {\n"
23532 " auto g = h();\n"
23533 " return g;\n"
23534 " }, bazzzzz,\n"
23535 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23536 "}",
23537 Style);
23538 Style.BinPackArguments = false;
23539 verifyFormat("void foo() {\n"
23540 " aFunction(\n"
23541 " 1,\n"
23542 " [](d) -> Foo {\n"
23543 " auto f = e(d);\n"
23544 " return f;\n"
23545 " },\n"
23546 " foo,\n"
23547 " Bar{},\n"
23548 " [] {\n"
23549 " auto g = h();\n"
23550 " return g;\n"
23551 " },\n"
23552 " bazzzzz,\n"
23553 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23554 "}",
23555 Style);
23556 Style.BinPackArguments = true;
23557 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23558 Style.BraceWrapping.BeforeLambdaBody = true;
23559 verifyFormat("void foo() {\n"
23560 " aFunction(\n"
23561 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23562 " {\n"
23563 " auto f = e(\n"
23564 " [&]\n"
23565 " {\n"
23566 " auto g = h();\n"
23567 " return g;\n"
23568 " }, qux, [&] -> Bar\n"
23569 " {\n"
23570 " auto i = j();\n"
23571 " return i;\n"
23572 " });\n"
23573 " return f;\n"
23574 " })));\n"
23575 "}",
23576 Style);
23577 }
23578
23579 TEST_F(FormatTest, LambdaWithLineComments) {
23580 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23581 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23582 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23583 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23584 FormatStyle::ShortLambdaStyle::SLS_All;
23585
23586 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
23587 verifyFormat("auto k = []() // comment\n"
23588 "{ return; }",
23589 LLVMWithBeforeLambdaBody);
23590 verifyFormat("auto k = []() /* comment */ { return; }",
23591 LLVMWithBeforeLambdaBody);
23592 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23593 LLVMWithBeforeLambdaBody);
23594 verifyFormat("auto k = []() // X\n"
23595 "{ return; }",
23596 LLVMWithBeforeLambdaBody);
23597 verifyFormat(
23598 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23599 "{ return; }",
23600 LLVMWithBeforeLambdaBody);
23601
23602 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
23603
23604 verifyFormat("foo([]()\n"
23605 " {\n"
23606 " bar(); //\n"
23607 " return 1; // comment\n"
23608 " }());",
23609 "foo([]() {\n"
23610 " bar(); //\n"
23611 " return 1; // comment\n"
23612 "}());",
23613 LLVMWithBeforeLambdaBody);
23614 verifyFormat("foo(\n"
23615 " 1, MACRO {\n"
23616 " baz();\n"
23617 " bar(); // comment\n"
23618 " },\n"
23619 " []() {});",
23620 "foo(\n"
23621 " 1, MACRO { baz(); bar(); // comment\n"
23622 " }, []() {}\n"
23623 ");",
23624 LLVMWithBeforeLambdaBody);
23625 }
23626
23627 TEST_F(FormatTest, EmptyLinesInLambdas) {
23628 verifyFormat("auto lambda = []() {\n"
23629 " x(); //\n"
23630 "};",
23631 "auto lambda = []() {\n"
23632 "\n"
23633 " x(); //\n"
23634 "\n"
23635 "};");
23636 }
23637
23638 TEST_F(FormatTest, FormatsBlocks) {
23639 FormatStyle ShortBlocks = getLLVMStyle();
23640 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23641 verifyFormat("int (^Block)(int, int);", ShortBlocks);
23642 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
23643 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
23644 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
23645 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
23646 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
23647
23648 verifyFormat("foo(^{ bar(); });", ShortBlocks);
23649 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
23650 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
23651
23652 verifyFormat("[operation setCompletionBlock:^{\n"
23653 " [self onOperationDone];\n"
23654 "}];");
23655 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23656 " [self onOperationDone];\n"
23657 "}]};");
23658 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23659 " f();\n"
23660 "}];");
23661 verifyFormat("int a = [operation block:^int(int *i) {\n"
23662 " return 1;\n"
23663 "}];");
23664 verifyFormat("[myObject doSomethingWith:arg1\n"
23665 " aaa:^int(int *a) {\n"
23666 " return 1;\n"
23667 " }\n"
23668 " bbb:f(a * bbbbbbbb)];");
23669
23670 verifyFormat("[operation setCompletionBlock:^{\n"
23671 " [self.delegate newDataAvailable];\n"
23672 "}];",
23673 getLLVMStyleWithColumns(60));
23674 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23675 " NSString *path = [self sessionFilePath];\n"
23676 " if (path) {\n"
23677 " // ...\n"
23678 " }\n"
23679 "});");
23680 verifyFormat("[[SessionService sharedService]\n"
23681 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23682 " if (window) {\n"
23683 " [self windowDidLoad:window];\n"
23684 " } else {\n"
23685 " [self errorLoadingWindow];\n"
23686 " }\n"
23687 " }];");
23688 verifyFormat("void (^largeBlock)(void) = ^{\n"
23689 " // ...\n"
23690 "};",
23691 getLLVMStyleWithColumns(40));
23692 verifyFormat("[[SessionService sharedService]\n"
23693 " loadWindowWithCompletionBlock: //\n"
23694 " ^(SessionWindow *window) {\n"
23695 " if (window) {\n"
23696 " [self windowDidLoad:window];\n"
23697 " } else {\n"
23698 " [self errorLoadingWindow];\n"
23699 " }\n"
23700 " }];",
23701 getLLVMStyleWithColumns(60));
23702 verifyFormat("[myObject doSomethingWith:arg1\n"
23703 " firstBlock:^(Foo *a) {\n"
23704 " // ...\n"
23705 " int i;\n"
23706 " }\n"
23707 " secondBlock:^(Bar *b) {\n"
23708 " // ...\n"
23709 " int i;\n"
23710 " }\n"
23711 " thirdBlock:^Foo(Bar *b) {\n"
23712 " // ...\n"
23713 " int i;\n"
23714 " }];");
23715 verifyFormat("[myObject doSomethingWith:arg1\n"
23716 " firstBlock:-1\n"
23717 " secondBlock:^(Bar *b) {\n"
23718 " // ...\n"
23719 " int i;\n"
23720 " }];");
23721
23722 verifyFormat("f(^{\n"
23723 " @autoreleasepool {\n"
23724 " if (a) {\n"
23725 " g();\n"
23726 " }\n"
23727 " }\n"
23728 "});");
23729 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23730 "};");
23731 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23732 "};");
23733
23734 FormatStyle FourIndent = getLLVMStyle();
23735 FourIndent.ObjCBlockIndentWidth = 4;
23736 verifyFormat("[operation setCompletionBlock:^{\n"
23737 " [self onOperationDone];\n"
23738 "}];",
23739 FourIndent);
23740 }
23741
23742 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
23743 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
23744
23745 verifyFormat("[[SessionService sharedService] "
23746 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23747 " if (window) {\n"
23748 " [self windowDidLoad:window];\n"
23749 " } else {\n"
23750 " [self errorLoadingWindow];\n"
23751 " }\n"
23752 "}];",
23753 ZeroColumn);
23754 verifyFormat("[[SessionService sharedService]\n"
23755 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23756 " if (window) {\n"
23757 " [self windowDidLoad:window];\n"
23758 " } else {\n"
23759 " [self errorLoadingWindow];\n"
23760 " }\n"
23761 " }];",
23762 "[[SessionService sharedService]\n"
23763 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23764 " if (window) {\n"
23765 " [self windowDidLoad:window];\n"
23766 " } else {\n"
23767 " [self errorLoadingWindow];\n"
23768 " }\n"
23769 "}];",
23770 ZeroColumn);
23771 verifyFormat("[myObject doSomethingWith:arg1\n"
23772 " firstBlock:^(Foo *a) {\n"
23773 " // ...\n"
23774 " int i;\n"
23775 " }\n"
23776 " secondBlock:^(Bar *b) {\n"
23777 " // ...\n"
23778 " int i;\n"
23779 " }\n"
23780 " thirdBlock:^Foo(Bar *b) {\n"
23781 " // ...\n"
23782 " int i;\n"
23783 " }];",
23784 ZeroColumn);
23785 verifyFormat("f(^{\n"
23786 " @autoreleasepool {\n"
23787 " if (a) {\n"
23788 " g();\n"
23789 " }\n"
23790 " }\n"
23791 "});",
23792 ZeroColumn);
23793 verifyFormat("void (^largeBlock)(void) = ^{\n"
23794 " // ...\n"
23795 "};",
23796 ZeroColumn);
23797
23798 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23799 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23800 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23801 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23802 verifyFormat("void (^largeBlock)(void) = ^{\n"
23803 " int i;\n"
23804 "};",
23805 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23806 }
23807
23808 TEST_F(FormatTest, SupportsCRLF) {
23809 verifyFormat("int a;\r\n"
23810 "int b;\r\n"
23811 "int c;",
23812 "int a;\r\n"
23813 " int b;\r\n"
23814 " int c;");
23815 verifyFormat("int a;\r\n"
23816 "int b;\r\n"
23817 "int c;\r\n",
23818 "int a;\r\n"
23819 " int b;\n"
23820 " int c;\r\n");
23821 verifyFormat("int a;\n"
23822 "int b;\n"
23823 "int c;",
23824 "int a;\r\n"
23825 " int b;\n"
23826 " int c;");
23827 // FIXME: unstable test case
23828 EXPECT_EQ("\"aaaaaaa \"\r\n"
23829 "\"bbbbbbb\";\r\n",
23830 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23831 verifyFormat("#define A \\\r\n"
23832 " b; \\\r\n"
23833 " c; \\\r\n"
23834 " d;",
23835 "#define A \\\r\n"
23836 " b; \\\r\n"
23837 " c; d; ",
23838 getGoogleStyle());
23839
23840 verifyNoChange("/*\r\n"
23841 "multi line block comments\r\n"
23842 "should not introduce\r\n"
23843 "an extra carriage return\r\n"
23844 "*/");
23845 verifyFormat("/*\r\n"
23846 "\r\n"
23847 "*/",
23848 "/*\r\n"
23849 " \r\r\r\n"
23850 "*/");
23851
23852 FormatStyle style = getLLVMStyle();
23853
23854 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
23855 verifyFormat("union FooBarBazQux {\n"
23856 " int foo;\n"
23857 " int bar;\n"
23858 " int baz;\n"
23859 "};",
23860 "union FooBarBazQux {\r\n"
23861 " int foo;\n"
23862 " int bar;\r\n"
23863 " int baz;\n"
23864 "};",
23865 style);
23866 style.LineEnding = FormatStyle::LE_DeriveCRLF;
23867 verifyFormat("union FooBarBazQux {\r\n"
23868 " int foo;\r\n"
23869 " int bar;\r\n"
23870 " int baz;\r\n"
23871 "};",
23872 "union FooBarBazQux {\r\n"
23873 " int foo;\n"
23874 " int bar;\r\n"
23875 " int baz;\n"
23876 "};",
23877 style);
23878
23879 style.LineEnding = FormatStyle::LE_LF;
23880 verifyFormat("union FooBarBazQux {\n"
23881 " int foo;\n"
23882 " int bar;\n"
23883 " int baz;\n"
23884 " int qux;\n"
23885 "};",
23886 "union FooBarBazQux {\r\n"
23887 " int foo;\n"
23888 " int bar;\r\n"
23889 " int baz;\n"
23890 " int qux;\r\n"
23891 "};",
23892 style);
23893 style.LineEnding = FormatStyle::LE_CRLF;
23894 verifyFormat("union FooBarBazQux {\r\n"
23895 " int foo;\r\n"
23896 " int bar;\r\n"
23897 " int baz;\r\n"
23898 " int qux;\r\n"
23899 "};",
23900 "union FooBarBazQux {\r\n"
23901 " int foo;\n"
23902 " int bar;\r\n"
23903 " int baz;\n"
23904 " int qux;\n"
23905 "};",
23906 style);
23907
23908 style.LineEnding = FormatStyle::LE_DeriveLF;
23909 verifyFormat("union FooBarBazQux {\r\n"
23910 " int foo;\r\n"
23911 " int bar;\r\n"
23912 " int baz;\r\n"
23913 " int qux;\r\n"
23914 "};",
23915 "union FooBarBazQux {\r\n"
23916 " int foo;\n"
23917 " int bar;\r\n"
23918 " int baz;\n"
23919 " int qux;\r\n"
23920 "};",
23921 style);
23922 style.LineEnding = FormatStyle::LE_DeriveCRLF;
23923 verifyFormat("union FooBarBazQux {\n"
23924 " int foo;\n"
23925 " int bar;\n"
23926 " int baz;\n"
23927 " int qux;\n"
23928 "};",
23929 "union FooBarBazQux {\r\n"
23930 " int foo;\n"
23931 " int bar;\r\n"
23932 " int baz;\n"
23933 " int qux;\n"
23934 "};",
23935 style);
23936 }
23937
23938 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
23939 verifyFormat("MY_CLASS(C) {\n"
23940 " int i;\n"
23941 " int j;\n"
23942 "};");
23943 }
23944
23945 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
23946 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
23947 TwoIndent.ContinuationIndentWidth = 2;
23948
23949 verifyFormat("int i =\n"
23950 " longFunction(\n"
23951 " arg);",
23952 "int i = longFunction(arg);", TwoIndent);
23953
23954 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
23955 SixIndent.ContinuationIndentWidth = 6;
23956
23957 verifyFormat("int i =\n"
23958 " longFunction(\n"
23959 " arg);",
23960 "int i = longFunction(arg);", SixIndent);
23961 }
23962
23963 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
23964 FormatStyle Style = getLLVMStyle();
23965 verifyFormat("int Foo::getter(\n"
23966 " //\n"
23967 ") const {\n"
23968 " return foo;\n"
23969 "}",
23970 Style);
23971 verifyFormat("void Foo::setter(\n"
23972 " //\n"
23973 ") {\n"
23974 " foo = 1;\n"
23975 "}",
23976 Style);
23977 }
23978
23979 TEST_F(FormatTest, SpacesInAngles) {
23980 FormatStyle Spaces = getLLVMStyle();
23981 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23982
23983 verifyFormat("vector< ::std::string > x1;", Spaces);
23984 verifyFormat("Foo< int, Bar > x2;", Spaces);
23985 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
23986
23987 verifyFormat("static_cast< int >(arg);", Spaces);
23988 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
23989 verifyFormat("f< int, float >();", Spaces);
23990 verifyFormat("template <> g() {}", Spaces);
23991 verifyFormat("template < std::vector< int > > f() {}", Spaces);
23992 verifyFormat("std::function< void(int, int) > fct;", Spaces);
23993 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
23994 Spaces);
23995
23996 Spaces.Standard = FormatStyle::LS_Cpp03;
23997 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23998 verifyFormat("A< A< int > >();", Spaces);
23999
24000 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24001 verifyFormat("A<A<int> >();", Spaces);
24002
24003 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24004 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24005 Spaces);
24006 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24007 Spaces);
24008
24009 verifyFormat("A<A<int> >();", Spaces);
24010 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
24011 verifyFormat("A< A< int > >();", Spaces);
24012
24013 Spaces.Standard = FormatStyle::LS_Cpp11;
24014 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24015 verifyFormat("A< A< int > >();", Spaces);
24016
24017 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24018 verifyFormat("vector<::std::string> x4;", Spaces);
24019 verifyFormat("vector<int> x5;", Spaces);
24020 verifyFormat("Foo<int, Bar> x6;", Spaces);
24021 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24022
24023 verifyFormat("A<A<int>>();", Spaces);
24024
24025 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24026 verifyFormat("vector<::std::string> x4;", Spaces);
24027 verifyFormat("vector< ::std::string > x4;", Spaces);
24028 verifyFormat("vector<int> x5;", Spaces);
24029 verifyFormat("vector< int > x5;", Spaces);
24030 verifyFormat("Foo<int, Bar> x6;", Spaces);
24031 verifyFormat("Foo< int, Bar > x6;", Spaces);
24032 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24033 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
24034
24035 verifyFormat("A<A<int>>();", Spaces);
24036 verifyFormat("A< A< int > >();", Spaces);
24037 verifyFormat("A<A<int > >();", Spaces);
24038 verifyFormat("A< A< int>>();", Spaces);
24039
24040 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24041 verifyFormat("// clang-format off\n"
24042 "foo<<<1, 1>>>();\n"
24043 "// clang-format on",
24044 Spaces);
24045 verifyFormat("// clang-format off\n"
24046 "foo< < <1, 1> > >();\n"
24047 "// clang-format on",
24048 Spaces);
24049 }
24050
24051 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
24052 FormatStyle Style = getLLVMStyle();
24053 Style.SpaceAfterTemplateKeyword = false;
24054 verifyFormat("template<int> void foo();", Style);
24055 }
24056
24057 TEST_F(FormatTest, TripleAngleBrackets) {
24058 verifyFormat("f<<<1, 1>>>();");
24059 verifyFormat("f<<<1, 1, 1, s>>>();");
24060 verifyFormat("f<<<a, b, c, d>>>();");
24061 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24062 verifyFormat("f<param><<<1, 1>>>();");
24063 verifyFormat("f<1><<<1, 1>>>();");
24064 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24065 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24066 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24067 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24068 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24069 }
24070
24071 TEST_F(FormatTest, MergeLessLessAtEnd) {
24072 verifyFormat("<<");
24073 verifyFormat("< < <", "\\\n<<<");
24074 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24075 "aaallvm::outs() <<");
24076 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24077 "aaaallvm::outs()\n <<");
24078 }
24079
24080 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
24081 std::string code = "#if A\n"
24082 "#if B\n"
24083 "a.\n"
24084 "#endif\n"
24085 " a = 1;\n"
24086 "#else\n"
24087 "#endif\n"
24088 "#if C\n"
24089 "#else\n"
24090 "#endif\n";
24091 verifyFormat(code);
24092 }
24093
24094 TEST_F(FormatTest, HandleConflictMarkers) {
24095 // Git/SVN conflict markers.
24096 verifyFormat("int a;\n"
24097 "void f() {\n"
24098 " callme(some(parameter1,\n"
24099 "<<<<<<< text by the vcs\n"
24100 " parameter2),\n"
24101 "||||||| text by the vcs\n"
24102 " parameter2),\n"
24103 " parameter3,\n"
24104 "======= text by the vcs\n"
24105 " parameter2, parameter3),\n"
24106 ">>>>>>> text by the vcs\n"
24107 " otherparameter);",
24108 "int a;\n"
24109 "void f() {\n"
24110 " callme(some(parameter1,\n"
24111 "<<<<<<< text by the vcs\n"
24112 " parameter2),\n"
24113 "||||||| text by the vcs\n"
24114 " parameter2),\n"
24115 " parameter3,\n"
24116 "======= text by the vcs\n"
24117 " parameter2,\n"
24118 " parameter3),\n"
24119 ">>>>>>> text by the vcs\n"
24120 " otherparameter);");
24121
24122 // Perforce markers.
24123 verifyFormat("void f() {\n"
24124 " function(\n"
24125 ">>>> text by the vcs\n"
24126 " parameter,\n"
24127 "==== text by the vcs\n"
24128 " parameter,\n"
24129 "==== text by the vcs\n"
24130 " parameter,\n"
24131 "<<<< text by the vcs\n"
24132 " parameter);",
24133 "void f() {\n"
24134 " function(\n"
24135 ">>>> text by the vcs\n"
24136 " parameter,\n"
24137 "==== text by the vcs\n"
24138 " parameter,\n"
24139 "==== text by the vcs\n"
24140 " parameter,\n"
24141 "<<<< text by the vcs\n"
24142 " parameter);");
24143
24144 verifyNoChange("<<<<<<<\n"
24145 "|||||||\n"
24146 "=======\n"
24147 ">>>>>>>");
24148
24149 verifyNoChange("<<<<<<<\n"
24150 "|||||||\n"
24151 "int i;\n"
24152 "=======\n"
24153 ">>>>>>>");
24154
24155 // FIXME: Handle parsing of macros around conflict markers correctly:
24156 verifyFormat("#define Macro \\\n"
24157 "<<<<<<<\n"
24158 "Something \\\n"
24159 "|||||||\n"
24160 "Else \\\n"
24161 "=======\n"
24162 "Other \\\n"
24163 ">>>>>>>\n"
24164 " End int i;",
24165 "#define Macro \\\n"
24166 "<<<<<<<\n"
24167 " Something \\\n"
24168 "|||||||\n"
24169 " Else \\\n"
24170 "=======\n"
24171 " Other \\\n"
24172 ">>>>>>>\n"
24173 " End\n"
24174 "int i;");
24175
24176 verifyFormat(R"(====
24177 #ifdef A
24178 a
24179 #else
24180 b
24181 #endif
24182 )");
24183 }
24184
24185 TEST_F(FormatTest, DisableRegions) {
24186 verifyFormat("int i;\n"
24187 "// clang-format off\n"
24188 " int j;\n"
24189 "// clang-format on\n"
24190 "int k;",
24191 " int i;\n"
24192 " // clang-format off\n"
24193 " int j;\n"
24194 " // clang-format on\n"
24195 " int k;");
24196 verifyFormat("int i;\n"
24197 "/* clang-format off */\n"
24198 " int j;\n"
24199 "/* clang-format on */\n"
24200 "int k;",
24201 " int i;\n"
24202 " /* clang-format off */\n"
24203 " int j;\n"
24204 " /* clang-format on */\n"
24205 " int k;");
24206
24207 // Don't reflow comments within disabled regions.
24208 verifyFormat("// clang-format off\n"
24209 "// long long long long long long line\n"
24210 "/* clang-format on */\n"
24211 "/* long long long\n"
24212 " * long long long\n"
24213 " * line */\n"
24214 "int i;\n"
24215 "/* clang-format off */\n"
24216 "/* long long long long long long line */",
24217 "// clang-format off\n"
24218 "// long long long long long long line\n"
24219 "/* clang-format on */\n"
24220 "/* long long long long long long line */\n"
24221 "int i;\n"
24222 "/* clang-format off */\n"
24223 "/* long long long long long long line */",
24224 getLLVMStyleWithColumns(20));
24225
24226 verifyFormat("int *i;\n"
24227 "// clang-format off:\n"
24228 "int* j;\n"
24229 "// clang-format on: 1\n"
24230 "int *k;",
24231 "int* i;\n"
24232 "// clang-format off:\n"
24233 "int* j;\n"
24234 "// clang-format on: 1\n"
24235 "int* k;");
24236
24237 verifyFormat("int *i;\n"
24238 "// clang-format off:0\n"
24239 "int* j;\n"
24240 "// clang-format only\n"
24241 "int* k;",
24242 "int* i;\n"
24243 "// clang-format off:0\n"
24244 "int* j;\n"
24245 "// clang-format only\n"
24246 "int* k;");
24247
24248 verifyNoChange("// clang-format off\n"
24249 "#if 0\n"
24250 " #if SHOULD_STAY_INDENTED\n"
24251 " #endif\n"
24252 "#endif\n"
24253 "// clang-format on");
24254 }
24255
24256 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
24257 format("? ) =");
24258 verifyNoCrash("#define a\\\n /**/}");
24259 }
24260
24261 TEST_F(FormatTest, FormatsTableGenCode) {
24262 FormatStyle Style = getLLVMStyle();
24263 Style.Language = FormatStyle::LK_TableGen;
24264 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
24265 }
24266
24267 TEST_F(FormatTest, ArrayOfTemplates) {
24268 verifyFormat("auto a = new unique_ptr<int>[10];",
24269 "auto a = new unique_ptr<int > [ 10];");
24270
24271 FormatStyle Spaces = getLLVMStyle();
24272 Spaces.SpacesInSquareBrackets = true;
24273 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24274 "auto a = new unique_ptr<int > [10];", Spaces);
24275 }
24276
24277 TEST_F(FormatTest, ArrayAsTemplateType) {
24278 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24279 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24280
24281 FormatStyle Spaces = getLLVMStyle();
24282 Spaces.SpacesInSquareBrackets = true;
24283 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24284 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
24285 }
24286
24287 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
24288
24289 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
24290 verifyFormat("using std::cin;\n"
24291 "using std::cout;",
24292 "using std::cout;\n"
24293 "using std::cin;",
24294 getGoogleStyle());
24295 }
24296
24297 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
24298 FormatStyle Style = getLLVMStyle();
24299 Style.Standard = FormatStyle::LS_Cpp03;
24300 // cpp03 recognize this string as identifier u8 and literal character 'a'
24301 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
24302 }
24303
24304 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
24305 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24306 // all modes, including C++11, C++14 and C++17
24307 verifyFormat("auto c = u8'a';");
24308 }
24309
24310 TEST_F(FormatTest, DoNotFormatLikelyXml) {
24311 verifyGoogleFormat("<!-- ;> -->");
24312 verifyNoChange(" <!-- >; -->", getGoogleStyle());
24313 }
24314
24315 TEST_F(FormatTest, StructuredBindings) {
24316 // Structured bindings is a C++17 feature.
24317 // all modes, including C++11, C++14 and C++17
24318 verifyFormat("auto [a, b] = f();");
24319 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24320 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
24321 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
24322 verifyFormat("auto const volatile [a, b] = f();",
24323 "auto const volatile[a, b] = f();");
24324 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
24325 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
24326 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
24327 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
24328 verifyFormat("auto const volatile &&[a, b] = f();",
24329 "auto const volatile &&[a, b] = f();");
24330 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
24331 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
24332 verifyFormat("const auto volatile &&[a, b] = f();",
24333 "const auto volatile &&[a, b] = f();");
24334 verifyFormat("volatile const auto &&[a, b] = f();",
24335 "volatile const auto &&[a, b] = f();");
24336 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
24337
24338 // Make sure we don't mistake structured bindings for lambdas.
24339 FormatStyle PointerMiddle = getLLVMStyle();
24340 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
24341 verifyGoogleFormat("auto [a1, b]{A * i};");
24342 verifyFormat("auto [a2, b]{A * i};");
24343 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
24344 verifyGoogleFormat("auto const [a1, b]{A * i};");
24345 verifyFormat("auto const [a2, b]{A * i};");
24346 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
24347 verifyGoogleFormat("auto const& [a1, b]{A * i};");
24348 verifyFormat("auto const &[a2, b]{A * i};");
24349 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
24350 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24351 verifyFormat("auto const &&[a2, b]{A * i};");
24352 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
24353
24354 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24355 "for (const auto && [a, b] : some_range) {\n}");
24356 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24357 "for (const auto & [a, b] : some_range) {\n}");
24358 verifyFormat("for (const auto [a, b] : some_range) {\n}",
24359 "for (const auto[a, b] : some_range) {\n}");
24360 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
24361 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
24362 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
24363 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
24364 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
24365 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
24366 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
24367 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
24368
24369 FormatStyle Spaces = getLLVMStyle();
24370 Spaces.SpacesInSquareBrackets = true;
24371 verifyFormat("auto [ a, b ] = f();", Spaces);
24372 verifyFormat("auto &&[ a, b ] = f();", Spaces);
24373 verifyFormat("auto &[ a, b ] = f();", Spaces);
24374 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
24375 verifyFormat("auto const &[ a, b ] = f();", Spaces);
24376 }
24377
24378 TEST_F(FormatTest, FileAndCode) {
24379 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
24380 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
24381 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
24382 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
24383 EXPECT_EQ(FormatStyle::LK_ObjC,
24384 guessLanguage("foo.h", "@interface Foo\n@end"));
24385 EXPECT_EQ(
24386 FormatStyle::LK_ObjC,
24387 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24388 EXPECT_EQ(FormatStyle::LK_ObjC,
24389 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24390 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
24391 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
24392 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
24393 EXPECT_EQ(FormatStyle::LK_ObjC,
24394 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24395 EXPECT_EQ(FormatStyle::LK_ObjC,
24396 guessLanguage(
24397 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24398 EXPECT_EQ(
24399 FormatStyle::LK_Cpp,
24400 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24401 // Only one of the two preprocessor regions has ObjC-like code.
24402 EXPECT_EQ(FormatStyle::LK_ObjC,
24403 guessLanguage("foo.h", "#if A\n"
24404 "#define B() C\n"
24405 "#else\n"
24406 "#define B() [NSString a:@\"\"]\n"
24407 "#endif"));
24408 }
24409
24410 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
24411 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
24412 EXPECT_EQ(FormatStyle::LK_ObjC,
24413 guessLanguage("foo.h", "array[[calculator getIndex]];"));
24414 EXPECT_EQ(FormatStyle::LK_Cpp,
24415 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24416 EXPECT_EQ(
24417 FormatStyle::LK_Cpp,
24418 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24419 EXPECT_EQ(FormatStyle::LK_ObjC,
24420 guessLanguage("foo.h", "[[noreturn foo] bar];"));
24421 EXPECT_EQ(FormatStyle::LK_Cpp,
24422 guessLanguage("foo.h", "[[clang::fallthrough]];"));
24423 EXPECT_EQ(FormatStyle::LK_ObjC,
24424 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24425 EXPECT_EQ(FormatStyle::LK_Cpp,
24426 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24427 EXPECT_EQ(FormatStyle::LK_Cpp,
24428 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24429 EXPECT_EQ(FormatStyle::LK_ObjC,
24430 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24431 EXPECT_EQ(FormatStyle::LK_Cpp,
24432 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24433 EXPECT_EQ(
24434 FormatStyle::LK_Cpp,
24435 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24436 EXPECT_EQ(
24437 FormatStyle::LK_Cpp,
24438 guessLanguage("foo.h",
24439 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24440 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24441 }
24442
24443 TEST_F(FormatTest, GuessLanguageWithCaret) {
24444 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
24445 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
24446 EXPECT_EQ(FormatStyle::LK_ObjC,
24447 guessLanguage("foo.h", "int(^)(char, float);"));
24448 EXPECT_EQ(FormatStyle::LK_ObjC,
24449 guessLanguage("foo.h", "int(^foo)(char, float);"));
24450 EXPECT_EQ(FormatStyle::LK_ObjC,
24451 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24452 EXPECT_EQ(FormatStyle::LK_ObjC,
24453 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24454 EXPECT_EQ(
24455 FormatStyle::LK_ObjC,
24456 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24457 }
24458
24459 TEST_F(FormatTest, GuessLanguageWithPragmas) {
24460 EXPECT_EQ(FormatStyle::LK_Cpp,
24461 guessLanguage("foo.h", "__pragma(warning(disable:))"));
24462 EXPECT_EQ(FormatStyle::LK_Cpp,
24463 guessLanguage("foo.h", "#pragma(warning(disable:))"));
24464 EXPECT_EQ(FormatStyle::LK_Cpp,
24465 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24466 }
24467
24468 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
24469 // ASM symbolic names are identifiers that must be surrounded by [] without
24470 // space in between:
24471 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24472
24473 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24474 verifyFormat(R"(//
24475 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
24476 )");
24477
24478 // A list of several ASM symbolic names.
24479 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
24480
24481 // ASM symbolic names in inline ASM with inputs and outputs.
24482 verifyFormat(R"(//
24483 asm("cmoveq %1, %2, %[result]"
24484 : [result] "=r"(result)
24485 : "r"(test), "r"(new), "[result]"(old));
24486 )");
24487
24488 // ASM symbolic names in inline ASM with no outputs.
24489 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
24490 }
24491
24492 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
24493 EXPECT_EQ(FormatStyle::LK_Cpp,
24494 guessLanguage("foo.h", "void f() {\n"
24495 " asm (\"mov %[e], %[d]\"\n"
24496 " : [d] \"=rm\" (d)\n"
24497 " [e] \"rm\" (*e));\n"
24498 "}"));
24499 EXPECT_EQ(FormatStyle::LK_Cpp,
24500 guessLanguage("foo.h", "void f() {\n"
24501 " _asm (\"mov %[e], %[d]\"\n"
24502 " : [d] \"=rm\" (d)\n"
24503 " [e] \"rm\" (*e));\n"
24504 "}"));
24505 EXPECT_EQ(FormatStyle::LK_Cpp,
24506 guessLanguage("foo.h", "void f() {\n"
24507 " __asm (\"mov %[e], %[d]\"\n"
24508 " : [d] \"=rm\" (d)\n"
24509 " [e] \"rm\" (*e));\n"
24510 "}"));
24511 EXPECT_EQ(FormatStyle::LK_Cpp,
24512 guessLanguage("foo.h", "void f() {\n"
24513 " __asm__ (\"mov %[e], %[d]\"\n"
24514 " : [d] \"=rm\" (d)\n"
24515 " [e] \"rm\" (*e));\n"
24516 "}"));
24517 EXPECT_EQ(FormatStyle::LK_Cpp,
24518 guessLanguage("foo.h", "void f() {\n"
24519 " asm (\"mov %[e], %[d]\"\n"
24520 " : [d] \"=rm\" (d),\n"
24521 " [e] \"rm\" (*e));\n"
24522 "}"));
24523 EXPECT_EQ(FormatStyle::LK_Cpp,
24524 guessLanguage("foo.h", "void f() {\n"
24525 " asm volatile (\"mov %[e], %[d]\"\n"
24526 " : [d] \"=rm\" (d)\n"
24527 " [e] \"rm\" (*e));\n"
24528 "}"));
24529 }
24530
24531 TEST_F(FormatTest, GuessLanguageWithChildLines) {
24532 EXPECT_EQ(FormatStyle::LK_Cpp,
24533 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24534 EXPECT_EQ(FormatStyle::LK_ObjC,
24535 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24536 EXPECT_EQ(
24537 FormatStyle::LK_Cpp,
24538 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24539 EXPECT_EQ(
24540 FormatStyle::LK_ObjC,
24541 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24542 }
24543
24544 TEST_F(FormatTest, TypenameMacros) {
24545 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24546
24547 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24548 FormatStyle Google = getGoogleStyleWithColumns(0);
24549 Google.TypenameMacros = TypenameMacros;
24550 verifyFormat("struct foo {\n"
24551 " int bar;\n"
24552 " TAILQ_ENTRY(a) bleh;\n"
24553 "};",
24554 Google);
24555
24556 FormatStyle Macros = getLLVMStyle();
24557 Macros.TypenameMacros = TypenameMacros;
24558
24559 verifyFormat("STACK_OF(int) a;", Macros);
24560 verifyFormat("STACK_OF(int) *a;", Macros);
24561 verifyFormat("STACK_OF(int const *) *a;", Macros);
24562 verifyFormat("STACK_OF(int *const) *a;", Macros);
24563 verifyFormat("STACK_OF(int, string) a;", Macros);
24564 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
24565 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
24566 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
24567 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
24568 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
24569 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
24570
24571 Macros.PointerAlignment = FormatStyle::PAS_Left;
24572 verifyFormat("STACK_OF(int)* a;", Macros);
24573 verifyFormat("STACK_OF(int*)* a;", Macros);
24574 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
24575 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
24576 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
24577 }
24578
24579 TEST_F(FormatTest, AtomicQualifier) {
24580 // Check that we treate _Atomic as a type and not a function call
24581 FormatStyle Google = getGoogleStyleWithColumns(0);
24582 verifyFormat("struct foo {\n"
24583 " int a1;\n"
24584 " _Atomic(a) a2;\n"
24585 " _Atomic(_Atomic(int) *const) a3;\n"
24586 "};",
24587 Google);
24588 verifyFormat("_Atomic(uint64_t) a;");
24589 verifyFormat("_Atomic(uint64_t) *a;");
24590 verifyFormat("_Atomic(uint64_t const *) *a;");
24591 verifyFormat("_Atomic(uint64_t *const) *a;");
24592 verifyFormat("_Atomic(const uint64_t *) *a;");
24593 verifyFormat("_Atomic(uint64_t) a;");
24594 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24595 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24596 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24597 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24598
24599 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24600 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24601 FormatStyle Style = getLLVMStyle();
24602 Style.PointerAlignment = FormatStyle::PAS_Left;
24603 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
24604 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
24605 verifyFormat("_Atomic(int)* a;", Style);
24606 verifyFormat("_Atomic(int*)* a;", Style);
24607 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
24608
24609 Style.SpacesInParens = FormatStyle::SIPO_Custom;
24610 Style.SpacesInParensOptions.InCStyleCasts = true;
24611 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
24612 Style.SpacesInParensOptions.InCStyleCasts = false;
24613 Style.SpacesInParensOptions.Other = true;
24614 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
24615 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
24616 }
24617
24618 TEST_F(FormatTest, C11Generic) {
24619 verifyFormat("_Generic(x, int: 1, default: 0)");
24620 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24621 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24622 verifyFormat("_Generic(x, int: f1, const int: f2)();");
24623 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24624
24625 verifyFormat("_Generic(x,\n"
24626 " float: f,\n"
24627 " default: d,\n"
24628 " long double: ld,\n"
24629 " float _Complex: fc,\n"
24630 " double _Complex: dc,\n"
24631 " long double _Complex: ldc)");
24632
24633 verifyFormat("while (_Generic(x, //\n"
24634 " long: x)(x) > x) {\n"
24635 "}");
24636 verifyFormat("while (_Generic(x, //\n"
24637 " long: x)(x)) {\n"
24638 "}");
24639 verifyFormat("x(_Generic(x, //\n"
24640 " long: x)(x));");
24641
24642 FormatStyle Style = getLLVMStyle();
24643 Style.ColumnLimit = 40;
24644 verifyFormat("#define LIMIT_MAX(T) \\\n"
24645 " _Generic(((T)0), \\\n"
24646 " unsigned int: UINT_MAX, \\\n"
24647 " unsigned long: ULONG_MAX, \\\n"
24648 " unsigned long long: ULLONG_MAX)",
24649 Style);
24650 verifyFormat("_Generic(x,\n"
24651 " struct A: 1,\n"
24652 " void (*)(void): 2);",
24653 Style);
24654
24655 Style.ContinuationIndentWidth = 2;
24656 verifyFormat("_Generic(x,\n"
24657 " struct A: 1,\n"
24658 " void (*)(void): 2);",
24659 Style);
24660 }
24661
24662 TEST_F(FormatTest, AmbersandInLamda) {
24663 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24664 FormatStyle AlignStyle = getLLVMStyle();
24665 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
24666 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24667 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
24668 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24669 }
24670
24671 TEST_F(FormatTest, TrailingReturnTypeAuto) {
24672 FormatStyle Style = getLLVMStyle();
24673 verifyFormat("[]() -> auto { return Val; }", Style);
24674 verifyFormat("[]() -> auto * { return Val; }", Style);
24675 verifyFormat("[]() -> auto & { return Val; }", Style);
24676 verifyFormat("auto foo() -> auto { return Val; }", Style);
24677 verifyFormat("auto foo() -> auto * { return Val; }", Style);
24678 verifyFormat("auto foo() -> auto & { return Val; }", Style);
24679 }
24680
24681 TEST_F(FormatTest, SpacesInConditionalStatement) {
24682 FormatStyle Spaces = getLLVMStyle();
24683 Spaces.IfMacros.clear();
24684 Spaces.IfMacros.push_back("MYIF");
24685 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
24686 Spaces.SpacesInParensOptions.InConditionalStatements = true;
24687 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
24688 verifyFormat("if ( !a )\n return;", Spaces);
24689 verifyFormat("if ( a )\n return;", Spaces);
24690 verifyFormat("if constexpr ( a )\n return;", Spaces);
24691 verifyFormat("MYIF ( a )\n return;", Spaces);
24692 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
24693 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
24694 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
24695 verifyFormat("while ( a )\n return;", Spaces);
24696 verifyFormat("while ( (a && b) )\n return;", Spaces);
24697 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
24698 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
24699 // Check that space on the left of "::" is inserted as expected at beginning
24700 // of condition.
24701 verifyFormat("while ( ::func() )\n return;", Spaces);
24702
24703 // Check impact of ControlStatementsExceptControlMacros is honored.
24704 Spaces.SpaceBeforeParens =
24705 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
24706 verifyFormat("MYIF( a )\n return;", Spaces);
24707 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
24708 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
24709 }
24710
24711 TEST_F(FormatTest, AlternativeOperators) {
24712 // Test case for ensuring alternate operators are not
24713 // combined with their right most neighbour.
24714 verifyFormat("int a and b;");
24715 verifyFormat("int a and_eq b;");
24716 verifyFormat("int a bitand b;");
24717 verifyFormat("int a bitor b;");
24718 verifyFormat("int a compl b;");
24719 verifyFormat("int a not b;");
24720 verifyFormat("int a not_eq b;");
24721 verifyFormat("int a or b;");
24722 verifyFormat("int a xor b;");
24723 verifyFormat("int a xor_eq b;");
24724 verifyFormat("return this not_eq bitand other;");
24725 verifyFormat("bool operator not_eq(const X bitand other)");
24726
24727 verifyFormat("int a and 5;");
24728 verifyFormat("int a and_eq 5;");
24729 verifyFormat("int a bitand 5;");
24730 verifyFormat("int a bitor 5;");
24731 verifyFormat("int a compl 5;");
24732 verifyFormat("int a not 5;");
24733 verifyFormat("int a not_eq 5;");
24734 verifyFormat("int a or 5;");
24735 verifyFormat("int a xor 5;");
24736 verifyFormat("int a xor_eq 5;");
24737
24738 verifyFormat("int a compl(5);");
24739 verifyFormat("int a not(5);");
24740
24741 verifyFormat("compl foo();"); // ~foo();
24742 verifyFormat("foo() <%%>"); // foo() {}
24743 verifyFormat("void foo() <%%>"); // void foo() {}
24744 verifyFormat("int a<:1:>;"); // int a[1];
24745 verifyFormat("%:define ABC abc"); // #define ABC abc
24746 verifyFormat("%:%:"); // ##
24747
24748 verifyFormat("a = v(not;);\n"
24749 "b = v(not+);\n"
24750 "c = v(not x);\n"
24751 "d = v(not 1);\n"
24752 "e = v(not 123.f);");
24753
24754 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
24755 " V(and) \\\n"
24756 " V(not) \\\n"
24757 " V(not!) \\\n"
24758 " V(other)",
24759 getLLVMStyleWithColumns(40));
24760 }
24761
24762 TEST_F(FormatTest, STLWhileNotDefineChed) {
24763 verifyFormat("#if defined(while)\n"
24764 "#define while EMIT WARNING C4005\n"
24765 "#endif // while");
24766 }
24767
24768 TEST_F(FormatTest, OperatorSpacing) {
24769 FormatStyle Style = getLLVMStyle();
24770 Style.PointerAlignment = FormatStyle::PAS_Right;
24771 verifyFormat("Foo::operator*();", Style);
24772 verifyFormat("Foo::operator void *();", Style);
24773 verifyFormat("Foo::operator void **();", Style);
24774 verifyFormat("Foo::operator void *&();", Style);
24775 verifyFormat("Foo::operator void *&&();", Style);
24776 verifyFormat("Foo::operator void const *();", Style);
24777 verifyFormat("Foo::operator void const **();", Style);
24778 verifyFormat("Foo::operator void const *&();", Style);
24779 verifyFormat("Foo::operator void const *&&();", Style);
24780 verifyFormat("Foo::operator()(void *);", Style);
24781 verifyFormat("Foo::operator*(void *);", Style);
24782 verifyFormat("Foo::operator*();", Style);
24783 verifyFormat("Foo::operator**();", Style);
24784 verifyFormat("Foo::operator&();", Style);
24785 verifyFormat("Foo::operator<int> *();", Style);
24786 verifyFormat("Foo::operator<Foo> *();", Style);
24787 verifyFormat("Foo::operator<int> **();", Style);
24788 verifyFormat("Foo::operator<Foo> **();", Style);
24789 verifyFormat("Foo::operator<int> &();", Style);
24790 verifyFormat("Foo::operator<Foo> &();", Style);
24791 verifyFormat("Foo::operator<int> &&();", Style);
24792 verifyFormat("Foo::operator<Foo> &&();", Style);
24793 verifyFormat("Foo::operator<int> *&();", Style);
24794 verifyFormat("Foo::operator<Foo> *&();", Style);
24795 verifyFormat("Foo::operator<int> *&&();", Style);
24796 verifyFormat("Foo::operator<Foo> *&&();", Style);
24797 verifyFormat("operator*(int (*)(), class Foo);", Style);
24798
24799 verifyFormat("Foo::operator&();", Style);
24800 verifyFormat("Foo::operator void &();", Style);
24801 verifyFormat("Foo::operator void const &();", Style);
24802 verifyFormat("Foo::operator()(void &);", Style);
24803 verifyFormat("Foo::operator&(void &);", Style);
24804 verifyFormat("Foo::operator&();", Style);
24805 verifyFormat("operator&(int (&)(), class Foo);", Style);
24806 verifyFormat("operator&&(int (&)(), class Foo);", Style);
24807
24808 verifyFormat("Foo::operator&&();", Style);
24809 verifyFormat("Foo::operator**();", Style);
24810 verifyFormat("Foo::operator void &&();", Style);
24811 verifyFormat("Foo::operator void const &&();", Style);
24812 verifyFormat("Foo::operator()(void &&);", Style);
24813 verifyFormat("Foo::operator&&(void &&);", Style);
24814 verifyFormat("Foo::operator&&();", Style);
24815 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24816 verifyFormat("operator const nsTArrayRight<E> &()", Style);
24817 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24818 Style);
24819 verifyFormat("operator void **()", Style);
24820 verifyFormat("operator const FooRight<Object> &()", Style);
24821 verifyFormat("operator const FooRight<Object> *()", Style);
24822 verifyFormat("operator const FooRight<Object> **()", Style);
24823 verifyFormat("operator const FooRight<Object> *&()", Style);
24824 verifyFormat("operator const FooRight<Object> *&&()", Style);
24825
24826 Style.PointerAlignment = FormatStyle::PAS_Left;
24827 verifyFormat("Foo::operator*();", Style);
24828 verifyFormat("Foo::operator**();", Style);
24829 verifyFormat("Foo::operator void*();", Style);
24830 verifyFormat("Foo::operator void**();", Style);
24831 verifyFormat("Foo::operator void*&();", Style);
24832 verifyFormat("Foo::operator void*&&();", Style);
24833 verifyFormat("Foo::operator void const*();", Style);
24834 verifyFormat("Foo::operator void const**();", Style);
24835 verifyFormat("Foo::operator void const*&();", Style);
24836 verifyFormat("Foo::operator void const*&&();", Style);
24837 verifyFormat("Foo::operator/*comment*/ void*();", Style);
24838 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
24839 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
24840 verifyFormat("Foo::operator()(void*);", Style);
24841 verifyFormat("Foo::operator*(void*);", Style);
24842 verifyFormat("Foo::operator*();", Style);
24843 verifyFormat("Foo::operator<int>*();", Style);
24844 verifyFormat("Foo::operator<Foo>*();", Style);
24845 verifyFormat("Foo::operator<int>**();", Style);
24846 verifyFormat("Foo::operator<Foo>**();", Style);
24847 verifyFormat("Foo::operator<Foo>*&();", Style);
24848 verifyFormat("Foo::operator<int>&();", Style);
24849 verifyFormat("Foo::operator<Foo>&();", Style);
24850 verifyFormat("Foo::operator<int>&&();", Style);
24851 verifyFormat("Foo::operator<Foo>&&();", Style);
24852 verifyFormat("Foo::operator<int>*&();", Style);
24853 verifyFormat("Foo::operator<Foo>*&();", Style);
24854 verifyFormat("operator*(int (*)(), class Foo);", Style);
24855
24856 verifyFormat("Foo::operator&();", Style);
24857 verifyFormat("Foo::operator void&();", Style);
24858 verifyFormat("Foo::operator void const&();", Style);
24859 verifyFormat("Foo::operator/*comment*/ void&();", Style);
24860 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
24861 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
24862 verifyFormat("Foo::operator()(void&);", Style);
24863 verifyFormat("Foo::operator&(void&);", Style);
24864 verifyFormat("Foo::operator&();", Style);
24865 verifyFormat("operator&(int (&)(), class Foo);", Style);
24866 verifyFormat("operator&(int (&&)(), class Foo);", Style);
24867 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24868
24869 verifyFormat("Foo::operator&&();", Style);
24870 verifyFormat("Foo::operator void&&();", Style);
24871 verifyFormat("Foo::operator void const&&();", Style);
24872 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
24873 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
24874 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
24875 verifyFormat("Foo::operator()(void&&);", Style);
24876 verifyFormat("Foo::operator&&(void&&);", Style);
24877 verifyFormat("Foo::operator&&();", Style);
24878 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24879 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
24880 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
24881 Style);
24882 verifyFormat("operator void**()", Style);
24883 verifyFormat("operator const FooLeft<Object>&()", Style);
24884 verifyFormat("operator const FooLeft<Object>*()", Style);
24885 verifyFormat("operator const FooLeft<Object>**()", Style);
24886 verifyFormat("operator const FooLeft<Object>*&()", Style);
24887 verifyFormat("operator const FooLeft<Object>*&&()", Style);
24888
24889 // PR45107
24890 verifyFormat("operator Vector<String>&();", Style);
24891 verifyFormat("operator const Vector<String>&();", Style);
24892 verifyFormat("operator foo::Bar*();", Style);
24893 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
24894 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
24895 Style);
24896
24897 Style.PointerAlignment = FormatStyle::PAS_Middle;
24898 verifyFormat("Foo::operator*();", Style);
24899 verifyFormat("Foo::operator void *();", Style);
24900 verifyFormat("Foo::operator()(void *);", Style);
24901 verifyFormat("Foo::operator*(void *);", Style);
24902 verifyFormat("Foo::operator*();", Style);
24903 verifyFormat("operator*(int (*)(), class Foo);", Style);
24904
24905 verifyFormat("Foo::operator&();", Style);
24906 verifyFormat("Foo::operator void &();", Style);
24907 verifyFormat("Foo::operator void const &();", Style);
24908 verifyFormat("Foo::operator()(void &);", Style);
24909 verifyFormat("Foo::operator&(void &);", Style);
24910 verifyFormat("Foo::operator&();", Style);
24911 verifyFormat("operator&(int (&)(), class Foo);", Style);
24912
24913 verifyFormat("Foo::operator&&();", Style);
24914 verifyFormat("Foo::operator void &&();", Style);
24915 verifyFormat("Foo::operator void const &&();", Style);
24916 verifyFormat("Foo::operator()(void &&);", Style);
24917 verifyFormat("Foo::operator&&(void &&);", Style);
24918 verifyFormat("Foo::operator&&();", Style);
24919 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24920 }
24921
24922 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
24923 FormatStyle Style = getLLVMStyle();
24924 // PR46157
24925 verifyFormat("foo(operator+, -42);", Style);
24926 verifyFormat("foo(operator++, -42);", Style);
24927 verifyFormat("foo(operator--, -42);", Style);
24928 verifyFormat("foo(-42, operator--);", Style);
24929 verifyFormat("foo(-42, operator, );", Style);
24930 verifyFormat("foo(operator, , -42);", Style);
24931 }
24932
24933 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
24934 FormatStyle Style = getLLVMStyle();
24935 Style.WhitespaceSensitiveMacros.push_back("FOO");
24936
24937 // Newlines are important here.
24938 verifyNoChange("FOO(1+2 )\n", Style);
24939 verifyNoChange("FOO(a:b:c)\n", Style);
24940
24941 // Don't use the helpers here, since 'mess up' will change the whitespace
24942 // and these are all whitespace sensitive by definition
24943 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
24944 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
24945 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
24946 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
24947 " Still=Intentional);",
24948 Style);
24949 Style.AlignConsecutiveAssignments.Enabled = true;
24950 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
24951 " Still=Intentional);",
24952 Style);
24953
24954 Style.ColumnLimit = 21;
24955 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
24956 }
24957
24958 TEST_F(FormatTest, SkipMacroDefinitionBody) {
24959 auto Style = getLLVMStyle();
24960 Style.SkipMacroDefinitionBody = true;
24961
24962 verifyFormat("#define A", "#define A", Style);
24963 verifyFormat("#define A a aa", "#define A a aa", Style);
24964 verifyNoChange("#define A b", Style);
24965 verifyNoChange("#define A ( args )", Style);
24966 verifyNoChange("#define A ( args ) = func ( args )", Style);
24967 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
24968 verifyNoChange("#define A ( args ) \\\n"
24969 " {\\\n"
24970 " int a = 1 ;\\\n"
24971 "}",
24972 Style);
24973
24974 verifyNoChange("#define A x:", Style);
24975 verifyNoChange("#define A a. b", Style);
24976
24977 // Surrounded with formatted code.
24978 verifyFormat("int a;\n"
24979 "#define A a\n"
24980 "int a;",
24981 "int a ;\n"
24982 "#define A a\n"
24983 "int a ;",
24984 Style);
24985
24986 // Columns are not broken when a limit is set.
24987 Style.ColumnLimit = 10;
24988 verifyFormat("#define A a a a a", " # define A a a a a ", Style);
24989 verifyNoChange("#define A a a a a", Style);
24990
24991 Style.ColumnLimit = 15;
24992 verifyFormat("#define A // a\n"
24993 " // very\n"
24994 " // long\n"
24995 " // comment",
24996 "#define A //a very long comment", Style);
24997 Style.ColumnLimit = 0;
24998
24999 // Multiline definition.
25000 verifyNoChange("#define A \\\n"
25001 "Line one with spaces . \\\n"
25002 " Line two.",
25003 Style);
25004 verifyNoChange("#define A \\\n"
25005 "a a \\\n"
25006 "a \\\n"
25007 "a",
25008 Style);
25009 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
25010 verifyNoChange("#define A \\\n"
25011 "a a \\\n"
25012 "a \\\n"
25013 "a",
25014 Style);
25015 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
25016 verifyNoChange("#define A \\\n"
25017 "a a \\\n"
25018 "a \\\n"
25019 "a",
25020 Style);
25021
25022 // Adjust indendations but don't change the definition.
25023 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25024 verifyNoChange("#if A\n"
25025 "#define A a\n"
25026 "#endif",
25027 Style);
25028 verifyFormat("#if A\n"
25029 "#define A a\n"
25030 "#endif",
25031 "#if A\n"
25032 " #define A a\n"
25033 "#endif",
25034 Style);
25035 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
25036 verifyNoChange("#if A\n"
25037 "# define A a\n"
25038 "#endif",
25039 Style);
25040 verifyFormat("#if A\n"
25041 "# define A a\n"
25042 "#endif",
25043 "#if A\n"
25044 " #define A a\n"
25045 "#endif",
25046 Style);
25047 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
25048 verifyNoChange("#if A\n"
25049 " #define A a\n"
25050 "#endif",
25051 Style);
25052 verifyFormat("#if A\n"
25053 " #define A a\n"
25054 "#endif",
25055 "#if A\n"
25056 " # define A a\n"
25057 "#endif",
25058 Style);
25059
25060 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25061 // SkipMacroDefinitionBody should not affect other PP directives
25062 verifyFormat("#if !defined(A)\n"
25063 "#define A a\n"
25064 "#endif",
25065 "#if ! defined ( A )\n"
25066 " #define A a\n"
25067 "#endif",
25068 Style);
25069
25070 // With comments.
25071 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25072 Style);
25073 verifyNoChange("/* */ #define A a // a a", Style);
25074
25075 verifyFormat("int a; // a\n"
25076 "#define A // a\n"
25077 "int aaa; // a",
25078 "int a; // a\n"
25079 "#define A // a\n"
25080 "int aaa; // a",
25081 Style);
25082
25083 verifyNoChange(
25084 "#define MACRO_WITH_COMMENTS() \\\n"
25085 " public: \\\n"
25086 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
25087 " static MyType getClassTypeId(); \\\n"
25088 " /** Normal comment for the following method. */ \\\n"
25089 " virtual MyType getTypeId() const;",
25090 Style);
25091
25092 // multiline macro definitions
25093 verifyNoChange("#define A a\\\n"
25094 " A a \\\n "
25095 " A a",
25096 Style);
25097 }
25098
25099 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
25100 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
25101 // test its interaction with line wrapping
25102 FormatStyle Style = getLLVMStyleWithColumns(80);
25103 verifyFormat("namespace {\n"
25104 "int i;\n"
25105 "int j;\n"
25106 "} // namespace",
25107 Style);
25108
25109 verifyFormat("namespace AAA {\n"
25110 "int i;\n"
25111 "int j;\n"
25112 "} // namespace AAA",
25113 Style);
25114
25115 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
25116 "int i;\n"
25117 "int j;\n"
25118 "} // namespace Averyveryveryverylongnamespace",
25119 "namespace Averyveryveryverylongnamespace {\n"
25120 "int i;\n"
25121 "int j;\n"
25122 "}",
25123 Style);
25124
25125 verifyFormat(
25126 "namespace "
25127 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25128 " went::mad::now {\n"
25129 "int i;\n"
25130 "int j;\n"
25131 "} // namespace\n"
25132 " // "
25133 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25134 "went::mad::now",
25135 "namespace "
25136 "would::it::save::you::a::lot::of::time::if_::i::"
25137 "just::gave::up::and_::went::mad::now {\n"
25138 "int i;\n"
25139 "int j;\n"
25140 "}",
25141 Style);
25142
25143 // This used to duplicate the comment again and again on subsequent runs
25144 verifyFormat(
25145 "namespace "
25146 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25147 " went::mad::now {\n"
25148 "int i;\n"
25149 "int j;\n"
25150 "} // namespace\n"
25151 " // "
25152 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25153 "went::mad::now",
25154 "namespace "
25155 "would::it::save::you::a::lot::of::time::if_::i::"
25156 "just::gave::up::and_::went::mad::now {\n"
25157 "int i;\n"
25158 "int j;\n"
25159 "} // namespace\n"
25160 " // "
25161 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
25162 "and_::went::mad::now",
25163 Style);
25164 }
25165
25166 TEST_F(FormatTest, LikelyUnlikely) {
25167 FormatStyle Style = getLLVMStyle();
25168
25169 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25170 " return 29;\n"
25171 "}",
25172 Style);
25173
25174 verifyFormat("if (argc > 5) [[likely]] {\n"
25175 " return 29;\n"
25176 "}",
25177 Style);
25178
25179 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25180 " return 29;\n"
25181 "} else [[likely]] {\n"
25182 " return 42;\n"
25183 "}",
25184 Style);
25185
25186 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25187 " return 29;\n"
25188 "} else if (argc > 10) [[likely]] {\n"
25189 " return 99;\n"
25190 "} else {\n"
25191 " return 42;\n"
25192 "}",
25193 Style);
25194
25195 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
25196 " return 29;\n"
25197 "}",
25198 Style);
25199
25200 verifyFormat("if (argc > 5) [[unlikely]]\n"
25201 " return 29;",
25202 Style);
25203 verifyFormat("if (argc > 5) [[likely]]\n"
25204 " return 29;",
25205 Style);
25206
25207 verifyFormat("while (limit > 0) [[unlikely]] {\n"
25208 " --limit;\n"
25209 "}",
25210 Style);
25211 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
25212 " --limit;\n"
25213 "}",
25214 Style);
25215
25216 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
25217 " --limit;",
25218 Style);
25219 verifyFormat("while (limit > 0) [[likely]]\n"
25220 " --limit;",
25221 Style);
25222
25223 Style.AttributeMacros.push_back("UNLIKELY");
25224 Style.AttributeMacros.push_back("LIKELY");
25225 verifyFormat("if (argc > 5) UNLIKELY\n"
25226 " return 29;",
25227 Style);
25228
25229 verifyFormat("if (argc > 5) UNLIKELY {\n"
25230 " return 29;\n"
25231 "}",
25232 Style);
25233 verifyFormat("if (argc > 5) UNLIKELY {\n"
25234 " return 29;\n"
25235 "} else [[likely]] {\n"
25236 " return 42;\n"
25237 "}",
25238 Style);
25239 verifyFormat("if (argc > 5) UNLIKELY {\n"
25240 " return 29;\n"
25241 "} else LIKELY {\n"
25242 " return 42;\n"
25243 "}",
25244 Style);
25245 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25246 " return 29;\n"
25247 "} else LIKELY {\n"
25248 " return 42;\n"
25249 "}",
25250 Style);
25251
25252 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25253 " --limit;\n"
25254 "}",
25255 Style);
25256 verifyFormat("while (limit > 0) LIKELY {\n"
25257 " --limit;\n"
25258 "}",
25259 Style);
25260
25261 verifyFormat("while (limit > 0) UNLIKELY\n"
25262 " --limit;",
25263 Style);
25264 verifyFormat("for (auto &limit : limits) LIKELY\n"
25265 " --limit;",
25266 Style);
25267 }
25268
25269 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
25270 verifyFormat("Constructor()\n"
25271 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25272 " aaaa(aaaaaaaaaaaaaaaaaa, "
25273 "aaaaaaaaaaaaaaaaaat))");
25274 verifyFormat("Constructor()\n"
25275 " : aaaaaaaaaaaaa(aaaaaa), "
25276 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25277
25278 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
25279 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
25280 verifyFormat("Constructor()\n"
25281 " : aaaaaa(aaaaaa),\n"
25282 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25283 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25284 StyleWithWhitespacePenalty);
25285 verifyFormat("Constructor()\n"
25286 " : aaaaaaaaaaaaa(aaaaaa), "
25287 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25288 StyleWithWhitespacePenalty);
25289 }
25290
25291 TEST_F(FormatTest, LLVMDefaultStyle) {
25292 FormatStyle Style = getLLVMStyle();
25293 verifyFormat("extern \"C\" {\n"
25294 "int foo();\n"
25295 "}",
25296 Style);
25297 }
25298 TEST_F(FormatTest, GNUDefaultStyle) {
25299 FormatStyle Style = getGNUStyle();
25300 verifyFormat("extern \"C\"\n"
25301 "{\n"
25302 " int foo ();\n"
25303 "}",
25304 Style);
25305 }
25306 TEST_F(FormatTest, MozillaDefaultStyle) {
25307 FormatStyle Style = getMozillaStyle();
25308 verifyFormat("extern \"C\"\n"
25309 "{\n"
25310 " int foo();\n"
25311 "}",
25312 Style);
25313 }
25314 TEST_F(FormatTest, GoogleDefaultStyle) {
25315 FormatStyle Style = getGoogleStyle();
25316 verifyFormat("extern \"C\" {\n"
25317 "int foo();\n"
25318 "}",
25319 Style);
25320 }
25321 TEST_F(FormatTest, ChromiumDefaultStyle) {
25322 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
25323 verifyFormat("extern \"C\" {\n"
25324 "int foo();\n"
25325 "}",
25326 Style);
25327 }
25328 TEST_F(FormatTest, MicrosoftDefaultStyle) {
25329 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
25330 verifyFormat("extern \"C\"\n"
25331 "{\n"
25332 " int foo();\n"
25333 "}",
25334 Style);
25335 }
25336 TEST_F(FormatTest, WebKitDefaultStyle) {
25337 FormatStyle Style = getWebKitStyle();
25338 verifyFormat("extern \"C\" {\n"
25339 "int foo();\n"
25340 "}",
25341 Style);
25342 }
25343
25344 TEST_F(FormatTest, Concepts) {
25345 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
25346 FormatStyle::BBCDS_Always);
25347
25348 // The default in LLVM style is REI_OuterScope, but these tests were written
25349 // when the default was REI_Keyword.
25350 FormatStyle Style = getLLVMStyle();
25351 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25352
25353 verifyFormat("template <typename T>\n"
25354 "concept True = true;");
25355
25356 verifyFormat("template <typename T>\n"
25357 "concept C = ((false || foo()) && C2<T>) ||\n"
25358 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25359 getLLVMStyleWithColumns(60));
25360
25361 verifyFormat("template <typename T>\n"
25362 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25363 "sizeof(T) <= 8;");
25364
25365 verifyFormat("template <typename T>\n"
25366 "concept DelayedCheck = true && requires(T t) {\n"
25367 " t.bar();\n"
25368 " t.baz();\n"
25369 " } && sizeof(T) <= 8;",
25370 Style);
25371
25372 verifyFormat("template <typename T>\n"
25373 "concept DelayedCheck = true && requires(T t) { // Comment\n"
25374 " t.bar();\n"
25375 " t.baz();\n"
25376 " } && sizeof(T) <= 8;",
25377 Style);
25378
25379 verifyFormat("template <typename T>\n"
25380 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25381 "sizeof(T) <= 8;");
25382
25383 verifyFormat("template <typename T>\n"
25384 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25385
25386 verifyFormat("template <typename T>\n"
25387 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25388
25389 verifyFormat("template <typename T>\n"
25390 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25391
25392 verifyFormat("template <typename T>\n"
25393 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25394 "&& sizeof(T) <= 8;");
25395
25396 verifyFormat("template <typename T>\n"
25397 "concept DelayedCheck =\n"
25398 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25399 "sizeof(T) <= 8;");
25400
25401 verifyFormat("template <typename T>\n"
25402 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25403 "&& sizeof(T) <= 8;");
25404
25405 verifyFormat(
25406 "template <typename T>\n"
25407 "concept DelayedCheck =\n"
25408 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25409
25410 verifyFormat("template <typename T>\n"
25411 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25412 "&& sizeof(T) <= 8;");
25413
25414 verifyFormat("template <typename T>\n"
25415 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25416 "sizeof(T) <= 8;");
25417
25418 verifyFormat("template <typename T>\n"
25419 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25420 " requires(T t) {\n"
25421 " t.bar();\n"
25422 " t.baz();\n"
25423 " } && sizeof(T) <= 8 && !(4 < 3);",
25424 getLLVMStyleWithColumns(60));
25425
25426 verifyFormat("template <typename T>\n"
25427 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25428
25429 verifyFormat("template <typename T>\n"
25430 "concept C = foo();");
25431
25432 verifyFormat("template <typename T>\n"
25433 "concept C = foo(T());");
25434
25435 verifyFormat("template <typename T>\n"
25436 "concept C = foo(T{});");
25437
25438 verifyFormat("template <typename T>\n"
25439 "concept Size = V<sizeof(T)>::Value > 5;");
25440
25441 verifyFormat("template <typename T>\n"
25442 "concept True = S<T>::Value;");
25443
25444 verifyFormat("template <S T>\n"
25445 "concept True = T.field;");
25446
25447 verifyFormat(
25448 "template <typename T>\n"
25449 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25450 " sizeof(T) <= 8;");
25451
25452 // FIXME: This is misformatted because the fake l paren starts at bool, not at
25453 // the lambda l square.
25454 verifyFormat("template <typename T>\n"
25455 "concept C = [] -> bool { return true; }() && requires(T t) { "
25456 "t.bar(); } &&\n"
25457 " sizeof(T) <= 8;");
25458
25459 verifyFormat(
25460 "template <typename T>\n"
25461 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25462 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25463
25464 verifyFormat("template <typename T>\n"
25465 "concept C = decltype([]() { return std::true_type{}; "
25466 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25467 getLLVMStyleWithColumns(120));
25468
25469 verifyFormat("template <typename T>\n"
25470 "concept C = decltype([]() -> std::true_type { return {}; "
25471 "}())::value &&\n"
25472 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25473
25474 verifyFormat("template <typename T>\n"
25475 "concept C = true;\n"
25476 "Foo Bar;");
25477
25478 verifyFormat("template <typename T>\n"
25479 "concept Hashable = requires(T a) {\n"
25480 " { std::hash<T>{}(a) } -> "
25481 "std::convertible_to<std::size_t>;\n"
25482 " };",
25483 Style);
25484
25485 verifyFormat(
25486 "template <typename T>\n"
25487 "concept EqualityComparable = requires(T a, T b) {\n"
25488 " { a == b } -> std::same_as<bool>;\n"
25489 " };",
25490 Style);
25491
25492 verifyFormat(
25493 "template <typename T>\n"
25494 "concept EqualityComparable = requires(T a, T b) {\n"
25495 " { a == b } -> std::same_as<bool>;\n"
25496 " { a != b } -> std::same_as<bool>;\n"
25497 " };",
25498 Style);
25499
25500 verifyFormat("template <typename T>\n"
25501 "concept WeakEqualityComparable = requires(T a, T b) {\n"
25502 " { a == b };\n"
25503 " { a != b };\n"
25504 " };",
25505 Style);
25506
25507 verifyFormat("template <typename T>\n"
25508 "concept HasSizeT = requires { typename T::size_t; };");
25509
25510 verifyFormat("template <typename T>\n"
25511 "concept Semiregular =\n"
25512 " DefaultConstructible<T> && CopyConstructible<T> && "
25513 "CopyAssignable<T> &&\n"
25514 " requires(T a, std::size_t n) {\n"
25515 " requires Same<T *, decltype(&a)>;\n"
25516 " { a.~T() } noexcept;\n"
25517 " requires Same<T *, decltype(new T)>;\n"
25518 " requires Same<T *, decltype(new T[n])>;\n"
25519 " { delete new T; };\n"
25520 " { delete new T[n]; };\n"
25521 " };",
25522 Style);
25523
25524 verifyFormat("template <typename T>\n"
25525 "concept Semiregular =\n"
25526 " requires(T a, std::size_t n) {\n"
25527 " requires Same<T *, decltype(&a)>;\n"
25528 " { a.~T() } noexcept;\n"
25529 " requires Same<T *, decltype(new T)>;\n"
25530 " requires Same<T *, decltype(new T[n])>;\n"
25531 " { delete new T; };\n"
25532 " { delete new T[n]; };\n"
25533 " { new T } -> std::same_as<T *>;\n"
25534 " } && DefaultConstructible<T> && CopyConstructible<T> && "
25535 "CopyAssignable<T>;",
25536 Style);
25537
25538 verifyFormat(
25539 "template <typename T>\n"
25540 "concept Semiregular =\n"
25541 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
25542 " requires Same<T *, decltype(&a)>;\n"
25543 " { a.~T() } noexcept;\n"
25544 " requires Same<T *, decltype(new T)>;\n"
25545 " requires Same<T *, decltype(new "
25546 "T[n])>;\n"
25547 " { delete new T; };\n"
25548 " { delete new T[n]; };\n"
25549 " } && CopyConstructible<T> && "
25550 "CopyAssignable<T>;",
25551 Style);
25552
25553 verifyFormat("template <typename T>\n"
25554 "concept Two = requires(T t) {\n"
25555 " { t.foo() } -> std::same_as<Bar>;\n"
25556 " } && requires(T &&t) {\n"
25557 " { t.foo() } -> std::same_as<Bar &&>;\n"
25558 " };",
25559 Style);
25560
25561 verifyFormat(
25562 "template <typename T>\n"
25563 "concept C = requires(T x) {\n"
25564 " { *x } -> std::convertible_to<typename T::inner>;\n"
25565 " { x + 1 } noexcept -> std::same_as<int>;\n"
25566 " { x * 1 } -> std::convertible_to<T>;\n"
25567 " };",
25568 Style);
25569
25570 verifyFormat("template <typename T>\n"
25571 "concept C = requires(T x) {\n"
25572 " {\n"
25573 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25574 " } -> long_long_concept_name<T>;\n"
25575 " {\n"
25576 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25577 " } noexcept -> long_long_concept_name<T>;\n"
25578 " };",
25579 Style);
25580
25581 verifyFormat(
25582 "template <typename T, typename U = T>\n"
25583 "concept Swappable = requires(T &&t, U &&u) {\n"
25584 " swap(std::forward<T>(t), std::forward<U>(u));\n"
25585 " swap(std::forward<U>(u), std::forward<T>(t));\n"
25586 " };",
25587 Style);
25588
25589 verifyFormat("template <typename T, typename U>\n"
25590 "concept Common = requires(T &&t, U &&u) {\n"
25591 " typename CommonType<T, U>;\n"
25592 " { CommonType<T, U>(std::forward<T>(t)) };\n"
25593 " };",
25594 Style);
25595
25596 verifyFormat("template <typename T, typename U>\n"
25597 "concept Common = requires(T &&t, U &&u) {\n"
25598 " typename CommonType<T, U>;\n"
25599 " { CommonType<T, U>{std::forward<T>(t)} };\n"
25600 " };",
25601 Style);
25602
25603 verifyFormat(
25604 "template <typename T>\n"
25605 "concept C = requires(T t) {\n"
25606 " requires Bar<T> && Foo<T>;\n"
25607 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25608 " };",
25609 Style);
25610
25611 verifyFormat("template <typename T>\n"
25612 "concept HasFoo = requires(T t) {\n"
25613 " { t.foo() };\n"
25614 " t.foo();\n"
25615 " };\n"
25616 "template <typename T>\n"
25617 "concept HasBar = requires(T t) {\n"
25618 " { t.bar() };\n"
25619 " t.bar();\n"
25620 " };",
25621 Style);
25622
25623 verifyFormat("template <typename T>\n"
25624 "concept Large = sizeof(T) > 10;");
25625
25626 verifyFormat("template <typename T, typename U>\n"
25627 "concept FooableWith = requires(T t, U u) {\n"
25628 " typename T::foo_type;\n"
25629 " { t.foo(u) } -> typename T::foo_type;\n"
25630 " t++;\n"
25631 " };\n"
25632 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25633 Style);
25634
25635 verifyFormat("template <typename T>\n"
25636 "concept Context = is_specialization_of_v<context, T>;");
25637
25638 verifyFormat("template <typename T>\n"
25639 "concept Node = std::is_object_v<T>;");
25640
25641 verifyFormat("template <class T>\n"
25642 "concept integral = __is_integral(T);");
25643
25644 verifyFormat("template <class T>\n"
25645 "concept is2D = __array_extent(T, 1) == 2;");
25646
25647 verifyFormat("template <class T>\n"
25648 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25649
25650 verifyFormat("template <class T, class T2>\n"
25651 "concept Same = __is_same_as<T, T2>;");
25652
25653 verifyFormat(
25654 "template <class _InIt, class _OutIt>\n"
25655 "concept _Can_reread_dest =\n"
25656 " std::forward_iterator<_OutIt> &&\n"
25657 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25658
25659 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
25660
25661 verifyFormat(
25662 "template <typename T>\n"
25663 "concept C = requires(T t) {\n"
25664 " requires Bar<T> && Foo<T>;\n"
25665 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25666 " };",
25667 Style);
25668
25669 verifyFormat("template <typename T>\n"
25670 "concept HasFoo = requires(T t) {\n"
25671 " { t.foo() };\n"
25672 " t.foo();\n"
25673 " };\n"
25674 "template <typename T>\n"
25675 "concept HasBar = requires(T t) {\n"
25676 " { t.bar() };\n"
25677 " t.bar();\n"
25678 " };",
25679 Style);
25680
25681 verifyFormat("template <typename T> concept True = true;", Style);
25682
25683 verifyFormat("template <typename T>\n"
25684 "concept C = decltype([]() -> std::true_type { return {}; "
25685 "}())::value &&\n"
25686 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25687 Style);
25688
25689 verifyFormat("template <typename T>\n"
25690 "concept Semiregular =\n"
25691 " DefaultConstructible<T> && CopyConstructible<T> && "
25692 "CopyAssignable<T> &&\n"
25693 " requires(T a, std::size_t n) {\n"
25694 " requires Same<T *, decltype(&a)>;\n"
25695 " { a.~T() } noexcept;\n"
25696 " requires Same<T *, decltype(new T)>;\n"
25697 " requires Same<T *, decltype(new T[n])>;\n"
25698 " { delete new T; };\n"
25699 " { delete new T[n]; };\n"
25700 " };",
25701 Style);
25702
25703 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
25704
25705 verifyFormat("template <typename T> concept C =\n"
25706 " requires(T t) {\n"
25707 " requires Bar<T> && Foo<T>;\n"
25708 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25709 " };",
25710 Style);
25711
25712 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25713 " { t.foo() };\n"
25714 " t.foo();\n"
25715 " };\n"
25716 "template <typename T> concept HasBar = requires(T t) {\n"
25717 " { t.bar() };\n"
25718 " t.bar();\n"
25719 " };",
25720 Style);
25721
25722 verifyFormat("template <typename T> concept True = true;", Style);
25723
25724 verifyFormat(
25725 "template <typename T> concept C =\n"
25726 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
25727 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25728 Style);
25729
25730 verifyFormat("template <typename T> concept Semiregular =\n"
25731 " DefaultConstructible<T> && CopyConstructible<T> && "
25732 "CopyAssignable<T> &&\n"
25733 " requires(T a, std::size_t n) {\n"
25734 " requires Same<T *, decltype(&a)>;\n"
25735 " { a.~T() } noexcept;\n"
25736 " requires Same<T *, decltype(new T)>;\n"
25737 " requires Same<T *, decltype(new T[n])>;\n"
25738 " { delete new T; };\n"
25739 " { delete new T[n]; };\n"
25740 " };",
25741 Style);
25742
25743 // The following tests are invalid C++, we just want to make sure we don't
25744 // assert.
25745 verifyNoCrash("template <typename T>\n"
25746 "concept C = requires C2<T>;");
25747
25748 verifyNoCrash("template <typename T>\n"
25749 "concept C = 5 + 4;");
25750
25751 verifyNoCrash("template <typename T>\n"
25752 "concept C = class X;");
25753
25754 verifyNoCrash("template <typename T>\n"
25755 "concept C = [] && true;");
25756
25757 verifyNoCrash("template <typename T>\n"
25758 "concept C = [] && requires(T t) { typename T::size_type; };");
25759 }
25760
25761 TEST_F(FormatTest, RequiresClausesPositions) {
25762 auto Style = getLLVMStyle();
25763 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
25764 EXPECT_EQ(Style.IndentRequiresClause, true);
25765
25766 // The default in LLVM style is REI_OuterScope, but these tests were written
25767 // when the default was REI_Keyword.
25768 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25769
25770 verifyFormat("template <typename T>\n"
25771 " requires(Foo<T> && std::trait<T>)\n"
25772 "struct Bar;",
25773 Style);
25774
25775 verifyFormat("template <typename T>\n"
25776 " requires(Foo<T> && std::trait<T>)\n"
25777 "class Bar {\n"
25778 "public:\n"
25779 " Bar(T t);\n"
25780 " bool baz();\n"
25781 "};",
25782 Style);
25783
25784 verifyFormat(
25785 "template <typename T>\n"
25786 " requires requires(T &&t) {\n"
25787 " typename T::I;\n"
25788 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
25789 " }\n"
25790 "Bar(T) -> Bar<typename T::I>;",
25791 Style);
25792
25793 verifyFormat("template <typename T>\n"
25794 " requires(Foo<T> && std::trait<T>)\n"
25795 "constexpr T MyGlobal;",
25796 Style);
25797
25798 verifyFormat("template <typename T>\n"
25799 " requires Foo<T> && requires(T t) {\n"
25800 " { t.baz() } -> std::same_as<bool>;\n"
25801 " requires std::same_as<T::Factor, int>;\n"
25802 " }\n"
25803 "inline int bar(T t) {\n"
25804 " return t.baz() ? T::Factor : 5;\n"
25805 "}",
25806 Style);
25807
25808 verifyFormat("template <typename T>\n"
25809 "inline int bar(T t)\n"
25810 " requires Foo<T> && requires(T t) {\n"
25811 " { t.baz() } -> std::same_as<bool>;\n"
25812 " requires std::same_as<T::Factor, int>;\n"
25813 " }\n"
25814 "{\n"
25815 " return t.baz() ? T::Factor : 5;\n"
25816 "}",
25817 Style);
25818
25819 verifyFormat("template <typename T>\n"
25820 " requires F<T>\n"
25821 "int bar(T t) {\n"
25822 " return 5;\n"
25823 "}",
25824 Style);
25825
25826 verifyFormat("template <typename T>\n"
25827 "int bar(T t)\n"
25828 " requires F<T>\n"
25829 "{\n"
25830 " return 5;\n"
25831 "}",
25832 Style);
25833
25834 verifyFormat("template <typename T>\n"
25835 "int S::bar(T t) &&\n"
25836 " requires F<T>\n"
25837 "{\n"
25838 " return 5;\n"
25839 "}",
25840 Style);
25841
25842 verifyFormat("template <typename T>\n"
25843 "int bar(T t)\n"
25844 " requires F<T>;",
25845 Style);
25846
25847 Style.IndentRequiresClause = false;
25848 verifyFormat("template <typename T>\n"
25849 "requires F<T>\n"
25850 "int bar(T t) {\n"
25851 " return 5;\n"
25852 "}",
25853 Style);
25854
25855 verifyFormat("template <typename T>\n"
25856 "int S::bar(T t) &&\n"
25857 "requires F<T>\n"
25858 "{\n"
25859 " return 5;\n"
25860 "}",
25861 Style);
25862
25863 verifyFormat("template <typename T>\n"
25864 "int bar(T t)\n"
25865 "requires F<T>\n"
25866 "{\n"
25867 " return 5;\n"
25868 "}",
25869 Style);
25870
25871 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
25872 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
25873 "template <typename T> requires Foo<T> void bar() {}\n"
25874 "template <typename T> void bar() requires Foo<T> {}\n"
25875 "template <typename T> void bar() requires Foo<T>;\n"
25876 "template <typename T> void S::bar() && requires Foo<T> {}\n"
25877 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
25878 Style);
25879
25880 auto ColumnStyle = Style;
25881 ColumnStyle.ColumnLimit = 40;
25882 verifyFormat("template <typename AAAAAAA>\n"
25883 "requires Foo<T> struct Bar {};\n"
25884 "template <typename AAAAAAA>\n"
25885 "requires Foo<T> void bar() {}\n"
25886 "template <typename AAAAAAA>\n"
25887 "void bar() requires Foo<T> {}\n"
25888 "template <typename T>\n"
25889 "void S::bar() && requires Foo<T> {}\n"
25890 "template <typename AAAAAAA>\n"
25891 "requires Foo<T> Baz(T) -> Baz<T>;",
25892 ColumnStyle);
25893
25894 verifyFormat("template <typename T>\n"
25895 "requires Foo<AAAAAAA> struct Bar {};\n"
25896 "template <typename T>\n"
25897 "requires Foo<AAAAAAA> void bar() {}\n"
25898 "template <typename T>\n"
25899 "void bar() requires Foo<AAAAAAA> {}\n"
25900 "template <typename T>\n"
25901 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
25902 ColumnStyle);
25903
25904 verifyFormat("template <typename AAAAAAA>\n"
25905 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25906 "struct Bar {};\n"
25907 "template <typename AAAAAAA>\n"
25908 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25909 "void bar() {}\n"
25910 "template <typename AAAAAAA>\n"
25911 "void bar()\n"
25912 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25913 "template <typename AAAAAAA>\n"
25914 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
25915 "template <typename AAAAAAA>\n"
25916 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25917 "Bar(T) -> Bar<T>;",
25918 ColumnStyle);
25919
25920 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
25921 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
25922
25923 verifyFormat("template <typename T>\n"
25924 "requires Foo<T> struct Bar {};\n"
25925 "template <typename T>\n"
25926 "requires Foo<T> void bar() {}\n"
25927 "template <typename T>\n"
25928 "void bar()\n"
25929 "requires Foo<T> {}\n"
25930 "template <typename T>\n"
25931 "void bar()\n"
25932 "requires Foo<T>;\n"
25933 "template <typename T>\n"
25934 "void S::bar() &&\n"
25935 "requires Foo<T> {}\n"
25936 "template <typename T>\n"
25937 "requires Foo<T> Bar(T) -> Bar<T>;",
25938 Style);
25939
25940 verifyFormat("template <typename AAAAAAA>\n"
25941 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25942 "struct Bar {};\n"
25943 "template <typename AAAAAAA>\n"
25944 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25945 "void bar() {}\n"
25946 "template <typename AAAAAAA>\n"
25947 "void bar()\n"
25948 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25949 "template <typename AAAAAAA>\n"
25950 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
25951 "template <typename AAAAAAA>\n"
25952 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25953 "Bar(T) -> Bar<T>;",
25954 ColumnStyle);
25955
25956 Style.IndentRequiresClause = true;
25957 ColumnStyle.IndentRequiresClause = true;
25958
25959 verifyFormat("template <typename T>\n"
25960 " requires Foo<T> struct Bar {};\n"
25961 "template <typename T>\n"
25962 " requires Foo<T> void bar() {}\n"
25963 "template <typename T>\n"
25964 "void bar()\n"
25965 " requires Foo<T> {}\n"
25966 "template <typename T>\n"
25967 "void S::bar() &&\n"
25968 " requires Foo<T> {}\n"
25969 "template <typename T>\n"
25970 " requires Foo<T> Bar(T) -> Bar<T>;",
25971 Style);
25972
25973 verifyFormat("template <typename AAAAAAA>\n"
25974 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25975 "struct Bar {};\n"
25976 "template <typename AAAAAAA>\n"
25977 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25978 "void bar() {}\n"
25979 "template <typename AAAAAAA>\n"
25980 "void bar()\n"
25981 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25982 "template <typename AAAAAAA>\n"
25983 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
25984 "template <typename AAAAAAA>\n"
25985 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25986 "Bar(T) -> Bar<T>;",
25987 ColumnStyle);
25988
25989 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
25990 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
25991
25992 verifyFormat("template <typename T> requires Foo<T>\n"
25993 "struct Bar {};\n"
25994 "template <typename T> requires Foo<T>\n"
25995 "void bar() {}\n"
25996 "template <typename T>\n"
25997 "void bar() requires Foo<T>\n"
25998 "{}\n"
25999 "template <typename T> void bar() requires Foo<T>;\n"
26000 "template <typename T>\n"
26001 "void S::bar() && requires Foo<T>\n"
26002 "{}\n"
26003 "template <typename T> requires Foo<T>\n"
26004 "Bar(T) -> Bar<T>;",
26005 Style);
26006
26007 verifyFormat("template <typename AAAAAAA>\n"
26008 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26009 "struct Bar {};\n"
26010 "template <typename AAAAAAA>\n"
26011 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26012 "void bar() {}\n"
26013 "template <typename AAAAAAA>\n"
26014 "void bar()\n"
26015 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26016 "{}\n"
26017 "template <typename AAAAAAA>\n"
26018 "requires Foo<AAAAAAAA>\n"
26019 "Bar(T) -> Bar<T>;\n"
26020 "template <typename AAAAAAA>\n"
26021 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26022 "Bar(T) -> Bar<T>;",
26023 ColumnStyle);
26024 }
26025
26026 TEST_F(FormatTest, RequiresClauses) {
26027 verifyFormat("struct [[nodiscard]] zero_t {\n"
26028 " template <class T>\n"
26029 " requires requires { number_zero_v<T>; }\n"
26030 " [[nodiscard]] constexpr operator T() const {\n"
26031 " return number_zero_v<T>;\n"
26032 " }\n"
26033 "};");
26034
26035 verifyFormat("template <class T>\n"
26036 " requires(std::same_as<int, T>)\n"
26037 "decltype(auto) fun() {}");
26038
26039 auto Style = getLLVMStyle();
26040
26041 verifyFormat(
26042 "template <typename T>\n"
26043 " requires is_default_constructible_v<hash<T>> and\n"
26044 " is_copy_constructible_v<hash<T>> and\n"
26045 " is_move_constructible_v<hash<T>> and\n"
26046 " is_copy_assignable_v<hash<T>> and "
26047 "is_move_assignable_v<hash<T>> and\n"
26048 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
26049 " is_callable_v<hash<T>(T)> and\n"
26050 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
26051 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
26052 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
26053 "struct S {};",
26054 Style);
26055
26056 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
26057 verifyFormat(
26058 "template <typename T>\n"
26059 " requires is_default_constructible_v<hash<T>>\n"
26060 " and is_copy_constructible_v<hash<T>>\n"
26061 " and is_move_constructible_v<hash<T>>\n"
26062 " and is_copy_assignable_v<hash<T>> and "
26063 "is_move_assignable_v<hash<T>>\n"
26064 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
26065 " and is_callable_v<hash<T>(T)>\n"
26066 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
26067 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
26068 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
26069 "&>()))>\n"
26070 "struct S {};",
26071 Style);
26072
26073 Style = getLLVMStyle();
26074 Style.ConstructorInitializerIndentWidth = 4;
26075 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
26076 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
26077 verifyFormat("constexpr Foo(Foo const &other)\n"
26078 " requires std::is_copy_constructible<T>\n"
26079 " : value{other.value} {\n"
26080 " do_magic();\n"
26081 " do_more_magic();\n"
26082 "}",
26083 Style);
26084
26085 // Not a clause, but we once hit an assert.
26086 verifyFormat("#if 0\n"
26087 "#else\n"
26088 "foo();\n"
26089 "#endif\n"
26090 "bar(requires);");
26091 }
26092
26093 TEST_F(FormatTest, RequiresExpressionIndentation) {
26094 auto Style = getLLVMStyle();
26095 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
26096
26097 verifyFormat("template <typename T>\n"
26098 "concept C = requires(T t) {\n"
26099 " typename T::value;\n"
26100 " requires requires(typename T::value v) {\n"
26101 " { t == v } -> std::same_as<bool>;\n"
26102 " };\n"
26103 "};",
26104 Style);
26105
26106 verifyFormat("template <typename T>\n"
26107 "void bar(T)\n"
26108 " requires Foo<T> && requires(T t) {\n"
26109 " { t.foo() } -> std::same_as<int>;\n"
26110 " } && requires(T t) {\n"
26111 " { t.bar() } -> std::same_as<bool>;\n"
26112 " --t;\n"
26113 " };",
26114 Style);
26115
26116 verifyFormat("template <typename T>\n"
26117 " requires Foo<T> &&\n"
26118 " requires(T t) {\n"
26119 " { t.foo() } -> std::same_as<int>;\n"
26120 " } && requires(T t) {\n"
26121 " { t.bar() } -> std::same_as<bool>;\n"
26122 " --t;\n"
26123 " }\n"
26124 "void bar(T);",
26125 Style);
26126
26127 verifyFormat("template <typename T> void f() {\n"
26128 " if constexpr (requires(T t) {\n"
26129 " { t.bar() } -> std::same_as<bool>;\n"
26130 " }) {\n"
26131 " }\n"
26132 "}",
26133 Style);
26134
26135 verifyFormat("template <typename T> void f() {\n"
26136 " if constexpr (condition && requires(T t) {\n"
26137 " { t.bar() } -> std::same_as<bool>;\n"
26138 " }) {\n"
26139 " }\n"
26140 "}",
26141 Style);
26142
26143 verifyFormat("template <typename T> struct C {\n"
26144 " void f()\n"
26145 " requires requires(T t) {\n"
26146 " { t.bar() } -> std::same_as<bool>;\n"
26147 " };\n"
26148 "};",
26149 Style);
26150
26151 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26152
26153 verifyFormat("template <typename T>\n"
26154 "concept C = requires(T t) {\n"
26155 " typename T::value;\n"
26156 " requires requires(typename T::value v) {\n"
26157 " { t == v } -> std::same_as<bool>;\n"
26158 " };\n"
26159 " };",
26160 Style);
26161
26162 verifyFormat(
26163 "template <typename T>\n"
26164 "void bar(T)\n"
26165 " requires Foo<T> && requires(T t) {\n"
26166 " { t.foo() } -> std::same_as<int>;\n"
26167 " } && requires(T t) {\n"
26168 " { t.bar() } -> std::same_as<bool>;\n"
26169 " --t;\n"
26170 " };",
26171 Style);
26172
26173 verifyFormat("template <typename T>\n"
26174 " requires Foo<T> &&\n"
26175 " requires(T t) {\n"
26176 " { t.foo() } -> std::same_as<int>;\n"
26177 " } && requires(T t) {\n"
26178 " { t.bar() } -> std::same_as<bool>;\n"
26179 " --t;\n"
26180 " }\n"
26181 "void bar(T);",
26182 Style);
26183
26184 verifyFormat("template <typename T> void f() {\n"
26185 " if constexpr (requires(T t) {\n"
26186 " { t.bar() } -> std::same_as<bool>;\n"
26187 " }) {\n"
26188 " }\n"
26189 "}",
26190 Style);
26191
26192 verifyFormat(
26193 "template <typename T> void f() {\n"
26194 " if constexpr (condition && requires(T t) {\n"
26195 " { t.bar() } -> std::same_as<bool>;\n"
26196 " }) {\n"
26197 " }\n"
26198 "}",
26199 Style);
26200
26201 verifyFormat("template <typename T> struct C {\n"
26202 " void f()\n"
26203 " requires requires(T t) {\n"
26204 " { t.bar() } -> std::same_as<bool>;\n"
26205 " };\n"
26206 "};",
26207 Style);
26208 }
26209
26210 TEST_F(FormatTest, StatementAttributeLikeMacros) {
26211 FormatStyle Style = getLLVMStyle();
26212 StringRef Source = "void Foo::slot() {\n"
26213 " unsigned char MyChar = 'x';\n"
26214 " emit signal(MyChar);\n"
26215 " Q_EMIT signal(MyChar);\n"
26216 "}";
26217
26218 verifyFormat(Source, Style);
26219
26220 Style.AlignConsecutiveDeclarations.Enabled = true;
26221 verifyFormat("void Foo::slot() {\n"
26222 " unsigned char MyChar = 'x';\n"
26223 " emit signal(MyChar);\n"
26224 " Q_EMIT signal(MyChar);\n"
26225 "}",
26226 Source, Style);
26227
26228 Style.StatementAttributeLikeMacros.push_back("emit");
26229 verifyFormat(Source, Style);
26230
26231 Style.StatementAttributeLikeMacros = {};
26232 verifyFormat("void Foo::slot() {\n"
26233 " unsigned char MyChar = 'x';\n"
26234 " emit signal(MyChar);\n"
26235 " Q_EMIT signal(MyChar);\n"
26236 "}",
26237 Source, Style);
26238 }
26239
26240 TEST_F(FormatTest, IndentAccessModifiers) {
26241 FormatStyle Style = getLLVMStyle();
26242 Style.IndentAccessModifiers = true;
26243 // Members are *two* levels below the record;
26244 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
26245 verifyFormat("class C {\n"
26246 " int i;\n"
26247 "};",
26248 Style);
26249 verifyFormat("union C {\n"
26250 " int i;\n"
26251 " unsigned u;\n"
26252 "};",
26253 Style);
26254 // Access modifiers should be indented one level below the record.
26255 verifyFormat("class C {\n"
26256 " public:\n"
26257 " int i;\n"
26258 "};",
26259 Style);
26260 verifyFormat("class C {\n"
26261 " public /* comment */:\n"
26262 " int i;\n"
26263 "};",
26264 Style);
26265 verifyFormat("struct S {\n"
26266 " private:\n"
26267 " class C {\n"
26268 " int j;\n"
26269 "\n"
26270 " public:\n"
26271 " C();\n"
26272 " };\n"
26273 "\n"
26274 " public:\n"
26275 " int i;\n"
26276 "};",
26277 Style);
26278 // Enumerations are not records and should be unaffected.
26279 Style.AllowShortEnumsOnASingleLine = false;
26280 verifyFormat("enum class E {\n"
26281 " A,\n"
26282 " B\n"
26283 "};",
26284 Style);
26285 // Test with a different indentation width;
26286 // also proves that the result is Style.AccessModifierOffset agnostic.
26287 Style.IndentWidth = 3;
26288 verifyFormat("class C {\n"
26289 " public:\n"
26290 " int i;\n"
26291 "};",
26292 Style);
26293 verifyFormat("class C {\n"
26294 " public /**/:\n"
26295 " int i;\n"
26296 "};",
26297 Style);
26298 Style.AttributeMacros.push_back("FOO");
26299 verifyFormat("class C {\n"
26300 " FOO public:\n"
26301 " int i;\n"
26302 "};",
26303 Style);
26304 }
26305
26306 TEST_F(FormatTest, LimitlessStringsAndComments) {
26307 auto Style = getLLVMStyleWithColumns(0);
26308 constexpr StringRef Code =
26309 "/**\n"
26310 " * This is a multiline comment with quite some long lines, at least for "
26311 "the LLVM Style.\n"
26312 " * We will redo this with strings and line comments. Just to check if "
26313 "everything is working.\n"
26314 " */\n"
26315 "bool foo() {\n"
26316 " /* Single line multi line comment. */\n"
26317 " const std::string String = \"This is a multiline string with quite "
26318 "some long lines, at least for the LLVM Style.\"\n"
26319 " \"We already did it with multi line "
26320 "comments, and we will do it with line comments. Just to check if "
26321 "everything is working.\";\n"
26322 " // This is a line comment (block) with quite some long lines, at "
26323 "least for the LLVM Style.\n"
26324 " // We already did this with multi line comments and strings. Just to "
26325 "check if everything is working.\n"
26326 " const std::string SmallString = \"Hello World\";\n"
26327 " // Small line comment\n"
26328 " return String.size() > SmallString.size();\n"
26329 "}";
26330 verifyNoChange(Code, Style);
26331 }
26332
26333 TEST_F(FormatTest, FormatDecayCopy) {
26334 // error cases from unit tests
26335 verifyFormat("foo(auto())");
26336 verifyFormat("foo(auto{})");
26337 verifyFormat("foo(auto({}))");
26338 verifyFormat("foo(auto{{}})");
26339
26340 verifyFormat("foo(auto(1))");
26341 verifyFormat("foo(auto{1})");
26342 verifyFormat("foo(new auto(1))");
26343 verifyFormat("foo(new auto{1})");
26344 verifyFormat("decltype(auto(1)) x;");
26345 verifyFormat("decltype(auto{1}) x;");
26346 verifyFormat("auto(x);");
26347 verifyFormat("auto{x};");
26348 verifyFormat("new auto{x};");
26349 verifyFormat("auto{x} = y;");
26350 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26351 // the user's own fault
26352 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26353 // clearly the user's own fault
26354 verifyFormat("auto (*p)() = f;");
26355 }
26356
26357 TEST_F(FormatTest, Cpp20ModulesSupport) {
26358 FormatStyle Style = getLLVMStyle();
26359 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
26360 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
26361
26362 verifyFormat("export import foo;", Style);
26363 verifyFormat("export import foo:bar;", Style);
26364 verifyFormat("export import foo.bar;", Style);
26365 verifyFormat("export import foo.bar:baz;", Style);
26366 verifyFormat("export import :bar;", Style);
26367 verifyFormat("export module foo:bar;", Style);
26368 verifyFormat("export module foo;", Style);
26369 verifyFormat("export module foo.bar;", Style);
26370 verifyFormat("export module foo.bar:baz;", Style);
26371 verifyFormat("export import <string_view>;", Style);
26372 verifyFormat("export import <Foo/Bar>;", Style);
26373
26374 verifyFormat("export type_name var;", Style);
26375 verifyFormat("template <class T> export using A = B<T>;", Style);
26376 verifyFormat("export using A = B;", Style);
26377 verifyFormat("export int func() {\n"
26378 " foo();\n"
26379 "}",
26380 Style);
26381 verifyFormat("export struct {\n"
26382 " int foo;\n"
26383 "};",
26384 Style);
26385 verifyFormat("export {\n"
26386 " int foo;\n"
26387 "};",
26388 Style);
26389 verifyFormat("export export char const *hello() { return \"hello\"; }");
26390
26391 verifyFormat("import bar;", Style);
26392 verifyFormat("import foo.bar;", Style);
26393 verifyFormat("import foo:bar;", Style);
26394 verifyFormat("import :bar;", Style);
26395 verifyFormat("import /* module partition */ :bar;", Style);
26396 verifyFormat("import <ctime>;", Style);
26397 verifyFormat("import \"header\";", Style);
26398
26399 verifyFormat("module foo;", Style);
26400 verifyFormat("module foo:bar;", Style);
26401 verifyFormat("module foo.bar;", Style);
26402 verifyFormat("module;", Style);
26403
26404 verifyFormat("export namespace hi {\n"
26405 "const char *sayhi();\n"
26406 "}",
26407 Style);
26408
26409 verifyFormat("module :private;", Style);
26410 verifyFormat("import <foo/bar.h>;", Style);
26411 verifyFormat("import foo...bar;", Style);
26412 verifyFormat("import ..........;", Style);
26413 verifyFormat("module foo:private;", Style);
26414 verifyFormat("import a", Style);
26415 verifyFormat("module a", Style);
26416 verifyFormat("export import a", Style);
26417 verifyFormat("export module a", Style);
26418
26419 verifyFormat("import", Style);
26420 verifyFormat("module", Style);
26421 verifyFormat("export", Style);
26422
26423 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26424 }
26425
26426 TEST_F(FormatTest, CoroutineForCoawait) {
26427 FormatStyle Style = getLLVMStyle();
26428 verifyFormat("for co_await (auto x : range())\n ;");
26429 verifyFormat("for (auto i : arr) {\n"
26430 "}",
26431 Style);
26432 verifyFormat("for co_await (auto i : arr) {\n"
26433 "}",
26434 Style);
26435 verifyFormat("for co_await (auto i : foo(T{})) {\n"
26436 "}",
26437 Style);
26438 }
26439
26440 TEST_F(FormatTest, CoroutineCoAwait) {
26441 verifyFormat("int x = co_await foo();");
26442 verifyFormat("int x = (co_await foo());");
26443 verifyFormat("co_await (42);");
26444 verifyFormat("void operator co_await(int);");
26445 verifyFormat("void operator co_await(a);");
26446 verifyFormat("co_await a;");
26447 verifyFormat("co_await missing_await_resume{};");
26448 verifyFormat("co_await a; // comment");
26449 verifyFormat("void test0() { co_await a; }");
26450 verifyFormat("co_await co_await co_await foo();");
26451 verifyFormat("co_await foo().bar();");
26452 verifyFormat("co_await [this]() -> Task { co_return x; }");
26453 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26454 "foo(); }(x, y);");
26455
26456 FormatStyle Style = getLLVMStyleWithColumns(40);
26457 verifyFormat("co_await [this](int a, int b) -> Task {\n"
26458 " co_return co_await foo();\n"
26459 "}(x, y);",
26460 Style);
26461 verifyFormat("co_await;");
26462 }
26463
26464 TEST_F(FormatTest, CoroutineCoYield) {
26465 verifyFormat("int x = co_yield foo();");
26466 verifyFormat("int x = (co_yield foo());");
26467 verifyFormat("co_yield (42);");
26468 verifyFormat("co_yield {42};");
26469 verifyFormat("co_yield 42;");
26470 verifyFormat("co_yield n++;");
26471 verifyFormat("co_yield ++n;");
26472 verifyFormat("co_yield;");
26473 }
26474
26475 TEST_F(FormatTest, CoroutineCoReturn) {
26476 verifyFormat("co_return (42);");
26477 verifyFormat("co_return;");
26478 verifyFormat("co_return {};");
26479 verifyFormat("co_return x;");
26480 verifyFormat("co_return co_await foo();");
26481 verifyFormat("co_return co_yield foo();");
26482 }
26483
26484 TEST_F(FormatTest, EmptyShortBlock) {
26485 auto Style = getLLVMStyle();
26486 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
26487
26488 verifyFormat("try {\n"
26489 " doA();\n"
26490 "} catch (Exception &e) {\n"
26491 " e.printStackTrace();\n"
26492 "}",
26493 Style);
26494
26495 verifyFormat("try {\n"
26496 " doA();\n"
26497 "} catch (Exception &e) {}",
26498 Style);
26499 }
26500
26501 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
26502 auto Style = getLLVMStyle();
26503
26504 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
26505 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
26506 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
26507 verifyFormat("struct Y<[] { return 0; }> {};", Style);
26508
26509 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
26510 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
26511 }
26512
26513 TEST_F(FormatTest, MultilineLambdaInConditional) {
26514 auto Style = getLLVMStyleWithColumns(70);
26515 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26516 " ;\n"
26517 " return 5;\n"
26518 "}()\n"
26519 " : 2;",
26520 Style);
26521 verifyFormat(
26522 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26523 " ;\n"
26524 " return 5;\n"
26525 "}();",
26526 Style);
26527
26528 Style = getLLVMStyleWithColumns(60);
26529 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26530 " ? []() {\n"
26531 " ;\n"
26532 " return 5;\n"
26533 " }()\n"
26534 " : 2;",
26535 Style);
26536 verifyFormat("auto aLengthyIdentifier =\n"
26537 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
26538 " ;\n"
26539 " return 5;\n"
26540 " }();",
26541 Style);
26542
26543 Style = getLLVMStyleWithColumns(40);
26544 verifyFormat("auto aLengthyIdentifier =\n"
26545 " oneExpressionSoThatWeBreak ? []() {\n"
26546 " ;\n"
26547 " return 5;\n"
26548 " }()\n"
26549 " : 2;",
26550 Style);
26551 verifyFormat("auto aLengthyIdentifier =\n"
26552 " oneExpressionSoThatWeBreak\n"
26553 " ? 2\n"
26554 " : []() {\n"
26555 " ;\n"
26556 " return 5;\n"
26557 " };",
26558 Style);
26559 }
26560
26561 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
26562 auto Style = getLLVMStyle();
26563
26564 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
26565 "void functionDecl(int a, int b, int c);";
26566
26567 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26568 "paramF, paramG, paramH, paramI);\n"
26569 "void functionDecl(int argumentA, int argumentB, int "
26570 "argumentC, int argumentD, int argumentE);";
26571
26572 verifyFormat(Short, Style);
26573
26574 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26575 "paramF, paramG, paramH,\n"
26576 " paramI);\n"
26577 "void functionDecl(int argumentA, int argumentB, int "
26578 "argumentC, int argumentD,\n"
26579 " int argumentE);";
26580
26581 verifyFormat(NoBreak, Medium, Style);
26582 verifyFormat(NoBreak,
26583 "functionCall(\n"
26584 " paramA,\n"
26585 " paramB,\n"
26586 " paramC,\n"
26587 " paramD,\n"
26588 " paramE,\n"
26589 " paramF,\n"
26590 " paramG,\n"
26591 " paramH,\n"
26592 " paramI\n"
26593 ");\n"
26594 "void functionDecl(\n"
26595 " int argumentA,\n"
26596 " int argumentB,\n"
26597 " int argumentC,\n"
26598 " int argumentD,\n"
26599 " int argumentE\n"
26600 ");",
26601 Style);
26602
26603 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26604 " nestedLongFunctionCall(argument1, "
26605 "argument2, argument3,\n"
26606 " argument4, "
26607 "argument5));",
26608 Style);
26609
26610 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26611
26612 verifyFormat(Short, Style);
26613 verifyFormat(
26614 "functionCall(\n"
26615 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26616 "paramI\n"
26617 ");\n"
26618 "void functionDecl(\n"
26619 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26620 "argumentE\n"
26621 ");",
26622 Medium, Style);
26623
26624 Style.AllowAllArgumentsOnNextLine = false;
26625 Style.AllowAllParametersOfDeclarationOnNextLine = false;
26626
26627 verifyFormat(Short, Style);
26628 verifyFormat(
26629 "functionCall(\n"
26630 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26631 "paramI\n"
26632 ");\n"
26633 "void functionDecl(\n"
26634 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26635 "argumentE\n"
26636 ");",
26637 Medium, Style);
26638
26639 Style.BinPackArguments = false;
26640 Style.BinPackParameters = false;
26641
26642 verifyFormat(Short, Style);
26643
26644 verifyFormat("functionCall(\n"
26645 " paramA,\n"
26646 " paramB,\n"
26647 " paramC,\n"
26648 " paramD,\n"
26649 " paramE,\n"
26650 " paramF,\n"
26651 " paramG,\n"
26652 " paramH,\n"
26653 " paramI\n"
26654 ");\n"
26655 "void functionDecl(\n"
26656 " int argumentA,\n"
26657 " int argumentB,\n"
26658 " int argumentC,\n"
26659 " int argumentD,\n"
26660 " int argumentE\n"
26661 ");",
26662 Medium, Style);
26663
26664 verifyFormat("outerFunctionCall(\n"
26665 " nestedFunctionCall(argument1),\n"
26666 " nestedLongFunctionCall(\n"
26667 " argument1,\n"
26668 " argument2,\n"
26669 " argument3,\n"
26670 " argument4,\n"
26671 " argument5\n"
26672 " )\n"
26673 ");",
26674 Style);
26675
26676 verifyFormat("int a = (int)b;", Style);
26677 verifyFormat("int a = (int)b;",
26678 "int a = (\n"
26679 " int\n"
26680 ") b;",
26681 Style);
26682
26683 verifyFormat("return (true);", Style);
26684 verifyFormat("return (true);",
26685 "return (\n"
26686 " true\n"
26687 ");",
26688 Style);
26689
26690 verifyFormat("void foo();", Style);
26691 verifyFormat("void foo();",
26692 "void foo(\n"
26693 ");",
26694 Style);
26695
26696 verifyFormat("void foo() {}", Style);
26697 verifyFormat("void foo() {}",
26698 "void foo(\n"
26699 ") {\n"
26700 "}",
26701 Style);
26702
26703 verifyFormat("auto string = std::string();", Style);
26704 verifyFormat("auto string = std::string();",
26705 "auto string = std::string(\n"
26706 ");",
26707 Style);
26708
26709 verifyFormat("void (*functionPointer)() = nullptr;", Style);
26710 verifyFormat("void (*functionPointer)() = nullptr;",
26711 "void (\n"
26712 " *functionPointer\n"
26713 ")\n"
26714 "(\n"
26715 ") = nullptr;",
26716 Style);
26717 }
26718
26719 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
26720 auto Style = getLLVMStyle();
26721
26722 verifyFormat("if (foo()) {\n"
26723 " return;\n"
26724 "}",
26725 Style);
26726
26727 verifyFormat("if (quiteLongArg !=\n"
26728 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26729 "comment\n"
26730 " return;\n"
26731 "}",
26732 Style);
26733
26734 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26735
26736 verifyFormat("if (foo()) {\n"
26737 " return;\n"
26738 "}",
26739 Style);
26740
26741 verifyFormat("if (quiteLongArg !=\n"
26742 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26743 "comment\n"
26744 " return;\n"
26745 "}",
26746 Style);
26747
26748 verifyFormat("void foo() {\n"
26749 " if (camelCaseName < alsoLongName ||\n"
26750 " anotherEvenLongerName <=\n"
26751 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
26752 "\n"
26753 " otherName < thisLastName) {\n"
26754 " return;\n"
26755 " } else if (quiteLongName < alsoLongName ||\n"
26756 " anotherEvenLongerName <=\n"
26757 " thisReallyReallyReallyReallyReallyReallyLonger"
26758 "Name ||\n"
26759 " otherName < thisLastName) {\n"
26760 " return;\n"
26761 " }\n"
26762 "}",
26763 Style);
26764
26765 Style.ContinuationIndentWidth = 2;
26766 verifyFormat("void foo() {\n"
26767 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
26768 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26769 " if (false) {\n"
26770 " return;\n"
26771 " } else if (thisIsRatherALongIfClause && "
26772 "thatIExpectToBeBroken ||\n"
26773 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26774 " return;\n"
26775 " }\n"
26776 " }\n"
26777 "}",
26778 Style);
26779 }
26780
26781 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
26782 auto Style = getLLVMStyle();
26783
26784 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
26785 " doSomething();\n"
26786 "}",
26787 Style);
26788
26789 verifyFormat("for (int myReallyLongCountVariable = 0; "
26790 "myReallyLongCountVariable < count;\n"
26791 " myReallyLongCountVariable++) {\n"
26792 " doSomething();\n"
26793 "}",
26794 Style);
26795
26796 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26797
26798 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
26799 " doSomething();\n"
26800 "}",
26801 Style);
26802
26803 verifyFormat("for (int myReallyLongCountVariable = 0; "
26804 "myReallyLongCountVariable < count;\n"
26805 " myReallyLongCountVariable++) {\n"
26806 " doSomething();\n"
26807 "}",
26808 Style);
26809 }
26810
26811 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
26812 auto Style = getLLVMStyleWithColumns(60);
26813 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26814 // Aggregate initialization.
26815 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
26816 " 10000000, 20000000\n"
26817 "};",
26818 Style);
26819 verifyFormat("SomeStruct s{\n"
26820 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
26821 " \"zzzzzzzzzzzzzzzz\"\n"
26822 "};",
26823 Style);
26824 // Designated initializers.
26825 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
26826 " [0] = 10000000, [1] = 20000000\n"
26827 "};",
26828 Style);
26829 verifyFormat("SomeStruct s{\n"
26830 " .foo = \"xxxxxxxxxxxxx\",\n"
26831 " .bar = \"yyyyyyyyyyyyy\",\n"
26832 " .baz = \"zzzzzzzzzzzzz\"\n"
26833 "};",
26834 Style);
26835 // List initialization.
26836 verifyFormat("SomeStruct s{\n"
26837 " \"xxxxxxxxxxxxx\",\n"
26838 " \"yyyyyyyyyyyyy\",\n"
26839 " \"zzzzzzzzzzzzz\",\n"
26840 "};",
26841 Style);
26842 verifyFormat("SomeStruct{\n"
26843 " \"xxxxxxxxxxxxx\",\n"
26844 " \"yyyyyyyyyyyyy\",\n"
26845 " \"zzzzzzzzzzzzz\",\n"
26846 "};",
26847 Style);
26848 verifyFormat("new SomeStruct{\n"
26849 " \"xxxxxxxxxxxxx\",\n"
26850 " \"yyyyyyyyyyyyy\",\n"
26851 " \"zzzzzzzzzzzzz\",\n"
26852 "};",
26853 Style);
26854 // Member initializer.
26855 verifyFormat("class SomeClass {\n"
26856 " SomeStruct s{\n"
26857 " \"xxxxxxxxxxxxx\",\n"
26858 " \"yyyyyyyyyyyyy\",\n"
26859 " \"zzzzzzzzzzzzz\",\n"
26860 " };\n"
26861 "};",
26862 Style);
26863 // Constructor member initializer.
26864 verifyFormat("SomeClass::SomeClass : strct{\n"
26865 " \"xxxxxxxxxxxxx\",\n"
26866 " \"yyyyyyyyyyyyy\",\n"
26867 " \"zzzzzzzzzzzzz\",\n"
26868 " } {}",
26869 Style);
26870 // Copy initialization.
26871 verifyFormat("SomeStruct s = SomeStruct{\n"
26872 " \"xxxxxxxxxxxxx\",\n"
26873 " \"yyyyyyyyyyyyy\",\n"
26874 " \"zzzzzzzzzzzzz\",\n"
26875 "};",
26876 Style);
26877 // Copy list initialization.
26878 verifyFormat("SomeStruct s = {\n"
26879 " \"xxxxxxxxxxxxx\",\n"
26880 " \"yyyyyyyyyyyyy\",\n"
26881 " \"zzzzzzzzzzzzz\",\n"
26882 "};",
26883 Style);
26884 // Assignment operand initialization.
26885 verifyFormat("s = {\n"
26886 " \"xxxxxxxxxxxxx\",\n"
26887 " \"yyyyyyyyyyyyy\",\n"
26888 " \"zzzzzzzzzzzzz\",\n"
26889 "};",
26890 Style);
26891 // Returned object initialization.
26892 verifyFormat("return {\n"
26893 " \"xxxxxxxxxxxxx\",\n"
26894 " \"yyyyyyyyyyyyy\",\n"
26895 " \"zzzzzzzzzzzzz\",\n"
26896 "};",
26897 Style);
26898 // Initializer list.
26899 verifyFormat("auto initializerList = {\n"
26900 " \"xxxxxxxxxxxxx\",\n"
26901 " \"yyyyyyyyyyyyy\",\n"
26902 " \"zzzzzzzzzzzzz\",\n"
26903 "};",
26904 Style);
26905 // Function parameter initialization.
26906 verifyFormat("func({\n"
26907 " \"xxxxxxxxxxxxx\",\n"
26908 " \"yyyyyyyyyyyyy\",\n"
26909 " \"zzzzzzzzzzzzz\",\n"
26910 "});",
26911 Style);
26912 // Nested init lists.
26913 verifyFormat("SomeStruct s = {\n"
26914 " {{init1, init2, init3, init4, init5},\n"
26915 " {init1, init2, init3, init4, init5}}\n"
26916 "};",
26917 Style);
26918 verifyFormat("SomeStruct s = {\n"
26919 " {{\n"
26920 " .init1 = 1,\n"
26921 " .init2 = 2,\n"
26922 " .init3 = 3,\n"
26923 " .init4 = 4,\n"
26924 " .init5 = 5,\n"
26925 " },\n"
26926 " {init1, init2, init3, init4, init5}}\n"
26927 "};",
26928 Style);
26929 verifyFormat("SomeArrayT a[3] = {\n"
26930 " {\n"
26931 " foo,\n"
26932 " bar,\n"
26933 " },\n"
26934 " {\n"
26935 " foo,\n"
26936 " bar,\n"
26937 " },\n"
26938 " SomeArrayT{},\n"
26939 "};",
26940 Style);
26941 verifyFormat("SomeArrayT a[3] = {\n"
26942 " {foo},\n"
26943 " {\n"
26944 " {\n"
26945 " init1,\n"
26946 " init2,\n"
26947 " init3,\n"
26948 " },\n"
26949 " {\n"
26950 " init1,\n"
26951 " init2,\n"
26952 " init3,\n"
26953 " },\n"
26954 " },\n"
26955 " {baz},\n"
26956 "};",
26957 Style);
26958 }
26959
26960 TEST_F(FormatTest, UnderstandsDigraphs) {
26961 verifyFormat("int arr<:5:> = {};");
26962 verifyFormat("int arr[5] = <%%>;");
26963 verifyFormat("int arr<:::qualified_variable:> = {};");
26964 verifyFormat("int arr[::qualified_variable] = <%%>;");
26965 verifyFormat("%:include <header>");
26966 verifyFormat("%:define A x##y");
26967 verifyFormat("#define A x%:%:y");
26968 }
26969
26970 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
26971 auto Style = getLLVMStyle();
26972 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
26973 Style.AlignConsecutiveAssignments.Enabled = true;
26974 Style.AlignConsecutiveDeclarations.Enabled = true;
26975
26976 // The AlignArray code is incorrect for non square Arrays and can cause
26977 // crashes, these tests assert that the array is not changed but will
26978 // also act as regression tests for when it is properly fixed
26979 verifyFormat("struct test demo[] = {\n"
26980 " {1, 2},\n"
26981 " {3, 4, 5},\n"
26982 " {6, 7, 8}\n"
26983 "};",
26984 Style);
26985 verifyFormat("struct test demo[] = {\n"
26986 " {1, 2, 3, 4, 5},\n"
26987 " {3, 4, 5},\n"
26988 " {6, 7, 8}\n"
26989 "};",
26990 Style);
26991 verifyFormat("struct test demo[] = {\n"
26992 " {1, 2, 3, 4, 5},\n"
26993 " {3, 4, 5},\n"
26994 " {6, 7, 8, 9, 10, 11, 12}\n"
26995 "};",
26996 Style);
26997 verifyFormat("struct test demo[] = {\n"
26998 " {1, 2, 3},\n"
26999 " {3, 4, 5},\n"
27000 " {6, 7, 8, 9, 10, 11, 12}\n"
27001 "};",
27002 Style);
27003
27004 verifyFormat("S{\n"
27005 " {},\n"
27006 " {},\n"
27007 " {a, b}\n"
27008 "};",
27009 Style);
27010 verifyFormat("S{\n"
27011 " {},\n"
27012 " {},\n"
27013 " {a, b},\n"
27014 "};",
27015 Style);
27016 verifyFormat("void foo() {\n"
27017 " auto thing = test{\n"
27018 " {\n"
27019 " {13}, {something}, // A\n"
27020 " }\n"
27021 " };\n"
27022 "}",
27023 "void foo() {\n"
27024 " auto thing = test{\n"
27025 " {\n"
27026 " {13},\n"
27027 " {something}, // A\n"
27028 " }\n"
27029 " };\n"
27030 "}",
27031 Style);
27032 }
27033
27034 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
27035 auto Style = getLLVMStyle();
27036 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
27037 Style.AlignConsecutiveAssignments.Enabled = true;
27038 Style.AlignConsecutiveDeclarations.Enabled = true;
27039
27040 // The AlignArray code is incorrect for non square Arrays and can cause
27041 // crashes, these tests assert that the array is not changed but will
27042 // also act as regression tests for when it is properly fixed
27043 verifyFormat("struct test demo[] = {\n"
27044 " {1, 2},\n"
27045 " {3, 4, 5},\n"
27046 " {6, 7, 8}\n"
27047 "};",
27048 Style);
27049 verifyFormat("struct test demo[] = {\n"
27050 " {1, 2, 3, 4, 5},\n"
27051 " {3, 4, 5},\n"
27052 " {6, 7, 8}\n"
27053 "};",
27054 Style);
27055 verifyFormat("struct test demo[] = {\n"
27056 " {1, 2, 3, 4, 5},\n"
27057 " {3, 4, 5},\n"
27058 " {6, 7, 8, 9, 10, 11, 12}\n"
27059 "};",
27060 Style);
27061 verifyFormat("struct test demo[] = {\n"
27062 " {1, 2, 3},\n"
27063 " {3, 4, 5},\n"
27064 " {6, 7, 8, 9, 10, 11, 12}\n"
27065 "};",
27066 Style);
27067
27068 verifyFormat("S{\n"
27069 " {},\n"
27070 " {},\n"
27071 " {a, b}\n"
27072 "};",
27073 Style);
27074 verifyFormat("S{\n"
27075 " {},\n"
27076 " {},\n"
27077 " {a, b},\n"
27078 "};",
27079 Style);
27080 verifyFormat("void foo() {\n"
27081 " auto thing = test{\n"
27082 " {\n"
27083 " {13}, {something}, // A\n"
27084 " }\n"
27085 " };\n"
27086 "}",
27087 "void foo() {\n"
27088 " auto thing = test{\n"
27089 " {\n"
27090 " {13},\n"
27091 " {something}, // A\n"
27092 " }\n"
27093 " };\n"
27094 "}",
27095 Style);
27096 }
27097
27098 TEST_F(FormatTest, FormatsVariableTemplates) {
27099 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27100 verifyFormat("template <typename T> "
27101 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27102 }
27103
27104 TEST_F(FormatTest, RemoveSemicolon) {
27105 FormatStyle Style = getLLVMStyle();
27106 Style.RemoveSemicolon = true;
27107
27108 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27109 "int max(int a, int b) { return a > b ? a : b; };", Style);
27110
27111 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27112 "int max(int a, int b) { return a > b ? a : b; };;", Style);
27113
27114 verifyFormat("class Foo {\n"
27115 " int getSomething() const { return something; }\n"
27116 "};",
27117 "class Foo {\n"
27118 " int getSomething() const { return something; };\n"
27119 "};",
27120 Style);
27121
27122 verifyFormat("class Foo {\n"
27123 " int getSomething() const { return something; }\n"
27124 "};",
27125 "class Foo {\n"
27126 " int getSomething() const { return something; };;\n"
27127 "};",
27128 Style);
27129
27130 verifyFormat("for (;;) {\n"
27131 "}",
27132 Style);
27133
27134 verifyFormat("class [[deprecated(\"\")]] C {\n"
27135 " int i;\n"
27136 "};",
27137 Style);
27138
27139 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27140 " int i;\n"
27141 "};",
27142 Style);
27143
27144 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
27145
27146 verifyFormat("void main() {}", "void main() {};", Style);
27147
27148 verifyFormat("struct Foo {\n"
27149 " Foo() {}\n"
27150 " ~Foo() {}\n"
27151 "};",
27152 "struct Foo {\n"
27153 " Foo() {};\n"
27154 " ~Foo() {};\n"
27155 "};",
27156 Style);
27157
27158 // We can't (and probably shouldn't) support the following.
27159 #if 0
27160 verifyFormat("void foo() {} //\n"
27161 "int bar;",
27162 "void foo() {}; //\n"
27163 "; int bar;",
27164 Style);
27165 #endif
27166
27167 Style.TypenameMacros.push_back("STRUCT");
27168 verifyFormat("STRUCT(T, B) { int i; };", Style);
27169 }
27170
27171 TEST_F(FormatTest, BreakAfterAttributes) {
27172 constexpr StringRef Code("[[maybe_unused]] const int i;\n"
27173 "[[foo([[]])]] [[maybe_unused]]\n"
27174 "int j;\n"
27175 "[[maybe_unused]]\n"
27176 "foo<int> k;\n"
27177 "[[nodiscard]] inline int f(int &i);\n"
27178 "[[foo([[]])]] [[nodiscard]]\n"
27179 "int g(int &i);\n"
27180 "[[nodiscard]]\n"
27181 "inline int f(int &i) {\n"
27182 " i = 1;\n"
27183 " return 0;\n"
27184 "}\n"
27185 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27186 " i = 0;\n"
27187 " return 1;\n"
27188 "}");
27189
27190 FormatStyle Style = getLLVMStyle();
27191 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
27192 verifyNoChange(Code, Style);
27193
27194 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27195 verifyFormat("[[maybe_unused]] const int i;\n"
27196 "[[foo([[]])]] [[maybe_unused]] int j;\n"
27197 "[[maybe_unused]] foo<int> k;\n"
27198 "[[nodiscard]] inline int f(int &i);\n"
27199 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27200 "[[nodiscard]] inline int f(int &i) {\n"
27201 " i = 1;\n"
27202 " return 0;\n"
27203 "}\n"
27204 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27205 " i = 0;\n"
27206 " return 1;\n"
27207 "}",
27208 Code, Style);
27209
27210 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27211 verifyFormat("[[maybe_unused]]\n"
27212 "const int i;\n"
27213 "[[foo([[]])]] [[maybe_unused]]\n"
27214 "int j;\n"
27215 "[[maybe_unused]]\n"
27216 "foo<int> k;\n"
27217 "[[nodiscard]]\n"
27218 "inline int f(int &i);\n"
27219 "[[foo([[]])]] [[nodiscard]]\n"
27220 "int g(int &i);\n"
27221 "[[nodiscard]]\n"
27222 "inline int f(int &i) {\n"
27223 " i = 1;\n"
27224 " return 0;\n"
27225 "}\n"
27226 "[[foo([[]])]] [[nodiscard]]\n"
27227 "int g(int &i) {\n"
27228 " i = 0;\n"
27229 " return 1;\n"
27230 "}",
27231 Code, Style);
27232
27233 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
27234 " f();\n"
27235 "else\n"
27236 " g();\n"
27237 "[[foo([[]])]]\n"
27238 "switch (b) {\n"
27239 "[[unlikely]] case 1:\n"
27240 " ++b;\n"
27241 " break;\n"
27242 "[[likely]]\n"
27243 "default:\n"
27244 " return;\n"
27245 "}\n"
27246 "[[unlikely]] for (; c > 0; --c)\n"
27247 " h();\n"
27248 "[[likely]]\n"
27249 "while (d > 0)\n"
27250 " --d;");
27251
27252 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
27253 verifyNoChange(CtrlStmtCode, Style);
27254
27255 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27256 verifyFormat("[[likely]] if (a)\n"
27257 " f();\n"
27258 "else\n"
27259 " g();\n"
27260 "[[foo([[]])]] switch (b) {\n"
27261 "[[unlikely]] case 1:\n"
27262 " ++b;\n"
27263 " break;\n"
27264 "[[likely]] default:\n"
27265 " return;\n"
27266 "}\n"
27267 "[[unlikely]] for (; c > 0; --c)\n"
27268 " h();\n"
27269 "[[likely]] while (d > 0)\n"
27270 " --d;",
27271 CtrlStmtCode, Style);
27272
27273 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27274 verifyFormat("[[likely]]\n"
27275 "if (a)\n"
27276 " f();\n"
27277 "else\n"
27278 " g();\n"
27279 "[[foo([[]])]]\n"
27280 "switch (b) {\n"
27281 "[[unlikely]]\n"
27282 "case 1:\n"
27283 " ++b;\n"
27284 " break;\n"
27285 "[[likely]]\n"
27286 "default:\n"
27287 " return;\n"
27288 "}\n"
27289 "[[unlikely]]\n"
27290 "for (; c > 0; --c)\n"
27291 " h();\n"
27292 "[[likely]]\n"
27293 "while (d > 0)\n"
27294 " --d;",
27295 CtrlStmtCode, Style);
27296
27297 constexpr StringRef CtorDtorCode("struct Foo {\n"
27298 " [[deprecated]] Foo();\n"
27299 " [[deprecated]] Foo() {}\n"
27300 " [[deprecated]] ~Foo();\n"
27301 " [[deprecated]] ~Foo() {}\n"
27302 " [[deprecated]] void f();\n"
27303 " [[deprecated]] void f() {}\n"
27304 "};\n"
27305 "[[deprecated]] Bar::Bar() {}\n"
27306 "[[deprecated]] Bar::~Bar() {}\n"
27307 "[[deprecated]] void g() {}");
27308 verifyFormat("struct Foo {\n"
27309 " [[deprecated]]\n"
27310 " Foo();\n"
27311 " [[deprecated]]\n"
27312 " Foo() {}\n"
27313 " [[deprecated]]\n"
27314 " ~Foo();\n"
27315 " [[deprecated]]\n"
27316 " ~Foo() {}\n"
27317 " [[deprecated]]\n"
27318 " void f();\n"
27319 " [[deprecated]]\n"
27320 " void f() {}\n"
27321 "};\n"
27322 "[[deprecated]]\n"
27323 "Bar::Bar() {}\n"
27324 "[[deprecated]]\n"
27325 "Bar::~Bar() {}\n"
27326 "[[deprecated]]\n"
27327 "void g() {}",
27328 CtorDtorCode, Style);
27329
27330 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
27331 verifyFormat("struct Foo {\n"
27332 " [[deprecated]]\n"
27333 " Foo();\n"
27334 " [[deprecated]]\n"
27335 " Foo()\n"
27336 " {\n"
27337 " }\n"
27338 " [[deprecated]]\n"
27339 " ~Foo();\n"
27340 " [[deprecated]]\n"
27341 " ~Foo()\n"
27342 " {\n"
27343 " }\n"
27344 " [[deprecated]]\n"
27345 " void f();\n"
27346 " [[deprecated]]\n"
27347 " void f()\n"
27348 " {\n"
27349 " }\n"
27350 "};\n"
27351 "[[deprecated]]\n"
27352 "Bar::Bar()\n"
27353 "{\n"
27354 "}\n"
27355 "[[deprecated]]\n"
27356 "Bar::~Bar()\n"
27357 "{\n"
27358 "}\n"
27359 "[[deprecated]]\n"
27360 "void g()\n"
27361 "{\n"
27362 "}",
27363 CtorDtorCode, Style);
27364
27365 verifyFormat("struct Foo {\n"
27366 " [[maybe_unused]]\n"
27367 " void operator+();\n"
27368 "};\n"
27369 "[[nodiscard]]\n"
27370 "Foo &operator-(Foo &);",
27371 Style);
27372
27373 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
27374 verifyFormat("[[nodiscard]]\n"
27375 "Foo& operator-(Foo&);",
27376 Style);
27377 }
27378
27379 TEST_F(FormatTest, InsertNewlineAtEOF) {
27380 FormatStyle Style = getLLVMStyle();
27381 Style.InsertNewlineAtEOF = true;
27382
27383 verifyNoChange("int i;\n", Style);
27384 verifyFormat("int i;\n", "int i;", Style);
27385
27386 constexpr StringRef Code{"namespace {\n"
27387 "int i;\n"
27388 "} // namespace"};
27389 verifyFormat(Code.str() + '\n', Code, Style,
27390 {tooling::Range(19, 13)}); // line 3
27391 }
27392
27393 TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
27394 FormatStyle Style = getLLVMStyle();
27395 Style.KeepEmptyLines.AtEndOfFile = true;
27396
27397 const StringRef Code{"int i;\n\n"};
27398 verifyNoChange(Code, Style);
27399 verifyFormat(Code, "int i;\n\n\n", Style);
27400 }
27401
27402 TEST_F(FormatTest, SpaceAfterUDL) {
27403 verifyFormat("auto c = (4s).count();");
27404 verifyFormat("auto x = 5s .count() == 5;");
27405 }
27406
27407 TEST_F(FormatTest, InterfaceAsClassMemberName) {
27408 verifyFormat("class Foo {\n"
27409 " int interface;\n"
27410 " Foo::Foo(int iface) : interface{iface} {}\n"
27411 "}");
27412 }
27413
27414 TEST_F(FormatTest, PreprocessorOverlappingRegions) {
27415 verifyFormat("#ifdef\n\n"
27416 "#else\n"
27417 "#endif",
27418 "#ifdef \n"
27419 " \n"
27420 "\n"
27421 "#else \n"
27422 "#endif ",
27423 getGoogleStyle());
27424 }
27425
27426 TEST_F(FormatTest, RemoveParentheses) {
27427 FormatStyle Style = getLLVMStyle();
27428 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
27429
27430 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
27431 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
27432 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
27433 verifyFormat("decltype((foo->bar)) baz;", Style);
27434 verifyFormat("class __declspec(dllimport) X {};",
27435 "class __declspec((dllimport)) X {};", Style);
27436 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
27437 verifyFormat("while (a)\n"
27438 " b;",
27439 "while (((a)))\n"
27440 " b;",
27441 Style);
27442 verifyFormat("while ((a = b))\n"
27443 " c;",
27444 "while (((a = b)))\n"
27445 " c;",
27446 Style);
27447 verifyFormat("if (a)\n"
27448 " b;",
27449 "if (((a)))\n"
27450 " b;",
27451 Style);
27452 verifyFormat("if constexpr ((a = b))\n"
27453 " c;",
27454 "if constexpr (((a = b)))\n"
27455 " c;",
27456 Style);
27457 verifyFormat("if (({ a; }))\n"
27458 " b;",
27459 "if ((({ a; })))\n"
27460 " b;",
27461 Style);
27462 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27463 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27464 Style);
27465 verifyFormat("return (0);", "return (((0)));", Style);
27466 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
27467 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27468 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27469 Style);
27470
27471 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
27472 verifyFormat("#define Return0 return (0);", Style);
27473 verifyFormat("return 0;", "return (0);", Style);
27474 verifyFormat("co_return 0;", "co_return ((0));", Style);
27475 verifyFormat("return 0;", "return (((0)));", Style);
27476 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
27477 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27478 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27479 Style);
27480 verifyFormat("inline decltype(auto) f() {\n"
27481 " if (a) {\n"
27482 " return (a);\n"
27483 " }\n"
27484 " return (b);\n"
27485 "}",
27486 "inline decltype(auto) f() {\n"
27487 " if (a) {\n"
27488 " return ((a));\n"
27489 " }\n"
27490 " return ((b));\n"
27491 "}",
27492 Style);
27493 verifyFormat("auto g() {\n"
27494 " decltype(auto) x = [] {\n"
27495 " auto y = [] {\n"
27496 " if (a) {\n"
27497 " return a;\n"
27498 " }\n"
27499 " return b;\n"
27500 " };\n"
27501 " if (c) {\n"
27502 " return (c);\n"
27503 " }\n"
27504 " return (d);\n"
27505 " };\n"
27506 " if (e) {\n"
27507 " return e;\n"
27508 " }\n"
27509 " return f;\n"
27510 "}",
27511 "auto g() {\n"
27512 " decltype(auto) x = [] {\n"
27513 " auto y = [] {\n"
27514 " if (a) {\n"
27515 " return ((a));\n"
27516 " }\n"
27517 " return ((b));\n"
27518 " };\n"
27519 " if (c) {\n"
27520 " return ((c));\n"
27521 " }\n"
27522 " return ((d));\n"
27523 " };\n"
27524 " if (e) {\n"
27525 " return ((e));\n"
27526 " }\n"
27527 " return ((f));\n"
27528 "}",
27529 Style);
27530
27531 Style.ColumnLimit = 25;
27532 verifyFormat("return (a + b) - (c + d);",
27533 "return (((a + b)) -\n"
27534 " ((c + d)));",
27535 Style);
27536 }
27537
27538 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
27539 auto Style = getLLVMStyleWithColumns(35);
27540
27541 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
27542 verifyFormat("void foo(int arg1,\n"
27543 " double arg2) noexcept;",
27544 Style);
27545
27546 // The following line does not fit within the 35 column limit, but that's what
27547 // happens with no break allowed.
27548 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27549 " noexcept(baz(arg1)) &&\n"
27550 " noexcept(baz(arg2)));",
27551 Style);
27552
27553 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27554 Style);
27555
27556 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
27557 verifyFormat("void foo(int arg1,\n"
27558 " double arg2) noexcept;",
27559 Style);
27560
27561 verifyFormat("void bar(int arg1, double arg2)\n"
27562 " noexcept(noexcept(baz(arg1)) &&\n"
27563 " noexcept(baz(arg2)));",
27564 Style);
27565
27566 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27567 " noexcept;",
27568 Style);
27569
27570 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
27571 verifyFormat("void foo(int arg1,\n"
27572 " double arg2) noexcept;",
27573 Style);
27574
27575 verifyFormat("void bar(int arg1, double arg2)\n"
27576 " noexcept(noexcept(baz(arg1)) &&\n"
27577 " noexcept(baz(arg2)));",
27578 Style);
27579
27580 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27581 Style);
27582 }
27583
27584 TEST_F(FormatTest, PPBranchesInBracedInit) {
27585 verifyFormat("A a_{kFlag1,\n"
27586 "#if BUILD_FLAG\n"
27587 " kFlag2,\n"
27588 "#else\n"
27589 " kFlag3,\n"
27590 "#endif\n"
27591 " kFlag4};",
27592 "A a_{\n"
27593 " kFlag1,\n"
27594 "#if BUILD_FLAG\n"
27595 " kFlag2,\n"
27596 "#else\n"
27597 " kFlag3,\n"
27598 "#endif\n"
27599 " kFlag4\n"
27600 "};");
27601 }
27602
27603 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
27604 verifyFormat("{\n"
27605 " char *a[] = {\n"
27606 " /* abc */ \"abc\",\n"
27607 "#if FOO\n"
27608 " /* xyz */ \"xyz\",\n"
27609 "#endif\n"
27610 " /* last */ \"last\"};\n"
27611 "}",
27612 getLLVMStyleWithColumns(30));
27613 }
27614
27615 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
27616 constexpr StringRef Code{
27617 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27618
27619 verifyFormat("return \"Code\"\n"
27620 " \"\\0\\52\\26\\55\\55\\0\"\n"
27621 " \"x013\"\n"
27622 " \"\\02\\xBA\";",
27623 Code);
27624
27625 auto Style = getLLVMStyle();
27626 Style.BreakAdjacentStringLiterals = false;
27627 verifyFormat(Code, Style);
27628 }
27629
27630 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
27631 verifyFormat(
27632 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
27633 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
27634 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
27635
27636 auto Style = getLLVMStyle();
27637 Style.ColumnLimit = 15;
27638 verifyNoChange("#define test \\\n"
27639 " /* 测试 */ \\\n"
27640 " \"aa\" \\\n"
27641 " \"bb\"",
27642 Style);
27643
27644 Style.ColumnLimit = 25;
27645 verifyFormat("struct foo {\n"
27646 " int iiiiii; ///< iiiiii\n"
27647 " int b; ///< ыыы\n"
27648 " int c; ///< ыыыы\n"
27649 "};",
27650 Style);
27651
27652 Style.ColumnLimit = 35;
27653 verifyFormat("#define SENSOR_DESC_1 \\\n"
27654 " \"{\" \\\n"
27655 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
27656 " \"}\"",
27657 Style);
27658
27659 Style.ColumnLimit = 80;
27660 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27661 verifyFormat("Languages languages = {\n"
27662 " Language{{'e', 'n'}, U\"Test English\" },\n"
27663 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
27664 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
27665 "};",
27666 Style);
27667 }
27668
27669 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
27670 verifyFormat("return .5;");
27671 verifyFormat("return not '5';");
27672 verifyFormat("return sizeof \"5\";");
27673 }
27674
27675 } // namespace
27676 } // namespace test
27677 } // namespace format
27678 } // namespace clang