[gn build] Port 0e80f9a1b51e
[llvm-project.git] / clang / unittests / Format / FormatTest.cpp
blobeeb857a914b1a28fdec16e84dbc019d840151e5f
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 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test"
13 namespace clang {
14 namespace format {
15 namespace test {
16 namespace {
18 class FormatTest : public test::FormatTestBase {};
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"));
28 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
29 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
32 TEST_F(FormatTest, LLVMStyleOverride) {
33 EXPECT_EQ(FormatStyle::LK_Proto,
34 getLLVMStyle(FormatStyle::LK_Proto).Language);
37 //===----------------------------------------------------------------------===//
38 // Basic function tests.
39 //===----------------------------------------------------------------------===//
41 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); }
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;");
49 auto Style = getLLVMStyle();
50 Style.KeepEmptyLines.AtStartOfFile = false;
51 verifyFormat("int i;", " \n\t \v \f int i;", Style);
54 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
55 verifyFormat("int i;", "int\ni;");
58 TEST_F(FormatTest, FormatsNestedBlockStatements) {
59 verifyFormat("{\n"
60 " {\n"
61 " {\n"
62 " }\n"
63 " }\n"
64 "}",
65 "{{{}}}");
68 TEST_F(FormatTest, FormatsNestedCall) {
69 verifyFormat("Method(f1, f2(f3));");
70 verifyFormat("Method(f1(f2, f3()));");
71 verifyFormat("Method(f1(f2, (f3())));");
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();");
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);
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 "};");
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());
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"
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);
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());
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());
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 "}");
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");
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;
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);
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);");
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;");
436 //===----------------------------------------------------------------------===//
437 // Tests for control statements.
438 //===----------------------------------------------------------------------===//
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();");
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 "}");
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 "}");
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);
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);
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);
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);
646 AllowsMergedIf.ColumnLimit = 13;
647 verifyFormat("if (a)\n return;", AllowsMergedIf);
648 verifyFormat("FI (a)\n return;", AllowsMergedIf);
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);
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);
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);
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);
974 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
975 FormatStyle::SIS_OnlyFirstIf;
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);
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);
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);
1181 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1182 FormatStyle::SIS_AllIfsAndElse;
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);
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);
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);
1367 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1368 verifyFormat("while (true)\n"
1369 " ;");
1370 verifyFormat("for (;;)\n"
1371 " ;");
1373 FormatStyle AllowsMergedLoops = getLLVMStyle();
1374 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1376 verifyFormat("while (true) continue;", AllowsMergedLoops);
1377 verifyFormat("for (;;) continue;", AllowsMergedLoops);
1378 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1379 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1380 verifyFormat("while (true);", AllowsMergedLoops);
1381 verifyFormat("for (;;);", AllowsMergedLoops);
1382 verifyFormat("for (;;)\n"
1383 " for (;;) continue;",
1384 AllowsMergedLoops);
1385 verifyFormat("for (;;)\n"
1386 " while (true) continue;",
1387 AllowsMergedLoops);
1388 verifyFormat("while (true)\n"
1389 " for (;;) continue;",
1390 AllowsMergedLoops);
1391 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1392 " for (;;) continue;",
1393 AllowsMergedLoops);
1394 verifyFormat("for (;;)\n"
1395 " BOOST_FOREACH (int &v, vec) continue;",
1396 AllowsMergedLoops);
1397 verifyFormat("for (;;) // Can't merge this\n"
1398 " continue;",
1399 AllowsMergedLoops);
1400 verifyFormat("for (;;) /* still don't merge */\n"
1401 " continue;",
1402 AllowsMergedLoops);
1403 verifyFormat("do a++;\n"
1404 "while (true);",
1405 AllowsMergedLoops);
1406 verifyFormat("do /* Don't merge */\n"
1407 " a++;\n"
1408 "while (true);",
1409 AllowsMergedLoops);
1410 verifyFormat("do // Don't merge\n"
1411 " a++;\n"
1412 "while (true);",
1413 AllowsMergedLoops);
1414 verifyFormat("do\n"
1415 " // Don't merge\n"
1416 " a++;\n"
1417 "while (true);",
1418 AllowsMergedLoops);
1420 // Without braces labels are interpreted differently.
1421 verifyFormat("{\n"
1422 " do\n"
1423 " label:\n"
1424 " a++;\n"
1425 " while (true);\n"
1426 "}",
1427 AllowsMergedLoops);
1429 // Don't merge if there are comments before the null statement.
1430 verifyFormat("while (1) //\n"
1431 " ;",
1432 AllowsMergedLoops);
1433 verifyFormat("for (;;) /**/\n"
1434 " ;",
1435 AllowsMergedLoops);
1436 verifyFormat("while (true) /**/\n"
1437 " ;",
1438 "while (true) /**/;", AllowsMergedLoops);
1441 TEST_F(FormatTest, FormatShortBracedStatements) {
1442 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1443 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1444 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1445 FormatStyle::SIS_Never);
1446 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1447 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1448 verifyFormat("for (;;) {\n"
1449 " f();\n"
1450 "}");
1451 verifyFormat("/*comment*/ for (;;) {\n"
1452 " f();\n"
1453 "}");
1454 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1455 " f();\n"
1456 "}");
1457 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1458 " f();\n"
1459 "}");
1460 verifyFormat("while (true) {\n"
1461 " f();\n"
1462 "}");
1463 verifyFormat("/*comment*/ while (true) {\n"
1464 " f();\n"
1465 "}");
1466 verifyFormat("if (true) {\n"
1467 " f();\n"
1468 "}");
1469 verifyFormat("/*comment*/ if (true) {\n"
1470 " f();\n"
1471 "}");
1473 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1474 FormatStyle::SBS_Empty;
1475 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1476 FormatStyle::SIS_WithoutElse;
1477 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1478 verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1479 verifyFormat("if (i > 0) {\n"
1480 " return i;\n"
1481 "}",
1482 AllowSimpleBracedStatements);
1484 AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1485 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1486 // Not IF to avoid any confusion that IF is somehow special.
1487 AllowSimpleBracedStatements.IfMacros.push_back("FI");
1488 AllowSimpleBracedStatements.ColumnLimit = 40;
1489 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1490 FormatStyle::SBS_Always;
1491 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1492 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1493 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1494 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1496 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1497 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1498 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1499 verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1500 verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1501 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1502 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1503 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1504 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1505 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1506 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1507 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1508 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1509 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1510 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1511 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1512 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1513 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1514 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1515 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1516 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1517 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1518 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1519 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1520 AllowSimpleBracedStatements);
1521 verifyFormat("if (true) {\n"
1522 " ffffffffffffffffffffffff();\n"
1523 "}",
1524 AllowSimpleBracedStatements);
1525 verifyFormat("if (true) {\n"
1526 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1527 "}",
1528 AllowSimpleBracedStatements);
1529 verifyFormat("if (true) { //\n"
1530 " f();\n"
1531 "}",
1532 AllowSimpleBracedStatements);
1533 verifyFormat("if (true) {\n"
1534 " f();\n"
1535 " f();\n"
1536 "}",
1537 AllowSimpleBracedStatements);
1538 verifyFormat("if (true) {\n"
1539 " f();\n"
1540 "} else {\n"
1541 " f();\n"
1542 "}",
1543 AllowSimpleBracedStatements);
1544 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1545 AllowSimpleBracedStatements);
1546 verifyFormat("MYIF (true) {\n"
1547 " ffffffffffffffffffffffff();\n"
1548 "}",
1549 AllowSimpleBracedStatements);
1550 verifyFormat("MYIF (true) {\n"
1551 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1552 "}",
1553 AllowSimpleBracedStatements);
1554 verifyFormat("MYIF (true) { //\n"
1555 " f();\n"
1556 "}",
1557 AllowSimpleBracedStatements);
1558 verifyFormat("MYIF (true) {\n"
1559 " f();\n"
1560 " f();\n"
1561 "}",
1562 AllowSimpleBracedStatements);
1563 verifyFormat("MYIF (true) {\n"
1564 " f();\n"
1565 "} else {\n"
1566 " f();\n"
1567 "}",
1568 AllowSimpleBracedStatements);
1570 verifyFormat("struct A2 {\n"
1571 " int X;\n"
1572 "};",
1573 AllowSimpleBracedStatements);
1574 verifyFormat("typedef struct A2 {\n"
1575 " int X;\n"
1576 "} A2_t;",
1577 AllowSimpleBracedStatements);
1578 verifyFormat("template <int> struct A2 {\n"
1579 " struct B {};\n"
1580 "};",
1581 AllowSimpleBracedStatements);
1583 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1584 FormatStyle::SIS_Never;
1585 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1586 verifyFormat("if (true) {\n"
1587 " f();\n"
1588 "}",
1589 AllowSimpleBracedStatements);
1590 verifyFormat("if (true) {\n"
1591 " f();\n"
1592 "} else {\n"
1593 " f();\n"
1594 "}",
1595 AllowSimpleBracedStatements);
1596 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1597 verifyFormat("MYIF (true) {\n"
1598 " f();\n"
1599 "}",
1600 AllowSimpleBracedStatements);
1601 verifyFormat("MYIF (true) {\n"
1602 " f();\n"
1603 "} else {\n"
1604 " f();\n"
1605 "}",
1606 AllowSimpleBracedStatements);
1608 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1609 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1610 verifyFormat("while (true) {\n"
1611 " f();\n"
1612 "}",
1613 AllowSimpleBracedStatements);
1614 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1615 verifyFormat("for (;;) {\n"
1616 " f();\n"
1617 "}",
1618 AllowSimpleBracedStatements);
1619 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1620 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1621 " f();\n"
1622 "}",
1623 AllowSimpleBracedStatements);
1625 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1626 FormatStyle::SIS_WithoutElse;
1627 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1628 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1629 FormatStyle::BWACS_Always;
1631 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1632 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1633 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1634 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1635 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1636 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1637 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1638 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1639 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1640 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1641 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1642 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1643 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1644 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1645 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1646 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1647 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1648 AllowSimpleBracedStatements);
1649 verifyFormat("if (true)\n"
1650 "{\n"
1651 " ffffffffffffffffffffffff();\n"
1652 "}",
1653 AllowSimpleBracedStatements);
1654 verifyFormat("if (true)\n"
1655 "{\n"
1656 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1657 "}",
1658 AllowSimpleBracedStatements);
1659 verifyFormat("if (true)\n"
1660 "{ //\n"
1661 " f();\n"
1662 "}",
1663 AllowSimpleBracedStatements);
1664 verifyFormat("if (true)\n"
1665 "{\n"
1666 " f();\n"
1667 " f();\n"
1668 "}",
1669 AllowSimpleBracedStatements);
1670 verifyFormat("if (true)\n"
1671 "{\n"
1672 " f();\n"
1673 "} else\n"
1674 "{\n"
1675 " f();\n"
1676 "}",
1677 AllowSimpleBracedStatements);
1678 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1679 AllowSimpleBracedStatements);
1680 verifyFormat("MYIF (true)\n"
1681 "{\n"
1682 " ffffffffffffffffffffffff();\n"
1683 "}",
1684 AllowSimpleBracedStatements);
1685 verifyFormat("MYIF (true)\n"
1686 "{\n"
1687 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1688 "}",
1689 AllowSimpleBracedStatements);
1690 verifyFormat("MYIF (true)\n"
1691 "{ //\n"
1692 " f();\n"
1693 "}",
1694 AllowSimpleBracedStatements);
1695 verifyFormat("MYIF (true)\n"
1696 "{\n"
1697 " f();\n"
1698 " f();\n"
1699 "}",
1700 AllowSimpleBracedStatements);
1701 verifyFormat("MYIF (true)\n"
1702 "{\n"
1703 " f();\n"
1704 "} else\n"
1705 "{\n"
1706 " f();\n"
1707 "}",
1708 AllowSimpleBracedStatements);
1710 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1711 FormatStyle::SIS_Never;
1712 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1713 verifyFormat("if (true)\n"
1714 "{\n"
1715 " f();\n"
1716 "}",
1717 AllowSimpleBracedStatements);
1718 verifyFormat("if (true)\n"
1719 "{\n"
1720 " f();\n"
1721 "} else\n"
1722 "{\n"
1723 " f();\n"
1724 "}",
1725 AllowSimpleBracedStatements);
1726 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1727 verifyFormat("MYIF (true)\n"
1728 "{\n"
1729 " f();\n"
1730 "}",
1731 AllowSimpleBracedStatements);
1732 verifyFormat("MYIF (true)\n"
1733 "{\n"
1734 " f();\n"
1735 "} else\n"
1736 "{\n"
1737 " f();\n"
1738 "}",
1739 AllowSimpleBracedStatements);
1741 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1742 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1743 verifyFormat("while (true)\n"
1744 "{\n"
1745 " f();\n"
1746 "}",
1747 AllowSimpleBracedStatements);
1748 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1749 verifyFormat("for (;;)\n"
1750 "{\n"
1751 " f();\n"
1752 "}",
1753 AllowSimpleBracedStatements);
1754 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1755 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1756 "{\n"
1757 " f();\n"
1758 "}",
1759 AllowSimpleBracedStatements);
1761 FormatStyle Style = getLLVMStyle();
1762 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1763 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1765 verifyFormat("while (i > 0)\n"
1766 "{\n"
1767 " --i;\n"
1768 "}",
1769 Style);
1771 verifyFormat("if (a)\n"
1772 "{\n"
1773 " ++b;\n"
1774 "}",
1775 Style);
1777 verifyFormat("if (a)\n"
1778 "{\n"
1779 " b = 1;\n"
1780 "} else\n"
1781 "{\n"
1782 " b = 0;\n"
1783 "}",
1784 Style);
1786 verifyFormat("if (a)\n"
1787 "{\n"
1788 " b = 1;\n"
1789 "} else if (c)\n"
1790 "{\n"
1791 " b = 2;\n"
1792 "} else\n"
1793 "{\n"
1794 " b = 0;\n"
1795 "}",
1796 Style);
1798 Style.BraceWrapping.BeforeElse = true;
1800 verifyFormat("if (a)\n"
1801 "{\n"
1802 " b = 1;\n"
1803 "}\n"
1804 "else\n"
1805 "{\n"
1806 " b = 0;\n"
1807 "}",
1808 Style);
1810 verifyFormat("if (a)\n"
1811 "{\n"
1812 " b = 1;\n"
1813 "}\n"
1814 "else if (c)\n"
1815 "{\n"
1816 " b = 2;\n"
1817 "}\n"
1818 "else\n"
1819 "{\n"
1820 " b = 0;\n"
1821 "}",
1822 Style);
1825 TEST_F(FormatTest, UnderstandsMacros) {
1826 verifyFormat("#define A (parentheses)");
1827 verifyFormat("/* comment */ #define A (parentheses)");
1828 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1829 // Even the partial code should never be merged.
1830 verifyNoChange("/* comment */ #define A (parentheses)\n"
1831 "#");
1832 verifyFormat("/* comment */ #define A (parentheses)\n"
1833 "#\n");
1834 verifyFormat("/* comment */ #define A (parentheses)\n"
1835 "#define B (parentheses)");
1836 verifyFormat("#define true ((int)1)");
1837 verifyFormat("#define and(x)");
1838 verifyFormat("#define if(x) x");
1839 verifyFormat("#define return(x) (x)");
1840 verifyFormat("#define while(x) for (; x;)");
1841 verifyFormat("#define xor(x) (^(x))");
1842 verifyFormat("#define __except(x)");
1843 verifyFormat("#define __try(x)");
1845 // https://llvm.org/PR54348.
1846 verifyFormat(
1847 "#define A"
1849 "\\\n"
1850 " class & {}");
1852 FormatStyle Style = getLLVMStyle();
1853 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1854 Style.BraceWrapping.AfterFunction = true;
1855 // Test that a macro definition never gets merged with the following
1856 // definition.
1857 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1858 verifyFormat("#define AAA "
1859 " \\\n"
1860 " N "
1861 " \\\n"
1862 " {\n"
1863 "#define BBB }",
1864 Style);
1865 // verifyFormat("#define AAA N { //", Style);
1867 verifyFormat("MACRO(return)");
1868 verifyFormat("MACRO(co_await)");
1869 verifyFormat("MACRO(co_return)");
1870 verifyFormat("MACRO(co_yield)");
1871 verifyFormat("MACRO(return, something)");
1872 verifyFormat("MACRO(co_return, something)");
1873 verifyFormat("MACRO(something##something)");
1874 verifyFormat("MACRO(return##something)");
1875 verifyFormat("MACRO(co_return##something)");
1877 verifyFormat("#define A x:");
1879 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1880 " { \\\n"
1881 " #Bar \\\n"
1882 " }");
1883 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1884 " { #Bar }");
1887 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1888 FormatStyle Style = getLLVMStyleWithColumns(60);
1889 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1890 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1891 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1892 verifyFormat("#define A \\\n"
1893 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1894 " { \\\n"
1895 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1896 " }\n"
1897 "X;",
1898 "#define A \\\n"
1899 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1900 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1901 " }\n"
1902 "X;",
1903 Style);
1906 TEST_F(FormatTest, ParseIfElse) {
1907 verifyFormat("if (true)\n"
1908 " if (true)\n"
1909 " if (true)\n"
1910 " f();\n"
1911 " else\n"
1912 " g();\n"
1913 " else\n"
1914 " h();\n"
1915 "else\n"
1916 " i();");
1917 verifyFormat("if (true)\n"
1918 " if (true)\n"
1919 " if (true) {\n"
1920 " if (true)\n"
1921 " f();\n"
1922 " } else {\n"
1923 " g();\n"
1924 " }\n"
1925 " else\n"
1926 " h();\n"
1927 "else {\n"
1928 " i();\n"
1929 "}");
1930 verifyFormat("if (true)\n"
1931 " if constexpr (true)\n"
1932 " if (true) {\n"
1933 " if constexpr (true)\n"
1934 " f();\n"
1935 " } else {\n"
1936 " g();\n"
1937 " }\n"
1938 " else\n"
1939 " h();\n"
1940 "else {\n"
1941 " i();\n"
1942 "}");
1943 verifyFormat("if (true)\n"
1944 " if CONSTEXPR (true)\n"
1945 " if (true) {\n"
1946 " if CONSTEXPR (true)\n"
1947 " f();\n"
1948 " } else {\n"
1949 " g();\n"
1950 " }\n"
1951 " else\n"
1952 " h();\n"
1953 "else {\n"
1954 " i();\n"
1955 "}");
1956 verifyFormat("void f() {\n"
1957 " if (a) {\n"
1958 " } else {\n"
1959 " }\n"
1960 "}");
1963 TEST_F(FormatTest, ElseIf) {
1964 verifyFormat("if (a) {\n} else if (b) {\n}");
1965 verifyFormat("if (a)\n"
1966 " f();\n"
1967 "else if (b)\n"
1968 " g();\n"
1969 "else\n"
1970 " h();");
1971 verifyFormat("if (a)\n"
1972 " f();\n"
1973 "else // comment\n"
1974 " if (b) {\n"
1975 " g();\n"
1976 " h();\n"
1977 " }");
1978 verifyFormat("if constexpr (a)\n"
1979 " f();\n"
1980 "else if constexpr (b)\n"
1981 " g();\n"
1982 "else\n"
1983 " h();");
1984 verifyFormat("if CONSTEXPR (a)\n"
1985 " f();\n"
1986 "else if CONSTEXPR (b)\n"
1987 " g();\n"
1988 "else\n"
1989 " h();");
1990 verifyFormat("if (a) {\n"
1991 " f();\n"
1992 "}\n"
1993 "// or else ..\n"
1994 "else {\n"
1995 " g()\n"
1996 "}");
1998 verifyFormat("if (a) {\n"
1999 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2001 "}");
2002 verifyFormat("if (a) {\n"
2003 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2005 "}");
2006 verifyFormat("if (a) {\n"
2007 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2009 "}");
2010 verifyFormat("if (a) {\n"
2011 "} else if (\n"
2012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2013 "}",
2014 getLLVMStyleWithColumns(62));
2015 verifyFormat("if (a) {\n"
2016 "} else if constexpr (\n"
2017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2018 "}",
2019 getLLVMStyleWithColumns(62));
2020 verifyFormat("if (a) {\n"
2021 "} else if CONSTEXPR (\n"
2022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2023 "}",
2024 getLLVMStyleWithColumns(62));
2027 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2028 FormatStyle Style = getLLVMStyle();
2029 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2030 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2031 verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2032 verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2033 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2034 verifyFormat("int *f1(int &a) const &;", Style);
2035 verifyFormat("int *f1(int &a) const & = 0;", Style);
2036 verifyFormat("int *a = f1();", Style);
2037 verifyFormat("int &b = f2();", Style);
2038 verifyFormat("int &&c = f3();", Style);
2039 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2040 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2041 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2042 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2043 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2044 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2045 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2046 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2047 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2048 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2049 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2050 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2051 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2052 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2053 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2054 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2055 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2056 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2057 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2058 verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2059 verifyFormat(
2060 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2061 " res2 = [](int &a) { return 0000000000000; };",
2062 Style);
2064 Style.AlignConsecutiveDeclarations.Enabled = true;
2065 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2066 verifyFormat("Const unsigned int *c;\n"
2067 "const unsigned int *d;\n"
2068 "Const unsigned int &e;\n"
2069 "const unsigned int &f;\n"
2070 "int *f1(int *a, int &b, int &&c);\n"
2071 "double *(*f2)(int *a, double &&b);\n"
2072 "const unsigned &&g;\n"
2073 "Const unsigned h;",
2074 Style);
2075 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2076 verifyFormat("Const unsigned int *c;\n"
2077 "const unsigned int *d;\n"
2078 "Const unsigned int &e;\n"
2079 "const unsigned int &f;\n"
2080 "int *f1(int *a, int &b, int &&c);\n"
2081 "double *(*f2)(int *a, double &&b);\n"
2082 "const unsigned &&g;\n"
2083 "Const unsigned h;",
2084 Style);
2086 Style.PointerAlignment = FormatStyle::PAS_Left;
2087 Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2088 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2089 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2090 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2091 verifyFormat("int* f1(int& a) const& = 0;", Style);
2092 verifyFormat("int* a = f1();", Style);
2093 verifyFormat("int& b = f2();", Style);
2094 verifyFormat("int&& c = f3();", Style);
2095 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2096 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2097 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2098 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2099 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2100 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2101 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2102 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2103 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2104 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2105 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2106 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2107 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2108 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2109 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2110 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2111 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2112 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2113 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2114 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2115 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2116 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2117 verifyFormat(
2118 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2119 " res2 = [](int& a) { return 0000000000000; };",
2120 Style);
2122 Style.AlignConsecutiveDeclarations.Enabled = true;
2123 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2124 verifyFormat("Const unsigned int* c;\n"
2125 "const unsigned int* d;\n"
2126 "Const unsigned int& e;\n"
2127 "const unsigned int& f;\n"
2128 "int* f1(int* a, int& b, int&& c);\n"
2129 "double* (*f2)(int* a, double&& b);\n"
2130 "const unsigned&& g;\n"
2131 "Const unsigned h;",
2132 Style);
2133 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2134 verifyFormat("Const unsigned int* c;\n"
2135 "const unsigned int* d;\n"
2136 "Const unsigned int& e;\n"
2137 "const unsigned int& f;\n"
2138 "int* f1(int* a, int& b, int&& c);\n"
2139 "double* (*f2)(int* a, double&& b);\n"
2140 "const unsigned&& g;\n"
2141 "Const unsigned h;",
2142 Style);
2144 Style.PointerAlignment = FormatStyle::PAS_Right;
2145 Style.ReferenceAlignment = FormatStyle::RAS_Left;
2146 verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2147 verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2148 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2149 verifyFormat("int *a = f1();", Style);
2150 verifyFormat("int& b = f2();", Style);
2151 verifyFormat("int&& c = f3();", Style);
2152 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2153 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2154 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2155 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2156 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2157 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2158 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2160 Style.AlignConsecutiveDeclarations.Enabled = true;
2161 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2162 verifyFormat("Const unsigned int *c;\n"
2163 "const unsigned int *d;\n"
2164 "Const unsigned int& e;\n"
2165 "const unsigned int& f;\n"
2166 "int *f1(int *a, int& b, int&& c);\n"
2167 "double *(*f2)(int *a, double&& b);\n"
2168 "const unsigned&& g;\n"
2169 "Const unsigned h;",
2170 Style);
2171 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2172 verifyFormat("Const unsigned int *c;\n"
2173 "const unsigned int *d;\n"
2174 "Const unsigned int& e;\n"
2175 "const unsigned int& f;\n"
2176 "int *f1(int *a, int& b, int&& c);\n"
2177 "double *(*f2)(int *a, double&& b);\n"
2178 "const unsigned&& g;\n"
2179 "Const unsigned h;",
2180 Style);
2182 Style.PointerAlignment = FormatStyle::PAS_Left;
2183 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2184 verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2185 verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2186 verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2187 verifyFormat("int* a = f1();", Style);
2188 verifyFormat("int & b = f2();", Style);
2189 verifyFormat("int && c = f3();", Style);
2190 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2191 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2192 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2193 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2194 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2195 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2196 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2197 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2198 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2199 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2200 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2201 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2202 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2203 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2204 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2205 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2206 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2207 verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2208 verifyFormat(
2209 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2210 " res2 = [](int & a) { return 0000000000000; };",
2211 Style);
2213 Style.AlignConsecutiveDeclarations.Enabled = true;
2214 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2215 verifyFormat("Const unsigned int* c;\n"
2216 "const unsigned int* d;\n"
2217 "Const unsigned int & e;\n"
2218 "const unsigned int & f;\n"
2219 "int* f1(int* a, int & b, int && c);\n"
2220 "double* (*f2)(int* a, double && b);\n"
2221 "const unsigned && g;\n"
2222 "Const unsigned h;",
2223 Style);
2224 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2225 verifyFormat("Const unsigned int* c;\n"
2226 "const unsigned int* d;\n"
2227 "Const unsigned int & e;\n"
2228 "const unsigned int & f;\n"
2229 "int* f1(int* a, int & b, int && c);\n"
2230 "double* (*f2)(int* a, double && b);\n"
2231 "const unsigned && g;\n"
2232 "Const unsigned h;",
2233 Style);
2235 Style.PointerAlignment = FormatStyle::PAS_Middle;
2236 Style.ReferenceAlignment = FormatStyle::RAS_Right;
2237 verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2238 verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2239 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2240 verifyFormat("int * a = f1();", Style);
2241 verifyFormat("int &b = f2();", Style);
2242 verifyFormat("int &&c = f3();", Style);
2243 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2244 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2245 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2246 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2247 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2248 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2249 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2251 Style.AlignConsecutiveDeclarations.Enabled = true;
2252 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2253 verifyFormat("Const unsigned int * c;\n"
2254 "const unsigned int * d;\n"
2255 "Const unsigned int &e;\n"
2256 "const unsigned int &f;\n"
2257 "int * f1(int * a, int &b, int &&c);\n"
2258 "double * (*f2)(int * a, double &&b);\n"
2259 "const unsigned &&g;\n"
2260 "Const unsigned h;",
2261 Style);
2262 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2263 verifyFormat("Const unsigned int * c;\n"
2264 "const unsigned int * d;\n"
2265 "Const unsigned int &e;\n"
2266 "const unsigned int &f;\n"
2267 "int * f1(int * a, int &b, int &&c);\n"
2268 "double * (*f2)(int * a, double &&b);\n"
2269 "const unsigned &&g;\n"
2270 "Const unsigned h;",
2271 Style);
2273 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2274 // specifically handled
2275 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2278 TEST_F(FormatTest, FormatsForLoop) {
2279 verifyFormat(
2280 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2281 " ++VeryVeryLongLoopVariable)\n"
2282 " ;");
2283 verifyFormat("for (;;)\n"
2284 " f();");
2285 verifyFormat("for (;;) {\n}");
2286 verifyFormat("for (;;) {\n"
2287 " f();\n"
2288 "}");
2289 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2291 verifyFormat(
2292 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2293 " E = UnwrappedLines.end();\n"
2294 " I != E; ++I) {\n}");
2296 verifyFormat(
2297 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2298 " ++IIIII) {\n}");
2299 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2300 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2301 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2302 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2303 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2304 " E = FD->getDeclsInPrototypeScope().end();\n"
2305 " I != E; ++I) {\n}");
2306 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2307 " I = Container.begin(),\n"
2308 " E = Container.end();\n"
2309 " I != E; ++I) {\n}",
2310 getLLVMStyleWithColumns(76));
2312 verifyFormat(
2313 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2317 " ++aaaaaaaaaaa) {\n}");
2318 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2319 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2320 " ++i) {\n}");
2321 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2322 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2323 "}");
2324 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2325 " aaaaaaaaaa);\n"
2326 " iter; ++iter) {\n"
2327 "}");
2328 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2330 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2331 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2333 // These should not be formatted as Objective-C for-in loops.
2334 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2335 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2336 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2337 verifyFormat(
2338 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2340 FormatStyle NoBinPacking = getLLVMStyle();
2341 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
2342 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2343 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2344 " aaaaaaaaaaaaaaaa,\n"
2345 " aaaaaaaaaaaaaaaa,\n"
2346 " aaaaaaaaaaaaaaaa);\n"
2347 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2348 "}",
2349 NoBinPacking);
2350 verifyFormat(
2351 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2352 " E = UnwrappedLines.end();\n"
2353 " I != E;\n"
2354 " ++I) {\n}",
2355 NoBinPacking);
2357 FormatStyle AlignLeft = getLLVMStyle();
2358 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2359 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2362 TEST_F(FormatTest, RangeBasedForLoops) {
2363 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2365 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2366 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2367 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2369 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2370 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2373 TEST_F(FormatTest, ForEachLoops) {
2374 FormatStyle Style = getLLVMStyle();
2375 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2376 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2377 verifyFormat("void f() {\n"
2378 " for (;;) {\n"
2379 " }\n"
2380 " foreach (Item *item, itemlist) {\n"
2381 " }\n"
2382 " Q_FOREACH (Item *item, itemlist) {\n"
2383 " }\n"
2384 " BOOST_FOREACH (Item *item, itemlist) {\n"
2385 " }\n"
2386 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2387 "}",
2388 Style);
2389 verifyFormat("void f() {\n"
2390 " for (;;)\n"
2391 " int j = 1;\n"
2392 " Q_FOREACH (int v, vec)\n"
2393 " v *= 2;\n"
2394 " for (;;) {\n"
2395 " int j = 1;\n"
2396 " }\n"
2397 " Q_FOREACH (int v, vec) {\n"
2398 " v *= 2;\n"
2399 " }\n"
2400 "}",
2401 Style);
2403 FormatStyle ShortBlocks = getLLVMStyle();
2404 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2405 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2406 verifyFormat("void f() {\n"
2407 " for (;;)\n"
2408 " int j = 1;\n"
2409 " Q_FOREACH (int &v, vec)\n"
2410 " v *= 2;\n"
2411 " for (;;) {\n"
2412 " int j = 1;\n"
2413 " }\n"
2414 " Q_FOREACH (int &v, vec) {\n"
2415 " int j = 1;\n"
2416 " }\n"
2417 "}",
2418 ShortBlocks);
2420 FormatStyle ShortLoops = getLLVMStyle();
2421 ShortLoops.AllowShortLoopsOnASingleLine = true;
2422 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2423 verifyFormat("void f() {\n"
2424 " for (;;) int j = 1;\n"
2425 " Q_FOREACH (int &v, vec) int j = 1;\n"
2426 " for (;;) {\n"
2427 " int j = 1;\n"
2428 " }\n"
2429 " Q_FOREACH (int &v, vec) {\n"
2430 " int j = 1;\n"
2431 " }\n"
2432 "}",
2433 ShortLoops);
2435 FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2436 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2437 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2438 verifyFormat("void f() {\n"
2439 " for (;;) int j = 1;\n"
2440 " Q_FOREACH (int &v, vec) int j = 1;\n"
2441 " for (;;) { int j = 1; }\n"
2442 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2443 "}",
2444 ShortBlocksAndLoops);
2446 Style.SpaceBeforeParens =
2447 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2448 verifyFormat("void f() {\n"
2449 " for (;;) {\n"
2450 " }\n"
2451 " foreach(Item *item, itemlist) {\n"
2452 " }\n"
2453 " Q_FOREACH(Item *item, itemlist) {\n"
2454 " }\n"
2455 " BOOST_FOREACH(Item *item, itemlist) {\n"
2456 " }\n"
2457 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2458 "}",
2459 Style);
2461 // As function-like macros.
2462 verifyFormat("#define foreach(x, y)\n"
2463 "#define Q_FOREACH(x, y)\n"
2464 "#define BOOST_FOREACH(x, y)\n"
2465 "#define UNKNOWN_FOREACH(x, y)");
2467 // Not as function-like macros.
2468 verifyFormat("#define foreach (x, y)\n"
2469 "#define Q_FOREACH (x, y)\n"
2470 "#define BOOST_FOREACH (x, y)\n"
2471 "#define UNKNOWN_FOREACH (x, y)");
2473 // handle microsoft non standard extension
2474 verifyFormat("for each (char c in x->MyStringProperty)");
2477 TEST_F(FormatTest, FormatsWhileLoop) {
2478 verifyFormat("while (true) {\n}");
2479 verifyFormat("while (true)\n"
2480 " f();");
2481 verifyFormat("while () {\n}");
2482 verifyFormat("while () {\n"
2483 " f();\n"
2484 "}");
2487 TEST_F(FormatTest, FormatsDoWhile) {
2488 verifyFormat("do {\n"
2489 " do_something();\n"
2490 "} while (something());");
2491 verifyFormat("do\n"
2492 " do_something();\n"
2493 "while (something());");
2496 TEST_F(FormatTest, FormatsSwitchStatement) {
2497 verifyFormat("switch (x) {\n"
2498 "case 1:\n"
2499 " f();\n"
2500 " break;\n"
2501 "case kFoo:\n"
2502 "case ns::kBar:\n"
2503 "case kBaz:\n"
2504 " break;\n"
2505 "default:\n"
2506 " g();\n"
2507 " break;\n"
2508 "}");
2509 verifyFormat("switch (x) {\n"
2510 "case 1: {\n"
2511 " f();\n"
2512 " break;\n"
2513 "}\n"
2514 "case 2: {\n"
2515 " break;\n"
2516 "}\n"
2517 "}");
2518 verifyFormat("switch (x) {\n"
2519 "case 1: {\n"
2520 " f();\n"
2521 " {\n"
2522 " g();\n"
2523 " h();\n"
2524 " }\n"
2525 " break;\n"
2526 "}\n"
2527 "}");
2528 verifyFormat("switch (x) {\n"
2529 "case 1: {\n"
2530 " f();\n"
2531 " if (foo) {\n"
2532 " g();\n"
2533 " h();\n"
2534 " }\n"
2535 " break;\n"
2536 "}\n"
2537 "}");
2538 verifyFormat("switch (x) {\n"
2539 "case 1: {\n"
2540 " f();\n"
2541 " g();\n"
2542 "} break;\n"
2543 "}");
2544 verifyFormat("switch (test)\n"
2545 " ;");
2546 verifyFormat("switch (x) {\n"
2547 "default: {\n"
2548 " // Do nothing.\n"
2549 "}\n"
2550 "}");
2551 verifyFormat("switch (x) {\n"
2552 "// comment\n"
2553 "// if 1, do f()\n"
2554 "case 1:\n"
2555 " f();\n"
2556 "}");
2557 verifyFormat("switch (x) {\n"
2558 "case 1:\n"
2559 " // Do amazing stuff\n"
2560 " {\n"
2561 " f();\n"
2562 " g();\n"
2563 " }\n"
2564 " break;\n"
2565 "}");
2566 verifyFormat("#define A \\\n"
2567 " switch (x) { \\\n"
2568 " case a: \\\n"
2569 " foo = b; \\\n"
2570 " }",
2571 getLLVMStyleWithColumns(20));
2572 verifyFormat("#define OPERATION_CASE(name) \\\n"
2573 " case OP_name: \\\n"
2574 " return operations::Operation##name",
2575 getLLVMStyleWithColumns(40));
2576 verifyFormat("switch (x) {\n"
2577 "case 1:;\n"
2578 "default:;\n"
2579 " int i;\n"
2580 "}");
2582 verifyGoogleFormat("switch (x) {\n"
2583 " case 1:\n"
2584 " f();\n"
2585 " break;\n"
2586 " case kFoo:\n"
2587 " case ns::kBar:\n"
2588 " case kBaz:\n"
2589 " break;\n"
2590 " default:\n"
2591 " g();\n"
2592 " break;\n"
2593 "}");
2594 verifyGoogleFormat("switch (x) {\n"
2595 " case 1: {\n"
2596 " f();\n"
2597 " break;\n"
2598 " }\n"
2599 "}");
2600 verifyGoogleFormat("switch (test)\n"
2601 " ;");
2603 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2604 " case OP_name: \\\n"
2605 " return operations::Operation##name");
2606 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2607 " // Get the correction operation class.\n"
2608 " switch (OpCode) {\n"
2609 " CASE(Add);\n"
2610 " CASE(Subtract);\n"
2611 " default:\n"
2612 " return operations::Unknown;\n"
2613 " }\n"
2614 "#undef OPERATION_CASE\n"
2615 "}");
2616 verifyFormat("DEBUG({\n"
2617 " switch (x) {\n"
2618 " case A:\n"
2619 " f();\n"
2620 " break;\n"
2621 " // fallthrough\n"
2622 " case B:\n"
2623 " g();\n"
2624 " break;\n"
2625 " }\n"
2626 "});");
2627 verifyNoChange("DEBUG({\n"
2628 " switch (x) {\n"
2629 " case A:\n"
2630 " f();\n"
2631 " break;\n"
2632 " // On B:\n"
2633 " case B:\n"
2634 " g();\n"
2635 " break;\n"
2636 " }\n"
2637 "});");
2638 verifyFormat("switch (n) {\n"
2639 "case 0: {\n"
2640 " return false;\n"
2641 "}\n"
2642 "default: {\n"
2643 " return true;\n"
2644 "}\n"
2645 "}",
2646 "switch (n)\n"
2647 "{\n"
2648 "case 0: {\n"
2649 " return false;\n"
2650 "}\n"
2651 "default: {\n"
2652 " return true;\n"
2653 "}\n"
2654 "}");
2655 verifyFormat("switch (a) {\n"
2656 "case (b):\n"
2657 " return;\n"
2658 "}");
2660 verifyFormat("switch (a) {\n"
2661 "case some_namespace::\n"
2662 " some_constant:\n"
2663 " return;\n"
2664 "}",
2665 getLLVMStyleWithColumns(34));
2667 verifyFormat("switch (a) {\n"
2668 "[[likely]] case 1:\n"
2669 " return;\n"
2670 "}");
2671 verifyFormat("switch (a) {\n"
2672 "[[likely]] [[other::likely]] case 1:\n"
2673 " return;\n"
2674 "}");
2675 verifyFormat("switch (x) {\n"
2676 "case 1:\n"
2677 " return;\n"
2678 "[[likely]] case 2:\n"
2679 " return;\n"
2680 "}");
2681 verifyFormat("switch (a) {\n"
2682 "case 1:\n"
2683 "[[likely]] case 2:\n"
2684 " return;\n"
2685 "}");
2686 FormatStyle Attributes = getLLVMStyle();
2687 Attributes.AttributeMacros.push_back("LIKELY");
2688 Attributes.AttributeMacros.push_back("OTHER_LIKELY");
2689 verifyFormat("switch (a) {\n"
2690 "LIKELY case b:\n"
2691 " return;\n"
2692 "}",
2693 Attributes);
2694 verifyFormat("switch (a) {\n"
2695 "LIKELY OTHER_LIKELY() case b:\n"
2696 " return;\n"
2697 "}",
2698 Attributes);
2699 verifyFormat("switch (a) {\n"
2700 "case 1:\n"
2701 " return;\n"
2702 "LIKELY case 2:\n"
2703 " return;\n"
2704 "}",
2705 Attributes);
2706 verifyFormat("switch (a) {\n"
2707 "case 1:\n"
2708 "LIKELY case 2:\n"
2709 " return;\n"
2710 "}",
2711 Attributes);
2713 FormatStyle Style = getLLVMStyle();
2714 Style.IndentCaseLabels = true;
2715 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2716 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2717 Style.BraceWrapping.AfterCaseLabel = true;
2718 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2719 verifyFormat("switch (n)\n"
2720 "{\n"
2721 " case 0:\n"
2722 " {\n"
2723 " return false;\n"
2724 " }\n"
2725 " default:\n"
2726 " {\n"
2727 " return true;\n"
2728 " }\n"
2729 "}",
2730 "switch (n) {\n"
2731 " case 0: {\n"
2732 " return false;\n"
2733 " }\n"
2734 " default: {\n"
2735 " return true;\n"
2736 " }\n"
2737 "}",
2738 Style);
2739 Style.BraceWrapping.AfterCaseLabel = false;
2740 verifyFormat("switch (n)\n"
2741 "{\n"
2742 " case 0: {\n"
2743 " return false;\n"
2744 " }\n"
2745 " default: {\n"
2746 " return true;\n"
2747 " }\n"
2748 "}",
2749 "switch (n) {\n"
2750 " case 0:\n"
2751 " {\n"
2752 " return false;\n"
2753 " }\n"
2754 " default:\n"
2755 " {\n"
2756 " return true;\n"
2757 " }\n"
2758 "}",
2759 Style);
2760 Style.IndentCaseLabels = false;
2761 Style.IndentCaseBlocks = true;
2762 verifyFormat("switch (n)\n"
2763 "{\n"
2764 "case 0:\n"
2765 " {\n"
2766 " return false;\n"
2767 " }\n"
2768 "case 1:\n"
2769 " break;\n"
2770 "default:\n"
2771 " {\n"
2772 " return true;\n"
2773 " }\n"
2774 "}",
2775 "switch (n) {\n"
2776 "case 0: {\n"
2777 " return false;\n"
2778 "}\n"
2779 "case 1:\n"
2780 " break;\n"
2781 "default: {\n"
2782 " return true;\n"
2783 "}\n"
2784 "}",
2785 Style);
2786 Style.IndentCaseLabels = true;
2787 Style.IndentCaseBlocks = true;
2788 verifyFormat("switch (n)\n"
2789 "{\n"
2790 " case 0:\n"
2791 " {\n"
2792 " return false;\n"
2793 " }\n"
2794 " case 1:\n"
2795 " break;\n"
2796 " default:\n"
2797 " {\n"
2798 " return true;\n"
2799 " }\n"
2800 "}",
2801 "switch (n) {\n"
2802 "case 0: {\n"
2803 " return false;\n"
2804 "}\n"
2805 "case 1:\n"
2806 " break;\n"
2807 "default: {\n"
2808 " return true;\n"
2809 "}\n"
2810 "}",
2811 Style);
2814 TEST_F(FormatTest, CaseRanges) {
2815 verifyFormat("switch (x) {\n"
2816 "case 'A' ... 'Z':\n"
2817 "case 1 ... 5:\n"
2818 "case a ... b:\n"
2819 " break;\n"
2820 "}");
2823 TEST_F(FormatTest, ShortEnums) {
2824 FormatStyle Style = getLLVMStyle();
2825 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2826 EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2827 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2828 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2829 Style.AllowShortEnumsOnASingleLine = false;
2830 verifyFormat("enum {\n"
2831 " A,\n"
2832 " B,\n"
2833 " C\n"
2834 "} ShortEnum1, ShortEnum2;",
2835 Style);
2836 verifyFormat("typedef enum {\n"
2837 " A,\n"
2838 " B,\n"
2839 " C\n"
2840 "} ShortEnum1, ShortEnum2;",
2841 Style);
2842 verifyFormat("enum {\n"
2843 " A,\n"
2844 "} ShortEnum1, ShortEnum2;",
2845 Style);
2846 verifyFormat("typedef enum {\n"
2847 " A,\n"
2848 "} ShortEnum1, ShortEnum2;",
2849 Style);
2850 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2851 Style.BraceWrapping.AfterEnum = true;
2852 verifyFormat("enum\n"
2853 "{\n"
2854 " A,\n"
2855 " B,\n"
2856 " C\n"
2857 "} ShortEnum1, ShortEnum2;",
2858 Style);
2859 verifyFormat("typedef enum\n"
2860 "{\n"
2861 " A,\n"
2862 " B,\n"
2863 " C\n"
2864 "} ShortEnum1, ShortEnum2;",
2865 Style);
2868 TEST_F(FormatTest, ShortCompoundRequirement) {
2869 FormatStyle Style = getLLVMStyle();
2870 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2871 verifyFormat("template <typename T>\n"
2872 "concept c = requires(T x) {\n"
2873 " { x + 1 } -> std::same_as<int>;\n"
2874 "};",
2875 Style);
2876 verifyFormat("template <typename T>\n"
2877 "concept c = requires(T x) {\n"
2878 " { x + 1 } -> std::same_as<int>;\n"
2879 " { x + 2 } -> std::same_as<int>;\n"
2880 "};",
2881 Style);
2882 Style.AllowShortCompoundRequirementOnASingleLine = false;
2883 verifyFormat("template <typename T>\n"
2884 "concept c = requires(T x) {\n"
2885 " {\n"
2886 " x + 1\n"
2887 " } -> std::same_as<int>;\n"
2888 "};",
2889 Style);
2890 verifyFormat("template <typename T>\n"
2891 "concept c = requires(T x) {\n"
2892 " {\n"
2893 " x + 1\n"
2894 " } -> std::same_as<int>;\n"
2895 " {\n"
2896 " x + 2\n"
2897 " } -> std::same_as<int>;\n"
2898 "};",
2899 Style);
2902 TEST_F(FormatTest, ShortCaseLabels) {
2903 FormatStyle Style = getLLVMStyle();
2904 Style.AllowShortCaseLabelsOnASingleLine = true;
2905 verifyFormat("switch (a) {\n"
2906 "case 1: x = 1; break;\n"
2907 "case 2: return;\n"
2908 "case 3:\n"
2909 "case 4:\n"
2910 "case 5: return;\n"
2911 "case 6: // comment\n"
2912 " return;\n"
2913 "case 7:\n"
2914 " // comment\n"
2915 " return;\n"
2916 "case 8:\n"
2917 " x = 8; // comment\n"
2918 " break;\n"
2919 "default: y = 1; break;\n"
2920 "}",
2921 Style);
2922 verifyFormat("switch (a) {\n"
2923 "case 0: return; // comment\n"
2924 "case 1: break; // comment\n"
2925 "case 2: return;\n"
2926 "// comment\n"
2927 "case 3: return;\n"
2928 "// comment 1\n"
2929 "// comment 2\n"
2930 "// comment 3\n"
2931 "case 4: break; /* comment */\n"
2932 "case 5:\n"
2933 " // comment\n"
2934 " break;\n"
2935 "case 6: /* comment */ x = 1; break;\n"
2936 "case 7: x = /* comment */ 1; break;\n"
2937 "case 8:\n"
2938 " x = 1; /* comment */\n"
2939 " break;\n"
2940 "case 9:\n"
2941 " break; // comment line 1\n"
2942 " // comment line 2\n"
2943 "}",
2944 Style);
2945 verifyFormat("switch (a) {\n"
2946 "case 1:\n"
2947 " x = 8;\n"
2948 " // fall through\n"
2949 "case 2: x = 8;\n"
2950 "// comment\n"
2951 "case 3:\n"
2952 " return; /* comment line 1\n"
2953 " * comment line 2 */\n"
2954 "case 4: i = 8;\n"
2955 "// something else\n"
2956 "#if FOO\n"
2957 "case 5: break;\n"
2958 "#endif\n"
2959 "}",
2960 "switch (a) {\n"
2961 "case 1: x = 8;\n"
2962 " // fall through\n"
2963 "case 2:\n"
2964 " x = 8;\n"
2965 "// comment\n"
2966 "case 3:\n"
2967 " return; /* comment line 1\n"
2968 " * comment line 2 */\n"
2969 "case 4:\n"
2970 " i = 8;\n"
2971 "// something else\n"
2972 "#if FOO\n"
2973 "case 5: break;\n"
2974 "#endif\n"
2975 "}",
2976 Style);
2977 verifyFormat("switch (a) {\n"
2978 "case 0:\n"
2979 " return; // long long long long long long long long long long "
2980 "long long comment\n"
2981 " // line\n"
2982 "}",
2983 "switch (a) {\n"
2984 "case 0: return; // long long long long long long long long "
2985 "long long long long comment line\n"
2986 "}",
2987 Style);
2988 verifyFormat("switch (a) {\n"
2989 "case 0:\n"
2990 " return; /* long long long long long long long long long long "
2991 "long long comment\n"
2992 " line */\n"
2993 "}",
2994 "switch (a) {\n"
2995 "case 0: return; /* long long long long long long long long "
2996 "long long long long comment line */\n"
2997 "}",
2998 Style);
2999 verifyFormat("switch (a) {\n"
3000 "#if FOO\n"
3001 "case 0: return 0;\n"
3002 "#endif\n"
3003 "}",
3004 Style);
3005 verifyFormat("switch (a) {\n"
3006 "case 1: {\n"
3007 "}\n"
3008 "case 2: {\n"
3009 " return;\n"
3010 "}\n"
3011 "case 3: {\n"
3012 " x = 1;\n"
3013 " return;\n"
3014 "}\n"
3015 "case 4:\n"
3016 " if (x)\n"
3017 " return;\n"
3018 "}",
3019 Style);
3020 Style.ColumnLimit = 21;
3021 verifyFormat("#define X \\\n"
3022 " case 0: break;\n"
3023 "#include \"f\"",
3024 Style);
3025 verifyFormat("switch (a) {\n"
3026 "case 1: x = 1; break;\n"
3027 "case 2: return;\n"
3028 "case 3:\n"
3029 "case 4:\n"
3030 "case 5: return;\n"
3031 "default:\n"
3032 " y = 1;\n"
3033 " break;\n"
3034 "}",
3035 Style);
3036 Style.ColumnLimit = 80;
3037 Style.AllowShortCaseLabelsOnASingleLine = false;
3038 Style.IndentCaseLabels = true;
3039 verifyFormat("switch (n) {\n"
3040 " default /*comments*/:\n"
3041 " return true;\n"
3042 " case 0:\n"
3043 " return false;\n"
3044 "}",
3045 "switch (n) {\n"
3046 "default/*comments*/:\n"
3047 " return true;\n"
3048 "case 0:\n"
3049 " return false;\n"
3050 "}",
3051 Style);
3052 Style.AllowShortCaseLabelsOnASingleLine = true;
3053 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3054 Style.BraceWrapping.AfterCaseLabel = true;
3055 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3056 verifyFormat("switch (n)\n"
3057 "{\n"
3058 " case 0:\n"
3059 " {\n"
3060 " return false;\n"
3061 " }\n"
3062 " default:\n"
3063 " {\n"
3064 " return true;\n"
3065 " }\n"
3066 "}",
3067 "switch (n) {\n"
3068 " case 0: {\n"
3069 " return false;\n"
3070 " }\n"
3071 " default:\n"
3072 " {\n"
3073 " return true;\n"
3074 " }\n"
3075 "}",
3076 Style);
3079 TEST_F(FormatTest, FormatsLabels) {
3080 verifyFormat("void f() {\n"
3081 " some_code();\n"
3082 "test_label:\n"
3083 " some_other_code();\n"
3084 " {\n"
3085 " some_more_code();\n"
3086 " another_label:\n"
3087 " some_more_code();\n"
3088 " }\n"
3089 "}");
3090 verifyFormat("{\n"
3091 " some_code();\n"
3092 "test_label:\n"
3093 " some_other_code();\n"
3094 "}");
3095 verifyFormat("{\n"
3096 " some_code();\n"
3097 "test_label:;\n"
3098 " int i = 0;\n"
3099 "}");
3100 verifyFormat("{\n"
3101 " some_code();\n"
3102 "test_label: { some_other_code(); }\n"
3103 "}");
3104 verifyFormat("{\n"
3105 " some_code();\n"
3106 "test_label: {\n"
3107 " some_other_code();\n"
3108 " some_other_code();\n"
3109 "}\n"
3110 "}");
3111 verifyFormat("{\n"
3112 "L0:\n"
3113 "[[foo]] L1:\n"
3114 "[[bar]] [[baz]] L2:\n"
3115 " g();\n"
3116 "}");
3117 verifyFormat("{\n"
3118 "[[foo]] L1: {\n"
3119 "[[bar]] [[baz]] L2:\n"
3120 " g();\n"
3121 "}\n"
3122 "}");
3123 verifyFormat("{\n"
3124 "[[foo]] L1:\n"
3125 " f();\n"
3126 " {\n"
3127 " [[bar]] [[baz]] L2:\n"
3128 " g();\n"
3129 " }\n"
3130 "}");
3132 FormatStyle Style = getLLVMStyle();
3133 Style.IndentGotoLabels = false;
3134 verifyFormat("void f() {\n"
3135 " some_code();\n"
3136 "test_label:\n"
3137 " some_other_code();\n"
3138 " {\n"
3139 " some_more_code();\n"
3140 "another_label:\n"
3141 " some_more_code();\n"
3142 " }\n"
3143 "}",
3144 Style);
3145 verifyFormat("{\n"
3146 " some_code();\n"
3147 "test_label:\n"
3148 " some_other_code();\n"
3149 "}",
3150 Style);
3151 verifyFormat("{\n"
3152 " some_code();\n"
3153 "test_label:;\n"
3154 " int i = 0;\n"
3155 "}",
3156 Style);
3157 verifyFormat("{\n"
3158 " some_code();\n"
3159 "test_label: { some_other_code(); }\n"
3160 "}",
3161 Style);
3162 verifyFormat("{\n"
3163 "[[foo]] L1:\n"
3164 " f();\n"
3165 " {\n"
3166 "[[bar]] [[baz]] L2:\n"
3167 " g();\n"
3168 " }\n"
3169 "}",
3170 Style);
3172 Style.ColumnLimit = 15;
3173 verifyFormat("#define FOO \\\n"
3174 "label: \\\n"
3175 " break;",
3176 Style);
3178 // The opening brace may either be on the same unwrapped line as the colon or
3179 // on a separate one. The formatter should recognize both.
3180 Style = getLLVMStyle();
3181 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman;
3182 verifyFormat("{\n"
3183 " some_code();\n"
3184 "test_label:\n"
3185 "{\n"
3186 " some_other_code();\n"
3187 "}\n"
3188 "}",
3189 Style);
3190 verifyFormat("{\n"
3191 "[[foo]] L1:\n"
3192 "{\n"
3193 "[[bar]] [[baz]] L2:\n"
3194 " g();\n"
3195 "}\n"
3196 "}",
3197 Style);
3200 TEST_F(FormatTest, MultiLineControlStatements) {
3201 FormatStyle Style = getLLVMStyleWithColumns(20);
3202 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3203 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3204 // Short lines should keep opening brace on same line.
3205 verifyFormat("if (foo) {\n"
3206 " bar();\n"
3207 "}",
3208 "if(foo){bar();}", Style);
3209 verifyFormat("if (foo) {\n"
3210 " bar();\n"
3211 "} else {\n"
3212 " baz();\n"
3213 "}",
3214 "if(foo){bar();}else{baz();}", Style);
3215 verifyFormat("if (foo && bar) {\n"
3216 " baz();\n"
3217 "}",
3218 "if(foo&&bar){baz();}", Style);
3219 verifyFormat("if (foo) {\n"
3220 " bar();\n"
3221 "} else if (baz) {\n"
3222 " quux();\n"
3223 "}",
3224 "if(foo){bar();}else if(baz){quux();}", Style);
3225 verifyFormat("if (foo) {\n"
3226 " bar();\n"
3227 "} else if (baz) {\n"
3228 " quux();\n"
3229 "} else {\n"
3230 " foobar();\n"
3231 "}",
3232 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3233 verifyFormat("for (;;) {\n"
3234 " foo();\n"
3235 "}",
3236 "for(;;){foo();}");
3237 verifyFormat("while (1) {\n"
3238 " foo();\n"
3239 "}",
3240 "while(1){foo();}", Style);
3241 verifyFormat("switch (foo) {\n"
3242 "case bar:\n"
3243 " return;\n"
3244 "}",
3245 "switch(foo){case bar:return;}", Style);
3246 verifyFormat("try {\n"
3247 " foo();\n"
3248 "} catch (...) {\n"
3249 " bar();\n"
3250 "}",
3251 "try{foo();}catch(...){bar();}", Style);
3252 verifyFormat("do {\n"
3253 " foo();\n"
3254 "} while (bar &&\n"
3255 " baz);",
3256 "do{foo();}while(bar&&baz);", Style);
3257 // Long lines should put opening brace on new line.
3258 verifyFormat("void f() {\n"
3259 " if (a1 && a2 &&\n"
3260 " a3)\n"
3261 " {\n"
3262 " quux();\n"
3263 " }\n"
3264 "}",
3265 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3266 verifyFormat("if (foo && bar &&\n"
3267 " baz)\n"
3268 "{\n"
3269 " quux();\n"
3270 "}",
3271 "if(foo&&bar&&baz){quux();}", Style);
3272 verifyFormat("if (foo && bar &&\n"
3273 " baz)\n"
3274 "{\n"
3275 " quux();\n"
3276 "}",
3277 "if (foo && bar &&\n"
3278 " baz) {\n"
3279 " quux();\n"
3280 "}",
3281 Style);
3282 verifyFormat("if (foo) {\n"
3283 " bar();\n"
3284 "} else if (baz ||\n"
3285 " quux)\n"
3286 "{\n"
3287 " foobar();\n"
3288 "}",
3289 "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3290 verifyFormat("if (foo) {\n"
3291 " bar();\n"
3292 "} else if (baz ||\n"
3293 " quux)\n"
3294 "{\n"
3295 " foobar();\n"
3296 "} else {\n"
3297 " barbaz();\n"
3298 "}",
3299 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3300 Style);
3301 verifyFormat("for (int i = 0;\n"
3302 " i < 10; ++i)\n"
3303 "{\n"
3304 " foo();\n"
3305 "}",
3306 "for(int i=0;i<10;++i){foo();}", Style);
3307 verifyFormat("foreach (int i,\n"
3308 " list)\n"
3309 "{\n"
3310 " foo();\n"
3311 "}",
3312 "foreach(int i, list){foo();}", Style);
3313 Style.ColumnLimit =
3314 40; // to concentrate at brace wrapping, not line wrap due to column limit
3315 verifyFormat("foreach (int i, list) {\n"
3316 " foo();\n"
3317 "}",
3318 "foreach(int i, list){foo();}", Style);
3319 Style.ColumnLimit =
3320 20; // to concentrate at brace wrapping, not line wrap due to column limit
3321 verifyFormat("while (foo || bar ||\n"
3322 " baz)\n"
3323 "{\n"
3324 " quux();\n"
3325 "}",
3326 "while(foo||bar||baz){quux();}", Style);
3327 verifyFormat("switch (\n"
3328 " foo = barbaz)\n"
3329 "{\n"
3330 "case quux:\n"
3331 " return;\n"
3332 "}",
3333 "switch(foo=barbaz){case quux:return;}", Style);
3334 verifyFormat("try {\n"
3335 " foo();\n"
3336 "} catch (\n"
3337 " Exception &bar)\n"
3338 "{\n"
3339 " baz();\n"
3340 "}",
3341 "try{foo();}catch(Exception&bar){baz();}", Style);
3342 Style.ColumnLimit =
3343 40; // to concentrate at brace wrapping, not line wrap due to column limit
3344 verifyFormat("try {\n"
3345 " foo();\n"
3346 "} catch (Exception &bar) {\n"
3347 " baz();\n"
3348 "}",
3349 "try{foo();}catch(Exception&bar){baz();}", Style);
3350 Style.ColumnLimit =
3351 20; // to concentrate at brace wrapping, not line wrap due to column limit
3353 Style.BraceWrapping.BeforeElse = true;
3354 verifyFormat("if (foo) {\n"
3355 " bar();\n"
3356 "}\n"
3357 "else if (baz ||\n"
3358 " quux)\n"
3359 "{\n"
3360 " foobar();\n"
3361 "}\n"
3362 "else {\n"
3363 " barbaz();\n"
3364 "}",
3365 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3366 Style);
3368 Style.BraceWrapping.BeforeCatch = true;
3369 verifyFormat("try {\n"
3370 " foo();\n"
3371 "}\n"
3372 "catch (...) {\n"
3373 " baz();\n"
3374 "}",
3375 "try{foo();}catch(...){baz();}", Style);
3377 Style.BraceWrapping.AfterFunction = true;
3378 Style.BraceWrapping.AfterStruct = false;
3379 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3380 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3381 Style.ColumnLimit = 80;
3382 verifyFormat("void shortfunction() { bar(); }", Style);
3383 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3384 verifyFormat("struct T {};", Style);
3386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3387 verifyFormat("void shortfunction()\n"
3388 "{\n"
3389 " bar();\n"
3390 "}",
3391 Style);
3392 verifyFormat("struct T shortfunction()\n"
3393 "{\n"
3394 " return bar();\n"
3395 "}",
3396 Style);
3397 verifyFormat("struct T {};", Style);
3399 Style.BraceWrapping.AfterFunction = false;
3400 Style.BraceWrapping.AfterStruct = true;
3401 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3402 verifyFormat("void shortfunction() { bar(); }", Style);
3403 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3404 verifyFormat("struct T\n"
3405 "{\n"
3406 "};",
3407 Style);
3409 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3410 verifyFormat("void shortfunction() {\n"
3411 " bar();\n"
3412 "}",
3413 Style);
3414 verifyFormat("struct T shortfunction() {\n"
3415 " return bar();\n"
3416 "}",
3417 Style);
3418 verifyFormat("struct T\n"
3419 "{\n"
3420 "};",
3421 Style);
3424 TEST_F(FormatTest, BeforeWhile) {
3425 FormatStyle Style = getLLVMStyle();
3426 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3428 verifyFormat("do {\n"
3429 " foo();\n"
3430 "} while (1);",
3431 Style);
3432 Style.BraceWrapping.BeforeWhile = true;
3433 verifyFormat("do {\n"
3434 " foo();\n"
3435 "}\n"
3436 "while (1);",
3437 Style);
3440 //===----------------------------------------------------------------------===//
3441 // Tests for classes, namespaces, etc.
3442 //===----------------------------------------------------------------------===//
3444 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3445 verifyFormat("class A {};");
3448 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3449 verifyFormat("class A {\n"
3450 "public:\n"
3451 "public: // comment\n"
3452 "protected:\n"
3453 "private:\n"
3454 " void f() {}\n"
3455 "};");
3456 verifyFormat("export class A {\n"
3457 "public:\n"
3458 "public: // comment\n"
3459 "protected:\n"
3460 "private:\n"
3461 " void f() {}\n"
3462 "};");
3463 verifyGoogleFormat("class A {\n"
3464 " public:\n"
3465 " protected:\n"
3466 " private:\n"
3467 " void f() {}\n"
3468 "};");
3469 verifyGoogleFormat("export class A {\n"
3470 " public:\n"
3471 " protected:\n"
3472 " private:\n"
3473 " void f() {}\n"
3474 "};");
3475 verifyFormat("class A {\n"
3476 "public slots:\n"
3477 " void f1() {}\n"
3478 "public Q_SLOTS:\n"
3479 " void f2() {}\n"
3480 "protected slots:\n"
3481 " void f3() {}\n"
3482 "protected Q_SLOTS:\n"
3483 " void f4() {}\n"
3484 "private slots:\n"
3485 " void f5() {}\n"
3486 "private Q_SLOTS:\n"
3487 " void f6() {}\n"
3488 "signals:\n"
3489 " void g1();\n"
3490 "Q_SIGNALS:\n"
3491 " void g2();\n"
3492 "};");
3494 // Don't interpret 'signals' the wrong way.
3495 verifyFormat("signals.set();");
3496 verifyFormat("for (Signals signals : f()) {\n}");
3497 verifyFormat("{\n"
3498 " signals.set(); // This needs indentation.\n"
3499 "}");
3500 verifyFormat("void f() {\n"
3501 "label:\n"
3502 " signals.baz();\n"
3503 "}");
3504 verifyFormat("private[1];");
3505 verifyFormat("testArray[public] = 1;");
3506 verifyFormat("public();");
3507 verifyFormat("myFunc(public);");
3508 verifyFormat("std::vector<int> testVec = {private};");
3509 verifyFormat("private.p = 1;");
3510 verifyFormat("void function(private...) {};");
3511 verifyFormat("if (private && public)");
3512 verifyFormat("private &= true;");
3513 verifyFormat("int x = private * public;");
3514 verifyFormat("public *= private;");
3515 verifyFormat("int x = public + private;");
3516 verifyFormat("private++;");
3517 verifyFormat("++private;");
3518 verifyFormat("public += private;");
3519 verifyFormat("public = public - private;");
3520 verifyFormat("public->foo();");
3521 verifyFormat("private--;");
3522 verifyFormat("--private;");
3523 verifyFormat("public -= 1;");
3524 verifyFormat("if (!private && !public)");
3525 verifyFormat("public != private;");
3526 verifyFormat("int x = public / private;");
3527 verifyFormat("public /= 2;");
3528 verifyFormat("public = public % 2;");
3529 verifyFormat("public %= 2;");
3530 verifyFormat("if (public < private)");
3531 verifyFormat("public << private;");
3532 verifyFormat("public <<= private;");
3533 verifyFormat("if (public > private)");
3534 verifyFormat("public >> private;");
3535 verifyFormat("public >>= private;");
3536 verifyFormat("public ^ private;");
3537 verifyFormat("public ^= private;");
3538 verifyFormat("public | private;");
3539 verifyFormat("public |= private;");
3540 verifyFormat("auto x = private ? 1 : 2;");
3541 verifyFormat("if (public == private)");
3542 verifyFormat("void foo(public, private)");
3543 verifyFormat("public::foo();");
3545 verifyFormat("class A {\n"
3546 "public:\n"
3547 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3548 "\n"
3549 "private:\n"
3550 " int c;\n"
3551 "};\n"
3552 "class B {\n"
3553 "public:\n"
3554 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3555 "\n"
3556 "private:\n"
3557 " int c;\n"
3558 "};");
3561 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3562 verifyFormat("class A {\n"
3563 "public:\n"
3564 " void f();\n"
3565 "\n"
3566 "private:\n"
3567 " void g() {}\n"
3568 " // test\n"
3569 "protected:\n"
3570 " int h;\n"
3571 "};",
3572 "class A {\n"
3573 "public:\n"
3574 "void f();\n"
3575 "private:\n"
3576 "void g() {}\n"
3577 "// test\n"
3578 "protected:\n"
3579 "int h;\n"
3580 "};");
3581 verifyFormat("class A {\n"
3582 "protected:\n"
3583 "public:\n"
3584 " void f();\n"
3585 "};",
3586 "class A {\n"
3587 "protected:\n"
3588 "\n"
3589 "public:\n"
3590 "\n"
3591 " void f();\n"
3592 "};");
3594 // Even ensure proper spacing inside macros.
3595 verifyFormat("#define B \\\n"
3596 " class A { \\\n"
3597 " protected: \\\n"
3598 " public: \\\n"
3599 " void f(); \\\n"
3600 " };",
3601 "#define B \\\n"
3602 " class A { \\\n"
3603 " protected: \\\n"
3604 " \\\n"
3605 " public: \\\n"
3606 " \\\n"
3607 " void f(); \\\n"
3608 " };",
3609 getGoogleStyle());
3610 // But don't remove empty lines after macros ending in access specifiers.
3611 verifyFormat("#define A private:\n"
3612 "\n"
3613 "int i;",
3614 "#define A private:\n"
3615 "\n"
3616 "int i;");
3619 TEST_F(FormatTest, FormatsClasses) {
3620 verifyFormat("class A : public B {};");
3621 verifyFormat("class A : public ::B {};");
3623 verifyFormat(
3624 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3625 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3626 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3627 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3628 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3629 verifyFormat(
3630 "class A : public B, public C, public D, public E, public F {};");
3631 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3632 " public C,\n"
3633 " public D,\n"
3634 " public E,\n"
3635 " public F,\n"
3636 " public G {};");
3638 verifyFormat("class\n"
3639 " ReallyReallyLongClassName {\n"
3640 " int i;\n"
3641 "};",
3642 getLLVMStyleWithColumns(32));
3643 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3644 " aaaaaaaaaaaaaaaa> {};");
3645 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3646 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3647 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3648 verifyFormat("template <class R, class C>\n"
3649 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3650 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3651 verifyFormat("class ::A::B {};");
3654 TEST_F(FormatTest, BreakInheritanceStyle) {
3655 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3656 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3657 FormatStyle::BILS_BeforeComma;
3658 verifyFormat("class MyClass : public X {};",
3659 StyleWithInheritanceBreakBeforeComma);
3660 verifyFormat("class MyClass\n"
3661 " : public X\n"
3662 " , public Y {};",
3663 StyleWithInheritanceBreakBeforeComma);
3664 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3665 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3666 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3667 StyleWithInheritanceBreakBeforeComma);
3668 verifyFormat("struct aaaaaaaaaaaaa\n"
3669 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3670 " aaaaaaaaaaaaaaaa> {};",
3671 StyleWithInheritanceBreakBeforeComma);
3673 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3674 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3675 FormatStyle::BILS_AfterColon;
3676 verifyFormat("class MyClass : public X {};",
3677 StyleWithInheritanceBreakAfterColon);
3678 verifyFormat("class MyClass : public X, public Y {};",
3679 StyleWithInheritanceBreakAfterColon);
3680 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3681 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3682 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3683 StyleWithInheritanceBreakAfterColon);
3684 verifyFormat("struct aaaaaaaaaaaaa :\n"
3685 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3686 " aaaaaaaaaaaaaaaa> {};",
3687 StyleWithInheritanceBreakAfterColon);
3689 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3690 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3691 FormatStyle::BILS_AfterComma;
3692 verifyFormat("class MyClass : public X {};",
3693 StyleWithInheritanceBreakAfterComma);
3694 verifyFormat("class MyClass : public X,\n"
3695 " public Y {};",
3696 StyleWithInheritanceBreakAfterComma);
3697 verifyFormat(
3698 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3699 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3700 "{};",
3701 StyleWithInheritanceBreakAfterComma);
3702 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3703 " aaaaaaaaaaaaaaaa> {};",
3704 StyleWithInheritanceBreakAfterComma);
3705 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3706 " : public OnceBreak,\n"
3707 " public AlwaysBreak,\n"
3708 " EvenBasesFitInOneLine {};",
3709 StyleWithInheritanceBreakAfterComma);
3712 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3713 verifyFormat("class A {\n} a, b;");
3714 verifyFormat("struct A {\n} a, b;");
3715 verifyFormat("union A {\n} a, b;");
3717 verifyFormat("constexpr class A {\n} a, b;");
3718 verifyFormat("constexpr struct A {\n} a, b;");
3719 verifyFormat("constexpr union A {\n} a, b;");
3721 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3722 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3723 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3725 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3726 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3727 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3729 verifyFormat("namespace ns {\n"
3730 "class {\n"
3731 "} a, b;\n"
3732 "} // namespace ns");
3733 verifyFormat("namespace ns {\n"
3734 "const class {\n"
3735 "} a, b;\n"
3736 "} // namespace ns");
3737 verifyFormat("namespace ns {\n"
3738 "constexpr class C {\n"
3739 "} a, b;\n"
3740 "} // namespace ns");
3741 verifyFormat("namespace ns {\n"
3742 "class { /* comment */\n"
3743 "} a, b;\n"
3744 "} // namespace ns");
3745 verifyFormat("namespace ns {\n"
3746 "const class { /* comment */\n"
3747 "} a, b;\n"
3748 "} // namespace ns");
3751 TEST_F(FormatTest, FormatsEnum) {
3752 verifyFormat("enum {\n"
3753 " Zero,\n"
3754 " One = 1,\n"
3755 " Two = One + 1,\n"
3756 " Three = (One + Two),\n"
3757 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3758 " Five = (One, Two, Three, Four, 5)\n"
3759 "};");
3760 verifyGoogleFormat("enum {\n"
3761 " Zero,\n"
3762 " One = 1,\n"
3763 " Two = One + 1,\n"
3764 " Three = (One + Two),\n"
3765 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3766 " Five = (One, Two, Three, Four, 5)\n"
3767 "};");
3768 verifyFormat("enum Enum {};");
3769 verifyFormat("enum {};");
3770 verifyFormat("enum X E {} d;");
3771 verifyFormat("enum __attribute__((...)) E {} d;");
3772 verifyFormat("enum __declspec__((...)) E {} d;");
3773 verifyFormat("enum [[nodiscard]] E {} d;");
3774 verifyFormat("enum {\n"
3775 " Bar = Foo<int, int>::value\n"
3776 "};",
3777 getLLVMStyleWithColumns(30));
3779 verifyFormat("enum ShortEnum { A, B, C };");
3780 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3782 verifyFormat("enum KeepEmptyLines {\n"
3783 " ONE,\n"
3784 "\n"
3785 " TWO,\n"
3786 "\n"
3787 " THREE\n"
3788 "}",
3789 "enum KeepEmptyLines {\n"
3790 " ONE,\n"
3791 "\n"
3792 " TWO,\n"
3793 "\n"
3794 "\n"
3795 " THREE\n"
3796 "}");
3797 verifyFormat("enum E { // comment\n"
3798 " ONE,\n"
3799 " TWO\n"
3800 "};\n"
3801 "int i;");
3803 FormatStyle EightIndent = getLLVMStyle();
3804 EightIndent.IndentWidth = 8;
3805 verifyFormat("enum {\n"
3806 " VOID,\n"
3807 " CHAR,\n"
3808 " SHORT,\n"
3809 " INT,\n"
3810 " LONG,\n"
3811 " SIGNED,\n"
3812 " UNSIGNED,\n"
3813 " BOOL,\n"
3814 " FLOAT,\n"
3815 " DOUBLE,\n"
3816 " COMPLEX\n"
3817 "};",
3818 EightIndent);
3820 verifyFormat("enum [[nodiscard]] E {\n"
3821 " ONE,\n"
3822 " TWO,\n"
3823 "};");
3824 verifyFormat("enum [[nodiscard]] E {\n"
3825 " // Comment 1\n"
3826 " ONE,\n"
3827 " // Comment 2\n"
3828 " TWO,\n"
3829 "};");
3830 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3831 " // Comment 1\n"
3832 " ONE,\n"
3833 " // Comment 2\n"
3834 " TWO\n"
3835 "};");
3836 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3837 " // Comment 1\n"
3838 " ONE,\n"
3839 " // Comment 2\n"
3840 " TWO\n"
3841 "};");
3842 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3843 " A,\n"
3844 " // bar\n"
3845 " B\n"
3846 "};",
3847 "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3848 " A,\n"
3849 " // bar\n"
3850 " B};");
3852 // Not enums.
3853 verifyFormat("enum X f() {\n"
3854 " a();\n"
3855 " return 42;\n"
3856 "}");
3857 verifyFormat("enum X Type::f() {\n"
3858 " a();\n"
3859 " return 42;\n"
3860 "}");
3861 verifyFormat("enum ::X f() {\n"
3862 " a();\n"
3863 " return 42;\n"
3864 "}");
3865 verifyFormat("enum ns::X f() {\n"
3866 " a();\n"
3867 " return 42;\n"
3868 "}");
3871 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3872 verifyFormat("enum Type {\n"
3873 " One = 0; // These semicolons should be commas.\n"
3874 " Two = 1;\n"
3875 "};");
3876 verifyFormat("namespace n {\n"
3877 "enum Type {\n"
3878 " One,\n"
3879 " Two, // missing };\n"
3880 " int i;\n"
3881 "}\n"
3882 "void g() {}");
3885 TEST_F(FormatTest, FormatsEnumStruct) {
3886 verifyFormat("enum struct {\n"
3887 " Zero,\n"
3888 " One = 1,\n"
3889 " Two = One + 1,\n"
3890 " Three = (One + Two),\n"
3891 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3892 " Five = (One, Two, Three, Four, 5)\n"
3893 "};");
3894 verifyFormat("enum struct Enum {};");
3895 verifyFormat("enum struct {};");
3896 verifyFormat("enum struct X E {} d;");
3897 verifyFormat("enum struct __attribute__((...)) E {} d;");
3898 verifyFormat("enum struct __declspec__((...)) E {} d;");
3899 verifyFormat("enum struct [[nodiscard]] E {} d;");
3900 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3902 verifyFormat("enum struct [[nodiscard]] E {\n"
3903 " ONE,\n"
3904 " TWO,\n"
3905 "};");
3906 verifyFormat("enum struct [[nodiscard]] E {\n"
3907 " // Comment 1\n"
3908 " ONE,\n"
3909 " // Comment 2\n"
3910 " TWO,\n"
3911 "};");
3914 TEST_F(FormatTest, FormatsEnumClass) {
3915 verifyFormat("enum class {\n"
3916 " Zero,\n"
3917 " One = 1,\n"
3918 " Two = One + 1,\n"
3919 " Three = (One + Two),\n"
3920 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3921 " Five = (One, Two, Three, Four, 5)\n"
3922 "};");
3923 verifyFormat("enum class Enum {};");
3924 verifyFormat("enum class {};");
3925 verifyFormat("enum class X E {} d;");
3926 verifyFormat("enum class __attribute__((...)) E {} d;");
3927 verifyFormat("enum class __declspec__((...)) E {} d;");
3928 verifyFormat("enum class [[nodiscard]] E {} d;");
3929 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3931 verifyFormat("enum class [[nodiscard]] E {\n"
3932 " ONE,\n"
3933 " TWO,\n"
3934 "};");
3935 verifyFormat("enum class [[nodiscard]] E {\n"
3936 " // Comment 1\n"
3937 " ONE,\n"
3938 " // Comment 2\n"
3939 " TWO,\n"
3940 "};");
3943 TEST_F(FormatTest, FormatsEnumTypes) {
3944 verifyFormat("enum X : int {\n"
3945 " A, // Force multiple lines.\n"
3946 " B\n"
3947 "};");
3948 verifyFormat("enum X : int { A, B };");
3949 verifyFormat("enum X : std::uint32_t { A, B };");
3952 TEST_F(FormatTest, FormatsTypedefEnum) {
3953 FormatStyle Style = getLLVMStyleWithColumns(40);
3954 verifyFormat("typedef enum {} EmptyEnum;");
3955 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3956 verifyFormat("typedef enum {\n"
3957 " ZERO = 0,\n"
3958 " ONE = 1,\n"
3959 " TWO = 2,\n"
3960 " THREE = 3\n"
3961 "} LongEnum;",
3962 Style);
3963 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3964 Style.BraceWrapping.AfterEnum = true;
3965 verifyFormat("typedef enum {} EmptyEnum;");
3966 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3967 verifyFormat("typedef enum\n"
3968 "{\n"
3969 " ZERO = 0,\n"
3970 " ONE = 1,\n"
3971 " TWO = 2,\n"
3972 " THREE = 3\n"
3973 "} LongEnum;",
3974 Style);
3977 TEST_F(FormatTest, FormatsNSEnums) {
3978 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3979 verifyGoogleFormat(
3980 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3981 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3982 " // Information about someDecentlyLongValue.\n"
3983 " someDecentlyLongValue,\n"
3984 " // Information about anotherDecentlyLongValue.\n"
3985 " anotherDecentlyLongValue,\n"
3986 " // Information about aThirdDecentlyLongValue.\n"
3987 " aThirdDecentlyLongValue\n"
3988 "};");
3989 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3990 " // Information about someDecentlyLongValue.\n"
3991 " someDecentlyLongValue,\n"
3992 " // Information about anotherDecentlyLongValue.\n"
3993 " anotherDecentlyLongValue,\n"
3994 " // Information about aThirdDecentlyLongValue.\n"
3995 " aThirdDecentlyLongValue\n"
3996 "};");
3997 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3998 " a = 1,\n"
3999 " b = 2,\n"
4000 " c = 3,\n"
4001 "};");
4002 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
4003 " a = 1,\n"
4004 " b = 2,\n"
4005 " c = 3,\n"
4006 "};");
4007 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
4008 " a = 1,\n"
4009 " b = 2,\n"
4010 " c = 3,\n"
4011 "};");
4012 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4013 " a = 1,\n"
4014 " b = 2,\n"
4015 " c = 3,\n"
4016 "};");
4019 TEST_F(FormatTest, FormatsBitfields) {
4020 verifyFormat("struct Bitfields {\n"
4021 " unsigned sClass : 8;\n"
4022 " unsigned ValueKind : 2;\n"
4023 "};");
4024 verifyFormat("struct A {\n"
4025 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4026 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4027 "};");
4028 verifyFormat("struct MyStruct {\n"
4029 " uchar data;\n"
4030 " uchar : 8;\n"
4031 " uchar : 8;\n"
4032 " uchar other;\n"
4033 "};");
4034 FormatStyle Style = getLLVMStyle();
4035 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
4036 verifyFormat("struct Bitfields {\n"
4037 " unsigned sClass:8;\n"
4038 " unsigned ValueKind:2;\n"
4039 " uchar other;\n"
4040 "};",
4041 Style);
4042 verifyFormat("struct A {\n"
4043 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4044 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4045 "};",
4046 Style);
4047 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
4048 verifyFormat("struct Bitfields {\n"
4049 " unsigned sClass :8;\n"
4050 " unsigned ValueKind :2;\n"
4051 " uchar other;\n"
4052 "};",
4053 Style);
4054 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
4055 verifyFormat("struct Bitfields {\n"
4056 " unsigned sClass: 8;\n"
4057 " unsigned ValueKind: 2;\n"
4058 " uchar other;\n"
4059 "};",
4060 Style);
4063 TEST_F(FormatTest, FormatsNamespaces) {
4064 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
4065 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
4067 verifyFormat("namespace some_namespace {\n"
4068 "class A {};\n"
4069 "void f() { f(); }\n"
4070 "}",
4071 LLVMWithNoNamespaceFix);
4072 verifyFormat("#define M(x) x##x\n"
4073 "namespace M(x) {\n"
4074 "class A {};\n"
4075 "void f() { f(); }\n"
4076 "}",
4077 LLVMWithNoNamespaceFix);
4078 verifyFormat("#define M(x) x##x\n"
4079 "namespace N::inline M(x) {\n"
4080 "class A {};\n"
4081 "void f() { f(); }\n"
4082 "}",
4083 LLVMWithNoNamespaceFix);
4084 verifyFormat("#define M(x) x##x\n"
4085 "namespace M(x)::inline N {\n"
4086 "class A {};\n"
4087 "void f() { f(); }\n"
4088 "}",
4089 LLVMWithNoNamespaceFix);
4090 verifyFormat("#define M(x) x##x\n"
4091 "namespace N::M(x) {\n"
4092 "class A {};\n"
4093 "void f() { f(); }\n"
4094 "}",
4095 LLVMWithNoNamespaceFix);
4096 verifyFormat("#define M(x) x##x\n"
4097 "namespace M::N(x) {\n"
4098 "class A {};\n"
4099 "void f() { f(); }\n"
4100 "}",
4101 LLVMWithNoNamespaceFix);
4102 verifyFormat("namespace N::inline D {\n"
4103 "class A {};\n"
4104 "void f() { f(); }\n"
4105 "}",
4106 LLVMWithNoNamespaceFix);
4107 verifyFormat("namespace N::inline D::E {\n"
4108 "class A {};\n"
4109 "void f() { f(); }\n"
4110 "}",
4111 LLVMWithNoNamespaceFix);
4112 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4113 "class A {};\n"
4114 "void f() { f(); }\n"
4115 "}",
4116 LLVMWithNoNamespaceFix);
4117 verifyFormat("/* something */ namespace some_namespace {\n"
4118 "class A {};\n"
4119 "void f() { f(); }\n"
4120 "}",
4121 LLVMWithNoNamespaceFix);
4122 verifyFormat("namespace {\n"
4123 "class A {};\n"
4124 "void f() { f(); }\n"
4125 "}",
4126 LLVMWithNoNamespaceFix);
4127 verifyFormat("/* something */ namespace {\n"
4128 "class A {};\n"
4129 "void f() { f(); }\n"
4130 "}",
4131 LLVMWithNoNamespaceFix);
4132 verifyFormat("inline namespace X {\n"
4133 "class A {};\n"
4134 "void f() { f(); }\n"
4135 "}",
4136 LLVMWithNoNamespaceFix);
4137 verifyFormat("/* something */ inline namespace X {\n"
4138 "class A {};\n"
4139 "void f() { f(); }\n"
4140 "}",
4141 LLVMWithNoNamespaceFix);
4142 verifyFormat("export namespace X {\n"
4143 "class A {};\n"
4144 "void f() { f(); }\n"
4145 "}",
4146 LLVMWithNoNamespaceFix);
4147 verifyFormat("using namespace some_namespace;\n"
4148 "class A {};\n"
4149 "void f() { f(); }",
4150 LLVMWithNoNamespaceFix);
4152 // This code is more common than we thought; if we
4153 // layout this correctly the semicolon will go into
4154 // its own line, which is undesirable.
4155 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4156 verifyFormat("namespace {\n"
4157 "class A {};\n"
4158 "};",
4159 LLVMWithNoNamespaceFix);
4161 verifyFormat("namespace {\n"
4162 "int SomeVariable = 0; // comment\n"
4163 "} // namespace",
4164 LLVMWithNoNamespaceFix);
4165 verifyFormat("#ifndef HEADER_GUARD\n"
4166 "#define HEADER_GUARD\n"
4167 "namespace my_namespace {\n"
4168 "int i;\n"
4169 "} // my_namespace\n"
4170 "#endif // HEADER_GUARD",
4171 "#ifndef HEADER_GUARD\n"
4172 " #define HEADER_GUARD\n"
4173 " namespace my_namespace {\n"
4174 "int i;\n"
4175 "} // my_namespace\n"
4176 "#endif // HEADER_GUARD",
4177 LLVMWithNoNamespaceFix);
4179 verifyFormat("namespace A::B {\n"
4180 "class C {};\n"
4181 "}",
4182 LLVMWithNoNamespaceFix);
4184 FormatStyle Style = getLLVMStyle();
4185 Style.NamespaceIndentation = FormatStyle::NI_All;
4186 verifyFormat("namespace out {\n"
4187 " int i;\n"
4188 " namespace in {\n"
4189 " int i;\n"
4190 " } // namespace in\n"
4191 "} // namespace out",
4192 "namespace out {\n"
4193 "int i;\n"
4194 "namespace in {\n"
4195 "int i;\n"
4196 "} // namespace in\n"
4197 "} // namespace out",
4198 Style);
4200 FormatStyle ShortInlineFunctions = getLLVMStyle();
4201 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4202 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4203 FormatStyle::SFS_Inline;
4204 verifyFormat("namespace {\n"
4205 " void f() {\n"
4206 " return;\n"
4207 " }\n"
4208 "} // namespace",
4209 ShortInlineFunctions);
4210 verifyFormat("namespace { /* comment */\n"
4211 " void f() {\n"
4212 " return;\n"
4213 " }\n"
4214 "} // namespace",
4215 ShortInlineFunctions);
4216 verifyFormat("namespace { // comment\n"
4217 " void f() {\n"
4218 " return;\n"
4219 " }\n"
4220 "} // namespace",
4221 ShortInlineFunctions);
4222 verifyFormat("namespace {\n"
4223 " int some_int;\n"
4224 " void f() {\n"
4225 " return;\n"
4226 " }\n"
4227 "} // namespace",
4228 ShortInlineFunctions);
4229 verifyFormat("namespace interface {\n"
4230 " void f() {\n"
4231 " return;\n"
4232 " }\n"
4233 "} // namespace interface",
4234 ShortInlineFunctions);
4235 verifyFormat("namespace {\n"
4236 " class X {\n"
4237 " void f() { return; }\n"
4238 " };\n"
4239 "} // namespace",
4240 ShortInlineFunctions);
4241 verifyFormat("namespace {\n"
4242 " class X { /* comment */\n"
4243 " void f() { return; }\n"
4244 " };\n"
4245 "} // namespace",
4246 ShortInlineFunctions);
4247 verifyFormat("namespace {\n"
4248 " class X { // comment\n"
4249 " void f() { return; }\n"
4250 " };\n"
4251 "} // namespace",
4252 ShortInlineFunctions);
4253 verifyFormat("namespace {\n"
4254 " struct X {\n"
4255 " void f() { return; }\n"
4256 " };\n"
4257 "} // namespace",
4258 ShortInlineFunctions);
4259 verifyFormat("namespace {\n"
4260 " union X {\n"
4261 " void f() { return; }\n"
4262 " };\n"
4263 "} // namespace",
4264 ShortInlineFunctions);
4265 verifyFormat("extern \"C\" {\n"
4266 "void f() {\n"
4267 " return;\n"
4268 "}\n"
4269 "} // namespace",
4270 ShortInlineFunctions);
4271 verifyFormat("namespace {\n"
4272 " class X {\n"
4273 " void f() { return; }\n"
4274 " } x;\n"
4275 "} // namespace",
4276 ShortInlineFunctions);
4277 verifyFormat("namespace {\n"
4278 " [[nodiscard]] class X {\n"
4279 " void f() { return; }\n"
4280 " };\n"
4281 "} // namespace",
4282 ShortInlineFunctions);
4283 verifyFormat("namespace {\n"
4284 " static class X {\n"
4285 " void f() { return; }\n"
4286 " } x;\n"
4287 "} // namespace",
4288 ShortInlineFunctions);
4289 verifyFormat("namespace {\n"
4290 " constexpr class X {\n"
4291 " void f() { return; }\n"
4292 " } x;\n"
4293 "} // namespace",
4294 ShortInlineFunctions);
4296 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4297 verifyFormat("extern \"C\" {\n"
4298 " void f() {\n"
4299 " return;\n"
4300 " }\n"
4301 "} // namespace",
4302 ShortInlineFunctions);
4304 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4305 verifyFormat("namespace out {\n"
4306 "int i;\n"
4307 "namespace in {\n"
4308 " int i;\n"
4309 "} // namespace in\n"
4310 "} // namespace out",
4311 "namespace out {\n"
4312 "int i;\n"
4313 "namespace in {\n"
4314 "int i;\n"
4315 "} // namespace in\n"
4316 "} // namespace out",
4317 Style);
4319 Style.NamespaceIndentation = FormatStyle::NI_None;
4320 verifyFormat("template <class T>\n"
4321 "concept a_concept = X<>;\n"
4322 "namespace B {\n"
4323 "struct b_struct {};\n"
4324 "} // namespace B",
4325 Style);
4326 verifyFormat("template <int I>\n"
4327 "constexpr void foo()\n"
4328 " requires(I == 42)\n"
4329 "{}\n"
4330 "namespace ns {\n"
4331 "void foo() {}\n"
4332 "} // namespace ns",
4333 Style);
4335 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4336 LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4337 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4338 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4339 "// block for debug mode\n"
4340 "#ifndef NDEBUG\n"
4341 "#endif\n"
4342 "}}} // namespace ns1::ns2::ns3",
4343 LLVMWithCompactInnerNamespace);
4346 TEST_F(FormatTest, NamespaceMacros) {
4347 FormatStyle Style = getLLVMStyle();
4348 Style.NamespaceMacros.push_back("TESTSUITE");
4350 verifyFormat("TESTSUITE(A) {\n"
4351 "int foo();\n"
4352 "} // TESTSUITE(A)",
4353 Style);
4355 verifyFormat("TESTSUITE(A, B) {\n"
4356 "int foo();\n"
4357 "} // TESTSUITE(A)",
4358 Style);
4360 // Properly indent according to NamespaceIndentation style
4361 Style.NamespaceIndentation = FormatStyle::NI_All;
4362 verifyFormat("TESTSUITE(A) {\n"
4363 " int foo();\n"
4364 "} // TESTSUITE(A)",
4365 Style);
4366 verifyFormat("TESTSUITE(A) {\n"
4367 " namespace B {\n"
4368 " int foo();\n"
4369 " } // namespace B\n"
4370 "} // TESTSUITE(A)",
4371 Style);
4372 verifyFormat("namespace A {\n"
4373 " TESTSUITE(B) {\n"
4374 " int foo();\n"
4375 " } // TESTSUITE(B)\n"
4376 "} // namespace A",
4377 Style);
4379 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4380 verifyFormat("TESTSUITE(A) {\n"
4381 "TESTSUITE(B) {\n"
4382 " int foo();\n"
4383 "} // TESTSUITE(B)\n"
4384 "} // TESTSUITE(A)",
4385 Style);
4386 verifyFormat("TESTSUITE(A) {\n"
4387 "namespace B {\n"
4388 " int foo();\n"
4389 "} // namespace B\n"
4390 "} // TESTSUITE(A)",
4391 Style);
4392 verifyFormat("namespace A {\n"
4393 "TESTSUITE(B) {\n"
4394 " int foo();\n"
4395 "} // TESTSUITE(B)\n"
4396 "} // namespace A",
4397 Style);
4399 // Properly merge namespace-macros blocks in CompactNamespaces mode
4400 Style.NamespaceIndentation = FormatStyle::NI_None;
4401 Style.CompactNamespaces = true;
4402 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4403 "}} // TESTSUITE(A::B)",
4404 Style);
4406 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4407 "}} // TESTSUITE(out::in)",
4408 "TESTSUITE(out) {\n"
4409 "TESTSUITE(in) {\n"
4410 "} // TESTSUITE(in)\n"
4411 "} // TESTSUITE(out)",
4412 Style);
4414 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4415 "}} // TESTSUITE(out::in)",
4416 "TESTSUITE(out) {\n"
4417 "TESTSUITE(in) {\n"
4418 "} // TESTSUITE(in)\n"
4419 "} // TESTSUITE(out)",
4420 Style);
4422 // Do not merge different namespaces/macros
4423 verifyFormat("namespace out {\n"
4424 "TESTSUITE(in) {\n"
4425 "} // TESTSUITE(in)\n"
4426 "} // namespace out",
4427 Style);
4428 verifyFormat("TESTSUITE(out) {\n"
4429 "namespace in {\n"
4430 "} // namespace in\n"
4431 "} // TESTSUITE(out)",
4432 Style);
4433 Style.NamespaceMacros.push_back("FOOBAR");
4434 verifyFormat("TESTSUITE(out) {\n"
4435 "FOOBAR(in) {\n"
4436 "} // FOOBAR(in)\n"
4437 "} // TESTSUITE(out)",
4438 Style);
4441 TEST_F(FormatTest, FormatsCompactNamespaces) {
4442 FormatStyle Style = getLLVMStyle();
4443 Style.CompactNamespaces = true;
4444 Style.NamespaceMacros.push_back("TESTSUITE");
4446 verifyFormat("namespace A { namespace B {\n"
4447 "}} // namespace A::B",
4448 Style);
4450 verifyFormat("namespace out { namespace in {\n"
4451 "}} // namespace out::in",
4452 "namespace out {\n"
4453 "namespace in {\n"
4454 "} // namespace in\n"
4455 "} // namespace out",
4456 Style);
4458 // Only namespaces which have both consecutive opening and end get compacted
4459 verifyFormat("namespace out {\n"
4460 "namespace in1 {\n"
4461 "} // namespace in1\n"
4462 "namespace in2 {\n"
4463 "} // namespace in2\n"
4464 "} // namespace out",
4465 Style);
4467 verifyFormat("namespace out {\n"
4468 "int i;\n"
4469 "namespace in {\n"
4470 "int j;\n"
4471 "} // namespace in\n"
4472 "int k;\n"
4473 "} // namespace out",
4474 "namespace out { int i;\n"
4475 "namespace in { int j; } // namespace in\n"
4476 "int k; } // namespace out",
4477 Style);
4479 verifyFormat("namespace A { namespace B { namespace C {\n"
4480 "}}} // namespace A::B::C",
4481 "namespace A { namespace B {\n"
4482 "namespace C {\n"
4483 "}} // namespace B::C\n"
4484 "} // namespace A",
4485 Style);
4487 Style.ColumnLimit = 40;
4488 verifyFormat("namespace aaaaaaaaaa {\n"
4489 "namespace bbbbbbbbbb {\n"
4490 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4491 "namespace aaaaaaaaaa {\n"
4492 "namespace bbbbbbbbbb {\n"
4493 "} // namespace bbbbbbbbbb\n"
4494 "} // namespace aaaaaaaaaa",
4495 Style);
4497 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4498 "namespace cccccc {\n"
4499 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4500 "namespace aaaaaa {\n"
4501 "namespace bbbbbb {\n"
4502 "namespace cccccc {\n"
4503 "} // namespace cccccc\n"
4504 "} // namespace bbbbbb\n"
4505 "} // namespace aaaaaa",
4506 Style);
4507 Style.ColumnLimit = 80;
4509 // Extra semicolon after 'inner' closing brace prevents merging
4510 verifyFormat("namespace out { namespace in {\n"
4511 "}; } // namespace out::in",
4512 "namespace out {\n"
4513 "namespace in {\n"
4514 "}; // namespace in\n"
4515 "} // namespace out",
4516 Style);
4518 // Extra semicolon after 'outer' closing brace is conserved
4519 verifyFormat("namespace out { namespace in {\n"
4520 "}}; // namespace out::in",
4521 "namespace out {\n"
4522 "namespace in {\n"
4523 "} // namespace in\n"
4524 "}; // namespace out",
4525 Style);
4527 Style.NamespaceIndentation = FormatStyle::NI_All;
4528 verifyFormat("namespace out { namespace in {\n"
4529 " int i;\n"
4530 "}} // namespace out::in",
4531 "namespace out {\n"
4532 "namespace in {\n"
4533 "int i;\n"
4534 "} // namespace in\n"
4535 "} // namespace out",
4536 Style);
4537 verifyFormat("namespace out { namespace mid {\n"
4538 " namespace in {\n"
4539 " int j;\n"
4540 " } // namespace in\n"
4541 " int k;\n"
4542 "}} // namespace out::mid",
4543 "namespace out { namespace mid {\n"
4544 "namespace in { int j; } // namespace in\n"
4545 "int k; }} // namespace out::mid",
4546 Style);
4548 verifyFormat("namespace A { namespace B { namespace C {\n"
4549 " int i;\n"
4550 "}}} // namespace A::B::C\n"
4551 "int main() {\n"
4552 " if (true)\n"
4553 " return 0;\n"
4554 "}",
4555 "namespace A { namespace B {\n"
4556 "namespace C {\n"
4557 " int i;\n"
4558 "}} // namespace B::C\n"
4559 "} // namespace A\n"
4560 "int main() {\n"
4561 " if (true)\n"
4562 " return 0;\n"
4563 "}",
4564 Style);
4566 verifyFormat("namespace A { namespace B { namespace C {\n"
4567 "#ifdef FOO\n"
4568 " int i;\n"
4569 "#endif\n"
4570 "}}} // namespace A::B::C\n"
4571 "int main() {\n"
4572 " if (true)\n"
4573 " return 0;\n"
4574 "}",
4575 "namespace A { namespace B {\n"
4576 "namespace C {\n"
4577 "#ifdef FOO\n"
4578 " int i;\n"
4579 "#endif\n"
4580 "}} // namespace B::C\n"
4581 "} // namespace A\n"
4582 "int main() {\n"
4583 " if (true)\n"
4584 " return 0;\n"
4585 "}",
4586 Style);
4588 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4589 verifyFormat("namespace out { namespace in {\n"
4590 " int i;\n"
4591 "}} // namespace out::in",
4592 "namespace out {\n"
4593 "namespace in {\n"
4594 "int i;\n"
4595 "} // namespace in\n"
4596 "} // namespace out",
4597 Style);
4598 verifyFormat("namespace out { namespace mid { namespace in {\n"
4599 " int i;\n"
4600 "}}} // namespace out::mid::in",
4601 "namespace out {\n"
4602 "namespace mid {\n"
4603 "namespace in {\n"
4604 "int i;\n"
4605 "} // namespace in\n"
4606 "} // namespace mid\n"
4607 "} // namespace out",
4608 Style);
4610 Style.CompactNamespaces = true;
4611 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4612 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4613 Style.BraceWrapping.BeforeLambdaBody = true;
4614 verifyFormat("namespace out { namespace in {\n"
4615 "}} // namespace out::in",
4616 Style);
4617 verifyFormat("namespace out { namespace in {\n"
4618 "}} // namespace out::in",
4619 "namespace out {\n"
4620 "namespace in {\n"
4621 "} // namespace in\n"
4622 "} // namespace out",
4623 Style);
4626 TEST_F(FormatTest, FormatsExternC) {
4627 verifyFormat("extern \"C\" {\nint a;");
4628 verifyFormat("extern \"C\" {}");
4629 verifyFormat("extern \"C\" {\n"
4630 "int foo();\n"
4631 "}");
4632 verifyFormat("extern \"C\" int foo() {}");
4633 verifyFormat("extern \"C\" int foo();");
4634 verifyFormat("extern \"C\" int foo() {\n"
4635 " int i = 42;\n"
4636 " return i;\n"
4637 "}");
4639 FormatStyle Style = getLLVMStyle();
4640 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4641 Style.BraceWrapping.AfterFunction = true;
4642 verifyFormat("extern \"C\" int foo() {}", Style);
4643 verifyFormat("extern \"C\" int foo();", Style);
4644 verifyFormat("extern \"C\" int foo()\n"
4645 "{\n"
4646 " int i = 42;\n"
4647 " return i;\n"
4648 "}",
4649 Style);
4651 Style.BraceWrapping.AfterExternBlock = true;
4652 Style.BraceWrapping.SplitEmptyRecord = false;
4653 verifyFormat("extern \"C\"\n"
4654 "{}",
4655 Style);
4656 verifyFormat("extern \"C\"\n"
4657 "{\n"
4658 " int foo();\n"
4659 "}",
4660 Style);
4663 TEST_F(FormatTest, IndentExternBlockStyle) {
4664 FormatStyle Style = getLLVMStyle();
4665 Style.IndentWidth = 2;
4667 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4668 verifyFormat("extern \"C\" { /*9*/\n"
4669 "}",
4670 Style);
4671 verifyFormat("extern \"C\" {\n"
4672 " int foo10();\n"
4673 "}",
4674 Style);
4676 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4677 verifyFormat("extern \"C\" { /*11*/\n"
4678 "}",
4679 Style);
4680 verifyFormat("extern \"C\" {\n"
4681 "int foo12();\n"
4682 "}",
4683 Style);
4685 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4686 verifyFormat("extern \"C\"\n"
4687 "{\n"
4688 "int i;\n"
4689 "}",
4690 Style);
4692 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4693 Style.BraceWrapping.AfterExternBlock = true;
4694 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4695 verifyFormat("extern \"C\"\n"
4696 "{ /*13*/\n"
4697 "}",
4698 Style);
4699 verifyFormat("extern \"C\"\n{\n"
4700 " int foo14();\n"
4701 "}",
4702 Style);
4704 Style.BraceWrapping.AfterExternBlock = false;
4705 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4706 verifyFormat("extern \"C\" { /*15*/\n"
4707 "}",
4708 Style);
4709 verifyFormat("extern \"C\" {\n"
4710 "int foo16();\n"
4711 "}",
4712 Style);
4714 Style.BraceWrapping.AfterExternBlock = true;
4715 verifyFormat("extern \"C\"\n"
4716 "{ /*13*/\n"
4717 "}",
4718 Style);
4719 verifyFormat("extern \"C\"\n"
4720 "{\n"
4721 "int foo14();\n"
4722 "}",
4723 Style);
4725 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4726 verifyFormat("extern \"C\"\n"
4727 "{ /*13*/\n"
4728 "}",
4729 Style);
4730 verifyFormat("extern \"C\"\n"
4731 "{\n"
4732 " int foo14();\n"
4733 "}",
4734 Style);
4737 TEST_F(FormatTest, FormatsInlineASM) {
4738 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4739 verifyFormat("asm(\"nop\" ::: \"memory\");");
4740 verifyFormat(
4741 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4742 " \"cpuid\\n\\t\"\n"
4743 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4744 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4745 " : \"a\"(value));");
4746 verifyFormat(
4747 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4748 " __asm {\n"
4749 " mov edx,[that] // vtable in edx\n"
4750 " mov eax,methodIndex\n"
4751 " call [edx][eax*4] // stdcall\n"
4752 " }\n"
4753 "}",
4754 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4755 " __asm {\n"
4756 " mov edx,[that] // vtable in edx\n"
4757 " mov eax,methodIndex\n"
4758 " call [edx][eax*4] // stdcall\n"
4759 " }\n"
4760 "}");
4761 verifyNoChange("_asm {\n"
4762 " xor eax, eax;\n"
4763 " cpuid;\n"
4764 "}");
4765 verifyFormat("void function() {\n"
4766 " // comment\n"
4767 " asm(\"\");\n"
4768 "}");
4769 verifyFormat("__asm {\n"
4770 "}\n"
4771 "int i;",
4772 "__asm {\n"
4773 "}\n"
4774 "int i;");
4776 auto Style = getLLVMStyleWithColumns(0);
4777 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4778 const StringRef Code2{"asm(\"xyz\"\n"
4779 " : \"=a\"(a), \"=d\"(b)\n"
4780 " : \"a\"(data));"};
4781 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4782 " : \"a\"(data));"};
4784 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4785 verifyFormat(Code1, Style);
4786 verifyNoChange(Code2, Style);
4787 verifyNoChange(Code3, Style);
4789 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4790 verifyFormat(Code2, Code1, Style);
4791 verifyNoChange(Code2, Style);
4792 verifyFormat(Code2, Code3, Style);
4795 TEST_F(FormatTest, FormatTryCatch) {
4796 verifyFormat("try {\n"
4797 " throw a * b;\n"
4798 "} catch (int a) {\n"
4799 " // Do nothing.\n"
4800 "} catch (...) {\n"
4801 " exit(42);\n"
4802 "}");
4804 // Function-level try statements.
4805 verifyFormat("int f() try { return 4; } catch (...) {\n"
4806 " return 5;\n"
4807 "}");
4808 verifyFormat("class A {\n"
4809 " int a;\n"
4810 " A() try : a(0) {\n"
4811 " } catch (...) {\n"
4812 " throw;\n"
4813 " }\n"
4814 "};");
4815 verifyFormat("class A {\n"
4816 " int a;\n"
4817 " A() try : a(0), b{1} {\n"
4818 " } catch (...) {\n"
4819 " throw;\n"
4820 " }\n"
4821 "};");
4822 verifyFormat("class A {\n"
4823 " int a;\n"
4824 " A() try : a(0), b{1}, c{2} {\n"
4825 " } catch (...) {\n"
4826 " throw;\n"
4827 " }\n"
4828 "};");
4829 verifyFormat("class A {\n"
4830 " int a;\n"
4831 " A() try : a(0), b{1}, c{2} {\n"
4832 " { // New scope.\n"
4833 " }\n"
4834 " } catch (...) {\n"
4835 " throw;\n"
4836 " }\n"
4837 "};");
4839 // Incomplete try-catch blocks.
4840 verifyIncompleteFormat("try {} catch (");
4843 TEST_F(FormatTest, FormatTryAsAVariable) {
4844 verifyFormat("int try;");
4845 verifyFormat("int try, size;");
4846 verifyFormat("try = foo();");
4847 verifyFormat("if (try < size) {\n return true;\n}");
4849 verifyFormat("int catch;");
4850 verifyFormat("int catch, size;");
4851 verifyFormat("catch = foo();");
4852 verifyFormat("if (catch < size) {\n return true;\n}");
4854 FormatStyle Style = getLLVMStyle();
4855 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4856 Style.BraceWrapping.AfterFunction = true;
4857 Style.BraceWrapping.BeforeCatch = true;
4858 verifyFormat("try {\n"
4859 " int bar = 1;\n"
4860 "}\n"
4861 "catch (...) {\n"
4862 " int bar = 1;\n"
4863 "}",
4864 Style);
4865 verifyFormat("#if NO_EX\n"
4866 "try\n"
4867 "#endif\n"
4868 "{\n"
4869 "}\n"
4870 "#if NO_EX\n"
4871 "catch (...) {\n"
4872 "}",
4873 Style);
4874 verifyFormat("try /* abc */ {\n"
4875 " int bar = 1;\n"
4876 "}\n"
4877 "catch (...) {\n"
4878 " int bar = 1;\n"
4879 "}",
4880 Style);
4881 verifyFormat("try\n"
4882 "// abc\n"
4883 "{\n"
4884 " int bar = 1;\n"
4885 "}\n"
4886 "catch (...) {\n"
4887 " int bar = 1;\n"
4888 "}",
4889 Style);
4892 TEST_F(FormatTest, FormatSEHTryCatch) {
4893 verifyFormat("__try {\n"
4894 " int a = b * c;\n"
4895 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4896 " // Do nothing.\n"
4897 "}");
4899 verifyFormat("__try {\n"
4900 " int a = b * c;\n"
4901 "} __finally {\n"
4902 " // Do nothing.\n"
4903 "}");
4905 verifyFormat("DEBUG({\n"
4906 " __try {\n"
4907 " } __finally {\n"
4908 " }\n"
4909 "});");
4912 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4913 verifyFormat("try {\n"
4914 " f();\n"
4915 "} catch {\n"
4916 " g();\n"
4917 "}");
4918 verifyFormat("try {\n"
4919 " f();\n"
4920 "} catch (A a) MACRO(x) {\n"
4921 " g();\n"
4922 "} catch (B b) MACRO(x) {\n"
4923 " g();\n"
4924 "}");
4927 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4928 FormatStyle Style = getLLVMStyle();
4929 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4930 FormatStyle::BS_WebKit}) {
4931 Style.BreakBeforeBraces = BraceStyle;
4932 verifyFormat("try {\n"
4933 " // something\n"
4934 "} catch (...) {\n"
4935 " // something\n"
4936 "}",
4937 Style);
4939 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4940 verifyFormat("try {\n"
4941 " // something\n"
4942 "}\n"
4943 "catch (...) {\n"
4944 " // something\n"
4945 "}",
4946 Style);
4947 verifyFormat("__try {\n"
4948 " // something\n"
4949 "}\n"
4950 "__finally {\n"
4951 " // something\n"
4952 "}",
4953 Style);
4954 verifyFormat("@try {\n"
4955 " // something\n"
4956 "}\n"
4957 "@finally {\n"
4958 " // something\n"
4959 "}",
4960 Style);
4961 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4962 verifyFormat("try\n"
4963 "{\n"
4964 " // something\n"
4965 "}\n"
4966 "catch (...)\n"
4967 "{\n"
4968 " // something\n"
4969 "}",
4970 Style);
4971 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4972 verifyFormat("try\n"
4973 " {\n"
4974 " // something white\n"
4975 " }\n"
4976 "catch (...)\n"
4977 " {\n"
4978 " // something white\n"
4979 " }",
4980 Style);
4981 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4982 verifyFormat("try\n"
4983 " {\n"
4984 " // something\n"
4985 " }\n"
4986 "catch (...)\n"
4987 " {\n"
4988 " // something\n"
4989 " }",
4990 Style);
4991 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4992 Style.BraceWrapping.BeforeCatch = true;
4993 verifyFormat("try {\n"
4994 " // something\n"
4995 "}\n"
4996 "catch (...) {\n"
4997 " // something\n"
4998 "}",
4999 Style);
5002 TEST_F(FormatTest, StaticInitializers) {
5003 verifyFormat("static SomeClass SC = {1, 'a'};");
5005 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
5006 " 100000000, "
5007 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
5009 // Here, everything other than the "}" would fit on a line.
5010 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
5011 " 10000000000000000000000000};");
5012 verifyFormat("S s = {a,\n"
5013 "\n"
5014 " b};",
5015 "S s = {\n"
5016 " a,\n"
5017 "\n"
5018 " b\n"
5019 "};");
5021 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5022 // line. However, the formatting looks a bit off and this probably doesn't
5023 // happen often in practice.
5024 verifyFormat("static int Variable[1] = {\n"
5025 " {1000000000000000000000000000000000000}};",
5026 getLLVMStyleWithColumns(40));
5029 TEST_F(FormatTest, DesignatedInitializers) {
5030 verifyFormat("const struct A a = {.a = 1, .b = 2};");
5031 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5032 " .bbbbbbbbbb = 2,\n"
5033 " .cccccccccc = 3,\n"
5034 " .dddddddddd = 4,\n"
5035 " .eeeeeeeeee = 5};");
5036 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5038 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5039 " .ccccccccccccccccccccccccccc = 3,\n"
5040 " .ddddddddddddddddddddddddddd = 4,\n"
5041 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5043 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5045 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5046 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5047 " [2] = bbbbbbbbbb,\n"
5048 " [3] = cccccccccc,\n"
5049 " [4] = dddddddddd,\n"
5050 " [5] = eeeeeeeeee};");
5051 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5052 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5054 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5055 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5056 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5058 verifyFormat("for (const TestCase &test_case : {\n"
5059 " TestCase{\n"
5060 " .a = 1,\n"
5061 " .b = 1,\n"
5062 " },\n"
5063 " TestCase{\n"
5064 " .a = 2,\n"
5065 " .b = 2,\n"
5066 " },\n"
5067 " }) {\n"
5068 "}");
5071 TEST_F(FormatTest, BracedInitializerIndentWidth) {
5072 auto Style = getLLVMStyleWithColumns(60);
5073 Style.BinPackArguments = true;
5074 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5075 Style.BracedInitializerIndentWidth = 6;
5077 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5078 verifyFormat("enum class {\n"
5079 " One,\n"
5080 " Two,\n"
5081 "};",
5082 Style);
5083 verifyFormat("class Foo {\n"
5084 " Foo() {}\n"
5085 " void bar();\n"
5086 "};",
5087 Style);
5088 verifyFormat("void foo() {\n"
5089 " auto bar = baz;\n"
5090 " return baz;\n"
5091 "};",
5092 Style);
5093 verifyFormat("auto foo = [&] {\n"
5094 " auto bar = baz;\n"
5095 " return baz;\n"
5096 "};",
5097 Style);
5098 verifyFormat("{\n"
5099 " auto bar = baz;\n"
5100 " return baz;\n"
5101 "};",
5102 Style);
5103 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5104 verifyFormat("SomeClass clazz(\n"
5105 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5106 " \"zzzzzzzzzzzzzzzzzz\");",
5107 Style);
5109 // The following types of initialization are all affected by
5110 // BracedInitializerIndentWidth. Aggregate initialization.
5111 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5112 " 10000000, 20000000};",
5113 Style);
5114 verifyFormat("SomeStruct s{\n"
5115 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5116 " \"zzzzzzzzzzzzzzzz\"};",
5117 Style);
5118 // Designated initializers.
5119 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5120 " [0] = 10000000, [1] = 20000000};",
5121 Style);
5122 verifyFormat("SomeStruct s{\n"
5123 " .foo = \"xxxxxxxxxxxxx\",\n"
5124 " .bar = \"yyyyyyyyyyyyy\",\n"
5125 " .baz = \"zzzzzzzzzzzzz\"};",
5126 Style);
5127 // List initialization.
5128 verifyFormat("SomeStruct s{\n"
5129 " \"xxxxxxxxxxxxx\",\n"
5130 " \"yyyyyyyyyyyyy\",\n"
5131 " \"zzzzzzzzzzzzz\",\n"
5132 "};",
5133 Style);
5134 verifyFormat("SomeStruct{\n"
5135 " \"xxxxxxxxxxxxx\",\n"
5136 " \"yyyyyyyyyyyyy\",\n"
5137 " \"zzzzzzzzzzzzz\",\n"
5138 "};",
5139 Style);
5140 verifyFormat("new SomeStruct{\n"
5141 " \"xxxxxxxxxxxxx\",\n"
5142 " \"yyyyyyyyyyyyy\",\n"
5143 " \"zzzzzzzzzzzzz\",\n"
5144 "};",
5145 Style);
5146 // Member initializer.
5147 verifyFormat("class SomeClass {\n"
5148 " SomeStruct s{\n"
5149 " \"xxxxxxxxxxxxx\",\n"
5150 " \"yyyyyyyyyyyyy\",\n"
5151 " \"zzzzzzzzzzzzz\",\n"
5152 " };\n"
5153 "};",
5154 Style);
5155 // Constructor member initializer.
5156 verifyFormat("SomeClass::SomeClass : strct{\n"
5157 " \"xxxxxxxxxxxxx\",\n"
5158 " \"yyyyyyyyyyyyy\",\n"
5159 " \"zzzzzzzzzzzzz\",\n"
5160 " } {}",
5161 Style);
5162 // Copy initialization.
5163 verifyFormat("SomeStruct s = SomeStruct{\n"
5164 " \"xxxxxxxxxxxxx\",\n"
5165 " \"yyyyyyyyyyyyy\",\n"
5166 " \"zzzzzzzzzzzzz\",\n"
5167 "};",
5168 Style);
5169 // Copy list initialization.
5170 verifyFormat("SomeStruct s = {\n"
5171 " \"xxxxxxxxxxxxx\",\n"
5172 " \"yyyyyyyyyyyyy\",\n"
5173 " \"zzzzzzzzzzzzz\",\n"
5174 "};",
5175 Style);
5176 // Assignment operand initialization.
5177 verifyFormat("s = {\n"
5178 " \"xxxxxxxxxxxxx\",\n"
5179 " \"yyyyyyyyyyyyy\",\n"
5180 " \"zzzzzzzzzzzzz\",\n"
5181 "};",
5182 Style);
5183 // Returned object initialization.
5184 verifyFormat("return {\n"
5185 " \"xxxxxxxxxxxxx\",\n"
5186 " \"yyyyyyyyyyyyy\",\n"
5187 " \"zzzzzzzzzzzzz\",\n"
5188 "};",
5189 Style);
5190 // Initializer list.
5191 verifyFormat("auto initializerList = {\n"
5192 " \"xxxxxxxxxxxxx\",\n"
5193 " \"yyyyyyyyyyyyy\",\n"
5194 " \"zzzzzzzzzzzzz\",\n"
5195 "};",
5196 Style);
5197 // Function parameter initialization.
5198 verifyFormat("func({\n"
5199 " \"xxxxxxxxxxxxx\",\n"
5200 " \"yyyyyyyyyyyyy\",\n"
5201 " \"zzzzzzzzzzzzz\",\n"
5202 "});",
5203 Style);
5204 // Nested init lists.
5205 verifyFormat("SomeStruct s = {\n"
5206 " {{init1, init2, init3, init4, init5},\n"
5207 " {init1, init2, init3, init4, init5}}};",
5208 Style);
5209 verifyFormat("SomeStruct s = {\n"
5210 " {{\n"
5211 " .init1 = 1,\n"
5212 " .init2 = 2,\n"
5213 " .init3 = 3,\n"
5214 " .init4 = 4,\n"
5215 " .init5 = 5,\n"
5216 " },\n"
5217 " {init1, init2, init3, init4, init5}}};",
5218 Style);
5219 verifyFormat("SomeArrayT a[3] = {\n"
5220 " {\n"
5221 " foo,\n"
5222 " bar,\n"
5223 " },\n"
5224 " {\n"
5225 " foo,\n"
5226 " bar,\n"
5227 " },\n"
5228 " SomeArrayT{},\n"
5229 "};",
5230 Style);
5231 verifyFormat("SomeArrayT a[3] = {\n"
5232 " {foo},\n"
5233 " {\n"
5234 " {\n"
5235 " init1,\n"
5236 " init2,\n"
5237 " init3,\n"
5238 " },\n"
5239 " {\n"
5240 " init1,\n"
5241 " init2,\n"
5242 " init3,\n"
5243 " },\n"
5244 " },\n"
5245 " {baz},\n"
5246 "};",
5247 Style);
5249 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5250 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5251 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5252 " \"zzzzzzzzzzzzz\"};",
5253 Style);
5256 TEST_F(FormatTest, NestedStaticInitializers) {
5257 verifyFormat("static A x = {{{}}};");
5258 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5259 " {init1, init2, init3, init4}}};",
5260 getLLVMStyleWithColumns(50));
5262 verifyFormat("somes Status::global_reps[3] = {\n"
5263 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5264 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5265 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5266 getLLVMStyleWithColumns(60));
5267 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5268 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5269 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5270 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5271 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5272 " {rect.fRight - rect.fLeft, rect.fBottom - "
5273 "rect.fTop}};");
5275 verifyFormat(
5276 "SomeArrayOfSomeType a = {\n"
5277 " {{1, 2, 3},\n"
5278 " {1, 2, 3},\n"
5279 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5280 " 333333333333333333333333333333},\n"
5281 " {1, 2, 3},\n"
5282 " {1, 2, 3}}};");
5283 verifyFormat(
5284 "SomeArrayOfSomeType a = {\n"
5285 " {{1, 2, 3}},\n"
5286 " {{1, 2, 3}},\n"
5287 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5288 " 333333333333333333333333333333}},\n"
5289 " {{1, 2, 3}},\n"
5290 " {{1, 2, 3}}};");
5292 verifyFormat("struct {\n"
5293 " unsigned bit;\n"
5294 " const char *const name;\n"
5295 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5296 " {kOsWin, \"Windows\"},\n"
5297 " {kOsLinux, \"Linux\"},\n"
5298 " {kOsCrOS, \"Chrome OS\"}};");
5299 verifyFormat("struct {\n"
5300 " unsigned bit;\n"
5301 " const char *const name;\n"
5302 "} kBitsToOs[] = {\n"
5303 " {kOsMac, \"Mac\"},\n"
5304 " {kOsWin, \"Windows\"},\n"
5305 " {kOsLinux, \"Linux\"},\n"
5306 " {kOsCrOS, \"Chrome OS\"},\n"
5307 "};");
5310 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5311 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5312 " \\\n"
5313 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5316 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5317 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5318 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5320 // Do break defaulted and deleted functions.
5321 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5322 " default;",
5323 getLLVMStyleWithColumns(40));
5324 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5325 " delete;",
5326 getLLVMStyleWithColumns(40));
5329 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5330 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5331 getLLVMStyleWithColumns(40));
5332 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5333 getLLVMStyleWithColumns(40));
5334 verifyFormat("#define Q \\\n"
5335 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5336 " \"aaaaaaaa.cpp\"",
5337 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5338 getLLVMStyleWithColumns(40));
5341 TEST_F(FormatTest, UnderstandsLinePPDirective) {
5342 verifyFormat("# 123 \"A string literal\"",
5343 " # 123 \"A string literal\"");
5346 TEST_F(FormatTest, LayoutUnknownPPDirective) {
5347 verifyFormat("#;");
5348 verifyFormat("#\n;\n;\n;");
5351 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5352 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5353 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5354 getLLVMStyleWithColumns(12));
5357 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5358 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5359 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5362 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5363 verifyFormat("#define A \\x20");
5364 verifyFormat("#define A \\ x20");
5365 verifyFormat("#define A \\ x20", "#define A \\ x20");
5366 verifyFormat("#define A ''");
5367 verifyFormat("#define A ''qqq");
5368 verifyFormat("#define A `qqq");
5369 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5370 verifyFormat("const char *c = STRINGIFY(\n"
5371 "\\na : b);",
5372 "const char * c = STRINGIFY(\n"
5373 "\\na : b);");
5375 verifyFormat("a\r\\");
5376 verifyFormat("a\v\\");
5377 verifyFormat("a\f\\");
5380 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5381 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5382 style.IndentWidth = 4;
5383 style.PPIndentWidth = 1;
5385 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5386 verifyFormat("#ifdef __linux__\n"
5387 "void foo() {\n"
5388 " int x = 0;\n"
5389 "}\n"
5390 "#define FOO\n"
5391 "#endif\n"
5392 "void bar() {\n"
5393 " int y = 0;\n"
5394 "}",
5395 style);
5397 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5398 verifyFormat("#ifdef __linux__\n"
5399 "void foo() {\n"
5400 " int x = 0;\n"
5401 "}\n"
5402 "# define FOO foo\n"
5403 "#endif\n"
5404 "void bar() {\n"
5405 " int y = 0;\n"
5406 "}",
5407 style);
5409 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5410 verifyFormat("#ifdef __linux__\n"
5411 "void foo() {\n"
5412 " int x = 0;\n"
5413 "}\n"
5414 " #define FOO foo\n"
5415 "#endif\n"
5416 "void bar() {\n"
5417 " int y = 0;\n"
5418 "}",
5419 style);
5420 verifyFormat("#if 1\n"
5421 " // some comments\n"
5422 " // another\n"
5423 " #define foo 1\n"
5424 "// not a define comment\n"
5425 "void bar() {\n"
5426 " // comment\n"
5427 " int y = 0;\n"
5428 "}",
5429 "#if 1\n"
5430 "// some comments\n"
5431 "// another\n"
5432 "#define foo 1\n"
5433 "// not a define comment\n"
5434 "void bar() {\n"
5435 " // comment\n"
5436 " int y = 0;\n"
5437 "}",
5438 style);
5440 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5441 verifyFormat("#ifdef foo\n"
5442 "#define bar() \\\n"
5443 " if (A) { \\\n"
5444 " B(); \\\n"
5445 " } \\\n"
5446 " C();\n"
5447 "#endif",
5448 style);
5449 verifyFormat("if (emacs) {\n"
5450 "#ifdef is\n"
5451 "#define lit \\\n"
5452 " if (af) { \\\n"
5453 " return duh(); \\\n"
5454 " }\n"
5455 "#endif\n"
5456 "}",
5457 style);
5458 verifyFormat("#if abc\n"
5459 "#ifdef foo\n"
5460 "#define bar() \\\n"
5461 " if (A) { \\\n"
5462 " if (B) { \\\n"
5463 " C(); \\\n"
5464 " } \\\n"
5465 " } \\\n"
5466 " D();\n"
5467 "#endif\n"
5468 "#endif",
5469 style);
5470 verifyFormat("#ifndef foo\n"
5471 "#define foo\n"
5472 "if (emacs) {\n"
5473 "#ifdef is\n"
5474 "#define lit \\\n"
5475 " if (af) { \\\n"
5476 " return duh(); \\\n"
5477 " }\n"
5478 "#endif\n"
5479 "}\n"
5480 "#endif",
5481 style);
5482 verifyFormat("#if 1\n"
5483 "#define X \\\n"
5484 " { \\\n"
5485 " x; \\\n"
5486 " x; \\\n"
5487 " }\n"
5488 "#endif",
5489 style);
5490 verifyFormat("#define X \\\n"
5491 " { \\\n"
5492 " x; \\\n"
5493 " x; \\\n"
5494 " }",
5495 style);
5497 style.PPIndentWidth = 2;
5498 verifyFormat("#ifdef foo\n"
5499 "#define bar() \\\n"
5500 " if (A) { \\\n"
5501 " B(); \\\n"
5502 " } \\\n"
5503 " C();\n"
5504 "#endif",
5505 style);
5506 style.IndentWidth = 8;
5507 verifyFormat("#ifdef foo\n"
5508 "#define bar() \\\n"
5509 " if (A) { \\\n"
5510 " B(); \\\n"
5511 " } \\\n"
5512 " C();\n"
5513 "#endif",
5514 style);
5516 style.IndentWidth = 1;
5517 style.PPIndentWidth = 4;
5518 verifyFormat("#if 1\n"
5519 "#define X \\\n"
5520 " { \\\n"
5521 " x; \\\n"
5522 " x; \\\n"
5523 " }\n"
5524 "#endif",
5525 style);
5526 verifyFormat("#define X \\\n"
5527 " { \\\n"
5528 " x; \\\n"
5529 " x; \\\n"
5530 " }",
5531 style);
5533 style.IndentWidth = 4;
5534 style.PPIndentWidth = 1;
5535 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5536 verifyFormat("#ifdef foo\n"
5537 "# define bar() \\\n"
5538 " if (A) { \\\n"
5539 " B(); \\\n"
5540 " } \\\n"
5541 " C();\n"
5542 "#endif",
5543 style);
5544 verifyFormat("#if abc\n"
5545 "# ifdef foo\n"
5546 "# define bar() \\\n"
5547 " if (A) { \\\n"
5548 " if (B) { \\\n"
5549 " C(); \\\n"
5550 " } \\\n"
5551 " } \\\n"
5552 " D();\n"
5553 "# endif\n"
5554 "#endif",
5555 style);
5556 verifyFormat("#ifndef foo\n"
5557 "#define foo\n"
5558 "if (emacs) {\n"
5559 "#ifdef is\n"
5560 "# define lit \\\n"
5561 " if (af) { \\\n"
5562 " return duh(); \\\n"
5563 " }\n"
5564 "#endif\n"
5565 "}\n"
5566 "#endif",
5567 style);
5568 verifyFormat("#define X \\\n"
5569 " { \\\n"
5570 " x; \\\n"
5571 " x; \\\n"
5572 " }",
5573 style);
5575 style.PPIndentWidth = 2;
5576 style.IndentWidth = 8;
5577 verifyFormat("#ifdef foo\n"
5578 "# define bar() \\\n"
5579 " if (A) { \\\n"
5580 " B(); \\\n"
5581 " } \\\n"
5582 " C();\n"
5583 "#endif",
5584 style);
5586 style.PPIndentWidth = 4;
5587 style.IndentWidth = 1;
5588 verifyFormat("#define X \\\n"
5589 " { \\\n"
5590 " x; \\\n"
5591 " x; \\\n"
5592 " }",
5593 style);
5595 style.IndentWidth = 4;
5596 style.PPIndentWidth = 1;
5597 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5598 verifyFormat("if (emacs) {\n"
5599 "#ifdef is\n"
5600 " #define lit \\\n"
5601 " if (af) { \\\n"
5602 " return duh(); \\\n"
5603 " }\n"
5604 "#endif\n"
5605 "}",
5606 style);
5607 verifyFormat("#if abc\n"
5608 " #ifdef foo\n"
5609 " #define bar() \\\n"
5610 " if (A) { \\\n"
5611 " B(); \\\n"
5612 " } \\\n"
5613 " C();\n"
5614 " #endif\n"
5615 "#endif",
5616 style);
5617 verifyFormat("#if 1\n"
5618 " #define X \\\n"
5619 " { \\\n"
5620 " x; \\\n"
5621 " x; \\\n"
5622 " }\n"
5623 "#endif",
5624 style);
5626 style.PPIndentWidth = 2;
5627 verifyFormat("#ifdef foo\n"
5628 " #define bar() \\\n"
5629 " if (A) { \\\n"
5630 " B(); \\\n"
5631 " } \\\n"
5632 " C();\n"
5633 "#endif",
5634 style);
5636 style.PPIndentWidth = 4;
5637 style.IndentWidth = 1;
5638 verifyFormat("#if 1\n"
5639 " #define X \\\n"
5640 " { \\\n"
5641 " x; \\\n"
5642 " x; \\\n"
5643 " }\n"
5644 "#endif",
5645 style);
5648 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5649 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5650 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5651 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5652 // FIXME: We never break before the macro name.
5653 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5655 verifyFormat("#define A A\n#define A A");
5656 verifyFormat("#define A(X) A\n#define A A");
5658 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5659 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5662 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5663 verifyFormat("// somecomment\n"
5664 "#include \"a.h\"\n"
5665 "#define A( \\\n"
5666 " A, B)\n"
5667 "#include \"b.h\"\n"
5668 "// somecomment",
5669 " // somecomment\n"
5670 " #include \"a.h\"\n"
5671 "#define A(A,\\\n"
5672 " B)\n"
5673 " #include \"b.h\"\n"
5674 " // somecomment",
5675 getLLVMStyleWithColumns(13));
5678 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5680 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5681 verifyFormat("#define A \\\n"
5682 " c; \\\n"
5683 " e;\n"
5684 "f;",
5685 "#define A c; e;\n"
5686 "f;",
5687 getLLVMStyleWithColumns(14));
5690 TEST_F(FormatTest, LayoutRemainingTokens) {
5691 verifyFormat("{\n"
5692 "}");
5695 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5696 verifyFormat("int x,\n"
5697 "#define A\n"
5698 " y;",
5699 "int x,\n#define A\ny;");
5702 TEST_F(FormatTest, HashInMacroDefinition) {
5703 verifyFormat("#define A(c) L#c");
5704 verifyFormat("#define A(c) u#c");
5705 verifyFormat("#define A(c) U#c");
5706 verifyFormat("#define A(c) u8#c");
5707 verifyFormat("#define A(c) LR#c");
5708 verifyFormat("#define A(c) uR#c");
5709 verifyFormat("#define A(c) UR#c");
5710 verifyFormat("#define A(c) u8R#c");
5711 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5712 verifyFormat("#define A \\\n"
5713 " { \\\n"
5714 " f(#c); \\\n"
5715 " }",
5716 getLLVMStyleWithColumns(11));
5718 verifyFormat("#define A(X) \\\n"
5719 " void function##X()",
5720 getLLVMStyleWithColumns(22));
5722 verifyFormat("#define A(a, b, c) \\\n"
5723 " void a##b##c()",
5724 getLLVMStyleWithColumns(22));
5726 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5728 #if 0
5729 // FIXME: The correct format is:
5730 verifyFormat("{\n"
5731 " {\n"
5732 "#define GEN_ID(_x) char *_x{#_x}\n"
5733 " GEN_ID(one);\n"
5734 " }\n"
5735 "}");
5736 #endif
5737 verifyFormat("{\n"
5738 " {\n"
5739 "#define GEN_ID(_x) \\\n"
5740 " char *_x { #_x }\n"
5741 " GEN_ID(one);\n"
5742 " }\n"
5743 "}",
5744 getGoogleStyle());
5747 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5748 verifyFormat("#define A (x)");
5749 verifyFormat("#define A(x)");
5751 FormatStyle Style = getLLVMStyle();
5752 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5753 verifyFormat("#define true ((foo)1)", Style);
5754 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5755 verifyFormat("#define false((foo)0)", Style);
5758 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5759 verifyFormat("#define A b;",
5760 "#define A \\\n"
5761 " \\\n"
5762 " b;",
5763 getLLVMStyleWithColumns(25));
5764 verifyNoChange("#define A \\\n"
5765 " \\\n"
5766 " a; \\\n"
5767 " b;",
5768 getLLVMStyleWithColumns(11));
5769 verifyNoChange("#define A \\\n"
5770 " a; \\\n"
5771 " \\\n"
5772 " b;",
5773 getLLVMStyleWithColumns(11));
5776 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5777 verifyIncompleteFormat("#define A :");
5778 verifyFormat("#define SOMECASES \\\n"
5779 " case 1: \\\n"
5780 " case 2",
5781 getLLVMStyleWithColumns(20));
5782 verifyFormat("#define MACRO(a) \\\n"
5783 " if (a) \\\n"
5784 " f(); \\\n"
5785 " else \\\n"
5786 " g()",
5787 getLLVMStyleWithColumns(18));
5788 verifyFormat("#define A template <typename T>");
5789 verifyIncompleteFormat("#define STR(x) #x\n"
5790 "f(STR(this_is_a_string_literal{));");
5791 verifyFormat("#pragma omp threadprivate( \\\n"
5792 " y)), // expected-warning",
5793 getLLVMStyleWithColumns(28));
5794 verifyFormat("#d, = };");
5795 verifyFormat("#if \"a");
5796 verifyIncompleteFormat("({\n"
5797 "#define b \\\n"
5798 " } \\\n"
5799 " a\n"
5800 "a",
5801 getLLVMStyleWithColumns(15));
5802 verifyFormat("#define A \\\n"
5803 " { \\\n"
5804 " {\n"
5805 "#define B \\\n"
5806 " } \\\n"
5807 " }",
5808 getLLVMStyleWithColumns(15));
5809 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5810 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5811 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5812 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5813 verifyNoCrash("#else\n"
5814 "#else\n"
5815 "#endif\n"
5816 "#endif");
5817 verifyNoCrash("#else\n"
5818 "#if X\n"
5819 "#endif\n"
5820 "#endif");
5821 verifyNoCrash("#else\n"
5822 "#endif\n"
5823 "#if X\n"
5824 "#endif");
5825 verifyNoCrash("#if X\n"
5826 "#else\n"
5827 "#else\n"
5828 "#endif\n"
5829 "#endif");
5830 verifyNoCrash("#if X\n"
5831 "#elif Y\n"
5832 "#elif Y\n"
5833 "#endif\n"
5834 "#endif");
5835 verifyNoCrash("#endif\n"
5836 "#endif");
5837 verifyNoCrash("#endif\n"
5838 "#else");
5839 verifyNoCrash("#endif\n"
5840 "#elif Y");
5843 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5844 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5845 verifyFormat("class A : public QObject {\n"
5846 " Q_OBJECT\n"
5847 "\n"
5848 " A() {}\n"
5849 "};",
5850 "class A : public QObject {\n"
5851 " Q_OBJECT\n"
5852 "\n"
5853 " A() {\n}\n"
5854 "} ;");
5855 verifyFormat("MACRO\n"
5856 "/*static*/ int i;",
5857 "MACRO\n"
5858 " /*static*/ int i;");
5859 verifyFormat("SOME_MACRO\n"
5860 "namespace {\n"
5861 "void f();\n"
5862 "} // namespace",
5863 "SOME_MACRO\n"
5864 " namespace {\n"
5865 "void f( );\n"
5866 "} // namespace");
5867 // Only if the identifier contains at least 5 characters.
5868 verifyFormat("HTTP f();", "HTTP\nf();");
5869 verifyNoChange("MACRO\nf();");
5870 // Only if everything is upper case.
5871 verifyFormat("class A : public QObject {\n"
5872 " Q_Object A() {}\n"
5873 "};",
5874 "class A : public QObject {\n"
5875 " Q_Object\n"
5876 " A() {\n}\n"
5877 "} ;");
5879 // Only if the next line can actually start an unwrapped line.
5880 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5881 "<< SomeThing;");
5883 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5884 "(n, buffers))",
5885 getChromiumStyle(FormatStyle::LK_Cpp));
5887 // See PR41483
5888 verifyNoChange("/**/ FOO(a)\n"
5889 "FOO(b)");
5892 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5893 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5894 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5895 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5896 "class X {};\n"
5897 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5898 "int *createScopDetectionPass() { return 0; }",
5899 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5900 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5901 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5902 " class X {};\n"
5903 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5904 " int *createScopDetectionPass() { return 0; }");
5905 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5906 // braces, so that inner block is indented one level more.
5907 verifyFormat("int q() {\n"
5908 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5909 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5910 " IPC_END_MESSAGE_MAP()\n"
5911 "}",
5912 "int q() {\n"
5913 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5914 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5915 " IPC_END_MESSAGE_MAP()\n"
5916 "}");
5918 // Same inside macros.
5919 verifyFormat("#define LIST(L) \\\n"
5920 " L(A) \\\n"
5921 " L(B) \\\n"
5922 " L(C)",
5923 "#define LIST(L) \\\n"
5924 " L(A) \\\n"
5925 " L(B) \\\n"
5926 " L(C)",
5927 getGoogleStyle());
5929 // These must not be recognized as macros.
5930 verifyFormat("int q() {\n"
5931 " f(x);\n"
5932 " f(x) {}\n"
5933 " f(x)->g();\n"
5934 " f(x)->*g();\n"
5935 " f(x).g();\n"
5936 " f(x) = x;\n"
5937 " f(x) += x;\n"
5938 " f(x) -= x;\n"
5939 " f(x) *= x;\n"
5940 " f(x) /= x;\n"
5941 " f(x) %= x;\n"
5942 " f(x) &= x;\n"
5943 " f(x) |= x;\n"
5944 " f(x) ^= x;\n"
5945 " f(x) >>= x;\n"
5946 " f(x) <<= x;\n"
5947 " f(x)[y].z();\n"
5948 " LOG(INFO) << x;\n"
5949 " ifstream(x) >> x;\n"
5950 "}",
5951 "int q() {\n"
5952 " f(x)\n;\n"
5953 " f(x)\n {}\n"
5954 " f(x)\n->g();\n"
5955 " f(x)\n->*g();\n"
5956 " f(x)\n.g();\n"
5957 " f(x)\n = x;\n"
5958 " f(x)\n += x;\n"
5959 " f(x)\n -= x;\n"
5960 " f(x)\n *= x;\n"
5961 " f(x)\n /= x;\n"
5962 " f(x)\n %= x;\n"
5963 " f(x)\n &= x;\n"
5964 " f(x)\n |= x;\n"
5965 " f(x)\n ^= x;\n"
5966 " f(x)\n >>= x;\n"
5967 " f(x)\n <<= x;\n"
5968 " f(x)\n[y].z();\n"
5969 " LOG(INFO)\n << x;\n"
5970 " ifstream(x)\n >> x;\n"
5971 "}");
5972 verifyFormat("int q() {\n"
5973 " F(x)\n"
5974 " if (1) {\n"
5975 " }\n"
5976 " F(x)\n"
5977 " while (1) {\n"
5978 " }\n"
5979 " F(x)\n"
5980 " G(x);\n"
5981 " F(x)\n"
5982 " try {\n"
5983 " Q();\n"
5984 " } catch (...) {\n"
5985 " }\n"
5986 "}",
5987 "int q() {\n"
5988 "F(x)\n"
5989 "if (1) {}\n"
5990 "F(x)\n"
5991 "while (1) {}\n"
5992 "F(x)\n"
5993 "G(x);\n"
5994 "F(x)\n"
5995 "try { Q(); } catch (...) {}\n"
5996 "}");
5997 verifyFormat("class A {\n"
5998 " A() : t(0) {}\n"
5999 " A(int i) noexcept() : {}\n"
6000 " A(X x)\n" // FIXME: function-level try blocks are broken.
6001 " try : t(0) {\n"
6002 " } catch (...) {\n"
6003 " }\n"
6004 "};",
6005 "class A {\n"
6006 " A()\n : t(0) {}\n"
6007 " A(int i)\n noexcept() : {}\n"
6008 " A(X x)\n"
6009 " try : t(0) {} catch (...) {}\n"
6010 "};");
6011 FormatStyle Style = getLLVMStyle();
6012 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6013 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6014 Style.BraceWrapping.AfterFunction = true;
6015 verifyFormat("void f()\n"
6016 "try\n"
6017 "{\n"
6018 "}",
6019 "void f() try {\n"
6020 "}",
6021 Style);
6022 verifyFormat("class SomeClass {\n"
6023 "public:\n"
6024 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6025 "};",
6026 "class SomeClass {\n"
6027 "public:\n"
6028 " SomeClass()\n"
6029 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6030 "};");
6031 verifyFormat("class SomeClass {\n"
6032 "public:\n"
6033 " SomeClass()\n"
6034 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6035 "};",
6036 "class SomeClass {\n"
6037 "public:\n"
6038 " SomeClass()\n"
6039 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6040 "};",
6041 getLLVMStyleWithColumns(40));
6043 verifyFormat("MACRO(>)");
6045 // Some macros contain an implicit semicolon.
6046 Style = getLLVMStyle();
6047 Style.StatementMacros.push_back("FOO");
6048 verifyFormat("FOO(a) int b = 0;");
6049 verifyFormat("FOO(a)\n"
6050 "int b = 0;",
6051 Style);
6052 verifyFormat("FOO(a);\n"
6053 "int b = 0;",
6054 Style);
6055 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6056 "int b = 0;",
6057 Style);
6058 verifyFormat("FOO()\n"
6059 "int b = 0;",
6060 Style);
6061 verifyFormat("FOO\n"
6062 "int b = 0;",
6063 Style);
6064 verifyFormat("void f() {\n"
6065 " FOO(a)\n"
6066 " return a;\n"
6067 "}",
6068 Style);
6069 verifyFormat("FOO(a)\n"
6070 "FOO(b)",
6071 Style);
6072 verifyFormat("int a = 0;\n"
6073 "FOO(b)\n"
6074 "int c = 0;",
6075 Style);
6076 verifyFormat("int a = 0;\n"
6077 "int x = FOO(a)\n"
6078 "int b = 0;",
6079 Style);
6080 verifyFormat("void foo(int a) { FOO(a) }\n"
6081 "uint32_t bar() {}",
6082 Style);
6085 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6086 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
6088 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6089 ZeroColumn);
6092 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6093 verifyFormat("#define A \\\n"
6094 " f({ \\\n"
6095 " g(); \\\n"
6096 " });",
6097 getLLVMStyleWithColumns(11));
6100 TEST_F(FormatTest, IndentPreprocessorDirectives) {
6101 FormatStyle Style = getLLVMStyleWithColumns(40);
6102 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6103 verifyFormat("#ifdef _WIN32\n"
6104 "#define A 0\n"
6105 "#ifdef VAR2\n"
6106 "#define B 1\n"
6107 "#include <someheader.h>\n"
6108 "#define MACRO \\\n"
6109 " some_very_long_func_aaaaaaaaaa();\n"
6110 "#endif\n"
6111 "#else\n"
6112 "#define A 1\n"
6113 "#endif",
6114 Style);
6115 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6116 verifyFormat("#if 1\n"
6117 "# define __STR(x) #x\n"
6118 "#endif",
6119 Style);
6120 verifyFormat("#ifdef _WIN32\n"
6121 "# define A 0\n"
6122 "# ifdef VAR2\n"
6123 "# define B 1\n"
6124 "# include <someheader.h>\n"
6125 "# define MACRO \\\n"
6126 " some_very_long_func_aaaaaaaaaa();\n"
6127 "# endif\n"
6128 "#else\n"
6129 "# define A 1\n"
6130 "#endif",
6131 Style);
6132 verifyFormat("#if A\n"
6133 "# define MACRO \\\n"
6134 " void a(int x) { \\\n"
6135 " b(); \\\n"
6136 " c(); \\\n"
6137 " d(); \\\n"
6138 " e(); \\\n"
6139 " f(); \\\n"
6140 " }\n"
6141 "#endif",
6142 Style);
6143 // Comments before include guard.
6144 verifyFormat("// file comment\n"
6145 "// file comment\n"
6146 "#ifndef HEADER_H\n"
6147 "#define HEADER_H\n"
6148 "code();\n"
6149 "#endif",
6150 Style);
6151 // Test with include guards.
6152 verifyFormat("#ifndef HEADER_H\n"
6153 "#define HEADER_H\n"
6154 "code();\n"
6155 "#endif",
6156 Style);
6157 // Include guards must have a #define with the same variable immediately
6158 // after #ifndef.
6159 verifyFormat("#ifndef NOT_GUARD\n"
6160 "# define FOO\n"
6161 "code();\n"
6162 "#endif",
6163 Style);
6165 // Include guards must cover the entire file.
6166 verifyFormat("code();\n"
6167 "code();\n"
6168 "#ifndef NOT_GUARD\n"
6169 "# define NOT_GUARD\n"
6170 "code();\n"
6171 "#endif",
6172 Style);
6173 verifyFormat("#ifndef NOT_GUARD\n"
6174 "# define NOT_GUARD\n"
6175 "code();\n"
6176 "#endif\n"
6177 "code();",
6178 Style);
6179 // Test with trailing blank lines.
6180 verifyFormat("#ifndef HEADER_H\n"
6181 "#define HEADER_H\n"
6182 "code();\n"
6183 "#endif",
6184 Style);
6185 // Include guards don't have #else.
6186 verifyFormat("#ifndef NOT_GUARD\n"
6187 "# define NOT_GUARD\n"
6188 "code();\n"
6189 "#else\n"
6190 "#endif",
6191 Style);
6192 verifyFormat("#ifndef NOT_GUARD\n"
6193 "# define NOT_GUARD\n"
6194 "code();\n"
6195 "#elif FOO\n"
6196 "#endif",
6197 Style);
6198 // Non-identifier #define after potential include guard.
6199 verifyFormat("#ifndef FOO\n"
6200 "# define 1\n"
6201 "#endif",
6202 Style);
6203 // #if closes past last non-preprocessor line.
6204 verifyFormat("#ifndef FOO\n"
6205 "#define FOO\n"
6206 "#if 1\n"
6207 "int i;\n"
6208 "# define A 0\n"
6209 "#endif\n"
6210 "#endif",
6211 Style);
6212 // Don't crash if there is an #elif directive without a condition.
6213 verifyFormat("#if 1\n"
6214 "int x;\n"
6215 "#elif\n"
6216 "int y;\n"
6217 "#else\n"
6218 "int z;\n"
6219 "#endif",
6220 Style);
6221 // FIXME: This doesn't handle the case where there's code between the
6222 // #ifndef and #define but all other conditions hold. This is because when
6223 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6224 // previous code line yet, so we can't detect it.
6225 verifyFormat("#ifndef NOT_GUARD\n"
6226 "code();\n"
6227 "#define NOT_GUARD\n"
6228 "code();\n"
6229 "#endif",
6230 "#ifndef NOT_GUARD\n"
6231 "code();\n"
6232 "# define NOT_GUARD\n"
6233 "code();\n"
6234 "#endif",
6235 Style);
6236 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6237 // be outside an include guard. Examples are #pragma once and
6238 // #pragma GCC diagnostic, or anything else that does not change the meaning
6239 // of the file if it's included multiple times.
6240 verifyFormat("#ifdef WIN32\n"
6241 "# pragma once\n"
6242 "#endif\n"
6243 "#ifndef HEADER_H\n"
6244 "# define HEADER_H\n"
6245 "code();\n"
6246 "#endif",
6247 "#ifdef WIN32\n"
6248 "# pragma once\n"
6249 "#endif\n"
6250 "#ifndef HEADER_H\n"
6251 "#define HEADER_H\n"
6252 "code();\n"
6253 "#endif",
6254 Style);
6255 // FIXME: This does not detect when there is a single non-preprocessor line
6256 // in front of an include-guard-like structure where other conditions hold
6257 // because ScopedLineState hides the line.
6258 verifyFormat("code();\n"
6259 "#ifndef HEADER_H\n"
6260 "#define HEADER_H\n"
6261 "code();\n"
6262 "#endif",
6263 "code();\n"
6264 "#ifndef HEADER_H\n"
6265 "# define HEADER_H\n"
6266 "code();\n"
6267 "#endif",
6268 Style);
6269 // Keep comments aligned with #, otherwise indent comments normally. These
6270 // tests cannot use verifyFormat because messUp manipulates leading
6271 // whitespace.
6273 const char *Expected = ""
6274 "void f() {\n"
6275 "#if 1\n"
6276 "// Preprocessor aligned.\n"
6277 "# define A 0\n"
6278 " // Code. Separated by blank line.\n"
6279 "\n"
6280 "# define B 0\n"
6281 " // Code. Not aligned with #\n"
6282 "# define C 0\n"
6283 "#endif";
6284 const char *ToFormat = ""
6285 "void f() {\n"
6286 "#if 1\n"
6287 "// Preprocessor aligned.\n"
6288 "# define A 0\n"
6289 "// Code. Separated by blank line.\n"
6290 "\n"
6291 "# define B 0\n"
6292 " // Code. Not aligned with #\n"
6293 "# define C 0\n"
6294 "#endif";
6295 verifyFormat(Expected, ToFormat, Style);
6296 verifyNoChange(Expected, Style);
6298 // Keep block quotes aligned.
6300 const char *Expected = ""
6301 "void f() {\n"
6302 "#if 1\n"
6303 "/* Preprocessor aligned. */\n"
6304 "# define A 0\n"
6305 " /* Code. Separated by blank line. */\n"
6306 "\n"
6307 "# define B 0\n"
6308 " /* Code. Not aligned with # */\n"
6309 "# define C 0\n"
6310 "#endif";
6311 const char *ToFormat = ""
6312 "void f() {\n"
6313 "#if 1\n"
6314 "/* Preprocessor aligned. */\n"
6315 "# define A 0\n"
6316 "/* Code. Separated by blank line. */\n"
6317 "\n"
6318 "# define B 0\n"
6319 " /* Code. Not aligned with # */\n"
6320 "# define C 0\n"
6321 "#endif";
6322 verifyFormat(Expected, ToFormat, Style);
6323 verifyNoChange(Expected, Style);
6325 // Keep comments aligned with un-indented directives.
6327 const char *Expected = ""
6328 "void f() {\n"
6329 "// Preprocessor aligned.\n"
6330 "#define A 0\n"
6331 " // Code. Separated by blank line.\n"
6332 "\n"
6333 "#define B 0\n"
6334 " // Code. Not aligned with #\n"
6335 "#define C 0\n";
6336 const char *ToFormat = ""
6337 "void f() {\n"
6338 "// Preprocessor aligned.\n"
6339 "#define A 0\n"
6340 "// Code. Separated by blank line.\n"
6341 "\n"
6342 "#define B 0\n"
6343 " // Code. Not aligned with #\n"
6344 "#define C 0\n";
6345 verifyFormat(Expected, ToFormat, Style);
6346 verifyNoChange(Expected, Style);
6348 // Test AfterHash with tabs.
6350 FormatStyle Tabbed = Style;
6351 Tabbed.UseTab = FormatStyle::UT_Always;
6352 Tabbed.IndentWidth = 8;
6353 Tabbed.TabWidth = 8;
6354 verifyFormat("#ifdef _WIN32\n"
6355 "#\tdefine A 0\n"
6356 "#\tifdef VAR2\n"
6357 "#\t\tdefine B 1\n"
6358 "#\t\tinclude <someheader.h>\n"
6359 "#\t\tdefine MACRO \\\n"
6360 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6361 "#\tendif\n"
6362 "#else\n"
6363 "#\tdefine A 1\n"
6364 "#endif",
6365 Tabbed);
6368 // Regression test: Multiline-macro inside include guards.
6369 verifyFormat("#ifndef HEADER_H\n"
6370 "#define HEADER_H\n"
6371 "#define A() \\\n"
6372 " int i; \\\n"
6373 " int j;\n"
6374 "#endif // HEADER_H",
6375 getLLVMStyleWithColumns(20));
6377 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6378 // Basic before hash indent tests
6379 verifyFormat("#ifdef _WIN32\n"
6380 " #define A 0\n"
6381 " #ifdef VAR2\n"
6382 " #define B 1\n"
6383 " #include <someheader.h>\n"
6384 " #define MACRO \\\n"
6385 " some_very_long_func_aaaaaaaaaa();\n"
6386 " #endif\n"
6387 "#else\n"
6388 " #define A 1\n"
6389 "#endif",
6390 Style);
6391 verifyFormat("#if A\n"
6392 " #define MACRO \\\n"
6393 " void a(int x) { \\\n"
6394 " b(); \\\n"
6395 " c(); \\\n"
6396 " d(); \\\n"
6397 " e(); \\\n"
6398 " f(); \\\n"
6399 " }\n"
6400 "#endif",
6401 Style);
6402 // Keep comments aligned with indented directives. These
6403 // tests cannot use verifyFormat because messUp manipulates leading
6404 // whitespace.
6406 const char *Expected = "void f() {\n"
6407 "// Aligned to preprocessor.\n"
6408 "#if 1\n"
6409 " // Aligned to code.\n"
6410 " int a;\n"
6411 " #if 1\n"
6412 " // Aligned to preprocessor.\n"
6413 " #define A 0\n"
6414 " // Aligned to code.\n"
6415 " int b;\n"
6416 " #endif\n"
6417 "#endif\n"
6418 "}";
6419 const char *ToFormat = "void f() {\n"
6420 "// Aligned to preprocessor.\n"
6421 "#if 1\n"
6422 "// Aligned to code.\n"
6423 "int a;\n"
6424 "#if 1\n"
6425 "// Aligned to preprocessor.\n"
6426 "#define A 0\n"
6427 "// Aligned to code.\n"
6428 "int b;\n"
6429 "#endif\n"
6430 "#endif\n"
6431 "}";
6432 verifyFormat(Expected, ToFormat, Style);
6433 verifyNoChange(Expected, Style);
6436 const char *Expected = "void f() {\n"
6437 "/* Aligned to preprocessor. */\n"
6438 "#if 1\n"
6439 " /* Aligned to code. */\n"
6440 " int a;\n"
6441 " #if 1\n"
6442 " /* Aligned to preprocessor. */\n"
6443 " #define A 0\n"
6444 " /* Aligned to code. */\n"
6445 " int b;\n"
6446 " #endif\n"
6447 "#endif\n"
6448 "}";
6449 const char *ToFormat = "void f() {\n"
6450 "/* Aligned to preprocessor. */\n"
6451 "#if 1\n"
6452 "/* Aligned to code. */\n"
6453 "int a;\n"
6454 "#if 1\n"
6455 "/* Aligned to preprocessor. */\n"
6456 "#define A 0\n"
6457 "/* Aligned to code. */\n"
6458 "int b;\n"
6459 "#endif\n"
6460 "#endif\n"
6461 "}";
6462 verifyFormat(Expected, ToFormat, Style);
6463 verifyNoChange(Expected, Style);
6466 // Test single comment before preprocessor
6467 verifyFormat("// Comment\n"
6468 "\n"
6469 "#if 1\n"
6470 "#endif",
6471 Style);
6474 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6475 FormatStyle Style = getLLVMStyle();
6476 Style.AlignConsecutiveAssignments.Enabled = true;
6477 Style.AlignConsecutiveDeclarations.Enabled = true;
6479 // Test with just #if blocks.
6480 verifyFormat("void f1() {\n"
6481 "#if 1\n"
6482 " int foo = 1;\n"
6483 " int foobar = 2;\n"
6484 "#endif\n"
6485 "}\n"
6486 "#if 1\n"
6487 "int baz = 3;\n"
6488 "#endif\n"
6489 "void f2() {\n"
6490 "#if 1\n"
6491 " char *foobarbaz = \"foobarbaz\";\n"
6492 " int quux = 4;\n"
6493 "}",
6494 Style);
6496 // Test with just #else blocks.
6497 verifyFormat("void f1() {\n"
6498 "#if 1\n"
6499 "#else\n"
6500 " int foo = 1;\n"
6501 " int foobar = 2;\n"
6502 "#endif\n"
6503 "}\n"
6504 "#if 1\n"
6505 "#else\n"
6506 "int baz = 3;\n"
6507 "#endif\n"
6508 "void f2() {\n"
6509 "#if 1\n"
6510 "#else\n"
6511 " char *foobarbaz = \"foobarbaz\";\n"
6512 " int quux = 4;\n"
6513 "}",
6514 Style);
6515 verifyFormat("auto foo = [] { return; };\n"
6516 "#if FOO\n"
6517 "#else\n"
6518 "count = bar;\n"
6519 "mbid = bid;\n"
6520 "#endif",
6521 Style);
6523 // Test with a mix of #if and #else blocks.
6524 verifyFormat("void f1() {\n"
6525 "#if 1\n"
6526 "#else\n"
6527 " int foo = 1;\n"
6528 " int foobar = 2;\n"
6529 "#endif\n"
6530 "}\n"
6531 "#if 1\n"
6532 "int baz = 3;\n"
6533 "#endif\n"
6534 "void f2() {\n"
6535 "#if 1\n"
6536 "#else\n"
6537 " // prevent alignment with #else in f1\n"
6538 " char *foobarbaz = \"foobarbaz\";\n"
6539 " int quux = 4;\n"
6540 "}",
6541 Style);
6543 // Test with nested #if and #else blocks.
6544 verifyFormat("void f1() {\n"
6545 "#if 1\n"
6546 "#else\n"
6547 "#if 2\n"
6548 "#else\n"
6549 " int foo = 1;\n"
6550 " int foobar = 2;\n"
6551 "#endif\n"
6552 "#endif\n"
6553 "}\n"
6554 "#if 1\n"
6555 "#else\n"
6556 "#if 2\n"
6557 "int baz = 3;\n"
6558 "#endif\n"
6559 "#endif\n"
6560 "void f2() {\n"
6561 "#if 1\n"
6562 "#if 2\n"
6563 "#else\n"
6564 " // prevent alignment with #else in f1\n"
6565 " char *foobarbaz = \"foobarbaz\";\n"
6566 " int quux = 4;\n"
6567 "#endif\n"
6568 "#endif\n"
6569 "}",
6570 Style);
6572 verifyFormat("#if FOO\n"
6573 "int a = 1;\n"
6574 "#else\n"
6575 "int ab = 2;\n"
6576 "#endif\n"
6577 "#ifdef BAR\n"
6578 "int abc = 3;\n"
6579 "#elifdef BAZ\n"
6580 "int abcd = 4;\n"
6581 "#endif",
6582 Style);
6584 verifyFormat("void f() {\n"
6585 " if (foo) {\n"
6586 "#if FOO\n"
6587 " int a = 1;\n"
6588 "#else\n"
6589 " bool a = true;\n"
6590 "#endif\n"
6591 " int abc = 3;\n"
6592 "#ifndef BAR\n"
6593 " int abcd = 4;\n"
6594 "#elif BAZ\n"
6595 " bool abcd = true;\n"
6596 "#endif\n"
6597 " }\n"
6598 "}",
6599 Style);
6601 verifyFormat("void f() {\n"
6602 "#if FOO\n"
6603 " a = 1;\n"
6604 "#else\n"
6605 " ab = 2;\n"
6606 "#endif\n"
6607 "}\n"
6608 "void g() {\n"
6609 "#if BAR\n"
6610 " abc = 3;\n"
6611 "#elifndef BAZ\n"
6612 " abcd = 4;\n"
6613 "#endif\n"
6614 "}",
6615 Style);
6618 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6619 verifyFormat("{\n"
6620 " {\n"
6621 " a #c;\n"
6622 " }\n"
6623 "}");
6626 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6627 verifyFormat("#define A \\\n { \\\n {\nint i;",
6628 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6629 verifyFormat("#define A \\\n } \\\n }\nint i;",
6630 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6633 TEST_F(FormatTest, EscapedNewlines) {
6634 FormatStyle Narrow = getLLVMStyleWithColumns(11);
6635 verifyFormat("#define A \\\n int i; \\\n int j;",
6636 "#define A \\\nint i;\\\n int j;", Narrow);
6637 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6638 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6639 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6640 verifyNoChange("<a\n\\\\\n>");
6642 FormatStyle AlignLeft = getLLVMStyle();
6643 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6644 verifyFormat("#define MACRO(x) \\\n"
6645 "private: \\\n"
6646 " int x(int a);",
6647 AlignLeft);
6649 // CRLF line endings
6650 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6651 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6652 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6653 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6654 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6655 verifyNoChange("<a\r\n\\\\\r\n>");
6656 verifyFormat("#define MACRO(x) \\\r\n"
6657 "private: \\\r\n"
6658 " int x(int a);",
6659 AlignLeft);
6661 constexpr StringRef Code{"#define A \\\n"
6662 " int a123; \\\n"
6663 " int a; \\\n"
6664 " int a1234;"};
6665 verifyFormat(Code, AlignLeft);
6667 constexpr StringRef Code2{"#define A \\\n"
6668 " int a123; \\\n"
6669 " int a; \\\n"
6670 " int a1234;"};
6671 auto LastLine = getLLVMStyle();
6672 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6673 verifyFormat(Code2, LastLine);
6675 LastLine.ColumnLimit = 13;
6676 verifyFormat(Code, LastLine);
6678 LastLine.ColumnLimit = 0;
6679 verifyFormat(Code2, LastLine);
6681 FormatStyle DontAlign = getLLVMStyle();
6682 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6683 DontAlign.MaxEmptyLinesToKeep = 3;
6684 // FIXME: can't use verifyFormat here because the newline before
6685 // "public:" is not inserted the first time it's reformatted
6686 verifyNoChange("#define A \\\n"
6687 " class Foo { \\\n"
6688 " void bar(); \\\n"
6689 "\\\n"
6690 "\\\n"
6691 "\\\n"
6692 " public: \\\n"
6693 " void baz(); \\\n"
6694 " };",
6695 DontAlign);
6698 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6699 verifyFormat("#define A \\\n"
6700 " int v( \\\n"
6701 " a); \\\n"
6702 " int i;",
6703 getLLVMStyleWithColumns(11));
6706 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6707 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6708 " \\\n"
6709 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6710 "\n"
6711 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6712 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6713 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6714 "\\\n"
6715 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6716 " \n"
6717 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6718 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6721 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6722 verifyFormat("int\n"
6723 "#define A\n"
6724 " a;",
6725 "int\n#define A\na;");
6726 verifyFormat("functionCallTo(\n"
6727 " someOtherFunction(\n"
6728 " withSomeParameters, whichInSequence,\n"
6729 " areLongerThanALine(andAnotherCall,\n"
6730 "#define A B\n"
6731 " withMoreParamters,\n"
6732 " whichStronglyInfluenceTheLayout),\n"
6733 " andMoreParameters),\n"
6734 " trailing);",
6735 getLLVMStyleWithColumns(69));
6736 verifyFormat("Foo::Foo()\n"
6737 "#ifdef BAR\n"
6738 " : baz(0)\n"
6739 "#endif\n"
6740 "{\n"
6741 "}");
6742 verifyFormat("void f() {\n"
6743 " if (true)\n"
6744 "#ifdef A\n"
6745 " f(42);\n"
6746 " x();\n"
6747 "#else\n"
6748 " g();\n"
6749 " x();\n"
6750 "#endif\n"
6751 "}");
6752 verifyFormat("void f(param1, param2,\n"
6753 " param3,\n"
6754 "#ifdef A\n"
6755 " param4(param5,\n"
6756 "#ifdef A1\n"
6757 " param6,\n"
6758 "#ifdef A2\n"
6759 " param7),\n"
6760 "#else\n"
6761 " param8),\n"
6762 " param9,\n"
6763 "#endif\n"
6764 " param10,\n"
6765 "#endif\n"
6766 " param11)\n"
6767 "#else\n"
6768 " param12)\n"
6769 "#endif\n"
6770 "{\n"
6771 " x();\n"
6772 "}",
6773 getLLVMStyleWithColumns(28));
6774 verifyFormat("#if 1\n"
6775 "int i;");
6776 verifyFormat("#if 1\n"
6777 "#endif\n"
6778 "#if 1\n"
6779 "#else\n"
6780 "#endif");
6781 verifyFormat("DEBUG({\n"
6782 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6784 "});\n"
6785 "#if a\n"
6786 "#else\n"
6787 "#endif");
6789 verifyIncompleteFormat("void f(\n"
6790 "#if A\n"
6791 ");\n"
6792 "#else\n"
6793 "#endif");
6795 // Verify that indentation is correct when there is an `#if 0` with an
6796 // `#else`.
6797 verifyFormat("#if 0\n"
6798 "{\n"
6799 "#else\n"
6800 "{\n"
6801 "#endif\n"
6802 " x;\n"
6803 "}");
6805 verifyFormat("#if 0\n"
6806 "#endif\n"
6807 "#if X\n"
6808 "int something_fairly_long; // Align here please\n"
6809 "#endif // Should be aligned");
6811 verifyFormat("#if 0\n"
6812 "#endif\n"
6813 "#if X\n"
6814 "#else // Align\n"
6815 ";\n"
6816 "#endif // Align");
6818 verifyFormat("void SomeFunction(int param1,\n"
6819 " template <\n"
6820 "#ifdef A\n"
6821 "#if 0\n"
6822 "#endif\n"
6823 " MyType<Some>>\n"
6824 "#else\n"
6825 " Type1, Type2>\n"
6826 "#endif\n"
6827 " param2,\n"
6828 " param3) {\n"
6829 " f();\n"
6830 "}");
6833 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6834 verifyFormat("#endif\n"
6835 "#if B");
6838 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6839 FormatStyle SingleLine = getLLVMStyle();
6840 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6841 verifyFormat("#if 0\n"
6842 "#elif 1\n"
6843 "#endif\n"
6844 "void foo() {\n"
6845 " if (test) foo2();\n"
6846 "}",
6847 SingleLine);
6850 TEST_F(FormatTest, LayoutBlockInsideParens) {
6851 verifyFormat("functionCall({ int i; });");
6852 verifyFormat("functionCall({\n"
6853 " int i;\n"
6854 " int j;\n"
6855 "});");
6856 verifyFormat("functionCall(\n"
6857 " {\n"
6858 " int i;\n"
6859 " int j;\n"
6860 " },\n"
6861 " aaaa, bbbb, cccc);");
6862 verifyFormat("functionA(functionB({\n"
6863 " int i;\n"
6864 " int j;\n"
6865 " }),\n"
6866 " aaaa, bbbb, cccc);");
6867 verifyFormat("functionCall(\n"
6868 " {\n"
6869 " int i;\n"
6870 " int j;\n"
6871 " },\n"
6872 " aaaa, bbbb, // comment\n"
6873 " cccc);");
6874 verifyFormat("functionA(functionB({\n"
6875 " int i;\n"
6876 " int j;\n"
6877 " }),\n"
6878 " aaaa, bbbb, // comment\n"
6879 " cccc);");
6880 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6881 verifyFormat("functionCall(aaaa, bbbb, {\n"
6882 " int i;\n"
6883 " int j;\n"
6884 "});");
6885 verifyFormat(
6886 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6887 " {\n"
6888 " int i; // break\n"
6889 " },\n"
6890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6891 " ccccccccccccccccc));");
6892 verifyFormat("DEBUG({\n"
6893 " if (a)\n"
6894 " f();\n"
6895 "});");
6898 TEST_F(FormatTest, LayoutBlockInsideStatement) {
6899 verifyFormat("SOME_MACRO { int i; }\n"
6900 "int i;",
6901 " SOME_MACRO {int i;} int i;");
6904 TEST_F(FormatTest, LayoutNestedBlocks) {
6905 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6906 " struct s {\n"
6907 " int i;\n"
6908 " };\n"
6909 " s kBitsToOs[] = {{10}};\n"
6910 " for (int i = 0; i < 10; ++i)\n"
6911 " return;\n"
6912 "}");
6913 verifyFormat("call(parameter, {\n"
6914 " something();\n"
6915 " // Comment using all columns.\n"
6916 " somethingelse();\n"
6917 "});",
6918 getLLVMStyleWithColumns(40));
6919 verifyFormat("DEBUG( //\n"
6920 " { f(); }, a);");
6921 verifyFormat("DEBUG( //\n"
6922 " {\n"
6923 " f(); //\n"
6924 " },\n"
6925 " a);");
6927 verifyFormat("call(parameter, {\n"
6928 " something();\n"
6929 " // Comment too\n"
6930 " // looooooooooong.\n"
6931 " somethingElse();\n"
6932 "});",
6933 "call(parameter, {\n"
6934 " something();\n"
6935 " // Comment too looooooooooong.\n"
6936 " somethingElse();\n"
6937 "});",
6938 getLLVMStyleWithColumns(29));
6939 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6940 verifyFormat("DEBUG({ // comment\n"
6941 " int i;\n"
6942 "});",
6943 "DEBUG({ // comment\n"
6944 "int i;\n"
6945 "});");
6946 verifyFormat("DEBUG({\n"
6947 " int i;\n"
6948 "\n"
6949 " // comment\n"
6950 " int j;\n"
6951 "});",
6952 "DEBUG({\n"
6953 " int i;\n"
6954 "\n"
6955 " // comment\n"
6956 " int j;\n"
6957 "});");
6959 verifyFormat("DEBUG({\n"
6960 " if (a)\n"
6961 " return;\n"
6962 "});");
6963 verifyGoogleFormat("DEBUG({\n"
6964 " if (a) return;\n"
6965 "});");
6966 FormatStyle Style = getGoogleStyle();
6967 Style.ColumnLimit = 45;
6968 verifyFormat("Debug(\n"
6969 " aaaaa,\n"
6970 " {\n"
6971 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6972 " },\n"
6973 " a);",
6974 Style);
6976 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6978 verifyNoCrash("^{v^{a}}");
6981 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6982 verifyFormat("#define MACRO() \\\n"
6983 " Debug(aaa, /* force line break */ \\\n"
6984 " { \\\n"
6985 " int i; \\\n"
6986 " int j; \\\n"
6987 " })",
6988 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6989 " { int i; int j; })",
6990 getGoogleStyle());
6992 verifyFormat("#define A \\\n"
6993 " [] { \\\n"
6994 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6995 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6996 " }",
6997 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6998 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6999 getGoogleStyle());
7002 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
7003 verifyFormat("enum E {};");
7004 verifyFormat("enum E {}");
7005 FormatStyle Style = getLLVMStyle();
7006 Style.SpaceInEmptyBlock = true;
7007 verifyFormat("void f() { }", "void f() {}", Style);
7008 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
7009 verifyFormat("{ }", Style);
7010 verifyFormat("while (true) { }", "while (true) {}", Style);
7011 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7012 Style.BraceWrapping.BeforeElse = false;
7013 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
7014 verifyFormat("if (a)\n"
7015 "{\n"
7016 "} else if (b)\n"
7017 "{\n"
7018 "} else\n"
7019 "{ }",
7020 Style);
7021 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
7022 verifyFormat("if (a) {\n"
7023 "} else if (b) {\n"
7024 "} else {\n"
7025 "}",
7026 Style);
7027 Style.BraceWrapping.BeforeElse = true;
7028 verifyFormat("if (a) { }\n"
7029 "else if (b) { }\n"
7030 "else { }",
7031 Style);
7033 Style = getLLVMStyle(FormatStyle::LK_CSharp);
7034 Style.SpaceInEmptyBlock = true;
7035 verifyFormat("Event += () => { };", Style);
7038 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
7039 FormatStyle Style = getLLVMStyle();
7040 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
7041 Style.MacroBlockEnd = "^[A-Z_]+_END$";
7042 verifyFormat("FOO_BEGIN\n"
7043 " FOO_ENTRY\n"
7044 "FOO_END",
7045 Style);
7046 verifyFormat("FOO_BEGIN\n"
7047 " NESTED_FOO_BEGIN\n"
7048 " NESTED_FOO_ENTRY\n"
7049 " NESTED_FOO_END\n"
7050 "FOO_END",
7051 Style);
7052 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7053 " int x;\n"
7054 " x = 1;\n"
7055 "FOO_END(Baz)",
7056 Style);
7058 Style.RemoveBracesLLVM = true;
7059 verifyNoCrash("for (;;)\n"
7060 " FOO_BEGIN\n"
7061 " foo();\n"
7062 " FOO_END",
7063 Style);
7066 //===----------------------------------------------------------------------===//
7067 // Line break tests.
7068 //===----------------------------------------------------------------------===//
7070 TEST_F(FormatTest, PreventConfusingIndents) {
7071 verifyFormat(
7072 "void f() {\n"
7073 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7074 " parameter, parameter, parameter)),\n"
7075 " SecondLongCall(parameter));\n"
7076 "}");
7077 verifyFormat(
7078 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7079 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7081 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7082 verifyFormat(
7083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7084 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7085 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7086 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7087 verifyFormat(
7088 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7089 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7091 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7092 verifyFormat("int a = bbbb && ccc &&\n"
7093 " fffff(\n"
7094 "#define A Just forcing a new line\n"
7095 " ddd);");
7098 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7099 verifyFormat(
7100 "bool aaaaaaa =\n"
7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7102 " bbbbbbbb();");
7103 verifyFormat(
7104 "bool aaaaaaa =\n"
7105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7106 " bbbbbbbb();");
7108 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7110 " ccccccccc == ddddddddddd;");
7111 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7113 " ccccccccc == ddddddddddd;");
7114 verifyFormat(
7115 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7117 " ccccccccc == ddddddddddd;");
7119 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7120 " aaaaaa) &&\n"
7121 " bbbbbb && cccccc;");
7122 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7123 " aaaaaa) >>\n"
7124 " bbbbbb;");
7125 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7126 " SourceMgr.getSpellingColumnNumber(\n"
7127 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7128 " 1);");
7130 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7131 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7132 " cccccc) {\n}");
7133 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7134 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7135 " cccccc) {\n}");
7136 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7137 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7138 " cccccc) {\n}");
7139 verifyFormat("b = a &&\n"
7140 " // Comment\n"
7141 " b.c && d;");
7143 // If the LHS of a comparison is not a binary expression itself, the
7144 // additional linebreak confuses many people.
7145 verifyFormat(
7146 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7148 "}");
7149 verifyFormat(
7150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7152 "}");
7153 verifyFormat(
7154 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7156 "}");
7157 verifyFormat(
7158 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7160 "}");
7161 // Even explicit parentheses stress the precedence enough to make the
7162 // additional break unnecessary.
7163 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7165 "}");
7166 // This cases is borderline, but with the indentation it is still readable.
7167 verifyFormat(
7168 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7169 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7171 "}",
7172 getLLVMStyleWithColumns(75));
7174 // If the LHS is a binary expression, we should still use the additional break
7175 // as otherwise the formatting hides the operator precedence.
7176 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7178 " 5) {\n"
7179 "}");
7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7182 " 5) {\n"
7183 "}");
7185 FormatStyle OnePerLine = getLLVMStyle();
7186 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7187 verifyFormat(
7188 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7191 OnePerLine);
7193 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7194 " .aaa(aaaaaaaaaaaaa) *\n"
7195 " aaaaaaa +\n"
7196 " aaaaaaa;",
7197 getLLVMStyleWithColumns(40));
7200 TEST_F(FormatTest, ExpressionIndentation) {
7201 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7205 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7206 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7209 " ccccccccccccccccccccccccccccccccccccccccc;");
7210 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7213 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7214 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7221 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7222 verifyFormat("if () {\n"
7223 "} else if (aaaaa && bbbbb > // break\n"
7224 " ccccc) {\n"
7225 "}");
7226 verifyFormat("if () {\n"
7227 "} else if constexpr (aaaaa && bbbbb > // break\n"
7228 " ccccc) {\n"
7229 "}");
7230 verifyFormat("if () {\n"
7231 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7232 " ccccc) {\n"
7233 "}");
7234 verifyFormat("if () {\n"
7235 "} else if (aaaaa &&\n"
7236 " bbbbb > // break\n"
7237 " ccccc &&\n"
7238 " ddddd) {\n"
7239 "}");
7241 // Presence of a trailing comment used to change indentation of b.
7242 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7243 " b;\n"
7244 "return aaaaaaaaaaaaaaaaaaa +\n"
7245 " b; //",
7246 getLLVMStyleWithColumns(30));
7249 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7250 // Not sure what the best system is here. Like this, the LHS can be found
7251 // immediately above an operator (everything with the same or a higher
7252 // indent). The RHS is aligned right of the operator and so compasses
7253 // everything until something with the same indent as the operator is found.
7254 // FIXME: Is this a good system?
7255 FormatStyle Style = getLLVMStyle();
7256 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7257 verifyFormat(
7258 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7259 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7260 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7261 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7262 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7264 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7265 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7266 " > ccccccccccccccccccccccccccccccccccccccccc;",
7267 Style);
7268 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7269 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7270 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7271 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7272 Style);
7273 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7274 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7275 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7276 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7277 Style);
7278 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7279 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7281 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7282 Style);
7283 verifyFormat("if () {\n"
7284 "} else if (aaaaa\n"
7285 " && bbbbb // break\n"
7286 " > ccccc) {\n"
7287 "}",
7288 Style);
7289 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7290 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7291 Style);
7292 verifyFormat("return (a)\n"
7293 " // comment\n"
7294 " + b;",
7295 Style);
7296 verifyFormat(
7297 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7298 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7299 " + cc;",
7300 Style);
7302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7303 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7304 Style);
7306 // Forced by comments.
7307 verifyFormat(
7308 "unsigned ContentSize =\n"
7309 " sizeof(int16_t) // DWARF ARange version number\n"
7310 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7311 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7312 " + sizeof(int8_t); // Segment Size (in bytes)");
7314 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7315 " == boost::fusion::at_c<1>(iiii).second;",
7316 Style);
7318 Style.ColumnLimit = 60;
7319 verifyFormat("zzzzzzzzzz\n"
7320 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7321 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7322 Style);
7324 Style.ColumnLimit = 80;
7325 Style.IndentWidth = 4;
7326 Style.TabWidth = 4;
7327 Style.UseTab = FormatStyle::UT_Always;
7328 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7329 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7330 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7331 "\t&& (someOtherLongishConditionPart1\n"
7332 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7333 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7334 "(someOtherLongishConditionPart1 || "
7335 "someOtherEvenLongerNestedConditionPart2);",
7336 Style);
7338 Style = getLLVMStyleWithColumns(20);
7339 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7340 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7341 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7342 Style.ContinuationIndentWidth = 2;
7343 verifyFormat("struct Foo {\n"
7344 " Foo(\n"
7345 " int arg1,\n"
7346 " int arg2)\n"
7347 " : Base(\n"
7348 " arg1,\n"
7349 " arg2) {}\n"
7350 "};",
7351 Style);
7352 verifyFormat("return abc\n"
7353 " ? foo(\n"
7354 " a,\n"
7355 " b,\n"
7356 " bar(\n"
7357 " abc))\n"
7358 " : g(abc);",
7359 Style);
7362 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7363 FormatStyle Style = getLLVMStyle();
7364 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7365 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7367 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7368 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7369 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7370 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7372 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7373 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7374 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7375 " > ccccccccccccccccccccccccccccccccccccccccc;",
7376 Style);
7377 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7378 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7379 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7380 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7381 Style);
7382 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7383 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7384 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7385 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7386 Style);
7387 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7388 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7389 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7390 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7391 Style);
7392 verifyFormat("if () {\n"
7393 "} else if (aaaaa\n"
7394 " && bbbbb // break\n"
7395 " > ccccc) {\n"
7396 "}",
7397 Style);
7398 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7399 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7400 Style);
7401 verifyFormat("return (a)\n"
7402 " // comment\n"
7403 " + b;",
7404 Style);
7405 verifyFormat(
7406 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7407 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7408 " + cc;",
7409 Style);
7410 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7411 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7412 " : 3333333333333333;",
7413 Style);
7414 verifyFormat(
7415 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7416 " : ccccccccccccccc ? dddddddddddddddddd\n"
7417 " : eeeeeeeeeeeeeeeeee)\n"
7418 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7419 " : 3333333333333333;",
7420 Style);
7421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7422 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7423 Style);
7425 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7426 " == boost::fusion::at_c<1>(iiii).second;",
7427 Style);
7429 Style.ColumnLimit = 60;
7430 verifyFormat("zzzzzzzzzzzzz\n"
7431 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7432 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7433 Style);
7435 // Forced by comments.
7436 Style.ColumnLimit = 80;
7437 verifyFormat(
7438 "unsigned ContentSize\n"
7439 " = sizeof(int16_t) // DWARF ARange version number\n"
7440 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7441 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7442 " + sizeof(int8_t); // Segment Size (in bytes)",
7443 Style);
7445 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7446 verifyFormat(
7447 "unsigned ContentSize =\n"
7448 " sizeof(int16_t) // DWARF ARange version number\n"
7449 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7450 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7451 " + sizeof(int8_t); // Segment Size (in bytes)",
7452 Style);
7454 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7455 verifyFormat(
7456 "unsigned ContentSize =\n"
7457 " sizeof(int16_t) // DWARF ARange version number\n"
7458 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7459 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7460 " + sizeof(int8_t); // Segment Size (in bytes)",
7461 Style);
7464 TEST_F(FormatTest, EnforcedOperatorWraps) {
7465 // Here we'd like to wrap after the || operators, but a comment is forcing an
7466 // earlier wrap.
7467 verifyFormat("bool x = aaaaa //\n"
7468 " || bbbbb\n"
7469 " //\n"
7470 " || cccc;");
7473 TEST_F(FormatTest, NoOperandAlignment) {
7474 FormatStyle Style = getLLVMStyle();
7475 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7476 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7479 Style);
7480 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7481 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7482 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7483 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7484 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7485 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7486 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7487 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7488 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489 " > ccccccccccccccccccccccccccccccccccccccccc;",
7490 Style);
7492 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7493 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7494 " + cc;",
7495 Style);
7496 verifyFormat("int a = aa\n"
7497 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7498 " * cccccccccccccccccccccccccccccccccccc;",
7499 Style);
7501 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7502 verifyFormat("return (a > b\n"
7503 " // comment1\n"
7504 " // comment2\n"
7505 " || c);",
7506 Style);
7509 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7510 FormatStyle Style = getLLVMStyle();
7511 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7512 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7515 Style);
7518 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7519 FormatStyle Style = getLLVMStyleWithColumns(40);
7520 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7521 Style.BinPackArguments = false;
7522 verifyFormat("void test() {\n"
7523 " someFunction(\n"
7524 " this + argument + is + quite\n"
7525 " + long + so + it + gets + wrapped\n"
7526 " + but + remains + bin - packed);\n"
7527 "}",
7528 Style);
7529 verifyFormat("void test() {\n"
7530 " someFunction(arg1,\n"
7531 " this + argument + is\n"
7532 " + quite + long + so\n"
7533 " + it + gets + wrapped\n"
7534 " + but + remains + bin\n"
7535 " - packed,\n"
7536 " arg3);\n"
7537 "}",
7538 Style);
7539 verifyFormat("void test() {\n"
7540 " someFunction(\n"
7541 " arg1,\n"
7542 " this + argument + has\n"
7543 " + anotherFunc(nested,\n"
7544 " calls + whose\n"
7545 " + arguments\n"
7546 " + are + also\n"
7547 " + wrapped,\n"
7548 " in + addition)\n"
7549 " + to + being + bin - packed,\n"
7550 " arg3);\n"
7551 "}",
7552 Style);
7554 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7555 verifyFormat("void test() {\n"
7556 " someFunction(\n"
7557 " arg1,\n"
7558 " this + argument + has +\n"
7559 " anotherFunc(nested,\n"
7560 " calls + whose +\n"
7561 " arguments +\n"
7562 " are + also +\n"
7563 " wrapped,\n"
7564 " in + addition) +\n"
7565 " to + being + bin - packed,\n"
7566 " arg3);\n"
7567 "}",
7568 Style);
7571 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7572 auto Style = getLLVMStyleWithColumns(45);
7573 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7574 verifyFormat("bool b =\n"
7575 " is_default_constructible_v<hash<T>> and\n"
7576 " is_copy_constructible_v<hash<T>> and\n"
7577 " is_move_constructible_v<hash<T>> and\n"
7578 " is_copy_assignable_v<hash<T>> and\n"
7579 " is_move_assignable_v<hash<T>> and\n"
7580 " is_destructible_v<hash<T>> and\n"
7581 " is_swappable_v<hash<T>> and\n"
7582 " is_callable_v<hash<T>(T)>;",
7583 Style);
7585 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7586 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7587 " and is_copy_constructible_v<hash<T>>\n"
7588 " and is_move_constructible_v<hash<T>>\n"
7589 " and is_copy_assignable_v<hash<T>>\n"
7590 " and is_move_assignable_v<hash<T>>\n"
7591 " and is_destructible_v<hash<T>>\n"
7592 " and is_swappable_v<hash<T>>\n"
7593 " and is_callable_v<hash<T>(T)>;",
7594 Style);
7596 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7597 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7598 " and is_copy_constructible_v<hash<T>>\n"
7599 " and is_move_constructible_v<hash<T>>\n"
7600 " and is_copy_assignable_v<hash<T>>\n"
7601 " and is_move_assignable_v<hash<T>>\n"
7602 " and is_destructible_v<hash<T>>\n"
7603 " and is_swappable_v<hash<T>>\n"
7604 " and is_callable_v<hash<T>(T)>;",
7605 Style);
7608 TEST_F(FormatTest, ConstructorInitializers) {
7609 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7610 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7611 getLLVMStyleWithColumns(45));
7612 verifyFormat("Constructor()\n"
7613 " : Inttializer(FitsOnTheLine) {}",
7614 getLLVMStyleWithColumns(44));
7615 verifyFormat("Constructor()\n"
7616 " : Inttializer(FitsOnTheLine) {}",
7617 getLLVMStyleWithColumns(43));
7619 verifyFormat("template <typename T>\n"
7620 "Constructor() : Initializer(FitsOnTheLine) {}",
7621 getLLVMStyleWithColumns(45));
7623 verifyFormat(
7624 "SomeClass::Constructor()\n"
7625 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7627 verifyFormat(
7628 "SomeClass::Constructor()\n"
7629 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7630 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7631 verifyFormat(
7632 "SomeClass::Constructor()\n"
7633 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7634 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7635 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7636 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7637 " : aaaaaaaaaa(aaaaaa) {}");
7639 verifyFormat("Constructor()\n"
7640 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7641 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7642 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7643 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7645 verifyFormat("Constructor()\n"
7646 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7649 verifyFormat("Constructor(int Parameter = 0)\n"
7650 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7651 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7652 verifyFormat("Constructor()\n"
7653 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7654 "}",
7655 getLLVMStyleWithColumns(60));
7656 verifyFormat("Constructor()\n"
7657 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7658 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7660 // Here a line could be saved by splitting the second initializer onto two
7661 // lines, but that is not desirable.
7662 verifyFormat("Constructor()\n"
7663 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7664 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7665 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7667 FormatStyle OnePerLine = getLLVMStyle();
7668 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7669 verifyFormat("MyClass::MyClass()\n"
7670 " : a(a),\n"
7671 " b(b),\n"
7672 " c(c) {}",
7673 OnePerLine);
7674 verifyFormat("MyClass::MyClass()\n"
7675 " : a(a), // comment\n"
7676 " b(b),\n"
7677 " c(c) {}",
7678 OnePerLine);
7679 verifyFormat("MyClass::MyClass(int a)\n"
7680 " : b(a), // comment\n"
7681 " c(a + 1) { // lined up\n"
7682 "}",
7683 OnePerLine);
7684 verifyFormat("Constructor()\n"
7685 " : a(b, b, b) {}",
7686 OnePerLine);
7687 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7688 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7689 verifyFormat("SomeClass::Constructor()\n"
7690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7691 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7692 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7693 OnePerLine);
7694 verifyFormat("SomeClass::Constructor()\n"
7695 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7696 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7697 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7698 OnePerLine);
7699 verifyFormat("MyClass::MyClass(int var)\n"
7700 " : some_var_(var), // 4 space indent\n"
7701 " some_other_var_(var + 1) { // lined up\n"
7702 "}",
7703 OnePerLine);
7704 verifyFormat("Constructor()\n"
7705 " : aaaaa(aaaaaa),\n"
7706 " aaaaa(aaaaaa),\n"
7707 " aaaaa(aaaaaa),\n"
7708 " aaaaa(aaaaaa),\n"
7709 " aaaaa(aaaaaa) {}",
7710 OnePerLine);
7711 verifyFormat("Constructor()\n"
7712 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7713 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7714 OnePerLine);
7715 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7716 verifyFormat(
7717 "Constructor()\n"
7718 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7719 " aaaaaaaaaaa().aaa(),\n"
7720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7721 OnePerLine);
7722 OnePerLine.ColumnLimit = 60;
7723 verifyFormat("Constructor()\n"
7724 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7725 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7726 OnePerLine);
7728 verifyFormat("Constructor()\n"
7729 " : // Comment forcing unwanted break.\n"
7730 " aaaa(aaaa) {}",
7731 "Constructor() :\n"
7732 " // Comment forcing unwanted break.\n"
7733 " aaaa(aaaa) {}");
7736 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7737 FormatStyle Style = getLLVMStyleWithColumns(60);
7738 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7739 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7741 for (int i = 0; i < 4; ++i) {
7742 // Test all combinations of parameters that should not have an effect.
7743 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7744 Style.AllowAllArgumentsOnNextLine = i & 2;
7746 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7747 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7748 verifyFormat("Constructor()\n"
7749 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7750 Style);
7751 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7753 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7754 verifyFormat("Constructor()\n"
7755 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7756 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7757 Style);
7758 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7760 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7761 verifyFormat("Constructor()\n"
7762 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7763 Style);
7764 verifyFormat("Constructor()\n"
7765 " : a(a), b(b) {}",
7766 Style);
7767 verifyFormat("Constructor()\n"
7768 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7769 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7770 " , cccccccccccccccccccccc(c) {}",
7771 Style);
7773 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7774 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7775 verifyFormat("Constructor()\n"
7776 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7777 Style);
7779 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7780 verifyFormat("Constructor()\n"
7781 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7782 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7783 Style);
7785 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7786 verifyFormat("Constructor()\n"
7787 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7788 Style);
7789 verifyFormat("Constructor()\n"
7790 " : a(a), b(b) {}",
7791 Style);
7792 verifyFormat("Constructor()\n"
7793 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7794 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7795 " cccccccccccccccccccccc(c) {}",
7796 Style);
7798 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7799 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7800 verifyFormat("Constructor() :\n"
7801 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7802 Style);
7804 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7805 verifyFormat("Constructor() :\n"
7806 " aaaaaaaaaaaaaaaaaa(a),\n"
7807 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7808 Style);
7810 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7811 verifyFormat("Constructor() :\n"
7812 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7813 Style);
7814 verifyFormat("Constructor() :\n"
7815 " a(a), b(b) {}",
7816 Style);
7817 verifyFormat("Constructor() :\n"
7818 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7819 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7820 " cccccccccccccccccccccc(c) {}",
7821 Style);
7824 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7825 // AllowAllConstructorInitializersOnNextLine in all
7826 // BreakConstructorInitializers modes
7827 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7828 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7829 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7830 verifyFormat("SomeClassWithALongName::Constructor(\n"
7831 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7832 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7833 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7834 Style);
7836 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7837 verifyFormat("SomeClassWithALongName::Constructor(\n"
7838 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7839 " int bbbbbbbbbbbbb,\n"
7840 " int cccccccccccccccc)\n"
7841 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7842 Style);
7844 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7845 verifyFormat("SomeClassWithALongName::Constructor(\n"
7846 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7847 " int bbbbbbbbbbbbb,\n"
7848 " int cccccccccccccccc)\n"
7849 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7850 Style);
7852 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7853 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7854 verifyFormat("SomeClassWithALongName::Constructor(\n"
7855 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7856 " int bbbbbbbbbbbbb)\n"
7857 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7858 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7859 Style);
7861 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7863 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7864 verifyFormat("SomeClassWithALongName::Constructor(\n"
7865 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7866 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7867 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7868 Style);
7870 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7871 verifyFormat("SomeClassWithALongName::Constructor(\n"
7872 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7873 " int bbbbbbbbbbbbb,\n"
7874 " int cccccccccccccccc)\n"
7875 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7876 Style);
7878 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7879 verifyFormat("SomeClassWithALongName::Constructor(\n"
7880 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7881 " int bbbbbbbbbbbbb,\n"
7882 " int cccccccccccccccc)\n"
7883 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7884 Style);
7886 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7887 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7888 verifyFormat("SomeClassWithALongName::Constructor(\n"
7889 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7890 " int bbbbbbbbbbbbb)\n"
7891 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7892 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7893 Style);
7895 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7896 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7897 verifyFormat("SomeClassWithALongName::Constructor(\n"
7898 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7899 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7900 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7901 Style);
7903 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7904 verifyFormat("SomeClassWithALongName::Constructor(\n"
7905 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7906 " int bbbbbbbbbbbbb,\n"
7907 " int cccccccccccccccc) :\n"
7908 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7909 Style);
7911 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7912 verifyFormat("SomeClassWithALongName::Constructor(\n"
7913 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7914 " int bbbbbbbbbbbbb,\n"
7915 " int cccccccccccccccc) :\n"
7916 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7917 Style);
7919 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7920 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7921 verifyFormat("SomeClassWithALongName::Constructor(\n"
7922 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7923 " int bbbbbbbbbbbbb) :\n"
7924 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7925 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7926 Style);
7928 Style = getLLVMStyleWithColumns(0);
7929 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7930 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7931 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7932 " : bar(bar), baz(baz) {}",
7933 Style);
7936 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7937 FormatStyle Style = getLLVMStyleWithColumns(60);
7938 Style.BinPackArguments = false;
7939 for (int i = 0; i < 4; ++i) {
7940 // Test all combinations of parameters that should not have an effect.
7941 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7942 Style.PackConstructorInitializers =
7943 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7945 Style.AllowAllArgumentsOnNextLine = true;
7946 verifyFormat("void foo() {\n"
7947 " FunctionCallWithReallyLongName(\n"
7948 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7949 "}",
7950 Style);
7951 Style.AllowAllArgumentsOnNextLine = false;
7952 verifyFormat("void foo() {\n"
7953 " FunctionCallWithReallyLongName(\n"
7954 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7955 " bbbbbbbbbbbb);\n"
7956 "}",
7957 Style);
7959 Style.AllowAllArgumentsOnNextLine = true;
7960 verifyFormat("void foo() {\n"
7961 " auto VariableWithReallyLongName = {\n"
7962 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7963 "}",
7964 Style);
7965 Style.AllowAllArgumentsOnNextLine = false;
7966 verifyFormat("void foo() {\n"
7967 " auto VariableWithReallyLongName = {\n"
7968 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7969 " bbbbbbbbbbbb};\n"
7970 "}",
7971 Style);
7974 // This parameter should not affect declarations.
7975 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7976 Style.AllowAllArgumentsOnNextLine = false;
7977 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7978 verifyFormat("void FunctionCallWithReallyLongName(\n"
7979 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7980 Style);
7981 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7982 verifyFormat("void FunctionCallWithReallyLongName(\n"
7983 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7984 " int bbbbbbbbbbbb);",
7985 Style);
7988 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7989 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7990 // and BAS_Align.
7991 FormatStyle Style = getLLVMStyleWithColumns(35);
7992 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7993 "void functionDecl(int A, int B, int C);";
7994 Style.AllowAllArgumentsOnNextLine = false;
7995 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7996 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7997 " paramC);\n"
7998 "void functionDecl(int A, int B,\n"
7999 " int C);"),
8000 Input, Style);
8001 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8002 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8003 " paramC);\n"
8004 "void functionDecl(int A, int B,\n"
8005 " int C);"),
8006 Input, Style);
8007 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
8008 // AllowAllArgumentsOnNextLine.
8009 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8010 verifyFormat(StringRef("functionCall(\n"
8011 " paramA, paramB, paramC);\n"
8012 "void functionDecl(\n"
8013 " int A, int B, int C);"),
8014 Input, Style);
8015 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8016 verifyFormat("functionCall(\n"
8017 " paramA, paramB, paramC\n"
8018 ");\n"
8019 "void functionDecl(\n"
8020 " int A, int B, int C\n"
8021 ");",
8022 Input, Style);
8024 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8025 // first argument.
8026 Style.AllowAllArgumentsOnNextLine = true;
8027 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8028 verifyFormat(StringRef("functionCall(\n"
8029 " paramA, paramB, paramC);\n"
8030 "void functionDecl(\n"
8031 " int A, int B, int C);"),
8032 Input, Style);
8033 // It wouldn't fit on one line with aligned parameters so this setting
8034 // doesn't change anything for BAS_Align.
8035 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8036 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8037 " paramC);\n"
8038 "void functionDecl(int A, int B,\n"
8039 " int C);"),
8040 Input, Style);
8041 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8042 verifyFormat(StringRef("functionCall(\n"
8043 " paramA, paramB, paramC);\n"
8044 "void functionDecl(\n"
8045 " int A, int B, int C);"),
8046 Input, Style);
8049 TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8050 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8051 "void emptyFunctionDefinition() {}\n"
8052 "void functionDefinition(int A, int B, int C) {}\n"
8053 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8054 verifyFormat(Input);
8056 FormatStyle Style = getLLVMStyle();
8057 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8058 Style.BreakFunctionDefinitionParameters = true;
8059 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8060 "void emptyFunctionDefinition() {}\n"
8061 "void functionDefinition(\n"
8062 " int A, int B, int C) {}\n"
8063 "Class::Class(\n"
8064 " int A, int B)\n"
8065 " : m_A(A), m_B(B) {}",
8066 Input, Style);
8068 // Test the style where all parameters are on their own lines.
8069 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8070 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8071 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8072 "void emptyFunctionDefinition() {}\n"
8073 "void functionDefinition(\n"
8074 " int A,\n"
8075 " int B,\n"
8076 " int C) {}\n"
8077 "Class::Class(\n"
8078 " int A,\n"
8079 " int B)\n"
8080 " : m_A(A), m_B(B) {}",
8081 Input, Style);
8084 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8085 FormatStyle Style = getLLVMStyle();
8086 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8087 /* Test the behaviour with long lines */
8088 Style.ColumnLimit = 40;
8089 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8090 " : : val);",
8091 Style);
8092 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8093 " : val1 : val2);",
8094 Style);
8095 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8096 " \"cpuid\\n\\t\"\n"
8097 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8098 " : \"=a\" : \"a\");",
8099 Style);
8100 Style.ColumnLimit = 80;
8101 verifyFormat("asm volatile(\"string\", : : val);", Style);
8102 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8104 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8105 verifyFormat("asm volatile(\"string\",\n"
8106 " :\n"
8107 " : val);",
8108 Style);
8109 verifyFormat("asm volatile(\"string\",\n"
8110 " : val1\n"
8111 " : val2);",
8112 Style);
8113 /* Test the behaviour with long lines */
8114 Style.ColumnLimit = 40;
8115 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8116 " \"cpuid\\n\\t\"\n"
8117 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8118 " : \"=a\"(*rEAX)\n"
8119 " : \"a\"(value));",
8120 Style);
8121 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8122 " \"cpuid\\n\\t\"\n"
8123 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8124 " :\n"
8125 " : \"a\"(value));",
8126 Style);
8127 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8128 " :\n"
8129 " : val);",
8130 Style);
8131 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8132 " : val1\n"
8133 " : val2);",
8134 Style);
8137 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8138 FormatStyle Style = getLLVMStyle();
8139 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8141 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8142 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8143 getStyleWithColumns(Style, 45));
8144 verifyFormat("Constructor() :\n"
8145 " Initializer(FitsOnTheLine) {}",
8146 getStyleWithColumns(Style, 44));
8147 verifyFormat("Constructor() :\n"
8148 " Initializer(FitsOnTheLine) {}",
8149 getStyleWithColumns(Style, 43));
8151 verifyFormat("template <typename T>\n"
8152 "Constructor() : Initializer(FitsOnTheLine) {}",
8153 getStyleWithColumns(Style, 50));
8154 verifyFormat(
8155 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8156 " int mooooooooooooore) noexcept :\n"
8157 " Super{some, arguments}, Member{5}, Member2{2} {}",
8158 Style);
8159 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8160 verifyFormat(
8161 "SomeClass::Constructor() :\n"
8162 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8163 Style);
8164 verifyFormat(
8165 "SomeClass::Constructor() : // NOLINT\n"
8166 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8167 Style);
8169 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8170 verifyFormat(
8171 "SomeClass::Constructor() :\n"
8172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8173 Style);
8174 verifyFormat(
8175 "SomeClass::Constructor() : // NOLINT\n"
8176 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8177 Style);
8179 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8180 verifyFormat(
8181 "SomeClass::Constructor() :\n"
8182 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8183 Style);
8185 verifyFormat(
8186 "SomeClass::Constructor() :\n"
8187 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8189 Style);
8190 verifyFormat(
8191 "SomeClass::Constructor() :\n"
8192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8193 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8194 Style);
8195 verifyFormat(
8196 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8197 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8198 Style);
8200 verifyFormat("Constructor() :\n"
8201 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8202 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8204 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8205 Style);
8207 verifyFormat("Constructor() :\n"
8208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8210 Style);
8212 verifyFormat("Constructor(int Parameter = 0) :\n"
8213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8214 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8215 Style);
8216 verifyFormat("Constructor() :\n"
8217 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8218 "}",
8219 getStyleWithColumns(Style, 60));
8220 verifyFormat("Constructor() :\n"
8221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8222 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8223 Style);
8225 // Here a line could be saved by splitting the second initializer onto two
8226 // lines, but that is not desirable.
8227 verifyFormat("Constructor() :\n"
8228 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8229 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8230 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8231 Style);
8233 FormatStyle OnePerLine = Style;
8234 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8235 verifyFormat("SomeClass::Constructor() :\n"
8236 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8237 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8238 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8239 OnePerLine);
8240 verifyFormat("SomeClass::Constructor() :\n"
8241 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8242 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8243 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8244 OnePerLine);
8245 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8246 " i(i), // comment\n"
8247 " j(j) {}",
8248 OnePerLine);
8249 verifyFormat("MyClass::MyClass(int var) :\n"
8250 " some_var_(var), // 4 space indent\n"
8251 " some_other_var_(var + 1) { // lined up\n"
8252 "}",
8253 OnePerLine);
8254 verifyFormat("Constructor() :\n"
8255 " aaaaa(aaaaaa),\n"
8256 " aaaaa(aaaaaa),\n"
8257 " aaaaa(aaaaaa),\n"
8258 " aaaaa(aaaaaa),\n"
8259 " aaaaa(aaaaaa) {}",
8260 OnePerLine);
8261 verifyFormat("Constructor() :\n"
8262 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8263 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8264 OnePerLine);
8265 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8266 verifyFormat("Constructor() :\n"
8267 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8268 " aaaaaaaaaaa().aaa(),\n"
8269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8270 OnePerLine);
8271 OnePerLine.ColumnLimit = 60;
8272 verifyFormat("Constructor() :\n"
8273 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8274 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8275 OnePerLine);
8277 verifyFormat("Constructor() :\n"
8278 " // Comment forcing unwanted break.\n"
8279 " aaaa(aaaa) {}",
8280 Style);
8281 verifyFormat("Constructor() : // NOLINT\n"
8282 " aaaa(aaaa) {}",
8283 Style);
8284 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8285 " on a single\n"
8286 " // line.\n"
8287 " aaaa(aaaa) {}",
8288 "Constructor() : // A very long trailing comment that cannot fit"
8289 " on a single line.\n"
8290 " aaaa(aaaa) {}",
8291 Style);
8293 Style.ColumnLimit = 0;
8294 verifyFormat("SomeClass::Constructor() :\n"
8295 " a(a) {}",
8296 Style);
8297 verifyFormat("SomeClass::Constructor() noexcept :\n"
8298 " a(a) {}",
8299 Style);
8300 verifyFormat("SomeClass::Constructor() :\n"
8301 " a(a), b(b), c(c) {}",
8302 Style);
8303 verifyFormat("SomeClass::Constructor() :\n"
8304 " a(a) {\n"
8305 " foo();\n"
8306 " bar();\n"
8307 "}",
8308 Style);
8310 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8311 verifyFormat("SomeClass::Constructor() :\n"
8312 " a(a), b(b), c(c) {\n"
8313 "}",
8314 Style);
8315 verifyFormat("SomeClass::Constructor() :\n"
8316 " a(a) {\n"
8317 "}",
8318 Style);
8320 Style.ColumnLimit = 80;
8321 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8322 Style.ConstructorInitializerIndentWidth = 2;
8323 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8324 verifyFormat("SomeClass::Constructor() :\n"
8325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8326 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8327 Style);
8329 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8330 // well
8331 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8332 verifyFormat(
8333 "class SomeClass\n"
8334 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8335 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8336 Style);
8337 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8338 verifyFormat(
8339 "class SomeClass\n"
8340 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8341 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8342 Style);
8343 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8344 verifyFormat(
8345 "class SomeClass :\n"
8346 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8347 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8348 Style);
8349 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8350 verifyFormat(
8351 "class SomeClass\n"
8352 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8353 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8354 Style);
8357 #ifndef EXPENSIVE_CHECKS
8358 // Expensive checks enables libstdc++ checking which includes validating the
8359 // state of ranges used in std::priority_queue - this blows out the
8360 // runtime/scalability of the function and makes this test unacceptably slow.
8361 TEST_F(FormatTest, MemoizationTests) {
8362 // This breaks if the memoization lookup does not take \c Indent and
8363 // \c LastSpace into account.
8364 verifyFormat(
8365 "extern CFRunLoopTimerRef\n"
8366 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8367 " CFTimeInterval interval, CFOptionFlags flags,\n"
8368 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8369 " CFRunLoopTimerContext *context) {}");
8371 // Deep nesting somewhat works around our memoization.
8372 verifyFormat(
8373 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8374 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8375 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8376 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8377 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8378 getLLVMStyleWithColumns(65));
8379 verifyFormat(
8380 "aaaaa(\n"
8381 " aaaaa,\n"
8382 " aaaaa(\n"
8383 " aaaaa,\n"
8384 " aaaaa(\n"
8385 " aaaaa,\n"
8386 " aaaaa(\n"
8387 " aaaaa,\n"
8388 " aaaaa(\n"
8389 " aaaaa,\n"
8390 " aaaaa(\n"
8391 " aaaaa,\n"
8392 " aaaaa(\n"
8393 " aaaaa,\n"
8394 " aaaaa(\n"
8395 " aaaaa,\n"
8396 " aaaaa(\n"
8397 " aaaaa,\n"
8398 " aaaaa(\n"
8399 " aaaaa,\n"
8400 " aaaaa(\n"
8401 " aaaaa,\n"
8402 " aaaaa(\n"
8403 " aaaaa,\n"
8404 " aaaaa))))))))))));",
8405 getLLVMStyleWithColumns(65));
8406 verifyFormat(
8407 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
8408 " a),\n"
8409 " a),\n"
8410 " a),\n"
8411 " a),\n"
8412 " a),\n"
8413 " a),\n"
8414 " a),\n"
8415 " a),\n"
8416 " a),\n"
8417 " a),\n"
8418 " a),\n"
8419 " a),\n"
8420 " a),\n"
8421 " a),\n"
8422 " a),\n"
8423 " a),\n"
8424 " a)",
8425 getLLVMStyleWithColumns(65));
8427 // This test takes VERY long when memoization is broken.
8428 FormatStyle OnePerLine = getLLVMStyle();
8429 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8430 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8431 std::string input = "Constructor()\n"
8432 " : aaaa(a,\n";
8433 for (unsigned i = 0, e = 80; i != e; ++i)
8434 input += " a,\n";
8435 input += " a) {}";
8436 verifyFormat(input, OnePerLine);
8437 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8438 verifyFormat(input, OnePerLine);
8440 #endif
8442 TEST_F(FormatTest, BreaksAsHighAsPossible) {
8443 verifyFormat(
8444 "void f() {\n"
8445 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8446 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8447 " f();\n"
8448 "}");
8449 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8450 " Intervals[i - 1].getRange().getLast()) {\n}");
8453 TEST_F(FormatTest, BreaksFunctionDeclarations) {
8454 // Principially, we break function declarations in a certain order:
8455 // 1) break amongst arguments.
8456 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8457 " Cccccccccccccc cccccccccccccc);");
8458 verifyFormat("template <class TemplateIt>\n"
8459 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8460 " TemplateIt *stop) {}");
8462 // 2) break after return type.
8463 verifyGoogleFormat(
8464 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8465 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8467 // 3) break after (.
8468 verifyGoogleFormat(
8469 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8470 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8472 // 4) break before after nested name specifiers.
8473 verifyGoogleFormat(
8474 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8475 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8476 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8478 // However, there are exceptions, if a sufficient amount of lines can be
8479 // saved.
8480 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8481 // more adjusting.
8482 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8483 " Cccccccccccccc cccccccccc,\n"
8484 " Cccccccccccccc cccccccccc,\n"
8485 " Cccccccccccccc cccccccccc,\n"
8486 " Cccccccccccccc cccccccccc);");
8487 verifyGoogleFormat(
8488 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8489 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8490 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8491 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8492 verifyFormat(
8493 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8494 " Cccccccccccccc cccccccccc,\n"
8495 " Cccccccccccccc cccccccccc,\n"
8496 " Cccccccccccccc cccccccccc,\n"
8497 " Cccccccccccccc cccccccccc,\n"
8498 " Cccccccccccccc cccccccccc,\n"
8499 " Cccccccccccccc cccccccccc);");
8500 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8501 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8502 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8503 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8504 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8506 // Break after multi-line parameters.
8507 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8510 " bbbb bbbb);");
8511 verifyFormat("void SomeLoooooooooooongFunction(\n"
8512 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8513 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8514 " int bbbbbbbbbbbbb);");
8516 // Treat overloaded operators like other functions.
8517 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8518 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8519 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8520 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8521 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8522 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8523 verifyGoogleFormat(
8524 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8525 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8526 verifyGoogleFormat(
8527 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8528 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8529 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8530 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8531 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8532 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8533 verifyGoogleFormat(
8534 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8535 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8536 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8537 verifyGoogleFormat("template <typename T>\n"
8538 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8539 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8540 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8542 FormatStyle Style = getLLVMStyle();
8543 Style.PointerAlignment = FormatStyle::PAS_Left;
8544 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8545 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8546 Style);
8547 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8549 Style);
8552 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8553 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8554 // Prefer keeping `::` followed by `operator` together.
8555 verifyFormat("const aaaa::bbbbbbb &\n"
8556 "ccccccccc::operator++() {\n"
8557 " stuff();\n"
8558 "}",
8559 "const aaaa::bbbbbbb\n"
8560 "&ccccccccc::operator++() { stuff(); }",
8561 getLLVMStyleWithColumns(40));
8564 TEST_F(FormatTest, TrailingReturnType) {
8565 verifyFormat("auto foo() -> int;");
8566 // correct trailing return type spacing
8567 verifyFormat("auto operator->() -> int;");
8568 verifyFormat("auto operator++(int) -> int;");
8570 verifyFormat("struct S {\n"
8571 " auto bar() const -> int;\n"
8572 "};");
8573 verifyFormat("template <size_t Order, typename T>\n"
8574 "auto load_img(const std::string &filename)\n"
8575 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8576 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8577 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8578 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8579 verifyFormat("template <typename T>\n"
8580 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8581 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8583 FormatStyle Style = getLLVMStyleWithColumns(60);
8584 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8585 " auto NAME() -> int { return 42; }",
8586 Style);
8588 // Not trailing return types.
8589 verifyFormat("void f() { auto a = b->c(); }");
8590 verifyFormat("auto a = p->foo();");
8591 verifyFormat("int a = p->foo();");
8592 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8595 TEST_F(FormatTest, DeductionGuides) {
8596 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8597 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8598 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8599 verifyFormat(
8600 "template <class... T>\n"
8601 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8602 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8603 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8604 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8605 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8606 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8607 verifyFormat("template <class T> x() -> x<1>;");
8608 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8610 verifyFormat("A(const char *) -> A<string &>;");
8611 verifyFormat("A() -> A<int>;");
8613 // Ensure not deduction guides.
8614 verifyFormat("c()->f<int>();");
8615 verifyFormat("x()->foo<1>;");
8616 verifyFormat("x = p->foo<3>();");
8617 verifyFormat("x()->x<1>();");
8620 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8621 // Avoid breaking before trailing 'const' or other trailing annotations, if
8622 // they are not function-like.
8623 FormatStyle Style = getGoogleStyleWithColumns(47);
8624 verifyFormat("void someLongFunction(\n"
8625 " int someLoooooooooooooongParameter) const {\n}",
8626 getLLVMStyleWithColumns(47));
8627 verifyFormat("LoooooongReturnType\n"
8628 "someLoooooooongFunction() const {}",
8629 getLLVMStyleWithColumns(47));
8630 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8631 " const {}",
8632 Style);
8633 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8634 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8635 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8636 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8637 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8638 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8639 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8640 " aaaaaaaaaaa aaaaa) const override;");
8641 verifyGoogleFormat(
8642 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8643 " const override;");
8645 // Even if the first parameter has to be wrapped.
8646 verifyFormat("void someLongFunction(\n"
8647 " int someLongParameter) const {}",
8648 getLLVMStyleWithColumns(46));
8649 verifyFormat("void someLongFunction(\n"
8650 " int someLongParameter) const {}",
8651 Style);
8652 verifyFormat("void someLongFunction(\n"
8653 " int someLongParameter) override {}",
8654 Style);
8655 verifyFormat("void someLongFunction(\n"
8656 " int someLongParameter) OVERRIDE {}",
8657 Style);
8658 verifyFormat("void someLongFunction(\n"
8659 " int someLongParameter) final {}",
8660 Style);
8661 verifyFormat("void someLongFunction(\n"
8662 " int someLongParameter) FINAL {}",
8663 Style);
8664 verifyFormat("void someLongFunction(\n"
8665 " int parameter) const override {}",
8666 Style);
8668 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8669 verifyFormat("void someLongFunction(\n"
8670 " int someLongParameter) const\n"
8671 "{\n"
8672 "}",
8673 Style);
8675 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8676 verifyFormat("void someLongFunction(\n"
8677 " int someLongParameter) const\n"
8678 " {\n"
8679 " }",
8680 Style);
8682 // Unless these are unknown annotations.
8683 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8684 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8685 " LONG_AND_UGLY_ANNOTATION;");
8687 // Breaking before function-like trailing annotations is fine to keep them
8688 // close to their arguments.
8689 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8690 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8691 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8692 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8693 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8694 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8695 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8696 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8697 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8699 verifyFormat(
8700 "void aaaaaaaaaaaaaaaaaa()\n"
8701 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8702 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8703 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8704 " __attribute__((unused));");
8706 Style = getGoogleStyle();
8708 verifyFormat(
8709 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8710 " GUARDED_BY(aaaaaaaaaaaa);",
8711 Style);
8712 verifyFormat(
8713 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8714 " GUARDED_BY(aaaaaaaaaaaa);",
8715 Style);
8716 verifyFormat(
8717 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8718 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8719 Style);
8720 verifyFormat(
8721 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8722 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8723 Style);
8725 verifyFormat(
8726 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8727 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8728 Style);
8729 verifyFormat(
8730 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8731 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8732 Style);
8733 verifyFormat(
8734 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8735 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8736 Style);
8737 verifyFormat(
8738 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8739 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8740 Style);
8743 TEST_F(FormatTest, FunctionAnnotations) {
8744 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8745 "int OldFunction(const string &parameter) {}");
8746 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8747 "string OldFunction(const string &parameter) {}");
8748 verifyFormat("template <typename T>\n"
8749 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8750 "string OldFunction(const string &parameter) {}");
8752 // Not function annotations.
8753 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8754 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8755 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8756 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8757 verifyFormat("MACRO(abc).function() // wrap\n"
8758 " << abc;");
8759 verifyFormat("MACRO(abc)->function() // wrap\n"
8760 " << abc;");
8761 verifyFormat("MACRO(abc)::function() // wrap\n"
8762 " << abc;");
8763 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8766 TEST_F(FormatTest, BreaksDesireably) {
8767 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8768 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8769 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8772 "}");
8774 verifyFormat(
8775 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8778 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8782 verifyFormat(
8783 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8784 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8786 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8790 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8792 verifyFormat(
8793 "void f() {\n"
8794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8796 "}");
8797 verifyFormat(
8798 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8800 verifyFormat(
8801 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8803 verifyFormat(
8804 "aaaaaa(aaa,\n"
8805 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8807 " aaaa);");
8808 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8812 // Indent consistently independent of call expression and unary operator.
8813 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8814 " dddddddddddddddddddddddddddddd));");
8815 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8816 " dddddddddddddddddddddddddddddd));");
8817 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8818 " dddddddddddddddddddddddddddddd));");
8820 // This test case breaks on an incorrect memoization, i.e. an optimization not
8821 // taking into account the StopAt value.
8822 verifyFormat(
8823 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8824 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8825 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8826 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8828 verifyFormat("{\n {\n {\n"
8829 " Annotation.SpaceRequiredBefore =\n"
8830 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8831 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8832 " }\n }\n}");
8834 // Break on an outer level if there was a break on an inner level.
8835 verifyFormat("f(g(h(a, // comment\n"
8836 " b, c),\n"
8837 " d, e),\n"
8838 " x, y);",
8839 "f(g(h(a, // comment\n"
8840 " b, c), d, e), x, y);");
8842 // Prefer breaking similar line breaks.
8843 verifyFormat(
8844 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8845 " NSTrackingMouseEnteredAndExited |\n"
8846 " NSTrackingActiveAlways;");
8849 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8850 FormatStyle NoBinPacking = getGoogleStyle();
8851 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8852 NoBinPacking.BinPackArguments = true;
8853 verifyFormat("void f() {\n"
8854 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8856 "}",
8857 NoBinPacking);
8858 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8859 " int aaaaaaaaaaaaaaaaaaaa,\n"
8860 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8861 NoBinPacking);
8863 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8864 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8865 " vector<int> bbbbbbbbbbbbbbb);",
8866 NoBinPacking);
8867 // FIXME: This behavior difference is probably not wanted. However, currently
8868 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8869 // template arguments from BreakBeforeParameter being set because of the
8870 // one-per-line formatting.
8871 verifyFormat(
8872 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8873 " aaaaaaaaaa> aaaaaaaaaa);",
8874 NoBinPacking);
8875 verifyFormat(
8876 "void fffffffffff(\n"
8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8878 " aaaaaaaaaa);");
8881 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8882 FormatStyle NoBinPacking = getGoogleStyle();
8883 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8884 NoBinPacking.BinPackArguments = false;
8885 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8886 " aaaaaaaaaaaaaaaaaaaa,\n"
8887 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8888 NoBinPacking);
8889 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8890 " aaaaaaaaaaaaa,\n"
8891 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8892 NoBinPacking);
8893 verifyFormat(
8894 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8895 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8897 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8899 NoBinPacking);
8900 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8901 " .aaaaaaaaaaaaaaaaaa();",
8902 NoBinPacking);
8903 verifyFormat("void f() {\n"
8904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8905 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8906 "}",
8907 NoBinPacking);
8909 verifyFormat(
8910 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8911 " aaaaaaaaaaaa,\n"
8912 " aaaaaaaaaaaa);",
8913 NoBinPacking);
8914 verifyFormat(
8915 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8916 " ddddddddddddddddddddddddddddd),\n"
8917 " test);",
8918 NoBinPacking);
8920 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8921 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8922 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8923 " aaaaaaaaaaaaaaaaaa;",
8924 NoBinPacking);
8925 verifyFormat("a(\"a\"\n"
8926 " \"a\",\n"
8927 " a);");
8929 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8930 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8931 " aaaaaaaaa,\n"
8932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8933 NoBinPacking);
8934 verifyFormat(
8935 "void f() {\n"
8936 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8937 " .aaaaaaa();\n"
8938 "}",
8939 NoBinPacking);
8940 verifyFormat(
8941 "template <class SomeType, class SomeOtherType>\n"
8942 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8943 NoBinPacking);
8946 TEST_F(FormatTest, FormatsDeclarationBreakAlways) {
8947 FormatStyle BreakAlways = getGoogleStyle();
8948 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
8949 verifyFormat("void f(int a,\n"
8950 " int b);",
8951 BreakAlways);
8952 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8953 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8954 " int cccccccccccccccccccccccc);",
8955 BreakAlways);
8957 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8958 // to BPPS_AlwaysOnePerLine.
8959 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8960 verifyFormat(
8961 "void someLongFunctionName(\n"
8962 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8963 " int b);",
8964 BreakAlways);
8965 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8966 verifyFormat(
8967 "void someLongFunctionName(\n"
8968 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8969 " int b\n"
8970 ");",
8971 BreakAlways);
8974 TEST_F(FormatTest, FormatsDefinitionBreakAlways) {
8975 FormatStyle BreakAlways = getGoogleStyle();
8976 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
8977 verifyFormat("void f(int a,\n"
8978 " int b) {\n"
8979 " f(a, b);\n"
8980 "}",
8981 BreakAlways);
8983 // Ensure BinPackArguments interact correctly when BinPackParameters is set to
8984 // BPPS_AlwaysOnePerLine.
8985 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8986 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8987 " int cccccccccccccccccccccccc) {\n"
8988 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8989 " cccccccccccccccccccccccc);\n"
8990 "}",
8991 BreakAlways);
8992 BreakAlways.BinPackArguments = false;
8993 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8994 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8995 " int cccccccccccccccccccccccc) {\n"
8996 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8997 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8998 " cccccccccccccccccccccccc);\n"
8999 "}",
9000 BreakAlways);
9002 // Ensure BreakFunctionDefinitionParameters interacts correctly when
9003 // BinPackParameters is set to BPPS_AlwaysOnePerLine.
9004 BreakAlways.BreakFunctionDefinitionParameters = true;
9005 verifyFormat("void f(\n"
9006 " int a,\n"
9007 " int b) {\n"
9008 " f(a, b);\n"
9009 "}",
9010 BreakAlways);
9011 BreakAlways.BreakFunctionDefinitionParameters = false;
9013 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
9014 // to BPPS_AlwaysOnePerLine.
9015 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9016 verifyFormat(
9017 "void someLongFunctionName(\n"
9018 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9019 " int b) {\n"
9020 " someLongFunctionName(\n"
9021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
9022 "}",
9023 BreakAlways);
9024 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9025 verifyFormat(
9026 "void someLongFunctionName(\n"
9027 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9028 " int b\n"
9029 ") {\n"
9030 " someLongFunctionName(\n"
9031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n"
9032 " );\n"
9033 "}",
9034 BreakAlways);
9037 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
9038 FormatStyle Style = getLLVMStyleWithColumns(15);
9039 Style.ExperimentalAutoDetectBinPacking = true;
9040 verifyFormat("aaa(aaaa,\n"
9041 " aaaa,\n"
9042 " aaaa);\n"
9043 "aaa(aaaa,\n"
9044 " aaaa,\n"
9045 " aaaa);",
9046 "aaa(aaaa,\n" // one-per-line
9047 " aaaa,\n"
9048 " aaaa );\n"
9049 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9050 Style);
9051 verifyFormat("aaa(aaaa, aaaa,\n"
9052 " aaaa);\n"
9053 "aaa(aaaa, aaaa,\n"
9054 " aaaa);",
9055 "aaa(aaaa, aaaa,\n" // bin-packed
9056 " aaaa );\n"
9057 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9058 Style);
9061 TEST_F(FormatTest, FormatsBuilderPattern) {
9062 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9063 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9064 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9065 " .StartsWith(\".init\", ORDER_INIT)\n"
9066 " .StartsWith(\".fini\", ORDER_FINI)\n"
9067 " .StartsWith(\".hash\", ORDER_HASH)\n"
9068 " .Default(ORDER_TEXT);");
9070 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9071 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9072 verifyFormat("aaaaaaa->aaaaaaa\n"
9073 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9075 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9076 verifyFormat(
9077 "aaaaaaa->aaaaaaa\n"
9078 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9079 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9080 verifyFormat(
9081 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9082 " aaaaaaaaaaaaaa);");
9083 verifyFormat(
9084 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9085 " aaaaaa->aaaaaaaaaaaa()\n"
9086 " ->aaaaaaaaaaaaaaaa(\n"
9087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9088 " ->aaaaaaaaaaaaaaaaa();");
9089 verifyGoogleFormat(
9090 "void f() {\n"
9091 " someo->Add((new util::filetools::Handler(dir))\n"
9092 " ->OnEvent1(NewPermanentCallback(\n"
9093 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9094 " ->OnEvent2(NewPermanentCallback(\n"
9095 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9096 " ->OnEvent3(NewPermanentCallback(\n"
9097 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9098 " ->OnEvent5(NewPermanentCallback(\n"
9099 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9100 " ->OnEvent6(NewPermanentCallback(\n"
9101 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9102 "}");
9104 verifyFormat(
9105 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9106 verifyFormat("aaaaaaaaaaaaaaa()\n"
9107 " .aaaaaaaaaaaaaaa()\n"
9108 " .aaaaaaaaaaaaaaa()\n"
9109 " .aaaaaaaaaaaaaaa()\n"
9110 " .aaaaaaaaaaaaaaa();");
9111 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9112 " .aaaaaaaaaaaaaaa()\n"
9113 " .aaaaaaaaaaaaaaa()\n"
9114 " .aaaaaaaaaaaaaaa();");
9115 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9116 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9117 " .aaaaaaaaaaaaaaa();");
9118 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9119 " ->aaaaaaaaaaaaaae(0)\n"
9120 " ->aaaaaaaaaaaaaaa();");
9122 // Don't linewrap after very short segments.
9123 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9124 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9125 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9126 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9127 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9128 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9129 verifyFormat("aaa()\n"
9130 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9131 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9132 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9134 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9135 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9136 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9137 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9138 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9141 // Prefer not to break after empty parentheses.
9142 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9143 " First->LastNewlineOffset);");
9145 // Prefer not to create "hanging" indents.
9146 verifyFormat(
9147 "return !soooooooooooooome_map\n"
9148 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9149 " .second;");
9150 verifyFormat(
9151 "return aaaaaaaaaaaaaaaa\n"
9152 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9153 " .aaaa(aaaaaaaaaaaaaa);");
9154 // No hanging indent here.
9155 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9157 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9159 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9160 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9161 getLLVMStyleWithColumns(60));
9162 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9163 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9164 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9165 getLLVMStyleWithColumns(59));
9166 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9168 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9170 // Dont break if only closing statements before member call
9171 verifyFormat("test() {\n"
9172 " ([]() -> {\n"
9173 " int b = 32;\n"
9174 " return 3;\n"
9175 " }).foo();\n"
9176 "}");
9177 verifyFormat("test() {\n"
9178 " (\n"
9179 " []() -> {\n"
9180 " int b = 32;\n"
9181 " return 3;\n"
9182 " },\n"
9183 " foo, bar)\n"
9184 " .foo();\n"
9185 "}");
9186 verifyFormat("test() {\n"
9187 " ([]() -> {\n"
9188 " int b = 32;\n"
9189 " return 3;\n"
9190 " })\n"
9191 " .foo()\n"
9192 " .bar();\n"
9193 "}");
9194 verifyFormat("test() {\n"
9195 " ([]() -> {\n"
9196 " int b = 32;\n"
9197 " return 3;\n"
9198 " })\n"
9199 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9200 " \"bbbb\");\n"
9201 "}",
9202 getLLVMStyleWithColumns(30));
9205 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9206 verifyFormat(
9207 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9208 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9209 verifyFormat(
9210 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9211 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9213 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9214 " ccccccccccccccccccccccccc) {\n}");
9215 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9216 " ccccccccccccccccccccccccc) {\n}");
9218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9219 " ccccccccccccccccccccccccc) {\n}");
9220 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9221 " ccccccccccccccccccccccccc) {\n}");
9223 verifyFormat(
9224 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9225 " ccccccccccccccccccccccccc) {\n}");
9226 verifyFormat(
9227 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9228 " ccccccccccccccccccccccccc) {\n}");
9230 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9231 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9232 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9233 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9234 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9235 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9236 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9237 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9239 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9240 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9241 " aaaaaaaaaaaaaaa != aa) {\n}");
9242 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9243 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9244 " aaaaaaaaaaaaaaa != aa) {\n}");
9247 TEST_F(FormatTest, BreaksAfterAssignments) {
9248 verifyFormat(
9249 "unsigned Cost =\n"
9250 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9251 " SI->getPointerAddressSpaceee());");
9252 verifyFormat(
9253 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9254 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9256 verifyFormat(
9257 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9258 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9259 verifyFormat("unsigned OriginalStartColumn =\n"
9260 " SourceMgr.getSpellingColumnNumber(\n"
9261 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9262 " 1;");
9265 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9266 FormatStyle Style = getLLVMStyle();
9267 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9268 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9269 Style);
9271 Style.PenaltyBreakAssignment = 20;
9272 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9273 " cccccccccccccccccccccccccc;",
9274 Style);
9277 TEST_F(FormatTest, AlignsAfterAssignments) {
9278 verifyFormat(
9279 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9280 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9281 verifyFormat(
9282 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9283 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9284 verifyFormat(
9285 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9286 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9287 verifyFormat(
9288 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9289 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9290 verifyFormat(
9291 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9292 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9293 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9296 TEST_F(FormatTest, AlignsAfterReturn) {
9297 verifyFormat(
9298 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9299 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9300 verifyFormat(
9301 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9302 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9303 verifyFormat(
9304 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9305 " aaaaaaaaaaaaaaaaaaaaaa();");
9306 verifyFormat(
9307 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9308 " aaaaaaaaaaaaaaaaaaaaaa());");
9309 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9311 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9314 verifyFormat("return\n"
9315 " // true if code is one of a or b.\n"
9316 " code == a || code == b;");
9319 TEST_F(FormatTest, AlignsAfterOpenBracket) {
9320 verifyFormat(
9321 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9322 " aaaaaaaaa aaaaaaa) {}");
9323 verifyFormat(
9324 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9325 " aaaaaaaaaaa aaaaaaaaa);");
9326 verifyFormat(
9327 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9328 " aaaaaaaaaaaaaaaaaaaaa));");
9329 FormatStyle Style = getLLVMStyle();
9330 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9331 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9332 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9333 Style);
9334 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9335 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9336 Style);
9337 verifyFormat("SomeLongVariableName->someFunction(\n"
9338 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9339 Style);
9340 verifyFormat(
9341 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9342 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9343 Style);
9344 verifyFormat(
9345 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9346 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9347 Style);
9348 verifyFormat(
9349 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9350 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9351 Style);
9353 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9354 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9355 " b));",
9356 Style);
9358 Style.ColumnLimit = 30;
9359 verifyFormat("for (int foo = 0; foo < FOO;\n"
9360 " ++foo) {\n"
9361 " bar(foo);\n"
9362 "}",
9363 Style);
9364 Style.ColumnLimit = 80;
9366 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9367 Style.BinPackArguments = false;
9368 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9369 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9370 " aaaaaaaaaaa aaaaaaaa,\n"
9371 " aaaaaaaaa aaaaaaa,\n"
9372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9373 Style);
9374 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9375 " aaaaaaaaaaa aaaaaaaaa,\n"
9376 " aaaaaaaaaaa aaaaaaaaa,\n"
9377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9378 Style);
9379 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9380 " aaaaaaaaaaaaaaa,\n"
9381 " aaaaaaaaaaaaaaaaaaaaa,\n"
9382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9383 Style);
9384 verifyFormat(
9385 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9386 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9387 Style);
9388 verifyFormat(
9389 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9390 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9391 Style);
9392 verifyFormat(
9393 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9394 " aaaaaaaaaaaaaaaaaaaaa(\n"
9395 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9396 " aaaaaaaaaaaaaaaa);",
9397 Style);
9398 verifyFormat(
9399 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9400 " aaaaaaaaaaaaaaaaaaaaa(\n"
9401 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9402 " aaaaaaaaaaaaaaaa);",
9403 Style);
9404 verifyFormat(
9405 "fooooooooooo(new BARRRRRRRRR(\n"
9406 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9407 Style);
9408 verifyFormat(
9409 "fooooooooooo(::new BARRRRRRRRR(\n"
9410 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9411 Style);
9412 verifyFormat(
9413 "fooooooooooo(new FOO::BARRRR(\n"
9414 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9415 Style);
9417 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9418 Style.BinPackArguments = false;
9419 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9420 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9421 " aaaaaaaaaaa aaaaaaaa,\n"
9422 " aaaaaaaaa aaaaaaa,\n"
9423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9424 ") {}",
9425 Style);
9426 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9427 " aaaaaaaaaaa aaaaaaaaa,\n"
9428 " aaaaaaaaaaa aaaaaaaaa,\n"
9429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9430 ");",
9431 Style);
9432 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9433 " aaaaaaaaaaaaaaa,\n"
9434 " aaaaaaaaaaaaaaaaaaaaa,\n"
9435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9436 "));",
9437 Style);
9438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9439 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9440 "));",
9441 Style);
9442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9443 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9444 "));",
9445 Style);
9446 verifyFormat(
9447 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9448 " aaaaaaaaaaaaaaaaaaaaa(\n"
9449 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9450 " ),\n"
9451 " aaaaaaaaaaaaaaaa\n"
9452 ");",
9453 Style);
9454 verifyFormat(
9455 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9456 " aaaaaaaaaaaaaaaaaaaaa(\n"
9457 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9458 " ) &&\n"
9459 " aaaaaaaaaaaaaaaa\n"
9460 ");",
9461 Style);
9462 verifyFormat("void foo(\n"
9463 " void (*foobarpntr)(\n"
9464 " aaaaaaaaaaaaaaaaaa *,\n"
9465 " bbbbbbbbbbbbbb *,\n"
9466 " cccccccccccccccccccc *,\n"
9467 " dddddddddddddddddd *\n"
9468 " )\n"
9469 ");",
9470 Style);
9471 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
9472 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9473 "};",
9474 Style);
9476 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9477 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
9478 ") const {\n"
9479 " return true;\n"
9480 "}",
9481 Style);
9482 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9483 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n"
9484 ") const;",
9485 Style);
9486 verifyFormat("void aaaaaaaaa(\n"
9487 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n"
9488 ") const noexcept -> std::vector<of_very_long_type>;",
9489 Style);
9490 verifyFormat(
9491 "x = aaaaaaaaaaaaaaa(\n"
9492 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
9493 ");",
9494 Style);
9495 Style.ColumnLimit = 60;
9496 verifyFormat("auto lambda =\n"
9497 " [&b](\n"
9498 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9499 " ) {};",
9500 Style);
9503 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9504 FormatStyle Style = getLLVMStyleWithColumns(40);
9505 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9506 " bbbbbbbbbbbbbbbbbbbbbb);",
9507 Style);
9508 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9509 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9510 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9511 " bbbbbbbbbbbbbbbbbbbbbb);",
9512 Style);
9513 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9514 Style.AlignOperands = FormatStyle::OAS_Align;
9515 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9516 " bbbbbbbbbbbbbbbbbbbbbb);",
9517 Style);
9518 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9519 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9520 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9521 " bbbbbbbbbbbbbbbbbbbbbb);",
9522 Style);
9525 TEST_F(FormatTest, BreaksConditionalExpressions) {
9526 verifyFormat(
9527 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9528 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9529 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9530 verifyFormat(
9531 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9532 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9533 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9534 verifyFormat(
9535 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9536 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9537 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9538 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9539 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9540 verifyFormat(
9541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9542 " : aaaaaaaaaaaaa);");
9543 verifyFormat(
9544 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9545 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9546 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9547 " aaaaaaaaaaaaa);");
9548 verifyFormat(
9549 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9550 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9551 " aaaaaaaaaaaaa);");
9552 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9553 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9557 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9559 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9561 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9563 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9564 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9566 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9568 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9569 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9570 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9571 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9572 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9574 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9575 " : aaaaaaaaaaaaaaaa;");
9576 verifyFormat(
9577 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9578 " ? aaaaaaaaaaaaaaa\n"
9579 " : aaaaaaaaaaaaaaa;");
9580 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9581 " aaaaaaaaa\n"
9582 " ? b\n"
9583 " : c);");
9584 verifyFormat("return aaaa == bbbb\n"
9585 " // comment\n"
9586 " ? aaaa\n"
9587 " : bbbb;");
9588 verifyFormat("unsigned Indent =\n"
9589 " format(TheLine.First,\n"
9590 " IndentForLevel[TheLine.Level] >= 0\n"
9591 " ? IndentForLevel[TheLine.Level]\n"
9592 " : TheLine * 2,\n"
9593 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9594 getLLVMStyleWithColumns(60));
9595 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9596 " ? aaaaaaaaaaaaaaa\n"
9597 " : bbbbbbbbbbbbbbb //\n"
9598 " ? ccccccccccccccc\n"
9599 " : ddddddddddddddd;");
9600 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9601 " ? aaaaaaaaaaaaaaa\n"
9602 " : (bbbbbbbbbbbbbbb //\n"
9603 " ? ccccccccccccccc\n"
9604 " : ddddddddddddddd);");
9605 verifyFormat(
9606 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9607 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9608 " aaaaaaaaaaaaaaaaaaaaa +\n"
9609 " aaaaaaaaaaaaaaaaaaaaa\n"
9610 " : aaaaaaaaaa;");
9611 verifyFormat(
9612 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9613 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9614 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9616 FormatStyle NoBinPacking = getLLVMStyle();
9617 NoBinPacking.BinPackArguments = false;
9618 verifyFormat(
9619 "void f() {\n"
9620 " g(aaa,\n"
9621 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9623 " ? aaaaaaaaaaaaaaa\n"
9624 " : aaaaaaaaaaaaaaa);\n"
9625 "}",
9626 NoBinPacking);
9627 verifyFormat(
9628 "void f() {\n"
9629 " g(aaa,\n"
9630 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9632 " ?: aaaaaaaaaaaaaaa);\n"
9633 "}",
9634 NoBinPacking);
9636 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9637 " // comment.\n"
9638 " ccccccccccccccccccccccccccccccccccccccc\n"
9639 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9640 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9642 // Assignments in conditional expressions. Apparently not uncommon :-(.
9643 verifyFormat("return a != b\n"
9644 " // comment\n"
9645 " ? a = b\n"
9646 " : a = b;");
9647 verifyFormat("return a != b\n"
9648 " // comment\n"
9649 " ? a = a != b\n"
9650 " // comment\n"
9651 " ? a = b\n"
9652 " : a\n"
9653 " : a;");
9654 verifyFormat("return a != b\n"
9655 " // comment\n"
9656 " ? a\n"
9657 " : a = a != b\n"
9658 " // comment\n"
9659 " ? a = b\n"
9660 " : a;");
9662 // Chained conditionals
9663 FormatStyle Style = getLLVMStyleWithColumns(70);
9664 Style.AlignOperands = FormatStyle::OAS_Align;
9665 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9666 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9667 " : 3333333333333333;",
9668 Style);
9669 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9670 " : bbbbbbbbbb ? 2222222222222222\n"
9671 " : 3333333333333333;",
9672 Style);
9673 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9674 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9675 " : 3333333333333333;",
9676 Style);
9677 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9678 " : bbbbbbbbbbbbbb ? 222222\n"
9679 " : 333333;",
9680 Style);
9681 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9682 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9683 " : cccccccccccccc ? 3333333333333333\n"
9684 " : 4444444444444444;",
9685 Style);
9686 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9687 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9688 " : 3333333333333333;",
9689 Style);
9690 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9691 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9692 " : (aaa ? bbb : ccc);",
9693 Style);
9694 verifyFormat(
9695 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9696 " : cccccccccccccccccc)\n"
9697 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9698 " : 3333333333333333;",
9699 Style);
9700 verifyFormat(
9701 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9702 " : cccccccccccccccccc)\n"
9703 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9704 " : 3333333333333333;",
9705 Style);
9706 verifyFormat(
9707 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9708 " : dddddddddddddddddd)\n"
9709 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9710 " : 3333333333333333;",
9711 Style);
9712 verifyFormat(
9713 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9714 " : dddddddddddddddddd)\n"
9715 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9716 " : 3333333333333333;",
9717 Style);
9718 verifyFormat(
9719 "return aaaaaaaaa ? 1111111111111111\n"
9720 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9721 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9722 " : dddddddddddddddddd)",
9723 Style);
9724 verifyFormat(
9725 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9726 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9727 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9728 " : cccccccccccccccccc);",
9729 Style);
9730 verifyFormat(
9731 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9732 " : ccccccccccccccc ? dddddddddddddddddd\n"
9733 " : eeeeeeeeeeeeeeeeee)\n"
9734 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9735 " : 3333333333333333;",
9736 Style);
9737 verifyFormat(
9738 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9739 " : ccccccccccccccc ? dddddddddddddddddd\n"
9740 " : eeeeeeeeeeeeeeeeee)\n"
9741 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9742 " : 3333333333333333;",
9743 Style);
9744 verifyFormat(
9745 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9746 " : cccccccccccc ? dddddddddddddddddd\n"
9747 " : eeeeeeeeeeeeeeeeee)\n"
9748 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9749 " : 3333333333333333;",
9750 Style);
9751 verifyFormat(
9752 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9753 " : cccccccccccccccccc\n"
9754 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9755 " : 3333333333333333;",
9756 Style);
9757 verifyFormat(
9758 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9759 " : cccccccccccccccc ? dddddddddddddddddd\n"
9760 " : eeeeeeeeeeeeeeeeee\n"
9761 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9762 " : 3333333333333333;",
9763 Style);
9764 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9765 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9766 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9767 " : eeeeeeeeeeeeeeeeee)\n"
9768 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9769 " : 3333333333333333;",
9770 Style);
9771 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9772 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9773 " : cccccccccccccccc ? dddddddddddddddddd\n"
9774 " : eeeeeeeeeeeeeeeeee\n"
9775 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9776 " : 3333333333333333;",
9777 Style);
9779 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9780 Style.BreakBeforeTernaryOperators = false;
9781 // FIXME: Aligning the question marks is weird given DontAlign.
9782 // Consider disabling this alignment in this case. Also check whether this
9783 // will render the adjustment from https://reviews.llvm.org/D82199
9784 // unnecessary.
9785 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9786 " bbbb ? cccccccccccccccccc :\n"
9787 " ddddd;",
9788 Style);
9790 verifyFormat(
9791 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9792 " /*\n"
9793 " */\n"
9794 " function() {\n"
9795 " try {\n"
9796 " return JJJJJJJJJJJJJJ(\n"
9797 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9798 " }\n"
9799 " } :\n"
9800 " function() {};",
9801 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9802 " /*\n"
9803 " */\n"
9804 " function() {\n"
9805 " try {\n"
9806 " return JJJJJJJJJJJJJJ(\n"
9807 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9808 " }\n"
9809 " } :\n"
9810 " function() {};",
9811 getGoogleStyle(FormatStyle::LK_JavaScript));
9814 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9815 FormatStyle Style = getLLVMStyleWithColumns(70);
9816 Style.BreakBeforeTernaryOperators = false;
9817 verifyFormat(
9818 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9820 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9821 Style);
9822 verifyFormat(
9823 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9824 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9826 Style);
9827 verifyFormat(
9828 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9830 Style);
9831 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9832 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9834 Style);
9835 verifyFormat(
9836 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9837 " aaaaaaaaaaaaa);",
9838 Style);
9839 verifyFormat(
9840 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9841 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9843 " aaaaaaaaaaaaa);",
9844 Style);
9845 verifyFormat(
9846 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9847 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9848 " aaaaaaaaaaaaa);",
9849 Style);
9850 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9855 Style);
9856 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9862 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9863 Style);
9864 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9868 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9869 Style);
9870 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9871 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9872 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9873 Style);
9874 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9878 Style);
9879 verifyFormat(
9880 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9881 " aaaaaaaaaaaaaaa :\n"
9882 " aaaaaaaaaaaaaaa;",
9883 Style);
9884 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9885 " aaaaaaaaa ?\n"
9886 " b :\n"
9887 " c);",
9888 Style);
9889 verifyFormat("unsigned Indent =\n"
9890 " format(TheLine.First,\n"
9891 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9892 " IndentForLevel[TheLine.Level] :\n"
9893 " TheLine * 2,\n"
9894 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9895 Style);
9896 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9897 " aaaaaaaaaaaaaaa :\n"
9898 " bbbbbbbbbbbbbbb ? //\n"
9899 " ccccccccccccccc :\n"
9900 " ddddddddddddddd;",
9901 Style);
9902 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9903 " aaaaaaaaaaaaaaa :\n"
9904 " (bbbbbbbbbbbbbbb ? //\n"
9905 " ccccccccccccccc :\n"
9906 " ddddddddddddddd);",
9907 Style);
9908 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9909 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9910 " ccccccccccccccccccccccccccc;",
9911 Style);
9912 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9913 " aaaaa :\n"
9914 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9915 Style);
9917 // Chained conditionals
9918 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9919 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9920 " 3333333333333333;",
9921 Style);
9922 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9923 " bbbbbbbbbb ? 2222222222222222 :\n"
9924 " 3333333333333333;",
9925 Style);
9926 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9927 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9928 " 3333333333333333;",
9929 Style);
9930 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9931 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9932 " 333333;",
9933 Style);
9934 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9935 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9936 " cccccccccccccccc ? 3333333333333333 :\n"
9937 " 4444444444444444;",
9938 Style);
9939 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9940 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9941 " 3333333333333333;",
9942 Style);
9943 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9944 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9945 " (aaa ? bbb : ccc);",
9946 Style);
9947 verifyFormat(
9948 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9949 " cccccccccccccccccc) :\n"
9950 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9951 " 3333333333333333;",
9952 Style);
9953 verifyFormat(
9954 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9955 " cccccccccccccccccc) :\n"
9956 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9957 " 3333333333333333;",
9958 Style);
9959 verifyFormat(
9960 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9961 " dddddddddddddddddd) :\n"
9962 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9963 " 3333333333333333;",
9964 Style);
9965 verifyFormat(
9966 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9967 " dddddddddddddddddd) :\n"
9968 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9969 " 3333333333333333;",
9970 Style);
9971 verifyFormat(
9972 "return aaaaaaaaa ? 1111111111111111 :\n"
9973 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9974 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9975 " dddddddddddddddddd)",
9976 Style);
9977 verifyFormat(
9978 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9979 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9980 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9981 " cccccccccccccccccc);",
9982 Style);
9983 verifyFormat(
9984 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9985 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9986 " eeeeeeeeeeeeeeeeee) :\n"
9987 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9988 " 3333333333333333;",
9989 Style);
9990 verifyFormat(
9991 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9992 " ccccccccccccc ? dddddddddddddddddd :\n"
9993 " eeeeeeeeeeeeeeeeee) :\n"
9994 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9995 " 3333333333333333;",
9996 Style);
9997 verifyFormat(
9998 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9999 " ccccccccccccccccc ? dddddddddddddddddd :\n"
10000 " eeeeeeeeeeeeeeeeee) :\n"
10001 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10002 " 3333333333333333;",
10003 Style);
10004 verifyFormat(
10005 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10006 " cccccccccccccccccc :\n"
10007 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10008 " 3333333333333333;",
10009 Style);
10010 verifyFormat(
10011 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10012 " cccccccccccccccccc ? dddddddddddddddddd :\n"
10013 " eeeeeeeeeeeeeeeeee :\n"
10014 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10015 " 3333333333333333;",
10016 Style);
10017 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
10018 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10019 " cccccccccccccccccc ? dddddddddddddddddd :\n"
10020 " eeeeeeeeeeeeeeeeee) :\n"
10021 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10022 " 3333333333333333;",
10023 Style);
10024 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
10025 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10026 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
10027 " eeeeeeeeeeeeeeeeee :\n"
10028 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10029 " 3333333333333333;",
10030 Style);
10033 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
10034 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
10035 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
10036 verifyFormat("bool a = true, b = false;");
10038 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
10040 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
10041 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
10042 verifyFormat(
10043 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
10044 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
10045 " d = e && f;");
10046 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
10047 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
10048 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10049 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
10050 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
10051 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
10053 FormatStyle Style = getGoogleStyle();
10054 Style.PointerAlignment = FormatStyle::PAS_Left;
10055 Style.DerivePointerAlignment = false;
10056 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10057 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
10058 " *b = bbbbbbbbbbbbbbbbbbb;",
10059 Style);
10060 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10061 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
10062 Style);
10063 verifyFormat("vector<int*> a, b;", Style);
10064 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
10065 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
10066 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
10067 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
10068 Style);
10069 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
10070 Style);
10071 verifyFormat(
10072 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
10073 Style);
10075 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
10076 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
10077 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
10078 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
10079 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
10080 Style);
10083 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
10084 verifyFormat("arr[foo ? bar : baz];");
10085 verifyFormat("f()[foo ? bar : baz];");
10086 verifyFormat("(a + b)[foo ? bar : baz];");
10087 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10090 TEST_F(FormatTest, AlignsStringLiterals) {
10091 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10092 " \"short literal\");");
10093 verifyFormat(
10094 "looooooooooooooooooooooooongFunction(\n"
10095 " \"short literal\"\n"
10096 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10097 verifyFormat("someFunction(\"Always break between multi-line\"\n"
10098 " \" string literals\",\n"
10099 " also, other, parameters);");
10100 verifyFormat("fun + \"1243\" /* comment */\n"
10101 " \"5678\";",
10102 "fun + \"1243\" /* comment */\n"
10103 " \"5678\";",
10104 getLLVMStyleWithColumns(28));
10105 verifyFormat(
10106 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10107 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10108 " \"aaaaaaaaaaaaaaaa\";",
10109 "aaaaaa ="
10110 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10111 "aaaaaaaaaaaaaaaaaaaaa\" "
10112 "\"aaaaaaaaaaaaaaaa\";");
10113 verifyFormat("a = a + \"a\"\n"
10114 " \"a\"\n"
10115 " \"a\";");
10116 verifyFormat("f(\"a\", \"b\"\n"
10117 " \"c\");");
10119 verifyFormat(
10120 "#define LL_FORMAT \"ll\"\n"
10121 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10122 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10124 verifyFormat("#define A(X) \\\n"
10125 " \"aaaaa\" #X \"bbbbbb\" \\\n"
10126 " \"ccccc\"",
10127 getLLVMStyleWithColumns(23));
10128 verifyFormat("#define A \"def\"\n"
10129 "f(\"abc\" A \"ghi\"\n"
10130 " \"jkl\");");
10132 verifyFormat("f(L\"a\"\n"
10133 " L\"b\");");
10134 verifyFormat("#define A(X) \\\n"
10135 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10136 " L\"ccccc\"",
10137 getLLVMStyleWithColumns(25));
10139 verifyFormat("f(@\"a\"\n"
10140 " @\"b\");");
10141 verifyFormat("NSString s = @\"a\"\n"
10142 " @\"b\"\n"
10143 " @\"c\";");
10144 verifyFormat("NSString s = @\"a\"\n"
10145 " \"b\"\n"
10146 " \"c\";");
10149 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
10150 FormatStyle Style = getLLVMStyle();
10151 Style.ColumnLimit = 60;
10153 // No declarations or definitions should be moved to own line.
10154 Style.BreakAfterReturnType = FormatStyle::RTBS_None;
10155 verifyFormat("class A {\n"
10156 " int f() { return 1; }\n"
10157 " int g();\n"
10158 " long\n"
10159 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10160 "};\n"
10161 "int f() { return 1; }\n"
10162 "int g();\n"
10163 "int foooooooooooooooooooooooooooo::\n"
10164 " baaaaaaaaaaaaaaaaaaaaar();",
10165 Style);
10167 // It is now allowed to break after a short return type if necessary.
10168 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
10169 verifyFormat("class A {\n"
10170 " int f() { return 1; }\n"
10171 " int g();\n"
10172 " long\n"
10173 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10174 "};\n"
10175 "int f() { return 1; }\n"
10176 "int g();\n"
10177 "int\n"
10178 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10179 Style);
10181 // It now must never break after a short return type.
10182 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
10183 verifyFormat("class A {\n"
10184 " int f() { return 1; }\n"
10185 " int g();\n"
10186 " long foooooooooooooooooooooooooooo::\n"
10187 " baaaaaaaaaaaaaaaaaaaar();\n"
10188 "};\n"
10189 "int f() { return 1; }\n"
10190 "int g();\n"
10191 "int foooooooooooooooooooooooooooo::\n"
10192 " baaaaaaaaaaaaaaaaaaaaar();",
10193 Style);
10195 // All declarations and definitions should have the return type moved to its
10196 // own line.
10197 Style.BreakAfterReturnType = FormatStyle::RTBS_All;
10198 Style.TypenameMacros = {"LIST"};
10199 verifyFormat("SomeType\n"
10200 "funcdecl(LIST(uint64_t));",
10201 Style);
10202 verifyFormat("class E {\n"
10203 " int\n"
10204 " f() {\n"
10205 " return 1;\n"
10206 " }\n"
10207 " int\n"
10208 " g();\n"
10209 " long\n"
10210 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10211 "};\n"
10212 "int\n"
10213 "f() {\n"
10214 " return 1;\n"
10215 "}\n"
10216 "int\n"
10217 "g();\n"
10218 "int\n"
10219 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10220 Style);
10222 // Top-level definitions, and no kinds of declarations should have the
10223 // return type moved to its own line.
10224 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10225 verifyFormat("class B {\n"
10226 " int f() { return 1; }\n"
10227 " int g();\n"
10228 "};\n"
10229 "int\n"
10230 "f() {\n"
10231 " return 1;\n"
10232 "}\n"
10233 "int g();",
10234 Style);
10236 // Top-level definitions and declarations should have the return type moved
10237 // to its own line.
10238 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10239 verifyFormat("class C {\n"
10240 " int f() { return 1; }\n"
10241 " int g();\n"
10242 "};\n"
10243 "int\n"
10244 "f() {\n"
10245 " return 1;\n"
10246 "}\n"
10247 "int\n"
10248 "g();\n"
10249 "int\n"
10250 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10251 Style);
10253 // All definitions should have the return type moved to its own line, but no
10254 // kinds of declarations.
10255 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10256 verifyFormat("class D {\n"
10257 " int\n"
10258 " f() {\n"
10259 " return 1;\n"
10260 " }\n"
10261 " int g();\n"
10262 "};\n"
10263 "int\n"
10264 "f() {\n"
10265 " return 1;\n"
10266 "}\n"
10267 "int g();",
10268 Style);
10269 verifyFormat("const char *\n"
10270 "f(void) {\n" // Break here.
10271 " return \"\";\n"
10272 "}\n"
10273 "const char *bar(void);", // No break here.
10274 Style);
10275 verifyFormat("template <class T>\n"
10276 "T *\n"
10277 "f(T &c) {\n" // Break here.
10278 " return NULL;\n"
10279 "}\n"
10280 "template <class T> T *f(T &c);", // No break here.
10281 Style);
10282 verifyFormat("class C {\n"
10283 " int\n"
10284 " operator+() {\n"
10285 " return 1;\n"
10286 " }\n"
10287 " int\n"
10288 " operator()() {\n"
10289 " return 1;\n"
10290 " }\n"
10291 "};",
10292 Style);
10293 verifyFormat("void\n"
10294 "A::operator()() {}\n"
10295 "void\n"
10296 "A::operator>>() {}\n"
10297 "void\n"
10298 "A::operator+() {}\n"
10299 "void\n"
10300 "A::operator*() {}\n"
10301 "void\n"
10302 "A::operator->() {}\n"
10303 "void\n"
10304 "A::operator void *() {}\n"
10305 "void\n"
10306 "A::operator void &() {}\n"
10307 "void\n"
10308 "A::operator void &&() {}\n"
10309 "void\n"
10310 "A::operator char *() {}\n"
10311 "void\n"
10312 "A::operator[]() {}\n"
10313 "void\n"
10314 "A::operator!() {}\n"
10315 "void\n"
10316 "A::operator**() {}\n"
10317 "void\n"
10318 "A::operator<Foo> *() {}\n"
10319 "void\n"
10320 "A::operator<Foo> **() {}\n"
10321 "void\n"
10322 "A::operator<Foo> &() {}\n"
10323 "void\n"
10324 "A::operator void **() {}",
10325 Style);
10326 verifyFormat("constexpr auto\n"
10327 "operator()() const -> reference {}\n"
10328 "constexpr auto\n"
10329 "operator>>() const -> reference {}\n"
10330 "constexpr auto\n"
10331 "operator+() const -> reference {}\n"
10332 "constexpr auto\n"
10333 "operator*() const -> reference {}\n"
10334 "constexpr auto\n"
10335 "operator->() const -> reference {}\n"
10336 "constexpr auto\n"
10337 "operator++() const -> reference {}\n"
10338 "constexpr auto\n"
10339 "operator void *() const -> reference {}\n"
10340 "constexpr auto\n"
10341 "operator void **() const -> reference {}\n"
10342 "constexpr auto\n"
10343 "operator void *() const -> reference {}\n"
10344 "constexpr auto\n"
10345 "operator void &() const -> reference {}\n"
10346 "constexpr auto\n"
10347 "operator void &&() const -> reference {}\n"
10348 "constexpr auto\n"
10349 "operator char *() const -> reference {}\n"
10350 "constexpr auto\n"
10351 "operator!() const -> reference {}\n"
10352 "constexpr auto\n"
10353 "operator[]() const -> reference {}",
10354 Style);
10355 verifyFormat("void *operator new(std::size_t s);", // No break here.
10356 Style);
10357 verifyFormat("void *\n"
10358 "operator new(std::size_t s) {}",
10359 Style);
10360 verifyFormat("void *\n"
10361 "operator delete[](void *ptr) {}",
10362 Style);
10363 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10364 verifyFormat("const char *\n"
10365 "f(void)\n" // Break here.
10366 "{\n"
10367 " return \"\";\n"
10368 "}\n"
10369 "const char *bar(void);", // No break here.
10370 Style);
10371 verifyFormat("template <class T>\n"
10372 "T *\n" // Problem here: no line break
10373 "f(T &c)\n" // Break here.
10374 "{\n"
10375 " return NULL;\n"
10376 "}\n"
10377 "template <class T> T *f(T &c);", // No break here.
10378 Style);
10379 verifyFormat("int\n"
10380 "foo(A<bool> a)\n"
10381 "{\n"
10382 " return a;\n"
10383 "}",
10384 Style);
10385 verifyFormat("int\n"
10386 "foo(A<8> a)\n"
10387 "{\n"
10388 " return a;\n"
10389 "}",
10390 Style);
10391 verifyFormat("int\n"
10392 "foo(A<B<bool>, 8> a)\n"
10393 "{\n"
10394 " return a;\n"
10395 "}",
10396 Style);
10397 verifyFormat("int\n"
10398 "foo(A<B<8>, bool> a)\n"
10399 "{\n"
10400 " return a;\n"
10401 "}",
10402 Style);
10403 verifyFormat("int\n"
10404 "foo(A<B<bool>, bool> a)\n"
10405 "{\n"
10406 " return a;\n"
10407 "}",
10408 Style);
10409 verifyFormat("int\n"
10410 "foo(A<B<8>, 8> a)\n"
10411 "{\n"
10412 " return a;\n"
10413 "}",
10414 Style);
10416 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10417 Style.BraceWrapping.AfterFunction = true;
10418 verifyFormat("int f(i);\n" // No break here.
10419 "int\n" // Break here.
10420 "f(i)\n"
10421 "{\n"
10422 " return i + 1;\n"
10423 "}\n"
10424 "int\n" // Break here.
10425 "f(i)\n"
10426 "{\n"
10427 " return i + 1;\n"
10428 "};",
10429 Style);
10430 verifyFormat("int f(a, b, c);\n" // No break here.
10431 "int\n" // Break here.
10432 "f(a, b, c)\n" // Break here.
10433 "short a, b;\n"
10434 "float c;\n"
10435 "{\n"
10436 " return a + b < c;\n"
10437 "}\n"
10438 "int\n" // Break here.
10439 "f(a, b, c)\n" // Break here.
10440 "short a, b;\n"
10441 "float c;\n"
10442 "{\n"
10443 " return a + b < c;\n"
10444 "};",
10445 Style);
10446 verifyFormat("byte *\n" // Break here.
10447 "f(a)\n" // Break here.
10448 "byte a[];\n"
10449 "{\n"
10450 " return a;\n"
10451 "}",
10452 Style);
10453 verifyFormat("byte *\n"
10454 "f(a)\n"
10455 "byte /* K&R C */ a[];\n"
10456 "{\n"
10457 " return a;\n"
10458 "}\n"
10459 "byte *\n"
10460 "g(p)\n"
10461 "byte /* K&R C */ *p;\n"
10462 "{\n"
10463 " return p;\n"
10464 "}",
10465 Style);
10466 verifyFormat("bool f(int a, int) override;\n"
10467 "Bar g(int a, Bar) final;\n"
10468 "Bar h(a, Bar) final;",
10469 Style);
10470 verifyFormat("int\n"
10471 "f(a)",
10472 Style);
10473 verifyFormat("bool\n"
10474 "f(size_t = 0, bool b = false)\n"
10475 "{\n"
10476 " return !b;\n"
10477 "}",
10478 Style);
10480 // The return breaking style doesn't affect:
10481 // * function and object definitions with attribute-like macros
10482 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10483 " ABSL_GUARDED_BY(mutex) = {};",
10484 getGoogleStyleWithColumns(40));
10485 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10486 " ABSL_GUARDED_BY(mutex); // comment",
10487 getGoogleStyleWithColumns(40));
10488 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10489 " ABSL_GUARDED_BY(mutex1)\n"
10490 " ABSL_GUARDED_BY(mutex2);",
10491 getGoogleStyleWithColumns(40));
10492 verifyFormat("Tttttt f(int a, int b)\n"
10493 " ABSL_GUARDED_BY(mutex1)\n"
10494 " ABSL_GUARDED_BY(mutex2);",
10495 getGoogleStyleWithColumns(40));
10496 // * typedefs
10497 verifyGoogleFormat("typedef ATTR(X) char x;");
10499 Style = getGNUStyle();
10501 // Test for comments at the end of function declarations.
10502 verifyFormat("void\n"
10503 "foo (int a, /*abc*/ int b) // def\n"
10504 "{\n"
10505 "}",
10506 Style);
10508 verifyFormat("void\n"
10509 "foo (int a, /* abc */ int b) /* def */\n"
10510 "{\n"
10511 "}",
10512 Style);
10514 // Definitions that should not break after return type
10515 verifyFormat("void foo (int a, int b); // def", Style);
10516 verifyFormat("void foo (int a, int b); /* def */", Style);
10517 verifyFormat("void foo (int a, int b);", Style);
10520 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10521 FormatStyle NoBreak = getLLVMStyle();
10522 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10523 FormatStyle Break = getLLVMStyle();
10524 Break.AlwaysBreakBeforeMultilineStrings = true;
10525 verifyFormat("aaaa = \"bbbb\"\n"
10526 " \"cccc\";",
10527 NoBreak);
10528 verifyFormat("aaaa =\n"
10529 " \"bbbb\"\n"
10530 " \"cccc\";",
10531 Break);
10532 verifyFormat("aaaa(\"bbbb\"\n"
10533 " \"cccc\");",
10534 NoBreak);
10535 verifyFormat("aaaa(\n"
10536 " \"bbbb\"\n"
10537 " \"cccc\");",
10538 Break);
10539 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10540 " \"cccc\");",
10541 NoBreak);
10542 verifyFormat("aaaa(qqq,\n"
10543 " \"bbbb\"\n"
10544 " \"cccc\");",
10545 Break);
10546 verifyFormat("aaaa(qqq,\n"
10547 " L\"bbbb\"\n"
10548 " L\"cccc\");",
10549 Break);
10550 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10551 " \"bbbb\"));",
10552 Break);
10553 verifyFormat("string s = someFunction(\n"
10554 " \"abc\"\n"
10555 " \"abc\");",
10556 Break);
10558 // As we break before unary operators, breaking right after them is bad.
10559 verifyFormat("string foo = abc ? \"x\"\n"
10560 " \"blah blah blah blah blah blah\"\n"
10561 " : \"y\";",
10562 Break);
10564 // Don't break if there is no column gain.
10565 verifyFormat("f(\"aaaa\"\n"
10566 " \"bbbb\");",
10567 Break);
10569 // Treat literals with escaped newlines like multi-line string literals.
10570 verifyNoChange("x = \"a\\\n"
10571 "b\\\n"
10572 "c\";",
10573 NoBreak);
10574 verifyFormat("xxxx =\n"
10575 " \"a\\\n"
10576 "b\\\n"
10577 "c\";",
10578 "xxxx = \"a\\\n"
10579 "b\\\n"
10580 "c\";",
10581 Break);
10583 verifyFormat("NSString *const kString =\n"
10584 " @\"aaaa\"\n"
10585 " @\"bbbb\";",
10586 "NSString *const kString = @\"aaaa\"\n"
10587 "@\"bbbb\";",
10588 Break);
10590 Break.ColumnLimit = 0;
10591 verifyFormat("const char *hello = \"hello llvm\";", Break);
10594 TEST_F(FormatTest, AlignsPipes) {
10595 verifyFormat(
10596 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10597 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10598 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10599 verifyFormat(
10600 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10601 " << aaaaaaaaaaaaaaaaaaaa;");
10602 verifyFormat(
10603 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10604 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10605 verifyFormat(
10606 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10607 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10608 verifyFormat(
10609 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10610 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10611 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10612 verifyFormat(
10613 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10614 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10615 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10616 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10619 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10620 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10621 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10622 verifyFormat(
10623 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10625 verifyFormat(
10626 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10627 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10629 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10630 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10631 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10633 " aaaaaaaaaaaaaaaaaaaaa)\n"
10634 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10635 verifyFormat("LOG_IF(aaa == //\n"
10636 " bbb)\n"
10637 " << a << b;");
10639 // But sometimes, breaking before the first "<<" is desirable.
10640 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10641 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10642 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10643 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10644 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10645 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10646 " << BEF << IsTemplate << Description << E->getType();");
10647 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10648 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10650 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10651 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10653 " << aaa;");
10655 verifyFormat(
10656 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10657 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10659 // Incomplete string literal.
10660 verifyFormat("llvm::errs() << \"\n"
10661 " << a;",
10662 "llvm::errs() << \"\n<<a;");
10664 verifyFormat("void f() {\n"
10665 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10666 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10667 "}");
10669 // Handle 'endl'.
10670 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10671 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10672 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10674 // Handle '\n'.
10675 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10676 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10677 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10678 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10679 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10680 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10681 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10684 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10685 verifyFormat("return out << \"somepacket = {\\n\"\n"
10686 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10687 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10688 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10689 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10690 " << \"}\";");
10692 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10693 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10694 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10695 verifyFormat(
10696 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10697 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10698 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10699 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10700 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10701 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10702 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10703 verifyFormat(
10704 "void f() {\n"
10705 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10706 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10707 "}");
10709 // Breaking before the first "<<" is generally not desirable.
10710 verifyFormat(
10711 "llvm::errs()\n"
10712 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10713 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10714 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10715 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10716 getLLVMStyleWithColumns(70));
10717 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10718 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10719 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10720 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10721 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10722 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10723 getLLVMStyleWithColumns(70));
10725 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10726 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10727 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10728 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10729 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10730 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10731 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10732 " (aaaa + aaaa);",
10733 getLLVMStyleWithColumns(40));
10734 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10735 " (aaaaaaa + aaaaa));",
10736 getLLVMStyleWithColumns(40));
10737 verifyFormat(
10738 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10739 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10740 " bbbbbbbbbbbbbbbbbbbbbbb);");
10743 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10744 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10746 verifyNoChange("QStringList() << \"foo\"\n"
10747 " << \"bar\";");
10749 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10750 "log_error(log, \"foo\"\n"
10751 " << \"bar\");");
10754 TEST_F(FormatTest, UnderstandsEquals) {
10755 verifyFormat(
10756 "aaaaaaaaaaaaaaaaa =\n"
10757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10758 verifyFormat(
10759 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10761 verifyFormat(
10762 "if (a) {\n"
10763 " f();\n"
10764 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10766 "}");
10768 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10769 " 100000000 + 10000000) {\n}");
10772 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10773 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10774 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10776 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10777 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10779 verifyFormat(
10780 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10781 " Parameter2);");
10783 verifyFormat(
10784 "ShortObject->shortFunction(\n"
10785 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10786 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10788 verifyFormat("loooooooooooooongFunction(\n"
10789 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10791 verifyFormat(
10792 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10793 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10795 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10796 " .WillRepeatedly(Return(SomeValue));");
10797 verifyFormat("void f() {\n"
10798 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10799 " .Times(2)\n"
10800 " .WillRepeatedly(Return(SomeValue));\n"
10801 "}");
10802 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10803 " ccccccccccccccccccccccc);");
10804 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10806 " .aaaaa(aaaaa),\n"
10807 " aaaaaaaaaaaaaaaaaaaaa);");
10808 verifyFormat("void f() {\n"
10809 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10810 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10811 "}");
10812 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10814 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10815 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10816 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10817 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10818 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10819 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10820 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10821 "}");
10823 // Here, it is not necessary to wrap at "." or "->".
10824 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10825 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10826 verifyFormat(
10827 "aaaaaaaaaaa->aaaaaaaaa(\n"
10828 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10829 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10831 verifyFormat(
10832 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10834 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10835 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10836 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10837 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10839 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10841 " .a();");
10843 FormatStyle NoBinPacking = getLLVMStyle();
10844 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
10845 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10846 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10847 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10848 " aaaaaaaaaaaaaaaaaaa,\n"
10849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10850 NoBinPacking);
10852 // If there is a subsequent call, change to hanging indentation.
10853 verifyFormat(
10854 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10855 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10856 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10857 verifyFormat(
10858 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10859 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10860 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10862 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10863 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10865 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10868 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10869 verifyFormat("template <typename T>\n"
10870 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10871 verifyFormat("template <typename T>\n"
10872 "// T should be one of {A, B}.\n"
10873 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10874 verifyFormat(
10875 "template <typename T>\n"
10876 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10877 verifyFormat("template <typename T>\n"
10878 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10879 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10880 verifyFormat(
10881 "template <typename T>\n"
10882 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10883 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10884 verifyFormat(
10885 "template <typename T>\n"
10886 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10887 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10889 verifyFormat("template <typename T>\n"
10890 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10891 " int aaaaaaaaaaaaaaaaaaaaaa);");
10892 verifyFormat(
10893 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10894 " typename T4 = char>\n"
10895 "void f();");
10896 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10897 " template <typename> class cccccccccccccccccccccc,\n"
10898 " typename ddddddddddddd>\n"
10899 "class C {};");
10900 verifyFormat(
10901 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10904 verifyFormat("void f() {\n"
10905 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10906 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10907 "}");
10909 verifyFormat("template <typename T> class C {};");
10910 verifyFormat("template <typename T> void f();");
10911 verifyFormat("template <typename T> void f() {}");
10912 verifyFormat(
10913 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10916 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10919 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10920 getLLVMStyleWithColumns(72));
10921 verifyFormat("static_cast<A< //\n"
10922 " B> *>(\n"
10923 "\n"
10924 ");",
10925 "static_cast<A<//\n"
10926 " B>*>(\n"
10927 "\n"
10928 " );");
10929 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10930 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10932 FormatStyle AlwaysBreak = getLLVMStyle();
10933 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10934 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10935 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10936 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10937 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10938 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10939 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10940 verifyFormat("template <template <typename> class Fooooooo,\n"
10941 " template <typename> class Baaaaaaar>\n"
10942 "struct C {};",
10943 AlwaysBreak);
10944 verifyFormat("template <typename T> // T can be A, B or C.\n"
10945 "struct C {};",
10946 AlwaysBreak);
10947 verifyFormat("template <typename T>\n"
10948 "C(T) noexcept;",
10949 AlwaysBreak);
10950 verifyFormat("template <typename T>\n"
10951 "ClassName(T) noexcept;",
10952 AlwaysBreak);
10953 verifyFormat("template <typename T>\n"
10954 "POOR_NAME(T) noexcept;",
10955 AlwaysBreak);
10956 verifyFormat("template <enum E> class A {\n"
10957 "public:\n"
10958 " E *f();\n"
10959 "};");
10961 FormatStyle NeverBreak = getLLVMStyle();
10962 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
10963 verifyFormat("template <typename T> class C {};", NeverBreak);
10964 verifyFormat("template <typename T> void f();", NeverBreak);
10965 verifyFormat("template <typename T> void f() {}", NeverBreak);
10966 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10967 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10968 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10969 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10970 "bbbbbbbbbbbbbbbbbbbb) {}",
10971 NeverBreak);
10972 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10973 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10974 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10975 NeverBreak);
10976 verifyFormat("template <template <typename> class Fooooooo,\n"
10977 " template <typename> class Baaaaaaar>\n"
10978 "struct C {};",
10979 NeverBreak);
10980 verifyFormat("template <typename T> // T can be A, B or C.\n"
10981 "struct C {};",
10982 NeverBreak);
10983 verifyFormat("template <enum E> class A {\n"
10984 "public:\n"
10985 " E *f();\n"
10986 "};",
10987 NeverBreak);
10988 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10989 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10990 "bbbbbbbbbbbbbbbbbbbb) {}",
10991 NeverBreak);
10993 auto Style = getLLVMStyle();
10994 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
10996 verifyNoChange("template <typename T>\n"
10997 "class C {};",
10998 Style);
10999 verifyFormat("template <typename T> class C {};", Style);
11001 verifyNoChange("template <typename T>\n"
11002 "void f();",
11003 Style);
11004 verifyFormat("template <typename T> void f();", Style);
11006 verifyNoChange("template <typename T>\n"
11007 "void f() {}",
11008 Style);
11009 verifyFormat("template <typename T> void f() {}", Style);
11011 verifyNoChange("template <typename T>\n"
11012 "// T can be A, B or C.\n"
11013 "struct C {};",
11014 Style);
11015 verifyFormat("template <typename T> // T can be A, B or C.\n"
11016 "struct C {};",
11017 Style);
11019 verifyNoChange("template <typename T>\n"
11020 "C(T) noexcept;",
11021 Style);
11022 verifyFormat("template <typename T> C(T) noexcept;", Style);
11024 verifyNoChange("template <enum E>\n"
11025 "class A {\n"
11026 "public:\n"
11027 " E *f();\n"
11028 "};",
11029 Style);
11030 verifyFormat("template <enum E> class A {\n"
11031 "public:\n"
11032 " E *f();\n"
11033 "};",
11034 Style);
11036 verifyNoChange("template <auto x>\n"
11037 "constexpr int simple(int) {\n"
11038 " char c;\n"
11039 " return 1;\n"
11040 "}",
11041 Style);
11042 verifyFormat("template <auto x> constexpr int simple(int) {\n"
11043 " char c;\n"
11044 " return 1;\n"
11045 "}",
11046 Style);
11048 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
11049 verifyNoChange("template <auto x>\n"
11050 "requires(x > 1)\n"
11051 "constexpr int with_req(int) {\n"
11052 " return 1;\n"
11053 "}",
11054 Style);
11055 verifyFormat("template <auto x> requires(x > 1)\n"
11056 "constexpr int with_req(int) {\n"
11057 " return 1;\n"
11058 "}",
11059 Style);
11062 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
11063 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11064 Style.ColumnLimit = 60;
11065 verifyFormat("// Baseline - no comments.\n"
11066 "template <\n"
11067 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
11068 "void f() {}",
11069 Style);
11071 verifyFormat("template <\n"
11072 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11073 "void f() {}",
11074 "template <\n"
11075 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11076 "void f() {}",
11077 Style);
11079 verifyFormat(
11080 "template <\n"
11081 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11082 "void f() {}",
11083 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11084 "void f() {}",
11085 Style);
11087 verifyFormat("template <\n"
11088 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11089 " // multiline\n"
11090 "void f() {}",
11091 "template <\n"
11092 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11093 " // multiline\n"
11094 "void f() {}",
11095 Style);
11097 verifyFormat(
11098 "template <typename aaaaaaaaaa<\n"
11099 " bbbbbbbbbbbb>::value> // trailing loooong\n"
11100 "void f() {}",
11101 "template <\n"
11102 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11103 "void f() {}",
11104 Style);
11107 TEST_F(FormatTest, WrapsTemplateParameters) {
11108 FormatStyle Style = getLLVMStyle();
11109 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11110 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11111 verifyFormat(
11112 "template <typename... a> struct q {};\n"
11113 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11114 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11115 " y;",
11116 Style);
11117 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11118 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11119 verifyFormat(
11120 "template <typename... a> struct r {};\n"
11121 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11122 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11123 " y;",
11124 Style);
11125 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11126 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11127 verifyFormat("template <typename... a> struct s {};\n"
11128 "extern s<\n"
11129 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11130 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11131 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11132 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11133 " y;",
11134 Style);
11135 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11136 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11137 verifyFormat("template <typename... a> struct t {};\n"
11138 "extern t<\n"
11139 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11140 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11141 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11142 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11143 " y;",
11144 Style);
11147 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
11148 verifyFormat(
11149 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11151 verifyFormat(
11152 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11156 // FIXME: Should we have the extra indent after the second break?
11157 verifyFormat(
11158 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11162 verifyFormat(
11163 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11164 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11166 // Breaking at nested name specifiers is generally not desirable.
11167 verifyFormat(
11168 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11169 " aaaaaaaaaaaaaaaaaaaaaaa);");
11171 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11174 " aaaaaaaaaaaaaaaaaaaaa);",
11175 getLLVMStyleWithColumns(74));
11177 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11179 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11181 verifyFormat(
11182 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11183 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11184 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11185 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11188 TEST_F(FormatTest, UnderstandsTemplateParameters) {
11189 verifyFormat("A<int> a;");
11190 verifyFormat("A<A<A<int>>> a;");
11191 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11192 verifyFormat("bool x = a < 1 || 2 > a;");
11193 verifyFormat("bool x = 5 < f<int>();");
11194 verifyFormat("bool x = f<int>() > 5;");
11195 verifyFormat("bool x = 5 < a<int>::x;");
11196 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11197 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11199 verifyGoogleFormat("A<A<int>> a;");
11200 verifyGoogleFormat("A<A<A<int>>> a;");
11201 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11202 verifyGoogleFormat("A<A<int> > a;");
11203 verifyGoogleFormat("A<A<A<int> > > a;");
11204 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11205 verifyGoogleFormat("A<::A<int>> a;");
11206 verifyGoogleFormat("A<::A> a;");
11207 verifyGoogleFormat("A< ::A> a;");
11208 verifyGoogleFormat("A< ::A<int> > a;");
11209 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11210 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11211 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11212 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11213 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11214 getGoogleStyle());
11216 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11218 // template closer followed by a token that starts with > or =
11219 verifyFormat("bool b = a<1> > 1;");
11220 verifyFormat("bool b = a<1> >= 1;");
11221 verifyFormat("int i = a<1> >> 1;");
11222 FormatStyle Style = getLLVMStyle();
11223 Style.SpaceBeforeAssignmentOperators = false;
11224 verifyFormat("bool b= a<1> == 1;", Style);
11225 verifyFormat("a<int> = 1;", Style);
11226 verifyFormat("a<int> >>= 1;", Style);
11228 verifyFormat("test < a | b >> c;");
11229 verifyFormat("test<test<a | b>> c;");
11230 verifyFormat("test >> a >> b;");
11231 verifyFormat("test << a >> b;");
11233 verifyFormat("f<int>();");
11234 verifyFormat("template <typename T> void f() {}");
11235 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11236 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11237 "sizeof(char)>::type>;");
11238 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11239 verifyFormat("f(a.operator()<A>());");
11240 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11241 " .template operator()<A>());",
11242 getLLVMStyleWithColumns(35));
11243 verifyFormat("bool_constant<a && noexcept(f())>;");
11244 verifyFormat("bool_constant<a || noexcept(f())>;");
11246 verifyFormat("if (std::tuple_size_v<T> > 0)");
11248 // Not template parameters.
11249 verifyFormat("return a < b && c > d;");
11250 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11251 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11252 verifyFormat("void f() {\n"
11253 " while (a < b && c > d) {\n"
11254 " }\n"
11255 "}");
11256 verifyFormat("template <typename... Types>\n"
11257 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11259 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11261 getLLVMStyleWithColumns(60));
11262 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11263 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11264 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11265 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11267 verifyFormat("#define FOO(typeName, realClass) \\\n"
11268 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11269 getLLVMStyleWithColumns(60));
11272 TEST_F(FormatTest, UnderstandsShiftOperators) {
11273 verifyFormat("if (i < x >> 1)");
11274 verifyFormat("while (i < x >> 1)");
11275 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11276 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11277 verifyFormat(
11278 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11279 verifyFormat("Foo.call<Bar<Function>>()");
11280 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11281 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11282 "++i, v = v >> 1)");
11283 verifyFormat("if (w<u<v<x>>, 1>::t)");
11286 TEST_F(FormatTest, BitshiftOperatorWidth) {
11287 verifyFormat("int a = 1 << 2; /* foo\n"
11288 " bar */",
11289 "int a=1<<2; /* foo\n"
11290 " bar */");
11292 verifyFormat("int b = 256 >> 1; /* foo\n"
11293 " bar */",
11294 "int b =256>>1 ; /* foo\n"
11295 " bar */");
11298 TEST_F(FormatTest, UnderstandsBinaryOperators) {
11299 verifyFormat("COMPARE(a, ==, b);");
11300 verifyFormat("auto s = sizeof...(Ts) - 1;");
11303 TEST_F(FormatTest, UnderstandsPointersToMembers) {
11304 verifyFormat("int A::*x;");
11305 verifyFormat("int (S::*func)(void *);");
11306 verifyFormat("void f() { int (S::*func)(void *); }");
11307 verifyFormat("typedef bool *(Class::*Member)() const;");
11308 verifyFormat("void f() {\n"
11309 " (a->*f)();\n"
11310 " a->*x;\n"
11311 " (a.*f)();\n"
11312 " ((*a).*f)();\n"
11313 " a.*x;\n"
11314 "}");
11315 verifyFormat("void f() {\n"
11316 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11317 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11318 "}");
11319 verifyFormat(
11320 "(aaaaaaaaaa->*bbbbbbb)(\n"
11321 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11323 FormatStyle Style = getLLVMStyle();
11324 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11325 verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11326 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
11328 Style.PointerAlignment = FormatStyle::PAS_Left;
11329 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
11330 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11332 Style.PointerAlignment = FormatStyle::PAS_Middle;
11333 verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11334 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11337 TEST_F(FormatTest, UnderstandsUnaryOperators) {
11338 verifyFormat("int a = -2;");
11339 verifyFormat("f(-1, -2, -3);");
11340 verifyFormat("a[-1] = 5;");
11341 verifyFormat("int a = 5 + -2;");
11342 verifyFormat("if (i == -1) {\n}");
11343 verifyFormat("if (i != -1) {\n}");
11344 verifyFormat("if (i > -1) {\n}");
11345 verifyFormat("if (i < -1) {\n}");
11346 verifyFormat("++(a->f());");
11347 verifyFormat("--(a->f());");
11348 verifyFormat("(a->f())++;");
11349 verifyFormat("a[42]++;");
11350 verifyFormat("if (!(a->f())) {\n}");
11351 verifyFormat("if (!+i) {\n}");
11352 verifyFormat("~&a;");
11353 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11354 verifyFormat("sizeof -x");
11355 verifyFormat("sizeof +x");
11356 verifyFormat("sizeof *x");
11357 verifyFormat("sizeof &x");
11358 verifyFormat("delete +x;");
11359 verifyFormat("co_await +x;");
11360 verifyFormat("case *x:");
11361 verifyFormat("case &x:");
11363 verifyFormat("a-- > b;");
11364 verifyFormat("b ? -a : c;");
11365 verifyFormat("n * sizeof char16;");
11366 verifyGoogleFormat("n * alignof char16;");
11367 verifyFormat("sizeof(char);");
11368 verifyGoogleFormat("alignof(char);");
11370 verifyFormat("return -1;");
11371 verifyFormat("throw -1;");
11372 verifyFormat("switch (a) {\n"
11373 "case -1:\n"
11374 " break;\n"
11375 "}");
11376 verifyFormat("#define X -1");
11377 verifyFormat("#define X -kConstant");
11379 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11380 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11382 verifyFormat("int a = /* confusing comment */ -1;");
11383 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11384 verifyFormat("int a = i /* confusing comment */++;");
11386 verifyFormat("co_yield -1;");
11387 verifyFormat("co_return -1;");
11389 // Check that * is not treated as a binary operator when we set
11390 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11391 FormatStyle PASLeftStyle = getLLVMStyle();
11392 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11393 verifyFormat("co_return *a;", PASLeftStyle);
11394 verifyFormat("co_await *a;", PASLeftStyle);
11395 verifyFormat("co_yield *a", PASLeftStyle);
11396 verifyFormat("return *a;", PASLeftStyle);
11399 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11400 verifyFormat("if (!aaaaaaaaaa( // break\n"
11401 " aaaaa)) {\n"
11402 "}");
11403 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11404 " aaaaa));");
11405 verifyFormat("*aaa = aaaaaaa( // break\n"
11406 " bbbbbb);");
11409 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11410 verifyFormat("bool operator<();");
11411 verifyFormat("bool operator>();");
11412 verifyFormat("bool operator=();");
11413 verifyFormat("bool operator==();");
11414 verifyFormat("bool operator!=();");
11415 verifyFormat("int operator+();");
11416 verifyFormat("int operator++();");
11417 verifyFormat("int operator++(int) volatile noexcept;");
11418 verifyFormat("bool operator,();");
11419 verifyFormat("bool operator();");
11420 verifyFormat("bool operator()();");
11421 verifyFormat("bool operator[]();");
11422 verifyFormat("operator bool();");
11423 verifyFormat("operator int();");
11424 verifyFormat("operator void *();");
11425 verifyFormat("operator SomeType<int>();");
11426 verifyFormat("operator SomeType<int, int>();");
11427 verifyFormat("operator SomeType<SomeType<int>>();");
11428 verifyFormat("operator< <>();");
11429 verifyFormat("operator<< <>();");
11430 verifyFormat("< <>");
11432 verifyFormat("void *operator new(std::size_t size);");
11433 verifyFormat("void *operator new[](std::size_t size);");
11434 verifyFormat("void operator delete(void *ptr);");
11435 verifyFormat("void operator delete[](void *ptr);");
11436 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11437 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11439 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11441 verifyFormat(
11442 "ostream &operator<<(ostream &OutputStream,\n"
11443 " SomeReallyLongType WithSomeReallyLongValue);");
11444 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11445 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11446 " return left.group < right.group;\n"
11447 "}");
11448 verifyFormat("SomeType &operator=(const SomeType &S);");
11449 verifyFormat("f.template operator()<int>();");
11451 verifyGoogleFormat("operator void*();");
11452 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11453 verifyGoogleFormat("operator ::A();");
11455 verifyFormat("using A::operator+;");
11456 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11457 "int i;");
11459 // Calling an operator as a member function.
11460 verifyFormat("void f() { a.operator*(); }");
11461 verifyFormat("void f() { a.operator*(b & b); }");
11462 verifyFormat("void f() { a->operator&(a * b); }");
11463 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11464 verifyFormat("void f() { operator*(a & a); }");
11465 verifyFormat("void f() { operator&(a, b * b); }");
11467 verifyFormat("void f() { return operator()(x) * b; }");
11468 verifyFormat("void f() { return operator[](x) * b; }");
11469 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11470 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11471 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11472 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11473 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11475 verifyFormat("::operator delete(foo);");
11476 verifyFormat("::operator new(n * sizeof(foo));");
11477 verifyFormat("foo() { ::operator delete(foo); }");
11478 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11481 TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11482 verifyFormat("C<&operator- > minus;");
11483 verifyFormat("C<&operator> > gt;");
11484 verifyFormat("C<&operator>= > ge;");
11485 verifyFormat("C<&operator<= > le;");
11486 verifyFormat("C<&operator< <X>> lt;");
11489 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11490 verifyFormat("void A::b() && {}");
11491 verifyFormat("void A::b() && noexcept {}");
11492 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11493 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11494 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11495 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11496 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11497 verifyFormat("Deleted &operator=(const Deleted &) &;");
11498 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11499 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11500 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11501 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11502 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11503 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11504 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11505 verifyFormat("void Fn(T const &) const &;");
11506 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11507 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11508 verifyGoogleFormat("template <typename T>\n"
11509 "void F(T) && = delete;");
11510 verifyFormat("template <typename T> void operator=(T) &;");
11511 verifyFormat("template <typename T> void operator=(T) const &;");
11512 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11513 verifyFormat("template <typename T> void operator=(T) & = default;");
11514 verifyFormat("template <typename T> void operator=(T) &&;");
11515 verifyFormat("template <typename T> void operator=(T) && = delete;");
11516 verifyFormat("template <typename T> void operator=(T) & {}");
11517 verifyFormat("template <typename T> void operator=(T) && {}");
11519 FormatStyle AlignLeft = getLLVMStyle();
11520 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11521 verifyFormat("void A::b() && {}", AlignLeft);
11522 verifyFormat("void A::b() && noexcept {}", AlignLeft);
11523 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11524 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11525 AlignLeft);
11526 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11527 AlignLeft);
11528 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11529 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11530 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11531 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11532 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11533 verifyFormat("auto Function(T) & -> void;", AlignLeft);
11534 verifyFormat("void Fn(T const&) const&;", AlignLeft);
11535 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11536 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11537 AlignLeft);
11538 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11539 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11540 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11541 AlignLeft);
11542 verifyFormat("template <typename T> void operator=(T) & = default;",
11543 AlignLeft);
11544 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11545 verifyFormat("template <typename T> void operator=(T) && = delete;",
11546 AlignLeft);
11547 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11548 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11549 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft);
11551 FormatStyle AlignMiddle = getLLVMStyle();
11552 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11553 verifyFormat("void A::b() && {}", AlignMiddle);
11554 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11555 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11556 AlignMiddle);
11557 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11558 AlignMiddle);
11559 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11560 AlignMiddle);
11561 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11562 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11563 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11564 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11565 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11566 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11567 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11568 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11569 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11570 AlignMiddle);
11571 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11572 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11573 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11574 AlignMiddle);
11575 verifyFormat("template <typename T> void operator=(T) & = default;",
11576 AlignMiddle);
11577 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11578 verifyFormat("template <typename T> void operator=(T) && = delete;",
11579 AlignMiddle);
11580 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11581 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11583 FormatStyle Spaces = getLLVMStyle();
11584 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11585 Spaces.SpacesInParensOptions = {};
11586 Spaces.SpacesInParensOptions.InCStyleCasts = true;
11587 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11588 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11589 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11590 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11592 Spaces.SpacesInParensOptions.InCStyleCasts = false;
11593 Spaces.SpacesInParensOptions.Other = true;
11594 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11595 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11596 Spaces);
11597 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11598 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11600 FormatStyle BreakTemplate = getLLVMStyle();
11601 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11603 verifyFormat("struct f {\n"
11604 " template <class T>\n"
11605 " int &foo(const std::string &str) & noexcept {}\n"
11606 "};",
11607 BreakTemplate);
11609 verifyFormat("struct f {\n"
11610 " template <class T>\n"
11611 " int &foo(const std::string &str) && noexcept {}\n"
11612 "};",
11613 BreakTemplate);
11615 verifyFormat("struct f {\n"
11616 " template <class T>\n"
11617 " int &foo(const std::string &str) const & noexcept {}\n"
11618 "};",
11619 BreakTemplate);
11621 verifyFormat("struct f {\n"
11622 " template <class T>\n"
11623 " int &foo(const std::string &str) const & noexcept {}\n"
11624 "};",
11625 BreakTemplate);
11627 verifyFormat("struct f {\n"
11628 " template <class T>\n"
11629 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11630 "};",
11631 BreakTemplate);
11633 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11634 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11635 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11637 verifyFormat("struct f {\n"
11638 " template <class T>\n"
11639 " int& foo(const std::string& str) & noexcept {}\n"
11640 "};",
11641 AlignLeftBreakTemplate);
11643 verifyFormat("struct f {\n"
11644 " template <class T>\n"
11645 " int& foo(const std::string& str) && noexcept {}\n"
11646 "};",
11647 AlignLeftBreakTemplate);
11649 verifyFormat("struct f {\n"
11650 " template <class T>\n"
11651 " int& foo(const std::string& str) const& noexcept {}\n"
11652 "};",
11653 AlignLeftBreakTemplate);
11655 verifyFormat("struct f {\n"
11656 " template <class T>\n"
11657 " int& foo(const std::string& str) const&& noexcept {}\n"
11658 "};",
11659 AlignLeftBreakTemplate);
11661 verifyFormat("struct f {\n"
11662 " template <class T>\n"
11663 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11664 "};",
11665 AlignLeftBreakTemplate);
11667 // The `&` in `Type&` should not be confused with a trailing `&` of
11668 // DEPRECATED(reason) member function.
11669 verifyFormat("struct f {\n"
11670 " template <class T>\n"
11671 " DEPRECATED(reason)\n"
11672 " Type &foo(arguments) {}\n"
11673 "};",
11674 BreakTemplate);
11676 verifyFormat("struct f {\n"
11677 " template <class T>\n"
11678 " DEPRECATED(reason)\n"
11679 " Type& foo(arguments) {}\n"
11680 "};",
11681 AlignLeftBreakTemplate);
11683 verifyFormat("void (*foopt)(int) = &func;");
11685 FormatStyle DerivePointerAlignment = getLLVMStyle();
11686 DerivePointerAlignment.DerivePointerAlignment = true;
11687 // There's always a space between the function and its trailing qualifiers.
11688 // This isn't evidence for PAS_Right (or for PAS_Left).
11689 std::string Prefix = "void a() &;\n"
11690 "void b() &;\n";
11691 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11692 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11693 // Same if the function is an overloaded operator, and with &&.
11694 Prefix = "void operator()() &&;\n"
11695 "void operator()() &&;\n";
11696 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11697 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11698 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11699 Prefix = "void a() const &;\n"
11700 "void b() const &;\n";
11701 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11704 TEST_F(FormatTest, PointerAlignmentFallback) {
11705 FormatStyle Style = getLLVMStyle();
11706 Style.DerivePointerAlignment = true;
11708 const StringRef Code("int* p;\n"
11709 "int *q;\n"
11710 "int * r;");
11712 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11713 verifyFormat("int *p;\n"
11714 "int *q;\n"
11715 "int *r;",
11716 Code, Style);
11718 Style.PointerAlignment = FormatStyle::PAS_Left;
11719 verifyFormat("int* p;\n"
11720 "int* q;\n"
11721 "int* r;",
11722 Code, Style);
11724 Style.PointerAlignment = FormatStyle::PAS_Middle;
11725 verifyFormat("int * p;\n"
11726 "int * q;\n"
11727 "int * r;",
11728 Code, Style);
11731 TEST_F(FormatTest, UnderstandsNewAndDelete) {
11732 verifyFormat("void f() {\n"
11733 " A *a = new A;\n"
11734 " A *a = new (placement) A;\n"
11735 " delete a;\n"
11736 " delete (A *)a;\n"
11737 "}");
11738 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11739 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11740 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11741 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11742 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11743 verifyFormat("delete[] h->p;");
11744 verifyFormat("delete[] (void *)p;");
11746 verifyFormat("void operator delete(void *foo) ATTRIB;");
11747 verifyFormat("void operator new(void *foo) ATTRIB;");
11748 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11749 verifyFormat("void operator delete(void *ptr) noexcept;");
11751 verifyFormat("void new(link p);\n"
11752 "void delete(link p);",
11753 "void new (link p);\n"
11754 "void delete (link p);");
11756 verifyFormat("{\n"
11757 " p->new();\n"
11758 "}\n"
11759 "{\n"
11760 " p->delete();\n"
11761 "}",
11762 "{\n"
11763 " p->new ();\n"
11764 "}\n"
11765 "{\n"
11766 " p->delete ();\n"
11767 "}");
11769 FormatStyle AfterPlacementOperator = getLLVMStyle();
11770 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11771 EXPECT_TRUE(
11772 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
11773 verifyFormat("new (buf) int;", AfterPlacementOperator);
11774 verifyFormat("struct A {\n"
11775 " int *a;\n"
11776 " A(int *p) : a(new (p) int) {\n"
11777 " new (p) int;\n"
11778 " int *b = new (p) int;\n"
11779 " int *c = new (p) int(3);\n"
11780 " delete (b);\n"
11781 " }\n"
11782 "};",
11783 AfterPlacementOperator);
11784 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11785 verifyFormat("delete (int *)p;", AfterPlacementOperator);
11787 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11788 false;
11789 verifyFormat("new(buf) int;", AfterPlacementOperator);
11790 verifyFormat("struct A {\n"
11791 " int *a;\n"
11792 " A(int *p) : a(new(p) int) {\n"
11793 " new(p) int;\n"
11794 " int *b = new(p) int;\n"
11795 " int *c = new(p) int(3);\n"
11796 " delete(b);\n"
11797 " }\n"
11798 "};",
11799 AfterPlacementOperator);
11800 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11801 verifyFormat("delete (int *)p;", AfterPlacementOperator);
11804 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11805 verifyFormat("int *f(int *a) {}");
11806 verifyFormat("int main(int argc, char **argv) {}");
11807 verifyFormat("Test::Test(int b) : a(b * b) {}");
11808 verifyIndependentOfContext("f(a, *a);");
11809 verifyFormat("void g() { f(*a); }");
11810 verifyIndependentOfContext("int a = b * 10;");
11811 verifyIndependentOfContext("int a = 10 * b;");
11812 verifyIndependentOfContext("int a = b * c;");
11813 verifyIndependentOfContext("int a += b * c;");
11814 verifyIndependentOfContext("int a -= b * c;");
11815 verifyIndependentOfContext("int a *= b * c;");
11816 verifyIndependentOfContext("int a /= b * c;");
11817 verifyIndependentOfContext("int a = *b;");
11818 verifyIndependentOfContext("int a = *b * c;");
11819 verifyIndependentOfContext("int a = b * *c;");
11820 verifyIndependentOfContext("int a = b * (10);");
11821 verifyIndependentOfContext("S << b * (10);");
11822 verifyIndependentOfContext("return 10 * b;");
11823 verifyIndependentOfContext("return *b * *c;");
11824 verifyIndependentOfContext("return a & ~b;");
11825 verifyIndependentOfContext("f(b ? *c : *d);");
11826 verifyIndependentOfContext("int a = b ? *c : *d;");
11827 verifyIndependentOfContext("*b = a;");
11828 verifyIndependentOfContext("a * ~b;");
11829 verifyIndependentOfContext("a * !b;");
11830 verifyIndependentOfContext("a * +b;");
11831 verifyIndependentOfContext("a * -b;");
11832 verifyIndependentOfContext("a * ++b;");
11833 verifyIndependentOfContext("a * --b;");
11834 verifyIndependentOfContext("a[4] * b;");
11835 verifyIndependentOfContext("a[a * a] = 1;");
11836 verifyIndependentOfContext("f() * b;");
11837 verifyIndependentOfContext("a * [self dostuff];");
11838 verifyIndependentOfContext("int x = a * (a + b);");
11839 verifyIndependentOfContext("(a *)(a + b);");
11840 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11841 verifyIndependentOfContext("int *pa = (int *)&a;");
11842 verifyIndependentOfContext("return sizeof(int **);");
11843 verifyIndependentOfContext("return sizeof(int ******);");
11844 verifyIndependentOfContext("return (int **&)a;");
11845 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11846 verifyFormat("void f(Type (*parameter)[10]) {}");
11847 verifyFormat("void f(Type (&parameter)[10]) {}");
11848 verifyGoogleFormat("return sizeof(int**);");
11849 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11850 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11851 verifyFormat("auto a = [](int **&, int ***) {};");
11852 verifyFormat("auto PointerBinding = [](const char *S) {};");
11853 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11854 verifyFormat("[](const decltype(*a) &value) {}");
11855 verifyFormat("[](const typeof(*a) &value) {}");
11856 verifyFormat("[](const _Atomic(a *) &value) {}");
11857 verifyFormat("[](const __underlying_type(a) &value) {}");
11858 verifyFormat("decltype(a * b) F();");
11859 verifyFormat("typeof(a * b) F();");
11860 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11861 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11862 verifyIndependentOfContext("typedef void (*f)(int *a);");
11863 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11864 verifyIndependentOfContext("int i{a * b};");
11865 verifyIndependentOfContext("aaa && aaa->f();");
11866 verifyIndependentOfContext("int x = ~*p;");
11867 verifyFormat("Constructor() : a(a), area(width * height) {}");
11868 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11869 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11870 verifyFormat("void f() { f(a, c * d); }");
11871 verifyFormat("void f() { f(new a(), c * d); }");
11872 verifyFormat("void f(const MyOverride &override);");
11873 verifyFormat("void f(const MyFinal &final);");
11874 verifyIndependentOfContext("bool a = f() && override.f();");
11875 verifyIndependentOfContext("bool a = f() && final.f();");
11877 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11879 verifyIndependentOfContext("A<int *> a;");
11880 verifyIndependentOfContext("A<int **> a;");
11881 verifyIndependentOfContext("A<int *, int *> a;");
11882 verifyIndependentOfContext("A<int *[]> a;");
11883 verifyIndependentOfContext(
11884 "const char *const p = reinterpret_cast<const char *const>(q);");
11885 verifyIndependentOfContext("A<int **, int **> a;");
11886 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11887 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11888 verifyFormat("for (; a && b;) {\n}");
11889 verifyFormat("bool foo = true && [] { return false; }();");
11891 verifyFormat(
11892 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11895 verifyGoogleFormat("int const* a = &b;");
11896 verifyGoogleFormat("**outparam = 1;");
11897 verifyGoogleFormat("*outparam = a * b;");
11898 verifyGoogleFormat("int main(int argc, char** argv) {}");
11899 verifyGoogleFormat("A<int*> a;");
11900 verifyGoogleFormat("A<int**> a;");
11901 verifyGoogleFormat("A<int*, int*> a;");
11902 verifyGoogleFormat("A<int**, int**> a;");
11903 verifyGoogleFormat("f(b ? *c : *d);");
11904 verifyGoogleFormat("int a = b ? *c : *d;");
11905 verifyGoogleFormat("Type* t = **x;");
11906 verifyGoogleFormat("Type* t = *++*x;");
11907 verifyGoogleFormat("*++*x;");
11908 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11909 verifyGoogleFormat("Type* t = x++ * y;");
11910 verifyGoogleFormat(
11911 "const char* const p = reinterpret_cast<const char* const>(q);");
11912 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11913 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11914 verifyGoogleFormat("template <typename T>\n"
11915 "void f(int i = 0, SomeType** temps = NULL);");
11917 FormatStyle Left = getLLVMStyle();
11918 Left.PointerAlignment = FormatStyle::PAS_Left;
11919 verifyFormat("x = *a(x) = *a(y);", Left);
11920 verifyFormat("for (;; *a = b) {\n}", Left);
11921 verifyFormat("return *this += 1;", Left);
11922 verifyFormat("throw *x;", Left);
11923 verifyFormat("delete *x;", Left);
11924 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
11925 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
11926 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
11927 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
11928 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
11929 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
11930 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
11931 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
11932 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
11934 verifyIndependentOfContext("a = *(x + y);");
11935 verifyIndependentOfContext("a = &(x + y);");
11936 verifyIndependentOfContext("*(x + y).call();");
11937 verifyIndependentOfContext("&(x + y)->call();");
11938 verifyFormat("void f() { &(*I).first; }");
11940 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11941 verifyFormat("f(* /* confusing comment */ foo);");
11942 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11943 verifyFormat("void foo(int * // this is the first paramters\n"
11944 " ,\n"
11945 " int second);");
11946 verifyFormat("double term = a * // first\n"
11947 " b;");
11948 verifyFormat(
11949 "int *MyValues = {\n"
11950 " *A, // Operator detection might be confused by the '{'\n"
11951 " *BB // Operator detection might be confused by previous comment\n"
11952 "};");
11954 verifyIndependentOfContext("if (int *a = &b)");
11955 verifyIndependentOfContext("if (int &a = *b)");
11956 verifyIndependentOfContext("if (a & b[i])");
11957 verifyIndependentOfContext("if constexpr (a & b[i])");
11958 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11959 verifyIndependentOfContext("if (a * (b * c))");
11960 verifyIndependentOfContext("if constexpr (a * (b * c))");
11961 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11962 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11963 verifyIndependentOfContext("if (*b[i])");
11964 verifyIndependentOfContext("if (int *a = (&b))");
11965 verifyIndependentOfContext("while (int *a = &b)");
11966 verifyIndependentOfContext("while (a * (b * c))");
11967 verifyIndependentOfContext("size = sizeof *a;");
11968 verifyIndependentOfContext("if (a && (b = c))");
11969 verifyFormat("void f() {\n"
11970 " for (const int &v : Values) {\n"
11971 " }\n"
11972 "}");
11973 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11974 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11975 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11977 verifyFormat("#define A (!a * b)");
11978 verifyFormat("#define MACRO \\\n"
11979 " int *i = a * b; \\\n"
11980 " void f(a *b);",
11981 getLLVMStyleWithColumns(19));
11983 verifyIndependentOfContext("A = new SomeType *[Length];");
11984 verifyIndependentOfContext("A = new SomeType *[Length]();");
11985 verifyIndependentOfContext("T **t = new T *;");
11986 verifyIndependentOfContext("T **t = new T *();");
11987 verifyGoogleFormat("A = new SomeType*[Length]();");
11988 verifyGoogleFormat("A = new SomeType*[Length];");
11989 verifyGoogleFormat("T** t = new T*;");
11990 verifyGoogleFormat("T** t = new T*();");
11992 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11993 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11994 verifyFormat("template <bool a, bool b> "
11995 "typename t::if<x && y>::type f() {}");
11996 verifyFormat("template <int *y> f() {}");
11997 verifyFormat("vector<int *> v;");
11998 verifyFormat("vector<int *const> v;");
11999 verifyFormat("vector<int *const **const *> v;");
12000 verifyFormat("vector<int *volatile> v;");
12001 verifyFormat("vector<a *_Nonnull> v;");
12002 verifyFormat("vector<a *_Nullable> v;");
12003 verifyFormat("vector<a *_Null_unspecified> v;");
12004 verifyFormat("vector<a *__ptr32> v;");
12005 verifyFormat("vector<a *__ptr64> v;");
12006 verifyFormat("vector<a *__capability> v;");
12007 FormatStyle TypeMacros = getLLVMStyle();
12008 TypeMacros.TypenameMacros = {"LIST"};
12009 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
12010 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
12011 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
12012 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
12013 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
12015 FormatStyle CustomQualifier = getLLVMStyle();
12016 // Add identifiers that should not be parsed as a qualifier by default.
12017 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12018 CustomQualifier.AttributeMacros.push_back("_My_qualifier");
12019 CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
12020 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
12021 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
12022 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
12023 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
12024 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
12025 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
12026 verifyFormat("vector<a * _NotAQualifier> v;");
12027 verifyFormat("vector<a * __not_a_qualifier> v;");
12028 verifyFormat("vector<a * b> v;");
12029 verifyFormat("foo<b && false>();");
12030 verifyFormat("foo<b & 1>();");
12031 verifyFormat("foo<b & (1)>();");
12032 verifyFormat("foo<b & (~0)>();");
12033 verifyFormat("foo<b & (true)>();");
12034 verifyFormat("foo<b & ((1))>();");
12035 verifyFormat("foo<b & (/*comment*/ 1)>();");
12036 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12037 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12038 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12039 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12040 verifyFormat(
12041 "template <class T, class = typename std::enable_if<\n"
12042 " std::is_integral<T>::value &&\n"
12043 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12044 "void F();",
12045 getLLVMStyleWithColumns(70));
12046 verifyFormat("template <class T,\n"
12047 " class = typename std::enable_if<\n"
12048 " std::is_integral<T>::value &&\n"
12049 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12050 " class U>\n"
12051 "void F();",
12052 getLLVMStyleWithColumns(70));
12053 verifyFormat(
12054 "template <class T,\n"
12055 " class = typename ::std::enable_if<\n"
12056 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12057 "void F();",
12058 getGoogleStyleWithColumns(68));
12060 FormatStyle Style = getLLVMStyle();
12061 Style.PointerAlignment = FormatStyle::PAS_Left;
12062 verifyFormat("struct {\n"
12063 "}* ptr;",
12064 Style);
12065 verifyFormat("union {\n"
12066 "}* ptr;",
12067 Style);
12068 verifyFormat("class {\n"
12069 "}* ptr;",
12070 Style);
12071 // Don't confuse a multiplication after a brace-initialized expression with
12072 // a class pointer.
12073 verifyFormat("int i = int{42} * 34;", Style);
12074 verifyFormat("struct {\n"
12075 "}&& ptr = {};",
12076 Style);
12077 verifyFormat("union {\n"
12078 "}&& ptr = {};",
12079 Style);
12080 verifyFormat("class {\n"
12081 "}&& ptr = {};",
12082 Style);
12083 verifyFormat("bool b = 3 == int{3} && true;");
12085 Style.PointerAlignment = FormatStyle::PAS_Middle;
12086 verifyFormat("struct {\n"
12087 "} * ptr;",
12088 Style);
12089 verifyFormat("union {\n"
12090 "} * ptr;",
12091 Style);
12092 verifyFormat("class {\n"
12093 "} * ptr;",
12094 Style);
12095 verifyFormat("struct {\n"
12096 "} && ptr = {};",
12097 Style);
12098 verifyFormat("union {\n"
12099 "} && ptr = {};",
12100 Style);
12101 verifyFormat("class {\n"
12102 "} && ptr = {};",
12103 Style);
12105 Style.PointerAlignment = FormatStyle::PAS_Right;
12106 verifyFormat("struct {\n"
12107 "} *ptr;",
12108 Style);
12109 verifyFormat("union {\n"
12110 "} *ptr;",
12111 Style);
12112 verifyFormat("class {\n"
12113 "} *ptr;",
12114 Style);
12115 verifyFormat("struct {\n"
12116 "} &&ptr = {};",
12117 Style);
12118 verifyFormat("union {\n"
12119 "} &&ptr = {};",
12120 Style);
12121 verifyFormat("class {\n"
12122 "} &&ptr = {};",
12123 Style);
12125 Style.PointerAlignment = FormatStyle::PAS_Left;
12126 verifyFormat("delete[] *ptr;", Style);
12127 verifyFormat("delete[] **ptr;", Style);
12128 verifyFormat("delete[] *(ptr);", Style);
12130 verifyIndependentOfContext("MACRO(int *i);");
12131 verifyIndependentOfContext("MACRO(auto *a);");
12132 verifyIndependentOfContext("MACRO(const A *a);");
12133 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12134 verifyIndependentOfContext("MACRO(decltype(A) *a);");
12135 verifyIndependentOfContext("MACRO(typeof(A) *a);");
12136 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12137 verifyIndependentOfContext("MACRO(A *const a);");
12138 verifyIndependentOfContext("MACRO(A *restrict a);");
12139 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12140 verifyIndependentOfContext("MACRO(A *__restrict a);");
12141 verifyIndependentOfContext("MACRO(A *volatile a);");
12142 verifyIndependentOfContext("MACRO(A *__volatile a);");
12143 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12144 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12145 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12146 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12147 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12148 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12149 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12150 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12151 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12152 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12153 verifyIndependentOfContext("MACRO(A *__capability);");
12154 verifyIndependentOfContext("MACRO(A &__capability);");
12155 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12156 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12157 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12158 // a type declaration:
12159 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
12160 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
12161 // Also check that TypenameMacros prevents parsing it as multiplication:
12162 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12163 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
12165 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12166 verifyFormat("void f() { f(float{1}, a * a); }");
12167 verifyFormat("void f() { f(float(1), a * a); }");
12169 verifyFormat("f((void (*)(int))g);");
12170 verifyFormat("f((void (&)(int))g);");
12171 verifyFormat("f((void (^)(int))g);");
12173 // FIXME: Is there a way to make this work?
12174 // verifyIndependentOfContext("MACRO(A *a);");
12175 verifyFormat("MACRO(A &B);");
12176 verifyFormat("MACRO(A *B);");
12177 verifyFormat("void f() { MACRO(A * B); }");
12178 verifyFormat("void f() { MACRO(A & B); }");
12180 // This lambda was mis-formatted after D88956 (treating it as a binop):
12181 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12182 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12183 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12184 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12186 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12187 verifyFormat("return options != nullptr && operator==(*options);");
12189 verifyFormat("#define OP(x) \\\n"
12190 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12191 " return s << a.DebugString(); \\\n"
12192 " }",
12193 "#define OP(x) \\\n"
12194 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12195 " return s << a.DebugString(); \\\n"
12196 " }",
12197 getLLVMStyleWithColumns(50));
12199 verifyFormat("#define FOO \\\n"
12200 " void foo() { \\\n"
12201 " operator+(a * b); \\\n"
12202 " }",
12203 getLLVMStyleWithColumns(25));
12205 // FIXME: We cannot handle this case yet; we might be able to figure out that
12206 // foo<x> d > v; doesn't make sense.
12207 verifyFormat("foo<a<b && c> d> v;");
12209 FormatStyle PointerMiddle = getLLVMStyle();
12210 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12211 verifyFormat("delete *x;", PointerMiddle);
12212 verifyFormat("int * x;", PointerMiddle);
12213 verifyFormat("int *[] x;", PointerMiddle);
12214 verifyFormat("template <int * y> f() {}", PointerMiddle);
12215 verifyFormat("int * f(int * a) {}", PointerMiddle);
12216 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12217 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12218 verifyFormat("A<int *> a;", PointerMiddle);
12219 verifyFormat("A<int **> a;", PointerMiddle);
12220 verifyFormat("A<int *, int *> a;", PointerMiddle);
12221 verifyFormat("A<int *[]> a;", PointerMiddle);
12222 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12223 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12224 verifyFormat("T ** t = new T *;", PointerMiddle);
12226 // Member function reference qualifiers aren't binary operators.
12227 verifyFormat("string // break\n"
12228 "operator()() & {}");
12229 verifyFormat("string // break\n"
12230 "operator()() && {}");
12231 verifyGoogleFormat("template <typename T>\n"
12232 "auto x() & -> int {}");
12234 // Should be binary operators when used as an argument expression (overloaded
12235 // operator invoked as a member function).
12236 verifyFormat("void f() { a.operator()(a * a); }");
12237 verifyFormat("void f() { a->operator()(a & a); }");
12238 verifyFormat("void f() { a.operator()(*a & *a); }");
12239 verifyFormat("void f() { a->operator()(*a * *a); }");
12241 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12242 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12244 verifyFormat("val1 & val2;");
12245 verifyFormat("val1 & val2 & val3;");
12246 verifyFormat("class c {\n"
12247 " void func(type &a) { a & member; }\n"
12248 " anotherType &member;\n"
12249 "}");
12252 TEST_F(FormatTest, UnderstandsAttributes) {
12253 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12254 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12255 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12256 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12257 FormatStyle AfterType = getLLVMStyle();
12258 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12259 verifyFormat("__attribute__((nodebug)) void\n"
12260 "foo() {}",
12261 AfterType);
12262 verifyFormat("__unused void\n"
12263 "foo() {}",
12264 AfterType);
12266 FormatStyle CustomAttrs = getLLVMStyle();
12267 CustomAttrs.AttributeMacros.push_back("__unused");
12268 CustomAttrs.AttributeMacros.push_back("__attr1");
12269 CustomAttrs.AttributeMacros.push_back("__attr2");
12270 CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
12271 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12272 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12273 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12274 // Check that it is parsed as a multiplication without AttributeMacros and
12275 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12276 verifyFormat("vector<SomeType * __attr1> v;");
12277 verifyFormat("vector<SomeType __attr1 *> v;");
12278 verifyFormat("vector<SomeType __attr1 *const> v;");
12279 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12280 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12281 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12282 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12283 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12284 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12285 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12286 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12287 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12288 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12289 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12291 // Check that these are not parsed as function declarations:
12292 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12293 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12294 verifyFormat("SomeType s(InitValue);", CustomAttrs);
12295 verifyFormat("SomeType s{InitValue};", CustomAttrs);
12296 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12297 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12298 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12299 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12300 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12301 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12304 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12305 // Check that qualifiers on pointers don't break parsing of casts.
12306 verifyFormat("x = (foo *const)*v;");
12307 verifyFormat("x = (foo *volatile)*v;");
12308 verifyFormat("x = (foo *restrict)*v;");
12309 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12310 verifyFormat("x = (foo *_Nonnull)*v;");
12311 verifyFormat("x = (foo *_Nullable)*v;");
12312 verifyFormat("x = (foo *_Null_unspecified)*v;");
12313 verifyFormat("x = (foo *_Nonnull)*v;");
12314 verifyFormat("x = (foo *[[clang::attr]])*v;");
12315 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12316 verifyFormat("x = (foo *__ptr32)*v;");
12317 verifyFormat("x = (foo *__ptr64)*v;");
12318 verifyFormat("x = (foo *__capability)*v;");
12320 // Check that we handle multiple trailing qualifiers and skip them all to
12321 // determine that the expression is a cast to a pointer type.
12322 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
12323 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
12324 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12325 StringRef AllQualifiers =
12326 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12327 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12328 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12329 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12331 // Also check that address-of is not parsed as a binary bitwise-and:
12332 verifyFormat("x = (foo *const)&v;");
12333 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12334 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12336 // Check custom qualifiers:
12337 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
12338 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12339 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12340 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12341 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12342 CustomQualifier);
12343 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12344 CustomQualifier);
12346 // Check that unknown identifiers result in binary operator parsing:
12347 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12348 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12351 TEST_F(FormatTest, UnderstandsSquareAttributes) {
12352 verifyFormat("SomeType s [[unused]] (InitValue);");
12353 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12354 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12355 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12356 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12357 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12358 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12359 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12360 verifyFormat("[[nodiscard]] bool f() { return false; }");
12361 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12362 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12363 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12364 verifyFormat("[[nodiscard]] ::qualified_type f();");
12366 // Make sure we do not mistake attributes for array subscripts.
12367 verifyFormat("int a() {}\n"
12368 "[[unused]] int b() {}");
12369 verifyFormat("NSArray *arr;\n"
12370 "arr[[Foo() bar]];");
12372 // On the other hand, we still need to correctly find array subscripts.
12373 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12375 // Make sure that we do not mistake Objective-C method inside array literals
12376 // as attributes, even if those method names are also keywords.
12377 verifyFormat("@[ [foo bar] ];");
12378 verifyFormat("@[ [NSArray class] ];");
12379 verifyFormat("@[ [foo enum] ];");
12381 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12383 // Make sure we do not parse attributes as lambda introducers.
12384 FormatStyle MultiLineFunctions = getLLVMStyle();
12385 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12386 verifyFormat("[[unused]] int b() {\n"
12387 " return 42;\n"
12388 "}",
12389 MultiLineFunctions);
12392 TEST_F(FormatTest, AttributeClass) {
12393 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
12394 verifyFormat("class S {\n"
12395 " S(S&&) = default;\n"
12396 "};",
12397 Style);
12398 verifyFormat("class [[nodiscard]] S {\n"
12399 " S(S&&) = default;\n"
12400 "};",
12401 Style);
12402 verifyFormat("class __attribute((maybeunused)) S {\n"
12403 " S(S&&) = default;\n"
12404 "};",
12405 Style);
12406 verifyFormat("struct S {\n"
12407 " S(S&&) = default;\n"
12408 "};",
12409 Style);
12410 verifyFormat("struct [[nodiscard]] S {\n"
12411 " S(S&&) = default;\n"
12412 "};",
12413 Style);
12416 TEST_F(FormatTest, AttributesAfterMacro) {
12417 FormatStyle Style = getLLVMStyle();
12418 verifyFormat("MACRO;\n"
12419 "__attribute__((maybe_unused)) int foo() {\n"
12420 " //...\n"
12421 "}");
12423 verifyFormat("MACRO;\n"
12424 "[[nodiscard]] int foo() {\n"
12425 " //...\n"
12426 "}");
12428 verifyNoChange("MACRO\n\n"
12429 "__attribute__((maybe_unused)) int foo() {\n"
12430 " //...\n"
12431 "}");
12433 verifyNoChange("MACRO\n\n"
12434 "[[nodiscard]] int foo() {\n"
12435 " //...\n"
12436 "}");
12439 TEST_F(FormatTest, AttributePenaltyBreaking) {
12440 FormatStyle Style = getLLVMStyle();
12441 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12442 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12443 Style);
12444 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12445 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12446 Style);
12447 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12448 "shared_ptr<ALongTypeName> &C d) {\n}",
12449 Style);
12452 TEST_F(FormatTest, UnderstandsEllipsis) {
12453 FormatStyle Style = getLLVMStyle();
12454 verifyFormat("int printf(const char *fmt, ...);");
12455 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12456 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12458 verifyFormat("template <int *...PP> a;", Style);
12460 Style.PointerAlignment = FormatStyle::PAS_Left;
12461 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12463 verifyFormat("template <int*... PP> a;", Style);
12465 Style.PointerAlignment = FormatStyle::PAS_Middle;
12466 verifyFormat("template <int *... PP> a;", Style);
12469 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12470 verifyFormat("int *a;\n"
12471 "int *a;\n"
12472 "int *a;",
12473 "int *a;\n"
12474 "int* a;\n"
12475 "int *a;",
12476 getGoogleStyle());
12477 verifyFormat("int* a;\n"
12478 "int* a;\n"
12479 "int* a;",
12480 "int* a;\n"
12481 "int* a;\n"
12482 "int *a;",
12483 getGoogleStyle());
12484 verifyFormat("int *a;\n"
12485 "int *a;\n"
12486 "int *a;",
12487 "int *a;\n"
12488 "int * a;\n"
12489 "int * a;",
12490 getGoogleStyle());
12491 verifyFormat("auto x = [] {\n"
12492 " int *a;\n"
12493 " int *a;\n"
12494 " int *a;\n"
12495 "};",
12496 "auto x=[]{int *a;\n"
12497 "int * a;\n"
12498 "int * a;};",
12499 getGoogleStyle());
12502 TEST_F(FormatTest, UnderstandsRvalueReferences) {
12503 verifyFormat("int f(int &&a) {}");
12504 verifyFormat("int f(int a, char &&b) {}");
12505 verifyFormat("void f() { int &&a = b; }");
12506 verifyGoogleFormat("int f(int a, char&& b) {}");
12507 verifyGoogleFormat("void f() { int&& a = b; }");
12509 verifyIndependentOfContext("A<int &&> a;");
12510 verifyIndependentOfContext("A<int &&, int &&> a;");
12511 verifyGoogleFormat("A<int&&> a;");
12512 verifyGoogleFormat("A<int&&, int&&> a;");
12514 // Not rvalue references:
12515 verifyFormat("template <bool B, bool C> class A {\n"
12516 " static_assert(B && C, \"Something is wrong\");\n"
12517 "};");
12518 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12519 verifyFormat("template <typename T> struct S {\n"
12520 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12521 "};");
12522 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12523 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12524 verifyFormat("#define A(a, b) (a && b)");
12527 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
12528 verifyFormat("void f() {\n"
12529 " x[aaaaaaaaa -\n"
12530 " b] = 23;\n"
12531 "}",
12532 getLLVMStyleWithColumns(15));
12535 TEST_F(FormatTest, FormatsCasts) {
12536 verifyFormat("Type *A = static_cast<Type *>(P);");
12537 verifyFormat("static_cast<Type *>(P);");
12538 verifyFormat("static_cast<Type &>(Fun)(Args);");
12539 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12540 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12541 // Check that static_cast<...>(...) does not require the next token to be on
12542 // the same line.
12543 verifyFormat("some_loooong_output << something_something__ << "
12544 "static_cast<const void *>(R)\n"
12545 " << something;");
12546 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12547 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12548 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12549 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12550 verifyFormat("Type *A = (Type *)P;");
12551 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12552 verifyFormat("int a = (int)(2.0f);");
12553 verifyFormat("int a = (int)2.0f;");
12554 verifyFormat("x[(int32)y];");
12555 verifyFormat("x = (int32)y;");
12556 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12557 verifyFormat("int a = (int)*b;");
12558 verifyFormat("int a = (int)2.0f;");
12559 verifyFormat("int a = (int)~0;");
12560 verifyFormat("int a = (int)++a;");
12561 verifyFormat("int a = (int)sizeof(int);");
12562 verifyFormat("int a = (int)+2;");
12563 verifyFormat("my_int a = (my_int)2.0f;");
12564 verifyFormat("my_int a = (my_int)sizeof(int);");
12565 verifyFormat("return (my_int)aaa;");
12566 verifyFormat("throw (my_int)aaa;");
12567 verifyFormat("#define x ((int)-1)");
12568 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12569 verifyFormat("#define p(q) ((int *)&q)");
12570 verifyFormat("fn(a)(b) + 1;");
12572 verifyFormat("void f() { my_int a = (my_int)*b; }");
12573 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12574 verifyFormat("my_int a = (my_int)~0;");
12575 verifyFormat("my_int a = (my_int)++a;");
12576 verifyFormat("my_int a = (my_int)-2;");
12577 verifyFormat("my_int a = (my_int)1;");
12578 verifyFormat("my_int a = (my_int *)1;");
12579 verifyFormat("my_int a = (const my_int)-1;");
12580 verifyFormat("my_int a = (const my_int *)-1;");
12581 verifyFormat("my_int a = (my_int)(my_int)-1;");
12582 verifyFormat("my_int a = (ns::my_int)-2;");
12583 verifyFormat("case (my_int)ONE:");
12584 verifyFormat("auto x = (X)this;");
12585 // Casts in Obj-C style calls used to not be recognized as such.
12586 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12588 // FIXME: single value wrapped with paren will be treated as cast.
12589 verifyFormat("void f(int i = (kValue)*kMask) {}");
12591 verifyFormat("{\n"
12592 " (void)F;\n"
12593 "}");
12595 // Don't break after a cast's
12596 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12597 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12598 " bbbbbbbbbbbbbbbbbbbbbb);");
12600 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12601 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12602 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12603 verifyFormat("bool *y = (bool *)(void *)(x);");
12604 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12605 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12606 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12607 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12609 // These are not casts.
12610 verifyFormat("void f(int *) {}");
12611 verifyFormat("f(foo)->b;");
12612 verifyFormat("f(foo).b;");
12613 verifyFormat("f(foo)(b);");
12614 verifyFormat("f(foo)[b];");
12615 verifyFormat("[](foo) { return 4; }(bar);");
12616 verifyFormat("(*funptr)(foo)[4];");
12617 verifyFormat("funptrs[4](foo)[4];");
12618 verifyFormat("void f(int *);");
12619 verifyFormat("void f(int *) = 0;");
12620 verifyFormat("void f(SmallVector<int>) {}");
12621 verifyFormat("void f(SmallVector<int>);");
12622 verifyFormat("void f(SmallVector<int>) = 0;");
12623 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12624 verifyFormat("int a = sizeof(int) * b;");
12625 verifyGoogleFormat("int a = alignof(int) * b;");
12626 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12627 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12628 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12630 // These are not casts, but at some point were confused with casts.
12631 verifyFormat("virtual void foo(int *) override;");
12632 verifyFormat("virtual void foo(char &) const;");
12633 verifyFormat("virtual void foo(int *a, char *) const;");
12634 verifyFormat("int a = sizeof(int *) + b;");
12635 verifyGoogleFormat("int a = alignof(int *) + b;");
12636 verifyFormat("bool b = f(g<int>) && c;");
12637 verifyFormat("typedef void (*f)(int i) func;");
12638 verifyFormat("void operator++(int) noexcept;");
12639 verifyFormat("void operator++(int &) noexcept;");
12640 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12641 "&) noexcept;");
12642 verifyFormat(
12643 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12644 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12645 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12646 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12647 verifyFormat("void operator delete(foo &) noexcept;");
12648 verifyFormat("void operator delete(foo) noexcept;");
12649 verifyFormat("void operator delete(int) noexcept;");
12650 verifyFormat("void operator delete(int &) noexcept;");
12651 verifyFormat("void operator delete(int &) volatile noexcept;");
12652 verifyFormat("void operator delete(int &) const");
12653 verifyFormat("void operator delete(int &) = default");
12654 verifyFormat("void operator delete(int &) = delete");
12655 verifyFormat("void operator delete(int &) [[noreturn]]");
12656 verifyFormat("void operator delete(int &) throw();");
12657 verifyFormat("void operator delete(int &) throw(int);");
12658 verifyFormat("auto operator delete(int &) -> int;");
12659 verifyFormat("auto operator delete(int &) override");
12660 verifyFormat("auto operator delete(int &) final");
12662 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12663 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12664 // FIXME: The indentation here is not ideal.
12665 verifyFormat(
12666 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12667 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12668 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12671 TEST_F(FormatTest, FormatsFunctionTypes) {
12672 verifyFormat("A<bool()> a;");
12673 verifyFormat("A<SomeType()> a;");
12674 verifyFormat("A<void (*)(int, std::string)> a;");
12675 verifyFormat("A<void *(int)>;");
12676 verifyFormat("void *(*a)(int *, SomeType *);");
12677 verifyFormat("int (*func)(void *);");
12678 verifyFormat("void f() { int (*func)(void *); }");
12679 verifyFormat("template <class CallbackClass>\n"
12680 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12682 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12683 verifyGoogleFormat("void* (*a)(int);");
12684 verifyGoogleFormat(
12685 "template <class CallbackClass>\n"
12686 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12688 // Other constructs can look somewhat like function types:
12689 verifyFormat("A<sizeof(*x)> a;");
12690 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12691 verifyFormat("some_var = function(*some_pointer_var)[0];");
12692 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12693 verifyFormat("int x = f(&h)();");
12694 verifyFormat("returnsFunction(&param1, &param2)(param);");
12695 verifyFormat("std::function<\n"
12696 " LooooooooooongTemplatedType<\n"
12697 " SomeType>*(\n"
12698 " LooooooooooooooooongType type)>\n"
12699 " function;",
12700 getGoogleStyleWithColumns(40));
12703 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12704 verifyFormat("A (*foo_)[6];");
12705 verifyFormat("vector<int> (*foo_)[6];");
12708 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12709 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12710 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12711 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12712 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12713 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12714 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12716 // Different ways of ()-initializiation.
12717 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12718 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12719 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12720 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12721 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12722 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12723 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12724 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12726 // Lambdas should not confuse the variable declaration heuristic.
12727 verifyFormat("LooooooooooooooooongType\n"
12728 " variable(nullptr, [](A *a) {});",
12729 getLLVMStyleWithColumns(40));
12732 TEST_F(FormatTest, BreaksLongDeclarations) {
12733 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12734 " AnotherNameForTheLongType;");
12735 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12737 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12738 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12739 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12740 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12741 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12742 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12743 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12744 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12745 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12746 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12747 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12748 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12749 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12750 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12751 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12752 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12753 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12754 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12755 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12756 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12757 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12758 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12759 FormatStyle Indented = getLLVMStyle();
12760 Indented.IndentWrappedFunctionNames = true;
12761 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12762 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12763 Indented);
12764 verifyFormat(
12765 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12766 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12767 Indented);
12768 verifyFormat(
12769 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12770 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12771 Indented);
12772 verifyFormat(
12773 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12774 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12775 Indented);
12777 // FIXME: Without the comment, this breaks after "(".
12778 verifyGoogleFormat(
12779 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12780 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12782 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12783 " int LoooooooooooooooooooongParam2) {}");
12784 verifyFormat(
12785 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12786 " SourceLocation L, IdentifierIn *II,\n"
12787 " Type *T) {}");
12788 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12789 "ReallyReaaallyLongFunctionName(\n"
12790 " const std::string &SomeParameter,\n"
12791 " const SomeType<string, SomeOtherTemplateParameter>\n"
12792 " &ReallyReallyLongParameterName,\n"
12793 " const SomeType<string, SomeOtherTemplateParameter>\n"
12794 " &AnotherLongParameterName) {}");
12795 verifyFormat("template <typename A>\n"
12796 "SomeLoooooooooooooooooooooongType<\n"
12797 " typename some_namespace::SomeOtherType<A>::Type>\n"
12798 "Function() {}");
12800 verifyGoogleFormat(
12801 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12802 " aaaaaaaaaaaaaaaaaaaaaaa;");
12803 verifyGoogleFormat(
12804 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12805 " SourceLocation L) {}");
12806 verifyGoogleFormat(
12807 "some_namespace::LongReturnType\n"
12808 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12809 " int first_long_parameter, int second_parameter) {}");
12811 verifyGoogleFormat("template <typename T>\n"
12812 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12813 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12814 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12815 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12817 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12818 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12819 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12820 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12821 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12822 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12823 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12824 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12828 verifyFormat("template <typename T> // Templates on own line.\n"
12829 "static int // Some comment.\n"
12830 "MyFunction(int a);");
12833 TEST_F(FormatTest, FormatsAccessModifiers) {
12834 FormatStyle Style = getLLVMStyle();
12835 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
12836 FormatStyle::ELBAMS_LogicalBlock);
12837 verifyFormat("struct foo {\n"
12838 "private:\n"
12839 " void f() {}\n"
12840 "\n"
12841 "private:\n"
12842 " int i;\n"
12843 "\n"
12844 "protected:\n"
12845 " int j;\n"
12846 "};",
12847 Style);
12848 verifyFormat("struct foo {\n"
12849 "private:\n"
12850 " void f() {}\n"
12851 "\n"
12852 "private:\n"
12853 " int i;\n"
12854 "\n"
12855 "protected:\n"
12856 " int j;\n"
12857 "};",
12858 "struct foo {\n"
12859 "private:\n"
12860 " void f() {}\n"
12861 "private:\n"
12862 " int i;\n"
12863 "protected:\n"
12864 " int j;\n"
12865 "};",
12866 Style);
12867 verifyFormat("struct foo { /* comment */\n"
12868 "private:\n"
12869 " int i;\n"
12870 " // comment\n"
12871 "private:\n"
12872 " int j;\n"
12873 "};",
12874 Style);
12875 verifyFormat("struct foo {\n"
12876 "#ifdef FOO\n"
12877 "#endif\n"
12878 "private:\n"
12879 " int i;\n"
12880 "#ifdef FOO\n"
12881 "private:\n"
12882 "#endif\n"
12883 " int j;\n"
12884 "};",
12885 Style);
12886 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12887 verifyFormat("struct foo {\n"
12888 "private:\n"
12889 " void f() {}\n"
12890 "private:\n"
12891 " int i;\n"
12892 "protected:\n"
12893 " int j;\n"
12894 "};",
12895 Style);
12896 verifyFormat("struct foo {\n"
12897 "private:\n"
12898 " void f() {}\n"
12899 "private:\n"
12900 " int i;\n"
12901 "protected:\n"
12902 " int j;\n"
12903 "};",
12904 "struct foo {\n"
12905 "\n"
12906 "private:\n"
12907 " void f() {}\n"
12908 "\n"
12909 "private:\n"
12910 " int i;\n"
12911 "\n"
12912 "protected:\n"
12913 " int j;\n"
12914 "};",
12915 Style);
12916 verifyFormat("struct foo { /* comment */\n"
12917 "private:\n"
12918 " int i;\n"
12919 " // comment\n"
12920 "private:\n"
12921 " int j;\n"
12922 "};",
12923 "struct foo { /* comment */\n"
12924 "\n"
12925 "private:\n"
12926 " int i;\n"
12927 " // comment\n"
12928 "\n"
12929 "private:\n"
12930 " int j;\n"
12931 "};",
12932 Style);
12933 verifyFormat("struct foo {\n"
12934 "#ifdef FOO\n"
12935 "#endif\n"
12936 "private:\n"
12937 " int i;\n"
12938 "#ifdef FOO\n"
12939 "private:\n"
12940 "#endif\n"
12941 " int j;\n"
12942 "};",
12943 "struct foo {\n"
12944 "#ifdef FOO\n"
12945 "#endif\n"
12946 "\n"
12947 "private:\n"
12948 " int i;\n"
12949 "#ifdef FOO\n"
12950 "\n"
12951 "private:\n"
12952 "#endif\n"
12953 " int j;\n"
12954 "};",
12955 Style);
12956 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12957 verifyFormat("struct foo {\n"
12958 "private:\n"
12959 " void f() {}\n"
12960 "\n"
12961 "private:\n"
12962 " int i;\n"
12963 "\n"
12964 "protected:\n"
12965 " int j;\n"
12966 "};",
12967 Style);
12968 verifyFormat("struct foo {\n"
12969 "private:\n"
12970 " void f() {}\n"
12971 "\n"
12972 "private:\n"
12973 " int i;\n"
12974 "\n"
12975 "protected:\n"
12976 " int j;\n"
12977 "};",
12978 "struct foo {\n"
12979 "private:\n"
12980 " void f() {}\n"
12981 "private:\n"
12982 " int i;\n"
12983 "protected:\n"
12984 " int j;\n"
12985 "};",
12986 Style);
12987 verifyFormat("struct foo { /* comment */\n"
12988 "private:\n"
12989 " int i;\n"
12990 " // comment\n"
12991 "\n"
12992 "private:\n"
12993 " int j;\n"
12994 "};",
12995 Style);
12996 verifyFormat("struct foo {\n"
12997 "#ifdef FOO\n"
12998 "#endif\n"
12999 "\n"
13000 "private:\n"
13001 " int i;\n"
13002 "#ifdef FOO\n"
13003 "\n"
13004 "private:\n"
13005 "#endif\n"
13006 " int j;\n"
13007 "};",
13008 "struct foo {\n"
13009 "#ifdef FOO\n"
13010 "#endif\n"
13011 "private:\n"
13012 " int i;\n"
13013 "#ifdef FOO\n"
13014 "private:\n"
13015 "#endif\n"
13016 " int j;\n"
13017 "};",
13018 Style);
13019 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13020 verifyNoChange("struct foo {\n"
13021 "\n"
13022 "private:\n"
13023 " void f() {}\n"
13024 "\n"
13025 "private:\n"
13026 " int i;\n"
13027 "\n"
13028 "protected:\n"
13029 " int j;\n"
13030 "};",
13031 Style);
13032 verifyFormat("struct foo {\n"
13033 "private:\n"
13034 " void f() {}\n"
13035 "private:\n"
13036 " int i;\n"
13037 "protected:\n"
13038 " int j;\n"
13039 "};",
13040 Style);
13041 verifyNoChange("struct foo { /* comment */\n"
13042 "\n"
13043 "private:\n"
13044 " int i;\n"
13045 " // comment\n"
13046 "\n"
13047 "private:\n"
13048 " int j;\n"
13049 "};",
13050 Style);
13051 verifyFormat("struct foo { /* comment */\n"
13052 "private:\n"
13053 " int i;\n"
13054 " // comment\n"
13055 "private:\n"
13056 " int j;\n"
13057 "};",
13058 Style);
13059 verifyNoChange("struct foo {\n"
13060 "#ifdef FOO\n"
13061 "#endif\n"
13062 "\n"
13063 "private:\n"
13064 " int i;\n"
13065 "#ifdef FOO\n"
13066 "\n"
13067 "private:\n"
13068 "#endif\n"
13069 " int j;\n"
13070 "};",
13071 Style);
13072 verifyFormat("struct foo {\n"
13073 "#ifdef FOO\n"
13074 "#endif\n"
13075 "private:\n"
13076 " int i;\n"
13077 "#ifdef FOO\n"
13078 "private:\n"
13079 "#endif\n"
13080 " int j;\n"
13081 "};",
13082 Style);
13083 Style.AttributeMacros.push_back("FOO");
13084 Style.AttributeMacros.push_back("BAR");
13085 verifyFormat("struct foo {\n"
13086 "FOO private:\n"
13087 " int i;\n"
13088 "BAR(x) protected:\n"
13089 " int j;\n"
13090 "};",
13091 Style);
13093 FormatStyle NoEmptyLines = getLLVMStyle();
13094 NoEmptyLines.MaxEmptyLinesToKeep = 0;
13095 verifyFormat("struct foo {\n"
13096 "private:\n"
13097 " void f() {}\n"
13098 "\n"
13099 "private:\n"
13100 " int i;\n"
13101 "\n"
13102 "public:\n"
13103 "protected:\n"
13104 " int j;\n"
13105 "};",
13106 NoEmptyLines);
13108 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13109 verifyFormat("struct foo {\n"
13110 "private:\n"
13111 " void f() {}\n"
13112 "private:\n"
13113 " int i;\n"
13114 "public:\n"
13115 "protected:\n"
13116 " int j;\n"
13117 "};",
13118 NoEmptyLines);
13120 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13121 verifyFormat("struct foo {\n"
13122 "private:\n"
13123 " void f() {}\n"
13124 "\n"
13125 "private:\n"
13126 " int i;\n"
13127 "\n"
13128 "public:\n"
13129 "\n"
13130 "protected:\n"
13131 " int j;\n"
13132 "};",
13133 NoEmptyLines);
13136 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
13138 FormatStyle Style = getLLVMStyle();
13139 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
13140 verifyFormat("struct foo {\n"
13141 "private:\n"
13142 " void f() {}\n"
13143 "\n"
13144 "private:\n"
13145 " int i;\n"
13146 "\n"
13147 "protected:\n"
13148 " int j;\n"
13149 "};",
13150 Style);
13152 // Check if lines are removed.
13153 verifyFormat("struct foo {\n"
13154 "private:\n"
13155 " void f() {}\n"
13156 "\n"
13157 "private:\n"
13158 " int i;\n"
13159 "\n"
13160 "protected:\n"
13161 " int j;\n"
13162 "};",
13163 "struct foo {\n"
13164 "private:\n"
13165 "\n"
13166 " void f() {}\n"
13167 "\n"
13168 "private:\n"
13169 "\n"
13170 " int i;\n"
13171 "\n"
13172 "protected:\n"
13173 "\n"
13174 " int j;\n"
13175 "};",
13176 Style);
13178 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13179 verifyFormat("struct foo {\n"
13180 "private:\n"
13181 "\n"
13182 " void f() {}\n"
13183 "\n"
13184 "private:\n"
13185 "\n"
13186 " int i;\n"
13187 "\n"
13188 "protected:\n"
13189 "\n"
13190 " int j;\n"
13191 "};",
13192 Style);
13194 // Check if lines are added.
13195 verifyFormat("struct foo {\n"
13196 "private:\n"
13197 "\n"
13198 " void f() {}\n"
13199 "\n"
13200 "private:\n"
13201 "\n"
13202 " int i;\n"
13203 "\n"
13204 "protected:\n"
13205 "\n"
13206 " int j;\n"
13207 "};",
13208 "struct foo {\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);
13220 // Leave tests rely on the code layout, test::messUp can not be used.
13221 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13222 Style.MaxEmptyLinesToKeep = 0u;
13223 verifyFormat("struct foo {\n"
13224 "private:\n"
13225 " void f() {}\n"
13226 "\n"
13227 "private:\n"
13228 " int i;\n"
13229 "\n"
13230 "protected:\n"
13231 " int j;\n"
13232 "};",
13233 Style);
13235 // Check if MaxEmptyLinesToKeep is respected.
13236 verifyFormat("struct foo {\n"
13237 "private:\n"
13238 " void f() {}\n"
13239 "\n"
13240 "private:\n"
13241 " int i;\n"
13242 "\n"
13243 "protected:\n"
13244 " int j;\n"
13245 "};",
13246 "struct foo {\n"
13247 "private:\n"
13248 "\n\n\n"
13249 " void f() {}\n"
13250 "\n"
13251 "private:\n"
13252 "\n\n\n"
13253 " int i;\n"
13254 "\n"
13255 "protected:\n"
13256 "\n\n\n"
13257 " int j;\n"
13258 "};",
13259 Style);
13261 Style.MaxEmptyLinesToKeep = 1u;
13262 verifyNoChange("struct foo {\n"
13263 "private:\n"
13264 "\n"
13265 " void f() {}\n"
13266 "\n"
13267 "private:\n"
13268 "\n"
13269 " int i;\n"
13270 "\n"
13271 "protected:\n"
13272 "\n"
13273 " int j;\n"
13274 "};",
13275 Style);
13276 // Check if no lines are kept.
13277 verifyFormat("struct foo {\n"
13278 "private:\n"
13279 " void f() {}\n"
13280 "\n"
13281 "private:\n"
13282 " int i;\n"
13283 "\n"
13284 "protected:\n"
13285 " int j;\n"
13286 "};",
13287 Style);
13288 // Check if MaxEmptyLinesToKeep is respected.
13289 verifyFormat("struct foo {\n"
13290 "private:\n"
13291 "\n"
13292 " void f() {}\n"
13293 "\n"
13294 "private:\n"
13295 "\n"
13296 " int i;\n"
13297 "\n"
13298 "protected:\n"
13299 "\n"
13300 " int j;\n"
13301 "};",
13302 "struct foo {\n"
13303 "private:\n"
13304 "\n\n\n"
13305 " void f() {}\n"
13306 "\n"
13307 "private:\n"
13308 "\n\n\n"
13309 " int i;\n"
13310 "\n"
13311 "protected:\n"
13312 "\n\n\n"
13313 " int j;\n"
13314 "};",
13315 Style);
13317 Style.MaxEmptyLinesToKeep = 10u;
13318 verifyNoChange("struct foo {\n"
13319 "private:\n"
13320 "\n\n\n"
13321 " void f() {}\n"
13322 "\n"
13323 "private:\n"
13324 "\n\n\n"
13325 " int i;\n"
13326 "\n"
13327 "protected:\n"
13328 "\n\n\n"
13329 " int j;\n"
13330 "};",
13331 Style);
13333 // Test with comments.
13334 Style = getLLVMStyle();
13335 verifyFormat("struct foo {\n"
13336 "private:\n"
13337 " // comment\n"
13338 " void f() {}\n"
13339 "\n"
13340 "private: /* comment */\n"
13341 " int i;\n"
13342 "};",
13343 Style);
13344 verifyFormat("struct foo {\n"
13345 "private:\n"
13346 " // comment\n"
13347 " void f() {}\n"
13348 "\n"
13349 "private: /* comment */\n"
13350 " int i;\n"
13351 "};",
13352 "struct foo {\n"
13353 "private:\n"
13354 "\n"
13355 " // comment\n"
13356 " void f() {}\n"
13357 "\n"
13358 "private: /* comment */\n"
13359 "\n"
13360 " int i;\n"
13361 "};",
13362 Style);
13364 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13365 verifyFormat("struct foo {\n"
13366 "private:\n"
13367 "\n"
13368 " // comment\n"
13369 " void f() {}\n"
13370 "\n"
13371 "private: /* comment */\n"
13372 "\n"
13373 " int i;\n"
13374 "};",
13375 "struct foo {\n"
13376 "private:\n"
13377 " // comment\n"
13378 " void f() {}\n"
13379 "\n"
13380 "private: /* comment */\n"
13381 " int i;\n"
13382 "};",
13383 Style);
13384 verifyFormat("struct foo {\n"
13385 "private:\n"
13386 "\n"
13387 " // comment\n"
13388 " void f() {}\n"
13389 "\n"
13390 "private: /* comment */\n"
13391 "\n"
13392 " int i;\n"
13393 "};",
13394 Style);
13396 // Test with preprocessor defines.
13397 Style = getLLVMStyle();
13398 verifyFormat("struct foo {\n"
13399 "private:\n"
13400 "#ifdef FOO\n"
13401 "#endif\n"
13402 " void f() {}\n"
13403 "};",
13404 Style);
13405 verifyFormat("struct foo {\n"
13406 "private:\n"
13407 "#ifdef FOO\n"
13408 "#endif\n"
13409 " void f() {}\n"
13410 "};",
13411 "struct foo {\n"
13412 "private:\n"
13413 "\n"
13414 "#ifdef FOO\n"
13415 "#endif\n"
13416 " void f() {}\n"
13417 "};",
13418 Style);
13419 verifyNoChange("struct foo {\n"
13420 "#ifdef FOO\n"
13421 "#else\n"
13422 "private:\n"
13423 "\n"
13424 "#endif\n"
13425 "};",
13426 Style);
13427 verifyFormat("struct foo {\n"
13428 "#ifdef FOO\n"
13429 "#else\n"
13430 "private:\n"
13431 "\n"
13432 "#endif\n"
13433 "};",
13434 "struct foo {\n"
13435 "#ifdef FOO\n"
13436 "#else\n"
13437 "private:\n"
13438 "\n"
13439 "\n"
13440 "#endif\n"
13441 "};",
13442 Style);
13443 verifyFormat("struct foo {\n"
13444 "#ifdef FOO\n"
13445 "private:\n"
13446 "#else\n"
13447 "#endif\n"
13448 "};",
13449 "struct foo {\n"
13450 "#ifdef FOO\n"
13451 "private:\n"
13452 "\n"
13453 "\n"
13454 "#else\n"
13455 "#endif\n"
13456 "};",
13457 Style);
13458 verifyFormat("struct foo {\n"
13459 "#if 0\n"
13460 "#else\n"
13461 "#endif\n"
13462 "#ifdef FOO\n"
13463 "private:\n"
13464 "#endif\n"
13465 "};",
13466 "struct foo {\n"
13467 "#if 0\n"
13468 "#else\n"
13469 "#endif\n"
13470 "#ifdef FOO\n"
13471 "private:\n"
13472 "\n"
13473 "\n"
13474 "#endif\n"
13475 "};",
13476 Style);
13478 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13479 verifyFormat("struct foo {\n"
13480 "private:\n"
13481 "\n"
13482 "#ifdef FOO\n"
13483 "#endif\n"
13484 " void f() {}\n"
13485 "};",
13486 "struct foo {\n"
13487 "private:\n"
13488 "#ifdef FOO\n"
13489 "#endif\n"
13490 " void f() {}\n"
13491 "};",
13492 Style);
13493 verifyFormat("struct foo {\n"
13494 "private:\n"
13495 "\n"
13496 "#ifdef FOO\n"
13497 "#endif\n"
13498 " void f() {}\n"
13499 "};",
13500 Style);
13503 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13504 // Combined tests of EmptyLineAfterAccessModifier and
13505 // EmptyLineBeforeAccessModifier.
13506 FormatStyle Style = getLLVMStyle();
13507 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13508 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13509 verifyFormat("struct foo {\n"
13510 "private:\n"
13511 "\n"
13512 "protected:\n"
13513 "};",
13514 Style);
13516 Style.MaxEmptyLinesToKeep = 10u;
13517 // Both remove all new lines.
13518 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13519 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13520 verifyFormat("struct foo {\n"
13521 "private:\n"
13522 "protected:\n"
13523 "};",
13524 "struct foo {\n"
13525 "private:\n"
13526 "\n\n\n"
13527 "protected:\n"
13528 "};",
13529 Style);
13531 // Leave tests rely on the code layout, test::messUp can not be used.
13532 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13533 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13534 Style.MaxEmptyLinesToKeep = 10u;
13535 verifyNoChange("struct foo {\n"
13536 "private:\n"
13537 "\n\n\n"
13538 "protected:\n"
13539 "};",
13540 Style);
13541 Style.MaxEmptyLinesToKeep = 3u;
13542 verifyNoChange("struct foo {\n"
13543 "private:\n"
13544 "\n\n\n"
13545 "protected:\n"
13546 "};",
13547 Style);
13548 Style.MaxEmptyLinesToKeep = 1u;
13549 verifyNoChange("struct foo {\n"
13550 "private:\n"
13551 "\n\n\n"
13552 "protected:\n"
13553 "};",
13554 Style); // Based on new lines in original document and not
13555 // on the setting.
13557 Style.MaxEmptyLinesToKeep = 10u;
13558 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13559 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13560 // Newlines are kept if they are greater than zero,
13561 // test::messUp removes all new lines which changes the logic
13562 verifyNoChange("struct foo {\n"
13563 "private:\n"
13564 "\n\n\n"
13565 "protected:\n"
13566 "};",
13567 Style);
13569 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13570 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13571 // test::messUp removes all new lines which changes the logic
13572 verifyNoChange("struct foo {\n"
13573 "private:\n"
13574 "\n\n\n"
13575 "protected:\n"
13576 "};",
13577 Style);
13579 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13580 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13581 verifyNoChange("struct foo {\n"
13582 "private:\n"
13583 "\n\n\n"
13584 "protected:\n"
13585 "};",
13586 Style); // test::messUp removes all new lines which changes
13587 // the logic.
13589 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13590 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13591 verifyFormat("struct foo {\n"
13592 "private:\n"
13593 "protected:\n"
13594 "};",
13595 "struct foo {\n"
13596 "private:\n"
13597 "\n\n\n"
13598 "protected:\n"
13599 "};",
13600 Style);
13602 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13603 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13604 verifyNoChange("struct foo {\n"
13605 "private:\n"
13606 "\n\n\n"
13607 "protected:\n"
13608 "};",
13609 Style); // test::messUp removes all new lines which changes
13610 // the logic.
13612 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13613 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13614 verifyFormat("struct foo {\n"
13615 "private:\n"
13616 "protected:\n"
13617 "};",
13618 "struct foo {\n"
13619 "private:\n"
13620 "\n\n\n"
13621 "protected:\n"
13622 "};",
13623 Style);
13625 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13626 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13627 verifyFormat("struct foo {\n"
13628 "private:\n"
13629 "protected:\n"
13630 "};",
13631 "struct foo {\n"
13632 "private:\n"
13633 "\n\n\n"
13634 "protected:\n"
13635 "};",
13636 Style);
13638 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13639 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13640 verifyFormat("struct foo {\n"
13641 "private:\n"
13642 "protected:\n"
13643 "};",
13644 "struct foo {\n"
13645 "private:\n"
13646 "\n\n\n"
13647 "protected:\n"
13648 "};",
13649 Style);
13651 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13652 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13653 verifyFormat("struct foo {\n"
13654 "private:\n"
13655 "protected:\n"
13656 "};",
13657 "struct foo {\n"
13658 "private:\n"
13659 "\n\n\n"
13660 "protected:\n"
13661 "};",
13662 Style);
13665 TEST_F(FormatTest, FormatsArrays) {
13666 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13667 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13669 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13670 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13671 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13673 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13674 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13675 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13676 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13677 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13678 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13679 verifyFormat(
13680 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13681 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13682 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13684 " .aaaaaaaaaaaaaaaaaaaaaa();");
13686 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13688 verifyFormat(
13689 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13690 " .aaaaaaa[0]\n"
13691 " .aaaaaaaaaaaaaaaaaaaaaa();");
13692 verifyFormat("a[::b::c];");
13694 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13696 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13697 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13700 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13701 verifyFormat("(a)->b();");
13702 verifyFormat("--a;");
13705 TEST_F(FormatTest, HandlesIncludeDirectives) {
13706 verifyFormat("#include <string>\n"
13707 "#include <a/b/c.h>\n"
13708 "#include \"a/b/string\"\n"
13709 "#include \"string.h\"\n"
13710 "#include \"string.h\"\n"
13711 "#include <a-a>\n"
13712 "#include < path with space >\n"
13713 "#include_next <test.h>"
13714 "#include \"abc.h\" // this is included for ABC\n"
13715 "#include \"some long include\" // with a comment\n"
13716 "#include \"some very long include path\"\n"
13717 "#include <some/very/long/include/path>",
13718 getLLVMStyleWithColumns(35));
13719 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13720 verifyFormat("#include <a>", "#include<a>");
13722 verifyFormat("#import <string>");
13723 verifyFormat("#import <a/b/c.h>");
13724 verifyFormat("#import \"a/b/string\"");
13725 verifyFormat("#import \"string.h\"");
13726 verifyFormat("#import \"string.h\"");
13727 verifyFormat("#if __has_include(<strstream>)\n"
13728 "#include <strstream>\n"
13729 "#endif");
13731 verifyFormat("#define MY_IMPORT <a/b>");
13733 verifyFormat("#if __has_include(<a/b>)");
13734 verifyFormat("#if __has_include_next(<a/b>)");
13735 verifyFormat("#define F __has_include(<a/b>)");
13736 verifyFormat("#define F __has_include_next(<a/b>)");
13738 // Protocol buffer definition or missing "#".
13739 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13740 getLLVMStyleWithColumns(30));
13742 FormatStyle Style = getLLVMStyle();
13743 Style.AlwaysBreakBeforeMultilineStrings = true;
13744 Style.ColumnLimit = 0;
13745 verifyFormat("#import \"abc.h\"", Style);
13747 // But 'import' might also be a regular C++ namespace.
13748 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13750 verifyFormat("import::Bar foo(val ? 2 : 1);");
13753 //===----------------------------------------------------------------------===//
13754 // Error recovery tests.
13755 //===----------------------------------------------------------------------===//
13757 TEST_F(FormatTest, IncompleteParameterLists) {
13758 FormatStyle NoBinPacking = getLLVMStyle();
13759 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
13760 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13761 " double *min_x,\n"
13762 " double *max_x,\n"
13763 " double *min_y,\n"
13764 " double *max_y,\n"
13765 " double *min_z,\n"
13766 " double *max_z, ) {}",
13767 NoBinPacking);
13770 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13771 verifyFormat("void f() { return; }\n42");
13772 verifyFormat("void f() {\n"
13773 " if (0)\n"
13774 " return;\n"
13775 "}\n"
13776 "42");
13777 verifyFormat("void f() { return }\n42");
13778 verifyFormat("void f() {\n"
13779 " if (0)\n"
13780 " return\n"
13781 "}\n"
13782 "42");
13785 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13786 verifyFormat("void f() { return }", "void f ( ) { return }");
13787 verifyFormat("void f() {\n"
13788 " if (a)\n"
13789 " return\n"
13790 "}",
13791 "void f ( ) { if ( a ) return }");
13792 verifyFormat("namespace N {\n"
13793 "void f()\n"
13794 "}",
13795 "namespace N { void f() }");
13796 verifyFormat("namespace N {\n"
13797 "void f() {}\n"
13798 "void g()\n"
13799 "} // namespace N",
13800 "namespace N { void f( ) { } void g( ) }");
13803 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13804 verifyFormat("int aaaaaaaa =\n"
13805 " // Overlylongcomment\n"
13806 " b;",
13807 getLLVMStyleWithColumns(20));
13808 verifyFormat("function(\n"
13809 " ShortArgument,\n"
13810 " LoooooooooooongArgument);",
13811 getLLVMStyleWithColumns(20));
13814 TEST_F(FormatTest, IncorrectAccessSpecifier) {
13815 verifyFormat("public:");
13816 verifyFormat("class A {\n"
13817 "public\n"
13818 " void f() {}\n"
13819 "};");
13820 verifyFormat("public\n"
13821 "int qwerty;");
13822 verifyFormat("public\n"
13823 "B {}");
13824 verifyFormat("public\n"
13825 "{\n"
13826 "}");
13827 verifyFormat("public\n"
13828 "B { int x; }");
13831 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
13832 verifyFormat("{");
13833 verifyFormat("#})");
13834 verifyNoCrash("(/**/[:!] ?[).");
13835 verifyNoCrash("struct X {\n"
13836 " operator iunt(\n"
13837 "};");
13838 verifyNoCrash("struct Foo {\n"
13839 " operator foo(bar\n"
13840 "};");
13843 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
13844 // Found by oss-fuzz:
13845 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13846 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
13847 Style.ColumnLimit = 60;
13848 verifyNoCrash(
13849 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13850 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13851 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13852 Style);
13855 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
13856 verifyFormat("do {\n}");
13857 verifyFormat("do {\n}\n"
13858 "f();");
13859 verifyFormat("do {\n}\n"
13860 "wheeee(fun);");
13861 verifyFormat("do {\n"
13862 " f();\n"
13863 "}");
13866 TEST_F(FormatTest, IncorrectCodeMissingParens) {
13867 verifyFormat("if {\n foo;\n foo();\n}");
13868 verifyFormat("switch {\n foo;\n foo();\n}");
13869 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13870 verifyIncompleteFormat("ERROR: for target;");
13871 verifyFormat("while {\n foo;\n foo();\n}");
13872 verifyFormat("do {\n foo;\n foo();\n} while;");
13875 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
13876 verifyIncompleteFormat("namespace {\n"
13877 "class Foo { Foo (\n"
13878 "};\n"
13879 "} // namespace");
13882 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13883 verifyFormat("{\n"
13884 " {\n"
13885 " }",
13886 "{\n"
13887 "{\n"
13888 "}");
13889 verifyFormat("{\n"
13890 " {\n"
13891 " }",
13892 "{\n"
13893 " {\n"
13894 "}");
13895 verifyFormat("{\n"
13896 " {\n"
13897 " }");
13898 verifyFormat("{\n"
13899 " {\n"
13900 " }\n"
13901 "}\n"
13902 "}",
13903 "{\n"
13904 " {\n"
13905 " }\n"
13906 " }\n"
13907 "}");
13909 verifyFormat("{\n"
13910 " {\n"
13911 " breakme(\n"
13912 " qwe);\n"
13913 " }",
13914 "{\n"
13915 " {\n"
13916 " breakme(qwe);\n"
13917 "}",
13918 getLLVMStyleWithColumns(10));
13921 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
13922 verifyFormat("int x = {\n"
13923 " avariable,\n"
13924 " b(alongervariable)};",
13925 getLLVMStyleWithColumns(25));
13928 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
13929 verifyFormat("return (a)(b){1, 2, 3};");
13932 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
13933 verifyFormat("vector<int> x{1, 2, 3, 4};");
13934 verifyFormat("vector<int> x{\n"
13935 " 1,\n"
13936 " 2,\n"
13937 " 3,\n"
13938 " 4,\n"
13939 "};");
13940 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13941 verifyFormat("f({1, 2});");
13942 verifyFormat("auto v = Foo{-1};");
13943 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13944 verifyFormat("Class::Class : member{1, 2, 3} {}");
13945 verifyFormat("new vector<int>{1, 2, 3};");
13946 verifyFormat("new int[3]{1, 2, 3};");
13947 verifyFormat("new int{1};");
13948 verifyFormat("return {arg1, arg2};");
13949 verifyFormat("return {arg1, SomeType{parameter}};");
13950 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13951 verifyFormat("new T{arg1, arg2};");
13952 verifyFormat("f(MyMap[{composite, key}]);");
13953 verifyFormat("class Class {\n"
13954 " T member = {arg1, arg2};\n"
13955 "};");
13956 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13957 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13958 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13959 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13960 verifyFormat("int a = std::is_integral<int>{} + 0;");
13962 verifyFormat("int foo(int i) { return fo1{}(i); }");
13963 verifyFormat("int foo(int i) { return fo1{}(i); }");
13964 verifyFormat("auto i = decltype(x){};");
13965 verifyFormat("auto i = typeof(x){};");
13966 verifyFormat("auto i = _Atomic(x){};");
13967 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13968 verifyFormat("Node n{1, Node{1000}, //\n"
13969 " 2};");
13970 verifyFormat("Aaaa aaaaaaa{\n"
13971 " {\n"
13972 " aaaa,\n"
13973 " },\n"
13974 "};");
13975 verifyFormat("class C : public D {\n"
13976 " SomeClass SC{2};\n"
13977 "};");
13978 verifyFormat("class C : public A {\n"
13979 " class D : public B {\n"
13980 " void f() { int i{2}; }\n"
13981 " };\n"
13982 "};");
13983 verifyFormat("#define A {a, a},");
13984 // Don't confuse braced list initializers with compound statements.
13985 verifyFormat(
13986 "class A {\n"
13987 " A() : a{} {}\n"
13988 " A() : Base<int>{} {}\n"
13989 " A() : Base<Foo<int>>{} {}\n"
13990 " A(int b) : b(b) {}\n"
13991 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13992 " int a, b;\n"
13993 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13994 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13995 "{}\n"
13996 "};");
13998 // Avoid breaking between equal sign and opening brace
13999 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
14000 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
14001 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
14002 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
14003 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
14004 " {\"ccccccccccccccccccccc\", 2}};",
14005 AvoidBreakingFirstArgument);
14007 // Binpacking only if there is no trailing comma
14008 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
14009 " cccccccccc, dddddddddd};",
14010 getLLVMStyleWithColumns(50));
14011 verifyFormat("const Aaaaaa aaaaa = {\n"
14012 " aaaaaaaaaaa,\n"
14013 " bbbbbbbbbbb,\n"
14014 " ccccccccccc,\n"
14015 " ddddddddddd,\n"
14016 "};",
14017 getLLVMStyleWithColumns(50));
14019 // Cases where distinguising braced lists and blocks is hard.
14020 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
14021 verifyFormat("void f() {\n"
14022 " return; // comment\n"
14023 "}\n"
14024 "SomeType t;");
14025 verifyFormat("void f() {\n"
14026 " if (a) {\n"
14027 " f();\n"
14028 " }\n"
14029 "}\n"
14030 "SomeType t;");
14032 // In combination with BinPackArguments = false.
14033 FormatStyle NoBinPacking = getLLVMStyle();
14034 NoBinPacking.BinPackArguments = false;
14035 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14036 " bbbbb,\n"
14037 " ccccc,\n"
14038 " ddddd,\n"
14039 " eeeee,\n"
14040 " ffffff,\n"
14041 " ggggg,\n"
14042 " hhhhhh,\n"
14043 " iiiiii,\n"
14044 " jjjjjj,\n"
14045 " kkkkkk};",
14046 NoBinPacking);
14047 verifyFormat("const Aaaaaa aaaaa = {\n"
14048 " aaaaa,\n"
14049 " bbbbb,\n"
14050 " ccccc,\n"
14051 " ddddd,\n"
14052 " eeeee,\n"
14053 " ffffff,\n"
14054 " ggggg,\n"
14055 " hhhhhh,\n"
14056 " iiiiii,\n"
14057 " jjjjjj,\n"
14058 " kkkkkk,\n"
14059 "};",
14060 NoBinPacking);
14061 verifyFormat(
14062 "const Aaaaaa aaaaa = {\n"
14063 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
14064 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
14065 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14066 "};",
14067 NoBinPacking);
14069 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14070 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14071 " CDDDP83848_BMCR_REGISTER,\n"
14072 " CDDDP83848_BMSR_REGISTER,\n"
14073 " CDDDP83848_RBR_REGISTER};",
14074 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14075 " CDDDP83848_BMSR_REGISTER,\n"
14076 " CDDDP83848_RBR_REGISTER};",
14077 NoBinPacking);
14079 // FIXME: The alignment of these trailing comments might be bad. Then again,
14080 // this might be utterly useless in real code.
14081 verifyFormat("Constructor::Constructor()\n"
14082 " : some_value{ //\n"
14083 " aaaaaaa, //\n"
14084 " bbbbbbb} {}");
14086 // In braced lists, the first comment is always assumed to belong to the
14087 // first element. Thus, it can be moved to the next or previous line as
14088 // appropriate.
14089 verifyFormat("function({// First element:\n"
14090 " 1,\n"
14091 " // Second element:\n"
14092 " 2});",
14093 "function({\n"
14094 " // First element:\n"
14095 " 1,\n"
14096 " // Second element:\n"
14097 " 2});");
14098 verifyFormat("std::vector<int> MyNumbers{\n"
14099 " // First element:\n"
14100 " 1,\n"
14101 " // Second element:\n"
14102 " 2};",
14103 "std::vector<int> MyNumbers{// First element:\n"
14104 " 1,\n"
14105 " // Second element:\n"
14106 " 2};",
14107 getLLVMStyleWithColumns(30));
14108 // A trailing comma should still lead to an enforced line break and no
14109 // binpacking.
14110 verifyFormat("vector<int> SomeVector = {\n"
14111 " // aaa\n"
14112 " 1,\n"
14113 " 2,\n"
14114 "};",
14115 "vector<int> SomeVector = { // aaa\n"
14116 " 1, 2, };");
14118 // C++11 brace initializer list l-braces should not be treated any differently
14119 // when breaking before lambda bodies is enabled
14120 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
14121 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
14122 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
14123 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
14124 verifyFormat(
14125 "std::runtime_error{\n"
14126 " \"Long string which will force a break onto the next line...\"};",
14127 BreakBeforeLambdaBody);
14129 FormatStyle ExtraSpaces = getLLVMStyle();
14130 ExtraSpaces.Cpp11BracedListStyle = false;
14131 ExtraSpaces.ColumnLimit = 75;
14132 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
14133 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
14134 verifyFormat("f({ 1, 2 });", ExtraSpaces);
14135 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
14136 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
14137 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
14138 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
14139 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
14140 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
14141 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
14142 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
14143 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
14144 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
14145 verifyFormat("class Class {\n"
14146 " T member = { arg1, arg2 };\n"
14147 "};",
14148 ExtraSpaces);
14149 verifyFormat(
14150 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14151 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14152 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14153 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14154 ExtraSpaces);
14155 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
14156 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14157 ExtraSpaces);
14158 verifyFormat(
14159 "someFunction(OtherParam,\n"
14160 " BracedList{ // comment 1 (Forcing interesting break)\n"
14161 " param1, param2,\n"
14162 " // comment 2\n"
14163 " param3, param4 });",
14164 ExtraSpaces);
14165 verifyFormat(
14166 "std::this_thread::sleep_for(\n"
14167 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14168 ExtraSpaces);
14169 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14170 " aaaaaaa,\n"
14171 " aaaaaaaaaa,\n"
14172 " aaaaa,\n"
14173 " aaaaaaaaaaaaaaa,\n"
14174 " aaa,\n"
14175 " aaaaaaaaaa,\n"
14176 " a,\n"
14177 " aaaaaaaaaaaaaaaaaaaaa,\n"
14178 " aaaaaaaaaaaa,\n"
14179 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14180 " aaaaaaa,\n"
14181 " a};");
14182 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
14183 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
14184 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
14186 // Avoid breaking between initializer/equal sign and opening brace
14187 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
14188 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14189 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14190 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14191 " { \"ccccccccccccccccccccc\", 2 }\n"
14192 "};",
14193 ExtraSpaces);
14194 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14195 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14196 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14197 " { \"ccccccccccccccccccccc\", 2 }\n"
14198 "};",
14199 ExtraSpaces);
14201 FormatStyle SpaceBeforeBrace = getLLVMStyle();
14202 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
14203 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
14204 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
14206 FormatStyle SpaceBetweenBraces = getLLVMStyle();
14207 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
14208 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14209 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
14210 SpaceBetweenBraces.SpacesInSquareBrackets = true;
14211 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
14212 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
14213 verifyFormat("vector< int > x{ // comment 1\n"
14214 " 1, 2, 3, 4 };",
14215 SpaceBetweenBraces);
14216 SpaceBetweenBraces.ColumnLimit = 20;
14217 verifyFormat("vector< int > x{\n"
14218 " 1, 2, 3, 4 };",
14219 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14220 SpaceBetweenBraces.ColumnLimit = 24;
14221 verifyFormat("vector< int > x{ 1, 2,\n"
14222 " 3, 4 };",
14223 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14224 verifyFormat("vector< int > x{\n"
14225 " 1,\n"
14226 " 2,\n"
14227 " 3,\n"
14228 " 4,\n"
14229 "};",
14230 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14231 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14232 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14233 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14234 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14237 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14238 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14239 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14240 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14241 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14242 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14243 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14244 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14245 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14246 " 1, 22, 333, 4444, 55555, //\n"
14247 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14248 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14249 verifyFormat(
14250 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14251 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14252 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14253 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14254 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14255 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14256 " 7777777};");
14257 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14258 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14259 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14260 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14261 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14262 " // Separating comment.\n"
14263 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14264 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14265 " // Leading comment\n"
14266 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14267 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14268 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14269 " 1, 1, 1, 1};",
14270 getLLVMStyleWithColumns(39));
14271 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14272 " 1, 1, 1, 1};",
14273 getLLVMStyleWithColumns(38));
14274 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14275 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14276 getLLVMStyleWithColumns(43));
14277 verifyFormat(
14278 "static unsigned SomeValues[10][3] = {\n"
14279 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14280 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14281 verifyFormat("static auto fields = new vector<string>{\n"
14282 " \"aaaaaaaaaaaaa\",\n"
14283 " \"aaaaaaaaaaaaa\",\n"
14284 " \"aaaaaaaaaaaa\",\n"
14285 " \"aaaaaaaaaaaaaa\",\n"
14286 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14287 " \"aaaaaaaaaaaa\",\n"
14288 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14289 "};");
14290 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14291 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14292 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14293 " 3, cccccccccccccccccccccc};",
14294 getLLVMStyleWithColumns(60));
14296 // Trailing commas.
14297 verifyFormat("vector<int> x = {\n"
14298 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14299 "};",
14300 getLLVMStyleWithColumns(39));
14301 verifyFormat("vector<int> x = {\n"
14302 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14303 "};",
14304 getLLVMStyleWithColumns(39));
14305 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14306 " 1, 1, 1, 1,\n"
14307 " /**/ /**/};",
14308 getLLVMStyleWithColumns(39));
14310 // Trailing comment in the first line.
14311 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14312 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14313 " 111111111, 222222222, 3333333333, 444444444, //\n"
14314 " 11111111, 22222222, 333333333, 44444444};");
14315 // Trailing comment in the last line.
14316 verifyFormat("int aaaaa[] = {\n"
14317 " 1, 2, 3, // comment\n"
14318 " 4, 5, 6 // comment\n"
14319 "};");
14321 // With nested lists, we should either format one item per line or all nested
14322 // lists one on line.
14323 // FIXME: For some nested lists, we can do better.
14324 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14325 " {aaaaaaaaaaaaaaaaaaa},\n"
14326 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14327 " {aaaaaaaaaaaaaaaaa}};",
14328 getLLVMStyleWithColumns(60));
14329 verifyFormat(
14330 "SomeStruct my_struct_array = {\n"
14331 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14332 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14333 " {aaa, aaa},\n"
14334 " {aaa, aaa},\n"
14335 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14336 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14337 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14339 // No column layout should be used here.
14340 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14341 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14343 verifyNoCrash("a<,");
14345 // No braced initializer here.
14346 verifyFormat("void f() {\n"
14347 " struct Dummy {};\n"
14348 " f(v);\n"
14349 "}");
14350 verifyFormat("void foo() {\n"
14351 " { // asdf\n"
14352 " {\n"
14353 " int a;\n"
14354 " }\n"
14355 " }\n"
14356 " {\n"
14357 " {\n"
14358 " int b;\n"
14359 " }\n"
14360 " }\n"
14361 "}");
14362 verifyFormat("namespace n {\n"
14363 "void foo() {\n"
14364 " {\n"
14365 " {\n"
14366 " statement();\n"
14367 " if (false) {\n"
14368 " }\n"
14369 " }\n"
14370 " }\n"
14371 " {\n"
14372 " }\n"
14373 "}\n"
14374 "} // namespace n");
14376 // Long lists should be formatted in columns even if they are nested.
14377 verifyFormat(
14378 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14379 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14380 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14381 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14382 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14383 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14385 // Allow "single-column" layout even if that violates the column limit. There
14386 // isn't going to be a better way.
14387 verifyFormat("std::vector<int> a = {\n"
14388 " aaaaaaaa,\n"
14389 " aaaaaaaa,\n"
14390 " aaaaaaaa,\n"
14391 " aaaaaaaa,\n"
14392 " aaaaaaaaaa,\n"
14393 " aaaaaaaa,\n"
14394 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14395 getLLVMStyleWithColumns(30));
14396 verifyFormat("vector<int> aaaa = {\n"
14397 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14398 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14399 " aaaaaa.aaaaaaa,\n"
14400 " aaaaaa.aaaaaaa,\n"
14401 " aaaaaa.aaaaaaa,\n"
14402 " aaaaaa.aaaaaaa,\n"
14403 "};");
14405 // Don't create hanging lists.
14406 verifyFormat("someFunction(Param, {List1, List2,\n"
14407 " List3});",
14408 getLLVMStyleWithColumns(35));
14409 verifyFormat("someFunction(Param, Param,\n"
14410 " {List1, List2,\n"
14411 " List3});",
14412 getLLVMStyleWithColumns(35));
14413 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14414 " aaaaaaaaaaaaaaaaaaaaaaa);");
14416 // No possible column formats, don't want the optimal paths penalized.
14417 verifyFormat(
14418 "waarudo::unit desk = {\n"
14419 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14420 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14421 "Output{1, 2}; },\n"
14422 " [](const Input &i) -> Output { return "
14423 "Output{1, 2}; });");
14424 FormatStyle NoBinPacking = getLLVMStyle();
14425 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
14426 verifyFormat("waarudo::unit desk = {\n"
14427 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14428 "1, 1} * w::m; }};",
14429 NoBinPacking);
14432 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14433 FormatStyle DoNotMerge = getLLVMStyle();
14434 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14436 verifyFormat("void f() { return 42; }");
14437 verifyFormat("void f() {\n"
14438 " return 42;\n"
14439 "}",
14440 DoNotMerge);
14441 verifyFormat("void f() {\n"
14442 " // Comment\n"
14443 "}");
14444 verifyFormat("{\n"
14445 "#error {\n"
14446 " int a;\n"
14447 "}");
14448 verifyFormat("{\n"
14449 " int a;\n"
14450 "#error {\n"
14451 "}");
14452 verifyFormat("void f() {} // comment");
14453 verifyFormat("void f() { int a; } // comment");
14454 verifyFormat("void f() {\n"
14455 "} // comment",
14456 DoNotMerge);
14457 verifyFormat("void f() {\n"
14458 " int a;\n"
14459 "} // comment",
14460 DoNotMerge);
14461 verifyFormat("void f() {\n"
14462 "} // comment",
14463 getLLVMStyleWithColumns(15));
14465 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14466 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14468 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14469 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14470 verifyGoogleFormat("class C {\n"
14471 " C()\n"
14472 " : iiiiiiii(nullptr),\n"
14473 " kkkkkkk(nullptr),\n"
14474 " mmmmmmm(nullptr),\n"
14475 " nnnnnnn(nullptr) {}\n"
14476 "};");
14478 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
14479 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
14480 verifyFormat("class C {\n"
14481 " A() : b(0) {}\n"
14482 "};",
14483 "class C{A():b(0){}};", NoColumnLimit);
14484 verifyFormat("A()\n"
14485 " : b(0) {\n"
14486 "}",
14487 "A()\n:b(0)\n{\n}", NoColumnLimit);
14489 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
14490 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
14491 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
14492 verifyFormat("class C {\n"
14493 "#pragma foo\n"
14494 " int foo { return 0; }\n"
14495 "};",
14496 NoColumnLimitWrapAfterFunction);
14497 verifyFormat("class C {\n"
14498 "#pragma foo\n"
14499 " void foo {}\n"
14500 "};",
14501 NoColumnLimitWrapAfterFunction);
14503 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
14504 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
14505 FormatStyle::SFS_None;
14506 verifyFormat("A()\n"
14507 " : b(0) {\n"
14508 "}",
14509 "A():b(0){}", DoNotMergeNoColumnLimit);
14510 verifyFormat("A()\n"
14511 " : b(0) {\n"
14512 "}",
14513 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
14515 verifyFormat("#define A \\\n"
14516 " void f() { \\\n"
14517 " int i; \\\n"
14518 " }",
14519 getLLVMStyleWithColumns(20));
14520 verifyFormat("#define A \\\n"
14521 " void f() { int i; }",
14522 getLLVMStyleWithColumns(21));
14523 verifyFormat("#define A \\\n"
14524 " void f() { \\\n"
14525 " int i; \\\n"
14526 " } \\\n"
14527 " int j;",
14528 getLLVMStyleWithColumns(22));
14529 verifyFormat("#define A \\\n"
14530 " void f() { int i; } \\\n"
14531 " int j;",
14532 getLLVMStyleWithColumns(23));
14534 verifyFormat(
14535 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14536 " aaaaaaaaaaaaaaaaaa,\n"
14537 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14539 constexpr StringRef Code{"void foo() { /* Empty */ }"};
14540 verifyFormat(Code);
14541 verifyFormat(Code, "void foo() { /* Empty */\n"
14542 "}");
14543 verifyFormat(Code, "void foo() {\n"
14544 "/* Empty */\n"
14545 "}");
14548 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
14549 FormatStyle MergeEmptyOnly = getLLVMStyle();
14550 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14551 verifyFormat("class C {\n"
14552 " int f() {}\n"
14553 "};",
14554 MergeEmptyOnly);
14555 verifyFormat("class C {\n"
14556 " int f() {\n"
14557 " return 42;\n"
14558 " }\n"
14559 "};",
14560 MergeEmptyOnly);
14561 verifyFormat("int f() {}", MergeEmptyOnly);
14562 verifyFormat("int f() {\n"
14563 " return 42;\n"
14564 "}",
14565 MergeEmptyOnly);
14567 // Also verify behavior when BraceWrapping.AfterFunction = true
14568 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14569 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
14570 verifyFormat("int f() {}", MergeEmptyOnly);
14571 verifyFormat("class C {\n"
14572 " int f() {}\n"
14573 "};",
14574 MergeEmptyOnly);
14577 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
14578 FormatStyle MergeInlineOnly = getLLVMStyle();
14579 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14580 verifyFormat("class C {\n"
14581 " int f() { return 42; }\n"
14582 "};",
14583 MergeInlineOnly);
14584 verifyFormat("int f() {\n"
14585 " return 42;\n"
14586 "}",
14587 MergeInlineOnly);
14589 // SFS_Inline implies SFS_Empty
14590 verifyFormat("class C {\n"
14591 " int f() {}\n"
14592 "};",
14593 MergeInlineOnly);
14594 verifyFormat("int f() {}", MergeInlineOnly);
14595 // https://llvm.org/PR54147
14596 verifyFormat("auto lambda = []() {\n"
14597 " // comment\n"
14598 " f();\n"
14599 " g();\n"
14600 "};",
14601 MergeInlineOnly);
14603 verifyFormat("class C {\n"
14604 "#ifdef A\n"
14605 " int f() { return 42; }\n"
14606 "#endif\n"
14607 "};",
14608 MergeInlineOnly);
14610 verifyFormat("struct S {\n"
14611 "// comment\n"
14612 "#ifdef FOO\n"
14613 " int foo() { bar(); }\n"
14614 "#endif\n"
14615 "};",
14616 MergeInlineOnly);
14618 // Also verify behavior when BraceWrapping.AfterFunction = true
14619 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14620 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14621 verifyFormat("class C {\n"
14622 " int f() { return 42; }\n"
14623 "};",
14624 MergeInlineOnly);
14625 verifyFormat("int f()\n"
14626 "{\n"
14627 " return 42;\n"
14628 "}",
14629 MergeInlineOnly);
14631 // SFS_Inline implies SFS_Empty
14632 verifyFormat("int f() {}", MergeInlineOnly);
14633 verifyFormat("class C {\n"
14634 " int f() {}\n"
14635 "};",
14636 MergeInlineOnly);
14638 MergeInlineOnly.BraceWrapping.AfterClass = true;
14639 MergeInlineOnly.BraceWrapping.AfterStruct = true;
14640 verifyFormat("class C\n"
14641 "{\n"
14642 " int f() { return 42; }\n"
14643 "};",
14644 MergeInlineOnly);
14645 verifyFormat("struct C\n"
14646 "{\n"
14647 " int f() { return 42; }\n"
14648 "};",
14649 MergeInlineOnly);
14650 verifyFormat("int f()\n"
14651 "{\n"
14652 " return 42;\n"
14653 "}",
14654 MergeInlineOnly);
14655 verifyFormat("int f() {}", MergeInlineOnly);
14656 verifyFormat("class C\n"
14657 "{\n"
14658 " int f() { return 42; }\n"
14659 "};",
14660 MergeInlineOnly);
14661 verifyFormat("struct C\n"
14662 "{\n"
14663 " int f() { return 42; }\n"
14664 "};",
14665 MergeInlineOnly);
14666 verifyFormat("struct C\n"
14667 "// comment\n"
14668 "/* comment */\n"
14669 "// comment\n"
14670 "{\n"
14671 " int f() { return 42; }\n"
14672 "};",
14673 MergeInlineOnly);
14674 verifyFormat("/* comment */ struct C\n"
14675 "{\n"
14676 " int f() { return 42; }\n"
14677 "};",
14678 MergeInlineOnly);
14681 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14682 FormatStyle MergeInlineOnly = getLLVMStyle();
14683 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14684 FormatStyle::SFS_InlineOnly;
14685 verifyFormat("class C {\n"
14686 " int f() { return 42; }\n"
14687 "};",
14688 MergeInlineOnly);
14689 verifyFormat("int f() {\n"
14690 " return 42;\n"
14691 "}",
14692 MergeInlineOnly);
14694 // SFS_InlineOnly does not imply SFS_Empty
14695 verifyFormat("class C {\n"
14696 " int f() {}\n"
14697 "};",
14698 MergeInlineOnly);
14699 verifyFormat("int f() {\n"
14700 "}",
14701 MergeInlineOnly);
14703 // Also verify behavior when BraceWrapping.AfterFunction = true
14704 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14705 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14706 verifyFormat("class C {\n"
14707 " int f() { return 42; }\n"
14708 "};",
14709 MergeInlineOnly);
14710 verifyFormat("int f()\n"
14711 "{\n"
14712 " return 42;\n"
14713 "}",
14714 MergeInlineOnly);
14716 // SFS_InlineOnly does not imply SFS_Empty
14717 verifyFormat("int f()\n"
14718 "{\n"
14719 "}",
14720 MergeInlineOnly);
14721 verifyFormat("class C {\n"
14722 " int f() {}\n"
14723 "};",
14724 MergeInlineOnly);
14727 TEST_F(FormatTest, SplitEmptyFunction) {
14728 FormatStyle Style = getLLVMStyleWithColumns(40);
14729 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14730 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14731 Style.BraceWrapping.AfterFunction = true;
14732 Style.BraceWrapping.SplitEmptyFunction = false;
14734 verifyFormat("int f()\n"
14735 "{}",
14736 Style);
14737 verifyFormat("int f()\n"
14738 "{\n"
14739 " return 42;\n"
14740 "}",
14741 Style);
14742 verifyFormat("int f()\n"
14743 "{\n"
14744 " // some comment\n"
14745 "}",
14746 Style);
14748 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14749 verifyFormat("int f() {}", Style);
14750 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14751 "{}",
14752 Style);
14753 verifyFormat("int f()\n"
14754 "{\n"
14755 " return 0;\n"
14756 "}",
14757 Style);
14759 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14760 verifyFormat("class Foo {\n"
14761 " int f() {}\n"
14762 "};",
14763 Style);
14764 verifyFormat("class Foo {\n"
14765 " int f() { return 0; }\n"
14766 "};",
14767 Style);
14768 verifyFormat("class Foo {\n"
14769 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14770 " {}\n"
14771 "};",
14772 Style);
14773 verifyFormat("class Foo {\n"
14774 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14775 " {\n"
14776 " return 0;\n"
14777 " }\n"
14778 "};",
14779 Style);
14781 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14782 verifyFormat("int f() {}", Style);
14783 verifyFormat("int f() { return 0; }", Style);
14784 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14785 "{}",
14786 Style);
14787 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14788 "{\n"
14789 " return 0;\n"
14790 "}",
14791 Style);
14794 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
14795 FormatStyle Style = getLLVMStyleWithColumns(40);
14796 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14797 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14798 Style.BraceWrapping.AfterFunction = true;
14799 Style.BraceWrapping.SplitEmptyFunction = true;
14800 Style.BraceWrapping.SplitEmptyRecord = false;
14802 verifyFormat("class C {};", Style);
14803 verifyFormat("struct C {};", Style);
14804 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14805 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14806 "{\n"
14807 "}",
14808 Style);
14809 verifyFormat("class C {\n"
14810 " C()\n"
14811 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14812 " bbbbbbbbbbbbbbbbbbb()\n"
14813 " {\n"
14814 " }\n"
14815 " void\n"
14816 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14817 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14818 " {\n"
14819 " }\n"
14820 "};",
14821 Style);
14824 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
14825 FormatStyle Style = getLLVMStyle();
14826 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14827 verifyFormat("#ifdef A\n"
14828 "int f() {}\n"
14829 "#else\n"
14830 "int g() {}\n"
14831 "#endif",
14832 Style);
14835 TEST_F(FormatTest, SplitEmptyClass) {
14836 FormatStyle Style = getLLVMStyle();
14837 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14838 Style.BraceWrapping.AfterClass = true;
14839 Style.BraceWrapping.SplitEmptyRecord = false;
14841 verifyFormat("class Foo\n"
14842 "{};",
14843 Style);
14844 verifyFormat("/* something */ class Foo\n"
14845 "{};",
14846 Style);
14847 verifyFormat("template <typename X> class Foo\n"
14848 "{};",
14849 Style);
14850 verifyFormat("class Foo\n"
14851 "{\n"
14852 " Foo();\n"
14853 "};",
14854 Style);
14855 verifyFormat("typedef class Foo\n"
14856 "{\n"
14857 "} Foo_t;",
14858 Style);
14860 Style.BraceWrapping.SplitEmptyRecord = true;
14861 Style.BraceWrapping.AfterStruct = true;
14862 verifyFormat("class rep\n"
14863 "{\n"
14864 "};",
14865 Style);
14866 verifyFormat("struct rep\n"
14867 "{\n"
14868 "};",
14869 Style);
14870 verifyFormat("template <typename T> class rep\n"
14871 "{\n"
14872 "};",
14873 Style);
14874 verifyFormat("template <typename T> struct rep\n"
14875 "{\n"
14876 "};",
14877 Style);
14878 verifyFormat("class rep\n"
14879 "{\n"
14880 " int x;\n"
14881 "};",
14882 Style);
14883 verifyFormat("struct rep\n"
14884 "{\n"
14885 " int x;\n"
14886 "};",
14887 Style);
14888 verifyFormat("template <typename T> class rep\n"
14889 "{\n"
14890 " int x;\n"
14891 "};",
14892 Style);
14893 verifyFormat("template <typename T> struct rep\n"
14894 "{\n"
14895 " int x;\n"
14896 "};",
14897 Style);
14898 verifyFormat("template <typename T> class rep // Foo\n"
14899 "{\n"
14900 " int x;\n"
14901 "};",
14902 Style);
14903 verifyFormat("template <typename T> struct rep // Bar\n"
14904 "{\n"
14905 " int x;\n"
14906 "};",
14907 Style);
14909 verifyFormat("template <typename T> class rep<T>\n"
14910 "{\n"
14911 " int x;\n"
14912 "};",
14913 Style);
14915 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14916 "{\n"
14917 " int x;\n"
14918 "};",
14919 Style);
14920 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14921 "{\n"
14922 "};",
14923 Style);
14925 verifyFormat("#include \"stdint.h\"\n"
14926 "namespace rep {}",
14927 Style);
14928 verifyFormat("#include <stdint.h>\n"
14929 "namespace rep {}",
14930 Style);
14931 verifyFormat("#include <stdint.h>\n"
14932 "namespace rep {}",
14933 "#include <stdint.h>\n"
14934 "namespace rep {\n"
14935 "\n"
14936 "\n"
14937 "}",
14938 Style);
14941 TEST_F(FormatTest, SplitEmptyStruct) {
14942 FormatStyle Style = getLLVMStyle();
14943 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14944 Style.BraceWrapping.AfterStruct = true;
14945 Style.BraceWrapping.SplitEmptyRecord = false;
14947 verifyFormat("struct Foo\n"
14948 "{};",
14949 Style);
14950 verifyFormat("/* something */ struct Foo\n"
14951 "{};",
14952 Style);
14953 verifyFormat("template <typename X> struct Foo\n"
14954 "{};",
14955 Style);
14956 verifyFormat("struct Foo\n"
14957 "{\n"
14958 " Foo();\n"
14959 "};",
14960 Style);
14961 verifyFormat("typedef struct Foo\n"
14962 "{\n"
14963 "} Foo_t;",
14964 Style);
14965 // typedef struct Bar {} Bar_t;
14968 TEST_F(FormatTest, SplitEmptyUnion) {
14969 FormatStyle Style = getLLVMStyle();
14970 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14971 Style.BraceWrapping.AfterUnion = true;
14972 Style.BraceWrapping.SplitEmptyRecord = false;
14974 verifyFormat("union Foo\n"
14975 "{};",
14976 Style);
14977 verifyFormat("/* something */ union Foo\n"
14978 "{};",
14979 Style);
14980 verifyFormat("union Foo\n"
14981 "{\n"
14982 " A,\n"
14983 "};",
14984 Style);
14985 verifyFormat("typedef union Foo\n"
14986 "{\n"
14987 "} Foo_t;",
14988 Style);
14991 TEST_F(FormatTest, SplitEmptyNamespace) {
14992 FormatStyle Style = getLLVMStyle();
14993 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14994 Style.BraceWrapping.AfterNamespace = true;
14995 Style.BraceWrapping.SplitEmptyNamespace = false;
14997 verifyFormat("namespace Foo\n"
14998 "{};",
14999 Style);
15000 verifyFormat("/* something */ namespace Foo\n"
15001 "{};",
15002 Style);
15003 verifyFormat("inline namespace Foo\n"
15004 "{};",
15005 Style);
15006 verifyFormat("/* something */ inline namespace Foo\n"
15007 "{};",
15008 Style);
15009 verifyFormat("export namespace Foo\n"
15010 "{};",
15011 Style);
15012 verifyFormat("namespace Foo\n"
15013 "{\n"
15014 "void Bar();\n"
15015 "};",
15016 Style);
15019 TEST_F(FormatTest, NeverMergeShortRecords) {
15020 FormatStyle Style = getLLVMStyle();
15022 verifyFormat("class Foo {\n"
15023 " Foo();\n"
15024 "};",
15025 Style);
15026 verifyFormat("typedef class Foo {\n"
15027 " Foo();\n"
15028 "} Foo_t;",
15029 Style);
15030 verifyFormat("struct Foo {\n"
15031 " Foo();\n"
15032 "};",
15033 Style);
15034 verifyFormat("typedef struct Foo {\n"
15035 " Foo();\n"
15036 "} Foo_t;",
15037 Style);
15038 verifyFormat("union Foo {\n"
15039 " A,\n"
15040 "};",
15041 Style);
15042 verifyFormat("typedef union Foo {\n"
15043 " A,\n"
15044 "} Foo_t;",
15045 Style);
15046 verifyFormat("namespace Foo {\n"
15047 "void Bar();\n"
15048 "};",
15049 Style);
15051 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15052 Style.BraceWrapping.AfterClass = true;
15053 Style.BraceWrapping.AfterStruct = true;
15054 Style.BraceWrapping.AfterUnion = true;
15055 Style.BraceWrapping.AfterNamespace = true;
15056 verifyFormat("class Foo\n"
15057 "{\n"
15058 " Foo();\n"
15059 "};",
15060 Style);
15061 verifyFormat("typedef class Foo\n"
15062 "{\n"
15063 " Foo();\n"
15064 "} Foo_t;",
15065 Style);
15066 verifyFormat("struct Foo\n"
15067 "{\n"
15068 " Foo();\n"
15069 "};",
15070 Style);
15071 verifyFormat("typedef struct Foo\n"
15072 "{\n"
15073 " Foo();\n"
15074 "} Foo_t;",
15075 Style);
15076 verifyFormat("union Foo\n"
15077 "{\n"
15078 " A,\n"
15079 "};",
15080 Style);
15081 verifyFormat("typedef union Foo\n"
15082 "{\n"
15083 " A,\n"
15084 "} Foo_t;",
15085 Style);
15086 verifyFormat("namespace Foo\n"
15087 "{\n"
15088 "void Bar();\n"
15089 "};",
15090 Style);
15093 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
15094 // Elaborate type variable declarations.
15095 verifyFormat("struct foo a = {bar};\nint n;");
15096 verifyFormat("class foo a = {bar};\nint n;");
15097 verifyFormat("union foo a = {bar};\nint n;");
15099 // Elaborate types inside function definitions.
15100 verifyFormat("struct foo f() {}\nint n;");
15101 verifyFormat("class foo f() {}\nint n;");
15102 verifyFormat("union foo f() {}\nint n;");
15104 // Templates.
15105 verifyFormat("template <class X> void f() {}\nint n;");
15106 verifyFormat("template <struct X> void f() {}\nint n;");
15107 verifyFormat("template <union X> void f() {}\nint n;");
15109 // Actual definitions...
15110 verifyFormat("struct {\n} n;");
15111 verifyFormat(
15112 "template <template <class T, class Y>, class Z> class X {\n} n;");
15113 verifyFormat("union Z {\n int n;\n} x;");
15114 verifyFormat("class MACRO Z {\n} n;");
15115 verifyFormat("class MACRO(X) Z {\n} n;");
15116 verifyFormat("class __attribute__((X)) Z {\n} n;");
15117 verifyFormat("class __declspec(X) Z {\n} n;");
15118 verifyFormat("class A##B##C {\n} n;");
15119 verifyFormat("class alignas(16) Z {\n} n;");
15120 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15121 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15123 // Redefinition from nested context:
15124 verifyFormat("class A::B::C {\n} n;");
15126 // Template definitions.
15127 verifyFormat(
15128 "template <typename F>\n"
15129 "Matcher(const Matcher<F> &Other,\n"
15130 " typename enable_if_c<is_base_of<F, T>::value &&\n"
15131 " !is_same<F, T>::value>::type * = 0)\n"
15132 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15134 // FIXME: This is still incorrectly handled at the formatter side.
15135 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15136 verifyFormat("int i = SomeFunction(a<b, a> b);");
15138 verifyFormat("class A<int> f() {}\n"
15139 "int n;");
15140 verifyFormat("template <typename T> class A<T> f() {}\n"
15141 "int n;");
15143 verifyFormat("template <> class Foo<int> F() {\n"
15144 "} n;");
15146 // Elaborate types where incorrectly parsing the structural element would
15147 // break the indent.
15148 verifyFormat("if (true)\n"
15149 " class X x;\n"
15150 "else\n"
15151 " f();");
15153 // This is simply incomplete. Formatting is not important, but must not crash.
15154 verifyFormat("class A:");
15157 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
15158 verifyNoChange("#error Leave all white!!!!! space* alone!");
15159 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15160 verifyFormat("#error 1", " # error 1");
15161 verifyFormat("#warning 1", " # warning 1");
15164 TEST_F(FormatTest, FormatHashIfExpressions) {
15165 verifyFormat("#if AAAA && BBBB");
15166 verifyFormat("#if (AAAA && BBBB)");
15167 verifyFormat("#elif (AAAA && BBBB)");
15168 // FIXME: Come up with a better indentation for #elif.
15169 verifyFormat(
15170 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15171 " defined(BBBBBBBB)\n"
15172 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15173 " defined(BBBBBBBB)\n"
15174 "#endif",
15175 getLLVMStyleWithColumns(65));
15178 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
15179 FormatStyle AllowsMergedIf = getGoogleStyle();
15180 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
15181 FormatStyle::SIS_WithoutElse;
15182 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
15183 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
15184 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
15185 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
15186 FormatStyle ShortMergedIf = AllowsMergedIf;
15187 ShortMergedIf.ColumnLimit = 25;
15188 verifyFormat("#define A \\\n"
15189 " if (true) return 42;",
15190 ShortMergedIf);
15191 verifyFormat("#define A \\\n"
15192 " f(); \\\n"
15193 " if (true)\n"
15194 "#define B",
15195 ShortMergedIf);
15196 verifyFormat("#define A \\\n"
15197 " f(); \\\n"
15198 " if (true)\n"
15199 "g();",
15200 ShortMergedIf);
15201 verifyFormat("{\n"
15202 "#ifdef A\n"
15203 " // Comment\n"
15204 " if (true) continue;\n"
15205 "#endif\n"
15206 " // Comment\n"
15207 " if (true) continue;\n"
15208 "}",
15209 ShortMergedIf);
15210 ShortMergedIf.ColumnLimit = 33;
15211 verifyFormat("#define A \\\n"
15212 " if constexpr (true) return 42;",
15213 ShortMergedIf);
15214 verifyFormat("#define A \\\n"
15215 " if CONSTEXPR (true) return 42;",
15216 ShortMergedIf);
15217 ShortMergedIf.ColumnLimit = 29;
15218 verifyFormat("#define A \\\n"
15219 " if (aaaaaaaaaa) return 1; \\\n"
15220 " return 2;",
15221 ShortMergedIf);
15222 ShortMergedIf.ColumnLimit = 28;
15223 verifyFormat("#define A \\\n"
15224 " if (aaaaaaaaaa) \\\n"
15225 " return 1; \\\n"
15226 " return 2;",
15227 ShortMergedIf);
15228 verifyFormat("#define A \\\n"
15229 " if constexpr (aaaaaaa) \\\n"
15230 " return 1; \\\n"
15231 " return 2;",
15232 ShortMergedIf);
15233 verifyFormat("#define A \\\n"
15234 " if CONSTEXPR (aaaaaaa) \\\n"
15235 " return 1; \\\n"
15236 " return 2;",
15237 ShortMergedIf);
15239 verifyFormat("//\n"
15240 "#define a \\\n"
15241 " if \\\n"
15242 " 0",
15243 getChromiumStyle(FormatStyle::LK_Cpp));
15246 TEST_F(FormatTest, FormatStarDependingOnContext) {
15247 verifyFormat("void f(int *a);");
15248 verifyFormat("void f() { f(fint * b); }");
15249 verifyFormat("class A {\n void f(int *a);\n};");
15250 verifyFormat("class A {\n int *a;\n};");
15251 verifyFormat("namespace a {\n"
15252 "namespace b {\n"
15253 "class A {\n"
15254 " void f() {}\n"
15255 " int *a;\n"
15256 "};\n"
15257 "} // namespace b\n"
15258 "} // namespace a");
15261 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15262 verifyFormat("while");
15263 verifyFormat("operator");
15266 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15267 // This code would be painfully slow to format if we didn't skip it.
15268 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
15269 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15270 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15271 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15272 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15273 "A(1, 1)\n"
15274 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15275 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15276 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15277 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15278 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15279 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15280 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15281 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15282 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15283 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15284 // Deeply nested part is untouched, rest is formatted.
15285 EXPECT_EQ(std::string("int i;") + Code + "int j;",
15286 format(std::string("int i;") + Code + "int j;",
15287 getLLVMStyle(), SC_ExpectIncomplete));
15290 //===----------------------------------------------------------------------===//
15291 // Objective-C tests.
15292 //===----------------------------------------------------------------------===//
15294 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15295 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15296 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15297 "-(NSUInteger)indexOfObject:(id)anObject;");
15298 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15299 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15300 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15301 "-(NSInteger)Method3:(id)anObject;");
15302 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15303 "-(NSInteger)Method4:(id)anObject;");
15304 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15305 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15306 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15307 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15308 "forAllCells:(BOOL)flag;");
15310 // Very long objectiveC method declaration.
15311 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15312 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15313 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15314 " inRange:(NSRange)range\n"
15315 " outRange:(NSRange)out_range\n"
15316 " outRange1:(NSRange)out_range1\n"
15317 " outRange2:(NSRange)out_range2\n"
15318 " outRange3:(NSRange)out_range3\n"
15319 " outRange4:(NSRange)out_range4\n"
15320 " outRange5:(NSRange)out_range5\n"
15321 " outRange6:(NSRange)out_range6\n"
15322 " outRange7:(NSRange)out_range7\n"
15323 " outRange8:(NSRange)out_range8\n"
15324 " outRange9:(NSRange)out_range9;");
15326 // When the function name has to be wrapped.
15327 FormatStyle Style = getLLVMStyle();
15328 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15329 // and always indents instead.
15330 Style.IndentWrappedFunctionNames = false;
15331 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15332 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15333 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15334 "}",
15335 Style);
15336 Style.IndentWrappedFunctionNames = true;
15337 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15338 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15339 " anotherName:(NSString)dddddddddddddd {\n"
15340 "}",
15341 Style);
15343 verifyFormat("- (int)sum:(vector<int>)numbers;");
15344 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15345 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15346 // protocol lists (but not for template classes):
15347 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15349 verifyFormat("- (int (*)())foo:(int (*)())f;");
15350 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15352 // If there's no return type (very rare in practice!), LLVM and Google style
15353 // agree.
15354 verifyFormat("- foo;");
15355 verifyFormat("- foo:(int)f;");
15356 verifyGoogleFormat("- foo:(int)foo;");
15359 TEST_F(FormatTest, BreaksStringLiterals) {
15360 // FIXME: unstable test case
15361 EXPECT_EQ("\"some text \"\n"
15362 "\"other\";",
15363 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15364 // FIXME: unstable test case
15365 EXPECT_EQ("\"some text \"\n"
15366 "\"other\";",
15367 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15368 verifyFormat("#define A \\\n"
15369 " \"some \" \\\n"
15370 " \"text \" \\\n"
15371 " \"other\";",
15372 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15373 verifyFormat("#define A \\\n"
15374 " \"so \" \\\n"
15375 " \"text \" \\\n"
15376 " \"other\";",
15377 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15379 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15380 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15381 // FIXME: unstable test case
15382 EXPECT_EQ("\"some \"\n"
15383 "\"text\"",
15384 format("\"some text\"", getLLVMStyleWithColumns(10)));
15385 // FIXME: unstable test case
15386 EXPECT_EQ("\"some \"\n"
15387 "\"text\"",
15388 format("\"some text\"", getLLVMStyleWithColumns(7)));
15389 // FIXME: unstable test case
15390 EXPECT_EQ("\"some\"\n"
15391 "\" tex\"\n"
15392 "\"t\"",
15393 format("\"some text\"", getLLVMStyleWithColumns(6)));
15394 // FIXME: unstable test case
15395 EXPECT_EQ("\"some\"\n"
15396 "\" tex\"\n"
15397 "\" and\"",
15398 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15399 // FIXME: unstable test case
15400 EXPECT_EQ("\"some\"\n"
15401 "\"/tex\"\n"
15402 "\"/and\"",
15403 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15405 verifyFormat("variable =\n"
15406 " \"long string \"\n"
15407 " \"literal\";",
15408 "variable = \"long string literal\";",
15409 getLLVMStyleWithColumns(20));
15411 verifyFormat("variable = f(\n"
15412 " \"long string \"\n"
15413 " \"literal\",\n"
15414 " short,\n"
15415 " loooooooooooooooooooong);",
15416 "variable = f(\"long string literal\", short, "
15417 "loooooooooooooooooooong);",
15418 getLLVMStyleWithColumns(20));
15420 verifyFormat("f(g(\"long string \"\n"
15421 " \"literal\"),\n"
15422 " b);",
15423 "f(g(\"long string literal\"), b);",
15424 getLLVMStyleWithColumns(20));
15425 verifyFormat("f(g(\"long string \"\n"
15426 " \"literal\",\n"
15427 " a),\n"
15428 " b);",
15429 "f(g(\"long string literal\", a), b);",
15430 getLLVMStyleWithColumns(20));
15431 verifyFormat("f(\"one two\".split(\n"
15432 " variable));",
15433 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15434 verifyFormat("f(\"one two three four five six \"\n"
15435 " \"seven\".split(\n"
15436 " really_looooong_variable));",
15437 "f(\"one two three four five six seven\"."
15438 "split(really_looooong_variable));",
15439 getLLVMStyleWithColumns(33));
15441 verifyFormat("f(\"some \"\n"
15442 " \"text\",\n"
15443 " other);",
15444 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15446 // Only break as a last resort.
15447 verifyFormat(
15448 "aaaaaaaaaaaaaaaaaaaa(\n"
15449 " aaaaaaaaaaaaaaaaaaaa,\n"
15450 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15452 // FIXME: unstable test case
15453 EXPECT_EQ("\"splitmea\"\n"
15454 "\"trandomp\"\n"
15455 "\"oint\"",
15456 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15458 // FIXME: unstable test case
15459 EXPECT_EQ("\"split/\"\n"
15460 "\"pathat/\"\n"
15461 "\"slashes\"",
15462 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15464 // FIXME: unstable test case
15465 EXPECT_EQ("\"split/\"\n"
15466 "\"pathat/\"\n"
15467 "\"slashes\"",
15468 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15469 // FIXME: unstable test case
15470 EXPECT_EQ("\"split at \"\n"
15471 "\"spaces/at/\"\n"
15472 "\"slashes.at.any$\"\n"
15473 "\"non-alphanumeric%\"\n"
15474 "\"1111111111characte\"\n"
15475 "\"rs\"",
15476 format("\"split at "
15477 "spaces/at/"
15478 "slashes.at."
15479 "any$non-"
15480 "alphanumeric%"
15481 "1111111111characte"
15482 "rs\"",
15483 getLLVMStyleWithColumns(20)));
15485 // Verify that splitting the strings understands
15486 // Style::AlwaysBreakBeforeMultilineStrings.
15487 verifyFormat("aaaaaaaaaaaa(\n"
15488 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15489 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15490 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15491 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15492 "aaaaaaaaaaaaaaaaaaaaaa\");",
15493 getGoogleStyle());
15494 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15495 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15496 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15497 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15498 "aaaaaaaaaaaaaaaaaaaaaa\";",
15499 getGoogleStyle());
15500 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15501 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15502 "llvm::outs() << "
15503 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15504 "aaaaaaaaaaaaaaaaaaa\";");
15505 verifyFormat("ffff(\n"
15506 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15507 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15508 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15510 getGoogleStyle());
15512 FormatStyle Style = getLLVMStyleWithColumns(12);
15513 Style.BreakStringLiterals = false;
15514 verifyFormat("\"some text other\";", Style);
15516 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
15517 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15518 verifyFormat("#define A \\\n"
15519 " \"some \" \\\n"
15520 " \"text \" \\\n"
15521 " \"other\";",
15522 "#define A \"some text other\";", AlignLeft);
15525 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
15526 verifyFormat("C a = \"some more \"\n"
15527 " \"text\";",
15528 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15531 TEST_F(FormatTest, FullyRemoveEmptyLines) {
15532 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
15533 NoEmptyLines.MaxEmptyLinesToKeep = 0;
15534 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15535 NoEmptyLines);
15538 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
15539 // FIXME: unstable test case
15540 EXPECT_EQ(
15541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15542 "(\n"
15543 " \"x\t\");",
15544 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15545 "aaaaaaa("
15546 "\"x\t\");"));
15549 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
15550 // FIXME: unstable test case
15551 EXPECT_EQ(
15552 "u8\"utf8 string \"\n"
15553 "u8\"literal\";",
15554 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15555 // FIXME: unstable test case
15556 EXPECT_EQ(
15557 "u\"utf16 string \"\n"
15558 "u\"literal\";",
15559 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15560 // FIXME: unstable test case
15561 EXPECT_EQ(
15562 "U\"utf32 string \"\n"
15563 "U\"literal\";",
15564 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15565 // FIXME: unstable test case
15566 EXPECT_EQ("L\"wide string \"\n"
15567 "L\"literal\";",
15568 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15569 verifyFormat("@\"NSString \"\n"
15570 "@\"literal\";",
15571 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15572 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
15574 // This input makes clang-format try to split the incomplete unicode escape
15575 // sequence, which used to lead to a crasher.
15576 verifyNoCrash(
15577 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15578 getLLVMStyleWithColumns(60));
15581 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
15582 FormatStyle Style = getGoogleStyleWithColumns(15);
15583 verifyFormat("R\"x(raw literal)x\";", Style);
15584 verifyFormat("uR\"x(raw literal)x\";", Style);
15585 verifyFormat("LR\"x(raw literal)x\";", Style);
15586 verifyFormat("UR\"x(raw literal)x\";", Style);
15587 verifyFormat("u8R\"x(raw literal)x\";", Style);
15590 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
15591 FormatStyle Style = getLLVMStyleWithColumns(20);
15592 // FIXME: unstable test case
15593 EXPECT_EQ(
15594 "_T(\"aaaaaaaaaaaaaa\")\n"
15595 "_T(\"aaaaaaaaaaaaaa\")\n"
15596 "_T(\"aaaaaaaaaaaa\")",
15597 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
15598 verifyFormat("f(x,\n"
15599 " _T(\"aaaaaaaaaaaa\")\n"
15600 " _T(\"aaa\"),\n"
15601 " z);",
15602 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
15604 // FIXME: Handle embedded spaces in one iteration.
15605 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15606 // "_T(\"aaaaaaaaaaaaa\")\n"
15607 // "_T(\"aaaaaaaaaaaaa\")\n"
15608 // "_T(\"a\")",
15609 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15610 // getLLVMStyleWithColumns(20)));
15611 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15612 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
15613 verifyFormat("f(\n"
15614 "#if !TEST\n"
15615 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15616 "#endif\n"
15617 ");",
15618 "f(\n"
15619 "#if !TEST\n"
15620 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15621 "#endif\n"
15622 ");");
15623 verifyFormat("f(\n"
15624 "\n"
15625 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15626 "f(\n"
15627 "\n"
15628 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15629 // Regression test for accessing tokens past the end of a vector in the
15630 // TokenLexer.
15631 verifyNoCrash(R"(_T(
15634 )");
15637 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15638 // In a function call with two operands, the second can be broken with no line
15639 // break before it.
15640 verifyFormat("func(a, \"long long \"\n"
15641 " \"long long\");",
15642 "func(a, \"long long long long\");",
15643 getLLVMStyleWithColumns(24));
15644 // In a function call with three operands, the second must be broken with a
15645 // line break before it.
15646 verifyFormat("func(a,\n"
15647 " \"long long long \"\n"
15648 " \"long\",\n"
15649 " c);",
15650 "func(a, \"long long long long\", c);",
15651 getLLVMStyleWithColumns(24));
15652 // In a function call with three operands, the third must be broken with a
15653 // line break before it.
15654 verifyFormat("func(a, b,\n"
15655 " \"long long long \"\n"
15656 " \"long\");",
15657 "func(a, b, \"long long long long\");",
15658 getLLVMStyleWithColumns(24));
15659 // In a function call with three operands, both the second and the third must
15660 // be broken with a line break before them.
15661 verifyFormat("func(a,\n"
15662 " \"long long long \"\n"
15663 " \"long\",\n"
15664 " \"long long long \"\n"
15665 " \"long\");",
15666 "func(a, \"long long long long\", \"long long long long\");",
15667 getLLVMStyleWithColumns(24));
15668 // In a chain of << with two operands, the second can be broken with no line
15669 // break before it.
15670 verifyFormat("a << \"line line \"\n"
15671 " \"line\";",
15672 "a << \"line line line\";", getLLVMStyleWithColumns(20));
15673 // In a chain of << with three operands, the second can be broken with no line
15674 // break before it.
15675 verifyFormat("abcde << \"line \"\n"
15676 " \"line line\"\n"
15677 " << c;",
15678 "abcde << \"line line line\" << c;",
15679 getLLVMStyleWithColumns(20));
15680 // In a chain of << with three operands, the third must be broken with a line
15681 // break before it.
15682 verifyFormat("a << b\n"
15683 " << \"line line \"\n"
15684 " \"line\";",
15685 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15686 // In a chain of << with three operands, the second can be broken with no line
15687 // break before it and the third must be broken with a line break before it.
15688 verifyFormat("abcd << \"line line \"\n"
15689 " \"line\"\n"
15690 " << \"line line \"\n"
15691 " \"line\";",
15692 "abcd << \"line line line\" << \"line line line\";",
15693 getLLVMStyleWithColumns(20));
15694 // In a chain of binary operators with two operands, the second can be broken
15695 // with no line break before it.
15696 verifyFormat("abcd + \"line line \"\n"
15697 " \"line line\";",
15698 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15699 // In a chain of binary operators with three operands, the second must be
15700 // broken with a line break before it.
15701 verifyFormat("abcd +\n"
15702 " \"line line \"\n"
15703 " \"line line\" +\n"
15704 " e;",
15705 "abcd + \"line line line line\" + e;",
15706 getLLVMStyleWithColumns(20));
15707 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15708 // the first must be broken with a line break before it.
15709 FormatStyle Style = getLLVMStyleWithColumns(25);
15710 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15711 verifyFormat("someFunction(\n"
15712 " \"long long long \"\n"
15713 " \"long\",\n"
15714 " a);",
15715 "someFunction(\"long long long long\", a);", Style);
15716 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15717 verifyFormat("someFunction(\n"
15718 " \"long long long \"\n"
15719 " \"long\",\n"
15720 " a\n"
15721 ");",
15722 Style);
15725 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15726 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15729 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
15734 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15735 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
15736 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
15737 verifyFormat("fffffffffff(g(R\"x(\n"
15738 "multiline raw string literal xxxxxxxxxxxxxx\n"
15739 ")x\",\n"
15740 " a),\n"
15741 " b);",
15742 "fffffffffff(g(R\"x(\n"
15743 "multiline raw string literal xxxxxxxxxxxxxx\n"
15744 ")x\", a), b);",
15745 getGoogleStyleWithColumns(20));
15746 verifyFormat("fffffffffff(\n"
15747 " g(R\"x(qqq\n"
15748 "multiline raw string literal xxxxxxxxxxxxxx\n"
15749 ")x\",\n"
15750 " a),\n"
15751 " b);",
15752 "fffffffffff(g(R\"x(qqq\n"
15753 "multiline raw string literal xxxxxxxxxxxxxx\n"
15754 ")x\", a), b);",
15755 getGoogleStyleWithColumns(20));
15757 verifyNoChange("fffffffffff(R\"x(\n"
15758 "multiline raw string literal xxxxxxxxxxxxxx\n"
15759 ")x\");",
15760 getGoogleStyleWithColumns(20));
15761 verifyFormat("fffffffffff(R\"x(\n"
15762 "multiline raw string literal xxxxxxxxxxxxxx\n"
15763 ")x\" + bbbbbb);",
15764 "fffffffffff(R\"x(\n"
15765 "multiline raw string literal xxxxxxxxxxxxxx\n"
15766 ")x\" + bbbbbb);",
15767 getGoogleStyleWithColumns(20));
15768 verifyFormat("fffffffffff(\n"
15769 " R\"x(\n"
15770 "multiline raw string literal xxxxxxxxxxxxxx\n"
15771 ")x\" +\n"
15772 " bbbbbb);",
15773 "fffffffffff(\n"
15774 " R\"x(\n"
15775 "multiline raw string literal xxxxxxxxxxxxxx\n"
15776 ")x\" + bbbbbb);",
15777 getGoogleStyleWithColumns(20));
15778 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
15779 "fffffffffff(\n"
15780 " R\"(single line raw string)\" + bbbbbb);");
15783 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15784 verifyFormat("string a = \"unterminated;");
15785 verifyFormat("function(\"unterminated,\n"
15786 " OtherParameter);",
15787 "function( \"unterminated,\n"
15788 " OtherParameter);");
15791 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15792 FormatStyle Style = getLLVMStyle();
15793 Style.Standard = FormatStyle::LS_Cpp03;
15794 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15795 "#define x(_a) printf(\"foo\"_a);", Style);
15798 TEST_F(FormatTest, CppLexVersion) {
15799 FormatStyle Style = getLLVMStyle();
15800 // Formatting of x * y differs if x is a type.
15801 verifyFormat("void foo() { MACRO(a * b); }", Style);
15802 verifyFormat("void foo() { MACRO(int *b); }", Style);
15804 // LLVM style uses latest lexer.
15805 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15806 Style.Standard = FormatStyle::LS_Cpp17;
15807 // But in c++17, char8_t isn't a keyword.
15808 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15811 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15813 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15814 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15815 " \"ddeeefff\");",
15816 "someFunction(\"aaabbbcccdddeeefff\");",
15817 getLLVMStyleWithColumns(25));
15818 verifyFormat("someFunction1234567890(\n"
15819 " \"aaabbbcccdddeeefff\");",
15820 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15821 getLLVMStyleWithColumns(26));
15822 verifyFormat("someFunction1234567890(\n"
15823 " \"aaabbbcccdddeeeff\"\n"
15824 " \"f\");",
15825 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15826 getLLVMStyleWithColumns(25));
15827 verifyFormat("someFunction1234567890(\n"
15828 " \"aaabbbcccdddeeeff\"\n"
15829 " \"f\");",
15830 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15831 getLLVMStyleWithColumns(24));
15832 verifyFormat("someFunction(\n"
15833 " \"aaabbbcc ddde \"\n"
15834 " \"efff\");",
15835 "someFunction(\"aaabbbcc ddde efff\");",
15836 getLLVMStyleWithColumns(25));
15837 verifyFormat("someFunction(\"aaabbbccc \"\n"
15838 " \"ddeeefff\");",
15839 "someFunction(\"aaabbbccc ddeeefff\");",
15840 getLLVMStyleWithColumns(25));
15841 verifyFormat("someFunction1234567890(\n"
15842 " \"aaabb \"\n"
15843 " \"cccdddeeefff\");",
15844 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15845 getLLVMStyleWithColumns(25));
15846 verifyFormat("#define A \\\n"
15847 " string s = \\\n"
15848 " \"123456789\" \\\n"
15849 " \"0\"; \\\n"
15850 " int i;",
15851 "#define A string s = \"1234567890\"; int i;",
15852 getLLVMStyleWithColumns(20));
15853 verifyFormat("someFunction(\n"
15854 " \"aaabbbcc \"\n"
15855 " \"dddeeefff\");",
15856 "someFunction(\"aaabbbcc dddeeefff\");",
15857 getLLVMStyleWithColumns(25));
15860 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15861 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15862 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15863 // FIXME: unstable test case
15864 EXPECT_EQ("\"test\"\n"
15865 "\"\\n\"",
15866 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15867 // FIXME: unstable test case
15868 EXPECT_EQ("\"tes\\\\\"\n"
15869 "\"n\"",
15870 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15871 // FIXME: unstable test case
15872 EXPECT_EQ("\"\\\\\\\\\"\n"
15873 "\"\\n\"",
15874 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15875 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15876 // FIXME: unstable test case
15877 EXPECT_EQ("\"\\uff01\"\n"
15878 "\"test\"",
15879 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15880 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15881 // FIXME: unstable test case
15882 EXPECT_EQ("\"\\x000000000001\"\n"
15883 "\"next\"",
15884 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15885 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15886 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15887 // FIXME: unstable test case
15888 EXPECT_EQ("\"test\"\n"
15889 "\"\\000000\"\n"
15890 "\"000001\"",
15891 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15892 // FIXME: unstable test case
15893 EXPECT_EQ("\"test\\000\"\n"
15894 "\"00000000\"\n"
15895 "\"1\"",
15896 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15899 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15900 verifyFormat("void f() {\n"
15901 " return g() {}\n"
15902 " void h() {}");
15903 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15904 "g();\n"
15905 "}");
15908 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15909 verifyFormat(
15910 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15913 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15914 verifyFormat("class X {\n"
15915 " void f() {\n"
15916 " }\n"
15917 "};",
15918 getLLVMStyleWithColumns(12));
15921 TEST_F(FormatTest, ConfigurableIndentWidth) {
15922 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15923 EightIndent.IndentWidth = 8;
15924 EightIndent.ContinuationIndentWidth = 8;
15925 verifyFormat("void f() {\n"
15926 " someFunction();\n"
15927 " if (true) {\n"
15928 " f();\n"
15929 " }\n"
15930 "}",
15931 EightIndent);
15932 verifyFormat("class X {\n"
15933 " void f() {\n"
15934 " }\n"
15935 "};",
15936 EightIndent);
15937 verifyFormat("int x[] = {\n"
15938 " call(),\n"
15939 " call()};",
15940 EightIndent);
15943 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15944 verifyFormat("double\n"
15945 "f();",
15946 getLLVMStyleWithColumns(8));
15949 TEST_F(FormatTest, ConfigurableUseOfTab) {
15950 FormatStyle Tab = getLLVMStyleWithColumns(42);
15951 Tab.IndentWidth = 8;
15952 Tab.UseTab = FormatStyle::UT_Always;
15953 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15955 verifyFormat("if (aaaaaaaa && // q\n"
15956 " bb)\t\t// w\n"
15957 "\t;",
15958 "if (aaaaaaaa &&// q\n"
15959 "bb)// w\n"
15960 ";",
15961 Tab);
15962 verifyFormat("if (aaa && bbb) // w\n"
15963 "\t;",
15964 "if(aaa&&bbb)// w\n"
15965 ";",
15966 Tab);
15968 verifyFormat("class X {\n"
15969 "\tvoid f() {\n"
15970 "\t\tsomeFunction(parameter1,\n"
15971 "\t\t\t parameter2);\n"
15972 "\t}\n"
15973 "};",
15974 Tab);
15975 verifyFormat("#define A \\\n"
15976 "\tvoid f() { \\\n"
15977 "\t\tsomeFunction( \\\n"
15978 "\t\t parameter1, \\\n"
15979 "\t\t parameter2); \\\n"
15980 "\t}",
15981 Tab);
15982 verifyFormat("int a;\t // x\n"
15983 "int bbbbbbbb; // x",
15984 Tab);
15986 FormatStyle TabAlignment = Tab;
15987 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
15988 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15989 verifyFormat("unsigned long long big;\n"
15990 "char*\t\t ptr;",
15991 TabAlignment);
15992 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15993 verifyFormat("unsigned long long big;\n"
15994 "char *\t\t ptr;",
15995 TabAlignment);
15996 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15997 verifyFormat("unsigned long long big;\n"
15998 "char\t\t *ptr;",
15999 TabAlignment);
16001 Tab.TabWidth = 4;
16002 Tab.IndentWidth = 8;
16003 verifyFormat("class TabWidth4Indent8 {\n"
16004 "\t\tvoid f() {\n"
16005 "\t\t\t\tsomeFunction(parameter1,\n"
16006 "\t\t\t\t\t\t\t parameter2);\n"
16007 "\t\t}\n"
16008 "};",
16009 Tab);
16011 Tab.TabWidth = 4;
16012 Tab.IndentWidth = 4;
16013 verifyFormat("class TabWidth4Indent4 {\n"
16014 "\tvoid f() {\n"
16015 "\t\tsomeFunction(parameter1,\n"
16016 "\t\t\t\t\t parameter2);\n"
16017 "\t}\n"
16018 "};",
16019 Tab);
16021 Tab.TabWidth = 8;
16022 Tab.IndentWidth = 4;
16023 verifyFormat("class TabWidth8Indent4 {\n"
16024 " void f() {\n"
16025 "\tsomeFunction(parameter1,\n"
16026 "\t\t parameter2);\n"
16027 " }\n"
16028 "};",
16029 Tab);
16031 Tab.TabWidth = 8;
16032 Tab.IndentWidth = 8;
16033 verifyFormat("/*\n"
16034 "\t a\t\tcomment\n"
16035 "\t in multiple lines\n"
16036 " */",
16037 " /*\t \t \n"
16038 " \t \t a\t\tcomment\t \t\n"
16039 " \t \t in multiple lines\t\n"
16040 " \t */",
16041 Tab);
16043 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
16044 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16045 verifyFormat("void f() {\n"
16046 "\tunsigned long long big;\n"
16047 "\tchar* ptr;\n"
16048 "}",
16049 TabAlignment);
16050 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16051 verifyFormat("void f() {\n"
16052 "\tunsigned long long big;\n"
16053 "\tchar * ptr;\n"
16054 "}",
16055 TabAlignment);
16056 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16057 verifyFormat("void f() {\n"
16058 "\tunsigned long long big;\n"
16059 "\tchar *ptr;\n"
16060 "}",
16061 TabAlignment);
16063 Tab.UseTab = FormatStyle::UT_ForIndentation;
16064 verifyFormat("{\n"
16065 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16066 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16067 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16068 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16069 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16070 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16071 "};",
16072 Tab);
16073 verifyFormat("enum AA {\n"
16074 "\ta1, // Force multiple lines\n"
16075 "\ta2,\n"
16076 "\ta3\n"
16077 "};",
16078 Tab);
16079 verifyFormat("if (aaaaaaaa && // q\n"
16080 " bb) // w\n"
16081 "\t;",
16082 "if (aaaaaaaa &&// q\n"
16083 "bb)// w\n"
16084 ";",
16085 Tab);
16086 verifyFormat("class X {\n"
16087 "\tvoid f() {\n"
16088 "\t\tsomeFunction(parameter1,\n"
16089 "\t\t parameter2);\n"
16090 "\t}\n"
16091 "};",
16092 Tab);
16093 verifyFormat("{\n"
16094 "\tQ(\n"
16095 "\t {\n"
16096 "\t\t int a;\n"
16097 "\t\t someFunction(aaaaaaaa,\n"
16098 "\t\t bbbbbbb);\n"
16099 "\t },\n"
16100 "\t p);\n"
16101 "}",
16102 Tab);
16103 verifyFormat("{\n"
16104 "\t/* aaaa\n"
16105 "\t bbbb */\n"
16106 "}",
16107 "{\n"
16108 "/* aaaa\n"
16109 " bbbb */\n"
16110 "}",
16111 Tab);
16112 verifyFormat("{\n"
16113 "\t/*\n"
16114 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16115 "\t bbbbbbbbbbbbb\n"
16116 "\t*/\n"
16117 "}",
16118 "{\n"
16119 "/*\n"
16120 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16121 "*/\n"
16122 "}",
16123 Tab);
16124 verifyFormat("{\n"
16125 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16126 "\t// bbbbbbbbbbbbb\n"
16127 "}",
16128 "{\n"
16129 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16130 "}",
16131 Tab);
16132 verifyFormat("{\n"
16133 "\t/*\n"
16134 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16135 "\t bbbbbbbbbbbbb\n"
16136 "\t*/\n"
16137 "}",
16138 "{\n"
16139 "\t/*\n"
16140 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16141 "\t*/\n"
16142 "}",
16143 Tab);
16144 verifyNoChange("{\n"
16145 "\t/*\n"
16146 "\n"
16147 "\t*/\n"
16148 "}",
16149 Tab);
16150 verifyNoChange("{\n"
16151 "\t/*\n"
16152 " asdf\n"
16153 "\t*/\n"
16154 "}",
16155 Tab);
16157 verifyFormat("void f() {\n"
16158 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16159 "\t : bbbbbbbbbbbbbbbbbb\n"
16160 "}",
16161 Tab);
16162 FormatStyle TabNoBreak = Tab;
16163 TabNoBreak.BreakBeforeTernaryOperators = false;
16164 verifyFormat("void f() {\n"
16165 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16166 "\t bbbbbbbbbbbbbbbbbb\n"
16167 "}",
16168 TabNoBreak);
16169 verifyFormat("void f() {\n"
16170 "\treturn true ?\n"
16171 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16172 "\t bbbbbbbbbbbbbbbbbbbb\n"
16173 "}",
16174 TabNoBreak);
16176 Tab.UseTab = FormatStyle::UT_Never;
16177 verifyFormat("/*\n"
16178 " a\t\tcomment\n"
16179 " in multiple lines\n"
16180 " */",
16181 " /*\t \t \n"
16182 " \t \t a\t\tcomment\t \t\n"
16183 " \t \t in multiple lines\t\n"
16184 " \t */",
16185 Tab);
16186 verifyFormat("/* some\n"
16187 " comment */",
16188 " \t \t /* some\n"
16189 " \t \t comment */",
16190 Tab);
16191 verifyFormat("int a; /* some\n"
16192 " comment */",
16193 " \t \t int a; /* some\n"
16194 " \t \t comment */",
16195 Tab);
16197 verifyFormat("int a; /* some\n"
16198 "comment */",
16199 " \t \t int\ta; /* some\n"
16200 " \t \t comment */",
16201 Tab);
16202 verifyFormat("f(\"\t\t\"); /* some\n"
16203 " comment */",
16204 " \t \t f(\"\t\t\"); /* some\n"
16205 " \t \t comment */",
16206 Tab);
16207 verifyFormat("{\n"
16208 " /*\n"
16209 " * Comment\n"
16210 " */\n"
16211 " int i;\n"
16212 "}",
16213 "{\n"
16214 "\t/*\n"
16215 "\t * Comment\n"
16216 "\t */\n"
16217 "\t int i;\n"
16218 "}",
16219 Tab);
16221 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16222 Tab.TabWidth = 8;
16223 Tab.IndentWidth = 8;
16224 verifyFormat("if (aaaaaaaa && // q\n"
16225 " bb) // w\n"
16226 "\t;",
16227 "if (aaaaaaaa &&// q\n"
16228 "bb)// w\n"
16229 ";",
16230 Tab);
16231 verifyFormat("if (aaa && bbb) // w\n"
16232 "\t;",
16233 "if(aaa&&bbb)// w\n"
16234 ";",
16235 Tab);
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 Tab.TabWidth = 4;
16251 Tab.IndentWidth = 8;
16252 verifyFormat("class TabWidth4Indent8 {\n"
16253 "\t\tvoid f() {\n"
16254 "\t\t\t\tsomeFunction(parameter1,\n"
16255 "\t\t\t\t\t\t\t parameter2);\n"
16256 "\t\t}\n"
16257 "};",
16258 Tab);
16259 Tab.TabWidth = 4;
16260 Tab.IndentWidth = 4;
16261 verifyFormat("class TabWidth4Indent4 {\n"
16262 "\tvoid f() {\n"
16263 "\t\tsomeFunction(parameter1,\n"
16264 "\t\t\t\t\t parameter2);\n"
16265 "\t}\n"
16266 "};",
16267 Tab);
16268 Tab.TabWidth = 8;
16269 Tab.IndentWidth = 4;
16270 verifyFormat("class TabWidth8Indent4 {\n"
16271 " void f() {\n"
16272 "\tsomeFunction(parameter1,\n"
16273 "\t\t parameter2);\n"
16274 " }\n"
16275 "};",
16276 Tab);
16277 Tab.TabWidth = 8;
16278 Tab.IndentWidth = 8;
16279 verifyFormat("/*\n"
16280 "\t a\t\tcomment\n"
16281 "\t in multiple lines\n"
16282 " */",
16283 " /*\t \t \n"
16284 " \t \t a\t\tcomment\t \t\n"
16285 " \t \t in multiple lines\t\n"
16286 " \t */",
16287 Tab);
16288 verifyFormat("{\n"
16289 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16290 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16291 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16292 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16293 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16294 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16295 "};",
16296 Tab);
16297 verifyFormat("enum AA {\n"
16298 "\ta1, // Force multiple lines\n"
16299 "\ta2,\n"
16300 "\ta3\n"
16301 "};",
16302 Tab);
16303 verifyFormat("if (aaaaaaaa && // q\n"
16304 " bb) // w\n"
16305 "\t;",
16306 "if (aaaaaaaa &&// q\n"
16307 "bb)// w\n"
16308 ";",
16309 Tab);
16310 verifyFormat("class X {\n"
16311 "\tvoid f() {\n"
16312 "\t\tsomeFunction(parameter1,\n"
16313 "\t\t\t parameter2);\n"
16314 "\t}\n"
16315 "};",
16316 Tab);
16317 verifyFormat("{\n"
16318 "\tQ(\n"
16319 "\t {\n"
16320 "\t\t int a;\n"
16321 "\t\t someFunction(aaaaaaaa,\n"
16322 "\t\t\t\t bbbbbbb);\n"
16323 "\t },\n"
16324 "\t p);\n"
16325 "}",
16326 Tab);
16327 verifyFormat("{\n"
16328 "\t/* aaaa\n"
16329 "\t bbbb */\n"
16330 "}",
16331 "{\n"
16332 "/* aaaa\n"
16333 " bbbb */\n"
16334 "}",
16335 Tab);
16336 verifyFormat("{\n"
16337 "\t/*\n"
16338 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16339 "\t bbbbbbbbbbbbb\n"
16340 "\t*/\n"
16341 "}",
16342 "{\n"
16343 "/*\n"
16344 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16345 "*/\n"
16346 "}",
16347 Tab);
16348 verifyFormat("{\n"
16349 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16350 "\t// bbbbbbbbbbbbb\n"
16351 "}",
16352 "{\n"
16353 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16354 "}",
16355 Tab);
16356 verifyFormat("{\n"
16357 "\t/*\n"
16358 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16359 "\t bbbbbbbbbbbbb\n"
16360 "\t*/\n"
16361 "}",
16362 "{\n"
16363 "\t/*\n"
16364 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16365 "\t*/\n"
16366 "}",
16367 Tab);
16368 verifyNoChange("{\n"
16369 "\t/*\n"
16370 "\n"
16371 "\t*/\n"
16372 "}",
16373 Tab);
16374 verifyNoChange("{\n"
16375 "\t/*\n"
16376 " asdf\n"
16377 "\t*/\n"
16378 "}",
16379 Tab);
16380 verifyFormat("/* some\n"
16381 " comment */",
16382 " \t \t /* some\n"
16383 " \t \t comment */",
16384 Tab);
16385 verifyFormat("int a; /* some\n"
16386 " comment */",
16387 " \t \t int a; /* some\n"
16388 " \t \t comment */",
16389 Tab);
16390 verifyFormat("int a; /* some\n"
16391 "comment */",
16392 " \t \t int\ta; /* some\n"
16393 " \t \t comment */",
16394 Tab);
16395 verifyFormat("f(\"\t\t\"); /* some\n"
16396 " comment */",
16397 " \t \t f(\"\t\t\"); /* some\n"
16398 " \t \t comment */",
16399 Tab);
16400 verifyFormat("{\n"
16401 "\t/*\n"
16402 "\t * Comment\n"
16403 "\t */\n"
16404 "\tint i;\n"
16405 "}",
16406 "{\n"
16407 "\t/*\n"
16408 "\t * Comment\n"
16409 "\t */\n"
16410 "\t int i;\n"
16411 "}",
16412 Tab);
16413 Tab.TabWidth = 2;
16414 Tab.IndentWidth = 2;
16415 verifyFormat("{\n"
16416 "\t/* aaaa\n"
16417 "\t\t bbbb */\n"
16418 "}",
16419 "{\n"
16420 "/* aaaa\n"
16421 "\t bbbb */\n"
16422 "}",
16423 Tab);
16424 verifyFormat("{\n"
16425 "\t/*\n"
16426 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16427 "\t\tbbbbbbbbbbbbb\n"
16428 "\t*/\n"
16429 "}",
16430 "{\n"
16431 "/*\n"
16432 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16433 "*/\n"
16434 "}",
16435 Tab);
16436 Tab.AlignConsecutiveAssignments.Enabled = true;
16437 Tab.AlignConsecutiveDeclarations.Enabled = true;
16438 Tab.TabWidth = 4;
16439 Tab.IndentWidth = 4;
16440 verifyFormat("class Assign {\n"
16441 "\tvoid f() {\n"
16442 "\t\tint x = 123;\n"
16443 "\t\tint random = 4;\n"
16444 "\t\tstd::string alphabet =\n"
16445 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16446 "\t}\n"
16447 "};",
16448 Tab);
16450 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16451 Tab.TabWidth = 8;
16452 Tab.IndentWidth = 8;
16453 verifyFormat("if (aaaaaaaa && // q\n"
16454 " bb) // w\n"
16455 "\t;",
16456 "if (aaaaaaaa &&// q\n"
16457 "bb)// w\n"
16458 ";",
16459 Tab);
16460 verifyFormat("if (aaa && bbb) // w\n"
16461 "\t;",
16462 "if(aaa&&bbb)// w\n"
16463 ";",
16464 Tab);
16465 verifyFormat("class X {\n"
16466 "\tvoid f() {\n"
16467 "\t\tsomeFunction(parameter1,\n"
16468 "\t\t parameter2);\n"
16469 "\t}\n"
16470 "};",
16471 Tab);
16472 verifyFormat("#define A \\\n"
16473 "\tvoid f() { \\\n"
16474 "\t\tsomeFunction( \\\n"
16475 "\t\t parameter1, \\\n"
16476 "\t\t parameter2); \\\n"
16477 "\t}",
16478 Tab);
16479 Tab.TabWidth = 4;
16480 Tab.IndentWidth = 8;
16481 verifyFormat("class TabWidth4Indent8 {\n"
16482 "\t\tvoid f() {\n"
16483 "\t\t\t\tsomeFunction(parameter1,\n"
16484 "\t\t\t\t parameter2);\n"
16485 "\t\t}\n"
16486 "};",
16487 Tab);
16488 Tab.TabWidth = 4;
16489 Tab.IndentWidth = 4;
16490 verifyFormat("class TabWidth4Indent4 {\n"
16491 "\tvoid f() {\n"
16492 "\t\tsomeFunction(parameter1,\n"
16493 "\t\t parameter2);\n"
16494 "\t}\n"
16495 "};",
16496 Tab);
16497 Tab.TabWidth = 8;
16498 Tab.IndentWidth = 4;
16499 verifyFormat("class TabWidth8Indent4 {\n"
16500 " void f() {\n"
16501 "\tsomeFunction(parameter1,\n"
16502 "\t parameter2);\n"
16503 " }\n"
16504 "};",
16505 Tab);
16506 Tab.TabWidth = 8;
16507 Tab.IndentWidth = 8;
16508 verifyFormat("/*\n"
16509 " a\t\tcomment\n"
16510 " in multiple lines\n"
16511 " */",
16512 " /*\t \t \n"
16513 " \t \t a\t\tcomment\t \t\n"
16514 " \t \t in multiple lines\t\n"
16515 " \t */",
16516 Tab);
16517 verifyFormat("{\n"
16518 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16519 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16520 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16521 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16522 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16523 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16524 "};",
16525 Tab);
16526 verifyFormat("enum AA {\n"
16527 "\ta1, // Force multiple lines\n"
16528 "\ta2,\n"
16529 "\ta3\n"
16530 "};",
16531 Tab);
16532 verifyFormat("if (aaaaaaaa && // q\n"
16533 " bb) // w\n"
16534 "\t;",
16535 "if (aaaaaaaa &&// q\n"
16536 "bb)// w\n"
16537 ";",
16538 Tab);
16539 verifyFormat("class X {\n"
16540 "\tvoid f() {\n"
16541 "\t\tsomeFunction(parameter1,\n"
16542 "\t\t parameter2);\n"
16543 "\t}\n"
16544 "};",
16545 Tab);
16546 verifyFormat("{\n"
16547 "\tQ(\n"
16548 "\t {\n"
16549 "\t\t int a;\n"
16550 "\t\t someFunction(aaaaaaaa,\n"
16551 "\t\t bbbbbbb);\n"
16552 "\t },\n"
16553 "\t p);\n"
16554 "}",
16555 Tab);
16556 verifyFormat("{\n"
16557 "\t/* aaaa\n"
16558 "\t bbbb */\n"
16559 "}",
16560 "{\n"
16561 "/* aaaa\n"
16562 " bbbb */\n"
16563 "}",
16564 Tab);
16565 verifyFormat("{\n"
16566 "\t/*\n"
16567 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16568 "\t bbbbbbbbbbbbb\n"
16569 "\t*/\n"
16570 "}",
16571 "{\n"
16572 "/*\n"
16573 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16574 "*/\n"
16575 "}",
16576 Tab);
16577 verifyFormat("{\n"
16578 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16579 "\t// bbbbbbbbbbbbb\n"
16580 "}",
16581 "{\n"
16582 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16583 "}",
16584 Tab);
16585 verifyFormat("{\n"
16586 "\t/*\n"
16587 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16588 "\t bbbbbbbbbbbbb\n"
16589 "\t*/\n"
16590 "}",
16591 "{\n"
16592 "\t/*\n"
16593 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16594 "\t*/\n"
16595 "}",
16596 Tab);
16597 verifyNoChange("{\n"
16598 "\t/*\n"
16599 "\n"
16600 "\t*/\n"
16601 "}",
16602 Tab);
16603 verifyNoChange("{\n"
16604 "\t/*\n"
16605 " asdf\n"
16606 "\t*/\n"
16607 "}",
16608 Tab);
16609 verifyFormat("/* some\n"
16610 " comment */",
16611 " \t \t /* some\n"
16612 " \t \t comment */",
16613 Tab);
16614 verifyFormat("int a; /* some\n"
16615 " comment */",
16616 " \t \t int a; /* some\n"
16617 " \t \t comment */",
16618 Tab);
16619 verifyFormat("int a; /* some\n"
16620 "comment */",
16621 " \t \t int\ta; /* some\n"
16622 " \t \t comment */",
16623 Tab);
16624 verifyFormat("f(\"\t\t\"); /* some\n"
16625 " comment */",
16626 " \t \t f(\"\t\t\"); /* some\n"
16627 " \t \t comment */",
16628 Tab);
16629 verifyFormat("{\n"
16630 "\t/*\n"
16631 "\t * Comment\n"
16632 "\t */\n"
16633 "\tint i;\n"
16634 "}",
16635 "{\n"
16636 "\t/*\n"
16637 "\t * Comment\n"
16638 "\t */\n"
16639 "\t int i;\n"
16640 "}",
16641 Tab);
16642 Tab.TabWidth = 2;
16643 Tab.IndentWidth = 2;
16644 verifyFormat("{\n"
16645 "\t/* aaaa\n"
16646 "\t bbbb */\n"
16647 "}",
16648 "{\n"
16649 "/* aaaa\n"
16650 " bbbb */\n"
16651 "}",
16652 Tab);
16653 verifyFormat("{\n"
16654 "\t/*\n"
16655 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16656 "\t bbbbbbbbbbbbb\n"
16657 "\t*/\n"
16658 "}",
16659 "{\n"
16660 "/*\n"
16661 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16662 "*/\n"
16663 "}",
16664 Tab);
16665 Tab.AlignConsecutiveAssignments.Enabled = true;
16666 Tab.AlignConsecutiveDeclarations.Enabled = true;
16667 Tab.TabWidth = 4;
16668 Tab.IndentWidth = 4;
16669 verifyFormat("class Assign {\n"
16670 "\tvoid f() {\n"
16671 "\t\tint x = 123;\n"
16672 "\t\tint random = 4;\n"
16673 "\t\tstd::string alphabet =\n"
16674 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16675 "\t}\n"
16676 "};",
16677 Tab);
16678 Tab.AlignOperands = FormatStyle::OAS_Align;
16679 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16680 " cccccccccccccccccccc;",
16681 Tab);
16682 // no alignment
16683 verifyFormat("int aaaaaaaaaa =\n"
16684 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16685 Tab);
16686 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16687 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16688 " : 333333333333333;",
16689 Tab);
16690 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16691 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16692 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16693 " + cccccccccccccccccccc;",
16694 Tab);
16697 TEST_F(FormatTest, ZeroTabWidth) {
16698 FormatStyle Tab = getLLVMStyleWithColumns(42);
16699 Tab.IndentWidth = 8;
16700 Tab.UseTab = FormatStyle::UT_Never;
16701 Tab.TabWidth = 0;
16702 verifyFormat("void a() {\n"
16703 " // line starts with '\t'\n"
16704 "};",
16705 "void a(){\n"
16706 "\t// line starts with '\t'\n"
16707 "};",
16708 Tab);
16710 verifyFormat("void a() {\n"
16711 " // line starts with '\t'\n"
16712 "};",
16713 "void a(){\n"
16714 "\t\t// line starts with '\t'\n"
16715 "};",
16716 Tab);
16718 Tab.UseTab = FormatStyle::UT_ForIndentation;
16719 verifyFormat("void a() {\n"
16720 " // line starts with '\t'\n"
16721 "};",
16722 "void a(){\n"
16723 "\t// line starts with '\t'\n"
16724 "};",
16725 Tab);
16727 verifyFormat("void a() {\n"
16728 " // line starts with '\t'\n"
16729 "};",
16730 "void a(){\n"
16731 "\t\t// line starts with '\t'\n"
16732 "};",
16733 Tab);
16735 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16736 verifyFormat("void a() {\n"
16737 " // line starts with '\t'\n"
16738 "};",
16739 "void a(){\n"
16740 "\t// line starts with '\t'\n"
16741 "};",
16742 Tab);
16744 verifyFormat("void a() {\n"
16745 " // line starts with '\t'\n"
16746 "};",
16747 "void a(){\n"
16748 "\t\t// line starts with '\t'\n"
16749 "};",
16750 Tab);
16752 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16753 verifyFormat("void a() {\n"
16754 " // line starts with '\t'\n"
16755 "};",
16756 "void a(){\n"
16757 "\t// line starts with '\t'\n"
16758 "};",
16759 Tab);
16761 verifyFormat("void a() {\n"
16762 " // line starts with '\t'\n"
16763 "};",
16764 "void a(){\n"
16765 "\t\t// line starts with '\t'\n"
16766 "};",
16767 Tab);
16769 Tab.UseTab = FormatStyle::UT_Always;
16770 verifyFormat("void a() {\n"
16771 "// line starts with '\t'\n"
16772 "};",
16773 "void a(){\n"
16774 "\t// line starts with '\t'\n"
16775 "};",
16776 Tab);
16778 verifyFormat("void a() {\n"
16779 "// line starts with '\t'\n"
16780 "};",
16781 "void a(){\n"
16782 "\t\t// line starts with '\t'\n"
16783 "};",
16784 Tab);
16787 TEST_F(FormatTest, CalculatesOriginalColumn) {
16788 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16789 "q\"; /* some\n"
16790 " comment */",
16791 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16792 "q\"; /* some\n"
16793 " comment */");
16794 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16795 "/* some\n"
16796 " comment */",
16797 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16798 " /* some\n"
16799 " comment */");
16800 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16801 "qqq\n"
16802 "/* some\n"
16803 " comment */",
16804 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16805 "qqq\n"
16806 " /* some\n"
16807 " comment */");
16808 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16809 "wwww; /* some\n"
16810 " comment */",
16811 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16812 "wwww; /* some\n"
16813 " comment */");
16816 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
16817 FormatStyle NoSpace = getLLVMStyle();
16818 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
16820 verifyFormat("while(true)\n"
16821 " continue;",
16822 NoSpace);
16823 verifyFormat("for(;;)\n"
16824 " continue;",
16825 NoSpace);
16826 verifyFormat("if(true)\n"
16827 " f();\n"
16828 "else if(true)\n"
16829 " f();",
16830 NoSpace);
16831 verifyFormat("do {\n"
16832 " do_something();\n"
16833 "} while(something());",
16834 NoSpace);
16835 verifyFormat("switch(x) {\n"
16836 "default:\n"
16837 " break;\n"
16838 "}",
16839 NoSpace);
16840 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
16841 verifyFormat("size_t x = sizeof(x);", NoSpace);
16842 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
16843 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
16844 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
16845 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
16846 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
16847 verifyFormat("alignas(128) char a[128];", NoSpace);
16848 verifyFormat("size_t x = alignof(MyType);", NoSpace);
16849 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
16850 verifyFormat("int f() throw(Deprecated);", NoSpace);
16851 verifyFormat("typedef void (*cb)(int);", NoSpace);
16852 verifyFormat("T A::operator()();", NoSpace);
16853 verifyFormat("X A::operator++(T);", NoSpace);
16854 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
16855 verifyFormat("#if (foo || bar) && baz\n"
16856 "#elif ((a || b) && c) || d\n"
16857 "#endif",
16858 NoSpace);
16860 FormatStyle Space = getLLVMStyle();
16861 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
16863 verifyFormat("int f ();", Space);
16864 verifyFormat("bool operator< ();", Space);
16865 verifyFormat("bool operator> ();", Space);
16866 verifyFormat("void f (int a, T b) {\n"
16867 " while (true)\n"
16868 " continue;\n"
16869 "}",
16870 Space);
16871 verifyFormat("if (true)\n"
16872 " f ();\n"
16873 "else if (true)\n"
16874 " f ();",
16875 Space);
16876 verifyFormat("do {\n"
16877 " do_something ();\n"
16878 "} while (something ());",
16879 Space);
16880 verifyFormat("switch (x) {\n"
16881 "default:\n"
16882 " break;\n"
16883 "}",
16884 Space);
16885 verifyFormat("A::A () : a (1) {}", Space);
16886 verifyFormat("void f () __attribute__ ((asdf));", Space);
16887 verifyFormat("*(&a + 1);\n"
16888 "&((&a)[1]);\n"
16889 "a[(b + c) * d];\n"
16890 "(((a + 1) * 2) + 3) * 4;",
16891 Space);
16892 verifyFormat("#define A(x) x", Space);
16893 verifyFormat("#define A (x) x", Space);
16894 verifyFormat("#if defined(x)\n"
16895 "#endif",
16896 Space);
16897 verifyFormat("auto i = std::make_unique<int> (5);", Space);
16898 verifyFormat("size_t x = sizeof (x);", Space);
16899 verifyFormat("auto f (int x) -> decltype (x);", Space);
16900 verifyFormat("auto f (int x) -> typeof (x);", Space);
16901 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
16902 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
16903 verifyFormat("int f (T x) noexcept (x.create ());", Space);
16904 verifyFormat("alignas (128) char a[128];", Space);
16905 verifyFormat("size_t x = alignof (MyType);", Space);
16906 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
16907 verifyFormat("int f () throw (Deprecated);", Space);
16908 verifyFormat("typedef void (*cb) (int);", Space);
16909 verifyFormat("T A::operator() ();", Space);
16910 verifyFormat("X A::operator++ (T);", Space);
16911 verifyFormat("auto lambda = [] () { return 0; };", Space);
16912 verifyFormat("int x = int (y);", Space);
16913 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
16914 verifyFormat("__builtin_LINE ()", Space);
16915 verifyFormat("__builtin_UNKNOWN ()", Space);
16917 FormatStyle SomeSpace = getLLVMStyle();
16918 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
16920 verifyFormat("[]() -> float {}", SomeSpace);
16921 verifyFormat("[] (auto foo) {}", SomeSpace);
16922 verifyFormat("[foo]() -> int {}", SomeSpace);
16923 verifyFormat("int f();", SomeSpace);
16924 verifyFormat("void f (int a, T b) {\n"
16925 " while (true)\n"
16926 " continue;\n"
16927 "}",
16928 SomeSpace);
16929 verifyFormat("if (true)\n"
16930 " f();\n"
16931 "else if (true)\n"
16932 " f();",
16933 SomeSpace);
16934 verifyFormat("do {\n"
16935 " do_something();\n"
16936 "} while (something());",
16937 SomeSpace);
16938 verifyFormat("switch (x) {\n"
16939 "default:\n"
16940 " break;\n"
16941 "}",
16942 SomeSpace);
16943 verifyFormat("A::A() : a (1) {}", SomeSpace);
16944 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
16945 verifyFormat("*(&a + 1);\n"
16946 "&((&a)[1]);\n"
16947 "a[(b + c) * d];\n"
16948 "(((a + 1) * 2) + 3) * 4;",
16949 SomeSpace);
16950 verifyFormat("#define A(x) x", SomeSpace);
16951 verifyFormat("#define A (x) x", SomeSpace);
16952 verifyFormat("#if defined(x)\n"
16953 "#endif",
16954 SomeSpace);
16955 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
16956 verifyFormat("size_t x = sizeof (x);", SomeSpace);
16957 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
16958 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
16959 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
16960 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
16961 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
16962 verifyFormat("alignas (128) char a[128];", SomeSpace);
16963 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
16964 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16965 SomeSpace);
16966 verifyFormat("int f() throw (Deprecated);", SomeSpace);
16967 verifyFormat("typedef void (*cb) (int);", SomeSpace);
16968 verifyFormat("T A::operator()();", SomeSpace);
16969 verifyFormat("X A::operator++ (T);", SomeSpace);
16970 verifyFormat("int x = int (y);", SomeSpace);
16971 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
16973 FormatStyle SpaceControlStatements = getLLVMStyle();
16974 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16975 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
16977 verifyFormat("while (true)\n"
16978 " continue;",
16979 SpaceControlStatements);
16980 verifyFormat("if (true)\n"
16981 " f();\n"
16982 "else if (true)\n"
16983 " f();",
16984 SpaceControlStatements);
16985 verifyFormat("for (;;) {\n"
16986 " do_something();\n"
16987 "}",
16988 SpaceControlStatements);
16989 verifyFormat("do {\n"
16990 " do_something();\n"
16991 "} while (something());",
16992 SpaceControlStatements);
16993 verifyFormat("switch (x) {\n"
16994 "default:\n"
16995 " break;\n"
16996 "}",
16997 SpaceControlStatements);
16999 FormatStyle SpaceFuncDecl = getLLVMStyle();
17000 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17001 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
17003 verifyFormat("int f ();", SpaceFuncDecl);
17004 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
17005 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
17006 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
17007 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
17008 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
17009 verifyFormat("#define A(x) x", SpaceFuncDecl);
17010 verifyFormat("#define A (x) x", SpaceFuncDecl);
17011 verifyFormat("#if defined(x)\n"
17012 "#endif",
17013 SpaceFuncDecl);
17014 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
17015 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
17016 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
17017 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
17018 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
17019 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
17020 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
17021 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
17022 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
17023 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17024 SpaceFuncDecl);
17025 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
17026 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
17027 verifyFormat("T A::operator()();", SpaceFuncDecl);
17028 verifyFormat("X A::operator++(T);", SpaceFuncDecl);
17029 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
17030 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
17031 verifyFormat("int x = int(y);", SpaceFuncDecl);
17032 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17033 SpaceFuncDecl);
17035 FormatStyle SpaceFuncDef = getLLVMStyle();
17036 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17037 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
17039 verifyFormat("int f();", SpaceFuncDef);
17040 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
17041 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
17042 verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
17043 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
17044 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
17045 verifyFormat("#define A(x) x", SpaceFuncDef);
17046 verifyFormat("#define A (x) x", SpaceFuncDef);
17047 verifyFormat("#if defined(x)\n"
17048 "#endif",
17049 SpaceFuncDef);
17050 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
17051 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
17052 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
17053 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
17054 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
17055 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
17056 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
17057 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
17058 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
17059 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17060 SpaceFuncDef);
17061 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
17062 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
17063 verifyFormat("T A::operator()();", SpaceFuncDef);
17064 verifyFormat("X A::operator++(T);", SpaceFuncDef);
17065 verifyFormat("T A::operator()() {}", SpaceFuncDef);
17066 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
17067 verifyFormat("int x = int(y);", SpaceFuncDef);
17068 verifyFormat("void foo::bar () {}", SpaceFuncDef);
17069 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17070 SpaceFuncDef);
17072 FormatStyle SpaceIfMacros = getLLVMStyle();
17073 SpaceIfMacros.IfMacros.clear();
17074 SpaceIfMacros.IfMacros.push_back("MYIF");
17075 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17076 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
17077 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
17078 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
17079 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
17081 FormatStyle SpaceForeachMacros = getLLVMStyle();
17082 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
17083 FormatStyle::SBS_Never);
17084 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
17085 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17086 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
17087 verifyFormat("for (;;) {\n"
17088 "}",
17089 SpaceForeachMacros);
17090 verifyFormat("foreach (Item *item, itemlist) {\n"
17091 "}",
17092 SpaceForeachMacros);
17093 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17094 "}",
17095 SpaceForeachMacros);
17096 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17097 "}",
17098 SpaceForeachMacros);
17099 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
17101 FormatStyle SomeSpace2 = getLLVMStyle();
17102 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17103 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
17104 verifyFormat("[]() -> float {}", SomeSpace2);
17105 verifyFormat("[] (auto foo) {}", SomeSpace2);
17106 verifyFormat("[foo]() -> int {}", SomeSpace2);
17107 verifyFormat("int f();", SomeSpace2);
17108 verifyFormat("void f (int a, T b) {\n"
17109 " while (true)\n"
17110 " continue;\n"
17111 "}",
17112 SomeSpace2);
17113 verifyFormat("if (true)\n"
17114 " f();\n"
17115 "else if (true)\n"
17116 " f();",
17117 SomeSpace2);
17118 verifyFormat("do {\n"
17119 " do_something();\n"
17120 "} while (something());",
17121 SomeSpace2);
17122 verifyFormat("switch (x) {\n"
17123 "default:\n"
17124 " break;\n"
17125 "}",
17126 SomeSpace2);
17127 verifyFormat("A::A() : a (1) {}", SomeSpace2);
17128 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
17129 verifyFormat("*(&a + 1);\n"
17130 "&((&a)[1]);\n"
17131 "a[(b + c) * d];\n"
17132 "(((a + 1) * 2) + 3) * 4;",
17133 SomeSpace2);
17134 verifyFormat("#define A(x) x", SomeSpace2);
17135 verifyFormat("#define A (x) x", SomeSpace2);
17136 verifyFormat("#if defined(x)\n"
17137 "#endif",
17138 SomeSpace2);
17139 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
17140 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
17141 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
17142 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
17143 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
17144 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
17145 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
17146 verifyFormat("alignas (128) char a[128];", SomeSpace2);
17147 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
17148 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17149 SomeSpace2);
17150 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
17151 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
17152 verifyFormat("T A::operator()();", SomeSpace2);
17153 verifyFormat("X A::operator++ (T);", SomeSpace2);
17154 verifyFormat("int x = int (y);", SomeSpace2);
17155 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
17157 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
17158 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17159 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17160 .AfterOverloadedOperator = true;
17162 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
17163 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
17164 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
17165 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17167 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17168 .AfterOverloadedOperator = false;
17170 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
17171 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
17172 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
17173 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17175 auto SpaceAfterRequires = getLLVMStyle();
17176 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17177 EXPECT_FALSE(
17178 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
17179 EXPECT_FALSE(
17180 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
17181 verifyFormat("void f(auto x)\n"
17182 " requires requires(int i) { x + i; }\n"
17183 "{}",
17184 SpaceAfterRequires);
17185 verifyFormat("void f(auto x)\n"
17186 " requires(requires(int i) { x + i; })\n"
17187 "{}",
17188 SpaceAfterRequires);
17189 verifyFormat("if (requires(int i) { x + i; })\n"
17190 " return;",
17191 SpaceAfterRequires);
17192 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17193 verifyFormat("template <typename T>\n"
17194 " requires(Foo<T>)\n"
17195 "class Bar;",
17196 SpaceAfterRequires);
17198 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17199 verifyFormat("void f(auto x)\n"
17200 " requires requires(int i) { x + i; }\n"
17201 "{}",
17202 SpaceAfterRequires);
17203 verifyFormat("void f(auto x)\n"
17204 " requires (requires(int i) { x + i; })\n"
17205 "{}",
17206 SpaceAfterRequires);
17207 verifyFormat("if (requires(int i) { x + i; })\n"
17208 " return;",
17209 SpaceAfterRequires);
17210 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17211 verifyFormat("template <typename T>\n"
17212 " requires (Foo<T>)\n"
17213 "class Bar;",
17214 SpaceAfterRequires);
17216 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
17217 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
17218 verifyFormat("void f(auto x)\n"
17219 " requires requires (int i) { x + i; }\n"
17220 "{}",
17221 SpaceAfterRequires);
17222 verifyFormat("void f(auto x)\n"
17223 " requires(requires (int i) { x + i; })\n"
17224 "{}",
17225 SpaceAfterRequires);
17226 verifyFormat("if (requires (int i) { x + i; })\n"
17227 " return;",
17228 SpaceAfterRequires);
17229 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17230 verifyFormat("template <typename T>\n"
17231 " requires(Foo<T>)\n"
17232 "class Bar;",
17233 SpaceAfterRequires);
17235 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17236 verifyFormat("void f(auto x)\n"
17237 " requires requires (int i) { x + i; }\n"
17238 "{}",
17239 SpaceAfterRequires);
17240 verifyFormat("void f(auto x)\n"
17241 " requires (requires (int i) { x + i; })\n"
17242 "{}",
17243 SpaceAfterRequires);
17244 verifyFormat("if (requires (int i) { x + i; })\n"
17245 " return;",
17246 SpaceAfterRequires);
17247 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17248 verifyFormat("template <typename T>\n"
17249 " requires (Foo<T>)\n"
17250 "class Bar;",
17251 SpaceAfterRequires);
17254 TEST_F(FormatTest, SpaceAfterLogicalNot) {
17255 FormatStyle Spaces = getLLVMStyle();
17256 Spaces.SpaceAfterLogicalNot = true;
17258 verifyFormat("bool x = ! y", Spaces);
17259 verifyFormat("if (! isFailure())", Spaces);
17260 verifyFormat("if (! (a && b))", Spaces);
17261 verifyFormat("\"Error!\"", Spaces);
17262 verifyFormat("! ! x", Spaces);
17265 TEST_F(FormatTest, ConfigurableSpacesInParens) {
17266 FormatStyle Spaces = getLLVMStyle();
17268 verifyFormat("do_something(::globalVar);", Spaces);
17269 verifyFormat("call(x, y, z);", Spaces);
17270 verifyFormat("call();", Spaces);
17271 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17272 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17273 Spaces);
17274 verifyFormat("while ((bool)1)\n"
17275 " continue;",
17276 Spaces);
17277 verifyFormat("for (;;)\n"
17278 " continue;",
17279 Spaces);
17280 verifyFormat("if (true)\n"
17281 " f();\n"
17282 "else if (true)\n"
17283 " f();",
17284 Spaces);
17285 verifyFormat("do {\n"
17286 " do_something((int)i);\n"
17287 "} while (something());",
17288 Spaces);
17289 verifyFormat("switch (x) {\n"
17290 "default:\n"
17291 " break;\n"
17292 "}",
17293 Spaces);
17294 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17295 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17296 verifyFormat("void f() __attribute__((asdf));", Spaces);
17297 verifyFormat("x = (int32)y;", Spaces);
17298 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces);
17299 verifyFormat("decltype(x) y = 42;", Spaces);
17300 verifyFormat("decltype((x)) y = z;", Spaces);
17301 verifyFormat("decltype((foo())) a = foo();", Spaces);
17302 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17303 verifyFormat("if ((x - y) && (a ^ b))\n"
17304 " f();",
17305 Spaces);
17306 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17307 " foo(i);",
17308 Spaces);
17309 verifyFormat("switch (x / (y + z)) {\n"
17310 "default:\n"
17311 " break;\n"
17312 "}",
17313 Spaces);
17315 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17316 Spaces.SpacesInParensOptions = {};
17317 Spaces.SpacesInParensOptions.Other = true;
17319 EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
17320 verifyFormat("if (a)\n"
17321 " return;",
17322 Spaces);
17324 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17325 verifyFormat("do_something( ::globalVar );", Spaces);
17326 verifyFormat("call( x, y, z );", Spaces);
17327 verifyFormat("call();", Spaces);
17328 verifyFormat("std::function<void( int, int )> callback;", Spaces);
17329 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17330 Spaces);
17331 verifyFormat("while ( (bool)1 )\n"
17332 " continue;",
17333 Spaces);
17334 verifyFormat("for ( ;; )\n"
17335 " continue;",
17336 Spaces);
17337 verifyFormat("if ( true )\n"
17338 " f();\n"
17339 "else if ( true )\n"
17340 " f();",
17341 Spaces);
17342 verifyFormat("do {\n"
17343 " do_something( (int)i );\n"
17344 "} while ( something() );",
17345 Spaces);
17346 verifyFormat("switch ( x ) {\n"
17347 "default:\n"
17348 " break;\n"
17349 "}",
17350 Spaces);
17351 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17352 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17353 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17354 verifyFormat("x = (int32)y;", Spaces);
17355 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces);
17356 verifyFormat("decltype( x ) y = 42;", Spaces);
17357 verifyFormat("decltype( ( x ) ) y = z;", Spaces);
17358 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces);
17359 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces);
17360 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17361 " f();",
17362 Spaces);
17363 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17364 " foo( i );",
17365 Spaces);
17366 verifyFormat("switch ( x / ( y + z ) ) {\n"
17367 "default:\n"
17368 " break;\n"
17369 "}",
17370 Spaces);
17372 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17373 Spaces.SpacesInParensOptions = {};
17374 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17375 verifyFormat("Type *A = ( Type * )P;", Spaces);
17376 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17377 verifyFormat("x = ( int32 )y;", Spaces);
17378 verifyFormat("throw ( int32 )x;", Spaces);
17379 verifyFormat("int a = ( int )(2.0f);", Spaces);
17380 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17381 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17382 verifyFormat("#define x (( int )-1)", Spaces);
17383 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17385 // Run the first set of tests again with:
17386 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17387 Spaces.SpacesInParensOptions = {};
17388 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17389 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17390 verifyFormat("call(x, y, z);", Spaces);
17391 verifyFormat("call( );", Spaces);
17392 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17393 verifyFormat("while (( bool )1)\n"
17394 " continue;",
17395 Spaces);
17396 verifyFormat("for (;;)\n"
17397 " continue;",
17398 Spaces);
17399 verifyFormat("if (true)\n"
17400 " f( );\n"
17401 "else if (true)\n"
17402 " f( );",
17403 Spaces);
17404 verifyFormat("do {\n"
17405 " do_something(( int )i);\n"
17406 "} while (something( ));",
17407 Spaces);
17408 verifyFormat("switch (x) {\n"
17409 "default:\n"
17410 " break;\n"
17411 "}",
17412 Spaces);
17413 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17414 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17415 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17416 verifyFormat("x = ( int32 )y;", Spaces);
17417 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17418 verifyFormat("decltype(x) y = 42;", Spaces);
17419 verifyFormat("decltype((x)) y = z;", Spaces);
17420 verifyFormat("decltype((foo( ))) a = foo( );", Spaces);
17421 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17422 verifyFormat("if ((x - y) && (a ^ b))\n"
17423 " f( );",
17424 Spaces);
17425 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17426 " foo(i);",
17427 Spaces);
17428 verifyFormat("switch (x / (y + z)) {\n"
17429 "default:\n"
17430 " break;\n"
17431 "}",
17432 Spaces);
17434 // Run the first set of tests again with:
17435 Spaces.SpaceAfterCStyleCast = true;
17436 verifyFormat("call(x, y, z);", Spaces);
17437 verifyFormat("call( );", Spaces);
17438 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17439 verifyFormat("while (( bool ) 1)\n"
17440 " continue;",
17441 Spaces);
17442 verifyFormat("for (;;)\n"
17443 " continue;",
17444 Spaces);
17445 verifyFormat("if (true)\n"
17446 " f( );\n"
17447 "else if (true)\n"
17448 " f( );",
17449 Spaces);
17450 verifyFormat("do {\n"
17451 " do_something(( int ) i);\n"
17452 "} while (something( ));",
17453 Spaces);
17454 verifyFormat("switch (x) {\n"
17455 "default:\n"
17456 " break;\n"
17457 "}",
17458 Spaces);
17459 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
17460 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
17461 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
17462 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
17463 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
17464 verifyFormat("throw ( int32 ) x;", Spaces);
17465 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17466 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17467 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17469 // Run subset of tests again with:
17470 Spaces.SpacesInParensOptions.InCStyleCasts = false;
17471 Spaces.SpaceAfterCStyleCast = true;
17472 verifyFormat("while ((bool) 1)\n"
17473 " continue;",
17474 Spaces);
17475 verifyFormat("do {\n"
17476 " do_something((int) i);\n"
17477 "} while (something( ));",
17478 Spaces);
17480 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
17481 verifyFormat("size_t idx = (size_t) a;", Spaces);
17482 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
17483 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17484 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17485 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17486 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17487 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
17488 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
17489 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
17490 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
17491 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
17492 verifyFormat("throw (int32) x;", Spaces);
17493 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17494 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17495 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17497 Spaces.ColumnLimit = 80;
17498 Spaces.IndentWidth = 4;
17499 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17500 verifyFormat("void foo( ) {\n"
17501 " size_t foo = (*(function))(\n"
17502 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17503 "BarrrrrrrrrrrrLong,\n"
17504 " FoooooooooLooooong);\n"
17505 "}",
17506 Spaces);
17507 Spaces.SpaceAfterCStyleCast = false;
17508 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17509 verifyFormat("size_t idx = (size_t)a;", Spaces);
17510 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17511 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17512 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17513 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17514 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17516 verifyFormat("void foo( ) {\n"
17517 " size_t foo = (*(function))(\n"
17518 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17519 "BarrrrrrrrrrrrLong,\n"
17520 " FoooooooooLooooong);\n"
17521 "}",
17522 Spaces);
17524 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
17525 verifyFormat("void foo( ) {\n"
17526 " size_t foo = (*(function))(\n"
17527 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17528 "BarrrrrrrrrrrrLong,\n"
17529 " FoooooooooLooooong\n"
17530 " );\n"
17531 "}",
17532 Spaces);
17533 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17534 verifyFormat("size_t idx = (size_t)a;", Spaces);
17535 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17536 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17537 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17538 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17539 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17541 // Check ExceptDoubleParentheses spaces
17542 Spaces.IndentWidth = 2;
17543 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17544 Spaces.SpacesInParensOptions = {};
17545 Spaces.SpacesInParensOptions.Other = true;
17546 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17547 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
17548 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces);
17549 verifyFormat("void f() __attribute__(( asdf ));", Spaces);
17550 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces);
17551 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces);
17552 verifyFormat("class __declspec( dllimport ) X {};", Spaces);
17553 verifyFormat("class __declspec(( dllimport )) X {};", Spaces);
17554 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces);
17555 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces);
17556 verifyFormat("decltype( x ) y = 42;", Spaces);
17557 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces);
17558 verifyFormat("if (( i = j ))\n"
17559 " do_something( i );",
17560 Spaces);
17562 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17563 Spaces.SpacesInParensOptions = {};
17564 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17565 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17566 verifyFormat("while ( (bool)1 )\n"
17567 " continue;",
17568 Spaces);
17569 verifyFormat("while ((i = j))\n"
17570 " continue;",
17571 Spaces);
17572 verifyFormat("do {\n"
17573 " do_something((int)i);\n"
17574 "} while ( something() );",
17575 Spaces);
17576 verifyFormat("do {\n"
17577 " do_something((int)i);\n"
17578 "} while ((i = i + 1));",
17579 Spaces);
17580 verifyFormat("if ( (x - y) && (a ^ b) )\n"
17581 " f();",
17582 Spaces);
17583 verifyFormat("if ((i = j))\n"
17584 " do_something(i);",
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 verifyFormat("if constexpr ((a = b))\n"
17595 " c;",
17596 Spaces);
17599 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
17600 verifyFormat("int a[5];");
17601 verifyFormat("a[3] += 42;");
17603 FormatStyle Spaces = getLLVMStyle();
17604 Spaces.SpacesInSquareBrackets = true;
17605 // Not lambdas.
17606 verifyFormat("int a[ 5 ];", Spaces);
17607 verifyFormat("a[ 3 ] += 42;", Spaces);
17608 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
17609 verifyFormat("double &operator[](int i) { return 0; }\n"
17610 "int i;",
17611 Spaces);
17612 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
17613 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
17614 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
17615 // Lambdas.
17616 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
17617 verifyFormat("return [ i, args... ] {};", Spaces);
17618 verifyFormat("int foo = [ &bar ]() {};", Spaces);
17619 verifyFormat("int foo = [ = ]() {};", Spaces);
17620 verifyFormat("int foo = [ & ]() {};", Spaces);
17621 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
17622 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
17625 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
17626 FormatStyle NoSpaceStyle = getLLVMStyle();
17627 verifyFormat("int a[5];", NoSpaceStyle);
17628 verifyFormat("a[3] += 42;", NoSpaceStyle);
17630 verifyFormat("int a[1];", NoSpaceStyle);
17631 verifyFormat("int 1 [a];", NoSpaceStyle);
17632 verifyFormat("int a[1][2];", NoSpaceStyle);
17633 verifyFormat("a[7] = 5;", NoSpaceStyle);
17634 verifyFormat("int a = (f())[23];", NoSpaceStyle);
17635 verifyFormat("f([] {})", NoSpaceStyle);
17637 FormatStyle Space = getLLVMStyle();
17638 Space.SpaceBeforeSquareBrackets = true;
17639 verifyFormat("int c = []() -> int { return 2; }();", Space);
17640 verifyFormat("return [i, args...] {};", Space);
17642 verifyFormat("int a [5];", Space);
17643 verifyFormat("a [3] += 42;", Space);
17644 verifyFormat("constexpr char hello []{\"hello\"};", Space);
17645 verifyFormat("double &operator[](int i) { return 0; }\n"
17646 "int i;",
17647 Space);
17648 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
17649 verifyFormat("int i = a [a][a]->f();", Space);
17650 verifyFormat("int i = (*b) [a]->f();", Space);
17652 verifyFormat("int a [1];", Space);
17653 verifyFormat("int 1 [a];", Space);
17654 verifyFormat("int a [1][2];", Space);
17655 verifyFormat("a [7] = 5;", Space);
17656 verifyFormat("int a = (f()) [23];", Space);
17657 verifyFormat("f([] {})", Space);
17660 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
17661 verifyFormat("int a = 5;");
17662 verifyFormat("a += 42;");
17663 verifyFormat("a or_eq 8;");
17664 verifyFormat("xor = foo;");
17666 FormatStyle Spaces = getLLVMStyle();
17667 Spaces.SpaceBeforeAssignmentOperators = false;
17668 verifyFormat("int a= 5;", Spaces);
17669 verifyFormat("a+= 42;", Spaces);
17670 verifyFormat("a or_eq 8;", Spaces);
17671 verifyFormat("xor= foo;", Spaces);
17674 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
17675 verifyFormat("class Foo : public Bar {};");
17676 verifyFormat("Foo::Foo() : foo(1) {}");
17677 verifyFormat("for (auto a : b) {\n}");
17678 verifyFormat("int x = a ? b : c;");
17679 verifyFormat("{\n"
17680 "label0:\n"
17681 " int x = 0;\n"
17682 "}");
17683 verifyFormat("switch (x) {\n"
17684 "case 1:\n"
17685 "default:\n"
17686 "}");
17687 verifyFormat("switch (allBraces) {\n"
17688 "case 1: {\n"
17689 " break;\n"
17690 "}\n"
17691 "case 2: {\n"
17692 " [[fallthrough]];\n"
17693 "}\n"
17694 "default: {\n"
17695 " break;\n"
17696 "}\n"
17697 "}");
17699 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
17700 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
17701 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
17702 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
17703 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
17704 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
17705 verifyFormat("{\n"
17706 "label1:\n"
17707 " int x = 0;\n"
17708 "}",
17709 CtorInitializerStyle);
17710 verifyFormat("switch (x) {\n"
17711 "case 1:\n"
17712 "default:\n"
17713 "}",
17714 CtorInitializerStyle);
17715 verifyFormat("switch (allBraces) {\n"
17716 "case 1: {\n"
17717 " break;\n"
17718 "}\n"
17719 "case 2: {\n"
17720 " [[fallthrough]];\n"
17721 "}\n"
17722 "default: {\n"
17723 " break;\n"
17724 "}\n"
17725 "}",
17726 CtorInitializerStyle);
17727 CtorInitializerStyle.BreakConstructorInitializers =
17728 FormatStyle::BCIS_AfterColon;
17729 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17730 " aaaaaaaaaaaaaaaa(1),\n"
17731 " bbbbbbbbbbbbbbbb(2) {}",
17732 CtorInitializerStyle);
17733 CtorInitializerStyle.BreakConstructorInitializers =
17734 FormatStyle::BCIS_BeforeComma;
17735 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17736 " : aaaaaaaaaaaaaaaa(1)\n"
17737 " , bbbbbbbbbbbbbbbb(2) {}",
17738 CtorInitializerStyle);
17739 CtorInitializerStyle.BreakConstructorInitializers =
17740 FormatStyle::BCIS_BeforeColon;
17741 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17742 " : aaaaaaaaaaaaaaaa(1),\n"
17743 " bbbbbbbbbbbbbbbb(2) {}",
17744 CtorInitializerStyle);
17745 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
17746 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17747 ": aaaaaaaaaaaaaaaa(1),\n"
17748 " bbbbbbbbbbbbbbbb(2) {}",
17749 CtorInitializerStyle);
17751 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
17752 InheritanceStyle.SpaceBeforeInheritanceColon = false;
17753 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
17754 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
17755 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
17756 verifyFormat("int x = a ? b : c;", InheritanceStyle);
17757 verifyFormat("{\n"
17758 "label2:\n"
17759 " int x = 0;\n"
17760 "}",
17761 InheritanceStyle);
17762 verifyFormat("switch (x) {\n"
17763 "case 1:\n"
17764 "default:\n"
17765 "}",
17766 InheritanceStyle);
17767 verifyFormat("switch (allBraces) {\n"
17768 "case 1: {\n"
17769 " break;\n"
17770 "}\n"
17771 "case 2: {\n"
17772 " [[fallthrough]];\n"
17773 "}\n"
17774 "default: {\n"
17775 " break;\n"
17776 "}\n"
17777 "}",
17778 InheritanceStyle);
17779 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
17780 verifyFormat("class Foooooooooooooooooooooo\n"
17781 " : public aaaaaaaaaaaaaaaaaa,\n"
17782 " public bbbbbbbbbbbbbbbbbb {\n"
17783 "}",
17784 InheritanceStyle);
17785 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
17786 verifyFormat("class Foooooooooooooooooooooo:\n"
17787 " public aaaaaaaaaaaaaaaaaa,\n"
17788 " public bbbbbbbbbbbbbbbbbb {\n"
17789 "}",
17790 InheritanceStyle);
17791 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
17792 verifyFormat("class Foooooooooooooooooooooo\n"
17793 " : public aaaaaaaaaaaaaaaaaa\n"
17794 " , public bbbbbbbbbbbbbbbbbb {\n"
17795 "}",
17796 InheritanceStyle);
17797 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17798 verifyFormat("class Foooooooooooooooooooooo\n"
17799 " : public aaaaaaaaaaaaaaaaaa,\n"
17800 " public bbbbbbbbbbbbbbbbbb {\n"
17801 "}",
17802 InheritanceStyle);
17803 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
17804 verifyFormat("class Foooooooooooooooooooooo\n"
17805 ": public aaaaaaaaaaaaaaaaaa,\n"
17806 " public bbbbbbbbbbbbbbbbbb {}",
17807 InheritanceStyle);
17809 FormatStyle ForLoopStyle = getLLVMStyle();
17810 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
17811 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
17812 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
17813 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
17814 verifyFormat("int x = a ? b : c;", ForLoopStyle);
17815 verifyFormat("{\n"
17816 "label2:\n"
17817 " int x = 0;\n"
17818 "}",
17819 ForLoopStyle);
17820 verifyFormat("switch (x) {\n"
17821 "case 1:\n"
17822 "default:\n"
17823 "}",
17824 ForLoopStyle);
17825 verifyFormat("switch (allBraces) {\n"
17826 "case 1: {\n"
17827 " break;\n"
17828 "}\n"
17829 "case 2: {\n"
17830 " [[fallthrough]];\n"
17831 "}\n"
17832 "default: {\n"
17833 " break;\n"
17834 "}\n"
17835 "}",
17836 ForLoopStyle);
17838 FormatStyle CaseStyle = getLLVMStyle();
17839 CaseStyle.SpaceBeforeCaseColon = true;
17840 verifyFormat("class Foo : public Bar {};", CaseStyle);
17841 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
17842 verifyFormat("for (auto a : b) {\n}", CaseStyle);
17843 verifyFormat("int x = a ? b : c;", CaseStyle);
17844 verifyFormat("switch (x) {\n"
17845 "case 1 :\n"
17846 "default :\n"
17847 "}",
17848 CaseStyle);
17849 verifyFormat("switch (allBraces) {\n"
17850 "case 1 : {\n"
17851 " break;\n"
17852 "}\n"
17853 "case 2 : {\n"
17854 " [[fallthrough]];\n"
17855 "}\n"
17856 "default : {\n"
17857 " break;\n"
17858 "}\n"
17859 "}",
17860 CaseStyle);
17861 // Goto labels should not be affected.
17862 verifyFormat("switch (x) {\n"
17863 "goto_label:\n"
17864 "default :\n"
17865 "}",
17866 CaseStyle);
17867 verifyFormat("switch (x) {\n"
17868 "goto_label: { break; }\n"
17869 "default : {\n"
17870 " break;\n"
17871 "}\n"
17872 "}",
17873 CaseStyle);
17875 FormatStyle NoSpaceStyle = getLLVMStyle();
17876 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
17877 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17878 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
17879 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17880 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
17881 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
17882 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
17883 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
17884 verifyFormat("{\n"
17885 "label3:\n"
17886 " int x = 0;\n"
17887 "}",
17888 NoSpaceStyle);
17889 verifyFormat("switch (x) {\n"
17890 "case 1:\n"
17891 "default:\n"
17892 "}",
17893 NoSpaceStyle);
17894 verifyFormat("switch (allBraces) {\n"
17895 "case 1: {\n"
17896 " break;\n"
17897 "}\n"
17898 "case 2: {\n"
17899 " [[fallthrough]];\n"
17900 "}\n"
17901 "default: {\n"
17902 " break;\n"
17903 "}\n"
17904 "}",
17905 NoSpaceStyle);
17907 FormatStyle InvertedSpaceStyle = getLLVMStyle();
17908 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
17909 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17910 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
17911 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17912 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
17913 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
17914 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
17915 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
17916 verifyFormat("{\n"
17917 "label3:\n"
17918 " int x = 0;\n"
17919 "}",
17920 InvertedSpaceStyle);
17921 verifyFormat("switch (x) {\n"
17922 "case 1 :\n"
17923 "case 2 : {\n"
17924 " break;\n"
17925 "}\n"
17926 "default :\n"
17927 " break;\n"
17928 "}",
17929 InvertedSpaceStyle);
17930 verifyFormat("switch (allBraces) {\n"
17931 "case 1 : {\n"
17932 " break;\n"
17933 "}\n"
17934 "case 2 : {\n"
17935 " [[fallthrough]];\n"
17936 "}\n"
17937 "default : {\n"
17938 " break;\n"
17939 "}\n"
17940 "}",
17941 InvertedSpaceStyle);
17944 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
17945 FormatStyle Style = getLLVMStyle();
17947 Style.PointerAlignment = FormatStyle::PAS_Left;
17948 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17949 verifyFormat("void* const* x = NULL;", Style);
17951 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17952 do { \
17953 Style.PointerAlignment = FormatStyle::Pointers; \
17954 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17955 verifyFormat(Code, Style); \
17956 } while (false)
17958 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
17959 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
17960 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
17962 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
17963 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
17964 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
17966 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
17967 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
17968 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
17970 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
17971 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
17972 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
17974 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
17975 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17976 SAPQ_Default);
17977 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17978 SAPQ_Default);
17980 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
17981 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17982 SAPQ_Before);
17983 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17984 SAPQ_Before);
17986 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
17987 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
17988 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17989 SAPQ_After);
17991 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
17992 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
17993 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
17995 #undef verifyQualifierSpaces
17997 FormatStyle Spaces = getLLVMStyle();
17998 Spaces.AttributeMacros.push_back("qualified");
17999 Spaces.PointerAlignment = FormatStyle::PAS_Right;
18000 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
18001 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
18002 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
18003 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
18004 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
18005 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18006 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
18007 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
18008 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
18009 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18010 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18011 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18013 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
18014 Spaces.PointerAlignment = FormatStyle::PAS_Left;
18015 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
18016 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
18017 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
18018 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
18019 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
18020 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18021 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
18022 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18023 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
18024 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
18025 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
18026 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
18027 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18029 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18030 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
18031 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18032 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
18033 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
18034 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18035 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18036 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18039 TEST_F(FormatTest, AlignConsecutiveMacros) {
18040 FormatStyle Style = getLLVMStyle();
18041 Style.AlignConsecutiveAssignments.Enabled = true;
18042 Style.AlignConsecutiveDeclarations.Enabled = true;
18044 verifyFormat("#define a 3\n"
18045 "#define bbbb 4\n"
18046 "#define ccc (5)",
18047 Style);
18049 verifyFormat("#define f(x) (x * x)\n"
18050 "#define fff(x, y, z) (x * y + z)\n"
18051 "#define ffff(x, y) (x - y)",
18052 Style);
18054 verifyFormat("#define foo(x, y) (x + y)\n"
18055 "#define bar (5, 6)(2 + 2)",
18056 Style);
18058 verifyFormat("#define a 3\n"
18059 "#define bbbb 4\n"
18060 "#define ccc (5)\n"
18061 "#define f(x) (x * x)\n"
18062 "#define fff(x, y, z) (x * y + z)\n"
18063 "#define ffff(x, y) (x - y)",
18064 Style);
18066 Style.AlignConsecutiveMacros.Enabled = true;
18067 verifyFormat("#define a 3\n"
18068 "#define bbbb 4\n"
18069 "#define ccc (5)",
18070 Style);
18072 verifyFormat("#define true 1\n"
18073 "#define false 0",
18074 Style);
18076 verifyFormat("#define f(x) (x * x)\n"
18077 "#define fff(x, y, z) (x * y + z)\n"
18078 "#define ffff(x, y) (x - y)",
18079 Style);
18081 verifyFormat("#define foo(x, y) (x + y)\n"
18082 "#define bar (5, 6)(2 + 2)",
18083 Style);
18085 verifyFormat("#define a 3\n"
18086 "#define bbbb 4\n"
18087 "#define ccc (5)\n"
18088 "#define f(x) (x * x)\n"
18089 "#define fff(x, y, z) (x * y + z)\n"
18090 "#define ffff(x, y) (x - y)",
18091 Style);
18093 verifyFormat("#define a 5\n"
18094 "#define foo(x, y) (x + y)\n"
18095 "#define CCC (6)\n"
18096 "auto lambda = []() {\n"
18097 " auto ii = 0;\n"
18098 " float j = 0;\n"
18099 " return 0;\n"
18100 "};\n"
18101 "int i = 0;\n"
18102 "float i2 = 0;\n"
18103 "auto v = type{\n"
18104 " i = 1, //\n"
18105 " (i = 2), //\n"
18106 " i = 3 //\n"
18107 "};",
18108 Style);
18110 Style.AlignConsecutiveMacros.Enabled = false;
18111 Style.ColumnLimit = 20;
18113 verifyFormat("#define a \\\n"
18114 " \"aabbbbbbbbbbbb\"\n"
18115 "#define D \\\n"
18116 " \"aabbbbbbbbbbbb\" \\\n"
18117 " \"ccddeeeeeeeee\"\n"
18118 "#define B \\\n"
18119 " \"QQQQQQQQQQQQQ\" \\\n"
18120 " \"FFFFFFFFFFFFF\" \\\n"
18121 " \"LLLLLLLL\"",
18122 Style);
18124 Style.AlignConsecutiveMacros.Enabled = true;
18125 verifyFormat("#define a \\\n"
18126 " \"aabbbbbbbbbbbb\"\n"
18127 "#define D \\\n"
18128 " \"aabbbbbbbbbbbb\" \\\n"
18129 " \"ccddeeeeeeeee\"\n"
18130 "#define B \\\n"
18131 " \"QQQQQQQQQQQQQ\" \\\n"
18132 " \"FFFFFFFFFFFFF\" \\\n"
18133 " \"LLLLLLLL\"",
18134 Style);
18136 // Test across comments
18137 Style.MaxEmptyLinesToKeep = 10;
18138 Style.ReflowComments = FormatStyle::RCS_Never;
18139 Style.AlignConsecutiveMacros.AcrossComments = true;
18140 verifyFormat("#define a 3\n"
18141 "// line comment\n"
18142 "#define bbbb 4\n"
18143 "#define ccc (5)",
18144 "#define a 3\n"
18145 "// line comment\n"
18146 "#define bbbb 4\n"
18147 "#define ccc (5)",
18148 Style);
18150 verifyFormat("#define a 3\n"
18151 "/* block comment */\n"
18152 "#define bbbb 4\n"
18153 "#define ccc (5)",
18154 "#define a 3\n"
18155 "/* block comment */\n"
18156 "#define bbbb 4\n"
18157 "#define ccc (5)",
18158 Style);
18160 verifyFormat("#define a 3\n"
18161 "/* multi-line *\n"
18162 " * block comment */\n"
18163 "#define bbbb 4\n"
18164 "#define ccc (5)",
18165 "#define a 3\n"
18166 "/* multi-line *\n"
18167 " * block comment */\n"
18168 "#define bbbb 4\n"
18169 "#define ccc (5)",
18170 Style);
18172 verifyFormat("#define a 3\n"
18173 "// multi-line line comment\n"
18174 "//\n"
18175 "#define bbbb 4\n"
18176 "#define ccc (5)",
18177 "#define a 3\n"
18178 "// multi-line line comment\n"
18179 "//\n"
18180 "#define bbbb 4\n"
18181 "#define ccc (5)",
18182 Style);
18184 verifyFormat("#define a 3\n"
18185 "// empty lines still break.\n"
18186 "\n"
18187 "#define bbbb 4\n"
18188 "#define ccc (5)",
18189 "#define a 3\n"
18190 "// empty lines still break.\n"
18191 "\n"
18192 "#define bbbb 4\n"
18193 "#define ccc (5)",
18194 Style);
18196 // Test across empty lines
18197 Style.AlignConsecutiveMacros.AcrossComments = false;
18198 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
18199 verifyFormat("#define a 3\n"
18200 "\n"
18201 "#define bbbb 4\n"
18202 "#define ccc (5)",
18203 "#define a 3\n"
18204 "\n"
18205 "#define bbbb 4\n"
18206 "#define ccc (5)",
18207 Style);
18209 verifyFormat("#define a 3\n"
18210 "\n"
18211 "\n"
18212 "\n"
18213 "#define bbbb 4\n"
18214 "#define ccc (5)",
18215 "#define a 3\n"
18216 "\n"
18217 "\n"
18218 "\n"
18219 "#define bbbb 4\n"
18220 "#define ccc (5)",
18221 Style);
18223 verifyFormat("#define a 3\n"
18224 "// comments should break alignment\n"
18225 "//\n"
18226 "#define bbbb 4\n"
18227 "#define ccc (5)",
18228 "#define a 3\n"
18229 "// comments should break alignment\n"
18230 "//\n"
18231 "#define bbbb 4\n"
18232 "#define ccc (5)",
18233 Style);
18235 // Test across empty lines and comments
18236 Style.AlignConsecutiveMacros.AcrossComments = true;
18237 verifyFormat("#define a 3\n"
18238 "\n"
18239 "// line comment\n"
18240 "#define bbbb 4\n"
18241 "#define ccc (5)",
18242 Style);
18244 verifyFormat("#define a 3\n"
18245 "\n"
18246 "\n"
18247 "/* multi-line *\n"
18248 " * block comment */\n"
18249 "\n"
18250 "\n"
18251 "#define bbbb 4\n"
18252 "#define ccc (5)",
18253 "#define a 3\n"
18254 "\n"
18255 "\n"
18256 "/* multi-line *\n"
18257 " * block comment */\n"
18258 "\n"
18259 "\n"
18260 "#define bbbb 4\n"
18261 "#define ccc (5)",
18262 Style);
18264 verifyFormat("#define a 3\n"
18265 "\n"
18266 "\n"
18267 "/* multi-line *\n"
18268 " * block comment */\n"
18269 "\n"
18270 "\n"
18271 "#define bbbb 4\n"
18272 "#define ccc (5)",
18273 "#define a 3\n"
18274 "\n"
18275 "\n"
18276 "/* multi-line *\n"
18277 " * block comment */\n"
18278 "\n"
18279 "\n"
18280 "#define bbbb 4\n"
18281 "#define ccc (5)",
18282 Style);
18285 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
18286 FormatStyle Alignment = getLLVMStyle();
18287 Alignment.AlignConsecutiveMacros.Enabled = true;
18288 Alignment.AlignConsecutiveAssignments.Enabled = true;
18289 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18291 Alignment.MaxEmptyLinesToKeep = 10;
18292 /* Test alignment across empty lines */
18293 verifyFormat("int a = 5;\n"
18294 "\n"
18295 "int oneTwoThree = 123;",
18296 "int a = 5;\n"
18297 "\n"
18298 "int oneTwoThree= 123;",
18299 Alignment);
18300 verifyFormat("int a = 5;\n"
18301 "int one = 1;\n"
18302 "\n"
18303 "int oneTwoThree = 123;",
18304 "int a = 5;\n"
18305 "int one = 1;\n"
18306 "\n"
18307 "int oneTwoThree = 123;",
18308 Alignment);
18309 verifyFormat("int a = 5;\n"
18310 "int one = 1;\n"
18311 "\n"
18312 "int oneTwoThree = 123;\n"
18313 "int oneTwo = 12;",
18314 "int a = 5;\n"
18315 "int one = 1;\n"
18316 "\n"
18317 "int oneTwoThree = 123;\n"
18318 "int oneTwo = 12;",
18319 Alignment);
18321 /* Test across comments */
18322 verifyFormat("int a = 5;\n"
18323 "/* block comment */\n"
18324 "int oneTwoThree = 123;",
18325 "int a = 5;\n"
18326 "/* block comment */\n"
18327 "int oneTwoThree=123;",
18328 Alignment);
18330 verifyFormat("int a = 5;\n"
18331 "// line comment\n"
18332 "int oneTwoThree = 123;",
18333 "int a = 5;\n"
18334 "// line comment\n"
18335 "int oneTwoThree=123;",
18336 Alignment);
18338 /* Test across comments and newlines */
18339 verifyFormat("int a = 5;\n"
18340 "\n"
18341 "/* block comment */\n"
18342 "int oneTwoThree = 123;",
18343 "int a = 5;\n"
18344 "\n"
18345 "/* block comment */\n"
18346 "int oneTwoThree=123;",
18347 Alignment);
18349 verifyFormat("int a = 5;\n"
18350 "\n"
18351 "// line comment\n"
18352 "int oneTwoThree = 123;",
18353 "int a = 5;\n"
18354 "\n"
18355 "// line comment\n"
18356 "int oneTwoThree=123;",
18357 Alignment);
18360 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18361 FormatStyle Alignment = getLLVMStyle();
18362 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18363 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18364 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18366 Alignment.MaxEmptyLinesToKeep = 10;
18367 /* Test alignment across empty lines */
18368 verifyFormat("int a = 5;\n"
18369 "\n"
18370 "float const oneTwoThree = 123;",
18371 "int a = 5;\n"
18372 "\n"
18373 "float const oneTwoThree = 123;",
18374 Alignment);
18375 verifyFormat("int a = 5;\n"
18376 "float const one = 1;\n"
18377 "\n"
18378 "int oneTwoThree = 123;",
18379 "int a = 5;\n"
18380 "float const one = 1;\n"
18381 "\n"
18382 "int oneTwoThree = 123;",
18383 Alignment);
18385 /* Test across comments */
18386 verifyFormat("float const a = 5;\n"
18387 "/* block comment */\n"
18388 "int oneTwoThree = 123;",
18389 "float const a = 5;\n"
18390 "/* block comment */\n"
18391 "int oneTwoThree=123;",
18392 Alignment);
18394 verifyFormat("float const a = 5;\n"
18395 "// line comment\n"
18396 "int oneTwoThree = 123;",
18397 "float const a = 5;\n"
18398 "// line comment\n"
18399 "int oneTwoThree=123;",
18400 Alignment);
18402 /* Test across comments and newlines */
18403 verifyFormat("float const a = 5;\n"
18404 "\n"
18405 "/* block comment */\n"
18406 "int oneTwoThree = 123;",
18407 "float const a = 5;\n"
18408 "\n"
18409 "/* block comment */\n"
18410 "int oneTwoThree=123;",
18411 Alignment);
18413 verifyFormat("float const a = 5;\n"
18414 "\n"
18415 "// line comment\n"
18416 "int oneTwoThree = 123;",
18417 "float const a = 5;\n"
18418 "\n"
18419 "// line comment\n"
18420 "int oneTwoThree=123;",
18421 Alignment);
18424 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18425 FormatStyle Alignment = getLLVMStyle();
18426 Alignment.AlignConsecutiveBitFields.Enabled = true;
18427 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18428 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18430 Alignment.MaxEmptyLinesToKeep = 10;
18431 /* Test alignment across empty lines */
18432 verifyFormat("int a : 5;\n"
18433 "\n"
18434 "int longbitfield : 6;",
18435 "int a : 5;\n"
18436 "\n"
18437 "int longbitfield : 6;",
18438 Alignment);
18439 verifyFormat("int a : 5;\n"
18440 "int one : 1;\n"
18441 "\n"
18442 "int longbitfield : 6;",
18443 "int a : 5;\n"
18444 "int one : 1;\n"
18445 "\n"
18446 "int longbitfield : 6;",
18447 Alignment);
18449 /* Test across comments */
18450 verifyFormat("int a : 5;\n"
18451 "/* block comment */\n"
18452 "int longbitfield : 6;",
18453 "int a : 5;\n"
18454 "/* block comment */\n"
18455 "int longbitfield : 6;",
18456 Alignment);
18457 verifyFormat("int a : 5;\n"
18458 "int one : 1;\n"
18459 "// line comment\n"
18460 "int longbitfield : 6;",
18461 "int a : 5;\n"
18462 "int one : 1;\n"
18463 "// line comment\n"
18464 "int longbitfield : 6;",
18465 Alignment);
18467 /* Test across comments and newlines */
18468 verifyFormat("int a : 5;\n"
18469 "/* block comment */\n"
18470 "\n"
18471 "int longbitfield : 6;",
18472 "int a : 5;\n"
18473 "/* block comment */\n"
18474 "\n"
18475 "int longbitfield : 6;",
18476 Alignment);
18477 verifyFormat("int a : 5;\n"
18478 "int one : 1;\n"
18479 "\n"
18480 "// line comment\n"
18481 "\n"
18482 "int longbitfield : 6;",
18483 "int a : 5;\n"
18484 "int one : 1;\n"
18485 "\n"
18486 "// line comment \n"
18487 "\n"
18488 "int longbitfield : 6;",
18489 Alignment);
18492 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
18493 FormatStyle Alignment = getLLVMStyle();
18494 Alignment.AlignConsecutiveMacros.Enabled = true;
18495 Alignment.AlignConsecutiveAssignments.Enabled = true;
18496 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18498 Alignment.MaxEmptyLinesToKeep = 10;
18499 /* Test alignment across empty lines */
18500 verifyFormat("int a = 5;\n"
18501 "\n"
18502 "int oneTwoThree = 123;",
18503 "int a = 5;\n"
18504 "\n"
18505 "int oneTwoThree= 123;",
18506 Alignment);
18507 verifyFormat("int a = 5;\n"
18508 "int one = 1;\n"
18509 "\n"
18510 "int oneTwoThree = 123;",
18511 "int a = 5;\n"
18512 "int one = 1;\n"
18513 "\n"
18514 "int oneTwoThree = 123;",
18515 Alignment);
18517 /* Test across comments */
18518 verifyFormat("int a = 5;\n"
18519 "/* block comment */\n"
18520 "int oneTwoThree = 123;",
18521 "int a = 5;\n"
18522 "/* block comment */\n"
18523 "int oneTwoThree=123;",
18524 Alignment);
18526 verifyFormat("int a = 5;\n"
18527 "// line comment\n"
18528 "int oneTwoThree = 123;",
18529 "int a = 5;\n"
18530 "// line comment\n"
18531 "int oneTwoThree=123;",
18532 Alignment);
18534 verifyFormat("int a = 5;\n"
18535 "/*\n"
18536 " * multi-line block comment\n"
18537 " */\n"
18538 "int oneTwoThree = 123;",
18539 "int a = 5;\n"
18540 "/*\n"
18541 " * multi-line block comment\n"
18542 " */\n"
18543 "int oneTwoThree=123;",
18544 Alignment);
18546 verifyFormat("int a = 5;\n"
18547 "//\n"
18548 "// multi-line line comment\n"
18549 "//\n"
18550 "int oneTwoThree = 123;",
18551 "int a = 5;\n"
18552 "//\n"
18553 "// multi-line line comment\n"
18554 "//\n"
18555 "int oneTwoThree=123;",
18556 Alignment);
18558 /* Test across comments and newlines */
18559 verifyFormat("int a = 5;\n"
18560 "\n"
18561 "/* block comment */\n"
18562 "int oneTwoThree = 123;",
18563 "int a = 5;\n"
18564 "\n"
18565 "/* block comment */\n"
18566 "int oneTwoThree=123;",
18567 Alignment);
18569 verifyFormat("int a = 5;\n"
18570 "\n"
18571 "// line comment\n"
18572 "int oneTwoThree = 123;",
18573 "int a = 5;\n"
18574 "\n"
18575 "// line comment\n"
18576 "int oneTwoThree=123;",
18577 Alignment);
18580 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
18581 FormatStyle Alignment = getLLVMStyle();
18582 Alignment.AlignConsecutiveMacros.Enabled = true;
18583 Alignment.AlignConsecutiveAssignments.Enabled = true;
18584 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18585 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18586 verifyFormat("int a = 5;\n"
18587 "int oneTwoThree = 123;",
18588 Alignment);
18589 verifyFormat("int a = method();\n"
18590 "int oneTwoThree = 133;",
18591 Alignment);
18592 verifyFormat("a &= 5;\n"
18593 "bcd *= 5;\n"
18594 "ghtyf += 5;\n"
18595 "dvfvdb -= 5;\n"
18596 "a /= 5;\n"
18597 "vdsvsv %= 5;\n"
18598 "sfdbddfbdfbb ^= 5;\n"
18599 "dvsdsv |= 5;\n"
18600 "int dsvvdvsdvvv = 123;",
18601 Alignment);
18602 verifyFormat("int i = 1, j = 10;\n"
18603 "something = 2000;",
18604 Alignment);
18605 verifyFormat("something = 2000;\n"
18606 "int i = 1, j = 10;",
18607 Alignment);
18608 verifyFormat("something = 2000;\n"
18609 "another = 911;\n"
18610 "int i = 1, j = 10;\n"
18611 "oneMore = 1;\n"
18612 "i = 2;",
18613 Alignment);
18614 verifyFormat("int a = 5;\n"
18615 "int one = 1;\n"
18616 "method();\n"
18617 "int oneTwoThree = 123;\n"
18618 "int oneTwo = 12;",
18619 Alignment);
18620 verifyFormat("int oneTwoThree = 123;\n"
18621 "int oneTwo = 12;\n"
18622 "method();",
18623 Alignment);
18624 verifyFormat("int oneTwoThree = 123; // comment\n"
18625 "int oneTwo = 12; // comment",
18626 Alignment);
18628 // Bug 25167
18629 /* Uncomment when fixed
18630 verifyFormat("#if A\n"
18631 "#else\n"
18632 "int aaaaaaaa = 12;\n"
18633 "#endif\n"
18634 "#if B\n"
18635 "#else\n"
18636 "int a = 12;\n"
18637 "#endif",
18638 Alignment);
18639 verifyFormat("enum foo {\n"
18640 "#if A\n"
18641 "#else\n"
18642 " aaaaaaaa = 12;\n"
18643 "#endif\n"
18644 "#if B\n"
18645 "#else\n"
18646 " a = 12;\n"
18647 "#endif\n"
18648 "};",
18649 Alignment);
18652 Alignment.MaxEmptyLinesToKeep = 10;
18653 /* Test alignment across empty lines */
18654 verifyFormat("int a = 5;\n"
18655 "\n"
18656 "int oneTwoThree = 123;",
18657 "int a = 5;\n"
18658 "\n"
18659 "int oneTwoThree= 123;",
18660 Alignment);
18661 verifyFormat("int a = 5;\n"
18662 "int one = 1;\n"
18663 "\n"
18664 "int oneTwoThree = 123;",
18665 "int a = 5;\n"
18666 "int one = 1;\n"
18667 "\n"
18668 "int oneTwoThree = 123;",
18669 Alignment);
18670 verifyFormat("int a = 5;\n"
18671 "int one = 1;\n"
18672 "\n"
18673 "int oneTwoThree = 123;\n"
18674 "int oneTwo = 12;",
18675 "int a = 5;\n"
18676 "int one = 1;\n"
18677 "\n"
18678 "int oneTwoThree = 123;\n"
18679 "int oneTwo = 12;",
18680 Alignment);
18682 /* Test across comments */
18683 verifyFormat("int a = 5;\n"
18684 "/* block comment */\n"
18685 "int oneTwoThree = 123;",
18686 "int a = 5;\n"
18687 "/* block comment */\n"
18688 "int oneTwoThree=123;",
18689 Alignment);
18691 verifyFormat("int a = 5;\n"
18692 "// line comment\n"
18693 "int oneTwoThree = 123;",
18694 "int a = 5;\n"
18695 "// line comment\n"
18696 "int oneTwoThree=123;",
18697 Alignment);
18699 /* Test across comments and newlines */
18700 verifyFormat("int a = 5;\n"
18701 "\n"
18702 "/* block comment */\n"
18703 "int oneTwoThree = 123;",
18704 "int a = 5;\n"
18705 "\n"
18706 "/* block comment */\n"
18707 "int oneTwoThree=123;",
18708 Alignment);
18710 verifyFormat("int a = 5;\n"
18711 "\n"
18712 "// line comment\n"
18713 "int oneTwoThree = 123;",
18714 "int a = 5;\n"
18715 "\n"
18716 "// line comment\n"
18717 "int oneTwoThree=123;",
18718 Alignment);
18720 verifyFormat("int a = 5;\n"
18721 "//\n"
18722 "// multi-line line comment\n"
18723 "//\n"
18724 "int oneTwoThree = 123;",
18725 "int a = 5;\n"
18726 "//\n"
18727 "// multi-line line comment\n"
18728 "//\n"
18729 "int oneTwoThree=123;",
18730 Alignment);
18732 verifyFormat("int a = 5;\n"
18733 "/*\n"
18734 " * multi-line block comment\n"
18735 " */\n"
18736 "int oneTwoThree = 123;",
18737 "int a = 5;\n"
18738 "/*\n"
18739 " * multi-line block comment\n"
18740 " */\n"
18741 "int oneTwoThree=123;",
18742 Alignment);
18744 verifyFormat("int a = 5;\n"
18745 "\n"
18746 "/* block comment */\n"
18747 "\n"
18748 "\n"
18749 "\n"
18750 "int oneTwoThree = 123;",
18751 "int a = 5;\n"
18752 "\n"
18753 "/* block comment */\n"
18754 "\n"
18755 "\n"
18756 "\n"
18757 "int oneTwoThree=123;",
18758 Alignment);
18760 verifyFormat("int a = 5;\n"
18761 "\n"
18762 "// line comment\n"
18763 "\n"
18764 "\n"
18765 "\n"
18766 "int oneTwoThree = 123;",
18767 "int a = 5;\n"
18768 "\n"
18769 "// line comment\n"
18770 "\n"
18771 "\n"
18772 "\n"
18773 "int oneTwoThree=123;",
18774 Alignment);
18776 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18777 verifyFormat("#define A \\\n"
18778 " int aaaa = 12; \\\n"
18779 " int b = 23; \\\n"
18780 " int ccc = 234; \\\n"
18781 " int dddddddddd = 2345;",
18782 Alignment);
18783 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18784 verifyFormat("#define A \\\n"
18785 " int aaaa = 12; \\\n"
18786 " int b = 23; \\\n"
18787 " int ccc = 234; \\\n"
18788 " int dddddddddd = 2345;",
18789 Alignment);
18790 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18791 verifyFormat("#define A "
18792 " \\\n"
18793 " int aaaa = 12; "
18794 " \\\n"
18795 " int b = 23; "
18796 " \\\n"
18797 " int ccc = 234; "
18798 " \\\n"
18799 " int dddddddddd = 2345;",
18800 Alignment);
18801 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18802 "k = 4, int l = 5,\n"
18803 " int m = 6) {\n"
18804 " int j = 10;\n"
18805 " otherThing = 1;\n"
18806 "}",
18807 Alignment);
18808 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18809 " int i = 1;\n"
18810 " int j = 2;\n"
18811 " int big = 10000;\n"
18812 "}",
18813 Alignment);
18814 verifyFormat("class C {\n"
18815 "public:\n"
18816 " int i = 1;\n"
18817 " virtual void f() = 0;\n"
18818 "};",
18819 Alignment);
18820 verifyFormat("int i = 1;\n"
18821 "if (SomeType t = getSomething()) {\n"
18822 "}\n"
18823 "int j = 2;\n"
18824 "int big = 10000;",
18825 Alignment);
18826 verifyFormat("int j = 7;\n"
18827 "for (int k = 0; k < N; ++k) {\n"
18828 "}\n"
18829 "int j = 2;\n"
18830 "int big = 10000;\n"
18831 "}",
18832 Alignment);
18833 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18834 verifyFormat("int i = 1;\n"
18835 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18836 " = someLooooooooooooooooongFunction();\n"
18837 "int j = 2;",
18838 Alignment);
18839 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18840 verifyFormat("int i = 1;\n"
18841 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18842 " someLooooooooooooooooongFunction();\n"
18843 "int j = 2;",
18844 Alignment);
18846 verifyFormat("auto lambda = []() {\n"
18847 " auto i = 0;\n"
18848 " return 0;\n"
18849 "};\n"
18850 "int i = 0;\n"
18851 "auto v = type{\n"
18852 " i = 1, //\n"
18853 " (i = 2), //\n"
18854 " i = 3 //\n"
18855 "};",
18856 Alignment);
18858 verifyFormat(
18859 "int i = 1;\n"
18860 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18861 " loooooooooooooooooooooongParameterB);\n"
18862 "int j = 2;",
18863 Alignment);
18865 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18866 " typename B = very_long_type_name_1,\n"
18867 " typename T_2 = very_long_type_name_2>\n"
18868 "auto foo() {}",
18869 Alignment);
18870 verifyFormat("int a, b = 1;\n"
18871 "int c = 2;\n"
18872 "int dd = 3;",
18873 Alignment);
18874 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18875 "float b[1][] = {{3.f}};",
18876 Alignment);
18877 verifyFormat("for (int i = 0; i < 1; i++)\n"
18878 " int x = 1;",
18879 Alignment);
18880 verifyFormat("for (i = 0; i < 1; i++)\n"
18881 " x = 1;\n"
18882 "y = 1;",
18883 Alignment);
18885 Alignment.ReflowComments = FormatStyle::RCS_Always;
18886 Alignment.ColumnLimit = 50;
18887 verifyFormat("int x = 0;\n"
18888 "int yy = 1; /// specificlennospace\n"
18889 "int zzz = 2;",
18890 "int x = 0;\n"
18891 "int yy = 1; ///specificlennospace\n"
18892 "int zzz = 2;",
18893 Alignment);
18896 TEST_F(FormatTest, AlignCompoundAssignments) {
18897 FormatStyle Alignment = getLLVMStyle();
18898 Alignment.AlignConsecutiveAssignments.Enabled = true;
18899 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
18900 Alignment.AlignConsecutiveAssignments.PadOperators = false;
18901 verifyFormat("sfdbddfbdfbb = 5;\n"
18902 "dvsdsv = 5;\n"
18903 "int dsvvdvsdvvv = 123;",
18904 Alignment);
18905 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18906 "dvsdsv |= 5;\n"
18907 "int dsvvdvsdvvv = 123;",
18908 Alignment);
18909 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18910 "dvsdsv <<= 5;\n"
18911 "int dsvvdvsdvvv = 123;",
18912 Alignment);
18913 verifyFormat("int xxx = 5;\n"
18914 "xxx = 5;\n"
18915 "{\n"
18916 " int yyy = 6;\n"
18917 " yyy = 6;\n"
18918 "}",
18919 Alignment);
18920 verifyFormat("int xxx = 5;\n"
18921 "xxx += 5;\n"
18922 "{\n"
18923 " int yyy = 6;\n"
18924 " yyy += 6;\n"
18925 "}",
18926 Alignment);
18927 // Test that `<=` is not treated as a compound assignment.
18928 verifyFormat("aa &= 5;\n"
18929 "b <= 10;\n"
18930 "c = 15;",
18931 Alignment);
18932 Alignment.AlignConsecutiveAssignments.PadOperators = true;
18933 verifyFormat("sfdbddfbdfbb = 5;\n"
18934 "dvsdsv = 5;\n"
18935 "int dsvvdvsdvvv = 123;",
18936 Alignment);
18937 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18938 "dvsdsv |= 5;\n"
18939 "int dsvvdvsdvvv = 123;",
18940 Alignment);
18941 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18942 "dvsdsv <<= 5;\n"
18943 "int dsvvdvsdvvv = 123;",
18944 Alignment);
18945 verifyFormat("a += 5;\n"
18946 "one = 1;\n"
18947 "\n"
18948 "oneTwoThree = 123;",
18949 "a += 5;\n"
18950 "one = 1;\n"
18951 "\n"
18952 "oneTwoThree = 123;",
18953 Alignment);
18954 verifyFormat("a += 5;\n"
18955 "one = 1;\n"
18956 "//\n"
18957 "oneTwoThree = 123;",
18958 "a += 5;\n"
18959 "one = 1;\n"
18960 "//\n"
18961 "oneTwoThree = 123;",
18962 Alignment);
18963 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18964 verifyFormat("a += 5;\n"
18965 "one = 1;\n"
18966 "\n"
18967 "oneTwoThree = 123;",
18968 "a += 5;\n"
18969 "one = 1;\n"
18970 "\n"
18971 "oneTwoThree = 123;",
18972 Alignment);
18973 verifyFormat("a += 5;\n"
18974 "one = 1;\n"
18975 "//\n"
18976 "oneTwoThree = 123;",
18977 "a += 5;\n"
18978 "one = 1;\n"
18979 "//\n"
18980 "oneTwoThree = 123;",
18981 Alignment);
18982 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
18983 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18984 verifyFormat("a += 5;\n"
18985 "one = 1;\n"
18986 "\n"
18987 "oneTwoThree = 123;",
18988 "a += 5;\n"
18989 "one = 1;\n"
18990 "\n"
18991 "oneTwoThree = 123;",
18992 Alignment);
18993 verifyFormat("a += 5;\n"
18994 "one = 1;\n"
18995 "//\n"
18996 "oneTwoThree = 123;",
18997 "a += 5;\n"
18998 "one = 1;\n"
18999 "//\n"
19000 "oneTwoThree = 123;",
19001 Alignment);
19002 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
19003 verifyFormat("a += 5;\n"
19004 "one >>= 1;\n"
19005 "\n"
19006 "oneTwoThree = 123;",
19007 "a += 5;\n"
19008 "one >>= 1;\n"
19009 "\n"
19010 "oneTwoThree = 123;",
19011 Alignment);
19012 verifyFormat("a += 5;\n"
19013 "one = 1;\n"
19014 "//\n"
19015 "oneTwoThree <<= 123;",
19016 "a += 5;\n"
19017 "one = 1;\n"
19018 "//\n"
19019 "oneTwoThree <<= 123;",
19020 Alignment);
19023 TEST_F(FormatTest, AlignConsecutiveAssignments) {
19024 FormatStyle Alignment = getLLVMStyle();
19025 Alignment.AlignConsecutiveMacros.Enabled = true;
19026 verifyFormat("int a = 5;\n"
19027 "int oneTwoThree = 123;",
19028 Alignment);
19029 verifyFormat("int a = 5;\n"
19030 "int oneTwoThree = 123;",
19031 Alignment);
19033 Alignment.AlignConsecutiveAssignments.Enabled = true;
19034 verifyFormat("int a = 5;\n"
19035 "int oneTwoThree = 123;",
19036 Alignment);
19037 verifyFormat("int a = method();\n"
19038 "int oneTwoThree = 133;",
19039 Alignment);
19040 verifyFormat("aa <= 5;\n"
19041 "a &= 5;\n"
19042 "bcd *= 5;\n"
19043 "ghtyf += 5;\n"
19044 "dvfvdb -= 5;\n"
19045 "a /= 5;\n"
19046 "vdsvsv %= 5;\n"
19047 "sfdbddfbdfbb ^= 5;\n"
19048 "dvsdsv |= 5;\n"
19049 "int dsvvdvsdvvv = 123;",
19050 Alignment);
19051 verifyFormat("int i = 1, j = 10;\n"
19052 "something = 2000;",
19053 Alignment);
19054 verifyFormat("something = 2000;\n"
19055 "int i = 1, j = 10;",
19056 Alignment);
19057 verifyFormat("something = 2000;\n"
19058 "another = 911;\n"
19059 "int i = 1, j = 10;\n"
19060 "oneMore = 1;\n"
19061 "i = 2;",
19062 Alignment);
19063 verifyFormat("int a = 5;\n"
19064 "int one = 1;\n"
19065 "method();\n"
19066 "int oneTwoThree = 123;\n"
19067 "int oneTwo = 12;",
19068 Alignment);
19069 verifyFormat("int oneTwoThree = 123;\n"
19070 "int oneTwo = 12;\n"
19071 "method();",
19072 Alignment);
19073 verifyFormat("int oneTwoThree = 123; // comment\n"
19074 "int oneTwo = 12; // comment",
19075 Alignment);
19076 verifyFormat("int f() = default;\n"
19077 "int &operator() = default;\n"
19078 "int &operator=() {",
19079 Alignment);
19080 verifyFormat("int f() = delete;\n"
19081 "int &operator() = delete;\n"
19082 "int &operator=() {",
19083 Alignment);
19084 verifyFormat("int f() = default; // comment\n"
19085 "int &operator() = default; // comment\n"
19086 "int &operator=() {",
19087 Alignment);
19088 verifyFormat("int f() = default;\n"
19089 "int &operator() = default;\n"
19090 "int &operator==() {",
19091 Alignment);
19092 verifyFormat("int f() = default;\n"
19093 "int &operator() = default;\n"
19094 "int &operator<=() {",
19095 Alignment);
19096 verifyFormat("int f() = default;\n"
19097 "int &operator() = default;\n"
19098 "int &operator!=() {",
19099 Alignment);
19100 verifyFormat("int f() = default;\n"
19101 "int &operator() = default;\n"
19102 "int &operator=();",
19103 Alignment);
19104 verifyFormat("int f() = delete;\n"
19105 "int &operator() = delete;\n"
19106 "int &operator=();",
19107 Alignment);
19108 verifyFormat("/* long long padding */ int f() = default;\n"
19109 "int &operator() = default;\n"
19110 "int &operator/**/ =();",
19111 Alignment);
19112 // https://llvm.org/PR33697
19113 FormatStyle AlignmentWithPenalty = getLLVMStyle();
19114 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
19115 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
19116 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19117 " void f() = delete;\n"
19118 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19119 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19120 "};",
19121 AlignmentWithPenalty);
19123 // Bug 25167
19124 /* Uncomment when fixed
19125 verifyFormat("#if A\n"
19126 "#else\n"
19127 "int aaaaaaaa = 12;\n"
19128 "#endif\n"
19129 "#if B\n"
19130 "#else\n"
19131 "int a = 12;\n"
19132 "#endif",
19133 Alignment);
19134 verifyFormat("enum foo {\n"
19135 "#if A\n"
19136 "#else\n"
19137 " aaaaaaaa = 12;\n"
19138 "#endif\n"
19139 "#if B\n"
19140 "#else\n"
19141 " a = 12;\n"
19142 "#endif\n"
19143 "};",
19144 Alignment);
19147 verifyFormat("int a = 5;\n"
19148 "\n"
19149 "int oneTwoThree = 123;",
19150 "int a = 5;\n"
19151 "\n"
19152 "int oneTwoThree= 123;",
19153 Alignment);
19154 verifyFormat("int a = 5;\n"
19155 "int one = 1;\n"
19156 "\n"
19157 "int oneTwoThree = 123;",
19158 "int a = 5;\n"
19159 "int one = 1;\n"
19160 "\n"
19161 "int oneTwoThree = 123;",
19162 Alignment);
19163 verifyFormat("int a = 5;\n"
19164 "int one = 1;\n"
19165 "\n"
19166 "int oneTwoThree = 123;\n"
19167 "int oneTwo = 12;",
19168 "int a = 5;\n"
19169 "int one = 1;\n"
19170 "\n"
19171 "int oneTwoThree = 123;\n"
19172 "int oneTwo = 12;",
19173 Alignment);
19174 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19175 verifyFormat("#define A \\\n"
19176 " int aaaa = 12; \\\n"
19177 " int b = 23; \\\n"
19178 " int ccc = 234; \\\n"
19179 " int dddddddddd = 2345;",
19180 Alignment);
19181 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19182 verifyFormat("#define A \\\n"
19183 " int aaaa = 12; \\\n"
19184 " int b = 23; \\\n"
19185 " int ccc = 234; \\\n"
19186 " int dddddddddd = 2345;",
19187 Alignment);
19188 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19189 verifyFormat("#define A "
19190 " \\\n"
19191 " int aaaa = 12; "
19192 " \\\n"
19193 " int b = 23; "
19194 " \\\n"
19195 " int ccc = 234; "
19196 " \\\n"
19197 " int dddddddddd = 2345;",
19198 Alignment);
19199 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19200 "k = 4, int l = 5,\n"
19201 " int m = 6) {\n"
19202 " int j = 10;\n"
19203 " otherThing = 1;\n"
19204 "}",
19205 Alignment);
19206 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19207 " int i = 1;\n"
19208 " int j = 2;\n"
19209 " int big = 10000;\n"
19210 "}",
19211 Alignment);
19212 verifyFormat("class C {\n"
19213 "public:\n"
19214 " int i = 1;\n"
19215 " virtual void f() = 0;\n"
19216 "};",
19217 Alignment);
19218 verifyFormat("int i = 1;\n"
19219 "if (SomeType t = getSomething()) {\n"
19220 "}\n"
19221 "int j = 2;\n"
19222 "int big = 10000;",
19223 Alignment);
19224 verifyFormat("int j = 7;\n"
19225 "for (int k = 0; k < N; ++k) {\n"
19226 "}\n"
19227 "int j = 2;\n"
19228 "int big = 10000;\n"
19229 "}",
19230 Alignment);
19231 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19232 verifyFormat("int i = 1;\n"
19233 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19234 " = someLooooooooooooooooongFunction();\n"
19235 "int j = 2;",
19236 Alignment);
19237 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19238 verifyFormat("int i = 1;\n"
19239 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19240 " someLooooooooooooooooongFunction();\n"
19241 "int j = 2;",
19242 Alignment);
19244 verifyFormat("auto lambda = []() {\n"
19245 " auto i = 0;\n"
19246 " return 0;\n"
19247 "};\n"
19248 "int i = 0;\n"
19249 "auto v = type{\n"
19250 " i = 1, //\n"
19251 " (i = 2), //\n"
19252 " i = 3 //\n"
19253 "};",
19254 Alignment);
19256 verifyFormat(
19257 "int i = 1;\n"
19258 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19259 " loooooooooooooooooooooongParameterB);\n"
19260 "int j = 2;",
19261 Alignment);
19263 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19264 " typename B = very_long_type_name_1,\n"
19265 " typename T_2 = very_long_type_name_2>\n"
19266 "auto foo() {}",
19267 Alignment);
19268 verifyFormat("int a, b = 1;\n"
19269 "int c = 2;\n"
19270 "int dd = 3;",
19271 Alignment);
19272 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19273 "float b[1][] = {{3.f}};",
19274 Alignment);
19275 verifyFormat("for (int i = 0; i < 1; i++)\n"
19276 " int x = 1;",
19277 Alignment);
19278 verifyFormat("for (i = 0; i < 1; i++)\n"
19279 " x = 1;\n"
19280 "y = 1;",
19281 Alignment);
19283 EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always);
19284 Alignment.ColumnLimit = 50;
19285 verifyFormat("int x = 0;\n"
19286 "int yy = 1; /// specificlennospace\n"
19287 "int zzz = 2;",
19288 "int x = 0;\n"
19289 "int yy = 1; ///specificlennospace\n"
19290 "int zzz = 2;",
19291 Alignment);
19293 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19294 "auto b = [] {\n"
19295 " f();\n"
19296 " return;\n"
19297 "};",
19298 Alignment);
19299 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19300 "auto b = g([] {\n"
19301 " f();\n"
19302 " return;\n"
19303 "});",
19304 Alignment);
19305 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19306 "auto b = g(param, [] {\n"
19307 " f();\n"
19308 " return;\n"
19309 "});",
19310 Alignment);
19311 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19312 "auto b = [] {\n"
19313 " if (condition) {\n"
19314 " return;\n"
19315 " }\n"
19316 "};",
19317 Alignment);
19319 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19320 " ccc ? aaaaa : bbbbb,\n"
19321 " dddddddddddddddddddddddddd);",
19322 Alignment);
19323 // FIXME: https://llvm.org/PR53497
19324 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19325 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19326 // " ccc ? aaaaa : bbbbb,\n"
19327 // " dddddddddddddddddddddddddd);",
19328 // Alignment);
19330 // Confirm proper handling of AlignConsecutiveAssignments with
19331 // BinPackArguments.
19332 // See https://llvm.org/PR55360
19333 Alignment = getLLVMStyleWithColumns(50);
19334 Alignment.AlignConsecutiveAssignments.Enabled = true;
19335 Alignment.BinPackArguments = false;
19336 verifyFormat("int a_long_name = 1;\n"
19337 "auto b = B({a_long_name, a_long_name},\n"
19338 " {a_longer_name_for_wrap,\n"
19339 " a_longer_name_for_wrap});",
19340 Alignment);
19341 verifyFormat("int a_long_name = 1;\n"
19342 "auto b = B{{a_long_name, a_long_name},\n"
19343 " {a_longer_name_for_wrap,\n"
19344 " a_longer_name_for_wrap}};",
19345 Alignment);
19347 Alignment = getLLVMStyleWithColumns(60);
19348 Alignment.AlignConsecutiveAssignments.Enabled = true;
19349 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19350 "using I = std::conditional_t<II::value >= 0,\n"
19351 " std::ic<int, II::value + 1>,\n"
19352 " std::ic<int, -1>>;",
19353 Alignment);
19354 verifyFormat("SomeName = Foo;\n"
19355 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19356 " arrrrrrrrrrg);",
19357 Alignment);
19359 Alignment.ColumnLimit = 80;
19360 Alignment.SpacesInAngles = FormatStyle::SIAS_Always;
19361 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19362 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19363 Alignment);
19364 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19365 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19366 Alignment);
19368 Alignment.SpacesInParens = FormatStyle::SIPO_Custom;
19369 Alignment.SpacesInParensOptions.InCStyleCasts = true;
19370 verifyFormat("void **ptr = ( void ** )unkn;\n"
19371 "ptr = ( void ** )ptr[0];",
19372 Alignment);
19373 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19374 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19375 Alignment);
19378 TEST_F(FormatTest, AlignConsecutiveBitFields) {
19379 FormatStyle Alignment = getLLVMStyle();
19380 Alignment.AlignConsecutiveBitFields.Enabled = true;
19381 verifyFormat("int const a : 5;\n"
19382 "int oneTwoThree : 23;",
19383 Alignment);
19385 // Initializers are allowed starting with c++2a
19386 verifyFormat("int const a : 5 = 1;\n"
19387 "int oneTwoThree : 23 = 0;",
19388 Alignment);
19390 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19391 verifyFormat("int const a : 5;\n"
19392 "int oneTwoThree : 23;",
19393 Alignment);
19395 verifyFormat("int const a : 5; // comment\n"
19396 "int oneTwoThree : 23; // comment",
19397 Alignment);
19399 verifyFormat("int const a : 5 = 1;\n"
19400 "int oneTwoThree : 23 = 0;",
19401 Alignment);
19403 Alignment.AlignConsecutiveAssignments.Enabled = true;
19404 verifyFormat("int const a : 5 = 1;\n"
19405 "int oneTwoThree : 23 = 0;",
19406 Alignment);
19407 verifyFormat("int const a : 5 = {1};\n"
19408 "int oneTwoThree : 23 = 0;",
19409 Alignment);
19411 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19412 verifyFormat("int const a :5;\n"
19413 "int oneTwoThree:23;",
19414 Alignment);
19416 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19417 verifyFormat("int const a :5;\n"
19418 "int oneTwoThree :23;",
19419 Alignment);
19421 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19422 verifyFormat("int const a : 5;\n"
19423 "int oneTwoThree: 23;",
19424 Alignment);
19426 // Known limitations: ':' is only recognized as a bitfield colon when
19427 // followed by a number.
19429 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19430 "int a : 5;",
19431 Alignment);
19435 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19436 FormatStyle Alignment = getLLVMStyle();
19437 Alignment.AlignConsecutiveMacros.Enabled = true;
19438 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19439 verifyFormat("float const a = 5;\n"
19440 "int oneTwoThree = 123;",
19441 Alignment);
19442 verifyFormat("int a = 5;\n"
19443 "float const oneTwoThree = 123;",
19444 Alignment);
19446 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19447 verifyFormat("float const a = 5;\n"
19448 "int oneTwoThree = 123;",
19449 Alignment);
19450 verifyFormat("int a = method();\n"
19451 "float const oneTwoThree = 133;",
19452 Alignment);
19453 verifyFormat("int i = 1, j = 10;\n"
19454 "something = 2000;",
19455 Alignment);
19456 verifyFormat("something = 2000;\n"
19457 "int i = 1, j = 10;",
19458 Alignment);
19459 verifyFormat("float something = 2000;\n"
19460 "double another = 911;\n"
19461 "int i = 1, j = 10;\n"
19462 "const int *oneMore = 1;\n"
19463 "unsigned i = 2;",
19464 Alignment);
19465 verifyFormat("float a = 5;\n"
19466 "int one = 1;\n"
19467 "method();\n"
19468 "const double oneTwoThree = 123;\n"
19469 "const unsigned int oneTwo = 12;",
19470 Alignment);
19471 verifyFormat("int oneTwoThree{0}; // comment\n"
19472 "unsigned oneTwo; // comment",
19473 Alignment);
19474 verifyFormat("unsigned int *a;\n"
19475 "int *b;\n"
19476 "unsigned int Const *c;\n"
19477 "unsigned int const *d;\n"
19478 "unsigned int Const &e;\n"
19479 "unsigned int const &f;",
19480 Alignment);
19481 verifyFormat("Const unsigned int *c;\n"
19482 "const unsigned int *d;\n"
19483 "Const unsigned int &e;\n"
19484 "const unsigned int &f;\n"
19485 "const unsigned g;\n"
19486 "Const unsigned h;",
19487 Alignment);
19488 verifyFormat("float const a = 5;\n"
19489 "\n"
19490 "int oneTwoThree = 123;",
19491 "float const a = 5;\n"
19492 "\n"
19493 "int oneTwoThree= 123;",
19494 Alignment);
19495 verifyFormat("float a = 5;\n"
19496 "int one = 1;\n"
19497 "\n"
19498 "unsigned oneTwoThree = 123;",
19499 "float a = 5;\n"
19500 "int one = 1;\n"
19501 "\n"
19502 "unsigned oneTwoThree = 123;",
19503 Alignment);
19504 verifyFormat("float a = 5;\n"
19505 "int one = 1;\n"
19506 "\n"
19507 "unsigned oneTwoThree = 123;\n"
19508 "int oneTwo = 12;",
19509 "float a = 5;\n"
19510 "int one = 1;\n"
19511 "\n"
19512 "unsigned oneTwoThree = 123;\n"
19513 "int oneTwo = 12;",
19514 Alignment);
19515 // Function prototype alignment
19516 verifyFormat("int a();\n"
19517 "double b();",
19518 Alignment);
19519 verifyFormat("int a(int x);\n"
19520 "double b();",
19521 Alignment);
19522 verifyFormat("int a(const Test & = Test());\n"
19523 "int a1(int &foo, const Test & = Test());\n"
19524 "int a2(int &foo, const Test &name = Test());\n"
19525 "double b();",
19526 Alignment);
19527 verifyFormat("struct Test {\n"
19528 " Test(const Test &) = default;\n"
19529 " ~Test() = default;\n"
19530 " Test &operator=(const Test &) = default;\n"
19531 "};",
19532 Alignment);
19533 unsigned OldColumnLimit = Alignment.ColumnLimit;
19534 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19535 // otherwise the function parameters will be re-flowed onto a single line.
19536 Alignment.ColumnLimit = 0;
19537 verifyFormat("int a(int x,\n"
19538 " float y);\n"
19539 "double b(int x,\n"
19540 " double y);",
19541 "int a(int x,\n"
19542 " float y);\n"
19543 "double b(int x,\n"
19544 " double y);",
19545 Alignment);
19546 // This ensures that function parameters of function declarations are
19547 // correctly indented when their owning functions are indented.
19548 // The failure case here is for 'double y' to not be indented enough.
19549 verifyFormat("double a(int x);\n"
19550 "int b(int y,\n"
19551 " double z);",
19552 "double a(int x);\n"
19553 "int b(int y,\n"
19554 " double z);",
19555 Alignment);
19556 // Set ColumnLimit low so that we induce wrapping immediately after
19557 // the function name and opening paren.
19558 Alignment.ColumnLimit = 13;
19559 verifyFormat("int function(\n"
19560 " int x,\n"
19561 " bool y);",
19562 Alignment);
19563 // Set ColumnLimit low so that we break the argument list in multiple lines.
19564 Alignment.ColumnLimit = 35;
19565 verifyFormat("int a3(SomeTypeName1 &x,\n"
19566 " SomeTypeName2 &y,\n"
19567 " const Test & = Test());\n"
19568 "double b();",
19569 Alignment);
19570 Alignment.ColumnLimit = OldColumnLimit;
19571 // Ensure function pointers don't screw up recursive alignment
19572 verifyFormat("int a(int x, void (*fp)(int y));\n"
19573 "double b();",
19574 Alignment);
19575 Alignment.AlignConsecutiveAssignments.Enabled = true;
19576 verifyFormat("struct Test {\n"
19577 " Test(const Test &) = default;\n"
19578 " ~Test() = default;\n"
19579 " Test &operator=(const Test &) = default;\n"
19580 "};",
19581 Alignment);
19582 // Ensure recursive alignment is broken by function braces, so that the
19583 // "a = 1" does not align with subsequent assignments inside the function
19584 // body.
19585 verifyFormat("int func(int a = 1) {\n"
19586 " int b = 2;\n"
19587 " int cc = 3;\n"
19588 "}",
19589 Alignment);
19590 verifyFormat("float something = 2000;\n"
19591 "double another = 911;\n"
19592 "int i = 1, j = 10;\n"
19593 "const int *oneMore = 1;\n"
19594 "unsigned i = 2;",
19595 Alignment);
19596 verifyFormat("int oneTwoThree = {0}; // comment\n"
19597 "unsigned oneTwo = 0; // comment",
19598 Alignment);
19599 // Make sure that scope is correctly tracked, in the absence of braces
19600 verifyFormat("for (int i = 0; i < n; i++)\n"
19601 " j = i;\n"
19602 "double x = 1;",
19603 Alignment);
19604 verifyFormat("if (int i = 0)\n"
19605 " j = i;\n"
19606 "double x = 1;",
19607 Alignment);
19608 // Ensure operator[] and operator() are comprehended
19609 verifyFormat("struct test {\n"
19610 " long long int foo();\n"
19611 " int operator[](int a);\n"
19612 " double bar();\n"
19613 "};",
19614 Alignment);
19615 verifyFormat("struct test {\n"
19616 " long long int foo();\n"
19617 " int operator()(int a);\n"
19618 " double bar();\n"
19619 "};",
19620 Alignment);
19621 // http://llvm.org/PR52914
19622 verifyFormat("char *a[] = {\"a\", // comment\n"
19623 " \"bb\"};\n"
19624 "int bbbbbbb = 0;",
19625 Alignment);
19626 // http://llvm.org/PR68079
19627 verifyFormat("using Fn = int (A::*)();\n"
19628 "using RFn = int (A::*)() &;\n"
19629 "using RRFn = int (A::*)() &&;",
19630 Alignment);
19631 verifyFormat("using Fn = int (A::*)();\n"
19632 "using RFn = int *(A::*)() &;\n"
19633 "using RRFn = double (A::*)() &&;",
19634 Alignment);
19636 // PAS_Right
19637 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19638 " int const i = 1;\n"
19639 " int *j = 2;\n"
19640 " int big = 10000;\n"
19641 "\n"
19642 " unsigned oneTwoThree = 123;\n"
19643 " int oneTwo = 12;\n"
19644 " method();\n"
19645 " float k = 2;\n"
19646 " int ll = 10000;\n"
19647 "}",
19648 "void SomeFunction(int parameter= 0) {\n"
19649 " int const i= 1;\n"
19650 " int *j=2;\n"
19651 " int big = 10000;\n"
19652 "\n"
19653 "unsigned oneTwoThree =123;\n"
19654 "int oneTwo = 12;\n"
19655 " method();\n"
19656 "float k= 2;\n"
19657 "int ll=10000;\n"
19658 "}",
19659 Alignment);
19660 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19661 " int const i = 1;\n"
19662 " int **j = 2, ***k;\n"
19663 " int &k = i;\n"
19664 " int &&l = i + j;\n"
19665 " int big = 10000;\n"
19666 "\n"
19667 " unsigned oneTwoThree = 123;\n"
19668 " int oneTwo = 12;\n"
19669 " method();\n"
19670 " float k = 2;\n"
19671 " int ll = 10000;\n"
19672 "}",
19673 "void SomeFunction(int parameter= 0) {\n"
19674 " int const i= 1;\n"
19675 " int **j=2,***k;\n"
19676 "int &k=i;\n"
19677 "int &&l=i+j;\n"
19678 " int big = 10000;\n"
19679 "\n"
19680 "unsigned oneTwoThree =123;\n"
19681 "int oneTwo = 12;\n"
19682 " method();\n"
19683 "float k= 2;\n"
19684 "int ll=10000;\n"
19685 "}",
19686 Alignment);
19687 // variables are aligned at their name, pointers are at the right most
19688 // position
19689 verifyFormat("int *a;\n"
19690 "int **b;\n"
19691 "int ***c;\n"
19692 "int foobar;",
19693 Alignment);
19695 // PAS_Left
19696 FormatStyle AlignmentLeft = Alignment;
19697 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
19698 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19699 " int const i = 1;\n"
19700 " int* j = 2;\n"
19701 " int big = 10000;\n"
19702 "\n"
19703 " unsigned oneTwoThree = 123;\n"
19704 " int oneTwo = 12;\n"
19705 " method();\n"
19706 " float k = 2;\n"
19707 " int ll = 10000;\n"
19708 "}",
19709 "void SomeFunction(int parameter= 0) {\n"
19710 " int const i= 1;\n"
19711 " int *j=2;\n"
19712 " int big = 10000;\n"
19713 "\n"
19714 "unsigned oneTwoThree =123;\n"
19715 "int oneTwo = 12;\n"
19716 " method();\n"
19717 "float k= 2;\n"
19718 "int ll=10000;\n"
19719 "}",
19720 AlignmentLeft);
19721 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19722 " int const i = 1;\n"
19723 " int** j = 2;\n"
19724 " int& k = i;\n"
19725 " int&& l = i + j;\n"
19726 " int big = 10000;\n"
19727 "\n"
19728 " unsigned oneTwoThree = 123;\n"
19729 " int oneTwo = 12;\n"
19730 " method();\n"
19731 " float k = 2;\n"
19732 " int ll = 10000;\n"
19733 "}",
19734 "void SomeFunction(int parameter= 0) {\n"
19735 " int const i= 1;\n"
19736 " int **j=2;\n"
19737 "int &k=i;\n"
19738 "int &&l=i+j;\n"
19739 " int big = 10000;\n"
19740 "\n"
19741 "unsigned oneTwoThree =123;\n"
19742 "int oneTwo = 12;\n"
19743 " method();\n"
19744 "float k= 2;\n"
19745 "int ll=10000;\n"
19746 "}",
19747 AlignmentLeft);
19748 // variables are aligned at their name, pointers are at the left most position
19749 verifyFormat("int* a;\n"
19750 "int** b;\n"
19751 "int*** c;\n"
19752 "int foobar;",
19753 AlignmentLeft);
19755 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19756 "double b();",
19757 AlignmentLeft);
19759 // PAS_Middle
19760 FormatStyle AlignmentMiddle = Alignment;
19761 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
19762 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19763 " int const i = 1;\n"
19764 " int * j = 2;\n"
19765 " int big = 10000;\n"
19766 "\n"
19767 " unsigned oneTwoThree = 123;\n"
19768 " int oneTwo = 12;\n"
19769 " method();\n"
19770 " float k = 2;\n"
19771 " int ll = 10000;\n"
19772 "}",
19773 "void SomeFunction(int parameter= 0) {\n"
19774 " int const i= 1;\n"
19775 " int *j=2;\n"
19776 " int big = 10000;\n"
19777 "\n"
19778 "unsigned oneTwoThree =123;\n"
19779 "int oneTwo = 12;\n"
19780 " method();\n"
19781 "float k= 2;\n"
19782 "int ll=10000;\n"
19783 "}",
19784 AlignmentMiddle);
19785 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19786 " int const i = 1;\n"
19787 " int ** j = 2, ***k;\n"
19788 " int & k = i;\n"
19789 " int && l = i + j;\n"
19790 " int big = 10000;\n"
19791 "\n"
19792 " unsigned oneTwoThree = 123;\n"
19793 " int oneTwo = 12;\n"
19794 " method();\n"
19795 " float k = 2;\n"
19796 " int ll = 10000;\n"
19797 "}",
19798 "void SomeFunction(int parameter= 0) {\n"
19799 " int const i= 1;\n"
19800 " int **j=2,***k;\n"
19801 "int &k=i;\n"
19802 "int &&l=i+j;\n"
19803 " int big = 10000;\n"
19804 "\n"
19805 "unsigned oneTwoThree =123;\n"
19806 "int oneTwo = 12;\n"
19807 " method();\n"
19808 "float k= 2;\n"
19809 "int ll=10000;\n"
19810 "}",
19811 AlignmentMiddle);
19812 // variables are aligned at their name, pointers are in the middle
19813 verifyFormat("int * a;\n"
19814 "int * b;\n"
19815 "int *** c;\n"
19816 "int foobar;",
19817 AlignmentMiddle);
19819 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19820 "double b();",
19821 AlignmentMiddle);
19823 Alignment.AlignConsecutiveAssignments.Enabled = false;
19824 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19825 verifyFormat("#define A \\\n"
19826 " int aaaa = 12; \\\n"
19827 " float b = 23; \\\n"
19828 " const int ccc = 234; \\\n"
19829 " unsigned dddddddddd = 2345;",
19830 Alignment);
19831 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19832 verifyFormat("#define A \\\n"
19833 " int aaaa = 12; \\\n"
19834 " float b = 23; \\\n"
19835 " const int ccc = 234; \\\n"
19836 " unsigned dddddddddd = 2345;",
19837 Alignment);
19838 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19839 Alignment.ColumnLimit = 30;
19840 verifyFormat("#define A \\\n"
19841 " int aaaa = 12; \\\n"
19842 " float b = 23; \\\n"
19843 " const int ccc = 234; \\\n"
19844 " int dddddddddd = 2345;",
19845 Alignment);
19846 Alignment.ColumnLimit = 80;
19847 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19848 "k = 4, int l = 5,\n"
19849 " int m = 6) {\n"
19850 " const int j = 10;\n"
19851 " otherThing = 1;\n"
19852 "}",
19853 Alignment);
19854 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19855 " int const i = 1;\n"
19856 " int *j = 2;\n"
19857 " int big = 10000;\n"
19858 "}",
19859 Alignment);
19860 verifyFormat("class C {\n"
19861 "public:\n"
19862 " int i = 1;\n"
19863 " virtual void f() = 0;\n"
19864 "};",
19865 Alignment);
19866 verifyFormat("float i = 1;\n"
19867 "if (SomeType t = getSomething()) {\n"
19868 "}\n"
19869 "const unsigned j = 2;\n"
19870 "int big = 10000;",
19871 Alignment);
19872 verifyFormat("float j = 7;\n"
19873 "for (int k = 0; k < N; ++k) {\n"
19874 "}\n"
19875 "unsigned j = 2;\n"
19876 "int big = 10000;\n"
19877 "}",
19878 Alignment);
19879 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19880 verifyFormat("float i = 1;\n"
19881 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19882 " = someLooooooooooooooooongFunction();\n"
19883 "int j = 2;",
19884 Alignment);
19885 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19886 verifyFormat("int i = 1;\n"
19887 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19888 " someLooooooooooooooooongFunction();\n"
19889 "int j = 2;",
19890 Alignment);
19892 Alignment.AlignConsecutiveAssignments.Enabled = true;
19893 verifyFormat("auto lambda = []() {\n"
19894 " auto ii = 0;\n"
19895 " float j = 0;\n"
19896 " return 0;\n"
19897 "};\n"
19898 "int i = 0;\n"
19899 "float i2 = 0;\n"
19900 "auto v = type{\n"
19901 " i = 1, //\n"
19902 " (i = 2), //\n"
19903 " i = 3 //\n"
19904 "};",
19905 Alignment);
19906 Alignment.AlignConsecutiveAssignments.Enabled = false;
19908 verifyFormat(
19909 "int i = 1;\n"
19910 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19911 " loooooooooooooooooooooongParameterB);\n"
19912 "int j = 2;",
19913 Alignment);
19915 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19916 // We expect declarations and assignments to align, as long as it doesn't
19917 // exceed the column limit, starting a new alignment sequence whenever it
19918 // happens.
19919 Alignment.AlignConsecutiveAssignments.Enabled = true;
19920 Alignment.ColumnLimit = 30;
19921 verifyFormat("float ii = 1;\n"
19922 "unsigned j = 2;\n"
19923 "int someVerylongVariable = 1;\n"
19924 "AnotherLongType ll = 123456;\n"
19925 "VeryVeryLongType k = 2;\n"
19926 "int myvar = 1;",
19927 Alignment);
19928 Alignment.ColumnLimit = 80;
19929 Alignment.AlignConsecutiveAssignments.Enabled = false;
19931 verifyFormat(
19932 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19933 " typename LongType, typename B>\n"
19934 "auto foo() {}",
19935 Alignment);
19936 verifyFormat("float a, b = 1;\n"
19937 "int c = 2;\n"
19938 "int dd = 3;",
19939 Alignment);
19940 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19941 "float b[1][] = {{3.f}};",
19942 Alignment);
19943 Alignment.AlignConsecutiveAssignments.Enabled = true;
19944 verifyFormat("float a, b = 1;\n"
19945 "int c = 2;\n"
19946 "int dd = 3;",
19947 Alignment);
19948 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19949 "float b[1][] = {{3.f}};",
19950 Alignment);
19951 Alignment.AlignConsecutiveAssignments.Enabled = false;
19953 Alignment.ColumnLimit = 30;
19954 Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine;
19955 verifyFormat("void foo(float a,\n"
19956 " float b,\n"
19957 " int c,\n"
19958 " uint32_t *d) {\n"
19959 " int *e = 0;\n"
19960 " float f = 0;\n"
19961 " double g = 0;\n"
19962 "}\n"
19963 "void bar(ino_t a,\n"
19964 " int b,\n"
19965 " uint32_t *c,\n"
19966 " bool d) {}",
19967 Alignment);
19968 Alignment.BinPackParameters = FormatStyle::BPPS_BinPack;
19969 Alignment.ColumnLimit = 80;
19971 // Bug 33507
19972 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19973 verifyFormat(
19974 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19975 " static const Version verVs2017;\n"
19976 " return true;\n"
19977 "});",
19978 Alignment);
19979 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19981 // See llvm.org/PR35641
19982 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19983 verifyFormat("int func() { //\n"
19984 " int b;\n"
19985 " unsigned c;\n"
19986 "}",
19987 Alignment);
19989 // See PR37175
19990 FormatStyle Style = getMozillaStyle();
19991 Style.AlignConsecutiveDeclarations.Enabled = true;
19992 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19993 "foo(int a);",
19994 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
19996 Alignment.PointerAlignment = FormatStyle::PAS_Left;
19997 verifyFormat("unsigned int* a;\n"
19998 "int* b;\n"
19999 "unsigned int Const* c;\n"
20000 "unsigned int const* d;\n"
20001 "unsigned int Const& e;\n"
20002 "unsigned int const& f;",
20003 Alignment);
20004 verifyFormat("Const unsigned int* c;\n"
20005 "const unsigned int* d;\n"
20006 "Const unsigned int& e;\n"
20007 "const unsigned int& f;\n"
20008 "const unsigned g;\n"
20009 "Const unsigned h;",
20010 Alignment);
20012 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
20013 verifyFormat("unsigned int * a;\n"
20014 "int * b;\n"
20015 "unsigned int Const * c;\n"
20016 "unsigned int const * d;\n"
20017 "unsigned int Const & e;\n"
20018 "unsigned int const & f;",
20019 Alignment);
20020 verifyFormat("Const unsigned int * c;\n"
20021 "const unsigned int * d;\n"
20022 "Const unsigned int & e;\n"
20023 "const unsigned int & f;\n"
20024 "const unsigned g;\n"
20025 "Const unsigned h;",
20026 Alignment);
20028 // See PR46529
20029 FormatStyle BracedAlign = getLLVMStyle();
20030 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
20031 verifyFormat("const auto result{[]() {\n"
20032 " const auto something = 1;\n"
20033 " return 2;\n"
20034 "}};",
20035 BracedAlign);
20036 verifyFormat("int foo{[]() {\n"
20037 " int bar{0};\n"
20038 " return 0;\n"
20039 "}()};",
20040 BracedAlign);
20041 BracedAlign.Cpp11BracedListStyle = false;
20042 verifyFormat("const auto result{ []() {\n"
20043 " const auto something = 1;\n"
20044 " return 2;\n"
20045 "} };",
20046 BracedAlign);
20047 verifyFormat("int foo{ []() {\n"
20048 " int bar{ 0 };\n"
20049 " return 0;\n"
20050 "}() };",
20051 BracedAlign);
20053 Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
20054 verifyFormat("unsigned int f1(void);\n"
20055 "void f2(void);\n"
20056 "size_t f3(void);",
20057 Alignment);
20060 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
20061 FormatStyle Alignment = getLLVMStyle();
20062 Alignment.AllowShortCaseLabelsOnASingleLine = true;
20063 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
20065 verifyFormat("switch (level) {\n"
20066 "case log::info: return \"info\";\n"
20067 "case log::warning: return \"warning\";\n"
20068 "default: return \"default\";\n"
20069 "}",
20070 Alignment);
20072 verifyFormat("switch (level) {\n"
20073 "case log::info: return \"info\";\n"
20074 "case log::warning: return \"warning\";\n"
20075 "}",
20076 "switch (level) {\n"
20077 "case log::info: return \"info\";\n"
20078 "case log::warning:\n"
20079 " return \"warning\";\n"
20080 "}",
20081 Alignment);
20083 // Empty case statements push out the alignment, but non-short case labels
20084 // don't.
20085 verifyFormat("switch (level) {\n"
20086 "case log::info: return \"info\";\n"
20087 "case log::critical:\n"
20088 "case log::warning:\n"
20089 "case log::severe: return \"severe\";\n"
20090 "case log::extra_severe:\n"
20091 " // comment\n"
20092 " return \"extra_severe\";\n"
20093 "}",
20094 Alignment);
20096 // Verify comments and empty lines break the alignment.
20097 verifyNoChange("switch (level) {\n"
20098 "case log::info: return \"info\";\n"
20099 "case log::warning: return \"warning\";\n"
20100 "// comment\n"
20101 "case log::critical: return \"critical\";\n"
20102 "default: return \"default\";\n"
20103 "\n"
20104 "case log::severe: return \"severe\";\n"
20105 "}",
20106 Alignment);
20108 // Empty case statements don't break the alignment, and potentially push it
20109 // out.
20110 verifyFormat("switch (level) {\n"
20111 "case log::info: return \"info\";\n"
20112 "case log::warning:\n"
20113 "case log::critical:\n"
20114 "default: return \"default\";\n"
20115 "}",
20116 Alignment);
20118 // Implicit fallthrough cases can be aligned with either a comment or
20119 // [[fallthrough]]
20120 verifyFormat("switch (level) {\n"
20121 "case log::info: return \"info\";\n"
20122 "case log::warning: // fallthrough\n"
20123 "case log::error: return \"error\";\n"
20124 "case log::critical: /*fallthrough*/\n"
20125 "case log::severe: return \"severe\";\n"
20126 "case log::diag: [[fallthrough]];\n"
20127 "default: return \"default\";\n"
20128 "}",
20129 Alignment);
20131 // Verify trailing comment that needs a reflow also gets aligned properly.
20132 verifyFormat("switch (level) {\n"
20133 "case log::info: return \"info\";\n"
20134 "case log::warning: // fallthrough\n"
20135 "case log::error: return \"error\";\n"
20136 "}",
20137 "switch (level) {\n"
20138 "case log::info: return \"info\";\n"
20139 "case log::warning: //fallthrough\n"
20140 "case log::error: return \"error\";\n"
20141 "}",
20142 Alignment);
20144 // Verify adjacent non-short case statements don't change the alignment, and
20145 // properly break the set of consecutive statements.
20146 verifyFormat("switch (level) {\n"
20147 "case log::critical:\n"
20148 " // comment\n"
20149 " return \"critical\";\n"
20150 "case log::info: return \"info\";\n"
20151 "case log::warning: return \"warning\";\n"
20152 "default:\n"
20153 " // comment\n"
20154 " return \"\";\n"
20155 "case log::error: return \"error\";\n"
20156 "case log::severe: return \"severe\";\n"
20157 "case log::extra_critical:\n"
20158 " // comment\n"
20159 " return \"extra critical\";\n"
20160 "}",
20161 Alignment);
20163 Alignment.SpaceBeforeCaseColon = true;
20164 verifyFormat("switch (level) {\n"
20165 "case log::info : return \"info\";\n"
20166 "case log::warning : return \"warning\";\n"
20167 "default : return \"default\";\n"
20168 "}",
20169 Alignment);
20170 Alignment.SpaceBeforeCaseColon = false;
20172 // Make sure we don't incorrectly align correctly across nested switch cases.
20173 verifyFormat("switch (level) {\n"
20174 "case log::info: return \"info\";\n"
20175 "case log::warning: return \"warning\";\n"
20176 "case log::other:\n"
20177 " switch (sublevel) {\n"
20178 " case log::info: return \"info\";\n"
20179 " case log::warning: return \"warning\";\n"
20180 " }\n"
20181 " break;\n"
20182 "case log::error: return \"error\";\n"
20183 "default: return \"default\";\n"
20184 "}",
20185 "switch (level) {\n"
20186 "case log::info: return \"info\";\n"
20187 "case log::warning: return \"warning\";\n"
20188 "case log::other: switch (sublevel) {\n"
20189 " case log::info: return \"info\";\n"
20190 " case log::warning: return \"warning\";\n"
20191 "}\n"
20192 "break;\n"
20193 "case log::error: return \"error\";\n"
20194 "default: return \"default\";\n"
20195 "}",
20196 Alignment);
20198 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
20200 verifyFormat("switch (level) {\n"
20201 "case log::info: return \"info\";\n"
20202 "\n"
20203 "case log::warning: return \"warning\";\n"
20204 "}",
20205 "switch (level) {\n"
20206 "case log::info: return \"info\";\n"
20207 "\n"
20208 "case log::warning: return \"warning\";\n"
20209 "}",
20210 Alignment);
20212 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
20214 verifyNoChange("switch (level) {\n"
20215 "case log::info: return \"info\";\n"
20216 "\n"
20217 "/* block comment */\n"
20218 "\n"
20219 "// line comment\n"
20220 "case log::warning: return \"warning\";\n"
20221 "}",
20222 Alignment);
20224 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
20226 verifyFormat("switch (level) {\n"
20227 "case log::info: return \"info\";\n"
20228 "//\n"
20229 "case log::warning: return \"warning\";\n"
20230 "}",
20231 Alignment);
20233 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
20235 verifyFormat("switch (level) {\n"
20236 "case log::info : return \"info\";\n"
20237 "case log::warning: return \"warning\";\n"
20238 "default : return \"default\";\n"
20239 "}",
20240 Alignment);
20242 // With AlignCaseColons, empty case statements don't break alignment of
20243 // consecutive case statements (and are aligned).
20244 verifyFormat("switch (level) {\n"
20245 "case log::info : return \"info\";\n"
20246 "case log::warning :\n"
20247 "case log::critical:\n"
20248 "default : return \"default\";\n"
20249 "}",
20250 Alignment);
20252 // Final non-short case labels shouldn't have their colon aligned
20253 verifyFormat("switch (level) {\n"
20254 "case log::info : return \"info\";\n"
20255 "case log::warning :\n"
20256 "case log::critical:\n"
20257 "case log::severe : return \"severe\";\n"
20258 "default:\n"
20259 " // comment\n"
20260 " return \"default\";\n"
20261 "}",
20262 Alignment);
20264 // Verify adjacent non-short case statements break the set of consecutive
20265 // alignments and aren't aligned with adjacent non-short case statements if
20266 // AlignCaseColons is set.
20267 verifyFormat("switch (level) {\n"
20268 "case log::critical:\n"
20269 " // comment\n"
20270 " return \"critical\";\n"
20271 "case log::info : return \"info\";\n"
20272 "case log::warning: return \"warning\";\n"
20273 "default:\n"
20274 " // comment\n"
20275 " return \"\";\n"
20276 "case log::error : return \"error\";\n"
20277 "case log::severe: return \"severe\";\n"
20278 "case log::extra_critical:\n"
20279 " // comment\n"
20280 " return \"extra critical\";\n"
20281 "}",
20282 Alignment);
20284 Alignment.SpaceBeforeCaseColon = true;
20285 verifyFormat("switch (level) {\n"
20286 "case log::info : return \"info\";\n"
20287 "case log::warning : return \"warning\";\n"
20288 "case log::error :\n"
20289 "default : return \"default\";\n"
20290 "}",
20291 Alignment);
20294 TEST_F(FormatTest, AlignWithLineBreaks) {
20295 auto Style = getLLVMStyleWithColumns(120);
20297 EXPECT_EQ(Style.AlignConsecutiveAssignments,
20298 FormatStyle::AlignConsecutiveStyle(
20299 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20300 /*AcrossComments=*/false, /*AlignCompound=*/false,
20301 /*AlignFunctionDeclarations=*/false,
20302 /*AlignFunctionPointers=*/false,
20303 /*PadOperators=*/true}));
20304 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20305 FormatStyle::AlignConsecutiveStyle(
20306 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20307 /*AcrossComments=*/false, /*AlignCompound=*/false,
20308 /*AlignFunctionDeclarations=*/true,
20309 /*AlignFunctionPointers=*/false,
20310 /*PadOperators=*/false}));
20311 verifyFormat("void foo() {\n"
20312 " int myVar = 5;\n"
20313 " double x = 3.14;\n"
20314 " auto str = \"Hello \"\n"
20315 " \"World\";\n"
20316 " auto s = \"Hello \"\n"
20317 " \"Again\";\n"
20318 "}",
20319 Style);
20321 // clang-format off
20322 verifyFormat("void foo() {\n"
20323 " const int capacityBefore = Entries.capacity();\n"
20324 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20325 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20326 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20327 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20328 "}",
20329 Style);
20330 // clang-format on
20332 Style.AlignConsecutiveAssignments.Enabled = true;
20333 verifyFormat("void foo() {\n"
20334 " int myVar = 5;\n"
20335 " double x = 3.14;\n"
20336 " auto str = \"Hello \"\n"
20337 " \"World\";\n"
20338 " auto s = \"Hello \"\n"
20339 " \"Again\";\n"
20340 "}",
20341 Style);
20343 // clang-format off
20344 verifyFormat("void foo() {\n"
20345 " const int capacityBefore = Entries.capacity();\n"
20346 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20347 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20348 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20349 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20350 "}",
20351 Style);
20352 // clang-format on
20354 Style.AlignConsecutiveAssignments.Enabled = false;
20355 Style.AlignConsecutiveDeclarations.Enabled = true;
20356 verifyFormat("void foo() {\n"
20357 " int myVar = 5;\n"
20358 " double x = 3.14;\n"
20359 " auto str = \"Hello \"\n"
20360 " \"World\";\n"
20361 " auto s = \"Hello \"\n"
20362 " \"Again\";\n"
20363 "}",
20364 Style);
20366 // clang-format off
20367 verifyFormat("void foo() {\n"
20368 " const int capacityBefore = Entries.capacity();\n"
20369 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20370 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20371 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20372 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20373 "}",
20374 Style);
20375 // clang-format on
20377 Style.AlignConsecutiveAssignments.Enabled = true;
20378 Style.AlignConsecutiveDeclarations.Enabled = true;
20380 verifyFormat("void foo() {\n"
20381 " int myVar = 5;\n"
20382 " double x = 3.14;\n"
20383 " auto str = \"Hello \"\n"
20384 " \"World\";\n"
20385 " auto s = \"Hello \"\n"
20386 " \"Again\";\n"
20387 "}",
20388 Style);
20390 // clang-format off
20391 verifyFormat("void foo() {\n"
20392 " const int capacityBefore = Entries.capacity();\n"
20393 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20394 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20395 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20396 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20397 "}",
20398 Style);
20399 // clang-format on
20401 Style = getLLVMStyleWithColumns(20);
20402 Style.AlignConsecutiveAssignments.Enabled = true;
20403 Style.IndentWidth = 4;
20405 verifyFormat("void foo() {\n"
20406 " int i1 = 1;\n"
20407 " int j = 0;\n"
20408 " int k = bar(\n"
20409 " argument1,\n"
20410 " argument2);\n"
20411 "}",
20412 Style);
20414 verifyFormat("unsigned i = 0;\n"
20415 "int a[] = {\n"
20416 " 1234567890,\n"
20417 " -1234567890};",
20418 Style);
20420 Style.ColumnLimit = 120;
20422 // clang-format off
20423 verifyFormat("void SomeFunc() {\n"
20424 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20425 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20426 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20427 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20428 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20429 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20430 "}",
20431 Style);
20432 // clang-format on
20434 Style.BinPackArguments = false;
20436 // clang-format off
20437 verifyFormat("void SomeFunc() {\n"
20438 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20439 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20440 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20441 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20442 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20443 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20444 "}",
20445 Style);
20446 // clang-format on
20449 TEST_F(FormatTest, AlignWithInitializerPeriods) {
20450 auto Style = getLLVMStyleWithColumns(60);
20452 verifyFormat("void foo1(void) {\n"
20453 " BYTE p[1] = 1;\n"
20454 " A B = {.one_foooooooooooooooo = 2,\n"
20455 " .two_fooooooooooooo = 3,\n"
20456 " .three_fooooooooooooo = 4};\n"
20457 " BYTE payload = 2;\n"
20458 "}",
20459 Style);
20461 Style.AlignConsecutiveAssignments.Enabled = true;
20462 Style.AlignConsecutiveDeclarations.Enabled = false;
20463 verifyFormat("void foo2(void) {\n"
20464 " BYTE p[1] = 1;\n"
20465 " A B = {.one_foooooooooooooooo = 2,\n"
20466 " .two_fooooooooooooo = 3,\n"
20467 " .three_fooooooooooooo = 4};\n"
20468 " BYTE payload = 2;\n"
20469 "}",
20470 Style);
20472 Style.AlignConsecutiveAssignments.Enabled = false;
20473 Style.AlignConsecutiveDeclarations.Enabled = true;
20474 verifyFormat("void foo3(void) {\n"
20475 " BYTE p[1] = 1;\n"
20476 " A B = {.one_foooooooooooooooo = 2,\n"
20477 " .two_fooooooooooooo = 3,\n"
20478 " .three_fooooooooooooo = 4};\n"
20479 " BYTE payload = 2;\n"
20480 "}",
20481 Style);
20483 Style.AlignConsecutiveAssignments.Enabled = true;
20484 Style.AlignConsecutiveDeclarations.Enabled = true;
20485 verifyFormat("void foo4(void) {\n"
20486 " BYTE p[1] = 1;\n"
20487 " A B = {.one_foooooooooooooooo = 2,\n"
20488 " .two_fooooooooooooo = 3,\n"
20489 " .three_fooooooooooooo = 4};\n"
20490 " BYTE payload = 2;\n"
20491 "}",
20492 Style);
20495 TEST_F(FormatTest, LinuxBraceBreaking) {
20496 FormatStyle LinuxBraceStyle = getLLVMStyle();
20497 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
20498 verifyFormat("namespace a\n"
20499 "{\n"
20500 "class A\n"
20501 "{\n"
20502 " void f()\n"
20503 " {\n"
20504 " if (true) {\n"
20505 " a();\n"
20506 " b();\n"
20507 " } else {\n"
20508 " a();\n"
20509 " }\n"
20510 " }\n"
20511 " void g() { return; }\n"
20512 "};\n"
20513 "struct B {\n"
20514 " int x;\n"
20515 "};\n"
20516 "} // namespace a",
20517 LinuxBraceStyle);
20518 verifyFormat("enum X {\n"
20519 " Y = 0,\n"
20520 "}",
20521 LinuxBraceStyle);
20522 verifyFormat("struct S {\n"
20523 " int Type;\n"
20524 " union {\n"
20525 " int x;\n"
20526 " double y;\n"
20527 " } Value;\n"
20528 " class C\n"
20529 " {\n"
20530 " MyFavoriteType Value;\n"
20531 " } Class;\n"
20532 "}",
20533 LinuxBraceStyle);
20536 TEST_F(FormatTest, MozillaBraceBreaking) {
20537 FormatStyle MozillaBraceStyle = getLLVMStyle();
20538 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
20539 MozillaBraceStyle.FixNamespaceComments = false;
20540 verifyFormat("namespace a {\n"
20541 "class A\n"
20542 "{\n"
20543 " void f()\n"
20544 " {\n"
20545 " if (true) {\n"
20546 " a();\n"
20547 " b();\n"
20548 " }\n"
20549 " }\n"
20550 " void g() { return; }\n"
20551 "};\n"
20552 "enum E\n"
20553 "{\n"
20554 " A,\n"
20555 " // foo\n"
20556 " B,\n"
20557 " C\n"
20558 "};\n"
20559 "struct B\n"
20560 "{\n"
20561 " int x;\n"
20562 "};\n"
20563 "}",
20564 MozillaBraceStyle);
20565 verifyFormat("struct S\n"
20566 "{\n"
20567 " int Type;\n"
20568 " union\n"
20569 " {\n"
20570 " int x;\n"
20571 " double y;\n"
20572 " } Value;\n"
20573 " class C\n"
20574 " {\n"
20575 " MyFavoriteType Value;\n"
20576 " } Class;\n"
20577 "}",
20578 MozillaBraceStyle);
20581 TEST_F(FormatTest, StroustrupBraceBreaking) {
20582 FormatStyle StroustrupBraceStyle = getLLVMStyle();
20583 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
20584 verifyFormat("namespace a {\n"
20585 "class A {\n"
20586 " void f()\n"
20587 " {\n"
20588 " if (true) {\n"
20589 " a();\n"
20590 " b();\n"
20591 " }\n"
20592 " }\n"
20593 " void g() { return; }\n"
20594 "};\n"
20595 "struct B {\n"
20596 " int x;\n"
20597 "};\n"
20598 "} // namespace a",
20599 StroustrupBraceStyle);
20601 verifyFormat("void foo()\n"
20602 "{\n"
20603 " if (a) {\n"
20604 " a();\n"
20605 " }\n"
20606 " else {\n"
20607 " b();\n"
20608 " }\n"
20609 "}",
20610 StroustrupBraceStyle);
20612 verifyFormat("#ifdef _DEBUG\n"
20613 "int foo(int i = 0)\n"
20614 "#else\n"
20615 "int foo(int i = 5)\n"
20616 "#endif\n"
20617 "{\n"
20618 " return i;\n"
20619 "}",
20620 StroustrupBraceStyle);
20622 verifyFormat("void foo() {}\n"
20623 "void bar()\n"
20624 "#ifdef _DEBUG\n"
20625 "{\n"
20626 " foo();\n"
20627 "}\n"
20628 "#else\n"
20629 "{\n"
20630 "}\n"
20631 "#endif",
20632 StroustrupBraceStyle);
20634 verifyFormat("void foobar() { int i = 5; }\n"
20635 "#ifdef _DEBUG\n"
20636 "void bar() {}\n"
20637 "#else\n"
20638 "void bar() { foobar(); }\n"
20639 "#endif",
20640 StroustrupBraceStyle);
20643 TEST_F(FormatTest, AllmanBraceBreaking) {
20644 FormatStyle AllmanBraceStyle = getLLVMStyle();
20645 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
20647 verifyFormat("namespace a\n"
20648 "{\n"
20649 "void f();\n"
20650 "void g();\n"
20651 "} // namespace a",
20652 "namespace a\n"
20653 "{\n"
20654 "void f();\n"
20655 "void g();\n"
20656 "}",
20657 AllmanBraceStyle);
20659 verifyFormat("namespace a\n"
20660 "{\n"
20661 "class A\n"
20662 "{\n"
20663 " void f()\n"
20664 " {\n"
20665 " if (true)\n"
20666 " {\n"
20667 " a();\n"
20668 " b();\n"
20669 " }\n"
20670 " }\n"
20671 " void g() { return; }\n"
20672 "};\n"
20673 "struct B\n"
20674 "{\n"
20675 " int x;\n"
20676 "};\n"
20677 "union C\n"
20678 "{\n"
20679 "};\n"
20680 "} // namespace a",
20681 AllmanBraceStyle);
20683 verifyFormat("void f()\n"
20684 "{\n"
20685 " if (true)\n"
20686 " {\n"
20687 " a();\n"
20688 " }\n"
20689 " else if (false)\n"
20690 " {\n"
20691 " b();\n"
20692 " }\n"
20693 " else\n"
20694 " {\n"
20695 " c();\n"
20696 " }\n"
20697 "}",
20698 AllmanBraceStyle);
20700 verifyFormat("void f()\n"
20701 "{\n"
20702 " for (int i = 0; i < 10; ++i)\n"
20703 " {\n"
20704 " a();\n"
20705 " }\n"
20706 " while (false)\n"
20707 " {\n"
20708 " b();\n"
20709 " }\n"
20710 " do\n"
20711 " {\n"
20712 " c();\n"
20713 " } while (false)\n"
20714 "}",
20715 AllmanBraceStyle);
20717 verifyFormat("void f(int a)\n"
20718 "{\n"
20719 " switch (a)\n"
20720 " {\n"
20721 " case 0:\n"
20722 " break;\n"
20723 " case 1:\n"
20724 " {\n"
20725 " break;\n"
20726 " }\n"
20727 " case 2:\n"
20728 " {\n"
20729 " }\n"
20730 " break;\n"
20731 " default:\n"
20732 " break;\n"
20733 " }\n"
20734 "}",
20735 AllmanBraceStyle);
20737 verifyFormat("enum X\n"
20738 "{\n"
20739 " Y = 0,\n"
20740 "}",
20741 AllmanBraceStyle);
20742 verifyFormat("enum X\n"
20743 "{\n"
20744 " Y = 0\n"
20745 "}",
20746 AllmanBraceStyle);
20748 verifyFormat("@interface BSApplicationController ()\n"
20749 "{\n"
20750 "@private\n"
20751 " id _extraIvar;\n"
20752 "}\n"
20753 "@end",
20754 AllmanBraceStyle);
20756 verifyFormat("#ifdef _DEBUG\n"
20757 "int foo(int i = 0)\n"
20758 "#else\n"
20759 "int foo(int i = 5)\n"
20760 "#endif\n"
20761 "{\n"
20762 " return i;\n"
20763 "}",
20764 AllmanBraceStyle);
20766 verifyFormat("void foo() {}\n"
20767 "void bar()\n"
20768 "#ifdef _DEBUG\n"
20769 "{\n"
20770 " foo();\n"
20771 "}\n"
20772 "#else\n"
20773 "{\n"
20774 "}\n"
20775 "#endif",
20776 AllmanBraceStyle);
20778 verifyFormat("void foobar() { int i = 5; }\n"
20779 "#ifdef _DEBUG\n"
20780 "void bar() {}\n"
20781 "#else\n"
20782 "void bar() { foobar(); }\n"
20783 "#endif",
20784 AllmanBraceStyle);
20786 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
20787 FormatStyle::SLS_All);
20789 verifyFormat("[](int i) { return i + 2; };\n"
20790 "[](int i, int j)\n"
20791 "{\n"
20792 " auto x = i + j;\n"
20793 " auto y = i * j;\n"
20794 " return x ^ y;\n"
20795 "};\n"
20796 "void foo()\n"
20797 "{\n"
20798 " auto shortLambda = [](int i) { return i + 2; };\n"
20799 " auto longLambda = [](int i, int j)\n"
20800 " {\n"
20801 " auto x = i + j;\n"
20802 " auto y = i * j;\n"
20803 " return x ^ y;\n"
20804 " };\n"
20805 "}",
20806 AllmanBraceStyle);
20808 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20810 verifyFormat("[](int i)\n"
20811 "{\n"
20812 " return i + 2;\n"
20813 "};\n"
20814 "[](int i, int j)\n"
20815 "{\n"
20816 " auto x = i + j;\n"
20817 " auto y = i * j;\n"
20818 " return x ^ y;\n"
20819 "};\n"
20820 "void foo()\n"
20821 "{\n"
20822 " auto shortLambda = [](int i)\n"
20823 " {\n"
20824 " return i + 2;\n"
20825 " };\n"
20826 " auto longLambda = [](int i, int j)\n"
20827 " {\n"
20828 " auto x = i + j;\n"
20829 " auto y = i * j;\n"
20830 " return x ^ y;\n"
20831 " };\n"
20832 "}",
20833 AllmanBraceStyle);
20835 // Reset
20836 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
20838 // This shouldn't affect ObjC blocks..
20839 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20840 " // ...\n"
20841 " int i;\n"
20842 "}];",
20843 AllmanBraceStyle);
20844 verifyFormat("void (^block)(void) = ^{\n"
20845 " // ...\n"
20846 " int i;\n"
20847 "};",
20848 AllmanBraceStyle);
20849 // .. or dict literals.
20850 verifyFormat("void f()\n"
20851 "{\n"
20852 " // ...\n"
20853 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20854 "}",
20855 AllmanBraceStyle);
20856 verifyFormat("void f()\n"
20857 "{\n"
20858 " // ...\n"
20859 " [object someMethod:@{a : @\"b\"}];\n"
20860 "}",
20861 AllmanBraceStyle);
20862 verifyFormat("int f()\n"
20863 "{ // comment\n"
20864 " return 42;\n"
20865 "}",
20866 AllmanBraceStyle);
20868 AllmanBraceStyle.ColumnLimit = 19;
20869 verifyFormat("void f() { int i; }", AllmanBraceStyle);
20870 AllmanBraceStyle.ColumnLimit = 18;
20871 verifyFormat("void f()\n"
20872 "{\n"
20873 " int i;\n"
20874 "}",
20875 AllmanBraceStyle);
20876 AllmanBraceStyle.ColumnLimit = 80;
20878 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
20879 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20880 FormatStyle::SIS_WithoutElse;
20881 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20882 verifyFormat("void f(bool b)\n"
20883 "{\n"
20884 " if (b)\n"
20885 " {\n"
20886 " return;\n"
20887 " }\n"
20888 "}",
20889 BreakBeforeBraceShortIfs);
20890 verifyFormat("void f(bool b)\n"
20891 "{\n"
20892 " if constexpr (b)\n"
20893 " {\n"
20894 " return;\n"
20895 " }\n"
20896 "}",
20897 BreakBeforeBraceShortIfs);
20898 verifyFormat("void f(bool b)\n"
20899 "{\n"
20900 " if CONSTEXPR (b)\n"
20901 " {\n"
20902 " return;\n"
20903 " }\n"
20904 "}",
20905 BreakBeforeBraceShortIfs);
20906 verifyFormat("void f(bool b)\n"
20907 "{\n"
20908 " if (b) return;\n"
20909 "}",
20910 BreakBeforeBraceShortIfs);
20911 verifyFormat("void f(bool b)\n"
20912 "{\n"
20913 " if constexpr (b) return;\n"
20914 "}",
20915 BreakBeforeBraceShortIfs);
20916 verifyFormat("void f(bool b)\n"
20917 "{\n"
20918 " if CONSTEXPR (b) return;\n"
20919 "}",
20920 BreakBeforeBraceShortIfs);
20921 verifyFormat("void f(bool b)\n"
20922 "{\n"
20923 " while (b)\n"
20924 " {\n"
20925 " return;\n"
20926 " }\n"
20927 "}",
20928 BreakBeforeBraceShortIfs);
20931 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
20932 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
20933 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
20935 // Make a few changes to the style for testing purposes
20936 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
20937 FormatStyle::SFS_Empty;
20938 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20940 // FIXME: this test case can't decide whether there should be a blank line
20941 // after the ~D() line or not. It adds one if one doesn't exist in the test
20942 // and it removes the line if one exists.
20944 verifyFormat("class A;\n"
20945 "namespace B\n"
20946 " {\n"
20947 "class C;\n"
20948 "// Comment\n"
20949 "class D\n"
20950 " {\n"
20951 "public:\n"
20952 " D();\n"
20953 " ~D() {}\n"
20954 "private:\n"
20955 " enum E\n"
20956 " {\n"
20957 " F\n"
20958 " }\n"
20959 " };\n"
20960 " } // namespace B",
20961 WhitesmithsBraceStyle);
20964 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
20965 verifyFormat("namespace a\n"
20966 " {\n"
20967 "class A\n"
20968 " {\n"
20969 " void f()\n"
20970 " {\n"
20971 " if (true)\n"
20972 " {\n"
20973 " a();\n"
20974 " b();\n"
20975 " }\n"
20976 " }\n"
20977 " void g()\n"
20978 " {\n"
20979 " return;\n"
20980 " }\n"
20981 " };\n"
20982 "struct B\n"
20983 " {\n"
20984 " int x;\n"
20985 " };\n"
20986 " } // namespace a",
20987 WhitesmithsBraceStyle);
20989 verifyFormat("namespace a\n"
20990 " {\n"
20991 "namespace b\n"
20992 " {\n"
20993 "class A\n"
20994 " {\n"
20995 " void f()\n"
20996 " {\n"
20997 " if (true)\n"
20998 " {\n"
20999 " a();\n"
21000 " b();\n"
21001 " }\n"
21002 " }\n"
21003 " void g()\n"
21004 " {\n"
21005 " return;\n"
21006 " }\n"
21007 " };\n"
21008 "struct B\n"
21009 " {\n"
21010 " int x;\n"
21011 " };\n"
21012 " } // namespace b\n"
21013 " } // namespace a",
21014 WhitesmithsBraceStyle);
21016 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
21017 verifyFormat("namespace a\n"
21018 " {\n"
21019 "namespace b\n"
21020 " {\n"
21021 " class A\n"
21022 " {\n"
21023 " void f()\n"
21024 " {\n"
21025 " if (true)\n"
21026 " {\n"
21027 " a();\n"
21028 " b();\n"
21029 " }\n"
21030 " }\n"
21031 " void g()\n"
21032 " {\n"
21033 " return;\n"
21034 " }\n"
21035 " };\n"
21036 " struct B\n"
21037 " {\n"
21038 " int x;\n"
21039 " };\n"
21040 " } // namespace b\n"
21041 " } // namespace a",
21042 WhitesmithsBraceStyle);
21044 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
21045 verifyFormat("namespace a\n"
21046 " {\n"
21047 " namespace b\n"
21048 " {\n"
21049 " class A\n"
21050 " {\n"
21051 " void f()\n"
21052 " {\n"
21053 " if (true)\n"
21054 " {\n"
21055 " a();\n"
21056 " b();\n"
21057 " }\n"
21058 " }\n"
21059 " void g()\n"
21060 " {\n"
21061 " return;\n"
21062 " }\n"
21063 " };\n"
21064 " struct B\n"
21065 " {\n"
21066 " int x;\n"
21067 " };\n"
21068 " } // namespace b\n"
21069 " } // namespace a",
21070 WhitesmithsBraceStyle);
21072 verifyFormat("void f()\n"
21073 " {\n"
21074 " if (true)\n"
21075 " {\n"
21076 " a();\n"
21077 " }\n"
21078 " else if (false)\n"
21079 " {\n"
21080 " b();\n"
21081 " }\n"
21082 " else\n"
21083 " {\n"
21084 " c();\n"
21085 " }\n"
21086 " }",
21087 WhitesmithsBraceStyle);
21089 verifyFormat("void f()\n"
21090 " {\n"
21091 " for (int i = 0; i < 10; ++i)\n"
21092 " {\n"
21093 " a();\n"
21094 " }\n"
21095 " while (false)\n"
21096 " {\n"
21097 " b();\n"
21098 " }\n"
21099 " do\n"
21100 " {\n"
21101 " c();\n"
21102 " } while (false)\n"
21103 " }",
21104 WhitesmithsBraceStyle);
21106 WhitesmithsBraceStyle.IndentCaseLabels = true;
21107 verifyFormat("void switchTest1(int a)\n"
21108 " {\n"
21109 " switch (a)\n"
21110 " {\n"
21111 " case 2:\n"
21112 " {\n"
21113 " }\n"
21114 " break;\n"
21115 " }\n"
21116 " }",
21117 WhitesmithsBraceStyle);
21119 verifyFormat("void switchTest2(int a)\n"
21120 " {\n"
21121 " switch (a)\n"
21122 " {\n"
21123 " case 0:\n"
21124 " break;\n"
21125 " case 1:\n"
21126 " {\n"
21127 " break;\n"
21128 " }\n"
21129 " case 2:\n"
21130 " {\n"
21131 " }\n"
21132 " break;\n"
21133 " default:\n"
21134 " break;\n"
21135 " }\n"
21136 " }",
21137 WhitesmithsBraceStyle);
21139 verifyFormat("void switchTest3(int a)\n"
21140 " {\n"
21141 " switch (a)\n"
21142 " {\n"
21143 " case 0:\n"
21144 " {\n"
21145 " foo(x);\n"
21146 " }\n"
21147 " break;\n"
21148 " default:\n"
21149 " {\n"
21150 " foo(1);\n"
21151 " }\n"
21152 " break;\n"
21153 " }\n"
21154 " }",
21155 WhitesmithsBraceStyle);
21157 WhitesmithsBraceStyle.IndentCaseLabels = false;
21159 verifyFormat("void switchTest4(int a)\n"
21160 " {\n"
21161 " switch (a)\n"
21162 " {\n"
21163 " case 2:\n"
21164 " {\n"
21165 " }\n"
21166 " break;\n"
21167 " }\n"
21168 " }",
21169 WhitesmithsBraceStyle);
21171 verifyFormat("void switchTest5(int a)\n"
21172 " {\n"
21173 " switch (a)\n"
21174 " {\n"
21175 " case 0:\n"
21176 " break;\n"
21177 " case 1:\n"
21178 " {\n"
21179 " foo();\n"
21180 " break;\n"
21181 " }\n"
21182 " case 2:\n"
21183 " {\n"
21184 " }\n"
21185 " break;\n"
21186 " default:\n"
21187 " break;\n"
21188 " }\n"
21189 " }",
21190 WhitesmithsBraceStyle);
21192 verifyFormat("void switchTest6(int a)\n"
21193 " {\n"
21194 " switch (a)\n"
21195 " {\n"
21196 " case 0:\n"
21197 " {\n"
21198 " foo(x);\n"
21199 " }\n"
21200 " break;\n"
21201 " default:\n"
21202 " {\n"
21203 " foo(1);\n"
21204 " }\n"
21205 " break;\n"
21206 " }\n"
21207 " }",
21208 WhitesmithsBraceStyle);
21210 verifyFormat("enum X\n"
21211 " {\n"
21212 " Y = 0, // testing\n"
21213 " }",
21214 WhitesmithsBraceStyle);
21216 verifyFormat("enum X\n"
21217 " {\n"
21218 " Y = 0\n"
21219 " }",
21220 WhitesmithsBraceStyle);
21221 verifyFormat("enum X\n"
21222 " {\n"
21223 " Y = 0,\n"
21224 " Z = 1\n"
21225 " };",
21226 WhitesmithsBraceStyle);
21228 verifyFormat("@interface BSApplicationController ()\n"
21229 " {\n"
21230 "@private\n"
21231 " id _extraIvar;\n"
21232 " }\n"
21233 "@end",
21234 WhitesmithsBraceStyle);
21236 verifyFormat("#ifdef _DEBUG\n"
21237 "int foo(int i = 0)\n"
21238 "#else\n"
21239 "int foo(int i = 5)\n"
21240 "#endif\n"
21241 " {\n"
21242 " return i;\n"
21243 " }",
21244 WhitesmithsBraceStyle);
21246 verifyFormat("void foo() {}\n"
21247 "void bar()\n"
21248 "#ifdef _DEBUG\n"
21249 " {\n"
21250 " foo();\n"
21251 " }\n"
21252 "#else\n"
21253 " {\n"
21254 " }\n"
21255 "#endif",
21256 WhitesmithsBraceStyle);
21258 verifyFormat("void foobar()\n"
21259 " {\n"
21260 " int i = 5;\n"
21261 " }\n"
21262 "#ifdef _DEBUG\n"
21263 "void bar() {}\n"
21264 "#else\n"
21265 "void bar()\n"
21266 " {\n"
21267 " foobar();\n"
21268 " }\n"
21269 "#endif",
21270 WhitesmithsBraceStyle);
21272 // This shouldn't affect ObjC blocks..
21273 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21274 " // ...\n"
21275 " int i;\n"
21276 "}];",
21277 WhitesmithsBraceStyle);
21278 verifyFormat("void (^block)(void) = ^{\n"
21279 " // ...\n"
21280 " int i;\n"
21281 "};",
21282 WhitesmithsBraceStyle);
21283 // .. or dict literals.
21284 verifyFormat("void f()\n"
21285 " {\n"
21286 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21287 " }",
21288 WhitesmithsBraceStyle);
21290 verifyFormat("int f()\n"
21291 " { // comment\n"
21292 " return 42;\n"
21293 " }",
21294 WhitesmithsBraceStyle);
21296 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
21297 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21298 FormatStyle::SIS_OnlyFirstIf;
21299 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21300 verifyFormat("void f(bool b)\n"
21301 " {\n"
21302 " if (b)\n"
21303 " {\n"
21304 " return;\n"
21305 " }\n"
21306 " }",
21307 BreakBeforeBraceShortIfs);
21308 verifyFormat("void f(bool b)\n"
21309 " {\n"
21310 " if (b) return;\n"
21311 " }",
21312 BreakBeforeBraceShortIfs);
21313 verifyFormat("void f(bool b)\n"
21314 " {\n"
21315 " while (b)\n"
21316 " {\n"
21317 " return;\n"
21318 " }\n"
21319 " }",
21320 BreakBeforeBraceShortIfs);
21323 TEST_F(FormatTest, GNUBraceBreaking) {
21324 FormatStyle GNUBraceStyle = getLLVMStyle();
21325 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
21326 verifyFormat("namespace a\n"
21327 "{\n"
21328 "class A\n"
21329 "{\n"
21330 " void f()\n"
21331 " {\n"
21332 " int a;\n"
21333 " {\n"
21334 " int b;\n"
21335 " }\n"
21336 " if (true)\n"
21337 " {\n"
21338 " a();\n"
21339 " b();\n"
21340 " }\n"
21341 " }\n"
21342 " void g() { return; }\n"
21343 "}\n"
21344 "} // namespace a",
21345 GNUBraceStyle);
21347 verifyFormat("void f()\n"
21348 "{\n"
21349 " if (true)\n"
21350 " {\n"
21351 " a();\n"
21352 " }\n"
21353 " else if (false)\n"
21354 " {\n"
21355 " b();\n"
21356 " }\n"
21357 " else\n"
21358 " {\n"
21359 " c();\n"
21360 " }\n"
21361 "}",
21362 GNUBraceStyle);
21364 verifyFormat("void f()\n"
21365 "{\n"
21366 " for (int i = 0; i < 10; ++i)\n"
21367 " {\n"
21368 " a();\n"
21369 " }\n"
21370 " while (false)\n"
21371 " {\n"
21372 " b();\n"
21373 " }\n"
21374 " do\n"
21375 " {\n"
21376 " c();\n"
21377 " }\n"
21378 " while (false);\n"
21379 "}",
21380 GNUBraceStyle);
21382 verifyFormat("void f(int a)\n"
21383 "{\n"
21384 " switch (a)\n"
21385 " {\n"
21386 " case 0:\n"
21387 " break;\n"
21388 " case 1:\n"
21389 " {\n"
21390 " break;\n"
21391 " }\n"
21392 " case 2:\n"
21393 " {\n"
21394 " }\n"
21395 " break;\n"
21396 " default:\n"
21397 " break;\n"
21398 " }\n"
21399 "}",
21400 GNUBraceStyle);
21402 verifyFormat("enum X\n"
21403 "{\n"
21404 " Y = 0,\n"
21405 "}",
21406 GNUBraceStyle);
21408 verifyFormat("@interface BSApplicationController ()\n"
21409 "{\n"
21410 "@private\n"
21411 " id _extraIvar;\n"
21412 "}\n"
21413 "@end",
21414 GNUBraceStyle);
21416 verifyFormat("#ifdef _DEBUG\n"
21417 "int foo(int i = 0)\n"
21418 "#else\n"
21419 "int foo(int i = 5)\n"
21420 "#endif\n"
21421 "{\n"
21422 " return i;\n"
21423 "}",
21424 GNUBraceStyle);
21426 verifyFormat("void foo() {}\n"
21427 "void bar()\n"
21428 "#ifdef _DEBUG\n"
21429 "{\n"
21430 " foo();\n"
21431 "}\n"
21432 "#else\n"
21433 "{\n"
21434 "}\n"
21435 "#endif",
21436 GNUBraceStyle);
21438 verifyFormat("void foobar() { int i = 5; }\n"
21439 "#ifdef _DEBUG\n"
21440 "void bar() {}\n"
21441 "#else\n"
21442 "void bar() { foobar(); }\n"
21443 "#endif",
21444 GNUBraceStyle);
21447 TEST_F(FormatTest, WebKitBraceBreaking) {
21448 FormatStyle WebKitBraceStyle = getLLVMStyle();
21449 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
21450 WebKitBraceStyle.FixNamespaceComments = false;
21451 verifyFormat("namespace a {\n"
21452 "class A {\n"
21453 " void f()\n"
21454 " {\n"
21455 " if (true) {\n"
21456 " a();\n"
21457 " b();\n"
21458 " }\n"
21459 " }\n"
21460 " void g() { return; }\n"
21461 "};\n"
21462 "enum E {\n"
21463 " A,\n"
21464 " // foo\n"
21465 " B,\n"
21466 " C\n"
21467 "};\n"
21468 "struct B {\n"
21469 " int x;\n"
21470 "};\n"
21471 "}",
21472 WebKitBraceStyle);
21473 verifyFormat("struct S {\n"
21474 " int Type;\n"
21475 " union {\n"
21476 " int x;\n"
21477 " double y;\n"
21478 " } Value;\n"
21479 " class C {\n"
21480 " MyFavoriteType Value;\n"
21481 " } Class;\n"
21482 "};",
21483 WebKitBraceStyle);
21486 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
21487 verifyFormat("void f() {\n"
21488 " try {\n"
21489 " } catch (const Exception &e) {\n"
21490 " }\n"
21491 "}");
21494 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
21495 auto Style = getLLVMStyle();
21496 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21497 verifyNoCrash("f({\n"
21498 "table({}, table({{\"\", false}}, {}))\n"
21499 "});",
21500 Style);
21502 Style.AlignConsecutiveAssignments.Enabled = true;
21503 Style.AlignConsecutiveDeclarations.Enabled = true;
21504 verifyFormat("struct test demo[] = {\n"
21505 " {56, 23, \"hello\"},\n"
21506 " {-1, 93463, \"world\"},\n"
21507 " { 7, 5, \"!!\"}\n"
21508 "};",
21509 Style);
21511 verifyFormat("struct test demo[] = {\n"
21512 " {56, 23, \"hello\"}, // first line\n"
21513 " {-1, 93463, \"world\"}, // second line\n"
21514 " { 7, 5, \"!!\"} // third line\n"
21515 "};",
21516 Style);
21518 verifyFormat("struct test demo[4] = {\n"
21519 " { 56, 23, 21, \"oh\"}, // first line\n"
21520 " { -1, 93463, 22, \"my\"}, // second line\n"
21521 " { 7, 5, 1, \"goodness\"} // third line\n"
21522 " {234, 5, 1, \"gracious\"} // fourth line\n"
21523 "};",
21524 Style);
21526 verifyFormat("struct test demo[3] = {\n"
21527 " {56, 23, \"hello\"},\n"
21528 " {-1, 93463, \"world\"},\n"
21529 " { 7, 5, \"!!\"}\n"
21530 "};",
21531 Style);
21533 verifyFormat("struct test demo[3] = {\n"
21534 " {int{56}, 23, \"hello\"},\n"
21535 " {int{-1}, 93463, \"world\"},\n"
21536 " { int{7}, 5, \"!!\"}\n"
21537 "};",
21538 Style);
21540 verifyFormat("struct test demo[] = {\n"
21541 " {56, 23, \"hello\"},\n"
21542 " {-1, 93463, \"world\"},\n"
21543 " { 7, 5, \"!!\"},\n"
21544 "};",
21545 Style);
21547 verifyFormat("test demo[] = {\n"
21548 " {56, 23, \"hello\"},\n"
21549 " {-1, 93463, \"world\"},\n"
21550 " { 7, 5, \"!!\"},\n"
21551 "};",
21552 Style);
21554 verifyFormat("demo = std::array<struct test, 3>{\n"
21555 " test{56, 23, \"hello\"},\n"
21556 " test{-1, 93463, \"world\"},\n"
21557 " test{ 7, 5, \"!!\"},\n"
21558 "};",
21559 Style);
21561 verifyFormat("test demo[] = {\n"
21562 " {56, 23, \"hello\"},\n"
21563 "#if X\n"
21564 " {-1, 93463, \"world\"},\n"
21565 "#endif\n"
21566 " { 7, 5, \"!!\"}\n"
21567 "};",
21568 Style);
21570 verifyFormat(
21571 "test demo[] = {\n"
21572 " { 7, 23,\n"
21573 " \"hello world i am a very long line that really, in any\"\n"
21574 " \"just world, ought to be split over multiple lines\"},\n"
21575 " {-1, 93463, \"world\"},\n"
21576 " {56, 5, \"!!\"}\n"
21577 "};",
21578 Style);
21580 verifyNoCrash("Foo f[] = {\n"
21581 " [0] = { 1, },\n"
21582 " [i] { 1, },\n"
21583 "};",
21584 Style);
21585 verifyNoCrash("Foo foo[] = {\n"
21586 " [0] = {1, 1},\n"
21587 " [1] { 1, 1, },\n"
21588 " [2] { 1, 1, },\n"
21589 "};",
21590 Style);
21591 verifyNoCrash("test arr[] = {\n"
21592 "#define FOO(i) {i, i},\n"
21593 "SOME_GENERATOR(FOO)\n"
21594 "{2, 2}\n"
21595 "};",
21596 Style);
21598 verifyFormat("return GradForUnaryCwise(g, {\n"
21599 " {{\"sign\"}, \"Sign\", "
21600 " {\"x\", \"dy\"}},\n"
21601 " { {\"dx\"}, \"Mul\", {\"dy\""
21602 ", \"sign\"}},\n"
21603 "});",
21604 Style);
21606 Style.Cpp11BracedListStyle = false;
21607 verifyFormat("struct test demo[] = {\n"
21608 " { 56, 23, \"hello\" },\n"
21609 " { -1, 93463, \"world\" },\n"
21610 " { 7, 5, \"!!\" }\n"
21611 "};",
21612 Style);
21613 Style.Cpp11BracedListStyle = true;
21615 Style.ColumnLimit = 0;
21616 verifyFormat(
21617 "test demo[] = {\n"
21618 " {56, 23, \"hello world i am a very long line that really, "
21619 "in any just world, ought to be split over multiple lines\"},\n"
21620 " {-1, 93463, "
21621 " \"world\"},\n"
21622 " { 7, 5, "
21623 " \"!!\"},\n"
21624 "};",
21625 "test demo[] = {{56, 23, \"hello world i am a very long line "
21626 "that really, in any just world, ought to be split over multiple "
21627 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21628 Style);
21630 Style.ColumnLimit = 80;
21631 verifyFormat("test demo[] = {\n"
21632 " {56, 23, /* a comment */ \"hello\"},\n"
21633 " {-1, 93463, \"world\"},\n"
21634 " { 7, 5, \"!!\"}\n"
21635 "};",
21636 Style);
21638 verifyFormat("test demo[] = {\n"
21639 " {56, 23, \"hello\"},\n"
21640 " {-1, 93463, \"world\" /* comment here */},\n"
21641 " { 7, 5, \"!!\"}\n"
21642 "};",
21643 Style);
21645 verifyFormat("test demo[] = {\n"
21646 " {56, /* a comment */ 23, \"hello\"},\n"
21647 " {-1, 93463, \"world\"},\n"
21648 " { 7, 5, \"!!\"}\n"
21649 "};",
21650 Style);
21652 Style.ColumnLimit = 20;
21653 verifyFormat("demo = std::array<\n"
21654 " struct test, 3>{\n"
21655 " test{\n"
21656 " 56, 23,\n"
21657 " \"hello \"\n"
21658 " \"world i \"\n"
21659 " \"am a very \"\n"
21660 " \"long line \"\n"
21661 " \"that \"\n"
21662 " \"really, \"\n"
21663 " \"in any \"\n"
21664 " \"just \"\n"
21665 " \"world, \"\n"
21666 " \"ought to \"\n"
21667 " \"be split \"\n"
21668 " \"over \"\n"
21669 " \"multiple \"\n"
21670 " \"lines\"},\n"
21671 " test{-1, 93463,\n"
21672 " \"world\"},\n"
21673 " test{ 7, 5,\n"
21674 " \"!!\" },\n"
21675 "};",
21676 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21677 "i am a very long line that really, in any just world, ought "
21678 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21679 "test{7, 5, \"!!\"},};",
21680 Style);
21681 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21682 Style = getLLVMStyleWithColumns(50);
21683 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21684 verifyFormat("static A x = {\n"
21685 " {{init1, init2, init3, init4},\n"
21686 " {init1, init2, init3, init4}}\n"
21687 "};",
21688 Style);
21689 // TODO: Fix the indentations below when this option is fully functional.
21690 #if 0
21691 verifyFormat("int a[][] = {\n"
21692 " {\n"
21693 " {0, 2}, //\n"
21694 " {1, 2} //\n"
21695 " }\n"
21696 "};",
21697 Style);
21698 #endif
21699 Style.ColumnLimit = 100;
21700 verifyFormat(
21701 "test demo[] = {\n"
21702 " {56, 23,\n"
21703 " \"hello world i am a very long line that really, in any just world"
21704 ", ought to be split over \"\n"
21705 " \"multiple lines\" },\n"
21706 " {-1, 93463, \"world\"},\n"
21707 " { 7, 5, \"!!\"},\n"
21708 "};",
21709 "test demo[] = {{56, 23, \"hello world i am a very long line "
21710 "that really, in any just world, ought to be split over multiple "
21711 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21712 Style);
21714 Style = getLLVMStyleWithColumns(50);
21715 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21716 verifyFormat("struct test demo[] = {\n"
21717 " {56, 23, \"hello\"},\n"
21718 " {-1, 93463, \"world\"},\n"
21719 " { 7, 5, \"!!\"}\n"
21720 "};\n"
21721 "static A x = {\n"
21722 " {{init1, init2, init3, init4},\n"
21723 " {init1, init2, init3, init4}}\n"
21724 "};",
21725 Style);
21726 Style.ColumnLimit = 100;
21727 Style.AlignConsecutiveAssignments.AcrossComments = true;
21728 Style.AlignConsecutiveDeclarations.AcrossComments = true;
21729 verifyFormat("struct test demo[] = {\n"
21730 " {56, 23, \"hello\"},\n"
21731 " {-1, 93463, \"world\"},\n"
21732 " { 7, 5, \"!!\"}\n"
21733 "};\n"
21734 "struct test demo[4] = {\n"
21735 " { 56, 23, 21, \"oh\"}, // first line\n"
21736 " { -1, 93463, 22, \"my\"}, // second line\n"
21737 " { 7, 5, 1, \"goodness\"} // third line\n"
21738 " {234, 5, 1, \"gracious\"} // fourth line\n"
21739 "};",
21740 Style);
21741 verifyFormat(
21742 "test demo[] = {\n"
21743 " {56,\n"
21744 " \"hello world i am a very long line that really, in any just world"
21745 ", ought to be split over \"\n"
21746 " \"multiple lines\", 23},\n"
21747 " {-1, \"world\", 93463},\n"
21748 " { 7, \"!!\", 5},\n"
21749 "};",
21750 "test demo[] = {{56, \"hello world i am a very long line "
21751 "that really, in any just world, ought to be split over multiple "
21752 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21753 Style);
21756 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
21757 auto Style = getLLVMStyle();
21758 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21759 /* FIXME: This case gets misformatted.
21760 verifyFormat("auto foo = Items{\n"
21761 " Section{0, bar(), },\n"
21762 " Section{1, boo() }\n"
21763 "};",
21764 Style);
21766 verifyFormat("auto foo = Items{\n"
21767 " Section{\n"
21768 " 0, bar(),\n"
21769 " }\n"
21770 "};",
21771 Style);
21772 verifyFormat("struct test demo[] = {\n"
21773 " {56, 23, \"hello\"},\n"
21774 " {-1, 93463, \"world\"},\n"
21775 " {7, 5, \"!!\" }\n"
21776 "};",
21777 Style);
21778 verifyFormat("struct test demo[] = {\n"
21779 " {56, 23, \"hello\"}, // first line\n"
21780 " {-1, 93463, \"world\"}, // second line\n"
21781 " {7, 5, \"!!\" } // third line\n"
21782 "};",
21783 Style);
21784 verifyFormat("struct test demo[4] = {\n"
21785 " {56, 23, 21, \"oh\" }, // first line\n"
21786 " {-1, 93463, 22, \"my\" }, // second line\n"
21787 " {7, 5, 1, \"goodness\"} // third line\n"
21788 " {234, 5, 1, \"gracious\"} // fourth line\n"
21789 "};",
21790 Style);
21791 verifyFormat("struct test demo[3] = {\n"
21792 " {56, 23, \"hello\"},\n"
21793 " {-1, 93463, \"world\"},\n"
21794 " {7, 5, \"!!\" }\n"
21795 "};",
21796 Style);
21798 verifyFormat("struct test demo[3] = {\n"
21799 " {int{56}, 23, \"hello\"},\n"
21800 " {int{-1}, 93463, \"world\"},\n"
21801 " {int{7}, 5, \"!!\" }\n"
21802 "};",
21803 Style);
21804 verifyFormat("struct test demo[] = {\n"
21805 " {56, 23, \"hello\"},\n"
21806 " {-1, 93463, \"world\"},\n"
21807 " {7, 5, \"!!\" },\n"
21808 "};",
21809 Style);
21810 verifyFormat("test demo[] = {\n"
21811 " {56, 23, \"hello\"},\n"
21812 " {-1, 93463, \"world\"},\n"
21813 " {7, 5, \"!!\" },\n"
21814 "};",
21815 Style);
21816 verifyFormat("demo = std::array<struct test, 3>{\n"
21817 " test{56, 23, \"hello\"},\n"
21818 " test{-1, 93463, \"world\"},\n"
21819 " test{7, 5, \"!!\" },\n"
21820 "};",
21821 Style);
21822 verifyFormat("test demo[] = {\n"
21823 " {56, 23, \"hello\"},\n"
21824 "#if X\n"
21825 " {-1, 93463, \"world\"},\n"
21826 "#endif\n"
21827 " {7, 5, \"!!\" }\n"
21828 "};",
21829 Style);
21830 verifyFormat(
21831 "test demo[] = {\n"
21832 " {7, 23,\n"
21833 " \"hello world i am a very long line that really, in any\"\n"
21834 " \"just world, ought to be split over multiple lines\"},\n"
21835 " {-1, 93463, \"world\" },\n"
21836 " {56, 5, \"!!\" }\n"
21837 "};",
21838 Style);
21840 verifyNoCrash("Foo f[] = {\n"
21841 " [0] = { 1, },\n"
21842 " [i] { 1, },\n"
21843 "};",
21844 Style);
21845 verifyNoCrash("Foo foo[] = {\n"
21846 " [0] = {1, 1},\n"
21847 " [1] { 1, 1, },\n"
21848 " [2] { 1, 1, },\n"
21849 "};",
21850 Style);
21851 verifyNoCrash("test arr[] = {\n"
21852 "#define FOO(i) {i, i},\n"
21853 "SOME_GENERATOR(FOO)\n"
21854 "{2, 2}\n"
21855 "};",
21856 Style);
21858 verifyFormat("return GradForUnaryCwise(g, {\n"
21859 " {{\"sign\"}, \"Sign\", {\"x\", "
21860 "\"dy\"} },\n"
21861 " {{\"dx\"}, \"Mul\", "
21862 "{\"dy\", \"sign\"}},\n"
21863 "});",
21864 Style);
21866 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
21867 verifyFormat("#define FOO \\\n"
21868 " int foo[][2] = { \\\n"
21869 " {0, 1} \\\n"
21870 " };",
21871 Style);
21873 Style.Cpp11BracedListStyle = false;
21874 verifyFormat("struct test demo[] = {\n"
21875 " { 56, 23, \"hello\" },\n"
21876 " { -1, 93463, \"world\" },\n"
21877 " { 7, 5, \"!!\" }\n"
21878 "};",
21879 Style);
21880 Style.Cpp11BracedListStyle = true;
21882 Style.ColumnLimit = 0;
21883 verifyFormat(
21884 "test demo[] = {\n"
21885 " {56, 23, \"hello world i am a very long line that really, in any "
21886 "just world, ought to be split over multiple lines\"},\n"
21887 " {-1, 93463, \"world\" "
21888 " },\n"
21889 " {7, 5, \"!!\" "
21890 " },\n"
21891 "};",
21892 "test demo[] = {{56, 23, \"hello world i am a very long line "
21893 "that really, in any just world, ought to be split over multiple "
21894 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21895 Style);
21897 Style.ColumnLimit = 80;
21898 verifyFormat("test demo[] = {\n"
21899 " {56, 23, /* a comment */ \"hello\"},\n"
21900 " {-1, 93463, \"world\" },\n"
21901 " {7, 5, \"!!\" }\n"
21902 "};",
21903 Style);
21905 verifyFormat("test demo[] = {\n"
21906 " {56, 23, \"hello\" },\n"
21907 " {-1, 93463, \"world\" /* comment here */},\n"
21908 " {7, 5, \"!!\" }\n"
21909 "};",
21910 Style);
21912 verifyFormat("test demo[] = {\n"
21913 " {56, /* a comment */ 23, \"hello\"},\n"
21914 " {-1, 93463, \"world\"},\n"
21915 " {7, 5, \"!!\" }\n"
21916 "};",
21917 Style);
21918 verifyFormat("Foo foo = {\n"
21919 " // comment\n"
21920 " {1, 2}\n"
21921 "};",
21922 Style);
21924 Style.ColumnLimit = 20;
21925 // FIXME: unstable test case
21926 EXPECT_EQ(
21927 "demo = std::array<\n"
21928 " struct test, 3>{\n"
21929 " test{\n"
21930 " 56, 23,\n"
21931 " \"hello \"\n"
21932 " \"world i \"\n"
21933 " \"am a very \"\n"
21934 " \"long line \"\n"
21935 " \"that \"\n"
21936 " \"really, \"\n"
21937 " \"in any \"\n"
21938 " \"just \"\n"
21939 " \"world, \"\n"
21940 " \"ought to \"\n"
21941 " \"be split \"\n"
21942 " \"over \"\n"
21943 " \"multiple \"\n"
21944 " \"lines\"},\n"
21945 " test{-1, 93463,\n"
21946 " \"world\"},\n"
21947 " test{7, 5,\n"
21948 " \"!!\" },\n"
21949 "};",
21950 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21951 "i am a very long line that really, in any just world, ought "
21952 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21953 "test{7, 5, \"!!\"},};",
21954 Style));
21956 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21957 Style = getLLVMStyleWithColumns(50);
21958 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21959 verifyFormat("static A x = {\n"
21960 " {{init1, init2, init3, init4},\n"
21961 " {init1, init2, init3, init4}}\n"
21962 "};",
21963 Style);
21964 Style.ColumnLimit = 100;
21965 verifyFormat(
21966 "test demo[] = {\n"
21967 " {56, 23,\n"
21968 " \"hello world i am a very long line that really, in any just world"
21969 ", ought to be split over \"\n"
21970 " \"multiple lines\" },\n"
21971 " {-1, 93463, \"world\"},\n"
21972 " {7, 5, \"!!\" },\n"
21973 "};",
21974 "test demo[] = {{56, 23, \"hello world i am a very long line "
21975 "that really, in any just world, ought to be split over multiple "
21976 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21977 Style);
21979 Style.ColumnLimit = 25;
21980 verifyNoCrash("Type foo{\n"
21981 " {\n"
21982 " 1, // A\n"
21983 " 2, // B\n"
21984 " 3, // C\n"
21985 " },\n"
21986 " \"hello\",\n"
21987 "};",
21988 Style);
21989 verifyNoCrash("Type object[X][Y] = {\n"
21990 " {{val}, {val}, {val}},\n"
21991 " {{val}, {val}, // some comment\n"
21992 " {val}}\n"
21993 "};",
21994 Style);
21996 Style.ColumnLimit = 120;
21997 verifyNoCrash(
21998 "T v[] {\n"
21999 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
22000 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
22001 "\"00000000000000000000000000000000000000000000000000000000"
22002 "00000000000000000000000000000000000000000000000000000000\" },\n"
22003 "};",
22004 Style);
22006 Style.SpacesInParens = FormatStyle::SIPO_Custom;
22007 Style.SpacesInParensOptions.Other = true;
22008 verifyFormat("Foo foo[] = {\n"
22009 " {1, 1},\n"
22010 " {1, 1},\n"
22011 "};",
22012 Style);
22015 TEST_F(FormatTest, UnderstandsPragmas) {
22016 verifyFormat("#pragma omp reduction(| : var)");
22017 verifyFormat("#pragma omp reduction(+ : var)");
22019 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22020 "(including parentheses).",
22021 "#pragma mark Any non-hyphenated or hyphenated string "
22022 "(including parentheses).");
22024 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22025 "(including parentheses).",
22026 "#pragma mark Any non-hyphenated or hyphenated string "
22027 "(including parentheses).");
22029 verifyFormat("#pragma comment(linker, \\\n"
22030 " \"argument\" \\\n"
22031 " \"argument\"",
22032 "#pragma comment(linker, \\\n"
22033 " \"argument\" \\\n"
22034 " \"argument\"",
22035 getStyleWithColumns(
22036 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32));
22039 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
22040 verifyFormat("#pragma omp target map(to : var)");
22041 verifyFormat("#pragma omp target map(to : var[ : N])");
22042 verifyFormat("#pragma omp target map(to : var[0 : N])");
22043 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22045 verifyFormat(
22046 "#pragma omp target \\\n"
22047 " reduction(+ : var) \\\n"
22048 " map(to : A[0 : N]) \\\n"
22049 " map(to : B[0 : N]) \\\n"
22050 " map(from : C[0 : N]) \\\n"
22051 " firstprivate(i) \\\n"
22052 " firstprivate(j) \\\n"
22053 " firstprivate(k)",
22054 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22055 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22056 getLLVMStyleWithColumns(26));
22059 TEST_F(FormatTest, UnderstandPragmaOption) {
22060 verifyFormat("#pragma option -C -A");
22062 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
22065 TEST_F(FormatTest, UnderstandPragmaRegion) {
22066 auto Style = getLLVMStyleWithColumns(0);
22067 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
22068 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
22071 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
22072 FormatStyle Style = getLLVMStyleWithColumns(20);
22074 // See PR41213
22075 verifyFormat("/*\n"
22076 " *\t9012345\n"
22077 " * /8901\n"
22078 " */",
22079 "/*\n"
22080 " *\t9012345 /8901\n"
22081 " */",
22082 Style);
22083 verifyFormat("/*\n"
22084 " *345678\n"
22085 " *\t/8901\n"
22086 " */",
22087 "/*\n"
22088 " *345678\t/8901\n"
22089 " */",
22090 Style);
22092 verifyFormat("int a; // the\n"
22093 " // comment",
22094 Style);
22095 verifyNoChange("int a; /* first line\n"
22096 " * second\n"
22097 " * line third\n"
22098 " * line\n"
22099 " */",
22100 Style);
22101 verifyFormat("int a; // first line\n"
22102 " // second\n"
22103 " // line third\n"
22104 " // line",
22105 "int a; // first line\n"
22106 " // second line\n"
22107 " // third line",
22108 Style);
22110 Style.PenaltyExcessCharacter = 90;
22111 verifyFormat("int a; // the comment", Style);
22112 verifyFormat("int a; // the comment\n"
22113 " // aaa",
22114 "int a; // the comment aaa", Style);
22115 verifyNoChange("int a; /* first line\n"
22116 " * second line\n"
22117 " * third line\n"
22118 " */",
22119 Style);
22120 verifyFormat("int a; // first line\n"
22121 " // second line\n"
22122 " // third line",
22123 Style);
22124 // FIXME: Investigate why this is not getting the same layout as the test
22125 // above.
22126 verifyFormat("int a; /* first line\n"
22127 " * second line\n"
22128 " * third line\n"
22129 " */",
22130 "int a; /* first line second line third line"
22131 "\n*/",
22132 Style);
22134 verifyFormat("// foo bar baz bazfoo\n"
22135 "// foo bar foo bar",
22136 "// foo bar baz bazfoo\n"
22137 "// foo bar foo bar",
22138 Style);
22139 verifyFormat("// foo bar baz bazfoo\n"
22140 "// foo bar foo bar",
22141 "// foo bar baz bazfoo\n"
22142 "// foo bar foo bar",
22143 Style);
22145 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22146 // next one.
22147 verifyFormat("// foo bar baz bazfoo\n"
22148 "// bar foo bar",
22149 "// foo bar baz bazfoo bar\n"
22150 "// foo bar",
22151 Style);
22153 // FIXME: unstable test case
22154 EXPECT_EQ("// foo bar baz bazfoo\n"
22155 "// foo bar baz bazfoo\n"
22156 "// bar foo bar",
22157 format("// foo bar baz bazfoo\n"
22158 "// foo bar baz bazfoo bar\n"
22159 "// foo bar",
22160 Style));
22162 // FIXME: unstable test case
22163 EXPECT_EQ("// foo bar baz bazfoo\n"
22164 "// foo bar baz bazfoo\n"
22165 "// bar foo bar",
22166 format("// foo bar baz bazfoo\n"
22167 "// foo bar baz bazfoo bar\n"
22168 "// foo bar",
22169 Style));
22171 // Make sure we do not keep protruding characters if strict mode reflow is
22172 // cheaper than keeping protruding characters.
22173 Style.ColumnLimit = 21;
22174 verifyFormat("// foo foo foo foo\n"
22175 "// foo foo foo foo\n"
22176 "// foo foo foo foo",
22177 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
22179 verifyFormat("int a = /* long block\n"
22180 " comment */\n"
22181 " 42;",
22182 "int a = /* long block comment */ 42;", Style);
22185 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
22186 FormatStyle Style = getLLVMStyle();
22187 Style.ColumnLimit = 8;
22188 Style.PenaltyExcessCharacter = 15;
22189 verifyFormat("int foo(\n"
22190 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22191 Style);
22192 Style.PenaltyBreakOpenParenthesis = 200;
22193 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22194 "int foo(\n"
22195 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22196 Style);
22199 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
22200 FormatStyle Style = getLLVMStyle();
22201 Style.ColumnLimit = 5;
22202 Style.PenaltyExcessCharacter = 150;
22203 verifyFormat("foo((\n"
22204 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22206 Style);
22207 Style.PenaltyBreakOpenParenthesis = 100'000;
22208 verifyFormat("foo((int)\n"
22209 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22210 "foo((\n"
22211 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22212 Style);
22215 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
22216 FormatStyle Style = getLLVMStyle();
22217 Style.ColumnLimit = 4;
22218 Style.PenaltyExcessCharacter = 100;
22219 verifyFormat("for (\n"
22220 " int iiiiiiiiiiiiiiiii =\n"
22221 " 0;\n"
22222 " iiiiiiiiiiiiiiiii <\n"
22223 " 2;\n"
22224 " iiiiiiiiiiiiiiiii++) {\n"
22225 "}",
22227 Style);
22228 Style.PenaltyBreakOpenParenthesis = 1250;
22229 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22230 " 0;\n"
22231 " iiiiiiiiiiiiiiiii <\n"
22232 " 2;\n"
22233 " iiiiiiiiiiiiiiiii++) {\n"
22234 "}",
22235 "for (\n"
22236 " int iiiiiiiiiiiiiiiii =\n"
22237 " 0;\n"
22238 " iiiiiiiiiiiiiiiii <\n"
22239 " 2;\n"
22240 " iiiiiiiiiiiiiiiii++) {\n"
22241 "}",
22242 Style);
22245 TEST_F(FormatTest, BreakPenaltyScopeResolution) {
22246 FormatStyle Style = getLLVMStyle();
22247 Style.ColumnLimit = 20;
22248 Style.PenaltyExcessCharacter = 100;
22249 verifyFormat("unsigned long\n"
22250 "foo::bar();",
22251 Style);
22252 Style.PenaltyBreakScopeResolution = 10;
22253 verifyFormat("unsigned long foo::\n"
22254 " bar();",
22255 Style);
22258 TEST_F(FormatTest, WorksFor8bitEncodings) {
22259 // FIXME: unstable test case
22260 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22261 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22262 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22263 "\"\xef\xee\xf0\xf3...\"",
22264 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22265 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22266 "\xef\xee\xf0\xf3...\"",
22267 getLLVMStyleWithColumns(12)));
22270 TEST_F(FormatTest, HandlesUTF8BOM) {
22271 verifyFormat("\xef\xbb\xbf");
22272 verifyFormat("\xef\xbb\xbf#include <iostream>");
22273 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22275 auto Style = getLLVMStyle();
22276 Style.KeepEmptyLines.AtStartOfFile = false;
22277 verifyFormat("\xef\xbb\xbf#include <iostream>",
22278 "\xef\xbb\xbf\n#include <iostream>", Style);
22281 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22282 #if !defined(_MSC_VER)
22284 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
22285 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22286 getLLVMStyleWithColumns(35));
22287 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22288 getLLVMStyleWithColumns(31));
22289 verifyFormat("// Однажды в студёную зимнюю пору...",
22290 getLLVMStyleWithColumns(36));
22291 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22292 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22293 getLLVMStyleWithColumns(39));
22294 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22295 getLLVMStyleWithColumns(35));
22298 TEST_F(FormatTest, SplitsUTF8Strings) {
22299 // Non-printable characters' width is currently considered to be the length in
22300 // bytes in UTF8. The characters can be displayed in very different manner
22301 // (zero-width, single width with a substitution glyph, expanded to their code
22302 // (e.g. "<8d>"), so there's no single correct way to handle them.
22303 // FIXME: unstable test case
22304 EXPECT_EQ("\"aaaaÄ\"\n"
22305 "\"\xc2\x8d\";",
22306 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22307 // FIXME: unstable test case
22308 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22309 "\"\xc2\x8d\";",
22310 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22311 // FIXME: unstable test case
22312 EXPECT_EQ("\"Однажды, в \"\n"
22313 "\"студёную \"\n"
22314 "\"зимнюю \"\n"
22315 "\"пору,\"",
22316 format("\"Однажды, в студёную зимнюю пору,\"",
22317 getLLVMStyleWithColumns(13)));
22318 // FIXME: unstable test case
22319 EXPECT_EQ(
22320 "\"一 二 三 \"\n"
22321 "\"四 五六 \"\n"
22322 "\"七 八 九 \"\n"
22323 "\"\"",
22324 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22325 // FIXME: unstable test case
22326 EXPECT_EQ("\"\t\"\n"
22327 "\" \t\"\n"
22328 "\"三 四 \"\n"
22329 "\"\t\"\n"
22330 "\" \t\"\n"
22331 "\" \"\n"
22332 "\"八九十\tqq\"",
22333 format("\"\t \t三 四 五\t \t七 八九十\tqq\"",
22334 getLLVMStyleWithColumns(11)));
22336 // UTF8 character in an escape sequence.
22337 // FIXME: unstable test case
22338 EXPECT_EQ("\"aaaaaa\"\n"
22339 "\"\\\xC2\x8D\"",
22340 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22343 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
22344 verifyFormat("const char *sssss =\n"
22345 " \"一二三四五六七八\\\n"
22346 " 九 十\";",
22347 "const char *sssss = \"一二三四五六七八\\\n"
22348 " 九 十\";",
22349 getLLVMStyleWithColumns(30));
22352 TEST_F(FormatTest, SplitsUTF8LineComments) {
22353 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22354 verifyFormat("// Я из лесу\n"
22355 "// вышел; был\n"
22356 "// сильный\n"
22357 "// мороз.",
22358 "// Я из лесу вышел; был сильный мороз.",
22359 getLLVMStyleWithColumns(13));
22360 verifyFormat("// 一二三\n"
22361 "// 四五六七\n"
22362 "// 八 九\n"
22363 "// 十",
22364 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22367 TEST_F(FormatTest, SplitsUTF8BlockComments) {
22368 verifyFormat("/* Гляжу,\n"
22369 " * поднимается\n"
22370 " * медленно в\n"
22371 " * гору\n"
22372 " * Лошадка,\n"
22373 " * везущая\n"
22374 " * хворосту\n"
22375 " * воз. */",
22376 "/* Гляжу, поднимается медленно в гору\n"
22377 " * Лошадка, везущая хворосту воз. */",
22378 getLLVMStyleWithColumns(13));
22379 verifyFormat("/* 一二三\n"
22380 " * 四五六七\n"
22381 " * 八 九\n"
22382 " * 十 */",
22383 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22384 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22385 " * 𝕓𝕪𝕥𝕖\n"
22386 " * 𝖀𝕿𝕱-𝟠 */",
22387 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22390 #endif // _MSC_VER
22392 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22393 FormatStyle Style = getLLVMStyle();
22395 Style.ConstructorInitializerIndentWidth = 4;
22396 verifyFormat(
22397 "SomeClass::Constructor()\n"
22398 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22399 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22400 Style);
22402 Style.ConstructorInitializerIndentWidth = 2;
22403 verifyFormat(
22404 "SomeClass::Constructor()\n"
22405 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22406 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22407 Style);
22409 Style.ConstructorInitializerIndentWidth = 0;
22410 verifyFormat(
22411 "SomeClass::Constructor()\n"
22412 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22413 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22414 Style);
22415 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
22416 verifyFormat(
22417 "SomeLongTemplateVariableName<\n"
22418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22419 Style);
22420 verifyFormat("bool smaller = 1 < "
22421 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22423 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22424 Style);
22426 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22427 verifyFormat("SomeClass::Constructor() :\n"
22428 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22429 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22430 Style);
22433 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
22434 FormatStyle Style = getLLVMStyle();
22435 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22436 Style.ConstructorInitializerIndentWidth = 4;
22437 verifyFormat("SomeClass::Constructor()\n"
22438 " : a(a)\n"
22439 " , b(b)\n"
22440 " , c(c) {}",
22441 Style);
22442 verifyFormat("SomeClass::Constructor()\n"
22443 " : a(a) {}",
22444 Style);
22446 Style.ColumnLimit = 0;
22447 verifyFormat("SomeClass::Constructor()\n"
22448 " : a(a) {}",
22449 Style);
22450 verifyFormat("SomeClass::Constructor() noexcept\n"
22451 " : a(a) {}",
22452 Style);
22453 verifyFormat("SomeClass::Constructor()\n"
22454 " : a(a)\n"
22455 " , b(b)\n"
22456 " , c(c) {}",
22457 Style);
22458 verifyFormat("SomeClass::Constructor()\n"
22459 " : a(a) {\n"
22460 " foo();\n"
22461 " bar();\n"
22462 "}",
22463 Style);
22465 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22466 verifyFormat("SomeClass::Constructor()\n"
22467 " : a(a)\n"
22468 " , b(b)\n"
22469 " , c(c) {\n}",
22470 Style);
22471 verifyFormat("SomeClass::Constructor()\n"
22472 " : a(a) {\n}",
22473 Style);
22475 Style.ColumnLimit = 80;
22476 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
22477 Style.ConstructorInitializerIndentWidth = 2;
22478 verifyFormat("SomeClass::Constructor()\n"
22479 " : a(a)\n"
22480 " , b(b)\n"
22481 " , c(c) {}",
22482 Style);
22484 Style.ConstructorInitializerIndentWidth = 0;
22485 verifyFormat("SomeClass::Constructor()\n"
22486 ": a(a)\n"
22487 ", b(b)\n"
22488 ", c(c) {}",
22489 Style);
22491 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22492 Style.ConstructorInitializerIndentWidth = 4;
22493 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
22494 verifyFormat(
22495 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22496 Style);
22497 verifyFormat(
22498 "SomeClass::Constructor()\n"
22499 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22500 Style);
22501 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22502 verifyFormat("SomeClass::Constructor()\n"
22503 " : aaaaaaaa(aaaaaaaa) {}",
22504 Style);
22505 verifyFormat("SomeClass::Constructor()\n"
22506 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22507 Style);
22508 verifyFormat(
22509 "SomeClass::Constructor()\n"
22510 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22511 Style);
22513 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22514 Style.ConstructorInitializerIndentWidth = 4;
22515 Style.ColumnLimit = 60;
22516 verifyFormat("SomeClass::Constructor()\n"
22517 " : aaaaaaaa(aaaaaaaa)\n"
22518 " , aaaaaaaa(aaaaaaaa)\n"
22519 " , aaaaaaaa(aaaaaaaa) {}",
22520 Style);
22521 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22522 verifyFormat("SomeClass::Constructor()\n"
22523 " : aaaaaaaa(aaaaaaaa)\n"
22524 " , aaaaaaaa(aaaaaaaa)\n"
22525 " , aaaaaaaa(aaaaaaaa) {}",
22526 Style);
22529 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
22530 FormatStyle Style = getLLVMStyle();
22531 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22532 Style.ConstructorInitializerIndentWidth = 4;
22533 verifyFormat("SomeClass::Constructor()\n"
22534 " : a{a}\n"
22535 " , b{b} {}",
22536 Style);
22537 verifyFormat("SomeClass::Constructor()\n"
22538 " : a{a}\n"
22539 "#if CONDITION\n"
22540 " , b{b}\n"
22541 "#endif\n"
22542 "{\n}",
22543 Style);
22544 Style.ConstructorInitializerIndentWidth = 2;
22545 verifyFormat("SomeClass::Constructor()\n"
22546 "#if CONDITION\n"
22547 " : a{a}\n"
22548 "#endif\n"
22549 " , b{b}\n"
22550 " , c{c} {\n}",
22551 Style);
22552 Style.ConstructorInitializerIndentWidth = 0;
22553 verifyFormat("SomeClass::Constructor()\n"
22554 ": a{a}\n"
22555 "#ifdef CONDITION\n"
22556 ", b{b}\n"
22557 "#else\n"
22558 ", c{c}\n"
22559 "#endif\n"
22560 ", d{d} {\n}",
22561 Style);
22562 Style.ConstructorInitializerIndentWidth = 4;
22563 verifyFormat("SomeClass::Constructor()\n"
22564 " : a{a}\n"
22565 "#if WINDOWS\n"
22566 "#if DEBUG\n"
22567 " , b{0}\n"
22568 "#else\n"
22569 " , b{1}\n"
22570 "#endif\n"
22571 "#else\n"
22572 "#if DEBUG\n"
22573 " , b{2}\n"
22574 "#else\n"
22575 " , b{3}\n"
22576 "#endif\n"
22577 "#endif\n"
22578 "{\n}",
22579 Style);
22580 verifyFormat("SomeClass::Constructor()\n"
22581 " : a{a}\n"
22582 "#if WINDOWS\n"
22583 " , b{0}\n"
22584 "#if DEBUG\n"
22585 " , c{0}\n"
22586 "#else\n"
22587 " , c{1}\n"
22588 "#endif\n"
22589 "#else\n"
22590 "#if DEBUG\n"
22591 " , c{2}\n"
22592 "#else\n"
22593 " , c{3}\n"
22594 "#endif\n"
22595 " , b{1}\n"
22596 "#endif\n"
22597 "{\n}",
22598 Style);
22601 TEST_F(FormatTest, Destructors) {
22602 verifyFormat("void F(int &i) { i.~int(); }");
22603 verifyFormat("void F(int &i) { i->~int(); }");
22606 TEST_F(FormatTest, FormatsWithWebKitStyle) {
22607 FormatStyle Style = getWebKitStyle();
22609 // Don't indent in outer namespaces.
22610 verifyFormat("namespace outer {\n"
22611 "int i;\n"
22612 "namespace inner {\n"
22613 " int i;\n"
22614 "} // namespace inner\n"
22615 "} // namespace outer\n"
22616 "namespace other_outer {\n"
22617 "int i;\n"
22618 "}",
22619 Style);
22621 // Don't indent case labels.
22622 verifyFormat("switch (variable) {\n"
22623 "case 1:\n"
22624 "case 2:\n"
22625 " doSomething();\n"
22626 " break;\n"
22627 "default:\n"
22628 " ++variable;\n"
22629 "}",
22630 Style);
22632 // Wrap before binary operators.
22633 verifyFormat(
22634 "void f()\n"
22635 "{\n"
22636 " if (aaaaaaaaaaaaaaaa\n"
22637 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22638 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22639 " return;\n"
22640 "}",
22641 "void f() {\n"
22642 "if (aaaaaaaaaaaaaaaa\n"
22643 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22644 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22645 "return;\n"
22646 "}",
22647 Style);
22649 // Allow functions on a single line.
22650 verifyFormat("void f() { return; }", Style);
22652 // Allow empty blocks on a single line and insert a space in empty blocks.
22653 verifyFormat("void f() { }", "void f() {}", Style);
22654 verifyFormat("while (true) { }", "while (true) {}", Style);
22655 // However, don't merge non-empty short loops.
22656 verifyFormat("while (true) {\n"
22657 " continue;\n"
22658 "}",
22659 "while (true) { continue; }", Style);
22661 // Constructor initializers are formatted one per line with the "," on the
22662 // new line.
22663 verifyFormat("Constructor()\n"
22664 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22665 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22666 " aaaaaaaaaaaaaa)\n"
22667 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22668 "{\n"
22669 "}",
22670 Style);
22671 verifyFormat("SomeClass::Constructor()\n"
22672 " : a(a)\n"
22673 "{\n"
22674 "}",
22675 Style);
22676 verifyFormat("SomeClass::Constructor()\n"
22677 " : a(a)\n"
22678 "{\n"
22679 "}",
22680 "SomeClass::Constructor():a(a){}", Style);
22681 verifyFormat("SomeClass::Constructor()\n"
22682 " : a(a)\n"
22683 " , b(b)\n"
22684 " , c(c)\n"
22685 "{\n"
22686 "}",
22687 Style);
22688 verifyFormat("SomeClass::Constructor()\n"
22689 " : a(a)\n"
22690 "{\n"
22691 " foo();\n"
22692 " bar();\n"
22693 "}",
22694 Style);
22696 // Access specifiers should be aligned left.
22697 verifyFormat("class C {\n"
22698 "public:\n"
22699 " int i;\n"
22700 "};",
22701 Style);
22703 // Do not align comments.
22704 verifyFormat("int a; // Do not\n"
22705 "double b; // align comments.",
22706 Style);
22708 // Do not align operands.
22709 verifyFormat("ASSERT(aaaa\n"
22710 " || bbbb);",
22711 "ASSERT ( aaaa\n||bbbb);", Style);
22713 // Accept input's line breaks.
22714 verifyFormat("if (aaaaaaaaaaaaaaa\n"
22715 " || bbbbbbbbbbbbbbb) {\n"
22716 " i++;\n"
22717 "}",
22718 "if (aaaaaaaaaaaaaaa\n"
22719 "|| bbbbbbbbbbbbbbb) { i++; }",
22720 Style);
22721 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22722 " i++;\n"
22723 "}",
22724 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
22726 // Don't automatically break all macro definitions (llvm.org/PR17842).
22727 verifyFormat("#define aNumber 10", Style);
22728 // However, generally keep the line breaks that the user authored.
22729 verifyFormat("#define aNumber \\\n"
22730 " 10",
22731 "#define aNumber \\\n"
22732 " 10",
22733 Style);
22735 // Keep empty and one-element array literals on a single line.
22736 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22737 " copyItems:YES];",
22738 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22739 "copyItems:YES];",
22740 Style);
22741 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22742 " copyItems:YES];",
22743 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22744 " copyItems:YES];",
22745 Style);
22746 // FIXME: This does not seem right, there should be more indentation before
22747 // the array literal's entries. Nested blocks have the same problem.
22748 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22749 " @\"a\",\n"
22750 " @\"a\"\n"
22751 "]\n"
22752 " copyItems:YES];",
22753 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22754 " @\"a\",\n"
22755 " @\"a\"\n"
22756 " ]\n"
22757 " copyItems:YES];",
22758 Style);
22759 verifyFormat(
22760 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22761 " copyItems:YES];",
22762 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22763 " copyItems:YES];",
22764 Style);
22766 verifyFormat("[self.a b:c c:d];", Style);
22767 verifyFormat("[self.a b:c\n"
22768 " c:d];",
22769 "[self.a b:c\n"
22770 "c:d];",
22771 Style);
22774 TEST_F(FormatTest, FormatsLambdas) {
22775 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22776 verifyFormat(
22777 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22778 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22779 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22780 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22781 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22782 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22783 verifyFormat("auto c = [a = [b = 42] {}] {};");
22784 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22785 verifyFormat("int x = f(*+[] {});");
22786 verifyFormat("void f() {\n"
22787 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22788 "}");
22789 verifyFormat("void f() {\n"
22790 " other(x.begin(), //\n"
22791 " x.end(), //\n"
22792 " [&](int, int) { return 1; });\n"
22793 "}");
22794 verifyFormat("void f() {\n"
22795 " other.other.other.other.other(\n"
22796 " x.begin(), x.end(),\n"
22797 " [something, rather](int, int, int, int, int, int, int) { "
22798 "return 1; });\n"
22799 "}");
22800 verifyFormat(
22801 "void f() {\n"
22802 " other.other.other.other.other(\n"
22803 " x.begin(), x.end(),\n"
22804 " [something, rather](int, int, int, int, int, int, int) {\n"
22805 " //\n"
22806 " });\n"
22807 "}");
22808 verifyFormat("SomeFunction([]() { // A cool function...\n"
22809 " return 43;\n"
22810 "});");
22811 verifyFormat("SomeFunction([]() {\n"
22812 "#define A a\n"
22813 " return 43;\n"
22814 "});",
22815 "SomeFunction([](){\n"
22816 "#define A a\n"
22817 "return 43;\n"
22818 "});");
22819 verifyFormat("void f() {\n"
22820 " SomeFunction([](decltype(x), A *a) {});\n"
22821 " SomeFunction([](typeof(x), A *a) {});\n"
22822 " SomeFunction([](_Atomic(x), A *a) {});\n"
22823 " SomeFunction([](__underlying_type(x), A *a) {});\n"
22824 "}");
22825 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22826 " [](const aaaaaaaaaa &a) { return a; });");
22827 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22828 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
22829 "});");
22830 verifyFormat("Constructor()\n"
22831 " : Field([] { // comment\n"
22832 " int i;\n"
22833 " }) {}");
22834 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22835 " return some_parameter.size();\n"
22836 "};");
22837 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22838 " [](const string &s) { return s; };");
22839 verifyFormat("int i = aaaaaa ? 1 //\n"
22840 " : [] {\n"
22841 " return 2; //\n"
22842 " }();");
22843 verifyFormat("llvm::errs() << \"number of twos is \"\n"
22844 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
22845 " return x == 2; // force break\n"
22846 " });");
22847 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22848 " [=](int iiiiiiiiiiii) {\n"
22849 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22850 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
22851 " });",
22852 getLLVMStyleWithColumns(60));
22854 verifyFormat("SomeFunction({[&] {\n"
22855 " // comment\n"
22856 " },\n"
22857 " [&] {\n"
22858 " // comment\n"
22859 " }});");
22860 verifyFormat("SomeFunction({[&] {\n"
22861 " // comment\n"
22862 "}});");
22863 verifyFormat(
22864 "virtual aaaaaaaaaaaaaaaa(\n"
22865 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22866 " aaaaa aaaaaaaaa);");
22868 // Lambdas with return types.
22869 verifyFormat("int c = []() -> int { return 2; }();");
22870 verifyFormat("int c = []() -> int * { return 2; }();");
22871 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22872 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22873 verifyFormat("foo([]() noexcept -> int {});");
22874 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22875 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22876 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22877 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22878 verifyFormat("[a, a]() -> a<1> {};");
22879 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22880 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22881 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22882 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22883 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22884 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22885 verifyFormat("[]() -> foo<!5> { return {}; };");
22886 verifyFormat("[]() -> foo<~5> { return {}; };");
22887 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22888 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22889 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22890 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22891 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22892 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22893 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22894 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22895 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22896 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22897 verifyFormat("namespace bar {\n"
22898 "// broken:\n"
22899 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22900 "} // namespace bar");
22901 verifyFormat("namespace bar {\n"
22902 "// broken:\n"
22903 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22904 "} // namespace bar");
22905 verifyFormat("namespace bar {\n"
22906 "// broken:\n"
22907 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22908 "} // namespace bar");
22909 verifyFormat("namespace bar {\n"
22910 "// broken:\n"
22911 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22912 "} // namespace bar");
22913 verifyFormat("namespace bar {\n"
22914 "// broken:\n"
22915 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22916 "} // namespace bar");
22917 verifyFormat("namespace bar {\n"
22918 "// broken:\n"
22919 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22920 "} // namespace bar");
22921 verifyFormat("namespace bar {\n"
22922 "// broken:\n"
22923 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22924 "} // namespace bar");
22925 verifyFormat("namespace bar {\n"
22926 "// broken:\n"
22927 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22928 "} // namespace bar");
22929 verifyFormat("namespace bar {\n"
22930 "// broken:\n"
22931 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22932 "} // namespace bar");
22933 verifyFormat("namespace bar {\n"
22934 "// broken:\n"
22935 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22936 "} // namespace bar");
22937 verifyFormat("namespace bar {\n"
22938 "// broken:\n"
22939 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22940 "} // namespace bar");
22941 verifyFormat("namespace bar {\n"
22942 "// broken:\n"
22943 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22944 "} // namespace bar");
22945 verifyFormat("namespace bar {\n"
22946 "// broken:\n"
22947 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22948 "} // namespace bar");
22949 verifyFormat("namespace bar {\n"
22950 "// broken:\n"
22951 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22952 "} // namespace bar");
22953 verifyFormat("namespace bar {\n"
22954 "// broken:\n"
22955 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22956 "} // namespace bar");
22957 verifyFormat("namespace bar {\n"
22958 "// broken:\n"
22959 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22960 "} // namespace bar");
22961 verifyFormat("namespace bar {\n"
22962 "// broken:\n"
22963 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22964 "} // namespace bar");
22965 verifyFormat("namespace bar {\n"
22966 "// broken:\n"
22967 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22968 "} // namespace bar");
22969 verifyFormat("[]() -> a<1> {};");
22970 verifyFormat("[]() -> a<1> { ; };");
22971 verifyFormat("[]() -> a<1> { ; }();");
22972 verifyFormat("[a, a]() -> a<true> {};");
22973 verifyFormat("[]() -> a<true> {};");
22974 verifyFormat("[]() -> a<true> { ; };");
22975 verifyFormat("[]() -> a<true> { ; }();");
22976 verifyFormat("[a, a]() -> a<false> {};");
22977 verifyFormat("[]() -> a<false> {};");
22978 verifyFormat("[]() -> a<false> { ; };");
22979 verifyFormat("[]() -> a<false> { ; }();");
22980 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22981 verifyFormat("namespace bar {\n"
22982 "auto foo{[]() -> foo<false> { ; }};\n"
22983 "} // namespace bar");
22984 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22985 " int j) -> int {\n"
22986 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22987 "};");
22988 verifyFormat(
22989 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22990 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22991 " return aaaaaaaaaaaaaaaaa;\n"
22992 " });",
22993 getLLVMStyleWithColumns(70));
22994 verifyFormat("[]() //\n"
22995 " -> int {\n"
22996 " return 1; //\n"
22997 "};");
22998 verifyFormat("[]() -> Void<T...> {};");
22999 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
23000 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
23001 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
23002 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
23003 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
23004 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
23005 verifyFormat("return int{[x = x]() { return x; }()};");
23007 // Lambdas with explicit template argument lists.
23008 verifyFormat(
23009 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
23010 verifyFormat("auto L = []<class T>(T) {\n"
23011 " {\n"
23012 " f();\n"
23013 " g();\n"
23014 " }\n"
23015 "};");
23016 verifyFormat("auto L = []<class... T>(T...) {\n"
23017 " {\n"
23018 " f();\n"
23019 " g();\n"
23020 " }\n"
23021 "};");
23022 verifyFormat("auto L = []<typename... T>(T...) {\n"
23023 " {\n"
23024 " f();\n"
23025 " g();\n"
23026 " }\n"
23027 "};");
23028 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23029 " {\n"
23030 " f();\n"
23031 " g();\n"
23032 " }\n"
23033 "};");
23034 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23035 " {\n"
23036 " f();\n"
23037 " g();\n"
23038 " }\n"
23039 "};");
23040 verifyFormat("auto L = []<int... T>(T...) {\n"
23041 " {\n"
23042 " f();\n"
23043 " g();\n"
23044 " }\n"
23045 "};");
23046 verifyFormat("auto L = []<Foo... T>(T...) {\n"
23047 " {\n"
23048 " f();\n"
23049 " g();\n"
23050 " }\n"
23051 "};");
23053 // Lambdas that fit on a single line within an argument list are not forced
23054 // onto new lines.
23055 verifyFormat("SomeFunction([] {});");
23056 verifyFormat("SomeFunction(0, [] {});");
23057 verifyFormat("SomeFunction([] {}, 0);");
23058 verifyFormat("SomeFunction(0, [] {}, 0);");
23059 verifyFormat("SomeFunction([] { return 0; }, 0);");
23060 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23061 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23062 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23063 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23064 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
23065 // Exceeded column limit. We need to break.
23066 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23067 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
23068 "return 0; }, b);");
23070 // Multiple multi-line lambdas in the same parentheses change indentation
23071 // rules. These lambdas are always forced to start on new lines.
23072 verifyFormat("SomeFunction(\n"
23073 " []() {\n"
23074 " //\n"
23075 " },\n"
23076 " []() {\n"
23077 " //\n"
23078 " });");
23080 // A multi-line lambda passed as arg0 is always pushed to the next line.
23081 verifyFormat("SomeFunction(\n"
23082 " [this] {\n"
23083 " //\n"
23084 " },\n"
23085 " 1);");
23087 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23088 // the arg0 case above.
23089 auto Style = getGoogleStyle();
23090 Style.BinPackArguments = false;
23091 verifyFormat("SomeFunction(\n"
23092 " a,\n"
23093 " [this] {\n"
23094 " //\n"
23095 " },\n"
23096 " b);",
23097 Style);
23098 verifyFormat("SomeFunction(\n"
23099 " a,\n"
23100 " [this] {\n"
23101 " //\n"
23102 " },\n"
23103 " b);");
23105 // A lambda with a very long line forces arg0 to be pushed out irrespective of
23106 // the BinPackArguments value (as long as the code is wide enough).
23107 verifyFormat(
23108 "something->SomeFunction(\n"
23109 " a,\n"
23110 " [this] {\n"
23112 "D0000000000000000000000000000000000000000000000000000000000001();\n"
23113 " },\n"
23114 " b);");
23116 // A multi-line lambda is pulled up as long as the introducer fits on the
23117 // previous line and there are no further args.
23118 verifyFormat("function(1, [this, that] {\n"
23119 " //\n"
23120 "});");
23121 verifyFormat("function([this, that] {\n"
23122 " //\n"
23123 "});");
23124 // FIXME: this format is not ideal and we should consider forcing the first
23125 // arg onto its own line.
23126 verifyFormat("function(a, b, c, //\n"
23127 " d, [this, that] {\n"
23128 " //\n"
23129 " });");
23131 // Multiple lambdas are treated correctly even when there is a short arg0.
23132 verifyFormat("SomeFunction(\n"
23133 " 1,\n"
23134 " [this] {\n"
23135 " //\n"
23136 " },\n"
23137 " [this] {\n"
23138 " //\n"
23139 " },\n"
23140 " 1);");
23142 // More complex introducers.
23143 verifyFormat("return [i, args...] {};");
23145 // Not lambdas.
23146 verifyFormat("constexpr char hello[]{\"hello\"};");
23147 verifyFormat("double &operator[](int i) { return 0; }\n"
23148 "int i;");
23149 verifyFormat("std::unique_ptr<int[]> foo() {}");
23150 verifyFormat("int i = a[a][a]->f();");
23151 verifyFormat("int i = (*b)[a]->f();");
23153 // Other corner cases.
23154 verifyFormat("void f() {\n"
23155 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23156 " );\n"
23157 "}");
23158 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23160 // Lambdas created through weird macros.
23161 verifyFormat("void f() {\n"
23162 " MACRO((const AA &a) { return 1; });\n"
23163 " MACRO((AA &a) { return 1; });\n"
23164 "}");
23166 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23167 " doo_dah();\n"
23168 " doo_dah();\n"
23169 " })) {\n"
23170 "}");
23171 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23172 " doo_dah();\n"
23173 " doo_dah();\n"
23174 " })) {\n"
23175 "}");
23176 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23177 " doo_dah();\n"
23178 " doo_dah();\n"
23179 " })) {\n"
23180 "}");
23181 verifyFormat("auto lambda = []() {\n"
23182 " int a = 2\n"
23183 "#if A\n"
23184 " + 2\n"
23185 "#endif\n"
23186 " ;\n"
23187 "};");
23189 // Lambdas with complex multiline introducers.
23190 verifyFormat(
23191 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23192 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23193 " -> ::std::unordered_set<\n"
23194 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23195 " //\n"
23196 " });");
23198 FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
23199 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23200 " [](auto n) noexcept [[back_attr]]\n"
23201 " -> std::unordered_map<very_long_type_name_A,\n"
23202 " very_long_type_name_B> {\n"
23203 " really_do_something();\n"
23204 " });",
23205 LLVMStyle);
23206 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23207 " [](auto n) constexpr\n"
23208 " -> std::unordered_map<very_long_type_name_A,\n"
23209 " very_long_type_name_B> {\n"
23210 " really_do_something();\n"
23211 " });",
23212 LLVMStyle);
23214 FormatStyle DoNotMerge = getLLVMStyle();
23215 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
23216 verifyFormat("auto c = []() {\n"
23217 " return b;\n"
23218 "};",
23219 "auto c = []() { return b; };", DoNotMerge);
23220 verifyFormat("auto c = []() {\n"
23221 "};",
23222 " auto c = []() {};", DoNotMerge);
23224 FormatStyle MergeEmptyOnly = getLLVMStyle();
23225 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
23226 verifyFormat("auto c = []() {\n"
23227 " return b;\n"
23228 "};",
23229 "auto c = []() {\n"
23230 " return b;\n"
23231 " };",
23232 MergeEmptyOnly);
23233 verifyFormat("auto c = []() {};",
23234 "auto c = []() {\n"
23235 "};",
23236 MergeEmptyOnly);
23238 FormatStyle MergeInline = getLLVMStyle();
23239 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
23240 verifyFormat("auto c = []() {\n"
23241 " return b;\n"
23242 "};",
23243 "auto c = []() { return b; };", MergeInline);
23244 verifyFormat("function([]() { return b; })", MergeInline);
23245 verifyFormat("function([]() { return b; }, a)", MergeInline);
23246 verifyFormat("function(a, []() { return b; })", MergeInline);
23248 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23249 // AllowShortLambdasOnASingleLine
23250 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23251 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23252 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23253 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23254 FormatStyle::ShortLambdaStyle::SLS_None;
23255 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23256 " []()\n"
23257 " {\n"
23258 " return 17;\n"
23259 " });",
23260 LLVMWithBeforeLambdaBody);
23261 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23262 " []()\n"
23263 " {\n"
23264 " });",
23265 LLVMWithBeforeLambdaBody);
23266 verifyFormat("auto fct_SLS_None = []()\n"
23267 "{\n"
23268 " return 17;\n"
23269 "};",
23270 LLVMWithBeforeLambdaBody);
23271 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23272 " []()\n"
23273 " {\n"
23274 " return Call(\n"
23275 " []()\n"
23276 " {\n"
23277 " return 17;\n"
23278 " });\n"
23279 " });",
23280 LLVMWithBeforeLambdaBody);
23281 verifyFormat("void Fct() {\n"
23282 " return {[]()\n"
23283 " {\n"
23284 " return 17;\n"
23285 " }};\n"
23286 "}",
23287 LLVMWithBeforeLambdaBody);
23289 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23290 FormatStyle::ShortLambdaStyle::SLS_Empty;
23291 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23292 " []()\n"
23293 " {\n"
23294 " return 17;\n"
23295 " });",
23296 LLVMWithBeforeLambdaBody);
23297 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23298 LLVMWithBeforeLambdaBody);
23299 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23300 "ongFunctionName_SLS_Empty(\n"
23301 " []() {});",
23302 LLVMWithBeforeLambdaBody);
23303 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23304 " []()\n"
23305 " {\n"
23306 " return 17;\n"
23307 " });",
23308 LLVMWithBeforeLambdaBody);
23309 verifyFormat("auto fct_SLS_Empty = []()\n"
23310 "{\n"
23311 " return 17;\n"
23312 "};",
23313 LLVMWithBeforeLambdaBody);
23314 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23315 " []()\n"
23316 " {\n"
23317 " return Call([]() {});\n"
23318 " });",
23319 LLVMWithBeforeLambdaBody);
23320 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23321 " []()\n"
23322 " {\n"
23323 " return Call([]() {});\n"
23324 " });",
23325 LLVMWithBeforeLambdaBody);
23326 verifyFormat(
23327 "FctWithLongLineInLambda_SLS_Empty(\n"
23328 " []()\n"
23329 " {\n"
23330 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23331 " AndShouldNotBeConsiderAsInline,\n"
23332 " LambdaBodyMustBeBreak);\n"
23333 " });",
23334 LLVMWithBeforeLambdaBody);
23336 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23337 FormatStyle::ShortLambdaStyle::SLS_Inline;
23338 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23339 LLVMWithBeforeLambdaBody);
23340 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23341 LLVMWithBeforeLambdaBody);
23342 verifyFormat("auto fct_SLS_Inline = []()\n"
23343 "{\n"
23344 " return 17;\n"
23345 "};",
23346 LLVMWithBeforeLambdaBody);
23347 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23348 "17; }); });",
23349 LLVMWithBeforeLambdaBody);
23350 verifyFormat(
23351 "FctWithLongLineInLambda_SLS_Inline(\n"
23352 " []()\n"
23353 " {\n"
23354 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23355 " AndShouldNotBeConsiderAsInline,\n"
23356 " LambdaBodyMustBeBreak);\n"
23357 " });",
23358 LLVMWithBeforeLambdaBody);
23359 verifyFormat("FctWithMultipleParams_SLS_Inline("
23360 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23361 " []() { return 17; });",
23362 LLVMWithBeforeLambdaBody);
23363 verifyFormat(
23364 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23365 LLVMWithBeforeLambdaBody);
23367 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23368 FormatStyle::ShortLambdaStyle::SLS_All;
23369 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23370 LLVMWithBeforeLambdaBody);
23371 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23372 LLVMWithBeforeLambdaBody);
23373 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23374 LLVMWithBeforeLambdaBody);
23375 verifyFormat("FctWithOneParam_SLS_All(\n"
23376 " []()\n"
23377 " {\n"
23378 " // A cool function...\n"
23379 " return 43;\n"
23380 " });",
23381 LLVMWithBeforeLambdaBody);
23382 verifyFormat("FctWithMultipleParams_SLS_All("
23383 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23384 " []() { return 17; });",
23385 LLVMWithBeforeLambdaBody);
23386 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23387 LLVMWithBeforeLambdaBody);
23388 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23389 LLVMWithBeforeLambdaBody);
23390 verifyFormat(
23391 "FctWithLongLineInLambda_SLS_All(\n"
23392 " []()\n"
23393 " {\n"
23394 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23395 " AndShouldNotBeConsiderAsInline,\n"
23396 " LambdaBodyMustBeBreak);\n"
23397 " });",
23398 LLVMWithBeforeLambdaBody);
23399 verifyFormat(
23400 "auto fct_SLS_All = []()\n"
23401 "{\n"
23402 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23403 " AndShouldNotBeConsiderAsInline,\n"
23404 " LambdaBodyMustBeBreak);\n"
23405 "};",
23406 LLVMWithBeforeLambdaBody);
23407 LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine;
23408 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23409 LLVMWithBeforeLambdaBody);
23410 verifyFormat(
23411 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23412 " FirstParam,\n"
23413 " SecondParam,\n"
23414 " ThirdParam,\n"
23415 " FourthParam);",
23416 LLVMWithBeforeLambdaBody);
23417 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23418 " []() { return "
23419 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23420 " FirstParam,\n"
23421 " SecondParam,\n"
23422 " ThirdParam,\n"
23423 " FourthParam);",
23424 LLVMWithBeforeLambdaBody);
23425 verifyFormat(
23426 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23427 " SecondParam,\n"
23428 " ThirdParam,\n"
23429 " FourthParam,\n"
23430 " []() { return SomeValueNotSoLong; });",
23431 LLVMWithBeforeLambdaBody);
23432 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23433 " []()\n"
23434 " {\n"
23435 " return "
23436 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23437 "eConsiderAsInline;\n"
23438 " });",
23439 LLVMWithBeforeLambdaBody);
23440 verifyFormat(
23441 "FctWithLongLineInLambda_SLS_All(\n"
23442 " []()\n"
23443 " {\n"
23444 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23445 " AndShouldNotBeConsiderAsInline,\n"
23446 " LambdaBodyMustBeBreak);\n"
23447 " });",
23448 LLVMWithBeforeLambdaBody);
23449 verifyFormat("FctWithTwoParams_SLS_All(\n"
23450 " []()\n"
23451 " {\n"
23452 " // A cool function...\n"
23453 " return 43;\n"
23454 " },\n"
23455 " 87);",
23456 LLVMWithBeforeLambdaBody);
23457 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23458 LLVMWithBeforeLambdaBody);
23459 verifyFormat(
23460 "FctWithTwoParams_SLS_All(\n"
23461 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23462 LLVMWithBeforeLambdaBody);
23463 verifyFormat(
23464 "FctWithTwoParams_SLS_All(\n"
23465 " 87,\n"
23466 " []()\n"
23467 " {\n"
23468 " return "
23469 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23470 " });",
23471 LLVMWithBeforeLambdaBody);
23472 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23473 LLVMWithBeforeLambdaBody);
23474 verifyFormat(
23475 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23476 LLVMWithBeforeLambdaBody);
23477 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23478 "}); }, x);",
23479 LLVMWithBeforeLambdaBody);
23480 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23481 " []()\n"
23482 " {\n"
23483 " // A cool function...\n"
23484 " return Call([]() { return 17; });\n"
23485 " });",
23486 LLVMWithBeforeLambdaBody);
23487 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23488 " []()\n"
23489 " {\n"
23490 " return Call(\n"
23491 " []()\n"
23492 " {\n"
23493 " // A cool function...\n"
23494 " return 17;\n"
23495 " });\n"
23496 " });",
23497 LLVMWithBeforeLambdaBody);
23499 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23500 FormatStyle::ShortLambdaStyle::SLS_None;
23502 verifyFormat("auto select = [this]() -> const Library::Object *\n"
23503 "{\n"
23504 " return MyAssignment::SelectFromList(this);\n"
23505 "};",
23506 LLVMWithBeforeLambdaBody);
23508 verifyFormat("auto select = [this]() -> const Library::Object &\n"
23509 "{\n"
23510 " return MyAssignment::SelectFromList(this);\n"
23511 "};",
23512 LLVMWithBeforeLambdaBody);
23514 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23515 "{\n"
23516 " return MyAssignment::SelectFromList(this);\n"
23517 "};",
23518 LLVMWithBeforeLambdaBody);
23520 verifyFormat("namespace test {\n"
23521 "class Test {\n"
23522 "public:\n"
23523 " Test() = default;\n"
23524 "};\n"
23525 "} // namespace test",
23526 LLVMWithBeforeLambdaBody);
23528 // Lambdas with different indentation styles.
23529 Style = getLLVMStyleWithColumns(60);
23530 verifyFormat("Result doSomething(Promise promise) {\n"
23531 " return promise.then(\n"
23532 " [this, obj = std::move(s)](int bar) mutable {\n"
23533 " return someObject.startAsyncAction().then(\n"
23534 " [this, &obj](Result result) mutable {\n"
23535 " result.processMore();\n"
23536 " });\n"
23537 " });\n"
23538 "}",
23539 Style);
23540 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
23541 verifyFormat("Result doSomething(Promise promise) {\n"
23542 " return promise.then(\n"
23543 " [this, obj = std::move(s)](int bar) mutable {\n"
23544 " return obj.startAsyncAction().then(\n"
23545 " [this, &obj](Result result) mutable {\n"
23546 " result.processMore();\n"
23547 " });\n"
23548 " });\n"
23549 "}",
23550 Style);
23551 verifyFormat("Result doSomething(Promise promise) {\n"
23552 " return promise.then([this, obj = std::move(s)] {\n"
23553 " return obj.startAsyncAction().then(\n"
23554 " [this, &obj](Result result) mutable {\n"
23555 " result.processMore();\n"
23556 " });\n"
23557 " });\n"
23558 "}",
23559 Style);
23560 verifyFormat("void test() {\n"
23561 " ([]() -> auto {\n"
23562 " int b = 32;\n"
23563 " return 3;\n"
23564 " }).foo();\n"
23565 "}",
23566 Style);
23567 verifyFormat("void test() {\n"
23568 " []() -> auto {\n"
23569 " int b = 32;\n"
23570 " return 3;\n"
23571 " }\n"
23572 "}",
23573 Style);
23574 verifyFormat("void test() {\n"
23575 " std::sort(v.begin(), v.end(),\n"
23576 " [](const auto &foo, const auto &bar) {\n"
23577 " return foo.baz < bar.baz;\n"
23578 " });\n"
23579 "};",
23580 Style);
23581 verifyFormat("void test() {\n"
23582 " (\n"
23583 " []() -> auto {\n"
23584 " int b = 32;\n"
23585 " return 3;\n"
23586 " }, foo, bar)\n"
23587 " .foo();\n"
23588 "}",
23589 Style);
23590 verifyFormat("void test() {\n"
23591 " ([]() -> auto {\n"
23592 " int b = 32;\n"
23593 " return 3;\n"
23594 " })\n"
23595 " .foo()\n"
23596 " .bar();\n"
23597 "}",
23598 Style);
23599 verifyFormat("#define A \\\n"
23600 " [] { \\\n"
23601 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
23602 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
23603 " }",
23604 Style);
23605 verifyFormat("#define SORT(v) \\\n"
23606 " std::sort(v.begin(), v.end(), \\\n"
23607 " [](const auto &foo, const auto &bar) { \\\n"
23608 " return foo.baz < bar.baz; \\\n"
23609 " });",
23610 Style);
23611 verifyFormat("void foo() {\n"
23612 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23613 " auto f = e(d);\n"
23614 " return f;\n"
23615 " })));\n"
23616 "}",
23617 Style);
23618 verifyFormat("void foo() {\n"
23619 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23620 " auto f = e(foo, [&] {\n"
23621 " auto g = h();\n"
23622 " return g;\n"
23623 " }, qux, [&] -> Bar {\n"
23624 " auto i = j();\n"
23625 " return i;\n"
23626 " });\n"
23627 " return f;\n"
23628 " })));\n"
23629 "}",
23630 Style);
23631 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23632 " AnotherLongClassName baz)\n"
23633 " : baz{baz}, func{[&] {\n"
23634 " auto qux = bar;\n"
23635 " return aFunkyFunctionCall(qux);\n"
23636 " }} {}",
23637 Style);
23638 verifyFormat("void foo() {\n"
23639 " class Foo {\n"
23640 " public:\n"
23641 " Foo()\n"
23642 " : qux{[](int quux) {\n"
23643 " auto tmp = quux;\n"
23644 " return tmp;\n"
23645 " }} {}\n"
23646 "\n"
23647 " private:\n"
23648 " std::function<void(int quux)> qux;\n"
23649 " };\n"
23650 "}",
23651 Style);
23652 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
23653 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23654 " AnotherLongClassName baz) :\n"
23655 " baz{baz}, func{[&] {\n"
23656 " auto qux = bar;\n"
23657 " return aFunkyFunctionCall(qux);\n"
23658 " }} {}",
23659 Style);
23660 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
23661 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23662 " AnotherLongClassName baz) :\n"
23663 " baz{baz},\n"
23664 " func{[&] {\n"
23665 " auto qux = bar;\n"
23666 " return aFunkyFunctionCall(qux);\n"
23667 " }} {}",
23668 Style);
23669 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
23670 // FIXME: The following test should pass, but fails at the time of writing.
23671 #if 0
23672 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23673 // doesn't force an initial line break, even if lambdas span multiple lines.
23674 verifyFormat("void foo() {\n"
23675 " aFunction(\n"
23676 " [](d) -> Foo {\n"
23677 " auto f = e(d);\n"
23678 " return f;\n"
23679 " }, foo, Bar{}, [] {\n"
23680 " auto g = h();\n"
23681 " return g;\n"
23682 " }, baz);\n"
23683 "}",
23684 Style);
23685 #endif
23686 // A long non-lambda argument forces arguments to span multiple lines and thus
23687 // forces an initial line break when using AlwaysBreak.
23688 verifyFormat("void foo() {\n"
23689 " aFunction(\n"
23690 " 1,\n"
23691 " [](d) -> Foo {\n"
23692 " auto f = e(d);\n"
23693 " return f;\n"
23694 " }, foo, Bar{},\n"
23695 " [] {\n"
23696 " auto g = h();\n"
23697 " return g;\n"
23698 " }, bazzzzz,\n"
23699 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23700 "}",
23701 Style);
23702 Style.BinPackArguments = false;
23703 verifyFormat("void foo() {\n"
23704 " aFunction(\n"
23705 " 1,\n"
23706 " [](d) -> Foo {\n"
23707 " auto f = e(d);\n"
23708 " return f;\n"
23709 " },\n"
23710 " foo,\n"
23711 " Bar{},\n"
23712 " [] {\n"
23713 " auto g = h();\n"
23714 " return g;\n"
23715 " },\n"
23716 " bazzzzz,\n"
23717 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23718 "}",
23719 Style);
23720 Style.BinPackArguments = true;
23721 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23722 Style.BraceWrapping.BeforeLambdaBody = true;
23723 verifyFormat("void foo() {\n"
23724 " aFunction(\n"
23725 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23726 " {\n"
23727 " auto f = e(\n"
23728 " [&]\n"
23729 " {\n"
23730 " auto g = h();\n"
23731 " return g;\n"
23732 " }, qux, [&] -> Bar\n"
23733 " {\n"
23734 " auto i = j();\n"
23735 " return i;\n"
23736 " });\n"
23737 " return f;\n"
23738 " })));\n"
23739 "}",
23740 Style);
23743 TEST_F(FormatTest, LambdaWithLineComments) {
23744 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23745 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23746 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23747 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23748 FormatStyle::ShortLambdaStyle::SLS_All;
23750 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
23751 verifyFormat("auto k = []() // comment\n"
23752 "{ return; }",
23753 LLVMWithBeforeLambdaBody);
23754 verifyFormat("auto k = []() /* comment */ { return; }",
23755 LLVMWithBeforeLambdaBody);
23756 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23757 LLVMWithBeforeLambdaBody);
23758 verifyFormat("auto k = []() // X\n"
23759 "{ return; }",
23760 LLVMWithBeforeLambdaBody);
23761 verifyFormat(
23762 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23763 "{ return; }",
23764 LLVMWithBeforeLambdaBody);
23766 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
23768 verifyFormat("foo([]()\n"
23769 " {\n"
23770 " bar(); //\n"
23771 " return 1; // comment\n"
23772 " }());",
23773 "foo([]() {\n"
23774 " bar(); //\n"
23775 " return 1; // comment\n"
23776 "}());",
23777 LLVMWithBeforeLambdaBody);
23778 verifyFormat("foo(\n"
23779 " 1, MACRO {\n"
23780 " baz();\n"
23781 " bar(); // comment\n"
23782 " },\n"
23783 " []() {});",
23784 "foo(\n"
23785 " 1, MACRO { baz(); bar(); // comment\n"
23786 " }, []() {}\n"
23787 ");",
23788 LLVMWithBeforeLambdaBody);
23791 TEST_F(FormatTest, EmptyLinesInLambdas) {
23792 verifyFormat("auto lambda = []() {\n"
23793 " x(); //\n"
23794 "};",
23795 "auto lambda = []() {\n"
23796 "\n"
23797 " x(); //\n"
23798 "\n"
23799 "};");
23802 TEST_F(FormatTest, FormatsBlocks) {
23803 FormatStyle ShortBlocks = getLLVMStyle();
23804 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23805 verifyFormat("int (^Block)(int, int);", ShortBlocks);
23806 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
23807 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
23808 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
23809 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
23810 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
23812 verifyFormat("foo(^{ bar(); });", ShortBlocks);
23813 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
23814 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
23816 verifyFormat("[operation setCompletionBlock:^{\n"
23817 " [self onOperationDone];\n"
23818 "}];");
23819 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23820 " [self onOperationDone];\n"
23821 "}]};");
23822 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23823 " f();\n"
23824 "}];");
23825 verifyFormat("int a = [operation block:^int(int *i) {\n"
23826 " return 1;\n"
23827 "}];");
23828 verifyFormat("[myObject doSomethingWith:arg1\n"
23829 " aaa:^int(int *a) {\n"
23830 " return 1;\n"
23831 " }\n"
23832 " bbb:f(a * bbbbbbbb)];");
23834 verifyFormat("[operation setCompletionBlock:^{\n"
23835 " [self.delegate newDataAvailable];\n"
23836 "}];",
23837 getLLVMStyleWithColumns(60));
23838 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23839 " NSString *path = [self sessionFilePath];\n"
23840 " if (path) {\n"
23841 " // ...\n"
23842 " }\n"
23843 "});");
23844 verifyFormat("[[SessionService sharedService]\n"
23845 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23846 " if (window) {\n"
23847 " [self windowDidLoad:window];\n"
23848 " } else {\n"
23849 " [self errorLoadingWindow];\n"
23850 " }\n"
23851 " }];");
23852 verifyFormat("void (^largeBlock)(void) = ^{\n"
23853 " // ...\n"
23854 "};",
23855 getLLVMStyleWithColumns(40));
23856 verifyFormat("[[SessionService sharedService]\n"
23857 " loadWindowWithCompletionBlock: //\n"
23858 " ^(SessionWindow *window) {\n"
23859 " if (window) {\n"
23860 " [self windowDidLoad:window];\n"
23861 " } else {\n"
23862 " [self errorLoadingWindow];\n"
23863 " }\n"
23864 " }];",
23865 getLLVMStyleWithColumns(60));
23866 verifyFormat("[myObject doSomethingWith:arg1\n"
23867 " firstBlock:^(Foo *a) {\n"
23868 " // ...\n"
23869 " int i;\n"
23870 " }\n"
23871 " secondBlock:^(Bar *b) {\n"
23872 " // ...\n"
23873 " int i;\n"
23874 " }\n"
23875 " thirdBlock:^Foo(Bar *b) {\n"
23876 " // ...\n"
23877 " int i;\n"
23878 " }];");
23879 verifyFormat("[myObject doSomethingWith:arg1\n"
23880 " firstBlock:-1\n"
23881 " secondBlock:^(Bar *b) {\n"
23882 " // ...\n"
23883 " int i;\n"
23884 " }];");
23886 verifyFormat("f(^{\n"
23887 " @autoreleasepool {\n"
23888 " if (a) {\n"
23889 " g();\n"
23890 " }\n"
23891 " }\n"
23892 "});");
23893 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23894 "};");
23895 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23896 "};");
23898 FormatStyle FourIndent = getLLVMStyle();
23899 FourIndent.ObjCBlockIndentWidth = 4;
23900 verifyFormat("[operation setCompletionBlock:^{\n"
23901 " [self onOperationDone];\n"
23902 "}];",
23903 FourIndent);
23906 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
23907 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
23909 verifyFormat("[[SessionService sharedService] "
23910 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23911 " if (window) {\n"
23912 " [self windowDidLoad:window];\n"
23913 " } else {\n"
23914 " [self errorLoadingWindow];\n"
23915 " }\n"
23916 "}];",
23917 ZeroColumn);
23918 verifyFormat("[[SessionService sharedService]\n"
23919 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23920 " if (window) {\n"
23921 " [self windowDidLoad:window];\n"
23922 " } else {\n"
23923 " [self errorLoadingWindow];\n"
23924 " }\n"
23925 " }];",
23926 "[[SessionService sharedService]\n"
23927 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23928 " if (window) {\n"
23929 " [self windowDidLoad:window];\n"
23930 " } else {\n"
23931 " [self errorLoadingWindow];\n"
23932 " }\n"
23933 "}];",
23934 ZeroColumn);
23935 verifyFormat("[myObject doSomethingWith:arg1\n"
23936 " firstBlock:^(Foo *a) {\n"
23937 " // ...\n"
23938 " int i;\n"
23939 " }\n"
23940 " secondBlock:^(Bar *b) {\n"
23941 " // ...\n"
23942 " int i;\n"
23943 " }\n"
23944 " thirdBlock:^Foo(Bar *b) {\n"
23945 " // ...\n"
23946 " int i;\n"
23947 " }];",
23948 ZeroColumn);
23949 verifyFormat("f(^{\n"
23950 " @autoreleasepool {\n"
23951 " if (a) {\n"
23952 " g();\n"
23953 " }\n"
23954 " }\n"
23955 "});",
23956 ZeroColumn);
23957 verifyFormat("void (^largeBlock)(void) = ^{\n"
23958 " // ...\n"
23959 "};",
23960 ZeroColumn);
23962 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23963 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23964 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23965 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23966 verifyFormat("void (^largeBlock)(void) = ^{\n"
23967 " int i;\n"
23968 "};",
23969 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23972 TEST_F(FormatTest, SupportsCRLF) {
23973 verifyFormat("int a;\r\n"
23974 "int b;\r\n"
23975 "int c;",
23976 "int a;\r\n"
23977 " int b;\r\n"
23978 " int c;");
23979 verifyFormat("int a;\r\n"
23980 "int b;\r\n"
23981 "int c;\r\n",
23982 "int a;\r\n"
23983 " int b;\n"
23984 " int c;\r\n");
23985 verifyFormat("int a;\n"
23986 "int b;\n"
23987 "int c;",
23988 "int a;\r\n"
23989 " int b;\n"
23990 " int c;");
23991 // FIXME: unstable test case
23992 EXPECT_EQ("\"aaaaaaa \"\r\n"
23993 "\"bbbbbbb\";\r\n",
23994 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23995 verifyFormat("#define A \\\r\n"
23996 " b; \\\r\n"
23997 " c; \\\r\n"
23998 " d;",
23999 "#define A \\\r\n"
24000 " b; \\\r\n"
24001 " c; d; ",
24002 getGoogleStyle());
24004 verifyNoChange("/*\r\n"
24005 "multi line block comments\r\n"
24006 "should not introduce\r\n"
24007 "an extra carriage return\r\n"
24008 "*/");
24009 verifyFormat("/*\r\n"
24010 "\r\n"
24011 "*/",
24012 "/*\r\n"
24013 " \r\r\r\n"
24014 "*/");
24016 FormatStyle style = getLLVMStyle();
24018 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
24019 verifyFormat("union FooBarBazQux {\n"
24020 " int foo;\n"
24021 " int bar;\n"
24022 " int baz;\n"
24023 "};",
24024 "union FooBarBazQux {\r\n"
24025 " int foo;\n"
24026 " int bar;\r\n"
24027 " int baz;\n"
24028 "};",
24029 style);
24030 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24031 verifyFormat("union FooBarBazQux {\r\n"
24032 " int foo;\r\n"
24033 " int bar;\r\n"
24034 " int baz;\r\n"
24035 "};",
24036 "union FooBarBazQux {\r\n"
24037 " int foo;\n"
24038 " int bar;\r\n"
24039 " int baz;\n"
24040 "};",
24041 style);
24043 style.LineEnding = FormatStyle::LE_LF;
24044 verifyFormat("union FooBarBazQux {\n"
24045 " int foo;\n"
24046 " int bar;\n"
24047 " int baz;\n"
24048 " int qux;\n"
24049 "};",
24050 "union FooBarBazQux {\r\n"
24051 " int foo;\n"
24052 " int bar;\r\n"
24053 " int baz;\n"
24054 " int qux;\r\n"
24055 "};",
24056 style);
24057 style.LineEnding = FormatStyle::LE_CRLF;
24058 verifyFormat("union FooBarBazQux {\r\n"
24059 " int foo;\r\n"
24060 " int bar;\r\n"
24061 " int baz;\r\n"
24062 " int qux;\r\n"
24063 "};",
24064 "union FooBarBazQux {\r\n"
24065 " int foo;\n"
24066 " int bar;\r\n"
24067 " int baz;\n"
24068 " int qux;\n"
24069 "};",
24070 style);
24072 style.LineEnding = FormatStyle::LE_DeriveLF;
24073 verifyFormat("union FooBarBazQux {\r\n"
24074 " int foo;\r\n"
24075 " int bar;\r\n"
24076 " int baz;\r\n"
24077 " int qux;\r\n"
24078 "};",
24079 "union FooBarBazQux {\r\n"
24080 " int foo;\n"
24081 " int bar;\r\n"
24082 " int baz;\n"
24083 " int qux;\r\n"
24084 "};",
24085 style);
24086 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24087 verifyFormat("union FooBarBazQux {\n"
24088 " int foo;\n"
24089 " int bar;\n"
24090 " int baz;\n"
24091 " int qux;\n"
24092 "};",
24093 "union FooBarBazQux {\r\n"
24094 " int foo;\n"
24095 " int bar;\r\n"
24096 " int baz;\n"
24097 " int qux;\n"
24098 "};",
24099 style);
24102 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
24103 verifyFormat("MY_CLASS(C) {\n"
24104 " int i;\n"
24105 " int j;\n"
24106 "};");
24109 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
24110 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
24111 TwoIndent.ContinuationIndentWidth = 2;
24113 verifyFormat("int i =\n"
24114 " longFunction(\n"
24115 " arg);",
24116 "int i = longFunction(arg);", TwoIndent);
24118 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
24119 SixIndent.ContinuationIndentWidth = 6;
24121 verifyFormat("int i =\n"
24122 " longFunction(\n"
24123 " arg);",
24124 "int i = longFunction(arg);", SixIndent);
24127 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
24128 FormatStyle Style = getLLVMStyle();
24129 verifyFormat("int Foo::getter(\n"
24130 " //\n"
24131 ") const {\n"
24132 " return foo;\n"
24133 "}",
24134 Style);
24135 verifyFormat("void Foo::setter(\n"
24136 " //\n"
24137 ") {\n"
24138 " foo = 1;\n"
24139 "}",
24140 Style);
24143 TEST_F(FormatTest, SpacesInAngles) {
24144 FormatStyle Spaces = getLLVMStyle();
24145 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24147 verifyFormat("vector< ::std::string > x1;", Spaces);
24148 verifyFormat("Foo< int, Bar > x2;", Spaces);
24149 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
24151 verifyFormat("static_cast< int >(arg);", Spaces);
24152 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
24153 verifyFormat("f< int, float >();", Spaces);
24154 verifyFormat("template <> g() {}", Spaces);
24155 verifyFormat("template < std::vector< int > > f() {}", Spaces);
24156 verifyFormat("std::function< void(int, int) > fct;", Spaces);
24157 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24158 Spaces);
24160 Spaces.Standard = FormatStyle::LS_Cpp03;
24161 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24162 verifyFormat("A< A< int > >();", Spaces);
24164 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24165 verifyFormat("A<A<int> >();", Spaces);
24167 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24168 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24169 Spaces);
24170 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24171 Spaces);
24173 verifyFormat("A<A<int> >();", Spaces);
24174 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
24175 verifyFormat("A< A< int > >();", Spaces);
24177 Spaces.Standard = FormatStyle::LS_Cpp11;
24178 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24179 verifyFormat("A< A< int > >();", Spaces);
24181 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24182 verifyFormat("vector<::std::string> x4;", Spaces);
24183 verifyFormat("vector<int> x5;", Spaces);
24184 verifyFormat("Foo<int, Bar> x6;", Spaces);
24185 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24187 verifyFormat("A<A<int>>();", Spaces);
24189 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24190 verifyFormat("vector<::std::string> x4;", Spaces);
24191 verifyFormat("vector< ::std::string > x4;", Spaces);
24192 verifyFormat("vector<int> x5;", Spaces);
24193 verifyFormat("vector< int > x5;", Spaces);
24194 verifyFormat("Foo<int, Bar> x6;", Spaces);
24195 verifyFormat("Foo< int, Bar > x6;", Spaces);
24196 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24197 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
24199 verifyFormat("A<A<int>>();", Spaces);
24200 verifyFormat("A< A< int > >();", Spaces);
24201 verifyFormat("A<A<int > >();", Spaces);
24202 verifyFormat("A< A< int>>();", Spaces);
24204 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24205 verifyFormat("// clang-format off\n"
24206 "foo<<<1, 1>>>();\n"
24207 "// clang-format on",
24208 Spaces);
24209 verifyFormat("// clang-format off\n"
24210 "foo< < <1, 1> > >();\n"
24211 "// clang-format on",
24212 Spaces);
24215 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
24216 FormatStyle Style = getLLVMStyle();
24217 Style.SpaceAfterTemplateKeyword = false;
24218 verifyFormat("template<int> void foo();", Style);
24221 TEST_F(FormatTest, TripleAngleBrackets) {
24222 verifyFormat("f<<<1, 1>>>();");
24223 verifyFormat("f<<<1, 1, 1, s>>>();");
24224 verifyFormat("f<<<a, b, c, d>>>();");
24225 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24226 verifyFormat("f<param><<<1, 1>>>();");
24227 verifyFormat("f<1><<<1, 1>>>();");
24228 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24229 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24230 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24231 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24232 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24235 TEST_F(FormatTest, MergeLessLessAtEnd) {
24236 verifyFormat("<<");
24237 verifyFormat("< < <", "\\\n<<<");
24238 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24239 "aaallvm::outs() <<");
24240 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24241 "aaaallvm::outs()\n <<");
24244 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
24245 std::string code = "#if A\n"
24246 "#if B\n"
24247 "a.\n"
24248 "#endif\n"
24249 " a = 1;\n"
24250 "#else\n"
24251 "#endif\n"
24252 "#if C\n"
24253 "#else\n"
24254 "#endif\n";
24255 verifyFormat(code);
24258 TEST_F(FormatTest, HandleConflictMarkers) {
24259 // Git/SVN conflict markers.
24260 verifyFormat("int a;\n"
24261 "void f() {\n"
24262 " callme(some(parameter1,\n"
24263 "<<<<<<< text by the vcs\n"
24264 " parameter2),\n"
24265 "||||||| text by the vcs\n"
24266 " parameter2),\n"
24267 " parameter3,\n"
24268 "======= text by the vcs\n"
24269 " parameter2, parameter3),\n"
24270 ">>>>>>> text by the vcs\n"
24271 " otherparameter);",
24272 "int a;\n"
24273 "void f() {\n"
24274 " callme(some(parameter1,\n"
24275 "<<<<<<< text by the vcs\n"
24276 " parameter2),\n"
24277 "||||||| text by the vcs\n"
24278 " parameter2),\n"
24279 " parameter3,\n"
24280 "======= text by the vcs\n"
24281 " parameter2,\n"
24282 " parameter3),\n"
24283 ">>>>>>> text by the vcs\n"
24284 " otherparameter);");
24286 // Perforce markers.
24287 verifyFormat("void f() {\n"
24288 " function(\n"
24289 ">>>> text by the vcs\n"
24290 " parameter,\n"
24291 "==== text by the vcs\n"
24292 " parameter,\n"
24293 "==== text by the vcs\n"
24294 " parameter,\n"
24295 "<<<< text by the vcs\n"
24296 " parameter);",
24297 "void f() {\n"
24298 " function(\n"
24299 ">>>> text by the vcs\n"
24300 " parameter,\n"
24301 "==== text by the vcs\n"
24302 " parameter,\n"
24303 "==== text by the vcs\n"
24304 " parameter,\n"
24305 "<<<< text by the vcs\n"
24306 " parameter);");
24308 verifyNoChange("<<<<<<<\n"
24309 "|||||||\n"
24310 "=======\n"
24311 ">>>>>>>");
24313 verifyNoChange("<<<<<<<\n"
24314 "|||||||\n"
24315 "int i;\n"
24316 "=======\n"
24317 ">>>>>>>");
24319 // FIXME: Handle parsing of macros around conflict markers correctly:
24320 verifyFormat("#define Macro \\\n"
24321 "<<<<<<<\n"
24322 "Something \\\n"
24323 "|||||||\n"
24324 "Else \\\n"
24325 "=======\n"
24326 "Other \\\n"
24327 ">>>>>>>\n"
24328 " End int i;",
24329 "#define Macro \\\n"
24330 "<<<<<<<\n"
24331 " Something \\\n"
24332 "|||||||\n"
24333 " Else \\\n"
24334 "=======\n"
24335 " Other \\\n"
24336 ">>>>>>>\n"
24337 " End\n"
24338 "int i;");
24340 verifyFormat(R"(====
24341 #ifdef A
24343 #else
24345 #endif
24346 )");
24349 TEST_F(FormatTest, DisableRegions) {
24350 verifyFormat("int i;\n"
24351 "// clang-format off\n"
24352 " int j;\n"
24353 "// clang-format on\n"
24354 "int k;",
24355 " int i;\n"
24356 " // clang-format off\n"
24357 " int j;\n"
24358 " // clang-format on\n"
24359 " int k;");
24360 verifyFormat("int i;\n"
24361 "/* clang-format off */\n"
24362 " int j;\n"
24363 "/* clang-format on */\n"
24364 "int k;",
24365 " int i;\n"
24366 " /* clang-format off */\n"
24367 " int j;\n"
24368 " /* clang-format on */\n"
24369 " int k;");
24371 // Don't reflow comments within disabled regions.
24372 verifyFormat("// clang-format off\n"
24373 "// long long long long long long line\n"
24374 "/* clang-format on */\n"
24375 "/* long long long\n"
24376 " * long long long\n"
24377 " * line */\n"
24378 "int i;\n"
24379 "/* clang-format off */\n"
24380 "/* long long long long long long line */",
24381 "// clang-format off\n"
24382 "// long long long long long long line\n"
24383 "/* clang-format on */\n"
24384 "/* long long long long long long line */\n"
24385 "int i;\n"
24386 "/* clang-format off */\n"
24387 "/* long long long long long long line */",
24388 getLLVMStyleWithColumns(20));
24390 verifyFormat("int *i;\n"
24391 "// clang-format off:\n"
24392 "int* j;\n"
24393 "// clang-format on: 1\n"
24394 "int *k;",
24395 "int* i;\n"
24396 "// clang-format off:\n"
24397 "int* j;\n"
24398 "// clang-format on: 1\n"
24399 "int* k;");
24401 verifyFormat("int *i;\n"
24402 "// clang-format off:0\n"
24403 "int* j;\n"
24404 "// clang-format only\n"
24405 "int* k;",
24406 "int* i;\n"
24407 "// clang-format off:0\n"
24408 "int* j;\n"
24409 "// clang-format only\n"
24410 "int* k;");
24412 verifyNoChange("// clang-format off\n"
24413 "#if 0\n"
24414 " #if SHOULD_STAY_INDENTED\n"
24415 " #endif\n"
24416 "#endif\n"
24417 "// clang-format on");
24420 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
24421 format("? ) =");
24422 verifyNoCrash("#define a\\\n /**/}");
24423 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n"
24424 "LY52: ! [internal]");
24427 TEST_F(FormatTest, FormatsTableGenCode) {
24428 FormatStyle Style = getLLVMStyle();
24429 Style.Language = FormatStyle::LK_TableGen;
24430 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
24433 TEST_F(FormatTest, ArrayOfTemplates) {
24434 verifyFormat("auto a = new unique_ptr<int>[10];",
24435 "auto a = new unique_ptr<int > [ 10];");
24437 FormatStyle Spaces = getLLVMStyle();
24438 Spaces.SpacesInSquareBrackets = true;
24439 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24440 "auto a = new unique_ptr<int > [10];", Spaces);
24443 TEST_F(FormatTest, ArrayAsTemplateType) {
24444 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24445 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24447 FormatStyle Spaces = getLLVMStyle();
24448 Spaces.SpacesInSquareBrackets = true;
24449 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24450 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
24453 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
24455 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
24456 verifyFormat("using std::cin;\n"
24457 "using std::cout;",
24458 "using std::cout;\n"
24459 "using std::cin;",
24460 getGoogleStyle());
24463 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
24464 FormatStyle Style = getLLVMStyle();
24465 Style.Standard = FormatStyle::LS_Cpp03;
24466 // cpp03 recognize this string as identifier u8 and literal character 'a'
24467 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
24470 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
24471 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24472 // all modes, including C++11, C++14 and C++17
24473 verifyFormat("auto c = u8'a';");
24476 TEST_F(FormatTest, DoNotFormatLikelyXml) {
24477 verifyGoogleFormat("<!-- ;> -->");
24478 verifyNoChange(" <!-- >; -->", getGoogleStyle());
24481 TEST_F(FormatTest, StructuredBindings) {
24482 // Structured bindings is a C++17 feature.
24483 // all modes, including C++11, C++14 and C++17
24484 verifyFormat("auto [a, b] = f();");
24485 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24486 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
24487 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
24488 verifyFormat("auto const volatile [a, b] = f();",
24489 "auto const volatile[a, b] = f();");
24490 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
24491 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
24492 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
24493 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
24494 verifyFormat("auto const volatile &&[a, b] = f();",
24495 "auto const volatile &&[a, b] = f();");
24496 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
24497 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
24498 verifyFormat("const auto volatile &&[a, b] = f();",
24499 "const auto volatile &&[a, b] = f();");
24500 verifyFormat("volatile const auto &&[a, b] = f();",
24501 "volatile const auto &&[a, b] = f();");
24502 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
24504 // Make sure we don't mistake structured bindings for lambdas.
24505 FormatStyle PointerMiddle = getLLVMStyle();
24506 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
24507 verifyGoogleFormat("auto [a1, b]{A * i};");
24508 verifyFormat("auto [a2, b]{A * i};");
24509 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
24510 verifyGoogleFormat("auto const [a1, b]{A * i};");
24511 verifyFormat("auto const [a2, b]{A * i};");
24512 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
24513 verifyGoogleFormat("auto const& [a1, b]{A * i};");
24514 verifyFormat("auto const &[a2, b]{A * i};");
24515 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
24516 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24517 verifyFormat("auto const &&[a2, b]{A * i};");
24518 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
24520 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24521 "for (const auto && [a, b] : some_range) {\n}");
24522 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24523 "for (const auto & [a, b] : some_range) {\n}");
24524 verifyFormat("for (const auto [a, b] : some_range) {\n}",
24525 "for (const auto[a, b] : some_range) {\n}");
24526 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
24527 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
24528 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
24529 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
24530 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
24531 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
24532 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
24533 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
24535 FormatStyle Spaces = getLLVMStyle();
24536 Spaces.SpacesInSquareBrackets = true;
24537 verifyFormat("auto [ a, b ] = f();", Spaces);
24538 verifyFormat("auto &&[ a, b ] = f();", Spaces);
24539 verifyFormat("auto &[ a, b ] = f();", Spaces);
24540 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
24541 verifyFormat("auto const &[ a, b ] = f();", Spaces);
24544 TEST_F(FormatTest, FileAndCode) {
24545 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
24546 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
24547 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
24548 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
24549 EXPECT_EQ(FormatStyle::LK_ObjC,
24550 guessLanguage("foo.h", "@interface Foo\n@end"));
24551 EXPECT_EQ(
24552 FormatStyle::LK_ObjC,
24553 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24554 EXPECT_EQ(FormatStyle::LK_ObjC,
24555 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24556 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
24557 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
24558 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
24559 EXPECT_EQ(FormatStyle::LK_ObjC,
24560 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24561 EXPECT_EQ(FormatStyle::LK_ObjC,
24562 guessLanguage(
24563 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24564 EXPECT_EQ(
24565 FormatStyle::LK_Cpp,
24566 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24567 // Only one of the two preprocessor regions has ObjC-like code.
24568 EXPECT_EQ(FormatStyle::LK_ObjC,
24569 guessLanguage("foo.h", "#if A\n"
24570 "#define B() C\n"
24571 "#else\n"
24572 "#define B() [NSString a:@\"\"]\n"
24573 "#endif"));
24576 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
24577 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
24578 EXPECT_EQ(FormatStyle::LK_ObjC,
24579 guessLanguage("foo.h", "array[[calculator getIndex]];"));
24580 EXPECT_EQ(FormatStyle::LK_Cpp,
24581 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24582 EXPECT_EQ(
24583 FormatStyle::LK_Cpp,
24584 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24585 EXPECT_EQ(FormatStyle::LK_ObjC,
24586 guessLanguage("foo.h", "[[noreturn foo] bar];"));
24587 EXPECT_EQ(FormatStyle::LK_Cpp,
24588 guessLanguage("foo.h", "[[clang::fallthrough]];"));
24589 EXPECT_EQ(FormatStyle::LK_ObjC,
24590 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24591 EXPECT_EQ(FormatStyle::LK_Cpp,
24592 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24593 EXPECT_EQ(FormatStyle::LK_Cpp,
24594 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24595 EXPECT_EQ(FormatStyle::LK_ObjC,
24596 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24597 EXPECT_EQ(FormatStyle::LK_Cpp,
24598 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24599 EXPECT_EQ(
24600 FormatStyle::LK_Cpp,
24601 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24602 EXPECT_EQ(
24603 FormatStyle::LK_Cpp,
24604 guessLanguage("foo.h",
24605 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24606 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24609 TEST_F(FormatTest, GuessLanguageWithCaret) {
24610 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
24611 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
24612 EXPECT_EQ(FormatStyle::LK_ObjC,
24613 guessLanguage("foo.h", "int(^)(char, float);"));
24614 EXPECT_EQ(FormatStyle::LK_ObjC,
24615 guessLanguage("foo.h", "int(^foo)(char, float);"));
24616 EXPECT_EQ(FormatStyle::LK_ObjC,
24617 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24618 EXPECT_EQ(FormatStyle::LK_ObjC,
24619 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24620 EXPECT_EQ(
24621 FormatStyle::LK_ObjC,
24622 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24625 TEST_F(FormatTest, GuessLanguageWithPragmas) {
24626 EXPECT_EQ(FormatStyle::LK_Cpp,
24627 guessLanguage("foo.h", "__pragma(warning(disable:))"));
24628 EXPECT_EQ(FormatStyle::LK_Cpp,
24629 guessLanguage("foo.h", "#pragma(warning(disable:))"));
24630 EXPECT_EQ(FormatStyle::LK_Cpp,
24631 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24634 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
24635 // ASM symbolic names are identifiers that must be surrounded by [] without
24636 // space in between:
24637 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24639 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24640 verifyFormat(R"(//
24641 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
24642 )");
24644 // A list of several ASM symbolic names.
24645 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
24647 // ASM symbolic names in inline ASM with inputs and outputs.
24648 verifyFormat(R"(//
24649 asm("cmoveq %1, %2, %[result]"
24650 : [result] "=r"(result)
24651 : "r"(test), "r"(new), "[result]"(old));
24652 )");
24654 // ASM symbolic names in inline ASM with no outputs.
24655 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
24658 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
24659 EXPECT_EQ(FormatStyle::LK_Cpp,
24660 guessLanguage("foo.h", "void f() {\n"
24661 " asm (\"mov %[e], %[d]\"\n"
24662 " : [d] \"=rm\" (d)\n"
24663 " [e] \"rm\" (*e));\n"
24664 "}"));
24665 EXPECT_EQ(FormatStyle::LK_Cpp,
24666 guessLanguage("foo.h", "void f() {\n"
24667 " _asm (\"mov %[e], %[d]\"\n"
24668 " : [d] \"=rm\" (d)\n"
24669 " [e] \"rm\" (*e));\n"
24670 "}"));
24671 EXPECT_EQ(FormatStyle::LK_Cpp,
24672 guessLanguage("foo.h", "void f() {\n"
24673 " __asm (\"mov %[e], %[d]\"\n"
24674 " : [d] \"=rm\" (d)\n"
24675 " [e] \"rm\" (*e));\n"
24676 "}"));
24677 EXPECT_EQ(FormatStyle::LK_Cpp,
24678 guessLanguage("foo.h", "void f() {\n"
24679 " __asm__ (\"mov %[e], %[d]\"\n"
24680 " : [d] \"=rm\" (d)\n"
24681 " [e] \"rm\" (*e));\n"
24682 "}"));
24683 EXPECT_EQ(FormatStyle::LK_Cpp,
24684 guessLanguage("foo.h", "void f() {\n"
24685 " asm (\"mov %[e], %[d]\"\n"
24686 " : [d] \"=rm\" (d),\n"
24687 " [e] \"rm\" (*e));\n"
24688 "}"));
24689 EXPECT_EQ(FormatStyle::LK_Cpp,
24690 guessLanguage("foo.h", "void f() {\n"
24691 " asm volatile (\"mov %[e], %[d]\"\n"
24692 " : [d] \"=rm\" (d)\n"
24693 " [e] \"rm\" (*e));\n"
24694 "}"));
24697 TEST_F(FormatTest, GuessLanguageWithChildLines) {
24698 EXPECT_EQ(FormatStyle::LK_Cpp,
24699 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24700 EXPECT_EQ(FormatStyle::LK_ObjC,
24701 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24702 EXPECT_EQ(
24703 FormatStyle::LK_Cpp,
24704 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24705 EXPECT_EQ(
24706 FormatStyle::LK_ObjC,
24707 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24710 TEST_F(FormatTest, TypenameMacros) {
24711 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24713 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24714 FormatStyle Google = getGoogleStyleWithColumns(0);
24715 Google.TypenameMacros = TypenameMacros;
24716 verifyFormat("struct foo {\n"
24717 " int bar;\n"
24718 " TAILQ_ENTRY(a) bleh;\n"
24719 "};",
24720 Google);
24722 FormatStyle Macros = getLLVMStyle();
24723 Macros.TypenameMacros = TypenameMacros;
24725 verifyFormat("STACK_OF(int) a;", Macros);
24726 verifyFormat("STACK_OF(int) *a;", Macros);
24727 verifyFormat("STACK_OF(int const *) *a;", Macros);
24728 verifyFormat("STACK_OF(int *const) *a;", Macros);
24729 verifyFormat("STACK_OF(int, string) a;", Macros);
24730 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
24731 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
24732 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
24733 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
24734 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
24735 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
24737 Macros.PointerAlignment = FormatStyle::PAS_Left;
24738 verifyFormat("STACK_OF(int)* a;", Macros);
24739 verifyFormat("STACK_OF(int*)* a;", Macros);
24740 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
24741 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
24742 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
24745 TEST_F(FormatTest, AtomicQualifier) {
24746 // Check that we treate _Atomic as a type and not a function call
24747 FormatStyle Google = getGoogleStyleWithColumns(0);
24748 verifyFormat("struct foo {\n"
24749 " int a1;\n"
24750 " _Atomic(a) a2;\n"
24751 " _Atomic(_Atomic(int) *const) a3;\n"
24752 "};",
24753 Google);
24754 verifyFormat("_Atomic(uint64_t) a;");
24755 verifyFormat("_Atomic(uint64_t) *a;");
24756 verifyFormat("_Atomic(uint64_t const *) *a;");
24757 verifyFormat("_Atomic(uint64_t *const) *a;");
24758 verifyFormat("_Atomic(const uint64_t *) *a;");
24759 verifyFormat("_Atomic(uint64_t) a;");
24760 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24761 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24762 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24763 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24765 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24766 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24767 FormatStyle Style = getLLVMStyle();
24768 Style.PointerAlignment = FormatStyle::PAS_Left;
24769 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
24770 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
24771 verifyFormat("_Atomic(int)* a;", Style);
24772 verifyFormat("_Atomic(int*)* a;", Style);
24773 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
24775 Style.SpacesInParens = FormatStyle::SIPO_Custom;
24776 Style.SpacesInParensOptions.InCStyleCasts = true;
24777 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
24778 Style.SpacesInParensOptions.InCStyleCasts = false;
24779 Style.SpacesInParensOptions.Other = true;
24780 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
24781 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
24784 TEST_F(FormatTest, C11Generic) {
24785 verifyFormat("_Generic(x, int: 1, default: 0)");
24786 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24787 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24788 verifyFormat("_Generic(x, int: f1, const int: f2)();");
24789 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24791 verifyFormat("_Generic(x,\n"
24792 " float: f,\n"
24793 " default: d,\n"
24794 " long double: ld,\n"
24795 " float _Complex: fc,\n"
24796 " double _Complex: dc,\n"
24797 " long double _Complex: ldc)");
24799 verifyFormat("while (_Generic(x, //\n"
24800 " long: x)(x) > x) {\n"
24801 "}");
24802 verifyFormat("while (_Generic(x, //\n"
24803 " long: x)(x)) {\n"
24804 "}");
24805 verifyFormat("x(_Generic(x, //\n"
24806 " long: x)(x));");
24808 FormatStyle Style = getLLVMStyle();
24809 Style.ColumnLimit = 40;
24810 verifyFormat("#define LIMIT_MAX(T) \\\n"
24811 " _Generic(((T)0), \\\n"
24812 " unsigned int: UINT_MAX, \\\n"
24813 " unsigned long: ULONG_MAX, \\\n"
24814 " unsigned long long: ULLONG_MAX)",
24815 Style);
24816 verifyFormat("_Generic(x,\n"
24817 " struct A: 1,\n"
24818 " void (*)(void): 2);",
24819 Style);
24821 Style.ContinuationIndentWidth = 2;
24822 verifyFormat("_Generic(x,\n"
24823 " struct A: 1,\n"
24824 " void (*)(void): 2);",
24825 Style);
24828 TEST_F(FormatTest, AmbersandInLamda) {
24829 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24830 FormatStyle AlignStyle = getLLVMStyle();
24831 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
24832 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24833 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
24834 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24837 TEST_F(FormatTest, TrailingReturnTypeAuto) {
24838 FormatStyle Style = getLLVMStyle();
24839 verifyFormat("[]() -> auto { return Val; }", Style);
24840 verifyFormat("[]() -> auto * { return Val; }", Style);
24841 verifyFormat("[]() -> auto & { return Val; }", Style);
24842 verifyFormat("auto foo() -> auto { return Val; }", Style);
24843 verifyFormat("auto foo() -> auto * { return Val; }", Style);
24844 verifyFormat("auto foo() -> auto & { return Val; }", Style);
24847 TEST_F(FormatTest, SpacesInConditionalStatement) {
24848 FormatStyle Spaces = getLLVMStyle();
24849 Spaces.IfMacros.clear();
24850 Spaces.IfMacros.push_back("MYIF");
24851 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
24852 Spaces.SpacesInParensOptions.InConditionalStatements = true;
24853 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
24854 verifyFormat("if ( !a )\n return;", Spaces);
24855 verifyFormat("if ( a )\n return;", Spaces);
24856 verifyFormat("if constexpr ( a )\n return;", Spaces);
24857 verifyFormat("MYIF ( a )\n return;", Spaces);
24858 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
24859 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
24860 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
24861 verifyFormat("while ( a )\n return;", Spaces);
24862 verifyFormat("while ( (a && b) )\n return;", Spaces);
24863 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
24864 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
24865 // Check that space on the left of "::" is inserted as expected at beginning
24866 // of condition.
24867 verifyFormat("while ( ::func() )\n return;", Spaces);
24869 // Check impact of ControlStatementsExceptControlMacros is honored.
24870 Spaces.SpaceBeforeParens =
24871 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
24872 verifyFormat("MYIF( a )\n return;", Spaces);
24873 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
24874 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
24877 TEST_F(FormatTest, AlternativeOperators) {
24878 // Test case for ensuring alternate operators are not
24879 // combined with their right most neighbour.
24880 verifyFormat("int a and b;");
24881 verifyFormat("int a and_eq b;");
24882 verifyFormat("int a bitand b;");
24883 verifyFormat("int a bitor b;");
24884 verifyFormat("int a compl b;");
24885 verifyFormat("int a not b;");
24886 verifyFormat("int a not_eq b;");
24887 verifyFormat("int a or b;");
24888 verifyFormat("int a xor b;");
24889 verifyFormat("int a xor_eq b;");
24890 verifyFormat("return this not_eq bitand other;");
24891 verifyFormat("bool operator not_eq(const X bitand other)");
24893 verifyFormat("int a and 5;");
24894 verifyFormat("int a and_eq 5;");
24895 verifyFormat("int a bitand 5;");
24896 verifyFormat("int a bitor 5;");
24897 verifyFormat("int a compl 5;");
24898 verifyFormat("int a not 5;");
24899 verifyFormat("int a not_eq 5;");
24900 verifyFormat("int a or 5;");
24901 verifyFormat("int a xor 5;");
24902 verifyFormat("int a xor_eq 5;");
24904 verifyFormat("int a compl(5);");
24905 verifyFormat("int a not(5);");
24907 verifyFormat("compl foo();"); // ~foo();
24908 verifyFormat("foo() <%%>"); // foo() {}
24909 verifyFormat("void foo() <%%>"); // void foo() {}
24910 verifyFormat("int a<:1:>;"); // int a[1];
24911 verifyFormat("%:define ABC abc"); // #define ABC abc
24912 verifyFormat("%:%:"); // ##
24914 verifyFormat("a = v(not;);\n"
24915 "b = v(not+);\n"
24916 "c = v(not x);\n"
24917 "d = v(not 1);\n"
24918 "e = v(not 123.f);");
24920 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
24921 " V(and) \\\n"
24922 " V(not) \\\n"
24923 " V(not!) \\\n"
24924 " V(other)",
24925 getLLVMStyleWithColumns(40));
24928 TEST_F(FormatTest, STLWhileNotDefineChed) {
24929 verifyFormat("#if defined(while)\n"
24930 "#define while EMIT WARNING C4005\n"
24931 "#endif // while");
24934 TEST_F(FormatTest, OperatorSpacing) {
24935 FormatStyle Style = getLLVMStyle();
24936 Style.PointerAlignment = FormatStyle::PAS_Right;
24937 verifyFormat("Foo::operator*();", Style);
24938 verifyFormat("Foo::operator void *();", Style);
24939 verifyFormat("Foo::operator void **();", Style);
24940 verifyFormat("Foo::operator void *&();", Style);
24941 verifyFormat("Foo::operator void *&&();", Style);
24942 verifyFormat("Foo::operator void const *();", Style);
24943 verifyFormat("Foo::operator void const **();", Style);
24944 verifyFormat("Foo::operator void const *&();", Style);
24945 verifyFormat("Foo::operator void const *&&();", Style);
24946 verifyFormat("Foo::operator()(void *);", Style);
24947 verifyFormat("Foo::operator*(void *);", Style);
24948 verifyFormat("Foo::operator*();", Style);
24949 verifyFormat("Foo::operator**();", Style);
24950 verifyFormat("Foo::operator&();", Style);
24951 verifyFormat("Foo::operator<int> *();", Style);
24952 verifyFormat("Foo::operator<Foo> *();", Style);
24953 verifyFormat("Foo::operator<int> **();", Style);
24954 verifyFormat("Foo::operator<Foo> **();", Style);
24955 verifyFormat("Foo::operator<int> &();", Style);
24956 verifyFormat("Foo::operator<Foo> &();", Style);
24957 verifyFormat("Foo::operator<int> &&();", Style);
24958 verifyFormat("Foo::operator<Foo> &&();", Style);
24959 verifyFormat("Foo::operator<int> *&();", Style);
24960 verifyFormat("Foo::operator<Foo> *&();", Style);
24961 verifyFormat("Foo::operator<int> *&&();", Style);
24962 verifyFormat("Foo::operator<Foo> *&&();", Style);
24963 verifyFormat("operator*(int (*)(), class Foo);", Style);
24965 verifyFormat("Foo::operator&();", Style);
24966 verifyFormat("Foo::operator void &();", Style);
24967 verifyFormat("Foo::operator void const &();", Style);
24968 verifyFormat("Foo::operator()(void &);", Style);
24969 verifyFormat("Foo::operator&(void &);", Style);
24970 verifyFormat("Foo::operator&();", Style);
24971 verifyFormat("operator&(int (&)(), class Foo);", Style);
24972 verifyFormat("operator&&(int (&)(), class Foo);", Style);
24974 verifyFormat("Foo::operator&&();", Style);
24975 verifyFormat("Foo::operator**();", Style);
24976 verifyFormat("Foo::operator void &&();", Style);
24977 verifyFormat("Foo::operator void const &&();", Style);
24978 verifyFormat("Foo::operator()(void &&);", Style);
24979 verifyFormat("Foo::operator&&(void &&);", Style);
24980 verifyFormat("Foo::operator&&();", Style);
24981 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24982 verifyFormat("operator const nsTArrayRight<E> &()", Style);
24983 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24984 Style);
24985 verifyFormat("operator void **()", Style);
24986 verifyFormat("operator const FooRight<Object> &()", Style);
24987 verifyFormat("operator const FooRight<Object> *()", Style);
24988 verifyFormat("operator const FooRight<Object> **()", Style);
24989 verifyFormat("operator const FooRight<Object> *&()", Style);
24990 verifyFormat("operator const FooRight<Object> *&&()", Style);
24992 Style.PointerAlignment = FormatStyle::PAS_Left;
24993 verifyFormat("Foo::operator*();", Style);
24994 verifyFormat("Foo::operator**();", Style);
24995 verifyFormat("Foo::operator void*();", Style);
24996 verifyFormat("Foo::operator void**();", Style);
24997 verifyFormat("Foo::operator void*&();", Style);
24998 verifyFormat("Foo::operator void*&&();", Style);
24999 verifyFormat("Foo::operator void const*();", Style);
25000 verifyFormat("Foo::operator void const**();", Style);
25001 verifyFormat("Foo::operator void const*&();", Style);
25002 verifyFormat("Foo::operator void const*&&();", Style);
25003 verifyFormat("Foo::operator/*comment*/ void*();", Style);
25004 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
25005 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
25006 verifyFormat("Foo::operator()(void*);", Style);
25007 verifyFormat("Foo::operator*(void*);", Style);
25008 verifyFormat("Foo::operator*();", Style);
25009 verifyFormat("Foo::operator<int>*();", Style);
25010 verifyFormat("Foo::operator<Foo>*();", Style);
25011 verifyFormat("Foo::operator<int>**();", Style);
25012 verifyFormat("Foo::operator<Foo>**();", Style);
25013 verifyFormat("Foo::operator<Foo>*&();", Style);
25014 verifyFormat("Foo::operator<int>&();", Style);
25015 verifyFormat("Foo::operator<Foo>&();", Style);
25016 verifyFormat("Foo::operator<int>&&();", Style);
25017 verifyFormat("Foo::operator<Foo>&&();", Style);
25018 verifyFormat("Foo::operator<int>*&();", Style);
25019 verifyFormat("Foo::operator<Foo>*&();", Style);
25020 verifyFormat("operator*(int (*)(), class Foo);", Style);
25022 verifyFormat("Foo::operator&();", Style);
25023 verifyFormat("Foo::operator void&();", Style);
25024 verifyFormat("Foo::operator void const&();", Style);
25025 verifyFormat("Foo::operator/*comment*/ void&();", Style);
25026 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
25027 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
25028 verifyFormat("Foo::operator()(void&);", Style);
25029 verifyFormat("Foo::operator&(void&);", Style);
25030 verifyFormat("Foo::operator&();", Style);
25031 verifyFormat("operator&(int (&)(), class Foo);", Style);
25032 verifyFormat("operator&(int (&&)(), class Foo);", Style);
25033 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25035 verifyFormat("Foo::operator&&();", Style);
25036 verifyFormat("Foo::operator void&&();", Style);
25037 verifyFormat("Foo::operator void const&&();", Style);
25038 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
25039 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
25040 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
25041 verifyFormat("Foo::operator()(void&&);", Style);
25042 verifyFormat("Foo::operator&&(void&&);", Style);
25043 verifyFormat("Foo::operator&&();", Style);
25044 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25045 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
25046 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25047 Style);
25048 verifyFormat("operator void**()", Style);
25049 verifyFormat("operator const FooLeft<Object>&()", Style);
25050 verifyFormat("operator const FooLeft<Object>*()", Style);
25051 verifyFormat("operator const FooLeft<Object>**()", Style);
25052 verifyFormat("operator const FooLeft<Object>*&()", Style);
25053 verifyFormat("operator const FooLeft<Object>*&&()", Style);
25055 // PR45107
25056 verifyFormat("operator Vector<String>&();", Style);
25057 verifyFormat("operator const Vector<String>&();", Style);
25058 verifyFormat("operator foo::Bar*();", Style);
25059 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
25060 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25061 Style);
25063 Style.PointerAlignment = FormatStyle::PAS_Middle;
25064 verifyFormat("Foo::operator*();", Style);
25065 verifyFormat("Foo::operator void *();", Style);
25066 verifyFormat("Foo::operator()(void *);", Style);
25067 verifyFormat("Foo::operator*(void *);", Style);
25068 verifyFormat("Foo::operator*();", Style);
25069 verifyFormat("operator*(int (*)(), class Foo);", Style);
25071 verifyFormat("Foo::operator&();", Style);
25072 verifyFormat("Foo::operator void &();", Style);
25073 verifyFormat("Foo::operator void const &();", Style);
25074 verifyFormat("Foo::operator()(void &);", Style);
25075 verifyFormat("Foo::operator&(void &);", Style);
25076 verifyFormat("Foo::operator&();", Style);
25077 verifyFormat("operator&(int (&)(), class Foo);", Style);
25079 verifyFormat("Foo::operator&&();", Style);
25080 verifyFormat("Foo::operator void &&();", Style);
25081 verifyFormat("Foo::operator void const &&();", Style);
25082 verifyFormat("Foo::operator()(void &&);", Style);
25083 verifyFormat("Foo::operator&&(void &&);", Style);
25084 verifyFormat("Foo::operator&&();", Style);
25085 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25088 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
25089 FormatStyle Style = getLLVMStyle();
25090 // PR46157
25091 verifyFormat("foo(operator+, -42);", Style);
25092 verifyFormat("foo(operator++, -42);", Style);
25093 verifyFormat("foo(operator--, -42);", Style);
25094 verifyFormat("foo(-42, operator--);", Style);
25095 verifyFormat("foo(-42, operator, );", Style);
25096 verifyFormat("foo(operator, , -42);", Style);
25099 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
25100 FormatStyle Style = getLLVMStyle();
25101 Style.WhitespaceSensitiveMacros.push_back("FOO");
25103 // Newlines are important here.
25104 verifyNoChange("FOO(1+2 )\n", Style);
25105 verifyNoChange("FOO(a:b:c)\n", Style);
25107 // Don't use the helpers here, since 'mess up' will change the whitespace
25108 // and these are all whitespace sensitive by definition
25109 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
25110 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
25111 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
25112 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25113 " Still=Intentional);",
25114 Style);
25115 Style.AlignConsecutiveAssignments.Enabled = true;
25116 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25117 " Still=Intentional);",
25118 Style);
25120 Style.ColumnLimit = 21;
25121 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
25124 TEST_F(FormatTest, SkipMacroDefinitionBody) {
25125 auto Style = getLLVMStyle();
25126 Style.SkipMacroDefinitionBody = true;
25128 verifyFormat("#define A", "#define A", Style);
25129 verifyFormat("#define A a aa", "#define A a aa", Style);
25130 verifyNoChange("#define A b", Style);
25131 verifyNoChange("#define A ( args )", Style);
25132 verifyNoChange("#define A ( args ) = func ( args )", Style);
25133 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
25134 verifyNoChange("#define A ( args ) \\\n"
25135 " {\\\n"
25136 " int a = 1 ;\\\n"
25137 "}",
25138 Style);
25140 verifyNoChange("#define A x:", Style);
25141 verifyNoChange("#define A a. b", Style);
25143 // Surrounded with formatted code.
25144 verifyFormat("int a;\n"
25145 "#define A a\n"
25146 "int a;",
25147 "int a ;\n"
25148 "#define A a\n"
25149 "int a ;",
25150 Style);
25152 // Columns are not broken when a limit is set.
25153 Style.ColumnLimit = 10;
25154 verifyFormat("#define A a a a a", " # define A a a a a ", Style);
25155 verifyNoChange("#define A a a a a", Style);
25157 Style.ColumnLimit = 15;
25158 verifyFormat("#define A // a\n"
25159 " // very\n"
25160 " // long\n"
25161 " // comment",
25162 "#define A //a very long comment", Style);
25163 Style.ColumnLimit = 0;
25165 // Multiline definition.
25166 verifyNoChange("#define A \\\n"
25167 "Line one with spaces . \\\n"
25168 " Line two.",
25169 Style);
25170 verifyNoChange("#define A \\\n"
25171 "a a \\\n"
25172 "a \\\n"
25173 "a",
25174 Style);
25175 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
25176 verifyNoChange("#define A \\\n"
25177 "a a \\\n"
25178 "a \\\n"
25179 "a",
25180 Style);
25181 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
25182 verifyNoChange("#define A \\\n"
25183 "a a \\\n"
25184 "a \\\n"
25185 "a",
25186 Style);
25188 // Adjust indendations but don't change the definition.
25189 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25190 verifyNoChange("#if A\n"
25191 "#define A a\n"
25192 "#endif",
25193 Style);
25194 verifyFormat("#if A\n"
25195 "#define A a\n"
25196 "#endif",
25197 "#if A\n"
25198 " #define A a\n"
25199 "#endif",
25200 Style);
25201 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
25202 verifyNoChange("#if A\n"
25203 "# define A a\n"
25204 "#endif",
25205 Style);
25206 verifyFormat("#if A\n"
25207 "# define A a\n"
25208 "#endif",
25209 "#if A\n"
25210 " #define A a\n"
25211 "#endif",
25212 Style);
25213 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
25214 verifyNoChange("#if A\n"
25215 " #define A a\n"
25216 "#endif",
25217 Style);
25218 verifyFormat("#if A\n"
25219 " #define A a\n"
25220 "#endif",
25221 "#if A\n"
25222 " # define A a\n"
25223 "#endif",
25224 Style);
25226 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25227 // SkipMacroDefinitionBody should not affect other PP directives
25228 verifyFormat("#if !defined(A)\n"
25229 "#define A a\n"
25230 "#endif",
25231 "#if ! defined ( A )\n"
25232 " #define A a\n"
25233 "#endif",
25234 Style);
25236 // With comments.
25237 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25238 Style);
25239 verifyNoChange("/* */ #define A a // a a", Style);
25241 verifyFormat("int a; // a\n"
25242 "#define A // a\n"
25243 "int aaa; // a",
25244 "int a; // a\n"
25245 "#define A // a\n"
25246 "int aaa; // a",
25247 Style);
25249 verifyNoChange(
25250 "#define MACRO_WITH_COMMENTS() \\\n"
25251 " public: \\\n"
25252 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
25253 " static MyType getClassTypeId(); \\\n"
25254 " /** Normal comment for the following method. */ \\\n"
25255 " virtual MyType getTypeId() const;",
25256 Style);
25258 // multiline macro definitions
25259 verifyNoChange("#define A a\\\n"
25260 " A a \\\n "
25261 " A a",
25262 Style);
25265 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
25266 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
25267 // test its interaction with line wrapping
25268 FormatStyle Style = getLLVMStyleWithColumns(80);
25269 verifyFormat("namespace {\n"
25270 "int i;\n"
25271 "int j;\n"
25272 "} // namespace",
25273 Style);
25275 verifyFormat("namespace AAA {\n"
25276 "int i;\n"
25277 "int j;\n"
25278 "} // namespace AAA",
25279 Style);
25281 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
25282 "int i;\n"
25283 "int j;\n"
25284 "} // namespace Averyveryveryverylongnamespace",
25285 "namespace Averyveryveryverylongnamespace {\n"
25286 "int i;\n"
25287 "int j;\n"
25288 "}",
25289 Style);
25291 verifyFormat(
25292 "namespace "
25293 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25294 " went::mad::now {\n"
25295 "int i;\n"
25296 "int j;\n"
25297 "} // namespace\n"
25298 " // "
25299 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25300 "went::mad::now",
25301 "namespace "
25302 "would::it::save::you::a::lot::of::time::if_::i::"
25303 "just::gave::up::and_::went::mad::now {\n"
25304 "int i;\n"
25305 "int j;\n"
25306 "}",
25307 Style);
25309 // This used to duplicate the comment again and again on subsequent runs
25310 verifyFormat(
25311 "namespace "
25312 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25313 " went::mad::now {\n"
25314 "int i;\n"
25315 "int j;\n"
25316 "} // namespace\n"
25317 " // "
25318 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25319 "went::mad::now",
25320 "namespace "
25321 "would::it::save::you::a::lot::of::time::if_::i::"
25322 "just::gave::up::and_::went::mad::now {\n"
25323 "int i;\n"
25324 "int j;\n"
25325 "} // namespace\n"
25326 " // "
25327 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
25328 "and_::went::mad::now",
25329 Style);
25332 TEST_F(FormatTest, LikelyUnlikely) {
25333 FormatStyle Style = getLLVMStyle();
25335 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25336 " return 29;\n"
25337 "}",
25338 Style);
25340 verifyFormat("if (argc > 5) [[likely]] {\n"
25341 " return 29;\n"
25342 "}",
25343 Style);
25345 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25346 " return 29;\n"
25347 "} else [[likely]] {\n"
25348 " return 42;\n"
25349 "}",
25350 Style);
25352 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25353 " return 29;\n"
25354 "} else if (argc > 10) [[likely]] {\n"
25355 " return 99;\n"
25356 "} else {\n"
25357 " return 42;\n"
25358 "}",
25359 Style);
25361 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
25362 " return 29;\n"
25363 "}",
25364 Style);
25366 verifyFormat("if (argc > 5) [[unlikely]]\n"
25367 " return 29;",
25368 Style);
25369 verifyFormat("if (argc > 5) [[likely]]\n"
25370 " return 29;",
25371 Style);
25373 verifyFormat("while (limit > 0) [[unlikely]] {\n"
25374 " --limit;\n"
25375 "}",
25376 Style);
25377 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
25378 " --limit;\n"
25379 "}",
25380 Style);
25382 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
25383 " --limit;",
25384 Style);
25385 verifyFormat("while (limit > 0) [[likely]]\n"
25386 " --limit;",
25387 Style);
25389 Style.AttributeMacros.push_back("UNLIKELY");
25390 Style.AttributeMacros.push_back("LIKELY");
25391 verifyFormat("if (argc > 5) UNLIKELY\n"
25392 " return 29;",
25393 Style);
25395 verifyFormat("if (argc > 5) UNLIKELY {\n"
25396 " return 29;\n"
25397 "}",
25398 Style);
25399 verifyFormat("if (argc > 5) UNLIKELY {\n"
25400 " return 29;\n"
25401 "} else [[likely]] {\n"
25402 " return 42;\n"
25403 "}",
25404 Style);
25405 verifyFormat("if (argc > 5) UNLIKELY {\n"
25406 " return 29;\n"
25407 "} else LIKELY {\n"
25408 " return 42;\n"
25409 "}",
25410 Style);
25411 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25412 " return 29;\n"
25413 "} else LIKELY {\n"
25414 " return 42;\n"
25415 "}",
25416 Style);
25418 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25419 " --limit;\n"
25420 "}",
25421 Style);
25422 verifyFormat("while (limit > 0) LIKELY {\n"
25423 " --limit;\n"
25424 "}",
25425 Style);
25427 verifyFormat("while (limit > 0) UNLIKELY\n"
25428 " --limit;",
25429 Style);
25430 verifyFormat("for (auto &limit : limits) LIKELY\n"
25431 " --limit;",
25432 Style);
25435 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
25436 verifyFormat("Constructor()\n"
25437 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25438 " aaaa(aaaaaaaaaaaaaaaaaa, "
25439 "aaaaaaaaaaaaaaaaaat))");
25440 verifyFormat("Constructor()\n"
25441 " : aaaaaaaaaaaaa(aaaaaa), "
25442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25444 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
25445 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
25446 verifyFormat("Constructor()\n"
25447 " : aaaaaa(aaaaaa),\n"
25448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25449 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25450 StyleWithWhitespacePenalty);
25451 verifyFormat("Constructor()\n"
25452 " : aaaaaaaaaaaaa(aaaaaa), "
25453 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25454 StyleWithWhitespacePenalty);
25457 TEST_F(FormatTest, LLVMDefaultStyle) {
25458 FormatStyle Style = getLLVMStyle();
25459 verifyFormat("extern \"C\" {\n"
25460 "int foo();\n"
25461 "}",
25462 Style);
25464 TEST_F(FormatTest, GNUDefaultStyle) {
25465 FormatStyle Style = getGNUStyle();
25466 verifyFormat("extern \"C\"\n"
25467 "{\n"
25468 " int foo ();\n"
25469 "}",
25470 Style);
25472 TEST_F(FormatTest, MozillaDefaultStyle) {
25473 FormatStyle Style = getMozillaStyle();
25474 verifyFormat("extern \"C\"\n"
25475 "{\n"
25476 " int foo();\n"
25477 "}",
25478 Style);
25480 TEST_F(FormatTest, GoogleDefaultStyle) {
25481 FormatStyle Style = getGoogleStyle();
25482 verifyFormat("extern \"C\" {\n"
25483 "int foo();\n"
25484 "}",
25485 Style);
25487 TEST_F(FormatTest, ChromiumDefaultStyle) {
25488 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
25489 verifyFormat("extern \"C\" {\n"
25490 "int foo();\n"
25491 "}",
25492 Style);
25494 TEST_F(FormatTest, MicrosoftDefaultStyle) {
25495 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
25496 verifyFormat("extern \"C\"\n"
25497 "{\n"
25498 " int foo();\n"
25499 "}",
25500 Style);
25502 TEST_F(FormatTest, WebKitDefaultStyle) {
25503 FormatStyle Style = getWebKitStyle();
25504 verifyFormat("extern \"C\" {\n"
25505 "int foo();\n"
25506 "}",
25507 Style);
25510 TEST_F(FormatTest, Concepts) {
25511 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
25512 FormatStyle::BBCDS_Always);
25514 // The default in LLVM style is REI_OuterScope, but these tests were written
25515 // when the default was REI_Keyword.
25516 FormatStyle Style = getLLVMStyle();
25517 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25519 verifyFormat("template <typename T>\n"
25520 "concept True = true;");
25522 verifyFormat("template <typename T>\n"
25523 "concept C = ((false || foo()) && C2<T>) ||\n"
25524 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25525 getLLVMStyleWithColumns(60));
25527 verifyFormat("template <typename T>\n"
25528 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25529 "sizeof(T) <= 8;");
25531 verifyFormat("template <typename T>\n"
25532 "concept DelayedCheck = true && requires(T t) {\n"
25533 " t.bar();\n"
25534 " t.baz();\n"
25535 " } && sizeof(T) <= 8;",
25536 Style);
25538 verifyFormat("template <typename T>\n"
25539 "concept DelayedCheck = true && requires(T t) { // Comment\n"
25540 " t.bar();\n"
25541 " t.baz();\n"
25542 " } && sizeof(T) <= 8;",
25543 Style);
25545 verifyFormat("template <typename T>\n"
25546 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25547 "sizeof(T) <= 8;");
25549 verifyFormat("template <typename T>\n"
25550 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25552 verifyFormat("template <typename T>\n"
25553 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25555 verifyFormat("template <typename T>\n"
25556 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25558 verifyFormat("template <typename T>\n"
25559 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25560 "&& sizeof(T) <= 8;");
25562 verifyFormat("template <typename T>\n"
25563 "concept DelayedCheck =\n"
25564 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25565 "sizeof(T) <= 8;");
25567 verifyFormat("template <typename T>\n"
25568 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25569 "&& sizeof(T) <= 8;");
25571 verifyFormat(
25572 "template <typename T>\n"
25573 "concept DelayedCheck =\n"
25574 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25576 verifyFormat("template <typename T>\n"
25577 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25578 "&& sizeof(T) <= 8;");
25580 verifyFormat("template <typename T>\n"
25581 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25582 "sizeof(T) <= 8;");
25584 verifyFormat("template <typename T>\n"
25585 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25586 " requires(T t) {\n"
25587 " t.bar();\n"
25588 " t.baz();\n"
25589 " } && sizeof(T) <= 8 && !(4 < 3);",
25590 getLLVMStyleWithColumns(60));
25592 verifyFormat("template <typename T>\n"
25593 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25595 verifyFormat("template <typename T>\n"
25596 "concept C = foo();");
25598 verifyFormat("template <typename T>\n"
25599 "concept C = foo(T());");
25601 verifyFormat("template <typename T>\n"
25602 "concept C = foo(T{});");
25604 verifyFormat("template <typename T>\n"
25605 "concept Size = V<sizeof(T)>::Value > 5;");
25607 verifyFormat("template <typename T>\n"
25608 "concept True = S<T>::Value;");
25610 verifyFormat("template <S T>\n"
25611 "concept True = T.field;");
25613 verifyFormat(
25614 "template <typename T>\n"
25615 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25616 " sizeof(T) <= 8;");
25618 // FIXME: This is misformatted because the fake l paren starts at bool, not at
25619 // the lambda l square.
25620 verifyFormat("template <typename T>\n"
25621 "concept C = [] -> bool { return true; }() && requires(T t) { "
25622 "t.bar(); } &&\n"
25623 " sizeof(T) <= 8;");
25625 verifyFormat(
25626 "template <typename T>\n"
25627 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25628 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25630 verifyFormat("template <typename T>\n"
25631 "concept C = decltype([]() { return std::true_type{}; "
25632 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25633 getLLVMStyleWithColumns(120));
25635 verifyFormat("template <typename T>\n"
25636 "concept C = decltype([]() -> std::true_type { return {}; "
25637 "}())::value &&\n"
25638 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25640 verifyFormat("template <typename T>\n"
25641 "concept C = true;\n"
25642 "Foo Bar;");
25644 verifyFormat("template <typename T>\n"
25645 "concept Hashable = requires(T a) {\n"
25646 " { std::hash<T>{}(a) } -> "
25647 "std::convertible_to<std::size_t>;\n"
25648 " };",
25649 Style);
25651 verifyFormat(
25652 "template <typename T>\n"
25653 "concept EqualityComparable = requires(T a, T b) {\n"
25654 " { a == b } -> std::same_as<bool>;\n"
25655 " };",
25656 Style);
25658 verifyFormat(
25659 "template <typename T>\n"
25660 "concept EqualityComparable = requires(T a, T b) {\n"
25661 " { a == b } -> std::same_as<bool>;\n"
25662 " { a != b } -> std::same_as<bool>;\n"
25663 " };",
25664 Style);
25666 verifyFormat("template <typename T>\n"
25667 "concept WeakEqualityComparable = requires(T a, T b) {\n"
25668 " { a == b };\n"
25669 " { a != b };\n"
25670 " };",
25671 Style);
25673 verifyFormat("template <typename T>\n"
25674 "concept HasSizeT = requires { typename T::size_t; };");
25676 verifyFormat("template <typename T>\n"
25677 "concept Semiregular =\n"
25678 " DefaultConstructible<T> && CopyConstructible<T> && "
25679 "CopyAssignable<T> &&\n"
25680 " requires(T a, std::size_t n) {\n"
25681 " requires Same<T *, decltype(&a)>;\n"
25682 " { a.~T() } noexcept;\n"
25683 " requires Same<T *, decltype(new T)>;\n"
25684 " requires Same<T *, decltype(new T[n])>;\n"
25685 " { delete new T; };\n"
25686 " { delete new T[n]; };\n"
25687 " };",
25688 Style);
25690 verifyFormat("template <typename T>\n"
25691 "concept Semiregular =\n"
25692 " requires(T a, std::size_t n) {\n"
25693 " requires Same<T *, decltype(&a)>;\n"
25694 " { a.~T() } noexcept;\n"
25695 " requires Same<T *, decltype(new T)>;\n"
25696 " requires Same<T *, decltype(new T[n])>;\n"
25697 " { delete new T; };\n"
25698 " { delete new T[n]; };\n"
25699 " { new T } -> std::same_as<T *>;\n"
25700 " } && DefaultConstructible<T> && CopyConstructible<T> && "
25701 "CopyAssignable<T>;",
25702 Style);
25704 verifyFormat(
25705 "template <typename T>\n"
25706 "concept Semiregular =\n"
25707 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
25708 " requires Same<T *, decltype(&a)>;\n"
25709 " { a.~T() } noexcept;\n"
25710 " requires Same<T *, decltype(new T)>;\n"
25711 " requires Same<T *, decltype(new "
25712 "T[n])>;\n"
25713 " { delete new T; };\n"
25714 " { delete new T[n]; };\n"
25715 " } && CopyConstructible<T> && "
25716 "CopyAssignable<T>;",
25717 Style);
25719 verifyFormat("template <typename T>\n"
25720 "concept Two = requires(T t) {\n"
25721 " { t.foo() } -> std::same_as<Bar>;\n"
25722 " } && requires(T &&t) {\n"
25723 " { t.foo() } -> std::same_as<Bar &&>;\n"
25724 " };",
25725 Style);
25727 verifyFormat(
25728 "template <typename T>\n"
25729 "concept C = requires(T x) {\n"
25730 " { *x } -> std::convertible_to<typename T::inner>;\n"
25731 " { x + 1 } noexcept -> std::same_as<int>;\n"
25732 " { x * 1 } -> std::convertible_to<T>;\n"
25733 " };",
25734 Style);
25736 verifyFormat("template <typename T>\n"
25737 "concept C = requires(T x) {\n"
25738 " {\n"
25739 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25740 " } -> long_long_concept_name<T>;\n"
25741 " {\n"
25742 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25743 " } noexcept -> long_long_concept_name<T>;\n"
25744 " };",
25745 Style);
25747 verifyFormat(
25748 "template <typename T, typename U = T>\n"
25749 "concept Swappable = requires(T &&t, U &&u) {\n"
25750 " swap(std::forward<T>(t), std::forward<U>(u));\n"
25751 " swap(std::forward<U>(u), std::forward<T>(t));\n"
25752 " };",
25753 Style);
25755 verifyFormat("template <typename T, typename U>\n"
25756 "concept Common = requires(T &&t, U &&u) {\n"
25757 " typename CommonType<T, U>;\n"
25758 " { CommonType<T, U>(std::forward<T>(t)) };\n"
25759 " };",
25760 Style);
25762 verifyFormat("template <typename T, typename U>\n"
25763 "concept Common = requires(T &&t, U &&u) {\n"
25764 " typename CommonType<T, U>;\n"
25765 " { CommonType<T, U>{std::forward<T>(t)} };\n"
25766 " };",
25767 Style);
25769 verifyFormat(
25770 "template <typename T>\n"
25771 "concept C = requires(T t) {\n"
25772 " requires Bar<T> && Foo<T>;\n"
25773 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25774 " };",
25775 Style);
25777 verifyFormat("template <typename T>\n"
25778 "concept HasFoo = requires(T t) {\n"
25779 " { t.foo() };\n"
25780 " t.foo();\n"
25781 " };\n"
25782 "template <typename T>\n"
25783 "concept HasBar = requires(T t) {\n"
25784 " { t.bar() };\n"
25785 " t.bar();\n"
25786 " };",
25787 Style);
25789 verifyFormat("template <typename T>\n"
25790 "concept Large = sizeof(T) > 10;");
25792 verifyFormat("template <typename T, typename U>\n"
25793 "concept FooableWith = requires(T t, U u) {\n"
25794 " typename T::foo_type;\n"
25795 " { t.foo(u) } -> typename T::foo_type;\n"
25796 " t++;\n"
25797 " };\n"
25798 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25799 Style);
25801 verifyFormat("template <typename T>\n"
25802 "concept Context = is_specialization_of_v<context, T>;");
25804 verifyFormat("template <typename T>\n"
25805 "concept Node = std::is_object_v<T>;");
25807 verifyFormat("template <class T>\n"
25808 "concept integral = __is_integral(T);");
25810 verifyFormat("template <class T>\n"
25811 "concept is2D = __array_extent(T, 1) == 2;");
25813 verifyFormat("template <class T>\n"
25814 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25816 verifyFormat("template <class T, class T2>\n"
25817 "concept Same = __is_same_as<T, T2>;");
25819 verifyFormat(
25820 "template <class _InIt, class _OutIt>\n"
25821 "concept _Can_reread_dest =\n"
25822 " std::forward_iterator<_OutIt> &&\n"
25823 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25825 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
25827 verifyFormat(
25828 "template <typename T>\n"
25829 "concept C = requires(T t) {\n"
25830 " requires Bar<T> && Foo<T>;\n"
25831 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25832 " };",
25833 Style);
25835 verifyFormat("template <typename T>\n"
25836 "concept HasFoo = requires(T t) {\n"
25837 " { t.foo() };\n"
25838 " t.foo();\n"
25839 " };\n"
25840 "template <typename T>\n"
25841 "concept HasBar = requires(T t) {\n"
25842 " { t.bar() };\n"
25843 " t.bar();\n"
25844 " };",
25845 Style);
25847 verifyFormat("template <typename T> concept True = true;", Style);
25849 verifyFormat("template <typename T>\n"
25850 "concept C = decltype([]() -> std::true_type { return {}; "
25851 "}())::value &&\n"
25852 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25853 Style);
25855 verifyFormat("template <typename T>\n"
25856 "concept Semiregular =\n"
25857 " DefaultConstructible<T> && CopyConstructible<T> && "
25858 "CopyAssignable<T> &&\n"
25859 " requires(T a, std::size_t n) {\n"
25860 " requires Same<T *, decltype(&a)>;\n"
25861 " { a.~T() } noexcept;\n"
25862 " requires Same<T *, decltype(new T)>;\n"
25863 " requires Same<T *, decltype(new T[n])>;\n"
25864 " { delete new T; };\n"
25865 " { delete new T[n]; };\n"
25866 " };",
25867 Style);
25869 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
25871 verifyFormat("template <typename T> concept C =\n"
25872 " requires(T t) {\n"
25873 " requires Bar<T> && Foo<T>;\n"
25874 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25875 " };",
25876 Style);
25878 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25879 " { t.foo() };\n"
25880 " t.foo();\n"
25881 " };\n"
25882 "template <typename T> concept HasBar = requires(T t) {\n"
25883 " { t.bar() };\n"
25884 " t.bar();\n"
25885 " };",
25886 Style);
25888 verifyFormat("template <typename T> concept True = true;", Style);
25890 verifyFormat(
25891 "template <typename T> concept C =\n"
25892 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
25893 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25894 Style);
25896 verifyFormat("template <typename T> concept Semiregular =\n"
25897 " DefaultConstructible<T> && CopyConstructible<T> && "
25898 "CopyAssignable<T> &&\n"
25899 " requires(T a, std::size_t n) {\n"
25900 " requires Same<T *, decltype(&a)>;\n"
25901 " { a.~T() } noexcept;\n"
25902 " requires Same<T *, decltype(new T)>;\n"
25903 " requires Same<T *, decltype(new T[n])>;\n"
25904 " { delete new T; };\n"
25905 " { delete new T[n]; };\n"
25906 " };",
25907 Style);
25909 // The following tests are invalid C++, we just want to make sure we don't
25910 // assert.
25911 verifyNoCrash("template <typename T>\n"
25912 "concept C = requires C2<T>;");
25914 verifyNoCrash("template <typename T>\n"
25915 "concept C = 5 + 4;");
25917 verifyNoCrash("template <typename T>\n"
25918 "concept C = class X;");
25920 verifyNoCrash("template <typename T>\n"
25921 "concept C = [] && true;");
25923 verifyNoCrash("template <typename T>\n"
25924 "concept C = [] && requires(T t) { typename T::size_type; };");
25927 TEST_F(FormatTest, RequiresClausesPositions) {
25928 auto Style = getLLVMStyle();
25929 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
25930 EXPECT_EQ(Style.IndentRequiresClause, true);
25932 // The default in LLVM style is REI_OuterScope, but these tests were written
25933 // when the default was REI_Keyword.
25934 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25936 verifyFormat("template <typename T>\n"
25937 " requires(Foo<T> && std::trait<T>)\n"
25938 "struct Bar;",
25939 Style);
25941 verifyFormat("template <typename T>\n"
25942 " requires(Foo<T> && std::trait<T>)\n"
25943 "class Bar {\n"
25944 "public:\n"
25945 " Bar(T t);\n"
25946 " bool baz();\n"
25947 "};",
25948 Style);
25950 verifyFormat(
25951 "template <typename T>\n"
25952 " requires requires(T &&t) {\n"
25953 " typename T::I;\n"
25954 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
25955 " }\n"
25956 "Bar(T) -> Bar<typename T::I>;",
25957 Style);
25959 verifyFormat("template <typename T>\n"
25960 " requires(Foo<T> && std::trait<T>)\n"
25961 "constexpr T MyGlobal;",
25962 Style);
25964 verifyFormat("template <typename T>\n"
25965 " requires Foo<T> && requires(T t) {\n"
25966 " { t.baz() } -> std::same_as<bool>;\n"
25967 " requires std::same_as<T::Factor, int>;\n"
25968 " }\n"
25969 "inline int bar(T t) {\n"
25970 " return t.baz() ? T::Factor : 5;\n"
25971 "}",
25972 Style);
25974 verifyFormat("template <typename T>\n"
25975 "inline int bar(T t)\n"
25976 " requires Foo<T> && requires(T t) {\n"
25977 " { t.baz() } -> std::same_as<bool>;\n"
25978 " requires std::same_as<T::Factor, int>;\n"
25979 " }\n"
25980 "{\n"
25981 " return t.baz() ? T::Factor : 5;\n"
25982 "}",
25983 Style);
25985 verifyFormat("template <typename T>\n"
25986 " requires F<T>\n"
25987 "int bar(T t) {\n"
25988 " return 5;\n"
25989 "}",
25990 Style);
25992 verifyFormat("template <typename T>\n"
25993 "int bar(T t)\n"
25994 " requires F<T>\n"
25995 "{\n"
25996 " return 5;\n"
25997 "}",
25998 Style);
26000 verifyFormat("template <typename T>\n"
26001 "int S::bar(T t) &&\n"
26002 " requires F<T>\n"
26003 "{\n"
26004 " return 5;\n"
26005 "}",
26006 Style);
26008 verifyFormat("template <typename T>\n"
26009 "int bar(T t)\n"
26010 " requires F<T>;",
26011 Style);
26013 Style.IndentRequiresClause = false;
26014 verifyFormat("template <typename T>\n"
26015 "requires F<T>\n"
26016 "int bar(T t) {\n"
26017 " return 5;\n"
26018 "}",
26019 Style);
26021 verifyFormat("template <typename T>\n"
26022 "int S::bar(T t) &&\n"
26023 "requires F<T>\n"
26024 "{\n"
26025 " return 5;\n"
26026 "}",
26027 Style);
26029 verifyFormat("template <typename T>\n"
26030 "int bar(T t)\n"
26031 "requires F<T>\n"
26032 "{\n"
26033 " return 5;\n"
26034 "}",
26035 Style);
26037 Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace;
26038 Style.IndentRequiresClause = true;
26040 verifyFormat("template <typename T>\n"
26041 " requires(Foo<T> && std::trait<T>)\n"
26042 "struct Bar;",
26043 Style);
26045 verifyFormat("template <typename T>\n"
26046 " requires(Foo<T> && std::trait<T>)\n"
26047 "class Bar {\n"
26048 "public:\n"
26049 " Bar(T t);\n"
26050 " bool baz();\n"
26051 "};",
26052 Style);
26054 verifyFormat(
26055 "template <typename T>\n"
26056 " requires requires(T &&t) {\n"
26057 " typename T::I;\n"
26058 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26059 " }\n"
26060 "Bar(T) -> Bar<typename T::I>;",
26061 Style);
26063 verifyFormat("template <typename T>\n"
26064 " requires(Foo<T> && std::trait<T>)\n"
26065 "constexpr T MyGlobal;",
26066 Style);
26068 verifyFormat("template <typename T>\n"
26069 " requires Foo<T> && requires(T t) {\n"
26070 " { t.baz() } -> std::same_as<bool>;\n"
26071 " requires std::same_as<T::Factor, int>;\n"
26072 " }\n"
26073 "inline int bar(T t) {\n"
26074 " return t.baz() ? T::Factor : 5;\n"
26075 "}",
26076 Style);
26078 verifyFormat("template <typename T>\n"
26079 "inline int bar(T t)\n"
26080 " requires Foo<T> && requires(T t) {\n"
26081 " { t.baz() } -> std::same_as<bool>;\n"
26082 " requires std::same_as<T::Factor, int>;\n"
26083 " } {\n"
26084 " return t.baz() ? T::Factor : 5;\n"
26085 "}",
26086 Style);
26088 verifyFormat("template <typename T>\n"
26089 " requires F<T>\n"
26090 "int bar(T t) {\n"
26091 " return 5;\n"
26092 "}",
26093 Style);
26095 verifyFormat("template <typename T>\n"
26096 "int bar(T t)\n"
26097 " requires F<T> {\n"
26098 " return 5;\n"
26099 "}",
26100 Style);
26102 verifyFormat("template <typename T>\n"
26103 "int S::bar(T t) &&\n"
26104 " requires F<T> {\n"
26105 " return 5;\n"
26106 "}",
26107 Style);
26109 verifyFormat("template <typename T>\n"
26110 "int bar(T t)\n"
26111 " requires F<T>;",
26112 Style);
26114 verifyFormat("template <typename T>\n"
26115 "int bar(T t)\n"
26116 " requires F<T> {}",
26117 Style);
26119 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
26120 Style.IndentRequiresClause = false;
26121 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26122 "template <typename T> requires Foo<T> void bar() {}\n"
26123 "template <typename T> void bar() requires Foo<T> {}\n"
26124 "template <typename T> void bar() requires Foo<T>;\n"
26125 "template <typename T> void S::bar() && requires Foo<T> {}\n"
26126 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26127 Style);
26129 auto ColumnStyle = Style;
26130 ColumnStyle.ColumnLimit = 40;
26131 verifyFormat("template <typename AAAAAAA>\n"
26132 "requires Foo<T> struct Bar {};\n"
26133 "template <typename AAAAAAA>\n"
26134 "requires Foo<T> void bar() {}\n"
26135 "template <typename AAAAAAA>\n"
26136 "void bar() requires Foo<T> {}\n"
26137 "template <typename T>\n"
26138 "void S::bar() && requires Foo<T> {}\n"
26139 "template <typename AAAAAAA>\n"
26140 "requires Foo<T> Baz(T) -> Baz<T>;",
26141 ColumnStyle);
26143 verifyFormat("template <typename T>\n"
26144 "requires Foo<AAAAAAA> struct Bar {};\n"
26145 "template <typename T>\n"
26146 "requires Foo<AAAAAAA> void bar() {}\n"
26147 "template <typename T>\n"
26148 "void bar() requires Foo<AAAAAAA> {}\n"
26149 "template <typename T>\n"
26150 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26151 ColumnStyle);
26153 verifyFormat("template <typename AAAAAAA>\n"
26154 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26155 "struct Bar {};\n"
26156 "template <typename AAAAAAA>\n"
26157 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26158 "void bar() {}\n"
26159 "template <typename AAAAAAA>\n"
26160 "void bar()\n"
26161 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26162 "template <typename AAAAAAA>\n"
26163 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26164 "template <typename AAAAAAA>\n"
26165 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26166 "Bar(T) -> Bar<T>;",
26167 ColumnStyle);
26169 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26170 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26172 verifyFormat("template <typename T>\n"
26173 "requires Foo<T> struct Bar {};\n"
26174 "template <typename T>\n"
26175 "requires Foo<T> void bar() {}\n"
26176 "template <typename T>\n"
26177 "void bar()\n"
26178 "requires Foo<T> {}\n"
26179 "template <typename T>\n"
26180 "void bar()\n"
26181 "requires Foo<T>;\n"
26182 "template <typename T>\n"
26183 "void S::bar() &&\n"
26184 "requires Foo<T> {}\n"
26185 "template <typename T>\n"
26186 "requires Foo<T> Bar(T) -> Bar<T>;",
26187 Style);
26189 verifyFormat("template <typename AAAAAAA>\n"
26190 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26191 "struct Bar {};\n"
26192 "template <typename AAAAAAA>\n"
26193 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26194 "void bar() {}\n"
26195 "template <typename AAAAAAA>\n"
26196 "void bar()\n"
26197 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26198 "template <typename AAAAAAA>\n"
26199 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26200 "template <typename AAAAAAA>\n"
26201 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26202 "Bar(T) -> Bar<T>;",
26203 ColumnStyle);
26205 Style.IndentRequiresClause = true;
26206 ColumnStyle.IndentRequiresClause = true;
26208 verifyFormat("template <typename T>\n"
26209 " requires Foo<T> struct Bar {};\n"
26210 "template <typename T>\n"
26211 " requires Foo<T> void bar() {}\n"
26212 "template <typename T>\n"
26213 "void bar()\n"
26214 " requires Foo<T> {}\n"
26215 "template <typename T>\n"
26216 "void S::bar() &&\n"
26217 " requires Foo<T> {}\n"
26218 "template <typename T>\n"
26219 " requires Foo<T> Bar(T) -> Bar<T>;",
26220 Style);
26222 verifyFormat("template <typename AAAAAAA>\n"
26223 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26224 "struct Bar {};\n"
26225 "template <typename AAAAAAA>\n"
26226 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26227 "void bar() {}\n"
26228 "template <typename AAAAAAA>\n"
26229 "void bar()\n"
26230 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26231 "template <typename AAAAAAA>\n"
26232 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26233 "template <typename AAAAAAA>\n"
26234 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26235 "Bar(T) -> Bar<T>;",
26236 ColumnStyle);
26238 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26239 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26241 verifyFormat("template <typename T> requires Foo<T>\n"
26242 "struct Bar {};\n"
26243 "template <typename T> requires Foo<T>\n"
26244 "void bar() {}\n"
26245 "template <typename T>\n"
26246 "void bar() requires Foo<T>\n"
26247 "{}\n"
26248 "template <typename T> void bar() requires Foo<T>;\n"
26249 "template <typename T>\n"
26250 "void S::bar() && requires Foo<T>\n"
26251 "{}\n"
26252 "template <typename T> requires Foo<T>\n"
26253 "Bar(T) -> Bar<T>;",
26254 Style);
26256 verifyFormat("template <typename AAAAAAA>\n"
26257 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26258 "struct Bar {};\n"
26259 "template <typename AAAAAAA>\n"
26260 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26261 "void bar() {}\n"
26262 "template <typename AAAAAAA>\n"
26263 "void bar()\n"
26264 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26265 "{}\n"
26266 "template <typename AAAAAAA>\n"
26267 "requires Foo<AAAAAAAA>\n"
26268 "Bar(T) -> Bar<T>;\n"
26269 "template <typename AAAAAAA>\n"
26270 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26271 "Bar(T) -> Bar<T>;",
26272 ColumnStyle);
26275 TEST_F(FormatTest, RequiresClauses) {
26276 verifyFormat("struct [[nodiscard]] zero_t {\n"
26277 " template <class T>\n"
26278 " requires requires { number_zero_v<T>; }\n"
26279 " [[nodiscard]] constexpr operator T() const {\n"
26280 " return number_zero_v<T>;\n"
26281 " }\n"
26282 "};");
26284 verifyFormat("template <class T>\n"
26285 " requires(std::same_as<int, T>)\n"
26286 "decltype(auto) fun() {}");
26288 auto Style = getLLVMStyle();
26290 verifyFormat(
26291 "template <typename T>\n"
26292 " requires is_default_constructible_v<hash<T>> and\n"
26293 " is_copy_constructible_v<hash<T>> and\n"
26294 " is_move_constructible_v<hash<T>> and\n"
26295 " is_copy_assignable_v<hash<T>> and "
26296 "is_move_assignable_v<hash<T>> and\n"
26297 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
26298 " is_callable_v<hash<T>(T)> and\n"
26299 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
26300 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
26301 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
26302 "struct S {};",
26303 Style);
26305 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
26306 verifyFormat(
26307 "template <typename T>\n"
26308 " requires is_default_constructible_v<hash<T>>\n"
26309 " and is_copy_constructible_v<hash<T>>\n"
26310 " and is_move_constructible_v<hash<T>>\n"
26311 " and is_copy_assignable_v<hash<T>> and "
26312 "is_move_assignable_v<hash<T>>\n"
26313 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
26314 " and is_callable_v<hash<T>(T)>\n"
26315 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
26316 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
26317 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
26318 "&>()))>\n"
26319 "struct S {};",
26320 Style);
26322 Style = getLLVMStyle();
26323 Style.ConstructorInitializerIndentWidth = 4;
26324 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
26325 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
26326 verifyFormat("constexpr Foo(Foo const &other)\n"
26327 " requires std::is_copy_constructible<T>\n"
26328 " : value{other.value} {\n"
26329 " do_magic();\n"
26330 " do_more_magic();\n"
26331 "}",
26332 Style);
26334 // Not a clause, but we once hit an assert.
26335 verifyFormat("#if 0\n"
26336 "#else\n"
26337 "foo();\n"
26338 "#endif\n"
26339 "bar(requires);");
26342 TEST_F(FormatTest, RequiresExpressionIndentation) {
26343 auto Style = getLLVMStyle();
26344 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
26346 verifyFormat("template <typename T>\n"
26347 "concept C = requires(T t) {\n"
26348 " typename T::value;\n"
26349 " requires requires(typename T::value v) {\n"
26350 " { t == v } -> std::same_as<bool>;\n"
26351 " };\n"
26352 "};",
26353 Style);
26355 verifyFormat("template <typename T>\n"
26356 "void bar(T)\n"
26357 " requires Foo<T> && requires(T t) {\n"
26358 " { t.foo() } -> std::same_as<int>;\n"
26359 " } && requires(T t) {\n"
26360 " { t.bar() } -> std::same_as<bool>;\n"
26361 " --t;\n"
26362 " };",
26363 Style);
26365 verifyFormat("template <typename T>\n"
26366 " requires Foo<T> &&\n"
26367 " requires(T t) {\n"
26368 " { t.foo() } -> std::same_as<int>;\n"
26369 " } && requires(T t) {\n"
26370 " { t.bar() } -> std::same_as<bool>;\n"
26371 " --t;\n"
26372 " }\n"
26373 "void bar(T);",
26374 Style);
26376 verifyFormat("template <typename T> void f() {\n"
26377 " if constexpr (requires(T t) {\n"
26378 " { t.bar() } -> std::same_as<bool>;\n"
26379 " }) {\n"
26380 " }\n"
26381 "}",
26382 Style);
26384 verifyFormat("template <typename T> void f() {\n"
26385 " if constexpr (condition && requires(T t) {\n"
26386 " { t.bar() } -> std::same_as<bool>;\n"
26387 " }) {\n"
26388 " }\n"
26389 "}",
26390 Style);
26392 verifyFormat("template <typename T> struct C {\n"
26393 " void f()\n"
26394 " requires requires(T t) {\n"
26395 " { t.bar() } -> std::same_as<bool>;\n"
26396 " };\n"
26397 "};",
26398 Style);
26400 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26402 verifyFormat("template <typename T>\n"
26403 "concept C = requires(T t) {\n"
26404 " typename T::value;\n"
26405 " requires requires(typename T::value v) {\n"
26406 " { t == v } -> std::same_as<bool>;\n"
26407 " };\n"
26408 " };",
26409 Style);
26411 verifyFormat(
26412 "template <typename T>\n"
26413 "void bar(T)\n"
26414 " requires Foo<T> && requires(T t) {\n"
26415 " { t.foo() } -> std::same_as<int>;\n"
26416 " } && requires(T t) {\n"
26417 " { t.bar() } -> std::same_as<bool>;\n"
26418 " --t;\n"
26419 " };",
26420 Style);
26422 verifyFormat("template <typename T>\n"
26423 " requires Foo<T> &&\n"
26424 " requires(T t) {\n"
26425 " { t.foo() } -> std::same_as<int>;\n"
26426 " } && requires(T t) {\n"
26427 " { t.bar() } -> std::same_as<bool>;\n"
26428 " --t;\n"
26429 " }\n"
26430 "void bar(T);",
26431 Style);
26433 verifyFormat("template <typename T> void f() {\n"
26434 " if constexpr (requires(T t) {\n"
26435 " { t.bar() } -> std::same_as<bool>;\n"
26436 " }) {\n"
26437 " }\n"
26438 "}",
26439 Style);
26441 verifyFormat(
26442 "template <typename T> void f() {\n"
26443 " if constexpr (condition && requires(T t) {\n"
26444 " { t.bar() } -> std::same_as<bool>;\n"
26445 " }) {\n"
26446 " }\n"
26447 "}",
26448 Style);
26450 verifyFormat("template <typename T> struct C {\n"
26451 " void f()\n"
26452 " requires requires(T t) {\n"
26453 " { t.bar() } -> std::same_as<bool>;\n"
26454 " };\n"
26455 "};",
26456 Style);
26459 TEST_F(FormatTest, StatementAttributeLikeMacros) {
26460 FormatStyle Style = getLLVMStyle();
26461 StringRef Source = "void Foo::slot() {\n"
26462 " unsigned char MyChar = 'x';\n"
26463 " emit signal(MyChar);\n"
26464 " Q_EMIT signal(MyChar);\n"
26465 "}";
26467 verifyFormat(Source, Style);
26469 Style.AlignConsecutiveDeclarations.Enabled = true;
26470 verifyFormat("void Foo::slot() {\n"
26471 " unsigned char MyChar = 'x';\n"
26472 " emit signal(MyChar);\n"
26473 " Q_EMIT signal(MyChar);\n"
26474 "}",
26475 Source, Style);
26477 Style.StatementAttributeLikeMacros.push_back("emit");
26478 verifyFormat(Source, Style);
26480 Style.StatementAttributeLikeMacros = {};
26481 verifyFormat("void Foo::slot() {\n"
26482 " unsigned char MyChar = 'x';\n"
26483 " emit signal(MyChar);\n"
26484 " Q_EMIT signal(MyChar);\n"
26485 "}",
26486 Source, Style);
26489 TEST_F(FormatTest, IndentAccessModifiers) {
26490 FormatStyle Style = getLLVMStyle();
26491 Style.IndentAccessModifiers = true;
26492 // Members are *two* levels below the record;
26493 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
26494 verifyFormat("class C {\n"
26495 " int i;\n"
26496 "};",
26497 Style);
26498 verifyFormat("union C {\n"
26499 " int i;\n"
26500 " unsigned u;\n"
26501 "};",
26502 Style);
26503 // Access modifiers should be indented one level below the record.
26504 verifyFormat("class C {\n"
26505 " public:\n"
26506 " int i;\n"
26507 "};",
26508 Style);
26509 verifyFormat("class C {\n"
26510 " public /* comment */:\n"
26511 " int i;\n"
26512 "};",
26513 Style);
26514 verifyFormat("struct S {\n"
26515 " private:\n"
26516 " class C {\n"
26517 " int j;\n"
26518 "\n"
26519 " public:\n"
26520 " C();\n"
26521 " };\n"
26522 "\n"
26523 " public:\n"
26524 " int i;\n"
26525 "};",
26526 Style);
26527 // Enumerations are not records and should be unaffected.
26528 Style.AllowShortEnumsOnASingleLine = false;
26529 verifyFormat("enum class E {\n"
26530 " A,\n"
26531 " B\n"
26532 "};",
26533 Style);
26534 // Test with a different indentation width;
26535 // also proves that the result is Style.AccessModifierOffset agnostic.
26536 Style.IndentWidth = 3;
26537 verifyFormat("class C {\n"
26538 " public:\n"
26539 " int i;\n"
26540 "};",
26541 Style);
26542 verifyFormat("class C {\n"
26543 " public /**/:\n"
26544 " int i;\n"
26545 "};",
26546 Style);
26547 Style.AttributeMacros.push_back("FOO");
26548 verifyFormat("class C {\n"
26549 " FOO public:\n"
26550 " int i;\n"
26551 "};",
26552 Style);
26555 TEST_F(FormatTest, LimitlessStringsAndComments) {
26556 auto Style = getLLVMStyleWithColumns(0);
26557 constexpr StringRef Code =
26558 "/**\n"
26559 " * This is a multiline comment with quite some long lines, at least for "
26560 "the LLVM Style.\n"
26561 " * We will redo this with strings and line comments. Just to check if "
26562 "everything is working.\n"
26563 " */\n"
26564 "bool foo() {\n"
26565 " /* Single line multi line comment. */\n"
26566 " const std::string String = \"This is a multiline string with quite "
26567 "some long lines, at least for the LLVM Style.\"\n"
26568 " \"We already did it with multi line "
26569 "comments, and we will do it with line comments. Just to check if "
26570 "everything is working.\";\n"
26571 " // This is a line comment (block) with quite some long lines, at "
26572 "least for the LLVM Style.\n"
26573 " // We already did this with multi line comments and strings. Just to "
26574 "check if everything is working.\n"
26575 " const std::string SmallString = \"Hello World\";\n"
26576 " // Small line comment\n"
26577 " return String.size() > SmallString.size();\n"
26578 "}";
26579 verifyNoChange(Code, Style);
26582 TEST_F(FormatTest, FormatDecayCopy) {
26583 // error cases from unit tests
26584 verifyFormat("foo(auto())");
26585 verifyFormat("foo(auto{})");
26586 verifyFormat("foo(auto({}))");
26587 verifyFormat("foo(auto{{}})");
26589 verifyFormat("foo(auto(1))");
26590 verifyFormat("foo(auto{1})");
26591 verifyFormat("foo(new auto(1))");
26592 verifyFormat("foo(new auto{1})");
26593 verifyFormat("decltype(auto(1)) x;");
26594 verifyFormat("decltype(auto{1}) x;");
26595 verifyFormat("auto(x);");
26596 verifyFormat("auto{x};");
26597 verifyFormat("new auto{x};");
26598 verifyFormat("auto{x} = y;");
26599 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26600 // the user's own fault
26601 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26602 // clearly the user's own fault
26603 verifyFormat("auto (*p)() = f;");
26606 TEST_F(FormatTest, Cpp20ModulesSupport) {
26607 FormatStyle Style = getLLVMStyle();
26608 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
26609 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
26611 verifyFormat("export import foo;", Style);
26612 verifyFormat("export import foo:bar;", Style);
26613 verifyFormat("export import foo.bar;", Style);
26614 verifyFormat("export import foo.bar:baz;", Style);
26615 verifyFormat("export import :bar;", Style);
26616 verifyFormat("export module foo:bar;", Style);
26617 verifyFormat("export module foo;", Style);
26618 verifyFormat("export module foo.bar;", Style);
26619 verifyFormat("export module foo.bar:baz;", Style);
26620 verifyFormat("export import <string_view>;", Style);
26621 verifyFormat("export import <Foo/Bar>;", Style);
26623 verifyFormat("export type_name var;", Style);
26624 verifyFormat("template <class T> export using A = B<T>;", Style);
26625 verifyFormat("export using A = B;", Style);
26626 verifyFormat("export int func() {\n"
26627 " foo();\n"
26628 "}",
26629 Style);
26630 verifyFormat("export struct {\n"
26631 " int foo;\n"
26632 "};",
26633 Style);
26634 verifyFormat("export {\n"
26635 " int foo;\n"
26636 "};",
26637 Style);
26638 verifyFormat("export export char const *hello() { return \"hello\"; }");
26640 verifyFormat("import bar;", Style);
26641 verifyFormat("import foo.bar;", Style);
26642 verifyFormat("import foo:bar;", Style);
26643 verifyFormat("import :bar;", Style);
26644 verifyFormat("import /* module partition */ :bar;", Style);
26645 verifyFormat("import <ctime>;", Style);
26646 verifyFormat("import \"header\";", Style);
26648 verifyFormat("module foo;", Style);
26649 verifyFormat("module foo:bar;", Style);
26650 verifyFormat("module foo.bar;", Style);
26651 verifyFormat("module;", Style);
26653 verifyFormat("export namespace hi {\n"
26654 "const char *sayhi();\n"
26655 "}",
26656 Style);
26658 verifyFormat("module :private;", Style);
26659 verifyFormat("import <foo/bar.h>;", Style);
26660 verifyFormat("import foo...bar;", Style);
26661 verifyFormat("import ..........;", Style);
26662 verifyFormat("module foo:private;", Style);
26663 verifyFormat("import a", Style);
26664 verifyFormat("module a", Style);
26665 verifyFormat("export import a", Style);
26666 verifyFormat("export module a", Style);
26668 verifyFormat("import", Style);
26669 verifyFormat("module", Style);
26670 verifyFormat("export", Style);
26672 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26675 TEST_F(FormatTest, CoroutineForCoawait) {
26676 FormatStyle Style = getLLVMStyle();
26677 verifyFormat("for co_await (auto x : range())\n ;");
26678 verifyFormat("for (auto i : arr) {\n"
26679 "}",
26680 Style);
26681 verifyFormat("for co_await (auto i : arr) {\n"
26682 "}",
26683 Style);
26684 verifyFormat("for co_await (auto i : foo(T{})) {\n"
26685 "}",
26686 Style);
26689 TEST_F(FormatTest, CoroutineCoAwait) {
26690 verifyFormat("int x = co_await foo();");
26691 verifyFormat("int x = (co_await foo());");
26692 verifyFormat("co_await (42);");
26693 verifyFormat("void operator co_await(int);");
26694 verifyFormat("void operator co_await(a);");
26695 verifyFormat("co_await a;");
26696 verifyFormat("co_await missing_await_resume{};");
26697 verifyFormat("co_await a; // comment");
26698 verifyFormat("void test0() { co_await a; }");
26699 verifyFormat("co_await co_await co_await foo();");
26700 verifyFormat("co_await foo().bar();");
26701 verifyFormat("co_await [this]() -> Task { co_return x; }");
26702 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26703 "foo(); }(x, y);");
26705 FormatStyle Style = getLLVMStyleWithColumns(40);
26706 verifyFormat("co_await [this](int a, int b) -> Task {\n"
26707 " co_return co_await foo();\n"
26708 "}(x, y);",
26709 Style);
26710 verifyFormat("co_await;");
26713 TEST_F(FormatTest, CoroutineCoYield) {
26714 verifyFormat("int x = co_yield foo();");
26715 verifyFormat("int x = (co_yield foo());");
26716 verifyFormat("co_yield (42);");
26717 verifyFormat("co_yield {42};");
26718 verifyFormat("co_yield 42;");
26719 verifyFormat("co_yield n++;");
26720 verifyFormat("co_yield ++n;");
26721 verifyFormat("co_yield;");
26724 TEST_F(FormatTest, CoroutineCoReturn) {
26725 verifyFormat("co_return (42);");
26726 verifyFormat("co_return;");
26727 verifyFormat("co_return {};");
26728 verifyFormat("co_return x;");
26729 verifyFormat("co_return co_await foo();");
26730 verifyFormat("co_return co_yield foo();");
26733 TEST_F(FormatTest, EmptyShortBlock) {
26734 auto Style = getLLVMStyle();
26735 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
26737 verifyFormat("try {\n"
26738 " doA();\n"
26739 "} catch (Exception &e) {\n"
26740 " e.printStackTrace();\n"
26741 "}",
26742 Style);
26744 verifyFormat("try {\n"
26745 " doA();\n"
26746 "} catch (Exception &e) {}",
26747 Style);
26750 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
26751 auto Style = getLLVMStyle();
26753 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
26754 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
26755 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
26756 verifyFormat("struct Y<[] { return 0; }> {};", Style);
26758 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
26759 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
26762 TEST_F(FormatTest, MultilineLambdaInConditional) {
26763 auto Style = getLLVMStyleWithColumns(70);
26764 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26765 " ;\n"
26766 " return 5;\n"
26767 "}()\n"
26768 " : 2;",
26769 Style);
26770 verifyFormat(
26771 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26772 " ;\n"
26773 " return 5;\n"
26774 "}();",
26775 Style);
26777 Style = getLLVMStyleWithColumns(60);
26778 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26779 " ? []() {\n"
26780 " ;\n"
26781 " return 5;\n"
26782 " }()\n"
26783 " : 2;",
26784 Style);
26785 verifyFormat("auto aLengthyIdentifier =\n"
26786 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
26787 " ;\n"
26788 " return 5;\n"
26789 " }();",
26790 Style);
26792 Style = getLLVMStyleWithColumns(40);
26793 verifyFormat("auto aLengthyIdentifier =\n"
26794 " oneExpressionSoThatWeBreak ? []() {\n"
26795 " ;\n"
26796 " return 5;\n"
26797 " }()\n"
26798 " : 2;",
26799 Style);
26800 verifyFormat("auto aLengthyIdentifier =\n"
26801 " oneExpressionSoThatWeBreak\n"
26802 " ? 2\n"
26803 " : []() {\n"
26804 " ;\n"
26805 " return 5;\n"
26806 " };",
26807 Style);
26810 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
26811 auto Style = getLLVMStyle();
26813 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
26814 "void functionDecl(int a, int b, int c);";
26816 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26817 "paramF, paramG, paramH, paramI);\n"
26818 "void functionDecl(int argumentA, int argumentB, int "
26819 "argumentC, int argumentD, int argumentE);";
26821 verifyFormat(Short, Style);
26823 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26824 "paramF, paramG, paramH,\n"
26825 " paramI);\n"
26826 "void functionDecl(int argumentA, int argumentB, int "
26827 "argumentC, int argumentD,\n"
26828 " int argumentE);";
26830 verifyFormat(NoBreak, Medium, Style);
26831 verifyFormat(NoBreak,
26832 "functionCall(\n"
26833 " paramA,\n"
26834 " paramB,\n"
26835 " paramC,\n"
26836 " paramD,\n"
26837 " paramE,\n"
26838 " paramF,\n"
26839 " paramG,\n"
26840 " paramH,\n"
26841 " paramI\n"
26842 ");\n"
26843 "void functionDecl(\n"
26844 " int argumentA,\n"
26845 " int argumentB,\n"
26846 " int argumentC,\n"
26847 " int argumentD,\n"
26848 " int argumentE\n"
26849 ");",
26850 Style);
26852 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26853 " nestedLongFunctionCall(argument1, "
26854 "argument2, argument3,\n"
26855 " argument4, "
26856 "argument5));",
26857 Style);
26859 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26861 verifyFormat(Short, Style);
26862 verifyFormat(
26863 "functionCall(\n"
26864 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26865 "paramI\n"
26866 ");\n"
26867 "void functionDecl(\n"
26868 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26869 "argumentE\n"
26870 ");",
26871 Medium, Style);
26873 Style.AllowAllArgumentsOnNextLine = false;
26874 Style.AllowAllParametersOfDeclarationOnNextLine = false;
26876 verifyFormat(Short, Style);
26877 verifyFormat(
26878 "functionCall(\n"
26879 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26880 "paramI\n"
26881 ");\n"
26882 "void functionDecl(\n"
26883 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26884 "argumentE\n"
26885 ");",
26886 Medium, Style);
26888 Style.BinPackArguments = false;
26889 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
26891 verifyFormat(Short, Style);
26893 verifyFormat("functionCall(\n"
26894 " paramA,\n"
26895 " paramB,\n"
26896 " paramC,\n"
26897 " paramD,\n"
26898 " paramE,\n"
26899 " paramF,\n"
26900 " paramG,\n"
26901 " paramH,\n"
26902 " paramI\n"
26903 ");\n"
26904 "void functionDecl(\n"
26905 " int argumentA,\n"
26906 " int argumentB,\n"
26907 " int argumentC,\n"
26908 " int argumentD,\n"
26909 " int argumentE\n"
26910 ");",
26911 Medium, Style);
26913 verifyFormat("outerFunctionCall(\n"
26914 " nestedFunctionCall(argument1),\n"
26915 " nestedLongFunctionCall(\n"
26916 " argument1,\n"
26917 " argument2,\n"
26918 " argument3,\n"
26919 " argument4,\n"
26920 " argument5\n"
26921 " )\n"
26922 ");",
26923 Style);
26925 verifyFormat("int a = (int)b;", Style);
26926 verifyFormat("int a = (int)b;",
26927 "int a = (\n"
26928 " int\n"
26929 ") b;",
26930 Style);
26932 verifyFormat("return (true);", Style);
26933 verifyFormat("return (true);",
26934 "return (\n"
26935 " true\n"
26936 ");",
26937 Style);
26939 verifyFormat("void foo();", Style);
26940 verifyFormat("void foo();",
26941 "void foo(\n"
26942 ");",
26943 Style);
26945 verifyFormat("void foo() {}", Style);
26946 verifyFormat("void foo() {}",
26947 "void foo(\n"
26948 ") {\n"
26949 "}",
26950 Style);
26952 verifyFormat("auto string = std::string();", Style);
26953 verifyFormat("auto string = std::string();",
26954 "auto string = std::string(\n"
26955 ");",
26956 Style);
26958 verifyFormat("void (*functionPointer)() = nullptr;", Style);
26959 verifyFormat("void (*functionPointer)() = nullptr;",
26960 "void (\n"
26961 " *functionPointer\n"
26962 ")\n"
26963 "(\n"
26964 ") = nullptr;",
26965 Style);
26968 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
26969 auto Style = getLLVMStyle();
26971 verifyFormat("if (foo()) {\n"
26972 " return;\n"
26973 "}",
26974 Style);
26976 verifyFormat("if (quiteLongArg !=\n"
26977 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26978 "comment\n"
26979 " return;\n"
26980 "}",
26981 Style);
26983 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26985 verifyFormat("if (foo()) {\n"
26986 " return;\n"
26987 "}",
26988 Style);
26990 verifyFormat("if (quiteLongArg !=\n"
26991 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26992 "comment\n"
26993 " return;\n"
26994 "}",
26995 Style);
26997 verifyFormat("void foo() {\n"
26998 " if (camelCaseName < alsoLongName ||\n"
26999 " anotherEvenLongerName <=\n"
27000 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
27001 "\n"
27002 " otherName < thisLastName) {\n"
27003 " return;\n"
27004 " } else if (quiteLongName < alsoLongName ||\n"
27005 " anotherEvenLongerName <=\n"
27006 " thisReallyReallyReallyReallyReallyReallyLonger"
27007 "Name ||\n"
27008 " otherName < thisLastName) {\n"
27009 " return;\n"
27010 " }\n"
27011 "}",
27012 Style);
27014 Style.ContinuationIndentWidth = 2;
27015 verifyFormat("void foo() {\n"
27016 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
27017 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27018 " if (false) {\n"
27019 " return;\n"
27020 " } else if (thisIsRatherALongIfClause && "
27021 "thatIExpectToBeBroken ||\n"
27022 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27023 " return;\n"
27024 " }\n"
27025 " }\n"
27026 "}",
27027 Style);
27030 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
27031 auto Style = getLLVMStyle();
27033 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27034 " doSomething();\n"
27035 "}",
27036 Style);
27038 verifyFormat("for (int myReallyLongCountVariable = 0; "
27039 "myReallyLongCountVariable < count;\n"
27040 " myReallyLongCountVariable++) {\n"
27041 " doSomething();\n"
27042 "}",
27043 Style);
27045 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27047 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27048 " doSomething();\n"
27049 "}",
27050 Style);
27052 verifyFormat("for (int myReallyLongCountVariable = 0; "
27053 "myReallyLongCountVariable < count;\n"
27054 " myReallyLongCountVariable++) {\n"
27055 " doSomething();\n"
27056 "}",
27057 Style);
27060 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
27061 auto Style = getLLVMStyleWithColumns(60);
27062 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27063 // Aggregate initialization.
27064 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27065 " 10000000, 20000000\n"
27066 "};",
27067 Style);
27068 verifyFormat("SomeStruct s{\n"
27069 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
27070 " \"zzzzzzzzzzzzzzzz\"\n"
27071 "};",
27072 Style);
27073 // Designated initializers.
27074 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27075 " [0] = 10000000, [1] = 20000000\n"
27076 "};",
27077 Style);
27078 verifyFormat("SomeStruct s{\n"
27079 " .foo = \"xxxxxxxxxxxxx\",\n"
27080 " .bar = \"yyyyyyyyyyyyy\",\n"
27081 " .baz = \"zzzzzzzzzzzzz\"\n"
27082 "};",
27083 Style);
27084 // List initialization.
27085 verifyFormat("SomeStruct s{\n"
27086 " \"xxxxxxxxxxxxx\",\n"
27087 " \"yyyyyyyyyyyyy\",\n"
27088 " \"zzzzzzzzzzzzz\",\n"
27089 "};",
27090 Style);
27091 verifyFormat("SomeStruct{\n"
27092 " \"xxxxxxxxxxxxx\",\n"
27093 " \"yyyyyyyyyyyyy\",\n"
27094 " \"zzzzzzzzzzzzz\",\n"
27095 "};",
27096 Style);
27097 verifyFormat("new SomeStruct{\n"
27098 " \"xxxxxxxxxxxxx\",\n"
27099 " \"yyyyyyyyyyyyy\",\n"
27100 " \"zzzzzzzzzzzzz\",\n"
27101 "};",
27102 Style);
27103 // Member initializer.
27104 verifyFormat("class SomeClass {\n"
27105 " SomeStruct s{\n"
27106 " \"xxxxxxxxxxxxx\",\n"
27107 " \"yyyyyyyyyyyyy\",\n"
27108 " \"zzzzzzzzzzzzz\",\n"
27109 " };\n"
27110 "};",
27111 Style);
27112 // Constructor member initializer.
27113 verifyFormat("SomeClass::SomeClass : strct{\n"
27114 " \"xxxxxxxxxxxxx\",\n"
27115 " \"yyyyyyyyyyyyy\",\n"
27116 " \"zzzzzzzzzzzzz\",\n"
27117 " } {}",
27118 Style);
27119 // Copy initialization.
27120 verifyFormat("SomeStruct s = SomeStruct{\n"
27121 " \"xxxxxxxxxxxxx\",\n"
27122 " \"yyyyyyyyyyyyy\",\n"
27123 " \"zzzzzzzzzzzzz\",\n"
27124 "};",
27125 Style);
27126 // Copy list initialization.
27127 verifyFormat("SomeStruct s = {\n"
27128 " \"xxxxxxxxxxxxx\",\n"
27129 " \"yyyyyyyyyyyyy\",\n"
27130 " \"zzzzzzzzzzzzz\",\n"
27131 "};",
27132 Style);
27133 // Assignment operand initialization.
27134 verifyFormat("s = {\n"
27135 " \"xxxxxxxxxxxxx\",\n"
27136 " \"yyyyyyyyyyyyy\",\n"
27137 " \"zzzzzzzzzzzzz\",\n"
27138 "};",
27139 Style);
27140 // Returned object initialization.
27141 verifyFormat("return {\n"
27142 " \"xxxxxxxxxxxxx\",\n"
27143 " \"yyyyyyyyyyyyy\",\n"
27144 " \"zzzzzzzzzzzzz\",\n"
27145 "};",
27146 Style);
27147 // Initializer list.
27148 verifyFormat("auto initializerList = {\n"
27149 " \"xxxxxxxxxxxxx\",\n"
27150 " \"yyyyyyyyyyyyy\",\n"
27151 " \"zzzzzzzzzzzzz\",\n"
27152 "};",
27153 Style);
27154 // Function parameter initialization.
27155 verifyFormat("func({\n"
27156 " \"xxxxxxxxxxxxx\",\n"
27157 " \"yyyyyyyyyyyyy\",\n"
27158 " \"zzzzzzzzzzzzz\",\n"
27159 "});",
27160 Style);
27161 // Nested init lists.
27162 verifyFormat("SomeStruct s = {\n"
27163 " {{init1, init2, init3, init4, init5},\n"
27164 " {init1, init2, init3, init4, init5}}\n"
27165 "};",
27166 Style);
27167 verifyFormat("SomeStruct s = {\n"
27168 " {{\n"
27169 " .init1 = 1,\n"
27170 " .init2 = 2,\n"
27171 " .init3 = 3,\n"
27172 " .init4 = 4,\n"
27173 " .init5 = 5,\n"
27174 " },\n"
27175 " {init1, init2, init3, init4, init5}}\n"
27176 "};",
27177 Style);
27178 verifyFormat("SomeArrayT a[3] = {\n"
27179 " {\n"
27180 " foo,\n"
27181 " bar,\n"
27182 " },\n"
27183 " {\n"
27184 " foo,\n"
27185 " bar,\n"
27186 " },\n"
27187 " SomeArrayT{},\n"
27188 "};",
27189 Style);
27190 verifyFormat("SomeArrayT a[3] = {\n"
27191 " {foo},\n"
27192 " {\n"
27193 " {\n"
27194 " init1,\n"
27195 " init2,\n"
27196 " init3,\n"
27197 " },\n"
27198 " {\n"
27199 " init1,\n"
27200 " init2,\n"
27201 " init3,\n"
27202 " },\n"
27203 " },\n"
27204 " {baz},\n"
27205 "};",
27206 Style);
27209 TEST_F(FormatTest, UnderstandsDigraphs) {
27210 verifyFormat("int arr<:5:> = {};");
27211 verifyFormat("int arr[5] = <%%>;");
27212 verifyFormat("int arr<:::qualified_variable:> = {};");
27213 verifyFormat("int arr[::qualified_variable] = <%%>;");
27214 verifyFormat("%:include <header>");
27215 verifyFormat("%:define A x##y");
27216 verifyFormat("#define A x%:%:y");
27219 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
27220 auto Style = getLLVMStyle();
27221 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27222 Style.AlignConsecutiveAssignments.Enabled = true;
27223 Style.AlignConsecutiveDeclarations.Enabled = true;
27225 // The AlignArray code is incorrect for non square Arrays and can cause
27226 // crashes, these tests assert that the array is not changed but will
27227 // also act as regression tests for when it is properly fixed
27228 verifyFormat("struct test demo[] = {\n"
27229 " {1, 2},\n"
27230 " {3, 4, 5},\n"
27231 " {6, 7, 8}\n"
27232 "};",
27233 Style);
27234 verifyFormat("struct test demo[] = {\n"
27235 " {1, 2, 3, 4, 5},\n"
27236 " {3, 4, 5},\n"
27237 " {6, 7, 8}\n"
27238 "};",
27239 Style);
27240 verifyFormat("struct test demo[] = {\n"
27241 " {1, 2, 3, 4, 5},\n"
27242 " {3, 4, 5},\n"
27243 " {6, 7, 8, 9, 10, 11, 12}\n"
27244 "};",
27245 Style);
27246 verifyFormat("struct test demo[] = {\n"
27247 " {1, 2, 3},\n"
27248 " {3, 4, 5},\n"
27249 " {6, 7, 8, 9, 10, 11, 12}\n"
27250 "};",
27251 Style);
27253 verifyFormat("S{\n"
27254 " {},\n"
27255 " {},\n"
27256 " {a, b}\n"
27257 "};",
27258 Style);
27259 verifyFormat("S{\n"
27260 " {},\n"
27261 " {},\n"
27262 " {a, b},\n"
27263 "};",
27264 Style);
27265 verifyFormat("void foo() {\n"
27266 " auto thing = test{\n"
27267 " {\n"
27268 " {13}, {something}, // A\n"
27269 " }\n"
27270 " };\n"
27271 "}",
27272 "void foo() {\n"
27273 " auto thing = test{\n"
27274 " {\n"
27275 " {13},\n"
27276 " {something}, // A\n"
27277 " }\n"
27278 " };\n"
27279 "}",
27280 Style);
27283 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
27284 auto Style = getLLVMStyle();
27285 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
27286 Style.AlignConsecutiveAssignments.Enabled = true;
27287 Style.AlignConsecutiveDeclarations.Enabled = true;
27289 // The AlignArray code is incorrect for non square Arrays and can cause
27290 // crashes, these tests assert that the array is not changed but will
27291 // also act as regression tests for when it is properly fixed
27292 verifyFormat("struct test demo[] = {\n"
27293 " {1, 2},\n"
27294 " {3, 4, 5},\n"
27295 " {6, 7, 8}\n"
27296 "};",
27297 Style);
27298 verifyFormat("struct test demo[] = {\n"
27299 " {1, 2, 3, 4, 5},\n"
27300 " {3, 4, 5},\n"
27301 " {6, 7, 8}\n"
27302 "};",
27303 Style);
27304 verifyFormat("struct test demo[] = {\n"
27305 " {1, 2, 3, 4, 5},\n"
27306 " {3, 4, 5},\n"
27307 " {6, 7, 8, 9, 10, 11, 12}\n"
27308 "};",
27309 Style);
27310 verifyFormat("struct test demo[] = {\n"
27311 " {1, 2, 3},\n"
27312 " {3, 4, 5},\n"
27313 " {6, 7, 8, 9, 10, 11, 12}\n"
27314 "};",
27315 Style);
27317 verifyFormat("S{\n"
27318 " {},\n"
27319 " {},\n"
27320 " {a, b}\n"
27321 "};",
27322 Style);
27323 verifyFormat("S{\n"
27324 " {},\n"
27325 " {},\n"
27326 " {a, b},\n"
27327 "};",
27328 Style);
27329 verifyFormat("void foo() {\n"
27330 " auto thing = test{\n"
27331 " {\n"
27332 " {13}, {something}, // A\n"
27333 " }\n"
27334 " };\n"
27335 "}",
27336 "void foo() {\n"
27337 " auto thing = test{\n"
27338 " {\n"
27339 " {13},\n"
27340 " {something}, // A\n"
27341 " }\n"
27342 " };\n"
27343 "}",
27344 Style);
27347 TEST_F(FormatTest, FormatsVariableTemplates) {
27348 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27349 verifyFormat("template <typename T> "
27350 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27353 TEST_F(FormatTest, RemoveSemicolon) {
27354 FormatStyle Style = getLLVMStyle();
27355 Style.RemoveSemicolon = true;
27357 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27358 "int max(int a, int b) { return a > b ? a : b; };", Style);
27360 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27361 "int max(int a, int b) { return a > b ? a : b; };;", Style);
27363 verifyFormat("class Foo {\n"
27364 " int getSomething() const { return something; }\n"
27365 "};",
27366 "class Foo {\n"
27367 " int getSomething() const { return something; };\n"
27368 "};",
27369 Style);
27371 verifyFormat("class Foo {\n"
27372 " int getSomething() const { return something; }\n"
27373 "};",
27374 "class Foo {\n"
27375 " int getSomething() const { return something; };;\n"
27376 "};",
27377 Style);
27379 verifyFormat("for (;;) {\n"
27380 "}",
27381 Style);
27383 verifyFormat("class [[deprecated(\"\")]] C {\n"
27384 " int i;\n"
27385 "};",
27386 Style);
27388 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27389 " int i;\n"
27390 "};",
27391 Style);
27393 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
27395 verifyFormat("void main() {}", "void main() {};", Style);
27397 verifyFormat("struct Foo {\n"
27398 " Foo() {}\n"
27399 " ~Foo() {}\n"
27400 "};",
27401 "struct Foo {\n"
27402 " Foo() {};\n"
27403 " ~Foo() {};\n"
27404 "};",
27405 Style);
27407 // We can't (and probably shouldn't) support the following.
27408 #if 0
27409 verifyFormat("void foo() {} //\n"
27410 "int bar;",
27411 "void foo() {}; //\n"
27412 "; int bar;",
27413 Style);
27414 #endif
27416 verifyFormat("auto sgf = [] {\n"
27417 " ogl = {\n"
27418 " a, b, c, d, e,\n"
27419 " };\n"
27420 "};",
27421 Style);
27423 Style.TypenameMacros.push_back("STRUCT");
27424 verifyFormat("STRUCT(T, B) { int i; };", Style);
27427 TEST_F(FormatTest, BreakAfterAttributes) {
27428 constexpr StringRef Code("[[maybe_unused]] const int i;\n"
27429 "[[foo([[]])]] [[maybe_unused]]\n"
27430 "int j;\n"
27431 "[[maybe_unused]]\n"
27432 "foo<int> k;\n"
27433 "[[nodiscard]] inline int f(int &i);\n"
27434 "[[foo([[]])]] [[nodiscard]]\n"
27435 "int g(int &i);\n"
27436 "[[nodiscard]]\n"
27437 "inline int f(int &i) {\n"
27438 " i = 1;\n"
27439 " return 0;\n"
27440 "}\n"
27441 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27442 " i = 0;\n"
27443 " return 1;\n"
27444 "}");
27446 FormatStyle Style = getLLVMStyle();
27447 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
27448 verifyNoChange(Code, Style);
27450 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27451 verifyFormat("[[maybe_unused]] const int i;\n"
27452 "[[foo([[]])]] [[maybe_unused]] int j;\n"
27453 "[[maybe_unused]] foo<int> k;\n"
27454 "[[nodiscard]] inline int f(int &i);\n"
27455 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27456 "[[nodiscard]] inline int f(int &i) {\n"
27457 " i = 1;\n"
27458 " return 0;\n"
27459 "}\n"
27460 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27461 " i = 0;\n"
27462 " return 1;\n"
27463 "}",
27464 Code, Style);
27466 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27467 verifyFormat("[[maybe_unused]]\n"
27468 "const int i;\n"
27469 "[[foo([[]])]] [[maybe_unused]]\n"
27470 "int j;\n"
27471 "[[maybe_unused]]\n"
27472 "foo<int> k;\n"
27473 "[[nodiscard]]\n"
27474 "inline int f(int &i);\n"
27475 "[[foo([[]])]] [[nodiscard]]\n"
27476 "int g(int &i);\n"
27477 "[[nodiscard]]\n"
27478 "inline int f(int &i) {\n"
27479 " i = 1;\n"
27480 " return 0;\n"
27481 "}\n"
27482 "[[foo([[]])]] [[nodiscard]]\n"
27483 "int g(int &i) {\n"
27484 " i = 0;\n"
27485 " return 1;\n"
27486 "}",
27487 Code, Style);
27489 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
27490 " f();\n"
27491 "else\n"
27492 " g();\n"
27493 "[[foo([[]])]]\n"
27494 "switch (b) {\n"
27495 "[[unlikely]] case 1:\n"
27496 " ++b;\n"
27497 " break;\n"
27498 "[[likely]]\n"
27499 "default:\n"
27500 " return;\n"
27501 "}\n"
27502 "[[unlikely]] for (; c > 0; --c)\n"
27503 " h();\n"
27504 "[[likely]]\n"
27505 "while (d > 0)\n"
27506 " --d;");
27508 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
27509 verifyNoChange(CtrlStmtCode, Style);
27511 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27512 verifyFormat("[[likely]] if (a)\n"
27513 " f();\n"
27514 "else\n"
27515 " g();\n"
27516 "[[foo([[]])]] switch (b) {\n"
27517 "[[unlikely]] case 1:\n"
27518 " ++b;\n"
27519 " break;\n"
27520 "[[likely]] default:\n"
27521 " return;\n"
27522 "}\n"
27523 "[[unlikely]] for (; c > 0; --c)\n"
27524 " h();\n"
27525 "[[likely]] while (d > 0)\n"
27526 " --d;",
27527 CtrlStmtCode, Style);
27529 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27530 verifyFormat("[[likely]]\n"
27531 "if (a)\n"
27532 " f();\n"
27533 "else\n"
27534 " g();\n"
27535 "[[foo([[]])]]\n"
27536 "switch (b) {\n"
27537 "[[unlikely]]\n"
27538 "case 1:\n"
27539 " ++b;\n"
27540 " break;\n"
27541 "[[likely]]\n"
27542 "default:\n"
27543 " return;\n"
27544 "}\n"
27545 "[[unlikely]]\n"
27546 "for (; c > 0; --c)\n"
27547 " h();\n"
27548 "[[likely]]\n"
27549 "while (d > 0)\n"
27550 " --d;",
27551 CtrlStmtCode, Style);
27553 constexpr StringRef CtorDtorCode("struct Foo {\n"
27554 " [[deprecated]] Foo();\n"
27555 " [[deprecated]] Foo() {}\n"
27556 " [[deprecated]] ~Foo();\n"
27557 " [[deprecated]] ~Foo() {}\n"
27558 " [[deprecated]] void f();\n"
27559 " [[deprecated]] void f() {}\n"
27560 "};\n"
27561 "[[deprecated]] Bar::Bar() {}\n"
27562 "[[deprecated]] Bar::~Bar() {}\n"
27563 "[[deprecated]] void g() {}");
27564 verifyFormat("struct Foo {\n"
27565 " [[deprecated]]\n"
27566 " Foo();\n"
27567 " [[deprecated]]\n"
27568 " Foo() {}\n"
27569 " [[deprecated]]\n"
27570 " ~Foo();\n"
27571 " [[deprecated]]\n"
27572 " ~Foo() {}\n"
27573 " [[deprecated]]\n"
27574 " void f();\n"
27575 " [[deprecated]]\n"
27576 " void f() {}\n"
27577 "};\n"
27578 "[[deprecated]]\n"
27579 "Bar::Bar() {}\n"
27580 "[[deprecated]]\n"
27581 "Bar::~Bar() {}\n"
27582 "[[deprecated]]\n"
27583 "void g() {}",
27584 CtorDtorCode, Style);
27586 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
27587 verifyFormat("struct Foo {\n"
27588 " [[deprecated]]\n"
27589 " Foo();\n"
27590 " [[deprecated]]\n"
27591 " Foo()\n"
27592 " {\n"
27593 " }\n"
27594 " [[deprecated]]\n"
27595 " ~Foo();\n"
27596 " [[deprecated]]\n"
27597 " ~Foo()\n"
27598 " {\n"
27599 " }\n"
27600 " [[deprecated]]\n"
27601 " void f();\n"
27602 " [[deprecated]]\n"
27603 " void f()\n"
27604 " {\n"
27605 " }\n"
27606 "};\n"
27607 "[[deprecated]]\n"
27608 "Bar::Bar()\n"
27609 "{\n"
27610 "}\n"
27611 "[[deprecated]]\n"
27612 "Bar::~Bar()\n"
27613 "{\n"
27614 "}\n"
27615 "[[deprecated]]\n"
27616 "void g()\n"
27617 "{\n"
27618 "}",
27619 CtorDtorCode, Style);
27621 verifyFormat("struct Foo {\n"
27622 " [[maybe_unused]]\n"
27623 " void operator+();\n"
27624 "};\n"
27625 "[[nodiscard]]\n"
27626 "Foo &operator-(Foo &);",
27627 Style);
27629 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
27630 verifyFormat("[[nodiscard]]\n"
27631 "Foo& operator-(Foo&);",
27632 Style);
27635 TEST_F(FormatTest, InsertNewlineAtEOF) {
27636 FormatStyle Style = getLLVMStyle();
27637 Style.InsertNewlineAtEOF = true;
27639 verifyNoChange("int i;\n", Style);
27640 verifyFormat("int i;\n", "int i;", Style);
27642 constexpr StringRef Code{"namespace {\n"
27643 "int i;\n"
27644 "} // namespace"};
27645 verifyFormat(Code.str() + '\n', Code, Style,
27646 {tooling::Range(19, 13)}); // line 3
27649 TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
27650 FormatStyle Style = getLLVMStyle();
27651 Style.KeepEmptyLines.AtEndOfFile = true;
27653 const StringRef Code{"int i;\n\n"};
27654 verifyNoChange(Code, Style);
27655 verifyFormat(Code, "int i;\n\n\n", Style);
27658 TEST_F(FormatTest, SpaceAfterUDL) {
27659 verifyFormat("auto c = (4s).count();");
27660 verifyFormat("auto x = 5s .count() == 5;");
27663 TEST_F(FormatTest, InterfaceAsClassMemberName) {
27664 verifyFormat("class Foo {\n"
27665 " int interface;\n"
27666 " Foo::Foo(int iface) : interface{iface} {}\n"
27667 "}");
27670 TEST_F(FormatTest, PreprocessorOverlappingRegions) {
27671 verifyFormat("#ifdef\n\n"
27672 "#else\n"
27673 "#endif",
27674 "#ifdef \n"
27675 " \n"
27676 "\n"
27677 "#else \n"
27678 "#endif ",
27679 getGoogleStyle());
27682 TEST_F(FormatTest, RemoveParentheses) {
27683 FormatStyle Style = getLLVMStyle();
27684 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
27686 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
27687 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
27688 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
27689 verifyFormat("decltype((foo->bar)) baz;", Style);
27690 verifyFormat("class __declspec(dllimport) X {};",
27691 "class __declspec((dllimport)) X {};", Style);
27692 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
27693 verifyFormat("while (a)\n"
27694 " b;",
27695 "while (((a)))\n"
27696 " b;",
27697 Style);
27698 verifyFormat("while ((a = b))\n"
27699 " c;",
27700 "while (((a = b)))\n"
27701 " c;",
27702 Style);
27703 verifyFormat("if (a)\n"
27704 " b;",
27705 "if (((a)))\n"
27706 " b;",
27707 Style);
27708 verifyFormat("if constexpr ((a = b))\n"
27709 " c;",
27710 "if constexpr (((a = b)))\n"
27711 " c;",
27712 Style);
27713 verifyFormat("if (({ a; }))\n"
27714 " b;",
27715 "if ((({ a; })))\n"
27716 " b;",
27717 Style);
27718 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27719 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27720 Style);
27721 verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
27722 verifyFormat("foo((a, b));", "foo(((a), b));", Style);
27723 verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
27724 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
27725 verifyFormat("return (0);", "return (((0)));", Style);
27726 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
27727 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27728 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27729 Style);
27731 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
27732 verifyFormat("#define Return0 return (0);", Style);
27733 verifyFormat("return 0;", "return (0);", Style);
27734 verifyFormat("co_return 0;", "co_return ((0));", Style);
27735 verifyFormat("return 0;", "return (((0)));", Style);
27736 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
27737 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27738 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27739 Style);
27740 verifyFormat("inline decltype(auto) f() {\n"
27741 " if (a) {\n"
27742 " return (a);\n"
27743 " }\n"
27744 " return (b);\n"
27745 "}",
27746 "inline decltype(auto) f() {\n"
27747 " if (a) {\n"
27748 " return ((a));\n"
27749 " }\n"
27750 " return ((b));\n"
27751 "}",
27752 Style);
27753 verifyFormat("auto g() {\n"
27754 " decltype(auto) x = [] {\n"
27755 " auto y = [] {\n"
27756 " if (a) {\n"
27757 " return a;\n"
27758 " }\n"
27759 " return b;\n"
27760 " };\n"
27761 " if (c) {\n"
27762 " return (c);\n"
27763 " }\n"
27764 " return (d);\n"
27765 " };\n"
27766 " if (e) {\n"
27767 " return e;\n"
27768 " }\n"
27769 " return f;\n"
27770 "}",
27771 "auto g() {\n"
27772 " decltype(auto) x = [] {\n"
27773 " auto y = [] {\n"
27774 " if (a) {\n"
27775 " return ((a));\n"
27776 " }\n"
27777 " return ((b));\n"
27778 " };\n"
27779 " if (c) {\n"
27780 " return ((c));\n"
27781 " }\n"
27782 " return ((d));\n"
27783 " };\n"
27784 " if (e) {\n"
27785 " return ((e));\n"
27786 " }\n"
27787 " return ((f));\n"
27788 "}",
27789 Style);
27791 Style.ColumnLimit = 25;
27792 verifyFormat("return (a + b) - (c + d);",
27793 "return (((a + b)) -\n"
27794 " ((c + d)));",
27795 Style);
27798 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
27799 auto Style = getLLVMStyleWithColumns(35);
27801 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
27802 verifyFormat("void foo(int arg1,\n"
27803 " double arg2) noexcept;",
27804 Style);
27806 // The following line does not fit within the 35 column limit, but that's what
27807 // happens with no break allowed.
27808 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27809 " noexcept(baz(arg1)) &&\n"
27810 " noexcept(baz(arg2)));",
27811 Style);
27813 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27814 Style);
27816 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
27817 verifyFormat("void foo(int arg1,\n"
27818 " double arg2) noexcept;",
27819 Style);
27821 verifyFormat("void bar(int arg1, double arg2)\n"
27822 " noexcept(noexcept(baz(arg1)) &&\n"
27823 " noexcept(baz(arg2)));",
27824 Style);
27826 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27827 " noexcept;",
27828 Style);
27830 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
27831 verifyFormat("void foo(int arg1,\n"
27832 " double arg2) noexcept;",
27833 Style);
27835 verifyFormat("void bar(int arg1, double arg2)\n"
27836 " noexcept(noexcept(baz(arg1)) &&\n"
27837 " noexcept(baz(arg2)));",
27838 Style);
27840 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27841 Style);
27844 TEST_F(FormatTest, PPBranchesInBracedInit) {
27845 verifyFormat("A a_{kFlag1,\n"
27846 "#if BUILD_FLAG\n"
27847 " kFlag2,\n"
27848 "#else\n"
27849 " kFlag3,\n"
27850 "#endif\n"
27851 " kFlag4};",
27852 "A a_{\n"
27853 " kFlag1,\n"
27854 "#if BUILD_FLAG\n"
27855 " kFlag2,\n"
27856 "#else\n"
27857 " kFlag3,\n"
27858 "#endif\n"
27859 " kFlag4\n"
27860 "};");
27863 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
27864 verifyFormat("{\n"
27865 " char *a[] = {\n"
27866 " /* abc */ \"abc\",\n"
27867 "#if FOO\n"
27868 " /* xyz */ \"xyz\",\n"
27869 "#endif\n"
27870 " /* last */ \"last\"};\n"
27871 "}",
27872 getLLVMStyleWithColumns(30));
27875 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
27876 constexpr StringRef Code{
27877 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27879 verifyFormat("return \"Code\"\n"
27880 " \"\\0\\52\\26\\55\\55\\0\"\n"
27881 " \"x013\"\n"
27882 " \"\\02\\xBA\";",
27883 Code);
27885 auto Style = getLLVMStyle();
27886 Style.BreakAdjacentStringLiterals = false;
27887 verifyFormat(Code, Style);
27890 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
27891 verifyFormat(
27892 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
27893 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
27894 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
27896 auto Style = getLLVMStyle();
27897 Style.ColumnLimit = 15;
27898 verifyNoChange("#define test \\\n"
27899 " /* 测试 */ \\\n"
27900 " \"aa\" \\\n"
27901 " \"bb\"",
27902 Style);
27904 Style.ColumnLimit = 25;
27905 verifyFormat("struct foo {\n"
27906 " int iiiiii; ///< iiiiii\n"
27907 " int b; ///< ыыы\n"
27908 " int c; ///< ыыыы\n"
27909 "};",
27910 Style);
27912 Style.ColumnLimit = 35;
27913 verifyFormat("#define SENSOR_DESC_1 \\\n"
27914 " \"{\" \\\n"
27915 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
27916 " \"}\"",
27917 Style);
27919 Style.ColumnLimit = 80;
27920 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27921 verifyFormat("Languages languages = {\n"
27922 " Language{{'e', 'n'}, U\"Test English\" },\n"
27923 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
27924 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
27925 "};",
27926 Style);
27929 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
27930 verifyFormat("return .5;");
27931 verifyFormat("return not '5';");
27932 verifyFormat("return sizeof \"5\";");
27935 TEST_F(FormatTest, BreakBinaryOperations) {
27936 auto Style = getLLVMStyleWithColumns(60);
27937 EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never);
27939 // Logical operations
27940 verifyFormat("if (condition1 && condition2) {\n"
27941 "}",
27942 Style);
27944 verifyFormat("if (condition1 && condition2 &&\n"
27945 " (condition3 || condition4) && condition5 &&\n"
27946 " condition6) {\n"
27947 "}",
27948 Style);
27950 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27951 " loooooooooooooooooooooongcondition2) {\n"
27952 "}",
27953 Style);
27955 // Arithmetic
27956 verifyFormat("const int result = lhs + rhs;", Style);
27958 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
27959 " loooooooooooooooooooooongop3;",
27960 Style);
27962 verifyFormat("result = longOperand1 + longOperand2 -\n"
27963 " (longOperand3 + longOperand4) -\n"
27964 " longOperand5 * longOperand6;",
27965 Style);
27967 verifyFormat("const int result =\n"
27968 " operand1 + operand2 - (operand3 + operand4);",
27969 Style);
27971 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
27973 // Logical operations
27974 verifyFormat("if (condition1 && condition2) {\n"
27975 "}",
27976 Style);
27978 verifyFormat("if (condition1 && // comment\n"
27979 " condition2 &&\n"
27980 " (condition3 || condition4) && // comment\n"
27981 " condition5 &&\n"
27982 " condition6) {\n"
27983 "}",
27984 Style);
27986 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27987 " loooooooooooooooooooooongcondition2) {\n"
27988 "}",
27989 Style);
27991 // Arithmetic
27992 verifyFormat("const int result = lhs + rhs;", Style);
27994 verifyFormat("result = loooooooooooooooooooooongop1 +\n"
27995 " loooooooooooooooooooooongop2 +\n"
27996 " loooooooooooooooooooooongop3;",
27997 Style);
27999 verifyFormat("const int result =\n"
28000 " operand1 + operand2 - (operand3 + operand4);",
28001 Style);
28003 verifyFormat("result = longOperand1 +\n"
28004 " longOperand2 -\n"
28005 " (longOperand3 + longOperand4) -\n"
28006 " longOperand5 +\n"
28007 " longOperand6;",
28008 Style);
28010 verifyFormat("result = operand1 +\n"
28011 " operand2 -\n"
28012 " operand3 +\n"
28013 " operand4 -\n"
28014 " operand5 +\n"
28015 " operand6;",
28016 Style);
28018 // Ensure mixed precedence operations are handled properly
28019 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28021 verifyFormat("result = operand1 +\n"
28022 " operand2 /\n"
28023 " operand3 +\n"
28024 " operand4 /\n"
28025 " operand5 *\n"
28026 " operand6;",
28027 Style);
28029 verifyFormat("result = operand1 *\n"
28030 " operand2 -\n"
28031 " operand3 *\n"
28032 " operand4 -\n"
28033 " operand5 +\n"
28034 " operand6;",
28035 Style);
28037 verifyFormat("result = operand1 *\n"
28038 " (operand2 - operand3 * operand4) -\n"
28039 " operand5 +\n"
28040 " operand6;",
28041 Style);
28043 verifyFormat("result = operand1.member *\n"
28044 " (operand2.member() - operand3->mem * operand4) -\n"
28045 " operand5.member() +\n"
28046 " operand6->member;",
28047 Style);
28049 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28050 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28052 verifyFormat("result = operand1 +\n"
28053 " operand2 / operand3 +\n"
28054 " operand4 / operand5 * operand6;",
28055 Style);
28057 verifyFormat("result = operand1 * operand2 -\n"
28058 " operand3 * operand4 -\n"
28059 " operand5 +\n"
28060 " operand6;",
28061 Style);
28063 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28064 " operand5 +\n"
28065 " operand6;",
28066 Style);
28068 verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28069 " byte_buffer[1] << 8 |\n"
28070 " byte_buffer[2] << 16 |\n"
28071 " byte_buffer[3] << 24;",
28072 Style);
28074 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28075 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
28077 // Logical operations
28078 verifyFormat("if (condition1 && condition2) {\n"
28079 "}",
28080 Style);
28082 verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28083 " && loooooooooooooooooooooongcondition2) {\n"
28084 "}",
28085 Style);
28087 // Arithmetic
28088 verifyFormat("const int result = lhs + rhs;", Style);
28090 verifyFormat("result = loooooooooooooooooooooongop1\n"
28091 " + loooooooooooooooooooooongop2\n"
28092 " + loooooooooooooooooooooongop3;",
28093 Style);
28095 verifyFormat("const int result =\n"
28096 " operand1 + operand2 - (operand3 + operand4);",
28097 Style);
28099 verifyFormat("result = longOperand1\n"
28100 " + longOperand2\n"
28101 " - (longOperand3 + longOperand4)\n"
28102 " - longOperand5\n"
28103 " + longOperand6;",
28104 Style);
28106 verifyFormat("result = operand1\n"
28107 " + operand2\n"
28108 " - operand3\n"
28109 " + operand4\n"
28110 " - operand5\n"
28111 " + operand6;",
28112 Style);
28114 // Ensure mixed precedence operations are handled properly
28115 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28117 verifyFormat("result = operand1\n"
28118 " + operand2\n"
28119 " / operand3\n"
28120 " + operand4\n"
28121 " / operand5\n"
28122 " * operand6;",
28123 Style);
28125 verifyFormat("result = operand1\n"
28126 " * operand2\n"
28127 " - operand3\n"
28128 " * operand4\n"
28129 " - operand5\n"
28130 " + operand6;",
28131 Style);
28133 verifyFormat("result = operand1\n"
28134 " * (operand2 - operand3 * operand4)\n"
28135 " - operand5\n"
28136 " + operand6;",
28137 Style);
28139 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28140 " | byte_buffer[1]\n"
28141 " << 8\n"
28142 " | byte_buffer[2]\n"
28143 " << 16\n"
28144 " | byte_buffer[3]\n"
28145 " << 24;",
28146 Style);
28148 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28149 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28151 verifyFormat("result = operand1\n"
28152 " + operand2 / operand3\n"
28153 " + operand4 / operand5 * operand6;",
28154 Style);
28156 verifyFormat("result = operand1 * operand2\n"
28157 " - operand3 * operand4\n"
28158 " - operand5\n"
28159 " + operand6;",
28160 Style);
28162 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
28163 " - operand5\n"
28164 " + operand6;",
28165 Style);
28167 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28168 " | byte_buffer[1] << 8\n"
28169 " | byte_buffer[2] << 16\n"
28170 " | byte_buffer[3] << 24;",
28171 Style);
28174 TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {
28175 auto Style = getLLVMStyle();
28176 Style.RemoveEmptyLinesInUnwrappedLines = true;
28178 verifyFormat("int c = a + b;",
28179 "int c\n"
28180 "\n"
28181 " = a + b;",
28182 Style);
28184 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
28185 "enum : unsigned\n"
28186 "\n"
28187 "{\n"
28188 " AA = 0,\n"
28189 " BB\n"
28190 "} myEnum;",
28191 Style);
28193 verifyFormat("class B : public E {\n"
28194 "private:\n"
28195 "};",
28196 "class B : public E\n"
28197 "\n"
28198 "{\n"
28199 "private:\n"
28200 "};",
28201 Style);
28203 verifyFormat(
28204 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
28205 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
28206 "\n"
28207 " 23, \"hello\"},\n"
28208 " {7, 5, \"!!\"}};",
28209 Style);
28211 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
28212 "int myFunction(\n"
28213 "\n"
28214 " int aaaaaaaaaaaaa,\n"
28215 "\n"
28216 " int ccccccccccccc, int d);",
28217 Style);
28219 verifyFormat("switch (e) {\n"
28220 "case 1:\n"
28221 " return e;\n"
28222 "case 2:\n"
28223 " return 2;\n"
28224 "}",
28225 "switch (\n"
28226 "\n"
28227 " e) {\n"
28228 "case 1:\n"
28229 " return e;\n"
28230 "case 2:\n"
28231 " return 2;\n"
28232 "}",
28233 Style);
28235 verifyFormat("while (true) {\n"
28236 "}",
28237 "while (\n"
28238 "\n"
28239 " true) {\n"
28240 "}",
28241 Style);
28243 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
28244 " std::map<int, std::string> *outputMap);",
28245 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
28246 "\n"
28247 " (std::map<int, std::string> *outputMap);",
28248 Style);
28251 TEST_F(FormatTest, KeepFormFeed) {
28252 auto Style = getLLVMStyle();
28253 Style.KeepFormFeed = true;
28255 constexpr StringRef NoFormFeed{"int i;\n"
28256 "\n"
28257 "void f();"};
28258 verifyFormat(NoFormFeed,
28259 "int i;\n"
28260 " \f\n"
28261 "void f();",
28262 Style);
28263 verifyFormat(NoFormFeed,
28264 "int i;\n"
28265 "\n"
28266 "\fvoid f();",
28267 Style);
28268 verifyFormat(NoFormFeed,
28269 "\fint i;\n"
28270 "\n"
28271 "void f();",
28272 Style);
28273 verifyFormat(NoFormFeed,
28274 "int i;\n"
28275 "\n"
28276 "void f();\f",
28277 Style);
28279 constexpr StringRef FormFeed{"int i;\n"
28280 "\f\n"
28281 "void f();"};
28282 verifyNoChange(FormFeed, Style);
28284 Style.LineEnding = FormatStyle::LE_LF;
28285 verifyFormat(FormFeed,
28286 "int i;\r\n"
28287 "\f\r\n"
28288 "void f();",
28289 Style);
28291 constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n"
28292 "\f\n"
28293 "\n"
28294 "void f();"};
28295 Style.MaxEmptyLinesToKeep = 2;
28296 verifyFormat(FormFeedBeforeEmptyLine,
28297 "int i;\n"
28298 "\n"
28299 "\f\n"
28300 "void f();",
28301 Style);
28302 verifyFormat(FormFeedBeforeEmptyLine,
28303 "int i;\n"
28304 "\f\n"
28305 "\f\n"
28306 "void f();",
28307 Style);
28310 } // namespace
28311 } // namespace test
28312 } // namespace format
28313 } // namespace clang