[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / clang / unittests / Format / FormatTest.cpp
blob250e51b5421664d04db342a3a34a7be9c67ec749
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));
5729 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5730 verifyFormat("#define A (x)");
5731 verifyFormat("#define A(x)");
5733 FormatStyle Style = getLLVMStyle();
5734 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5735 verifyFormat("#define true ((foo)1)", Style);
5736 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5737 verifyFormat("#define false((foo)0)", Style);
5740 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5741 verifyFormat("#define A b;",
5742 "#define A \\\n"
5743 " \\\n"
5744 " b;",
5745 getLLVMStyleWithColumns(25));
5746 verifyNoChange("#define A \\\n"
5747 " \\\n"
5748 " a; \\\n"
5749 " b;",
5750 getLLVMStyleWithColumns(11));
5751 verifyNoChange("#define A \\\n"
5752 " a; \\\n"
5753 " \\\n"
5754 " b;",
5755 getLLVMStyleWithColumns(11));
5758 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5759 verifyIncompleteFormat("#define A :");
5760 verifyFormat("#define SOMECASES \\\n"
5761 " case 1: \\\n"
5762 " case 2",
5763 getLLVMStyleWithColumns(20));
5764 verifyFormat("#define MACRO(a) \\\n"
5765 " if (a) \\\n"
5766 " f(); \\\n"
5767 " else \\\n"
5768 " g()",
5769 getLLVMStyleWithColumns(18));
5770 verifyFormat("#define A template <typename T>");
5771 verifyIncompleteFormat("#define STR(x) #x\n"
5772 "f(STR(this_is_a_string_literal{));");
5773 verifyFormat("#pragma omp threadprivate( \\\n"
5774 " y)), // expected-warning",
5775 getLLVMStyleWithColumns(28));
5776 verifyFormat("#d, = };");
5777 verifyFormat("#if \"a");
5778 verifyIncompleteFormat("({\n"
5779 "#define b \\\n"
5780 " } \\\n"
5781 " a\n"
5782 "a",
5783 getLLVMStyleWithColumns(15));
5784 verifyFormat("#define A \\\n"
5785 " { \\\n"
5786 " {\n"
5787 "#define B \\\n"
5788 " } \\\n"
5789 " }",
5790 getLLVMStyleWithColumns(15));
5791 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5792 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5793 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5794 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5795 verifyNoCrash("#else\n"
5796 "#else\n"
5797 "#endif\n"
5798 "#endif");
5799 verifyNoCrash("#else\n"
5800 "#if X\n"
5801 "#endif\n"
5802 "#endif");
5803 verifyNoCrash("#else\n"
5804 "#endif\n"
5805 "#if X\n"
5806 "#endif");
5807 verifyNoCrash("#if X\n"
5808 "#else\n"
5809 "#else\n"
5810 "#endif\n"
5811 "#endif");
5812 verifyNoCrash("#if X\n"
5813 "#elif Y\n"
5814 "#elif Y\n"
5815 "#endif\n"
5816 "#endif");
5817 verifyNoCrash("#endif\n"
5818 "#endif");
5819 verifyNoCrash("#endif\n"
5820 "#else");
5821 verifyNoCrash("#endif\n"
5822 "#elif Y");
5825 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5826 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5827 verifyFormat("class A : public QObject {\n"
5828 " Q_OBJECT\n"
5829 "\n"
5830 " A() {}\n"
5831 "};",
5832 "class A : public QObject {\n"
5833 " Q_OBJECT\n"
5834 "\n"
5835 " A() {\n}\n"
5836 "} ;");
5837 verifyFormat("MACRO\n"
5838 "/*static*/ int i;",
5839 "MACRO\n"
5840 " /*static*/ int i;");
5841 verifyFormat("SOME_MACRO\n"
5842 "namespace {\n"
5843 "void f();\n"
5844 "} // namespace",
5845 "SOME_MACRO\n"
5846 " namespace {\n"
5847 "void f( );\n"
5848 "} // namespace");
5849 // Only if the identifier contains at least 5 characters.
5850 verifyFormat("HTTP f();", "HTTP\nf();");
5851 verifyNoChange("MACRO\nf();");
5852 // Only if everything is upper case.
5853 verifyFormat("class A : public QObject {\n"
5854 " Q_Object A() {}\n"
5855 "};",
5856 "class A : public QObject {\n"
5857 " Q_Object\n"
5858 " A() {\n}\n"
5859 "} ;");
5861 // Only if the next line can actually start an unwrapped line.
5862 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5863 "<< SomeThing;");
5865 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5866 "(n, buffers))",
5867 getChromiumStyle(FormatStyle::LK_Cpp));
5869 // See PR41483
5870 verifyNoChange("/**/ FOO(a)\n"
5871 "FOO(b)");
5874 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5875 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5876 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5877 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5878 "class X {};\n"
5879 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5880 "int *createScopDetectionPass() { return 0; }",
5881 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5882 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5883 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5884 " class X {};\n"
5885 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5886 " int *createScopDetectionPass() { return 0; }");
5887 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5888 // braces, so that inner block is indented one level more.
5889 verifyFormat("int q() {\n"
5890 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5891 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5892 " IPC_END_MESSAGE_MAP()\n"
5893 "}",
5894 "int q() {\n"
5895 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5896 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5897 " IPC_END_MESSAGE_MAP()\n"
5898 "}");
5900 // Same inside macros.
5901 verifyFormat("#define LIST(L) \\\n"
5902 " L(A) \\\n"
5903 " L(B) \\\n"
5904 " L(C)",
5905 "#define LIST(L) \\\n"
5906 " L(A) \\\n"
5907 " L(B) \\\n"
5908 " L(C)",
5909 getGoogleStyle());
5911 // These must not be recognized as macros.
5912 verifyFormat("int q() {\n"
5913 " f(x);\n"
5914 " f(x) {}\n"
5915 " f(x)->g();\n"
5916 " f(x)->*g();\n"
5917 " f(x).g();\n"
5918 " f(x) = x;\n"
5919 " f(x) += x;\n"
5920 " f(x) -= x;\n"
5921 " f(x) *= x;\n"
5922 " f(x) /= x;\n"
5923 " f(x) %= x;\n"
5924 " f(x) &= x;\n"
5925 " f(x) |= x;\n"
5926 " f(x) ^= x;\n"
5927 " f(x) >>= x;\n"
5928 " f(x) <<= x;\n"
5929 " f(x)[y].z();\n"
5930 " LOG(INFO) << x;\n"
5931 " ifstream(x) >> x;\n"
5932 "}",
5933 "int q() {\n"
5934 " f(x)\n;\n"
5935 " f(x)\n {}\n"
5936 " f(x)\n->g();\n"
5937 " f(x)\n->*g();\n"
5938 " f(x)\n.g();\n"
5939 " f(x)\n = x;\n"
5940 " f(x)\n += x;\n"
5941 " f(x)\n -= x;\n"
5942 " f(x)\n *= x;\n"
5943 " f(x)\n /= x;\n"
5944 " f(x)\n %= x;\n"
5945 " f(x)\n &= x;\n"
5946 " f(x)\n |= x;\n"
5947 " f(x)\n ^= x;\n"
5948 " f(x)\n >>= x;\n"
5949 " f(x)\n <<= x;\n"
5950 " f(x)\n[y].z();\n"
5951 " LOG(INFO)\n << x;\n"
5952 " ifstream(x)\n >> x;\n"
5953 "}");
5954 verifyFormat("int q() {\n"
5955 " F(x)\n"
5956 " if (1) {\n"
5957 " }\n"
5958 " F(x)\n"
5959 " while (1) {\n"
5960 " }\n"
5961 " F(x)\n"
5962 " G(x);\n"
5963 " F(x)\n"
5964 " try {\n"
5965 " Q();\n"
5966 " } catch (...) {\n"
5967 " }\n"
5968 "}",
5969 "int q() {\n"
5970 "F(x)\n"
5971 "if (1) {}\n"
5972 "F(x)\n"
5973 "while (1) {}\n"
5974 "F(x)\n"
5975 "G(x);\n"
5976 "F(x)\n"
5977 "try { Q(); } catch (...) {}\n"
5978 "}");
5979 verifyFormat("class A {\n"
5980 " A() : t(0) {}\n"
5981 " A(int i) noexcept() : {}\n"
5982 " A(X x)\n" // FIXME: function-level try blocks are broken.
5983 " try : t(0) {\n"
5984 " } catch (...) {\n"
5985 " }\n"
5986 "};",
5987 "class A {\n"
5988 " A()\n : t(0) {}\n"
5989 " A(int i)\n noexcept() : {}\n"
5990 " A(X x)\n"
5991 " try : t(0) {} catch (...) {}\n"
5992 "};");
5993 FormatStyle Style = getLLVMStyle();
5994 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5995 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5996 Style.BraceWrapping.AfterFunction = true;
5997 verifyFormat("void f()\n"
5998 "try\n"
5999 "{\n"
6000 "}",
6001 "void f() try {\n"
6002 "}",
6003 Style);
6004 verifyFormat("class SomeClass {\n"
6005 "public:\n"
6006 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6007 "};",
6008 "class SomeClass {\n"
6009 "public:\n"
6010 " SomeClass()\n"
6011 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6012 "};");
6013 verifyFormat("class SomeClass {\n"
6014 "public:\n"
6015 " SomeClass()\n"
6016 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6017 "};",
6018 "class SomeClass {\n"
6019 "public:\n"
6020 " SomeClass()\n"
6021 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6022 "};",
6023 getLLVMStyleWithColumns(40));
6025 verifyFormat("MACRO(>)");
6027 // Some macros contain an implicit semicolon.
6028 Style = getLLVMStyle();
6029 Style.StatementMacros.push_back("FOO");
6030 verifyFormat("FOO(a) int b = 0;");
6031 verifyFormat("FOO(a)\n"
6032 "int b = 0;",
6033 Style);
6034 verifyFormat("FOO(a);\n"
6035 "int b = 0;",
6036 Style);
6037 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6038 "int b = 0;",
6039 Style);
6040 verifyFormat("FOO()\n"
6041 "int b = 0;",
6042 Style);
6043 verifyFormat("FOO\n"
6044 "int b = 0;",
6045 Style);
6046 verifyFormat("void f() {\n"
6047 " FOO(a)\n"
6048 " return a;\n"
6049 "}",
6050 Style);
6051 verifyFormat("FOO(a)\n"
6052 "FOO(b)",
6053 Style);
6054 verifyFormat("int a = 0;\n"
6055 "FOO(b)\n"
6056 "int c = 0;",
6057 Style);
6058 verifyFormat("int a = 0;\n"
6059 "int x = FOO(a)\n"
6060 "int b = 0;",
6061 Style);
6062 verifyFormat("void foo(int a) { FOO(a) }\n"
6063 "uint32_t bar() {}",
6064 Style);
6067 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6068 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
6070 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6071 ZeroColumn);
6074 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6075 verifyFormat("#define A \\\n"
6076 " f({ \\\n"
6077 " g(); \\\n"
6078 " });",
6079 getLLVMStyleWithColumns(11));
6082 TEST_F(FormatTest, IndentPreprocessorDirectives) {
6083 FormatStyle Style = getLLVMStyleWithColumns(40);
6084 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6085 verifyFormat("#ifdef _WIN32\n"
6086 "#define A 0\n"
6087 "#ifdef VAR2\n"
6088 "#define B 1\n"
6089 "#include <someheader.h>\n"
6090 "#define MACRO \\\n"
6091 " some_very_long_func_aaaaaaaaaa();\n"
6092 "#endif\n"
6093 "#else\n"
6094 "#define A 1\n"
6095 "#endif",
6096 Style);
6097 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6098 verifyFormat("#if 1\n"
6099 "# define __STR(x) #x\n"
6100 "#endif",
6101 Style);
6102 verifyFormat("#ifdef _WIN32\n"
6103 "# define A 0\n"
6104 "# ifdef VAR2\n"
6105 "# define B 1\n"
6106 "# include <someheader.h>\n"
6107 "# define MACRO \\\n"
6108 " some_very_long_func_aaaaaaaaaa();\n"
6109 "# endif\n"
6110 "#else\n"
6111 "# define A 1\n"
6112 "#endif",
6113 Style);
6114 verifyFormat("#if A\n"
6115 "# define MACRO \\\n"
6116 " void a(int x) { \\\n"
6117 " b(); \\\n"
6118 " c(); \\\n"
6119 " d(); \\\n"
6120 " e(); \\\n"
6121 " f(); \\\n"
6122 " }\n"
6123 "#endif",
6124 Style);
6125 // Comments before include guard.
6126 verifyFormat("// file comment\n"
6127 "// file comment\n"
6128 "#ifndef HEADER_H\n"
6129 "#define HEADER_H\n"
6130 "code();\n"
6131 "#endif",
6132 Style);
6133 // Test with include guards.
6134 verifyFormat("#ifndef HEADER_H\n"
6135 "#define HEADER_H\n"
6136 "code();\n"
6137 "#endif",
6138 Style);
6139 // Include guards must have a #define with the same variable immediately
6140 // after #ifndef.
6141 verifyFormat("#ifndef NOT_GUARD\n"
6142 "# define FOO\n"
6143 "code();\n"
6144 "#endif",
6145 Style);
6147 // Include guards must cover the entire file.
6148 verifyFormat("code();\n"
6149 "code();\n"
6150 "#ifndef NOT_GUARD\n"
6151 "# define NOT_GUARD\n"
6152 "code();\n"
6153 "#endif",
6154 Style);
6155 verifyFormat("#ifndef NOT_GUARD\n"
6156 "# define NOT_GUARD\n"
6157 "code();\n"
6158 "#endif\n"
6159 "code();",
6160 Style);
6161 // Test with trailing blank lines.
6162 verifyFormat("#ifndef HEADER_H\n"
6163 "#define HEADER_H\n"
6164 "code();\n"
6165 "#endif",
6166 Style);
6167 // Include guards don't have #else.
6168 verifyFormat("#ifndef NOT_GUARD\n"
6169 "# define NOT_GUARD\n"
6170 "code();\n"
6171 "#else\n"
6172 "#endif",
6173 Style);
6174 verifyFormat("#ifndef NOT_GUARD\n"
6175 "# define NOT_GUARD\n"
6176 "code();\n"
6177 "#elif FOO\n"
6178 "#endif",
6179 Style);
6180 // Non-identifier #define after potential include guard.
6181 verifyFormat("#ifndef FOO\n"
6182 "# define 1\n"
6183 "#endif",
6184 Style);
6185 // #if closes past last non-preprocessor line.
6186 verifyFormat("#ifndef FOO\n"
6187 "#define FOO\n"
6188 "#if 1\n"
6189 "int i;\n"
6190 "# define A 0\n"
6191 "#endif\n"
6192 "#endif",
6193 Style);
6194 // Don't crash if there is an #elif directive without a condition.
6195 verifyFormat("#if 1\n"
6196 "int x;\n"
6197 "#elif\n"
6198 "int y;\n"
6199 "#else\n"
6200 "int z;\n"
6201 "#endif",
6202 Style);
6203 // FIXME: This doesn't handle the case where there's code between the
6204 // #ifndef and #define but all other conditions hold. This is because when
6205 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6206 // previous code line yet, so we can't detect it.
6207 verifyFormat("#ifndef NOT_GUARD\n"
6208 "code();\n"
6209 "#define NOT_GUARD\n"
6210 "code();\n"
6211 "#endif",
6212 "#ifndef NOT_GUARD\n"
6213 "code();\n"
6214 "# define NOT_GUARD\n"
6215 "code();\n"
6216 "#endif",
6217 Style);
6218 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6219 // be outside an include guard. Examples are #pragma once and
6220 // #pragma GCC diagnostic, or anything else that does not change the meaning
6221 // of the file if it's included multiple times.
6222 verifyFormat("#ifdef WIN32\n"
6223 "# pragma once\n"
6224 "#endif\n"
6225 "#ifndef HEADER_H\n"
6226 "# define HEADER_H\n"
6227 "code();\n"
6228 "#endif",
6229 "#ifdef WIN32\n"
6230 "# pragma once\n"
6231 "#endif\n"
6232 "#ifndef HEADER_H\n"
6233 "#define HEADER_H\n"
6234 "code();\n"
6235 "#endif",
6236 Style);
6237 // FIXME: This does not detect when there is a single non-preprocessor line
6238 // in front of an include-guard-like structure where other conditions hold
6239 // because ScopedLineState hides the line.
6240 verifyFormat("code();\n"
6241 "#ifndef HEADER_H\n"
6242 "#define HEADER_H\n"
6243 "code();\n"
6244 "#endif",
6245 "code();\n"
6246 "#ifndef HEADER_H\n"
6247 "# define HEADER_H\n"
6248 "code();\n"
6249 "#endif",
6250 Style);
6251 // Keep comments aligned with #, otherwise indent comments normally. These
6252 // tests cannot use verifyFormat because messUp manipulates leading
6253 // whitespace.
6255 const char *Expected = ""
6256 "void f() {\n"
6257 "#if 1\n"
6258 "// Preprocessor aligned.\n"
6259 "# define A 0\n"
6260 " // Code. Separated by blank line.\n"
6261 "\n"
6262 "# define B 0\n"
6263 " // Code. Not aligned with #\n"
6264 "# define C 0\n"
6265 "#endif";
6266 const char *ToFormat = ""
6267 "void f() {\n"
6268 "#if 1\n"
6269 "// Preprocessor aligned.\n"
6270 "# define A 0\n"
6271 "// Code. Separated by blank line.\n"
6272 "\n"
6273 "# define B 0\n"
6274 " // Code. Not aligned with #\n"
6275 "# define C 0\n"
6276 "#endif";
6277 verifyFormat(Expected, ToFormat, Style);
6278 verifyNoChange(Expected, Style);
6280 // Keep block quotes aligned.
6282 const char *Expected = ""
6283 "void f() {\n"
6284 "#if 1\n"
6285 "/* Preprocessor aligned. */\n"
6286 "# define A 0\n"
6287 " /* Code. Separated by blank line. */\n"
6288 "\n"
6289 "# define B 0\n"
6290 " /* Code. Not aligned with # */\n"
6291 "# define C 0\n"
6292 "#endif";
6293 const char *ToFormat = ""
6294 "void f() {\n"
6295 "#if 1\n"
6296 "/* Preprocessor aligned. */\n"
6297 "# define A 0\n"
6298 "/* Code. Separated by blank line. */\n"
6299 "\n"
6300 "# define B 0\n"
6301 " /* Code. Not aligned with # */\n"
6302 "# define C 0\n"
6303 "#endif";
6304 verifyFormat(Expected, ToFormat, Style);
6305 verifyNoChange(Expected, Style);
6307 // Keep comments aligned with un-indented directives.
6309 const char *Expected = ""
6310 "void f() {\n"
6311 "// Preprocessor aligned.\n"
6312 "#define A 0\n"
6313 " // Code. Separated by blank line.\n"
6314 "\n"
6315 "#define B 0\n"
6316 " // Code. Not aligned with #\n"
6317 "#define C 0\n";
6318 const char *ToFormat = ""
6319 "void f() {\n"
6320 "// Preprocessor aligned.\n"
6321 "#define A 0\n"
6322 "// Code. Separated by blank line.\n"
6323 "\n"
6324 "#define B 0\n"
6325 " // Code. Not aligned with #\n"
6326 "#define C 0\n";
6327 verifyFormat(Expected, ToFormat, Style);
6328 verifyNoChange(Expected, Style);
6330 // Test AfterHash with tabs.
6332 FormatStyle Tabbed = Style;
6333 Tabbed.UseTab = FormatStyle::UT_Always;
6334 Tabbed.IndentWidth = 8;
6335 Tabbed.TabWidth = 8;
6336 verifyFormat("#ifdef _WIN32\n"
6337 "#\tdefine A 0\n"
6338 "#\tifdef VAR2\n"
6339 "#\t\tdefine B 1\n"
6340 "#\t\tinclude <someheader.h>\n"
6341 "#\t\tdefine MACRO \\\n"
6342 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6343 "#\tendif\n"
6344 "#else\n"
6345 "#\tdefine A 1\n"
6346 "#endif",
6347 Tabbed);
6350 // Regression test: Multiline-macro inside include guards.
6351 verifyFormat("#ifndef HEADER_H\n"
6352 "#define HEADER_H\n"
6353 "#define A() \\\n"
6354 " int i; \\\n"
6355 " int j;\n"
6356 "#endif // HEADER_H",
6357 getLLVMStyleWithColumns(20));
6359 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6360 // Basic before hash indent tests
6361 verifyFormat("#ifdef _WIN32\n"
6362 " #define A 0\n"
6363 " #ifdef VAR2\n"
6364 " #define B 1\n"
6365 " #include <someheader.h>\n"
6366 " #define MACRO \\\n"
6367 " some_very_long_func_aaaaaaaaaa();\n"
6368 " #endif\n"
6369 "#else\n"
6370 " #define A 1\n"
6371 "#endif",
6372 Style);
6373 verifyFormat("#if A\n"
6374 " #define MACRO \\\n"
6375 " void a(int x) { \\\n"
6376 " b(); \\\n"
6377 " c(); \\\n"
6378 " d(); \\\n"
6379 " e(); \\\n"
6380 " f(); \\\n"
6381 " }\n"
6382 "#endif",
6383 Style);
6384 // Keep comments aligned with indented directives. These
6385 // tests cannot use verifyFormat because messUp manipulates leading
6386 // whitespace.
6388 const char *Expected = "void f() {\n"
6389 "// Aligned to preprocessor.\n"
6390 "#if 1\n"
6391 " // Aligned to code.\n"
6392 " int a;\n"
6393 " #if 1\n"
6394 " // Aligned to preprocessor.\n"
6395 " #define A 0\n"
6396 " // Aligned to code.\n"
6397 " int b;\n"
6398 " #endif\n"
6399 "#endif\n"
6400 "}";
6401 const char *ToFormat = "void f() {\n"
6402 "// Aligned to preprocessor.\n"
6403 "#if 1\n"
6404 "// Aligned to code.\n"
6405 "int a;\n"
6406 "#if 1\n"
6407 "// Aligned to preprocessor.\n"
6408 "#define A 0\n"
6409 "// Aligned to code.\n"
6410 "int b;\n"
6411 "#endif\n"
6412 "#endif\n"
6413 "}";
6414 verifyFormat(Expected, ToFormat, Style);
6415 verifyNoChange(Expected, Style);
6418 const char *Expected = "void f() {\n"
6419 "/* Aligned to preprocessor. */\n"
6420 "#if 1\n"
6421 " /* Aligned to code. */\n"
6422 " int a;\n"
6423 " #if 1\n"
6424 " /* Aligned to preprocessor. */\n"
6425 " #define A 0\n"
6426 " /* Aligned to code. */\n"
6427 " int b;\n"
6428 " #endif\n"
6429 "#endif\n"
6430 "}";
6431 const char *ToFormat = "void f() {\n"
6432 "/* Aligned to preprocessor. */\n"
6433 "#if 1\n"
6434 "/* Aligned to code. */\n"
6435 "int a;\n"
6436 "#if 1\n"
6437 "/* Aligned to preprocessor. */\n"
6438 "#define A 0\n"
6439 "/* Aligned to code. */\n"
6440 "int b;\n"
6441 "#endif\n"
6442 "#endif\n"
6443 "}";
6444 verifyFormat(Expected, ToFormat, Style);
6445 verifyNoChange(Expected, Style);
6448 // Test single comment before preprocessor
6449 verifyFormat("// Comment\n"
6450 "\n"
6451 "#if 1\n"
6452 "#endif",
6453 Style);
6456 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6457 FormatStyle Style = getLLVMStyle();
6458 Style.AlignConsecutiveAssignments.Enabled = true;
6459 Style.AlignConsecutiveDeclarations.Enabled = true;
6461 // Test with just #if blocks.
6462 verifyFormat("void f1() {\n"
6463 "#if 1\n"
6464 " int foo = 1;\n"
6465 " int foobar = 2;\n"
6466 "#endif\n"
6467 "}\n"
6468 "#if 1\n"
6469 "int baz = 3;\n"
6470 "#endif\n"
6471 "void f2() {\n"
6472 "#if 1\n"
6473 " char *foobarbaz = \"foobarbaz\";\n"
6474 " int quux = 4;\n"
6475 "}",
6476 Style);
6478 // Test with just #else blocks.
6479 verifyFormat("void f1() {\n"
6480 "#if 1\n"
6481 "#else\n"
6482 " int foo = 1;\n"
6483 " int foobar = 2;\n"
6484 "#endif\n"
6485 "}\n"
6486 "#if 1\n"
6487 "#else\n"
6488 "int baz = 3;\n"
6489 "#endif\n"
6490 "void f2() {\n"
6491 "#if 1\n"
6492 "#else\n"
6493 " char *foobarbaz = \"foobarbaz\";\n"
6494 " int quux = 4;\n"
6495 "}",
6496 Style);
6497 verifyFormat("auto foo = [] { return; };\n"
6498 "#if FOO\n"
6499 "#else\n"
6500 "count = bar;\n"
6501 "mbid = bid;\n"
6502 "#endif",
6503 Style);
6505 // Test with a mix of #if and #else blocks.
6506 verifyFormat("void f1() {\n"
6507 "#if 1\n"
6508 "#else\n"
6509 " int foo = 1;\n"
6510 " int foobar = 2;\n"
6511 "#endif\n"
6512 "}\n"
6513 "#if 1\n"
6514 "int baz = 3;\n"
6515 "#endif\n"
6516 "void f2() {\n"
6517 "#if 1\n"
6518 "#else\n"
6519 " // prevent alignment with #else in f1\n"
6520 " char *foobarbaz = \"foobarbaz\";\n"
6521 " int quux = 4;\n"
6522 "}",
6523 Style);
6525 // Test with nested #if and #else blocks.
6526 verifyFormat("void f1() {\n"
6527 "#if 1\n"
6528 "#else\n"
6529 "#if 2\n"
6530 "#else\n"
6531 " int foo = 1;\n"
6532 " int foobar = 2;\n"
6533 "#endif\n"
6534 "#endif\n"
6535 "}\n"
6536 "#if 1\n"
6537 "#else\n"
6538 "#if 2\n"
6539 "int baz = 3;\n"
6540 "#endif\n"
6541 "#endif\n"
6542 "void f2() {\n"
6543 "#if 1\n"
6544 "#if 2\n"
6545 "#else\n"
6546 " // prevent alignment with #else in f1\n"
6547 " char *foobarbaz = \"foobarbaz\";\n"
6548 " int quux = 4;\n"
6549 "#endif\n"
6550 "#endif\n"
6551 "}",
6552 Style);
6554 verifyFormat("#if FOO\n"
6555 "int a = 1;\n"
6556 "#else\n"
6557 "int ab = 2;\n"
6558 "#endif\n"
6559 "#ifdef BAR\n"
6560 "int abc = 3;\n"
6561 "#elifdef BAZ\n"
6562 "int abcd = 4;\n"
6563 "#endif",
6564 Style);
6566 verifyFormat("void f() {\n"
6567 " if (foo) {\n"
6568 "#if FOO\n"
6569 " int a = 1;\n"
6570 "#else\n"
6571 " bool a = true;\n"
6572 "#endif\n"
6573 " int abc = 3;\n"
6574 "#ifndef BAR\n"
6575 " int abcd = 4;\n"
6576 "#elif BAZ\n"
6577 " bool abcd = true;\n"
6578 "#endif\n"
6579 " }\n"
6580 "}",
6581 Style);
6583 verifyFormat("void f() {\n"
6584 "#if FOO\n"
6585 " a = 1;\n"
6586 "#else\n"
6587 " ab = 2;\n"
6588 "#endif\n"
6589 "}\n"
6590 "void g() {\n"
6591 "#if BAR\n"
6592 " abc = 3;\n"
6593 "#elifndef BAZ\n"
6594 " abcd = 4;\n"
6595 "#endif\n"
6596 "}",
6597 Style);
6600 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6601 verifyFormat("{\n"
6602 " {\n"
6603 " a #c;\n"
6604 " }\n"
6605 "}");
6608 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6609 verifyFormat("#define A \\\n { \\\n {\nint i;",
6610 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6611 verifyFormat("#define A \\\n } \\\n }\nint i;",
6612 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6615 TEST_F(FormatTest, EscapedNewlines) {
6616 FormatStyle Narrow = getLLVMStyleWithColumns(11);
6617 verifyFormat("#define A \\\n int i; \\\n int j;",
6618 "#define A \\\nint i;\\\n int j;", Narrow);
6619 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6620 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6621 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6622 verifyNoChange("<a\n\\\\\n>");
6624 FormatStyle AlignLeft = getLLVMStyle();
6625 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6626 verifyFormat("#define MACRO(x) \\\n"
6627 "private: \\\n"
6628 " int x(int a);",
6629 AlignLeft);
6631 // CRLF line endings
6632 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6633 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6634 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6635 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6636 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6637 verifyNoChange("<a\r\n\\\\\r\n>");
6638 verifyFormat("#define MACRO(x) \\\r\n"
6639 "private: \\\r\n"
6640 " int x(int a);",
6641 AlignLeft);
6643 constexpr StringRef Code{"#define A \\\n"
6644 " int a123; \\\n"
6645 " int a; \\\n"
6646 " int a1234;"};
6647 verifyFormat(Code, AlignLeft);
6649 constexpr StringRef Code2{"#define A \\\n"
6650 " int a123; \\\n"
6651 " int a; \\\n"
6652 " int a1234;"};
6653 auto LastLine = getLLVMStyle();
6654 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6655 verifyFormat(Code2, LastLine);
6657 LastLine.ColumnLimit = 13;
6658 verifyFormat(Code, LastLine);
6660 LastLine.ColumnLimit = 0;
6661 verifyFormat(Code2, LastLine);
6663 FormatStyle DontAlign = getLLVMStyle();
6664 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6665 DontAlign.MaxEmptyLinesToKeep = 3;
6666 // FIXME: can't use verifyFormat here because the newline before
6667 // "public:" is not inserted the first time it's reformatted
6668 verifyNoChange("#define A \\\n"
6669 " class Foo { \\\n"
6670 " void bar(); \\\n"
6671 "\\\n"
6672 "\\\n"
6673 "\\\n"
6674 " public: \\\n"
6675 " void baz(); \\\n"
6676 " };",
6677 DontAlign);
6680 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6681 verifyFormat("#define A \\\n"
6682 " int v( \\\n"
6683 " a); \\\n"
6684 " int i;",
6685 getLLVMStyleWithColumns(11));
6688 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6689 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6690 " \\\n"
6691 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6692 "\n"
6693 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6694 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6695 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6696 "\\\n"
6697 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6698 " \n"
6699 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6700 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6703 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6704 verifyFormat("int\n"
6705 "#define A\n"
6706 " a;",
6707 "int\n#define A\na;");
6708 verifyFormat("functionCallTo(\n"
6709 " someOtherFunction(\n"
6710 " withSomeParameters, whichInSequence,\n"
6711 " areLongerThanALine(andAnotherCall,\n"
6712 "#define A B\n"
6713 " withMoreParamters,\n"
6714 " whichStronglyInfluenceTheLayout),\n"
6715 " andMoreParameters),\n"
6716 " trailing);",
6717 getLLVMStyleWithColumns(69));
6718 verifyFormat("Foo::Foo()\n"
6719 "#ifdef BAR\n"
6720 " : baz(0)\n"
6721 "#endif\n"
6722 "{\n"
6723 "}");
6724 verifyFormat("void f() {\n"
6725 " if (true)\n"
6726 "#ifdef A\n"
6727 " f(42);\n"
6728 " x();\n"
6729 "#else\n"
6730 " g();\n"
6731 " x();\n"
6732 "#endif\n"
6733 "}");
6734 verifyFormat("void f(param1, param2,\n"
6735 " param3,\n"
6736 "#ifdef A\n"
6737 " param4(param5,\n"
6738 "#ifdef A1\n"
6739 " param6,\n"
6740 "#ifdef A2\n"
6741 " param7),\n"
6742 "#else\n"
6743 " param8),\n"
6744 " param9,\n"
6745 "#endif\n"
6746 " param10,\n"
6747 "#endif\n"
6748 " param11)\n"
6749 "#else\n"
6750 " param12)\n"
6751 "#endif\n"
6752 "{\n"
6753 " x();\n"
6754 "}",
6755 getLLVMStyleWithColumns(28));
6756 verifyFormat("#if 1\n"
6757 "int i;");
6758 verifyFormat("#if 1\n"
6759 "#endif\n"
6760 "#if 1\n"
6761 "#else\n"
6762 "#endif");
6763 verifyFormat("DEBUG({\n"
6764 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6766 "});\n"
6767 "#if a\n"
6768 "#else\n"
6769 "#endif");
6771 verifyIncompleteFormat("void f(\n"
6772 "#if A\n"
6773 ");\n"
6774 "#else\n"
6775 "#endif");
6777 // Verify that indentation is correct when there is an `#if 0` with an
6778 // `#else`.
6779 verifyFormat("#if 0\n"
6780 "{\n"
6781 "#else\n"
6782 "{\n"
6783 "#endif\n"
6784 " x;\n"
6785 "}");
6787 verifyFormat("#if 0\n"
6788 "#endif\n"
6789 "#if X\n"
6790 "int something_fairly_long; // Align here please\n"
6791 "#endif // Should be aligned");
6793 verifyFormat("#if 0\n"
6794 "#endif\n"
6795 "#if X\n"
6796 "#else // Align\n"
6797 ";\n"
6798 "#endif // Align");
6800 verifyFormat("void SomeFunction(int param1,\n"
6801 " template <\n"
6802 "#ifdef A\n"
6803 "#if 0\n"
6804 "#endif\n"
6805 " MyType<Some>>\n"
6806 "#else\n"
6807 " Type1, Type2>\n"
6808 "#endif\n"
6809 " param2,\n"
6810 " param3) {\n"
6811 " f();\n"
6812 "}");
6815 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6816 verifyFormat("#endif\n"
6817 "#if B");
6820 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6821 FormatStyle SingleLine = getLLVMStyle();
6822 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6823 verifyFormat("#if 0\n"
6824 "#elif 1\n"
6825 "#endif\n"
6826 "void foo() {\n"
6827 " if (test) foo2();\n"
6828 "}",
6829 SingleLine);
6832 TEST_F(FormatTest, LayoutBlockInsideParens) {
6833 verifyFormat("functionCall({ int i; });");
6834 verifyFormat("functionCall({\n"
6835 " int i;\n"
6836 " int j;\n"
6837 "});");
6838 verifyFormat("functionCall(\n"
6839 " {\n"
6840 " int i;\n"
6841 " int j;\n"
6842 " },\n"
6843 " aaaa, bbbb, cccc);");
6844 verifyFormat("functionA(functionB({\n"
6845 " int i;\n"
6846 " int j;\n"
6847 " }),\n"
6848 " aaaa, bbbb, cccc);");
6849 verifyFormat("functionCall(\n"
6850 " {\n"
6851 " int i;\n"
6852 " int j;\n"
6853 " },\n"
6854 " aaaa, bbbb, // comment\n"
6855 " cccc);");
6856 verifyFormat("functionA(functionB({\n"
6857 " int i;\n"
6858 " int j;\n"
6859 " }),\n"
6860 " aaaa, bbbb, // comment\n"
6861 " cccc);");
6862 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6863 verifyFormat("functionCall(aaaa, bbbb, {\n"
6864 " int i;\n"
6865 " int j;\n"
6866 "});");
6867 verifyFormat(
6868 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6869 " {\n"
6870 " int i; // break\n"
6871 " },\n"
6872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6873 " ccccccccccccccccc));");
6874 verifyFormat("DEBUG({\n"
6875 " if (a)\n"
6876 " f();\n"
6877 "});");
6880 TEST_F(FormatTest, LayoutBlockInsideStatement) {
6881 verifyFormat("SOME_MACRO { int i; }\n"
6882 "int i;",
6883 " SOME_MACRO {int i;} int i;");
6886 TEST_F(FormatTest, LayoutNestedBlocks) {
6887 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6888 " struct s {\n"
6889 " int i;\n"
6890 " };\n"
6891 " s kBitsToOs[] = {{10}};\n"
6892 " for (int i = 0; i < 10; ++i)\n"
6893 " return;\n"
6894 "}");
6895 verifyFormat("call(parameter, {\n"
6896 " something();\n"
6897 " // Comment using all columns.\n"
6898 " somethingelse();\n"
6899 "});",
6900 getLLVMStyleWithColumns(40));
6901 verifyFormat("DEBUG( //\n"
6902 " { f(); }, a);");
6903 verifyFormat("DEBUG( //\n"
6904 " {\n"
6905 " f(); //\n"
6906 " },\n"
6907 " a);");
6909 verifyFormat("call(parameter, {\n"
6910 " something();\n"
6911 " // Comment too\n"
6912 " // looooooooooong.\n"
6913 " somethingElse();\n"
6914 "});",
6915 "call(parameter, {\n"
6916 " something();\n"
6917 " // Comment too looooooooooong.\n"
6918 " somethingElse();\n"
6919 "});",
6920 getLLVMStyleWithColumns(29));
6921 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6922 verifyFormat("DEBUG({ // comment\n"
6923 " int i;\n"
6924 "});",
6925 "DEBUG({ // comment\n"
6926 "int i;\n"
6927 "});");
6928 verifyFormat("DEBUG({\n"
6929 " int i;\n"
6930 "\n"
6931 " // comment\n"
6932 " int j;\n"
6933 "});",
6934 "DEBUG({\n"
6935 " int i;\n"
6936 "\n"
6937 " // comment\n"
6938 " int j;\n"
6939 "});");
6941 verifyFormat("DEBUG({\n"
6942 " if (a)\n"
6943 " return;\n"
6944 "});");
6945 verifyGoogleFormat("DEBUG({\n"
6946 " if (a) return;\n"
6947 "});");
6948 FormatStyle Style = getGoogleStyle();
6949 Style.ColumnLimit = 45;
6950 verifyFormat("Debug(\n"
6951 " aaaaa,\n"
6952 " {\n"
6953 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6954 " },\n"
6955 " a);",
6956 Style);
6958 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6960 verifyNoCrash("^{v^{a}}");
6963 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6964 verifyFormat("#define MACRO() \\\n"
6965 " Debug(aaa, /* force line break */ \\\n"
6966 " { \\\n"
6967 " int i; \\\n"
6968 " int j; \\\n"
6969 " })",
6970 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6971 " { int i; int j; })",
6972 getGoogleStyle());
6974 verifyFormat("#define A \\\n"
6975 " [] { \\\n"
6976 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6977 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6978 " }",
6979 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6980 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6981 getGoogleStyle());
6984 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
6985 verifyFormat("enum E {};");
6986 verifyFormat("enum E {}");
6987 FormatStyle Style = getLLVMStyle();
6988 Style.SpaceInEmptyBlock = true;
6989 verifyFormat("void f() { }", "void f() {}", Style);
6990 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
6991 verifyFormat("{ }", Style);
6992 verifyFormat("while (true) { }", "while (true) {}", Style);
6993 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6994 Style.BraceWrapping.BeforeElse = false;
6995 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6996 verifyFormat("if (a)\n"
6997 "{\n"
6998 "} else if (b)\n"
6999 "{\n"
7000 "} else\n"
7001 "{ }",
7002 Style);
7003 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
7004 verifyFormat("if (a) {\n"
7005 "} else if (b) {\n"
7006 "} else {\n"
7007 "}",
7008 Style);
7009 Style.BraceWrapping.BeforeElse = true;
7010 verifyFormat("if (a) { }\n"
7011 "else if (b) { }\n"
7012 "else { }",
7013 Style);
7015 Style = getLLVMStyle(FormatStyle::LK_CSharp);
7016 Style.SpaceInEmptyBlock = true;
7017 verifyFormat("Event += () => { };", Style);
7020 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
7021 FormatStyle Style = getLLVMStyle();
7022 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
7023 Style.MacroBlockEnd = "^[A-Z_]+_END$";
7024 verifyFormat("FOO_BEGIN\n"
7025 " FOO_ENTRY\n"
7026 "FOO_END",
7027 Style);
7028 verifyFormat("FOO_BEGIN\n"
7029 " NESTED_FOO_BEGIN\n"
7030 " NESTED_FOO_ENTRY\n"
7031 " NESTED_FOO_END\n"
7032 "FOO_END",
7033 Style);
7034 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7035 " int x;\n"
7036 " x = 1;\n"
7037 "FOO_END(Baz)",
7038 Style);
7040 Style.RemoveBracesLLVM = true;
7041 verifyNoCrash("for (;;)\n"
7042 " FOO_BEGIN\n"
7043 " foo();\n"
7044 " FOO_END",
7045 Style);
7048 //===----------------------------------------------------------------------===//
7049 // Line break tests.
7050 //===----------------------------------------------------------------------===//
7052 TEST_F(FormatTest, PreventConfusingIndents) {
7053 verifyFormat(
7054 "void f() {\n"
7055 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7056 " parameter, parameter, parameter)),\n"
7057 " SecondLongCall(parameter));\n"
7058 "}");
7059 verifyFormat(
7060 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7061 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7063 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7064 verifyFormat(
7065 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7066 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7067 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7068 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7069 verifyFormat(
7070 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7071 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7073 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7074 verifyFormat("int a = bbbb && ccc &&\n"
7075 " fffff(\n"
7076 "#define A Just forcing a new line\n"
7077 " ddd);");
7080 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7081 verifyFormat(
7082 "bool aaaaaaa =\n"
7083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7084 " bbbbbbbb();");
7085 verifyFormat(
7086 "bool aaaaaaa =\n"
7087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7088 " bbbbbbbb();");
7090 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7092 " ccccccccc == ddddddddddd;");
7093 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7095 " ccccccccc == ddddddddddd;");
7096 verifyFormat(
7097 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7099 " ccccccccc == ddddddddddd;");
7101 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7102 " aaaaaa) &&\n"
7103 " bbbbbb && cccccc;");
7104 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7105 " aaaaaa) >>\n"
7106 " bbbbbb;");
7107 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7108 " SourceMgr.getSpellingColumnNumber(\n"
7109 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7110 " 1);");
7112 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7113 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7114 " cccccc) {\n}");
7115 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7116 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7117 " cccccc) {\n}");
7118 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7119 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7120 " cccccc) {\n}");
7121 verifyFormat("b = a &&\n"
7122 " // Comment\n"
7123 " b.c && d;");
7125 // If the LHS of a comparison is not a binary expression itself, the
7126 // additional linebreak confuses many people.
7127 verifyFormat(
7128 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7130 "}");
7131 verifyFormat(
7132 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7134 "}");
7135 verifyFormat(
7136 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7138 "}");
7139 verifyFormat(
7140 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7142 "}");
7143 // Even explicit parentheses stress the precedence enough to make the
7144 // additional break unnecessary.
7145 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7147 "}");
7148 // This cases is borderline, but with the indentation it is still readable.
7149 verifyFormat(
7150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7151 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7153 "}",
7154 getLLVMStyleWithColumns(75));
7156 // If the LHS is a binary expression, we should still use the additional break
7157 // as otherwise the formatting hides the operator precedence.
7158 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7160 " 5) {\n"
7161 "}");
7162 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7164 " 5) {\n"
7165 "}");
7167 FormatStyle OnePerLine = getLLVMStyle();
7168 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7169 verifyFormat(
7170 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7173 OnePerLine);
7175 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7176 " .aaa(aaaaaaaaaaaaa) *\n"
7177 " aaaaaaa +\n"
7178 " aaaaaaa;",
7179 getLLVMStyleWithColumns(40));
7182 TEST_F(FormatTest, ExpressionIndentation) {
7183 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7185 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7187 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7188 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7191 " ccccccccccccccccccccccccccccccccccccccccc;");
7192 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7194 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7195 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7196 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7199 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7200 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7203 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7204 verifyFormat("if () {\n"
7205 "} else if (aaaaa && bbbbb > // break\n"
7206 " ccccc) {\n"
7207 "}");
7208 verifyFormat("if () {\n"
7209 "} else if constexpr (aaaaa && bbbbb > // break\n"
7210 " ccccc) {\n"
7211 "}");
7212 verifyFormat("if () {\n"
7213 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7214 " ccccc) {\n"
7215 "}");
7216 verifyFormat("if () {\n"
7217 "} else if (aaaaa &&\n"
7218 " bbbbb > // break\n"
7219 " ccccc &&\n"
7220 " ddddd) {\n"
7221 "}");
7223 // Presence of a trailing comment used to change indentation of b.
7224 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7225 " b;\n"
7226 "return aaaaaaaaaaaaaaaaaaa +\n"
7227 " b; //",
7228 getLLVMStyleWithColumns(30));
7231 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7232 // Not sure what the best system is here. Like this, the LHS can be found
7233 // immediately above an operator (everything with the same or a higher
7234 // indent). The RHS is aligned right of the operator and so compasses
7235 // everything until something with the same indent as the operator is found.
7236 // FIXME: Is this a good system?
7237 FormatStyle Style = getLLVMStyle();
7238 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7239 verifyFormat(
7240 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7241 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7242 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7243 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7244 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7245 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7246 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7247 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7248 " > ccccccccccccccccccccccccccccccccccccccccc;",
7249 Style);
7250 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7251 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7252 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7253 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7254 Style);
7255 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7256 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7257 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7258 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7259 Style);
7260 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7261 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7262 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7264 Style);
7265 verifyFormat("if () {\n"
7266 "} else if (aaaaa\n"
7267 " && bbbbb // break\n"
7268 " > ccccc) {\n"
7269 "}",
7270 Style);
7271 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7272 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7273 Style);
7274 verifyFormat("return (a)\n"
7275 " // comment\n"
7276 " + b;",
7277 Style);
7278 verifyFormat(
7279 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7281 " + cc;",
7282 Style);
7284 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7285 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7286 Style);
7288 // Forced by comments.
7289 verifyFormat(
7290 "unsigned ContentSize =\n"
7291 " sizeof(int16_t) // DWARF ARange version number\n"
7292 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7293 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7294 " + sizeof(int8_t); // Segment Size (in bytes)");
7296 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7297 " == boost::fusion::at_c<1>(iiii).second;",
7298 Style);
7300 Style.ColumnLimit = 60;
7301 verifyFormat("zzzzzzzzzz\n"
7302 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7303 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7304 Style);
7306 Style.ColumnLimit = 80;
7307 Style.IndentWidth = 4;
7308 Style.TabWidth = 4;
7309 Style.UseTab = FormatStyle::UT_Always;
7310 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7311 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7312 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7313 "\t&& (someOtherLongishConditionPart1\n"
7314 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7315 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7316 "(someOtherLongishConditionPart1 || "
7317 "someOtherEvenLongerNestedConditionPart2);",
7318 Style);
7320 Style = getLLVMStyleWithColumns(20);
7321 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7322 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7323 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7324 Style.ContinuationIndentWidth = 2;
7325 verifyFormat("struct Foo {\n"
7326 " Foo(\n"
7327 " int arg1,\n"
7328 " int arg2)\n"
7329 " : Base(\n"
7330 " arg1,\n"
7331 " arg2) {}\n"
7332 "};",
7333 Style);
7334 verifyFormat("return abc\n"
7335 " ? foo(\n"
7336 " a,\n"
7337 " b,\n"
7338 " bar(\n"
7339 " abc))\n"
7340 " : g(abc);",
7341 Style);
7344 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7345 FormatStyle Style = getLLVMStyle();
7346 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7347 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7349 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7350 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7351 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7352 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7353 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7354 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7355 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7356 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7357 " > ccccccccccccccccccccccccccccccccccccccccc;",
7358 Style);
7359 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7360 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7361 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7362 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7363 Style);
7364 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7365 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7366 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7367 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7368 Style);
7369 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7370 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7372 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7373 Style);
7374 verifyFormat("if () {\n"
7375 "} else if (aaaaa\n"
7376 " && bbbbb // break\n"
7377 " > ccccc) {\n"
7378 "}",
7379 Style);
7380 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7381 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7382 Style);
7383 verifyFormat("return (a)\n"
7384 " // comment\n"
7385 " + b;",
7386 Style);
7387 verifyFormat(
7388 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7389 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7390 " + cc;",
7391 Style);
7392 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7393 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7394 " : 3333333333333333;",
7395 Style);
7396 verifyFormat(
7397 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7398 " : ccccccccccccccc ? dddddddddddddddddd\n"
7399 " : eeeeeeeeeeeeeeeeee)\n"
7400 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7401 " : 3333333333333333;",
7402 Style);
7403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7404 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7405 Style);
7407 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7408 " == boost::fusion::at_c<1>(iiii).second;",
7409 Style);
7411 Style.ColumnLimit = 60;
7412 verifyFormat("zzzzzzzzzzzzz\n"
7413 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7414 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7415 Style);
7417 // Forced by comments.
7418 Style.ColumnLimit = 80;
7419 verifyFormat(
7420 "unsigned ContentSize\n"
7421 " = sizeof(int16_t) // DWARF ARange version number\n"
7422 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7423 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7424 " + sizeof(int8_t); // Segment Size (in bytes)",
7425 Style);
7427 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7428 verifyFormat(
7429 "unsigned ContentSize =\n"
7430 " sizeof(int16_t) // DWARF ARange version number\n"
7431 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7432 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7433 " + sizeof(int8_t); // Segment Size (in bytes)",
7434 Style);
7436 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
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);
7446 TEST_F(FormatTest, EnforcedOperatorWraps) {
7447 // Here we'd like to wrap after the || operators, but a comment is forcing an
7448 // earlier wrap.
7449 verifyFormat("bool x = aaaaa //\n"
7450 " || bbbbb\n"
7451 " //\n"
7452 " || cccc;");
7455 TEST_F(FormatTest, NoOperandAlignment) {
7456 FormatStyle Style = getLLVMStyle();
7457 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7458 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7461 Style);
7462 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7463 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7464 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7465 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7466 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7467 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7468 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7469 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7470 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7471 " > ccccccccccccccccccccccccccccccccccccccccc;",
7472 Style);
7474 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7475 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7476 " + cc;",
7477 Style);
7478 verifyFormat("int a = aa\n"
7479 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7480 " * cccccccccccccccccccccccccccccccccccc;",
7481 Style);
7483 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7484 verifyFormat("return (a > b\n"
7485 " // comment1\n"
7486 " // comment2\n"
7487 " || c);",
7488 Style);
7491 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7492 FormatStyle Style = getLLVMStyle();
7493 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7494 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7496 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7497 Style);
7500 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7501 FormatStyle Style = getLLVMStyleWithColumns(40);
7502 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7503 Style.BinPackArguments = false;
7504 verifyFormat("void test() {\n"
7505 " someFunction(\n"
7506 " this + argument + is + quite\n"
7507 " + long + so + it + gets + wrapped\n"
7508 " + but + remains + bin - packed);\n"
7509 "}",
7510 Style);
7511 verifyFormat("void test() {\n"
7512 " someFunction(arg1,\n"
7513 " this + argument + is\n"
7514 " + quite + long + so\n"
7515 " + it + gets + wrapped\n"
7516 " + but + remains + bin\n"
7517 " - packed,\n"
7518 " arg3);\n"
7519 "}",
7520 Style);
7521 verifyFormat("void test() {\n"
7522 " someFunction(\n"
7523 " arg1,\n"
7524 " this + argument + has\n"
7525 " + anotherFunc(nested,\n"
7526 " calls + whose\n"
7527 " + arguments\n"
7528 " + are + also\n"
7529 " + wrapped,\n"
7530 " in + addition)\n"
7531 " + to + being + bin - packed,\n"
7532 " arg3);\n"
7533 "}",
7534 Style);
7536 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7537 verifyFormat("void test() {\n"
7538 " someFunction(\n"
7539 " arg1,\n"
7540 " this + argument + has +\n"
7541 " anotherFunc(nested,\n"
7542 " calls + whose +\n"
7543 " arguments +\n"
7544 " are + also +\n"
7545 " wrapped,\n"
7546 " in + addition) +\n"
7547 " to + being + bin - packed,\n"
7548 " arg3);\n"
7549 "}",
7550 Style);
7553 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7554 auto Style = getLLVMStyleWithColumns(45);
7555 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7556 verifyFormat("bool b =\n"
7557 " is_default_constructible_v<hash<T>> and\n"
7558 " is_copy_constructible_v<hash<T>> and\n"
7559 " is_move_constructible_v<hash<T>> and\n"
7560 " is_copy_assignable_v<hash<T>> and\n"
7561 " is_move_assignable_v<hash<T>> and\n"
7562 " is_destructible_v<hash<T>> and\n"
7563 " is_swappable_v<hash<T>> and\n"
7564 " is_callable_v<hash<T>(T)>;",
7565 Style);
7567 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7568 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7569 " and is_copy_constructible_v<hash<T>>\n"
7570 " and is_move_constructible_v<hash<T>>\n"
7571 " and is_copy_assignable_v<hash<T>>\n"
7572 " and is_move_assignable_v<hash<T>>\n"
7573 " and is_destructible_v<hash<T>>\n"
7574 " and is_swappable_v<hash<T>>\n"
7575 " and is_callable_v<hash<T>(T)>;",
7576 Style);
7578 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7579 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7580 " and is_copy_constructible_v<hash<T>>\n"
7581 " and is_move_constructible_v<hash<T>>\n"
7582 " and is_copy_assignable_v<hash<T>>\n"
7583 " and is_move_assignable_v<hash<T>>\n"
7584 " and is_destructible_v<hash<T>>\n"
7585 " and is_swappable_v<hash<T>>\n"
7586 " and is_callable_v<hash<T>(T)>;",
7587 Style);
7590 TEST_F(FormatTest, ConstructorInitializers) {
7591 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7592 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7593 getLLVMStyleWithColumns(45));
7594 verifyFormat("Constructor()\n"
7595 " : Inttializer(FitsOnTheLine) {}",
7596 getLLVMStyleWithColumns(44));
7597 verifyFormat("Constructor()\n"
7598 " : Inttializer(FitsOnTheLine) {}",
7599 getLLVMStyleWithColumns(43));
7601 verifyFormat("template <typename T>\n"
7602 "Constructor() : Initializer(FitsOnTheLine) {}",
7603 getLLVMStyleWithColumns(45));
7605 verifyFormat(
7606 "SomeClass::Constructor()\n"
7607 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7609 verifyFormat(
7610 "SomeClass::Constructor()\n"
7611 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7612 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7613 verifyFormat(
7614 "SomeClass::Constructor()\n"
7615 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7616 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7617 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7618 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7619 " : aaaaaaaaaa(aaaaaa) {}");
7621 verifyFormat("Constructor()\n"
7622 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7623 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7624 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7625 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7627 verifyFormat("Constructor()\n"
7628 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7631 verifyFormat("Constructor(int Parameter = 0)\n"
7632 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7633 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7634 verifyFormat("Constructor()\n"
7635 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7636 "}",
7637 getLLVMStyleWithColumns(60));
7638 verifyFormat("Constructor()\n"
7639 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7640 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7642 // Here a line could be saved by splitting the second initializer onto two
7643 // lines, but that is not desirable.
7644 verifyFormat("Constructor()\n"
7645 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7646 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7647 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7649 FormatStyle OnePerLine = getLLVMStyle();
7650 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7651 verifyFormat("MyClass::MyClass()\n"
7652 " : a(a),\n"
7653 " b(b),\n"
7654 " c(c) {}",
7655 OnePerLine);
7656 verifyFormat("MyClass::MyClass()\n"
7657 " : a(a), // comment\n"
7658 " b(b),\n"
7659 " c(c) {}",
7660 OnePerLine);
7661 verifyFormat("MyClass::MyClass(int a)\n"
7662 " : b(a), // comment\n"
7663 " c(a + 1) { // lined up\n"
7664 "}",
7665 OnePerLine);
7666 verifyFormat("Constructor()\n"
7667 " : a(b, b, b) {}",
7668 OnePerLine);
7669 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7670 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7671 verifyFormat("SomeClass::Constructor()\n"
7672 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7673 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7674 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7675 OnePerLine);
7676 verifyFormat("SomeClass::Constructor()\n"
7677 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7678 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7679 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7680 OnePerLine);
7681 verifyFormat("MyClass::MyClass(int var)\n"
7682 " : some_var_(var), // 4 space indent\n"
7683 " some_other_var_(var + 1) { // lined up\n"
7684 "}",
7685 OnePerLine);
7686 verifyFormat("Constructor()\n"
7687 " : aaaaa(aaaaaa),\n"
7688 " aaaaa(aaaaaa),\n"
7689 " aaaaa(aaaaaa),\n"
7690 " aaaaa(aaaaaa),\n"
7691 " aaaaa(aaaaaa) {}",
7692 OnePerLine);
7693 verifyFormat("Constructor()\n"
7694 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7695 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7696 OnePerLine);
7697 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7698 verifyFormat(
7699 "Constructor()\n"
7700 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7701 " aaaaaaaaaaa().aaa(),\n"
7702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7703 OnePerLine);
7704 OnePerLine.ColumnLimit = 60;
7705 verifyFormat("Constructor()\n"
7706 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7707 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7708 OnePerLine);
7710 verifyFormat("Constructor()\n"
7711 " : // Comment forcing unwanted break.\n"
7712 " aaaa(aaaa) {}",
7713 "Constructor() :\n"
7714 " // Comment forcing unwanted break.\n"
7715 " aaaa(aaaa) {}");
7718 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7719 FormatStyle Style = getLLVMStyleWithColumns(60);
7720 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7721 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7723 for (int i = 0; i < 4; ++i) {
7724 // Test all combinations of parameters that should not have an effect.
7725 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7726 Style.AllowAllArgumentsOnNextLine = i & 2;
7728 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7729 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7730 verifyFormat("Constructor()\n"
7731 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7732 Style);
7733 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7735 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7736 verifyFormat("Constructor()\n"
7737 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7738 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7739 Style);
7740 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7742 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7743 verifyFormat("Constructor()\n"
7744 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7745 Style);
7746 verifyFormat("Constructor()\n"
7747 " : a(a), b(b) {}",
7748 Style);
7749 verifyFormat("Constructor()\n"
7750 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7751 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7752 " , cccccccccccccccccccccc(c) {}",
7753 Style);
7755 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7756 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7757 verifyFormat("Constructor()\n"
7758 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7759 Style);
7761 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7762 verifyFormat("Constructor()\n"
7763 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7764 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7765 Style);
7767 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7768 verifyFormat("Constructor()\n"
7769 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7770 Style);
7771 verifyFormat("Constructor()\n"
7772 " : a(a), b(b) {}",
7773 Style);
7774 verifyFormat("Constructor()\n"
7775 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7776 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7777 " cccccccccccccccccccccc(c) {}",
7778 Style);
7780 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7781 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7782 verifyFormat("Constructor() :\n"
7783 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7784 Style);
7786 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7787 verifyFormat("Constructor() :\n"
7788 " aaaaaaaaaaaaaaaaaa(a),\n"
7789 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7790 Style);
7792 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7793 verifyFormat("Constructor() :\n"
7794 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7795 Style);
7796 verifyFormat("Constructor() :\n"
7797 " a(a), b(b) {}",
7798 Style);
7799 verifyFormat("Constructor() :\n"
7800 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7801 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7802 " cccccccccccccccccccccc(c) {}",
7803 Style);
7806 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7807 // AllowAllConstructorInitializersOnNextLine in all
7808 // BreakConstructorInitializers modes
7809 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7810 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7811 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7812 verifyFormat("SomeClassWithALongName::Constructor(\n"
7813 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7814 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7815 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7816 Style);
7818 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7819 verifyFormat("SomeClassWithALongName::Constructor(\n"
7820 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7821 " int bbbbbbbbbbbbb,\n"
7822 " int cccccccccccccccc)\n"
7823 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7824 Style);
7826 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7827 verifyFormat("SomeClassWithALongName::Constructor(\n"
7828 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7829 " int bbbbbbbbbbbbb,\n"
7830 " int cccccccccccccccc)\n"
7831 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7832 Style);
7834 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7835 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7836 verifyFormat("SomeClassWithALongName::Constructor(\n"
7837 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7838 " int bbbbbbbbbbbbb)\n"
7839 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7840 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7841 Style);
7843 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7845 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7846 verifyFormat("SomeClassWithALongName::Constructor(\n"
7847 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7848 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7849 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7850 Style);
7852 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7853 verifyFormat("SomeClassWithALongName::Constructor(\n"
7854 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7855 " int bbbbbbbbbbbbb,\n"
7856 " int cccccccccccccccc)\n"
7857 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7858 Style);
7860 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7861 verifyFormat("SomeClassWithALongName::Constructor(\n"
7862 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7863 " int bbbbbbbbbbbbb,\n"
7864 " int cccccccccccccccc)\n"
7865 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7866 Style);
7868 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7869 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7870 verifyFormat("SomeClassWithALongName::Constructor(\n"
7871 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7872 " int bbbbbbbbbbbbb)\n"
7873 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7874 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7875 Style);
7877 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7878 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7879 verifyFormat("SomeClassWithALongName::Constructor(\n"
7880 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7881 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7882 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7883 Style);
7885 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7886 verifyFormat("SomeClassWithALongName::Constructor(\n"
7887 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7888 " int bbbbbbbbbbbbb,\n"
7889 " int cccccccccccccccc) :\n"
7890 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7891 Style);
7893 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7894 verifyFormat("SomeClassWithALongName::Constructor(\n"
7895 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7896 " int bbbbbbbbbbbbb,\n"
7897 " int cccccccccccccccc) :\n"
7898 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7899 Style);
7901 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7902 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7903 verifyFormat("SomeClassWithALongName::Constructor(\n"
7904 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7905 " int bbbbbbbbbbbbb) :\n"
7906 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7907 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7908 Style);
7910 Style = getLLVMStyleWithColumns(0);
7911 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7912 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7913 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7914 " : bar(bar), baz(baz) {}",
7915 Style);
7918 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7919 FormatStyle Style = getLLVMStyleWithColumns(60);
7920 Style.BinPackArguments = false;
7921 for (int i = 0; i < 4; ++i) {
7922 // Test all combinations of parameters that should not have an effect.
7923 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7924 Style.PackConstructorInitializers =
7925 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7927 Style.AllowAllArgumentsOnNextLine = true;
7928 verifyFormat("void foo() {\n"
7929 " FunctionCallWithReallyLongName(\n"
7930 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7931 "}",
7932 Style);
7933 Style.AllowAllArgumentsOnNextLine = false;
7934 verifyFormat("void foo() {\n"
7935 " FunctionCallWithReallyLongName(\n"
7936 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7937 " bbbbbbbbbbbb);\n"
7938 "}",
7939 Style);
7941 Style.AllowAllArgumentsOnNextLine = true;
7942 verifyFormat("void foo() {\n"
7943 " auto VariableWithReallyLongName = {\n"
7944 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7945 "}",
7946 Style);
7947 Style.AllowAllArgumentsOnNextLine = false;
7948 verifyFormat("void foo() {\n"
7949 " auto VariableWithReallyLongName = {\n"
7950 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7951 " bbbbbbbbbbbb};\n"
7952 "}",
7953 Style);
7956 // This parameter should not affect declarations.
7957 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7958 Style.AllowAllArgumentsOnNextLine = false;
7959 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7960 verifyFormat("void FunctionCallWithReallyLongName(\n"
7961 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7962 Style);
7963 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7964 verifyFormat("void FunctionCallWithReallyLongName(\n"
7965 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7966 " int bbbbbbbbbbbb);",
7967 Style);
7970 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7971 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7972 // and BAS_Align.
7973 FormatStyle Style = getLLVMStyleWithColumns(35);
7974 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7975 "void functionDecl(int A, int B, int C);";
7976 Style.AllowAllArgumentsOnNextLine = false;
7977 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7978 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7979 " paramC);\n"
7980 "void functionDecl(int A, int B,\n"
7981 " int C);"),
7982 Input, Style);
7983 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7984 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7985 " paramC);\n"
7986 "void functionDecl(int A, int B,\n"
7987 " int C);"),
7988 Input, Style);
7989 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7990 // AllowAllArgumentsOnNextLine.
7991 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7992 verifyFormat(StringRef("functionCall(\n"
7993 " paramA, paramB, paramC);\n"
7994 "void functionDecl(\n"
7995 " int A, int B, int C);"),
7996 Input, Style);
7997 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
7998 verifyFormat("functionCall(\n"
7999 " paramA, paramB, paramC\n"
8000 ");\n"
8001 "void functionDecl(\n"
8002 " int A, int B, int C\n"
8003 ");",
8004 Input, Style);
8006 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8007 // first argument.
8008 Style.AllowAllArgumentsOnNextLine = true;
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 // It wouldn't fit on one line with aligned parameters so this setting
8016 // doesn't change anything for BAS_Align.
8017 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8018 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8019 " paramC);\n"
8020 "void functionDecl(int A, int B,\n"
8021 " int C);"),
8022 Input, Style);
8023 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8024 verifyFormat(StringRef("functionCall(\n"
8025 " paramA, paramB, paramC);\n"
8026 "void functionDecl(\n"
8027 " int A, int B, int C);"),
8028 Input, Style);
8031 TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8032 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8033 "void emptyFunctionDefinition() {}\n"
8034 "void functionDefinition(int A, int B, int C) {}\n"
8035 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8036 verifyFormat(Input);
8038 FormatStyle Style = getLLVMStyle();
8039 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8040 Style.BreakFunctionDefinitionParameters = true;
8041 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8042 "void emptyFunctionDefinition() {}\n"
8043 "void functionDefinition(\n"
8044 " int A, int B, int C) {}\n"
8045 "Class::Class(\n"
8046 " int A, int B)\n"
8047 " : m_A(A), m_B(B) {}",
8048 Input, Style);
8050 // Test the style where all parameters are on their own lines.
8051 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8052 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8053 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8054 "void emptyFunctionDefinition() {}\n"
8055 "void functionDefinition(\n"
8056 " int A,\n"
8057 " int B,\n"
8058 " int C) {}\n"
8059 "Class::Class(\n"
8060 " int A,\n"
8061 " int B)\n"
8062 " : m_A(A), m_B(B) {}",
8063 Input, Style);
8066 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8067 FormatStyle Style = getLLVMStyle();
8068 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8069 /* Test the behaviour with long lines */
8070 Style.ColumnLimit = 40;
8071 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8072 " : : val);",
8073 Style);
8074 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8075 " : val1 : val2);",
8076 Style);
8077 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8078 " \"cpuid\\n\\t\"\n"
8079 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8080 " : \"=a\" : \"a\");",
8081 Style);
8082 Style.ColumnLimit = 80;
8083 verifyFormat("asm volatile(\"string\", : : val);", Style);
8084 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8086 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8087 verifyFormat("asm volatile(\"string\",\n"
8088 " :\n"
8089 " : val);",
8090 Style);
8091 verifyFormat("asm volatile(\"string\",\n"
8092 " : val1\n"
8093 " : val2);",
8094 Style);
8095 /* Test the behaviour with long lines */
8096 Style.ColumnLimit = 40;
8097 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8098 " \"cpuid\\n\\t\"\n"
8099 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8100 " : \"=a\"(*rEAX)\n"
8101 " : \"a\"(value));",
8102 Style);
8103 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8104 " \"cpuid\\n\\t\"\n"
8105 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8106 " :\n"
8107 " : \"a\"(value));",
8108 Style);
8109 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8110 " :\n"
8111 " : val);",
8112 Style);
8113 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8114 " : val1\n"
8115 " : val2);",
8116 Style);
8119 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8120 FormatStyle Style = getLLVMStyle();
8121 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8123 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8124 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8125 getStyleWithColumns(Style, 45));
8126 verifyFormat("Constructor() :\n"
8127 " Initializer(FitsOnTheLine) {}",
8128 getStyleWithColumns(Style, 44));
8129 verifyFormat("Constructor() :\n"
8130 " Initializer(FitsOnTheLine) {}",
8131 getStyleWithColumns(Style, 43));
8133 verifyFormat("template <typename T>\n"
8134 "Constructor() : Initializer(FitsOnTheLine) {}",
8135 getStyleWithColumns(Style, 50));
8136 verifyFormat(
8137 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8138 " int mooooooooooooore) noexcept :\n"
8139 " Super{some, arguments}, Member{5}, Member2{2} {}",
8140 Style);
8141 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8142 verifyFormat(
8143 "SomeClass::Constructor() :\n"
8144 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8145 Style);
8146 verifyFormat(
8147 "SomeClass::Constructor() : // NOLINT\n"
8148 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8149 Style);
8151 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8152 verifyFormat(
8153 "SomeClass::Constructor() :\n"
8154 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8155 Style);
8156 verifyFormat(
8157 "SomeClass::Constructor() : // NOLINT\n"
8158 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8159 Style);
8161 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8162 verifyFormat(
8163 "SomeClass::Constructor() :\n"
8164 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8165 Style);
8167 verifyFormat(
8168 "SomeClass::Constructor() :\n"
8169 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8170 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8171 Style);
8172 verifyFormat(
8173 "SomeClass::Constructor() :\n"
8174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8175 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8176 Style);
8177 verifyFormat(
8178 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8179 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8180 Style);
8182 verifyFormat("Constructor() :\n"
8183 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8184 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8185 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8186 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8187 Style);
8189 verifyFormat("Constructor() :\n"
8190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8192 Style);
8194 verifyFormat("Constructor(int Parameter = 0) :\n"
8195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8196 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8197 Style);
8198 verifyFormat("Constructor() :\n"
8199 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8200 "}",
8201 getStyleWithColumns(Style, 60));
8202 verifyFormat("Constructor() :\n"
8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8204 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8205 Style);
8207 // Here a line could be saved by splitting the second initializer onto two
8208 // lines, but that is not desirable.
8209 verifyFormat("Constructor() :\n"
8210 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8211 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8212 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8213 Style);
8215 FormatStyle OnePerLine = Style;
8216 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8217 verifyFormat("SomeClass::Constructor() :\n"
8218 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8219 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8220 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8221 OnePerLine);
8222 verifyFormat("SomeClass::Constructor() :\n"
8223 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8224 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8225 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8226 OnePerLine);
8227 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8228 " i(i), // comment\n"
8229 " j(j) {}",
8230 OnePerLine);
8231 verifyFormat("MyClass::MyClass(int var) :\n"
8232 " some_var_(var), // 4 space indent\n"
8233 " some_other_var_(var + 1) { // lined up\n"
8234 "}",
8235 OnePerLine);
8236 verifyFormat("Constructor() :\n"
8237 " aaaaa(aaaaaa),\n"
8238 " aaaaa(aaaaaa),\n"
8239 " aaaaa(aaaaaa),\n"
8240 " aaaaa(aaaaaa),\n"
8241 " aaaaa(aaaaaa) {}",
8242 OnePerLine);
8243 verifyFormat("Constructor() :\n"
8244 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8245 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8246 OnePerLine);
8247 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8248 verifyFormat("Constructor() :\n"
8249 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8250 " aaaaaaaaaaa().aaa(),\n"
8251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8252 OnePerLine);
8253 OnePerLine.ColumnLimit = 60;
8254 verifyFormat("Constructor() :\n"
8255 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8256 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8257 OnePerLine);
8259 verifyFormat("Constructor() :\n"
8260 " // Comment forcing unwanted break.\n"
8261 " aaaa(aaaa) {}",
8262 Style);
8263 verifyFormat("Constructor() : // NOLINT\n"
8264 " aaaa(aaaa) {}",
8265 Style);
8266 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8267 " on a single\n"
8268 " // line.\n"
8269 " aaaa(aaaa) {}",
8270 "Constructor() : // A very long trailing comment that cannot fit"
8271 " on a single line.\n"
8272 " aaaa(aaaa) {}",
8273 Style);
8275 Style.ColumnLimit = 0;
8276 verifyFormat("SomeClass::Constructor() :\n"
8277 " a(a) {}",
8278 Style);
8279 verifyFormat("SomeClass::Constructor() noexcept :\n"
8280 " a(a) {}",
8281 Style);
8282 verifyFormat("SomeClass::Constructor() :\n"
8283 " a(a), b(b), c(c) {}",
8284 Style);
8285 verifyFormat("SomeClass::Constructor() :\n"
8286 " a(a) {\n"
8287 " foo();\n"
8288 " bar();\n"
8289 "}",
8290 Style);
8292 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8293 verifyFormat("SomeClass::Constructor() :\n"
8294 " a(a), b(b), c(c) {\n"
8295 "}",
8296 Style);
8297 verifyFormat("SomeClass::Constructor() :\n"
8298 " a(a) {\n"
8299 "}",
8300 Style);
8302 Style.ColumnLimit = 80;
8303 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8304 Style.ConstructorInitializerIndentWidth = 2;
8305 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8306 verifyFormat("SomeClass::Constructor() :\n"
8307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8308 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8309 Style);
8311 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8312 // well
8313 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8314 verifyFormat(
8315 "class SomeClass\n"
8316 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8317 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8318 Style);
8319 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8320 verifyFormat(
8321 "class SomeClass\n"
8322 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8323 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8324 Style);
8325 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8326 verifyFormat(
8327 "class SomeClass :\n"
8328 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8329 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8330 Style);
8331 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8332 verifyFormat(
8333 "class SomeClass\n"
8334 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8335 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8336 Style);
8339 #ifndef EXPENSIVE_CHECKS
8340 // Expensive checks enables libstdc++ checking which includes validating the
8341 // state of ranges used in std::priority_queue - this blows out the
8342 // runtime/scalability of the function and makes this test unacceptably slow.
8343 TEST_F(FormatTest, MemoizationTests) {
8344 // This breaks if the memoization lookup does not take \c Indent and
8345 // \c LastSpace into account.
8346 verifyFormat(
8347 "extern CFRunLoopTimerRef\n"
8348 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8349 " CFTimeInterval interval, CFOptionFlags flags,\n"
8350 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8351 " CFRunLoopTimerContext *context) {}");
8353 // Deep nesting somewhat works around our memoization.
8354 verifyFormat(
8355 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8356 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8357 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8358 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8359 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8360 getLLVMStyleWithColumns(65));
8361 verifyFormat(
8362 "aaaaa(\n"
8363 " aaaaa,\n"
8364 " aaaaa(\n"
8365 " aaaaa,\n"
8366 " aaaaa(\n"
8367 " aaaaa,\n"
8368 " aaaaa(\n"
8369 " aaaaa,\n"
8370 " aaaaa(\n"
8371 " aaaaa,\n"
8372 " aaaaa(\n"
8373 " aaaaa,\n"
8374 " aaaaa(\n"
8375 " aaaaa,\n"
8376 " aaaaa(\n"
8377 " aaaaa,\n"
8378 " aaaaa(\n"
8379 " aaaaa,\n"
8380 " aaaaa(\n"
8381 " aaaaa,\n"
8382 " aaaaa(\n"
8383 " aaaaa,\n"
8384 " aaaaa(\n"
8385 " aaaaa,\n"
8386 " aaaaa))))))))))));",
8387 getLLVMStyleWithColumns(65));
8388 verifyFormat(
8389 "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"
8390 " a),\n"
8391 " a),\n"
8392 " a),\n"
8393 " a),\n"
8394 " a),\n"
8395 " a),\n"
8396 " a),\n"
8397 " a),\n"
8398 " a),\n"
8399 " a),\n"
8400 " a),\n"
8401 " a),\n"
8402 " a),\n"
8403 " a),\n"
8404 " a),\n"
8405 " a),\n"
8406 " a)",
8407 getLLVMStyleWithColumns(65));
8409 // This test takes VERY long when memoization is broken.
8410 FormatStyle OnePerLine = getLLVMStyle();
8411 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8412 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8413 std::string input = "Constructor()\n"
8414 " : aaaa(a,\n";
8415 for (unsigned i = 0, e = 80; i != e; ++i)
8416 input += " a,\n";
8417 input += " a) {}";
8418 verifyFormat(input, OnePerLine);
8419 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8420 verifyFormat(input, OnePerLine);
8422 #endif
8424 TEST_F(FormatTest, BreaksAsHighAsPossible) {
8425 verifyFormat(
8426 "void f() {\n"
8427 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8428 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8429 " f();\n"
8430 "}");
8431 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8432 " Intervals[i - 1].getRange().getLast()) {\n}");
8435 TEST_F(FormatTest, BreaksFunctionDeclarations) {
8436 // Principially, we break function declarations in a certain order:
8437 // 1) break amongst arguments.
8438 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8439 " Cccccccccccccc cccccccccccccc);");
8440 verifyFormat("template <class TemplateIt>\n"
8441 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8442 " TemplateIt *stop) {}");
8444 // 2) break after return type.
8445 verifyGoogleFormat(
8446 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8447 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8449 // 3) break after (.
8450 verifyGoogleFormat(
8451 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8452 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8454 // 4) break before after nested name specifiers.
8455 verifyGoogleFormat(
8456 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8457 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8458 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8460 // However, there are exceptions, if a sufficient amount of lines can be
8461 // saved.
8462 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8463 // more adjusting.
8464 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8465 " Cccccccccccccc cccccccccc,\n"
8466 " Cccccccccccccc cccccccccc,\n"
8467 " Cccccccccccccc cccccccccc,\n"
8468 " Cccccccccccccc cccccccccc);");
8469 verifyGoogleFormat(
8470 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8471 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8472 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8473 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8474 verifyFormat(
8475 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8476 " Cccccccccccccc cccccccccc,\n"
8477 " Cccccccccccccc cccccccccc,\n"
8478 " Cccccccccccccc cccccccccc,\n"
8479 " Cccccccccccccc cccccccccc,\n"
8480 " Cccccccccccccc cccccccccc,\n"
8481 " Cccccccccccccc cccccccccc);");
8482 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8483 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8484 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8485 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8486 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8488 // Break after multi-line parameters.
8489 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8492 " bbbb bbbb);");
8493 verifyFormat("void SomeLoooooooooooongFunction(\n"
8494 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8495 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8496 " int bbbbbbbbbbbbb);");
8498 // Treat overloaded operators like other functions.
8499 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8500 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8501 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8502 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8503 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8504 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8505 verifyGoogleFormat(
8506 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8507 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8508 verifyGoogleFormat(
8509 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8510 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8511 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8512 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8513 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8514 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8515 verifyGoogleFormat(
8516 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8517 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8518 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8519 verifyGoogleFormat("template <typename T>\n"
8520 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8521 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8522 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8524 FormatStyle Style = getLLVMStyle();
8525 Style.PointerAlignment = FormatStyle::PAS_Left;
8526 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8527 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8528 Style);
8529 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8531 Style);
8534 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8535 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8536 // Prefer keeping `::` followed by `operator` together.
8537 verifyFormat("const aaaa::bbbbbbb &\n"
8538 "ccccccccc::operator++() {\n"
8539 " stuff();\n"
8540 "}",
8541 "const aaaa::bbbbbbb\n"
8542 "&ccccccccc::operator++() { stuff(); }",
8543 getLLVMStyleWithColumns(40));
8546 TEST_F(FormatTest, TrailingReturnType) {
8547 verifyFormat("auto foo() -> int;");
8548 // correct trailing return type spacing
8549 verifyFormat("auto operator->() -> int;");
8550 verifyFormat("auto operator++(int) -> int;");
8552 verifyFormat("struct S {\n"
8553 " auto bar() const -> int;\n"
8554 "};");
8555 verifyFormat("template <size_t Order, typename T>\n"
8556 "auto load_img(const std::string &filename)\n"
8557 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8558 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8559 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8560 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8561 verifyFormat("template <typename T>\n"
8562 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8563 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8565 FormatStyle Style = getLLVMStyleWithColumns(60);
8566 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8567 " auto NAME() -> int { return 42; }",
8568 Style);
8570 // Not trailing return types.
8571 verifyFormat("void f() { auto a = b->c(); }");
8572 verifyFormat("auto a = p->foo();");
8573 verifyFormat("int a = p->foo();");
8574 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8577 TEST_F(FormatTest, DeductionGuides) {
8578 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8579 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8580 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8581 verifyFormat(
8582 "template <class... T>\n"
8583 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8584 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8585 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8586 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8587 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8588 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8589 verifyFormat("template <class T> x() -> x<1>;");
8590 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8592 verifyFormat("A(const char *) -> A<string &>;");
8593 verifyFormat("A() -> A<int>;");
8595 // Ensure not deduction guides.
8596 verifyFormat("c()->f<int>();");
8597 verifyFormat("x()->foo<1>;");
8598 verifyFormat("x = p->foo<3>();");
8599 verifyFormat("x()->x<1>();");
8602 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8603 // Avoid breaking before trailing 'const' or other trailing annotations, if
8604 // they are not function-like.
8605 FormatStyle Style = getGoogleStyleWithColumns(47);
8606 verifyFormat("void someLongFunction(\n"
8607 " int someLoooooooooooooongParameter) const {\n}",
8608 getLLVMStyleWithColumns(47));
8609 verifyFormat("LoooooongReturnType\n"
8610 "someLoooooooongFunction() const {}",
8611 getLLVMStyleWithColumns(47));
8612 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8613 " const {}",
8614 Style);
8615 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8616 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8617 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8618 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8619 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8620 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8621 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8622 " aaaaaaaaaaa aaaaa) const override;");
8623 verifyGoogleFormat(
8624 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8625 " const override;");
8627 // Even if the first parameter has to be wrapped.
8628 verifyFormat("void someLongFunction(\n"
8629 " int someLongParameter) const {}",
8630 getLLVMStyleWithColumns(46));
8631 verifyFormat("void someLongFunction(\n"
8632 " int someLongParameter) const {}",
8633 Style);
8634 verifyFormat("void someLongFunction(\n"
8635 " int someLongParameter) override {}",
8636 Style);
8637 verifyFormat("void someLongFunction(\n"
8638 " int someLongParameter) OVERRIDE {}",
8639 Style);
8640 verifyFormat("void someLongFunction(\n"
8641 " int someLongParameter) final {}",
8642 Style);
8643 verifyFormat("void someLongFunction(\n"
8644 " int someLongParameter) FINAL {}",
8645 Style);
8646 verifyFormat("void someLongFunction(\n"
8647 " int parameter) const override {}",
8648 Style);
8650 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8651 verifyFormat("void someLongFunction(\n"
8652 " int someLongParameter) const\n"
8653 "{\n"
8654 "}",
8655 Style);
8657 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8658 verifyFormat("void someLongFunction(\n"
8659 " int someLongParameter) const\n"
8660 " {\n"
8661 " }",
8662 Style);
8664 // Unless these are unknown annotations.
8665 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8666 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8667 " LONG_AND_UGLY_ANNOTATION;");
8669 // Breaking before function-like trailing annotations is fine to keep them
8670 // close to their arguments.
8671 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8672 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8673 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8674 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8675 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8676 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8677 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8678 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8679 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8681 verifyFormat(
8682 "void aaaaaaaaaaaaaaaaaa()\n"
8683 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8684 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8685 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8686 " __attribute__((unused));");
8688 Style = getGoogleStyle();
8690 verifyFormat(
8691 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8692 " GUARDED_BY(aaaaaaaaaaaa);",
8693 Style);
8694 verifyFormat(
8695 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8696 " GUARDED_BY(aaaaaaaaaaaa);",
8697 Style);
8698 verifyFormat(
8699 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8700 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8701 Style);
8702 verifyFormat(
8703 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8704 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8705 Style);
8707 verifyFormat(
8708 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8709 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8710 Style);
8711 verifyFormat(
8712 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8713 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8714 Style);
8715 verifyFormat(
8716 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8717 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8718 Style);
8719 verifyFormat(
8720 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8721 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8722 Style);
8725 TEST_F(FormatTest, FunctionAnnotations) {
8726 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8727 "int OldFunction(const string &parameter) {}");
8728 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8729 "string OldFunction(const string &parameter) {}");
8730 verifyFormat("template <typename T>\n"
8731 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8732 "string OldFunction(const string &parameter) {}");
8734 // Not function annotations.
8735 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8736 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8737 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8738 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8739 verifyFormat("MACRO(abc).function() // wrap\n"
8740 " << abc;");
8741 verifyFormat("MACRO(abc)->function() // wrap\n"
8742 " << abc;");
8743 verifyFormat("MACRO(abc)::function() // wrap\n"
8744 " << abc;");
8745 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8748 TEST_F(FormatTest, BreaksDesireably) {
8749 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8750 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8751 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8752 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8754 "}");
8756 verifyFormat(
8757 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8760 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8764 verifyFormat(
8765 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8766 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8768 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8771 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8772 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8774 verifyFormat(
8775 "void f() {\n"
8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8778 "}");
8779 verifyFormat(
8780 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8782 verifyFormat(
8783 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8785 verifyFormat(
8786 "aaaaaa(aaa,\n"
8787 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8789 " aaaa);");
8790 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8794 // Indent consistently independent of call expression and unary operator.
8795 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8796 " dddddddddddddddddddddddddddddd));");
8797 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8798 " dddddddddddddddddddddddddddddd));");
8799 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8800 " dddddddddddddddddddddddddddddd));");
8802 // This test case breaks on an incorrect memoization, i.e. an optimization not
8803 // taking into account the StopAt value.
8804 verifyFormat(
8805 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8806 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8807 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8808 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8810 verifyFormat("{\n {\n {\n"
8811 " Annotation.SpaceRequiredBefore =\n"
8812 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8813 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8814 " }\n }\n}");
8816 // Break on an outer level if there was a break on an inner level.
8817 verifyFormat("f(g(h(a, // comment\n"
8818 " b, c),\n"
8819 " d, e),\n"
8820 " x, y);",
8821 "f(g(h(a, // comment\n"
8822 " b, c), d, e), x, y);");
8824 // Prefer breaking similar line breaks.
8825 verifyFormat(
8826 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8827 " NSTrackingMouseEnteredAndExited |\n"
8828 " NSTrackingActiveAlways;");
8831 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8832 FormatStyle NoBinPacking = getGoogleStyle();
8833 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8834 NoBinPacking.BinPackArguments = true;
8835 verifyFormat("void f() {\n"
8836 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8838 "}",
8839 NoBinPacking);
8840 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8841 " int aaaaaaaaaaaaaaaaaaaa,\n"
8842 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8843 NoBinPacking);
8845 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8846 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8847 " vector<int> bbbbbbbbbbbbbbb);",
8848 NoBinPacking);
8849 // FIXME: This behavior difference is probably not wanted. However, currently
8850 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8851 // template arguments from BreakBeforeParameter being set because of the
8852 // one-per-line formatting.
8853 verifyFormat(
8854 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8855 " aaaaaaaaaa> aaaaaaaaaa);",
8856 NoBinPacking);
8857 verifyFormat(
8858 "void fffffffffff(\n"
8859 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8860 " aaaaaaaaaa);");
8863 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8864 FormatStyle NoBinPacking = getGoogleStyle();
8865 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8866 NoBinPacking.BinPackArguments = false;
8867 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8868 " aaaaaaaaaaaaaaaaaaaa,\n"
8869 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8870 NoBinPacking);
8871 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8872 " aaaaaaaaaaaaa,\n"
8873 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8874 NoBinPacking);
8875 verifyFormat(
8876 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8877 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8879 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8881 NoBinPacking);
8882 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8883 " .aaaaaaaaaaaaaaaaaa();",
8884 NoBinPacking);
8885 verifyFormat("void f() {\n"
8886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8887 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8888 "}",
8889 NoBinPacking);
8891 verifyFormat(
8892 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8893 " aaaaaaaaaaaa,\n"
8894 " aaaaaaaaaaaa);",
8895 NoBinPacking);
8896 verifyFormat(
8897 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8898 " ddddddddddddddddddddddddddddd),\n"
8899 " test);",
8900 NoBinPacking);
8902 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8903 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8904 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8905 " aaaaaaaaaaaaaaaaaa;",
8906 NoBinPacking);
8907 verifyFormat("a(\"a\"\n"
8908 " \"a\",\n"
8909 " a);");
8911 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8912 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8913 " aaaaaaaaa,\n"
8914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8915 NoBinPacking);
8916 verifyFormat(
8917 "void f() {\n"
8918 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8919 " .aaaaaaa();\n"
8920 "}",
8921 NoBinPacking);
8922 verifyFormat(
8923 "template <class SomeType, class SomeOtherType>\n"
8924 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8925 NoBinPacking);
8928 TEST_F(FormatTest, FormatsDeclarationBreakAlways) {
8929 FormatStyle BreakAlways = getGoogleStyle();
8930 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
8931 verifyFormat("void f(int a,\n"
8932 " int b);",
8933 BreakAlways);
8934 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8935 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8936 " int cccccccccccccccccccccccc);",
8937 BreakAlways);
8939 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8940 // to BPPS_AlwaysOnePerLine.
8941 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8942 verifyFormat(
8943 "void someLongFunctionName(\n"
8944 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8945 " int b);",
8946 BreakAlways);
8947 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8948 verifyFormat(
8949 "void someLongFunctionName(\n"
8950 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8951 " int b\n"
8952 ");",
8953 BreakAlways);
8956 TEST_F(FormatTest, FormatsDefinitionBreakAlways) {
8957 FormatStyle BreakAlways = getGoogleStyle();
8958 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
8959 verifyFormat("void f(int a,\n"
8960 " int b) {\n"
8961 " f(a, b);\n"
8962 "}",
8963 BreakAlways);
8965 // Ensure BinPackArguments interact correctly when BinPackParameters is set to
8966 // BPPS_AlwaysOnePerLine.
8967 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8968 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8969 " int cccccccccccccccccccccccc) {\n"
8970 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8971 " cccccccccccccccccccccccc);\n"
8972 "}",
8973 BreakAlways);
8974 BreakAlways.BinPackArguments = false;
8975 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8976 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8977 " int cccccccccccccccccccccccc) {\n"
8978 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8979 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8980 " cccccccccccccccccccccccc);\n"
8981 "}",
8982 BreakAlways);
8984 // Ensure BreakFunctionDefinitionParameters interacts correctly when
8985 // BinPackParameters is set to BPPS_AlwaysOnePerLine.
8986 BreakAlways.BreakFunctionDefinitionParameters = true;
8987 verifyFormat("void f(\n"
8988 " int a,\n"
8989 " int b) {\n"
8990 " f(a, b);\n"
8991 "}",
8992 BreakAlways);
8993 BreakAlways.BreakFunctionDefinitionParameters = false;
8995 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8996 // to BPPS_AlwaysOnePerLine.
8997 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8998 verifyFormat(
8999 "void someLongFunctionName(\n"
9000 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9001 " int b) {\n"
9002 " someLongFunctionName(\n"
9003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
9004 "}",
9005 BreakAlways);
9006 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9007 verifyFormat(
9008 "void someLongFunctionName(\n"
9009 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9010 " int b\n"
9011 ") {\n"
9012 " someLongFunctionName(\n"
9013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n"
9014 " );\n"
9015 "}",
9016 BreakAlways);
9019 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
9020 FormatStyle Style = getLLVMStyleWithColumns(15);
9021 Style.ExperimentalAutoDetectBinPacking = true;
9022 verifyFormat("aaa(aaaa,\n"
9023 " aaaa,\n"
9024 " aaaa);\n"
9025 "aaa(aaaa,\n"
9026 " aaaa,\n"
9027 " aaaa);",
9028 "aaa(aaaa,\n" // one-per-line
9029 " aaaa,\n"
9030 " aaaa );\n"
9031 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9032 Style);
9033 verifyFormat("aaa(aaaa, aaaa,\n"
9034 " aaaa);\n"
9035 "aaa(aaaa, aaaa,\n"
9036 " aaaa);",
9037 "aaa(aaaa, aaaa,\n" // bin-packed
9038 " aaaa );\n"
9039 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9040 Style);
9043 TEST_F(FormatTest, FormatsBuilderPattern) {
9044 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9045 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9046 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9047 " .StartsWith(\".init\", ORDER_INIT)\n"
9048 " .StartsWith(\".fini\", ORDER_FINI)\n"
9049 " .StartsWith(\".hash\", ORDER_HASH)\n"
9050 " .Default(ORDER_TEXT);");
9052 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9053 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9054 verifyFormat("aaaaaaa->aaaaaaa\n"
9055 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9057 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9058 verifyFormat(
9059 "aaaaaaa->aaaaaaa\n"
9060 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9061 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9062 verifyFormat(
9063 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9064 " aaaaaaaaaaaaaa);");
9065 verifyFormat(
9066 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9067 " aaaaaa->aaaaaaaaaaaa()\n"
9068 " ->aaaaaaaaaaaaaaaa(\n"
9069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9070 " ->aaaaaaaaaaaaaaaaa();");
9071 verifyGoogleFormat(
9072 "void f() {\n"
9073 " someo->Add((new util::filetools::Handler(dir))\n"
9074 " ->OnEvent1(NewPermanentCallback(\n"
9075 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9076 " ->OnEvent2(NewPermanentCallback(\n"
9077 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9078 " ->OnEvent3(NewPermanentCallback(\n"
9079 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9080 " ->OnEvent5(NewPermanentCallback(\n"
9081 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9082 " ->OnEvent6(NewPermanentCallback(\n"
9083 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9084 "}");
9086 verifyFormat(
9087 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9088 verifyFormat("aaaaaaaaaaaaaaa()\n"
9089 " .aaaaaaaaaaaaaaa()\n"
9090 " .aaaaaaaaaaaaaaa()\n"
9091 " .aaaaaaaaaaaaaaa()\n"
9092 " .aaaaaaaaaaaaaaa();");
9093 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9094 " .aaaaaaaaaaaaaaa()\n"
9095 " .aaaaaaaaaaaaaaa()\n"
9096 " .aaaaaaaaaaaaaaa();");
9097 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9098 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9099 " .aaaaaaaaaaaaaaa();");
9100 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9101 " ->aaaaaaaaaaaaaae(0)\n"
9102 " ->aaaaaaaaaaaaaaa();");
9104 // Don't linewrap after very short segments.
9105 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9106 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9107 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9108 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9109 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9110 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9111 verifyFormat("aaa()\n"
9112 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9113 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9114 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9116 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9117 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9118 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9119 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9120 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9123 // Prefer not to break after empty parentheses.
9124 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9125 " First->LastNewlineOffset);");
9127 // Prefer not to create "hanging" indents.
9128 verifyFormat(
9129 "return !soooooooooooooome_map\n"
9130 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9131 " .second;");
9132 verifyFormat(
9133 "return aaaaaaaaaaaaaaaa\n"
9134 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9135 " .aaaa(aaaaaaaaaaaaaa);");
9136 // No hanging indent here.
9137 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9139 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9141 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9142 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9143 getLLVMStyleWithColumns(60));
9144 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9145 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9146 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9147 getLLVMStyleWithColumns(59));
9148 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9150 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9152 // Dont break if only closing statements before member call
9153 verifyFormat("test() {\n"
9154 " ([]() -> {\n"
9155 " int b = 32;\n"
9156 " return 3;\n"
9157 " }).foo();\n"
9158 "}");
9159 verifyFormat("test() {\n"
9160 " (\n"
9161 " []() -> {\n"
9162 " int b = 32;\n"
9163 " return 3;\n"
9164 " },\n"
9165 " foo, bar)\n"
9166 " .foo();\n"
9167 "}");
9168 verifyFormat("test() {\n"
9169 " ([]() -> {\n"
9170 " int b = 32;\n"
9171 " return 3;\n"
9172 " })\n"
9173 " .foo()\n"
9174 " .bar();\n"
9175 "}");
9176 verifyFormat("test() {\n"
9177 " ([]() -> {\n"
9178 " int b = 32;\n"
9179 " return 3;\n"
9180 " })\n"
9181 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9182 " \"bbbb\");\n"
9183 "}",
9184 getLLVMStyleWithColumns(30));
9187 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9188 verifyFormat(
9189 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9190 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9191 verifyFormat(
9192 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9193 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9195 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9196 " ccccccccccccccccccccccccc) {\n}");
9197 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9198 " ccccccccccccccccccccccccc) {\n}");
9200 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9201 " ccccccccccccccccccccccccc) {\n}");
9202 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9203 " ccccccccccccccccccccccccc) {\n}");
9205 verifyFormat(
9206 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9207 " ccccccccccccccccccccccccc) {\n}");
9208 verifyFormat(
9209 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9210 " ccccccccccccccccccccccccc) {\n}");
9212 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9213 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9214 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9215 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9216 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9217 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9218 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9219 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9221 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9222 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9223 " aaaaaaaaaaaaaaa != aa) {\n}");
9224 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9225 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9226 " aaaaaaaaaaaaaaa != aa) {\n}");
9229 TEST_F(FormatTest, BreaksAfterAssignments) {
9230 verifyFormat(
9231 "unsigned Cost =\n"
9232 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9233 " SI->getPointerAddressSpaceee());");
9234 verifyFormat(
9235 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9236 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9238 verifyFormat(
9239 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9240 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9241 verifyFormat("unsigned OriginalStartColumn =\n"
9242 " SourceMgr.getSpellingColumnNumber(\n"
9243 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9244 " 1;");
9247 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9248 FormatStyle Style = getLLVMStyle();
9249 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9250 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9251 Style);
9253 Style.PenaltyBreakAssignment = 20;
9254 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9255 " cccccccccccccccccccccccccc;",
9256 Style);
9259 TEST_F(FormatTest, AlignsAfterAssignments) {
9260 verifyFormat(
9261 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9262 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9263 verifyFormat(
9264 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9265 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9266 verifyFormat(
9267 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9268 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9269 verifyFormat(
9270 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9271 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9272 verifyFormat(
9273 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9274 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9275 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9278 TEST_F(FormatTest, AlignsAfterReturn) {
9279 verifyFormat(
9280 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9281 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9282 verifyFormat(
9283 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9284 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9285 verifyFormat(
9286 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9287 " aaaaaaaaaaaaaaaaaaaaaa();");
9288 verifyFormat(
9289 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9290 " aaaaaaaaaaaaaaaaaaaaaa());");
9291 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9293 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9296 verifyFormat("return\n"
9297 " // true if code is one of a or b.\n"
9298 " code == a || code == b;");
9301 TEST_F(FormatTest, AlignsAfterOpenBracket) {
9302 verifyFormat(
9303 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9304 " aaaaaaaaa aaaaaaa) {}");
9305 verifyFormat(
9306 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9307 " aaaaaaaaaaa aaaaaaaaa);");
9308 verifyFormat(
9309 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9310 " aaaaaaaaaaaaaaaaaaaaa));");
9311 FormatStyle Style = getLLVMStyle();
9312 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9313 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9314 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9315 Style);
9316 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9317 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9318 Style);
9319 verifyFormat("SomeLongVariableName->someFunction(\n"
9320 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9321 Style);
9322 verifyFormat(
9323 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9324 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9325 Style);
9326 verifyFormat(
9327 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9328 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9329 Style);
9330 verifyFormat(
9331 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9332 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9333 Style);
9335 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9336 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9337 " b));",
9338 Style);
9340 Style.ColumnLimit = 30;
9341 verifyFormat("for (int foo = 0; foo < FOO;\n"
9342 " ++foo) {\n"
9343 " bar(foo);\n"
9344 "}",
9345 Style);
9346 Style.ColumnLimit = 80;
9348 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9349 Style.BinPackArguments = false;
9350 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9351 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9352 " aaaaaaaaaaa aaaaaaaa,\n"
9353 " aaaaaaaaa aaaaaaa,\n"
9354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9355 Style);
9356 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9357 " aaaaaaaaaaa aaaaaaaaa,\n"
9358 " aaaaaaaaaaa aaaaaaaaa,\n"
9359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9360 Style);
9361 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9362 " aaaaaaaaaaaaaaa,\n"
9363 " aaaaaaaaaaaaaaaaaaaaa,\n"
9364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9365 Style);
9366 verifyFormat(
9367 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9368 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9369 Style);
9370 verifyFormat(
9371 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9372 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9373 Style);
9374 verifyFormat(
9375 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9376 " aaaaaaaaaaaaaaaaaaaaa(\n"
9377 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9378 " aaaaaaaaaaaaaaaa);",
9379 Style);
9380 verifyFormat(
9381 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9382 " aaaaaaaaaaaaaaaaaaaaa(\n"
9383 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9384 " aaaaaaaaaaaaaaaa);",
9385 Style);
9386 verifyFormat(
9387 "fooooooooooo(new BARRRRRRRRR(\n"
9388 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9389 Style);
9390 verifyFormat(
9391 "fooooooooooo(::new BARRRRRRRRR(\n"
9392 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9393 Style);
9394 verifyFormat(
9395 "fooooooooooo(new FOO::BARRRR(\n"
9396 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9397 Style);
9399 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9400 Style.BinPackArguments = false;
9401 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9402 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9403 " aaaaaaaaaaa aaaaaaaa,\n"
9404 " aaaaaaaaa aaaaaaa,\n"
9405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9406 ") {}",
9407 Style);
9408 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9409 " aaaaaaaaaaa aaaaaaaaa,\n"
9410 " aaaaaaaaaaa aaaaaaaaa,\n"
9411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9412 ");",
9413 Style);
9414 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9415 " aaaaaaaaaaaaaaa,\n"
9416 " aaaaaaaaaaaaaaaaaaaaa,\n"
9417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9418 "));",
9419 Style);
9420 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9421 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9422 "));",
9423 Style);
9424 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9425 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9426 "));",
9427 Style);
9428 verifyFormat(
9429 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9430 " aaaaaaaaaaaaaaaaaaaaa(\n"
9431 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9432 " ),\n"
9433 " aaaaaaaaaaaaaaaa\n"
9434 ");",
9435 Style);
9436 verifyFormat(
9437 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9438 " aaaaaaaaaaaaaaaaaaaaa(\n"
9439 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9440 " ) &&\n"
9441 " aaaaaaaaaaaaaaaa\n"
9442 ");",
9443 Style);
9444 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
9445 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9446 "};",
9447 Style);
9449 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9450 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
9451 ") const {\n"
9452 " return true;\n"
9453 "}",
9454 Style);
9455 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9456 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n"
9457 ") const;",
9458 Style);
9459 verifyFormat("void aaaaaaaaa(\n"
9460 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n"
9461 ") const noexcept -> std::vector<of_very_long_type>;",
9462 Style);
9463 verifyFormat(
9464 "x = aaaaaaaaaaaaaaa(\n"
9465 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
9466 ");",
9467 Style);
9468 Style.ColumnLimit = 60;
9469 verifyFormat("auto lambda =\n"
9470 " [&b](\n"
9471 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9472 " ) {};",
9473 Style);
9476 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9477 FormatStyle Style = getLLVMStyleWithColumns(40);
9478 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9479 " bbbbbbbbbbbbbbbbbbbbbb);",
9480 Style);
9481 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9482 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9483 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9484 " bbbbbbbbbbbbbbbbbbbbbb);",
9485 Style);
9486 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9487 Style.AlignOperands = FormatStyle::OAS_Align;
9488 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9489 " bbbbbbbbbbbbbbbbbbbbbb);",
9490 Style);
9491 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9492 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9493 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9494 " bbbbbbbbbbbbbbbbbbbbbb);",
9495 Style);
9498 TEST_F(FormatTest, BreaksConditionalExpressions) {
9499 verifyFormat(
9500 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9501 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9502 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9503 verifyFormat(
9504 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9505 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9506 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9507 verifyFormat(
9508 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9509 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9510 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9511 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9512 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9513 verifyFormat(
9514 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9515 " : aaaaaaaaaaaaa);");
9516 verifyFormat(
9517 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9518 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9519 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9520 " aaaaaaaaaaaaa);");
9521 verifyFormat(
9522 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9523 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9524 " aaaaaaaaaaaaa);");
9525 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9526 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9528 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9530 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9532 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9534 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9536 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9537 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9539 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9541 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9542 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9543 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9544 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9545 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9547 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9548 " : aaaaaaaaaaaaaaaa;");
9549 verifyFormat(
9550 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9551 " ? aaaaaaaaaaaaaaa\n"
9552 " : aaaaaaaaaaaaaaa;");
9553 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9554 " aaaaaaaaa\n"
9555 " ? b\n"
9556 " : c);");
9557 verifyFormat("return aaaa == bbbb\n"
9558 " // comment\n"
9559 " ? aaaa\n"
9560 " : bbbb;");
9561 verifyFormat("unsigned Indent =\n"
9562 " format(TheLine.First,\n"
9563 " IndentForLevel[TheLine.Level] >= 0\n"
9564 " ? IndentForLevel[TheLine.Level]\n"
9565 " : TheLine * 2,\n"
9566 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9567 getLLVMStyleWithColumns(60));
9568 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9569 " ? aaaaaaaaaaaaaaa\n"
9570 " : bbbbbbbbbbbbbbb //\n"
9571 " ? ccccccccccccccc\n"
9572 " : ddddddddddddddd;");
9573 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9574 " ? aaaaaaaaaaaaaaa\n"
9575 " : (bbbbbbbbbbbbbbb //\n"
9576 " ? ccccccccccccccc\n"
9577 " : ddddddddddddddd);");
9578 verifyFormat(
9579 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9580 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9581 " aaaaaaaaaaaaaaaaaaaaa +\n"
9582 " aaaaaaaaaaaaaaaaaaaaa\n"
9583 " : aaaaaaaaaa;");
9584 verifyFormat(
9585 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9586 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9589 FormatStyle NoBinPacking = getLLVMStyle();
9590 NoBinPacking.BinPackArguments = false;
9591 verifyFormat(
9592 "void f() {\n"
9593 " g(aaa,\n"
9594 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9596 " ? aaaaaaaaaaaaaaa\n"
9597 " : aaaaaaaaaaaaaaa);\n"
9598 "}",
9599 NoBinPacking);
9600 verifyFormat(
9601 "void f() {\n"
9602 " g(aaa,\n"
9603 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9605 " ?: aaaaaaaaaaaaaaa);\n"
9606 "}",
9607 NoBinPacking);
9609 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9610 " // comment.\n"
9611 " ccccccccccccccccccccccccccccccccccccccc\n"
9612 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9613 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9615 // Assignments in conditional expressions. Apparently not uncommon :-(.
9616 verifyFormat("return a != b\n"
9617 " // comment\n"
9618 " ? a = b\n"
9619 " : a = b;");
9620 verifyFormat("return a != b\n"
9621 " // comment\n"
9622 " ? a = a != b\n"
9623 " // comment\n"
9624 " ? a = b\n"
9625 " : a\n"
9626 " : a;");
9627 verifyFormat("return a != b\n"
9628 " // comment\n"
9629 " ? a\n"
9630 " : a = a != b\n"
9631 " // comment\n"
9632 " ? a = b\n"
9633 " : a;");
9635 // Chained conditionals
9636 FormatStyle Style = getLLVMStyleWithColumns(70);
9637 Style.AlignOperands = FormatStyle::OAS_Align;
9638 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9639 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9640 " : 3333333333333333;",
9641 Style);
9642 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9643 " : bbbbbbbbbb ? 2222222222222222\n"
9644 " : 3333333333333333;",
9645 Style);
9646 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9647 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9648 " : 3333333333333333;",
9649 Style);
9650 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9651 " : bbbbbbbbbbbbbb ? 222222\n"
9652 " : 333333;",
9653 Style);
9654 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9655 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9656 " : cccccccccccccc ? 3333333333333333\n"
9657 " : 4444444444444444;",
9658 Style);
9659 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9660 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9661 " : 3333333333333333;",
9662 Style);
9663 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9664 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9665 " : (aaa ? bbb : ccc);",
9666 Style);
9667 verifyFormat(
9668 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9669 " : cccccccccccccccccc)\n"
9670 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9671 " : 3333333333333333;",
9672 Style);
9673 verifyFormat(
9674 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9675 " : cccccccccccccccccc)\n"
9676 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9677 " : 3333333333333333;",
9678 Style);
9679 verifyFormat(
9680 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9681 " : dddddddddddddddddd)\n"
9682 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9683 " : 3333333333333333;",
9684 Style);
9685 verifyFormat(
9686 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9687 " : dddddddddddddddddd)\n"
9688 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9689 " : 3333333333333333;",
9690 Style);
9691 verifyFormat(
9692 "return aaaaaaaaa ? 1111111111111111\n"
9693 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9694 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9695 " : dddddddddddddddddd)",
9696 Style);
9697 verifyFormat(
9698 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9699 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9700 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9701 " : cccccccccccccccccc);",
9702 Style);
9703 verifyFormat(
9704 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9705 " : ccccccccccccccc ? dddddddddddddddddd\n"
9706 " : eeeeeeeeeeeeeeeeee)\n"
9707 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9708 " : 3333333333333333;",
9709 Style);
9710 verifyFormat(
9711 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9712 " : ccccccccccccccc ? dddddddddddddddddd\n"
9713 " : eeeeeeeeeeeeeeeeee)\n"
9714 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9715 " : 3333333333333333;",
9716 Style);
9717 verifyFormat(
9718 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9719 " : cccccccccccc ? dddddddddddddddddd\n"
9720 " : eeeeeeeeeeeeeeeeee)\n"
9721 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9722 " : 3333333333333333;",
9723 Style);
9724 verifyFormat(
9725 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9726 " : cccccccccccccccccc\n"
9727 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9728 " : 3333333333333333;",
9729 Style);
9730 verifyFormat(
9731 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9732 " : cccccccccccccccc ? dddddddddddddddddd\n"
9733 " : eeeeeeeeeeeeeeeeee\n"
9734 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9735 " : 3333333333333333;",
9736 Style);
9737 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9738 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9739 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9740 " : eeeeeeeeeeeeeeeeee)\n"
9741 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9742 " : 3333333333333333;",
9743 Style);
9744 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9745 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9746 " : cccccccccccccccc ? dddddddddddddddddd\n"
9747 " : eeeeeeeeeeeeeeeeee\n"
9748 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9749 " : 3333333333333333;",
9750 Style);
9752 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9753 Style.BreakBeforeTernaryOperators = false;
9754 // FIXME: Aligning the question marks is weird given DontAlign.
9755 // Consider disabling this alignment in this case. Also check whether this
9756 // will render the adjustment from https://reviews.llvm.org/D82199
9757 // unnecessary.
9758 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9759 " bbbb ? cccccccccccccccccc :\n"
9760 " ddddd;",
9761 Style);
9763 verifyFormat(
9764 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9765 " /*\n"
9766 " */\n"
9767 " function() {\n"
9768 " try {\n"
9769 " return JJJJJJJJJJJJJJ(\n"
9770 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9771 " }\n"
9772 " } :\n"
9773 " function() {};",
9774 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9775 " /*\n"
9776 " */\n"
9777 " function() {\n"
9778 " try {\n"
9779 " return JJJJJJJJJJJJJJ(\n"
9780 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9781 " }\n"
9782 " } :\n"
9783 " function() {};",
9784 getGoogleStyle(FormatStyle::LK_JavaScript));
9787 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9788 FormatStyle Style = getLLVMStyleWithColumns(70);
9789 Style.BreakBeforeTernaryOperators = false;
9790 verifyFormat(
9791 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9794 Style);
9795 verifyFormat(
9796 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9797 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9799 Style);
9800 verifyFormat(
9801 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9803 Style);
9804 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9805 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9807 Style);
9808 verifyFormat(
9809 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9810 " aaaaaaaaaaaaa);",
9811 Style);
9812 verifyFormat(
9813 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9814 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9816 " aaaaaaaaaaaaa);",
9817 Style);
9818 verifyFormat(
9819 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9820 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9821 " aaaaaaaaaaaaa);",
9822 Style);
9823 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9828 Style);
9829 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9835 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9836 Style);
9837 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9841 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9842 Style);
9843 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9844 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9845 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9846 Style);
9847 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9851 Style);
9852 verifyFormat(
9853 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9854 " aaaaaaaaaaaaaaa :\n"
9855 " aaaaaaaaaaaaaaa;",
9856 Style);
9857 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9858 " aaaaaaaaa ?\n"
9859 " b :\n"
9860 " c);",
9861 Style);
9862 verifyFormat("unsigned Indent =\n"
9863 " format(TheLine.First,\n"
9864 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9865 " IndentForLevel[TheLine.Level] :\n"
9866 " TheLine * 2,\n"
9867 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9868 Style);
9869 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9870 " aaaaaaaaaaaaaaa :\n"
9871 " bbbbbbbbbbbbbbb ? //\n"
9872 " ccccccccccccccc :\n"
9873 " ddddddddddddddd;",
9874 Style);
9875 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9876 " aaaaaaaaaaaaaaa :\n"
9877 " (bbbbbbbbbbbbbbb ? //\n"
9878 " ccccccccccccccc :\n"
9879 " ddddddddddddddd);",
9880 Style);
9881 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9882 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9883 " ccccccccccccccccccccccccccc;",
9884 Style);
9885 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9886 " aaaaa :\n"
9887 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9888 Style);
9890 // Chained conditionals
9891 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9892 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9893 " 3333333333333333;",
9894 Style);
9895 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9896 " bbbbbbbbbb ? 2222222222222222 :\n"
9897 " 3333333333333333;",
9898 Style);
9899 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9900 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9901 " 3333333333333333;",
9902 Style);
9903 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9904 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9905 " 333333;",
9906 Style);
9907 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9908 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9909 " cccccccccccccccc ? 3333333333333333 :\n"
9910 " 4444444444444444;",
9911 Style);
9912 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9913 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9914 " 3333333333333333;",
9915 Style);
9916 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9917 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9918 " (aaa ? bbb : ccc);",
9919 Style);
9920 verifyFormat(
9921 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9922 " cccccccccccccccccc) :\n"
9923 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9924 " 3333333333333333;",
9925 Style);
9926 verifyFormat(
9927 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9928 " cccccccccccccccccc) :\n"
9929 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9930 " 3333333333333333;",
9931 Style);
9932 verifyFormat(
9933 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9934 " dddddddddddddddddd) :\n"
9935 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9936 " 3333333333333333;",
9937 Style);
9938 verifyFormat(
9939 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9940 " dddddddddddddddddd) :\n"
9941 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9942 " 3333333333333333;",
9943 Style);
9944 verifyFormat(
9945 "return aaaaaaaaa ? 1111111111111111 :\n"
9946 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9947 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9948 " dddddddddddddddddd)",
9949 Style);
9950 verifyFormat(
9951 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9952 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9953 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9954 " cccccccccccccccccc);",
9955 Style);
9956 verifyFormat(
9957 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9958 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9959 " eeeeeeeeeeeeeeeeee) :\n"
9960 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9961 " 3333333333333333;",
9962 Style);
9963 verifyFormat(
9964 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9965 " ccccccccccccc ? dddddddddddddddddd :\n"
9966 " eeeeeeeeeeeeeeeeee) :\n"
9967 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9968 " 3333333333333333;",
9969 Style);
9970 verifyFormat(
9971 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9972 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9973 " eeeeeeeeeeeeeeeeee) :\n"
9974 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9975 " 3333333333333333;",
9976 Style);
9977 verifyFormat(
9978 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9979 " cccccccccccccccccc :\n"
9980 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9981 " 3333333333333333;",
9982 Style);
9983 verifyFormat(
9984 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9985 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9986 " eeeeeeeeeeeeeeeeee :\n"
9987 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9988 " 3333333333333333;",
9989 Style);
9990 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9991 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9992 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9993 " eeeeeeeeeeeeeeeeee) :\n"
9994 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9995 " 3333333333333333;",
9996 Style);
9997 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9998 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9999 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
10000 " eeeeeeeeeeeeeeeeee :\n"
10001 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10002 " 3333333333333333;",
10003 Style);
10006 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
10007 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
10008 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
10009 verifyFormat("bool a = true, b = false;");
10011 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
10013 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
10014 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
10015 verifyFormat(
10016 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
10017 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
10018 " d = e && f;");
10019 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
10020 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
10021 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10022 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
10023 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
10024 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
10026 FormatStyle Style = getGoogleStyle();
10027 Style.PointerAlignment = FormatStyle::PAS_Left;
10028 Style.DerivePointerAlignment = false;
10029 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10030 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
10031 " *b = bbbbbbbbbbbbbbbbbbb;",
10032 Style);
10033 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10034 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
10035 Style);
10036 verifyFormat("vector<int*> a, b;", Style);
10037 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
10038 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
10039 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
10040 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
10041 Style);
10042 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
10043 Style);
10044 verifyFormat(
10045 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
10046 Style);
10048 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
10049 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
10050 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
10051 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
10052 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
10053 Style);
10056 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
10057 verifyFormat("arr[foo ? bar : baz];");
10058 verifyFormat("f()[foo ? bar : baz];");
10059 verifyFormat("(a + b)[foo ? bar : baz];");
10060 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10063 TEST_F(FormatTest, AlignsStringLiterals) {
10064 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10065 " \"short literal\");");
10066 verifyFormat(
10067 "looooooooooooooooooooooooongFunction(\n"
10068 " \"short literal\"\n"
10069 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10070 verifyFormat("someFunction(\"Always break between multi-line\"\n"
10071 " \" string literals\",\n"
10072 " also, other, parameters);");
10073 verifyFormat("fun + \"1243\" /* comment */\n"
10074 " \"5678\";",
10075 "fun + \"1243\" /* comment */\n"
10076 " \"5678\";",
10077 getLLVMStyleWithColumns(28));
10078 verifyFormat(
10079 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10080 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10081 " \"aaaaaaaaaaaaaaaa\";",
10082 "aaaaaa ="
10083 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10084 "aaaaaaaaaaaaaaaaaaaaa\" "
10085 "\"aaaaaaaaaaaaaaaa\";");
10086 verifyFormat("a = a + \"a\"\n"
10087 " \"a\"\n"
10088 " \"a\";");
10089 verifyFormat("f(\"a\", \"b\"\n"
10090 " \"c\");");
10092 verifyFormat(
10093 "#define LL_FORMAT \"ll\"\n"
10094 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10095 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10097 verifyFormat("#define A(X) \\\n"
10098 " \"aaaaa\" #X \"bbbbbb\" \\\n"
10099 " \"ccccc\"",
10100 getLLVMStyleWithColumns(23));
10101 verifyFormat("#define A \"def\"\n"
10102 "f(\"abc\" A \"ghi\"\n"
10103 " \"jkl\");");
10105 verifyFormat("f(L\"a\"\n"
10106 " L\"b\");");
10107 verifyFormat("#define A(X) \\\n"
10108 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10109 " L\"ccccc\"",
10110 getLLVMStyleWithColumns(25));
10112 verifyFormat("f(@\"a\"\n"
10113 " @\"b\");");
10114 verifyFormat("NSString s = @\"a\"\n"
10115 " @\"b\"\n"
10116 " @\"c\";");
10117 verifyFormat("NSString s = @\"a\"\n"
10118 " \"b\"\n"
10119 " \"c\";");
10122 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
10123 FormatStyle Style = getLLVMStyle();
10124 Style.ColumnLimit = 60;
10126 // No declarations or definitions should be moved to own line.
10127 Style.BreakAfterReturnType = FormatStyle::RTBS_None;
10128 verifyFormat("class A {\n"
10129 " int f() { return 1; }\n"
10130 " int g();\n"
10131 " long\n"
10132 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10133 "};\n"
10134 "int f() { return 1; }\n"
10135 "int g();\n"
10136 "int foooooooooooooooooooooooooooo::\n"
10137 " baaaaaaaaaaaaaaaaaaaaar();",
10138 Style);
10140 // It is now allowed to break after a short return type if necessary.
10141 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
10142 verifyFormat("class A {\n"
10143 " int f() { return 1; }\n"
10144 " int g();\n"
10145 " long\n"
10146 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10147 "};\n"
10148 "int f() { return 1; }\n"
10149 "int g();\n"
10150 "int\n"
10151 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10152 Style);
10154 // It now must never break after a short return type.
10155 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
10156 verifyFormat("class A {\n"
10157 " int f() { return 1; }\n"
10158 " int g();\n"
10159 " long foooooooooooooooooooooooooooo::\n"
10160 " baaaaaaaaaaaaaaaaaaaar();\n"
10161 "};\n"
10162 "int f() { return 1; }\n"
10163 "int g();\n"
10164 "int foooooooooooooooooooooooooooo::\n"
10165 " baaaaaaaaaaaaaaaaaaaaar();",
10166 Style);
10168 // All declarations and definitions should have the return type moved to its
10169 // own line.
10170 Style.BreakAfterReturnType = FormatStyle::RTBS_All;
10171 Style.TypenameMacros = {"LIST"};
10172 verifyFormat("SomeType\n"
10173 "funcdecl(LIST(uint64_t));",
10174 Style);
10175 verifyFormat("class E {\n"
10176 " int\n"
10177 " f() {\n"
10178 " return 1;\n"
10179 " }\n"
10180 " int\n"
10181 " g();\n"
10182 " long\n"
10183 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10184 "};\n"
10185 "int\n"
10186 "f() {\n"
10187 " return 1;\n"
10188 "}\n"
10189 "int\n"
10190 "g();\n"
10191 "int\n"
10192 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10193 Style);
10195 // Top-level definitions, and no kinds of declarations should have the
10196 // return type moved to its own line.
10197 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10198 verifyFormat("class B {\n"
10199 " int f() { return 1; }\n"
10200 " int g();\n"
10201 "};\n"
10202 "int\n"
10203 "f() {\n"
10204 " return 1;\n"
10205 "}\n"
10206 "int g();",
10207 Style);
10209 // Top-level definitions and declarations should have the return type moved
10210 // to its own line.
10211 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10212 verifyFormat("class C {\n"
10213 " int f() { return 1; }\n"
10214 " int g();\n"
10215 "};\n"
10216 "int\n"
10217 "f() {\n"
10218 " return 1;\n"
10219 "}\n"
10220 "int\n"
10221 "g();\n"
10222 "int\n"
10223 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10224 Style);
10226 // All definitions should have the return type moved to its own line, but no
10227 // kinds of declarations.
10228 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10229 verifyFormat("class D {\n"
10230 " int\n"
10231 " f() {\n"
10232 " return 1;\n"
10233 " }\n"
10234 " int g();\n"
10235 "};\n"
10236 "int\n"
10237 "f() {\n"
10238 " return 1;\n"
10239 "}\n"
10240 "int g();",
10241 Style);
10242 verifyFormat("const char *\n"
10243 "f(void) {\n" // Break here.
10244 " return \"\";\n"
10245 "}\n"
10246 "const char *bar(void);", // No break here.
10247 Style);
10248 verifyFormat("template <class T>\n"
10249 "T *\n"
10250 "f(T &c) {\n" // Break here.
10251 " return NULL;\n"
10252 "}\n"
10253 "template <class T> T *f(T &c);", // No break here.
10254 Style);
10255 verifyFormat("class C {\n"
10256 " int\n"
10257 " operator+() {\n"
10258 " return 1;\n"
10259 " }\n"
10260 " int\n"
10261 " operator()() {\n"
10262 " return 1;\n"
10263 " }\n"
10264 "};",
10265 Style);
10266 verifyFormat("void\n"
10267 "A::operator()() {}\n"
10268 "void\n"
10269 "A::operator>>() {}\n"
10270 "void\n"
10271 "A::operator+() {}\n"
10272 "void\n"
10273 "A::operator*() {}\n"
10274 "void\n"
10275 "A::operator->() {}\n"
10276 "void\n"
10277 "A::operator void *() {}\n"
10278 "void\n"
10279 "A::operator void &() {}\n"
10280 "void\n"
10281 "A::operator void &&() {}\n"
10282 "void\n"
10283 "A::operator char *() {}\n"
10284 "void\n"
10285 "A::operator[]() {}\n"
10286 "void\n"
10287 "A::operator!() {}\n"
10288 "void\n"
10289 "A::operator**() {}\n"
10290 "void\n"
10291 "A::operator<Foo> *() {}\n"
10292 "void\n"
10293 "A::operator<Foo> **() {}\n"
10294 "void\n"
10295 "A::operator<Foo> &() {}\n"
10296 "void\n"
10297 "A::operator void **() {}",
10298 Style);
10299 verifyFormat("constexpr auto\n"
10300 "operator()() const -> reference {}\n"
10301 "constexpr auto\n"
10302 "operator>>() const -> reference {}\n"
10303 "constexpr auto\n"
10304 "operator+() const -> reference {}\n"
10305 "constexpr auto\n"
10306 "operator*() const -> reference {}\n"
10307 "constexpr auto\n"
10308 "operator->() const -> reference {}\n"
10309 "constexpr auto\n"
10310 "operator++() const -> reference {}\n"
10311 "constexpr auto\n"
10312 "operator void *() const -> reference {}\n"
10313 "constexpr auto\n"
10314 "operator void **() const -> reference {}\n"
10315 "constexpr auto\n"
10316 "operator void *() const -> reference {}\n"
10317 "constexpr auto\n"
10318 "operator void &() const -> reference {}\n"
10319 "constexpr auto\n"
10320 "operator void &&() const -> reference {}\n"
10321 "constexpr auto\n"
10322 "operator char *() const -> reference {}\n"
10323 "constexpr auto\n"
10324 "operator!() const -> reference {}\n"
10325 "constexpr auto\n"
10326 "operator[]() const -> reference {}",
10327 Style);
10328 verifyFormat("void *operator new(std::size_t s);", // No break here.
10329 Style);
10330 verifyFormat("void *\n"
10331 "operator new(std::size_t s) {}",
10332 Style);
10333 verifyFormat("void *\n"
10334 "operator delete[](void *ptr) {}",
10335 Style);
10336 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10337 verifyFormat("const char *\n"
10338 "f(void)\n" // Break here.
10339 "{\n"
10340 " return \"\";\n"
10341 "}\n"
10342 "const char *bar(void);", // No break here.
10343 Style);
10344 verifyFormat("template <class T>\n"
10345 "T *\n" // Problem here: no line break
10346 "f(T &c)\n" // Break here.
10347 "{\n"
10348 " return NULL;\n"
10349 "}\n"
10350 "template <class T> T *f(T &c);", // No break here.
10351 Style);
10352 verifyFormat("int\n"
10353 "foo(A<bool> a)\n"
10354 "{\n"
10355 " return a;\n"
10356 "}",
10357 Style);
10358 verifyFormat("int\n"
10359 "foo(A<8> a)\n"
10360 "{\n"
10361 " return a;\n"
10362 "}",
10363 Style);
10364 verifyFormat("int\n"
10365 "foo(A<B<bool>, 8> a)\n"
10366 "{\n"
10367 " return a;\n"
10368 "}",
10369 Style);
10370 verifyFormat("int\n"
10371 "foo(A<B<8>, bool> a)\n"
10372 "{\n"
10373 " return a;\n"
10374 "}",
10375 Style);
10376 verifyFormat("int\n"
10377 "foo(A<B<bool>, bool> a)\n"
10378 "{\n"
10379 " return a;\n"
10380 "}",
10381 Style);
10382 verifyFormat("int\n"
10383 "foo(A<B<8>, 8> a)\n"
10384 "{\n"
10385 " return a;\n"
10386 "}",
10387 Style);
10389 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10390 Style.BraceWrapping.AfterFunction = true;
10391 verifyFormat("int f(i);\n" // No break here.
10392 "int\n" // Break here.
10393 "f(i)\n"
10394 "{\n"
10395 " return i + 1;\n"
10396 "}\n"
10397 "int\n" // Break here.
10398 "f(i)\n"
10399 "{\n"
10400 " return i + 1;\n"
10401 "};",
10402 Style);
10403 verifyFormat("int f(a, b, c);\n" // No break here.
10404 "int\n" // Break here.
10405 "f(a, b, c)\n" // Break here.
10406 "short a, b;\n"
10407 "float c;\n"
10408 "{\n"
10409 " return a + b < c;\n"
10410 "}\n"
10411 "int\n" // Break here.
10412 "f(a, b, c)\n" // Break here.
10413 "short a, b;\n"
10414 "float c;\n"
10415 "{\n"
10416 " return a + b < c;\n"
10417 "};",
10418 Style);
10419 verifyFormat("byte *\n" // Break here.
10420 "f(a)\n" // Break here.
10421 "byte a[];\n"
10422 "{\n"
10423 " return a;\n"
10424 "}",
10425 Style);
10426 verifyFormat("byte *\n"
10427 "f(a)\n"
10428 "byte /* K&R C */ a[];\n"
10429 "{\n"
10430 " return a;\n"
10431 "}\n"
10432 "byte *\n"
10433 "g(p)\n"
10434 "byte /* K&R C */ *p;\n"
10435 "{\n"
10436 " return p;\n"
10437 "}",
10438 Style);
10439 verifyFormat("bool f(int a, int) override;\n"
10440 "Bar g(int a, Bar) final;\n"
10441 "Bar h(a, Bar) final;",
10442 Style);
10443 verifyFormat("int\n"
10444 "f(a)",
10445 Style);
10446 verifyFormat("bool\n"
10447 "f(size_t = 0, bool b = false)\n"
10448 "{\n"
10449 " return !b;\n"
10450 "}",
10451 Style);
10453 // The return breaking style doesn't affect:
10454 // * function and object definitions with attribute-like macros
10455 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10456 " ABSL_GUARDED_BY(mutex) = {};",
10457 getGoogleStyleWithColumns(40));
10458 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10459 " ABSL_GUARDED_BY(mutex); // comment",
10460 getGoogleStyleWithColumns(40));
10461 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10462 " ABSL_GUARDED_BY(mutex1)\n"
10463 " ABSL_GUARDED_BY(mutex2);",
10464 getGoogleStyleWithColumns(40));
10465 verifyFormat("Tttttt f(int a, int b)\n"
10466 " ABSL_GUARDED_BY(mutex1)\n"
10467 " ABSL_GUARDED_BY(mutex2);",
10468 getGoogleStyleWithColumns(40));
10469 // * typedefs
10470 verifyGoogleFormat("typedef ATTR(X) char x;");
10472 Style = getGNUStyle();
10474 // Test for comments at the end of function declarations.
10475 verifyFormat("void\n"
10476 "foo (int a, /*abc*/ int b) // def\n"
10477 "{\n"
10478 "}",
10479 Style);
10481 verifyFormat("void\n"
10482 "foo (int a, /* abc */ int b) /* def */\n"
10483 "{\n"
10484 "}",
10485 Style);
10487 // Definitions that should not break after return type
10488 verifyFormat("void foo (int a, int b); // def", Style);
10489 verifyFormat("void foo (int a, int b); /* def */", Style);
10490 verifyFormat("void foo (int a, int b);", Style);
10493 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10494 FormatStyle NoBreak = getLLVMStyle();
10495 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10496 FormatStyle Break = getLLVMStyle();
10497 Break.AlwaysBreakBeforeMultilineStrings = true;
10498 verifyFormat("aaaa = \"bbbb\"\n"
10499 " \"cccc\";",
10500 NoBreak);
10501 verifyFormat("aaaa =\n"
10502 " \"bbbb\"\n"
10503 " \"cccc\";",
10504 Break);
10505 verifyFormat("aaaa(\"bbbb\"\n"
10506 " \"cccc\");",
10507 NoBreak);
10508 verifyFormat("aaaa(\n"
10509 " \"bbbb\"\n"
10510 " \"cccc\");",
10511 Break);
10512 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10513 " \"cccc\");",
10514 NoBreak);
10515 verifyFormat("aaaa(qqq,\n"
10516 " \"bbbb\"\n"
10517 " \"cccc\");",
10518 Break);
10519 verifyFormat("aaaa(qqq,\n"
10520 " L\"bbbb\"\n"
10521 " L\"cccc\");",
10522 Break);
10523 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10524 " \"bbbb\"));",
10525 Break);
10526 verifyFormat("string s = someFunction(\n"
10527 " \"abc\"\n"
10528 " \"abc\");",
10529 Break);
10531 // As we break before unary operators, breaking right after them is bad.
10532 verifyFormat("string foo = abc ? \"x\"\n"
10533 " \"blah blah blah blah blah blah\"\n"
10534 " : \"y\";",
10535 Break);
10537 // Don't break if there is no column gain.
10538 verifyFormat("f(\"aaaa\"\n"
10539 " \"bbbb\");",
10540 Break);
10542 // Treat literals with escaped newlines like multi-line string literals.
10543 verifyNoChange("x = \"a\\\n"
10544 "b\\\n"
10545 "c\";",
10546 NoBreak);
10547 verifyFormat("xxxx =\n"
10548 " \"a\\\n"
10549 "b\\\n"
10550 "c\";",
10551 "xxxx = \"a\\\n"
10552 "b\\\n"
10553 "c\";",
10554 Break);
10556 verifyFormat("NSString *const kString =\n"
10557 " @\"aaaa\"\n"
10558 " @\"bbbb\";",
10559 "NSString *const kString = @\"aaaa\"\n"
10560 "@\"bbbb\";",
10561 Break);
10563 Break.ColumnLimit = 0;
10564 verifyFormat("const char *hello = \"hello llvm\";", Break);
10567 TEST_F(FormatTest, AlignsPipes) {
10568 verifyFormat(
10569 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10570 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10571 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10572 verifyFormat(
10573 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10574 " << aaaaaaaaaaaaaaaaaaaa;");
10575 verifyFormat(
10576 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10577 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10578 verifyFormat(
10579 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10580 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10581 verifyFormat(
10582 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10583 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10584 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10585 verifyFormat(
10586 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10587 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10588 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10589 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10592 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10593 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10594 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10595 verifyFormat(
10596 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10598 verifyFormat(
10599 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10600 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10602 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10603 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10604 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10606 " aaaaaaaaaaaaaaaaaaaaa)\n"
10607 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10608 verifyFormat("LOG_IF(aaa == //\n"
10609 " bbb)\n"
10610 " << a << b;");
10612 // But sometimes, breaking before the first "<<" is desirable.
10613 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10614 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10615 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10616 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10617 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10618 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10619 " << BEF << IsTemplate << Description << E->getType();");
10620 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10621 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10623 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10624 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10626 " << aaa;");
10628 verifyFormat(
10629 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10630 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10632 // Incomplete string literal.
10633 verifyFormat("llvm::errs() << \"\n"
10634 " << a;",
10635 "llvm::errs() << \"\n<<a;");
10637 verifyFormat("void f() {\n"
10638 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10639 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10640 "}");
10642 // Handle 'endl'.
10643 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10644 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10645 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10647 // Handle '\n'.
10648 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10649 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10650 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10651 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10652 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10653 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10654 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10657 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10658 verifyFormat("return out << \"somepacket = {\\n\"\n"
10659 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10660 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10661 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10662 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10663 " << \"}\";");
10665 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10666 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10667 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10668 verifyFormat(
10669 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10670 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10671 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10672 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10673 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10674 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10675 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10676 verifyFormat(
10677 "void f() {\n"
10678 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10679 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10680 "}");
10682 // Breaking before the first "<<" is generally not desirable.
10683 verifyFormat(
10684 "llvm::errs()\n"
10685 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10686 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10687 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10688 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10689 getLLVMStyleWithColumns(70));
10690 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10691 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10692 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10693 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10694 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10695 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10696 getLLVMStyleWithColumns(70));
10698 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10699 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10700 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10701 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10702 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10703 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10704 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10705 " (aaaa + aaaa);",
10706 getLLVMStyleWithColumns(40));
10707 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10708 " (aaaaaaa + aaaaa));",
10709 getLLVMStyleWithColumns(40));
10710 verifyFormat(
10711 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10712 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10713 " bbbbbbbbbbbbbbbbbbbbbbb);");
10716 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10717 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10719 verifyNoChange("QStringList() << \"foo\"\n"
10720 " << \"bar\";");
10722 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10723 "log_error(log, \"foo\"\n"
10724 " << \"bar\");");
10727 TEST_F(FormatTest, UnderstandsEquals) {
10728 verifyFormat(
10729 "aaaaaaaaaaaaaaaaa =\n"
10730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10731 verifyFormat(
10732 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10734 verifyFormat(
10735 "if (a) {\n"
10736 " f();\n"
10737 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10739 "}");
10741 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10742 " 100000000 + 10000000) {\n}");
10745 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10746 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10747 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10749 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10750 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10752 verifyFormat(
10753 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10754 " Parameter2);");
10756 verifyFormat(
10757 "ShortObject->shortFunction(\n"
10758 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10759 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10761 verifyFormat("loooooooooooooongFunction(\n"
10762 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10764 verifyFormat(
10765 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10766 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10768 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10769 " .WillRepeatedly(Return(SomeValue));");
10770 verifyFormat("void f() {\n"
10771 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10772 " .Times(2)\n"
10773 " .WillRepeatedly(Return(SomeValue));\n"
10774 "}");
10775 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10776 " ccccccccccccccccccccccc);");
10777 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10779 " .aaaaa(aaaaa),\n"
10780 " aaaaaaaaaaaaaaaaaaaaa);");
10781 verifyFormat("void f() {\n"
10782 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10783 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10784 "}");
10785 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10787 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10788 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10789 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10790 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10791 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10792 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10793 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10794 "}");
10796 // Here, it is not necessary to wrap at "." or "->".
10797 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10798 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10799 verifyFormat(
10800 "aaaaaaaaaaa->aaaaaaaaa(\n"
10801 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10802 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10804 verifyFormat(
10805 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10807 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10808 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10809 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10810 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10812 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10814 " .a();");
10816 FormatStyle NoBinPacking = getLLVMStyle();
10817 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
10818 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10819 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10820 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10821 " aaaaaaaaaaaaaaaaaaa,\n"
10822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10823 NoBinPacking);
10825 // If there is a subsequent call, change to hanging indentation.
10826 verifyFormat(
10827 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10828 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10829 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10830 verifyFormat(
10831 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10832 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10833 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10835 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10836 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10838 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10841 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10842 verifyFormat("template <typename T>\n"
10843 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10844 verifyFormat("template <typename T>\n"
10845 "// T should be one of {A, B}.\n"
10846 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10847 verifyFormat(
10848 "template <typename T>\n"
10849 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10850 verifyFormat("template <typename T>\n"
10851 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10852 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10853 verifyFormat(
10854 "template <typename T>\n"
10855 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10856 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10857 verifyFormat(
10858 "template <typename T>\n"
10859 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10860 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10862 verifyFormat("template <typename T>\n"
10863 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10864 " int aaaaaaaaaaaaaaaaaaaaaa);");
10865 verifyFormat(
10866 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10867 " typename T4 = char>\n"
10868 "void f();");
10869 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10870 " template <typename> class cccccccccccccccccccccc,\n"
10871 " typename ddddddddddddd>\n"
10872 "class C {};");
10873 verifyFormat(
10874 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10877 verifyFormat("void f() {\n"
10878 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10879 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10880 "}");
10882 verifyFormat("template <typename T> class C {};");
10883 verifyFormat("template <typename T> void f();");
10884 verifyFormat("template <typename T> void f() {}");
10885 verifyFormat(
10886 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10889 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10892 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10893 getLLVMStyleWithColumns(72));
10894 verifyFormat("static_cast<A< //\n"
10895 " B> *>(\n"
10896 "\n"
10897 ");",
10898 "static_cast<A<//\n"
10899 " B>*>(\n"
10900 "\n"
10901 " );");
10902 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10903 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10905 FormatStyle AlwaysBreak = getLLVMStyle();
10906 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10907 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10908 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10909 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10910 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10911 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10912 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10913 verifyFormat("template <template <typename> class Fooooooo,\n"
10914 " template <typename> class Baaaaaaar>\n"
10915 "struct C {};",
10916 AlwaysBreak);
10917 verifyFormat("template <typename T> // T can be A, B or C.\n"
10918 "struct C {};",
10919 AlwaysBreak);
10920 verifyFormat("template <typename T>\n"
10921 "C(T) noexcept;",
10922 AlwaysBreak);
10923 verifyFormat("template <typename T>\n"
10924 "ClassName(T) noexcept;",
10925 AlwaysBreak);
10926 verifyFormat("template <typename T>\n"
10927 "POOR_NAME(T) noexcept;",
10928 AlwaysBreak);
10929 verifyFormat("template <enum E> class A {\n"
10930 "public:\n"
10931 " E *f();\n"
10932 "};");
10934 FormatStyle NeverBreak = getLLVMStyle();
10935 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
10936 verifyFormat("template <typename T> class C {};", NeverBreak);
10937 verifyFormat("template <typename T> void f();", NeverBreak);
10938 verifyFormat("template <typename T> void f() {}", NeverBreak);
10939 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10940 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10941 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10942 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10943 "bbbbbbbbbbbbbbbbbbbb) {}",
10944 NeverBreak);
10945 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10946 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10947 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10948 NeverBreak);
10949 verifyFormat("template <template <typename> class Fooooooo,\n"
10950 " template <typename> class Baaaaaaar>\n"
10951 "struct C {};",
10952 NeverBreak);
10953 verifyFormat("template <typename T> // T can be A, B or C.\n"
10954 "struct C {};",
10955 NeverBreak);
10956 verifyFormat("template <enum E> class A {\n"
10957 "public:\n"
10958 " E *f();\n"
10959 "};",
10960 NeverBreak);
10961 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10962 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10963 "bbbbbbbbbbbbbbbbbbbb) {}",
10964 NeverBreak);
10966 auto Style = getLLVMStyle();
10967 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
10969 verifyNoChange("template <typename T>\n"
10970 "class C {};",
10971 Style);
10972 verifyFormat("template <typename T> class C {};", Style);
10974 verifyNoChange("template <typename T>\n"
10975 "void f();",
10976 Style);
10977 verifyFormat("template <typename T> void f();", Style);
10979 verifyNoChange("template <typename T>\n"
10980 "void f() {}",
10981 Style);
10982 verifyFormat("template <typename T> void f() {}", Style);
10984 verifyNoChange("template <typename T>\n"
10985 "// T can be A, B or C.\n"
10986 "struct C {};",
10987 Style);
10988 verifyFormat("template <typename T> // T can be A, B or C.\n"
10989 "struct C {};",
10990 Style);
10992 verifyNoChange("template <typename T>\n"
10993 "C(T) noexcept;",
10994 Style);
10995 verifyFormat("template <typename T> C(T) noexcept;", Style);
10997 verifyNoChange("template <enum E>\n"
10998 "class A {\n"
10999 "public:\n"
11000 " E *f();\n"
11001 "};",
11002 Style);
11003 verifyFormat("template <enum E> class A {\n"
11004 "public:\n"
11005 " E *f();\n"
11006 "};",
11007 Style);
11009 verifyNoChange("template <auto x>\n"
11010 "constexpr int simple(int) {\n"
11011 " char c;\n"
11012 " return 1;\n"
11013 "}",
11014 Style);
11015 verifyFormat("template <auto x> constexpr int simple(int) {\n"
11016 " char c;\n"
11017 " return 1;\n"
11018 "}",
11019 Style);
11021 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
11022 verifyNoChange("template <auto x>\n"
11023 "requires(x > 1)\n"
11024 "constexpr int with_req(int) {\n"
11025 " return 1;\n"
11026 "}",
11027 Style);
11028 verifyFormat("template <auto x> requires(x > 1)\n"
11029 "constexpr int with_req(int) {\n"
11030 " return 1;\n"
11031 "}",
11032 Style);
11035 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
11036 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
11037 Style.ColumnLimit = 60;
11038 verifyFormat("// Baseline - no comments.\n"
11039 "template <\n"
11040 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
11041 "void f() {}",
11042 Style);
11044 verifyFormat("template <\n"
11045 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11046 "void f() {}",
11047 "template <\n"
11048 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11049 "void f() {}",
11050 Style);
11052 verifyFormat(
11053 "template <\n"
11054 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11055 "void f() {}",
11056 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11057 "void f() {}",
11058 Style);
11060 verifyFormat("template <\n"
11061 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11062 " // multiline\n"
11063 "void f() {}",
11064 "template <\n"
11065 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11066 " // multiline\n"
11067 "void f() {}",
11068 Style);
11070 verifyFormat(
11071 "template <typename aaaaaaaaaa<\n"
11072 " bbbbbbbbbbbb>::value> // trailing loooong\n"
11073 "void f() {}",
11074 "template <\n"
11075 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11076 "void f() {}",
11077 Style);
11080 TEST_F(FormatTest, WrapsTemplateParameters) {
11081 FormatStyle Style = getLLVMStyle();
11082 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11083 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11084 verifyFormat(
11085 "template <typename... a> struct q {};\n"
11086 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11087 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11088 " y;",
11089 Style);
11090 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11091 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11092 verifyFormat(
11093 "template <typename... a> struct r {};\n"
11094 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11095 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11096 " y;",
11097 Style);
11098 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11099 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11100 verifyFormat("template <typename... a> struct s {};\n"
11101 "extern s<\n"
11102 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11103 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11104 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11105 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11106 " y;",
11107 Style);
11108 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11109 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11110 verifyFormat("template <typename... a> struct t {};\n"
11111 "extern t<\n"
11112 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11113 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11114 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11115 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11116 " y;",
11117 Style);
11120 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
11121 verifyFormat(
11122 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11124 verifyFormat(
11125 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11129 // FIXME: Should we have the extra indent after the second break?
11130 verifyFormat(
11131 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11135 verifyFormat(
11136 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11137 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11139 // Breaking at nested name specifiers is generally not desirable.
11140 verifyFormat(
11141 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11142 " aaaaaaaaaaaaaaaaaaaaaaa);");
11144 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11147 " aaaaaaaaaaaaaaaaaaaaa);",
11148 getLLVMStyleWithColumns(74));
11150 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11152 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11154 verifyFormat(
11155 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11156 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11157 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11158 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11161 TEST_F(FormatTest, UnderstandsTemplateParameters) {
11162 verifyFormat("A<int> a;");
11163 verifyFormat("A<A<A<int>>> a;");
11164 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11165 verifyFormat("bool x = a < 1 || 2 > a;");
11166 verifyFormat("bool x = 5 < f<int>();");
11167 verifyFormat("bool x = f<int>() > 5;");
11168 verifyFormat("bool x = 5 < a<int>::x;");
11169 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11170 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11172 verifyGoogleFormat("A<A<int>> a;");
11173 verifyGoogleFormat("A<A<A<int>>> a;");
11174 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11175 verifyGoogleFormat("A<A<int> > a;");
11176 verifyGoogleFormat("A<A<A<int> > > a;");
11177 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11178 verifyGoogleFormat("A<::A<int>> a;");
11179 verifyGoogleFormat("A<::A> a;");
11180 verifyGoogleFormat("A< ::A> a;");
11181 verifyGoogleFormat("A< ::A<int> > a;");
11182 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11183 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11184 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11185 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11186 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11187 getGoogleStyle());
11189 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11191 // template closer followed by a token that starts with > or =
11192 verifyFormat("bool b = a<1> > 1;");
11193 verifyFormat("bool b = a<1> >= 1;");
11194 verifyFormat("int i = a<1> >> 1;");
11195 FormatStyle Style = getLLVMStyle();
11196 Style.SpaceBeforeAssignmentOperators = false;
11197 verifyFormat("bool b= a<1> == 1;", Style);
11198 verifyFormat("a<int> = 1;", Style);
11199 verifyFormat("a<int> >>= 1;", Style);
11201 verifyFormat("test < a | b >> c;");
11202 verifyFormat("test<test<a | b>> c;");
11203 verifyFormat("test >> a >> b;");
11204 verifyFormat("test << a >> b;");
11206 verifyFormat("f<int>();");
11207 verifyFormat("template <typename T> void f() {}");
11208 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11209 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11210 "sizeof(char)>::type>;");
11211 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11212 verifyFormat("f(a.operator()<A>());");
11213 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11214 " .template operator()<A>());",
11215 getLLVMStyleWithColumns(35));
11216 verifyFormat("bool_constant<a && noexcept(f())>;");
11217 verifyFormat("bool_constant<a || noexcept(f())>;");
11219 verifyFormat("if (std::tuple_size_v<T> > 0)");
11221 // Not template parameters.
11222 verifyFormat("return a < b && c > d;");
11223 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11224 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11225 verifyFormat("void f() {\n"
11226 " while (a < b && c > d) {\n"
11227 " }\n"
11228 "}");
11229 verifyFormat("template <typename... Types>\n"
11230 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11232 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11234 getLLVMStyleWithColumns(60));
11235 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11236 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11237 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11238 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11240 verifyFormat("#define FOO(typeName, realClass) \\\n"
11241 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11242 getLLVMStyleWithColumns(60));
11245 TEST_F(FormatTest, UnderstandsShiftOperators) {
11246 verifyFormat("if (i < x >> 1)");
11247 verifyFormat("while (i < x >> 1)");
11248 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11249 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11250 verifyFormat(
11251 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11252 verifyFormat("Foo.call<Bar<Function>>()");
11253 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11254 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11255 "++i, v = v >> 1)");
11256 verifyFormat("if (w<u<v<x>>, 1>::t)");
11259 TEST_F(FormatTest, BitshiftOperatorWidth) {
11260 verifyFormat("int a = 1 << 2; /* foo\n"
11261 " bar */",
11262 "int a=1<<2; /* foo\n"
11263 " bar */");
11265 verifyFormat("int b = 256 >> 1; /* foo\n"
11266 " bar */",
11267 "int b =256>>1 ; /* foo\n"
11268 " bar */");
11271 TEST_F(FormatTest, UnderstandsBinaryOperators) {
11272 verifyFormat("COMPARE(a, ==, b);");
11273 verifyFormat("auto s = sizeof...(Ts) - 1;");
11276 TEST_F(FormatTest, UnderstandsPointersToMembers) {
11277 verifyFormat("int A::*x;");
11278 verifyFormat("int (S::*func)(void *);");
11279 verifyFormat("void f() { int (S::*func)(void *); }");
11280 verifyFormat("typedef bool *(Class::*Member)() const;");
11281 verifyFormat("void f() {\n"
11282 " (a->*f)();\n"
11283 " a->*x;\n"
11284 " (a.*f)();\n"
11285 " ((*a).*f)();\n"
11286 " a.*x;\n"
11287 "}");
11288 verifyFormat("void f() {\n"
11289 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11290 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11291 "}");
11292 verifyFormat(
11293 "(aaaaaaaaaa->*bbbbbbb)(\n"
11294 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11296 FormatStyle Style = getLLVMStyle();
11297 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11298 verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11299 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
11301 Style.PointerAlignment = FormatStyle::PAS_Left;
11302 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
11303 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11305 Style.PointerAlignment = FormatStyle::PAS_Middle;
11306 verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11307 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11310 TEST_F(FormatTest, UnderstandsUnaryOperators) {
11311 verifyFormat("int a = -2;");
11312 verifyFormat("f(-1, -2, -3);");
11313 verifyFormat("a[-1] = 5;");
11314 verifyFormat("int a = 5 + -2;");
11315 verifyFormat("if (i == -1) {\n}");
11316 verifyFormat("if (i != -1) {\n}");
11317 verifyFormat("if (i > -1) {\n}");
11318 verifyFormat("if (i < -1) {\n}");
11319 verifyFormat("++(a->f());");
11320 verifyFormat("--(a->f());");
11321 verifyFormat("(a->f())++;");
11322 verifyFormat("a[42]++;");
11323 verifyFormat("if (!(a->f())) {\n}");
11324 verifyFormat("if (!+i) {\n}");
11325 verifyFormat("~&a;");
11326 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11327 verifyFormat("sizeof -x");
11328 verifyFormat("sizeof +x");
11329 verifyFormat("sizeof *x");
11330 verifyFormat("sizeof &x");
11331 verifyFormat("delete +x;");
11332 verifyFormat("co_await +x;");
11333 verifyFormat("case *x:");
11334 verifyFormat("case &x:");
11336 verifyFormat("a-- > b;");
11337 verifyFormat("b ? -a : c;");
11338 verifyFormat("n * sizeof char16;");
11339 verifyGoogleFormat("n * alignof char16;");
11340 verifyFormat("sizeof(char);");
11341 verifyGoogleFormat("alignof(char);");
11343 verifyFormat("return -1;");
11344 verifyFormat("throw -1;");
11345 verifyFormat("switch (a) {\n"
11346 "case -1:\n"
11347 " break;\n"
11348 "}");
11349 verifyFormat("#define X -1");
11350 verifyFormat("#define X -kConstant");
11352 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11353 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11355 verifyFormat("int a = /* confusing comment */ -1;");
11356 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11357 verifyFormat("int a = i /* confusing comment */++;");
11359 verifyFormat("co_yield -1;");
11360 verifyFormat("co_return -1;");
11362 // Check that * is not treated as a binary operator when we set
11363 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11364 FormatStyle PASLeftStyle = getLLVMStyle();
11365 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11366 verifyFormat("co_return *a;", PASLeftStyle);
11367 verifyFormat("co_await *a;", PASLeftStyle);
11368 verifyFormat("co_yield *a", PASLeftStyle);
11369 verifyFormat("return *a;", PASLeftStyle);
11372 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11373 verifyFormat("if (!aaaaaaaaaa( // break\n"
11374 " aaaaa)) {\n"
11375 "}");
11376 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11377 " aaaaa));");
11378 verifyFormat("*aaa = aaaaaaa( // break\n"
11379 " bbbbbb);");
11382 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11383 verifyFormat("bool operator<();");
11384 verifyFormat("bool operator>();");
11385 verifyFormat("bool operator=();");
11386 verifyFormat("bool operator==();");
11387 verifyFormat("bool operator!=();");
11388 verifyFormat("int operator+();");
11389 verifyFormat("int operator++();");
11390 verifyFormat("int operator++(int) volatile noexcept;");
11391 verifyFormat("bool operator,();");
11392 verifyFormat("bool operator();");
11393 verifyFormat("bool operator()();");
11394 verifyFormat("bool operator[]();");
11395 verifyFormat("operator bool();");
11396 verifyFormat("operator int();");
11397 verifyFormat("operator void *();");
11398 verifyFormat("operator SomeType<int>();");
11399 verifyFormat("operator SomeType<int, int>();");
11400 verifyFormat("operator SomeType<SomeType<int>>();");
11401 verifyFormat("operator< <>();");
11402 verifyFormat("operator<< <>();");
11403 verifyFormat("< <>");
11405 verifyFormat("void *operator new(std::size_t size);");
11406 verifyFormat("void *operator new[](std::size_t size);");
11407 verifyFormat("void operator delete(void *ptr);");
11408 verifyFormat("void operator delete[](void *ptr);");
11409 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11410 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11411 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11412 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11414 verifyFormat(
11415 "ostream &operator<<(ostream &OutputStream,\n"
11416 " SomeReallyLongType WithSomeReallyLongValue);");
11417 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11418 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11419 " return left.group < right.group;\n"
11420 "}");
11421 verifyFormat("SomeType &operator=(const SomeType &S);");
11422 verifyFormat("f.template operator()<int>();");
11424 verifyGoogleFormat("operator void*();");
11425 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11426 verifyGoogleFormat("operator ::A();");
11428 verifyFormat("using A::operator+;");
11429 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11430 "int i;");
11432 // Calling an operator as a member function.
11433 verifyFormat("void f() { a.operator*(); }");
11434 verifyFormat("void f() { a.operator*(b & b); }");
11435 verifyFormat("void f() { a->operator&(a * b); }");
11436 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11437 verifyFormat("void f() { operator*(a & a); }");
11438 verifyFormat("void f() { operator&(a, b * b); }");
11440 verifyFormat("void f() { return operator()(x) * b; }");
11441 verifyFormat("void f() { return operator[](x) * b; }");
11442 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11443 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11444 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11445 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11446 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11448 verifyFormat("::operator delete(foo);");
11449 verifyFormat("::operator new(n * sizeof(foo));");
11450 verifyFormat("foo() { ::operator delete(foo); }");
11451 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11454 TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11455 verifyFormat("C<&operator- > minus;");
11456 verifyFormat("C<&operator> > gt;");
11457 verifyFormat("C<&operator>= > ge;");
11458 verifyFormat("C<&operator<= > le;");
11459 verifyFormat("C<&operator< <X>> lt;");
11462 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11463 verifyFormat("void A::b() && {}");
11464 verifyFormat("void A::b() && noexcept {}");
11465 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11466 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11467 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11468 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11469 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11470 verifyFormat("Deleted &operator=(const Deleted &) &;");
11471 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11472 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11473 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11474 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11475 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11476 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11477 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11478 verifyFormat("void Fn(T const &) const &;");
11479 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11480 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11481 verifyGoogleFormat("template <typename T>\n"
11482 "void F(T) && = delete;");
11483 verifyFormat("template <typename T> void operator=(T) &;");
11484 verifyFormat("template <typename T> void operator=(T) const &;");
11485 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11486 verifyFormat("template <typename T> void operator=(T) & = default;");
11487 verifyFormat("template <typename T> void operator=(T) &&;");
11488 verifyFormat("template <typename T> void operator=(T) && = delete;");
11489 verifyFormat("template <typename T> void operator=(T) & {}");
11490 verifyFormat("template <typename T> void operator=(T) && {}");
11492 FormatStyle AlignLeft = getLLVMStyle();
11493 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11494 verifyFormat("void A::b() && {}", AlignLeft);
11495 verifyFormat("void A::b() && noexcept {}", AlignLeft);
11496 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11497 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11498 AlignLeft);
11499 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11500 AlignLeft);
11501 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11502 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11503 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11504 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11505 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11506 verifyFormat("auto Function(T) & -> void;", AlignLeft);
11507 verifyFormat("void Fn(T const&) const&;", AlignLeft);
11508 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11509 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11510 AlignLeft);
11511 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11512 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11513 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11514 AlignLeft);
11515 verifyFormat("template <typename T> void operator=(T) & = default;",
11516 AlignLeft);
11517 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11518 verifyFormat("template <typename T> void operator=(T) && = delete;",
11519 AlignLeft);
11520 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11521 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11522 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft);
11524 FormatStyle AlignMiddle = getLLVMStyle();
11525 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11526 verifyFormat("void A::b() && {}", AlignMiddle);
11527 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11528 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11529 AlignMiddle);
11530 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11531 AlignMiddle);
11532 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11533 AlignMiddle);
11534 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11535 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11536 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11537 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11538 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11539 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11540 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11541 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11542 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11543 AlignMiddle);
11544 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11545 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11546 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11547 AlignMiddle);
11548 verifyFormat("template <typename T> void operator=(T) & = default;",
11549 AlignMiddle);
11550 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11551 verifyFormat("template <typename T> void operator=(T) && = delete;",
11552 AlignMiddle);
11553 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11554 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11556 FormatStyle Spaces = getLLVMStyle();
11557 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11558 Spaces.SpacesInParensOptions = {};
11559 Spaces.SpacesInParensOptions.InCStyleCasts = true;
11560 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11561 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11562 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11563 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11565 Spaces.SpacesInParensOptions.InCStyleCasts = false;
11566 Spaces.SpacesInParensOptions.Other = true;
11567 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11568 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11569 Spaces);
11570 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11571 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11573 FormatStyle BreakTemplate = getLLVMStyle();
11574 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11576 verifyFormat("struct f {\n"
11577 " template <class T>\n"
11578 " int &foo(const std::string &str) & noexcept {}\n"
11579 "};",
11580 BreakTemplate);
11582 verifyFormat("struct f {\n"
11583 " template <class T>\n"
11584 " int &foo(const std::string &str) && noexcept {}\n"
11585 "};",
11586 BreakTemplate);
11588 verifyFormat("struct f {\n"
11589 " template <class T>\n"
11590 " int &foo(const std::string &str) const & noexcept {}\n"
11591 "};",
11592 BreakTemplate);
11594 verifyFormat("struct f {\n"
11595 " template <class T>\n"
11596 " int &foo(const std::string &str) const & noexcept {}\n"
11597 "};",
11598 BreakTemplate);
11600 verifyFormat("struct f {\n"
11601 " template <class T>\n"
11602 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11603 "};",
11604 BreakTemplate);
11606 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11607 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11608 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11610 verifyFormat("struct f {\n"
11611 " template <class T>\n"
11612 " int& foo(const std::string& str) & noexcept {}\n"
11613 "};",
11614 AlignLeftBreakTemplate);
11616 verifyFormat("struct f {\n"
11617 " template <class T>\n"
11618 " int& foo(const std::string& str) && noexcept {}\n"
11619 "};",
11620 AlignLeftBreakTemplate);
11622 verifyFormat("struct f {\n"
11623 " template <class T>\n"
11624 " int& foo(const std::string& str) const& noexcept {}\n"
11625 "};",
11626 AlignLeftBreakTemplate);
11628 verifyFormat("struct f {\n"
11629 " template <class T>\n"
11630 " int& foo(const std::string& str) const&& noexcept {}\n"
11631 "};",
11632 AlignLeftBreakTemplate);
11634 verifyFormat("struct f {\n"
11635 " template <class T>\n"
11636 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11637 "};",
11638 AlignLeftBreakTemplate);
11640 // The `&` in `Type&` should not be confused with a trailing `&` of
11641 // DEPRECATED(reason) member function.
11642 verifyFormat("struct f {\n"
11643 " template <class T>\n"
11644 " DEPRECATED(reason)\n"
11645 " Type &foo(arguments) {}\n"
11646 "};",
11647 BreakTemplate);
11649 verifyFormat("struct f {\n"
11650 " template <class T>\n"
11651 " DEPRECATED(reason)\n"
11652 " Type& foo(arguments) {}\n"
11653 "};",
11654 AlignLeftBreakTemplate);
11656 verifyFormat("void (*foopt)(int) = &func;");
11658 FormatStyle DerivePointerAlignment = getLLVMStyle();
11659 DerivePointerAlignment.DerivePointerAlignment = true;
11660 // There's always a space between the function and its trailing qualifiers.
11661 // This isn't evidence for PAS_Right (or for PAS_Left).
11662 std::string Prefix = "void a() &;\n"
11663 "void b() &;\n";
11664 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11665 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11666 // Same if the function is an overloaded operator, and with &&.
11667 Prefix = "void operator()() &&;\n"
11668 "void operator()() &&;\n";
11669 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11670 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11671 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11672 Prefix = "void a() const &;\n"
11673 "void b() const &;\n";
11674 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11677 TEST_F(FormatTest, PointerAlignmentFallback) {
11678 FormatStyle Style = getLLVMStyle();
11679 Style.DerivePointerAlignment = true;
11681 const StringRef Code("int* p;\n"
11682 "int *q;\n"
11683 "int * r;");
11685 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11686 verifyFormat("int *p;\n"
11687 "int *q;\n"
11688 "int *r;",
11689 Code, Style);
11691 Style.PointerAlignment = FormatStyle::PAS_Left;
11692 verifyFormat("int* p;\n"
11693 "int* q;\n"
11694 "int* r;",
11695 Code, Style);
11697 Style.PointerAlignment = FormatStyle::PAS_Middle;
11698 verifyFormat("int * p;\n"
11699 "int * q;\n"
11700 "int * r;",
11701 Code, Style);
11704 TEST_F(FormatTest, UnderstandsNewAndDelete) {
11705 verifyFormat("void f() {\n"
11706 " A *a = new A;\n"
11707 " A *a = new (placement) A;\n"
11708 " delete a;\n"
11709 " delete (A *)a;\n"
11710 "}");
11711 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11712 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11713 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11714 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11715 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11716 verifyFormat("delete[] h->p;");
11717 verifyFormat("delete[] (void *)p;");
11719 verifyFormat("void operator delete(void *foo) ATTRIB;");
11720 verifyFormat("void operator new(void *foo) ATTRIB;");
11721 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11722 verifyFormat("void operator delete(void *ptr) noexcept;");
11724 verifyFormat("void new(link p);\n"
11725 "void delete(link p);",
11726 "void new (link p);\n"
11727 "void delete (link p);");
11729 verifyFormat("{\n"
11730 " p->new();\n"
11731 "}\n"
11732 "{\n"
11733 " p->delete();\n"
11734 "}",
11735 "{\n"
11736 " p->new ();\n"
11737 "}\n"
11738 "{\n"
11739 " p->delete ();\n"
11740 "}");
11742 FormatStyle AfterPlacementOperator = getLLVMStyle();
11743 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11744 EXPECT_TRUE(
11745 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
11746 verifyFormat("new (buf) int;", AfterPlacementOperator);
11747 verifyFormat("struct A {\n"
11748 " int *a;\n"
11749 " A(int *p) : a(new (p) int) {\n"
11750 " new (p) int;\n"
11751 " int *b = new (p) int;\n"
11752 " int *c = new (p) int(3);\n"
11753 " delete (b);\n"
11754 " }\n"
11755 "};",
11756 AfterPlacementOperator);
11757 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11758 verifyFormat("delete (int *)p;", AfterPlacementOperator);
11760 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11761 false;
11762 verifyFormat("new(buf) int;", AfterPlacementOperator);
11763 verifyFormat("struct A {\n"
11764 " int *a;\n"
11765 " A(int *p) : a(new(p) int) {\n"
11766 " new(p) int;\n"
11767 " int *b = new(p) int;\n"
11768 " int *c = new(p) int(3);\n"
11769 " delete(b);\n"
11770 " }\n"
11771 "};",
11772 AfterPlacementOperator);
11773 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11774 verifyFormat("delete (int *)p;", AfterPlacementOperator);
11777 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11778 verifyFormat("int *f(int *a) {}");
11779 verifyFormat("int main(int argc, char **argv) {}");
11780 verifyFormat("Test::Test(int b) : a(b * b) {}");
11781 verifyIndependentOfContext("f(a, *a);");
11782 verifyFormat("void g() { f(*a); }");
11783 verifyIndependentOfContext("int a = b * 10;");
11784 verifyIndependentOfContext("int a = 10 * b;");
11785 verifyIndependentOfContext("int a = b * c;");
11786 verifyIndependentOfContext("int a += b * c;");
11787 verifyIndependentOfContext("int a -= b * c;");
11788 verifyIndependentOfContext("int a *= b * c;");
11789 verifyIndependentOfContext("int a /= b * c;");
11790 verifyIndependentOfContext("int a = *b;");
11791 verifyIndependentOfContext("int a = *b * c;");
11792 verifyIndependentOfContext("int a = b * *c;");
11793 verifyIndependentOfContext("int a = b * (10);");
11794 verifyIndependentOfContext("S << b * (10);");
11795 verifyIndependentOfContext("return 10 * b;");
11796 verifyIndependentOfContext("return *b * *c;");
11797 verifyIndependentOfContext("return a & ~b;");
11798 verifyIndependentOfContext("f(b ? *c : *d);");
11799 verifyIndependentOfContext("int a = b ? *c : *d;");
11800 verifyIndependentOfContext("*b = a;");
11801 verifyIndependentOfContext("a * ~b;");
11802 verifyIndependentOfContext("a * !b;");
11803 verifyIndependentOfContext("a * +b;");
11804 verifyIndependentOfContext("a * -b;");
11805 verifyIndependentOfContext("a * ++b;");
11806 verifyIndependentOfContext("a * --b;");
11807 verifyIndependentOfContext("a[4] * b;");
11808 verifyIndependentOfContext("a[a * a] = 1;");
11809 verifyIndependentOfContext("f() * b;");
11810 verifyIndependentOfContext("a * [self dostuff];");
11811 verifyIndependentOfContext("int x = a * (a + b);");
11812 verifyIndependentOfContext("(a *)(a + b);");
11813 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11814 verifyIndependentOfContext("int *pa = (int *)&a;");
11815 verifyIndependentOfContext("return sizeof(int **);");
11816 verifyIndependentOfContext("return sizeof(int ******);");
11817 verifyIndependentOfContext("return (int **&)a;");
11818 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11819 verifyFormat("void f(Type (*parameter)[10]) {}");
11820 verifyFormat("void f(Type (&parameter)[10]) {}");
11821 verifyGoogleFormat("return sizeof(int**);");
11822 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11823 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11824 verifyFormat("auto a = [](int **&, int ***) {};");
11825 verifyFormat("auto PointerBinding = [](const char *S) {};");
11826 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11827 verifyFormat("[](const decltype(*a) &value) {}");
11828 verifyFormat("[](const typeof(*a) &value) {}");
11829 verifyFormat("[](const _Atomic(a *) &value) {}");
11830 verifyFormat("[](const __underlying_type(a) &value) {}");
11831 verifyFormat("decltype(a * b) F();");
11832 verifyFormat("typeof(a * b) F();");
11833 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11834 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11835 verifyIndependentOfContext("typedef void (*f)(int *a);");
11836 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11837 verifyIndependentOfContext("int i{a * b};");
11838 verifyIndependentOfContext("aaa && aaa->f();");
11839 verifyIndependentOfContext("int x = ~*p;");
11840 verifyFormat("Constructor() : a(a), area(width * height) {}");
11841 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11842 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11843 verifyFormat("void f() { f(a, c * d); }");
11844 verifyFormat("void f() { f(new a(), c * d); }");
11845 verifyFormat("void f(const MyOverride &override);");
11846 verifyFormat("void f(const MyFinal &final);");
11847 verifyIndependentOfContext("bool a = f() && override.f();");
11848 verifyIndependentOfContext("bool a = f() && final.f();");
11850 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11852 verifyIndependentOfContext("A<int *> a;");
11853 verifyIndependentOfContext("A<int **> a;");
11854 verifyIndependentOfContext("A<int *, int *> a;");
11855 verifyIndependentOfContext("A<int *[]> a;");
11856 verifyIndependentOfContext(
11857 "const char *const p = reinterpret_cast<const char *const>(q);");
11858 verifyIndependentOfContext("A<int **, int **> a;");
11859 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11860 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11861 verifyFormat("for (; a && b;) {\n}");
11862 verifyFormat("bool foo = true && [] { return false; }();");
11864 verifyFormat(
11865 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11868 verifyGoogleFormat("int const* a = &b;");
11869 verifyGoogleFormat("**outparam = 1;");
11870 verifyGoogleFormat("*outparam = a * b;");
11871 verifyGoogleFormat("int main(int argc, char** argv) {}");
11872 verifyGoogleFormat("A<int*> a;");
11873 verifyGoogleFormat("A<int**> a;");
11874 verifyGoogleFormat("A<int*, int*> a;");
11875 verifyGoogleFormat("A<int**, int**> a;");
11876 verifyGoogleFormat("f(b ? *c : *d);");
11877 verifyGoogleFormat("int a = b ? *c : *d;");
11878 verifyGoogleFormat("Type* t = **x;");
11879 verifyGoogleFormat("Type* t = *++*x;");
11880 verifyGoogleFormat("*++*x;");
11881 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11882 verifyGoogleFormat("Type* t = x++ * y;");
11883 verifyGoogleFormat(
11884 "const char* const p = reinterpret_cast<const char* const>(q);");
11885 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11886 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11887 verifyGoogleFormat("template <typename T>\n"
11888 "void f(int i = 0, SomeType** temps = NULL);");
11890 FormatStyle Left = getLLVMStyle();
11891 Left.PointerAlignment = FormatStyle::PAS_Left;
11892 verifyFormat("x = *a(x) = *a(y);", Left);
11893 verifyFormat("for (;; *a = b) {\n}", Left);
11894 verifyFormat("return *this += 1;", Left);
11895 verifyFormat("throw *x;", Left);
11896 verifyFormat("delete *x;", Left);
11897 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
11898 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
11899 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
11900 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
11901 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
11902 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
11903 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
11904 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
11905 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
11907 verifyIndependentOfContext("a = *(x + y);");
11908 verifyIndependentOfContext("a = &(x + y);");
11909 verifyIndependentOfContext("*(x + y).call();");
11910 verifyIndependentOfContext("&(x + y)->call();");
11911 verifyFormat("void f() { &(*I).first; }");
11913 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11914 verifyFormat("f(* /* confusing comment */ foo);");
11915 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11916 verifyFormat("void foo(int * // this is the first paramters\n"
11917 " ,\n"
11918 " int second);");
11919 verifyFormat("double term = a * // first\n"
11920 " b;");
11921 verifyFormat(
11922 "int *MyValues = {\n"
11923 " *A, // Operator detection might be confused by the '{'\n"
11924 " *BB // Operator detection might be confused by previous comment\n"
11925 "};");
11927 verifyIndependentOfContext("if (int *a = &b)");
11928 verifyIndependentOfContext("if (int &a = *b)");
11929 verifyIndependentOfContext("if (a & b[i])");
11930 verifyIndependentOfContext("if constexpr (a & b[i])");
11931 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11932 verifyIndependentOfContext("if (a * (b * c))");
11933 verifyIndependentOfContext("if constexpr (a * (b * c))");
11934 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11935 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11936 verifyIndependentOfContext("if (*b[i])");
11937 verifyIndependentOfContext("if (int *a = (&b))");
11938 verifyIndependentOfContext("while (int *a = &b)");
11939 verifyIndependentOfContext("while (a * (b * c))");
11940 verifyIndependentOfContext("size = sizeof *a;");
11941 verifyIndependentOfContext("if (a && (b = c))");
11942 verifyFormat("void f() {\n"
11943 " for (const int &v : Values) {\n"
11944 " }\n"
11945 "}");
11946 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11947 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11948 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11950 verifyFormat("#define A (!a * b)");
11951 verifyFormat("#define MACRO \\\n"
11952 " int *i = a * b; \\\n"
11953 " void f(a *b);",
11954 getLLVMStyleWithColumns(19));
11956 verifyIndependentOfContext("A = new SomeType *[Length];");
11957 verifyIndependentOfContext("A = new SomeType *[Length]();");
11958 verifyIndependentOfContext("T **t = new T *;");
11959 verifyIndependentOfContext("T **t = new T *();");
11960 verifyGoogleFormat("A = new SomeType*[Length]();");
11961 verifyGoogleFormat("A = new SomeType*[Length];");
11962 verifyGoogleFormat("T** t = new T*;");
11963 verifyGoogleFormat("T** t = new T*();");
11965 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11966 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11967 verifyFormat("template <bool a, bool b> "
11968 "typename t::if<x && y>::type f() {}");
11969 verifyFormat("template <int *y> f() {}");
11970 verifyFormat("vector<int *> v;");
11971 verifyFormat("vector<int *const> v;");
11972 verifyFormat("vector<int *const **const *> v;");
11973 verifyFormat("vector<int *volatile> v;");
11974 verifyFormat("vector<a *_Nonnull> v;");
11975 verifyFormat("vector<a *_Nullable> v;");
11976 verifyFormat("vector<a *_Null_unspecified> v;");
11977 verifyFormat("vector<a *__ptr32> v;");
11978 verifyFormat("vector<a *__ptr64> v;");
11979 verifyFormat("vector<a *__capability> v;");
11980 FormatStyle TypeMacros = getLLVMStyle();
11981 TypeMacros.TypenameMacros = {"LIST"};
11982 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
11983 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
11984 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
11985 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
11986 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
11988 FormatStyle CustomQualifier = getLLVMStyle();
11989 // Add identifiers that should not be parsed as a qualifier by default.
11990 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11991 CustomQualifier.AttributeMacros.push_back("_My_qualifier");
11992 CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
11993 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11994 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
11995 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11996 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
11997 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11998 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
11999 verifyFormat("vector<a * _NotAQualifier> v;");
12000 verifyFormat("vector<a * __not_a_qualifier> v;");
12001 verifyFormat("vector<a * b> v;");
12002 verifyFormat("foo<b && false>();");
12003 verifyFormat("foo<b & 1>();");
12004 verifyFormat("foo<b & (1)>();");
12005 verifyFormat("foo<b & (~0)>();");
12006 verifyFormat("foo<b & (true)>();");
12007 verifyFormat("foo<b & ((1))>();");
12008 verifyFormat("foo<b & (/*comment*/ 1)>();");
12009 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12010 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12011 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12012 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12013 verifyFormat(
12014 "template <class T, class = typename std::enable_if<\n"
12015 " std::is_integral<T>::value &&\n"
12016 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12017 "void F();",
12018 getLLVMStyleWithColumns(70));
12019 verifyFormat("template <class T,\n"
12020 " class = typename std::enable_if<\n"
12021 " std::is_integral<T>::value &&\n"
12022 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12023 " class U>\n"
12024 "void F();",
12025 getLLVMStyleWithColumns(70));
12026 verifyFormat(
12027 "template <class T,\n"
12028 " class = typename ::std::enable_if<\n"
12029 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12030 "void F();",
12031 getGoogleStyleWithColumns(68));
12033 FormatStyle Style = getLLVMStyle();
12034 Style.PointerAlignment = FormatStyle::PAS_Left;
12035 verifyFormat("struct {\n"
12036 "}* ptr;",
12037 Style);
12038 verifyFormat("union {\n"
12039 "}* ptr;",
12040 Style);
12041 verifyFormat("class {\n"
12042 "}* ptr;",
12043 Style);
12044 // Don't confuse a multiplication after a brace-initialized expression with
12045 // a class pointer.
12046 verifyFormat("int i = int{42} * 34;", Style);
12047 verifyFormat("struct {\n"
12048 "}&& ptr = {};",
12049 Style);
12050 verifyFormat("union {\n"
12051 "}&& ptr = {};",
12052 Style);
12053 verifyFormat("class {\n"
12054 "}&& ptr = {};",
12055 Style);
12056 verifyFormat("bool b = 3 == int{3} && true;");
12058 Style.PointerAlignment = FormatStyle::PAS_Middle;
12059 verifyFormat("struct {\n"
12060 "} * ptr;",
12061 Style);
12062 verifyFormat("union {\n"
12063 "} * ptr;",
12064 Style);
12065 verifyFormat("class {\n"
12066 "} * ptr;",
12067 Style);
12068 verifyFormat("struct {\n"
12069 "} && ptr = {};",
12070 Style);
12071 verifyFormat("union {\n"
12072 "} && ptr = {};",
12073 Style);
12074 verifyFormat("class {\n"
12075 "} && ptr = {};",
12076 Style);
12078 Style.PointerAlignment = FormatStyle::PAS_Right;
12079 verifyFormat("struct {\n"
12080 "} *ptr;",
12081 Style);
12082 verifyFormat("union {\n"
12083 "} *ptr;",
12084 Style);
12085 verifyFormat("class {\n"
12086 "} *ptr;",
12087 Style);
12088 verifyFormat("struct {\n"
12089 "} &&ptr = {};",
12090 Style);
12091 verifyFormat("union {\n"
12092 "} &&ptr = {};",
12093 Style);
12094 verifyFormat("class {\n"
12095 "} &&ptr = {};",
12096 Style);
12098 Style.PointerAlignment = FormatStyle::PAS_Left;
12099 verifyFormat("delete[] *ptr;", Style);
12100 verifyFormat("delete[] **ptr;", Style);
12101 verifyFormat("delete[] *(ptr);", Style);
12103 verifyIndependentOfContext("MACRO(int *i);");
12104 verifyIndependentOfContext("MACRO(auto *a);");
12105 verifyIndependentOfContext("MACRO(const A *a);");
12106 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12107 verifyIndependentOfContext("MACRO(decltype(A) *a);");
12108 verifyIndependentOfContext("MACRO(typeof(A) *a);");
12109 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12110 verifyIndependentOfContext("MACRO(A *const a);");
12111 verifyIndependentOfContext("MACRO(A *restrict a);");
12112 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12113 verifyIndependentOfContext("MACRO(A *__restrict a);");
12114 verifyIndependentOfContext("MACRO(A *volatile a);");
12115 verifyIndependentOfContext("MACRO(A *__volatile a);");
12116 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12117 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12118 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12119 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12120 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12121 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12122 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12123 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12124 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12125 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12126 verifyIndependentOfContext("MACRO(A *__capability);");
12127 verifyIndependentOfContext("MACRO(A &__capability);");
12128 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12129 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12130 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12131 // a type declaration:
12132 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
12133 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
12134 // Also check that TypenameMacros prevents parsing it as multiplication:
12135 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12136 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
12138 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12139 verifyFormat("void f() { f(float{1}, a * a); }");
12140 verifyFormat("void f() { f(float(1), a * a); }");
12142 verifyFormat("f((void (*)(int))g);");
12143 verifyFormat("f((void (&)(int))g);");
12144 verifyFormat("f((void (^)(int))g);");
12146 // FIXME: Is there a way to make this work?
12147 // verifyIndependentOfContext("MACRO(A *a);");
12148 verifyFormat("MACRO(A &B);");
12149 verifyFormat("MACRO(A *B);");
12150 verifyFormat("void f() { MACRO(A * B); }");
12151 verifyFormat("void f() { MACRO(A & B); }");
12153 // This lambda was mis-formatted after D88956 (treating it as a binop):
12154 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12155 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12156 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12157 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12159 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12160 verifyFormat("return options != nullptr && operator==(*options);");
12162 verifyFormat("#define OP(x) \\\n"
12163 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12164 " return s << a.DebugString(); \\\n"
12165 " }",
12166 "#define OP(x) \\\n"
12167 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12168 " return s << a.DebugString(); \\\n"
12169 " }",
12170 getLLVMStyleWithColumns(50));
12172 verifyFormat("#define FOO \\\n"
12173 " void foo() { \\\n"
12174 " operator+(a * b); \\\n"
12175 " }",
12176 getLLVMStyleWithColumns(25));
12178 // FIXME: We cannot handle this case yet; we might be able to figure out that
12179 // foo<x> d > v; doesn't make sense.
12180 verifyFormat("foo<a<b && c> d> v;");
12182 FormatStyle PointerMiddle = getLLVMStyle();
12183 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12184 verifyFormat("delete *x;", PointerMiddle);
12185 verifyFormat("int * x;", PointerMiddle);
12186 verifyFormat("int *[] x;", PointerMiddle);
12187 verifyFormat("template <int * y> f() {}", PointerMiddle);
12188 verifyFormat("int * f(int * a) {}", PointerMiddle);
12189 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12190 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12191 verifyFormat("A<int *> a;", PointerMiddle);
12192 verifyFormat("A<int **> a;", PointerMiddle);
12193 verifyFormat("A<int *, int *> a;", PointerMiddle);
12194 verifyFormat("A<int *[]> a;", PointerMiddle);
12195 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12196 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12197 verifyFormat("T ** t = new T *;", PointerMiddle);
12199 // Member function reference qualifiers aren't binary operators.
12200 verifyFormat("string // break\n"
12201 "operator()() & {}");
12202 verifyFormat("string // break\n"
12203 "operator()() && {}");
12204 verifyGoogleFormat("template <typename T>\n"
12205 "auto x() & -> int {}");
12207 // Should be binary operators when used as an argument expression (overloaded
12208 // operator invoked as a member function).
12209 verifyFormat("void f() { a.operator()(a * a); }");
12210 verifyFormat("void f() { a->operator()(a & a); }");
12211 verifyFormat("void f() { a.operator()(*a & *a); }");
12212 verifyFormat("void f() { a->operator()(*a * *a); }");
12214 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12215 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12217 verifyFormat("val1 & val2;");
12218 verifyFormat("val1 & val2 & val3;");
12219 verifyFormat("class c {\n"
12220 " void func(type &a) { a & member; }\n"
12221 " anotherType &member;\n"
12222 "}");
12225 TEST_F(FormatTest, UnderstandsAttributes) {
12226 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12227 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12228 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12229 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12230 FormatStyle AfterType = getLLVMStyle();
12231 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12232 verifyFormat("__attribute__((nodebug)) void\n"
12233 "foo() {}",
12234 AfterType);
12235 verifyFormat("__unused void\n"
12236 "foo() {}",
12237 AfterType);
12239 FormatStyle CustomAttrs = getLLVMStyle();
12240 CustomAttrs.AttributeMacros.push_back("__unused");
12241 CustomAttrs.AttributeMacros.push_back("__attr1");
12242 CustomAttrs.AttributeMacros.push_back("__attr2");
12243 CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
12244 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12245 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12246 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12247 // Check that it is parsed as a multiplication without AttributeMacros and
12248 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12249 verifyFormat("vector<SomeType * __attr1> v;");
12250 verifyFormat("vector<SomeType __attr1 *> v;");
12251 verifyFormat("vector<SomeType __attr1 *const> v;");
12252 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12253 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12254 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12255 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12256 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12257 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12258 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12259 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12260 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12261 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12262 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12264 // Check that these are not parsed as function declarations:
12265 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12266 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12267 verifyFormat("SomeType s(InitValue);", CustomAttrs);
12268 verifyFormat("SomeType s{InitValue};", CustomAttrs);
12269 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12270 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12271 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12272 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12273 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12274 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12277 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12278 // Check that qualifiers on pointers don't break parsing of casts.
12279 verifyFormat("x = (foo *const)*v;");
12280 verifyFormat("x = (foo *volatile)*v;");
12281 verifyFormat("x = (foo *restrict)*v;");
12282 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12283 verifyFormat("x = (foo *_Nonnull)*v;");
12284 verifyFormat("x = (foo *_Nullable)*v;");
12285 verifyFormat("x = (foo *_Null_unspecified)*v;");
12286 verifyFormat("x = (foo *_Nonnull)*v;");
12287 verifyFormat("x = (foo *[[clang::attr]])*v;");
12288 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12289 verifyFormat("x = (foo *__ptr32)*v;");
12290 verifyFormat("x = (foo *__ptr64)*v;");
12291 verifyFormat("x = (foo *__capability)*v;");
12293 // Check that we handle multiple trailing qualifiers and skip them all to
12294 // determine that the expression is a cast to a pointer type.
12295 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
12296 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
12297 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12298 StringRef AllQualifiers =
12299 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12300 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12301 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12302 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12304 // Also check that address-of is not parsed as a binary bitwise-and:
12305 verifyFormat("x = (foo *const)&v;");
12306 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12307 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12309 // Check custom qualifiers:
12310 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
12311 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
12312 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12313 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12314 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12315 CustomQualifier);
12316 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12317 CustomQualifier);
12319 // Check that unknown identifiers result in binary operator parsing:
12320 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12321 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12324 TEST_F(FormatTest, UnderstandsSquareAttributes) {
12325 verifyFormat("SomeType s [[unused]] (InitValue);");
12326 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12327 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12328 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12329 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12330 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12331 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12332 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12333 verifyFormat("[[nodiscard]] bool f() { return false; }");
12334 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12335 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12336 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12337 verifyFormat("[[nodiscard]] ::qualified_type f();");
12339 // Make sure we do not mistake attributes for array subscripts.
12340 verifyFormat("int a() {}\n"
12341 "[[unused]] int b() {}");
12342 verifyFormat("NSArray *arr;\n"
12343 "arr[[Foo() bar]];");
12345 // On the other hand, we still need to correctly find array subscripts.
12346 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12348 // Make sure that we do not mistake Objective-C method inside array literals
12349 // as attributes, even if those method names are also keywords.
12350 verifyFormat("@[ [foo bar] ];");
12351 verifyFormat("@[ [NSArray class] ];");
12352 verifyFormat("@[ [foo enum] ];");
12354 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12356 // Make sure we do not parse attributes as lambda introducers.
12357 FormatStyle MultiLineFunctions = getLLVMStyle();
12358 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12359 verifyFormat("[[unused]] int b() {\n"
12360 " return 42;\n"
12361 "}",
12362 MultiLineFunctions);
12365 TEST_F(FormatTest, AttributeClass) {
12366 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
12367 verifyFormat("class S {\n"
12368 " S(S&&) = default;\n"
12369 "};",
12370 Style);
12371 verifyFormat("class [[nodiscard]] S {\n"
12372 " S(S&&) = default;\n"
12373 "};",
12374 Style);
12375 verifyFormat("class __attribute((maybeunused)) S {\n"
12376 " S(S&&) = default;\n"
12377 "};",
12378 Style);
12379 verifyFormat("struct S {\n"
12380 " S(S&&) = default;\n"
12381 "};",
12382 Style);
12383 verifyFormat("struct [[nodiscard]] S {\n"
12384 " S(S&&) = default;\n"
12385 "};",
12386 Style);
12389 TEST_F(FormatTest, AttributesAfterMacro) {
12390 FormatStyle Style = getLLVMStyle();
12391 verifyFormat("MACRO;\n"
12392 "__attribute__((maybe_unused)) int foo() {\n"
12393 " //...\n"
12394 "}");
12396 verifyFormat("MACRO;\n"
12397 "[[nodiscard]] int foo() {\n"
12398 " //...\n"
12399 "}");
12401 verifyNoChange("MACRO\n\n"
12402 "__attribute__((maybe_unused)) int foo() {\n"
12403 " //...\n"
12404 "}");
12406 verifyNoChange("MACRO\n\n"
12407 "[[nodiscard]] int foo() {\n"
12408 " //...\n"
12409 "}");
12412 TEST_F(FormatTest, AttributePenaltyBreaking) {
12413 FormatStyle Style = getLLVMStyle();
12414 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12415 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12416 Style);
12417 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12418 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12419 Style);
12420 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12421 "shared_ptr<ALongTypeName> &C d) {\n}",
12422 Style);
12425 TEST_F(FormatTest, UnderstandsEllipsis) {
12426 FormatStyle Style = getLLVMStyle();
12427 verifyFormat("int printf(const char *fmt, ...);");
12428 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12429 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12431 verifyFormat("template <int *...PP> a;", Style);
12433 Style.PointerAlignment = FormatStyle::PAS_Left;
12434 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12436 verifyFormat("template <int*... PP> a;", Style);
12438 Style.PointerAlignment = FormatStyle::PAS_Middle;
12439 verifyFormat("template <int *... PP> a;", Style);
12442 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12443 verifyFormat("int *a;\n"
12444 "int *a;\n"
12445 "int *a;",
12446 "int *a;\n"
12447 "int* a;\n"
12448 "int *a;",
12449 getGoogleStyle());
12450 verifyFormat("int* a;\n"
12451 "int* a;\n"
12452 "int* a;",
12453 "int* a;\n"
12454 "int* a;\n"
12455 "int *a;",
12456 getGoogleStyle());
12457 verifyFormat("int *a;\n"
12458 "int *a;\n"
12459 "int *a;",
12460 "int *a;\n"
12461 "int * a;\n"
12462 "int * a;",
12463 getGoogleStyle());
12464 verifyFormat("auto x = [] {\n"
12465 " int *a;\n"
12466 " int *a;\n"
12467 " int *a;\n"
12468 "};",
12469 "auto x=[]{int *a;\n"
12470 "int * a;\n"
12471 "int * a;};",
12472 getGoogleStyle());
12475 TEST_F(FormatTest, UnderstandsRvalueReferences) {
12476 verifyFormat("int f(int &&a) {}");
12477 verifyFormat("int f(int a, char &&b) {}");
12478 verifyFormat("void f() { int &&a = b; }");
12479 verifyGoogleFormat("int f(int a, char&& b) {}");
12480 verifyGoogleFormat("void f() { int&& a = b; }");
12482 verifyIndependentOfContext("A<int &&> a;");
12483 verifyIndependentOfContext("A<int &&, int &&> a;");
12484 verifyGoogleFormat("A<int&&> a;");
12485 verifyGoogleFormat("A<int&&, int&&> a;");
12487 // Not rvalue references:
12488 verifyFormat("template <bool B, bool C> class A {\n"
12489 " static_assert(B && C, \"Something is wrong\");\n"
12490 "};");
12491 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12492 verifyFormat("template <typename T> struct S {\n"
12493 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12494 "};");
12495 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12496 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12497 verifyFormat("#define A(a, b) (a && b)");
12500 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
12501 verifyFormat("void f() {\n"
12502 " x[aaaaaaaaa -\n"
12503 " b] = 23;\n"
12504 "}",
12505 getLLVMStyleWithColumns(15));
12508 TEST_F(FormatTest, FormatsCasts) {
12509 verifyFormat("Type *A = static_cast<Type *>(P);");
12510 verifyFormat("static_cast<Type *>(P);");
12511 verifyFormat("static_cast<Type &>(Fun)(Args);");
12512 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12513 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12514 // Check that static_cast<...>(...) does not require the next token to be on
12515 // the same line.
12516 verifyFormat("some_loooong_output << something_something__ << "
12517 "static_cast<const void *>(R)\n"
12518 " << something;");
12519 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12520 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12521 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12522 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12523 verifyFormat("Type *A = (Type *)P;");
12524 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12525 verifyFormat("int a = (int)(2.0f);");
12526 verifyFormat("int a = (int)2.0f;");
12527 verifyFormat("x[(int32)y];");
12528 verifyFormat("x = (int32)y;");
12529 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12530 verifyFormat("int a = (int)*b;");
12531 verifyFormat("int a = (int)2.0f;");
12532 verifyFormat("int a = (int)~0;");
12533 verifyFormat("int a = (int)++a;");
12534 verifyFormat("int a = (int)sizeof(int);");
12535 verifyFormat("int a = (int)+2;");
12536 verifyFormat("my_int a = (my_int)2.0f;");
12537 verifyFormat("my_int a = (my_int)sizeof(int);");
12538 verifyFormat("return (my_int)aaa;");
12539 verifyFormat("throw (my_int)aaa;");
12540 verifyFormat("#define x ((int)-1)");
12541 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12542 verifyFormat("#define p(q) ((int *)&q)");
12543 verifyFormat("fn(a)(b) + 1;");
12545 verifyFormat("void f() { my_int a = (my_int)*b; }");
12546 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12547 verifyFormat("my_int a = (my_int)~0;");
12548 verifyFormat("my_int a = (my_int)++a;");
12549 verifyFormat("my_int a = (my_int)-2;");
12550 verifyFormat("my_int a = (my_int)1;");
12551 verifyFormat("my_int a = (my_int *)1;");
12552 verifyFormat("my_int a = (const my_int)-1;");
12553 verifyFormat("my_int a = (const my_int *)-1;");
12554 verifyFormat("my_int a = (my_int)(my_int)-1;");
12555 verifyFormat("my_int a = (ns::my_int)-2;");
12556 verifyFormat("case (my_int)ONE:");
12557 verifyFormat("auto x = (X)this;");
12558 // Casts in Obj-C style calls used to not be recognized as such.
12559 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12561 // FIXME: single value wrapped with paren will be treated as cast.
12562 verifyFormat("void f(int i = (kValue)*kMask) {}");
12564 verifyFormat("{\n"
12565 " (void)F;\n"
12566 "}");
12568 // Don't break after a cast's
12569 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12570 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12571 " bbbbbbbbbbbbbbbbbbbbbb);");
12573 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12574 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12575 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12576 verifyFormat("bool *y = (bool *)(void *)(x);");
12577 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12578 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12579 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12580 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12582 // These are not casts.
12583 verifyFormat("void f(int *) {}");
12584 verifyFormat("f(foo)->b;");
12585 verifyFormat("f(foo).b;");
12586 verifyFormat("f(foo)(b);");
12587 verifyFormat("f(foo)[b];");
12588 verifyFormat("[](foo) { return 4; }(bar);");
12589 verifyFormat("(*funptr)(foo)[4];");
12590 verifyFormat("funptrs[4](foo)[4];");
12591 verifyFormat("void f(int *);");
12592 verifyFormat("void f(int *) = 0;");
12593 verifyFormat("void f(SmallVector<int>) {}");
12594 verifyFormat("void f(SmallVector<int>);");
12595 verifyFormat("void f(SmallVector<int>) = 0;");
12596 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12597 verifyFormat("int a = sizeof(int) * b;");
12598 verifyGoogleFormat("int a = alignof(int) * b;");
12599 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12600 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12601 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12603 // These are not casts, but at some point were confused with casts.
12604 verifyFormat("virtual void foo(int *) override;");
12605 verifyFormat("virtual void foo(char &) const;");
12606 verifyFormat("virtual void foo(int *a, char *) const;");
12607 verifyFormat("int a = sizeof(int *) + b;");
12608 verifyGoogleFormat("int a = alignof(int *) + b;");
12609 verifyFormat("bool b = f(g<int>) && c;");
12610 verifyFormat("typedef void (*f)(int i) func;");
12611 verifyFormat("void operator++(int) noexcept;");
12612 verifyFormat("void operator++(int &) noexcept;");
12613 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12614 "&) noexcept;");
12615 verifyFormat(
12616 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12617 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12618 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12619 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12620 verifyFormat("void operator delete(foo &) noexcept;");
12621 verifyFormat("void operator delete(foo) noexcept;");
12622 verifyFormat("void operator delete(int) noexcept;");
12623 verifyFormat("void operator delete(int &) noexcept;");
12624 verifyFormat("void operator delete(int &) volatile noexcept;");
12625 verifyFormat("void operator delete(int &) const");
12626 verifyFormat("void operator delete(int &) = default");
12627 verifyFormat("void operator delete(int &) = delete");
12628 verifyFormat("void operator delete(int &) [[noreturn]]");
12629 verifyFormat("void operator delete(int &) throw();");
12630 verifyFormat("void operator delete(int &) throw(int);");
12631 verifyFormat("auto operator delete(int &) -> int;");
12632 verifyFormat("auto operator delete(int &) override");
12633 verifyFormat("auto operator delete(int &) final");
12635 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12636 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12637 // FIXME: The indentation here is not ideal.
12638 verifyFormat(
12639 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12640 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12641 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12644 TEST_F(FormatTest, FormatsFunctionTypes) {
12645 verifyFormat("A<bool()> a;");
12646 verifyFormat("A<SomeType()> a;");
12647 verifyFormat("A<void (*)(int, std::string)> a;");
12648 verifyFormat("A<void *(int)>;");
12649 verifyFormat("void *(*a)(int *, SomeType *);");
12650 verifyFormat("int (*func)(void *);");
12651 verifyFormat("void f() { int (*func)(void *); }");
12652 verifyFormat("template <class CallbackClass>\n"
12653 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12655 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12656 verifyGoogleFormat("void* (*a)(int);");
12657 verifyGoogleFormat(
12658 "template <class CallbackClass>\n"
12659 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12661 // Other constructs can look somewhat like function types:
12662 verifyFormat("A<sizeof(*x)> a;");
12663 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12664 verifyFormat("some_var = function(*some_pointer_var)[0];");
12665 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12666 verifyFormat("int x = f(&h)();");
12667 verifyFormat("returnsFunction(&param1, &param2)(param);");
12668 verifyFormat("std::function<\n"
12669 " LooooooooooongTemplatedType<\n"
12670 " SomeType>*(\n"
12671 " LooooooooooooooooongType type)>\n"
12672 " function;",
12673 getGoogleStyleWithColumns(40));
12676 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12677 verifyFormat("A (*foo_)[6];");
12678 verifyFormat("vector<int> (*foo_)[6];");
12681 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12682 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12683 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12684 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12685 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12686 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12687 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12689 // Different ways of ()-initializiation.
12690 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12691 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12692 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12693 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12694 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12695 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12696 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12697 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12699 // Lambdas should not confuse the variable declaration heuristic.
12700 verifyFormat("LooooooooooooooooongType\n"
12701 " variable(nullptr, [](A *a) {});",
12702 getLLVMStyleWithColumns(40));
12705 TEST_F(FormatTest, BreaksLongDeclarations) {
12706 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12707 " AnotherNameForTheLongType;");
12708 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12710 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12711 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12712 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12713 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12714 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12715 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12716 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12717 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12718 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12719 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12720 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12721 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12722 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12723 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12724 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12725 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12726 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12727 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12728 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12729 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12730 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12731 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12732 FormatStyle Indented = getLLVMStyle();
12733 Indented.IndentWrappedFunctionNames = true;
12734 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12735 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12736 Indented);
12737 verifyFormat(
12738 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12739 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12740 Indented);
12741 verifyFormat(
12742 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12743 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12744 Indented);
12745 verifyFormat(
12746 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12747 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12748 Indented);
12750 // FIXME: Without the comment, this breaks after "(".
12751 verifyGoogleFormat(
12752 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12753 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12755 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12756 " int LoooooooooooooooooooongParam2) {}");
12757 verifyFormat(
12758 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12759 " SourceLocation L, IdentifierIn *II,\n"
12760 " Type *T) {}");
12761 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12762 "ReallyReaaallyLongFunctionName(\n"
12763 " const std::string &SomeParameter,\n"
12764 " const SomeType<string, SomeOtherTemplateParameter>\n"
12765 " &ReallyReallyLongParameterName,\n"
12766 " const SomeType<string, SomeOtherTemplateParameter>\n"
12767 " &AnotherLongParameterName) {}");
12768 verifyFormat("template <typename A>\n"
12769 "SomeLoooooooooooooooooooooongType<\n"
12770 " typename some_namespace::SomeOtherType<A>::Type>\n"
12771 "Function() {}");
12773 verifyGoogleFormat(
12774 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12775 " aaaaaaaaaaaaaaaaaaaaaaa;");
12776 verifyGoogleFormat(
12777 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12778 " SourceLocation L) {}");
12779 verifyGoogleFormat(
12780 "some_namespace::LongReturnType\n"
12781 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12782 " int first_long_parameter, int second_parameter) {}");
12784 verifyGoogleFormat("template <typename T>\n"
12785 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12786 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12787 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12788 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12790 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12791 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12792 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12793 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12794 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12795 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12796 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12797 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12801 verifyFormat("template <typename T> // Templates on own line.\n"
12802 "static int // Some comment.\n"
12803 "MyFunction(int a);");
12806 TEST_F(FormatTest, FormatsAccessModifiers) {
12807 FormatStyle Style = getLLVMStyle();
12808 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
12809 FormatStyle::ELBAMS_LogicalBlock);
12810 verifyFormat("struct foo {\n"
12811 "private:\n"
12812 " void f() {}\n"
12813 "\n"
12814 "private:\n"
12815 " int i;\n"
12816 "\n"
12817 "protected:\n"
12818 " int j;\n"
12819 "};",
12820 Style);
12821 verifyFormat("struct foo {\n"
12822 "private:\n"
12823 " void f() {}\n"
12824 "\n"
12825 "private:\n"
12826 " int i;\n"
12827 "\n"
12828 "protected:\n"
12829 " int j;\n"
12830 "};",
12831 "struct foo {\n"
12832 "private:\n"
12833 " void f() {}\n"
12834 "private:\n"
12835 " int i;\n"
12836 "protected:\n"
12837 " int j;\n"
12838 "};",
12839 Style);
12840 verifyFormat("struct foo { /* comment */\n"
12841 "private:\n"
12842 " int i;\n"
12843 " // comment\n"
12844 "private:\n"
12845 " int j;\n"
12846 "};",
12847 Style);
12848 verifyFormat("struct foo {\n"
12849 "#ifdef FOO\n"
12850 "#endif\n"
12851 "private:\n"
12852 " int i;\n"
12853 "#ifdef FOO\n"
12854 "private:\n"
12855 "#endif\n"
12856 " int j;\n"
12857 "};",
12858 Style);
12859 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12860 verifyFormat("struct foo {\n"
12861 "private:\n"
12862 " void f() {}\n"
12863 "private:\n"
12864 " int i;\n"
12865 "protected:\n"
12866 " int j;\n"
12867 "};",
12868 Style);
12869 verifyFormat("struct foo {\n"
12870 "private:\n"
12871 " void f() {}\n"
12872 "private:\n"
12873 " int i;\n"
12874 "protected:\n"
12875 " int j;\n"
12876 "};",
12877 "struct foo {\n"
12878 "\n"
12879 "private:\n"
12880 " void f() {}\n"
12881 "\n"
12882 "private:\n"
12883 " int i;\n"
12884 "\n"
12885 "protected:\n"
12886 " int j;\n"
12887 "};",
12888 Style);
12889 verifyFormat("struct foo { /* comment */\n"
12890 "private:\n"
12891 " int i;\n"
12892 " // comment\n"
12893 "private:\n"
12894 " int j;\n"
12895 "};",
12896 "struct foo { /* comment */\n"
12897 "\n"
12898 "private:\n"
12899 " int i;\n"
12900 " // comment\n"
12901 "\n"
12902 "private:\n"
12903 " int j;\n"
12904 "};",
12905 Style);
12906 verifyFormat("struct foo {\n"
12907 "#ifdef FOO\n"
12908 "#endif\n"
12909 "private:\n"
12910 " int i;\n"
12911 "#ifdef FOO\n"
12912 "private:\n"
12913 "#endif\n"
12914 " int j;\n"
12915 "};",
12916 "struct foo {\n"
12917 "#ifdef FOO\n"
12918 "#endif\n"
12919 "\n"
12920 "private:\n"
12921 " int i;\n"
12922 "#ifdef FOO\n"
12923 "\n"
12924 "private:\n"
12925 "#endif\n"
12926 " int j;\n"
12927 "};",
12928 Style);
12929 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12930 verifyFormat("struct foo {\n"
12931 "private:\n"
12932 " void f() {}\n"
12933 "\n"
12934 "private:\n"
12935 " int i;\n"
12936 "\n"
12937 "protected:\n"
12938 " int j;\n"
12939 "};",
12940 Style);
12941 verifyFormat("struct foo {\n"
12942 "private:\n"
12943 " void f() {}\n"
12944 "\n"
12945 "private:\n"
12946 " int i;\n"
12947 "\n"
12948 "protected:\n"
12949 " int j;\n"
12950 "};",
12951 "struct foo {\n"
12952 "private:\n"
12953 " void f() {}\n"
12954 "private:\n"
12955 " int i;\n"
12956 "protected:\n"
12957 " int j;\n"
12958 "};",
12959 Style);
12960 verifyFormat("struct foo { /* comment */\n"
12961 "private:\n"
12962 " int i;\n"
12963 " // comment\n"
12964 "\n"
12965 "private:\n"
12966 " int j;\n"
12967 "};",
12968 Style);
12969 verifyFormat("struct foo {\n"
12970 "#ifdef FOO\n"
12971 "#endif\n"
12972 "\n"
12973 "private:\n"
12974 " int i;\n"
12975 "#ifdef FOO\n"
12976 "\n"
12977 "private:\n"
12978 "#endif\n"
12979 " int j;\n"
12980 "};",
12981 "struct foo {\n"
12982 "#ifdef FOO\n"
12983 "#endif\n"
12984 "private:\n"
12985 " int i;\n"
12986 "#ifdef FOO\n"
12987 "private:\n"
12988 "#endif\n"
12989 " int j;\n"
12990 "};",
12991 Style);
12992 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12993 verifyNoChange("struct foo {\n"
12994 "\n"
12995 "private:\n"
12996 " void f() {}\n"
12997 "\n"
12998 "private:\n"
12999 " int i;\n"
13000 "\n"
13001 "protected:\n"
13002 " int j;\n"
13003 "};",
13004 Style);
13005 verifyFormat("struct foo {\n"
13006 "private:\n"
13007 " void f() {}\n"
13008 "private:\n"
13009 " int i;\n"
13010 "protected:\n"
13011 " int j;\n"
13012 "};",
13013 Style);
13014 verifyNoChange("struct foo { /* comment */\n"
13015 "\n"
13016 "private:\n"
13017 " int i;\n"
13018 " // comment\n"
13019 "\n"
13020 "private:\n"
13021 " int j;\n"
13022 "};",
13023 Style);
13024 verifyFormat("struct foo { /* comment */\n"
13025 "private:\n"
13026 " int i;\n"
13027 " // comment\n"
13028 "private:\n"
13029 " int j;\n"
13030 "};",
13031 Style);
13032 verifyNoChange("struct foo {\n"
13033 "#ifdef FOO\n"
13034 "#endif\n"
13035 "\n"
13036 "private:\n"
13037 " int i;\n"
13038 "#ifdef FOO\n"
13039 "\n"
13040 "private:\n"
13041 "#endif\n"
13042 " int j;\n"
13043 "};",
13044 Style);
13045 verifyFormat("struct foo {\n"
13046 "#ifdef FOO\n"
13047 "#endif\n"
13048 "private:\n"
13049 " int i;\n"
13050 "#ifdef FOO\n"
13051 "private:\n"
13052 "#endif\n"
13053 " int j;\n"
13054 "};",
13055 Style);
13056 Style.AttributeMacros.push_back("FOO");
13057 Style.AttributeMacros.push_back("BAR");
13058 verifyFormat("struct foo {\n"
13059 "FOO private:\n"
13060 " int i;\n"
13061 "BAR(x) protected:\n"
13062 " int j;\n"
13063 "};",
13064 Style);
13066 FormatStyle NoEmptyLines = getLLVMStyle();
13067 NoEmptyLines.MaxEmptyLinesToKeep = 0;
13068 verifyFormat("struct foo {\n"
13069 "private:\n"
13070 " void f() {}\n"
13071 "\n"
13072 "private:\n"
13073 " int i;\n"
13074 "\n"
13075 "public:\n"
13076 "protected:\n"
13077 " int j;\n"
13078 "};",
13079 NoEmptyLines);
13081 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13082 verifyFormat("struct foo {\n"
13083 "private:\n"
13084 " void f() {}\n"
13085 "private:\n"
13086 " int i;\n"
13087 "public:\n"
13088 "protected:\n"
13089 " int j;\n"
13090 "};",
13091 NoEmptyLines);
13093 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13094 verifyFormat("struct foo {\n"
13095 "private:\n"
13096 " void f() {}\n"
13097 "\n"
13098 "private:\n"
13099 " int i;\n"
13100 "\n"
13101 "public:\n"
13102 "\n"
13103 "protected:\n"
13104 " int j;\n"
13105 "};",
13106 NoEmptyLines);
13109 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
13111 FormatStyle Style = getLLVMStyle();
13112 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
13113 verifyFormat("struct foo {\n"
13114 "private:\n"
13115 " void f() {}\n"
13116 "\n"
13117 "private:\n"
13118 " int i;\n"
13119 "\n"
13120 "protected:\n"
13121 " int j;\n"
13122 "};",
13123 Style);
13125 // Check if lines are removed.
13126 verifyFormat("struct foo {\n"
13127 "private:\n"
13128 " void f() {}\n"
13129 "\n"
13130 "private:\n"
13131 " int i;\n"
13132 "\n"
13133 "protected:\n"
13134 " int j;\n"
13135 "};",
13136 "struct foo {\n"
13137 "private:\n"
13138 "\n"
13139 " void f() {}\n"
13140 "\n"
13141 "private:\n"
13142 "\n"
13143 " int i;\n"
13144 "\n"
13145 "protected:\n"
13146 "\n"
13147 " int j;\n"
13148 "};",
13149 Style);
13151 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13152 verifyFormat("struct foo {\n"
13153 "private:\n"
13154 "\n"
13155 " void f() {}\n"
13156 "\n"
13157 "private:\n"
13158 "\n"
13159 " int i;\n"
13160 "\n"
13161 "protected:\n"
13162 "\n"
13163 " int j;\n"
13164 "};",
13165 Style);
13167 // Check if lines are added.
13168 verifyFormat("struct foo {\n"
13169 "private:\n"
13170 "\n"
13171 " void f() {}\n"
13172 "\n"
13173 "private:\n"
13174 "\n"
13175 " int i;\n"
13176 "\n"
13177 "protected:\n"
13178 "\n"
13179 " int j;\n"
13180 "};",
13181 "struct foo {\n"
13182 "private:\n"
13183 " void f() {}\n"
13184 "\n"
13185 "private:\n"
13186 " int i;\n"
13187 "\n"
13188 "protected:\n"
13189 " int j;\n"
13190 "};",
13191 Style);
13193 // Leave tests rely on the code layout, test::messUp can not be used.
13194 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13195 Style.MaxEmptyLinesToKeep = 0u;
13196 verifyFormat("struct foo {\n"
13197 "private:\n"
13198 " void f() {}\n"
13199 "\n"
13200 "private:\n"
13201 " int i;\n"
13202 "\n"
13203 "protected:\n"
13204 " int j;\n"
13205 "};",
13206 Style);
13208 // Check if MaxEmptyLinesToKeep is respected.
13209 verifyFormat("struct foo {\n"
13210 "private:\n"
13211 " void f() {}\n"
13212 "\n"
13213 "private:\n"
13214 " int i;\n"
13215 "\n"
13216 "protected:\n"
13217 " int j;\n"
13218 "};",
13219 "struct foo {\n"
13220 "private:\n"
13221 "\n\n\n"
13222 " void f() {}\n"
13223 "\n"
13224 "private:\n"
13225 "\n\n\n"
13226 " int i;\n"
13227 "\n"
13228 "protected:\n"
13229 "\n\n\n"
13230 " int j;\n"
13231 "};",
13232 Style);
13234 Style.MaxEmptyLinesToKeep = 1u;
13235 verifyNoChange("struct foo {\n"
13236 "private:\n"
13237 "\n"
13238 " void f() {}\n"
13239 "\n"
13240 "private:\n"
13241 "\n"
13242 " int i;\n"
13243 "\n"
13244 "protected:\n"
13245 "\n"
13246 " int j;\n"
13247 "};",
13248 Style);
13249 // Check if no lines are kept.
13250 verifyFormat("struct foo {\n"
13251 "private:\n"
13252 " void f() {}\n"
13253 "\n"
13254 "private:\n"
13255 " int i;\n"
13256 "\n"
13257 "protected:\n"
13258 " int j;\n"
13259 "};",
13260 Style);
13261 // Check if MaxEmptyLinesToKeep is respected.
13262 verifyFormat("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 "struct foo {\n"
13276 "private:\n"
13277 "\n\n\n"
13278 " void f() {}\n"
13279 "\n"
13280 "private:\n"
13281 "\n\n\n"
13282 " int i;\n"
13283 "\n"
13284 "protected:\n"
13285 "\n\n\n"
13286 " int j;\n"
13287 "};",
13288 Style);
13290 Style.MaxEmptyLinesToKeep = 10u;
13291 verifyNoChange("struct foo {\n"
13292 "private:\n"
13293 "\n\n\n"
13294 " void f() {}\n"
13295 "\n"
13296 "private:\n"
13297 "\n\n\n"
13298 " int i;\n"
13299 "\n"
13300 "protected:\n"
13301 "\n\n\n"
13302 " int j;\n"
13303 "};",
13304 Style);
13306 // Test with comments.
13307 Style = getLLVMStyle();
13308 verifyFormat("struct foo {\n"
13309 "private:\n"
13310 " // comment\n"
13311 " void f() {}\n"
13312 "\n"
13313 "private: /* comment */\n"
13314 " int i;\n"
13315 "};",
13316 Style);
13317 verifyFormat("struct foo {\n"
13318 "private:\n"
13319 " // comment\n"
13320 " void f() {}\n"
13321 "\n"
13322 "private: /* comment */\n"
13323 " int i;\n"
13324 "};",
13325 "struct foo {\n"
13326 "private:\n"
13327 "\n"
13328 " // comment\n"
13329 " void f() {}\n"
13330 "\n"
13331 "private: /* comment */\n"
13332 "\n"
13333 " int i;\n"
13334 "};",
13335 Style);
13337 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13338 verifyFormat("struct foo {\n"
13339 "private:\n"
13340 "\n"
13341 " // comment\n"
13342 " void f() {}\n"
13343 "\n"
13344 "private: /* comment */\n"
13345 "\n"
13346 " int i;\n"
13347 "};",
13348 "struct foo {\n"
13349 "private:\n"
13350 " // comment\n"
13351 " void f() {}\n"
13352 "\n"
13353 "private: /* comment */\n"
13354 " int i;\n"
13355 "};",
13356 Style);
13357 verifyFormat("struct foo {\n"
13358 "private:\n"
13359 "\n"
13360 " // comment\n"
13361 " void f() {}\n"
13362 "\n"
13363 "private: /* comment */\n"
13364 "\n"
13365 " int i;\n"
13366 "};",
13367 Style);
13369 // Test with preprocessor defines.
13370 Style = getLLVMStyle();
13371 verifyFormat("struct foo {\n"
13372 "private:\n"
13373 "#ifdef FOO\n"
13374 "#endif\n"
13375 " void f() {}\n"
13376 "};",
13377 Style);
13378 verifyFormat("struct foo {\n"
13379 "private:\n"
13380 "#ifdef FOO\n"
13381 "#endif\n"
13382 " void f() {}\n"
13383 "};",
13384 "struct foo {\n"
13385 "private:\n"
13386 "\n"
13387 "#ifdef FOO\n"
13388 "#endif\n"
13389 " void f() {}\n"
13390 "};",
13391 Style);
13392 verifyNoChange("struct foo {\n"
13393 "#ifdef FOO\n"
13394 "#else\n"
13395 "private:\n"
13396 "\n"
13397 "#endif\n"
13398 "};",
13399 Style);
13400 verifyFormat("struct foo {\n"
13401 "#ifdef FOO\n"
13402 "#else\n"
13403 "private:\n"
13404 "\n"
13405 "#endif\n"
13406 "};",
13407 "struct foo {\n"
13408 "#ifdef FOO\n"
13409 "#else\n"
13410 "private:\n"
13411 "\n"
13412 "\n"
13413 "#endif\n"
13414 "};",
13415 Style);
13416 verifyFormat("struct foo {\n"
13417 "#ifdef FOO\n"
13418 "private:\n"
13419 "#else\n"
13420 "#endif\n"
13421 "};",
13422 "struct foo {\n"
13423 "#ifdef FOO\n"
13424 "private:\n"
13425 "\n"
13426 "\n"
13427 "#else\n"
13428 "#endif\n"
13429 "};",
13430 Style);
13431 verifyFormat("struct foo {\n"
13432 "#if 0\n"
13433 "#else\n"
13434 "#endif\n"
13435 "#ifdef FOO\n"
13436 "private:\n"
13437 "#endif\n"
13438 "};",
13439 "struct foo {\n"
13440 "#if 0\n"
13441 "#else\n"
13442 "#endif\n"
13443 "#ifdef FOO\n"
13444 "private:\n"
13445 "\n"
13446 "\n"
13447 "#endif\n"
13448 "};",
13449 Style);
13451 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13452 verifyFormat("struct foo {\n"
13453 "private:\n"
13454 "\n"
13455 "#ifdef FOO\n"
13456 "#endif\n"
13457 " void f() {}\n"
13458 "};",
13459 "struct foo {\n"
13460 "private:\n"
13461 "#ifdef FOO\n"
13462 "#endif\n"
13463 " void f() {}\n"
13464 "};",
13465 Style);
13466 verifyFormat("struct foo {\n"
13467 "private:\n"
13468 "\n"
13469 "#ifdef FOO\n"
13470 "#endif\n"
13471 " void f() {}\n"
13472 "};",
13473 Style);
13476 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13477 // Combined tests of EmptyLineAfterAccessModifier and
13478 // EmptyLineBeforeAccessModifier.
13479 FormatStyle Style = getLLVMStyle();
13480 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13481 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13482 verifyFormat("struct foo {\n"
13483 "private:\n"
13484 "\n"
13485 "protected:\n"
13486 "};",
13487 Style);
13489 Style.MaxEmptyLinesToKeep = 10u;
13490 // Both remove all new lines.
13491 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13492 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13493 verifyFormat("struct foo {\n"
13494 "private:\n"
13495 "protected:\n"
13496 "};",
13497 "struct foo {\n"
13498 "private:\n"
13499 "\n\n\n"
13500 "protected:\n"
13501 "};",
13502 Style);
13504 // Leave tests rely on the code layout, test::messUp can not be used.
13505 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13506 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13507 Style.MaxEmptyLinesToKeep = 10u;
13508 verifyNoChange("struct foo {\n"
13509 "private:\n"
13510 "\n\n\n"
13511 "protected:\n"
13512 "};",
13513 Style);
13514 Style.MaxEmptyLinesToKeep = 3u;
13515 verifyNoChange("struct foo {\n"
13516 "private:\n"
13517 "\n\n\n"
13518 "protected:\n"
13519 "};",
13520 Style);
13521 Style.MaxEmptyLinesToKeep = 1u;
13522 verifyNoChange("struct foo {\n"
13523 "private:\n"
13524 "\n\n\n"
13525 "protected:\n"
13526 "};",
13527 Style); // Based on new lines in original document and not
13528 // on the setting.
13530 Style.MaxEmptyLinesToKeep = 10u;
13531 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13532 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13533 // Newlines are kept if they are greater than zero,
13534 // test::messUp removes all new lines which changes the logic
13535 verifyNoChange("struct foo {\n"
13536 "private:\n"
13537 "\n\n\n"
13538 "protected:\n"
13539 "};",
13540 Style);
13542 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13543 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13544 // test::messUp removes all new lines which changes the logic
13545 verifyNoChange("struct foo {\n"
13546 "private:\n"
13547 "\n\n\n"
13548 "protected:\n"
13549 "};",
13550 Style);
13552 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13553 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13554 verifyNoChange("struct foo {\n"
13555 "private:\n"
13556 "\n\n\n"
13557 "protected:\n"
13558 "};",
13559 Style); // test::messUp removes all new lines which changes
13560 // the logic.
13562 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13563 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13564 verifyFormat("struct foo {\n"
13565 "private:\n"
13566 "protected:\n"
13567 "};",
13568 "struct foo {\n"
13569 "private:\n"
13570 "\n\n\n"
13571 "protected:\n"
13572 "};",
13573 Style);
13575 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13576 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13577 verifyNoChange("struct foo {\n"
13578 "private:\n"
13579 "\n\n\n"
13580 "protected:\n"
13581 "};",
13582 Style); // test::messUp removes all new lines which changes
13583 // the logic.
13585 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13586 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13587 verifyFormat("struct foo {\n"
13588 "private:\n"
13589 "protected:\n"
13590 "};",
13591 "struct foo {\n"
13592 "private:\n"
13593 "\n\n\n"
13594 "protected:\n"
13595 "};",
13596 Style);
13598 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13599 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13600 verifyFormat("struct foo {\n"
13601 "private:\n"
13602 "protected:\n"
13603 "};",
13604 "struct foo {\n"
13605 "private:\n"
13606 "\n\n\n"
13607 "protected:\n"
13608 "};",
13609 Style);
13611 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13612 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13613 verifyFormat("struct foo {\n"
13614 "private:\n"
13615 "protected:\n"
13616 "};",
13617 "struct foo {\n"
13618 "private:\n"
13619 "\n\n\n"
13620 "protected:\n"
13621 "};",
13622 Style);
13624 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13625 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13626 verifyFormat("struct foo {\n"
13627 "private:\n"
13628 "protected:\n"
13629 "};",
13630 "struct foo {\n"
13631 "private:\n"
13632 "\n\n\n"
13633 "protected:\n"
13634 "};",
13635 Style);
13638 TEST_F(FormatTest, FormatsArrays) {
13639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13640 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13641 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13642 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13643 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13644 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13645 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13646 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13647 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13648 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13649 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13650 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13651 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13652 verifyFormat(
13653 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13654 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13655 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13656 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13657 " .aaaaaaaaaaaaaaaaaaaaaa();");
13659 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13661 verifyFormat(
13662 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13663 " .aaaaaaa[0]\n"
13664 " .aaaaaaaaaaaaaaaaaaaaaa();");
13665 verifyFormat("a[::b::c];");
13667 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13669 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13670 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13673 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13674 verifyFormat("(a)->b();");
13675 verifyFormat("--a;");
13678 TEST_F(FormatTest, HandlesIncludeDirectives) {
13679 verifyFormat("#include <string>\n"
13680 "#include <a/b/c.h>\n"
13681 "#include \"a/b/string\"\n"
13682 "#include \"string.h\"\n"
13683 "#include \"string.h\"\n"
13684 "#include <a-a>\n"
13685 "#include < path with space >\n"
13686 "#include_next <test.h>"
13687 "#include \"abc.h\" // this is included for ABC\n"
13688 "#include \"some long include\" // with a comment\n"
13689 "#include \"some very long include path\"\n"
13690 "#include <some/very/long/include/path>",
13691 getLLVMStyleWithColumns(35));
13692 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13693 verifyFormat("#include <a>", "#include<a>");
13695 verifyFormat("#import <string>");
13696 verifyFormat("#import <a/b/c.h>");
13697 verifyFormat("#import \"a/b/string\"");
13698 verifyFormat("#import \"string.h\"");
13699 verifyFormat("#import \"string.h\"");
13700 verifyFormat("#if __has_include(<strstream>)\n"
13701 "#include <strstream>\n"
13702 "#endif");
13704 verifyFormat("#define MY_IMPORT <a/b>");
13706 verifyFormat("#if __has_include(<a/b>)");
13707 verifyFormat("#if __has_include_next(<a/b>)");
13708 verifyFormat("#define F __has_include(<a/b>)");
13709 verifyFormat("#define F __has_include_next(<a/b>)");
13711 // Protocol buffer definition or missing "#".
13712 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13713 getLLVMStyleWithColumns(30));
13715 FormatStyle Style = getLLVMStyle();
13716 Style.AlwaysBreakBeforeMultilineStrings = true;
13717 Style.ColumnLimit = 0;
13718 verifyFormat("#import \"abc.h\"", Style);
13720 // But 'import' might also be a regular C++ namespace.
13721 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13723 verifyFormat("import::Bar foo(val ? 2 : 1);");
13726 //===----------------------------------------------------------------------===//
13727 // Error recovery tests.
13728 //===----------------------------------------------------------------------===//
13730 TEST_F(FormatTest, IncompleteParameterLists) {
13731 FormatStyle NoBinPacking = getLLVMStyle();
13732 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
13733 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13734 " double *min_x,\n"
13735 " double *max_x,\n"
13736 " double *min_y,\n"
13737 " double *max_y,\n"
13738 " double *min_z,\n"
13739 " double *max_z, ) {}",
13740 NoBinPacking);
13743 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13744 verifyFormat("void f() { return; }\n42");
13745 verifyFormat("void f() {\n"
13746 " if (0)\n"
13747 " return;\n"
13748 "}\n"
13749 "42");
13750 verifyFormat("void f() { return }\n42");
13751 verifyFormat("void f() {\n"
13752 " if (0)\n"
13753 " return\n"
13754 "}\n"
13755 "42");
13758 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13759 verifyFormat("void f() { return }", "void f ( ) { return }");
13760 verifyFormat("void f() {\n"
13761 " if (a)\n"
13762 " return\n"
13763 "}",
13764 "void f ( ) { if ( a ) return }");
13765 verifyFormat("namespace N {\n"
13766 "void f()\n"
13767 "}",
13768 "namespace N { void f() }");
13769 verifyFormat("namespace N {\n"
13770 "void f() {}\n"
13771 "void g()\n"
13772 "} // namespace N",
13773 "namespace N { void f( ) { } void g( ) }");
13776 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13777 verifyFormat("int aaaaaaaa =\n"
13778 " // Overlylongcomment\n"
13779 " b;",
13780 getLLVMStyleWithColumns(20));
13781 verifyFormat("function(\n"
13782 " ShortArgument,\n"
13783 " LoooooooooooongArgument);",
13784 getLLVMStyleWithColumns(20));
13787 TEST_F(FormatTest, IncorrectAccessSpecifier) {
13788 verifyFormat("public:");
13789 verifyFormat("class A {\n"
13790 "public\n"
13791 " void f() {}\n"
13792 "};");
13793 verifyFormat("public\n"
13794 "int qwerty;");
13795 verifyFormat("public\n"
13796 "B {}");
13797 verifyFormat("public\n"
13798 "{\n"
13799 "}");
13800 verifyFormat("public\n"
13801 "B { int x; }");
13804 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
13805 verifyFormat("{");
13806 verifyFormat("#})");
13807 verifyNoCrash("(/**/[:!] ?[).");
13808 verifyNoCrash("struct X {\n"
13809 " operator iunt(\n"
13810 "};");
13811 verifyNoCrash("struct Foo {\n"
13812 " operator foo(bar\n"
13813 "};");
13816 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
13817 // Found by oss-fuzz:
13818 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13819 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
13820 Style.ColumnLimit = 60;
13821 verifyNoCrash(
13822 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13823 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13824 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13825 Style);
13828 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
13829 verifyFormat("do {\n}");
13830 verifyFormat("do {\n}\n"
13831 "f();");
13832 verifyFormat("do {\n}\n"
13833 "wheeee(fun);");
13834 verifyFormat("do {\n"
13835 " f();\n"
13836 "}");
13839 TEST_F(FormatTest, IncorrectCodeMissingParens) {
13840 verifyFormat("if {\n foo;\n foo();\n}");
13841 verifyFormat("switch {\n foo;\n foo();\n}");
13842 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13843 verifyIncompleteFormat("ERROR: for target;");
13844 verifyFormat("while {\n foo;\n foo();\n}");
13845 verifyFormat("do {\n foo;\n foo();\n} while;");
13848 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
13849 verifyIncompleteFormat("namespace {\n"
13850 "class Foo { Foo (\n"
13851 "};\n"
13852 "} // namespace");
13855 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13856 verifyFormat("{\n"
13857 " {\n"
13858 " }",
13859 "{\n"
13860 "{\n"
13861 "}");
13862 verifyFormat("{\n"
13863 " {\n"
13864 " }",
13865 "{\n"
13866 " {\n"
13867 "}");
13868 verifyFormat("{\n"
13869 " {\n"
13870 " }");
13871 verifyFormat("{\n"
13872 " {\n"
13873 " }\n"
13874 "}\n"
13875 "}",
13876 "{\n"
13877 " {\n"
13878 " }\n"
13879 " }\n"
13880 "}");
13882 verifyFormat("{\n"
13883 " {\n"
13884 " breakme(\n"
13885 " qwe);\n"
13886 " }",
13887 "{\n"
13888 " {\n"
13889 " breakme(qwe);\n"
13890 "}",
13891 getLLVMStyleWithColumns(10));
13894 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
13895 verifyFormat("int x = {\n"
13896 " avariable,\n"
13897 " b(alongervariable)};",
13898 getLLVMStyleWithColumns(25));
13901 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
13902 verifyFormat("return (a)(b){1, 2, 3};");
13905 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
13906 verifyFormat("vector<int> x{1, 2, 3, 4};");
13907 verifyFormat("vector<int> x{\n"
13908 " 1,\n"
13909 " 2,\n"
13910 " 3,\n"
13911 " 4,\n"
13912 "};");
13913 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13914 verifyFormat("f({1, 2});");
13915 verifyFormat("auto v = Foo{-1};");
13916 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13917 verifyFormat("Class::Class : member{1, 2, 3} {}");
13918 verifyFormat("new vector<int>{1, 2, 3};");
13919 verifyFormat("new int[3]{1, 2, 3};");
13920 verifyFormat("new int{1};");
13921 verifyFormat("return {arg1, arg2};");
13922 verifyFormat("return {arg1, SomeType{parameter}};");
13923 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13924 verifyFormat("new T{arg1, arg2};");
13925 verifyFormat("f(MyMap[{composite, key}]);");
13926 verifyFormat("class Class {\n"
13927 " T member = {arg1, arg2};\n"
13928 "};");
13929 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13930 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13931 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13932 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13933 verifyFormat("int a = std::is_integral<int>{} + 0;");
13935 verifyFormat("int foo(int i) { return fo1{}(i); }");
13936 verifyFormat("int foo(int i) { return fo1{}(i); }");
13937 verifyFormat("auto i = decltype(x){};");
13938 verifyFormat("auto i = typeof(x){};");
13939 verifyFormat("auto i = _Atomic(x){};");
13940 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13941 verifyFormat("Node n{1, Node{1000}, //\n"
13942 " 2};");
13943 verifyFormat("Aaaa aaaaaaa{\n"
13944 " {\n"
13945 " aaaa,\n"
13946 " },\n"
13947 "};");
13948 verifyFormat("class C : public D {\n"
13949 " SomeClass SC{2};\n"
13950 "};");
13951 verifyFormat("class C : public A {\n"
13952 " class D : public B {\n"
13953 " void f() { int i{2}; }\n"
13954 " };\n"
13955 "};");
13956 verifyFormat("#define A {a, a},");
13957 // Don't confuse braced list initializers with compound statements.
13958 verifyFormat(
13959 "class A {\n"
13960 " A() : a{} {}\n"
13961 " A() : Base<int>{} {}\n"
13962 " A() : Base<Foo<int>>{} {}\n"
13963 " A(int b) : b(b) {}\n"
13964 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13965 " int a, b;\n"
13966 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13967 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13968 "{}\n"
13969 "};");
13971 // Avoid breaking between equal sign and opening brace
13972 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
13973 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
13974 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13975 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13976 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13977 " {\"ccccccccccccccccccccc\", 2}};",
13978 AvoidBreakingFirstArgument);
13980 // Binpacking only if there is no trailing comma
13981 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13982 " cccccccccc, dddddddddd};",
13983 getLLVMStyleWithColumns(50));
13984 verifyFormat("const Aaaaaa aaaaa = {\n"
13985 " aaaaaaaaaaa,\n"
13986 " bbbbbbbbbbb,\n"
13987 " ccccccccccc,\n"
13988 " ddddddddddd,\n"
13989 "};",
13990 getLLVMStyleWithColumns(50));
13992 // Cases where distinguising braced lists and blocks is hard.
13993 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13994 verifyFormat("void f() {\n"
13995 " return; // comment\n"
13996 "}\n"
13997 "SomeType t;");
13998 verifyFormat("void f() {\n"
13999 " if (a) {\n"
14000 " f();\n"
14001 " }\n"
14002 "}\n"
14003 "SomeType t;");
14005 // In combination with BinPackArguments = false.
14006 FormatStyle NoBinPacking = getLLVMStyle();
14007 NoBinPacking.BinPackArguments = false;
14008 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14009 " bbbbb,\n"
14010 " ccccc,\n"
14011 " ddddd,\n"
14012 " eeeee,\n"
14013 " ffffff,\n"
14014 " ggggg,\n"
14015 " hhhhhh,\n"
14016 " iiiiii,\n"
14017 " jjjjjj,\n"
14018 " kkkkkk};",
14019 NoBinPacking);
14020 verifyFormat("const Aaaaaa aaaaa = {\n"
14021 " aaaaa,\n"
14022 " bbbbb,\n"
14023 " ccccc,\n"
14024 " ddddd,\n"
14025 " eeeee,\n"
14026 " ffffff,\n"
14027 " ggggg,\n"
14028 " hhhhhh,\n"
14029 " iiiiii,\n"
14030 " jjjjjj,\n"
14031 " kkkkkk,\n"
14032 "};",
14033 NoBinPacking);
14034 verifyFormat(
14035 "const Aaaaaa aaaaa = {\n"
14036 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
14037 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
14038 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14039 "};",
14040 NoBinPacking);
14042 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14043 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14044 " CDDDP83848_BMCR_REGISTER,\n"
14045 " CDDDP83848_BMSR_REGISTER,\n"
14046 " CDDDP83848_RBR_REGISTER};",
14047 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14048 " CDDDP83848_BMSR_REGISTER,\n"
14049 " CDDDP83848_RBR_REGISTER};",
14050 NoBinPacking);
14052 // FIXME: The alignment of these trailing comments might be bad. Then again,
14053 // this might be utterly useless in real code.
14054 verifyFormat("Constructor::Constructor()\n"
14055 " : some_value{ //\n"
14056 " aaaaaaa, //\n"
14057 " bbbbbbb} {}");
14059 // In braced lists, the first comment is always assumed to belong to the
14060 // first element. Thus, it can be moved to the next or previous line as
14061 // appropriate.
14062 verifyFormat("function({// First element:\n"
14063 " 1,\n"
14064 " // Second element:\n"
14065 " 2});",
14066 "function({\n"
14067 " // First element:\n"
14068 " 1,\n"
14069 " // Second element:\n"
14070 " 2});");
14071 verifyFormat("std::vector<int> MyNumbers{\n"
14072 " // First element:\n"
14073 " 1,\n"
14074 " // Second element:\n"
14075 " 2};",
14076 "std::vector<int> MyNumbers{// First element:\n"
14077 " 1,\n"
14078 " // Second element:\n"
14079 " 2};",
14080 getLLVMStyleWithColumns(30));
14081 // A trailing comma should still lead to an enforced line break and no
14082 // binpacking.
14083 verifyFormat("vector<int> SomeVector = {\n"
14084 " // aaa\n"
14085 " 1,\n"
14086 " 2,\n"
14087 "};",
14088 "vector<int> SomeVector = { // aaa\n"
14089 " 1, 2, };");
14091 // C++11 brace initializer list l-braces should not be treated any differently
14092 // when breaking before lambda bodies is enabled
14093 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
14094 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
14095 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
14096 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
14097 verifyFormat(
14098 "std::runtime_error{\n"
14099 " \"Long string which will force a break onto the next line...\"};",
14100 BreakBeforeLambdaBody);
14102 FormatStyle ExtraSpaces = getLLVMStyle();
14103 ExtraSpaces.Cpp11BracedListStyle = false;
14104 ExtraSpaces.ColumnLimit = 75;
14105 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
14106 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
14107 verifyFormat("f({ 1, 2 });", ExtraSpaces);
14108 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
14109 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
14110 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
14111 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
14112 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
14113 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
14114 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
14115 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
14116 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
14117 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
14118 verifyFormat("class Class {\n"
14119 " T member = { arg1, arg2 };\n"
14120 "};",
14121 ExtraSpaces);
14122 verifyFormat(
14123 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14124 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14125 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14126 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14127 ExtraSpaces);
14128 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
14129 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14130 ExtraSpaces);
14131 verifyFormat(
14132 "someFunction(OtherParam,\n"
14133 " BracedList{ // comment 1 (Forcing interesting break)\n"
14134 " param1, param2,\n"
14135 " // comment 2\n"
14136 " param3, param4 });",
14137 ExtraSpaces);
14138 verifyFormat(
14139 "std::this_thread::sleep_for(\n"
14140 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14141 ExtraSpaces);
14142 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14143 " aaaaaaa,\n"
14144 " aaaaaaaaaa,\n"
14145 " aaaaa,\n"
14146 " aaaaaaaaaaaaaaa,\n"
14147 " aaa,\n"
14148 " aaaaaaaaaa,\n"
14149 " a,\n"
14150 " aaaaaaaaaaaaaaaaaaaaa,\n"
14151 " aaaaaaaaaaaa,\n"
14152 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14153 " aaaaaaa,\n"
14154 " a};");
14155 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
14156 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
14157 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
14159 // Avoid breaking between initializer/equal sign and opening brace
14160 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
14161 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14162 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14163 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14164 " { \"ccccccccccccccccccccc\", 2 }\n"
14165 "};",
14166 ExtraSpaces);
14167 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14168 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14169 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14170 " { \"ccccccccccccccccccccc\", 2 }\n"
14171 "};",
14172 ExtraSpaces);
14174 FormatStyle SpaceBeforeBrace = getLLVMStyle();
14175 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
14176 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
14177 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
14179 FormatStyle SpaceBetweenBraces = getLLVMStyle();
14180 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
14181 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14182 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
14183 SpaceBetweenBraces.SpacesInSquareBrackets = true;
14184 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
14185 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
14186 verifyFormat("vector< int > x{ // comment 1\n"
14187 " 1, 2, 3, 4 };",
14188 SpaceBetweenBraces);
14189 SpaceBetweenBraces.ColumnLimit = 20;
14190 verifyFormat("vector< int > x{\n"
14191 " 1, 2, 3, 4 };",
14192 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14193 SpaceBetweenBraces.ColumnLimit = 24;
14194 verifyFormat("vector< int > x{ 1, 2,\n"
14195 " 3, 4 };",
14196 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14197 verifyFormat("vector< int > x{\n"
14198 " 1,\n"
14199 " 2,\n"
14200 " 3,\n"
14201 " 4,\n"
14202 "};",
14203 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14204 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14205 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14206 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14207 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14210 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14211 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14212 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14213 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14214 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14215 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14216 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14217 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14218 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14219 " 1, 22, 333, 4444, 55555, //\n"
14220 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14221 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14222 verifyFormat(
14223 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14224 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14225 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14226 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14227 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14228 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14229 " 7777777};");
14230 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14231 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14232 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14233 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14234 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14235 " // Separating comment.\n"
14236 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14237 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14238 " // Leading comment\n"
14239 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14240 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14241 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14242 " 1, 1, 1, 1};",
14243 getLLVMStyleWithColumns(39));
14244 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14245 " 1, 1, 1, 1};",
14246 getLLVMStyleWithColumns(38));
14247 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14248 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14249 getLLVMStyleWithColumns(43));
14250 verifyFormat(
14251 "static unsigned SomeValues[10][3] = {\n"
14252 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14253 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14254 verifyFormat("static auto fields = new vector<string>{\n"
14255 " \"aaaaaaaaaaaaa\",\n"
14256 " \"aaaaaaaaaaaaa\",\n"
14257 " \"aaaaaaaaaaaa\",\n"
14258 " \"aaaaaaaaaaaaaa\",\n"
14259 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14260 " \"aaaaaaaaaaaa\",\n"
14261 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14262 "};");
14263 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14264 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14265 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14266 " 3, cccccccccccccccccccccc};",
14267 getLLVMStyleWithColumns(60));
14269 // Trailing commas.
14270 verifyFormat("vector<int> x = {\n"
14271 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14272 "};",
14273 getLLVMStyleWithColumns(39));
14274 verifyFormat("vector<int> x = {\n"
14275 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14276 "};",
14277 getLLVMStyleWithColumns(39));
14278 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14279 " 1, 1, 1, 1,\n"
14280 " /**/ /**/};",
14281 getLLVMStyleWithColumns(39));
14283 // Trailing comment in the first line.
14284 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14285 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14286 " 111111111, 222222222, 3333333333, 444444444, //\n"
14287 " 11111111, 22222222, 333333333, 44444444};");
14288 // Trailing comment in the last line.
14289 verifyFormat("int aaaaa[] = {\n"
14290 " 1, 2, 3, // comment\n"
14291 " 4, 5, 6 // comment\n"
14292 "};");
14294 // With nested lists, we should either format one item per line or all nested
14295 // lists one on line.
14296 // FIXME: For some nested lists, we can do better.
14297 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14298 " {aaaaaaaaaaaaaaaaaaa},\n"
14299 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14300 " {aaaaaaaaaaaaaaaaa}};",
14301 getLLVMStyleWithColumns(60));
14302 verifyFormat(
14303 "SomeStruct my_struct_array = {\n"
14304 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14305 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14306 " {aaa, aaa},\n"
14307 " {aaa, aaa},\n"
14308 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14309 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14310 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14312 // No column layout should be used here.
14313 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14314 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14316 verifyNoCrash("a<,");
14318 // No braced initializer here.
14319 verifyFormat("void f() {\n"
14320 " struct Dummy {};\n"
14321 " f(v);\n"
14322 "}");
14323 verifyFormat("void foo() {\n"
14324 " { // asdf\n"
14325 " {\n"
14326 " int a;\n"
14327 " }\n"
14328 " }\n"
14329 " {\n"
14330 " {\n"
14331 " int b;\n"
14332 " }\n"
14333 " }\n"
14334 "}");
14335 verifyFormat("namespace n {\n"
14336 "void foo() {\n"
14337 " {\n"
14338 " {\n"
14339 " statement();\n"
14340 " if (false) {\n"
14341 " }\n"
14342 " }\n"
14343 " }\n"
14344 " {\n"
14345 " }\n"
14346 "}\n"
14347 "} // namespace n");
14349 // Long lists should be formatted in columns even if they are nested.
14350 verifyFormat(
14351 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14352 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14353 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14354 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14355 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14356 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14358 // Allow "single-column" layout even if that violates the column limit. There
14359 // isn't going to be a better way.
14360 verifyFormat("std::vector<int> a = {\n"
14361 " aaaaaaaa,\n"
14362 " aaaaaaaa,\n"
14363 " aaaaaaaa,\n"
14364 " aaaaaaaa,\n"
14365 " aaaaaaaaaa,\n"
14366 " aaaaaaaa,\n"
14367 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14368 getLLVMStyleWithColumns(30));
14369 verifyFormat("vector<int> aaaa = {\n"
14370 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14371 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14372 " aaaaaa.aaaaaaa,\n"
14373 " aaaaaa.aaaaaaa,\n"
14374 " aaaaaa.aaaaaaa,\n"
14375 " aaaaaa.aaaaaaa,\n"
14376 "};");
14378 // Don't create hanging lists.
14379 verifyFormat("someFunction(Param, {List1, List2,\n"
14380 " List3});",
14381 getLLVMStyleWithColumns(35));
14382 verifyFormat("someFunction(Param, Param,\n"
14383 " {List1, List2,\n"
14384 " List3});",
14385 getLLVMStyleWithColumns(35));
14386 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14387 " aaaaaaaaaaaaaaaaaaaaaaa);");
14389 // No possible column formats, don't want the optimal paths penalized.
14390 verifyFormat(
14391 "waarudo::unit desk = {\n"
14392 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14393 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14394 "Output{1, 2}; },\n"
14395 " [](const Input &i) -> Output { return "
14396 "Output{1, 2}; });");
14397 FormatStyle NoBinPacking = getLLVMStyle();
14398 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
14399 verifyFormat("waarudo::unit desk = {\n"
14400 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14401 "1, 1} * w::m; }};",
14402 NoBinPacking);
14405 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14406 FormatStyle DoNotMerge = getLLVMStyle();
14407 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14409 verifyFormat("void f() { return 42; }");
14410 verifyFormat("void f() {\n"
14411 " return 42;\n"
14412 "}",
14413 DoNotMerge);
14414 verifyFormat("void f() {\n"
14415 " // Comment\n"
14416 "}");
14417 verifyFormat("{\n"
14418 "#error {\n"
14419 " int a;\n"
14420 "}");
14421 verifyFormat("{\n"
14422 " int a;\n"
14423 "#error {\n"
14424 "}");
14425 verifyFormat("void f() {} // comment");
14426 verifyFormat("void f() { int a; } // comment");
14427 verifyFormat("void f() {\n"
14428 "} // comment",
14429 DoNotMerge);
14430 verifyFormat("void f() {\n"
14431 " int a;\n"
14432 "} // comment",
14433 DoNotMerge);
14434 verifyFormat("void f() {\n"
14435 "} // comment",
14436 getLLVMStyleWithColumns(15));
14438 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14439 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14441 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14442 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14443 verifyGoogleFormat("class C {\n"
14444 " C()\n"
14445 " : iiiiiiii(nullptr),\n"
14446 " kkkkkkk(nullptr),\n"
14447 " mmmmmmm(nullptr),\n"
14448 " nnnnnnn(nullptr) {}\n"
14449 "};");
14451 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
14452 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
14453 verifyFormat("class C {\n"
14454 " A() : b(0) {}\n"
14455 "};",
14456 "class C{A():b(0){}};", NoColumnLimit);
14457 verifyFormat("A()\n"
14458 " : b(0) {\n"
14459 "}",
14460 "A()\n:b(0)\n{\n}", NoColumnLimit);
14462 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
14463 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
14464 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
14465 verifyFormat("class C {\n"
14466 "#pragma foo\n"
14467 " int foo { return 0; }\n"
14468 "};",
14469 NoColumnLimitWrapAfterFunction);
14470 verifyFormat("class C {\n"
14471 "#pragma foo\n"
14472 " void foo {}\n"
14473 "};",
14474 NoColumnLimitWrapAfterFunction);
14476 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
14477 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
14478 FormatStyle::SFS_None;
14479 verifyFormat("A()\n"
14480 " : b(0) {\n"
14481 "}",
14482 "A():b(0){}", DoNotMergeNoColumnLimit);
14483 verifyFormat("A()\n"
14484 " : b(0) {\n"
14485 "}",
14486 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
14488 verifyFormat("#define A \\\n"
14489 " void f() { \\\n"
14490 " int i; \\\n"
14491 " }",
14492 getLLVMStyleWithColumns(20));
14493 verifyFormat("#define A \\\n"
14494 " void f() { int i; }",
14495 getLLVMStyleWithColumns(21));
14496 verifyFormat("#define A \\\n"
14497 " void f() { \\\n"
14498 " int i; \\\n"
14499 " } \\\n"
14500 " int j;",
14501 getLLVMStyleWithColumns(22));
14502 verifyFormat("#define A \\\n"
14503 " void f() { int i; } \\\n"
14504 " int j;",
14505 getLLVMStyleWithColumns(23));
14507 verifyFormat(
14508 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14509 " aaaaaaaaaaaaaaaaaa,\n"
14510 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14512 constexpr StringRef Code{"void foo() { /* Empty */ }"};
14513 verifyFormat(Code);
14514 verifyFormat(Code, "void foo() { /* Empty */\n"
14515 "}");
14516 verifyFormat(Code, "void foo() {\n"
14517 "/* Empty */\n"
14518 "}");
14521 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
14522 FormatStyle MergeEmptyOnly = getLLVMStyle();
14523 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14524 verifyFormat("class C {\n"
14525 " int f() {}\n"
14526 "};",
14527 MergeEmptyOnly);
14528 verifyFormat("class C {\n"
14529 " int f() {\n"
14530 " return 42;\n"
14531 " }\n"
14532 "};",
14533 MergeEmptyOnly);
14534 verifyFormat("int f() {}", MergeEmptyOnly);
14535 verifyFormat("int f() {\n"
14536 " return 42;\n"
14537 "}",
14538 MergeEmptyOnly);
14540 // Also verify behavior when BraceWrapping.AfterFunction = true
14541 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14542 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
14543 verifyFormat("int f() {}", MergeEmptyOnly);
14544 verifyFormat("class C {\n"
14545 " int f() {}\n"
14546 "};",
14547 MergeEmptyOnly);
14550 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
14551 FormatStyle MergeInlineOnly = getLLVMStyle();
14552 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14553 verifyFormat("class C {\n"
14554 " int f() { return 42; }\n"
14555 "};",
14556 MergeInlineOnly);
14557 verifyFormat("int f() {\n"
14558 " return 42;\n"
14559 "}",
14560 MergeInlineOnly);
14562 // SFS_Inline implies SFS_Empty
14563 verifyFormat("class C {\n"
14564 " int f() {}\n"
14565 "};",
14566 MergeInlineOnly);
14567 verifyFormat("int f() {}", MergeInlineOnly);
14568 // https://llvm.org/PR54147
14569 verifyFormat("auto lambda = []() {\n"
14570 " // comment\n"
14571 " f();\n"
14572 " g();\n"
14573 "};",
14574 MergeInlineOnly);
14576 verifyFormat("class C {\n"
14577 "#ifdef A\n"
14578 " int f() { return 42; }\n"
14579 "#endif\n"
14580 "};",
14581 MergeInlineOnly);
14583 verifyFormat("struct S {\n"
14584 "// comment\n"
14585 "#ifdef FOO\n"
14586 " int foo() { bar(); }\n"
14587 "#endif\n"
14588 "};",
14589 MergeInlineOnly);
14591 // Also verify behavior when BraceWrapping.AfterFunction = true
14592 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14593 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14594 verifyFormat("class C {\n"
14595 " int f() { return 42; }\n"
14596 "};",
14597 MergeInlineOnly);
14598 verifyFormat("int f()\n"
14599 "{\n"
14600 " return 42;\n"
14601 "}",
14602 MergeInlineOnly);
14604 // SFS_Inline implies SFS_Empty
14605 verifyFormat("int f() {}", MergeInlineOnly);
14606 verifyFormat("class C {\n"
14607 " int f() {}\n"
14608 "};",
14609 MergeInlineOnly);
14611 MergeInlineOnly.BraceWrapping.AfterClass = true;
14612 MergeInlineOnly.BraceWrapping.AfterStruct = true;
14613 verifyFormat("class C\n"
14614 "{\n"
14615 " int f() { return 42; }\n"
14616 "};",
14617 MergeInlineOnly);
14618 verifyFormat("struct C\n"
14619 "{\n"
14620 " int f() { return 42; }\n"
14621 "};",
14622 MergeInlineOnly);
14623 verifyFormat("int f()\n"
14624 "{\n"
14625 " return 42;\n"
14626 "}",
14627 MergeInlineOnly);
14628 verifyFormat("int f() {}", MergeInlineOnly);
14629 verifyFormat("class C\n"
14630 "{\n"
14631 " int f() { return 42; }\n"
14632 "};",
14633 MergeInlineOnly);
14634 verifyFormat("struct C\n"
14635 "{\n"
14636 " int f() { return 42; }\n"
14637 "};",
14638 MergeInlineOnly);
14639 verifyFormat("struct C\n"
14640 "// comment\n"
14641 "/* comment */\n"
14642 "// comment\n"
14643 "{\n"
14644 " int f() { return 42; }\n"
14645 "};",
14646 MergeInlineOnly);
14647 verifyFormat("/* comment */ struct C\n"
14648 "{\n"
14649 " int f() { return 42; }\n"
14650 "};",
14651 MergeInlineOnly);
14654 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14655 FormatStyle MergeInlineOnly = getLLVMStyle();
14656 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14657 FormatStyle::SFS_InlineOnly;
14658 verifyFormat("class C {\n"
14659 " int f() { return 42; }\n"
14660 "};",
14661 MergeInlineOnly);
14662 verifyFormat("int f() {\n"
14663 " return 42;\n"
14664 "}",
14665 MergeInlineOnly);
14667 // SFS_InlineOnly does not imply SFS_Empty
14668 verifyFormat("class C {\n"
14669 " int f() {}\n"
14670 "};",
14671 MergeInlineOnly);
14672 verifyFormat("int f() {\n"
14673 "}",
14674 MergeInlineOnly);
14676 // Also verify behavior when BraceWrapping.AfterFunction = true
14677 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14678 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14679 verifyFormat("class C {\n"
14680 " int f() { return 42; }\n"
14681 "};",
14682 MergeInlineOnly);
14683 verifyFormat("int f()\n"
14684 "{\n"
14685 " return 42;\n"
14686 "}",
14687 MergeInlineOnly);
14689 // SFS_InlineOnly does not imply SFS_Empty
14690 verifyFormat("int f()\n"
14691 "{\n"
14692 "}",
14693 MergeInlineOnly);
14694 verifyFormat("class C {\n"
14695 " int f() {}\n"
14696 "};",
14697 MergeInlineOnly);
14700 TEST_F(FormatTest, SplitEmptyFunction) {
14701 FormatStyle Style = getLLVMStyleWithColumns(40);
14702 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14703 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14704 Style.BraceWrapping.AfterFunction = true;
14705 Style.BraceWrapping.SplitEmptyFunction = false;
14707 verifyFormat("int f()\n"
14708 "{}",
14709 Style);
14710 verifyFormat("int f()\n"
14711 "{\n"
14712 " return 42;\n"
14713 "}",
14714 Style);
14715 verifyFormat("int f()\n"
14716 "{\n"
14717 " // some comment\n"
14718 "}",
14719 Style);
14721 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14722 verifyFormat("int f() {}", Style);
14723 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14724 "{}",
14725 Style);
14726 verifyFormat("int f()\n"
14727 "{\n"
14728 " return 0;\n"
14729 "}",
14730 Style);
14732 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14733 verifyFormat("class Foo {\n"
14734 " int f() {}\n"
14735 "};",
14736 Style);
14737 verifyFormat("class Foo {\n"
14738 " int f() { return 0; }\n"
14739 "};",
14740 Style);
14741 verifyFormat("class Foo {\n"
14742 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14743 " {}\n"
14744 "};",
14745 Style);
14746 verifyFormat("class Foo {\n"
14747 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14748 " {\n"
14749 " return 0;\n"
14750 " }\n"
14751 "};",
14752 Style);
14754 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14755 verifyFormat("int f() {}", Style);
14756 verifyFormat("int f() { return 0; }", Style);
14757 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14758 "{}",
14759 Style);
14760 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14761 "{\n"
14762 " return 0;\n"
14763 "}",
14764 Style);
14767 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
14768 FormatStyle Style = getLLVMStyleWithColumns(40);
14769 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14770 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14771 Style.BraceWrapping.AfterFunction = true;
14772 Style.BraceWrapping.SplitEmptyFunction = true;
14773 Style.BraceWrapping.SplitEmptyRecord = false;
14775 verifyFormat("class C {};", Style);
14776 verifyFormat("struct C {};", Style);
14777 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14778 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14779 "{\n"
14780 "}",
14781 Style);
14782 verifyFormat("class C {\n"
14783 " C()\n"
14784 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14785 " bbbbbbbbbbbbbbbbbbb()\n"
14786 " {\n"
14787 " }\n"
14788 " void\n"
14789 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14790 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14791 " {\n"
14792 " }\n"
14793 "};",
14794 Style);
14797 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
14798 FormatStyle Style = getLLVMStyle();
14799 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14800 verifyFormat("#ifdef A\n"
14801 "int f() {}\n"
14802 "#else\n"
14803 "int g() {}\n"
14804 "#endif",
14805 Style);
14808 TEST_F(FormatTest, SplitEmptyClass) {
14809 FormatStyle Style = getLLVMStyle();
14810 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14811 Style.BraceWrapping.AfterClass = true;
14812 Style.BraceWrapping.SplitEmptyRecord = false;
14814 verifyFormat("class Foo\n"
14815 "{};",
14816 Style);
14817 verifyFormat("/* something */ class Foo\n"
14818 "{};",
14819 Style);
14820 verifyFormat("template <typename X> class Foo\n"
14821 "{};",
14822 Style);
14823 verifyFormat("class Foo\n"
14824 "{\n"
14825 " Foo();\n"
14826 "};",
14827 Style);
14828 verifyFormat("typedef class Foo\n"
14829 "{\n"
14830 "} Foo_t;",
14831 Style);
14833 Style.BraceWrapping.SplitEmptyRecord = true;
14834 Style.BraceWrapping.AfterStruct = true;
14835 verifyFormat("class rep\n"
14836 "{\n"
14837 "};",
14838 Style);
14839 verifyFormat("struct rep\n"
14840 "{\n"
14841 "};",
14842 Style);
14843 verifyFormat("template <typename T> class rep\n"
14844 "{\n"
14845 "};",
14846 Style);
14847 verifyFormat("template <typename T> struct rep\n"
14848 "{\n"
14849 "};",
14850 Style);
14851 verifyFormat("class rep\n"
14852 "{\n"
14853 " int x;\n"
14854 "};",
14855 Style);
14856 verifyFormat("struct rep\n"
14857 "{\n"
14858 " int x;\n"
14859 "};",
14860 Style);
14861 verifyFormat("template <typename T> class rep\n"
14862 "{\n"
14863 " int x;\n"
14864 "};",
14865 Style);
14866 verifyFormat("template <typename T> struct rep\n"
14867 "{\n"
14868 " int x;\n"
14869 "};",
14870 Style);
14871 verifyFormat("template <typename T> class rep // Foo\n"
14872 "{\n"
14873 " int x;\n"
14874 "};",
14875 Style);
14876 verifyFormat("template <typename T> struct rep // Bar\n"
14877 "{\n"
14878 " int x;\n"
14879 "};",
14880 Style);
14882 verifyFormat("template <typename T> class rep<T>\n"
14883 "{\n"
14884 " int x;\n"
14885 "};",
14886 Style);
14888 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14889 "{\n"
14890 " int x;\n"
14891 "};",
14892 Style);
14893 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14894 "{\n"
14895 "};",
14896 Style);
14898 verifyFormat("#include \"stdint.h\"\n"
14899 "namespace rep {}",
14900 Style);
14901 verifyFormat("#include <stdint.h>\n"
14902 "namespace rep {}",
14903 Style);
14904 verifyFormat("#include <stdint.h>\n"
14905 "namespace rep {}",
14906 "#include <stdint.h>\n"
14907 "namespace rep {\n"
14908 "\n"
14909 "\n"
14910 "}",
14911 Style);
14914 TEST_F(FormatTest, SplitEmptyStruct) {
14915 FormatStyle Style = getLLVMStyle();
14916 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14917 Style.BraceWrapping.AfterStruct = true;
14918 Style.BraceWrapping.SplitEmptyRecord = false;
14920 verifyFormat("struct Foo\n"
14921 "{};",
14922 Style);
14923 verifyFormat("/* something */ struct Foo\n"
14924 "{};",
14925 Style);
14926 verifyFormat("template <typename X> struct Foo\n"
14927 "{};",
14928 Style);
14929 verifyFormat("struct Foo\n"
14930 "{\n"
14931 " Foo();\n"
14932 "};",
14933 Style);
14934 verifyFormat("typedef struct Foo\n"
14935 "{\n"
14936 "} Foo_t;",
14937 Style);
14938 // typedef struct Bar {} Bar_t;
14941 TEST_F(FormatTest, SplitEmptyUnion) {
14942 FormatStyle Style = getLLVMStyle();
14943 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14944 Style.BraceWrapping.AfterUnion = true;
14945 Style.BraceWrapping.SplitEmptyRecord = false;
14947 verifyFormat("union Foo\n"
14948 "{};",
14949 Style);
14950 verifyFormat("/* something */ union Foo\n"
14951 "{};",
14952 Style);
14953 verifyFormat("union Foo\n"
14954 "{\n"
14955 " A,\n"
14956 "};",
14957 Style);
14958 verifyFormat("typedef union Foo\n"
14959 "{\n"
14960 "} Foo_t;",
14961 Style);
14964 TEST_F(FormatTest, SplitEmptyNamespace) {
14965 FormatStyle Style = getLLVMStyle();
14966 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14967 Style.BraceWrapping.AfterNamespace = true;
14968 Style.BraceWrapping.SplitEmptyNamespace = false;
14970 verifyFormat("namespace Foo\n"
14971 "{};",
14972 Style);
14973 verifyFormat("/* something */ namespace Foo\n"
14974 "{};",
14975 Style);
14976 verifyFormat("inline namespace Foo\n"
14977 "{};",
14978 Style);
14979 verifyFormat("/* something */ inline namespace Foo\n"
14980 "{};",
14981 Style);
14982 verifyFormat("export namespace Foo\n"
14983 "{};",
14984 Style);
14985 verifyFormat("namespace Foo\n"
14986 "{\n"
14987 "void Bar();\n"
14988 "};",
14989 Style);
14992 TEST_F(FormatTest, NeverMergeShortRecords) {
14993 FormatStyle Style = getLLVMStyle();
14995 verifyFormat("class Foo {\n"
14996 " Foo();\n"
14997 "};",
14998 Style);
14999 verifyFormat("typedef class Foo {\n"
15000 " Foo();\n"
15001 "} Foo_t;",
15002 Style);
15003 verifyFormat("struct Foo {\n"
15004 " Foo();\n"
15005 "};",
15006 Style);
15007 verifyFormat("typedef struct Foo {\n"
15008 " Foo();\n"
15009 "} Foo_t;",
15010 Style);
15011 verifyFormat("union Foo {\n"
15012 " A,\n"
15013 "};",
15014 Style);
15015 verifyFormat("typedef union Foo {\n"
15016 " A,\n"
15017 "} Foo_t;",
15018 Style);
15019 verifyFormat("namespace Foo {\n"
15020 "void Bar();\n"
15021 "};",
15022 Style);
15024 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15025 Style.BraceWrapping.AfterClass = true;
15026 Style.BraceWrapping.AfterStruct = true;
15027 Style.BraceWrapping.AfterUnion = true;
15028 Style.BraceWrapping.AfterNamespace = true;
15029 verifyFormat("class Foo\n"
15030 "{\n"
15031 " Foo();\n"
15032 "};",
15033 Style);
15034 verifyFormat("typedef class Foo\n"
15035 "{\n"
15036 " Foo();\n"
15037 "} Foo_t;",
15038 Style);
15039 verifyFormat("struct Foo\n"
15040 "{\n"
15041 " Foo();\n"
15042 "};",
15043 Style);
15044 verifyFormat("typedef struct Foo\n"
15045 "{\n"
15046 " Foo();\n"
15047 "} Foo_t;",
15048 Style);
15049 verifyFormat("union Foo\n"
15050 "{\n"
15051 " A,\n"
15052 "};",
15053 Style);
15054 verifyFormat("typedef union Foo\n"
15055 "{\n"
15056 " A,\n"
15057 "} Foo_t;",
15058 Style);
15059 verifyFormat("namespace Foo\n"
15060 "{\n"
15061 "void Bar();\n"
15062 "};",
15063 Style);
15066 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
15067 // Elaborate type variable declarations.
15068 verifyFormat("struct foo a = {bar};\nint n;");
15069 verifyFormat("class foo a = {bar};\nint n;");
15070 verifyFormat("union foo a = {bar};\nint n;");
15072 // Elaborate types inside function definitions.
15073 verifyFormat("struct foo f() {}\nint n;");
15074 verifyFormat("class foo f() {}\nint n;");
15075 verifyFormat("union foo f() {}\nint n;");
15077 // Templates.
15078 verifyFormat("template <class X> void f() {}\nint n;");
15079 verifyFormat("template <struct X> void f() {}\nint n;");
15080 verifyFormat("template <union X> void f() {}\nint n;");
15082 // Actual definitions...
15083 verifyFormat("struct {\n} n;");
15084 verifyFormat(
15085 "template <template <class T, class Y>, class Z> class X {\n} n;");
15086 verifyFormat("union Z {\n int n;\n} x;");
15087 verifyFormat("class MACRO Z {\n} n;");
15088 verifyFormat("class MACRO(X) Z {\n} n;");
15089 verifyFormat("class __attribute__((X)) Z {\n} n;");
15090 verifyFormat("class __declspec(X) Z {\n} n;");
15091 verifyFormat("class A##B##C {\n} n;");
15092 verifyFormat("class alignas(16) Z {\n} n;");
15093 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15094 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15096 // Redefinition from nested context:
15097 verifyFormat("class A::B::C {\n} n;");
15099 // Template definitions.
15100 verifyFormat(
15101 "template <typename F>\n"
15102 "Matcher(const Matcher<F> &Other,\n"
15103 " typename enable_if_c<is_base_of<F, T>::value &&\n"
15104 " !is_same<F, T>::value>::type * = 0)\n"
15105 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15107 // FIXME: This is still incorrectly handled at the formatter side.
15108 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15109 verifyFormat("int i = SomeFunction(a<b, a> b);");
15111 verifyFormat("class A<int> f() {}\n"
15112 "int n;");
15113 verifyFormat("template <typename T> class A<T> f() {}\n"
15114 "int n;");
15116 verifyFormat("template <> class Foo<int> F() {\n"
15117 "} n;");
15119 // Elaborate types where incorrectly parsing the structural element would
15120 // break the indent.
15121 verifyFormat("if (true)\n"
15122 " class X x;\n"
15123 "else\n"
15124 " f();");
15126 // This is simply incomplete. Formatting is not important, but must not crash.
15127 verifyFormat("class A:");
15130 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
15131 verifyNoChange("#error Leave all white!!!!! space* alone!");
15132 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15133 verifyFormat("#error 1", " # error 1");
15134 verifyFormat("#warning 1", " # warning 1");
15137 TEST_F(FormatTest, FormatHashIfExpressions) {
15138 verifyFormat("#if AAAA && BBBB");
15139 verifyFormat("#if (AAAA && BBBB)");
15140 verifyFormat("#elif (AAAA && BBBB)");
15141 // FIXME: Come up with a better indentation for #elif.
15142 verifyFormat(
15143 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15144 " defined(BBBBBBBB)\n"
15145 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15146 " defined(BBBBBBBB)\n"
15147 "#endif",
15148 getLLVMStyleWithColumns(65));
15151 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
15152 FormatStyle AllowsMergedIf = getGoogleStyle();
15153 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
15154 FormatStyle::SIS_WithoutElse;
15155 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
15156 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
15157 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
15158 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
15159 FormatStyle ShortMergedIf = AllowsMergedIf;
15160 ShortMergedIf.ColumnLimit = 25;
15161 verifyFormat("#define A \\\n"
15162 " if (true) return 42;",
15163 ShortMergedIf);
15164 verifyFormat("#define A \\\n"
15165 " f(); \\\n"
15166 " if (true)\n"
15167 "#define B",
15168 ShortMergedIf);
15169 verifyFormat("#define A \\\n"
15170 " f(); \\\n"
15171 " if (true)\n"
15172 "g();",
15173 ShortMergedIf);
15174 verifyFormat("{\n"
15175 "#ifdef A\n"
15176 " // Comment\n"
15177 " if (true) continue;\n"
15178 "#endif\n"
15179 " // Comment\n"
15180 " if (true) continue;\n"
15181 "}",
15182 ShortMergedIf);
15183 ShortMergedIf.ColumnLimit = 33;
15184 verifyFormat("#define A \\\n"
15185 " if constexpr (true) return 42;",
15186 ShortMergedIf);
15187 verifyFormat("#define A \\\n"
15188 " if CONSTEXPR (true) return 42;",
15189 ShortMergedIf);
15190 ShortMergedIf.ColumnLimit = 29;
15191 verifyFormat("#define A \\\n"
15192 " if (aaaaaaaaaa) return 1; \\\n"
15193 " return 2;",
15194 ShortMergedIf);
15195 ShortMergedIf.ColumnLimit = 28;
15196 verifyFormat("#define A \\\n"
15197 " if (aaaaaaaaaa) \\\n"
15198 " return 1; \\\n"
15199 " return 2;",
15200 ShortMergedIf);
15201 verifyFormat("#define A \\\n"
15202 " if constexpr (aaaaaaa) \\\n"
15203 " return 1; \\\n"
15204 " return 2;",
15205 ShortMergedIf);
15206 verifyFormat("#define A \\\n"
15207 " if CONSTEXPR (aaaaaaa) \\\n"
15208 " return 1; \\\n"
15209 " return 2;",
15210 ShortMergedIf);
15212 verifyFormat("//\n"
15213 "#define a \\\n"
15214 " if \\\n"
15215 " 0",
15216 getChromiumStyle(FormatStyle::LK_Cpp));
15219 TEST_F(FormatTest, FormatStarDependingOnContext) {
15220 verifyFormat("void f(int *a);");
15221 verifyFormat("void f() { f(fint * b); }");
15222 verifyFormat("class A {\n void f(int *a);\n};");
15223 verifyFormat("class A {\n int *a;\n};");
15224 verifyFormat("namespace a {\n"
15225 "namespace b {\n"
15226 "class A {\n"
15227 " void f() {}\n"
15228 " int *a;\n"
15229 "};\n"
15230 "} // namespace b\n"
15231 "} // namespace a");
15234 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15235 verifyFormat("while");
15236 verifyFormat("operator");
15239 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15240 // This code would be painfully slow to format if we didn't skip it.
15241 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
15242 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15243 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15244 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15245 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15246 "A(1, 1)\n"
15247 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15248 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15249 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15250 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15251 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15252 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15253 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15254 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15255 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15256 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15257 // Deeply nested part is untouched, rest is formatted.
15258 EXPECT_EQ(std::string("int i;") + Code + "int j;",
15259 format(std::string("int i;") + Code + "int j;",
15260 getLLVMStyle(), SC_ExpectIncomplete));
15263 //===----------------------------------------------------------------------===//
15264 // Objective-C tests.
15265 //===----------------------------------------------------------------------===//
15267 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15268 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15269 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15270 "-(NSUInteger)indexOfObject:(id)anObject;");
15271 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15272 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15273 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15274 "-(NSInteger)Method3:(id)anObject;");
15275 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15276 "-(NSInteger)Method4:(id)anObject;");
15277 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15278 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15279 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15280 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15281 "forAllCells:(BOOL)flag;");
15283 // Very long objectiveC method declaration.
15284 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15285 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15286 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15287 " inRange:(NSRange)range\n"
15288 " outRange:(NSRange)out_range\n"
15289 " outRange1:(NSRange)out_range1\n"
15290 " outRange2:(NSRange)out_range2\n"
15291 " outRange3:(NSRange)out_range3\n"
15292 " outRange4:(NSRange)out_range4\n"
15293 " outRange5:(NSRange)out_range5\n"
15294 " outRange6:(NSRange)out_range6\n"
15295 " outRange7:(NSRange)out_range7\n"
15296 " outRange8:(NSRange)out_range8\n"
15297 " outRange9:(NSRange)out_range9;");
15299 // When the function name has to be wrapped.
15300 FormatStyle Style = getLLVMStyle();
15301 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15302 // and always indents instead.
15303 Style.IndentWrappedFunctionNames = false;
15304 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15305 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15306 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15307 "}",
15308 Style);
15309 Style.IndentWrappedFunctionNames = true;
15310 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15311 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15312 " anotherName:(NSString)dddddddddddddd {\n"
15313 "}",
15314 Style);
15316 verifyFormat("- (int)sum:(vector<int>)numbers;");
15317 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15318 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15319 // protocol lists (but not for template classes):
15320 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15322 verifyFormat("- (int (*)())foo:(int (*)())f;");
15323 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15325 // If there's no return type (very rare in practice!), LLVM and Google style
15326 // agree.
15327 verifyFormat("- foo;");
15328 verifyFormat("- foo:(int)f;");
15329 verifyGoogleFormat("- foo:(int)foo;");
15332 TEST_F(FormatTest, BreaksStringLiterals) {
15333 // FIXME: unstable test case
15334 EXPECT_EQ("\"some text \"\n"
15335 "\"other\";",
15336 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15337 // FIXME: unstable test case
15338 EXPECT_EQ("\"some text \"\n"
15339 "\"other\";",
15340 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15341 verifyFormat("#define A \\\n"
15342 " \"some \" \\\n"
15343 " \"text \" \\\n"
15344 " \"other\";",
15345 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15346 verifyFormat("#define A \\\n"
15347 " \"so \" \\\n"
15348 " \"text \" \\\n"
15349 " \"other\";",
15350 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15352 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15353 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15354 // FIXME: unstable test case
15355 EXPECT_EQ("\"some \"\n"
15356 "\"text\"",
15357 format("\"some text\"", getLLVMStyleWithColumns(10)));
15358 // FIXME: unstable test case
15359 EXPECT_EQ("\"some \"\n"
15360 "\"text\"",
15361 format("\"some text\"", getLLVMStyleWithColumns(7)));
15362 // FIXME: unstable test case
15363 EXPECT_EQ("\"some\"\n"
15364 "\" tex\"\n"
15365 "\"t\"",
15366 format("\"some text\"", getLLVMStyleWithColumns(6)));
15367 // FIXME: unstable test case
15368 EXPECT_EQ("\"some\"\n"
15369 "\" tex\"\n"
15370 "\" and\"",
15371 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15372 // FIXME: unstable test case
15373 EXPECT_EQ("\"some\"\n"
15374 "\"/tex\"\n"
15375 "\"/and\"",
15376 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15378 verifyFormat("variable =\n"
15379 " \"long string \"\n"
15380 " \"literal\";",
15381 "variable = \"long string literal\";",
15382 getLLVMStyleWithColumns(20));
15384 verifyFormat("variable = f(\n"
15385 " \"long string \"\n"
15386 " \"literal\",\n"
15387 " short,\n"
15388 " loooooooooooooooooooong);",
15389 "variable = f(\"long string literal\", short, "
15390 "loooooooooooooooooooong);",
15391 getLLVMStyleWithColumns(20));
15393 verifyFormat("f(g(\"long string \"\n"
15394 " \"literal\"),\n"
15395 " b);",
15396 "f(g(\"long string literal\"), b);",
15397 getLLVMStyleWithColumns(20));
15398 verifyFormat("f(g(\"long string \"\n"
15399 " \"literal\",\n"
15400 " a),\n"
15401 " b);",
15402 "f(g(\"long string literal\", a), b);",
15403 getLLVMStyleWithColumns(20));
15404 verifyFormat("f(\"one two\".split(\n"
15405 " variable));",
15406 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15407 verifyFormat("f(\"one two three four five six \"\n"
15408 " \"seven\".split(\n"
15409 " really_looooong_variable));",
15410 "f(\"one two three four five six seven\"."
15411 "split(really_looooong_variable));",
15412 getLLVMStyleWithColumns(33));
15414 verifyFormat("f(\"some \"\n"
15415 " \"text\",\n"
15416 " other);",
15417 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15419 // Only break as a last resort.
15420 verifyFormat(
15421 "aaaaaaaaaaaaaaaaaaaa(\n"
15422 " aaaaaaaaaaaaaaaaaaaa,\n"
15423 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15425 // FIXME: unstable test case
15426 EXPECT_EQ("\"splitmea\"\n"
15427 "\"trandomp\"\n"
15428 "\"oint\"",
15429 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15431 // FIXME: unstable test case
15432 EXPECT_EQ("\"split/\"\n"
15433 "\"pathat/\"\n"
15434 "\"slashes\"",
15435 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15437 // FIXME: unstable test case
15438 EXPECT_EQ("\"split/\"\n"
15439 "\"pathat/\"\n"
15440 "\"slashes\"",
15441 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15442 // FIXME: unstable test case
15443 EXPECT_EQ("\"split at \"\n"
15444 "\"spaces/at/\"\n"
15445 "\"slashes.at.any$\"\n"
15446 "\"non-alphanumeric%\"\n"
15447 "\"1111111111characte\"\n"
15448 "\"rs\"",
15449 format("\"split at "
15450 "spaces/at/"
15451 "slashes.at."
15452 "any$non-"
15453 "alphanumeric%"
15454 "1111111111characte"
15455 "rs\"",
15456 getLLVMStyleWithColumns(20)));
15458 // Verify that splitting the strings understands
15459 // Style::AlwaysBreakBeforeMultilineStrings.
15460 verifyFormat("aaaaaaaaaaaa(\n"
15461 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15462 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15463 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15464 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15465 "aaaaaaaaaaaaaaaaaaaaaa\");",
15466 getGoogleStyle());
15467 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15468 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15469 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15470 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15471 "aaaaaaaaaaaaaaaaaaaaaa\";",
15472 getGoogleStyle());
15473 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15474 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15475 "llvm::outs() << "
15476 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15477 "aaaaaaaaaaaaaaaaaaa\";");
15478 verifyFormat("ffff(\n"
15479 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15480 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15481 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15482 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15483 getGoogleStyle());
15485 FormatStyle Style = getLLVMStyleWithColumns(12);
15486 Style.BreakStringLiterals = false;
15487 verifyFormat("\"some text other\";", Style);
15489 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
15490 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15491 verifyFormat("#define A \\\n"
15492 " \"some \" \\\n"
15493 " \"text \" \\\n"
15494 " \"other\";",
15495 "#define A \"some text other\";", AlignLeft);
15498 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
15499 verifyFormat("C a = \"some more \"\n"
15500 " \"text\";",
15501 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15504 TEST_F(FormatTest, FullyRemoveEmptyLines) {
15505 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
15506 NoEmptyLines.MaxEmptyLinesToKeep = 0;
15507 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15508 NoEmptyLines);
15511 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
15512 // FIXME: unstable test case
15513 EXPECT_EQ(
15514 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15515 "(\n"
15516 " \"x\t\");",
15517 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15518 "aaaaaaa("
15519 "\"x\t\");"));
15522 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
15523 // FIXME: unstable test case
15524 EXPECT_EQ(
15525 "u8\"utf8 string \"\n"
15526 "u8\"literal\";",
15527 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15528 // FIXME: unstable test case
15529 EXPECT_EQ(
15530 "u\"utf16 string \"\n"
15531 "u\"literal\";",
15532 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15533 // FIXME: unstable test case
15534 EXPECT_EQ(
15535 "U\"utf32 string \"\n"
15536 "U\"literal\";",
15537 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15538 // FIXME: unstable test case
15539 EXPECT_EQ("L\"wide string \"\n"
15540 "L\"literal\";",
15541 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15542 verifyFormat("@\"NSString \"\n"
15543 "@\"literal\";",
15544 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15545 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
15547 // This input makes clang-format try to split the incomplete unicode escape
15548 // sequence, which used to lead to a crasher.
15549 verifyNoCrash(
15550 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15551 getLLVMStyleWithColumns(60));
15554 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
15555 FormatStyle Style = getGoogleStyleWithColumns(15);
15556 verifyFormat("R\"x(raw literal)x\";", Style);
15557 verifyFormat("uR\"x(raw literal)x\";", Style);
15558 verifyFormat("LR\"x(raw literal)x\";", Style);
15559 verifyFormat("UR\"x(raw literal)x\";", Style);
15560 verifyFormat("u8R\"x(raw literal)x\";", Style);
15563 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
15564 FormatStyle Style = getLLVMStyleWithColumns(20);
15565 // FIXME: unstable test case
15566 EXPECT_EQ(
15567 "_T(\"aaaaaaaaaaaaaa\")\n"
15568 "_T(\"aaaaaaaaaaaaaa\")\n"
15569 "_T(\"aaaaaaaaaaaa\")",
15570 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
15571 verifyFormat("f(x,\n"
15572 " _T(\"aaaaaaaaaaaa\")\n"
15573 " _T(\"aaa\"),\n"
15574 " z);",
15575 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
15577 // FIXME: Handle embedded spaces in one iteration.
15578 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15579 // "_T(\"aaaaaaaaaaaaa\")\n"
15580 // "_T(\"aaaaaaaaaaaaa\")\n"
15581 // "_T(\"a\")",
15582 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15583 // getLLVMStyleWithColumns(20)));
15584 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15585 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
15586 verifyFormat("f(\n"
15587 "#if !TEST\n"
15588 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15589 "#endif\n"
15590 ");",
15591 "f(\n"
15592 "#if !TEST\n"
15593 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15594 "#endif\n"
15595 ");");
15596 verifyFormat("f(\n"
15597 "\n"
15598 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15599 "f(\n"
15600 "\n"
15601 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15602 // Regression test for accessing tokens past the end of a vector in the
15603 // TokenLexer.
15604 verifyNoCrash(R"(_T(
15607 )");
15610 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15611 // In a function call with two operands, the second can be broken with no line
15612 // break before it.
15613 verifyFormat("func(a, \"long long \"\n"
15614 " \"long long\");",
15615 "func(a, \"long long long long\");",
15616 getLLVMStyleWithColumns(24));
15617 // In a function call with three operands, the second must be broken with a
15618 // line break before it.
15619 verifyFormat("func(a,\n"
15620 " \"long long long \"\n"
15621 " \"long\",\n"
15622 " c);",
15623 "func(a, \"long long long long\", c);",
15624 getLLVMStyleWithColumns(24));
15625 // In a function call with three operands, the third must be broken with a
15626 // line break before it.
15627 verifyFormat("func(a, b,\n"
15628 " \"long long long \"\n"
15629 " \"long\");",
15630 "func(a, b, \"long long long long\");",
15631 getLLVMStyleWithColumns(24));
15632 // In a function call with three operands, both the second and the third must
15633 // be broken with a line break before them.
15634 verifyFormat("func(a,\n"
15635 " \"long long long \"\n"
15636 " \"long\",\n"
15637 " \"long long long \"\n"
15638 " \"long\");",
15639 "func(a, \"long long long long\", \"long long long long\");",
15640 getLLVMStyleWithColumns(24));
15641 // In a chain of << with two operands, the second can be broken with no line
15642 // break before it.
15643 verifyFormat("a << \"line line \"\n"
15644 " \"line\";",
15645 "a << \"line line line\";", getLLVMStyleWithColumns(20));
15646 // In a chain of << with three operands, the second can be broken with no line
15647 // break before it.
15648 verifyFormat("abcde << \"line \"\n"
15649 " \"line line\"\n"
15650 " << c;",
15651 "abcde << \"line line line\" << c;",
15652 getLLVMStyleWithColumns(20));
15653 // In a chain of << with three operands, the third must be broken with a line
15654 // break before it.
15655 verifyFormat("a << b\n"
15656 " << \"line line \"\n"
15657 " \"line\";",
15658 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15659 // In a chain of << with three operands, the second can be broken with no line
15660 // break before it and the third must be broken with a line break before it.
15661 verifyFormat("abcd << \"line line \"\n"
15662 " \"line\"\n"
15663 " << \"line line \"\n"
15664 " \"line\";",
15665 "abcd << \"line line line\" << \"line line line\";",
15666 getLLVMStyleWithColumns(20));
15667 // In a chain of binary operators with two operands, the second can be broken
15668 // with no line break before it.
15669 verifyFormat("abcd + \"line line \"\n"
15670 " \"line line\";",
15671 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15672 // In a chain of binary operators with three operands, the second must be
15673 // broken with a line break before it.
15674 verifyFormat("abcd +\n"
15675 " \"line line \"\n"
15676 " \"line line\" +\n"
15677 " e;",
15678 "abcd + \"line line line line\" + e;",
15679 getLLVMStyleWithColumns(20));
15680 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15681 // the first must be broken with a line break before it.
15682 FormatStyle Style = getLLVMStyleWithColumns(25);
15683 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15684 verifyFormat("someFunction(\n"
15685 " \"long long long \"\n"
15686 " \"long\",\n"
15687 " a);",
15688 "someFunction(\"long long long long\", a);", Style);
15689 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15690 verifyFormat("someFunction(\n"
15691 " \"long long long \"\n"
15692 " \"long\",\n"
15693 " a\n"
15694 ");",
15695 Style);
15698 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15699 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15702 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
15707 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15708 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
15709 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
15710 verifyFormat("fffffffffff(g(R\"x(\n"
15711 "multiline raw string literal xxxxxxxxxxxxxx\n"
15712 ")x\",\n"
15713 " a),\n"
15714 " b);",
15715 "fffffffffff(g(R\"x(\n"
15716 "multiline raw string literal xxxxxxxxxxxxxx\n"
15717 ")x\", a), b);",
15718 getGoogleStyleWithColumns(20));
15719 verifyFormat("fffffffffff(\n"
15720 " g(R\"x(qqq\n"
15721 "multiline raw string literal xxxxxxxxxxxxxx\n"
15722 ")x\",\n"
15723 " a),\n"
15724 " b);",
15725 "fffffffffff(g(R\"x(qqq\n"
15726 "multiline raw string literal xxxxxxxxxxxxxx\n"
15727 ")x\", a), b);",
15728 getGoogleStyleWithColumns(20));
15730 verifyNoChange("fffffffffff(R\"x(\n"
15731 "multiline raw string literal xxxxxxxxxxxxxx\n"
15732 ")x\");",
15733 getGoogleStyleWithColumns(20));
15734 verifyFormat("fffffffffff(R\"x(\n"
15735 "multiline raw string literal xxxxxxxxxxxxxx\n"
15736 ")x\" + bbbbbb);",
15737 "fffffffffff(R\"x(\n"
15738 "multiline raw string literal xxxxxxxxxxxxxx\n"
15739 ")x\" + bbbbbb);",
15740 getGoogleStyleWithColumns(20));
15741 verifyFormat("fffffffffff(\n"
15742 " R\"x(\n"
15743 "multiline raw string literal xxxxxxxxxxxxxx\n"
15744 ")x\" +\n"
15745 " bbbbbb);",
15746 "fffffffffff(\n"
15747 " R\"x(\n"
15748 "multiline raw string literal xxxxxxxxxxxxxx\n"
15749 ")x\" + bbbbbb);",
15750 getGoogleStyleWithColumns(20));
15751 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
15752 "fffffffffff(\n"
15753 " R\"(single line raw string)\" + bbbbbb);");
15756 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15757 verifyFormat("string a = \"unterminated;");
15758 verifyFormat("function(\"unterminated,\n"
15759 " OtherParameter);",
15760 "function( \"unterminated,\n"
15761 " OtherParameter);");
15764 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15765 FormatStyle Style = getLLVMStyle();
15766 Style.Standard = FormatStyle::LS_Cpp03;
15767 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15768 "#define x(_a) printf(\"foo\"_a);", Style);
15771 TEST_F(FormatTest, CppLexVersion) {
15772 FormatStyle Style = getLLVMStyle();
15773 // Formatting of x * y differs if x is a type.
15774 verifyFormat("void foo() { MACRO(a * b); }", Style);
15775 verifyFormat("void foo() { MACRO(int *b); }", Style);
15777 // LLVM style uses latest lexer.
15778 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15779 Style.Standard = FormatStyle::LS_Cpp17;
15780 // But in c++17, char8_t isn't a keyword.
15781 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15784 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15786 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15787 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15788 " \"ddeeefff\");",
15789 "someFunction(\"aaabbbcccdddeeefff\");",
15790 getLLVMStyleWithColumns(25));
15791 verifyFormat("someFunction1234567890(\n"
15792 " \"aaabbbcccdddeeefff\");",
15793 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15794 getLLVMStyleWithColumns(26));
15795 verifyFormat("someFunction1234567890(\n"
15796 " \"aaabbbcccdddeeeff\"\n"
15797 " \"f\");",
15798 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15799 getLLVMStyleWithColumns(25));
15800 verifyFormat("someFunction1234567890(\n"
15801 " \"aaabbbcccdddeeeff\"\n"
15802 " \"f\");",
15803 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15804 getLLVMStyleWithColumns(24));
15805 verifyFormat("someFunction(\n"
15806 " \"aaabbbcc ddde \"\n"
15807 " \"efff\");",
15808 "someFunction(\"aaabbbcc ddde efff\");",
15809 getLLVMStyleWithColumns(25));
15810 verifyFormat("someFunction(\"aaabbbccc \"\n"
15811 " \"ddeeefff\");",
15812 "someFunction(\"aaabbbccc ddeeefff\");",
15813 getLLVMStyleWithColumns(25));
15814 verifyFormat("someFunction1234567890(\n"
15815 " \"aaabb \"\n"
15816 " \"cccdddeeefff\");",
15817 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15818 getLLVMStyleWithColumns(25));
15819 verifyFormat("#define A \\\n"
15820 " string s = \\\n"
15821 " \"123456789\" \\\n"
15822 " \"0\"; \\\n"
15823 " int i;",
15824 "#define A string s = \"1234567890\"; int i;",
15825 getLLVMStyleWithColumns(20));
15826 verifyFormat("someFunction(\n"
15827 " \"aaabbbcc \"\n"
15828 " \"dddeeefff\");",
15829 "someFunction(\"aaabbbcc dddeeefff\");",
15830 getLLVMStyleWithColumns(25));
15833 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15834 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15835 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15836 // FIXME: unstable test case
15837 EXPECT_EQ("\"test\"\n"
15838 "\"\\n\"",
15839 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15840 // FIXME: unstable test case
15841 EXPECT_EQ("\"tes\\\\\"\n"
15842 "\"n\"",
15843 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15844 // FIXME: unstable test case
15845 EXPECT_EQ("\"\\\\\\\\\"\n"
15846 "\"\\n\"",
15847 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15848 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15849 // FIXME: unstable test case
15850 EXPECT_EQ("\"\\uff01\"\n"
15851 "\"test\"",
15852 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15853 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15854 // FIXME: unstable test case
15855 EXPECT_EQ("\"\\x000000000001\"\n"
15856 "\"next\"",
15857 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15858 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15859 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15860 // FIXME: unstable test case
15861 EXPECT_EQ("\"test\"\n"
15862 "\"\\000000\"\n"
15863 "\"000001\"",
15864 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15865 // FIXME: unstable test case
15866 EXPECT_EQ("\"test\\000\"\n"
15867 "\"00000000\"\n"
15868 "\"1\"",
15869 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15872 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15873 verifyFormat("void f() {\n"
15874 " return g() {}\n"
15875 " void h() {}");
15876 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15877 "g();\n"
15878 "}");
15881 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15882 verifyFormat(
15883 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15886 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15887 verifyFormat("class X {\n"
15888 " void f() {\n"
15889 " }\n"
15890 "};",
15891 getLLVMStyleWithColumns(12));
15894 TEST_F(FormatTest, ConfigurableIndentWidth) {
15895 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15896 EightIndent.IndentWidth = 8;
15897 EightIndent.ContinuationIndentWidth = 8;
15898 verifyFormat("void f() {\n"
15899 " someFunction();\n"
15900 " if (true) {\n"
15901 " f();\n"
15902 " }\n"
15903 "}",
15904 EightIndent);
15905 verifyFormat("class X {\n"
15906 " void f() {\n"
15907 " }\n"
15908 "};",
15909 EightIndent);
15910 verifyFormat("int x[] = {\n"
15911 " call(),\n"
15912 " call()};",
15913 EightIndent);
15916 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15917 verifyFormat("double\n"
15918 "f();",
15919 getLLVMStyleWithColumns(8));
15922 TEST_F(FormatTest, ConfigurableUseOfTab) {
15923 FormatStyle Tab = getLLVMStyleWithColumns(42);
15924 Tab.IndentWidth = 8;
15925 Tab.UseTab = FormatStyle::UT_Always;
15926 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15928 verifyFormat("if (aaaaaaaa && // q\n"
15929 " bb)\t\t// w\n"
15930 "\t;",
15931 "if (aaaaaaaa &&// q\n"
15932 "bb)// w\n"
15933 ";",
15934 Tab);
15935 verifyFormat("if (aaa && bbb) // w\n"
15936 "\t;",
15937 "if(aaa&&bbb)// w\n"
15938 ";",
15939 Tab);
15941 verifyFormat("class X {\n"
15942 "\tvoid f() {\n"
15943 "\t\tsomeFunction(parameter1,\n"
15944 "\t\t\t parameter2);\n"
15945 "\t}\n"
15946 "};",
15947 Tab);
15948 verifyFormat("#define A \\\n"
15949 "\tvoid f() { \\\n"
15950 "\t\tsomeFunction( \\\n"
15951 "\t\t parameter1, \\\n"
15952 "\t\t parameter2); \\\n"
15953 "\t}",
15954 Tab);
15955 verifyFormat("int a;\t // x\n"
15956 "int bbbbbbbb; // x",
15957 Tab);
15959 FormatStyle TabAlignment = Tab;
15960 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
15961 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15962 verifyFormat("unsigned long long big;\n"
15963 "char*\t\t ptr;",
15964 TabAlignment);
15965 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15966 verifyFormat("unsigned long long big;\n"
15967 "char *\t\t ptr;",
15968 TabAlignment);
15969 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15970 verifyFormat("unsigned long long big;\n"
15971 "char\t\t *ptr;",
15972 TabAlignment);
15974 Tab.TabWidth = 4;
15975 Tab.IndentWidth = 8;
15976 verifyFormat("class TabWidth4Indent8 {\n"
15977 "\t\tvoid f() {\n"
15978 "\t\t\t\tsomeFunction(parameter1,\n"
15979 "\t\t\t\t\t\t\t parameter2);\n"
15980 "\t\t}\n"
15981 "};",
15982 Tab);
15984 Tab.TabWidth = 4;
15985 Tab.IndentWidth = 4;
15986 verifyFormat("class TabWidth4Indent4 {\n"
15987 "\tvoid f() {\n"
15988 "\t\tsomeFunction(parameter1,\n"
15989 "\t\t\t\t\t parameter2);\n"
15990 "\t}\n"
15991 "};",
15992 Tab);
15994 Tab.TabWidth = 8;
15995 Tab.IndentWidth = 4;
15996 verifyFormat("class TabWidth8Indent4 {\n"
15997 " void f() {\n"
15998 "\tsomeFunction(parameter1,\n"
15999 "\t\t parameter2);\n"
16000 " }\n"
16001 "};",
16002 Tab);
16004 Tab.TabWidth = 8;
16005 Tab.IndentWidth = 8;
16006 verifyFormat("/*\n"
16007 "\t a\t\tcomment\n"
16008 "\t in multiple lines\n"
16009 " */",
16010 " /*\t \t \n"
16011 " \t \t a\t\tcomment\t \t\n"
16012 " \t \t in multiple lines\t\n"
16013 " \t */",
16014 Tab);
16016 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
16017 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16018 verifyFormat("void f() {\n"
16019 "\tunsigned long long big;\n"
16020 "\tchar* ptr;\n"
16021 "}",
16022 TabAlignment);
16023 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16024 verifyFormat("void f() {\n"
16025 "\tunsigned long long big;\n"
16026 "\tchar * ptr;\n"
16027 "}",
16028 TabAlignment);
16029 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16030 verifyFormat("void f() {\n"
16031 "\tunsigned long long big;\n"
16032 "\tchar *ptr;\n"
16033 "}",
16034 TabAlignment);
16036 Tab.UseTab = FormatStyle::UT_ForIndentation;
16037 verifyFormat("{\n"
16038 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16039 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16040 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16041 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16042 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16043 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16044 "};",
16045 Tab);
16046 verifyFormat("enum AA {\n"
16047 "\ta1, // Force multiple lines\n"
16048 "\ta2,\n"
16049 "\ta3\n"
16050 "};",
16051 Tab);
16052 verifyFormat("if (aaaaaaaa && // q\n"
16053 " bb) // w\n"
16054 "\t;",
16055 "if (aaaaaaaa &&// q\n"
16056 "bb)// w\n"
16057 ";",
16058 Tab);
16059 verifyFormat("class X {\n"
16060 "\tvoid f() {\n"
16061 "\t\tsomeFunction(parameter1,\n"
16062 "\t\t parameter2);\n"
16063 "\t}\n"
16064 "};",
16065 Tab);
16066 verifyFormat("{\n"
16067 "\tQ(\n"
16068 "\t {\n"
16069 "\t\t int a;\n"
16070 "\t\t someFunction(aaaaaaaa,\n"
16071 "\t\t bbbbbbb);\n"
16072 "\t },\n"
16073 "\t p);\n"
16074 "}",
16075 Tab);
16076 verifyFormat("{\n"
16077 "\t/* aaaa\n"
16078 "\t bbbb */\n"
16079 "}",
16080 "{\n"
16081 "/* aaaa\n"
16082 " bbbb */\n"
16083 "}",
16084 Tab);
16085 verifyFormat("{\n"
16086 "\t/*\n"
16087 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16088 "\t bbbbbbbbbbbbb\n"
16089 "\t*/\n"
16090 "}",
16091 "{\n"
16092 "/*\n"
16093 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16094 "*/\n"
16095 "}",
16096 Tab);
16097 verifyFormat("{\n"
16098 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16099 "\t// bbbbbbbbbbbbb\n"
16100 "}",
16101 "{\n"
16102 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16103 "}",
16104 Tab);
16105 verifyFormat("{\n"
16106 "\t/*\n"
16107 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16108 "\t bbbbbbbbbbbbb\n"
16109 "\t*/\n"
16110 "}",
16111 "{\n"
16112 "\t/*\n"
16113 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16114 "\t*/\n"
16115 "}",
16116 Tab);
16117 verifyNoChange("{\n"
16118 "\t/*\n"
16119 "\n"
16120 "\t*/\n"
16121 "}",
16122 Tab);
16123 verifyNoChange("{\n"
16124 "\t/*\n"
16125 " asdf\n"
16126 "\t*/\n"
16127 "}",
16128 Tab);
16130 verifyFormat("void f() {\n"
16131 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16132 "\t : bbbbbbbbbbbbbbbbbb\n"
16133 "}",
16134 Tab);
16135 FormatStyle TabNoBreak = Tab;
16136 TabNoBreak.BreakBeforeTernaryOperators = false;
16137 verifyFormat("void f() {\n"
16138 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16139 "\t bbbbbbbbbbbbbbbbbb\n"
16140 "}",
16141 TabNoBreak);
16142 verifyFormat("void f() {\n"
16143 "\treturn true ?\n"
16144 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16145 "\t bbbbbbbbbbbbbbbbbbbb\n"
16146 "}",
16147 TabNoBreak);
16149 Tab.UseTab = FormatStyle::UT_Never;
16150 verifyFormat("/*\n"
16151 " a\t\tcomment\n"
16152 " in multiple lines\n"
16153 " */",
16154 " /*\t \t \n"
16155 " \t \t a\t\tcomment\t \t\n"
16156 " \t \t in multiple lines\t\n"
16157 " \t */",
16158 Tab);
16159 verifyFormat("/* some\n"
16160 " comment */",
16161 " \t \t /* some\n"
16162 " \t \t comment */",
16163 Tab);
16164 verifyFormat("int a; /* some\n"
16165 " comment */",
16166 " \t \t int a; /* some\n"
16167 " \t \t comment */",
16168 Tab);
16170 verifyFormat("int a; /* some\n"
16171 "comment */",
16172 " \t \t int\ta; /* some\n"
16173 " \t \t comment */",
16174 Tab);
16175 verifyFormat("f(\"\t\t\"); /* some\n"
16176 " comment */",
16177 " \t \t f(\"\t\t\"); /* some\n"
16178 " \t \t comment */",
16179 Tab);
16180 verifyFormat("{\n"
16181 " /*\n"
16182 " * Comment\n"
16183 " */\n"
16184 " int i;\n"
16185 "}",
16186 "{\n"
16187 "\t/*\n"
16188 "\t * Comment\n"
16189 "\t */\n"
16190 "\t int i;\n"
16191 "}",
16192 Tab);
16194 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16195 Tab.TabWidth = 8;
16196 Tab.IndentWidth = 8;
16197 verifyFormat("if (aaaaaaaa && // q\n"
16198 " bb) // w\n"
16199 "\t;",
16200 "if (aaaaaaaa &&// q\n"
16201 "bb)// w\n"
16202 ";",
16203 Tab);
16204 verifyFormat("if (aaa && bbb) // w\n"
16205 "\t;",
16206 "if(aaa&&bbb)// w\n"
16207 ";",
16208 Tab);
16209 verifyFormat("class X {\n"
16210 "\tvoid f() {\n"
16211 "\t\tsomeFunction(parameter1,\n"
16212 "\t\t\t parameter2);\n"
16213 "\t}\n"
16214 "};",
16215 Tab);
16216 verifyFormat("#define A \\\n"
16217 "\tvoid f() { \\\n"
16218 "\t\tsomeFunction( \\\n"
16219 "\t\t parameter1, \\\n"
16220 "\t\t parameter2); \\\n"
16221 "\t}",
16222 Tab);
16223 Tab.TabWidth = 4;
16224 Tab.IndentWidth = 8;
16225 verifyFormat("class TabWidth4Indent8 {\n"
16226 "\t\tvoid f() {\n"
16227 "\t\t\t\tsomeFunction(parameter1,\n"
16228 "\t\t\t\t\t\t\t parameter2);\n"
16229 "\t\t}\n"
16230 "};",
16231 Tab);
16232 Tab.TabWidth = 4;
16233 Tab.IndentWidth = 4;
16234 verifyFormat("class TabWidth4Indent4 {\n"
16235 "\tvoid f() {\n"
16236 "\t\tsomeFunction(parameter1,\n"
16237 "\t\t\t\t\t parameter2);\n"
16238 "\t}\n"
16239 "};",
16240 Tab);
16241 Tab.TabWidth = 8;
16242 Tab.IndentWidth = 4;
16243 verifyFormat("class TabWidth8Indent4 {\n"
16244 " void f() {\n"
16245 "\tsomeFunction(parameter1,\n"
16246 "\t\t parameter2);\n"
16247 " }\n"
16248 "};",
16249 Tab);
16250 Tab.TabWidth = 8;
16251 Tab.IndentWidth = 8;
16252 verifyFormat("/*\n"
16253 "\t a\t\tcomment\n"
16254 "\t in multiple lines\n"
16255 " */",
16256 " /*\t \t \n"
16257 " \t \t a\t\tcomment\t \t\n"
16258 " \t \t in multiple lines\t\n"
16259 " \t */",
16260 Tab);
16261 verifyFormat("{\n"
16262 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16263 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16264 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16265 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16266 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16267 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16268 "};",
16269 Tab);
16270 verifyFormat("enum AA {\n"
16271 "\ta1, // Force multiple lines\n"
16272 "\ta2,\n"
16273 "\ta3\n"
16274 "};",
16275 Tab);
16276 verifyFormat("if (aaaaaaaa && // q\n"
16277 " bb) // w\n"
16278 "\t;",
16279 "if (aaaaaaaa &&// q\n"
16280 "bb)// w\n"
16281 ";",
16282 Tab);
16283 verifyFormat("class X {\n"
16284 "\tvoid f() {\n"
16285 "\t\tsomeFunction(parameter1,\n"
16286 "\t\t\t parameter2);\n"
16287 "\t}\n"
16288 "};",
16289 Tab);
16290 verifyFormat("{\n"
16291 "\tQ(\n"
16292 "\t {\n"
16293 "\t\t int a;\n"
16294 "\t\t someFunction(aaaaaaaa,\n"
16295 "\t\t\t\t bbbbbbb);\n"
16296 "\t },\n"
16297 "\t p);\n"
16298 "}",
16299 Tab);
16300 verifyFormat("{\n"
16301 "\t/* aaaa\n"
16302 "\t bbbb */\n"
16303 "}",
16304 "{\n"
16305 "/* aaaa\n"
16306 " bbbb */\n"
16307 "}",
16308 Tab);
16309 verifyFormat("{\n"
16310 "\t/*\n"
16311 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16312 "\t bbbbbbbbbbbbb\n"
16313 "\t*/\n"
16314 "}",
16315 "{\n"
16316 "/*\n"
16317 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16318 "*/\n"
16319 "}",
16320 Tab);
16321 verifyFormat("{\n"
16322 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16323 "\t// bbbbbbbbbbbbb\n"
16324 "}",
16325 "{\n"
16326 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16327 "}",
16328 Tab);
16329 verifyFormat("{\n"
16330 "\t/*\n"
16331 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16332 "\t bbbbbbbbbbbbb\n"
16333 "\t*/\n"
16334 "}",
16335 "{\n"
16336 "\t/*\n"
16337 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16338 "\t*/\n"
16339 "}",
16340 Tab);
16341 verifyNoChange("{\n"
16342 "\t/*\n"
16343 "\n"
16344 "\t*/\n"
16345 "}",
16346 Tab);
16347 verifyNoChange("{\n"
16348 "\t/*\n"
16349 " asdf\n"
16350 "\t*/\n"
16351 "}",
16352 Tab);
16353 verifyFormat("/* some\n"
16354 " comment */",
16355 " \t \t /* some\n"
16356 " \t \t comment */",
16357 Tab);
16358 verifyFormat("int a; /* some\n"
16359 " comment */",
16360 " \t \t int a; /* some\n"
16361 " \t \t comment */",
16362 Tab);
16363 verifyFormat("int a; /* some\n"
16364 "comment */",
16365 " \t \t int\ta; /* some\n"
16366 " \t \t comment */",
16367 Tab);
16368 verifyFormat("f(\"\t\t\"); /* some\n"
16369 " comment */",
16370 " \t \t f(\"\t\t\"); /* some\n"
16371 " \t \t comment */",
16372 Tab);
16373 verifyFormat("{\n"
16374 "\t/*\n"
16375 "\t * Comment\n"
16376 "\t */\n"
16377 "\tint i;\n"
16378 "}",
16379 "{\n"
16380 "\t/*\n"
16381 "\t * Comment\n"
16382 "\t */\n"
16383 "\t int i;\n"
16384 "}",
16385 Tab);
16386 Tab.TabWidth = 2;
16387 Tab.IndentWidth = 2;
16388 verifyFormat("{\n"
16389 "\t/* aaaa\n"
16390 "\t\t bbbb */\n"
16391 "}",
16392 "{\n"
16393 "/* aaaa\n"
16394 "\t bbbb */\n"
16395 "}",
16396 Tab);
16397 verifyFormat("{\n"
16398 "\t/*\n"
16399 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16400 "\t\tbbbbbbbbbbbbb\n"
16401 "\t*/\n"
16402 "}",
16403 "{\n"
16404 "/*\n"
16405 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16406 "*/\n"
16407 "}",
16408 Tab);
16409 Tab.AlignConsecutiveAssignments.Enabled = true;
16410 Tab.AlignConsecutiveDeclarations.Enabled = true;
16411 Tab.TabWidth = 4;
16412 Tab.IndentWidth = 4;
16413 verifyFormat("class Assign {\n"
16414 "\tvoid f() {\n"
16415 "\t\tint x = 123;\n"
16416 "\t\tint random = 4;\n"
16417 "\t\tstd::string alphabet =\n"
16418 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16419 "\t}\n"
16420 "};",
16421 Tab);
16423 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16424 Tab.TabWidth = 8;
16425 Tab.IndentWidth = 8;
16426 verifyFormat("if (aaaaaaaa && // q\n"
16427 " bb) // w\n"
16428 "\t;",
16429 "if (aaaaaaaa &&// q\n"
16430 "bb)// w\n"
16431 ";",
16432 Tab);
16433 verifyFormat("if (aaa && bbb) // w\n"
16434 "\t;",
16435 "if(aaa&&bbb)// w\n"
16436 ";",
16437 Tab);
16438 verifyFormat("class X {\n"
16439 "\tvoid f() {\n"
16440 "\t\tsomeFunction(parameter1,\n"
16441 "\t\t parameter2);\n"
16442 "\t}\n"
16443 "};",
16444 Tab);
16445 verifyFormat("#define A \\\n"
16446 "\tvoid f() { \\\n"
16447 "\t\tsomeFunction( \\\n"
16448 "\t\t parameter1, \\\n"
16449 "\t\t parameter2); \\\n"
16450 "\t}",
16451 Tab);
16452 Tab.TabWidth = 4;
16453 Tab.IndentWidth = 8;
16454 verifyFormat("class TabWidth4Indent8 {\n"
16455 "\t\tvoid f() {\n"
16456 "\t\t\t\tsomeFunction(parameter1,\n"
16457 "\t\t\t\t parameter2);\n"
16458 "\t\t}\n"
16459 "};",
16460 Tab);
16461 Tab.TabWidth = 4;
16462 Tab.IndentWidth = 4;
16463 verifyFormat("class TabWidth4Indent4 {\n"
16464 "\tvoid f() {\n"
16465 "\t\tsomeFunction(parameter1,\n"
16466 "\t\t parameter2);\n"
16467 "\t}\n"
16468 "};",
16469 Tab);
16470 Tab.TabWidth = 8;
16471 Tab.IndentWidth = 4;
16472 verifyFormat("class TabWidth8Indent4 {\n"
16473 " void f() {\n"
16474 "\tsomeFunction(parameter1,\n"
16475 "\t parameter2);\n"
16476 " }\n"
16477 "};",
16478 Tab);
16479 Tab.TabWidth = 8;
16480 Tab.IndentWidth = 8;
16481 verifyFormat("/*\n"
16482 " a\t\tcomment\n"
16483 " in multiple lines\n"
16484 " */",
16485 " /*\t \t \n"
16486 " \t \t a\t\tcomment\t \t\n"
16487 " \t \t in multiple lines\t\n"
16488 " \t */",
16489 Tab);
16490 verifyFormat("{\n"
16491 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16492 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16493 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16494 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16495 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16496 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16497 "};",
16498 Tab);
16499 verifyFormat("enum AA {\n"
16500 "\ta1, // Force multiple lines\n"
16501 "\ta2,\n"
16502 "\ta3\n"
16503 "};",
16504 Tab);
16505 verifyFormat("if (aaaaaaaa && // q\n"
16506 " bb) // w\n"
16507 "\t;",
16508 "if (aaaaaaaa &&// q\n"
16509 "bb)// w\n"
16510 ";",
16511 Tab);
16512 verifyFormat("class X {\n"
16513 "\tvoid f() {\n"
16514 "\t\tsomeFunction(parameter1,\n"
16515 "\t\t parameter2);\n"
16516 "\t}\n"
16517 "};",
16518 Tab);
16519 verifyFormat("{\n"
16520 "\tQ(\n"
16521 "\t {\n"
16522 "\t\t int a;\n"
16523 "\t\t someFunction(aaaaaaaa,\n"
16524 "\t\t bbbbbbb);\n"
16525 "\t },\n"
16526 "\t p);\n"
16527 "}",
16528 Tab);
16529 verifyFormat("{\n"
16530 "\t/* aaaa\n"
16531 "\t bbbb */\n"
16532 "}",
16533 "{\n"
16534 "/* aaaa\n"
16535 " bbbb */\n"
16536 "}",
16537 Tab);
16538 verifyFormat("{\n"
16539 "\t/*\n"
16540 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16541 "\t bbbbbbbbbbbbb\n"
16542 "\t*/\n"
16543 "}",
16544 "{\n"
16545 "/*\n"
16546 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16547 "*/\n"
16548 "}",
16549 Tab);
16550 verifyFormat("{\n"
16551 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16552 "\t// bbbbbbbbbbbbb\n"
16553 "}",
16554 "{\n"
16555 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16556 "}",
16557 Tab);
16558 verifyFormat("{\n"
16559 "\t/*\n"
16560 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16561 "\t bbbbbbbbbbbbb\n"
16562 "\t*/\n"
16563 "}",
16564 "{\n"
16565 "\t/*\n"
16566 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16567 "\t*/\n"
16568 "}",
16569 Tab);
16570 verifyNoChange("{\n"
16571 "\t/*\n"
16572 "\n"
16573 "\t*/\n"
16574 "}",
16575 Tab);
16576 verifyNoChange("{\n"
16577 "\t/*\n"
16578 " asdf\n"
16579 "\t*/\n"
16580 "}",
16581 Tab);
16582 verifyFormat("/* some\n"
16583 " comment */",
16584 " \t \t /* some\n"
16585 " \t \t comment */",
16586 Tab);
16587 verifyFormat("int a; /* some\n"
16588 " comment */",
16589 " \t \t int a; /* some\n"
16590 " \t \t comment */",
16591 Tab);
16592 verifyFormat("int a; /* some\n"
16593 "comment */",
16594 " \t \t int\ta; /* some\n"
16595 " \t \t comment */",
16596 Tab);
16597 verifyFormat("f(\"\t\t\"); /* some\n"
16598 " comment */",
16599 " \t \t f(\"\t\t\"); /* some\n"
16600 " \t \t comment */",
16601 Tab);
16602 verifyFormat("{\n"
16603 "\t/*\n"
16604 "\t * Comment\n"
16605 "\t */\n"
16606 "\tint i;\n"
16607 "}",
16608 "{\n"
16609 "\t/*\n"
16610 "\t * Comment\n"
16611 "\t */\n"
16612 "\t int i;\n"
16613 "}",
16614 Tab);
16615 Tab.TabWidth = 2;
16616 Tab.IndentWidth = 2;
16617 verifyFormat("{\n"
16618 "\t/* aaaa\n"
16619 "\t bbbb */\n"
16620 "}",
16621 "{\n"
16622 "/* aaaa\n"
16623 " bbbb */\n"
16624 "}",
16625 Tab);
16626 verifyFormat("{\n"
16627 "\t/*\n"
16628 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16629 "\t bbbbbbbbbbbbb\n"
16630 "\t*/\n"
16631 "}",
16632 "{\n"
16633 "/*\n"
16634 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16635 "*/\n"
16636 "}",
16637 Tab);
16638 Tab.AlignConsecutiveAssignments.Enabled = true;
16639 Tab.AlignConsecutiveDeclarations.Enabled = true;
16640 Tab.TabWidth = 4;
16641 Tab.IndentWidth = 4;
16642 verifyFormat("class Assign {\n"
16643 "\tvoid f() {\n"
16644 "\t\tint x = 123;\n"
16645 "\t\tint random = 4;\n"
16646 "\t\tstd::string alphabet =\n"
16647 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16648 "\t}\n"
16649 "};",
16650 Tab);
16651 Tab.AlignOperands = FormatStyle::OAS_Align;
16652 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16653 " cccccccccccccccccccc;",
16654 Tab);
16655 // no alignment
16656 verifyFormat("int aaaaaaaaaa =\n"
16657 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16658 Tab);
16659 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16660 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16661 " : 333333333333333;",
16662 Tab);
16663 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16664 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16665 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16666 " + cccccccccccccccccccc;",
16667 Tab);
16670 TEST_F(FormatTest, ZeroTabWidth) {
16671 FormatStyle Tab = getLLVMStyleWithColumns(42);
16672 Tab.IndentWidth = 8;
16673 Tab.UseTab = FormatStyle::UT_Never;
16674 Tab.TabWidth = 0;
16675 verifyFormat("void a() {\n"
16676 " // line starts with '\t'\n"
16677 "};",
16678 "void a(){\n"
16679 "\t// line starts with '\t'\n"
16680 "};",
16681 Tab);
16683 verifyFormat("void a() {\n"
16684 " // line starts with '\t'\n"
16685 "};",
16686 "void a(){\n"
16687 "\t\t// line starts with '\t'\n"
16688 "};",
16689 Tab);
16691 Tab.UseTab = FormatStyle::UT_ForIndentation;
16692 verifyFormat("void a() {\n"
16693 " // line starts with '\t'\n"
16694 "};",
16695 "void a(){\n"
16696 "\t// line starts with '\t'\n"
16697 "};",
16698 Tab);
16700 verifyFormat("void a() {\n"
16701 " // line starts with '\t'\n"
16702 "};",
16703 "void a(){\n"
16704 "\t\t// line starts with '\t'\n"
16705 "};",
16706 Tab);
16708 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16709 verifyFormat("void a() {\n"
16710 " // line starts with '\t'\n"
16711 "};",
16712 "void a(){\n"
16713 "\t// line starts with '\t'\n"
16714 "};",
16715 Tab);
16717 verifyFormat("void a() {\n"
16718 " // line starts with '\t'\n"
16719 "};",
16720 "void a(){\n"
16721 "\t\t// line starts with '\t'\n"
16722 "};",
16723 Tab);
16725 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16726 verifyFormat("void a() {\n"
16727 " // line starts with '\t'\n"
16728 "};",
16729 "void a(){\n"
16730 "\t// line starts with '\t'\n"
16731 "};",
16732 Tab);
16734 verifyFormat("void a() {\n"
16735 " // line starts with '\t'\n"
16736 "};",
16737 "void a(){\n"
16738 "\t\t// line starts with '\t'\n"
16739 "};",
16740 Tab);
16742 Tab.UseTab = FormatStyle::UT_Always;
16743 verifyFormat("void a() {\n"
16744 "// line starts with '\t'\n"
16745 "};",
16746 "void a(){\n"
16747 "\t// line starts with '\t'\n"
16748 "};",
16749 Tab);
16751 verifyFormat("void a() {\n"
16752 "// line starts with '\t'\n"
16753 "};",
16754 "void a(){\n"
16755 "\t\t// line starts with '\t'\n"
16756 "};",
16757 Tab);
16760 TEST_F(FormatTest, CalculatesOriginalColumn) {
16761 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16762 "q\"; /* some\n"
16763 " comment */",
16764 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16765 "q\"; /* some\n"
16766 " comment */");
16767 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16768 "/* some\n"
16769 " comment */",
16770 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16771 " /* some\n"
16772 " comment */");
16773 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16774 "qqq\n"
16775 "/* some\n"
16776 " comment */",
16777 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16778 "qqq\n"
16779 " /* some\n"
16780 " comment */");
16781 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16782 "wwww; /* some\n"
16783 " comment */",
16784 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16785 "wwww; /* some\n"
16786 " comment */");
16789 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
16790 FormatStyle NoSpace = getLLVMStyle();
16791 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
16793 verifyFormat("while(true)\n"
16794 " continue;",
16795 NoSpace);
16796 verifyFormat("for(;;)\n"
16797 " continue;",
16798 NoSpace);
16799 verifyFormat("if(true)\n"
16800 " f();\n"
16801 "else if(true)\n"
16802 " f();",
16803 NoSpace);
16804 verifyFormat("do {\n"
16805 " do_something();\n"
16806 "} while(something());",
16807 NoSpace);
16808 verifyFormat("switch(x) {\n"
16809 "default:\n"
16810 " break;\n"
16811 "}",
16812 NoSpace);
16813 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
16814 verifyFormat("size_t x = sizeof(x);", NoSpace);
16815 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
16816 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
16817 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
16818 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
16819 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
16820 verifyFormat("alignas(128) char a[128];", NoSpace);
16821 verifyFormat("size_t x = alignof(MyType);", NoSpace);
16822 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
16823 verifyFormat("int f() throw(Deprecated);", NoSpace);
16824 verifyFormat("typedef void (*cb)(int);", NoSpace);
16825 verifyFormat("T A::operator()();", NoSpace);
16826 verifyFormat("X A::operator++(T);", NoSpace);
16827 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
16828 verifyFormat("#if (foo || bar) && baz\n"
16829 "#elif ((a || b) && c) || d\n"
16830 "#endif",
16831 NoSpace);
16833 FormatStyle Space = getLLVMStyle();
16834 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
16836 verifyFormat("int f ();", Space);
16837 verifyFormat("bool operator< ();", Space);
16838 verifyFormat("bool operator> ();", Space);
16839 verifyFormat("void f (int a, T b) {\n"
16840 " while (true)\n"
16841 " continue;\n"
16842 "}",
16843 Space);
16844 verifyFormat("if (true)\n"
16845 " f ();\n"
16846 "else if (true)\n"
16847 " f ();",
16848 Space);
16849 verifyFormat("do {\n"
16850 " do_something ();\n"
16851 "} while (something ());",
16852 Space);
16853 verifyFormat("switch (x) {\n"
16854 "default:\n"
16855 " break;\n"
16856 "}",
16857 Space);
16858 verifyFormat("A::A () : a (1) {}", Space);
16859 verifyFormat("void f () __attribute__ ((asdf));", Space);
16860 verifyFormat("*(&a + 1);\n"
16861 "&((&a)[1]);\n"
16862 "a[(b + c) * d];\n"
16863 "(((a + 1) * 2) + 3) * 4;",
16864 Space);
16865 verifyFormat("#define A(x) x", Space);
16866 verifyFormat("#define A (x) x", Space);
16867 verifyFormat("#if defined(x)\n"
16868 "#endif",
16869 Space);
16870 verifyFormat("auto i = std::make_unique<int> (5);", Space);
16871 verifyFormat("size_t x = sizeof (x);", Space);
16872 verifyFormat("auto f (int x) -> decltype (x);", Space);
16873 verifyFormat("auto f (int x) -> typeof (x);", Space);
16874 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
16875 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
16876 verifyFormat("int f (T x) noexcept (x.create ());", Space);
16877 verifyFormat("alignas (128) char a[128];", Space);
16878 verifyFormat("size_t x = alignof (MyType);", Space);
16879 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
16880 verifyFormat("int f () throw (Deprecated);", Space);
16881 verifyFormat("typedef void (*cb) (int);", Space);
16882 verifyFormat("T A::operator() ();", Space);
16883 verifyFormat("X A::operator++ (T);", Space);
16884 verifyFormat("auto lambda = [] () { return 0; };", Space);
16885 verifyFormat("int x = int (y);", Space);
16886 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
16887 verifyFormat("__builtin_LINE ()", Space);
16888 verifyFormat("__builtin_UNKNOWN ()", Space);
16890 FormatStyle SomeSpace = getLLVMStyle();
16891 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
16893 verifyFormat("[]() -> float {}", SomeSpace);
16894 verifyFormat("[] (auto foo) {}", SomeSpace);
16895 verifyFormat("[foo]() -> int {}", SomeSpace);
16896 verifyFormat("int f();", SomeSpace);
16897 verifyFormat("void f (int a, T b) {\n"
16898 " while (true)\n"
16899 " continue;\n"
16900 "}",
16901 SomeSpace);
16902 verifyFormat("if (true)\n"
16903 " f();\n"
16904 "else if (true)\n"
16905 " f();",
16906 SomeSpace);
16907 verifyFormat("do {\n"
16908 " do_something();\n"
16909 "} while (something());",
16910 SomeSpace);
16911 verifyFormat("switch (x) {\n"
16912 "default:\n"
16913 " break;\n"
16914 "}",
16915 SomeSpace);
16916 verifyFormat("A::A() : a (1) {}", SomeSpace);
16917 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
16918 verifyFormat("*(&a + 1);\n"
16919 "&((&a)[1]);\n"
16920 "a[(b + c) * d];\n"
16921 "(((a + 1) * 2) + 3) * 4;",
16922 SomeSpace);
16923 verifyFormat("#define A(x) x", SomeSpace);
16924 verifyFormat("#define A (x) x", SomeSpace);
16925 verifyFormat("#if defined(x)\n"
16926 "#endif",
16927 SomeSpace);
16928 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
16929 verifyFormat("size_t x = sizeof (x);", SomeSpace);
16930 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
16931 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
16932 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
16933 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
16934 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
16935 verifyFormat("alignas (128) char a[128];", SomeSpace);
16936 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
16937 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16938 SomeSpace);
16939 verifyFormat("int f() throw (Deprecated);", SomeSpace);
16940 verifyFormat("typedef void (*cb) (int);", SomeSpace);
16941 verifyFormat("T A::operator()();", SomeSpace);
16942 verifyFormat("X A::operator++ (T);", SomeSpace);
16943 verifyFormat("int x = int (y);", SomeSpace);
16944 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
16946 FormatStyle SpaceControlStatements = getLLVMStyle();
16947 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16948 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
16950 verifyFormat("while (true)\n"
16951 " continue;",
16952 SpaceControlStatements);
16953 verifyFormat("if (true)\n"
16954 " f();\n"
16955 "else if (true)\n"
16956 " f();",
16957 SpaceControlStatements);
16958 verifyFormat("for (;;) {\n"
16959 " do_something();\n"
16960 "}",
16961 SpaceControlStatements);
16962 verifyFormat("do {\n"
16963 " do_something();\n"
16964 "} while (something());",
16965 SpaceControlStatements);
16966 verifyFormat("switch (x) {\n"
16967 "default:\n"
16968 " break;\n"
16969 "}",
16970 SpaceControlStatements);
16972 FormatStyle SpaceFuncDecl = getLLVMStyle();
16973 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16974 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
16976 verifyFormat("int f ();", SpaceFuncDecl);
16977 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
16978 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
16979 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
16980 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
16981 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
16982 verifyFormat("#define A(x) x", SpaceFuncDecl);
16983 verifyFormat("#define A (x) x", SpaceFuncDecl);
16984 verifyFormat("#if defined(x)\n"
16985 "#endif",
16986 SpaceFuncDecl);
16987 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
16988 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
16989 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
16990 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
16991 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
16992 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
16993 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
16994 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
16995 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
16996 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16997 SpaceFuncDecl);
16998 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
16999 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
17000 verifyFormat("T A::operator()();", SpaceFuncDecl);
17001 verifyFormat("X A::operator++(T);", SpaceFuncDecl);
17002 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
17003 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
17004 verifyFormat("int x = int(y);", SpaceFuncDecl);
17005 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17006 SpaceFuncDecl);
17008 FormatStyle SpaceFuncDef = getLLVMStyle();
17009 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17010 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
17012 verifyFormat("int f();", SpaceFuncDef);
17013 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
17014 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
17015 verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
17016 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
17017 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
17018 verifyFormat("#define A(x) x", SpaceFuncDef);
17019 verifyFormat("#define A (x) x", SpaceFuncDef);
17020 verifyFormat("#if defined(x)\n"
17021 "#endif",
17022 SpaceFuncDef);
17023 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
17024 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
17025 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
17026 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
17027 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
17028 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
17029 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
17030 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
17031 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
17032 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17033 SpaceFuncDef);
17034 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
17035 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
17036 verifyFormat("T A::operator()();", SpaceFuncDef);
17037 verifyFormat("X A::operator++(T);", SpaceFuncDef);
17038 verifyFormat("T A::operator()() {}", SpaceFuncDef);
17039 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
17040 verifyFormat("int x = int(y);", SpaceFuncDef);
17041 verifyFormat("void foo::bar () {}", SpaceFuncDef);
17042 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17043 SpaceFuncDef);
17045 FormatStyle SpaceIfMacros = getLLVMStyle();
17046 SpaceIfMacros.IfMacros.clear();
17047 SpaceIfMacros.IfMacros.push_back("MYIF");
17048 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17049 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
17050 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
17051 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
17052 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
17054 FormatStyle SpaceForeachMacros = getLLVMStyle();
17055 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
17056 FormatStyle::SBS_Never);
17057 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
17058 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17059 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
17060 verifyFormat("for (;;) {\n"
17061 "}",
17062 SpaceForeachMacros);
17063 verifyFormat("foreach (Item *item, itemlist) {\n"
17064 "}",
17065 SpaceForeachMacros);
17066 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17067 "}",
17068 SpaceForeachMacros);
17069 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17070 "}",
17071 SpaceForeachMacros);
17072 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
17074 FormatStyle SomeSpace2 = getLLVMStyle();
17075 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17076 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
17077 verifyFormat("[]() -> float {}", SomeSpace2);
17078 verifyFormat("[] (auto foo) {}", SomeSpace2);
17079 verifyFormat("[foo]() -> int {}", SomeSpace2);
17080 verifyFormat("int f();", SomeSpace2);
17081 verifyFormat("void f (int a, T b) {\n"
17082 " while (true)\n"
17083 " continue;\n"
17084 "}",
17085 SomeSpace2);
17086 verifyFormat("if (true)\n"
17087 " f();\n"
17088 "else if (true)\n"
17089 " f();",
17090 SomeSpace2);
17091 verifyFormat("do {\n"
17092 " do_something();\n"
17093 "} while (something());",
17094 SomeSpace2);
17095 verifyFormat("switch (x) {\n"
17096 "default:\n"
17097 " break;\n"
17098 "}",
17099 SomeSpace2);
17100 verifyFormat("A::A() : a (1) {}", SomeSpace2);
17101 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
17102 verifyFormat("*(&a + 1);\n"
17103 "&((&a)[1]);\n"
17104 "a[(b + c) * d];\n"
17105 "(((a + 1) * 2) + 3) * 4;",
17106 SomeSpace2);
17107 verifyFormat("#define A(x) x", SomeSpace2);
17108 verifyFormat("#define A (x) x", SomeSpace2);
17109 verifyFormat("#if defined(x)\n"
17110 "#endif",
17111 SomeSpace2);
17112 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
17113 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
17114 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
17115 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
17116 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
17117 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
17118 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
17119 verifyFormat("alignas (128) char a[128];", SomeSpace2);
17120 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
17121 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17122 SomeSpace2);
17123 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
17124 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
17125 verifyFormat("T A::operator()();", SomeSpace2);
17126 verifyFormat("X A::operator++ (T);", SomeSpace2);
17127 verifyFormat("int x = int (y);", SomeSpace2);
17128 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
17130 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
17131 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17132 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17133 .AfterOverloadedOperator = true;
17135 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
17136 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
17137 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
17138 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17140 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17141 .AfterOverloadedOperator = false;
17143 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
17144 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
17145 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
17146 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17148 auto SpaceAfterRequires = getLLVMStyle();
17149 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17150 EXPECT_FALSE(
17151 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
17152 EXPECT_FALSE(
17153 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
17154 verifyFormat("void f(auto x)\n"
17155 " requires requires(int i) { x + i; }\n"
17156 "{}",
17157 SpaceAfterRequires);
17158 verifyFormat("void f(auto x)\n"
17159 " requires(requires(int i) { x + i; })\n"
17160 "{}",
17161 SpaceAfterRequires);
17162 verifyFormat("if (requires(int i) { x + i; })\n"
17163 " return;",
17164 SpaceAfterRequires);
17165 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17166 verifyFormat("template <typename T>\n"
17167 " requires(Foo<T>)\n"
17168 "class Bar;",
17169 SpaceAfterRequires);
17171 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17172 verifyFormat("void f(auto x)\n"
17173 " requires requires(int i) { x + i; }\n"
17174 "{}",
17175 SpaceAfterRequires);
17176 verifyFormat("void f(auto x)\n"
17177 " requires (requires(int i) { x + i; })\n"
17178 "{}",
17179 SpaceAfterRequires);
17180 verifyFormat("if (requires(int i) { x + i; })\n"
17181 " return;",
17182 SpaceAfterRequires);
17183 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17184 verifyFormat("template <typename T>\n"
17185 " requires (Foo<T>)\n"
17186 "class Bar;",
17187 SpaceAfterRequires);
17189 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
17190 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
17191 verifyFormat("void f(auto x)\n"
17192 " requires requires (int i) { x + i; }\n"
17193 "{}",
17194 SpaceAfterRequires);
17195 verifyFormat("void f(auto x)\n"
17196 " requires(requires (int i) { x + i; })\n"
17197 "{}",
17198 SpaceAfterRequires);
17199 verifyFormat("if (requires (int i) { x + i; })\n"
17200 " return;",
17201 SpaceAfterRequires);
17202 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17203 verifyFormat("template <typename T>\n"
17204 " requires(Foo<T>)\n"
17205 "class Bar;",
17206 SpaceAfterRequires);
17208 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17209 verifyFormat("void f(auto x)\n"
17210 " requires requires (int i) { x + i; }\n"
17211 "{}",
17212 SpaceAfterRequires);
17213 verifyFormat("void f(auto x)\n"
17214 " requires (requires (int i) { x + i; })\n"
17215 "{}",
17216 SpaceAfterRequires);
17217 verifyFormat("if (requires (int i) { x + i; })\n"
17218 " return;",
17219 SpaceAfterRequires);
17220 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17221 verifyFormat("template <typename T>\n"
17222 " requires (Foo<T>)\n"
17223 "class Bar;",
17224 SpaceAfterRequires);
17227 TEST_F(FormatTest, SpaceAfterLogicalNot) {
17228 FormatStyle Spaces = getLLVMStyle();
17229 Spaces.SpaceAfterLogicalNot = true;
17231 verifyFormat("bool x = ! y", Spaces);
17232 verifyFormat("if (! isFailure())", Spaces);
17233 verifyFormat("if (! (a && b))", Spaces);
17234 verifyFormat("\"Error!\"", Spaces);
17235 verifyFormat("! ! x", Spaces);
17238 TEST_F(FormatTest, ConfigurableSpacesInParens) {
17239 FormatStyle Spaces = getLLVMStyle();
17241 verifyFormat("do_something(::globalVar);", Spaces);
17242 verifyFormat("call(x, y, z);", Spaces);
17243 verifyFormat("call();", Spaces);
17244 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17245 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17246 Spaces);
17247 verifyFormat("while ((bool)1)\n"
17248 " continue;",
17249 Spaces);
17250 verifyFormat("for (;;)\n"
17251 " continue;",
17252 Spaces);
17253 verifyFormat("if (true)\n"
17254 " f();\n"
17255 "else if (true)\n"
17256 " f();",
17257 Spaces);
17258 verifyFormat("do {\n"
17259 " do_something((int)i);\n"
17260 "} while (something());",
17261 Spaces);
17262 verifyFormat("switch (x) {\n"
17263 "default:\n"
17264 " break;\n"
17265 "}",
17266 Spaces);
17267 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17268 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17269 verifyFormat("void f() __attribute__((asdf));", Spaces);
17270 verifyFormat("x = (int32)y;", Spaces);
17271 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces);
17272 verifyFormat("decltype(x) y = 42;", Spaces);
17273 verifyFormat("decltype((x)) y = z;", Spaces);
17274 verifyFormat("decltype((foo())) a = foo();", Spaces);
17275 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17276 verifyFormat("if ((x - y) && (a ^ b))\n"
17277 " f();",
17278 Spaces);
17279 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17280 " foo(i);",
17281 Spaces);
17282 verifyFormat("switch (x / (y + z)) {\n"
17283 "default:\n"
17284 " break;\n"
17285 "}",
17286 Spaces);
17288 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17289 Spaces.SpacesInParensOptions = {};
17290 Spaces.SpacesInParensOptions.Other = true;
17292 EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
17293 verifyFormat("if (a)\n"
17294 " return;",
17295 Spaces);
17297 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17298 verifyFormat("do_something( ::globalVar );", Spaces);
17299 verifyFormat("call( x, y, z );", Spaces);
17300 verifyFormat("call();", Spaces);
17301 verifyFormat("std::function<void( int, int )> callback;", Spaces);
17302 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17303 Spaces);
17304 verifyFormat("while ( (bool)1 )\n"
17305 " continue;",
17306 Spaces);
17307 verifyFormat("for ( ;; )\n"
17308 " continue;",
17309 Spaces);
17310 verifyFormat("if ( true )\n"
17311 " f();\n"
17312 "else if ( true )\n"
17313 " f();",
17314 Spaces);
17315 verifyFormat("do {\n"
17316 " do_something( (int)i );\n"
17317 "} while ( something() );",
17318 Spaces);
17319 verifyFormat("switch ( x ) {\n"
17320 "default:\n"
17321 " break;\n"
17322 "}",
17323 Spaces);
17324 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17325 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17326 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17327 verifyFormat("x = (int32)y;", Spaces);
17328 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces);
17329 verifyFormat("decltype( x ) y = 42;", Spaces);
17330 verifyFormat("decltype( ( x ) ) y = z;", Spaces);
17331 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces);
17332 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces);
17333 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17334 " f();",
17335 Spaces);
17336 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17337 " foo( i );",
17338 Spaces);
17339 verifyFormat("switch ( x / ( y + z ) ) {\n"
17340 "default:\n"
17341 " break;\n"
17342 "}",
17343 Spaces);
17345 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17346 Spaces.SpacesInParensOptions = {};
17347 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17348 verifyFormat("Type *A = ( Type * )P;", Spaces);
17349 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17350 verifyFormat("x = ( int32 )y;", Spaces);
17351 verifyFormat("throw ( int32 )x;", Spaces);
17352 verifyFormat("int a = ( int )(2.0f);", Spaces);
17353 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17354 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17355 verifyFormat("#define x (( int )-1)", Spaces);
17356 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17358 // Run the first set of tests again with:
17359 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17360 Spaces.SpacesInParensOptions = {};
17361 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17362 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17363 verifyFormat("call(x, y, z);", Spaces);
17364 verifyFormat("call( );", Spaces);
17365 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17366 verifyFormat("while (( bool )1)\n"
17367 " continue;",
17368 Spaces);
17369 verifyFormat("for (;;)\n"
17370 " continue;",
17371 Spaces);
17372 verifyFormat("if (true)\n"
17373 " f( );\n"
17374 "else if (true)\n"
17375 " f( );",
17376 Spaces);
17377 verifyFormat("do {\n"
17378 " do_something(( int )i);\n"
17379 "} while (something( ));",
17380 Spaces);
17381 verifyFormat("switch (x) {\n"
17382 "default:\n"
17383 " break;\n"
17384 "}",
17385 Spaces);
17386 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17387 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17388 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17389 verifyFormat("x = ( int32 )y;", Spaces);
17390 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17391 verifyFormat("decltype(x) y = 42;", Spaces);
17392 verifyFormat("decltype((x)) y = z;", Spaces);
17393 verifyFormat("decltype((foo( ))) a = foo( );", Spaces);
17394 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17395 verifyFormat("if ((x - y) && (a ^ b))\n"
17396 " f( );",
17397 Spaces);
17398 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17399 " foo(i);",
17400 Spaces);
17401 verifyFormat("switch (x / (y + z)) {\n"
17402 "default:\n"
17403 " break;\n"
17404 "}",
17405 Spaces);
17407 // Run the first set of tests again with:
17408 Spaces.SpaceAfterCStyleCast = true;
17409 verifyFormat("call(x, y, z);", Spaces);
17410 verifyFormat("call( );", Spaces);
17411 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17412 verifyFormat("while (( bool ) 1)\n"
17413 " continue;",
17414 Spaces);
17415 verifyFormat("for (;;)\n"
17416 " continue;",
17417 Spaces);
17418 verifyFormat("if (true)\n"
17419 " f( );\n"
17420 "else if (true)\n"
17421 " f( );",
17422 Spaces);
17423 verifyFormat("do {\n"
17424 " do_something(( int ) i);\n"
17425 "} while (something( ));",
17426 Spaces);
17427 verifyFormat("switch (x) {\n"
17428 "default:\n"
17429 " break;\n"
17430 "}",
17431 Spaces);
17432 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
17433 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
17434 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
17435 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
17436 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
17437 verifyFormat("throw ( int32 ) x;", Spaces);
17438 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17439 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17440 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17442 // Run subset of tests again with:
17443 Spaces.SpacesInParensOptions.InCStyleCasts = false;
17444 Spaces.SpaceAfterCStyleCast = true;
17445 verifyFormat("while ((bool) 1)\n"
17446 " continue;",
17447 Spaces);
17448 verifyFormat("do {\n"
17449 " do_something((int) i);\n"
17450 "} while (something( ));",
17451 Spaces);
17453 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
17454 verifyFormat("size_t idx = (size_t) a;", Spaces);
17455 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
17456 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17457 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17458 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17459 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17460 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
17461 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
17462 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
17463 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
17464 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
17465 verifyFormat("throw (int32) x;", Spaces);
17466 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17467 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17468 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17470 Spaces.ColumnLimit = 80;
17471 Spaces.IndentWidth = 4;
17472 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17473 verifyFormat("void foo( ) {\n"
17474 " size_t foo = (*(function))(\n"
17475 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17476 "BarrrrrrrrrrrrLong,\n"
17477 " FoooooooooLooooong);\n"
17478 "}",
17479 Spaces);
17480 Spaces.SpaceAfterCStyleCast = false;
17481 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17482 verifyFormat("size_t idx = (size_t)a;", Spaces);
17483 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17484 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17485 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17486 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17487 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17489 verifyFormat("void foo( ) {\n"
17490 " size_t foo = (*(function))(\n"
17491 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17492 "BarrrrrrrrrrrrLong,\n"
17493 " FoooooooooLooooong);\n"
17494 "}",
17495 Spaces);
17497 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
17498 verifyFormat("void foo( ) {\n"
17499 " size_t foo = (*(function))(\n"
17500 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17501 "BarrrrrrrrrrrrLong,\n"
17502 " FoooooooooLooooong\n"
17503 " );\n"
17504 "}",
17505 Spaces);
17506 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17507 verifyFormat("size_t idx = (size_t)a;", Spaces);
17508 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17509 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17510 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17511 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17512 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17514 // Check ExceptDoubleParentheses spaces
17515 Spaces.IndentWidth = 2;
17516 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17517 Spaces.SpacesInParensOptions = {};
17518 Spaces.SpacesInParensOptions.Other = true;
17519 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17520 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
17521 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces);
17522 verifyFormat("void f() __attribute__(( asdf ));", Spaces);
17523 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces);
17524 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces);
17525 verifyFormat("class __declspec( dllimport ) X {};", Spaces);
17526 verifyFormat("class __declspec(( dllimport )) X {};", Spaces);
17527 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces);
17528 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces);
17529 verifyFormat("decltype( x ) y = 42;", Spaces);
17530 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces);
17531 verifyFormat("if (( i = j ))\n"
17532 " do_something( i );",
17533 Spaces);
17535 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17536 Spaces.SpacesInParensOptions = {};
17537 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17538 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
17539 verifyFormat("while ( (bool)1 )\n"
17540 " continue;",
17541 Spaces);
17542 verifyFormat("while ((i = j))\n"
17543 " continue;",
17544 Spaces);
17545 verifyFormat("do {\n"
17546 " do_something((int)i);\n"
17547 "} while ( something() );",
17548 Spaces);
17549 verifyFormat("do {\n"
17550 " do_something((int)i);\n"
17551 "} while ((i = i + 1));",
17552 Spaces);
17553 verifyFormat("if ( (x - y) && (a ^ b) )\n"
17554 " f();",
17555 Spaces);
17556 verifyFormat("if ((i = j))\n"
17557 " do_something(i);",
17558 Spaces);
17559 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
17560 " foo(i);",
17561 Spaces);
17562 verifyFormat("switch ( x / (y + z) ) {\n"
17563 "default:\n"
17564 " break;\n"
17565 "}",
17566 Spaces);
17567 verifyFormat("if constexpr ((a = b))\n"
17568 " c;",
17569 Spaces);
17572 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
17573 verifyFormat("int a[5];");
17574 verifyFormat("a[3] += 42;");
17576 FormatStyle Spaces = getLLVMStyle();
17577 Spaces.SpacesInSquareBrackets = true;
17578 // Not lambdas.
17579 verifyFormat("int a[ 5 ];", Spaces);
17580 verifyFormat("a[ 3 ] += 42;", Spaces);
17581 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
17582 verifyFormat("double &operator[](int i) { return 0; }\n"
17583 "int i;",
17584 Spaces);
17585 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
17586 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
17587 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
17588 // Lambdas.
17589 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
17590 verifyFormat("return [ i, args... ] {};", Spaces);
17591 verifyFormat("int foo = [ &bar ]() {};", Spaces);
17592 verifyFormat("int foo = [ = ]() {};", Spaces);
17593 verifyFormat("int foo = [ & ]() {};", Spaces);
17594 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
17595 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
17598 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
17599 FormatStyle NoSpaceStyle = getLLVMStyle();
17600 verifyFormat("int a[5];", NoSpaceStyle);
17601 verifyFormat("a[3] += 42;", NoSpaceStyle);
17603 verifyFormat("int a[1];", NoSpaceStyle);
17604 verifyFormat("int 1 [a];", NoSpaceStyle);
17605 verifyFormat("int a[1][2];", NoSpaceStyle);
17606 verifyFormat("a[7] = 5;", NoSpaceStyle);
17607 verifyFormat("int a = (f())[23];", NoSpaceStyle);
17608 verifyFormat("f([] {})", NoSpaceStyle);
17610 FormatStyle Space = getLLVMStyle();
17611 Space.SpaceBeforeSquareBrackets = true;
17612 verifyFormat("int c = []() -> int { return 2; }();", Space);
17613 verifyFormat("return [i, args...] {};", Space);
17615 verifyFormat("int a [5];", Space);
17616 verifyFormat("a [3] += 42;", Space);
17617 verifyFormat("constexpr char hello []{\"hello\"};", Space);
17618 verifyFormat("double &operator[](int i) { return 0; }\n"
17619 "int i;",
17620 Space);
17621 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
17622 verifyFormat("int i = a [a][a]->f();", Space);
17623 verifyFormat("int i = (*b) [a]->f();", Space);
17625 verifyFormat("int a [1];", Space);
17626 verifyFormat("int 1 [a];", Space);
17627 verifyFormat("int a [1][2];", Space);
17628 verifyFormat("a [7] = 5;", Space);
17629 verifyFormat("int a = (f()) [23];", Space);
17630 verifyFormat("f([] {})", Space);
17633 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
17634 verifyFormat("int a = 5;");
17635 verifyFormat("a += 42;");
17636 verifyFormat("a or_eq 8;");
17637 verifyFormat("xor = foo;");
17639 FormatStyle Spaces = getLLVMStyle();
17640 Spaces.SpaceBeforeAssignmentOperators = false;
17641 verifyFormat("int a= 5;", Spaces);
17642 verifyFormat("a+= 42;", Spaces);
17643 verifyFormat("a or_eq 8;", Spaces);
17644 verifyFormat("xor= foo;", Spaces);
17647 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
17648 verifyFormat("class Foo : public Bar {};");
17649 verifyFormat("Foo::Foo() : foo(1) {}");
17650 verifyFormat("for (auto a : b) {\n}");
17651 verifyFormat("int x = a ? b : c;");
17652 verifyFormat("{\n"
17653 "label0:\n"
17654 " int x = 0;\n"
17655 "}");
17656 verifyFormat("switch (x) {\n"
17657 "case 1:\n"
17658 "default:\n"
17659 "}");
17660 verifyFormat("switch (allBraces) {\n"
17661 "case 1: {\n"
17662 " break;\n"
17663 "}\n"
17664 "case 2: {\n"
17665 " [[fallthrough]];\n"
17666 "}\n"
17667 "default: {\n"
17668 " break;\n"
17669 "}\n"
17670 "}");
17672 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
17673 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
17674 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
17675 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
17676 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
17677 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
17678 verifyFormat("{\n"
17679 "label1:\n"
17680 " int x = 0;\n"
17681 "}",
17682 CtorInitializerStyle);
17683 verifyFormat("switch (x) {\n"
17684 "case 1:\n"
17685 "default:\n"
17686 "}",
17687 CtorInitializerStyle);
17688 verifyFormat("switch (allBraces) {\n"
17689 "case 1: {\n"
17690 " break;\n"
17691 "}\n"
17692 "case 2: {\n"
17693 " [[fallthrough]];\n"
17694 "}\n"
17695 "default: {\n"
17696 " break;\n"
17697 "}\n"
17698 "}",
17699 CtorInitializerStyle);
17700 CtorInitializerStyle.BreakConstructorInitializers =
17701 FormatStyle::BCIS_AfterColon;
17702 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17703 " aaaaaaaaaaaaaaaa(1),\n"
17704 " bbbbbbbbbbbbbbbb(2) {}",
17705 CtorInitializerStyle);
17706 CtorInitializerStyle.BreakConstructorInitializers =
17707 FormatStyle::BCIS_BeforeComma;
17708 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17709 " : aaaaaaaaaaaaaaaa(1)\n"
17710 " , bbbbbbbbbbbbbbbb(2) {}",
17711 CtorInitializerStyle);
17712 CtorInitializerStyle.BreakConstructorInitializers =
17713 FormatStyle::BCIS_BeforeColon;
17714 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17715 " : aaaaaaaaaaaaaaaa(1),\n"
17716 " bbbbbbbbbbbbbbbb(2) {}",
17717 CtorInitializerStyle);
17718 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
17719 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17720 ": aaaaaaaaaaaaaaaa(1),\n"
17721 " bbbbbbbbbbbbbbbb(2) {}",
17722 CtorInitializerStyle);
17724 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
17725 InheritanceStyle.SpaceBeforeInheritanceColon = false;
17726 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
17727 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
17728 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
17729 verifyFormat("int x = a ? b : c;", InheritanceStyle);
17730 verifyFormat("{\n"
17731 "label2:\n"
17732 " int x = 0;\n"
17733 "}",
17734 InheritanceStyle);
17735 verifyFormat("switch (x) {\n"
17736 "case 1:\n"
17737 "default:\n"
17738 "}",
17739 InheritanceStyle);
17740 verifyFormat("switch (allBraces) {\n"
17741 "case 1: {\n"
17742 " break;\n"
17743 "}\n"
17744 "case 2: {\n"
17745 " [[fallthrough]];\n"
17746 "}\n"
17747 "default: {\n"
17748 " break;\n"
17749 "}\n"
17750 "}",
17751 InheritanceStyle);
17752 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
17753 verifyFormat("class Foooooooooooooooooooooo\n"
17754 " : public aaaaaaaaaaaaaaaaaa,\n"
17755 " public bbbbbbbbbbbbbbbbbb {\n"
17756 "}",
17757 InheritanceStyle);
17758 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
17759 verifyFormat("class Foooooooooooooooooooooo:\n"
17760 " public aaaaaaaaaaaaaaaaaa,\n"
17761 " public bbbbbbbbbbbbbbbbbb {\n"
17762 "}",
17763 InheritanceStyle);
17764 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
17765 verifyFormat("class Foooooooooooooooooooooo\n"
17766 " : public aaaaaaaaaaaaaaaaaa\n"
17767 " , public bbbbbbbbbbbbbbbbbb {\n"
17768 "}",
17769 InheritanceStyle);
17770 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17771 verifyFormat("class Foooooooooooooooooooooo\n"
17772 " : public aaaaaaaaaaaaaaaaaa,\n"
17773 " public bbbbbbbbbbbbbbbbbb {\n"
17774 "}",
17775 InheritanceStyle);
17776 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
17777 verifyFormat("class Foooooooooooooooooooooo\n"
17778 ": public aaaaaaaaaaaaaaaaaa,\n"
17779 " public bbbbbbbbbbbbbbbbbb {}",
17780 InheritanceStyle);
17782 FormatStyle ForLoopStyle = getLLVMStyle();
17783 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
17784 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
17785 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
17786 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
17787 verifyFormat("int x = a ? b : c;", ForLoopStyle);
17788 verifyFormat("{\n"
17789 "label2:\n"
17790 " int x = 0;\n"
17791 "}",
17792 ForLoopStyle);
17793 verifyFormat("switch (x) {\n"
17794 "case 1:\n"
17795 "default:\n"
17796 "}",
17797 ForLoopStyle);
17798 verifyFormat("switch (allBraces) {\n"
17799 "case 1: {\n"
17800 " break;\n"
17801 "}\n"
17802 "case 2: {\n"
17803 " [[fallthrough]];\n"
17804 "}\n"
17805 "default: {\n"
17806 " break;\n"
17807 "}\n"
17808 "}",
17809 ForLoopStyle);
17811 FormatStyle CaseStyle = getLLVMStyle();
17812 CaseStyle.SpaceBeforeCaseColon = true;
17813 verifyFormat("class Foo : public Bar {};", CaseStyle);
17814 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
17815 verifyFormat("for (auto a : b) {\n}", CaseStyle);
17816 verifyFormat("int x = a ? b : c;", CaseStyle);
17817 verifyFormat("switch (x) {\n"
17818 "case 1 :\n"
17819 "default :\n"
17820 "}",
17821 CaseStyle);
17822 verifyFormat("switch (allBraces) {\n"
17823 "case 1 : {\n"
17824 " break;\n"
17825 "}\n"
17826 "case 2 : {\n"
17827 " [[fallthrough]];\n"
17828 "}\n"
17829 "default : {\n"
17830 " break;\n"
17831 "}\n"
17832 "}",
17833 CaseStyle);
17834 // Goto labels should not be affected.
17835 verifyFormat("switch (x) {\n"
17836 "goto_label:\n"
17837 "default :\n"
17838 "}",
17839 CaseStyle);
17840 verifyFormat("switch (x) {\n"
17841 "goto_label: { break; }\n"
17842 "default : {\n"
17843 " break;\n"
17844 "}\n"
17845 "}",
17846 CaseStyle);
17848 FormatStyle NoSpaceStyle = getLLVMStyle();
17849 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
17850 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17851 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
17852 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17853 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
17854 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
17855 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
17856 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
17857 verifyFormat("{\n"
17858 "label3:\n"
17859 " int x = 0;\n"
17860 "}",
17861 NoSpaceStyle);
17862 verifyFormat("switch (x) {\n"
17863 "case 1:\n"
17864 "default:\n"
17865 "}",
17866 NoSpaceStyle);
17867 verifyFormat("switch (allBraces) {\n"
17868 "case 1: {\n"
17869 " break;\n"
17870 "}\n"
17871 "case 2: {\n"
17872 " [[fallthrough]];\n"
17873 "}\n"
17874 "default: {\n"
17875 " break;\n"
17876 "}\n"
17877 "}",
17878 NoSpaceStyle);
17880 FormatStyle InvertedSpaceStyle = getLLVMStyle();
17881 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
17882 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17883 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
17884 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17885 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
17886 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
17887 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
17888 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
17889 verifyFormat("{\n"
17890 "label3:\n"
17891 " int x = 0;\n"
17892 "}",
17893 InvertedSpaceStyle);
17894 verifyFormat("switch (x) {\n"
17895 "case 1 :\n"
17896 "case 2 : {\n"
17897 " break;\n"
17898 "}\n"
17899 "default :\n"
17900 " break;\n"
17901 "}",
17902 InvertedSpaceStyle);
17903 verifyFormat("switch (allBraces) {\n"
17904 "case 1 : {\n"
17905 " break;\n"
17906 "}\n"
17907 "case 2 : {\n"
17908 " [[fallthrough]];\n"
17909 "}\n"
17910 "default : {\n"
17911 " break;\n"
17912 "}\n"
17913 "}",
17914 InvertedSpaceStyle);
17917 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
17918 FormatStyle Style = getLLVMStyle();
17920 Style.PointerAlignment = FormatStyle::PAS_Left;
17921 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17922 verifyFormat("void* const* x = NULL;", Style);
17924 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17925 do { \
17926 Style.PointerAlignment = FormatStyle::Pointers; \
17927 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17928 verifyFormat(Code, Style); \
17929 } while (false)
17931 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
17932 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
17933 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
17935 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
17936 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
17937 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
17939 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
17940 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
17941 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
17943 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
17944 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
17945 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
17947 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
17948 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17949 SAPQ_Default);
17950 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17951 SAPQ_Default);
17953 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
17954 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17955 SAPQ_Before);
17956 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17957 SAPQ_Before);
17959 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
17960 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
17961 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17962 SAPQ_After);
17964 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
17965 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
17966 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
17968 #undef verifyQualifierSpaces
17970 FormatStyle Spaces = getLLVMStyle();
17971 Spaces.AttributeMacros.push_back("qualified");
17972 Spaces.PointerAlignment = FormatStyle::PAS_Right;
17973 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17974 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
17975 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17976 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
17977 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
17978 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17979 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17980 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
17981 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
17982 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17983 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17984 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17986 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17987 Spaces.PointerAlignment = FormatStyle::PAS_Left;
17988 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17989 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
17990 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
17991 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
17992 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
17993 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17994 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17995 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17996 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
17997 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
17998 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
17999 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
18000 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18002 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18003 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
18004 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18005 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
18006 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
18007 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18008 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18009 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18012 TEST_F(FormatTest, AlignConsecutiveMacros) {
18013 FormatStyle Style = getLLVMStyle();
18014 Style.AlignConsecutiveAssignments.Enabled = true;
18015 Style.AlignConsecutiveDeclarations.Enabled = true;
18017 verifyFormat("#define a 3\n"
18018 "#define bbbb 4\n"
18019 "#define ccc (5)",
18020 Style);
18022 verifyFormat("#define f(x) (x * x)\n"
18023 "#define fff(x, y, z) (x * y + z)\n"
18024 "#define ffff(x, y) (x - y)",
18025 Style);
18027 verifyFormat("#define foo(x, y) (x + y)\n"
18028 "#define bar (5, 6)(2 + 2)",
18029 Style);
18031 verifyFormat("#define a 3\n"
18032 "#define bbbb 4\n"
18033 "#define ccc (5)\n"
18034 "#define f(x) (x * x)\n"
18035 "#define fff(x, y, z) (x * y + z)\n"
18036 "#define ffff(x, y) (x - y)",
18037 Style);
18039 Style.AlignConsecutiveMacros.Enabled = true;
18040 verifyFormat("#define a 3\n"
18041 "#define bbbb 4\n"
18042 "#define ccc (5)",
18043 Style);
18045 verifyFormat("#define true 1\n"
18046 "#define false 0",
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 verifyFormat("#define a 5\n"
18067 "#define foo(x, y) (x + y)\n"
18068 "#define CCC (6)\n"
18069 "auto lambda = []() {\n"
18070 " auto ii = 0;\n"
18071 " float j = 0;\n"
18072 " return 0;\n"
18073 "};\n"
18074 "int i = 0;\n"
18075 "float i2 = 0;\n"
18076 "auto v = type{\n"
18077 " i = 1, //\n"
18078 " (i = 2), //\n"
18079 " i = 3 //\n"
18080 "};",
18081 Style);
18083 Style.AlignConsecutiveMacros.Enabled = false;
18084 Style.ColumnLimit = 20;
18086 verifyFormat("#define a \\\n"
18087 " \"aabbbbbbbbbbbb\"\n"
18088 "#define D \\\n"
18089 " \"aabbbbbbbbbbbb\" \\\n"
18090 " \"ccddeeeeeeeee\"\n"
18091 "#define B \\\n"
18092 " \"QQQQQQQQQQQQQ\" \\\n"
18093 " \"FFFFFFFFFFFFF\" \\\n"
18094 " \"LLLLLLLL\"",
18095 Style);
18097 Style.AlignConsecutiveMacros.Enabled = true;
18098 verifyFormat("#define a \\\n"
18099 " \"aabbbbbbbbbbbb\"\n"
18100 "#define D \\\n"
18101 " \"aabbbbbbbbbbbb\" \\\n"
18102 " \"ccddeeeeeeeee\"\n"
18103 "#define B \\\n"
18104 " \"QQQQQQQQQQQQQ\" \\\n"
18105 " \"FFFFFFFFFFFFF\" \\\n"
18106 " \"LLLLLLLL\"",
18107 Style);
18109 // Test across comments
18110 Style.MaxEmptyLinesToKeep = 10;
18111 Style.ReflowComments = FormatStyle::RCS_Never;
18112 Style.AlignConsecutiveMacros.AcrossComments = true;
18113 verifyFormat("#define a 3\n"
18114 "// line comment\n"
18115 "#define bbbb 4\n"
18116 "#define ccc (5)",
18117 "#define a 3\n"
18118 "// line comment\n"
18119 "#define bbbb 4\n"
18120 "#define ccc (5)",
18121 Style);
18123 verifyFormat("#define a 3\n"
18124 "/* block comment */\n"
18125 "#define bbbb 4\n"
18126 "#define ccc (5)",
18127 "#define a 3\n"
18128 "/* block comment */\n"
18129 "#define bbbb 4\n"
18130 "#define ccc (5)",
18131 Style);
18133 verifyFormat("#define a 3\n"
18134 "/* multi-line *\n"
18135 " * block comment */\n"
18136 "#define bbbb 4\n"
18137 "#define ccc (5)",
18138 "#define a 3\n"
18139 "/* multi-line *\n"
18140 " * block comment */\n"
18141 "#define bbbb 4\n"
18142 "#define ccc (5)",
18143 Style);
18145 verifyFormat("#define a 3\n"
18146 "// multi-line line comment\n"
18147 "//\n"
18148 "#define bbbb 4\n"
18149 "#define ccc (5)",
18150 "#define a 3\n"
18151 "// multi-line line comment\n"
18152 "//\n"
18153 "#define bbbb 4\n"
18154 "#define ccc (5)",
18155 Style);
18157 verifyFormat("#define a 3\n"
18158 "// empty lines still break.\n"
18159 "\n"
18160 "#define bbbb 4\n"
18161 "#define ccc (5)",
18162 "#define a 3\n"
18163 "// empty lines still break.\n"
18164 "\n"
18165 "#define bbbb 4\n"
18166 "#define ccc (5)",
18167 Style);
18169 // Test across empty lines
18170 Style.AlignConsecutiveMacros.AcrossComments = false;
18171 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
18172 verifyFormat("#define a 3\n"
18173 "\n"
18174 "#define bbbb 4\n"
18175 "#define ccc (5)",
18176 "#define a 3\n"
18177 "\n"
18178 "#define bbbb 4\n"
18179 "#define ccc (5)",
18180 Style);
18182 verifyFormat("#define a 3\n"
18183 "\n"
18184 "\n"
18185 "\n"
18186 "#define bbbb 4\n"
18187 "#define ccc (5)",
18188 "#define a 3\n"
18189 "\n"
18190 "\n"
18191 "\n"
18192 "#define bbbb 4\n"
18193 "#define ccc (5)",
18194 Style);
18196 verifyFormat("#define a 3\n"
18197 "// comments should break alignment\n"
18198 "//\n"
18199 "#define bbbb 4\n"
18200 "#define ccc (5)",
18201 "#define a 3\n"
18202 "// comments should break alignment\n"
18203 "//\n"
18204 "#define bbbb 4\n"
18205 "#define ccc (5)",
18206 Style);
18208 // Test across empty lines and comments
18209 Style.AlignConsecutiveMacros.AcrossComments = true;
18210 verifyFormat("#define a 3\n"
18211 "\n"
18212 "// line comment\n"
18213 "#define bbbb 4\n"
18214 "#define ccc (5)",
18215 Style);
18217 verifyFormat("#define a 3\n"
18218 "\n"
18219 "\n"
18220 "/* multi-line *\n"
18221 " * block comment */\n"
18222 "\n"
18223 "\n"
18224 "#define bbbb 4\n"
18225 "#define ccc (5)",
18226 "#define a 3\n"
18227 "\n"
18228 "\n"
18229 "/* multi-line *\n"
18230 " * block comment */\n"
18231 "\n"
18232 "\n"
18233 "#define bbbb 4\n"
18234 "#define ccc (5)",
18235 Style);
18237 verifyFormat("#define a 3\n"
18238 "\n"
18239 "\n"
18240 "/* multi-line *\n"
18241 " * block comment */\n"
18242 "\n"
18243 "\n"
18244 "#define bbbb 4\n"
18245 "#define ccc (5)",
18246 "#define a 3\n"
18247 "\n"
18248 "\n"
18249 "/* multi-line *\n"
18250 " * block comment */\n"
18251 "\n"
18252 "\n"
18253 "#define bbbb 4\n"
18254 "#define ccc (5)",
18255 Style);
18258 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
18259 FormatStyle Alignment = getLLVMStyle();
18260 Alignment.AlignConsecutiveMacros.Enabled = true;
18261 Alignment.AlignConsecutiveAssignments.Enabled = true;
18262 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18264 Alignment.MaxEmptyLinesToKeep = 10;
18265 /* Test alignment across empty lines */
18266 verifyFormat("int a = 5;\n"
18267 "\n"
18268 "int oneTwoThree = 123;",
18269 "int a = 5;\n"
18270 "\n"
18271 "int oneTwoThree= 123;",
18272 Alignment);
18273 verifyFormat("int a = 5;\n"
18274 "int one = 1;\n"
18275 "\n"
18276 "int oneTwoThree = 123;",
18277 "int a = 5;\n"
18278 "int one = 1;\n"
18279 "\n"
18280 "int oneTwoThree = 123;",
18281 Alignment);
18282 verifyFormat("int a = 5;\n"
18283 "int one = 1;\n"
18284 "\n"
18285 "int oneTwoThree = 123;\n"
18286 "int oneTwo = 12;",
18287 "int a = 5;\n"
18288 "int one = 1;\n"
18289 "\n"
18290 "int oneTwoThree = 123;\n"
18291 "int oneTwo = 12;",
18292 Alignment);
18294 /* Test across comments */
18295 verifyFormat("int a = 5;\n"
18296 "/* block comment */\n"
18297 "int oneTwoThree = 123;",
18298 "int a = 5;\n"
18299 "/* block comment */\n"
18300 "int oneTwoThree=123;",
18301 Alignment);
18303 verifyFormat("int a = 5;\n"
18304 "// line comment\n"
18305 "int oneTwoThree = 123;",
18306 "int a = 5;\n"
18307 "// line comment\n"
18308 "int oneTwoThree=123;",
18309 Alignment);
18311 /* Test across comments and newlines */
18312 verifyFormat("int a = 5;\n"
18313 "\n"
18314 "/* block comment */\n"
18315 "int oneTwoThree = 123;",
18316 "int a = 5;\n"
18317 "\n"
18318 "/* block comment */\n"
18319 "int oneTwoThree=123;",
18320 Alignment);
18322 verifyFormat("int a = 5;\n"
18323 "\n"
18324 "// line comment\n"
18325 "int oneTwoThree = 123;",
18326 "int a = 5;\n"
18327 "\n"
18328 "// line comment\n"
18329 "int oneTwoThree=123;",
18330 Alignment);
18333 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18334 FormatStyle Alignment = getLLVMStyle();
18335 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18336 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18337 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18339 Alignment.MaxEmptyLinesToKeep = 10;
18340 /* Test alignment across empty lines */
18341 verifyFormat("int a = 5;\n"
18342 "\n"
18343 "float const oneTwoThree = 123;",
18344 "int a = 5;\n"
18345 "\n"
18346 "float const oneTwoThree = 123;",
18347 Alignment);
18348 verifyFormat("int a = 5;\n"
18349 "float const one = 1;\n"
18350 "\n"
18351 "int oneTwoThree = 123;",
18352 "int a = 5;\n"
18353 "float const one = 1;\n"
18354 "\n"
18355 "int oneTwoThree = 123;",
18356 Alignment);
18358 /* Test across comments */
18359 verifyFormat("float const a = 5;\n"
18360 "/* block comment */\n"
18361 "int oneTwoThree = 123;",
18362 "float const a = 5;\n"
18363 "/* block comment */\n"
18364 "int oneTwoThree=123;",
18365 Alignment);
18367 verifyFormat("float const a = 5;\n"
18368 "// line comment\n"
18369 "int oneTwoThree = 123;",
18370 "float const a = 5;\n"
18371 "// line comment\n"
18372 "int oneTwoThree=123;",
18373 Alignment);
18375 /* Test across comments and newlines */
18376 verifyFormat("float const a = 5;\n"
18377 "\n"
18378 "/* block comment */\n"
18379 "int oneTwoThree = 123;",
18380 "float const a = 5;\n"
18381 "\n"
18382 "/* block comment */\n"
18383 "int oneTwoThree=123;",
18384 Alignment);
18386 verifyFormat("float const a = 5;\n"
18387 "\n"
18388 "// line comment\n"
18389 "int oneTwoThree = 123;",
18390 "float const a = 5;\n"
18391 "\n"
18392 "// line comment\n"
18393 "int oneTwoThree=123;",
18394 Alignment);
18397 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18398 FormatStyle Alignment = getLLVMStyle();
18399 Alignment.AlignConsecutiveBitFields.Enabled = true;
18400 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18401 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18403 Alignment.MaxEmptyLinesToKeep = 10;
18404 /* Test alignment across empty lines */
18405 verifyFormat("int a : 5;\n"
18406 "\n"
18407 "int longbitfield : 6;",
18408 "int a : 5;\n"
18409 "\n"
18410 "int longbitfield : 6;",
18411 Alignment);
18412 verifyFormat("int a : 5;\n"
18413 "int one : 1;\n"
18414 "\n"
18415 "int longbitfield : 6;",
18416 "int a : 5;\n"
18417 "int one : 1;\n"
18418 "\n"
18419 "int longbitfield : 6;",
18420 Alignment);
18422 /* Test across comments */
18423 verifyFormat("int a : 5;\n"
18424 "/* block comment */\n"
18425 "int longbitfield : 6;",
18426 "int a : 5;\n"
18427 "/* block comment */\n"
18428 "int longbitfield : 6;",
18429 Alignment);
18430 verifyFormat("int a : 5;\n"
18431 "int one : 1;\n"
18432 "// line comment\n"
18433 "int longbitfield : 6;",
18434 "int a : 5;\n"
18435 "int one : 1;\n"
18436 "// line comment\n"
18437 "int longbitfield : 6;",
18438 Alignment);
18440 /* Test across comments and newlines */
18441 verifyFormat("int a : 5;\n"
18442 "/* block comment */\n"
18443 "\n"
18444 "int longbitfield : 6;",
18445 "int a : 5;\n"
18446 "/* block comment */\n"
18447 "\n"
18448 "int longbitfield : 6;",
18449 Alignment);
18450 verifyFormat("int a : 5;\n"
18451 "int one : 1;\n"
18452 "\n"
18453 "// line comment\n"
18454 "\n"
18455 "int longbitfield : 6;",
18456 "int a : 5;\n"
18457 "int one : 1;\n"
18458 "\n"
18459 "// line comment \n"
18460 "\n"
18461 "int longbitfield : 6;",
18462 Alignment);
18465 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
18466 FormatStyle Alignment = getLLVMStyle();
18467 Alignment.AlignConsecutiveMacros.Enabled = true;
18468 Alignment.AlignConsecutiveAssignments.Enabled = true;
18469 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18471 Alignment.MaxEmptyLinesToKeep = 10;
18472 /* Test alignment across empty lines */
18473 verifyFormat("int a = 5;\n"
18474 "\n"
18475 "int oneTwoThree = 123;",
18476 "int a = 5;\n"
18477 "\n"
18478 "int oneTwoThree= 123;",
18479 Alignment);
18480 verifyFormat("int a = 5;\n"
18481 "int one = 1;\n"
18482 "\n"
18483 "int oneTwoThree = 123;",
18484 "int a = 5;\n"
18485 "int one = 1;\n"
18486 "\n"
18487 "int oneTwoThree = 123;",
18488 Alignment);
18490 /* Test across comments */
18491 verifyFormat("int a = 5;\n"
18492 "/* block comment */\n"
18493 "int oneTwoThree = 123;",
18494 "int a = 5;\n"
18495 "/* block comment */\n"
18496 "int oneTwoThree=123;",
18497 Alignment);
18499 verifyFormat("int a = 5;\n"
18500 "// line comment\n"
18501 "int oneTwoThree = 123;",
18502 "int a = 5;\n"
18503 "// line comment\n"
18504 "int oneTwoThree=123;",
18505 Alignment);
18507 verifyFormat("int a = 5;\n"
18508 "/*\n"
18509 " * multi-line block comment\n"
18510 " */\n"
18511 "int oneTwoThree = 123;",
18512 "int a = 5;\n"
18513 "/*\n"
18514 " * multi-line block comment\n"
18515 " */\n"
18516 "int oneTwoThree=123;",
18517 Alignment);
18519 verifyFormat("int a = 5;\n"
18520 "//\n"
18521 "// multi-line line comment\n"
18522 "//\n"
18523 "int oneTwoThree = 123;",
18524 "int a = 5;\n"
18525 "//\n"
18526 "// multi-line line comment\n"
18527 "//\n"
18528 "int oneTwoThree=123;",
18529 Alignment);
18531 /* Test across comments and newlines */
18532 verifyFormat("int a = 5;\n"
18533 "\n"
18534 "/* block comment */\n"
18535 "int oneTwoThree = 123;",
18536 "int a = 5;\n"
18537 "\n"
18538 "/* block comment */\n"
18539 "int oneTwoThree=123;",
18540 Alignment);
18542 verifyFormat("int a = 5;\n"
18543 "\n"
18544 "// line comment\n"
18545 "int oneTwoThree = 123;",
18546 "int a = 5;\n"
18547 "\n"
18548 "// line comment\n"
18549 "int oneTwoThree=123;",
18550 Alignment);
18553 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
18554 FormatStyle Alignment = getLLVMStyle();
18555 Alignment.AlignConsecutiveMacros.Enabled = true;
18556 Alignment.AlignConsecutiveAssignments.Enabled = true;
18557 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18558 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18559 verifyFormat("int a = 5;\n"
18560 "int oneTwoThree = 123;",
18561 Alignment);
18562 verifyFormat("int a = method();\n"
18563 "int oneTwoThree = 133;",
18564 Alignment);
18565 verifyFormat("a &= 5;\n"
18566 "bcd *= 5;\n"
18567 "ghtyf += 5;\n"
18568 "dvfvdb -= 5;\n"
18569 "a /= 5;\n"
18570 "vdsvsv %= 5;\n"
18571 "sfdbddfbdfbb ^= 5;\n"
18572 "dvsdsv |= 5;\n"
18573 "int dsvvdvsdvvv = 123;",
18574 Alignment);
18575 verifyFormat("int i = 1, j = 10;\n"
18576 "something = 2000;",
18577 Alignment);
18578 verifyFormat("something = 2000;\n"
18579 "int i = 1, j = 10;",
18580 Alignment);
18581 verifyFormat("something = 2000;\n"
18582 "another = 911;\n"
18583 "int i = 1, j = 10;\n"
18584 "oneMore = 1;\n"
18585 "i = 2;",
18586 Alignment);
18587 verifyFormat("int a = 5;\n"
18588 "int one = 1;\n"
18589 "method();\n"
18590 "int oneTwoThree = 123;\n"
18591 "int oneTwo = 12;",
18592 Alignment);
18593 verifyFormat("int oneTwoThree = 123;\n"
18594 "int oneTwo = 12;\n"
18595 "method();",
18596 Alignment);
18597 verifyFormat("int oneTwoThree = 123; // comment\n"
18598 "int oneTwo = 12; // comment",
18599 Alignment);
18601 // Bug 25167
18602 /* Uncomment when fixed
18603 verifyFormat("#if A\n"
18604 "#else\n"
18605 "int aaaaaaaa = 12;\n"
18606 "#endif\n"
18607 "#if B\n"
18608 "#else\n"
18609 "int a = 12;\n"
18610 "#endif",
18611 Alignment);
18612 verifyFormat("enum foo {\n"
18613 "#if A\n"
18614 "#else\n"
18615 " aaaaaaaa = 12;\n"
18616 "#endif\n"
18617 "#if B\n"
18618 "#else\n"
18619 " a = 12;\n"
18620 "#endif\n"
18621 "};",
18622 Alignment);
18625 Alignment.MaxEmptyLinesToKeep = 10;
18626 /* Test alignment across empty lines */
18627 verifyFormat("int a = 5;\n"
18628 "\n"
18629 "int oneTwoThree = 123;",
18630 "int a = 5;\n"
18631 "\n"
18632 "int oneTwoThree= 123;",
18633 Alignment);
18634 verifyFormat("int a = 5;\n"
18635 "int one = 1;\n"
18636 "\n"
18637 "int oneTwoThree = 123;",
18638 "int a = 5;\n"
18639 "int one = 1;\n"
18640 "\n"
18641 "int oneTwoThree = 123;",
18642 Alignment);
18643 verifyFormat("int a = 5;\n"
18644 "int one = 1;\n"
18645 "\n"
18646 "int oneTwoThree = 123;\n"
18647 "int oneTwo = 12;",
18648 "int a = 5;\n"
18649 "int one = 1;\n"
18650 "\n"
18651 "int oneTwoThree = 123;\n"
18652 "int oneTwo = 12;",
18653 Alignment);
18655 /* Test across comments */
18656 verifyFormat("int a = 5;\n"
18657 "/* block comment */\n"
18658 "int oneTwoThree = 123;",
18659 "int a = 5;\n"
18660 "/* block comment */\n"
18661 "int oneTwoThree=123;",
18662 Alignment);
18664 verifyFormat("int a = 5;\n"
18665 "// line comment\n"
18666 "int oneTwoThree = 123;",
18667 "int a = 5;\n"
18668 "// line comment\n"
18669 "int oneTwoThree=123;",
18670 Alignment);
18672 /* Test across comments and newlines */
18673 verifyFormat("int a = 5;\n"
18674 "\n"
18675 "/* block comment */\n"
18676 "int oneTwoThree = 123;",
18677 "int a = 5;\n"
18678 "\n"
18679 "/* block comment */\n"
18680 "int oneTwoThree=123;",
18681 Alignment);
18683 verifyFormat("int a = 5;\n"
18684 "\n"
18685 "// line comment\n"
18686 "int oneTwoThree = 123;",
18687 "int a = 5;\n"
18688 "\n"
18689 "// line comment\n"
18690 "int oneTwoThree=123;",
18691 Alignment);
18693 verifyFormat("int a = 5;\n"
18694 "//\n"
18695 "// multi-line line comment\n"
18696 "//\n"
18697 "int oneTwoThree = 123;",
18698 "int a = 5;\n"
18699 "//\n"
18700 "// multi-line line comment\n"
18701 "//\n"
18702 "int oneTwoThree=123;",
18703 Alignment);
18705 verifyFormat("int a = 5;\n"
18706 "/*\n"
18707 " * multi-line block comment\n"
18708 " */\n"
18709 "int oneTwoThree = 123;",
18710 "int a = 5;\n"
18711 "/*\n"
18712 " * multi-line block comment\n"
18713 " */\n"
18714 "int oneTwoThree=123;",
18715 Alignment);
18717 verifyFormat("int a = 5;\n"
18718 "\n"
18719 "/* block comment */\n"
18720 "\n"
18721 "\n"
18722 "\n"
18723 "int oneTwoThree = 123;",
18724 "int a = 5;\n"
18725 "\n"
18726 "/* block comment */\n"
18727 "\n"
18728 "\n"
18729 "\n"
18730 "int oneTwoThree=123;",
18731 Alignment);
18733 verifyFormat("int a = 5;\n"
18734 "\n"
18735 "// line comment\n"
18736 "\n"
18737 "\n"
18738 "\n"
18739 "int oneTwoThree = 123;",
18740 "int a = 5;\n"
18741 "\n"
18742 "// line comment\n"
18743 "\n"
18744 "\n"
18745 "\n"
18746 "int oneTwoThree=123;",
18747 Alignment);
18749 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18750 verifyFormat("#define A \\\n"
18751 " int aaaa = 12; \\\n"
18752 " int b = 23; \\\n"
18753 " int ccc = 234; \\\n"
18754 " int dddddddddd = 2345;",
18755 Alignment);
18756 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18757 verifyFormat("#define A \\\n"
18758 " int aaaa = 12; \\\n"
18759 " int b = 23; \\\n"
18760 " int ccc = 234; \\\n"
18761 " int dddddddddd = 2345;",
18762 Alignment);
18763 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18764 verifyFormat("#define A "
18765 " \\\n"
18766 " int aaaa = 12; "
18767 " \\\n"
18768 " int b = 23; "
18769 " \\\n"
18770 " int ccc = 234; "
18771 " \\\n"
18772 " int dddddddddd = 2345;",
18773 Alignment);
18774 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18775 "k = 4, int l = 5,\n"
18776 " int m = 6) {\n"
18777 " int j = 10;\n"
18778 " otherThing = 1;\n"
18779 "}",
18780 Alignment);
18781 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18782 " int i = 1;\n"
18783 " int j = 2;\n"
18784 " int big = 10000;\n"
18785 "}",
18786 Alignment);
18787 verifyFormat("class C {\n"
18788 "public:\n"
18789 " int i = 1;\n"
18790 " virtual void f() = 0;\n"
18791 "};",
18792 Alignment);
18793 verifyFormat("int i = 1;\n"
18794 "if (SomeType t = getSomething()) {\n"
18795 "}\n"
18796 "int j = 2;\n"
18797 "int big = 10000;",
18798 Alignment);
18799 verifyFormat("int j = 7;\n"
18800 "for (int k = 0; k < N; ++k) {\n"
18801 "}\n"
18802 "int j = 2;\n"
18803 "int big = 10000;\n"
18804 "}",
18805 Alignment);
18806 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18807 verifyFormat("int i = 1;\n"
18808 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18809 " = someLooooooooooooooooongFunction();\n"
18810 "int j = 2;",
18811 Alignment);
18812 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18813 verifyFormat("int i = 1;\n"
18814 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18815 " someLooooooooooooooooongFunction();\n"
18816 "int j = 2;",
18817 Alignment);
18819 verifyFormat("auto lambda = []() {\n"
18820 " auto i = 0;\n"
18821 " return 0;\n"
18822 "};\n"
18823 "int i = 0;\n"
18824 "auto v = type{\n"
18825 " i = 1, //\n"
18826 " (i = 2), //\n"
18827 " i = 3 //\n"
18828 "};",
18829 Alignment);
18831 verifyFormat(
18832 "int i = 1;\n"
18833 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18834 " loooooooooooooooooooooongParameterB);\n"
18835 "int j = 2;",
18836 Alignment);
18838 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18839 " typename B = very_long_type_name_1,\n"
18840 " typename T_2 = very_long_type_name_2>\n"
18841 "auto foo() {}",
18842 Alignment);
18843 verifyFormat("int a, b = 1;\n"
18844 "int c = 2;\n"
18845 "int dd = 3;",
18846 Alignment);
18847 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18848 "float b[1][] = {{3.f}};",
18849 Alignment);
18850 verifyFormat("for (int i = 0; i < 1; i++)\n"
18851 " int x = 1;",
18852 Alignment);
18853 verifyFormat("for (i = 0; i < 1; i++)\n"
18854 " x = 1;\n"
18855 "y = 1;",
18856 Alignment);
18858 Alignment.ReflowComments = FormatStyle::RCS_Always;
18859 Alignment.ColumnLimit = 50;
18860 verifyFormat("int x = 0;\n"
18861 "int yy = 1; /// specificlennospace\n"
18862 "int zzz = 2;",
18863 "int x = 0;\n"
18864 "int yy = 1; ///specificlennospace\n"
18865 "int zzz = 2;",
18866 Alignment);
18869 TEST_F(FormatTest, AlignCompoundAssignments) {
18870 FormatStyle Alignment = getLLVMStyle();
18871 Alignment.AlignConsecutiveAssignments.Enabled = true;
18872 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
18873 Alignment.AlignConsecutiveAssignments.PadOperators = false;
18874 verifyFormat("sfdbddfbdfbb = 5;\n"
18875 "dvsdsv = 5;\n"
18876 "int dsvvdvsdvvv = 123;",
18877 Alignment);
18878 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18879 "dvsdsv |= 5;\n"
18880 "int dsvvdvsdvvv = 123;",
18881 Alignment);
18882 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18883 "dvsdsv <<= 5;\n"
18884 "int dsvvdvsdvvv = 123;",
18885 Alignment);
18886 verifyFormat("int xxx = 5;\n"
18887 "xxx = 5;\n"
18888 "{\n"
18889 " int yyy = 6;\n"
18890 " yyy = 6;\n"
18891 "}",
18892 Alignment);
18893 verifyFormat("int xxx = 5;\n"
18894 "xxx += 5;\n"
18895 "{\n"
18896 " int yyy = 6;\n"
18897 " yyy += 6;\n"
18898 "}",
18899 Alignment);
18900 // Test that `<=` is not treated as a compound assignment.
18901 verifyFormat("aa &= 5;\n"
18902 "b <= 10;\n"
18903 "c = 15;",
18904 Alignment);
18905 Alignment.AlignConsecutiveAssignments.PadOperators = true;
18906 verifyFormat("sfdbddfbdfbb = 5;\n"
18907 "dvsdsv = 5;\n"
18908 "int dsvvdvsdvvv = 123;",
18909 Alignment);
18910 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18911 "dvsdsv |= 5;\n"
18912 "int dsvvdvsdvvv = 123;",
18913 Alignment);
18914 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18915 "dvsdsv <<= 5;\n"
18916 "int dsvvdvsdvvv = 123;",
18917 Alignment);
18918 verifyFormat("a += 5;\n"
18919 "one = 1;\n"
18920 "\n"
18921 "oneTwoThree = 123;",
18922 "a += 5;\n"
18923 "one = 1;\n"
18924 "\n"
18925 "oneTwoThree = 123;",
18926 Alignment);
18927 verifyFormat("a += 5;\n"
18928 "one = 1;\n"
18929 "//\n"
18930 "oneTwoThree = 123;",
18931 "a += 5;\n"
18932 "one = 1;\n"
18933 "//\n"
18934 "oneTwoThree = 123;",
18935 Alignment);
18936 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18937 verifyFormat("a += 5;\n"
18938 "one = 1;\n"
18939 "\n"
18940 "oneTwoThree = 123;",
18941 "a += 5;\n"
18942 "one = 1;\n"
18943 "\n"
18944 "oneTwoThree = 123;",
18945 Alignment);
18946 verifyFormat("a += 5;\n"
18947 "one = 1;\n"
18948 "//\n"
18949 "oneTwoThree = 123;",
18950 "a += 5;\n"
18951 "one = 1;\n"
18952 "//\n"
18953 "oneTwoThree = 123;",
18954 Alignment);
18955 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
18956 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18957 verifyFormat("a += 5;\n"
18958 "one = 1;\n"
18959 "\n"
18960 "oneTwoThree = 123;",
18961 "a += 5;\n"
18962 "one = 1;\n"
18963 "\n"
18964 "oneTwoThree = 123;",
18965 Alignment);
18966 verifyFormat("a += 5;\n"
18967 "one = 1;\n"
18968 "//\n"
18969 "oneTwoThree = 123;",
18970 "a += 5;\n"
18971 "one = 1;\n"
18972 "//\n"
18973 "oneTwoThree = 123;",
18974 Alignment);
18975 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18976 verifyFormat("a += 5;\n"
18977 "one >>= 1;\n"
18978 "\n"
18979 "oneTwoThree = 123;",
18980 "a += 5;\n"
18981 "one >>= 1;\n"
18982 "\n"
18983 "oneTwoThree = 123;",
18984 Alignment);
18985 verifyFormat("a += 5;\n"
18986 "one = 1;\n"
18987 "//\n"
18988 "oneTwoThree <<= 123;",
18989 "a += 5;\n"
18990 "one = 1;\n"
18991 "//\n"
18992 "oneTwoThree <<= 123;",
18993 Alignment);
18996 TEST_F(FormatTest, AlignConsecutiveAssignments) {
18997 FormatStyle Alignment = getLLVMStyle();
18998 Alignment.AlignConsecutiveMacros.Enabled = true;
18999 verifyFormat("int a = 5;\n"
19000 "int oneTwoThree = 123;",
19001 Alignment);
19002 verifyFormat("int a = 5;\n"
19003 "int oneTwoThree = 123;",
19004 Alignment);
19006 Alignment.AlignConsecutiveAssignments.Enabled = true;
19007 verifyFormat("int a = 5;\n"
19008 "int oneTwoThree = 123;",
19009 Alignment);
19010 verifyFormat("int a = method();\n"
19011 "int oneTwoThree = 133;",
19012 Alignment);
19013 verifyFormat("aa <= 5;\n"
19014 "a &= 5;\n"
19015 "bcd *= 5;\n"
19016 "ghtyf += 5;\n"
19017 "dvfvdb -= 5;\n"
19018 "a /= 5;\n"
19019 "vdsvsv %= 5;\n"
19020 "sfdbddfbdfbb ^= 5;\n"
19021 "dvsdsv |= 5;\n"
19022 "int dsvvdvsdvvv = 123;",
19023 Alignment);
19024 verifyFormat("int i = 1, j = 10;\n"
19025 "something = 2000;",
19026 Alignment);
19027 verifyFormat("something = 2000;\n"
19028 "int i = 1, j = 10;",
19029 Alignment);
19030 verifyFormat("something = 2000;\n"
19031 "another = 911;\n"
19032 "int i = 1, j = 10;\n"
19033 "oneMore = 1;\n"
19034 "i = 2;",
19035 Alignment);
19036 verifyFormat("int a = 5;\n"
19037 "int one = 1;\n"
19038 "method();\n"
19039 "int oneTwoThree = 123;\n"
19040 "int oneTwo = 12;",
19041 Alignment);
19042 verifyFormat("int oneTwoThree = 123;\n"
19043 "int oneTwo = 12;\n"
19044 "method();",
19045 Alignment);
19046 verifyFormat("int oneTwoThree = 123; // comment\n"
19047 "int oneTwo = 12; // comment",
19048 Alignment);
19049 verifyFormat("int f() = default;\n"
19050 "int &operator() = default;\n"
19051 "int &operator=() {",
19052 Alignment);
19053 verifyFormat("int f() = delete;\n"
19054 "int &operator() = delete;\n"
19055 "int &operator=() {",
19056 Alignment);
19057 verifyFormat("int f() = default; // comment\n"
19058 "int &operator() = default; // comment\n"
19059 "int &operator=() {",
19060 Alignment);
19061 verifyFormat("int f() = default;\n"
19062 "int &operator() = default;\n"
19063 "int &operator==() {",
19064 Alignment);
19065 verifyFormat("int f() = default;\n"
19066 "int &operator() = default;\n"
19067 "int &operator<=() {",
19068 Alignment);
19069 verifyFormat("int f() = default;\n"
19070 "int &operator() = default;\n"
19071 "int &operator!=() {",
19072 Alignment);
19073 verifyFormat("int f() = default;\n"
19074 "int &operator() = default;\n"
19075 "int &operator=();",
19076 Alignment);
19077 verifyFormat("int f() = delete;\n"
19078 "int &operator() = delete;\n"
19079 "int &operator=();",
19080 Alignment);
19081 verifyFormat("/* long long padding */ int f() = default;\n"
19082 "int &operator() = default;\n"
19083 "int &operator/**/ =();",
19084 Alignment);
19085 // https://llvm.org/PR33697
19086 FormatStyle AlignmentWithPenalty = getLLVMStyle();
19087 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
19088 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
19089 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19090 " void f() = delete;\n"
19091 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19092 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19093 "};",
19094 AlignmentWithPenalty);
19096 // Bug 25167
19097 /* Uncomment when fixed
19098 verifyFormat("#if A\n"
19099 "#else\n"
19100 "int aaaaaaaa = 12;\n"
19101 "#endif\n"
19102 "#if B\n"
19103 "#else\n"
19104 "int a = 12;\n"
19105 "#endif",
19106 Alignment);
19107 verifyFormat("enum foo {\n"
19108 "#if A\n"
19109 "#else\n"
19110 " aaaaaaaa = 12;\n"
19111 "#endif\n"
19112 "#if B\n"
19113 "#else\n"
19114 " a = 12;\n"
19115 "#endif\n"
19116 "};",
19117 Alignment);
19120 verifyFormat("int a = 5;\n"
19121 "\n"
19122 "int oneTwoThree = 123;",
19123 "int a = 5;\n"
19124 "\n"
19125 "int oneTwoThree= 123;",
19126 Alignment);
19127 verifyFormat("int a = 5;\n"
19128 "int one = 1;\n"
19129 "\n"
19130 "int oneTwoThree = 123;",
19131 "int a = 5;\n"
19132 "int one = 1;\n"
19133 "\n"
19134 "int oneTwoThree = 123;",
19135 Alignment);
19136 verifyFormat("int a = 5;\n"
19137 "int one = 1;\n"
19138 "\n"
19139 "int oneTwoThree = 123;\n"
19140 "int oneTwo = 12;",
19141 "int a = 5;\n"
19142 "int one = 1;\n"
19143 "\n"
19144 "int oneTwoThree = 123;\n"
19145 "int oneTwo = 12;",
19146 Alignment);
19147 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19148 verifyFormat("#define A \\\n"
19149 " int aaaa = 12; \\\n"
19150 " int b = 23; \\\n"
19151 " int ccc = 234; \\\n"
19152 " int dddddddddd = 2345;",
19153 Alignment);
19154 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19155 verifyFormat("#define A \\\n"
19156 " int aaaa = 12; \\\n"
19157 " int b = 23; \\\n"
19158 " int ccc = 234; \\\n"
19159 " int dddddddddd = 2345;",
19160 Alignment);
19161 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19162 verifyFormat("#define A "
19163 " \\\n"
19164 " int aaaa = 12; "
19165 " \\\n"
19166 " int b = 23; "
19167 " \\\n"
19168 " int ccc = 234; "
19169 " \\\n"
19170 " int dddddddddd = 2345;",
19171 Alignment);
19172 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19173 "k = 4, int l = 5,\n"
19174 " int m = 6) {\n"
19175 " int j = 10;\n"
19176 " otherThing = 1;\n"
19177 "}",
19178 Alignment);
19179 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19180 " int i = 1;\n"
19181 " int j = 2;\n"
19182 " int big = 10000;\n"
19183 "}",
19184 Alignment);
19185 verifyFormat("class C {\n"
19186 "public:\n"
19187 " int i = 1;\n"
19188 " virtual void f() = 0;\n"
19189 "};",
19190 Alignment);
19191 verifyFormat("int i = 1;\n"
19192 "if (SomeType t = getSomething()) {\n"
19193 "}\n"
19194 "int j = 2;\n"
19195 "int big = 10000;",
19196 Alignment);
19197 verifyFormat("int j = 7;\n"
19198 "for (int k = 0; k < N; ++k) {\n"
19199 "}\n"
19200 "int j = 2;\n"
19201 "int big = 10000;\n"
19202 "}",
19203 Alignment);
19204 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19205 verifyFormat("int i = 1;\n"
19206 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19207 " = someLooooooooooooooooongFunction();\n"
19208 "int j = 2;",
19209 Alignment);
19210 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19211 verifyFormat("int i = 1;\n"
19212 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19213 " someLooooooooooooooooongFunction();\n"
19214 "int j = 2;",
19215 Alignment);
19217 verifyFormat("auto lambda = []() {\n"
19218 " auto i = 0;\n"
19219 " return 0;\n"
19220 "};\n"
19221 "int i = 0;\n"
19222 "auto v = type{\n"
19223 " i = 1, //\n"
19224 " (i = 2), //\n"
19225 " i = 3 //\n"
19226 "};",
19227 Alignment);
19229 verifyFormat(
19230 "int i = 1;\n"
19231 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19232 " loooooooooooooooooooooongParameterB);\n"
19233 "int j = 2;",
19234 Alignment);
19236 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19237 " typename B = very_long_type_name_1,\n"
19238 " typename T_2 = very_long_type_name_2>\n"
19239 "auto foo() {}",
19240 Alignment);
19241 verifyFormat("int a, b = 1;\n"
19242 "int c = 2;\n"
19243 "int dd = 3;",
19244 Alignment);
19245 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19246 "float b[1][] = {{3.f}};",
19247 Alignment);
19248 verifyFormat("for (int i = 0; i < 1; i++)\n"
19249 " int x = 1;",
19250 Alignment);
19251 verifyFormat("for (i = 0; i < 1; i++)\n"
19252 " x = 1;\n"
19253 "y = 1;",
19254 Alignment);
19256 EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always);
19257 Alignment.ColumnLimit = 50;
19258 verifyFormat("int x = 0;\n"
19259 "int yy = 1; /// specificlennospace\n"
19260 "int zzz = 2;",
19261 "int x = 0;\n"
19262 "int yy = 1; ///specificlennospace\n"
19263 "int zzz = 2;",
19264 Alignment);
19266 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19267 "auto b = [] {\n"
19268 " f();\n"
19269 " return;\n"
19270 "};",
19271 Alignment);
19272 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19273 "auto b = g([] {\n"
19274 " f();\n"
19275 " return;\n"
19276 "});",
19277 Alignment);
19278 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19279 "auto b = g(param, [] {\n"
19280 " f();\n"
19281 " return;\n"
19282 "});",
19283 Alignment);
19284 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19285 "auto b = [] {\n"
19286 " if (condition) {\n"
19287 " return;\n"
19288 " }\n"
19289 "};",
19290 Alignment);
19292 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19293 " ccc ? aaaaa : bbbbb,\n"
19294 " dddddddddddddddddddddddddd);",
19295 Alignment);
19296 // FIXME: https://llvm.org/PR53497
19297 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19298 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19299 // " ccc ? aaaaa : bbbbb,\n"
19300 // " dddddddddddddddddddddddddd);",
19301 // Alignment);
19303 // Confirm proper handling of AlignConsecutiveAssignments with
19304 // BinPackArguments.
19305 // See https://llvm.org/PR55360
19306 Alignment = getLLVMStyleWithColumns(50);
19307 Alignment.AlignConsecutiveAssignments.Enabled = true;
19308 Alignment.BinPackArguments = false;
19309 verifyFormat("int a_long_name = 1;\n"
19310 "auto b = B({a_long_name, a_long_name},\n"
19311 " {a_longer_name_for_wrap,\n"
19312 " a_longer_name_for_wrap});",
19313 Alignment);
19314 verifyFormat("int a_long_name = 1;\n"
19315 "auto b = B{{a_long_name, a_long_name},\n"
19316 " {a_longer_name_for_wrap,\n"
19317 " a_longer_name_for_wrap}};",
19318 Alignment);
19320 Alignment = getLLVMStyleWithColumns(60);
19321 Alignment.AlignConsecutiveAssignments.Enabled = true;
19322 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19323 "using I = std::conditional_t<II::value >= 0,\n"
19324 " std::ic<int, II::value + 1>,\n"
19325 " std::ic<int, -1>>;",
19326 Alignment);
19327 verifyFormat("SomeName = Foo;\n"
19328 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19329 " arrrrrrrrrrg);",
19330 Alignment);
19332 Alignment.ColumnLimit = 80;
19333 Alignment.SpacesInAngles = FormatStyle::SIAS_Always;
19334 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19335 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19336 Alignment);
19337 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19338 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19339 Alignment);
19341 Alignment.SpacesInParens = FormatStyle::SIPO_Custom;
19342 Alignment.SpacesInParensOptions.InCStyleCasts = true;
19343 verifyFormat("void **ptr = ( void ** )unkn;\n"
19344 "ptr = ( void ** )ptr[0];",
19345 Alignment);
19346 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19347 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19348 Alignment);
19351 TEST_F(FormatTest, AlignConsecutiveBitFields) {
19352 FormatStyle Alignment = getLLVMStyle();
19353 Alignment.AlignConsecutiveBitFields.Enabled = true;
19354 verifyFormat("int const a : 5;\n"
19355 "int oneTwoThree : 23;",
19356 Alignment);
19358 // Initializers are allowed starting with c++2a
19359 verifyFormat("int const a : 5 = 1;\n"
19360 "int oneTwoThree : 23 = 0;",
19361 Alignment);
19363 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19364 verifyFormat("int const a : 5;\n"
19365 "int oneTwoThree : 23;",
19366 Alignment);
19368 verifyFormat("int const a : 5; // comment\n"
19369 "int oneTwoThree : 23; // comment",
19370 Alignment);
19372 verifyFormat("int const a : 5 = 1;\n"
19373 "int oneTwoThree : 23 = 0;",
19374 Alignment);
19376 Alignment.AlignConsecutiveAssignments.Enabled = true;
19377 verifyFormat("int const a : 5 = 1;\n"
19378 "int oneTwoThree : 23 = 0;",
19379 Alignment);
19380 verifyFormat("int const a : 5 = {1};\n"
19381 "int oneTwoThree : 23 = 0;",
19382 Alignment);
19384 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19385 verifyFormat("int const a :5;\n"
19386 "int oneTwoThree:23;",
19387 Alignment);
19389 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19390 verifyFormat("int const a :5;\n"
19391 "int oneTwoThree :23;",
19392 Alignment);
19394 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19395 verifyFormat("int const a : 5;\n"
19396 "int oneTwoThree: 23;",
19397 Alignment);
19399 // Known limitations: ':' is only recognized as a bitfield colon when
19400 // followed by a number.
19402 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19403 "int a : 5;",
19404 Alignment);
19408 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19409 FormatStyle Alignment = getLLVMStyle();
19410 Alignment.AlignConsecutiveMacros.Enabled = true;
19411 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19412 verifyFormat("float const a = 5;\n"
19413 "int oneTwoThree = 123;",
19414 Alignment);
19415 verifyFormat("int a = 5;\n"
19416 "float const oneTwoThree = 123;",
19417 Alignment);
19419 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19420 verifyFormat("float const a = 5;\n"
19421 "int oneTwoThree = 123;",
19422 Alignment);
19423 verifyFormat("int a = method();\n"
19424 "float const oneTwoThree = 133;",
19425 Alignment);
19426 verifyFormat("int i = 1, j = 10;\n"
19427 "something = 2000;",
19428 Alignment);
19429 verifyFormat("something = 2000;\n"
19430 "int i = 1, j = 10;",
19431 Alignment);
19432 verifyFormat("float something = 2000;\n"
19433 "double another = 911;\n"
19434 "int i = 1, j = 10;\n"
19435 "const int *oneMore = 1;\n"
19436 "unsigned i = 2;",
19437 Alignment);
19438 verifyFormat("float a = 5;\n"
19439 "int one = 1;\n"
19440 "method();\n"
19441 "const double oneTwoThree = 123;\n"
19442 "const unsigned int oneTwo = 12;",
19443 Alignment);
19444 verifyFormat("int oneTwoThree{0}; // comment\n"
19445 "unsigned oneTwo; // comment",
19446 Alignment);
19447 verifyFormat("unsigned int *a;\n"
19448 "int *b;\n"
19449 "unsigned int Const *c;\n"
19450 "unsigned int const *d;\n"
19451 "unsigned int Const &e;\n"
19452 "unsigned int const &f;",
19453 Alignment);
19454 verifyFormat("Const unsigned int *c;\n"
19455 "const unsigned int *d;\n"
19456 "Const unsigned int &e;\n"
19457 "const unsigned int &f;\n"
19458 "const unsigned g;\n"
19459 "Const unsigned h;",
19460 Alignment);
19461 verifyFormat("float const a = 5;\n"
19462 "\n"
19463 "int oneTwoThree = 123;",
19464 "float const a = 5;\n"
19465 "\n"
19466 "int oneTwoThree= 123;",
19467 Alignment);
19468 verifyFormat("float a = 5;\n"
19469 "int one = 1;\n"
19470 "\n"
19471 "unsigned oneTwoThree = 123;",
19472 "float a = 5;\n"
19473 "int one = 1;\n"
19474 "\n"
19475 "unsigned oneTwoThree = 123;",
19476 Alignment);
19477 verifyFormat("float a = 5;\n"
19478 "int one = 1;\n"
19479 "\n"
19480 "unsigned oneTwoThree = 123;\n"
19481 "int oneTwo = 12;",
19482 "float a = 5;\n"
19483 "int one = 1;\n"
19484 "\n"
19485 "unsigned oneTwoThree = 123;\n"
19486 "int oneTwo = 12;",
19487 Alignment);
19488 // Function prototype alignment
19489 verifyFormat("int a();\n"
19490 "double b();",
19491 Alignment);
19492 verifyFormat("int a(int x);\n"
19493 "double b();",
19494 Alignment);
19495 verifyFormat("int a(const Test & = Test());\n"
19496 "int a1(int &foo, const Test & = Test());\n"
19497 "int a2(int &foo, const Test &name = Test());\n"
19498 "double b();",
19499 Alignment);
19500 verifyFormat("struct Test {\n"
19501 " Test(const Test &) = default;\n"
19502 " ~Test() = default;\n"
19503 " Test &operator=(const Test &) = default;\n"
19504 "};",
19505 Alignment);
19506 unsigned OldColumnLimit = Alignment.ColumnLimit;
19507 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19508 // otherwise the function parameters will be re-flowed onto a single line.
19509 Alignment.ColumnLimit = 0;
19510 verifyFormat("int a(int x,\n"
19511 " float y);\n"
19512 "double b(int x,\n"
19513 " double y);",
19514 "int a(int x,\n"
19515 " float y);\n"
19516 "double b(int x,\n"
19517 " double y);",
19518 Alignment);
19519 // This ensures that function parameters of function declarations are
19520 // correctly indented when their owning functions are indented.
19521 // The failure case here is for 'double y' to not be indented enough.
19522 verifyFormat("double a(int x);\n"
19523 "int b(int y,\n"
19524 " double z);",
19525 "double a(int x);\n"
19526 "int b(int y,\n"
19527 " double z);",
19528 Alignment);
19529 // Set ColumnLimit low so that we induce wrapping immediately after
19530 // the function name and opening paren.
19531 Alignment.ColumnLimit = 13;
19532 verifyFormat("int function(\n"
19533 " int x,\n"
19534 " bool y);",
19535 Alignment);
19536 // Set ColumnLimit low so that we break the argument list in multiple lines.
19537 Alignment.ColumnLimit = 35;
19538 verifyFormat("int a3(SomeTypeName1 &x,\n"
19539 " SomeTypeName2 &y,\n"
19540 " const Test & = Test());\n"
19541 "double b();",
19542 Alignment);
19543 Alignment.ColumnLimit = OldColumnLimit;
19544 // Ensure function pointers don't screw up recursive alignment
19545 verifyFormat("int a(int x, void (*fp)(int y));\n"
19546 "double b();",
19547 Alignment);
19548 Alignment.AlignConsecutiveAssignments.Enabled = true;
19549 verifyFormat("struct Test {\n"
19550 " Test(const Test &) = default;\n"
19551 " ~Test() = default;\n"
19552 " Test &operator=(const Test &) = default;\n"
19553 "};",
19554 Alignment);
19555 // Ensure recursive alignment is broken by function braces, so that the
19556 // "a = 1" does not align with subsequent assignments inside the function
19557 // body.
19558 verifyFormat("int func(int a = 1) {\n"
19559 " int b = 2;\n"
19560 " int cc = 3;\n"
19561 "}",
19562 Alignment);
19563 verifyFormat("float something = 2000;\n"
19564 "double another = 911;\n"
19565 "int i = 1, j = 10;\n"
19566 "const int *oneMore = 1;\n"
19567 "unsigned i = 2;",
19568 Alignment);
19569 verifyFormat("int oneTwoThree = {0}; // comment\n"
19570 "unsigned oneTwo = 0; // comment",
19571 Alignment);
19572 // Make sure that scope is correctly tracked, in the absence of braces
19573 verifyFormat("for (int i = 0; i < n; i++)\n"
19574 " j = i;\n"
19575 "double x = 1;",
19576 Alignment);
19577 verifyFormat("if (int i = 0)\n"
19578 " j = i;\n"
19579 "double x = 1;",
19580 Alignment);
19581 // Ensure operator[] and operator() are comprehended
19582 verifyFormat("struct test {\n"
19583 " long long int foo();\n"
19584 " int operator[](int a);\n"
19585 " double bar();\n"
19586 "};",
19587 Alignment);
19588 verifyFormat("struct test {\n"
19589 " long long int foo();\n"
19590 " int operator()(int a);\n"
19591 " double bar();\n"
19592 "};",
19593 Alignment);
19594 // http://llvm.org/PR52914
19595 verifyFormat("char *a[] = {\"a\", // comment\n"
19596 " \"bb\"};\n"
19597 "int bbbbbbb = 0;",
19598 Alignment);
19599 // http://llvm.org/PR68079
19600 verifyFormat("using Fn = int (A::*)();\n"
19601 "using RFn = int (A::*)() &;\n"
19602 "using RRFn = int (A::*)() &&;",
19603 Alignment);
19604 verifyFormat("using Fn = int (A::*)();\n"
19605 "using RFn = int *(A::*)() &;\n"
19606 "using RRFn = double (A::*)() &&;",
19607 Alignment);
19609 // PAS_Right
19610 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19611 " int const i = 1;\n"
19612 " int *j = 2;\n"
19613 " int big = 10000;\n"
19614 "\n"
19615 " unsigned oneTwoThree = 123;\n"
19616 " int oneTwo = 12;\n"
19617 " method();\n"
19618 " float k = 2;\n"
19619 " int ll = 10000;\n"
19620 "}",
19621 "void SomeFunction(int parameter= 0) {\n"
19622 " int const i= 1;\n"
19623 " int *j=2;\n"
19624 " int big = 10000;\n"
19625 "\n"
19626 "unsigned oneTwoThree =123;\n"
19627 "int oneTwo = 12;\n"
19628 " method();\n"
19629 "float k= 2;\n"
19630 "int ll=10000;\n"
19631 "}",
19632 Alignment);
19633 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19634 " int const i = 1;\n"
19635 " int **j = 2, ***k;\n"
19636 " int &k = i;\n"
19637 " int &&l = i + j;\n"
19638 " int big = 10000;\n"
19639 "\n"
19640 " unsigned oneTwoThree = 123;\n"
19641 " int oneTwo = 12;\n"
19642 " method();\n"
19643 " float k = 2;\n"
19644 " int ll = 10000;\n"
19645 "}",
19646 "void SomeFunction(int parameter= 0) {\n"
19647 " int const i= 1;\n"
19648 " int **j=2,***k;\n"
19649 "int &k=i;\n"
19650 "int &&l=i+j;\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 // variables are aligned at their name, pointers are at the right most
19661 // position
19662 verifyFormat("int *a;\n"
19663 "int **b;\n"
19664 "int ***c;\n"
19665 "int foobar;",
19666 Alignment);
19668 // PAS_Left
19669 FormatStyle AlignmentLeft = Alignment;
19670 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
19671 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19672 " int const i = 1;\n"
19673 " int* j = 2;\n"
19674 " int big = 10000;\n"
19675 "\n"
19676 " unsigned oneTwoThree = 123;\n"
19677 " int oneTwo = 12;\n"
19678 " method();\n"
19679 " float k = 2;\n"
19680 " int ll = 10000;\n"
19681 "}",
19682 "void SomeFunction(int parameter= 0) {\n"
19683 " int const i= 1;\n"
19684 " int *j=2;\n"
19685 " int big = 10000;\n"
19686 "\n"
19687 "unsigned oneTwoThree =123;\n"
19688 "int oneTwo = 12;\n"
19689 " method();\n"
19690 "float k= 2;\n"
19691 "int ll=10000;\n"
19692 "}",
19693 AlignmentLeft);
19694 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19695 " int const i = 1;\n"
19696 " int** j = 2;\n"
19697 " int& k = i;\n"
19698 " int&& l = i + j;\n"
19699 " int big = 10000;\n"
19700 "\n"
19701 " unsigned oneTwoThree = 123;\n"
19702 " int oneTwo = 12;\n"
19703 " method();\n"
19704 " float k = 2;\n"
19705 " int ll = 10000;\n"
19706 "}",
19707 "void SomeFunction(int parameter= 0) {\n"
19708 " int const i= 1;\n"
19709 " int **j=2;\n"
19710 "int &k=i;\n"
19711 "int &&l=i+j;\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 // variables are aligned at their name, pointers are at the left most position
19722 verifyFormat("int* a;\n"
19723 "int** b;\n"
19724 "int*** c;\n"
19725 "int foobar;",
19726 AlignmentLeft);
19728 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19729 "double b();",
19730 AlignmentLeft);
19732 // PAS_Middle
19733 FormatStyle AlignmentMiddle = Alignment;
19734 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
19735 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19736 " int const i = 1;\n"
19737 " int * j = 2;\n"
19738 " int big = 10000;\n"
19739 "\n"
19740 " unsigned oneTwoThree = 123;\n"
19741 " int oneTwo = 12;\n"
19742 " method();\n"
19743 " float k = 2;\n"
19744 " int ll = 10000;\n"
19745 "}",
19746 "void SomeFunction(int parameter= 0) {\n"
19747 " int const i= 1;\n"
19748 " int *j=2;\n"
19749 " int big = 10000;\n"
19750 "\n"
19751 "unsigned oneTwoThree =123;\n"
19752 "int oneTwo = 12;\n"
19753 " method();\n"
19754 "float k= 2;\n"
19755 "int ll=10000;\n"
19756 "}",
19757 AlignmentMiddle);
19758 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19759 " int const i = 1;\n"
19760 " int ** j = 2, ***k;\n"
19761 " int & k = i;\n"
19762 " int && l = i + j;\n"
19763 " int big = 10000;\n"
19764 "\n"
19765 " unsigned oneTwoThree = 123;\n"
19766 " int oneTwo = 12;\n"
19767 " method();\n"
19768 " float k = 2;\n"
19769 " int ll = 10000;\n"
19770 "}",
19771 "void SomeFunction(int parameter= 0) {\n"
19772 " int const i= 1;\n"
19773 " int **j=2,***k;\n"
19774 "int &k=i;\n"
19775 "int &&l=i+j;\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 // variables are aligned at their name, pointers are in the middle
19786 verifyFormat("int * a;\n"
19787 "int * b;\n"
19788 "int *** c;\n"
19789 "int foobar;",
19790 AlignmentMiddle);
19792 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19793 "double b();",
19794 AlignmentMiddle);
19796 Alignment.AlignConsecutiveAssignments.Enabled = false;
19797 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19798 verifyFormat("#define A \\\n"
19799 " int aaaa = 12; \\\n"
19800 " float b = 23; \\\n"
19801 " const int ccc = 234; \\\n"
19802 " unsigned dddddddddd = 2345;",
19803 Alignment);
19804 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19805 verifyFormat("#define A \\\n"
19806 " int aaaa = 12; \\\n"
19807 " float b = 23; \\\n"
19808 " const int ccc = 234; \\\n"
19809 " unsigned dddddddddd = 2345;",
19810 Alignment);
19811 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19812 Alignment.ColumnLimit = 30;
19813 verifyFormat("#define A \\\n"
19814 " int aaaa = 12; \\\n"
19815 " float b = 23; \\\n"
19816 " const int ccc = 234; \\\n"
19817 " int dddddddddd = 2345;",
19818 Alignment);
19819 Alignment.ColumnLimit = 80;
19820 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19821 "k = 4, int l = 5,\n"
19822 " int m = 6) {\n"
19823 " const int j = 10;\n"
19824 " otherThing = 1;\n"
19825 "}",
19826 Alignment);
19827 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19828 " int const i = 1;\n"
19829 " int *j = 2;\n"
19830 " int big = 10000;\n"
19831 "}",
19832 Alignment);
19833 verifyFormat("class C {\n"
19834 "public:\n"
19835 " int i = 1;\n"
19836 " virtual void f() = 0;\n"
19837 "};",
19838 Alignment);
19839 verifyFormat("float i = 1;\n"
19840 "if (SomeType t = getSomething()) {\n"
19841 "}\n"
19842 "const unsigned j = 2;\n"
19843 "int big = 10000;",
19844 Alignment);
19845 verifyFormat("float j = 7;\n"
19846 "for (int k = 0; k < N; ++k) {\n"
19847 "}\n"
19848 "unsigned j = 2;\n"
19849 "int big = 10000;\n"
19850 "}",
19851 Alignment);
19852 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19853 verifyFormat("float i = 1;\n"
19854 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19855 " = someLooooooooooooooooongFunction();\n"
19856 "int j = 2;",
19857 Alignment);
19858 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19859 verifyFormat("int i = 1;\n"
19860 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19861 " someLooooooooooooooooongFunction();\n"
19862 "int j = 2;",
19863 Alignment);
19865 Alignment.AlignConsecutiveAssignments.Enabled = true;
19866 verifyFormat("auto lambda = []() {\n"
19867 " auto ii = 0;\n"
19868 " float j = 0;\n"
19869 " return 0;\n"
19870 "};\n"
19871 "int i = 0;\n"
19872 "float i2 = 0;\n"
19873 "auto v = type{\n"
19874 " i = 1, //\n"
19875 " (i = 2), //\n"
19876 " i = 3 //\n"
19877 "};",
19878 Alignment);
19879 Alignment.AlignConsecutiveAssignments.Enabled = false;
19881 verifyFormat(
19882 "int i = 1;\n"
19883 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19884 " loooooooooooooooooooooongParameterB);\n"
19885 "int j = 2;",
19886 Alignment);
19888 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19889 // We expect declarations and assignments to align, as long as it doesn't
19890 // exceed the column limit, starting a new alignment sequence whenever it
19891 // happens.
19892 Alignment.AlignConsecutiveAssignments.Enabled = true;
19893 Alignment.ColumnLimit = 30;
19894 verifyFormat("float ii = 1;\n"
19895 "unsigned j = 2;\n"
19896 "int someVerylongVariable = 1;\n"
19897 "AnotherLongType ll = 123456;\n"
19898 "VeryVeryLongType k = 2;\n"
19899 "int myvar = 1;",
19900 Alignment);
19901 Alignment.ColumnLimit = 80;
19902 Alignment.AlignConsecutiveAssignments.Enabled = false;
19904 verifyFormat(
19905 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19906 " typename LongType, typename B>\n"
19907 "auto foo() {}",
19908 Alignment);
19909 verifyFormat("float a, b = 1;\n"
19910 "int c = 2;\n"
19911 "int dd = 3;",
19912 Alignment);
19913 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19914 "float b[1][] = {{3.f}};",
19915 Alignment);
19916 Alignment.AlignConsecutiveAssignments.Enabled = true;
19917 verifyFormat("float a, b = 1;\n"
19918 "int c = 2;\n"
19919 "int dd = 3;",
19920 Alignment);
19921 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19922 "float b[1][] = {{3.f}};",
19923 Alignment);
19924 Alignment.AlignConsecutiveAssignments.Enabled = false;
19926 Alignment.ColumnLimit = 30;
19927 Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine;
19928 verifyFormat("void foo(float a,\n"
19929 " float b,\n"
19930 " int c,\n"
19931 " uint32_t *d) {\n"
19932 " int *e = 0;\n"
19933 " float f = 0;\n"
19934 " double g = 0;\n"
19935 "}\n"
19936 "void bar(ino_t a,\n"
19937 " int b,\n"
19938 " uint32_t *c,\n"
19939 " bool d) {}",
19940 Alignment);
19941 Alignment.BinPackParameters = FormatStyle::BPPS_BinPack;
19942 Alignment.ColumnLimit = 80;
19944 // Bug 33507
19945 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19946 verifyFormat(
19947 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19948 " static const Version verVs2017;\n"
19949 " return true;\n"
19950 "});",
19951 Alignment);
19952 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19954 // See llvm.org/PR35641
19955 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19956 verifyFormat("int func() { //\n"
19957 " int b;\n"
19958 " unsigned c;\n"
19959 "}",
19960 Alignment);
19962 // See PR37175
19963 FormatStyle Style = getMozillaStyle();
19964 Style.AlignConsecutiveDeclarations.Enabled = true;
19965 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19966 "foo(int a);",
19967 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
19969 Alignment.PointerAlignment = FormatStyle::PAS_Left;
19970 verifyFormat("unsigned int* a;\n"
19971 "int* b;\n"
19972 "unsigned int Const* c;\n"
19973 "unsigned int const* d;\n"
19974 "unsigned int Const& e;\n"
19975 "unsigned int const& f;",
19976 Alignment);
19977 verifyFormat("Const unsigned int* c;\n"
19978 "const unsigned int* d;\n"
19979 "Const unsigned int& e;\n"
19980 "const unsigned int& f;\n"
19981 "const unsigned g;\n"
19982 "Const unsigned h;",
19983 Alignment);
19985 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19986 verifyFormat("unsigned int * a;\n"
19987 "int * b;\n"
19988 "unsigned int Const * c;\n"
19989 "unsigned int const * d;\n"
19990 "unsigned int Const & e;\n"
19991 "unsigned int const & f;",
19992 Alignment);
19993 verifyFormat("Const unsigned int * c;\n"
19994 "const unsigned int * d;\n"
19995 "Const unsigned int & e;\n"
19996 "const unsigned int & f;\n"
19997 "const unsigned g;\n"
19998 "Const unsigned h;",
19999 Alignment);
20001 // See PR46529
20002 FormatStyle BracedAlign = getLLVMStyle();
20003 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
20004 verifyFormat("const auto result{[]() {\n"
20005 " const auto something = 1;\n"
20006 " return 2;\n"
20007 "}};",
20008 BracedAlign);
20009 verifyFormat("int foo{[]() {\n"
20010 " int bar{0};\n"
20011 " return 0;\n"
20012 "}()};",
20013 BracedAlign);
20014 BracedAlign.Cpp11BracedListStyle = false;
20015 verifyFormat("const auto result{ []() {\n"
20016 " const auto something = 1;\n"
20017 " return 2;\n"
20018 "} };",
20019 BracedAlign);
20020 verifyFormat("int foo{ []() {\n"
20021 " int bar{ 0 };\n"
20022 " return 0;\n"
20023 "}() };",
20024 BracedAlign);
20026 Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
20027 verifyFormat("unsigned int f1(void);\n"
20028 "void f2(void);\n"
20029 "size_t f3(void);",
20030 Alignment);
20033 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
20034 FormatStyle Alignment = getLLVMStyle();
20035 Alignment.AllowShortCaseLabelsOnASingleLine = true;
20036 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
20038 verifyFormat("switch (level) {\n"
20039 "case log::info: return \"info\";\n"
20040 "case log::warning: return \"warning\";\n"
20041 "default: return \"default\";\n"
20042 "}",
20043 Alignment);
20045 verifyFormat("switch (level) {\n"
20046 "case log::info: return \"info\";\n"
20047 "case log::warning: return \"warning\";\n"
20048 "}",
20049 "switch (level) {\n"
20050 "case log::info: return \"info\";\n"
20051 "case log::warning:\n"
20052 " return \"warning\";\n"
20053 "}",
20054 Alignment);
20056 // Empty case statements push out the alignment, but non-short case labels
20057 // don't.
20058 verifyFormat("switch (level) {\n"
20059 "case log::info: return \"info\";\n"
20060 "case log::critical:\n"
20061 "case log::warning:\n"
20062 "case log::severe: return \"severe\";\n"
20063 "case log::extra_severe:\n"
20064 " // comment\n"
20065 " return \"extra_severe\";\n"
20066 "}",
20067 Alignment);
20069 // Verify comments and empty lines break the alignment.
20070 verifyNoChange("switch (level) {\n"
20071 "case log::info: return \"info\";\n"
20072 "case log::warning: return \"warning\";\n"
20073 "// comment\n"
20074 "case log::critical: return \"critical\";\n"
20075 "default: return \"default\";\n"
20076 "\n"
20077 "case log::severe: return \"severe\";\n"
20078 "}",
20079 Alignment);
20081 // Empty case statements don't break the alignment, and potentially push it
20082 // out.
20083 verifyFormat("switch (level) {\n"
20084 "case log::info: return \"info\";\n"
20085 "case log::warning:\n"
20086 "case log::critical:\n"
20087 "default: return \"default\";\n"
20088 "}",
20089 Alignment);
20091 // Implicit fallthrough cases can be aligned with either a comment or
20092 // [[fallthrough]]
20093 verifyFormat("switch (level) {\n"
20094 "case log::info: return \"info\";\n"
20095 "case log::warning: // fallthrough\n"
20096 "case log::error: return \"error\";\n"
20097 "case log::critical: /*fallthrough*/\n"
20098 "case log::severe: return \"severe\";\n"
20099 "case log::diag: [[fallthrough]];\n"
20100 "default: return \"default\";\n"
20101 "}",
20102 Alignment);
20104 // Verify trailing comment that needs a reflow also gets aligned properly.
20105 verifyFormat("switch (level) {\n"
20106 "case log::info: return \"info\";\n"
20107 "case log::warning: // fallthrough\n"
20108 "case log::error: return \"error\";\n"
20109 "}",
20110 "switch (level) {\n"
20111 "case log::info: return \"info\";\n"
20112 "case log::warning: //fallthrough\n"
20113 "case log::error: return \"error\";\n"
20114 "}",
20115 Alignment);
20117 // Verify adjacent non-short case statements don't change the alignment, and
20118 // properly break the set of consecutive statements.
20119 verifyFormat("switch (level) {\n"
20120 "case log::critical:\n"
20121 " // comment\n"
20122 " return \"critical\";\n"
20123 "case log::info: return \"info\";\n"
20124 "case log::warning: return \"warning\";\n"
20125 "default:\n"
20126 " // comment\n"
20127 " return \"\";\n"
20128 "case log::error: return \"error\";\n"
20129 "case log::severe: return \"severe\";\n"
20130 "case log::extra_critical:\n"
20131 " // comment\n"
20132 " return \"extra critical\";\n"
20133 "}",
20134 Alignment);
20136 Alignment.SpaceBeforeCaseColon = true;
20137 verifyFormat("switch (level) {\n"
20138 "case log::info : return \"info\";\n"
20139 "case log::warning : return \"warning\";\n"
20140 "default : return \"default\";\n"
20141 "}",
20142 Alignment);
20143 Alignment.SpaceBeforeCaseColon = false;
20145 // Make sure we don't incorrectly align correctly across nested switch cases.
20146 verifyFormat("switch (level) {\n"
20147 "case log::info: return \"info\";\n"
20148 "case log::warning: return \"warning\";\n"
20149 "case log::other:\n"
20150 " switch (sublevel) {\n"
20151 " case log::info: return \"info\";\n"
20152 " case log::warning: return \"warning\";\n"
20153 " }\n"
20154 " break;\n"
20155 "case log::error: return \"error\";\n"
20156 "default: return \"default\";\n"
20157 "}",
20158 "switch (level) {\n"
20159 "case log::info: return \"info\";\n"
20160 "case log::warning: return \"warning\";\n"
20161 "case log::other: switch (sublevel) {\n"
20162 " case log::info: return \"info\";\n"
20163 " case log::warning: return \"warning\";\n"
20164 "}\n"
20165 "break;\n"
20166 "case log::error: return \"error\";\n"
20167 "default: return \"default\";\n"
20168 "}",
20169 Alignment);
20171 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
20173 verifyFormat("switch (level) {\n"
20174 "case log::info: return \"info\";\n"
20175 "\n"
20176 "case log::warning: return \"warning\";\n"
20177 "}",
20178 "switch (level) {\n"
20179 "case log::info: return \"info\";\n"
20180 "\n"
20181 "case log::warning: return \"warning\";\n"
20182 "}",
20183 Alignment);
20185 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
20187 verifyNoChange("switch (level) {\n"
20188 "case log::info: return \"info\";\n"
20189 "\n"
20190 "/* block comment */\n"
20191 "\n"
20192 "// line comment\n"
20193 "case log::warning: return \"warning\";\n"
20194 "}",
20195 Alignment);
20197 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
20199 verifyFormat("switch (level) {\n"
20200 "case log::info: return \"info\";\n"
20201 "//\n"
20202 "case log::warning: return \"warning\";\n"
20203 "}",
20204 Alignment);
20206 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
20208 verifyFormat("switch (level) {\n"
20209 "case log::info : return \"info\";\n"
20210 "case log::warning: return \"warning\";\n"
20211 "default : return \"default\";\n"
20212 "}",
20213 Alignment);
20215 // With AlignCaseColons, empty case statements don't break alignment of
20216 // consecutive case statements (and are aligned).
20217 verifyFormat("switch (level) {\n"
20218 "case log::info : return \"info\";\n"
20219 "case log::warning :\n"
20220 "case log::critical:\n"
20221 "default : return \"default\";\n"
20222 "}",
20223 Alignment);
20225 // Final non-short case labels shouldn't have their colon aligned
20226 verifyFormat("switch (level) {\n"
20227 "case log::info : return \"info\";\n"
20228 "case log::warning :\n"
20229 "case log::critical:\n"
20230 "case log::severe : return \"severe\";\n"
20231 "default:\n"
20232 " // comment\n"
20233 " return \"default\";\n"
20234 "}",
20235 Alignment);
20237 // Verify adjacent non-short case statements break the set of consecutive
20238 // alignments and aren't aligned with adjacent non-short case statements if
20239 // AlignCaseColons is set.
20240 verifyFormat("switch (level) {\n"
20241 "case log::critical:\n"
20242 " // comment\n"
20243 " return \"critical\";\n"
20244 "case log::info : return \"info\";\n"
20245 "case log::warning: return \"warning\";\n"
20246 "default:\n"
20247 " // comment\n"
20248 " return \"\";\n"
20249 "case log::error : return \"error\";\n"
20250 "case log::severe: return \"severe\";\n"
20251 "case log::extra_critical:\n"
20252 " // comment\n"
20253 " return \"extra critical\";\n"
20254 "}",
20255 Alignment);
20257 Alignment.SpaceBeforeCaseColon = true;
20258 verifyFormat("switch (level) {\n"
20259 "case log::info : return \"info\";\n"
20260 "case log::warning : return \"warning\";\n"
20261 "case log::error :\n"
20262 "default : return \"default\";\n"
20263 "}",
20264 Alignment);
20267 TEST_F(FormatTest, AlignWithLineBreaks) {
20268 auto Style = getLLVMStyleWithColumns(120);
20270 EXPECT_EQ(Style.AlignConsecutiveAssignments,
20271 FormatStyle::AlignConsecutiveStyle(
20272 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20273 /*AcrossComments=*/false, /*AlignCompound=*/false,
20274 /*AlignFunctionDeclarations=*/false,
20275 /*AlignFunctionPointers=*/false,
20276 /*PadOperators=*/true}));
20277 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20278 FormatStyle::AlignConsecutiveStyle(
20279 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20280 /*AcrossComments=*/false, /*AlignCompound=*/false,
20281 /*AlignFunctionDeclarations=*/true,
20282 /*AlignFunctionPointers=*/false,
20283 /*PadOperators=*/false}));
20284 verifyFormat("void foo() {\n"
20285 " int myVar = 5;\n"
20286 " double x = 3.14;\n"
20287 " auto str = \"Hello \"\n"
20288 " \"World\";\n"
20289 " auto s = \"Hello \"\n"
20290 " \"Again\";\n"
20291 "}",
20292 Style);
20294 // clang-format off
20295 verifyFormat("void foo() {\n"
20296 " const int capacityBefore = Entries.capacity();\n"
20297 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20298 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20299 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20300 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20301 "}",
20302 Style);
20303 // clang-format on
20305 Style.AlignConsecutiveAssignments.Enabled = true;
20306 verifyFormat("void foo() {\n"
20307 " int myVar = 5;\n"
20308 " double x = 3.14;\n"
20309 " auto str = \"Hello \"\n"
20310 " \"World\";\n"
20311 " auto s = \"Hello \"\n"
20312 " \"Again\";\n"
20313 "}",
20314 Style);
20316 // clang-format off
20317 verifyFormat("void foo() {\n"
20318 " const int capacityBefore = Entries.capacity();\n"
20319 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20320 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20321 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20322 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20323 "}",
20324 Style);
20325 // clang-format on
20327 Style.AlignConsecutiveAssignments.Enabled = false;
20328 Style.AlignConsecutiveDeclarations.Enabled = true;
20329 verifyFormat("void foo() {\n"
20330 " int myVar = 5;\n"
20331 " double x = 3.14;\n"
20332 " auto str = \"Hello \"\n"
20333 " \"World\";\n"
20334 " auto s = \"Hello \"\n"
20335 " \"Again\";\n"
20336 "}",
20337 Style);
20339 // clang-format off
20340 verifyFormat("void foo() {\n"
20341 " const int capacityBefore = Entries.capacity();\n"
20342 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20343 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20344 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20345 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20346 "}",
20347 Style);
20348 // clang-format on
20350 Style.AlignConsecutiveAssignments.Enabled = true;
20351 Style.AlignConsecutiveDeclarations.Enabled = true;
20353 verifyFormat("void foo() {\n"
20354 " int myVar = 5;\n"
20355 " double x = 3.14;\n"
20356 " auto str = \"Hello \"\n"
20357 " \"World\";\n"
20358 " auto s = \"Hello \"\n"
20359 " \"Again\";\n"
20360 "}",
20361 Style);
20363 // clang-format off
20364 verifyFormat("void foo() {\n"
20365 " const int capacityBefore = Entries.capacity();\n"
20366 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20367 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20368 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20369 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20370 "}",
20371 Style);
20372 // clang-format on
20374 Style = getLLVMStyleWithColumns(20);
20375 Style.AlignConsecutiveAssignments.Enabled = true;
20376 Style.IndentWidth = 4;
20378 verifyFormat("void foo() {\n"
20379 " int i1 = 1;\n"
20380 " int j = 0;\n"
20381 " int k = bar(\n"
20382 " argument1,\n"
20383 " argument2);\n"
20384 "}",
20385 Style);
20387 verifyFormat("unsigned i = 0;\n"
20388 "int a[] = {\n"
20389 " 1234567890,\n"
20390 " -1234567890};",
20391 Style);
20393 Style.ColumnLimit = 120;
20395 // clang-format off
20396 verifyFormat("void SomeFunc() {\n"
20397 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20398 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20399 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20400 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20401 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20402 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20403 "}",
20404 Style);
20405 // clang-format on
20407 Style.BinPackArguments = false;
20409 // clang-format off
20410 verifyFormat("void SomeFunc() {\n"
20411 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20412 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20413 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20414 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20415 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20416 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20417 "}",
20418 Style);
20419 // clang-format on
20422 TEST_F(FormatTest, AlignWithInitializerPeriods) {
20423 auto Style = getLLVMStyleWithColumns(60);
20425 verifyFormat("void foo1(void) {\n"
20426 " BYTE p[1] = 1;\n"
20427 " A B = {.one_foooooooooooooooo = 2,\n"
20428 " .two_fooooooooooooo = 3,\n"
20429 " .three_fooooooooooooo = 4};\n"
20430 " BYTE payload = 2;\n"
20431 "}",
20432 Style);
20434 Style.AlignConsecutiveAssignments.Enabled = true;
20435 Style.AlignConsecutiveDeclarations.Enabled = false;
20436 verifyFormat("void foo2(void) {\n"
20437 " BYTE p[1] = 1;\n"
20438 " A B = {.one_foooooooooooooooo = 2,\n"
20439 " .two_fooooooooooooo = 3,\n"
20440 " .three_fooooooooooooo = 4};\n"
20441 " BYTE payload = 2;\n"
20442 "}",
20443 Style);
20445 Style.AlignConsecutiveAssignments.Enabled = false;
20446 Style.AlignConsecutiveDeclarations.Enabled = true;
20447 verifyFormat("void foo3(void) {\n"
20448 " BYTE p[1] = 1;\n"
20449 " A B = {.one_foooooooooooooooo = 2,\n"
20450 " .two_fooooooooooooo = 3,\n"
20451 " .three_fooooooooooooo = 4};\n"
20452 " BYTE payload = 2;\n"
20453 "}",
20454 Style);
20456 Style.AlignConsecutiveAssignments.Enabled = true;
20457 Style.AlignConsecutiveDeclarations.Enabled = true;
20458 verifyFormat("void foo4(void) {\n"
20459 " BYTE p[1] = 1;\n"
20460 " A B = {.one_foooooooooooooooo = 2,\n"
20461 " .two_fooooooooooooo = 3,\n"
20462 " .three_fooooooooooooo = 4};\n"
20463 " BYTE payload = 2;\n"
20464 "}",
20465 Style);
20468 TEST_F(FormatTest, LinuxBraceBreaking) {
20469 FormatStyle LinuxBraceStyle = getLLVMStyle();
20470 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
20471 verifyFormat("namespace a\n"
20472 "{\n"
20473 "class A\n"
20474 "{\n"
20475 " void f()\n"
20476 " {\n"
20477 " if (true) {\n"
20478 " a();\n"
20479 " b();\n"
20480 " } else {\n"
20481 " a();\n"
20482 " }\n"
20483 " }\n"
20484 " void g() { return; }\n"
20485 "};\n"
20486 "struct B {\n"
20487 " int x;\n"
20488 "};\n"
20489 "} // namespace a",
20490 LinuxBraceStyle);
20491 verifyFormat("enum X {\n"
20492 " Y = 0,\n"
20493 "}",
20494 LinuxBraceStyle);
20495 verifyFormat("struct S {\n"
20496 " int Type;\n"
20497 " union {\n"
20498 " int x;\n"
20499 " double y;\n"
20500 " } Value;\n"
20501 " class C\n"
20502 " {\n"
20503 " MyFavoriteType Value;\n"
20504 " } Class;\n"
20505 "}",
20506 LinuxBraceStyle);
20509 TEST_F(FormatTest, MozillaBraceBreaking) {
20510 FormatStyle MozillaBraceStyle = getLLVMStyle();
20511 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
20512 MozillaBraceStyle.FixNamespaceComments = false;
20513 verifyFormat("namespace a {\n"
20514 "class A\n"
20515 "{\n"
20516 " void f()\n"
20517 " {\n"
20518 " if (true) {\n"
20519 " a();\n"
20520 " b();\n"
20521 " }\n"
20522 " }\n"
20523 " void g() { return; }\n"
20524 "};\n"
20525 "enum E\n"
20526 "{\n"
20527 " A,\n"
20528 " // foo\n"
20529 " B,\n"
20530 " C\n"
20531 "};\n"
20532 "struct B\n"
20533 "{\n"
20534 " int x;\n"
20535 "};\n"
20536 "}",
20537 MozillaBraceStyle);
20538 verifyFormat("struct S\n"
20539 "{\n"
20540 " int Type;\n"
20541 " union\n"
20542 " {\n"
20543 " int x;\n"
20544 " double y;\n"
20545 " } Value;\n"
20546 " class C\n"
20547 " {\n"
20548 " MyFavoriteType Value;\n"
20549 " } Class;\n"
20550 "}",
20551 MozillaBraceStyle);
20554 TEST_F(FormatTest, StroustrupBraceBreaking) {
20555 FormatStyle StroustrupBraceStyle = getLLVMStyle();
20556 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
20557 verifyFormat("namespace a {\n"
20558 "class A {\n"
20559 " void f()\n"
20560 " {\n"
20561 " if (true) {\n"
20562 " a();\n"
20563 " b();\n"
20564 " }\n"
20565 " }\n"
20566 " void g() { return; }\n"
20567 "};\n"
20568 "struct B {\n"
20569 " int x;\n"
20570 "};\n"
20571 "} // namespace a",
20572 StroustrupBraceStyle);
20574 verifyFormat("void foo()\n"
20575 "{\n"
20576 " if (a) {\n"
20577 " a();\n"
20578 " }\n"
20579 " else {\n"
20580 " b();\n"
20581 " }\n"
20582 "}",
20583 StroustrupBraceStyle);
20585 verifyFormat("#ifdef _DEBUG\n"
20586 "int foo(int i = 0)\n"
20587 "#else\n"
20588 "int foo(int i = 5)\n"
20589 "#endif\n"
20590 "{\n"
20591 " return i;\n"
20592 "}",
20593 StroustrupBraceStyle);
20595 verifyFormat("void foo() {}\n"
20596 "void bar()\n"
20597 "#ifdef _DEBUG\n"
20598 "{\n"
20599 " foo();\n"
20600 "}\n"
20601 "#else\n"
20602 "{\n"
20603 "}\n"
20604 "#endif",
20605 StroustrupBraceStyle);
20607 verifyFormat("void foobar() { int i = 5; }\n"
20608 "#ifdef _DEBUG\n"
20609 "void bar() {}\n"
20610 "#else\n"
20611 "void bar() { foobar(); }\n"
20612 "#endif",
20613 StroustrupBraceStyle);
20616 TEST_F(FormatTest, AllmanBraceBreaking) {
20617 FormatStyle AllmanBraceStyle = getLLVMStyle();
20618 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
20620 verifyFormat("namespace a\n"
20621 "{\n"
20622 "void f();\n"
20623 "void g();\n"
20624 "} // namespace a",
20625 "namespace a\n"
20626 "{\n"
20627 "void f();\n"
20628 "void g();\n"
20629 "}",
20630 AllmanBraceStyle);
20632 verifyFormat("namespace a\n"
20633 "{\n"
20634 "class A\n"
20635 "{\n"
20636 " void f()\n"
20637 " {\n"
20638 " if (true)\n"
20639 " {\n"
20640 " a();\n"
20641 " b();\n"
20642 " }\n"
20643 " }\n"
20644 " void g() { return; }\n"
20645 "};\n"
20646 "struct B\n"
20647 "{\n"
20648 " int x;\n"
20649 "};\n"
20650 "union C\n"
20651 "{\n"
20652 "};\n"
20653 "} // namespace a",
20654 AllmanBraceStyle);
20656 verifyFormat("void f()\n"
20657 "{\n"
20658 " if (true)\n"
20659 " {\n"
20660 " a();\n"
20661 " }\n"
20662 " else if (false)\n"
20663 " {\n"
20664 " b();\n"
20665 " }\n"
20666 " else\n"
20667 " {\n"
20668 " c();\n"
20669 " }\n"
20670 "}",
20671 AllmanBraceStyle);
20673 verifyFormat("void f()\n"
20674 "{\n"
20675 " for (int i = 0; i < 10; ++i)\n"
20676 " {\n"
20677 " a();\n"
20678 " }\n"
20679 " while (false)\n"
20680 " {\n"
20681 " b();\n"
20682 " }\n"
20683 " do\n"
20684 " {\n"
20685 " c();\n"
20686 " } while (false)\n"
20687 "}",
20688 AllmanBraceStyle);
20690 verifyFormat("void f(int a)\n"
20691 "{\n"
20692 " switch (a)\n"
20693 " {\n"
20694 " case 0:\n"
20695 " break;\n"
20696 " case 1:\n"
20697 " {\n"
20698 " break;\n"
20699 " }\n"
20700 " case 2:\n"
20701 " {\n"
20702 " }\n"
20703 " break;\n"
20704 " default:\n"
20705 " break;\n"
20706 " }\n"
20707 "}",
20708 AllmanBraceStyle);
20710 verifyFormat("enum X\n"
20711 "{\n"
20712 " Y = 0,\n"
20713 "}",
20714 AllmanBraceStyle);
20715 verifyFormat("enum X\n"
20716 "{\n"
20717 " Y = 0\n"
20718 "}",
20719 AllmanBraceStyle);
20721 verifyFormat("@interface BSApplicationController ()\n"
20722 "{\n"
20723 "@private\n"
20724 " id _extraIvar;\n"
20725 "}\n"
20726 "@end",
20727 AllmanBraceStyle);
20729 verifyFormat("#ifdef _DEBUG\n"
20730 "int foo(int i = 0)\n"
20731 "#else\n"
20732 "int foo(int i = 5)\n"
20733 "#endif\n"
20734 "{\n"
20735 " return i;\n"
20736 "}",
20737 AllmanBraceStyle);
20739 verifyFormat("void foo() {}\n"
20740 "void bar()\n"
20741 "#ifdef _DEBUG\n"
20742 "{\n"
20743 " foo();\n"
20744 "}\n"
20745 "#else\n"
20746 "{\n"
20747 "}\n"
20748 "#endif",
20749 AllmanBraceStyle);
20751 verifyFormat("void foobar() { int i = 5; }\n"
20752 "#ifdef _DEBUG\n"
20753 "void bar() {}\n"
20754 "#else\n"
20755 "void bar() { foobar(); }\n"
20756 "#endif",
20757 AllmanBraceStyle);
20759 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
20760 FormatStyle::SLS_All);
20762 verifyFormat("[](int i) { return i + 2; };\n"
20763 "[](int i, int j)\n"
20764 "{\n"
20765 " auto x = i + j;\n"
20766 " auto y = i * j;\n"
20767 " return x ^ y;\n"
20768 "};\n"
20769 "void foo()\n"
20770 "{\n"
20771 " auto shortLambda = [](int i) { return i + 2; };\n"
20772 " auto longLambda = [](int i, int j)\n"
20773 " {\n"
20774 " auto x = i + j;\n"
20775 " auto y = i * j;\n"
20776 " return x ^ y;\n"
20777 " };\n"
20778 "}",
20779 AllmanBraceStyle);
20781 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20783 verifyFormat("[](int i)\n"
20784 "{\n"
20785 " return i + 2;\n"
20786 "};\n"
20787 "[](int i, int j)\n"
20788 "{\n"
20789 " auto x = i + j;\n"
20790 " auto y = i * j;\n"
20791 " return x ^ y;\n"
20792 "};\n"
20793 "void foo()\n"
20794 "{\n"
20795 " auto shortLambda = [](int i)\n"
20796 " {\n"
20797 " return i + 2;\n"
20798 " };\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 // Reset
20809 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
20811 // This shouldn't affect ObjC blocks..
20812 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20813 " // ...\n"
20814 " int i;\n"
20815 "}];",
20816 AllmanBraceStyle);
20817 verifyFormat("void (^block)(void) = ^{\n"
20818 " // ...\n"
20819 " int i;\n"
20820 "};",
20821 AllmanBraceStyle);
20822 // .. or dict literals.
20823 verifyFormat("void f()\n"
20824 "{\n"
20825 " // ...\n"
20826 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20827 "}",
20828 AllmanBraceStyle);
20829 verifyFormat("void f()\n"
20830 "{\n"
20831 " // ...\n"
20832 " [object someMethod:@{a : @\"b\"}];\n"
20833 "}",
20834 AllmanBraceStyle);
20835 verifyFormat("int f()\n"
20836 "{ // comment\n"
20837 " return 42;\n"
20838 "}",
20839 AllmanBraceStyle);
20841 AllmanBraceStyle.ColumnLimit = 19;
20842 verifyFormat("void f() { int i; }", AllmanBraceStyle);
20843 AllmanBraceStyle.ColumnLimit = 18;
20844 verifyFormat("void f()\n"
20845 "{\n"
20846 " int i;\n"
20847 "}",
20848 AllmanBraceStyle);
20849 AllmanBraceStyle.ColumnLimit = 80;
20851 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
20852 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20853 FormatStyle::SIS_WithoutElse;
20854 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20855 verifyFormat("void f(bool b)\n"
20856 "{\n"
20857 " if (b)\n"
20858 " {\n"
20859 " return;\n"
20860 " }\n"
20861 "}",
20862 BreakBeforeBraceShortIfs);
20863 verifyFormat("void f(bool b)\n"
20864 "{\n"
20865 " if constexpr (b)\n"
20866 " {\n"
20867 " return;\n"
20868 " }\n"
20869 "}",
20870 BreakBeforeBraceShortIfs);
20871 verifyFormat("void f(bool b)\n"
20872 "{\n"
20873 " if CONSTEXPR (b)\n"
20874 " {\n"
20875 " return;\n"
20876 " }\n"
20877 "}",
20878 BreakBeforeBraceShortIfs);
20879 verifyFormat("void f(bool b)\n"
20880 "{\n"
20881 " if (b) return;\n"
20882 "}",
20883 BreakBeforeBraceShortIfs);
20884 verifyFormat("void f(bool b)\n"
20885 "{\n"
20886 " if constexpr (b) return;\n"
20887 "}",
20888 BreakBeforeBraceShortIfs);
20889 verifyFormat("void f(bool b)\n"
20890 "{\n"
20891 " if CONSTEXPR (b) return;\n"
20892 "}",
20893 BreakBeforeBraceShortIfs);
20894 verifyFormat("void f(bool b)\n"
20895 "{\n"
20896 " while (b)\n"
20897 " {\n"
20898 " return;\n"
20899 " }\n"
20900 "}",
20901 BreakBeforeBraceShortIfs);
20904 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
20905 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
20906 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
20908 // Make a few changes to the style for testing purposes
20909 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
20910 FormatStyle::SFS_Empty;
20911 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20913 // FIXME: this test case can't decide whether there should be a blank line
20914 // after the ~D() line or not. It adds one if one doesn't exist in the test
20915 // and it removes the line if one exists.
20917 verifyFormat("class A;\n"
20918 "namespace B\n"
20919 " {\n"
20920 "class C;\n"
20921 "// Comment\n"
20922 "class D\n"
20923 " {\n"
20924 "public:\n"
20925 " D();\n"
20926 " ~D() {}\n"
20927 "private:\n"
20928 " enum E\n"
20929 " {\n"
20930 " F\n"
20931 " }\n"
20932 " };\n"
20933 " } // namespace B",
20934 WhitesmithsBraceStyle);
20937 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
20938 verifyFormat("namespace a\n"
20939 " {\n"
20940 "class A\n"
20941 " {\n"
20942 " void f()\n"
20943 " {\n"
20944 " if (true)\n"
20945 " {\n"
20946 " a();\n"
20947 " b();\n"
20948 " }\n"
20949 " }\n"
20950 " void g()\n"
20951 " {\n"
20952 " return;\n"
20953 " }\n"
20954 " };\n"
20955 "struct B\n"
20956 " {\n"
20957 " int x;\n"
20958 " };\n"
20959 " } // namespace a",
20960 WhitesmithsBraceStyle);
20962 verifyFormat("namespace a\n"
20963 " {\n"
20964 "namespace b\n"
20965 " {\n"
20966 "class A\n"
20967 " {\n"
20968 " void f()\n"
20969 " {\n"
20970 " if (true)\n"
20971 " {\n"
20972 " a();\n"
20973 " b();\n"
20974 " }\n"
20975 " }\n"
20976 " void g()\n"
20977 " {\n"
20978 " return;\n"
20979 " }\n"
20980 " };\n"
20981 "struct B\n"
20982 " {\n"
20983 " int x;\n"
20984 " };\n"
20985 " } // namespace b\n"
20986 " } // namespace a",
20987 WhitesmithsBraceStyle);
20989 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
20990 verifyFormat("namespace a\n"
20991 " {\n"
20992 "namespace b\n"
20993 " {\n"
20994 " class A\n"
20995 " {\n"
20996 " void f()\n"
20997 " {\n"
20998 " if (true)\n"
20999 " {\n"
21000 " a();\n"
21001 " b();\n"
21002 " }\n"
21003 " }\n"
21004 " void g()\n"
21005 " {\n"
21006 " return;\n"
21007 " }\n"
21008 " };\n"
21009 " struct B\n"
21010 " {\n"
21011 " int x;\n"
21012 " };\n"
21013 " } // namespace b\n"
21014 " } // namespace a",
21015 WhitesmithsBraceStyle);
21017 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
21018 verifyFormat("namespace a\n"
21019 " {\n"
21020 " namespace b\n"
21021 " {\n"
21022 " class A\n"
21023 " {\n"
21024 " void f()\n"
21025 " {\n"
21026 " if (true)\n"
21027 " {\n"
21028 " a();\n"
21029 " b();\n"
21030 " }\n"
21031 " }\n"
21032 " void g()\n"
21033 " {\n"
21034 " return;\n"
21035 " }\n"
21036 " };\n"
21037 " struct B\n"
21038 " {\n"
21039 " int x;\n"
21040 " };\n"
21041 " } // namespace b\n"
21042 " } // namespace a",
21043 WhitesmithsBraceStyle);
21045 verifyFormat("void f()\n"
21046 " {\n"
21047 " if (true)\n"
21048 " {\n"
21049 " a();\n"
21050 " }\n"
21051 " else if (false)\n"
21052 " {\n"
21053 " b();\n"
21054 " }\n"
21055 " else\n"
21056 " {\n"
21057 " c();\n"
21058 " }\n"
21059 " }",
21060 WhitesmithsBraceStyle);
21062 verifyFormat("void f()\n"
21063 " {\n"
21064 " for (int i = 0; i < 10; ++i)\n"
21065 " {\n"
21066 " a();\n"
21067 " }\n"
21068 " while (false)\n"
21069 " {\n"
21070 " b();\n"
21071 " }\n"
21072 " do\n"
21073 " {\n"
21074 " c();\n"
21075 " } while (false)\n"
21076 " }",
21077 WhitesmithsBraceStyle);
21079 WhitesmithsBraceStyle.IndentCaseLabels = true;
21080 verifyFormat("void switchTest1(int a)\n"
21081 " {\n"
21082 " switch (a)\n"
21083 " {\n"
21084 " case 2:\n"
21085 " {\n"
21086 " }\n"
21087 " break;\n"
21088 " }\n"
21089 " }",
21090 WhitesmithsBraceStyle);
21092 verifyFormat("void switchTest2(int a)\n"
21093 " {\n"
21094 " switch (a)\n"
21095 " {\n"
21096 " case 0:\n"
21097 " break;\n"
21098 " case 1:\n"
21099 " {\n"
21100 " break;\n"
21101 " }\n"
21102 " case 2:\n"
21103 " {\n"
21104 " }\n"
21105 " break;\n"
21106 " default:\n"
21107 " break;\n"
21108 " }\n"
21109 " }",
21110 WhitesmithsBraceStyle);
21112 verifyFormat("void switchTest3(int a)\n"
21113 " {\n"
21114 " switch (a)\n"
21115 " {\n"
21116 " case 0:\n"
21117 " {\n"
21118 " foo(x);\n"
21119 " }\n"
21120 " break;\n"
21121 " default:\n"
21122 " {\n"
21123 " foo(1);\n"
21124 " }\n"
21125 " break;\n"
21126 " }\n"
21127 " }",
21128 WhitesmithsBraceStyle);
21130 WhitesmithsBraceStyle.IndentCaseLabels = false;
21132 verifyFormat("void switchTest4(int a)\n"
21133 " {\n"
21134 " switch (a)\n"
21135 " {\n"
21136 " case 2:\n"
21137 " {\n"
21138 " }\n"
21139 " break;\n"
21140 " }\n"
21141 " }",
21142 WhitesmithsBraceStyle);
21144 verifyFormat("void switchTest5(int a)\n"
21145 " {\n"
21146 " switch (a)\n"
21147 " {\n"
21148 " case 0:\n"
21149 " break;\n"
21150 " case 1:\n"
21151 " {\n"
21152 " foo();\n"
21153 " break;\n"
21154 " }\n"
21155 " case 2:\n"
21156 " {\n"
21157 " }\n"
21158 " break;\n"
21159 " default:\n"
21160 " break;\n"
21161 " }\n"
21162 " }",
21163 WhitesmithsBraceStyle);
21165 verifyFormat("void switchTest6(int a)\n"
21166 " {\n"
21167 " switch (a)\n"
21168 " {\n"
21169 " case 0:\n"
21170 " {\n"
21171 " foo(x);\n"
21172 " }\n"
21173 " break;\n"
21174 " default:\n"
21175 " {\n"
21176 " foo(1);\n"
21177 " }\n"
21178 " break;\n"
21179 " }\n"
21180 " }",
21181 WhitesmithsBraceStyle);
21183 verifyFormat("enum X\n"
21184 " {\n"
21185 " Y = 0, // testing\n"
21186 " }",
21187 WhitesmithsBraceStyle);
21189 verifyFormat("enum X\n"
21190 " {\n"
21191 " Y = 0\n"
21192 " }",
21193 WhitesmithsBraceStyle);
21194 verifyFormat("enum X\n"
21195 " {\n"
21196 " Y = 0,\n"
21197 " Z = 1\n"
21198 " };",
21199 WhitesmithsBraceStyle);
21201 verifyFormat("@interface BSApplicationController ()\n"
21202 " {\n"
21203 "@private\n"
21204 " id _extraIvar;\n"
21205 " }\n"
21206 "@end",
21207 WhitesmithsBraceStyle);
21209 verifyFormat("#ifdef _DEBUG\n"
21210 "int foo(int i = 0)\n"
21211 "#else\n"
21212 "int foo(int i = 5)\n"
21213 "#endif\n"
21214 " {\n"
21215 " return i;\n"
21216 " }",
21217 WhitesmithsBraceStyle);
21219 verifyFormat("void foo() {}\n"
21220 "void bar()\n"
21221 "#ifdef _DEBUG\n"
21222 " {\n"
21223 " foo();\n"
21224 " }\n"
21225 "#else\n"
21226 " {\n"
21227 " }\n"
21228 "#endif",
21229 WhitesmithsBraceStyle);
21231 verifyFormat("void foobar()\n"
21232 " {\n"
21233 " int i = 5;\n"
21234 " }\n"
21235 "#ifdef _DEBUG\n"
21236 "void bar() {}\n"
21237 "#else\n"
21238 "void bar()\n"
21239 " {\n"
21240 " foobar();\n"
21241 " }\n"
21242 "#endif",
21243 WhitesmithsBraceStyle);
21245 // This shouldn't affect ObjC blocks..
21246 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21247 " // ...\n"
21248 " int i;\n"
21249 "}];",
21250 WhitesmithsBraceStyle);
21251 verifyFormat("void (^block)(void) = ^{\n"
21252 " // ...\n"
21253 " int i;\n"
21254 "};",
21255 WhitesmithsBraceStyle);
21256 // .. or dict literals.
21257 verifyFormat("void f()\n"
21258 " {\n"
21259 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21260 " }",
21261 WhitesmithsBraceStyle);
21263 verifyFormat("int f()\n"
21264 " { // comment\n"
21265 " return 42;\n"
21266 " }",
21267 WhitesmithsBraceStyle);
21269 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
21270 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21271 FormatStyle::SIS_OnlyFirstIf;
21272 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21273 verifyFormat("void f(bool b)\n"
21274 " {\n"
21275 " if (b)\n"
21276 " {\n"
21277 " return;\n"
21278 " }\n"
21279 " }",
21280 BreakBeforeBraceShortIfs);
21281 verifyFormat("void f(bool b)\n"
21282 " {\n"
21283 " if (b) return;\n"
21284 " }",
21285 BreakBeforeBraceShortIfs);
21286 verifyFormat("void f(bool b)\n"
21287 " {\n"
21288 " while (b)\n"
21289 " {\n"
21290 " return;\n"
21291 " }\n"
21292 " }",
21293 BreakBeforeBraceShortIfs);
21296 TEST_F(FormatTest, GNUBraceBreaking) {
21297 FormatStyle GNUBraceStyle = getLLVMStyle();
21298 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
21299 verifyFormat("namespace a\n"
21300 "{\n"
21301 "class A\n"
21302 "{\n"
21303 " void f()\n"
21304 " {\n"
21305 " int a;\n"
21306 " {\n"
21307 " int b;\n"
21308 " }\n"
21309 " if (true)\n"
21310 " {\n"
21311 " a();\n"
21312 " b();\n"
21313 " }\n"
21314 " }\n"
21315 " void g() { return; }\n"
21316 "}\n"
21317 "} // namespace a",
21318 GNUBraceStyle);
21320 verifyFormat("void f()\n"
21321 "{\n"
21322 " if (true)\n"
21323 " {\n"
21324 " a();\n"
21325 " }\n"
21326 " else if (false)\n"
21327 " {\n"
21328 " b();\n"
21329 " }\n"
21330 " else\n"
21331 " {\n"
21332 " c();\n"
21333 " }\n"
21334 "}",
21335 GNUBraceStyle);
21337 verifyFormat("void f()\n"
21338 "{\n"
21339 " for (int i = 0; i < 10; ++i)\n"
21340 " {\n"
21341 " a();\n"
21342 " }\n"
21343 " while (false)\n"
21344 " {\n"
21345 " b();\n"
21346 " }\n"
21347 " do\n"
21348 " {\n"
21349 " c();\n"
21350 " }\n"
21351 " while (false);\n"
21352 "}",
21353 GNUBraceStyle);
21355 verifyFormat("void f(int a)\n"
21356 "{\n"
21357 " switch (a)\n"
21358 " {\n"
21359 " case 0:\n"
21360 " break;\n"
21361 " case 1:\n"
21362 " {\n"
21363 " break;\n"
21364 " }\n"
21365 " case 2:\n"
21366 " {\n"
21367 " }\n"
21368 " break;\n"
21369 " default:\n"
21370 " break;\n"
21371 " }\n"
21372 "}",
21373 GNUBraceStyle);
21375 verifyFormat("enum X\n"
21376 "{\n"
21377 " Y = 0,\n"
21378 "}",
21379 GNUBraceStyle);
21381 verifyFormat("@interface BSApplicationController ()\n"
21382 "{\n"
21383 "@private\n"
21384 " id _extraIvar;\n"
21385 "}\n"
21386 "@end",
21387 GNUBraceStyle);
21389 verifyFormat("#ifdef _DEBUG\n"
21390 "int foo(int i = 0)\n"
21391 "#else\n"
21392 "int foo(int i = 5)\n"
21393 "#endif\n"
21394 "{\n"
21395 " return i;\n"
21396 "}",
21397 GNUBraceStyle);
21399 verifyFormat("void foo() {}\n"
21400 "void bar()\n"
21401 "#ifdef _DEBUG\n"
21402 "{\n"
21403 " foo();\n"
21404 "}\n"
21405 "#else\n"
21406 "{\n"
21407 "}\n"
21408 "#endif",
21409 GNUBraceStyle);
21411 verifyFormat("void foobar() { int i = 5; }\n"
21412 "#ifdef _DEBUG\n"
21413 "void bar() {}\n"
21414 "#else\n"
21415 "void bar() { foobar(); }\n"
21416 "#endif",
21417 GNUBraceStyle);
21420 TEST_F(FormatTest, WebKitBraceBreaking) {
21421 FormatStyle WebKitBraceStyle = getLLVMStyle();
21422 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
21423 WebKitBraceStyle.FixNamespaceComments = false;
21424 verifyFormat("namespace a {\n"
21425 "class A {\n"
21426 " void f()\n"
21427 " {\n"
21428 " if (true) {\n"
21429 " a();\n"
21430 " b();\n"
21431 " }\n"
21432 " }\n"
21433 " void g() { return; }\n"
21434 "};\n"
21435 "enum E {\n"
21436 " A,\n"
21437 " // foo\n"
21438 " B,\n"
21439 " C\n"
21440 "};\n"
21441 "struct B {\n"
21442 " int x;\n"
21443 "};\n"
21444 "}",
21445 WebKitBraceStyle);
21446 verifyFormat("struct S {\n"
21447 " int Type;\n"
21448 " union {\n"
21449 " int x;\n"
21450 " double y;\n"
21451 " } Value;\n"
21452 " class C {\n"
21453 " MyFavoriteType Value;\n"
21454 " } Class;\n"
21455 "};",
21456 WebKitBraceStyle);
21459 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
21460 verifyFormat("void f() {\n"
21461 " try {\n"
21462 " } catch (const Exception &e) {\n"
21463 " }\n"
21464 "}");
21467 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
21468 auto Style = getLLVMStyle();
21469 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21470 verifyNoCrash("f({\n"
21471 "table({}, table({{\"\", false}}, {}))\n"
21472 "});",
21473 Style);
21475 Style.AlignConsecutiveAssignments.Enabled = true;
21476 Style.AlignConsecutiveDeclarations.Enabled = true;
21477 verifyFormat("struct test demo[] = {\n"
21478 " {56, 23, \"hello\"},\n"
21479 " {-1, 93463, \"world\"},\n"
21480 " { 7, 5, \"!!\"}\n"
21481 "};",
21482 Style);
21484 verifyFormat("struct test demo[] = {\n"
21485 " {56, 23, \"hello\"}, // first line\n"
21486 " {-1, 93463, \"world\"}, // second line\n"
21487 " { 7, 5, \"!!\"} // third line\n"
21488 "};",
21489 Style);
21491 verifyFormat("struct test demo[4] = {\n"
21492 " { 56, 23, 21, \"oh\"}, // first line\n"
21493 " { -1, 93463, 22, \"my\"}, // second line\n"
21494 " { 7, 5, 1, \"goodness\"} // third line\n"
21495 " {234, 5, 1, \"gracious\"} // fourth line\n"
21496 "};",
21497 Style);
21499 verifyFormat("struct test demo[3] = {\n"
21500 " {56, 23, \"hello\"},\n"
21501 " {-1, 93463, \"world\"},\n"
21502 " { 7, 5, \"!!\"}\n"
21503 "};",
21504 Style);
21506 verifyFormat("struct test demo[3] = {\n"
21507 " {int{56}, 23, \"hello\"},\n"
21508 " {int{-1}, 93463, \"world\"},\n"
21509 " { int{7}, 5, \"!!\"}\n"
21510 "};",
21511 Style);
21513 verifyFormat("struct test demo[] = {\n"
21514 " {56, 23, \"hello\"},\n"
21515 " {-1, 93463, \"world\"},\n"
21516 " { 7, 5, \"!!\"},\n"
21517 "};",
21518 Style);
21520 verifyFormat("test demo[] = {\n"
21521 " {56, 23, \"hello\"},\n"
21522 " {-1, 93463, \"world\"},\n"
21523 " { 7, 5, \"!!\"},\n"
21524 "};",
21525 Style);
21527 verifyFormat("demo = std::array<struct test, 3>{\n"
21528 " test{56, 23, \"hello\"},\n"
21529 " test{-1, 93463, \"world\"},\n"
21530 " test{ 7, 5, \"!!\"},\n"
21531 "};",
21532 Style);
21534 verifyFormat("test demo[] = {\n"
21535 " {56, 23, \"hello\"},\n"
21536 "#if X\n"
21537 " {-1, 93463, \"world\"},\n"
21538 "#endif\n"
21539 " { 7, 5, \"!!\"}\n"
21540 "};",
21541 Style);
21543 verifyFormat(
21544 "test demo[] = {\n"
21545 " { 7, 23,\n"
21546 " \"hello world i am a very long line that really, in any\"\n"
21547 " \"just world, ought to be split over multiple lines\"},\n"
21548 " {-1, 93463, \"world\"},\n"
21549 " {56, 5, \"!!\"}\n"
21550 "};",
21551 Style);
21553 verifyNoCrash("Foo f[] = {\n"
21554 " [0] = { 1, },\n"
21555 " [i] { 1, },\n"
21556 "};",
21557 Style);
21558 verifyNoCrash("Foo foo[] = {\n"
21559 " [0] = {1, 1},\n"
21560 " [1] { 1, 1, },\n"
21561 " [2] { 1, 1, },\n"
21562 "};",
21563 Style);
21564 verifyNoCrash("test arr[] = {\n"
21565 "#define FOO(i) {i, i},\n"
21566 "SOME_GENERATOR(FOO)\n"
21567 "{2, 2}\n"
21568 "};",
21569 Style);
21571 verifyFormat("return GradForUnaryCwise(g, {\n"
21572 " {{\"sign\"}, \"Sign\", "
21573 " {\"x\", \"dy\"}},\n"
21574 " { {\"dx\"}, \"Mul\", {\"dy\""
21575 ", \"sign\"}},\n"
21576 "});",
21577 Style);
21579 Style.Cpp11BracedListStyle = false;
21580 verifyFormat("struct test demo[] = {\n"
21581 " { 56, 23, \"hello\" },\n"
21582 " { -1, 93463, \"world\" },\n"
21583 " { 7, 5, \"!!\" }\n"
21584 "};",
21585 Style);
21586 Style.Cpp11BracedListStyle = true;
21588 Style.ColumnLimit = 0;
21589 verifyFormat(
21590 "test demo[] = {\n"
21591 " {56, 23, \"hello world i am a very long line that really, "
21592 "in any just world, ought to be split over multiple lines\"},\n"
21593 " {-1, 93463, "
21594 " \"world\"},\n"
21595 " { 7, 5, "
21596 " \"!!\"},\n"
21597 "};",
21598 "test demo[] = {{56, 23, \"hello world i am a very long line "
21599 "that really, in any just world, ought to be split over multiple "
21600 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21601 Style);
21603 Style.ColumnLimit = 80;
21604 verifyFormat("test demo[] = {\n"
21605 " {56, 23, /* a comment */ \"hello\"},\n"
21606 " {-1, 93463, \"world\"},\n"
21607 " { 7, 5, \"!!\"}\n"
21608 "};",
21609 Style);
21611 verifyFormat("test demo[] = {\n"
21612 " {56, 23, \"hello\"},\n"
21613 " {-1, 93463, \"world\" /* comment here */},\n"
21614 " { 7, 5, \"!!\"}\n"
21615 "};",
21616 Style);
21618 verifyFormat("test demo[] = {\n"
21619 " {56, /* a comment */ 23, \"hello\"},\n"
21620 " {-1, 93463, \"world\"},\n"
21621 " { 7, 5, \"!!\"}\n"
21622 "};",
21623 Style);
21625 Style.ColumnLimit = 20;
21626 verifyFormat("demo = std::array<\n"
21627 " struct test, 3>{\n"
21628 " test{\n"
21629 " 56, 23,\n"
21630 " \"hello \"\n"
21631 " \"world i \"\n"
21632 " \"am a very \"\n"
21633 " \"long line \"\n"
21634 " \"that \"\n"
21635 " \"really, \"\n"
21636 " \"in any \"\n"
21637 " \"just \"\n"
21638 " \"world, \"\n"
21639 " \"ought to \"\n"
21640 " \"be split \"\n"
21641 " \"over \"\n"
21642 " \"multiple \"\n"
21643 " \"lines\"},\n"
21644 " test{-1, 93463,\n"
21645 " \"world\"},\n"
21646 " test{ 7, 5,\n"
21647 " \"!!\" },\n"
21648 "};",
21649 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21650 "i am a very long line that really, in any just world, ought "
21651 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21652 "test{7, 5, \"!!\"},};",
21653 Style);
21654 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21655 Style = getLLVMStyleWithColumns(50);
21656 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21657 verifyFormat("static A x = {\n"
21658 " {{init1, init2, init3, init4},\n"
21659 " {init1, init2, init3, init4}}\n"
21660 "};",
21661 Style);
21662 // TODO: Fix the indentations below when this option is fully functional.
21663 #if 0
21664 verifyFormat("int a[][] = {\n"
21665 " {\n"
21666 " {0, 2}, //\n"
21667 " {1, 2} //\n"
21668 " }\n"
21669 "};",
21670 Style);
21671 #endif
21672 Style.ColumnLimit = 100;
21673 verifyFormat(
21674 "test demo[] = {\n"
21675 " {56, 23,\n"
21676 " \"hello world i am a very long line that really, in any just world"
21677 ", ought to be split over \"\n"
21678 " \"multiple lines\" },\n"
21679 " {-1, 93463, \"world\"},\n"
21680 " { 7, 5, \"!!\"},\n"
21681 "};",
21682 "test demo[] = {{56, 23, \"hello world i am a very long line "
21683 "that really, in any just world, ought to be split over multiple "
21684 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21685 Style);
21687 Style = getLLVMStyleWithColumns(50);
21688 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21689 verifyFormat("struct test demo[] = {\n"
21690 " {56, 23, \"hello\"},\n"
21691 " {-1, 93463, \"world\"},\n"
21692 " { 7, 5, \"!!\"}\n"
21693 "};\n"
21694 "static A x = {\n"
21695 " {{init1, init2, init3, init4},\n"
21696 " {init1, init2, init3, init4}}\n"
21697 "};",
21698 Style);
21699 Style.ColumnLimit = 100;
21700 Style.AlignConsecutiveAssignments.AcrossComments = true;
21701 Style.AlignConsecutiveDeclarations.AcrossComments = true;
21702 verifyFormat("struct test demo[] = {\n"
21703 " {56, 23, \"hello\"},\n"
21704 " {-1, 93463, \"world\"},\n"
21705 " { 7, 5, \"!!\"}\n"
21706 "};\n"
21707 "struct test demo[4] = {\n"
21708 " { 56, 23, 21, \"oh\"}, // first line\n"
21709 " { -1, 93463, 22, \"my\"}, // second line\n"
21710 " { 7, 5, 1, \"goodness\"} // third line\n"
21711 " {234, 5, 1, \"gracious\"} // fourth line\n"
21712 "};",
21713 Style);
21714 verifyFormat(
21715 "test demo[] = {\n"
21716 " {56,\n"
21717 " \"hello world i am a very long line that really, in any just world"
21718 ", ought to be split over \"\n"
21719 " \"multiple lines\", 23},\n"
21720 " {-1, \"world\", 93463},\n"
21721 " { 7, \"!!\", 5},\n"
21722 "};",
21723 "test demo[] = {{56, \"hello world i am a very long line "
21724 "that really, in any just world, ought to be split over multiple "
21725 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21726 Style);
21729 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
21730 auto Style = getLLVMStyle();
21731 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21732 /* FIXME: This case gets misformatted.
21733 verifyFormat("auto foo = Items{\n"
21734 " Section{0, bar(), },\n"
21735 " Section{1, boo() }\n"
21736 "};",
21737 Style);
21739 verifyFormat("auto foo = Items{\n"
21740 " Section{\n"
21741 " 0, bar(),\n"
21742 " }\n"
21743 "};",
21744 Style);
21745 verifyFormat("struct test demo[] = {\n"
21746 " {56, 23, \"hello\"},\n"
21747 " {-1, 93463, \"world\"},\n"
21748 " {7, 5, \"!!\" }\n"
21749 "};",
21750 Style);
21751 verifyFormat("struct test demo[] = {\n"
21752 " {56, 23, \"hello\"}, // first line\n"
21753 " {-1, 93463, \"world\"}, // second line\n"
21754 " {7, 5, \"!!\" } // third line\n"
21755 "};",
21756 Style);
21757 verifyFormat("struct test demo[4] = {\n"
21758 " {56, 23, 21, \"oh\" }, // first line\n"
21759 " {-1, 93463, 22, \"my\" }, // second line\n"
21760 " {7, 5, 1, \"goodness\"} // third line\n"
21761 " {234, 5, 1, \"gracious\"} // fourth line\n"
21762 "};",
21763 Style);
21764 verifyFormat("struct test demo[3] = {\n"
21765 " {56, 23, \"hello\"},\n"
21766 " {-1, 93463, \"world\"},\n"
21767 " {7, 5, \"!!\" }\n"
21768 "};",
21769 Style);
21771 verifyFormat("struct test demo[3] = {\n"
21772 " {int{56}, 23, \"hello\"},\n"
21773 " {int{-1}, 93463, \"world\"},\n"
21774 " {int{7}, 5, \"!!\" }\n"
21775 "};",
21776 Style);
21777 verifyFormat("struct test demo[] = {\n"
21778 " {56, 23, \"hello\"},\n"
21779 " {-1, 93463, \"world\"},\n"
21780 " {7, 5, \"!!\" },\n"
21781 "};",
21782 Style);
21783 verifyFormat("test demo[] = {\n"
21784 " {56, 23, \"hello\"},\n"
21785 " {-1, 93463, \"world\"},\n"
21786 " {7, 5, \"!!\" },\n"
21787 "};",
21788 Style);
21789 verifyFormat("demo = std::array<struct test, 3>{\n"
21790 " test{56, 23, \"hello\"},\n"
21791 " test{-1, 93463, \"world\"},\n"
21792 " test{7, 5, \"!!\" },\n"
21793 "};",
21794 Style);
21795 verifyFormat("test demo[] = {\n"
21796 " {56, 23, \"hello\"},\n"
21797 "#if X\n"
21798 " {-1, 93463, \"world\"},\n"
21799 "#endif\n"
21800 " {7, 5, \"!!\" }\n"
21801 "};",
21802 Style);
21803 verifyFormat(
21804 "test demo[] = {\n"
21805 " {7, 23,\n"
21806 " \"hello world i am a very long line that really, in any\"\n"
21807 " \"just world, ought to be split over multiple lines\"},\n"
21808 " {-1, 93463, \"world\" },\n"
21809 " {56, 5, \"!!\" }\n"
21810 "};",
21811 Style);
21813 verifyNoCrash("Foo f[] = {\n"
21814 " [0] = { 1, },\n"
21815 " [i] { 1, },\n"
21816 "};",
21817 Style);
21818 verifyNoCrash("Foo foo[] = {\n"
21819 " [0] = {1, 1},\n"
21820 " [1] { 1, 1, },\n"
21821 " [2] { 1, 1, },\n"
21822 "};",
21823 Style);
21824 verifyNoCrash("test arr[] = {\n"
21825 "#define FOO(i) {i, i},\n"
21826 "SOME_GENERATOR(FOO)\n"
21827 "{2, 2}\n"
21828 "};",
21829 Style);
21831 verifyFormat("return GradForUnaryCwise(g, {\n"
21832 " {{\"sign\"}, \"Sign\", {\"x\", "
21833 "\"dy\"} },\n"
21834 " {{\"dx\"}, \"Mul\", "
21835 "{\"dy\", \"sign\"}},\n"
21836 "});",
21837 Style);
21839 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
21840 verifyFormat("#define FOO \\\n"
21841 " int foo[][2] = { \\\n"
21842 " {0, 1} \\\n"
21843 " };",
21844 Style);
21846 Style.Cpp11BracedListStyle = false;
21847 verifyFormat("struct test demo[] = {\n"
21848 " { 56, 23, \"hello\" },\n"
21849 " { -1, 93463, \"world\" },\n"
21850 " { 7, 5, \"!!\" }\n"
21851 "};",
21852 Style);
21853 Style.Cpp11BracedListStyle = true;
21855 Style.ColumnLimit = 0;
21856 verifyFormat(
21857 "test demo[] = {\n"
21858 " {56, 23, \"hello world i am a very long line that really, in any "
21859 "just world, ought to be split over multiple lines\"},\n"
21860 " {-1, 93463, \"world\" "
21861 " },\n"
21862 " {7, 5, \"!!\" "
21863 " },\n"
21864 "};",
21865 "test demo[] = {{56, 23, \"hello world i am a very long line "
21866 "that really, in any just world, ought to be split over multiple "
21867 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21868 Style);
21870 Style.ColumnLimit = 80;
21871 verifyFormat("test demo[] = {\n"
21872 " {56, 23, /* a comment */ \"hello\"},\n"
21873 " {-1, 93463, \"world\" },\n"
21874 " {7, 5, \"!!\" }\n"
21875 "};",
21876 Style);
21878 verifyFormat("test demo[] = {\n"
21879 " {56, 23, \"hello\" },\n"
21880 " {-1, 93463, \"world\" /* comment here */},\n"
21881 " {7, 5, \"!!\" }\n"
21882 "};",
21883 Style);
21885 verifyFormat("test demo[] = {\n"
21886 " {56, /* a comment */ 23, \"hello\"},\n"
21887 " {-1, 93463, \"world\"},\n"
21888 " {7, 5, \"!!\" }\n"
21889 "};",
21890 Style);
21891 verifyFormat("Foo foo = {\n"
21892 " // comment\n"
21893 " {1, 2}\n"
21894 "};",
21895 Style);
21897 Style.ColumnLimit = 20;
21898 // FIXME: unstable test case
21899 EXPECT_EQ(
21900 "demo = std::array<\n"
21901 " struct test, 3>{\n"
21902 " test{\n"
21903 " 56, 23,\n"
21904 " \"hello \"\n"
21905 " \"world i \"\n"
21906 " \"am a very \"\n"
21907 " \"long line \"\n"
21908 " \"that \"\n"
21909 " \"really, \"\n"
21910 " \"in any \"\n"
21911 " \"just \"\n"
21912 " \"world, \"\n"
21913 " \"ought to \"\n"
21914 " \"be split \"\n"
21915 " \"over \"\n"
21916 " \"multiple \"\n"
21917 " \"lines\"},\n"
21918 " test{-1, 93463,\n"
21919 " \"world\"},\n"
21920 " test{7, 5,\n"
21921 " \"!!\" },\n"
21922 "};",
21923 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21924 "i am a very long line that really, in any just world, ought "
21925 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21926 "test{7, 5, \"!!\"},};",
21927 Style));
21929 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21930 Style = getLLVMStyleWithColumns(50);
21931 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21932 verifyFormat("static A x = {\n"
21933 " {{init1, init2, init3, init4},\n"
21934 " {init1, init2, init3, init4}}\n"
21935 "};",
21936 Style);
21937 Style.ColumnLimit = 100;
21938 verifyFormat(
21939 "test demo[] = {\n"
21940 " {56, 23,\n"
21941 " \"hello world i am a very long line that really, in any just world"
21942 ", ought to be split over \"\n"
21943 " \"multiple lines\" },\n"
21944 " {-1, 93463, \"world\"},\n"
21945 " {7, 5, \"!!\" },\n"
21946 "};",
21947 "test demo[] = {{56, 23, \"hello world i am a very long line "
21948 "that really, in any just world, ought to be split over multiple "
21949 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21950 Style);
21952 Style.ColumnLimit = 25;
21953 verifyNoCrash("Type foo{\n"
21954 " {\n"
21955 " 1, // A\n"
21956 " 2, // B\n"
21957 " 3, // C\n"
21958 " },\n"
21959 " \"hello\",\n"
21960 "};",
21961 Style);
21962 verifyNoCrash("Type object[X][Y] = {\n"
21963 " {{val}, {val}, {val}},\n"
21964 " {{val}, {val}, // some comment\n"
21965 " {val}}\n"
21966 "};",
21967 Style);
21969 Style.ColumnLimit = 120;
21970 verifyNoCrash(
21971 "T v[] {\n"
21972 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21973 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21974 "\"00000000000000000000000000000000000000000000000000000000"
21975 "00000000000000000000000000000000000000000000000000000000\" },\n"
21976 "};",
21977 Style);
21979 Style.SpacesInParens = FormatStyle::SIPO_Custom;
21980 Style.SpacesInParensOptions.Other = true;
21981 verifyFormat("Foo foo[] = {\n"
21982 " {1, 1},\n"
21983 " {1, 1},\n"
21984 "};",
21985 Style);
21988 TEST_F(FormatTest, UnderstandsPragmas) {
21989 verifyFormat("#pragma omp reduction(| : var)");
21990 verifyFormat("#pragma omp reduction(+ : var)");
21992 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21993 "(including parentheses).",
21994 "#pragma mark Any non-hyphenated or hyphenated string "
21995 "(including parentheses).");
21997 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21998 "(including parentheses).",
21999 "#pragma mark Any non-hyphenated or hyphenated string "
22000 "(including parentheses).");
22002 verifyFormat("#pragma comment(linker, \\\n"
22003 " \"argument\" \\\n"
22004 " \"argument\"",
22005 "#pragma comment(linker, \\\n"
22006 " \"argument\" \\\n"
22007 " \"argument\"",
22008 getStyleWithColumns(
22009 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32));
22012 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
22013 verifyFormat("#pragma omp target map(to : var)");
22014 verifyFormat("#pragma omp target map(to : var[ : N])");
22015 verifyFormat("#pragma omp target map(to : var[0 : N])");
22016 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22018 verifyFormat(
22019 "#pragma omp target \\\n"
22020 " reduction(+ : var) \\\n"
22021 " map(to : A[0 : N]) \\\n"
22022 " map(to : B[0 : N]) \\\n"
22023 " map(from : C[0 : N]) \\\n"
22024 " firstprivate(i) \\\n"
22025 " firstprivate(j) \\\n"
22026 " firstprivate(k)",
22027 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22028 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22029 getLLVMStyleWithColumns(26));
22032 TEST_F(FormatTest, UnderstandPragmaOption) {
22033 verifyFormat("#pragma option -C -A");
22035 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
22038 TEST_F(FormatTest, UnderstandPragmaRegion) {
22039 auto Style = getLLVMStyleWithColumns(0);
22040 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
22041 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
22044 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
22045 FormatStyle Style = getLLVMStyleWithColumns(20);
22047 // See PR41213
22048 verifyFormat("/*\n"
22049 " *\t9012345\n"
22050 " * /8901\n"
22051 " */",
22052 "/*\n"
22053 " *\t9012345 /8901\n"
22054 " */",
22055 Style);
22056 verifyFormat("/*\n"
22057 " *345678\n"
22058 " *\t/8901\n"
22059 " */",
22060 "/*\n"
22061 " *345678\t/8901\n"
22062 " */",
22063 Style);
22065 verifyFormat("int a; // the\n"
22066 " // comment",
22067 Style);
22068 verifyNoChange("int a; /* first line\n"
22069 " * second\n"
22070 " * line third\n"
22071 " * line\n"
22072 " */",
22073 Style);
22074 verifyFormat("int a; // first line\n"
22075 " // second\n"
22076 " // line third\n"
22077 " // line",
22078 "int a; // first line\n"
22079 " // second line\n"
22080 " // third line",
22081 Style);
22083 Style.PenaltyExcessCharacter = 90;
22084 verifyFormat("int a; // the comment", Style);
22085 verifyFormat("int a; // the comment\n"
22086 " // aaa",
22087 "int a; // the comment aaa", Style);
22088 verifyNoChange("int a; /* first line\n"
22089 " * second line\n"
22090 " * third line\n"
22091 " */",
22092 Style);
22093 verifyFormat("int a; // first line\n"
22094 " // second line\n"
22095 " // third line",
22096 Style);
22097 // FIXME: Investigate why this is not getting the same layout as the test
22098 // above.
22099 verifyFormat("int a; /* first line\n"
22100 " * second line\n"
22101 " * third line\n"
22102 " */",
22103 "int a; /* first line second line third line"
22104 "\n*/",
22105 Style);
22107 verifyFormat("// foo bar baz bazfoo\n"
22108 "// foo bar foo bar",
22109 "// foo bar baz bazfoo\n"
22110 "// foo bar foo bar",
22111 Style);
22112 verifyFormat("// foo bar baz bazfoo\n"
22113 "// foo bar foo bar",
22114 "// foo bar baz bazfoo\n"
22115 "// foo bar foo bar",
22116 Style);
22118 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22119 // next one.
22120 verifyFormat("// foo bar baz bazfoo\n"
22121 "// bar foo bar",
22122 "// foo bar baz bazfoo bar\n"
22123 "// foo bar",
22124 Style);
22126 // FIXME: unstable test case
22127 EXPECT_EQ("// foo bar baz bazfoo\n"
22128 "// foo bar baz bazfoo\n"
22129 "// bar foo bar",
22130 format("// foo bar baz bazfoo\n"
22131 "// foo bar baz bazfoo bar\n"
22132 "// foo bar",
22133 Style));
22135 // FIXME: unstable test case
22136 EXPECT_EQ("// foo bar baz bazfoo\n"
22137 "// foo bar baz bazfoo\n"
22138 "// bar foo bar",
22139 format("// foo bar baz bazfoo\n"
22140 "// foo bar baz bazfoo bar\n"
22141 "// foo bar",
22142 Style));
22144 // Make sure we do not keep protruding characters if strict mode reflow is
22145 // cheaper than keeping protruding characters.
22146 Style.ColumnLimit = 21;
22147 verifyFormat("// foo foo foo foo\n"
22148 "// foo foo foo foo\n"
22149 "// foo foo foo foo",
22150 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
22152 verifyFormat("int a = /* long block\n"
22153 " comment */\n"
22154 " 42;",
22155 "int a = /* long block comment */ 42;", Style);
22158 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
22159 FormatStyle Style = getLLVMStyle();
22160 Style.ColumnLimit = 8;
22161 Style.PenaltyExcessCharacter = 15;
22162 verifyFormat("int foo(\n"
22163 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22164 Style);
22165 Style.PenaltyBreakOpenParenthesis = 200;
22166 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22167 "int foo(\n"
22168 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22169 Style);
22172 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
22173 FormatStyle Style = getLLVMStyle();
22174 Style.ColumnLimit = 5;
22175 Style.PenaltyExcessCharacter = 150;
22176 verifyFormat("foo((\n"
22177 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22179 Style);
22180 Style.PenaltyBreakOpenParenthesis = 100'000;
22181 verifyFormat("foo((int)\n"
22182 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22183 "foo((\n"
22184 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22185 Style);
22188 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
22189 FormatStyle Style = getLLVMStyle();
22190 Style.ColumnLimit = 4;
22191 Style.PenaltyExcessCharacter = 100;
22192 verifyFormat("for (\n"
22193 " int iiiiiiiiiiiiiiiii =\n"
22194 " 0;\n"
22195 " iiiiiiiiiiiiiiiii <\n"
22196 " 2;\n"
22197 " iiiiiiiiiiiiiiiii++) {\n"
22198 "}",
22200 Style);
22201 Style.PenaltyBreakOpenParenthesis = 1250;
22202 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22203 " 0;\n"
22204 " iiiiiiiiiiiiiiiii <\n"
22205 " 2;\n"
22206 " iiiiiiiiiiiiiiiii++) {\n"
22207 "}",
22208 "for (\n"
22209 " int iiiiiiiiiiiiiiiii =\n"
22210 " 0;\n"
22211 " iiiiiiiiiiiiiiiii <\n"
22212 " 2;\n"
22213 " iiiiiiiiiiiiiiiii++) {\n"
22214 "}",
22215 Style);
22218 TEST_F(FormatTest, BreakPenaltyScopeResolution) {
22219 FormatStyle Style = getLLVMStyle();
22220 Style.ColumnLimit = 20;
22221 Style.PenaltyExcessCharacter = 100;
22222 verifyFormat("unsigned long\n"
22223 "foo::bar();",
22224 Style);
22225 Style.PenaltyBreakScopeResolution = 10;
22226 verifyFormat("unsigned long foo::\n"
22227 " bar();",
22228 Style);
22231 TEST_F(FormatTest, WorksFor8bitEncodings) {
22232 // FIXME: unstable test case
22233 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22234 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22235 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22236 "\"\xef\xee\xf0\xf3...\"",
22237 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22238 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22239 "\xef\xee\xf0\xf3...\"",
22240 getLLVMStyleWithColumns(12)));
22243 TEST_F(FormatTest, HandlesUTF8BOM) {
22244 verifyFormat("\xef\xbb\xbf");
22245 verifyFormat("\xef\xbb\xbf#include <iostream>");
22246 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22248 auto Style = getLLVMStyle();
22249 Style.KeepEmptyLines.AtStartOfFile = false;
22250 verifyFormat("\xef\xbb\xbf#include <iostream>",
22251 "\xef\xbb\xbf\n#include <iostream>", Style);
22254 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22255 #if !defined(_MSC_VER)
22257 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
22258 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22259 getLLVMStyleWithColumns(35));
22260 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22261 getLLVMStyleWithColumns(31));
22262 verifyFormat("// Однажды в студёную зимнюю пору...",
22263 getLLVMStyleWithColumns(36));
22264 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22265 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22266 getLLVMStyleWithColumns(39));
22267 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22268 getLLVMStyleWithColumns(35));
22271 TEST_F(FormatTest, SplitsUTF8Strings) {
22272 // Non-printable characters' width is currently considered to be the length in
22273 // bytes in UTF8. The characters can be displayed in very different manner
22274 // (zero-width, single width with a substitution glyph, expanded to their code
22275 // (e.g. "<8d>"), so there's no single correct way to handle them.
22276 // FIXME: unstable test case
22277 EXPECT_EQ("\"aaaaÄ\"\n"
22278 "\"\xc2\x8d\";",
22279 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22280 // FIXME: unstable test case
22281 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22282 "\"\xc2\x8d\";",
22283 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22284 // FIXME: unstable test case
22285 EXPECT_EQ("\"Однажды, в \"\n"
22286 "\"студёную \"\n"
22287 "\"зимнюю \"\n"
22288 "\"пору,\"",
22289 format("\"Однажды, в студёную зимнюю пору,\"",
22290 getLLVMStyleWithColumns(13)));
22291 // FIXME: unstable test case
22292 EXPECT_EQ(
22293 "\"一 二 三 \"\n"
22294 "\"四 五六 \"\n"
22295 "\"七 八 九 \"\n"
22296 "\"\"",
22297 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22298 // FIXME: unstable test case
22299 EXPECT_EQ("\"\t\"\n"
22300 "\" \t\"\n"
22301 "\"三 四 \"\n"
22302 "\"\t\"\n"
22303 "\" \t\"\n"
22304 "\" \"\n"
22305 "\"八九十\tqq\"",
22306 format("\"\t \t三 四 五\t \t七 八九十\tqq\"",
22307 getLLVMStyleWithColumns(11)));
22309 // UTF8 character in an escape sequence.
22310 // FIXME: unstable test case
22311 EXPECT_EQ("\"aaaaaa\"\n"
22312 "\"\\\xC2\x8D\"",
22313 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22316 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
22317 verifyFormat("const char *sssss =\n"
22318 " \"一二三四五六七八\\\n"
22319 " 九 十\";",
22320 "const char *sssss = \"一二三四五六七八\\\n"
22321 " 九 十\";",
22322 getLLVMStyleWithColumns(30));
22325 TEST_F(FormatTest, SplitsUTF8LineComments) {
22326 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22327 verifyFormat("// Я из лесу\n"
22328 "// вышел; был\n"
22329 "// сильный\n"
22330 "// мороз.",
22331 "// Я из лесу вышел; был сильный мороз.",
22332 getLLVMStyleWithColumns(13));
22333 verifyFormat("// 一二三\n"
22334 "// 四五六七\n"
22335 "// 八 九\n"
22336 "// 十",
22337 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22340 TEST_F(FormatTest, SplitsUTF8BlockComments) {
22341 verifyFormat("/* Гляжу,\n"
22342 " * поднимается\n"
22343 " * медленно в\n"
22344 " * гору\n"
22345 " * Лошадка,\n"
22346 " * везущая\n"
22347 " * хворосту\n"
22348 " * воз. */",
22349 "/* Гляжу, поднимается медленно в гору\n"
22350 " * Лошадка, везущая хворосту воз. */",
22351 getLLVMStyleWithColumns(13));
22352 verifyFormat("/* 一二三\n"
22353 " * 四五六七\n"
22354 " * 八 九\n"
22355 " * 十 */",
22356 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22357 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22358 " * 𝕓𝕪𝕥𝕖\n"
22359 " * 𝖀𝕿𝕱-𝟠 */",
22360 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22363 #endif // _MSC_VER
22365 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22366 FormatStyle Style = getLLVMStyle();
22368 Style.ConstructorInitializerIndentWidth = 4;
22369 verifyFormat(
22370 "SomeClass::Constructor()\n"
22371 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22372 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22373 Style);
22375 Style.ConstructorInitializerIndentWidth = 2;
22376 verifyFormat(
22377 "SomeClass::Constructor()\n"
22378 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22379 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22380 Style);
22382 Style.ConstructorInitializerIndentWidth = 0;
22383 verifyFormat(
22384 "SomeClass::Constructor()\n"
22385 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22386 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22387 Style);
22388 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
22389 verifyFormat(
22390 "SomeLongTemplateVariableName<\n"
22391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22392 Style);
22393 verifyFormat("bool smaller = 1 < "
22394 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22396 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22397 Style);
22399 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22400 verifyFormat("SomeClass::Constructor() :\n"
22401 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22402 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22403 Style);
22406 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
22407 FormatStyle Style = getLLVMStyle();
22408 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22409 Style.ConstructorInitializerIndentWidth = 4;
22410 verifyFormat("SomeClass::Constructor()\n"
22411 " : a(a)\n"
22412 " , b(b)\n"
22413 " , c(c) {}",
22414 Style);
22415 verifyFormat("SomeClass::Constructor()\n"
22416 " : a(a) {}",
22417 Style);
22419 Style.ColumnLimit = 0;
22420 verifyFormat("SomeClass::Constructor()\n"
22421 " : a(a) {}",
22422 Style);
22423 verifyFormat("SomeClass::Constructor() noexcept\n"
22424 " : a(a) {}",
22425 Style);
22426 verifyFormat("SomeClass::Constructor()\n"
22427 " : a(a)\n"
22428 " , b(b)\n"
22429 " , c(c) {}",
22430 Style);
22431 verifyFormat("SomeClass::Constructor()\n"
22432 " : a(a) {\n"
22433 " foo();\n"
22434 " bar();\n"
22435 "}",
22436 Style);
22438 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22439 verifyFormat("SomeClass::Constructor()\n"
22440 " : a(a)\n"
22441 " , b(b)\n"
22442 " , c(c) {\n}",
22443 Style);
22444 verifyFormat("SomeClass::Constructor()\n"
22445 " : a(a) {\n}",
22446 Style);
22448 Style.ColumnLimit = 80;
22449 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
22450 Style.ConstructorInitializerIndentWidth = 2;
22451 verifyFormat("SomeClass::Constructor()\n"
22452 " : a(a)\n"
22453 " , b(b)\n"
22454 " , c(c) {}",
22455 Style);
22457 Style.ConstructorInitializerIndentWidth = 0;
22458 verifyFormat("SomeClass::Constructor()\n"
22459 ": a(a)\n"
22460 ", b(b)\n"
22461 ", c(c) {}",
22462 Style);
22464 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22465 Style.ConstructorInitializerIndentWidth = 4;
22466 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
22467 verifyFormat(
22468 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22469 Style);
22470 verifyFormat(
22471 "SomeClass::Constructor()\n"
22472 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22473 Style);
22474 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22475 verifyFormat("SomeClass::Constructor()\n"
22476 " : aaaaaaaa(aaaaaaaa) {}",
22477 Style);
22478 verifyFormat("SomeClass::Constructor()\n"
22479 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22480 Style);
22481 verifyFormat(
22482 "SomeClass::Constructor()\n"
22483 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22484 Style);
22486 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22487 Style.ConstructorInitializerIndentWidth = 4;
22488 Style.ColumnLimit = 60;
22489 verifyFormat("SomeClass::Constructor()\n"
22490 " : aaaaaaaa(aaaaaaaa)\n"
22491 " , aaaaaaaa(aaaaaaaa)\n"
22492 " , aaaaaaaa(aaaaaaaa) {}",
22493 Style);
22494 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22495 verifyFormat("SomeClass::Constructor()\n"
22496 " : aaaaaaaa(aaaaaaaa)\n"
22497 " , aaaaaaaa(aaaaaaaa)\n"
22498 " , aaaaaaaa(aaaaaaaa) {}",
22499 Style);
22502 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
22503 FormatStyle Style = getLLVMStyle();
22504 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22505 Style.ConstructorInitializerIndentWidth = 4;
22506 verifyFormat("SomeClass::Constructor()\n"
22507 " : a{a}\n"
22508 " , b{b} {}",
22509 Style);
22510 verifyFormat("SomeClass::Constructor()\n"
22511 " : a{a}\n"
22512 "#if CONDITION\n"
22513 " , b{b}\n"
22514 "#endif\n"
22515 "{\n}",
22516 Style);
22517 Style.ConstructorInitializerIndentWidth = 2;
22518 verifyFormat("SomeClass::Constructor()\n"
22519 "#if CONDITION\n"
22520 " : a{a}\n"
22521 "#endif\n"
22522 " , b{b}\n"
22523 " , c{c} {\n}",
22524 Style);
22525 Style.ConstructorInitializerIndentWidth = 0;
22526 verifyFormat("SomeClass::Constructor()\n"
22527 ": a{a}\n"
22528 "#ifdef CONDITION\n"
22529 ", b{b}\n"
22530 "#else\n"
22531 ", c{c}\n"
22532 "#endif\n"
22533 ", d{d} {\n}",
22534 Style);
22535 Style.ConstructorInitializerIndentWidth = 4;
22536 verifyFormat("SomeClass::Constructor()\n"
22537 " : a{a}\n"
22538 "#if WINDOWS\n"
22539 "#if DEBUG\n"
22540 " , b{0}\n"
22541 "#else\n"
22542 " , b{1}\n"
22543 "#endif\n"
22544 "#else\n"
22545 "#if DEBUG\n"
22546 " , b{2}\n"
22547 "#else\n"
22548 " , b{3}\n"
22549 "#endif\n"
22550 "#endif\n"
22551 "{\n}",
22552 Style);
22553 verifyFormat("SomeClass::Constructor()\n"
22554 " : a{a}\n"
22555 "#if WINDOWS\n"
22556 " , b{0}\n"
22557 "#if DEBUG\n"
22558 " , c{0}\n"
22559 "#else\n"
22560 " , c{1}\n"
22561 "#endif\n"
22562 "#else\n"
22563 "#if DEBUG\n"
22564 " , c{2}\n"
22565 "#else\n"
22566 " , c{3}\n"
22567 "#endif\n"
22568 " , b{1}\n"
22569 "#endif\n"
22570 "{\n}",
22571 Style);
22574 TEST_F(FormatTest, Destructors) {
22575 verifyFormat("void F(int &i) { i.~int(); }");
22576 verifyFormat("void F(int &i) { i->~int(); }");
22579 TEST_F(FormatTest, FormatsWithWebKitStyle) {
22580 FormatStyle Style = getWebKitStyle();
22582 // Don't indent in outer namespaces.
22583 verifyFormat("namespace outer {\n"
22584 "int i;\n"
22585 "namespace inner {\n"
22586 " int i;\n"
22587 "} // namespace inner\n"
22588 "} // namespace outer\n"
22589 "namespace other_outer {\n"
22590 "int i;\n"
22591 "}",
22592 Style);
22594 // Don't indent case labels.
22595 verifyFormat("switch (variable) {\n"
22596 "case 1:\n"
22597 "case 2:\n"
22598 " doSomething();\n"
22599 " break;\n"
22600 "default:\n"
22601 " ++variable;\n"
22602 "}",
22603 Style);
22605 // Wrap before binary operators.
22606 verifyFormat(
22607 "void f()\n"
22608 "{\n"
22609 " if (aaaaaaaaaaaaaaaa\n"
22610 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22611 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22612 " return;\n"
22613 "}",
22614 "void f() {\n"
22615 "if (aaaaaaaaaaaaaaaa\n"
22616 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22617 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22618 "return;\n"
22619 "}",
22620 Style);
22622 // Allow functions on a single line.
22623 verifyFormat("void f() { return; }", Style);
22625 // Allow empty blocks on a single line and insert a space in empty blocks.
22626 verifyFormat("void f() { }", "void f() {}", Style);
22627 verifyFormat("while (true) { }", "while (true) {}", Style);
22628 // However, don't merge non-empty short loops.
22629 verifyFormat("while (true) {\n"
22630 " continue;\n"
22631 "}",
22632 "while (true) { continue; }", Style);
22634 // Constructor initializers are formatted one per line with the "," on the
22635 // new line.
22636 verifyFormat("Constructor()\n"
22637 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22638 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22639 " aaaaaaaaaaaaaa)\n"
22640 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22641 "{\n"
22642 "}",
22643 Style);
22644 verifyFormat("SomeClass::Constructor()\n"
22645 " : a(a)\n"
22646 "{\n"
22647 "}",
22648 Style);
22649 verifyFormat("SomeClass::Constructor()\n"
22650 " : a(a)\n"
22651 "{\n"
22652 "}",
22653 "SomeClass::Constructor():a(a){}", Style);
22654 verifyFormat("SomeClass::Constructor()\n"
22655 " : a(a)\n"
22656 " , b(b)\n"
22657 " , c(c)\n"
22658 "{\n"
22659 "}",
22660 Style);
22661 verifyFormat("SomeClass::Constructor()\n"
22662 " : a(a)\n"
22663 "{\n"
22664 " foo();\n"
22665 " bar();\n"
22666 "}",
22667 Style);
22669 // Access specifiers should be aligned left.
22670 verifyFormat("class C {\n"
22671 "public:\n"
22672 " int i;\n"
22673 "};",
22674 Style);
22676 // Do not align comments.
22677 verifyFormat("int a; // Do not\n"
22678 "double b; // align comments.",
22679 Style);
22681 // Do not align operands.
22682 verifyFormat("ASSERT(aaaa\n"
22683 " || bbbb);",
22684 "ASSERT ( aaaa\n||bbbb);", Style);
22686 // Accept input's line breaks.
22687 verifyFormat("if (aaaaaaaaaaaaaaa\n"
22688 " || bbbbbbbbbbbbbbb) {\n"
22689 " i++;\n"
22690 "}",
22691 "if (aaaaaaaaaaaaaaa\n"
22692 "|| bbbbbbbbbbbbbbb) { i++; }",
22693 Style);
22694 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22695 " i++;\n"
22696 "}",
22697 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
22699 // Don't automatically break all macro definitions (llvm.org/PR17842).
22700 verifyFormat("#define aNumber 10", Style);
22701 // However, generally keep the line breaks that the user authored.
22702 verifyFormat("#define aNumber \\\n"
22703 " 10",
22704 "#define aNumber \\\n"
22705 " 10",
22706 Style);
22708 // Keep empty and one-element array literals on a single line.
22709 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22710 " copyItems:YES];",
22711 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22712 "copyItems:YES];",
22713 Style);
22714 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22715 " copyItems:YES];",
22716 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22717 " copyItems:YES];",
22718 Style);
22719 // FIXME: This does not seem right, there should be more indentation before
22720 // the array literal's entries. Nested blocks have the same problem.
22721 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22722 " @\"a\",\n"
22723 " @\"a\"\n"
22724 "]\n"
22725 " copyItems:YES];",
22726 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22727 " @\"a\",\n"
22728 " @\"a\"\n"
22729 " ]\n"
22730 " copyItems:YES];",
22731 Style);
22732 verifyFormat(
22733 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22734 " copyItems:YES];",
22735 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22736 " copyItems:YES];",
22737 Style);
22739 verifyFormat("[self.a b:c c:d];", Style);
22740 verifyFormat("[self.a b:c\n"
22741 " c:d];",
22742 "[self.a b:c\n"
22743 "c:d];",
22744 Style);
22747 TEST_F(FormatTest, FormatsLambdas) {
22748 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22749 verifyFormat(
22750 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22751 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22752 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22753 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22754 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22755 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22756 verifyFormat("auto c = [a = [b = 42] {}] {};");
22757 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22758 verifyFormat("int x = f(*+[] {});");
22759 verifyFormat("void f() {\n"
22760 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22761 "}");
22762 verifyFormat("void f() {\n"
22763 " other(x.begin(), //\n"
22764 " x.end(), //\n"
22765 " [&](int, int) { return 1; });\n"
22766 "}");
22767 verifyFormat("void f() {\n"
22768 " other.other.other.other.other(\n"
22769 " x.begin(), x.end(),\n"
22770 " [something, rather](int, int, int, int, int, int, int) { "
22771 "return 1; });\n"
22772 "}");
22773 verifyFormat(
22774 "void f() {\n"
22775 " other.other.other.other.other(\n"
22776 " x.begin(), x.end(),\n"
22777 " [something, rather](int, int, int, int, int, int, int) {\n"
22778 " //\n"
22779 " });\n"
22780 "}");
22781 verifyFormat("SomeFunction([]() { // A cool function...\n"
22782 " return 43;\n"
22783 "});");
22784 verifyFormat("SomeFunction([]() {\n"
22785 "#define A a\n"
22786 " return 43;\n"
22787 "});",
22788 "SomeFunction([](){\n"
22789 "#define A a\n"
22790 "return 43;\n"
22791 "});");
22792 verifyFormat("void f() {\n"
22793 " SomeFunction([](decltype(x), A *a) {});\n"
22794 " SomeFunction([](typeof(x), A *a) {});\n"
22795 " SomeFunction([](_Atomic(x), A *a) {});\n"
22796 " SomeFunction([](__underlying_type(x), A *a) {});\n"
22797 "}");
22798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22799 " [](const aaaaaaaaaa &a) { return a; });");
22800 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22801 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
22802 "});");
22803 verifyFormat("Constructor()\n"
22804 " : Field([] { // comment\n"
22805 " int i;\n"
22806 " }) {}");
22807 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22808 " return some_parameter.size();\n"
22809 "};");
22810 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22811 " [](const string &s) { return s; };");
22812 verifyFormat("int i = aaaaaa ? 1 //\n"
22813 " : [] {\n"
22814 " return 2; //\n"
22815 " }();");
22816 verifyFormat("llvm::errs() << \"number of twos is \"\n"
22817 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
22818 " return x == 2; // force break\n"
22819 " });");
22820 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22821 " [=](int iiiiiiiiiiii) {\n"
22822 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22823 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
22824 " });",
22825 getLLVMStyleWithColumns(60));
22827 verifyFormat("SomeFunction({[&] {\n"
22828 " // comment\n"
22829 " },\n"
22830 " [&] {\n"
22831 " // comment\n"
22832 " }});");
22833 verifyFormat("SomeFunction({[&] {\n"
22834 " // comment\n"
22835 "}});");
22836 verifyFormat(
22837 "virtual aaaaaaaaaaaaaaaa(\n"
22838 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22839 " aaaaa aaaaaaaaa);");
22841 // Lambdas with return types.
22842 verifyFormat("int c = []() -> int { return 2; }();");
22843 verifyFormat("int c = []() -> int * { return 2; }();");
22844 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22845 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22846 verifyFormat("foo([]() noexcept -> int {});");
22847 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22848 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22849 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22850 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22851 verifyFormat("[a, a]() -> a<1> {};");
22852 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22853 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22854 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22855 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22856 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22857 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22858 verifyFormat("[]() -> foo<!5> { return {}; };");
22859 verifyFormat("[]() -> foo<~5> { return {}; };");
22860 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22861 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22862 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22863 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22864 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22865 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22866 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22867 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22868 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22869 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22870 verifyFormat("namespace bar {\n"
22871 "// broken:\n"
22872 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22873 "} // namespace bar");
22874 verifyFormat("namespace bar {\n"
22875 "// broken:\n"
22876 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22877 "} // namespace bar");
22878 verifyFormat("namespace bar {\n"
22879 "// broken:\n"
22880 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22881 "} // namespace bar");
22882 verifyFormat("namespace bar {\n"
22883 "// broken:\n"
22884 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22885 "} // namespace bar");
22886 verifyFormat("namespace bar {\n"
22887 "// broken:\n"
22888 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22889 "} // namespace bar");
22890 verifyFormat("namespace bar {\n"
22891 "// broken:\n"
22892 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22893 "} // namespace bar");
22894 verifyFormat("namespace bar {\n"
22895 "// broken:\n"
22896 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22897 "} // namespace bar");
22898 verifyFormat("namespace bar {\n"
22899 "// broken:\n"
22900 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22901 "} // namespace bar");
22902 verifyFormat("namespace bar {\n"
22903 "// broken:\n"
22904 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22905 "} // namespace bar");
22906 verifyFormat("namespace bar {\n"
22907 "// broken:\n"
22908 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22909 "} // namespace bar");
22910 verifyFormat("namespace bar {\n"
22911 "// broken:\n"
22912 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22913 "} // namespace bar");
22914 verifyFormat("namespace bar {\n"
22915 "// broken:\n"
22916 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22917 "} // namespace bar");
22918 verifyFormat("namespace bar {\n"
22919 "// broken:\n"
22920 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22921 "} // namespace bar");
22922 verifyFormat("namespace bar {\n"
22923 "// broken:\n"
22924 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22925 "} // namespace bar");
22926 verifyFormat("namespace bar {\n"
22927 "// broken:\n"
22928 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22929 "} // namespace bar");
22930 verifyFormat("namespace bar {\n"
22931 "// broken:\n"
22932 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22933 "} // namespace bar");
22934 verifyFormat("namespace bar {\n"
22935 "// broken:\n"
22936 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22937 "} // namespace bar");
22938 verifyFormat("namespace bar {\n"
22939 "// broken:\n"
22940 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22941 "} // namespace bar");
22942 verifyFormat("[]() -> a<1> {};");
22943 verifyFormat("[]() -> a<1> { ; };");
22944 verifyFormat("[]() -> a<1> { ; }();");
22945 verifyFormat("[a, a]() -> a<true> {};");
22946 verifyFormat("[]() -> a<true> {};");
22947 verifyFormat("[]() -> a<true> { ; };");
22948 verifyFormat("[]() -> a<true> { ; }();");
22949 verifyFormat("[a, a]() -> a<false> {};");
22950 verifyFormat("[]() -> a<false> {};");
22951 verifyFormat("[]() -> a<false> { ; };");
22952 verifyFormat("[]() -> a<false> { ; }();");
22953 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22954 verifyFormat("namespace bar {\n"
22955 "auto foo{[]() -> foo<false> { ; }};\n"
22956 "} // namespace bar");
22957 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22958 " int j) -> int {\n"
22959 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22960 "};");
22961 verifyFormat(
22962 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22963 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22964 " return aaaaaaaaaaaaaaaaa;\n"
22965 " });",
22966 getLLVMStyleWithColumns(70));
22967 verifyFormat("[]() //\n"
22968 " -> int {\n"
22969 " return 1; //\n"
22970 "};");
22971 verifyFormat("[]() -> Void<T...> {};");
22972 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22973 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22974 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22975 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22976 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22977 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
22978 verifyFormat("return int{[x = x]() { return x; }()};");
22980 // Lambdas with explicit template argument lists.
22981 verifyFormat(
22982 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22983 verifyFormat("auto L = []<class T>(T) {\n"
22984 " {\n"
22985 " f();\n"
22986 " g();\n"
22987 " }\n"
22988 "};");
22989 verifyFormat("auto L = []<class... T>(T...) {\n"
22990 " {\n"
22991 " f();\n"
22992 " g();\n"
22993 " }\n"
22994 "};");
22995 verifyFormat("auto L = []<typename... T>(T...) {\n"
22996 " {\n"
22997 " f();\n"
22998 " g();\n"
22999 " }\n"
23000 "};");
23001 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23002 " {\n"
23003 " f();\n"
23004 " g();\n"
23005 " }\n"
23006 "};");
23007 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23008 " {\n"
23009 " f();\n"
23010 " g();\n"
23011 " }\n"
23012 "};");
23013 verifyFormat("auto L = []<int... T>(T...) {\n"
23014 " {\n"
23015 " f();\n"
23016 " g();\n"
23017 " }\n"
23018 "};");
23019 verifyFormat("auto L = []<Foo... T>(T...) {\n"
23020 " {\n"
23021 " f();\n"
23022 " g();\n"
23023 " }\n"
23024 "};");
23026 // Lambdas that fit on a single line within an argument list are not forced
23027 // onto new lines.
23028 verifyFormat("SomeFunction([] {});");
23029 verifyFormat("SomeFunction(0, [] {});");
23030 verifyFormat("SomeFunction([] {}, 0);");
23031 verifyFormat("SomeFunction(0, [] {}, 0);");
23032 verifyFormat("SomeFunction([] { return 0; }, 0);");
23033 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23034 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23035 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23036 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23037 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
23038 // Exceeded column limit. We need to break.
23039 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23040 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
23041 "return 0; }, b);");
23043 // Multiple multi-line lambdas in the same parentheses change indentation
23044 // rules. These lambdas are always forced to start on new lines.
23045 verifyFormat("SomeFunction(\n"
23046 " []() {\n"
23047 " //\n"
23048 " },\n"
23049 " []() {\n"
23050 " //\n"
23051 " });");
23053 // A multi-line lambda passed as arg0 is always pushed to the next line.
23054 verifyFormat("SomeFunction(\n"
23055 " [this] {\n"
23056 " //\n"
23057 " },\n"
23058 " 1);");
23060 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23061 // the arg0 case above.
23062 auto Style = getGoogleStyle();
23063 Style.BinPackArguments = false;
23064 verifyFormat("SomeFunction(\n"
23065 " a,\n"
23066 " [this] {\n"
23067 " //\n"
23068 " },\n"
23069 " b);",
23070 Style);
23071 verifyFormat("SomeFunction(\n"
23072 " a,\n"
23073 " [this] {\n"
23074 " //\n"
23075 " },\n"
23076 " b);");
23078 // A lambda with a very long line forces arg0 to be pushed out irrespective of
23079 // the BinPackArguments value (as long as the code is wide enough).
23080 verifyFormat(
23081 "something->SomeFunction(\n"
23082 " a,\n"
23083 " [this] {\n"
23085 "D0000000000000000000000000000000000000000000000000000000000001();\n"
23086 " },\n"
23087 " b);");
23089 // A multi-line lambda is pulled up as long as the introducer fits on the
23090 // previous line and there are no further args.
23091 verifyFormat("function(1, [this, that] {\n"
23092 " //\n"
23093 "});");
23094 verifyFormat("function([this, that] {\n"
23095 " //\n"
23096 "});");
23097 // FIXME: this format is not ideal and we should consider forcing the first
23098 // arg onto its own line.
23099 verifyFormat("function(a, b, c, //\n"
23100 " d, [this, that] {\n"
23101 " //\n"
23102 " });");
23104 // Multiple lambdas are treated correctly even when there is a short arg0.
23105 verifyFormat("SomeFunction(\n"
23106 " 1,\n"
23107 " [this] {\n"
23108 " //\n"
23109 " },\n"
23110 " [this] {\n"
23111 " //\n"
23112 " },\n"
23113 " 1);");
23115 // More complex introducers.
23116 verifyFormat("return [i, args...] {};");
23118 // Not lambdas.
23119 verifyFormat("constexpr char hello[]{\"hello\"};");
23120 verifyFormat("double &operator[](int i) { return 0; }\n"
23121 "int i;");
23122 verifyFormat("std::unique_ptr<int[]> foo() {}");
23123 verifyFormat("int i = a[a][a]->f();");
23124 verifyFormat("int i = (*b)[a]->f();");
23126 // Other corner cases.
23127 verifyFormat("void f() {\n"
23128 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23129 " );\n"
23130 "}");
23131 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23133 // Lambdas created through weird macros.
23134 verifyFormat("void f() {\n"
23135 " MACRO((const AA &a) { return 1; });\n"
23136 " MACRO((AA &a) { return 1; });\n"
23137 "}");
23139 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23140 " doo_dah();\n"
23141 " doo_dah();\n"
23142 " })) {\n"
23143 "}");
23144 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23145 " doo_dah();\n"
23146 " doo_dah();\n"
23147 " })) {\n"
23148 "}");
23149 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23150 " doo_dah();\n"
23151 " doo_dah();\n"
23152 " })) {\n"
23153 "}");
23154 verifyFormat("auto lambda = []() {\n"
23155 " int a = 2\n"
23156 "#if A\n"
23157 " + 2\n"
23158 "#endif\n"
23159 " ;\n"
23160 "};");
23162 // Lambdas with complex multiline introducers.
23163 verifyFormat(
23164 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23165 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23166 " -> ::std::unordered_set<\n"
23167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23168 " //\n"
23169 " });");
23171 FormatStyle LLVMStyle = getLLVMStyleWithColumns(60);
23172 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23173 " [](auto n) noexcept [[back_attr]]\n"
23174 " -> std::unordered_map<very_long_type_name_A,\n"
23175 " very_long_type_name_B> {\n"
23176 " really_do_something();\n"
23177 " });",
23178 LLVMStyle);
23179 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23180 " [](auto n) constexpr\n"
23181 " -> std::unordered_map<very_long_type_name_A,\n"
23182 " very_long_type_name_B> {\n"
23183 " really_do_something();\n"
23184 " });",
23185 LLVMStyle);
23187 FormatStyle DoNotMerge = getLLVMStyle();
23188 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
23189 verifyFormat("auto c = []() {\n"
23190 " return b;\n"
23191 "};",
23192 "auto c = []() { return b; };", DoNotMerge);
23193 verifyFormat("auto c = []() {\n"
23194 "};",
23195 " auto c = []() {};", DoNotMerge);
23197 FormatStyle MergeEmptyOnly = getLLVMStyle();
23198 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
23199 verifyFormat("auto c = []() {\n"
23200 " return b;\n"
23201 "};",
23202 "auto c = []() {\n"
23203 " return b;\n"
23204 " };",
23205 MergeEmptyOnly);
23206 verifyFormat("auto c = []() {};",
23207 "auto c = []() {\n"
23208 "};",
23209 MergeEmptyOnly);
23211 FormatStyle MergeInline = getLLVMStyle();
23212 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
23213 verifyFormat("auto c = []() {\n"
23214 " return b;\n"
23215 "};",
23216 "auto c = []() { return b; };", MergeInline);
23217 verifyFormat("function([]() { return b; })", MergeInline);
23218 verifyFormat("function([]() { return b; }, a)", MergeInline);
23219 verifyFormat("function(a, []() { return b; })", MergeInline);
23221 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23222 // AllowShortLambdasOnASingleLine
23223 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23224 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23225 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23226 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23227 FormatStyle::ShortLambdaStyle::SLS_None;
23228 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23229 " []()\n"
23230 " {\n"
23231 " return 17;\n"
23232 " });",
23233 LLVMWithBeforeLambdaBody);
23234 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23235 " []()\n"
23236 " {\n"
23237 " });",
23238 LLVMWithBeforeLambdaBody);
23239 verifyFormat("auto fct_SLS_None = []()\n"
23240 "{\n"
23241 " return 17;\n"
23242 "};",
23243 LLVMWithBeforeLambdaBody);
23244 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23245 " []()\n"
23246 " {\n"
23247 " return Call(\n"
23248 " []()\n"
23249 " {\n"
23250 " return 17;\n"
23251 " });\n"
23252 " });",
23253 LLVMWithBeforeLambdaBody);
23254 verifyFormat("void Fct() {\n"
23255 " return {[]()\n"
23256 " {\n"
23257 " return 17;\n"
23258 " }};\n"
23259 "}",
23260 LLVMWithBeforeLambdaBody);
23262 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23263 FormatStyle::ShortLambdaStyle::SLS_Empty;
23264 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23265 " []()\n"
23266 " {\n"
23267 " return 17;\n"
23268 " });",
23269 LLVMWithBeforeLambdaBody);
23270 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23271 LLVMWithBeforeLambdaBody);
23272 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23273 "ongFunctionName_SLS_Empty(\n"
23274 " []() {});",
23275 LLVMWithBeforeLambdaBody);
23276 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23277 " []()\n"
23278 " {\n"
23279 " return 17;\n"
23280 " });",
23281 LLVMWithBeforeLambdaBody);
23282 verifyFormat("auto fct_SLS_Empty = []()\n"
23283 "{\n"
23284 " return 17;\n"
23285 "};",
23286 LLVMWithBeforeLambdaBody);
23287 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23288 " []()\n"
23289 " {\n"
23290 " return Call([]() {});\n"
23291 " });",
23292 LLVMWithBeforeLambdaBody);
23293 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23294 " []()\n"
23295 " {\n"
23296 " return Call([]() {});\n"
23297 " });",
23298 LLVMWithBeforeLambdaBody);
23299 verifyFormat(
23300 "FctWithLongLineInLambda_SLS_Empty(\n"
23301 " []()\n"
23302 " {\n"
23303 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23304 " AndShouldNotBeConsiderAsInline,\n"
23305 " LambdaBodyMustBeBreak);\n"
23306 " });",
23307 LLVMWithBeforeLambdaBody);
23309 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23310 FormatStyle::ShortLambdaStyle::SLS_Inline;
23311 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23312 LLVMWithBeforeLambdaBody);
23313 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23314 LLVMWithBeforeLambdaBody);
23315 verifyFormat("auto fct_SLS_Inline = []()\n"
23316 "{\n"
23317 " return 17;\n"
23318 "};",
23319 LLVMWithBeforeLambdaBody);
23320 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23321 "17; }); });",
23322 LLVMWithBeforeLambdaBody);
23323 verifyFormat(
23324 "FctWithLongLineInLambda_SLS_Inline(\n"
23325 " []()\n"
23326 " {\n"
23327 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23328 " AndShouldNotBeConsiderAsInline,\n"
23329 " LambdaBodyMustBeBreak);\n"
23330 " });",
23331 LLVMWithBeforeLambdaBody);
23332 verifyFormat("FctWithMultipleParams_SLS_Inline("
23333 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23334 " []() { return 17; });",
23335 LLVMWithBeforeLambdaBody);
23336 verifyFormat(
23337 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23338 LLVMWithBeforeLambdaBody);
23340 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23341 FormatStyle::ShortLambdaStyle::SLS_All;
23342 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23343 LLVMWithBeforeLambdaBody);
23344 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23345 LLVMWithBeforeLambdaBody);
23346 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23347 LLVMWithBeforeLambdaBody);
23348 verifyFormat("FctWithOneParam_SLS_All(\n"
23349 " []()\n"
23350 " {\n"
23351 " // A cool function...\n"
23352 " return 43;\n"
23353 " });",
23354 LLVMWithBeforeLambdaBody);
23355 verifyFormat("FctWithMultipleParams_SLS_All("
23356 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23357 " []() { return 17; });",
23358 LLVMWithBeforeLambdaBody);
23359 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23360 LLVMWithBeforeLambdaBody);
23361 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23362 LLVMWithBeforeLambdaBody);
23363 verifyFormat(
23364 "FctWithLongLineInLambda_SLS_All(\n"
23365 " []()\n"
23366 " {\n"
23367 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23368 " AndShouldNotBeConsiderAsInline,\n"
23369 " LambdaBodyMustBeBreak);\n"
23370 " });",
23371 LLVMWithBeforeLambdaBody);
23372 verifyFormat(
23373 "auto fct_SLS_All = []()\n"
23374 "{\n"
23375 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23376 " AndShouldNotBeConsiderAsInline,\n"
23377 " LambdaBodyMustBeBreak);\n"
23378 "};",
23379 LLVMWithBeforeLambdaBody);
23380 LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine;
23381 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23382 LLVMWithBeforeLambdaBody);
23383 verifyFormat(
23384 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23385 " FirstParam,\n"
23386 " SecondParam,\n"
23387 " ThirdParam,\n"
23388 " FourthParam);",
23389 LLVMWithBeforeLambdaBody);
23390 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23391 " []() { return "
23392 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23393 " FirstParam,\n"
23394 " SecondParam,\n"
23395 " ThirdParam,\n"
23396 " FourthParam);",
23397 LLVMWithBeforeLambdaBody);
23398 verifyFormat(
23399 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23400 " SecondParam,\n"
23401 " ThirdParam,\n"
23402 " FourthParam,\n"
23403 " []() { return SomeValueNotSoLong; });",
23404 LLVMWithBeforeLambdaBody);
23405 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23406 " []()\n"
23407 " {\n"
23408 " return "
23409 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23410 "eConsiderAsInline;\n"
23411 " });",
23412 LLVMWithBeforeLambdaBody);
23413 verifyFormat(
23414 "FctWithLongLineInLambda_SLS_All(\n"
23415 " []()\n"
23416 " {\n"
23417 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23418 " AndShouldNotBeConsiderAsInline,\n"
23419 " LambdaBodyMustBeBreak);\n"
23420 " });",
23421 LLVMWithBeforeLambdaBody);
23422 verifyFormat("FctWithTwoParams_SLS_All(\n"
23423 " []()\n"
23424 " {\n"
23425 " // A cool function...\n"
23426 " return 43;\n"
23427 " },\n"
23428 " 87);",
23429 LLVMWithBeforeLambdaBody);
23430 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23431 LLVMWithBeforeLambdaBody);
23432 verifyFormat(
23433 "FctWithTwoParams_SLS_All(\n"
23434 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23435 LLVMWithBeforeLambdaBody);
23436 verifyFormat(
23437 "FctWithTwoParams_SLS_All(\n"
23438 " 87,\n"
23439 " []()\n"
23440 " {\n"
23441 " return "
23442 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23443 " });",
23444 LLVMWithBeforeLambdaBody);
23445 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23446 LLVMWithBeforeLambdaBody);
23447 verifyFormat(
23448 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23449 LLVMWithBeforeLambdaBody);
23450 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23451 "}); }, x);",
23452 LLVMWithBeforeLambdaBody);
23453 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23454 " []()\n"
23455 " {\n"
23456 " // A cool function...\n"
23457 " return Call([]() { return 17; });\n"
23458 " });",
23459 LLVMWithBeforeLambdaBody);
23460 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23461 " []()\n"
23462 " {\n"
23463 " return Call(\n"
23464 " []()\n"
23465 " {\n"
23466 " // A cool function...\n"
23467 " return 17;\n"
23468 " });\n"
23469 " });",
23470 LLVMWithBeforeLambdaBody);
23472 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23473 FormatStyle::ShortLambdaStyle::SLS_None;
23475 verifyFormat("auto select = [this]() -> const Library::Object *\n"
23476 "{\n"
23477 " return MyAssignment::SelectFromList(this);\n"
23478 "};",
23479 LLVMWithBeforeLambdaBody);
23481 verifyFormat("auto select = [this]() -> const Library::Object &\n"
23482 "{\n"
23483 " return MyAssignment::SelectFromList(this);\n"
23484 "};",
23485 LLVMWithBeforeLambdaBody);
23487 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23488 "{\n"
23489 " return MyAssignment::SelectFromList(this);\n"
23490 "};",
23491 LLVMWithBeforeLambdaBody);
23493 verifyFormat("namespace test {\n"
23494 "class Test {\n"
23495 "public:\n"
23496 " Test() = default;\n"
23497 "};\n"
23498 "} // namespace test",
23499 LLVMWithBeforeLambdaBody);
23501 // Lambdas with different indentation styles.
23502 Style = getLLVMStyleWithColumns(60);
23503 verifyFormat("Result doSomething(Promise promise) {\n"
23504 " return promise.then(\n"
23505 " [this, obj = std::move(s)](int bar) mutable {\n"
23506 " return someObject.startAsyncAction().then(\n"
23507 " [this, &obj](Result result) mutable {\n"
23508 " result.processMore();\n"
23509 " });\n"
23510 " });\n"
23511 "}",
23512 Style);
23513 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
23514 verifyFormat("Result doSomething(Promise promise) {\n"
23515 " return promise.then(\n"
23516 " [this, obj = std::move(s)](int bar) mutable {\n"
23517 " return obj.startAsyncAction().then(\n"
23518 " [this, &obj](Result result) mutable {\n"
23519 " result.processMore();\n"
23520 " });\n"
23521 " });\n"
23522 "}",
23523 Style);
23524 verifyFormat("Result doSomething(Promise promise) {\n"
23525 " return promise.then([this, obj = std::move(s)] {\n"
23526 " return obj.startAsyncAction().then(\n"
23527 " [this, &obj](Result result) mutable {\n"
23528 " result.processMore();\n"
23529 " });\n"
23530 " });\n"
23531 "}",
23532 Style);
23533 verifyFormat("void test() {\n"
23534 " ([]() -> auto {\n"
23535 " int b = 32;\n"
23536 " return 3;\n"
23537 " }).foo();\n"
23538 "}",
23539 Style);
23540 verifyFormat("void test() {\n"
23541 " []() -> auto {\n"
23542 " int b = 32;\n"
23543 " return 3;\n"
23544 " }\n"
23545 "}",
23546 Style);
23547 verifyFormat("void test() {\n"
23548 " std::sort(v.begin(), v.end(),\n"
23549 " [](const auto &foo, const auto &bar) {\n"
23550 " return foo.baz < bar.baz;\n"
23551 " });\n"
23552 "};",
23553 Style);
23554 verifyFormat("void test() {\n"
23555 " (\n"
23556 " []() -> auto {\n"
23557 " int b = 32;\n"
23558 " return 3;\n"
23559 " }, foo, bar)\n"
23560 " .foo();\n"
23561 "}",
23562 Style);
23563 verifyFormat("void test() {\n"
23564 " ([]() -> auto {\n"
23565 " int b = 32;\n"
23566 " return 3;\n"
23567 " })\n"
23568 " .foo()\n"
23569 " .bar();\n"
23570 "}",
23571 Style);
23572 verifyFormat("#define A \\\n"
23573 " [] { \\\n"
23574 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
23575 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
23576 " }",
23577 Style);
23578 verifyFormat("#define SORT(v) \\\n"
23579 " std::sort(v.begin(), v.end(), \\\n"
23580 " [](const auto &foo, const auto &bar) { \\\n"
23581 " return foo.baz < bar.baz; \\\n"
23582 " });",
23583 Style);
23584 verifyFormat("void foo() {\n"
23585 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23586 " auto f = e(d);\n"
23587 " return f;\n"
23588 " })));\n"
23589 "}",
23590 Style);
23591 verifyFormat("void foo() {\n"
23592 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23593 " auto f = e(foo, [&] {\n"
23594 " auto g = h();\n"
23595 " return g;\n"
23596 " }, qux, [&] -> Bar {\n"
23597 " auto i = j();\n"
23598 " return i;\n"
23599 " });\n"
23600 " return f;\n"
23601 " })));\n"
23602 "}",
23603 Style);
23604 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23605 " AnotherLongClassName baz)\n"
23606 " : baz{baz}, func{[&] {\n"
23607 " auto qux = bar;\n"
23608 " return aFunkyFunctionCall(qux);\n"
23609 " }} {}",
23610 Style);
23611 verifyFormat("void foo() {\n"
23612 " class Foo {\n"
23613 " public:\n"
23614 " Foo()\n"
23615 " : qux{[](int quux) {\n"
23616 " auto tmp = quux;\n"
23617 " return tmp;\n"
23618 " }} {}\n"
23619 "\n"
23620 " private:\n"
23621 " std::function<void(int quux)> qux;\n"
23622 " };\n"
23623 "}",
23624 Style);
23625 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
23626 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23627 " AnotherLongClassName baz) :\n"
23628 " baz{baz}, func{[&] {\n"
23629 " auto qux = bar;\n"
23630 " return aFunkyFunctionCall(qux);\n"
23631 " }} {}",
23632 Style);
23633 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
23634 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23635 " AnotherLongClassName baz) :\n"
23636 " baz{baz},\n"
23637 " func{[&] {\n"
23638 " auto qux = bar;\n"
23639 " return aFunkyFunctionCall(qux);\n"
23640 " }} {}",
23641 Style);
23642 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
23643 // FIXME: The following test should pass, but fails at the time of writing.
23644 #if 0
23645 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23646 // doesn't force an initial line break, even if lambdas span multiple lines.
23647 verifyFormat("void foo() {\n"
23648 " aFunction(\n"
23649 " [](d) -> Foo {\n"
23650 " auto f = e(d);\n"
23651 " return f;\n"
23652 " }, foo, Bar{}, [] {\n"
23653 " auto g = h();\n"
23654 " return g;\n"
23655 " }, baz);\n"
23656 "}",
23657 Style);
23658 #endif
23659 // A long non-lambda argument forces arguments to span multiple lines and thus
23660 // forces an initial line break when using AlwaysBreak.
23661 verifyFormat("void foo() {\n"
23662 " aFunction(\n"
23663 " 1,\n"
23664 " [](d) -> Foo {\n"
23665 " auto f = e(d);\n"
23666 " return f;\n"
23667 " }, foo, Bar{},\n"
23668 " [] {\n"
23669 " auto g = h();\n"
23670 " return g;\n"
23671 " }, bazzzzz,\n"
23672 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23673 "}",
23674 Style);
23675 Style.BinPackArguments = false;
23676 verifyFormat("void foo() {\n"
23677 " aFunction(\n"
23678 " 1,\n"
23679 " [](d) -> Foo {\n"
23680 " auto f = e(d);\n"
23681 " return f;\n"
23682 " },\n"
23683 " foo,\n"
23684 " Bar{},\n"
23685 " [] {\n"
23686 " auto g = h();\n"
23687 " return g;\n"
23688 " },\n"
23689 " bazzzzz,\n"
23690 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23691 "}",
23692 Style);
23693 Style.BinPackArguments = true;
23694 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23695 Style.BraceWrapping.BeforeLambdaBody = true;
23696 verifyFormat("void foo() {\n"
23697 " aFunction(\n"
23698 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23699 " {\n"
23700 " auto f = e(\n"
23701 " [&]\n"
23702 " {\n"
23703 " auto g = h();\n"
23704 " return g;\n"
23705 " }, qux, [&] -> Bar\n"
23706 " {\n"
23707 " auto i = j();\n"
23708 " return i;\n"
23709 " });\n"
23710 " return f;\n"
23711 " })));\n"
23712 "}",
23713 Style);
23716 TEST_F(FormatTest, LambdaWithLineComments) {
23717 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23718 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23719 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23720 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23721 FormatStyle::ShortLambdaStyle::SLS_All;
23723 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
23724 verifyFormat("auto k = []() // comment\n"
23725 "{ return; }",
23726 LLVMWithBeforeLambdaBody);
23727 verifyFormat("auto k = []() /* comment */ { return; }",
23728 LLVMWithBeforeLambdaBody);
23729 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23730 LLVMWithBeforeLambdaBody);
23731 verifyFormat("auto k = []() // X\n"
23732 "{ return; }",
23733 LLVMWithBeforeLambdaBody);
23734 verifyFormat(
23735 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23736 "{ return; }",
23737 LLVMWithBeforeLambdaBody);
23739 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
23741 verifyFormat("foo([]()\n"
23742 " {\n"
23743 " bar(); //\n"
23744 " return 1; // comment\n"
23745 " }());",
23746 "foo([]() {\n"
23747 " bar(); //\n"
23748 " return 1; // comment\n"
23749 "}());",
23750 LLVMWithBeforeLambdaBody);
23751 verifyFormat("foo(\n"
23752 " 1, MACRO {\n"
23753 " baz();\n"
23754 " bar(); // comment\n"
23755 " },\n"
23756 " []() {});",
23757 "foo(\n"
23758 " 1, MACRO { baz(); bar(); // comment\n"
23759 " }, []() {}\n"
23760 ");",
23761 LLVMWithBeforeLambdaBody);
23764 TEST_F(FormatTest, EmptyLinesInLambdas) {
23765 verifyFormat("auto lambda = []() {\n"
23766 " x(); //\n"
23767 "};",
23768 "auto lambda = []() {\n"
23769 "\n"
23770 " x(); //\n"
23771 "\n"
23772 "};");
23775 TEST_F(FormatTest, FormatsBlocks) {
23776 FormatStyle ShortBlocks = getLLVMStyle();
23777 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23778 verifyFormat("int (^Block)(int, int);", ShortBlocks);
23779 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
23780 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
23781 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
23782 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
23783 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
23785 verifyFormat("foo(^{ bar(); });", ShortBlocks);
23786 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
23787 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
23789 verifyFormat("[operation setCompletionBlock:^{\n"
23790 " [self onOperationDone];\n"
23791 "}];");
23792 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23793 " [self onOperationDone];\n"
23794 "}]};");
23795 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23796 " f();\n"
23797 "}];");
23798 verifyFormat("int a = [operation block:^int(int *i) {\n"
23799 " return 1;\n"
23800 "}];");
23801 verifyFormat("[myObject doSomethingWith:arg1\n"
23802 " aaa:^int(int *a) {\n"
23803 " return 1;\n"
23804 " }\n"
23805 " bbb:f(a * bbbbbbbb)];");
23807 verifyFormat("[operation setCompletionBlock:^{\n"
23808 " [self.delegate newDataAvailable];\n"
23809 "}];",
23810 getLLVMStyleWithColumns(60));
23811 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23812 " NSString *path = [self sessionFilePath];\n"
23813 " if (path) {\n"
23814 " // ...\n"
23815 " }\n"
23816 "});");
23817 verifyFormat("[[SessionService sharedService]\n"
23818 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23819 " if (window) {\n"
23820 " [self windowDidLoad:window];\n"
23821 " } else {\n"
23822 " [self errorLoadingWindow];\n"
23823 " }\n"
23824 " }];");
23825 verifyFormat("void (^largeBlock)(void) = ^{\n"
23826 " // ...\n"
23827 "};",
23828 getLLVMStyleWithColumns(40));
23829 verifyFormat("[[SessionService sharedService]\n"
23830 " loadWindowWithCompletionBlock: //\n"
23831 " ^(SessionWindow *window) {\n"
23832 " if (window) {\n"
23833 " [self windowDidLoad:window];\n"
23834 " } else {\n"
23835 " [self errorLoadingWindow];\n"
23836 " }\n"
23837 " }];",
23838 getLLVMStyleWithColumns(60));
23839 verifyFormat("[myObject doSomethingWith:arg1\n"
23840 " firstBlock:^(Foo *a) {\n"
23841 " // ...\n"
23842 " int i;\n"
23843 " }\n"
23844 " secondBlock:^(Bar *b) {\n"
23845 " // ...\n"
23846 " int i;\n"
23847 " }\n"
23848 " thirdBlock:^Foo(Bar *b) {\n"
23849 " // ...\n"
23850 " int i;\n"
23851 " }];");
23852 verifyFormat("[myObject doSomethingWith:arg1\n"
23853 " firstBlock:-1\n"
23854 " secondBlock:^(Bar *b) {\n"
23855 " // ...\n"
23856 " int i;\n"
23857 " }];");
23859 verifyFormat("f(^{\n"
23860 " @autoreleasepool {\n"
23861 " if (a) {\n"
23862 " g();\n"
23863 " }\n"
23864 " }\n"
23865 "});");
23866 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23867 "};");
23868 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23869 "};");
23871 FormatStyle FourIndent = getLLVMStyle();
23872 FourIndent.ObjCBlockIndentWidth = 4;
23873 verifyFormat("[operation setCompletionBlock:^{\n"
23874 " [self onOperationDone];\n"
23875 "}];",
23876 FourIndent);
23879 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
23880 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
23882 verifyFormat("[[SessionService sharedService] "
23883 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23884 " if (window) {\n"
23885 " [self windowDidLoad:window];\n"
23886 " } else {\n"
23887 " [self errorLoadingWindow];\n"
23888 " }\n"
23889 "}];",
23890 ZeroColumn);
23891 verifyFormat("[[SessionService sharedService]\n"
23892 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23893 " if (window) {\n"
23894 " [self windowDidLoad:window];\n"
23895 " } else {\n"
23896 " [self errorLoadingWindow];\n"
23897 " }\n"
23898 " }];",
23899 "[[SessionService sharedService]\n"
23900 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23901 " if (window) {\n"
23902 " [self windowDidLoad:window];\n"
23903 " } else {\n"
23904 " [self errorLoadingWindow];\n"
23905 " }\n"
23906 "}];",
23907 ZeroColumn);
23908 verifyFormat("[myObject doSomethingWith:arg1\n"
23909 " firstBlock:^(Foo *a) {\n"
23910 " // ...\n"
23911 " int i;\n"
23912 " }\n"
23913 " secondBlock:^(Bar *b) {\n"
23914 " // ...\n"
23915 " int i;\n"
23916 " }\n"
23917 " thirdBlock:^Foo(Bar *b) {\n"
23918 " // ...\n"
23919 " int i;\n"
23920 " }];",
23921 ZeroColumn);
23922 verifyFormat("f(^{\n"
23923 " @autoreleasepool {\n"
23924 " if (a) {\n"
23925 " g();\n"
23926 " }\n"
23927 " }\n"
23928 "});",
23929 ZeroColumn);
23930 verifyFormat("void (^largeBlock)(void) = ^{\n"
23931 " // ...\n"
23932 "};",
23933 ZeroColumn);
23935 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23936 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23937 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23938 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23939 verifyFormat("void (^largeBlock)(void) = ^{\n"
23940 " int i;\n"
23941 "};",
23942 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23945 TEST_F(FormatTest, SupportsCRLF) {
23946 verifyFormat("int a;\r\n"
23947 "int b;\r\n"
23948 "int c;",
23949 "int a;\r\n"
23950 " int b;\r\n"
23951 " int c;");
23952 verifyFormat("int a;\r\n"
23953 "int b;\r\n"
23954 "int c;\r\n",
23955 "int a;\r\n"
23956 " int b;\n"
23957 " int c;\r\n");
23958 verifyFormat("int a;\n"
23959 "int b;\n"
23960 "int c;",
23961 "int a;\r\n"
23962 " int b;\n"
23963 " int c;");
23964 // FIXME: unstable test case
23965 EXPECT_EQ("\"aaaaaaa \"\r\n"
23966 "\"bbbbbbb\";\r\n",
23967 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23968 verifyFormat("#define A \\\r\n"
23969 " b; \\\r\n"
23970 " c; \\\r\n"
23971 " d;",
23972 "#define A \\\r\n"
23973 " b; \\\r\n"
23974 " c; d; ",
23975 getGoogleStyle());
23977 verifyNoChange("/*\r\n"
23978 "multi line block comments\r\n"
23979 "should not introduce\r\n"
23980 "an extra carriage return\r\n"
23981 "*/");
23982 verifyFormat("/*\r\n"
23983 "\r\n"
23984 "*/",
23985 "/*\r\n"
23986 " \r\r\r\n"
23987 "*/");
23989 FormatStyle style = getLLVMStyle();
23991 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
23992 verifyFormat("union FooBarBazQux {\n"
23993 " int foo;\n"
23994 " int bar;\n"
23995 " int baz;\n"
23996 "};",
23997 "union FooBarBazQux {\r\n"
23998 " int foo;\n"
23999 " int bar;\r\n"
24000 " int baz;\n"
24001 "};",
24002 style);
24003 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24004 verifyFormat("union FooBarBazQux {\r\n"
24005 " int foo;\r\n"
24006 " int bar;\r\n"
24007 " int baz;\r\n"
24008 "};",
24009 "union FooBarBazQux {\r\n"
24010 " int foo;\n"
24011 " int bar;\r\n"
24012 " int baz;\n"
24013 "};",
24014 style);
24016 style.LineEnding = FormatStyle::LE_LF;
24017 verifyFormat("union FooBarBazQux {\n"
24018 " int foo;\n"
24019 " int bar;\n"
24020 " int baz;\n"
24021 " int qux;\n"
24022 "};",
24023 "union FooBarBazQux {\r\n"
24024 " int foo;\n"
24025 " int bar;\r\n"
24026 " int baz;\n"
24027 " int qux;\r\n"
24028 "};",
24029 style);
24030 style.LineEnding = FormatStyle::LE_CRLF;
24031 verifyFormat("union FooBarBazQux {\r\n"
24032 " int foo;\r\n"
24033 " int bar;\r\n"
24034 " int baz;\r\n"
24035 " int qux;\r\n"
24036 "};",
24037 "union FooBarBazQux {\r\n"
24038 " int foo;\n"
24039 " int bar;\r\n"
24040 " int baz;\n"
24041 " int qux;\n"
24042 "};",
24043 style);
24045 style.LineEnding = FormatStyle::LE_DeriveLF;
24046 verifyFormat("union FooBarBazQux {\r\n"
24047 " int foo;\r\n"
24048 " int bar;\r\n"
24049 " int baz;\r\n"
24050 " int qux;\r\n"
24051 "};",
24052 "union FooBarBazQux {\r\n"
24053 " int foo;\n"
24054 " int bar;\r\n"
24055 " int baz;\n"
24056 " int qux;\r\n"
24057 "};",
24058 style);
24059 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24060 verifyFormat("union FooBarBazQux {\n"
24061 " int foo;\n"
24062 " int bar;\n"
24063 " int baz;\n"
24064 " int qux;\n"
24065 "};",
24066 "union FooBarBazQux {\r\n"
24067 " int foo;\n"
24068 " int bar;\r\n"
24069 " int baz;\n"
24070 " int qux;\n"
24071 "};",
24072 style);
24075 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
24076 verifyFormat("MY_CLASS(C) {\n"
24077 " int i;\n"
24078 " int j;\n"
24079 "};");
24082 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
24083 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
24084 TwoIndent.ContinuationIndentWidth = 2;
24086 verifyFormat("int i =\n"
24087 " longFunction(\n"
24088 " arg);",
24089 "int i = longFunction(arg);", TwoIndent);
24091 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
24092 SixIndent.ContinuationIndentWidth = 6;
24094 verifyFormat("int i =\n"
24095 " longFunction(\n"
24096 " arg);",
24097 "int i = longFunction(arg);", SixIndent);
24100 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
24101 FormatStyle Style = getLLVMStyle();
24102 verifyFormat("int Foo::getter(\n"
24103 " //\n"
24104 ") const {\n"
24105 " return foo;\n"
24106 "}",
24107 Style);
24108 verifyFormat("void Foo::setter(\n"
24109 " //\n"
24110 ") {\n"
24111 " foo = 1;\n"
24112 "}",
24113 Style);
24116 TEST_F(FormatTest, SpacesInAngles) {
24117 FormatStyle Spaces = getLLVMStyle();
24118 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24120 verifyFormat("vector< ::std::string > x1;", Spaces);
24121 verifyFormat("Foo< int, Bar > x2;", Spaces);
24122 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
24124 verifyFormat("static_cast< int >(arg);", Spaces);
24125 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
24126 verifyFormat("f< int, float >();", Spaces);
24127 verifyFormat("template <> g() {}", Spaces);
24128 verifyFormat("template < std::vector< int > > f() {}", Spaces);
24129 verifyFormat("std::function< void(int, int) > fct;", Spaces);
24130 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24131 Spaces);
24133 Spaces.Standard = FormatStyle::LS_Cpp03;
24134 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24135 verifyFormat("A< A< int > >();", Spaces);
24137 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24138 verifyFormat("A<A<int> >();", Spaces);
24140 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24141 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24142 Spaces);
24143 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24144 Spaces);
24146 verifyFormat("A<A<int> >();", Spaces);
24147 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
24148 verifyFormat("A< A< int > >();", Spaces);
24150 Spaces.Standard = FormatStyle::LS_Cpp11;
24151 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24152 verifyFormat("A< A< int > >();", Spaces);
24154 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24155 verifyFormat("vector<::std::string> x4;", Spaces);
24156 verifyFormat("vector<int> x5;", Spaces);
24157 verifyFormat("Foo<int, Bar> x6;", Spaces);
24158 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24160 verifyFormat("A<A<int>>();", Spaces);
24162 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24163 verifyFormat("vector<::std::string> x4;", Spaces);
24164 verifyFormat("vector< ::std::string > x4;", Spaces);
24165 verifyFormat("vector<int> x5;", Spaces);
24166 verifyFormat("vector< int > x5;", Spaces);
24167 verifyFormat("Foo<int, Bar> x6;", Spaces);
24168 verifyFormat("Foo< int, Bar > x6;", Spaces);
24169 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24170 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
24172 verifyFormat("A<A<int>>();", Spaces);
24173 verifyFormat("A< A< int > >();", Spaces);
24174 verifyFormat("A<A<int > >();", Spaces);
24175 verifyFormat("A< A< int>>();", Spaces);
24177 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24178 verifyFormat("// clang-format off\n"
24179 "foo<<<1, 1>>>();\n"
24180 "// clang-format on",
24181 Spaces);
24182 verifyFormat("// clang-format off\n"
24183 "foo< < <1, 1> > >();\n"
24184 "// clang-format on",
24185 Spaces);
24188 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
24189 FormatStyle Style = getLLVMStyle();
24190 Style.SpaceAfterTemplateKeyword = false;
24191 verifyFormat("template<int> void foo();", Style);
24194 TEST_F(FormatTest, TripleAngleBrackets) {
24195 verifyFormat("f<<<1, 1>>>();");
24196 verifyFormat("f<<<1, 1, 1, s>>>();");
24197 verifyFormat("f<<<a, b, c, d>>>();");
24198 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24199 verifyFormat("f<param><<<1, 1>>>();");
24200 verifyFormat("f<1><<<1, 1>>>();");
24201 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24202 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24203 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24204 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24205 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24208 TEST_F(FormatTest, MergeLessLessAtEnd) {
24209 verifyFormat("<<");
24210 verifyFormat("< < <", "\\\n<<<");
24211 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24212 "aaallvm::outs() <<");
24213 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24214 "aaaallvm::outs()\n <<");
24217 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
24218 std::string code = "#if A\n"
24219 "#if B\n"
24220 "a.\n"
24221 "#endif\n"
24222 " a = 1;\n"
24223 "#else\n"
24224 "#endif\n"
24225 "#if C\n"
24226 "#else\n"
24227 "#endif\n";
24228 verifyFormat(code);
24231 TEST_F(FormatTest, HandleConflictMarkers) {
24232 // Git/SVN conflict markers.
24233 verifyFormat("int a;\n"
24234 "void f() {\n"
24235 " callme(some(parameter1,\n"
24236 "<<<<<<< text by the vcs\n"
24237 " parameter2),\n"
24238 "||||||| text by the vcs\n"
24239 " parameter2),\n"
24240 " parameter3,\n"
24241 "======= text by the vcs\n"
24242 " parameter2, parameter3),\n"
24243 ">>>>>>> text by the vcs\n"
24244 " otherparameter);",
24245 "int a;\n"
24246 "void f() {\n"
24247 " callme(some(parameter1,\n"
24248 "<<<<<<< text by the vcs\n"
24249 " parameter2),\n"
24250 "||||||| text by the vcs\n"
24251 " parameter2),\n"
24252 " parameter3,\n"
24253 "======= text by the vcs\n"
24254 " parameter2,\n"
24255 " parameter3),\n"
24256 ">>>>>>> text by the vcs\n"
24257 " otherparameter);");
24259 // Perforce markers.
24260 verifyFormat("void f() {\n"
24261 " function(\n"
24262 ">>>> text by the vcs\n"
24263 " parameter,\n"
24264 "==== text by the vcs\n"
24265 " parameter,\n"
24266 "==== text by the vcs\n"
24267 " parameter,\n"
24268 "<<<< text by the vcs\n"
24269 " parameter);",
24270 "void f() {\n"
24271 " function(\n"
24272 ">>>> text by the vcs\n"
24273 " parameter,\n"
24274 "==== text by the vcs\n"
24275 " parameter,\n"
24276 "==== text by the vcs\n"
24277 " parameter,\n"
24278 "<<<< text by the vcs\n"
24279 " parameter);");
24281 verifyNoChange("<<<<<<<\n"
24282 "|||||||\n"
24283 "=======\n"
24284 ">>>>>>>");
24286 verifyNoChange("<<<<<<<\n"
24287 "|||||||\n"
24288 "int i;\n"
24289 "=======\n"
24290 ">>>>>>>");
24292 // FIXME: Handle parsing of macros around conflict markers correctly:
24293 verifyFormat("#define Macro \\\n"
24294 "<<<<<<<\n"
24295 "Something \\\n"
24296 "|||||||\n"
24297 "Else \\\n"
24298 "=======\n"
24299 "Other \\\n"
24300 ">>>>>>>\n"
24301 " End int i;",
24302 "#define Macro \\\n"
24303 "<<<<<<<\n"
24304 " Something \\\n"
24305 "|||||||\n"
24306 " Else \\\n"
24307 "=======\n"
24308 " Other \\\n"
24309 ">>>>>>>\n"
24310 " End\n"
24311 "int i;");
24313 verifyFormat(R"(====
24314 #ifdef A
24316 #else
24318 #endif
24319 )");
24322 TEST_F(FormatTest, DisableRegions) {
24323 verifyFormat("int i;\n"
24324 "// clang-format off\n"
24325 " int j;\n"
24326 "// clang-format on\n"
24327 "int k;",
24328 " int i;\n"
24329 " // clang-format off\n"
24330 " int j;\n"
24331 " // clang-format on\n"
24332 " int k;");
24333 verifyFormat("int i;\n"
24334 "/* clang-format off */\n"
24335 " int j;\n"
24336 "/* clang-format on */\n"
24337 "int k;",
24338 " int i;\n"
24339 " /* clang-format off */\n"
24340 " int j;\n"
24341 " /* clang-format on */\n"
24342 " int k;");
24344 // Don't reflow comments within disabled regions.
24345 verifyFormat("// clang-format off\n"
24346 "// long long long long long long line\n"
24347 "/* clang-format on */\n"
24348 "/* long long long\n"
24349 " * long long long\n"
24350 " * line */\n"
24351 "int i;\n"
24352 "/* clang-format off */\n"
24353 "/* long long long long long long line */",
24354 "// clang-format off\n"
24355 "// long long long long long long line\n"
24356 "/* clang-format on */\n"
24357 "/* long long long long long long line */\n"
24358 "int i;\n"
24359 "/* clang-format off */\n"
24360 "/* long long long long long long line */",
24361 getLLVMStyleWithColumns(20));
24363 verifyFormat("int *i;\n"
24364 "// clang-format off:\n"
24365 "int* j;\n"
24366 "// clang-format on: 1\n"
24367 "int *k;",
24368 "int* i;\n"
24369 "// clang-format off:\n"
24370 "int* j;\n"
24371 "// clang-format on: 1\n"
24372 "int* k;");
24374 verifyFormat("int *i;\n"
24375 "// clang-format off:0\n"
24376 "int* j;\n"
24377 "// clang-format only\n"
24378 "int* k;",
24379 "int* i;\n"
24380 "// clang-format off:0\n"
24381 "int* j;\n"
24382 "// clang-format only\n"
24383 "int* k;");
24385 verifyNoChange("// clang-format off\n"
24386 "#if 0\n"
24387 " #if SHOULD_STAY_INDENTED\n"
24388 " #endif\n"
24389 "#endif\n"
24390 "// clang-format on");
24393 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
24394 format("? ) =");
24395 verifyNoCrash("#define a\\\n /**/}");
24396 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n"
24397 "LY52: ! [internal]");
24400 TEST_F(FormatTest, FormatsTableGenCode) {
24401 FormatStyle Style = getLLVMStyle();
24402 Style.Language = FormatStyle::LK_TableGen;
24403 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
24406 TEST_F(FormatTest, ArrayOfTemplates) {
24407 verifyFormat("auto a = new unique_ptr<int>[10];",
24408 "auto a = new unique_ptr<int > [ 10];");
24410 FormatStyle Spaces = getLLVMStyle();
24411 Spaces.SpacesInSquareBrackets = true;
24412 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24413 "auto a = new unique_ptr<int > [10];", Spaces);
24416 TEST_F(FormatTest, ArrayAsTemplateType) {
24417 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24418 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24420 FormatStyle Spaces = getLLVMStyle();
24421 Spaces.SpacesInSquareBrackets = true;
24422 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24423 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
24426 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
24428 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
24429 verifyFormat("using std::cin;\n"
24430 "using std::cout;",
24431 "using std::cout;\n"
24432 "using std::cin;",
24433 getGoogleStyle());
24436 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
24437 FormatStyle Style = getLLVMStyle();
24438 Style.Standard = FormatStyle::LS_Cpp03;
24439 // cpp03 recognize this string as identifier u8 and literal character 'a'
24440 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
24443 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
24444 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24445 // all modes, including C++11, C++14 and C++17
24446 verifyFormat("auto c = u8'a';");
24449 TEST_F(FormatTest, DoNotFormatLikelyXml) {
24450 verifyGoogleFormat("<!-- ;> -->");
24451 verifyNoChange(" <!-- >; -->", getGoogleStyle());
24454 TEST_F(FormatTest, StructuredBindings) {
24455 // Structured bindings is a C++17 feature.
24456 // all modes, including C++11, C++14 and C++17
24457 verifyFormat("auto [a, b] = f();");
24458 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24459 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
24460 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
24461 verifyFormat("auto const volatile [a, b] = f();",
24462 "auto const volatile[a, b] = f();");
24463 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
24464 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
24465 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
24466 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
24467 verifyFormat("auto const volatile &&[a, b] = f();",
24468 "auto const volatile &&[a, b] = f();");
24469 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
24470 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
24471 verifyFormat("const auto volatile &&[a, b] = f();",
24472 "const auto volatile &&[a, b] = f();");
24473 verifyFormat("volatile const auto &&[a, b] = f();",
24474 "volatile const auto &&[a, b] = f();");
24475 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
24477 // Make sure we don't mistake structured bindings for lambdas.
24478 FormatStyle PointerMiddle = getLLVMStyle();
24479 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
24480 verifyGoogleFormat("auto [a1, b]{A * i};");
24481 verifyFormat("auto [a2, b]{A * i};");
24482 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
24483 verifyGoogleFormat("auto const [a1, b]{A * i};");
24484 verifyFormat("auto const [a2, b]{A * i};");
24485 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
24486 verifyGoogleFormat("auto const& [a1, b]{A * i};");
24487 verifyFormat("auto const &[a2, b]{A * i};");
24488 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
24489 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24490 verifyFormat("auto const &&[a2, b]{A * i};");
24491 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
24493 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24494 "for (const auto && [a, b] : some_range) {\n}");
24495 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24496 "for (const auto & [a, b] : some_range) {\n}");
24497 verifyFormat("for (const auto [a, b] : some_range) {\n}",
24498 "for (const auto[a, b] : some_range) {\n}");
24499 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
24500 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
24501 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
24502 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
24503 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
24504 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
24505 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
24506 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
24508 FormatStyle Spaces = getLLVMStyle();
24509 Spaces.SpacesInSquareBrackets = true;
24510 verifyFormat("auto [ a, b ] = f();", Spaces);
24511 verifyFormat("auto &&[ a, b ] = f();", Spaces);
24512 verifyFormat("auto &[ a, b ] = f();", Spaces);
24513 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
24514 verifyFormat("auto const &[ a, b ] = f();", Spaces);
24517 TEST_F(FormatTest, FileAndCode) {
24518 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
24519 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
24520 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
24521 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
24522 EXPECT_EQ(FormatStyle::LK_ObjC,
24523 guessLanguage("foo.h", "@interface Foo\n@end"));
24524 EXPECT_EQ(
24525 FormatStyle::LK_ObjC,
24526 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24527 EXPECT_EQ(FormatStyle::LK_ObjC,
24528 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24529 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
24530 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
24531 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
24532 EXPECT_EQ(FormatStyle::LK_ObjC,
24533 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24534 EXPECT_EQ(FormatStyle::LK_ObjC,
24535 guessLanguage(
24536 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24537 EXPECT_EQ(
24538 FormatStyle::LK_Cpp,
24539 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24540 // Only one of the two preprocessor regions has ObjC-like code.
24541 EXPECT_EQ(FormatStyle::LK_ObjC,
24542 guessLanguage("foo.h", "#if A\n"
24543 "#define B() C\n"
24544 "#else\n"
24545 "#define B() [NSString a:@\"\"]\n"
24546 "#endif"));
24549 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
24550 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
24551 EXPECT_EQ(FormatStyle::LK_ObjC,
24552 guessLanguage("foo.h", "array[[calculator getIndex]];"));
24553 EXPECT_EQ(FormatStyle::LK_Cpp,
24554 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24555 EXPECT_EQ(
24556 FormatStyle::LK_Cpp,
24557 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24558 EXPECT_EQ(FormatStyle::LK_ObjC,
24559 guessLanguage("foo.h", "[[noreturn foo] bar];"));
24560 EXPECT_EQ(FormatStyle::LK_Cpp,
24561 guessLanguage("foo.h", "[[clang::fallthrough]];"));
24562 EXPECT_EQ(FormatStyle::LK_ObjC,
24563 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24564 EXPECT_EQ(FormatStyle::LK_Cpp,
24565 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24566 EXPECT_EQ(FormatStyle::LK_Cpp,
24567 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24568 EXPECT_EQ(FormatStyle::LK_ObjC,
24569 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24570 EXPECT_EQ(FormatStyle::LK_Cpp,
24571 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24572 EXPECT_EQ(
24573 FormatStyle::LK_Cpp,
24574 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24575 EXPECT_EQ(
24576 FormatStyle::LK_Cpp,
24577 guessLanguage("foo.h",
24578 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24579 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24582 TEST_F(FormatTest, GuessLanguageWithCaret) {
24583 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
24584 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
24585 EXPECT_EQ(FormatStyle::LK_ObjC,
24586 guessLanguage("foo.h", "int(^)(char, float);"));
24587 EXPECT_EQ(FormatStyle::LK_ObjC,
24588 guessLanguage("foo.h", "int(^foo)(char, float);"));
24589 EXPECT_EQ(FormatStyle::LK_ObjC,
24590 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24591 EXPECT_EQ(FormatStyle::LK_ObjC,
24592 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24593 EXPECT_EQ(
24594 FormatStyle::LK_ObjC,
24595 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24598 TEST_F(FormatTest, GuessLanguageWithPragmas) {
24599 EXPECT_EQ(FormatStyle::LK_Cpp,
24600 guessLanguage("foo.h", "__pragma(warning(disable:))"));
24601 EXPECT_EQ(FormatStyle::LK_Cpp,
24602 guessLanguage("foo.h", "#pragma(warning(disable:))"));
24603 EXPECT_EQ(FormatStyle::LK_Cpp,
24604 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24607 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
24608 // ASM symbolic names are identifiers that must be surrounded by [] without
24609 // space in between:
24610 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24612 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24613 verifyFormat(R"(//
24614 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
24615 )");
24617 // A list of several ASM symbolic names.
24618 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
24620 // ASM symbolic names in inline ASM with inputs and outputs.
24621 verifyFormat(R"(//
24622 asm("cmoveq %1, %2, %[result]"
24623 : [result] "=r"(result)
24624 : "r"(test), "r"(new), "[result]"(old));
24625 )");
24627 // ASM symbolic names in inline ASM with no outputs.
24628 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
24631 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
24632 EXPECT_EQ(FormatStyle::LK_Cpp,
24633 guessLanguage("foo.h", "void f() {\n"
24634 " asm (\"mov %[e], %[d]\"\n"
24635 " : [d] \"=rm\" (d)\n"
24636 " [e] \"rm\" (*e));\n"
24637 "}"));
24638 EXPECT_EQ(FormatStyle::LK_Cpp,
24639 guessLanguage("foo.h", "void f() {\n"
24640 " _asm (\"mov %[e], %[d]\"\n"
24641 " : [d] \"=rm\" (d)\n"
24642 " [e] \"rm\" (*e));\n"
24643 "}"));
24644 EXPECT_EQ(FormatStyle::LK_Cpp,
24645 guessLanguage("foo.h", "void f() {\n"
24646 " __asm (\"mov %[e], %[d]\"\n"
24647 " : [d] \"=rm\" (d)\n"
24648 " [e] \"rm\" (*e));\n"
24649 "}"));
24650 EXPECT_EQ(FormatStyle::LK_Cpp,
24651 guessLanguage("foo.h", "void f() {\n"
24652 " __asm__ (\"mov %[e], %[d]\"\n"
24653 " : [d] \"=rm\" (d)\n"
24654 " [e] \"rm\" (*e));\n"
24655 "}"));
24656 EXPECT_EQ(FormatStyle::LK_Cpp,
24657 guessLanguage("foo.h", "void f() {\n"
24658 " asm (\"mov %[e], %[d]\"\n"
24659 " : [d] \"=rm\" (d),\n"
24660 " [e] \"rm\" (*e));\n"
24661 "}"));
24662 EXPECT_EQ(FormatStyle::LK_Cpp,
24663 guessLanguage("foo.h", "void f() {\n"
24664 " asm volatile (\"mov %[e], %[d]\"\n"
24665 " : [d] \"=rm\" (d)\n"
24666 " [e] \"rm\" (*e));\n"
24667 "}"));
24670 TEST_F(FormatTest, GuessLanguageWithChildLines) {
24671 EXPECT_EQ(FormatStyle::LK_Cpp,
24672 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24673 EXPECT_EQ(FormatStyle::LK_ObjC,
24674 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24675 EXPECT_EQ(
24676 FormatStyle::LK_Cpp,
24677 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24678 EXPECT_EQ(
24679 FormatStyle::LK_ObjC,
24680 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24683 TEST_F(FormatTest, TypenameMacros) {
24684 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24686 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24687 FormatStyle Google = getGoogleStyleWithColumns(0);
24688 Google.TypenameMacros = TypenameMacros;
24689 verifyFormat("struct foo {\n"
24690 " int bar;\n"
24691 " TAILQ_ENTRY(a) bleh;\n"
24692 "};",
24693 Google);
24695 FormatStyle Macros = getLLVMStyle();
24696 Macros.TypenameMacros = TypenameMacros;
24698 verifyFormat("STACK_OF(int) a;", Macros);
24699 verifyFormat("STACK_OF(int) *a;", Macros);
24700 verifyFormat("STACK_OF(int const *) *a;", Macros);
24701 verifyFormat("STACK_OF(int *const) *a;", Macros);
24702 verifyFormat("STACK_OF(int, string) a;", Macros);
24703 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
24704 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
24705 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
24706 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
24707 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
24708 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
24710 Macros.PointerAlignment = FormatStyle::PAS_Left;
24711 verifyFormat("STACK_OF(int)* a;", Macros);
24712 verifyFormat("STACK_OF(int*)* a;", Macros);
24713 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
24714 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
24715 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
24718 TEST_F(FormatTest, AtomicQualifier) {
24719 // Check that we treate _Atomic as a type and not a function call
24720 FormatStyle Google = getGoogleStyleWithColumns(0);
24721 verifyFormat("struct foo {\n"
24722 " int a1;\n"
24723 " _Atomic(a) a2;\n"
24724 " _Atomic(_Atomic(int) *const) a3;\n"
24725 "};",
24726 Google);
24727 verifyFormat("_Atomic(uint64_t) a;");
24728 verifyFormat("_Atomic(uint64_t) *a;");
24729 verifyFormat("_Atomic(uint64_t const *) *a;");
24730 verifyFormat("_Atomic(uint64_t *const) *a;");
24731 verifyFormat("_Atomic(const uint64_t *) *a;");
24732 verifyFormat("_Atomic(uint64_t) a;");
24733 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24734 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24735 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24736 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24738 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24739 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24740 FormatStyle Style = getLLVMStyle();
24741 Style.PointerAlignment = FormatStyle::PAS_Left;
24742 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
24743 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
24744 verifyFormat("_Atomic(int)* a;", Style);
24745 verifyFormat("_Atomic(int*)* a;", Style);
24746 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
24748 Style.SpacesInParens = FormatStyle::SIPO_Custom;
24749 Style.SpacesInParensOptions.InCStyleCasts = true;
24750 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
24751 Style.SpacesInParensOptions.InCStyleCasts = false;
24752 Style.SpacesInParensOptions.Other = true;
24753 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
24754 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
24757 TEST_F(FormatTest, C11Generic) {
24758 verifyFormat("_Generic(x, int: 1, default: 0)");
24759 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24760 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24761 verifyFormat("_Generic(x, int: f1, const int: f2)();");
24762 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24764 verifyFormat("_Generic(x,\n"
24765 " float: f,\n"
24766 " default: d,\n"
24767 " long double: ld,\n"
24768 " float _Complex: fc,\n"
24769 " double _Complex: dc,\n"
24770 " long double _Complex: ldc)");
24772 verifyFormat("while (_Generic(x, //\n"
24773 " long: x)(x) > x) {\n"
24774 "}");
24775 verifyFormat("while (_Generic(x, //\n"
24776 " long: x)(x)) {\n"
24777 "}");
24778 verifyFormat("x(_Generic(x, //\n"
24779 " long: x)(x));");
24781 FormatStyle Style = getLLVMStyle();
24782 Style.ColumnLimit = 40;
24783 verifyFormat("#define LIMIT_MAX(T) \\\n"
24784 " _Generic(((T)0), \\\n"
24785 " unsigned int: UINT_MAX, \\\n"
24786 " unsigned long: ULONG_MAX, \\\n"
24787 " unsigned long long: ULLONG_MAX)",
24788 Style);
24789 verifyFormat("_Generic(x,\n"
24790 " struct A: 1,\n"
24791 " void (*)(void): 2);",
24792 Style);
24794 Style.ContinuationIndentWidth = 2;
24795 verifyFormat("_Generic(x,\n"
24796 " struct A: 1,\n"
24797 " void (*)(void): 2);",
24798 Style);
24801 TEST_F(FormatTest, AmbersandInLamda) {
24802 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24803 FormatStyle AlignStyle = getLLVMStyle();
24804 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
24805 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24806 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
24807 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24810 TEST_F(FormatTest, TrailingReturnTypeAuto) {
24811 FormatStyle Style = getLLVMStyle();
24812 verifyFormat("[]() -> auto { return Val; }", Style);
24813 verifyFormat("[]() -> auto * { return Val; }", Style);
24814 verifyFormat("[]() -> auto & { return Val; }", Style);
24815 verifyFormat("auto foo() -> auto { return Val; }", Style);
24816 verifyFormat("auto foo() -> auto * { return Val; }", Style);
24817 verifyFormat("auto foo() -> auto & { return Val; }", Style);
24820 TEST_F(FormatTest, SpacesInConditionalStatement) {
24821 FormatStyle Spaces = getLLVMStyle();
24822 Spaces.IfMacros.clear();
24823 Spaces.IfMacros.push_back("MYIF");
24824 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
24825 Spaces.SpacesInParensOptions.InConditionalStatements = true;
24826 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
24827 verifyFormat("if ( !a )\n return;", Spaces);
24828 verifyFormat("if ( a )\n return;", Spaces);
24829 verifyFormat("if constexpr ( a )\n return;", Spaces);
24830 verifyFormat("MYIF ( a )\n return;", Spaces);
24831 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
24832 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
24833 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
24834 verifyFormat("while ( a )\n return;", Spaces);
24835 verifyFormat("while ( (a && b) )\n return;", Spaces);
24836 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
24837 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
24838 // Check that space on the left of "::" is inserted as expected at beginning
24839 // of condition.
24840 verifyFormat("while ( ::func() )\n return;", Spaces);
24842 // Check impact of ControlStatementsExceptControlMacros is honored.
24843 Spaces.SpaceBeforeParens =
24844 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
24845 verifyFormat("MYIF( a )\n return;", Spaces);
24846 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
24847 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
24850 TEST_F(FormatTest, AlternativeOperators) {
24851 // Test case for ensuring alternate operators are not
24852 // combined with their right most neighbour.
24853 verifyFormat("int a and b;");
24854 verifyFormat("int a and_eq b;");
24855 verifyFormat("int a bitand b;");
24856 verifyFormat("int a bitor b;");
24857 verifyFormat("int a compl b;");
24858 verifyFormat("int a not b;");
24859 verifyFormat("int a not_eq b;");
24860 verifyFormat("int a or b;");
24861 verifyFormat("int a xor b;");
24862 verifyFormat("int a xor_eq b;");
24863 verifyFormat("return this not_eq bitand other;");
24864 verifyFormat("bool operator not_eq(const X bitand other)");
24866 verifyFormat("int a and 5;");
24867 verifyFormat("int a and_eq 5;");
24868 verifyFormat("int a bitand 5;");
24869 verifyFormat("int a bitor 5;");
24870 verifyFormat("int a compl 5;");
24871 verifyFormat("int a not 5;");
24872 verifyFormat("int a not_eq 5;");
24873 verifyFormat("int a or 5;");
24874 verifyFormat("int a xor 5;");
24875 verifyFormat("int a xor_eq 5;");
24877 verifyFormat("int a compl(5);");
24878 verifyFormat("int a not(5);");
24880 verifyFormat("compl foo();"); // ~foo();
24881 verifyFormat("foo() <%%>"); // foo() {}
24882 verifyFormat("void foo() <%%>"); // void foo() {}
24883 verifyFormat("int a<:1:>;"); // int a[1];
24884 verifyFormat("%:define ABC abc"); // #define ABC abc
24885 verifyFormat("%:%:"); // ##
24887 verifyFormat("a = v(not;);\n"
24888 "b = v(not+);\n"
24889 "c = v(not x);\n"
24890 "d = v(not 1);\n"
24891 "e = v(not 123.f);");
24893 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
24894 " V(and) \\\n"
24895 " V(not) \\\n"
24896 " V(not!) \\\n"
24897 " V(other)",
24898 getLLVMStyleWithColumns(40));
24901 TEST_F(FormatTest, STLWhileNotDefineChed) {
24902 verifyFormat("#if defined(while)\n"
24903 "#define while EMIT WARNING C4005\n"
24904 "#endif // while");
24907 TEST_F(FormatTest, OperatorSpacing) {
24908 FormatStyle Style = getLLVMStyle();
24909 Style.PointerAlignment = FormatStyle::PAS_Right;
24910 verifyFormat("Foo::operator*();", Style);
24911 verifyFormat("Foo::operator void *();", Style);
24912 verifyFormat("Foo::operator void **();", Style);
24913 verifyFormat("Foo::operator void *&();", Style);
24914 verifyFormat("Foo::operator void *&&();", Style);
24915 verifyFormat("Foo::operator void const *();", Style);
24916 verifyFormat("Foo::operator void const **();", Style);
24917 verifyFormat("Foo::operator void const *&();", Style);
24918 verifyFormat("Foo::operator void const *&&();", Style);
24919 verifyFormat("Foo::operator()(void *);", Style);
24920 verifyFormat("Foo::operator*(void *);", Style);
24921 verifyFormat("Foo::operator*();", Style);
24922 verifyFormat("Foo::operator**();", Style);
24923 verifyFormat("Foo::operator&();", Style);
24924 verifyFormat("Foo::operator<int> *();", Style);
24925 verifyFormat("Foo::operator<Foo> *();", Style);
24926 verifyFormat("Foo::operator<int> **();", Style);
24927 verifyFormat("Foo::operator<Foo> **();", Style);
24928 verifyFormat("Foo::operator<int> &();", Style);
24929 verifyFormat("Foo::operator<Foo> &();", Style);
24930 verifyFormat("Foo::operator<int> &&();", Style);
24931 verifyFormat("Foo::operator<Foo> &&();", Style);
24932 verifyFormat("Foo::operator<int> *&();", Style);
24933 verifyFormat("Foo::operator<Foo> *&();", Style);
24934 verifyFormat("Foo::operator<int> *&&();", Style);
24935 verifyFormat("Foo::operator<Foo> *&&();", Style);
24936 verifyFormat("operator*(int (*)(), class Foo);", Style);
24938 verifyFormat("Foo::operator&();", Style);
24939 verifyFormat("Foo::operator void &();", Style);
24940 verifyFormat("Foo::operator void const &();", Style);
24941 verifyFormat("Foo::operator()(void &);", Style);
24942 verifyFormat("Foo::operator&(void &);", Style);
24943 verifyFormat("Foo::operator&();", Style);
24944 verifyFormat("operator&(int (&)(), class Foo);", Style);
24945 verifyFormat("operator&&(int (&)(), class Foo);", Style);
24947 verifyFormat("Foo::operator&&();", Style);
24948 verifyFormat("Foo::operator**();", Style);
24949 verifyFormat("Foo::operator void &&();", Style);
24950 verifyFormat("Foo::operator void const &&();", Style);
24951 verifyFormat("Foo::operator()(void &&);", Style);
24952 verifyFormat("Foo::operator&&(void &&);", Style);
24953 verifyFormat("Foo::operator&&();", Style);
24954 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24955 verifyFormat("operator const nsTArrayRight<E> &()", Style);
24956 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24957 Style);
24958 verifyFormat("operator void **()", Style);
24959 verifyFormat("operator const FooRight<Object> &()", Style);
24960 verifyFormat("operator const FooRight<Object> *()", Style);
24961 verifyFormat("operator const FooRight<Object> **()", Style);
24962 verifyFormat("operator const FooRight<Object> *&()", Style);
24963 verifyFormat("operator const FooRight<Object> *&&()", Style);
24965 Style.PointerAlignment = FormatStyle::PAS_Left;
24966 verifyFormat("Foo::operator*();", Style);
24967 verifyFormat("Foo::operator**();", Style);
24968 verifyFormat("Foo::operator void*();", Style);
24969 verifyFormat("Foo::operator void**();", Style);
24970 verifyFormat("Foo::operator void*&();", Style);
24971 verifyFormat("Foo::operator void*&&();", Style);
24972 verifyFormat("Foo::operator void const*();", Style);
24973 verifyFormat("Foo::operator void const**();", Style);
24974 verifyFormat("Foo::operator void const*&();", Style);
24975 verifyFormat("Foo::operator void const*&&();", Style);
24976 verifyFormat("Foo::operator/*comment*/ void*();", Style);
24977 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
24978 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
24979 verifyFormat("Foo::operator()(void*);", Style);
24980 verifyFormat("Foo::operator*(void*);", Style);
24981 verifyFormat("Foo::operator*();", Style);
24982 verifyFormat("Foo::operator<int>*();", Style);
24983 verifyFormat("Foo::operator<Foo>*();", Style);
24984 verifyFormat("Foo::operator<int>**();", Style);
24985 verifyFormat("Foo::operator<Foo>**();", Style);
24986 verifyFormat("Foo::operator<Foo>*&();", Style);
24987 verifyFormat("Foo::operator<int>&();", Style);
24988 verifyFormat("Foo::operator<Foo>&();", Style);
24989 verifyFormat("Foo::operator<int>&&();", Style);
24990 verifyFormat("Foo::operator<Foo>&&();", Style);
24991 verifyFormat("Foo::operator<int>*&();", Style);
24992 verifyFormat("Foo::operator<Foo>*&();", Style);
24993 verifyFormat("operator*(int (*)(), class Foo);", Style);
24995 verifyFormat("Foo::operator&();", Style);
24996 verifyFormat("Foo::operator void&();", Style);
24997 verifyFormat("Foo::operator void const&();", Style);
24998 verifyFormat("Foo::operator/*comment*/ void&();", Style);
24999 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
25000 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
25001 verifyFormat("Foo::operator()(void&);", Style);
25002 verifyFormat("Foo::operator&(void&);", Style);
25003 verifyFormat("Foo::operator&();", Style);
25004 verifyFormat("operator&(int (&)(), class Foo);", Style);
25005 verifyFormat("operator&(int (&&)(), class Foo);", Style);
25006 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25008 verifyFormat("Foo::operator&&();", Style);
25009 verifyFormat("Foo::operator void&&();", Style);
25010 verifyFormat("Foo::operator void const&&();", Style);
25011 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
25012 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
25013 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
25014 verifyFormat("Foo::operator()(void&&);", Style);
25015 verifyFormat("Foo::operator&&(void&&);", Style);
25016 verifyFormat("Foo::operator&&();", Style);
25017 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25018 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
25019 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25020 Style);
25021 verifyFormat("operator void**()", Style);
25022 verifyFormat("operator const FooLeft<Object>&()", Style);
25023 verifyFormat("operator const FooLeft<Object>*()", Style);
25024 verifyFormat("operator const FooLeft<Object>**()", Style);
25025 verifyFormat("operator const FooLeft<Object>*&()", Style);
25026 verifyFormat("operator const FooLeft<Object>*&&()", Style);
25028 // PR45107
25029 verifyFormat("operator Vector<String>&();", Style);
25030 verifyFormat("operator const Vector<String>&();", Style);
25031 verifyFormat("operator foo::Bar*();", Style);
25032 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
25033 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25034 Style);
25036 Style.PointerAlignment = FormatStyle::PAS_Middle;
25037 verifyFormat("Foo::operator*();", Style);
25038 verifyFormat("Foo::operator void *();", Style);
25039 verifyFormat("Foo::operator()(void *);", Style);
25040 verifyFormat("Foo::operator*(void *);", Style);
25041 verifyFormat("Foo::operator*();", Style);
25042 verifyFormat("operator*(int (*)(), class Foo);", Style);
25044 verifyFormat("Foo::operator&();", Style);
25045 verifyFormat("Foo::operator void &();", Style);
25046 verifyFormat("Foo::operator void const &();", Style);
25047 verifyFormat("Foo::operator()(void &);", Style);
25048 verifyFormat("Foo::operator&(void &);", Style);
25049 verifyFormat("Foo::operator&();", Style);
25050 verifyFormat("operator&(int (&)(), class Foo);", Style);
25052 verifyFormat("Foo::operator&&();", Style);
25053 verifyFormat("Foo::operator void &&();", Style);
25054 verifyFormat("Foo::operator void const &&();", Style);
25055 verifyFormat("Foo::operator()(void &&);", Style);
25056 verifyFormat("Foo::operator&&(void &&);", Style);
25057 verifyFormat("Foo::operator&&();", Style);
25058 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25061 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
25062 FormatStyle Style = getLLVMStyle();
25063 // PR46157
25064 verifyFormat("foo(operator+, -42);", Style);
25065 verifyFormat("foo(operator++, -42);", Style);
25066 verifyFormat("foo(operator--, -42);", Style);
25067 verifyFormat("foo(-42, operator--);", Style);
25068 verifyFormat("foo(-42, operator, );", Style);
25069 verifyFormat("foo(operator, , -42);", Style);
25072 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
25073 FormatStyle Style = getLLVMStyle();
25074 Style.WhitespaceSensitiveMacros.push_back("FOO");
25076 // Newlines are important here.
25077 verifyNoChange("FOO(1+2 )\n", Style);
25078 verifyNoChange("FOO(a:b:c)\n", Style);
25080 // Don't use the helpers here, since 'mess up' will change the whitespace
25081 // and these are all whitespace sensitive by definition
25082 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
25083 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
25084 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
25085 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25086 " Still=Intentional);",
25087 Style);
25088 Style.AlignConsecutiveAssignments.Enabled = true;
25089 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25090 " Still=Intentional);",
25091 Style);
25093 Style.ColumnLimit = 21;
25094 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
25097 TEST_F(FormatTest, SkipMacroDefinitionBody) {
25098 auto Style = getLLVMStyle();
25099 Style.SkipMacroDefinitionBody = true;
25101 verifyFormat("#define A", "#define A", Style);
25102 verifyFormat("#define A a aa", "#define A a aa", Style);
25103 verifyNoChange("#define A b", Style);
25104 verifyNoChange("#define A ( args )", Style);
25105 verifyNoChange("#define A ( args ) = func ( args )", Style);
25106 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
25107 verifyNoChange("#define A ( args ) \\\n"
25108 " {\\\n"
25109 " int a = 1 ;\\\n"
25110 "}",
25111 Style);
25113 verifyNoChange("#define A x:", Style);
25114 verifyNoChange("#define A a. b", Style);
25116 // Surrounded with formatted code.
25117 verifyFormat("int a;\n"
25118 "#define A a\n"
25119 "int a;",
25120 "int a ;\n"
25121 "#define A a\n"
25122 "int a ;",
25123 Style);
25125 // Columns are not broken when a limit is set.
25126 Style.ColumnLimit = 10;
25127 verifyFormat("#define A a a a a", " # define A a a a a ", Style);
25128 verifyNoChange("#define A a a a a", Style);
25130 Style.ColumnLimit = 15;
25131 verifyFormat("#define A // a\n"
25132 " // very\n"
25133 " // long\n"
25134 " // comment",
25135 "#define A //a very long comment", Style);
25136 Style.ColumnLimit = 0;
25138 // Multiline definition.
25139 verifyNoChange("#define A \\\n"
25140 "Line one with spaces . \\\n"
25141 " Line two.",
25142 Style);
25143 verifyNoChange("#define A \\\n"
25144 "a a \\\n"
25145 "a \\\n"
25146 "a",
25147 Style);
25148 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
25149 verifyNoChange("#define A \\\n"
25150 "a a \\\n"
25151 "a \\\n"
25152 "a",
25153 Style);
25154 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
25155 verifyNoChange("#define A \\\n"
25156 "a a \\\n"
25157 "a \\\n"
25158 "a",
25159 Style);
25161 // Adjust indendations but don't change the definition.
25162 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25163 verifyNoChange("#if A\n"
25164 "#define A a\n"
25165 "#endif",
25166 Style);
25167 verifyFormat("#if A\n"
25168 "#define A a\n"
25169 "#endif",
25170 "#if A\n"
25171 " #define A a\n"
25172 "#endif",
25173 Style);
25174 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
25175 verifyNoChange("#if A\n"
25176 "# define A a\n"
25177 "#endif",
25178 Style);
25179 verifyFormat("#if A\n"
25180 "# define A a\n"
25181 "#endif",
25182 "#if A\n"
25183 " #define A a\n"
25184 "#endif",
25185 Style);
25186 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
25187 verifyNoChange("#if A\n"
25188 " #define A a\n"
25189 "#endif",
25190 Style);
25191 verifyFormat("#if A\n"
25192 " #define A a\n"
25193 "#endif",
25194 "#if A\n"
25195 " # define A a\n"
25196 "#endif",
25197 Style);
25199 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25200 // SkipMacroDefinitionBody should not affect other PP directives
25201 verifyFormat("#if !defined(A)\n"
25202 "#define A a\n"
25203 "#endif",
25204 "#if ! defined ( A )\n"
25205 " #define A a\n"
25206 "#endif",
25207 Style);
25209 // With comments.
25210 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25211 Style);
25212 verifyNoChange("/* */ #define A a // a a", Style);
25214 verifyFormat("int a; // a\n"
25215 "#define A // a\n"
25216 "int aaa; // a",
25217 "int a; // a\n"
25218 "#define A // a\n"
25219 "int aaa; // a",
25220 Style);
25222 verifyNoChange(
25223 "#define MACRO_WITH_COMMENTS() \\\n"
25224 " public: \\\n"
25225 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
25226 " static MyType getClassTypeId(); \\\n"
25227 " /** Normal comment for the following method. */ \\\n"
25228 " virtual MyType getTypeId() const;",
25229 Style);
25231 // multiline macro definitions
25232 verifyNoChange("#define A a\\\n"
25233 " A a \\\n "
25234 " A a",
25235 Style);
25238 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
25239 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
25240 // test its interaction with line wrapping
25241 FormatStyle Style = getLLVMStyleWithColumns(80);
25242 verifyFormat("namespace {\n"
25243 "int i;\n"
25244 "int j;\n"
25245 "} // namespace",
25246 Style);
25248 verifyFormat("namespace AAA {\n"
25249 "int i;\n"
25250 "int j;\n"
25251 "} // namespace AAA",
25252 Style);
25254 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
25255 "int i;\n"
25256 "int j;\n"
25257 "} // namespace Averyveryveryverylongnamespace",
25258 "namespace Averyveryveryverylongnamespace {\n"
25259 "int i;\n"
25260 "int j;\n"
25261 "}",
25262 Style);
25264 verifyFormat(
25265 "namespace "
25266 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25267 " went::mad::now {\n"
25268 "int i;\n"
25269 "int j;\n"
25270 "} // namespace\n"
25271 " // "
25272 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25273 "went::mad::now",
25274 "namespace "
25275 "would::it::save::you::a::lot::of::time::if_::i::"
25276 "just::gave::up::and_::went::mad::now {\n"
25277 "int i;\n"
25278 "int j;\n"
25279 "}",
25280 Style);
25282 // This used to duplicate the comment again and again on subsequent runs
25283 verifyFormat(
25284 "namespace "
25285 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25286 " went::mad::now {\n"
25287 "int i;\n"
25288 "int j;\n"
25289 "} // namespace\n"
25290 " // "
25291 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25292 "went::mad::now",
25293 "namespace "
25294 "would::it::save::you::a::lot::of::time::if_::i::"
25295 "just::gave::up::and_::went::mad::now {\n"
25296 "int i;\n"
25297 "int j;\n"
25298 "} // namespace\n"
25299 " // "
25300 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
25301 "and_::went::mad::now",
25302 Style);
25305 TEST_F(FormatTest, LikelyUnlikely) {
25306 FormatStyle Style = getLLVMStyle();
25308 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25309 " return 29;\n"
25310 "}",
25311 Style);
25313 verifyFormat("if (argc > 5) [[likely]] {\n"
25314 " return 29;\n"
25315 "}",
25316 Style);
25318 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25319 " return 29;\n"
25320 "} else [[likely]] {\n"
25321 " return 42;\n"
25322 "}",
25323 Style);
25325 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25326 " return 29;\n"
25327 "} else if (argc > 10) [[likely]] {\n"
25328 " return 99;\n"
25329 "} else {\n"
25330 " return 42;\n"
25331 "}",
25332 Style);
25334 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
25335 " return 29;\n"
25336 "}",
25337 Style);
25339 verifyFormat("if (argc > 5) [[unlikely]]\n"
25340 " return 29;",
25341 Style);
25342 verifyFormat("if (argc > 5) [[likely]]\n"
25343 " return 29;",
25344 Style);
25346 verifyFormat("while (limit > 0) [[unlikely]] {\n"
25347 " --limit;\n"
25348 "}",
25349 Style);
25350 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
25351 " --limit;\n"
25352 "}",
25353 Style);
25355 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
25356 " --limit;",
25357 Style);
25358 verifyFormat("while (limit > 0) [[likely]]\n"
25359 " --limit;",
25360 Style);
25362 Style.AttributeMacros.push_back("UNLIKELY");
25363 Style.AttributeMacros.push_back("LIKELY");
25364 verifyFormat("if (argc > 5) UNLIKELY\n"
25365 " return 29;",
25366 Style);
25368 verifyFormat("if (argc > 5) UNLIKELY {\n"
25369 " return 29;\n"
25370 "}",
25371 Style);
25372 verifyFormat("if (argc > 5) UNLIKELY {\n"
25373 " return 29;\n"
25374 "} else [[likely]] {\n"
25375 " return 42;\n"
25376 "}",
25377 Style);
25378 verifyFormat("if (argc > 5) UNLIKELY {\n"
25379 " return 29;\n"
25380 "} else LIKELY {\n"
25381 " return 42;\n"
25382 "}",
25383 Style);
25384 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25385 " return 29;\n"
25386 "} else LIKELY {\n"
25387 " return 42;\n"
25388 "}",
25389 Style);
25391 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25392 " --limit;\n"
25393 "}",
25394 Style);
25395 verifyFormat("while (limit > 0) LIKELY {\n"
25396 " --limit;\n"
25397 "}",
25398 Style);
25400 verifyFormat("while (limit > 0) UNLIKELY\n"
25401 " --limit;",
25402 Style);
25403 verifyFormat("for (auto &limit : limits) LIKELY\n"
25404 " --limit;",
25405 Style);
25408 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
25409 verifyFormat("Constructor()\n"
25410 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25411 " aaaa(aaaaaaaaaaaaaaaaaa, "
25412 "aaaaaaaaaaaaaaaaaat))");
25413 verifyFormat("Constructor()\n"
25414 " : aaaaaaaaaaaaa(aaaaaa), "
25415 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25417 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
25418 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
25419 verifyFormat("Constructor()\n"
25420 " : aaaaaa(aaaaaa),\n"
25421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25422 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25423 StyleWithWhitespacePenalty);
25424 verifyFormat("Constructor()\n"
25425 " : aaaaaaaaaaaaa(aaaaaa), "
25426 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25427 StyleWithWhitespacePenalty);
25430 TEST_F(FormatTest, LLVMDefaultStyle) {
25431 FormatStyle Style = getLLVMStyle();
25432 verifyFormat("extern \"C\" {\n"
25433 "int foo();\n"
25434 "}",
25435 Style);
25437 TEST_F(FormatTest, GNUDefaultStyle) {
25438 FormatStyle Style = getGNUStyle();
25439 verifyFormat("extern \"C\"\n"
25440 "{\n"
25441 " int foo ();\n"
25442 "}",
25443 Style);
25445 TEST_F(FormatTest, MozillaDefaultStyle) {
25446 FormatStyle Style = getMozillaStyle();
25447 verifyFormat("extern \"C\"\n"
25448 "{\n"
25449 " int foo();\n"
25450 "}",
25451 Style);
25453 TEST_F(FormatTest, GoogleDefaultStyle) {
25454 FormatStyle Style = getGoogleStyle();
25455 verifyFormat("extern \"C\" {\n"
25456 "int foo();\n"
25457 "}",
25458 Style);
25460 TEST_F(FormatTest, ChromiumDefaultStyle) {
25461 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
25462 verifyFormat("extern \"C\" {\n"
25463 "int foo();\n"
25464 "}",
25465 Style);
25467 TEST_F(FormatTest, MicrosoftDefaultStyle) {
25468 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
25469 verifyFormat("extern \"C\"\n"
25470 "{\n"
25471 " int foo();\n"
25472 "}",
25473 Style);
25475 TEST_F(FormatTest, WebKitDefaultStyle) {
25476 FormatStyle Style = getWebKitStyle();
25477 verifyFormat("extern \"C\" {\n"
25478 "int foo();\n"
25479 "}",
25480 Style);
25483 TEST_F(FormatTest, Concepts) {
25484 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
25485 FormatStyle::BBCDS_Always);
25487 // The default in LLVM style is REI_OuterScope, but these tests were written
25488 // when the default was REI_Keyword.
25489 FormatStyle Style = getLLVMStyle();
25490 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25492 verifyFormat("template <typename T>\n"
25493 "concept True = true;");
25495 verifyFormat("template <typename T>\n"
25496 "concept C = ((false || foo()) && C2<T>) ||\n"
25497 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25498 getLLVMStyleWithColumns(60));
25500 verifyFormat("template <typename T>\n"
25501 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25502 "sizeof(T) <= 8;");
25504 verifyFormat("template <typename T>\n"
25505 "concept DelayedCheck = true && requires(T t) {\n"
25506 " t.bar();\n"
25507 " t.baz();\n"
25508 " } && sizeof(T) <= 8;",
25509 Style);
25511 verifyFormat("template <typename T>\n"
25512 "concept DelayedCheck = true && requires(T t) { // Comment\n"
25513 " t.bar();\n"
25514 " t.baz();\n"
25515 " } && sizeof(T) <= 8;",
25516 Style);
25518 verifyFormat("template <typename T>\n"
25519 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25520 "sizeof(T) <= 8;");
25522 verifyFormat("template <typename T>\n"
25523 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25525 verifyFormat("template <typename T>\n"
25526 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25528 verifyFormat("template <typename T>\n"
25529 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25531 verifyFormat("template <typename T>\n"
25532 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25533 "&& sizeof(T) <= 8;");
25535 verifyFormat("template <typename T>\n"
25536 "concept DelayedCheck =\n"
25537 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25538 "sizeof(T) <= 8;");
25540 verifyFormat("template <typename T>\n"
25541 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25542 "&& sizeof(T) <= 8;");
25544 verifyFormat(
25545 "template <typename T>\n"
25546 "concept DelayedCheck =\n"
25547 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25549 verifyFormat("template <typename T>\n"
25550 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25551 "&& sizeof(T) <= 8;");
25553 verifyFormat("template <typename T>\n"
25554 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25555 "sizeof(T) <= 8;");
25557 verifyFormat("template <typename T>\n"
25558 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25559 " requires(T t) {\n"
25560 " t.bar();\n"
25561 " t.baz();\n"
25562 " } && sizeof(T) <= 8 && !(4 < 3);",
25563 getLLVMStyleWithColumns(60));
25565 verifyFormat("template <typename T>\n"
25566 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25568 verifyFormat("template <typename T>\n"
25569 "concept C = foo();");
25571 verifyFormat("template <typename T>\n"
25572 "concept C = foo(T());");
25574 verifyFormat("template <typename T>\n"
25575 "concept C = foo(T{});");
25577 verifyFormat("template <typename T>\n"
25578 "concept Size = V<sizeof(T)>::Value > 5;");
25580 verifyFormat("template <typename T>\n"
25581 "concept True = S<T>::Value;");
25583 verifyFormat("template <S T>\n"
25584 "concept True = T.field;");
25586 verifyFormat(
25587 "template <typename T>\n"
25588 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25589 " sizeof(T) <= 8;");
25591 // FIXME: This is misformatted because the fake l paren starts at bool, not at
25592 // the lambda l square.
25593 verifyFormat("template <typename T>\n"
25594 "concept C = [] -> bool { return true; }() && requires(T t) { "
25595 "t.bar(); } &&\n"
25596 " sizeof(T) <= 8;");
25598 verifyFormat(
25599 "template <typename T>\n"
25600 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25601 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25603 verifyFormat("template <typename T>\n"
25604 "concept C = decltype([]() { return std::true_type{}; "
25605 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25606 getLLVMStyleWithColumns(120));
25608 verifyFormat("template <typename T>\n"
25609 "concept C = decltype([]() -> std::true_type { return {}; "
25610 "}())::value &&\n"
25611 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25613 verifyFormat("template <typename T>\n"
25614 "concept C = true;\n"
25615 "Foo Bar;");
25617 verifyFormat("template <typename T>\n"
25618 "concept Hashable = requires(T a) {\n"
25619 " { std::hash<T>{}(a) } -> "
25620 "std::convertible_to<std::size_t>;\n"
25621 " };",
25622 Style);
25624 verifyFormat(
25625 "template <typename T>\n"
25626 "concept EqualityComparable = requires(T a, T b) {\n"
25627 " { a == b } -> std::same_as<bool>;\n"
25628 " };",
25629 Style);
25631 verifyFormat(
25632 "template <typename T>\n"
25633 "concept EqualityComparable = requires(T a, T b) {\n"
25634 " { a == b } -> std::same_as<bool>;\n"
25635 " { a != b } -> std::same_as<bool>;\n"
25636 " };",
25637 Style);
25639 verifyFormat("template <typename T>\n"
25640 "concept WeakEqualityComparable = requires(T a, T b) {\n"
25641 " { a == b };\n"
25642 " { a != b };\n"
25643 " };",
25644 Style);
25646 verifyFormat("template <typename T>\n"
25647 "concept HasSizeT = requires { typename T::size_t; };");
25649 verifyFormat("template <typename T>\n"
25650 "concept Semiregular =\n"
25651 " DefaultConstructible<T> && CopyConstructible<T> && "
25652 "CopyAssignable<T> &&\n"
25653 " requires(T a, std::size_t n) {\n"
25654 " requires Same<T *, decltype(&a)>;\n"
25655 " { a.~T() } noexcept;\n"
25656 " requires Same<T *, decltype(new T)>;\n"
25657 " requires Same<T *, decltype(new T[n])>;\n"
25658 " { delete new T; };\n"
25659 " { delete new T[n]; };\n"
25660 " };",
25661 Style);
25663 verifyFormat("template <typename T>\n"
25664 "concept Semiregular =\n"
25665 " requires(T a, std::size_t n) {\n"
25666 " requires Same<T *, decltype(&a)>;\n"
25667 " { a.~T() } noexcept;\n"
25668 " requires Same<T *, decltype(new T)>;\n"
25669 " requires Same<T *, decltype(new T[n])>;\n"
25670 " { delete new T; };\n"
25671 " { delete new T[n]; };\n"
25672 " { new T } -> std::same_as<T *>;\n"
25673 " } && DefaultConstructible<T> && CopyConstructible<T> && "
25674 "CopyAssignable<T>;",
25675 Style);
25677 verifyFormat(
25678 "template <typename T>\n"
25679 "concept Semiregular =\n"
25680 " DefaultConstructible<T> && 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 "
25685 "T[n])>;\n"
25686 " { delete new T; };\n"
25687 " { delete new T[n]; };\n"
25688 " } && CopyConstructible<T> && "
25689 "CopyAssignable<T>;",
25690 Style);
25692 verifyFormat("template <typename T>\n"
25693 "concept Two = requires(T t) {\n"
25694 " { t.foo() } -> std::same_as<Bar>;\n"
25695 " } && requires(T &&t) {\n"
25696 " { t.foo() } -> std::same_as<Bar &&>;\n"
25697 " };",
25698 Style);
25700 verifyFormat(
25701 "template <typename T>\n"
25702 "concept C = requires(T x) {\n"
25703 " { *x } -> std::convertible_to<typename T::inner>;\n"
25704 " { x + 1 } noexcept -> std::same_as<int>;\n"
25705 " { x * 1 } -> std::convertible_to<T>;\n"
25706 " };",
25707 Style);
25709 verifyFormat("template <typename T>\n"
25710 "concept C = requires(T x) {\n"
25711 " {\n"
25712 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25713 " } -> long_long_concept_name<T>;\n"
25714 " {\n"
25715 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25716 " } noexcept -> long_long_concept_name<T>;\n"
25717 " };",
25718 Style);
25720 verifyFormat(
25721 "template <typename T, typename U = T>\n"
25722 "concept Swappable = requires(T &&t, U &&u) {\n"
25723 " swap(std::forward<T>(t), std::forward<U>(u));\n"
25724 " swap(std::forward<U>(u), std::forward<T>(t));\n"
25725 " };",
25726 Style);
25728 verifyFormat("template <typename T, typename U>\n"
25729 "concept Common = requires(T &&t, U &&u) {\n"
25730 " typename CommonType<T, U>;\n"
25731 " { CommonType<T, U>(std::forward<T>(t)) };\n"
25732 " };",
25733 Style);
25735 verifyFormat("template <typename T, typename U>\n"
25736 "concept Common = requires(T &&t, U &&u) {\n"
25737 " typename CommonType<T, U>;\n"
25738 " { CommonType<T, U>{std::forward<T>(t)} };\n"
25739 " };",
25740 Style);
25742 verifyFormat(
25743 "template <typename T>\n"
25744 "concept C = requires(T t) {\n"
25745 " requires Bar<T> && Foo<T>;\n"
25746 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25747 " };",
25748 Style);
25750 verifyFormat("template <typename T>\n"
25751 "concept HasFoo = requires(T t) {\n"
25752 " { t.foo() };\n"
25753 " t.foo();\n"
25754 " };\n"
25755 "template <typename T>\n"
25756 "concept HasBar = requires(T t) {\n"
25757 " { t.bar() };\n"
25758 " t.bar();\n"
25759 " };",
25760 Style);
25762 verifyFormat("template <typename T>\n"
25763 "concept Large = sizeof(T) > 10;");
25765 verifyFormat("template <typename T, typename U>\n"
25766 "concept FooableWith = requires(T t, U u) {\n"
25767 " typename T::foo_type;\n"
25768 " { t.foo(u) } -> typename T::foo_type;\n"
25769 " t++;\n"
25770 " };\n"
25771 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25772 Style);
25774 verifyFormat("template <typename T>\n"
25775 "concept Context = is_specialization_of_v<context, T>;");
25777 verifyFormat("template <typename T>\n"
25778 "concept Node = std::is_object_v<T>;");
25780 verifyFormat("template <class T>\n"
25781 "concept integral = __is_integral(T);");
25783 verifyFormat("template <class T>\n"
25784 "concept is2D = __array_extent(T, 1) == 2;");
25786 verifyFormat("template <class T>\n"
25787 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25789 verifyFormat("template <class T, class T2>\n"
25790 "concept Same = __is_same_as<T, T2>;");
25792 verifyFormat(
25793 "template <class _InIt, class _OutIt>\n"
25794 "concept _Can_reread_dest =\n"
25795 " std::forward_iterator<_OutIt> &&\n"
25796 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25798 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
25800 verifyFormat(
25801 "template <typename T>\n"
25802 "concept C = requires(T t) {\n"
25803 " requires Bar<T> && Foo<T>;\n"
25804 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25805 " };",
25806 Style);
25808 verifyFormat("template <typename T>\n"
25809 "concept HasFoo = requires(T t) {\n"
25810 " { t.foo() };\n"
25811 " t.foo();\n"
25812 " };\n"
25813 "template <typename T>\n"
25814 "concept HasBar = requires(T t) {\n"
25815 " { t.bar() };\n"
25816 " t.bar();\n"
25817 " };",
25818 Style);
25820 verifyFormat("template <typename T> concept True = true;", Style);
25822 verifyFormat("template <typename T>\n"
25823 "concept C = decltype([]() -> std::true_type { return {}; "
25824 "}())::value &&\n"
25825 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25826 Style);
25828 verifyFormat("template <typename T>\n"
25829 "concept Semiregular =\n"
25830 " DefaultConstructible<T> && CopyConstructible<T> && "
25831 "CopyAssignable<T> &&\n"
25832 " requires(T a, std::size_t n) {\n"
25833 " requires Same<T *, decltype(&a)>;\n"
25834 " { a.~T() } noexcept;\n"
25835 " requires Same<T *, decltype(new T)>;\n"
25836 " requires Same<T *, decltype(new T[n])>;\n"
25837 " { delete new T; };\n"
25838 " { delete new T[n]; };\n"
25839 " };",
25840 Style);
25842 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
25844 verifyFormat("template <typename T> concept C =\n"
25845 " requires(T t) {\n"
25846 " requires Bar<T> && Foo<T>;\n"
25847 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25848 " };",
25849 Style);
25851 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25852 " { t.foo() };\n"
25853 " t.foo();\n"
25854 " };\n"
25855 "template <typename T> concept HasBar = requires(T t) {\n"
25856 " { t.bar() };\n"
25857 " t.bar();\n"
25858 " };",
25859 Style);
25861 verifyFormat("template <typename T> concept True = true;", Style);
25863 verifyFormat(
25864 "template <typename T> concept C =\n"
25865 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
25866 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25867 Style);
25869 verifyFormat("template <typename T> concept Semiregular =\n"
25870 " DefaultConstructible<T> && CopyConstructible<T> && "
25871 "CopyAssignable<T> &&\n"
25872 " requires(T a, std::size_t n) {\n"
25873 " requires Same<T *, decltype(&a)>;\n"
25874 " { a.~T() } noexcept;\n"
25875 " requires Same<T *, decltype(new T)>;\n"
25876 " requires Same<T *, decltype(new T[n])>;\n"
25877 " { delete new T; };\n"
25878 " { delete new T[n]; };\n"
25879 " };",
25880 Style);
25882 // The following tests are invalid C++, we just want to make sure we don't
25883 // assert.
25884 verifyNoCrash("template <typename T>\n"
25885 "concept C = requires C2<T>;");
25887 verifyNoCrash("template <typename T>\n"
25888 "concept C = 5 + 4;");
25890 verifyNoCrash("template <typename T>\n"
25891 "concept C = class X;");
25893 verifyNoCrash("template <typename T>\n"
25894 "concept C = [] && true;");
25896 verifyNoCrash("template <typename T>\n"
25897 "concept C = [] && requires(T t) { typename T::size_type; };");
25900 TEST_F(FormatTest, RequiresClausesPositions) {
25901 auto Style = getLLVMStyle();
25902 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
25903 EXPECT_EQ(Style.IndentRequiresClause, true);
25905 // The default in LLVM style is REI_OuterScope, but these tests were written
25906 // when the default was REI_Keyword.
25907 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25909 verifyFormat("template <typename T>\n"
25910 " requires(Foo<T> && std::trait<T>)\n"
25911 "struct Bar;",
25912 Style);
25914 verifyFormat("template <typename T>\n"
25915 " requires(Foo<T> && std::trait<T>)\n"
25916 "class Bar {\n"
25917 "public:\n"
25918 " Bar(T t);\n"
25919 " bool baz();\n"
25920 "};",
25921 Style);
25923 verifyFormat(
25924 "template <typename T>\n"
25925 " requires requires(T &&t) {\n"
25926 " typename T::I;\n"
25927 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
25928 " }\n"
25929 "Bar(T) -> Bar<typename T::I>;",
25930 Style);
25932 verifyFormat("template <typename T>\n"
25933 " requires(Foo<T> && std::trait<T>)\n"
25934 "constexpr T MyGlobal;",
25935 Style);
25937 verifyFormat("template <typename T>\n"
25938 " requires Foo<T> && requires(T t) {\n"
25939 " { t.baz() } -> std::same_as<bool>;\n"
25940 " requires std::same_as<T::Factor, int>;\n"
25941 " }\n"
25942 "inline int bar(T t) {\n"
25943 " return t.baz() ? T::Factor : 5;\n"
25944 "}",
25945 Style);
25947 verifyFormat("template <typename T>\n"
25948 "inline int bar(T t)\n"
25949 " requires Foo<T> && requires(T t) {\n"
25950 " { t.baz() } -> std::same_as<bool>;\n"
25951 " requires std::same_as<T::Factor, int>;\n"
25952 " }\n"
25953 "{\n"
25954 " return t.baz() ? T::Factor : 5;\n"
25955 "}",
25956 Style);
25958 verifyFormat("template <typename T>\n"
25959 " requires F<T>\n"
25960 "int bar(T t) {\n"
25961 " return 5;\n"
25962 "}",
25963 Style);
25965 verifyFormat("template <typename T>\n"
25966 "int bar(T t)\n"
25967 " requires F<T>\n"
25968 "{\n"
25969 " return 5;\n"
25970 "}",
25971 Style);
25973 verifyFormat("template <typename T>\n"
25974 "int S::bar(T t) &&\n"
25975 " requires F<T>\n"
25976 "{\n"
25977 " return 5;\n"
25978 "}",
25979 Style);
25981 verifyFormat("template <typename T>\n"
25982 "int bar(T t)\n"
25983 " requires F<T>;",
25984 Style);
25986 Style.IndentRequiresClause = false;
25987 verifyFormat("template <typename T>\n"
25988 "requires F<T>\n"
25989 "int bar(T t) {\n"
25990 " return 5;\n"
25991 "}",
25992 Style);
25994 verifyFormat("template <typename T>\n"
25995 "int S::bar(T t) &&\n"
25996 "requires F<T>\n"
25997 "{\n"
25998 " return 5;\n"
25999 "}",
26000 Style);
26002 verifyFormat("template <typename T>\n"
26003 "int bar(T t)\n"
26004 "requires F<T>\n"
26005 "{\n"
26006 " return 5;\n"
26007 "}",
26008 Style);
26010 Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace;
26011 Style.IndentRequiresClause = true;
26013 verifyFormat("template <typename T>\n"
26014 " requires(Foo<T> && std::trait<T>)\n"
26015 "struct Bar;",
26016 Style);
26018 verifyFormat("template <typename T>\n"
26019 " requires(Foo<T> && std::trait<T>)\n"
26020 "class Bar {\n"
26021 "public:\n"
26022 " Bar(T t);\n"
26023 " bool baz();\n"
26024 "};",
26025 Style);
26027 verifyFormat(
26028 "template <typename T>\n"
26029 " requires requires(T &&t) {\n"
26030 " typename T::I;\n"
26031 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26032 " }\n"
26033 "Bar(T) -> Bar<typename T::I>;",
26034 Style);
26036 verifyFormat("template <typename T>\n"
26037 " requires(Foo<T> && std::trait<T>)\n"
26038 "constexpr T MyGlobal;",
26039 Style);
26041 verifyFormat("template <typename T>\n"
26042 " requires Foo<T> && requires(T t) {\n"
26043 " { t.baz() } -> std::same_as<bool>;\n"
26044 " requires std::same_as<T::Factor, int>;\n"
26045 " }\n"
26046 "inline int bar(T t) {\n"
26047 " return t.baz() ? T::Factor : 5;\n"
26048 "}",
26049 Style);
26051 verifyFormat("template <typename T>\n"
26052 "inline int bar(T t)\n"
26053 " requires Foo<T> && requires(T t) {\n"
26054 " { t.baz() } -> std::same_as<bool>;\n"
26055 " requires std::same_as<T::Factor, int>;\n"
26056 " } {\n"
26057 " return t.baz() ? T::Factor : 5;\n"
26058 "}",
26059 Style);
26061 verifyFormat("template <typename T>\n"
26062 " requires F<T>\n"
26063 "int bar(T t) {\n"
26064 " return 5;\n"
26065 "}",
26066 Style);
26068 verifyFormat("template <typename T>\n"
26069 "int bar(T t)\n"
26070 " requires F<T> {\n"
26071 " return 5;\n"
26072 "}",
26073 Style);
26075 verifyFormat("template <typename T>\n"
26076 "int S::bar(T t) &&\n"
26077 " requires F<T> {\n"
26078 " return 5;\n"
26079 "}",
26080 Style);
26082 verifyFormat("template <typename T>\n"
26083 "int bar(T t)\n"
26084 " requires F<T>;",
26085 Style);
26087 verifyFormat("template <typename T>\n"
26088 "int bar(T t)\n"
26089 " requires F<T> {}",
26090 Style);
26092 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
26093 Style.IndentRequiresClause = false;
26094 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26095 "template <typename T> requires Foo<T> void bar() {}\n"
26096 "template <typename T> void bar() requires Foo<T> {}\n"
26097 "template <typename T> void bar() requires Foo<T>;\n"
26098 "template <typename T> void S::bar() && requires Foo<T> {}\n"
26099 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26100 Style);
26102 auto ColumnStyle = Style;
26103 ColumnStyle.ColumnLimit = 40;
26104 verifyFormat("template <typename AAAAAAA>\n"
26105 "requires Foo<T> struct Bar {};\n"
26106 "template <typename AAAAAAA>\n"
26107 "requires Foo<T> void bar() {}\n"
26108 "template <typename AAAAAAA>\n"
26109 "void bar() requires Foo<T> {}\n"
26110 "template <typename T>\n"
26111 "void S::bar() && requires Foo<T> {}\n"
26112 "template <typename AAAAAAA>\n"
26113 "requires Foo<T> Baz(T) -> Baz<T>;",
26114 ColumnStyle);
26116 verifyFormat("template <typename T>\n"
26117 "requires Foo<AAAAAAA> struct Bar {};\n"
26118 "template <typename T>\n"
26119 "requires Foo<AAAAAAA> void bar() {}\n"
26120 "template <typename T>\n"
26121 "void bar() requires Foo<AAAAAAA> {}\n"
26122 "template <typename T>\n"
26123 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26124 ColumnStyle);
26126 verifyFormat("template <typename AAAAAAA>\n"
26127 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26128 "struct Bar {};\n"
26129 "template <typename AAAAAAA>\n"
26130 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26131 "void bar() {}\n"
26132 "template <typename AAAAAAA>\n"
26133 "void bar()\n"
26134 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26135 "template <typename AAAAAAA>\n"
26136 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26137 "template <typename AAAAAAA>\n"
26138 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26139 "Bar(T) -> Bar<T>;",
26140 ColumnStyle);
26142 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26143 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26145 verifyFormat("template <typename T>\n"
26146 "requires Foo<T> struct Bar {};\n"
26147 "template <typename T>\n"
26148 "requires Foo<T> void bar() {}\n"
26149 "template <typename T>\n"
26150 "void bar()\n"
26151 "requires Foo<T> {}\n"
26152 "template <typename T>\n"
26153 "void bar()\n"
26154 "requires Foo<T>;\n"
26155 "template <typename T>\n"
26156 "void S::bar() &&\n"
26157 "requires Foo<T> {}\n"
26158 "template <typename T>\n"
26159 "requires Foo<T> Bar(T) -> Bar<T>;",
26160 Style);
26162 verifyFormat("template <typename AAAAAAA>\n"
26163 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26164 "struct Bar {};\n"
26165 "template <typename AAAAAAA>\n"
26166 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26167 "void bar() {}\n"
26168 "template <typename AAAAAAA>\n"
26169 "void bar()\n"
26170 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26171 "template <typename AAAAAAA>\n"
26172 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26173 "template <typename AAAAAAA>\n"
26174 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26175 "Bar(T) -> Bar<T>;",
26176 ColumnStyle);
26178 Style.IndentRequiresClause = true;
26179 ColumnStyle.IndentRequiresClause = true;
26181 verifyFormat("template <typename T>\n"
26182 " requires Foo<T> struct Bar {};\n"
26183 "template <typename T>\n"
26184 " requires Foo<T> void bar() {}\n"
26185 "template <typename T>\n"
26186 "void bar()\n"
26187 " requires Foo<T> {}\n"
26188 "template <typename T>\n"
26189 "void S::bar() &&\n"
26190 " requires Foo<T> {}\n"
26191 "template <typename T>\n"
26192 " requires Foo<T> Bar(T) -> Bar<T>;",
26193 Style);
26195 verifyFormat("template <typename AAAAAAA>\n"
26196 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26197 "struct Bar {};\n"
26198 "template <typename AAAAAAA>\n"
26199 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26200 "void bar() {}\n"
26201 "template <typename AAAAAAA>\n"
26202 "void bar()\n"
26203 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26204 "template <typename AAAAAAA>\n"
26205 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26206 "template <typename AAAAAAA>\n"
26207 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26208 "Bar(T) -> Bar<T>;",
26209 ColumnStyle);
26211 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26212 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
26214 verifyFormat("template <typename T> requires Foo<T>\n"
26215 "struct Bar {};\n"
26216 "template <typename T> requires Foo<T>\n"
26217 "void bar() {}\n"
26218 "template <typename T>\n"
26219 "void bar() requires Foo<T>\n"
26220 "{}\n"
26221 "template <typename T> void bar() requires Foo<T>;\n"
26222 "template <typename T>\n"
26223 "void S::bar() && requires Foo<T>\n"
26224 "{}\n"
26225 "template <typename T> requires Foo<T>\n"
26226 "Bar(T) -> Bar<T>;",
26227 Style);
26229 verifyFormat("template <typename AAAAAAA>\n"
26230 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26231 "struct Bar {};\n"
26232 "template <typename AAAAAAA>\n"
26233 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26234 "void bar() {}\n"
26235 "template <typename AAAAAAA>\n"
26236 "void bar()\n"
26237 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26238 "{}\n"
26239 "template <typename AAAAAAA>\n"
26240 "requires Foo<AAAAAAAA>\n"
26241 "Bar(T) -> Bar<T>;\n"
26242 "template <typename AAAAAAA>\n"
26243 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26244 "Bar(T) -> Bar<T>;",
26245 ColumnStyle);
26248 TEST_F(FormatTest, RequiresClauses) {
26249 verifyFormat("struct [[nodiscard]] zero_t {\n"
26250 " template <class T>\n"
26251 " requires requires { number_zero_v<T>; }\n"
26252 " [[nodiscard]] constexpr operator T() const {\n"
26253 " return number_zero_v<T>;\n"
26254 " }\n"
26255 "};");
26257 verifyFormat("template <class T>\n"
26258 " requires(std::same_as<int, T>)\n"
26259 "decltype(auto) fun() {}");
26261 auto Style = getLLVMStyle();
26263 verifyFormat(
26264 "template <typename T>\n"
26265 " requires is_default_constructible_v<hash<T>> and\n"
26266 " is_copy_constructible_v<hash<T>> and\n"
26267 " is_move_constructible_v<hash<T>> and\n"
26268 " is_copy_assignable_v<hash<T>> and "
26269 "is_move_assignable_v<hash<T>> and\n"
26270 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
26271 " is_callable_v<hash<T>(T)> and\n"
26272 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
26273 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
26274 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
26275 "struct S {};",
26276 Style);
26278 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
26279 verifyFormat(
26280 "template <typename T>\n"
26281 " requires is_default_constructible_v<hash<T>>\n"
26282 " and is_copy_constructible_v<hash<T>>\n"
26283 " and is_move_constructible_v<hash<T>>\n"
26284 " and is_copy_assignable_v<hash<T>> and "
26285 "is_move_assignable_v<hash<T>>\n"
26286 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
26287 " and is_callable_v<hash<T>(T)>\n"
26288 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
26289 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
26290 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
26291 "&>()))>\n"
26292 "struct S {};",
26293 Style);
26295 Style = getLLVMStyle();
26296 Style.ConstructorInitializerIndentWidth = 4;
26297 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
26298 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
26299 verifyFormat("constexpr Foo(Foo const &other)\n"
26300 " requires std::is_copy_constructible<T>\n"
26301 " : value{other.value} {\n"
26302 " do_magic();\n"
26303 " do_more_magic();\n"
26304 "}",
26305 Style);
26307 // Not a clause, but we once hit an assert.
26308 verifyFormat("#if 0\n"
26309 "#else\n"
26310 "foo();\n"
26311 "#endif\n"
26312 "bar(requires);");
26315 TEST_F(FormatTest, RequiresExpressionIndentation) {
26316 auto Style = getLLVMStyle();
26317 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
26319 verifyFormat("template <typename T>\n"
26320 "concept C = requires(T t) {\n"
26321 " typename T::value;\n"
26322 " requires requires(typename T::value v) {\n"
26323 " { t == v } -> std::same_as<bool>;\n"
26324 " };\n"
26325 "};",
26326 Style);
26328 verifyFormat("template <typename T>\n"
26329 "void bar(T)\n"
26330 " requires Foo<T> && requires(T t) {\n"
26331 " { t.foo() } -> std::same_as<int>;\n"
26332 " } && requires(T t) {\n"
26333 " { t.bar() } -> std::same_as<bool>;\n"
26334 " --t;\n"
26335 " };",
26336 Style);
26338 verifyFormat("template <typename T>\n"
26339 " requires Foo<T> &&\n"
26340 " requires(T t) {\n"
26341 " { t.foo() } -> std::same_as<int>;\n"
26342 " } && requires(T t) {\n"
26343 " { t.bar() } -> std::same_as<bool>;\n"
26344 " --t;\n"
26345 " }\n"
26346 "void bar(T);",
26347 Style);
26349 verifyFormat("template <typename T> void f() {\n"
26350 " if constexpr (requires(T t) {\n"
26351 " { t.bar() } -> std::same_as<bool>;\n"
26352 " }) {\n"
26353 " }\n"
26354 "}",
26355 Style);
26357 verifyFormat("template <typename T> void f() {\n"
26358 " if constexpr (condition && requires(T t) {\n"
26359 " { t.bar() } -> std::same_as<bool>;\n"
26360 " }) {\n"
26361 " }\n"
26362 "}",
26363 Style);
26365 verifyFormat("template <typename T> struct C {\n"
26366 " void f()\n"
26367 " requires requires(T t) {\n"
26368 " { t.bar() } -> std::same_as<bool>;\n"
26369 " };\n"
26370 "};",
26371 Style);
26373 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26375 verifyFormat("template <typename T>\n"
26376 "concept C = requires(T t) {\n"
26377 " typename T::value;\n"
26378 " requires requires(typename T::value v) {\n"
26379 " { t == v } -> std::same_as<bool>;\n"
26380 " };\n"
26381 " };",
26382 Style);
26384 verifyFormat(
26385 "template <typename T>\n"
26386 "void bar(T)\n"
26387 " requires Foo<T> && requires(T t) {\n"
26388 " { t.foo() } -> std::same_as<int>;\n"
26389 " } && requires(T t) {\n"
26390 " { t.bar() } -> std::same_as<bool>;\n"
26391 " --t;\n"
26392 " };",
26393 Style);
26395 verifyFormat("template <typename T>\n"
26396 " requires Foo<T> &&\n"
26397 " requires(T t) {\n"
26398 " { t.foo() } -> std::same_as<int>;\n"
26399 " } && requires(T t) {\n"
26400 " { t.bar() } -> std::same_as<bool>;\n"
26401 " --t;\n"
26402 " }\n"
26403 "void bar(T);",
26404 Style);
26406 verifyFormat("template <typename T> void f() {\n"
26407 " if constexpr (requires(T t) {\n"
26408 " { t.bar() } -> std::same_as<bool>;\n"
26409 " }) {\n"
26410 " }\n"
26411 "}",
26412 Style);
26414 verifyFormat(
26415 "template <typename T> void f() {\n"
26416 " if constexpr (condition && requires(T t) {\n"
26417 " { t.bar() } -> std::same_as<bool>;\n"
26418 " }) {\n"
26419 " }\n"
26420 "}",
26421 Style);
26423 verifyFormat("template <typename T> struct C {\n"
26424 " void f()\n"
26425 " requires requires(T t) {\n"
26426 " { t.bar() } -> std::same_as<bool>;\n"
26427 " };\n"
26428 "};",
26429 Style);
26432 TEST_F(FormatTest, StatementAttributeLikeMacros) {
26433 FormatStyle Style = getLLVMStyle();
26434 StringRef Source = "void Foo::slot() {\n"
26435 " unsigned char MyChar = 'x';\n"
26436 " emit signal(MyChar);\n"
26437 " Q_EMIT signal(MyChar);\n"
26438 "}";
26440 verifyFormat(Source, Style);
26442 Style.AlignConsecutiveDeclarations.Enabled = true;
26443 verifyFormat("void Foo::slot() {\n"
26444 " unsigned char MyChar = 'x';\n"
26445 " emit signal(MyChar);\n"
26446 " Q_EMIT signal(MyChar);\n"
26447 "}",
26448 Source, Style);
26450 Style.StatementAttributeLikeMacros.push_back("emit");
26451 verifyFormat(Source, Style);
26453 Style.StatementAttributeLikeMacros = {};
26454 verifyFormat("void Foo::slot() {\n"
26455 " unsigned char MyChar = 'x';\n"
26456 " emit signal(MyChar);\n"
26457 " Q_EMIT signal(MyChar);\n"
26458 "}",
26459 Source, Style);
26462 TEST_F(FormatTest, IndentAccessModifiers) {
26463 FormatStyle Style = getLLVMStyle();
26464 Style.IndentAccessModifiers = true;
26465 // Members are *two* levels below the record;
26466 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
26467 verifyFormat("class C {\n"
26468 " int i;\n"
26469 "};",
26470 Style);
26471 verifyFormat("union C {\n"
26472 " int i;\n"
26473 " unsigned u;\n"
26474 "};",
26475 Style);
26476 // Access modifiers should be indented one level below the record.
26477 verifyFormat("class C {\n"
26478 " public:\n"
26479 " int i;\n"
26480 "};",
26481 Style);
26482 verifyFormat("class C {\n"
26483 " public /* comment */:\n"
26484 " int i;\n"
26485 "};",
26486 Style);
26487 verifyFormat("struct S {\n"
26488 " private:\n"
26489 " class C {\n"
26490 " int j;\n"
26491 "\n"
26492 " public:\n"
26493 " C();\n"
26494 " };\n"
26495 "\n"
26496 " public:\n"
26497 " int i;\n"
26498 "};",
26499 Style);
26500 // Enumerations are not records and should be unaffected.
26501 Style.AllowShortEnumsOnASingleLine = false;
26502 verifyFormat("enum class E {\n"
26503 " A,\n"
26504 " B\n"
26505 "};",
26506 Style);
26507 // Test with a different indentation width;
26508 // also proves that the result is Style.AccessModifierOffset agnostic.
26509 Style.IndentWidth = 3;
26510 verifyFormat("class C {\n"
26511 " public:\n"
26512 " int i;\n"
26513 "};",
26514 Style);
26515 verifyFormat("class C {\n"
26516 " public /**/:\n"
26517 " int i;\n"
26518 "};",
26519 Style);
26520 Style.AttributeMacros.push_back("FOO");
26521 verifyFormat("class C {\n"
26522 " FOO public:\n"
26523 " int i;\n"
26524 "};",
26525 Style);
26528 TEST_F(FormatTest, LimitlessStringsAndComments) {
26529 auto Style = getLLVMStyleWithColumns(0);
26530 constexpr StringRef Code =
26531 "/**\n"
26532 " * This is a multiline comment with quite some long lines, at least for "
26533 "the LLVM Style.\n"
26534 " * We will redo this with strings and line comments. Just to check if "
26535 "everything is working.\n"
26536 " */\n"
26537 "bool foo() {\n"
26538 " /* Single line multi line comment. */\n"
26539 " const std::string String = \"This is a multiline string with quite "
26540 "some long lines, at least for the LLVM Style.\"\n"
26541 " \"We already did it with multi line "
26542 "comments, and we will do it with line comments. Just to check if "
26543 "everything is working.\";\n"
26544 " // This is a line comment (block) with quite some long lines, at "
26545 "least for the LLVM Style.\n"
26546 " // We already did this with multi line comments and strings. Just to "
26547 "check if everything is working.\n"
26548 " const std::string SmallString = \"Hello World\";\n"
26549 " // Small line comment\n"
26550 " return String.size() > SmallString.size();\n"
26551 "}";
26552 verifyNoChange(Code, Style);
26555 TEST_F(FormatTest, FormatDecayCopy) {
26556 // error cases from unit tests
26557 verifyFormat("foo(auto())");
26558 verifyFormat("foo(auto{})");
26559 verifyFormat("foo(auto({}))");
26560 verifyFormat("foo(auto{{}})");
26562 verifyFormat("foo(auto(1))");
26563 verifyFormat("foo(auto{1})");
26564 verifyFormat("foo(new auto(1))");
26565 verifyFormat("foo(new auto{1})");
26566 verifyFormat("decltype(auto(1)) x;");
26567 verifyFormat("decltype(auto{1}) x;");
26568 verifyFormat("auto(x);");
26569 verifyFormat("auto{x};");
26570 verifyFormat("new auto{x};");
26571 verifyFormat("auto{x} = y;");
26572 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26573 // the user's own fault
26574 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26575 // clearly the user's own fault
26576 verifyFormat("auto (*p)() = f;");
26579 TEST_F(FormatTest, Cpp20ModulesSupport) {
26580 FormatStyle Style = getLLVMStyle();
26581 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
26582 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
26584 verifyFormat("export import foo;", Style);
26585 verifyFormat("export import foo:bar;", Style);
26586 verifyFormat("export import foo.bar;", Style);
26587 verifyFormat("export import foo.bar:baz;", Style);
26588 verifyFormat("export import :bar;", Style);
26589 verifyFormat("export module foo:bar;", Style);
26590 verifyFormat("export module foo;", Style);
26591 verifyFormat("export module foo.bar;", Style);
26592 verifyFormat("export module foo.bar:baz;", Style);
26593 verifyFormat("export import <string_view>;", Style);
26594 verifyFormat("export import <Foo/Bar>;", Style);
26596 verifyFormat("export type_name var;", Style);
26597 verifyFormat("template <class T> export using A = B<T>;", Style);
26598 verifyFormat("export using A = B;", Style);
26599 verifyFormat("export int func() {\n"
26600 " foo();\n"
26601 "}",
26602 Style);
26603 verifyFormat("export struct {\n"
26604 " int foo;\n"
26605 "};",
26606 Style);
26607 verifyFormat("export {\n"
26608 " int foo;\n"
26609 "};",
26610 Style);
26611 verifyFormat("export export char const *hello() { return \"hello\"; }");
26613 verifyFormat("import bar;", Style);
26614 verifyFormat("import foo.bar;", Style);
26615 verifyFormat("import foo:bar;", Style);
26616 verifyFormat("import :bar;", Style);
26617 verifyFormat("import /* module partition */ :bar;", Style);
26618 verifyFormat("import <ctime>;", Style);
26619 verifyFormat("import \"header\";", Style);
26621 verifyFormat("module foo;", Style);
26622 verifyFormat("module foo:bar;", Style);
26623 verifyFormat("module foo.bar;", Style);
26624 verifyFormat("module;", Style);
26626 verifyFormat("export namespace hi {\n"
26627 "const char *sayhi();\n"
26628 "}",
26629 Style);
26631 verifyFormat("module :private;", Style);
26632 verifyFormat("import <foo/bar.h>;", Style);
26633 verifyFormat("import foo...bar;", Style);
26634 verifyFormat("import ..........;", Style);
26635 verifyFormat("module foo:private;", Style);
26636 verifyFormat("import a", Style);
26637 verifyFormat("module a", Style);
26638 verifyFormat("export import a", Style);
26639 verifyFormat("export module a", Style);
26641 verifyFormat("import", Style);
26642 verifyFormat("module", Style);
26643 verifyFormat("export", Style);
26645 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26648 TEST_F(FormatTest, CoroutineForCoawait) {
26649 FormatStyle Style = getLLVMStyle();
26650 verifyFormat("for co_await (auto x : range())\n ;");
26651 verifyFormat("for (auto i : arr) {\n"
26652 "}",
26653 Style);
26654 verifyFormat("for co_await (auto i : arr) {\n"
26655 "}",
26656 Style);
26657 verifyFormat("for co_await (auto i : foo(T{})) {\n"
26658 "}",
26659 Style);
26662 TEST_F(FormatTest, CoroutineCoAwait) {
26663 verifyFormat("int x = co_await foo();");
26664 verifyFormat("int x = (co_await foo());");
26665 verifyFormat("co_await (42);");
26666 verifyFormat("void operator co_await(int);");
26667 verifyFormat("void operator co_await(a);");
26668 verifyFormat("co_await a;");
26669 verifyFormat("co_await missing_await_resume{};");
26670 verifyFormat("co_await a; // comment");
26671 verifyFormat("void test0() { co_await a; }");
26672 verifyFormat("co_await co_await co_await foo();");
26673 verifyFormat("co_await foo().bar();");
26674 verifyFormat("co_await [this]() -> Task { co_return x; }");
26675 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26676 "foo(); }(x, y);");
26678 FormatStyle Style = getLLVMStyleWithColumns(40);
26679 verifyFormat("co_await [this](int a, int b) -> Task {\n"
26680 " co_return co_await foo();\n"
26681 "}(x, y);",
26682 Style);
26683 verifyFormat("co_await;");
26686 TEST_F(FormatTest, CoroutineCoYield) {
26687 verifyFormat("int x = co_yield foo();");
26688 verifyFormat("int x = (co_yield foo());");
26689 verifyFormat("co_yield (42);");
26690 verifyFormat("co_yield {42};");
26691 verifyFormat("co_yield 42;");
26692 verifyFormat("co_yield n++;");
26693 verifyFormat("co_yield ++n;");
26694 verifyFormat("co_yield;");
26697 TEST_F(FormatTest, CoroutineCoReturn) {
26698 verifyFormat("co_return (42);");
26699 verifyFormat("co_return;");
26700 verifyFormat("co_return {};");
26701 verifyFormat("co_return x;");
26702 verifyFormat("co_return co_await foo();");
26703 verifyFormat("co_return co_yield foo();");
26706 TEST_F(FormatTest, EmptyShortBlock) {
26707 auto Style = getLLVMStyle();
26708 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
26710 verifyFormat("try {\n"
26711 " doA();\n"
26712 "} catch (Exception &e) {\n"
26713 " e.printStackTrace();\n"
26714 "}",
26715 Style);
26717 verifyFormat("try {\n"
26718 " doA();\n"
26719 "} catch (Exception &e) {}",
26720 Style);
26723 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
26724 auto Style = getLLVMStyle();
26726 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
26727 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
26728 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
26729 verifyFormat("struct Y<[] { return 0; }> {};", Style);
26731 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
26732 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
26735 TEST_F(FormatTest, MultilineLambdaInConditional) {
26736 auto Style = getLLVMStyleWithColumns(70);
26737 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26738 " ;\n"
26739 " return 5;\n"
26740 "}()\n"
26741 " : 2;",
26742 Style);
26743 verifyFormat(
26744 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26745 " ;\n"
26746 " return 5;\n"
26747 "}();",
26748 Style);
26750 Style = getLLVMStyleWithColumns(60);
26751 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26752 " ? []() {\n"
26753 " ;\n"
26754 " return 5;\n"
26755 " }()\n"
26756 " : 2;",
26757 Style);
26758 verifyFormat("auto aLengthyIdentifier =\n"
26759 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
26760 " ;\n"
26761 " return 5;\n"
26762 " }();",
26763 Style);
26765 Style = getLLVMStyleWithColumns(40);
26766 verifyFormat("auto aLengthyIdentifier =\n"
26767 " oneExpressionSoThatWeBreak ? []() {\n"
26768 " ;\n"
26769 " return 5;\n"
26770 " }()\n"
26771 " : 2;",
26772 Style);
26773 verifyFormat("auto aLengthyIdentifier =\n"
26774 " oneExpressionSoThatWeBreak\n"
26775 " ? 2\n"
26776 " : []() {\n"
26777 " ;\n"
26778 " return 5;\n"
26779 " };",
26780 Style);
26783 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
26784 auto Style = getLLVMStyle();
26786 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
26787 "void functionDecl(int a, int b, int c);";
26789 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26790 "paramF, paramG, paramH, paramI);\n"
26791 "void functionDecl(int argumentA, int argumentB, int "
26792 "argumentC, int argumentD, int argumentE);";
26794 verifyFormat(Short, Style);
26796 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26797 "paramF, paramG, paramH,\n"
26798 " paramI);\n"
26799 "void functionDecl(int argumentA, int argumentB, int "
26800 "argumentC, int argumentD,\n"
26801 " int argumentE);";
26803 verifyFormat(NoBreak, Medium, Style);
26804 verifyFormat(NoBreak,
26805 "functionCall(\n"
26806 " paramA,\n"
26807 " paramB,\n"
26808 " paramC,\n"
26809 " paramD,\n"
26810 " paramE,\n"
26811 " paramF,\n"
26812 " paramG,\n"
26813 " paramH,\n"
26814 " paramI\n"
26815 ");\n"
26816 "void functionDecl(\n"
26817 " int argumentA,\n"
26818 " int argumentB,\n"
26819 " int argumentC,\n"
26820 " int argumentD,\n"
26821 " int argumentE\n"
26822 ");",
26823 Style);
26825 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26826 " nestedLongFunctionCall(argument1, "
26827 "argument2, argument3,\n"
26828 " argument4, "
26829 "argument5));",
26830 Style);
26832 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26834 verifyFormat(Short, Style);
26835 verifyFormat(
26836 "functionCall(\n"
26837 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26838 "paramI\n"
26839 ");\n"
26840 "void functionDecl(\n"
26841 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26842 "argumentE\n"
26843 ");",
26844 Medium, Style);
26846 Style.AllowAllArgumentsOnNextLine = false;
26847 Style.AllowAllParametersOfDeclarationOnNextLine = false;
26849 verifyFormat(Short, Style);
26850 verifyFormat(
26851 "functionCall(\n"
26852 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26853 "paramI\n"
26854 ");\n"
26855 "void functionDecl(\n"
26856 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26857 "argumentE\n"
26858 ");",
26859 Medium, Style);
26861 Style.BinPackArguments = false;
26862 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
26864 verifyFormat(Short, Style);
26866 verifyFormat("functionCall(\n"
26867 " paramA,\n"
26868 " paramB,\n"
26869 " paramC,\n"
26870 " paramD,\n"
26871 " paramE,\n"
26872 " paramF,\n"
26873 " paramG,\n"
26874 " paramH,\n"
26875 " paramI\n"
26876 ");\n"
26877 "void functionDecl(\n"
26878 " int argumentA,\n"
26879 " int argumentB,\n"
26880 " int argumentC,\n"
26881 " int argumentD,\n"
26882 " int argumentE\n"
26883 ");",
26884 Medium, Style);
26886 verifyFormat("outerFunctionCall(\n"
26887 " nestedFunctionCall(argument1),\n"
26888 " nestedLongFunctionCall(\n"
26889 " argument1,\n"
26890 " argument2,\n"
26891 " argument3,\n"
26892 " argument4,\n"
26893 " argument5\n"
26894 " )\n"
26895 ");",
26896 Style);
26898 verifyFormat("int a = (int)b;", Style);
26899 verifyFormat("int a = (int)b;",
26900 "int a = (\n"
26901 " int\n"
26902 ") b;",
26903 Style);
26905 verifyFormat("return (true);", Style);
26906 verifyFormat("return (true);",
26907 "return (\n"
26908 " true\n"
26909 ");",
26910 Style);
26912 verifyFormat("void foo();", Style);
26913 verifyFormat("void foo();",
26914 "void foo(\n"
26915 ");",
26916 Style);
26918 verifyFormat("void foo() {}", Style);
26919 verifyFormat("void foo() {}",
26920 "void foo(\n"
26921 ") {\n"
26922 "}",
26923 Style);
26925 verifyFormat("auto string = std::string();", Style);
26926 verifyFormat("auto string = std::string();",
26927 "auto string = std::string(\n"
26928 ");",
26929 Style);
26931 verifyFormat("void (*functionPointer)() = nullptr;", Style);
26932 verifyFormat("void (*functionPointer)() = nullptr;",
26933 "void (\n"
26934 " *functionPointer\n"
26935 ")\n"
26936 "(\n"
26937 ") = nullptr;",
26938 Style);
26941 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
26942 auto Style = getLLVMStyle();
26944 verifyFormat("if (foo()) {\n"
26945 " return;\n"
26946 "}",
26947 Style);
26949 verifyFormat("if (quiteLongArg !=\n"
26950 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26951 "comment\n"
26952 " return;\n"
26953 "}",
26954 Style);
26956 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26958 verifyFormat("if (foo()) {\n"
26959 " return;\n"
26960 "}",
26961 Style);
26963 verifyFormat("if (quiteLongArg !=\n"
26964 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26965 "comment\n"
26966 " return;\n"
26967 "}",
26968 Style);
26970 verifyFormat("void foo() {\n"
26971 " if (camelCaseName < alsoLongName ||\n"
26972 " anotherEvenLongerName <=\n"
26973 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
26974 "\n"
26975 " otherName < thisLastName) {\n"
26976 " return;\n"
26977 " } else if (quiteLongName < alsoLongName ||\n"
26978 " anotherEvenLongerName <=\n"
26979 " thisReallyReallyReallyReallyReallyReallyLonger"
26980 "Name ||\n"
26981 " otherName < thisLastName) {\n"
26982 " return;\n"
26983 " }\n"
26984 "}",
26985 Style);
26987 Style.ContinuationIndentWidth = 2;
26988 verifyFormat("void foo() {\n"
26989 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
26990 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26991 " if (false) {\n"
26992 " return;\n"
26993 " } else if (thisIsRatherALongIfClause && "
26994 "thatIExpectToBeBroken ||\n"
26995 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26996 " return;\n"
26997 " }\n"
26998 " }\n"
26999 "}",
27000 Style);
27003 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
27004 auto Style = getLLVMStyle();
27006 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27007 " doSomething();\n"
27008 "}",
27009 Style);
27011 verifyFormat("for (int myReallyLongCountVariable = 0; "
27012 "myReallyLongCountVariable < count;\n"
27013 " myReallyLongCountVariable++) {\n"
27014 " doSomething();\n"
27015 "}",
27016 Style);
27018 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27020 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27021 " doSomething();\n"
27022 "}",
27023 Style);
27025 verifyFormat("for (int myReallyLongCountVariable = 0; "
27026 "myReallyLongCountVariable < count;\n"
27027 " myReallyLongCountVariable++) {\n"
27028 " doSomething();\n"
27029 "}",
27030 Style);
27033 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
27034 auto Style = getLLVMStyleWithColumns(60);
27035 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27036 // Aggregate initialization.
27037 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27038 " 10000000, 20000000\n"
27039 "};",
27040 Style);
27041 verifyFormat("SomeStruct s{\n"
27042 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
27043 " \"zzzzzzzzzzzzzzzz\"\n"
27044 "};",
27045 Style);
27046 // Designated initializers.
27047 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27048 " [0] = 10000000, [1] = 20000000\n"
27049 "};",
27050 Style);
27051 verifyFormat("SomeStruct s{\n"
27052 " .foo = \"xxxxxxxxxxxxx\",\n"
27053 " .bar = \"yyyyyyyyyyyyy\",\n"
27054 " .baz = \"zzzzzzzzzzzzz\"\n"
27055 "};",
27056 Style);
27057 // List initialization.
27058 verifyFormat("SomeStruct s{\n"
27059 " \"xxxxxxxxxxxxx\",\n"
27060 " \"yyyyyyyyyyyyy\",\n"
27061 " \"zzzzzzzzzzzzz\",\n"
27062 "};",
27063 Style);
27064 verifyFormat("SomeStruct{\n"
27065 " \"xxxxxxxxxxxxx\",\n"
27066 " \"yyyyyyyyyyyyy\",\n"
27067 " \"zzzzzzzzzzzzz\",\n"
27068 "};",
27069 Style);
27070 verifyFormat("new SomeStruct{\n"
27071 " \"xxxxxxxxxxxxx\",\n"
27072 " \"yyyyyyyyyyyyy\",\n"
27073 " \"zzzzzzzzzzzzz\",\n"
27074 "};",
27075 Style);
27076 // Member initializer.
27077 verifyFormat("class SomeClass {\n"
27078 " SomeStruct s{\n"
27079 " \"xxxxxxxxxxxxx\",\n"
27080 " \"yyyyyyyyyyyyy\",\n"
27081 " \"zzzzzzzzzzzzz\",\n"
27082 " };\n"
27083 "};",
27084 Style);
27085 // Constructor member initializer.
27086 verifyFormat("SomeClass::SomeClass : strct{\n"
27087 " \"xxxxxxxxxxxxx\",\n"
27088 " \"yyyyyyyyyyyyy\",\n"
27089 " \"zzzzzzzzzzzzz\",\n"
27090 " } {}",
27091 Style);
27092 // Copy initialization.
27093 verifyFormat("SomeStruct s = SomeStruct{\n"
27094 " \"xxxxxxxxxxxxx\",\n"
27095 " \"yyyyyyyyyyyyy\",\n"
27096 " \"zzzzzzzzzzzzz\",\n"
27097 "};",
27098 Style);
27099 // Copy list initialization.
27100 verifyFormat("SomeStruct s = {\n"
27101 " \"xxxxxxxxxxxxx\",\n"
27102 " \"yyyyyyyyyyyyy\",\n"
27103 " \"zzzzzzzzzzzzz\",\n"
27104 "};",
27105 Style);
27106 // Assignment operand initialization.
27107 verifyFormat("s = {\n"
27108 " \"xxxxxxxxxxxxx\",\n"
27109 " \"yyyyyyyyyyyyy\",\n"
27110 " \"zzzzzzzzzzzzz\",\n"
27111 "};",
27112 Style);
27113 // Returned object initialization.
27114 verifyFormat("return {\n"
27115 " \"xxxxxxxxxxxxx\",\n"
27116 " \"yyyyyyyyyyyyy\",\n"
27117 " \"zzzzzzzzzzzzz\",\n"
27118 "};",
27119 Style);
27120 // Initializer list.
27121 verifyFormat("auto initializerList = {\n"
27122 " \"xxxxxxxxxxxxx\",\n"
27123 " \"yyyyyyyyyyyyy\",\n"
27124 " \"zzzzzzzzzzzzz\",\n"
27125 "};",
27126 Style);
27127 // Function parameter initialization.
27128 verifyFormat("func({\n"
27129 " \"xxxxxxxxxxxxx\",\n"
27130 " \"yyyyyyyyyyyyy\",\n"
27131 " \"zzzzzzzzzzzzz\",\n"
27132 "});",
27133 Style);
27134 // Nested init lists.
27135 verifyFormat("SomeStruct s = {\n"
27136 " {{init1, init2, init3, init4, init5},\n"
27137 " {init1, init2, init3, init4, init5}}\n"
27138 "};",
27139 Style);
27140 verifyFormat("SomeStruct s = {\n"
27141 " {{\n"
27142 " .init1 = 1,\n"
27143 " .init2 = 2,\n"
27144 " .init3 = 3,\n"
27145 " .init4 = 4,\n"
27146 " .init5 = 5,\n"
27147 " },\n"
27148 " {init1, init2, init3, init4, init5}}\n"
27149 "};",
27150 Style);
27151 verifyFormat("SomeArrayT a[3] = {\n"
27152 " {\n"
27153 " foo,\n"
27154 " bar,\n"
27155 " },\n"
27156 " {\n"
27157 " foo,\n"
27158 " bar,\n"
27159 " },\n"
27160 " SomeArrayT{},\n"
27161 "};",
27162 Style);
27163 verifyFormat("SomeArrayT a[3] = {\n"
27164 " {foo},\n"
27165 " {\n"
27166 " {\n"
27167 " init1,\n"
27168 " init2,\n"
27169 " init3,\n"
27170 " },\n"
27171 " {\n"
27172 " init1,\n"
27173 " init2,\n"
27174 " init3,\n"
27175 " },\n"
27176 " },\n"
27177 " {baz},\n"
27178 "};",
27179 Style);
27182 TEST_F(FormatTest, UnderstandsDigraphs) {
27183 verifyFormat("int arr<:5:> = {};");
27184 verifyFormat("int arr[5] = <%%>;");
27185 verifyFormat("int arr<:::qualified_variable:> = {};");
27186 verifyFormat("int arr[::qualified_variable] = <%%>;");
27187 verifyFormat("%:include <header>");
27188 verifyFormat("%:define A x##y");
27189 verifyFormat("#define A x%:%:y");
27192 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
27193 auto Style = getLLVMStyle();
27194 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27195 Style.AlignConsecutiveAssignments.Enabled = true;
27196 Style.AlignConsecutiveDeclarations.Enabled = true;
27198 // The AlignArray code is incorrect for non square Arrays and can cause
27199 // crashes, these tests assert that the array is not changed but will
27200 // also act as regression tests for when it is properly fixed
27201 verifyFormat("struct test demo[] = {\n"
27202 " {1, 2},\n"
27203 " {3, 4, 5},\n"
27204 " {6, 7, 8}\n"
27205 "};",
27206 Style);
27207 verifyFormat("struct test demo[] = {\n"
27208 " {1, 2, 3, 4, 5},\n"
27209 " {3, 4, 5},\n"
27210 " {6, 7, 8}\n"
27211 "};",
27212 Style);
27213 verifyFormat("struct test demo[] = {\n"
27214 " {1, 2, 3, 4, 5},\n"
27215 " {3, 4, 5},\n"
27216 " {6, 7, 8, 9, 10, 11, 12}\n"
27217 "};",
27218 Style);
27219 verifyFormat("struct test demo[] = {\n"
27220 " {1, 2, 3},\n"
27221 " {3, 4, 5},\n"
27222 " {6, 7, 8, 9, 10, 11, 12}\n"
27223 "};",
27224 Style);
27226 verifyFormat("S{\n"
27227 " {},\n"
27228 " {},\n"
27229 " {a, b}\n"
27230 "};",
27231 Style);
27232 verifyFormat("S{\n"
27233 " {},\n"
27234 " {},\n"
27235 " {a, b},\n"
27236 "};",
27237 Style);
27238 verifyFormat("void foo() {\n"
27239 " auto thing = test{\n"
27240 " {\n"
27241 " {13}, {something}, // A\n"
27242 " }\n"
27243 " };\n"
27244 "}",
27245 "void foo() {\n"
27246 " auto thing = test{\n"
27247 " {\n"
27248 " {13},\n"
27249 " {something}, // A\n"
27250 " }\n"
27251 " };\n"
27252 "}",
27253 Style);
27256 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
27257 auto Style = getLLVMStyle();
27258 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
27259 Style.AlignConsecutiveAssignments.Enabled = true;
27260 Style.AlignConsecutiveDeclarations.Enabled = true;
27262 // The AlignArray code is incorrect for non square Arrays and can cause
27263 // crashes, these tests assert that the array is not changed but will
27264 // also act as regression tests for when it is properly fixed
27265 verifyFormat("struct test demo[] = {\n"
27266 " {1, 2},\n"
27267 " {3, 4, 5},\n"
27268 " {6, 7, 8}\n"
27269 "};",
27270 Style);
27271 verifyFormat("struct test demo[] = {\n"
27272 " {1, 2, 3, 4, 5},\n"
27273 " {3, 4, 5},\n"
27274 " {6, 7, 8}\n"
27275 "};",
27276 Style);
27277 verifyFormat("struct test demo[] = {\n"
27278 " {1, 2, 3, 4, 5},\n"
27279 " {3, 4, 5},\n"
27280 " {6, 7, 8, 9, 10, 11, 12}\n"
27281 "};",
27282 Style);
27283 verifyFormat("struct test demo[] = {\n"
27284 " {1, 2, 3},\n"
27285 " {3, 4, 5},\n"
27286 " {6, 7, 8, 9, 10, 11, 12}\n"
27287 "};",
27288 Style);
27290 verifyFormat("S{\n"
27291 " {},\n"
27292 " {},\n"
27293 " {a, b}\n"
27294 "};",
27295 Style);
27296 verifyFormat("S{\n"
27297 " {},\n"
27298 " {},\n"
27299 " {a, b},\n"
27300 "};",
27301 Style);
27302 verifyFormat("void foo() {\n"
27303 " auto thing = test{\n"
27304 " {\n"
27305 " {13}, {something}, // A\n"
27306 " }\n"
27307 " };\n"
27308 "}",
27309 "void foo() {\n"
27310 " auto thing = test{\n"
27311 " {\n"
27312 " {13},\n"
27313 " {something}, // A\n"
27314 " }\n"
27315 " };\n"
27316 "}",
27317 Style);
27320 TEST_F(FormatTest, FormatsVariableTemplates) {
27321 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27322 verifyFormat("template <typename T> "
27323 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27326 TEST_F(FormatTest, RemoveSemicolon) {
27327 FormatStyle Style = getLLVMStyle();
27328 Style.RemoveSemicolon = true;
27330 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27331 "int max(int a, int b) { return a > b ? a : b; };", Style);
27333 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27334 "int max(int a, int b) { return a > b ? a : b; };;", Style);
27336 verifyFormat("class Foo {\n"
27337 " int getSomething() const { return something; }\n"
27338 "};",
27339 "class Foo {\n"
27340 " int getSomething() const { return something; };\n"
27341 "};",
27342 Style);
27344 verifyFormat("class Foo {\n"
27345 " int getSomething() const { return something; }\n"
27346 "};",
27347 "class Foo {\n"
27348 " int getSomething() const { return something; };;\n"
27349 "};",
27350 Style);
27352 verifyFormat("for (;;) {\n"
27353 "}",
27354 Style);
27356 verifyFormat("class [[deprecated(\"\")]] C {\n"
27357 " int i;\n"
27358 "};",
27359 Style);
27361 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27362 " int i;\n"
27363 "};",
27364 Style);
27366 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
27368 verifyFormat("void main() {}", "void main() {};", Style);
27370 verifyFormat("struct Foo {\n"
27371 " Foo() {}\n"
27372 " ~Foo() {}\n"
27373 "};",
27374 "struct Foo {\n"
27375 " Foo() {};\n"
27376 " ~Foo() {};\n"
27377 "};",
27378 Style);
27380 // We can't (and probably shouldn't) support the following.
27381 #if 0
27382 verifyFormat("void foo() {} //\n"
27383 "int bar;",
27384 "void foo() {}; //\n"
27385 "; int bar;",
27386 Style);
27387 #endif
27389 Style.TypenameMacros.push_back("STRUCT");
27390 verifyFormat("STRUCT(T, B) { int i; };", Style);
27393 TEST_F(FormatTest, BreakAfterAttributes) {
27394 constexpr StringRef Code("[[maybe_unused]] const int i;\n"
27395 "[[foo([[]])]] [[maybe_unused]]\n"
27396 "int j;\n"
27397 "[[maybe_unused]]\n"
27398 "foo<int> k;\n"
27399 "[[nodiscard]] inline int f(int &i);\n"
27400 "[[foo([[]])]] [[nodiscard]]\n"
27401 "int g(int &i);\n"
27402 "[[nodiscard]]\n"
27403 "inline int f(int &i) {\n"
27404 " i = 1;\n"
27405 " return 0;\n"
27406 "}\n"
27407 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27408 " i = 0;\n"
27409 " return 1;\n"
27410 "}");
27412 FormatStyle Style = getLLVMStyle();
27413 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
27414 verifyNoChange(Code, Style);
27416 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27417 verifyFormat("[[maybe_unused]] const int i;\n"
27418 "[[foo([[]])]] [[maybe_unused]] int j;\n"
27419 "[[maybe_unused]] foo<int> k;\n"
27420 "[[nodiscard]] inline int f(int &i);\n"
27421 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27422 "[[nodiscard]] inline int f(int &i) {\n"
27423 " i = 1;\n"
27424 " return 0;\n"
27425 "}\n"
27426 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27427 " i = 0;\n"
27428 " return 1;\n"
27429 "}",
27430 Code, Style);
27432 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27433 verifyFormat("[[maybe_unused]]\n"
27434 "const int i;\n"
27435 "[[foo([[]])]] [[maybe_unused]]\n"
27436 "int j;\n"
27437 "[[maybe_unused]]\n"
27438 "foo<int> k;\n"
27439 "[[nodiscard]]\n"
27440 "inline int f(int &i);\n"
27441 "[[foo([[]])]] [[nodiscard]]\n"
27442 "int g(int &i);\n"
27443 "[[nodiscard]]\n"
27444 "inline int f(int &i) {\n"
27445 " i = 1;\n"
27446 " return 0;\n"
27447 "}\n"
27448 "[[foo([[]])]] [[nodiscard]]\n"
27449 "int g(int &i) {\n"
27450 " i = 0;\n"
27451 " return 1;\n"
27452 "}",
27453 Code, Style);
27455 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
27456 " f();\n"
27457 "else\n"
27458 " g();\n"
27459 "[[foo([[]])]]\n"
27460 "switch (b) {\n"
27461 "[[unlikely]] case 1:\n"
27462 " ++b;\n"
27463 " break;\n"
27464 "[[likely]]\n"
27465 "default:\n"
27466 " return;\n"
27467 "}\n"
27468 "[[unlikely]] for (; c > 0; --c)\n"
27469 " h();\n"
27470 "[[likely]]\n"
27471 "while (d > 0)\n"
27472 " --d;");
27474 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
27475 verifyNoChange(CtrlStmtCode, Style);
27477 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
27478 verifyFormat("[[likely]] if (a)\n"
27479 " f();\n"
27480 "else\n"
27481 " g();\n"
27482 "[[foo([[]])]] switch (b) {\n"
27483 "[[unlikely]] case 1:\n"
27484 " ++b;\n"
27485 " break;\n"
27486 "[[likely]] default:\n"
27487 " return;\n"
27488 "}\n"
27489 "[[unlikely]] for (; c > 0; --c)\n"
27490 " h();\n"
27491 "[[likely]] while (d > 0)\n"
27492 " --d;",
27493 CtrlStmtCode, Style);
27495 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27496 verifyFormat("[[likely]]\n"
27497 "if (a)\n"
27498 " f();\n"
27499 "else\n"
27500 " g();\n"
27501 "[[foo([[]])]]\n"
27502 "switch (b) {\n"
27503 "[[unlikely]]\n"
27504 "case 1:\n"
27505 " ++b;\n"
27506 " break;\n"
27507 "[[likely]]\n"
27508 "default:\n"
27509 " return;\n"
27510 "}\n"
27511 "[[unlikely]]\n"
27512 "for (; c > 0; --c)\n"
27513 " h();\n"
27514 "[[likely]]\n"
27515 "while (d > 0)\n"
27516 " --d;",
27517 CtrlStmtCode, Style);
27519 constexpr StringRef CtorDtorCode("struct Foo {\n"
27520 " [[deprecated]] Foo();\n"
27521 " [[deprecated]] Foo() {}\n"
27522 " [[deprecated]] ~Foo();\n"
27523 " [[deprecated]] ~Foo() {}\n"
27524 " [[deprecated]] void f();\n"
27525 " [[deprecated]] void f() {}\n"
27526 "};\n"
27527 "[[deprecated]] Bar::Bar() {}\n"
27528 "[[deprecated]] Bar::~Bar() {}\n"
27529 "[[deprecated]] void g() {}");
27530 verifyFormat("struct Foo {\n"
27531 " [[deprecated]]\n"
27532 " Foo();\n"
27533 " [[deprecated]]\n"
27534 " Foo() {}\n"
27535 " [[deprecated]]\n"
27536 " ~Foo();\n"
27537 " [[deprecated]]\n"
27538 " ~Foo() {}\n"
27539 " [[deprecated]]\n"
27540 " void f();\n"
27541 " [[deprecated]]\n"
27542 " void f() {}\n"
27543 "};\n"
27544 "[[deprecated]]\n"
27545 "Bar::Bar() {}\n"
27546 "[[deprecated]]\n"
27547 "Bar::~Bar() {}\n"
27548 "[[deprecated]]\n"
27549 "void g() {}",
27550 CtorDtorCode, Style);
27552 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
27553 verifyFormat("struct Foo {\n"
27554 " [[deprecated]]\n"
27555 " Foo();\n"
27556 " [[deprecated]]\n"
27557 " Foo()\n"
27558 " {\n"
27559 " }\n"
27560 " [[deprecated]]\n"
27561 " ~Foo();\n"
27562 " [[deprecated]]\n"
27563 " ~Foo()\n"
27564 " {\n"
27565 " }\n"
27566 " [[deprecated]]\n"
27567 " void f();\n"
27568 " [[deprecated]]\n"
27569 " void f()\n"
27570 " {\n"
27571 " }\n"
27572 "};\n"
27573 "[[deprecated]]\n"
27574 "Bar::Bar()\n"
27575 "{\n"
27576 "}\n"
27577 "[[deprecated]]\n"
27578 "Bar::~Bar()\n"
27579 "{\n"
27580 "}\n"
27581 "[[deprecated]]\n"
27582 "void g()\n"
27583 "{\n"
27584 "}",
27585 CtorDtorCode, Style);
27587 verifyFormat("struct Foo {\n"
27588 " [[maybe_unused]]\n"
27589 " void operator+();\n"
27590 "};\n"
27591 "[[nodiscard]]\n"
27592 "Foo &operator-(Foo &);",
27593 Style);
27595 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
27596 verifyFormat("[[nodiscard]]\n"
27597 "Foo& operator-(Foo&);",
27598 Style);
27601 TEST_F(FormatTest, InsertNewlineAtEOF) {
27602 FormatStyle Style = getLLVMStyle();
27603 Style.InsertNewlineAtEOF = true;
27605 verifyNoChange("int i;\n", Style);
27606 verifyFormat("int i;\n", "int i;", Style);
27608 constexpr StringRef Code{"namespace {\n"
27609 "int i;\n"
27610 "} // namespace"};
27611 verifyFormat(Code.str() + '\n', Code, Style,
27612 {tooling::Range(19, 13)}); // line 3
27615 TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
27616 FormatStyle Style = getLLVMStyle();
27617 Style.KeepEmptyLines.AtEndOfFile = true;
27619 const StringRef Code{"int i;\n\n"};
27620 verifyNoChange(Code, Style);
27621 verifyFormat(Code, "int i;\n\n\n", Style);
27624 TEST_F(FormatTest, SpaceAfterUDL) {
27625 verifyFormat("auto c = (4s).count();");
27626 verifyFormat("auto x = 5s .count() == 5;");
27629 TEST_F(FormatTest, InterfaceAsClassMemberName) {
27630 verifyFormat("class Foo {\n"
27631 " int interface;\n"
27632 " Foo::Foo(int iface) : interface{iface} {}\n"
27633 "}");
27636 TEST_F(FormatTest, PreprocessorOverlappingRegions) {
27637 verifyFormat("#ifdef\n\n"
27638 "#else\n"
27639 "#endif",
27640 "#ifdef \n"
27641 " \n"
27642 "\n"
27643 "#else \n"
27644 "#endif ",
27645 getGoogleStyle());
27648 TEST_F(FormatTest, RemoveParentheses) {
27649 FormatStyle Style = getLLVMStyle();
27650 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
27652 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
27653 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
27654 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
27655 verifyFormat("decltype((foo->bar)) baz;", Style);
27656 verifyFormat("class __declspec(dllimport) X {};",
27657 "class __declspec((dllimport)) X {};", Style);
27658 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
27659 verifyFormat("while (a)\n"
27660 " b;",
27661 "while (((a)))\n"
27662 " b;",
27663 Style);
27664 verifyFormat("while ((a = b))\n"
27665 " c;",
27666 "while (((a = b)))\n"
27667 " c;",
27668 Style);
27669 verifyFormat("if (a)\n"
27670 " b;",
27671 "if (((a)))\n"
27672 " b;",
27673 Style);
27674 verifyFormat("if constexpr ((a = b))\n"
27675 " c;",
27676 "if constexpr (((a = b)))\n"
27677 " c;",
27678 Style);
27679 verifyFormat("if (({ a; }))\n"
27680 " b;",
27681 "if ((({ a; })))\n"
27682 " b;",
27683 Style);
27684 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27685 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27686 Style);
27687 verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
27688 verifyFormat("foo((a, b));", "foo(((a), b));", Style);
27689 verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
27690 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
27691 verifyFormat("return (0);", "return (((0)));", Style);
27692 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
27693 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27694 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27695 Style);
27697 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
27698 verifyFormat("#define Return0 return (0);", Style);
27699 verifyFormat("return 0;", "return (0);", Style);
27700 verifyFormat("co_return 0;", "co_return ((0));", Style);
27701 verifyFormat("return 0;", "return (((0)));", Style);
27702 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
27703 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27704 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27705 Style);
27706 verifyFormat("inline decltype(auto) f() {\n"
27707 " if (a) {\n"
27708 " return (a);\n"
27709 " }\n"
27710 " return (b);\n"
27711 "}",
27712 "inline decltype(auto) f() {\n"
27713 " if (a) {\n"
27714 " return ((a));\n"
27715 " }\n"
27716 " return ((b));\n"
27717 "}",
27718 Style);
27719 verifyFormat("auto g() {\n"
27720 " decltype(auto) x = [] {\n"
27721 " auto y = [] {\n"
27722 " if (a) {\n"
27723 " return a;\n"
27724 " }\n"
27725 " return b;\n"
27726 " };\n"
27727 " if (c) {\n"
27728 " return (c);\n"
27729 " }\n"
27730 " return (d);\n"
27731 " };\n"
27732 " if (e) {\n"
27733 " return e;\n"
27734 " }\n"
27735 " return f;\n"
27736 "}",
27737 "auto g() {\n"
27738 " decltype(auto) x = [] {\n"
27739 " auto y = [] {\n"
27740 " if (a) {\n"
27741 " return ((a));\n"
27742 " }\n"
27743 " return ((b));\n"
27744 " };\n"
27745 " if (c) {\n"
27746 " return ((c));\n"
27747 " }\n"
27748 " return ((d));\n"
27749 " };\n"
27750 " if (e) {\n"
27751 " return ((e));\n"
27752 " }\n"
27753 " return ((f));\n"
27754 "}",
27755 Style);
27757 Style.ColumnLimit = 25;
27758 verifyFormat("return (a + b) - (c + d);",
27759 "return (((a + b)) -\n"
27760 " ((c + d)));",
27761 Style);
27764 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
27765 auto Style = getLLVMStyleWithColumns(35);
27767 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
27768 verifyFormat("void foo(int arg1,\n"
27769 " double arg2) noexcept;",
27770 Style);
27772 // The following line does not fit within the 35 column limit, but that's what
27773 // happens with no break allowed.
27774 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27775 " noexcept(baz(arg1)) &&\n"
27776 " noexcept(baz(arg2)));",
27777 Style);
27779 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27780 Style);
27782 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
27783 verifyFormat("void foo(int arg1,\n"
27784 " double arg2) noexcept;",
27785 Style);
27787 verifyFormat("void bar(int arg1, double arg2)\n"
27788 " noexcept(noexcept(baz(arg1)) &&\n"
27789 " noexcept(baz(arg2)));",
27790 Style);
27792 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27793 " noexcept;",
27794 Style);
27796 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
27797 verifyFormat("void foo(int arg1,\n"
27798 " double arg2) noexcept;",
27799 Style);
27801 verifyFormat("void bar(int arg1, double arg2)\n"
27802 " noexcept(noexcept(baz(arg1)) &&\n"
27803 " noexcept(baz(arg2)));",
27804 Style);
27806 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27807 Style);
27810 TEST_F(FormatTest, PPBranchesInBracedInit) {
27811 verifyFormat("A a_{kFlag1,\n"
27812 "#if BUILD_FLAG\n"
27813 " kFlag2,\n"
27814 "#else\n"
27815 " kFlag3,\n"
27816 "#endif\n"
27817 " kFlag4};",
27818 "A a_{\n"
27819 " kFlag1,\n"
27820 "#if BUILD_FLAG\n"
27821 " kFlag2,\n"
27822 "#else\n"
27823 " kFlag3,\n"
27824 "#endif\n"
27825 " kFlag4\n"
27826 "};");
27829 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
27830 verifyFormat("{\n"
27831 " char *a[] = {\n"
27832 " /* abc */ \"abc\",\n"
27833 "#if FOO\n"
27834 " /* xyz */ \"xyz\",\n"
27835 "#endif\n"
27836 " /* last */ \"last\"};\n"
27837 "}",
27838 getLLVMStyleWithColumns(30));
27841 TEST_F(FormatTest, BreakAdjacentStringLiterals) {
27842 constexpr StringRef Code{
27843 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27845 verifyFormat("return \"Code\"\n"
27846 " \"\\0\\52\\26\\55\\55\\0\"\n"
27847 " \"x013\"\n"
27848 " \"\\02\\xBA\";",
27849 Code);
27851 auto Style = getLLVMStyle();
27852 Style.BreakAdjacentStringLiterals = false;
27853 verifyFormat(Code, Style);
27856 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
27857 verifyFormat(
27858 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
27859 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
27860 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
27862 auto Style = getLLVMStyle();
27863 Style.ColumnLimit = 15;
27864 verifyNoChange("#define test \\\n"
27865 " /* 测试 */ \\\n"
27866 " \"aa\" \\\n"
27867 " \"bb\"",
27868 Style);
27870 Style.ColumnLimit = 25;
27871 verifyFormat("struct foo {\n"
27872 " int iiiiii; ///< iiiiii\n"
27873 " int b; ///< ыыы\n"
27874 " int c; ///< ыыыы\n"
27875 "};",
27876 Style);
27878 Style.ColumnLimit = 35;
27879 verifyFormat("#define SENSOR_DESC_1 \\\n"
27880 " \"{\" \\\n"
27881 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
27882 " \"}\"",
27883 Style);
27885 Style.ColumnLimit = 80;
27886 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27887 verifyFormat("Languages languages = {\n"
27888 " Language{{'e', 'n'}, U\"Test English\" },\n"
27889 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
27890 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
27891 "};",
27892 Style);
27895 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
27896 verifyFormat("return .5;");
27897 verifyFormat("return not '5';");
27898 verifyFormat("return sizeof \"5\";");
27901 TEST_F(FormatTest, BreakBinaryOperations) {
27902 auto Style = getLLVMStyleWithColumns(60);
27903 EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never);
27905 // Logical operations
27906 verifyFormat("if (condition1 && condition2) {\n"
27907 "}",
27908 Style);
27910 verifyFormat("if (condition1 && condition2 &&\n"
27911 " (condition3 || condition4) && condition5 &&\n"
27912 " condition6) {\n"
27913 "}",
27914 Style);
27916 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27917 " loooooooooooooooooooooongcondition2) {\n"
27918 "}",
27919 Style);
27921 // Arithmetic
27922 verifyFormat("const int result = lhs + rhs;", Style);
27924 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
27925 " loooooooooooooooooooooongop3;",
27926 Style);
27928 verifyFormat("result = longOperand1 + longOperand2 -\n"
27929 " (longOperand3 + longOperand4) -\n"
27930 " longOperand5 * longOperand6;",
27931 Style);
27933 verifyFormat("const int result =\n"
27934 " operand1 + operand2 - (operand3 + operand4);",
27935 Style);
27937 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
27939 // Logical operations
27940 verifyFormat("if (condition1 && condition2) {\n"
27941 "}",
27942 Style);
27944 verifyFormat("if (condition1 && // comment\n"
27945 " condition2 &&\n"
27946 " (condition3 || condition4) && // comment\n"
27947 " condition5 &&\n"
27948 " condition6) {\n"
27949 "}",
27950 Style);
27952 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27953 " loooooooooooooooooooooongcondition2) {\n"
27954 "}",
27955 Style);
27957 // Arithmetic
27958 verifyFormat("const int result = lhs + rhs;", Style);
27960 verifyFormat("result = loooooooooooooooooooooongop1 +\n"
27961 " loooooooooooooooooooooongop2 +\n"
27962 " loooooooooooooooooooooongop3;",
27963 Style);
27965 verifyFormat("const int result =\n"
27966 " operand1 + operand2 - (operand3 + operand4);",
27967 Style);
27969 verifyFormat("result = longOperand1 +\n"
27970 " longOperand2 -\n"
27971 " (longOperand3 + longOperand4) -\n"
27972 " longOperand5 +\n"
27973 " longOperand6;",
27974 Style);
27976 verifyFormat("result = operand1 +\n"
27977 " operand2 -\n"
27978 " operand3 +\n"
27979 " operand4 -\n"
27980 " operand5 +\n"
27981 " operand6;",
27982 Style);
27984 // Ensure mixed precedence operations are handled properly
27985 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
27987 verifyFormat("result = operand1 +\n"
27988 " operand2 /\n"
27989 " operand3 +\n"
27990 " operand4 /\n"
27991 " operand5 *\n"
27992 " operand6;",
27993 Style);
27995 verifyFormat("result = operand1 *\n"
27996 " operand2 -\n"
27997 " operand3 *\n"
27998 " operand4 -\n"
27999 " operand5 +\n"
28000 " operand6;",
28001 Style);
28003 verifyFormat("result = operand1 *\n"
28004 " (operand2 - operand3 * operand4) -\n"
28005 " operand5 +\n"
28006 " operand6;",
28007 Style);
28009 verifyFormat("result = operand1.member *\n"
28010 " (operand2.member() - operand3->mem * operand4) -\n"
28011 " operand5.member() +\n"
28012 " operand6->member;",
28013 Style);
28015 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28016 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28018 verifyFormat("result = operand1 +\n"
28019 " operand2 / operand3 +\n"
28020 " operand4 / operand5 * operand6;",
28021 Style);
28023 verifyFormat("result = operand1 * operand2 -\n"
28024 " operand3 * operand4 -\n"
28025 " operand5 +\n"
28026 " operand6;",
28027 Style);
28029 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28030 " operand5 +\n"
28031 " operand6;",
28032 Style);
28034 verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28035 " byte_buffer[1] << 8 |\n"
28036 " byte_buffer[2] << 16 |\n"
28037 " byte_buffer[3] << 24;",
28038 Style);
28040 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28041 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
28043 // Logical operations
28044 verifyFormat("if (condition1 && condition2) {\n"
28045 "}",
28046 Style);
28048 verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28049 " && loooooooooooooooooooooongcondition2) {\n"
28050 "}",
28051 Style);
28053 // Arithmetic
28054 verifyFormat("const int result = lhs + rhs;", Style);
28056 verifyFormat("result = loooooooooooooooooooooongop1\n"
28057 " + loooooooooooooooooooooongop2\n"
28058 " + loooooooooooooooooooooongop3;",
28059 Style);
28061 verifyFormat("const int result =\n"
28062 " operand1 + operand2 - (operand3 + operand4);",
28063 Style);
28065 verifyFormat("result = longOperand1\n"
28066 " + longOperand2\n"
28067 " - (longOperand3 + longOperand4)\n"
28068 " - longOperand5\n"
28069 " + longOperand6;",
28070 Style);
28072 verifyFormat("result = operand1\n"
28073 " + operand2\n"
28074 " - operand3\n"
28075 " + operand4\n"
28076 " - operand5\n"
28077 " + operand6;",
28078 Style);
28080 // Ensure mixed precedence operations are handled properly
28081 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28083 verifyFormat("result = operand1\n"
28084 " + operand2\n"
28085 " / operand3\n"
28086 " + operand4\n"
28087 " / operand5\n"
28088 " * operand6;",
28089 Style);
28091 verifyFormat("result = operand1\n"
28092 " * operand2\n"
28093 " - operand3\n"
28094 " * operand4\n"
28095 " - operand5\n"
28096 " + operand6;",
28097 Style);
28099 verifyFormat("result = operand1\n"
28100 " * (operand2 - operand3 * operand4)\n"
28101 " - operand5\n"
28102 " + operand6;",
28103 Style);
28105 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28106 " | byte_buffer[1]\n"
28107 " << 8\n"
28108 " | byte_buffer[2]\n"
28109 " << 16\n"
28110 " | byte_buffer[3]\n"
28111 " << 24;",
28112 Style);
28114 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28115 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28117 verifyFormat("result = operand1\n"
28118 " + operand2 / operand3\n"
28119 " + operand4 / operand5 * operand6;",
28120 Style);
28122 verifyFormat("result = operand1 * operand2\n"
28123 " - operand3 * operand4\n"
28124 " - operand5\n"
28125 " + operand6;",
28126 Style);
28128 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
28129 " - operand5\n"
28130 " + operand6;",
28131 Style);
28133 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28134 " | byte_buffer[1] << 8\n"
28135 " | byte_buffer[2] << 16\n"
28136 " | byte_buffer[3] << 24;",
28137 Style);
28140 TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {
28141 auto Style = getLLVMStyle();
28142 Style.RemoveEmptyLinesInUnwrappedLines = true;
28144 verifyFormat("int c = a + b;",
28145 "int c\n"
28146 "\n"
28147 " = a + b;",
28148 Style);
28150 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
28151 "enum : unsigned\n"
28152 "\n"
28153 "{\n"
28154 " AA = 0,\n"
28155 " BB\n"
28156 "} myEnum;",
28157 Style);
28159 verifyFormat("class B : public E {\n"
28160 "private:\n"
28161 "};",
28162 "class B : public E\n"
28163 "\n"
28164 "{\n"
28165 "private:\n"
28166 "};",
28167 Style);
28169 verifyFormat(
28170 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
28171 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
28172 "\n"
28173 " 23, \"hello\"},\n"
28174 " {7, 5, \"!!\"}};",
28175 Style);
28177 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
28178 "int myFunction(\n"
28179 "\n"
28180 " int aaaaaaaaaaaaa,\n"
28181 "\n"
28182 " int ccccccccccccc, int d);",
28183 Style);
28185 verifyFormat("switch (e) {\n"
28186 "case 1:\n"
28187 " return e;\n"
28188 "case 2:\n"
28189 " return 2;\n"
28190 "}",
28191 "switch (\n"
28192 "\n"
28193 " e) {\n"
28194 "case 1:\n"
28195 " return e;\n"
28196 "case 2:\n"
28197 " return 2;\n"
28198 "}",
28199 Style);
28201 verifyFormat("while (true) {\n"
28202 "}",
28203 "while (\n"
28204 "\n"
28205 " true) {\n"
28206 "}",
28207 Style);
28209 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
28210 " std::map<int, std::string> *outputMap);",
28211 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
28212 "\n"
28213 " (std::map<int, std::string> *outputMap);",
28214 Style);
28217 TEST_F(FormatTest, KeepFormFeed) {
28218 auto Style = getLLVMStyle();
28219 Style.KeepFormFeed = true;
28221 constexpr StringRef NoFormFeed{"int i;\n"
28222 "\n"
28223 "void f();"};
28224 verifyFormat(NoFormFeed,
28225 "int i;\n"
28226 " \f\n"
28227 "void f();",
28228 Style);
28229 verifyFormat(NoFormFeed,
28230 "int i;\n"
28231 "\n"
28232 "\fvoid f();",
28233 Style);
28234 verifyFormat(NoFormFeed,
28235 "\fint i;\n"
28236 "\n"
28237 "void f();",
28238 Style);
28239 verifyFormat(NoFormFeed,
28240 "int i;\n"
28241 "\n"
28242 "void f();\f",
28243 Style);
28245 constexpr StringRef FormFeed{"int i;\n"
28246 "\f\n"
28247 "void f();"};
28248 verifyNoChange(FormFeed, Style);
28250 Style.LineEnding = FormatStyle::LE_LF;
28251 verifyFormat(FormFeed,
28252 "int i;\r\n"
28253 "\f\r\n"
28254 "void f();",
28255 Style);
28257 constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n"
28258 "\f\n"
28259 "\n"
28260 "void f();"};
28261 Style.MaxEmptyLinesToKeep = 2;
28262 verifyFormat(FormFeedBeforeEmptyLine,
28263 "int i;\n"
28264 "\n"
28265 "\f\n"
28266 "void f();",
28267 Style);
28268 verifyFormat(FormFeedBeforeEmptyLine,
28269 "int i;\n"
28270 "\f\n"
28271 "\f\n"
28272 "void f();",
28273 Style);
28276 } // namespace
28277 } // namespace test
28278 } // namespace format
28279 } // namespace clang