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, WrapMultipleStatementIfAndElseBraces) {
1368 auto Style = getLLVMStyle();
1369 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1370 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_AllIfsAndElse;
1371 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1372 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1373 Style.BraceWrapping.BeforeElse = true;
1374
1375 verifyFormat("if (x)\n"
1376 "{\n"
1377 " ++x;\n"
1378 " --y;\n"
1379 "}\n"
1380 "else\n"
1381 "{\n"
1382 " --x;\n"
1383 " ++y;\n"
1384 "}",
1385 Style);
1386 }
1387
1388 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1389 verifyFormat("while (true)\n"
1390 " ;");
1391 verifyFormat("for (;;)\n"
1392 " ;");
1393
1394 FormatStyle AllowsMergedLoops = getLLVMStyle();
1395 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1396
1397 verifyFormat("while (true) continue;", AllowsMergedLoops);
1398 verifyFormat("for (;;) continue;", AllowsMergedLoops);
1399 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1400 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1401 verifyFormat("while (true);", AllowsMergedLoops);
1402 verifyFormat("for (;;);", AllowsMergedLoops);
1403 verifyFormat("for (;;)\n"
1404 " for (;;) continue;",
1405 AllowsMergedLoops);
1406 verifyFormat("for (;;)\n"
1407 " while (true) continue;",
1408 AllowsMergedLoops);
1409 verifyFormat("while (true)\n"
1410 " for (;;) continue;",
1411 AllowsMergedLoops);
1412 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1413 " for (;;) continue;",
1414 AllowsMergedLoops);
1415 verifyFormat("for (;;)\n"
1416 " BOOST_FOREACH (int &v, vec) continue;",
1417 AllowsMergedLoops);
1418 verifyFormat("for (;;) // Can't merge this\n"
1419 " continue;",
1420 AllowsMergedLoops);
1421 verifyFormat("for (;;) /* still don't merge */\n"
1422 " continue;",
1423 AllowsMergedLoops);
1424 verifyFormat("do a++;\n"
1425 "while (true);",
1426 AllowsMergedLoops);
1427 verifyFormat("do /* Don't merge */\n"
1428 " a++;\n"
1429 "while (true);",
1430 AllowsMergedLoops);
1431 verifyFormat("do // Don't merge\n"
1432 " a++;\n"
1433 "while (true);",
1434 AllowsMergedLoops);
1435 verifyFormat("do\n"
1436 " // Don't merge\n"
1437 " a++;\n"
1438 "while (true);",
1439 AllowsMergedLoops);
1440
1441 // Without braces labels are interpreted differently.
1442 verifyFormat("{\n"
1443 " do\n"
1444 " label:\n"
1445 " a++;\n"
1446 " while (true);\n"
1447 "}",
1448 AllowsMergedLoops);
1449
1450 // Don't merge if there are comments before the null statement.
1451 verifyFormat("while (1) //\n"
1452 " ;",
1453 AllowsMergedLoops);
1454 verifyFormat("for (;;) /**/\n"
1455 " ;",
1456 AllowsMergedLoops);
1457 verifyFormat("while (true) /**/\n"
1458 " ;",
1459 "while (true) /**/;", AllowsMergedLoops);
1460 }
1461
1462 TEST_F(FormatTest, FormatShortBracedStatements) {
1463 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1464 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1465 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1466 FormatStyle::SIS_Never);
1467 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1468 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1469 verifyFormat("for (;;) {\n"
1470 " f();\n"
1471 "}");
1472 verifyFormat("/*comment*/ for (;;) {\n"
1473 " f();\n"
1474 "}");
1475 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1476 " f();\n"
1477 "}");
1478 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1479 " f();\n"
1480 "}");
1481 verifyFormat("while (true) {\n"
1482 " f();\n"
1483 "}");
1484 verifyFormat("/*comment*/ while (true) {\n"
1485 " f();\n"
1486 "}");
1487 verifyFormat("if (true) {\n"
1488 " f();\n"
1489 "}");
1490 verifyFormat("/*comment*/ if (true) {\n"
1491 " f();\n"
1492 "}");
1493
1494 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1495 FormatStyle::SBS_Empty;
1496 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1497 FormatStyle::SIS_WithoutElse;
1498 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1499 verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1500 verifyFormat("if (i > 0) {\n"
1501 " return i;\n"
1502 "}",
1503 AllowSimpleBracedStatements);
1504
1505 AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1506 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1507 // Not IF to avoid any confusion that IF is somehow special.
1508 AllowSimpleBracedStatements.IfMacros.push_back("FI");
1509 AllowSimpleBracedStatements.ColumnLimit = 40;
1510 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1511 FormatStyle::SBS_Always;
1512 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1513 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1514 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1515 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1516
1517 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1518 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1519 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1520 verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1521 verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1522 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1523 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1524 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1525 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1526 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1527 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1528 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1529 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1530 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1531 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1532 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1533 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1534 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1535 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1536 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1537 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1538 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1539 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1540 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1541 AllowSimpleBracedStatements);
1542 verifyFormat("if (true) {\n"
1543 " ffffffffffffffffffffffff();\n"
1544 "}",
1545 AllowSimpleBracedStatements);
1546 verifyFormat("if (true) {\n"
1547 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1548 "}",
1549 AllowSimpleBracedStatements);
1550 verifyFormat("if (true) { //\n"
1551 " f();\n"
1552 "}",
1553 AllowSimpleBracedStatements);
1554 verifyFormat("if (true) {\n"
1555 " f();\n"
1556 " f();\n"
1557 "}",
1558 AllowSimpleBracedStatements);
1559 verifyFormat("if (true) {\n"
1560 " f();\n"
1561 "} else {\n"
1562 " f();\n"
1563 "}",
1564 AllowSimpleBracedStatements);
1565 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1566 AllowSimpleBracedStatements);
1567 verifyFormat("MYIF (true) {\n"
1568 " ffffffffffffffffffffffff();\n"
1569 "}",
1570 AllowSimpleBracedStatements);
1571 verifyFormat("MYIF (true) {\n"
1572 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1573 "}",
1574 AllowSimpleBracedStatements);
1575 verifyFormat("MYIF (true) { //\n"
1576 " f();\n"
1577 "}",
1578 AllowSimpleBracedStatements);
1579 verifyFormat("MYIF (true) {\n"
1580 " f();\n"
1581 " f();\n"
1582 "}",
1583 AllowSimpleBracedStatements);
1584 verifyFormat("MYIF (true) {\n"
1585 " f();\n"
1586 "} else {\n"
1587 " f();\n"
1588 "}",
1589 AllowSimpleBracedStatements);
1590
1591 verifyFormat("struct A2 {\n"
1592 " int X;\n"
1593 "};",
1594 AllowSimpleBracedStatements);
1595 verifyFormat("typedef struct A2 {\n"
1596 " int X;\n"
1597 "} A2_t;",
1598 AllowSimpleBracedStatements);
1599 verifyFormat("template <int> struct A2 {\n"
1600 " struct B {};\n"
1601 "};",
1602 AllowSimpleBracedStatements);
1603
1604 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1605 FormatStyle::SIS_Never;
1606 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1607 verifyFormat("if (true) {\n"
1608 " f();\n"
1609 "}",
1610 AllowSimpleBracedStatements);
1611 verifyFormat("if (true) {\n"
1612 " f();\n"
1613 "} else {\n"
1614 " f();\n"
1615 "}",
1616 AllowSimpleBracedStatements);
1617 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1618 verifyFormat("MYIF (true) {\n"
1619 " f();\n"
1620 "}",
1621 AllowSimpleBracedStatements);
1622 verifyFormat("MYIF (true) {\n"
1623 " f();\n"
1624 "} else {\n"
1625 " f();\n"
1626 "}",
1627 AllowSimpleBracedStatements);
1628
1629 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1630 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1631 verifyFormat("while (true) {\n"
1632 " f();\n"
1633 "}",
1634 AllowSimpleBracedStatements);
1635 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1636 verifyFormat("for (;;) {\n"
1637 " f();\n"
1638 "}",
1639 AllowSimpleBracedStatements);
1640 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1641 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1642 " f();\n"
1643 "}",
1644 AllowSimpleBracedStatements);
1645
1646 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1647 FormatStyle::SIS_WithoutElse;
1648 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1649 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1650 FormatStyle::BWACS_Always;
1651
1652 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1653 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1654 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1655 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1656 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1657 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1658 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1659 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1660 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1661 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1662 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1663 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1664 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1665 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1666 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1667 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1668 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1669 AllowSimpleBracedStatements);
1670 verifyFormat("if (true)\n"
1671 "{\n"
1672 " ffffffffffffffffffffffff();\n"
1673 "}",
1674 AllowSimpleBracedStatements);
1675 verifyFormat("if (true)\n"
1676 "{\n"
1677 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1678 "}",
1679 AllowSimpleBracedStatements);
1680 verifyFormat("if (true)\n"
1681 "{ //\n"
1682 " f();\n"
1683 "}",
1684 AllowSimpleBracedStatements);
1685 verifyFormat("if (true)\n"
1686 "{\n"
1687 " f();\n"
1688 " f();\n"
1689 "}",
1690 AllowSimpleBracedStatements);
1691 verifyFormat("if (true)\n"
1692 "{\n"
1693 " f();\n"
1694 "} else\n"
1695 "{\n"
1696 " f();\n"
1697 "}",
1698 AllowSimpleBracedStatements);
1699 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1700 AllowSimpleBracedStatements);
1701 verifyFormat("MYIF (true)\n"
1702 "{\n"
1703 " ffffffffffffffffffffffff();\n"
1704 "}",
1705 AllowSimpleBracedStatements);
1706 verifyFormat("MYIF (true)\n"
1707 "{\n"
1708 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1709 "}",
1710 AllowSimpleBracedStatements);
1711 verifyFormat("MYIF (true)\n"
1712 "{ //\n"
1713 " f();\n"
1714 "}",
1715 AllowSimpleBracedStatements);
1716 verifyFormat("MYIF (true)\n"
1717 "{\n"
1718 " f();\n"
1719 " f();\n"
1720 "}",
1721 AllowSimpleBracedStatements);
1722 verifyFormat("MYIF (true)\n"
1723 "{\n"
1724 " f();\n"
1725 "} else\n"
1726 "{\n"
1727 " f();\n"
1728 "}",
1729 AllowSimpleBracedStatements);
1730
1731 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1732 FormatStyle::SIS_Never;
1733 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1734 verifyFormat("if (true)\n"
1735 "{\n"
1736 " f();\n"
1737 "}",
1738 AllowSimpleBracedStatements);
1739 verifyFormat("if (true)\n"
1740 "{\n"
1741 " f();\n"
1742 "} else\n"
1743 "{\n"
1744 " f();\n"
1745 "}",
1746 AllowSimpleBracedStatements);
1747 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1748 verifyFormat("MYIF (true)\n"
1749 "{\n"
1750 " f();\n"
1751 "}",
1752 AllowSimpleBracedStatements);
1753 verifyFormat("MYIF (true)\n"
1754 "{\n"
1755 " f();\n"
1756 "} else\n"
1757 "{\n"
1758 " f();\n"
1759 "}",
1760 AllowSimpleBracedStatements);
1761
1762 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1763 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1764 verifyFormat("while (true)\n"
1765 "{\n"
1766 " f();\n"
1767 "}",
1768 AllowSimpleBracedStatements);
1769 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1770 verifyFormat("for (;;)\n"
1771 "{\n"
1772 " f();\n"
1773 "}",
1774 AllowSimpleBracedStatements);
1775 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1776 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1777 "{\n"
1778 " f();\n"
1779 "}",
1780 AllowSimpleBracedStatements);
1781
1782 FormatStyle Style = getLLVMStyle();
1783 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1784 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1785
1786 verifyFormat("while (i > 0)\n"
1787 "{\n"
1788 " --i;\n"
1789 "}",
1790 Style);
1791
1792 verifyFormat("if (a)\n"
1793 "{\n"
1794 " ++b;\n"
1795 "}",
1796 Style);
1797
1798 verifyFormat("if (a)\n"
1799 "{\n"
1800 " b = 1;\n"
1801 "} else\n"
1802 "{\n"
1803 " b = 0;\n"
1804 "}",
1805 Style);
1806
1807 verifyFormat("if (a)\n"
1808 "{\n"
1809 " b = 1;\n"
1810 "} else if (c)\n"
1811 "{\n"
1812 " b = 2;\n"
1813 "} else\n"
1814 "{\n"
1815 " b = 0;\n"
1816 "}",
1817 Style);
1818
1819 Style.BraceWrapping.BeforeElse = true;
1820
1821 verifyFormat("if (a)\n"
1822 "{\n"
1823 " b = 1;\n"
1824 "}\n"
1825 "else\n"
1826 "{\n"
1827 " b = 0;\n"
1828 "}",
1829 Style);
1830
1831 verifyFormat("if (a)\n"
1832 "{\n"
1833 " b = 1;\n"
1834 "}\n"
1835 "else if (c)\n"
1836 "{\n"
1837 " b = 2;\n"
1838 "}\n"
1839 "else\n"
1840 "{\n"
1841 " b = 0;\n"
1842 "}",
1843 Style);
1844 }
1845
1846 TEST_F(FormatTest, UnderstandsMacros) {
1847 verifyFormat("#define A (parentheses)");
1848 verifyFormat("/* comment */ #define A (parentheses)");
1849 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1850 // Even the partial code should never be merged.
1851 verifyNoChange("/* comment */ #define A (parentheses)\n"
1852 "#");
1853 verifyFormat("/* comment */ #define A (parentheses)\n"
1854 "#\n");
1855 verifyFormat("/* comment */ #define A (parentheses)\n"
1856 "#define B (parentheses)");
1857 verifyFormat("#define true ((int)1)");
1858 verifyFormat("#define and(x)");
1859 verifyFormat("#define if(x) x");
1860 verifyFormat("#define return(x) (x)");
1861 verifyFormat("#define while(x) for (; x;)");
1862 verifyFormat("#define xor(x) (^(x))");
1863 verifyFormat("#define __except(x)");
1864 verifyFormat("#define __try(x)");
1865
1866 // https://llvm.org/PR54348.
1867 verifyFormat(
1868 "#define A"
1869 " "
1870 "\\\n"
1871 " class & {}");
1872
1873 FormatStyle Style = getLLVMStyle();
1874 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1875 Style.BraceWrapping.AfterFunction = true;
1876 // Test that a macro definition never gets merged with the following
1877 // definition.
1878 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1879 verifyFormat("#define AAA "
1880 " \\\n"
1881 " N "
1882 " \\\n"
1883 " {\n"
1884 "#define BBB }",
1885 Style);
1886 // verifyFormat("#define AAA N { //", Style);
1887
1888 verifyFormat("MACRO(return)");
1889 verifyFormat("MACRO(co_await)");
1890 verifyFormat("MACRO(co_return)");
1891 verifyFormat("MACRO(co_yield)");
1892 verifyFormat("MACRO(return, something)");
1893 verifyFormat("MACRO(co_return, something)");
1894 verifyFormat("MACRO(something##something)");
1895 verifyFormat("MACRO(return##something)");
1896 verifyFormat("MACRO(co_return##something)");
1897
1898 verifyFormat("#define A x:");
1899
1900 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1901 " { \\\n"
1902 " #Bar \\\n"
1903 " }");
1904 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1905 " { #Bar }");
1906 }
1907
1908 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1909 FormatStyle Style = getLLVMStyleWithColumns(60);
1910 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1911 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1912 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1913 verifyFormat("#define A \\\n"
1914 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1915 " { \\\n"
1916 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1917 " }\n"
1918 "X;",
1919 "#define A \\\n"
1920 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1921 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1922 " }\n"
1923 "X;",
1924 Style);
1925 }
1926
1927 TEST_F(FormatTest, ParseIfElse) {
1928 verifyFormat("if (true)\n"
1929 " if (true)\n"
1930 " if (true)\n"
1931 " f();\n"
1932 " else\n"
1933 " g();\n"
1934 " else\n"
1935 " h();\n"
1936 "else\n"
1937 " i();");
1938 verifyFormat("if (true)\n"
1939 " if (true)\n"
1940 " if (true) {\n"
1941 " if (true)\n"
1942 " f();\n"
1943 " } else {\n"
1944 " g();\n"
1945 " }\n"
1946 " else\n"
1947 " h();\n"
1948 "else {\n"
1949 " i();\n"
1950 "}");
1951 verifyFormat("if (true)\n"
1952 " if constexpr (true)\n"
1953 " if (true) {\n"
1954 " if constexpr (true)\n"
1955 " f();\n"
1956 " } else {\n"
1957 " g();\n"
1958 " }\n"
1959 " else\n"
1960 " h();\n"
1961 "else {\n"
1962 " i();\n"
1963 "}");
1964 verifyFormat("if (true)\n"
1965 " if CONSTEXPR (true)\n"
1966 " if (true) {\n"
1967 " if CONSTEXPR (true)\n"
1968 " f();\n"
1969 " } else {\n"
1970 " g();\n"
1971 " }\n"
1972 " else\n"
1973 " h();\n"
1974 "else {\n"
1975 " i();\n"
1976 "}");
1977 verifyFormat("void f() {\n"
1978 " if (a) {\n"
1979 " } else {\n"
1980 " }\n"
1981 "}");
1982 }
1983
1984 TEST_F(FormatTest, ElseIf) {
1985 verifyFormat("if (a) {\n} else if (b) {\n}");
1986 verifyFormat("if (a)\n"
1987 " f();\n"
1988 "else if (b)\n"
1989 " g();\n"
1990 "else\n"
1991 " h();");
1992 verifyFormat("if (a)\n"
1993 " f();\n"
1994 "else // comment\n"
1995 " if (b) {\n"
1996 " g();\n"
1997 " h();\n"
1998 " }");
1999 verifyFormat("if constexpr (a)\n"
2000 " f();\n"
2001 "else if constexpr (b)\n"
2002 " g();\n"
2003 "else\n"
2004 " h();");
2005 verifyFormat("if CONSTEXPR (a)\n"
2006 " f();\n"
2007 "else if CONSTEXPR (b)\n"
2008 " g();\n"
2009 "else\n"
2010 " h();");
2011 verifyFormat("if (a) {\n"
2012 " f();\n"
2013 "}\n"
2014 "// or else ..\n"
2015 "else {\n"
2016 " g()\n"
2017 "}");
2018
2019 verifyFormat("if (a) {\n"
2020 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2022 "}");
2023 verifyFormat("if (a) {\n"
2024 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2026 "}");
2027 verifyFormat("if (a) {\n"
2028 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2030 "}");
2031 verifyFormat("if (a) {\n"
2032 "} else if (\n"
2033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2034 "}",
2035 getLLVMStyleWithColumns(62));
2036 verifyFormat("if (a) {\n"
2037 "} else if constexpr (\n"
2038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2039 "}",
2040 getLLVMStyleWithColumns(62));
2041 verifyFormat("if (a) {\n"
2042 "} else if CONSTEXPR (\n"
2043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2044 "}",
2045 getLLVMStyleWithColumns(62));
2046 }
2047
2048 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2049 FormatStyle Style = getLLVMStyle();
2050 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2051 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2052 verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2053 verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2054 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2055 verifyFormat("int *f1(int &a) const &;", Style);
2056 verifyFormat("int *f1(int &a) const & = 0;", Style);
2057 verifyFormat("int *a = f1();", Style);
2058 verifyFormat("int &b = f2();", Style);
2059 verifyFormat("int &&c = f3();", Style);
2060 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2061 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2062 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2063 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2064 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2065 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2066 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2067 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2068 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2069 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2070 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2071 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2072 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2073 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2074 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2075 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2076 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2077 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2078 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2079 verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2080 verifyFormat(
2081 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2082 " res2 = [](int &a) { return 0000000000000; };",
2083 Style);
2084
2085 Style.AlignConsecutiveDeclarations.Enabled = true;
2086 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2087 verifyFormat("Const unsigned int *c;\n"
2088 "const unsigned int *d;\n"
2089 "Const unsigned int &e;\n"
2090 "const unsigned int &f;\n"
2091 "int *f1(int *a, int &b, int &&c);\n"
2092 "double *(*f2)(int *a, double &&b);\n"
2093 "const unsigned &&g;\n"
2094 "Const unsigned h;",
2095 Style);
2096 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2097 verifyFormat("Const unsigned int *c;\n"
2098 "const unsigned int *d;\n"
2099 "Const unsigned int &e;\n"
2100 "const unsigned int &f;\n"
2101 "int *f1(int *a, int &b, int &&c);\n"
2102 "double *(*f2)(int *a, double &&b);\n"
2103 "const unsigned &&g;\n"
2104 "Const unsigned h;",
2105 Style);
2106
2107 Style.PointerAlignment = FormatStyle::PAS_Left;
2108 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2109 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2110 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2111 verifyFormat("int* f1(int& a) const& = 0;", Style);
2112 verifyFormat("int* a = f1();", Style);
2113 verifyFormat("int& b = f2();", Style);
2114 verifyFormat("int&& c = f3();", Style);
2115 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2116 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2117 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2118 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2119 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2120 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2121 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2122 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2123 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2124 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2125 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2126 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2127 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2128 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2129 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2130 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2131 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2132 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2133 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2134 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2135 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2136 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2137 verifyFormat(
2138 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2139 " res2 = [](int& a) { return 0000000000000; };",
2140 Style);
2141 verifyFormat("[](decltype(foo)& Bar) {}", Style);
2142
2143 Style.AlignConsecutiveDeclarations.Enabled = true;
2144 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2145 verifyFormat("Const unsigned int* c;\n"
2146 "const unsigned int* d;\n"
2147 "Const unsigned int& e;\n"
2148 "const unsigned int& f;\n"
2149 "int* f1(int* a, int& b, int&& c);\n"
2150 "double* (*f2)(int* a, double&& b);\n"
2151 "const unsigned&& g;\n"
2152 "Const unsigned h;",
2153 Style);
2154 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2155 verifyFormat("Const unsigned int* c;\n"
2156 "const unsigned int* d;\n"
2157 "Const unsigned int& e;\n"
2158 "const unsigned int& f;\n"
2159 "int* f1(int* a, int& b, int&& c);\n"
2160 "double* (*f2)(int* a, double&& b);\n"
2161 "const unsigned&& g;\n"
2162 "Const unsigned h;",
2163 Style);
2164
2165 Style.PointerAlignment = FormatStyle::PAS_Right;
2166 Style.ReferenceAlignment = FormatStyle::RAS_Left;
2167 verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2168 verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2169 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2170 verifyFormat("int *a = f1();", Style);
2171 verifyFormat("int& b = f2();", Style);
2172 verifyFormat("int&& c = f3();", Style);
2173 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2174 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2175 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2176 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2177 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2178 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2179 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2180
2181 Style.AlignConsecutiveDeclarations.Enabled = true;
2182 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2183 verifyFormat("Const unsigned int *c;\n"
2184 "const unsigned int *d;\n"
2185 "Const unsigned int& e;\n"
2186 "const unsigned int& f;\n"
2187 "int *f1(int *a, int& b, int&& c);\n"
2188 "double *(*f2)(int *a, double&& b);\n"
2189 "const unsigned&& g;\n"
2190 "Const unsigned h;",
2191 Style);
2192 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2193 verifyFormat("Const unsigned int *c;\n"
2194 "const unsigned int *d;\n"
2195 "Const unsigned int& e;\n"
2196 "const unsigned int& f;\n"
2197 "int *f1(int *a, int& b, int&& c);\n"
2198 "double *(*f2)(int *a, double&& b);\n"
2199 "const unsigned&& g;\n"
2200 "Const unsigned h;",
2201 Style);
2202
2203 Style.PointerAlignment = FormatStyle::PAS_Left;
2204 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2205 verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2206 verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2207 verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2208 verifyFormat("int* a = f1();", Style);
2209 verifyFormat("int & b = f2();", Style);
2210 verifyFormat("int && c = f3();", Style);
2211 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2212 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2213 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2214 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2215 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2216 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2217 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2218 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2219 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2220 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2221 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2222 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2223 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2224 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2225 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2226 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2227 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2228 verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2229 verifyFormat(
2230 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2231 " res2 = [](int & a) { return 0000000000000; };",
2232 Style);
2233
2234 Style.AlignConsecutiveDeclarations.Enabled = true;
2235 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2236 verifyFormat("Const unsigned int* c;\n"
2237 "const unsigned int* d;\n"
2238 "Const unsigned int & e;\n"
2239 "const unsigned int & f;\n"
2240 "int* f1(int* a, int & b, int && c);\n"
2241 "double* (*f2)(int* a, double && b);\n"
2242 "const unsigned && g;\n"
2243 "Const unsigned h;",
2244 Style);
2245 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2246 verifyFormat("Const unsigned int* c;\n"
2247 "const unsigned int* d;\n"
2248 "Const unsigned int & e;\n"
2249 "const unsigned int & f;\n"
2250 "int* f1(int* a, int & b, int && c);\n"
2251 "double* (*f2)(int* a, double && b);\n"
2252 "const unsigned && g;\n"
2253 "Const unsigned h;",
2254 Style);
2255
2256 Style.PointerAlignment = FormatStyle::PAS_Middle;
2257 Style.ReferenceAlignment = FormatStyle::RAS_Right;
2258 verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2259 verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2260 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2261 verifyFormat("int * a = f1();", Style);
2262 verifyFormat("int &b = f2();", Style);
2263 verifyFormat("int &&c = f3();", Style);
2264 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2265 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2266 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2267 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2268 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2269 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2270 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2271
2272 Style.AlignConsecutiveDeclarations.Enabled = true;
2273 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2274 verifyFormat("Const unsigned int * c;\n"
2275 "const unsigned int * d;\n"
2276 "Const unsigned int &e;\n"
2277 "const unsigned int &f;\n"
2278 "int * f1(int * a, int &b, int &&c);\n"
2279 "double * (*f2)(int * a, double &&b);\n"
2280 "const unsigned &&g;\n"
2281 "Const unsigned h;",
2282 Style);
2283 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2284 verifyFormat("Const unsigned int * c;\n"
2285 "const unsigned int * d;\n"
2286 "Const unsigned int &e;\n"
2287 "const unsigned int &f;\n"
2288 "int * f1(int * a, int &b, int &&c);\n"
2289 "double * (*f2)(int * a, double &&b);\n"
2290 "const unsigned &&g;\n"
2291 "Const unsigned h;",
2292 Style);
2293
2294 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2295 // specifically handled
2296 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2297 }
2298
2299 TEST_F(FormatTest, FormatsForLoop) {
2300 verifyFormat(
2301 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2302 " ++VeryVeryLongLoopVariable)\n"
2303 " ;");
2304 verifyFormat("for (;;)\n"
2305 " f();");
2306 verifyFormat("for (;;) {\n}");
2307 verifyFormat("for (;;) {\n"
2308 " f();\n"
2309 "}");
2310 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2311
2312 verifyFormat(
2313 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2314 " E = UnwrappedLines.end();\n"
2315 " I != E; ++I) {\n}");
2316
2317 verifyFormat(
2318 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2319 " ++IIIII) {\n}");
2320 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2321 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2322 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2323 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2324 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2325 " E = FD->getDeclsInPrototypeScope().end();\n"
2326 " I != E; ++I) {\n}");
2327 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2328 " I = Container.begin(),\n"
2329 " E = Container.end();\n"
2330 " I != E; ++I) {\n}",
2331 getLLVMStyleWithColumns(76));
2332
2333 verifyFormat(
2334 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2338 " ++aaaaaaaaaaa) {\n}");
2339 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2340 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2341 " ++i) {\n}");
2342 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2343 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2344 "}");
2345 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2346 " aaaaaaaaaa);\n"
2347 " iter; ++iter) {\n"
2348 "}");
2349 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2351 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2352 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2353
2354 // These should not be formatted as Objective-C for-in loops.
2355 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2356 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2357 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2358 verifyFormat(
2359 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2360
2361 FormatStyle NoBinPacking = getLLVMStyle();
2362 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
2363 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2364 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2365 " aaaaaaaaaaaaaaaa,\n"
2366 " aaaaaaaaaaaaaaaa,\n"
2367 " aaaaaaaaaaaaaaaa);\n"
2368 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2369 "}",
2370 NoBinPacking);
2371 verifyFormat(
2372 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2373 " E = UnwrappedLines.end();\n"
2374 " I != E;\n"
2375 " ++I) {\n}",
2376 NoBinPacking);
2377
2378 FormatStyle AlignLeft = getLLVMStyle();
2379 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2380 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2381 }
2382
2383 TEST_F(FormatTest, RangeBasedForLoops) {
2384 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2386 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2387 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2388 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2390 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2391 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2392 }
2393
2394 TEST_F(FormatTest, ForEachLoops) {
2395 FormatStyle Style = getLLVMStyle();
2396 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2397 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2398 verifyFormat("void f() {\n"
2399 " for (;;) {\n"
2400 " }\n"
2401 " foreach (Item *item, itemlist) {\n"
2402 " }\n"
2403 " Q_FOREACH (Item *item, itemlist) {\n"
2404 " }\n"
2405 " BOOST_FOREACH (Item *item, itemlist) {\n"
2406 " }\n"
2407 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2408 "}",
2409 Style);
2410 verifyFormat("void f() {\n"
2411 " for (;;)\n"
2412 " int j = 1;\n"
2413 " Q_FOREACH (int v, vec)\n"
2414 " v *= 2;\n"
2415 " for (;;) {\n"
2416 " int j = 1;\n"
2417 " }\n"
2418 " Q_FOREACH (int v, vec) {\n"
2419 " v *= 2;\n"
2420 " }\n"
2421 "}",
2422 Style);
2423
2424 FormatStyle ShortBlocks = getLLVMStyle();
2425 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2426 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2427 verifyFormat("void f() {\n"
2428 " for (;;)\n"
2429 " int j = 1;\n"
2430 " Q_FOREACH (int &v, vec)\n"
2431 " v *= 2;\n"
2432 " for (;;) {\n"
2433 " int j = 1;\n"
2434 " }\n"
2435 " Q_FOREACH (int &v, vec) {\n"
2436 " int j = 1;\n"
2437 " }\n"
2438 "}",
2439 ShortBlocks);
2440
2441 FormatStyle ShortLoops = getLLVMStyle();
2442 ShortLoops.AllowShortLoopsOnASingleLine = true;
2443 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2444 verifyFormat("void f() {\n"
2445 " for (;;) int j = 1;\n"
2446 " Q_FOREACH (int &v, vec) int j = 1;\n"
2447 " for (;;) {\n"
2448 " int j = 1;\n"
2449 " }\n"
2450 " Q_FOREACH (int &v, vec) {\n"
2451 " int j = 1;\n"
2452 " }\n"
2453 "}",
2454 ShortLoops);
2455
2456 FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2457 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2458 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2459 verifyFormat("void f() {\n"
2460 " for (;;) int j = 1;\n"
2461 " Q_FOREACH (int &v, vec) int j = 1;\n"
2462 " for (;;) { int j = 1; }\n"
2463 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2464 "}",
2465 ShortBlocksAndLoops);
2466
2467 Style.SpaceBeforeParens =
2468 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2469 verifyFormat("void f() {\n"
2470 " for (;;) {\n"
2471 " }\n"
2472 " foreach(Item *item, itemlist) {\n"
2473 " }\n"
2474 " Q_FOREACH(Item *item, itemlist) {\n"
2475 " }\n"
2476 " BOOST_FOREACH(Item *item, itemlist) {\n"
2477 " }\n"
2478 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2479 "}",
2480 Style);
2481
2482 // As function-like macros.
2483 verifyFormat("#define foreach(x, y)\n"
2484 "#define Q_FOREACH(x, y)\n"
2485 "#define BOOST_FOREACH(x, y)\n"
2486 "#define UNKNOWN_FOREACH(x, y)");
2487
2488 // Not as function-like macros.
2489 verifyFormat("#define foreach (x, y)\n"
2490 "#define Q_FOREACH (x, y)\n"
2491 "#define BOOST_FOREACH (x, y)\n"
2492 "#define UNKNOWN_FOREACH (x, y)");
2493
2494 // handle microsoft non standard extension
2495 verifyFormat("for each (char c in x->MyStringProperty)");
2496 }
2497
2498 TEST_F(FormatTest, FormatsWhileLoop) {
2499 verifyFormat("while (true) {\n}");
2500 verifyFormat("while (true)\n"
2501 " f();");
2502 verifyFormat("while () {\n}");
2503 verifyFormat("while () {\n"
2504 " f();\n"
2505 "}");
2506 }
2507
2508 TEST_F(FormatTest, FormatsDoWhile) {
2509 verifyFormat("do {\n"
2510 " do_something();\n"
2511 "} while (something());");
2512 verifyFormat("do\n"
2513 " do_something();\n"
2514 "while (something());");
2515 }
2516
2517 TEST_F(FormatTest, FormatsSwitchStatement) {
2518 verifyFormat("switch (x) {\n"
2519 "case 1:\n"
2520 " f();\n"
2521 " break;\n"
2522 "case kFoo:\n"
2523 "case ns::kBar:\n"
2524 "case kBaz:\n"
2525 " break;\n"
2526 "default:\n"
2527 " g();\n"
2528 " break;\n"
2529 "}");
2530 verifyFormat("switch (x) {\n"
2531 "case 1: {\n"
2532 " f();\n"
2533 " break;\n"
2534 "}\n"
2535 "case 2: {\n"
2536 " break;\n"
2537 "}\n"
2538 "}");
2539 verifyFormat("switch (x) {\n"
2540 "case 1: {\n"
2541 " f();\n"
2542 " {\n"
2543 " g();\n"
2544 " h();\n"
2545 " }\n"
2546 " break;\n"
2547 "}\n"
2548 "}");
2549 verifyFormat("switch (x) {\n"
2550 "case 1: {\n"
2551 " f();\n"
2552 " if (foo) {\n"
2553 " g();\n"
2554 " h();\n"
2555 " }\n"
2556 " break;\n"
2557 "}\n"
2558 "}");
2559 verifyFormat("switch (x) {\n"
2560 "case 1: {\n"
2561 " f();\n"
2562 " g();\n"
2563 "} break;\n"
2564 "}");
2565 verifyFormat("switch (test)\n"
2566 " ;");
2567 verifyFormat("switch (x) {\n"
2568 "default: {\n"
2569 " // Do nothing.\n"
2570 "}\n"
2571 "}");
2572 verifyFormat("switch (x) {\n"
2573 "// comment\n"
2574 "// if 1, do f()\n"
2575 "case 1:\n"
2576 " f();\n"
2577 "}");
2578 verifyFormat("switch (x) {\n"
2579 "case 1:\n"
2580 " // Do amazing stuff\n"
2581 " {\n"
2582 " f();\n"
2583 " g();\n"
2584 " }\n"
2585 " break;\n"
2586 "}");
2587 verifyFormat("#define A \\\n"
2588 " switch (x) { \\\n"
2589 " case a: \\\n"
2590 " foo = b; \\\n"
2591 " }",
2592 getLLVMStyleWithColumns(20));
2593 verifyFormat("#define OPERATION_CASE(name) \\\n"
2594 " case OP_name: \\\n"
2595 " return operations::Operation##name",
2596 getLLVMStyleWithColumns(40));
2597 verifyFormat("switch (x) {\n"
2598 "case 1:;\n"
2599 "default:;\n"
2600 " int i;\n"
2601 "}");
2602
2603 verifyGoogleFormat("switch (x) {\n"
2604 " case 1:\n"
2605 " f();\n"
2606 " break;\n"
2607 " case kFoo:\n"
2608 " case ns::kBar:\n"
2609 " case kBaz:\n"
2610 " break;\n"
2611 " default:\n"
2612 " g();\n"
2613 " break;\n"
2614 "}");
2615 verifyGoogleFormat("switch (x) {\n"
2616 " case 1: {\n"
2617 " f();\n"
2618 " break;\n"
2619 " }\n"
2620 "}");
2621 verifyGoogleFormat("switch (test)\n"
2622 " ;");
2623
2624 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2625 " case OP_name: \\\n"
2626 " return operations::Operation##name");
2627 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2628 " // Get the correction operation class.\n"
2629 " switch (OpCode) {\n"
2630 " CASE(Add);\n"
2631 " CASE(Subtract);\n"
2632 " default:\n"
2633 " return operations::Unknown;\n"
2634 " }\n"
2635 "#undef OPERATION_CASE\n"
2636 "}");
2637 verifyFormat("DEBUG({\n"
2638 " switch (x) {\n"
2639 " case A:\n"
2640 " f();\n"
2641 " break;\n"
2642 " // fallthrough\n"
2643 " case B:\n"
2644 " g();\n"
2645 " break;\n"
2646 " }\n"
2647 "});");
2648 verifyNoChange("DEBUG({\n"
2649 " switch (x) {\n"
2650 " case A:\n"
2651 " f();\n"
2652 " break;\n"
2653 " // On B:\n"
2654 " case B:\n"
2655 " g();\n"
2656 " break;\n"
2657 " }\n"
2658 "});");
2659 verifyFormat("switch (n) {\n"
2660 "case 0: {\n"
2661 " return false;\n"
2662 "}\n"
2663 "default: {\n"
2664 " return true;\n"
2665 "}\n"
2666 "}",
2667 "switch (n)\n"
2668 "{\n"
2669 "case 0: {\n"
2670 " return false;\n"
2671 "}\n"
2672 "default: {\n"
2673 " return true;\n"
2674 "}\n"
2675 "}");
2676 verifyFormat("switch (a) {\n"
2677 "case (b):\n"
2678 " return;\n"
2679 "}");
2680
2681 verifyFormat("switch (a) {\n"
2682 "case some_namespace::\n"
2683 " some_constant:\n"
2684 " return;\n"
2685 "}",
2686 getLLVMStyleWithColumns(34));
2687
2688 verifyFormat("switch (a) {\n"
2689 "[[likely]] case 1:\n"
2690 " return;\n"
2691 "}");
2692 verifyFormat("switch (a) {\n"
2693 "[[likely]] [[other::likely]] case 1:\n"
2694 " return;\n"
2695 "}");
2696 verifyFormat("switch (x) {\n"
2697 "case 1:\n"
2698 " return;\n"
2699 "[[likely]] case 2:\n"
2700 " return;\n"
2701 "}");
2702 verifyFormat("switch (a) {\n"
2703 "case 1:\n"
2704 "[[likely]] case 2:\n"
2705 " return;\n"
2706 "}");
2707 FormatStyle Attributes = getLLVMStyle();
2708 Attributes.AttributeMacros.push_back("LIKELY");
2709 Attributes.AttributeMacros.push_back("OTHER_LIKELY");
2710 verifyFormat("switch (a) {\n"
2711 "LIKELY case b:\n"
2712 " return;\n"
2713 "}",
2714 Attributes);
2715 verifyFormat("switch (a) {\n"
2716 "LIKELY OTHER_LIKELY() case b:\n"
2717 " return;\n"
2718 "}",
2719 Attributes);
2720 verifyFormat("switch (a) {\n"
2721 "case 1:\n"
2722 " return;\n"
2723 "LIKELY case 2:\n"
2724 " return;\n"
2725 "}",
2726 Attributes);
2727 verifyFormat("switch (a) {\n"
2728 "case 1:\n"
2729 "LIKELY case 2:\n"
2730 " return;\n"
2731 "}",
2732 Attributes);
2733
2734 FormatStyle Style = getLLVMStyle();
2735 Style.IndentCaseLabels = true;
2736 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2737 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2738 Style.BraceWrapping.AfterCaseLabel = true;
2739 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2740 verifyFormat("switch (n)\n"
2741 "{\n"
2742 " case 0:\n"
2743 " {\n"
2744 " return false;\n"
2745 " }\n"
2746 " default:\n"
2747 " {\n"
2748 " return true;\n"
2749 " }\n"
2750 "}",
2751 "switch (n) {\n"
2752 " case 0: {\n"
2753 " return false;\n"
2754 " }\n"
2755 " default: {\n"
2756 " return true;\n"
2757 " }\n"
2758 "}",
2759 Style);
2760 Style.BraceWrapping.AfterCaseLabel = false;
2761 verifyFormat("switch (n)\n"
2762 "{\n"
2763 " case 0: {\n"
2764 " return false;\n"
2765 " }\n"
2766 " default: {\n"
2767 " return true;\n"
2768 " }\n"
2769 "}",
2770 "switch (n) {\n"
2771 " case 0:\n"
2772 " {\n"
2773 " return false;\n"
2774 " }\n"
2775 " default:\n"
2776 " {\n"
2777 " return true;\n"
2778 " }\n"
2779 "}",
2780 Style);
2781 Style.IndentCaseLabels = false;
2782 Style.IndentCaseBlocks = true;
2783 verifyFormat("switch (n)\n"
2784 "{\n"
2785 "case 0:\n"
2786 " {\n"
2787 " return false;\n"
2788 " }\n"
2789 "case 1:\n"
2790 " break;\n"
2791 "default:\n"
2792 " {\n"
2793 " return true;\n"
2794 " }\n"
2795 "}",
2796 "switch (n) {\n"
2797 "case 0: {\n"
2798 " return false;\n"
2799 "}\n"
2800 "case 1:\n"
2801 " break;\n"
2802 "default: {\n"
2803 " return true;\n"
2804 "}\n"
2805 "}",
2806 Style);
2807 Style.IndentCaseLabels = true;
2808 Style.IndentCaseBlocks = true;
2809 verifyFormat("switch (n)\n"
2810 "{\n"
2811 " case 0:\n"
2812 " {\n"
2813 " return false;\n"
2814 " }\n"
2815 " case 1:\n"
2816 " break;\n"
2817 " default:\n"
2818 " {\n"
2819 " return true;\n"
2820 " }\n"
2821 "}",
2822 "switch (n) {\n"
2823 "case 0: {\n"
2824 " return false;\n"
2825 "}\n"
2826 "case 1:\n"
2827 " break;\n"
2828 "default: {\n"
2829 " return true;\n"
2830 "}\n"
2831 "}",
2832 Style);
2833 }
2834
2835 TEST_F(FormatTest, CaseRanges) {
2836 verifyFormat("switch (x) {\n"
2837 "case 'A' ... 'Z':\n"
2838 "case 1 ... 5:\n"
2839 "case a ... b:\n"
2840 " break;\n"
2841 "}");
2842 }
2843
2844 TEST_F(FormatTest, ShortEnums) {
2845 FormatStyle Style = getLLVMStyle();
2846 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2847 EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2848 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2849 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2850 Style.AllowShortEnumsOnASingleLine = false;
2851 verifyFormat("enum {\n"
2852 " A,\n"
2853 " B,\n"
2854 " C\n"
2855 "} ShortEnum1, ShortEnum2;",
2856 Style);
2857 verifyFormat("typedef enum {\n"
2858 " A,\n"
2859 " B,\n"
2860 " C\n"
2861 "} ShortEnum1, ShortEnum2;",
2862 Style);
2863 verifyFormat("enum {\n"
2864 " A,\n"
2865 "} ShortEnum1, ShortEnum2;",
2866 Style);
2867 verifyFormat("typedef enum {\n"
2868 " A,\n"
2869 "} ShortEnum1, ShortEnum2;",
2870 Style);
2871 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2872 Style.BraceWrapping.AfterEnum = true;
2873 verifyFormat("enum\n"
2874 "{\n"
2875 " A,\n"
2876 " B,\n"
2877 " C\n"
2878 "} ShortEnum1, ShortEnum2;",
2879 Style);
2880 verifyFormat("typedef enum\n"
2881 "{\n"
2882 " A,\n"
2883 " B,\n"
2884 " C\n"
2885 "} ShortEnum1, ShortEnum2;",
2886 Style);
2887 }
2888
2889 TEST_F(FormatTest, ShortCompoundRequirement) {
2890 constexpr StringRef Code("template <typename T>\n"
2891 "concept c = requires(T x) {\n"
2892 " { x + 1 } -> std::same_as<int>;\n"
2893 "};");
2894
2895 FormatStyle Style = getLLVMStyle();
2896 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2897 verifyFormat(Code, Style);
2898 verifyFormat("template <typename T>\n"
2899 "concept c = requires(T x) {\n"
2900 " { x + 1 } -> std::same_as<int>;\n"
2901 " { x + 2 } -> std::same_as<int>;\n"
2902 "};",
2903 Style);
2904
2905 Style.AllowShortCompoundRequirementOnASingleLine = false;
2906 verifyFormat("template <typename T>\n"
2907 "concept c = requires(T x) {\n"
2908 " {\n"
2909 " x + 1\n"
2910 " } -> std::same_as<int>;\n"
2911 "};",
2912 Code, Style);
2913 verifyFormat("template <typename T>\n"
2914 "concept c = requires(T x) {\n"
2915 " {\n"
2916 " x + 1\n"
2917 " } -> std::same_as<int>;\n"
2918 " {\n"
2919 " x + 2\n"
2920 " } -> std::same_as<int>;\n"
2921 "};",
2922 Style);
2923
2924 Style.AllowShortCompoundRequirementOnASingleLine = true;
2925 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2926 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2927 verifyFormat(Code, Style);
2928 }
2929
2930 TEST_F(FormatTest, ShortCaseLabels) {
2931 FormatStyle Style = getLLVMStyle();
2932 Style.AllowShortCaseLabelsOnASingleLine = true;
2933 verifyFormat("switch (a) {\n"
2934 "case 1: x = 1; break;\n"
2935 "case 2: return;\n"
2936 "case 3:\n"
2937 "case 4:\n"
2938 "case 5: return;\n"
2939 "case 6: // comment\n"
2940 " return;\n"
2941 "case 7:\n"
2942 " // comment\n"
2943 " return;\n"
2944 "case 8:\n"
2945 " x = 8; // comment\n"
2946 " break;\n"
2947 "default: y = 1; break;\n"
2948 "}",
2949 Style);
2950 verifyFormat("switch (a) {\n"
2951 "case 0: return; // comment\n"
2952 "case 1: break; // comment\n"
2953 "case 2: return;\n"
2954 "// comment\n"
2955 "case 3: return;\n"
2956 "// comment 1\n"
2957 "// comment 2\n"
2958 "// comment 3\n"
2959 "case 4: break; /* comment */\n"
2960 "case 5:\n"
2961 " // comment\n"
2962 " break;\n"
2963 "case 6: /* comment */ x = 1; break;\n"
2964 "case 7: x = /* comment */ 1; break;\n"
2965 "case 8:\n"
2966 " x = 1; /* comment */\n"
2967 " break;\n"
2968 "case 9:\n"
2969 " break; // comment line 1\n"
2970 " // comment line 2\n"
2971 "}",
2972 Style);
2973 verifyFormat("switch (a) {\n"
2974 "case 1:\n"
2975 " x = 8;\n"
2976 " // fall through\n"
2977 "case 2: x = 8;\n"
2978 "// comment\n"
2979 "case 3:\n"
2980 " return; /* comment line 1\n"
2981 " * comment line 2 */\n"
2982 "case 4: i = 8;\n"
2983 "// something else\n"
2984 "#if FOO\n"
2985 "case 5: break;\n"
2986 "#endif\n"
2987 "}",
2988 "switch (a) {\n"
2989 "case 1: x = 8;\n"
2990 " // fall through\n"
2991 "case 2:\n"
2992 " x = 8;\n"
2993 "// comment\n"
2994 "case 3:\n"
2995 " return; /* comment line 1\n"
2996 " * comment line 2 */\n"
2997 "case 4:\n"
2998 " i = 8;\n"
2999 "// something else\n"
3000 "#if FOO\n"
3001 "case 5: break;\n"
3002 "#endif\n"
3003 "}",
3004 Style);
3005 verifyFormat("switch (a) {\n"
3006 "case 0:\n"
3007 " return; // long long long long long long long long long long "
3008 "long long comment\n"
3009 " // line\n"
3010 "}",
3011 "switch (a) {\n"
3012 "case 0: return; // long long long long long long long long "
3013 "long long long long comment line\n"
3014 "}",
3015 Style);
3016 verifyFormat("switch (a) {\n"
3017 "case 0:\n"
3018 " return; /* long long long long long long long long long long "
3019 "long long comment\n"
3020 " line */\n"
3021 "}",
3022 "switch (a) {\n"
3023 "case 0: return; /* long long long long long long long long "
3024 "long long long long comment line */\n"
3025 "}",
3026 Style);
3027 verifyFormat("switch (a) {\n"
3028 "#if FOO\n"
3029 "case 0: return 0;\n"
3030 "#endif\n"
3031 "}",
3032 Style);
3033 verifyFormat("switch (a) {\n"
3034 "case 1: {\n"
3035 "}\n"
3036 "case 2: {\n"
3037 " return;\n"
3038 "}\n"
3039 "case 3: {\n"
3040 " x = 1;\n"
3041 " return;\n"
3042 "}\n"
3043 "case 4:\n"
3044 " if (x)\n"
3045 " return;\n"
3046 "}",
3047 Style);
3048 Style.ColumnLimit = 21;
3049 verifyFormat("#define X \\\n"
3050 " case 0: break;\n"
3051 "#include \"f\"",
3052 Style);
3053 verifyFormat("switch (a) {\n"
3054 "case 1: x = 1; break;\n"
3055 "case 2: return;\n"
3056 "case 3:\n"
3057 "case 4:\n"
3058 "case 5: return;\n"
3059 "default:\n"
3060 " y = 1;\n"
3061 " break;\n"
3062 "}",
3063 Style);
3064 Style.ColumnLimit = 80;
3065 Style.AllowShortCaseLabelsOnASingleLine = false;
3066 Style.IndentCaseLabels = true;
3067 verifyFormat("switch (n) {\n"
3068 " default /*comments*/:\n"
3069 " return true;\n"
3070 " case 0:\n"
3071 " return false;\n"
3072 "}",
3073 "switch (n) {\n"
3074 "default/*comments*/:\n"
3075 " return true;\n"
3076 "case 0:\n"
3077 " return false;\n"
3078 "}",
3079 Style);
3080 Style.AllowShortCaseLabelsOnASingleLine = true;
3081 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3082 Style.BraceWrapping.AfterCaseLabel = true;
3083 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3084 verifyFormat("switch (n)\n"
3085 "{\n"
3086 " case 0:\n"
3087 " {\n"
3088 " return false;\n"
3089 " }\n"
3090 " default:\n"
3091 " {\n"
3092 " return true;\n"
3093 " }\n"
3094 "}",
3095 "switch (n) {\n"
3096 " case 0: {\n"
3097 " return false;\n"
3098 " }\n"
3099 " default:\n"
3100 " {\n"
3101 " return true;\n"
3102 " }\n"
3103 "}",
3104 Style);
3105 }
3106
3107 TEST_F(FormatTest, FormatsLabels) {
3108 verifyFormat("void f() {\n"
3109 " some_code();\n"
3110 "test_label:\n"
3111 " some_other_code();\n"
3112 " {\n"
3113 " some_more_code();\n"
3114 " another_label:\n"
3115 " some_more_code();\n"
3116 " }\n"
3117 "}");
3118 verifyFormat("{\n"
3119 " some_code();\n"
3120 "test_label:\n"
3121 " some_other_code();\n"
3122 "}");
3123 verifyFormat("{\n"
3124 " some_code();\n"
3125 "test_label:;\n"
3126 " int i = 0;\n"
3127 "}");
3128 verifyFormat("{\n"
3129 " some_code();\n"
3130 "test_label: { some_other_code(); }\n"
3131 "}");
3132 verifyFormat("{\n"
3133 " some_code();\n"
3134 "test_label: {\n"
3135 " some_other_code();\n"
3136 " some_other_code();\n"
3137 "}\n"
3138 "}");
3139 verifyFormat("{\n"
3140 "L0:\n"
3141 "[[foo]] L1:\n"
3142 "[[bar]] [[baz]] L2:\n"
3143 " g();\n"
3144 "}");
3145 verifyFormat("{\n"
3146 "[[foo]] L1: {\n"
3147 "[[bar]] [[baz]] L2:\n"
3148 " g();\n"
3149 "}\n"
3150 "}");
3151 verifyFormat("{\n"
3152 "[[foo]] L1:\n"
3153 " f();\n"
3154 " {\n"
3155 " [[bar]] [[baz]] L2:\n"
3156 " g();\n"
3157 " }\n"
3158 "}");
3159
3160 FormatStyle Style = getLLVMStyle();
3161 Style.IndentGotoLabels = false;
3162 verifyFormat("void f() {\n"
3163 " some_code();\n"
3164 "test_label:\n"
3165 " some_other_code();\n"
3166 " {\n"
3167 " some_more_code();\n"
3168 "another_label:\n"
3169 " some_more_code();\n"
3170 " }\n"
3171 "}",
3172 Style);
3173 verifyFormat("{\n"
3174 " some_code();\n"
3175 "test_label:\n"
3176 " some_other_code();\n"
3177 "}",
3178 Style);
3179 verifyFormat("{\n"
3180 " some_code();\n"
3181 "test_label:;\n"
3182 " int i = 0;\n"
3183 "}",
3184 Style);
3185 verifyFormat("{\n"
3186 " some_code();\n"
3187 "test_label: { some_other_code(); }\n"
3188 "}",
3189 Style);
3190 verifyFormat("{\n"
3191 "[[foo]] L1:\n"
3192 " f();\n"
3193 " {\n"
3194 "[[bar]] [[baz]] L2:\n"
3195 " g();\n"
3196 " }\n"
3197 "}",
3198 Style);
3199
3200 Style.ColumnLimit = 15;
3201 verifyFormat("#define FOO \\\n"
3202 "label: \\\n"
3203 " break;",
3204 Style);
3205
3206 // The opening brace may either be on the same unwrapped line as the colon or
3207 // on a separate one. The formatter should recognize both.
3208 Style = getLLVMStyle();
3209 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3210 verifyFormat("{\n"
3211 " some_code();\n"
3212 "test_label:\n"
3213 "{\n"
3214 " some_other_code();\n"
3215 "}\n"
3216 "}",
3217 Style);
3218 verifyFormat("{\n"
3219 "[[foo]] L1:\n"
3220 "{\n"
3221 "[[bar]] [[baz]] L2:\n"
3222 " g();\n"
3223 "}\n"
3224 "}",
3225 Style);
3226 }
3227
3228 TEST_F(FormatTest, MultiLineControlStatements) {
3229 FormatStyle Style = getLLVMStyleWithColumns(20);
3230 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3231 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3232 // Short lines should keep opening brace on same line.
3233 verifyFormat("if (foo) {\n"
3234 " bar();\n"
3235 "}",
3236 "if(foo){bar();}", Style);
3237 verifyFormat("if (foo) {\n"
3238 " bar();\n"
3239 "} else {\n"
3240 " baz();\n"
3241 "}",
3242 "if(foo){bar();}else{baz();}", Style);
3243 verifyFormat("if (foo && bar) {\n"
3244 " baz();\n"
3245 "}",
3246 "if(foo&&bar){baz();}", Style);
3247 verifyFormat("if (foo) {\n"
3248 " bar();\n"
3249 "} else if (baz) {\n"
3250 " quux();\n"
3251 "}",
3252 "if(foo){bar();}else if(baz){quux();}", Style);
3253 verifyFormat("if (foo) {\n"
3254 " bar();\n"
3255 "} else if (baz) {\n"
3256 " quux();\n"
3257 "} else {\n"
3258 " foobar();\n"
3259 "}",
3260 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3261 verifyFormat("for (;;) {\n"
3262 " foo();\n"
3263 "}",
3264 "for(;;){foo();}");
3265 verifyFormat("while (1) {\n"
3266 " foo();\n"
3267 "}",
3268 "while(1){foo();}", Style);
3269 verifyFormat("switch (foo) {\n"
3270 "case bar:\n"
3271 " return;\n"
3272 "}",
3273 "switch(foo){case bar:return;}", Style);
3274 verifyFormat("try {\n"
3275 " foo();\n"
3276 "} catch (...) {\n"
3277 " bar();\n"
3278 "}",
3279 "try{foo();}catch(...){bar();}", Style);
3280 verifyFormat("do {\n"
3281 " foo();\n"
3282 "} while (bar &&\n"
3283 " baz);",
3284 "do{foo();}while(bar&&baz);", Style);
3285 // Long lines should put opening brace on new line.
3286 verifyFormat("void f() {\n"
3287 " if (a1 && a2 &&\n"
3288 " a3)\n"
3289 " {\n"
3290 " quux();\n"
3291 " }\n"
3292 "}",
3293 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3294 verifyFormat("if (foo && bar &&\n"
3295 " baz)\n"
3296 "{\n"
3297 " quux();\n"
3298 "}",
3299 "if(foo&&bar&&baz){quux();}", Style);
3300 verifyFormat("if (foo && bar &&\n"
3301 " baz)\n"
3302 "{\n"
3303 " quux();\n"
3304 "}",
3305 "if (foo && bar &&\n"
3306 " baz) {\n"
3307 " quux();\n"
3308 "}",
3309 Style);
3310 verifyFormat("if (foo) {\n"
3311 " bar();\n"
3312 "} else if (baz ||\n"
3313 " quux)\n"
3314 "{\n"
3315 " foobar();\n"
3316 "}",
3317 "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3318 verifyFormat("if (foo) {\n"
3319 " bar();\n"
3320 "} else if (baz ||\n"
3321 " quux)\n"
3322 "{\n"
3323 " foobar();\n"
3324 "} else {\n"
3325 " barbaz();\n"
3326 "}",
3327 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3328 Style);
3329 verifyFormat("for (int i = 0;\n"
3330 " i < 10; ++i)\n"
3331 "{\n"
3332 " foo();\n"
3333 "}",
3334 "for(int i=0;i<10;++i){foo();}", Style);
3335 verifyFormat("foreach (int i,\n"
3336 " list)\n"
3337 "{\n"
3338 " foo();\n"
3339 "}",
3340 "foreach(int i, list){foo();}", Style);
3341 Style.ColumnLimit =
3342 40; // to concentrate at brace wrapping, not line wrap due to column limit
3343 verifyFormat("foreach (int i, list) {\n"
3344 " foo();\n"
3345 "}",
3346 "foreach(int i, list){foo();}", Style);
3347 Style.ColumnLimit =
3348 20; // to concentrate at brace wrapping, not line wrap due to column limit
3349 verifyFormat("while (foo || bar ||\n"
3350 " baz)\n"
3351 "{\n"
3352 " quux();\n"
3353 "}",
3354 "while(foo||bar||baz){quux();}", Style);
3355 verifyFormat("switch (\n"
3356 " foo = barbaz)\n"
3357 "{\n"
3358 "case quux:\n"
3359 " return;\n"
3360 "}",
3361 "switch(foo=barbaz){case quux:return;}", Style);
3362 verifyFormat("try {\n"
3363 " foo();\n"
3364 "} catch (\n"
3365 " Exception &bar)\n"
3366 "{\n"
3367 " baz();\n"
3368 "}",
3369 "try{foo();}catch(Exception&bar){baz();}", Style);
3370 Style.ColumnLimit =
3371 40; // to concentrate at brace wrapping, not line wrap due to column limit
3372 verifyFormat("try {\n"
3373 " foo();\n"
3374 "} catch (Exception &bar) {\n"
3375 " baz();\n"
3376 "}",
3377 "try{foo();}catch(Exception&bar){baz();}", Style);
3378 Style.ColumnLimit =
3379 20; // to concentrate at brace wrapping, not line wrap due to column limit
3380
3381 Style.BraceWrapping.BeforeElse = true;
3382 verifyFormat("if (foo) {\n"
3383 " bar();\n"
3384 "}\n"
3385 "else if (baz ||\n"
3386 " quux)\n"
3387 "{\n"
3388 " foobar();\n"
3389 "}\n"
3390 "else {\n"
3391 " barbaz();\n"
3392 "}",
3393 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3394 Style);
3395
3396 Style.BraceWrapping.BeforeCatch = true;
3397 verifyFormat("try {\n"
3398 " foo();\n"
3399 "}\n"
3400 "catch (...) {\n"
3401 " baz();\n"
3402 "}",
3403 "try{foo();}catch(...){baz();}", Style);
3404
3405 Style.BraceWrapping.AfterFunction = true;
3406 Style.BraceWrapping.AfterStruct = false;
3407 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3408 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3409 Style.ColumnLimit = 80;
3410 verifyFormat("void shortfunction() { bar(); }", Style);
3411 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3412 verifyFormat("struct T {};", Style);
3413
3414 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3415 verifyFormat("void shortfunction()\n"
3416 "{\n"
3417 " bar();\n"
3418 "}",
3419 Style);
3420 verifyFormat("struct T shortfunction()\n"
3421 "{\n"
3422 " return bar();\n"
3423 "}",
3424 Style);
3425 verifyFormat("struct T {};", Style);
3426
3427 Style.BraceWrapping.AfterFunction = false;
3428 Style.BraceWrapping.AfterStruct = true;
3429 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3430 verifyFormat("void shortfunction() { bar(); }", Style);
3431 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3432 verifyFormat("struct T\n"
3433 "{\n"
3434 "};",
3435 Style);
3436
3437 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3438 verifyFormat("void shortfunction() {\n"
3439 " bar();\n"
3440 "}",
3441 Style);
3442 verifyFormat("struct T shortfunction() {\n"
3443 " return bar();\n"
3444 "}",
3445 Style);
3446 verifyFormat("struct T\n"
3447 "{\n"
3448 "};",
3449 Style);
3450
3451 Style = getLLVMStyle();
3452 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
3453 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
3454 Style.AllowShortLoopsOnASingleLine = true;
3455 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3456 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3457 verifyFormat("if (true) { return; }", Style);
3458 verifyFormat("while (true) { return; }", Style);
3459 // Failing test in https://reviews.llvm.org/D114521#3151727
3460 verifyFormat("for (;;) { bar(); }", Style);
3461 }
3462
3463 TEST_F(FormatTest, BeforeWhile) {
3464 FormatStyle Style = getLLVMStyle();
3465 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3466
3467 verifyFormat("do {\n"
3468 " foo();\n"
3469 "} while (1);",
3470 Style);
3471 Style.BraceWrapping.BeforeWhile = true;
3472 verifyFormat("do {\n"
3473 " foo();\n"
3474 "}\n"
3475 "while (1);",
3476 Style);
3477 }
3478
3479 //===----------------------------------------------------------------------===//
3480 // Tests for classes, namespaces, etc.
3481 //===----------------------------------------------------------------------===//
3482
3483 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3484 verifyFormat("class A {};");
3485 }
3486
3487 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3488 verifyFormat("class A {\n"
3489 "public:\n"
3490 "public: // comment\n"
3491 "protected:\n"
3492 "private:\n"
3493 " void f() {}\n"
3494 "};");
3495 verifyFormat("export class A {\n"
3496 "public:\n"
3497 "public: // comment\n"
3498 "protected:\n"
3499 "private:\n"
3500 " void f() {}\n"
3501 "};");
3502 verifyGoogleFormat("class A {\n"
3503 " public:\n"
3504 " protected:\n"
3505 " private:\n"
3506 " void f() {}\n"
3507 "};");
3508 verifyGoogleFormat("export class A {\n"
3509 " public:\n"
3510 " protected:\n"
3511 " private:\n"
3512 " void f() {}\n"
3513 "};");
3514 verifyFormat("class A {\n"
3515 "public slots:\n"
3516 " void f1() {}\n"
3517 "public Q_SLOTS:\n"
3518 " void f2() {}\n"
3519 "protected slots:\n"
3520 " void f3() {}\n"
3521 "protected Q_SLOTS:\n"
3522 " void f4() {}\n"
3523 "private slots:\n"
3524 " void f5() {}\n"
3525 "private Q_SLOTS:\n"
3526 " void f6() {}\n"
3527 "signals:\n"
3528 " void g1();\n"
3529 "Q_SIGNALS:\n"
3530 " void g2();\n"
3531 "};");
3532
3533 // Don't interpret 'signals' the wrong way.
3534 verifyFormat("signals.set();");
3535 verifyFormat("for (Signals signals : f()) {\n}");
3536 verifyFormat("{\n"
3537 " signals.set(); // This needs indentation.\n"
3538 "}");
3539 verifyFormat("void f() {\n"
3540 "label:\n"
3541 " signals.baz();\n"
3542 "}");
3543
3544 const auto Style = getLLVMStyle(FormatStyle::LK_C);
3545 verifyFormat("private[1];", Style);
3546 verifyFormat("testArray[public] = 1;");
3547 verifyFormat("public();", Style);
3548 verifyFormat("myFunc(public);");
3549 verifyFormat("std::vector<int> testVec = {private};");
3550 verifyFormat("private.p = 1;", Style);
3551 verifyFormat("void function(private...) {};");
3552 verifyFormat("if (private && public)");
3553 verifyFormat("private &= true;", Style);
3554 verifyFormat("int x = private * public;");
3555 verifyFormat("public *= private;", Style);
3556 verifyFormat("int x = public + private;");
3557 verifyFormat("private++;", Style);
3558 verifyFormat("++private;");
3559 verifyFormat("public += private;", Style);
3560 verifyFormat("public = public - private;", Style);
3561 verifyFormat("public->foo();", Style);
3562 verifyFormat("private--;", Style);
3563 verifyFormat("--private;");
3564 verifyFormat("public -= 1;", Style);
3565 verifyFormat("if (!private && !public)");
3566 verifyFormat("public != private;", Style);
3567 verifyFormat("int x = public / private;");
3568 verifyFormat("public /= 2;", Style);
3569 verifyFormat("public = public % 2;", Style);
3570 verifyFormat("public %= 2;", Style);
3571 verifyFormat("if (public < private)");
3572 verifyFormat("public << private;", Style);
3573 verifyFormat("public <<= private;", Style);
3574 verifyFormat("if (public > private)");
3575 verifyFormat("public >> private;", Style);
3576 verifyFormat("public >>= private;", Style);
3577 verifyFormat("public ^ private;", Style);
3578 verifyFormat("public ^= private;", Style);
3579 verifyFormat("public | private;", Style);
3580 verifyFormat("public |= private;", Style);
3581 verifyFormat("auto x = private ? 1 : 2;");
3582 verifyFormat("if (public == private)");
3583 verifyFormat("void foo(public, private)");
3584
3585 verifyFormat("class A {\n"
3586 "public:\n"
3587 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3588 "\n"
3589 "private:\n"
3590 " int c;\n"
3591 "};\n"
3592 "class B {\n"
3593 "public:\n"
3594 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3595 "\n"
3596 "private:\n"
3597 " int c;\n"
3598 "};");
3599 }
3600
3601 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3602 verifyFormat("class A {\n"
3603 "public:\n"
3604 " void f();\n"
3605 "\n"
3606 "private:\n"
3607 " void g() {}\n"
3608 " // test\n"
3609 "protected:\n"
3610 " int h;\n"
3611 "};",
3612 "class A {\n"
3613 "public:\n"
3614 "void f();\n"
3615 "private:\n"
3616 "void g() {}\n"
3617 "// test\n"
3618 "protected:\n"
3619 "int h;\n"
3620 "};");
3621 verifyFormat("class A {\n"
3622 "protected:\n"
3623 "public:\n"
3624 " void f();\n"
3625 "};",
3626 "class A {\n"
3627 "protected:\n"
3628 "\n"
3629 "public:\n"
3630 "\n"
3631 " void f();\n"
3632 "};");
3633
3634 // Even ensure proper spacing inside macros.
3635 verifyFormat("#define B \\\n"
3636 " class A { \\\n"
3637 " protected: \\\n"
3638 " public: \\\n"
3639 " void f(); \\\n"
3640 " };",
3641 "#define B \\\n"
3642 " class A { \\\n"
3643 " protected: \\\n"
3644 " \\\n"
3645 " public: \\\n"
3646 " \\\n"
3647 " void f(); \\\n"
3648 " };",
3649 getGoogleStyle());
3650 // But don't remove empty lines after macros ending in access specifiers.
3651 verifyFormat("#define A private:\n"
3652 "\n"
3653 "int i;",
3654 "#define A private:\n"
3655 "\n"
3656 "int i;");
3657 }
3658
3659 TEST_F(FormatTest, FormatsClasses) {
3660 verifyFormat("class A : public B {};");
3661 verifyFormat("class A : public ::B {};");
3662
3663 verifyFormat(
3664 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3665 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3666 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3667 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3668 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3669 verifyFormat(
3670 "class A : public B, public C, public D, public E, public F {};");
3671 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3672 " public C,\n"
3673 " public D,\n"
3674 " public E,\n"
3675 " public F,\n"
3676 " public G {};");
3677
3678 verifyFormat("class\n"
3679 " ReallyReallyLongClassName {\n"
3680 " int i;\n"
3681 "};",
3682 getLLVMStyleWithColumns(32));
3683 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3684 " aaaaaaaaaaaaaaaa> {};");
3685 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3686 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3687 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3688 verifyFormat("template <class R, class C>\n"
3689 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3690 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3691 verifyFormat("class ::A::B {};");
3692 }
3693
3694 TEST_F(FormatTest, BreakInheritanceStyle) {
3695 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3696 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3697 FormatStyle::BILS_BeforeComma;
3698 verifyFormat("class MyClass : public X {};",
3699 StyleWithInheritanceBreakBeforeComma);
3700 verifyFormat("class MyClass\n"
3701 " : public X\n"
3702 " , public Y {};",
3703 StyleWithInheritanceBreakBeforeComma);
3704 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3705 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3706 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3707 StyleWithInheritanceBreakBeforeComma);
3708 verifyFormat("struct aaaaaaaaaaaaa\n"
3709 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3710 " aaaaaaaaaaaaaaaa> {};",
3711 StyleWithInheritanceBreakBeforeComma);
3712
3713 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3714 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3715 FormatStyle::BILS_AfterColon;
3716 verifyFormat("class MyClass : public X {};",
3717 StyleWithInheritanceBreakAfterColon);
3718 verifyFormat("class MyClass : public X, public Y {};",
3719 StyleWithInheritanceBreakAfterColon);
3720 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3721 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3722 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3723 StyleWithInheritanceBreakAfterColon);
3724 verifyFormat("struct aaaaaaaaaaaaa :\n"
3725 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3726 " aaaaaaaaaaaaaaaa> {};",
3727 StyleWithInheritanceBreakAfterColon);
3728
3729 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3730 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3731 FormatStyle::BILS_AfterComma;
3732 verifyFormat("class MyClass : public X {};",
3733 StyleWithInheritanceBreakAfterComma);
3734 verifyFormat("class MyClass : public X,\n"
3735 " public Y {};",
3736 StyleWithInheritanceBreakAfterComma);
3737 verifyFormat(
3738 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3739 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3740 "{};",
3741 StyleWithInheritanceBreakAfterComma);
3742 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3743 " aaaaaaaaaaaaaaaa> {};",
3744 StyleWithInheritanceBreakAfterComma);
3745 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3746 " : public OnceBreak,\n"
3747 " public AlwaysBreak,\n"
3748 " EvenBasesFitInOneLine {};",
3749 StyleWithInheritanceBreakAfterComma);
3750 }
3751
3752 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3753 verifyFormat("class A {\n} a, b;");
3754 verifyFormat("struct A {\n} a, b;");
3755 verifyFormat("union A {\n} a, b;");
3756
3757 verifyFormat("constexpr class A {\n} a, b;");
3758 verifyFormat("constexpr struct A {\n} a, b;");
3759 verifyFormat("constexpr union A {\n} a, b;");
3760
3761 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3762 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3763 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3764
3765 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3766 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3767 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3768
3769 verifyFormat("namespace ns {\n"
3770 "class {\n"
3771 "} a, b;\n"
3772 "} // namespace ns");
3773 verifyFormat("namespace ns {\n"
3774 "const class {\n"
3775 "} a, b;\n"
3776 "} // namespace ns");
3777 verifyFormat("namespace ns {\n"
3778 "constexpr class C {\n"
3779 "} a, b;\n"
3780 "} // namespace ns");
3781 verifyFormat("namespace ns {\n"
3782 "class { /* comment */\n"
3783 "} a, b;\n"
3784 "} // namespace ns");
3785 verifyFormat("namespace ns {\n"
3786 "const class { /* comment */\n"
3787 "} a, b;\n"
3788 "} // namespace ns");
3789 }
3790
3791 TEST_F(FormatTest, FormatsEnum) {
3792 verifyFormat("enum {\n"
3793 " Zero,\n"
3794 " One = 1,\n"
3795 " Two = One + 1,\n"
3796 " Three = (One + Two),\n"
3797 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3798 " Five = (One, Two, Three, Four, 5)\n"
3799 "};");
3800 verifyGoogleFormat("enum {\n"
3801 " Zero,\n"
3802 " One = 1,\n"
3803 " Two = One + 1,\n"
3804 " Three = (One + Two),\n"
3805 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3806 " Five = (One, Two, Three, Four, 5)\n"
3807 "};");
3808 verifyFormat("enum Enum {};");
3809 verifyFormat("enum {};");
3810 verifyFormat("enum X E {} d;");
3811 verifyFormat("enum __attribute__((...)) E {} d;");
3812 verifyFormat("enum __declspec__((...)) E {} d;");
3813 verifyFormat("enum [[nodiscard]] E {} d;");
3814 verifyFormat("enum {\n"
3815 " Bar = Foo<int, int>::value\n"
3816 "};",
3817 getLLVMStyleWithColumns(30));
3818
3819 verifyFormat("enum ShortEnum { A, B, C };");
3820 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3821
3822 verifyFormat("enum KeepEmptyLines {\n"
3823 " ONE,\n"
3824 "\n"
3825 " TWO,\n"
3826 "\n"
3827 " THREE\n"
3828 "}",
3829 "enum KeepEmptyLines {\n"
3830 " ONE,\n"
3831 "\n"
3832 " TWO,\n"
3833 "\n"
3834 "\n"
3835 " THREE\n"
3836 "}");
3837 verifyFormat("enum E { // comment\n"
3838 " ONE,\n"
3839 " TWO\n"
3840 "};\n"
3841 "int i;");
3842
3843 FormatStyle EightIndent = getLLVMStyle();
3844 EightIndent.IndentWidth = 8;
3845 verifyFormat("enum {\n"
3846 " VOID,\n"
3847 " CHAR,\n"
3848 " SHORT,\n"
3849 " INT,\n"
3850 " LONG,\n"
3851 " SIGNED,\n"
3852 " UNSIGNED,\n"
3853 " BOOL,\n"
3854 " FLOAT,\n"
3855 " DOUBLE,\n"
3856 " COMPLEX\n"
3857 "};",
3858 EightIndent);
3859
3860 verifyFormat("enum [[nodiscard]] E {\n"
3861 " ONE,\n"
3862 " TWO,\n"
3863 "};");
3864 verifyFormat("enum [[nodiscard]] E {\n"
3865 " // Comment 1\n"
3866 " ONE,\n"
3867 " // Comment 2\n"
3868 " TWO,\n"
3869 "};");
3870 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3871 " // Comment 1\n"
3872 " ONE,\n"
3873 " // Comment 2\n"
3874 " TWO\n"
3875 "};");
3876 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3877 " // Comment 1\n"
3878 " ONE,\n"
3879 " // Comment 2\n"
3880 " TWO\n"
3881 "};");
3882 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3883 " A,\n"
3884 " // bar\n"
3885 " B\n"
3886 "};",
3887 "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3888 " A,\n"
3889 " // bar\n"
3890 " B};");
3891
3892 // Not enums.
3893 verifyFormat("enum X f() {\n"
3894 " a();\n"
3895 " return 42;\n"
3896 "}");
3897 verifyFormat("enum X Type::f() {\n"
3898 " a();\n"
3899 " return 42;\n"
3900 "}");
3901 verifyFormat("enum ::X f() {\n"
3902 " a();\n"
3903 " return 42;\n"
3904 "}");
3905 verifyFormat("enum ns::X f() {\n"
3906 " a();\n"
3907 " return 42;\n"
3908 "}");
3909 }
3910
3911 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3912 verifyFormat("enum Type {\n"
3913 " One = 0; // These semicolons should be commas.\n"
3914 " Two = 1;\n"
3915 "};");
3916 verifyFormat("namespace n {\n"
3917 "enum Type {\n"
3918 " One,\n"
3919 " Two, // missing };\n"
3920 " int i;\n"
3921 "}\n"
3922 "void g() {}");
3923 }
3924
3925 TEST_F(FormatTest, FormatsEnumStruct) {
3926 verifyFormat("enum struct {\n"
3927 " Zero,\n"
3928 " One = 1,\n"
3929 " Two = One + 1,\n"
3930 " Three = (One + Two),\n"
3931 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3932 " Five = (One, Two, Three, Four, 5)\n"
3933 "};");
3934 verifyFormat("enum struct Enum {};");
3935 verifyFormat("enum struct {};");
3936 verifyFormat("enum struct X E {} d;");
3937 verifyFormat("enum struct __attribute__((...)) E {} d;");
3938 verifyFormat("enum struct __declspec__((...)) E {} d;");
3939 verifyFormat("enum struct [[nodiscard]] E {} d;");
3940 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3941
3942 verifyFormat("enum struct [[nodiscard]] E {\n"
3943 " ONE,\n"
3944 " TWO,\n"
3945 "};");
3946 verifyFormat("enum struct [[nodiscard]] E {\n"
3947 " // Comment 1\n"
3948 " ONE,\n"
3949 " // Comment 2\n"
3950 " TWO,\n"
3951 "};");
3952 }
3953
3954 TEST_F(FormatTest, FormatsEnumClass) {
3955 verifyFormat("enum class {\n"
3956 " Zero,\n"
3957 " One = 1,\n"
3958 " Two = One + 1,\n"
3959 " Three = (One + Two),\n"
3960 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3961 " Five = (One, Two, Three, Four, 5)\n"
3962 "};");
3963 verifyFormat("enum class Enum {};");
3964 verifyFormat("enum class {};");
3965 verifyFormat("enum class X E {} d;");
3966 verifyFormat("enum class __attribute__((...)) E {} d;");
3967 verifyFormat("enum class __declspec__((...)) E {} d;");
3968 verifyFormat("enum class [[nodiscard]] E {} d;");
3969 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3970
3971 verifyFormat("enum class [[nodiscard]] E {\n"
3972 " ONE,\n"
3973 " TWO,\n"
3974 "};");
3975 verifyFormat("enum class [[nodiscard]] E {\n"
3976 " // Comment 1\n"
3977 " ONE,\n"
3978 " // Comment 2\n"
3979 " TWO,\n"
3980 "};");
3981 }
3982
3983 TEST_F(FormatTest, FormatsEnumTypes) {
3984 verifyFormat("enum X : int {\n"
3985 " A, // Force multiple lines.\n"
3986 " B\n"
3987 "};");
3988 verifyFormat("enum X : int { A, B };");
3989 verifyFormat("enum X : std::uint32_t { A, B };");
3990 }
3991
3992 TEST_F(FormatTest, FormatsTypedefEnum) {
3993 FormatStyle Style = getLLVMStyleWithColumns(40);
3994 verifyFormat("typedef enum {} EmptyEnum;");
3995 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3996 verifyFormat("typedef enum {\n"
3997 " ZERO = 0,\n"
3998 " ONE = 1,\n"
3999 " TWO = 2,\n"
4000 " THREE = 3\n"
4001 "} LongEnum;",
4002 Style);
4003 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4004 Style.BraceWrapping.AfterEnum = true;
4005 verifyFormat("typedef enum {} EmptyEnum;");
4006 verifyFormat("typedef enum { A, B, C } ShortEnum;");
4007 verifyFormat("typedef enum\n"
4008 "{\n"
4009 " ZERO = 0,\n"
4010 " ONE = 1,\n"
4011 " TWO = 2,\n"
4012 " THREE = 3\n"
4013 "} LongEnum;",
4014 Style);
4015 }
4016
4017 TEST_F(FormatTest, FormatsNSEnums) {
4018 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
4019 verifyGoogleFormat(
4020 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
4021 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
4022 " // Information about someDecentlyLongValue.\n"
4023 " someDecentlyLongValue,\n"
4024 " // Information about anotherDecentlyLongValue.\n"
4025 " anotherDecentlyLongValue,\n"
4026 " // Information about aThirdDecentlyLongValue.\n"
4027 " aThirdDecentlyLongValue\n"
4028 "};");
4029 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
4030 " // Information about someDecentlyLongValue.\n"
4031 " someDecentlyLongValue,\n"
4032 " // Information about anotherDecentlyLongValue.\n"
4033 " anotherDecentlyLongValue,\n"
4034 " // Information about aThirdDecentlyLongValue.\n"
4035 " aThirdDecentlyLongValue\n"
4036 "};");
4037 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
4038 " a = 1,\n"
4039 " b = 2,\n"
4040 " c = 3,\n"
4041 "};");
4042 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
4043 " a = 1,\n"
4044 " b = 2,\n"
4045 " c = 3,\n"
4046 "};");
4047 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
4048 " a = 1,\n"
4049 " b = 2,\n"
4050 " c = 3,\n"
4051 "};");
4052 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4053 " a = 1,\n"
4054 " b = 2,\n"
4055 " c = 3,\n"
4056 "};");
4057 }
4058
4059 TEST_F(FormatTest, FormatsBitfields) {
4060 verifyFormat("struct Bitfields {\n"
4061 " unsigned sClass : 8;\n"
4062 " unsigned ValueKind : 2;\n"
4063 "};");
4064 verifyFormat("struct A {\n"
4065 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4066 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4067 "};");
4068 verifyFormat("struct MyStruct {\n"
4069 " uchar data;\n"
4070 " uchar : 8;\n"
4071 " uchar : 8;\n"
4072 " uchar other;\n"
4073 "};");
4074 verifyFormat("struct foo {\n"
4075 " uint8_t i_am_a_bit_field_this_long\n"
4076 " : struct_with_constexpr::i_am_a_constexpr_lengthhhhh;\n"
4077 "};");
4078 FormatStyle Style = getLLVMStyle();
4079 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
4080 verifyFormat("struct Bitfields {\n"
4081 " unsigned sClass:8;\n"
4082 " unsigned ValueKind:2;\n"
4083 " uchar other;\n"
4084 "};",
4085 Style);
4086 verifyFormat("struct A {\n"
4087 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4088 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4089 "};",
4090 Style);
4091 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
4092 verifyFormat("struct Bitfields {\n"
4093 " unsigned sClass :8;\n"
4094 " unsigned ValueKind :2;\n"
4095 " uchar other;\n"
4096 "};",
4097 Style);
4098 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
4099 verifyFormat("struct Bitfields {\n"
4100 " unsigned sClass: 8;\n"
4101 " unsigned ValueKind: 2;\n"
4102 " uchar other;\n"
4103 "};",
4104 Style);
4105 }
4106
4107 TEST_F(FormatTest, FormatsNamespaces) {
4108 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
4109 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
4110
4111 verifyFormat("namespace some_namespace {\n"
4112 "class A {};\n"
4113 "void f() { f(); }\n"
4114 "}",
4115 LLVMWithNoNamespaceFix);
4116 verifyFormat("#define M(x) x##x\n"
4117 "namespace M(x) {\n"
4118 "class A {};\n"
4119 "void f() { f(); }\n"
4120 "}",
4121 LLVMWithNoNamespaceFix);
4122 verifyFormat("#define M(x) x##x\n"
4123 "namespace N::inline M(x) {\n"
4124 "class A {};\n"
4125 "void f() { f(); }\n"
4126 "}",
4127 LLVMWithNoNamespaceFix);
4128 verifyFormat("#define M(x) x##x\n"
4129 "namespace M(x)::inline N {\n"
4130 "class A {};\n"
4131 "void f() { f(); }\n"
4132 "}",
4133 LLVMWithNoNamespaceFix);
4134 verifyFormat("#define M(x) x##x\n"
4135 "namespace N::M(x) {\n"
4136 "class A {};\n"
4137 "void f() { f(); }\n"
4138 "}",
4139 LLVMWithNoNamespaceFix);
4140 verifyFormat("#define M(x) x##x\n"
4141 "namespace M::N(x) {\n"
4142 "class A {};\n"
4143 "void f() { f(); }\n"
4144 "}",
4145 LLVMWithNoNamespaceFix);
4146 verifyFormat("namespace N::inline D {\n"
4147 "class A {};\n"
4148 "void f() { f(); }\n"
4149 "}",
4150 LLVMWithNoNamespaceFix);
4151 verifyFormat("namespace N::inline D::E {\n"
4152 "class A {};\n"
4153 "void f() { f(); }\n"
4154 "}",
4155 LLVMWithNoNamespaceFix);
4156 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4157 "class A {};\n"
4158 "void f() { f(); }\n"
4159 "}",
4160 LLVMWithNoNamespaceFix);
4161 verifyFormat("/* something */ namespace some_namespace {\n"
4162 "class A {};\n"
4163 "void f() { f(); }\n"
4164 "}",
4165 LLVMWithNoNamespaceFix);
4166 verifyFormat("namespace {\n"
4167 "class A {};\n"
4168 "void f() { f(); }\n"
4169 "}",
4170 LLVMWithNoNamespaceFix);
4171 verifyFormat("/* something */ namespace {\n"
4172 "class A {};\n"
4173 "void f() { f(); }\n"
4174 "}",
4175 LLVMWithNoNamespaceFix);
4176 verifyFormat("inline namespace X {\n"
4177 "class A {};\n"
4178 "void f() { f(); }\n"
4179 "}",
4180 LLVMWithNoNamespaceFix);
4181 verifyFormat("/* something */ inline namespace X {\n"
4182 "class A {};\n"
4183 "void f() { f(); }\n"
4184 "}",
4185 LLVMWithNoNamespaceFix);
4186 verifyFormat("export namespace X {\n"
4187 "class A {};\n"
4188 "void f() { f(); }\n"
4189 "}",
4190 LLVMWithNoNamespaceFix);
4191 verifyFormat("using namespace some_namespace;\n"
4192 "class A {};\n"
4193 "void f() { f(); }",
4194 LLVMWithNoNamespaceFix);
4195
4196 // This code is more common than we thought; if we
4197 // layout this correctly the semicolon will go into
4198 // its own line, which is undesirable.
4199 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4200 verifyFormat("namespace {\n"
4201 "class A {};\n"
4202 "};",
4203 LLVMWithNoNamespaceFix);
4204
4205 verifyFormat("namespace {\n"
4206 "int SomeVariable = 0; // comment\n"
4207 "} // namespace",
4208 LLVMWithNoNamespaceFix);
4209 verifyFormat("#ifndef HEADER_GUARD\n"
4210 "#define HEADER_GUARD\n"
4211 "namespace my_namespace {\n"
4212 "int i;\n"
4213 "} // my_namespace\n"
4214 "#endif // HEADER_GUARD",
4215 "#ifndef HEADER_GUARD\n"
4216 " #define HEADER_GUARD\n"
4217 " namespace my_namespace {\n"
4218 "int i;\n"
4219 "} // my_namespace\n"
4220 "#endif // HEADER_GUARD",
4221 LLVMWithNoNamespaceFix);
4222
4223 verifyFormat("namespace A::B {\n"
4224 "class C {};\n"
4225 "}",
4226 LLVMWithNoNamespaceFix);
4227
4228 FormatStyle Style = getLLVMStyle();
4229 Style.NamespaceIndentation = FormatStyle::NI_All;
4230 verifyFormat("namespace out {\n"
4231 " int i;\n"
4232 " namespace in {\n"
4233 " int i;\n"
4234 " } // namespace in\n"
4235 "} // namespace out",
4236 "namespace out {\n"
4237 "int i;\n"
4238 "namespace in {\n"
4239 "int i;\n"
4240 "} // namespace in\n"
4241 "} // namespace out",
4242 Style);
4243
4244 FormatStyle ShortInlineFunctions = getLLVMStyle();
4245 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4246 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4247 FormatStyle::SFS_Inline;
4248 verifyFormat("namespace {\n"
4249 " void f() {\n"
4250 " return;\n"
4251 " }\n"
4252 "} // namespace",
4253 ShortInlineFunctions);
4254 verifyFormat("namespace { /* comment */\n"
4255 " void f() {\n"
4256 " return;\n"
4257 " }\n"
4258 "} // namespace",
4259 ShortInlineFunctions);
4260 verifyFormat("namespace { // comment\n"
4261 " void f() {\n"
4262 " return;\n"
4263 " }\n"
4264 "} // namespace",
4265 ShortInlineFunctions);
4266 verifyFormat("namespace {\n"
4267 " int some_int;\n"
4268 " void f() {\n"
4269 " return;\n"
4270 " }\n"
4271 "} // namespace",
4272 ShortInlineFunctions);
4273 verifyFormat("namespace interface {\n"
4274 " void f() {\n"
4275 " return;\n"
4276 " }\n"
4277 "} // namespace interface",
4278 ShortInlineFunctions);
4279 verifyFormat("namespace {\n"
4280 " class X {\n"
4281 " void f() { return; }\n"
4282 " };\n"
4283 "} // namespace",
4284 ShortInlineFunctions);
4285 verifyFormat("namespace {\n"
4286 " class X { /* comment */\n"
4287 " void f() { return; }\n"
4288 " };\n"
4289 "} // namespace",
4290 ShortInlineFunctions);
4291 verifyFormat("namespace {\n"
4292 " class X { // comment\n"
4293 " void f() { return; }\n"
4294 " };\n"
4295 "} // namespace",
4296 ShortInlineFunctions);
4297 verifyFormat("namespace {\n"
4298 " struct X {\n"
4299 " void f() { return; }\n"
4300 " };\n"
4301 "} // namespace",
4302 ShortInlineFunctions);
4303 verifyFormat("namespace {\n"
4304 " union X {\n"
4305 " void f() { return; }\n"
4306 " };\n"
4307 "} // namespace",
4308 ShortInlineFunctions);
4309 verifyFormat("extern \"C\" {\n"
4310 "void f() {\n"
4311 " return;\n"
4312 "}\n"
4313 "} // namespace",
4314 ShortInlineFunctions);
4315 verifyFormat("namespace {\n"
4316 " class X {\n"
4317 " void f() { return; }\n"
4318 " } x;\n"
4319 "} // namespace",
4320 ShortInlineFunctions);
4321 verifyFormat("namespace {\n"
4322 " [[nodiscard]] class X {\n"
4323 " void f() { return; }\n"
4324 " };\n"
4325 "} // namespace",
4326 ShortInlineFunctions);
4327 verifyFormat("namespace {\n"
4328 " static class X {\n"
4329 " void f() { return; }\n"
4330 " } x;\n"
4331 "} // namespace",
4332 ShortInlineFunctions);
4333 verifyFormat("namespace {\n"
4334 " constexpr class X {\n"
4335 " void f() { return; }\n"
4336 " } x;\n"
4337 "} // namespace",
4338 ShortInlineFunctions);
4339
4340 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4341 verifyFormat("extern \"C\" {\n"
4342 " void f() {\n"
4343 " return;\n"
4344 " }\n"
4345 "} // namespace",
4346 ShortInlineFunctions);
4347
4348 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4349 verifyFormat("namespace out {\n"
4350 "int i;\n"
4351 "namespace in {\n"
4352 " int i;\n"
4353 "} // namespace in\n"
4354 "} // namespace out",
4355 "namespace out {\n"
4356 "int i;\n"
4357 "namespace in {\n"
4358 "int i;\n"
4359 "} // namespace in\n"
4360 "} // namespace out",
4361 Style);
4362
4363 Style.NamespaceIndentation = FormatStyle::NI_None;
4364 verifyFormat("template <class T>\n"
4365 "concept a_concept = X<>;\n"
4366 "namespace B {\n"
4367 "struct b_struct {};\n"
4368 "} // namespace B",
4369 Style);
4370 verifyFormat("template <int I>\n"
4371 "constexpr void foo()\n"
4372 " requires(I == 42)\n"
4373 "{}\n"
4374 "namespace ns {\n"
4375 "void foo() {}\n"
4376 "} // namespace ns",
4377 Style);
4378
4379 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4380 LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4381 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4382 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4383 "// block for debug mode\n"
4384 "#ifndef NDEBUG\n"
4385 "#endif\n"
4386 "}}} // namespace ns1::ns2::ns3",
4387 LLVMWithCompactInnerNamespace);
4388 }
4389
4390 TEST_F(FormatTest, NamespaceMacros) {
4391 FormatStyle Style = getLLVMStyle();
4392 Style.NamespaceMacros.push_back("TESTSUITE");
4393
4394 verifyFormat("TESTSUITE(A) {\n"
4395 "int foo();\n"
4396 "} // TESTSUITE(A)",
4397 Style);
4398
4399 verifyFormat("TESTSUITE(A, B) {\n"
4400 "int foo();\n"
4401 "} // TESTSUITE(A)",
4402 Style);
4403
4404 // Properly indent according to NamespaceIndentation style
4405 Style.NamespaceIndentation = FormatStyle::NI_All;
4406 verifyFormat("TESTSUITE(A) {\n"
4407 " int foo();\n"
4408 "} // TESTSUITE(A)",
4409 Style);
4410 verifyFormat("TESTSUITE(A) {\n"
4411 " namespace B {\n"
4412 " int foo();\n"
4413 " } // namespace B\n"
4414 "} // TESTSUITE(A)",
4415 Style);
4416 verifyFormat("namespace A {\n"
4417 " TESTSUITE(B) {\n"
4418 " int foo();\n"
4419 " } // TESTSUITE(B)\n"
4420 "} // namespace A",
4421 Style);
4422
4423 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4424 verifyFormat("TESTSUITE(A) {\n"
4425 "TESTSUITE(B) {\n"
4426 " int foo();\n"
4427 "} // TESTSUITE(B)\n"
4428 "} // TESTSUITE(A)",
4429 Style);
4430 verifyFormat("TESTSUITE(A) {\n"
4431 "namespace B {\n"
4432 " int foo();\n"
4433 "} // namespace B\n"
4434 "} // TESTSUITE(A)",
4435 Style);
4436 verifyFormat("namespace A {\n"
4437 "TESTSUITE(B) {\n"
4438 " int foo();\n"
4439 "} // TESTSUITE(B)\n"
4440 "} // namespace A",
4441 Style);
4442
4443 // Properly merge namespace-macros blocks in CompactNamespaces mode
4444 Style.NamespaceIndentation = FormatStyle::NI_None;
4445 Style.CompactNamespaces = true;
4446 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4447 "}} // TESTSUITE(A::B)",
4448 Style);
4449
4450 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4451 "}} // TESTSUITE(out::in)",
4452 "TESTSUITE(out) {\n"
4453 "TESTSUITE(in) {\n"
4454 "} // TESTSUITE(in)\n"
4455 "} // TESTSUITE(out)",
4456 Style);
4457
4458 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4459 "}} // TESTSUITE(out::in)",
4460 "TESTSUITE(out) {\n"
4461 "TESTSUITE(in) {\n"
4462 "} // TESTSUITE(in)\n"
4463 "} // TESTSUITE(out)",
4464 Style);
4465
4466 // Do not merge different namespaces/macros
4467 verifyFormat("namespace out {\n"
4468 "TESTSUITE(in) {\n"
4469 "} // TESTSUITE(in)\n"
4470 "} // namespace out",
4471 Style);
4472 verifyFormat("TESTSUITE(out) {\n"
4473 "namespace in {\n"
4474 "} // namespace in\n"
4475 "} // TESTSUITE(out)",
4476 Style);
4477 Style.NamespaceMacros.push_back("FOOBAR");
4478 verifyFormat("TESTSUITE(out) {\n"
4479 "FOOBAR(in) {\n"
4480 "} // FOOBAR(in)\n"
4481 "} // TESTSUITE(out)",
4482 Style);
4483 }
4484
4485 TEST_F(FormatTest, FormatsCompactNamespaces) {
4486 FormatStyle Style = getLLVMStyle();
4487 Style.CompactNamespaces = true;
4488 Style.NamespaceMacros.push_back("TESTSUITE");
4489
4490 verifyFormat("namespace A { namespace B {\n"
4491 "}} // namespace A::B",
4492 Style);
4493
4494 verifyFormat("namespace out { namespace in {\n"
4495 "}} // namespace out::in",
4496 "namespace out {\n"
4497 "namespace in {\n"
4498 "} // namespace in\n"
4499 "} // namespace out",
4500 Style);
4501
4502 // Only namespaces which have both consecutive opening and end get compacted
4503 verifyFormat("namespace out {\n"
4504 "namespace in1 {\n"
4505 "} // namespace in1\n"
4506 "namespace in2 {\n"
4507 "} // namespace in2\n"
4508 "} // namespace out",
4509 Style);
4510
4511 verifyFormat("namespace out {\n"
4512 "int i;\n"
4513 "namespace in {\n"
4514 "int j;\n"
4515 "} // namespace in\n"
4516 "int k;\n"
4517 "} // namespace out",
4518 "namespace out { int i;\n"
4519 "namespace in { int j; } // namespace in\n"
4520 "int k; } // namespace out",
4521 Style);
4522
4523 Style.ColumnLimit = 41;
4524 verifyFormat("namespace A { namespace B { namespace C {\n"
4525 "}}} // namespace A::B::C",
4526 "namespace A { namespace B {\n"
4527 "namespace C {\n"
4528 "}} // namespace B::C\n"
4529 "} // namespace A",
4530 Style);
4531
4532 Style.ColumnLimit = 40;
4533 verifyFormat("namespace aaaaaaaaaa {\n"
4534 "namespace bbbbbbbbbb {\n"
4535 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4536 "namespace aaaaaaaaaa {\n"
4537 "namespace bbbbbbbbbb {\n"
4538 "} // namespace bbbbbbbbbb\n"
4539 "} // namespace aaaaaaaaaa",
4540 Style);
4541
4542 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4543 "namespace cccccc {\n"
4544 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4545 "namespace aaaaaa {\n"
4546 "namespace bbbbbb {\n"
4547 "namespace cccccc {\n"
4548 "} // namespace cccccc\n"
4549 "} // namespace bbbbbb\n"
4550 "} // namespace aaaaaa",
4551 Style);
4552
4553 verifyFormat("namespace a { namespace b {\n"
4554 "namespace c {\n"
4555 "}}} // namespace a::b::c",
4556 Style);
4557
4558 Style.ColumnLimit = 80;
4559
4560 // Extra semicolon after 'inner' closing brace prevents merging
4561 verifyFormat("namespace out { namespace in {\n"
4562 "}; } // namespace out::in",
4563 "namespace out {\n"
4564 "namespace in {\n"
4565 "}; // namespace in\n"
4566 "} // namespace out",
4567 Style);
4568
4569 // Extra semicolon after 'outer' closing brace is conserved
4570 verifyFormat("namespace out { namespace in {\n"
4571 "}}; // namespace out::in",
4572 "namespace out {\n"
4573 "namespace in {\n"
4574 "} // namespace in\n"
4575 "}; // namespace out",
4576 Style);
4577
4578 Style.NamespaceIndentation = FormatStyle::NI_All;
4579 verifyFormat("namespace out { namespace in {\n"
4580 " int i;\n"
4581 "}} // namespace out::in",
4582 "namespace out {\n"
4583 "namespace in {\n"
4584 "int i;\n"
4585 "} // namespace in\n"
4586 "} // namespace out",
4587 Style);
4588 verifyFormat("namespace out { namespace mid {\n"
4589 " namespace in {\n"
4590 " int j;\n"
4591 " } // namespace in\n"
4592 " int k;\n"
4593 "}} // namespace out::mid",
4594 "namespace out { namespace mid {\n"
4595 "namespace in { int j; } // namespace in\n"
4596 "int k; }} // namespace out::mid",
4597 Style);
4598
4599 verifyFormat("namespace A { namespace B { namespace C {\n"
4600 " int i;\n"
4601 "}}} // namespace A::B::C\n"
4602 "int main() {\n"
4603 " if (true)\n"
4604 " return 0;\n"
4605 "}",
4606 "namespace A { namespace B {\n"
4607 "namespace C {\n"
4608 " int i;\n"
4609 "}} // namespace B::C\n"
4610 "} // namespace A\n"
4611 "int main() {\n"
4612 " if (true)\n"
4613 " return 0;\n"
4614 "}",
4615 Style);
4616
4617 verifyFormat("namespace A { namespace B { namespace C {\n"
4618 "#ifdef FOO\n"
4619 " int i;\n"
4620 "#endif\n"
4621 "}}} // namespace A::B::C\n"
4622 "int main() {\n"
4623 " if (true)\n"
4624 " return 0;\n"
4625 "}",
4626 "namespace A { namespace B {\n"
4627 "namespace C {\n"
4628 "#ifdef FOO\n"
4629 " int i;\n"
4630 "#endif\n"
4631 "}} // namespace B::C\n"
4632 "} // namespace A\n"
4633 "int main() {\n"
4634 " if (true)\n"
4635 " return 0;\n"
4636 "}",
4637 Style);
4638
4639 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4640 verifyFormat("namespace out { namespace in {\n"
4641 " int i;\n"
4642 "}} // namespace out::in",
4643 "namespace out {\n"
4644 "namespace in {\n"
4645 "int i;\n"
4646 "} // namespace in\n"
4647 "} // namespace out",
4648 Style);
4649 verifyFormat("namespace out { namespace mid { namespace in {\n"
4650 " int i;\n"
4651 "}}} // namespace out::mid::in",
4652 "namespace out {\n"
4653 "namespace mid {\n"
4654 "namespace in {\n"
4655 "int i;\n"
4656 "} // namespace in\n"
4657 "} // namespace mid\n"
4658 "} // namespace out",
4659 Style);
4660
4661 Style.CompactNamespaces = true;
4662 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4663 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4664 Style.BraceWrapping.BeforeLambdaBody = true;
4665 verifyFormat("namespace out { namespace in {\n"
4666 "}} // namespace out::in",
4667 Style);
4668 verifyFormat("namespace out { namespace in {\n"
4669 "}} // namespace out::in",
4670 "namespace out {\n"
4671 "namespace in {\n"
4672 "} // namespace in\n"
4673 "} // namespace out",
4674 Style);
4675 }
4676
4677 TEST_F(FormatTest, FormatsExternC) {
4678 verifyFormat("extern \"C\" {\nint a;");
4679 verifyFormat("extern \"C\" {}");
4680 verifyFormat("extern \"C\" {\n"
4681 "int foo();\n"
4682 "}");
4683 verifyFormat("extern \"C\" int foo() {}");
4684 verifyFormat("extern \"C\" int foo();");
4685 verifyFormat("extern \"C\" int foo() {\n"
4686 " int i = 42;\n"
4687 " return i;\n"
4688 "}");
4689 verifyFormat(
4690 "extern \"C\" char const *const\n"
4691 " OpenCL_source_OpenCLRunTime_test_attribute_opencl_unroll_hint;");
4692
4693 FormatStyle Style = getLLVMStyle();
4694 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4695 Style.BraceWrapping.AfterFunction = true;
4696 verifyFormat("extern \"C\" int foo() {}", Style);
4697 verifyFormat("extern \"C\" int foo();", Style);
4698 verifyFormat("extern \"C\" int foo()\n"
4699 "{\n"
4700 " int i = 42;\n"
4701 " return i;\n"
4702 "}",
4703 Style);
4704
4705 Style.BraceWrapping.AfterExternBlock = true;
4706 Style.BraceWrapping.SplitEmptyRecord = false;
4707 verifyFormat("extern \"C\"\n"
4708 "{}",
4709 Style);
4710 verifyFormat("extern \"C\"\n"
4711 "{\n"
4712 " int foo();\n"
4713 "}",
4714 Style);
4715 }
4716
4717 TEST_F(FormatTest, IndentExternBlockStyle) {
4718 FormatStyle Style = getLLVMStyle();
4719 Style.IndentWidth = 2;
4720
4721 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4722 verifyFormat("extern \"C\" { /*9*/\n"
4723 "}",
4724 Style);
4725 verifyFormat("extern \"C\" {\n"
4726 " int foo10();\n"
4727 "}",
4728 Style);
4729
4730 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4731 verifyFormat("extern \"C\" { /*11*/\n"
4732 "}",
4733 Style);
4734 verifyFormat("extern \"C\" {\n"
4735 "int foo12();\n"
4736 "}",
4737 Style);
4738
4739 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4740 verifyFormat("extern \"C\"\n"
4741 "{\n"
4742 "int i;\n"
4743 "}",
4744 Style);
4745
4746 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4747 Style.BraceWrapping.AfterExternBlock = true;
4748 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4749 verifyFormat("extern \"C\"\n"
4750 "{ /*13*/\n"
4751 "}",
4752 Style);
4753 verifyFormat("extern \"C\"\n{\n"
4754 " int foo14();\n"
4755 "}",
4756 Style);
4757
4758 Style.BraceWrapping.AfterExternBlock = false;
4759 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4760 verifyFormat("extern \"C\" { /*15*/\n"
4761 "}",
4762 Style);
4763 verifyFormat("extern \"C\" {\n"
4764 "int foo16();\n"
4765 "}",
4766 Style);
4767
4768 Style.BraceWrapping.AfterExternBlock = true;
4769 verifyFormat("extern \"C\"\n"
4770 "{ /*13*/\n"
4771 "}",
4772 Style);
4773 verifyFormat("extern \"C\"\n"
4774 "{\n"
4775 "int foo14();\n"
4776 "}",
4777 Style);
4778
4779 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4780 verifyFormat("extern \"C\"\n"
4781 "{ /*13*/\n"
4782 "}",
4783 Style);
4784 verifyFormat("extern \"C\"\n"
4785 "{\n"
4786 " int foo14();\n"
4787 "}",
4788 Style);
4789 }
4790
4791 TEST_F(FormatTest, FormatsInlineASM) {
4792 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4793 verifyFormat("asm(\"nop\" ::: \"memory\");");
4794 verifyFormat(
4795 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4796 " \"cpuid\\n\\t\"\n"
4797 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4798 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4799 " : \"a\"(value));");
4800 verifyFormat(
4801 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4802 " __asm {\n"
4803 " mov edx,[that] // vtable in edx\n"
4804 " mov eax,methodIndex\n"
4805 " call [edx][eax*4] // stdcall\n"
4806 " }\n"
4807 "}",
4808 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4809 " __asm {\n"
4810 " mov edx,[that] // vtable in edx\n"
4811 " mov eax,methodIndex\n"
4812 " call [edx][eax*4] // stdcall\n"
4813 " }\n"
4814 "}");
4815 verifyNoChange("_asm {\n"
4816 " xor eax, eax;\n"
4817 " cpuid;\n"
4818 "}");
4819 verifyFormat("void function() {\n"
4820 " // comment\n"
4821 " asm(\"\");\n"
4822 "}");
4823 verifyFormat("__asm {\n"
4824 "}\n"
4825 "int i;",
4826 "__asm {\n"
4827 "}\n"
4828 "int i;");
4829
4830 auto Style = getLLVMStyleWithColumns(0);
4831 constexpr StringRef Code1(
4832 "asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4833 constexpr StringRef Code2("asm(\"xyz\"\n"
4834 " : \"=a\"(a), \"=d\"(b)\n"
4835 " : \"a\"(data));");
4836 constexpr StringRef Code3("asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4837 " : \"a\"(data));");
4838
4839 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4840 verifyFormat(Code1, Style);
4841 verifyNoChange(Code2, Style);
4842 verifyNoChange(Code3, Style);
4843
4844 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4845 verifyFormat(Code2, Code1, Style);
4846 verifyNoChange(Code2, Style);
4847 verifyFormat(Code2, Code3, Style);
4848 }
4849
4850 TEST_F(FormatTest, FormatTryCatch) {
4851 verifyFormat("try {\n"
4852 " throw a * b;\n"
4853 "} catch (int a) {\n"
4854 " // Do nothing.\n"
4855 "} catch (...) {\n"
4856 " exit(42);\n"
4857 "}");
4858
4859 // Function-level try statements.
4860 verifyFormat("int f() try { return 4; } catch (...) {\n"
4861 " return 5;\n"
4862 "}");
4863 verifyFormat("class A {\n"
4864 " int a;\n"
4865 " A() try : a(0) {\n"
4866 " } catch (...) {\n"
4867 " throw;\n"
4868 " }\n"
4869 "};");
4870 verifyFormat("class A {\n"
4871 " int a;\n"
4872 " A() try : a(0), b{1} {\n"
4873 " } catch (...) {\n"
4874 " throw;\n"
4875 " }\n"
4876 "};");
4877 verifyFormat("class A {\n"
4878 " int a;\n"
4879 " A() try : a(0), b{1}, c{2} {\n"
4880 " } catch (...) {\n"
4881 " throw;\n"
4882 " }\n"
4883 "};");
4884 verifyFormat("class A {\n"
4885 " int a;\n"
4886 " A() try : a(0), b{1}, c{2} {\n"
4887 " { // New scope.\n"
4888 " }\n"
4889 " } catch (...) {\n"
4890 " throw;\n"
4891 " }\n"
4892 "};");
4893
4894 // Incomplete try-catch blocks.
4895 verifyIncompleteFormat("try {} catch (");
4896 }
4897
4898 TEST_F(FormatTest, FormatTryAsAVariable) {
4899 verifyFormat("int try;");
4900 verifyFormat("int try, size;");
4901 verifyFormat("try = foo();");
4902 verifyFormat("if (try < size) {\n return true;\n}");
4903
4904 verifyFormat("int catch;");
4905 verifyFormat("int catch, size;");
4906 verifyFormat("catch = foo();");
4907 verifyFormat("if (catch < size) {\n return true;\n}");
4908
4909 FormatStyle Style = getLLVMStyle();
4910 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4911 Style.BraceWrapping.AfterFunction = true;
4912 Style.BraceWrapping.BeforeCatch = true;
4913 verifyFormat("try {\n"
4914 " int bar = 1;\n"
4915 "}\n"
4916 "catch (...) {\n"
4917 " int bar = 1;\n"
4918 "}",
4919 Style);
4920 verifyFormat("#if NO_EX\n"
4921 "try\n"
4922 "#endif\n"
4923 "{\n"
4924 "}\n"
4925 "#if NO_EX\n"
4926 "catch (...) {\n"
4927 "}",
4928 Style);
4929 verifyFormat("try /* abc */ {\n"
4930 " int bar = 1;\n"
4931 "}\n"
4932 "catch (...) {\n"
4933 " int bar = 1;\n"
4934 "}",
4935 Style);
4936 verifyFormat("try\n"
4937 "// abc\n"
4938 "{\n"
4939 " int bar = 1;\n"
4940 "}\n"
4941 "catch (...) {\n"
4942 " int bar = 1;\n"
4943 "}",
4944 Style);
4945 }
4946
4947 TEST_F(FormatTest, FormatSEHTryCatch) {
4948 verifyFormat("__try {\n"
4949 " int a = b * c;\n"
4950 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4951 " // Do nothing.\n"
4952 "}");
4953
4954 verifyFormat("__try {\n"
4955 " int a = b * c;\n"
4956 "} __finally {\n"
4957 " // Do nothing.\n"
4958 "}");
4959
4960 verifyFormat("DEBUG({\n"
4961 " __try {\n"
4962 " } __finally {\n"
4963 " }\n"
4964 "});");
4965 }
4966
4967 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4968 verifyFormat("try {\n"
4969 " f();\n"
4970 "} catch {\n"
4971 " g();\n"
4972 "}");
4973 verifyFormat("try {\n"
4974 " f();\n"
4975 "} catch (A a) MACRO(x) {\n"
4976 " g();\n"
4977 "} catch (B b) MACRO(x) {\n"
4978 " g();\n"
4979 "}");
4980 }
4981
4982 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4983 FormatStyle Style = getLLVMStyle();
4984 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4985 FormatStyle::BS_WebKit}) {
4986 Style.BreakBeforeBraces = BraceStyle;
4987 verifyFormat("try {\n"
4988 " // something\n"
4989 "} catch (...) {\n"
4990 " // something\n"
4991 "}",
4992 Style);
4993 }
4994 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4995 verifyFormat("try {\n"
4996 " // something\n"
4997 "}\n"
4998 "catch (...) {\n"
4999 " // something\n"
5000 "}",
5001 Style);
5002 verifyFormat("__try {\n"
5003 " // something\n"
5004 "}\n"
5005 "__finally {\n"
5006 " // something\n"
5007 "}",
5008 Style);
5009 verifyFormat("@try {\n"
5010 " // something\n"
5011 "}\n"
5012 "@finally {\n"
5013 " // something\n"
5014 "}",
5015 Style);
5016 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
5017 verifyFormat("try\n"
5018 "{\n"
5019 " // something\n"
5020 "}\n"
5021 "catch (...)\n"
5022 "{\n"
5023 " // something\n"
5024 "}",
5025 Style);
5026 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
5027 verifyFormat("try\n"
5028 " {\n"
5029 " // something white\n"
5030 " }\n"
5031 "catch (...)\n"
5032 " {\n"
5033 " // something white\n"
5034 " }",
5035 Style);
5036 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
5037 verifyFormat("try\n"
5038 " {\n"
5039 " // something\n"
5040 " }\n"
5041 "catch (...)\n"
5042 " {\n"
5043 " // something\n"
5044 " }",
5045 Style);
5046 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5047 Style.BraceWrapping.BeforeCatch = true;
5048 verifyFormat("try {\n"
5049 " // something\n"
5050 "}\n"
5051 "catch (...) {\n"
5052 " // something\n"
5053 "}",
5054 Style);
5055 }
5056
5057 TEST_F(FormatTest, StaticInitializers) {
5058 verifyFormat("static SomeClass SC = {1, 'a'};");
5059
5060 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
5061 " 100000000, "
5062 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
5063
5064 // Here, everything other than the "}" would fit on a line.
5065 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
5066 " 10000000000000000000000000};");
5067 verifyFormat("S s = {a,\n"
5068 "\n"
5069 " b};",
5070 "S s = {\n"
5071 " a,\n"
5072 "\n"
5073 " b\n"
5074 "};");
5075
5076 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5077 // line. However, the formatting looks a bit off and this probably doesn't
5078 // happen often in practice.
5079 verifyFormat("static int Variable[1] = {\n"
5080 " {1000000000000000000000000000000000000}};",
5081 getLLVMStyleWithColumns(40));
5082 }
5083
5084 TEST_F(FormatTest, DesignatedInitializers) {
5085 verifyFormat("const struct A a = {.a = 1, .b = 2};");
5086 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5087 " .bbbbbbbbbb = 2,\n"
5088 " .cccccccccc = 3,\n"
5089 " .dddddddddd = 4,\n"
5090 " .eeeeeeeeee = 5};");
5091 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5092 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5093 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5094 " .ccccccccccccccccccccccccccc = 3,\n"
5095 " .ddddddddddddddddddddddddddd = 4,\n"
5096 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5097
5098 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5099
5100 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5101 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5102 " [2] = bbbbbbbbbb,\n"
5103 " [3] = cccccccccc,\n"
5104 " [4] = dddddddddd,\n"
5105 " [5] = eeeeeeeeee};");
5106 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5107 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5108 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5109 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5110 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5111 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5112
5113 verifyFormat("for (const TestCase &test_case : {\n"
5114 " TestCase{\n"
5115 " .a = 1,\n"
5116 " .b = 1,\n"
5117 " },\n"
5118 " TestCase{\n"
5119 " .a = 2,\n"
5120 " .b = 2,\n"
5121 " },\n"
5122 " }) {\n"
5123 "}");
5124 }
5125
5126 TEST_F(FormatTest, BracedInitializerIndentWidth) {
5127 auto Style = getLLVMStyleWithColumns(60);
5128 Style.BinPackArguments = true;
5129 Style.BreakAfterOpenBracketFunction = true;
5130 Style.BreakAfterOpenBracketBracedList = true;
5131 Style.BracedInitializerIndentWidth = 6;
5132
5133 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5134 verifyFormat("enum class {\n"
5135 " One,\n"
5136 " Two,\n"
5137 "};",
5138 Style);
5139 verifyFormat("class Foo {\n"
5140 " Foo() {}\n"
5141 " void bar();\n"
5142 "};",
5143 Style);
5144 verifyFormat("void foo() {\n"
5145 " auto bar = baz;\n"
5146 " return baz;\n"
5147 "};",
5148 Style);
5149 verifyFormat("auto foo = [&] {\n"
5150 " auto bar = baz;\n"
5151 " return baz;\n"
5152 "};",
5153 Style);
5154 verifyFormat("{\n"
5155 " auto bar = baz;\n"
5156 " return baz;\n"
5157 "};",
5158 Style);
5159 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5160 verifyFormat("SomeClass clazz(\n"
5161 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5162 " \"zzzzzzzzzzzzzzzzzz\");",
5163 Style);
5164
5165 // The following types of initialization are all affected by
5166 // BracedInitializerIndentWidth. Aggregate initialization.
5167 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5168 " 10000000, 20000000};",
5169 Style);
5170 verifyFormat("SomeStruct s{\n"
5171 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5172 " \"zzzzzzzzzzzzzzzz\"};",
5173 Style);
5174 // Designated initializers.
5175 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5176 " [0] = 10000000, [1] = 20000000};",
5177 Style);
5178 verifyFormat("SomeStruct s{\n"
5179 " .foo = \"xxxxxxxxxxxxx\",\n"
5180 " .bar = \"yyyyyyyyyyyyy\",\n"
5181 " .baz = \"zzzzzzzzzzzzz\"};",
5182 Style);
5183 // List initialization.
5184 verifyFormat("SomeStruct s{\n"
5185 " \"xxxxxxxxxxxxx\",\n"
5186 " \"yyyyyyyyyyyyy\",\n"
5187 " \"zzzzzzzzzzzzz\",\n"
5188 "};",
5189 Style);
5190 verifyFormat("SomeStruct{\n"
5191 " \"xxxxxxxxxxxxx\",\n"
5192 " \"yyyyyyyyyyyyy\",\n"
5193 " \"zzzzzzzzzzzzz\",\n"
5194 "};",
5195 Style);
5196 verifyFormat("new SomeStruct{\n"
5197 " \"xxxxxxxxxxxxx\",\n"
5198 " \"yyyyyyyyyyyyy\",\n"
5199 " \"zzzzzzzzzzzzz\",\n"
5200 "};",
5201 Style);
5202 // Member initializer.
5203 verifyFormat("class SomeClass {\n"
5204 " SomeStruct s{\n"
5205 " \"xxxxxxxxxxxxx\",\n"
5206 " \"yyyyyyyyyyyyy\",\n"
5207 " \"zzzzzzzzzzzzz\",\n"
5208 " };\n"
5209 "};",
5210 Style);
5211 // Constructor member initializer.
5212 verifyFormat("SomeClass::SomeClass : strct{\n"
5213 " \"xxxxxxxxxxxxx\",\n"
5214 " \"yyyyyyyyyyyyy\",\n"
5215 " \"zzzzzzzzzzzzz\",\n"
5216 " } {}",
5217 Style);
5218 // Copy initialization.
5219 verifyFormat("SomeStruct s = SomeStruct{\n"
5220 " \"xxxxxxxxxxxxx\",\n"
5221 " \"yyyyyyyyyyyyy\",\n"
5222 " \"zzzzzzzzzzzzz\",\n"
5223 "};",
5224 Style);
5225 // Copy list initialization.
5226 verifyFormat("SomeStruct s = {\n"
5227 " \"xxxxxxxxxxxxx\",\n"
5228 " \"yyyyyyyyyyyyy\",\n"
5229 " \"zzzzzzzzzzzzz\",\n"
5230 "};",
5231 Style);
5232 // Assignment operand initialization.
5233 verifyFormat("s = {\n"
5234 " \"xxxxxxxxxxxxx\",\n"
5235 " \"yyyyyyyyyyyyy\",\n"
5236 " \"zzzzzzzzzzzzz\",\n"
5237 "};",
5238 Style);
5239 // Returned object initialization.
5240 verifyFormat("return {\n"
5241 " \"xxxxxxxxxxxxx\",\n"
5242 " \"yyyyyyyyyyyyy\",\n"
5243 " \"zzzzzzzzzzzzz\",\n"
5244 "};",
5245 Style);
5246 // Initializer list.
5247 verifyFormat("auto initializerList = {\n"
5248 " \"xxxxxxxxxxxxx\",\n"
5249 " \"yyyyyyyyyyyyy\",\n"
5250 " \"zzzzzzzzzzzzz\",\n"
5251 "};",
5252 Style);
5253 // Function parameter initialization.
5254 verifyFormat("func({\n"
5255 " \"xxxxxxxxxxxxx\",\n"
5256 " \"yyyyyyyyyyyyy\",\n"
5257 " \"zzzzzzzzzzzzz\",\n"
5258 "});",
5259 Style);
5260 // Nested init lists.
5261 verifyFormat("SomeStruct s = {\n"
5262 " {{init1, init2, init3, init4, init5},\n"
5263 " {init1, init2, init3, init4, init5}}};",
5264 Style);
5265 verifyFormat("SomeStruct s = {\n"
5266 " {{\n"
5267 " .init1 = 1,\n"
5268 " .init2 = 2,\n"
5269 " .init3 = 3,\n"
5270 " .init4 = 4,\n"
5271 " .init5 = 5,\n"
5272 " },\n"
5273 " {init1, init2, init3, init4, init5}}};",
5274 Style);
5275 verifyFormat("SomeArrayT a[3] = {\n"
5276 " {\n"
5277 " foo,\n"
5278 " bar,\n"
5279 " },\n"
5280 " {\n"
5281 " foo,\n"
5282 " bar,\n"
5283 " },\n"
5284 " SomeArrayT{},\n"
5285 "};",
5286 Style);
5287 verifyFormat("SomeArrayT a[3] = {\n"
5288 " {foo},\n"
5289 " {\n"
5290 " {\n"
5291 " init1,\n"
5292 " init2,\n"
5293 " init3,\n"
5294 " },\n"
5295 " {\n"
5296 " init1,\n"
5297 " init2,\n"
5298 " init3,\n"
5299 " },\n"
5300 " },\n"
5301 " {baz},\n"
5302 "};",
5303 Style);
5304
5305 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5306 Style.AlignAfterOpenBracket = true;
5307 Style.BreakAfterOpenBracketBracedList = false;
5308 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5309 " \"zzzzzzzzzzzzz\"};",
5310 Style);
5311 }
5312
5313 TEST_F(FormatTest, NestedStaticInitializers) {
5314 verifyFormat("static A x = {{{}}};");
5315 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5316 " {init1, init2, init3, init4}}};",
5317 getLLVMStyleWithColumns(50));
5318
5319 verifyFormat("somes Status::global_reps[3] = {\n"
5320 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5321 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5322 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5323 getLLVMStyleWithColumns(60));
5324 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5325 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5326 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5327 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5328 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5329 " {rect.fRight - rect.fLeft, rect.fBottom - "
5330 "rect.fTop}};");
5331
5332 verifyFormat(
5333 "SomeArrayOfSomeType a = {\n"
5334 " {{1, 2, 3},\n"
5335 " {1, 2, 3},\n"
5336 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5337 " 333333333333333333333333333333},\n"
5338 " {1, 2, 3},\n"
5339 " {1, 2, 3}}};");
5340 verifyFormat(
5341 "SomeArrayOfSomeType a = {\n"
5342 " {{1, 2, 3}},\n"
5343 " {{1, 2, 3}},\n"
5344 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5345 " 333333333333333333333333333333}},\n"
5346 " {{1, 2, 3}},\n"
5347 " {{1, 2, 3}}};");
5348
5349 verifyFormat("struct {\n"
5350 " unsigned bit;\n"
5351 " const char *const name;\n"
5352 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5353 " {kOsWin, \"Windows\"},\n"
5354 " {kOsLinux, \"Linux\"},\n"
5355 " {kOsCrOS, \"Chrome OS\"}};");
5356 verifyFormat("struct {\n"
5357 " unsigned bit;\n"
5358 " const char *const name;\n"
5359 "} kBitsToOs[] = {\n"
5360 " {kOsMac, \"Mac\"},\n"
5361 " {kOsWin, \"Windows\"},\n"
5362 " {kOsLinux, \"Linux\"},\n"
5363 " {kOsCrOS, \"Chrome OS\"},\n"
5364 "};");
5365 }
5366
5367 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5368 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5369 " \\\n"
5370 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5371 }
5372
5373 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5374 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5375 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5376
5377 // Do break defaulted and deleted functions.
5378 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5379 " default;",
5380 getLLVMStyleWithColumns(40));
5381 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5382 " delete;",
5383 getLLVMStyleWithColumns(40));
5384 }
5385
5386 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5387 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5388 getLLVMStyleWithColumns(40));
5389 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5390 getLLVMStyleWithColumns(40));
5391 verifyFormat("#define Q \\\n"
5392 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5393 " \"aaaaaaaa.cpp\"",
5394 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5395 getLLVMStyleWithColumns(40));
5396 }
5397
5398 TEST_F(FormatTest, UnderstandsLinePPDirective) {
5399 verifyFormat("# 123 \"A string literal\"",
5400 " # 123 \"A string literal\"");
5401 }
5402
5403 TEST_F(FormatTest, LayoutUnknownPPDirective) {
5404 verifyFormat("#;");
5405 verifyFormat("#\n;\n;\n;");
5406 }
5407
5408 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5409 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5410 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5411 getLLVMStyleWithColumns(12));
5412 }
5413
5414 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5415 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5416 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5417 }
5418
5419 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5420 verifyFormat("#define A \\x20");
5421 verifyFormat("#define A \\ x20");
5422 verifyFormat("#define A \\ x20", "#define A \\ x20");
5423 verifyFormat("#define A ''");
5424 verifyFormat("#define A ''qqq");
5425 verifyFormat("#define A `qqq");
5426 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5427 verifyFormat("const char *c = STRINGIFY(\n"
5428 "\\na : b);",
5429 "const char * c = STRINGIFY(\n"
5430 "\\na : b);");
5431
5432 verifyFormat("a\r\\");
5433 verifyFormat("a\v\\");
5434 verifyFormat("a\f\\");
5435 }
5436
5437 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5438 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5439 style.IndentWidth = 4;
5440 style.PPIndentWidth = 1;
5441
5442 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5443 verifyFormat("#ifdef __linux__\n"
5444 "void foo() {\n"
5445 " int x = 0;\n"
5446 "}\n"
5447 "#define FOO\n"
5448 "#endif\n"
5449 "void bar() {\n"
5450 " int y = 0;\n"
5451 "}",
5452 style);
5453
5454 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5455 verifyFormat("#ifdef __linux__\n"
5456 "void foo() {\n"
5457 " int x = 0;\n"
5458 "}\n"
5459 "# define FOO foo\n"
5460 "#endif\n"
5461 "void bar() {\n"
5462 " int y = 0;\n"
5463 "}",
5464 style);
5465
5466 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5467 verifyFormat("#ifdef __linux__\n"
5468 "void foo() {\n"
5469 " int x = 0;\n"
5470 "}\n"
5471 " #define FOO foo\n"
5472 "#endif\n"
5473 "void bar() {\n"
5474 " int y = 0;\n"
5475 "}",
5476 style);
5477 verifyFormat("#if 1\n"
5478 " // some comments\n"
5479 " // another\n"
5480 " #define foo 1\n"
5481 "// not a define comment\n"
5482 "void bar() {\n"
5483 " // comment\n"
5484 " int y = 0;\n"
5485 "}",
5486 "#if 1\n"
5487 "// some comments\n"
5488 "// another\n"
5489 "#define foo 1\n"
5490 "// not a define comment\n"
5491 "void bar() {\n"
5492 " // comment\n"
5493 " int y = 0;\n"
5494 "}",
5495 style);
5496
5497 style.IndentPPDirectives = FormatStyle::PPDIS_None;
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 verifyFormat("if (emacs) {\n"
5507 "#ifdef is\n"
5508 "#define lit \\\n"
5509 " if (af) { \\\n"
5510 " return duh(); \\\n"
5511 " }\n"
5512 "#endif\n"
5513 "}",
5514 style);
5515 verifyFormat("#if abc\n"
5516 "#ifdef foo\n"
5517 "#define bar() \\\n"
5518 " if (A) { \\\n"
5519 " if (B) { \\\n"
5520 " C(); \\\n"
5521 " } \\\n"
5522 " } \\\n"
5523 " D();\n"
5524 "#endif\n"
5525 "#endif",
5526 style);
5527 verifyFormat("#ifndef foo\n"
5528 "#define foo\n"
5529 "if (emacs) {\n"
5530 "#ifdef is\n"
5531 "#define lit \\\n"
5532 " if (af) { \\\n"
5533 " return duh(); \\\n"
5534 " }\n"
5535 "#endif\n"
5536 "}\n"
5537 "#endif",
5538 style);
5539 verifyFormat("#if 1\n"
5540 "#define X \\\n"
5541 " { \\\n"
5542 " x; \\\n"
5543 " x; \\\n"
5544 " }\n"
5545 "#endif",
5546 style);
5547 verifyFormat("#define X \\\n"
5548 " { \\\n"
5549 " x; \\\n"
5550 " x; \\\n"
5551 " }",
5552 style);
5553
5554 style.PPIndentWidth = 2;
5555 verifyFormat("#ifdef foo\n"
5556 "#define bar() \\\n"
5557 " if (A) { \\\n"
5558 " B(); \\\n"
5559 " } \\\n"
5560 " C();\n"
5561 "#endif",
5562 style);
5563 style.IndentWidth = 8;
5564 verifyFormat("#ifdef foo\n"
5565 "#define bar() \\\n"
5566 " if (A) { \\\n"
5567 " B(); \\\n"
5568 " } \\\n"
5569 " C();\n"
5570 "#endif",
5571 style);
5572
5573 style.IndentWidth = 1;
5574 style.PPIndentWidth = 4;
5575 verifyFormat("#if 1\n"
5576 "#define X \\\n"
5577 " { \\\n"
5578 " x; \\\n"
5579 " x; \\\n"
5580 " }\n"
5581 "#endif",
5582 style);
5583 verifyFormat("#define X \\\n"
5584 " { \\\n"
5585 " x; \\\n"
5586 " x; \\\n"
5587 " }",
5588 style);
5589
5590 style.IndentPPDirectives = FormatStyle::PPDIS_Leave;
5591 style.IndentWidth = 4;
5592 verifyNoChange("#ifndef foo\n"
5593 "#define foo\n"
5594 "if (emacs) {\n"
5595 "#ifdef is\n"
5596 "#define lit \\\n"
5597 " if (af) { \\\n"
5598 " return duh(); \\\n"
5599 " }\n"
5600 "#endif\n"
5601 "}\n"
5602 "#endif",
5603 style);
5604 verifyNoChange("#ifndef foo\n"
5605 " #define foo\n"
5606 "if (emacs) {\n"
5607 " #ifdef is\n"
5608 "#define lit \\\n"
5609 " if (af) { \\\n"
5610 " return duh(); \\\n"
5611 " }\n"
5612 " #endif\n"
5613 "}\n"
5614 "#endif",
5615 style);
5616 verifyNoChange(" #ifndef foo\n"
5617 "# define foo\n"
5618 "if (emacs) {\n"
5619 "#ifdef is\n"
5620 " # define lit \\\n"
5621 " if (af) { \\\n"
5622 " return duh(); \\\n"
5623 " }\n"
5624 "#endif\n"
5625 "}\n"
5626 " #endif",
5627 style);
5628 verifyNoChange("#ifdef foo\n"
5629 "#else\n"
5630 "/* This is a comment */\n"
5631 "#ifdef BAR\n"
5632 "#endif\n"
5633 "#endif",
5634 style);
5635
5636 style.IndentWidth = 1;
5637 style.PPIndentWidth = 4;
5638 verifyNoChange("# if 1\n"
5639 " #define X \\\n"
5640 " { \\\n"
5641 " x; \\\n"
5642 " x; \\\n"
5643 " }\n"
5644 "# endif",
5645 style);
5646
5647 style.IndentWidth = 4;
5648 style.PPIndentWidth = 1;
5649 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5650 verifyFormat("#ifdef foo\n"
5651 "# define bar() \\\n"
5652 " if (A) { \\\n"
5653 " B(); \\\n"
5654 " } \\\n"
5655 " C();\n"
5656 "#endif",
5657 style);
5658 verifyFormat("#if abc\n"
5659 "# ifdef foo\n"
5660 "# define bar() \\\n"
5661 " if (A) { \\\n"
5662 " if (B) { \\\n"
5663 " C(); \\\n"
5664 " } \\\n"
5665 " } \\\n"
5666 " D();\n"
5667 "# endif\n"
5668 "#endif",
5669 style);
5670 verifyFormat("#ifndef foo\n"
5671 "#define foo\n"
5672 "if (emacs) {\n"
5673 "#ifdef is\n"
5674 "# define lit \\\n"
5675 " if (af) { \\\n"
5676 " return duh(); \\\n"
5677 " }\n"
5678 "#endif\n"
5679 "}\n"
5680 "#endif",
5681 style);
5682 verifyFormat("#define X \\\n"
5683 " { \\\n"
5684 " x; \\\n"
5685 " x; \\\n"
5686 " }",
5687 style);
5688
5689 style.PPIndentWidth = 2;
5690 style.IndentWidth = 8;
5691 verifyFormat("#ifdef foo\n"
5692 "# define bar() \\\n"
5693 " if (A) { \\\n"
5694 " B(); \\\n"
5695 " } \\\n"
5696 " C();\n"
5697 "#endif",
5698 style);
5699
5700 style.PPIndentWidth = 4;
5701 style.IndentWidth = 1;
5702 verifyFormat("#define X \\\n"
5703 " { \\\n"
5704 " x; \\\n"
5705 " x; \\\n"
5706 " }",
5707 style);
5708
5709 style.IndentWidth = 4;
5710 style.PPIndentWidth = 1;
5711 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5712 verifyFormat("if (emacs) {\n"
5713 "#ifdef is\n"
5714 " #define lit \\\n"
5715 " if (af) { \\\n"
5716 " return duh(); \\\n"
5717 " }\n"
5718 "#endif\n"
5719 "}",
5720 style);
5721 verifyFormat("#if abc\n"
5722 " #ifdef foo\n"
5723 " #define bar() \\\n"
5724 " if (A) { \\\n"
5725 " B(); \\\n"
5726 " } \\\n"
5727 " C();\n"
5728 " #endif\n"
5729 "#endif",
5730 style);
5731 verifyFormat("#if 1\n"
5732 " #define X \\\n"
5733 " { \\\n"
5734 " x; \\\n"
5735 " x; \\\n"
5736 " }\n"
5737 "#endif",
5738 style);
5739
5740 style.PPIndentWidth = 2;
5741 verifyFormat("#ifdef foo\n"
5742 " #define bar() \\\n"
5743 " if (A) { \\\n"
5744 " B(); \\\n"
5745 " } \\\n"
5746 " C();\n"
5747 "#endif",
5748 style);
5749
5750 style.PPIndentWidth = 4;
5751 style.IndentWidth = 1;
5752 verifyFormat("#if 1\n"
5753 " #define X \\\n"
5754 " { \\\n"
5755 " x; \\\n"
5756 " x; \\\n"
5757 " }\n"
5758 "#endif",
5759 style);
5760 }
5761
5762 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5763 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5764 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5765 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5766 // FIXME: We never break before the macro name.
5767 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5768
5769 verifyFormat("#define A A\n#define A A");
5770 verifyFormat("#define A(X) A\n#define A A");
5771
5772 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5773 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5774 }
5775
5776 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5777 verifyFormat("// somecomment\n"
5778 "#include \"a.h\"\n"
5779 "#define A( \\\n"
5780 " A, B)\n"
5781 "#include \"b.h\"\n"
5782 "// somecomment",
5783 " // somecomment\n"
5784 " #include \"a.h\"\n"
5785 "#define A(A,\\\n"
5786 " B)\n"
5787 " #include \"b.h\"\n"
5788 " // somecomment",
5789 getLLVMStyleWithColumns(13));
5790 }
5791
5792 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5793
5794 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5795 verifyFormat("#define A \\\n"
5796 " c; \\\n"
5797 " e;\n"
5798 "f;",
5799 "#define A c; e;\n"
5800 "f;",
5801 getLLVMStyleWithColumns(14));
5802 }
5803
5804 TEST_F(FormatTest, LayoutRemainingTokens) {
5805 verifyFormat("{\n"
5806 "}");
5807 }
5808
5809 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5810 verifyFormat("int x,\n"
5811 "#define A\n"
5812 " y;",
5813 "int x,\n#define A\ny;");
5814 }
5815
5816 TEST_F(FormatTest, HashInMacroDefinition) {
5817 verifyFormat("#define A(c) L#c");
5818 verifyFormat("#define A(c) u#c");
5819 verifyFormat("#define A(c) U#c");
5820 verifyFormat("#define A(c) u8#c");
5821 verifyFormat("#define A(c) LR#c");
5822 verifyFormat("#define A(c) uR#c");
5823 verifyFormat("#define A(c) UR#c");
5824 verifyFormat("#define A(c) u8R#c");
5825 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5826 verifyFormat("#define A \\\n"
5827 " { \\\n"
5828 " f(#c); \\\n"
5829 " }",
5830 getLLVMStyleWithColumns(11));
5831
5832 verifyFormat("#define A(X) \\\n"
5833 " void function##X()",
5834 getLLVMStyleWithColumns(22));
5835
5836 verifyFormat("#define A(a, b, c) \\\n"
5837 " void a##b##c()",
5838 getLLVMStyleWithColumns(22));
5839
5840 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5841
5842 verifyFormat("{\n"
5843 " {\n"
5844 "#define GEN_ID(_x) char *_x{#_x}\n"
5845 " GEN_ID(one);\n"
5846 " }\n"
5847 "}");
5848 }
5849
5850 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5851 verifyFormat("#define A (x)");
5852 verifyFormat("#define A(x)");
5853
5854 FormatStyle Style = getLLVMStyle();
5855 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5856 verifyFormat("#define true ((foo)1)", Style);
5857 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5858 verifyFormat("#define false((foo)0)", Style);
5859 }
5860
5861 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5862 verifyFormat("#define A b;",
5863 "#define A \\\n"
5864 " \\\n"
5865 " b;",
5866 getLLVMStyleWithColumns(25));
5867 verifyNoChange("#define A \\\n"
5868 " \\\n"
5869 " a; \\\n"
5870 " b;",
5871 getLLVMStyleWithColumns(11));
5872 verifyNoChange("#define A \\\n"
5873 " a; \\\n"
5874 " \\\n"
5875 " b;",
5876 getLLVMStyleWithColumns(11));
5877 }
5878
5879 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5880 verifyIncompleteFormat("#define A :");
5881 verifyFormat("#define SOMECASES \\\n"
5882 " case 1: \\\n"
5883 " case 2",
5884 getLLVMStyleWithColumns(20));
5885 verifyFormat("#define MACRO(a) \\\n"
5886 " if (a) \\\n"
5887 " f(); \\\n"
5888 " else \\\n"
5889 " g()",
5890 getLLVMStyleWithColumns(18));
5891 verifyFormat("#define A template <typename T>");
5892 verifyIncompleteFormat("#define STR(x) #x\n"
5893 "f(STR(this_is_a_string_literal{));");
5894 verifyFormat("#pragma omp threadprivate( \\\n"
5895 " y)), // expected-warning",
5896 getLLVMStyleWithColumns(28));
5897 verifyFormat("#d, = };");
5898 verifyFormat("#if \"a");
5899 verifyIncompleteFormat("({\n"
5900 "#define b \\\n"
5901 " } \\\n"
5902 " a\n"
5903 "a",
5904 getLLVMStyleWithColumns(15));
5905 verifyFormat("#define A \\\n"
5906 " { \\\n"
5907 " {\n"
5908 "#define B \\\n"
5909 " } \\\n"
5910 " }",
5911 getLLVMStyleWithColumns(15));
5912 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5913 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5914 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5915 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5916 verifyNoCrash("#else\n"
5917 "#else\n"
5918 "#endif\n"
5919 "#endif");
5920 verifyNoCrash("#else\n"
5921 "#if X\n"
5922 "#endif\n"
5923 "#endif");
5924 verifyNoCrash("#else\n"
5925 "#endif\n"
5926 "#if X\n"
5927 "#endif");
5928 verifyNoCrash("#if X\n"
5929 "#else\n"
5930 "#else\n"
5931 "#endif\n"
5932 "#endif");
5933 verifyNoCrash("#if X\n"
5934 "#elif Y\n"
5935 "#elif Y\n"
5936 "#endif\n"
5937 "#endif");
5938 verifyNoCrash("#endif\n"
5939 "#endif");
5940 verifyNoCrash("#endif\n"
5941 "#else");
5942 verifyNoCrash("#endif\n"
5943 "#elif Y");
5944 }
5945
5946 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5947 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5948 verifyFormat("class A : public QObject {\n"
5949 " Q_OBJECT\n"
5950 "\n"
5951 " A() {}\n"
5952 "};",
5953 "class A : public QObject {\n"
5954 " Q_OBJECT\n"
5955 "\n"
5956 " A() {\n}\n"
5957 "} ;");
5958 verifyFormat("MACRO\n"
5959 "/*static*/ int i;",
5960 "MACRO\n"
5961 " /*static*/ int i;");
5962 verifyFormat("SOME_MACRO\n"
5963 "namespace {\n"
5964 "void f();\n"
5965 "} // namespace",
5966 "SOME_MACRO\n"
5967 " namespace {\n"
5968 "void f( );\n"
5969 "} // namespace");
5970 // Only if the identifier contains at least 5 characters.
5971 verifyFormat("HTTP f();", "HTTP\nf();");
5972 verifyNoChange("MACRO\nf();");
5973 // Only if everything is upper case.
5974 verifyFormat("class A : public QObject {\n"
5975 " Q_Object A() {}\n"
5976 "};",
5977 "class A : public QObject {\n"
5978 " Q_Object\n"
5979 " A() {\n}\n"
5980 "} ;");
5981
5982 // Only if the next line can actually start an unwrapped line.
5983 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5984 "<< SomeThing;");
5985
5986 verifyFormat("GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)(foo);",
5987 "GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)\n"
5988 "(foo);",
5989 getLLVMStyleWithColumns(60));
5990
5991 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5992 "(n, buffers))",
5993 getChromiumStyle(FormatStyle::LK_Cpp));
5994
5995 // See PR41483
5996 verifyNoChange("/**/ FOO(a)\n"
5997 "FOO(b)");
5998 }
5999
6000 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
6001 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
6002 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
6003 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
6004 "class X {};\n"
6005 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
6006 "int *createScopDetectionPass() { return 0; }",
6007 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
6008 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
6009 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
6010 " class X {};\n"
6011 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
6012 " int *createScopDetectionPass() { return 0; }");
6013 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
6014 // braces, so that inner block is indented one level more.
6015 verifyFormat("int q() {\n"
6016 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
6017 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
6018 " IPC_END_MESSAGE_MAP()\n"
6019 "}",
6020 "int q() {\n"
6021 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
6022 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
6023 " IPC_END_MESSAGE_MAP()\n"
6024 "}");
6025
6026 // Same inside macros.
6027 verifyFormat("#define LIST(L) \\\n"
6028 " L(A) \\\n"
6029 " L(B) \\\n"
6030 " L(C)",
6031 "#define LIST(L) \\\n"
6032 " L(A) \\\n"
6033 " L(B) \\\n"
6034 " L(C)",
6035 getGoogleStyle());
6036
6037 // These must not be recognized as macros.
6038 verifyFormat("int q() {\n"
6039 " f(x);\n"
6040 " f(x) {}\n"
6041 " f(x)->g();\n"
6042 " f(x)->*g();\n"
6043 " f(x).g();\n"
6044 " f(x) = x;\n"
6045 " f(x) += x;\n"
6046 " f(x) -= x;\n"
6047 " f(x) *= x;\n"
6048 " f(x) /= x;\n"
6049 " f(x) %= x;\n"
6050 " f(x) &= x;\n"
6051 " f(x) |= x;\n"
6052 " f(x) ^= x;\n"
6053 " f(x) >>= x;\n"
6054 " f(x) <<= x;\n"
6055 " f(x)[y].z();\n"
6056 " LOG(INFO) << x;\n"
6057 " ifstream(x) >> x;\n"
6058 "}",
6059 "int q() {\n"
6060 " f(x)\n;\n"
6061 " f(x)\n {}\n"
6062 " f(x)\n->g();\n"
6063 " f(x)\n->*g();\n"
6064 " f(x)\n.g();\n"
6065 " f(x)\n = x;\n"
6066 " f(x)\n += x;\n"
6067 " f(x)\n -= x;\n"
6068 " f(x)\n *= x;\n"
6069 " f(x)\n /= x;\n"
6070 " f(x)\n %= x;\n"
6071 " f(x)\n &= x;\n"
6072 " f(x)\n |= x;\n"
6073 " f(x)\n ^= x;\n"
6074 " f(x)\n >>= x;\n"
6075 " f(x)\n <<= x;\n"
6076 " f(x)\n[y].z();\n"
6077 " LOG(INFO)\n << x;\n"
6078 " ifstream(x)\n >> x;\n"
6079 "}");
6080 verifyFormat("int q() {\n"
6081 " F(x)\n"
6082 " if (1) {\n"
6083 " }\n"
6084 " F(x)\n"
6085 " while (1) {\n"
6086 " }\n"
6087 " F(x)\n"
6088 " G(x);\n"
6089 " F(x)\n"
6090 " try {\n"
6091 " Q();\n"
6092 " } catch (...) {\n"
6093 " }\n"
6094 "}",
6095 "int q() {\n"
6096 "F(x)\n"
6097 "if (1) {}\n"
6098 "F(x)\n"
6099 "while (1) {}\n"
6100 "F(x)\n"
6101 "G(x);\n"
6102 "F(x)\n"
6103 "try { Q(); } catch (...) {}\n"
6104 "}");
6105 verifyFormat("class A {\n"
6106 " A() : t(0) {}\n"
6107 " A(int i) noexcept() : {}\n"
6108 " A(X x)\n" // FIXME: function-level try blocks are broken.
6109 " try : t(0) {\n"
6110 " } catch (...) {\n"
6111 " }\n"
6112 "};",
6113 "class A {\n"
6114 " A()\n : t(0) {}\n"
6115 " A(int i)\n noexcept() : {}\n"
6116 " A(X x)\n"
6117 " try : t(0) {} catch (...) {}\n"
6118 "};");
6119 FormatStyle Style = getLLVMStyle();
6120 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6121 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6122 Style.BraceWrapping.AfterFunction = true;
6123 verifyFormat("void f()\n"
6124 "try\n"
6125 "{\n"
6126 "}",
6127 "void f() try {\n"
6128 "}",
6129 Style);
6130 verifyFormat("class SomeClass {\n"
6131 "public:\n"
6132 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6133 "};",
6134 "class SomeClass {\n"
6135 "public:\n"
6136 " SomeClass()\n"
6137 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6138 "};");
6139 verifyFormat("class SomeClass {\n"
6140 "public:\n"
6141 " SomeClass()\n"
6142 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6143 "};",
6144 "class SomeClass {\n"
6145 "public:\n"
6146 " SomeClass()\n"
6147 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6148 "};",
6149 getLLVMStyleWithColumns(40));
6150
6151 verifyFormat("MACRO(>)");
6152
6153 // Some macros contain an implicit semicolon.
6154 Style = getLLVMStyle();
6155 Style.StatementMacros.push_back("FOO");
6156 verifyFormat("FOO(a) int b = 0;");
6157 verifyFormat("FOO(a)\n"
6158 "int b = 0;",
6159 Style);
6160 verifyFormat("FOO(a);\n"
6161 "int b = 0;",
6162 Style);
6163 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6164 "int b = 0;",
6165 Style);
6166 verifyFormat("FOO()\n"
6167 "int b = 0;",
6168 Style);
6169 verifyFormat("FOO\n"
6170 "int b = 0;",
6171 Style);
6172 verifyFormat("void f() {\n"
6173 " FOO(a)\n"
6174 " return a;\n"
6175 "}",
6176 Style);
6177 verifyFormat("FOO(a)\n"
6178 "FOO(b)",
6179 Style);
6180 verifyFormat("int a = 0;\n"
6181 "FOO(b)\n"
6182 "int c = 0;",
6183 Style);
6184 verifyFormat("int a = 0;\n"
6185 "int x = FOO(a)\n"
6186 "int b = 0;",
6187 Style);
6188 verifyFormat("void foo(int a) { FOO(a) }\n"
6189 "uint32_t bar() {}",
6190 Style);
6191 }
6192
6193 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6194 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
6195
6196 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6197 ZeroColumn);
6198 }
6199
6200 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6201 verifyFormat("#define A \\\n"
6202 " f({ \\\n"
6203 " g(); \\\n"
6204 " });",
6205 getLLVMStyleWithColumns(11));
6206 }
6207
6208 TEST_F(FormatTest, IndentPreprocessorDirectives) {
6209 FormatStyle Style = getLLVMStyleWithColumns(40);
6210 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6211 verifyFormat("#ifdef _WIN32\n"
6212 "#define A 0\n"
6213 "#ifdef VAR2\n"
6214 "#define B 1\n"
6215 "#include <someheader.h>\n"
6216 "#define MACRO \\\n"
6217 " some_very_long_func_aaaaaaaaaa();\n"
6218 "#endif\n"
6219 "#else\n"
6220 "#define A 1\n"
6221 "#endif",
6222 Style);
6223 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6224 verifyFormat("#if 1\n"
6225 "# define __STR(x) #x\n"
6226 "#endif",
6227 Style);
6228 verifyFormat("#ifdef _WIN32\n"
6229 "# define A 0\n"
6230 "# ifdef VAR2\n"
6231 "# define B 1\n"
6232 "# include <someheader.h>\n"
6233 "# define MACRO \\\n"
6234 " some_very_long_func_aaaaaaaaaa();\n"
6235 "# endif\n"
6236 "#else\n"
6237 "# define A 1\n"
6238 "#endif",
6239 Style);
6240 verifyFormat("#if A\n"
6241 "# define MACRO \\\n"
6242 " void a(int x) { \\\n"
6243 " b(); \\\n"
6244 " c(); \\\n"
6245 " d(); \\\n"
6246 " e(); \\\n"
6247 " f(); \\\n"
6248 " }\n"
6249 "#endif",
6250 Style);
6251 // Comments before include guard.
6252 verifyFormat("// file comment\n"
6253 "// file comment\n"
6254 "#ifndef HEADER_H\n"
6255 "#define HEADER_H\n"
6256 "code();\n"
6257 "#endif",
6258 Style);
6259 // Test with include guards.
6260 verifyFormat("#ifndef HEADER_H\n"
6261 "#define HEADER_H\n"
6262 "code();\n"
6263 "#endif",
6264 Style);
6265 // Include guards must have a #define with the same variable immediately
6266 // after #ifndef.
6267 verifyFormat("#ifndef NOT_GUARD\n"
6268 "# define FOO\n"
6269 "code();\n"
6270 "#endif",
6271 Style);
6272
6273 // Include guards must cover the entire file.
6274 verifyFormat("code();\n"
6275 "code();\n"
6276 "#ifndef NOT_GUARD\n"
6277 "# define NOT_GUARD\n"
6278 "code();\n"
6279 "#endif",
6280 Style);
6281 verifyFormat("#ifndef NOT_GUARD\n"
6282 "# define NOT_GUARD\n"
6283 "code();\n"
6284 "#endif\n"
6285 "code();",
6286 Style);
6287 // Test with trailing blank lines.
6288 verifyFormat("#ifndef HEADER_H\n"
6289 "#define HEADER_H\n"
6290 "code();\n"
6291 "#endif",
6292 Style);
6293 // Include guards don't have #else.
6294 verifyFormat("#ifndef NOT_GUARD\n"
6295 "# define NOT_GUARD\n"
6296 "code();\n"
6297 "#else\n"
6298 "#endif",
6299 Style);
6300 verifyFormat("#ifndef NOT_GUARD\n"
6301 "# define NOT_GUARD\n"
6302 "code();\n"
6303 "#elif FOO\n"
6304 "#endif",
6305 Style);
6306 // Non-identifier #define after potential include guard.
6307 verifyFormat("#ifndef FOO\n"
6308 "# define 1\n"
6309 "#endif",
6310 Style);
6311 // #if closes past last non-preprocessor line.
6312 verifyFormat("#ifndef FOO\n"
6313 "#define FOO\n"
6314 "#if 1\n"
6315 "int i;\n"
6316 "# define A 0\n"
6317 "#endif\n"
6318 "#endif",
6319 Style);
6320 // Don't crash if there is an #elif directive without a condition.
6321 verifyFormat("#if 1\n"
6322 "int x;\n"
6323 "#elif\n"
6324 "int y;\n"
6325 "#else\n"
6326 "int z;\n"
6327 "#endif",
6328 Style);
6329 // FIXME: This doesn't handle the case where there's code between the
6330 // #ifndef and #define but all other conditions hold. This is because when
6331 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6332 // previous code line yet, so we can't detect it.
6333 verifyFormat("#ifndef NOT_GUARD\n"
6334 "code();\n"
6335 "#define NOT_GUARD\n"
6336 "code();\n"
6337 "#endif",
6338 "#ifndef NOT_GUARD\n"
6339 "code();\n"
6340 "# define NOT_GUARD\n"
6341 "code();\n"
6342 "#endif",
6343 Style);
6344 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6345 // be outside an include guard. Examples are #pragma once and
6346 // #pragma GCC diagnostic, or anything else that does not change the meaning
6347 // of the file if it's included multiple times.
6348 verifyFormat("#ifdef WIN32\n"
6349 "# pragma once\n"
6350 "#endif\n"
6351 "#ifndef HEADER_H\n"
6352 "# define HEADER_H\n"
6353 "code();\n"
6354 "#endif",
6355 "#ifdef WIN32\n"
6356 "# pragma once\n"
6357 "#endif\n"
6358 "#ifndef HEADER_H\n"
6359 "#define HEADER_H\n"
6360 "code();\n"
6361 "#endif",
6362 Style);
6363 // FIXME: This does not detect when there is a single non-preprocessor line
6364 // in front of an include-guard-like structure where other conditions hold
6365 // because ScopedLineState hides the line.
6366 verifyFormat("code();\n"
6367 "#ifndef HEADER_H\n"
6368 "#define HEADER_H\n"
6369 "code();\n"
6370 "#endif",
6371 "code();\n"
6372 "#ifndef HEADER_H\n"
6373 "# define HEADER_H\n"
6374 "code();\n"
6375 "#endif",
6376 Style);
6377 // Keep comments aligned with #, otherwise indent comments normally. These
6378 // tests cannot use verifyFormat because messUp manipulates leading
6379 // whitespace.
6380 {
6381 const char *Expected = ""
6382 "void f() {\n"
6383 "#if 1\n"
6384 "// Preprocessor aligned.\n"
6385 "# define A 0\n"
6386 " // Code. Separated by blank line.\n"
6387 "\n"
6388 "# define B 0\n"
6389 " // Code. Not aligned with #\n"
6390 "# define C 0\n"
6391 "#endif";
6392 const char *ToFormat = ""
6393 "void f() {\n"
6394 "#if 1\n"
6395 "// Preprocessor aligned.\n"
6396 "# define A 0\n"
6397 "// Code. Separated by blank line.\n"
6398 "\n"
6399 "# define B 0\n"
6400 " // Code. Not aligned with #\n"
6401 "# define C 0\n"
6402 "#endif";
6403 verifyFormat(Expected, ToFormat, Style);
6404 verifyNoChange(Expected, Style);
6405 }
6406 // Keep block quotes aligned.
6407 {
6408 const char *Expected = ""
6409 "void f() {\n"
6410 "#if 1\n"
6411 "/* Preprocessor aligned. */\n"
6412 "# define A 0\n"
6413 " /* Code. Separated by blank line. */\n"
6414 "\n"
6415 "# define B 0\n"
6416 " /* Code. Not aligned with # */\n"
6417 "# define C 0\n"
6418 "#endif";
6419 const char *ToFormat = ""
6420 "void f() {\n"
6421 "#if 1\n"
6422 "/* Preprocessor aligned. */\n"
6423 "# define A 0\n"
6424 "/* Code. Separated by blank line. */\n"
6425 "\n"
6426 "# define B 0\n"
6427 " /* Code. Not aligned with # */\n"
6428 "# define C 0\n"
6429 "#endif";
6430 verifyFormat(Expected, ToFormat, Style);
6431 verifyNoChange(Expected, Style);
6432 }
6433 // Keep comments aligned with un-indented directives.
6434 {
6435 const char *Expected = ""
6436 "void f() {\n"
6437 "// Preprocessor aligned.\n"
6438 "#define A 0\n"
6439 " // Code. Separated by blank line.\n"
6440 "\n"
6441 "#define B 0\n"
6442 " // Code. Not aligned with #\n"
6443 "#define C 0\n";
6444 const char *ToFormat = ""
6445 "void f() {\n"
6446 "// Preprocessor aligned.\n"
6447 "#define A 0\n"
6448 "// Code. Separated by blank line.\n"
6449 "\n"
6450 "#define B 0\n"
6451 " // Code. Not aligned with #\n"
6452 "#define C 0\n";
6453 verifyFormat(Expected, ToFormat, Style);
6454 verifyNoChange(Expected, Style);
6455 }
6456 // Test AfterHash with tabs.
6457 {
6458 FormatStyle Tabbed = Style;
6459 Tabbed.UseTab = FormatStyle::UT_Always;
6460 Tabbed.IndentWidth = 8;
6461 Tabbed.TabWidth = 8;
6462 verifyFormat("#ifdef _WIN32\n"
6463 "#\tdefine A 0\n"
6464 "#\tifdef VAR2\n"
6465 "#\t\tdefine B 1\n"
6466 "#\t\tinclude <someheader.h>\n"
6467 "#\t\tdefine MACRO \\\n"
6468 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6469 "#\tendif\n"
6470 "#else\n"
6471 "#\tdefine A 1\n"
6472 "#endif",
6473 Tabbed);
6474 }
6475
6476 // Regression test: Multiline-macro inside include guards.
6477 verifyFormat("#ifndef HEADER_H\n"
6478 "#define HEADER_H\n"
6479 "#define A() \\\n"
6480 " int i; \\\n"
6481 " int j;\n"
6482 "#endif // HEADER_H",
6483 getLLVMStyleWithColumns(20));
6484
6485 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6486 // Basic before hash indent tests
6487 verifyFormat("#ifdef _WIN32\n"
6488 " #define A 0\n"
6489 " #ifdef VAR2\n"
6490 " #define B 1\n"
6491 " #include <someheader.h>\n"
6492 " #define MACRO \\\n"
6493 " some_very_long_func_aaaaaaaaaa();\n"
6494 " #endif\n"
6495 "#else\n"
6496 " #define A 1\n"
6497 "#endif",
6498 Style);
6499 verifyFormat("#if A\n"
6500 " #define MACRO \\\n"
6501 " void a(int x) { \\\n"
6502 " b(); \\\n"
6503 " c(); \\\n"
6504 " d(); \\\n"
6505 " e(); \\\n"
6506 " f(); \\\n"
6507 " }\n"
6508 "#endif",
6509 Style);
6510 // Keep comments aligned with indented directives. These
6511 // tests cannot use verifyFormat because messUp manipulates leading
6512 // whitespace.
6513 {
6514 const char *Expected = "void f() {\n"
6515 "// Aligned to preprocessor.\n"
6516 "#if 1\n"
6517 " // Aligned to code.\n"
6518 " int a;\n"
6519 " #if 1\n"
6520 " // Aligned to preprocessor.\n"
6521 " #define A 0\n"
6522 " // Aligned to code.\n"
6523 " int b;\n"
6524 " #endif\n"
6525 "#endif\n"
6526 "}";
6527 const char *ToFormat = "void f() {\n"
6528 "// Aligned to preprocessor.\n"
6529 "#if 1\n"
6530 "// Aligned to code.\n"
6531 "int a;\n"
6532 "#if 1\n"
6533 "// Aligned to preprocessor.\n"
6534 "#define A 0\n"
6535 "// Aligned to code.\n"
6536 "int b;\n"
6537 "#endif\n"
6538 "#endif\n"
6539 "}";
6540 verifyFormat(Expected, ToFormat, Style);
6541 verifyNoChange(Expected, Style);
6542 }
6543 {
6544 const char *Expected = "void f() {\n"
6545 "/* Aligned to preprocessor. */\n"
6546 "#if 1\n"
6547 " /* Aligned to code. */\n"
6548 " int a;\n"
6549 " #if 1\n"
6550 " /* Aligned to preprocessor. */\n"
6551 " #define A 0\n"
6552 " /* Aligned to code. */\n"
6553 " int b;\n"
6554 " #endif\n"
6555 "#endif\n"
6556 "}";
6557 const char *ToFormat = "void f() {\n"
6558 "/* Aligned to preprocessor. */\n"
6559 "#if 1\n"
6560 "/* Aligned to code. */\n"
6561 "int a;\n"
6562 "#if 1\n"
6563 "/* Aligned to preprocessor. */\n"
6564 "#define A 0\n"
6565 "/* Aligned to code. */\n"
6566 "int b;\n"
6567 "#endif\n"
6568 "#endif\n"
6569 "}";
6570 verifyFormat(Expected, ToFormat, Style);
6571 verifyNoChange(Expected, Style);
6572 }
6573
6574 // Test single comment before preprocessor
6575 verifyFormat("// Comment\n"
6576 "\n"
6577 "#if 1\n"
6578 "#endif",
6579 Style);
6580
6581 verifyFormat("#ifndef ABCDE\n"
6582 " #define ABCDE 0\n"
6583 "#endif\n"
6584 "\n"
6585 "#define FGHIJK",
6586 "#ifndef ABCDE\n"
6587 "#define ABCDE 0\n"
6588 "#endif\n"
6589 "\n"
6590 "#define FGHIJK",
6591 Style);
6592 }
6593
6594 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6595 FormatStyle Style = getLLVMStyle();
6596 Style.AlignConsecutiveAssignments.Enabled = true;
6597 Style.AlignConsecutiveDeclarations.Enabled = true;
6598
6599 // Test with just #if blocks.
6600 verifyFormat("void f1() {\n"
6601 "#if 1\n"
6602 " int foo = 1;\n"
6603 " int foobar = 2;\n"
6604 "#endif\n"
6605 "}\n"
6606 "#if 1\n"
6607 "int baz = 3;\n"
6608 "#endif\n"
6609 "void f2() {\n"
6610 "#if 1\n"
6611 " char *foobarbaz = \"foobarbaz\";\n"
6612 " int quux = 4;\n"
6613 "}",
6614 Style);
6615
6616 // Test with just #else blocks.
6617 verifyFormat("void f1() {\n"
6618 "#if 1\n"
6619 "#else\n"
6620 " int foo = 1;\n"
6621 " int foobar = 2;\n"
6622 "#endif\n"
6623 "}\n"
6624 "#if 1\n"
6625 "#else\n"
6626 "int baz = 3;\n"
6627 "#endif\n"
6628 "void f2() {\n"
6629 "#if 1\n"
6630 "#else\n"
6631 " char *foobarbaz = \"foobarbaz\";\n"
6632 " int quux = 4;\n"
6633 "}",
6634 Style);
6635 verifyFormat("auto foo = [] { return; };\n"
6636 "#if FOO\n"
6637 "#else\n"
6638 "count = bar;\n"
6639 "mbid = bid;\n"
6640 "#endif",
6641 Style);
6642
6643 // Test with a mix of #if and #else blocks.
6644 verifyFormat("void f1() {\n"
6645 "#if 1\n"
6646 "#else\n"
6647 " int foo = 1;\n"
6648 " int foobar = 2;\n"
6649 "#endif\n"
6650 "}\n"
6651 "#if 1\n"
6652 "int baz = 3;\n"
6653 "#endif\n"
6654 "void f2() {\n"
6655 "#if 1\n"
6656 "#else\n"
6657 " // prevent alignment with #else in f1\n"
6658 " char *foobarbaz = \"foobarbaz\";\n"
6659 " int quux = 4;\n"
6660 "}",
6661 Style);
6662
6663 // Test with nested #if and #else blocks.
6664 verifyFormat("void f1() {\n"
6665 "#if 1\n"
6666 "#else\n"
6667 "#if 2\n"
6668 "#else\n"
6669 " int foo = 1;\n"
6670 " int foobar = 2;\n"
6671 "#endif\n"
6672 "#endif\n"
6673 "}\n"
6674 "#if 1\n"
6675 "#else\n"
6676 "#if 2\n"
6677 "int baz = 3;\n"
6678 "#endif\n"
6679 "#endif\n"
6680 "void f2() {\n"
6681 "#if 1\n"
6682 "#if 2\n"
6683 "#else\n"
6684 " // prevent alignment with #else in f1\n"
6685 " char *foobarbaz = \"foobarbaz\";\n"
6686 " int quux = 4;\n"
6687 "#endif\n"
6688 "#endif\n"
6689 "}",
6690 Style);
6691
6692 verifyFormat("#if FOO\n"
6693 "int a = 1;\n"
6694 "#else\n"
6695 "int ab = 2;\n"
6696 "#endif\n"
6697 "#ifdef BAR\n"
6698 "int abc = 3;\n"
6699 "#elifdef BAZ\n"
6700 "int abcd = 4;\n"
6701 "#endif",
6702 Style);
6703
6704 verifyFormat("void f() {\n"
6705 " if (foo) {\n"
6706 "#if FOO\n"
6707 " int a = 1;\n"
6708 "#else\n"
6709 " bool a = true;\n"
6710 "#endif\n"
6711 " int abc = 3;\n"
6712 "#ifndef BAR\n"
6713 " int abcd = 4;\n"
6714 "#elif BAZ\n"
6715 " bool abcd = true;\n"
6716 "#endif\n"
6717 " }\n"
6718 "}",
6719 Style);
6720
6721 verifyFormat("void f() {\n"
6722 "#if FOO\n"
6723 " a = 1;\n"
6724 "#else\n"
6725 " ab = 2;\n"
6726 "#endif\n"
6727 "}\n"
6728 "void g() {\n"
6729 "#if BAR\n"
6730 " abc = 3;\n"
6731 "#elifndef BAZ\n"
6732 " abcd = 4;\n"
6733 "#endif\n"
6734 "}",
6735 Style);
6736 }
6737
6738 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6739 verifyFormat("{\n"
6740 " {\n"
6741 " a #c;\n"
6742 " }\n"
6743 "}");
6744 }
6745
6746 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6747 verifyFormat("#define A \\\n { \\\n {\nint i;",
6748 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6749 verifyFormat("#define A \\\n } \\\n }\nint i;",
6750 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6751 }
6752
6753 TEST_F(FormatTest, EscapedNewlines) {
6754 FormatStyle Narrow = getLLVMStyleWithColumns(11);
6755 verifyFormat("#define A \\\n int i; \\\n int j;",
6756 "#define A \\\nint i;\\\n int j;", Narrow);
6757 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6758 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6759 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6760 verifyNoChange("<a\n\\\\\n>");
6761
6762 FormatStyle AlignLeft = getLLVMStyle();
6763 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6764 verifyFormat("#define MACRO(x) \\\n"
6765 "private: \\\n"
6766 " int x(int a);",
6767 AlignLeft);
6768
6769 // Escaped with a trigraph. The program just has to avoid crashing.
6770 verifyNoCrash("#define A \?\?/\n"
6771 "int i;\?\?/\n"
6772 " int j;");
6773 verifyNoCrash("#define A \?\?/\r\n"
6774 "int i;\?\?/\r\n"
6775 " int j;");
6776 verifyNoCrash("#define A \?\?/\n"
6777 "int i;",
6778 getGoogleStyle(FormatStyle::LK_CSharp));
6779
6780 // CRLF line endings
6781 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6782 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6783 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6784 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6785 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6786 verifyNoChange("<a\r\n\\\\\r\n>");
6787 verifyFormat("#define MACRO(x) \\\r\n"
6788 "private: \\\r\n"
6789 " int x(int a);",
6790 AlignLeft);
6791
6792 constexpr StringRef Code("#define A \\\n"
6793 " int a123; \\\n"
6794 " int a; \\\n"
6795 " int a1234;");
6796 verifyFormat(Code, AlignLeft);
6797
6798 constexpr StringRef Code2("#define A \\\n"
6799 " int a123; \\\n"
6800 " int a; \\\n"
6801 " int a1234;");
6802 auto LastLine = getLLVMStyle();
6803 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6804 verifyFormat(Code2, LastLine);
6805
6806 LastLine.ColumnLimit = 13;
6807 verifyFormat(Code, LastLine);
6808
6809 LastLine.ColumnLimit = 0;
6810 verifyFormat(Code2, LastLine);
6811
6812 FormatStyle DontAlign = getLLVMStyle();
6813 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6814 DontAlign.MaxEmptyLinesToKeep = 3;
6815 // FIXME: can't use verifyFormat here because the newline before
6816 // "public:" is not inserted the first time it's reformatted
6817 verifyNoChange("#define A \\\n"
6818 " class Foo { \\\n"
6819 " void bar(); \\\n"
6820 "\\\n"
6821 "\\\n"
6822 "\\\n"
6823 " public: \\\n"
6824 " void baz(); \\\n"
6825 " };",
6826 DontAlign);
6827 }
6828
6829 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6830 verifyFormat("#define A \\\n"
6831 " int v( \\\n"
6832 " a); \\\n"
6833 " int i;",
6834 getLLVMStyleWithColumns(11));
6835 }
6836
6837 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6838 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6839 " \\\n"
6840 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6841 "\n"
6842 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6843 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6844 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6845 "\\\n"
6846 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6847 " \n"
6848 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6849 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6850 }
6851
6852 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6853 verifyFormat("int\n"
6854 "#define A\n"
6855 " a;",
6856 "int\n#define A\na;");
6857 verifyFormat("functionCallTo(\n"
6858 " someOtherFunction(\n"
6859 " withSomeParameters, whichInSequence,\n"
6860 " areLongerThanALine(andAnotherCall,\n"
6861 "#define A B\n"
6862 " withMoreParamters,\n"
6863 " whichStronglyInfluenceTheLayout),\n"
6864 " andMoreParameters),\n"
6865 " trailing);",
6866 getLLVMStyleWithColumns(69));
6867 verifyFormat("Foo::Foo()\n"
6868 "#ifdef BAR\n"
6869 " : baz(0)\n"
6870 "#endif\n"
6871 "{\n"
6872 "}");
6873 verifyFormat("void f() {\n"
6874 " if (true)\n"
6875 "#ifdef A\n"
6876 " f(42);\n"
6877 " x();\n"
6878 "#else\n"
6879 " g();\n"
6880 " x();\n"
6881 "#endif\n"
6882 "}");
6883 verifyFormat("void f(param1, param2,\n"
6884 " param3,\n"
6885 "#ifdef A\n"
6886 " param4(param5,\n"
6887 "#ifdef A1\n"
6888 " param6,\n"
6889 "#ifdef A2\n"
6890 " param7),\n"
6891 "#else\n"
6892 " param8),\n"
6893 " param9,\n"
6894 "#endif\n"
6895 " param10,\n"
6896 "#endif\n"
6897 " param11)\n"
6898 "#else\n"
6899 " param12)\n"
6900 "#endif\n"
6901 "{\n"
6902 " x();\n"
6903 "}",
6904 getLLVMStyleWithColumns(28));
6905 verifyFormat("#if 1\n"
6906 "int i;");
6907 verifyFormat("#if 1\n"
6908 "#endif\n"
6909 "#if 1\n"
6910 "#else\n"
6911 "#endif");
6912 verifyFormat("DEBUG({\n"
6913 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6915 "});\n"
6916 "#if a\n"
6917 "#else\n"
6918 "#endif");
6919
6920 verifyIncompleteFormat("void f(\n"
6921 "#if A\n"
6922 ");\n"
6923 "#else\n"
6924 "#endif");
6925
6926 // Verify that indentation is correct when there is an `#if 0` with an
6927 // `#else`.
6928 verifyFormat("#if 0\n"
6929 "{\n"
6930 "#else\n"
6931 "{\n"
6932 "#endif\n"
6933 " x;\n"
6934 "}");
6935
6936 verifyFormat("#if 0\n"
6937 "#endif\n"
6938 "#if X\n"
6939 "int something_fairly_long; // Align here please\n"
6940 "#endif // Should be aligned");
6941
6942 verifyFormat("#if 0\n"
6943 "#endif\n"
6944 "#if X\n"
6945 "#else // Align\n"
6946 ";\n"
6947 "#endif // Align");
6948
6949 verifyFormat("void SomeFunction(int param1,\n"
6950 " template <\n"
6951 "#ifdef A\n"
6952 "#if 0\n"
6953 "#endif\n"
6954 " MyType<Some>>\n"
6955 "#else\n"
6956 " Type1, Type2>\n"
6957 "#endif\n"
6958 " param2,\n"
6959 " param3) {\n"
6960 " f();\n"
6961 "}");
6962
6963 verifyFormat("#ifdef __cplusplus\n"
6964 "extern \"C\"\n"
6965 "#endif\n"
6966 " void f();");
6967 }
6968
6969 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6970 verifyFormat("#endif\n"
6971 "#if B");
6972 }
6973
6974 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6975 FormatStyle SingleLine = getLLVMStyle();
6976 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6977 verifyFormat("#if 0\n"
6978 "#elif 1\n"
6979 "#endif\n"
6980 "void foo() {\n"
6981 " if (test) foo2();\n"
6982 "}",
6983 SingleLine);
6984 }
6985
6986 TEST_F(FormatTest, LayoutBlockInsideParens) {
6987 verifyFormat("functionCall({ int i; });");
6988 verifyFormat("functionCall({\n"
6989 " int i;\n"
6990 " int j;\n"
6991 "});");
6992 verifyFormat("functionCall(\n"
6993 " {\n"
6994 " int i;\n"
6995 " int j;\n"
6996 " },\n"
6997 " aaaa, bbbb, cccc);");
6998 verifyFormat("functionA(functionB({\n"
6999 " int i;\n"
7000 " int j;\n"
7001 " }),\n"
7002 " aaaa, bbbb, cccc);");
7003 verifyFormat("functionCall(\n"
7004 " {\n"
7005 " int i;\n"
7006 " int j;\n"
7007 " },\n"
7008 " aaaa, bbbb, // comment\n"
7009 " cccc);");
7010 verifyFormat("functionA(functionB({\n"
7011 " int i;\n"
7012 " int j;\n"
7013 " }),\n"
7014 " aaaa, bbbb, // comment\n"
7015 " cccc);");
7016 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
7017 verifyFormat("functionCall(aaaa, bbbb, {\n"
7018 " int i;\n"
7019 " int j;\n"
7020 "});");
7021 verifyFormat(
7022 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
7023 " {\n"
7024 " int i; // break\n"
7025 " },\n"
7026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
7027 " ccccccccccccccccc));");
7028 verifyFormat("DEBUG({\n"
7029 " if (a)\n"
7030 " f();\n"
7031 "});");
7032 }
7033
7034 TEST_F(FormatTest, LayoutBlockInsideStatement) {
7035 verifyFormat("SOME_MACRO { int i; }\n"
7036 "int i;",
7037 " SOME_MACRO {int i;} int i;");
7038 }
7039
7040 TEST_F(FormatTest, LayoutNestedBlocks) {
7041 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
7042 " struct s {\n"
7043 " int i;\n"
7044 " };\n"
7045 " s kBitsToOs[] = {{10}};\n"
7046 " for (int i = 0; i < 10; ++i)\n"
7047 " return;\n"
7048 "}");
7049 verifyFormat("call(parameter, {\n"
7050 " something();\n"
7051 " // Comment using all columns.\n"
7052 " somethingelse();\n"
7053 "});",
7054 getLLVMStyleWithColumns(40));
7055 verifyFormat("DEBUG( //\n"
7056 " { f(); }, a);");
7057 verifyFormat("DEBUG( //\n"
7058 " {\n"
7059 " f(); //\n"
7060 " },\n"
7061 " a);");
7062
7063 verifyFormat("call(parameter, {\n"
7064 " something();\n"
7065 " // Comment too\n"
7066 " // looooooooooong.\n"
7067 " somethingElse();\n"
7068 "});",
7069 "call(parameter, {\n"
7070 " something();\n"
7071 " // Comment too looooooooooong.\n"
7072 " somethingElse();\n"
7073 "});",
7074 getLLVMStyleWithColumns(29));
7075 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
7076 verifyFormat("DEBUG({ // comment\n"
7077 " int i;\n"
7078 "});",
7079 "DEBUG({ // comment\n"
7080 "int i;\n"
7081 "});");
7082 verifyFormat("DEBUG({\n"
7083 " int i;\n"
7084 "\n"
7085 " // comment\n"
7086 " int j;\n"
7087 "});",
7088 "DEBUG({\n"
7089 " int i;\n"
7090 "\n"
7091 " // comment\n"
7092 " int j;\n"
7093 "});");
7094
7095 verifyFormat("DEBUG({\n"
7096 " if (a)\n"
7097 " return;\n"
7098 "});");
7099 verifyGoogleFormat("DEBUG({\n"
7100 " if (a) return;\n"
7101 "});");
7102 FormatStyle Style = getGoogleStyle();
7103 Style.ColumnLimit = 45;
7104 verifyFormat("Debug(\n"
7105 " aaaaa,\n"
7106 " {\n"
7107 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
7108 " },\n"
7109 " a);",
7110 Style);
7111
7112 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
7113
7114 verifyNoCrash("^{v^{a}}");
7115 }
7116
7117 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
7118 verifyFormat("#define MACRO() \\\n"
7119 " Debug(aaa, /* force line break */ \\\n"
7120 " { \\\n"
7121 " int i; \\\n"
7122 " int j; \\\n"
7123 " })",
7124 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
7125 " { int i; int j; })",
7126 getGoogleStyle());
7127
7128 verifyFormat("#define A \\\n"
7129 " [] { \\\n"
7130 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
7131 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
7132 " }",
7133 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
7134 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
7135 getGoogleStyle());
7136 }
7137
7138 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
7139 verifyFormat("enum E {};");
7140 verifyFormat("enum E {}");
7141 FormatStyle Style = getLLVMStyle();
7142 Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
7143 verifyFormat("void f() { }", "void f() {}", Style);
7144 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
7145 verifyFormat("{ }", Style);
7146 verifyFormat("while (true) { }", "while (true) {}", Style);
7147 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7148 Style.BraceWrapping.BeforeElse = false;
7149 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
7150 verifyFormat("if (a)\n"
7151 "{\n"
7152 "} else if (b)\n"
7153 "{\n"
7154 "} else\n"
7155 "{ }",
7156 Style);
7157 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
7158 verifyFormat("if (a) {\n"
7159 "} else if (b) {\n"
7160 "} else {\n"
7161 "}",
7162 Style);
7163 Style.BraceWrapping.BeforeElse = true;
7164 verifyFormat("if (a) { }\n"
7165 "else if (b) { }\n"
7166 "else { }",
7167 Style);
7168
7169 Style = getLLVMStyle(FormatStyle::LK_CSharp);
7170 Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
7171 verifyFormat("Event += () => { };", Style);
7172 }
7173
7174 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
7175 FormatStyle Style = getLLVMStyle();
7176 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
7177 Style.MacroBlockEnd = "^[A-Z_]+_END$";
7178 verifyFormat("FOO_BEGIN\n"
7179 " FOO_ENTRY\n"
7180 "FOO_END",
7181 Style);
7182 verifyFormat("FOO_BEGIN\n"
7183 " NESTED_FOO_BEGIN\n"
7184 " NESTED_FOO_ENTRY\n"
7185 " NESTED_FOO_END\n"
7186 "FOO_END",
7187 Style);
7188 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7189 " int x;\n"
7190 " x = 1;\n"
7191 "FOO_END(Baz)",
7192 Style);
7193
7194 Style.RemoveBracesLLVM = true;
7195 verifyNoCrash("for (;;)\n"
7196 " FOO_BEGIN\n"
7197 " foo();\n"
7198 " FOO_END",
7199 Style);
7200 }
7201
7202 //===----------------------------------------------------------------------===//
7203 // Line break tests.
7204 //===----------------------------------------------------------------------===//
7205
7206 TEST_F(FormatTest, PreventConfusingIndents) {
7207 verifyFormat(
7208 "void f() {\n"
7209 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7210 " parameter, parameter, parameter)),\n"
7211 " SecondLongCall(parameter));\n"
7212 "}");
7213 verifyFormat(
7214 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7215 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7217 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7218 verifyFormat(
7219 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7220 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7221 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7222 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7223 verifyFormat(
7224 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7225 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7227 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7228 verifyFormat("int a = bbbb && ccc &&\n"
7229 " fffff(\n"
7230 "#define A Just forcing a new line\n"
7231 " ddd);");
7232 }
7233
7234 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7235 verifyFormat(
7236 "bool aaaaaaa =\n"
7237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7238 " bbbbbbbb();");
7239 verifyFormat(
7240 "bool aaaaaaa =\n"
7241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7242 " bbbbbbbb();");
7243
7244 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7246 " ccccccccc == ddddddddddd;");
7247 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7249 " ccccccccc == ddddddddddd;");
7250 verifyFormat(
7251 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7253 " ccccccccc == ddddddddddd;");
7254
7255 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7256 " aaaaaa) &&\n"
7257 " bbbbbb && cccccc;");
7258 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7259 " aaaaaa) >>\n"
7260 " bbbbbb;");
7261 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7262 " SourceMgr.getSpellingColumnNumber(\n"
7263 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7264 " 1);");
7265
7266 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7267 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7268 " cccccc) {\n}");
7269 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7270 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7271 " cccccc) {\n}");
7272 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7273 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7274 " cccccc) {\n}");
7275 verifyFormat("b = a &&\n"
7276 " // Comment\n"
7277 " b.c && d;");
7278
7279 // If the LHS of a comparison is not a binary expression itself, the
7280 // additional linebreak confuses many people.
7281 verifyFormat(
7282 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7284 "}");
7285 verifyFormat(
7286 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7288 "}");
7289 verifyFormat(
7290 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7292 "}");
7293 verifyFormat(
7294 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7296 "}");
7297 // Even explicit parentheses stress the precedence enough to make the
7298 // additional break unnecessary.
7299 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7301 "}");
7302 // This cases is borderline, but with the indentation it is still readable.
7303 verifyFormat(
7304 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7305 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7307 "}",
7308 getLLVMStyleWithColumns(75));
7309
7310 // If the LHS is a binary expression, we should still use the additional break
7311 // as otherwise the formatting hides the operator precedence.
7312 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7314 " 5) {\n"
7315 "}");
7316 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7318 " 5) {\n"
7319 "}");
7320
7321 FormatStyle OnePerLine = getLLVMStyle();
7322 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7323 verifyFormat(
7324 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7327 OnePerLine);
7328
7329 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7330 " .aaa(aaaaaaaaaaaaa) *\n"
7331 " aaaaaaa +\n"
7332 " aaaaaaa;",
7333 getLLVMStyleWithColumns(40));
7334 }
7335
7336 TEST_F(FormatTest, ExpressionIndentation) {
7337 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7341 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7342 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7345 " ccccccccccccccccccccccccccccccccccccccccc;");
7346 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7349 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7350 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7353 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7354 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7357 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7358 verifyFormat("if () {\n"
7359 "} else if (aaaaa && bbbbb > // break\n"
7360 " ccccc) {\n"
7361 "}");
7362 verifyFormat("if () {\n"
7363 "} else if constexpr (aaaaa && bbbbb > // break\n"
7364 " ccccc) {\n"
7365 "}");
7366 verifyFormat("if () {\n"
7367 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7368 " ccccc) {\n"
7369 "}");
7370 verifyFormat("if () {\n"
7371 "} else if (aaaaa &&\n"
7372 " bbbbb > // break\n"
7373 " ccccc &&\n"
7374 " ddddd) {\n"
7375 "}");
7376
7377 // Presence of a trailing comment used to change indentation of b.
7378 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7379 " b;\n"
7380 "return aaaaaaaaaaaaaaaaaaa +\n"
7381 " b; //",
7382 getLLVMStyleWithColumns(30));
7383 }
7384
7385 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7386 // Not sure what the best system is here. Like this, the LHS can be found
7387 // immediately above an operator (everything with the same or a higher
7388 // indent). The RHS is aligned right of the operator and so compasses
7389 // everything until something with the same indent as the operator is found.
7390 // FIXME: Is this a good system?
7391 FormatStyle Style = getLLVMStyle();
7392 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7393 verifyFormat(
7394 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7395 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7396 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7397 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7398 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7399 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7400 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7401 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7402 " > ccccccccccccccccccccccccccccccccccccccccc;",
7403 Style);
7404 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7405 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7406 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7407 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7408 Style);
7409 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7410 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7411 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7412 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7413 Style);
7414 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7415 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7416 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7417 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7418 Style);
7419 verifyFormat("if () {\n"
7420 "} else if (aaaaa\n"
7421 " && bbbbb // break\n"
7422 " > ccccc) {\n"
7423 "}",
7424 Style);
7425 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7426 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7427 Style);
7428 verifyFormat("return (a)\n"
7429 " // comment\n"
7430 " + b;",
7431 Style);
7432 verifyFormat(
7433 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7434 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7435 " + cc;",
7436 Style);
7437
7438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7439 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7440 Style);
7441
7442 // Forced by comments.
7443 verifyFormat(
7444 "unsigned ContentSize =\n"
7445 " sizeof(int16_t) // DWARF ARange version number\n"
7446 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7447 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7448 " + sizeof(int8_t); // Segment Size (in bytes)");
7449
7450 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7451 " == boost::fusion::at_c<1>(iiii).second;",
7452 Style);
7453
7454 Style.ColumnLimit = 60;
7455 verifyFormat("zzzzzzzzzz\n"
7456 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7457 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7458 Style);
7459
7460 Style.ColumnLimit = 80;
7461 Style.IndentWidth = 4;
7462 Style.TabWidth = 4;
7463 Style.UseTab = FormatStyle::UT_Always;
7464 Style.AlignAfterOpenBracket = false;
7465 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7466 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7467 "\t&& (someOtherLongishConditionPart1\n"
7468 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7469 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7470 "(someOtherLongishConditionPart1 || "
7471 "someOtherEvenLongerNestedConditionPart2);",
7472 Style);
7473
7474 Style = getLLVMStyleWithColumns(20);
7475 Style.BreakAfterOpenBracketFunction = true;
7476 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7477 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7478 Style.ContinuationIndentWidth = 2;
7479 verifyFormat("struct Foo {\n"
7480 " Foo(\n"
7481 " int arg1,\n"
7482 " int arg2)\n"
7483 " : Base(\n"
7484 " arg1,\n"
7485 " arg2) {}\n"
7486 "};",
7487 Style);
7488 verifyFormat("return abc\n"
7489 " ? foo(\n"
7490 " a,\n"
7491 " b,\n"
7492 " bar(\n"
7493 " abc))\n"
7494 " : g(abc);",
7495 Style);
7496 }
7497
7498 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7499 FormatStyle Style = getLLVMStyle();
7500 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7501 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7502
7503 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7504 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7505 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7506 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7507 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7508 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7509 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7510 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7511 " > ccccccccccccccccccccccccccccccccccccccccc;",
7512 Style);
7513 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7515 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7516 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7517 Style);
7518 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7519 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7520 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7521 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7522 Style);
7523 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7524 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7525 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7526 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7527 Style);
7528 verifyFormat("if () {\n"
7529 "} else if (aaaaa\n"
7530 " && bbbbb // break\n"
7531 " > ccccc) {\n"
7532 "}",
7533 Style);
7534 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7535 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7536 Style);
7537 verifyFormat("return (a)\n"
7538 " // comment\n"
7539 " + b;",
7540 Style);
7541 verifyFormat(
7542 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7543 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7544 " + cc;",
7545 Style);
7546 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7547 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7548 " : 3333333333333333;",
7549 Style);
7550 verifyFormat(
7551 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7552 " : ccccccccccccccc ? dddddddddddddddddd\n"
7553 " : eeeeeeeeeeeeeeeeee)\n"
7554 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7555 " : 3333333333333333;",
7556 Style);
7557 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7558 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7559 Style);
7560
7561 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7562 " == boost::fusion::at_c<1>(iiii).second;",
7563 Style);
7564
7565 Style.ColumnLimit = 60;
7566 verifyFormat("zzzzzzzzzzzzz\n"
7567 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7568 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7569 Style);
7570
7571 // Forced by comments.
7572 Style.ColumnLimit = 80;
7573 verifyFormat(
7574 "unsigned ContentSize\n"
7575 " = sizeof(int16_t) // DWARF ARange version number\n"
7576 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7577 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7578 " + sizeof(int8_t); // Segment Size (in bytes)",
7579 Style);
7580
7581 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7582 verifyFormat(
7583 "unsigned ContentSize =\n"
7584 " sizeof(int16_t) // DWARF ARange version number\n"
7585 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7586 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7587 " + sizeof(int8_t); // Segment Size (in bytes)",
7588 Style);
7589
7590 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7591 verifyFormat(
7592 "unsigned ContentSize =\n"
7593 " sizeof(int16_t) // DWARF ARange version number\n"
7594 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7595 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7596 " + sizeof(int8_t); // Segment Size (in bytes)",
7597 Style);
7598 }
7599
7600 TEST_F(FormatTest, EnforcedOperatorWraps) {
7601 // Here we'd like to wrap after the || operators, but a comment is forcing an
7602 // earlier wrap.
7603 verifyFormat("bool x = aaaaa //\n"
7604 " || bbbbb\n"
7605 " //\n"
7606 " || cccc;");
7607 }
7608
7609 TEST_F(FormatTest, NoOperandAlignment) {
7610 FormatStyle Style = getLLVMStyle();
7611 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7612 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7615 Style);
7616 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7617 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7618 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7619 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7620 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7621 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7622 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7623 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7624 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7625 " > ccccccccccccccccccccccccccccccccccccccccc;",
7626 Style);
7627
7628 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7629 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7630 " + cc;",
7631 Style);
7632 verifyFormat("int a = aa\n"
7633 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7634 " * cccccccccccccccccccccccccccccccccccc;",
7635 Style);
7636
7637 Style.AlignAfterOpenBracket = false;
7638 verifyFormat("return (a > b\n"
7639 " // comment1\n"
7640 " // comment2\n"
7641 " || c);",
7642 Style);
7643 }
7644
7645 TEST_F(FormatTest, BreakingBeforeNonAssignmentOperators) {
7646 FormatStyle Style = getLLVMStyle();
7647 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7648 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7650 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7651 Style);
7652 }
7653
7654 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7655 FormatStyle Style = getLLVMStyleWithColumns(40);
7656 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7657 Style.BinPackArguments = false;
7658 verifyFormat("void test() {\n"
7659 " someFunction(\n"
7660 " this + argument + is + quite\n"
7661 " + long + so + it + gets + wrapped\n"
7662 " + but + remains + bin - packed);\n"
7663 "}",
7664 Style);
7665 verifyFormat("void test() {\n"
7666 " someFunction(arg1,\n"
7667 " this + argument + is\n"
7668 " + quite + long + so\n"
7669 " + it + gets + wrapped\n"
7670 " + but + remains + bin\n"
7671 " - packed,\n"
7672 " arg3);\n"
7673 "}",
7674 Style);
7675 verifyFormat("void test() {\n"
7676 " someFunction(\n"
7677 " arg1,\n"
7678 " this + argument + has\n"
7679 " + anotherFunc(nested,\n"
7680 " calls + whose\n"
7681 " + arguments\n"
7682 " + are + also\n"
7683 " + wrapped,\n"
7684 " in + addition)\n"
7685 " + to + being + bin - packed,\n"
7686 " arg3);\n"
7687 "}",
7688 Style);
7689
7690 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7691 verifyFormat("void test() {\n"
7692 " someFunction(\n"
7693 " arg1,\n"
7694 " this + argument + has +\n"
7695 " anotherFunc(nested,\n"
7696 " calls + whose +\n"
7697 " arguments +\n"
7698 " are + also +\n"
7699 " wrapped,\n"
7700 " in + addition) +\n"
7701 " to + being + bin - packed,\n"
7702 " arg3);\n"
7703 "}",
7704 Style);
7705 }
7706
7707 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7708 auto Style = getLLVMStyleWithColumns(45);
7709 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7710 verifyFormat("bool b =\n"
7711 " is_default_constructible_v<hash<T>> and\n"
7712 " is_copy_constructible_v<hash<T>> and\n"
7713 " is_move_constructible_v<hash<T>> and\n"
7714 " is_copy_assignable_v<hash<T>> and\n"
7715 " is_move_assignable_v<hash<T>> and\n"
7716 " is_destructible_v<hash<T>> and\n"
7717 " is_swappable_v<hash<T>> and\n"
7718 " is_callable_v<hash<T>(T)>;",
7719 Style);
7720
7721 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7722 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7723 " and is_copy_constructible_v<hash<T>>\n"
7724 " and is_move_constructible_v<hash<T>>\n"
7725 " and is_copy_assignable_v<hash<T>>\n"
7726 " and is_move_assignable_v<hash<T>>\n"
7727 " and is_destructible_v<hash<T>>\n"
7728 " and is_swappable_v<hash<T>>\n"
7729 " and is_callable_v<hash<T>(T)>;",
7730 Style);
7731
7732 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7733 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7734 " and is_copy_constructible_v<hash<T>>\n"
7735 " and is_move_constructible_v<hash<T>>\n"
7736 " and is_copy_assignable_v<hash<T>>\n"
7737 " and is_move_assignable_v<hash<T>>\n"
7738 " and is_destructible_v<hash<T>>\n"
7739 " and is_swappable_v<hash<T>>\n"
7740 " and is_callable_v<hash<T>(T)>;",
7741 Style);
7742 }
7743
7744 TEST_F(FormatTest, ConstructorInitializers) {
7745 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7746 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7747 getLLVMStyleWithColumns(45));
7748 verifyFormat("Constructor()\n"
7749 " : Inttializer(FitsOnTheLine) {}",
7750 getLLVMStyleWithColumns(44));
7751 verifyFormat("Constructor()\n"
7752 " : Inttializer(FitsOnTheLine) {}",
7753 getLLVMStyleWithColumns(43));
7754
7755 verifyFormat("template <typename T>\n"
7756 "Constructor() : Initializer(FitsOnTheLine) {}",
7757 getLLVMStyleWithColumns(45));
7758
7759 verifyFormat(
7760 "SomeClass::Constructor()\n"
7761 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7762
7763 verifyFormat(
7764 "SomeClass::Constructor()\n"
7765 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7766 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7767 verifyFormat(
7768 "SomeClass::Constructor()\n"
7769 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7770 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7771 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7772 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7773 " : aaaaaaaaaa(aaaaaa) {}");
7774
7775 verifyFormat("Constructor()\n"
7776 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7777 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7778 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7779 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7780
7781 verifyFormat("Constructor()\n"
7782 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7784
7785 verifyFormat("Constructor(int Parameter = 0)\n"
7786 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7787 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7788 verifyFormat("Constructor()\n"
7789 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7790 "}",
7791 getLLVMStyleWithColumns(60));
7792 verifyFormat("Constructor()\n"
7793 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7794 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7795
7796 // Here a line could be saved by splitting the second initializer onto two
7797 // lines, but that is not desirable.
7798 verifyFormat("Constructor()\n"
7799 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7800 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7801 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7802
7803 FormatStyle OnePerLine = getLLVMStyle();
7804 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7805 verifyFormat("MyClass::MyClass()\n"
7806 " : a(a),\n"
7807 " b(b),\n"
7808 " c(c) {}",
7809 OnePerLine);
7810 verifyFormat("MyClass::MyClass()\n"
7811 " : a(a), // comment\n"
7812 " b(b),\n"
7813 " c(c) {}",
7814 OnePerLine);
7815 verifyFormat("MyClass::MyClass(int a)\n"
7816 " : b(a), // comment\n"
7817 " c(a + 1) { // lined up\n"
7818 "}",
7819 OnePerLine);
7820 verifyFormat("Constructor()\n"
7821 " : a(b, b, b) {}",
7822 OnePerLine);
7823 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7824 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7825 verifyFormat("SomeClass::Constructor()\n"
7826 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7827 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7828 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7829 OnePerLine);
7830 verifyFormat("SomeClass::Constructor()\n"
7831 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7832 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7833 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7834 OnePerLine);
7835 verifyFormat("MyClass::MyClass(int var)\n"
7836 " : some_var_(var), // 4 space indent\n"
7837 " some_other_var_(var + 1) { // lined up\n"
7838 "}",
7839 OnePerLine);
7840 verifyFormat("Constructor()\n"
7841 " : aaaaa(aaaaaa),\n"
7842 " aaaaa(aaaaaa),\n"
7843 " aaaaa(aaaaaa),\n"
7844 " aaaaa(aaaaaa),\n"
7845 " aaaaa(aaaaaa) {}",
7846 OnePerLine);
7847 verifyFormat("Constructor()\n"
7848 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7849 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7850 OnePerLine);
7851 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7852 verifyFormat(
7853 "Constructor()\n"
7854 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7855 " aaaaaaaaaaa().aaa(),\n"
7856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7857 OnePerLine);
7858 OnePerLine.ColumnLimit = 60;
7859 verifyFormat("Constructor()\n"
7860 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7861 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7862 OnePerLine);
7863
7864 verifyFormat("Constructor()\n"
7865 " : // Comment forcing unwanted break.\n"
7866 " aaaa(aaaa) {}",
7867 "Constructor() :\n"
7868 " // Comment forcing unwanted break.\n"
7869 " aaaa(aaaa) {}");
7870
7871 // Braced initializers with trailing commas.
7872 verifyFormat("MyClass::MyClass()\n"
7873 " : aaaa{\n"
7874 " 0,\n"
7875 " },\n"
7876 " bbbb{\n"
7877 " 0,\n"
7878 " } {}",
7879 "MyClass::MyClass():aaaa{0,},bbbb{0,}{}");
7880 }
7881
7882 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7883 FormatStyle Style = getLLVMStyleWithColumns(60);
7884 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7885 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7886
7887 for (int i = 0; i < 4; ++i) {
7888 // Test all combinations of parameters that should not have an effect.
7889 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7890 Style.AllowAllArgumentsOnNextLine = i & 2;
7891
7892 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7893 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7894 verifyFormat("Constructor()\n"
7895 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7896 Style);
7897 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7898
7899 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7900 verifyFormat("Constructor()\n"
7901 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7902 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7903 Style);
7904 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7905
7906 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7907 verifyFormat("Constructor()\n"
7908 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7909 Style);
7910 verifyFormat("Constructor()\n"
7911 " : a(a), b(b) {}",
7912 Style);
7913 verifyFormat("Constructor()\n"
7914 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7915 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7916 " , cccccccccccccccccccccc(c) {}",
7917 Style);
7918
7919 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7920 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7921 verifyFormat("Constructor()\n"
7922 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7923 Style);
7924
7925 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7926 verifyFormat("Constructor()\n"
7927 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7928 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7929 Style);
7930
7931 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7932 verifyFormat("Constructor()\n"
7933 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7934 Style);
7935 verifyFormat("Constructor()\n"
7936 " : a(a), b(b) {}",
7937 Style);
7938 verifyFormat("Constructor()\n"
7939 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7940 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7941 " cccccccccccccccccccccc(c) {}",
7942 Style);
7943
7944 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7945 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7946 verifyFormat("Constructor() :\n"
7947 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7948 Style);
7949
7950 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7951 verifyFormat("Constructor() :\n"
7952 " aaaaaaaaaaaaaaaaaa(a),\n"
7953 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7954 Style);
7955
7956 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7957 verifyFormat("Constructor() :\n"
7958 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7959 Style);
7960 verifyFormat("Constructor() :\n"
7961 " a(a), b(b) {}",
7962 Style);
7963 verifyFormat("Constructor() :\n"
7964 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7965 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7966 " cccccccccccccccccccccc(c) {}",
7967 Style);
7968 }
7969
7970 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7971 // AllowAllConstructorInitializersOnNextLine in all
7972 // BreakConstructorInitializers modes
7973 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7974 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7975 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7976 verifyFormat("SomeClassWithALongName::Constructor(\n"
7977 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7978 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7979 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7980 Style);
7981
7982 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7983 verifyFormat("SomeClassWithALongName::Constructor(\n"
7984 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7985 " int bbbbbbbbbbbbb,\n"
7986 " int cccccccccccccccc)\n"
7987 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7988 Style);
7989
7990 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7991 verifyFormat("SomeClassWithALongName::Constructor(\n"
7992 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7993 " int bbbbbbbbbbbbb,\n"
7994 " int cccccccccccccccc)\n"
7995 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7996 Style);
7997
7998 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7999 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8000 verifyFormat("SomeClassWithALongName::Constructor(\n"
8001 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8002 " int bbbbbbbbbbbbb)\n"
8003 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
8004 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
8005 Style);
8006
8007 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
8008
8009 Style.AllowAllParametersOfDeclarationOnNextLine = true;
8010 verifyFormat("SomeClassWithALongName::Constructor(\n"
8011 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
8012 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
8013 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
8014 Style);
8015
8016 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8017 verifyFormat("SomeClassWithALongName::Constructor(\n"
8018 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8019 " int bbbbbbbbbbbbb,\n"
8020 " int cccccccccccccccc)\n"
8021 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
8022 Style);
8023
8024 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8025 verifyFormat("SomeClassWithALongName::Constructor(\n"
8026 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8027 " int bbbbbbbbbbbbb,\n"
8028 " int cccccccccccccccc)\n"
8029 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
8030 Style);
8031
8032 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8033 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8034 verifyFormat("SomeClassWithALongName::Constructor(\n"
8035 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8036 " int bbbbbbbbbbbbb)\n"
8037 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
8038 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
8039 Style);
8040
8041 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8042 Style.AllowAllParametersOfDeclarationOnNextLine = true;
8043 verifyFormat("SomeClassWithALongName::Constructor(\n"
8044 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
8045 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8046 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
8047 Style);
8048
8049 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8050 verifyFormat("SomeClassWithALongName::Constructor(\n"
8051 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8052 " int bbbbbbbbbbbbb,\n"
8053 " int cccccccccccccccc) :\n"
8054 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
8055 Style);
8056
8057 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8058 verifyFormat("SomeClassWithALongName::Constructor(\n"
8059 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8060 " int bbbbbbbbbbbbb,\n"
8061 " int cccccccccccccccc) :\n"
8062 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
8063 Style);
8064
8065 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8066 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8067 verifyFormat("SomeClassWithALongName::Constructor(\n"
8068 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
8069 " int bbbbbbbbbbbbb) :\n"
8070 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8071 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
8072 Style);
8073
8074 Style = getLLVMStyleWithColumns(0);
8075 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8076 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
8077 verifyNoChange("Foo(Bar bar, Baz baz)\n"
8078 " : bar(bar), baz(baz) {}",
8079 Style);
8080 }
8081
8082 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
8083 FormatStyle Style = getLLVMStyleWithColumns(60);
8084 Style.BinPackArguments = false;
8085 for (int i = 0; i < 4; ++i) {
8086 // Test all combinations of parameters that should not have an effect.
8087 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
8088 Style.PackConstructorInitializers =
8089 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
8090
8091 Style.AllowAllArgumentsOnNextLine = true;
8092 verifyFormat("void foo() {\n"
8093 " FunctionCallWithReallyLongName(\n"
8094 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
8095 "}",
8096 Style);
8097 Style.AllowAllArgumentsOnNextLine = false;
8098 verifyFormat("void foo() {\n"
8099 " FunctionCallWithReallyLongName(\n"
8100 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8101 " bbbbbbbbbbbb);\n"
8102 "}",
8103 Style);
8104
8105 Style.AllowAllArgumentsOnNextLine = true;
8106 verifyFormat("void foo() {\n"
8107 " auto VariableWithReallyLongName = {\n"
8108 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
8109 "}",
8110 Style);
8111 Style.AllowAllArgumentsOnNextLine = false;
8112 verifyFormat("void foo() {\n"
8113 " auto VariableWithReallyLongName = {\n"
8114 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8115 " bbbbbbbbbbbb};\n"
8116 "}",
8117 Style);
8118 }
8119
8120 // This parameter should not affect declarations.
8121 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8122 Style.AllowAllArgumentsOnNextLine = false;
8123 Style.AllowAllParametersOfDeclarationOnNextLine = true;
8124 verifyFormat("void FunctionCallWithReallyLongName(\n"
8125 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
8126 Style);
8127 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8128 verifyFormat("void FunctionCallWithReallyLongName(\n"
8129 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
8130 " int bbbbbbbbbbbb);",
8131 Style);
8132 }
8133
8134 TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8135 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8136 "void emptyFunctionDefinition() {}\n"
8137 "void functionDefinition(int A, int B, int C) {}\n"
8138 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8139 verifyFormat(Input);
8140
8141 FormatStyle Style = getLLVMStyle();
8142 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8143 Style.BreakFunctionDefinitionParameters = true;
8144 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8145 "void emptyFunctionDefinition() {}\n"
8146 "void functionDefinition(\n"
8147 " int A, int B, int C) {}\n"
8148 "Class::Class(\n"
8149 " int A, int B)\n"
8150 " : m_A(A), m_B(B) {}",
8151 Input, Style);
8152
8153 // Test the style where all parameters are on their own lines.
8154 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8155 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8156 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8157 "void emptyFunctionDefinition() {}\n"
8158 "void functionDefinition(\n"
8159 " int A,\n"
8160 " int B,\n"
8161 " int C) {}\n"
8162 "Class::Class(\n"
8163 " int A,\n"
8164 " int B)\n"
8165 " : m_A(A), m_B(B) {}",
8166 Input, Style);
8167 }
8168
8169 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8170 FormatStyle Style = getLLVMStyle();
8171 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8172 /* Test the behaviour with long lines */
8173 Style.ColumnLimit = 40;
8174 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8175 " : : val);",
8176 Style);
8177 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8178 " : val1 : val2);",
8179 Style);
8180 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8181 " \"cpuid\\n\\t\"\n"
8182 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8183 " : \"=a\" : \"a\");",
8184 Style);
8185 Style.ColumnLimit = 80;
8186 verifyFormat("asm volatile(\"string\", : : val);", Style);
8187 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8188
8189 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8190 verifyFormat("asm volatile(\"string\",\n"
8191 " :\n"
8192 " : val);",
8193 Style);
8194 verifyFormat("asm volatile(\"string\",\n"
8195 " : val1\n"
8196 " : val2);",
8197 Style);
8198 /* Test the behaviour with long lines */
8199 Style.ColumnLimit = 40;
8200 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8201 " \"cpuid\\n\\t\"\n"
8202 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8203 " : \"=a\"(*rEAX)\n"
8204 " : \"a\"(value));",
8205 Style);
8206 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8207 " \"cpuid\\n\\t\"\n"
8208 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8209 " :\n"
8210 " : \"a\"(value));",
8211 Style);
8212 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8213 " :\n"
8214 " : val);",
8215 Style);
8216 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8217 " : val1\n"
8218 " : val2);",
8219 Style);
8220 }
8221
8222 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8223 FormatStyle Style = getLLVMStyle();
8224 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8225
8226 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8227 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8228 getStyleWithColumns(Style, 45));
8229 verifyFormat("Constructor() :\n"
8230 " Initializer(FitsOnTheLine) {}",
8231 getStyleWithColumns(Style, 44));
8232 verifyFormat("Constructor() :\n"
8233 " Initializer(FitsOnTheLine) {}",
8234 getStyleWithColumns(Style, 43));
8235
8236 verifyFormat("template <typename T>\n"
8237 "Constructor() : Initializer(FitsOnTheLine) {}",
8238 getStyleWithColumns(Style, 50));
8239 verifyFormat(
8240 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8241 " int mooooooooooooore) noexcept :\n"
8242 " Super{some, arguments}, Member{5}, Member2{2} {}",
8243 Style);
8244 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8245 verifyFormat(
8246 "SomeClass::Constructor() :\n"
8247 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8248 Style);
8249 verifyFormat(
8250 "SomeClass::Constructor() : // NOLINT\n"
8251 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8252 Style);
8253
8254 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8255 verifyFormat(
8256 "SomeClass::Constructor() :\n"
8257 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8258 Style);
8259 verifyFormat(
8260 "SomeClass::Constructor() : // NOLINT\n"
8261 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8262 Style);
8263
8264 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8265 verifyFormat(
8266 "SomeClass::Constructor() :\n"
8267 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8268 Style);
8269
8270 verifyFormat(
8271 "SomeClass::Constructor() :\n"
8272 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8273 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8274 Style);
8275 verifyFormat(
8276 "SomeClass::Constructor() :\n"
8277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8278 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8279 Style);
8280 verifyFormat(
8281 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8282 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8283 Style);
8284
8285 verifyFormat("Constructor() :\n"
8286 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8287 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8288 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8289 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8290 Style);
8291
8292 verifyFormat("Constructor() :\n"
8293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8295 Style);
8296
8297 verifyFormat("Constructor(int Parameter = 0) :\n"
8298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8299 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8300 Style);
8301 verifyFormat("Constructor() :\n"
8302 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8303 "}",
8304 getStyleWithColumns(Style, 60));
8305 verifyFormat("Constructor() :\n"
8306 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8307 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8308 Style);
8309
8310 // Here a line could be saved by splitting the second initializer onto two
8311 // lines, but that is not desirable.
8312 verifyFormat("Constructor() :\n"
8313 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8314 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8315 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8316 Style);
8317
8318 FormatStyle OnePerLine = Style;
8319 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8320 verifyFormat("SomeClass::Constructor() :\n"
8321 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8322 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8323 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8324 OnePerLine);
8325 verifyFormat("SomeClass::Constructor() :\n"
8326 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8327 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8328 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8329 OnePerLine);
8330 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8331 " i(i), // comment\n"
8332 " j(j) {}",
8333 OnePerLine);
8334 verifyFormat("MyClass::MyClass(int var) :\n"
8335 " some_var_(var), // 4 space indent\n"
8336 " some_other_var_(var + 1) { // lined up\n"
8337 "}",
8338 OnePerLine);
8339 verifyFormat("Constructor() :\n"
8340 " aaaaa(aaaaaa),\n"
8341 " aaaaa(aaaaaa),\n"
8342 " aaaaa(aaaaaa),\n"
8343 " aaaaa(aaaaaa),\n"
8344 " aaaaa(aaaaaa) {}",
8345 OnePerLine);
8346 verifyFormat("Constructor() :\n"
8347 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8348 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8349 OnePerLine);
8350 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8351 verifyFormat("Constructor() :\n"
8352 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8353 " aaaaaaaaaaa().aaa(),\n"
8354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8355 OnePerLine);
8356 OnePerLine.ColumnLimit = 60;
8357 verifyFormat("Constructor() :\n"
8358 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8359 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8360 OnePerLine);
8361
8362 verifyFormat("Constructor() :\n"
8363 " // Comment forcing unwanted break.\n"
8364 " aaaa(aaaa) {}",
8365 Style);
8366 verifyFormat("Constructor() : // NOLINT\n"
8367 " aaaa(aaaa) {}",
8368 Style);
8369 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8370 " on a single\n"
8371 " // line.\n"
8372 " aaaa(aaaa) {}",
8373 "Constructor() : // A very long trailing comment that cannot fit"
8374 " on a single line.\n"
8375 " aaaa(aaaa) {}",
8376 Style);
8377
8378 Style.ColumnLimit = 0;
8379 verifyNoChange("SomeClass::Constructor() :\n"
8380 " a(a) {}",
8381 Style);
8382 verifyNoChange("SomeClass::Constructor() noexcept :\n"
8383 " a(a) {}",
8384 Style);
8385 verifyNoChange("SomeClass::Constructor() :\n"
8386 " a(a), b(b), c(c) {}",
8387 Style);
8388 verifyNoChange("SomeClass::Constructor() :\n"
8389 " a(a) {\n"
8390 " foo();\n"
8391 " bar();\n"
8392 "}",
8393 Style);
8394 verifyFormat("struct Foo {\n"
8395 " int x;\n"
8396 " Foo() : x(0) {}\n"
8397 "};",
8398 "struct Foo {\n"
8399 " int x;\n"
8400 " Foo():x(0) {}\n"
8401 "};",
8402 Style);
8403
8404 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8405 verifyNoChange("SomeClass::Constructor() :\n"
8406 " a(a), b(b), c(c) {\n"
8407 "}",
8408 Style);
8409 verifyNoChange("SomeClass::Constructor() :\n"
8410 " a(a) {\n"
8411 "}",
8412 Style);
8413
8414 Style.ColumnLimit = 80;
8415 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8416 Style.ConstructorInitializerIndentWidth = 2;
8417 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8418 verifyFormat("SomeClass::Constructor() :\n"
8419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8420 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8421 Style);
8422
8423 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8424 // well
8425 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8426 verifyFormat(
8427 "class SomeClass\n"
8428 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8429 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8430 Style);
8431 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8432 verifyFormat(
8433 "class SomeClass\n"
8434 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8435 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8436 Style);
8437 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8438 verifyFormat(
8439 "class SomeClass :\n"
8440 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8441 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8442 Style);
8443 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8444 verifyFormat(
8445 "class SomeClass\n"
8446 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8447 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8448 Style);
8449 }
8450
8451 #ifndef EXPENSIVE_CHECKS
8452 // Expensive checks enables libstdc++ checking which includes validating the
8453 // state of ranges used in std::priority_queue - this blows out the
8454 // runtime/scalability of the function and makes this test unacceptably slow.
8455 TEST_F(FormatTest, MemoizationTests) {
8456 // This breaks if the memoization lookup does not take \c Indent and
8457 // \c LastSpace into account.
8458 verifyFormat(
8459 "extern CFRunLoopTimerRef\n"
8460 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8461 " CFTimeInterval interval, CFOptionFlags flags,\n"
8462 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8463 " CFRunLoopTimerContext *context) {}");
8464
8465 // Deep nesting somewhat works around our memoization.
8466 verifyFormat(
8467 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8468 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8469 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8470 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8471 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8472 getLLVMStyleWithColumns(65));
8473 verifyFormat(
8474 "aaaaa(\n"
8475 " aaaaa,\n"
8476 " aaaaa(\n"
8477 " aaaaa,\n"
8478 " aaaaa(\n"
8479 " aaaaa,\n"
8480 " aaaaa(\n"
8481 " aaaaa,\n"
8482 " aaaaa(\n"
8483 " aaaaa,\n"
8484 " aaaaa(\n"
8485 " aaaaa,\n"
8486 " aaaaa(\n"
8487 " aaaaa,\n"
8488 " aaaaa(\n"
8489 " aaaaa,\n"
8490 " aaaaa(\n"
8491 " aaaaa,\n"
8492 " aaaaa(\n"
8493 " aaaaa,\n"
8494 " aaaaa(\n"
8495 " aaaaa,\n"
8496 " aaaaa(\n"
8497 " aaaaa,\n"
8498 " aaaaa))))))))))));",
8499 getLLVMStyleWithColumns(65));
8500 verifyFormat(
8501 "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"
8502 " a),\n"
8503 " a),\n"
8504 " a),\n"
8505 " a),\n"
8506 " a),\n"
8507 " a),\n"
8508 " a),\n"
8509 " a),\n"
8510 " a),\n"
8511 " a),\n"
8512 " a),\n"
8513 " a),\n"
8514 " a),\n"
8515 " a),\n"
8516 " a),\n"
8517 " a),\n"
8518 " a)",
8519 getLLVMStyleWithColumns(65));
8520
8521 // This test takes VERY long when memoization is broken.
8522 FormatStyle OnePerLine = getLLVMStyle();
8523 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8524 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8525 std::string input = "Constructor()\n"
8526 " : aaaa(a,\n";
8527 for (unsigned i = 0, e = 80; i != e; ++i)
8528 input += " a,\n";
8529 input += " a) {}";
8530 verifyFormat(input, OnePerLine);
8531 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8532 verifyFormat(input, OnePerLine);
8533 }
8534 #endif
8535
8536 TEST_F(FormatTest, BreaksAsHighAsPossible) {
8537 verifyFormat(
8538 "void f() {\n"
8539 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8540 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8541 " f();\n"
8542 "}");
8543 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8544 " Intervals[i - 1].getRange().getLast()) {\n}");
8545 }
8546
8547 TEST_F(FormatTest, BreaksFunctionDeclarations) {
8548 // Principially, we break function declarations in a certain order:
8549 // 1) break amongst arguments.
8550 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8551 " Cccccccccccccc cccccccccccccc);");
8552 verifyFormat("template <class TemplateIt>\n"
8553 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8554 " TemplateIt *stop) {}");
8555
8556 // 2) break after return type.
8557 verifyGoogleFormat(
8558 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8559 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8560
8561 // 3) break after (.
8562 verifyGoogleFormat(
8563 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8564 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8565
8566 // 4) break before after nested name specifiers.
8567 verifyGoogleFormat(
8568 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8569 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8570 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8571
8572 // However, there are exceptions, if a sufficient amount of lines can be
8573 // saved.
8574 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8575 // more adjusting.
8576 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8577 " Cccccccccccccc cccccccccc,\n"
8578 " Cccccccccccccc cccccccccc,\n"
8579 " Cccccccccccccc cccccccccc,\n"
8580 " Cccccccccccccc cccccccccc);");
8581 verifyGoogleFormat(
8582 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8583 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8584 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8585 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8586 verifyFormat(
8587 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8588 " Cccccccccccccc cccccccccc,\n"
8589 " Cccccccccccccc cccccccccc,\n"
8590 " Cccccccccccccc cccccccccc,\n"
8591 " Cccccccccccccc cccccccccc,\n"
8592 " Cccccccccccccc cccccccccc,\n"
8593 " Cccccccccccccc cccccccccc);");
8594 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8595 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8596 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8597 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8598 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8599
8600 // Break after multi-line parameters.
8601 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8602 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8604 " bbbb bbbb);");
8605 verifyFormat("void SomeLoooooooooooongFunction(\n"
8606 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8607 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8608 " int bbbbbbbbbbbbb);");
8609
8610 // Treat overloaded operators like other functions.
8611 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8612 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8613 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8614 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8615 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8616 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8617 verifyGoogleFormat(
8618 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8619 " const SomeLooooooooogType& a, const SomeLooooooooogType& b);");
8620 verifyGoogleFormat(
8621 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8622 " const SomeLooooooooogType& a, const SomeLooooooooogType& b);");
8623
8624 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8625 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8626 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8627 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8628 verifyGoogleFormat(
8629 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8630 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8631 " bool* aaaaaaaaaaaaaaaaaa, bool* aa) {}");
8632 verifyGoogleFormat("template <typename T>\n"
8633 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8634 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8635 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8636
8637 verifyFormat("extern \"C\" //\n"
8638 " void f();");
8639
8640 auto Style = getLLVMStyle();
8641 Style.PointerAlignment = FormatStyle::PAS_Left;
8642 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8643 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8644 Style);
8645 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8647 Style);
8648
8649 Style = getLLVMStyleWithColumns(45);
8650 Style.PenaltyReturnTypeOnItsOwnLine = 400;
8651 verifyFormat("template <bool abool, // a comment\n"
8652 " bool anotherbool>\n"
8653 "static inline std::pair<size_t, MyCustomType>\n"
8654 "myfunc(const char *buf, const char *&err);",
8655 Style);
8656 }
8657
8658 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8659 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8660 // Prefer keeping `::` followed by `operator` together.
8661 verifyFormat("const aaaa::bbbbbbb &\n"
8662 "ccccccccc::operator++() {\n"
8663 " stuff();\n"
8664 "}",
8665 "const aaaa::bbbbbbb\n"
8666 "&ccccccccc::operator++() { stuff(); }",
8667 getLLVMStyleWithColumns(40));
8668 }
8669
8670 TEST_F(FormatTest, TrailingReturnType) {
8671 verifyFormat("auto foo() -> int;");
8672 // correct trailing return type spacing
8673 verifyFormat("auto operator->() -> int;");
8674 verifyFormat("auto operator++(int) -> int;");
8675
8676 verifyFormat("struct S {\n"
8677 " auto bar() const -> int;\n"
8678 "};");
8679 verifyFormat("template <size_t Order, typename T>\n"
8680 "auto load_img(const std::string &filename)\n"
8681 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8682 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8683 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8684 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8685 verifyFormat("template <typename T>\n"
8686 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8687 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8688
8689 FormatStyle Style = getLLVMStyleWithColumns(60);
8690 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8691 " auto NAME() -> int { return 42; }",
8692 Style);
8693
8694 // Not trailing return types.
8695 verifyFormat("void f() { auto a = b->c(); }");
8696 verifyFormat("auto a = p->foo();");
8697 verifyFormat("int a = p->foo();");
8698 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8699 }
8700
8701 TEST_F(FormatTest, DeductionGuides) {
8702 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8703 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8704 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8705 verifyFormat(
8706 "template <class... T>\n"
8707 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8708 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8709 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8710 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8711 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8712 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8713 verifyFormat("template <class T> x() -> x<1>;");
8714 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8715
8716 verifyFormat("A(const char *) -> A<string &>;");
8717 verifyFormat("A() -> A<int>;");
8718
8719 // Ensure not deduction guides.
8720 verifyFormat("c()->f<int>();");
8721 verifyFormat("x()->foo<1>;");
8722 verifyFormat("x = p->foo<3>();");
8723 verifyFormat("x()->x<1>();");
8724 }
8725
8726 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8727 // Avoid breaking before trailing 'const' or other trailing annotations, if
8728 // they are not function-like.
8729 FormatStyle Style = getGoogleStyleWithColumns(47);
8730 verifyFormat("void someLongFunction(\n"
8731 " int someLoooooooooooooongParameter) const {\n}",
8732 getLLVMStyleWithColumns(47));
8733 verifyFormat("LoooooongReturnType\n"
8734 "someLoooooooongFunction() const {}",
8735 getLLVMStyleWithColumns(47));
8736 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8737 " const {}",
8738 Style);
8739 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8740 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8741 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8742 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8743 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8744 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8745 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8746 " aaaaaaaaaaa aaaaa) const override;");
8747 verifyGoogleFormat(
8748 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8749 " const override;");
8750
8751 // Even if the first parameter has to be wrapped.
8752 verifyFormat("void someLongFunction(\n"
8753 " int someLongParameter) const {}",
8754 getLLVMStyleWithColumns(46));
8755 verifyFormat("void someLongFunction(\n"
8756 " int someLongParameter) const {}",
8757 Style);
8758 verifyFormat("void someLongFunction(\n"
8759 " int someLongParameter) override {}",
8760 Style);
8761 verifyFormat("void someLongFunction(\n"
8762 " int someLongParameter) OVERRIDE {}",
8763 Style);
8764 verifyFormat("void someLongFunction(\n"
8765 " int someLongParameter) final {}",
8766 Style);
8767 verifyFormat("void someLongFunction(\n"
8768 " int someLongParameter) FINAL {}",
8769 Style);
8770 verifyFormat("void someLongFunction(\n"
8771 " int parameter) const override {}",
8772 Style);
8773
8774 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8775 verifyFormat("void someLongFunction(\n"
8776 " int someLongParameter) const\n"
8777 "{\n"
8778 "}",
8779 Style);
8780
8781 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8782 verifyFormat("void someLongFunction(\n"
8783 " int someLongParameter) const\n"
8784 " {\n"
8785 " }",
8786 Style);
8787
8788 // Unless these are unknown annotations.
8789 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8790 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8791 " LONG_AND_UGLY_ANNOTATION;");
8792
8793 // Breaking before function-like trailing annotations is fine to keep them
8794 // close to their arguments.
8795 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8796 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8797 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8798 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8799 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8800 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8801 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8802 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8803 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8804
8805 verifyFormat(
8806 "void aaaaaaaaaaaaaaaaaa()\n"
8807 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8808 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8809 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8810 " __attribute__((unused));");
8811
8812 Style = getGoogleStyle();
8813
8814 verifyFormat(
8815 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8816 " GUARDED_BY(aaaaaaaaaaaa);",
8817 Style);
8818 verifyFormat(
8819 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8820 " GUARDED_BY(aaaaaaaaaaaa);",
8821 Style);
8822 verifyFormat(
8823 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8824 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8825 Style);
8826 verifyFormat(
8827 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8828 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8829 Style);
8830
8831 verifyFormat(
8832 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8833 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8834 Style);
8835 verifyFormat(
8836 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8837 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8838 Style);
8839 verifyFormat(
8840 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8841 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8842 Style);
8843 verifyFormat(
8844 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8845 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8846 Style);
8847 }
8848
8849 TEST_F(FormatTest, FunctionAnnotations) {
8850 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8851 "int OldFunction(const string &parameter) {}");
8852 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8853 "string OldFunction(const string &parameter) {}");
8854 verifyFormat("template <typename T>\n"
8855 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8856 "string OldFunction(const string &parameter) {}");
8857
8858 // Not function annotations.
8859 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8860 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8861 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8862 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8863 verifyFormat("MACRO(abc).function() // wrap\n"
8864 " << abc;");
8865 verifyFormat("MACRO(abc)->function() // wrap\n"
8866 " << abc;");
8867 verifyFormat("MACRO(abc)::function() // wrap\n"
8868 " << abc;");
8869 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8870 }
8871
8872 TEST_F(FormatTest, BreaksDesireably) {
8873 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8874 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8875 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8876 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8878 "}");
8879
8880 verifyFormat(
8881 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8883
8884 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8887
8888 verifyFormat(
8889 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8890 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8892 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8894
8895 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8896 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8897
8898 verifyFormat(
8899 "void f() {\n"
8900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8901 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8902 "}");
8903 verifyFormat(
8904 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8906 verifyFormat(
8907 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8909 verifyFormat(
8910 "aaaaaa(aaa,\n"
8911 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8913 " aaaa);");
8914 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8917
8918 // Indent consistently independent of call expression and unary operator.
8919 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8920 " dddddddddddddddddddddddddddddd));");
8921 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8922 " dddddddddddddddddddddddddddddd));");
8923 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8924 " dddddddddddddddddddddddddddddd));");
8925
8926 // This test case breaks on an incorrect memoization, i.e. an optimization not
8927 // taking into account the StopAt value.
8928 verifyFormat(
8929 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8930 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8931 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8932 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8933
8934 verifyFormat("{\n {\n {\n"
8935 " Annotation.SpaceRequiredBefore =\n"
8936 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8937 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8938 " }\n }\n}");
8939
8940 // Break on an outer level if there was a break on an inner level.
8941 verifyFormat("f(g(h(a, // comment\n"
8942 " b, c),\n"
8943 " d, e),\n"
8944 " x, y);",
8945 "f(g(h(a, // comment\n"
8946 " b, c), d, e), x, y);");
8947
8948 // Prefer breaking similar line breaks.
8949 verifyFormat(
8950 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8951 " NSTrackingMouseEnteredAndExited |\n"
8952 " NSTrackingActiveAlways;");
8953 }
8954
8955 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8956 FormatStyle NoBinPacking = getGoogleStyle();
8957 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8958 NoBinPacking.BinPackArguments = true;
8959 verifyFormat("void f() {\n"
8960 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8961 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8962 "}",
8963 NoBinPacking);
8964 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8965 " int aaaaaaaaaaaaaaaaaaaa,\n"
8966 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8967 NoBinPacking);
8968
8969 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8970 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8971 " vector<int> bbbbbbbbbbbbbbb);",
8972 NoBinPacking);
8973 // FIXME: This behavior difference is probably not wanted. However, currently
8974 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8975 // template arguments from BreakBeforeParameter being set because of the
8976 // one-per-line formatting.
8977 verifyFormat(
8978 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8979 " aaaaaaaaaa> aaaaaaaaaa);",
8980 NoBinPacking);
8981 verifyFormat(
8982 "void fffffffffff(\n"
8983 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8984 " aaaaaaaaaa);");
8985 }
8986
8987 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8988 FormatStyle NoBinPacking = getGoogleStyle();
8989 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8990 NoBinPacking.BinPackArguments = false;
8991 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8992 " aaaaaaaaaaaaaaaaaaaa,\n"
8993 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8994 NoBinPacking);
8995 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8996 " aaaaaaaaaaaaa,\n"
8997 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8998 NoBinPacking);
8999 verifyFormat(
9000 "aaaaaaaa(aaaaaaaaaaaaa,\n"
9001 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
9003 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
9005 NoBinPacking);
9006 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
9007 " .aaaaaaaaaaaaaaaaaa();",
9008 NoBinPacking);
9009 verifyFormat("void f() {\n"
9010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9011 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
9012 "}",
9013 NoBinPacking);
9014
9015 verifyFormat(
9016 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9017 " aaaaaaaaaaaa,\n"
9018 " aaaaaaaaaaaa);",
9019 NoBinPacking);
9020 verifyFormat(
9021 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
9022 " ddddddddddddddddddddddddddddd),\n"
9023 " test);",
9024 NoBinPacking);
9025
9026 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
9027 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
9028 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
9029 " aaaaaaaaaaaaaaaaaa;",
9030 NoBinPacking);
9031 verifyFormat("a(\"a\"\n"
9032 " \"a\",\n"
9033 " a);");
9034
9035 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
9036 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
9037 " aaaaaaaaa,\n"
9038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9039 NoBinPacking);
9040 verifyFormat(
9041 "void f() {\n"
9042 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
9043 " .aaaaaaa();\n"
9044 "}",
9045 NoBinPacking);
9046 verifyFormat(
9047 "template <class SomeType, class SomeOtherType>\n"
9048 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
9049 NoBinPacking);
9050 }
9051
9052 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
9053 FormatStyle Style = getLLVMStyleWithColumns(15);
9054 Style.ExperimentalAutoDetectBinPacking = true;
9055 verifyFormat("aaa(aaaa,\n"
9056 " aaaa,\n"
9057 " aaaa);\n"
9058 "aaa(aaaa,\n"
9059 " aaaa,\n"
9060 " aaaa);",
9061 "aaa(aaaa,\n" // one-per-line
9062 " aaaa,\n"
9063 " aaaa );\n"
9064 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9065 Style);
9066 verifyFormat("aaa(aaaa, aaaa,\n"
9067 " aaaa);\n"
9068 "aaa(aaaa, aaaa,\n"
9069 " aaaa);",
9070 "aaa(aaaa, aaaa,\n" // bin-packed
9071 " aaaa );\n"
9072 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9073 Style);
9074 }
9075
9076 TEST_F(FormatTest, IndentExportBlock) {
9077 FormatStyle Style = getLLVMStyleWithColumns(80);
9078 Style.IndentExportBlock = true;
9079 verifyFormat("export {\n"
9080 " int x;\n"
9081 " int y;\n"
9082 "}",
9083 "export {\n"
9084 "int x;\n"
9085 "int y;\n"
9086 "}",
9087 Style);
9088
9089 Style.IndentExportBlock = false;
9090 verifyFormat("export {\n"
9091 "int x;\n"
9092 "int y;\n"
9093 "}",
9094 "export {\n"
9095 " int x;\n"
9096 " int y;\n"
9097 "}",
9098 Style);
9099 }
9100
9101 TEST_F(FormatTest, ShortExportBlocks) {
9102 FormatStyle Style = getLLVMStyleWithColumns(80);
9103 Style.IndentExportBlock = false;
9104
9105 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
9106 verifyFormat("export {\n"
9107 "}",
9108 Style);
9109
9110 verifyFormat("export {\n"
9111 "int x;\n"
9112 "}",
9113 Style);
9114
9115 verifyFormat("export {\n"
9116 "int x;\n"
9117 "}",
9118 "export\n"
9119 "{\n"
9120 "int x;\n"
9121 "}",
9122 Style);
9123
9124 verifyFormat("export {\n"
9125 "}",
9126 "export {}", Style);
9127
9128 verifyFormat("export {\n"
9129 "int x;\n"
9130 "}",
9131 "export { int x; }", Style);
9132
9133 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
9134 verifyFormat("export {}",
9135 "export {\n"
9136 "}",
9137 Style);
9138
9139 verifyFormat("export { int x; }",
9140 "export {\n"
9141 "int x;\n"
9142 "}",
9143 Style);
9144
9145 verifyFormat("export { int x; }",
9146 "export\n"
9147 "{\n"
9148 "int x;\n"
9149 "}",
9150 Style);
9151
9152 verifyFormat("export {}",
9153 "export {\n"
9154 "}",
9155 Style);
9156
9157 verifyFormat("export { int x; }",
9158 "export {\n"
9159 "int x;\n"
9160 "}",
9161 Style);
9162
9163 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
9164 verifyFormat("export {}",
9165 "export {\n"
9166 "}",
9167 Style);
9168
9169 verifyFormat("export {\n"
9170 "int x;\n"
9171 "}",
9172 Style);
9173
9174 verifyFormat("export {\n"
9175 "int x;\n"
9176 "}",
9177 "export\n"
9178 "{\n"
9179 "int x;\n"
9180 "}",
9181 Style);
9182
9183 verifyFormat("export {}", Style);
9184
9185 verifyFormat("export {\n"
9186 "int x;\n"
9187 "}",
9188 "export { int x; }", Style);
9189 }
9190
9191 TEST_F(FormatTest, FormatsBuilderPattern) {
9192 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9193 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9194 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9195 " .StartsWith(\".init\", ORDER_INIT)\n"
9196 " .StartsWith(\".fini\", ORDER_FINI)\n"
9197 " .StartsWith(\".hash\", ORDER_HASH)\n"
9198 " .Default(ORDER_TEXT);");
9199
9200 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9201 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9202 verifyFormat("aaaaaaa->aaaaaaa\n"
9203 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9205 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9206 verifyFormat(
9207 "aaaaaaa->aaaaaaa\n"
9208 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9209 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9210 verifyFormat(
9211 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9212 " aaaaaaaaaaaaaa);");
9213 verifyFormat(
9214 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9215 " aaaaaa->aaaaaaaaaaaa()\n"
9216 " ->aaaaaaaaaaaaaaaa(\n"
9217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9218 " ->aaaaaaaaaaaaaaaaa();");
9219 verifyGoogleFormat(
9220 "void f() {\n"
9221 " someo->Add((new util::filetools::Handler(dir))\n"
9222 " ->OnEvent1(NewPermanentCallback(\n"
9223 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9224 " ->OnEvent2(NewPermanentCallback(\n"
9225 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9226 " ->OnEvent3(NewPermanentCallback(\n"
9227 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9228 " ->OnEvent5(NewPermanentCallback(\n"
9229 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9230 " ->OnEvent6(NewPermanentCallback(\n"
9231 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9232 "}");
9233
9234 verifyFormat(
9235 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9236 verifyFormat("aaaaaaaaaaaaaaa()\n"
9237 " .aaaaaaaaaaaaaaa()\n"
9238 " .aaaaaaaaaaaaaaa()\n"
9239 " .aaaaaaaaaaaaaaa()\n"
9240 " .aaaaaaaaaaaaaaa();");
9241 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9242 " .aaaaaaaaaaaaaaa()\n"
9243 " .aaaaaaaaaaaaaaa()\n"
9244 " .aaaaaaaaaaaaaaa();");
9245 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9246 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9247 " .aaaaaaaaaaaaaaa();");
9248 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9249 " ->aaaaaaaaaaaaaae(0)\n"
9250 " ->aaaaaaaaaaaaaaa();");
9251
9252 // Don't linewrap after very short segments.
9253 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9254 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9255 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9256 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9257 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9258 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9259 verifyFormat("aaa()\n"
9260 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9261 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9262 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9263
9264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9265 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9266 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9267 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9268 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9270
9271 // Prefer not to break after empty parentheses.
9272 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9273 " First->LastNewlineOffset);");
9274
9275 // Prefer not to create "hanging" indents.
9276 verifyFormat(
9277 "return !soooooooooooooome_map\n"
9278 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9279 " .second;");
9280 verifyFormat(
9281 "return aaaaaaaaaaaaaaaa\n"
9282 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9283 " .aaaa(aaaaaaaaaaaaaa);");
9284 // No hanging indent here.
9285 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9287 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9289 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9290 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9291 getLLVMStyleWithColumns(60));
9292 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9293 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9294 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9295 getLLVMStyleWithColumns(59));
9296 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9298 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9299
9300 // Dont break if only closing statements before member call
9301 verifyFormat("test() {\n"
9302 " ([]() -> {\n"
9303 " int b = 32;\n"
9304 " return 3;\n"
9305 " }).foo();\n"
9306 "}");
9307 verifyFormat("test() {\n"
9308 " (\n"
9309 " []() -> {\n"
9310 " int b = 32;\n"
9311 " return 3;\n"
9312 " },\n"
9313 " foo, bar)\n"
9314 " .foo();\n"
9315 "}");
9316 verifyFormat("test() {\n"
9317 " ([]() -> {\n"
9318 " int b = 32;\n"
9319 " return 3;\n"
9320 " })\n"
9321 " .foo()\n"
9322 " .bar();\n"
9323 "}");
9324 verifyFormat("test() {\n"
9325 " ([]() -> {\n"
9326 " int b = 32;\n"
9327 " return 3;\n"
9328 " })\n"
9329 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9330 " \"bbbb\");\n"
9331 "}",
9332 getLLVMStyleWithColumns(30));
9333 }
9334
9335 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9336 verifyFormat(
9337 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9338 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9339 verifyFormat(
9340 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9341 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9342
9343 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9344 " ccccccccccccccccccccccccc) {\n}");
9345 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9346 " ccccccccccccccccccccccccc) {\n}");
9347
9348 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9349 " ccccccccccccccccccccccccc) {\n}");
9350 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9351 " ccccccccccccccccccccccccc) {\n}");
9352
9353 verifyFormat(
9354 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9355 " ccccccccccccccccccccccccc) {\n}");
9356 verifyFormat(
9357 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9358 " ccccccccccccccccccccccccc) {\n}");
9359
9360 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9361 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9362 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9363 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9364 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9365 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9366 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9367 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9368
9369 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9370 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9371 " aaaaaaaaaaaaaaa != aa) {\n}");
9372 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9373 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9374 " aaaaaaaaaaaaaaa != aa) {\n}");
9375 }
9376
9377 TEST_F(FormatTest, BreaksAfterAssignments) {
9378 verifyFormat(
9379 "unsigned Cost =\n"
9380 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9381 " SI->getPointerAddressSpaceee());");
9382 verifyFormat(
9383 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9384 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9385
9386 verifyFormat(
9387 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9388 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9389 verifyFormat("unsigned OriginalStartColumn =\n"
9390 " SourceMgr.getSpellingColumnNumber(\n"
9391 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9392 " 1;");
9393 }
9394
9395 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9396 FormatStyle Style = getLLVMStyle();
9397 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9398 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9399 Style);
9400
9401 Style.PenaltyBreakAssignment = 20;
9402 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9403 " cccccccccccccccccccccccccc;",
9404 Style);
9405 }
9406
9407 TEST_F(FormatTest, AlignsAfterAssignments) {
9408 verifyFormat(
9409 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9410 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9411 verifyFormat(
9412 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9413 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9414 verifyFormat(
9415 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9416 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9417 verifyFormat(
9418 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9419 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9420 verifyFormat(
9421 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9422 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9423 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9424 }
9425
9426 TEST_F(FormatTest, AlignsAfterReturn) {
9427 verifyFormat(
9428 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9429 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9430 verifyFormat(
9431 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9432 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9433 verifyFormat(
9434 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9435 " aaaaaaaaaaaaaaaaaaaaaa();");
9436 verifyFormat(
9437 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9438 " aaaaaaaaaaaaaaaaaaaaaa());");
9439 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9441 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9444 verifyFormat("return\n"
9445 " // true if code is one of a or b.\n"
9446 " code == a || code == b;");
9447 }
9448
9449 TEST_F(FormatTest, BreaksConditionalExpressions) {
9450 verifyFormat(
9451 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9452 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9453 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9454 verifyFormat(
9455 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9456 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9457 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9458 verifyFormat(
9459 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9460 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9461 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9462 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9463 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9464 verifyFormat(
9465 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9466 " : aaaaaaaaaaaaa);");
9467 verifyFormat(
9468 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9469 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9470 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9471 " aaaaaaaaaaaaa);");
9472 verifyFormat(
9473 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9474 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9475 " aaaaaaaaaaaaa);");
9476 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9477 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9479 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9481 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9483 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9485 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9487 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9488 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9490 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9492 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9493 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9494 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9495 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9496 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9498 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9499 " : aaaaaaaaaaaaaaaa;");
9500 verifyFormat(
9501 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9502 " ? aaaaaaaaaaaaaaa\n"
9503 " : aaaaaaaaaaaaaaa;");
9504 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9505 " aaaaaaaaa\n"
9506 " ? b\n"
9507 " : c);");
9508 verifyFormat("return aaaa == bbbb\n"
9509 " // comment\n"
9510 " ? aaaa\n"
9511 " : bbbb;");
9512 verifyFormat("unsigned Indent =\n"
9513 " format(TheLine.First,\n"
9514 " IndentForLevel[TheLine.Level] >= 0\n"
9515 " ? IndentForLevel[TheLine.Level]\n"
9516 " : TheLine * 2,\n"
9517 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9518 getLLVMStyleWithColumns(60));
9519 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9520 " ? aaaaaaaaaaaaaaa\n"
9521 " : bbbbbbbbbbbbbbb //\n"
9522 " ? ccccccccccccccc\n"
9523 " : ddddddddddddddd;");
9524 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9525 " ? aaaaaaaaaaaaaaa\n"
9526 " : (bbbbbbbbbbbbbbb //\n"
9527 " ? ccccccccccccccc\n"
9528 " : ddddddddddddddd);");
9529 verifyFormat(
9530 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9531 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9532 " aaaaaaaaaaaaaaaaaaaaa +\n"
9533 " aaaaaaaaaaaaaaaaaaaaa\n"
9534 " : aaaaaaaaaa;");
9535 verifyFormat(
9536 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9537 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9538 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9539
9540 FormatStyle NoBinPacking = getLLVMStyle();
9541 NoBinPacking.BinPackArguments = false;
9542 verifyFormat(
9543 "void f() {\n"
9544 " g(aaa,\n"
9545 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9547 " ? aaaaaaaaaaaaaaa\n"
9548 " : aaaaaaaaaaaaaaa);\n"
9549 "}",
9550 NoBinPacking);
9551 verifyFormat(
9552 "void f() {\n"
9553 " g(aaa,\n"
9554 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9556 " ?: aaaaaaaaaaaaaaa);\n"
9557 "}",
9558 NoBinPacking);
9559
9560 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9561 " // comment.\n"
9562 " ccccccccccccccccccccccccccccccccccccccc\n"
9563 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9564 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9565
9566 // Assignments in conditional expressions. Apparently not uncommon :-(.
9567 verifyFormat("return a != b\n"
9568 " // comment\n"
9569 " ? a = b\n"
9570 " : a = b;");
9571 verifyFormat("return a != b\n"
9572 " // comment\n"
9573 " ? a = a != b\n"
9574 " // comment\n"
9575 " ? a = b\n"
9576 " : a\n"
9577 " : a;");
9578 verifyFormat("return a != b\n"
9579 " // comment\n"
9580 " ? a\n"
9581 " : a = a != b\n"
9582 " // comment\n"
9583 " ? a = b\n"
9584 " : a;");
9585
9586 // Chained conditionals
9587 FormatStyle Style = getLLVMStyleWithColumns(70);
9588 Style.AlignOperands = FormatStyle::OAS_Align;
9589 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9590 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9591 " : 3333333333333333;",
9592 Style);
9593 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9594 " : bbbbbbbbbb ? 2222222222222222\n"
9595 " : 3333333333333333;",
9596 Style);
9597 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9598 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9599 " : 3333333333333333;",
9600 Style);
9601 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9602 " : bbbbbbbbbbbbbb ? 222222\n"
9603 " : 333333;",
9604 Style);
9605 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9606 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9607 " : cccccccccccccc ? 3333333333333333\n"
9608 " : 4444444444444444;",
9609 Style);
9610 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9611 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9612 " : 3333333333333333;",
9613 Style);
9614 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9615 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9616 " : (aaa ? bbb : ccc);",
9617 Style);
9618 verifyFormat(
9619 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9620 " : cccccccccccccccccc)\n"
9621 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9622 " : 3333333333333333;",
9623 Style);
9624 verifyFormat(
9625 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9626 " : cccccccccccccccccc)\n"
9627 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9628 " : 3333333333333333;",
9629 Style);
9630 verifyFormat(
9631 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9632 " : dddddddddddddddddd)\n"
9633 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9634 " : 3333333333333333;",
9635 Style);
9636 verifyFormat(
9637 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9638 " : dddddddddddddddddd)\n"
9639 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9640 " : 3333333333333333;",
9641 Style);
9642 verifyFormat(
9643 "return aaaaaaaaa ? 1111111111111111\n"
9644 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9645 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9646 " : dddddddddddddddddd)",
9647 Style);
9648 verifyFormat(
9649 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9650 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9651 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9652 " : cccccccccccccccccc);",
9653 Style);
9654 verifyFormat(
9655 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9656 " : ccccccccccccccc ? dddddddddddddddddd\n"
9657 " : eeeeeeeeeeeeeeeeee)\n"
9658 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9659 " : 3333333333333333;",
9660 Style);
9661 verifyFormat(
9662 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9663 " : ccccccccccccccc ? dddddddddddddddddd\n"
9664 " : eeeeeeeeeeeeeeeeee)\n"
9665 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9666 " : 3333333333333333;",
9667 Style);
9668 verifyFormat(
9669 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9670 " : cccccccccccc ? dddddddddddddddddd\n"
9671 " : eeeeeeeeeeeeeeeeee)\n"
9672 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9673 " : 3333333333333333;",
9674 Style);
9675 verifyFormat(
9676 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9677 " : cccccccccccccccccc\n"
9678 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9679 " : 3333333333333333;",
9680 Style);
9681 verifyFormat(
9682 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9683 " : cccccccccccccccc ? dddddddddddddddddd\n"
9684 " : eeeeeeeeeeeeeeeeee\n"
9685 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9686 " : 3333333333333333;",
9687 Style);
9688 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9689 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9690 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9691 " : eeeeeeeeeeeeeeeeee)\n"
9692 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9693 " : 3333333333333333;",
9694 Style);
9695 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9696 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9697 " : cccccccccccccccc ? dddddddddddddddddd\n"
9698 " : eeeeeeeeeeeeeeeeee\n"
9699 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9700 " : 3333333333333333;",
9701 Style);
9702
9703 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9704 Style.BreakBeforeTernaryOperators = false;
9705 // FIXME: Aligning the question marks is weird given DontAlign.
9706 // Consider disabling this alignment in this case. Also check whether this
9707 // will render the adjustment from https://reviews.llvm.org/D82199
9708 // unnecessary.
9709 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9710 " bbbb ? cccccccccccccccccc :\n"
9711 " ddddd;",
9712 Style);
9713
9714 verifyFormat(
9715 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9716 " /*\n"
9717 " */\n"
9718 " function() {\n"
9719 " try {\n"
9720 " return JJJJJJJJJJJJJJ(\n"
9721 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9722 " }\n"
9723 " } :\n"
9724 " function() {};",
9725 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9726 " /*\n"
9727 " */\n"
9728 " function() {\n"
9729 " try {\n"
9730 " return JJJJJJJJJJJJJJ(\n"
9731 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9732 " }\n"
9733 " } :\n"
9734 " function() {};",
9735 getGoogleStyle(FormatStyle::LK_JavaScript));
9736 }
9737
9738 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9739 FormatStyle Style = getLLVMStyleWithColumns(70);
9740 Style.BreakBeforeTernaryOperators = false;
9741 verifyFormat(
9742 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9745 Style);
9746 verifyFormat(
9747 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9748 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9750 Style);
9751 verifyFormat(
9752 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9754 Style);
9755 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9756 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9758 Style);
9759 verifyFormat(
9760 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9761 " aaaaaaaaaaaaa);",
9762 Style);
9763 verifyFormat(
9764 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9765 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9767 " aaaaaaaaaaaaa);",
9768 Style);
9769 verifyFormat(
9770 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9771 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9772 " aaaaaaaaaaaaa);",
9773 Style);
9774 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9779 Style);
9780 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9782 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9786 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9787 Style);
9788 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9789 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9792 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9793 Style);
9794 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9795 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9796 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9797 Style);
9798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9802 Style);
9803 verifyFormat(
9804 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9805 " aaaaaaaaaaaaaaa :\n"
9806 " aaaaaaaaaaaaaaa;",
9807 Style);
9808 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9809 " aaaaaaaaa ?\n"
9810 " b :\n"
9811 " c);",
9812 Style);
9813 verifyFormat("unsigned Indent =\n"
9814 " format(TheLine.First,\n"
9815 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9816 " IndentForLevel[TheLine.Level] :\n"
9817 " TheLine * 2,\n"
9818 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9819 Style);
9820 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9821 " aaaaaaaaaaaaaaa :\n"
9822 " bbbbbbbbbbbbbbb ? //\n"
9823 " ccccccccccccccc :\n"
9824 " ddddddddddddddd;",
9825 Style);
9826 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9827 " aaaaaaaaaaaaaaa :\n"
9828 " (bbbbbbbbbbbbbbb ? //\n"
9829 " ccccccccccccccc :\n"
9830 " ddddddddddddddd);",
9831 Style);
9832 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9833 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9834 " ccccccccccccccccccccccccccc;",
9835 Style);
9836 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9837 " aaaaa :\n"
9838 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9839 Style);
9840
9841 // Chained conditionals
9842 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9843 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9844 " 3333333333333333;",
9845 Style);
9846 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9847 " bbbbbbbbbb ? 2222222222222222 :\n"
9848 " 3333333333333333;",
9849 Style);
9850 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9851 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9852 " 3333333333333333;",
9853 Style);
9854 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9855 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9856 " 333333;",
9857 Style);
9858 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9859 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9860 " cccccccccccccccc ? 3333333333333333 :\n"
9861 " 4444444444444444;",
9862 Style);
9863 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9864 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9865 " 3333333333333333;",
9866 Style);
9867 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9868 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9869 " (aaa ? bbb : ccc);",
9870 Style);
9871 verifyFormat(
9872 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9873 " cccccccccccccccccc) :\n"
9874 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9875 " 3333333333333333;",
9876 Style);
9877 verifyFormat(
9878 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9879 " cccccccccccccccccc) :\n"
9880 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9881 " 3333333333333333;",
9882 Style);
9883 verifyFormat(
9884 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9885 " dddddddddddddddddd) :\n"
9886 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9887 " 3333333333333333;",
9888 Style);
9889 verifyFormat(
9890 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9891 " dddddddddddddddddd) :\n"
9892 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9893 " 3333333333333333;",
9894 Style);
9895 verifyFormat(
9896 "return aaaaaaaaa ? 1111111111111111 :\n"
9897 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9898 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9899 " dddddddddddddddddd)",
9900 Style);
9901 verifyFormat(
9902 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9903 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9904 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9905 " cccccccccccccccccc);",
9906 Style);
9907 verifyFormat(
9908 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9909 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9910 " eeeeeeeeeeeeeeeeee) :\n"
9911 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9912 " 3333333333333333;",
9913 Style);
9914 verifyFormat(
9915 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9916 " ccccccccccccc ? dddddddddddddddddd :\n"
9917 " eeeeeeeeeeeeeeeeee) :\n"
9918 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9919 " 3333333333333333;",
9920 Style);
9921 verifyFormat(
9922 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9923 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9924 " eeeeeeeeeeeeeeeeee) :\n"
9925 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9926 " 3333333333333333;",
9927 Style);
9928 verifyFormat(
9929 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9930 " cccccccccccccccccc :\n"
9931 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9932 " 3333333333333333;",
9933 Style);
9934 verifyFormat(
9935 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9936 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9937 " eeeeeeeeeeeeeeeeee :\n"
9938 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9939 " 3333333333333333;",
9940 Style);
9941 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9942 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9943 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9944 " eeeeeeeeeeeeeeeeee) :\n"
9945 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9946 " 3333333333333333;",
9947 Style);
9948 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9949 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9950 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
9951 " eeeeeeeeeeeeeeeeee :\n"
9952 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9953 " 3333333333333333;",
9954 Style);
9955 }
9956
9957 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
9958 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
9959 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
9960 verifyFormat("bool a = true, b = false;");
9961
9962 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
9964 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
9965 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
9966 verifyFormat(
9967 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
9968 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
9969 " d = e && f;");
9970 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
9971 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
9972 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9973 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
9974 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
9975 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
9976
9977 FormatStyle Style = getGoogleStyle();
9978 Style.PointerAlignment = FormatStyle::PAS_Left;
9979 Style.DerivePointerAlignment = false;
9980 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9981 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
9982 " *b = bbbbbbbbbbbbbbbbbbb;",
9983 Style);
9984 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9985 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
9986 Style);
9987 verifyFormat("vector<int*> a, b;", Style);
9988 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
9989 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
9990 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
9991 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
9992 Style);
9993 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
9994 Style);
9995 verifyFormat(
9996 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
9997 Style);
9998
9999 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
10000 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
10001 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
10002 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
10003 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
10004 Style);
10005 }
10006
10007 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
10008 verifyFormat("arr[foo ? bar : baz];");
10009 verifyFormat("f()[foo ? bar : baz];");
10010 verifyFormat("(a + b)[foo ? bar : baz];");
10011 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10012 }
10013
10014 TEST_F(FormatTest, AlignsStringLiterals) {
10015 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10016 " \"short literal\");");
10017 verifyFormat(
10018 "looooooooooooooooooooooooongFunction(\n"
10019 " \"short literal\"\n"
10020 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10021 verifyFormat("someFunction(\"Always break between multi-line\"\n"
10022 " \" string literals\",\n"
10023 " also, other, parameters);");
10024 verifyFormat("fun + \"1243\" /* comment */\n"
10025 " \"5678\";",
10026 "fun + \"1243\" /* comment */\n"
10027 " \"5678\";",
10028 getLLVMStyleWithColumns(28));
10029 verifyFormat(
10030 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10031 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10032 " \"aaaaaaaaaaaaaaaa\";",
10033 "aaaaaa ="
10034 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10035 "aaaaaaaaaaaaaaaaaaaaa\" "
10036 "\"aaaaaaaaaaaaaaaa\";");
10037 verifyFormat("a = a + \"a\"\n"
10038 " \"a\"\n"
10039 " \"a\";");
10040 verifyFormat("f(\"a\", \"b\"\n"
10041 " \"c\");");
10042
10043 verifyFormat(
10044 "#define LL_FORMAT \"ll\"\n"
10045 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10046 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10047
10048 verifyFormat("#define A(X) \\\n"
10049 " \"aaaaa\" #X \"bbbbbb\" \\\n"
10050 " \"ccccc\"",
10051 getLLVMStyleWithColumns(23));
10052 verifyFormat("#define A \"def\"\n"
10053 "f(\"abc\" A \"ghi\"\n"
10054 " \"jkl\");");
10055
10056 verifyFormat("f(L\"a\"\n"
10057 " L\"b\");");
10058 verifyFormat("#define A(X) \\\n"
10059 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10060 " L\"ccccc\"",
10061 getLLVMStyleWithColumns(25));
10062
10063 verifyFormat("f(@\"a\"\n"
10064 " @\"b\");");
10065 verifyFormat("NSString s = @\"a\"\n"
10066 " @\"b\"\n"
10067 " @\"c\";");
10068 verifyFormat("NSString s = @\"a\"\n"
10069 " \"b\"\n"
10070 " \"c\";");
10071 }
10072
10073 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
10074 FormatStyle Style = getLLVMStyle();
10075 Style.ColumnLimit = 60;
10076
10077 // No declarations or definitions should be moved to own line.
10078 Style.BreakAfterReturnType = FormatStyle::RTBS_None;
10079 verifyFormat("class A {\n"
10080 " int f() { return 1; }\n"
10081 " int g();\n"
10082 " long\n"
10083 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10084 "};\n"
10085 "int f() { return 1; }\n"
10086 "int g();\n"
10087 "int foooooooooooooooooooooooooooo::\n"
10088 " baaaaaaaaaaaaaaaaaaaaar();",
10089 Style);
10090
10091 // It is now allowed to break after a short return type if necessary.
10092 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
10093 verifyFormat("class A {\n"
10094 " int f() { return 1; }\n"
10095 " int g();\n"
10096 " long\n"
10097 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10098 "};\n"
10099 "int f() { return 1; }\n"
10100 "int g();\n"
10101 "int\n"
10102 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10103 Style);
10104
10105 // It now must never break after a short return type.
10106 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
10107 verifyFormat("class A {\n"
10108 " int f() { return 1; }\n"
10109 " int g();\n"
10110 " long foooooooooooooooooooooooooooo::\n"
10111 " baaaaaaaaaaaaaaaaaaaar();\n"
10112 "};\n"
10113 "int f() { return 1; }\n"
10114 "int g();\n"
10115 "int foooooooooooooooooooooooooooo::\n"
10116 " baaaaaaaaaaaaaaaaaaaaar();",
10117 Style);
10118
10119 // All declarations and definitions should have the return type moved to its
10120 // own line.
10121 Style.BreakAfterReturnType = FormatStyle::RTBS_All;
10122 Style.TypenameMacros = {"LIST"};
10123 verifyFormat("SomeType\n"
10124 "funcdecl(LIST(uint64_t));",
10125 Style);
10126 verifyFormat("class E {\n"
10127 " int\n"
10128 " f() {\n"
10129 " return 1;\n"
10130 " }\n"
10131 " int\n"
10132 " g();\n"
10133 " long\n"
10134 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10135 "};\n"
10136 "int\n"
10137 "f() {\n"
10138 " return 1;\n"
10139 "}\n"
10140 "int\n"
10141 "g();\n"
10142 "int\n"
10143 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10144 Style);
10145
10146 // Top-level definitions, and no kinds of declarations should have the
10147 // return type moved to its own line.
10148 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10149 verifyFormat("class B {\n"
10150 " int f() { return 1; }\n"
10151 " int g();\n"
10152 "};\n"
10153 "int\n"
10154 "f() {\n"
10155 " return 1;\n"
10156 "}\n"
10157 "int g();",
10158 Style);
10159
10160 // Top-level definitions and declarations should have the return type moved
10161 // to its own line.
10162 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10163 verifyFormat("class C {\n"
10164 " int f() { return 1; }\n"
10165 " int g();\n"
10166 "};\n"
10167 "int\n"
10168 "f() {\n"
10169 " return 1;\n"
10170 "}\n"
10171 "int\n"
10172 "g();\n"
10173 "int\n"
10174 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10175 Style);
10176
10177 // All definitions should have the return type moved to its own line, but no
10178 // kinds of declarations.
10179 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10180 verifyFormat("class D {\n"
10181 " int\n"
10182 " f() {\n"
10183 " return 1;\n"
10184 " }\n"
10185 " int g();\n"
10186 "};\n"
10187 "int\n"
10188 "f() {\n"
10189 " return 1;\n"
10190 "}\n"
10191 "int g();",
10192 Style);
10193 verifyFormat("const char *\n"
10194 "f(void) {\n" // Break here.
10195 " return \"\";\n"
10196 "}\n"
10197 "const char *bar(void);", // No break here.
10198 Style);
10199 verifyFormat("template <class T>\n"
10200 "T *\n"
10201 "f(T &c) {\n" // Break here.
10202 " return NULL;\n"
10203 "}\n"
10204 "template <class T> T *f(T &c);", // No break here.
10205 Style);
10206 verifyFormat("class C {\n"
10207 " int\n"
10208 " operator+() {\n"
10209 " return 1;\n"
10210 " }\n"
10211 " int\n"
10212 " operator()() {\n"
10213 " return 1;\n"
10214 " }\n"
10215 "};",
10216 Style);
10217 verifyFormat("void\n"
10218 "A::operator()() {}\n"
10219 "void\n"
10220 "A::operator>>() {}\n"
10221 "void\n"
10222 "A::operator+() {}\n"
10223 "void\n"
10224 "A::operator*() {}\n"
10225 "void\n"
10226 "A::operator->() {}\n"
10227 "void\n"
10228 "A::operator&() {}\n"
10229 "void\n"
10230 "A::operator&&() {}\n"
10231 "void\n"
10232 "A::operator[]() {}\n"
10233 "void\n"
10234 "A::operator!() {}\n"
10235 "void\n"
10236 "A::operator<Foo> *() {}\n"
10237 "void\n"
10238 "A::operator<Foo> &() {}\n",
10239 Style);
10240 verifyFormat("constexpr auto\n"
10241 "operator()() const -> reference {}\n"
10242 "constexpr auto\n"
10243 "operator>>() const -> reference {}\n"
10244 "constexpr auto\n"
10245 "operator+() const -> reference {}\n"
10246 "constexpr auto\n"
10247 "operator*() const -> reference {}\n"
10248 "constexpr auto\n"
10249 "operator->() const -> reference {}\n"
10250 "constexpr auto\n"
10251 "operator++() const -> reference {}\n"
10252 "constexpr auto\n"
10253 "operator void *() const -> reference {}\n"
10254 "constexpr auto\n"
10255 "operator void **() const -> reference {}\n"
10256 "constexpr auto\n"
10257 "operator void *() const -> reference {}\n"
10258 "constexpr auto\n"
10259 "operator void &() const -> reference {}\n"
10260 "constexpr auto\n"
10261 "operator&&() const -> reference {}\n"
10262 "constexpr auto\n"
10263 "operator char *() const -> reference {}\n"
10264 "constexpr auto\n"
10265 "operator!() const -> reference {}\n"
10266 "constexpr auto\n"
10267 "operator[]() const -> reference {}",
10268 Style);
10269 verifyFormat("void *operator new(std::size_t s);", // No break here.
10270 Style);
10271 verifyFormat("void *\n"
10272 "operator new(std::size_t s) {}",
10273 Style);
10274 verifyFormat("void *\n"
10275 "operator delete[](void *ptr) {}",
10276 Style);
10277 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10278 verifyFormat("const char *\n"
10279 "f(void)\n" // Break here.
10280 "{\n"
10281 " return \"\";\n"
10282 "}\n"
10283 "const char *bar(void);", // No break here.
10284 Style);
10285 verifyFormat("template <class T>\n"
10286 "T *\n" // Problem here: no line break
10287 "f(T &c)\n" // Break here.
10288 "{\n"
10289 " return NULL;\n"
10290 "}\n"
10291 "template <class T> T *f(T &c);", // No break here.
10292 Style);
10293 verifyFormat("int\n"
10294 "foo(A<bool> a)\n"
10295 "{\n"
10296 " return a;\n"
10297 "}",
10298 Style);
10299 verifyFormat("int\n"
10300 "foo(A<8> a)\n"
10301 "{\n"
10302 " return a;\n"
10303 "}",
10304 Style);
10305 verifyFormat("int\n"
10306 "foo(A<B<bool>, 8> a)\n"
10307 "{\n"
10308 " return a;\n"
10309 "}",
10310 Style);
10311 verifyFormat("int\n"
10312 "foo(A<B<8>, bool> a)\n"
10313 "{\n"
10314 " return a;\n"
10315 "}",
10316 Style);
10317 verifyFormat("int\n"
10318 "foo(A<B<bool>, bool> a)\n"
10319 "{\n"
10320 " return a;\n"
10321 "}",
10322 Style);
10323 verifyFormat("int\n"
10324 "foo(A<B<8>, 8> a)\n"
10325 "{\n"
10326 " return a;\n"
10327 "}",
10328 Style);
10329
10330 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10331 Style.BraceWrapping.AfterFunction = true;
10332 verifyFormat("int f(i);\n" // No break here.
10333 "int\n" // Break here.
10334 "f(i)\n"
10335 "{\n"
10336 " return i + 1;\n"
10337 "}\n"
10338 "int\n" // Break here.
10339 "f(i)\n"
10340 "{\n"
10341 " return i + 1;\n"
10342 "};",
10343 Style);
10344 verifyFormat("int f(a, b, c);\n" // No break here.
10345 "int\n" // Break here.
10346 "f(a, b, c)\n" // Break here.
10347 "short a, b;\n"
10348 "float c;\n"
10349 "{\n"
10350 " return a + b < c;\n"
10351 "}\n"
10352 "int\n" // Break here.
10353 "f(a, b, c)\n" // Break here.
10354 "short a, b;\n"
10355 "float c;\n"
10356 "{\n"
10357 " return a + b < c;\n"
10358 "};",
10359 Style);
10360 verifyFormat("byte *\n" // Break here.
10361 "f(a)\n" // Break here.
10362 "byte a[];\n"
10363 "{\n"
10364 " return a;\n"
10365 "}",
10366 Style);
10367 verifyFormat("byte *\n"
10368 "f(a)\n"
10369 "byte /* K&R C */ a[];\n"
10370 "{\n"
10371 " return a;\n"
10372 "}\n"
10373 "byte *\n"
10374 "g(p)\n"
10375 "byte /* K&R C */ *p;\n"
10376 "{\n"
10377 " return p;\n"
10378 "}",
10379 Style);
10380 verifyFormat("bool f(int a, int) override;\n"
10381 "Bar g(int a, Bar) final;\n"
10382 "Bar h(a, Bar) final;",
10383 Style);
10384 verifyFormat("int\n"
10385 "f(a)",
10386 Style);
10387 verifyFormat("bool\n"
10388 "f(size_t = 0, bool b = false)\n"
10389 "{\n"
10390 " return !b;\n"
10391 "}",
10392 Style);
10393
10394 // The return breaking style doesn't affect:
10395 // * function and object definitions with attribute-like macros
10396 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10397 " ABSL_GUARDED_BY(mutex) = {};",
10398 getGoogleStyleWithColumns(40));
10399 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10400 " ABSL_GUARDED_BY(mutex); // comment",
10401 getGoogleStyleWithColumns(40));
10402 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10403 " ABSL_GUARDED_BY(mutex1)\n"
10404 " ABSL_GUARDED_BY(mutex2);",
10405 getGoogleStyleWithColumns(40));
10406 verifyFormat("Tttttt f(int a, int b)\n"
10407 " ABSL_GUARDED_BY(mutex1)\n"
10408 " ABSL_GUARDED_BY(mutex2);",
10409 getGoogleStyleWithColumns(40));
10410 // * typedefs
10411 verifyGoogleFormat("typedef ATTR(X) char x;");
10412
10413 Style = getGNUStyle();
10414
10415 // Test for comments at the end of function declarations.
10416 verifyFormat("void\n"
10417 "foo (int a, /*abc*/ int b) // def\n"
10418 "{\n"
10419 "}",
10420 Style);
10421
10422 verifyFormat("void\n"
10423 "foo (int a, /* abc */ int b) /* def */\n"
10424 "{\n"
10425 "}",
10426 Style);
10427
10428 // Definitions that should not break after return type
10429 verifyFormat("void foo (int a, int b); // def", Style);
10430 verifyFormat("void foo (int a, int b); /* def */", Style);
10431 verifyFormat("void foo (int a, int b);", Style);
10432 }
10433
10434 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10435 FormatStyle NoBreak = getLLVMStyle();
10436 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10437 FormatStyle Break = getLLVMStyle();
10438 Break.AlwaysBreakBeforeMultilineStrings = true;
10439 verifyFormat("aaaa = \"bbbb\"\n"
10440 " \"cccc\";",
10441 NoBreak);
10442 verifyFormat("aaaa =\n"
10443 " \"bbbb\"\n"
10444 " \"cccc\";",
10445 Break);
10446 verifyFormat("aaaa(\"bbbb\"\n"
10447 " \"cccc\");",
10448 NoBreak);
10449 verifyFormat("aaaa(\n"
10450 " \"bbbb\"\n"
10451 " \"cccc\");",
10452 Break);
10453 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10454 " \"cccc\");",
10455 NoBreak);
10456 verifyFormat("aaaa(qqq,\n"
10457 " \"bbbb\"\n"
10458 " \"cccc\");",
10459 Break);
10460 verifyFormat("aaaa(qqq,\n"
10461 " L\"bbbb\"\n"
10462 " L\"cccc\");",
10463 Break);
10464 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10465 " \"bbbb\"));",
10466 Break);
10467 verifyFormat("string s = someFunction(\n"
10468 " \"abc\"\n"
10469 " \"abc\");",
10470 Break);
10471
10472 // As we break before unary operators, breaking right after them is bad.
10473 verifyFormat("string foo = abc ? \"x\"\n"
10474 " \"blah blah blah blah blah blah\"\n"
10475 " : \"y\";",
10476 Break);
10477
10478 // Don't break if there is no column gain.
10479 verifyFormat("f(\"aaaa\"\n"
10480 " \"bbbb\");",
10481 Break);
10482
10483 // Treat literals with escaped newlines like multi-line string literals.
10484 verifyNoChange("x = \"a\\\n"
10485 "b\\\n"
10486 "c\";",
10487 NoBreak);
10488 verifyFormat("xxxx =\n"
10489 " \"a\\\n"
10490 "b\\\n"
10491 "c\";",
10492 "xxxx = \"a\\\n"
10493 "b\\\n"
10494 "c\";",
10495 Break);
10496
10497 verifyFormat("NSString *const kString =\n"
10498 " @\"aaaa\"\n"
10499 " @\"bbbb\";",
10500 "NSString *const kString = @\"aaaa\"\n"
10501 "@\"bbbb\";",
10502 Break);
10503
10504 Break.ColumnLimit = 0;
10505 verifyFormat("const char *hello = \"hello llvm\";", Break);
10506 }
10507
10508 TEST_F(FormatTest, AlignsPipes) {
10509 verifyFormat(
10510 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10511 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10512 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10513 verifyFormat(
10514 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10515 " << aaaaaaaaaaaaaaaaaaaa;");
10516 verifyFormat(
10517 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10518 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10519 verifyFormat(
10520 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10521 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10522 verifyFormat(
10523 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10524 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10525 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10526 verifyFormat(
10527 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10528 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10529 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10530 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10533 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10534 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10535 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10536 verifyFormat(
10537 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10539 verifyFormat(
10540 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10541 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10542
10543 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10544 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10545 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10547 " aaaaaaaaaaaaaaaaaaaaa)\n"
10548 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10549 verifyFormat("LOG_IF(aaa == //\n"
10550 " bbb)\n"
10551 " << a << b;");
10552
10553 // But sometimes, breaking before the first "<<" is desirable.
10554 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10555 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10556 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10557 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10558 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10559 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10560 " << BEF << IsTemplate << Description << E->getType();");
10561 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10562 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10564 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10565 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10567 " << aaa;");
10568
10569 verifyFormat(
10570 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10571 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10572
10573 // Incomplete string literal.
10574 verifyFormat("llvm::errs() << \"\n"
10575 " << a;",
10576 "llvm::errs() << \"\n<<a;");
10577
10578 verifyFormat("void f() {\n"
10579 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10580 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10581 "}");
10582
10583 // Handle 'endl'.
10584 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10585 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10586 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10587
10588 // Handle '\n'.
10589 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10590 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10591 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10592 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10593 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10594 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10595 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10596 }
10597
10598 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10599 verifyFormat("return out << \"somepacket = {\\n\"\n"
10600 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10601 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10602 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10603 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10604 " << \"}\";");
10605
10606 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10607 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10608 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10609 verifyFormat(
10610 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10611 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10612 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10613 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10614 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10615 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10616 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10617 verifyFormat(
10618 "void f() {\n"
10619 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10620 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10621 "}");
10622
10623 // Breaking before the first "<<" is generally not desirable.
10624 verifyFormat(
10625 "llvm::errs()\n"
10626 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10627 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10628 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10629 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10630 getLLVMStyleWithColumns(70));
10631 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10632 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10633 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10634 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10635 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10636 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10637 getLLVMStyleWithColumns(70));
10638
10639 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10640 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10641 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10642 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10643 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10644 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10645 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10646 " (aaaa + aaaa);",
10647 getLLVMStyleWithColumns(40));
10648 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10649 " (aaaaaaa + aaaaa));",
10650 getLLVMStyleWithColumns(40));
10651 verifyFormat(
10652 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10653 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10654 " bbbbbbbbbbbbbbbbbbbbbbb);");
10655 }
10656
10657 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10658 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10659
10660 verifyNoChange("QStringList() << \"foo\"\n"
10661 " << \"bar\";");
10662
10663 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10664 "log_error(log, \"foo\"\n"
10665 " << \"bar\");");
10666 }
10667
10668 TEST_F(FormatTest, UnderstandsEquals) {
10669 verifyFormat(
10670 "aaaaaaaaaaaaaaaaa =\n"
10671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10672 verifyFormat(
10673 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10675 verifyFormat(
10676 "if (a) {\n"
10677 " f();\n"
10678 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10680 "}");
10681
10682 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10683 " 100000000 + 10000000) {\n}");
10684 }
10685
10686 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10687 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10688 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10689
10690 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10691 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10692
10693 verifyFormat(
10694 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10695 " Parameter2);");
10696
10697 verifyFormat(
10698 "ShortObject->shortFunction(\n"
10699 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10700 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10701
10702 verifyFormat("loooooooooooooongFunction(\n"
10703 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10704
10705 verifyFormat(
10706 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10707 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10708
10709 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10710 " .WillRepeatedly(Return(SomeValue));");
10711 verifyFormat("void f() {\n"
10712 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10713 " .Times(2)\n"
10714 " .WillRepeatedly(Return(SomeValue));\n"
10715 "}");
10716 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10717 " ccccccccccccccccccccccc);");
10718 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10720 " .aaaaa(aaaaa),\n"
10721 " aaaaaaaaaaaaaaaaaaaaa);");
10722 verifyFormat("void f() {\n"
10723 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10724 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10725 "}");
10726 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10728 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10729 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10730 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10731 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10732 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10733 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10734 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10735 "}");
10736
10737 // Here, it is not necessary to wrap at "." or "->".
10738 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10739 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10740 verifyFormat(
10741 "aaaaaaaaaaa->aaaaaaaaa(\n"
10742 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10743 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10744
10745 verifyFormat(
10746 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10748 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10749 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10750 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10751 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10752
10753 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10755 " .a();");
10756
10757 FormatStyle NoBinPacking = getLLVMStyle();
10758 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
10759 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10760 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10761 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10762 " aaaaaaaaaaaaaaaaaaa,\n"
10763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10764 NoBinPacking);
10765
10766 // If there is a subsequent call, change to hanging indentation.
10767 verifyFormat(
10768 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10769 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10770 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10771 verifyFormat(
10772 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10773 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10774 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10776 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10777 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10779 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10780 }
10781
10782 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10783 verifyFormat("template <typename T>\n"
10784 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10785 verifyFormat("template <typename T>\n"
10786 "// T should be one of {A, B}.\n"
10787 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10788 verifyFormat(
10789 "template <typename T>\n"
10790 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10791 verifyFormat("template <typename T>\n"
10792 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10793 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10794 verifyFormat(
10795 "template <typename T>\n"
10796 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10797 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10798 verifyFormat(
10799 "template <typename T>\n"
10800 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10801 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10803 verifyFormat("template <typename T>\n"
10804 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10805 " int aaaaaaaaaaaaaaaaaaaaaa);");
10806 verifyFormat(
10807 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10808 " typename T4 = char>\n"
10809 "void f();");
10810 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10811 " template <typename> class cccccccccccccccccccccc,\n"
10812 " typename ddddddddddddd>\n"
10813 "class C {};");
10814 verifyFormat(
10815 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10817
10818 verifyFormat("void f() {\n"
10819 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10820 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10821 "}");
10822
10823 verifyFormat("template <typename T> class C {};");
10824 verifyFormat("template <typename T> void f();");
10825 verifyFormat("template <typename T> void f() {}");
10826 verifyFormat(
10827 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10830 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10833 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10834 getLLVMStyleWithColumns(72));
10835 verifyFormat("static_cast<A< //\n"
10836 " B> *>(\n"
10837 "\n"
10838 ");",
10839 "static_cast<A<//\n"
10840 " B>*>(\n"
10841 "\n"
10842 " );");
10843 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10844 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10845
10846 FormatStyle AlwaysBreak = getLLVMStyle();
10847 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10848 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10849 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10850 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10851 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10852 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10853 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10854 verifyFormat("template <template <typename> class Fooooooo,\n"
10855 " template <typename> class Baaaaaaar>\n"
10856 "struct C {};",
10857 AlwaysBreak);
10858 verifyFormat("template <typename T> // T can be A, B or C.\n"
10859 "struct C {};",
10860 AlwaysBreak);
10861 verifyFormat("template <typename T>\n"
10862 "C(T) noexcept;",
10863 AlwaysBreak);
10864 verifyFormat("template <typename T>\n"
10865 "ClassName(T) noexcept;",
10866 AlwaysBreak);
10867 verifyFormat("template <typename T>\n"
10868 "POOR_NAME(T) noexcept;",
10869 AlwaysBreak);
10870 verifyFormat("template <enum E> class A {\n"
10871 "public:\n"
10872 " E *f();\n"
10873 "};");
10874
10875 FormatStyle NeverBreak = getLLVMStyle();
10876 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
10877 verifyFormat("template <typename T> class C {};", NeverBreak);
10878 verifyFormat("template <typename T> void f();", NeverBreak);
10879 verifyFormat("template <typename T> void f() {}", NeverBreak);
10880 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10881 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10882 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10883 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10884 "bbbbbbbbbbbbbbbbbbbb) {}",
10885 NeverBreak);
10886 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10887 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10888 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10889 NeverBreak);
10890 verifyFormat("template <template <typename> class Fooooooo,\n"
10891 " template <typename> class Baaaaaaar>\n"
10892 "struct C {};",
10893 NeverBreak);
10894 verifyFormat("template <typename T> // T can be A, B or C.\n"
10895 "struct C {};",
10896 NeverBreak);
10897 verifyFormat("template <enum E> class A {\n"
10898 "public:\n"
10899 " E *f();\n"
10900 "};",
10901 NeverBreak);
10902 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10903 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10904 "bbbbbbbbbbbbbbbbbbbb) {}",
10905 NeverBreak);
10906
10907 auto Style = getLLVMStyle();
10908 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
10909
10910 verifyNoChange("template <typename T>\n"
10911 "class C {};",
10912 Style);
10913 verifyFormat("template <typename T> class C {};", Style);
10914
10915 verifyNoChange("template <typename T>\n"
10916 "void f();",
10917 Style);
10918 verifyFormat("template <typename T> void f();", Style);
10919
10920 verifyNoChange("template <typename T>\n"
10921 "void f() {}",
10922 Style);
10923 verifyFormat("template <typename T> void f() {}", Style);
10924
10925 verifyNoChange("template <typename T>\n"
10926 "// T can be A, B or C.\n"
10927 "struct C {};",
10928 Style);
10929 verifyFormat("template <typename T> // T can be A, B or C.\n"
10930 "struct C {};",
10931 Style);
10932
10933 verifyNoChange("template <typename T>\n"
10934 "C(T) noexcept;",
10935 Style);
10936 verifyFormat("template <typename T> C(T) noexcept;", Style);
10937
10938 verifyNoChange("template <enum E>\n"
10939 "class A {\n"
10940 "public:\n"
10941 " E *f();\n"
10942 "};",
10943 Style);
10944 verifyFormat("template <enum E> class A {\n"
10945 "public:\n"
10946 " E *f();\n"
10947 "};",
10948 Style);
10949
10950 verifyNoChange("template <auto x>\n"
10951 "constexpr int simple(int) {\n"
10952 " char c;\n"
10953 " return 1;\n"
10954 "}",
10955 Style);
10956 verifyFormat("template <auto x> constexpr int simple(int) {\n"
10957 " char c;\n"
10958 " return 1;\n"
10959 "}",
10960 Style);
10961
10962 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
10963 verifyNoChange("template <auto x>\n"
10964 "requires(x > 1)\n"
10965 "constexpr int with_req(int) {\n"
10966 " return 1;\n"
10967 "}",
10968 Style);
10969 verifyFormat("template <auto x> requires(x > 1)\n"
10970 "constexpr int with_req(int) {\n"
10971 " return 1;\n"
10972 "}",
10973 Style);
10974 }
10975
10976 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
10977 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
10978 Style.ColumnLimit = 60;
10979 verifyFormat("// Baseline - no comments.\n"
10980 "template <\n"
10981 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10982 "void f() {}",
10983 Style);
10984
10985 verifyFormat("template <\n"
10986 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10987 "void f() {}",
10988 "template <\n"
10989 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10990 "void f() {}",
10991 Style);
10992
10993 verifyFormat(
10994 "template <\n"
10995 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10996 "void f() {}",
10997 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10998 "void f() {}",
10999 Style);
11000
11001 verifyFormat("template <\n"
11002 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11003 " // multiline\n"
11004 "void f() {}",
11005 "template <\n"
11006 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11007 " // multiline\n"
11008 "void f() {}",
11009 Style);
11010
11011 verifyFormat(
11012 "template <typename aaaaaaaaaa<\n"
11013 " bbbbbbbbbbbb>::value> // trailing loooong\n"
11014 "void f() {}",
11015 "template <\n"
11016 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11017 "void f() {}",
11018 Style);
11019 }
11020
11021 TEST_F(FormatTest, BreakBeforeTemplateCloser) {
11022 auto Style = getLLVMStyle();
11023 // Begin with tests covering the case where there is no constraint on the
11024 // column limit.
11025 Style.ColumnLimit = 0;
11026 Style.BreakBeforeTemplateCloser = true;
11027 // BreakBeforeTemplateCloser should NOT force template declarations onto
11028 // multiple lines.
11029 verifyFormat("template <typename Foo>\n"
11030 "void foo() {}",
11031 Style);
11032 verifyFormat("template <typename Foo, typename Bar>\n"
11033 "void foo() {}",
11034 Style);
11035 // It should add a line break before > if not already present:
11036 verifyFormat("template <\n"
11037 " typename Foo\n"
11038 ">\n"
11039 "void foo() {}",
11040 "template <\n"
11041 " typename Foo>\n"
11042 "void foo() {}",
11043 Style);
11044 verifyFormat("template <\n"
11045 " typename Foo,\n"
11046 " typename Bar\n"
11047 ">\n"
11048 "void foo() {}",
11049 "template <\n"
11050 " typename Foo,\n"
11051 " typename Bar>\n"
11052 "void foo() {}",
11053 Style);
11054 // When within an indent scope, the > should be placed accordingly:
11055 verifyFormat("struct Baz {\n"
11056 " template <\n"
11057 " typename Foo,\n"
11058 " typename Bar\n"
11059 " >\n"
11060 " void foo() {}\n"
11061 "};",
11062 "struct Baz {\n"
11063 " template <\n"
11064 " typename Foo,\n"
11065 " typename Bar>\n"
11066 " void foo() {}\n"
11067 "};",
11068 Style);
11069
11070 // Test from https://github.com/llvm/llvm-project/issues/80049:
11071 verifyFormat(
11072 "using type = std::remove_cv_t<\n"
11073 " add_common_cv_reference<\n"
11074 " std::common_type_t<std::decay_t<T0>, std::decay_t<T1>>,\n"
11075 " T0,\n"
11076 " T1\n"
11077 " >\n"
11078 ">;",
11079 "using type = std::remove_cv_t<\n"
11080 " add_common_cv_reference<\n"
11081 " std::common_type_t<std::decay_t<T0>, std::decay_t<T1>>,\n"
11082 " T0,\n"
11083 " T1>>;",
11084 Style);
11085
11086 // Test lambda goes to next line:
11087 verifyFormat("void foo() {\n"
11088 " auto lambda = []<\n"
11089 " typename T\n"
11090 " >(T t) {\n"
11091 " };\n"
11092 "}",
11093 "void foo() {\n"
11094 " auto lambda = []<\n"
11095 " typename T>(T t){\n"
11096 " };\n"
11097 "}",
11098 Style);
11099 // With no column limit, two parameters can go on the same line:
11100 verifyFormat("void foo() {\n"
11101 " auto lambda = []<\n"
11102 " typename T, typename Foo\n"
11103 " >(T t) {\n"
11104 " };\n"
11105 "}",
11106 "void foo() {\n"
11107 " auto lambda = []<\n"
11108 " typename T, typename Foo>(T t){\n"
11109 " };\n"
11110 "}",
11111 Style);
11112 // Or on different lines:
11113 verifyFormat("void foo() {\n"
11114 " auto lambda = []<\n"
11115 " typename T,\n"
11116 " typename Foo\n"
11117 " >(T t) {\n"
11118 " };\n"
11119 "}",
11120 "void foo() {\n"
11121 " auto lambda = []<\n"
11122 " typename T,\n"
11123 " typename Foo>(T t){\n"
11124 " };\n"
11125 "}",
11126 Style);
11127
11128 // Test template usage goes to next line too:
11129 verifyFormat("void foo() {\n"
11130 " myFunc<\n"
11131 " T\n"
11132 " >();\n"
11133 "}",
11134 "void foo() {\n"
11135 " myFunc<\n"
11136 " T>();\n"
11137 "}",
11138 Style);
11139
11140 // Now test that it handles the cases when the column limit forces wrapping.
11141 Style.ColumnLimit = 40;
11142 // The typename goes on the first line if it fits:
11143 verifyFormat("template <typename Fooooooooooooooooooo,\n"
11144 " typename Bar>\n"
11145 "void foo() {}",
11146 Style);
11147 verifyFormat("template <typename Foo,\n"
11148 " typename Barrrrrrrrrrrrrrrrrr>\n"
11149 "void foo() {}",
11150 Style);
11151 // Long names should be split in one step:
11152 verifyFormat("template <\n"
11153 " typename Foo,\n"
11154 " typename Barrrrrrrrrrrrrrrrrrr\n"
11155 ">\n"
11156 "void foo() {}",
11157 "template <typename Foo, typename Barrrrrrrrrrrrrrrrrrr>\n"
11158 "void foo() {}",
11159 Style);
11160 verifyFormat("template <\n"
11161 " typename Foooooooooooooooooooo,\n"
11162 " typename Bar\n"
11163 ">\n"
11164 "void foo() {}",
11165 "template <typename Foooooooooooooooooooo, typename Bar>\n"
11166 "void foo() {}",
11167 Style);
11168 // Even when there is only one long name:
11169 verifyFormat("template <\n"
11170 " typename Foooooooooooooooooooo\n"
11171 ">\n"
11172 "void foo() {}",
11173 "template <typename Foooooooooooooooooooo>\n"
11174 "void foo() {}",
11175 Style);
11176 // Test lambda goes to next line if the type is looong:
11177 verifyFormat("void foo() {\n"
11178 " auto lambda =\n"
11179 " []<\n"
11180 " typename Loooooooooooooooooooooooooooooooooong\n"
11181 " >(T t) {};\n"
11182 " auto lambda =\n"
11183 " [looooooooooooooong]<\n"
11184 " typename Loooooooooooooooooooooooooooooooooong\n"
11185 " >(T t) {};\n"
11186 " auto lambda =\n"
11187 " []<\n"
11188 " typename T,\n"
11189 " typename Loooooooooooooooooooooooooooooooooong\n"
11190 " >(T t) {};\n"
11191 // Nested:
11192 " auto lambda =\n"
11193 " []<\n"
11194 " template <typename, typename>\n"
11195 " typename Looooooooooooooooooong\n"
11196 " >(T t) {};\n"
11197 // Same idea, the "T" is now short rather than Looong:
11198 " auto lambda =\n"
11199 " []<template <typename, typename>\n"
11200 " typename T>(T t) {};\n"
11201 // Nested with long capture forces the style to block indent:
11202 " auto lambda =\n"
11203 " [loooooooooooooooooooong]<\n"
11204 " template <typename, typename>\n"
11205 " typename Looooooooooooooooooong\n"
11206 " >(T t) {};\n"
11207 // But *now* it stays block indented even when T is short:
11208 " auto lambda =\n"
11209 " [loooooooooooooooooooong]<\n"
11210 " template <typename, typename>\n"
11211 " typename T\n"
11212 " >(T t) {};\n"
11213 // Nested, with long name and long captures:
11214 " auto lambda =\n"
11215 " [loooooooooooooooooooong]<\n"
11216 " template <\n"
11217 " typename Foooooooooooooooo,\n"
11218 " typename\n"
11219 " >\n"
11220 " typename T\n"
11221 " >(T t) {};\n"
11222 // Allow the nested template to be on the same line:
11223 " auto lambda =\n"
11224 " [loooooooooooooooooooong]<\n"
11225 " template <typename Fooooooooo,\n"
11226 " typename>\n"
11227 " typename T\n"
11228 " >(T t) {};\n"
11229 "}",
11230 Style);
11231
11232 // Test template usage goes to next line if the type is looong:
11233 verifyFormat("void foo() {\n"
11234 " myFunc<\n"
11235 " Looooooooooooooooooooooooong\n"
11236 " >();\n"
11237 "}",
11238 Style);
11239 // Even a single type in the middle is enough to force it to block indent
11240 // style:
11241 verifyFormat("void foo() {\n"
11242 " myFunc<\n"
11243 " Foo, Foo, Foo,\n"
11244 " Foooooooooooooooooooooooooooooo,\n"
11245 " Foo, Foo, Foo, Foo\n"
11246 " >();\n"
11247 "}",
11248 Style);
11249 }
11250
11251 TEST_F(FormatTest, WrapsTemplateParameters) {
11252 FormatStyle Style = getLLVMStyle();
11253 Style.AlignAfterOpenBracket = false;
11254 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11255 verifyFormat(
11256 "template <typename... a> struct q {};\n"
11257 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11258 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11259 " y;",
11260 Style);
11261 Style.AlignAfterOpenBracket = false;
11262 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11263 verifyFormat(
11264 "template <typename... a> struct r {};\n"
11265 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11266 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11267 " y;",
11268 Style);
11269 Style.BreakAfterOpenBracketFunction = true;
11270 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11271 verifyFormat("template <typename... a> struct s {};\n"
11272 "extern s<\n"
11273 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11274 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11275 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11276 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11277 " y;",
11278 Style);
11279 Style.BreakAfterOpenBracketFunction = true;
11280 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11281 verifyFormat("template <typename... a> struct t {};\n"
11282 "extern t<\n"
11283 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11284 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11285 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11286 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11287 " y;",
11288 Style);
11289 }
11290
11291 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
11292 verifyFormat(
11293 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11295 verifyFormat(
11296 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11298 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11299
11300 // FIXME: Should we have the extra indent after the second break?
11301 verifyFormat(
11302 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11305
11306 verifyFormat(
11307 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11308 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11309
11310 // Breaking at nested name specifiers is generally not desirable.
11311 verifyFormat(
11312 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11313 " aaaaaaaaaaaaaaaaaaaaaaa);");
11314
11315 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11318 " aaaaaaaaaaaaaaaaaaaaa);",
11319 getLLVMStyleWithColumns(74));
11320
11321 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11323 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11324
11325 verifyFormat(
11326 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11327 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11328 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11329 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11330 }
11331
11332 TEST_F(FormatTest, UnderstandsTemplateParameters) {
11333 verifyFormat("A<int> a;");
11334 verifyFormat("A<A<A<int>>> a;");
11335 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11336 verifyFormat("bool x = a < 1 || 2 > a;");
11337 verifyFormat("bool x = 5 < f<int>();");
11338 verifyFormat("bool x = f<int>() > 5;");
11339 verifyFormat("bool x = 5 < a<int>::x;");
11340 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11341 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11342
11343 verifyGoogleFormat("A<A<int>> a;");
11344 verifyGoogleFormat("A<A<A<int>>> a;");
11345 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11346 verifyGoogleFormat("A<A<int> > a;");
11347 verifyGoogleFormat("A<A<A<int> > > a;");
11348 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11349 verifyGoogleFormat("A<::A<int>> a;");
11350 verifyGoogleFormat("A<::A> a;");
11351 verifyGoogleFormat("A< ::A> a;");
11352 verifyGoogleFormat("A< ::A<int> > a;");
11353 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11354 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11355 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11356 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11357 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11358 getGoogleStyle());
11359
11360 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11361
11362 // template closer followed by a token that starts with > or =
11363 verifyFormat("bool b = a<1> > 1;");
11364 verifyFormat("bool b = a<1> >= 1;");
11365 verifyFormat("int i = a<1> >> 1;");
11366 FormatStyle Style = getLLVMStyle();
11367 Style.SpaceBeforeAssignmentOperators = false;
11368 verifyFormat("bool b= a<1> == 1;", Style);
11369 verifyFormat("a<int> = 1;", Style);
11370 verifyFormat("a<int> >>= 1;", Style);
11371
11372 verifyFormat("test < a | b >> c;");
11373 verifyFormat("test<test<a | b>> c;");
11374 verifyFormat("test >> a >> b;");
11375 verifyFormat("test << a >> b;");
11376
11377 verifyFormat("f<int>();");
11378 verifyFormat("template <typename T> void f() {}");
11379 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11380 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11381 "sizeof(char)>::type>;");
11382 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11383 verifyFormat("f(a.operator()<A>());");
11384 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11385 " .template operator()<A>());",
11386 getLLVMStyleWithColumns(35));
11387 verifyFormat("bool_constant<a && noexcept(f())>;");
11388 verifyFormat("bool_constant<a || noexcept(f())>;");
11389
11390 verifyFormat("if (std::tuple_size_v<T> > 0)");
11391
11392 // Not template parameters.
11393 verifyFormat("return a < b && c > d;");
11394 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11395 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11396 verifyFormat("void f() {\n"
11397 " while (a < b && c > d) {\n"
11398 " }\n"
11399 "}");
11400 verifyFormat("template <typename... Types>\n"
11401 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11402
11403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11404 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11405 getLLVMStyleWithColumns(60));
11406 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11407 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11408 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11409 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11410
11411 verifyFormat("#define FOO(typeName, realClass) \\\n"
11412 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11413 getLLVMStyleWithColumns(60));
11414 }
11415
11416 TEST_F(FormatTest, UnderstandsShiftOperators) {
11417 verifyFormat("if (i < x >> 1)");
11418 verifyFormat("while (i < x >> 1)");
11419 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11420 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11421 verifyFormat(
11422 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11423 verifyFormat("Foo.call<Bar<Function>>()");
11424 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11425 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11426 "++i, v = v >> 1)");
11427 verifyFormat("if (w<u<v<x>>, 1>::t)");
11428 }
11429
11430 TEST_F(FormatTest, BitshiftOperatorWidth) {
11431 verifyFormat("int a = 1 << 2; /* foo\n"
11432 " bar */",
11433 "int a=1<<2; /* foo\n"
11434 " bar */");
11435
11436 verifyFormat("int b = 256 >> 1; /* foo\n"
11437 " bar */",
11438 "int b =256>>1 ; /* foo\n"
11439 " bar */");
11440 }
11441
11442 TEST_F(FormatTest, UnderstandsBinaryOperators) {
11443 verifyFormat("COMPARE(a, ==, b);");
11444 verifyFormat("auto s = sizeof...(Ts) - 1;");
11445 }
11446
11447 TEST_F(FormatTest, UnderstandsPointersToMembers) {
11448 verifyFormat("int A::*x;");
11449 verifyFormat("int (S::*func)(void *);");
11450 verifyFormat("void f() { int (S::*func)(void *); }");
11451 verifyFormat("typedef bool *(Class::*Member)() const;");
11452 verifyFormat("void f() {\n"
11453 " (a->*f)();\n"
11454 " a->*x;\n"
11455 " (a.*f)();\n"
11456 " ((*a).*f)();\n"
11457 " a.*x;\n"
11458 "}");
11459 verifyFormat("void f() {\n"
11460 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11461 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11462 "}");
11463 verifyFormat(
11464 "(aaaaaaaaaa->*bbbbbbb)(\n"
11465 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11466
11467 FormatStyle Style = getLLVMStyle();
11468 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11469 verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11470 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
11471
11472 Style.PointerAlignment = FormatStyle::PAS_Left;
11473 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
11474 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11475
11476 Style.PointerAlignment = FormatStyle::PAS_Middle;
11477 verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11478 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11479 }
11480
11481 TEST_F(FormatTest, UnderstandsUnaryOperators) {
11482 verifyFormat("int a = -2;");
11483 verifyFormat("f(-1, -2, -3);");
11484 verifyFormat("a[-1] = 5;");
11485 verifyFormat("int a = 5 + -2;");
11486 verifyFormat("if (i == -1) {\n}");
11487 verifyFormat("if (i != -1) {\n}");
11488 verifyFormat("if (i > -1) {\n}");
11489 verifyFormat("if (i < -1) {\n}");
11490 verifyFormat("++(a->f());");
11491 verifyFormat("--(a->f());");
11492 verifyFormat("(a->f())++;");
11493 verifyFormat("a[42]++;");
11494 verifyFormat("if (!(a->f())) {\n}");
11495 verifyFormat("if (!+i) {\n}");
11496 verifyFormat("~&a;");
11497 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11498 verifyFormat("sizeof -x");
11499 verifyFormat("sizeof +x");
11500 verifyFormat("sizeof *x");
11501 verifyFormat("sizeof &x");
11502 verifyFormat("delete +x;");
11503 verifyFormat("co_await +x;");
11504 verifyFormat("case *x:");
11505 verifyFormat("case &x:");
11506
11507 verifyFormat("a-- > b;");
11508 verifyFormat("b ? -a : c;");
11509 verifyFormat("n * sizeof char16;");
11510 verifyGoogleFormat("n * alignof char16;");
11511 verifyFormat("sizeof(char);");
11512 verifyGoogleFormat("alignof(char);");
11513
11514 verifyFormat("return -1;");
11515 verifyFormat("throw -1;");
11516 verifyFormat("switch (a) {\n"
11517 "case -1:\n"
11518 " break;\n"
11519 "}");
11520 verifyFormat("#define X -1");
11521 verifyFormat("#define X -kConstant");
11522
11523 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11524 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11525
11526 verifyFormat("int a = /* confusing comment */ -1;");
11527 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11528 verifyFormat("int a = i /* confusing comment */++;");
11529
11530 verifyFormat("co_yield -1;");
11531 verifyFormat("co_return -1;");
11532
11533 // Check that * is not treated as a binary operator when we set
11534 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11535 FormatStyle PASLeftStyle = getLLVMStyle();
11536 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11537 verifyFormat("co_return *a;", PASLeftStyle);
11538 verifyFormat("co_await *a;", PASLeftStyle);
11539 verifyFormat("co_yield *a", PASLeftStyle);
11540 verifyFormat("return *a;", PASLeftStyle);
11541 }
11542
11543 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11544 verifyFormat("if (!aaaaaaaaaa( // break\n"
11545 " aaaaa)) {\n"
11546 "}");
11547 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11548 " aaaaa));");
11549 verifyFormat("*aaa = aaaaaaa( // break\n"
11550 " bbbbbb);");
11551 }
11552
11553 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11554 verifyFormat("bool operator<();");
11555 verifyFormat("bool operator>();");
11556 verifyFormat("bool operator=();");
11557 verifyFormat("bool operator==();");
11558 verifyFormat("bool operator!=();");
11559 verifyFormat("int operator+();");
11560 verifyFormat("int operator++();");
11561 verifyFormat("int operator++(int) volatile noexcept;");
11562 verifyFormat("bool operator,();");
11563 verifyFormat("bool operator();");
11564 verifyFormat("bool operator()();");
11565 verifyFormat("bool operator[]();");
11566 verifyFormat("operator bool();");
11567 verifyFormat("operator int();");
11568 verifyFormat("operator void *();");
11569 verifyFormat("operator SomeType<int>();");
11570 verifyFormat("operator SomeType<int, int>();");
11571 verifyFormat("operator SomeType<SomeType<int>>();");
11572 verifyFormat("operator< <>();");
11573 verifyFormat("operator<< <>();");
11574 verifyFormat("< <>");
11575
11576 verifyFormat("void *operator new(std::size_t size);");
11577 verifyFormat("void *operator new[](std::size_t size);");
11578 verifyFormat("void operator delete(void *ptr);");
11579 verifyFormat("void operator delete[](void *ptr);");
11580 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11581 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11582 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11583 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11584
11585 verifyFormat(
11586 "ostream &operator<<(ostream &OutputStream,\n"
11587 " SomeReallyLongType WithSomeReallyLongValue);");
11588 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11589 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11590 " return left.group < right.group;\n"
11591 "}");
11592 verifyFormat("SomeType &operator=(const SomeType &S);");
11593 verifyFormat("f.template operator()<int>();");
11594
11595 verifyGoogleFormat("operator void*();");
11596 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11597 verifyGoogleFormat("operator ::A();");
11598
11599 verifyFormat("using A::operator+;");
11600 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11601 "int i;");
11602
11603 // Calling an operator as a member function.
11604 verifyFormat("void f() { a.operator*(); }");
11605 verifyFormat("void f() { a.operator*(b & b); }");
11606 verifyFormat("void f() { a->operator&(a * b); }");
11607 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11608 verifyFormat("void f() { operator*(a & a); }");
11609 verifyFormat("void f() { operator&(a, b * b); }");
11610
11611 verifyFormat("void f() { return operator()(x) * b; }");
11612 verifyFormat("void f() { return operator[](x) * b; }");
11613 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11614 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11615 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11616 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11617 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11618
11619 verifyFormat("::operator delete(foo);");
11620 verifyFormat("::operator new(n * sizeof(foo));");
11621 verifyFormat("foo() { ::operator delete(foo); }");
11622 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11623 }
11624
11625 TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11626 verifyFormat("C<&operator- > minus;");
11627 verifyFormat("C<&operator> > gt;");
11628 verifyFormat("C<&operator>= > ge;");
11629 verifyFormat("C<&operator<= > le;");
11630 verifyFormat("C<&operator< <X>> lt;");
11631 }
11632
11633 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11634 verifyFormat("void A::b() && {}");
11635 verifyFormat("void A::b() && noexcept {}");
11636 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11637 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11638 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11639 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11640 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11641 verifyFormat("Deleted &operator=(const Deleted &) &;");
11642 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11643 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11644 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11645 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11646 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11647 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11648 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11649 verifyFormat("void Fn(T const &) const &;");
11650 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11651 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11652 verifyGoogleFormat("template <typename T>\n"
11653 "void F(T) && = delete;");
11654 verifyFormat("template <typename T> void operator=(T) &;");
11655 verifyFormat("template <typename T> void operator=(T) const &;");
11656 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11657 verifyFormat("template <typename T> void operator=(T) & = default;");
11658 verifyFormat("template <typename T> void operator=(T) &&;");
11659 verifyFormat("template <typename T> void operator=(T) && = delete;");
11660 verifyFormat("template <typename T> void operator=(T) & {}");
11661 verifyFormat("template <typename T> void operator=(T) && {}");
11662
11663 FormatStyle AlignLeft = getLLVMStyle();
11664 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11665 verifyFormat("void A::b() && {}", AlignLeft);
11666 verifyFormat("void A::b() && noexcept {}", AlignLeft);
11667 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11668 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11669 AlignLeft);
11670 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11671 AlignLeft);
11672 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11673 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11674 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11675 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11676 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11677 verifyFormat("auto Function(T) & -> void;", AlignLeft);
11678 verifyFormat("void Fn(T const&) const&;", AlignLeft);
11679 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11680 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11681 AlignLeft);
11682 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11683 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11684 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11685 AlignLeft);
11686 verifyFormat("template <typename T> void operator=(T) & = default;",
11687 AlignLeft);
11688 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11689 verifyFormat("template <typename T> void operator=(T) && = delete;",
11690 AlignLeft);
11691 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11692 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11693 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft);
11694
11695 FormatStyle AlignMiddle = getLLVMStyle();
11696 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11697 verifyFormat("void A::b() && {}", AlignMiddle);
11698 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11699 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11700 AlignMiddle);
11701 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11702 AlignMiddle);
11703 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11704 AlignMiddle);
11705 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11706 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11707 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11708 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11709 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11710 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11711 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11712 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11713 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11714 AlignMiddle);
11715 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11716 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11717 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11718 AlignMiddle);
11719 verifyFormat("template <typename T> void operator=(T) & = default;",
11720 AlignMiddle);
11721 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11722 verifyFormat("template <typename T> void operator=(T) && = delete;",
11723 AlignMiddle);
11724 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11725 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11726
11727 FormatStyle Spaces = getLLVMStyle();
11728 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11729 Spaces.SpacesInParensOptions = {};
11730 Spaces.SpacesInParensOptions.InCStyleCasts = true;
11731 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11732 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11733 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11734 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11735
11736 Spaces.SpacesInParensOptions.InCStyleCasts = false;
11737 Spaces.SpacesInParensOptions.Other = true;
11738 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11739 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11740 Spaces);
11741 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11742 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11743
11744 FormatStyle BreakTemplate = getLLVMStyle();
11745 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11746
11747 verifyFormat("struct f {\n"
11748 " template <class T>\n"
11749 " int &foo(const std::string &str) & noexcept {}\n"
11750 "};",
11751 BreakTemplate);
11752
11753 verifyFormat("struct f {\n"
11754 " template <class T>\n"
11755 " int &foo(const std::string &str) && noexcept {}\n"
11756 "};",
11757 BreakTemplate);
11758
11759 verifyFormat("struct f {\n"
11760 " template <class T>\n"
11761 " int &foo(const std::string &str) const & noexcept {}\n"
11762 "};",
11763 BreakTemplate);
11764
11765 verifyFormat("struct f {\n"
11766 " template <class T>\n"
11767 " int &foo(const std::string &str) const & noexcept {}\n"
11768 "};",
11769 BreakTemplate);
11770
11771 verifyFormat("struct f {\n"
11772 " template <class T>\n"
11773 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11774 "};",
11775 BreakTemplate);
11776
11777 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11778 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11779 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11780
11781 verifyFormat("struct f {\n"
11782 " template <class T>\n"
11783 " int& foo(const std::string& str) & noexcept {}\n"
11784 "};",
11785 AlignLeftBreakTemplate);
11786
11787 verifyFormat("struct f {\n"
11788 " template <class T>\n"
11789 " int& foo(const std::string& str) && noexcept {}\n"
11790 "};",
11791 AlignLeftBreakTemplate);
11792
11793 verifyFormat("struct f {\n"
11794 " template <class T>\n"
11795 " int& foo(const std::string& str) const& noexcept {}\n"
11796 "};",
11797 AlignLeftBreakTemplate);
11798
11799 verifyFormat("struct f {\n"
11800 " template <class T>\n"
11801 " int& foo(const std::string& str) const&& noexcept {}\n"
11802 "};",
11803 AlignLeftBreakTemplate);
11804
11805 verifyFormat("struct f {\n"
11806 " template <class T>\n"
11807 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11808 "};",
11809 AlignLeftBreakTemplate);
11810
11811 // The `&` in `Type&` should not be confused with a trailing `&` of
11812 // DEPRECATED(reason) member function.
11813 verifyFormat("struct f {\n"
11814 " template <class T>\n"
11815 " DEPRECATED(reason)\n"
11816 " Type &foo(arguments) {}\n"
11817 "};",
11818 BreakTemplate);
11819
11820 verifyFormat("struct f {\n"
11821 " template <class T>\n"
11822 " DEPRECATED(reason)\n"
11823 " Type& foo(arguments) {}\n"
11824 "};",
11825 AlignLeftBreakTemplate);
11826
11827 verifyFormat("void (*foopt)(int) = &func;");
11828
11829 FormatStyle DerivePointerAlignment = getLLVMStyle();
11830 DerivePointerAlignment.DerivePointerAlignment = true;
11831 // There's always a space between the function and its trailing qualifiers.
11832 // This isn't evidence for PAS_Right (or for PAS_Left).
11833 std::string Prefix = "void a() &;\n"
11834 "void b() &;\n";
11835 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11836 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11837 // Same if the function is an overloaded operator, and with &&.
11838 Prefix = "void operator()() &&;\n"
11839 "void operator()() &&;\n";
11840 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11841 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11842 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11843 Prefix = "void a() const &;\n"
11844 "void b() const &;\n";
11845 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11846
11847 constexpr StringRef Code("MACRO(int*, std::function<void() &&>);");
11848 verifyFormat(Code, DerivePointerAlignment);
11849
11850 auto Style = getGoogleStyle();
11851 Style.DerivePointerAlignment = true;
11852 verifyFormat(Code, Style);
11853 }
11854
11855 TEST_F(FormatTest, PointerAlignmentFallback) {
11856 FormatStyle Style = getLLVMStyle();
11857 Style.DerivePointerAlignment = true;
11858
11859 constexpr StringRef Code("int* p;\n"
11860 "int *q;\n"
11861 "int * r;");
11862
11863 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11864 verifyFormat("int *p;\n"
11865 "int *q;\n"
11866 "int *r;",
11867 Code, Style);
11868
11869 Style.PointerAlignment = FormatStyle::PAS_Left;
11870 verifyFormat("int* p;\n"
11871 "int* q;\n"
11872 "int* r;",
11873 Code, Style);
11874
11875 Style.PointerAlignment = FormatStyle::PAS_Middle;
11876 verifyFormat("int * p;\n"
11877 "int * q;\n"
11878 "int * r;",
11879 Code, Style);
11880 }
11881
11882 TEST_F(FormatTest, UnderstandsNewAndDelete) {
11883 verifyFormat("A(void *p) : a(new (p) int) {}");
11884 verifyFormat("void f() {\n"
11885 " A *a = new A;\n"
11886 " A *a = new (placement) A;\n"
11887 " delete a;\n"
11888 " delete (A *)a;\n"
11889 "}");
11890 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11891 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11892 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11893 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11894 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11895 verifyFormat("delete[] h->p;");
11896 verifyFormat("delete[] (void *)p;");
11897
11898 verifyFormat("void operator delete(void *foo) ATTRIB;");
11899 verifyFormat("void operator new(void *foo) ATTRIB;");
11900 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11901 verifyFormat("void operator delete(void *ptr) noexcept;");
11902
11903 verifyFormat("void new(link p);\n"
11904 "void delete(link p);",
11905 "void new (link p);\n"
11906 "void delete (link p);",
11907 getLLVMStyle(FormatStyle::LK_C));
11908
11909 verifyFormat("{\n"
11910 " p->new();\n"
11911 "}\n"
11912 "{\n"
11913 " p->delete();\n"
11914 "}",
11915 "{\n"
11916 " p->new ();\n"
11917 "}\n"
11918 "{\n"
11919 " p->delete ();\n"
11920 "}");
11921
11922 FormatStyle AfterPlacementOperator = getLLVMStyle();
11923 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11924 EXPECT_TRUE(
11925 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
11926 verifyFormat("new (buf) int;", AfterPlacementOperator);
11927 verifyFormat("struct A {\n"
11928 " int *a;\n"
11929 " A(int *p) : a(new (p) int) {\n"
11930 " new (p) int;\n"
11931 " int *b = new (p) int;\n"
11932 " int *c = new (p) int(3);\n"
11933 " delete (b);\n"
11934 " }\n"
11935 "};",
11936 AfterPlacementOperator);
11937 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11938 verifyFormat("delete (int *)p;", AfterPlacementOperator);
11939
11940 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11941 false;
11942 verifyFormat("new(buf) int;", AfterPlacementOperator);
11943 verifyFormat("struct A {\n"
11944 " int *a;\n"
11945 " A(int *p) : a(new(p) int) {\n"
11946 " new(p) int;\n"
11947 " int *b = new(p) int;\n"
11948 " int *c = new(p) int(3);\n"
11949 " delete(b);\n"
11950 " }\n"
11951 "};",
11952 AfterPlacementOperator);
11953 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11954 verifyFormat("delete (int *)p;", AfterPlacementOperator);
11955 }
11956
11957 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11958 verifyFormat("int *f(int *a) {}");
11959 verifyFormat("int main(int argc, char **argv) {}");
11960 verifyFormat("Test::Test(int b) : a(b * b) {}");
11961 verifyIndependentOfContext("f(a, *a);");
11962 verifyFormat("void g() { f(*a); }");
11963 verifyIndependentOfContext("int a = b * 10;");
11964 verifyIndependentOfContext("int a = 10 * b;");
11965 verifyIndependentOfContext("int a = b * c;");
11966 verifyIndependentOfContext("int a += b * c;");
11967 verifyIndependentOfContext("int a -= b * c;");
11968 verifyIndependentOfContext("int a *= b * c;");
11969 verifyIndependentOfContext("int a /= b * c;");
11970 verifyIndependentOfContext("int a = *b;");
11971 verifyIndependentOfContext("int a = *b * c;");
11972 verifyIndependentOfContext("int a = b * *c;");
11973 verifyIndependentOfContext("int a = b * (10);");
11974 verifyIndependentOfContext("S << b * (10);");
11975 verifyIndependentOfContext("return 10 * b;");
11976 verifyIndependentOfContext("return *b * *c;");
11977 verifyIndependentOfContext("return a & ~b;");
11978 verifyIndependentOfContext("f(b ? *c : *d);");
11979 verifyIndependentOfContext("int a = b ? *c : *d;");
11980 verifyIndependentOfContext("*b = a;");
11981 verifyIndependentOfContext("a * ~b;");
11982 verifyIndependentOfContext("a * !b;");
11983 verifyIndependentOfContext("a * +b;");
11984 verifyIndependentOfContext("a * -b;");
11985 verifyIndependentOfContext("a * ++b;");
11986 verifyIndependentOfContext("a * --b;");
11987 verifyIndependentOfContext("a[4] * b;");
11988 verifyIndependentOfContext("a[a * a] = 1;");
11989 verifyIndependentOfContext("f() * b;");
11990 verifyIndependentOfContext("a * [self dostuff];");
11991 verifyIndependentOfContext("int x = a * (a + b);");
11992 verifyIndependentOfContext("(a *)(a + b);");
11993 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11994 verifyIndependentOfContext("int *pa = (int *)&a;");
11995 verifyIndependentOfContext("return sizeof(int **);");
11996 verifyIndependentOfContext("return sizeof(int ******);");
11997 verifyIndependentOfContext("return (int **&)a;");
11998 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11999 verifyFormat("void f(Type (*parameter)[10]) {}");
12000 verifyFormat("void f(Type (&parameter)[10]) {}");
12001 verifyGoogleFormat("return sizeof(int**);");
12002 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
12003 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
12004 verifyFormat("auto a = [](int **&, int ***) {};");
12005 verifyFormat("auto PointerBinding = [](const char *S) {};");
12006 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
12007 verifyFormat("[](const decltype(*a) &value) {}");
12008 verifyFormat("[](const typeof(*a) &value) {}");
12009 verifyFormat("[](const _Atomic(a *) &value) {}");
12010 verifyFormat("[](const __underlying_type(a) &value) {}");
12011 verifyFormat("decltype(a * b) F();");
12012 verifyFormat("typeof(a * b) F();");
12013 verifyFormat("#define MACRO() [](A *a) { return 1; }");
12014 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
12015 verifyIndependentOfContext("typedef void (*f)(int *a);");
12016 verifyIndependentOfContext("typedef void (*f)(Type *a);");
12017 verifyIndependentOfContext("int i{a * b};");
12018 verifyIndependentOfContext("aaa && aaa->f();");
12019 verifyIndependentOfContext("int x = ~*p;");
12020 verifyFormat("Constructor() : a(a), area(width * height) {}");
12021 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
12022 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
12023 verifyFormat("void f() { f(a, c * d); }");
12024 verifyFormat("void f() { f(new a(), c * d); }");
12025 verifyFormat("void f(const MyOverride &override);");
12026 verifyFormat("void f(const MyFinal &final);");
12027 verifyIndependentOfContext("bool a = f() && override.f();");
12028 verifyIndependentOfContext("bool a = f() && final.f();");
12029
12030 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
12031
12032 verifyIndependentOfContext("A<int *> a;");
12033 verifyIndependentOfContext("A<int **> a;");
12034 verifyIndependentOfContext("A<int *, int *> a;");
12035 verifyIndependentOfContext("A<int *[]> a;");
12036 verifyIndependentOfContext(
12037 "const char *const p = reinterpret_cast<const char *const>(q);");
12038 verifyIndependentOfContext("A<int **, int **> a;");
12039 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
12040 verifyFormat("for (char **a = b; *a; ++a) {\n}");
12041 verifyFormat("for (; a && b;) {\n}");
12042 verifyFormat("bool foo = true && [] { return false; }();");
12043
12044 verifyFormat(
12045 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12046 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12047
12048 verifyGoogleFormat("int const* a = &b;");
12049 verifyGoogleFormat("**outparam = 1;");
12050 verifyGoogleFormat("*outparam = a * b;");
12051 verifyGoogleFormat("int main(int argc, char** argv) {}");
12052 verifyGoogleFormat("A<int*> a;");
12053 verifyGoogleFormat("A<int**> a;");
12054 verifyGoogleFormat("A<int*, int*> a;");
12055 verifyGoogleFormat("A<int**, int**> a;");
12056 verifyGoogleFormat("f(b ? *c : *d);");
12057 verifyGoogleFormat("int a = b ? *c : *d;");
12058 verifyGoogleFormat("Type* t = **x;");
12059 verifyGoogleFormat("Type* t = *++*x;");
12060 verifyGoogleFormat("*++*x;");
12061 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
12062 verifyGoogleFormat("Type* t = x++ * y;");
12063 verifyGoogleFormat(
12064 "const char* const p = reinterpret_cast<const char* const>(q);");
12065 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
12066 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
12067 verifyGoogleFormat("template <typename T>\n"
12068 "void f(int i = 0, SomeType** temps = NULL);");
12069
12070 FormatStyle Left = getLLVMStyle();
12071 Left.PointerAlignment = FormatStyle::PAS_Left;
12072 verifyFormat("x = *a(x) = *a(y);", Left);
12073 verifyFormat("for (;; *a = b) {\n}", Left);
12074 verifyFormat("return *this += 1;", Left);
12075 verifyFormat("throw *x;", Left);
12076 verifyFormat("delete *x;", Left);
12077 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
12078 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
12079 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
12080 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
12081 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
12082 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
12083 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
12084 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
12085 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
12086
12087 verifyIndependentOfContext("a = *(x + y);");
12088 verifyIndependentOfContext("a = &(x + y);");
12089 verifyIndependentOfContext("*(x + y).call();");
12090 verifyIndependentOfContext("&(x + y)->call();");
12091 verifyFormat("void f() { &(*I).first; }");
12092
12093 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
12094 verifyFormat("f(* /* confusing comment */ foo);");
12095 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
12096 verifyFormat("void foo(int * // this is the first paramters\n"
12097 " ,\n"
12098 " int second);");
12099 verifyFormat("double term = a * // first\n"
12100 " b;");
12101 verifyFormat(
12102 "int *MyValues = {\n"
12103 " *A, // Operator detection might be confused by the '{'\n"
12104 " *BB // Operator detection might be confused by previous comment\n"
12105 "};");
12106
12107 verifyIndependentOfContext("if (int *a = &b)");
12108 verifyIndependentOfContext("if (int &a = *b)");
12109 verifyIndependentOfContext("if (a & b[i])");
12110 verifyIndependentOfContext("if constexpr (a & b[i])");
12111 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
12112 verifyIndependentOfContext("if (a * (b * c))");
12113 verifyIndependentOfContext("if constexpr (a * (b * c))");
12114 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
12115 verifyIndependentOfContext("if (a::b::c::d & b[i])");
12116 verifyIndependentOfContext("if (*b[i])");
12117 verifyIndependentOfContext("if (int *a = (&b))");
12118 verifyIndependentOfContext("while (int *a = &b)");
12119 verifyIndependentOfContext("while (a * (b * c))");
12120 verifyIndependentOfContext("size = sizeof *a;");
12121 verifyIndependentOfContext("if (a && (b = c))");
12122 verifyFormat("void f() {\n"
12123 " for (const int &v : Values) {\n"
12124 " }\n"
12125 "}");
12126 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
12127 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
12128 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
12129
12130 verifyFormat("#define A (!a * b)");
12131 verifyFormat("#define MACRO \\\n"
12132 " int *i = a * b; \\\n"
12133 " void f(a *b);",
12134 getLLVMStyleWithColumns(19));
12135
12136 verifyIndependentOfContext("A = new SomeType *[Length];");
12137 verifyIndependentOfContext("A = new SomeType *[Length]();");
12138 verifyIndependentOfContext("T **t = new T *;");
12139 verifyIndependentOfContext("T **t = new T *();");
12140 verifyGoogleFormat("A = new SomeType*[Length]();");
12141 verifyGoogleFormat("A = new SomeType*[Length];");
12142 verifyGoogleFormat("T** t = new T*;");
12143 verifyGoogleFormat("T** t = new T*();");
12144
12145 verifyFormat("STATIC_ASSERT((a & b) == 0);");
12146 verifyFormat("STATIC_ASSERT(0 == (a & b));");
12147 verifyFormat("template <bool a, bool b> "
12148 "typename t::if<x && y>::type f() {}");
12149 verifyFormat("template <int *y> f() {}");
12150 verifyFormat("vector<int *> v;");
12151 verifyFormat("vector<int *const> v;");
12152 verifyFormat("vector<int *const **const *> v;");
12153 verifyFormat("vector<int *volatile> v;");
12154 verifyFormat("vector<a *_Nonnull> v;");
12155 verifyFormat("vector<a *_Nullable> v;");
12156 verifyFormat("vector<a *_Null_unspecified> v;");
12157 verifyGoogleFormat("vector<a* absl_nonnull> v;");
12158 verifyGoogleFormat("vector<a* absl_nullable> v;");
12159 verifyGoogleFormat("vector<a* absl_nullability_unknown> v;");
12160 verifyFormat("vector<a *__ptr32> v;");
12161 verifyFormat("vector<a *__ptr64> v;");
12162 verifyFormat("vector<a *__capability> v;");
12163 FormatStyle TypeMacros = getLLVMStyle();
12164 TypeMacros.TypenameMacros = {"LIST"};
12165 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
12166 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
12167 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
12168 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
12169 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
12170
12171 FormatStyle CustomQualifier = getLLVMStyle();
12172 // Add identifiers that should not be parsed as a qualifier by default.
12173 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12174 CustomQualifier.AttributeMacros.push_back("_My_qualifier");
12175 CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
12176 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
12177 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
12178 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
12179 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
12180 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
12181 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
12182 verifyFormat("vector<a * _NotAQualifier> v;");
12183 verifyFormat("vector<a * __not_a_qualifier> v;");
12184 verifyFormat("vector<a * b> v;");
12185 verifyFormat("foo<b && false>();");
12186 verifyFormat("foo<b & 1>();");
12187 verifyFormat("foo<b & (1)>();");
12188 verifyFormat("foo<b & (~0)>();");
12189 verifyFormat("foo<b & (true)>();");
12190 verifyFormat("foo<b & ((1))>();");
12191 verifyFormat("foo<b & (/*comment*/ 1)>();");
12192 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12193 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12194 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12195 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12196 verifyFormat(
12197 "template <class T, class = typename std::enable_if<\n"
12198 " std::is_integral<T>::value &&\n"
12199 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12200 "void F();",
12201 getLLVMStyleWithColumns(70));
12202 verifyFormat("template <class T,\n"
12203 " class = typename std::enable_if<\n"
12204 " std::is_integral<T>::value &&\n"
12205 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12206 " class U>\n"
12207 "void F();",
12208 getLLVMStyleWithColumns(70));
12209 verifyFormat(
12210 "template <class T,\n"
12211 " class = typename ::std::enable_if<\n"
12212 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12213 "void F();",
12214 getGoogleStyleWithColumns(68));
12215
12216 FormatStyle Style = getLLVMStyle();
12217 Style.PointerAlignment = FormatStyle::PAS_Left;
12218 verifyFormat("struct {\n"
12219 "}* ptr;",
12220 Style);
12221 verifyFormat("union {\n"
12222 "}* ptr;",
12223 Style);
12224 verifyFormat("class {\n"
12225 "}* ptr;",
12226 Style);
12227 // Don't confuse a multiplication after a brace-initialized expression with
12228 // a class pointer.
12229 verifyFormat("int i = int{42} * 34;", Style);
12230 verifyFormat("struct {\n"
12231 "}&& ptr = {};",
12232 Style);
12233 verifyFormat("union {\n"
12234 "}&& ptr = {};",
12235 Style);
12236 verifyFormat("class {\n"
12237 "}&& ptr = {};",
12238 Style);
12239 verifyFormat("bool b = 3 == int{3} && true;");
12240
12241 Style.PointerAlignment = FormatStyle::PAS_Middle;
12242 verifyFormat("struct {\n"
12243 "} * ptr;",
12244 Style);
12245 verifyFormat("union {\n"
12246 "} * ptr;",
12247 Style);
12248 verifyFormat("class {\n"
12249 "} * ptr;",
12250 Style);
12251 verifyFormat("struct {\n"
12252 "} && ptr = {};",
12253 Style);
12254 verifyFormat("union {\n"
12255 "} && ptr = {};",
12256 Style);
12257 verifyFormat("class {\n"
12258 "} && ptr = {};",
12259 Style);
12260
12261 Style.PointerAlignment = FormatStyle::PAS_Right;
12262 verifyFormat("struct {\n"
12263 "} *ptr;",
12264 Style);
12265 verifyFormat("union {\n"
12266 "} *ptr;",
12267 Style);
12268 verifyFormat("class {\n"
12269 "} *ptr;",
12270 Style);
12271 verifyFormat("struct {\n"
12272 "} &&ptr = {};",
12273 Style);
12274 verifyFormat("union {\n"
12275 "} &&ptr = {};",
12276 Style);
12277 verifyFormat("class {\n"
12278 "} &&ptr = {};",
12279 Style);
12280
12281 Style.PointerAlignment = FormatStyle::PAS_Left;
12282 verifyFormat("delete[] *ptr;", Style);
12283 verifyFormat("delete[] **ptr;", Style);
12284 verifyFormat("delete[] *(ptr);", Style);
12285
12286 verifyIndependentOfContext("MACRO(int *i);");
12287 verifyIndependentOfContext("MACRO(auto *a);");
12288 verifyIndependentOfContext("MACRO(const A *a);");
12289 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12290 verifyIndependentOfContext("MACRO(decltype(A) *a);");
12291 verifyIndependentOfContext("MACRO(typeof(A) *a);");
12292 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12293 verifyIndependentOfContext("MACRO(A *const a);");
12294 verifyIndependentOfContext("MACRO(A *restrict a);");
12295 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12296 verifyIndependentOfContext("MACRO(A *__restrict a);");
12297 verifyIndependentOfContext("MACRO(A *volatile a);");
12298 verifyIndependentOfContext("MACRO(A *__volatile a);");
12299 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12300 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12301 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12302 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12303
12304 Style = getGoogleStyle();
12305 verifyIndependentOfContext("MACRO(A* absl_nonnull a);", Style);
12306 verifyIndependentOfContext("MACRO(A* absl_nullable a);", Style);
12307 verifyIndependentOfContext("MACRO(A* absl_nullability_unknown a);", Style);
12308
12309 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12310 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12311 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12312 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12313 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12314 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12315 verifyIndependentOfContext("MACRO(A *__capability);");
12316 verifyIndependentOfContext("MACRO(A &__capability);");
12317 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12318 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12319 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12320 // a type declaration:
12321 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
12322 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
12323 // Also check that TypenameMacros prevents parsing it as multiplication:
12324 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12325 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
12326
12327 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12328 verifyFormat("void f() { f(float{1}, a * a); }");
12329 verifyFormat("void f() { f(float(1), a * a); }");
12330
12331 verifyFormat("f((void (*)(int))g);");
12332 verifyFormat("f((void (&)(int))g);");
12333 verifyFormat("f((void (^)(int))g);");
12334
12335 // FIXME: Is there a way to make this work?
12336 // verifyIndependentOfContext("MACRO(A *a);");
12337 verifyFormat("MACRO(A &B);");
12338 verifyFormat("MACRO(A *B);");
12339 verifyFormat("void f() { MACRO(A * B); }");
12340 verifyFormat("void f() { MACRO(A & B); }");
12341
12342 // This lambda was mis-formatted after D88956 (treating it as a binop):
12343 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12344 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12345 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12346 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12347
12348 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12349 verifyFormat("return options != nullptr && operator==(*options);");
12350
12351 verifyFormat("#define OP(x) \\\n"
12352 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12353 " return s << a.DebugString(); \\\n"
12354 " }",
12355 "#define OP(x) \\\n"
12356 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12357 " return s << a.DebugString(); \\\n"
12358 " }",
12359 getLLVMStyleWithColumns(50));
12360
12361 verifyFormat("#define FOO \\\n"
12362 " void foo() { \\\n"
12363 " operator+(a * b); \\\n"
12364 " }",
12365 getLLVMStyleWithColumns(25));
12366
12367 // FIXME: We cannot handle this case yet; we might be able to figure out that
12368 // foo<x> d > v; doesn't make sense.
12369 verifyFormat("foo<a<b && c> d> v;");
12370
12371 FormatStyle PointerMiddle = getLLVMStyle();
12372 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12373 verifyFormat("delete *x;", PointerMiddle);
12374 verifyFormat("int * x;", PointerMiddle);
12375 verifyFormat("int *[] x;", PointerMiddle);
12376 verifyFormat("template <int * y> f() {}", PointerMiddle);
12377 verifyFormat("int * f(int * a) {}", PointerMiddle);
12378 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12379 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12380 verifyFormat("A<int *> a;", PointerMiddle);
12381 verifyFormat("A<int **> a;", PointerMiddle);
12382 verifyFormat("A<int *, int *> a;", PointerMiddle);
12383 verifyFormat("A<int *[]> a;", PointerMiddle);
12384 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12385 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12386 verifyFormat("T ** t = new T *;", PointerMiddle);
12387
12388 // Member function reference qualifiers aren't binary operators.
12389 verifyFormat("string // break\n"
12390 "operator()() & {}");
12391 verifyFormat("string // break\n"
12392 "operator()() && {}");
12393 verifyGoogleFormat("template <typename T>\n"
12394 "auto x() & -> int {}");
12395
12396 // Should be binary operators when used as an argument expression (overloaded
12397 // operator invoked as a member function).
12398 verifyFormat("void f() { a.operator()(a * a); }");
12399 verifyFormat("void f() { a->operator()(a & a); }");
12400 verifyFormat("void f() { a.operator()(*a & *a); }");
12401 verifyFormat("void f() { a->operator()(*a * *a); }");
12402
12403 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12404 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12405
12406 verifyFormat("val1 & val2;");
12407 verifyFormat("val1 & val2 & val3;");
12408 verifyFormat("class c {\n"
12409 " void func(type &a) { a & member; }\n"
12410 " anotherType &member;\n"
12411 "}");
12412 }
12413
12414 TEST_F(FormatTest, UnderstandsAttributes) {
12415 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12416 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12417 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12418 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12419 FormatStyle AfterType = getLLVMStyle();
12420 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12421 verifyFormat("__attribute__((nodebug)) void\n"
12422 "foo() {}",
12423 AfterType);
12424 verifyFormat("__unused void\n"
12425 "foo() {}",
12426 AfterType);
12427
12428 FormatStyle CustomAttrs = getLLVMStyle();
12429 CustomAttrs.AttributeMacros.push_back("my_attr_name");
12430 verifyFormat("void MyGoodOldFunction(\n"
12431 " void *const long_enough = nullptr,\n"
12432 " void *my_attr_name even_longeeeeeeeeeeeeeeeeer = nullptr);",
12433 CustomAttrs);
12434
12435 CustomAttrs.AttributeMacros.push_back("__unused");
12436 CustomAttrs.AttributeMacros.push_back("__attr1");
12437 CustomAttrs.AttributeMacros.push_back("__attr2");
12438 CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
12439 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12440 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12441 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12442 // Check that it is parsed as a multiplication without AttributeMacros and
12443 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12444 verifyFormat("vector<SomeType * __attr1> v;");
12445 verifyFormat("vector<SomeType __attr1 *> v;");
12446 verifyFormat("vector<SomeType __attr1 *const> v;");
12447 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12448 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12449 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12450 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12451 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12452 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12453 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12454 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12455 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12456 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12457 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12458
12459 // Check that these are not parsed as function declarations:
12460 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12461 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12462 verifyFormat("SomeType s(InitValue);", CustomAttrs);
12463 verifyFormat("SomeType s{InitValue};", CustomAttrs);
12464 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12465 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12466 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12467 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12468 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12469 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12470 verifyGoogleFormat("SomeType* absl_nonnull s(InitValue);");
12471 verifyGoogleFormat("SomeType* absl_nonnull s{InitValue};");
12472 verifyGoogleFormat("SomeType* absl_nullable s(InitValue);");
12473 verifyGoogleFormat("SomeType* absl_nullable s{InitValue};");
12474 verifyGoogleFormat("SomeType* absl_nullability_unknown s(InitValue);");
12475 verifyGoogleFormat("SomeType* absl_nullability_unknown s{InitValue};");
12476
12477 auto Style = getLLVMStyleWithColumns(60);
12478 Style.AttributeMacros.push_back("my_fancy_attr");
12479 Style.PointerAlignment = FormatStyle::PAS_Left;
12480 verifyFormat("void foo(const MyLongTypeNameeeeeeeeeeeee* my_fancy_attr\n"
12481 " testttttttttt);",
12482 Style);
12483 }
12484
12485 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12486 // Check that qualifiers on pointers don't break parsing of casts.
12487 verifyFormat("x = (foo *const)*v;");
12488 verifyFormat("x = (foo *volatile)*v;");
12489 verifyFormat("x = (foo *restrict)*v;");
12490 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12491 verifyFormat("x = (foo *_Nonnull)*v;");
12492 verifyFormat("x = (foo *_Nullable)*v;");
12493 verifyFormat("x = (foo *_Null_unspecified)*v;");
12494 verifyGoogleFormat("x = (foo* absl_nonnull)*v;");
12495 verifyGoogleFormat("x = (foo* absl_nullable)*v;");
12496 verifyGoogleFormat("x = (foo* absl_nullability_unknown)*v;");
12497 verifyFormat("x = (foo *[[clang::attr]])*v;");
12498 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12499 verifyFormat("x = (foo *__ptr32)*v;");
12500 verifyFormat("x = (foo *__ptr64)*v;");
12501 verifyFormat("x = (foo *__capability)*v;");
12502
12503 // Check that we handle multiple trailing qualifiers and skip them all to
12504 // determine that the expression is a cast to a pointer type.
12505 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
12506 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
12507 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12508 StringRef AllQualifiers =
12509 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12510 "_Nullable [[clang::attr]] __ptr32 __ptr64 __capability";
12511 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12512 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12513
12514 // Also check that address-of is not parsed as a binary bitwise-and:
12515 verifyFormat("x = (foo *const)&v;");
12516 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12517 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12518
12519 // Check custom qualifiers:
12520 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
12521 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12522 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12523 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12524 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12525 CustomQualifier);
12526 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12527 CustomQualifier);
12528
12529 // Check that unknown identifiers result in binary operator parsing:
12530 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12531 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12532 }
12533
12534 TEST_F(FormatTest, UnderstandsSquareAttributes) {
12535 verifyFormat("SomeType s [[unused]] (InitValue);");
12536 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12537 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12538 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12539 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12540 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12541 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12542 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12543 verifyFormat("[[nodiscard]] bool f() { return false; }");
12544 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12545 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12546 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12547 verifyFormat("[[nodiscard]] ::qualified_type f();");
12548
12549 // Make sure we do not mistake attributes for array subscripts.
12550 verifyFormat("int a() {}\n"
12551 "[[unused]] int b() {}");
12552 verifyFormat("NSArray *arr;\n"
12553 "arr[[Foo() bar]];");
12554
12555 // On the other hand, we still need to correctly find array subscripts.
12556 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12557
12558 // Make sure that we do not mistake Objective-C method inside array literals
12559 // as attributes, even if those method names are also keywords.
12560 verifyFormat("@[ [foo bar] ];");
12561 verifyFormat("@[ [NSArray class] ];");
12562 verifyFormat("@[ [foo enum] ];");
12563
12564 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12565
12566 // Make sure we do not parse attributes as lambda introducers.
12567 FormatStyle MultiLineFunctions = getLLVMStyle();
12568 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12569 verifyFormat("[[unused]] int b() {\n"
12570 " return 42;\n"
12571 "}",
12572 MultiLineFunctions);
12573 }
12574
12575 TEST_F(FormatTest, AttributeClass) {
12576 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
12577 verifyFormat("class S {\n"
12578 " S(S&&) = default;\n"
12579 "};",
12580 Style);
12581 verifyFormat("class [[nodiscard]] S {\n"
12582 " S(S&&) = default;\n"
12583 "};",
12584 Style);
12585 verifyFormat("class __attribute((maybeunused)) S {\n"
12586 " S(S&&) = default;\n"
12587 "};",
12588 Style);
12589 verifyFormat("struct S {\n"
12590 " S(S&&) = default;\n"
12591 "};",
12592 Style);
12593 verifyFormat("struct [[nodiscard]] S {\n"
12594 " S(S&&) = default;\n"
12595 "};",
12596 Style);
12597 }
12598
12599 TEST_F(FormatTest, AttributesAfterMacro) {
12600 FormatStyle Style = getLLVMStyle();
12601 verifyFormat("MACRO;\n"
12602 "__attribute__((maybe_unused)) int foo() {\n"
12603 " //...\n"
12604 "}");
12605
12606 verifyFormat("MACRO;\n"
12607 "[[nodiscard]] int foo() {\n"
12608 " //...\n"
12609 "}");
12610
12611 verifyNoChange("MACRO\n\n"
12612 "__attribute__((maybe_unused)) int foo() {\n"
12613 " //...\n"
12614 "}");
12615
12616 verifyNoChange("MACRO\n\n"
12617 "[[nodiscard]] int foo() {\n"
12618 " //...\n"
12619 "}");
12620 }
12621
12622 TEST_F(FormatTest, AttributePenaltyBreaking) {
12623 FormatStyle Style = getLLVMStyle();
12624 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12625 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12626 Style);
12627 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12628 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12629 Style);
12630 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12631 "shared_ptr<ALongTypeName> &C d) {\n}",
12632 Style);
12633 }
12634
12635 TEST_F(FormatTest, UnderstandsEllipsis) {
12636 FormatStyle Style = getLLVMStyle();
12637 verifyFormat("int printf(const char *fmt, ...);");
12638 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12639 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12640
12641 verifyFormat("template <int *...PP> a;", Style);
12642
12643 Style.PointerAlignment = FormatStyle::PAS_Left;
12644 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12645
12646 verifyFormat("template <int*... PP> a;", Style);
12647
12648 Style.PointerAlignment = FormatStyle::PAS_Middle;
12649 verifyFormat("template <int *... PP> a;", Style);
12650 }
12651
12652 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12653 auto Style = getGoogleStyle();
12654 EXPECT_FALSE(Style.DerivePointerAlignment);
12655 Style.DerivePointerAlignment = true;
12656
12657 verifyFormat("int *a;\n"
12658 "int *a;\n"
12659 "int *a;",
12660 "int *a;\n"
12661 "int* a;\n"
12662 "int *a;",
12663 Style);
12664 verifyFormat("int* a;\n"
12665 "int* a;\n"
12666 "int* a;",
12667 "int* a;\n"
12668 "int* a;\n"
12669 "int *a;",
12670 Style);
12671 verifyFormat("int *a;\n"
12672 "int *a;\n"
12673 "int *a;",
12674 "int *a;\n"
12675 "int * a;\n"
12676 "int * a;",
12677 Style);
12678 verifyFormat("auto x = [] {\n"
12679 " int *a;\n"
12680 " int *a;\n"
12681 " int *a;\n"
12682 "};",
12683 "auto x=[]{int *a;\n"
12684 "int * a;\n"
12685 "int * a;};",
12686 Style);
12687 }
12688
12689 TEST_F(FormatTest, UnderstandsRvalueReferences) {
12690 verifyFormat("int f(int &&a) {}");
12691 verifyFormat("int f(int a, char &&b) {}");
12692 verifyFormat("void f() { int &&a = b; }");
12693 verifyGoogleFormat("int f(int a, char&& b) {}");
12694 verifyGoogleFormat("void f() { int&& a = b; }");
12695
12696 verifyIndependentOfContext("A<int &&> a;");
12697 verifyIndependentOfContext("A<int &&, int &&> a;");
12698 verifyGoogleFormat("A<int&&> a;");
12699 verifyGoogleFormat("A<int&&, int&&> a;");
12700
12701 // Not rvalue references:
12702 verifyFormat("template <bool B, bool C> class A {\n"
12703 " static_assert(B && C, \"Something is wrong\");\n"
12704 "};");
12705 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12706 verifyFormat("template <typename T> struct S {\n"
12707 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12708 "};");
12709 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12710 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12711 verifyFormat("#define A(a, b) (a && b)");
12712 }
12713
12714 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
12715 verifyFormat("void f() {\n"
12716 " x[aaaaaaaaa -\n"
12717 " b] = 23;\n"
12718 "}",
12719 getLLVMStyleWithColumns(15));
12720 }
12721
12722 TEST_F(FormatTest, FormatsCasts) {
12723 verifyFormat("Type *A = static_cast<Type *>(P);");
12724 verifyFormat("static_cast<Type *>(P);");
12725 verifyFormat("static_cast<Type &>(Fun)(Args);");
12726 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12727 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12728 // Check that static_cast<...>(...) does not require the next token to be on
12729 // the same line.
12730 verifyFormat("some_loooong_output << something_something__ << "
12731 "static_cast<const void *>(R)\n"
12732 " << something;");
12733 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12734 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12735 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12736 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12737 verifyFormat("Type *A = (Type *)P;");
12738 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12739 verifyFormat("int a = (int)(2.0f);");
12740 verifyFormat("int a = (int)2.0f;");
12741 verifyFormat("x[(int32)y];");
12742 verifyFormat("x = (int32)y;");
12743 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12744 verifyFormat("int a = (int)*b;");
12745 verifyFormat("int a = (int)2.0f;");
12746 verifyFormat("int a = (int)~0;");
12747 verifyFormat("int a = (int)++a;");
12748 verifyFormat("int a = (int)sizeof(int);");
12749 verifyFormat("int a = (int)+2;");
12750 verifyFormat("my_int a = (my_int)2.0f;");
12751 verifyFormat("my_int a = (my_int)sizeof(int);");
12752 verifyFormat("return (my_int)aaa;");
12753 verifyFormat("throw (my_int)aaa;");
12754 verifyFormat("#define x ((int)-1)");
12755 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12756 verifyFormat("#define p(q) ((int *)&q)");
12757 verifyFormat("fn(a)(b) + 1;");
12758
12759 verifyFormat("void f() { my_int a = (my_int)*b; }");
12760 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12761 verifyFormat("my_int a = (my_int)~0;");
12762 verifyFormat("my_int a = (my_int)++a;");
12763 verifyFormat("my_int a = (my_int)-2;");
12764 verifyFormat("my_int a = (my_int)1;");
12765 verifyFormat("my_int a = (my_int *)1;");
12766 verifyFormat("my_int a = (const my_int)-1;");
12767 verifyFormat("my_int a = (const my_int *)-1;");
12768 verifyFormat("my_int a = (my_int)(my_int)-1;");
12769 verifyFormat("my_int a = (ns::my_int)-2;");
12770 verifyFormat("case (my_int)ONE:");
12771 verifyFormat("auto x = (X)this;");
12772 // Casts in Obj-C style calls used to not be recognized as such.
12773 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12774
12775 // FIXME: single value wrapped with paren will be treated as cast.
12776 verifyFormat("void f(int i = (kValue)*kMask) {}");
12777
12778 verifyFormat("{\n"
12779 " (void)F;\n"
12780 "}");
12781
12782 // Don't break after a cast's
12783 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12784 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12785 " bbbbbbbbbbbbbbbbbbbbbb);");
12786
12787 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12788 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12789 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12790 verifyFormat("bool *y = (bool *)(void *)(x);");
12791 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12792 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12793 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12794 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12795
12796 // These are not casts.
12797 verifyFormat("void f(int *) {}");
12798 verifyFormat("f(foo)->b;");
12799 verifyFormat("f(foo).b;");
12800 verifyFormat("f(foo)(b);");
12801 verifyFormat("f(foo)[b];");
12802 verifyFormat("[](foo) { return 4; }(bar);");
12803 verifyFormat("(*funptr)(foo)[4];");
12804 verifyFormat("funptrs[4](foo)[4];");
12805 verifyFormat("void f(int *);");
12806 verifyFormat("void f(int *) = 0;");
12807 verifyFormat("void f(SmallVector<int>) {}");
12808 verifyFormat("void f(SmallVector<int>);");
12809 verifyFormat("void f(SmallVector<int>) = 0;");
12810 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12811 verifyFormat("int a = sizeof(int) * b;");
12812 verifyGoogleFormat("int a = alignof(int) * b;");
12813 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12814 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12815 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12816
12817 // These are not casts, but at some point were confused with casts.
12818 verifyFormat("virtual void foo(int *) override;");
12819 verifyFormat("virtual void foo(char &) const;");
12820 verifyFormat("virtual void foo(int *a, char *) const;");
12821 verifyFormat("int a = sizeof(int *) + b;");
12822 verifyGoogleFormat("int a = alignof(int*) + b;");
12823 verifyFormat("bool b = f(g<int>) && c;");
12824 verifyFormat("typedef void (*f)(int i) func;");
12825 verifyFormat("void operator++(int) noexcept;");
12826 verifyFormat("void operator++(int &) noexcept;");
12827 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12828 "&) noexcept;");
12829 verifyFormat(
12830 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12831 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12832 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12833 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12834 verifyFormat("void operator delete(foo &) noexcept;");
12835 verifyFormat("void operator delete(foo) noexcept;");
12836 verifyFormat("void operator delete(int) noexcept;");
12837 verifyFormat("void operator delete(int &) noexcept;");
12838 verifyFormat("void operator delete(int &) volatile noexcept;");
12839 verifyFormat("void operator delete(int &) const");
12840 verifyFormat("void operator delete(int &) = default");
12841 verifyFormat("void operator delete(int &) = delete");
12842 verifyFormat("void operator delete(int &) [[noreturn]]");
12843 verifyFormat("void operator delete(int &) throw();");
12844 verifyFormat("void operator delete(int &) throw(int);");
12845 verifyFormat("auto operator delete(int &) -> int;");
12846 verifyFormat("auto operator delete(int &) override");
12847 verifyFormat("auto operator delete(int &) final");
12848
12849 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12850 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12851 // FIXME: The indentation here is not ideal.
12852 verifyFormat(
12853 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12854 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12855 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12856 }
12857
12858 TEST_F(FormatTest, FormatsFunctionTypes) {
12859 verifyFormat("A<bool()> a;");
12860 verifyFormat("A<SomeType()> a;");
12861 verifyFormat("A<void (*)(int, std::string)> a;");
12862 verifyFormat("A<void *(int)>;");
12863 verifyFormat("void *(*a)(int *, SomeType *);");
12864 verifyFormat("int (*func)(void *);");
12865 verifyFormat("void f() { int (*func)(void *); }");
12866 verifyFormat("template <class CallbackClass>\n"
12867 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12868
12869 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12870 verifyGoogleFormat("void* (*a)(int);");
12871 verifyGoogleFormat(
12872 "template <class CallbackClass>\n"
12873 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12874
12875 // Other constructs can look somewhat like function types:
12876 verifyFormat("A<sizeof(*x)> a;");
12877 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12878 verifyFormat("some_var = function(*some_pointer_var)[0];");
12879 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12880 verifyFormat("int x = f(&h)();");
12881 verifyFormat("returnsFunction(&param1, &param2)(param);");
12882 verifyFormat("std::function<\n"
12883 " LooooooooooongTemplatedType<\n"
12884 " SomeType>*(\n"
12885 " LooooooooooooooooongType type)>\n"
12886 " function;",
12887 getGoogleStyleWithColumns(40));
12888 }
12889
12890 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12891 verifyFormat("A (*foo_)[6];");
12892 verifyFormat("vector<int> (*foo_)[6];");
12893 }
12894
12895 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12896 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12897 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12898 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12899 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12900 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12901 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12902
12903 // Different ways of ()-initializiation.
12904 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12905 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12906 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12907 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12908 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12909 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12910 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12911 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12912
12913 // Lambdas should not confuse the variable declaration heuristic.
12914 verifyFormat("LooooooooooooooooongType\n"
12915 " variable(nullptr, [](A *a) {});",
12916 getLLVMStyleWithColumns(40));
12917 }
12918
12919 TEST_F(FormatTest, BreaksLongDeclarations) {
12920 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12921 " AnotherNameForTheLongType;");
12922 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12924 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12925 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12926 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12927 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12928 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12929 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12930 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12931 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12932 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12933 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12934 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12935 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12936 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12937 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12938 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12939 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12940 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12941 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12942 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12943 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12944 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12945 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12946 FormatStyle Indented = getLLVMStyle();
12947 Indented.IndentWrappedFunctionNames = true;
12948 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12949 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12950 Indented);
12951 verifyFormat(
12952 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12953 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12954 Indented);
12955 verifyFormat(
12956 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12957 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12958 Indented);
12959 verifyFormat(
12960 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12961 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12962 Indented);
12963
12964 // FIXME: Without the comment, this breaks after "(".
12965 verifyGoogleFormat(
12966 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12967 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12968
12969 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12970 " int LoooooooooooooooooooongParam2) {}");
12971 verifyFormat(
12972 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12973 " SourceLocation L, IdentifierIn *II,\n"
12974 " Type *T) {}");
12975 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12976 "ReallyReaaallyLongFunctionName(\n"
12977 " const std::string &SomeParameter,\n"
12978 " const SomeType<string, SomeOtherTemplateParameter>\n"
12979 " &ReallyReallyLongParameterName,\n"
12980 " const SomeType<string, SomeOtherTemplateParameter>\n"
12981 " &AnotherLongParameterName) {}");
12982 verifyFormat("template <typename A>\n"
12983 "SomeLoooooooooooooooooooooongType<\n"
12984 " typename some_namespace::SomeOtherType<A>::Type>\n"
12985 "Function() {}");
12986
12987 verifyGoogleFormat(
12988 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12989 " aaaaaaaaaaaaaaaaaaaaaaa;");
12990 verifyGoogleFormat(
12991 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12992 " SourceLocation L) {}");
12993 verifyGoogleFormat(
12994 "some_namespace::LongReturnType\n"
12995 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12996 " int first_long_parameter, int second_parameter) {}");
12997
12998 verifyGoogleFormat("template <typename T>\n"
12999 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
13000 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
13001 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13002 " int aaaaaaaaaaaaaaaaaaaaaaa);");
13003
13004 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
13005 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13006 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13007 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
13008 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
13009 " aaaaaaaaaaaaaaaaaaaaaaaa);");
13010 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
13011 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
13012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
13013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13014
13015 verifyFormat("template <typename T> // Templates on own line.\n"
13016 "static int // Some comment.\n"
13017 "MyFunction(int a);");
13018 }
13019
13020 TEST_F(FormatTest, FormatsAccessModifiers) {
13021 FormatStyle Style = getLLVMStyle();
13022 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
13023 FormatStyle::ELBAMS_LogicalBlock);
13024 verifyFormat("struct foo {\n"
13025 "private:\n"
13026 " void f() {}\n"
13027 "\n"
13028 "private:\n"
13029 " int i;\n"
13030 "\n"
13031 "protected:\n"
13032 " int j;\n"
13033 "};",
13034 Style);
13035 verifyFormat("struct foo {\n"
13036 "private:\n"
13037 " void f() {}\n"
13038 "\n"
13039 "private:\n"
13040 " int i;\n"
13041 "\n"
13042 "protected:\n"
13043 " int j;\n"
13044 "};",
13045 "struct foo {\n"
13046 "private:\n"
13047 " void f() {}\n"
13048 "private:\n"
13049 " int i;\n"
13050 "protected:\n"
13051 " int j;\n"
13052 "};",
13053 Style);
13054 verifyFormat("struct foo { /* comment */\n"
13055 "private:\n"
13056 " int i;\n"
13057 " // comment\n"
13058 "private:\n"
13059 " int j;\n"
13060 "};",
13061 Style);
13062 verifyFormat("struct foo {\n"
13063 "#ifdef FOO\n"
13064 "#endif\n"
13065 "private:\n"
13066 " int i;\n"
13067 "#ifdef FOO\n"
13068 "private:\n"
13069 "#endif\n"
13070 " int j;\n"
13071 "};",
13072 Style);
13073 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13074 verifyFormat("struct foo {\n"
13075 "private:\n"
13076 " void f() {}\n"
13077 "private:\n"
13078 " int i;\n"
13079 "protected:\n"
13080 " int j;\n"
13081 "};",
13082 Style);
13083 verifyFormat("struct foo {\n"
13084 "private:\n"
13085 " void f() {}\n"
13086 "private:\n"
13087 " int i;\n"
13088 "protected:\n"
13089 " int j;\n"
13090 "};",
13091 "struct foo {\n"
13092 "\n"
13093 "private:\n"
13094 " void f() {}\n"
13095 "\n"
13096 "private:\n"
13097 " int i;\n"
13098 "\n"
13099 "protected:\n"
13100 " int j;\n"
13101 "};",
13102 Style);
13103 verifyFormat("struct foo { /* comment */\n"
13104 "private:\n"
13105 " int i;\n"
13106 " // comment\n"
13107 "private:\n"
13108 " int j;\n"
13109 "};",
13110 "struct foo { /* comment */\n"
13111 "\n"
13112 "private:\n"
13113 " int i;\n"
13114 " // comment\n"
13115 "\n"
13116 "private:\n"
13117 " int j;\n"
13118 "};",
13119 Style);
13120 verifyFormat("struct foo {\n"
13121 "#ifdef FOO\n"
13122 "#endif\n"
13123 "private:\n"
13124 " int i;\n"
13125 "#ifdef FOO\n"
13126 "private:\n"
13127 "#endif\n"
13128 " int j;\n"
13129 "};",
13130 "struct foo {\n"
13131 "#ifdef FOO\n"
13132 "#endif\n"
13133 "\n"
13134 "private:\n"
13135 " int i;\n"
13136 "#ifdef FOO\n"
13137 "\n"
13138 "private:\n"
13139 "#endif\n"
13140 " int j;\n"
13141 "};",
13142 Style);
13143 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13144 verifyFormat("struct foo {\n"
13145 "private:\n"
13146 " void f() {}\n"
13147 "\n"
13148 "private:\n"
13149 " int i;\n"
13150 "\n"
13151 "protected:\n"
13152 " int j;\n"
13153 "};",
13154 Style);
13155 verifyFormat("struct foo {\n"
13156 "private:\n"
13157 " void f() {}\n"
13158 "\n"
13159 "private:\n"
13160 " int i;\n"
13161 "\n"
13162 "protected:\n"
13163 " int j;\n"
13164 "};",
13165 "struct foo {\n"
13166 "private:\n"
13167 " void f() {}\n"
13168 "private:\n"
13169 " int i;\n"
13170 "protected:\n"
13171 " int j;\n"
13172 "};",
13173 Style);
13174 verifyFormat("struct foo { /* comment */\n"
13175 "private:\n"
13176 " int i;\n"
13177 " // comment\n"
13178 "\n"
13179 "private:\n"
13180 " int j;\n"
13181 "};",
13182 Style);
13183 verifyFormat("struct foo {\n"
13184 "#ifdef FOO\n"
13185 "#endif\n"
13186 "\n"
13187 "private:\n"
13188 " int i;\n"
13189 "#ifdef FOO\n"
13190 "\n"
13191 "private:\n"
13192 "#endif\n"
13193 " int j;\n"
13194 "};",
13195 "struct foo {\n"
13196 "#ifdef FOO\n"
13197 "#endif\n"
13198 "private:\n"
13199 " int i;\n"
13200 "#ifdef FOO\n"
13201 "private:\n"
13202 "#endif\n"
13203 " int j;\n"
13204 "};",
13205 Style);
13206 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13207 verifyNoChange("struct foo {\n"
13208 "\n"
13209 "private:\n"
13210 " void f() {}\n"
13211 "\n"
13212 "private:\n"
13213 " int i;\n"
13214 "\n"
13215 "protected:\n"
13216 " int j;\n"
13217 "};",
13218 Style);
13219 verifyFormat("struct foo {\n"
13220 "private:\n"
13221 " void f() {}\n"
13222 "private:\n"
13223 " int i;\n"
13224 "protected:\n"
13225 " int j;\n"
13226 "};",
13227 Style);
13228 verifyNoChange("struct foo { /* comment */\n"
13229 "\n"
13230 "private:\n"
13231 " int i;\n"
13232 " // comment\n"
13233 "\n"
13234 "private:\n"
13235 " int j;\n"
13236 "};",
13237 Style);
13238 verifyFormat("struct foo { /* comment */\n"
13239 "private:\n"
13240 " int i;\n"
13241 " // comment\n"
13242 "private:\n"
13243 " int j;\n"
13244 "};",
13245 Style);
13246 verifyNoChange("struct foo {\n"
13247 "#ifdef FOO\n"
13248 "#endif\n"
13249 "\n"
13250 "private:\n"
13251 " int i;\n"
13252 "#ifdef FOO\n"
13253 "\n"
13254 "private:\n"
13255 "#endif\n"
13256 " int j;\n"
13257 "};",
13258 Style);
13259 verifyFormat("struct foo {\n"
13260 "#ifdef FOO\n"
13261 "#endif\n"
13262 "private:\n"
13263 " int i;\n"
13264 "#ifdef FOO\n"
13265 "private:\n"
13266 "#endif\n"
13267 " int j;\n"
13268 "};",
13269 Style);
13270 Style.AttributeMacros.push_back("FOO");
13271 Style.AttributeMacros.push_back("BAR");
13272 verifyFormat("struct foo {\n"
13273 "FOO private:\n"
13274 " int i;\n"
13275 "BAR(x) protected:\n"
13276 " int j;\n"
13277 "};",
13278 Style);
13279
13280 FormatStyle NoEmptyLines = getLLVMStyle();
13281 NoEmptyLines.MaxEmptyLinesToKeep = 0;
13282 verifyFormat("struct foo {\n"
13283 "private:\n"
13284 " void f() {}\n"
13285 "\n"
13286 "private:\n"
13287 " int i;\n"
13288 "\n"
13289 "public:\n"
13290 "protected:\n"
13291 " int j;\n"
13292 "};",
13293 NoEmptyLines);
13294
13295 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13296 verifyFormat("struct foo {\n"
13297 "private:\n"
13298 " void f() {}\n"
13299 "private:\n"
13300 " int i;\n"
13301 "public:\n"
13302 "protected:\n"
13303 " int j;\n"
13304 "};",
13305 NoEmptyLines);
13306
13307 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13308 verifyFormat("struct foo {\n"
13309 "private:\n"
13310 " void f() {}\n"
13311 "\n"
13312 "private:\n"
13313 " int i;\n"
13314 "\n"
13315 "public:\n"
13316 "\n"
13317 "protected:\n"
13318 " int j;\n"
13319 "};",
13320 NoEmptyLines);
13321 }
13322
13323 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
13324
13325 FormatStyle Style = getLLVMStyle();
13326 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
13327 verifyFormat("struct foo {\n"
13328 "private:\n"
13329 " void f() {}\n"
13330 "\n"
13331 "private:\n"
13332 " int i;\n"
13333 "\n"
13334 "protected:\n"
13335 " int j;\n"
13336 "};",
13337 Style);
13338
13339 // Check if lines are removed.
13340 verifyFormat("struct foo {\n"
13341 "private:\n"
13342 " void f() {}\n"
13343 "\n"
13344 "private:\n"
13345 " int i;\n"
13346 "\n"
13347 "protected:\n"
13348 " int j;\n"
13349 "};",
13350 "struct foo {\n"
13351 "private:\n"
13352 "\n"
13353 " void f() {}\n"
13354 "\n"
13355 "private:\n"
13356 "\n"
13357 " int i;\n"
13358 "\n"
13359 "protected:\n"
13360 "\n"
13361 " int j;\n"
13362 "};",
13363 Style);
13364
13365 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13366 verifyFormat("struct foo {\n"
13367 "private:\n"
13368 "\n"
13369 " void f() {}\n"
13370 "\n"
13371 "private:\n"
13372 "\n"
13373 " int i;\n"
13374 "\n"
13375 "protected:\n"
13376 "\n"
13377 " int j;\n"
13378 "};",
13379 Style);
13380
13381 // Check if lines are added.
13382 verifyFormat("struct foo {\n"
13383 "private:\n"
13384 "\n"
13385 " void f() {}\n"
13386 "\n"
13387 "private:\n"
13388 "\n"
13389 " int i;\n"
13390 "\n"
13391 "protected:\n"
13392 "\n"
13393 " int j;\n"
13394 "};",
13395 "struct foo {\n"
13396 "private:\n"
13397 " void f() {}\n"
13398 "\n"
13399 "private:\n"
13400 " int i;\n"
13401 "\n"
13402 "protected:\n"
13403 " int j;\n"
13404 "};",
13405 Style);
13406
13407 // Leave tests rely on the code layout, test::messUp can not be used.
13408 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13409 Style.MaxEmptyLinesToKeep = 0u;
13410 verifyFormat("struct foo {\n"
13411 "private:\n"
13412 " void f() {}\n"
13413 "\n"
13414 "private:\n"
13415 " int i;\n"
13416 "\n"
13417 "protected:\n"
13418 " int j;\n"
13419 "};",
13420 Style);
13421
13422 // Check if MaxEmptyLinesToKeep is respected.
13423 verifyFormat("struct foo {\n"
13424 "private:\n"
13425 " void f() {}\n"
13426 "\n"
13427 "private:\n"
13428 " int i;\n"
13429 "\n"
13430 "protected:\n"
13431 " int j;\n"
13432 "};",
13433 "struct foo {\n"
13434 "private:\n"
13435 "\n\n\n"
13436 " void f() {}\n"
13437 "\n"
13438 "private:\n"
13439 "\n\n\n"
13440 " int i;\n"
13441 "\n"
13442 "protected:\n"
13443 "\n\n\n"
13444 " int j;\n"
13445 "};",
13446 Style);
13447
13448 Style.MaxEmptyLinesToKeep = 1u;
13449 verifyNoChange("struct foo {\n"
13450 "private:\n"
13451 "\n"
13452 " void f() {}\n"
13453 "\n"
13454 "private:\n"
13455 "\n"
13456 " int i;\n"
13457 "\n"
13458 "protected:\n"
13459 "\n"
13460 " int j;\n"
13461 "};",
13462 Style);
13463 // Check if no lines are kept.
13464 verifyFormat("struct foo {\n"
13465 "private:\n"
13466 " void f() {}\n"
13467 "\n"
13468 "private:\n"
13469 " int i;\n"
13470 "\n"
13471 "protected:\n"
13472 " int j;\n"
13473 "};",
13474 Style);
13475 // Check if MaxEmptyLinesToKeep is respected.
13476 verifyFormat("struct foo {\n"
13477 "private:\n"
13478 "\n"
13479 " void f() {}\n"
13480 "\n"
13481 "private:\n"
13482 "\n"
13483 " int i;\n"
13484 "\n"
13485 "protected:\n"
13486 "\n"
13487 " int j;\n"
13488 "};",
13489 "struct foo {\n"
13490 "private:\n"
13491 "\n\n\n"
13492 " void f() {}\n"
13493 "\n"
13494 "private:\n"
13495 "\n\n\n"
13496 " int i;\n"
13497 "\n"
13498 "protected:\n"
13499 "\n\n\n"
13500 " int j;\n"
13501 "};",
13502 Style);
13503
13504 Style.MaxEmptyLinesToKeep = 10u;
13505 verifyNoChange("struct foo {\n"
13506 "private:\n"
13507 "\n\n\n"
13508 " void f() {}\n"
13509 "\n"
13510 "private:\n"
13511 "\n\n\n"
13512 " int i;\n"
13513 "\n"
13514 "protected:\n"
13515 "\n\n\n"
13516 " int j;\n"
13517 "};",
13518 Style);
13519
13520 // Test with comments.
13521 Style = getLLVMStyle();
13522 verifyFormat("struct foo {\n"
13523 "private:\n"
13524 " // comment\n"
13525 " void f() {}\n"
13526 "\n"
13527 "private: /* comment */\n"
13528 " int i;\n"
13529 "};",
13530 Style);
13531 verifyFormat("struct foo {\n"
13532 "private:\n"
13533 " // comment\n"
13534 " void f() {}\n"
13535 "\n"
13536 "private: /* comment */\n"
13537 " int i;\n"
13538 "};",
13539 "struct foo {\n"
13540 "private:\n"
13541 "\n"
13542 " // comment\n"
13543 " void f() {}\n"
13544 "\n"
13545 "private: /* comment */\n"
13546 "\n"
13547 " int i;\n"
13548 "};",
13549 Style);
13550
13551 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13552 verifyFormat("struct foo {\n"
13553 "private:\n"
13554 "\n"
13555 " // comment\n"
13556 " void f() {}\n"
13557 "\n"
13558 "private: /* comment */\n"
13559 "\n"
13560 " int i;\n"
13561 "};",
13562 "struct foo {\n"
13563 "private:\n"
13564 " // comment\n"
13565 " void f() {}\n"
13566 "\n"
13567 "private: /* comment */\n"
13568 " int i;\n"
13569 "};",
13570 Style);
13571 verifyFormat("struct foo {\n"
13572 "private:\n"
13573 "\n"
13574 " // comment\n"
13575 " void f() {}\n"
13576 "\n"
13577 "private: /* comment */\n"
13578 "\n"
13579 " int i;\n"
13580 "};",
13581 Style);
13582
13583 // Test with preprocessor defines.
13584 Style = getLLVMStyle();
13585 verifyFormat("struct foo {\n"
13586 "private:\n"
13587 "#ifdef FOO\n"
13588 "#endif\n"
13589 " void f() {}\n"
13590 "};",
13591 Style);
13592 verifyFormat("struct foo {\n"
13593 "private:\n"
13594 "#ifdef FOO\n"
13595 "#endif\n"
13596 " void f() {}\n"
13597 "};",
13598 "struct foo {\n"
13599 "private:\n"
13600 "\n"
13601 "#ifdef FOO\n"
13602 "#endif\n"
13603 " void f() {}\n"
13604 "};",
13605 Style);
13606 verifyNoChange("struct foo {\n"
13607 "#ifdef FOO\n"
13608 "#else\n"
13609 "private:\n"
13610 "\n"
13611 "#endif\n"
13612 "};",
13613 Style);
13614 verifyFormat("struct foo {\n"
13615 "#ifdef FOO\n"
13616 "#else\n"
13617 "private:\n"
13618 "\n"
13619 "#endif\n"
13620 "};",
13621 "struct foo {\n"
13622 "#ifdef FOO\n"
13623 "#else\n"
13624 "private:\n"
13625 "\n"
13626 "\n"
13627 "#endif\n"
13628 "};",
13629 Style);
13630 verifyFormat("struct foo {\n"
13631 "#ifdef FOO\n"
13632 "private:\n"
13633 "#else\n"
13634 "#endif\n"
13635 "};",
13636 "struct foo {\n"
13637 "#ifdef FOO\n"
13638 "private:\n"
13639 "\n"
13640 "\n"
13641 "#else\n"
13642 "#endif\n"
13643 "};",
13644 Style);
13645 verifyFormat("struct foo {\n"
13646 "#if 0\n"
13647 "#else\n"
13648 "#endif\n"
13649 "#ifdef FOO\n"
13650 "private:\n"
13651 "#endif\n"
13652 "};",
13653 "struct foo {\n"
13654 "#if 0\n"
13655 "#else\n"
13656 "#endif\n"
13657 "#ifdef FOO\n"
13658 "private:\n"
13659 "\n"
13660 "\n"
13661 "#endif\n"
13662 "};",
13663 Style);
13664
13665 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13666 verifyFormat("struct foo {\n"
13667 "private:\n"
13668 "\n"
13669 "#ifdef FOO\n"
13670 "#endif\n"
13671 " void f() {}\n"
13672 "};",
13673 "struct foo {\n"
13674 "private:\n"
13675 "#ifdef FOO\n"
13676 "#endif\n"
13677 " void f() {}\n"
13678 "};",
13679 Style);
13680 verifyFormat("struct foo {\n"
13681 "private:\n"
13682 "\n"
13683 "#ifdef FOO\n"
13684 "#endif\n"
13685 " void f() {}\n"
13686 "};",
13687 Style);
13688 }
13689
13690 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13691 // Combined tests of EmptyLineAfterAccessModifier and
13692 // EmptyLineBeforeAccessModifier.
13693 FormatStyle Style = getLLVMStyle();
13694 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13695 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13696 verifyFormat("struct foo {\n"
13697 "private:\n"
13698 "\n"
13699 "protected:\n"
13700 "};",
13701 Style);
13702
13703 Style.MaxEmptyLinesToKeep = 10u;
13704 // Both remove all new lines.
13705 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13706 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13707 verifyFormat("struct foo {\n"
13708 "private:\n"
13709 "protected:\n"
13710 "};",
13711 "struct foo {\n"
13712 "private:\n"
13713 "\n\n\n"
13714 "protected:\n"
13715 "};",
13716 Style);
13717
13718 // Leave tests rely on the code layout, test::messUp can not be used.
13719 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13720 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13721 Style.MaxEmptyLinesToKeep = 10u;
13722 verifyNoChange("struct foo {\n"
13723 "private:\n"
13724 "\n\n\n"
13725 "protected:\n"
13726 "};",
13727 Style);
13728 Style.MaxEmptyLinesToKeep = 3u;
13729 verifyNoChange("struct foo {\n"
13730 "private:\n"
13731 "\n\n\n"
13732 "protected:\n"
13733 "};",
13734 Style);
13735 Style.MaxEmptyLinesToKeep = 1u;
13736 verifyNoChange("struct foo {\n"
13737 "private:\n"
13738 "\n\n\n"
13739 "protected:\n"
13740 "};",
13741 Style); // Based on new lines in original document and not
13742 // on the setting.
13743
13744 Style.MaxEmptyLinesToKeep = 10u;
13745 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13746 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13747 // Newlines are kept if they are greater than zero,
13748 // test::messUp removes all new lines which changes the logic
13749 verifyNoChange("struct foo {\n"
13750 "private:\n"
13751 "\n\n\n"
13752 "protected:\n"
13753 "};",
13754 Style);
13755
13756 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13757 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13758 // test::messUp removes all new lines which changes the logic
13759 verifyNoChange("struct foo {\n"
13760 "private:\n"
13761 "\n\n\n"
13762 "protected:\n"
13763 "};",
13764 Style);
13765
13766 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13767 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13768 verifyNoChange("struct foo {\n"
13769 "private:\n"
13770 "\n\n\n"
13771 "protected:\n"
13772 "};",
13773 Style); // test::messUp removes all new lines which changes
13774 // the logic.
13775
13776 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13777 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13778 verifyFormat("struct foo {\n"
13779 "private:\n"
13780 "protected:\n"
13781 "};",
13782 "struct foo {\n"
13783 "private:\n"
13784 "\n\n\n"
13785 "protected:\n"
13786 "};",
13787 Style);
13788
13789 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13790 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13791 verifyNoChange("struct foo {\n"
13792 "private:\n"
13793 "\n\n\n"
13794 "protected:\n"
13795 "};",
13796 Style); // test::messUp removes all new lines which changes
13797 // the logic.
13798
13799 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13800 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13801 verifyFormat("struct foo {\n"
13802 "private:\n"
13803 "protected:\n"
13804 "};",
13805 "struct foo {\n"
13806 "private:\n"
13807 "\n\n\n"
13808 "protected:\n"
13809 "};",
13810 Style);
13811
13812 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13813 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13814 verifyFormat("struct foo {\n"
13815 "private:\n"
13816 "protected:\n"
13817 "};",
13818 "struct foo {\n"
13819 "private:\n"
13820 "\n\n\n"
13821 "protected:\n"
13822 "};",
13823 Style);
13824
13825 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13826 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13827 verifyFormat("struct foo {\n"
13828 "private:\n"
13829 "protected:\n"
13830 "};",
13831 "struct foo {\n"
13832 "private:\n"
13833 "\n\n\n"
13834 "protected:\n"
13835 "};",
13836 Style);
13837
13838 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13839 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13840 verifyFormat("struct foo {\n"
13841 "private:\n"
13842 "protected:\n"
13843 "};",
13844 "struct foo {\n"
13845 "private:\n"
13846 "\n\n\n"
13847 "protected:\n"
13848 "};",
13849 Style);
13850 }
13851
13852 TEST_F(FormatTest, FormatsArrays) {
13853 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13854 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13855 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13856 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13857 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13858 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13859 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13860 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13861 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13862 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13863 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13864 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13865 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13866 verifyFormat(
13867 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13868 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13869 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13870 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13871 " .aaaaaaaaaaaaaaaaaaaaaa();");
13872
13873 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13875 verifyFormat(
13876 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13877 " .aaaaaaa[0]\n"
13878 " .aaaaaaaaaaaaaaaaaaaaaa();");
13879 verifyFormat("a[::b::c];");
13880
13881 verifyFormat("{\n"
13882 " (*a)[0] = 1;\n"
13883 "}");
13884
13885 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13886
13887 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13888 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13889 }
13890
13891 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13892 verifyFormat("(a)->b();");
13893 verifyFormat("--a;");
13894 }
13895
13896 TEST_F(FormatTest, HandlesIncludeDirectives) {
13897 verifyFormat("#include <string>\n"
13898 "#include <a/b/c.h>\n"
13899 "#include \"a/b/string\"\n"
13900 "#include \"string.h\"\n"
13901 "#include \"string.h\"\n"
13902 "#include <a-a>\n"
13903 "#include < path with space >\n"
13904 "#include_next <test.h>"
13905 "#include \"abc.h\" // this is included for ABC\n"
13906 "#include \"some long include\" // with a comment\n"
13907 "#include \"some very long include path\"\n"
13908 "#include <some/very/long/include/path>",
13909 getLLVMStyleWithColumns(35));
13910 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13911 verifyFormat("#include <a>", "#include<a>");
13912
13913 verifyFormat("#import <string>");
13914 verifyFormat("#import <a/b/c.h>");
13915 verifyFormat("#import \"a/b/string\"");
13916 verifyFormat("#import \"string.h\"");
13917 verifyFormat("#import \"string.h\"");
13918 verifyFormat("#if __has_include(<strstream>)\n"
13919 "#include <strstream>\n"
13920 "#endif");
13921
13922 verifyFormat("#define MY_IMPORT <a/b>");
13923
13924 verifyFormat("#if __has_include(<a/b>)");
13925 verifyFormat("#if __has_include_next(<a/b>)");
13926 verifyFormat("#define F __has_include(<a/b>)");
13927 verifyFormat("#define F __has_include_next(<a/b>)");
13928
13929 // Protocol buffer definition or missing "#".
13930 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13931 getLLVMStyleWithColumns(30));
13932
13933 FormatStyle Style = getLLVMStyle();
13934 Style.AlwaysBreakBeforeMultilineStrings = true;
13935 Style.ColumnLimit = 0;
13936 verifyFormat("#import \"abc.h\"", Style);
13937
13938 // But 'import' might also be a regular C++ namespace.
13939 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13941 verifyFormat("import::Bar foo(val ? 2 : 1);");
13942 }
13943
13944 //===----------------------------------------------------------------------===//
13945 // Error recovery tests.
13946 //===----------------------------------------------------------------------===//
13947
13948 TEST_F(FormatTest, IncompleteParameterLists) {
13949 FormatStyle NoBinPacking = getLLVMStyle();
13950 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
13951 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13952 " double *min_x,\n"
13953 " double *max_x,\n"
13954 " double *min_y,\n"
13955 " double *max_y,\n"
13956 " double *min_z,\n"
13957 " double *max_z, ) {}",
13958 NoBinPacking);
13959 }
13960
13961 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13962 verifyFormat("void f() { return; }\n42");
13963 verifyFormat("void f() {\n"
13964 " if (0)\n"
13965 " return;\n"
13966 "}\n"
13967 "42");
13968 verifyFormat("void f() { return }\n42");
13969 verifyFormat("void f() {\n"
13970 " if (0)\n"
13971 " return\n"
13972 "}\n"
13973 "42");
13974 }
13975
13976 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13977 verifyFormat("void f() { return }", "void f ( ) { return }");
13978 verifyFormat("void f() {\n"
13979 " if (a)\n"
13980 " return\n"
13981 "}",
13982 "void f ( ) { if ( a ) return }");
13983 verifyFormat("namespace N {\n"
13984 "void f()\n"
13985 "}",
13986 "namespace N { void f() }");
13987 verifyFormat("namespace N {\n"
13988 "void f() {}\n"
13989 "void g()\n"
13990 "} // namespace N",
13991 "namespace N { void f( ) { } void g( ) }");
13992 }
13993
13994 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13995 verifyFormat("int aaaaaaaa =\n"
13996 " // Overlylongcomment\n"
13997 " b;",
13998 getLLVMStyleWithColumns(20));
13999 verifyFormat("function(\n"
14000 " ShortArgument,\n"
14001 " LoooooooooooongArgument);",
14002 getLLVMStyleWithColumns(20));
14003 }
14004
14005 TEST_F(FormatTest, IncorrectAccessSpecifier) {
14006 verifyFormat("public:");
14007 verifyFormat("class A {\n"
14008 "public\n"
14009 " void f() {}\n"
14010 "};");
14011 verifyFormat("public\n"
14012 "int qwerty;");
14013 verifyFormat("public\n"
14014 "B {}");
14015 verifyFormat("public\n"
14016 "{\n"
14017 "}");
14018 verifyFormat("public\n"
14019 "B { int x; }");
14020 }
14021
14022 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
14023 verifyFormat("{");
14024 verifyFormat("#})");
14025 verifyNoCrash("(/**/[:!] ?[).");
14026 verifyNoCrash("struct X {\n"
14027 " operator iunt(\n"
14028 "};");
14029 verifyNoCrash("struct Foo {\n"
14030 " operator foo(bar\n"
14031 "};");
14032 verifyNoCrash("decltype( {\n"
14033 " {");
14034 }
14035
14036 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
14037 // Found by oss-fuzz:
14038 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
14039 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
14040 Style.ColumnLimit = 60;
14041 verifyNoCrash(
14042 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
14043 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
14044 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
14045 Style);
14046 }
14047
14048 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
14049 verifyFormat("do {\n}");
14050 verifyFormat("do {\n}\n"
14051 "f();");
14052 verifyFormat("do {\n}\n"
14053 "wheeee(fun);");
14054 verifyFormat("do {\n"
14055 " f();\n"
14056 "}");
14057 }
14058
14059 TEST_F(FormatTest, IncorrectCodeMissingParens) {
14060 verifyFormat("if {\n foo;\n foo();\n}");
14061 verifyFormat("switch {\n foo;\n foo();\n}");
14062 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
14063 verifyIncompleteFormat("ERROR: for target;");
14064 verifyFormat("while {\n foo;\n foo();\n}");
14065 verifyFormat("do {\n foo;\n foo();\n} while;");
14066 }
14067
14068 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
14069 verifyIncompleteFormat("namespace {\n"
14070 "class Foo { Foo (\n"
14071 "};\n"
14072 "} // namespace");
14073 }
14074
14075 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
14076 verifyFormat("{\n"
14077 " {\n"
14078 " }",
14079 "{\n"
14080 "{\n"
14081 "}");
14082 verifyFormat("{\n"
14083 " {\n"
14084 " }",
14085 "{\n"
14086 " {\n"
14087 "}");
14088 verifyFormat("{\n"
14089 " {\n"
14090 " }");
14091 verifyFormat("{\n"
14092 " {\n"
14093 " }\n"
14094 "}\n"
14095 "}",
14096 "{\n"
14097 " {\n"
14098 " }\n"
14099 " }\n"
14100 "}");
14101
14102 verifyFormat("{\n"
14103 " {\n"
14104 " breakme(\n"
14105 " qwe);\n"
14106 " }",
14107 "{\n"
14108 " {\n"
14109 " breakme(qwe);\n"
14110 "}",
14111 getLLVMStyleWithColumns(10));
14112 }
14113
14114 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
14115 verifyFormat("int x = {\n"
14116 " avariable,\n"
14117 " b(alongervariable)};",
14118 getLLVMStyleWithColumns(25));
14119 }
14120
14121 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
14122 verifyFormat("return (a)(b){1, 2, 3};");
14123 }
14124
14125 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
14126 verifyFormat("vector<int> x{1, 2, 3, 4};");
14127 verifyFormat("vector<int> x{\n"
14128 " 1,\n"
14129 " 2,\n"
14130 " 3,\n"
14131 " 4,\n"
14132 "};");
14133 verifyFormat("vector<T> x{{}, {}, {}, {}};");
14134 verifyFormat("f({1, 2});");
14135 verifyFormat("auto v = Foo{-1};");
14136 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
14137 verifyFormat("Class::Class : member{1, 2, 3} {}");
14138 verifyFormat("new vector<int>{1, 2, 3};");
14139 verifyFormat("new int[3]{1, 2, 3};");
14140 verifyFormat("new int{1};");
14141 verifyFormat("return {arg1, arg2};");
14142 verifyFormat("return {arg1, SomeType{parameter}};");
14143 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
14144 verifyFormat("new T{arg1, arg2};");
14145 verifyFormat("f(MyMap[{composite, key}]);");
14146 verifyFormat("class Class {\n"
14147 " T member = {arg1, arg2};\n"
14148 "};");
14149 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
14150 verifyFormat("const struct A a = {.a = 1, .b = 2};");
14151 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
14152 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
14153 verifyFormat("int a = std::is_integral<int>{} + 0;");
14154
14155 verifyFormat("int foo(int i) { return fo1{}(i); }");
14156 verifyFormat("int foo(int i) { return fo1{}(i); }");
14157 verifyFormat("auto i = decltype(x){};");
14158 verifyFormat("auto i = typeof(x){};");
14159 verifyFormat("auto i = _Atomic(x){};");
14160 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
14161 verifyFormat("Node n{1, Node{1000}, //\n"
14162 " 2};");
14163 verifyFormat("Aaaa aaaaaaa{\n"
14164 " {\n"
14165 " aaaa,\n"
14166 " },\n"
14167 "};");
14168 verifyFormat("class C : public D {\n"
14169 " SomeClass SC{2};\n"
14170 "};");
14171 verifyFormat("class C : public A {\n"
14172 " class D : public B {\n"
14173 " void f() { int i{2}; }\n"
14174 " };\n"
14175 "};");
14176 verifyFormat("#define A {a, a},");
14177 // Don't confuse braced list initializers with compound statements.
14178 verifyFormat(
14179 "class A {\n"
14180 " A() : a{} {}\n"
14181 " A() : Base<int>{} {}\n"
14182 " A() : Base<Foo<int>>{} {}\n"
14183 " A(int b) : b(b) {}\n"
14184 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
14185 " int a, b;\n"
14186 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
14187 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
14188 "{}\n"
14189 "};");
14190
14191 // Avoid breaking between equal sign and opening brace
14192 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
14193 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
14194 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
14195 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
14196 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
14197 " {\"ccccccccccccccccccccc\", 2}};",
14198 AvoidBreakingFirstArgument);
14199
14200 // Binpacking only if there is no trailing comma
14201 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
14202 " cccccccccc, dddddddddd};",
14203 getLLVMStyleWithColumns(50));
14204 verifyFormat("const Aaaaaa aaaaa = {\n"
14205 " aaaaaaaaaaa,\n"
14206 " bbbbbbbbbbb,\n"
14207 " ccccccccccc,\n"
14208 " ddddddddddd,\n"
14209 "};",
14210 getLLVMStyleWithColumns(50));
14211
14212 // Cases where distinguising braced lists and blocks is hard.
14213 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
14214 verifyFormat("void f() {\n"
14215 " return; // comment\n"
14216 "}\n"
14217 "SomeType t;");
14218 verifyFormat("void f() {\n"
14219 " if (a) {\n"
14220 " f();\n"
14221 " }\n"
14222 "}\n"
14223 "SomeType t;");
14224
14225 // In combination with BinPackArguments = false.
14226 FormatStyle NoBinPacking = getLLVMStyle();
14227 NoBinPacking.BinPackArguments = false;
14228 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14229 " bbbbb,\n"
14230 " ccccc,\n"
14231 " ddddd,\n"
14232 " eeeee,\n"
14233 " ffffff,\n"
14234 " ggggg,\n"
14235 " hhhhhh,\n"
14236 " iiiiii,\n"
14237 " jjjjjj,\n"
14238 " kkkkkk};",
14239 NoBinPacking);
14240 verifyFormat("const Aaaaaa aaaaa = {\n"
14241 " aaaaa,\n"
14242 " bbbbb,\n"
14243 " ccccc,\n"
14244 " ddddd,\n"
14245 " eeeee,\n"
14246 " ffffff,\n"
14247 " ggggg,\n"
14248 " hhhhhh,\n"
14249 " iiiiii,\n"
14250 " jjjjjj,\n"
14251 " kkkkkk,\n"
14252 "};",
14253 NoBinPacking);
14254 verifyFormat(
14255 "const Aaaaaa aaaaa = {\n"
14256 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
14257 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
14258 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14259 "};",
14260 NoBinPacking);
14261
14262 NoBinPacking.BinPackLongBracedList = false;
14263 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14264 " bbbbb,\n"
14265 " ccccc,\n"
14266 " ddddd,\n"
14267 " eeeee,\n"
14268 " ffffff,\n"
14269 " ggggg,\n"
14270 " hhhhhh,\n"
14271 " iiiiii,\n"
14272 " jjjjjj,\n"
14273 " kkkkkk,\n"
14274 " aaaaa,\n"
14275 " bbbbb,\n"
14276 " ccccc,\n"
14277 " ddddd,\n"
14278 " eeeee,\n"
14279 " ffffff,\n"
14280 " ggggg,\n"
14281 " hhhhhh,\n"
14282 " iiiiii};",
14283 NoBinPacking);
14284 verifyFormat("const Aaaaaa aaaaa = {\n"
14285 " aaaaa,\n"
14286 " bbbbb,\n"
14287 " ccccc,\n"
14288 " ddddd,\n"
14289 " eeeee,\n"
14290 " ffffff,\n"
14291 " ggggg,\n"
14292 " hhhhhh,\n"
14293 " iiiiii,\n"
14294 " jjjjjj,\n"
14295 " kkkkkk,\n"
14296 " aaaaa,\n"
14297 " bbbbb,\n"
14298 " ccccc,\n"
14299 " ddddd,\n"
14300 " eeeee,\n"
14301 " ffffff,\n"
14302 " ggggg,\n"
14303 " hhhhhh,\n"
14304 "};",
14305 NoBinPacking);
14306
14307 NoBinPacking.BreakAfterOpenBracketBracedList = true;
14308 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14309 " CDDDP83848_BMCR_REGISTER,\n"
14310 " CDDDP83848_BMSR_REGISTER,\n"
14311 " CDDDP83848_RBR_REGISTER};",
14312 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14313 " CDDDP83848_BMSR_REGISTER,\n"
14314 " CDDDP83848_RBR_REGISTER};",
14315 NoBinPacking);
14316
14317 // FIXME: The alignment of these trailing comments might be bad. Then again,
14318 // this might be utterly useless in real code.
14319 verifyFormat("Constructor::Constructor()\n"
14320 " : some_value{ //\n"
14321 " aaaaaaa, //\n"
14322 " bbbbbbb} {}");
14323
14324 // In braced lists, the first comment is always assumed to belong to the
14325 // first element. Thus, it can be moved to the next or previous line as
14326 // appropriate.
14327 verifyFormat("function({// First element:\n"
14328 " 1,\n"
14329 " // Second element:\n"
14330 " 2});",
14331 "function({\n"
14332 " // First element:\n"
14333 " 1,\n"
14334 " // Second element:\n"
14335 " 2});");
14336 verifyFormat("std::vector<int> MyNumbers{\n"
14337 " // First element:\n"
14338 " 1,\n"
14339 " // Second element:\n"
14340 " 2};",
14341 "std::vector<int> MyNumbers{// First element:\n"
14342 " 1,\n"
14343 " // Second element:\n"
14344 " 2};",
14345 getLLVMStyleWithColumns(30));
14346 // A trailing comma should still lead to an enforced line break and no
14347 // binpacking.
14348 verifyFormat("vector<int> SomeVector = {\n"
14349 " // aaa\n"
14350 " 1,\n"
14351 " 2,\n"
14352 "};",
14353 "vector<int> SomeVector = { // aaa\n"
14354 " 1, 2, };");
14355
14356 // C++11 brace initializer list l-braces should not be treated any differently
14357 // when breaking before lambda bodies is enabled
14358 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
14359 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
14360 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
14361 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
14362 verifyFormat(
14363 "std::runtime_error{\n"
14364 " \"Long string which will force a break onto the next line...\"};",
14365 BreakBeforeLambdaBody);
14366
14367 FormatStyle ExtraSpaces = getLLVMStyle();
14368 ExtraSpaces.Cpp11BracedListStyle = FormatStyle::BLS_Block;
14369 ExtraSpaces.ColumnLimit = 75;
14370 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
14371 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
14372 verifyFormat("f({ 1, 2 });", ExtraSpaces);
14373 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
14374 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
14375 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
14376 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
14377 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
14378 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
14379 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
14380 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
14381 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
14382 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
14383 verifyFormat("class Class {\n"
14384 " T member = { arg1, arg2 };\n"
14385 "};",
14386 ExtraSpaces);
14387 verifyFormat(
14388 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14389 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14390 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14391 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14392 ExtraSpaces);
14393 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
14394 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14395 ExtraSpaces);
14396 verifyFormat(
14397 "someFunction(OtherParam,\n"
14398 " BracedList{ // comment 1 (Forcing interesting break)\n"
14399 " param1, param2,\n"
14400 " // comment 2\n"
14401 " param3, param4 });",
14402 ExtraSpaces);
14403 verifyFormat(
14404 "std::this_thread::sleep_for(\n"
14405 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14406 ExtraSpaces);
14407 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14408 " aaaaaaa,\n"
14409 " aaaaaaaaaa,\n"
14410 " aaaaa,\n"
14411 " aaaaaaaaaaaaaaa,\n"
14412 " aaa,\n"
14413 " aaaaaaaaaa,\n"
14414 " a,\n"
14415 " aaaaaaaaaaaaaaaaaaaaa,\n"
14416 " aaaaaaaaaaaa,\n"
14417 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14418 " aaaaaaa,\n"
14419 " a};");
14420 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
14421 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
14422 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
14423
14424 // Avoid breaking between initializer/equal sign and opening brace
14425 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
14426 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14427 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14428 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14429 " { \"ccccccccccccccccccccc\", 2 }\n"
14430 "};",
14431 ExtraSpaces);
14432 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14433 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14434 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14435 " { \"ccccccccccccccccccccc\", 2 }\n"
14436 "};",
14437 ExtraSpaces);
14438
14439 FormatStyle SpaceBeforeBrace = getLLVMStyle();
14440 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
14441 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
14442 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
14443
14444 FormatStyle SpaceBetweenBraces = getLLVMStyle();
14445 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
14446 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14447 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
14448 SpaceBetweenBraces.SpacesInSquareBrackets = true;
14449 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
14450 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
14451 verifyFormat("vector< int > x{ // comment 1\n"
14452 " 1, 2, 3, 4 };",
14453 SpaceBetweenBraces);
14454 SpaceBetweenBraces.ColumnLimit = 20;
14455 verifyFormat("vector< int > x{\n"
14456 " 1, 2, 3, 4 };",
14457 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14458 SpaceBetweenBraces.ColumnLimit = 24;
14459 verifyFormat("vector< int > x{ 1, 2,\n"
14460 " 3, 4 };",
14461 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14462 verifyFormat("vector< int > x{\n"
14463 " 1,\n"
14464 " 2,\n"
14465 " 3,\n"
14466 " 4,\n"
14467 "};",
14468 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14469 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14470 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14471 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14472 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14473 }
14474
14475 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14476 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14477 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14478 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14479 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14480 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14481 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14482 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14483 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14484 " 1, 22, 333, 4444, 55555, //\n"
14485 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14486 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14487 verifyFormat(
14488 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14489 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14490 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14491 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14492 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14493 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14494 " 7777777};");
14495 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14496 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14497 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14498 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14499 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14500 " // Separating comment.\n"
14501 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14502 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14503 " // Leading comment\n"
14504 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14505 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14506 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14507 " 1, 1, 1, 1};",
14508 getLLVMStyleWithColumns(39));
14509 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14510 " 1, 1, 1, 1};",
14511 getLLVMStyleWithColumns(38));
14512 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14513 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14514 getLLVMStyleWithColumns(43));
14515 verifyFormat(
14516 "static unsigned SomeValues[10][3] = {\n"
14517 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14518 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14519 verifyFormat("static auto fields = new vector<string>{\n"
14520 " \"aaaaaaaaaaaaa\",\n"
14521 " \"aaaaaaaaaaaaa\",\n"
14522 " \"aaaaaaaaaaaa\",\n"
14523 " \"aaaaaaaaaaaaaa\",\n"
14524 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14525 " \"aaaaaaaaaaaa\",\n"
14526 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14527 "};");
14528 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14529 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14530 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14531 " 3, cccccccccccccccccccccc};",
14532 getLLVMStyleWithColumns(60));
14533
14534 // Trailing commas.
14535 verifyFormat("vector<int> x = {\n"
14536 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14537 "};",
14538 getLLVMStyleWithColumns(39));
14539 verifyFormat("vector<int> x = {\n"
14540 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14541 "};",
14542 getLLVMStyleWithColumns(39));
14543 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14544 " 1, 1, 1, 1,\n"
14545 " /**/ /**/};",
14546 getLLVMStyleWithColumns(39));
14547
14548 // Trailing comment in the first line.
14549 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14550 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14551 " 111111111, 222222222, 3333333333, 444444444, //\n"
14552 " 11111111, 22222222, 333333333, 44444444};");
14553 // Trailing comment in the last line.
14554 verifyFormat("int aaaaa[] = {\n"
14555 " 1, 2, 3, // comment\n"
14556 " 4, 5, 6 // comment\n"
14557 "};");
14558
14559 // With nested lists, we should either format one item per line or all nested
14560 // lists one on line.
14561 // FIXME: For some nested lists, we can do better.
14562 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14563 " {aaaaaaaaaaaaaaaaaaa},\n"
14564 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14565 " {aaaaaaaaaaaaaaaaa}};",
14566 getLLVMStyleWithColumns(60));
14567 verifyFormat(
14568 "SomeStruct my_struct_array = {\n"
14569 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14570 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14571 " {aaa, aaa},\n"
14572 " {aaa, aaa},\n"
14573 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14574 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14575 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14576
14577 // No column layout should be used here.
14578 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14579 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14580
14581 verifyNoCrash("a<,");
14582
14583 // No braced initializer here.
14584 verifyFormat("void f() {\n"
14585 " struct Dummy {};\n"
14586 " f(v);\n"
14587 "}");
14588 verifyFormat("void foo() {\n"
14589 " { // asdf\n"
14590 " {\n"
14591 " int a;\n"
14592 " }\n"
14593 " }\n"
14594 " {\n"
14595 " {\n"
14596 " int b;\n"
14597 " }\n"
14598 " }\n"
14599 "}");
14600 verifyFormat("namespace n {\n"
14601 "void foo() {\n"
14602 " {\n"
14603 " {\n"
14604 " statement();\n"
14605 " if (false) {\n"
14606 " }\n"
14607 " }\n"
14608 " }\n"
14609 " {\n"
14610 " }\n"
14611 "}\n"
14612 "} // namespace n");
14613
14614 // Long lists should be formatted in columns even if they are nested.
14615 verifyFormat(
14616 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14617 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14618 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14619 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14620 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14621 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14622
14623 // Allow "single-column" layout even if that violates the column limit. There
14624 // isn't going to be a better way.
14625 verifyFormat("std::vector<int> a = {\n"
14626 " aaaaaaaa,\n"
14627 " aaaaaaaa,\n"
14628 " aaaaaaaa,\n"
14629 " aaaaaaaa,\n"
14630 " aaaaaaaaaa,\n"
14631 " aaaaaaaa,\n"
14632 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14633 getLLVMStyleWithColumns(30));
14634 verifyFormat("vector<int> aaaa = {\n"
14635 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14636 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14637 " aaaaaa.aaaaaaa,\n"
14638 " aaaaaa.aaaaaaa,\n"
14639 " aaaaaa.aaaaaaa,\n"
14640 " aaaaaa.aaaaaaa,\n"
14641 "};");
14642
14643 // Don't create hanging lists.
14644 verifyFormat("someFunction(Param, {List1, List2,\n"
14645 " List3});",
14646 getLLVMStyleWithColumns(35));
14647 verifyFormat("someFunction(Param, Param,\n"
14648 " {List1, List2,\n"
14649 " List3});",
14650 getLLVMStyleWithColumns(35));
14651 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14652 " aaaaaaaaaaaaaaaaaaaaaaa);");
14653
14654 // No possible column formats, don't want the optimal paths penalized.
14655 verifyFormat(
14656 "waarudo::unit desk = {\n"
14657 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14658 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14659 "Output{1, 2}; },\n"
14660 " [](const Input &i) -> Output { return "
14661 "Output{1, 2}; });");
14662 FormatStyle NoBinPacking = getLLVMStyle();
14663 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
14664 verifyFormat("waarudo::unit desk = {\n"
14665 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14666 "1, 1} * w::m; }};",
14667 NoBinPacking);
14668 }
14669
14670 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14671 FormatStyle DoNotMerge = getLLVMStyle();
14672 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14673
14674 verifyFormat("void f() { return 42; }");
14675 verifyFormat("void f() {\n"
14676 " return 42;\n"
14677 "}",
14678 DoNotMerge);
14679 verifyFormat("void f() {\n"
14680 " // Comment\n"
14681 "}");
14682 verifyFormat("{\n"
14683 "#error {\n"
14684 " int a;\n"
14685 "}");
14686 verifyFormat("{\n"
14687 " int a;\n"
14688 "#error {\n"
14689 "}");
14690 verifyFormat("void f() {} // comment");
14691 verifyFormat("void f() { int a; } // comment");
14692 verifyFormat("void f() {\n"
14693 "} // comment",
14694 DoNotMerge);
14695 verifyFormat("void f() {\n"
14696 " int a;\n"
14697 "} // comment",
14698 DoNotMerge);
14699 verifyFormat("void f() {\n"
14700 "} // comment",
14701 getLLVMStyleWithColumns(15));
14702
14703 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14704 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14705
14706 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14707 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14708 verifyGoogleFormat("class C {\n"
14709 " C()\n"
14710 " : iiiiiiii(nullptr),\n"
14711 " kkkkkkk(nullptr),\n"
14712 " mmmmmmm(nullptr),\n"
14713 " nnnnnnn(nullptr) {}\n"
14714 "};");
14715
14716 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
14717 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
14718 verifyFormat("class C {\n"
14719 " A() : b(0) {}\n"
14720 "};",
14721 "class C{A():b(0){}};", NoColumnLimit);
14722 verifyFormat("A()\n"
14723 " : b(0) {\n"
14724 "}",
14725 "A()\n:b(0)\n{\n}", NoColumnLimit);
14726
14727 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
14728 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
14729 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
14730 verifyFormat("class C {\n"
14731 "#pragma foo\n"
14732 " int foo { return 0; }\n"
14733 "};",
14734 NoColumnLimitWrapAfterFunction);
14735 verifyFormat("class C {\n"
14736 "#pragma foo\n"
14737 " void foo {}\n"
14738 "};",
14739 NoColumnLimitWrapAfterFunction);
14740
14741 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
14742 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
14743 FormatStyle::SFS_None;
14744 verifyFormat("A() : b(0) {\n"
14745 "}",
14746 DoNotMergeNoColumnLimit);
14747 verifyNoChange("A()\n"
14748 " : b(0) {\n"
14749 "}",
14750 DoNotMergeNoColumnLimit);
14751 verifyFormat("A()\n"
14752 " : b(0) {\n"
14753 "}",
14754 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
14755
14756 verifyFormat("#define A \\\n"
14757 " void f() { \\\n"
14758 " int i; \\\n"
14759 " }",
14760 getLLVMStyleWithColumns(20));
14761 verifyFormat("#define A \\\n"
14762 " void f() { int i; }",
14763 getLLVMStyleWithColumns(21));
14764 verifyFormat("#define A \\\n"
14765 " void f() { \\\n"
14766 " int i; \\\n"
14767 " } \\\n"
14768 " int j;",
14769 getLLVMStyleWithColumns(22));
14770 verifyFormat("#define A \\\n"
14771 " void f() { int i; } \\\n"
14772 " int j;",
14773 getLLVMStyleWithColumns(23));
14774
14775 verifyFormat(
14776 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14777 " aaaaaaaaaaaaaaaaaa,\n"
14778 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14779
14780 constexpr StringRef Code("void foo() { /* Empty */ }");
14781 verifyFormat(Code);
14782 verifyFormat(Code, "void foo() { /* Empty */\n"
14783 "}");
14784 verifyFormat(Code, "void foo() {\n"
14785 "/* Empty */\n"
14786 "}");
14787 }
14788
14789 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
14790 FormatStyle MergeEmptyOnly = getLLVMStyle();
14791 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14792 verifyFormat("class C {\n"
14793 " int f() {}\n"
14794 "};",
14795 MergeEmptyOnly);
14796 verifyFormat("class C {\n"
14797 " int f() {\n"
14798 " return 42;\n"
14799 " }\n"
14800 "};",
14801 MergeEmptyOnly);
14802 verifyFormat("int f() {}", MergeEmptyOnly);
14803 verifyFormat("int f() {\n"
14804 " return 42;\n"
14805 "}",
14806 MergeEmptyOnly);
14807
14808 // Also verify behavior when BraceWrapping.AfterFunction = true
14809 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14810 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
14811 verifyFormat("int f() {}", MergeEmptyOnly);
14812 verifyFormat("class C {\n"
14813 " int f() {}\n"
14814 "};",
14815 MergeEmptyOnly);
14816 }
14817
14818 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
14819 FormatStyle MergeInlineOnly = getLLVMStyle();
14820 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14821 verifyFormat("class C {\n"
14822 " int f() { return 42; }\n"
14823 "};",
14824 MergeInlineOnly);
14825 verifyFormat("int f() {\n"
14826 " return 42;\n"
14827 "}",
14828 MergeInlineOnly);
14829
14830 // SFS_Inline implies SFS_Empty
14831 verifyFormat("class C {\n"
14832 " int f() {}\n"
14833 "};",
14834 MergeInlineOnly);
14835 verifyFormat("int f() {}", MergeInlineOnly);
14836 // https://llvm.org/PR54147
14837 verifyFormat("auto lambda = []() {\n"
14838 " // comment\n"
14839 " f();\n"
14840 " g();\n"
14841 "};",
14842 MergeInlineOnly);
14843
14844 verifyFormat("class C {\n"
14845 "#ifdef A\n"
14846 " int f() { return 42; }\n"
14847 "#endif\n"
14848 "};",
14849 MergeInlineOnly);
14850
14851 verifyFormat("struct S {\n"
14852 "// comment\n"
14853 "#ifdef FOO\n"
14854 " int foo() { bar(); }\n"
14855 "#endif\n"
14856 "};",
14857 MergeInlineOnly);
14858
14859 MergeInlineOnly.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14860 verifyFormat("#define Foo \\\n"
14861 " struct S { \\\n"
14862 " void foo() { return; } \\\n"
14863 " }",
14864 MergeInlineOnly);
14865
14866 // Also verify behavior when BraceWrapping.AfterFunction = true
14867 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14868 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14869 verifyFormat("class C {\n"
14870 " int f() { return 42; }\n"
14871 "};",
14872 MergeInlineOnly);
14873 verifyFormat("int f()\n"
14874 "{\n"
14875 " return 42;\n"
14876 "}",
14877 MergeInlineOnly);
14878
14879 // SFS_Inline implies SFS_Empty
14880 verifyFormat("int f() {}", MergeInlineOnly);
14881 verifyFormat("class C {\n"
14882 " int f() {}\n"
14883 "};",
14884 MergeInlineOnly);
14885
14886 MergeInlineOnly.BraceWrapping.AfterClass = true;
14887 MergeInlineOnly.BraceWrapping.AfterStruct = true;
14888 verifyFormat("class C\n"
14889 "{\n"
14890 " int f() { return 42; }\n"
14891 "};",
14892 MergeInlineOnly);
14893 verifyFormat("struct C\n"
14894 "{\n"
14895 " int f() { return 42; }\n"
14896 "};",
14897 MergeInlineOnly);
14898 verifyFormat("int f()\n"
14899 "{\n"
14900 " return 42;\n"
14901 "}",
14902 MergeInlineOnly);
14903 verifyFormat("int f() {}", MergeInlineOnly);
14904 verifyFormat("class C\n"
14905 "{\n"
14906 " int f() { return 42; }\n"
14907 "};",
14908 MergeInlineOnly);
14909 verifyFormat("struct C\n"
14910 "{\n"
14911 " int f() { return 42; }\n"
14912 "};",
14913 MergeInlineOnly);
14914 verifyFormat("struct C\n"
14915 "// comment\n"
14916 "/* comment */\n"
14917 "// comment\n"
14918 "{\n"
14919 " int f() { return 42; }\n"
14920 "};",
14921 MergeInlineOnly);
14922 verifyFormat("/* comment */ struct C\n"
14923 "{\n"
14924 " int f() { return 42; }\n"
14925 "};",
14926 MergeInlineOnly);
14927 }
14928
14929 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14930 FormatStyle MergeInlineOnly = getLLVMStyle();
14931 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14932 FormatStyle::SFS_InlineOnly;
14933 verifyFormat("class C {\n"
14934 " int f() { return 42; }\n"
14935 "};",
14936 MergeInlineOnly);
14937 verifyFormat("int f() {\n"
14938 " return 42;\n"
14939 "}",
14940 MergeInlineOnly);
14941
14942 // SFS_InlineOnly does not imply SFS_Empty
14943 verifyFormat("class C {\n"
14944 " int f() {}\n"
14945 "};",
14946 MergeInlineOnly);
14947 verifyFormat("int f() {\n"
14948 "}",
14949 MergeInlineOnly);
14950
14951 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
14952 verifyFormat("class Foo\n"
14953 " {\n"
14954 " void f() { foo(); }\n"
14955 " };",
14956 MergeInlineOnly);
14957
14958 // Also verify behavior when BraceWrapping.AfterFunction = true
14959 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14960 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14961 verifyFormat("class C {\n"
14962 " int f() { return 42; }\n"
14963 "};",
14964 MergeInlineOnly);
14965 verifyFormat("int f()\n"
14966 "{\n"
14967 " return 42;\n"
14968 "}",
14969 MergeInlineOnly);
14970
14971 // SFS_InlineOnly does not imply SFS_Empty
14972 verifyFormat("int f()\n"
14973 "{\n"
14974 "}",
14975 MergeInlineOnly);
14976 verifyFormat("class C {\n"
14977 " int f() {}\n"
14978 "};",
14979 MergeInlineOnly);
14980 }
14981
14982 TEST_F(FormatTest, SplitEmptyFunction) {
14983 FormatStyle Style = getLLVMStyleWithColumns(40);
14984 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14985 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14986 Style.BraceWrapping.AfterFunction = true;
14987 Style.BraceWrapping.SplitEmptyFunction = false;
14988
14989 verifyFormat("int f()\n"
14990 "{}",
14991 Style);
14992 verifyFormat("int f()\n"
14993 "{\n"
14994 " return 42;\n"
14995 "}",
14996 Style);
14997 verifyFormat("int f()\n"
14998 "{\n"
14999 " // some comment\n"
15000 "}",
15001 Style);
15002
15003 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
15004 verifyFormat("int f() {}", Style);
15005 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15006 "{}",
15007 Style);
15008 verifyFormat("int f()\n"
15009 "{\n"
15010 " return 0;\n"
15011 "}",
15012 Style);
15013
15014 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
15015 verifyFormat("class Foo {\n"
15016 " int f() {}\n"
15017 "};",
15018 Style);
15019 verifyFormat("class Foo {\n"
15020 " int f() { return 0; }\n"
15021 "};",
15022 Style);
15023 verifyFormat("class Foo {\n"
15024 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15025 " {}\n"
15026 "};",
15027 Style);
15028 verifyFormat("class Foo {\n"
15029 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15030 " {\n"
15031 " return 0;\n"
15032 " }\n"
15033 "};",
15034 Style);
15035
15036 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
15037 verifyFormat("int f() {}", Style);
15038 verifyFormat("int f() { return 0; }", Style);
15039 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15040 "{}",
15041 Style);
15042 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15043 "{\n"
15044 " return 0;\n"
15045 "}",
15046 Style);
15047 }
15048
15049 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
15050 FormatStyle Style = getLLVMStyleWithColumns(40);
15051 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
15052 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15053 Style.BraceWrapping.AfterFunction = true;
15054 Style.BraceWrapping.SplitEmptyFunction = true;
15055 Style.BraceWrapping.SplitEmptyRecord = false;
15056
15057 verifyFormat("class C {};", Style);
15058 verifyFormat("struct C {};", Style);
15059 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
15060 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
15061 "{\n"
15062 "}",
15063 Style);
15064 verifyFormat("class C {\n"
15065 " C()\n"
15066 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
15067 " bbbbbbbbbbbbbbbbbbb()\n"
15068 " {\n"
15069 " }\n"
15070 " void\n"
15071 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
15072 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
15073 " {\n"
15074 " }\n"
15075 "};",
15076 Style);
15077 }
15078
15079 TEST_F(FormatTest, MergeShortFunctionBody) {
15080 auto Style = getLLVMStyle();
15081 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
15082 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
15083 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15084 Style.BraceWrapping.AfterFunction = true;
15085
15086 verifyFormat("int foo()\n"
15087 "{ return 1; }",
15088 Style);
15089 }
15090
15091 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
15092 FormatStyle Style = getLLVMStyle();
15093 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
15094 verifyFormat("#ifdef A\n"
15095 "int f() {}\n"
15096 "#else\n"
15097 "int g() {}\n"
15098 "#endif",
15099 Style);
15100 }
15101
15102 TEST_F(FormatTest, SplitEmptyClass) {
15103 FormatStyle Style = getLLVMStyle();
15104 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15105 Style.BraceWrapping.AfterClass = true;
15106 Style.BraceWrapping.SplitEmptyRecord = false;
15107
15108 verifyFormat("class Foo\n"
15109 "{};",
15110 Style);
15111 verifyFormat("/* something */ class Foo\n"
15112 "{};",
15113 Style);
15114 verifyFormat("template <typename X> class Foo\n"
15115 "{};",
15116 Style);
15117 verifyFormat("class Foo\n"
15118 "{\n"
15119 " Foo();\n"
15120 "};",
15121 Style);
15122 verifyFormat("typedef class Foo\n"
15123 "{\n"
15124 "} Foo_t;",
15125 Style);
15126
15127 Style.BraceWrapping.SplitEmptyRecord = true;
15128 Style.BraceWrapping.AfterStruct = true;
15129 verifyFormat("class rep\n"
15130 "{\n"
15131 "};",
15132 Style);
15133 verifyFormat("struct rep\n"
15134 "{\n"
15135 "};",
15136 Style);
15137 verifyFormat("template <typename T> class rep\n"
15138 "{\n"
15139 "};",
15140 Style);
15141 verifyFormat("template <typename T> struct rep\n"
15142 "{\n"
15143 "};",
15144 Style);
15145 verifyFormat("class rep\n"
15146 "{\n"
15147 " int x;\n"
15148 "};",
15149 Style);
15150 verifyFormat("struct rep\n"
15151 "{\n"
15152 " int x;\n"
15153 "};",
15154 Style);
15155 verifyFormat("template <typename T> class rep\n"
15156 "{\n"
15157 " int x;\n"
15158 "};",
15159 Style);
15160 verifyFormat("template <typename T> struct rep\n"
15161 "{\n"
15162 " int x;\n"
15163 "};",
15164 Style);
15165 verifyFormat("template <typename T> class rep // Foo\n"
15166 "{\n"
15167 " int x;\n"
15168 "};",
15169 Style);
15170 verifyFormat("template <typename T> struct rep // Bar\n"
15171 "{\n"
15172 " int x;\n"
15173 "};",
15174 Style);
15175
15176 verifyFormat("template <typename T> class rep<T>\n"
15177 "{\n"
15178 " int x;\n"
15179 "};",
15180 Style);
15181
15182 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
15183 "{\n"
15184 " int x;\n"
15185 "};",
15186 Style);
15187 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
15188 "{\n"
15189 "};",
15190 Style);
15191
15192 verifyFormat("#include \"stdint.h\"\n"
15193 "namespace rep {}",
15194 Style);
15195 verifyFormat("#include <stdint.h>\n"
15196 "namespace rep {}",
15197 Style);
15198 verifyFormat("#include <stdint.h>\n"
15199 "namespace rep {}",
15200 "#include <stdint.h>\n"
15201 "namespace rep {\n"
15202 "\n"
15203 "\n"
15204 "}",
15205 Style);
15206 }
15207
15208 TEST_F(FormatTest, SplitEmptyStruct) {
15209 FormatStyle Style = getLLVMStyle();
15210 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15211 Style.BraceWrapping.AfterStruct = true;
15212 Style.BraceWrapping.SplitEmptyRecord = false;
15213
15214 verifyFormat("struct Foo\n"
15215 "{};",
15216 Style);
15217 verifyFormat("/* something */ struct Foo\n"
15218 "{};",
15219 Style);
15220 verifyFormat("template <typename X> struct Foo\n"
15221 "{};",
15222 Style);
15223 verifyFormat("struct Foo\n"
15224 "{\n"
15225 " Foo();\n"
15226 "};",
15227 Style);
15228 verifyFormat("typedef struct Foo\n"
15229 "{\n"
15230 "} Foo_t;",
15231 Style);
15232 // typedef struct Bar {} Bar_t;
15233 }
15234
15235 TEST_F(FormatTest, SplitEmptyUnion) {
15236 FormatStyle Style = getLLVMStyle();
15237 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15238 Style.BraceWrapping.AfterUnion = true;
15239 Style.BraceWrapping.SplitEmptyRecord = false;
15240
15241 verifyFormat("union Foo\n"
15242 "{};",
15243 Style);
15244 verifyFormat("/* something */ union Foo\n"
15245 "{};",
15246 Style);
15247 verifyFormat("union Foo\n"
15248 "{\n"
15249 " A,\n"
15250 "};",
15251 Style);
15252 verifyFormat("typedef union Foo\n"
15253 "{\n"
15254 "} Foo_t;",
15255 Style);
15256 }
15257
15258 TEST_F(FormatTest, SplitEmptyNamespace) {
15259 FormatStyle Style = getLLVMStyle();
15260 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15261 Style.BraceWrapping.AfterNamespace = true;
15262 Style.BraceWrapping.SplitEmptyNamespace = false;
15263
15264 verifyFormat("namespace Foo\n"
15265 "{};",
15266 Style);
15267 verifyFormat("/* something */ namespace Foo\n"
15268 "{};",
15269 Style);
15270 verifyFormat("inline namespace Foo\n"
15271 "{};",
15272 Style);
15273 verifyFormat("/* something */ inline namespace Foo\n"
15274 "{};",
15275 Style);
15276 verifyFormat("export namespace Foo\n"
15277 "{};",
15278 Style);
15279 verifyFormat("namespace Foo\n"
15280 "{\n"
15281 "void Bar();\n"
15282 "};",
15283 Style);
15284 }
15285
15286 TEST_F(FormatTest, NeverMergeShortRecords) {
15287 FormatStyle Style = getLLVMStyle();
15288
15289 verifyFormat("class Foo {\n"
15290 " Foo();\n"
15291 "};",
15292 Style);
15293 verifyFormat("typedef class Foo {\n"
15294 " Foo();\n"
15295 "} Foo_t;",
15296 Style);
15297 verifyFormat("struct Foo {\n"
15298 " Foo();\n"
15299 "};",
15300 Style);
15301 verifyFormat("typedef struct Foo {\n"
15302 " Foo();\n"
15303 "} Foo_t;",
15304 Style);
15305 verifyFormat("union Foo {\n"
15306 " A,\n"
15307 "};",
15308 Style);
15309 verifyFormat("typedef union Foo {\n"
15310 " A,\n"
15311 "} Foo_t;",
15312 Style);
15313 verifyFormat("namespace Foo {\n"
15314 "void Bar();\n"
15315 "};",
15316 Style);
15317
15318 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15319 Style.BraceWrapping.AfterClass = true;
15320 Style.BraceWrapping.AfterStruct = true;
15321 Style.BraceWrapping.AfterUnion = true;
15322 Style.BraceWrapping.AfterNamespace = true;
15323 verifyFormat("class Foo\n"
15324 "{\n"
15325 " Foo();\n"
15326 "};",
15327 Style);
15328 verifyFormat("typedef class Foo\n"
15329 "{\n"
15330 " Foo();\n"
15331 "} Foo_t;",
15332 Style);
15333 verifyFormat("struct Foo\n"
15334 "{\n"
15335 " Foo();\n"
15336 "};",
15337 Style);
15338 verifyFormat("typedef struct Foo\n"
15339 "{\n"
15340 " Foo();\n"
15341 "} Foo_t;",
15342 Style);
15343 verifyFormat("union Foo\n"
15344 "{\n"
15345 " A,\n"
15346 "};",
15347 Style);
15348 verifyFormat("typedef union Foo\n"
15349 "{\n"
15350 " A,\n"
15351 "} Foo_t;",
15352 Style);
15353 verifyFormat("namespace Foo\n"
15354 "{\n"
15355 "void Bar();\n"
15356 "};",
15357 Style);
15358 }
15359
15360 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
15361 // Elaborate type variable declarations.
15362 verifyFormat("struct foo a = {bar};\nint n;");
15363 verifyFormat("class foo a = {bar};\nint n;");
15364 verifyFormat("union foo a = {bar};\nint n;");
15365
15366 // Elaborate types inside function definitions.
15367 verifyFormat("struct foo f() {}\nint n;");
15368 verifyFormat("class foo f() {}\nint n;");
15369 verifyFormat("union foo f() {}\nint n;");
15370
15371 // Templates.
15372 verifyFormat("template <class X> void f() {}\nint n;");
15373 verifyFormat("template <struct X> void f() {}\nint n;");
15374 verifyFormat("template <union X> void f() {}\nint n;");
15375
15376 // Actual definitions...
15377 verifyFormat("struct {\n} n;");
15378 verifyFormat(
15379 "template <template <class T, class Y>, class Z> class X {\n} n;");
15380 verifyFormat("union Z {\n int n;\n} x;");
15381 verifyFormat("class MACRO Z {\n} n;");
15382 verifyFormat("class MACRO(X) Z {\n} n;");
15383 verifyFormat("class __attribute__((X)) Z {\n} n;");
15384 verifyFormat("class __declspec(X) Z {\n} n;");
15385 verifyFormat("class A##B##C {\n} n;");
15386 verifyFormat("class alignas(16) Z {\n} n;");
15387 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15388 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15389
15390 // Redefinition from nested context:
15391 verifyFormat("class A::B::C {\n} n;");
15392
15393 // Template definitions.
15394 verifyFormat(
15395 "template <typename F>\n"
15396 "Matcher(const Matcher<F> &Other,\n"
15397 " typename enable_if_c<is_base_of<F, T>::value &&\n"
15398 " !is_same<F, T>::value>::type * = 0)\n"
15399 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15400
15401 // FIXME: This is still incorrectly handled at the formatter side.
15402 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15403 verifyFormat("int i = SomeFunction(a<b, a> b);");
15404
15405 verifyFormat("class A<int> f() {}\n"
15406 "int n;");
15407 verifyFormat("template <typename T> class A<T> f() {}\n"
15408 "int n;");
15409
15410 verifyFormat("template <> class Foo<int> F() {\n"
15411 "} n;");
15412
15413 // Elaborate types where incorrectly parsing the structural element would
15414 // break the indent.
15415 verifyFormat("if (true)\n"
15416 " class X x;\n"
15417 "else\n"
15418 " f();");
15419
15420 // This is simply incomplete. Formatting is not important, but must not crash.
15421 verifyFormat("class A:");
15422 }
15423
15424 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
15425 verifyNoChange("#error Leave all white!!!!! space* alone!");
15426 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15427 verifyFormat("#error 1", " # error 1");
15428 verifyFormat("#warning 1", " # warning 1");
15429 }
15430
15431 TEST_F(FormatTest, FormatHashIfExpressions) {
15432 verifyFormat("#if AAAA && BBBB");
15433 verifyFormat("#if (AAAA && BBBB)");
15434 verifyFormat("#elif (AAAA && BBBB)");
15435 // FIXME: Come up with a better indentation for #elif.
15436 verifyFormat(
15437 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15438 " defined(BBBBBBBB)\n"
15439 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15440 " defined(BBBBBBBB)\n"
15441 "#endif",
15442 getLLVMStyleWithColumns(65));
15443 }
15444
15445 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
15446 FormatStyle AllowsMergedIf = getGoogleStyle();
15447 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
15448 FormatStyle::SIS_WithoutElse;
15449 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
15450 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
15451 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
15452 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
15453 FormatStyle ShortMergedIf = AllowsMergedIf;
15454 ShortMergedIf.ColumnLimit = 25;
15455 verifyFormat("#define A \\\n"
15456 " if (true) return 42;",
15457 ShortMergedIf);
15458 verifyFormat("#define A \\\n"
15459 " f(); \\\n"
15460 " if (true)\n"
15461 "#define B",
15462 ShortMergedIf);
15463 verifyFormat("#define A \\\n"
15464 " f(); \\\n"
15465 " if (true)\n"
15466 "g();",
15467 ShortMergedIf);
15468 verifyFormat("{\n"
15469 "#ifdef A\n"
15470 " // Comment\n"
15471 " if (true) continue;\n"
15472 "#endif\n"
15473 " // Comment\n"
15474 " if (true) continue;\n"
15475 "}",
15476 ShortMergedIf);
15477 ShortMergedIf.ColumnLimit = 33;
15478 verifyFormat("#define A \\\n"
15479 " if constexpr (true) return 42;",
15480 ShortMergedIf);
15481 verifyFormat("#define A \\\n"
15482 " if CONSTEXPR (true) return 42;",
15483 ShortMergedIf);
15484 ShortMergedIf.ColumnLimit = 29;
15485 verifyFormat("#define A \\\n"
15486 " if (aaaaaaaaaa) return 1; \\\n"
15487 " return 2;",
15488 ShortMergedIf);
15489 ShortMergedIf.ColumnLimit = 28;
15490 verifyFormat("#define A \\\n"
15491 " if (aaaaaaaaaa) \\\n"
15492 " return 1; \\\n"
15493 " return 2;",
15494 ShortMergedIf);
15495 verifyFormat("#define A \\\n"
15496 " if constexpr (aaaaaaa) \\\n"
15497 " return 1; \\\n"
15498 " return 2;",
15499 ShortMergedIf);
15500 verifyFormat("#define A \\\n"
15501 " if CONSTEXPR (aaaaaaa) \\\n"
15502 " return 1; \\\n"
15503 " return 2;",
15504 ShortMergedIf);
15505
15506 verifyFormat("//\n"
15507 "#define a \\\n"
15508 " if \\\n"
15509 " 0",
15510 getChromiumStyle(FormatStyle::LK_Cpp));
15511 }
15512
15513 TEST_F(FormatTest, FormatStarDependingOnContext) {
15514 verifyFormat("void f(int *a);");
15515 verifyFormat("void f() { f(fint * b); }");
15516 verifyFormat("class A {\n void f(int *a);\n};");
15517 verifyFormat("class A {\n int *a;\n};");
15518 verifyFormat("namespace a {\n"
15519 "namespace b {\n"
15520 "class A {\n"
15521 " void f() {}\n"
15522 " int *a;\n"
15523 "};\n"
15524 "} // namespace b\n"
15525 "} // namespace a");
15526 }
15527
15528 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15529 verifyFormat("while");
15530 verifyFormat("operator");
15531 }
15532
15533 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15534 // This code would be painfully slow to format if we didn't skip it.
15535 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
15536 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15537 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15538 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15539 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15540 "A(1, 1)\n"
15541 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15542 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15543 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15544 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15545 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15546 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15547 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15548 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15549 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15550 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15551 // Deeply nested part is untouched, rest is formatted.
15552 EXPECT_EQ(std::string("int i;") + Code + "int j;",
15553 format(std::string("int i;") + Code + "int j;",
15554 getLLVMStyle(), SC_ExpectIncomplete));
15555 }
15556
15557 //===----------------------------------------------------------------------===//
15558 // Objective-C tests.
15559 //===----------------------------------------------------------------------===//
15560
15561 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15562 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15563 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15564 "-(NSUInteger)indexOfObject:(id)anObject;");
15565 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15566 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15567 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15568 "-(NSInteger)Method3:(id)anObject;");
15569 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15570 "-(NSInteger)Method4:(id)anObject;");
15571 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15572 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15573 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15574 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15575 "forAllCells:(BOOL)flag;");
15576
15577 // Very long objectiveC method declaration.
15578 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15579 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15580 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15581 " inRange:(NSRange)range\n"
15582 " outRange:(NSRange)out_range\n"
15583 " outRange1:(NSRange)out_range1\n"
15584 " outRange2:(NSRange)out_range2\n"
15585 " outRange3:(NSRange)out_range3\n"
15586 " outRange4:(NSRange)out_range4\n"
15587 " outRange5:(NSRange)out_range5\n"
15588 " outRange6:(NSRange)out_range6\n"
15589 " outRange7:(NSRange)out_range7\n"
15590 " outRange8:(NSRange)out_range8\n"
15591 " outRange9:(NSRange)out_range9;");
15592
15593 // When the function name has to be wrapped.
15594 FormatStyle Style = getLLVMStyle();
15595 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15596 // and always indents instead.
15597 Style.IndentWrappedFunctionNames = false;
15598 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15599 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15600 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15601 "}",
15602 Style);
15603 Style.IndentWrappedFunctionNames = true;
15604 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15605 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15606 " anotherName:(NSString)dddddddddddddd {\n"
15607 "}",
15608 Style);
15609
15610 verifyFormat("- (int)sum:(vector<int>)numbers;");
15611 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15612 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15613 // protocol lists (but not for template classes):
15614 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15615
15616 verifyFormat("- (int (*)())foo:(int (*)())f;");
15617 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15618
15619 // If there's no return type (very rare in practice!), LLVM and Google style
15620 // agree.
15621 verifyFormat("- foo;");
15622 verifyFormat("- foo:(int)f;");
15623 verifyGoogleFormat("- foo:(int)foo;");
15624 }
15625
15626 TEST_F(FormatTest, BreaksStringLiterals) {
15627 // FIXME: unstable test case
15628 EXPECT_EQ("\"some text \"\n"
15629 "\"other\";",
15630 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15631 // FIXME: unstable test case
15632 EXPECT_EQ("\"some text \"\n"
15633 "\"other\";",
15634 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15635 verifyFormat("#define A \\\n"
15636 " \"some \" \\\n"
15637 " \"text \" \\\n"
15638 " \"other\";",
15639 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15640 verifyFormat("#define A \\\n"
15641 " \"so \" \\\n"
15642 " \"text \" \\\n"
15643 " \"other\";",
15644 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15645
15646 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15647 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15648 // FIXME: unstable test case
15649 EXPECT_EQ("\"some \"\n"
15650 "\"text\"",
15651 format("\"some text\"", getLLVMStyleWithColumns(10)));
15652 // FIXME: unstable test case
15653 EXPECT_EQ("\"some \"\n"
15654 "\"text\"",
15655 format("\"some text\"", getLLVMStyleWithColumns(7)));
15656 // FIXME: unstable test case
15657 EXPECT_EQ("\"some\"\n"
15658 "\" tex\"\n"
15659 "\"t\"",
15660 format("\"some text\"", getLLVMStyleWithColumns(6)));
15661 // FIXME: unstable test case
15662 EXPECT_EQ("\"some\"\n"
15663 "\" tex\"\n"
15664 "\" and\"",
15665 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15666 // FIXME: unstable test case
15667 EXPECT_EQ("\"some\"\n"
15668 "\"/tex\"\n"
15669 "\"/and\"",
15670 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15671
15672 verifyFormat("variable =\n"
15673 " \"long string \"\n"
15674 " \"literal\";",
15675 "variable = \"long string literal\";",
15676 getLLVMStyleWithColumns(20));
15677
15678 verifyFormat("variable = f(\n"
15679 " \"long string \"\n"
15680 " \"literal\",\n"
15681 " short,\n"
15682 " loooooooooooooooooooong);",
15683 "variable = f(\"long string literal\", short, "
15684 "loooooooooooooooooooong);",
15685 getLLVMStyleWithColumns(20));
15686
15687 verifyFormat("f(g(\"long string \"\n"
15688 " \"literal\"),\n"
15689 " b);",
15690 "f(g(\"long string literal\"), b);",
15691 getLLVMStyleWithColumns(20));
15692 verifyFormat("f(g(\"long string \"\n"
15693 " \"literal\",\n"
15694 " a),\n"
15695 " b);",
15696 "f(g(\"long string literal\", a), b);",
15697 getLLVMStyleWithColumns(20));
15698 verifyFormat("f(\"one two\".split(\n"
15699 " variable));",
15700 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15701 verifyFormat("f(\"one two three four five six \"\n"
15702 " \"seven\".split(\n"
15703 " really_looooong_variable));",
15704 "f(\"one two three four five six seven\"."
15705 "split(really_looooong_variable));",
15706 getLLVMStyleWithColumns(33));
15707
15708 verifyFormat("f(\"some \"\n"
15709 " \"text\",\n"
15710 " other);",
15711 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15712
15713 // Only break as a last resort.
15714 verifyFormat(
15715 "aaaaaaaaaaaaaaaaaaaa(\n"
15716 " aaaaaaaaaaaaaaaaaaaa,\n"
15717 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15718
15719 // FIXME: unstable test case
15720 EXPECT_EQ("\"splitmea\"\n"
15721 "\"trandomp\"\n"
15722 "\"oint\"",
15723 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15724
15725 // FIXME: unstable test case
15726 EXPECT_EQ("\"split/\"\n"
15727 "\"pathat/\"\n"
15728 "\"slashes\"",
15729 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15730
15731 // FIXME: unstable test case
15732 EXPECT_EQ("\"split/\"\n"
15733 "\"pathat/\"\n"
15734 "\"slashes\"",
15735 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15736 // FIXME: unstable test case
15737 EXPECT_EQ("\"split at \"\n"
15738 "\"spaces/at/\"\n"
15739 "\"slashes.at.any$\"\n"
15740 "\"non-alphanumeric%\"\n"
15741 "\"1111111111characte\"\n"
15742 "\"rs\"",
15743 format("\"split at "
15744 "spaces/at/"
15745 "slashes.at."
15746 "any$non-"
15747 "alphanumeric%"
15748 "1111111111characte"
15749 "rs\"",
15750 getLLVMStyleWithColumns(20)));
15751
15752 // Verify that splitting the strings understands
15753 // Style::AlwaysBreakBeforeMultilineStrings.
15754 verifyFormat("aaaaaaaaaaaa(\n"
15755 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15756 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15757 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15758 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15759 "aaaaaaaaaaaaaaaaaaaaaa\");",
15760 getGoogleStyle());
15761 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15762 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15763 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15764 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15765 "aaaaaaaaaaaaaaaaaaaaaa\";",
15766 getGoogleStyle());
15767 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15768 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15769 "llvm::outs() << "
15770 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15771 "aaaaaaaaaaaaaaaaaaa\";");
15772 verifyFormat("ffff(\n"
15773 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15774 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15775 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15776 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15777 getGoogleStyle());
15778
15779 FormatStyle Style = getLLVMStyleWithColumns(12);
15780 Style.BreakStringLiterals = false;
15781 verifyFormat("\"some text other\";", Style);
15782
15783 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
15784 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15785 verifyFormat("#define A \\\n"
15786 " \"some \" \\\n"
15787 " \"text \" \\\n"
15788 " \"other\";",
15789 "#define A \"some text other\";", AlignLeft);
15790 }
15791
15792 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
15793 verifyFormat("C a = \"some more \"\n"
15794 " \"text\";",
15795 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15796 }
15797
15798 TEST_F(FormatTest, FullyRemoveEmptyLines) {
15799 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
15800 NoEmptyLines.MaxEmptyLinesToKeep = 0;
15801 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15802 NoEmptyLines);
15803 }
15804
15805 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
15806 // FIXME: unstable test case
15807 EXPECT_EQ(
15808 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15809 "(\n"
15810 " \"x\t\");",
15811 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15812 "aaaaaaa("
15813 "\"x\t\");"));
15814 }
15815
15816 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
15817 // FIXME: unstable test case
15818 EXPECT_EQ(
15819 "u8\"utf8 string \"\n"
15820 "u8\"literal\";",
15821 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15822 // FIXME: unstable test case
15823 EXPECT_EQ(
15824 "u\"utf16 string \"\n"
15825 "u\"literal\";",
15826 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15827 // FIXME: unstable test case
15828 EXPECT_EQ(
15829 "U\"utf32 string \"\n"
15830 "U\"literal\";",
15831 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15832 // FIXME: unstable test case
15833 EXPECT_EQ("L\"wide string \"\n"
15834 "L\"literal\";",
15835 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15836 verifyFormat("@\"NSString \"\n"
15837 "@\"literal\";",
15838 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15839 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
15840
15841 // This input makes clang-format try to split the incomplete unicode escape
15842 // sequence, which used to lead to a crasher.
15843 verifyNoCrash(
15844 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15845 getLLVMStyleWithColumns(60));
15846 }
15847
15848 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
15849 FormatStyle Style = getGoogleStyleWithColumns(15);
15850 verifyFormat("R\"x(raw literal)x\";", Style);
15851 verifyFormat("uR\"x(raw literal)x\";", Style);
15852 verifyFormat("LR\"x(raw literal)x\";", Style);
15853 verifyFormat("UR\"x(raw literal)x\";", Style);
15854 verifyFormat("u8R\"x(raw literal)x\";", Style);
15855 }
15856
15857 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
15858 FormatStyle Style = getLLVMStyleWithColumns(20);
15859 // FIXME: unstable test case
15860 EXPECT_EQ(
15861 "_T(\"aaaaaaaaaaaaaa\")\n"
15862 "_T(\"aaaaaaaaaaaaaa\")\n"
15863 "_T(\"aaaaaaaaaaaa\")",
15864 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
15865 verifyFormat("f(x,\n"
15866 " _T(\"aaaaaaaaaaaa\")\n"
15867 " _T(\"aaa\"),\n"
15868 " z);",
15869 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
15870
15871 // FIXME: Handle embedded spaces in one iteration.
15872 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15873 // "_T(\"aaaaaaaaaaaaa\")\n"
15874 // "_T(\"aaaaaaaaaaaaa\")\n"
15875 // "_T(\"a\")",
15876 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15877 // getLLVMStyleWithColumns(20)));
15878 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15879 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
15880 verifyFormat("f(\n"
15881 "#if !TEST\n"
15882 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15883 "#endif\n"
15884 ");",
15885 "f(\n"
15886 "#if !TEST\n"
15887 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15888 "#endif\n"
15889 ");");
15890 verifyFormat("f(\n"
15891 "\n"
15892 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15893 "f(\n"
15894 "\n"
15895 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15896 // Regression test for accessing tokens past the end of a vector in the
15897 // TokenLexer.
15898 verifyNoCrash(R"(_T(
15899 "
15900 )
15901 )");
15902 }
15903
15904 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15905 // In a function call with two operands, the second can be broken with no line
15906 // break before it.
15907 verifyFormat("func(a, \"long long \"\n"
15908 " \"long long\");",
15909 "func(a, \"long long long long\");",
15910 getLLVMStyleWithColumns(24));
15911 // In a function call with three operands, the second must be broken with a
15912 // line break before it.
15913 verifyFormat("func(a,\n"
15914 " \"long long long \"\n"
15915 " \"long\",\n"
15916 " c);",
15917 "func(a, \"long long long long\", c);",
15918 getLLVMStyleWithColumns(24));
15919 // In a function call with three operands, the third must be broken with a
15920 // line break before it.
15921 verifyFormat("func(a, b,\n"
15922 " \"long long long \"\n"
15923 " \"long\");",
15924 "func(a, b, \"long long long long\");",
15925 getLLVMStyleWithColumns(24));
15926 // In a function call with three operands, both the second and the third must
15927 // be broken with a line break before them.
15928 verifyFormat("func(a,\n"
15929 " \"long long long \"\n"
15930 " \"long\",\n"
15931 " \"long long long \"\n"
15932 " \"long\");",
15933 "func(a, \"long long long long\", \"long long long long\");",
15934 getLLVMStyleWithColumns(24));
15935 // In a chain of << with two operands, the second can be broken with no line
15936 // break before it.
15937 verifyFormat("a << \"line line \"\n"
15938 " \"line\";",
15939 "a << \"line line line\";", getLLVMStyleWithColumns(20));
15940 // In a chain of << with three operands, the second can be broken with no line
15941 // break before it.
15942 verifyFormat("abcde << \"line \"\n"
15943 " \"line line\"\n"
15944 " << c;",
15945 "abcde << \"line line line\" << c;",
15946 getLLVMStyleWithColumns(20));
15947 // In a chain of << with three operands, the third must be broken with a line
15948 // break before it.
15949 verifyFormat("a << b\n"
15950 " << \"line line \"\n"
15951 " \"line\";",
15952 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15953 // In a chain of << with three operands, the second can be broken with no line
15954 // break before it and the third must be broken with a line break before it.
15955 verifyFormat("abcd << \"line line \"\n"
15956 " \"line\"\n"
15957 " << \"line line \"\n"
15958 " \"line\";",
15959 "abcd << \"line line line\" << \"line line line\";",
15960 getLLVMStyleWithColumns(20));
15961 // In a chain of binary operators with two operands, the second can be broken
15962 // with no line break before it.
15963 verifyFormat("abcd + \"line line \"\n"
15964 " \"line line\";",
15965 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15966 // In a chain of binary operators with three operands, the second must be
15967 // broken with a line break before it.
15968 verifyFormat("abcd +\n"
15969 " \"line line \"\n"
15970 " \"line line\" +\n"
15971 " e;",
15972 "abcd + \"line line line line\" + e;",
15973 getLLVMStyleWithColumns(20));
15974 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15975 // the first must be broken with a line break before it.
15976 FormatStyle Style = getLLVMStyleWithColumns(25);
15977 Style.BreakAfterOpenBracketFunction = true;
15978 verifyFormat("someFunction(\n"
15979 " \"long long long \"\n"
15980 " \"long\",\n"
15981 " a);",
15982 "someFunction(\"long long long long\", a);", Style);
15983 Style.BreakAfterOpenBracketFunction = true;
15984 Style.BreakBeforeCloseBracketFunction = true;
15985 verifyFormat("someFunction(\n"
15986 " \"long long long \"\n"
15987 " \"long\",\n"
15988 " a\n"
15989 ");",
15990 Style);
15991 }
15992
15993 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15994 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15997 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15998 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
16000 }
16001
16002 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
16003 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
16004 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
16005 verifyFormat("fffffffffff(g(R\"x(\n"
16006 "multiline raw string literal xxxxxxxxxxxxxx\n"
16007 ")x\",\n"
16008 " a),\n"
16009 " b);",
16010 "fffffffffff(g(R\"x(\n"
16011 "multiline raw string literal xxxxxxxxxxxxxx\n"
16012 ")x\", a), b);",
16013 getGoogleStyleWithColumns(20));
16014 verifyFormat("fffffffffff(\n"
16015 " g(R\"x(qqq\n"
16016 "multiline raw string literal xxxxxxxxxxxxxx\n"
16017 ")x\",\n"
16018 " a),\n"
16019 " b);",
16020 "fffffffffff(g(R\"x(qqq\n"
16021 "multiline raw string literal xxxxxxxxxxxxxx\n"
16022 ")x\", a), b);",
16023 getGoogleStyleWithColumns(20));
16024
16025 verifyNoChange("fffffffffff(R\"x(\n"
16026 "multiline raw string literal xxxxxxxxxxxxxx\n"
16027 ")x\");",
16028 getGoogleStyleWithColumns(20));
16029 verifyFormat("fffffffffff(R\"x(\n"
16030 "multiline raw string literal xxxxxxxxxxxxxx\n"
16031 ")x\" + bbbbbb);",
16032 "fffffffffff(R\"x(\n"
16033 "multiline raw string literal xxxxxxxxxxxxxx\n"
16034 ")x\" + bbbbbb);",
16035 getGoogleStyleWithColumns(20));
16036 verifyFormat("fffffffffff(\n"
16037 " R\"x(\n"
16038 "multiline raw string literal xxxxxxxxxxxxxx\n"
16039 ")x\" +\n"
16040 " bbbbbb);",
16041 "fffffffffff(\n"
16042 " R\"x(\n"
16043 "multiline raw string literal xxxxxxxxxxxxxx\n"
16044 ")x\" + bbbbbb);",
16045 getGoogleStyleWithColumns(20));
16046 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
16047 "fffffffffff(\n"
16048 " R\"(single line raw string)\" + bbbbbb);");
16049 }
16050
16051 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
16052 verifyFormat("string a = \"unterminated;");
16053 verifyFormat("function(\"unterminated,\n"
16054 " OtherParameter);",
16055 "function( \"unterminated,\n"
16056 " OtherParameter);");
16057 }
16058
16059 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
16060 FormatStyle Style = getLLVMStyle();
16061 Style.Standard = FormatStyle::LS_Cpp03;
16062 verifyFormat("#define x(_a) printf(\"foo\" _a);",
16063 "#define x(_a) printf(\"foo\"_a);", Style);
16064 }
16065
16066 TEST_F(FormatTest, CppLexVersion) {
16067 FormatStyle Style = getLLVMStyle();
16068 // Formatting of x * y differs if x is a type.
16069 verifyFormat("void foo() { MACRO(a * b); }", Style);
16070 verifyFormat("void foo() { MACRO(int *b); }", Style);
16071
16072 // LLVM style uses latest lexer.
16073 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
16074 Style.Standard = FormatStyle::LS_Cpp17;
16075 // But in c++17, char8_t isn't a keyword.
16076 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
16077 }
16078
16079 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
16080
16081 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
16082 verifyFormat("someFunction(\"aaabbbcccd\"\n"
16083 " \"ddeeefff\");",
16084 "someFunction(\"aaabbbcccdddeeefff\");",
16085 getLLVMStyleWithColumns(25));
16086 verifyFormat("someFunction1234567890(\n"
16087 " \"aaabbbcccdddeeefff\");",
16088 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
16089 getLLVMStyleWithColumns(26));
16090 verifyFormat("someFunction1234567890(\n"
16091 " \"aaabbbcccdddeeeff\"\n"
16092 " \"f\");",
16093 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
16094 getLLVMStyleWithColumns(25));
16095 verifyFormat("someFunction1234567890(\n"
16096 " \"aaabbbcccdddeeeff\"\n"
16097 " \"f\");",
16098 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
16099 getLLVMStyleWithColumns(24));
16100 verifyFormat("someFunction(\n"
16101 " \"aaabbbcc ddde \"\n"
16102 " \"efff\");",
16103 "someFunction(\"aaabbbcc ddde efff\");",
16104 getLLVMStyleWithColumns(25));
16105 verifyFormat("someFunction(\"aaabbbccc \"\n"
16106 " \"ddeeefff\");",
16107 "someFunction(\"aaabbbccc ddeeefff\");",
16108 getLLVMStyleWithColumns(25));
16109 verifyFormat("someFunction1234567890(\n"
16110 " \"aaabb \"\n"
16111 " \"cccdddeeefff\");",
16112 "someFunction1234567890(\"aaabb cccdddeeefff\");",
16113 getLLVMStyleWithColumns(25));
16114 verifyFormat("#define A \\\n"
16115 " string s = \\\n"
16116 " \"123456789\" \\\n"
16117 " \"0\"; \\\n"
16118 " int i;",
16119 "#define A string s = \"1234567890\"; int i;",
16120 getLLVMStyleWithColumns(20));
16121 verifyFormat("someFunction(\n"
16122 " \"aaabbbcc \"\n"
16123 " \"dddeeefff\");",
16124 "someFunction(\"aaabbbcc dddeeefff\");",
16125 getLLVMStyleWithColumns(25));
16126 }
16127
16128 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
16129 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
16130 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
16131 // FIXME: unstable test case
16132 EXPECT_EQ("\"test\"\n"
16133 "\"\\n\"",
16134 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
16135 // FIXME: unstable test case
16136 EXPECT_EQ("\"tes\\\\\"\n"
16137 "\"n\"",
16138 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
16139 // FIXME: unstable test case
16140 EXPECT_EQ("\"\\\\\\\\\"\n"
16141 "\"\\n\"",
16142 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
16143 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
16144 // FIXME: unstable test case
16145 EXPECT_EQ("\"\\uff01\"\n"
16146 "\"test\"",
16147 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
16148 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
16149 // FIXME: unstable test case
16150 EXPECT_EQ("\"\\x000000000001\"\n"
16151 "\"next\"",
16152 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
16153 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
16154 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
16155 // FIXME: unstable test case
16156 EXPECT_EQ("\"test\"\n"
16157 "\"\\000000\"\n"
16158 "\"000001\"",
16159 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
16160 // FIXME: unstable test case
16161 EXPECT_EQ("\"test\\000\"\n"
16162 "\"00000000\"\n"
16163 "\"1\"",
16164 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
16165 }
16166
16167 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
16168 verifyFormat("void f() {\n"
16169 " return g() {}\n"
16170 " void h() {}");
16171 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
16172 "g();\n"
16173 "}");
16174 }
16175
16176 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
16177 verifyFormat(
16178 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
16179 }
16180
16181 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
16182 verifyFormat("class X {\n"
16183 " void f() {\n"
16184 " }\n"
16185 "};",
16186 getLLVMStyleWithColumns(12));
16187 }
16188
16189 TEST_F(FormatTest, ConfigurableIndentWidth) {
16190 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
16191 EightIndent.IndentWidth = 8;
16192 EightIndent.ContinuationIndentWidth = 8;
16193 verifyFormat("void f() {\n"
16194 " someFunction();\n"
16195 " if (true) {\n"
16196 " f();\n"
16197 " }\n"
16198 "}",
16199 EightIndent);
16200 verifyFormat("class X {\n"
16201 " void f() {\n"
16202 " }\n"
16203 "};",
16204 EightIndent);
16205 verifyFormat("int x[] = {\n"
16206 " call(),\n"
16207 " call()};",
16208 EightIndent);
16209 }
16210
16211 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
16212 verifyFormat("double\n"
16213 "f();",
16214 getLLVMStyleWithColumns(8));
16215 }
16216
16217 TEST_F(FormatTest, ConfigurableUseOfTab) {
16218 FormatStyle Tab = getLLVMStyleWithColumns(42);
16219 Tab.IndentWidth = 8;
16220 Tab.UseTab = FormatStyle::UT_Always;
16221 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16222
16223 verifyFormat("if (aaaaaaaa && // q\n"
16224 " bb)\t\t// w\n"
16225 "\t;",
16226 "if (aaaaaaaa &&// q\n"
16227 "bb)// w\n"
16228 ";",
16229 Tab);
16230 verifyFormat("if (aaa && bbb) // w\n"
16231 "\t;",
16232 "if(aaa&&bbb)// w\n"
16233 ";",
16234 Tab);
16235
16236 verifyFormat("class X {\n"
16237 "\tvoid f() {\n"
16238 "\t\tsomeFunction(parameter1,\n"
16239 "\t\t\t parameter2);\n"
16240 "\t}\n"
16241 "};",
16242 Tab);
16243 verifyFormat("#define A \\\n"
16244 "\tvoid f() { \\\n"
16245 "\t\tsomeFunction( \\\n"
16246 "\t\t parameter1, \\\n"
16247 "\t\t parameter2); \\\n"
16248 "\t}",
16249 Tab);
16250 verifyFormat("int a;\t // x\n"
16251 "int bbbbbbbb; // x",
16252 Tab);
16253
16254 FormatStyle TabAlignment = Tab;
16255 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
16256 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16257 verifyFormat("unsigned long long big;\n"
16258 "char*\t\t ptr;",
16259 TabAlignment);
16260 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16261 verifyFormat("unsigned long long big;\n"
16262 "char *\t\t ptr;",
16263 TabAlignment);
16264 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16265 verifyFormat("unsigned long long big;\n"
16266 "char\t\t *ptr;",
16267 TabAlignment);
16268
16269 Tab.TabWidth = 4;
16270 Tab.IndentWidth = 8;
16271 verifyFormat("class TabWidth4Indent8 {\n"
16272 "\t\tvoid f() {\n"
16273 "\t\t\t\tsomeFunction(parameter1,\n"
16274 "\t\t\t\t\t\t\t parameter2);\n"
16275 "\t\t}\n"
16276 "};",
16277 Tab);
16278
16279 Tab.TabWidth = 4;
16280 Tab.IndentWidth = 4;
16281 verifyFormat("class TabWidth4Indent4 {\n"
16282 "\tvoid f() {\n"
16283 "\t\tsomeFunction(parameter1,\n"
16284 "\t\t\t\t\t parameter2);\n"
16285 "\t}\n"
16286 "};",
16287 Tab);
16288
16289 Tab.TabWidth = 8;
16290 Tab.IndentWidth = 4;
16291 verifyFormat("class TabWidth8Indent4 {\n"
16292 " void f() {\n"
16293 "\tsomeFunction(parameter1,\n"
16294 "\t\t parameter2);\n"
16295 " }\n"
16296 "};",
16297 Tab);
16298
16299 Tab.TabWidth = 8;
16300 Tab.IndentWidth = 8;
16301 verifyFormat("/*\n"
16302 "\t a\t\tcomment\n"
16303 "\t in multiple lines\n"
16304 " */",
16305 " /*\t \t \n"
16306 " \t \t a\t\tcomment\t \t\n"
16307 " \t \t in multiple lines\t\n"
16308 " \t */",
16309 Tab);
16310
16311 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
16312 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16313 verifyFormat("void f() {\n"
16314 "\tunsigned long long big;\n"
16315 "\tchar* ptr;\n"
16316 "}",
16317 TabAlignment);
16318 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16319 verifyFormat("void f() {\n"
16320 "\tunsigned long long big;\n"
16321 "\tchar * ptr;\n"
16322 "}",
16323 TabAlignment);
16324 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16325 verifyFormat("void f() {\n"
16326 "\tunsigned long long big;\n"
16327 "\tchar *ptr;\n"
16328 "}",
16329 TabAlignment);
16330
16331 Tab.UseTab = FormatStyle::UT_ForIndentation;
16332 verifyFormat("{\n"
16333 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16334 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16335 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16336 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16337 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16338 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16339 "};",
16340 Tab);
16341 verifyFormat("enum AA {\n"
16342 "\ta1, // Force multiple lines\n"
16343 "\ta2,\n"
16344 "\ta3\n"
16345 "};",
16346 Tab);
16347 verifyFormat("if (aaaaaaaa && // q\n"
16348 " bb) // w\n"
16349 "\t;",
16350 "if (aaaaaaaa &&// q\n"
16351 "bb)// w\n"
16352 ";",
16353 Tab);
16354 verifyFormat("class X {\n"
16355 "\tvoid f() {\n"
16356 "\t\tsomeFunction(parameter1,\n"
16357 "\t\t parameter2);\n"
16358 "\t}\n"
16359 "};",
16360 Tab);
16361 verifyFormat("{\n"
16362 "\tQ(\n"
16363 "\t {\n"
16364 "\t\t int a;\n"
16365 "\t\t someFunction(aaaaaaaa,\n"
16366 "\t\t bbbbbbb);\n"
16367 "\t },\n"
16368 "\t p);\n"
16369 "}",
16370 Tab);
16371 verifyFormat("{\n"
16372 "\t/* aaaa\n"
16373 "\t bbbb */\n"
16374 "}",
16375 "{\n"
16376 "/* aaaa\n"
16377 " bbbb */\n"
16378 "}",
16379 Tab);
16380 verifyFormat("{\n"
16381 "\t/*\n"
16382 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16383 "\t bbbbbbbbbbbbb\n"
16384 "\t*/\n"
16385 "}",
16386 "{\n"
16387 "/*\n"
16388 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16389 "*/\n"
16390 "}",
16391 Tab);
16392 verifyFormat("{\n"
16393 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16394 "\t// bbbbbbbbbbbbb\n"
16395 "}",
16396 "{\n"
16397 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16398 "}",
16399 Tab);
16400 verifyFormat("{\n"
16401 "\t/*\n"
16402 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16403 "\t bbbbbbbbbbbbb\n"
16404 "\t*/\n"
16405 "}",
16406 "{\n"
16407 "\t/*\n"
16408 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16409 "\t*/\n"
16410 "}",
16411 Tab);
16412 verifyNoChange("{\n"
16413 "\t/*\n"
16414 "\n"
16415 "\t*/\n"
16416 "}",
16417 Tab);
16418 verifyNoChange("{\n"
16419 "\t/*\n"
16420 " asdf\n"
16421 "\t*/\n"
16422 "}",
16423 Tab);
16424
16425 verifyFormat("void f() {\n"
16426 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16427 "\t : bbbbbbbbbbbbbbbbbb\n"
16428 "}",
16429 Tab);
16430 FormatStyle TabNoBreak = Tab;
16431 TabNoBreak.BreakBeforeTernaryOperators = false;
16432 verifyFormat("void f() {\n"
16433 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16434 "\t bbbbbbbbbbbbbbbbbb\n"
16435 "}",
16436 TabNoBreak);
16437 verifyFormat("void f() {\n"
16438 "\treturn true ?\n"
16439 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16440 "\t bbbbbbbbbbbbbbbbbbbb\n"
16441 "}",
16442 TabNoBreak);
16443
16444 Tab.UseTab = FormatStyle::UT_Never;
16445 verifyFormat("/*\n"
16446 " a\t\tcomment\n"
16447 " in multiple lines\n"
16448 " */",
16449 " /*\t \t \n"
16450 " \t \t a\t\tcomment\t \t\n"
16451 " \t \t in multiple lines\t\n"
16452 " \t */",
16453 Tab);
16454 verifyFormat("/* some\n"
16455 " comment */",
16456 " \t \t /* some\n"
16457 " \t \t comment */",
16458 Tab);
16459 verifyFormat("int a; /* some\n"
16460 " comment */",
16461 " \t \t int a; /* some\n"
16462 " \t \t comment */",
16463 Tab);
16464
16465 verifyFormat("int a; /* some\n"
16466 "comment */",
16467 " \t \t int\ta; /* some\n"
16468 " \t \t comment */",
16469 Tab);
16470 verifyFormat("f(\"\t\t\"); /* some\n"
16471 " comment */",
16472 " \t \t f(\"\t\t\"); /* some\n"
16473 " \t \t comment */",
16474 Tab);
16475 verifyFormat("{\n"
16476 " /*\n"
16477 " * Comment\n"
16478 " */\n"
16479 " int i;\n"
16480 "}",
16481 "{\n"
16482 "\t/*\n"
16483 "\t * Comment\n"
16484 "\t */\n"
16485 "\t int i;\n"
16486 "}",
16487 Tab);
16488
16489 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16490 Tab.TabWidth = 8;
16491 Tab.IndentWidth = 8;
16492 verifyFormat("if (aaaaaaaa && // q\n"
16493 " bb) // w\n"
16494 "\t;",
16495 "if (aaaaaaaa &&// q\n"
16496 "bb)// w\n"
16497 ";",
16498 Tab);
16499 verifyFormat("if (aaa && bbb) // w\n"
16500 "\t;",
16501 "if(aaa&&bbb)// w\n"
16502 ";",
16503 Tab);
16504 verifyFormat("class X {\n"
16505 "\tvoid f() {\n"
16506 "\t\tsomeFunction(parameter1,\n"
16507 "\t\t\t parameter2);\n"
16508 "\t}\n"
16509 "};",
16510 Tab);
16511 verifyFormat("#define A \\\n"
16512 "\tvoid f() { \\\n"
16513 "\t\tsomeFunction( \\\n"
16514 "\t\t parameter1, \\\n"
16515 "\t\t parameter2); \\\n"
16516 "\t}",
16517 Tab);
16518 Tab.TabWidth = 4;
16519 Tab.IndentWidth = 8;
16520 verifyFormat("class TabWidth4Indent8 {\n"
16521 "\t\tvoid f() {\n"
16522 "\t\t\t\tsomeFunction(parameter1,\n"
16523 "\t\t\t\t\t\t\t parameter2);\n"
16524 "\t\t}\n"
16525 "};",
16526 Tab);
16527 Tab.TabWidth = 4;
16528 Tab.IndentWidth = 4;
16529 verifyFormat("class TabWidth4Indent4 {\n"
16530 "\tvoid f() {\n"
16531 "\t\tsomeFunction(parameter1,\n"
16532 "\t\t\t\t\t parameter2);\n"
16533 "\t}\n"
16534 "};",
16535 Tab);
16536 Tab.TabWidth = 8;
16537 Tab.IndentWidth = 4;
16538 verifyFormat("class TabWidth8Indent4 {\n"
16539 " void f() {\n"
16540 "\tsomeFunction(parameter1,\n"
16541 "\t\t parameter2);\n"
16542 " }\n"
16543 "};",
16544 Tab);
16545 Tab.TabWidth = 8;
16546 Tab.IndentWidth = 8;
16547 verifyFormat("/*\n"
16548 "\t a\t\tcomment\n"
16549 "\t in multiple lines\n"
16550 " */",
16551 " /*\t \t \n"
16552 " \t \t a\t\tcomment\t \t\n"
16553 " \t \t in multiple lines\t\n"
16554 " \t */",
16555 Tab);
16556 verifyFormat("{\n"
16557 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16558 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16559 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16560 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16561 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16562 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16563 "};",
16564 Tab);
16565 verifyFormat("enum AA {\n"
16566 "\ta1, // Force multiple lines\n"
16567 "\ta2,\n"
16568 "\ta3\n"
16569 "};",
16570 Tab);
16571 verifyFormat("if (aaaaaaaa && // q\n"
16572 " bb) // w\n"
16573 "\t;",
16574 "if (aaaaaaaa &&// q\n"
16575 "bb)// w\n"
16576 ";",
16577 Tab);
16578 verifyFormat("class X {\n"
16579 "\tvoid f() {\n"
16580 "\t\tsomeFunction(parameter1,\n"
16581 "\t\t\t parameter2);\n"
16582 "\t}\n"
16583 "};",
16584 Tab);
16585 verifyFormat("{\n"
16586 "\tQ(\n"
16587 "\t {\n"
16588 "\t\t int a;\n"
16589 "\t\t someFunction(aaaaaaaa,\n"
16590 "\t\t\t\t bbbbbbb);\n"
16591 "\t },\n"
16592 "\t p);\n"
16593 "}",
16594 Tab);
16595 verifyFormat("{\n"
16596 "\t/* aaaa\n"
16597 "\t bbbb */\n"
16598 "}",
16599 "{\n"
16600 "/* aaaa\n"
16601 " bbbb */\n"
16602 "}",
16603 Tab);
16604 verifyFormat("{\n"
16605 "\t/*\n"
16606 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16607 "\t bbbbbbbbbbbbb\n"
16608 "\t*/\n"
16609 "}",
16610 "{\n"
16611 "/*\n"
16612 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16613 "*/\n"
16614 "}",
16615 Tab);
16616 verifyFormat("{\n"
16617 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16618 "\t// bbbbbbbbbbbbb\n"
16619 "}",
16620 "{\n"
16621 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16622 "}",
16623 Tab);
16624 verifyFormat("{\n"
16625 "\t/*\n"
16626 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16627 "\t bbbbbbbbbbbbb\n"
16628 "\t*/\n"
16629 "}",
16630 "{\n"
16631 "\t/*\n"
16632 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16633 "\t*/\n"
16634 "}",
16635 Tab);
16636 verifyNoChange("{\n"
16637 "\t/*\n"
16638 "\n"
16639 "\t*/\n"
16640 "}",
16641 Tab);
16642 verifyNoChange("{\n"
16643 "\t/*\n"
16644 " asdf\n"
16645 "\t*/\n"
16646 "}",
16647 Tab);
16648 verifyFormat("/* some\n"
16649 " comment */",
16650 " \t \t /* some\n"
16651 " \t \t comment */",
16652 Tab);
16653 verifyFormat("int a; /* some\n"
16654 " comment */",
16655 " \t \t int a; /* some\n"
16656 " \t \t comment */",
16657 Tab);
16658 verifyFormat("int a; /* some\n"
16659 "comment */",
16660 " \t \t int\ta; /* some\n"
16661 " \t \t comment */",
16662 Tab);
16663 verifyFormat("f(\"\t\t\"); /* some\n"
16664 " comment */",
16665 " \t \t f(\"\t\t\"); /* some\n"
16666 " \t \t comment */",
16667 Tab);
16668 verifyFormat("{\n"
16669 "\t/*\n"
16670 "\t * Comment\n"
16671 "\t */\n"
16672 "\tint i;\n"
16673 "}",
16674 "{\n"
16675 "\t/*\n"
16676 "\t * Comment\n"
16677 "\t */\n"
16678 "\t int i;\n"
16679 "}",
16680 Tab);
16681 Tab.TabWidth = 2;
16682 Tab.IndentWidth = 2;
16683 verifyFormat("{\n"
16684 "\t/* aaaa\n"
16685 "\t\t bbbb */\n"
16686 "}",
16687 "{\n"
16688 "/* aaaa\n"
16689 "\t bbbb */\n"
16690 "}",
16691 Tab);
16692 verifyFormat("{\n"
16693 "\t/*\n"
16694 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16695 "\t\tbbbbbbbbbbbbb\n"
16696 "\t*/\n"
16697 "}",
16698 "{\n"
16699 "/*\n"
16700 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16701 "*/\n"
16702 "}",
16703 Tab);
16704 Tab.AlignConsecutiveAssignments.Enabled = true;
16705 Tab.AlignConsecutiveDeclarations.Enabled = true;
16706 Tab.TabWidth = 4;
16707 Tab.IndentWidth = 4;
16708 verifyFormat("class Assign {\n"
16709 "\tvoid f() {\n"
16710 "\t\tint x = 123;\n"
16711 "\t\tint random = 4;\n"
16712 "\t\tstd::string alphabet =\n"
16713 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16714 "\t}\n"
16715 "};",
16716 Tab);
16717
16718 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16719 Tab.TabWidth = 8;
16720 Tab.IndentWidth = 8;
16721 verifyFormat("if (aaaaaaaa && // q\n"
16722 " bb) // w\n"
16723 "\t;",
16724 "if (aaaaaaaa &&// q\n"
16725 "bb)// w\n"
16726 ";",
16727 Tab);
16728 verifyFormat("if (aaa && bbb) // w\n"
16729 "\t;",
16730 "if(aaa&&bbb)// w\n"
16731 ";",
16732 Tab);
16733 verifyFormat("class X {\n"
16734 "\tvoid f() {\n"
16735 "\t\tsomeFunction(parameter1,\n"
16736 "\t\t parameter2);\n"
16737 "\t}\n"
16738 "};",
16739 Tab);
16740 verifyFormat("#define A \\\n"
16741 "\tvoid f() { \\\n"
16742 "\t\tsomeFunction( \\\n"
16743 "\t\t parameter1, \\\n"
16744 "\t\t parameter2); \\\n"
16745 "\t}",
16746 Tab);
16747 Tab.TabWidth = 4;
16748 Tab.IndentWidth = 8;
16749 verifyFormat("class TabWidth4Indent8 {\n"
16750 "\t\tvoid f() {\n"
16751 "\t\t\t\tsomeFunction(parameter1,\n"
16752 "\t\t\t\t parameter2);\n"
16753 "\t\t}\n"
16754 "};",
16755 Tab);
16756 Tab.TabWidth = 4;
16757 Tab.IndentWidth = 4;
16758 verifyFormat("class TabWidth4Indent4 {\n"
16759 "\tvoid f() {\n"
16760 "\t\tsomeFunction(parameter1,\n"
16761 "\t\t parameter2);\n"
16762 "\t}\n"
16763 "};",
16764 Tab);
16765 Tab.TabWidth = 8;
16766 Tab.IndentWidth = 4;
16767 verifyFormat("class TabWidth8Indent4 {\n"
16768 " void f() {\n"
16769 "\tsomeFunction(parameter1,\n"
16770 "\t parameter2);\n"
16771 " }\n"
16772 "};",
16773 Tab);
16774 Tab.TabWidth = 8;
16775 Tab.IndentWidth = 8;
16776 verifyFormat("/*\n"
16777 " a\t\tcomment\n"
16778 " in multiple lines\n"
16779 " */",
16780 " /*\t \t \n"
16781 " \t \t a\t\tcomment\t \t\n"
16782 " \t \t in multiple lines\t\n"
16783 " \t */",
16784 Tab);
16785 verifyFormat("{\n"
16786 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16787 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16788 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16789 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16790 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16791 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16792 "};",
16793 Tab);
16794 verifyFormat("enum AA {\n"
16795 "\ta1, // Force multiple lines\n"
16796 "\ta2,\n"
16797 "\ta3\n"
16798 "};",
16799 Tab);
16800 verifyFormat("if (aaaaaaaa && // q\n"
16801 " bb) // w\n"
16802 "\t;",
16803 "if (aaaaaaaa &&// q\n"
16804 "bb)// w\n"
16805 ";",
16806 Tab);
16807 verifyFormat("class X {\n"
16808 "\tvoid f() {\n"
16809 "\t\tsomeFunction(parameter1,\n"
16810 "\t\t parameter2);\n"
16811 "\t}\n"
16812 "};",
16813 Tab);
16814 verifyFormat("{\n"
16815 "\tQ(\n"
16816 "\t {\n"
16817 "\t\t int a;\n"
16818 "\t\t someFunction(aaaaaaaa,\n"
16819 "\t\t bbbbbbb);\n"
16820 "\t },\n"
16821 "\t p);\n"
16822 "}",
16823 Tab);
16824 verifyFormat("{\n"
16825 "\t/* aaaa\n"
16826 "\t bbbb */\n"
16827 "}",
16828 "{\n"
16829 "/* aaaa\n"
16830 " bbbb */\n"
16831 "}",
16832 Tab);
16833 verifyFormat("{\n"
16834 "\t/*\n"
16835 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16836 "\t bbbbbbbbbbbbb\n"
16837 "\t*/\n"
16838 "}",
16839 "{\n"
16840 "/*\n"
16841 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16842 "*/\n"
16843 "}",
16844 Tab);
16845 verifyFormat("{\n"
16846 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16847 "\t// bbbbbbbbbbbbb\n"
16848 "}",
16849 "{\n"
16850 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16851 "}",
16852 Tab);
16853 verifyFormat("{\n"
16854 "\t/*\n"
16855 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16856 "\t bbbbbbbbbbbbb\n"
16857 "\t*/\n"
16858 "}",
16859 "{\n"
16860 "\t/*\n"
16861 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16862 "\t*/\n"
16863 "}",
16864 Tab);
16865 verifyNoChange("{\n"
16866 "\t/*\n"
16867 "\n"
16868 "\t*/\n"
16869 "}",
16870 Tab);
16871 verifyNoChange("{\n"
16872 "\t/*\n"
16873 " asdf\n"
16874 "\t*/\n"
16875 "}",
16876 Tab);
16877 verifyFormat("/* some\n"
16878 " comment */",
16879 " \t \t /* some\n"
16880 " \t \t comment */",
16881 Tab);
16882 verifyFormat("int a; /* some\n"
16883 " comment */",
16884 " \t \t int a; /* some\n"
16885 " \t \t comment */",
16886 Tab);
16887 verifyFormat("int a; /* some\n"
16888 "comment */",
16889 " \t \t int\ta; /* some\n"
16890 " \t \t comment */",
16891 Tab);
16892 verifyFormat("f(\"\t\t\"); /* some\n"
16893 " comment */",
16894 " \t \t f(\"\t\t\"); /* some\n"
16895 " \t \t comment */",
16896 Tab);
16897 verifyFormat("{\n"
16898 "\t/*\n"
16899 "\t * Comment\n"
16900 "\t */\n"
16901 "\tint i;\n"
16902 "}",
16903 "{\n"
16904 "\t/*\n"
16905 "\t * Comment\n"
16906 "\t */\n"
16907 "\t int i;\n"
16908 "}",
16909 Tab);
16910 Tab.TabWidth = 2;
16911 Tab.IndentWidth = 2;
16912 verifyFormat("{\n"
16913 "\t/* aaaa\n"
16914 "\t bbbb */\n"
16915 "}",
16916 "{\n"
16917 "/* aaaa\n"
16918 " bbbb */\n"
16919 "}",
16920 Tab);
16921 verifyFormat("{\n"
16922 "\t/*\n"
16923 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16924 "\t bbbbbbbbbbbbb\n"
16925 "\t*/\n"
16926 "}",
16927 "{\n"
16928 "/*\n"
16929 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16930 "*/\n"
16931 "}",
16932 Tab);
16933 Tab.AlignConsecutiveAssignments.Enabled = true;
16934 Tab.AlignConsecutiveDeclarations.Enabled = true;
16935 Tab.TabWidth = 4;
16936 Tab.IndentWidth = 4;
16937 verifyFormat("class Assign {\n"
16938 "\tvoid f() {\n"
16939 "\t\tint x = 123;\n"
16940 "\t\tint random = 4;\n"
16941 "\t\tstd::string alphabet =\n"
16942 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16943 "\t}\n"
16944 "};",
16945 Tab);
16946 Tab.AlignOperands = FormatStyle::OAS_Align;
16947 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16948 " cccccccccccccccccccc;",
16949 Tab);
16950 // no alignment
16951 verifyFormat("int aaaaaaaaaa =\n"
16952 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16953 Tab);
16954 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16955 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16956 " : 333333333333333;",
16957 Tab);
16958 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16959 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16960 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16961 " + cccccccccccccccccccc;",
16962 Tab);
16963 }
16964
16965 TEST_F(FormatTest, ZeroTabWidth) {
16966 FormatStyle Tab = getLLVMStyleWithColumns(42);
16967 Tab.IndentWidth = 8;
16968 Tab.UseTab = FormatStyle::UT_Never;
16969 Tab.TabWidth = 0;
16970 verifyFormat("void a() {\n"
16971 " // line starts with '\t'\n"
16972 "};",
16973 "void a(){\n"
16974 "\t// line starts with '\t'\n"
16975 "};",
16976 Tab);
16977
16978 verifyFormat("void a() {\n"
16979 " // line starts with '\t'\n"
16980 "};",
16981 "void a(){\n"
16982 "\t\t// line starts with '\t'\n"
16983 "};",
16984 Tab);
16985
16986 Tab.UseTab = FormatStyle::UT_ForIndentation;
16987 verifyFormat("void a() {\n"
16988 " // line starts with '\t'\n"
16989 "};",
16990 "void a(){\n"
16991 "\t// line starts with '\t'\n"
16992 "};",
16993 Tab);
16994
16995 verifyFormat("void a() {\n"
16996 " // line starts with '\t'\n"
16997 "};",
16998 "void a(){\n"
16999 "\t\t// line starts with '\t'\n"
17000 "};",
17001 Tab);
17002
17003 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
17004 verifyFormat("void a() {\n"
17005 " // line starts with '\t'\n"
17006 "};",
17007 "void a(){\n"
17008 "\t// line starts with '\t'\n"
17009 "};",
17010 Tab);
17011
17012 verifyFormat("void a() {\n"
17013 " // line starts with '\t'\n"
17014 "};",
17015 "void a(){\n"
17016 "\t\t// line starts with '\t'\n"
17017 "};",
17018 Tab);
17019
17020 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
17021 verifyFormat("void a() {\n"
17022 " // line starts with '\t'\n"
17023 "};",
17024 "void a(){\n"
17025 "\t// line starts with '\t'\n"
17026 "};",
17027 Tab);
17028
17029 verifyFormat("void a() {\n"
17030 " // line starts with '\t'\n"
17031 "};",
17032 "void a(){\n"
17033 "\t\t// line starts with '\t'\n"
17034 "};",
17035 Tab);
17036
17037 Tab.UseTab = FormatStyle::UT_Always;
17038 verifyFormat("void a() {\n"
17039 "// line starts with '\t'\n"
17040 "};",
17041 "void a(){\n"
17042 "\t// line starts with '\t'\n"
17043 "};",
17044 Tab);
17045
17046 verifyFormat("void a() {\n"
17047 "// line starts with '\t'\n"
17048 "};",
17049 "void a(){\n"
17050 "\t\t// line starts with '\t'\n"
17051 "};",
17052 Tab);
17053 }
17054
17055 TEST_F(FormatTest, CalculatesOriginalColumn) {
17056 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17057 "q\"; /* some\n"
17058 " comment */",
17059 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17060 "q\"; /* some\n"
17061 " comment */");
17062 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
17063 "/* some\n"
17064 " comment */",
17065 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
17066 " /* some\n"
17067 " comment */");
17068 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17069 "qqq\n"
17070 "/* some\n"
17071 " comment */",
17072 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17073 "qqq\n"
17074 " /* some\n"
17075 " comment */");
17076 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17077 "wwww; /* some\n"
17078 " comment */",
17079 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17080 "wwww; /* some\n"
17081 " comment */");
17082 }
17083
17084 TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
17085 auto SpaceAfterOperatorKeyword = getLLVMStyle();
17086 SpaceAfterOperatorKeyword.SpaceAfterOperatorKeyword = true;
17087 verifyFormat("bool operator ++(int a);", SpaceAfterOperatorKeyword);
17088 }
17089
17090 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
17091 FormatStyle NoSpace = getLLVMStyle();
17092 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
17093
17094 verifyFormat("while(true)\n"
17095 " continue;",
17096 NoSpace);
17097 verifyFormat("for(;;)\n"
17098 " continue;",
17099 NoSpace);
17100 verifyFormat("if(true)\n"
17101 " f();\n"
17102 "else if(true)\n"
17103 " f();",
17104 NoSpace);
17105 verifyFormat("do {\n"
17106 " do_something();\n"
17107 "} while(something());",
17108 NoSpace);
17109 verifyFormat("switch(x) {\n"
17110 "default:\n"
17111 " break;\n"
17112 "}",
17113 NoSpace);
17114 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
17115 verifyFormat("size_t x = sizeof(x);", NoSpace);
17116 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
17117 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
17118 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
17119 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
17120 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
17121 verifyFormat("alignas(128) char a[128];", NoSpace);
17122 verifyFormat("size_t x = alignof(MyType);", NoSpace);
17123 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
17124 verifyFormat("int f() throw(Deprecated);", NoSpace);
17125 verifyFormat("typedef void (*cb)(int);", NoSpace);
17126 verifyFormat("T A::operator()();", NoSpace);
17127 verifyFormat("X A::operator++(T);", NoSpace);
17128 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
17129 verifyFormat("#if (foo || bar) && baz\n"
17130 "#elif ((a || b) && c) || d\n"
17131 "#endif",
17132 NoSpace);
17133
17134 FormatStyle Space = getLLVMStyle();
17135 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
17136
17137 verifyFormat("int f ();", Space);
17138 verifyFormat("bool operator< ();", Space);
17139 verifyFormat("bool operator> ();", Space);
17140 verifyFormat("void f (int a, T b) {\n"
17141 " while (true)\n"
17142 " continue;\n"
17143 "}",
17144 Space);
17145 verifyFormat("if (true)\n"
17146 " f ();\n"
17147 "else if (true)\n"
17148 " f ();",
17149 Space);
17150 verifyFormat("do {\n"
17151 " do_something ();\n"
17152 "} while (something ());",
17153 Space);
17154 verifyFormat("switch (x) {\n"
17155 "default:\n"
17156 " break;\n"
17157 "}",
17158 Space);
17159 verifyFormat("A::A () : a (1) {}", Space);
17160 verifyFormat("void f () __attribute__ ((asdf));", Space);
17161 verifyFormat("*(&a + 1);\n"
17162 "&((&a)[1]);\n"
17163 "a[(b + c) * d];\n"
17164 "(((a + 1) * 2) + 3) * 4;",
17165 Space);
17166 verifyFormat("#define A(x) x", Space);
17167 verifyFormat("#define A (x) x", Space);
17168 verifyFormat("#if defined(x)\n"
17169 "#endif",
17170 Space);
17171 verifyFormat("auto i = std::make_unique<int> (5);", Space);
17172 verifyFormat("size_t x = sizeof (x);", Space);
17173 verifyFormat("auto f (int x) -> decltype (x);", Space);
17174 verifyFormat("auto f (int x) -> typeof (x);", Space);
17175 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
17176 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
17177 verifyFormat("int f (T x) noexcept (x.create ());", Space);
17178 verifyFormat("alignas (128) char a[128];", Space);
17179 verifyFormat("size_t x = alignof (MyType);", Space);
17180 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
17181 verifyFormat("int f () throw (Deprecated);", Space);
17182 verifyFormat("typedef void (*cb) (int);", Space);
17183 verifyFormat("T A::operator() ();", Space);
17184 verifyFormat("X A::operator++ (T);", Space);
17185 verifyFormat("auto lambda = [] () { return 0; };", Space);
17186 verifyFormat("int x = int (y);", Space);
17187 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
17188 verifyFormat("__builtin_LINE ()", Space);
17189 verifyFormat("__builtin_UNKNOWN ()", Space);
17190
17191 FormatStyle SomeSpace = getLLVMStyle();
17192 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
17193
17194 verifyFormat("[]() -> float {}", SomeSpace);
17195 verifyFormat("[] (auto foo) {}", SomeSpace);
17196 verifyFormat("[foo]() -> int {}", SomeSpace);
17197 verifyFormat("int f();", SomeSpace);
17198 verifyFormat("void f (int a, T b) {\n"
17199 " while (true)\n"
17200 " continue;\n"
17201 "}",
17202 SomeSpace);
17203 verifyFormat("if (true)\n"
17204 " f();\n"
17205 "else if (true)\n"
17206 " f();",
17207 SomeSpace);
17208 verifyFormat("do {\n"
17209 " do_something();\n"
17210 "} while (something());",
17211 SomeSpace);
17212 verifyFormat("switch (x) {\n"
17213 "default:\n"
17214 " break;\n"
17215 "}",
17216 SomeSpace);
17217 verifyFormat("A::A() : a (1) {}", SomeSpace);
17218 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
17219 verifyFormat("*(&a + 1);\n"
17220 "&((&a)[1]);\n"
17221 "a[(b + c) * d];\n"
17222 "(((a + 1) * 2) + 3) * 4;",
17223 SomeSpace);
17224 verifyFormat("#define A(x) x", SomeSpace);
17225 verifyFormat("#define A (x) x", SomeSpace);
17226 verifyFormat("#if defined(x)\n"
17227 "#endif",
17228 SomeSpace);
17229 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
17230 verifyFormat("size_t x = sizeof (x);", SomeSpace);
17231 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
17232 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
17233 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
17234 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
17235 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
17236 verifyFormat("alignas (128) char a[128];", SomeSpace);
17237 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
17238 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17239 SomeSpace);
17240 verifyFormat("int f() throw (Deprecated);", SomeSpace);
17241 verifyFormat("typedef void (*cb) (int);", SomeSpace);
17242 verifyFormat("T A::operator()();", SomeSpace);
17243 verifyFormat("X A::operator++ (T);", SomeSpace);
17244 verifyFormat("int x = int (y);", SomeSpace);
17245 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
17246
17247 FormatStyle SpaceControlStatements = getLLVMStyle();
17248 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17249 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
17250
17251 verifyFormat("while (true)\n"
17252 " continue;",
17253 SpaceControlStatements);
17254 verifyFormat("if (true)\n"
17255 " f();\n"
17256 "else if (true)\n"
17257 " f();",
17258 SpaceControlStatements);
17259 verifyFormat("for (;;) {\n"
17260 " do_something();\n"
17261 "}",
17262 SpaceControlStatements);
17263 verifyFormat("do {\n"
17264 " do_something();\n"
17265 "} while (something());",
17266 SpaceControlStatements);
17267 verifyFormat("switch (x) {\n"
17268 "default:\n"
17269 " break;\n"
17270 "}",
17271 SpaceControlStatements);
17272
17273 FormatStyle SpaceFuncDecl = getLLVMStyle();
17274 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17275 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
17276
17277 verifyFormat("int f ();", SpaceFuncDecl);
17278 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
17279 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
17280 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
17281 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
17282 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
17283 verifyFormat("#define A(x) x", SpaceFuncDecl);
17284 verifyFormat("#define A (x) x", SpaceFuncDecl);
17285 verifyFormat("#if defined(x)\n"
17286 "#endif",
17287 SpaceFuncDecl);
17288 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
17289 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
17290 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
17291 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
17292 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
17293 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
17294 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
17295 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
17296 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
17297 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17298 SpaceFuncDecl);
17299 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
17300 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
17301 verifyFormat("T A::operator()();", SpaceFuncDecl);
17302 verifyFormat("X A::operator++(T);", SpaceFuncDecl);
17303 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
17304 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
17305 verifyFormat("int x = int(y);", SpaceFuncDecl);
17306 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17307 SpaceFuncDecl);
17308
17309 FormatStyle SpaceFuncDef = getLLVMStyle();
17310 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17311 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
17312
17313 verifyFormat("int f();", SpaceFuncDef);
17314 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
17315 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
17316 verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
17317 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
17318 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
17319 verifyFormat("#define A(x) x", SpaceFuncDef);
17320 verifyFormat("#define A (x) x", SpaceFuncDef);
17321 verifyFormat("#if defined(x)\n"
17322 "#endif",
17323 SpaceFuncDef);
17324 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
17325 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
17326 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
17327 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
17328 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
17329 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
17330 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
17331 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
17332 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
17333 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17334 SpaceFuncDef);
17335 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
17336 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
17337 verifyFormat("T A::operator()();", SpaceFuncDef);
17338 verifyFormat("X A::operator++(T);", SpaceFuncDef);
17339 verifyFormat("T A::operator()() {}", SpaceFuncDef);
17340 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
17341 verifyFormat("int x = int(y);", SpaceFuncDef);
17342 verifyFormat("void foo::bar () {}", SpaceFuncDef);
17343 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17344 SpaceFuncDef);
17345
17346 FormatStyle SpaceIfMacros = getLLVMStyle();
17347 SpaceIfMacros.IfMacros.clear();
17348 SpaceIfMacros.IfMacros.push_back("MYIF");
17349 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17350 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
17351 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
17352 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
17353 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
17354
17355 FormatStyle SpaceForeachMacros = getLLVMStyle();
17356 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
17357 FormatStyle::SBS_Never);
17358 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
17359 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17360 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
17361 verifyFormat("for (;;) {\n"
17362 "}",
17363 SpaceForeachMacros);
17364 verifyFormat("foreach (Item *item, itemlist) {\n"
17365 "}",
17366 SpaceForeachMacros);
17367 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17368 "}",
17369 SpaceForeachMacros);
17370 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17371 "}",
17372 SpaceForeachMacros);
17373 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
17374
17375 FormatStyle SomeSpace2 = getLLVMStyle();
17376 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17377 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
17378 verifyFormat("[]() -> float {}", SomeSpace2);
17379 verifyFormat("[] (auto foo) {}", SomeSpace2);
17380 verifyFormat("[foo]() -> int {}", SomeSpace2);
17381 verifyFormat("int f();", SomeSpace2);
17382 verifyFormat("void f (int a, T b) {\n"
17383 " while (true)\n"
17384 " continue;\n"
17385 "}",
17386 SomeSpace2);
17387 verifyFormat("if (true)\n"
17388 " f();\n"
17389 "else if (true)\n"
17390 " f();",
17391 SomeSpace2);
17392 verifyFormat("do {\n"
17393 " do_something();\n"
17394 "} while (something());",
17395 SomeSpace2);
17396 verifyFormat("switch (x) {\n"
17397 "default:\n"
17398 " break;\n"
17399 "}",
17400 SomeSpace2);
17401 verifyFormat("A::A() : a (1) {}", SomeSpace2);
17402 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
17403 verifyFormat("*(&a + 1);\n"
17404 "&((&a)[1]);\n"
17405 "a[(b + c) * d];\n"
17406 "(((a + 1) * 2) + 3) * 4;",
17407 SomeSpace2);
17408 verifyFormat("#define A(x) x", SomeSpace2);
17409 verifyFormat("#define A (x) x", SomeSpace2);
17410 verifyFormat("#if defined(x)\n"
17411 "#endif",
17412 SomeSpace2);
17413 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
17414 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
17415 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
17416 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
17417 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
17418 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
17419 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
17420 verifyFormat("alignas (128) char a[128];", SomeSpace2);
17421 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
17422 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17423 SomeSpace2);
17424 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
17425 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
17426 verifyFormat("T A::operator()();", SomeSpace2);
17427 verifyFormat("X A::operator++ (T);", SomeSpace2);
17428 verifyFormat("int x = int (y);", SomeSpace2);
17429 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
17430
17431 auto Style = getLLVMStyle();
17432 Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17433 EXPECT_FALSE(Style.SpaceBeforeParensOptions.AfterNot);
17434 Style.SpaceBeforeParensOptions.AfterNot = true;
17435 verifyFormat("return not (a || b);", Style);
17436
17437 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
17438 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17439 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17440 .AfterOverloadedOperator = true;
17441
17442 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
17443 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
17444 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
17445 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17446
17447 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17448 .AfterOverloadedOperator = false;
17449
17450 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
17451 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
17452 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
17453 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17454
17455 auto SpaceAfterRequires = getLLVMStyle();
17456 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17457 EXPECT_FALSE(
17458 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
17459 EXPECT_FALSE(
17460 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
17461 verifyFormat("void f(auto x)\n"
17462 " requires requires(int i) { x + i; }\n"
17463 "{}",
17464 SpaceAfterRequires);
17465 verifyFormat("void f(auto x)\n"
17466 " requires(requires(int i) { x + i; })\n"
17467 "{}",
17468 SpaceAfterRequires);
17469 verifyFormat("if (requires(int i) { x + i; })\n"
17470 " return;",
17471 SpaceAfterRequires);
17472 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17473 verifyFormat("template <typename T>\n"
17474 " requires(Foo<T>)\n"
17475 "class Bar;",
17476 SpaceAfterRequires);
17477
17478 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17479 verifyFormat("void f(auto x)\n"
17480 " requires requires(int i) { x + i; }\n"
17481 "{}",
17482 SpaceAfterRequires);
17483 verifyFormat("void f(auto x)\n"
17484 " requires (requires(int i) { x + i; })\n"
17485 "{}",
17486 SpaceAfterRequires);
17487 verifyFormat("if (requires(int i) { x + i; })\n"
17488 " return;",
17489 SpaceAfterRequires);
17490 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17491 verifyFormat("template <typename T>\n"
17492 " requires (Foo<T>)\n"
17493 "class Bar;",
17494 SpaceAfterRequires);
17495
17496 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
17497 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
17498 verifyFormat("void f(auto x)\n"
17499 " requires requires (int i) { x + i; }\n"
17500 "{}",
17501 SpaceAfterRequires);
17502 verifyFormat("void f(auto x)\n"
17503 " requires(requires (int i) { x + i; })\n"
17504 "{}",
17505 SpaceAfterRequires);
17506 verifyFormat("if (requires (int i) { x + i; })\n"
17507 " return;",
17508 SpaceAfterRequires);
17509 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17510 verifyFormat("template <typename T>\n"
17511 " requires(Foo<T>)\n"
17512 "class Bar;",
17513 SpaceAfterRequires);
17514
17515 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17516 verifyFormat("void f(auto x)\n"
17517 " requires requires (int i) { x + i; }\n"
17518 "{}",
17519 SpaceAfterRequires);
17520 verifyFormat("void f(auto x)\n"
17521 " requires (requires (int i) { x + i; })\n"
17522 "{}",
17523 SpaceAfterRequires);
17524 verifyFormat("if (requires (int i) { x + i; })\n"
17525 " return;",
17526 SpaceAfterRequires);
17527 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17528 verifyFormat("template <typename T>\n"
17529 " requires (Foo<T>)\n"
17530 "class Bar;",
17531 SpaceAfterRequires);
17532 }
17533
17534 TEST_F(FormatTest, SpaceAfterLogicalNot) {
17535 FormatStyle Spaces = getLLVMStyle();
17536 Spaces.SpaceAfterLogicalNot = true;
17537
17538 verifyFormat("bool x = ! y", Spaces);
17539 verifyFormat("if (! isFailure())", Spaces);
17540 verifyFormat("if (! (a && b))", Spaces);
17541 verifyFormat("\"Error!\"", Spaces);
17542 verifyFormat("! ! x", Spaces);
17543 }
17544
17545 TEST_F(FormatTest, ConfigurableSpacesInParens) {
17546 FormatStyle Spaces = getLLVMStyle();
17547
17548 verifyFormat("do_something(::globalVar);", Spaces);
17549 verifyFormat("call(x, y, z);", Spaces);
17550 verifyFormat("call();", Spaces);
17551 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17552 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17553 Spaces);
17554 verifyFormat("while ((bool)1)\n"
17555 " continue;",
17556 Spaces);
17557 verifyFormat("for (;;)\n"
17558 " continue;",
17559 Spaces);
17560 verifyFormat("if (true)\n"
17561 " f();\n"
17562 "else if (true)\n"
17563 " f();",
17564 Spaces);
17565 verifyFormat("do {\n"
17566 " do_something((int)i);\n"
17567 "} while (something());",
17568 Spaces);
17569 verifyFormat("switch (x) {\n"
17570 "default:\n"
17571 " break;\n"
17572 "}",
17573 Spaces);
17574 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17575 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17576 verifyFormat("void f() __attribute__((asdf));", Spaces);
17577 verifyFormat("x = (int32)y;", Spaces);
17578 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces);
17579 verifyFormat("decltype(x) y = 42;", Spaces);
17580 verifyFormat("decltype((x)) y = z;", Spaces);
17581 verifyFormat("decltype((foo())) a = foo();", Spaces);
17582 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17583 verifyFormat("if ((x - y) && (a ^ b))\n"
17584 " f();",
17585 Spaces);
17586 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17587 " foo(i);",
17588 Spaces);
17589 verifyFormat("switch (x / (y + z)) {\n"
17590 "default:\n"
17591 " break;\n"
17592 "}",
17593 Spaces);
17594
17595 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17596 Spaces.SpacesInParensOptions = {};
17597 Spaces.SpacesInParensOptions.Other = true;
17598
17599 EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
17600 verifyFormat("if (a)\n"
17601 " return;",
17602 Spaces);
17603
17604 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17605 verifyFormat("do_something( ::globalVar );", Spaces);
17606 verifyFormat("call( x, y, z );", Spaces);
17607 verifyFormat("call();", Spaces);
17608 verifyFormat("std::function<void( int, int )> callback;", Spaces);
17609 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17610 Spaces);
17611 verifyFormat("while ( (bool)1 )\n"
17612 " continue;",
17613 Spaces);
17614 verifyFormat("for ( ;; )\n"
17615 " continue;",
17616 Spaces);
17617 verifyFormat("if ( true )\n"
17618 " f();\n"
17619 "else if ( true )\n"
17620 " f();",
17621 Spaces);
17622 verifyFormat("do {\n"
17623 " do_something( (int)i );\n"
17624 "} while ( something() );",
17625 Spaces);
17626 verifyFormat("switch ( x ) {\n"
17627 "default:\n"
17628 " break;\n"
17629 "}",
17630 Spaces);
17631 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17632 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17633 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17634 verifyFormat("x = (int32)y;", Spaces);
17635 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces);
17636 verifyFormat("decltype( x ) y = 42;", Spaces);
17637 verifyFormat("decltype( ( x ) ) y = z;", Spaces);
17638 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces);
17639 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces);
17640 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17641 " f();",
17642 Spaces);
17643 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17644 " foo( i );",
17645 Spaces);
17646 verifyFormat("switch ( x / ( y + z ) ) {\n"
17647 "default:\n"
17648 " break;\n"
17649 "}",
17650 Spaces);
17651
17652 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17653 Spaces.SpacesInParensOptions = {};
17654 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17655 verifyFormat("Type *A = ( Type * )P;", Spaces);
17656 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17657 verifyFormat("x = ( int32 )y;", Spaces);
17658 verifyFormat("throw ( int32 )x;", Spaces);
17659 verifyFormat("int a = ( int )(2.0f);", Spaces);
17660 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17661 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17662 verifyFormat("#define x (( int )-1)", Spaces);
17663 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17664
17665 // Run the first set of tests again with:
17666 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17667 Spaces.SpacesInParensOptions = {};
17668 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17669 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17670 verifyFormat("call(x, y, z);", Spaces);
17671 verifyFormat("call( );", Spaces);
17672 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17673 verifyFormat("while (( bool )1)\n"
17674 " continue;",
17675 Spaces);
17676 verifyFormat("for (;;)\n"
17677 " continue;",
17678 Spaces);
17679 verifyFormat("if (true)\n"
17680 " f( );\n"
17681 "else if (true)\n"
17682 " f( );",
17683 Spaces);
17684 verifyFormat("do {\n"
17685 " do_something(( int )i);\n"
17686 "} while (something( ));",
17687 Spaces);
17688 verifyFormat("switch (x) {\n"
17689 "default:\n"
17690 " break;\n"
17691 "}",
17692 Spaces);
17693 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17694 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17695 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17696 verifyFormat("x = ( int32 )y;", Spaces);
17697 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17698 verifyFormat("decltype(x) y = 42;", Spaces);
17699 verifyFormat("decltype((x)) y = z;", Spaces);
17700 verifyFormat("decltype((foo( ))) a = foo( );", Spaces);
17701 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17702 verifyFormat("if ((x - y) && (a ^ b))\n"
17703 " f( );",
17704 Spaces);
17705 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17706 " foo(i);",
17707 Spaces);
17708 verifyFormat("switch (x / (y + z)) {\n"
17709 "default:\n"
17710 " break;\n"
17711 "}",
17712 Spaces);
17713
17714 // Run the first set of tests again with:
17715 Spaces.SpaceAfterCStyleCast = true;
17716 verifyFormat("call(x, y, z);", Spaces);
17717 verifyFormat("call( );", Spaces);
17718 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17719 verifyFormat("while (( bool ) 1)\n"
17720 " continue;",
17721 Spaces);
17722 verifyFormat("for (;;)\n"
17723 " continue;",
17724 Spaces);
17725 verifyFormat("if (true)\n"
17726 " f( );\n"
17727 "else if (true)\n"
17728 " f( );",
17729 Spaces);
17730 verifyFormat("do {\n"
17731 " do_something(( int ) i);\n"
17732 "} while (something( ));",
17733 Spaces);
17734 verifyFormat("switch (x) {\n"
17735 "default:\n"
17736 " break;\n"
17737 "}",
17738 Spaces);
17739 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
17740 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
17741 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
17742 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
17743 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
17744 verifyFormat("throw ( int32 ) x;", Spaces);
17745 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17746 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17747 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17748
17749 // Run subset of tests again with:
17750 Spaces.SpacesInParensOptions.InCStyleCasts = false;
17751 Spaces.SpaceAfterCStyleCast = true;
17752 verifyFormat("while ((bool) 1)\n"
17753 " continue;",
17754 Spaces);
17755 verifyFormat("do {\n"
17756 " do_something((int) i);\n"
17757 "} while (something( ));",
17758 Spaces);
17759
17760 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
17761 verifyFormat("size_t idx = (size_t) a;", Spaces);
17762 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
17763 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17764 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17765 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17766 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17767 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
17768 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
17769 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
17770 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
17771 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
17772 verifyFormat("throw (int32) x;", Spaces);
17773 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17774 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17775 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17776
17777 Spaces.ColumnLimit = 80;
17778 Spaces.IndentWidth = 4;
17779 Spaces.BreakAfterOpenBracketFunction = true;
17780 verifyFormat("void foo( ) {\n"
17781 " size_t foo = (*(function))(\n"
17782 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17783 "BarrrrrrrrrrrrLong,\n"
17784 " FoooooooooLooooong);\n"
17785 "}",
17786 Spaces);
17787 Spaces.SpaceAfterCStyleCast = false;
17788 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17789 verifyFormat("size_t idx = (size_t)a;", Spaces);
17790 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17791 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17792 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17793 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17794 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17795
17796 verifyFormat("void foo( ) {\n"
17797 " size_t foo = (*(function))(\n"
17798 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17799 "BarrrrrrrrrrrrLong,\n"
17800 " FoooooooooLooooong);\n"
17801 "}",
17802 Spaces);
17803
17804 Spaces.BreakAfterOpenBracketFunction = true;
17805 Spaces.BreakBeforeCloseBracketFunction = true;
17806 verifyFormat("void foo( ) {\n"
17807 " size_t foo = (*(function))(\n"
17808 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17809 "BarrrrrrrrrrrrLong,\n"
17810 " FoooooooooLooooong\n"
17811 " );\n"
17812 "}",
17813 Spaces);
17814 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17815 verifyFormat("size_t idx = (size_t)a;", Spaces);
17816 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17817 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17818 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17819 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17820 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17821
17822 // Check ExceptDoubleParentheses spaces
17823 Spaces.IndentWidth = 2;
17824 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17825 Spaces.SpacesInParensOptions = {};
17826 Spaces.SpacesInParensOptions.Other = true;
17827 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17828 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
17829 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces);
17830 verifyFormat("void f() __attribute__(( asdf ));", Spaces);
17831 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces);
17832 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces);
17833 verifyFormat("class __declspec( dllimport ) X {};", Spaces);
17834 verifyFormat("class __declspec(( dllimport )) X {};", Spaces);
17835 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces);
17836 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces);
17837 verifyFormat("decltype( x ) y = 42;", Spaces);
17838 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces);
17839 verifyFormat("if (( i = j ))\n"
17840 " do_something( i );",
17841 Spaces);
17842
17843 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17844 Spaces.SpacesInParensOptions = {};
17845 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17846 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17847 verifyFormat("while ( (bool)1 )\n"
17848 " continue;",
17849 Spaces);
17850 verifyFormat("while ((i = j))\n"
17851 " continue;",
17852 Spaces);
17853 verifyFormat("do {\n"
17854 " do_something((int)i);\n"
17855 "} while ( something() );",
17856 Spaces);
17857 verifyFormat("do {\n"
17858 " do_something((int)i);\n"
17859 "} while ((i = i + 1));",
17860 Spaces);
17861 verifyFormat("if ( (x - y) && (a ^ b) )\n"
17862 " f();",
17863 Spaces);
17864 verifyFormat("if ((i = j))\n"
17865 " do_something(i);",
17866 Spaces);
17867 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
17868 " foo(i);",
17869 Spaces);
17870 verifyFormat("switch ( x / (y + z) ) {\n"
17871 "default:\n"
17872 " break;\n"
17873 "}",
17874 Spaces);
17875 verifyFormat("if constexpr ((a = b))\n"
17876 " c;",
17877 Spaces);
17878 }
17879
17880 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
17881 verifyFormat("int a[5];");
17882 verifyFormat("a[3] += 42;");
17883
17884 FormatStyle Spaces = getLLVMStyle();
17885 Spaces.SpacesInSquareBrackets = true;
17886 // Not lambdas.
17887 verifyFormat("int a[ 5 ];", Spaces);
17888 verifyFormat("a[ 3 ] += 42;", Spaces);
17889 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
17890 verifyFormat("double &operator[](int i) { return 0; }\n"
17891 "int i;",
17892 Spaces);
17893 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
17894 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
17895 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
17896 // Lambdas.
17897 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
17898 verifyFormat("return [ i, args... ] {};", Spaces);
17899 verifyFormat("int foo = [ &bar ]() {};", Spaces);
17900 verifyFormat("int foo = [ = ]() {};", Spaces);
17901 verifyFormat("int foo = [ & ]() {};", Spaces);
17902 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
17903 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
17904 }
17905
17906 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
17907 FormatStyle NoSpaceStyle = getLLVMStyle();
17908 verifyFormat("int a[5];", NoSpaceStyle);
17909 verifyFormat("a[3] += 42;", NoSpaceStyle);
17910
17911 verifyFormat("int a[1];", NoSpaceStyle);
17912 verifyFormat("int 1 [a];", NoSpaceStyle);
17913 verifyFormat("int a[1][2];", NoSpaceStyle);
17914 verifyFormat("a[7] = 5;", NoSpaceStyle);
17915 verifyFormat("int a = (f())[23];", NoSpaceStyle);
17916 verifyFormat("f([] {})", NoSpaceStyle);
17917
17918 FormatStyle Space = getLLVMStyle();
17919 Space.SpaceBeforeSquareBrackets = true;
17920 verifyFormat("int c = []() -> int { return 2; }();", Space);
17921 verifyFormat("return [i, args...] {};", Space);
17922
17923 verifyFormat("int a [5];", Space);
17924 verifyFormat("a [3] += 42;", Space);
17925 verifyFormat("constexpr char hello []{\"hello\"};", Space);
17926 verifyFormat("double &operator[](int i) { return 0; }\n"
17927 "int i;",
17928 Space);
17929 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
17930 verifyFormat("int i = a [a][a]->f();", Space);
17931 verifyFormat("int i = (*b) [a]->f();", Space);
17932
17933 verifyFormat("int a [1];", Space);
17934 verifyFormat("int 1 [a];", Space);
17935 verifyFormat("int a [1][2];", Space);
17936 verifyFormat("a [7] = 5;", Space);
17937 verifyFormat("int a = (f()) [23];", Space);
17938 verifyFormat("f([] {})", Space);
17939 }
17940
17941 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
17942 verifyFormat("int a = 5;");
17943 verifyFormat("a += 42;");
17944 verifyFormat("a or_eq 8;");
17945
17946 auto Spaces = getLLVMStyle(FormatStyle::LK_C);
17947 verifyFormat("xor = foo;", Spaces);
17948
17949 Spaces.Language = FormatStyle::LK_Cpp;
17950 Spaces.SpaceBeforeAssignmentOperators = false;
17951 verifyFormat("int a= 5;", Spaces);
17952 verifyFormat("a+= 42;", Spaces);
17953 verifyFormat("a or_eq 8;", Spaces);
17954 verifyFormat("xor= foo;", Spaces);
17955 }
17956
17957 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
17958 verifyFormat("class Foo : public Bar {};");
17959 verifyFormat("Foo::Foo() : foo(1) {}");
17960 verifyFormat("for (auto a : b) {\n}");
17961 verifyFormat("int x = a ? b : c;");
17962 verifyFormat("{\n"
17963 "label0:\n"
17964 " int x = 0;\n"
17965 "}");
17966 verifyFormat("switch (x) {\n"
17967 "case 1:\n"
17968 "default:\n"
17969 "}");
17970 verifyFormat("switch (allBraces) {\n"
17971 "case 1: {\n"
17972 " break;\n"
17973 "}\n"
17974 "case 2: {\n"
17975 " [[fallthrough]];\n"
17976 "}\n"
17977 "default: {\n"
17978 " break;\n"
17979 "}\n"
17980 "}");
17981
17982 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
17983 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
17984 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
17985 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
17986 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
17987 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
17988 verifyFormat("{\n"
17989 "label1:\n"
17990 " int x = 0;\n"
17991 "}",
17992 CtorInitializerStyle);
17993 verifyFormat("switch (x) {\n"
17994 "case 1:\n"
17995 "default:\n"
17996 "}",
17997 CtorInitializerStyle);
17998 verifyFormat("switch (allBraces) {\n"
17999 "case 1: {\n"
18000 " break;\n"
18001 "}\n"
18002 "case 2: {\n"
18003 " [[fallthrough]];\n"
18004 "}\n"
18005 "default: {\n"
18006 " break;\n"
18007 "}\n"
18008 "}",
18009 CtorInitializerStyle);
18010 CtorInitializerStyle.BreakConstructorInitializers =
18011 FormatStyle::BCIS_AfterColon;
18012 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
18013 " aaaaaaaaaaaaaaaa(1),\n"
18014 " bbbbbbbbbbbbbbbb(2) {}",
18015 CtorInitializerStyle);
18016 CtorInitializerStyle.BreakConstructorInitializers =
18017 FormatStyle::BCIS_BeforeComma;
18018 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
18019 " : aaaaaaaaaaaaaaaa(1)\n"
18020 " , bbbbbbbbbbbbbbbb(2) {}",
18021 CtorInitializerStyle);
18022 CtorInitializerStyle.BreakConstructorInitializers =
18023 FormatStyle::BCIS_BeforeColon;
18024 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
18025 " : aaaaaaaaaaaaaaaa(1),\n"
18026 " bbbbbbbbbbbbbbbb(2) {}",
18027 CtorInitializerStyle);
18028 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
18029 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
18030 ": aaaaaaaaaaaaaaaa(1),\n"
18031 " bbbbbbbbbbbbbbbb(2) {}",
18032 CtorInitializerStyle);
18033
18034 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
18035 InheritanceStyle.SpaceBeforeInheritanceColon = false;
18036 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
18037 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
18038 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
18039 verifyFormat("int x = a ? b : c;", InheritanceStyle);
18040 verifyFormat("{\n"
18041 "label2:\n"
18042 " int x = 0;\n"
18043 "}",
18044 InheritanceStyle);
18045 verifyFormat("switch (x) {\n"
18046 "case 1:\n"
18047 "default:\n"
18048 "}",
18049 InheritanceStyle);
18050 verifyFormat("switch (allBraces) {\n"
18051 "case 1: {\n"
18052 " break;\n"
18053 "}\n"
18054 "case 2: {\n"
18055 " [[fallthrough]];\n"
18056 "}\n"
18057 "default: {\n"
18058 " break;\n"
18059 "}\n"
18060 "}",
18061 InheritanceStyle);
18062 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
18063 verifyFormat("class Foooooooooooooooooooooo\n"
18064 " : public aaaaaaaaaaaaaaaaaa,\n"
18065 " public bbbbbbbbbbbbbbbbbb {\n"
18066 "}",
18067 InheritanceStyle);
18068 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
18069 verifyFormat("class Foooooooooooooooooooooo:\n"
18070 " public aaaaaaaaaaaaaaaaaa,\n"
18071 " public bbbbbbbbbbbbbbbbbb {\n"
18072 "}",
18073 InheritanceStyle);
18074 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
18075 verifyFormat("class Foooooooooooooooooooooo\n"
18076 " : public aaaaaaaaaaaaaaaaaa\n"
18077 " , public bbbbbbbbbbbbbbbbbb {\n"
18078 "}",
18079 InheritanceStyle);
18080 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18081 verifyFormat("class Foooooooooooooooooooooo\n"
18082 " : public aaaaaaaaaaaaaaaaaa,\n"
18083 " public bbbbbbbbbbbbbbbbbb {\n"
18084 "}",
18085 InheritanceStyle);
18086 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
18087 verifyFormat("class Foooooooooooooooooooooo\n"
18088 ": public aaaaaaaaaaaaaaaaaa,\n"
18089 " public bbbbbbbbbbbbbbbbbb {}",
18090 InheritanceStyle);
18091
18092 FormatStyle ForLoopStyle = getLLVMStyle();
18093 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
18094 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
18095 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
18096 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
18097 verifyFormat("int x = a ? b : c;", ForLoopStyle);
18098 verifyFormat("{\n"
18099 "label2:\n"
18100 " int x = 0;\n"
18101 "}",
18102 ForLoopStyle);
18103 verifyFormat("switch (x) {\n"
18104 "case 1:\n"
18105 "default:\n"
18106 "}",
18107 ForLoopStyle);
18108 verifyFormat("switch (allBraces) {\n"
18109 "case 1: {\n"
18110 " break;\n"
18111 "}\n"
18112 "case 2: {\n"
18113 " [[fallthrough]];\n"
18114 "}\n"
18115 "default: {\n"
18116 " break;\n"
18117 "}\n"
18118 "}",
18119 ForLoopStyle);
18120
18121 FormatStyle CaseStyle = getLLVMStyle();
18122 CaseStyle.SpaceBeforeCaseColon = true;
18123 verifyFormat("class Foo : public Bar {};", CaseStyle);
18124 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
18125 verifyFormat("for (auto a : b) {\n}", CaseStyle);
18126 verifyFormat("int x = a ? b : c;", CaseStyle);
18127 verifyFormat("switch (x) {\n"
18128 "case 1 :\n"
18129 "default :\n"
18130 "}",
18131 CaseStyle);
18132 verifyFormat("switch (allBraces) {\n"
18133 "case 1 : {\n"
18134 " break;\n"
18135 "}\n"
18136 "case 2 : {\n"
18137 " [[fallthrough]];\n"
18138 "}\n"
18139 "default : {\n"
18140 " break;\n"
18141 "}\n"
18142 "}",
18143 CaseStyle);
18144 // Goto labels should not be affected.
18145 verifyFormat("switch (x) {\n"
18146 "goto_label:\n"
18147 "default :\n"
18148 "}",
18149 CaseStyle);
18150 verifyFormat("switch (x) {\n"
18151 "goto_label: { break; }\n"
18152 "default : {\n"
18153 " break;\n"
18154 "}\n"
18155 "}",
18156 CaseStyle);
18157
18158 FormatStyle NoSpaceStyle = getLLVMStyle();
18159 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
18160 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
18161 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
18162 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
18163 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
18164 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
18165 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
18166 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
18167 verifyFormat("{\n"
18168 "label3:\n"
18169 " int x = 0;\n"
18170 "}",
18171 NoSpaceStyle);
18172 verifyFormat("switch (x) {\n"
18173 "case 1:\n"
18174 "default:\n"
18175 "}",
18176 NoSpaceStyle);
18177 verifyFormat("switch (allBraces) {\n"
18178 "case 1: {\n"
18179 " break;\n"
18180 "}\n"
18181 "case 2: {\n"
18182 " [[fallthrough]];\n"
18183 "}\n"
18184 "default: {\n"
18185 " break;\n"
18186 "}\n"
18187 "}",
18188 NoSpaceStyle);
18189
18190 FormatStyle InvertedSpaceStyle = getLLVMStyle();
18191 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
18192 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
18193 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
18194 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
18195 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
18196 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
18197 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
18198 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
18199 verifyFormat("{\n"
18200 "label3:\n"
18201 " int x = 0;\n"
18202 "}",
18203 InvertedSpaceStyle);
18204 verifyFormat("switch (x) {\n"
18205 "case 1 :\n"
18206 "case 2 : {\n"
18207 " break;\n"
18208 "}\n"
18209 "default :\n"
18210 " break;\n"
18211 "}",
18212 InvertedSpaceStyle);
18213 verifyFormat("switch (allBraces) {\n"
18214 "case 1 : {\n"
18215 " break;\n"
18216 "}\n"
18217 "case 2 : {\n"
18218 " [[fallthrough]];\n"
18219 "}\n"
18220 "default : {\n"
18221 " break;\n"
18222 "}\n"
18223 "}",
18224 InvertedSpaceStyle);
18225 }
18226
18227 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
18228 FormatStyle Style = getLLVMStyle();
18229
18230 Style.PointerAlignment = FormatStyle::PAS_Left;
18231 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
18232 verifyFormat("void* const* x = NULL;", Style);
18233
18234 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
18235 do { \
18236 Style.PointerAlignment = FormatStyle::Pointers; \
18237 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
18238 verifyFormat(Code, Style); \
18239 } while (false)
18240
18241 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
18242 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
18243 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
18244
18245 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
18246 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
18247 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
18248
18249 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
18250 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
18251 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
18252
18253 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
18254 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
18255 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
18256
18257 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
18258 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
18259 SAPQ_Default);
18260 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
18261 SAPQ_Default);
18262
18263 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
18264 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
18265 SAPQ_Before);
18266 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
18267 SAPQ_Before);
18268
18269 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
18270 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
18271 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
18272 SAPQ_After);
18273
18274 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
18275 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
18276 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
18277
18278 #undef verifyQualifierSpaces
18279
18280 FormatStyle Spaces = getLLVMStyle();
18281 Spaces.AttributeMacros.push_back("qualified");
18282 Spaces.PointerAlignment = FormatStyle::PAS_Right;
18283 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
18284 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
18285 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
18286 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
18287 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
18288 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18289 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
18290 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
18291 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
18292 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18293 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18294 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18295
18296 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
18297 Spaces.PointerAlignment = FormatStyle::PAS_Left;
18298 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
18299 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
18300 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
18301 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
18302 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
18303 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18304 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
18305 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18306 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
18307 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
18308 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
18309 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
18310 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18311
18312 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18313 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
18314 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18315 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
18316 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
18317 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18318 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18319 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18320 }
18321
18322 TEST_F(FormatTest, AlignConsecutiveMacros) {
18323 FormatStyle Style = getLLVMStyle();
18324 Style.AlignConsecutiveAssignments.Enabled = true;
18325 Style.AlignConsecutiveDeclarations.Enabled = true;
18326
18327 verifyFormat("#define a 3\n"
18328 "#define bbbb 4\n"
18329 "#define ccc (5)",
18330 Style);
18331
18332 verifyFormat("#define f(x) (x * x)\n"
18333 "#define fff(x, y, z) (x * y + z)\n"
18334 "#define ffff(x, y) (x - y)",
18335 Style);
18336
18337 verifyFormat("#define foo(x, y) (x + y)\n"
18338 "#define bar (5, 6)(2 + 2)",
18339 Style);
18340
18341 verifyFormat("#define a 3\n"
18342 "#define bbbb 4\n"
18343 "#define ccc (5)\n"
18344 "#define f(x) (x * x)\n"
18345 "#define fff(x, y, z) (x * y + z)\n"
18346 "#define ffff(x, y) (x - y)",
18347 Style);
18348
18349 Style.AlignConsecutiveMacros.Enabled = true;
18350 verifyFormat("#define a 3\n"
18351 "#define bbbb 4\n"
18352 "#define ccc (5)",
18353 Style);
18354
18355 verifyFormat("#define true 1\n"
18356 "#define false 0",
18357 Style);
18358
18359 verifyFormat("#define f(x) (x * x)\n"
18360 "#define fff(x, y, z) (x * y + z)\n"
18361 "#define ffff(x, y) (x - y)",
18362 Style);
18363
18364 verifyFormat("#define foo(x, y) (x + y)\n"
18365 "#define bar (5, 6)(2 + 2)",
18366 Style);
18367
18368 verifyFormat("#define a 3\n"
18369 "#define bbbb 4\n"
18370 "#define ccc (5)\n"
18371 "#define f(x) (x * x)\n"
18372 "#define fff(x, y, z) (x * y + z)\n"
18373 "#define ffff(x, y) (x - y)",
18374 Style);
18375
18376 verifyFormat("#define a 5\n"
18377 "#define foo(x, y) (x + y)\n"
18378 "#define CCC (6)\n"
18379 "auto lambda = []() {\n"
18380 " auto ii = 0;\n"
18381 " float j = 0;\n"
18382 " return 0;\n"
18383 "};\n"
18384 "int i = 0;\n"
18385 "float i2 = 0;\n"
18386 "auto v = type{\n"
18387 " i = 1, //\n"
18388 " (i = 2), //\n"
18389 " i = 3 //\n"
18390 "};",
18391 Style);
18392
18393 Style.AlignConsecutiveMacros.Enabled = false;
18394 Style.ColumnLimit = 20;
18395
18396 verifyFormat("#define a \\\n"
18397 " \"aabbbbbbbbbbbb\"\n"
18398 "#define D \\\n"
18399 " \"aabbbbbbbbbbbb\" \\\n"
18400 " \"ccddeeeeeeeee\"\n"
18401 "#define B \\\n"
18402 " \"QQQQQQQQQQQQQ\" \\\n"
18403 " \"FFFFFFFFFFFFF\" \\\n"
18404 " \"LLLLLLLL\"",
18405 Style);
18406
18407 Style.AlignConsecutiveMacros.Enabled = true;
18408 verifyFormat("#define a \\\n"
18409 " \"aabbbbbbbbbbbb\"\n"
18410 "#define D \\\n"
18411 " \"aabbbbbbbbbbbb\" \\\n"
18412 " \"ccddeeeeeeeee\"\n"
18413 "#define B \\\n"
18414 " \"QQQQQQQQQQQQQ\" \\\n"
18415 " \"FFFFFFFFFFFFF\" \\\n"
18416 " \"LLLLLLLL\"",
18417 Style);
18418
18419 // Test across comments
18420 Style.MaxEmptyLinesToKeep = 10;
18421 Style.ReflowComments = FormatStyle::RCS_Never;
18422 Style.AlignConsecutiveMacros.AcrossComments = true;
18423 verifyFormat("#define a 3\n"
18424 "// line comment\n"
18425 "#define bbbb 4\n"
18426 "#define ccc (5)",
18427 "#define a 3\n"
18428 "// line comment\n"
18429 "#define bbbb 4\n"
18430 "#define ccc (5)",
18431 Style);
18432
18433 verifyFormat("#define a 3\n"
18434 "/* block comment */\n"
18435 "#define bbbb 4\n"
18436 "#define ccc (5)",
18437 "#define a 3\n"
18438 "/* block comment */\n"
18439 "#define bbbb 4\n"
18440 "#define ccc (5)",
18441 Style);
18442
18443 verifyFormat("#define a 3\n"
18444 "/* multi-line *\n"
18445 " * block comment */\n"
18446 "#define bbbb 4\n"
18447 "#define ccc (5)",
18448 "#define a 3\n"
18449 "/* multi-line *\n"
18450 " * block comment */\n"
18451 "#define bbbb 4\n"
18452 "#define ccc (5)",
18453 Style);
18454
18455 verifyFormat("#define a 3\n"
18456 "// multi-line line comment\n"
18457 "//\n"
18458 "#define bbbb 4\n"
18459 "#define ccc (5)",
18460 "#define a 3\n"
18461 "// multi-line line comment\n"
18462 "//\n"
18463 "#define bbbb 4\n"
18464 "#define ccc (5)",
18465 Style);
18466
18467 verifyFormat("#define a 3\n"
18468 "// empty lines still break.\n"
18469 "\n"
18470 "#define bbbb 4\n"
18471 "#define ccc (5)",
18472 "#define a 3\n"
18473 "// empty lines still break.\n"
18474 "\n"
18475 "#define bbbb 4\n"
18476 "#define ccc (5)",
18477 Style);
18478
18479 // Test across empty lines
18480 Style.AlignConsecutiveMacros.AcrossComments = false;
18481 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
18482 verifyFormat("#define a 3\n"
18483 "\n"
18484 "#define bbbb 4\n"
18485 "#define ccc (5)",
18486 "#define a 3\n"
18487 "\n"
18488 "#define bbbb 4\n"
18489 "#define ccc (5)",
18490 Style);
18491
18492 verifyFormat("#define a 3\n"
18493 "\n"
18494 "\n"
18495 "\n"
18496 "#define bbbb 4\n"
18497 "#define ccc (5)",
18498 "#define a 3\n"
18499 "\n"
18500 "\n"
18501 "\n"
18502 "#define bbbb 4\n"
18503 "#define ccc (5)",
18504 Style);
18505
18506 verifyFormat("#define a 3\n"
18507 "// comments should break alignment\n"
18508 "//\n"
18509 "#define bbbb 4\n"
18510 "#define ccc (5)",
18511 "#define a 3\n"
18512 "// comments should break alignment\n"
18513 "//\n"
18514 "#define bbbb 4\n"
18515 "#define ccc (5)",
18516 Style);
18517
18518 // Test across empty lines and comments
18519 Style.AlignConsecutiveMacros.AcrossComments = true;
18520 verifyFormat("#define a 3\n"
18521 "\n"
18522 "// line comment\n"
18523 "#define bbbb 4\n"
18524 "#define ccc (5)",
18525 Style);
18526
18527 verifyFormat("#define a 3\n"
18528 "\n"
18529 "\n"
18530 "/* multi-line *\n"
18531 " * block comment */\n"
18532 "\n"
18533 "\n"
18534 "#define bbbb 4\n"
18535 "#define ccc (5)",
18536 "#define a 3\n"
18537 "\n"
18538 "\n"
18539 "/* multi-line *\n"
18540 " * block comment */\n"
18541 "\n"
18542 "\n"
18543 "#define bbbb 4\n"
18544 "#define ccc (5)",
18545 Style);
18546
18547 verifyFormat("#define a 3\n"
18548 "\n"
18549 "\n"
18550 "/* multi-line *\n"
18551 " * block comment */\n"
18552 "\n"
18553 "\n"
18554 "#define bbbb 4\n"
18555 "#define ccc (5)",
18556 "#define a 3\n"
18557 "\n"
18558 "\n"
18559 "/* multi-line *\n"
18560 " * block comment */\n"
18561 "\n"
18562 "\n"
18563 "#define bbbb 4\n"
18564 "#define ccc (5)",
18565 Style);
18566
18567 Style.ColumnLimit = 30;
18568 verifyFormat("#define MY_FUNC(x) callMe(X)\n"
18569 "#define MY_LONG_CONSTANT 17",
18570 Style);
18571 }
18572
18573 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
18574 FormatStyle Alignment = getLLVMStyle();
18575 Alignment.AlignConsecutiveMacros.Enabled = true;
18576 Alignment.AlignConsecutiveAssignments.Enabled = true;
18577 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18578
18579 Alignment.MaxEmptyLinesToKeep = 10;
18580 /* Test alignment across empty lines */
18581 verifyFormat("int a = 5;\n"
18582 "\n"
18583 "int oneTwoThree = 123;",
18584 "int a = 5;\n"
18585 "\n"
18586 "int oneTwoThree= 123;",
18587 Alignment);
18588 verifyFormat("int a = 5;\n"
18589 "int one = 1;\n"
18590 "\n"
18591 "int oneTwoThree = 123;",
18592 "int a = 5;\n"
18593 "int one = 1;\n"
18594 "\n"
18595 "int oneTwoThree = 123;",
18596 Alignment);
18597 verifyFormat("int a = 5;\n"
18598 "int one = 1;\n"
18599 "\n"
18600 "int oneTwoThree = 123;\n"
18601 "int oneTwo = 12;",
18602 "int a = 5;\n"
18603 "int one = 1;\n"
18604 "\n"
18605 "int oneTwoThree = 123;\n"
18606 "int oneTwo = 12;",
18607 Alignment);
18608
18609 /* Test across comments */
18610 verifyFormat("int a = 5;\n"
18611 "/* block comment */\n"
18612 "int oneTwoThree = 123;",
18613 "int a = 5;\n"
18614 "/* block comment */\n"
18615 "int oneTwoThree=123;",
18616 Alignment);
18617
18618 verifyFormat("int a = 5;\n"
18619 "// line comment\n"
18620 "int oneTwoThree = 123;",
18621 "int a = 5;\n"
18622 "// line comment\n"
18623 "int oneTwoThree=123;",
18624 Alignment);
18625
18626 /* Test across comments and newlines */
18627 verifyFormat("int a = 5;\n"
18628 "\n"
18629 "/* block comment */\n"
18630 "int oneTwoThree = 123;",
18631 "int a = 5;\n"
18632 "\n"
18633 "/* block comment */\n"
18634 "int oneTwoThree=123;",
18635 Alignment);
18636
18637 verifyFormat("int a = 5;\n"
18638 "\n"
18639 "// line comment\n"
18640 "int oneTwoThree = 123;",
18641 "int a = 5;\n"
18642 "\n"
18643 "// line comment\n"
18644 "int oneTwoThree=123;",
18645 Alignment);
18646 }
18647
18648 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18649 FormatStyle Alignment = getLLVMStyle();
18650 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18651 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18652 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18653
18654 Alignment.MaxEmptyLinesToKeep = 10;
18655 /* Test alignment across empty lines */
18656 verifyFormat("int a = 5;\n"
18657 "\n"
18658 "float const oneTwoThree = 123;",
18659 "int a = 5;\n"
18660 "\n"
18661 "float const oneTwoThree = 123;",
18662 Alignment);
18663 verifyFormat("int a = 5;\n"
18664 "float const one = 1;\n"
18665 "\n"
18666 "int oneTwoThree = 123;",
18667 "int a = 5;\n"
18668 "float const one = 1;\n"
18669 "\n"
18670 "int oneTwoThree = 123;",
18671 Alignment);
18672
18673 /* Test across comments */
18674 verifyFormat("float const a = 5;\n"
18675 "/* block comment */\n"
18676 "int oneTwoThree = 123;",
18677 "float const a = 5;\n"
18678 "/* block comment */\n"
18679 "int oneTwoThree=123;",
18680 Alignment);
18681
18682 verifyFormat("float const a = 5;\n"
18683 "// line comment\n"
18684 "int oneTwoThree = 123;",
18685 "float const a = 5;\n"
18686 "// line comment\n"
18687 "int oneTwoThree=123;",
18688 Alignment);
18689
18690 /* Test across comments and newlines */
18691 verifyFormat("float const a = 5;\n"
18692 "\n"
18693 "/* block comment */\n"
18694 "int oneTwoThree = 123;",
18695 "float const a = 5;\n"
18696 "\n"
18697 "/* block comment */\n"
18698 "int oneTwoThree=123;",
18699 Alignment);
18700
18701 verifyFormat("float const a = 5;\n"
18702 "\n"
18703 "// line comment\n"
18704 "int oneTwoThree = 123;",
18705 "float const a = 5;\n"
18706 "\n"
18707 "// line comment\n"
18708 "int oneTwoThree=123;",
18709 Alignment);
18710 }
18711
18712 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18713 FormatStyle Alignment = getLLVMStyle();
18714 Alignment.AlignConsecutiveBitFields.Enabled = true;
18715 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18716 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18717
18718 Alignment.MaxEmptyLinesToKeep = 10;
18719 /* Test alignment across empty lines */
18720 verifyFormat("int a : 5;\n"
18721 "\n"
18722 "int longbitfield : 6;",
18723 "int a : 5;\n"
18724 "\n"
18725 "int longbitfield : 6;",
18726 Alignment);
18727 verifyFormat("int a : 5;\n"
18728 "int one : 1;\n"
18729 "\n"
18730 "int longbitfield : 6;",
18731 "int a : 5;\n"
18732 "int one : 1;\n"
18733 "\n"
18734 "int longbitfield : 6;",
18735 Alignment);
18736
18737 /* Test across comments */
18738 verifyFormat("int a : 5;\n"
18739 "/* block comment */\n"
18740 "int longbitfield : 6;",
18741 "int a : 5;\n"
18742 "/* block comment */\n"
18743 "int longbitfield : 6;",
18744 Alignment);
18745 verifyFormat("int a : 5;\n"
18746 "int one : 1;\n"
18747 "// line comment\n"
18748 "int longbitfield : 6;",
18749 "int a : 5;\n"
18750 "int one : 1;\n"
18751 "// line comment\n"
18752 "int longbitfield : 6;",
18753 Alignment);
18754
18755 /* Test across comments and newlines */
18756 verifyFormat("int a : 5;\n"
18757 "/* block comment */\n"
18758 "\n"
18759 "int longbitfield : 6;",
18760 "int a : 5;\n"
18761 "/* block comment */\n"
18762 "\n"
18763 "int longbitfield : 6;",
18764 Alignment);
18765 verifyFormat("int a : 5;\n"
18766 "int one : 1;\n"
18767 "\n"
18768 "// line comment\n"
18769 "\n"
18770 "int longbitfield : 6;",
18771 "int a : 5;\n"
18772 "int one : 1;\n"
18773 "\n"
18774 "// line comment \n"
18775 "\n"
18776 "int longbitfield : 6;",
18777 Alignment);
18778 }
18779
18780 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
18781 FormatStyle Alignment = getLLVMStyle();
18782 Alignment.AlignConsecutiveMacros.Enabled = true;
18783 Alignment.AlignConsecutiveAssignments.Enabled = true;
18784 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18785
18786 Alignment.MaxEmptyLinesToKeep = 10;
18787 /* Test alignment across empty lines */
18788 verifyFormat("int a = 5;\n"
18789 "\n"
18790 "int oneTwoThree = 123;",
18791 "int a = 5;\n"
18792 "\n"
18793 "int oneTwoThree= 123;",
18794 Alignment);
18795 verifyFormat("int a = 5;\n"
18796 "int one = 1;\n"
18797 "\n"
18798 "int oneTwoThree = 123;",
18799 "int a = 5;\n"
18800 "int one = 1;\n"
18801 "\n"
18802 "int oneTwoThree = 123;",
18803 Alignment);
18804
18805 /* Test across comments */
18806 verifyFormat("int a = 5;\n"
18807 "/* block comment */\n"
18808 "int oneTwoThree = 123;",
18809 "int a = 5;\n"
18810 "/* block comment */\n"
18811 "int oneTwoThree=123;",
18812 Alignment);
18813
18814 verifyFormat("int a = 5;\n"
18815 "// line comment\n"
18816 "int oneTwoThree = 123;",
18817 "int a = 5;\n"
18818 "// line comment\n"
18819 "int oneTwoThree=123;",
18820 Alignment);
18821
18822 verifyFormat("int a = 5;\n"
18823 "/*\n"
18824 " * multi-line block comment\n"
18825 " */\n"
18826 "int oneTwoThree = 123;",
18827 "int a = 5;\n"
18828 "/*\n"
18829 " * multi-line block comment\n"
18830 " */\n"
18831 "int oneTwoThree=123;",
18832 Alignment);
18833
18834 verifyFormat("int a = 5;\n"
18835 "//\n"
18836 "// multi-line line comment\n"
18837 "//\n"
18838 "int oneTwoThree = 123;",
18839 "int a = 5;\n"
18840 "//\n"
18841 "// multi-line line comment\n"
18842 "//\n"
18843 "int oneTwoThree=123;",
18844 Alignment);
18845
18846 /* Test across comments and newlines */
18847 verifyFormat("int a = 5;\n"
18848 "\n"
18849 "/* block comment */\n"
18850 "int oneTwoThree = 123;",
18851 "int a = 5;\n"
18852 "\n"
18853 "/* block comment */\n"
18854 "int oneTwoThree=123;",
18855 Alignment);
18856
18857 verifyFormat("int a = 5;\n"
18858 "\n"
18859 "// line comment\n"
18860 "int oneTwoThree = 123;",
18861 "int a = 5;\n"
18862 "\n"
18863 "// line comment\n"
18864 "int oneTwoThree=123;",
18865 Alignment);
18866 }
18867
18868 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
18869 FormatStyle Alignment = getLLVMStyle();
18870 Alignment.AlignConsecutiveMacros.Enabled = true;
18871 Alignment.AlignConsecutiveAssignments.Enabled = true;
18872 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18873 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18874 verifyFormat("int a = 5;\n"
18875 "int oneTwoThree = 123;",
18876 Alignment);
18877 verifyFormat("int a = method();\n"
18878 "int oneTwoThree = 133;",
18879 Alignment);
18880 verifyFormat("a &= 5;\n"
18881 "bcd *= 5;\n"
18882 "ghtyf += 5;\n"
18883 "dvfvdb -= 5;\n"
18884 "a /= 5;\n"
18885 "vdsvsv %= 5;\n"
18886 "sfdbddfbdfbb ^= 5;\n"
18887 "dvsdsv |= 5;\n"
18888 "int dsvvdvsdvvv = 123;",
18889 Alignment);
18890 verifyFormat("int i = 1, j = 10;\n"
18891 "something = 2000;",
18892 Alignment);
18893 verifyFormat("something = 2000;\n"
18894 "int i = 1, j = 10;",
18895 Alignment);
18896 verifyFormat("something = 2000;\n"
18897 "another = 911;\n"
18898 "int i = 1, j = 10;\n"
18899 "oneMore = 1;\n"
18900 "i = 2;",
18901 Alignment);
18902 verifyFormat("int a = 5;\n"
18903 "int one = 1;\n"
18904 "method();\n"
18905 "int oneTwoThree = 123;\n"
18906 "int oneTwo = 12;",
18907 Alignment);
18908 verifyFormat("int oneTwoThree = 123;\n"
18909 "int oneTwo = 12;\n"
18910 "method();",
18911 Alignment);
18912 verifyFormat("int oneTwoThree = 123; // comment\n"
18913 "int oneTwo = 12; // comment",
18914 Alignment);
18915
18916 // Bug 25167
18917 /* Uncomment when fixed
18918 verifyFormat("#if A\n"
18919 "#else\n"
18920 "int aaaaaaaa = 12;\n"
18921 "#endif\n"
18922 "#if B\n"
18923 "#else\n"
18924 "int a = 12;\n"
18925 "#endif",
18926 Alignment);
18927 verifyFormat("enum foo {\n"
18928 "#if A\n"
18929 "#else\n"
18930 " aaaaaaaa = 12;\n"
18931 "#endif\n"
18932 "#if B\n"
18933 "#else\n"
18934 " a = 12;\n"
18935 "#endif\n"
18936 "};",
18937 Alignment);
18938 */
18939
18940 Alignment.MaxEmptyLinesToKeep = 10;
18941 /* Test alignment across empty lines */
18942 verifyFormat("int a = 5;\n"
18943 "\n"
18944 "int oneTwoThree = 123;",
18945 "int a = 5;\n"
18946 "\n"
18947 "int oneTwoThree= 123;",
18948 Alignment);
18949 verifyFormat("int a = 5;\n"
18950 "int one = 1;\n"
18951 "\n"
18952 "int oneTwoThree = 123;",
18953 "int a = 5;\n"
18954 "int one = 1;\n"
18955 "\n"
18956 "int oneTwoThree = 123;",
18957 Alignment);
18958 verifyFormat("int a = 5;\n"
18959 "int one = 1;\n"
18960 "\n"
18961 "int oneTwoThree = 123;\n"
18962 "int oneTwo = 12;",
18963 "int a = 5;\n"
18964 "int one = 1;\n"
18965 "\n"
18966 "int oneTwoThree = 123;\n"
18967 "int oneTwo = 12;",
18968 Alignment);
18969
18970 /* Test across comments */
18971 verifyFormat("int a = 5;\n"
18972 "/* block comment */\n"
18973 "int oneTwoThree = 123;",
18974 "int a = 5;\n"
18975 "/* block comment */\n"
18976 "int oneTwoThree=123;",
18977 Alignment);
18978
18979 verifyFormat("int a = 5;\n"
18980 "// line comment\n"
18981 "int oneTwoThree = 123;",
18982 "int a = 5;\n"
18983 "// line comment\n"
18984 "int oneTwoThree=123;",
18985 Alignment);
18986
18987 /* Test across comments and newlines */
18988 verifyFormat("int a = 5;\n"
18989 "\n"
18990 "/* block comment */\n"
18991 "int oneTwoThree = 123;",
18992 "int a = 5;\n"
18993 "\n"
18994 "/* block comment */\n"
18995 "int oneTwoThree=123;",
18996 Alignment);
18997
18998 verifyFormat("int a = 5;\n"
18999 "\n"
19000 "// line comment\n"
19001 "int oneTwoThree = 123;",
19002 "int a = 5;\n"
19003 "\n"
19004 "// line comment\n"
19005 "int oneTwoThree=123;",
19006 Alignment);
19007
19008 verifyFormat("int a = 5;\n"
19009 "//\n"
19010 "// multi-line line comment\n"
19011 "//\n"
19012 "int oneTwoThree = 123;",
19013 "int a = 5;\n"
19014 "//\n"
19015 "// multi-line line comment\n"
19016 "//\n"
19017 "int oneTwoThree=123;",
19018 Alignment);
19019
19020 verifyFormat("int a = 5;\n"
19021 "/*\n"
19022 " * multi-line block comment\n"
19023 " */\n"
19024 "int oneTwoThree = 123;",
19025 "int a = 5;\n"
19026 "/*\n"
19027 " * multi-line block comment\n"
19028 " */\n"
19029 "int oneTwoThree=123;",
19030 Alignment);
19031
19032 verifyFormat("int a = 5;\n"
19033 "\n"
19034 "/* block comment */\n"
19035 "\n"
19036 "\n"
19037 "\n"
19038 "int oneTwoThree = 123;",
19039 "int a = 5;\n"
19040 "\n"
19041 "/* block comment */\n"
19042 "\n"
19043 "\n"
19044 "\n"
19045 "int oneTwoThree=123;",
19046 Alignment);
19047
19048 verifyFormat("int a = 5;\n"
19049 "\n"
19050 "// line comment\n"
19051 "\n"
19052 "\n"
19053 "\n"
19054 "int oneTwoThree = 123;",
19055 "int a = 5;\n"
19056 "\n"
19057 "// line comment\n"
19058 "\n"
19059 "\n"
19060 "\n"
19061 "int oneTwoThree=123;",
19062 Alignment);
19063
19064 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19065 verifyFormat("#define A \\\n"
19066 " int aaaa = 12; \\\n"
19067 " int b = 23; \\\n"
19068 " int ccc = 234; \\\n"
19069 " int dddddddddd = 2345;",
19070 Alignment);
19071 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19072 verifyFormat("#define A \\\n"
19073 " int aaaa = 12; \\\n"
19074 " int b = 23; \\\n"
19075 " int ccc = 234; \\\n"
19076 " int dddddddddd = 2345;",
19077 Alignment);
19078 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19079 verifyFormat("#define A "
19080 " \\\n"
19081 " int aaaa = 12; "
19082 " \\\n"
19083 " int b = 23; "
19084 " \\\n"
19085 " int ccc = 234; "
19086 " \\\n"
19087 " int dddddddddd = 2345;",
19088 Alignment);
19089 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19090 "k = 4, int l = 5,\n"
19091 " int m = 6) {\n"
19092 " int j = 10;\n"
19093 " otherThing = 1;\n"
19094 "}",
19095 Alignment);
19096 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19097 " int i = 1;\n"
19098 " int j = 2;\n"
19099 " int big = 10000;\n"
19100 "}",
19101 Alignment);
19102 verifyFormat("class C {\n"
19103 "public:\n"
19104 " int i = 1;\n"
19105 " virtual void f() = 0;\n"
19106 "};",
19107 Alignment);
19108 verifyFormat("int i = 1;\n"
19109 "if (SomeType t = getSomething()) {\n"
19110 "}\n"
19111 "int j = 2;\n"
19112 "int big = 10000;",
19113 Alignment);
19114 verifyFormat("int j = 7;\n"
19115 "for (int k = 0; k < N; ++k) {\n"
19116 "}\n"
19117 "int j = 2;\n"
19118 "int big = 10000;\n"
19119 "}",
19120 Alignment);
19121 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19122 verifyFormat("int i = 1;\n"
19123 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19124 " = someLooooooooooooooooongFunction();\n"
19125 "int j = 2;",
19126 Alignment);
19127 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19128 verifyFormat("int i = 1;\n"
19129 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19130 " someLooooooooooooooooongFunction();\n"
19131 "int j = 2;",
19132 Alignment);
19133
19134 verifyFormat("auto lambda = []() {\n"
19135 " auto i = 0;\n"
19136 " return 0;\n"
19137 "};\n"
19138 "int i = 0;\n"
19139 "auto v = type{\n"
19140 " i = 1, //\n"
19141 " (i = 2), //\n"
19142 " i = 3 //\n"
19143 "};",
19144 Alignment);
19145
19146 verifyFormat(
19147 "int i = 1;\n"
19148 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19149 " loooooooooooooooooooooongParameterB);\n"
19150 "int j = 2;",
19151 Alignment);
19152
19153 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19154 " typename B = very_long_type_name_1,\n"
19155 " typename T_2 = very_long_type_name_2>\n"
19156 "auto foo() {}",
19157 Alignment);
19158 verifyFormat("int a, b = 1;\n"
19159 "int c = 2;\n"
19160 "int dd = 3;",
19161 Alignment);
19162 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19163 "float b[1][] = {{3.f}};",
19164 Alignment);
19165 verifyFormat("for (int i = 0; i < 1; i++)\n"
19166 " int x = 1;",
19167 Alignment);
19168 verifyFormat("for (i = 0; i < 1; i++)\n"
19169 " x = 1;\n"
19170 "y = 1;",
19171 Alignment);
19172
19173 Alignment.ReflowComments = FormatStyle::RCS_Always;
19174 Alignment.ColumnLimit = 50;
19175 verifyFormat("int x = 0;\n"
19176 "int yy = 1; /// specificlennospace\n"
19177 "int zzz = 2;",
19178 "int x = 0;\n"
19179 "int yy = 1; ///specificlennospace\n"
19180 "int zzz = 2;",
19181 Alignment);
19182 }
19183
19184 TEST_F(FormatTest, AlignCompoundAssignments) {
19185 FormatStyle Alignment = getLLVMStyle();
19186 Alignment.AlignConsecutiveAssignments.Enabled = true;
19187 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
19188 Alignment.AlignConsecutiveAssignments.PadOperators = false;
19189 verifyFormat("sfdbddfbdfbb = 5;\n"
19190 "dvsdsv = 5;\n"
19191 "int dsvvdvsdvvv = 123;",
19192 Alignment);
19193 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19194 "dvsdsv |= 5;\n"
19195 "int dsvvdvsdvvv = 123;",
19196 Alignment);
19197 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19198 "dvsdsv <<= 5;\n"
19199 "int dsvvdvsdvvv = 123;",
19200 Alignment);
19201 verifyFormat("int xxx = 5;\n"
19202 "xxx = 5;\n"
19203 "{\n"
19204 " int yyy = 6;\n"
19205 " yyy = 6;\n"
19206 "}",
19207 Alignment);
19208 verifyFormat("int xxx = 5;\n"
19209 "xxx += 5;\n"
19210 "{\n"
19211 " int yyy = 6;\n"
19212 " yyy += 6;\n"
19213 "}",
19214 Alignment);
19215 // Test that `<=` is not treated as a compound assignment.
19216 verifyFormat("aa &= 5;\n"
19217 "b <= 10;\n"
19218 "c = 15;",
19219 Alignment);
19220 Alignment.AlignConsecutiveAssignments.PadOperators = true;
19221 verifyFormat("sfdbddfbdfbb = 5;\n"
19222 "dvsdsv = 5;\n"
19223 "int dsvvdvsdvvv = 123;",
19224 Alignment);
19225 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19226 "dvsdsv |= 5;\n"
19227 "int dsvvdvsdvvv = 123;",
19228 Alignment);
19229 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19230 "dvsdsv <<= 5;\n"
19231 "int dsvvdvsdvvv = 123;",
19232 Alignment);
19233 verifyFormat("a += 5;\n"
19234 "one = 1;\n"
19235 "\n"
19236 "oneTwoThree = 123;",
19237 "a += 5;\n"
19238 "one = 1;\n"
19239 "\n"
19240 "oneTwoThree = 123;",
19241 Alignment);
19242 verifyFormat("a += 5;\n"
19243 "one = 1;\n"
19244 "//\n"
19245 "oneTwoThree = 123;",
19246 "a += 5;\n"
19247 "one = 1;\n"
19248 "//\n"
19249 "oneTwoThree = 123;",
19250 Alignment);
19251 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
19252 verifyFormat("a += 5;\n"
19253 "one = 1;\n"
19254 "\n"
19255 "oneTwoThree = 123;",
19256 "a += 5;\n"
19257 "one = 1;\n"
19258 "\n"
19259 "oneTwoThree = 123;",
19260 Alignment);
19261 verifyFormat("a += 5;\n"
19262 "one = 1;\n"
19263 "//\n"
19264 "oneTwoThree = 123;",
19265 "a += 5;\n"
19266 "one = 1;\n"
19267 "//\n"
19268 "oneTwoThree = 123;",
19269 Alignment);
19270 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
19271 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
19272 verifyFormat("a += 5;\n"
19273 "one = 1;\n"
19274 "\n"
19275 "oneTwoThree = 123;",
19276 "a += 5;\n"
19277 "one = 1;\n"
19278 "\n"
19279 "oneTwoThree = 123;",
19280 Alignment);
19281 verifyFormat("a += 5;\n"
19282 "one = 1;\n"
19283 "//\n"
19284 "oneTwoThree = 123;",
19285 "a += 5;\n"
19286 "one = 1;\n"
19287 "//\n"
19288 "oneTwoThree = 123;",
19289 Alignment);
19290 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
19291 verifyFormat("a += 5;\n"
19292 "one >>= 1;\n"
19293 "\n"
19294 "oneTwoThree = 123;",
19295 "a += 5;\n"
19296 "one >>= 1;\n"
19297 "\n"
19298 "oneTwoThree = 123;",
19299 Alignment);
19300 verifyFormat("a += 5;\n"
19301 "one = 1;\n"
19302 "//\n"
19303 "oneTwoThree <<= 123;",
19304 "a += 5;\n"
19305 "one = 1;\n"
19306 "//\n"
19307 "oneTwoThree <<= 123;",
19308 Alignment);
19309 }
19310
19311 TEST_F(FormatTest, AlignConsecutiveAssignments) {
19312 FormatStyle Alignment = getLLVMStyle();
19313 Alignment.AlignConsecutiveMacros.Enabled = true;
19314 verifyFormat("int a = 5;\n"
19315 "int oneTwoThree = 123;",
19316 Alignment);
19317 verifyFormat("int a = 5;\n"
19318 "int oneTwoThree = 123;",
19319 Alignment);
19320
19321 Alignment.AlignConsecutiveAssignments.Enabled = true;
19322 verifyFormat("int a = 5;\n"
19323 "int oneTwoThree = 123;",
19324 Alignment);
19325 verifyFormat("int a = method();\n"
19326 "int oneTwoThree = 133;",
19327 Alignment);
19328 verifyFormat("aa <= 5;\n"
19329 "a &= 5;\n"
19330 "bcd *= 5;\n"
19331 "ghtyf += 5;\n"
19332 "dvfvdb -= 5;\n"
19333 "a /= 5;\n"
19334 "vdsvsv %= 5;\n"
19335 "sfdbddfbdfbb ^= 5;\n"
19336 "dvsdsv |= 5;\n"
19337 "int dsvvdvsdvvv = 123;",
19338 Alignment);
19339 verifyFormat("int i = 1, j = 10;\n"
19340 "something = 2000;",
19341 Alignment);
19342 verifyFormat("something = 2000;\n"
19343 "int i = 1, j = 10;",
19344 Alignment);
19345 verifyFormat("something = 2000;\n"
19346 "another = 911;\n"
19347 "int i = 1, j = 10;\n"
19348 "oneMore = 1;\n"
19349 "i = 2;",
19350 Alignment);
19351 verifyFormat("int a = 5;\n"
19352 "int one = 1;\n"
19353 "method();\n"
19354 "int oneTwoThree = 123;\n"
19355 "int oneTwo = 12;",
19356 Alignment);
19357 verifyFormat("int oneTwoThree = 123;\n"
19358 "int oneTwo = 12;\n"
19359 "method();",
19360 Alignment);
19361 verifyFormat("int oneTwoThree = 123; // comment\n"
19362 "int oneTwo = 12; // comment",
19363 Alignment);
19364 verifyFormat("int f() = default;\n"
19365 "int &operator() = default;\n"
19366 "int &operator=() {",
19367 Alignment);
19368 verifyFormat("int f() = delete;\n"
19369 "int &operator() = delete;\n"
19370 "int &operator=() {",
19371 Alignment);
19372 verifyFormat("int f() = default; // comment\n"
19373 "int &operator() = default; // comment\n"
19374 "int &operator=() {",
19375 Alignment);
19376 verifyFormat("int f() = default;\n"
19377 "int &operator() = default;\n"
19378 "int &operator==() {",
19379 Alignment);
19380 verifyFormat("int f() = default;\n"
19381 "int &operator() = default;\n"
19382 "int &operator<=() {",
19383 Alignment);
19384 verifyFormat("int f() = default;\n"
19385 "int &operator() = default;\n"
19386 "int &operator!=() {",
19387 Alignment);
19388 verifyFormat("int f() = default;\n"
19389 "int &operator() = default;\n"
19390 "int &operator=();",
19391 Alignment);
19392 verifyFormat("int f() = delete;\n"
19393 "int &operator() = delete;\n"
19394 "int &operator=();",
19395 Alignment);
19396 verifyFormat("/* long long padding */ int f() = default;\n"
19397 "int &operator() = default;\n"
19398 "int &operator/**/ =();",
19399 Alignment);
19400 // https://llvm.org/PR33697
19401 FormatStyle AlignmentWithPenalty = getLLVMStyle();
19402 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
19403 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
19404 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19405 " void f() = delete;\n"
19406 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19407 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19408 "};",
19409 AlignmentWithPenalty);
19410
19411 // Bug 25167
19412 /* Uncomment when fixed
19413 verifyFormat("#if A\n"
19414 "#else\n"
19415 "int aaaaaaaa = 12;\n"
19416 "#endif\n"
19417 "#if B\n"
19418 "#else\n"
19419 "int a = 12;\n"
19420 "#endif",
19421 Alignment);
19422 verifyFormat("enum foo {\n"
19423 "#if A\n"
19424 "#else\n"
19425 " aaaaaaaa = 12;\n"
19426 "#endif\n"
19427 "#if B\n"
19428 "#else\n"
19429 " a = 12;\n"
19430 "#endif\n"
19431 "};",
19432 Alignment);
19433 */
19434
19435 verifyFormat("int a = 5;\n"
19436 "\n"
19437 "int oneTwoThree = 123;",
19438 "int a = 5;\n"
19439 "\n"
19440 "int oneTwoThree= 123;",
19441 Alignment);
19442 verifyFormat("int a = 5;\n"
19443 "int one = 1;\n"
19444 "\n"
19445 "int oneTwoThree = 123;",
19446 "int a = 5;\n"
19447 "int one = 1;\n"
19448 "\n"
19449 "int oneTwoThree = 123;",
19450 Alignment);
19451 verifyFormat("int a = 5;\n"
19452 "int one = 1;\n"
19453 "\n"
19454 "int oneTwoThree = 123;\n"
19455 "int oneTwo = 12;",
19456 "int a = 5;\n"
19457 "int one = 1;\n"
19458 "\n"
19459 "int oneTwoThree = 123;\n"
19460 "int oneTwo = 12;",
19461 Alignment);
19462 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19463 verifyFormat("#define A \\\n"
19464 " int aaaa = 12; \\\n"
19465 " int b = 23; \\\n"
19466 " int ccc = 234; \\\n"
19467 " int dddddddddd = 2345;",
19468 Alignment);
19469 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19470 verifyFormat("#define A \\\n"
19471 " int aaaa = 12; \\\n"
19472 " int b = 23; \\\n"
19473 " int ccc = 234; \\\n"
19474 " int dddddddddd = 2345;",
19475 Alignment);
19476 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19477 verifyFormat("#define A "
19478 " \\\n"
19479 " int aaaa = 12; "
19480 " \\\n"
19481 " int b = 23; "
19482 " \\\n"
19483 " int ccc = 234; "
19484 " \\\n"
19485 " int dddddddddd = 2345;",
19486 Alignment);
19487 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19488 "k = 4, int l = 5,\n"
19489 " int m = 6) {\n"
19490 " int j = 10;\n"
19491 " otherThing = 1;\n"
19492 "}",
19493 Alignment);
19494 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19495 " int i = 1;\n"
19496 " int j = 2;\n"
19497 " int big = 10000;\n"
19498 "}",
19499 Alignment);
19500 verifyFormat("class C {\n"
19501 "public:\n"
19502 " int i = 1;\n"
19503 " virtual void f() = 0;\n"
19504 "};",
19505 Alignment);
19506 verifyFormat("int i = 1;\n"
19507 "if (SomeType t = getSomething()) {\n"
19508 "}\n"
19509 "int j = 2;\n"
19510 "int big = 10000;",
19511 Alignment);
19512 verifyFormat("int j = 7;\n"
19513 "for (int k = 0; k < N; ++k) {\n"
19514 "}\n"
19515 "int j = 2;\n"
19516 "int big = 10000;\n"
19517 "}",
19518 Alignment);
19519 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19520 verifyFormat("int i = 1;\n"
19521 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19522 " = someLooooooooooooooooongFunction();\n"
19523 "int j = 2;",
19524 Alignment);
19525 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19526 verifyFormat("int i = 1;\n"
19527 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19528 " someLooooooooooooooooongFunction();\n"
19529 "int j = 2;",
19530 Alignment);
19531
19532 verifyFormat("auto lambda = []() {\n"
19533 " auto i = 0;\n"
19534 " return 0;\n"
19535 "};\n"
19536 "int i = 0;\n"
19537 "auto v = type{\n"
19538 " i = 1, //\n"
19539 " (i = 2), //\n"
19540 " i = 3 //\n"
19541 "};",
19542 Alignment);
19543
19544 verifyFormat(
19545 "int i = 1;\n"
19546 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19547 " loooooooooooooooooooooongParameterB);\n"
19548 "int j = 2;",
19549 Alignment);
19550
19551 verifyFormat("int abcdefghijk = 111;\n"
19552 "auto lambda = [] {\n"
19553 " int c = call(1, //\n"
19554 " 2, //\n"
19555 " 3, //\n"
19556 " 4);\n"
19557 "};",
19558 Alignment);
19559
19560 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19561 " typename B = very_long_type_name_1,\n"
19562 " typename T_2 = very_long_type_name_2>\n"
19563 "auto foo() {}",
19564 Alignment);
19565 verifyFormat("int a, b = 1;\n"
19566 "int c = 2;\n"
19567 "int dd = 3;",
19568 Alignment);
19569 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19570 "float b[1][] = {{3.f}};",
19571 Alignment);
19572 verifyFormat("for (int i = 0; i < 1; i++)\n"
19573 " int x = 1;",
19574 Alignment);
19575 verifyFormat("for (i = 0; i < 1; i++)\n"
19576 " x = 1;\n"
19577 "y = 1;",
19578 Alignment);
19579
19580 EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always);
19581 Alignment.ColumnLimit = 50;
19582 verifyFormat("int x = 0;\n"
19583 "int yy = 1; /// specificlennospace\n"
19584 "int zzz = 2;",
19585 "int x = 0;\n"
19586 "int yy = 1; ///specificlennospace\n"
19587 "int zzz = 2;",
19588 Alignment);
19589
19590 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19591 "auto b = [] {\n"
19592 " f();\n"
19593 " return;\n"
19594 "};",
19595 Alignment);
19596 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19597 "auto b = g([] {\n"
19598 " return \"Hello \"\n"
19599 " \"World\";\n"
19600 "});",
19601 Alignment);
19602 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19603 "auto b = g([] {\n"
19604 " f();\n"
19605 " return;\n"
19606 "});",
19607 Alignment);
19608 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19609 "auto b = g(param, [] {\n"
19610 " f();\n"
19611 " return;\n"
19612 "});",
19613 Alignment);
19614 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19615 "auto b = [] {\n"
19616 " if (condition) {\n"
19617 " return;\n"
19618 " }\n"
19619 "};",
19620 Alignment);
19621
19622 // Aligning lines should not mess up the comments. However, feel free to
19623 // change the test if it turns out that comments inside the closure should not
19624 // be aligned with those outside it.
19625 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {}; //\n"
19626 "auto b = [] { //\n"
19627 " return; //\n"
19628 "};",
19629 Alignment);
19630 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {}; //\n"
19631 "auto b = [] { //\n"
19632 " return aaaaaaaaaaaaaaaaaaaaa; //\n"
19633 "};",
19634 Alignment);
19635 verifyFormat("auto aaaaaaaaaaaaaaa = {}; //\n"
19636 "auto b = [] { //\n"
19637 " return aaaaaaaaaaaaaaaaaaaaa; //\n"
19638 "};",
19639 Alignment);
19640
19641 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19642 " ccc ? aaaaa : bbbbb,\n"
19643 " dddddddddddddddddddddddddd);",
19644 Alignment);
19645 verifyFormat("auto aaaaaaaaaaaa = f();\n"
19646 "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19647 " ccc ? aaaaa : bbbbb,\n"
19648 " dddddddddddddddddddddddddd);",
19649 Alignment);
19650
19651 // Confirm proper handling of AlignConsecutiveAssignments with
19652 // BinPackArguments.
19653 // See https://llvm.org/PR55360
19654 Alignment = getLLVMStyleWithColumns(50);
19655 Alignment.AlignConsecutiveAssignments.Enabled = true;
19656 Alignment.BinPackArguments = false;
19657 verifyFormat("int a_long_name = 1;\n"
19658 "auto b = B({a_long_name, a_long_name},\n"
19659 " {a_longer_name_for_wrap,\n"
19660 " a_longer_name_for_wrap});",
19661 Alignment);
19662 verifyFormat("int a_long_name = 1;\n"
19663 "auto b = B{{a_long_name, a_long_name},\n"
19664 " {a_longer_name_for_wrap,\n"
19665 " a_longer_name_for_wrap}};",
19666 Alignment);
19667
19668 Alignment = getLLVMStyleWithColumns(60);
19669 Alignment.AlignConsecutiveAssignments.Enabled = true;
19670 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19671 "using I = std::conditional_t<II::value >= 0,\n"
19672 " std::ic<int, II::value + 1>,\n"
19673 " std::ic<int, -1>>;",
19674 Alignment);
19675 verifyFormat("SomeName = Foo;\n"
19676 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19677 " arrrrrrrrrrg);",
19678 Alignment);
19679
19680 Alignment.ColumnLimit = 80;
19681 Alignment.SpacesInAngles = FormatStyle::SIAS_Always;
19682 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19683 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19684 Alignment);
19685 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19686 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19687 Alignment);
19688
19689 Alignment.SpacesInParens = FormatStyle::SIPO_Custom;
19690 Alignment.SpacesInParensOptions.InCStyleCasts = true;
19691 verifyFormat("void **ptr = ( void ** )unkn;\n"
19692 "ptr = ( void ** )ptr[0];",
19693 Alignment);
19694 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19695 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19696 Alignment);
19697 }
19698
19699 TEST_F(FormatTest, AlignConsecutiveBitFields) {
19700 FormatStyle Alignment = getLLVMStyle();
19701 Alignment.AlignConsecutiveBitFields.Enabled = true;
19702 verifyFormat("int const a : 5;\n"
19703 "int oneTwoThree : 23;",
19704 Alignment);
19705
19706 // Initializers are allowed starting with c++2a
19707 verifyFormat("int const a : 5 = 1;\n"
19708 "int oneTwoThree : 23 = 0;",
19709 Alignment);
19710
19711 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19712 verifyFormat("int const a : 5;\n"
19713 "int oneTwoThree : 23;",
19714 Alignment);
19715
19716 verifyFormat("int const a : 5; // comment\n"
19717 "int oneTwoThree : 23; // comment",
19718 Alignment);
19719
19720 verifyFormat("int const a : 5 = 1;\n"
19721 "int oneTwoThree : 23 = 0;",
19722 Alignment);
19723
19724 Alignment.AlignConsecutiveAssignments.Enabled = true;
19725 verifyFormat("int const a : 5 = 1;\n"
19726 "int oneTwoThree : 23 = 0;",
19727 Alignment);
19728 verifyFormat("int const a : 5 = {1};\n"
19729 "int oneTwoThree : 23 = 0;",
19730 Alignment);
19731
19732 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19733 verifyFormat("int const a :5;\n"
19734 "int oneTwoThree:23;",
19735 Alignment);
19736
19737 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19738 verifyFormat("int const a :5;\n"
19739 "int oneTwoThree :23;",
19740 Alignment);
19741
19742 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19743 verifyFormat("int const a : 5;\n"
19744 "int oneTwoThree: 23;",
19745 Alignment);
19746
19747 // Known limitations: ':' is only recognized as a bitfield colon when
19748 // followed by a number.
19749 /*
19750 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19751 "int a : 5;",
19752 Alignment);
19753 */
19754 }
19755
19756 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19757 FormatStyle Alignment = getLLVMStyle();
19758 Alignment.AlignConsecutiveMacros.Enabled = true;
19759 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19760 verifyFormat("float const a = 5;\n"
19761 "int oneTwoThree = 123;",
19762 Alignment);
19763 verifyFormat("int a = 5;\n"
19764 "float const oneTwoThree = 123;",
19765 Alignment);
19766
19767 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19768 verifyFormat("float const a = 5;\n"
19769 "int oneTwoThree = 123;",
19770 Alignment);
19771 verifyFormat("int a = method();\n"
19772 "float const oneTwoThree = 133;",
19773 Alignment);
19774 verifyFormat("int i = 1, j = 10;\n"
19775 "something = 2000;",
19776 Alignment);
19777 verifyFormat("something = 2000;\n"
19778 "int i = 1, j = 10;",
19779 Alignment);
19780 verifyFormat("float something = 2000;\n"
19781 "double another = 911;\n"
19782 "int i = 1, j = 10;\n"
19783 "const int *oneMore = 1;\n"
19784 "unsigned i = 2;",
19785 Alignment);
19786 verifyFormat("float a = 5;\n"
19787 "int one = 1;\n"
19788 "method();\n"
19789 "const double oneTwoThree = 123;\n"
19790 "const unsigned int oneTwo = 12;",
19791 Alignment);
19792 verifyFormat("int oneTwoThree{0}; // comment\n"
19793 "unsigned oneTwo; // comment",
19794 Alignment);
19795 verifyFormat("unsigned int *a;\n"
19796 "int *b;\n"
19797 "unsigned int Const *c;\n"
19798 "unsigned int const *d;\n"
19799 "unsigned int Const &e;\n"
19800 "unsigned int const &f;",
19801 Alignment);
19802 verifyFormat("Const unsigned int *c;\n"
19803 "const unsigned int *d;\n"
19804 "Const unsigned int &e;\n"
19805 "const unsigned int &f;\n"
19806 "const unsigned g;\n"
19807 "Const unsigned h;",
19808 Alignment);
19809 verifyFormat("float const a = 5;\n"
19810 "\n"
19811 "int oneTwoThree = 123;",
19812 "float const a = 5;\n"
19813 "\n"
19814 "int oneTwoThree= 123;",
19815 Alignment);
19816 verifyFormat("float a = 5;\n"
19817 "int one = 1;\n"
19818 "\n"
19819 "unsigned oneTwoThree = 123;",
19820 "float a = 5;\n"
19821 "int one = 1;\n"
19822 "\n"
19823 "unsigned oneTwoThree = 123;",
19824 Alignment);
19825 verifyFormat("float a = 5;\n"
19826 "int one = 1;\n"
19827 "\n"
19828 "unsigned oneTwoThree = 123;\n"
19829 "int oneTwo = 12;",
19830 "float a = 5;\n"
19831 "int one = 1;\n"
19832 "\n"
19833 "unsigned oneTwoThree = 123;\n"
19834 "int oneTwo = 12;",
19835 Alignment);
19836 // Function prototype alignment
19837 verifyFormat("int a();\n"
19838 "double b();",
19839 Alignment);
19840 verifyFormat("int a(int x);\n"
19841 "double b();",
19842 Alignment);
19843 verifyFormat("int a(const Test & = Test());\n"
19844 "int a1(int &foo, const Test & = Test());\n"
19845 "int a2(int &foo, const Test &name = Test());\n"
19846 "double b();",
19847 Alignment);
19848 verifyFormat("struct Test {\n"
19849 " Test(const Test &) = default;\n"
19850 " ~Test() = default;\n"
19851 " Test &operator=(const Test &) = default;\n"
19852 "};",
19853 Alignment);
19854
19855 // The comment to the right should still align right.
19856 verifyFormat("void foo(int name, // name\n"
19857 " float name, // name\n"
19858 " int name) // name\n"
19859 "{}",
19860 Alignment);
19861
19862 unsigned OldColumnLimit = Alignment.ColumnLimit;
19863 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19864 // otherwise the function parameters will be re-flowed onto a single line.
19865 Alignment.ColumnLimit = 0;
19866 verifyFormat("int a(int x,\n"
19867 " float y);\n"
19868 "double b(int x,\n"
19869 " double y);",
19870 "int a(int x,\n"
19871 " float y);\n"
19872 "double b(int x,\n"
19873 " double y);",
19874 Alignment);
19875 // This ensures that function parameters of function declarations are
19876 // correctly indented when their owning functions are indented.
19877 // The failure case here is for 'double y' to not be indented enough.
19878 verifyFormat("double a(int x);\n"
19879 "int b(int y,\n"
19880 " double z);",
19881 "double a(int x);\n"
19882 "int b(int y,\n"
19883 " double z);",
19884 Alignment);
19885 // Set ColumnLimit low so that we induce wrapping immediately after
19886 // the function name and opening paren.
19887 Alignment.ColumnLimit = 13;
19888 verifyFormat("int function(\n"
19889 " int x,\n"
19890 " bool y);",
19891 Alignment);
19892 // Set ColumnLimit low so that we break the argument list in multiple lines.
19893 Alignment.ColumnLimit = 35;
19894 verifyFormat("int a3(SomeTypeName1 &x,\n"
19895 " SomeTypeName2 &y,\n"
19896 " const Test & = Test());\n"
19897 "double b();",
19898 Alignment);
19899 Alignment.ColumnLimit = OldColumnLimit;
19900 // Ensure function pointers don't screw up recursive alignment
19901 verifyFormat("int a(int x, void (*fp)(int y));\n"
19902 "double b();",
19903 Alignment);
19904 Alignment.AlignConsecutiveAssignments.Enabled = true;
19905 verifyFormat("struct Test {\n"
19906 " Test(const Test &) = default;\n"
19907 " ~Test() = default;\n"
19908 " Test &operator=(const Test &) = default;\n"
19909 "};",
19910 Alignment);
19911 // Ensure recursive alignment is broken by function braces, so that the
19912 // "a = 1" does not align with subsequent assignments inside the function
19913 // body.
19914 verifyFormat("int func(int a = 1) {\n"
19915 " int b = 2;\n"
19916 " int cc = 3;\n"
19917 "}",
19918 Alignment);
19919 verifyFormat("float something = 2000;\n"
19920 "double another = 911;\n"
19921 "int i = 1, j = 10;\n"
19922 "const int *oneMore = 1;\n"
19923 "unsigned i = 2;",
19924 Alignment);
19925 verifyFormat("int oneTwoThree = {0}; // comment\n"
19926 "unsigned oneTwo = 0; // comment",
19927 Alignment);
19928 // Make sure that scope is correctly tracked, in the absence of braces
19929 verifyFormat("for (int i = 0; i < n; i++)\n"
19930 " j = i;\n"
19931 "double x = 1;",
19932 Alignment);
19933 verifyFormat("if (int i = 0)\n"
19934 " j = i;\n"
19935 "double x = 1;",
19936 Alignment);
19937 // Ensure operator[] and operator() are comprehended
19938 verifyFormat("struct test {\n"
19939 " long long int foo();\n"
19940 " int operator[](int a);\n"
19941 " double bar();\n"
19942 "};",
19943 Alignment);
19944 verifyFormat("struct test {\n"
19945 " long long int foo();\n"
19946 " int operator()(int a);\n"
19947 " double bar();\n"
19948 "};",
19949 Alignment);
19950 // http://llvm.org/PR52914
19951 verifyFormat("char *a[] = {\"a\", // comment\n"
19952 " \"bb\"};\n"
19953 "int bbbbbbb = 0;",
19954 Alignment);
19955 // http://llvm.org/PR68079
19956 verifyFormat("using Fn = int (A::*)();\n"
19957 "using RFn = int (A::*)() &;\n"
19958 "using RRFn = int (A::*)() &&;",
19959 Alignment);
19960 verifyFormat("using Fn = int (A::*)();\n"
19961 "using RFn = int *(A::*)() &;\n"
19962 "using RRFn = double (A::*)() &&;",
19963 Alignment);
19964
19965 // PAS_Right
19966 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19967 " int const i = 1;\n"
19968 " int *j = 2;\n"
19969 " int big = 10000;\n"
19970 "\n"
19971 " unsigned oneTwoThree = 123;\n"
19972 " int oneTwo = 12;\n"
19973 " method();\n"
19974 " float k = 2;\n"
19975 " int ll = 10000;\n"
19976 "}",
19977 "void SomeFunction(int parameter= 0) {\n"
19978 " int const i= 1;\n"
19979 " int *j=2;\n"
19980 " int big = 10000;\n"
19981 "\n"
19982 "unsigned oneTwoThree =123;\n"
19983 "int oneTwo = 12;\n"
19984 " method();\n"
19985 "float k= 2;\n"
19986 "int ll=10000;\n"
19987 "}",
19988 Alignment);
19989 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19990 " int const i = 1;\n"
19991 " int **j = 2, ***k;\n"
19992 " int &k = i;\n"
19993 " int &&l = i + j;\n"
19994 " int big = 10000;\n"
19995 "\n"
19996 " unsigned oneTwoThree = 123;\n"
19997 " int oneTwo = 12;\n"
19998 " method();\n"
19999 " float k = 2;\n"
20000 " int ll = 10000;\n"
20001 "}",
20002 "void SomeFunction(int parameter= 0) {\n"
20003 " int const i= 1;\n"
20004 " int **j=2,***k;\n"
20005 "int &k=i;\n"
20006 "int &&l=i+j;\n"
20007 " int big = 10000;\n"
20008 "\n"
20009 "unsigned oneTwoThree =123;\n"
20010 "int oneTwo = 12;\n"
20011 " method();\n"
20012 "float k= 2;\n"
20013 "int ll=10000;\n"
20014 "}",
20015 Alignment);
20016 // variables are aligned at their name, pointers are at the right most
20017 // position
20018 verifyFormat("int *a;\n"
20019 "int **b;\n"
20020 "int ***c;\n"
20021 "int foobar;",
20022 Alignment);
20023
20024 // PAS_Left
20025 FormatStyle AlignmentLeft = Alignment;
20026 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
20027 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20028 " int const i = 1;\n"
20029 " int* j = 2;\n"
20030 " int big = 10000;\n"
20031 "\n"
20032 " unsigned oneTwoThree = 123;\n"
20033 " int oneTwo = 12;\n"
20034 " method();\n"
20035 " float k = 2;\n"
20036 " int ll = 10000;\n"
20037 "}",
20038 "void SomeFunction(int parameter= 0) {\n"
20039 " int const i= 1;\n"
20040 " int *j=2;\n"
20041 " int big = 10000;\n"
20042 "\n"
20043 "unsigned oneTwoThree =123;\n"
20044 "int oneTwo = 12;\n"
20045 " method();\n"
20046 "float k= 2;\n"
20047 "int ll=10000;\n"
20048 "}",
20049 AlignmentLeft);
20050 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20051 " int const i = 1;\n"
20052 " int** j = 2;\n"
20053 " int& k = i;\n"
20054 " int&& l = i + j;\n"
20055 " int big = 10000;\n"
20056 "\n"
20057 " unsigned oneTwoThree = 123;\n"
20058 " int oneTwo = 12;\n"
20059 " method();\n"
20060 " float k = 2;\n"
20061 " int ll = 10000;\n"
20062 "}",
20063 "void SomeFunction(int parameter= 0) {\n"
20064 " int const i= 1;\n"
20065 " int **j=2;\n"
20066 "int &k=i;\n"
20067 "int &&l=i+j;\n"
20068 " int big = 10000;\n"
20069 "\n"
20070 "unsigned oneTwoThree =123;\n"
20071 "int oneTwo = 12;\n"
20072 " method();\n"
20073 "float k= 2;\n"
20074 "int ll=10000;\n"
20075 "}",
20076 AlignmentLeft);
20077 // variables are aligned at their name, pointers are at the left most position
20078 verifyFormat("int* a;\n"
20079 "int** b;\n"
20080 "int*** c;\n"
20081 "int foobar;",
20082 AlignmentLeft);
20083
20084 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
20085 "double b();",
20086 AlignmentLeft);
20087
20088 auto Style = AlignmentLeft;
20089 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
20090 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
20091 verifyFormat("int function_name(const wchar_t* title,\n"
20092 " int x = 0,\n"
20093 " long extraStyle = 0,\n"
20094 " bool readOnly = false,\n"
20095 " FancyClassType* module = nullptr);",
20096 Style);
20097
20098 // PAS_Middle
20099 FormatStyle AlignmentMiddle = Alignment;
20100 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
20101 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20102 " int const i = 1;\n"
20103 " int * j = 2;\n"
20104 " int big = 10000;\n"
20105 "\n"
20106 " unsigned oneTwoThree = 123;\n"
20107 " int oneTwo = 12;\n"
20108 " method();\n"
20109 " float k = 2;\n"
20110 " int ll = 10000;\n"
20111 "}",
20112 "void SomeFunction(int parameter= 0) {\n"
20113 " int const i= 1;\n"
20114 " int *j=2;\n"
20115 " int big = 10000;\n"
20116 "\n"
20117 "unsigned oneTwoThree =123;\n"
20118 "int oneTwo = 12;\n"
20119 " method();\n"
20120 "float k= 2;\n"
20121 "int ll=10000;\n"
20122 "}",
20123 AlignmentMiddle);
20124 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20125 " int const i = 1;\n"
20126 " int ** j = 2, ***k;\n"
20127 " int & k = i;\n"
20128 " int && l = i + j;\n"
20129 " int big = 10000;\n"
20130 "\n"
20131 " unsigned oneTwoThree = 123;\n"
20132 " int oneTwo = 12;\n"
20133 " method();\n"
20134 " float k = 2;\n"
20135 " int ll = 10000;\n"
20136 "}",
20137 "void SomeFunction(int parameter= 0) {\n"
20138 " int const i= 1;\n"
20139 " int **j=2,***k;\n"
20140 "int &k=i;\n"
20141 "int &&l=i+j;\n"
20142 " int big = 10000;\n"
20143 "\n"
20144 "unsigned oneTwoThree =123;\n"
20145 "int oneTwo = 12;\n"
20146 " method();\n"
20147 "float k= 2;\n"
20148 "int ll=10000;\n"
20149 "}",
20150 AlignmentMiddle);
20151 // variables are aligned at their name, pointers are in the middle
20152 verifyFormat("int * a;\n"
20153 "int * b;\n"
20154 "int *** c;\n"
20155 "int foobar;",
20156 AlignmentMiddle);
20157
20158 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
20159 "double b();",
20160 AlignmentMiddle);
20161
20162 Alignment.AlignConsecutiveAssignments.Enabled = false;
20163 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
20164 verifyFormat("#define A \\\n"
20165 " int aaaa = 12; \\\n"
20166 " float b = 23; \\\n"
20167 " const int ccc = 234; \\\n"
20168 " unsigned dddddddddd = 2345;",
20169 Alignment);
20170 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
20171 verifyFormat("#define A \\\n"
20172 " int aaaa = 12; \\\n"
20173 " float b = 23; \\\n"
20174 " const int ccc = 234; \\\n"
20175 " unsigned dddddddddd = 2345;",
20176 Alignment);
20177 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
20178 Alignment.ColumnLimit = 30;
20179 verifyFormat("#define A \\\n"
20180 " int aaaa = 12; \\\n"
20181 " float b = 23; \\\n"
20182 " const int ccc = 234; \\\n"
20183 " int dddddddddd = 2345;",
20184 Alignment);
20185 Alignment.ColumnLimit = 80;
20186 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
20187 "k = 4, int l = 5,\n"
20188 " int m = 6) {\n"
20189 " const int j = 10;\n"
20190 " otherThing = 1;\n"
20191 "}",
20192 Alignment);
20193 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20194 " int const i = 1;\n"
20195 " int *j = 2;\n"
20196 " int big = 10000;\n"
20197 "}",
20198 Alignment);
20199 verifyFormat("class C {\n"
20200 "public:\n"
20201 " int i = 1;\n"
20202 " virtual void f() = 0;\n"
20203 "};",
20204 Alignment);
20205 verifyFormat("float i = 1;\n"
20206 "if (SomeType t = getSomething()) {\n"
20207 "}\n"
20208 "const unsigned j = 2;\n"
20209 "int big = 10000;",
20210 Alignment);
20211 verifyFormat("float j = 7;\n"
20212 "for (int k = 0; k < N; ++k) {\n"
20213 "}\n"
20214 "unsigned j = 2;\n"
20215 "int big = 10000;\n"
20216 "}",
20217 Alignment);
20218 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
20219 verifyFormat("float i = 1;\n"
20220 "LooooooooooongType loooooooooooooooooooooongVariable\n"
20221 " = someLooooooooooooooooongFunction();\n"
20222 "int j = 2;",
20223 Alignment);
20224 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
20225 verifyFormat("int i = 1;\n"
20226 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
20227 " someLooooooooooooooooongFunction();\n"
20228 "int j = 2;",
20229 Alignment);
20230
20231 Alignment.AlignConsecutiveAssignments.Enabled = true;
20232 verifyFormat("auto lambda = []() {\n"
20233 " auto ii = 0;\n"
20234 " float j = 0;\n"
20235 " return 0;\n"
20236 "};\n"
20237 "int i = 0;\n"
20238 "float i2 = 0;\n"
20239 "auto v = type{\n"
20240 " i = 1, //\n"
20241 " (i = 2), //\n"
20242 " i = 3 //\n"
20243 "};",
20244 Alignment);
20245 // When assignments are nested, each level should be aligned.
20246 verifyFormat("float i2 = 0;\n"
20247 "auto v = type{i2 = 1, //\n"
20248 " i = 3};",
20249 Alignment);
20250 Alignment.AlignConsecutiveAssignments.Enabled = false;
20251
20252 verifyFormat(
20253 "int i = 1;\n"
20254 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
20255 " loooooooooooooooooooooongParameterB);\n"
20256 "int j = 2;",
20257 Alignment);
20258
20259 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
20260 // We expect declarations and assignments to align, as long as it doesn't
20261 // exceed the column limit, starting a new alignment sequence whenever it
20262 // happens.
20263 Alignment.AlignConsecutiveAssignments.Enabled = true;
20264 Alignment.ColumnLimit = 30;
20265 verifyFormat("float ii = 1;\n"
20266 "unsigned j = 2;\n"
20267 "int someVerylongVariable = 1;\n"
20268 "AnotherLongType ll = 123456;\n"
20269 "VeryVeryLongType k = 2;\n"
20270 "int myvar = 1;",
20271 Alignment);
20272 Alignment.ColumnLimit = 80;
20273 Alignment.AlignConsecutiveAssignments.Enabled = false;
20274
20275 verifyFormat(
20276 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
20277 " typename LongType, typename B>\n"
20278 "auto foo() {}",
20279 Alignment);
20280 verifyFormat("float a, b = 1;\n"
20281 "int c = 2;\n"
20282 "int dd = 3;",
20283 Alignment);
20284 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
20285 "float b[1][] = {{3.f}};",
20286 Alignment);
20287 Alignment.AlignConsecutiveAssignments.Enabled = true;
20288 verifyFormat("float a, b = 1;\n"
20289 "int c = 2;\n"
20290 "int dd = 3;",
20291 Alignment);
20292 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
20293 "float b[1][] = {{3.f}};",
20294 Alignment);
20295 Alignment.AlignConsecutiveAssignments.Enabled = false;
20296
20297 Alignment.ColumnLimit = 30;
20298 Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine;
20299 verifyFormat("void foo(float a,\n"
20300 " float b,\n"
20301 " int c,\n"
20302 " uint32_t *d) {\n"
20303 " int *e = 0;\n"
20304 " float f = 0;\n"
20305 " double g = 0;\n"
20306 "}\n"
20307 "void bar(ino_t a,\n"
20308 " int b,\n"
20309 " uint32_t *c,\n"
20310 " bool d) {}",
20311 Alignment);
20312 Alignment.BinPackParameters = FormatStyle::BPPS_BinPack;
20313 Alignment.ColumnLimit = 80;
20314
20315 // Bug 33507
20316 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
20317 verifyFormat(
20318 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
20319 " static const Version verVs2017;\n"
20320 " return true;\n"
20321 "});",
20322 Alignment);
20323 Alignment.PointerAlignment = FormatStyle::PAS_Right;
20324
20325 // See llvm.org/PR35641
20326 Alignment.AlignConsecutiveDeclarations.Enabled = true;
20327 verifyFormat("int func() { //\n"
20328 " int b;\n"
20329 " unsigned c;\n"
20330 "}",
20331 Alignment);
20332
20333 // See PR37175
20334 Style = getMozillaStyle();
20335 Style.AlignConsecutiveDeclarations.Enabled = true;
20336 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
20337 "foo(int a);",
20338 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
20339
20340 Alignment.PointerAlignment = FormatStyle::PAS_Left;
20341 verifyFormat("unsigned int* a;\n"
20342 "int* b;\n"
20343 "unsigned int Const* c;\n"
20344 "unsigned int const* d;\n"
20345 "unsigned int Const& e;\n"
20346 "unsigned int const& f;",
20347 Alignment);
20348 verifyFormat("Const unsigned int* c;\n"
20349 "const unsigned int* d;\n"
20350 "Const unsigned int& e;\n"
20351 "const unsigned int& f;\n"
20352 "const unsigned g;\n"
20353 "Const unsigned h;",
20354 Alignment);
20355
20356 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
20357 verifyFormat("unsigned int * a;\n"
20358 "int * b;\n"
20359 "unsigned int Const * c;\n"
20360 "unsigned int const * d;\n"
20361 "unsigned int Const & e;\n"
20362 "unsigned int const & f;",
20363 Alignment);
20364 verifyFormat("Const unsigned int * c;\n"
20365 "const unsigned int * d;\n"
20366 "Const unsigned int & e;\n"
20367 "const unsigned int & f;\n"
20368 "const unsigned g;\n"
20369 "Const unsigned h;",
20370 Alignment);
20371
20372 // See PR46529
20373 FormatStyle BracedAlign = getLLVMStyle();
20374 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
20375 verifyFormat("const auto result{[]() {\n"
20376 " const auto something = 1;\n"
20377 " return 2;\n"
20378 "}};",
20379 BracedAlign);
20380 verifyFormat("int foo{[]() {\n"
20381 " int bar{0};\n"
20382 " return 0;\n"
20383 "}()};",
20384 BracedAlign);
20385 BracedAlign.Cpp11BracedListStyle = FormatStyle::BLS_Block;
20386 verifyFormat("const auto result{ []() {\n"
20387 " const auto something = 1;\n"
20388 " return 2;\n"
20389 "} };",
20390 BracedAlign);
20391 verifyFormat("const volatile auto result{ []() {\n"
20392 " const auto something = 1;\n"
20393 " return 2;\n"
20394 "} };",
20395 BracedAlign);
20396 verifyFormat("int foo{ []() {\n"
20397 " int bar{ 0 };\n"
20398 " return 0;\n"
20399 "}() };",
20400 BracedAlign);
20401
20402 Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
20403 verifyFormat("unsigned int f1(void);\n"
20404 "void f2(void);\n"
20405 "size_t f3(void);",
20406 Alignment);
20407 }
20408
20409 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
20410 FormatStyle Alignment = getLLVMStyle();
20411 Alignment.AllowShortCaseLabelsOnASingleLine = true;
20412 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
20413
20414 verifyFormat("switch (level) {\n"
20415 "case log::info: return \"info\";\n"
20416 "case log::warning: return \"warning\";\n"
20417 "default: return \"default\";\n"
20418 "}",
20419 Alignment);
20420
20421 verifyFormat("switch (level) {\n"
20422 "case log::info: return \"info\";\n"
20423 "case log::warning: return \"warning\";\n"
20424 "}",
20425 "switch (level) {\n"
20426 "case log::info: return \"info\";\n"
20427 "case log::warning:\n"
20428 " return \"warning\";\n"
20429 "}",
20430 Alignment);
20431
20432 // Empty case statements push out the alignment, but non-short case labels
20433 // don't.
20434 verifyFormat("switch (level) {\n"
20435 "case log::info: return \"info\";\n"
20436 "case log::critical:\n"
20437 "case log::warning:\n"
20438 "case log::severe: return \"severe\";\n"
20439 "case log::extra_severe:\n"
20440 " // comment\n"
20441 " return \"extra_severe\";\n"
20442 "}",
20443 Alignment);
20444
20445 // Verify comments and empty lines break the alignment.
20446 verifyNoChange("switch (level) {\n"
20447 "case log::info: return \"info\";\n"
20448 "case log::warning: return \"warning\";\n"
20449 "// comment\n"
20450 "case log::critical: return \"critical\";\n"
20451 "default: return \"default\";\n"
20452 "\n"
20453 "case log::severe: return \"severe\";\n"
20454 "}",
20455 Alignment);
20456
20457 // Empty case statements don't break the alignment, and potentially push it
20458 // out.
20459 verifyFormat("switch (level) {\n"
20460 "case log::info: return \"info\";\n"
20461 "case log::warning:\n"
20462 "case log::critical:\n"
20463 "default: return \"default\";\n"
20464 "}",
20465 Alignment);
20466
20467 // Implicit fallthrough cases can be aligned with either a comment or
20468 // [[fallthrough]]
20469 verifyFormat("switch (level) {\n"
20470 "case log::info: return \"info\";\n"
20471 "case log::warning: // fallthrough\n"
20472 "case log::error: return \"error\";\n"
20473 "case log::critical: /*fallthrough*/\n"
20474 "case log::severe: return \"severe\";\n"
20475 "case log::diag: [[fallthrough]];\n"
20476 "default: return \"default\";\n"
20477 "}",
20478 Alignment);
20479
20480 // Verify trailing comment that needs a reflow also gets aligned properly.
20481 verifyFormat("switch (level) {\n"
20482 "case log::info: return \"info\";\n"
20483 "case log::warning: // fallthrough\n"
20484 "case log::error: return \"error\";\n"
20485 "}",
20486 "switch (level) {\n"
20487 "case log::info: return \"info\";\n"
20488 "case log::warning: //fallthrough\n"
20489 "case log::error: return \"error\";\n"
20490 "}",
20491 Alignment);
20492
20493 // Verify adjacent non-short case statements don't change the alignment, and
20494 // properly break the set of consecutive statements.
20495 verifyFormat("switch (level) {\n"
20496 "case log::critical:\n"
20497 " // comment\n"
20498 " return \"critical\";\n"
20499 "case log::info: return \"info\";\n"
20500 "case log::warning: return \"warning\";\n"
20501 "default:\n"
20502 " // comment\n"
20503 " return \"\";\n"
20504 "case log::error: return \"error\";\n"
20505 "case log::severe: return \"severe\";\n"
20506 "case log::extra_critical:\n"
20507 " // comment\n"
20508 " return \"extra critical\";\n"
20509 "}",
20510 Alignment);
20511
20512 Alignment.SpaceBeforeCaseColon = true;
20513 verifyFormat("switch (level) {\n"
20514 "case log::info : return \"info\";\n"
20515 "case log::warning : return \"warning\";\n"
20516 "default : return \"default\";\n"
20517 "}",
20518 Alignment);
20519 Alignment.SpaceBeforeCaseColon = false;
20520
20521 // Make sure we don't incorrectly align correctly across nested switch cases.
20522 verifyFormat("switch (level) {\n"
20523 "case log::info: return \"info\";\n"
20524 "case log::warning: return \"warning\";\n"
20525 "case log::other:\n"
20526 " switch (sublevel) {\n"
20527 " case log::info: return \"info\";\n"
20528 " case log::warning: return \"warning\";\n"
20529 " }\n"
20530 " break;\n"
20531 "case log::error: return \"error\";\n"
20532 "default: return \"default\";\n"
20533 "}",
20534 "switch (level) {\n"
20535 "case log::info: return \"info\";\n"
20536 "case log::warning: return \"warning\";\n"
20537 "case log::other: switch (sublevel) {\n"
20538 " case log::info: return \"info\";\n"
20539 " case log::warning: return \"warning\";\n"
20540 "}\n"
20541 "break;\n"
20542 "case log::error: return \"error\";\n"
20543 "default: return \"default\";\n"
20544 "}",
20545 Alignment);
20546
20547 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
20548
20549 verifyFormat("switch (level) {\n"
20550 "case log::info: return \"info\";\n"
20551 "\n"
20552 "case log::warning: return \"warning\";\n"
20553 "}",
20554 "switch (level) {\n"
20555 "case log::info: return \"info\";\n"
20556 "\n"
20557 "case log::warning: return \"warning\";\n"
20558 "}",
20559 Alignment);
20560
20561 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
20562
20563 verifyNoChange("switch (level) {\n"
20564 "case log::info: return \"info\";\n"
20565 "\n"
20566 "/* block comment */\n"
20567 "\n"
20568 "// line comment\n"
20569 "case log::warning: return \"warning\";\n"
20570 "}",
20571 Alignment);
20572
20573 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
20574
20575 verifyFormat("switch (level) {\n"
20576 "case log::info: return \"info\";\n"
20577 "//\n"
20578 "case log::warning: return \"warning\";\n"
20579 "}",
20580 Alignment);
20581
20582 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
20583
20584 verifyFormat("switch (level) {\n"
20585 "case log::info : return \"info\";\n"
20586 "case log::warning: return \"warning\";\n"
20587 "default : return \"default\";\n"
20588 "}",
20589 Alignment);
20590
20591 // With AlignCaseColons, empty case statements don't break alignment of
20592 // consecutive case statements (and are aligned).
20593 verifyFormat("switch (level) {\n"
20594 "case log::info : return \"info\";\n"
20595 "case log::warning :\n"
20596 "case log::critical:\n"
20597 "default : return \"default\";\n"
20598 "}",
20599 Alignment);
20600
20601 // Final non-short case labels shouldn't have their colon aligned
20602 verifyFormat("switch (level) {\n"
20603 "case log::info : return \"info\";\n"
20604 "case log::warning :\n"
20605 "case log::critical:\n"
20606 "case log::severe : return \"severe\";\n"
20607 "default:\n"
20608 " // comment\n"
20609 " return \"default\";\n"
20610 "}",
20611 Alignment);
20612
20613 // Verify adjacent non-short case statements break the set of consecutive
20614 // alignments and aren't aligned with adjacent non-short case statements if
20615 // AlignCaseColons is set.
20616 verifyFormat("switch (level) {\n"
20617 "case log::critical:\n"
20618 " // comment\n"
20619 " return \"critical\";\n"
20620 "case log::info : return \"info\";\n"
20621 "case log::warning: return \"warning\";\n"
20622 "default:\n"
20623 " // comment\n"
20624 " return \"\";\n"
20625 "case log::error : return \"error\";\n"
20626 "case log::severe: return \"severe\";\n"
20627 "case log::extra_critical:\n"
20628 " // comment\n"
20629 " return \"extra critical\";\n"
20630 "}",
20631 Alignment);
20632
20633 Alignment.SpaceBeforeCaseColon = true;
20634 verifyFormat("switch (level) {\n"
20635 "case log::info : return \"info\";\n"
20636 "case log::warning : return \"warning\";\n"
20637 "case log::error :\n"
20638 "default : return \"default\";\n"
20639 "}",
20640 Alignment);
20641 }
20642
20643 TEST_F(FormatTest, AlignWithLineBreaks) {
20644 auto Style = getLLVMStyleWithColumns(120);
20645
20646 EXPECT_EQ(Style.AlignConsecutiveAssignments,
20647 FormatStyle::AlignConsecutiveStyle(
20648 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20649 /*AcrossComments=*/false, /*AlignCompound=*/false,
20650 /*AlignFunctionDeclarations=*/false,
20651 /*AlignFunctionPointers=*/false,
20652 /*PadOperators=*/true}));
20653 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20654 FormatStyle::AlignConsecutiveStyle(
20655 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20656 /*AcrossComments=*/false, /*AlignCompound=*/false,
20657 /*AlignFunctionDeclarations=*/true,
20658 /*AlignFunctionPointers=*/false,
20659 /*PadOperators=*/false}));
20660 verifyFormat("void foo() {\n"
20661 " int myVar = 5;\n"
20662 " double x = 3.14;\n"
20663 " auto str = \"Hello \"\n"
20664 " \"World\";\n"
20665 " auto s = \"Hello \"\n"
20666 " \"Again\";\n"
20667 "}",
20668 Style);
20669
20670 // clang-format off
20671 verifyFormat("void foo() {\n"
20672 " const int capacityBefore = Entries.capacity();\n"
20673 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20674 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20675 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20676 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20677 "}",
20678 Style);
20679 // clang-format on
20680
20681 Style.AlignConsecutiveAssignments.Enabled = true;
20682 verifyFormat("void foo() {\n"
20683 " int myVar = 5;\n"
20684 " double x = 3.14;\n"
20685 " auto str = \"Hello \"\n"
20686 " \"World\";\n"
20687 " auto s = \"Hello \"\n"
20688 " \"Again\";\n"
20689 "}",
20690 Style);
20691
20692 verifyFormat("auto someLongName = 3;\n"
20693 "auto x = someLongExpression //\n"
20694 " | ranges::views::values;",
20695 Style);
20696 verifyFormat(
20697 "veryverylongvariablename = somethingelse;\n"
20698 "shortervariablename = anotherverylonglonglongvariablename + //\n"
20699 " somevariablethatwastoolongtofitonthesamerow;",
20700 Style);
20701
20702 // clang-format off
20703 verifyFormat("void foo() {\n"
20704 " const int capacityBefore = Entries.capacity();\n"
20705 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20706 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20707 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20708 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20709 "}",
20710 Style);
20711 // clang-format on
20712
20713 Style.AlignConsecutiveAssignments.Enabled = false;
20714 Style.AlignConsecutiveDeclarations.Enabled = true;
20715 verifyFormat("void foo() {\n"
20716 " int myVar = 5;\n"
20717 " double x = 3.14;\n"
20718 " auto str = \"Hello \"\n"
20719 " \"World\";\n"
20720 " auto s = \"Hello \"\n"
20721 " \"Again\";\n"
20722 "}",
20723 Style);
20724
20725 // clang-format off
20726 verifyFormat("void foo() {\n"
20727 " const int capacityBefore = Entries.capacity();\n"
20728 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20729 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20730 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20731 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20732 "}",
20733 Style);
20734 // clang-format on
20735
20736 Style.AlignConsecutiveAssignments.Enabled = true;
20737 Style.AlignConsecutiveDeclarations.Enabled = true;
20738
20739 verifyFormat("void foo() {\n"
20740 " int myVar = 5;\n"
20741 " double x = 3.14;\n"
20742 " auto str = \"Hello \"\n"
20743 " \"World\";\n"
20744 " auto s = \"Hello \"\n"
20745 " \"Again\";\n"
20746 "}",
20747 Style);
20748
20749 verifyFormat("void foo() {\n"
20750 " int myVar = 5;\n"
20751 " double x = 3.14;\n"
20752 " auto str = (\"Hello \"\n"
20753 " \"World\");\n"
20754 " auto s = (\"Hello \"\n"
20755 " \"Again\");\n"
20756 "}",
20757 Style);
20758
20759 verifyFormat("A B = {\"Hello \"\n"
20760 " \"World\"};\n"
20761 "BYTE payload = 2;",
20762 Style);
20763
20764 // clang-format off
20765 verifyFormat("void foo() {\n"
20766 " const int capacityBefore = Entries.capacity();\n"
20767 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20768 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20769 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20770 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20771 "}",
20772 Style);
20773 // clang-format on
20774
20775 // The start of the closure is indented from the start of the line. It should
20776 // not move with the equal sign.
20777 Style.ContinuationIndentWidth = 6;
20778 Style.IndentWidth = 8;
20779 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
20780 Style.BraceWrapping.BeforeLambdaBody = true;
20781 Style.BraceWrapping.IndentBraces = true;
20782 Style.ColumnLimit = 32;
20783 verifyFormat("auto aaaaaaaaaaa = {};\n"
20784 "b = []() constexpr\n"
20785 " -> aaaaaaaaaaaaaaaaaaaaaaa\n"
20786 " {\n"
20787 " return {}; //\n"
20788 " };",
20789 Style);
20790 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
20791 "b = []()\n"
20792 " {\n"
20793 " return; //\n"
20794 " };",
20795 Style);
20796 Style.ColumnLimit = 33;
20797 verifyFormat("auto aaaaaaaaaaa = {};\n"
20798 "b = []() constexpr\n"
20799 " -> aaaaaaaaaaaaaaaaaaaaaaa\n"
20800 " {\n"
20801 " return {}; //\n"
20802 " };",
20803 Style);
20804 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
20805 "b = []()\n"
20806 " {\n"
20807 " return; //\n"
20808 " };",
20809 Style);
20810
20811 Style = getLLVMStyleWithColumns(20);
20812 Style.AlignConsecutiveAssignments.Enabled = true;
20813 Style.IndentWidth = 4;
20814
20815 verifyFormat("void foo() {\n"
20816 " int i1 = 1;\n"
20817 " int j = 0;\n"
20818 " int k = bar(\n"
20819 " argument1,\n"
20820 " argument2);\n"
20821 "}",
20822 Style);
20823
20824 verifyFormat("unsigned i = 0;\n"
20825 "int a[] = {\n"
20826 " 1234567890,\n"
20827 " -1234567890};",
20828 Style);
20829
20830 Style.ColumnLimit = 120;
20831
20832 // clang-format off
20833 verifyFormat("void SomeFunc() {\n"
20834 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20835 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20836 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20837 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20838 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20839 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20840 "}",
20841 Style);
20842 // clang-format on
20843
20844 Style.BinPackArguments = false;
20845
20846 // clang-format off
20847 verifyFormat("void SomeFunc() {\n"
20848 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20849 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20850 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20851 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20852 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20853 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20854 "}",
20855 Style);
20856 // clang-format on
20857
20858 Style = getLLVMStyle();
20859 Style.AlignConsecutiveAssignments.Enabled = true;
20860 verifyFormat("param->fault_depth = grid->jfault * grid->dz; //\n"
20861 "grid->corner = grid->jlid + 1; //\n"
20862 "param->peclet = param->V //\n"
20863 " * param->L * 1000.0 //\n"
20864 " / param->kappa; //",
20865 Style);
20866 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
20867 verifyFormat("param->fault_depth = grid->jfault * grid->dz; //\n"
20868 "grid->corner = grid->jlid + 1; //\n"
20869 "param->peclet = param->V //\n"
20870 " * param->L * 1000.0 //\n"
20871 " / param->kappa; //",
20872 Style);
20873
20874 Style = getLLVMStyleWithColumns(70);
20875 Style.AlignConsecutiveDeclarations.Enabled = true;
20876 verifyFormat(
20877 "ReturnType\n"
20878 "MyFancyIntefaceFunction(Context *context,\n"
20879 " ALongTypeName *response) noexcept override;\n"
20880 "ReturnType func();",
20881 Style);
20882
20883 verifyFormat(
20884 "ReturnType\n"
20885 "MyFancyIntefaceFunction(B<int> *context,\n"
20886 " decltype(AFunc) *response) noexcept override;\n"
20887 "ReturnType func();",
20888 Style);
20889
20890 Style.AlignConsecutiveAssignments.Enabled = true;
20891 verifyFormat("float i2 = 0;\n"
20892 "auto v = false ? type{}\n"
20893 " : type{\n"
20894 " 1,\n"
20895 " };",
20896 Style);
20897
20898 verifyFormat("const char *const MatHtool[] = {};\n"
20899 "const char Htool[] = \"@article{m,\\n\"\n"
20900 " \" \"\n"
20901 " \" \"\n"
20902 " \" \"\n"
20903 " \"}\\n\";",
20904 Style);
20905
20906 Style.ColumnLimit = 15;
20907 verifyFormat("int i1 = 1;\n"
20908 "k = bar(\n"
20909 " argument1,\n"
20910 " argument2);",
20911 Style);
20912
20913 Style.ColumnLimit = 45;
20914 verifyFormat("auto xxxxxxxx = foo;\n"
20915 "auto x = whatever ? some / long -\n"
20916 " computition / stuff\n"
20917 " : random;",
20918 Style);
20919 }
20920
20921 TEST_F(FormatTest, AlignWithInitializerPeriods) {
20922 auto Style = getLLVMStyleWithColumns(60);
20923
20924 verifyFormat("void foo1(void) {\n"
20925 " BYTE p[1] = 1;\n"
20926 " A B = {.one_foooooooooooooooo = 2,\n"
20927 " .two_fooooooooooooo = 3,\n"
20928 " .three_fooooooooooooo = 4};\n"
20929 " BYTE payload = 2;\n"
20930 "}",
20931 Style);
20932
20933 Style.AlignConsecutiveAssignments.Enabled = true;
20934 Style.AlignConsecutiveDeclarations.Enabled = false;
20935 verifyFormat("void foo2(void) {\n"
20936 " BYTE p[1] = 1;\n"
20937 " A B = {.one_foooooooooooooooo = 2,\n"
20938 " .two_fooooooooooooo = 3,\n"
20939 " .three_fooooooooooooo = 4};\n"
20940 " BYTE payload = 2;\n"
20941 "}",
20942 Style);
20943
20944 // The lines inside the braces are supposed to be indented by
20945 // BracedInitializerIndentWidth from the start of the line. They should not
20946 // move with the opening brace.
20947 verifyFormat("void foo2(void) {\n"
20948 " BYTE p[1] = 1;\n"
20949 " A B = {\n"
20950 " .one_foooooooooooooooo = 2,\n"
20951 " .two_fooooooooooooo = 3,\n"
20952 " .three_fooooooooooooo = 4,\n"
20953 " };\n"
20954 " BYTE payload = 2;\n"
20955 "}",
20956 Style);
20957
20958 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
20959 "auto b = g([] {\n"
20960 " x = {.one_foooooooooooooooo = 2, //\n"
20961 " .two_fooooooooooooo = 3, //\n"
20962 " .three_fooooooooooooo = 4};\n"
20963 "});",
20964 Style);
20965
20966 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
20967 "auto b = {.a = {\n"
20968 " .a = 0,\n"
20969 " }};",
20970 Style);
20971
20972 Style.AlignConsecutiveAssignments.Enabled = false;
20973 Style.AlignConsecutiveDeclarations.Enabled = true;
20974 verifyFormat("void foo3(void) {\n"
20975 " BYTE p[1] = 1;\n"
20976 " A B = {.one_foooooooooooooooo = 2,\n"
20977 " .two_fooooooooooooo = 3,\n"
20978 " .three_fooooooooooooo = 4};\n"
20979 " BYTE payload = 2;\n"
20980 "}",
20981 Style);
20982
20983 Style.AlignConsecutiveAssignments.Enabled = true;
20984 Style.AlignConsecutiveDeclarations.Enabled = true;
20985 verifyFormat("void foo4(void) {\n"
20986 " BYTE p[1] = 1;\n"
20987 " A B = {.one_foooooooooooooooo = 2,\n"
20988 " .two_fooooooooooooo = 3,\n"
20989 " .three_fooooooooooooo = 4};\n"
20990 " BYTE payload = 2;\n"
20991 "}",
20992 Style);
20993 }
20994
20995 TEST_F(FormatTest, LinuxBraceBreaking) {
20996 FormatStyle LinuxBraceStyle = getLLVMStyle();
20997 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
20998 verifyFormat("namespace a\n"
20999 "{\n"
21000 "class A\n"
21001 "{\n"
21002 " void f()\n"
21003 " {\n"
21004 " if (true) {\n"
21005 " a();\n"
21006 " b();\n"
21007 " } else {\n"
21008 " a();\n"
21009 " }\n"
21010 " }\n"
21011 " void g() { return; }\n"
21012 "};\n"
21013 "struct B {\n"
21014 " int x;\n"
21015 "};\n"
21016 "} // namespace a",
21017 LinuxBraceStyle);
21018 verifyFormat("enum X {\n"
21019 " Y = 0,\n"
21020 "}",
21021 LinuxBraceStyle);
21022 verifyFormat("struct S {\n"
21023 " int Type;\n"
21024 " union {\n"
21025 " int x;\n"
21026 " double y;\n"
21027 " } Value;\n"
21028 " class C\n"
21029 " {\n"
21030 " MyFavoriteType Value;\n"
21031 " } Class;\n"
21032 "}",
21033 LinuxBraceStyle);
21034 }
21035
21036 TEST_F(FormatTest, MozillaBraceBreaking) {
21037 FormatStyle MozillaBraceStyle = getLLVMStyle();
21038 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
21039 MozillaBraceStyle.FixNamespaceComments = false;
21040 verifyFormat("namespace a {\n"
21041 "class A\n"
21042 "{\n"
21043 " void f()\n"
21044 " {\n"
21045 " if (true) {\n"
21046 " a();\n"
21047 " b();\n"
21048 " }\n"
21049 " }\n"
21050 " void g() { return; }\n"
21051 "};\n"
21052 "enum E\n"
21053 "{\n"
21054 " A,\n"
21055 " // foo\n"
21056 " B,\n"
21057 " C\n"
21058 "};\n"
21059 "struct B\n"
21060 "{\n"
21061 " int x;\n"
21062 "};\n"
21063 "}",
21064 MozillaBraceStyle);
21065 verifyFormat("struct S\n"
21066 "{\n"
21067 " int Type;\n"
21068 " union\n"
21069 " {\n"
21070 " int x;\n"
21071 " double y;\n"
21072 " } Value;\n"
21073 " class C\n"
21074 " {\n"
21075 " MyFavoriteType Value;\n"
21076 " } Class;\n"
21077 "}",
21078 MozillaBraceStyle);
21079 }
21080
21081 TEST_F(FormatTest, StroustrupBraceBreaking) {
21082 FormatStyle StroustrupBraceStyle = getLLVMStyle();
21083 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
21084 verifyFormat("namespace a {\n"
21085 "class A {\n"
21086 " void f()\n"
21087 " {\n"
21088 " if (true) {\n"
21089 " a();\n"
21090 " b();\n"
21091 " }\n"
21092 " }\n"
21093 " void g() { return; }\n"
21094 "};\n"
21095 "struct B {\n"
21096 " int x;\n"
21097 "};\n"
21098 "} // namespace a",
21099 StroustrupBraceStyle);
21100
21101 verifyFormat("void foo()\n"
21102 "{\n"
21103 " if (a) {\n"
21104 " a();\n"
21105 " }\n"
21106 " else {\n"
21107 " b();\n"
21108 " }\n"
21109 "}",
21110 StroustrupBraceStyle);
21111
21112 verifyFormat("#ifdef _DEBUG\n"
21113 "int foo(int i = 0)\n"
21114 "#else\n"
21115 "int foo(int i = 5)\n"
21116 "#endif\n"
21117 "{\n"
21118 " return i;\n"
21119 "}",
21120 StroustrupBraceStyle);
21121
21122 verifyFormat("void foo() {}\n"
21123 "void bar()\n"
21124 "#ifdef _DEBUG\n"
21125 "{\n"
21126 " foo();\n"
21127 "}\n"
21128 "#else\n"
21129 "{\n"
21130 "}\n"
21131 "#endif",
21132 StroustrupBraceStyle);
21133
21134 verifyFormat("void foobar() { int i = 5; }\n"
21135 "#ifdef _DEBUG\n"
21136 "void bar() {}\n"
21137 "#else\n"
21138 "void bar() { foobar(); }\n"
21139 "#endif",
21140 StroustrupBraceStyle);
21141 }
21142
21143 TEST_F(FormatTest, AllmanBraceBreaking) {
21144 FormatStyle AllmanBraceStyle = getLLVMStyle();
21145 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
21146
21147 verifyFormat("namespace a\n"
21148 "{\n"
21149 "void f();\n"
21150 "void g();\n"
21151 "} // namespace a",
21152 "namespace a\n"
21153 "{\n"
21154 "void f();\n"
21155 "void g();\n"
21156 "}",
21157 AllmanBraceStyle);
21158
21159 verifyFormat("namespace a\n"
21160 "{\n"
21161 "class A\n"
21162 "{\n"
21163 " void f()\n"
21164 " {\n"
21165 " if (true)\n"
21166 " {\n"
21167 " a();\n"
21168 " b();\n"
21169 " }\n"
21170 " }\n"
21171 " void g() { return; }\n"
21172 "};\n"
21173 "struct B\n"
21174 "{\n"
21175 " int x;\n"
21176 "};\n"
21177 "union C\n"
21178 "{\n"
21179 "};\n"
21180 "} // namespace a",
21181 AllmanBraceStyle);
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 AllmanBraceStyle);
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 " } while (false)\n"
21214 "}",
21215 AllmanBraceStyle);
21216
21217 verifyFormat("void f(int a)\n"
21218 "{\n"
21219 " switch (a)\n"
21220 " {\n"
21221 " case 0:\n"
21222 " break;\n"
21223 " case 1:\n"
21224 " {\n"
21225 " break;\n"
21226 " }\n"
21227 " case 2:\n"
21228 " {\n"
21229 " }\n"
21230 " break;\n"
21231 " default:\n"
21232 " break;\n"
21233 " }\n"
21234 "}",
21235 AllmanBraceStyle);
21236
21237 verifyFormat("enum X\n"
21238 "{\n"
21239 " Y = 0,\n"
21240 "}",
21241 AllmanBraceStyle);
21242 verifyFormat("enum X\n"
21243 "{\n"
21244 " Y = 0\n"
21245 "}",
21246 AllmanBraceStyle);
21247
21248 verifyFormat("@interface BSApplicationController ()\n"
21249 "{\n"
21250 "@private\n"
21251 " id _extraIvar;\n"
21252 "}\n"
21253 "@end",
21254 AllmanBraceStyle);
21255
21256 verifyFormat("#ifdef _DEBUG\n"
21257 "int foo(int i = 0)\n"
21258 "#else\n"
21259 "int foo(int i = 5)\n"
21260 "#endif\n"
21261 "{\n"
21262 " return i;\n"
21263 "}",
21264 AllmanBraceStyle);
21265
21266 verifyFormat("void foo() {}\n"
21267 "void bar()\n"
21268 "#ifdef _DEBUG\n"
21269 "{\n"
21270 " foo();\n"
21271 "}\n"
21272 "#else\n"
21273 "{\n"
21274 "}\n"
21275 "#endif",
21276 AllmanBraceStyle);
21277
21278 verifyFormat("void foobar() { int i = 5; }\n"
21279 "#ifdef _DEBUG\n"
21280 "void bar() {}\n"
21281 "#else\n"
21282 "void bar() { foobar(); }\n"
21283 "#endif",
21284 AllmanBraceStyle);
21285
21286 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
21287 FormatStyle::SLS_All);
21288
21289 verifyFormat("[](int i) { return i + 2; };\n"
21290 "[](int i, int j)\n"
21291 "{\n"
21292 " auto x = i + j;\n"
21293 " auto y = i * j;\n"
21294 " return x ^ y;\n"
21295 "};\n"
21296 "void foo()\n"
21297 "{\n"
21298 " auto shortLambda = [](int i) { return i + 2; };\n"
21299 " auto longLambda = [](int i, int j)\n"
21300 " {\n"
21301 " auto x = i + j;\n"
21302 " auto y = i * j;\n"
21303 " return x ^ y;\n"
21304 " };\n"
21305 "}",
21306 AllmanBraceStyle);
21307
21308 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21309
21310 verifyFormat("[](int i)\n"
21311 "{\n"
21312 " return i + 2;\n"
21313 "};\n"
21314 "[](int i, int j)\n"
21315 "{\n"
21316 " auto x = i + j;\n"
21317 " auto y = i * j;\n"
21318 " return x ^ y;\n"
21319 "};\n"
21320 "void foo()\n"
21321 "{\n"
21322 " auto shortLambda = [](int i)\n"
21323 " {\n"
21324 " return i + 2;\n"
21325 " };\n"
21326 " auto longLambda = [](int i, int j)\n"
21327 " {\n"
21328 " auto x = i + j;\n"
21329 " auto y = i * j;\n"
21330 " return x ^ y;\n"
21331 " };\n"
21332 "}",
21333 AllmanBraceStyle);
21334
21335 // Reset
21336 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
21337
21338 // This shouldn't affect ObjC blocks..
21339 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21340 " // ...\n"
21341 " int i;\n"
21342 "}];",
21343 AllmanBraceStyle);
21344 verifyFormat("void (^block)(void) = ^{\n"
21345 " // ...\n"
21346 " int i;\n"
21347 "};",
21348 AllmanBraceStyle);
21349 // .. or dict literals.
21350 verifyFormat("void f()\n"
21351 "{\n"
21352 " // ...\n"
21353 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21354 "}",
21355 AllmanBraceStyle);
21356 verifyFormat("void f()\n"
21357 "{\n"
21358 " // ...\n"
21359 " [object someMethod:@{a : @\"b\"}];\n"
21360 "}",
21361 AllmanBraceStyle);
21362 verifyFormat("int f()\n"
21363 "{ // comment\n"
21364 " return 42;\n"
21365 "}",
21366 AllmanBraceStyle);
21367
21368 AllmanBraceStyle.ColumnLimit = 19;
21369 verifyFormat("void f() { int i; }", AllmanBraceStyle);
21370 AllmanBraceStyle.ColumnLimit = 18;
21371 verifyFormat("void f()\n"
21372 "{\n"
21373 " int i;\n"
21374 "}",
21375 AllmanBraceStyle);
21376 AllmanBraceStyle.ColumnLimit = 80;
21377
21378 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
21379 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21380 FormatStyle::SIS_WithoutElse;
21381 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21382 verifyFormat("void f(bool b)\n"
21383 "{\n"
21384 " if (b)\n"
21385 " {\n"
21386 " return;\n"
21387 " }\n"
21388 "}",
21389 BreakBeforeBraceShortIfs);
21390 verifyFormat("void f(bool b)\n"
21391 "{\n"
21392 " if constexpr (b)\n"
21393 " {\n"
21394 " return;\n"
21395 " }\n"
21396 "}",
21397 BreakBeforeBraceShortIfs);
21398 verifyFormat("void f(bool b)\n"
21399 "{\n"
21400 " if CONSTEXPR (b)\n"
21401 " {\n"
21402 " return;\n"
21403 " }\n"
21404 "}",
21405 BreakBeforeBraceShortIfs);
21406 verifyFormat("void f(bool b)\n"
21407 "{\n"
21408 " if (b) return;\n"
21409 "}",
21410 BreakBeforeBraceShortIfs);
21411 verifyFormat("void f(bool b)\n"
21412 "{\n"
21413 " if constexpr (b) return;\n"
21414 "}",
21415 BreakBeforeBraceShortIfs);
21416 verifyFormat("void f(bool b)\n"
21417 "{\n"
21418 " if CONSTEXPR (b) return;\n"
21419 "}",
21420 BreakBeforeBraceShortIfs);
21421 verifyFormat("void f(bool b)\n"
21422 "{\n"
21423 " while (b)\n"
21424 " {\n"
21425 " return;\n"
21426 " }\n"
21427 "}",
21428 BreakBeforeBraceShortIfs);
21429 }
21430
21431 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
21432 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
21433 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
21434
21435 // Make a few changes to the style for testing purposes
21436 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
21437 FormatStyle::SFS_Empty;
21438 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21439
21440 // FIXME: this test case can't decide whether there should be a blank line
21441 // after the ~D() line or not. It adds one if one doesn't exist in the test
21442 // and it removes the line if one exists.
21443 /*
21444 verifyFormat("class A;\n"
21445 "namespace B\n"
21446 " {\n"
21447 "class C;\n"
21448 "// Comment\n"
21449 "class D\n"
21450 " {\n"
21451 "public:\n"
21452 " D();\n"
21453 " ~D() {}\n"
21454 "private:\n"
21455 " enum E\n"
21456 " {\n"
21457 " F\n"
21458 " }\n"
21459 " };\n"
21460 " } // namespace B",
21461 WhitesmithsBraceStyle);
21462 */
21463
21464 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
21465 verifyFormat("namespace a\n"
21466 " {\n"
21467 "class A\n"
21468 " {\n"
21469 " void f()\n"
21470 " {\n"
21471 " if (true)\n"
21472 " {\n"
21473 " a();\n"
21474 " b();\n"
21475 " }\n"
21476 " }\n"
21477 " void g()\n"
21478 " {\n"
21479 " return;\n"
21480 " }\n"
21481 " };\n"
21482 "struct B\n"
21483 " {\n"
21484 " int x;\n"
21485 " };\n"
21486 " } // namespace a",
21487 WhitesmithsBraceStyle);
21488
21489 verifyFormat("namespace a\n"
21490 " {\n"
21491 "namespace b\n"
21492 " {\n"
21493 "class A\n"
21494 " {\n"
21495 " void f()\n"
21496 " {\n"
21497 " if (true)\n"
21498 " {\n"
21499 " a();\n"
21500 " b();\n"
21501 " }\n"
21502 " }\n"
21503 " void g()\n"
21504 " {\n"
21505 " return;\n"
21506 " }\n"
21507 " };\n"
21508 "struct B\n"
21509 " {\n"
21510 " int x;\n"
21511 " };\n"
21512 " } // namespace b\n"
21513 " } // namespace a",
21514 WhitesmithsBraceStyle);
21515
21516 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
21517 verifyFormat("namespace a\n"
21518 " {\n"
21519 "namespace b\n"
21520 " {\n"
21521 " class A\n"
21522 " {\n"
21523 " void f()\n"
21524 " {\n"
21525 " if (true)\n"
21526 " {\n"
21527 " a();\n"
21528 " b();\n"
21529 " }\n"
21530 " }\n"
21531 " void g()\n"
21532 " {\n"
21533 " return;\n"
21534 " }\n"
21535 " };\n"
21536 " struct B\n"
21537 " {\n"
21538 " int x;\n"
21539 " };\n"
21540 " } // namespace b\n"
21541 " } // namespace a",
21542 WhitesmithsBraceStyle);
21543
21544 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
21545 verifyFormat("namespace a\n"
21546 " {\n"
21547 " namespace b\n"
21548 " {\n"
21549 " class A\n"
21550 " {\n"
21551 " void f()\n"
21552 " {\n"
21553 " if (true)\n"
21554 " {\n"
21555 " a();\n"
21556 " b();\n"
21557 " }\n"
21558 " }\n"
21559 " void g()\n"
21560 " {\n"
21561 " return;\n"
21562 " }\n"
21563 " };\n"
21564 " struct B\n"
21565 " {\n"
21566 " int x;\n"
21567 " };\n"
21568 " } // namespace b\n"
21569 " } // namespace a",
21570 WhitesmithsBraceStyle);
21571
21572 verifyFormat("void f()\n"
21573 " {\n"
21574 " if (true)\n"
21575 " {\n"
21576 " a();\n"
21577 " }\n"
21578 " else if (false)\n"
21579 " {\n"
21580 " b();\n"
21581 " }\n"
21582 " else\n"
21583 " {\n"
21584 " c();\n"
21585 " }\n"
21586 " }",
21587 WhitesmithsBraceStyle);
21588
21589 verifyFormat("void f()\n"
21590 " {\n"
21591 " for (int i = 0; i < 10; ++i)\n"
21592 " {\n"
21593 " a();\n"
21594 " }\n"
21595 " while (false)\n"
21596 " {\n"
21597 " b();\n"
21598 " }\n"
21599 " do\n"
21600 " {\n"
21601 " c();\n"
21602 " } while (false)\n"
21603 " }",
21604 WhitesmithsBraceStyle);
21605
21606 WhitesmithsBraceStyle.IndentCaseLabels = true;
21607 verifyFormat("void switchTest1(int a)\n"
21608 " {\n"
21609 " switch (a)\n"
21610 " {\n"
21611 " case 2:\n"
21612 " {\n"
21613 " }\n"
21614 " break;\n"
21615 " }\n"
21616 " }",
21617 WhitesmithsBraceStyle);
21618
21619 verifyFormat("void switchTest2(int a)\n"
21620 " {\n"
21621 " switch (a)\n"
21622 " {\n"
21623 " case 0:\n"
21624 " break;\n"
21625 " case 1:\n"
21626 " {\n"
21627 " break;\n"
21628 " }\n"
21629 " case 2:\n"
21630 " {\n"
21631 " }\n"
21632 " break;\n"
21633 " default:\n"
21634 " break;\n"
21635 " }\n"
21636 " }",
21637 WhitesmithsBraceStyle);
21638
21639 verifyFormat("void switchTest3(int a)\n"
21640 " {\n"
21641 " switch (a)\n"
21642 " {\n"
21643 " case 0:\n"
21644 " {\n"
21645 " foo(x);\n"
21646 " }\n"
21647 " break;\n"
21648 " default:\n"
21649 " {\n"
21650 " foo(1);\n"
21651 " }\n"
21652 " break;\n"
21653 " }\n"
21654 " }",
21655 WhitesmithsBraceStyle);
21656
21657 WhitesmithsBraceStyle.IndentCaseLabels = false;
21658
21659 verifyFormat("void switchTest4(int a)\n"
21660 " {\n"
21661 " switch (a)\n"
21662 " {\n"
21663 " case 2:\n"
21664 " {\n"
21665 " }\n"
21666 " break;\n"
21667 " }\n"
21668 " }",
21669 WhitesmithsBraceStyle);
21670
21671 verifyFormat("void switchTest5(int a)\n"
21672 " {\n"
21673 " switch (a)\n"
21674 " {\n"
21675 " case 0:\n"
21676 " break;\n"
21677 " case 1:\n"
21678 " {\n"
21679 " foo();\n"
21680 " break;\n"
21681 " }\n"
21682 " case 2:\n"
21683 " {\n"
21684 " }\n"
21685 " break;\n"
21686 " default:\n"
21687 " break;\n"
21688 " }\n"
21689 " }",
21690 WhitesmithsBraceStyle);
21691
21692 verifyFormat("void switchTest6(int a)\n"
21693 " {\n"
21694 " switch (a)\n"
21695 " {\n"
21696 " case 0:\n"
21697 " {\n"
21698 " foo(x);\n"
21699 " }\n"
21700 " break;\n"
21701 " default:\n"
21702 " {\n"
21703 " foo(1);\n"
21704 " }\n"
21705 " break;\n"
21706 " }\n"
21707 " }",
21708 WhitesmithsBraceStyle);
21709
21710 verifyFormat("enum X\n"
21711 " {\n"
21712 " Y = 0, // testing\n"
21713 " }",
21714 WhitesmithsBraceStyle);
21715
21716 verifyFormat("enum X\n"
21717 " {\n"
21718 " Y = 0\n"
21719 " }",
21720 WhitesmithsBraceStyle);
21721 verifyFormat("enum X\n"
21722 " {\n"
21723 " Y = 0,\n"
21724 " Z = 1\n"
21725 " };",
21726 WhitesmithsBraceStyle);
21727
21728 verifyFormat("@interface BSApplicationController ()\n"
21729 " {\n"
21730 "@private\n"
21731 " id _extraIvar;\n"
21732 " }\n"
21733 "@end",
21734 WhitesmithsBraceStyle);
21735
21736 verifyFormat("#ifdef _DEBUG\n"
21737 "int foo(int i = 0)\n"
21738 "#else\n"
21739 "int foo(int i = 5)\n"
21740 "#endif\n"
21741 " {\n"
21742 " return i;\n"
21743 " }",
21744 WhitesmithsBraceStyle);
21745
21746 verifyFormat("void foo() {}\n"
21747 "void bar()\n"
21748 "#ifdef _DEBUG\n"
21749 " {\n"
21750 " foo();\n"
21751 " }\n"
21752 "#else\n"
21753 " {\n"
21754 " }\n"
21755 "#endif",
21756 WhitesmithsBraceStyle);
21757
21758 verifyFormat("void foobar()\n"
21759 " {\n"
21760 " int i = 5;\n"
21761 " }\n"
21762 "#ifdef _DEBUG\n"
21763 "void bar() {}\n"
21764 "#else\n"
21765 "void bar()\n"
21766 " {\n"
21767 " foobar();\n"
21768 " }\n"
21769 "#endif",
21770 WhitesmithsBraceStyle);
21771
21772 // This shouldn't affect ObjC blocks..
21773 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21774 " // ...\n"
21775 " int i;\n"
21776 "}];",
21777 WhitesmithsBraceStyle);
21778 verifyFormat("void (^block)(void) = ^{\n"
21779 " // ...\n"
21780 " int i;\n"
21781 "};",
21782 WhitesmithsBraceStyle);
21783 // .. or dict literals.
21784 verifyFormat("void f()\n"
21785 " {\n"
21786 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21787 " }",
21788 WhitesmithsBraceStyle);
21789
21790 verifyFormat("int f()\n"
21791 " { // comment\n"
21792 " return 42;\n"
21793 " }",
21794 WhitesmithsBraceStyle);
21795
21796 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
21797 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21798 FormatStyle::SIS_OnlyFirstIf;
21799 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21800 verifyFormat("void f(bool b)\n"
21801 " {\n"
21802 " if (b)\n"
21803 " {\n"
21804 " return;\n"
21805 " }\n"
21806 " }",
21807 BreakBeforeBraceShortIfs);
21808 verifyFormat("void f(bool b)\n"
21809 " {\n"
21810 " if (b) return;\n"
21811 " }",
21812 BreakBeforeBraceShortIfs);
21813 verifyFormat("void f(bool b)\n"
21814 " {\n"
21815 " while (b)\n"
21816 " {\n"
21817 " return;\n"
21818 " }\n"
21819 " }",
21820 BreakBeforeBraceShortIfs);
21821 }
21822
21823 TEST_F(FormatTest, GNUBraceBreaking) {
21824 FormatStyle GNUBraceStyle = getLLVMStyle();
21825 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
21826 verifyFormat("namespace a\n"
21827 "{\n"
21828 "class A\n"
21829 "{\n"
21830 " void f()\n"
21831 " {\n"
21832 " int a;\n"
21833 " {\n"
21834 " int b;\n"
21835 " }\n"
21836 " if (true)\n"
21837 " {\n"
21838 " a();\n"
21839 " b();\n"
21840 " }\n"
21841 " }\n"
21842 " void g() { return; }\n"
21843 "}\n"
21844 "} // namespace a",
21845 GNUBraceStyle);
21846
21847 verifyFormat("void f()\n"
21848 "{\n"
21849 " if (true)\n"
21850 " {\n"
21851 " a();\n"
21852 " }\n"
21853 " else if (false)\n"
21854 " {\n"
21855 " b();\n"
21856 " }\n"
21857 " else\n"
21858 " {\n"
21859 " c();\n"
21860 " }\n"
21861 "}",
21862 GNUBraceStyle);
21863
21864 verifyFormat("void f()\n"
21865 "{\n"
21866 " for (int i = 0; i < 10; ++i)\n"
21867 " {\n"
21868 " a();\n"
21869 " }\n"
21870 " while (false)\n"
21871 " {\n"
21872 " b();\n"
21873 " }\n"
21874 " do\n"
21875 " {\n"
21876 " c();\n"
21877 " }\n"
21878 " while (false);\n"
21879 "}",
21880 GNUBraceStyle);
21881
21882 verifyFormat("void f(int a)\n"
21883 "{\n"
21884 " switch (a)\n"
21885 " {\n"
21886 " case 0:\n"
21887 " break;\n"
21888 " case 1:\n"
21889 " {\n"
21890 " break;\n"
21891 " }\n"
21892 " case 2:\n"
21893 " {\n"
21894 " }\n"
21895 " break;\n"
21896 " default:\n"
21897 " break;\n"
21898 " }\n"
21899 "}",
21900 GNUBraceStyle);
21901
21902 verifyFormat("enum X\n"
21903 "{\n"
21904 " Y = 0,\n"
21905 "}",
21906 GNUBraceStyle);
21907
21908 verifyFormat("@interface BSApplicationController ()\n"
21909 "{\n"
21910 "@private\n"
21911 " id _extraIvar;\n"
21912 "}\n"
21913 "@end",
21914 GNUBraceStyle);
21915
21916 verifyFormat("#ifdef _DEBUG\n"
21917 "int foo(int i = 0)\n"
21918 "#else\n"
21919 "int foo(int i = 5)\n"
21920 "#endif\n"
21921 "{\n"
21922 " return i;\n"
21923 "}",
21924 GNUBraceStyle);
21925
21926 verifyFormat("void foo() {}\n"
21927 "void bar()\n"
21928 "#ifdef _DEBUG\n"
21929 "{\n"
21930 " foo();\n"
21931 "}\n"
21932 "#else\n"
21933 "{\n"
21934 "}\n"
21935 "#endif",
21936 GNUBraceStyle);
21937
21938 verifyFormat("void foobar() { int i = 5; }\n"
21939 "#ifdef _DEBUG\n"
21940 "void bar() {}\n"
21941 "#else\n"
21942 "void bar() { foobar(); }\n"
21943 "#endif",
21944 GNUBraceStyle);
21945 }
21946
21947 TEST_F(FormatTest, WebKitBraceBreaking) {
21948 FormatStyle WebKitBraceStyle = getLLVMStyle();
21949 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
21950 WebKitBraceStyle.FixNamespaceComments = false;
21951 verifyFormat("namespace a {\n"
21952 "class A {\n"
21953 " void f()\n"
21954 " {\n"
21955 " if (true) {\n"
21956 " a();\n"
21957 " b();\n"
21958 " }\n"
21959 " }\n"
21960 " void g() { return; }\n"
21961 "};\n"
21962 "enum E {\n"
21963 " A,\n"
21964 " // foo\n"
21965 " B,\n"
21966 " C\n"
21967 "};\n"
21968 "struct B {\n"
21969 " int x;\n"
21970 "};\n"
21971 "}",
21972 WebKitBraceStyle);
21973 verifyFormat("struct S {\n"
21974 " int Type;\n"
21975 " union {\n"
21976 " int x;\n"
21977 " double y;\n"
21978 " } Value;\n"
21979 " class C {\n"
21980 " MyFavoriteType Value;\n"
21981 " } Class;\n"
21982 "};",
21983 WebKitBraceStyle);
21984 }
21985
21986 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
21987 verifyFormat("void f() {\n"
21988 " try {\n"
21989 " } catch (const Exception &e) {\n"
21990 " }\n"
21991 "}");
21992 }
21993
21994 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
21995 auto Style = getLLVMStyle();
21996 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21997 verifyNoCrash("f({\n"
21998 "table({}, table({{\"\", false}}, {}))\n"
21999 "});",
22000 Style);
22001
22002 Style.AlignConsecutiveAssignments.Enabled = true;
22003 Style.AlignConsecutiveDeclarations.Enabled = true;
22004 verifyFormat("struct test demo[] = {\n"
22005 " {56, 23, \"hello\"},\n"
22006 " {-1, 93463, \"world\"},\n"
22007 " { 7, 5, \"!!\"}\n"
22008 "};",
22009 Style);
22010
22011 verifyFormat("struct test demo[] = {\n"
22012 " {56, 23, \"hello\"}, // first line\n"
22013 " {-1, 93463, \"world\"}, // second line\n"
22014 " { 7, 5, \"!!\"} // third line\n"
22015 "};",
22016 Style);
22017
22018 verifyFormat("struct test demo[4] = {\n"
22019 " { 56, 23, 21, \"oh\"}, // first line\n"
22020 " { -1, 93463, 22, \"my\"}, // second line\n"
22021 " { 7, 5, 1, \"goodness\"} // third line\n"
22022 " {234, 5, 1, \"gracious\"} // fourth line\n"
22023 "};",
22024 Style);
22025
22026 verifyFormat("struct test demo[3] = {\n"
22027 " {56, 23, \"hello\"},\n"
22028 " {-1, 93463, \"world\"},\n"
22029 " { 7, 5, \"!!\"}\n"
22030 "};",
22031 Style);
22032
22033 verifyFormat("struct test demo[3] = {\n"
22034 " {int{56}, 23, \"hello\"},\n"
22035 " {int{-1}, 93463, \"world\"},\n"
22036 " { int{7}, 5, \"!!\"}\n"
22037 "};",
22038 Style);
22039
22040 verifyFormat("struct test demo[] = {\n"
22041 " {56, 23, \"hello\"},\n"
22042 " {-1, 93463, \"world\"},\n"
22043 " { 7, 5, \"!!\"},\n"
22044 "};",
22045 Style);
22046
22047 verifyFormat("test demo[] = {\n"
22048 " {56, 23, \"hello\"},\n"
22049 " {-1, 93463, \"world\"},\n"
22050 " { 7, 5, \"!!\"},\n"
22051 "};",
22052 Style);
22053
22054 verifyFormat("demo = std::array<struct test, 3>{\n"
22055 " test{56, 23, \"hello\"},\n"
22056 " test{-1, 93463, \"world\"},\n"
22057 " test{ 7, 5, \"!!\"},\n"
22058 "};",
22059 Style);
22060
22061 verifyFormat("test demo[] = {\n"
22062 " {56, 23, \"hello\"},\n"
22063 "#if X\n"
22064 " {-1, 93463, \"world\"},\n"
22065 "#endif\n"
22066 " { 7, 5, \"!!\"}\n"
22067 "};",
22068 Style);
22069
22070 verifyFormat(
22071 "test demo[] = {\n"
22072 " { 7, 23,\n"
22073 " \"hello world i am a very long line that really, in any\"\n"
22074 " \"just world, ought to be split over multiple lines\"},\n"
22075 " {-1, 93463, \"world\"},\n"
22076 " {56, 5, \"!!\"}\n"
22077 "};",
22078 Style);
22079
22080 verifyNoCrash("Foo f[] = {\n"
22081 " [0] = { 1, },\n"
22082 " [i] { 1, },\n"
22083 "};",
22084 Style);
22085 verifyNoCrash("Foo foo[] = {\n"
22086 " [0] = {1, 1},\n"
22087 " [1] { 1, 1, },\n"
22088 " [2] { 1, 1, },\n"
22089 "};",
22090 Style);
22091 verifyNoCrash("test arr[] = {\n"
22092 "#define FOO(i) {i, i},\n"
22093 "SOME_GENERATOR(FOO)\n"
22094 "{2, 2}\n"
22095 "};",
22096 Style);
22097
22098 verifyFormat("return GradForUnaryCwise(g, {\n"
22099 " {{\"sign\"}, \"Sign\", "
22100 " {\"x\", \"dy\"}},\n"
22101 " { {\"dx\"}, \"Mul\", {\"dy\""
22102 ", \"sign\"}},\n"
22103 "});",
22104 Style);
22105
22106 Style.Cpp11BracedListStyle = FormatStyle::BLS_Block;
22107 verifyFormat("struct test demo[] = {\n"
22108 " { 56, 23, \"hello\" },\n"
22109 " { -1, 93463, \"world\" },\n"
22110 " { 7, 5, \"!!\" }\n"
22111 "};",
22112 Style);
22113 Style.Cpp11BracedListStyle = FormatStyle::BLS_AlignFirstComment;
22114
22115 Style.ColumnLimit = 0;
22116 verifyFormat(
22117 "test demo[] = {\n"
22118 " {56, 23, \"hello world i am a very long line that really, "
22119 "in any just world, ought to be split over multiple lines\"},\n"
22120 " {-1, 93463, "
22121 " \"world\"},\n"
22122 " { 7, 5, "
22123 " \"!!\"},\n"
22124 "};",
22125 "test demo[] = {{56, 23, \"hello world i am a very long line "
22126 "that really, in any just world, ought to be split over multiple "
22127 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22128 Style);
22129
22130 Style.ColumnLimit = 80;
22131 verifyFormat("test demo[] = {\n"
22132 " {56, 23, /* a comment */ \"hello\"},\n"
22133 " {-1, 93463, \"world\"},\n"
22134 " { 7, 5, \"!!\"}\n"
22135 "};",
22136 Style);
22137
22138 verifyFormat("test demo[] = {\n"
22139 " {56, 23, \"hello\"},\n"
22140 " {-1, 93463, \"world\" /* comment here */},\n"
22141 " { 7, 5, \"!!\"}\n"
22142 "};",
22143 Style);
22144
22145 verifyFormat("test demo[] = {\n"
22146 " {56, /* a comment */ 23, \"hello\"},\n"
22147 " {-1, 93463, \"world\"},\n"
22148 " { 7, 5, \"!!\"}\n"
22149 "};",
22150 Style);
22151
22152 Style.ColumnLimit = 20;
22153 verifyFormat("demo = std::array<\n"
22154 " struct test, 3>{\n"
22155 " test{\n"
22156 " 56, 23,\n"
22157 " \"hello \"\n"
22158 " \"world i \"\n"
22159 " \"am a very \"\n"
22160 " \"long line \"\n"
22161 " \"that \"\n"
22162 " \"really, \"\n"
22163 " \"in any \"\n"
22164 " \"just \"\n"
22165 " \"world, \"\n"
22166 " \"ought to \"\n"
22167 " \"be split \"\n"
22168 " \"over \"\n"
22169 " \"multiple \"\n"
22170 " \"lines\"},\n"
22171 " test{-1, 93463,\n"
22172 " \"world\"},\n"
22173 " test{ 7, 5,\n"
22174 " \"!!\" },\n"
22175 "};",
22176 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
22177 "i am a very long line that really, in any just world, ought "
22178 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
22179 "test{7, 5, \"!!\"},};",
22180 Style);
22181 // This caused a core dump by enabling Alignment in the LLVMStyle globally
22182 Style = getLLVMStyleWithColumns(50);
22183 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
22184 verifyFormat("static A x = {\n"
22185 " {{init1, init2, init3, init4},\n"
22186 " {init1, init2, init3, init4}}\n"
22187 "};",
22188 Style);
22189 // TODO: Fix the indentations below when this option is fully functional.
22190 #if 0
22191 verifyFormat("int a[][] = {\n"
22192 " {\n"
22193 " {0, 2}, //\n"
22194 " {1, 2} //\n"
22195 " }\n"
22196 "};",
22197 Style);
22198 #endif
22199 Style.ColumnLimit = 100;
22200 verifyFormat(
22201 "test demo[] = {\n"
22202 " {56, 23,\n"
22203 " \"hello world i am a very long line that really, in any just world"
22204 ", ought to be split over \"\n"
22205 " \"multiple lines\" },\n"
22206 " {-1, 93463, \"world\"},\n"
22207 " { 7, 5, \"!!\"},\n"
22208 "};",
22209 "test demo[] = {{56, 23, \"hello world i am a very long line "
22210 "that really, in any just world, ought to be split over multiple "
22211 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22212 Style);
22213
22214 Style = getLLVMStyleWithColumns(50);
22215 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
22216 verifyFormat("struct test demo[] = {\n"
22217 " {56, 23, \"hello\"},\n"
22218 " {-1, 93463, \"world\"},\n"
22219 " { 7, 5, \"!!\"}\n"
22220 "};\n"
22221 "static A x = {\n"
22222 " {{init1, init2, init3, init4},\n"
22223 " {init1, init2, init3, init4}}\n"
22224 "};",
22225 Style);
22226 Style.ColumnLimit = 100;
22227 Style.AlignConsecutiveAssignments.AcrossComments = true;
22228 Style.AlignConsecutiveDeclarations.AcrossComments = true;
22229 verifyFormat("struct test demo[] = {\n"
22230 " {56, 23, \"hello\"},\n"
22231 " {-1, 93463, \"world\"},\n"
22232 " { 7, 5, \"!!\"}\n"
22233 "};\n"
22234 "struct test demo[4] = {\n"
22235 " { 56, 23, 21, \"oh\"}, // first line\n"
22236 " { -1, 93463, 22, \"my\"}, // second line\n"
22237 " { 7, 5, 1, \"goodness\"} // third line\n"
22238 " {234, 5, 1, \"gracious\"} // fourth line\n"
22239 "};",
22240 Style);
22241 verifyFormat(
22242 "test demo[] = {\n"
22243 " {56,\n"
22244 " \"hello world i am a very long line that really, in any just world"
22245 ", ought to be split over \"\n"
22246 " \"multiple lines\", 23},\n"
22247 " {-1, \"world\", 93463},\n"
22248 " { 7, \"!!\", 5},\n"
22249 "};",
22250 "test demo[] = {{56, \"hello world i am a very long line "
22251 "that really, in any just world, ought to be split over multiple "
22252 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
22253 Style);
22254 }
22255
22256 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
22257 auto Style = getLLVMStyle();
22258 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
22259 /* FIXME: This case gets misformatted.
22260 verifyFormat("auto foo = Items{\n"
22261 " Section{0, bar(), },\n"
22262 " Section{1, boo() }\n"
22263 "};",
22264 Style);
22265 */
22266 verifyFormat("auto foo = Items{\n"
22267 " Section{\n"
22268 " 0, bar(),\n"
22269 " }\n"
22270 "};",
22271 Style);
22272 verifyFormat("struct test demo[] = {\n"
22273 " {56, 23, \"hello\"},\n"
22274 " {-1, 93463, \"world\"},\n"
22275 " {7, 5, \"!!\" }\n"
22276 "};",
22277 Style);
22278 verifyFormat("struct test demo[] = {\n"
22279 " {56, 23, \"hello\"}, // first line\n"
22280 " {-1, 93463, \"world\"}, // second line\n"
22281 " {7, 5, \"!!\" } // third line\n"
22282 "};",
22283 Style);
22284 verifyFormat("struct test demo[4] = {\n"
22285 " {56, 23, 21, \"oh\" }, // first line\n"
22286 " {-1, 93463, 22, \"my\" }, // second line\n"
22287 " {7, 5, 1, \"goodness\"} // third line\n"
22288 " {234, 5, 1, \"gracious\"} // fourth line\n"
22289 "};",
22290 Style);
22291 verifyFormat("struct test demo[3] = {\n"
22292 " {56, 23, \"hello\"},\n"
22293 " {-1, 93463, \"world\"},\n"
22294 " {7, 5, \"!!\" }\n"
22295 "};",
22296 Style);
22297
22298 verifyFormat("struct test demo[3] = {\n"
22299 " {int{56}, 23, \"hello\"},\n"
22300 " {int{-1}, 93463, \"world\"},\n"
22301 " {int{7}, 5, \"!!\" }\n"
22302 "};",
22303 Style);
22304 verifyFormat("struct test demo[] = {\n"
22305 " {56, 23, \"hello\"},\n"
22306 " {-1, 93463, \"world\"},\n"
22307 " {7, 5, \"!!\" },\n"
22308 "};",
22309 Style);
22310 verifyFormat("test demo[] = {\n"
22311 " {56, 23, \"hello\"},\n"
22312 " {-1, 93463, \"world\"},\n"
22313 " {7, 5, \"!!\" },\n"
22314 "};",
22315 Style);
22316 verifyFormat("demo = std::array<struct test, 3>{\n"
22317 " test{56, 23, \"hello\"},\n"
22318 " test{-1, 93463, \"world\"},\n"
22319 " test{7, 5, \"!!\" },\n"
22320 "};",
22321 Style);
22322 verifyFormat("test demo[] = {\n"
22323 " {56, 23, \"hello\"},\n"
22324 "#if X\n"
22325 " {-1, 93463, \"world\"},\n"
22326 "#endif\n"
22327 " {7, 5, \"!!\" }\n"
22328 "};",
22329 Style);
22330 verifyFormat(
22331 "test demo[] = {\n"
22332 " {7, 23,\n"
22333 " \"hello world i am a very long line that really, in any\"\n"
22334 " \"just world, ought to be split over multiple lines\"},\n"
22335 " {-1, 93463, \"world\" },\n"
22336 " {56, 5, \"!!\" }\n"
22337 "};",
22338 Style);
22339
22340 verifyNoCrash("Foo f[] = {\n"
22341 " [0] = { 1, },\n"
22342 " [i] { 1, },\n"
22343 "};",
22344 Style);
22345 verifyNoCrash("Foo foo[] = {\n"
22346 " [0] = {1, 1},\n"
22347 " [1] { 1, 1, },\n"
22348 " [2] { 1, 1, },\n"
22349 "};",
22350 Style);
22351 verifyNoCrash("test arr[] = {\n"
22352 "#define FOO(i) {i, i},\n"
22353 "SOME_GENERATOR(FOO)\n"
22354 "{2, 2}\n"
22355 "};",
22356 Style);
22357
22358 verifyFormat("return GradForUnaryCwise(g, {\n"
22359 " {{\"sign\"}, \"Sign\", {\"x\", "
22360 "\"dy\"} },\n"
22361 " {{\"dx\"}, \"Mul\", "
22362 "{\"dy\", \"sign\"}},\n"
22363 "});",
22364 Style);
22365
22366 verifyNoCrash(
22367 "PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
22368 " {\n"
22369 " {PIC(0), PIC(0), PIC(99), PIC(81), 0}, // Backbox\n"
22370 " {PIC(1), PIC(83), PIC(191), PIC(137), 0}, // AK47\n"
22371 "\n"
22372 "#define PICALL1(a, b, c, d) \\\n"
22373 " { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
22374 "\n"
22375 " PICALL1(1, 1, 75, 50),\n"
22376 "};",
22377 Style);
22378
22379 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
22380 verifyFormat("#define FOO \\\n"
22381 " int foo[][2] = { \\\n"
22382 " {0, 1} \\\n"
22383 " };",
22384 Style);
22385
22386 Style.Cpp11BracedListStyle = FormatStyle::BLS_Block;
22387 verifyFormat("struct test demo[] = {\n"
22388 " { 56, 23, \"hello\" },\n"
22389 " { -1, 93463, \"world\" },\n"
22390 " { 7, 5, \"!!\" }\n"
22391 "};",
22392 Style);
22393 Style.Cpp11BracedListStyle = FormatStyle::BLS_AlignFirstComment;
22394
22395 Style.ColumnLimit = 0;
22396 verifyFormat(
22397 "test demo[] = {\n"
22398 " {56, 23, \"hello world i am a very long line that really, in any "
22399 "just world, ought to be split over multiple lines\"},\n"
22400 " {-1, 93463, \"world\" "
22401 " },\n"
22402 " {7, 5, \"!!\" "
22403 " },\n"
22404 "};",
22405 "test demo[] = {{56, 23, \"hello world i am a very long line "
22406 "that really, in any just world, ought to be split over multiple "
22407 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22408 Style);
22409
22410 Style.ColumnLimit = 80;
22411 verifyFormat("test demo[] = {\n"
22412 " {56, 23, /* a comment */ \"hello\"},\n"
22413 " {-1, 93463, \"world\" },\n"
22414 " {7, 5, \"!!\" }\n"
22415 "};",
22416 Style);
22417
22418 verifyFormat("test demo[] = {\n"
22419 " {56, 23, \"hello\" },\n"
22420 " {-1, 93463, \"world\" /* comment here */},\n"
22421 " {7, 5, \"!!\" }\n"
22422 "};",
22423 Style);
22424
22425 verifyFormat("test demo[] = {\n"
22426 " {56, /* a comment */ 23, \"hello\"},\n"
22427 " {-1, 93463, \"world\"},\n"
22428 " {7, 5, \"!!\" }\n"
22429 "};",
22430 Style);
22431 verifyFormat("Foo foo = {\n"
22432 " // comment\n"
22433 " {1, 2}\n"
22434 "};",
22435 Style);
22436
22437 Style.ColumnLimit = 20;
22438 // FIXME: unstable test case
22439 EXPECT_EQ(
22440 "demo = std::array<\n"
22441 " struct test, 3>{\n"
22442 " test{\n"
22443 " 56, 23,\n"
22444 " \"hello \"\n"
22445 " \"world i \"\n"
22446 " \"am a very \"\n"
22447 " \"long line \"\n"
22448 " \"that \"\n"
22449 " \"really, \"\n"
22450 " \"in any \"\n"
22451 " \"just \"\n"
22452 " \"world, \"\n"
22453 " \"ought to \"\n"
22454 " \"be split \"\n"
22455 " \"over \"\n"
22456 " \"multiple \"\n"
22457 " \"lines\"},\n"
22458 " test{-1, 93463,\n"
22459 " \"world\"},\n"
22460 " test{7, 5,\n"
22461 " \"!!\" },\n"
22462 "};",
22463 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
22464 "i am a very long line that really, in any just world, ought "
22465 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
22466 "test{7, 5, \"!!\"},};",
22467 Style));
22468
22469 // This caused a core dump by enabling Alignment in the LLVMStyle globally
22470 Style = getLLVMStyleWithColumns(50);
22471 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
22472 verifyFormat("static A x = {\n"
22473 " {{init1, init2, init3, init4},\n"
22474 " {init1, init2, init3, init4}}\n"
22475 "};",
22476 Style);
22477 Style.ColumnLimit = 100;
22478 verifyFormat(
22479 "test demo[] = {\n"
22480 " {56, 23,\n"
22481 " \"hello world i am a very long line that really, in any just world"
22482 ", ought to be split over \"\n"
22483 " \"multiple lines\" },\n"
22484 " {-1, 93463, \"world\"},\n"
22485 " {7, 5, \"!!\" },\n"
22486 "};",
22487 "test demo[] = {{56, 23, \"hello world i am a very long line "
22488 "that really, in any just world, ought to be split over multiple "
22489 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22490 Style);
22491
22492 Style.ColumnLimit = 25;
22493 verifyNoCrash("Type foo{\n"
22494 " {\n"
22495 " 1, // A\n"
22496 " 2, // B\n"
22497 " 3, // C\n"
22498 " },\n"
22499 " \"hello\",\n"
22500 "};",
22501 Style);
22502 verifyNoCrash("Type object[X][Y] = {\n"
22503 " {{val}, {val}, {val}},\n"
22504 " {{val}, {val}, // some comment\n"
22505 " {val}}\n"
22506 "};",
22507 Style);
22508
22509 Style.ColumnLimit = 120;
22510 verifyNoCrash(
22511 "T v[] {\n"
22512 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
22513 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
22514 "\"00000000000000000000000000000000000000000000000000000000"
22515 "00000000000000000000000000000000000000000000000000000000\" },\n"
22516 "};",
22517 Style);
22518
22519 Style.SpacesInParens = FormatStyle::SIPO_Custom;
22520 Style.SpacesInParensOptions.Other = true;
22521 verifyFormat("Foo foo[] = {\n"
22522 " {1, 1},\n"
22523 " {1, 1},\n"
22524 "};",
22525 Style);
22526 }
22527
22528 TEST_F(FormatTest, UnderstandsPragmas) {
22529 verifyFormat("#pragma omp reduction(| : var)");
22530 verifyFormat("#pragma omp reduction(+ : var)");
22531
22532 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22533 "(including parentheses).",
22534 "#pragma mark Any non-hyphenated or hyphenated string "
22535 "(including parentheses).");
22536
22537 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22538 "(including parentheses).",
22539 "#pragma mark Any non-hyphenated or hyphenated string "
22540 "(including parentheses).");
22541
22542 verifyFormat("#pragma comment(linker, \\\n"
22543 " \"argument\" \\\n"
22544 " \"argument\"",
22545 "#pragma comment(linker, \\\n"
22546 " \"argument\" \\\n"
22547 " \"argument\"",
22548 getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32));
22549 }
22550
22551 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
22552 verifyFormat("#pragma omp target map(to : var)");
22553 verifyFormat("#pragma omp target map(to : var[ : N])");
22554 verifyFormat("#pragma omp target map(to : var[0 : N])");
22555 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22556
22557 verifyFormat(
22558 "#pragma omp target \\\n"
22559 " reduction(+ : var) \\\n"
22560 " map(to : A[0 : N]) \\\n"
22561 " map(to : B[0 : N]) \\\n"
22562 " map(from : C[0 : N]) \\\n"
22563 " firstprivate(i) \\\n"
22564 " firstprivate(j) \\\n"
22565 " firstprivate(k)",
22566 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22567 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22568 getLLVMStyleWithColumns(26));
22569 }
22570
22571 TEST_F(FormatTest, UnderstandPragmaOption) {
22572 verifyFormat("#pragma option -C -A");
22573
22574 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
22575 }
22576
22577 TEST_F(FormatTest, UnderstandPragmaRegion) {
22578 auto Style = getLLVMStyleWithColumns(0);
22579 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
22580 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
22581 }
22582
22583 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
22584 FormatStyle Style = getLLVMStyleWithColumns(20);
22585
22586 // See PR41213
22587 verifyFormat("/*\n"
22588 " *\t9012345\n"
22589 " * /8901\n"
22590 " */",
22591 "/*\n"
22592 " *\t9012345 /8901\n"
22593 " */",
22594 Style);
22595 verifyFormat("/*\n"
22596 " *345678\n"
22597 " *\t/8901\n"
22598 " */",
22599 "/*\n"
22600 " *345678\t/8901\n"
22601 " */",
22602 Style);
22603
22604 verifyFormat("int a; // the\n"
22605 " // comment",
22606 Style);
22607 verifyNoChange("int a; /* first line\n"
22608 " * second\n"
22609 " * line third\n"
22610 " * line\n"
22611 " */",
22612 Style);
22613 verifyFormat("int a; // first line\n"
22614 " // second\n"
22615 " // line third\n"
22616 " // line",
22617 "int a; // first line\n"
22618 " // second line\n"
22619 " // third line",
22620 Style);
22621
22622 Style.PenaltyExcessCharacter = 90;
22623 verifyFormat("int a; // the comment", Style);
22624 verifyFormat("int a; // the comment\n"
22625 " // aaa",
22626 "int a; // the comment aaa", Style);
22627 verifyNoChange("int a; /* first line\n"
22628 " * second line\n"
22629 " * third line\n"
22630 " */",
22631 Style);
22632 verifyFormat("int a; // first line\n"
22633 " // second line\n"
22634 " // third line",
22635 Style);
22636 // FIXME: Investigate why this is not getting the same layout as the test
22637 // above.
22638 verifyFormat("int a; /* first line\n"
22639 " * second line\n"
22640 " * third line\n"
22641 " */",
22642 "int a; /* first line second line third line"
22643 "\n*/",
22644 Style);
22645
22646 verifyFormat("// foo bar baz bazfoo\n"
22647 "// foo bar foo bar",
22648 "// foo bar baz bazfoo\n"
22649 "// foo bar foo bar",
22650 Style);
22651 verifyFormat("// foo bar baz bazfoo\n"
22652 "// foo bar foo bar",
22653 "// foo bar baz bazfoo\n"
22654 "// foo bar foo bar",
22655 Style);
22656
22657 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22658 // next one.
22659 verifyFormat("// foo bar baz bazfoo\n"
22660 "// bar foo bar",
22661 "// foo bar baz bazfoo bar\n"
22662 "// foo bar",
22663 Style);
22664
22665 // FIXME: unstable test case
22666 EXPECT_EQ("// foo bar baz bazfoo\n"
22667 "// foo bar baz bazfoo\n"
22668 "// bar foo bar",
22669 format("// foo bar baz bazfoo\n"
22670 "// foo bar baz bazfoo bar\n"
22671 "// foo bar",
22672 Style));
22673
22674 // FIXME: unstable test case
22675 EXPECT_EQ("// foo bar baz bazfoo\n"
22676 "// foo bar baz bazfoo\n"
22677 "// bar foo bar",
22678 format("// foo bar baz bazfoo\n"
22679 "// foo bar baz bazfoo bar\n"
22680 "// foo bar",
22681 Style));
22682
22683 // Make sure we do not keep protruding characters if strict mode reflow is
22684 // cheaper than keeping protruding characters.
22685 Style.ColumnLimit = 21;
22686 verifyFormat("// foo foo foo foo\n"
22687 "// foo foo foo foo\n"
22688 "// foo foo foo foo",
22689 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
22690
22691 verifyFormat("int a = /* long block\n"
22692 " comment */\n"
22693 " 42;",
22694 "int a = /* long block comment */ 42;", Style);
22695 }
22696
22697 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
22698 FormatStyle Style = getLLVMStyle();
22699 Style.ColumnLimit = 8;
22700 Style.PenaltyExcessCharacter = 15;
22701 verifyFormat("int foo(\n"
22702 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22703 Style);
22704 Style.PenaltyBreakOpenParenthesis = 200;
22705 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22706 "int foo(\n"
22707 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22708 Style);
22709 }
22710
22711 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
22712 FormatStyle Style = getLLVMStyle();
22713 Style.ColumnLimit = 5;
22714 Style.PenaltyExcessCharacter = 150;
22715 verifyFormat("foo((\n"
22716 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22717
22718 Style);
22719 Style.PenaltyBreakOpenParenthesis = 100'000;
22720 verifyFormat("foo((int)\n"
22721 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22722 "foo((\n"
22723 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22724 Style);
22725 }
22726
22727 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
22728 FormatStyle Style = getLLVMStyle();
22729 Style.ColumnLimit = 4;
22730 Style.PenaltyExcessCharacter = 100;
22731 verifyFormat("for (\n"
22732 " int iiiiiiiiiiiiiiiii =\n"
22733 " 0;\n"
22734 " iiiiiiiiiiiiiiiii <\n"
22735 " 2;\n"
22736 " iiiiiiiiiiiiiiiii++) {\n"
22737 "}",
22738
22739 Style);
22740 Style.PenaltyBreakOpenParenthesis = 1250;
22741 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22742 " 0;\n"
22743 " iiiiiiiiiiiiiiiii <\n"
22744 " 2;\n"
22745 " iiiiiiiiiiiiiiiii++) {\n"
22746 "}",
22747 "for (\n"
22748 " int iiiiiiiiiiiiiiiii =\n"
22749 " 0;\n"
22750 " iiiiiiiiiiiiiiiii <\n"
22751 " 2;\n"
22752 " iiiiiiiiiiiiiiiii++) {\n"
22753 "}",
22754 Style);
22755 }
22756
22757 TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
22758 auto Style = getLLVMStyle();
22759 EXPECT_EQ(Style.PenaltyBreakBeforeMemberAccess, 150u);
22760
22761 Style.ColumnLimit = 60;
22762 Style.PenaltyBreakBeforeMemberAccess = 110;
22763 verifyFormat("aaaaaaaa.aaaaaaaa.bbbbbbbb()\n"
22764 " .ccccccccccccccccccccc(dddddddd);\n"
22765 "aaaaaaaa.aaaaaaaa\n"
22766 " .bbbbbbbb(cccccccccccccccccccccccccccccccc);",
22767 Style);
22768
22769 Style.ColumnLimit = 13;
22770 verifyFormat("foo->bar\n"
22771 " .b(a);",
22772 Style);
22773 }
22774
22775 TEST_F(FormatTest, BreakPenaltyScopeResolution) {
22776 FormatStyle Style = getLLVMStyle();
22777 Style.ColumnLimit = 20;
22778 Style.PenaltyExcessCharacter = 100;
22779 verifyFormat("unsigned long\n"
22780 "foo::bar();",
22781 Style);
22782 Style.PenaltyBreakScopeResolution = 10;
22783 verifyFormat("unsigned long foo::\n"
22784 " bar();",
22785 Style);
22786 }
22787
22788 TEST_F(FormatTest, WorksFor8bitEncodings) {
22789 // FIXME: unstable test case
22790 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22791 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22792 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22793 "\"\xef\xee\xf0\xf3...\"",
22794 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22795 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22796 "\xef\xee\xf0\xf3...\"",
22797 getLLVMStyleWithColumns(12)));
22798 }
22799
22800 TEST_F(FormatTest, HandlesUTF8BOM) {
22801 verifyFormat("\xef\xbb\xbf");
22802 verifyFormat("\xef\xbb\xbf#include <iostream>");
22803 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22804
22805 auto Style = getLLVMStyle();
22806 Style.KeepEmptyLines.AtStartOfFile = false;
22807 verifyFormat("\xef\xbb\xbf#include <iostream>",
22808 "\xef\xbb\xbf\n#include <iostream>", Style);
22809 }
22810
22811 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22812 #if !defined(_MSC_VER)
22813
22814 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
22815 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22816 getLLVMStyleWithColumns(35));
22817 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22818 getLLVMStyleWithColumns(31));
22819 verifyFormat("// Однажды в студёную зимнюю пору...",
22820 getLLVMStyleWithColumns(36));
22821 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22822 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22823 getLLVMStyleWithColumns(39));
22824 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22825 getLLVMStyleWithColumns(35));
22826 }
22827
22828 TEST_F(FormatTest, SplitsUTF8Strings) {
22829 // Non-printable characters' width is currently considered to be the length in
22830 // bytes in UTF8. The characters can be displayed in very different manner
22831 // (zero-width, single width with a substitution glyph, expanded to their code
22832 // (e.g. "<8d>"), so there's no single correct way to handle them.
22833 // FIXME: unstable test case
22834 EXPECT_EQ("\"aaaaÄ\"\n"
22835 "\"\xc2\x8d\";",
22836 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22837 // FIXME: unstable test case
22838 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22839 "\"\xc2\x8d\";",
22840 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22841 // FIXME: unstable test case
22842 EXPECT_EQ("\"Однажды, в \"\n"
22843 "\"студёную \"\n"
22844 "\"зимнюю \"\n"
22845 "\"пору,\"",
22846 format("\"Однажды, в студёную зимнюю пору,\"",
22847 getLLVMStyleWithColumns(13)));
22848 // FIXME: unstable test case
22849 EXPECT_EQ(
22850 "\"一 二 三 \"\n"
22851 "\"四 五六 \"\n"
22852 "\"七 八 九 \"\n"
22853 "\"十\"",
22854 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22855 // FIXME: unstable test case
22856 EXPECT_EQ("\"一\t\"\n"
22857 "\"二 \t\"\n"
22858 "\"三 四 \"\n"
22859 "\"五\t\"\n"
22860 "\"六 \t\"\n"
22861 "\"七 \"\n"
22862 "\"八九十\tqq\"",
22863 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
22864 getLLVMStyleWithColumns(11)));
22865
22866 // UTF8 character in an escape sequence.
22867 // FIXME: unstable test case
22868 EXPECT_EQ("\"aaaaaa\"\n"
22869 "\"\\\xC2\x8D\"",
22870 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22871 }
22872
22873 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
22874 verifyFormat("const char *sssss =\n"
22875 " \"一二三四五六七八\\\n"
22876 " 九 十\";",
22877 "const char *sssss = \"一二三四五六七八\\\n"
22878 " 九 十\";",
22879 getLLVMStyleWithColumns(30));
22880 }
22881
22882 TEST_F(FormatTest, SplitsUTF8LineComments) {
22883 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22884 verifyFormat("// Я из лесу\n"
22885 "// вышел; был\n"
22886 "// сильный\n"
22887 "// мороз.",
22888 "// Я из лесу вышел; был сильный мороз.",
22889 getLLVMStyleWithColumns(13));
22890 verifyFormat("// 一二三\n"
22891 "// 四五六七\n"
22892 "// 八 九\n"
22893 "// 十",
22894 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22895 }
22896
22897 TEST_F(FormatTest, SplitsUTF8BlockComments) {
22898 verifyFormat("/* Гляжу,\n"
22899 " * поднимается\n"
22900 " * медленно в\n"
22901 " * гору\n"
22902 " * Лошадка,\n"
22903 " * везущая\n"
22904 " * хворосту\n"
22905 " * воз. */",
22906 "/* Гляжу, поднимается медленно в гору\n"
22907 " * Лошадка, везущая хворосту воз. */",
22908 getLLVMStyleWithColumns(13));
22909 verifyFormat("/* 一二三\n"
22910 " * 四五六七\n"
22911 " * 八 九\n"
22912 " * 十 */",
22913 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22914 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22915 " * 𝕓𝕪𝕥𝕖\n"
22916 " * 𝖀𝕿𝕱-𝟠 */",
22917 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22918 }
22919
22920 #endif // _MSC_VER
22921
22922 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22923 FormatStyle Style = getLLVMStyle();
22924
22925 Style.ConstructorInitializerIndentWidth = 4;
22926 verifyFormat(
22927 "SomeClass::Constructor()\n"
22928 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22929 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22930 Style);
22931
22932 Style.ConstructorInitializerIndentWidth = 2;
22933 verifyFormat(
22934 "SomeClass::Constructor()\n"
22935 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22936 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22937 Style);
22938
22939 Style.ConstructorInitializerIndentWidth = 0;
22940 verifyFormat(
22941 "SomeClass::Constructor()\n"
22942 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22943 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22944 Style);
22945 Style.BreakAfterOpenBracketFunction = true;
22946 verifyFormat(
22947 "SomeLongTemplateVariableName<\n"
22948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22949 Style);
22950 verifyFormat("bool smaller = 1 < "
22951 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22952 " "
22953 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22954 Style);
22955
22956 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22957 verifyFormat("SomeClass::Constructor() :\n"
22958 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22959 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22960 Style);
22961 }
22962
22963 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
22964 FormatStyle Style = getLLVMStyle();
22965 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22966 Style.ConstructorInitializerIndentWidth = 4;
22967 verifyFormat("SomeClass::Constructor()\n"
22968 " : a(a)\n"
22969 " , b(b)\n"
22970 " , c(c) {}",
22971 Style);
22972 verifyFormat("SomeClass::Constructor()\n"
22973 " : a(a) {}",
22974 Style);
22975
22976 Style.ColumnLimit = 0;
22977 verifyFormat("SomeClass::Constructor()\n"
22978 " : a(a) {}",
22979 Style);
22980 verifyFormat("SomeClass::Constructor() noexcept\n"
22981 " : a(a) {}",
22982 Style);
22983 verifyFormat("SomeClass::Constructor()\n"
22984 " : a(a)\n"
22985 " , b(b)\n"
22986 " , c(c) {}",
22987 Style);
22988 verifyFormat("SomeClass::Constructor()\n"
22989 " : a(a) {\n"
22990 " foo();\n"
22991 " bar();\n"
22992 "}",
22993 Style);
22994
22995 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22996 verifyFormat("SomeClass::Constructor()\n"
22997 " : a(a)\n"
22998 " , b(b)\n"
22999 " , c(c) {\n}",
23000 Style);
23001 verifyFormat("SomeClass::Constructor()\n"
23002 " : a(a) {\n}",
23003 Style);
23004
23005 Style.ColumnLimit = 80;
23006 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
23007 Style.ConstructorInitializerIndentWidth = 2;
23008 verifyFormat("SomeClass::Constructor()\n"
23009 " : a(a)\n"
23010 " , b(b)\n"
23011 " , c(c) {}",
23012 Style);
23013
23014 Style.ConstructorInitializerIndentWidth = 0;
23015 verifyFormat("SomeClass::Constructor()\n"
23016 ": a(a)\n"
23017 ", b(b)\n"
23018 ", c(c) {}",
23019 Style);
23020
23021 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
23022 Style.ConstructorInitializerIndentWidth = 4;
23023 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
23024 verifyFormat(
23025 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
23026 Style);
23027 verifyFormat(
23028 "SomeClass::Constructor()\n"
23029 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
23030 Style);
23031 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
23032 verifyFormat("SomeClass::Constructor()\n"
23033 " : aaaaaaaa(aaaaaaaa) {}",
23034 Style);
23035 verifyFormat("SomeClass::Constructor()\n"
23036 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
23037 Style);
23038 verifyFormat(
23039 "SomeClass::Constructor()\n"
23040 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
23041 Style);
23042
23043 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
23044 Style.ConstructorInitializerIndentWidth = 4;
23045 Style.ColumnLimit = 60;
23046 verifyFormat("SomeClass::Constructor()\n"
23047 " : aaaaaaaa(aaaaaaaa)\n"
23048 " , aaaaaaaa(aaaaaaaa)\n"
23049 " , aaaaaaaa(aaaaaaaa) {}",
23050 Style);
23051 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
23052 verifyFormat("SomeClass::Constructor()\n"
23053 " : aaaaaaaa(aaaaaaaa)\n"
23054 " , aaaaaaaa(aaaaaaaa)\n"
23055 " , aaaaaaaa(aaaaaaaa) {}",
23056 Style);
23057 }
23058
23059 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
23060 FormatStyle Style = getLLVMStyle();
23061 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
23062 Style.ConstructorInitializerIndentWidth = 4;
23063 verifyFormat("SomeClass::Constructor()\n"
23064 " : a{a}\n"
23065 " , b{b} {}",
23066 Style);
23067 verifyFormat("SomeClass::Constructor()\n"
23068 " : a{a}\n"
23069 "#if CONDITION\n"
23070 " , b{b}\n"
23071 "#endif\n"
23072 "{\n}",
23073 Style);
23074 Style.ConstructorInitializerIndentWidth = 2;
23075 verifyFormat("SomeClass::Constructor()\n"
23076 "#if CONDITION\n"
23077 " : a{a}\n"
23078 "#endif\n"
23079 " , b{b}\n"
23080 " , c{c} {\n}",
23081 Style);
23082 Style.ConstructorInitializerIndentWidth = 0;
23083 verifyFormat("SomeClass::Constructor()\n"
23084 ": a{a}\n"
23085 "#ifdef CONDITION\n"
23086 ", b{b}\n"
23087 "#else\n"
23088 ", c{c}\n"
23089 "#endif\n"
23090 ", d{d} {\n}",
23091 Style);
23092 Style.ConstructorInitializerIndentWidth = 4;
23093 verifyFormat("SomeClass::Constructor()\n"
23094 " : a{a}\n"
23095 "#if WINDOWS\n"
23096 "#if DEBUG\n"
23097 " , b{0}\n"
23098 "#else\n"
23099 " , b{1}\n"
23100 "#endif\n"
23101 "#else\n"
23102 "#if DEBUG\n"
23103 " , b{2}\n"
23104 "#else\n"
23105 " , b{3}\n"
23106 "#endif\n"
23107 "#endif\n"
23108 "{\n}",
23109 Style);
23110 verifyFormat("SomeClass::Constructor()\n"
23111 " : a{a}\n"
23112 "#if WINDOWS\n"
23113 " , b{0}\n"
23114 "#if DEBUG\n"
23115 " , c{0}\n"
23116 "#else\n"
23117 " , c{1}\n"
23118 "#endif\n"
23119 "#else\n"
23120 "#if DEBUG\n"
23121 " , c{2}\n"
23122 "#else\n"
23123 " , c{3}\n"
23124 "#endif\n"
23125 " , b{1}\n"
23126 "#endif\n"
23127 "{\n}",
23128 Style);
23129 }
23130
23131 TEST_F(FormatTest, Destructors) {
23132 verifyFormat("void F(int &i) { i.~int(); }");
23133 verifyFormat("void F(int &i) { i->~int(); }");
23134 }
23135
23136 TEST_F(FormatTest, FormatsWithWebKitStyle) {
23137 FormatStyle Style = getWebKitStyle();
23138
23139 // Don't indent in outer namespaces.
23140 verifyFormat("namespace outer {\n"
23141 "int i;\n"
23142 "namespace inner {\n"
23143 " int i;\n"
23144 "} // namespace inner\n"
23145 "} // namespace outer\n"
23146 "namespace other_outer {\n"
23147 "int i;\n"
23148 "}",
23149 Style);
23150
23151 // Don't indent case labels.
23152 verifyFormat("switch (variable) {\n"
23153 "case 1:\n"
23154 "case 2:\n"
23155 " doSomething();\n"
23156 " break;\n"
23157 "default:\n"
23158 " ++variable;\n"
23159 "}",
23160 Style);
23161
23162 // Wrap before binary operators.
23163 verifyFormat(
23164 "void f()\n"
23165 "{\n"
23166 " if (aaaaaaaaaaaaaaaa\n"
23167 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
23168 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
23169 " return;\n"
23170 "}",
23171 "void f() {\n"
23172 "if (aaaaaaaaaaaaaaaa\n"
23173 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
23174 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
23175 "return;\n"
23176 "}",
23177 Style);
23178
23179 // Allow functions on a single line.
23180 verifyFormat("void f() { return; }", Style);
23181
23182 // Allow empty blocks on a single line and insert a space in empty blocks.
23183 verifyFormat("void f() { }", "void f() {}", Style);
23184 verifyFormat("while (true) { }", "while (true) {}", Style);
23185 // However, don't merge non-empty short loops.
23186 verifyFormat("while (true) {\n"
23187 " continue;\n"
23188 "}",
23189 "while (true) { continue; }", Style);
23190
23191 // Constructor initializers are formatted one per line with the "," on the
23192 // new line.
23193 verifyFormat("Constructor()\n"
23194 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
23195 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
23196 " aaaaaaaaaaaaaa)\n"
23197 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
23198 "{\n"
23199 "}",
23200 Style);
23201 verifyFormat("SomeClass::Constructor()\n"
23202 " : a(a)\n"
23203 "{\n"
23204 "}",
23205 Style);
23206 verifyFormat("SomeClass::Constructor()\n"
23207 " : a(a)\n"
23208 "{\n"
23209 "}",
23210 "SomeClass::Constructor():a(a){}", Style);
23211 verifyFormat("SomeClass::Constructor()\n"
23212 " : a(a)\n"
23213 " , b(b)\n"
23214 " , c(c)\n"
23215 "{\n"
23216 "}",
23217 Style);
23218 verifyFormat("SomeClass::Constructor()\n"
23219 " : a(a)\n"
23220 "{\n"
23221 " foo();\n"
23222 " bar();\n"
23223 "}",
23224 Style);
23225
23226 // Access specifiers should be aligned left.
23227 verifyFormat("class C {\n"
23228 "public:\n"
23229 " int i;\n"
23230 "};",
23231 Style);
23232
23233 // Do not align comments.
23234 verifyFormat("int a; // Do not\n"
23235 "double b; // align comments.",
23236 Style);
23237
23238 // Do not align operands.
23239 verifyFormat("ASSERT(aaaa\n"
23240 " || bbbb);",
23241 "ASSERT ( aaaa\n||bbbb);", Style);
23242
23243 // Accept input's line breaks.
23244 verifyFormat("if (aaaaaaaaaaaaaaa\n"
23245 " || bbbbbbbbbbbbbbb) {\n"
23246 " i++;\n"
23247 "}",
23248 "if (aaaaaaaaaaaaaaa\n"
23249 "|| bbbbbbbbbbbbbbb) { i++; }",
23250 Style);
23251 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
23252 " i++;\n"
23253 "}",
23254 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
23255
23256 // Don't automatically break all macro definitions (llvm.org/PR17842).
23257 verifyFormat("#define aNumber 10", Style);
23258 // However, generally keep the line breaks that the user authored.
23259 verifyFormat("#define aNumber \\\n"
23260 " 10",
23261 "#define aNumber \\\n"
23262 " 10",
23263 Style);
23264
23265 // Keep empty and one-element array literals on a single line.
23266 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
23267 " copyItems:YES];",
23268 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
23269 "copyItems:YES];",
23270 Style);
23271 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
23272 " copyItems:YES];",
23273 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
23274 " copyItems:YES];",
23275 Style);
23276 // FIXME: This does not seem right, there should be more indentation before
23277 // the array literal's entries. Nested blocks have the same problem.
23278 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
23279 " @\"a\",\n"
23280 " @\"a\"\n"
23281 "]\n"
23282 " copyItems:YES];",
23283 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
23284 " @\"a\",\n"
23285 " @\"a\"\n"
23286 " ]\n"
23287 " copyItems:YES];",
23288 Style);
23289 verifyFormat(
23290 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
23291 " copyItems:YES];",
23292 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
23293 " copyItems:YES];",
23294 Style);
23295
23296 verifyFormat("[self.a b:c c:d];", Style);
23297 verifyFormat("[self.a b:c\n"
23298 " c:d];",
23299 "[self.a b:c\n"
23300 "c:d];",
23301 Style);
23302 }
23303
23304 TEST_F(FormatTest, FormatsLambdas) {
23305 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
23306 verifyFormat(
23307 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
23308 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
23309 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
23310 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
23311 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
23312 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
23313 verifyFormat("auto c = [a = [b = 42] {}] {};");
23314 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
23315 verifyFormat("int x = f(*+[] {});");
23316 verifyFormat("void f() {\n"
23317 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
23318 "}");
23319 verifyFormat("void f() {\n"
23320 " other(x.begin(), //\n"
23321 " x.end(), //\n"
23322 " [&](int, int) { return 1; });\n"
23323 "}");
23324 verifyFormat("void f() {\n"
23325 " other.other.other.other.other(\n"
23326 " x.begin(), x.end(),\n"
23327 " [something, rather](int, int, int, int, int, int, int) { "
23328 "return 1; });\n"
23329 "}");
23330 verifyFormat(
23331 "void f() {\n"
23332 " other.other.other.other.other(\n"
23333 " x.begin(), x.end(),\n"
23334 " [something, rather](int, int, int, int, int, int, int) {\n"
23335 " //\n"
23336 " });\n"
23337 "}");
23338 verifyFormat("SomeFunction([]() { // A cool function...\n"
23339 " return 43;\n"
23340 "});");
23341 verifyFormat("SomeFunction([]() {\n"
23342 "#define A a\n"
23343 " return 43;\n"
23344 "});",
23345 "SomeFunction([](){\n"
23346 "#define A a\n"
23347 "return 43;\n"
23348 "});");
23349 verifyFormat("void f() {\n"
23350 " SomeFunction([](decltype(x), A *a) {});\n"
23351 " SomeFunction([](typeof(x), A *a) {});\n"
23352 " SomeFunction([](_Atomic(x), A *a) {});\n"
23353 " SomeFunction([](__underlying_type(x), A *a) {});\n"
23354 "}");
23355 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23356 " [](const aaaaaaaaaa &a) { return a; });");
23357 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
23358 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
23359 "});");
23360 verifyFormat("Constructor()\n"
23361 " : Field([] { // comment\n"
23362 " int i;\n"
23363 " }) {}");
23364 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
23365 " return some_parameter.size();\n"
23366 "};");
23367 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
23368 " [](const string &s) { return s; };");
23369 verifyFormat("int i = aaaaaa ? 1 //\n"
23370 " : [] {\n"
23371 " return 2; //\n"
23372 " }();");
23373 verifyFormat("llvm::errs() << \"number of twos is \"\n"
23374 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
23375 " return x == 2; // force break\n"
23376 " });");
23377 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23378 " [=](int iiiiiiiiiiii) {\n"
23379 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
23380 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
23381 " });",
23382 getLLVMStyleWithColumns(60));
23383
23384 verifyFormat("SomeFunction({[&] {\n"
23385 " // comment\n"
23386 " },\n"
23387 " [&] {\n"
23388 " // comment\n"
23389 " }});");
23390 verifyFormat("SomeFunction({[&] {\n"
23391 " // comment\n"
23392 "}});");
23393 verifyFormat(
23394 "virtual aaaaaaaaaaaaaaaa(\n"
23395 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
23396 " aaaaa aaaaaaaaa);");
23397
23398 // Lambdas with return types.
23399 verifyFormat("int c = []() -> int { return 2; }();");
23400 verifyFormat("int c = []() -> int * { return 2; }();");
23401 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
23402 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
23403 verifyFormat("foo([]() noexcept -> int {});");
23404 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
23405 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
23406 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
23407 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
23408 verifyFormat("[a, a]() -> a<1> {};");
23409 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
23410 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
23411 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
23412 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
23413 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
23414 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
23415 verifyFormat("[]() -> foo<!5> { return {}; };");
23416 verifyFormat("[]() -> foo<~5> { return {}; };");
23417 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
23418 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
23419 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
23420 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
23421 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
23422 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
23423 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
23424 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
23425 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
23426 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
23427 verifyFormat("namespace bar {\n"
23428 "// broken:\n"
23429 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
23430 "} // namespace bar");
23431 verifyFormat("namespace bar {\n"
23432 "// broken:\n"
23433 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
23434 "} // namespace bar");
23435 verifyFormat("namespace bar {\n"
23436 "// broken:\n"
23437 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
23438 "} // namespace bar");
23439 verifyFormat("namespace bar {\n"
23440 "// broken:\n"
23441 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
23442 "} // namespace bar");
23443 verifyFormat("namespace bar {\n"
23444 "// broken:\n"
23445 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
23446 "} // namespace bar");
23447 verifyFormat("namespace bar {\n"
23448 "// broken:\n"
23449 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
23450 "} // namespace bar");
23451 verifyFormat("namespace bar {\n"
23452 "// broken:\n"
23453 "auto foo{[]() -> foo<!5> { return {}; }};\n"
23454 "} // namespace bar");
23455 verifyFormat("namespace bar {\n"
23456 "// broken:\n"
23457 "auto foo{[]() -> foo<~5> { return {}; }};\n"
23458 "} // namespace bar");
23459 verifyFormat("namespace bar {\n"
23460 "// broken:\n"
23461 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
23462 "} // namespace bar");
23463 verifyFormat("namespace bar {\n"
23464 "// broken:\n"
23465 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
23466 "} // namespace bar");
23467 verifyFormat("namespace bar {\n"
23468 "// broken:\n"
23469 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
23470 "} // namespace bar");
23471 verifyFormat("namespace bar {\n"
23472 "// broken:\n"
23473 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
23474 "} // namespace bar");
23475 verifyFormat("namespace bar {\n"
23476 "// broken:\n"
23477 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
23478 "} // namespace bar");
23479 verifyFormat("namespace bar {\n"
23480 "// broken:\n"
23481 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
23482 "} // namespace bar");
23483 verifyFormat("namespace bar {\n"
23484 "// broken:\n"
23485 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
23486 "} // namespace bar");
23487 verifyFormat("namespace bar {\n"
23488 "// broken:\n"
23489 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
23490 "} // namespace bar");
23491 verifyFormat("namespace bar {\n"
23492 "// broken:\n"
23493 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
23494 "} // namespace bar");
23495 verifyFormat("namespace bar {\n"
23496 "// broken:\n"
23497 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
23498 "} // namespace bar");
23499 verifyFormat("[]() -> a<1> {};");
23500 verifyFormat("[]() -> a<1> { ; };");
23501 verifyFormat("[]() -> a<1> { ; }();");
23502 verifyFormat("[a, a]() -> a<true> {};");
23503 verifyFormat("[]() -> a<true> {};");
23504 verifyFormat("[]() -> a<true> { ; };");
23505 verifyFormat("[]() -> a<true> { ; }();");
23506 verifyFormat("[a, a]() -> a<false> {};");
23507 verifyFormat("[]() -> a<false> {};");
23508 verifyFormat("[]() -> a<false> { ; };");
23509 verifyFormat("[]() -> a<false> { ; }();");
23510 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
23511 verifyFormat("namespace bar {\n"
23512 "auto foo{[]() -> foo<false> { ; }};\n"
23513 "} // namespace bar");
23514 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
23515 " int j) -> int {\n"
23516 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
23517 "};");
23518 verifyFormat(
23519 "aaaaaaaaaaaaaaaaaaaaaa(\n"
23520 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
23521 " return aaaaaaaaaaaaaaaaa;\n"
23522 " });",
23523 getLLVMStyleWithColumns(70));
23524 verifyFormat("[]() //\n"
23525 " -> int {\n"
23526 " return 1; //\n"
23527 "};");
23528 verifyFormat("[]() -> Void<T...> {};");
23529 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
23530 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
23531 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
23532 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
23533 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
23534 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
23535 verifyFormat("return int{[x = x]() { return x; }()};");
23536
23537 // Lambdas with explicit template argument lists.
23538 verifyFormat(
23539 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
23540 verifyFormat("auto L = []<class T>(T) {\n"
23541 " {\n"
23542 " f();\n"
23543 " g();\n"
23544 " }\n"
23545 "};");
23546 verifyFormat("auto L = []<class... T>(T...) {\n"
23547 " {\n"
23548 " f();\n"
23549 " g();\n"
23550 " }\n"
23551 "};");
23552 verifyFormat("auto L = []<typename... T>(T...) {\n"
23553 " {\n"
23554 " f();\n"
23555 " g();\n"
23556 " }\n"
23557 "};");
23558 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23559 " {\n"
23560 " f();\n"
23561 " g();\n"
23562 " }\n"
23563 "};");
23564 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23565 " {\n"
23566 " f();\n"
23567 " g();\n"
23568 " }\n"
23569 "};");
23570 verifyFormat("auto L = []<int... T>(T...) {\n"
23571 " {\n"
23572 " f();\n"
23573 " g();\n"
23574 " }\n"
23575 "};");
23576 verifyFormat("auto L = []<Foo... T>(T...) {\n"
23577 " {\n"
23578 " f();\n"
23579 " g();\n"
23580 " }\n"
23581 "};");
23582
23583 // Lambdas that fit on a single line within an argument list are not forced
23584 // onto new lines.
23585 verifyFormat("SomeFunction([] {});");
23586 verifyFormat("SomeFunction(0, [] {});");
23587 verifyFormat("SomeFunction([] {}, 0);");
23588 verifyFormat("SomeFunction(0, [] {}, 0);");
23589 verifyFormat("SomeFunction([] { return 0; }, 0);");
23590 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23591 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23592 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23593 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23594 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
23595 // Exceeded column limit. We need to break.
23596 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23597 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
23598 "return 0; }, b);");
23599
23600 // Multiple multi-line lambdas in the same parentheses change indentation
23601 // rules. These lambdas are always forced to start on new lines.
23602 verifyFormat("SomeFunction(\n"
23603 " []() {\n"
23604 " //\n"
23605 " },\n"
23606 " []() {\n"
23607 " //\n"
23608 " });");
23609
23610 // A multi-line lambda passed as arg0 is always pushed to the next line.
23611 verifyFormat("SomeFunction(\n"
23612 " [this] {\n"
23613 " //\n"
23614 " },\n"
23615 " 1);");
23616
23617 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23618 // the arg0 case above.
23619 auto Style = getGoogleStyle();
23620 Style.BinPackArguments = false;
23621 verifyFormat("SomeFunction(\n"
23622 " a,\n"
23623 " [this] {\n"
23624 " //\n"
23625 " },\n"
23626 " b);",
23627 Style);
23628 verifyFormat("SomeFunction(\n"
23629 " a,\n"
23630 " [this] {\n"
23631 " //\n"
23632 " },\n"
23633 " b);");
23634
23635 // A lambda with a very long line forces arg0 to be pushed out irrespective of
23636 // the BinPackArguments value (as long as the code is wide enough).
23637 verifyFormat(
23638 "something->SomeFunction(\n"
23639 " a,\n"
23640 " [this] {\n"
23641 " "
23642 "D0000000000000000000000000000000000000000000000000000000000001();\n"
23643 " },\n"
23644 " b);");
23645
23646 // A multi-line lambda is pulled up as long as the introducer fits on the
23647 // previous line and there are no further args.
23648 verifyFormat("function(1, [this, that] {\n"
23649 " //\n"
23650 "});");
23651 verifyFormat("function([this, that] {\n"
23652 " //\n"
23653 "});");
23654 // FIXME: this format is not ideal and we should consider forcing the first
23655 // arg onto its own line.
23656 verifyFormat("function(a, b, c, //\n"
23657 " d, [this, that] {\n"
23658 " //\n"
23659 " });");
23660
23661 // Multiple lambdas are treated correctly even when there is a short arg0.
23662 verifyFormat("SomeFunction(\n"
23663 " 1,\n"
23664 " [this] {\n"
23665 " //\n"
23666 " },\n"
23667 " [this] {\n"
23668 " //\n"
23669 " },\n"
23670 " 1);");
23671
23672 // More complex introducers.
23673 verifyFormat("return [i, args...] {};");
23674
23675 // Not lambdas.
23676 verifyFormat("constexpr char hello[]{\"hello\"};");
23677 verifyFormat("double &operator[](int i) { return 0; }\n"
23678 "int i;");
23679 verifyFormat("std::unique_ptr<int[]> foo() {}");
23680 verifyFormat("int i = a[a][a]->f();");
23681 verifyFormat("int i = (*b)[a]->f();");
23682
23683 // Other corner cases.
23684 verifyFormat("void f() {\n"
23685 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23686 " );\n"
23687 "}");
23688 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23689
23690 // Lambdas created through weird macros.
23691 verifyFormat("void f() {\n"
23692 " MACRO((const AA &a) { return 1; });\n"
23693 " MACRO((AA &a) { return 1; });\n"
23694 "}");
23695
23696 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23697 " doo_dah();\n"
23698 " doo_dah();\n"
23699 " })) {\n"
23700 "}");
23701 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23702 " doo_dah();\n"
23703 " doo_dah();\n"
23704 " })) {\n"
23705 "}");
23706 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23707 " doo_dah();\n"
23708 " doo_dah();\n"
23709 " })) {\n"
23710 "}");
23711 verifyFormat("auto lambda = []() {\n"
23712 " int a = 2\n"
23713 "#if A\n"
23714 " + 2\n"
23715 "#endif\n"
23716 " ;\n"
23717 "};");
23718
23719 // Lambdas with complex multiline introducers.
23720 verifyFormat(
23721 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23722 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23723 " -> ::std::unordered_set<\n"
23724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23725 " //\n"
23726 " });");
23727
23728 FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
23729 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23730 " [](auto n) noexcept [[back_attr]]\n"
23731 " -> std::unordered_map<very_long_type_name_A,\n"
23732 " very_long_type_name_B> {\n"
23733 " really_do_something();\n"
23734 " });",
23735 LLVMStyle);
23736 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23737 " [](auto n) constexpr\n"
23738 " -> std::unordered_map<very_long_type_name_A,\n"
23739 " very_long_type_name_B> {\n"
23740 " really_do_something();\n"
23741 " });",
23742 LLVMStyle);
23743
23744 FormatStyle DoNotMerge = getLLVMStyle();
23745 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
23746 verifyFormat("auto c = []() {\n"
23747 " return b;\n"
23748 "};",
23749 "auto c = []() { return b; };", DoNotMerge);
23750 verifyFormat("auto c = []() {\n"
23751 "};",
23752 " auto c = []() {};", DoNotMerge);
23753
23754 FormatStyle MergeEmptyOnly = getLLVMStyle();
23755 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
23756 verifyFormat("auto c = []() {\n"
23757 " return b;\n"
23758 "};",
23759 "auto c = []() {\n"
23760 " return b;\n"
23761 " };",
23762 MergeEmptyOnly);
23763 verifyFormat("auto c = []() {};",
23764 "auto c = []() {\n"
23765 "};",
23766 MergeEmptyOnly);
23767
23768 FormatStyle MergeInline = getLLVMStyle();
23769 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
23770 verifyFormat("auto c = []() {\n"
23771 " return b;\n"
23772 "};",
23773 "auto c = []() { return b; };", MergeInline);
23774 verifyFormat("function([]() { return b; })", MergeInline);
23775 verifyFormat("function([]() { return b; }, a)", MergeInline);
23776 verifyFormat("function(a, []() { return b; })", MergeInline);
23777 verifyFormat("auto guard = foo{[&] { exit_status = true; }};", MergeInline);
23778
23779 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23780 // AllowShortLambdasOnASingleLine
23781 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23782 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23783 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23784 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23785 FormatStyle::SLS_None;
23786 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23787 " []()\n"
23788 " {\n"
23789 " return 17;\n"
23790 " });",
23791 LLVMWithBeforeLambdaBody);
23792 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23793 " []()\n"
23794 " {\n"
23795 " });",
23796 LLVMWithBeforeLambdaBody);
23797 verifyFormat("auto fct_SLS_None = []()\n"
23798 "{\n"
23799 " return 17;\n"
23800 "};",
23801 LLVMWithBeforeLambdaBody);
23802 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23803 " []()\n"
23804 " {\n"
23805 " return Call(\n"
23806 " []()\n"
23807 " {\n"
23808 " return 17;\n"
23809 " });\n"
23810 " });",
23811 LLVMWithBeforeLambdaBody);
23812 verifyFormat("void Fct() {\n"
23813 " return {[]()\n"
23814 " {\n"
23815 " return 17;\n"
23816 " }};\n"
23817 "}",
23818 LLVMWithBeforeLambdaBody);
23819
23820 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23821 FormatStyle::SLS_Empty;
23822 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23823 " []()\n"
23824 " {\n"
23825 " return 17;\n"
23826 " });",
23827 LLVMWithBeforeLambdaBody);
23828 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23829 LLVMWithBeforeLambdaBody);
23830 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23831 "ongFunctionName_SLS_Empty(\n"
23832 " []() {});",
23833 LLVMWithBeforeLambdaBody);
23834 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23835 " []()\n"
23836 " {\n"
23837 " return 17;\n"
23838 " });",
23839 LLVMWithBeforeLambdaBody);
23840 verifyFormat("auto fct_SLS_Empty = []()\n"
23841 "{\n"
23842 " return 17;\n"
23843 "};",
23844 LLVMWithBeforeLambdaBody);
23845 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23846 " []()\n"
23847 " {\n"
23848 " return Call([]() {});\n"
23849 " });",
23850 LLVMWithBeforeLambdaBody);
23851 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23852 " []()\n"
23853 " {\n"
23854 " return Call([]() {});\n"
23855 " });",
23856 LLVMWithBeforeLambdaBody);
23857 verifyFormat(
23858 "FctWithLongLineInLambda_SLS_Empty(\n"
23859 " []()\n"
23860 " {\n"
23861 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23862 " AndShouldNotBeConsiderAsInline,\n"
23863 " LambdaBodyMustBeBreak);\n"
23864 " });",
23865 LLVMWithBeforeLambdaBody);
23866
23867 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23868 FormatStyle::SLS_Inline;
23869 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23870 LLVMWithBeforeLambdaBody);
23871 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23872 LLVMWithBeforeLambdaBody);
23873 verifyFormat("auto fct_SLS_Inline = []()\n"
23874 "{\n"
23875 " return 17;\n"
23876 "};",
23877 LLVMWithBeforeLambdaBody);
23878 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23879 "17; }); });",
23880 LLVMWithBeforeLambdaBody);
23881 verifyFormat(
23882 "FctWithLongLineInLambda_SLS_Inline(\n"
23883 " []()\n"
23884 " {\n"
23885 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23886 " AndShouldNotBeConsiderAsInline,\n"
23887 " LambdaBodyMustBeBreak);\n"
23888 " });",
23889 LLVMWithBeforeLambdaBody);
23890 verifyFormat("FctWithMultipleParams_SLS_Inline("
23891 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23892 " []() { return 17; });",
23893 LLVMWithBeforeLambdaBody);
23894 verifyFormat(
23895 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23896 LLVMWithBeforeLambdaBody);
23897
23898 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23899 FormatStyle::SLS_All;
23900 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23901 LLVMWithBeforeLambdaBody);
23902 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23903 LLVMWithBeforeLambdaBody);
23904 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23905 LLVMWithBeforeLambdaBody);
23906 verifyFormat("FctWithOneParam_SLS_All(\n"
23907 " []()\n"
23908 " {\n"
23909 " // A cool function...\n"
23910 " return 43;\n"
23911 " });",
23912 LLVMWithBeforeLambdaBody);
23913 verifyFormat("FctWithMultipleParams_SLS_All("
23914 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23915 " []() { return 17; });",
23916 LLVMWithBeforeLambdaBody);
23917 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23918 LLVMWithBeforeLambdaBody);
23919 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23920 LLVMWithBeforeLambdaBody);
23921 verifyFormat(
23922 "FctWithLongLineInLambda_SLS_All(\n"
23923 " []()\n"
23924 " {\n"
23925 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23926 " AndShouldNotBeConsiderAsInline,\n"
23927 " LambdaBodyMustBeBreak);\n"
23928 " });",
23929 LLVMWithBeforeLambdaBody);
23930 verifyFormat(
23931 "auto fct_SLS_All = []()\n"
23932 "{\n"
23933 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23934 " AndShouldNotBeConsiderAsInline,\n"
23935 " LambdaBodyMustBeBreak);\n"
23936 "};",
23937 LLVMWithBeforeLambdaBody);
23938 LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine;
23939 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23940 LLVMWithBeforeLambdaBody);
23941 verifyFormat(
23942 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23943 " FirstParam,\n"
23944 " SecondParam,\n"
23945 " ThirdParam,\n"
23946 " FourthParam);",
23947 LLVMWithBeforeLambdaBody);
23948 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23949 " []() { return "
23950 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23951 " FirstParam,\n"
23952 " SecondParam,\n"
23953 " ThirdParam,\n"
23954 " FourthParam);",
23955 LLVMWithBeforeLambdaBody);
23956 verifyFormat(
23957 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23958 " SecondParam,\n"
23959 " ThirdParam,\n"
23960 " FourthParam,\n"
23961 " []() { return SomeValueNotSoLong; });",
23962 LLVMWithBeforeLambdaBody);
23963 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23964 " []()\n"
23965 " {\n"
23966 " return "
23967 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23968 "eConsiderAsInline;\n"
23969 " });",
23970 LLVMWithBeforeLambdaBody);
23971 verifyFormat(
23972 "FctWithLongLineInLambda_SLS_All(\n"
23973 " []()\n"
23974 " {\n"
23975 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23976 " AndShouldNotBeConsiderAsInline,\n"
23977 " LambdaBodyMustBeBreak);\n"
23978 " });",
23979 LLVMWithBeforeLambdaBody);
23980 verifyFormat("FctWithTwoParams_SLS_All(\n"
23981 " []()\n"
23982 " {\n"
23983 " // A cool function...\n"
23984 " return 43;\n"
23985 " },\n"
23986 " 87);",
23987 LLVMWithBeforeLambdaBody);
23988 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23989 LLVMWithBeforeLambdaBody);
23990 verifyFormat(
23991 "FctWithTwoParams_SLS_All(\n"
23992 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23993 LLVMWithBeforeLambdaBody);
23994 verifyFormat(
23995 "FctWithTwoParams_SLS_All(\n"
23996 " 87,\n"
23997 " []()\n"
23998 " {\n"
23999 " return "
24000 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
24001 " });",
24002 LLVMWithBeforeLambdaBody);
24003 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
24004 LLVMWithBeforeLambdaBody);
24005 verifyFormat(
24006 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
24007 LLVMWithBeforeLambdaBody);
24008 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
24009 "}); }, x);",
24010 LLVMWithBeforeLambdaBody);
24011 verifyFormat("TwoNestedLambdas_SLS_All(\n"
24012 " []()\n"
24013 " {\n"
24014 " // A cool function...\n"
24015 " return Call([]() { return 17; });\n"
24016 " });",
24017 LLVMWithBeforeLambdaBody);
24018 verifyFormat("TwoNestedLambdas_SLS_All(\n"
24019 " []()\n"
24020 " {\n"
24021 " return Call(\n"
24022 " []()\n"
24023 " {\n"
24024 " // A cool function...\n"
24025 " return 17;\n"
24026 " });\n"
24027 " });",
24028 LLVMWithBeforeLambdaBody);
24029
24030 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
24031 FormatStyle::SLS_None;
24032
24033 verifyFormat("auto select = [this]() -> const Library::Object *\n"
24034 "{\n"
24035 " return MyAssignment::SelectFromList(this);\n"
24036 "};",
24037 LLVMWithBeforeLambdaBody);
24038
24039 verifyFormat("auto select = [this]() -> const Library::Object &\n"
24040 "{\n"
24041 " return MyAssignment::SelectFromList(this);\n"
24042 "};",
24043 LLVMWithBeforeLambdaBody);
24044
24045 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
24046 "{\n"
24047 " return MyAssignment::SelectFromList(this);\n"
24048 "};",
24049 LLVMWithBeforeLambdaBody);
24050
24051 verifyFormat("namespace test {\n"
24052 "class Test {\n"
24053 "public:\n"
24054 " Test() = default;\n"
24055 "};\n"
24056 "} // namespace test",
24057 LLVMWithBeforeLambdaBody);
24058
24059 // Lambdas with different indentation styles.
24060 Style = getLLVMStyleWithColumns(60);
24061 verifyFormat("Result doSomething(Promise promise) {\n"
24062 " return promise.then(\n"
24063 " [this, obj = std::move(s)](int bar) mutable {\n"
24064 " return someObject.startAsyncAction().then(\n"
24065 " [this, &obj](Result result) mutable {\n"
24066 " result.processMore();\n"
24067 " });\n"
24068 " });\n"
24069 "}",
24070 Style);
24071 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24072 verifyFormat("Result doSomething(Promise promise) {\n"
24073 " return promise.then(\n"
24074 " [this, obj = std::move(s)](int bar) mutable {\n"
24075 " return obj.startAsyncAction().then(\n"
24076 " [this, &obj](Result result) mutable {\n"
24077 " result.processMore();\n"
24078 " });\n"
24079 " });\n"
24080 "}",
24081 Style);
24082 verifyFormat("Result doSomething(Promise promise) {\n"
24083 " return promise.then([this, obj = std::move(s)] {\n"
24084 " return obj.startAsyncAction().then(\n"
24085 " [this, &obj](Result result) mutable {\n"
24086 " result.processMore();\n"
24087 " });\n"
24088 " });\n"
24089 "}",
24090 Style);
24091 verifyFormat("void test() {\n"
24092 " ([]() -> auto {\n"
24093 " int b = 32;\n"
24094 " return 3;\n"
24095 " }).foo();\n"
24096 "}",
24097 Style);
24098 verifyFormat("void test() {\n"
24099 " []() -> auto {\n"
24100 " int b = 32;\n"
24101 " return 3;\n"
24102 " }\n"
24103 "}",
24104 Style);
24105 verifyFormat("void test() {\n"
24106 " std::sort(v.begin(), v.end(),\n"
24107 " [](const auto &foo, const auto &bar) {\n"
24108 " return foo.baz < bar.baz;\n"
24109 " });\n"
24110 "};",
24111 Style);
24112 verifyFormat("void test() {\n"
24113 " (\n"
24114 " []() -> auto {\n"
24115 " int b = 32;\n"
24116 " return 3;\n"
24117 " }, foo, bar)\n"
24118 " .foo();\n"
24119 "}",
24120 Style);
24121 verifyFormat("void test() {\n"
24122 " ([]() -> auto {\n"
24123 " int b = 32;\n"
24124 " return 3;\n"
24125 " })\n"
24126 " .foo()\n"
24127 " .bar();\n"
24128 "}",
24129 Style);
24130 verifyFormat("#define A \\\n"
24131 " [] { \\\n"
24132 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
24133 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
24134 " }",
24135 Style);
24136 verifyFormat("#define SORT(v) \\\n"
24137 " std::sort(v.begin(), v.end(), \\\n"
24138 " [](const auto &foo, const auto &bar) { \\\n"
24139 " return foo.baz < bar.baz; \\\n"
24140 " });",
24141 Style);
24142 verifyFormat("void foo() {\n"
24143 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
24144 " auto f = e(d);\n"
24145 " return f;\n"
24146 " })));\n"
24147 "}",
24148 Style);
24149 verifyFormat("void foo() {\n"
24150 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
24151 " auto f = e(foo, [&] {\n"
24152 " auto g = h();\n"
24153 " return g;\n"
24154 " }, qux, [&] -> Bar {\n"
24155 " auto i = j();\n"
24156 " return i;\n"
24157 " });\n"
24158 " return f;\n"
24159 " })));\n"
24160 "}",
24161 Style);
24162 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
24163 " AnotherLongClassName baz)\n"
24164 " : baz{baz}, func{[&] {\n"
24165 " auto qux = bar;\n"
24166 " return aFunkyFunctionCall(qux);\n"
24167 " }} {}",
24168 Style);
24169 verifyFormat("void foo() {\n"
24170 " class Foo {\n"
24171 " public:\n"
24172 " Foo()\n"
24173 " : qux{[](int quux) {\n"
24174 " auto tmp = quux;\n"
24175 " return tmp;\n"
24176 " }} {}\n"
24177 "\n"
24178 " private:\n"
24179 " std::function<void(int quux)> qux;\n"
24180 " };\n"
24181 "}",
24182 Style);
24183 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
24184 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
24185 " AnotherLongClassName baz) :\n"
24186 " baz{baz}, func{[&] {\n"
24187 " auto qux = bar;\n"
24188 " return aFunkyFunctionCall(qux);\n"
24189 " }} {}",
24190 Style);
24191 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
24192 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
24193 " AnotherLongClassName baz) :\n"
24194 " baz{baz},\n"
24195 " func{[&] {\n"
24196 " auto qux = bar;\n"
24197 " return aFunkyFunctionCall(qux);\n"
24198 " }} {}",
24199 Style);
24200 Style.BreakAfterOpenBracketFunction = true;
24201 // FIXME: The following test should pass, but fails at the time of writing.
24202 #if 0
24203 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
24204 // doesn't force an initial line break, even if lambdas span multiple lines.
24205 verifyFormat("void foo() {\n"
24206 " aFunction(\n"
24207 " [](d) -> Foo {\n"
24208 " auto f = e(d);\n"
24209 " return f;\n"
24210 " }, foo, Bar{}, [] {\n"
24211 " auto g = h();\n"
24212 " return g;\n"
24213 " }, baz);\n"
24214 "}",
24215 Style);
24216 #endif
24217 // A long non-lambda argument forces arguments to span multiple lines and thus
24218 // forces an initial line break when using AlwaysBreak.
24219 verifyFormat("void foo() {\n"
24220 " aFunction(\n"
24221 " 1,\n"
24222 " [](d) -> Foo {\n"
24223 " auto f = e(d);\n"
24224 " return f;\n"
24225 " }, foo, Bar{},\n"
24226 " [] {\n"
24227 " auto g = h();\n"
24228 " return g;\n"
24229 " }, bazzzzz,\n"
24230 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
24231 "}",
24232 Style);
24233 Style.BinPackArguments = false;
24234 verifyFormat("void foo() {\n"
24235 " aFunction(\n"
24236 " 1,\n"
24237 " [](d) -> Foo {\n"
24238 " auto f = e(d);\n"
24239 " return f;\n"
24240 " },\n"
24241 " foo,\n"
24242 " Bar{},\n"
24243 " [] {\n"
24244 " auto g = h();\n"
24245 " return g;\n"
24246 " },\n"
24247 " bazzzzz,\n"
24248 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
24249 "}",
24250 Style);
24251 Style.BinPackArguments = true;
24252 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
24253 Style.BraceWrapping.BeforeLambdaBody = true;
24254 verifyFormat("void foo() {\n"
24255 " aFunction(\n"
24256 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
24257 " {\n"
24258 " auto f = e(\n"
24259 " [&]\n"
24260 " {\n"
24261 " auto g = h();\n"
24262 " return g;\n"
24263 " }, qux, [&] -> Bar\n"
24264 " {\n"
24265 " auto i = j();\n"
24266 " return i;\n"
24267 " });\n"
24268 " return f;\n"
24269 " })));\n"
24270 "}",
24271 Style);
24272 }
24273
24274 TEST_F(FormatTest, LambdaWithLineComments) {
24275 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
24276 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
24277 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
24278 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
24279 FormatStyle::SLS_All;
24280
24281 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
24282 verifyFormat("auto k = []() // comment\n"
24283 "{ return; }",
24284 LLVMWithBeforeLambdaBody);
24285 verifyFormat("auto k = []() /* comment */ { return; }",
24286 LLVMWithBeforeLambdaBody);
24287 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
24288 LLVMWithBeforeLambdaBody);
24289 verifyFormat("auto k = []() // X\n"
24290 "{ return; }",
24291 LLVMWithBeforeLambdaBody);
24292 verifyFormat(
24293 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
24294 "{ return; }",
24295 LLVMWithBeforeLambdaBody);
24296
24297 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
24298
24299 verifyFormat("foo([]()\n"
24300 " {\n"
24301 " bar(); //\n"
24302 " return 1; // comment\n"
24303 " }());",
24304 "foo([]() {\n"
24305 " bar(); //\n"
24306 " return 1; // comment\n"
24307 "}());",
24308 LLVMWithBeforeLambdaBody);
24309 verifyFormat("foo(\n"
24310 " 1, MACRO {\n"
24311 " baz();\n"
24312 " bar(); // comment\n"
24313 " },\n"
24314 " []() {});",
24315 "foo(\n"
24316 " 1, MACRO { baz(); bar(); // comment\n"
24317 " }, []() {}\n"
24318 ");",
24319 LLVMWithBeforeLambdaBody);
24320 }
24321
24322 TEST_F(FormatTest, EmptyLinesInLambdas) {
24323 verifyFormat("auto lambda = []() {\n"
24324 " x(); //\n"
24325 "};",
24326 "auto lambda = []() {\n"
24327 "\n"
24328 " x(); //\n"
24329 "\n"
24330 "};");
24331 }
24332
24333 TEST_F(FormatTest, LambdaBracesInGNU) {
24334 auto Style = getGNUStyle();
24335 EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);
24336
24337 constexpr StringRef Code("auto x = [&] ()\n"
24338 " {\n"
24339 " for (int i = 0; i < y; ++i)\n"
24340 " return 97;\n"
24341 " };");
24342 verifyFormat(Code, Style);
24343
24344 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24345 verifyFormat(Code, Style);
24346 verifyFormat("for_each_thread ([] (thread_info *thread)\n"
24347 " {\n"
24348 " /* Lambda body. */\n"
24349 " });",
24350 "for_each_thread([](thread_info *thread) {\n"
24351 " /* Lambda body. */\n"
24352 "});",
24353 Style);
24354 verifyFormat("iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info)\n"
24355 " {\n"
24356 " /* Lambda body. */\n"
24357 " });",
24358 "iterate_over_lwps(scope_ptid, [=](struct lwp_info *info) {\n"
24359 " /* Lambda body. */\n"
24360 "});",
24361 Style);
24362 }
24363
24364 TEST_F(FormatTest, FormatsBlocks) {
24365 FormatStyle ShortBlocks = getLLVMStyle();
24366 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
24367 verifyFormat("int (^Block)(int, int);", ShortBlocks);
24368 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
24369 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
24370 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
24371 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
24372 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
24373
24374 verifyFormat("foo(^{ bar(); });", ShortBlocks);
24375 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
24376 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
24377
24378 verifyFormat("[operation setCompletionBlock:^{\n"
24379 " [self onOperationDone];\n"
24380 "}];");
24381 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
24382 " [self onOperationDone];\n"
24383 "}]};");
24384 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
24385 " f();\n"
24386 "}];");
24387 verifyFormat("int a = [operation block:^int(int *i) {\n"
24388 " return 1;\n"
24389 "}];");
24390 verifyFormat("[myObject doSomethingWith:arg1\n"
24391 " aaa:^int(int *a) {\n"
24392 " return 1;\n"
24393 " }\n"
24394 " bbb:f(a * bbbbbbbb)];");
24395
24396 verifyFormat("[operation setCompletionBlock:^{\n"
24397 " [self.delegate newDataAvailable];\n"
24398 "}];",
24399 getLLVMStyleWithColumns(60));
24400 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
24401 " NSString *path = [self sessionFilePath];\n"
24402 " if (path) {\n"
24403 " // ...\n"
24404 " }\n"
24405 "});");
24406 verifyFormat("[[SessionService sharedService]\n"
24407 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24408 " if (window) {\n"
24409 " [self windowDidLoad:window];\n"
24410 " } else {\n"
24411 " [self errorLoadingWindow];\n"
24412 " }\n"
24413 " }];");
24414 verifyFormat("void (^largeBlock)(void) = ^{\n"
24415 " // ...\n"
24416 "};",
24417 getLLVMStyleWithColumns(40));
24418 verifyFormat("[[SessionService sharedService]\n"
24419 " loadWindowWithCompletionBlock: //\n"
24420 " ^(SessionWindow *window) {\n"
24421 " if (window) {\n"
24422 " [self windowDidLoad:window];\n"
24423 " } else {\n"
24424 " [self errorLoadingWindow];\n"
24425 " }\n"
24426 " }];",
24427 getLLVMStyleWithColumns(60));
24428 verifyFormat("[myObject doSomethingWith:arg1\n"
24429 " firstBlock:^(Foo *a) {\n"
24430 " // ...\n"
24431 " int i;\n"
24432 " }\n"
24433 " secondBlock:^(Bar *b) {\n"
24434 " // ...\n"
24435 " int i;\n"
24436 " }\n"
24437 " thirdBlock:^Foo(Bar *b) {\n"
24438 " // ...\n"
24439 " int i;\n"
24440 " }];");
24441 verifyFormat("[myObject doSomethingWith:arg1\n"
24442 " firstBlock:-1\n"
24443 " secondBlock:^(Bar *b) {\n"
24444 " // ...\n"
24445 " int i;\n"
24446 " }];");
24447
24448 verifyFormat("f(^{\n"
24449 " @autoreleasepool {\n"
24450 " if (a) {\n"
24451 " g();\n"
24452 " }\n"
24453 " }\n"
24454 "});");
24455 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
24456 "};");
24457 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
24458 "};");
24459
24460 FormatStyle FourIndent = getLLVMStyle();
24461 FourIndent.ObjCBlockIndentWidth = 4;
24462 verifyFormat("[operation setCompletionBlock:^{\n"
24463 " [self onOperationDone];\n"
24464 "}];",
24465 FourIndent);
24466 }
24467
24468 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
24469 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
24470
24471 verifyFormat("[[SessionService sharedService] "
24472 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24473 " if (window) {\n"
24474 " [self windowDidLoad:window];\n"
24475 " } else {\n"
24476 " [self errorLoadingWindow];\n"
24477 " }\n"
24478 "}];",
24479 ZeroColumn);
24480 verifyFormat("[[SessionService sharedService]\n"
24481 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24482 " if (window) {\n"
24483 " [self windowDidLoad:window];\n"
24484 " } else {\n"
24485 " [self errorLoadingWindow];\n"
24486 " }\n"
24487 " }];",
24488 "[[SessionService sharedService]\n"
24489 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24490 " if (window) {\n"
24491 " [self windowDidLoad:window];\n"
24492 " } else {\n"
24493 " [self errorLoadingWindow];\n"
24494 " }\n"
24495 "}];",
24496 ZeroColumn);
24497 verifyFormat("[myObject doSomethingWith:arg1\n"
24498 " firstBlock:^(Foo *a) {\n"
24499 " // ...\n"
24500 " int i;\n"
24501 " }\n"
24502 " secondBlock:^(Bar *b) {\n"
24503 " // ...\n"
24504 " int i;\n"
24505 " }\n"
24506 " thirdBlock:^Foo(Bar *b) {\n"
24507 " // ...\n"
24508 " int i;\n"
24509 " }];",
24510 ZeroColumn);
24511 verifyFormat("f(^{\n"
24512 " @autoreleasepool {\n"
24513 " if (a) {\n"
24514 " g();\n"
24515 " }\n"
24516 " }\n"
24517 "});",
24518 ZeroColumn);
24519 verifyFormat("void (^largeBlock)(void) = ^{\n"
24520 " // ...\n"
24521 "};",
24522 ZeroColumn);
24523
24524 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
24525 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
24526 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
24527 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
24528 verifyFormat("void (^largeBlock)(void) = ^{\n"
24529 " int i;\n"
24530 "};",
24531 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
24532 }
24533
24534 TEST_F(FormatTest, SupportsCRLF) {
24535 verifyFormat("int a;\r\n"
24536 "int b;\r\n"
24537 "int c;",
24538 "int a;\r\n"
24539 " int b;\r\n"
24540 " int c;");
24541 verifyFormat("int a;\r\n"
24542 "int b;\r\n"
24543 "int c;\r\n",
24544 "int a;\r\n"
24545 " int b;\n"
24546 " int c;\r\n");
24547 verifyFormat("int a;\n"
24548 "int b;\n"
24549 "int c;",
24550 "int a;\r\n"
24551 " int b;\n"
24552 " int c;");
24553 // FIXME: unstable test case
24554 EXPECT_EQ("\"aaaaaaa \"\r\n"
24555 "\"bbbbbbb\";\r\n",
24556 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
24557 verifyFormat("#define A \\\r\n"
24558 " b; \\\r\n"
24559 " c; \\\r\n"
24560 " d;",
24561 "#define A \\\r\n"
24562 " b; \\\r\n"
24563 " c; d; ",
24564 getGoogleStyle());
24565
24566 verifyNoChange("/*\r\n"
24567 "multi line block comments\r\n"
24568 "should not introduce\r\n"
24569 "an extra carriage return\r\n"
24570 "*/");
24571 verifyFormat("/*\r\n"
24572 "\r\n"
24573 "*/",
24574 "/*\r\n"
24575 " \r\r\r\n"
24576 "*/");
24577
24578 FormatStyle style = getLLVMStyle();
24579
24580 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
24581 verifyFormat("union FooBarBazQux {\n"
24582 " int foo;\n"
24583 " int bar;\n"
24584 " int baz;\n"
24585 "};",
24586 "union FooBarBazQux {\r\n"
24587 " int foo;\n"
24588 " int bar;\r\n"
24589 " int baz;\n"
24590 "};",
24591 style);
24592 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24593 verifyFormat("union FooBarBazQux {\r\n"
24594 " int foo;\r\n"
24595 " int bar;\r\n"
24596 " int baz;\r\n"
24597 "};",
24598 "union FooBarBazQux {\r\n"
24599 " int foo;\n"
24600 " int bar;\r\n"
24601 " int baz;\n"
24602 "};",
24603 style);
24604
24605 style.LineEnding = FormatStyle::LE_LF;
24606 verifyFormat("union FooBarBazQux {\n"
24607 " int foo;\n"
24608 " int bar;\n"
24609 " int baz;\n"
24610 " int qux;\n"
24611 "};",
24612 "union FooBarBazQux {\r\n"
24613 " int foo;\n"
24614 " int bar;\r\n"
24615 " int baz;\n"
24616 " int qux;\r\n"
24617 "};",
24618 style);
24619 style.LineEnding = FormatStyle::LE_CRLF;
24620 verifyFormat("union FooBarBazQux {\r\n"
24621 " int foo;\r\n"
24622 " int bar;\r\n"
24623 " int baz;\r\n"
24624 " int qux;\r\n"
24625 "};",
24626 "union FooBarBazQux {\r\n"
24627 " int foo;\n"
24628 " int bar;\r\n"
24629 " int baz;\n"
24630 " int qux;\n"
24631 "};",
24632 style);
24633
24634 style.LineEnding = FormatStyle::LE_DeriveLF;
24635 verifyFormat("union FooBarBazQux {\r\n"
24636 " int foo;\r\n"
24637 " int bar;\r\n"
24638 " int baz;\r\n"
24639 " int qux;\r\n"
24640 "};",
24641 "union FooBarBazQux {\r\n"
24642 " int foo;\n"
24643 " int bar;\r\n"
24644 " int baz;\n"
24645 " int qux;\r\n"
24646 "};",
24647 style);
24648 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24649 verifyFormat("union FooBarBazQux {\n"
24650 " int foo;\n"
24651 " int bar;\n"
24652 " int baz;\n"
24653 " int qux;\n"
24654 "};",
24655 "union FooBarBazQux {\r\n"
24656 " int foo;\n"
24657 " int bar;\r\n"
24658 " int baz;\n"
24659 " int qux;\n"
24660 "};",
24661 style);
24662 }
24663
24664 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
24665 verifyFormat("MY_CLASS(C) {\n"
24666 " int i;\n"
24667 " int j;\n"
24668 "};");
24669 }
24670
24671 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
24672 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
24673 TwoIndent.ContinuationIndentWidth = 2;
24674
24675 verifyFormat("int i =\n"
24676 " longFunction(\n"
24677 " arg);",
24678 "int i = longFunction(arg);", TwoIndent);
24679
24680 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
24681 SixIndent.ContinuationIndentWidth = 6;
24682
24683 verifyFormat("int i =\n"
24684 " longFunction(\n"
24685 " arg);",
24686 "int i = longFunction(arg);", SixIndent);
24687 }
24688
24689 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
24690 FormatStyle Style = getLLVMStyle();
24691 verifyFormat("int Foo::getter(\n"
24692 " //\n"
24693 ") const {\n"
24694 " return foo;\n"
24695 "}",
24696 Style);
24697 verifyFormat("void Foo::setter(\n"
24698 " //\n"
24699 ") {\n"
24700 " foo = 1;\n"
24701 "}",
24702 Style);
24703 }
24704
24705 TEST_F(FormatTest, SpacesInAngles) {
24706 FormatStyle Spaces = getLLVMStyle();
24707 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24708
24709 verifyFormat("vector< ::std::string > x1;", Spaces);
24710 verifyFormat("Foo< int, Bar > x2;", Spaces);
24711 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
24712
24713 verifyFormat("static_cast< int >(arg);", Spaces);
24714 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
24715 verifyFormat("f< int, float >();", Spaces);
24716 verifyFormat("template <> g() {}", Spaces);
24717 verifyFormat("template < std::vector< int > > f() {}", Spaces);
24718 verifyFormat("std::function< void(int, int) > fct;", Spaces);
24719 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24720 Spaces);
24721
24722 Spaces.Standard = FormatStyle::LS_Cpp03;
24723 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24724 verifyFormat("A< A< int > >();", Spaces);
24725
24726 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24727 verifyFormat("A<A<int> >();", Spaces);
24728
24729 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24730 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24731 Spaces);
24732 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24733 Spaces);
24734
24735 verifyFormat("A<A<int> >();", Spaces);
24736 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
24737 verifyFormat("A< A< int > >();", Spaces);
24738
24739 Spaces.Standard = FormatStyle::LS_Cpp11;
24740 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24741 verifyFormat("A< A< int > >();", Spaces);
24742
24743 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24744 verifyFormat("vector<::std::string> x4;", Spaces);
24745 verifyFormat("vector<int> x5;", Spaces);
24746 verifyFormat("Foo<int, Bar> x6;", Spaces);
24747 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24748
24749 verifyFormat("A<A<int>>();", Spaces);
24750
24751 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24752 verifyFormat("vector<::std::string> x4;", Spaces);
24753 verifyFormat("vector< ::std::string > x4;", Spaces);
24754 verifyFormat("vector<int> x5;", Spaces);
24755 verifyFormat("vector< int > x5;", Spaces);
24756 verifyFormat("Foo<int, Bar> x6;", Spaces);
24757 verifyFormat("Foo< int, Bar > x6;", Spaces);
24758 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24759 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
24760
24761 verifyFormat("A<A<int>>();", Spaces);
24762 verifyFormat("A< A< int > >();", Spaces);
24763 verifyFormat("A<A<int > >();", Spaces);
24764 verifyFormat("A< A< int>>();", Spaces);
24765
24766 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24767 verifyFormat("// clang-format off\n"
24768 "foo<<<1, 1>>>();\n"
24769 "// clang-format on",
24770 Spaces);
24771 verifyFormat("// clang-format off\n"
24772 "foo< < <1, 1> > >();\n"
24773 "// clang-format on",
24774 Spaces);
24775 }
24776
24777 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
24778 FormatStyle Style = getLLVMStyle();
24779 Style.SpaceAfterTemplateKeyword = false;
24780 verifyFormat("template<int> void foo();", Style);
24781 }
24782
24783 TEST_F(FormatTest, TripleAngleBrackets) {
24784 verifyFormat("f<<<1, 1>>>();");
24785 verifyFormat("f<<<1, 1, 1, s>>>();");
24786 verifyFormat("f<<<a, b, c, d>>>();");
24787 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24788 verifyFormat("f<param><<<1, 1>>>();");
24789 verifyFormat("f<1><<<1, 1>>>();");
24790 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24792 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24793 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24794 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24795 }
24796
24797 TEST_F(FormatTest, MergeLessLessAtEnd) {
24798 verifyFormat("<<");
24799 verifyFormat("< < <", "\\\n<<<");
24800 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24801 "aaallvm::outs() <<");
24802 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24803 "aaaallvm::outs()\n <<");
24804 }
24805
24806 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
24807 std::string code = "#if A\n"
24808 "#if B\n"
24809 "a.\n"
24810 "#endif\n"
24811 " a = 1;\n"
24812 "#else\n"
24813 "#endif\n"
24814 "#if C\n"
24815 "#else\n"
24816 "#endif\n";
24817 verifyFormat(code);
24818 }
24819
24820 TEST_F(FormatTest, HandleConflictMarkers) {
24821 // Git/SVN conflict markers.
24822 verifyFormat("int a;\n"
24823 "void f() {\n"
24824 " callme(some(parameter1,\n"
24825 "<<<<<<< text by the vcs\n"
24826 " parameter2),\n"
24827 "||||||| text by the vcs\n"
24828 " parameter2),\n"
24829 " parameter3,\n"
24830 "======= text by the vcs\n"
24831 " parameter2, parameter3),\n"
24832 ">>>>>>> text by the vcs\n"
24833 " otherparameter);",
24834 "int a;\n"
24835 "void f() {\n"
24836 " callme(some(parameter1,\n"
24837 "<<<<<<< text by the vcs\n"
24838 " parameter2),\n"
24839 "||||||| text by the vcs\n"
24840 " parameter2),\n"
24841 " parameter3,\n"
24842 "======= text by the vcs\n"
24843 " parameter2,\n"
24844 " parameter3),\n"
24845 ">>>>>>> text by the vcs\n"
24846 " otherparameter);");
24847
24848 // Perforce markers.
24849 verifyFormat("void f() {\n"
24850 " function(\n"
24851 ">>>> text by the vcs\n"
24852 " parameter,\n"
24853 "==== text by the vcs\n"
24854 " parameter,\n"
24855 "==== text by the vcs\n"
24856 " parameter,\n"
24857 "<<<< text by the vcs\n"
24858 " parameter);",
24859 "void f() {\n"
24860 " function(\n"
24861 ">>>> text by the vcs\n"
24862 " parameter,\n"
24863 "==== text by the vcs\n"
24864 " parameter,\n"
24865 "==== text by the vcs\n"
24866 " parameter,\n"
24867 "<<<< text by the vcs\n"
24868 " parameter);");
24869
24870 verifyNoChange("<<<<<<<\n"
24871 "|||||||\n"
24872 "=======\n"
24873 ">>>>>>>");
24874
24875 verifyNoChange("<<<<<<<\n"
24876 "|||||||\n"
24877 "int i;\n"
24878 "=======\n"
24879 ">>>>>>>");
24880
24881 // FIXME: Handle parsing of macros around conflict markers correctly:
24882 verifyFormat("#define Macro \\\n"
24883 "<<<<<<<\n"
24884 "Something \\\n"
24885 "|||||||\n"
24886 "Else \\\n"
24887 "=======\n"
24888 "Other \\\n"
24889 ">>>>>>>\n"
24890 " End int i;",
24891 "#define Macro \\\n"
24892 "<<<<<<<\n"
24893 " Something \\\n"
24894 "|||||||\n"
24895 " Else \\\n"
24896 "=======\n"
24897 " Other \\\n"
24898 ">>>>>>>\n"
24899 " End\n"
24900 "int i;");
24901
24902 verifyFormat(R"(====
24903 #ifdef A
24904 a
24905 #else
24906 b
24907 #endif
24908 )");
24909 }
24910
24911 TEST_F(FormatTest, DisableRegions) {
24912 verifyFormat("int i;\n"
24913 "// clang-format off\n"
24914 " int j;\n"
24915 "// clang-format on\n"
24916 "int k;",
24917 " int i;\n"
24918 " // clang-format off\n"
24919 " int j;\n"
24920 " // clang-format on\n"
24921 " int k;");
24922 verifyFormat("int i;\n"
24923 "/* clang-format off */\n"
24924 " int j;\n"
24925 "/* clang-format on */\n"
24926 "int k;",
24927 " int i;\n"
24928 " /* clang-format off */\n"
24929 " int j;\n"
24930 " /* clang-format on */\n"
24931 " int k;");
24932
24933 // Don't reflow comments within disabled regions.
24934 verifyFormat("// clang-format off\n"
24935 "// long long long long long long line\n"
24936 "/* clang-format on */\n"
24937 "/* long long long\n"
24938 " * long long long\n"
24939 " * line */\n"
24940 "int i;\n"
24941 "/* clang-format off */\n"
24942 "/* long long long long long long line */",
24943 "// clang-format off\n"
24944 "// long long long long long long line\n"
24945 "/* clang-format on */\n"
24946 "/* long long long long long long line */\n"
24947 "int i;\n"
24948 "/* clang-format off */\n"
24949 "/* long long long long long long line */",
24950 getLLVMStyleWithColumns(20));
24951
24952 verifyFormat("int *i;\n"
24953 "// clang-format off:\n"
24954 "int* j;\n"
24955 "// clang-format on: 1\n"
24956 "int *k;",
24957 "int* i;\n"
24958 "// clang-format off:\n"
24959 "int* j;\n"
24960 "// clang-format on: 1\n"
24961 "int* k;");
24962
24963 verifyFormat("int *i;\n"
24964 "// clang-format off:0\n"
24965 "int* j;\n"
24966 "// clang-format only\n"
24967 "int* k;",
24968 "int* i;\n"
24969 "// clang-format off:0\n"
24970 "int* j;\n"
24971 "// clang-format only\n"
24972 "int* k;");
24973
24974 verifyNoChange("// clang-format off\n"
24975 "#if 0\n"
24976 " #if SHOULD_STAY_INDENTED\n"
24977 " #endif\n"
24978 "#endif\n"
24979 "// clang-format on");
24980 }
24981
24982 TEST_F(FormatTest, OneLineFormatOffRegex) {
24983 auto Style = getLLVMStyle();
24984 Style.OneLineFormatOffRegex = "// format off$";
24985
24986 verifyFormat(" // format off\n"
24987 " int i ;\n"
24988 "int j;",
24989 " // format off\n"
24990 " int i ;\n"
24991 " int j ;",
24992 Style);
24993 verifyFormat("// format off?\n"
24994 "int i;",
24995 " // format off?\n"
24996 " int i ;",
24997 Style);
24998 verifyFormat("f(\"// format off\");", " f(\"// format off\") ;", Style);
24999
25000 verifyFormat("int i;\n"
25001 " // format off\n"
25002 " int j ;\n"
25003 "int k;",
25004 " int i ;\n"
25005 " // format off\n"
25006 " int j ;\n"
25007 " int k ;",
25008 Style);
25009
25010 verifyFormat(" // format off\n"
25011 "\n"
25012 "int i;",
25013 " // format off\n"
25014 " \n"
25015 " int i ;",
25016 Style);
25017
25018 verifyFormat("int i;\n"
25019 " int j ; // format off\n"
25020 "int k;",
25021 " int i ;\n"
25022 " int j ; // format off\n"
25023 " int k ;",
25024 Style);
25025
25026 verifyFormat("// clang-format off\n"
25027 " int i ;\n"
25028 " int j ; // format off\n"
25029 " int k ;\n"
25030 "// clang-format on\n"
25031 "f();",
25032 " // clang-format off\n"
25033 " int i ;\n"
25034 " int j ; // format off\n"
25035 " int k ;\n"
25036 " // clang-format on\n"
25037 " f() ;",
25038 Style);
25039
25040 Style.OneLineFormatOffRegex = "^/\\* format off \\*/";
25041 verifyFormat("int i;\n"
25042 " /* format off */ int j ;\n"
25043 "int k;",
25044 " int i ;\n"
25045 " /* format off */ int j ;\n"
25046 " int k ;",
25047 Style);
25048 verifyFormat("f(\"/* format off */\");", " f(\"/* format off */\") ;", Style);
25049
25050 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
25051 verifyFormat("#define A \\\n"
25052 " do { \\\n"
25053 " /* format off */\\\n"
25054 " f() ; \\\n"
25055 " g(); \\\n"
25056 " } while (0)",
25057 "# define A\\\n"
25058 " do{ \\\n"
25059 " /* format off */\\\n"
25060 " f() ; \\\n"
25061 " g() ;\\\n"
25062 " } while (0 )",
25063 Style);
25064
25065 Style.OneLineFormatOffRegex = "MACRO_TEST";
25066 verifyNoChange(" MACRO_TEST1 ( ) ;\n"
25067 " MACRO_TEST2( );",
25068 Style);
25069
25070 Style.ColumnLimit = 50;
25071 Style.OneLineFormatOffRegex = "^LogErrorPrint$";
25072 verifyFormat(" myproject::LogErrorPrint(logger, \"Don't split me!\");\n"
25073 "myproject::MyLogErrorPrinter(myLogger,\n"
25074 " \"Split me!\");",
25075 " myproject::LogErrorPrint(logger, \"Don't split me!\");\n"
25076 " myproject::MyLogErrorPrinter(myLogger, \"Split me!\");",
25077 Style);
25078
25079 Style.OneLineFormatOffRegex = "//(< clang-format off| NO_TRANSLATION)$";
25080 verifyNoChange(
25081 " int i ; //< clang-format off\n"
25082 " msg = sprintf(\"Long string with placeholders.\"); // NO_TRANSLATION",
25083 Style);
25084 }
25085
25086 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
25087 format("? ) =");
25088 verifyNoCrash("#define a\\\n /**/}");
25089 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n"
25090 "LY52: ! [internal]");
25091 }
25092
25093 TEST_F(FormatTest, FormatsTableGenCode) {
25094 FormatStyle Style = getLLVMStyle();
25095 Style.Language = FormatStyle::LK_TableGen;
25096 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
25097 }
25098
25099 TEST_F(FormatTest, ArrayOfTemplates) {
25100 verifyFormat("auto a = new unique_ptr<int>[10];",
25101 "auto a = new unique_ptr<int > [ 10];");
25102
25103 FormatStyle Spaces = getLLVMStyle();
25104 Spaces.SpacesInSquareBrackets = true;
25105 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
25106 "auto a = new unique_ptr<int > [10];", Spaces);
25107 }
25108
25109 TEST_F(FormatTest, ArrayAsTemplateType) {
25110 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
25111 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
25112
25113 FormatStyle Spaces = getLLVMStyle();
25114 Spaces.SpacesInSquareBrackets = true;
25115 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
25116 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
25117 }
25118
25119 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
25120
25121 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
25122 verifyFormat("using std::cin;\n"
25123 "using std::cout;",
25124 "using std::cout;\n"
25125 "using std::cin;",
25126 getGoogleStyle());
25127 }
25128
25129 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
25130 FormatStyle Style = getLLVMStyle();
25131 Style.Standard = FormatStyle::LS_Cpp03;
25132 // cpp03 recognize this string as identifier u8 and literal character 'a'
25133 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
25134 }
25135
25136 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
25137 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
25138 // all modes, including C++11, C++14 and C++17
25139 verifyFormat("auto c = u8'a';");
25140 }
25141
25142 TEST_F(FormatTest, DoNotFormatLikelyXml) {
25143 verifyGoogleFormat("<!-- ;> -->");
25144 verifyNoChange(" <!-- >; -->", getGoogleStyle());
25145 }
25146
25147 TEST_F(FormatTest, StructuredBindings) {
25148 // Structured bindings is a C++17 feature.
25149 // all modes, including C++11, C++14 and C++17
25150 verifyFormat("auto [a, b] = f();");
25151 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
25152 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
25153 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
25154 verifyFormat("auto const volatile [a, b] = f();",
25155 "auto const volatile[a, b] = f();");
25156 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
25157 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
25158 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
25159 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
25160 verifyFormat("auto const volatile &&[a, b] = f();",
25161 "auto const volatile &&[a, b] = f();");
25162 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
25163 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
25164 verifyFormat("const auto volatile &&[a, b] = f();",
25165 "const auto volatile &&[a, b] = f();");
25166 verifyFormat("volatile const auto &&[a, b] = f();",
25167 "volatile const auto &&[a, b] = f();");
25168 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
25169
25170 // Make sure we don't mistake structured bindings for lambdas.
25171 FormatStyle PointerMiddle = getLLVMStyle();
25172 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
25173 verifyGoogleFormat("auto [a1, b]{A * i};");
25174 verifyFormat("auto [a2, b]{A * i};");
25175 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
25176 verifyGoogleFormat("auto const [a1, b]{A * i};");
25177 verifyFormat("auto const [a2, b]{A * i};");
25178 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
25179 verifyGoogleFormat("auto const& [a1, b]{A * i};");
25180 verifyFormat("auto const &[a2, b]{A * i};");
25181 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
25182 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
25183 verifyFormat("auto const &&[a2, b]{A * i};");
25184 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
25185
25186 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
25187 "for (const auto && [a, b] : some_range) {\n}");
25188 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
25189 "for (const auto & [a, b] : some_range) {\n}");
25190 verifyFormat("for (const auto [a, b] : some_range) {\n}",
25191 "for (const auto[a, b] : some_range) {\n}");
25192 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
25193 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
25194 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
25195 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
25196 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
25197 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
25198 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
25199 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
25200
25201 FormatStyle Spaces = getLLVMStyle();
25202 Spaces.SpacesInSquareBrackets = true;
25203 verifyFormat("auto [ a, b ] = f();", Spaces);
25204 verifyFormat("auto &&[ a, b ] = f();", Spaces);
25205 verifyFormat("auto &[ a, b ] = f();", Spaces);
25206 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
25207 verifyFormat("auto const &[ a, b ] = f();", Spaces);
25208 }
25209
25210 TEST_F(FormatTest, FileAndCode) {
25211 EXPECT_EQ(FormatStyle::LK_C, guessLanguage("foo.c", ""));
25212 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
25213 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
25214 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
25215 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
25216 EXPECT_EQ(FormatStyle::LK_ObjC,
25217 guessLanguage("foo.h", "@interface Foo\n@end"));
25218 EXPECT_EQ(
25219 FormatStyle::LK_ObjC,
25220 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
25221 EXPECT_EQ(FormatStyle::LK_ObjC,
25222 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
25223 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
25224 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
25225 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
25226 EXPECT_EQ(FormatStyle::LK_ObjC,
25227 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
25228 EXPECT_EQ(FormatStyle::LK_ObjC,
25229 guessLanguage(
25230 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
25231 EXPECT_EQ(
25232 FormatStyle::LK_Cpp,
25233 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
25234 // Only one of the two preprocessor regions has ObjC-like code.
25235 EXPECT_EQ(FormatStyle::LK_ObjC,
25236 guessLanguage("foo.h", "#if A\n"
25237 "#define B() C\n"
25238 "#else\n"
25239 "#define B() [NSString a:@\"\"]\n"
25240 "#endif"));
25241 }
25242
25243 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
25244 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
25245 EXPECT_EQ(FormatStyle::LK_ObjC,
25246 guessLanguage("foo.h", "array[[calculator getIndex]];"));
25247 EXPECT_EQ(FormatStyle::LK_Cpp,
25248 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
25249 EXPECT_EQ(
25250 FormatStyle::LK_Cpp,
25251 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
25252 EXPECT_EQ(FormatStyle::LK_ObjC,
25253 guessLanguage("foo.h", "[[noreturn foo] bar];"));
25254 EXPECT_EQ(FormatStyle::LK_Cpp,
25255 guessLanguage("foo.h", "[[clang::fallthrough]];"));
25256 EXPECT_EQ(FormatStyle::LK_ObjC,
25257 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
25258 EXPECT_EQ(FormatStyle::LK_Cpp,
25259 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
25260 EXPECT_EQ(FormatStyle::LK_Cpp,
25261 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
25262 EXPECT_EQ(FormatStyle::LK_ObjC,
25263 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
25264 EXPECT_EQ(FormatStyle::LK_Cpp,
25265 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
25266 EXPECT_EQ(
25267 FormatStyle::LK_Cpp,
25268 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
25269 EXPECT_EQ(
25270 FormatStyle::LK_Cpp,
25271 guessLanguage("foo.h",
25272 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
25273 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
25274 }
25275
25276 TEST_F(FormatTest, GuessLanguageWithCaret) {
25277 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
25278 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
25279 EXPECT_EQ(FormatStyle::LK_ObjC,
25280 guessLanguage("foo.h", "int(^)(char, float);"));
25281 EXPECT_EQ(FormatStyle::LK_ObjC,
25282 guessLanguage("foo.h", "int(^foo)(char, float);"));
25283 EXPECT_EQ(FormatStyle::LK_ObjC,
25284 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
25285 EXPECT_EQ(FormatStyle::LK_ObjC,
25286 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
25287 EXPECT_EQ(
25288 FormatStyle::LK_ObjC,
25289 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
25290 }
25291
25292 TEST_F(FormatTest, GuessLanguageWithPragmas) {
25293 EXPECT_EQ(FormatStyle::LK_Cpp,
25294 guessLanguage("foo.h", "__pragma(warning(disable:))"));
25295 EXPECT_EQ(FormatStyle::LK_Cpp,
25296 guessLanguage("foo.h", "#pragma(warning(disable:))"));
25297 EXPECT_EQ(FormatStyle::LK_Cpp,
25298 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
25299 }
25300
25301 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
25302 // ASM symbolic names are identifiers that must be surrounded by [] without
25303 // space in between:
25304 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
25305
25306 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
25307 verifyFormat(R"(//
25308 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
25309 )");
25310
25311 // A list of several ASM symbolic names.
25312 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
25313
25314 // ASM symbolic names in inline ASM with inputs and outputs.
25315 verifyFormat(R"(//
25316 asm("cmoveq %1, %2, %[result]"
25317 : [result] "=r"(result)
25318 : "r"(test), "r"(new), "[result]"(old));
25319 )");
25320
25321 // ASM symbolic names in inline ASM with no outputs.
25322 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
25323 }
25324
25325 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
25326 EXPECT_EQ(FormatStyle::LK_Cpp,
25327 guessLanguage("foo.h", "void f() {\n"
25328 " asm (\"mov %[e], %[d]\"\n"
25329 " : [d] \"=rm\" (d)\n"
25330 " [e] \"rm\" (*e));\n"
25331 "}"));
25332 EXPECT_EQ(FormatStyle::LK_Cpp,
25333 guessLanguage("foo.h", "void f() {\n"
25334 " _asm (\"mov %[e], %[d]\"\n"
25335 " : [d] \"=rm\" (d)\n"
25336 " [e] \"rm\" (*e));\n"
25337 "}"));
25338 EXPECT_EQ(FormatStyle::LK_Cpp,
25339 guessLanguage("foo.h", "void f() {\n"
25340 " __asm (\"mov %[e], %[d]\"\n"
25341 " : [d] \"=rm\" (d)\n"
25342 " [e] \"rm\" (*e));\n"
25343 "}"));
25344 EXPECT_EQ(FormatStyle::LK_Cpp,
25345 guessLanguage("foo.h", "void f() {\n"
25346 " __asm__ (\"mov %[e], %[d]\"\n"
25347 " : [d] \"=rm\" (d)\n"
25348 " [e] \"rm\" (*e));\n"
25349 "}"));
25350 EXPECT_EQ(FormatStyle::LK_Cpp,
25351 guessLanguage("foo.h", "void f() {\n"
25352 " asm (\"mov %[e], %[d]\"\n"
25353 " : [d] \"=rm\" (d),\n"
25354 " [e] \"rm\" (*e));\n"
25355 "}"));
25356 EXPECT_EQ(FormatStyle::LK_Cpp,
25357 guessLanguage("foo.h", "void f() {\n"
25358 " asm volatile (\"mov %[e], %[d]\"\n"
25359 " : [d] \"=rm\" (d)\n"
25360 " [e] \"rm\" (*e));\n"
25361 "}"));
25362 }
25363
25364 TEST_F(FormatTest, GuessLanguageWithChildLines) {
25365 EXPECT_EQ(FormatStyle::LK_Cpp,
25366 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
25367 EXPECT_EQ(FormatStyle::LK_ObjC,
25368 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
25369 EXPECT_EQ(
25370 FormatStyle::LK_Cpp,
25371 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
25372 EXPECT_EQ(
25373 FormatStyle::LK_ObjC,
25374 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
25375 }
25376
25377 TEST_F(FormatTest, GetLanguageByComment) {
25378 EXPECT_EQ(FormatStyle::LK_C,
25379 guessLanguage("foo.h", "// clang-format Language: C\n"
25380 "int i;"));
25381 EXPECT_EQ(FormatStyle::LK_Cpp,
25382 guessLanguage("foo.h", "// clang-format Language: Cpp\n"
25383 "int DoStuff(CGRect rect);"));
25384 EXPECT_EQ(FormatStyle::LK_ObjC,
25385 guessLanguage("foo.h", "// clang-format Language: ObjC\n"
25386 "int i;"));
25387 }
25388
25389 TEST_F(FormatTest, TypenameMacros) {
25390 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
25391
25392 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
25393 FormatStyle Google = getGoogleStyleWithColumns(0);
25394 Google.TypenameMacros = TypenameMacros;
25395 verifyFormat("struct foo {\n"
25396 " int bar;\n"
25397 " TAILQ_ENTRY(a) bleh;\n"
25398 "};",
25399 Google);
25400
25401 FormatStyle Macros = getLLVMStyle();
25402 Macros.TypenameMacros = TypenameMacros;
25403
25404 verifyFormat("STACK_OF(int) a;", Macros);
25405 verifyFormat("STACK_OF(int) *a;", Macros);
25406 verifyFormat("STACK_OF(int const *) *a;", Macros);
25407 verifyFormat("STACK_OF(int *const) *a;", Macros);
25408 verifyFormat("STACK_OF(int, string) a;", Macros);
25409 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
25410 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
25411 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
25412 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
25413 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
25414 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
25415
25416 Macros.PointerAlignment = FormatStyle::PAS_Left;
25417 verifyFormat("STACK_OF(int)* a;", Macros);
25418 verifyFormat("STACK_OF(int*)* a;", Macros);
25419 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
25420 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
25421 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
25422 }
25423
25424 TEST_F(FormatTest, AtomicQualifier) {
25425 // Check that we treate _Atomic as a type and not a function call
25426 FormatStyle Google = getGoogleStyleWithColumns(0);
25427 verifyFormat("struct foo {\n"
25428 " int a1;\n"
25429 " _Atomic(a) a2;\n"
25430 " _Atomic(_Atomic(int)* const) a3;\n"
25431 "};",
25432 Google);
25433 verifyFormat("_Atomic(uint64_t) a;");
25434 verifyFormat("_Atomic(uint64_t) *a;");
25435 verifyFormat("_Atomic(uint64_t const *) *a;");
25436 verifyFormat("_Atomic(uint64_t *const) *a;");
25437 verifyFormat("_Atomic(const uint64_t *) *a;");
25438 verifyFormat("_Atomic(uint64_t) a;");
25439 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
25440 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
25441 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
25442 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
25443
25444 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
25445 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
25446 FormatStyle Style = getLLVMStyle();
25447 Style.PointerAlignment = FormatStyle::PAS_Left;
25448 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
25449 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
25450 verifyFormat("_Atomic(int)* a;", Style);
25451 verifyFormat("_Atomic(int*)* a;", Style);
25452 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
25453
25454 Style.SpacesInParens = FormatStyle::SIPO_Custom;
25455 Style.SpacesInParensOptions.InCStyleCasts = true;
25456 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
25457 Style.SpacesInParensOptions.InCStyleCasts = false;
25458 Style.SpacesInParensOptions.Other = true;
25459 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
25460 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
25461 }
25462
25463 TEST_F(FormatTest, C11Generic) {
25464 verifyFormat("_Generic(x, int: 1, default: 0)");
25465 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
25466 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
25467 verifyFormat("_Generic(x, int: f1, const int: f2)();");
25468 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
25469
25470 verifyFormat("_Generic(x,\n"
25471 " float: f,\n"
25472 " default: d,\n"
25473 " long double: ld,\n"
25474 " float _Complex: fc,\n"
25475 " double _Complex: dc,\n"
25476 " long double _Complex: ldc)");
25477
25478 verifyFormat("while (_Generic(x, //\n"
25479 " long: x)(x) > x) {\n"
25480 "}");
25481 verifyFormat("while (_Generic(x, //\n"
25482 " long: x)(x)) {\n"
25483 "}");
25484 verifyFormat("x(_Generic(x, //\n"
25485 " long: x)(x));");
25486
25487 FormatStyle Style = getLLVMStyle();
25488 Style.ColumnLimit = 40;
25489 verifyFormat("#define LIMIT_MAX(T) \\\n"
25490 " _Generic(((T)0), \\\n"
25491 " unsigned int: UINT_MAX, \\\n"
25492 " unsigned long: ULONG_MAX, \\\n"
25493 " unsigned long long: ULLONG_MAX)",
25494 Style);
25495 verifyFormat("_Generic(x,\n"
25496 " struct A: 1,\n"
25497 " void (*)(void): 2);",
25498 Style);
25499
25500 Style.ContinuationIndentWidth = 2;
25501 verifyFormat("_Generic(x,\n"
25502 " struct A: 1,\n"
25503 " void (*)(void): 2);",
25504 Style);
25505 }
25506
25507 TEST_F(FormatTest, AmbersandInLamda) {
25508 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
25509 FormatStyle AlignStyle = getLLVMStyle();
25510 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
25511 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
25512 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
25513 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
25514 }
25515
25516 TEST_F(FormatTest, TrailingReturnTypeAuto) {
25517 FormatStyle Style = getLLVMStyle();
25518 verifyFormat("[]() -> auto { return Val; }", Style);
25519 verifyFormat("[]() -> auto * { return Val; }", Style);
25520 verifyFormat("[]() -> auto & { return Val; }", Style);
25521 verifyFormat("auto foo() -> auto { return Val; }", Style);
25522 verifyFormat("auto foo() -> auto * { return Val; }", Style);
25523 verifyFormat("auto foo() -> auto & { return Val; }", Style);
25524 }
25525
25526 TEST_F(FormatTest, SpacesInConditionalStatement) {
25527 FormatStyle Spaces = getLLVMStyle();
25528 Spaces.IfMacros.clear();
25529 Spaces.IfMacros.push_back("MYIF");
25530 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
25531 Spaces.SpacesInParensOptions.InConditionalStatements = true;
25532 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
25533 verifyFormat("if ( !a )\n return;", Spaces);
25534 verifyFormat("if ( a )\n return;", Spaces);
25535 verifyFormat("if constexpr ( a )\n return;", Spaces);
25536 verifyFormat("MYIF ( a )\n return;", Spaces);
25537 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
25538 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
25539 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
25540 verifyFormat("while ( a )\n return;", Spaces);
25541 verifyFormat("while ( (a && b) )\n return;", Spaces);
25542 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
25543 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
25544 // Check that space on the left of "::" is inserted as expected at beginning
25545 // of condition.
25546 verifyFormat("while ( ::func() )\n return;", Spaces);
25547
25548 // Check impact of ControlStatementsExceptControlMacros is honored.
25549 Spaces.SpaceBeforeParens =
25550 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
25551 verifyFormat("MYIF( a )\n return;", Spaces);
25552 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
25553 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
25554 }
25555
25556 TEST_F(FormatTest, SpaceInEmptyBraces) {
25557 constexpr StringRef Code("void f() {}\n"
25558 "class Unit {};\n"
25559 "auto a = [] {};\n"
25560 "int x{};");
25561 verifyFormat(Code);
25562
25563 auto Style = getWebKitStyle();
25564 EXPECT_EQ(Style.SpaceInEmptyBraces, FormatStyle::SIEB_Always);
25565
25566 verifyFormat("void f() { }\n"
25567 "class Unit { };\n"
25568 "auto a = [] { };\n"
25569 "int x { };",
25570 Code, Style);
25571
25572 Style.SpaceInEmptyBraces = FormatStyle::SIEB_Block;
25573 verifyFormat("void f() { }\n"
25574 "class Unit { };\n"
25575 "auto a = [] { };\n"
25576 "int x {};",
25577 Code, Style);
25578 }
25579
25580 TEST_F(FormatTest, AlternativeOperators) {
25581 // Test case for ensuring alternate operators are not
25582 // combined with their right most neighbour.
25583 verifyFormat("int a and b;");
25584 verifyFormat("int a and_eq b;");
25585 verifyFormat("int a bitand b;");
25586 verifyFormat("int a bitor b;");
25587 verifyFormat("int a compl b;");
25588 verifyFormat("int a not b;");
25589 verifyFormat("int a not_eq b;");
25590 verifyFormat("int a or b;");
25591 verifyFormat("int a xor b;");
25592 verifyFormat("int a xor_eq b;");
25593 verifyFormat("return this not_eq bitand other;");
25594 verifyFormat("bool operator not_eq(const X bitand other)");
25595
25596 verifyFormat("int a and 5;");
25597 verifyFormat("int a and_eq 5;");
25598 verifyFormat("int a bitand 5;");
25599 verifyFormat("int a bitor 5;");
25600 verifyFormat("int a compl 5;");
25601 verifyFormat("int a not 5;");
25602 verifyFormat("int a not_eq 5;");
25603 verifyFormat("int a or 5;");
25604 verifyFormat("int a xor 5;");
25605 verifyFormat("int a xor_eq 5;");
25606
25607 verifyFormat("int a compl(5);");
25608 verifyFormat("int a not(5);");
25609
25610 verifyFormat("compl foo();"); // ~foo();
25611 verifyFormat("foo() <%%>"); // foo() {}
25612 verifyFormat("void foo() <%%>"); // void foo() {}
25613 verifyFormat("int a<:1:>;"); // int a[1];
25614 verifyFormat("%:define ABC abc"); // #define ABC abc
25615 verifyFormat("%:%:"); // ##
25616
25617 verifyFormat("return not ::f();");
25618 verifyFormat("return not *foo;");
25619
25620 verifyFormat("a = v(not;);\n"
25621 "c = v(not x);\n"
25622 "d = v(not 1);\n"
25623 "e = v(not 123.f);");
25624
25625 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
25626 " V(and) \\\n"
25627 " V(not) \\\n"
25628 " V(other)",
25629 getLLVMStyleWithColumns(40));
25630 }
25631
25632 TEST_F(FormatTest, STLWhileNotDefineChed) {
25633 verifyFormat("#if defined(while)\n"
25634 "#define while EMIT WARNING C4005\n"
25635 "#endif // while");
25636 }
25637
25638 TEST_F(FormatTest, OperatorSpacing) {
25639 FormatStyle Style = getLLVMStyle();
25640 Style.PointerAlignment = FormatStyle::PAS_Right;
25641 verifyFormat("Foo::operator*();", Style);
25642 verifyFormat("Foo::operator void *();", Style);
25643 verifyFormat("Foo::operator void **();", Style);
25644 verifyFormat("Foo::operator void *&();", Style);
25645 verifyFormat("Foo::operator void *&&();", Style);
25646 verifyFormat("Foo::operator void const *();", Style);
25647 verifyFormat("Foo::operator void const **();", Style);
25648 verifyFormat("Foo::operator void const *&();", Style);
25649 verifyFormat("Foo::operator void const *&&();", Style);
25650 verifyFormat("Foo::operator()(void *);", Style);
25651 verifyFormat("Foo::operator*(void *);", Style);
25652 verifyFormat("Foo::operator*();", Style);
25653 verifyFormat("Foo::operator**();", Style);
25654 verifyFormat("Foo::operator&();", Style);
25655 verifyFormat("Foo::operator<int> *();", Style);
25656 verifyFormat("Foo::operator<Foo> *();", Style);
25657 verifyFormat("Foo::operator<int> **();", Style);
25658 verifyFormat("Foo::operator<Foo> **();", Style);
25659 verifyFormat("Foo::operator<int> &();", Style);
25660 verifyFormat("Foo::operator<Foo> &();", Style);
25661 verifyFormat("Foo::operator<int> &&();", Style);
25662 verifyFormat("Foo::operator<Foo> &&();", Style);
25663 verifyFormat("Foo::operator<int> *&();", Style);
25664 verifyFormat("Foo::operator<Foo> *&();", Style);
25665 verifyFormat("Foo::operator<int> *&&();", Style);
25666 verifyFormat("Foo::operator<Foo> *&&();", Style);
25667 verifyFormat("operator*(int (*)(), class Foo);", Style);
25668
25669 verifyFormat("Foo::operator&();", Style);
25670 verifyFormat("Foo::operator void &();", Style);
25671 verifyFormat("Foo::operator void const &();", Style);
25672 verifyFormat("Foo::operator()(void &);", Style);
25673 verifyFormat("Foo::operator&(void &);", Style);
25674 verifyFormat("Foo::operator&();", Style);
25675 verifyFormat("operator&(int (&)(), class Foo);", Style);
25676 verifyFormat("operator&&(int (&)(), class Foo);", Style);
25677
25678 verifyFormat("Foo::operator&&();", Style);
25679 verifyFormat("Foo::operator**();", Style);
25680 verifyFormat("Foo::operator void &&();", Style);
25681 verifyFormat("Foo::operator void const &&();", Style);
25682 verifyFormat("Foo::operator()(void &&);", Style);
25683 verifyFormat("Foo::operator&&(void &&);", Style);
25684 verifyFormat("Foo::operator&&();", Style);
25685 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25686 verifyFormat("operator const nsTArrayRight<E> &()", Style);
25687 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
25688 Style);
25689 verifyFormat("operator void **()", Style);
25690 verifyFormat("operator const FooRight<Object> &()", Style);
25691 verifyFormat("operator const FooRight<Object> *()", Style);
25692 verifyFormat("operator const FooRight<Object> **()", Style);
25693 verifyFormat("operator const FooRight<Object> *&()", Style);
25694 verifyFormat("operator const FooRight<Object> *&&()", Style);
25695
25696 Style.PointerAlignment = FormatStyle::PAS_Left;
25697 verifyFormat("Foo::operator*();", Style);
25698 verifyFormat("Foo::operator**();", Style);
25699 verifyFormat("Foo::operator void*();", Style);
25700 verifyFormat("Foo::operator void**();", Style);
25701 verifyFormat("Foo::operator void*&();", Style);
25702 verifyFormat("Foo::operator void*&&();", Style);
25703 verifyFormat("Foo::operator void const*();", Style);
25704 verifyFormat("Foo::operator void const**();", Style);
25705 verifyFormat("Foo::operator void const*&();", Style);
25706 verifyFormat("Foo::operator void const*&&();", Style);
25707 verifyFormat("Foo::operator/*comment*/ void*();", Style);
25708 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
25709 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
25710 verifyFormat("Foo::operator()(void*);", Style);
25711 verifyFormat("Foo::operator*(void*);", Style);
25712 verifyFormat("Foo::operator*();", Style);
25713 verifyFormat("Foo::operator<int>*();", Style);
25714 verifyFormat("Foo::operator<Foo>*();", Style);
25715 verifyFormat("Foo::operator<int>**();", Style);
25716 verifyFormat("Foo::operator<Foo>**();", Style);
25717 verifyFormat("Foo::operator<Foo>*&();", Style);
25718 verifyFormat("Foo::operator<int>&();", Style);
25719 verifyFormat("Foo::operator<Foo>&();", Style);
25720 verifyFormat("Foo::operator<int>&&();", Style);
25721 verifyFormat("Foo::operator<Foo>&&();", Style);
25722 verifyFormat("Foo::operator<int>*&();", Style);
25723 verifyFormat("Foo::operator<Foo>*&();", Style);
25724 verifyFormat("operator*(int (*)(), class Foo);", Style);
25725
25726 verifyFormat("Foo::operator&();", Style);
25727 verifyFormat("Foo::operator void&();", Style);
25728 verifyFormat("Foo::operator void const&();", Style);
25729 verifyFormat("Foo::operator/*comment*/ void&();", Style);
25730 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
25731 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
25732 verifyFormat("Foo::operator()(void&);", Style);
25733 verifyFormat("Foo::operator&(void&);", Style);
25734 verifyFormat("Foo::operator&();", Style);
25735 verifyFormat("operator&(int (&)(), class Foo);", Style);
25736 verifyFormat("operator&(int (&&)(), class Foo);", Style);
25737 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25738
25739 verifyFormat("Foo::operator&&();", Style);
25740 verifyFormat("Foo::operator void&&();", Style);
25741 verifyFormat("Foo::operator void const&&();", Style);
25742 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
25743 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
25744 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
25745 verifyFormat("Foo::operator()(void&&);", Style);
25746 verifyFormat("Foo::operator&&(void&&);", Style);
25747 verifyFormat("Foo::operator&&();", Style);
25748 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25749 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
25750 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25751 Style);
25752 verifyFormat("operator void**()", Style);
25753 verifyFormat("operator const FooLeft<Object>&()", Style);
25754 verifyFormat("operator const FooLeft<Object>*()", Style);
25755 verifyFormat("operator const FooLeft<Object>**()", Style);
25756 verifyFormat("operator const FooLeft<Object>*&()", Style);
25757 verifyFormat("operator const FooLeft<Object>*&&()", Style);
25758
25759 // PR45107
25760 verifyFormat("operator Vector<String>&();", Style);
25761 verifyFormat("operator const Vector<String>&();", Style);
25762 verifyFormat("operator foo::Bar*();", Style);
25763 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
25764 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25765 Style);
25766
25767 Style.PointerAlignment = FormatStyle::PAS_Middle;
25768 verifyFormat("Foo::operator*();", Style);
25769 verifyFormat("Foo::operator void *();", Style);
25770 verifyFormat("Foo::operator()(void *);", Style);
25771 verifyFormat("Foo::operator*(void *);", Style);
25772 verifyFormat("Foo::operator*();", Style);
25773 verifyFormat("operator*(int (*)(), class Foo);", Style);
25774
25775 verifyFormat("Foo::operator&();", Style);
25776 verifyFormat("Foo::operator void &();", Style);
25777 verifyFormat("Foo::operator void const &();", Style);
25778 verifyFormat("Foo::operator()(void &);", Style);
25779 verifyFormat("Foo::operator&(void &);", Style);
25780 verifyFormat("Foo::operator&();", Style);
25781 verifyFormat("operator&(int (&)(), class Foo);", Style);
25782
25783 verifyFormat("Foo::operator&&();", Style);
25784 verifyFormat("Foo::operator void &&();", Style);
25785 verifyFormat("Foo::operator void const &&();", Style);
25786 verifyFormat("Foo::operator()(void &&);", Style);
25787 verifyFormat("Foo::operator&&(void &&);", Style);
25788 verifyFormat("Foo::operator&&();", Style);
25789 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25790 }
25791
25792 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
25793 FormatStyle Style = getLLVMStyle();
25794 // PR46157
25795 verifyFormat("foo(operator+, -42);", Style);
25796 verifyFormat("foo(operator++, -42);", Style);
25797 verifyFormat("foo(operator--, -42);", Style);
25798 verifyFormat("foo(-42, operator--);", Style);
25799 verifyFormat("foo(-42, operator, );", Style);
25800 verifyFormat("foo(operator, , -42);", Style);
25801 }
25802
25803 TEST_F(FormatTest, LineSpliceWithTrailingWhitespace) {
25804 auto Style = getLLVMStyle();
25805 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
25806 Style.UseTab = FormatStyle::UT_Never;
25807
25808 verifyFormat("int i;", " \\ \n"
25809 " int i;");
25810 verifyFormat("#define FOO(args) \\\n"
25811 " struct a {};",
25812 "#define FOO( args ) \\ \n"
25813 "struct a{\\\t\t\t\n"
25814 " };",
25815 Style);
25816 }
25817
25818 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
25819 FormatStyle Style = getLLVMStyle();
25820 Style.WhitespaceSensitiveMacros.push_back("FOO");
25821
25822 // Newlines are important here.
25823 verifyNoChange("FOO(1+2 )\n", Style);
25824 verifyNoChange("FOO(a:b:c)\n", Style);
25825
25826 // Don't use the helpers here, since 'mess up' will change the whitespace
25827 // and these are all whitespace sensitive by definition
25828 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
25829 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
25830 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
25831 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25832 " Still=Intentional);",
25833 Style);
25834 Style.AlignConsecutiveAssignments.Enabled = true;
25835 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25836 " Still=Intentional);",
25837 Style);
25838
25839 Style.ColumnLimit = 21;
25840 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
25841 }
25842
25843 TEST_F(FormatTest, SkipMacroDefinitionBody) {
25844 auto Style = getLLVMStyle();
25845 Style.SkipMacroDefinitionBody = true;
25846
25847 verifyFormat("#define A", "#define A", Style);
25848 verifyFormat("#define A a aa", "#define A a aa", Style);
25849 verifyNoChange("#define A b", Style);
25850 verifyNoChange("#define A ( args )", Style);
25851 verifyNoChange("#define A ( args ) = func ( args )", Style);
25852 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
25853 verifyNoChange("#define A ( args ) \\\n"
25854 " {\\\n"
25855 " int a = 1 ;\\\n"
25856 "}",
25857 Style);
25858
25859 verifyNoChange("#define A x:", Style);
25860 verifyNoChange("#define A a. b", Style);
25861
25862 // Surrounded with formatted code.
25863 verifyFormat("int a;\n"
25864 "#define A a\n"
25865 "int a;",
25866 "int a ;\n"
25867 "#define A a\n"
25868 "int a ;",
25869 Style);
25870
25871 // Columns are not broken when a limit is set.
25872 Style.ColumnLimit = 10;
25873 verifyFormat("#define A a a a a", " # define A a a a a ", Style);
25874 verifyNoChange("#define A a a a a", Style);
25875
25876 Style.ColumnLimit = 15;
25877 verifyFormat("#define A // a\n"
25878 " // very\n"
25879 " // long\n"
25880 " // comment",
25881 "#define A //a very long comment", Style);
25882 Style.ColumnLimit = 0;
25883
25884 // Multiline definition.
25885 verifyNoChange("#define A \\\n"
25886 "Line one with spaces . \\\n"
25887 " Line two.",
25888 Style);
25889 verifyNoChange("#define A \\\n"
25890 "a a \\\n"
25891 "a \\\n"
25892 "a",
25893 Style);
25894 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
25895 verifyNoChange("#define A \\\n"
25896 "a a \\\n"
25897 "a \\\n"
25898 "a",
25899 Style);
25900 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
25901 verifyNoChange("#define A \\\n"
25902 "a a \\\n"
25903 "a \\\n"
25904 "a",
25905 Style);
25906
25907 Style.IndentPPDirectives = FormatStyle::PPDIS_Leave;
25908 verifyNoChange("#if A\n"
25909 "#define A a\n"
25910 "#endif",
25911 Style);
25912 verifyNoChange("#if A\n"
25913 " #define A a\n"
25914 "#endif",
25915 Style);
25916 verifyNoChange("#if A\n"
25917 "# define A a\n"
25918 "#endif",
25919 Style);
25920
25921 // Adjust indendations but don't change the definition.
25922 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25923 verifyNoChange("#if A\n"
25924 "#define A a\n"
25925 "#endif",
25926 Style);
25927 verifyFormat("#if A\n"
25928 "#define A a\n"
25929 "#endif",
25930 "#if A\n"
25931 " #define A a\n"
25932 "#endif",
25933 Style);
25934 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
25935 verifyNoChange("#if A\n"
25936 "# define A a\n"
25937 "#endif",
25938 Style);
25939 verifyFormat("#if A\n"
25940 "# define A a\n"
25941 "#endif",
25942 "#if A\n"
25943 " #define A a\n"
25944 "#endif",
25945 Style);
25946 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
25947 verifyNoChange("#if A\n"
25948 " #define A a\n"
25949 "#endif",
25950 Style);
25951 verifyFormat("#if A\n"
25952 " #define A a\n"
25953 "#endif",
25954 "#if A\n"
25955 " # define A a\n"
25956 "#endif",
25957 Style);
25958
25959 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25960 // SkipMacroDefinitionBody should not affect other PP directives
25961 verifyFormat("#if !defined(A)\n"
25962 "#define A a\n"
25963 "#endif",
25964 "#if ! defined ( A )\n"
25965 " #define A a\n"
25966 "#endif",
25967 Style);
25968
25969 // With comments.
25970 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25971 Style);
25972 verifyNoChange("/* */ #define A a // a a", Style);
25973
25974 verifyFormat("int a; // a\n"
25975 "#define A // a\n"
25976 "int aaa; // a",
25977 "int a; // a\n"
25978 "#define A // a\n"
25979 "int aaa; // a",
25980 Style);
25981
25982 verifyNoChange(
25983 "#define MACRO_WITH_COMMENTS() \\\n"
25984 " public: \\\n"
25985 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
25986 " static MyType getClassTypeId(); \\\n"
25987 " /** Normal comment for the following method. */ \\\n"
25988 " virtual MyType getTypeId() const;",
25989 Style);
25990
25991 // multiline macro definitions
25992 verifyNoChange("#define A a\\\n"
25993 " A a \\\n "
25994 " A a",
25995 Style);
25996 verifyNoChange("#define MY_MACRO \\\n"
25997 " /*foo*//*bar*/ \\\n"
25998 " /* comment */ \\\n"
25999 " 1",
26000 Style);
26001 }
26002
26003 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
26004 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
26005 // test its interaction with line wrapping
26006 FormatStyle Style = getLLVMStyleWithColumns(80);
26007 verifyFormat("namespace {\n"
26008 "int i;\n"
26009 "int j;\n"
26010 "} // namespace",
26011 Style);
26012
26013 verifyFormat("namespace AAA {\n"
26014 "int i;\n"
26015 "int j;\n"
26016 "} // namespace AAA",
26017 Style);
26018
26019 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
26020 "int i;\n"
26021 "int j;\n"
26022 "} // namespace Averyveryveryverylongnamespace",
26023 "namespace Averyveryveryverylongnamespace {\n"
26024 "int i;\n"
26025 "int j;\n"
26026 "}",
26027 Style);
26028
26029 verifyFormat(
26030 "namespace "
26031 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
26032 " went::mad::now {\n"
26033 "int i;\n"
26034 "int j;\n"
26035 "} // namespace\n"
26036 " // "
26037 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
26038 "went::mad::now",
26039 "namespace "
26040 "would::it::save::you::a::lot::of::time::if_::i::"
26041 "just::gave::up::and_::went::mad::now {\n"
26042 "int i;\n"
26043 "int j;\n"
26044 "}",
26045 Style);
26046
26047 // This used to duplicate the comment again and again on subsequent runs
26048 verifyFormat(
26049 "namespace "
26050 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
26051 " went::mad::now {\n"
26052 "int i;\n"
26053 "int j;\n"
26054 "} // namespace\n"
26055 " // "
26056 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
26057 "went::mad::now",
26058 "namespace "
26059 "would::it::save::you::a::lot::of::time::if_::i::"
26060 "just::gave::up::and_::went::mad::now {\n"
26061 "int i;\n"
26062 "int j;\n"
26063 "} // namespace\n"
26064 " // "
26065 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
26066 "and_::went::mad::now",
26067 Style);
26068 }
26069
26070 TEST_F(FormatTest, LikelyUnlikely) {
26071 FormatStyle Style = getLLVMStyle();
26072
26073 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26074 " return 29;\n"
26075 "}",
26076 Style);
26077
26078 verifyFormat("if (argc > 5) [[likely]] {\n"
26079 " return 29;\n"
26080 "}",
26081 Style);
26082
26083 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26084 " return 29;\n"
26085 "} else [[likely]] {\n"
26086 " return 42;\n"
26087 "}",
26088 Style);
26089
26090 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26091 " return 29;\n"
26092 "} else if (argc > 10) [[likely]] {\n"
26093 " return 99;\n"
26094 "} else {\n"
26095 " return 42;\n"
26096 "}",
26097 Style);
26098
26099 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
26100 " return 29;\n"
26101 "}",
26102 Style);
26103
26104 verifyFormat("if (argc > 5) [[unlikely]]\n"
26105 " return 29;",
26106 Style);
26107 verifyFormat("if (argc > 5) [[likely]]\n"
26108 " return 29;",
26109 Style);
26110
26111 verifyFormat("while (limit > 0) [[unlikely]] {\n"
26112 " --limit;\n"
26113 "}",
26114 Style);
26115 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
26116 " --limit;\n"
26117 "}",
26118 Style);
26119
26120 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
26121 " --limit;",
26122 Style);
26123 verifyFormat("while (limit > 0) [[likely]]\n"
26124 " --limit;",
26125 Style);
26126
26127 Style.AttributeMacros.push_back("UNLIKELY");
26128 Style.AttributeMacros.push_back("LIKELY");
26129 verifyFormat("if (argc > 5) UNLIKELY\n"
26130 " return 29;",
26131 Style);
26132
26133 verifyFormat("if (argc > 5) UNLIKELY {\n"
26134 " return 29;\n"
26135 "}",
26136 Style);
26137 verifyFormat("if (argc > 5) UNLIKELY {\n"
26138 " return 29;\n"
26139 "} else [[likely]] {\n"
26140 " return 42;\n"
26141 "}",
26142 Style);
26143 verifyFormat("if (argc > 5) UNLIKELY {\n"
26144 " return 29;\n"
26145 "} else LIKELY {\n"
26146 " return 42;\n"
26147 "}",
26148 Style);
26149 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26150 " return 29;\n"
26151 "} else LIKELY {\n"
26152 " return 42;\n"
26153 "}",
26154 Style);
26155
26156 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
26157 " --limit;\n"
26158 "}",
26159 Style);
26160 verifyFormat("while (limit > 0) LIKELY {\n"
26161 " --limit;\n"
26162 "}",
26163 Style);
26164
26165 verifyFormat("while (limit > 0) UNLIKELY\n"
26166 " --limit;",
26167 Style);
26168 verifyFormat("for (auto &limit : limits) LIKELY\n"
26169 " --limit;",
26170 Style);
26171 }
26172
26173 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
26174 verifyFormat("Constructor()\n"
26175 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
26176 " aaaa(aaaaaaaaaaaaaaaaaa, "
26177 "aaaaaaaaaaaaaaaaaat))");
26178 verifyFormat("Constructor()\n"
26179 " : aaaaaaaaaaaaa(aaaaaa), "
26180 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
26181
26182 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
26183 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
26184 verifyFormat("Constructor()\n"
26185 " : aaaaaa(aaaaaa),\n"
26186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
26187 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
26188 StyleWithWhitespacePenalty);
26189 verifyFormat("Constructor()\n"
26190 " : aaaaaaaaaaaaa(aaaaaa), "
26191 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
26192 StyleWithWhitespacePenalty);
26193 }
26194
26195 TEST_F(FormatTest, LLVMDefaultStyle) {
26196 FormatStyle Style = getLLVMStyle();
26197 verifyFormat("extern \"C\" {\n"
26198 "int foo();\n"
26199 "}",
26200 Style);
26201 }
26202 TEST_F(FormatTest, GNUDefaultStyle) {
26203 FormatStyle Style = getGNUStyle();
26204 verifyFormat("extern \"C\"\n"
26205 "{\n"
26206 " int foo ();\n"
26207 "}",
26208 Style);
26209 }
26210 TEST_F(FormatTest, MozillaDefaultStyle) {
26211 FormatStyle Style = getMozillaStyle();
26212 verifyFormat("extern \"C\"\n"
26213 "{\n"
26214 " int foo();\n"
26215 "}",
26216 Style);
26217 }
26218 TEST_F(FormatTest, GoogleDefaultStyle) {
26219 FormatStyle Style = getGoogleStyle();
26220 verifyFormat("extern \"C\" {\n"
26221 "int foo();\n"
26222 "}",
26223 Style);
26224 }
26225 TEST_F(FormatTest, ChromiumDefaultStyle) {
26226 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
26227 verifyFormat("extern \"C\" {\n"
26228 "int foo();\n"
26229 "}",
26230 Style);
26231 }
26232 TEST_F(FormatTest, MicrosoftDefaultStyle) {
26233 FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_Cpp);
26234 verifyFormat("extern \"C\"\n"
26235 "{\n"
26236 " int foo();\n"
26237 "}",
26238 Style);
26239 }
26240 TEST_F(FormatTest, WebKitDefaultStyle) {
26241 FormatStyle Style = getWebKitStyle();
26242 verifyFormat("extern \"C\" {\n"
26243 "int foo();\n"
26244 "}",
26245 Style);
26246 }
26247
26248 TEST_F(FormatTest, Concepts) {
26249 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
26250 FormatStyle::BBCDS_Always);
26251
26252 // The default in LLVM style is REI_OuterScope, but these tests were written
26253 // when the default was REI_Keyword.
26254 FormatStyle Style = getLLVMStyle();
26255 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26256
26257 verifyFormat("template <typename T>\n"
26258 "concept True = true;");
26259
26260 verifyFormat("template <typename T>\n"
26261 "concept C = ((false || foo()) && C2<T>) ||\n"
26262 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
26263 getLLVMStyleWithColumns(60));
26264
26265 verifyFormat("template <typename T>\n"
26266 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
26267 "sizeof(T) <= 8;");
26268
26269 verifyFormat("template <typename T>\n"
26270 "concept DelayedCheck = true && requires(T t) {\n"
26271 " t.bar();\n"
26272 " t.baz();\n"
26273 " } && sizeof(T) <= 8;",
26274 Style);
26275
26276 verifyFormat("template <typename T>\n"
26277 "concept DelayedCheck = true && requires(T t) { // Comment\n"
26278 " t.bar();\n"
26279 " t.baz();\n"
26280 " } && sizeof(T) <= 8;",
26281 Style);
26282
26283 verifyFormat("template <typename T>\n"
26284 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
26285 "sizeof(T) <= 8;");
26286
26287 verifyFormat("template <typename T>\n"
26288 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
26289
26290 verifyFormat("template <typename T>\n"
26291 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
26292
26293 verifyFormat("template <typename T>\n"
26294 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
26295
26296 verifyFormat("template <typename T>\n"
26297 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
26298 "&& sizeof(T) <= 8;");
26299
26300 verifyFormat("template <typename T>\n"
26301 "concept DelayedCheck =\n"
26302 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
26303 "sizeof(T) <= 8;");
26304
26305 verifyFormat("template <typename T>\n"
26306 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
26307 "&& sizeof(T) <= 8;");
26308
26309 verifyFormat(
26310 "template <typename T>\n"
26311 "concept DelayedCheck =\n"
26312 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
26313
26314 verifyFormat("template <typename T>\n"
26315 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
26316 "&& sizeof(T) <= 8;");
26317
26318 verifyFormat("template <typename T>\n"
26319 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
26320 "sizeof(T) <= 8;");
26321
26322 verifyFormat("template <typename T>\n"
26323 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
26324 " requires(T t) {\n"
26325 " t.bar();\n"
26326 " t.baz();\n"
26327 " } && sizeof(T) <= 8 && !(4 < 3);",
26328 getLLVMStyleWithColumns(60));
26329
26330 verifyFormat("template <typename T>\n"
26331 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
26332
26333 verifyFormat("template <typename T>\n"
26334 "concept C = foo();");
26335
26336 verifyFormat("template <typename T>\n"
26337 "concept C = foo(T());");
26338
26339 verifyFormat("template <typename T>\n"
26340 "concept C = foo(T{});");
26341
26342 verifyFormat("template <typename T>\n"
26343 "concept Size = V<sizeof(T)>::Value > 5;");
26344
26345 verifyFormat("template <typename T>\n"
26346 "concept True = S<T>::Value;");
26347
26348 verifyFormat("template <S T>\n"
26349 "concept True = T.field;");
26350
26351 verifyFormat(
26352 "template <typename T>\n"
26353 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
26354 " sizeof(T) <= 8;");
26355
26356 // FIXME: This is misformatted because the fake l paren starts at bool, not at
26357 // the lambda l square.
26358 verifyFormat("template <typename T>\n"
26359 "concept C = [] -> bool { return true; }() && requires(T t) { "
26360 "t.bar(); } &&\n"
26361 " sizeof(T) <= 8;");
26362
26363 verifyFormat(
26364 "template <typename T>\n"
26365 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
26366 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
26367
26368 verifyFormat("template <typename T>\n"
26369 "concept C = decltype([]() { return std::true_type{}; "
26370 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
26371 getLLVMStyleWithColumns(120));
26372
26373 verifyFormat("template <typename T>\n"
26374 "concept C = decltype([]() -> std::true_type { return {}; "
26375 "}())::value &&\n"
26376 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
26377
26378 verifyFormat("template <typename T>\n"
26379 "concept C = true;\n"
26380 "Foo Bar;");
26381
26382 verifyFormat("template <typename T>\n"
26383 "concept Hashable = requires(T a) {\n"
26384 " { std::hash<T>{}(a) } -> "
26385 "std::convertible_to<std::size_t>;\n"
26386 " };",
26387 Style);
26388
26389 verifyFormat(
26390 "template <typename T>\n"
26391 "concept EqualityComparable = requires(T a, T b) {\n"
26392 " { a == b } -> std::same_as<bool>;\n"
26393 " };",
26394 Style);
26395
26396 verifyFormat(
26397 "template <typename T>\n"
26398 "concept EqualityComparable = requires(T a, T b) {\n"
26399 " { a == b } -> std::same_as<bool>;\n"
26400 " { a != b } -> std::same_as<bool>;\n"
26401 " };",
26402 Style);
26403
26404 verifyFormat("template <typename T>\n"
26405 "concept WeakEqualityComparable = requires(T a, T b) {\n"
26406 " { a == b };\n"
26407 " { a != b };\n"
26408 " };",
26409 Style);
26410
26411 verifyFormat("template <typename T>\n"
26412 "concept HasSizeT = requires { typename T::size_t; };");
26413
26414 verifyFormat("template <typename T>\n"
26415 "concept Semiregular =\n"
26416 " DefaultConstructible<T> && CopyConstructible<T> && "
26417 "CopyAssignable<T> &&\n"
26418 " requires(T a, std::size_t n) {\n"
26419 " requires Same<T *, decltype(&a)>;\n"
26420 " { a.~T() } noexcept;\n"
26421 " requires Same<T *, decltype(new T)>;\n"
26422 " requires Same<T *, decltype(new T[n])>;\n"
26423 " { delete new T; };\n"
26424 " { delete new T[n]; };\n"
26425 " };",
26426 Style);
26427
26428 verifyFormat("template <typename T>\n"
26429 "concept Semiregular =\n"
26430 " requires(T a, std::size_t n) {\n"
26431 " requires Same<T *, decltype(&a)>;\n"
26432 " { a.~T() } noexcept;\n"
26433 " requires Same<T *, decltype(new T)>;\n"
26434 " requires Same<T *, decltype(new T[n])>;\n"
26435 " { delete new T; };\n"
26436 " { delete new T[n]; };\n"
26437 " { new T } -> std::same_as<T *>;\n"
26438 " } && DefaultConstructible<T> && CopyConstructible<T> && "
26439 "CopyAssignable<T>;",
26440 Style);
26441
26442 verifyFormat(
26443 "template <typename T>\n"
26444 "concept Semiregular =\n"
26445 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
26446 " requires Same<T *, decltype(&a)>;\n"
26447 " { a.~T() } noexcept;\n"
26448 " requires Same<T *, decltype(new T)>;\n"
26449 " requires Same<T *, decltype(new "
26450 "T[n])>;\n"
26451 " { delete new T; };\n"
26452 " { delete new T[n]; };\n"
26453 " } && CopyConstructible<T> && "
26454 "CopyAssignable<T>;",
26455 Style);
26456
26457 verifyFormat("template <typename T>\n"
26458 "concept Two = requires(T t) {\n"
26459 " { t.foo() } -> std::same_as<Bar>;\n"
26460 " } && requires(T &&t) {\n"
26461 " { t.foo() } -> std::same_as<Bar &&>;\n"
26462 " };",
26463 Style);
26464
26465 verifyFormat(
26466 "template <typename T>\n"
26467 "concept C = requires(T x) {\n"
26468 " { *x } -> std::convertible_to<typename T::inner>;\n"
26469 " { x + 1 } noexcept -> std::same_as<int>;\n"
26470 " { x * 1 } -> std::convertible_to<T>;\n"
26471 " };",
26472 Style);
26473
26474 verifyFormat("template <typename T>\n"
26475 "concept C = requires(T x) {\n"
26476 " {\n"
26477 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
26478 " } -> long_long_concept_name<T>;\n"
26479 " {\n"
26480 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
26481 " } noexcept -> long_long_concept_name<T>;\n"
26482 " };",
26483 Style);
26484
26485 verifyFormat(
26486 "template <typename T, typename U = T>\n"
26487 "concept Swappable = requires(T &&t, U &&u) {\n"
26488 " swap(std::forward<T>(t), std::forward<U>(u));\n"
26489 " swap(std::forward<U>(u), std::forward<T>(t));\n"
26490 " };",
26491 Style);
26492
26493 verifyFormat("template <typename T, typename U>\n"
26494 "concept Common = requires(T &&t, U &&u) {\n"
26495 " typename CommonType<T, U>;\n"
26496 " { CommonType<T, U>(std::forward<T>(t)) };\n"
26497 " };",
26498 Style);
26499
26500 verifyFormat("template <typename T, typename U>\n"
26501 "concept Common = requires(T &&t, U &&u) {\n"
26502 " typename CommonType<T, U>;\n"
26503 " { CommonType<T, U>{std::forward<T>(t)} };\n"
26504 " };",
26505 Style);
26506
26507 verifyFormat(
26508 "template <typename T>\n"
26509 "concept C = requires(T t) {\n"
26510 " requires Bar<T> && Foo<T>;\n"
26511 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
26512 " };",
26513 Style);
26514
26515 verifyFormat("template <typename T>\n"
26516 "concept HasFoo = requires(T t) {\n"
26517 " { t.foo() };\n"
26518 " t.foo();\n"
26519 " };\n"
26520 "template <typename T>\n"
26521 "concept HasBar = requires(T t) {\n"
26522 " { t.bar() };\n"
26523 " t.bar();\n"
26524 " };",
26525 Style);
26526
26527 verifyFormat("template <typename T>\n"
26528 "concept Large = sizeof(T) > 10;");
26529
26530 verifyFormat("template <typename T, typename U>\n"
26531 "concept FooableWith = requires(T t, U u) {\n"
26532 " typename T::foo_type;\n"
26533 " { t.foo(u) } -> typename T::foo_type;\n"
26534 " t++;\n"
26535 " };\n"
26536 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
26537 Style);
26538
26539 verifyFormat("template <typename T>\n"
26540 "concept Context = is_specialization_of_v<context, T>;");
26541
26542 verifyFormat("template <typename T>\n"
26543 "concept Node = std::is_object_v<T>;");
26544
26545 verifyFormat("template <class T>\n"
26546 "concept integral = __is_integral(T);");
26547
26548 verifyFormat("template <class T>\n"
26549 "concept is2D = __array_extent(T, 1) == 2;");
26550
26551 verifyFormat("template <class T>\n"
26552 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
26553
26554 verifyFormat("template <class T, class T2>\n"
26555 "concept Same = __is_same_as<T, T2>;");
26556
26557 verifyFormat(
26558 "template <class _InIt, class _OutIt>\n"
26559 "concept _Can_reread_dest =\n"
26560 " std::forward_iterator<_OutIt> &&\n"
26561 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
26562
26563 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
26564
26565 verifyFormat(
26566 "template <typename T>\n"
26567 "concept C = requires(T t) {\n"
26568 " requires Bar<T> && Foo<T>;\n"
26569 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
26570 " };",
26571 Style);
26572
26573 verifyFormat("template <typename T>\n"
26574 "concept HasFoo = requires(T t) {\n"
26575 " { t.foo() };\n"
26576 " t.foo();\n"
26577 " };\n"
26578 "template <typename T>\n"
26579 "concept HasBar = requires(T t) {\n"
26580 " { t.bar() };\n"
26581 " t.bar();\n"
26582 " };",
26583 Style);
26584
26585 verifyFormat("template <typename T> concept True = true;", Style);
26586
26587 verifyFormat("template <typename T>\n"
26588 "concept C = decltype([]() -> std::true_type { return {}; "
26589 "}())::value &&\n"
26590 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
26591 Style);
26592
26593 verifyFormat("template <typename T>\n"
26594 "concept Semiregular =\n"
26595 " DefaultConstructible<T> && CopyConstructible<T> && "
26596 "CopyAssignable<T> &&\n"
26597 " requires(T a, std::size_t n) {\n"
26598 " requires Same<T *, decltype(&a)>;\n"
26599 " { a.~T() } noexcept;\n"
26600 " requires Same<T *, decltype(new T)>;\n"
26601 " requires Same<T *, decltype(new T[n])>;\n"
26602 " { delete new T; };\n"
26603 " { delete new T[n]; };\n"
26604 " };",
26605 Style);
26606
26607 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
26608
26609 verifyFormat("template <typename T> concept C =\n"
26610 " requires(T t) {\n"
26611 " requires Bar<T> && Foo<T>;\n"
26612 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
26613 " };",
26614 Style);
26615
26616 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
26617 " { t.foo() };\n"
26618 " t.foo();\n"
26619 " };\n"
26620 "template <typename T> concept HasBar = requires(T t) {\n"
26621 " { t.bar() };\n"
26622 " t.bar();\n"
26623 " };",
26624 Style);
26625
26626 verifyFormat("template <typename T> concept True = true;", Style);
26627
26628 verifyFormat(
26629 "template <typename T> concept C =\n"
26630 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
26631 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
26632 Style);
26633
26634 verifyFormat("template <typename T> concept Semiregular =\n"
26635 " DefaultConstructible<T> && CopyConstructible<T> && "
26636 "CopyAssignable<T> &&\n"
26637 " requires(T a, std::size_t n) {\n"
26638 " requires Same<T *, decltype(&a)>;\n"
26639 " { a.~T() } noexcept;\n"
26640 " requires Same<T *, decltype(new T)>;\n"
26641 " requires Same<T *, decltype(new T[n])>;\n"
26642 " { delete new T; };\n"
26643 " { delete new T[n]; };\n"
26644 " };",
26645 Style);
26646
26647 // The following tests are invalid C++, we just want to make sure we don't
26648 // assert.
26649 verifyNoCrash("template <typename T>\n"
26650 "concept C = requires C2<T>;");
26651
26652 verifyNoCrash("template <typename T>\n"
26653 "concept C = 5 + 4;");
26654
26655 verifyNoCrash("template <typename T>\n"
26656 "concept C = class X;");
26657
26658 verifyNoCrash("template <typename T>\n"
26659 "concept C = [] && true;");
26660
26661 verifyNoCrash("template <typename T>\n"
26662 "concept C = [] && requires(T t) { typename T::size_type; };");
26663 }
26664
26665 TEST_F(FormatTest, RequiresClausesPositions) {
26666 auto Style = getLLVMStyle();
26667 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
26668 EXPECT_EQ(Style.IndentRequiresClause, true);
26669
26670 // The default in LLVM style is REI_OuterScope, but these tests were written
26671 // when the default was REI_Keyword.
26672 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26673
26674 verifyFormat("template <typename T>\n"
26675 " requires(Foo<T> && std::trait<T>)\n"
26676 "struct Bar;",
26677 Style);
26678
26679 verifyFormat("template <typename T>\n"
26680 " requires(Foo<T> && std::trait<T>)\n"
26681 "class Bar {\n"
26682 "public:\n"
26683 " Bar(T t);\n"
26684 " bool baz();\n"
26685 "};",
26686 Style);
26687
26688 verifyFormat(
26689 "template <typename T>\n"
26690 " requires requires(T &&t) {\n"
26691 " typename T::I;\n"
26692 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26693 " }\n"
26694 "Bar(T) -> Bar<typename T::I>;",
26695 Style);
26696
26697 verifyFormat("template <typename T>\n"
26698 " requires(Foo<T> && std::trait<T>)\n"
26699 "constexpr T MyGlobal;",
26700 Style);
26701
26702 verifyFormat("template <typename T>\n"
26703 " requires Foo<T> && requires(T t) {\n"
26704 " { t.baz() } -> std::same_as<bool>;\n"
26705 " requires std::same_as<T::Factor, int>;\n"
26706 " }\n"
26707 "inline int bar(T t) {\n"
26708 " return t.baz() ? T::Factor : 5;\n"
26709 "}",
26710 Style);
26711
26712 verifyFormat("template <typename T>\n"
26713 "inline int bar(T t)\n"
26714 " requires Foo<T> && requires(T t) {\n"
26715 " { t.baz() } -> std::same_as<bool>;\n"
26716 " requires std::same_as<T::Factor, int>;\n"
26717 " }\n"
26718 "{\n"
26719 " return t.baz() ? T::Factor : 5;\n"
26720 "}",
26721 Style);
26722
26723 verifyFormat("template <typename T>\n"
26724 " requires F<T>\n"
26725 "int bar(T t) {\n"
26726 " return 5;\n"
26727 "}",
26728 Style);
26729
26730 verifyFormat("template <typename T>\n"
26731 "int bar(T t)\n"
26732 " requires F<T>\n"
26733 "{\n"
26734 " return 5;\n"
26735 "}",
26736 Style);
26737
26738 verifyFormat("template <typename T>\n"
26739 "int S::bar(T t) &&\n"
26740 " requires F<T>\n"
26741 "{\n"
26742 " return 5;\n"
26743 "}",
26744 Style);
26745
26746 verifyFormat("template <typename T>\n"
26747 "int bar(T t)\n"
26748 " requires F<T>;",
26749 Style);
26750
26751 Style.IndentRequiresClause = false;
26752 verifyFormat("template <typename T>\n"
26753 "requires F<T>\n"
26754 "int bar(T t) {\n"
26755 " return 5;\n"
26756 "}",
26757 Style);
26758
26759 verifyFormat("template <typename T>\n"
26760 "int S::bar(T t) &&\n"
26761 "requires F<T>\n"
26762 "{\n"
26763 " return 5;\n"
26764 "}",
26765 Style);
26766
26767 verifyFormat("template <typename T>\n"
26768 "int bar(T t)\n"
26769 "requires F<T>\n"
26770 "{\n"
26771 " return 5;\n"
26772 "}",
26773 Style);
26774
26775 Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace;
26776 Style.IndentRequiresClause = true;
26777
26778 verifyFormat("template <typename T>\n"
26779 " requires(Foo<T> && std::trait<T>)\n"
26780 "struct Bar;",
26781 Style);
26782
26783 verifyFormat("template <typename T>\n"
26784 " requires(Foo<T> && std::trait<T>)\n"
26785 "class Bar {\n"
26786 "public:\n"
26787 " Bar(T t);\n"
26788 " bool baz();\n"
26789 "};",
26790 Style);
26791
26792 verifyFormat(
26793 "template <typename T>\n"
26794 " requires requires(T &&t) {\n"
26795 " typename T::I;\n"
26796 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26797 " }\n"
26798 "Bar(T) -> Bar<typename T::I>;",
26799 Style);
26800
26801 verifyFormat("template <typename T>\n"
26802 " requires(Foo<T> && std::trait<T>)\n"
26803 "constexpr T MyGlobal;",
26804 Style);
26805
26806 verifyFormat("template <typename T>\n"
26807 " requires Foo<T> && requires(T t) {\n"
26808 " { t.baz() } -> std::same_as<bool>;\n"
26809 " requires std::same_as<T::Factor, int>;\n"
26810 " }\n"
26811 "inline int bar(T t) {\n"
26812 " return t.baz() ? T::Factor : 5;\n"
26813 "}",
26814 Style);
26815
26816 verifyFormat("template <typename T>\n"
26817 "inline int bar(T t)\n"
26818 " requires Foo<T> && requires(T t) {\n"
26819 " { t.baz() } -> std::same_as<bool>;\n"
26820 " requires std::same_as<T::Factor, int>;\n"
26821 " } {\n"
26822 " return t.baz() ? T::Factor : 5;\n"
26823 "}",
26824 Style);
26825
26826 verifyFormat("template <typename T>\n"
26827 " requires F<T>\n"
26828 "int bar(T t) {\n"
26829 " return 5;\n"
26830 "}",
26831 Style);
26832
26833 verifyFormat("template <typename T>\n"
26834 "int bar(T t)\n"
26835 " requires F<T> {\n"
26836 " return 5;\n"
26837 "}",
26838 Style);
26839
26840 verifyFormat("template <typename T>\n"
26841 "int S::bar(T t) &&\n"
26842 " requires F<T> {\n"
26843 " return 5;\n"
26844 "}",
26845 Style);
26846
26847 verifyFormat("template <typename T>\n"
26848 "int bar(T t)\n"
26849 " requires F<T>;",
26850 Style);
26851
26852 verifyFormat("template <typename T>\n"
26853 "int bar(T t)\n"
26854 " requires F<T> {}",
26855 Style);
26856
26857 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
26858 Style.IndentRequiresClause = false;
26859 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26860 "template <typename T> requires Foo<T> void bar() {}\n"
26861 "template <typename T> void bar() requires Foo<T> {}\n"
26862 "template <typename T> void bar() requires Foo<T>;\n"
26863 "template <typename T> void S::bar() && requires Foo<T> {}\n"
26864 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26865 Style);
26866
26867 auto ColumnStyle = Style;
26868 ColumnStyle.ColumnLimit = 40;
26869 verifyFormat("template <typename AAAAAAA>\n"
26870 "requires Foo<T> struct Bar {};\n"
26871 "template <typename AAAAAAA>\n"
26872 "requires Foo<T> void bar() {}\n"
26873 "template <typename AAAAAAA>\n"
26874 "void bar() requires Foo<T> {}\n"
26875 "template <typename T>\n"
26876 "void S::bar() && requires Foo<T> {}\n"
26877 "template <typename AAAAAAA>\n"
26878 "requires Foo<T> Baz(T) -> Baz<T>;",
26879 ColumnStyle);
26880
26881 verifyFormat("template <typename T>\n"
26882 "requires Foo<AAAAAAA> struct Bar {};\n"
26883 "template <typename T>\n"
26884 "requires Foo<AAAAAAA> void bar() {}\n"
26885 "template <typename T>\n"
26886 "void bar() requires Foo<AAAAAAA> {}\n"
26887 "template <typename T>\n"
26888 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26889 ColumnStyle);
26890
26891 verifyFormat("template <typename AAAAAAA>\n"
26892 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26893 "struct Bar {};\n"
26894 "template <typename AAAAAAA>\n"
26895 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26896 "void bar() {}\n"
26897 "template <typename AAAAAAA>\n"
26898 "void bar()\n"
26899 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26900 "template <typename AAAAAAA>\n"
26901 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26902 "template <typename AAAAAAA>\n"
26903 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26904 "Bar(T) -> Bar<T>;",
26905 ColumnStyle);
26906
26907 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26908 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26909
26910 verifyFormat("template <typename T>\n"
26911 "requires Foo<T> struct Bar {};\n"
26912 "template <typename T>\n"
26913 "requires Foo<T> void bar() {}\n"
26914 "template <typename T>\n"
26915 "void bar()\n"
26916 "requires Foo<T> {}\n"
26917 "template <typename T>\n"
26918 "void bar()\n"
26919 "requires Foo<T>;\n"
26920 "template <typename T>\n"
26921 "void S::bar() &&\n"
26922 "requires Foo<T> {}\n"
26923 "template <typename T>\n"
26924 "requires Foo<T> Bar(T) -> Bar<T>;",
26925 Style);
26926
26927 verifyFormat("template <typename AAAAAAA>\n"
26928 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26929 "struct Bar {};\n"
26930 "template <typename AAAAAAA>\n"
26931 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26932 "void bar() {}\n"
26933 "template <typename AAAAAAA>\n"
26934 "void bar()\n"
26935 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26936 "template <typename AAAAAAA>\n"
26937 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26938 "template <typename AAAAAAA>\n"
26939 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26940 "Bar(T) -> Bar<T>;",
26941 ColumnStyle);
26942
26943 Style.IndentRequiresClause = true;
26944 ColumnStyle.IndentRequiresClause = true;
26945
26946 verifyFormat("template <typename T>\n"
26947 " requires Foo<T> struct Bar {};\n"
26948 "template <typename T>\n"
26949 " requires Foo<T> void bar() {}\n"
26950 "template <typename T>\n"
26951 "void bar()\n"
26952 " requires Foo<T> {}\n"
26953 "template <typename T>\n"
26954 "void S::bar() &&\n"
26955 " requires Foo<T> {}\n"
26956 "template <typename T>\n"
26957 " requires Foo<T> Bar(T) -> Bar<T>;",
26958 Style);
26959
26960 verifyFormat("template <typename AAAAAAA>\n"
26961 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26962 "struct Bar {};\n"
26963 "template <typename AAAAAAA>\n"
26964 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26965 "void bar() {}\n"
26966 "template <typename AAAAAAA>\n"
26967 "void bar()\n"
26968 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26969 "template <typename AAAAAAA>\n"
26970 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26971 "template <typename AAAAAAA>\n"
26972 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26973 "Bar(T) -> Bar<T>;",
26974 ColumnStyle);
26975
26976 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26977 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26978
26979 verifyFormat("template <typename T> requires Foo<T>\n"
26980 "struct Bar {};\n"
26981 "template <typename T> requires Foo<T>\n"
26982 "void bar() {}\n"
26983 "template <typename T>\n"
26984 "void bar() requires Foo<T>\n"
26985 "{}\n"
26986 "template <typename T> void bar() requires Foo<T>;\n"
26987 "template <typename T>\n"
26988 "void S::bar() && requires Foo<T>\n"
26989 "{}\n"
26990 "template <typename T> requires Foo<T>\n"
26991 "Bar(T) -> Bar<T>;",
26992 Style);
26993
26994 verifyFormat("template <typename AAAAAAA>\n"
26995 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26996 "struct Bar {};\n"
26997 "template <typename AAAAAAA>\n"
26998 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26999 "void bar() {}\n"
27000 "template <typename AAAAAAA>\n"
27001 "void bar()\n"
27002 " requires Foo<AAAAAAAAAAAAAAAA>\n"
27003 "{}\n"
27004 "template <typename AAAAAAA>\n"
27005 "requires Foo<AAAAAAAA>\n"
27006 "Bar(T) -> Bar<T>;\n"
27007 "template <typename AAAAAAA>\n"
27008 "requires Foo<AAAAAAAAAAAAAAAA>\n"
27009 "Bar(T) -> Bar<T>;",
27010 ColumnStyle);
27011 }
27012
27013 TEST_F(FormatTest, RequiresClauses) {
27014 verifyFormat("struct [[nodiscard]] zero_t {\n"
27015 " template <class T>\n"
27016 " requires requires { number_zero_v<T>; }\n"
27017 " [[nodiscard]] constexpr operator T() const {\n"
27018 " return number_zero_v<T>;\n"
27019 " }\n"
27020 "};");
27021
27022 verifyFormat("template <class T>\n"
27023 " requires(std::same_as<int, T>)\n"
27024 "decltype(auto) fun() {}");
27025
27026 auto Style = getLLVMStyle();
27027
27028 verifyFormat(
27029 "template <typename T>\n"
27030 " requires is_default_constructible_v<hash<T>> and\n"
27031 " is_copy_constructible_v<hash<T>> and\n"
27032 " is_move_constructible_v<hash<T>> and\n"
27033 " is_copy_assignable_v<hash<T>> and "
27034 "is_move_assignable_v<hash<T>> and\n"
27035 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
27036 " is_callable_v<hash<T>(T)> and\n"
27037 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
27038 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
27039 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
27040 "struct S {};",
27041 Style);
27042
27043 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
27044 verifyFormat(
27045 "template <typename T>\n"
27046 " requires is_default_constructible_v<hash<T>>\n"
27047 " and is_copy_constructible_v<hash<T>>\n"
27048 " and is_move_constructible_v<hash<T>>\n"
27049 " and is_copy_assignable_v<hash<T>> and "
27050 "is_move_assignable_v<hash<T>>\n"
27051 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
27052 " and is_callable_v<hash<T>(T)>\n"
27053 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
27054 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
27055 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
27056 "&>()))>\n"
27057 "struct S {};",
27058 Style);
27059
27060 Style = getLLVMStyle();
27061 Style.ConstructorInitializerIndentWidth = 4;
27062 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
27063 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
27064 verifyFormat("constexpr Foo(Foo const &other)\n"
27065 " requires std::is_copy_constructible<T>\n"
27066 " : value{other.value} {\n"
27067 " do_magic();\n"
27068 " do_more_magic();\n"
27069 "}",
27070 Style);
27071
27072 // Not a clause, but we once hit an assert.
27073 verifyFormat("#if 0\n"
27074 "#else\n"
27075 "foo();\n"
27076 "#endif\n"
27077 "bar(requires);");
27078
27079 verifyNoCrash("template <class T>\n"
27080 " requires(requires { std::declval<T>()");
27081 }
27082
27083 TEST_F(FormatTest, RequiresExpressionIndentation) {
27084 auto Style = getLLVMStyle();
27085 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
27086
27087 verifyFormat("template <typename T>\n"
27088 "concept C = requires(T t) {\n"
27089 " typename T::value;\n"
27090 " requires requires(typename T::value v) {\n"
27091 " { t == v } -> std::same_as<bool>;\n"
27092 " };\n"
27093 "};",
27094 Style);
27095
27096 verifyFormat("template <typename T>\n"
27097 "void bar(T)\n"
27098 " requires Foo<T> && requires(T t) {\n"
27099 " { t.foo() } -> std::same_as<int>;\n"
27100 " } && requires(T t) {\n"
27101 " { t.bar() } -> std::same_as<bool>;\n"
27102 " --t;\n"
27103 " };",
27104 Style);
27105
27106 verifyFormat("template <typename T>\n"
27107 " requires Foo<T> &&\n"
27108 " requires(T t) {\n"
27109 " { t.foo() } -> std::same_as<int>;\n"
27110 " } && requires(T t) {\n"
27111 " { t.bar() } -> std::same_as<bool>;\n"
27112 " --t;\n"
27113 " }\n"
27114 "void bar(T);",
27115 Style);
27116
27117 verifyFormat("template <typename T> void f() {\n"
27118 " if constexpr (requires(T t) {\n"
27119 " { t.bar() } -> std::same_as<bool>;\n"
27120 " }) {\n"
27121 " }\n"
27122 "}",
27123 Style);
27124
27125 verifyFormat("template <typename T> void f() {\n"
27126 " if constexpr (condition && requires(T t) {\n"
27127 " { t.bar() } -> std::same_as<bool>;\n"
27128 " }) {\n"
27129 " }\n"
27130 "}",
27131 Style);
27132
27133 verifyFormat("template <typename T> struct C {\n"
27134 " void f()\n"
27135 " requires requires(T t) {\n"
27136 " { t.bar() } -> std::same_as<bool>;\n"
27137 " };\n"
27138 "};",
27139 Style);
27140
27141 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
27142
27143 verifyFormat("template <typename T>\n"
27144 "concept C = requires(T t) {\n"
27145 " typename T::value;\n"
27146 " requires requires(typename T::value v) {\n"
27147 " { t == v } -> std::same_as<bool>;\n"
27148 " };\n"
27149 " };",
27150 Style);
27151
27152 verifyFormat(
27153 "template <typename T>\n"
27154 "void bar(T)\n"
27155 " requires Foo<T> && requires(T t) {\n"
27156 " { t.foo() } -> std::same_as<int>;\n"
27157 " } && requires(T t) {\n"
27158 " { t.bar() } -> std::same_as<bool>;\n"
27159 " --t;\n"
27160 " };",
27161 Style);
27162
27163 verifyFormat("template <typename T>\n"
27164 " requires Foo<T> &&\n"
27165 " requires(T t) {\n"
27166 " { t.foo() } -> std::same_as<int>;\n"
27167 " } && requires(T t) {\n"
27168 " { t.bar() } -> std::same_as<bool>;\n"
27169 " --t;\n"
27170 " }\n"
27171 "void bar(T);",
27172 Style);
27173
27174 verifyFormat("template <typename T> void f() {\n"
27175 " if constexpr (requires(T t) {\n"
27176 " { t.bar() } -> std::same_as<bool>;\n"
27177 " }) {\n"
27178 " }\n"
27179 "}",
27180 Style);
27181
27182 verifyFormat(
27183 "template <typename T> void f() {\n"
27184 " if constexpr (condition && requires(T t) {\n"
27185 " { t.bar() } -> std::same_as<bool>;\n"
27186 " }) {\n"
27187 " }\n"
27188 "}",
27189 Style);
27190
27191 verifyFormat("template <typename T> struct C {\n"
27192 " void f()\n"
27193 " requires requires(T t) {\n"
27194 " { t.bar() } -> std::same_as<bool>;\n"
27195 " };\n"
27196 "};",
27197 Style);
27198 }
27199
27200 TEST_F(FormatTest, StatementAttributeLikeMacros) {
27201 FormatStyle Style = getLLVMStyle();
27202 StringRef Source = "void Foo::slot() {\n"
27203 " unsigned char MyChar = 'x';\n"
27204 " emit signal(MyChar);\n"
27205 " Q_EMIT signal(MyChar);\n"
27206 "}";
27207
27208 verifyFormat(Source, Style);
27209
27210 Style.AlignConsecutiveDeclarations.Enabled = true;
27211 verifyFormat("void Foo::slot() {\n"
27212 " unsigned char MyChar = 'x';\n"
27213 " emit signal(MyChar);\n"
27214 " Q_EMIT signal(MyChar);\n"
27215 "}",
27216 Source, Style);
27217
27218 Style.StatementAttributeLikeMacros.push_back("emit");
27219 verifyFormat(Source, Style);
27220
27221 Style.StatementAttributeLikeMacros = {};
27222 verifyFormat("void Foo::slot() {\n"
27223 " unsigned char MyChar = 'x';\n"
27224 " emit signal(MyChar);\n"
27225 " Q_EMIT signal(MyChar);\n"
27226 "}",
27227 Source, Style);
27228 }
27229
27230 TEST_F(FormatTest, IndentAccessModifiers) {
27231 FormatStyle Style = getLLVMStyle();
27232 Style.IndentAccessModifiers = true;
27233 // Members are *two* levels below the record;
27234 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
27235 verifyFormat("class C {\n"
27236 " int i;\n"
27237 "};",
27238 Style);
27239 verifyFormat("union C {\n"
27240 " int i;\n"
27241 " unsigned u;\n"
27242 "};",
27243 Style);
27244 // Access modifiers should be indented one level below the record.
27245 verifyFormat("class C {\n"
27246 " public:\n"
27247 " int i;\n"
27248 "};",
27249 Style);
27250 verifyFormat("class C {\n"
27251 " public /* comment */:\n"
27252 " int i;\n"
27253 "};",
27254 Style);
27255 verifyFormat("struct S {\n"
27256 " private:\n"
27257 " class C {\n"
27258 " int j;\n"
27259 "\n"
27260 " public:\n"
27261 " C();\n"
27262 " };\n"
27263 "\n"
27264 " public:\n"
27265 " int i;\n"
27266 "};",
27267 Style);
27268 // Enumerations are not records and should be unaffected.
27269 Style.AllowShortEnumsOnASingleLine = false;
27270 verifyFormat("enum class E {\n"
27271 " A,\n"
27272 " B\n"
27273 "};",
27274 Style);
27275 // Test with a different indentation width;
27276 // also proves that the result is Style.AccessModifierOffset agnostic.
27277 Style.IndentWidth = 3;
27278 verifyFormat("class C {\n"
27279 " public:\n"
27280 " int i;\n"
27281 "};",
27282 Style);
27283 verifyFormat("class C {\n"
27284 " public /**/:\n"
27285 " int i;\n"
27286 "};",
27287 Style);
27288 Style.AttributeMacros.push_back("FOO");
27289 verifyFormat("class C {\n"
27290 " FOO public:\n"
27291 " int i;\n"
27292 "};",
27293 Style);
27294 }
27295
27296 TEST_F(FormatTest, LimitlessStringsAndComments) {
27297 auto Style = getLLVMStyleWithColumns(0);
27298 constexpr StringRef Code(
27299 "/**\n"
27300 " * This is a multiline comment with quite some long lines, at least for "
27301 "the LLVM Style.\n"
27302 " * We will redo this with strings and line comments. Just to check if "
27303 "everything is working.\n"
27304 " */\n"
27305 "bool foo() {\n"
27306 " /* Single line multi line comment. */\n"
27307 " const std::string String = \"This is a multiline string with quite "
27308 "some long lines, at least for the LLVM Style.\"\n"
27309 " \"We already did it with multi line "
27310 "comments, and we will do it with line comments. Just to check if "
27311 "everything is working.\";\n"
27312 " // This is a line comment (block) with quite some long lines, at "
27313 "least for the LLVM Style.\n"
27314 " // We already did this with multi line comments and strings. Just to "
27315 "check if everything is working.\n"
27316 " const std::string SmallString = \"Hello World\";\n"
27317 " // Small line comment\n"
27318 " return String.size() > SmallString.size();\n"
27319 "}");
27320 verifyNoChange(Code, Style);
27321 }
27322
27323 TEST_F(FormatTest, FormatDecayCopy) {
27324 // error cases from unit tests
27325 verifyFormat("foo(auto())");
27326 verifyFormat("foo(auto{})");
27327 verifyFormat("foo(auto({}))");
27328 verifyFormat("foo(auto{{}})");
27329
27330 verifyFormat("foo(auto(1))");
27331 verifyFormat("foo(auto{1})");
27332 verifyFormat("foo(new auto(1))");
27333 verifyFormat("foo(new auto{1})");
27334 verifyFormat("decltype(auto(1)) x;");
27335 verifyFormat("decltype(auto{1}) x;");
27336 verifyFormat("auto(x);");
27337 verifyFormat("auto{x};");
27338 verifyFormat("new auto{x};");
27339 verifyFormat("auto{x} = y;");
27340 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
27341 // the user's own fault
27342 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
27343 // clearly the user's own fault
27344 verifyFormat("auto (*p)() = f;");
27345 }
27346
27347 TEST_F(FormatTest, Cpp20ModulesSupport) {
27348 FormatStyle Style = getLLVMStyle();
27349 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
27350 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
27351
27352 verifyFormat("export import foo;", Style);
27353 verifyFormat("export import foo:bar;", Style);
27354 verifyFormat("export import foo.bar;", Style);
27355 verifyFormat("export import foo.bar:baz;", Style);
27356 verifyFormat("export import :bar;", Style);
27357 verifyFormat("export module foo:bar;", Style);
27358 verifyFormat("export module foo;", Style);
27359 verifyFormat("export module foo.bar;", Style);
27360 verifyFormat("export module foo.bar:baz;", Style);
27361 verifyFormat("export import <string_view>;", Style);
27362 verifyFormat("export import <Foo/Bar>;", Style);
27363
27364 verifyFormat("export type_name var;", Style);
27365 verifyFormat("template <class T> export using A = B<T>;", Style);
27366 verifyFormat("export using A = B;", Style);
27367 verifyFormat("export int func() {\n"
27368 " foo();\n"
27369 "}",
27370 Style);
27371 verifyFormat("export struct {\n"
27372 " int foo;\n"
27373 "};",
27374 Style);
27375 verifyFormat("export {\n"
27376 " int foo;\n"
27377 "};",
27378 Style);
27379 verifyFormat("export export char const *hello() { return \"hello\"; }");
27380
27381 verifyFormat("import bar;", Style);
27382 verifyFormat("import foo.bar;", Style);
27383 verifyFormat("import foo:bar;", Style);
27384 verifyFormat("import :bar;", Style);
27385 verifyFormat("import /* module partition */ :bar;", Style);
27386 verifyFormat("import <ctime>;", Style);
27387 verifyFormat("import \"header\";", Style);
27388
27389 verifyFormat("module foo;", Style);
27390 verifyFormat("module foo:bar;", Style);
27391 verifyFormat("module foo.bar;", Style);
27392 verifyFormat("module;", Style);
27393
27394 verifyFormat("export namespace hi {\n"
27395 "const char *sayhi();\n"
27396 "}",
27397 Style);
27398
27399 verifyFormat("module :private;", Style);
27400 verifyFormat("import <foo/bar.h>;", Style);
27401 verifyFormat("import foo...bar;", Style);
27402 verifyFormat("import ..........;", Style);
27403 verifyFormat("module foo:private;", Style);
27404 verifyFormat("import a", Style);
27405 verifyFormat("module a", Style);
27406 verifyFormat("export import a", Style);
27407 verifyFormat("export module a", Style);
27408
27409 verifyFormat("import", Style);
27410 verifyFormat("module", Style);
27411 verifyFormat("export", Style);
27412
27413 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
27414 verifyFormat("_world->import<engine_module>();");
27415 }
27416
27417 TEST_F(FormatTest, CoroutineForCoawait) {
27418 FormatStyle Style = getLLVMStyle();
27419 verifyFormat("for co_await (auto x : range())\n ;");
27420 verifyFormat("for (auto i : arr) {\n"
27421 "}",
27422 Style);
27423 verifyFormat("for co_await (auto i : arr) {\n"
27424 "}",
27425 Style);
27426 verifyFormat("for co_await (auto i : foo(T{})) {\n"
27427 "}",
27428 Style);
27429 }
27430
27431 TEST_F(FormatTest, CoroutineCoAwait) {
27432 verifyFormat("int x = co_await foo();");
27433 verifyFormat("int x = (co_await foo());");
27434 verifyFormat("co_await (42);");
27435 verifyFormat("void operator co_await(int);");
27436 verifyFormat("void operator co_await(a);");
27437 verifyFormat("co_await a;");
27438 verifyFormat("co_await missing_await_resume{};");
27439 verifyFormat("co_await a; // comment");
27440 verifyFormat("void test0() { co_await a; }");
27441 verifyFormat("co_await co_await co_await foo();");
27442 verifyFormat("co_await foo().bar();");
27443 verifyFormat("co_await [this]() -> Task { co_return x; }");
27444 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
27445 "foo(); }(x, y);");
27446
27447 FormatStyle Style = getLLVMStyleWithColumns(40);
27448 verifyFormat("co_await [this](int a, int b) -> Task {\n"
27449 " co_return co_await foo();\n"
27450 "}(x, y);",
27451 Style);
27452 verifyFormat("co_await;");
27453 }
27454
27455 TEST_F(FormatTest, CoroutineCoYield) {
27456 verifyFormat("int x = co_yield foo();");
27457 verifyFormat("int x = (co_yield foo());");
27458 verifyFormat("co_yield (42);");
27459 verifyFormat("co_yield {42};");
27460 verifyFormat("co_yield 42;");
27461 verifyFormat("co_yield n++;");
27462 verifyFormat("co_yield ++n;");
27463 verifyFormat("co_yield;");
27464 }
27465
27466 TEST_F(FormatTest, CoroutineCoReturn) {
27467 verifyFormat("co_return (42);");
27468 verifyFormat("co_return;");
27469 verifyFormat("co_return {};");
27470 verifyFormat("co_return x;");
27471 verifyFormat("co_return co_await foo();");
27472 verifyFormat("co_return co_yield foo();");
27473 }
27474
27475 TEST_F(FormatTest, EmptyShortBlock) {
27476 auto Style = getLLVMStyle();
27477 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
27478
27479 verifyFormat("try {\n"
27480 " doA();\n"
27481 "} catch (Exception &e) {\n"
27482 " e.printStackTrace();\n"
27483 "}",
27484 Style);
27485
27486 verifyFormat("try {\n"
27487 " doA();\n"
27488 "} catch (Exception &e) {}",
27489 Style);
27490 }
27491
27492 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
27493 auto Style = getLLVMStyle();
27494
27495 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
27496 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
27497 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
27498 verifyFormat("struct Y<[] { return 0; }> {};", Style);
27499
27500 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
27501 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
27502 }
27503
27504 TEST_F(FormatTest, MultilineLambdaInConditional) {
27505 auto Style = getLLVMStyleWithColumns(70);
27506 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
27507 " ;\n"
27508 " return 5;\n"
27509 "}()\n"
27510 " : 2;",
27511 Style);
27512 verifyFormat(
27513 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
27514 " ;\n"
27515 " return 5;\n"
27516 "}();",
27517 Style);
27518
27519 Style = getLLVMStyleWithColumns(60);
27520 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
27521 " ? []() {\n"
27522 " ;\n"
27523 " return 5;\n"
27524 " }()\n"
27525 " : 2;",
27526 Style);
27527 verifyFormat("auto aLengthyIdentifier =\n"
27528 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
27529 " ;\n"
27530 " return 5;\n"
27531 " }();",
27532 Style);
27533
27534 Style = getLLVMStyleWithColumns(40);
27535 verifyFormat("auto aLengthyIdentifier =\n"
27536 " oneExpressionSoThatWeBreak ? []() {\n"
27537 " ;\n"
27538 " return 5;\n"
27539 " }()\n"
27540 " : 2;",
27541 Style);
27542 verifyFormat("auto aLengthyIdentifier =\n"
27543 " oneExpressionSoThatWeBreak\n"
27544 " ? 2\n"
27545 " : []() {\n"
27546 " ;\n"
27547 " return 5;\n"
27548 " };",
27549 Style);
27550 }
27551
27552 TEST_F(FormatTest, UnderstandsDigraphs) {
27553 verifyFormat("int arr<:5:> = {};");
27554 verifyFormat("int arr[5] = <%%>;");
27555 verifyFormat("int arr<:::qualified_variable:> = {};");
27556 verifyFormat("int arr[::qualified_variable] = <%%>;");
27557 verifyFormat("%:include <header>");
27558 verifyFormat("%:define A x##y");
27559 verifyFormat("#define A x%:%:y");
27560 }
27561
27562 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
27563 auto Style = getLLVMStyle();
27564 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27565 Style.AlignConsecutiveAssignments.Enabled = true;
27566 Style.AlignConsecutiveDeclarations.Enabled = true;
27567
27568 // The AlignArray code is incorrect for non square Arrays and can cause
27569 // crashes, these tests assert that the array is not changed but will
27570 // also act as regression tests for when it is properly fixed
27571 verifyFormat("struct test demo[] = {\n"
27572 " {1, 2},\n"
27573 " {3, 4, 5},\n"
27574 " {6, 7, 8}\n"
27575 "};",
27576 Style);
27577 verifyFormat("struct test demo[] = {\n"
27578 " {1, 2, 3, 4, 5},\n"
27579 " {3, 4, 5},\n"
27580 " {6, 7, 8}\n"
27581 "};",
27582 Style);
27583 verifyFormat("struct test demo[] = {\n"
27584 " {1, 2, 3, 4, 5},\n"
27585 " {3, 4, 5},\n"
27586 " {6, 7, 8, 9, 10, 11, 12}\n"
27587 "};",
27588 Style);
27589 verifyFormat("struct test demo[] = {\n"
27590 " {1, 2, 3},\n"
27591 " {3, 4, 5},\n"
27592 " {6, 7, 8, 9, 10, 11, 12}\n"
27593 "};",
27594 Style);
27595
27596 verifyFormat("S{\n"
27597 " {},\n"
27598 " {},\n"
27599 " {a, b}\n"
27600 "};",
27601 Style);
27602 verifyFormat("S{\n"
27603 " {},\n"
27604 " {},\n"
27605 " {a, b},\n"
27606 "};",
27607 Style);
27608 verifyFormat("void foo() {\n"
27609 " auto thing = test{\n"
27610 " {\n"
27611 " {13},\n"
27612 " {something}, // A\n"
27613 " }\n"
27614 " };\n"
27615 "}",
27616 Style);
27617 }
27618
27619 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
27620 auto Style = getLLVMStyle();
27621 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
27622 Style.AlignConsecutiveAssignments.Enabled = true;
27623 Style.AlignConsecutiveDeclarations.Enabled = true;
27624
27625 // The AlignArray code is incorrect for non square Arrays and can cause
27626 // crashes, these tests assert that the array is not changed but will
27627 // also act as regression tests for when it is properly fixed
27628 verifyFormat("struct test demo[] = {\n"
27629 " {1, 2},\n"
27630 " {3, 4, 5},\n"
27631 " {6, 7, 8}\n"
27632 "};",
27633 Style);
27634 verifyFormat("struct test demo[] = {\n"
27635 " {1, 2, 3, 4, 5},\n"
27636 " {3, 4, 5},\n"
27637 " {6, 7, 8}\n"
27638 "};",
27639 Style);
27640 verifyFormat("struct test demo[] = {\n"
27641 " {1, 2, 3, 4, 5},\n"
27642 " {3, 4, 5},\n"
27643 " {6, 7, 8, 9, 10, 11, 12}\n"
27644 "};",
27645 Style);
27646 verifyFormat("struct test demo[] = {\n"
27647 " {1, 2, 3},\n"
27648 " {3, 4, 5},\n"
27649 " {6, 7, 8, 9, 10, 11, 12}\n"
27650 "};",
27651 Style);
27652
27653 verifyFormat("S{\n"
27654 " {},\n"
27655 " {},\n"
27656 " {a, b}\n"
27657 "};",
27658 Style);
27659 verifyFormat("S{\n"
27660 " {},\n"
27661 " {},\n"
27662 " {a, b},\n"
27663 "};",
27664 Style);
27665 verifyFormat("void foo() {\n"
27666 " auto thing = test{\n"
27667 " {\n"
27668 " {13},\n"
27669 " {something}, // A\n"
27670 " }\n"
27671 " };\n"
27672 "}",
27673 Style);
27674 }
27675
27676 TEST_F(FormatTest, FormatsVariableTemplates) {
27677 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27678 verifyFormat("template <typename T> "
27679 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27680 }
27681
27682 TEST_F(FormatTest, RemoveSemicolon) {
27683 FormatStyle Style = getLLVMStyle();
27684 Style.RemoveSemicolon = true;
27685
27686 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27687 "int max(int a, int b) { return a > b ? a : b; };", Style);
27688
27689 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27690 "int max(int a, int b) { return a > b ? a : b; };;", Style);
27691
27692 verifyFormat("class Foo {\n"
27693 " int getSomething() const { return something; }\n"
27694 "};",
27695 "class Foo {\n"
27696 " int getSomething() const { return something; };\n"
27697 "};",
27698 Style);
27699
27700 verifyFormat("class Foo {\n"
27701 " int getSomething() const { return something; }\n"
27702 "};",
27703 "class Foo {\n"
27704 " int getSomething() const { return something; };;\n"
27705 "};",
27706 Style);
27707
27708 verifyFormat("for (;;) {\n"
27709 "}",
27710 Style);
27711
27712 verifyFormat("class [[deprecated(\"\")]] C {\n"
27713 " int i;\n"
27714 "};",
27715 Style);
27716
27717 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27718 " int i;\n"
27719 "};",
27720 Style);
27721
27722 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
27723
27724 verifyFormat("void main() {}", "void main() {};", Style);
27725
27726 verifyFormat("struct Foo {\n"
27727 " Foo() {}\n"
27728 " ~Foo() {}\n"
27729 "};",
27730 "struct Foo {\n"
27731 " Foo() {};\n"
27732 " ~Foo() {};\n"
27733 "};",
27734 Style);
27735
27736 // We can't (and probably shouldn't) support the following.
27737 #if 0
27738 verifyFormat("void foo() {} //\n"
27739 "int bar;",
27740 "void foo() {}; //\n"
27741 "; int bar;",
27742 Style);
27743 #endif
27744
27745 verifyFormat("auto sgf = [] {\n"
27746 " ogl = {\n"
27747 " a, b, c, d, e,\n"
27748 " };\n"
27749 "};",
27750 Style);
27751
27752 Style.TypenameMacros.push_back("STRUCT");
27753 verifyFormat("STRUCT(T, B) { int i; };", Style);
27754 }
27755
27756 TEST_F(FormatTest, EnumTrailingComma) {
27757 constexpr StringRef Code("enum : int { /**/ };\n"
27758 "enum {\n"
27759 " a,\n"
27760 " b,\n"
27761 " c, //\n"
27762 "};\n"
27763 "enum Color { red, green, blue /**/ };");
27764 verifyFormat(Code);
27765
27766 auto Style = getLLVMStyle();
27767 Style.EnumTrailingComma = FormatStyle::ETC_Insert;
27768 verifyFormat("enum : int { /**/ };\n"
27769 "enum {\n"
27770 " a,\n"
27771 " b,\n"
27772 " c, //\n"
27773 "};\n"
27774 "enum Color { red, green, blue, /**/ };",
27775 Code, Style);
27776
27777 Style.EnumTrailingComma = FormatStyle::ETC_Remove;
27778 verifyFormat("enum : int { /**/ };\n"
27779 "enum {\n"
27780 " a,\n"
27781 " b,\n"
27782 " c //\n"
27783 "};\n"
27784 "enum Color { red, green, blue /**/ };",
27785 Code, Style);
27786
27787 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
27788 Style.AllowShortEnumsOnASingleLine = false;
27789
27790 constexpr StringRef Input("enum {\n"
27791 " //\n"
27792 " a,\n"
27793 " /**/\n"
27794 " b,\n"
27795 "};");
27796 verifyFormat(Input, Input, Style, {tooling::Range(12, 3)}); // line 3
27797 verifyFormat("enum {\n"
27798 " //\n"
27799 " a,\n"
27800 " /**/\n"
27801 " b\n"
27802 "};",
27803 Input, Style, {tooling::Range(24, 3)}); // line 5
27804
27805 Style.EnumTrailingComma = FormatStyle::ETC_Insert;
27806 verifyFormat("enum class MyEnum_E {\n"
27807 " MY_ENUM = 0U,\n"
27808 "};",
27809 "enum class MyEnum_E {\n"
27810 " MY_ENUM = 0U\n"
27811 "};",
27812 Style);
27813 }
27814
27815 TEST_F(FormatTest, BreakAfterAttributes) {
27816 constexpr StringRef Code("[[maybe_unused]] const int i;\n"
27817 "[[foo([[]])]] [[maybe_unused]]\n"
27818 "int j;\n"
27819 "[[maybe_unused]]\n"
27820 "foo<int> k;\n"
27821 "[[nodiscard]] inline int f(int &i);\n"
27822 "[[foo([[]])]] [[nodiscard]]\n"
27823 "int g(int &i);\n"
27824 "[[nodiscard]]\n"
27825 "inline int f(int &i) {\n"
27826 " i = 1;\n"
27827 " return 0;\n"
27828 "}\n"
27829 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27830 " i = 0;\n"
27831 " return 1;\n"
27832 "}");
27833
27834 FormatStyle Style = getLLVMStyle();
27835 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
27836 verifyNoChange(Code, Style);
27837
27838 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27839 verifyFormat("[[maybe_unused]] const int i;\n"
27840 "[[foo([[]])]] [[maybe_unused]] int j;\n"
27841 "[[maybe_unused]] foo<int> k;\n"
27842 "[[nodiscard]] inline int f(int &i);\n"
27843 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27844 "[[nodiscard]] inline int f(int &i) {\n"
27845 " i = 1;\n"
27846 " return 0;\n"
27847 "}\n"
27848 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27849 " i = 0;\n"
27850 " return 1;\n"
27851 "}",
27852 Code, Style);
27853
27854 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27855 verifyFormat("[[maybe_unused]]\n"
27856 "const int i;\n"
27857 "[[foo([[]])]] [[maybe_unused]]\n"
27858 "int j;\n"
27859 "[[maybe_unused]]\n"
27860 "foo<int> k;\n"
27861 "[[nodiscard]]\n"
27862 "inline int f(int &i);\n"
27863 "[[foo([[]])]] [[nodiscard]]\n"
27864 "int g(int &i);\n"
27865 "[[nodiscard]]\n"
27866 "inline int f(int &i) {\n"
27867 " i = 1;\n"
27868 " return 0;\n"
27869 "}\n"
27870 "[[foo([[]])]] [[nodiscard]]\n"
27871 "int g(int &i) {\n"
27872 " i = 0;\n"
27873 " return 1;\n"
27874 "}",
27875 Code, Style);
27876
27877 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
27878 " f();\n"
27879 "else\n"
27880 " g();\n"
27881 "[[foo([[]])]]\n"
27882 "switch (b) {\n"
27883 "[[unlikely]] case 1:\n"
27884 " ++b;\n"
27885 " break;\n"
27886 "[[likely]]\n"
27887 "default:\n"
27888 " return;\n"
27889 "}\n"
27890 "[[unlikely]] for (; c > 0; --c)\n"
27891 " h();\n"
27892 "[[likely]]\n"
27893 "while (d > 0)\n"
27894 " --d;");
27895
27896 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
27897 verifyNoChange(CtrlStmtCode, Style);
27898
27899 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27900 verifyFormat("[[likely]] if (a)\n"
27901 " f();\n"
27902 "else\n"
27903 " g();\n"
27904 "[[foo([[]])]] switch (b) {\n"
27905 "[[unlikely]] case 1:\n"
27906 " ++b;\n"
27907 " break;\n"
27908 "[[likely]] default:\n"
27909 " return;\n"
27910 "}\n"
27911 "[[unlikely]] for (; c > 0; --c)\n"
27912 " h();\n"
27913 "[[likely]] while (d > 0)\n"
27914 " --d;",
27915 CtrlStmtCode, Style);
27916
27917 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27918 verifyFormat("[[likely]]\n"
27919 "if (a)\n"
27920 " f();\n"
27921 "else\n"
27922 " g();\n"
27923 "[[foo([[]])]]\n"
27924 "switch (b) {\n"
27925 "[[unlikely]]\n"
27926 "case 1:\n"
27927 " ++b;\n"
27928 " break;\n"
27929 "[[likely]]\n"
27930 "default:\n"
27931 " return;\n"
27932 "}\n"
27933 "[[unlikely]]\n"
27934 "for (; c > 0; --c)\n"
27935 " h();\n"
27936 "[[likely]]\n"
27937 "while (d > 0)\n"
27938 " --d;",
27939 CtrlStmtCode, Style);
27940
27941 verifyFormat("[[nodiscard]]\n"
27942 "operator bool();\n"
27943 "[[nodiscard]]\n"
27944 "operator bool() {\n"
27945 " return true;\n"
27946 "}",
27947 "[[nodiscard]] operator bool();\n"
27948 "[[nodiscard]] operator bool() { return true; }",
27949 Style);
27950
27951 constexpr StringRef CtorDtorCode("struct Foo {\n"
27952 " [[deprecated]] Foo();\n"
27953 " [[deprecated]] Foo() {}\n"
27954 " [[deprecated]] ~Foo();\n"
27955 " [[deprecated]] ~Foo() {}\n"
27956 " [[deprecated]] void f();\n"
27957 " [[deprecated]] void f() {}\n"
27958 "};\n"
27959 "[[deprecated]] Bar::Bar() {}\n"
27960 "[[deprecated]] Bar::~Bar() {}\n"
27961 "[[deprecated]] void g() {}");
27962 verifyFormat("struct Foo {\n"
27963 " [[deprecated]]\n"
27964 " Foo();\n"
27965 " [[deprecated]]\n"
27966 " Foo() {}\n"
27967 " [[deprecated]]\n"
27968 " ~Foo();\n"
27969 " [[deprecated]]\n"
27970 " ~Foo() {}\n"
27971 " [[deprecated]]\n"
27972 " void f();\n"
27973 " [[deprecated]]\n"
27974 " void f() {}\n"
27975 "};\n"
27976 "[[deprecated]]\n"
27977 "Bar::Bar() {}\n"
27978 "[[deprecated]]\n"
27979 "Bar::~Bar() {}\n"
27980 "[[deprecated]]\n"
27981 "void g() {}",
27982 CtorDtorCode, Style);
27983
27984 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
27985 verifyFormat("struct Foo {\n"
27986 " [[deprecated]]\n"
27987 " Foo();\n"
27988 " [[deprecated]]\n"
27989 " Foo()\n"
27990 " {\n"
27991 " }\n"
27992 " [[deprecated]]\n"
27993 " ~Foo();\n"
27994 " [[deprecated]]\n"
27995 " ~Foo()\n"
27996 " {\n"
27997 " }\n"
27998 " [[deprecated]]\n"
27999 " void f();\n"
28000 " [[deprecated]]\n"
28001 " void f()\n"
28002 " {\n"
28003 " }\n"
28004 "};\n"
28005 "[[deprecated]]\n"
28006 "Bar::Bar()\n"
28007 "{\n"
28008 "}\n"
28009 "[[deprecated]]\n"
28010 "Bar::~Bar()\n"
28011 "{\n"
28012 "}\n"
28013 "[[deprecated]]\n"
28014 "void g()\n"
28015 "{\n"
28016 "}",
28017 CtorDtorCode, Style);
28018
28019 verifyFormat("struct Foo {\n"
28020 " [[maybe_unused]]\n"
28021 " void operator+();\n"
28022 "};\n"
28023 "[[nodiscard]]\n"
28024 "Foo &operator-(Foo &);",
28025 Style);
28026
28027 Style.ReferenceAlignment = FormatStyle::RAS_Left;
28028 verifyFormat("[[nodiscard]]\n"
28029 "Foo& operator-(Foo&);",
28030 Style);
28031
28032 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
28033 verifyFormat("[[deprecated]]\n"
28034 "void f() = delete;",
28035 Style);
28036 }
28037
28038 TEST_F(FormatTest, InsertNewlineAtEOF) {
28039 FormatStyle Style = getLLVMStyle();
28040 Style.InsertNewlineAtEOF = true;
28041
28042 verifyNoChange("int i;\n", Style);
28043 verifyFormat("int i;\n", "int i;", Style);
28044
28045 constexpr StringRef Code("namespace {\n"
28046 "int i;\n"
28047 "} // namespace");
28048 verifyFormat(Code.str() + '\n', Code, Style,
28049 {tooling::Range(19, 13)}); // line 3
28050 }
28051
28052 TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
28053 FormatStyle Style = getLLVMStyle();
28054 Style.KeepEmptyLines.AtEndOfFile = true;
28055
28056 constexpr StringRef Code("int i;\n\n");
28057 verifyNoChange(Code, Style);
28058 verifyFormat(Code, "int i;\n\n\n", Style);
28059 }
28060
28061 TEST_F(FormatTest, SpaceAfterUDL) {
28062 verifyFormat("auto c = (4s).count();");
28063 verifyFormat("auto x = 5s .count() == 5;");
28064 }
28065
28066 TEST_F(FormatTest, InterfaceAsClassMemberName) {
28067 verifyFormat("class Foo {\n"
28068 " int interface;\n"
28069 " Foo::Foo(int iface) : interface{iface} {}\n"
28070 "}");
28071 }
28072
28073 TEST_F(FormatTest, PreprocessorOverlappingRegions) {
28074 verifyFormat("#ifdef\n\n"
28075 "#else\n"
28076 "#endif",
28077 "#ifdef \n"
28078 " \n"
28079 "\n"
28080 "#else \n"
28081 "#endif ",
28082 getGoogleStyle());
28083 }
28084
28085 TEST_F(FormatTest, RemoveParentheses) {
28086 FormatStyle Style = getLLVMStyle();
28087 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
28088
28089 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
28090 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
28091 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
28092 verifyFormat("decltype((foo->bar)) baz;", Style);
28093 verifyFormat("class __declspec(dllimport) X {};",
28094 "class __declspec((dllimport)) X {};", Style);
28095 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
28096 verifyFormat("while (a)\n"
28097 " b;",
28098 "while (((a)))\n"
28099 " b;",
28100 Style);
28101 verifyFormat("while ((a = b))\n"
28102 " c;",
28103 "while (((a = b)))\n"
28104 " c;",
28105 Style);
28106 verifyFormat("if (a)\n"
28107 " b;",
28108 "if (((a)))\n"
28109 " b;",
28110 Style);
28111 verifyFormat("if constexpr ((a = b))\n"
28112 " c;",
28113 "if constexpr (((a = b)))\n"
28114 " c;",
28115 Style);
28116 verifyFormat("if (({ a; }))\n"
28117 " b;",
28118 "if ((({ a; })))\n"
28119 " b;",
28120 Style);
28121 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
28122 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
28123 Style);
28124 verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
28125 verifyFormat("foo((a, b));", "foo(((a), b));", Style);
28126 verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
28127 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
28128 verifyFormat("(..., (hash_a = hash_combine(hash_a, hash_b)));",
28129 "(..., ((hash_a = hash_combine(hash_a, hash_b))));", Style);
28130 verifyFormat("((hash_a = hash_combine(hash_a, hash_b)), ...);",
28131 "(((hash_a = hash_combine(hash_a, hash_b))), ...);", Style);
28132 verifyFormat("return (0);", "return (((0)));", Style);
28133 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
28134 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
28135 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
28136 Style);
28137 verifyFormat("MOCK_METHOD(void, Function, (), override);",
28138 "MOCK_METHOD(void, Function, (), (override));", Style);
28139
28140 Style.MacrosSkippedByRemoveParentheses.push_back("FOO");
28141 verifyFormat("FOO((a && b));", Style);
28142 verifyFormat("FOO((int), func, ((std::map<int, int>)), (override));", Style);
28143
28144 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
28145 verifyFormat("#define Return0 return (0);", Style);
28146 verifyFormat("return 0;", "return (0);", Style);
28147 verifyFormat("co_return 0;", "co_return ((0));", Style);
28148 verifyFormat("return 0;", "return (((0)));", Style);
28149 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
28150 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
28151 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
28152 Style);
28153 verifyFormat("inline decltype(auto) f() {\n"
28154 " if (a) {\n"
28155 " return (a);\n"
28156 " }\n"
28157 " return (b);\n"
28158 "}",
28159 "inline decltype(auto) f() {\n"
28160 " if (a) {\n"
28161 " return ((a));\n"
28162 " }\n"
28163 " return ((b));\n"
28164 "}",
28165 Style);
28166 verifyFormat("auto g() {\n"
28167 " decltype(auto) x = [] {\n"
28168 " auto y = [] {\n"
28169 " if (a) {\n"
28170 " return a;\n"
28171 " }\n"
28172 " return b;\n"
28173 " };\n"
28174 " if (c) {\n"
28175 " return (c);\n"
28176 " }\n"
28177 " return (d);\n"
28178 " };\n"
28179 " if (e) {\n"
28180 " return e;\n"
28181 " }\n"
28182 " return f;\n"
28183 "}",
28184 "auto g() {\n"
28185 " decltype(auto) x = [] {\n"
28186 " auto y = [] {\n"
28187 " if (a) {\n"
28188 " return ((a));\n"
28189 " }\n"
28190 " return ((b));\n"
28191 " };\n"
28192 " if (c) {\n"
28193 " return ((c));\n"
28194 " }\n"
28195 " return ((d));\n"
28196 " };\n"
28197 " if (e) {\n"
28198 " return ((e));\n"
28199 " }\n"
28200 " return ((f));\n"
28201 "}",
28202 Style);
28203
28204 Style.ColumnLimit = 25;
28205 verifyFormat("return (a + b) - (c + d);",
28206 "return (((a + b)) -\n"
28207 " ((c + d)));",
28208 Style);
28209 }
28210
28211 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
28212 auto Style = getLLVMStyleWithColumns(35);
28213
28214 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
28215 verifyFormat("void foo(int arg1,\n"
28216 " double arg2) noexcept;",
28217 Style);
28218
28219 // The following line does not fit within the 35 column limit, but that's what
28220 // happens with no break allowed.
28221 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
28222 " noexcept(baz(arg1)) &&\n"
28223 " noexcept(baz(arg2)));",
28224 Style);
28225
28226 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
28227 Style);
28228
28229 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
28230 verifyFormat("void foo(int arg1,\n"
28231 " double arg2) noexcept;",
28232 Style);
28233
28234 verifyFormat("void bar(int arg1, double arg2)\n"
28235 " noexcept(noexcept(baz(arg1)) &&\n"
28236 " noexcept(baz(arg2)));",
28237 Style);
28238
28239 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
28240 " noexcept;",
28241 Style);
28242
28243 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
28244 verifyFormat("void foo(int arg1,\n"
28245 " double arg2) noexcept;",
28246 Style);
28247
28248 verifyFormat("void bar(int arg1, double arg2)\n"
28249 " noexcept(noexcept(baz(arg1)) &&\n"
28250 " noexcept(baz(arg2)));",
28251 Style);
28252
28253 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
28254 Style);
28255 }
28256
28257 TEST_F(FormatTest, PPBranchesInBracedInit) {
28258 verifyFormat("A a_{kFlag1,\n"
28259 "#if BUILD_FLAG\n"
28260 " kFlag2,\n"
28261 "#else\n"
28262 " kFlag3,\n"
28263 "#endif\n"
28264 " kFlag4};",
28265 "A a_{\n"
28266 " kFlag1,\n"
28267 "#if BUILD_FLAG\n"
28268 " kFlag2,\n"
28269 "#else\n"
28270 " kFlag3,\n"
28271 "#endif\n"
28272 " kFlag4\n"
28273 "};");
28274 }
28275
28276 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
28277 verifyFormat("{\n"
28278 " char *a[] = {\n"
28279 " /* abc */ \"abc\",\n"
28280 "#if FOO\n"
28281 " /* xyz */ \"xyz\",\n"
28282 "#endif\n"
28283 " /* last */ \"last\"};\n"
28284 "}",
28285 getLLVMStyleWithColumns(30));
28286 }
28287
28288 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
28289 constexpr StringRef Code(
28290 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";");
28291
28292 verifyFormat("return \"Code\"\n"
28293 " \"\\0\\52\\26\\55\\55\\0\"\n"
28294 " \"x013\"\n"
28295 " \"\\02\\xBA\";",
28296 Code);
28297
28298 auto Style = getLLVMStyle();
28299 Style.BreakAdjacentStringLiterals = false;
28300 verifyFormat(Code, Style);
28301 }
28302
28303 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
28304 verifyFormat(
28305 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
28306 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
28307 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
28308
28309 auto Style = getLLVMStyle();
28310 Style.ColumnLimit = 15;
28311 verifyNoChange("#define test \\\n"
28312 " /* 测试 */ \\\n"
28313 " \"aa\" \\\n"
28314 " \"bb\"",
28315 Style);
28316
28317 Style.ColumnLimit = 25;
28318 verifyFormat("struct foo {\n"
28319 " int iiiiii; ///< iiiiii\n"
28320 " int b; ///< ыыы\n"
28321 " int c; ///< ыыыы\n"
28322 "};",
28323 Style);
28324
28325 Style.ColumnLimit = 35;
28326 verifyFormat("#define SENSOR_DESC_1 \\\n"
28327 " \"{\" \\\n"
28328 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
28329 " \"}\"",
28330 Style);
28331
28332 Style.ColumnLimit = 80;
28333 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
28334 verifyFormat("Languages languages = {\n"
28335 " Language{{'e', 'n'}, U\"Test English\" },\n"
28336 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
28337 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
28338 "};",
28339 Style);
28340 }
28341
28342 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
28343 verifyFormat("return .5;");
28344 verifyFormat("return not '5';");
28345 verifyFormat("return sizeof \"5\";");
28346 }
28347
28348 TEST_F(FormatTest, BreakBinaryOperations) {
28349 auto Style = getLLVMStyleWithColumns(60);
28350 EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never);
28351
28352 // Logical operations
28353 verifyFormat("if (condition1 && condition2) {\n"
28354 "}",
28355 Style);
28356
28357 verifyFormat("if (condition1 && condition2 &&\n"
28358 " (condition3 || condition4) && condition5 &&\n"
28359 " condition6) {\n"
28360 "}",
28361 Style);
28362
28363 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
28364 " loooooooooooooooooooooongcondition2) {\n"
28365 "}",
28366 Style);
28367
28368 // Arithmetic
28369 verifyFormat("const int result = lhs + rhs;", Style);
28370
28371 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
28372 " loooooooooooooooooooooongop3;",
28373 Style);
28374
28375 verifyFormat("result = longOperand1 + longOperand2 -\n"
28376 " (longOperand3 + longOperand4) -\n"
28377 " longOperand5 * longOperand6;",
28378 Style);
28379
28380 verifyFormat("const int result =\n"
28381 " operand1 + operand2 - (operand3 + operand4);",
28382 Style);
28383
28384 // Check operator>> special case.
28385 verifyFormat("std::cin >> longOperand_1 >> longOperand_2 >>\n"
28386 " longOperand_3_;",
28387 Style);
28388
28389 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28390
28391 // Logical operations
28392 verifyFormat("if (condition1 && condition2) {\n"
28393 "}",
28394 Style);
28395
28396 verifyFormat("if (condition1 && // comment\n"
28397 " condition2 &&\n"
28398 " (condition3 || condition4) && // comment\n"
28399 " condition5 &&\n"
28400 " condition6) {\n"
28401 "}",
28402 Style);
28403
28404 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
28405 " loooooooooooooooooooooongcondition2) {\n"
28406 "}",
28407 Style);
28408
28409 // Arithmetic
28410 verifyFormat("const int result = lhs + rhs;", Style);
28411
28412 verifyFormat("result = loooooooooooooooooooooongop1 +\n"
28413 " loooooooooooooooooooooongop2 +\n"
28414 " loooooooooooooooooooooongop3;",
28415 Style);
28416
28417 verifyFormat("const int result =\n"
28418 " operand1 + operand2 - (operand3 + operand4);",
28419 Style);
28420
28421 verifyFormat("result = longOperand1 +\n"
28422 " longOperand2 -\n"
28423 " (longOperand3 + longOperand4) -\n"
28424 " longOperand5 +\n"
28425 " longOperand6;",
28426 Style);
28427
28428 verifyFormat("result = operand1 +\n"
28429 " operand2 -\n"
28430 " operand3 +\n"
28431 " operand4 -\n"
28432 " operand5 +\n"
28433 " operand6;",
28434 Style);
28435
28436 // Ensure mixed precedence operations are handled properly
28437 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28438
28439 verifyFormat("result = operand1 +\n"
28440 " operand2 /\n"
28441 " operand3 +\n"
28442 " operand4 /\n"
28443 " operand5 *\n"
28444 " operand6;",
28445 Style);
28446
28447 verifyFormat("result = operand1 *\n"
28448 " operand2 -\n"
28449 " operand3 *\n"
28450 " operand4 -\n"
28451 " operand5 +\n"
28452 " operand6;",
28453 Style);
28454
28455 verifyFormat("result = operand1 *\n"
28456 " (operand2 - operand3 * operand4) -\n"
28457 " operand5 +\n"
28458 " operand6;",
28459 Style);
28460
28461 verifyFormat("result = operand1.member *\n"
28462 " (operand2.member() - operand3->mem * operand4) -\n"
28463 " operand5.member() +\n"
28464 " operand6->member;",
28465 Style);
28466
28467 // Check operator>> special case.
28468 verifyFormat("std::cin >>\n"
28469 " longOperand_1 >>\n"
28470 " longOperand_2 >>\n"
28471 " longOperand_3_;",
28472 Style);
28473
28474 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28475 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28476
28477 verifyFormat("result = operand1 +\n"
28478 " operand2 / operand3 +\n"
28479 " operand4 / operand5 * operand6;",
28480 Style);
28481
28482 verifyFormat("result = operand1 * operand2 -\n"
28483 " operand3 * operand4 -\n"
28484 " operand5 +\n"
28485 " operand6;",
28486 Style);
28487
28488 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28489 " operand5 +\n"
28490 " operand6;",
28491 Style);
28492
28493 verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28494 " byte_buffer[1] << 8 |\n"
28495 " byte_buffer[2] << 16 |\n"
28496 " byte_buffer[3] << 24;",
28497 Style);
28498
28499 // Check operator>> special case.
28500 verifyFormat("std::cin >>\n"
28501 " longOperand_1 >>\n"
28502 " longOperand_2 >>\n"
28503 " longOperand_3_;",
28504 Style);
28505
28506 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28507 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
28508
28509 // Logical operations
28510 verifyFormat("if (condition1 && condition2) {\n"
28511 "}",
28512 Style);
28513
28514 verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28515 " && loooooooooooooooooooooongcondition2) {\n"
28516 "}",
28517 Style);
28518
28519 // Arithmetic
28520 verifyFormat("const int result = lhs + rhs;", Style);
28521
28522 verifyFormat("result = loooooooooooooooooooooongop1\n"
28523 " + loooooooooooooooooooooongop2\n"
28524 " + loooooooooooooooooooooongop3;",
28525 Style);
28526
28527 verifyFormat("const int result =\n"
28528 " operand1 + operand2 - (operand3 + operand4);",
28529 Style);
28530
28531 verifyFormat("result = longOperand1\n"
28532 " + longOperand2\n"
28533 " - (longOperand3 + longOperand4)\n"
28534 " - longOperand5\n"
28535 " + longOperand6;",
28536 Style);
28537
28538 verifyFormat("result = operand1\n"
28539 " + operand2\n"
28540 " - operand3\n"
28541 " + operand4\n"
28542 " - operand5\n"
28543 " + operand6;",
28544 Style);
28545
28546 // Ensure mixed precedence operations are handled properly
28547 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28548
28549 verifyFormat("result = operand1\n"
28550 " + operand2\n"
28551 " / operand3\n"
28552 " + operand4\n"
28553 " / operand5\n"
28554 " * operand6;",
28555 Style);
28556
28557 verifyFormat("result = operand1\n"
28558 " * operand2\n"
28559 " - operand3\n"
28560 " * operand4\n"
28561 " - operand5\n"
28562 " + operand6;",
28563 Style);
28564
28565 verifyFormat("result = operand1\n"
28566 " * (operand2 - operand3 * operand4)\n"
28567 " - operand5\n"
28568 " + operand6;",
28569 Style);
28570
28571 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28572 " | byte_buffer[1]\n"
28573 " << 8\n"
28574 " | byte_buffer[2]\n"
28575 " << 16\n"
28576 " | byte_buffer[3]\n"
28577 " << 24;",
28578 Style);
28579
28580 // Check operator>> special case.
28581 verifyFormat("std::cin\n"
28582 " >> longOperand_1\n"
28583 " >> longOperand_2\n"
28584 " >> longOperand_3_;",
28585 Style);
28586
28587 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28588 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28589
28590 verifyFormat("result = operand1\n"
28591 " + operand2 / operand3\n"
28592 " + operand4 / operand5 * operand6;",
28593 Style);
28594
28595 verifyFormat("result = operand1 * operand2\n"
28596 " - operand3 * operand4\n"
28597 " - operand5\n"
28598 " + operand6;",
28599 Style);
28600
28601 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
28602 " - operand5\n"
28603 " + operand6;",
28604 Style);
28605
28606 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28607 " | byte_buffer[1] << 8\n"
28608 " | byte_buffer[2] << 16\n"
28609 " | byte_buffer[3] << 24;",
28610 Style);
28611
28612 // Check operator>> special case.
28613 verifyFormat("std::cin\n"
28614 " >> longOperand_1\n"
28615 " >> longOperand_2\n"
28616 " >> longOperand_3_;",
28617 Style);
28618 }
28619
28620 TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {
28621 auto Style = getLLVMStyle();
28622 Style.RemoveEmptyLinesInUnwrappedLines = true;
28623
28624 verifyFormat("int c = a + b;",
28625 "int c\n"
28626 "\n"
28627 " = a + b;",
28628 Style);
28629
28630 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
28631 "enum : unsigned\n"
28632 "\n"
28633 "{\n"
28634 " AA = 0,\n"
28635 " BB\n"
28636 "} myEnum;",
28637 Style);
28638
28639 verifyFormat("class B : public E {\n"
28640 "private:\n"
28641 "};",
28642 "class B : public E\n"
28643 "\n"
28644 "{\n"
28645 "private:\n"
28646 "};",
28647 Style);
28648
28649 verifyFormat(
28650 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
28651 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
28652 "\n"
28653 " 23, \"hello\"},\n"
28654 " {7, 5, \"!!\"}};",
28655 Style);
28656
28657 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
28658 "int myFunction(\n"
28659 "\n"
28660 " int aaaaaaaaaaaaa,\n"
28661 "\n"
28662 " int ccccccccccccc, int d);",
28663 Style);
28664
28665 verifyFormat("switch (e) {\n"
28666 "case 1:\n"
28667 " return e;\n"
28668 "case 2:\n"
28669 " return 2;\n"
28670 "}",
28671 "switch (\n"
28672 "\n"
28673 " e) {\n"
28674 "case 1:\n"
28675 " return e;\n"
28676 "case 2:\n"
28677 " return 2;\n"
28678 "}",
28679 Style);
28680
28681 verifyFormat("while (true) {\n"
28682 "}",
28683 "while (\n"
28684 "\n"
28685 " true) {\n"
28686 "}",
28687 Style);
28688
28689 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
28690 " std::map<int, std::string> *outputMap);",
28691 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
28692 "\n"
28693 " (std::map<int, std::string> *outputMap);",
28694 Style);
28695 }
28696
28697 TEST_F(FormatTest, KeepFormFeed) {
28698 auto Style = getLLVMStyle();
28699 Style.KeepFormFeed = true;
28700
28701 constexpr StringRef NoFormFeed("int i;\n"
28702 "\n"
28703 "void f();");
28704 verifyFormat(NoFormFeed,
28705 "int i;\n"
28706 " \f\n"
28707 "void f();",
28708 Style);
28709 verifyFormat(NoFormFeed,
28710 "int i;\n"
28711 "\n"
28712 "\fvoid f();",
28713 Style);
28714 verifyFormat(NoFormFeed,
28715 "\fint i;\n"
28716 "\n"
28717 "void f();",
28718 Style);
28719 verifyFormat(NoFormFeed,
28720 "int i;\n"
28721 "\n"
28722 "void f();\f",
28723 Style);
28724
28725 constexpr StringRef FormFeed("int i;\n"
28726 "\f\n"
28727 "void f();");
28728 verifyNoChange(FormFeed, Style);
28729
28730 Style.LineEnding = FormatStyle::LE_LF;
28731 verifyFormat(FormFeed,
28732 "int i;\r\n"
28733 "\f\r\n"
28734 "void f();",
28735 Style);
28736
28737 constexpr StringRef FormFeedBeforeEmptyLine("int i;\n"
28738 "\f\n"
28739 "\n"
28740 "void f();");
28741 Style.MaxEmptyLinesToKeep = 2;
28742 verifyFormat(FormFeedBeforeEmptyLine,
28743 "int i;\n"
28744 "\n"
28745 "\f\n"
28746 "void f();",
28747 Style);
28748 verifyFormat(FormFeedBeforeEmptyLine,
28749 "int i;\n"
28750 "\f\n"
28751 "\f\n"
28752 "void f();",
28753 Style);
28754 }
28755
28756 TEST_F(FormatTest, ShortNamespacesOption) {
28757 auto Style = getLLVMStyle();
28758 Style.AllowShortNamespacesOnASingleLine = true;
28759 Style.CompactNamespaces = true;
28760 Style.FixNamespaceComments = false;
28761
28762 // Basic functionality.
28763 verifyFormat("namespace foo { class bar; }", Style);
28764 verifyFormat("namespace foo::bar { class baz; }", Style);
28765 verifyFormat("namespace { class bar; }", Style);
28766 verifyFormat("namespace foo {\n"
28767 "class bar;\n"
28768 "class baz;\n"
28769 "}",
28770 Style);
28771
28772 // Trailing comments prevent merging.
28773 verifyFormat("namespace foo { namespace baz {\n"
28774 "class qux;\n"
28775 "} // comment\n"
28776 "}",
28777 Style);
28778
28779 // Make sure code doesn't walk too far on unbalanced code.
28780 verifyFormat("namespace foo {", Style);
28781 verifyFormat("namespace foo {\n"
28782 "class baz;",
28783 Style);
28784 verifyFormat("namespace foo {\n"
28785 "namespace bar { class baz; }",
28786 Style);
28787
28788 // Nested namespaces.
28789 verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
28790
28791 // Without CompactNamespaces, we won't merge consecutive namespace
28792 // declarations.
28793 Style.CompactNamespaces = false;
28794 verifyFormat("namespace foo {\n"
28795 "namespace bar { class baz; }\n"
28796 "}",
28797 Style);
28798
28799 verifyFormat("namespace foo {\n"
28800 "namespace bar { class baz; }\n"
28801 "namespace qux { class quux; }\n"
28802 "}",
28803 Style);
28804
28805 Style.CompactNamespaces = true;
28806
28807 // Varying inner content.
28808 verifyFormat("namespace foo {\n"
28809 "int f() { return 5; }\n"
28810 "}",
28811 Style);
28812 verifyFormat("namespace foo { template <T> struct bar; }", Style);
28813 verifyFormat("namespace foo { constexpr int num = 42; }", Style);
28814
28815 // Validate nested namespace wrapping scenarios around the ColumnLimit.
28816 Style.ColumnLimit = 64;
28817
28818 // Validate just under the ColumnLimit.
28819 verifyFormat(
28820 "namespace foo { namespace bar { namespace baz { class qux; } } }",
28821 Style);
28822
28823 // Validate just over the ColumnLimit.
28824 verifyFormat("namespace foo { namespace baar { namespace baaz {\n"
28825 "class quux;\n"
28826 "}}}",
28827 Style);
28828
28829 verifyFormat(
28830 "namespace foo { namespace bar { namespace baz { namespace qux {\n"
28831 "class quux;\n"
28832 "}}}}",
28833 Style);
28834
28835 // Validate that the ColumnLimit logic accounts for trailing content as well.
28836 verifyFormat("namespace foo { namespace bar { class qux; } } // extra",
28837 Style);
28838
28839 verifyFormat("namespace foo { namespace bar { namespace baz {\n"
28840 "class qux;\n"
28841 "}}} // extra",
28842 Style);
28843
28844 // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing
28845 // namespace comment from 'FixNamespaceComments', as it's not really necessary
28846 // in this scenario, but the two options work at very different layers of the
28847 // formatter, so I'm not sure how to make them interact.
28848 //
28849 // As it stands, the trailing comment will be added and likely make the line
28850 // too long to fit within the ColumnLimit, reducing the how likely the line
28851 // will still fit on a single line. The recommendation for now is to use the
28852 // concatenated namespace syntax instead. e.g. 'namespace foo::bar'
28853 Style.FixNamespaceComments = true;
28854 verifyFormat(
28855 "namespace foo { namespace bar { namespace baz {\n"
28856 "class qux;\n"
28857 "}}} // namespace foo::bar::baz",
28858 "namespace foo { namespace bar { namespace baz { class qux; } } }",
28859 Style);
28860 Style.FixNamespaceComments = false;
28861
28862 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
28863 Style.BraceWrapping.AfterNamespace = true;
28864 verifyFormat("namespace foo { class bar; }", Style);
28865 verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
28866 verifyFormat("namespace foo\n"
28867 "{ // comment\n"
28868 "class bar;\n"
28869 "}",
28870 Style);
28871 verifyFormat("namespace foo { class bar; }",
28872 "namespace foo {\n"
28873 "class bar;\n"
28874 "}",
28875 Style);
28876 verifyFormat("namespace foo\n"
28877 "{\n"
28878 "namespace bar\n"
28879 "{ // comment\n"
28880 "class baz;\n"
28881 "}\n"
28882 "}",
28883 Style);
28884 verifyFormat("namespace foo // comment\n"
28885 "{\n"
28886 "class baz;\n"
28887 "}",
28888 Style);
28889 }
28890
28891 TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
28892 auto Style = getLLVMStyle();
28893 Style.FixNamespaceComments = false;
28894 Style.MaxEmptyLinesToKeep = 2;
28895 Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never;
28896
28897 // Empty namespace.
28898 verifyFormat("namespace N {}", Style);
28899
28900 // Single namespace.
28901 verifyFormat("namespace N {\n"
28902 "int f1(int a) { return 2 * a; }\n"
28903 "}",
28904 "namespace N {\n"
28905 "\n"
28906 "\n"
28907 "int f1(int a) { return 2 * a; }\n"
28908 "\n"
28909 "\n"
28910 "}",
28911 Style);
28912
28913 // Nested namespace.
28914 verifyFormat("namespace N1 {\n"
28915 "namespace N2 {\n"
28916 "int a = 1;\n"
28917 "}\n"
28918 "}",
28919 "namespace N1 {\n"
28920 "\n"
28921 "\n"
28922 "namespace N2 {\n"
28923 "\n"
28924 "int a = 1;\n"
28925 "\n"
28926 "}\n"
28927 "\n"
28928 "\n"
28929 "}",
28930 Style);
28931
28932 Style.CompactNamespaces = true;
28933
28934 verifyFormat("namespace N1 { namespace N2 {\n"
28935 "int a = 1;\n"
28936 "}}",
28937 "namespace N1 { namespace N2 {\n"
28938 "\n"
28939 "\n"
28940 "int a = 1;\n"
28941 "\n"
28942 "\n"
28943 "}}",
28944 Style);
28945 }
28946
28947 TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
28948 auto Style = getLLVMStyle();
28949 Style.FixNamespaceComments = false;
28950 Style.MaxEmptyLinesToKeep = 2;
28951 Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always;
28952
28953 // Empty namespace.
28954 verifyFormat("namespace N {}", Style);
28955
28956 // Single namespace.
28957 verifyFormat("namespace N {\n"
28958 "\n"
28959 "int f1(int a) { return 2 * a; }\n"
28960 "\n"
28961 "}",
28962 "namespace N {\n"
28963 "int f1(int a) { return 2 * a; }\n"
28964 "}",
28965 Style);
28966
28967 // Nested namespace.
28968 verifyFormat("namespace N1 {\n"
28969 "namespace N2 {\n"
28970 "\n"
28971 "int a = 1;\n"
28972 "\n"
28973 "}\n"
28974 "}",
28975 "namespace N1 {\n"
28976 "namespace N2 {\n"
28977 "int a = 1;\n"
28978 "}\n"
28979 "}",
28980 Style);
28981
28982 verifyFormat("namespace N1 {\n"
28983 "\n"
28984 "namespace N2 {\n"
28985 "\n"
28986 "\n"
28987 "int a = 1;\n"
28988 "\n"
28989 "\n"
28990 "}\n"
28991 "\n"
28992 "}",
28993 "namespace N1 {\n"
28994 "\n"
28995 "namespace N2 {\n"
28996 "\n"
28997 "\n"
28998 "\n"
28999 "int a = 1;\n"
29000 "\n"
29001 "\n"
29002 "\n"
29003 "}\n"
29004 "\n"
29005 "}",
29006 Style);
29007
29008 Style.CompactNamespaces = true;
29009
29010 verifyFormat("namespace N1 { namespace N2 {\n"
29011 "\n"
29012 "int a = 1;\n"
29013 "\n"
29014 "}}",
29015 "namespace N1 { namespace N2 {\n"
29016 "int a = 1;\n"
29017 "}}",
29018 Style);
29019 }
29020
29021 TEST_F(FormatTest, BreakBeforeClassName) {
29022 verifyFormat("class ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_NULLABILITY_COMPATIBLE\n"
29023 " ArenaSafeUniquePtr {};");
29024 }
29025
29026 TEST_F(FormatTest, KeywordedFunctionLikeMacros) {
29027 constexpr StringRef Code("Q_PROPERTY(int name\n"
29028 " READ name\n"
29029 " WRITE setName\n"
29030 " NOTIFY nameChanged)");
29031 constexpr StringRef Code2("class A {\n"
29032 " Q_PROPERTY(int name\n"
29033 " READ name\n"
29034 " WRITE setName\n"
29035 " NOTIFY nameChanged)\n"
29036 "};");
29037
29038 auto Style = getLLVMStyle();
29039 Style.AllowBreakBeforeQtProperty = true;
29040
29041 Style.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
29042 verifyFormat(Code, Style);
29043 verifyFormat(Code2, Style);
29044
29045 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
29046 Style.ColumnLimit = 40;
29047 verifyFormat(Code, Style);
29048 verifyFormat(Code2, Style);
29049 verifyFormat("/* sdf */ Q_PROPERTY(int name\n"
29050 " READ name\n"
29051 " WRITE setName\n"
29052 " NOTIFY nameChanged)",
29053 Style);
29054 }
29055
29056 TEST_F(FormatTest, UnbalancedAngleBrackets) {
29057 verifyFormat("template <");
29058
29059 verifyNoCrash("typename foo<bar>::value, const String &>::type f();",
29060 getLLVMStyleWithColumns(50));
29061 }
29062
29063 } // namespace
29064 } // namespace test
29065 } // namespace format
29066 } // namespace clang