Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Format / FormatTest.cpp
blob15c12c2b8b0da3f6bdeae684fb973d6950214f85
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;");
50 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
51 verifyFormat("int i;", "int\ni;");
54 TEST_F(FormatTest, FormatsNestedBlockStatements) {
55 verifyFormat("{\n {\n {}\n }\n}", "{{{}}}");
58 TEST_F(FormatTest, FormatsNestedCall) {
59 verifyFormat("Method(f1, f2(f3));");
60 verifyFormat("Method(f1(f2, f3()));");
61 verifyFormat("Method(f1(f2, (f3())));");
64 TEST_F(FormatTest, NestedNameSpecifiers) {
65 verifyFormat("vector<::Type> v;");
66 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
67 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
68 verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
69 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
70 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
71 verifyFormat("bool a = 2 < ::SomeFunction();");
72 verifyFormat("ALWAYS_INLINE ::std::string getName();");
73 verifyFormat("some::string getName();");
76 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
77 verifyFormat("if (a) {\n"
78 " f();\n"
79 "}",
80 "if(a){f();}");
81 EXPECT_EQ(4, ReplacementCount);
82 verifyNoChange("if (a) {\n"
83 " f();\n"
84 "}");
85 EXPECT_EQ(0, ReplacementCount);
86 verifyNoChange("/*\r\n"
87 "\r\n"
88 "*/");
89 EXPECT_EQ(0, ReplacementCount);
92 TEST_F(FormatTest, RemovesEmptyLines) {
93 verifyFormat("class C {\n"
94 " int i;\n"
95 "};",
96 "class C {\n"
97 " int i;\n"
98 "\n"
99 "};");
101 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
102 verifyFormat("namespace N {\n"
103 "\n"
104 "int i;\n"
105 "}",
106 "namespace N {\n"
107 "\n"
108 "int i;\n"
109 "}",
110 getGoogleStyle());
111 verifyFormat("/* something */ namespace N {\n"
112 "\n"
113 "int i;\n"
114 "}",
115 "/* something */ namespace N {\n"
116 "\n"
117 "int i;\n"
118 "}",
119 getGoogleStyle());
120 verifyFormat("inline namespace N {\n"
121 "\n"
122 "int i;\n"
123 "}",
124 "inline namespace N {\n"
125 "\n"
126 "int i;\n"
127 "}",
128 getGoogleStyle());
129 verifyFormat("/* something */ inline namespace N {\n"
130 "\n"
131 "int i;\n"
132 "}",
133 "/* something */ inline namespace N {\n"
134 "\n"
135 "int i;\n"
136 "}",
137 getGoogleStyle());
138 verifyFormat("export namespace N {\n"
139 "\n"
140 "int i;\n"
141 "}",
142 "export namespace N {\n"
143 "\n"
144 "int i;\n"
145 "}",
146 getGoogleStyle());
147 verifyFormat("extern /**/ \"C\" /**/ {\n"
148 "\n"
149 "int i;\n"
150 "}",
151 "extern /**/ \"C\" /**/ {\n"
152 "\n"
153 "int i;\n"
154 "}",
155 getGoogleStyle());
157 auto CustomStyle = getLLVMStyle();
158 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
159 CustomStyle.BraceWrapping.AfterNamespace = true;
160 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
161 verifyFormat("namespace N\n"
162 "{\n"
163 "\n"
164 "int i;\n"
165 "}",
166 "namespace N\n"
167 "{\n"
168 "\n"
169 "\n"
170 "int i;\n"
171 "}",
172 CustomStyle);
173 verifyFormat("/* something */ namespace N\n"
174 "{\n"
175 "\n"
176 "int i;\n"
177 "}",
178 "/* something */ namespace N {\n"
179 "\n"
180 "\n"
181 "int i;\n"
182 "}",
183 CustomStyle);
184 verifyFormat("inline namespace N\n"
185 "{\n"
186 "\n"
187 "int i;\n"
188 "}",
189 "inline namespace N\n"
190 "{\n"
191 "\n"
192 "\n"
193 "int i;\n"
194 "}",
195 CustomStyle);
196 verifyFormat("/* something */ inline namespace N\n"
197 "{\n"
198 "\n"
199 "int i;\n"
200 "}",
201 "/* something */ inline namespace N\n"
202 "{\n"
203 "\n"
204 "int i;\n"
205 "}",
206 CustomStyle);
207 verifyFormat("export namespace N\n"
208 "{\n"
209 "\n"
210 "int i;\n"
211 "}",
212 "export namespace N\n"
213 "{\n"
214 "\n"
215 "int i;\n"
216 "}",
217 CustomStyle);
218 verifyFormat("namespace a\n"
219 "{\n"
220 "namespace b\n"
221 "{\n"
222 "\n"
223 "class AA {};\n"
224 "\n"
225 "} // namespace b\n"
226 "} // namespace a",
227 "namespace a\n"
228 "{\n"
229 "namespace b\n"
230 "{\n"
231 "\n"
232 "\n"
233 "class AA {};\n"
234 "\n"
235 "\n"
236 "}\n"
237 "}",
238 CustomStyle);
239 verifyFormat("namespace A /* comment */\n"
240 "{\n"
241 "class B {}\n"
242 "} // namespace A",
243 "namespace A /* comment */ { class B {} }", CustomStyle);
244 verifyFormat("namespace A\n"
245 "{ /* comment */\n"
246 "class B {}\n"
247 "} // namespace A",
248 "namespace A {/* comment */ class B {} }", CustomStyle);
249 verifyFormat("namespace A\n"
250 "{ /* comment */\n"
251 "\n"
252 "class B {}\n"
253 "\n"
255 "} // namespace A",
256 "namespace A { /* comment */\n"
257 "\n"
258 "\n"
259 "class B {}\n"
260 "\n"
261 "\n"
262 "}",
263 CustomStyle);
264 verifyFormat("namespace A /* comment */\n"
265 "{\n"
266 "\n"
267 "class B {}\n"
268 "\n"
269 "} // namespace A",
270 "namespace A/* comment */ {\n"
271 "\n"
272 "\n"
273 "class B {}\n"
274 "\n"
275 "\n"
276 "}",
277 CustomStyle);
279 // ...but do keep inlining and removing empty lines for non-block extern "C"
280 // functions.
281 verifyGoogleFormat("extern \"C\" int f() { return 42; }");
282 verifyFormat("extern \"C\" int f() {\n"
283 " int i = 42;\n"
284 " return i;\n"
285 "}",
286 "extern \"C\" int f() {\n"
287 "\n"
288 " int i = 42;\n"
289 " return i;\n"
290 "}",
291 getGoogleStyle());
293 // Remove empty lines at the beginning and end of blocks.
294 verifyFormat("void f() {\n"
295 "\n"
296 " if (a) {\n"
297 "\n"
298 " f();\n"
299 " }\n"
300 "}",
301 "void f() {\n"
302 "\n"
303 " if (a) {\n"
304 "\n"
305 " f();\n"
306 "\n"
307 " }\n"
308 "\n"
309 "}");
310 verifyFormat("void f() {\n"
311 " if (a) {\n"
312 " f();\n"
313 " }\n"
314 "}",
315 "void f() {\n"
316 "\n"
317 " if (a) {\n"
318 "\n"
319 " f();\n"
320 "\n"
321 " }\n"
322 "\n"
323 "}",
324 getGoogleStyle());
326 // Don't remove empty lines in more complex control statements.
327 verifyFormat("void f() {\n"
328 " if (a) {\n"
329 " f();\n"
330 "\n"
331 " } else if (b) {\n"
332 " f();\n"
333 " }\n"
334 "}",
335 "void f() {\n"
336 " if (a) {\n"
337 " f();\n"
338 "\n"
339 " } else if (b) {\n"
340 " f();\n"
341 "\n"
342 " }\n"
343 "\n"
344 "}");
346 // Don't remove empty lines before namespace endings.
347 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
348 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
349 verifyNoChange("namespace {\n"
350 "int i;\n"
351 "\n"
352 "}",
353 LLVMWithNoNamespaceFix);
354 verifyFormat("namespace {\n"
355 "int i;\n"
356 "}",
357 LLVMWithNoNamespaceFix);
358 verifyNoChange("namespace {\n"
359 "int i;\n"
360 "\n"
361 "};",
362 LLVMWithNoNamespaceFix);
363 verifyFormat("namespace {\n"
364 "int i;\n"
365 "};",
366 LLVMWithNoNamespaceFix);
367 verifyNoChange("namespace {\n"
368 "int i;\n"
369 "\n"
370 "}");
371 verifyFormat("namespace {\n"
372 "int i;\n"
373 "\n"
374 "} // namespace",
375 "namespace {\n"
376 "int i;\n"
377 "\n"
378 "} // namespace");
380 FormatStyle Style = getLLVMStyle();
381 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
382 Style.MaxEmptyLinesToKeep = 2;
383 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
384 Style.BraceWrapping.AfterClass = true;
385 Style.BraceWrapping.AfterFunction = true;
386 Style.KeepEmptyLinesAtTheStartOfBlocks = false;
388 verifyFormat("class Foo\n"
389 "{\n"
390 " Foo() {}\n"
391 "\n"
392 " void funk() {}\n"
393 "};",
394 "class Foo\n"
395 "{\n"
396 " Foo()\n"
397 " {\n"
398 " }\n"
399 "\n"
400 " void funk() {}\n"
401 "};",
402 Style);
405 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
406 verifyFormat("x = (a) and (b);");
407 verifyFormat("x = (a) or (b);");
408 verifyFormat("x = (a) bitand (b);");
409 verifyFormat("x = (a) bitor (b);");
410 verifyFormat("x = (a) not_eq (b);");
411 verifyFormat("x = (a) and_eq (b);");
412 verifyFormat("x = (a) or_eq (b);");
413 verifyFormat("x = (a) xor (b);");
416 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
417 verifyFormat("x = compl(a);");
418 verifyFormat("x = not(a);");
419 verifyFormat("x = bitand(a);");
420 // Unary operator must not be merged with the next identifier
421 verifyFormat("x = compl a;");
422 verifyFormat("x = not a;");
423 verifyFormat("x = bitand a;");
426 //===----------------------------------------------------------------------===//
427 // Tests for control statements.
428 //===----------------------------------------------------------------------===//
430 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
431 verifyFormat("if (true)\n f();\ng();");
432 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
433 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
434 verifyFormat("if constexpr (true)\n"
435 " f();\ng();");
436 verifyFormat("if CONSTEXPR (true)\n"
437 " f();\ng();");
438 verifyFormat("if constexpr (a)\n"
439 " if constexpr (b)\n"
440 " if constexpr (c)\n"
441 " g();\n"
442 "h();");
443 verifyFormat("if CONSTEXPR (a)\n"
444 " if CONSTEXPR (b)\n"
445 " if CONSTEXPR (c)\n"
446 " g();\n"
447 "h();");
448 verifyFormat("if constexpr (a)\n"
449 " if constexpr (b) {\n"
450 " f();\n"
451 " }\n"
452 "g();");
453 verifyFormat("if CONSTEXPR (a)\n"
454 " if CONSTEXPR (b) {\n"
455 " f();\n"
456 " }\n"
457 "g();");
459 verifyFormat("if consteval {\n}");
460 verifyFormat("if !consteval {\n}");
461 verifyFormat("if not consteval {\n}");
462 verifyFormat("if consteval {\n} else {\n}");
463 verifyFormat("if !consteval {\n} else {\n}");
464 verifyFormat("if consteval {\n"
465 " f();\n"
466 "}");
467 verifyFormat("if !consteval {\n"
468 " f();\n"
469 "}");
470 verifyFormat("if consteval {\n"
471 " f();\n"
472 "} else {\n"
473 " g();\n"
474 "}");
475 verifyFormat("if CONSTEVAL {\n"
476 " f();\n"
477 "}");
478 verifyFormat("if !CONSTEVAL {\n"
479 " f();\n"
480 "}");
482 verifyFormat("if (a)\n"
483 " g();");
484 verifyFormat("if (a) {\n"
485 " g()\n"
486 "};");
487 verifyFormat("if (a)\n"
488 " g();\n"
489 "else\n"
490 " g();");
491 verifyFormat("if (a) {\n"
492 " g();\n"
493 "} else\n"
494 " g();");
495 verifyFormat("if (a)\n"
496 " g();\n"
497 "else {\n"
498 " g();\n"
499 "}");
500 verifyFormat("if (a) {\n"
501 " g();\n"
502 "} else {\n"
503 " g();\n"
504 "}");
505 verifyFormat("if (a)\n"
506 " g();\n"
507 "else if (b)\n"
508 " g();\n"
509 "else\n"
510 " g();");
511 verifyFormat("if (a) {\n"
512 " g();\n"
513 "} else if (b)\n"
514 " g();\n"
515 "else\n"
516 " g();");
517 verifyFormat("if (a)\n"
518 " g();\n"
519 "else if (b) {\n"
520 " g();\n"
521 "} else\n"
522 " g();");
523 verifyFormat("if (a)\n"
524 " g();\n"
525 "else if (b)\n"
526 " g();\n"
527 "else {\n"
528 " g();\n"
529 "}");
530 verifyFormat("if (a)\n"
531 " g();\n"
532 "else if (b) {\n"
533 " g();\n"
534 "} else {\n"
535 " g();\n"
536 "}");
537 verifyFormat("if (a) {\n"
538 " g();\n"
539 "} else if (b) {\n"
540 " g();\n"
541 "} else {\n"
542 " g();\n"
543 "}");
545 FormatStyle AllowsMergedIf = getLLVMStyle();
546 AllowsMergedIf.IfMacros.push_back("MYIF");
547 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
548 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
549 FormatStyle::SIS_WithoutElse;
550 verifyFormat("if (a)\n"
551 " // comment\n"
552 " f();",
553 AllowsMergedIf);
554 verifyFormat("{\n"
555 " if (a)\n"
556 " label:\n"
557 " f();\n"
558 "}",
559 AllowsMergedIf);
560 verifyFormat("#define A \\\n"
561 " if (a) \\\n"
562 " label: \\\n"
563 " f()",
564 AllowsMergedIf);
565 verifyFormat("if (a)\n"
566 " ;",
567 AllowsMergedIf);
568 verifyFormat("if (a)\n"
569 " if (b) return;",
570 AllowsMergedIf);
572 verifyFormat("if (a) // Can't merge this\n"
573 " f();",
574 AllowsMergedIf);
575 verifyFormat("if (a) /* still don't merge */\n"
576 " f();",
577 AllowsMergedIf);
578 verifyFormat("if (a) { // Never merge this\n"
579 " f();\n"
580 "}",
581 AllowsMergedIf);
582 verifyFormat("if (a) { /* Never merge this */\n"
583 " f();\n"
584 "}",
585 AllowsMergedIf);
586 verifyFormat("MYIF (a)\n"
587 " // comment\n"
588 " f();",
589 AllowsMergedIf);
590 verifyFormat("{\n"
591 " MYIF (a)\n"
592 " label:\n"
593 " f();\n"
594 "}",
595 AllowsMergedIf);
596 verifyFormat("#define A \\\n"
597 " MYIF (a) \\\n"
598 " label: \\\n"
599 " f()",
600 AllowsMergedIf);
601 verifyFormat("MYIF (a)\n"
602 " ;",
603 AllowsMergedIf);
604 verifyFormat("MYIF (a)\n"
605 " MYIF (b) return;",
606 AllowsMergedIf);
608 verifyFormat("MYIF (a) // Can't merge this\n"
609 " f();",
610 AllowsMergedIf);
611 verifyFormat("MYIF (a) /* still don't merge */\n"
612 " f();",
613 AllowsMergedIf);
614 verifyFormat("MYIF (a) { // Never merge this\n"
615 " f();\n"
616 "}",
617 AllowsMergedIf);
618 verifyFormat("MYIF (a) { /* Never merge this */\n"
619 " f();\n"
620 "}",
621 AllowsMergedIf);
623 AllowsMergedIf.ColumnLimit = 14;
624 // Where line-lengths matter, a 2-letter synonym that maintains line length.
625 // Not IF to avoid any confusion that IF is somehow special.
626 AllowsMergedIf.IfMacros.push_back("FI");
627 verifyFormat("if (a) return;", AllowsMergedIf);
628 verifyFormat("if (aaaaaaaaa)\n"
629 " return;",
630 AllowsMergedIf);
631 verifyFormat("FI (a) return;", AllowsMergedIf);
632 verifyFormat("FI (aaaaaaaaa)\n"
633 " return;",
634 AllowsMergedIf);
636 AllowsMergedIf.ColumnLimit = 13;
637 verifyFormat("if (a)\n return;", AllowsMergedIf);
638 verifyFormat("FI (a)\n return;", AllowsMergedIf);
640 FormatStyle AllowsMergedIfElse = getLLVMStyle();
641 AllowsMergedIfElse.IfMacros.push_back("MYIF");
642 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
643 FormatStyle::SIS_AllIfsAndElse;
644 verifyFormat("if (a)\n"
645 " // comment\n"
646 " f();\n"
647 "else\n"
648 " // comment\n"
649 " f();",
650 AllowsMergedIfElse);
651 verifyFormat("{\n"
652 " if (a)\n"
653 " label:\n"
654 " f();\n"
655 " else\n"
656 " label:\n"
657 " f();\n"
658 "}",
659 AllowsMergedIfElse);
660 verifyFormat("if (a)\n"
661 " ;\n"
662 "else\n"
663 " ;",
664 AllowsMergedIfElse);
665 verifyFormat("if (a) {\n"
666 "} else {\n"
667 "}",
668 AllowsMergedIfElse);
669 verifyFormat("if (a) return;\n"
670 "else if (b) return;\n"
671 "else return;",
672 AllowsMergedIfElse);
673 verifyFormat("if (a) {\n"
674 "} else return;",
675 AllowsMergedIfElse);
676 verifyFormat("if (a) {\n"
677 "} else if (b) return;\n"
678 "else return;",
679 AllowsMergedIfElse);
680 verifyFormat("if (a) return;\n"
681 "else if (b) {\n"
682 "} else return;",
683 AllowsMergedIfElse);
684 verifyFormat("if (a)\n"
685 " if (b) return;\n"
686 " else return;",
687 AllowsMergedIfElse);
688 verifyFormat("if constexpr (a)\n"
689 " if constexpr (b) return;\n"
690 " else if constexpr (c) return;\n"
691 " else return;",
692 AllowsMergedIfElse);
693 verifyFormat("MYIF (a)\n"
694 " // comment\n"
695 " f();\n"
696 "else\n"
697 " // comment\n"
698 " f();",
699 AllowsMergedIfElse);
700 verifyFormat("{\n"
701 " MYIF (a)\n"
702 " label:\n"
703 " f();\n"
704 " else\n"
705 " label:\n"
706 " f();\n"
707 "}",
708 AllowsMergedIfElse);
709 verifyFormat("MYIF (a)\n"
710 " ;\n"
711 "else\n"
712 " ;",
713 AllowsMergedIfElse);
714 verifyFormat("MYIF (a) {\n"
715 "} else {\n"
716 "}",
717 AllowsMergedIfElse);
718 verifyFormat("MYIF (a) return;\n"
719 "else MYIF (b) return;\n"
720 "else return;",
721 AllowsMergedIfElse);
722 verifyFormat("MYIF (a) {\n"
723 "} else return;",
724 AllowsMergedIfElse);
725 verifyFormat("MYIF (a) {\n"
726 "} else MYIF (b) return;\n"
727 "else return;",
728 AllowsMergedIfElse);
729 verifyFormat("MYIF (a) return;\n"
730 "else MYIF (b) {\n"
731 "} else return;",
732 AllowsMergedIfElse);
733 verifyFormat("MYIF (a)\n"
734 " MYIF (b) return;\n"
735 " else return;",
736 AllowsMergedIfElse);
737 verifyFormat("MYIF constexpr (a)\n"
738 " MYIF constexpr (b) return;\n"
739 " else MYIF constexpr (c) return;\n"
740 " else return;",
741 AllowsMergedIfElse);
744 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
745 FormatStyle AllowsMergedIf = getLLVMStyle();
746 AllowsMergedIf.IfMacros.push_back("MYIF");
747 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
748 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
749 FormatStyle::SIS_WithoutElse;
750 verifyFormat("if (a)\n"
751 " f();\n"
752 "else {\n"
753 " g();\n"
754 "}",
755 AllowsMergedIf);
756 verifyFormat("if (a)\n"
757 " f();\n"
758 "else\n"
759 " g();",
760 AllowsMergedIf);
762 verifyFormat("if (a) g();", AllowsMergedIf);
763 verifyFormat("if (a) {\n"
764 " g()\n"
765 "};",
766 AllowsMergedIf);
767 verifyFormat("if (a)\n"
768 " g();\n"
769 "else\n"
770 " g();",
771 AllowsMergedIf);
772 verifyFormat("if (a) {\n"
773 " g();\n"
774 "} else\n"
775 " g();",
776 AllowsMergedIf);
777 verifyFormat("if (a)\n"
778 " g();\n"
779 "else {\n"
780 " g();\n"
781 "}",
782 AllowsMergedIf);
783 verifyFormat("if (a) {\n"
784 " g();\n"
785 "} else {\n"
786 " g();\n"
787 "}",
788 AllowsMergedIf);
789 verifyFormat("if (a)\n"
790 " g();\n"
791 "else if (b)\n"
792 " g();\n"
793 "else\n"
794 " g();",
795 AllowsMergedIf);
796 verifyFormat("if (a) {\n"
797 " g();\n"
798 "} else if (b)\n"
799 " g();\n"
800 "else\n"
801 " g();",
802 AllowsMergedIf);
803 verifyFormat("if (a)\n"
804 " g();\n"
805 "else if (b) {\n"
806 " g();\n"
807 "} else\n"
808 " g();",
809 AllowsMergedIf);
810 verifyFormat("if (a)\n"
811 " g();\n"
812 "else if (b)\n"
813 " g();\n"
814 "else {\n"
815 " g();\n"
816 "}",
817 AllowsMergedIf);
818 verifyFormat("if (a)\n"
819 " g();\n"
820 "else if (b) {\n"
821 " g();\n"
822 "} else {\n"
823 " g();\n"
824 "}",
825 AllowsMergedIf);
826 verifyFormat("if (a) {\n"
827 " g();\n"
828 "} else if (b) {\n"
829 " g();\n"
830 "} else {\n"
831 " g();\n"
832 "}",
833 AllowsMergedIf);
834 verifyFormat("MYIF (a)\n"
835 " f();\n"
836 "else {\n"
837 " g();\n"
838 "}",
839 AllowsMergedIf);
840 verifyFormat("MYIF (a)\n"
841 " f();\n"
842 "else\n"
843 " g();",
844 AllowsMergedIf);
846 verifyFormat("MYIF (a) g();", AllowsMergedIf);
847 verifyFormat("MYIF (a) {\n"
848 " g()\n"
849 "};",
850 AllowsMergedIf);
851 verifyFormat("MYIF (a)\n"
852 " g();\n"
853 "else\n"
854 " g();",
855 AllowsMergedIf);
856 verifyFormat("MYIF (a) {\n"
857 " g();\n"
858 "} else\n"
859 " g();",
860 AllowsMergedIf);
861 verifyFormat("MYIF (a)\n"
862 " g();\n"
863 "else {\n"
864 " g();\n"
865 "}",
866 AllowsMergedIf);
867 verifyFormat("MYIF (a) {\n"
868 " g();\n"
869 "} else {\n"
870 " g();\n"
871 "}",
872 AllowsMergedIf);
873 verifyFormat("MYIF (a)\n"
874 " g();\n"
875 "else MYIF (b)\n"
876 " g();\n"
877 "else\n"
878 " g();",
879 AllowsMergedIf);
880 verifyFormat("MYIF (a)\n"
881 " g();\n"
882 "else if (b)\n"
883 " g();\n"
884 "else\n"
885 " g();",
886 AllowsMergedIf);
887 verifyFormat("MYIF (a) {\n"
888 " g();\n"
889 "} else MYIF (b)\n"
890 " g();\n"
891 "else\n"
892 " g();",
893 AllowsMergedIf);
894 verifyFormat("MYIF (a) {\n"
895 " g();\n"
896 "} else if (b)\n"
897 " g();\n"
898 "else\n"
899 " g();",
900 AllowsMergedIf);
901 verifyFormat("MYIF (a)\n"
902 " g();\n"
903 "else MYIF (b) {\n"
904 " g();\n"
905 "} else\n"
906 " g();",
907 AllowsMergedIf);
908 verifyFormat("MYIF (a)\n"
909 " g();\n"
910 "else if (b) {\n"
911 " g();\n"
912 "} else\n"
913 " g();",
914 AllowsMergedIf);
915 verifyFormat("MYIF (a)\n"
916 " g();\n"
917 "else MYIF (b)\n"
918 " g();\n"
919 "else {\n"
920 " g();\n"
921 "}",
922 AllowsMergedIf);
923 verifyFormat("MYIF (a)\n"
924 " g();\n"
925 "else if (b)\n"
926 " g();\n"
927 "else {\n"
928 " g();\n"
929 "}",
930 AllowsMergedIf);
931 verifyFormat("MYIF (a)\n"
932 " g();\n"
933 "else MYIF (b) {\n"
934 " g();\n"
935 "} else {\n"
936 " g();\n"
937 "}",
938 AllowsMergedIf);
939 verifyFormat("MYIF (a)\n"
940 " g();\n"
941 "else if (b) {\n"
942 " g();\n"
943 "} else {\n"
944 " g();\n"
945 "}",
946 AllowsMergedIf);
947 verifyFormat("MYIF (a) {\n"
948 " g();\n"
949 "} else MYIF (b) {\n"
950 " g();\n"
951 "} else {\n"
952 " g();\n"
953 "}",
954 AllowsMergedIf);
955 verifyFormat("MYIF (a) {\n"
956 " g();\n"
957 "} else if (b) {\n"
958 " g();\n"
959 "} else {\n"
960 " g();\n"
961 "}",
962 AllowsMergedIf);
964 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
965 FormatStyle::SIS_OnlyFirstIf;
967 verifyFormat("if (a) f();\n"
968 "else {\n"
969 " g();\n"
970 "}",
971 AllowsMergedIf);
972 verifyFormat("if (a) f();\n"
973 "else {\n"
974 " if (a) f();\n"
975 " else {\n"
976 " g();\n"
977 " }\n"
978 " g();\n"
979 "}",
980 AllowsMergedIf);
982 verifyFormat("if (a) g();", AllowsMergedIf);
983 verifyFormat("if (a) {\n"
984 " g()\n"
985 "};",
986 AllowsMergedIf);
987 verifyFormat("if (a) g();\n"
988 "else\n"
989 " g();",
990 AllowsMergedIf);
991 verifyFormat("if (a) {\n"
992 " g();\n"
993 "} else\n"
994 " g();",
995 AllowsMergedIf);
996 verifyFormat("if (a) g();\n"
997 "else {\n"
998 " g();\n"
999 "}",
1000 AllowsMergedIf);
1001 verifyFormat("if (a) {\n"
1002 " g();\n"
1003 "} else {\n"
1004 " g();\n"
1005 "}",
1006 AllowsMergedIf);
1007 verifyFormat("if (a) g();\n"
1008 "else if (b)\n"
1009 " g();\n"
1010 "else\n"
1011 " g();",
1012 AllowsMergedIf);
1013 verifyFormat("if (a) {\n"
1014 " g();\n"
1015 "} else if (b)\n"
1016 " g();\n"
1017 "else\n"
1018 " g();",
1019 AllowsMergedIf);
1020 verifyFormat("if (a) g();\n"
1021 "else if (b) {\n"
1022 " g();\n"
1023 "} else\n"
1024 " g();",
1025 AllowsMergedIf);
1026 verifyFormat("if (a) g();\n"
1027 "else if (b)\n"
1028 " g();\n"
1029 "else {\n"
1030 " g();\n"
1031 "}",
1032 AllowsMergedIf);
1033 verifyFormat("if (a) g();\n"
1034 "else if (b) {\n"
1035 " g();\n"
1036 "} else {\n"
1037 " g();\n"
1038 "}",
1039 AllowsMergedIf);
1040 verifyFormat("if (a) {\n"
1041 " g();\n"
1042 "} else if (b) {\n"
1043 " g();\n"
1044 "} else {\n"
1045 " g();\n"
1046 "}",
1047 AllowsMergedIf);
1048 verifyFormat("MYIF (a) f();\n"
1049 "else {\n"
1050 " g();\n"
1051 "}",
1052 AllowsMergedIf);
1053 verifyFormat("MYIF (a) f();\n"
1054 "else {\n"
1055 " if (a) f();\n"
1056 " else {\n"
1057 " g();\n"
1058 " }\n"
1059 " g();\n"
1060 "}",
1061 AllowsMergedIf);
1063 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1064 verifyFormat("MYIF (a) {\n"
1065 " g()\n"
1066 "};",
1067 AllowsMergedIf);
1068 verifyFormat("MYIF (a) g();\n"
1069 "else\n"
1070 " g();",
1071 AllowsMergedIf);
1072 verifyFormat("MYIF (a) {\n"
1073 " g();\n"
1074 "} else\n"
1075 " g();",
1076 AllowsMergedIf);
1077 verifyFormat("MYIF (a) g();\n"
1078 "else {\n"
1079 " g();\n"
1080 "}",
1081 AllowsMergedIf);
1082 verifyFormat("MYIF (a) {\n"
1083 " g();\n"
1084 "} else {\n"
1085 " g();\n"
1086 "}",
1087 AllowsMergedIf);
1088 verifyFormat("MYIF (a) g();\n"
1089 "else MYIF (b)\n"
1090 " g();\n"
1091 "else\n"
1092 " g();",
1093 AllowsMergedIf);
1094 verifyFormat("MYIF (a) g();\n"
1095 "else if (b)\n"
1096 " g();\n"
1097 "else\n"
1098 " g();",
1099 AllowsMergedIf);
1100 verifyFormat("MYIF (a) {\n"
1101 " g();\n"
1102 "} else MYIF (b)\n"
1103 " g();\n"
1104 "else\n"
1105 " g();",
1106 AllowsMergedIf);
1107 verifyFormat("MYIF (a) {\n"
1108 " g();\n"
1109 "} else if (b)\n"
1110 " g();\n"
1111 "else\n"
1112 " g();",
1113 AllowsMergedIf);
1114 verifyFormat("MYIF (a) g();\n"
1115 "else MYIF (b) {\n"
1116 " g();\n"
1117 "} else\n"
1118 " g();",
1119 AllowsMergedIf);
1120 verifyFormat("MYIF (a) g();\n"
1121 "else if (b) {\n"
1122 " g();\n"
1123 "} else\n"
1124 " g();",
1125 AllowsMergedIf);
1126 verifyFormat("MYIF (a) g();\n"
1127 "else MYIF (b)\n"
1128 " g();\n"
1129 "else {\n"
1130 " g();\n"
1131 "}",
1132 AllowsMergedIf);
1133 verifyFormat("MYIF (a) g();\n"
1134 "else if (b)\n"
1135 " g();\n"
1136 "else {\n"
1137 " g();\n"
1138 "}",
1139 AllowsMergedIf);
1140 verifyFormat("MYIF (a) g();\n"
1141 "else MYIF (b) {\n"
1142 " g();\n"
1143 "} else {\n"
1144 " g();\n"
1145 "}",
1146 AllowsMergedIf);
1147 verifyFormat("MYIF (a) g();\n"
1148 "else if (b) {\n"
1149 " g();\n"
1150 "} else {\n"
1151 " g();\n"
1152 "}",
1153 AllowsMergedIf);
1154 verifyFormat("MYIF (a) {\n"
1155 " g();\n"
1156 "} else MYIF (b) {\n"
1157 " g();\n"
1158 "} else {\n"
1159 " g();\n"
1160 "}",
1161 AllowsMergedIf);
1162 verifyFormat("MYIF (a) {\n"
1163 " g();\n"
1164 "} else if (b) {\n"
1165 " g();\n"
1166 "} else {\n"
1167 " g();\n"
1168 "}",
1169 AllowsMergedIf);
1171 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1172 FormatStyle::SIS_AllIfsAndElse;
1174 verifyFormat("if (a) f();\n"
1175 "else {\n"
1176 " g();\n"
1177 "}",
1178 AllowsMergedIf);
1179 verifyFormat("if (a) f();\n"
1180 "else {\n"
1181 " if (a) f();\n"
1182 " else {\n"
1183 " g();\n"
1184 " }\n"
1185 " g();\n"
1186 "}",
1187 AllowsMergedIf);
1189 verifyFormat("if (a) g();", AllowsMergedIf);
1190 verifyFormat("if (a) {\n"
1191 " g()\n"
1192 "};",
1193 AllowsMergedIf);
1194 verifyFormat("if (a) g();\n"
1195 "else g();",
1196 AllowsMergedIf);
1197 verifyFormat("if (a) {\n"
1198 " g();\n"
1199 "} else g();",
1200 AllowsMergedIf);
1201 verifyFormat("if (a) g();\n"
1202 "else {\n"
1203 " g();\n"
1204 "}",
1205 AllowsMergedIf);
1206 verifyFormat("if (a) {\n"
1207 " g();\n"
1208 "} else {\n"
1209 " g();\n"
1210 "}",
1211 AllowsMergedIf);
1212 verifyFormat("if (a) g();\n"
1213 "else if (b) g();\n"
1214 "else g();",
1215 AllowsMergedIf);
1216 verifyFormat("if (a) {\n"
1217 " g();\n"
1218 "} else if (b) g();\n"
1219 "else g();",
1220 AllowsMergedIf);
1221 verifyFormat("if (a) g();\n"
1222 "else if (b) {\n"
1223 " g();\n"
1224 "} else g();",
1225 AllowsMergedIf);
1226 verifyFormat("if (a) g();\n"
1227 "else if (b) g();\n"
1228 "else {\n"
1229 " g();\n"
1230 "}",
1231 AllowsMergedIf);
1232 verifyFormat("if (a) g();\n"
1233 "else if (b) {\n"
1234 " g();\n"
1235 "} else {\n"
1236 " g();\n"
1237 "}",
1238 AllowsMergedIf);
1239 verifyFormat("if (a) {\n"
1240 " g();\n"
1241 "} else if (b) {\n"
1242 " g();\n"
1243 "} else {\n"
1244 " g();\n"
1245 "}",
1246 AllowsMergedIf);
1247 verifyFormat("MYIF (a) f();\n"
1248 "else {\n"
1249 " g();\n"
1250 "}",
1251 AllowsMergedIf);
1252 verifyFormat("MYIF (a) f();\n"
1253 "else {\n"
1254 " if (a) f();\n"
1255 " else {\n"
1256 " g();\n"
1257 " }\n"
1258 " g();\n"
1259 "}",
1260 AllowsMergedIf);
1262 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1263 verifyFormat("MYIF (a) {\n"
1264 " g()\n"
1265 "};",
1266 AllowsMergedIf);
1267 verifyFormat("MYIF (a) g();\n"
1268 "else g();",
1269 AllowsMergedIf);
1270 verifyFormat("MYIF (a) {\n"
1271 " g();\n"
1272 "} else g();",
1273 AllowsMergedIf);
1274 verifyFormat("MYIF (a) g();\n"
1275 "else {\n"
1276 " g();\n"
1277 "}",
1278 AllowsMergedIf);
1279 verifyFormat("MYIF (a) {\n"
1280 " g();\n"
1281 "} else {\n"
1282 " g();\n"
1283 "}",
1284 AllowsMergedIf);
1285 verifyFormat("MYIF (a) g();\n"
1286 "else MYIF (b) g();\n"
1287 "else g();",
1288 AllowsMergedIf);
1289 verifyFormat("MYIF (a) g();\n"
1290 "else if (b) g();\n"
1291 "else g();",
1292 AllowsMergedIf);
1293 verifyFormat("MYIF (a) {\n"
1294 " g();\n"
1295 "} else MYIF (b) g();\n"
1296 "else g();",
1297 AllowsMergedIf);
1298 verifyFormat("MYIF (a) {\n"
1299 " g();\n"
1300 "} else if (b) g();\n"
1301 "else g();",
1302 AllowsMergedIf);
1303 verifyFormat("MYIF (a) g();\n"
1304 "else MYIF (b) {\n"
1305 " g();\n"
1306 "} else g();",
1307 AllowsMergedIf);
1308 verifyFormat("MYIF (a) g();\n"
1309 "else if (b) {\n"
1310 " g();\n"
1311 "} else g();",
1312 AllowsMergedIf);
1313 verifyFormat("MYIF (a) g();\n"
1314 "else MYIF (b) g();\n"
1315 "else {\n"
1316 " g();\n"
1317 "}",
1318 AllowsMergedIf);
1319 verifyFormat("MYIF (a) g();\n"
1320 "else if (b) g();\n"
1321 "else {\n"
1322 " g();\n"
1323 "}",
1324 AllowsMergedIf);
1325 verifyFormat("MYIF (a) g();\n"
1326 "else MYIF (b) {\n"
1327 " g();\n"
1328 "} else {\n"
1329 " g();\n"
1330 "}",
1331 AllowsMergedIf);
1332 verifyFormat("MYIF (a) g();\n"
1333 "else if (b) {\n"
1334 " g();\n"
1335 "} else {\n"
1336 " g();\n"
1337 "}",
1338 AllowsMergedIf);
1339 verifyFormat("MYIF (a) {\n"
1340 " g();\n"
1341 "} else MYIF (b) {\n"
1342 " g();\n"
1343 "} else {\n"
1344 " g();\n"
1345 "}",
1346 AllowsMergedIf);
1347 verifyFormat("MYIF (a) {\n"
1348 " g();\n"
1349 "} else if (b) {\n"
1350 " g();\n"
1351 "} else {\n"
1352 " g();\n"
1353 "}",
1354 AllowsMergedIf);
1357 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1358 FormatStyle AllowsMergedLoops = getLLVMStyle();
1359 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1360 verifyFormat("while (true) continue;", AllowsMergedLoops);
1361 verifyFormat("for (;;) continue;", AllowsMergedLoops);
1362 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1363 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1364 verifyFormat("while (true)\n"
1365 " ;",
1366 AllowsMergedLoops);
1367 verifyFormat("for (;;)\n"
1368 " ;",
1369 AllowsMergedLoops);
1370 verifyFormat("for (;;)\n"
1371 " for (;;) continue;",
1372 AllowsMergedLoops);
1373 verifyFormat("for (;;)\n"
1374 " while (true) continue;",
1375 AllowsMergedLoops);
1376 verifyFormat("while (true)\n"
1377 " for (;;) continue;",
1378 AllowsMergedLoops);
1379 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1380 " for (;;) continue;",
1381 AllowsMergedLoops);
1382 verifyFormat("for (;;)\n"
1383 " BOOST_FOREACH (int &v, vec) continue;",
1384 AllowsMergedLoops);
1385 verifyFormat("for (;;) // Can't merge this\n"
1386 " continue;",
1387 AllowsMergedLoops);
1388 verifyFormat("for (;;) /* still don't merge */\n"
1389 " continue;",
1390 AllowsMergedLoops);
1391 verifyFormat("do a++;\n"
1392 "while (true);",
1393 AllowsMergedLoops);
1394 verifyFormat("do /* Don't merge */\n"
1395 " a++;\n"
1396 "while (true);",
1397 AllowsMergedLoops);
1398 verifyFormat("do // Don't merge\n"
1399 " a++;\n"
1400 "while (true);",
1401 AllowsMergedLoops);
1402 verifyFormat("do\n"
1403 " // Don't merge\n"
1404 " a++;\n"
1405 "while (true);",
1406 AllowsMergedLoops);
1407 // Without braces labels are interpreted differently.
1408 verifyFormat("{\n"
1409 " do\n"
1410 " label:\n"
1411 " a++;\n"
1412 " while (true);\n"
1413 "}",
1414 AllowsMergedLoops);
1417 TEST_F(FormatTest, FormatShortBracedStatements) {
1418 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1419 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1420 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1421 FormatStyle::SIS_Never);
1422 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1423 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1424 verifyFormat("for (;;) {\n"
1425 " f();\n"
1426 "}");
1427 verifyFormat("/*comment*/ for (;;) {\n"
1428 " f();\n"
1429 "}");
1430 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1431 " f();\n"
1432 "}");
1433 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1434 " f();\n"
1435 "}");
1436 verifyFormat("while (true) {\n"
1437 " f();\n"
1438 "}");
1439 verifyFormat("/*comment*/ while (true) {\n"
1440 " f();\n"
1441 "}");
1442 verifyFormat("if (true) {\n"
1443 " f();\n"
1444 "}");
1445 verifyFormat("/*comment*/ if (true) {\n"
1446 " f();\n"
1447 "}");
1449 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1450 FormatStyle::SBS_Empty;
1451 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1452 FormatStyle::SIS_WithoutElse;
1453 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1454 verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1455 verifyFormat("if (i > 0) {\n"
1456 " return i;\n"
1457 "}",
1458 AllowSimpleBracedStatements);
1460 AllowSimpleBracedStatements.IfMacros.push_back("MYIF");
1461 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1462 // Not IF to avoid any confusion that IF is somehow special.
1463 AllowSimpleBracedStatements.IfMacros.push_back("FI");
1464 AllowSimpleBracedStatements.ColumnLimit = 40;
1465 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1466 FormatStyle::SBS_Always;
1467 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1468 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1469 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1470 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1472 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1473 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1474 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1475 verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1476 verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1477 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1478 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1479 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1480 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1481 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1482 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1483 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1484 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1485 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1486 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1487 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1488 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1489 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1490 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1491 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1492 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1493 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1494 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1495 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1496 AllowSimpleBracedStatements);
1497 verifyFormat("if (true) {\n"
1498 " ffffffffffffffffffffffff();\n"
1499 "}",
1500 AllowSimpleBracedStatements);
1501 verifyFormat("if (true) {\n"
1502 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1503 "}",
1504 AllowSimpleBracedStatements);
1505 verifyFormat("if (true) { //\n"
1506 " f();\n"
1507 "}",
1508 AllowSimpleBracedStatements);
1509 verifyFormat("if (true) {\n"
1510 " f();\n"
1511 " f();\n"
1512 "}",
1513 AllowSimpleBracedStatements);
1514 verifyFormat("if (true) {\n"
1515 " f();\n"
1516 "} else {\n"
1517 " f();\n"
1518 "}",
1519 AllowSimpleBracedStatements);
1520 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1521 AllowSimpleBracedStatements);
1522 verifyFormat("MYIF (true) {\n"
1523 " ffffffffffffffffffffffff();\n"
1524 "}",
1525 AllowSimpleBracedStatements);
1526 verifyFormat("MYIF (true) {\n"
1527 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1528 "}",
1529 AllowSimpleBracedStatements);
1530 verifyFormat("MYIF (true) { //\n"
1531 " f();\n"
1532 "}",
1533 AllowSimpleBracedStatements);
1534 verifyFormat("MYIF (true) {\n"
1535 " f();\n"
1536 " f();\n"
1537 "}",
1538 AllowSimpleBracedStatements);
1539 verifyFormat("MYIF (true) {\n"
1540 " f();\n"
1541 "} else {\n"
1542 " f();\n"
1543 "}",
1544 AllowSimpleBracedStatements);
1546 verifyFormat("struct A2 {\n"
1547 " int X;\n"
1548 "};",
1549 AllowSimpleBracedStatements);
1550 verifyFormat("typedef struct A2 {\n"
1551 " int X;\n"
1552 "} A2_t;",
1553 AllowSimpleBracedStatements);
1554 verifyFormat("template <int> struct A2 {\n"
1555 " struct B {};\n"
1556 "};",
1557 AllowSimpleBracedStatements);
1559 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1560 FormatStyle::SIS_Never;
1561 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1562 verifyFormat("if (true) {\n"
1563 " f();\n"
1564 "}",
1565 AllowSimpleBracedStatements);
1566 verifyFormat("if (true) {\n"
1567 " f();\n"
1568 "} else {\n"
1569 " f();\n"
1570 "}",
1571 AllowSimpleBracedStatements);
1572 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1573 verifyFormat("MYIF (true) {\n"
1574 " f();\n"
1575 "}",
1576 AllowSimpleBracedStatements);
1577 verifyFormat("MYIF (true) {\n"
1578 " f();\n"
1579 "} else {\n"
1580 " f();\n"
1581 "}",
1582 AllowSimpleBracedStatements);
1584 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1585 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1586 verifyFormat("while (true) {\n"
1587 " f();\n"
1588 "}",
1589 AllowSimpleBracedStatements);
1590 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1591 verifyFormat("for (;;) {\n"
1592 " f();\n"
1593 "}",
1594 AllowSimpleBracedStatements);
1595 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1596 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1597 " f();\n"
1598 "}",
1599 AllowSimpleBracedStatements);
1601 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1602 FormatStyle::SIS_WithoutElse;
1603 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1604 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1605 FormatStyle::BWACS_Always;
1607 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1608 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1609 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1610 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1611 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1612 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1613 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1614 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1615 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1616 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1617 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1618 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1619 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1620 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1621 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1622 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1623 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1624 AllowSimpleBracedStatements);
1625 verifyFormat("if (true)\n"
1626 "{\n"
1627 " ffffffffffffffffffffffff();\n"
1628 "}",
1629 AllowSimpleBracedStatements);
1630 verifyFormat("if (true)\n"
1631 "{\n"
1632 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1633 "}",
1634 AllowSimpleBracedStatements);
1635 verifyFormat("if (true)\n"
1636 "{ //\n"
1637 " f();\n"
1638 "}",
1639 AllowSimpleBracedStatements);
1640 verifyFormat("if (true)\n"
1641 "{\n"
1642 " f();\n"
1643 " f();\n"
1644 "}",
1645 AllowSimpleBracedStatements);
1646 verifyFormat("if (true)\n"
1647 "{\n"
1648 " f();\n"
1649 "} else\n"
1650 "{\n"
1651 " f();\n"
1652 "}",
1653 AllowSimpleBracedStatements);
1654 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1655 AllowSimpleBracedStatements);
1656 verifyFormat("MYIF (true)\n"
1657 "{\n"
1658 " ffffffffffffffffffffffff();\n"
1659 "}",
1660 AllowSimpleBracedStatements);
1661 verifyFormat("MYIF (true)\n"
1662 "{\n"
1663 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1664 "}",
1665 AllowSimpleBracedStatements);
1666 verifyFormat("MYIF (true)\n"
1667 "{ //\n"
1668 " f();\n"
1669 "}",
1670 AllowSimpleBracedStatements);
1671 verifyFormat("MYIF (true)\n"
1672 "{\n"
1673 " f();\n"
1674 " f();\n"
1675 "}",
1676 AllowSimpleBracedStatements);
1677 verifyFormat("MYIF (true)\n"
1678 "{\n"
1679 " f();\n"
1680 "} else\n"
1681 "{\n"
1682 " f();\n"
1683 "}",
1684 AllowSimpleBracedStatements);
1686 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1687 FormatStyle::SIS_Never;
1688 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1689 verifyFormat("if (true)\n"
1690 "{\n"
1691 " f();\n"
1692 "}",
1693 AllowSimpleBracedStatements);
1694 verifyFormat("if (true)\n"
1695 "{\n"
1696 " f();\n"
1697 "} else\n"
1698 "{\n"
1699 " f();\n"
1700 "}",
1701 AllowSimpleBracedStatements);
1702 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1703 verifyFormat("MYIF (true)\n"
1704 "{\n"
1705 " f();\n"
1706 "}",
1707 AllowSimpleBracedStatements);
1708 verifyFormat("MYIF (true)\n"
1709 "{\n"
1710 " f();\n"
1711 "} else\n"
1712 "{\n"
1713 " f();\n"
1714 "}",
1715 AllowSimpleBracedStatements);
1717 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1718 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1719 verifyFormat("while (true)\n"
1720 "{\n"
1721 " f();\n"
1722 "}",
1723 AllowSimpleBracedStatements);
1724 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1725 verifyFormat("for (;;)\n"
1726 "{\n"
1727 " f();\n"
1728 "}",
1729 AllowSimpleBracedStatements);
1730 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1731 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1732 "{\n"
1733 " f();\n"
1734 "}",
1735 AllowSimpleBracedStatements);
1737 FormatStyle Style = getLLVMStyle();
1738 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1739 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1741 verifyFormat("while (i > 0)\n"
1742 "{\n"
1743 " --i;\n"
1744 "}",
1745 Style);
1747 verifyFormat("if (a)\n"
1748 "{\n"
1749 " ++b;\n"
1750 "}",
1751 Style);
1753 verifyFormat("if (a)\n"
1754 "{\n"
1755 " b = 1;\n"
1756 "} else\n"
1757 "{\n"
1758 " b = 0;\n"
1759 "}",
1760 Style);
1762 verifyFormat("if (a)\n"
1763 "{\n"
1764 " b = 1;\n"
1765 "} else if (c)\n"
1766 "{\n"
1767 " b = 2;\n"
1768 "} else\n"
1769 "{\n"
1770 " b = 0;\n"
1771 "}",
1772 Style);
1774 Style.BraceWrapping.BeforeElse = true;
1776 verifyFormat("if (a)\n"
1777 "{\n"
1778 " b = 1;\n"
1779 "}\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 "}\n"
1790 "else if (c)\n"
1791 "{\n"
1792 " b = 2;\n"
1793 "}\n"
1794 "else\n"
1795 "{\n"
1796 " b = 0;\n"
1797 "}",
1798 Style);
1801 TEST_F(FormatTest, UnderstandsMacros) {
1802 verifyFormat("#define A (parentheses)");
1803 verifyFormat("/* comment */ #define A (parentheses)");
1804 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1805 // Even the partial code should never be merged.
1806 verifyNoChange("/* comment */ #define A (parentheses)\n"
1807 "#");
1808 verifyFormat("/* comment */ #define A (parentheses)\n"
1809 "#\n");
1810 verifyFormat("/* comment */ #define A (parentheses)\n"
1811 "#define B (parentheses)");
1812 verifyFormat("#define true ((int)1)");
1813 verifyFormat("#define and(x)");
1814 verifyFormat("#define if(x) x");
1815 verifyFormat("#define return(x) (x)");
1816 verifyFormat("#define while(x) for (; x;)");
1817 verifyFormat("#define xor(x) (^(x))");
1818 verifyFormat("#define __except(x)");
1819 verifyFormat("#define __try(x)");
1821 // https://llvm.org/PR54348.
1822 verifyFormat(
1823 "#define A"
1825 "\\\n"
1826 " class & {}");
1828 FormatStyle Style = getLLVMStyle();
1829 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1830 Style.BraceWrapping.AfterFunction = true;
1831 // Test that a macro definition never gets merged with the following
1832 // definition.
1833 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1834 verifyFormat("#define AAA "
1835 " \\\n"
1836 " N "
1837 " \\\n"
1838 " {\n"
1839 "#define BBB }",
1840 Style);
1841 // verifyFormat("#define AAA N { //", Style);
1843 verifyFormat("MACRO(return)");
1844 verifyFormat("MACRO(co_await)");
1845 verifyFormat("MACRO(co_return)");
1846 verifyFormat("MACRO(co_yield)");
1847 verifyFormat("MACRO(return, something)");
1848 verifyFormat("MACRO(co_return, something)");
1849 verifyFormat("MACRO(something##something)");
1850 verifyFormat("MACRO(return##something)");
1851 verifyFormat("MACRO(co_return##something)");
1854 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1855 FormatStyle Style = getLLVMStyleWithColumns(60);
1856 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1857 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1858 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1859 verifyFormat("#define A \\\n"
1860 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1861 " { \\\n"
1862 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1863 " }\n"
1864 "X;",
1865 "#define A \\\n"
1866 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1867 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1868 " }\n"
1869 "X;",
1870 Style);
1873 TEST_F(FormatTest, ParseIfElse) {
1874 verifyFormat("if (true)\n"
1875 " if (true)\n"
1876 " if (true)\n"
1877 " f();\n"
1878 " else\n"
1879 " g();\n"
1880 " else\n"
1881 " h();\n"
1882 "else\n"
1883 " i();");
1884 verifyFormat("if (true)\n"
1885 " if (true)\n"
1886 " if (true) {\n"
1887 " if (true)\n"
1888 " f();\n"
1889 " } else {\n"
1890 " g();\n"
1891 " }\n"
1892 " else\n"
1893 " h();\n"
1894 "else {\n"
1895 " i();\n"
1896 "}");
1897 verifyFormat("if (true)\n"
1898 " if constexpr (true)\n"
1899 " if (true) {\n"
1900 " if constexpr (true)\n"
1901 " f();\n"
1902 " } else {\n"
1903 " g();\n"
1904 " }\n"
1905 " else\n"
1906 " h();\n"
1907 "else {\n"
1908 " i();\n"
1909 "}");
1910 verifyFormat("if (true)\n"
1911 " if CONSTEXPR (true)\n"
1912 " if (true) {\n"
1913 " if CONSTEXPR (true)\n"
1914 " f();\n"
1915 " } else {\n"
1916 " g();\n"
1917 " }\n"
1918 " else\n"
1919 " h();\n"
1920 "else {\n"
1921 " i();\n"
1922 "}");
1923 verifyFormat("void f() {\n"
1924 " if (a) {\n"
1925 " } else {\n"
1926 " }\n"
1927 "}");
1930 TEST_F(FormatTest, ElseIf) {
1931 verifyFormat("if (a) {\n} else if (b) {\n}");
1932 verifyFormat("if (a)\n"
1933 " f();\n"
1934 "else if (b)\n"
1935 " g();\n"
1936 "else\n"
1937 " h();");
1938 verifyFormat("if (a)\n"
1939 " f();\n"
1940 "else // comment\n"
1941 " if (b) {\n"
1942 " g();\n"
1943 " h();\n"
1944 " }");
1945 verifyFormat("if constexpr (a)\n"
1946 " f();\n"
1947 "else if constexpr (b)\n"
1948 " g();\n"
1949 "else\n"
1950 " h();");
1951 verifyFormat("if CONSTEXPR (a)\n"
1952 " f();\n"
1953 "else if CONSTEXPR (b)\n"
1954 " g();\n"
1955 "else\n"
1956 " h();");
1957 verifyFormat("if (a) {\n"
1958 " f();\n"
1959 "}\n"
1960 "// or else ..\n"
1961 "else {\n"
1962 " g()\n"
1963 "}");
1965 verifyFormat("if (a) {\n"
1966 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1968 "}");
1969 verifyFormat("if (a) {\n"
1970 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1972 "}");
1973 verifyFormat("if (a) {\n"
1974 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1976 "}");
1977 verifyFormat("if (a) {\n"
1978 "} else if (\n"
1979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1980 "}",
1981 getLLVMStyleWithColumns(62));
1982 verifyFormat("if (a) {\n"
1983 "} else if constexpr (\n"
1984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1985 "}",
1986 getLLVMStyleWithColumns(62));
1987 verifyFormat("if (a) {\n"
1988 "} else if CONSTEXPR (\n"
1989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1990 "}",
1991 getLLVMStyleWithColumns(62));
1994 TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
1995 FormatStyle Style = getLLVMStyle();
1996 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
1997 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
1998 verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
1999 verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2000 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2001 verifyFormat("int *f1(int &a) const &;", Style);
2002 verifyFormat("int *f1(int &a) const & = 0;", Style);
2003 verifyFormat("int *a = f1();", Style);
2004 verifyFormat("int &b = f2();", Style);
2005 verifyFormat("int &&c = f3();", Style);
2006 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2007 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2008 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2009 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2010 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2011 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2012 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2013 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2014 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2015 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2016 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2017 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2018 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2019 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2020 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2021 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2022 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2023 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2024 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2025 verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2026 verifyFormat(
2027 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2028 " res2 = [](int &a) { return 0000000000000; };",
2029 Style);
2031 Style.AlignConsecutiveDeclarations.Enabled = true;
2032 verifyFormat("Const unsigned int *c;\n"
2033 "const unsigned int *d;\n"
2034 "Const unsigned int &e;\n"
2035 "const unsigned int &f;\n"
2036 "const unsigned &&g;\n"
2037 "Const unsigned h;",
2038 Style);
2040 Style.PointerAlignment = FormatStyle::PAS_Left;
2041 Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2042 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2043 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2044 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2045 verifyFormat("int* f1(int& a) const& = 0;", Style);
2046 verifyFormat("int* a = f1();", Style);
2047 verifyFormat("int& b = f2();", Style);
2048 verifyFormat("int&& c = f3();", Style);
2049 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2050 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2051 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2052 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2053 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2054 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2055 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2056 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2057 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2058 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2059 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2060 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2061 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2062 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2063 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2064 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2065 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2066 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2067 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2068 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2069 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2070 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2071 verifyFormat(
2072 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2073 " res2 = [](int& a) { return 0000000000000; };",
2074 Style);
2076 Style.AlignConsecutiveDeclarations.Enabled = true;
2077 verifyFormat("Const unsigned int* c;\n"
2078 "const unsigned int* d;\n"
2079 "Const unsigned int& e;\n"
2080 "const unsigned int& f;\n"
2081 "const unsigned&& g;\n"
2082 "Const unsigned h;",
2083 Style);
2085 Style.PointerAlignment = FormatStyle::PAS_Right;
2086 Style.ReferenceAlignment = FormatStyle::RAS_Left;
2087 verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2088 verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2089 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2090 verifyFormat("int *a = f1();", Style);
2091 verifyFormat("int& b = f2();", Style);
2092 verifyFormat("int&& c = f3();", Style);
2093 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2094 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2095 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2096 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2097 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2098 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2099 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2101 Style.AlignConsecutiveDeclarations.Enabled = true;
2102 verifyFormat("Const unsigned int *c;\n"
2103 "const unsigned int *d;\n"
2104 "Const unsigned int& e;\n"
2105 "const unsigned int& f;\n"
2106 "const unsigned g;\n"
2107 "Const unsigned h;",
2108 Style);
2110 Style.PointerAlignment = FormatStyle::PAS_Left;
2111 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2112 verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2113 verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2114 verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2115 verifyFormat("int* a = f1();", Style);
2116 verifyFormat("int & b = f2();", Style);
2117 verifyFormat("int && c = f3();", Style);
2118 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2119 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2120 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2121 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2122 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2123 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2124 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2125 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2126 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2127 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2128 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2129 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2130 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2131 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2132 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2133 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2134 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2135 verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2136 verifyFormat(
2137 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2138 " res2 = [](int & a) { return 0000000000000; };",
2139 Style);
2141 Style.AlignConsecutiveDeclarations.Enabled = true;
2142 verifyFormat("Const unsigned int* c;\n"
2143 "const unsigned int* d;\n"
2144 "Const unsigned int & e;\n"
2145 "const unsigned int & f;\n"
2146 "const unsigned && g;\n"
2147 "Const unsigned h;",
2148 Style);
2150 Style.PointerAlignment = FormatStyle::PAS_Middle;
2151 Style.ReferenceAlignment = FormatStyle::RAS_Right;
2152 verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2153 verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2154 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2155 verifyFormat("int * a = f1();", Style);
2156 verifyFormat("int &b = f2();", Style);
2157 verifyFormat("int &&c = f3();", Style);
2158 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2159 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2160 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2161 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2162 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2163 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2164 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2166 Style.AlignConsecutiveDeclarations.Enabled = true;
2167 verifyFormat("Const unsigned int * c;\n"
2168 "const unsigned int * d;\n"
2169 "Const unsigned int &e;\n"
2170 "const unsigned int &f;\n"
2171 "const unsigned &&g;\n"
2172 "Const unsigned h;",
2173 Style);
2175 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2176 // specifically handled
2177 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2180 TEST_F(FormatTest, FormatsForLoop) {
2181 verifyFormat(
2182 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2183 " ++VeryVeryLongLoopVariable)\n"
2184 " ;");
2185 verifyFormat("for (;;)\n"
2186 " f();");
2187 verifyFormat("for (;;) {\n}");
2188 verifyFormat("for (;;) {\n"
2189 " f();\n"
2190 "}");
2191 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2193 verifyFormat(
2194 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2195 " E = UnwrappedLines.end();\n"
2196 " I != E; ++I) {\n}");
2198 verifyFormat(
2199 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2200 " ++IIIII) {\n}");
2201 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2202 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2203 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2204 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2205 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2206 " E = FD->getDeclsInPrototypeScope().end();\n"
2207 " I != E; ++I) {\n}");
2208 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2209 " I = Container.begin(),\n"
2210 " E = Container.end();\n"
2211 " I != E; ++I) {\n}",
2212 getLLVMStyleWithColumns(76));
2214 verifyFormat(
2215 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2219 " ++aaaaaaaaaaa) {\n}");
2220 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2221 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2222 " ++i) {\n}");
2223 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2224 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2225 "}");
2226 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2227 " aaaaaaaaaa);\n"
2228 " iter; ++iter) {\n"
2229 "}");
2230 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2232 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2233 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2235 // These should not be formatted as Objective-C for-in loops.
2236 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2237 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2238 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2239 verifyFormat(
2240 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2242 FormatStyle NoBinPacking = getLLVMStyle();
2243 NoBinPacking.BinPackParameters = false;
2244 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2245 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2246 " aaaaaaaaaaaaaaaa,\n"
2247 " aaaaaaaaaaaaaaaa,\n"
2248 " aaaaaaaaaaaaaaaa);\n"
2249 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2250 "}",
2251 NoBinPacking);
2252 verifyFormat(
2253 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2254 " E = UnwrappedLines.end();\n"
2255 " I != E;\n"
2256 " ++I) {\n}",
2257 NoBinPacking);
2259 FormatStyle AlignLeft = getLLVMStyle();
2260 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2261 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2264 TEST_F(FormatTest, RangeBasedForLoops) {
2265 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2267 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2268 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2269 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2271 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2272 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2275 TEST_F(FormatTest, ForEachLoops) {
2276 FormatStyle Style = getLLVMStyle();
2277 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2278 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2279 verifyFormat("void f() {\n"
2280 " for (;;) {\n"
2281 " }\n"
2282 " foreach (Item *item, itemlist) {\n"
2283 " }\n"
2284 " Q_FOREACH (Item *item, itemlist) {\n"
2285 " }\n"
2286 " BOOST_FOREACH (Item *item, itemlist) {\n"
2287 " }\n"
2288 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2289 "}",
2290 Style);
2291 verifyFormat("void f() {\n"
2292 " for (;;)\n"
2293 " int j = 1;\n"
2294 " Q_FOREACH (int v, vec)\n"
2295 " v *= 2;\n"
2296 " for (;;) {\n"
2297 " int j = 1;\n"
2298 " }\n"
2299 " Q_FOREACH (int v, vec) {\n"
2300 " v *= 2;\n"
2301 " }\n"
2302 "}",
2303 Style);
2305 FormatStyle ShortBlocks = getLLVMStyle();
2306 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2307 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2308 verifyFormat("void f() {\n"
2309 " for (;;)\n"
2310 " int j = 1;\n"
2311 " Q_FOREACH (int &v, vec)\n"
2312 " v *= 2;\n"
2313 " for (;;) {\n"
2314 " int j = 1;\n"
2315 " }\n"
2316 " Q_FOREACH (int &v, vec) {\n"
2317 " int j = 1;\n"
2318 " }\n"
2319 "}",
2320 ShortBlocks);
2322 FormatStyle ShortLoops = getLLVMStyle();
2323 ShortLoops.AllowShortLoopsOnASingleLine = true;
2324 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2325 verifyFormat("void f() {\n"
2326 " for (;;) int j = 1;\n"
2327 " Q_FOREACH (int &v, vec) int j = 1;\n"
2328 " for (;;) {\n"
2329 " int j = 1;\n"
2330 " }\n"
2331 " Q_FOREACH (int &v, vec) {\n"
2332 " int j = 1;\n"
2333 " }\n"
2334 "}",
2335 ShortLoops);
2337 FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2338 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2339 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2340 verifyFormat("void f() {\n"
2341 " for (;;) int j = 1;\n"
2342 " Q_FOREACH (int &v, vec) int j = 1;\n"
2343 " for (;;) { int j = 1; }\n"
2344 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2345 "}",
2346 ShortBlocksAndLoops);
2348 Style.SpaceBeforeParens =
2349 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2350 verifyFormat("void f() {\n"
2351 " for (;;) {\n"
2352 " }\n"
2353 " foreach(Item *item, itemlist) {\n"
2354 " }\n"
2355 " Q_FOREACH(Item *item, itemlist) {\n"
2356 " }\n"
2357 " BOOST_FOREACH(Item *item, itemlist) {\n"
2358 " }\n"
2359 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2360 "}",
2361 Style);
2363 // As function-like macros.
2364 verifyFormat("#define foreach(x, y)\n"
2365 "#define Q_FOREACH(x, y)\n"
2366 "#define BOOST_FOREACH(x, y)\n"
2367 "#define UNKNOWN_FOREACH(x, y)");
2369 // Not as function-like macros.
2370 verifyFormat("#define foreach (x, y)\n"
2371 "#define Q_FOREACH (x, y)\n"
2372 "#define BOOST_FOREACH (x, y)\n"
2373 "#define UNKNOWN_FOREACH (x, y)");
2375 // handle microsoft non standard extension
2376 verifyFormat("for each (char c in x->MyStringProperty)");
2379 TEST_F(FormatTest, FormatsWhileLoop) {
2380 verifyFormat("while (true) {\n}");
2381 verifyFormat("while (true)\n"
2382 " f();");
2383 verifyFormat("while () {\n}");
2384 verifyFormat("while () {\n"
2385 " f();\n"
2386 "}");
2389 TEST_F(FormatTest, FormatsDoWhile) {
2390 verifyFormat("do {\n"
2391 " do_something();\n"
2392 "} while (something());");
2393 verifyFormat("do\n"
2394 " do_something();\n"
2395 "while (something());");
2398 TEST_F(FormatTest, FormatsSwitchStatement) {
2399 verifyFormat("switch (x) {\n"
2400 "case 1:\n"
2401 " f();\n"
2402 " break;\n"
2403 "case kFoo:\n"
2404 "case ns::kBar:\n"
2405 "case kBaz:\n"
2406 " break;\n"
2407 "default:\n"
2408 " g();\n"
2409 " break;\n"
2410 "}");
2411 verifyFormat("switch (x) {\n"
2412 "case 1: {\n"
2413 " f();\n"
2414 " break;\n"
2415 "}\n"
2416 "case 2: {\n"
2417 " break;\n"
2418 "}\n"
2419 "}");
2420 verifyFormat("switch (x) {\n"
2421 "case 1: {\n"
2422 " f();\n"
2423 " {\n"
2424 " g();\n"
2425 " h();\n"
2426 " }\n"
2427 " break;\n"
2428 "}\n"
2429 "}");
2430 verifyFormat("switch (x) {\n"
2431 "case 1: {\n"
2432 " f();\n"
2433 " if (foo) {\n"
2434 " g();\n"
2435 " h();\n"
2436 " }\n"
2437 " break;\n"
2438 "}\n"
2439 "}");
2440 verifyFormat("switch (x) {\n"
2441 "case 1: {\n"
2442 " f();\n"
2443 " g();\n"
2444 "} break;\n"
2445 "}");
2446 verifyFormat("switch (test)\n"
2447 " ;");
2448 verifyFormat("switch (x) {\n"
2449 "default: {\n"
2450 " // Do nothing.\n"
2451 "}\n"
2452 "}");
2453 verifyFormat("switch (x) {\n"
2454 "// comment\n"
2455 "// if 1, do f()\n"
2456 "case 1:\n"
2457 " f();\n"
2458 "}");
2459 verifyFormat("switch (x) {\n"
2460 "case 1:\n"
2461 " // Do amazing stuff\n"
2462 " {\n"
2463 " f();\n"
2464 " g();\n"
2465 " }\n"
2466 " break;\n"
2467 "}");
2468 verifyFormat("#define A \\\n"
2469 " switch (x) { \\\n"
2470 " case a: \\\n"
2471 " foo = b; \\\n"
2472 " }",
2473 getLLVMStyleWithColumns(20));
2474 verifyFormat("#define OPERATION_CASE(name) \\\n"
2475 " case OP_name: \\\n"
2476 " return operations::Operation##name",
2477 getLLVMStyleWithColumns(40));
2478 verifyFormat("switch (x) {\n"
2479 "case 1:;\n"
2480 "default:;\n"
2481 " int i;\n"
2482 "}");
2484 verifyGoogleFormat("switch (x) {\n"
2485 " case 1:\n"
2486 " f();\n"
2487 " break;\n"
2488 " case kFoo:\n"
2489 " case ns::kBar:\n"
2490 " case kBaz:\n"
2491 " break;\n"
2492 " default:\n"
2493 " g();\n"
2494 " break;\n"
2495 "}");
2496 verifyGoogleFormat("switch (x) {\n"
2497 " case 1: {\n"
2498 " f();\n"
2499 " break;\n"
2500 " }\n"
2501 "}");
2502 verifyGoogleFormat("switch (test)\n"
2503 " ;");
2505 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2506 " case OP_name: \\\n"
2507 " return operations::Operation##name");
2508 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2509 " // Get the correction operation class.\n"
2510 " switch (OpCode) {\n"
2511 " CASE(Add);\n"
2512 " CASE(Subtract);\n"
2513 " default:\n"
2514 " return operations::Unknown;\n"
2515 " }\n"
2516 "#undef OPERATION_CASE\n"
2517 "}");
2518 verifyFormat("DEBUG({\n"
2519 " switch (x) {\n"
2520 " case A:\n"
2521 " f();\n"
2522 " break;\n"
2523 " // fallthrough\n"
2524 " case B:\n"
2525 " g();\n"
2526 " break;\n"
2527 " }\n"
2528 "});");
2529 verifyNoChange("DEBUG({\n"
2530 " switch (x) {\n"
2531 " case A:\n"
2532 " f();\n"
2533 " break;\n"
2534 " // On B:\n"
2535 " case B:\n"
2536 " g();\n"
2537 " break;\n"
2538 " }\n"
2539 "});");
2540 verifyFormat("switch (n) {\n"
2541 "case 0: {\n"
2542 " return false;\n"
2543 "}\n"
2544 "default: {\n"
2545 " return true;\n"
2546 "}\n"
2547 "}",
2548 "switch (n)\n"
2549 "{\n"
2550 "case 0: {\n"
2551 " return false;\n"
2552 "}\n"
2553 "default: {\n"
2554 " return true;\n"
2555 "}\n"
2556 "}");
2557 verifyFormat("switch (a) {\n"
2558 "case (b):\n"
2559 " return;\n"
2560 "}");
2562 verifyFormat("switch (a) {\n"
2563 "case some_namespace::\n"
2564 " some_constant:\n"
2565 " return;\n"
2566 "}",
2567 getLLVMStyleWithColumns(34));
2569 verifyFormat("switch (a) {\n"
2570 "[[likely]] case 1:\n"
2571 " return;\n"
2572 "}");
2573 verifyFormat("switch (a) {\n"
2574 "[[likely]] [[other::likely]] case 1:\n"
2575 " return;\n"
2576 "}");
2577 verifyFormat("switch (x) {\n"
2578 "case 1:\n"
2579 " return;\n"
2580 "[[likely]] case 2:\n"
2581 " return;\n"
2582 "}");
2583 verifyFormat("switch (a) {\n"
2584 "case 1:\n"
2585 "[[likely]] case 2:\n"
2586 " return;\n"
2587 "}");
2588 FormatStyle Attributes = getLLVMStyle();
2589 Attributes.AttributeMacros.push_back("LIKELY");
2590 Attributes.AttributeMacros.push_back("OTHER_LIKELY");
2591 verifyFormat("switch (a) {\n"
2592 "LIKELY case b:\n"
2593 " return;\n"
2594 "}",
2595 Attributes);
2596 verifyFormat("switch (a) {\n"
2597 "LIKELY OTHER_LIKELY() case b:\n"
2598 " return;\n"
2599 "}",
2600 Attributes);
2601 verifyFormat("switch (a) {\n"
2602 "case 1:\n"
2603 " return;\n"
2604 "LIKELY case 2:\n"
2605 " return;\n"
2606 "}",
2607 Attributes);
2608 verifyFormat("switch (a) {\n"
2609 "case 1:\n"
2610 "LIKELY case 2:\n"
2611 " return;\n"
2612 "}",
2613 Attributes);
2615 FormatStyle Style = getLLVMStyle();
2616 Style.IndentCaseLabels = true;
2617 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2618 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2619 Style.BraceWrapping.AfterCaseLabel = true;
2620 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2621 verifyFormat("switch (n)\n"
2622 "{\n"
2623 " case 0:\n"
2624 " {\n"
2625 " return false;\n"
2626 " }\n"
2627 " default:\n"
2628 " {\n"
2629 " return true;\n"
2630 " }\n"
2631 "}",
2632 "switch (n) {\n"
2633 " case 0: {\n"
2634 " return false;\n"
2635 " }\n"
2636 " default: {\n"
2637 " return true;\n"
2638 " }\n"
2639 "}",
2640 Style);
2641 Style.BraceWrapping.AfterCaseLabel = false;
2642 verifyFormat("switch (n)\n"
2643 "{\n"
2644 " case 0: {\n"
2645 " return false;\n"
2646 " }\n"
2647 " default: {\n"
2648 " return true;\n"
2649 " }\n"
2650 "}",
2651 "switch (n) {\n"
2652 " case 0:\n"
2653 " {\n"
2654 " return false;\n"
2655 " }\n"
2656 " default:\n"
2657 " {\n"
2658 " return true;\n"
2659 " }\n"
2660 "}",
2661 Style);
2662 Style.IndentCaseLabels = false;
2663 Style.IndentCaseBlocks = true;
2664 verifyFormat("switch (n)\n"
2665 "{\n"
2666 "case 0:\n"
2667 " {\n"
2668 " return false;\n"
2669 " }\n"
2670 "case 1:\n"
2671 " break;\n"
2672 "default:\n"
2673 " {\n"
2674 " return true;\n"
2675 " }\n"
2676 "}",
2677 "switch (n) {\n"
2678 "case 0: {\n"
2679 " return false;\n"
2680 "}\n"
2681 "case 1:\n"
2682 " break;\n"
2683 "default: {\n"
2684 " return true;\n"
2685 "}\n"
2686 "}",
2687 Style);
2688 Style.IndentCaseLabels = true;
2689 Style.IndentCaseBlocks = true;
2690 verifyFormat("switch (n)\n"
2691 "{\n"
2692 " case 0:\n"
2693 " {\n"
2694 " return false;\n"
2695 " }\n"
2696 " case 1:\n"
2697 " break;\n"
2698 " default:\n"
2699 " {\n"
2700 " return true;\n"
2701 " }\n"
2702 "}",
2703 "switch (n) {\n"
2704 "case 0: {\n"
2705 " return false;\n"
2706 "}\n"
2707 "case 1:\n"
2708 " break;\n"
2709 "default: {\n"
2710 " return true;\n"
2711 "}\n"
2712 "}",
2713 Style);
2716 TEST_F(FormatTest, CaseRanges) {
2717 verifyFormat("switch (x) {\n"
2718 "case 'A' ... 'Z':\n"
2719 "case 1 ... 5:\n"
2720 "case a ... b:\n"
2721 " break;\n"
2722 "}");
2725 TEST_F(FormatTest, ShortEnums) {
2726 FormatStyle Style = getLLVMStyle();
2727 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2728 EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2729 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2730 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2731 Style.AllowShortEnumsOnASingleLine = false;
2732 verifyFormat("enum {\n"
2733 " A,\n"
2734 " B,\n"
2735 " C\n"
2736 "} ShortEnum1, ShortEnum2;",
2737 Style);
2738 verifyFormat("typedef enum {\n"
2739 " A,\n"
2740 " B,\n"
2741 " C\n"
2742 "} ShortEnum1, ShortEnum2;",
2743 Style);
2744 verifyFormat("enum {\n"
2745 " A,\n"
2746 "} ShortEnum1, ShortEnum2;",
2747 Style);
2748 verifyFormat("typedef enum {\n"
2749 " A,\n"
2750 "} ShortEnum1, ShortEnum2;",
2751 Style);
2752 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2753 Style.BraceWrapping.AfterEnum = true;
2754 verifyFormat("enum\n"
2755 "{\n"
2756 " A,\n"
2757 " B,\n"
2758 " C\n"
2759 "} ShortEnum1, ShortEnum2;",
2760 Style);
2761 verifyFormat("typedef enum\n"
2762 "{\n"
2763 " A,\n"
2764 " B,\n"
2765 " C\n"
2766 "} ShortEnum1, ShortEnum2;",
2767 Style);
2770 TEST_F(FormatTest, ShortCompoundRequirement) {
2771 FormatStyle Style = getLLVMStyle();
2772 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2773 verifyFormat("template <typename T>\n"
2774 "concept c = requires(T x) {\n"
2775 " { x + 1 } -> std::same_as<int>;\n"
2776 "};",
2777 Style);
2778 verifyFormat("template <typename T>\n"
2779 "concept c = requires(T x) {\n"
2780 " { x + 1 } -> std::same_as<int>;\n"
2781 " { x + 2 } -> std::same_as<int>;\n"
2782 "};",
2783 Style);
2784 Style.AllowShortCompoundRequirementOnASingleLine = false;
2785 verifyFormat("template <typename T>\n"
2786 "concept c = requires(T x) {\n"
2787 " {\n"
2788 " x + 1\n"
2789 " } -> std::same_as<int>;\n"
2790 "};",
2791 Style);
2792 verifyFormat("template <typename T>\n"
2793 "concept c = requires(T x) {\n"
2794 " {\n"
2795 " x + 1\n"
2796 " } -> std::same_as<int>;\n"
2797 " {\n"
2798 " x + 2\n"
2799 " } -> std::same_as<int>;\n"
2800 "};",
2801 Style);
2804 TEST_F(FormatTest, ShortCaseLabels) {
2805 FormatStyle Style = getLLVMStyle();
2806 Style.AllowShortCaseLabelsOnASingleLine = true;
2807 verifyFormat("switch (a) {\n"
2808 "case 1: x = 1; break;\n"
2809 "case 2: return;\n"
2810 "case 3:\n"
2811 "case 4:\n"
2812 "case 5: return;\n"
2813 "case 6: // comment\n"
2814 " return;\n"
2815 "case 7:\n"
2816 " // comment\n"
2817 " return;\n"
2818 "case 8:\n"
2819 " x = 8; // comment\n"
2820 " break;\n"
2821 "default: y = 1; break;\n"
2822 "}",
2823 Style);
2824 verifyFormat("switch (a) {\n"
2825 "case 0: return; // comment\n"
2826 "case 1: break; // comment\n"
2827 "case 2: return;\n"
2828 "// comment\n"
2829 "case 3: return;\n"
2830 "// comment 1\n"
2831 "// comment 2\n"
2832 "// comment 3\n"
2833 "case 4: break; /* comment */\n"
2834 "case 5:\n"
2835 " // comment\n"
2836 " break;\n"
2837 "case 6: /* comment */ x = 1; break;\n"
2838 "case 7: x = /* comment */ 1; break;\n"
2839 "case 8:\n"
2840 " x = 1; /* comment */\n"
2841 " break;\n"
2842 "case 9:\n"
2843 " break; // comment line 1\n"
2844 " // comment line 2\n"
2845 "}",
2846 Style);
2847 verifyFormat("switch (a) {\n"
2848 "case 1:\n"
2849 " x = 8;\n"
2850 " // fall through\n"
2851 "case 2: x = 8;\n"
2852 "// comment\n"
2853 "case 3:\n"
2854 " return; /* comment line 1\n"
2855 " * comment line 2 */\n"
2856 "case 4: i = 8;\n"
2857 "// something else\n"
2858 "#if FOO\n"
2859 "case 5: break;\n"
2860 "#endif\n"
2861 "}",
2862 "switch (a) {\n"
2863 "case 1: x = 8;\n"
2864 " // fall through\n"
2865 "case 2:\n"
2866 " x = 8;\n"
2867 "// comment\n"
2868 "case 3:\n"
2869 " return; /* comment line 1\n"
2870 " * comment line 2 */\n"
2871 "case 4:\n"
2872 " i = 8;\n"
2873 "// something else\n"
2874 "#if FOO\n"
2875 "case 5: break;\n"
2876 "#endif\n"
2877 "}",
2878 Style);
2879 verifyFormat("switch (a) {\n"
2880 "case 0:\n"
2881 " return; // long long long long long long long long long long "
2882 "long long comment\n"
2883 " // line\n"
2884 "}",
2885 "switch (a) {\n"
2886 "case 0: return; // long long long long long long long long "
2887 "long long long long comment line\n"
2888 "}",
2889 Style);
2890 verifyFormat("switch (a) {\n"
2891 "case 0:\n"
2892 " return; /* long long long long long long long long long long "
2893 "long long comment\n"
2894 " line */\n"
2895 "}",
2896 "switch (a) {\n"
2897 "case 0: return; /* long long long long long long long long "
2898 "long long long long comment line */\n"
2899 "}",
2900 Style);
2901 verifyFormat("switch (a) {\n"
2902 "#if FOO\n"
2903 "case 0: return 0;\n"
2904 "#endif\n"
2905 "}",
2906 Style);
2907 verifyFormat("switch (a) {\n"
2908 "case 1: {\n"
2909 "}\n"
2910 "case 2: {\n"
2911 " return;\n"
2912 "}\n"
2913 "case 3: {\n"
2914 " x = 1;\n"
2915 " return;\n"
2916 "}\n"
2917 "case 4:\n"
2918 " if (x)\n"
2919 " return;\n"
2920 "}",
2921 Style);
2922 Style.ColumnLimit = 21;
2923 verifyFormat("#define X \\\n"
2924 " case 0: break;\n"
2925 "#include \"f\"",
2926 Style);
2927 verifyFormat("switch (a) {\n"
2928 "case 1: x = 1; break;\n"
2929 "case 2: return;\n"
2930 "case 3:\n"
2931 "case 4:\n"
2932 "case 5: return;\n"
2933 "default:\n"
2934 " y = 1;\n"
2935 " break;\n"
2936 "}",
2937 Style);
2938 Style.ColumnLimit = 80;
2939 Style.AllowShortCaseLabelsOnASingleLine = false;
2940 Style.IndentCaseLabels = true;
2941 verifyFormat("switch (n) {\n"
2942 " default /*comments*/:\n"
2943 " return true;\n"
2944 " case 0:\n"
2945 " return false;\n"
2946 "}",
2947 "switch (n) {\n"
2948 "default/*comments*/:\n"
2949 " return true;\n"
2950 "case 0:\n"
2951 " return false;\n"
2952 "}",
2953 Style);
2954 Style.AllowShortCaseLabelsOnASingleLine = true;
2955 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2956 Style.BraceWrapping.AfterCaseLabel = true;
2957 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2958 verifyFormat("switch (n)\n"
2959 "{\n"
2960 " case 0:\n"
2961 " {\n"
2962 " return false;\n"
2963 " }\n"
2964 " default:\n"
2965 " {\n"
2966 " return true;\n"
2967 " }\n"
2968 "}",
2969 "switch (n) {\n"
2970 " case 0: {\n"
2971 " return false;\n"
2972 " }\n"
2973 " default:\n"
2974 " {\n"
2975 " return true;\n"
2976 " }\n"
2977 "}",
2978 Style);
2981 TEST_F(FormatTest, FormatsLabels) {
2982 verifyFormat("void f() {\n"
2983 " some_code();\n"
2984 "test_label:\n"
2985 " some_other_code();\n"
2986 " {\n"
2987 " some_more_code();\n"
2988 " another_label:\n"
2989 " some_more_code();\n"
2990 " }\n"
2991 "}");
2992 verifyFormat("{\n"
2993 " some_code();\n"
2994 "test_label:\n"
2995 " some_other_code();\n"
2996 "}");
2997 verifyFormat("{\n"
2998 " some_code();\n"
2999 "test_label:;\n"
3000 " int i = 0;\n"
3001 "}");
3002 verifyFormat("{\n"
3003 " some_code();\n"
3004 "test_label: { some_other_code(); }\n"
3005 "}");
3006 verifyFormat("{\n"
3007 " some_code();\n"
3008 "test_label: {\n"
3009 " some_other_code();\n"
3010 " some_other_code();\n"
3011 "}\n"
3012 "}");
3013 verifyFormat("{\n"
3014 "L0:\n"
3015 "[[foo]] L1:\n"
3016 "[[bar]] [[baz]] L2:\n"
3017 " g();\n"
3018 "}");
3019 verifyFormat("{\n"
3020 "[[foo]] L1: {\n"
3021 "[[bar]] [[baz]] L2:\n"
3022 " g();\n"
3023 "}\n"
3024 "}");
3025 verifyFormat("{\n"
3026 "[[foo]] L1:\n"
3027 " f();\n"
3028 " {\n"
3029 " [[bar]] [[baz]] L2:\n"
3030 " g();\n"
3031 " }\n"
3032 "}");
3033 FormatStyle Style = getLLVMStyle();
3034 Style.IndentGotoLabels = false;
3035 verifyFormat("void f() {\n"
3036 " some_code();\n"
3037 "test_label:\n"
3038 " some_other_code();\n"
3039 " {\n"
3040 " some_more_code();\n"
3041 "another_label:\n"
3042 " some_more_code();\n"
3043 " }\n"
3044 "}",
3045 Style);
3046 verifyFormat("{\n"
3047 " some_code();\n"
3048 "test_label:\n"
3049 " some_other_code();\n"
3050 "}",
3051 Style);
3052 verifyFormat("{\n"
3053 " some_code();\n"
3054 "test_label:;\n"
3055 " int i = 0;\n"
3056 "}",
3057 Style);
3058 verifyFormat("{\n"
3059 " some_code();\n"
3060 "test_label: { some_other_code(); }\n"
3061 "}",
3062 Style);
3063 verifyFormat("{\n"
3064 "[[foo]] L1:\n"
3065 " f();\n"
3066 " {\n"
3067 "[[bar]] [[baz]] L2:\n"
3068 " g();\n"
3069 " }\n"
3070 "}",
3071 Style);
3072 // The opening brace may either be on the same unwrapped line as the colon or
3073 // on a separate one. The formatter should recognize both.
3074 Style = getLLVMStyle();
3075 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman;
3076 verifyFormat("{\n"
3077 " some_code();\n"
3078 "test_label:\n"
3079 "{\n"
3080 " some_other_code();\n"
3081 "}\n"
3082 "}",
3083 Style);
3084 verifyFormat("{\n"
3085 "[[foo]] L1:\n"
3086 "{\n"
3087 "[[bar]] [[baz]] L2:\n"
3088 " g();\n"
3089 "}\n"
3090 "}",
3091 Style);
3094 TEST_F(FormatTest, MultiLineControlStatements) {
3095 FormatStyle Style = getLLVMStyleWithColumns(20);
3096 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3097 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3098 // Short lines should keep opening brace on same line.
3099 verifyFormat("if (foo) {\n"
3100 " bar();\n"
3101 "}",
3102 "if(foo){bar();}", Style);
3103 verifyFormat("if (foo) {\n"
3104 " bar();\n"
3105 "} else {\n"
3106 " baz();\n"
3107 "}",
3108 "if(foo){bar();}else{baz();}", Style);
3109 verifyFormat("if (foo && bar) {\n"
3110 " baz();\n"
3111 "}",
3112 "if(foo&&bar){baz();}", Style);
3113 verifyFormat("if (foo) {\n"
3114 " bar();\n"
3115 "} else if (baz) {\n"
3116 " quux();\n"
3117 "}",
3118 "if(foo){bar();}else if(baz){quux();}", Style);
3119 verifyFormat("if (foo) {\n"
3120 " bar();\n"
3121 "} else if (baz) {\n"
3122 " quux();\n"
3123 "} else {\n"
3124 " foobar();\n"
3125 "}",
3126 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3127 verifyFormat("for (;;) {\n"
3128 " foo();\n"
3129 "}",
3130 "for(;;){foo();}");
3131 verifyFormat("while (1) {\n"
3132 " foo();\n"
3133 "}",
3134 "while(1){foo();}", Style);
3135 verifyFormat("switch (foo) {\n"
3136 "case bar:\n"
3137 " return;\n"
3138 "}",
3139 "switch(foo){case bar:return;}", Style);
3140 verifyFormat("try {\n"
3141 " foo();\n"
3142 "} catch (...) {\n"
3143 " bar();\n"
3144 "}",
3145 "try{foo();}catch(...){bar();}", Style);
3146 verifyFormat("do {\n"
3147 " foo();\n"
3148 "} while (bar &&\n"
3149 " baz);",
3150 "do{foo();}while(bar&&baz);", Style);
3151 // Long lines should put opening brace on new line.
3152 verifyFormat("void f() {\n"
3153 " if (a1 && a2 &&\n"
3154 " a3)\n"
3155 " {\n"
3156 " quux();\n"
3157 " }\n"
3158 "}",
3159 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3160 verifyFormat("if (foo && bar &&\n"
3161 " baz)\n"
3162 "{\n"
3163 " quux();\n"
3164 "}",
3165 "if(foo&&bar&&baz){quux();}", Style);
3166 verifyFormat("if (foo && bar &&\n"
3167 " baz)\n"
3168 "{\n"
3169 " quux();\n"
3170 "}",
3171 "if (foo && bar &&\n"
3172 " baz) {\n"
3173 " quux();\n"
3174 "}",
3175 Style);
3176 verifyFormat("if (foo) {\n"
3177 " bar();\n"
3178 "} else if (baz ||\n"
3179 " quux)\n"
3180 "{\n"
3181 " foobar();\n"
3182 "}",
3183 "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3184 verifyFormat("if (foo) {\n"
3185 " bar();\n"
3186 "} else if (baz ||\n"
3187 " quux)\n"
3188 "{\n"
3189 " foobar();\n"
3190 "} else {\n"
3191 " barbaz();\n"
3192 "}",
3193 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3194 Style);
3195 verifyFormat("for (int i = 0;\n"
3196 " i < 10; ++i)\n"
3197 "{\n"
3198 " foo();\n"
3199 "}",
3200 "for(int i=0;i<10;++i){foo();}", Style);
3201 verifyFormat("foreach (int i,\n"
3202 " list)\n"
3203 "{\n"
3204 " foo();\n"
3205 "}",
3206 "foreach(int i, list){foo();}", Style);
3207 Style.ColumnLimit =
3208 40; // to concentrate at brace wrapping, not line wrap due to column limit
3209 verifyFormat("foreach (int i, list) {\n"
3210 " foo();\n"
3211 "}",
3212 "foreach(int i, list){foo();}", Style);
3213 Style.ColumnLimit =
3214 20; // to concentrate at brace wrapping, not line wrap due to column limit
3215 verifyFormat("while (foo || bar ||\n"
3216 " baz)\n"
3217 "{\n"
3218 " quux();\n"
3219 "}",
3220 "while(foo||bar||baz){quux();}", Style);
3221 verifyFormat("switch (\n"
3222 " foo = barbaz)\n"
3223 "{\n"
3224 "case quux:\n"
3225 " return;\n"
3226 "}",
3227 "switch(foo=barbaz){case quux:return;}", Style);
3228 verifyFormat("try {\n"
3229 " foo();\n"
3230 "} catch (\n"
3231 " Exception &bar)\n"
3232 "{\n"
3233 " baz();\n"
3234 "}",
3235 "try{foo();}catch(Exception&bar){baz();}", Style);
3236 Style.ColumnLimit =
3237 40; // to concentrate at brace wrapping, not line wrap due to column limit
3238 verifyFormat("try {\n"
3239 " foo();\n"
3240 "} catch (Exception &bar) {\n"
3241 " baz();\n"
3242 "}",
3243 "try{foo();}catch(Exception&bar){baz();}", Style);
3244 Style.ColumnLimit =
3245 20; // to concentrate at brace wrapping, not line wrap due to column limit
3247 Style.BraceWrapping.BeforeElse = true;
3248 verifyFormat("if (foo) {\n"
3249 " bar();\n"
3250 "}\n"
3251 "else if (baz ||\n"
3252 " quux)\n"
3253 "{\n"
3254 " foobar();\n"
3255 "}\n"
3256 "else {\n"
3257 " barbaz();\n"
3258 "}",
3259 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3260 Style);
3262 Style.BraceWrapping.BeforeCatch = true;
3263 verifyFormat("try {\n"
3264 " foo();\n"
3265 "}\n"
3266 "catch (...) {\n"
3267 " baz();\n"
3268 "}",
3269 "try{foo();}catch(...){baz();}", Style);
3271 Style.BraceWrapping.AfterFunction = true;
3272 Style.BraceWrapping.AfterStruct = false;
3273 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3274 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3275 Style.ColumnLimit = 80;
3276 verifyFormat("void shortfunction() { bar(); }", Style);
3277 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3278 verifyFormat("struct T {};", Style);
3280 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3281 verifyFormat("void shortfunction()\n"
3282 "{\n"
3283 " bar();\n"
3284 "}",
3285 Style);
3286 verifyFormat("struct T shortfunction()\n"
3287 "{\n"
3288 " return bar();\n"
3289 "}",
3290 Style);
3291 verifyFormat("struct T {};", Style);
3293 Style.BraceWrapping.AfterFunction = false;
3294 Style.BraceWrapping.AfterStruct = true;
3295 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3296 verifyFormat("void shortfunction() { bar(); }", Style);
3297 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3298 verifyFormat("struct T\n"
3299 "{\n"
3300 "};",
3301 Style);
3303 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3304 verifyFormat("void shortfunction() {\n"
3305 " bar();\n"
3306 "}",
3307 Style);
3308 verifyFormat("struct T shortfunction() {\n"
3309 " return bar();\n"
3310 "}",
3311 Style);
3312 verifyFormat("struct T\n"
3313 "{\n"
3314 "};",
3315 Style);
3318 TEST_F(FormatTest, BeforeWhile) {
3319 FormatStyle Style = getLLVMStyle();
3320 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3322 verifyFormat("do {\n"
3323 " foo();\n"
3324 "} while (1);",
3325 Style);
3326 Style.BraceWrapping.BeforeWhile = true;
3327 verifyFormat("do {\n"
3328 " foo();\n"
3329 "}\n"
3330 "while (1);",
3331 Style);
3334 //===----------------------------------------------------------------------===//
3335 // Tests for classes, namespaces, etc.
3336 //===----------------------------------------------------------------------===//
3338 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3339 verifyFormat("class A {};");
3342 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3343 verifyFormat("class A {\n"
3344 "public:\n"
3345 "public: // comment\n"
3346 "protected:\n"
3347 "private:\n"
3348 " void f() {}\n"
3349 "};");
3350 verifyFormat("export class A {\n"
3351 "public:\n"
3352 "public: // comment\n"
3353 "protected:\n"
3354 "private:\n"
3355 " void f() {}\n"
3356 "};");
3357 verifyGoogleFormat("class A {\n"
3358 " public:\n"
3359 " protected:\n"
3360 " private:\n"
3361 " void f() {}\n"
3362 "};");
3363 verifyGoogleFormat("export class A {\n"
3364 " public:\n"
3365 " protected:\n"
3366 " private:\n"
3367 " void f() {}\n"
3368 "};");
3369 verifyFormat("class A {\n"
3370 "public slots:\n"
3371 " void f1() {}\n"
3372 "public Q_SLOTS:\n"
3373 " void f2() {}\n"
3374 "protected slots:\n"
3375 " void f3() {}\n"
3376 "protected Q_SLOTS:\n"
3377 " void f4() {}\n"
3378 "private slots:\n"
3379 " void f5() {}\n"
3380 "private Q_SLOTS:\n"
3381 " void f6() {}\n"
3382 "signals:\n"
3383 " void g1();\n"
3384 "Q_SIGNALS:\n"
3385 " void g2();\n"
3386 "};");
3388 // Don't interpret 'signals' the wrong way.
3389 verifyFormat("signals.set();");
3390 verifyFormat("for (Signals signals : f()) {\n}");
3391 verifyFormat("{\n"
3392 " signals.set(); // This needs indentation.\n"
3393 "}");
3394 verifyFormat("void f() {\n"
3395 "label:\n"
3396 " signals.baz();\n"
3397 "}");
3398 verifyFormat("private[1];");
3399 verifyFormat("testArray[public] = 1;");
3400 verifyFormat("public();");
3401 verifyFormat("myFunc(public);");
3402 verifyFormat("std::vector<int> testVec = {private};");
3403 verifyFormat("private.p = 1;");
3404 verifyFormat("void function(private...){};");
3405 verifyFormat("if (private && public)");
3406 verifyFormat("private &= true;");
3407 verifyFormat("int x = private * public;");
3408 verifyFormat("public *= private;");
3409 verifyFormat("int x = public + private;");
3410 verifyFormat("private++;");
3411 verifyFormat("++private;");
3412 verifyFormat("public += private;");
3413 verifyFormat("public = public - private;");
3414 verifyFormat("public->foo();");
3415 verifyFormat("private--;");
3416 verifyFormat("--private;");
3417 verifyFormat("public -= 1;");
3418 verifyFormat("if (!private && !public)");
3419 verifyFormat("public != private;");
3420 verifyFormat("int x = public / private;");
3421 verifyFormat("public /= 2;");
3422 verifyFormat("public = public % 2;");
3423 verifyFormat("public %= 2;");
3424 verifyFormat("if (public < private)");
3425 verifyFormat("public << private;");
3426 verifyFormat("public <<= private;");
3427 verifyFormat("if (public > private)");
3428 verifyFormat("public >> private;");
3429 verifyFormat("public >>= private;");
3430 verifyFormat("public ^ private;");
3431 verifyFormat("public ^= private;");
3432 verifyFormat("public | private;");
3433 verifyFormat("public |= private;");
3434 verifyFormat("auto x = private ? 1 : 2;");
3435 verifyFormat("if (public == private)");
3436 verifyFormat("void foo(public, private)");
3437 verifyFormat("public::foo();");
3439 verifyFormat("class A {\n"
3440 "public:\n"
3441 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3442 "\n"
3443 "private:\n"
3444 " int c;\n"
3445 "};\n"
3446 "class B {\n"
3447 "public:\n"
3448 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3449 "\n"
3450 "private:\n"
3451 " int c;\n"
3452 "};");
3455 TEST_F(FormatTest, SeparatesLogicalBlocks) {
3456 verifyFormat("class A {\n"
3457 "public:\n"
3458 " void f();\n"
3459 "\n"
3460 "private:\n"
3461 " void g() {}\n"
3462 " // test\n"
3463 "protected:\n"
3464 " int h;\n"
3465 "};",
3466 "class A {\n"
3467 "public:\n"
3468 "void f();\n"
3469 "private:\n"
3470 "void g() {}\n"
3471 "// test\n"
3472 "protected:\n"
3473 "int h;\n"
3474 "};");
3475 verifyFormat("class A {\n"
3476 "protected:\n"
3477 "public:\n"
3478 " void f();\n"
3479 "};",
3480 "class A {\n"
3481 "protected:\n"
3482 "\n"
3483 "public:\n"
3484 "\n"
3485 " void f();\n"
3486 "};");
3488 // Even ensure proper spacing inside macros.
3489 verifyFormat("#define B \\\n"
3490 " class A { \\\n"
3491 " protected: \\\n"
3492 " public: \\\n"
3493 " void f(); \\\n"
3494 " };",
3495 "#define B \\\n"
3496 " class A { \\\n"
3497 " protected: \\\n"
3498 " \\\n"
3499 " public: \\\n"
3500 " \\\n"
3501 " void f(); \\\n"
3502 " };",
3503 getGoogleStyle());
3504 // But don't remove empty lines after macros ending in access specifiers.
3505 verifyFormat("#define A private:\n"
3506 "\n"
3507 "int i;",
3508 "#define A private:\n"
3509 "\n"
3510 "int i;");
3513 TEST_F(FormatTest, FormatsClasses) {
3514 verifyFormat("class A : public B {};");
3515 verifyFormat("class A : public ::B {};");
3517 verifyFormat(
3518 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3519 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3520 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3521 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3522 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3523 verifyFormat(
3524 "class A : public B, public C, public D, public E, public F {};");
3525 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3526 " public C,\n"
3527 " public D,\n"
3528 " public E,\n"
3529 " public F,\n"
3530 " public G {};");
3532 verifyFormat("class\n"
3533 " ReallyReallyLongClassName {\n"
3534 " int i;\n"
3535 "};",
3536 getLLVMStyleWithColumns(32));
3537 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3538 " aaaaaaaaaaaaaaaa> {};");
3539 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3540 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3541 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3542 verifyFormat("template <class R, class C>\n"
3543 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3544 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3545 verifyFormat("class ::A::B {};");
3548 TEST_F(FormatTest, BreakInheritanceStyle) {
3549 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3550 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3551 FormatStyle::BILS_BeforeComma;
3552 verifyFormat("class MyClass : public X {};",
3553 StyleWithInheritanceBreakBeforeComma);
3554 verifyFormat("class MyClass\n"
3555 " : public X\n"
3556 " , public Y {};",
3557 StyleWithInheritanceBreakBeforeComma);
3558 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3559 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3560 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3561 StyleWithInheritanceBreakBeforeComma);
3562 verifyFormat("struct aaaaaaaaaaaaa\n"
3563 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3564 " aaaaaaaaaaaaaaaa> {};",
3565 StyleWithInheritanceBreakBeforeComma);
3567 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3568 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3569 FormatStyle::BILS_AfterColon;
3570 verifyFormat("class MyClass : public X {};",
3571 StyleWithInheritanceBreakAfterColon);
3572 verifyFormat("class MyClass : public X, public Y {};",
3573 StyleWithInheritanceBreakAfterColon);
3574 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3575 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3576 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3577 StyleWithInheritanceBreakAfterColon);
3578 verifyFormat("struct aaaaaaaaaaaaa :\n"
3579 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3580 " aaaaaaaaaaaaaaaa> {};",
3581 StyleWithInheritanceBreakAfterColon);
3583 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3584 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3585 FormatStyle::BILS_AfterComma;
3586 verifyFormat("class MyClass : public X {};",
3587 StyleWithInheritanceBreakAfterComma);
3588 verifyFormat("class MyClass : public X,\n"
3589 " public Y {};",
3590 StyleWithInheritanceBreakAfterComma);
3591 verifyFormat(
3592 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3593 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3594 "{};",
3595 StyleWithInheritanceBreakAfterComma);
3596 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3597 " aaaaaaaaaaaaaaaa> {};",
3598 StyleWithInheritanceBreakAfterComma);
3599 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3600 " : public OnceBreak,\n"
3601 " public AlwaysBreak,\n"
3602 " EvenBasesFitInOneLine {};",
3603 StyleWithInheritanceBreakAfterComma);
3606 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3607 verifyFormat("class A {\n} a, b;");
3608 verifyFormat("struct A {\n} a, b;");
3609 verifyFormat("union A {\n} a, b;");
3611 verifyFormat("constexpr class A {\n} a, b;");
3612 verifyFormat("constexpr struct A {\n} a, b;");
3613 verifyFormat("constexpr union A {\n} a, b;");
3615 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3616 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3617 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3619 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3620 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3621 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3623 verifyFormat("namespace ns {\n"
3624 "class {\n"
3625 "} a, b;\n"
3626 "} // namespace ns");
3627 verifyFormat("namespace ns {\n"
3628 "const class {\n"
3629 "} a, b;\n"
3630 "} // namespace ns");
3631 verifyFormat("namespace ns {\n"
3632 "constexpr class C {\n"
3633 "} a, b;\n"
3634 "} // namespace ns");
3635 verifyFormat("namespace ns {\n"
3636 "class { /* comment */\n"
3637 "} a, b;\n"
3638 "} // namespace ns");
3639 verifyFormat("namespace ns {\n"
3640 "const class { /* comment */\n"
3641 "} a, b;\n"
3642 "} // namespace ns");
3645 TEST_F(FormatTest, FormatsEnum) {
3646 verifyFormat("enum {\n"
3647 " Zero,\n"
3648 " One = 1,\n"
3649 " Two = One + 1,\n"
3650 " Three = (One + Two),\n"
3651 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3652 " Five = (One, Two, Three, Four, 5)\n"
3653 "};");
3654 verifyGoogleFormat("enum {\n"
3655 " Zero,\n"
3656 " One = 1,\n"
3657 " Two = One + 1,\n"
3658 " Three = (One + Two),\n"
3659 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3660 " Five = (One, Two, Three, Four, 5)\n"
3661 "};");
3662 verifyFormat("enum Enum {};");
3663 verifyFormat("enum {};");
3664 verifyFormat("enum X E {} d;");
3665 verifyFormat("enum __attribute__((...)) E {} d;");
3666 verifyFormat("enum __declspec__((...)) E {} d;");
3667 verifyFormat("enum [[nodiscard]] E {} d;");
3668 verifyFormat("enum {\n"
3669 " Bar = Foo<int, int>::value\n"
3670 "};",
3671 getLLVMStyleWithColumns(30));
3673 verifyFormat("enum ShortEnum { A, B, C };");
3674 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3676 verifyFormat("enum KeepEmptyLines {\n"
3677 " ONE,\n"
3678 "\n"
3679 " TWO,\n"
3680 "\n"
3681 " THREE\n"
3682 "}",
3683 "enum KeepEmptyLines {\n"
3684 " ONE,\n"
3685 "\n"
3686 " TWO,\n"
3687 "\n"
3688 "\n"
3689 " THREE\n"
3690 "}");
3691 verifyFormat("enum E { // comment\n"
3692 " ONE,\n"
3693 " TWO\n"
3694 "};\n"
3695 "int i;");
3697 FormatStyle EightIndent = getLLVMStyle();
3698 EightIndent.IndentWidth = 8;
3699 verifyFormat("enum {\n"
3700 " VOID,\n"
3701 " CHAR,\n"
3702 " SHORT,\n"
3703 " INT,\n"
3704 " LONG,\n"
3705 " SIGNED,\n"
3706 " UNSIGNED,\n"
3707 " BOOL,\n"
3708 " FLOAT,\n"
3709 " DOUBLE,\n"
3710 " COMPLEX\n"
3711 "};",
3712 EightIndent);
3714 verifyFormat("enum [[nodiscard]] E {\n"
3715 " ONE,\n"
3716 " TWO,\n"
3717 "};");
3718 verifyFormat("enum [[nodiscard]] E {\n"
3719 " // Comment 1\n"
3720 " ONE,\n"
3721 " // Comment 2\n"
3722 " TWO,\n"
3723 "};");
3725 // Not enums.
3726 verifyFormat("enum X f() {\n"
3727 " a();\n"
3728 " return 42;\n"
3729 "}");
3730 verifyFormat("enum X Type::f() {\n"
3731 " a();\n"
3732 " return 42;\n"
3733 "}");
3734 verifyFormat("enum ::X f() {\n"
3735 " a();\n"
3736 " return 42;\n"
3737 "}");
3738 verifyFormat("enum ns::X f() {\n"
3739 " a();\n"
3740 " return 42;\n"
3741 "}");
3744 TEST_F(FormatTest, FormatsEnumsWithErrors) {
3745 verifyFormat("enum Type {\n"
3746 " One = 0; // These semicolons should be commas.\n"
3747 " Two = 1;\n"
3748 "};");
3749 verifyFormat("namespace n {\n"
3750 "enum Type {\n"
3751 " One,\n"
3752 " Two, // missing };\n"
3753 " int i;\n"
3754 "}\n"
3755 "void g() {}");
3758 TEST_F(FormatTest, FormatsEnumStruct) {
3759 verifyFormat("enum struct {\n"
3760 " Zero,\n"
3761 " One = 1,\n"
3762 " Two = One + 1,\n"
3763 " Three = (One + Two),\n"
3764 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3765 " Five = (One, Two, Three, Four, 5)\n"
3766 "};");
3767 verifyFormat("enum struct Enum {};");
3768 verifyFormat("enum struct {};");
3769 verifyFormat("enum struct X E {} d;");
3770 verifyFormat("enum struct __attribute__((...)) E {} d;");
3771 verifyFormat("enum struct __declspec__((...)) E {} d;");
3772 verifyFormat("enum struct [[nodiscard]] E {} d;");
3773 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3775 verifyFormat("enum struct [[nodiscard]] E {\n"
3776 " ONE,\n"
3777 " TWO,\n"
3778 "};");
3779 verifyFormat("enum struct [[nodiscard]] E {\n"
3780 " // Comment 1\n"
3781 " ONE,\n"
3782 " // Comment 2\n"
3783 " TWO,\n"
3784 "};");
3787 TEST_F(FormatTest, FormatsEnumClass) {
3788 verifyFormat("enum class {\n"
3789 " Zero,\n"
3790 " One = 1,\n"
3791 " Two = One + 1,\n"
3792 " Three = (One + Two),\n"
3793 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3794 " Five = (One, Two, Three, Four, 5)\n"
3795 "};");
3796 verifyFormat("enum class Enum {};");
3797 verifyFormat("enum class {};");
3798 verifyFormat("enum class X E {} d;");
3799 verifyFormat("enum class __attribute__((...)) E {} d;");
3800 verifyFormat("enum class __declspec__((...)) E {} d;");
3801 verifyFormat("enum class [[nodiscard]] E {} d;");
3802 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3804 verifyFormat("enum class [[nodiscard]] E {\n"
3805 " ONE,\n"
3806 " TWO,\n"
3807 "};");
3808 verifyFormat("enum class [[nodiscard]] E {\n"
3809 " // Comment 1\n"
3810 " ONE,\n"
3811 " // Comment 2\n"
3812 " TWO,\n"
3813 "};");
3816 TEST_F(FormatTest, FormatsEnumTypes) {
3817 verifyFormat("enum X : int {\n"
3818 " A, // Force multiple lines.\n"
3819 " B\n"
3820 "};");
3821 verifyFormat("enum X : int { A, B };");
3822 verifyFormat("enum X : std::uint32_t { A, B };");
3825 TEST_F(FormatTest, FormatsTypedefEnum) {
3826 FormatStyle Style = getLLVMStyleWithColumns(40);
3827 verifyFormat("typedef enum {} EmptyEnum;");
3828 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3829 verifyFormat("typedef enum {\n"
3830 " ZERO = 0,\n"
3831 " ONE = 1,\n"
3832 " TWO = 2,\n"
3833 " THREE = 3\n"
3834 "} LongEnum;",
3835 Style);
3836 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3837 Style.BraceWrapping.AfterEnum = true;
3838 verifyFormat("typedef enum {} EmptyEnum;");
3839 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3840 verifyFormat("typedef enum\n"
3841 "{\n"
3842 " ZERO = 0,\n"
3843 " ONE = 1,\n"
3844 " TWO = 2,\n"
3845 " THREE = 3\n"
3846 "} LongEnum;",
3847 Style);
3850 TEST_F(FormatTest, FormatsNSEnums) {
3851 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3852 verifyGoogleFormat(
3853 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3854 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3855 " // Information about someDecentlyLongValue.\n"
3856 " someDecentlyLongValue,\n"
3857 " // Information about anotherDecentlyLongValue.\n"
3858 " anotherDecentlyLongValue,\n"
3859 " // Information about aThirdDecentlyLongValue.\n"
3860 " aThirdDecentlyLongValue\n"
3861 "};");
3862 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3863 " // Information about someDecentlyLongValue.\n"
3864 " someDecentlyLongValue,\n"
3865 " // Information about anotherDecentlyLongValue.\n"
3866 " anotherDecentlyLongValue,\n"
3867 " // Information about aThirdDecentlyLongValue.\n"
3868 " aThirdDecentlyLongValue\n"
3869 "};");
3870 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3871 " a = 1,\n"
3872 " b = 2,\n"
3873 " c = 3,\n"
3874 "};");
3875 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3876 " a = 1,\n"
3877 " b = 2,\n"
3878 " c = 3,\n"
3879 "};");
3880 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3881 " a = 1,\n"
3882 " b = 2,\n"
3883 " c = 3,\n"
3884 "};");
3885 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3886 " a = 1,\n"
3887 " b = 2,\n"
3888 " c = 3,\n"
3889 "};");
3892 TEST_F(FormatTest, FormatsBitfields) {
3893 verifyFormat("struct Bitfields {\n"
3894 " unsigned sClass : 8;\n"
3895 " unsigned ValueKind : 2;\n"
3896 "};");
3897 verifyFormat("struct A {\n"
3898 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3899 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3900 "};");
3901 verifyFormat("struct MyStruct {\n"
3902 " uchar data;\n"
3903 " uchar : 8;\n"
3904 " uchar : 8;\n"
3905 " uchar other;\n"
3906 "};");
3907 FormatStyle Style = getLLVMStyle();
3908 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
3909 verifyFormat("struct Bitfields {\n"
3910 " unsigned sClass:8;\n"
3911 " unsigned ValueKind:2;\n"
3912 " uchar other;\n"
3913 "};",
3914 Style);
3915 verifyFormat("struct A {\n"
3916 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3917 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3918 "};",
3919 Style);
3920 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
3921 verifyFormat("struct Bitfields {\n"
3922 " unsigned sClass :8;\n"
3923 " unsigned ValueKind :2;\n"
3924 " uchar other;\n"
3925 "};",
3926 Style);
3927 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
3928 verifyFormat("struct Bitfields {\n"
3929 " unsigned sClass: 8;\n"
3930 " unsigned ValueKind: 2;\n"
3931 " uchar other;\n"
3932 "};",
3933 Style);
3936 TEST_F(FormatTest, FormatsNamespaces) {
3937 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
3938 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
3940 verifyFormat("namespace some_namespace {\n"
3941 "class A {};\n"
3942 "void f() { f(); }\n"
3943 "}",
3944 LLVMWithNoNamespaceFix);
3945 verifyFormat("#define M(x) x##x\n"
3946 "namespace M(x) {\n"
3947 "class A {};\n"
3948 "void f() { f(); }\n"
3949 "}",
3950 LLVMWithNoNamespaceFix);
3951 verifyFormat("#define M(x) x##x\n"
3952 "namespace N::inline M(x) {\n"
3953 "class A {};\n"
3954 "void f() { f(); }\n"
3955 "}",
3956 LLVMWithNoNamespaceFix);
3957 verifyFormat("#define M(x) x##x\n"
3958 "namespace M(x)::inline N {\n"
3959 "class A {};\n"
3960 "void f() { f(); }\n"
3961 "}",
3962 LLVMWithNoNamespaceFix);
3963 verifyFormat("#define M(x) x##x\n"
3964 "namespace N::M(x) {\n"
3965 "class A {};\n"
3966 "void f() { f(); }\n"
3967 "}",
3968 LLVMWithNoNamespaceFix);
3969 verifyFormat("#define M(x) x##x\n"
3970 "namespace M::N(x) {\n"
3971 "class A {};\n"
3972 "void f() { f(); }\n"
3973 "}",
3974 LLVMWithNoNamespaceFix);
3975 verifyFormat("namespace N::inline D {\n"
3976 "class A {};\n"
3977 "void f() { f(); }\n"
3978 "}",
3979 LLVMWithNoNamespaceFix);
3980 verifyFormat("namespace N::inline D::E {\n"
3981 "class A {};\n"
3982 "void f() { f(); }\n"
3983 "}",
3984 LLVMWithNoNamespaceFix);
3985 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3986 "class A {};\n"
3987 "void f() { f(); }\n"
3988 "}",
3989 LLVMWithNoNamespaceFix);
3990 verifyFormat("/* something */ namespace some_namespace {\n"
3991 "class A {};\n"
3992 "void f() { f(); }\n"
3993 "}",
3994 LLVMWithNoNamespaceFix);
3995 verifyFormat("namespace {\n"
3996 "class A {};\n"
3997 "void f() { f(); }\n"
3998 "}",
3999 LLVMWithNoNamespaceFix);
4000 verifyFormat("/* something */ namespace {\n"
4001 "class A {};\n"
4002 "void f() { f(); }\n"
4003 "}",
4004 LLVMWithNoNamespaceFix);
4005 verifyFormat("inline namespace X {\n"
4006 "class A {};\n"
4007 "void f() { f(); }\n"
4008 "}",
4009 LLVMWithNoNamespaceFix);
4010 verifyFormat("/* something */ inline namespace X {\n"
4011 "class A {};\n"
4012 "void f() { f(); }\n"
4013 "}",
4014 LLVMWithNoNamespaceFix);
4015 verifyFormat("export namespace X {\n"
4016 "class A {};\n"
4017 "void f() { f(); }\n"
4018 "}",
4019 LLVMWithNoNamespaceFix);
4020 verifyFormat("using namespace some_namespace;\n"
4021 "class A {};\n"
4022 "void f() { f(); }",
4023 LLVMWithNoNamespaceFix);
4025 // This code is more common than we thought; if we
4026 // layout this correctly the semicolon will go into
4027 // its own line, which is undesirable.
4028 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4029 verifyFormat("namespace {\n"
4030 "class A {};\n"
4031 "};",
4032 LLVMWithNoNamespaceFix);
4034 verifyFormat("namespace {\n"
4035 "int SomeVariable = 0; // comment\n"
4036 "} // namespace",
4037 LLVMWithNoNamespaceFix);
4038 verifyFormat("#ifndef HEADER_GUARD\n"
4039 "#define HEADER_GUARD\n"
4040 "namespace my_namespace {\n"
4041 "int i;\n"
4042 "} // my_namespace\n"
4043 "#endif // HEADER_GUARD",
4044 "#ifndef HEADER_GUARD\n"
4045 " #define HEADER_GUARD\n"
4046 " namespace my_namespace {\n"
4047 "int i;\n"
4048 "} // my_namespace\n"
4049 "#endif // HEADER_GUARD",
4050 LLVMWithNoNamespaceFix);
4052 verifyFormat("namespace A::B {\n"
4053 "class C {};\n"
4054 "}",
4055 LLVMWithNoNamespaceFix);
4057 FormatStyle Style = getLLVMStyle();
4058 Style.NamespaceIndentation = FormatStyle::NI_All;
4059 verifyFormat("namespace out {\n"
4060 " int i;\n"
4061 " namespace in {\n"
4062 " int i;\n"
4063 " } // namespace in\n"
4064 "} // namespace out",
4065 "namespace out {\n"
4066 "int i;\n"
4067 "namespace in {\n"
4068 "int i;\n"
4069 "} // namespace in\n"
4070 "} // namespace out",
4071 Style);
4073 FormatStyle ShortInlineFunctions = getLLVMStyle();
4074 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4075 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4076 FormatStyle::SFS_Inline;
4077 verifyFormat("namespace {\n"
4078 " void f() {\n"
4079 " return;\n"
4080 " }\n"
4081 "} // namespace",
4082 ShortInlineFunctions);
4083 verifyFormat("namespace { /* comment */\n"
4084 " void f() {\n"
4085 " return;\n"
4086 " }\n"
4087 "} // namespace",
4088 ShortInlineFunctions);
4089 verifyFormat("namespace { // comment\n"
4090 " void f() {\n"
4091 " return;\n"
4092 " }\n"
4093 "} // namespace",
4094 ShortInlineFunctions);
4095 verifyFormat("namespace {\n"
4096 " int some_int;\n"
4097 " void f() {\n"
4098 " return;\n"
4099 " }\n"
4100 "} // namespace",
4101 ShortInlineFunctions);
4102 verifyFormat("namespace interface {\n"
4103 " void f() {\n"
4104 " return;\n"
4105 " }\n"
4106 "} // namespace interface",
4107 ShortInlineFunctions);
4108 verifyFormat("namespace {\n"
4109 " class X {\n"
4110 " void f() { return; }\n"
4111 " };\n"
4112 "} // namespace",
4113 ShortInlineFunctions);
4114 verifyFormat("namespace {\n"
4115 " class X { /* comment */\n"
4116 " void f() { return; }\n"
4117 " };\n"
4118 "} // namespace",
4119 ShortInlineFunctions);
4120 verifyFormat("namespace {\n"
4121 " class X { // comment\n"
4122 " void f() { return; }\n"
4123 " };\n"
4124 "} // namespace",
4125 ShortInlineFunctions);
4126 verifyFormat("namespace {\n"
4127 " struct X {\n"
4128 " void f() { return; }\n"
4129 " };\n"
4130 "} // namespace",
4131 ShortInlineFunctions);
4132 verifyFormat("namespace {\n"
4133 " union X {\n"
4134 " void f() { return; }\n"
4135 " };\n"
4136 "} // namespace",
4137 ShortInlineFunctions);
4138 verifyFormat("extern \"C\" {\n"
4139 "void f() {\n"
4140 " return;\n"
4141 "}\n"
4142 "} // namespace",
4143 ShortInlineFunctions);
4144 verifyFormat("namespace {\n"
4145 " class X {\n"
4146 " void f() { return; }\n"
4147 " } x;\n"
4148 "} // namespace",
4149 ShortInlineFunctions);
4150 verifyFormat("namespace {\n"
4151 " [[nodiscard]] class X {\n"
4152 " void f() { return; }\n"
4153 " };\n"
4154 "} // namespace",
4155 ShortInlineFunctions);
4156 verifyFormat("namespace {\n"
4157 " static class X {\n"
4158 " void f() { return; }\n"
4159 " } x;\n"
4160 "} // namespace",
4161 ShortInlineFunctions);
4162 verifyFormat("namespace {\n"
4163 " constexpr class X {\n"
4164 " void f() { return; }\n"
4165 " } x;\n"
4166 "} // namespace",
4167 ShortInlineFunctions);
4169 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4170 verifyFormat("extern \"C\" {\n"
4171 " void f() {\n"
4172 " return;\n"
4173 " }\n"
4174 "} // namespace",
4175 ShortInlineFunctions);
4177 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4178 verifyFormat("namespace out {\n"
4179 "int i;\n"
4180 "namespace in {\n"
4181 " int i;\n"
4182 "} // namespace in\n"
4183 "} // namespace out",
4184 "namespace out {\n"
4185 "int i;\n"
4186 "namespace in {\n"
4187 "int i;\n"
4188 "} // namespace in\n"
4189 "} // namespace out",
4190 Style);
4192 Style.NamespaceIndentation = FormatStyle::NI_None;
4193 verifyFormat("template <class T>\n"
4194 "concept a_concept = X<>;\n"
4195 "namespace B {\n"
4196 "struct b_struct {};\n"
4197 "} // namespace B",
4198 Style);
4199 verifyFormat("template <int I>\n"
4200 "constexpr void foo()\n"
4201 " requires(I == 42)\n"
4202 "{}\n"
4203 "namespace ns {\n"
4204 "void foo() {}\n"
4205 "} // namespace ns",
4206 Style);
4208 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4209 LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4210 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4211 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4212 "// block for debug mode\n"
4213 "#ifndef NDEBUG\n"
4214 "#endif\n"
4215 "}}} // namespace ns1::ns2::ns3",
4216 LLVMWithCompactInnerNamespace);
4219 TEST_F(FormatTest, NamespaceMacros) {
4220 FormatStyle Style = getLLVMStyle();
4221 Style.NamespaceMacros.push_back("TESTSUITE");
4223 verifyFormat("TESTSUITE(A) {\n"
4224 "int foo();\n"
4225 "} // TESTSUITE(A)",
4226 Style);
4228 verifyFormat("TESTSUITE(A, B) {\n"
4229 "int foo();\n"
4230 "} // TESTSUITE(A)",
4231 Style);
4233 // Properly indent according to NamespaceIndentation style
4234 Style.NamespaceIndentation = FormatStyle::NI_All;
4235 verifyFormat("TESTSUITE(A) {\n"
4236 " int foo();\n"
4237 "} // TESTSUITE(A)",
4238 Style);
4239 verifyFormat("TESTSUITE(A) {\n"
4240 " namespace B {\n"
4241 " int foo();\n"
4242 " } // namespace B\n"
4243 "} // TESTSUITE(A)",
4244 Style);
4245 verifyFormat("namespace A {\n"
4246 " TESTSUITE(B) {\n"
4247 " int foo();\n"
4248 " } // TESTSUITE(B)\n"
4249 "} // namespace A",
4250 Style);
4252 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4253 verifyFormat("TESTSUITE(A) {\n"
4254 "TESTSUITE(B) {\n"
4255 " int foo();\n"
4256 "} // TESTSUITE(B)\n"
4257 "} // TESTSUITE(A)",
4258 Style);
4259 verifyFormat("TESTSUITE(A) {\n"
4260 "namespace B {\n"
4261 " int foo();\n"
4262 "} // namespace B\n"
4263 "} // TESTSUITE(A)",
4264 Style);
4265 verifyFormat("namespace A {\n"
4266 "TESTSUITE(B) {\n"
4267 " int foo();\n"
4268 "} // TESTSUITE(B)\n"
4269 "} // namespace A",
4270 Style);
4272 // Properly merge namespace-macros blocks in CompactNamespaces mode
4273 Style.NamespaceIndentation = FormatStyle::NI_None;
4274 Style.CompactNamespaces = true;
4275 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4276 "}} // TESTSUITE(A::B)",
4277 Style);
4279 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4280 "}} // TESTSUITE(out::in)",
4281 "TESTSUITE(out) {\n"
4282 "TESTSUITE(in) {\n"
4283 "} // TESTSUITE(in)\n"
4284 "} // TESTSUITE(out)",
4285 Style);
4287 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4288 "}} // TESTSUITE(out::in)",
4289 "TESTSUITE(out) {\n"
4290 "TESTSUITE(in) {\n"
4291 "} // TESTSUITE(in)\n"
4292 "} // TESTSUITE(out)",
4293 Style);
4295 // Do not merge different namespaces/macros
4296 verifyFormat("namespace out {\n"
4297 "TESTSUITE(in) {\n"
4298 "} // TESTSUITE(in)\n"
4299 "} // namespace out",
4300 Style);
4301 verifyFormat("TESTSUITE(out) {\n"
4302 "namespace in {\n"
4303 "} // namespace in\n"
4304 "} // TESTSUITE(out)",
4305 Style);
4306 Style.NamespaceMacros.push_back("FOOBAR");
4307 verifyFormat("TESTSUITE(out) {\n"
4308 "FOOBAR(in) {\n"
4309 "} // FOOBAR(in)\n"
4310 "} // TESTSUITE(out)",
4311 Style);
4314 TEST_F(FormatTest, FormatsCompactNamespaces) {
4315 FormatStyle Style = getLLVMStyle();
4316 Style.CompactNamespaces = true;
4317 Style.NamespaceMacros.push_back("TESTSUITE");
4319 verifyFormat("namespace A { namespace B {\n"
4320 "}} // namespace A::B",
4321 Style);
4323 verifyFormat("namespace out { namespace in {\n"
4324 "}} // namespace out::in",
4325 "namespace out {\n"
4326 "namespace in {\n"
4327 "} // namespace in\n"
4328 "} // namespace out",
4329 Style);
4331 // Only namespaces which have both consecutive opening and end get compacted
4332 verifyFormat("namespace out {\n"
4333 "namespace in1 {\n"
4334 "} // namespace in1\n"
4335 "namespace in2 {\n"
4336 "} // namespace in2\n"
4337 "} // namespace out",
4338 Style);
4340 verifyFormat("namespace out {\n"
4341 "int i;\n"
4342 "namespace in {\n"
4343 "int j;\n"
4344 "} // namespace in\n"
4345 "int k;\n"
4346 "} // namespace out",
4347 "namespace out { int i;\n"
4348 "namespace in { int j; } // namespace in\n"
4349 "int k; } // namespace out",
4350 Style);
4352 verifyFormat("namespace A { namespace B { namespace C {\n"
4353 "}}} // namespace A::B::C",
4354 "namespace A { namespace B {\n"
4355 "namespace C {\n"
4356 "}} // namespace B::C\n"
4357 "} // namespace A",
4358 Style);
4360 Style.ColumnLimit = 40;
4361 verifyFormat("namespace aaaaaaaaaa {\n"
4362 "namespace bbbbbbbbbb {\n"
4363 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4364 "namespace aaaaaaaaaa {\n"
4365 "namespace bbbbbbbbbb {\n"
4366 "} // namespace bbbbbbbbbb\n"
4367 "} // namespace aaaaaaaaaa",
4368 Style);
4370 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4371 "namespace cccccc {\n"
4372 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4373 "namespace aaaaaa {\n"
4374 "namespace bbbbbb {\n"
4375 "namespace cccccc {\n"
4376 "} // namespace cccccc\n"
4377 "} // namespace bbbbbb\n"
4378 "} // namespace aaaaaa",
4379 Style);
4380 Style.ColumnLimit = 80;
4382 // Extra semicolon after 'inner' closing brace prevents merging
4383 verifyFormat("namespace out { namespace in {\n"
4384 "}; } // namespace out::in",
4385 "namespace out {\n"
4386 "namespace in {\n"
4387 "}; // namespace in\n"
4388 "} // namespace out",
4389 Style);
4391 // Extra semicolon after 'outer' closing brace is conserved
4392 verifyFormat("namespace out { namespace in {\n"
4393 "}}; // namespace out::in",
4394 "namespace out {\n"
4395 "namespace in {\n"
4396 "} // namespace in\n"
4397 "}; // namespace out",
4398 Style);
4400 Style.NamespaceIndentation = FormatStyle::NI_All;
4401 verifyFormat("namespace out { namespace in {\n"
4402 " int i;\n"
4403 "}} // namespace out::in",
4404 "namespace out {\n"
4405 "namespace in {\n"
4406 "int i;\n"
4407 "} // namespace in\n"
4408 "} // namespace out",
4409 Style);
4410 verifyFormat("namespace out { namespace mid {\n"
4411 " namespace in {\n"
4412 " int j;\n"
4413 " } // namespace in\n"
4414 " int k;\n"
4415 "}} // namespace out::mid",
4416 "namespace out { namespace mid {\n"
4417 "namespace in { int j; } // namespace in\n"
4418 "int k; }} // namespace out::mid",
4419 Style);
4421 verifyFormat("namespace A { namespace B { namespace C {\n"
4422 " int i;\n"
4423 "}}} // namespace A::B::C\n"
4424 "int main() {\n"
4425 " if (true)\n"
4426 " return 0;\n"
4427 "}",
4428 "namespace A { namespace B {\n"
4429 "namespace C {\n"
4430 " int i;\n"
4431 "}} // namespace B::C\n"
4432 "} // namespace A\n"
4433 "int main() {\n"
4434 " if (true)\n"
4435 " return 0;\n"
4436 "}",
4437 Style);
4439 verifyFormat("namespace A { namespace B { namespace C {\n"
4440 "#ifdef FOO\n"
4441 " int i;\n"
4442 "#endif\n"
4443 "}}} // namespace A::B::C\n"
4444 "int main() {\n"
4445 " if (true)\n"
4446 " return 0;\n"
4447 "}",
4448 "namespace A { namespace B {\n"
4449 "namespace C {\n"
4450 "#ifdef FOO\n"
4451 " int i;\n"
4452 "#endif\n"
4453 "}} // namespace B::C\n"
4454 "} // namespace A\n"
4455 "int main() {\n"
4456 " if (true)\n"
4457 " return 0;\n"
4458 "}",
4459 Style);
4461 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4462 verifyFormat("namespace out { namespace in {\n"
4463 " int i;\n"
4464 "}} // namespace out::in",
4465 "namespace out {\n"
4466 "namespace in {\n"
4467 "int i;\n"
4468 "} // namespace in\n"
4469 "} // namespace out",
4470 Style);
4471 verifyFormat("namespace out { namespace mid { namespace in {\n"
4472 " int i;\n"
4473 "}}} // namespace out::mid::in",
4474 "namespace out {\n"
4475 "namespace mid {\n"
4476 "namespace in {\n"
4477 "int i;\n"
4478 "} // namespace in\n"
4479 "} // namespace mid\n"
4480 "} // namespace out",
4481 Style);
4483 Style.CompactNamespaces = true;
4484 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4485 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4486 Style.BraceWrapping.BeforeLambdaBody = true;
4487 verifyFormat("namespace out { namespace in {\n"
4488 "}} // namespace out::in",
4489 Style);
4490 verifyFormat("namespace out { namespace in {\n"
4491 "}} // namespace out::in",
4492 "namespace out {\n"
4493 "namespace in {\n"
4494 "} // namespace in\n"
4495 "} // namespace out",
4496 Style);
4499 TEST_F(FormatTest, FormatsExternC) {
4500 verifyFormat("extern \"C\" {\nint a;");
4501 verifyFormat("extern \"C\" {}");
4502 verifyFormat("extern \"C\" {\n"
4503 "int foo();\n"
4504 "}");
4505 verifyFormat("extern \"C\" int foo() {}");
4506 verifyFormat("extern \"C\" int foo();");
4507 verifyFormat("extern \"C\" int foo() {\n"
4508 " int i = 42;\n"
4509 " return i;\n"
4510 "}");
4512 FormatStyle Style = getLLVMStyle();
4513 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4514 Style.BraceWrapping.AfterFunction = true;
4515 verifyFormat("extern \"C\" int foo() {}", Style);
4516 verifyFormat("extern \"C\" int foo();", Style);
4517 verifyFormat("extern \"C\" int foo()\n"
4518 "{\n"
4519 " int i = 42;\n"
4520 " return i;\n"
4521 "}",
4522 Style);
4524 Style.BraceWrapping.AfterExternBlock = true;
4525 Style.BraceWrapping.SplitEmptyRecord = false;
4526 verifyFormat("extern \"C\"\n"
4527 "{}",
4528 Style);
4529 verifyFormat("extern \"C\"\n"
4530 "{\n"
4531 " int foo();\n"
4532 "}",
4533 Style);
4536 TEST_F(FormatTest, IndentExternBlockStyle) {
4537 FormatStyle Style = getLLVMStyle();
4538 Style.IndentWidth = 2;
4540 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4541 verifyFormat("extern \"C\" { /*9*/\n"
4542 "}",
4543 Style);
4544 verifyFormat("extern \"C\" {\n"
4545 " int foo10();\n"
4546 "}",
4547 Style);
4549 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4550 verifyFormat("extern \"C\" { /*11*/\n"
4551 "}",
4552 Style);
4553 verifyFormat("extern \"C\" {\n"
4554 "int foo12();\n"
4555 "}",
4556 Style);
4558 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4559 Style.BraceWrapping.AfterExternBlock = true;
4560 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4561 verifyFormat("extern \"C\"\n"
4562 "{ /*13*/\n"
4563 "}",
4564 Style);
4565 verifyFormat("extern \"C\"\n{\n"
4566 " int foo14();\n"
4567 "}",
4568 Style);
4570 Style.BraceWrapping.AfterExternBlock = false;
4571 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4572 verifyFormat("extern \"C\" { /*15*/\n"
4573 "}",
4574 Style);
4575 verifyFormat("extern \"C\" {\n"
4576 "int foo16();\n"
4577 "}",
4578 Style);
4580 Style.BraceWrapping.AfterExternBlock = true;
4581 verifyFormat("extern \"C\"\n"
4582 "{ /*13*/\n"
4583 "}",
4584 Style);
4585 verifyFormat("extern \"C\"\n"
4586 "{\n"
4587 "int foo14();\n"
4588 "}",
4589 Style);
4591 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4592 verifyFormat("extern \"C\"\n"
4593 "{ /*13*/\n"
4594 "}",
4595 Style);
4596 verifyFormat("extern \"C\"\n"
4597 "{\n"
4598 " int foo14();\n"
4599 "}",
4600 Style);
4603 TEST_F(FormatTest, FormatsInlineASM) {
4604 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4605 verifyFormat("asm(\"nop\" ::: \"memory\");");
4606 verifyFormat(
4607 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4608 " \"cpuid\\n\\t\"\n"
4609 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4610 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4611 " : \"a\"(value));");
4612 verifyFormat(
4613 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4614 " __asm {\n"
4615 " mov edx,[that] // vtable in edx\n"
4616 " mov eax,methodIndex\n"
4617 " call [edx][eax*4] // stdcall\n"
4618 " }\n"
4619 "}",
4620 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4621 " __asm {\n"
4622 " mov edx,[that] // vtable in edx\n"
4623 " mov eax,methodIndex\n"
4624 " call [edx][eax*4] // stdcall\n"
4625 " }\n"
4626 "}");
4627 verifyNoChange("_asm {\n"
4628 " xor eax, eax;\n"
4629 " cpuid;\n"
4630 "}");
4631 verifyFormat("void function() {\n"
4632 " // comment\n"
4633 " asm(\"\");\n"
4634 "}");
4635 verifyFormat("__asm {\n"
4636 "}\n"
4637 "int i;",
4638 "__asm {\n"
4639 "}\n"
4640 "int i;");
4642 auto Style = getLLVMStyleWithColumns(0);
4643 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4644 const StringRef Code2{"asm(\"xyz\"\n"
4645 " : \"=a\"(a), \"=d\"(b)\n"
4646 " : \"a\"(data));"};
4647 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4648 " : \"a\"(data));"};
4650 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4651 verifyFormat(Code1, Style);
4652 verifyNoChange(Code2, Style);
4653 verifyNoChange(Code3, Style);
4655 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4656 verifyFormat(Code2, Code1, Style);
4657 verifyNoChange(Code2, Style);
4658 verifyFormat(Code2, Code3, Style);
4661 TEST_F(FormatTest, FormatTryCatch) {
4662 verifyFormat("try {\n"
4663 " throw a * b;\n"
4664 "} catch (int a) {\n"
4665 " // Do nothing.\n"
4666 "} catch (...) {\n"
4667 " exit(42);\n"
4668 "}");
4670 // Function-level try statements.
4671 verifyFormat("int f() try { return 4; } catch (...) {\n"
4672 " return 5;\n"
4673 "}");
4674 verifyFormat("class A {\n"
4675 " int a;\n"
4676 " A() try : a(0) {\n"
4677 " } catch (...) {\n"
4678 " throw;\n"
4679 " }\n"
4680 "};");
4681 verifyFormat("class A {\n"
4682 " int a;\n"
4683 " A() try : a(0), b{1} {\n"
4684 " } catch (...) {\n"
4685 " throw;\n"
4686 " }\n"
4687 "};");
4688 verifyFormat("class A {\n"
4689 " int a;\n"
4690 " A() try : a(0), b{1}, c{2} {\n"
4691 " } catch (...) {\n"
4692 " throw;\n"
4693 " }\n"
4694 "};");
4695 verifyFormat("class A {\n"
4696 " int a;\n"
4697 " A() try : a(0), b{1}, c{2} {\n"
4698 " { // New scope.\n"
4699 " }\n"
4700 " } catch (...) {\n"
4701 " throw;\n"
4702 " }\n"
4703 "};");
4705 // Incomplete try-catch blocks.
4706 verifyIncompleteFormat("try {} catch (");
4709 TEST_F(FormatTest, FormatTryAsAVariable) {
4710 verifyFormat("int try;");
4711 verifyFormat("int try, size;");
4712 verifyFormat("try = foo();");
4713 verifyFormat("if (try < size) {\n return true;\n}");
4715 verifyFormat("int catch;");
4716 verifyFormat("int catch, size;");
4717 verifyFormat("catch = foo();");
4718 verifyFormat("if (catch < size) {\n return true;\n}");
4720 FormatStyle Style = getLLVMStyle();
4721 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4722 Style.BraceWrapping.AfterFunction = true;
4723 Style.BraceWrapping.BeforeCatch = true;
4724 verifyFormat("try {\n"
4725 " int bar = 1;\n"
4726 "}\n"
4727 "catch (...) {\n"
4728 " int bar = 1;\n"
4729 "}",
4730 Style);
4731 verifyFormat("#if NO_EX\n"
4732 "try\n"
4733 "#endif\n"
4734 "{\n"
4735 "}\n"
4736 "#if NO_EX\n"
4737 "catch (...) {\n"
4738 "}",
4739 Style);
4740 verifyFormat("try /* abc */ {\n"
4741 " int bar = 1;\n"
4742 "}\n"
4743 "catch (...) {\n"
4744 " int bar = 1;\n"
4745 "}",
4746 Style);
4747 verifyFormat("try\n"
4748 "// abc\n"
4749 "{\n"
4750 " int bar = 1;\n"
4751 "}\n"
4752 "catch (...) {\n"
4753 " int bar = 1;\n"
4754 "}",
4755 Style);
4758 TEST_F(FormatTest, FormatSEHTryCatch) {
4759 verifyFormat("__try {\n"
4760 " int a = b * c;\n"
4761 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4762 " // Do nothing.\n"
4763 "}");
4765 verifyFormat("__try {\n"
4766 " int a = b * c;\n"
4767 "} __finally {\n"
4768 " // Do nothing.\n"
4769 "}");
4771 verifyFormat("DEBUG({\n"
4772 " __try {\n"
4773 " } __finally {\n"
4774 " }\n"
4775 "});");
4778 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4779 verifyFormat("try {\n"
4780 " f();\n"
4781 "} catch {\n"
4782 " g();\n"
4783 "}");
4784 verifyFormat("try {\n"
4785 " f();\n"
4786 "} catch (A a) MACRO(x) {\n"
4787 " g();\n"
4788 "} catch (B b) MACRO(x) {\n"
4789 " g();\n"
4790 "}");
4793 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4794 FormatStyle Style = getLLVMStyle();
4795 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4796 FormatStyle::BS_WebKit}) {
4797 Style.BreakBeforeBraces = BraceStyle;
4798 verifyFormat("try {\n"
4799 " // something\n"
4800 "} catch (...) {\n"
4801 " // something\n"
4802 "}",
4803 Style);
4805 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4806 verifyFormat("try {\n"
4807 " // something\n"
4808 "}\n"
4809 "catch (...) {\n"
4810 " // something\n"
4811 "}",
4812 Style);
4813 verifyFormat("__try {\n"
4814 " // something\n"
4815 "}\n"
4816 "__finally {\n"
4817 " // something\n"
4818 "}",
4819 Style);
4820 verifyFormat("@try {\n"
4821 " // something\n"
4822 "}\n"
4823 "@finally {\n"
4824 " // something\n"
4825 "}",
4826 Style);
4827 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4828 verifyFormat("try\n"
4829 "{\n"
4830 " // something\n"
4831 "}\n"
4832 "catch (...)\n"
4833 "{\n"
4834 " // something\n"
4835 "}",
4836 Style);
4837 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4838 verifyFormat("try\n"
4839 " {\n"
4840 " // something white\n"
4841 " }\n"
4842 "catch (...)\n"
4843 " {\n"
4844 " // something white\n"
4845 " }",
4846 Style);
4847 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4848 verifyFormat("try\n"
4849 " {\n"
4850 " // something\n"
4851 " }\n"
4852 "catch (...)\n"
4853 " {\n"
4854 " // something\n"
4855 " }",
4856 Style);
4857 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4858 Style.BraceWrapping.BeforeCatch = true;
4859 verifyFormat("try {\n"
4860 " // something\n"
4861 "}\n"
4862 "catch (...) {\n"
4863 " // something\n"
4864 "}",
4865 Style);
4868 TEST_F(FormatTest, StaticInitializers) {
4869 verifyFormat("static SomeClass SC = {1, 'a'};");
4871 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4872 " 100000000, "
4873 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4875 // Here, everything other than the "}" would fit on a line.
4876 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4877 " 10000000000000000000000000};");
4878 verifyFormat("S s = {a,\n"
4879 "\n"
4880 " b};",
4881 "S s = {\n"
4882 " a,\n"
4883 "\n"
4884 " b\n"
4885 "};");
4887 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4888 // line. However, the formatting looks a bit off and this probably doesn't
4889 // happen often in practice.
4890 verifyFormat("static int Variable[1] = {\n"
4891 " {1000000000000000000000000000000000000}};",
4892 getLLVMStyleWithColumns(40));
4895 TEST_F(FormatTest, DesignatedInitializers) {
4896 verifyFormat("const struct A a = {.a = 1, .b = 2};");
4897 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4898 " .bbbbbbbbbb = 2,\n"
4899 " .cccccccccc = 3,\n"
4900 " .dddddddddd = 4,\n"
4901 " .eeeeeeeeee = 5};");
4902 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4903 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4904 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4905 " .ccccccccccccccccccccccccccc = 3,\n"
4906 " .ddddddddddddddddddddddddddd = 4,\n"
4907 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4909 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4911 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4912 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4913 " [2] = bbbbbbbbbb,\n"
4914 " [3] = cccccccccc,\n"
4915 " [4] = dddddddddd,\n"
4916 " [5] = eeeeeeeeee};");
4917 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4918 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4919 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4920 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4921 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4922 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4925 TEST_F(FormatTest, BracedInitializerIndentWidth) {
4926 auto Style = getLLVMStyleWithColumns(60);
4927 Style.BinPackArguments = true;
4928 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4929 Style.BracedInitializerIndentWidth = 6;
4931 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
4932 verifyFormat("enum class {\n"
4933 " One,\n"
4934 " Two,\n"
4935 "};",
4936 Style);
4937 verifyFormat("class Foo {\n"
4938 " Foo() {}\n"
4939 " void bar();\n"
4940 "};",
4941 Style);
4942 verifyFormat("void foo() {\n"
4943 " auto bar = baz;\n"
4944 " return baz;\n"
4945 "};",
4946 Style);
4947 verifyFormat("auto foo = [&] {\n"
4948 " auto bar = baz;\n"
4949 " return baz;\n"
4950 "};",
4951 Style);
4952 verifyFormat("{\n"
4953 " auto bar = baz;\n"
4954 " return baz;\n"
4955 "};",
4956 Style);
4957 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
4958 verifyFormat("SomeClass clazz(\n"
4959 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
4960 " \"zzzzzzzzzzzzzzzzzz\");",
4961 Style);
4963 // The following types of initialization are all affected by
4964 // BracedInitializerIndentWidth. Aggregate initialization.
4965 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
4966 " 10000000, 20000000};",
4967 Style);
4968 verifyFormat("SomeStruct s{\n"
4969 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
4970 " \"zzzzzzzzzzzzzzzz\"};",
4971 Style);
4972 // Designated initializers.
4973 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
4974 " [0] = 10000000, [1] = 20000000};",
4975 Style);
4976 verifyFormat("SomeStruct s{\n"
4977 " .foo = \"xxxxxxxxxxxxx\",\n"
4978 " .bar = \"yyyyyyyyyyyyy\",\n"
4979 " .baz = \"zzzzzzzzzzzzz\"};",
4980 Style);
4981 // List initialization.
4982 verifyFormat("SomeStruct s{\n"
4983 " \"xxxxxxxxxxxxx\",\n"
4984 " \"yyyyyyyyyyyyy\",\n"
4985 " \"zzzzzzzzzzzzz\",\n"
4986 "};",
4987 Style);
4988 verifyFormat("SomeStruct{\n"
4989 " \"xxxxxxxxxxxxx\",\n"
4990 " \"yyyyyyyyyyyyy\",\n"
4991 " \"zzzzzzzzzzzzz\",\n"
4992 "};",
4993 Style);
4994 verifyFormat("new SomeStruct{\n"
4995 " \"xxxxxxxxxxxxx\",\n"
4996 " \"yyyyyyyyyyyyy\",\n"
4997 " \"zzzzzzzzzzzzz\",\n"
4998 "};",
4999 Style);
5000 // Member initializer.
5001 verifyFormat("class SomeClass {\n"
5002 " SomeStruct s{\n"
5003 " \"xxxxxxxxxxxxx\",\n"
5004 " \"yyyyyyyyyyyyy\",\n"
5005 " \"zzzzzzzzzzzzz\",\n"
5006 " };\n"
5007 "};",
5008 Style);
5009 // Constructor member initializer.
5010 verifyFormat("SomeClass::SomeClass : strct{\n"
5011 " \"xxxxxxxxxxxxx\",\n"
5012 " \"yyyyyyyyyyyyy\",\n"
5013 " \"zzzzzzzzzzzzz\",\n"
5014 " } {}",
5015 Style);
5016 // Copy initialization.
5017 verifyFormat("SomeStruct s = SomeStruct{\n"
5018 " \"xxxxxxxxxxxxx\",\n"
5019 " \"yyyyyyyyyyyyy\",\n"
5020 " \"zzzzzzzzzzzzz\",\n"
5021 "};",
5022 Style);
5023 // Copy list initialization.
5024 verifyFormat("SomeStruct s = {\n"
5025 " \"xxxxxxxxxxxxx\",\n"
5026 " \"yyyyyyyyyyyyy\",\n"
5027 " \"zzzzzzzzzzzzz\",\n"
5028 "};",
5029 Style);
5030 // Assignment operand initialization.
5031 verifyFormat("s = {\n"
5032 " \"xxxxxxxxxxxxx\",\n"
5033 " \"yyyyyyyyyyyyy\",\n"
5034 " \"zzzzzzzzzzzzz\",\n"
5035 "};",
5036 Style);
5037 // Returned object initialization.
5038 verifyFormat("return {\n"
5039 " \"xxxxxxxxxxxxx\",\n"
5040 " \"yyyyyyyyyyyyy\",\n"
5041 " \"zzzzzzzzzzzzz\",\n"
5042 "};",
5043 Style);
5044 // Initializer list.
5045 verifyFormat("auto initializerList = {\n"
5046 " \"xxxxxxxxxxxxx\",\n"
5047 " \"yyyyyyyyyyyyy\",\n"
5048 " \"zzzzzzzzzzzzz\",\n"
5049 "};",
5050 Style);
5051 // Function parameter initialization.
5052 verifyFormat("func({\n"
5053 " \"xxxxxxxxxxxxx\",\n"
5054 " \"yyyyyyyyyyyyy\",\n"
5055 " \"zzzzzzzzzzzzz\",\n"
5056 "});",
5057 Style);
5058 // Nested init lists.
5059 verifyFormat("SomeStruct s = {\n"
5060 " {{init1, init2, init3, init4, init5},\n"
5061 " {init1, init2, init3, init4, init5}}};",
5062 Style);
5063 verifyFormat("SomeStruct s = {\n"
5064 " {{\n"
5065 " .init1 = 1,\n"
5066 " .init2 = 2,\n"
5067 " .init3 = 3,\n"
5068 " .init4 = 4,\n"
5069 " .init5 = 5,\n"
5070 " },\n"
5071 " {init1, init2, init3, init4, init5}}};",
5072 Style);
5073 verifyFormat("SomeArrayT a[3] = {\n"
5074 " {\n"
5075 " foo,\n"
5076 " bar,\n"
5077 " },\n"
5078 " {\n"
5079 " foo,\n"
5080 " bar,\n"
5081 " },\n"
5082 " SomeArrayT{},\n"
5083 "};",
5084 Style);
5085 verifyFormat("SomeArrayT a[3] = {\n"
5086 " {foo},\n"
5087 " {\n"
5088 " {\n"
5089 " init1,\n"
5090 " init2,\n"
5091 " init3,\n"
5092 " },\n"
5093 " {\n"
5094 " init1,\n"
5095 " init2,\n"
5096 " init3,\n"
5097 " },\n"
5098 " },\n"
5099 " {baz},\n"
5100 "};",
5101 Style);
5103 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5104 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5105 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5106 " \"zzzzzzzzzzzzz\"};",
5107 Style);
5110 TEST_F(FormatTest, NestedStaticInitializers) {
5111 verifyFormat("static A x = {{{}}};");
5112 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5113 " {init1, init2, init3, init4}}};",
5114 getLLVMStyleWithColumns(50));
5116 verifyFormat("somes Status::global_reps[3] = {\n"
5117 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5118 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5119 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5120 getLLVMStyleWithColumns(60));
5121 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5122 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5123 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5124 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5125 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5126 " {rect.fRight - rect.fLeft, rect.fBottom - "
5127 "rect.fTop}};");
5129 verifyFormat(
5130 "SomeArrayOfSomeType a = {\n"
5131 " {{1, 2, 3},\n"
5132 " {1, 2, 3},\n"
5133 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5134 " 333333333333333333333333333333},\n"
5135 " {1, 2, 3},\n"
5136 " {1, 2, 3}}};");
5137 verifyFormat(
5138 "SomeArrayOfSomeType a = {\n"
5139 " {{1, 2, 3}},\n"
5140 " {{1, 2, 3}},\n"
5141 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5142 " 333333333333333333333333333333}},\n"
5143 " {{1, 2, 3}},\n"
5144 " {{1, 2, 3}}};");
5146 verifyFormat("struct {\n"
5147 " unsigned bit;\n"
5148 " const char *const name;\n"
5149 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5150 " {kOsWin, \"Windows\"},\n"
5151 " {kOsLinux, \"Linux\"},\n"
5152 " {kOsCrOS, \"Chrome OS\"}};");
5153 verifyFormat("struct {\n"
5154 " unsigned bit;\n"
5155 " const char *const name;\n"
5156 "} kBitsToOs[] = {\n"
5157 " {kOsMac, \"Mac\"},\n"
5158 " {kOsWin, \"Windows\"},\n"
5159 " {kOsLinux, \"Linux\"},\n"
5160 " {kOsCrOS, \"Chrome OS\"},\n"
5161 "};");
5164 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5165 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5166 " \\\n"
5167 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5170 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5171 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5172 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5174 // Do break defaulted and deleted functions.
5175 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5176 " default;",
5177 getLLVMStyleWithColumns(40));
5178 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5179 " delete;",
5180 getLLVMStyleWithColumns(40));
5183 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5184 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5185 getLLVMStyleWithColumns(40));
5186 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5187 getLLVMStyleWithColumns(40));
5188 verifyFormat("#define Q \\\n"
5189 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5190 " \"aaaaaaaa.cpp\"",
5191 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5192 getLLVMStyleWithColumns(40));
5195 TEST_F(FormatTest, UnderstandsLinePPDirective) {
5196 verifyFormat("# 123 \"A string literal\"",
5197 " # 123 \"A string literal\"");
5200 TEST_F(FormatTest, LayoutUnknownPPDirective) {
5201 verifyFormat("#;");
5202 verifyFormat("#\n;\n;\n;");
5205 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5206 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5207 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5208 getLLVMStyleWithColumns(12));
5211 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5212 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5213 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5216 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5217 verifyFormat("#define A \\x20");
5218 verifyFormat("#define A \\ x20");
5219 verifyFormat("#define A \\ x20", "#define A \\ x20");
5220 verifyFormat("#define A ''");
5221 verifyFormat("#define A ''qqq");
5222 verifyFormat("#define A `qqq");
5223 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5224 verifyFormat("const char *c = STRINGIFY(\n"
5225 "\\na : b);",
5226 "const char * c = STRINGIFY(\n"
5227 "\\na : b);");
5229 verifyFormat("a\r\\");
5230 verifyFormat("a\v\\");
5231 verifyFormat("a\f\\");
5234 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5235 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp);
5236 style.IndentWidth = 4;
5237 style.PPIndentWidth = 1;
5239 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5240 verifyFormat("#ifdef __linux__\n"
5241 "void foo() {\n"
5242 " int x = 0;\n"
5243 "}\n"
5244 "#define FOO\n"
5245 "#endif\n"
5246 "void bar() {\n"
5247 " int y = 0;\n"
5248 "}",
5249 style);
5251 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5252 verifyFormat("#ifdef __linux__\n"
5253 "void foo() {\n"
5254 " int x = 0;\n"
5255 "}\n"
5256 "# define FOO foo\n"
5257 "#endif\n"
5258 "void bar() {\n"
5259 " int y = 0;\n"
5260 "}",
5261 style);
5263 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5264 verifyFormat("#ifdef __linux__\n"
5265 "void foo() {\n"
5266 " int x = 0;\n"
5267 "}\n"
5268 " #define FOO foo\n"
5269 "#endif\n"
5270 "void bar() {\n"
5271 " int y = 0;\n"
5272 "}",
5273 style);
5274 verifyFormat("#if 1\n"
5275 " // some comments\n"
5276 " // another\n"
5277 " #define foo 1\n"
5278 "// not a define comment\n"
5279 "void bar() {\n"
5280 " // comment\n"
5281 " int y = 0;\n"
5282 "}",
5283 "#if 1\n"
5284 "// some comments\n"
5285 "// another\n"
5286 "#define foo 1\n"
5287 "// not a define comment\n"
5288 "void bar() {\n"
5289 " // comment\n"
5290 " int y = 0;\n"
5291 "}",
5292 style);
5294 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5295 verifyFormat("#ifdef foo\n"
5296 "#define bar() \\\n"
5297 " if (A) { \\\n"
5298 " B(); \\\n"
5299 " } \\\n"
5300 " C();\n"
5301 "#endif",
5302 style);
5303 verifyFormat("if (emacs) {\n"
5304 "#ifdef is\n"
5305 "#define lit \\\n"
5306 " if (af) { \\\n"
5307 " return duh(); \\\n"
5308 " }\n"
5309 "#endif\n"
5310 "}",
5311 style);
5312 verifyFormat("#if abc\n"
5313 "#ifdef foo\n"
5314 "#define bar() \\\n"
5315 " if (A) { \\\n"
5316 " if (B) { \\\n"
5317 " C(); \\\n"
5318 " } \\\n"
5319 " } \\\n"
5320 " D();\n"
5321 "#endif\n"
5322 "#endif",
5323 style);
5324 verifyFormat("#ifndef foo\n"
5325 "#define foo\n"
5326 "if (emacs) {\n"
5327 "#ifdef is\n"
5328 "#define lit \\\n"
5329 " if (af) { \\\n"
5330 " return duh(); \\\n"
5331 " }\n"
5332 "#endif\n"
5333 "}\n"
5334 "#endif",
5335 style);
5336 verifyFormat("#if 1\n"
5337 "#define X \\\n"
5338 " { \\\n"
5339 " x; \\\n"
5340 " x; \\\n"
5341 " }\n"
5342 "#endif",
5343 style);
5344 verifyFormat("#define X \\\n"
5345 " { \\\n"
5346 " x; \\\n"
5347 " x; \\\n"
5348 " }",
5349 style);
5351 style.PPIndentWidth = 2;
5352 verifyFormat("#ifdef foo\n"
5353 "#define bar() \\\n"
5354 " if (A) { \\\n"
5355 " B(); \\\n"
5356 " } \\\n"
5357 " C();\n"
5358 "#endif",
5359 style);
5360 style.IndentWidth = 8;
5361 verifyFormat("#ifdef foo\n"
5362 "#define bar() \\\n"
5363 " if (A) { \\\n"
5364 " B(); \\\n"
5365 " } \\\n"
5366 " C();\n"
5367 "#endif",
5368 style);
5370 style.IndentWidth = 1;
5371 style.PPIndentWidth = 4;
5372 verifyFormat("#if 1\n"
5373 "#define X \\\n"
5374 " { \\\n"
5375 " x; \\\n"
5376 " x; \\\n"
5377 " }\n"
5378 "#endif",
5379 style);
5380 verifyFormat("#define X \\\n"
5381 " { \\\n"
5382 " x; \\\n"
5383 " x; \\\n"
5384 " }",
5385 style);
5387 style.IndentWidth = 4;
5388 style.PPIndentWidth = 1;
5389 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5390 verifyFormat("#ifdef foo\n"
5391 "# define bar() \\\n"
5392 " if (A) { \\\n"
5393 " B(); \\\n"
5394 " } \\\n"
5395 " C();\n"
5396 "#endif",
5397 style);
5398 verifyFormat("#if abc\n"
5399 "# ifdef foo\n"
5400 "# define bar() \\\n"
5401 " if (A) { \\\n"
5402 " if (B) { \\\n"
5403 " C(); \\\n"
5404 " } \\\n"
5405 " } \\\n"
5406 " D();\n"
5407 "# endif\n"
5408 "#endif",
5409 style);
5410 verifyFormat("#ifndef foo\n"
5411 "#define foo\n"
5412 "if (emacs) {\n"
5413 "#ifdef is\n"
5414 "# define lit \\\n"
5415 " if (af) { \\\n"
5416 " return duh(); \\\n"
5417 " }\n"
5418 "#endif\n"
5419 "}\n"
5420 "#endif",
5421 style);
5422 verifyFormat("#define X \\\n"
5423 " { \\\n"
5424 " x; \\\n"
5425 " x; \\\n"
5426 " }",
5427 style);
5429 style.PPIndentWidth = 2;
5430 style.IndentWidth = 8;
5431 verifyFormat("#ifdef foo\n"
5432 "# define bar() \\\n"
5433 " if (A) { \\\n"
5434 " B(); \\\n"
5435 " } \\\n"
5436 " C();\n"
5437 "#endif",
5438 style);
5440 style.PPIndentWidth = 4;
5441 style.IndentWidth = 1;
5442 verifyFormat("#define X \\\n"
5443 " { \\\n"
5444 " x; \\\n"
5445 " x; \\\n"
5446 " }",
5447 style);
5449 style.IndentWidth = 4;
5450 style.PPIndentWidth = 1;
5451 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5452 verifyFormat("if (emacs) {\n"
5453 "#ifdef is\n"
5454 " #define lit \\\n"
5455 " if (af) { \\\n"
5456 " return duh(); \\\n"
5457 " }\n"
5458 "#endif\n"
5459 "}",
5460 style);
5461 verifyFormat("#if abc\n"
5462 " #ifdef foo\n"
5463 " #define bar() \\\n"
5464 " if (A) { \\\n"
5465 " B(); \\\n"
5466 " } \\\n"
5467 " C();\n"
5468 " #endif\n"
5469 "#endif",
5470 style);
5471 verifyFormat("#if 1\n"
5472 " #define X \\\n"
5473 " { \\\n"
5474 " x; \\\n"
5475 " x; \\\n"
5476 " }\n"
5477 "#endif",
5478 style);
5480 style.PPIndentWidth = 2;
5481 verifyFormat("#ifdef foo\n"
5482 " #define bar() \\\n"
5483 " if (A) { \\\n"
5484 " B(); \\\n"
5485 " } \\\n"
5486 " C();\n"
5487 "#endif",
5488 style);
5490 style.PPIndentWidth = 4;
5491 style.IndentWidth = 1;
5492 verifyFormat("#if 1\n"
5493 " #define X \\\n"
5494 " { \\\n"
5495 " x; \\\n"
5496 " x; \\\n"
5497 " }\n"
5498 "#endif",
5499 style);
5502 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5503 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5504 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5505 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5506 // FIXME: We never break before the macro name.
5507 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5509 verifyFormat("#define A A\n#define A A");
5510 verifyFormat("#define A(X) A\n#define A A");
5512 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5513 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5516 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5517 verifyFormat("// somecomment\n"
5518 "#include \"a.h\"\n"
5519 "#define A( \\\n"
5520 " A, B)\n"
5521 "#include \"b.h\"\n"
5522 "// somecomment",
5523 " // somecomment\n"
5524 " #include \"a.h\"\n"
5525 "#define A(A,\\\n"
5526 " B)\n"
5527 " #include \"b.h\"\n"
5528 " // somecomment",
5529 getLLVMStyleWithColumns(13));
5532 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5534 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5535 verifyFormat("#define A \\\n"
5536 " c; \\\n"
5537 " e;\n"
5538 "f;",
5539 "#define A c; e;\n"
5540 "f;",
5541 getLLVMStyleWithColumns(14));
5544 TEST_F(FormatTest, LayoutRemainingTokens) { verifyFormat("{}"); }
5546 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5547 verifyFormat("int x,\n"
5548 "#define A\n"
5549 " y;",
5550 "int x,\n#define A\ny;");
5553 TEST_F(FormatTest, HashInMacroDefinition) {
5554 verifyFormat("#define A(c) L#c");
5555 verifyFormat("#define A(c) u#c");
5556 verifyFormat("#define A(c) U#c");
5557 verifyFormat("#define A(c) u8#c");
5558 verifyFormat("#define A(c) LR#c");
5559 verifyFormat("#define A(c) uR#c");
5560 verifyFormat("#define A(c) UR#c");
5561 verifyFormat("#define A(c) u8R#c");
5562 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5563 verifyFormat("#define A \\\n"
5564 " { \\\n"
5565 " f(#c); \\\n"
5566 " }",
5567 getLLVMStyleWithColumns(11));
5569 verifyFormat("#define A(X) \\\n"
5570 " void function##X()",
5571 getLLVMStyleWithColumns(22));
5573 verifyFormat("#define A(a, b, c) \\\n"
5574 " void a##b##c()",
5575 getLLVMStyleWithColumns(22));
5577 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5580 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5581 verifyFormat("#define A (x)");
5582 verifyFormat("#define A(x)");
5584 FormatStyle Style = getLLVMStyle();
5585 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5586 verifyFormat("#define true ((foo)1)", Style);
5587 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5588 verifyFormat("#define false((foo)0)", Style);
5591 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5592 verifyFormat("#define A b;",
5593 "#define A \\\n"
5594 " \\\n"
5595 " b;",
5596 getLLVMStyleWithColumns(25));
5597 verifyNoChange("#define A \\\n"
5598 " \\\n"
5599 " a; \\\n"
5600 " b;",
5601 getLLVMStyleWithColumns(11));
5602 verifyNoChange("#define A \\\n"
5603 " a; \\\n"
5604 " \\\n"
5605 " b;",
5606 getLLVMStyleWithColumns(11));
5609 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5610 verifyIncompleteFormat("#define A :");
5611 verifyFormat("#define SOMECASES \\\n"
5612 " case 1: \\\n"
5613 " case 2",
5614 getLLVMStyleWithColumns(20));
5615 verifyFormat("#define MACRO(a) \\\n"
5616 " if (a) \\\n"
5617 " f(); \\\n"
5618 " else \\\n"
5619 " g()",
5620 getLLVMStyleWithColumns(18));
5621 verifyFormat("#define A template <typename T>");
5622 verifyIncompleteFormat("#define STR(x) #x\n"
5623 "f(STR(this_is_a_string_literal{));");
5624 verifyFormat("#pragma omp threadprivate( \\\n"
5625 " y)), // expected-warning",
5626 getLLVMStyleWithColumns(28));
5627 verifyFormat("#d, = };");
5628 verifyFormat("#if \"a");
5629 verifyIncompleteFormat("({\n"
5630 "#define b \\\n"
5631 " } \\\n"
5632 " a\n"
5633 "a",
5634 getLLVMStyleWithColumns(15));
5635 verifyFormat("#define A \\\n"
5636 " { \\\n"
5637 " {\n"
5638 "#define B \\\n"
5639 " } \\\n"
5640 " }",
5641 getLLVMStyleWithColumns(15));
5642 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5643 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5644 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5645 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5646 verifyNoCrash("#else\n"
5647 "#else\n"
5648 "#endif\n"
5649 "#endif");
5650 verifyNoCrash("#else\n"
5651 "#if X\n"
5652 "#endif\n"
5653 "#endif");
5654 verifyNoCrash("#else\n"
5655 "#endif\n"
5656 "#if X\n"
5657 "#endif");
5658 verifyNoCrash("#if X\n"
5659 "#else\n"
5660 "#else\n"
5661 "#endif\n"
5662 "#endif");
5663 verifyNoCrash("#if X\n"
5664 "#elif Y\n"
5665 "#elif Y\n"
5666 "#endif\n"
5667 "#endif");
5668 verifyNoCrash("#endif\n"
5669 "#endif");
5670 verifyNoCrash("#endif\n"
5671 "#else");
5672 verifyNoCrash("#endif\n"
5673 "#elif Y");
5676 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5677 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5678 verifyFormat("class A : public QObject {\n"
5679 " Q_OBJECT\n"
5680 "\n"
5681 " A() {}\n"
5682 "};",
5683 "class A : public QObject {\n"
5684 " Q_OBJECT\n"
5685 "\n"
5686 " A() {\n}\n"
5687 "} ;");
5688 verifyFormat("MACRO\n"
5689 "/*static*/ int i;",
5690 "MACRO\n"
5691 " /*static*/ int i;");
5692 verifyFormat("SOME_MACRO\n"
5693 "namespace {\n"
5694 "void f();\n"
5695 "} // namespace",
5696 "SOME_MACRO\n"
5697 " namespace {\n"
5698 "void f( );\n"
5699 "} // namespace");
5700 // Only if the identifier contains at least 5 characters.
5701 verifyFormat("HTTP f();", "HTTP\nf();");
5702 verifyNoChange("MACRO\nf();");
5703 // Only if everything is upper case.
5704 verifyFormat("class A : public QObject {\n"
5705 " Q_Object A() {}\n"
5706 "};",
5707 "class A : public QObject {\n"
5708 " Q_Object\n"
5709 " A() {\n}\n"
5710 "} ;");
5712 // Only if the next line can actually start an unwrapped line.
5713 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5714 "<< SomeThing;");
5716 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5717 "(n, buffers))",
5718 getChromiumStyle(FormatStyle::LK_Cpp));
5720 // See PR41483
5721 verifyNoChange("/**/ FOO(a)\n"
5722 "FOO(b)");
5725 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5726 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5727 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5728 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5729 "class X {};\n"
5730 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5731 "int *createScopDetectionPass() { return 0; }",
5732 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5733 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5734 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5735 " class X {};\n"
5736 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5737 " int *createScopDetectionPass() { return 0; }");
5738 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5739 // braces, so that inner block is indented one level more.
5740 verifyFormat("int q() {\n"
5741 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5742 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5743 " IPC_END_MESSAGE_MAP()\n"
5744 "}",
5745 "int q() {\n"
5746 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5747 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5748 " IPC_END_MESSAGE_MAP()\n"
5749 "}");
5751 // Same inside macros.
5752 verifyFormat("#define LIST(L) \\\n"
5753 " L(A) \\\n"
5754 " L(B) \\\n"
5755 " L(C)",
5756 "#define LIST(L) \\\n"
5757 " L(A) \\\n"
5758 " L(B) \\\n"
5759 " L(C)",
5760 getGoogleStyle());
5762 // These must not be recognized as macros.
5763 verifyFormat("int q() {\n"
5764 " f(x);\n"
5765 " f(x) {}\n"
5766 " f(x)->g();\n"
5767 " f(x)->*g();\n"
5768 " f(x).g();\n"
5769 " f(x) = x;\n"
5770 " f(x) += x;\n"
5771 " f(x) -= x;\n"
5772 " f(x) *= x;\n"
5773 " f(x) /= x;\n"
5774 " f(x) %= x;\n"
5775 " f(x) &= x;\n"
5776 " f(x) |= x;\n"
5777 " f(x) ^= x;\n"
5778 " f(x) >>= x;\n"
5779 " f(x) <<= x;\n"
5780 " f(x)[y].z();\n"
5781 " LOG(INFO) << x;\n"
5782 " ifstream(x) >> x;\n"
5783 "}",
5784 "int q() {\n"
5785 " f(x)\n;\n"
5786 " f(x)\n {}\n"
5787 " f(x)\n->g();\n"
5788 " f(x)\n->*g();\n"
5789 " f(x)\n.g();\n"
5790 " f(x)\n = x;\n"
5791 " f(x)\n += x;\n"
5792 " f(x)\n -= x;\n"
5793 " f(x)\n *= x;\n"
5794 " f(x)\n /= x;\n"
5795 " f(x)\n %= x;\n"
5796 " f(x)\n &= x;\n"
5797 " f(x)\n |= x;\n"
5798 " f(x)\n ^= x;\n"
5799 " f(x)\n >>= x;\n"
5800 " f(x)\n <<= x;\n"
5801 " f(x)\n[y].z();\n"
5802 " LOG(INFO)\n << x;\n"
5803 " ifstream(x)\n >> x;\n"
5804 "}");
5805 verifyFormat("int q() {\n"
5806 " F(x)\n"
5807 " if (1) {\n"
5808 " }\n"
5809 " F(x)\n"
5810 " while (1) {\n"
5811 " }\n"
5812 " F(x)\n"
5813 " G(x);\n"
5814 " F(x)\n"
5815 " try {\n"
5816 " Q();\n"
5817 " } catch (...) {\n"
5818 " }\n"
5819 "}",
5820 "int q() {\n"
5821 "F(x)\n"
5822 "if (1) {}\n"
5823 "F(x)\n"
5824 "while (1) {}\n"
5825 "F(x)\n"
5826 "G(x);\n"
5827 "F(x)\n"
5828 "try { Q(); } catch (...) {}\n"
5829 "}");
5830 verifyFormat("class A {\n"
5831 " A() : t(0) {}\n"
5832 " A(int i) noexcept() : {}\n"
5833 " A(X x)\n" // FIXME: function-level try blocks are broken.
5834 " try : t(0) {\n"
5835 " } catch (...) {\n"
5836 " }\n"
5837 "};",
5838 "class A {\n"
5839 " A()\n : t(0) {}\n"
5840 " A(int i)\n noexcept() : {}\n"
5841 " A(X x)\n"
5842 " try : t(0) {} catch (...) {}\n"
5843 "};");
5844 FormatStyle Style = getLLVMStyle();
5845 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5846 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5847 Style.BraceWrapping.AfterFunction = true;
5848 verifyFormat("void f()\n"
5849 "try\n"
5850 "{\n"
5851 "}",
5852 "void f() try {\n"
5853 "}",
5854 Style);
5855 verifyFormat("class SomeClass {\n"
5856 "public:\n"
5857 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5858 "};",
5859 "class SomeClass {\n"
5860 "public:\n"
5861 " SomeClass()\n"
5862 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5863 "};");
5864 verifyFormat("class SomeClass {\n"
5865 "public:\n"
5866 " SomeClass()\n"
5867 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5868 "};",
5869 "class SomeClass {\n"
5870 "public:\n"
5871 " SomeClass()\n"
5872 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5873 "};",
5874 getLLVMStyleWithColumns(40));
5876 verifyFormat("MACRO(>)");
5878 // Some macros contain an implicit semicolon.
5879 Style = getLLVMStyle();
5880 Style.StatementMacros.push_back("FOO");
5881 verifyFormat("FOO(a) int b = 0;");
5882 verifyFormat("FOO(a)\n"
5883 "int b = 0;",
5884 Style);
5885 verifyFormat("FOO(a);\n"
5886 "int b = 0;",
5887 Style);
5888 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5889 "int b = 0;",
5890 Style);
5891 verifyFormat("FOO()\n"
5892 "int b = 0;",
5893 Style);
5894 verifyFormat("FOO\n"
5895 "int b = 0;",
5896 Style);
5897 verifyFormat("void f() {\n"
5898 " FOO(a)\n"
5899 " return a;\n"
5900 "}",
5901 Style);
5902 verifyFormat("FOO(a)\n"
5903 "FOO(b)",
5904 Style);
5905 verifyFormat("int a = 0;\n"
5906 "FOO(b)\n"
5907 "int c = 0;",
5908 Style);
5909 verifyFormat("int a = 0;\n"
5910 "int x = FOO(a)\n"
5911 "int b = 0;",
5912 Style);
5913 verifyFormat("void foo(int a) { FOO(a) }\n"
5914 "uint32_t bar() {}",
5915 Style);
5918 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
5919 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
5921 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5922 ZeroColumn);
5925 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
5926 verifyFormat("#define A \\\n"
5927 " f({ \\\n"
5928 " g(); \\\n"
5929 " });",
5930 getLLVMStyleWithColumns(11));
5933 TEST_F(FormatTest, IndentPreprocessorDirectives) {
5934 FormatStyle Style = getLLVMStyleWithColumns(40);
5935 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
5936 verifyFormat("#ifdef _WIN32\n"
5937 "#define A 0\n"
5938 "#ifdef VAR2\n"
5939 "#define B 1\n"
5940 "#include <someheader.h>\n"
5941 "#define MACRO \\\n"
5942 " some_very_long_func_aaaaaaaaaa();\n"
5943 "#endif\n"
5944 "#else\n"
5945 "#define A 1\n"
5946 "#endif",
5947 Style);
5948 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5949 verifyFormat("#if 1\n"
5950 "# define __STR(x) #x\n"
5951 "#endif",
5952 Style);
5953 verifyFormat("#ifdef _WIN32\n"
5954 "# define A 0\n"
5955 "# ifdef VAR2\n"
5956 "# define B 1\n"
5957 "# include <someheader.h>\n"
5958 "# define MACRO \\\n"
5959 " some_very_long_func_aaaaaaaaaa();\n"
5960 "# endif\n"
5961 "#else\n"
5962 "# define A 1\n"
5963 "#endif",
5964 Style);
5965 verifyFormat("#if A\n"
5966 "# define MACRO \\\n"
5967 " void a(int x) { \\\n"
5968 " b(); \\\n"
5969 " c(); \\\n"
5970 " d(); \\\n"
5971 " e(); \\\n"
5972 " f(); \\\n"
5973 " }\n"
5974 "#endif",
5975 Style);
5976 // Comments before include guard.
5977 verifyFormat("// file comment\n"
5978 "// file comment\n"
5979 "#ifndef HEADER_H\n"
5980 "#define HEADER_H\n"
5981 "code();\n"
5982 "#endif",
5983 Style);
5984 // Test with include guards.
5985 verifyFormat("#ifndef HEADER_H\n"
5986 "#define HEADER_H\n"
5987 "code();\n"
5988 "#endif",
5989 Style);
5990 // Include guards must have a #define with the same variable immediately
5991 // after #ifndef.
5992 verifyFormat("#ifndef NOT_GUARD\n"
5993 "# define FOO\n"
5994 "code();\n"
5995 "#endif",
5996 Style);
5998 // Include guards must cover the entire file.
5999 verifyFormat("code();\n"
6000 "code();\n"
6001 "#ifndef NOT_GUARD\n"
6002 "# define NOT_GUARD\n"
6003 "code();\n"
6004 "#endif",
6005 Style);
6006 verifyFormat("#ifndef NOT_GUARD\n"
6007 "# define NOT_GUARD\n"
6008 "code();\n"
6009 "#endif\n"
6010 "code();",
6011 Style);
6012 // Test with trailing blank lines.
6013 verifyFormat("#ifndef HEADER_H\n"
6014 "#define HEADER_H\n"
6015 "code();\n"
6016 "#endif",
6017 Style);
6018 // Include guards don't have #else.
6019 verifyFormat("#ifndef NOT_GUARD\n"
6020 "# define NOT_GUARD\n"
6021 "code();\n"
6022 "#else\n"
6023 "#endif",
6024 Style);
6025 verifyFormat("#ifndef NOT_GUARD\n"
6026 "# define NOT_GUARD\n"
6027 "code();\n"
6028 "#elif FOO\n"
6029 "#endif",
6030 Style);
6031 // Non-identifier #define after potential include guard.
6032 verifyFormat("#ifndef FOO\n"
6033 "# define 1\n"
6034 "#endif",
6035 Style);
6036 // #if closes past last non-preprocessor line.
6037 verifyFormat("#ifndef FOO\n"
6038 "#define FOO\n"
6039 "#if 1\n"
6040 "int i;\n"
6041 "# define A 0\n"
6042 "#endif\n"
6043 "#endif",
6044 Style);
6045 // Don't crash if there is an #elif directive without a condition.
6046 verifyFormat("#if 1\n"
6047 "int x;\n"
6048 "#elif\n"
6049 "int y;\n"
6050 "#else\n"
6051 "int z;\n"
6052 "#endif",
6053 Style);
6054 // FIXME: This doesn't handle the case where there's code between the
6055 // #ifndef and #define but all other conditions hold. This is because when
6056 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6057 // previous code line yet, so we can't detect it.
6058 verifyFormat("#ifndef NOT_GUARD\n"
6059 "code();\n"
6060 "#define NOT_GUARD\n"
6061 "code();\n"
6062 "#endif",
6063 "#ifndef NOT_GUARD\n"
6064 "code();\n"
6065 "# define NOT_GUARD\n"
6066 "code();\n"
6067 "#endif",
6068 Style);
6069 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6070 // be outside an include guard. Examples are #pragma once and
6071 // #pragma GCC diagnostic, or anything else that does not change the meaning
6072 // of the file if it's included multiple times.
6073 verifyFormat("#ifdef WIN32\n"
6074 "# pragma once\n"
6075 "#endif\n"
6076 "#ifndef HEADER_H\n"
6077 "# define HEADER_H\n"
6078 "code();\n"
6079 "#endif",
6080 "#ifdef WIN32\n"
6081 "# pragma once\n"
6082 "#endif\n"
6083 "#ifndef HEADER_H\n"
6084 "#define HEADER_H\n"
6085 "code();\n"
6086 "#endif",
6087 Style);
6088 // FIXME: This does not detect when there is a single non-preprocessor line
6089 // in front of an include-guard-like structure where other conditions hold
6090 // because ScopedLineState hides the line.
6091 verifyFormat("code();\n"
6092 "#ifndef HEADER_H\n"
6093 "#define HEADER_H\n"
6094 "code();\n"
6095 "#endif",
6096 "code();\n"
6097 "#ifndef HEADER_H\n"
6098 "# define HEADER_H\n"
6099 "code();\n"
6100 "#endif",
6101 Style);
6102 // Keep comments aligned with #, otherwise indent comments normally. These
6103 // tests cannot use verifyFormat because messUp manipulates leading
6104 // whitespace.
6106 const char *Expected = ""
6107 "void f() {\n"
6108 "#if 1\n"
6109 "// Preprocessor aligned.\n"
6110 "# define A 0\n"
6111 " // Code. Separated by blank line.\n"
6112 "\n"
6113 "# define B 0\n"
6114 " // Code. Not aligned with #\n"
6115 "# define C 0\n"
6116 "#endif";
6117 const char *ToFormat = ""
6118 "void f() {\n"
6119 "#if 1\n"
6120 "// Preprocessor aligned.\n"
6121 "# define A 0\n"
6122 "// Code. Separated by blank line.\n"
6123 "\n"
6124 "# define B 0\n"
6125 " // Code. Not aligned with #\n"
6126 "# define C 0\n"
6127 "#endif";
6128 verifyFormat(Expected, ToFormat, Style);
6129 verifyNoChange(Expected, Style);
6131 // Keep block quotes aligned.
6133 const char *Expected = ""
6134 "void f() {\n"
6135 "#if 1\n"
6136 "/* Preprocessor aligned. */\n"
6137 "# define A 0\n"
6138 " /* Code. Separated by blank line. */\n"
6139 "\n"
6140 "# define B 0\n"
6141 " /* Code. Not aligned with # */\n"
6142 "# define C 0\n"
6143 "#endif";
6144 const char *ToFormat = ""
6145 "void f() {\n"
6146 "#if 1\n"
6147 "/* Preprocessor aligned. */\n"
6148 "# define A 0\n"
6149 "/* Code. Separated by blank line. */\n"
6150 "\n"
6151 "# define B 0\n"
6152 " /* Code. Not aligned with # */\n"
6153 "# define C 0\n"
6154 "#endif";
6155 verifyFormat(Expected, ToFormat, Style);
6156 verifyNoChange(Expected, Style);
6158 // Keep comments aligned with un-indented directives.
6160 const char *Expected = ""
6161 "void f() {\n"
6162 "// Preprocessor aligned.\n"
6163 "#define A 0\n"
6164 " // Code. Separated by blank line.\n"
6165 "\n"
6166 "#define B 0\n"
6167 " // Code. Not aligned with #\n"
6168 "#define C 0\n";
6169 const char *ToFormat = ""
6170 "void f() {\n"
6171 "// Preprocessor aligned.\n"
6172 "#define A 0\n"
6173 "// Code. Separated by blank line.\n"
6174 "\n"
6175 "#define B 0\n"
6176 " // Code. Not aligned with #\n"
6177 "#define C 0\n";
6178 verifyFormat(Expected, ToFormat, Style);
6179 verifyNoChange(Expected, Style);
6181 // Test AfterHash with tabs.
6183 FormatStyle Tabbed = Style;
6184 Tabbed.UseTab = FormatStyle::UT_Always;
6185 Tabbed.IndentWidth = 8;
6186 Tabbed.TabWidth = 8;
6187 verifyFormat("#ifdef _WIN32\n"
6188 "#\tdefine A 0\n"
6189 "#\tifdef VAR2\n"
6190 "#\t\tdefine B 1\n"
6191 "#\t\tinclude <someheader.h>\n"
6192 "#\t\tdefine MACRO \\\n"
6193 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6194 "#\tendif\n"
6195 "#else\n"
6196 "#\tdefine A 1\n"
6197 "#endif",
6198 Tabbed);
6201 // Regression test: Multiline-macro inside include guards.
6202 verifyFormat("#ifndef HEADER_H\n"
6203 "#define HEADER_H\n"
6204 "#define A() \\\n"
6205 " int i; \\\n"
6206 " int j;\n"
6207 "#endif // HEADER_H",
6208 getLLVMStyleWithColumns(20));
6210 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6211 // Basic before hash indent tests
6212 verifyFormat("#ifdef _WIN32\n"
6213 " #define A 0\n"
6214 " #ifdef VAR2\n"
6215 " #define B 1\n"
6216 " #include <someheader.h>\n"
6217 " #define MACRO \\\n"
6218 " some_very_long_func_aaaaaaaaaa();\n"
6219 " #endif\n"
6220 "#else\n"
6221 " #define A 1\n"
6222 "#endif",
6223 Style);
6224 verifyFormat("#if A\n"
6225 " #define MACRO \\\n"
6226 " void a(int x) { \\\n"
6227 " b(); \\\n"
6228 " c(); \\\n"
6229 " d(); \\\n"
6230 " e(); \\\n"
6231 " f(); \\\n"
6232 " }\n"
6233 "#endif",
6234 Style);
6235 // Keep comments aligned with indented directives. These
6236 // tests cannot use verifyFormat because messUp manipulates leading
6237 // whitespace.
6239 const char *Expected = "void f() {\n"
6240 "// Aligned to preprocessor.\n"
6241 "#if 1\n"
6242 " // Aligned to code.\n"
6243 " int a;\n"
6244 " #if 1\n"
6245 " // Aligned to preprocessor.\n"
6246 " #define A 0\n"
6247 " // Aligned to code.\n"
6248 " int b;\n"
6249 " #endif\n"
6250 "#endif\n"
6251 "}";
6252 const char *ToFormat = "void f() {\n"
6253 "// Aligned to preprocessor.\n"
6254 "#if 1\n"
6255 "// Aligned to code.\n"
6256 "int a;\n"
6257 "#if 1\n"
6258 "// Aligned to preprocessor.\n"
6259 "#define A 0\n"
6260 "// Aligned to code.\n"
6261 "int b;\n"
6262 "#endif\n"
6263 "#endif\n"
6264 "}";
6265 verifyFormat(Expected, ToFormat, Style);
6266 verifyNoChange(Expected, Style);
6269 const char *Expected = "void f() {\n"
6270 "/* Aligned to preprocessor. */\n"
6271 "#if 1\n"
6272 " /* Aligned to code. */\n"
6273 " int a;\n"
6274 " #if 1\n"
6275 " /* Aligned to preprocessor. */\n"
6276 " #define A 0\n"
6277 " /* Aligned to code. */\n"
6278 " int b;\n"
6279 " #endif\n"
6280 "#endif\n"
6281 "}";
6282 const char *ToFormat = "void f() {\n"
6283 "/* Aligned to preprocessor. */\n"
6284 "#if 1\n"
6285 "/* Aligned to code. */\n"
6286 "int a;\n"
6287 "#if 1\n"
6288 "/* Aligned to preprocessor. */\n"
6289 "#define A 0\n"
6290 "/* Aligned to code. */\n"
6291 "int b;\n"
6292 "#endif\n"
6293 "#endif\n"
6294 "}";
6295 verifyFormat(Expected, ToFormat, Style);
6296 verifyNoChange(Expected, Style);
6299 // Test single comment before preprocessor
6300 verifyFormat("// Comment\n"
6301 "\n"
6302 "#if 1\n"
6303 "#endif",
6304 Style);
6307 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6308 FormatStyle Style = getLLVMStyle();
6309 Style.AlignConsecutiveAssignments.Enabled = true;
6310 Style.AlignConsecutiveDeclarations.Enabled = true;
6312 // Test with just #if blocks.
6313 verifyFormat("void f1() {\n"
6314 "#if 1\n"
6315 " int foo = 1;\n"
6316 " int foobar = 2;\n"
6317 "#endif\n"
6318 "}\n"
6319 "#if 1\n"
6320 "int baz = 3;\n"
6321 "#endif\n"
6322 "void f2() {\n"
6323 "#if 1\n"
6324 " char *foobarbaz = \"foobarbaz\";\n"
6325 " int quux = 4;\n"
6326 "}",
6327 Style);
6329 // Test with just #else blocks.
6330 verifyFormat("void f1() {\n"
6331 "#if 1\n"
6332 "#else\n"
6333 " int foo = 1;\n"
6334 " int foobar = 2;\n"
6335 "#endif\n"
6336 "}\n"
6337 "#if 1\n"
6338 "#else\n"
6339 "int baz = 3;\n"
6340 "#endif\n"
6341 "void f2() {\n"
6342 "#if 1\n"
6343 "#else\n"
6344 " char *foobarbaz = \"foobarbaz\";\n"
6345 " int quux = 4;\n"
6346 "}",
6347 Style);
6349 // Test with a mix of #if and #else blocks.
6350 verifyFormat("void f1() {\n"
6351 "#if 1\n"
6352 "#else\n"
6353 " int foo = 1;\n"
6354 " int foobar = 2;\n"
6355 "#endif\n"
6356 "}\n"
6357 "#if 1\n"
6358 "int baz = 3;\n"
6359 "#endif\n"
6360 "void f2() {\n"
6361 "#if 1\n"
6362 "#else\n"
6363 " // prevent alignment with #else in f1\n"
6364 " char *foobarbaz = \"foobarbaz\";\n"
6365 " int quux = 4;\n"
6366 "}",
6367 Style);
6369 // Test with nested #if and #else blocks.
6370 verifyFormat("void f1() {\n"
6371 "#if 1\n"
6372 "#else\n"
6373 "#if 2\n"
6374 "#else\n"
6375 " int foo = 1;\n"
6376 " int foobar = 2;\n"
6377 "#endif\n"
6378 "#endif\n"
6379 "}\n"
6380 "#if 1\n"
6381 "#else\n"
6382 "#if 2\n"
6383 "int baz = 3;\n"
6384 "#endif\n"
6385 "#endif\n"
6386 "void f2() {\n"
6387 "#if 1\n"
6388 "#if 2\n"
6389 "#else\n"
6390 " // prevent alignment with #else in f1\n"
6391 " char *foobarbaz = \"foobarbaz\";\n"
6392 " int quux = 4;\n"
6393 "#endif\n"
6394 "#endif\n"
6395 "}",
6396 Style);
6398 verifyFormat("#if FOO\n"
6399 "int a = 1;\n"
6400 "#else\n"
6401 "int ab = 2;\n"
6402 "#endif\n"
6403 "#ifdef BAR\n"
6404 "int abc = 3;\n"
6405 "#elifdef BAZ\n"
6406 "int abcd = 4;\n"
6407 "#endif",
6408 Style);
6410 verifyFormat("void f() {\n"
6411 " if (foo) {\n"
6412 "#if FOO\n"
6413 " int a = 1;\n"
6414 "#else\n"
6415 " bool a = true;\n"
6416 "#endif\n"
6417 " int abc = 3;\n"
6418 "#ifndef BAR\n"
6419 " int abcd = 4;\n"
6420 "#elif BAZ\n"
6421 " bool abcd = true;\n"
6422 "#endif\n"
6423 " }\n"
6424 "}",
6425 Style);
6427 verifyFormat("void f() {\n"
6428 "#if FOO\n"
6429 " a = 1;\n"
6430 "#else\n"
6431 " ab = 2;\n"
6432 "#endif\n"
6433 "}\n"
6434 "void g() {\n"
6435 "#if BAR\n"
6436 " abc = 3;\n"
6437 "#elifndef BAZ\n"
6438 " abcd = 4;\n"
6439 "#endif\n"
6440 "}",
6441 Style);
6444 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6445 verifyFormat("{\n { a #c; }\n}");
6448 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6449 verifyFormat("#define A \\\n { \\\n {\nint i;",
6450 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6451 verifyFormat("#define A \\\n } \\\n }\nint i;",
6452 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6455 TEST_F(FormatTest, EscapedNewlines) {
6456 FormatStyle Narrow = getLLVMStyleWithColumns(11);
6457 verifyFormat("#define A \\\n int i; \\\n int j;",
6458 "#define A \\\nint i;\\\n int j;", Narrow);
6459 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6460 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6461 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6462 verifyNoChange("<a\n\\\\\n>");
6464 FormatStyle AlignLeft = getLLVMStyle();
6465 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6466 verifyFormat("#define MACRO(x) \\\n"
6467 "private: \\\n"
6468 " int x(int a);",
6469 AlignLeft);
6471 // CRLF line endings
6472 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6473 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6474 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6475 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6476 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6477 verifyNoChange("<a\r\n\\\\\r\n>");
6478 verifyFormat("#define MACRO(x) \\\r\n"
6479 "private: \\\r\n"
6480 " int x(int a);",
6481 AlignLeft);
6483 FormatStyle DontAlign = getLLVMStyle();
6484 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6485 DontAlign.MaxEmptyLinesToKeep = 3;
6486 // FIXME: can't use verifyFormat here because the newline before
6487 // "public:" is not inserted the first time it's reformatted
6488 verifyNoChange("#define A \\\n"
6489 " class Foo { \\\n"
6490 " void bar(); \\\n"
6491 "\\\n"
6492 "\\\n"
6493 "\\\n"
6494 " public: \\\n"
6495 " void baz(); \\\n"
6496 " };",
6497 DontAlign);
6500 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6501 verifyFormat("#define A \\\n"
6502 " int v( \\\n"
6503 " a); \\\n"
6504 " int i;",
6505 getLLVMStyleWithColumns(11));
6508 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6509 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6510 " \\\n"
6511 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6512 "\n"
6513 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6514 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6515 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6516 "\\\n"
6517 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6518 " \n"
6519 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6520 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6523 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6524 verifyFormat("int\n"
6525 "#define A\n"
6526 " a;",
6527 "int\n#define A\na;");
6528 verifyFormat("functionCallTo(\n"
6529 " someOtherFunction(\n"
6530 " withSomeParameters, whichInSequence,\n"
6531 " areLongerThanALine(andAnotherCall,\n"
6532 "#define A B\n"
6533 " withMoreParamters,\n"
6534 " whichStronglyInfluenceTheLayout),\n"
6535 " andMoreParameters),\n"
6536 " trailing);",
6537 getLLVMStyleWithColumns(69));
6538 verifyFormat("Foo::Foo()\n"
6539 "#ifdef BAR\n"
6540 " : baz(0)\n"
6541 "#endif\n"
6542 "{\n"
6543 "}");
6544 verifyFormat("void f() {\n"
6545 " if (true)\n"
6546 "#ifdef A\n"
6547 " f(42);\n"
6548 " x();\n"
6549 "#else\n"
6550 " g();\n"
6551 " x();\n"
6552 "#endif\n"
6553 "}");
6554 verifyFormat("void f(param1, param2,\n"
6555 " param3,\n"
6556 "#ifdef A\n"
6557 " param4(param5,\n"
6558 "#ifdef A1\n"
6559 " param6,\n"
6560 "#ifdef A2\n"
6561 " param7),\n"
6562 "#else\n"
6563 " param8),\n"
6564 " param9,\n"
6565 "#endif\n"
6566 " param10,\n"
6567 "#endif\n"
6568 " param11)\n"
6569 "#else\n"
6570 " param12)\n"
6571 "#endif\n"
6572 "{\n"
6573 " x();\n"
6574 "}",
6575 getLLVMStyleWithColumns(28));
6576 verifyFormat("#if 1\n"
6577 "int i;");
6578 verifyFormat("#if 1\n"
6579 "#endif\n"
6580 "#if 1\n"
6581 "#else\n"
6582 "#endif");
6583 verifyFormat("DEBUG({\n"
6584 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6586 "});\n"
6587 "#if a\n"
6588 "#else\n"
6589 "#endif");
6591 verifyIncompleteFormat("void f(\n"
6592 "#if A\n"
6593 ");\n"
6594 "#else\n"
6595 "#endif");
6597 // Verify that indentation is correct when there is an `#if 0` with an
6598 // `#else`.
6599 verifyFormat("#if 0\n"
6600 "{\n"
6601 "#else\n"
6602 "{\n"
6603 "#endif\n"
6604 " x;\n"
6605 "}");
6607 verifyFormat("#if 0\n"
6608 "#endif\n"
6609 "#if X\n"
6610 "int something_fairly_long; // Align here please\n"
6611 "#endif // Should be aligned");
6613 verifyFormat("#if 0\n"
6614 "#endif\n"
6615 "#if X\n"
6616 "#else // Align\n"
6617 ";\n"
6618 "#endif // Align");
6620 verifyFormat("void SomeFunction(int param1,\n"
6621 " template <\n"
6622 "#ifdef A\n"
6623 "#if 0\n"
6624 "#endif\n"
6625 " MyType<Some>>\n"
6626 "#else\n"
6627 " Type1, Type2>\n"
6628 "#endif\n"
6629 " param2,\n"
6630 " param3) {\n"
6631 " f();\n"
6632 "}");
6635 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6636 verifyFormat("#endif\n"
6637 "#if B");
6640 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6641 FormatStyle SingleLine = getLLVMStyle();
6642 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6643 verifyFormat("#if 0\n"
6644 "#elif 1\n"
6645 "#endif\n"
6646 "void foo() {\n"
6647 " if (test) foo2();\n"
6648 "}",
6649 SingleLine);
6652 TEST_F(FormatTest, LayoutBlockInsideParens) {
6653 verifyFormat("functionCall({ int i; });");
6654 verifyFormat("functionCall({\n"
6655 " int i;\n"
6656 " int j;\n"
6657 "});");
6658 verifyFormat("functionCall(\n"
6659 " {\n"
6660 " int i;\n"
6661 " int j;\n"
6662 " },\n"
6663 " aaaa, bbbb, cccc);");
6664 verifyFormat("functionA(functionB({\n"
6665 " int i;\n"
6666 " int j;\n"
6667 " }),\n"
6668 " aaaa, bbbb, cccc);");
6669 verifyFormat("functionCall(\n"
6670 " {\n"
6671 " int i;\n"
6672 " int j;\n"
6673 " },\n"
6674 " aaaa, bbbb, // comment\n"
6675 " cccc);");
6676 verifyFormat("functionA(functionB({\n"
6677 " int i;\n"
6678 " int j;\n"
6679 " }),\n"
6680 " aaaa, bbbb, // comment\n"
6681 " cccc);");
6682 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6683 verifyFormat("functionCall(aaaa, bbbb, {\n"
6684 " int i;\n"
6685 " int j;\n"
6686 "});");
6687 verifyFormat(
6688 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6689 " {\n"
6690 " int i; // break\n"
6691 " },\n"
6692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6693 " ccccccccccccccccc));");
6694 verifyFormat("DEBUG({\n"
6695 " if (a)\n"
6696 " f();\n"
6697 "});");
6700 TEST_F(FormatTest, LayoutBlockInsideStatement) {
6701 verifyFormat("SOME_MACRO { int i; }\n"
6702 "int i;",
6703 " SOME_MACRO {int i;} int i;");
6706 TEST_F(FormatTest, LayoutNestedBlocks) {
6707 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6708 " struct s {\n"
6709 " int i;\n"
6710 " };\n"
6711 " s kBitsToOs[] = {{10}};\n"
6712 " for (int i = 0; i < 10; ++i)\n"
6713 " return;\n"
6714 "}");
6715 verifyFormat("call(parameter, {\n"
6716 " something();\n"
6717 " // Comment using all columns.\n"
6718 " somethingelse();\n"
6719 "});",
6720 getLLVMStyleWithColumns(40));
6721 verifyFormat("DEBUG( //\n"
6722 " { f(); }, a);");
6723 verifyFormat("DEBUG( //\n"
6724 " {\n"
6725 " f(); //\n"
6726 " },\n"
6727 " a);");
6729 verifyFormat("call(parameter, {\n"
6730 " something();\n"
6731 " // Comment too\n"
6732 " // looooooooooong.\n"
6733 " somethingElse();\n"
6734 "});",
6735 "call(parameter, {\n"
6736 " something();\n"
6737 " // Comment too looooooooooong.\n"
6738 " somethingElse();\n"
6739 "});",
6740 getLLVMStyleWithColumns(29));
6741 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6742 verifyFormat("DEBUG({ // comment\n"
6743 " int i;\n"
6744 "});",
6745 "DEBUG({ // comment\n"
6746 "int i;\n"
6747 "});");
6748 verifyFormat("DEBUG({\n"
6749 " int i;\n"
6750 "\n"
6751 " // comment\n"
6752 " int j;\n"
6753 "});",
6754 "DEBUG({\n"
6755 " int i;\n"
6756 "\n"
6757 " // comment\n"
6758 " int j;\n"
6759 "});");
6761 verifyFormat("DEBUG({\n"
6762 " if (a)\n"
6763 " return;\n"
6764 "});");
6765 verifyGoogleFormat("DEBUG({\n"
6766 " if (a) return;\n"
6767 "});");
6768 FormatStyle Style = getGoogleStyle();
6769 Style.ColumnLimit = 45;
6770 verifyFormat("Debug(\n"
6771 " aaaaa,\n"
6772 " {\n"
6773 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6774 " },\n"
6775 " a);",
6776 Style);
6778 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6780 verifyNoCrash("^{v^{a}}");
6783 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6784 verifyFormat("#define MACRO() \\\n"
6785 " Debug(aaa, /* force line break */ \\\n"
6786 " { \\\n"
6787 " int i; \\\n"
6788 " int j; \\\n"
6789 " })",
6790 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6791 " { int i; int j; })",
6792 getGoogleStyle());
6794 verifyFormat("#define A \\\n"
6795 " [] { \\\n"
6796 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6797 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6798 " }",
6799 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6800 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6801 getGoogleStyle());
6804 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
6805 verifyFormat("{}");
6806 verifyFormat("enum E {};");
6807 verifyFormat("enum E {}");
6808 FormatStyle Style = getLLVMStyle();
6809 Style.SpaceInEmptyBlock = true;
6810 verifyFormat("void f() { }", "void f() {}", Style);
6811 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
6812 verifyFormat("while (true) { }", "while (true) {}", Style);
6813 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6814 Style.BraceWrapping.BeforeElse = false;
6815 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6816 verifyFormat("if (a)\n"
6817 "{\n"
6818 "} else if (b)\n"
6819 "{\n"
6820 "} else\n"
6821 "{ }",
6822 Style);
6823 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
6824 verifyFormat("if (a) {\n"
6825 "} else if (b) {\n"
6826 "} else {\n"
6827 "}",
6828 Style);
6829 Style.BraceWrapping.BeforeElse = true;
6830 verifyFormat("if (a) { }\n"
6831 "else if (b) { }\n"
6832 "else { }",
6833 Style);
6836 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
6837 FormatStyle Style = getLLVMStyle();
6838 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
6839 Style.MacroBlockEnd = "^[A-Z_]+_END$";
6840 verifyFormat("FOO_BEGIN\n"
6841 " FOO_ENTRY\n"
6842 "FOO_END",
6843 Style);
6844 verifyFormat("FOO_BEGIN\n"
6845 " NESTED_FOO_BEGIN\n"
6846 " NESTED_FOO_ENTRY\n"
6847 " NESTED_FOO_END\n"
6848 "FOO_END",
6849 Style);
6850 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
6851 " int x;\n"
6852 " x = 1;\n"
6853 "FOO_END(Baz)",
6854 Style);
6856 Style.RemoveBracesLLVM = true;
6857 verifyNoCrash("for (;;)\n"
6858 " FOO_BEGIN\n"
6859 " foo();\n"
6860 " FOO_END",
6861 Style);
6864 //===----------------------------------------------------------------------===//
6865 // Line break tests.
6866 //===----------------------------------------------------------------------===//
6868 TEST_F(FormatTest, PreventConfusingIndents) {
6869 verifyFormat(
6870 "void f() {\n"
6871 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
6872 " parameter, parameter, parameter)),\n"
6873 " SecondLongCall(parameter));\n"
6874 "}");
6875 verifyFormat(
6876 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6877 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6879 " aaaaaaaaaaaaaaaaaaaaaaaa);");
6880 verifyFormat(
6881 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6882 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
6883 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6884 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
6885 verifyFormat(
6886 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6887 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
6888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
6889 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
6890 verifyFormat("int a = bbbb && ccc &&\n"
6891 " fffff(\n"
6892 "#define A Just forcing a new line\n"
6893 " ddd);");
6896 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
6897 verifyFormat(
6898 "bool aaaaaaa =\n"
6899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
6900 " bbbbbbbb();");
6901 verifyFormat(
6902 "bool aaaaaaa =\n"
6903 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
6904 " bbbbbbbb();");
6906 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
6907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
6908 " ccccccccc == ddddddddddd;");
6909 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
6910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
6911 " ccccccccc == ddddddddddd;");
6912 verifyFormat(
6913 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
6914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
6915 " ccccccccc == ddddddddddd;");
6917 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6918 " aaaaaa) &&\n"
6919 " bbbbbb && cccccc;");
6920 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6921 " aaaaaa) >>\n"
6922 " bbbbbb;");
6923 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
6924 " SourceMgr.getSpellingColumnNumber(\n"
6925 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
6926 " 1);");
6928 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6929 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
6930 " cccccc) {\n}");
6931 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6932 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6933 " cccccc) {\n}");
6934 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6935 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6936 " cccccc) {\n}");
6937 verifyFormat("b = a &&\n"
6938 " // Comment\n"
6939 " b.c && d;");
6941 // If the LHS of a comparison is not a binary expression itself, the
6942 // additional linebreak confuses many people.
6943 verifyFormat(
6944 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
6946 "}");
6947 verifyFormat(
6948 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6950 "}");
6951 verifyFormat(
6952 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
6953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6954 "}");
6955 verifyFormat(
6956 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
6958 "}");
6959 // Even explicit parentheses stress the precedence enough to make the
6960 // additional break unnecessary.
6961 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6963 "}");
6964 // This cases is borderline, but with the indentation it is still readable.
6965 verifyFormat(
6966 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6967 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
6969 "}",
6970 getLLVMStyleWithColumns(75));
6972 // If the LHS is a binary expression, we should still use the additional break
6973 // as otherwise the formatting hides the operator precedence.
6974 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6976 " 5) {\n"
6977 "}");
6978 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
6980 " 5) {\n"
6981 "}");
6983 FormatStyle OnePerLine = getLLVMStyle();
6984 OnePerLine.BinPackParameters = false;
6985 verifyFormat(
6986 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
6989 OnePerLine);
6991 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
6992 " .aaa(aaaaaaaaaaaaa) *\n"
6993 " aaaaaaa +\n"
6994 " aaaaaaa;",
6995 getLLVMStyleWithColumns(40));
6998 TEST_F(FormatTest, ExpressionIndentation) {
6999 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7003 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7004 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7007 " ccccccccccccccccccccccccccccccccccccccccc;");
7008 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7011 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7012 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7015 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7016 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7019 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7020 verifyFormat("if () {\n"
7021 "} else if (aaaaa && bbbbb > // break\n"
7022 " ccccc) {\n"
7023 "}");
7024 verifyFormat("if () {\n"
7025 "} else if constexpr (aaaaa && bbbbb > // break\n"
7026 " ccccc) {\n"
7027 "}");
7028 verifyFormat("if () {\n"
7029 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7030 " ccccc) {\n"
7031 "}");
7032 verifyFormat("if () {\n"
7033 "} else if (aaaaa &&\n"
7034 " bbbbb > // break\n"
7035 " ccccc &&\n"
7036 " ddddd) {\n"
7037 "}");
7039 // Presence of a trailing comment used to change indentation of b.
7040 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7041 " b;\n"
7042 "return aaaaaaaaaaaaaaaaaaa +\n"
7043 " b; //",
7044 getLLVMStyleWithColumns(30));
7047 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7048 // Not sure what the best system is here. Like this, the LHS can be found
7049 // immediately above an operator (everything with the same or a higher
7050 // indent). The RHS is aligned right of the operator and so compasses
7051 // everything until something with the same indent as the operator is found.
7052 // FIXME: Is this a good system?
7053 FormatStyle Style = getLLVMStyle();
7054 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7055 verifyFormat(
7056 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7057 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7058 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7059 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7060 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7061 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7062 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7063 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7064 " > ccccccccccccccccccccccccccccccccccccccccc;",
7065 Style);
7066 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7067 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7068 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7069 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7070 Style);
7071 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7072 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7073 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7074 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7075 Style);
7076 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7077 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7078 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7079 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7080 Style);
7081 verifyFormat("if () {\n"
7082 "} else if (aaaaa\n"
7083 " && bbbbb // break\n"
7084 " > ccccc) {\n"
7085 "}",
7086 Style);
7087 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7088 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7089 Style);
7090 verifyFormat("return (a)\n"
7091 " // comment\n"
7092 " + b;",
7093 Style);
7094 verifyFormat(
7095 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7096 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7097 " + cc;",
7098 Style);
7100 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7101 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7102 Style);
7104 // Forced by comments.
7105 verifyFormat(
7106 "unsigned ContentSize =\n"
7107 " sizeof(int16_t) // DWARF ARange version number\n"
7108 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7109 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7110 " + sizeof(int8_t); // Segment Size (in bytes)");
7112 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7113 " == boost::fusion::at_c<1>(iiii).second;",
7114 Style);
7116 Style.ColumnLimit = 60;
7117 verifyFormat("zzzzzzzzzz\n"
7118 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7119 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7120 Style);
7122 Style.ColumnLimit = 80;
7123 Style.IndentWidth = 4;
7124 Style.TabWidth = 4;
7125 Style.UseTab = FormatStyle::UT_Always;
7126 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7127 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7128 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7129 "\t&& (someOtherLongishConditionPart1\n"
7130 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7131 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7132 "(someOtherLongishConditionPart1 || "
7133 "someOtherEvenLongerNestedConditionPart2);",
7134 Style);
7136 Style = getLLVMStyleWithColumns(20);
7137 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7138 Style.BinPackParameters = false;
7139 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7140 Style.ContinuationIndentWidth = 2;
7141 verifyFormat("struct Foo {\n"
7142 " Foo(\n"
7143 " int arg1,\n"
7144 " int arg2)\n"
7145 " : Base(\n"
7146 " arg1,\n"
7147 " arg2) {}\n"
7148 "};",
7149 Style);
7150 verifyFormat("return abc\n"
7151 " ? foo(\n"
7152 " a,\n"
7153 " b,\n"
7154 " bar(\n"
7155 " abc))\n"
7156 " : g(abc);",
7157 Style);
7160 TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7161 FormatStyle Style = getLLVMStyle();
7162 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7163 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7165 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7166 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7167 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7168 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7169 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7170 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7171 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7172 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7173 " > ccccccccccccccccccccccccccccccccccccccccc;",
7174 Style);
7175 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7176 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7177 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7178 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7179 Style);
7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7181 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7182 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7183 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7184 Style);
7185 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7186 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7187 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7188 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7189 Style);
7190 verifyFormat("if () {\n"
7191 "} else if (aaaaa\n"
7192 " && bbbbb // break\n"
7193 " > ccccc) {\n"
7194 "}",
7195 Style);
7196 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7197 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7198 Style);
7199 verifyFormat("return (a)\n"
7200 " // comment\n"
7201 " + b;",
7202 Style);
7203 verifyFormat(
7204 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7205 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7206 " + cc;",
7207 Style);
7208 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7209 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7210 " : 3333333333333333;",
7211 Style);
7212 verifyFormat(
7213 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7214 " : ccccccccccccccc ? dddddddddddddddddd\n"
7215 " : eeeeeeeeeeeeeeeeee)\n"
7216 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7217 " : 3333333333333333;",
7218 Style);
7219 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7220 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7221 Style);
7223 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7224 " == boost::fusion::at_c<1>(iiii).second;",
7225 Style);
7227 Style.ColumnLimit = 60;
7228 verifyFormat("zzzzzzzzzzzzz\n"
7229 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7230 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7231 Style);
7233 // Forced by comments.
7234 Style.ColumnLimit = 80;
7235 verifyFormat(
7236 "unsigned ContentSize\n"
7237 " = sizeof(int16_t) // DWARF ARange version number\n"
7238 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7239 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7240 " + sizeof(int8_t); // Segment Size (in bytes)",
7241 Style);
7243 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7244 verifyFormat(
7245 "unsigned ContentSize =\n"
7246 " sizeof(int16_t) // DWARF ARange version number\n"
7247 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7248 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7249 " + sizeof(int8_t); // Segment Size (in bytes)",
7250 Style);
7252 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7253 verifyFormat(
7254 "unsigned ContentSize =\n"
7255 " sizeof(int16_t) // DWARF ARange version number\n"
7256 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7257 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7258 " + sizeof(int8_t); // Segment Size (in bytes)",
7259 Style);
7262 TEST_F(FormatTest, EnforcedOperatorWraps) {
7263 // Here we'd like to wrap after the || operators, but a comment is forcing an
7264 // earlier wrap.
7265 verifyFormat("bool x = aaaaa //\n"
7266 " || bbbbb\n"
7267 " //\n"
7268 " || cccc;");
7271 TEST_F(FormatTest, NoOperandAlignment) {
7272 FormatStyle Style = getLLVMStyle();
7273 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7274 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7277 Style);
7278 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7279 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7281 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7282 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7283 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7284 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7285 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7286 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7287 " > ccccccccccccccccccccccccccccccccccccccccc;",
7288 Style);
7290 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7291 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7292 " + cc;",
7293 Style);
7294 verifyFormat("int a = aa\n"
7295 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7296 " * cccccccccccccccccccccccccccccccccccc;",
7297 Style);
7299 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7300 verifyFormat("return (a > b\n"
7301 " // comment1\n"
7302 " // comment2\n"
7303 " || c);",
7304 Style);
7307 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7308 FormatStyle Style = getLLVMStyle();
7309 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7310 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7312 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7313 Style);
7316 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7317 FormatStyle Style = getLLVMStyleWithColumns(40);
7318 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7319 Style.BinPackArguments = false;
7320 verifyFormat("void test() {\n"
7321 " someFunction(\n"
7322 " this + argument + is + quite\n"
7323 " + long + so + it + gets + wrapped\n"
7324 " + but + remains + bin - packed);\n"
7325 "}",
7326 Style);
7327 verifyFormat("void test() {\n"
7328 " someFunction(arg1,\n"
7329 " this + argument + is\n"
7330 " + quite + long + so\n"
7331 " + it + gets + wrapped\n"
7332 " + but + remains + bin\n"
7333 " - packed,\n"
7334 " arg3);\n"
7335 "}",
7336 Style);
7337 verifyFormat("void test() {\n"
7338 " someFunction(\n"
7339 " arg1,\n"
7340 " this + argument + has\n"
7341 " + anotherFunc(nested,\n"
7342 " calls + whose\n"
7343 " + arguments\n"
7344 " + are + also\n"
7345 " + wrapped,\n"
7346 " in + addition)\n"
7347 " + to + being + bin - packed,\n"
7348 " arg3);\n"
7349 "}",
7350 Style);
7352 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7353 verifyFormat("void test() {\n"
7354 " someFunction(\n"
7355 " arg1,\n"
7356 " this + argument + has +\n"
7357 " anotherFunc(nested,\n"
7358 " calls + whose +\n"
7359 " arguments +\n"
7360 " are + also +\n"
7361 " wrapped,\n"
7362 " in + addition) +\n"
7363 " to + being + bin - packed,\n"
7364 " arg3);\n"
7365 "}",
7366 Style);
7369 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7370 auto Style = getLLVMStyleWithColumns(45);
7371 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7372 verifyFormat("bool b =\n"
7373 " is_default_constructible_v<hash<T>> and\n"
7374 " is_copy_constructible_v<hash<T>> and\n"
7375 " is_move_constructible_v<hash<T>> and\n"
7376 " is_copy_assignable_v<hash<T>> and\n"
7377 " is_move_assignable_v<hash<T>> and\n"
7378 " is_destructible_v<hash<T>> and\n"
7379 " is_swappable_v<hash<T>> and\n"
7380 " is_callable_v<hash<T>(T)>;",
7381 Style);
7383 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7384 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7385 " and is_copy_constructible_v<hash<T>>\n"
7386 " and is_move_constructible_v<hash<T>>\n"
7387 " and is_copy_assignable_v<hash<T>>\n"
7388 " and is_move_assignable_v<hash<T>>\n"
7389 " and is_destructible_v<hash<T>>\n"
7390 " and is_swappable_v<hash<T>>\n"
7391 " and is_callable_v<hash<T>(T)>;",
7392 Style);
7394 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7395 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7396 " and is_copy_constructible_v<hash<T>>\n"
7397 " and is_move_constructible_v<hash<T>>\n"
7398 " and is_copy_assignable_v<hash<T>>\n"
7399 " and is_move_assignable_v<hash<T>>\n"
7400 " and is_destructible_v<hash<T>>\n"
7401 " and is_swappable_v<hash<T>>\n"
7402 " and is_callable_v<hash<T>(T)>;",
7403 Style);
7406 TEST_F(FormatTest, ConstructorInitializers) {
7407 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7408 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7409 getLLVMStyleWithColumns(45));
7410 verifyFormat("Constructor()\n"
7411 " : Inttializer(FitsOnTheLine) {}",
7412 getLLVMStyleWithColumns(44));
7413 verifyFormat("Constructor()\n"
7414 " : Inttializer(FitsOnTheLine) {}",
7415 getLLVMStyleWithColumns(43));
7417 verifyFormat("template <typename T>\n"
7418 "Constructor() : Initializer(FitsOnTheLine) {}",
7419 getLLVMStyleWithColumns(45));
7421 verifyFormat(
7422 "SomeClass::Constructor()\n"
7423 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7425 verifyFormat(
7426 "SomeClass::Constructor()\n"
7427 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7428 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7429 verifyFormat(
7430 "SomeClass::Constructor()\n"
7431 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7432 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7433 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7434 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7435 " : aaaaaaaaaa(aaaaaa) {}");
7437 verifyFormat("Constructor()\n"
7438 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7439 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7440 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7441 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7443 verifyFormat("Constructor()\n"
7444 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7447 verifyFormat("Constructor(int Parameter = 0)\n"
7448 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7449 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7450 verifyFormat("Constructor()\n"
7451 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7452 "}",
7453 getLLVMStyleWithColumns(60));
7454 verifyFormat("Constructor()\n"
7455 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7456 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7458 // Here a line could be saved by splitting the second initializer onto two
7459 // lines, but that is not desirable.
7460 verifyFormat("Constructor()\n"
7461 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7462 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7463 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7465 FormatStyle OnePerLine = getLLVMStyle();
7466 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7467 verifyFormat("MyClass::MyClass()\n"
7468 " : a(a),\n"
7469 " b(b),\n"
7470 " c(c) {}",
7471 OnePerLine);
7472 verifyFormat("MyClass::MyClass()\n"
7473 " : a(a), // comment\n"
7474 " b(b),\n"
7475 " c(c) {}",
7476 OnePerLine);
7477 verifyFormat("MyClass::MyClass(int a)\n"
7478 " : b(a), // comment\n"
7479 " c(a + 1) { // lined up\n"
7480 "}",
7481 OnePerLine);
7482 verifyFormat("Constructor()\n"
7483 " : a(b, b, b) {}",
7484 OnePerLine);
7485 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7486 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7487 verifyFormat("SomeClass::Constructor()\n"
7488 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7489 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7490 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7491 OnePerLine);
7492 verifyFormat("SomeClass::Constructor()\n"
7493 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7494 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7495 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7496 OnePerLine);
7497 verifyFormat("MyClass::MyClass(int var)\n"
7498 " : some_var_(var), // 4 space indent\n"
7499 " some_other_var_(var + 1) { // lined up\n"
7500 "}",
7501 OnePerLine);
7502 verifyFormat("Constructor()\n"
7503 " : aaaaa(aaaaaa),\n"
7504 " aaaaa(aaaaaa),\n"
7505 " aaaaa(aaaaaa),\n"
7506 " aaaaa(aaaaaa),\n"
7507 " aaaaa(aaaaaa) {}",
7508 OnePerLine);
7509 verifyFormat("Constructor()\n"
7510 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7511 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7512 OnePerLine);
7513 OnePerLine.BinPackParameters = false;
7514 verifyFormat(
7515 "Constructor()\n"
7516 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7517 " aaaaaaaaaaa().aaa(),\n"
7518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7519 OnePerLine);
7520 OnePerLine.ColumnLimit = 60;
7521 verifyFormat("Constructor()\n"
7522 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7523 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7524 OnePerLine);
7526 verifyFormat("Constructor()\n"
7527 " : // Comment forcing unwanted break.\n"
7528 " aaaa(aaaa) {}",
7529 "Constructor() :\n"
7530 " // Comment forcing unwanted break.\n"
7531 " aaaa(aaaa) {}");
7534 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7535 FormatStyle Style = getLLVMStyleWithColumns(60);
7536 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7537 Style.BinPackParameters = false;
7539 for (int i = 0; i < 4; ++i) {
7540 // Test all combinations of parameters that should not have an effect.
7541 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7542 Style.AllowAllArgumentsOnNextLine = i & 2;
7544 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7545 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7546 verifyFormat("Constructor()\n"
7547 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7548 Style);
7549 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7551 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7552 verifyFormat("Constructor()\n"
7553 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7554 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7555 Style);
7556 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7558 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7559 verifyFormat("Constructor()\n"
7560 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7561 Style);
7562 verifyFormat("Constructor()\n"
7563 " : a(a), b(b) {}",
7564 Style);
7565 verifyFormat("Constructor()\n"
7566 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7567 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7568 " , cccccccccccccccccccccc(c) {}",
7569 Style);
7571 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7572 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7573 verifyFormat("Constructor()\n"
7574 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7575 Style);
7577 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7578 verifyFormat("Constructor()\n"
7579 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7580 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7581 Style);
7583 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7584 verifyFormat("Constructor()\n"
7585 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7586 Style);
7587 verifyFormat("Constructor()\n"
7588 " : a(a), b(b) {}",
7589 Style);
7590 verifyFormat("Constructor()\n"
7591 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7592 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7593 " cccccccccccccccccccccc(c) {}",
7594 Style);
7596 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7597 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7598 verifyFormat("Constructor() :\n"
7599 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7600 Style);
7602 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7603 verifyFormat("Constructor() :\n"
7604 " aaaaaaaaaaaaaaaaaa(a),\n"
7605 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7606 Style);
7608 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7609 verifyFormat("Constructor() :\n"
7610 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7611 Style);
7612 verifyFormat("Constructor() :\n"
7613 " a(a), b(b) {}",
7614 Style);
7615 verifyFormat("Constructor() :\n"
7616 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7617 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7618 " cccccccccccccccccccccc(c) {}",
7619 Style);
7622 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7623 // AllowAllConstructorInitializersOnNextLine in all
7624 // BreakConstructorInitializers modes
7625 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7626 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7627 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7628 verifyFormat("SomeClassWithALongName::Constructor(\n"
7629 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7630 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7631 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7632 Style);
7634 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7635 verifyFormat("SomeClassWithALongName::Constructor(\n"
7636 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7637 " int bbbbbbbbbbbbb,\n"
7638 " int cccccccccccccccc)\n"
7639 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7640 Style);
7642 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7643 verifyFormat("SomeClassWithALongName::Constructor(\n"
7644 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7645 " int bbbbbbbbbbbbb,\n"
7646 " int cccccccccccccccc)\n"
7647 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7648 Style);
7650 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7651 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7652 verifyFormat("SomeClassWithALongName::Constructor(\n"
7653 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7654 " int bbbbbbbbbbbbb)\n"
7655 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7656 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7657 Style);
7659 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7661 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7662 verifyFormat("SomeClassWithALongName::Constructor(\n"
7663 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7664 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7665 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7666 Style);
7668 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7669 verifyFormat("SomeClassWithALongName::Constructor(\n"
7670 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7671 " int bbbbbbbbbbbbb,\n"
7672 " int cccccccccccccccc)\n"
7673 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7674 Style);
7676 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7677 verifyFormat("SomeClassWithALongName::Constructor(\n"
7678 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7679 " int bbbbbbbbbbbbb,\n"
7680 " int cccccccccccccccc)\n"
7681 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7682 Style);
7684 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7685 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7686 verifyFormat("SomeClassWithALongName::Constructor(\n"
7687 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7688 " int bbbbbbbbbbbbb)\n"
7689 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7690 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7691 Style);
7693 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7694 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7695 verifyFormat("SomeClassWithALongName::Constructor(\n"
7696 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7697 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7698 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7699 Style);
7701 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7702 verifyFormat("SomeClassWithALongName::Constructor(\n"
7703 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7704 " int bbbbbbbbbbbbb,\n"
7705 " int cccccccccccccccc) :\n"
7706 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7707 Style);
7709 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7710 verifyFormat("SomeClassWithALongName::Constructor(\n"
7711 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7712 " int bbbbbbbbbbbbb,\n"
7713 " int cccccccccccccccc) :\n"
7714 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7715 Style);
7717 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7718 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7719 verifyFormat("SomeClassWithALongName::Constructor(\n"
7720 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7721 " int bbbbbbbbbbbbb) :\n"
7722 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7723 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7724 Style);
7726 Style = getLLVMStyleWithColumns(0);
7727 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7728 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7729 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7730 " : bar(bar), baz(baz) {}",
7731 Style);
7734 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7735 FormatStyle Style = getLLVMStyleWithColumns(60);
7736 Style.BinPackArguments = false;
7737 for (int i = 0; i < 4; ++i) {
7738 // Test all combinations of parameters that should not have an effect.
7739 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7740 Style.PackConstructorInitializers =
7741 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7743 Style.AllowAllArgumentsOnNextLine = true;
7744 verifyFormat("void foo() {\n"
7745 " FunctionCallWithReallyLongName(\n"
7746 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7747 "}",
7748 Style);
7749 Style.AllowAllArgumentsOnNextLine = false;
7750 verifyFormat("void foo() {\n"
7751 " FunctionCallWithReallyLongName(\n"
7752 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7753 " bbbbbbbbbbbb);\n"
7754 "}",
7755 Style);
7757 Style.AllowAllArgumentsOnNextLine = true;
7758 verifyFormat("void foo() {\n"
7759 " auto VariableWithReallyLongName = {\n"
7760 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7761 "}",
7762 Style);
7763 Style.AllowAllArgumentsOnNextLine = false;
7764 verifyFormat("void foo() {\n"
7765 " auto VariableWithReallyLongName = {\n"
7766 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7767 " bbbbbbbbbbbb};\n"
7768 "}",
7769 Style);
7772 // This parameter should not affect declarations.
7773 Style.BinPackParameters = false;
7774 Style.AllowAllArgumentsOnNextLine = false;
7775 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7776 verifyFormat("void FunctionCallWithReallyLongName(\n"
7777 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7778 Style);
7779 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7780 verifyFormat("void FunctionCallWithReallyLongName(\n"
7781 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7782 " int bbbbbbbbbbbb);",
7783 Style);
7786 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7787 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7788 // and BAS_Align.
7789 FormatStyle Style = getLLVMStyleWithColumns(35);
7790 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7791 "void functionDecl(int A, int B, int C);";
7792 Style.AllowAllArgumentsOnNextLine = false;
7793 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7794 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7795 " paramC);\n"
7796 "void functionDecl(int A, int B,\n"
7797 " int C);"),
7798 Input, Style);
7799 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7800 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7801 " paramC);\n"
7802 "void functionDecl(int A, int B,\n"
7803 " int C);"),
7804 Input, Style);
7805 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7806 // AllowAllArgumentsOnNextLine.
7807 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7808 verifyFormat(StringRef("functionCall(\n"
7809 " paramA, paramB, paramC);\n"
7810 "void functionDecl(\n"
7811 " int A, int B, int C);"),
7812 Input, Style);
7813 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
7814 verifyFormat("functionCall(\n"
7815 " paramA, paramB, paramC\n"
7816 ");\n"
7817 "void functionDecl(\n"
7818 " int A, int B, int C\n"
7819 ");",
7820 Input, Style);
7822 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
7823 // first argument.
7824 Style.AllowAllArgumentsOnNextLine = true;
7825 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7826 verifyFormat(StringRef("functionCall(\n"
7827 " paramA, paramB, paramC);\n"
7828 "void functionDecl(\n"
7829 " int A, int B, int C);"),
7830 Input, Style);
7831 // It wouldn't fit on one line with aligned parameters so this setting
7832 // doesn't change anything for BAS_Align.
7833 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7834 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7835 " paramC);\n"
7836 "void functionDecl(int A, int B,\n"
7837 " int C);"),
7838 Input, Style);
7839 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7840 verifyFormat(StringRef("functionCall(\n"
7841 " paramA, paramB, paramC);\n"
7842 "void functionDecl(\n"
7843 " int A, int B, int C);"),
7844 Input, Style);
7847 TEST_F(FormatTest, BreakBeforeInlineASMColon) {
7848 FormatStyle Style = getLLVMStyle();
7849 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
7850 /* Test the behaviour with long lines */
7851 Style.ColumnLimit = 40;
7852 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7853 " : : val);",
7854 Style);
7855 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7856 " : val1 : val2);",
7857 Style);
7858 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7859 " \"cpuid\\n\\t\"\n"
7860 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
7861 " : \"=a\" : \"a\");",
7862 Style);
7863 Style.ColumnLimit = 80;
7864 verifyFormat("asm volatile(\"string\", : : val);", Style);
7865 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
7867 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
7868 verifyFormat("asm volatile(\"string\",\n"
7869 " :\n"
7870 " : val);",
7871 Style);
7872 verifyFormat("asm volatile(\"string\",\n"
7873 " : val1\n"
7874 " : val2);",
7875 Style);
7876 /* Test the behaviour with long lines */
7877 Style.ColumnLimit = 40;
7878 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7879 " \"cpuid\\n\\t\"\n"
7880 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
7881 " : \"=a\"(*rEAX)\n"
7882 " : \"a\"(value));",
7883 Style);
7884 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7885 " \"cpuid\\n\\t\"\n"
7886 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
7887 " :\n"
7888 " : \"a\"(value));",
7889 Style);
7890 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7891 " :\n"
7892 " : val);",
7893 Style);
7894 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7895 " : val1\n"
7896 " : val2);",
7897 Style);
7900 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
7901 FormatStyle Style = getLLVMStyle();
7902 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7904 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7905 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
7906 getStyleWithColumns(Style, 45));
7907 verifyFormat("Constructor() :\n"
7908 " Initializer(FitsOnTheLine) {}",
7909 getStyleWithColumns(Style, 44));
7910 verifyFormat("Constructor() :\n"
7911 " Initializer(FitsOnTheLine) {}",
7912 getStyleWithColumns(Style, 43));
7914 verifyFormat("template <typename T>\n"
7915 "Constructor() : Initializer(FitsOnTheLine) {}",
7916 getStyleWithColumns(Style, 50));
7917 verifyFormat(
7918 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
7919 " int mooooooooooooore) noexcept :\n"
7920 " Super{some, arguments}, Member{5}, Member2{2} {}",
7921 Style);
7922 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7923 verifyFormat(
7924 "SomeClass::Constructor() :\n"
7925 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7926 Style);
7927 verifyFormat(
7928 "SomeClass::Constructor() : // NOLINT\n"
7929 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7930 Style);
7932 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7933 verifyFormat(
7934 "SomeClass::Constructor() :\n"
7935 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7936 Style);
7937 verifyFormat(
7938 "SomeClass::Constructor() : // NOLINT\n"
7939 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7940 Style);
7942 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
7943 verifyFormat(
7944 "SomeClass::Constructor() :\n"
7945 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7946 Style);
7948 verifyFormat(
7949 "SomeClass::Constructor() :\n"
7950 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7951 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7952 Style);
7953 verifyFormat(
7954 "SomeClass::Constructor() :\n"
7955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7956 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7957 Style);
7958 verifyFormat(
7959 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7960 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
7961 Style);
7963 verifyFormat("Constructor() :\n"
7964 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7965 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7966 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7967 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
7968 Style);
7970 verifyFormat("Constructor() :\n"
7971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7972 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7973 Style);
7975 verifyFormat("Constructor(int Parameter = 0) :\n"
7976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7977 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
7978 Style);
7979 verifyFormat("Constructor() :\n"
7980 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7981 "}",
7982 getStyleWithColumns(Style, 60));
7983 verifyFormat("Constructor() :\n"
7984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7985 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
7986 Style);
7988 // Here a line could be saved by splitting the second initializer onto two
7989 // lines, but that is not desirable.
7990 verifyFormat("Constructor() :\n"
7991 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7992 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7993 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7994 Style);
7996 FormatStyle OnePerLine = Style;
7997 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7998 verifyFormat("SomeClass::Constructor() :\n"
7999 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8000 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8001 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8002 OnePerLine);
8003 verifyFormat("SomeClass::Constructor() :\n"
8004 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8005 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8006 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8007 OnePerLine);
8008 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8009 " i(i), // comment\n"
8010 " j(j) {}",
8011 OnePerLine);
8012 verifyFormat("MyClass::MyClass(int var) :\n"
8013 " some_var_(var), // 4 space indent\n"
8014 " some_other_var_(var + 1) { // lined up\n"
8015 "}",
8016 OnePerLine);
8017 verifyFormat("Constructor() :\n"
8018 " aaaaa(aaaaaa),\n"
8019 " aaaaa(aaaaaa),\n"
8020 " aaaaa(aaaaaa),\n"
8021 " aaaaa(aaaaaa),\n"
8022 " aaaaa(aaaaaa) {}",
8023 OnePerLine);
8024 verifyFormat("Constructor() :\n"
8025 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8026 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8027 OnePerLine);
8028 OnePerLine.BinPackParameters = false;
8029 verifyFormat("Constructor() :\n"
8030 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8031 " aaaaaaaaaaa().aaa(),\n"
8032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8033 OnePerLine);
8034 OnePerLine.ColumnLimit = 60;
8035 verifyFormat("Constructor() :\n"
8036 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8037 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8038 OnePerLine);
8040 verifyFormat("Constructor() :\n"
8041 " // Comment forcing unwanted break.\n"
8042 " aaaa(aaaa) {}",
8043 Style);
8044 verifyFormat("Constructor() : // NOLINT\n"
8045 " aaaa(aaaa) {}",
8046 Style);
8047 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8048 " on a single\n"
8049 " // line.\n"
8050 " aaaa(aaaa) {}",
8051 "Constructor() : // A very long trailing comment that cannot fit"
8052 " on a single line.\n"
8053 " aaaa(aaaa) {}",
8054 Style);
8056 Style.ColumnLimit = 0;
8057 verifyFormat("SomeClass::Constructor() :\n"
8058 " a(a) {}",
8059 Style);
8060 verifyFormat("SomeClass::Constructor() noexcept :\n"
8061 " a(a) {}",
8062 Style);
8063 verifyFormat("SomeClass::Constructor() :\n"
8064 " a(a), b(b), c(c) {}",
8065 Style);
8066 verifyFormat("SomeClass::Constructor() :\n"
8067 " a(a) {\n"
8068 " foo();\n"
8069 " bar();\n"
8070 "}",
8071 Style);
8073 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8074 verifyFormat("SomeClass::Constructor() :\n"
8075 " a(a), b(b), c(c) {\n"
8076 "}",
8077 Style);
8078 verifyFormat("SomeClass::Constructor() :\n"
8079 " a(a) {\n"
8080 "}",
8081 Style);
8083 Style.ColumnLimit = 80;
8084 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8085 Style.ConstructorInitializerIndentWidth = 2;
8086 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8087 verifyFormat("SomeClass::Constructor() :\n"
8088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8089 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8090 Style);
8092 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8093 // well
8094 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8095 verifyFormat(
8096 "class SomeClass\n"
8097 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8098 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8099 Style);
8100 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8101 verifyFormat(
8102 "class SomeClass\n"
8103 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8104 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8105 Style);
8106 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8107 verifyFormat(
8108 "class SomeClass :\n"
8109 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8110 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8111 Style);
8112 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8113 verifyFormat(
8114 "class SomeClass\n"
8115 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8116 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8117 Style);
8120 #ifndef EXPENSIVE_CHECKS
8121 // Expensive checks enables libstdc++ checking which includes validating the
8122 // state of ranges used in std::priority_queue - this blows out the
8123 // runtime/scalability of the function and makes this test unacceptably slow.
8124 TEST_F(FormatTest, MemoizationTests) {
8125 // This breaks if the memoization lookup does not take \c Indent and
8126 // \c LastSpace into account.
8127 verifyFormat(
8128 "extern CFRunLoopTimerRef\n"
8129 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8130 " CFTimeInterval interval, CFOptionFlags flags,\n"
8131 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8132 " CFRunLoopTimerContext *context) {}");
8134 // Deep nesting somewhat works around our memoization.
8135 verifyFormat(
8136 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8137 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8138 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8139 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8140 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8141 getLLVMStyleWithColumns(65));
8142 verifyFormat(
8143 "aaaaa(\n"
8144 " aaaaa,\n"
8145 " aaaaa(\n"
8146 " aaaaa,\n"
8147 " aaaaa(\n"
8148 " aaaaa,\n"
8149 " aaaaa(\n"
8150 " aaaaa,\n"
8151 " aaaaa(\n"
8152 " aaaaa,\n"
8153 " aaaaa(\n"
8154 " aaaaa,\n"
8155 " aaaaa(\n"
8156 " aaaaa,\n"
8157 " aaaaa(\n"
8158 " aaaaa,\n"
8159 " aaaaa(\n"
8160 " aaaaa,\n"
8161 " aaaaa(\n"
8162 " aaaaa,\n"
8163 " aaaaa(\n"
8164 " aaaaa,\n"
8165 " aaaaa(\n"
8166 " aaaaa,\n"
8167 " aaaaa))))))))))));",
8168 getLLVMStyleWithColumns(65));
8169 verifyFormat(
8170 "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"
8171 " a),\n"
8172 " a),\n"
8173 " a),\n"
8174 " a),\n"
8175 " a),\n"
8176 " a),\n"
8177 " a),\n"
8178 " a),\n"
8179 " a),\n"
8180 " a),\n"
8181 " a),\n"
8182 " a),\n"
8183 " a),\n"
8184 " a),\n"
8185 " a),\n"
8186 " a),\n"
8187 " a)",
8188 getLLVMStyleWithColumns(65));
8190 // This test takes VERY long when memoization is broken.
8191 FormatStyle OnePerLine = getLLVMStyle();
8192 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8193 OnePerLine.BinPackParameters = false;
8194 std::string input = "Constructor()\n"
8195 " : aaaa(a,\n";
8196 for (unsigned i = 0, e = 80; i != e; ++i)
8197 input += " a,\n";
8198 input += " a) {}";
8199 verifyFormat(input, OnePerLine);
8200 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8201 verifyFormat(input, OnePerLine);
8203 #endif
8205 TEST_F(FormatTest, BreaksAsHighAsPossible) {
8206 verifyFormat(
8207 "void f() {\n"
8208 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8209 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8210 " f();\n"
8211 "}");
8212 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8213 " Intervals[i - 1].getRange().getLast()) {\n}");
8216 TEST_F(FormatTest, BreaksFunctionDeclarations) {
8217 // Principially, we break function declarations in a certain order:
8218 // 1) break amongst arguments.
8219 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8220 " Cccccccccccccc cccccccccccccc);");
8221 verifyFormat("template <class TemplateIt>\n"
8222 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8223 " TemplateIt *stop) {}");
8225 // 2) break after return type.
8226 verifyGoogleFormat(
8227 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8228 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8230 // 3) break after (.
8231 verifyGoogleFormat(
8232 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8233 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8235 // 4) break before after nested name specifiers.
8236 verifyGoogleFormat(
8237 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8238 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8239 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8241 // However, there are exceptions, if a sufficient amount of lines can be
8242 // saved.
8243 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8244 // more adjusting.
8245 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8246 " Cccccccccccccc cccccccccc,\n"
8247 " Cccccccccccccc cccccccccc,\n"
8248 " Cccccccccccccc cccccccccc,\n"
8249 " Cccccccccccccc cccccccccc);");
8250 verifyGoogleFormat(
8251 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8252 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8253 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8254 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8255 verifyFormat(
8256 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8257 " Cccccccccccccc cccccccccc,\n"
8258 " Cccccccccccccc cccccccccc,\n"
8259 " Cccccccccccccc cccccccccc,\n"
8260 " Cccccccccccccc cccccccccc,\n"
8261 " Cccccccccccccc cccccccccc,\n"
8262 " Cccccccccccccc cccccccccc);");
8263 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8264 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8265 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8266 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8267 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8269 // Break after multi-line parameters.
8270 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8273 " bbbb bbbb);");
8274 verifyFormat("void SomeLoooooooooooongFunction(\n"
8275 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8276 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8277 " int bbbbbbbbbbbbb);");
8279 // Treat overloaded operators like other functions.
8280 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8281 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8282 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8283 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8284 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8285 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8286 verifyGoogleFormat(
8287 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8288 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8289 verifyGoogleFormat(
8290 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8291 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8292 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8293 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8294 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8295 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8296 verifyGoogleFormat(
8297 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8298 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8299 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8300 verifyGoogleFormat("template <typename T>\n"
8301 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8302 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8303 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8305 FormatStyle Style = getLLVMStyle();
8306 Style.PointerAlignment = FormatStyle::PAS_Left;
8307 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8308 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8309 Style);
8310 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8312 Style);
8315 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8316 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8317 // Prefer keeping `::` followed by `operator` together.
8318 verifyFormat("const aaaa::bbbbbbb &\n"
8319 "ccccccccc::operator++() {\n"
8320 " stuff();\n"
8321 "}",
8322 "const aaaa::bbbbbbb\n"
8323 "&ccccccccc::operator++() { stuff(); }",
8324 getLLVMStyleWithColumns(40));
8327 TEST_F(FormatTest, TrailingReturnType) {
8328 verifyFormat("auto foo() -> int;");
8329 // correct trailing return type spacing
8330 verifyFormat("auto operator->() -> int;");
8331 verifyFormat("auto operator++(int) -> int;");
8333 verifyFormat("struct S {\n"
8334 " auto bar() const -> int;\n"
8335 "};");
8336 verifyFormat("template <size_t Order, typename T>\n"
8337 "auto load_img(const std::string &filename)\n"
8338 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8339 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8340 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8341 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8342 verifyFormat("template <typename T>\n"
8343 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8344 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8346 FormatStyle Style = getLLVMStyleWithColumns(60);
8347 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8348 " auto NAME() -> int { return 42; }",
8349 Style);
8351 // Not trailing return types.
8352 verifyFormat("void f() { auto a = b->c(); }");
8353 verifyFormat("auto a = p->foo();");
8354 verifyFormat("int a = p->foo();");
8355 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8358 TEST_F(FormatTest, DeductionGuides) {
8359 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8360 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8361 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8362 verifyFormat(
8363 "template <class... T>\n"
8364 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8365 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8366 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8367 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8368 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8369 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8370 verifyFormat("template <class T> x() -> x<1>;");
8371 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8373 verifyFormat("A(const char *) -> A<string &>;");
8374 verifyFormat("A() -> A<int>;");
8376 // Ensure not deduction guides.
8377 verifyFormat("c()->f<int>();");
8378 verifyFormat("x()->foo<1>;");
8379 verifyFormat("x = p->foo<3>();");
8380 verifyFormat("x()->x<1>();");
8383 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8384 // Avoid breaking before trailing 'const' or other trailing annotations, if
8385 // they are not function-like.
8386 FormatStyle Style = getGoogleStyleWithColumns(47);
8387 verifyFormat("void someLongFunction(\n"
8388 " int someLoooooooooooooongParameter) const {\n}",
8389 getLLVMStyleWithColumns(47));
8390 verifyFormat("LoooooongReturnType\n"
8391 "someLoooooooongFunction() const {}",
8392 getLLVMStyleWithColumns(47));
8393 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8394 " const {}",
8395 Style);
8396 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8397 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8398 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8399 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8400 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8401 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8402 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8403 " aaaaaaaaaaa aaaaa) const override;");
8404 verifyGoogleFormat(
8405 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8406 " const override;");
8408 // Even if the first parameter has to be wrapped.
8409 verifyFormat("void someLongFunction(\n"
8410 " int someLongParameter) const {}",
8411 getLLVMStyleWithColumns(46));
8412 verifyFormat("void someLongFunction(\n"
8413 " int someLongParameter) const {}",
8414 Style);
8415 verifyFormat("void someLongFunction(\n"
8416 " int someLongParameter) override {}",
8417 Style);
8418 verifyFormat("void someLongFunction(\n"
8419 " int someLongParameter) OVERRIDE {}",
8420 Style);
8421 verifyFormat("void someLongFunction(\n"
8422 " int someLongParameter) final {}",
8423 Style);
8424 verifyFormat("void someLongFunction(\n"
8425 " int someLongParameter) FINAL {}",
8426 Style);
8427 verifyFormat("void someLongFunction(\n"
8428 " int parameter) const override {}",
8429 Style);
8431 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8432 verifyFormat("void someLongFunction(\n"
8433 " int someLongParameter) const\n"
8434 "{\n"
8435 "}",
8436 Style);
8438 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8439 verifyFormat("void someLongFunction(\n"
8440 " int someLongParameter) const\n"
8441 " {\n"
8442 " }",
8443 Style);
8445 // Unless these are unknown annotations.
8446 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8447 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8448 " LONG_AND_UGLY_ANNOTATION;");
8450 // Breaking before function-like trailing annotations is fine to keep them
8451 // close to their arguments.
8452 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8453 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8454 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8455 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8456 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8457 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8458 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8459 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8460 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8462 verifyFormat(
8463 "void aaaaaaaaaaaaaaaaaa()\n"
8464 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8465 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8466 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8467 " __attribute__((unused));");
8468 verifyGoogleFormat(
8469 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8470 " GUARDED_BY(aaaaaaaaaaaa);");
8471 verifyGoogleFormat(
8472 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8473 " GUARDED_BY(aaaaaaaaaaaa);");
8474 verifyGoogleFormat(
8475 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8476 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8477 verifyGoogleFormat(
8478 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8479 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8482 TEST_F(FormatTest, FunctionAnnotations) {
8483 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8484 "int OldFunction(const string &parameter) {}");
8485 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8486 "string OldFunction(const string &parameter) {}");
8487 verifyFormat("template <typename T>\n"
8488 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8489 "string OldFunction(const string &parameter) {}");
8491 // Not function annotations.
8492 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8493 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8494 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8495 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8496 verifyFormat("MACRO(abc).function() // wrap\n"
8497 " << abc;");
8498 verifyFormat("MACRO(abc)->function() // wrap\n"
8499 " << abc;");
8500 verifyFormat("MACRO(abc)::function() // wrap\n"
8501 " << abc;");
8504 TEST_F(FormatTest, BreaksDesireably) {
8505 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8506 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8507 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8508 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8510 "}");
8512 verifyFormat(
8513 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8520 verifyFormat(
8521 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8522 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8524 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8527 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8528 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8530 verifyFormat(
8531 "void f() {\n"
8532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8534 "}");
8535 verifyFormat(
8536 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8538 verifyFormat(
8539 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8541 verifyFormat(
8542 "aaaaaa(aaa,\n"
8543 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8545 " aaaa);");
8546 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8550 // Indent consistently independent of call expression and unary operator.
8551 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8552 " dddddddddddddddddddddddddddddd));");
8553 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8554 " dddddddddddddddddddddddddddddd));");
8555 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8556 " dddddddddddddddddddddddddddddd));");
8558 // This test case breaks on an incorrect memoization, i.e. an optimization not
8559 // taking into account the StopAt value.
8560 verifyFormat(
8561 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8562 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8563 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8564 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8566 verifyFormat("{\n {\n {\n"
8567 " Annotation.SpaceRequiredBefore =\n"
8568 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8569 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8570 " }\n }\n}");
8572 // Break on an outer level if there was a break on an inner level.
8573 verifyFormat("f(g(h(a, // comment\n"
8574 " b, c),\n"
8575 " d, e),\n"
8576 " x, y);",
8577 "f(g(h(a, // comment\n"
8578 " b, c), d, e), x, y);");
8580 // Prefer breaking similar line breaks.
8581 verifyFormat(
8582 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8583 " NSTrackingMouseEnteredAndExited |\n"
8584 " NSTrackingActiveAlways;");
8587 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8588 FormatStyle NoBinPacking = getGoogleStyle();
8589 NoBinPacking.BinPackParameters = false;
8590 NoBinPacking.BinPackArguments = true;
8591 verifyFormat("void f() {\n"
8592 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8594 "}",
8595 NoBinPacking);
8596 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8597 " int aaaaaaaaaaaaaaaaaaaa,\n"
8598 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8599 NoBinPacking);
8601 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8602 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8603 " vector<int> bbbbbbbbbbbbbbb);",
8604 NoBinPacking);
8605 // FIXME: This behavior difference is probably not wanted. However, currently
8606 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8607 // template arguments from BreakBeforeParameter being set because of the
8608 // one-per-line formatting.
8609 verifyFormat(
8610 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8611 " aaaaaaaaaa> aaaaaaaaaa);",
8612 NoBinPacking);
8613 verifyFormat(
8614 "void fffffffffff(\n"
8615 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8616 " aaaaaaaaaa);");
8619 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8620 FormatStyle NoBinPacking = getGoogleStyle();
8621 NoBinPacking.BinPackParameters = false;
8622 NoBinPacking.BinPackArguments = false;
8623 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8624 " aaaaaaaaaaaaaaaaaaaa,\n"
8625 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8626 NoBinPacking);
8627 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8628 " aaaaaaaaaaaaa,\n"
8629 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8630 NoBinPacking);
8631 verifyFormat(
8632 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8633 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8635 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8637 NoBinPacking);
8638 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8639 " .aaaaaaaaaaaaaaaaaa();",
8640 NoBinPacking);
8641 verifyFormat("void f() {\n"
8642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8643 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8644 "}",
8645 NoBinPacking);
8647 verifyFormat(
8648 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8649 " aaaaaaaaaaaa,\n"
8650 " aaaaaaaaaaaa);",
8651 NoBinPacking);
8652 verifyFormat(
8653 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8654 " ddddddddddddddddddddddddddddd),\n"
8655 " test);",
8656 NoBinPacking);
8658 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8659 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8660 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8661 " aaaaaaaaaaaaaaaaaa;",
8662 NoBinPacking);
8663 verifyFormat("a(\"a\"\n"
8664 " \"a\",\n"
8665 " a);");
8667 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8668 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8669 " aaaaaaaaa,\n"
8670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8671 NoBinPacking);
8672 verifyFormat(
8673 "void f() {\n"
8674 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8675 " .aaaaaaa();\n"
8676 "}",
8677 NoBinPacking);
8678 verifyFormat(
8679 "template <class SomeType, class SomeOtherType>\n"
8680 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8681 NoBinPacking);
8684 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
8685 FormatStyle Style = getLLVMStyleWithColumns(15);
8686 Style.ExperimentalAutoDetectBinPacking = true;
8687 verifyFormat("aaa(aaaa,\n"
8688 " aaaa,\n"
8689 " aaaa);\n"
8690 "aaa(aaaa,\n"
8691 " aaaa,\n"
8692 " aaaa);",
8693 "aaa(aaaa,\n" // one-per-line
8694 " aaaa,\n"
8695 " aaaa );\n"
8696 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8697 Style);
8698 verifyFormat("aaa(aaaa, aaaa,\n"
8699 " aaaa);\n"
8700 "aaa(aaaa, aaaa,\n"
8701 " aaaa);",
8702 "aaa(aaaa, aaaa,\n" // bin-packed
8703 " aaaa );\n"
8704 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8705 Style);
8708 TEST_F(FormatTest, FormatsBuilderPattern) {
8709 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
8710 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
8711 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
8712 " .StartsWith(\".init\", ORDER_INIT)\n"
8713 " .StartsWith(\".fini\", ORDER_FINI)\n"
8714 " .StartsWith(\".hash\", ORDER_HASH)\n"
8715 " .Default(ORDER_TEXT);");
8717 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
8718 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
8719 verifyFormat("aaaaaaa->aaaaaaa\n"
8720 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8722 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8723 verifyFormat(
8724 "aaaaaaa->aaaaaaa\n"
8725 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8726 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8727 verifyFormat(
8728 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
8729 " aaaaaaaaaaaaaa);");
8730 verifyFormat(
8731 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
8732 " aaaaaa->aaaaaaaaaaaa()\n"
8733 " ->aaaaaaaaaaaaaaaa(\n"
8734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8735 " ->aaaaaaaaaaaaaaaaa();");
8736 verifyGoogleFormat(
8737 "void f() {\n"
8738 " someo->Add((new util::filetools::Handler(dir))\n"
8739 " ->OnEvent1(NewPermanentCallback(\n"
8740 " this, &HandlerHolderClass::EventHandlerCBA))\n"
8741 " ->OnEvent2(NewPermanentCallback(\n"
8742 " this, &HandlerHolderClass::EventHandlerCBB))\n"
8743 " ->OnEvent3(NewPermanentCallback(\n"
8744 " this, &HandlerHolderClass::EventHandlerCBC))\n"
8745 " ->OnEvent5(NewPermanentCallback(\n"
8746 " this, &HandlerHolderClass::EventHandlerCBD))\n"
8747 " ->OnEvent6(NewPermanentCallback(\n"
8748 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
8749 "}");
8751 verifyFormat(
8752 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
8753 verifyFormat("aaaaaaaaaaaaaaa()\n"
8754 " .aaaaaaaaaaaaaaa()\n"
8755 " .aaaaaaaaaaaaaaa()\n"
8756 " .aaaaaaaaaaaaaaa()\n"
8757 " .aaaaaaaaaaaaaaa();");
8758 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8759 " .aaaaaaaaaaaaaaa()\n"
8760 " .aaaaaaaaaaaaaaa()\n"
8761 " .aaaaaaaaaaaaaaa();");
8762 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8763 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8764 " .aaaaaaaaaaaaaaa();");
8765 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
8766 " ->aaaaaaaaaaaaaae(0)\n"
8767 " ->aaaaaaaaaaaaaaa();");
8769 // Don't linewrap after very short segments.
8770 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8771 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8772 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8773 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8774 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8775 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8776 verifyFormat("aaa()\n"
8777 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8778 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8779 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8781 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8782 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8783 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
8784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8785 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
8786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
8788 // Prefer not to break after empty parentheses.
8789 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
8790 " First->LastNewlineOffset);");
8792 // Prefer not to create "hanging" indents.
8793 verifyFormat(
8794 "return !soooooooooooooome_map\n"
8795 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8796 " .second;");
8797 verifyFormat(
8798 "return aaaaaaaaaaaaaaaa\n"
8799 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
8800 " .aaaa(aaaaaaaaaaaaaa);");
8801 // No hanging indent here.
8802 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
8803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8804 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
8805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8806 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
8807 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8808 getLLVMStyleWithColumns(60));
8809 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
8810 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
8811 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8812 getLLVMStyleWithColumns(59));
8813 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8815 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8817 // Dont break if only closing statements before member call
8818 verifyFormat("test() {\n"
8819 " ([]() -> {\n"
8820 " int b = 32;\n"
8821 " return 3;\n"
8822 " }).foo();\n"
8823 "}");
8824 verifyFormat("test() {\n"
8825 " (\n"
8826 " []() -> {\n"
8827 " int b = 32;\n"
8828 " return 3;\n"
8829 " },\n"
8830 " foo, bar)\n"
8831 " .foo();\n"
8832 "}");
8833 verifyFormat("test() {\n"
8834 " ([]() -> {\n"
8835 " int b = 32;\n"
8836 " return 3;\n"
8837 " })\n"
8838 " .foo()\n"
8839 " .bar();\n"
8840 "}");
8841 verifyFormat("test() {\n"
8842 " ([]() -> {\n"
8843 " int b = 32;\n"
8844 " return 3;\n"
8845 " })\n"
8846 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
8847 " \"bbbb\");\n"
8848 "}",
8849 getLLVMStyleWithColumns(30));
8852 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
8853 verifyFormat(
8854 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8855 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
8856 verifyFormat(
8857 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
8858 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
8860 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
8861 " ccccccccccccccccccccccccc) {\n}");
8862 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
8863 " ccccccccccccccccccccccccc) {\n}");
8865 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
8866 " ccccccccccccccccccccccccc) {\n}");
8867 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
8868 " ccccccccccccccccccccccccc) {\n}");
8870 verifyFormat(
8871 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
8872 " ccccccccccccccccccccccccc) {\n}");
8873 verifyFormat(
8874 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
8875 " ccccccccccccccccccccccccc) {\n}");
8877 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
8878 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
8879 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
8880 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
8881 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
8882 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
8883 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
8884 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
8886 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
8887 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
8888 " aaaaaaaaaaaaaaa != aa) {\n}");
8889 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
8890 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
8891 " aaaaaaaaaaaaaaa != aa) {\n}");
8894 TEST_F(FormatTest, BreaksAfterAssignments) {
8895 verifyFormat(
8896 "unsigned Cost =\n"
8897 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
8898 " SI->getPointerAddressSpaceee());");
8899 verifyFormat(
8900 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
8901 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
8903 verifyFormat(
8904 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
8905 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
8906 verifyFormat("unsigned OriginalStartColumn =\n"
8907 " SourceMgr.getSpellingColumnNumber(\n"
8908 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
8909 " 1;");
8912 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
8913 FormatStyle Style = getLLVMStyle();
8914 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8915 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
8916 Style);
8918 Style.PenaltyBreakAssignment = 20;
8919 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
8920 " cccccccccccccccccccccccccc;",
8921 Style);
8924 TEST_F(FormatTest, AlignsAfterAssignments) {
8925 verifyFormat(
8926 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8927 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8928 verifyFormat(
8929 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8930 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8931 verifyFormat(
8932 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8933 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8934 verifyFormat(
8935 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8936 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
8937 verifyFormat(
8938 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
8939 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
8940 " aaaaaaaaaaaaaaaaaaaaaaaa;");
8943 TEST_F(FormatTest, AlignsAfterReturn) {
8944 verifyFormat(
8945 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8946 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8947 verifyFormat(
8948 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8949 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
8950 verifyFormat(
8951 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
8952 " aaaaaaaaaaaaaaaaaaaaaa();");
8953 verifyFormat(
8954 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
8955 " aaaaaaaaaaaaaaaaaaaaaa());");
8956 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8958 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8959 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
8960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8961 verifyFormat("return\n"
8962 " // true if code is one of a or b.\n"
8963 " code == a || code == b;");
8966 TEST_F(FormatTest, AlignsAfterOpenBracket) {
8967 verifyFormat(
8968 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
8969 " aaaaaaaaa aaaaaaa) {}");
8970 verifyFormat(
8971 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
8972 " aaaaaaaaaaa aaaaaaaaa);");
8973 verifyFormat(
8974 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
8975 " aaaaaaaaaaaaaaaaaaaaa));");
8976 FormatStyle Style = getLLVMStyle();
8977 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8978 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8979 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
8980 Style);
8981 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
8982 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
8983 Style);
8984 verifyFormat("SomeLongVariableName->someFunction(\n"
8985 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
8986 Style);
8987 verifyFormat(
8988 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
8989 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8990 Style);
8991 verifyFormat(
8992 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
8993 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8994 Style);
8995 verifyFormat(
8996 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
8997 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
8998 Style);
9000 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9001 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9002 " b));",
9003 Style);
9005 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9006 Style.BinPackArguments = false;
9007 Style.BinPackParameters = false;
9008 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9009 " aaaaaaaaaaa aaaaaaaa,\n"
9010 " aaaaaaaaa aaaaaaa,\n"
9011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9012 Style);
9013 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9014 " aaaaaaaaaaa aaaaaaaaa,\n"
9015 " aaaaaaaaaaa aaaaaaaaa,\n"
9016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9017 Style);
9018 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9019 " aaaaaaaaaaaaaaa,\n"
9020 " aaaaaaaaaaaaaaaaaaaaa,\n"
9021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9022 Style);
9023 verifyFormat(
9024 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9025 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9026 Style);
9027 verifyFormat(
9028 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9029 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9030 Style);
9031 verifyFormat(
9032 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9033 " aaaaaaaaaaaaaaaaaaaaa(\n"
9034 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9035 " aaaaaaaaaaaaaaaa);",
9036 Style);
9037 verifyFormat(
9038 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9039 " aaaaaaaaaaaaaaaaaaaaa(\n"
9040 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9041 " aaaaaaaaaaaaaaaa);",
9042 Style);
9044 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9045 Style.BinPackArguments = false;
9046 Style.BinPackParameters = false;
9047 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9048 " aaaaaaaaaaa aaaaaaaa,\n"
9049 " aaaaaaaaa aaaaaaa,\n"
9050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9051 ") {}",
9052 Style);
9053 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9054 " aaaaaaaaaaa aaaaaaaaa,\n"
9055 " aaaaaaaaaaa aaaaaaaaa,\n"
9056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9057 ");",
9058 Style);
9059 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9060 " aaaaaaaaaaaaaaa,\n"
9061 " aaaaaaaaaaaaaaaaaaaaa,\n"
9062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9063 "));",
9064 Style);
9065 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9066 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9067 "));",
9068 Style);
9069 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9070 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9071 "));",
9072 Style);
9073 verifyFormat(
9074 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9075 " aaaaaaaaaaaaaaaaaaaaa(\n"
9076 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9077 " ),\n"
9078 " aaaaaaaaaaaaaaaa\n"
9079 ");",
9080 Style);
9081 verifyFormat(
9082 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9083 " aaaaaaaaaaaaaaaaaaaaa(\n"
9084 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9085 " ) &&\n"
9086 " aaaaaaaaaaaaaaaa\n"
9087 ");",
9088 Style);
9091 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9092 FormatStyle Style = getLLVMStyleWithColumns(40);
9093 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9094 " bbbbbbbbbbbbbbbbbbbbbb);",
9095 Style);
9096 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9097 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9098 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9099 " bbbbbbbbbbbbbbbbbbbbbb);",
9100 Style);
9101 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9102 Style.AlignOperands = FormatStyle::OAS_Align;
9103 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9104 " bbbbbbbbbbbbbbbbbbbbbb);",
9105 Style);
9106 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9107 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9108 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9109 " bbbbbbbbbbbbbbbbbbbbbb);",
9110 Style);
9113 TEST_F(FormatTest, BreaksConditionalExpressions) {
9114 verifyFormat(
9115 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9116 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9117 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9118 verifyFormat(
9119 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9120 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9121 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9122 verifyFormat(
9123 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9124 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9125 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9126 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9127 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9128 verifyFormat(
9129 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9130 " : aaaaaaaaaaaaa);");
9131 verifyFormat(
9132 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9133 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9134 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9135 " aaaaaaaaaaaaa);");
9136 verifyFormat(
9137 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9138 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9139 " aaaaaaaaaaaaa);");
9140 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9141 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9143 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9145 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9147 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9149 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9151 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9152 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9154 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9156 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9157 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9158 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9159 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9160 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9162 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9163 " : aaaaaaaaaaaaaaaa;");
9164 verifyFormat(
9165 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9166 " ? aaaaaaaaaaaaaaa\n"
9167 " : aaaaaaaaaaaaaaa;");
9168 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9169 " aaaaaaaaa\n"
9170 " ? b\n"
9171 " : c);");
9172 verifyFormat("return aaaa == bbbb\n"
9173 " // comment\n"
9174 " ? aaaa\n"
9175 " : bbbb;");
9176 verifyFormat("unsigned Indent =\n"
9177 " format(TheLine.First,\n"
9178 " IndentForLevel[TheLine.Level] >= 0\n"
9179 " ? IndentForLevel[TheLine.Level]\n"
9180 " : TheLine * 2,\n"
9181 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9182 getLLVMStyleWithColumns(60));
9183 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9184 " ? aaaaaaaaaaaaaaa\n"
9185 " : bbbbbbbbbbbbbbb //\n"
9186 " ? ccccccccccccccc\n"
9187 " : ddddddddddddddd;");
9188 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9189 " ? aaaaaaaaaaaaaaa\n"
9190 " : (bbbbbbbbbbbbbbb //\n"
9191 " ? ccccccccccccccc\n"
9192 " : ddddddddddddddd);");
9193 verifyFormat(
9194 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9195 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9196 " aaaaaaaaaaaaaaaaaaaaa +\n"
9197 " aaaaaaaaaaaaaaaaaaaaa\n"
9198 " : aaaaaaaaaa;");
9199 verifyFormat(
9200 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9201 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9202 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9204 FormatStyle NoBinPacking = getLLVMStyle();
9205 NoBinPacking.BinPackArguments = false;
9206 verifyFormat(
9207 "void f() {\n"
9208 " g(aaa,\n"
9209 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9211 " ? aaaaaaaaaaaaaaa\n"
9212 " : aaaaaaaaaaaaaaa);\n"
9213 "}",
9214 NoBinPacking);
9215 verifyFormat(
9216 "void f() {\n"
9217 " g(aaa,\n"
9218 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9220 " ?: aaaaaaaaaaaaaaa);\n"
9221 "}",
9222 NoBinPacking);
9224 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9225 " // comment.\n"
9226 " ccccccccccccccccccccccccccccccccccccccc\n"
9227 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9228 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9230 // Assignments in conditional expressions. Apparently not uncommon :-(.
9231 verifyFormat("return a != b\n"
9232 " // comment\n"
9233 " ? a = b\n"
9234 " : a = b;");
9235 verifyFormat("return a != b\n"
9236 " // comment\n"
9237 " ? a = a != b\n"
9238 " // comment\n"
9239 " ? a = b\n"
9240 " : a\n"
9241 " : a;");
9242 verifyFormat("return a != b\n"
9243 " // comment\n"
9244 " ? a\n"
9245 " : a = a != b\n"
9246 " // comment\n"
9247 " ? a = b\n"
9248 " : a;");
9250 // Chained conditionals
9251 FormatStyle Style = getLLVMStyleWithColumns(70);
9252 Style.AlignOperands = FormatStyle::OAS_Align;
9253 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9254 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9255 " : 3333333333333333;",
9256 Style);
9257 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9258 " : bbbbbbbbbb ? 2222222222222222\n"
9259 " : 3333333333333333;",
9260 Style);
9261 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9262 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9263 " : 3333333333333333;",
9264 Style);
9265 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9266 " : bbbbbbbbbbbbbb ? 222222\n"
9267 " : 333333;",
9268 Style);
9269 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9270 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9271 " : cccccccccccccc ? 3333333333333333\n"
9272 " : 4444444444444444;",
9273 Style);
9274 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9275 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9276 " : 3333333333333333;",
9277 Style);
9278 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9279 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9280 " : (aaa ? bbb : ccc);",
9281 Style);
9282 verifyFormat(
9283 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9284 " : cccccccccccccccccc)\n"
9285 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9286 " : 3333333333333333;",
9287 Style);
9288 verifyFormat(
9289 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9290 " : cccccccccccccccccc)\n"
9291 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9292 " : 3333333333333333;",
9293 Style);
9294 verifyFormat(
9295 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9296 " : dddddddddddddddddd)\n"
9297 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9298 " : 3333333333333333;",
9299 Style);
9300 verifyFormat(
9301 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9302 " : dddddddddddddddddd)\n"
9303 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9304 " : 3333333333333333;",
9305 Style);
9306 verifyFormat(
9307 "return aaaaaaaaa ? 1111111111111111\n"
9308 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9309 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9310 " : dddddddddddddddddd)",
9311 Style);
9312 verifyFormat(
9313 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9314 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9315 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9316 " : cccccccccccccccccc);",
9317 Style);
9318 verifyFormat(
9319 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9320 " : ccccccccccccccc ? dddddddddddddddddd\n"
9321 " : eeeeeeeeeeeeeeeeee)\n"
9322 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9323 " : 3333333333333333;",
9324 Style);
9325 verifyFormat(
9326 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9327 " : ccccccccccccccc ? dddddddddddddddddd\n"
9328 " : eeeeeeeeeeeeeeeeee)\n"
9329 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9330 " : 3333333333333333;",
9331 Style);
9332 verifyFormat(
9333 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9334 " : cccccccccccc ? dddddddddddddddddd\n"
9335 " : eeeeeeeeeeeeeeeeee)\n"
9336 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9337 " : 3333333333333333;",
9338 Style);
9339 verifyFormat(
9340 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9341 " : cccccccccccccccccc\n"
9342 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9343 " : 3333333333333333;",
9344 Style);
9345 verifyFormat(
9346 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9347 " : cccccccccccccccc ? dddddddddddddddddd\n"
9348 " : eeeeeeeeeeeeeeeeee\n"
9349 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9350 " : 3333333333333333;",
9351 Style);
9352 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9353 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9354 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9355 " : eeeeeeeeeeeeeeeeee)\n"
9356 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9357 " : 3333333333333333;",
9358 Style);
9359 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9360 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9361 " : cccccccccccccccc ? dddddddddddddddddd\n"
9362 " : eeeeeeeeeeeeeeeeee\n"
9363 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9364 " : 3333333333333333;",
9365 Style);
9367 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9368 Style.BreakBeforeTernaryOperators = false;
9369 // FIXME: Aligning the question marks is weird given DontAlign.
9370 // Consider disabling this alignment in this case. Also check whether this
9371 // will render the adjustment from https://reviews.llvm.org/D82199
9372 // unnecessary.
9373 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9374 " bbbb ? cccccccccccccccccc :\n"
9375 " ddddd;",
9376 Style);
9378 verifyFormat(
9379 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9380 " /*\n"
9381 " */\n"
9382 " function() {\n"
9383 " try {\n"
9384 " return JJJJJJJJJJJJJJ(\n"
9385 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9386 " }\n"
9387 " } :\n"
9388 " function() {};",
9389 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9390 " /*\n"
9391 " */\n"
9392 " function() {\n"
9393 " try {\n"
9394 " return JJJJJJJJJJJJJJ(\n"
9395 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9396 " }\n"
9397 " } :\n"
9398 " function() {};",
9399 getGoogleStyle(FormatStyle::LK_JavaScript));
9402 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9403 FormatStyle Style = getLLVMStyleWithColumns(70);
9404 Style.BreakBeforeTernaryOperators = false;
9405 verifyFormat(
9406 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9409 Style);
9410 verifyFormat(
9411 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9412 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9414 Style);
9415 verifyFormat(
9416 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9418 Style);
9419 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9420 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9422 Style);
9423 verifyFormat(
9424 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9425 " aaaaaaaaaaaaa);",
9426 Style);
9427 verifyFormat(
9428 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9429 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9431 " aaaaaaaaaaaaa);",
9432 Style);
9433 verifyFormat(
9434 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9435 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9436 " aaaaaaaaaaaaa);",
9437 Style);
9438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9443 Style);
9444 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9450 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9451 Style);
9452 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9456 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9457 Style);
9458 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9459 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9460 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9461 Style);
9462 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9466 Style);
9467 verifyFormat(
9468 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9469 " aaaaaaaaaaaaaaa :\n"
9470 " aaaaaaaaaaaaaaa;",
9471 Style);
9472 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9473 " aaaaaaaaa ?\n"
9474 " b :\n"
9475 " c);",
9476 Style);
9477 verifyFormat("unsigned Indent =\n"
9478 " format(TheLine.First,\n"
9479 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9480 " IndentForLevel[TheLine.Level] :\n"
9481 " TheLine * 2,\n"
9482 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9483 Style);
9484 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9485 " aaaaaaaaaaaaaaa :\n"
9486 " bbbbbbbbbbbbbbb ? //\n"
9487 " ccccccccccccccc :\n"
9488 " ddddddddddddddd;",
9489 Style);
9490 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9491 " aaaaaaaaaaaaaaa :\n"
9492 " (bbbbbbbbbbbbbbb ? //\n"
9493 " ccccccccccccccc :\n"
9494 " ddddddddddddddd);",
9495 Style);
9496 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9497 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9498 " ccccccccccccccccccccccccccc;",
9499 Style);
9500 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9501 " aaaaa :\n"
9502 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9503 Style);
9505 // Chained conditionals
9506 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9507 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9508 " 3333333333333333;",
9509 Style);
9510 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9511 " bbbbbbbbbb ? 2222222222222222 :\n"
9512 " 3333333333333333;",
9513 Style);
9514 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9515 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9516 " 3333333333333333;",
9517 Style);
9518 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9519 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9520 " 333333;",
9521 Style);
9522 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9523 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9524 " cccccccccccccccc ? 3333333333333333 :\n"
9525 " 4444444444444444;",
9526 Style);
9527 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9528 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9529 " 3333333333333333;",
9530 Style);
9531 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9532 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9533 " (aaa ? bbb : ccc);",
9534 Style);
9535 verifyFormat(
9536 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9537 " cccccccccccccccccc) :\n"
9538 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9539 " 3333333333333333;",
9540 Style);
9541 verifyFormat(
9542 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9543 " cccccccccccccccccc) :\n"
9544 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9545 " 3333333333333333;",
9546 Style);
9547 verifyFormat(
9548 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9549 " dddddddddddddddddd) :\n"
9550 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9551 " 3333333333333333;",
9552 Style);
9553 verifyFormat(
9554 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9555 " dddddddddddddddddd) :\n"
9556 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9557 " 3333333333333333;",
9558 Style);
9559 verifyFormat(
9560 "return aaaaaaaaa ? 1111111111111111 :\n"
9561 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9562 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9563 " dddddddddddddddddd)",
9564 Style);
9565 verifyFormat(
9566 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9567 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9568 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9569 " cccccccccccccccccc);",
9570 Style);
9571 verifyFormat(
9572 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9573 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9574 " eeeeeeeeeeeeeeeeee) :\n"
9575 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9576 " 3333333333333333;",
9577 Style);
9578 verifyFormat(
9579 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9580 " ccccccccccccc ? dddddddddddddddddd :\n"
9581 " eeeeeeeeeeeeeeeeee) :\n"
9582 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9583 " 3333333333333333;",
9584 Style);
9585 verifyFormat(
9586 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9587 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9588 " eeeeeeeeeeeeeeeeee) :\n"
9589 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9590 " 3333333333333333;",
9591 Style);
9592 verifyFormat(
9593 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9594 " cccccccccccccccccc :\n"
9595 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9596 " 3333333333333333;",
9597 Style);
9598 verifyFormat(
9599 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9600 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9601 " eeeeeeeeeeeeeeeeee :\n"
9602 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9603 " 3333333333333333;",
9604 Style);
9605 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9606 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9607 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9608 " eeeeeeeeeeeeeeeeee) :\n"
9609 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9610 " 3333333333333333;",
9611 Style);
9612 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9613 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9614 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
9615 " eeeeeeeeeeeeeeeeee :\n"
9616 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9617 " 3333333333333333;",
9618 Style);
9621 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
9622 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
9623 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
9624 verifyFormat("bool a = true, b = false;");
9626 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
9628 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
9629 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
9630 verifyFormat(
9631 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
9632 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
9633 " d = e && f;");
9634 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
9635 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
9636 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9637 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
9638 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
9639 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
9641 FormatStyle Style = getGoogleStyle();
9642 Style.PointerAlignment = FormatStyle::PAS_Left;
9643 Style.DerivePointerAlignment = false;
9644 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9645 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
9646 " *b = bbbbbbbbbbbbbbbbbbb;",
9647 Style);
9648 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9649 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
9650 Style);
9651 verifyFormat("vector<int*> a, b;", Style);
9652 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
9653 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
9654 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
9655 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
9656 Style);
9657 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
9658 Style);
9659 verifyFormat(
9660 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
9661 Style);
9663 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
9664 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
9665 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
9666 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
9667 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
9668 Style);
9671 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
9672 verifyFormat("arr[foo ? bar : baz];");
9673 verifyFormat("f()[foo ? bar : baz];");
9674 verifyFormat("(a + b)[foo ? bar : baz];");
9675 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
9678 TEST_F(FormatTest, AlignsStringLiterals) {
9679 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
9680 " \"short literal\");");
9681 verifyFormat(
9682 "looooooooooooooooooooooooongFunction(\n"
9683 " \"short literal\"\n"
9684 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
9685 verifyFormat("someFunction(\"Always break between multi-line\"\n"
9686 " \" string literals\",\n"
9687 " also, other, parameters);");
9688 verifyFormat("fun + \"1243\" /* comment */\n"
9689 " \"5678\";",
9690 "fun + \"1243\" /* comment */\n"
9691 " \"5678\";",
9692 getLLVMStyleWithColumns(28));
9693 verifyFormat(
9694 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
9695 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
9696 " \"aaaaaaaaaaaaaaaa\";",
9697 "aaaaaa ="
9698 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
9699 "aaaaaaaaaaaaaaaaaaaaa\" "
9700 "\"aaaaaaaaaaaaaaaa\";");
9701 verifyFormat("a = a + \"a\"\n"
9702 " \"a\"\n"
9703 " \"a\";");
9704 verifyFormat("f(\"a\", \"b\"\n"
9705 " \"c\");");
9707 verifyFormat(
9708 "#define LL_FORMAT \"ll\"\n"
9709 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
9710 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
9712 verifyFormat("#define A(X) \\\n"
9713 " \"aaaaa\" #X \"bbbbbb\" \\\n"
9714 " \"ccccc\"",
9715 getLLVMStyleWithColumns(23));
9716 verifyFormat("#define A \"def\"\n"
9717 "f(\"abc\" A \"ghi\"\n"
9718 " \"jkl\");");
9720 verifyFormat("f(L\"a\"\n"
9721 " L\"b\");");
9722 verifyFormat("#define A(X) \\\n"
9723 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
9724 " L\"ccccc\"",
9725 getLLVMStyleWithColumns(25));
9727 verifyFormat("f(@\"a\"\n"
9728 " @\"b\");");
9729 verifyFormat("NSString s = @\"a\"\n"
9730 " @\"b\"\n"
9731 " @\"c\";");
9732 verifyFormat("NSString s = @\"a\"\n"
9733 " \"b\"\n"
9734 " \"c\";");
9737 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
9738 FormatStyle Style = getLLVMStyle();
9739 // No declarations or definitions should be moved to own line.
9740 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
9741 verifyFormat("class A {\n"
9742 " int f() { return 1; }\n"
9743 " int g();\n"
9744 "};\n"
9745 "int f() { return 1; }\n"
9746 "int g();",
9747 Style);
9749 // All declarations and definitions should have the return type moved to its
9750 // own line.
9751 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9752 Style.TypenameMacros = {"LIST"};
9753 verifyFormat("SomeType\n"
9754 "funcdecl(LIST(uint64_t));",
9755 Style);
9756 verifyFormat("class E {\n"
9757 " int\n"
9758 " f() {\n"
9759 " return 1;\n"
9760 " }\n"
9761 " int\n"
9762 " g();\n"
9763 "};\n"
9764 "int\n"
9765 "f() {\n"
9766 " return 1;\n"
9767 "}\n"
9768 "int\n"
9769 "g();",
9770 Style);
9772 // Top-level definitions, and no kinds of declarations should have the
9773 // return type moved to its own line.
9774 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
9775 verifyFormat("class B {\n"
9776 " int f() { return 1; }\n"
9777 " int g();\n"
9778 "};\n"
9779 "int\n"
9780 "f() {\n"
9781 " return 1;\n"
9782 "}\n"
9783 "int g();",
9784 Style);
9786 // Top-level definitions and declarations should have the return type moved
9787 // to its own line.
9788 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
9789 verifyFormat("class C {\n"
9790 " int f() { return 1; }\n"
9791 " int g();\n"
9792 "};\n"
9793 "int\n"
9794 "f() {\n"
9795 " return 1;\n"
9796 "}\n"
9797 "int\n"
9798 "g();",
9799 Style);
9801 // All definitions should have the return type moved to its own line, but no
9802 // kinds of declarations.
9803 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
9804 verifyFormat("class D {\n"
9805 " int\n"
9806 " f() {\n"
9807 " return 1;\n"
9808 " }\n"
9809 " int g();\n"
9810 "};\n"
9811 "int\n"
9812 "f() {\n"
9813 " return 1;\n"
9814 "}\n"
9815 "int g();",
9816 Style);
9817 verifyFormat("const char *\n"
9818 "f(void) {\n" // Break here.
9819 " return \"\";\n"
9820 "}\n"
9821 "const char *bar(void);", // No break here.
9822 Style);
9823 verifyFormat("template <class T>\n"
9824 "T *\n"
9825 "f(T &c) {\n" // Break here.
9826 " return NULL;\n"
9827 "}\n"
9828 "template <class T> T *f(T &c);", // No break here.
9829 Style);
9830 verifyFormat("class C {\n"
9831 " int\n"
9832 " operator+() {\n"
9833 " return 1;\n"
9834 " }\n"
9835 " int\n"
9836 " operator()() {\n"
9837 " return 1;\n"
9838 " }\n"
9839 "};",
9840 Style);
9841 verifyFormat("void\n"
9842 "A::operator()() {}\n"
9843 "void\n"
9844 "A::operator>>() {}\n"
9845 "void\n"
9846 "A::operator+() {}\n"
9847 "void\n"
9848 "A::operator*() {}\n"
9849 "void\n"
9850 "A::operator->() {}\n"
9851 "void\n"
9852 "A::operator void *() {}\n"
9853 "void\n"
9854 "A::operator void &() {}\n"
9855 "void\n"
9856 "A::operator void &&() {}\n"
9857 "void\n"
9858 "A::operator char *() {}\n"
9859 "void\n"
9860 "A::operator[]() {}\n"
9861 "void\n"
9862 "A::operator!() {}\n"
9863 "void\n"
9864 "A::operator**() {}\n"
9865 "void\n"
9866 "A::operator<Foo> *() {}\n"
9867 "void\n"
9868 "A::operator<Foo> **() {}\n"
9869 "void\n"
9870 "A::operator<Foo> &() {}\n"
9871 "void\n"
9872 "A::operator void **() {}",
9873 Style);
9874 verifyFormat("constexpr auto\n"
9875 "operator()() const -> reference {}\n"
9876 "constexpr auto\n"
9877 "operator>>() const -> reference {}\n"
9878 "constexpr auto\n"
9879 "operator+() const -> reference {}\n"
9880 "constexpr auto\n"
9881 "operator*() const -> reference {}\n"
9882 "constexpr auto\n"
9883 "operator->() const -> reference {}\n"
9884 "constexpr auto\n"
9885 "operator++() const -> reference {}\n"
9886 "constexpr auto\n"
9887 "operator void *() const -> reference {}\n"
9888 "constexpr auto\n"
9889 "operator void **() const -> reference {}\n"
9890 "constexpr auto\n"
9891 "operator void *() const -> reference {}\n"
9892 "constexpr auto\n"
9893 "operator void &() const -> reference {}\n"
9894 "constexpr auto\n"
9895 "operator void &&() const -> reference {}\n"
9896 "constexpr auto\n"
9897 "operator char *() const -> reference {}\n"
9898 "constexpr auto\n"
9899 "operator!() const -> reference {}\n"
9900 "constexpr auto\n"
9901 "operator[]() const -> reference {}",
9902 Style);
9903 verifyFormat("void *operator new(std::size_t s);", // No break here.
9904 Style);
9905 verifyFormat("void *\n"
9906 "operator new(std::size_t s) {}",
9907 Style);
9908 verifyFormat("void *\n"
9909 "operator delete[](void *ptr) {}",
9910 Style);
9911 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9912 verifyFormat("const char *\n"
9913 "f(void)\n" // Break here.
9914 "{\n"
9915 " return \"\";\n"
9916 "}\n"
9917 "const char *bar(void);", // No break here.
9918 Style);
9919 verifyFormat("template <class T>\n"
9920 "T *\n" // Problem here: no line break
9921 "f(T &c)\n" // Break here.
9922 "{\n"
9923 " return NULL;\n"
9924 "}\n"
9925 "template <class T> T *f(T &c);", // No break here.
9926 Style);
9927 verifyFormat("int\n"
9928 "foo(A<bool> a)\n"
9929 "{\n"
9930 " return a;\n"
9931 "}",
9932 Style);
9933 verifyFormat("int\n"
9934 "foo(A<8> a)\n"
9935 "{\n"
9936 " return a;\n"
9937 "}",
9938 Style);
9939 verifyFormat("int\n"
9940 "foo(A<B<bool>, 8> a)\n"
9941 "{\n"
9942 " return a;\n"
9943 "}",
9944 Style);
9945 verifyFormat("int\n"
9946 "foo(A<B<8>, bool> a)\n"
9947 "{\n"
9948 " return a;\n"
9949 "}",
9950 Style);
9951 verifyFormat("int\n"
9952 "foo(A<B<bool>, bool> a)\n"
9953 "{\n"
9954 " return a;\n"
9955 "}",
9956 Style);
9957 verifyFormat("int\n"
9958 "foo(A<B<8>, 8> a)\n"
9959 "{\n"
9960 " return a;\n"
9961 "}",
9962 Style);
9964 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
9965 Style.BraceWrapping.AfterFunction = true;
9966 verifyFormat("int f(i);\n" // No break here.
9967 "int\n" // Break here.
9968 "f(i)\n"
9969 "{\n"
9970 " return i + 1;\n"
9971 "}\n"
9972 "int\n" // Break here.
9973 "f(i)\n"
9974 "{\n"
9975 " return i + 1;\n"
9976 "};",
9977 Style);
9978 verifyFormat("int f(a, b, c);\n" // No break here.
9979 "int\n" // Break here.
9980 "f(a, b, c)\n" // Break here.
9981 "short a, b;\n"
9982 "float c;\n"
9983 "{\n"
9984 " return a + b < c;\n"
9985 "}\n"
9986 "int\n" // Break here.
9987 "f(a, b, c)\n" // Break here.
9988 "short a, b;\n"
9989 "float c;\n"
9990 "{\n"
9991 " return a + b < c;\n"
9992 "};",
9993 Style);
9994 verifyFormat("byte *\n" // Break here.
9995 "f(a)\n" // Break here.
9996 "byte a[];\n"
9997 "{\n"
9998 " return a;\n"
9999 "}",
10000 Style);
10001 verifyFormat("byte *\n"
10002 "f(a)\n"
10003 "byte /* K&R C */ a[];\n"
10004 "{\n"
10005 " return a;\n"
10006 "}\n"
10007 "byte *\n"
10008 "g(p)\n"
10009 "byte /* K&R C */ *p;\n"
10010 "{\n"
10011 " return p;\n"
10012 "}",
10013 Style);
10014 verifyFormat("bool f(int a, int) override;\n"
10015 "Bar g(int a, Bar) final;\n"
10016 "Bar h(a, Bar) final;",
10017 Style);
10018 verifyFormat("int\n"
10019 "f(a)",
10020 Style);
10021 verifyFormat("bool\n"
10022 "f(size_t = 0, bool b = false)\n"
10023 "{\n"
10024 " return !b;\n"
10025 "}",
10026 Style);
10028 // The return breaking style doesn't affect:
10029 // * function and object definitions with attribute-like macros
10030 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10031 " ABSL_GUARDED_BY(mutex) = {};",
10032 getGoogleStyleWithColumns(40));
10033 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10034 " ABSL_GUARDED_BY(mutex); // comment",
10035 getGoogleStyleWithColumns(40));
10036 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10037 " ABSL_GUARDED_BY(mutex1)\n"
10038 " ABSL_GUARDED_BY(mutex2);",
10039 getGoogleStyleWithColumns(40));
10040 verifyFormat("Tttttt f(int a, int b)\n"
10041 " ABSL_GUARDED_BY(mutex1)\n"
10042 " ABSL_GUARDED_BY(mutex2);",
10043 getGoogleStyleWithColumns(40));
10044 // * typedefs
10045 verifyGoogleFormat("typedef ATTR(X) char x;");
10047 Style = getGNUStyle();
10049 // Test for comments at the end of function declarations.
10050 verifyFormat("void\n"
10051 "foo (int a, /*abc*/ int b) // def\n"
10052 "{\n"
10053 "}",
10054 Style);
10056 verifyFormat("void\n"
10057 "foo (int a, /* abc */ int b) /* def */\n"
10058 "{\n"
10059 "}",
10060 Style);
10062 // Definitions that should not break after return type
10063 verifyFormat("void foo (int a, int b); // def", Style);
10064 verifyFormat("void foo (int a, int b); /* def */", Style);
10065 verifyFormat("void foo (int a, int b);", Style);
10068 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10069 FormatStyle NoBreak = getLLVMStyle();
10070 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10071 FormatStyle Break = getLLVMStyle();
10072 Break.AlwaysBreakBeforeMultilineStrings = true;
10073 verifyFormat("aaaa = \"bbbb\"\n"
10074 " \"cccc\";",
10075 NoBreak);
10076 verifyFormat("aaaa =\n"
10077 " \"bbbb\"\n"
10078 " \"cccc\";",
10079 Break);
10080 verifyFormat("aaaa(\"bbbb\"\n"
10081 " \"cccc\");",
10082 NoBreak);
10083 verifyFormat("aaaa(\n"
10084 " \"bbbb\"\n"
10085 " \"cccc\");",
10086 Break);
10087 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10088 " \"cccc\");",
10089 NoBreak);
10090 verifyFormat("aaaa(qqq,\n"
10091 " \"bbbb\"\n"
10092 " \"cccc\");",
10093 Break);
10094 verifyFormat("aaaa(qqq,\n"
10095 " L\"bbbb\"\n"
10096 " L\"cccc\");",
10097 Break);
10098 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10099 " \"bbbb\"));",
10100 Break);
10101 verifyFormat("string s = someFunction(\n"
10102 " \"abc\"\n"
10103 " \"abc\");",
10104 Break);
10106 // As we break before unary operators, breaking right after them is bad.
10107 verifyFormat("string foo = abc ? \"x\"\n"
10108 " \"blah blah blah blah blah blah\"\n"
10109 " : \"y\";",
10110 Break);
10112 // Don't break if there is no column gain.
10113 verifyFormat("f(\"aaaa\"\n"
10114 " \"bbbb\");",
10115 Break);
10117 // Treat literals with escaped newlines like multi-line string literals.
10118 verifyNoChange("x = \"a\\\n"
10119 "b\\\n"
10120 "c\";",
10121 NoBreak);
10122 verifyFormat("xxxx =\n"
10123 " \"a\\\n"
10124 "b\\\n"
10125 "c\";",
10126 "xxxx = \"a\\\n"
10127 "b\\\n"
10128 "c\";",
10129 Break);
10131 verifyFormat("NSString *const kString =\n"
10132 " @\"aaaa\"\n"
10133 " @\"bbbb\";",
10134 "NSString *const kString = @\"aaaa\"\n"
10135 "@\"bbbb\";",
10136 Break);
10138 Break.ColumnLimit = 0;
10139 verifyFormat("const char *hello = \"hello llvm\";", Break);
10142 TEST_F(FormatTest, AlignsPipes) {
10143 verifyFormat(
10144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10145 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10146 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10147 verifyFormat(
10148 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10149 " << aaaaaaaaaaaaaaaaaaaa;");
10150 verifyFormat(
10151 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10152 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10153 verifyFormat(
10154 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10155 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10156 verifyFormat(
10157 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10158 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10159 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10160 verifyFormat(
10161 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10162 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10163 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10164 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10167 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10168 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10169 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10170 verifyFormat(
10171 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10173 verifyFormat(
10174 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10175 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10177 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10178 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10179 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10181 " aaaaaaaaaaaaaaaaaaaaa)\n"
10182 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10183 verifyFormat("LOG_IF(aaa == //\n"
10184 " bbb)\n"
10185 " << a << b;");
10187 // But sometimes, breaking before the first "<<" is desirable.
10188 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10189 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10190 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10191 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10192 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10193 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10194 " << BEF << IsTemplate << Description << E->getType();");
10195 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10196 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10198 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10199 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10201 " << aaa;");
10203 verifyFormat(
10204 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10205 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10207 // Incomplete string literal.
10208 verifyFormat("llvm::errs() << \"\n"
10209 " << a;",
10210 "llvm::errs() << \"\n<<a;");
10212 verifyFormat("void f() {\n"
10213 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10214 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10215 "}");
10217 // Handle 'endl'.
10218 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10219 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10220 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10222 // Handle '\n'.
10223 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10224 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10225 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10226 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10227 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10228 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10229 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10232 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10233 verifyFormat("return out << \"somepacket = {\\n\"\n"
10234 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10235 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10236 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10237 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10238 " << \"}\";");
10240 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10241 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10242 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10243 verifyFormat(
10244 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10245 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10246 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10247 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10248 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10249 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10250 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10251 verifyFormat(
10252 "void f() {\n"
10253 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10254 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10255 "}");
10257 // Breaking before the first "<<" is generally not desirable.
10258 verifyFormat(
10259 "llvm::errs()\n"
10260 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10261 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10262 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10263 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10264 getLLVMStyleWithColumns(70));
10265 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10266 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10267 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10268 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10269 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10270 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10271 getLLVMStyleWithColumns(70));
10273 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10274 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10275 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10276 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10277 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10278 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10279 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10280 " (aaaa + aaaa);",
10281 getLLVMStyleWithColumns(40));
10282 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10283 " (aaaaaaa + aaaaa));",
10284 getLLVMStyleWithColumns(40));
10285 verifyFormat(
10286 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10287 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10288 " bbbbbbbbbbbbbbbbbbbbbbb);");
10291 TEST_F(FormatTest, UnderstandsEquals) {
10292 verifyFormat(
10293 "aaaaaaaaaaaaaaaaa =\n"
10294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10295 verifyFormat(
10296 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10298 verifyFormat(
10299 "if (a) {\n"
10300 " f();\n"
10301 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10303 "}");
10305 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10306 " 100000000 + 10000000) {\n}");
10309 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10310 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10311 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10313 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10314 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10316 verifyFormat(
10317 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10318 " Parameter2);");
10320 verifyFormat(
10321 "ShortObject->shortFunction(\n"
10322 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10323 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10325 verifyFormat("loooooooooooooongFunction(\n"
10326 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10328 verifyFormat(
10329 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10330 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10332 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10333 " .WillRepeatedly(Return(SomeValue));");
10334 verifyFormat("void f() {\n"
10335 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10336 " .Times(2)\n"
10337 " .WillRepeatedly(Return(SomeValue));\n"
10338 "}");
10339 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10340 " ccccccccccccccccccccccc);");
10341 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10343 " .aaaaa(aaaaa),\n"
10344 " aaaaaaaaaaaaaaaaaaaaa);");
10345 verifyFormat("void f() {\n"
10346 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10347 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10348 "}");
10349 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10351 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10352 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10353 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10354 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10355 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10356 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10357 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10358 "}");
10360 // Here, it is not necessary to wrap at "." or "->".
10361 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10362 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10363 verifyFormat(
10364 "aaaaaaaaaaa->aaaaaaaaa(\n"
10365 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10366 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10368 verifyFormat(
10369 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10370 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10371 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10372 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10373 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10374 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10376 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10378 " .a();");
10380 FormatStyle NoBinPacking = getLLVMStyle();
10381 NoBinPacking.BinPackParameters = false;
10382 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10383 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10384 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10385 " aaaaaaaaaaaaaaaaaaa,\n"
10386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10387 NoBinPacking);
10389 // If there is a subsequent call, change to hanging indentation.
10390 verifyFormat(
10391 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10392 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10393 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10394 verifyFormat(
10395 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10396 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10397 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10399 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10400 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10402 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10405 TEST_F(FormatTest, WrapsTemplateDeclarations) {
10406 verifyFormat("template <typename T>\n"
10407 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10408 verifyFormat("template <typename T>\n"
10409 "// T should be one of {A, B}.\n"
10410 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10411 verifyFormat(
10412 "template <typename T>\n"
10413 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10414 verifyFormat("template <typename T>\n"
10415 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10416 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10417 verifyFormat(
10418 "template <typename T>\n"
10419 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10420 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10421 verifyFormat(
10422 "template <typename T>\n"
10423 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10424 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10426 verifyFormat("template <typename T>\n"
10427 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10428 " int aaaaaaaaaaaaaaaaaaaaaa);");
10429 verifyFormat(
10430 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10431 " typename T4 = char>\n"
10432 "void f();");
10433 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10434 " template <typename> class cccccccccccccccccccccc,\n"
10435 " typename ddddddddddddd>\n"
10436 "class C {};");
10437 verifyFormat(
10438 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10441 verifyFormat("void f() {\n"
10442 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10443 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10444 "}");
10446 verifyFormat("template <typename T> class C {};");
10447 verifyFormat("template <typename T> void f();");
10448 verifyFormat("template <typename T> void f() {}");
10449 verifyFormat(
10450 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10453 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10456 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10457 getLLVMStyleWithColumns(72));
10458 verifyFormat("static_cast<A< //\n"
10459 " B> *>(\n"
10460 "\n"
10461 ");",
10462 "static_cast<A<//\n"
10463 " B>*>(\n"
10464 "\n"
10465 " );");
10466 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10467 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10469 FormatStyle AlwaysBreak = getLLVMStyle();
10470 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10471 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10472 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10473 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10474 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10475 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10476 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10477 verifyFormat("template <template <typename> class Fooooooo,\n"
10478 " template <typename> class Baaaaaaar>\n"
10479 "struct C {};",
10480 AlwaysBreak);
10481 verifyFormat("template <typename T> // T can be A, B or C.\n"
10482 "struct C {};",
10483 AlwaysBreak);
10484 verifyFormat("template <typename T>\n"
10485 "C(T) noexcept;",
10486 AlwaysBreak);
10487 verifyFormat("template <typename T>\n"
10488 "ClassName(T) noexcept;",
10489 AlwaysBreak);
10490 verifyFormat("template <typename T>\n"
10491 "POOR_NAME(T) noexcept;",
10492 AlwaysBreak);
10493 verifyFormat("template <enum E> class A {\n"
10494 "public:\n"
10495 " E *f();\n"
10496 "};");
10498 FormatStyle NeverBreak = getLLVMStyle();
10499 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No;
10500 verifyFormat("template <typename T> class C {};", NeverBreak);
10501 verifyFormat("template <typename T> void f();", NeverBreak);
10502 verifyFormat("template <typename T> void f() {}", NeverBreak);
10503 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10504 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10505 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10506 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10507 "bbbbbbbbbbbbbbbbbbbb) {}",
10508 NeverBreak);
10509 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10510 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10511 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10512 NeverBreak);
10513 verifyFormat("template <template <typename> class Fooooooo,\n"
10514 " template <typename> class Baaaaaaar>\n"
10515 "struct C {};",
10516 NeverBreak);
10517 verifyFormat("template <typename T> // T can be A, B or C.\n"
10518 "struct C {};",
10519 NeverBreak);
10520 verifyFormat("template <enum E> class A {\n"
10521 "public:\n"
10522 " E *f();\n"
10523 "};",
10524 NeverBreak);
10525 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10526 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10527 "bbbbbbbbbbbbbbbbbbbb) {}",
10528 NeverBreak);
10531 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
10532 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
10533 Style.ColumnLimit = 60;
10534 verifyFormat("// Baseline - no comments.\n"
10535 "template <\n"
10536 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10537 "void f() {}",
10538 Style);
10540 verifyFormat("template <\n"
10541 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10542 "void f() {}",
10543 "template <\n"
10544 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10545 "void f() {}",
10546 Style);
10548 verifyFormat(
10549 "template <\n"
10550 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10551 "void f() {}",
10552 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10553 "void f() {}",
10554 Style);
10556 verifyFormat("template <\n"
10557 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10558 " // multiline\n"
10559 "void f() {}",
10560 "template <\n"
10561 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10562 " // multiline\n"
10563 "void f() {}",
10564 Style);
10566 verifyFormat(
10567 "template <typename aaaaaaaaaa<\n"
10568 " bbbbbbbbbbbb>::value> // trailing loooong\n"
10569 "void f() {}",
10570 "template <\n"
10571 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
10572 "void f() {}",
10573 Style);
10576 TEST_F(FormatTest, WrapsTemplateParameters) {
10577 FormatStyle Style = getLLVMStyle();
10578 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10579 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10580 verifyFormat(
10581 "template <typename... a> struct q {};\n"
10582 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10583 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10584 " y;",
10585 Style);
10586 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10587 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10588 verifyFormat(
10589 "template <typename... a> struct r {};\n"
10590 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10591 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10592 " y;",
10593 Style);
10594 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10595 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10596 verifyFormat("template <typename... a> struct s {};\n"
10597 "extern s<\n"
10598 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10599 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10600 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10601 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10602 " y;",
10603 Style);
10604 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10605 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10606 verifyFormat("template <typename... a> struct t {};\n"
10607 "extern t<\n"
10608 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10609 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10610 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10611 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10612 " y;",
10613 Style);
10616 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
10617 verifyFormat(
10618 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10620 verifyFormat(
10621 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10625 // FIXME: Should we have the extra indent after the second break?
10626 verifyFormat(
10627 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10631 verifyFormat(
10632 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
10633 " cccccccccccccccccccccccccccccccccccccccccccccc());");
10635 // Breaking at nested name specifiers is generally not desirable.
10636 verifyFormat(
10637 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10638 " aaaaaaaaaaaaaaaaaaaaaaa);");
10640 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
10641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10643 " aaaaaaaaaaaaaaaaaaaaa);",
10644 getLLVMStyleWithColumns(74));
10646 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10648 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10650 verifyFormat(
10651 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10652 " AndAnotherLongClassNameToShowTheIssue() {}\n"
10653 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10654 " ~AndAnotherLongClassNameToShowTheIssue() {}");
10657 TEST_F(FormatTest, UnderstandsTemplateParameters) {
10658 verifyFormat("A<int> a;");
10659 verifyFormat("A<A<A<int>>> a;");
10660 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
10661 verifyFormat("bool x = a < 1 || 2 > a;");
10662 verifyFormat("bool x = 5 < f<int>();");
10663 verifyFormat("bool x = f<int>() > 5;");
10664 verifyFormat("bool x = 5 < a<int>::x;");
10665 verifyFormat("bool x = a < 4 ? a > 2 : false;");
10666 verifyFormat("bool x = f() ? a < 2 : a > 2;");
10668 verifyGoogleFormat("A<A<int>> a;");
10669 verifyGoogleFormat("A<A<A<int>>> a;");
10670 verifyGoogleFormat("A<A<A<A<int>>>> a;");
10671 verifyGoogleFormat("A<A<int> > a;");
10672 verifyGoogleFormat("A<A<A<int> > > a;");
10673 verifyGoogleFormat("A<A<A<A<int> > > > a;");
10674 verifyGoogleFormat("A<::A<int>> a;");
10675 verifyGoogleFormat("A<::A> a;");
10676 verifyGoogleFormat("A< ::A> a;");
10677 verifyGoogleFormat("A< ::A<int> > a;");
10678 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
10679 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
10680 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
10681 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
10682 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
10683 getGoogleStyle());
10685 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
10687 // template closer followed by a token that starts with > or =
10688 verifyFormat("bool b = a<1> > 1;");
10689 verifyFormat("bool b = a<1> >= 1;");
10690 verifyFormat("int i = a<1> >> 1;");
10691 FormatStyle Style = getLLVMStyle();
10692 Style.SpaceBeforeAssignmentOperators = false;
10693 verifyFormat("bool b= a<1> == 1;", Style);
10694 verifyFormat("a<int> = 1;", Style);
10695 verifyFormat("a<int> >>= 1;", Style);
10697 verifyFormat("test < a | b >> c;");
10698 verifyFormat("test<test<a | b>> c;");
10699 verifyFormat("test >> a >> b;");
10700 verifyFormat("test << a >> b;");
10702 verifyFormat("f<int>();");
10703 verifyFormat("template <typename T> void f() {}");
10704 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
10705 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
10706 "sizeof(char)>::type>;");
10707 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
10708 verifyFormat("f(a.operator()<A>());");
10709 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10710 " .template operator()<A>());",
10711 getLLVMStyleWithColumns(35));
10712 verifyFormat("bool_constant<a && noexcept(f())>;");
10713 verifyFormat("bool_constant<a || noexcept(f())>;");
10715 verifyFormat("if (std::tuple_size_v<T> > 0)");
10717 // Not template parameters.
10718 verifyFormat("return a < b && c > d;");
10719 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
10720 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
10721 verifyFormat("void f() {\n"
10722 " while (a < b && c > d) {\n"
10723 " }\n"
10724 "}");
10725 verifyFormat("template <typename... Types>\n"
10726 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
10728 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
10730 getLLVMStyleWithColumns(60));
10731 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
10732 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
10733 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
10734 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
10736 verifyFormat("#define FOO(typeName, realClass) \\\n"
10737 " { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }",
10738 getLLVMStyleWithColumns(60));
10741 TEST_F(FormatTest, UnderstandsShiftOperators) {
10742 verifyFormat("if (i < x >> 1)");
10743 verifyFormat("while (i < x >> 1)");
10744 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
10745 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
10746 verifyFormat(
10747 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
10748 verifyFormat("Foo.call<Bar<Function>>()");
10749 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
10750 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
10751 "++i, v = v >> 1)");
10752 verifyFormat("if (w<u<v<x>>, 1>::t)");
10755 TEST_F(FormatTest, BitshiftOperatorWidth) {
10756 verifyFormat("int a = 1 << 2; /* foo\n"
10757 " bar */",
10758 "int a=1<<2; /* foo\n"
10759 " bar */");
10761 verifyFormat("int b = 256 >> 1; /* foo\n"
10762 " bar */",
10763 "int b =256>>1 ; /* foo\n"
10764 " bar */");
10767 TEST_F(FormatTest, UnderstandsBinaryOperators) {
10768 verifyFormat("COMPARE(a, ==, b);");
10769 verifyFormat("auto s = sizeof...(Ts) - 1;");
10772 TEST_F(FormatTest, UnderstandsPointersToMembers) {
10773 verifyFormat("int A::*x;");
10774 verifyFormat("int (S::*func)(void *);");
10775 verifyFormat("void f() { int (S::*func)(void *); }");
10776 verifyFormat("typedef bool *(Class::*Member)() const;");
10777 verifyFormat("void f() {\n"
10778 " (a->*f)();\n"
10779 " a->*x;\n"
10780 " (a.*f)();\n"
10781 " ((*a).*f)();\n"
10782 " a.*x;\n"
10783 "}");
10784 verifyFormat("void f() {\n"
10785 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10786 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
10787 "}");
10788 verifyFormat(
10789 "(aaaaaaaaaa->*bbbbbbb)(\n"
10790 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10791 FormatStyle Style = getLLVMStyle();
10792 Style.PointerAlignment = FormatStyle::PAS_Left;
10793 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
10796 TEST_F(FormatTest, UnderstandsUnaryOperators) {
10797 verifyFormat("int a = -2;");
10798 verifyFormat("f(-1, -2, -3);");
10799 verifyFormat("a[-1] = 5;");
10800 verifyFormat("int a = 5 + -2;");
10801 verifyFormat("if (i == -1) {\n}");
10802 verifyFormat("if (i != -1) {\n}");
10803 verifyFormat("if (i > -1) {\n}");
10804 verifyFormat("if (i < -1) {\n}");
10805 verifyFormat("++(a->f());");
10806 verifyFormat("--(a->f());");
10807 verifyFormat("(a->f())++;");
10808 verifyFormat("a[42]++;");
10809 verifyFormat("if (!(a->f())) {\n}");
10810 verifyFormat("if (!+i) {\n}");
10811 verifyFormat("~&a;");
10812 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
10813 verifyFormat("sizeof -x");
10814 verifyFormat("sizeof +x");
10815 verifyFormat("sizeof *x");
10816 verifyFormat("sizeof &x");
10817 verifyFormat("delete +x;");
10818 verifyFormat("co_await +x;");
10819 verifyFormat("case *x:");
10820 verifyFormat("case &x:");
10822 verifyFormat("a-- > b;");
10823 verifyFormat("b ? -a : c;");
10824 verifyFormat("n * sizeof char16;");
10825 verifyGoogleFormat("n * alignof char16;");
10826 verifyFormat("sizeof(char);");
10827 verifyGoogleFormat("alignof(char);");
10829 verifyFormat("return -1;");
10830 verifyFormat("throw -1;");
10831 verifyFormat("switch (a) {\n"
10832 "case -1:\n"
10833 " break;\n"
10834 "}");
10835 verifyFormat("#define X -1");
10836 verifyFormat("#define X -kConstant");
10838 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
10839 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
10841 verifyFormat("int a = /* confusing comment */ -1;");
10842 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
10843 verifyFormat("int a = i /* confusing comment */++;");
10845 verifyFormat("co_yield -1;");
10846 verifyFormat("co_return -1;");
10848 // Check that * is not treated as a binary operator when we set
10849 // PointerAlignment as PAS_Left after a keyword and not a declaration.
10850 FormatStyle PASLeftStyle = getLLVMStyle();
10851 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
10852 verifyFormat("co_return *a;", PASLeftStyle);
10853 verifyFormat("co_await *a;", PASLeftStyle);
10854 verifyFormat("co_yield *a", PASLeftStyle);
10855 verifyFormat("return *a;", PASLeftStyle);
10858 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
10859 verifyFormat("if (!aaaaaaaaaa( // break\n"
10860 " aaaaa)) {\n"
10861 "}");
10862 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
10863 " aaaaa));");
10864 verifyFormat("*aaa = aaaaaaa( // break\n"
10865 " bbbbbb);");
10868 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
10869 verifyFormat("bool operator<();");
10870 verifyFormat("bool operator>();");
10871 verifyFormat("bool operator=();");
10872 verifyFormat("bool operator==();");
10873 verifyFormat("bool operator!=();");
10874 verifyFormat("int operator+();");
10875 verifyFormat("int operator++();");
10876 verifyFormat("int operator++(int) volatile noexcept;");
10877 verifyFormat("bool operator,();");
10878 verifyFormat("bool operator();");
10879 verifyFormat("bool operator()();");
10880 verifyFormat("bool operator[]();");
10881 verifyFormat("operator bool();");
10882 verifyFormat("operator int();");
10883 verifyFormat("operator void *();");
10884 verifyFormat("operator SomeType<int>();");
10885 verifyFormat("operator SomeType<int, int>();");
10886 verifyFormat("operator SomeType<SomeType<int>>();");
10887 verifyFormat("operator< <>();");
10888 verifyFormat("operator<< <>();");
10889 verifyFormat("< <>");
10891 verifyFormat("void *operator new(std::size_t size);");
10892 verifyFormat("void *operator new[](std::size_t size);");
10893 verifyFormat("void operator delete(void *ptr);");
10894 verifyFormat("void operator delete[](void *ptr);");
10895 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
10896 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
10897 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
10898 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
10900 verifyFormat(
10901 "ostream &operator<<(ostream &OutputStream,\n"
10902 " SomeReallyLongType WithSomeReallyLongValue);");
10903 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
10904 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
10905 " return left.group < right.group;\n"
10906 "}");
10907 verifyFormat("SomeType &operator=(const SomeType &S);");
10908 verifyFormat("f.template operator()<int>();");
10910 verifyGoogleFormat("operator void*();");
10911 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
10912 verifyGoogleFormat("operator ::A();");
10914 verifyFormat("using A::operator+;");
10915 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
10916 "int i;");
10918 // Calling an operator as a member function.
10919 verifyFormat("void f() { a.operator*(); }");
10920 verifyFormat("void f() { a.operator*(b & b); }");
10921 verifyFormat("void f() { a->operator&(a * b); }");
10922 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
10923 // TODO: Calling an operator as a non-member function is hard to distinguish.
10924 // https://llvm.org/PR50629
10925 // verifyFormat("void f() { operator*(a & a); }");
10926 // verifyFormat("void f() { operator&(a, b * b); }");
10928 verifyFormat("void f() { return operator()(x) * b; }");
10929 verifyFormat("void f() { return operator[](x) * b; }");
10930 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
10931 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
10932 verifyFormat("void f() { return operator\"\"s(x) * b; }");
10933 verifyFormat("void f() { return operator\"\" s(x) * b; }");
10934 verifyFormat("void f() { return operator\"\"if(x) * b; }");
10936 verifyFormat("::operator delete(foo);");
10937 verifyFormat("::operator new(n * sizeof(foo));");
10938 verifyFormat("foo() { ::operator delete(foo); }");
10939 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
10942 TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
10943 verifyFormat("C<&operator- > minus;");
10944 verifyFormat("C<&operator> > gt;");
10945 verifyFormat("C<&operator>= > ge;");
10946 verifyFormat("C<&operator<= > le;");
10947 verifyFormat("C<&operator< <X>> lt;");
10950 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
10951 verifyFormat("void A::b() && {}");
10952 verifyFormat("void A::b() && noexcept {}");
10953 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
10954 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
10955 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
10956 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
10957 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
10958 verifyFormat("Deleted &operator=(const Deleted &) &;");
10959 verifyFormat("Deleted &operator=(const Deleted &) &&;");
10960 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
10961 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
10962 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
10963 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
10964 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
10965 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
10966 verifyFormat("void Fn(T const &) const &;");
10967 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
10968 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
10969 verifyGoogleFormat("template <typename T>\n"
10970 "void F(T) && = delete;");
10971 verifyFormat("template <typename T> void operator=(T) &;");
10972 verifyFormat("template <typename T> void operator=(T) const &;");
10973 verifyFormat("template <typename T> void operator=(T) & noexcept;");
10974 verifyFormat("template <typename T> void operator=(T) & = default;");
10975 verifyFormat("template <typename T> void operator=(T) &&;");
10976 verifyFormat("template <typename T> void operator=(T) && = delete;");
10977 verifyFormat("template <typename T> void operator=(T) & {}");
10978 verifyFormat("template <typename T> void operator=(T) && {}");
10980 FormatStyle AlignLeft = getLLVMStyle();
10981 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
10982 verifyFormat("void A::b() && {}", AlignLeft);
10983 verifyFormat("void A::b() && noexcept {}", AlignLeft);
10984 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
10985 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
10986 AlignLeft);
10987 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
10988 AlignLeft);
10989 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
10990 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
10991 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
10992 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
10993 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
10994 verifyFormat("auto Function(T) & -> void;", AlignLeft);
10995 verifyFormat("void Fn(T const&) const&;", AlignLeft);
10996 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
10997 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
10998 AlignLeft);
10999 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11000 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11001 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11002 AlignLeft);
11003 verifyFormat("template <typename T> void operator=(T) & = default;",
11004 AlignLeft);
11005 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11006 verifyFormat("template <typename T> void operator=(T) && = delete;",
11007 AlignLeft);
11008 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11009 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11011 FormatStyle AlignMiddle = getLLVMStyle();
11012 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11013 verifyFormat("void A::b() && {}", AlignMiddle);
11014 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11015 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11016 AlignMiddle);
11017 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11018 AlignMiddle);
11019 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11020 AlignMiddle);
11021 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11022 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11023 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11024 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11025 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11026 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11027 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11028 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11029 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11030 AlignMiddle);
11031 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11032 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11033 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11034 AlignMiddle);
11035 verifyFormat("template <typename T> void operator=(T) & = default;",
11036 AlignMiddle);
11037 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11038 verifyFormat("template <typename T> void operator=(T) && = delete;",
11039 AlignMiddle);
11040 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11041 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11043 FormatStyle Spaces = getLLVMStyle();
11044 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11045 Spaces.SpacesInParensOptions = {};
11046 Spaces.SpacesInParensOptions.InCStyleCasts = true;
11047 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11048 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11049 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11050 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11052 Spaces.SpacesInParensOptions.InCStyleCasts = false;
11053 Spaces.SpacesInParensOptions.Other = true;
11054 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11055 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11056 Spaces);
11057 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11058 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11060 FormatStyle BreakTemplate = getLLVMStyle();
11061 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11063 verifyFormat("struct f {\n"
11064 " template <class T>\n"
11065 " int &foo(const std::string &str) & noexcept {}\n"
11066 "};",
11067 BreakTemplate);
11069 verifyFormat("struct f {\n"
11070 " template <class T>\n"
11071 " int &foo(const std::string &str) && noexcept {}\n"
11072 "};",
11073 BreakTemplate);
11075 verifyFormat("struct f {\n"
11076 " template <class T>\n"
11077 " int &foo(const std::string &str) const & noexcept {}\n"
11078 "};",
11079 BreakTemplate);
11081 verifyFormat("struct f {\n"
11082 " template <class T>\n"
11083 " int &foo(const std::string &str) const & noexcept {}\n"
11084 "};",
11085 BreakTemplate);
11087 verifyFormat("struct f {\n"
11088 " template <class T>\n"
11089 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11090 "};",
11091 BreakTemplate);
11093 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11094 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations =
11095 FormatStyle::BTDS_Yes;
11096 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11098 verifyFormat("struct f {\n"
11099 " template <class T>\n"
11100 " int& foo(const std::string& str) & noexcept {}\n"
11101 "};",
11102 AlignLeftBreakTemplate);
11104 verifyFormat("struct f {\n"
11105 " template <class T>\n"
11106 " int& foo(const std::string& str) && noexcept {}\n"
11107 "};",
11108 AlignLeftBreakTemplate);
11110 verifyFormat("struct f {\n"
11111 " template <class T>\n"
11112 " int& foo(const std::string& str) const& noexcept {}\n"
11113 "};",
11114 AlignLeftBreakTemplate);
11116 verifyFormat("struct f {\n"
11117 " template <class T>\n"
11118 " int& foo(const std::string& str) const&& noexcept {}\n"
11119 "};",
11120 AlignLeftBreakTemplate);
11122 verifyFormat("struct f {\n"
11123 " template <class T>\n"
11124 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11125 "};",
11126 AlignLeftBreakTemplate);
11128 // The `&` in `Type&` should not be confused with a trailing `&` of
11129 // DEPRECATED(reason) member function.
11130 verifyFormat("struct f {\n"
11131 " template <class T>\n"
11132 " DEPRECATED(reason)\n"
11133 " Type &foo(arguments) {}\n"
11134 "};",
11135 BreakTemplate);
11137 verifyFormat("struct f {\n"
11138 " template <class T>\n"
11139 " DEPRECATED(reason)\n"
11140 " Type& foo(arguments) {}\n"
11141 "};",
11142 AlignLeftBreakTemplate);
11144 verifyFormat("void (*foopt)(int) = &func;");
11146 FormatStyle DerivePointerAlignment = getLLVMStyle();
11147 DerivePointerAlignment.DerivePointerAlignment = true;
11148 // There's always a space between the function and its trailing qualifiers.
11149 // This isn't evidence for PAS_Right (or for PAS_Left).
11150 std::string Prefix = "void a() &;\n"
11151 "void b() &;\n";
11152 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11153 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11154 // Same if the function is an overloaded operator, and with &&.
11155 Prefix = "void operator()() &&;\n"
11156 "void operator()() &&;\n";
11157 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11158 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11159 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11160 Prefix = "void a() const &;\n"
11161 "void b() const &;\n";
11162 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11165 TEST_F(FormatTest, PointerAlignmentFallback) {
11166 FormatStyle Style = getLLVMStyle();
11167 Style.DerivePointerAlignment = true;
11169 const StringRef Code("int* p;\n"
11170 "int *q;\n"
11171 "int * r;");
11173 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11174 verifyFormat("int *p;\n"
11175 "int *q;\n"
11176 "int *r;",
11177 Code, Style);
11179 Style.PointerAlignment = FormatStyle::PAS_Left;
11180 verifyFormat("int* p;\n"
11181 "int* q;\n"
11182 "int* r;",
11183 Code, Style);
11185 Style.PointerAlignment = FormatStyle::PAS_Middle;
11186 verifyFormat("int * p;\n"
11187 "int * q;\n"
11188 "int * r;",
11189 Code, Style);
11192 TEST_F(FormatTest, UnderstandsNewAndDelete) {
11193 verifyFormat("void f() {\n"
11194 " A *a = new A;\n"
11195 " A *a = new (placement) A;\n"
11196 " delete a;\n"
11197 " delete (A *)a;\n"
11198 "}");
11199 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11200 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11201 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11202 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11203 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11204 verifyFormat("delete[] h->p;");
11205 verifyFormat("delete[] (void *)p;");
11207 verifyFormat("void operator delete(void *foo) ATTRIB;");
11208 verifyFormat("void operator new(void *foo) ATTRIB;");
11209 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11210 verifyFormat("void operator delete(void *ptr) noexcept;");
11212 verifyFormat("void new(link p);\n"
11213 "void delete(link p);",
11214 "void new (link p);\n"
11215 "void delete (link p);");
11217 FormatStyle AfterPlacementOperator = getLLVMStyle();
11218 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11219 EXPECT_EQ(
11220 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator,
11221 FormatStyle::SpaceBeforeParensCustom::APO_Leave);
11222 verifyFormat("new (buf) int;", AfterPlacementOperator);
11223 verifyFormat("new(buf) int;", AfterPlacementOperator);
11225 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11226 FormatStyle::SpaceBeforeParensCustom::APO_Never;
11227 verifyFormat("struct A {\n"
11228 " int *a;\n"
11229 " A(int *p) : a(new(p) int) {\n"
11230 " new(p) int;\n"
11231 " int *b = new(p) int;\n"
11232 " int *c = new(p) int(3);\n"
11233 " delete(b);\n"
11234 " }\n"
11235 "};",
11236 AfterPlacementOperator);
11237 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11239 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11240 FormatStyle::SpaceBeforeParensCustom::APO_Always;
11241 verifyFormat("struct A {\n"
11242 " int *a;\n"
11243 " A(int *p) : a(new (p) int) {\n"
11244 " new (p) int;\n"
11245 " int *b = new (p) int;\n"
11246 " int *c = new (p) int(3);\n"
11247 " delete (b);\n"
11248 " }\n"
11249 "};",
11250 AfterPlacementOperator);
11251 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11254 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11255 verifyFormat("int *f(int *a) {}");
11256 verifyFormat("int main(int argc, char **argv) {}");
11257 verifyFormat("Test::Test(int b) : a(b * b) {}");
11258 verifyIndependentOfContext("f(a, *a);");
11259 verifyFormat("void g() { f(*a); }");
11260 verifyIndependentOfContext("int a = b * 10;");
11261 verifyIndependentOfContext("int a = 10 * b;");
11262 verifyIndependentOfContext("int a = b * c;");
11263 verifyIndependentOfContext("int a += b * c;");
11264 verifyIndependentOfContext("int a -= b * c;");
11265 verifyIndependentOfContext("int a *= b * c;");
11266 verifyIndependentOfContext("int a /= b * c;");
11267 verifyIndependentOfContext("int a = *b;");
11268 verifyIndependentOfContext("int a = *b * c;");
11269 verifyIndependentOfContext("int a = b * *c;");
11270 verifyIndependentOfContext("int a = b * (10);");
11271 verifyIndependentOfContext("S << b * (10);");
11272 verifyIndependentOfContext("return 10 * b;");
11273 verifyIndependentOfContext("return *b * *c;");
11274 verifyIndependentOfContext("return a & ~b;");
11275 verifyIndependentOfContext("f(b ? *c : *d);");
11276 verifyIndependentOfContext("int a = b ? *c : *d;");
11277 verifyIndependentOfContext("*b = a;");
11278 verifyIndependentOfContext("a * ~b;");
11279 verifyIndependentOfContext("a * !b;");
11280 verifyIndependentOfContext("a * +b;");
11281 verifyIndependentOfContext("a * -b;");
11282 verifyIndependentOfContext("a * ++b;");
11283 verifyIndependentOfContext("a * --b;");
11284 verifyIndependentOfContext("a[4] * b;");
11285 verifyIndependentOfContext("a[a * a] = 1;");
11286 verifyIndependentOfContext("f() * b;");
11287 verifyIndependentOfContext("a * [self dostuff];");
11288 verifyIndependentOfContext("int x = a * (a + b);");
11289 verifyIndependentOfContext("(a *)(a + b);");
11290 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11291 verifyIndependentOfContext("int *pa = (int *)&a;");
11292 verifyIndependentOfContext("return sizeof(int **);");
11293 verifyIndependentOfContext("return sizeof(int ******);");
11294 verifyIndependentOfContext("return (int **&)a;");
11295 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11296 verifyFormat("void f(Type (*parameter)[10]) {}");
11297 verifyFormat("void f(Type (&parameter)[10]) {}");
11298 verifyGoogleFormat("return sizeof(int**);");
11299 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11300 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11301 verifyFormat("auto a = [](int **&, int ***) {};");
11302 verifyFormat("auto PointerBinding = [](const char *S) {};");
11303 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11304 verifyFormat("[](const decltype(*a) &value) {}");
11305 verifyFormat("[](const typeof(*a) &value) {}");
11306 verifyFormat("[](const _Atomic(a *) &value) {}");
11307 verifyFormat("[](const __underlying_type(a) &value) {}");
11308 verifyFormat("decltype(a * b) F();");
11309 verifyFormat("typeof(a * b) F();");
11310 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11311 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11312 verifyIndependentOfContext("typedef void (*f)(int *a);");
11313 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11314 verifyIndependentOfContext("int i{a * b};");
11315 verifyIndependentOfContext("aaa && aaa->f();");
11316 verifyIndependentOfContext("int x = ~*p;");
11317 verifyFormat("Constructor() : a(a), area(width * height) {}");
11318 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11319 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11320 verifyFormat("void f() { f(a, c * d); }");
11321 verifyFormat("void f() { f(new a(), c * d); }");
11322 verifyFormat("void f(const MyOverride &override);");
11323 verifyFormat("void f(const MyFinal &final);");
11324 verifyIndependentOfContext("bool a = f() && override.f();");
11325 verifyIndependentOfContext("bool a = f() && final.f();");
11327 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11329 verifyIndependentOfContext("A<int *> a;");
11330 verifyIndependentOfContext("A<int **> a;");
11331 verifyIndependentOfContext("A<int *, int *> a;");
11332 verifyIndependentOfContext("A<int *[]> a;");
11333 verifyIndependentOfContext(
11334 "const char *const p = reinterpret_cast<const char *const>(q);");
11335 verifyIndependentOfContext("A<int **, int **> a;");
11336 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11337 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11338 verifyFormat("for (; a && b;) {\n}");
11339 verifyFormat("bool foo = true && [] { return false; }();");
11341 verifyFormat(
11342 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11345 verifyGoogleFormat("int const* a = &b;");
11346 verifyGoogleFormat("**outparam = 1;");
11347 verifyGoogleFormat("*outparam = a * b;");
11348 verifyGoogleFormat("int main(int argc, char** argv) {}");
11349 verifyGoogleFormat("A<int*> a;");
11350 verifyGoogleFormat("A<int**> a;");
11351 verifyGoogleFormat("A<int*, int*> a;");
11352 verifyGoogleFormat("A<int**, int**> a;");
11353 verifyGoogleFormat("f(b ? *c : *d);");
11354 verifyGoogleFormat("int a = b ? *c : *d;");
11355 verifyGoogleFormat("Type* t = **x;");
11356 verifyGoogleFormat("Type* t = *++*x;");
11357 verifyGoogleFormat("*++*x;");
11358 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11359 verifyGoogleFormat("Type* t = x++ * y;");
11360 verifyGoogleFormat(
11361 "const char* const p = reinterpret_cast<const char* const>(q);");
11362 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11363 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11364 verifyGoogleFormat("template <typename T>\n"
11365 "void f(int i = 0, SomeType** temps = NULL);");
11367 FormatStyle Left = getLLVMStyle();
11368 Left.PointerAlignment = FormatStyle::PAS_Left;
11369 verifyFormat("x = *a(x) = *a(y);", Left);
11370 verifyFormat("for (;; *a = b) {\n}", Left);
11371 verifyFormat("return *this += 1;", Left);
11372 verifyFormat("throw *x;", Left);
11373 verifyFormat("delete *x;", Left);
11374 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
11375 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
11376 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
11377 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
11378 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
11379 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
11380 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
11381 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
11382 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
11384 verifyIndependentOfContext("a = *(x + y);");
11385 verifyIndependentOfContext("a = &(x + y);");
11386 verifyIndependentOfContext("*(x + y).call();");
11387 verifyIndependentOfContext("&(x + y)->call();");
11388 verifyFormat("void f() { &(*I).first; }");
11390 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11391 verifyFormat("f(* /* confusing comment */ foo);");
11392 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11393 verifyFormat("void foo(int * // this is the first paramters\n"
11394 " ,\n"
11395 " int second);");
11396 verifyFormat("double term = a * // first\n"
11397 " b;");
11398 verifyFormat(
11399 "int *MyValues = {\n"
11400 " *A, // Operator detection might be confused by the '{'\n"
11401 " *BB // Operator detection might be confused by previous comment\n"
11402 "};");
11404 verifyIndependentOfContext("if (int *a = &b)");
11405 verifyIndependentOfContext("if (int &a = *b)");
11406 verifyIndependentOfContext("if (a & b[i])");
11407 verifyIndependentOfContext("if constexpr (a & b[i])");
11408 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11409 verifyIndependentOfContext("if (a * (b * c))");
11410 verifyIndependentOfContext("if constexpr (a * (b * c))");
11411 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11412 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11413 verifyIndependentOfContext("if (*b[i])");
11414 verifyIndependentOfContext("if (int *a = (&b))");
11415 verifyIndependentOfContext("while (int *a = &b)");
11416 verifyIndependentOfContext("while (a * (b * c))");
11417 verifyIndependentOfContext("size = sizeof *a;");
11418 verifyIndependentOfContext("if (a && (b = c))");
11419 verifyFormat("void f() {\n"
11420 " for (const int &v : Values) {\n"
11421 " }\n"
11422 "}");
11423 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11424 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11425 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11427 verifyFormat("#define A (!a * b)");
11428 verifyFormat("#define MACRO \\\n"
11429 " int *i = a * b; \\\n"
11430 " void f(a *b);",
11431 getLLVMStyleWithColumns(19));
11433 verifyIndependentOfContext("A = new SomeType *[Length];");
11434 verifyIndependentOfContext("A = new SomeType *[Length]();");
11435 verifyIndependentOfContext("T **t = new T *;");
11436 verifyIndependentOfContext("T **t = new T *();");
11437 verifyGoogleFormat("A = new SomeType*[Length]();");
11438 verifyGoogleFormat("A = new SomeType*[Length];");
11439 verifyGoogleFormat("T** t = new T*;");
11440 verifyGoogleFormat("T** t = new T*();");
11442 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11443 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11444 verifyFormat("template <bool a, bool b> "
11445 "typename t::if<x && y>::type f() {}");
11446 verifyFormat("template <int *y> f() {}");
11447 verifyFormat("vector<int *> v;");
11448 verifyFormat("vector<int *const> v;");
11449 verifyFormat("vector<int *const **const *> v;");
11450 verifyFormat("vector<int *volatile> v;");
11451 verifyFormat("vector<a *_Nonnull> v;");
11452 verifyFormat("vector<a *_Nullable> v;");
11453 verifyFormat("vector<a *_Null_unspecified> v;");
11454 verifyFormat("vector<a *__ptr32> v;");
11455 verifyFormat("vector<a *__ptr64> v;");
11456 verifyFormat("vector<a *__capability> v;");
11457 FormatStyle TypeMacros = getLLVMStyle();
11458 TypeMacros.TypenameMacros = {"LIST"};
11459 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
11460 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
11461 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
11462 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
11463 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
11465 FormatStyle CustomQualifier = getLLVMStyle();
11466 // Add identifiers that should not be parsed as a qualifier by default.
11467 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11468 CustomQualifier.AttributeMacros.push_back("_My_qualifier");
11469 CustomQualifier.AttributeMacros.push_back("my_other_qualifier");
11470 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11471 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
11472 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11473 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
11474 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11475 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
11476 verifyFormat("vector<a * _NotAQualifier> v;");
11477 verifyFormat("vector<a * __not_a_qualifier> v;");
11478 verifyFormat("vector<a * b> v;");
11479 verifyFormat("foo<b && false>();");
11480 verifyFormat("foo<b & 1>();");
11481 verifyFormat("foo<b & (1)>();");
11482 verifyFormat("foo<b & (~0)>();");
11483 verifyFormat("foo<b & (true)>();");
11484 verifyFormat("foo<b & ((1))>();");
11485 verifyFormat("foo<b & (/*comment*/ 1)>();");
11486 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
11487 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
11488 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
11489 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
11490 verifyFormat(
11491 "template <class T, class = typename std::enable_if<\n"
11492 " std::is_integral<T>::value &&\n"
11493 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
11494 "void F();",
11495 getLLVMStyleWithColumns(70));
11496 verifyFormat("template <class T,\n"
11497 " class = typename std::enable_if<\n"
11498 " std::is_integral<T>::value &&\n"
11499 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
11500 " class U>\n"
11501 "void F();",
11502 getLLVMStyleWithColumns(70));
11503 verifyFormat(
11504 "template <class T,\n"
11505 " class = typename ::std::enable_if<\n"
11506 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
11507 "void F();",
11508 getGoogleStyleWithColumns(68));
11510 FormatStyle Style = getLLVMStyle();
11511 Style.PointerAlignment = FormatStyle::PAS_Left;
11512 verifyFormat("struct {\n"
11513 "}* ptr;",
11514 Style);
11515 verifyFormat("union {\n"
11516 "}* ptr;",
11517 Style);
11518 verifyFormat("class {\n"
11519 "}* ptr;",
11520 Style);
11521 // Don't confuse a multiplication after a brace-initialized expression with
11522 // a class pointer.
11523 verifyFormat("int i = int{42} * 34;", Style);
11524 verifyFormat("struct {\n"
11525 "}&& ptr = {};",
11526 Style);
11527 verifyFormat("union {\n"
11528 "}&& ptr = {};",
11529 Style);
11530 verifyFormat("class {\n"
11531 "}&& ptr = {};",
11532 Style);
11533 verifyFormat("bool b = 3 == int{3} && true;");
11535 Style.PointerAlignment = FormatStyle::PAS_Middle;
11536 verifyFormat("struct {\n"
11537 "} * ptr;",
11538 Style);
11539 verifyFormat("union {\n"
11540 "} * ptr;",
11541 Style);
11542 verifyFormat("class {\n"
11543 "} * ptr;",
11544 Style);
11545 verifyFormat("struct {\n"
11546 "} && ptr = {};",
11547 Style);
11548 verifyFormat("union {\n"
11549 "} && ptr = {};",
11550 Style);
11551 verifyFormat("class {\n"
11552 "} && ptr = {};",
11553 Style);
11555 Style.PointerAlignment = FormatStyle::PAS_Right;
11556 verifyFormat("struct {\n"
11557 "} *ptr;",
11558 Style);
11559 verifyFormat("union {\n"
11560 "} *ptr;",
11561 Style);
11562 verifyFormat("class {\n"
11563 "} *ptr;",
11564 Style);
11565 verifyFormat("struct {\n"
11566 "} &&ptr = {};",
11567 Style);
11568 verifyFormat("union {\n"
11569 "} &&ptr = {};",
11570 Style);
11571 verifyFormat("class {\n"
11572 "} &&ptr = {};",
11573 Style);
11575 Style.PointerAlignment = FormatStyle::PAS_Left;
11576 verifyFormat("delete[] *ptr;", Style);
11577 verifyFormat("delete[] **ptr;", Style);
11578 verifyFormat("delete[] *(ptr);", Style);
11580 verifyIndependentOfContext("MACRO(int *i);");
11581 verifyIndependentOfContext("MACRO(auto *a);");
11582 verifyIndependentOfContext("MACRO(const A *a);");
11583 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
11584 verifyIndependentOfContext("MACRO(decltype(A) *a);");
11585 verifyIndependentOfContext("MACRO(typeof(A) *a);");
11586 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
11587 verifyIndependentOfContext("MACRO(A *const a);");
11588 verifyIndependentOfContext("MACRO(A *restrict a);");
11589 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
11590 verifyIndependentOfContext("MACRO(A *__restrict a);");
11591 verifyIndependentOfContext("MACRO(A *volatile a);");
11592 verifyIndependentOfContext("MACRO(A *__volatile a);");
11593 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
11594 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
11595 verifyIndependentOfContext("MACRO(A *_Nullable a);");
11596 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
11597 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
11598 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
11599 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
11600 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
11601 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
11602 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
11603 verifyIndependentOfContext("MACRO(A *__capability);");
11604 verifyIndependentOfContext("MACRO(A &__capability);");
11605 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
11606 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
11607 // If we add __my_qualifier to AttributeMacros it should always be parsed as
11608 // a type declaration:
11609 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
11610 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
11611 // Also check that TypenameMacros prevents parsing it as multiplication:
11612 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
11613 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
11615 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
11616 verifyFormat("void f() { f(float{1}, a * a); }");
11617 verifyFormat("void f() { f(float(1), a * a); }");
11619 verifyFormat("f((void (*)(int))g);");
11620 verifyFormat("f((void (&)(int))g);");
11621 verifyFormat("f((void (^)(int))g);");
11623 // FIXME: Is there a way to make this work?
11624 // verifyIndependentOfContext("MACRO(A *a);");
11625 verifyFormat("MACRO(A &B);");
11626 verifyFormat("MACRO(A *B);");
11627 verifyFormat("void f() { MACRO(A * B); }");
11628 verifyFormat("void f() { MACRO(A & B); }");
11630 // This lambda was mis-formatted after D88956 (treating it as a binop):
11631 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
11632 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
11633 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
11634 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
11636 verifyFormat("DatumHandle const *operator->() const { return input_; }");
11637 verifyFormat("return options != nullptr && operator==(*options);");
11639 verifyFormat("#define OP(x) \\\n"
11640 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11641 " return s << a.DebugString(); \\\n"
11642 " }",
11643 "#define OP(x) \\\n"
11644 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11645 " return s << a.DebugString(); \\\n"
11646 " }",
11647 getLLVMStyleWithColumns(50));
11649 verifyFormat("#define FOO \\\n"
11650 " void foo() { \\\n"
11651 " operator+(a * b); \\\n"
11652 " }",
11653 getLLVMStyleWithColumns(25));
11655 // FIXME: We cannot handle this case yet; we might be able to figure out that
11656 // foo<x> d > v; doesn't make sense.
11657 verifyFormat("foo<a<b && c> d> v;");
11659 FormatStyle PointerMiddle = getLLVMStyle();
11660 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11661 verifyFormat("delete *x;", PointerMiddle);
11662 verifyFormat("int * x;", PointerMiddle);
11663 verifyFormat("int *[] x;", PointerMiddle);
11664 verifyFormat("template <int * y> f() {}", PointerMiddle);
11665 verifyFormat("int * f(int * a) {}", PointerMiddle);
11666 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
11667 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
11668 verifyFormat("A<int *> a;", PointerMiddle);
11669 verifyFormat("A<int **> a;", PointerMiddle);
11670 verifyFormat("A<int *, int *> a;", PointerMiddle);
11671 verifyFormat("A<int *[]> a;", PointerMiddle);
11672 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
11673 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
11674 verifyFormat("T ** t = new T *;", PointerMiddle);
11676 // Member function reference qualifiers aren't binary operators.
11677 verifyFormat("string // break\n"
11678 "operator()() & {}");
11679 verifyFormat("string // break\n"
11680 "operator()() && {}");
11681 verifyGoogleFormat("template <typename T>\n"
11682 "auto x() & -> int {}");
11684 // Should be binary operators when used as an argument expression (overloaded
11685 // operator invoked as a member function).
11686 verifyFormat("void f() { a.operator()(a * a); }");
11687 verifyFormat("void f() { a->operator()(a & a); }");
11688 verifyFormat("void f() { a.operator()(*a & *a); }");
11689 verifyFormat("void f() { a->operator()(*a * *a); }");
11691 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
11692 verifyFormat("int operator()(T (&)[N]) { return 0; }");
11694 verifyFormat("val1 & val2;");
11695 verifyFormat("val1 & val2 & val3;");
11696 verifyFormat("class c {\n"
11697 " void func(type &a) { a & member; }\n"
11698 " anotherType &member;\n"
11699 "}");
11702 TEST_F(FormatTest, UnderstandsAttributes) {
11703 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
11704 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
11705 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
11706 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
11707 FormatStyle AfterType = getLLVMStyle();
11708 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11709 verifyFormat("__attribute__((nodebug)) void\n"
11710 "foo() {}",
11711 AfterType);
11712 verifyFormat("__unused void\n"
11713 "foo() {}",
11714 AfterType);
11716 FormatStyle CustomAttrs = getLLVMStyle();
11717 CustomAttrs.AttributeMacros.push_back("__unused");
11718 CustomAttrs.AttributeMacros.push_back("__attr1");
11719 CustomAttrs.AttributeMacros.push_back("__attr2");
11720 CustomAttrs.AttributeMacros.push_back("no_underscore_attr");
11721 verifyFormat("vector<SomeType *__attribute((foo))> v;");
11722 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
11723 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
11724 // Check that it is parsed as a multiplication without AttributeMacros and
11725 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
11726 verifyFormat("vector<SomeType * __attr1> v;");
11727 verifyFormat("vector<SomeType __attr1 *> v;");
11728 verifyFormat("vector<SomeType __attr1 *const> v;");
11729 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
11730 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
11731 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
11732 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
11733 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
11734 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
11735 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
11736 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
11737 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
11738 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
11739 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
11741 // Check that these are not parsed as function declarations:
11742 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11743 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
11744 verifyFormat("SomeType s(InitValue);", CustomAttrs);
11745 verifyFormat("SomeType s{InitValue};", CustomAttrs);
11746 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
11747 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
11748 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
11749 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
11750 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
11751 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
11754 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
11755 // Check that qualifiers on pointers don't break parsing of casts.
11756 verifyFormat("x = (foo *const)*v;");
11757 verifyFormat("x = (foo *volatile)*v;");
11758 verifyFormat("x = (foo *restrict)*v;");
11759 verifyFormat("x = (foo *__attribute__((foo)))*v;");
11760 verifyFormat("x = (foo *_Nonnull)*v;");
11761 verifyFormat("x = (foo *_Nullable)*v;");
11762 verifyFormat("x = (foo *_Null_unspecified)*v;");
11763 verifyFormat("x = (foo *_Nonnull)*v;");
11764 verifyFormat("x = (foo *[[clang::attr]])*v;");
11765 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
11766 verifyFormat("x = (foo *__ptr32)*v;");
11767 verifyFormat("x = (foo *__ptr64)*v;");
11768 verifyFormat("x = (foo *__capability)*v;");
11770 // Check that we handle multiple trailing qualifiers and skip them all to
11771 // determine that the expression is a cast to a pointer type.
11772 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999);
11773 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999);
11774 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
11775 StringRef AllQualifiers =
11776 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
11777 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
11778 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
11779 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
11781 // Also check that address-of is not parsed as a binary bitwise-and:
11782 verifyFormat("x = (foo *const)&v;");
11783 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
11784 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
11786 // Check custom qualifiers:
11787 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999);
11788 CustomQualifier.AttributeMacros.push_back("__my_qualifier");
11789 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
11790 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
11791 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
11792 CustomQualifier);
11793 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
11794 CustomQualifier);
11796 // Check that unknown identifiers result in binary operator parsing:
11797 verifyFormat("x = (foo * __unknown_qualifier) * v;");
11798 verifyFormat("x = (foo * __unknown_qualifier) & v;");
11801 TEST_F(FormatTest, UnderstandsSquareAttributes) {
11802 verifyFormat("SomeType s [[unused]] (InitValue);");
11803 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
11804 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
11805 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
11806 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
11807 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11808 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
11809 verifyFormat("[[nodiscard]] bool f() { return false; }");
11810 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
11811 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
11812 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
11813 verifyFormat("[[nodiscard]] ::qualified_type f();");
11815 // Make sure we do not mistake attributes for array subscripts.
11816 verifyFormat("int a() {}\n"
11817 "[[unused]] int b() {}");
11818 verifyFormat("NSArray *arr;\n"
11819 "arr[[Foo() bar]];");
11821 // On the other hand, we still need to correctly find array subscripts.
11822 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
11824 // Make sure that we do not mistake Objective-C method inside array literals
11825 // as attributes, even if those method names are also keywords.
11826 verifyFormat("@[ [foo bar] ];");
11827 verifyFormat("@[ [NSArray class] ];");
11828 verifyFormat("@[ [foo enum] ];");
11830 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
11832 // Make sure we do not parse attributes as lambda introducers.
11833 FormatStyle MultiLineFunctions = getLLVMStyle();
11834 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
11835 verifyFormat("[[unused]] int b() {\n"
11836 " return 42;\n"
11837 "}",
11838 MultiLineFunctions);
11841 TEST_F(FormatTest, AttributeClass) {
11842 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp);
11843 verifyFormat("class S {\n"
11844 " S(S&&) = default;\n"
11845 "};",
11846 Style);
11847 verifyFormat("class [[nodiscard]] S {\n"
11848 " S(S&&) = default;\n"
11849 "};",
11850 Style);
11851 verifyFormat("class __attribute((maybeunused)) S {\n"
11852 " S(S&&) = default;\n"
11853 "};",
11854 Style);
11855 verifyFormat("struct S {\n"
11856 " S(S&&) = default;\n"
11857 "};",
11858 Style);
11859 verifyFormat("struct [[nodiscard]] S {\n"
11860 " S(S&&) = default;\n"
11861 "};",
11862 Style);
11865 TEST_F(FormatTest, AttributesAfterMacro) {
11866 FormatStyle Style = getLLVMStyle();
11867 verifyFormat("MACRO;\n"
11868 "__attribute__((maybe_unused)) int foo() {\n"
11869 " //...\n"
11870 "}");
11872 verifyFormat("MACRO;\n"
11873 "[[nodiscard]] int foo() {\n"
11874 " //...\n"
11875 "}");
11877 verifyNoChange("MACRO\n\n"
11878 "__attribute__((maybe_unused)) int foo() {\n"
11879 " //...\n"
11880 "}");
11882 verifyNoChange("MACRO\n\n"
11883 "[[nodiscard]] int foo() {\n"
11884 " //...\n"
11885 "}");
11888 TEST_F(FormatTest, AttributePenaltyBreaking) {
11889 FormatStyle Style = getLLVMStyle();
11890 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
11891 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
11892 Style);
11893 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
11894 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
11895 Style);
11896 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
11897 "shared_ptr<ALongTypeName> &C d) {\n}",
11898 Style);
11901 TEST_F(FormatTest, UnderstandsEllipsis) {
11902 FormatStyle Style = getLLVMStyle();
11903 verifyFormat("int printf(const char *fmt, ...);");
11904 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
11905 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
11907 verifyFormat("template <int *...PP> a;", Style);
11909 Style.PointerAlignment = FormatStyle::PAS_Left;
11910 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
11912 verifyFormat("template <int*... PP> a;", Style);
11914 Style.PointerAlignment = FormatStyle::PAS_Middle;
11915 verifyFormat("template <int *... PP> a;", Style);
11918 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
11919 verifyFormat("int *a;\n"
11920 "int *a;\n"
11921 "int *a;",
11922 "int *a;\n"
11923 "int* a;\n"
11924 "int *a;",
11925 getGoogleStyle());
11926 verifyFormat("int* a;\n"
11927 "int* a;\n"
11928 "int* a;",
11929 "int* a;\n"
11930 "int* a;\n"
11931 "int *a;",
11932 getGoogleStyle());
11933 verifyFormat("int *a;\n"
11934 "int *a;\n"
11935 "int *a;",
11936 "int *a;\n"
11937 "int * a;\n"
11938 "int * a;",
11939 getGoogleStyle());
11940 verifyFormat("auto x = [] {\n"
11941 " int *a;\n"
11942 " int *a;\n"
11943 " int *a;\n"
11944 "};",
11945 "auto x=[]{int *a;\n"
11946 "int * a;\n"
11947 "int * a;};",
11948 getGoogleStyle());
11951 TEST_F(FormatTest, UnderstandsRvalueReferences) {
11952 verifyFormat("int f(int &&a) {}");
11953 verifyFormat("int f(int a, char &&b) {}");
11954 verifyFormat("void f() { int &&a = b; }");
11955 verifyGoogleFormat("int f(int a, char&& b) {}");
11956 verifyGoogleFormat("void f() { int&& a = b; }");
11958 verifyIndependentOfContext("A<int &&> a;");
11959 verifyIndependentOfContext("A<int &&, int &&> a;");
11960 verifyGoogleFormat("A<int&&> a;");
11961 verifyGoogleFormat("A<int&&, int&&> a;");
11963 // Not rvalue references:
11964 verifyFormat("template <bool B, bool C> class A {\n"
11965 " static_assert(B && C, \"Something is wrong\");\n"
11966 "};");
11967 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
11968 verifyFormat("template <typename T> struct S {\n"
11969 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
11970 "};");
11971 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
11972 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
11973 verifyFormat("#define A(a, b) (a && b)");
11976 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
11977 verifyFormat("void f() {\n"
11978 " x[aaaaaaaaa -\n"
11979 " b] = 23;\n"
11980 "}",
11981 getLLVMStyleWithColumns(15));
11984 TEST_F(FormatTest, FormatsCasts) {
11985 verifyFormat("Type *A = static_cast<Type *>(P);");
11986 verifyFormat("static_cast<Type *>(P);");
11987 verifyFormat("static_cast<Type &>(Fun)(Args);");
11988 verifyFormat("static_cast<Type &>(*Fun)(Args);");
11989 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
11990 // Check that static_cast<...>(...) does not require the next token to be on
11991 // the same line.
11992 verifyFormat("some_loooong_output << something_something__ << "
11993 "static_cast<const void *>(R)\n"
11994 " << something;");
11995 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
11996 verifyFormat("const_cast<Type &>(*Fun)(Args);");
11997 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
11998 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
11999 verifyFormat("Type *A = (Type *)P;");
12000 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12001 verifyFormat("int a = (int)(2.0f);");
12002 verifyFormat("int a = (int)2.0f;");
12003 verifyFormat("x[(int32)y];");
12004 verifyFormat("x = (int32)y;");
12005 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12006 verifyFormat("int a = (int)*b;");
12007 verifyFormat("int a = (int)2.0f;");
12008 verifyFormat("int a = (int)~0;");
12009 verifyFormat("int a = (int)++a;");
12010 verifyFormat("int a = (int)sizeof(int);");
12011 verifyFormat("int a = (int)+2;");
12012 verifyFormat("my_int a = (my_int)2.0f;");
12013 verifyFormat("my_int a = (my_int)sizeof(int);");
12014 verifyFormat("return (my_int)aaa;");
12015 verifyFormat("throw (my_int)aaa;");
12016 verifyFormat("#define x ((int)-1)");
12017 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12018 verifyFormat("#define p(q) ((int *)&q)");
12019 verifyFormat("fn(a)(b) + 1;");
12021 verifyFormat("void f() { my_int a = (my_int)*b; }");
12022 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12023 verifyFormat("my_int a = (my_int)~0;");
12024 verifyFormat("my_int a = (my_int)++a;");
12025 verifyFormat("my_int a = (my_int)-2;");
12026 verifyFormat("my_int a = (my_int)1;");
12027 verifyFormat("my_int a = (my_int *)1;");
12028 verifyFormat("my_int a = (const my_int)-1;");
12029 verifyFormat("my_int a = (const my_int *)-1;");
12030 verifyFormat("my_int a = (my_int)(my_int)-1;");
12031 verifyFormat("my_int a = (ns::my_int)-2;");
12032 verifyFormat("case (my_int)ONE:");
12033 verifyFormat("auto x = (X)this;");
12034 // Casts in Obj-C style calls used to not be recognized as such.
12035 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12037 // FIXME: single value wrapped with paren will be treated as cast.
12038 verifyFormat("void f(int i = (kValue)*kMask) {}");
12040 verifyFormat("{ (void)F; }");
12042 // Don't break after a cast's
12043 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12044 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12045 " bbbbbbbbbbbbbbbbbbbbbb);");
12047 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12048 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12049 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12050 verifyFormat("bool *y = (bool *)(void *)(x);");
12051 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12052 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12053 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12054 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12056 // These are not casts.
12057 verifyFormat("void f(int *) {}");
12058 verifyFormat("f(foo)->b;");
12059 verifyFormat("f(foo).b;");
12060 verifyFormat("f(foo)(b);");
12061 verifyFormat("f(foo)[b];");
12062 verifyFormat("[](foo) { return 4; }(bar);");
12063 verifyFormat("(*funptr)(foo)[4];");
12064 verifyFormat("funptrs[4](foo)[4];");
12065 verifyFormat("void f(int *);");
12066 verifyFormat("void f(int *) = 0;");
12067 verifyFormat("void f(SmallVector<int>) {}");
12068 verifyFormat("void f(SmallVector<int>);");
12069 verifyFormat("void f(SmallVector<int>) = 0;");
12070 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12071 verifyFormat("int a = sizeof(int) * b;");
12072 verifyGoogleFormat("int a = alignof(int) * b;");
12073 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12074 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12075 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12077 // These are not casts, but at some point were confused with casts.
12078 verifyFormat("virtual void foo(int *) override;");
12079 verifyFormat("virtual void foo(char &) const;");
12080 verifyFormat("virtual void foo(int *a, char *) const;");
12081 verifyFormat("int a = sizeof(int *) + b;");
12082 verifyGoogleFormat("int a = alignof(int *) + b;");
12083 verifyFormat("bool b = f(g<int>) && c;");
12084 verifyFormat("typedef void (*f)(int i) func;");
12085 verifyFormat("void operator++(int) noexcept;");
12086 verifyFormat("void operator++(int &) noexcept;");
12087 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12088 "&) noexcept;");
12089 verifyFormat(
12090 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12091 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12092 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12093 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12094 verifyFormat("void operator delete(foo &) noexcept;");
12095 verifyFormat("void operator delete(foo) noexcept;");
12096 verifyFormat("void operator delete(int) noexcept;");
12097 verifyFormat("void operator delete(int &) noexcept;");
12098 verifyFormat("void operator delete(int &) volatile noexcept;");
12099 verifyFormat("void operator delete(int &) const");
12100 verifyFormat("void operator delete(int &) = default");
12101 verifyFormat("void operator delete(int &) = delete");
12102 verifyFormat("void operator delete(int &) [[noreturn]]");
12103 verifyFormat("void operator delete(int &) throw();");
12104 verifyFormat("void operator delete(int &) throw(int);");
12105 verifyFormat("auto operator delete(int &) -> int;");
12106 verifyFormat("auto operator delete(int &) override");
12107 verifyFormat("auto operator delete(int &) final");
12109 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12110 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12111 // FIXME: The indentation here is not ideal.
12112 verifyFormat(
12113 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12114 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12115 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12118 TEST_F(FormatTest, FormatsFunctionTypes) {
12119 verifyFormat("A<bool()> a;");
12120 verifyFormat("A<SomeType()> a;");
12121 verifyFormat("A<void (*)(int, std::string)> a;");
12122 verifyFormat("A<void *(int)>;");
12123 verifyFormat("void *(*a)(int *, SomeType *);");
12124 verifyFormat("int (*func)(void *);");
12125 verifyFormat("void f() { int (*func)(void *); }");
12126 verifyFormat("template <class CallbackClass>\n"
12127 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12129 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12130 verifyGoogleFormat("void* (*a)(int);");
12131 verifyGoogleFormat(
12132 "template <class CallbackClass>\n"
12133 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12135 // Other constructs can look somewhat like function types:
12136 verifyFormat("A<sizeof(*x)> a;");
12137 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12138 verifyFormat("some_var = function(*some_pointer_var)[0];");
12139 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12140 verifyFormat("int x = f(&h)();");
12141 verifyFormat("returnsFunction(&param1, &param2)(param);");
12142 verifyFormat("std::function<\n"
12143 " LooooooooooongTemplatedType<\n"
12144 " SomeType>*(\n"
12145 " LooooooooooooooooongType type)>\n"
12146 " function;",
12147 getGoogleStyleWithColumns(40));
12150 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12151 verifyFormat("A (*foo_)[6];");
12152 verifyFormat("vector<int> (*foo_)[6];");
12155 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12156 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12157 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12158 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12159 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12160 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12161 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12163 // Different ways of ()-initializiation.
12164 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12165 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12166 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12167 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12168 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12169 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12170 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12171 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12173 // Lambdas should not confuse the variable declaration heuristic.
12174 verifyFormat("LooooooooooooooooongType\n"
12175 " variable(nullptr, [](A *a) {});",
12176 getLLVMStyleWithColumns(40));
12179 TEST_F(FormatTest, BreaksLongDeclarations) {
12180 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12181 " AnotherNameForTheLongType;");
12182 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12184 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12185 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12186 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12187 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12188 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12189 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12190 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12191 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12192 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12193 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12194 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12195 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12196 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12197 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12198 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12199 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12200 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12201 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12202 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12203 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12204 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12205 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12206 FormatStyle Indented = getLLVMStyle();
12207 Indented.IndentWrappedFunctionNames = true;
12208 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12209 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12210 Indented);
12211 verifyFormat(
12212 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12213 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12214 Indented);
12215 verifyFormat(
12216 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12217 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12218 Indented);
12219 verifyFormat(
12220 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12221 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12222 Indented);
12224 // FIXME: Without the comment, this breaks after "(".
12225 verifyGoogleFormat(
12226 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12227 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12229 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12230 " int LoooooooooooooooooooongParam2) {}");
12231 verifyFormat(
12232 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12233 " SourceLocation L, IdentifierIn *II,\n"
12234 " Type *T) {}");
12235 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12236 "ReallyReaaallyLongFunctionName(\n"
12237 " const std::string &SomeParameter,\n"
12238 " const SomeType<string, SomeOtherTemplateParameter>\n"
12239 " &ReallyReallyLongParameterName,\n"
12240 " const SomeType<string, SomeOtherTemplateParameter>\n"
12241 " &AnotherLongParameterName) {}");
12242 verifyFormat("template <typename A>\n"
12243 "SomeLoooooooooooooooooooooongType<\n"
12244 " typename some_namespace::SomeOtherType<A>::Type>\n"
12245 "Function() {}");
12247 verifyGoogleFormat(
12248 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12249 " aaaaaaaaaaaaaaaaaaaaaaa;");
12250 verifyGoogleFormat(
12251 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12252 " SourceLocation L) {}");
12253 verifyGoogleFormat(
12254 "some_namespace::LongReturnType\n"
12255 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12256 " int first_long_parameter, int second_parameter) {}");
12258 verifyGoogleFormat("template <typename T>\n"
12259 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12260 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12261 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12262 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12264 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12265 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12266 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12267 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12268 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12269 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12270 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12271 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12275 verifyFormat("template <typename T> // Templates on own line.\n"
12276 "static int // Some comment.\n"
12277 "MyFunction(int a);");
12280 TEST_F(FormatTest, FormatsAccessModifiers) {
12281 FormatStyle Style = getLLVMStyle();
12282 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
12283 FormatStyle::ELBAMS_LogicalBlock);
12284 verifyFormat("struct foo {\n"
12285 "private:\n"
12286 " void f() {}\n"
12287 "\n"
12288 "private:\n"
12289 " int i;\n"
12290 "\n"
12291 "protected:\n"
12292 " int j;\n"
12293 "};",
12294 Style);
12295 verifyFormat("struct foo {\n"
12296 "private:\n"
12297 " void f() {}\n"
12298 "\n"
12299 "private:\n"
12300 " int i;\n"
12301 "\n"
12302 "protected:\n"
12303 " int j;\n"
12304 "};",
12305 "struct foo {\n"
12306 "private:\n"
12307 " void f() {}\n"
12308 "private:\n"
12309 " int i;\n"
12310 "protected:\n"
12311 " int j;\n"
12312 "};",
12313 Style);
12314 verifyFormat("struct foo { /* comment */\n"
12315 "private:\n"
12316 " int i;\n"
12317 " // comment\n"
12318 "private:\n"
12319 " int j;\n"
12320 "};",
12321 Style);
12322 verifyFormat("struct foo {\n"
12323 "#ifdef FOO\n"
12324 "#endif\n"
12325 "private:\n"
12326 " int i;\n"
12327 "#ifdef FOO\n"
12328 "private:\n"
12329 "#endif\n"
12330 " int j;\n"
12331 "};",
12332 Style);
12333 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12334 verifyFormat("struct foo {\n"
12335 "private:\n"
12336 " void f() {}\n"
12337 "private:\n"
12338 " int i;\n"
12339 "protected:\n"
12340 " int j;\n"
12341 "};",
12342 Style);
12343 verifyFormat("struct foo {\n"
12344 "private:\n"
12345 " void f() {}\n"
12346 "private:\n"
12347 " int i;\n"
12348 "protected:\n"
12349 " int j;\n"
12350 "};",
12351 "struct foo {\n"
12352 "\n"
12353 "private:\n"
12354 " void f() {}\n"
12355 "\n"
12356 "private:\n"
12357 " int i;\n"
12358 "\n"
12359 "protected:\n"
12360 " int j;\n"
12361 "};",
12362 Style);
12363 verifyFormat("struct foo { /* comment */\n"
12364 "private:\n"
12365 " int i;\n"
12366 " // comment\n"
12367 "private:\n"
12368 " int j;\n"
12369 "};",
12370 "struct foo { /* comment */\n"
12371 "\n"
12372 "private:\n"
12373 " int i;\n"
12374 " // comment\n"
12375 "\n"
12376 "private:\n"
12377 " int j;\n"
12378 "};",
12379 Style);
12380 verifyFormat("struct foo {\n"
12381 "#ifdef FOO\n"
12382 "#endif\n"
12383 "private:\n"
12384 " int i;\n"
12385 "#ifdef FOO\n"
12386 "private:\n"
12387 "#endif\n"
12388 " int j;\n"
12389 "};",
12390 "struct foo {\n"
12391 "#ifdef FOO\n"
12392 "#endif\n"
12393 "\n"
12394 "private:\n"
12395 " int i;\n"
12396 "#ifdef FOO\n"
12397 "\n"
12398 "private:\n"
12399 "#endif\n"
12400 " int j;\n"
12401 "};",
12402 Style);
12403 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12404 verifyFormat("struct foo {\n"
12405 "private:\n"
12406 " void f() {}\n"
12407 "\n"
12408 "private:\n"
12409 " int i;\n"
12410 "\n"
12411 "protected:\n"
12412 " int j;\n"
12413 "};",
12414 Style);
12415 verifyFormat("struct foo {\n"
12416 "private:\n"
12417 " void f() {}\n"
12418 "\n"
12419 "private:\n"
12420 " int i;\n"
12421 "\n"
12422 "protected:\n"
12423 " int j;\n"
12424 "};",
12425 "struct foo {\n"
12426 "private:\n"
12427 " void f() {}\n"
12428 "private:\n"
12429 " int i;\n"
12430 "protected:\n"
12431 " int j;\n"
12432 "};",
12433 Style);
12434 verifyFormat("struct foo { /* comment */\n"
12435 "private:\n"
12436 " int i;\n"
12437 " // comment\n"
12438 "\n"
12439 "private:\n"
12440 " int j;\n"
12441 "};",
12442 Style);
12443 verifyFormat("struct foo {\n"
12444 "#ifdef FOO\n"
12445 "#endif\n"
12446 "\n"
12447 "private:\n"
12448 " int i;\n"
12449 "#ifdef FOO\n"
12450 "\n"
12451 "private:\n"
12452 "#endif\n"
12453 " int j;\n"
12454 "};",
12455 "struct foo {\n"
12456 "#ifdef FOO\n"
12457 "#endif\n"
12458 "private:\n"
12459 " int i;\n"
12460 "#ifdef FOO\n"
12461 "private:\n"
12462 "#endif\n"
12463 " int j;\n"
12464 "};",
12465 Style);
12466 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12467 verifyNoChange("struct foo {\n"
12468 "\n"
12469 "private:\n"
12470 " void f() {}\n"
12471 "\n"
12472 "private:\n"
12473 " int i;\n"
12474 "\n"
12475 "protected:\n"
12476 " int j;\n"
12477 "};",
12478 Style);
12479 verifyFormat("struct foo {\n"
12480 "private:\n"
12481 " void f() {}\n"
12482 "private:\n"
12483 " int i;\n"
12484 "protected:\n"
12485 " int j;\n"
12486 "};",
12487 Style);
12488 verifyNoChange("struct foo { /* comment */\n"
12489 "\n"
12490 "private:\n"
12491 " int i;\n"
12492 " // comment\n"
12493 "\n"
12494 "private:\n"
12495 " int j;\n"
12496 "};",
12497 Style);
12498 verifyFormat("struct foo { /* comment */\n"
12499 "private:\n"
12500 " int i;\n"
12501 " // comment\n"
12502 "private:\n"
12503 " int j;\n"
12504 "};",
12505 Style);
12506 verifyNoChange("struct foo {\n"
12507 "#ifdef FOO\n"
12508 "#endif\n"
12509 "\n"
12510 "private:\n"
12511 " int i;\n"
12512 "#ifdef FOO\n"
12513 "\n"
12514 "private:\n"
12515 "#endif\n"
12516 " int j;\n"
12517 "};",
12518 Style);
12519 verifyFormat("struct foo {\n"
12520 "#ifdef FOO\n"
12521 "#endif\n"
12522 "private:\n"
12523 " int i;\n"
12524 "#ifdef FOO\n"
12525 "private:\n"
12526 "#endif\n"
12527 " int j;\n"
12528 "};",
12529 Style);
12531 FormatStyle NoEmptyLines = getLLVMStyle();
12532 NoEmptyLines.MaxEmptyLinesToKeep = 0;
12533 verifyFormat("struct foo {\n"
12534 "private:\n"
12535 " void f() {}\n"
12536 "\n"
12537 "private:\n"
12538 " int i;\n"
12539 "\n"
12540 "public:\n"
12541 "protected:\n"
12542 " int j;\n"
12543 "};",
12544 NoEmptyLines);
12546 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12547 verifyFormat("struct foo {\n"
12548 "private:\n"
12549 " void f() {}\n"
12550 "private:\n"
12551 " int i;\n"
12552 "public:\n"
12553 "protected:\n"
12554 " int j;\n"
12555 "};",
12556 NoEmptyLines);
12558 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12559 verifyFormat("struct foo {\n"
12560 "private:\n"
12561 " void f() {}\n"
12562 "\n"
12563 "private:\n"
12564 " int i;\n"
12565 "\n"
12566 "public:\n"
12567 "\n"
12568 "protected:\n"
12569 " int j;\n"
12570 "};",
12571 NoEmptyLines);
12574 TEST_F(FormatTest, FormatsAfterAccessModifiers) {
12576 FormatStyle Style = getLLVMStyle();
12577 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
12578 verifyFormat("struct foo {\n"
12579 "private:\n"
12580 " void f() {}\n"
12581 "\n"
12582 "private:\n"
12583 " int i;\n"
12584 "\n"
12585 "protected:\n"
12586 " int j;\n"
12587 "};",
12588 Style);
12590 // Check if lines are removed.
12591 verifyFormat("struct foo {\n"
12592 "private:\n"
12593 " void f() {}\n"
12594 "\n"
12595 "private:\n"
12596 " int i;\n"
12597 "\n"
12598 "protected:\n"
12599 " int j;\n"
12600 "};",
12601 "struct foo {\n"
12602 "private:\n"
12603 "\n"
12604 " void f() {}\n"
12605 "\n"
12606 "private:\n"
12607 "\n"
12608 " int i;\n"
12609 "\n"
12610 "protected:\n"
12611 "\n"
12612 " int j;\n"
12613 "};",
12614 Style);
12616 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12617 verifyFormat("struct foo {\n"
12618 "private:\n"
12619 "\n"
12620 " void f() {}\n"
12621 "\n"
12622 "private:\n"
12623 "\n"
12624 " int i;\n"
12625 "\n"
12626 "protected:\n"
12627 "\n"
12628 " int j;\n"
12629 "};",
12630 Style);
12632 // Check if lines are added.
12633 verifyFormat("struct foo {\n"
12634 "private:\n"
12635 "\n"
12636 " void f() {}\n"
12637 "\n"
12638 "private:\n"
12639 "\n"
12640 " int i;\n"
12641 "\n"
12642 "protected:\n"
12643 "\n"
12644 " int j;\n"
12645 "};",
12646 "struct foo {\n"
12647 "private:\n"
12648 " void f() {}\n"
12649 "\n"
12650 "private:\n"
12651 " int i;\n"
12652 "\n"
12653 "protected:\n"
12654 " int j;\n"
12655 "};",
12656 Style);
12658 // Leave tests rely on the code layout, test::messUp can not be used.
12659 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12660 Style.MaxEmptyLinesToKeep = 0u;
12661 verifyFormat("struct foo {\n"
12662 "private:\n"
12663 " void f() {}\n"
12664 "\n"
12665 "private:\n"
12666 " int i;\n"
12667 "\n"
12668 "protected:\n"
12669 " int j;\n"
12670 "};",
12671 Style);
12673 // Check if MaxEmptyLinesToKeep is respected.
12674 verifyFormat("struct foo {\n"
12675 "private:\n"
12676 " void f() {}\n"
12677 "\n"
12678 "private:\n"
12679 " int i;\n"
12680 "\n"
12681 "protected:\n"
12682 " int j;\n"
12683 "};",
12684 "struct foo {\n"
12685 "private:\n"
12686 "\n\n\n"
12687 " void f() {}\n"
12688 "\n"
12689 "private:\n"
12690 "\n\n\n"
12691 " int i;\n"
12692 "\n"
12693 "protected:\n"
12694 "\n\n\n"
12695 " int j;\n"
12696 "};",
12697 Style);
12699 Style.MaxEmptyLinesToKeep = 1u;
12700 verifyNoChange("struct foo {\n"
12701 "private:\n"
12702 "\n"
12703 " void f() {}\n"
12704 "\n"
12705 "private:\n"
12706 "\n"
12707 " int i;\n"
12708 "\n"
12709 "protected:\n"
12710 "\n"
12711 " int j;\n"
12712 "};",
12713 Style);
12714 // Check if no lines are kept.
12715 verifyFormat("struct foo {\n"
12716 "private:\n"
12717 " void f() {}\n"
12718 "\n"
12719 "private:\n"
12720 " int i;\n"
12721 "\n"
12722 "protected:\n"
12723 " int j;\n"
12724 "};",
12725 Style);
12726 // Check if MaxEmptyLinesToKeep is respected.
12727 verifyFormat("struct foo {\n"
12728 "private:\n"
12729 "\n"
12730 " void f() {}\n"
12731 "\n"
12732 "private:\n"
12733 "\n"
12734 " int i;\n"
12735 "\n"
12736 "protected:\n"
12737 "\n"
12738 " int j;\n"
12739 "};",
12740 "struct foo {\n"
12741 "private:\n"
12742 "\n\n\n"
12743 " void f() {}\n"
12744 "\n"
12745 "private:\n"
12746 "\n\n\n"
12747 " int i;\n"
12748 "\n"
12749 "protected:\n"
12750 "\n\n\n"
12751 " int j;\n"
12752 "};",
12753 Style);
12755 Style.MaxEmptyLinesToKeep = 10u;
12756 verifyNoChange("struct foo {\n"
12757 "private:\n"
12758 "\n\n\n"
12759 " void f() {}\n"
12760 "\n"
12761 "private:\n"
12762 "\n\n\n"
12763 " int i;\n"
12764 "\n"
12765 "protected:\n"
12766 "\n\n\n"
12767 " int j;\n"
12768 "};",
12769 Style);
12771 // Test with comments.
12772 Style = getLLVMStyle();
12773 verifyFormat("struct foo {\n"
12774 "private:\n"
12775 " // comment\n"
12776 " void f() {}\n"
12777 "\n"
12778 "private: /* comment */\n"
12779 " int i;\n"
12780 "};",
12781 Style);
12782 verifyFormat("struct foo {\n"
12783 "private:\n"
12784 " // comment\n"
12785 " void f() {}\n"
12786 "\n"
12787 "private: /* comment */\n"
12788 " int i;\n"
12789 "};",
12790 "struct foo {\n"
12791 "private:\n"
12792 "\n"
12793 " // comment\n"
12794 " void f() {}\n"
12795 "\n"
12796 "private: /* comment */\n"
12797 "\n"
12798 " int i;\n"
12799 "};",
12800 Style);
12802 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12803 verifyFormat("struct foo {\n"
12804 "private:\n"
12805 "\n"
12806 " // comment\n"
12807 " void f() {}\n"
12808 "\n"
12809 "private: /* comment */\n"
12810 "\n"
12811 " int i;\n"
12812 "};",
12813 "struct foo {\n"
12814 "private:\n"
12815 " // comment\n"
12816 " void f() {}\n"
12817 "\n"
12818 "private: /* comment */\n"
12819 " int i;\n"
12820 "};",
12821 Style);
12822 verifyFormat("struct foo {\n"
12823 "private:\n"
12824 "\n"
12825 " // comment\n"
12826 " void f() {}\n"
12827 "\n"
12828 "private: /* comment */\n"
12829 "\n"
12830 " int i;\n"
12831 "};",
12832 Style);
12834 // Test with preprocessor defines.
12835 Style = getLLVMStyle();
12836 verifyFormat("struct foo {\n"
12837 "private:\n"
12838 "#ifdef FOO\n"
12839 "#endif\n"
12840 " void f() {}\n"
12841 "};",
12842 Style);
12843 verifyFormat("struct foo {\n"
12844 "private:\n"
12845 "#ifdef FOO\n"
12846 "#endif\n"
12847 " void f() {}\n"
12848 "};",
12849 "struct foo {\n"
12850 "private:\n"
12851 "\n"
12852 "#ifdef FOO\n"
12853 "#endif\n"
12854 " void f() {}\n"
12855 "};",
12856 Style);
12857 verifyNoChange("struct foo {\n"
12858 "#ifdef FOO\n"
12859 "#else\n"
12860 "private:\n"
12861 "\n"
12862 "#endif\n"
12863 "};",
12864 Style);
12865 verifyFormat("struct foo {\n"
12866 "#ifdef FOO\n"
12867 "#else\n"
12868 "private:\n"
12869 "\n"
12870 "#endif\n"
12871 "};",
12872 "struct foo {\n"
12873 "#ifdef FOO\n"
12874 "#else\n"
12875 "private:\n"
12876 "\n"
12877 "\n"
12878 "#endif\n"
12879 "};",
12880 Style);
12881 verifyFormat("struct foo {\n"
12882 "#ifdef FOO\n"
12883 "private:\n"
12884 "#else\n"
12885 "#endif\n"
12886 "};",
12887 "struct foo {\n"
12888 "#ifdef FOO\n"
12889 "private:\n"
12890 "\n"
12891 "\n"
12892 "#else\n"
12893 "#endif\n"
12894 "};",
12895 Style);
12896 verifyFormat("struct foo {\n"
12897 "#if 0\n"
12898 "#else\n"
12899 "#endif\n"
12900 "#ifdef FOO\n"
12901 "private:\n"
12902 "#endif\n"
12903 "};",
12904 "struct foo {\n"
12905 "#if 0\n"
12906 "#else\n"
12907 "#endif\n"
12908 "#ifdef FOO\n"
12909 "private:\n"
12910 "\n"
12911 "\n"
12912 "#endif\n"
12913 "};",
12914 Style);
12916 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12917 verifyFormat("struct foo {\n"
12918 "private:\n"
12919 "\n"
12920 "#ifdef FOO\n"
12921 "#endif\n"
12922 " void f() {}\n"
12923 "};",
12924 "struct foo {\n"
12925 "private:\n"
12926 "#ifdef FOO\n"
12927 "#endif\n"
12928 " void f() {}\n"
12929 "};",
12930 Style);
12931 verifyFormat("struct foo {\n"
12932 "private:\n"
12933 "\n"
12934 "#ifdef FOO\n"
12935 "#endif\n"
12936 " void f() {}\n"
12937 "};",
12938 Style);
12941 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
12942 // Combined tests of EmptyLineAfterAccessModifier and
12943 // EmptyLineBeforeAccessModifier.
12944 FormatStyle Style = getLLVMStyle();
12945 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12946 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12947 verifyFormat("struct foo {\n"
12948 "private:\n"
12949 "\n"
12950 "protected:\n"
12951 "};",
12952 Style);
12954 Style.MaxEmptyLinesToKeep = 10u;
12955 // Both remove all new lines.
12956 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12957 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
12958 verifyFormat("struct foo {\n"
12959 "private:\n"
12960 "protected:\n"
12961 "};",
12962 "struct foo {\n"
12963 "private:\n"
12964 "\n\n\n"
12965 "protected:\n"
12966 "};",
12967 Style);
12969 // Leave tests rely on the code layout, test::messUp can not be used.
12970 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12971 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12972 Style.MaxEmptyLinesToKeep = 10u;
12973 verifyNoChange("struct foo {\n"
12974 "private:\n"
12975 "\n\n\n"
12976 "protected:\n"
12977 "};",
12978 Style);
12979 Style.MaxEmptyLinesToKeep = 3u;
12980 verifyNoChange("struct foo {\n"
12981 "private:\n"
12982 "\n\n\n"
12983 "protected:\n"
12984 "};",
12985 Style);
12986 Style.MaxEmptyLinesToKeep = 1u;
12987 verifyNoChange("struct foo {\n"
12988 "private:\n"
12989 "\n\n\n"
12990 "protected:\n"
12991 "};",
12992 Style); // Based on new lines in original document and not
12993 // on the setting.
12995 Style.MaxEmptyLinesToKeep = 10u;
12996 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12997 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12998 // Newlines are kept if they are greater than zero,
12999 // test::messUp removes all new lines which changes the logic
13000 verifyNoChange("struct foo {\n"
13001 "private:\n"
13002 "\n\n\n"
13003 "protected:\n"
13004 "};",
13005 Style);
13007 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13008 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13009 // test::messUp removes all new lines which changes the logic
13010 verifyNoChange("struct foo {\n"
13011 "private:\n"
13012 "\n\n\n"
13013 "protected:\n"
13014 "};",
13015 Style);
13017 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13018 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13019 verifyNoChange("struct foo {\n"
13020 "private:\n"
13021 "\n\n\n"
13022 "protected:\n"
13023 "};",
13024 Style); // test::messUp removes all new lines which changes
13025 // the logic.
13027 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13028 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13029 verifyFormat("struct foo {\n"
13030 "private:\n"
13031 "protected:\n"
13032 "};",
13033 "struct foo {\n"
13034 "private:\n"
13035 "\n\n\n"
13036 "protected:\n"
13037 "};",
13038 Style);
13040 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13041 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13042 verifyNoChange("struct foo {\n"
13043 "private:\n"
13044 "\n\n\n"
13045 "protected:\n"
13046 "};",
13047 Style); // test::messUp removes all new lines which changes
13048 // the logic.
13050 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13051 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13052 verifyFormat("struct foo {\n"
13053 "private:\n"
13054 "protected:\n"
13055 "};",
13056 "struct foo {\n"
13057 "private:\n"
13058 "\n\n\n"
13059 "protected:\n"
13060 "};",
13061 Style);
13063 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13064 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13065 verifyFormat("struct foo {\n"
13066 "private:\n"
13067 "protected:\n"
13068 "};",
13069 "struct foo {\n"
13070 "private:\n"
13071 "\n\n\n"
13072 "protected:\n"
13073 "};",
13074 Style);
13076 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13077 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13078 verifyFormat("struct foo {\n"
13079 "private:\n"
13080 "protected:\n"
13081 "};",
13082 "struct foo {\n"
13083 "private:\n"
13084 "\n\n\n"
13085 "protected:\n"
13086 "};",
13087 Style);
13089 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13090 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13091 verifyFormat("struct foo {\n"
13092 "private:\n"
13093 "protected:\n"
13094 "};",
13095 "struct foo {\n"
13096 "private:\n"
13097 "\n\n\n"
13098 "protected:\n"
13099 "};",
13100 Style);
13103 TEST_F(FormatTest, FormatsArrays) {
13104 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13105 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13106 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13107 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13108 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13109 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13110 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13111 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13112 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13113 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13114 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13115 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13116 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13117 verifyFormat(
13118 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13119 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13120 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13121 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13122 " .aaaaaaaaaaaaaaaaaaaaaa();");
13124 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13126 verifyFormat(
13127 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13128 " .aaaaaaa[0]\n"
13129 " .aaaaaaaaaaaaaaaaaaaaaa();");
13130 verifyFormat("a[::b::c];");
13132 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13134 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13135 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13138 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13139 verifyFormat("(a)->b();");
13140 verifyFormat("--a;");
13143 TEST_F(FormatTest, HandlesIncludeDirectives) {
13144 verifyFormat("#include <string>\n"
13145 "#include <a/b/c.h>\n"
13146 "#include \"a/b/string\"\n"
13147 "#include \"string.h\"\n"
13148 "#include \"string.h\"\n"
13149 "#include <a-a>\n"
13150 "#include < path with space >\n"
13151 "#include_next <test.h>"
13152 "#include \"abc.h\" // this is included for ABC\n"
13153 "#include \"some long include\" // with a comment\n"
13154 "#include \"some very long include path\"\n"
13155 "#include <some/very/long/include/path>",
13156 getLLVMStyleWithColumns(35));
13157 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13158 verifyFormat("#include <a>", "#include<a>");
13160 verifyFormat("#import <string>");
13161 verifyFormat("#import <a/b/c.h>");
13162 verifyFormat("#import \"a/b/string\"");
13163 verifyFormat("#import \"string.h\"");
13164 verifyFormat("#import \"string.h\"");
13165 verifyFormat("#if __has_include(<strstream>)\n"
13166 "#include <strstream>\n"
13167 "#endif");
13169 verifyFormat("#define MY_IMPORT <a/b>");
13171 verifyFormat("#if __has_include(<a/b>)");
13172 verifyFormat("#if __has_include_next(<a/b>)");
13173 verifyFormat("#define F __has_include(<a/b>)");
13174 verifyFormat("#define F __has_include_next(<a/b>)");
13176 // Protocol buffer definition or missing "#".
13177 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13178 getLLVMStyleWithColumns(30));
13180 FormatStyle Style = getLLVMStyle();
13181 Style.AlwaysBreakBeforeMultilineStrings = true;
13182 Style.ColumnLimit = 0;
13183 verifyFormat("#import \"abc.h\"", Style);
13185 // But 'import' might also be a regular C++ namespace.
13186 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13188 verifyFormat("import::Bar foo(val ? 2 : 1);");
13191 //===----------------------------------------------------------------------===//
13192 // Error recovery tests.
13193 //===----------------------------------------------------------------------===//
13195 TEST_F(FormatTest, IncompleteParameterLists) {
13196 FormatStyle NoBinPacking = getLLVMStyle();
13197 NoBinPacking.BinPackParameters = false;
13198 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13199 " double *min_x,\n"
13200 " double *max_x,\n"
13201 " double *min_y,\n"
13202 " double *max_y,\n"
13203 " double *min_z,\n"
13204 " double *max_z, ) {}",
13205 NoBinPacking);
13208 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13209 verifyFormat("void f() { return; }\n42");
13210 verifyFormat("void f() {\n"
13211 " if (0)\n"
13212 " return;\n"
13213 "}\n"
13214 "42");
13215 verifyFormat("void f() { return }\n42");
13216 verifyFormat("void f() {\n"
13217 " if (0)\n"
13218 " return\n"
13219 "}\n"
13220 "42");
13223 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13224 verifyFormat("void f() { return }", "void f ( ) { return }");
13225 verifyFormat("void f() {\n"
13226 " if (a)\n"
13227 " return\n"
13228 "}",
13229 "void f ( ) { if ( a ) return }");
13230 verifyFormat("namespace N {\n"
13231 "void f()\n"
13232 "}",
13233 "namespace N { void f() }");
13234 verifyFormat("namespace N {\n"
13235 "void f() {}\n"
13236 "void g()\n"
13237 "} // namespace N",
13238 "namespace N { void f( ) { } void g( ) }");
13241 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13242 verifyFormat("int aaaaaaaa =\n"
13243 " // Overlylongcomment\n"
13244 " b;",
13245 getLLVMStyleWithColumns(20));
13246 verifyFormat("function(\n"
13247 " ShortArgument,\n"
13248 " LoooooooooooongArgument);",
13249 getLLVMStyleWithColumns(20));
13252 TEST_F(FormatTest, IncorrectAccessSpecifier) {
13253 verifyFormat("public:");
13254 verifyFormat("class A {\n"
13255 "public\n"
13256 " void f() {}\n"
13257 "};");
13258 verifyFormat("public\n"
13259 "int qwerty;");
13260 verifyFormat("public\n"
13261 "B {}");
13262 verifyFormat("public\n"
13263 "{}");
13264 verifyFormat("public\n"
13265 "B { int x; }");
13268 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
13269 verifyFormat("{");
13270 verifyFormat("#})");
13271 verifyNoCrash("(/**/[:!] ?[).");
13274 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
13275 // Found by oss-fuzz:
13276 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13277 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
13278 Style.ColumnLimit = 60;
13279 verifyNoCrash(
13280 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13281 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13282 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13283 Style);
13286 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
13287 verifyFormat("do {\n}");
13288 verifyFormat("do {\n}\n"
13289 "f();");
13290 verifyFormat("do {\n}\n"
13291 "wheeee(fun);");
13292 verifyFormat("do {\n"
13293 " f();\n"
13294 "}");
13297 TEST_F(FormatTest, IncorrectCodeMissingParens) {
13298 verifyFormat("if {\n foo;\n foo();\n}");
13299 verifyFormat("switch {\n foo;\n foo();\n}");
13300 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13301 verifyIncompleteFormat("ERROR: for target;");
13302 verifyFormat("while {\n foo;\n foo();\n}");
13303 verifyFormat("do {\n foo;\n foo();\n} while;");
13306 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
13307 verifyIncompleteFormat("namespace {\n"
13308 "class Foo { Foo (\n"
13309 "};\n"
13310 "} // namespace");
13313 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13314 verifyFormat("{\n {}", "{\n{\n}");
13315 verifyFormat("{\n {}", "{\n {\n}");
13316 verifyFormat("{\n {}", "{\n {\n }");
13317 verifyFormat("{\n {}\n}\n}", "{\n {\n }\n }\n}");
13319 verifyFormat("{\n"
13320 " {\n"
13321 " breakme(\n"
13322 " qwe);\n"
13323 " }",
13324 "{\n"
13325 " {\n"
13326 " breakme(qwe);\n"
13327 "}",
13328 getLLVMStyleWithColumns(10));
13331 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
13332 verifyFormat("int x = {\n"
13333 " avariable,\n"
13334 " b(alongervariable)};",
13335 getLLVMStyleWithColumns(25));
13338 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
13339 verifyFormat("return (a)(b){1, 2, 3};");
13342 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
13343 verifyFormat("vector<int> x{1, 2, 3, 4};");
13344 verifyFormat("vector<int> x{\n"
13345 " 1,\n"
13346 " 2,\n"
13347 " 3,\n"
13348 " 4,\n"
13349 "};");
13350 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13351 verifyFormat("f({1, 2});");
13352 verifyFormat("auto v = Foo{-1};");
13353 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13354 verifyFormat("Class::Class : member{1, 2, 3} {}");
13355 verifyFormat("new vector<int>{1, 2, 3};");
13356 verifyFormat("new int[3]{1, 2, 3};");
13357 verifyFormat("new int{1};");
13358 verifyFormat("return {arg1, arg2};");
13359 verifyFormat("return {arg1, SomeType{parameter}};");
13360 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13361 verifyFormat("new T{arg1, arg2};");
13362 verifyFormat("f(MyMap[{composite, key}]);");
13363 verifyFormat("class Class {\n"
13364 " T member = {arg1, arg2};\n"
13365 "};");
13366 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13367 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13368 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13369 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13370 verifyFormat("int a = std::is_integral<int>{} + 0;");
13372 verifyFormat("int foo(int i) { return fo1{}(i); }");
13373 verifyFormat("int foo(int i) { return fo1{}(i); }");
13374 verifyFormat("auto i = decltype(x){};");
13375 verifyFormat("auto i = typeof(x){};");
13376 verifyFormat("auto i = _Atomic(x){};");
13377 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13378 verifyFormat("Node n{1, Node{1000}, //\n"
13379 " 2};");
13380 verifyFormat("Aaaa aaaaaaa{\n"
13381 " {\n"
13382 " aaaa,\n"
13383 " },\n"
13384 "};");
13385 verifyFormat("class C : public D {\n"
13386 " SomeClass SC{2};\n"
13387 "};");
13388 verifyFormat("class C : public A {\n"
13389 " class D : public B {\n"
13390 " void f() { int i{2}; }\n"
13391 " };\n"
13392 "};");
13393 verifyFormat("#define A {a, a},");
13394 // Don't confuse braced list initializers with compound statements.
13395 verifyFormat(
13396 "class A {\n"
13397 " A() : a{} {}\n"
13398 " A() : Base<int>{} {}\n"
13399 " A() : Base<Foo<int>>{} {}\n"
13400 " A(int b) : b(b) {}\n"
13401 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13402 " int a, b;\n"
13403 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13404 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13405 "{}\n"
13406 "};");
13408 // Avoid breaking between equal sign and opening brace
13409 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
13410 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
13411 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13412 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13413 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13414 " {\"ccccccccccccccccccccc\", 2}};",
13415 AvoidBreakingFirstArgument);
13417 // Binpacking only if there is no trailing comma
13418 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13419 " cccccccccc, dddddddddd};",
13420 getLLVMStyleWithColumns(50));
13421 verifyFormat("const Aaaaaa aaaaa = {\n"
13422 " aaaaaaaaaaa,\n"
13423 " bbbbbbbbbbb,\n"
13424 " ccccccccccc,\n"
13425 " ddddddddddd,\n"
13426 "};",
13427 getLLVMStyleWithColumns(50));
13429 // Cases where distinguising braced lists and blocks is hard.
13430 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13431 verifyFormat("void f() {\n"
13432 " return; // comment\n"
13433 "}\n"
13434 "SomeType t;");
13435 verifyFormat("void f() {\n"
13436 " if (a) {\n"
13437 " f();\n"
13438 " }\n"
13439 "}\n"
13440 "SomeType t;");
13442 // In combination with BinPackArguments = false.
13443 FormatStyle NoBinPacking = getLLVMStyle();
13444 NoBinPacking.BinPackArguments = false;
13445 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
13446 " bbbbb,\n"
13447 " ccccc,\n"
13448 " ddddd,\n"
13449 " eeeee,\n"
13450 " ffffff,\n"
13451 " ggggg,\n"
13452 " hhhhhh,\n"
13453 " iiiiii,\n"
13454 " jjjjjj,\n"
13455 " kkkkkk};",
13456 NoBinPacking);
13457 verifyFormat("const Aaaaaa aaaaa = {\n"
13458 " aaaaa,\n"
13459 " bbbbb,\n"
13460 " ccccc,\n"
13461 " ddddd,\n"
13462 " eeeee,\n"
13463 " ffffff,\n"
13464 " ggggg,\n"
13465 " hhhhhh,\n"
13466 " iiiiii,\n"
13467 " jjjjjj,\n"
13468 " kkkkkk,\n"
13469 "};",
13470 NoBinPacking);
13471 verifyFormat(
13472 "const Aaaaaa aaaaa = {\n"
13473 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
13474 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
13475 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
13476 "};",
13477 NoBinPacking);
13479 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13480 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
13481 " CDDDP83848_BMCR_REGISTER,\n"
13482 " CDDDP83848_BMSR_REGISTER,\n"
13483 " CDDDP83848_RBR_REGISTER};",
13484 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
13485 " CDDDP83848_BMSR_REGISTER,\n"
13486 " CDDDP83848_RBR_REGISTER};",
13487 NoBinPacking);
13489 // FIXME: The alignment of these trailing comments might be bad. Then again,
13490 // this might be utterly useless in real code.
13491 verifyFormat("Constructor::Constructor()\n"
13492 " : some_value{ //\n"
13493 " aaaaaaa, //\n"
13494 " bbbbbbb} {}");
13496 // In braced lists, the first comment is always assumed to belong to the
13497 // first element. Thus, it can be moved to the next or previous line as
13498 // appropriate.
13499 verifyFormat("function({// First element:\n"
13500 " 1,\n"
13501 " // Second element:\n"
13502 " 2});",
13503 "function({\n"
13504 " // First element:\n"
13505 " 1,\n"
13506 " // Second element:\n"
13507 " 2});");
13508 verifyFormat("std::vector<int> MyNumbers{\n"
13509 " // First element:\n"
13510 " 1,\n"
13511 " // Second element:\n"
13512 " 2};",
13513 "std::vector<int> MyNumbers{// First element:\n"
13514 " 1,\n"
13515 " // Second element:\n"
13516 " 2};",
13517 getLLVMStyleWithColumns(30));
13518 // A trailing comma should still lead to an enforced line break and no
13519 // binpacking.
13520 verifyFormat("vector<int> SomeVector = {\n"
13521 " // aaa\n"
13522 " 1,\n"
13523 " 2,\n"
13524 "};",
13525 "vector<int> SomeVector = { // aaa\n"
13526 " 1, 2, };");
13528 // C++11 brace initializer list l-braces should not be treated any differently
13529 // when breaking before lambda bodies is enabled
13530 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
13531 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
13532 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
13533 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
13534 verifyFormat(
13535 "std::runtime_error{\n"
13536 " \"Long string which will force a break onto the next line...\"};",
13537 BreakBeforeLambdaBody);
13539 FormatStyle ExtraSpaces = getLLVMStyle();
13540 ExtraSpaces.Cpp11BracedListStyle = false;
13541 ExtraSpaces.ColumnLimit = 75;
13542 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
13543 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
13544 verifyFormat("f({ 1, 2 });", ExtraSpaces);
13545 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
13546 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
13547 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
13548 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
13549 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
13550 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
13551 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
13552 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
13553 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
13554 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
13555 verifyFormat("class Class {\n"
13556 " T member = { arg1, arg2 };\n"
13557 "};",
13558 ExtraSpaces);
13559 verifyFormat(
13560 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13561 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
13562 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
13563 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
13564 ExtraSpaces);
13565 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
13566 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
13567 ExtraSpaces);
13568 verifyFormat(
13569 "someFunction(OtherParam,\n"
13570 " BracedList{ // comment 1 (Forcing interesting break)\n"
13571 " param1, param2,\n"
13572 " // comment 2\n"
13573 " param3, param4 });",
13574 ExtraSpaces);
13575 verifyFormat(
13576 "std::this_thread::sleep_for(\n"
13577 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
13578 ExtraSpaces);
13579 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
13580 " aaaaaaa,\n"
13581 " aaaaaaaaaa,\n"
13582 " aaaaa,\n"
13583 " aaaaaaaaaaaaaaa,\n"
13584 " aaa,\n"
13585 " aaaaaaaaaa,\n"
13586 " a,\n"
13587 " aaaaaaaaaaaaaaaaaaaaa,\n"
13588 " aaaaaaaaaaaa,\n"
13589 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
13590 " aaaaaaa,\n"
13591 " a};");
13592 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
13593 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
13594 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
13596 // Avoid breaking between initializer/equal sign and opening brace
13597 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
13598 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
13599 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13600 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13601 " { \"ccccccccccccccccccccc\", 2 }\n"
13602 "};",
13603 ExtraSpaces);
13604 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
13605 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13606 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13607 " { \"ccccccccccccccccccccc\", 2 }\n"
13608 "};",
13609 ExtraSpaces);
13611 FormatStyle SpaceBeforeBrace = getLLVMStyle();
13612 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
13613 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
13614 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
13616 FormatStyle SpaceBetweenBraces = getLLVMStyle();
13617 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
13618 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
13619 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
13620 SpaceBetweenBraces.SpacesInSquareBrackets = true;
13621 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
13622 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
13623 verifyFormat("vector< int > x{ // comment 1\n"
13624 " 1, 2, 3, 4 };",
13625 SpaceBetweenBraces);
13626 SpaceBetweenBraces.ColumnLimit = 20;
13627 verifyFormat("vector< int > x{\n"
13628 " 1, 2, 3, 4 };",
13629 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
13630 SpaceBetweenBraces.ColumnLimit = 24;
13631 verifyFormat("vector< int > x{ 1, 2,\n"
13632 " 3, 4 };",
13633 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
13634 verifyFormat("vector< int > x{\n"
13635 " 1,\n"
13636 " 2,\n"
13637 " 3,\n"
13638 " 4,\n"
13639 "};",
13640 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
13641 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
13642 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
13643 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
13644 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
13647 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
13648 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13649 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13650 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13651 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13652 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13653 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
13654 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
13655 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13656 " 1, 22, 333, 4444, 55555, //\n"
13657 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13658 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
13659 verifyFormat(
13660 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13661 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13662 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
13663 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13664 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13665 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13666 " 7777777};");
13667 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13668 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13669 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13670 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13671 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13672 " // Separating comment.\n"
13673 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13674 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13675 " // Leading comment\n"
13676 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13677 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13678 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13679 " 1, 1, 1, 1};",
13680 getLLVMStyleWithColumns(39));
13681 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13682 " 1, 1, 1, 1};",
13683 getLLVMStyleWithColumns(38));
13684 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
13685 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
13686 getLLVMStyleWithColumns(43));
13687 verifyFormat(
13688 "static unsigned SomeValues[10][3] = {\n"
13689 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
13690 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
13691 verifyFormat("static auto fields = new vector<string>{\n"
13692 " \"aaaaaaaaaaaaa\",\n"
13693 " \"aaaaaaaaaaaaa\",\n"
13694 " \"aaaaaaaaaaaa\",\n"
13695 " \"aaaaaaaaaaaaaa\",\n"
13696 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
13697 " \"aaaaaaaaaaaa\",\n"
13698 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
13699 "};");
13700 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
13701 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
13702 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
13703 " 3, cccccccccccccccccccccc};",
13704 getLLVMStyleWithColumns(60));
13706 // Trailing commas.
13707 verifyFormat("vector<int> x = {\n"
13708 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
13709 "};",
13710 getLLVMStyleWithColumns(39));
13711 verifyFormat("vector<int> x = {\n"
13712 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
13713 "};",
13714 getLLVMStyleWithColumns(39));
13715 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13716 " 1, 1, 1, 1,\n"
13717 " /**/ /**/};",
13718 getLLVMStyleWithColumns(39));
13720 // Trailing comment in the first line.
13721 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
13722 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
13723 " 111111111, 222222222, 3333333333, 444444444, //\n"
13724 " 11111111, 22222222, 333333333, 44444444};");
13725 // Trailing comment in the last line.
13726 verifyFormat("int aaaaa[] = {\n"
13727 " 1, 2, 3, // comment\n"
13728 " 4, 5, 6 // comment\n"
13729 "};");
13731 // With nested lists, we should either format one item per line or all nested
13732 // lists one on line.
13733 // FIXME: For some nested lists, we can do better.
13734 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
13735 " {aaaaaaaaaaaaaaaaaaa},\n"
13736 " {aaaaaaaaaaaaaaaaaaaaa},\n"
13737 " {aaaaaaaaaaaaaaaaa}};",
13738 getLLVMStyleWithColumns(60));
13739 verifyFormat(
13740 "SomeStruct my_struct_array = {\n"
13741 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
13742 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
13743 " {aaa, aaa},\n"
13744 " {aaa, aaa},\n"
13745 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
13746 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
13747 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
13749 // No column layout should be used here.
13750 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
13751 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
13753 verifyNoCrash("a<,");
13755 // No braced initializer here.
13756 verifyFormat("void f() {\n"
13757 " struct Dummy {};\n"
13758 " f(v);\n"
13759 "}");
13760 verifyFormat("void foo() {\n"
13761 " { // asdf\n"
13762 " { int a; }\n"
13763 " }\n"
13764 " {\n"
13765 " { int b; }\n"
13766 " }\n"
13767 "}");
13768 verifyFormat("namespace n {\n"
13769 "void foo() {\n"
13770 " {\n"
13771 " {\n"
13772 " statement();\n"
13773 " if (false) {\n"
13774 " }\n"
13775 " }\n"
13776 " }\n"
13777 " {}\n"
13778 "}\n"
13779 "} // namespace n");
13781 // Long lists should be formatted in columns even if they are nested.
13782 verifyFormat(
13783 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13784 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13785 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13786 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13787 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13788 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
13790 // Allow "single-column" layout even if that violates the column limit. There
13791 // isn't going to be a better way.
13792 verifyFormat("std::vector<int> a = {\n"
13793 " aaaaaaaa,\n"
13794 " aaaaaaaa,\n"
13795 " aaaaaaaa,\n"
13796 " aaaaaaaa,\n"
13797 " aaaaaaaaaa,\n"
13798 " aaaaaaaa,\n"
13799 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
13800 getLLVMStyleWithColumns(30));
13801 verifyFormat("vector<int> aaaa = {\n"
13802 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13803 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13804 " aaaaaa.aaaaaaa,\n"
13805 " aaaaaa.aaaaaaa,\n"
13806 " aaaaaa.aaaaaaa,\n"
13807 " aaaaaa.aaaaaaa,\n"
13808 "};");
13810 // Don't create hanging lists.
13811 verifyFormat("someFunction(Param, {List1, List2,\n"
13812 " List3});",
13813 getLLVMStyleWithColumns(35));
13814 verifyFormat("someFunction(Param, Param,\n"
13815 " {List1, List2,\n"
13816 " List3});",
13817 getLLVMStyleWithColumns(35));
13818 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
13819 " aaaaaaaaaaaaaaaaaaaaaaa);");
13822 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
13823 FormatStyle DoNotMerge = getLLVMStyle();
13824 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
13826 verifyFormat("void f() { return 42; }");
13827 verifyFormat("void f() {\n"
13828 " return 42;\n"
13829 "}",
13830 DoNotMerge);
13831 verifyFormat("void f() {\n"
13832 " // Comment\n"
13833 "}");
13834 verifyFormat("{\n"
13835 "#error {\n"
13836 " int a;\n"
13837 "}");
13838 verifyFormat("{\n"
13839 " int a;\n"
13840 "#error {\n"
13841 "}");
13842 verifyFormat("void f() {} // comment");
13843 verifyFormat("void f() { int a; } // comment");
13844 verifyFormat("void f() {\n"
13845 "} // comment",
13846 DoNotMerge);
13847 verifyFormat("void f() {\n"
13848 " int a;\n"
13849 "} // comment",
13850 DoNotMerge);
13851 verifyFormat("void f() {\n"
13852 "} // comment",
13853 getLLVMStyleWithColumns(15));
13855 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
13856 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
13858 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
13859 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
13860 verifyGoogleFormat("class C {\n"
13861 " C()\n"
13862 " : iiiiiiii(nullptr),\n"
13863 " kkkkkkk(nullptr),\n"
13864 " mmmmmmm(nullptr),\n"
13865 " nnnnnnn(nullptr) {}\n"
13866 "};");
13868 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
13869 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
13870 verifyFormat("class C {\n"
13871 " A() : b(0) {}\n"
13872 "};",
13873 "class C{A():b(0){}};", NoColumnLimit);
13874 verifyFormat("A()\n"
13875 " : b(0) {\n"
13876 "}",
13877 "A()\n:b(0)\n{\n}", NoColumnLimit);
13879 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
13880 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
13881 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
13882 verifyFormat("class C {\n"
13883 "#pragma foo\n"
13884 " int foo { return 0; }\n"
13885 "};",
13886 NoColumnLimitWrapAfterFunction);
13887 verifyFormat("class C {\n"
13888 "#pragma foo\n"
13889 " void foo {}\n"
13890 "};",
13891 NoColumnLimitWrapAfterFunction);
13893 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
13894 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
13895 FormatStyle::SFS_None;
13896 verifyFormat("A()\n"
13897 " : b(0) {\n"
13898 "}",
13899 "A():b(0){}", DoNotMergeNoColumnLimit);
13900 verifyFormat("A()\n"
13901 " : b(0) {\n"
13902 "}",
13903 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
13905 verifyFormat("#define A \\\n"
13906 " void f() { \\\n"
13907 " int i; \\\n"
13908 " }",
13909 getLLVMStyleWithColumns(20));
13910 verifyFormat("#define A \\\n"
13911 " void f() { int i; }",
13912 getLLVMStyleWithColumns(21));
13913 verifyFormat("#define A \\\n"
13914 " void f() { \\\n"
13915 " int i; \\\n"
13916 " } \\\n"
13917 " int j;",
13918 getLLVMStyleWithColumns(22));
13919 verifyFormat("#define A \\\n"
13920 " void f() { int i; } \\\n"
13921 " int j;",
13922 getLLVMStyleWithColumns(23));
13925 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
13926 FormatStyle MergeEmptyOnly = getLLVMStyle();
13927 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
13928 verifyFormat("class C {\n"
13929 " int f() {}\n"
13930 "};",
13931 MergeEmptyOnly);
13932 verifyFormat("class C {\n"
13933 " int f() {\n"
13934 " return 42;\n"
13935 " }\n"
13936 "};",
13937 MergeEmptyOnly);
13938 verifyFormat("int f() {}", MergeEmptyOnly);
13939 verifyFormat("int f() {\n"
13940 " return 42;\n"
13941 "}",
13942 MergeEmptyOnly);
13944 // Also verify behavior when BraceWrapping.AfterFunction = true
13945 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
13946 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
13947 verifyFormat("int f() {}", MergeEmptyOnly);
13948 verifyFormat("class C {\n"
13949 " int f() {}\n"
13950 "};",
13951 MergeEmptyOnly);
13954 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
13955 FormatStyle MergeInlineOnly = getLLVMStyle();
13956 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
13957 verifyFormat("class C {\n"
13958 " int f() { return 42; }\n"
13959 "};",
13960 MergeInlineOnly);
13961 verifyFormat("int f() {\n"
13962 " return 42;\n"
13963 "}",
13964 MergeInlineOnly);
13966 // SFS_Inline implies SFS_Empty
13967 verifyFormat("class C {\n"
13968 " int f() {}\n"
13969 "};",
13970 MergeInlineOnly);
13971 verifyFormat("int f() {}", MergeInlineOnly);
13972 // https://llvm.org/PR54147
13973 verifyFormat("auto lambda = []() {\n"
13974 " // comment\n"
13975 " f();\n"
13976 " g();\n"
13977 "};",
13978 MergeInlineOnly);
13980 verifyFormat("class C {\n"
13981 "#ifdef A\n"
13982 " int f() { return 42; }\n"
13983 "#endif\n"
13984 "};",
13985 MergeInlineOnly);
13987 verifyFormat("struct S {\n"
13988 "// comment\n"
13989 "#ifdef FOO\n"
13990 " int foo() { bar(); }\n"
13991 "#endif\n"
13992 "};",
13993 MergeInlineOnly);
13995 // Also verify behavior when BraceWrapping.AfterFunction = true
13996 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
13997 MergeInlineOnly.BraceWrapping.AfterFunction = true;
13998 verifyFormat("class C {\n"
13999 " int f() { return 42; }\n"
14000 "};",
14001 MergeInlineOnly);
14002 verifyFormat("int f()\n"
14003 "{\n"
14004 " return 42;\n"
14005 "}",
14006 MergeInlineOnly);
14008 // SFS_Inline implies SFS_Empty
14009 verifyFormat("int f() {}", MergeInlineOnly);
14010 verifyFormat("class C {\n"
14011 " int f() {}\n"
14012 "};",
14013 MergeInlineOnly);
14015 MergeInlineOnly.BraceWrapping.AfterClass = true;
14016 MergeInlineOnly.BraceWrapping.AfterStruct = true;
14017 verifyFormat("class C\n"
14018 "{\n"
14019 " int f() { return 42; }\n"
14020 "};",
14021 MergeInlineOnly);
14022 verifyFormat("struct C\n"
14023 "{\n"
14024 " int f() { return 42; }\n"
14025 "};",
14026 MergeInlineOnly);
14027 verifyFormat("int f()\n"
14028 "{\n"
14029 " return 42;\n"
14030 "}",
14031 MergeInlineOnly);
14032 verifyFormat("int f() {}", MergeInlineOnly);
14033 verifyFormat("class C\n"
14034 "{\n"
14035 " int f() { return 42; }\n"
14036 "};",
14037 MergeInlineOnly);
14038 verifyFormat("struct C\n"
14039 "{\n"
14040 " int f() { return 42; }\n"
14041 "};",
14042 MergeInlineOnly);
14043 verifyFormat("struct C\n"
14044 "// comment\n"
14045 "/* comment */\n"
14046 "// comment\n"
14047 "{\n"
14048 " int f() { return 42; }\n"
14049 "};",
14050 MergeInlineOnly);
14051 verifyFormat("/* comment */ struct C\n"
14052 "{\n"
14053 " int f() { return 42; }\n"
14054 "};",
14055 MergeInlineOnly);
14058 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14059 FormatStyle MergeInlineOnly = getLLVMStyle();
14060 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14061 FormatStyle::SFS_InlineOnly;
14062 verifyFormat("class C {\n"
14063 " int f() { return 42; }\n"
14064 "};",
14065 MergeInlineOnly);
14066 verifyFormat("int f() {\n"
14067 " return 42;\n"
14068 "}",
14069 MergeInlineOnly);
14071 // SFS_InlineOnly does not imply SFS_Empty
14072 verifyFormat("class C {\n"
14073 " int f() {}\n"
14074 "};",
14075 MergeInlineOnly);
14076 verifyFormat("int f() {\n"
14077 "}",
14078 MergeInlineOnly);
14080 // Also verify behavior when BraceWrapping.AfterFunction = true
14081 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14082 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14083 verifyFormat("class C {\n"
14084 " int f() { return 42; }\n"
14085 "};",
14086 MergeInlineOnly);
14087 verifyFormat("int f()\n"
14088 "{\n"
14089 " return 42;\n"
14090 "}",
14091 MergeInlineOnly);
14093 // SFS_InlineOnly does not imply SFS_Empty
14094 verifyFormat("int f()\n"
14095 "{\n"
14096 "}",
14097 MergeInlineOnly);
14098 verifyFormat("class C {\n"
14099 " int f() {}\n"
14100 "};",
14101 MergeInlineOnly);
14104 TEST_F(FormatTest, SplitEmptyFunction) {
14105 FormatStyle Style = getLLVMStyleWithColumns(40);
14106 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14107 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14108 Style.BraceWrapping.AfterFunction = true;
14109 Style.BraceWrapping.SplitEmptyFunction = false;
14111 verifyFormat("int f()\n"
14112 "{}",
14113 Style);
14114 verifyFormat("int f()\n"
14115 "{\n"
14116 " return 42;\n"
14117 "}",
14118 Style);
14119 verifyFormat("int f()\n"
14120 "{\n"
14121 " // some comment\n"
14122 "}",
14123 Style);
14125 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14126 verifyFormat("int f() {}", Style);
14127 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14128 "{}",
14129 Style);
14130 verifyFormat("int f()\n"
14131 "{\n"
14132 " return 0;\n"
14133 "}",
14134 Style);
14136 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14137 verifyFormat("class Foo {\n"
14138 " int f() {}\n"
14139 "};",
14140 Style);
14141 verifyFormat("class Foo {\n"
14142 " int f() { return 0; }\n"
14143 "};",
14144 Style);
14145 verifyFormat("class Foo {\n"
14146 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14147 " {}\n"
14148 "};",
14149 Style);
14150 verifyFormat("class Foo {\n"
14151 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14152 " {\n"
14153 " return 0;\n"
14154 " }\n"
14155 "};",
14156 Style);
14158 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14159 verifyFormat("int f() {}", Style);
14160 verifyFormat("int f() { return 0; }", Style);
14161 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14162 "{}",
14163 Style);
14164 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14165 "{\n"
14166 " return 0;\n"
14167 "}",
14168 Style);
14171 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
14172 FormatStyle Style = getLLVMStyleWithColumns(40);
14173 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14174 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14175 Style.BraceWrapping.AfterFunction = true;
14176 Style.BraceWrapping.SplitEmptyFunction = true;
14177 Style.BraceWrapping.SplitEmptyRecord = false;
14179 verifyFormat("class C {};", Style);
14180 verifyFormat("struct C {};", Style);
14181 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14182 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14183 "{\n"
14184 "}",
14185 Style);
14186 verifyFormat("class C {\n"
14187 " C()\n"
14188 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14189 " bbbbbbbbbbbbbbbbbbb()\n"
14190 " {\n"
14191 " }\n"
14192 " void\n"
14193 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14194 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14195 " {\n"
14196 " }\n"
14197 "};",
14198 Style);
14201 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
14202 FormatStyle Style = getLLVMStyle();
14203 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14204 verifyFormat("#ifdef A\n"
14205 "int f() {}\n"
14206 "#else\n"
14207 "int g() {}\n"
14208 "#endif",
14209 Style);
14212 TEST_F(FormatTest, SplitEmptyClass) {
14213 FormatStyle Style = getLLVMStyle();
14214 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14215 Style.BraceWrapping.AfterClass = true;
14216 Style.BraceWrapping.SplitEmptyRecord = false;
14218 verifyFormat("class Foo\n"
14219 "{};",
14220 Style);
14221 verifyFormat("/* something */ class Foo\n"
14222 "{};",
14223 Style);
14224 verifyFormat("template <typename X> class Foo\n"
14225 "{};",
14226 Style);
14227 verifyFormat("class Foo\n"
14228 "{\n"
14229 " Foo();\n"
14230 "};",
14231 Style);
14232 verifyFormat("typedef class Foo\n"
14233 "{\n"
14234 "} Foo_t;",
14235 Style);
14237 Style.BraceWrapping.SplitEmptyRecord = true;
14238 Style.BraceWrapping.AfterStruct = true;
14239 verifyFormat("class rep\n"
14240 "{\n"
14241 "};",
14242 Style);
14243 verifyFormat("struct rep\n"
14244 "{\n"
14245 "};",
14246 Style);
14247 verifyFormat("template <typename T> class rep\n"
14248 "{\n"
14249 "};",
14250 Style);
14251 verifyFormat("template <typename T> struct rep\n"
14252 "{\n"
14253 "};",
14254 Style);
14255 verifyFormat("class rep\n"
14256 "{\n"
14257 " int x;\n"
14258 "};",
14259 Style);
14260 verifyFormat("struct rep\n"
14261 "{\n"
14262 " int x;\n"
14263 "};",
14264 Style);
14265 verifyFormat("template <typename T> class rep\n"
14266 "{\n"
14267 " int x;\n"
14268 "};",
14269 Style);
14270 verifyFormat("template <typename T> struct rep\n"
14271 "{\n"
14272 " int x;\n"
14273 "};",
14274 Style);
14275 verifyFormat("template <typename T> class rep // Foo\n"
14276 "{\n"
14277 " int x;\n"
14278 "};",
14279 Style);
14280 verifyFormat("template <typename T> struct rep // Bar\n"
14281 "{\n"
14282 " int x;\n"
14283 "};",
14284 Style);
14286 verifyFormat("template <typename T> class rep<T>\n"
14287 "{\n"
14288 " int x;\n"
14289 "};",
14290 Style);
14292 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14293 "{\n"
14294 " int x;\n"
14295 "};",
14296 Style);
14297 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14298 "{\n"
14299 "};",
14300 Style);
14302 verifyFormat("#include \"stdint.h\"\n"
14303 "namespace rep {}",
14304 Style);
14305 verifyFormat("#include <stdint.h>\n"
14306 "namespace rep {}",
14307 Style);
14308 verifyFormat("#include <stdint.h>\n"
14309 "namespace rep {}",
14310 "#include <stdint.h>\n"
14311 "namespace rep {\n"
14312 "\n"
14313 "\n"
14314 "}",
14315 Style);
14318 TEST_F(FormatTest, SplitEmptyStruct) {
14319 FormatStyle Style = getLLVMStyle();
14320 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14321 Style.BraceWrapping.AfterStruct = true;
14322 Style.BraceWrapping.SplitEmptyRecord = false;
14324 verifyFormat("struct Foo\n"
14325 "{};",
14326 Style);
14327 verifyFormat("/* something */ struct Foo\n"
14328 "{};",
14329 Style);
14330 verifyFormat("template <typename X> struct Foo\n"
14331 "{};",
14332 Style);
14333 verifyFormat("struct Foo\n"
14334 "{\n"
14335 " Foo();\n"
14336 "};",
14337 Style);
14338 verifyFormat("typedef struct Foo\n"
14339 "{\n"
14340 "} Foo_t;",
14341 Style);
14342 // typedef struct Bar {} Bar_t;
14345 TEST_F(FormatTest, SplitEmptyUnion) {
14346 FormatStyle Style = getLLVMStyle();
14347 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14348 Style.BraceWrapping.AfterUnion = true;
14349 Style.BraceWrapping.SplitEmptyRecord = false;
14351 verifyFormat("union Foo\n"
14352 "{};",
14353 Style);
14354 verifyFormat("/* something */ union Foo\n"
14355 "{};",
14356 Style);
14357 verifyFormat("union Foo\n"
14358 "{\n"
14359 " A,\n"
14360 "};",
14361 Style);
14362 verifyFormat("typedef union Foo\n"
14363 "{\n"
14364 "} Foo_t;",
14365 Style);
14368 TEST_F(FormatTest, SplitEmptyNamespace) {
14369 FormatStyle Style = getLLVMStyle();
14370 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14371 Style.BraceWrapping.AfterNamespace = true;
14372 Style.BraceWrapping.SplitEmptyNamespace = false;
14374 verifyFormat("namespace Foo\n"
14375 "{};",
14376 Style);
14377 verifyFormat("/* something */ namespace Foo\n"
14378 "{};",
14379 Style);
14380 verifyFormat("inline namespace Foo\n"
14381 "{};",
14382 Style);
14383 verifyFormat("/* something */ inline namespace Foo\n"
14384 "{};",
14385 Style);
14386 verifyFormat("export namespace Foo\n"
14387 "{};",
14388 Style);
14389 verifyFormat("namespace Foo\n"
14390 "{\n"
14391 "void Bar();\n"
14392 "};",
14393 Style);
14396 TEST_F(FormatTest, NeverMergeShortRecords) {
14397 FormatStyle Style = getLLVMStyle();
14399 verifyFormat("class Foo {\n"
14400 " Foo();\n"
14401 "};",
14402 Style);
14403 verifyFormat("typedef class Foo {\n"
14404 " Foo();\n"
14405 "} Foo_t;",
14406 Style);
14407 verifyFormat("struct Foo {\n"
14408 " Foo();\n"
14409 "};",
14410 Style);
14411 verifyFormat("typedef struct Foo {\n"
14412 " Foo();\n"
14413 "} Foo_t;",
14414 Style);
14415 verifyFormat("union Foo {\n"
14416 " A,\n"
14417 "};",
14418 Style);
14419 verifyFormat("typedef union Foo {\n"
14420 " A,\n"
14421 "} Foo_t;",
14422 Style);
14423 verifyFormat("namespace Foo {\n"
14424 "void Bar();\n"
14425 "};",
14426 Style);
14428 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14429 Style.BraceWrapping.AfterClass = true;
14430 Style.BraceWrapping.AfterStruct = true;
14431 Style.BraceWrapping.AfterUnion = true;
14432 Style.BraceWrapping.AfterNamespace = true;
14433 verifyFormat("class Foo\n"
14434 "{\n"
14435 " Foo();\n"
14436 "};",
14437 Style);
14438 verifyFormat("typedef class Foo\n"
14439 "{\n"
14440 " Foo();\n"
14441 "} Foo_t;",
14442 Style);
14443 verifyFormat("struct Foo\n"
14444 "{\n"
14445 " Foo();\n"
14446 "};",
14447 Style);
14448 verifyFormat("typedef struct Foo\n"
14449 "{\n"
14450 " Foo();\n"
14451 "} Foo_t;",
14452 Style);
14453 verifyFormat("union Foo\n"
14454 "{\n"
14455 " A,\n"
14456 "};",
14457 Style);
14458 verifyFormat("typedef union Foo\n"
14459 "{\n"
14460 " A,\n"
14461 "} Foo_t;",
14462 Style);
14463 verifyFormat("namespace Foo\n"
14464 "{\n"
14465 "void Bar();\n"
14466 "};",
14467 Style);
14470 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
14471 // Elaborate type variable declarations.
14472 verifyFormat("struct foo a = {bar};\nint n;");
14473 verifyFormat("class foo a = {bar};\nint n;");
14474 verifyFormat("union foo a = {bar};\nint n;");
14476 // Elaborate types inside function definitions.
14477 verifyFormat("struct foo f() {}\nint n;");
14478 verifyFormat("class foo f() {}\nint n;");
14479 verifyFormat("union foo f() {}\nint n;");
14481 // Templates.
14482 verifyFormat("template <class X> void f() {}\nint n;");
14483 verifyFormat("template <struct X> void f() {}\nint n;");
14484 verifyFormat("template <union X> void f() {}\nint n;");
14486 // Actual definitions...
14487 verifyFormat("struct {\n} n;");
14488 verifyFormat(
14489 "template <template <class T, class Y>, class Z> class X {\n} n;");
14490 verifyFormat("union Z {\n int n;\n} x;");
14491 verifyFormat("class MACRO Z {\n} n;");
14492 verifyFormat("class MACRO(X) Z {\n} n;");
14493 verifyFormat("class __attribute__(X) Z {\n} n;");
14494 verifyFormat("class __declspec(X) Z {\n} n;");
14495 verifyFormat("class A##B##C {\n} n;");
14496 verifyFormat("class alignas(16) Z {\n} n;");
14497 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
14498 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
14500 // Redefinition from nested context:
14501 verifyFormat("class A::B::C {\n} n;");
14503 // Template definitions.
14504 verifyFormat(
14505 "template <typename F>\n"
14506 "Matcher(const Matcher<F> &Other,\n"
14507 " typename enable_if_c<is_base_of<F, T>::value &&\n"
14508 " !is_same<F, T>::value>::type * = 0)\n"
14509 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
14511 // FIXME: This is still incorrectly handled at the formatter side.
14512 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
14513 verifyFormat("int i = SomeFunction(a<b, a> b);");
14515 // FIXME:
14516 // This now gets parsed incorrectly as class definition.
14517 // verifyFormat("class A<int> f() {\n}\nint n;");
14519 // Elaborate types where incorrectly parsing the structural element would
14520 // break the indent.
14521 verifyFormat("if (true)\n"
14522 " class X x;\n"
14523 "else\n"
14524 " f();");
14526 // This is simply incomplete. Formatting is not important, but must not crash.
14527 verifyFormat("class A:");
14530 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
14531 verifyNoChange("#error Leave all white!!!!! space* alone!");
14532 verifyNoChange("#warning Leave all white!!!!! space* alone!");
14533 verifyFormat("#error 1", " # error 1");
14534 verifyFormat("#warning 1", " # warning 1");
14537 TEST_F(FormatTest, FormatHashIfExpressions) {
14538 verifyFormat("#if AAAA && BBBB");
14539 verifyFormat("#if (AAAA && BBBB)");
14540 verifyFormat("#elif (AAAA && BBBB)");
14541 // FIXME: Come up with a better indentation for #elif.
14542 verifyFormat(
14543 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
14544 " defined(BBBBBBBB)\n"
14545 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
14546 " defined(BBBBBBBB)\n"
14547 "#endif",
14548 getLLVMStyleWithColumns(65));
14551 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
14552 FormatStyle AllowsMergedIf = getGoogleStyle();
14553 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
14554 FormatStyle::SIS_WithoutElse;
14555 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
14556 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
14557 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
14558 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
14559 FormatStyle ShortMergedIf = AllowsMergedIf;
14560 ShortMergedIf.ColumnLimit = 25;
14561 verifyFormat("#define A \\\n"
14562 " if (true) return 42;",
14563 ShortMergedIf);
14564 verifyFormat("#define A \\\n"
14565 " f(); \\\n"
14566 " if (true)\n"
14567 "#define B",
14568 ShortMergedIf);
14569 verifyFormat("#define A \\\n"
14570 " f(); \\\n"
14571 " if (true)\n"
14572 "g();",
14573 ShortMergedIf);
14574 verifyFormat("{\n"
14575 "#ifdef A\n"
14576 " // Comment\n"
14577 " if (true) continue;\n"
14578 "#endif\n"
14579 " // Comment\n"
14580 " if (true) continue;\n"
14581 "}",
14582 ShortMergedIf);
14583 ShortMergedIf.ColumnLimit = 33;
14584 verifyFormat("#define A \\\n"
14585 " if constexpr (true) return 42;",
14586 ShortMergedIf);
14587 verifyFormat("#define A \\\n"
14588 " if CONSTEXPR (true) return 42;",
14589 ShortMergedIf);
14590 ShortMergedIf.ColumnLimit = 29;
14591 verifyFormat("#define A \\\n"
14592 " if (aaaaaaaaaa) return 1; \\\n"
14593 " return 2;",
14594 ShortMergedIf);
14595 ShortMergedIf.ColumnLimit = 28;
14596 verifyFormat("#define A \\\n"
14597 " if (aaaaaaaaaa) \\\n"
14598 " return 1; \\\n"
14599 " return 2;",
14600 ShortMergedIf);
14601 verifyFormat("#define A \\\n"
14602 " if constexpr (aaaaaaa) \\\n"
14603 " return 1; \\\n"
14604 " return 2;",
14605 ShortMergedIf);
14606 verifyFormat("#define A \\\n"
14607 " if CONSTEXPR (aaaaaaa) \\\n"
14608 " return 1; \\\n"
14609 " return 2;",
14610 ShortMergedIf);
14612 verifyFormat("//\n"
14613 "#define a \\\n"
14614 " if \\\n"
14615 " 0",
14616 getChromiumStyle(FormatStyle::LK_Cpp));
14619 TEST_F(FormatTest, FormatStarDependingOnContext) {
14620 verifyFormat("void f(int *a);");
14621 verifyFormat("void f() { f(fint * b); }");
14622 verifyFormat("class A {\n void f(int *a);\n};");
14623 verifyFormat("class A {\n int *a;\n};");
14624 verifyFormat("namespace a {\n"
14625 "namespace b {\n"
14626 "class A {\n"
14627 " void f() {}\n"
14628 " int *a;\n"
14629 "};\n"
14630 "} // namespace b\n"
14631 "} // namespace a");
14634 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
14635 verifyFormat("while");
14636 verifyFormat("operator");
14639 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
14640 // This code would be painfully slow to format if we didn't skip it.
14641 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
14642 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14643 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14644 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14645 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14646 "A(1, 1)\n"
14647 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
14648 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14649 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14650 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14651 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14652 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14653 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14654 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14655 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14656 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
14657 // Deeply nested part is untouched, rest is formatted.
14658 EXPECT_EQ(std::string("int i;") + Code + "int j;",
14659 format(std::string("int i;") + Code + "int j;",
14660 getLLVMStyle(), SC_ExpectIncomplete));
14663 //===----------------------------------------------------------------------===//
14664 // Objective-C tests.
14665 //===----------------------------------------------------------------------===//
14667 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
14668 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
14669 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
14670 "-(NSUInteger)indexOfObject:(id)anObject;");
14671 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
14672 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
14673 verifyFormat("- (NSInteger)Method3:(id)anObject;",
14674 "-(NSInteger)Method3:(id)anObject;");
14675 verifyFormat("- (NSInteger)Method4:(id)anObject;",
14676 "-(NSInteger)Method4:(id)anObject;");
14677 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
14678 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
14679 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
14680 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
14681 "forAllCells:(BOOL)flag;");
14683 // Very long objectiveC method declaration.
14684 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
14685 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
14686 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
14687 " inRange:(NSRange)range\n"
14688 " outRange:(NSRange)out_range\n"
14689 " outRange1:(NSRange)out_range1\n"
14690 " outRange2:(NSRange)out_range2\n"
14691 " outRange3:(NSRange)out_range3\n"
14692 " outRange4:(NSRange)out_range4\n"
14693 " outRange5:(NSRange)out_range5\n"
14694 " outRange6:(NSRange)out_range6\n"
14695 " outRange7:(NSRange)out_range7\n"
14696 " outRange8:(NSRange)out_range8\n"
14697 " outRange9:(NSRange)out_range9;");
14699 // When the function name has to be wrapped.
14700 FormatStyle Style = getLLVMStyle();
14701 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
14702 // and always indents instead.
14703 Style.IndentWrappedFunctionNames = false;
14704 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
14705 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
14706 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
14707 "}",
14708 Style);
14709 Style.IndentWrappedFunctionNames = true;
14710 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
14711 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
14712 " anotherName:(NSString)dddddddddddddd {\n"
14713 "}",
14714 Style);
14716 verifyFormat("- (int)sum:(vector<int>)numbers;");
14717 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
14718 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
14719 // protocol lists (but not for template classes):
14720 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
14722 verifyFormat("- (int (*)())foo:(int (*)())f;");
14723 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
14725 // If there's no return type (very rare in practice!), LLVM and Google style
14726 // agree.
14727 verifyFormat("- foo;");
14728 verifyFormat("- foo:(int)f;");
14729 verifyGoogleFormat("- foo:(int)foo;");
14732 TEST_F(FormatTest, BreaksStringLiterals) {
14733 // FIXME: unstable test case
14734 EXPECT_EQ("\"some text \"\n"
14735 "\"other\";",
14736 format("\"some text other\";", getLLVMStyleWithColumns(12)));
14737 // FIXME: unstable test case
14738 EXPECT_EQ("\"some text \"\n"
14739 "\"other\";",
14740 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
14741 verifyFormat("#define A \\\n"
14742 " \"some \" \\\n"
14743 " \"text \" \\\n"
14744 " \"other\";",
14745 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
14746 verifyFormat("#define A \\\n"
14747 " \"so \" \\\n"
14748 " \"text \" \\\n"
14749 " \"other\";",
14750 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
14752 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
14753 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
14754 // FIXME: unstable test case
14755 EXPECT_EQ("\"some \"\n"
14756 "\"text\"",
14757 format("\"some text\"", getLLVMStyleWithColumns(10)));
14758 // FIXME: unstable test case
14759 EXPECT_EQ("\"some \"\n"
14760 "\"text\"",
14761 format("\"some text\"", getLLVMStyleWithColumns(7)));
14762 // FIXME: unstable test case
14763 EXPECT_EQ("\"some\"\n"
14764 "\" tex\"\n"
14765 "\"t\"",
14766 format("\"some text\"", getLLVMStyleWithColumns(6)));
14767 // FIXME: unstable test case
14768 EXPECT_EQ("\"some\"\n"
14769 "\" tex\"\n"
14770 "\" and\"",
14771 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
14772 // FIXME: unstable test case
14773 EXPECT_EQ("\"some\"\n"
14774 "\"/tex\"\n"
14775 "\"/and\"",
14776 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
14778 verifyFormat("variable =\n"
14779 " \"long string \"\n"
14780 " \"literal\";",
14781 "variable = \"long string literal\";",
14782 getLLVMStyleWithColumns(20));
14784 verifyFormat("variable = f(\n"
14785 " \"long string \"\n"
14786 " \"literal\",\n"
14787 " short,\n"
14788 " loooooooooooooooooooong);",
14789 "variable = f(\"long string literal\", short, "
14790 "loooooooooooooooooooong);",
14791 getLLVMStyleWithColumns(20));
14793 verifyFormat("f(g(\"long string \"\n"
14794 " \"literal\"),\n"
14795 " b);",
14796 "f(g(\"long string literal\"), b);",
14797 getLLVMStyleWithColumns(20));
14798 verifyFormat("f(g(\"long string \"\n"
14799 " \"literal\",\n"
14800 " a),\n"
14801 " b);",
14802 "f(g(\"long string literal\", a), b);",
14803 getLLVMStyleWithColumns(20));
14804 verifyFormat("f(\"one two\".split(\n"
14805 " variable));",
14806 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
14807 verifyFormat("f(\"one two three four five six \"\n"
14808 " \"seven\".split(\n"
14809 " really_looooong_variable));",
14810 "f(\"one two three four five six seven\"."
14811 "split(really_looooong_variable));",
14812 getLLVMStyleWithColumns(33));
14814 verifyFormat("f(\"some \"\n"
14815 " \"text\",\n"
14816 " other);",
14817 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
14819 // Only break as a last resort.
14820 verifyFormat(
14821 "aaaaaaaaaaaaaaaaaaaa(\n"
14822 " aaaaaaaaaaaaaaaaaaaa,\n"
14823 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
14825 // FIXME: unstable test case
14826 EXPECT_EQ("\"splitmea\"\n"
14827 "\"trandomp\"\n"
14828 "\"oint\"",
14829 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
14831 // FIXME: unstable test case
14832 EXPECT_EQ("\"split/\"\n"
14833 "\"pathat/\"\n"
14834 "\"slashes\"",
14835 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
14837 // FIXME: unstable test case
14838 EXPECT_EQ("\"split/\"\n"
14839 "\"pathat/\"\n"
14840 "\"slashes\"",
14841 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
14842 // FIXME: unstable test case
14843 EXPECT_EQ("\"split at \"\n"
14844 "\"spaces/at/\"\n"
14845 "\"slashes.at.any$\"\n"
14846 "\"non-alphanumeric%\"\n"
14847 "\"1111111111characte\"\n"
14848 "\"rs\"",
14849 format("\"split at "
14850 "spaces/at/"
14851 "slashes.at."
14852 "any$non-"
14853 "alphanumeric%"
14854 "1111111111characte"
14855 "rs\"",
14856 getLLVMStyleWithColumns(20)));
14858 // Verify that splitting the strings understands
14859 // Style::AlwaysBreakBeforeMultilineStrings.
14860 verifyFormat("aaaaaaaaaaaa(\n"
14861 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
14862 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
14863 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
14864 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
14865 "aaaaaaaaaaaaaaaaaaaaaa\");",
14866 getGoogleStyle());
14867 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14868 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
14869 "return \"aaaaaaaaaaaaaaaaaaaaaa "
14870 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
14871 "aaaaaaaaaaaaaaaaaaaaaa\";",
14872 getGoogleStyle());
14873 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14874 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
14875 "llvm::outs() << "
14876 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
14877 "aaaaaaaaaaaaaaaaaaa\";");
14878 verifyFormat("ffff(\n"
14879 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14880 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
14881 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
14882 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
14883 getGoogleStyle());
14885 FormatStyle Style = getLLVMStyleWithColumns(12);
14886 Style.BreakStringLiterals = false;
14887 verifyFormat("\"some text other\";", Style);
14889 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
14890 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
14891 verifyFormat("#define A \\\n"
14892 " \"some \" \\\n"
14893 " \"text \" \\\n"
14894 " \"other\";",
14895 "#define A \"some text other\";", AlignLeft);
14898 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
14899 verifyFormat("C a = \"some more \"\n"
14900 " \"text\";",
14901 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
14904 TEST_F(FormatTest, FullyRemoveEmptyLines) {
14905 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
14906 NoEmptyLines.MaxEmptyLinesToKeep = 0;
14907 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
14908 NoEmptyLines);
14911 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
14912 // FIXME: unstable test case
14913 EXPECT_EQ(
14914 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14915 "(\n"
14916 " \"x\t\");",
14917 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14918 "aaaaaaa("
14919 "\"x\t\");"));
14922 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
14923 // FIXME: unstable test case
14924 EXPECT_EQ(
14925 "u8\"utf8 string \"\n"
14926 "u8\"literal\";",
14927 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
14928 // FIXME: unstable test case
14929 EXPECT_EQ(
14930 "u\"utf16 string \"\n"
14931 "u\"literal\";",
14932 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
14933 // FIXME: unstable test case
14934 EXPECT_EQ(
14935 "U\"utf32 string \"\n"
14936 "U\"literal\";",
14937 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
14938 // FIXME: unstable test case
14939 EXPECT_EQ("L\"wide string \"\n"
14940 "L\"literal\";",
14941 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
14942 verifyFormat("@\"NSString \"\n"
14943 "@\"literal\";",
14944 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
14945 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
14947 // This input makes clang-format try to split the incomplete unicode escape
14948 // sequence, which used to lead to a crasher.
14949 verifyNoCrash(
14950 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14951 getLLVMStyleWithColumns(60));
14954 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
14955 FormatStyle Style = getGoogleStyleWithColumns(15);
14956 verifyFormat("R\"x(raw literal)x\";", Style);
14957 verifyFormat("uR\"x(raw literal)x\";", Style);
14958 verifyFormat("LR\"x(raw literal)x\";", Style);
14959 verifyFormat("UR\"x(raw literal)x\";", Style);
14960 verifyFormat("u8R\"x(raw literal)x\";", Style);
14963 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
14964 FormatStyle Style = getLLVMStyleWithColumns(20);
14965 // FIXME: unstable test case
14966 EXPECT_EQ(
14967 "_T(\"aaaaaaaaaaaaaa\")\n"
14968 "_T(\"aaaaaaaaaaaaaa\")\n"
14969 "_T(\"aaaaaaaaaaaa\")",
14970 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
14971 verifyFormat("f(x,\n"
14972 " _T(\"aaaaaaaaaaaa\")\n"
14973 " _T(\"aaa\"),\n"
14974 " z);",
14975 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
14977 // FIXME: Handle embedded spaces in one iteration.
14978 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
14979 // "_T(\"aaaaaaaaaaaaa\")\n"
14980 // "_T(\"aaaaaaaaaaaaa\")\n"
14981 // "_T(\"a\")",
14982 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
14983 // getLLVMStyleWithColumns(20)));
14984 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
14985 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
14986 verifyFormat("f(\n"
14987 "#if !TEST\n"
14988 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
14989 "#endif\n"
14990 ");",
14991 "f(\n"
14992 "#if !TEST\n"
14993 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
14994 "#endif\n"
14995 ");");
14996 verifyFormat("f(\n"
14997 "\n"
14998 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
14999 "f(\n"
15000 "\n"
15001 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15002 // Regression test for accessing tokens past the end of a vector in the
15003 // TokenLexer.
15004 verifyNoCrash(R"(_T(
15007 )");
15010 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15011 // In a function call with two operands, the second can be broken with no line
15012 // break before it.
15013 verifyFormat("func(a, \"long long \"\n"
15014 " \"long long\");",
15015 "func(a, \"long long long long\");",
15016 getLLVMStyleWithColumns(24));
15017 // In a function call with three operands, the second must be broken with a
15018 // line break before it.
15019 verifyFormat("func(a,\n"
15020 " \"long long long \"\n"
15021 " \"long\",\n"
15022 " c);",
15023 "func(a, \"long long long long\", c);",
15024 getLLVMStyleWithColumns(24));
15025 // In a function call with three operands, the third must be broken with a
15026 // line break before it.
15027 verifyFormat("func(a, b,\n"
15028 " \"long long long \"\n"
15029 " \"long\");",
15030 "func(a, b, \"long long long long\");",
15031 getLLVMStyleWithColumns(24));
15032 // In a function call with three operands, both the second and the third must
15033 // be broken with a line break before them.
15034 verifyFormat("func(a,\n"
15035 " \"long long long \"\n"
15036 " \"long\",\n"
15037 " \"long long long \"\n"
15038 " \"long\");",
15039 "func(a, \"long long long long\", \"long long long long\");",
15040 getLLVMStyleWithColumns(24));
15041 // In a chain of << with two operands, the second can be broken with no line
15042 // break before it.
15043 verifyFormat("a << \"line line \"\n"
15044 " \"line\";",
15045 "a << \"line line line\";", getLLVMStyleWithColumns(20));
15046 // In a chain of << with three operands, the second can be broken with no line
15047 // break before it.
15048 verifyFormat("abcde << \"line \"\n"
15049 " \"line line\"\n"
15050 " << c;",
15051 "abcde << \"line line line\" << c;",
15052 getLLVMStyleWithColumns(20));
15053 // In a chain of << with three operands, the third must be broken with a line
15054 // break before it.
15055 verifyFormat("a << b\n"
15056 " << \"line line \"\n"
15057 " \"line\";",
15058 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15059 // In a chain of << with three operands, the second can be broken with no line
15060 // break before it and the third must be broken with a line break before it.
15061 verifyFormat("abcd << \"line line \"\n"
15062 " \"line\"\n"
15063 " << \"line line \"\n"
15064 " \"line\";",
15065 "abcd << \"line line line\" << \"line line line\";",
15066 getLLVMStyleWithColumns(20));
15067 // In a chain of binary operators with two operands, the second can be broken
15068 // with no line break before it.
15069 verifyFormat("abcd + \"line line \"\n"
15070 " \"line line\";",
15071 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15072 // In a chain of binary operators with three operands, the second must be
15073 // broken with a line break before it.
15074 verifyFormat("abcd +\n"
15075 " \"line line \"\n"
15076 " \"line line\" +\n"
15077 " e;",
15078 "abcd + \"line line line line\" + e;",
15079 getLLVMStyleWithColumns(20));
15080 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15081 // the first must be broken with a line break before it.
15082 FormatStyle Style = getLLVMStyleWithColumns(25);
15083 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15084 verifyFormat("someFunction(\n"
15085 " \"long long long \"\n"
15086 " \"long\",\n"
15087 " a);",
15088 "someFunction(\"long long long long\", a);", Style);
15089 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15090 verifyFormat("someFunction(\n"
15091 " \"long long long \"\n"
15092 " \"long\",\n"
15093 " a\n"
15094 ");",
15095 Style);
15098 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15099 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15102 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
15107 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15108 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
15109 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
15110 verifyFormat("fffffffffff(g(R\"x(\n"
15111 "multiline raw string literal xxxxxxxxxxxxxx\n"
15112 ")x\",\n"
15113 " a),\n"
15114 " b);",
15115 "fffffffffff(g(R\"x(\n"
15116 "multiline raw string literal xxxxxxxxxxxxxx\n"
15117 ")x\", a), b);",
15118 getGoogleStyleWithColumns(20));
15119 verifyFormat("fffffffffff(\n"
15120 " g(R\"x(qqq\n"
15121 "multiline raw string literal xxxxxxxxxxxxxx\n"
15122 ")x\",\n"
15123 " a),\n"
15124 " b);",
15125 "fffffffffff(g(R\"x(qqq\n"
15126 "multiline raw string literal xxxxxxxxxxxxxx\n"
15127 ")x\", a), b);",
15128 getGoogleStyleWithColumns(20));
15130 verifyNoChange("fffffffffff(R\"x(\n"
15131 "multiline raw string literal xxxxxxxxxxxxxx\n"
15132 ")x\");",
15133 getGoogleStyleWithColumns(20));
15134 verifyFormat("fffffffffff(R\"x(\n"
15135 "multiline raw string literal xxxxxxxxxxxxxx\n"
15136 ")x\" + bbbbbb);",
15137 "fffffffffff(R\"x(\n"
15138 "multiline raw string literal xxxxxxxxxxxxxx\n"
15139 ")x\" + bbbbbb);",
15140 getGoogleStyleWithColumns(20));
15141 verifyFormat("fffffffffff(\n"
15142 " R\"x(\n"
15143 "multiline raw string literal xxxxxxxxxxxxxx\n"
15144 ")x\" +\n"
15145 " bbbbbb);",
15146 "fffffffffff(\n"
15147 " R\"x(\n"
15148 "multiline raw string literal xxxxxxxxxxxxxx\n"
15149 ")x\" + bbbbbb);",
15150 getGoogleStyleWithColumns(20));
15151 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
15152 "fffffffffff(\n"
15153 " R\"(single line raw string)\" + bbbbbb);");
15156 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15157 verifyFormat("string a = \"unterminated;");
15158 verifyFormat("function(\"unterminated,\n"
15159 " OtherParameter);",
15160 "function( \"unterminated,\n"
15161 " OtherParameter);");
15164 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15165 FormatStyle Style = getLLVMStyle();
15166 Style.Standard = FormatStyle::LS_Cpp03;
15167 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15168 "#define x(_a) printf(\"foo\"_a);", Style);
15171 TEST_F(FormatTest, CppLexVersion) {
15172 FormatStyle Style = getLLVMStyle();
15173 // Formatting of x * y differs if x is a type.
15174 verifyFormat("void foo() { MACRO(a * b); }", Style);
15175 verifyFormat("void foo() { MACRO(int *b); }", Style);
15177 // LLVM style uses latest lexer.
15178 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15179 Style.Standard = FormatStyle::LS_Cpp17;
15180 // But in c++17, char8_t isn't a keyword.
15181 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15184 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15186 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15187 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15188 " \"ddeeefff\");",
15189 "someFunction(\"aaabbbcccdddeeefff\");",
15190 getLLVMStyleWithColumns(25));
15191 verifyFormat("someFunction1234567890(\n"
15192 " \"aaabbbcccdddeeefff\");",
15193 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15194 getLLVMStyleWithColumns(26));
15195 verifyFormat("someFunction1234567890(\n"
15196 " \"aaabbbcccdddeeeff\"\n"
15197 " \"f\");",
15198 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15199 getLLVMStyleWithColumns(25));
15200 verifyFormat("someFunction1234567890(\n"
15201 " \"aaabbbcccdddeeeff\"\n"
15202 " \"f\");",
15203 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15204 getLLVMStyleWithColumns(24));
15205 verifyFormat("someFunction(\n"
15206 " \"aaabbbcc ddde \"\n"
15207 " \"efff\");",
15208 "someFunction(\"aaabbbcc ddde efff\");",
15209 getLLVMStyleWithColumns(25));
15210 verifyFormat("someFunction(\"aaabbbccc \"\n"
15211 " \"ddeeefff\");",
15212 "someFunction(\"aaabbbccc ddeeefff\");",
15213 getLLVMStyleWithColumns(25));
15214 verifyFormat("someFunction1234567890(\n"
15215 " \"aaabb \"\n"
15216 " \"cccdddeeefff\");",
15217 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15218 getLLVMStyleWithColumns(25));
15219 verifyFormat("#define A \\\n"
15220 " string s = \\\n"
15221 " \"123456789\" \\\n"
15222 " \"0\"; \\\n"
15223 " int i;",
15224 "#define A string s = \"1234567890\"; int i;",
15225 getLLVMStyleWithColumns(20));
15226 verifyFormat("someFunction(\n"
15227 " \"aaabbbcc \"\n"
15228 " \"dddeeefff\");",
15229 "someFunction(\"aaabbbcc dddeeefff\");",
15230 getLLVMStyleWithColumns(25));
15233 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15234 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15235 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15236 // FIXME: unstable test case
15237 EXPECT_EQ("\"test\"\n"
15238 "\"\\n\"",
15239 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15240 // FIXME: unstable test case
15241 EXPECT_EQ("\"tes\\\\\"\n"
15242 "\"n\"",
15243 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15244 // FIXME: unstable test case
15245 EXPECT_EQ("\"\\\\\\\\\"\n"
15246 "\"\\n\"",
15247 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15248 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15249 // FIXME: unstable test case
15250 EXPECT_EQ("\"\\uff01\"\n"
15251 "\"test\"",
15252 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15253 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15254 // FIXME: unstable test case
15255 EXPECT_EQ("\"\\x000000000001\"\n"
15256 "\"next\"",
15257 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15258 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15259 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15260 // FIXME: unstable test case
15261 EXPECT_EQ("\"test\"\n"
15262 "\"\\000000\"\n"
15263 "\"000001\"",
15264 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15265 // FIXME: unstable test case
15266 EXPECT_EQ("\"test\\000\"\n"
15267 "\"00000000\"\n"
15268 "\"1\"",
15269 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15272 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15273 verifyFormat("void f() {\n"
15274 " return g() {}\n"
15275 " void h() {}");
15276 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15277 "g();\n"
15278 "}");
15281 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15282 verifyFormat(
15283 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15286 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15287 verifyFormat("class X {\n"
15288 " void f() {\n"
15289 " }\n"
15290 "};",
15291 getLLVMStyleWithColumns(12));
15294 TEST_F(FormatTest, ConfigurableIndentWidth) {
15295 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15296 EightIndent.IndentWidth = 8;
15297 EightIndent.ContinuationIndentWidth = 8;
15298 verifyFormat("void f() {\n"
15299 " someFunction();\n"
15300 " if (true) {\n"
15301 " f();\n"
15302 " }\n"
15303 "}",
15304 EightIndent);
15305 verifyFormat("class X {\n"
15306 " void f() {\n"
15307 " }\n"
15308 "};",
15309 EightIndent);
15310 verifyFormat("int x[] = {\n"
15311 " call(),\n"
15312 " call()};",
15313 EightIndent);
15316 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15317 verifyFormat("double\n"
15318 "f();",
15319 getLLVMStyleWithColumns(8));
15322 TEST_F(FormatTest, ConfigurableUseOfTab) {
15323 FormatStyle Tab = getLLVMStyleWithColumns(42);
15324 Tab.IndentWidth = 8;
15325 Tab.UseTab = FormatStyle::UT_Always;
15326 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15328 verifyFormat("if (aaaaaaaa && // q\n"
15329 " bb)\t\t// w\n"
15330 "\t;",
15331 "if (aaaaaaaa &&// q\n"
15332 "bb)// w\n"
15333 ";",
15334 Tab);
15335 verifyFormat("if (aaa && bbb) // w\n"
15336 "\t;",
15337 "if(aaa&&bbb)// w\n"
15338 ";",
15339 Tab);
15341 verifyFormat("class X {\n"
15342 "\tvoid f() {\n"
15343 "\t\tsomeFunction(parameter1,\n"
15344 "\t\t\t parameter2);\n"
15345 "\t}\n"
15346 "};",
15347 Tab);
15348 verifyFormat("#define A \\\n"
15349 "\tvoid f() { \\\n"
15350 "\t\tsomeFunction( \\\n"
15351 "\t\t parameter1, \\\n"
15352 "\t\t parameter2); \\\n"
15353 "\t}",
15354 Tab);
15355 verifyFormat("int a;\t // x\n"
15356 "int bbbbbbbb; // x",
15357 Tab);
15359 FormatStyle TabAlignment = Tab;
15360 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
15361 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15362 verifyFormat("unsigned long long big;\n"
15363 "char*\t\t ptr;",
15364 TabAlignment);
15365 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15366 verifyFormat("unsigned long long big;\n"
15367 "char *\t\t ptr;",
15368 TabAlignment);
15369 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15370 verifyFormat("unsigned long long big;\n"
15371 "char\t\t *ptr;",
15372 TabAlignment);
15374 Tab.TabWidth = 4;
15375 Tab.IndentWidth = 8;
15376 verifyFormat("class TabWidth4Indent8 {\n"
15377 "\t\tvoid f() {\n"
15378 "\t\t\t\tsomeFunction(parameter1,\n"
15379 "\t\t\t\t\t\t\t parameter2);\n"
15380 "\t\t}\n"
15381 "};",
15382 Tab);
15384 Tab.TabWidth = 4;
15385 Tab.IndentWidth = 4;
15386 verifyFormat("class TabWidth4Indent4 {\n"
15387 "\tvoid f() {\n"
15388 "\t\tsomeFunction(parameter1,\n"
15389 "\t\t\t\t\t parameter2);\n"
15390 "\t}\n"
15391 "};",
15392 Tab);
15394 Tab.TabWidth = 8;
15395 Tab.IndentWidth = 4;
15396 verifyFormat("class TabWidth8Indent4 {\n"
15397 " void f() {\n"
15398 "\tsomeFunction(parameter1,\n"
15399 "\t\t parameter2);\n"
15400 " }\n"
15401 "};",
15402 Tab);
15404 Tab.TabWidth = 8;
15405 Tab.IndentWidth = 8;
15406 verifyFormat("/*\n"
15407 "\t a\t\tcomment\n"
15408 "\t in multiple lines\n"
15409 " */",
15410 " /*\t \t \n"
15411 " \t \t a\t\tcomment\t \t\n"
15412 " \t \t in multiple lines\t\n"
15413 " \t */",
15414 Tab);
15416 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
15417 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15418 verifyFormat("void f() {\n"
15419 "\tunsigned long long big;\n"
15420 "\tchar* ptr;\n"
15421 "}",
15422 TabAlignment);
15423 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15424 verifyFormat("void f() {\n"
15425 "\tunsigned long long big;\n"
15426 "\tchar * ptr;\n"
15427 "}",
15428 TabAlignment);
15429 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15430 verifyFormat("void f() {\n"
15431 "\tunsigned long long big;\n"
15432 "\tchar *ptr;\n"
15433 "}",
15434 TabAlignment);
15436 Tab.UseTab = FormatStyle::UT_ForIndentation;
15437 verifyFormat("{\n"
15438 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15439 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15440 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15441 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15442 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15443 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15444 "};",
15445 Tab);
15446 verifyFormat("enum AA {\n"
15447 "\ta1, // Force multiple lines\n"
15448 "\ta2,\n"
15449 "\ta3\n"
15450 "};",
15451 Tab);
15452 verifyFormat("if (aaaaaaaa && // q\n"
15453 " bb) // w\n"
15454 "\t;",
15455 "if (aaaaaaaa &&// q\n"
15456 "bb)// w\n"
15457 ";",
15458 Tab);
15459 verifyFormat("class X {\n"
15460 "\tvoid f() {\n"
15461 "\t\tsomeFunction(parameter1,\n"
15462 "\t\t parameter2);\n"
15463 "\t}\n"
15464 "};",
15465 Tab);
15466 verifyFormat("{\n"
15467 "\tQ(\n"
15468 "\t {\n"
15469 "\t\t int a;\n"
15470 "\t\t someFunction(aaaaaaaa,\n"
15471 "\t\t bbbbbbb);\n"
15472 "\t },\n"
15473 "\t p);\n"
15474 "}",
15475 Tab);
15476 verifyFormat("{\n"
15477 "\t/* aaaa\n"
15478 "\t bbbb */\n"
15479 "}",
15480 "{\n"
15481 "/* aaaa\n"
15482 " bbbb */\n"
15483 "}",
15484 Tab);
15485 verifyFormat("{\n"
15486 "\t/*\n"
15487 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15488 "\t bbbbbbbbbbbbb\n"
15489 "\t*/\n"
15490 "}",
15491 "{\n"
15492 "/*\n"
15493 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15494 "*/\n"
15495 "}",
15496 Tab);
15497 verifyFormat("{\n"
15498 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15499 "\t// bbbbbbbbbbbbb\n"
15500 "}",
15501 "{\n"
15502 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15503 "}",
15504 Tab);
15505 verifyFormat("{\n"
15506 "\t/*\n"
15507 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15508 "\t bbbbbbbbbbbbb\n"
15509 "\t*/\n"
15510 "}",
15511 "{\n"
15512 "\t/*\n"
15513 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15514 "\t*/\n"
15515 "}",
15516 Tab);
15517 verifyNoChange("{\n"
15518 "\t/*\n"
15519 "\n"
15520 "\t*/\n"
15521 "}",
15522 Tab);
15523 verifyNoChange("{\n"
15524 "\t/*\n"
15525 " asdf\n"
15526 "\t*/\n"
15527 "}",
15528 Tab);
15530 verifyFormat("void f() {\n"
15531 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
15532 "\t : bbbbbbbbbbbbbbbbbb\n"
15533 "}",
15534 Tab);
15535 FormatStyle TabNoBreak = Tab;
15536 TabNoBreak.BreakBeforeTernaryOperators = false;
15537 verifyFormat("void f() {\n"
15538 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
15539 "\t bbbbbbbbbbbbbbbbbb\n"
15540 "}",
15541 TabNoBreak);
15542 verifyFormat("void f() {\n"
15543 "\treturn true ?\n"
15544 "\t aaaaaaaaaaaaaaaaaaaa :\n"
15545 "\t bbbbbbbbbbbbbbbbbbbb\n"
15546 "}",
15547 TabNoBreak);
15549 Tab.UseTab = FormatStyle::UT_Never;
15550 verifyFormat("/*\n"
15551 " a\t\tcomment\n"
15552 " in multiple lines\n"
15553 " */",
15554 " /*\t \t \n"
15555 " \t \t a\t\tcomment\t \t\n"
15556 " \t \t in multiple lines\t\n"
15557 " \t */",
15558 Tab);
15559 verifyFormat("/* some\n"
15560 " comment */",
15561 " \t \t /* some\n"
15562 " \t \t comment */",
15563 Tab);
15564 verifyFormat("int a; /* some\n"
15565 " comment */",
15566 " \t \t int a; /* some\n"
15567 " \t \t comment */",
15568 Tab);
15570 verifyFormat("int a; /* some\n"
15571 "comment */",
15572 " \t \t int\ta; /* some\n"
15573 " \t \t comment */",
15574 Tab);
15575 verifyFormat("f(\"\t\t\"); /* some\n"
15576 " comment */",
15577 " \t \t f(\"\t\t\"); /* some\n"
15578 " \t \t comment */",
15579 Tab);
15580 verifyFormat("{\n"
15581 " /*\n"
15582 " * Comment\n"
15583 " */\n"
15584 " int i;\n"
15585 "}",
15586 "{\n"
15587 "\t/*\n"
15588 "\t * Comment\n"
15589 "\t */\n"
15590 "\t int i;\n"
15591 "}",
15592 Tab);
15594 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
15595 Tab.TabWidth = 8;
15596 Tab.IndentWidth = 8;
15597 verifyFormat("if (aaaaaaaa && // q\n"
15598 " bb) // w\n"
15599 "\t;",
15600 "if (aaaaaaaa &&// q\n"
15601 "bb)// w\n"
15602 ";",
15603 Tab);
15604 verifyFormat("if (aaa && bbb) // w\n"
15605 "\t;",
15606 "if(aaa&&bbb)// w\n"
15607 ";",
15608 Tab);
15609 verifyFormat("class X {\n"
15610 "\tvoid f() {\n"
15611 "\t\tsomeFunction(parameter1,\n"
15612 "\t\t\t parameter2);\n"
15613 "\t}\n"
15614 "};",
15615 Tab);
15616 verifyFormat("#define A \\\n"
15617 "\tvoid f() { \\\n"
15618 "\t\tsomeFunction( \\\n"
15619 "\t\t parameter1, \\\n"
15620 "\t\t parameter2); \\\n"
15621 "\t}",
15622 Tab);
15623 Tab.TabWidth = 4;
15624 Tab.IndentWidth = 8;
15625 verifyFormat("class TabWidth4Indent8 {\n"
15626 "\t\tvoid f() {\n"
15627 "\t\t\t\tsomeFunction(parameter1,\n"
15628 "\t\t\t\t\t\t\t parameter2);\n"
15629 "\t\t}\n"
15630 "};",
15631 Tab);
15632 Tab.TabWidth = 4;
15633 Tab.IndentWidth = 4;
15634 verifyFormat("class TabWidth4Indent4 {\n"
15635 "\tvoid f() {\n"
15636 "\t\tsomeFunction(parameter1,\n"
15637 "\t\t\t\t\t parameter2);\n"
15638 "\t}\n"
15639 "};",
15640 Tab);
15641 Tab.TabWidth = 8;
15642 Tab.IndentWidth = 4;
15643 verifyFormat("class TabWidth8Indent4 {\n"
15644 " void f() {\n"
15645 "\tsomeFunction(parameter1,\n"
15646 "\t\t parameter2);\n"
15647 " }\n"
15648 "};",
15649 Tab);
15650 Tab.TabWidth = 8;
15651 Tab.IndentWidth = 8;
15652 verifyFormat("/*\n"
15653 "\t a\t\tcomment\n"
15654 "\t in multiple lines\n"
15655 " */",
15656 " /*\t \t \n"
15657 " \t \t a\t\tcomment\t \t\n"
15658 " \t \t in multiple lines\t\n"
15659 " \t */",
15660 Tab);
15661 verifyFormat("{\n"
15662 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15663 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15664 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15665 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15666 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15667 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15668 "};",
15669 Tab);
15670 verifyFormat("enum AA {\n"
15671 "\ta1, // Force multiple lines\n"
15672 "\ta2,\n"
15673 "\ta3\n"
15674 "};",
15675 Tab);
15676 verifyFormat("if (aaaaaaaa && // q\n"
15677 " bb) // w\n"
15678 "\t;",
15679 "if (aaaaaaaa &&// q\n"
15680 "bb)// w\n"
15681 ";",
15682 Tab);
15683 verifyFormat("class X {\n"
15684 "\tvoid f() {\n"
15685 "\t\tsomeFunction(parameter1,\n"
15686 "\t\t\t parameter2);\n"
15687 "\t}\n"
15688 "};",
15689 Tab);
15690 verifyFormat("{\n"
15691 "\tQ(\n"
15692 "\t {\n"
15693 "\t\t int a;\n"
15694 "\t\t someFunction(aaaaaaaa,\n"
15695 "\t\t\t\t bbbbbbb);\n"
15696 "\t },\n"
15697 "\t p);\n"
15698 "}",
15699 Tab);
15700 verifyFormat("{\n"
15701 "\t/* aaaa\n"
15702 "\t bbbb */\n"
15703 "}",
15704 "{\n"
15705 "/* aaaa\n"
15706 " bbbb */\n"
15707 "}",
15708 Tab);
15709 verifyFormat("{\n"
15710 "\t/*\n"
15711 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15712 "\t bbbbbbbbbbbbb\n"
15713 "\t*/\n"
15714 "}",
15715 "{\n"
15716 "/*\n"
15717 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15718 "*/\n"
15719 "}",
15720 Tab);
15721 verifyFormat("{\n"
15722 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15723 "\t// bbbbbbbbbbbbb\n"
15724 "}",
15725 "{\n"
15726 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15727 "}",
15728 Tab);
15729 verifyFormat("{\n"
15730 "\t/*\n"
15731 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15732 "\t bbbbbbbbbbbbb\n"
15733 "\t*/\n"
15734 "}",
15735 "{\n"
15736 "\t/*\n"
15737 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15738 "\t*/\n"
15739 "}",
15740 Tab);
15741 verifyNoChange("{\n"
15742 "\t/*\n"
15743 "\n"
15744 "\t*/\n"
15745 "}",
15746 Tab);
15747 verifyNoChange("{\n"
15748 "\t/*\n"
15749 " asdf\n"
15750 "\t*/\n"
15751 "}",
15752 Tab);
15753 verifyFormat("/* some\n"
15754 " comment */",
15755 " \t \t /* some\n"
15756 " \t \t comment */",
15757 Tab);
15758 verifyFormat("int a; /* some\n"
15759 " comment */",
15760 " \t \t int a; /* some\n"
15761 " \t \t comment */",
15762 Tab);
15763 verifyFormat("int a; /* some\n"
15764 "comment */",
15765 " \t \t int\ta; /* some\n"
15766 " \t \t comment */",
15767 Tab);
15768 verifyFormat("f(\"\t\t\"); /* some\n"
15769 " comment */",
15770 " \t \t f(\"\t\t\"); /* some\n"
15771 " \t \t comment */",
15772 Tab);
15773 verifyFormat("{\n"
15774 "\t/*\n"
15775 "\t * Comment\n"
15776 "\t */\n"
15777 "\tint i;\n"
15778 "}",
15779 "{\n"
15780 "\t/*\n"
15781 "\t * Comment\n"
15782 "\t */\n"
15783 "\t int i;\n"
15784 "}",
15785 Tab);
15786 Tab.TabWidth = 2;
15787 Tab.IndentWidth = 2;
15788 verifyFormat("{\n"
15789 "\t/* aaaa\n"
15790 "\t\t bbbb */\n"
15791 "}",
15792 "{\n"
15793 "/* aaaa\n"
15794 "\t bbbb */\n"
15795 "}",
15796 Tab);
15797 verifyFormat("{\n"
15798 "\t/*\n"
15799 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15800 "\t\tbbbbbbbbbbbbb\n"
15801 "\t*/\n"
15802 "}",
15803 "{\n"
15804 "/*\n"
15805 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15806 "*/\n"
15807 "}",
15808 Tab);
15809 Tab.AlignConsecutiveAssignments.Enabled = true;
15810 Tab.AlignConsecutiveDeclarations.Enabled = true;
15811 Tab.TabWidth = 4;
15812 Tab.IndentWidth = 4;
15813 verifyFormat("class Assign {\n"
15814 "\tvoid f() {\n"
15815 "\t\tint x = 123;\n"
15816 "\t\tint random = 4;\n"
15817 "\t\tstd::string alphabet =\n"
15818 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
15819 "\t}\n"
15820 "};",
15821 Tab);
15823 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
15824 Tab.TabWidth = 8;
15825 Tab.IndentWidth = 8;
15826 verifyFormat("if (aaaaaaaa && // q\n"
15827 " bb) // w\n"
15828 "\t;",
15829 "if (aaaaaaaa &&// q\n"
15830 "bb)// w\n"
15831 ";",
15832 Tab);
15833 verifyFormat("if (aaa && bbb) // w\n"
15834 "\t;",
15835 "if(aaa&&bbb)// w\n"
15836 ";",
15837 Tab);
15838 verifyFormat("class X {\n"
15839 "\tvoid f() {\n"
15840 "\t\tsomeFunction(parameter1,\n"
15841 "\t\t parameter2);\n"
15842 "\t}\n"
15843 "};",
15844 Tab);
15845 verifyFormat("#define A \\\n"
15846 "\tvoid f() { \\\n"
15847 "\t\tsomeFunction( \\\n"
15848 "\t\t parameter1, \\\n"
15849 "\t\t parameter2); \\\n"
15850 "\t}",
15851 Tab);
15852 Tab.TabWidth = 4;
15853 Tab.IndentWidth = 8;
15854 verifyFormat("class TabWidth4Indent8 {\n"
15855 "\t\tvoid f() {\n"
15856 "\t\t\t\tsomeFunction(parameter1,\n"
15857 "\t\t\t\t parameter2);\n"
15858 "\t\t}\n"
15859 "};",
15860 Tab);
15861 Tab.TabWidth = 4;
15862 Tab.IndentWidth = 4;
15863 verifyFormat("class TabWidth4Indent4 {\n"
15864 "\tvoid f() {\n"
15865 "\t\tsomeFunction(parameter1,\n"
15866 "\t\t parameter2);\n"
15867 "\t}\n"
15868 "};",
15869 Tab);
15870 Tab.TabWidth = 8;
15871 Tab.IndentWidth = 4;
15872 verifyFormat("class TabWidth8Indent4 {\n"
15873 " void f() {\n"
15874 "\tsomeFunction(parameter1,\n"
15875 "\t parameter2);\n"
15876 " }\n"
15877 "};",
15878 Tab);
15879 Tab.TabWidth = 8;
15880 Tab.IndentWidth = 8;
15881 verifyFormat("/*\n"
15882 " a\t\tcomment\n"
15883 " in multiple lines\n"
15884 " */",
15885 " /*\t \t \n"
15886 " \t \t a\t\tcomment\t \t\n"
15887 " \t \t in multiple lines\t\n"
15888 " \t */",
15889 Tab);
15890 verifyFormat("{\n"
15891 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15892 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15893 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15894 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15895 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15896 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15897 "};",
15898 Tab);
15899 verifyFormat("enum AA {\n"
15900 "\ta1, // Force multiple lines\n"
15901 "\ta2,\n"
15902 "\ta3\n"
15903 "};",
15904 Tab);
15905 verifyFormat("if (aaaaaaaa && // q\n"
15906 " bb) // w\n"
15907 "\t;",
15908 "if (aaaaaaaa &&// q\n"
15909 "bb)// w\n"
15910 ";",
15911 Tab);
15912 verifyFormat("class X {\n"
15913 "\tvoid f() {\n"
15914 "\t\tsomeFunction(parameter1,\n"
15915 "\t\t parameter2);\n"
15916 "\t}\n"
15917 "};",
15918 Tab);
15919 verifyFormat("{\n"
15920 "\tQ(\n"
15921 "\t {\n"
15922 "\t\t int a;\n"
15923 "\t\t someFunction(aaaaaaaa,\n"
15924 "\t\t bbbbbbb);\n"
15925 "\t },\n"
15926 "\t p);\n"
15927 "}",
15928 Tab);
15929 verifyFormat("{\n"
15930 "\t/* aaaa\n"
15931 "\t bbbb */\n"
15932 "}",
15933 "{\n"
15934 "/* aaaa\n"
15935 " bbbb */\n"
15936 "}",
15937 Tab);
15938 verifyFormat("{\n"
15939 "\t/*\n"
15940 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15941 "\t bbbbbbbbbbbbb\n"
15942 "\t*/\n"
15943 "}",
15944 "{\n"
15945 "/*\n"
15946 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15947 "*/\n"
15948 "}",
15949 Tab);
15950 verifyFormat("{\n"
15951 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15952 "\t// bbbbbbbbbbbbb\n"
15953 "}",
15954 "{\n"
15955 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15956 "}",
15957 Tab);
15958 verifyFormat("{\n"
15959 "\t/*\n"
15960 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15961 "\t bbbbbbbbbbbbb\n"
15962 "\t*/\n"
15963 "}",
15964 "{\n"
15965 "\t/*\n"
15966 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15967 "\t*/\n"
15968 "}",
15969 Tab);
15970 verifyNoChange("{\n"
15971 "\t/*\n"
15972 "\n"
15973 "\t*/\n"
15974 "}",
15975 Tab);
15976 verifyNoChange("{\n"
15977 "\t/*\n"
15978 " asdf\n"
15979 "\t*/\n"
15980 "}",
15981 Tab);
15982 verifyFormat("/* some\n"
15983 " comment */",
15984 " \t \t /* some\n"
15985 " \t \t comment */",
15986 Tab);
15987 verifyFormat("int a; /* some\n"
15988 " comment */",
15989 " \t \t int a; /* some\n"
15990 " \t \t comment */",
15991 Tab);
15992 verifyFormat("int a; /* some\n"
15993 "comment */",
15994 " \t \t int\ta; /* some\n"
15995 " \t \t comment */",
15996 Tab);
15997 verifyFormat("f(\"\t\t\"); /* some\n"
15998 " comment */",
15999 " \t \t f(\"\t\t\"); /* some\n"
16000 " \t \t comment */",
16001 Tab);
16002 verifyFormat("{\n"
16003 "\t/*\n"
16004 "\t * Comment\n"
16005 "\t */\n"
16006 "\tint i;\n"
16007 "}",
16008 "{\n"
16009 "\t/*\n"
16010 "\t * Comment\n"
16011 "\t */\n"
16012 "\t int i;\n"
16013 "}",
16014 Tab);
16015 Tab.TabWidth = 2;
16016 Tab.IndentWidth = 2;
16017 verifyFormat("{\n"
16018 "\t/* aaaa\n"
16019 "\t bbbb */\n"
16020 "}",
16021 "{\n"
16022 "/* aaaa\n"
16023 " bbbb */\n"
16024 "}",
16025 Tab);
16026 verifyFormat("{\n"
16027 "\t/*\n"
16028 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16029 "\t bbbbbbbbbbbbb\n"
16030 "\t*/\n"
16031 "}",
16032 "{\n"
16033 "/*\n"
16034 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16035 "*/\n"
16036 "}",
16037 Tab);
16038 Tab.AlignConsecutiveAssignments.Enabled = true;
16039 Tab.AlignConsecutiveDeclarations.Enabled = true;
16040 Tab.TabWidth = 4;
16041 Tab.IndentWidth = 4;
16042 verifyFormat("class Assign {\n"
16043 "\tvoid f() {\n"
16044 "\t\tint x = 123;\n"
16045 "\t\tint random = 4;\n"
16046 "\t\tstd::string alphabet =\n"
16047 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16048 "\t}\n"
16049 "};",
16050 Tab);
16051 Tab.AlignOperands = FormatStyle::OAS_Align;
16052 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16053 " cccccccccccccccccccc;",
16054 Tab);
16055 // no alignment
16056 verifyFormat("int aaaaaaaaaa =\n"
16057 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16058 Tab);
16059 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16060 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16061 " : 333333333333333;",
16062 Tab);
16063 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16064 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16065 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16066 " + cccccccccccccccccccc;",
16067 Tab);
16070 TEST_F(FormatTest, ZeroTabWidth) {
16071 FormatStyle Tab = getLLVMStyleWithColumns(42);
16072 Tab.IndentWidth = 8;
16073 Tab.UseTab = FormatStyle::UT_Never;
16074 Tab.TabWidth = 0;
16075 verifyFormat("void a(){\n"
16076 " // line starts with '\t'\n"
16077 "};",
16078 "void a(){\n"
16079 "\t// line starts with '\t'\n"
16080 "};",
16081 Tab);
16083 verifyFormat("void a(){\n"
16084 " // line starts with '\t'\n"
16085 "};",
16086 "void a(){\n"
16087 "\t\t// line starts with '\t'\n"
16088 "};",
16089 Tab);
16091 Tab.UseTab = FormatStyle::UT_ForIndentation;
16092 verifyFormat("void a(){\n"
16093 " // line starts with '\t'\n"
16094 "};",
16095 "void a(){\n"
16096 "\t// line starts with '\t'\n"
16097 "};",
16098 Tab);
16100 verifyFormat("void a(){\n"
16101 " // line starts with '\t'\n"
16102 "};",
16103 "void a(){\n"
16104 "\t\t// line starts with '\t'\n"
16105 "};",
16106 Tab);
16108 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16109 verifyFormat("void a(){\n"
16110 " // line starts with '\t'\n"
16111 "};",
16112 "void a(){\n"
16113 "\t// line starts with '\t'\n"
16114 "};",
16115 Tab);
16117 verifyFormat("void a(){\n"
16118 " // line starts with '\t'\n"
16119 "};",
16120 "void a(){\n"
16121 "\t\t// line starts with '\t'\n"
16122 "};",
16123 Tab);
16125 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16126 verifyFormat("void a(){\n"
16127 " // line starts with '\t'\n"
16128 "};",
16129 "void a(){\n"
16130 "\t// line starts with '\t'\n"
16131 "};",
16132 Tab);
16134 verifyFormat("void a(){\n"
16135 " // line starts with '\t'\n"
16136 "};",
16137 "void a(){\n"
16138 "\t\t// line starts with '\t'\n"
16139 "};",
16140 Tab);
16142 Tab.UseTab = FormatStyle::UT_Always;
16143 verifyFormat("void a(){\n"
16144 "// line starts with '\t'\n"
16145 "};",
16146 "void a(){\n"
16147 "\t// line starts with '\t'\n"
16148 "};",
16149 Tab);
16151 verifyFormat("void a(){\n"
16152 "// line starts with '\t'\n"
16153 "};",
16154 "void a(){\n"
16155 "\t\t// line starts with '\t'\n"
16156 "};",
16157 Tab);
16160 TEST_F(FormatTest, CalculatesOriginalColumn) {
16161 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16162 "q\"; /* some\n"
16163 " comment */",
16164 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16165 "q\"; /* some\n"
16166 " comment */");
16167 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16168 "/* some\n"
16169 " comment */",
16170 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16171 " /* some\n"
16172 " comment */");
16173 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16174 "qqq\n"
16175 "/* some\n"
16176 " comment */",
16177 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16178 "qqq\n"
16179 " /* some\n"
16180 " comment */");
16181 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16182 "wwww; /* some\n"
16183 " comment */",
16184 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16185 "wwww; /* some\n"
16186 " comment */");
16189 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
16190 FormatStyle NoSpace = getLLVMStyle();
16191 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
16193 verifyFormat("while(true)\n"
16194 " continue;",
16195 NoSpace);
16196 verifyFormat("for(;;)\n"
16197 " continue;",
16198 NoSpace);
16199 verifyFormat("if(true)\n"
16200 " f();\n"
16201 "else if(true)\n"
16202 " f();",
16203 NoSpace);
16204 verifyFormat("do {\n"
16205 " do_something();\n"
16206 "} while(something());",
16207 NoSpace);
16208 verifyFormat("switch(x) {\n"
16209 "default:\n"
16210 " break;\n"
16211 "}",
16212 NoSpace);
16213 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
16214 verifyFormat("size_t x = sizeof(x);", NoSpace);
16215 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
16216 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
16217 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
16218 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
16219 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
16220 verifyFormat("alignas(128) char a[128];", NoSpace);
16221 verifyFormat("size_t x = alignof(MyType);", NoSpace);
16222 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
16223 verifyFormat("int f() throw(Deprecated);", NoSpace);
16224 verifyFormat("typedef void (*cb)(int);", NoSpace);
16225 verifyFormat("T A::operator()();", NoSpace);
16226 verifyFormat("X A::operator++(T);", NoSpace);
16227 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
16229 FormatStyle Space = getLLVMStyle();
16230 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
16232 verifyFormat("int f ();", Space);
16233 verifyFormat("bool operator< ();", Space);
16234 verifyFormat("bool operator> ();", Space);
16235 verifyFormat("void f (int a, T b) {\n"
16236 " while (true)\n"
16237 " continue;\n"
16238 "}",
16239 Space);
16240 verifyFormat("if (true)\n"
16241 " f ();\n"
16242 "else if (true)\n"
16243 " f ();",
16244 Space);
16245 verifyFormat("do {\n"
16246 " do_something ();\n"
16247 "} while (something ());",
16248 Space);
16249 verifyFormat("switch (x) {\n"
16250 "default:\n"
16251 " break;\n"
16252 "}",
16253 Space);
16254 verifyFormat("A::A () : a (1) {}", Space);
16255 verifyFormat("void f () __attribute__ ((asdf));", Space);
16256 verifyFormat("*(&a + 1);\n"
16257 "&((&a)[1]);\n"
16258 "a[(b + c) * d];\n"
16259 "(((a + 1) * 2) + 3) * 4;",
16260 Space);
16261 verifyFormat("#define A(x) x", Space);
16262 verifyFormat("#define A (x) x", Space);
16263 verifyFormat("#if defined(x)\n"
16264 "#endif",
16265 Space);
16266 verifyFormat("auto i = std::make_unique<int> (5);", Space);
16267 verifyFormat("size_t x = sizeof (x);", Space);
16268 verifyFormat("auto f (int x) -> decltype (x);", Space);
16269 verifyFormat("auto f (int x) -> typeof (x);", Space);
16270 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
16271 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
16272 verifyFormat("int f (T x) noexcept (x.create ());", Space);
16273 verifyFormat("alignas (128) char a[128];", Space);
16274 verifyFormat("size_t x = alignof (MyType);", Space);
16275 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
16276 verifyFormat("int f () throw (Deprecated);", Space);
16277 verifyFormat("typedef void (*cb) (int);", Space);
16278 // FIXME these tests regressed behaviour.
16279 // verifyFormat("T A::operator() ();", Space);
16280 // verifyFormat("X A::operator++ (T);", Space);
16281 verifyFormat("auto lambda = [] () { return 0; };", Space);
16282 verifyFormat("int x = int (y);", Space);
16283 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
16284 verifyFormat("__builtin_LINE ()", Space);
16285 verifyFormat("__builtin_UNKNOWN ()", Space);
16287 FormatStyle SomeSpace = getLLVMStyle();
16288 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
16290 verifyFormat("[]() -> float {}", SomeSpace);
16291 verifyFormat("[] (auto foo) {}", SomeSpace);
16292 verifyFormat("[foo]() -> int {}", SomeSpace);
16293 verifyFormat("int f();", SomeSpace);
16294 verifyFormat("void f (int a, T b) {\n"
16295 " while (true)\n"
16296 " continue;\n"
16297 "}",
16298 SomeSpace);
16299 verifyFormat("if (true)\n"
16300 " f();\n"
16301 "else if (true)\n"
16302 " f();",
16303 SomeSpace);
16304 verifyFormat("do {\n"
16305 " do_something();\n"
16306 "} while (something());",
16307 SomeSpace);
16308 verifyFormat("switch (x) {\n"
16309 "default:\n"
16310 " break;\n"
16311 "}",
16312 SomeSpace);
16313 verifyFormat("A::A() : a (1) {}", SomeSpace);
16314 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
16315 verifyFormat("*(&a + 1);\n"
16316 "&((&a)[1]);\n"
16317 "a[(b + c) * d];\n"
16318 "(((a + 1) * 2) + 3) * 4;",
16319 SomeSpace);
16320 verifyFormat("#define A(x) x", SomeSpace);
16321 verifyFormat("#define A (x) x", SomeSpace);
16322 verifyFormat("#if defined(x)\n"
16323 "#endif",
16324 SomeSpace);
16325 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
16326 verifyFormat("size_t x = sizeof (x);", SomeSpace);
16327 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
16328 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
16329 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
16330 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
16331 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
16332 verifyFormat("alignas (128) char a[128];", SomeSpace);
16333 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
16334 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16335 SomeSpace);
16336 verifyFormat("int f() throw (Deprecated);", SomeSpace);
16337 verifyFormat("typedef void (*cb) (int);", SomeSpace);
16338 verifyFormat("T A::operator()();", SomeSpace);
16339 // FIXME these tests regressed behaviour.
16340 // verifyFormat("X A::operator++ (T);", SomeSpace);
16341 verifyFormat("int x = int (y);", SomeSpace);
16342 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
16344 FormatStyle SpaceControlStatements = getLLVMStyle();
16345 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16346 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
16348 verifyFormat("while (true)\n"
16349 " continue;",
16350 SpaceControlStatements);
16351 verifyFormat("if (true)\n"
16352 " f();\n"
16353 "else if (true)\n"
16354 " f();",
16355 SpaceControlStatements);
16356 verifyFormat("for (;;) {\n"
16357 " do_something();\n"
16358 "}",
16359 SpaceControlStatements);
16360 verifyFormat("do {\n"
16361 " do_something();\n"
16362 "} while (something());",
16363 SpaceControlStatements);
16364 verifyFormat("switch (x) {\n"
16365 "default:\n"
16366 " break;\n"
16367 "}",
16368 SpaceControlStatements);
16370 FormatStyle SpaceFuncDecl = getLLVMStyle();
16371 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16372 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
16374 verifyFormat("int f ();", SpaceFuncDecl);
16375 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
16376 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
16377 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
16378 verifyFormat("#define A(x) x", SpaceFuncDecl);
16379 verifyFormat("#define A (x) x", SpaceFuncDecl);
16380 verifyFormat("#if defined(x)\n"
16381 "#endif",
16382 SpaceFuncDecl);
16383 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
16384 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
16385 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
16386 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
16387 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
16388 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
16389 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
16390 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
16391 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
16392 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16393 SpaceFuncDecl);
16394 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
16395 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
16396 // FIXME these tests regressed behaviour.
16397 // verifyFormat("T A::operator() ();", SpaceFuncDecl);
16398 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
16399 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
16400 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
16401 verifyFormat("int x = int(y);", SpaceFuncDecl);
16402 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16403 SpaceFuncDecl);
16405 FormatStyle SpaceFuncDef = getLLVMStyle();
16406 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16407 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
16409 verifyFormat("int f();", SpaceFuncDef);
16410 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16411 verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
16412 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
16413 verifyFormat("#define A(x) x", SpaceFuncDef);
16414 verifyFormat("#define A (x) x", SpaceFuncDef);
16415 verifyFormat("#if defined(x)\n"
16416 "#endif",
16417 SpaceFuncDef);
16418 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
16419 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
16420 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
16421 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
16422 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
16423 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
16424 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
16425 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
16426 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
16427 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16428 SpaceFuncDef);
16429 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
16430 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
16431 verifyFormat("T A::operator()();", SpaceFuncDef);
16432 verifyFormat("X A::operator++(T);", SpaceFuncDef);
16433 // verifyFormat("T A::operator() () {}", SpaceFuncDef);
16434 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
16435 verifyFormat("int x = int(y);", SpaceFuncDef);
16436 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16437 SpaceFuncDef);
16439 FormatStyle SpaceIfMacros = getLLVMStyle();
16440 SpaceIfMacros.IfMacros.clear();
16441 SpaceIfMacros.IfMacros.push_back("MYIF");
16442 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16443 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
16444 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
16445 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
16446 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
16448 FormatStyle SpaceForeachMacros = getLLVMStyle();
16449 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
16450 FormatStyle::SBS_Never);
16451 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
16452 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16453 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
16454 verifyFormat("for (;;) {\n"
16455 "}",
16456 SpaceForeachMacros);
16457 verifyFormat("foreach (Item *item, itemlist) {\n"
16458 "}",
16459 SpaceForeachMacros);
16460 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
16461 "}",
16462 SpaceForeachMacros);
16463 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
16464 "}",
16465 SpaceForeachMacros);
16466 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
16468 FormatStyle SomeSpace2 = getLLVMStyle();
16469 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16470 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
16471 verifyFormat("[]() -> float {}", SomeSpace2);
16472 verifyFormat("[] (auto foo) {}", SomeSpace2);
16473 verifyFormat("[foo]() -> int {}", SomeSpace2);
16474 verifyFormat("int f();", SomeSpace2);
16475 verifyFormat("void f (int a, T b) {\n"
16476 " while (true)\n"
16477 " continue;\n"
16478 "}",
16479 SomeSpace2);
16480 verifyFormat("if (true)\n"
16481 " f();\n"
16482 "else if (true)\n"
16483 " f();",
16484 SomeSpace2);
16485 verifyFormat("do {\n"
16486 " do_something();\n"
16487 "} while (something());",
16488 SomeSpace2);
16489 verifyFormat("switch (x) {\n"
16490 "default:\n"
16491 " break;\n"
16492 "}",
16493 SomeSpace2);
16494 verifyFormat("A::A() : a (1) {}", SomeSpace2);
16495 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
16496 verifyFormat("*(&a + 1);\n"
16497 "&((&a)[1]);\n"
16498 "a[(b + c) * d];\n"
16499 "(((a + 1) * 2) + 3) * 4;",
16500 SomeSpace2);
16501 verifyFormat("#define A(x) x", SomeSpace2);
16502 verifyFormat("#define A (x) x", SomeSpace2);
16503 verifyFormat("#if defined(x)\n"
16504 "#endif",
16505 SomeSpace2);
16506 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
16507 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
16508 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
16509 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
16510 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
16511 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
16512 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
16513 verifyFormat("alignas (128) char a[128];", SomeSpace2);
16514 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
16515 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16516 SomeSpace2);
16517 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
16518 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
16519 verifyFormat("T A::operator()();", SomeSpace2);
16520 // verifyFormat("X A::operator++ (T);", SomeSpace2);
16521 verifyFormat("int x = int (y);", SomeSpace2);
16522 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
16524 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
16525 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16526 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
16527 .AfterOverloadedOperator = true;
16529 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
16530 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
16531 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
16532 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
16534 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
16535 .AfterOverloadedOperator = false;
16537 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
16538 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
16539 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
16540 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
16542 auto SpaceAfterRequires = getLLVMStyle();
16543 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16544 EXPECT_FALSE(
16545 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
16546 EXPECT_FALSE(
16547 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
16548 verifyFormat("void f(auto x)\n"
16549 " requires requires(int i) { x + i; }\n"
16550 "{}",
16551 SpaceAfterRequires);
16552 verifyFormat("void f(auto x)\n"
16553 " requires(requires(int i) { x + i; })\n"
16554 "{}",
16555 SpaceAfterRequires);
16556 verifyFormat("if (requires(int i) { x + i; })\n"
16557 " return;",
16558 SpaceAfterRequires);
16559 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
16560 verifyFormat("template <typename T>\n"
16561 " requires(Foo<T>)\n"
16562 "class Bar;",
16563 SpaceAfterRequires);
16565 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
16566 verifyFormat("void f(auto x)\n"
16567 " requires requires(int i) { x + i; }\n"
16568 "{}",
16569 SpaceAfterRequires);
16570 verifyFormat("void f(auto x)\n"
16571 " requires (requires(int i) { x + i; })\n"
16572 "{}",
16573 SpaceAfterRequires);
16574 verifyFormat("if (requires(int i) { x + i; })\n"
16575 " return;",
16576 SpaceAfterRequires);
16577 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
16578 verifyFormat("template <typename T>\n"
16579 " requires (Foo<T>)\n"
16580 "class Bar;",
16581 SpaceAfterRequires);
16583 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
16584 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
16585 verifyFormat("void f(auto x)\n"
16586 " requires requires (int i) { x + i; }\n"
16587 "{}",
16588 SpaceAfterRequires);
16589 verifyFormat("void f(auto x)\n"
16590 " requires(requires (int i) { x + i; })\n"
16591 "{}",
16592 SpaceAfterRequires);
16593 verifyFormat("if (requires (int i) { x + i; })\n"
16594 " return;",
16595 SpaceAfterRequires);
16596 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
16597 verifyFormat("template <typename T>\n"
16598 " requires(Foo<T>)\n"
16599 "class Bar;",
16600 SpaceAfterRequires);
16602 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
16603 verifyFormat("void f(auto x)\n"
16604 " requires requires (int i) { x + i; }\n"
16605 "{}",
16606 SpaceAfterRequires);
16607 verifyFormat("void f(auto x)\n"
16608 " requires (requires (int i) { x + i; })\n"
16609 "{}",
16610 SpaceAfterRequires);
16611 verifyFormat("if (requires (int i) { x + i; })\n"
16612 " return;",
16613 SpaceAfterRequires);
16614 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
16615 verifyFormat("template <typename T>\n"
16616 " requires (Foo<T>)\n"
16617 "class Bar;",
16618 SpaceAfterRequires);
16621 TEST_F(FormatTest, SpaceAfterLogicalNot) {
16622 FormatStyle Spaces = getLLVMStyle();
16623 Spaces.SpaceAfterLogicalNot = true;
16625 verifyFormat("bool x = ! y", Spaces);
16626 verifyFormat("if (! isFailure())", Spaces);
16627 verifyFormat("if (! (a && b))", Spaces);
16628 verifyFormat("\"Error!\"", Spaces);
16629 verifyFormat("! ! x", Spaces);
16632 TEST_F(FormatTest, ConfigurableSpacesInParens) {
16633 FormatStyle Spaces = getLLVMStyle();
16635 verifyFormat("do_something(::globalVar);", Spaces);
16636 verifyFormat("call(x, y, z);", Spaces);
16637 verifyFormat("call();", Spaces);
16638 verifyFormat("std::function<void(int, int)> callback;", Spaces);
16639 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
16640 Spaces);
16641 verifyFormat("while ((bool)1)\n"
16642 " continue;",
16643 Spaces);
16644 verifyFormat("for (;;)\n"
16645 " continue;",
16646 Spaces);
16647 verifyFormat("if (true)\n"
16648 " f();\n"
16649 "else if (true)\n"
16650 " f();",
16651 Spaces);
16652 verifyFormat("do {\n"
16653 " do_something((int)i);\n"
16654 "} while (something());",
16655 Spaces);
16656 verifyFormat("switch (x) {\n"
16657 "default:\n"
16658 " break;\n"
16659 "}",
16660 Spaces);
16661 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
16662 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
16663 verifyFormat("void f() __attribute__((asdf));", Spaces);
16665 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
16666 Spaces.SpacesInParensOptions = {};
16667 Spaces.SpacesInParensOptions.Other = true;
16668 Spaces.SpacesInParensOptions.InConditionalStatements = true;
16669 verifyFormat("do_something( ::globalVar );", Spaces);
16670 verifyFormat("call( x, y, z );", Spaces);
16671 verifyFormat("call();", Spaces);
16672 verifyFormat("std::function<void( int, int )> callback;", Spaces);
16673 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
16674 Spaces);
16675 verifyFormat("while ( (bool)1 )\n"
16676 " continue;",
16677 Spaces);
16678 verifyFormat("for ( ;; )\n"
16679 " continue;",
16680 Spaces);
16681 verifyFormat("if ( true )\n"
16682 " f();\n"
16683 "else if ( true )\n"
16684 " f();",
16685 Spaces);
16686 verifyFormat("do {\n"
16687 " do_something( (int)i );\n"
16688 "} while ( something() );",
16689 Spaces);
16690 verifyFormat("switch ( x ) {\n"
16691 "default:\n"
16692 " break;\n"
16693 "}",
16694 Spaces);
16695 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
16696 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
16697 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
16699 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
16700 Spaces.SpacesInParensOptions = {};
16701 Spaces.SpacesInParensOptions.InCStyleCasts = true;
16702 verifyFormat("Type *A = ( Type * )P;", Spaces);
16703 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
16704 verifyFormat("x = ( int32 )y;", Spaces);
16705 verifyFormat("throw ( int32 )x;", Spaces);
16706 verifyFormat("int a = ( int )(2.0f);", Spaces);
16707 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
16708 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
16709 verifyFormat("#define x (( int )-1)", Spaces);
16711 // Run the first set of tests again with:
16712 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
16713 Spaces.SpacesInParensOptions = {};
16714 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
16715 Spaces.SpacesInParensOptions.InCStyleCasts = true;
16716 verifyFormat("call(x, y, z);", Spaces);
16717 verifyFormat("call( );", Spaces);
16718 verifyFormat("std::function<void(int, int)> callback;", Spaces);
16719 verifyFormat("while (( bool )1)\n"
16720 " continue;",
16721 Spaces);
16722 verifyFormat("for (;;)\n"
16723 " continue;",
16724 Spaces);
16725 verifyFormat("if (true)\n"
16726 " f( );\n"
16727 "else if (true)\n"
16728 " f( );",
16729 Spaces);
16730 verifyFormat("do {\n"
16731 " do_something(( int )i);\n"
16732 "} while (something( ));",
16733 Spaces);
16734 verifyFormat("switch (x) {\n"
16735 "default:\n"
16736 " break;\n"
16737 "}",
16738 Spaces);
16739 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
16740 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
16741 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
16743 // Run the first set of tests again with:
16744 Spaces.SpaceAfterCStyleCast = true;
16745 verifyFormat("call(x, y, z);", Spaces);
16746 verifyFormat("call( );", Spaces);
16747 verifyFormat("std::function<void(int, int)> callback;", Spaces);
16748 verifyFormat("while (( bool ) 1)\n"
16749 " continue;",
16750 Spaces);
16751 verifyFormat("for (;;)\n"
16752 " continue;",
16753 Spaces);
16754 verifyFormat("if (true)\n"
16755 " f( );\n"
16756 "else if (true)\n"
16757 " f( );",
16758 Spaces);
16759 verifyFormat("do {\n"
16760 " do_something(( int ) i);\n"
16761 "} while (something( ));",
16762 Spaces);
16763 verifyFormat("switch (x) {\n"
16764 "default:\n"
16765 " break;\n"
16766 "}",
16767 Spaces);
16768 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
16769 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
16770 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
16771 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
16772 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
16773 verifyFormat("throw ( int32 ) x;", Spaces);
16774 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
16775 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
16776 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
16778 // Run subset of tests again with:
16779 Spaces.SpacesInParensOptions.InCStyleCasts = false;
16780 Spaces.SpaceAfterCStyleCast = true;
16781 verifyFormat("while ((bool) 1)\n"
16782 " continue;",
16783 Spaces);
16784 verifyFormat("do {\n"
16785 " do_something((int) i);\n"
16786 "} while (something( ));",
16787 Spaces);
16789 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
16790 verifyFormat("size_t idx = (size_t) a;", Spaces);
16791 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
16792 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
16793 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
16794 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
16795 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
16796 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
16797 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
16798 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
16799 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
16800 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
16801 verifyFormat("throw (int32) x;", Spaces);
16802 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
16803 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
16804 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
16806 Spaces.ColumnLimit = 80;
16807 Spaces.IndentWidth = 4;
16808 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
16809 verifyFormat("void foo( ) {\n"
16810 " size_t foo = (*(function))(\n"
16811 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16812 "BarrrrrrrrrrrrLong,\n"
16813 " FoooooooooLooooong);\n"
16814 "}",
16815 Spaces);
16816 Spaces.SpaceAfterCStyleCast = false;
16817 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
16818 verifyFormat("size_t idx = (size_t)a;", Spaces);
16819 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
16820 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
16821 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
16822 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
16823 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
16825 verifyFormat("void foo( ) {\n"
16826 " size_t foo = (*(function))(\n"
16827 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16828 "BarrrrrrrrrrrrLong,\n"
16829 " FoooooooooLooooong);\n"
16830 "}",
16831 Spaces);
16833 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
16834 verifyFormat("void foo( ) {\n"
16835 " size_t foo = (*(function))(\n"
16836 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16837 "BarrrrrrrrrrrrLong,\n"
16838 " FoooooooooLooooong\n"
16839 " );\n"
16840 "}",
16841 Spaces);
16842 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
16843 verifyFormat("size_t idx = (size_t)a;", Spaces);
16844 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
16845 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
16846 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
16847 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
16848 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
16851 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
16852 verifyFormat("int a[5];");
16853 verifyFormat("a[3] += 42;");
16855 FormatStyle Spaces = getLLVMStyle();
16856 Spaces.SpacesInSquareBrackets = true;
16857 // Not lambdas.
16858 verifyFormat("int a[ 5 ];", Spaces);
16859 verifyFormat("a[ 3 ] += 42;", Spaces);
16860 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
16861 verifyFormat("double &operator[](int i) { return 0; }\n"
16862 "int i;",
16863 Spaces);
16864 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
16865 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
16866 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
16867 // Lambdas.
16868 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
16869 verifyFormat("return [ i, args... ] {};", Spaces);
16870 verifyFormat("int foo = [ &bar ]() {};", Spaces);
16871 verifyFormat("int foo = [ = ]() {};", Spaces);
16872 verifyFormat("int foo = [ & ]() {};", Spaces);
16873 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
16874 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
16877 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
16878 FormatStyle NoSpaceStyle = getLLVMStyle();
16879 verifyFormat("int a[5];", NoSpaceStyle);
16880 verifyFormat("a[3] += 42;", NoSpaceStyle);
16882 verifyFormat("int a[1];", NoSpaceStyle);
16883 verifyFormat("int 1 [a];", NoSpaceStyle);
16884 verifyFormat("int a[1][2];", NoSpaceStyle);
16885 verifyFormat("a[7] = 5;", NoSpaceStyle);
16886 verifyFormat("int a = (f())[23];", NoSpaceStyle);
16887 verifyFormat("f([] {})", NoSpaceStyle);
16889 FormatStyle Space = getLLVMStyle();
16890 Space.SpaceBeforeSquareBrackets = true;
16891 verifyFormat("int c = []() -> int { return 2; }();", Space);
16892 verifyFormat("return [i, args...] {};", Space);
16894 verifyFormat("int a [5];", Space);
16895 verifyFormat("a [3] += 42;", Space);
16896 verifyFormat("constexpr char hello []{\"hello\"};", Space);
16897 verifyFormat("double &operator[](int i) { return 0; }\n"
16898 "int i;",
16899 Space);
16900 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
16901 verifyFormat("int i = a [a][a]->f();", Space);
16902 verifyFormat("int i = (*b) [a]->f();", Space);
16904 verifyFormat("int a [1];", Space);
16905 verifyFormat("int 1 [a];", Space);
16906 verifyFormat("int a [1][2];", Space);
16907 verifyFormat("a [7] = 5;", Space);
16908 verifyFormat("int a = (f()) [23];", Space);
16909 verifyFormat("f([] {})", Space);
16912 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
16913 verifyFormat("int a = 5;");
16914 verifyFormat("a += 42;");
16915 verifyFormat("a or_eq 8;");
16917 FormatStyle Spaces = getLLVMStyle();
16918 Spaces.SpaceBeforeAssignmentOperators = false;
16919 verifyFormat("int a= 5;", Spaces);
16920 verifyFormat("a+= 42;", Spaces);
16921 verifyFormat("a or_eq 8;", Spaces);
16924 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
16925 verifyFormat("class Foo : public Bar {};");
16926 verifyFormat("Foo::Foo() : foo(1) {}");
16927 verifyFormat("for (auto a : b) {\n}");
16928 verifyFormat("int x = a ? b : c;");
16929 verifyFormat("{\n"
16930 "label0:\n"
16931 " int x = 0;\n"
16932 "}");
16933 verifyFormat("switch (x) {\n"
16934 "case 1:\n"
16935 "default:\n"
16936 "}");
16937 verifyFormat("switch (allBraces) {\n"
16938 "case 1: {\n"
16939 " break;\n"
16940 "}\n"
16941 "case 2: {\n"
16942 " [[fallthrough]];\n"
16943 "}\n"
16944 "default: {\n"
16945 " break;\n"
16946 "}\n"
16947 "}");
16949 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30);
16950 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
16951 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
16952 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
16953 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
16954 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
16955 verifyFormat("{\n"
16956 "label1:\n"
16957 " int x = 0;\n"
16958 "}",
16959 CtorInitializerStyle);
16960 verifyFormat("switch (x) {\n"
16961 "case 1:\n"
16962 "default:\n"
16963 "}",
16964 CtorInitializerStyle);
16965 verifyFormat("switch (allBraces) {\n"
16966 "case 1: {\n"
16967 " break;\n"
16968 "}\n"
16969 "case 2: {\n"
16970 " [[fallthrough]];\n"
16971 "}\n"
16972 "default: {\n"
16973 " break;\n"
16974 "}\n"
16975 "}",
16976 CtorInitializerStyle);
16977 CtorInitializerStyle.BreakConstructorInitializers =
16978 FormatStyle::BCIS_AfterColon;
16979 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
16980 " aaaaaaaaaaaaaaaa(1),\n"
16981 " bbbbbbbbbbbbbbbb(2) {}",
16982 CtorInitializerStyle);
16983 CtorInitializerStyle.BreakConstructorInitializers =
16984 FormatStyle::BCIS_BeforeComma;
16985 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16986 " : aaaaaaaaaaaaaaaa(1)\n"
16987 " , bbbbbbbbbbbbbbbb(2) {}",
16988 CtorInitializerStyle);
16989 CtorInitializerStyle.BreakConstructorInitializers =
16990 FormatStyle::BCIS_BeforeColon;
16991 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16992 " : aaaaaaaaaaaaaaaa(1),\n"
16993 " bbbbbbbbbbbbbbbb(2) {}",
16994 CtorInitializerStyle);
16995 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
16996 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16997 ": aaaaaaaaaaaaaaaa(1),\n"
16998 " bbbbbbbbbbbbbbbb(2) {}",
16999 CtorInitializerStyle);
17001 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30);
17002 InheritanceStyle.SpaceBeforeInheritanceColon = false;
17003 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
17004 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
17005 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
17006 verifyFormat("int x = a ? b : c;", InheritanceStyle);
17007 verifyFormat("{\n"
17008 "label2:\n"
17009 " int x = 0;\n"
17010 "}",
17011 InheritanceStyle);
17012 verifyFormat("switch (x) {\n"
17013 "case 1:\n"
17014 "default:\n"
17015 "}",
17016 InheritanceStyle);
17017 verifyFormat("switch (allBraces) {\n"
17018 "case 1: {\n"
17019 " break;\n"
17020 "}\n"
17021 "case 2: {\n"
17022 " [[fallthrough]];\n"
17023 "}\n"
17024 "default: {\n"
17025 " break;\n"
17026 "}\n"
17027 "}",
17028 InheritanceStyle);
17029 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
17030 verifyFormat("class Foooooooooooooooooooooo\n"
17031 " : public aaaaaaaaaaaaaaaaaa,\n"
17032 " public bbbbbbbbbbbbbbbbbb {\n"
17033 "}",
17034 InheritanceStyle);
17035 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
17036 verifyFormat("class Foooooooooooooooooooooo:\n"
17037 " public aaaaaaaaaaaaaaaaaa,\n"
17038 " public bbbbbbbbbbbbbbbbbb {\n"
17039 "}",
17040 InheritanceStyle);
17041 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
17042 verifyFormat("class Foooooooooooooooooooooo\n"
17043 " : public aaaaaaaaaaaaaaaaaa\n"
17044 " , public bbbbbbbbbbbbbbbbbb {\n"
17045 "}",
17046 InheritanceStyle);
17047 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17048 verifyFormat("class Foooooooooooooooooooooo\n"
17049 " : public aaaaaaaaaaaaaaaaaa,\n"
17050 " public bbbbbbbbbbbbbbbbbb {\n"
17051 "}",
17052 InheritanceStyle);
17053 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
17054 verifyFormat("class Foooooooooooooooooooooo\n"
17055 ": public aaaaaaaaaaaaaaaaaa,\n"
17056 " public bbbbbbbbbbbbbbbbbb {}",
17057 InheritanceStyle);
17059 FormatStyle ForLoopStyle = getLLVMStyle();
17060 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
17061 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
17062 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
17063 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
17064 verifyFormat("int x = a ? b : c;", ForLoopStyle);
17065 verifyFormat("{\n"
17066 "label2:\n"
17067 " int x = 0;\n"
17068 "}",
17069 ForLoopStyle);
17070 verifyFormat("switch (x) {\n"
17071 "case 1:\n"
17072 "default:\n"
17073 "}",
17074 ForLoopStyle);
17075 verifyFormat("switch (allBraces) {\n"
17076 "case 1: {\n"
17077 " break;\n"
17078 "}\n"
17079 "case 2: {\n"
17080 " [[fallthrough]];\n"
17081 "}\n"
17082 "default: {\n"
17083 " break;\n"
17084 "}\n"
17085 "}",
17086 ForLoopStyle);
17088 FormatStyle CaseStyle = getLLVMStyle();
17089 CaseStyle.SpaceBeforeCaseColon = true;
17090 verifyFormat("class Foo : public Bar {};", CaseStyle);
17091 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
17092 verifyFormat("for (auto a : b) {\n}", CaseStyle);
17093 verifyFormat("int x = a ? b : c;", CaseStyle);
17094 verifyFormat("switch (x) {\n"
17095 "case 1 :\n"
17096 "default :\n"
17097 "}",
17098 CaseStyle);
17099 verifyFormat("switch (allBraces) {\n"
17100 "case 1 : {\n"
17101 " break;\n"
17102 "}\n"
17103 "case 2 : {\n"
17104 " [[fallthrough]];\n"
17105 "}\n"
17106 "default : {\n"
17107 " break;\n"
17108 "}\n"
17109 "}",
17110 CaseStyle);
17111 // Goto labels should not be affected.
17112 verifyFormat("switch (x) {\n"
17113 "goto_label:\n"
17114 "default :\n"
17115 "}",
17116 CaseStyle);
17117 verifyFormat("switch (x) {\n"
17118 "goto_label: { break; }\n"
17119 "default : {\n"
17120 " break;\n"
17121 "}\n"
17122 "}",
17123 CaseStyle);
17125 FormatStyle NoSpaceStyle = getLLVMStyle();
17126 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
17127 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17128 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
17129 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17130 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
17131 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
17132 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
17133 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
17134 verifyFormat("{\n"
17135 "label3:\n"
17136 " int x = 0;\n"
17137 "}",
17138 NoSpaceStyle);
17139 verifyFormat("switch (x) {\n"
17140 "case 1:\n"
17141 "default:\n"
17142 "}",
17143 NoSpaceStyle);
17144 verifyFormat("switch (allBraces) {\n"
17145 "case 1: {\n"
17146 " break;\n"
17147 "}\n"
17148 "case 2: {\n"
17149 " [[fallthrough]];\n"
17150 "}\n"
17151 "default: {\n"
17152 " break;\n"
17153 "}\n"
17154 "}",
17155 NoSpaceStyle);
17157 FormatStyle InvertedSpaceStyle = getLLVMStyle();
17158 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
17159 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17160 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
17161 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17162 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
17163 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
17164 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
17165 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
17166 verifyFormat("{\n"
17167 "label3:\n"
17168 " int x = 0;\n"
17169 "}",
17170 InvertedSpaceStyle);
17171 verifyFormat("switch (x) {\n"
17172 "case 1 :\n"
17173 "case 2 : {\n"
17174 " break;\n"
17175 "}\n"
17176 "default :\n"
17177 " break;\n"
17178 "}",
17179 InvertedSpaceStyle);
17180 verifyFormat("switch (allBraces) {\n"
17181 "case 1 : {\n"
17182 " break;\n"
17183 "}\n"
17184 "case 2 : {\n"
17185 " [[fallthrough]];\n"
17186 "}\n"
17187 "default : {\n"
17188 " break;\n"
17189 "}\n"
17190 "}",
17191 InvertedSpaceStyle);
17194 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
17195 FormatStyle Style = getLLVMStyle();
17197 Style.PointerAlignment = FormatStyle::PAS_Left;
17198 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17199 verifyFormat("void* const* x = NULL;", Style);
17201 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17202 do { \
17203 Style.PointerAlignment = FormatStyle::Pointers; \
17204 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17205 verifyFormat(Code, Style); \
17206 } while (false)
17208 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
17209 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
17210 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
17212 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
17213 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
17214 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
17216 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
17217 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
17218 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
17220 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
17221 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
17222 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
17224 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
17225 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17226 SAPQ_Default);
17227 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17228 SAPQ_Default);
17230 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
17231 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17232 SAPQ_Before);
17233 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17234 SAPQ_Before);
17236 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
17237 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
17238 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17239 SAPQ_After);
17241 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
17242 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
17243 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
17245 #undef verifyQualifierSpaces
17247 FormatStyle Spaces = getLLVMStyle();
17248 Spaces.AttributeMacros.push_back("qualified");
17249 Spaces.PointerAlignment = FormatStyle::PAS_Right;
17250 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17251 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
17252 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17253 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
17254 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
17255 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17256 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17257 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
17258 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
17259 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17260 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17261 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17263 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17264 Spaces.PointerAlignment = FormatStyle::PAS_Left;
17265 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17266 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
17267 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
17268 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
17269 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
17270 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17271 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17272 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17273 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
17274 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
17275 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
17276 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
17277 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17279 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
17280 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
17281 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17282 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
17283 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
17284 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17285 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17286 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17289 TEST_F(FormatTest, AlignConsecutiveMacros) {
17290 FormatStyle Style = getLLVMStyle();
17291 Style.AlignConsecutiveAssignments.Enabled = true;
17292 Style.AlignConsecutiveDeclarations.Enabled = true;
17294 verifyFormat("#define a 3\n"
17295 "#define bbbb 4\n"
17296 "#define ccc (5)",
17297 Style);
17299 verifyFormat("#define f(x) (x * x)\n"
17300 "#define fff(x, y, z) (x * y + z)\n"
17301 "#define ffff(x, y) (x - y)",
17302 Style);
17304 verifyFormat("#define foo(x, y) (x + y)\n"
17305 "#define bar (5, 6)(2 + 2)",
17306 Style);
17308 verifyFormat("#define a 3\n"
17309 "#define bbbb 4\n"
17310 "#define ccc (5)\n"
17311 "#define f(x) (x * x)\n"
17312 "#define fff(x, y, z) (x * y + z)\n"
17313 "#define ffff(x, y) (x - y)",
17314 Style);
17316 Style.AlignConsecutiveMacros.Enabled = true;
17317 verifyFormat("#define a 3\n"
17318 "#define bbbb 4\n"
17319 "#define ccc (5)",
17320 Style);
17322 verifyFormat("#define true 1\n"
17323 "#define false 0",
17324 Style);
17326 verifyFormat("#define f(x) (x * x)\n"
17327 "#define fff(x, y, z) (x * y + z)\n"
17328 "#define ffff(x, y) (x - y)",
17329 Style);
17331 verifyFormat("#define foo(x, y) (x + y)\n"
17332 "#define bar (5, 6)(2 + 2)",
17333 Style);
17335 verifyFormat("#define a 3\n"
17336 "#define bbbb 4\n"
17337 "#define ccc (5)\n"
17338 "#define f(x) (x * x)\n"
17339 "#define fff(x, y, z) (x * y + z)\n"
17340 "#define ffff(x, y) (x - y)",
17341 Style);
17343 verifyFormat("#define a 5\n"
17344 "#define foo(x, y) (x + y)\n"
17345 "#define CCC (6)\n"
17346 "auto lambda = []() {\n"
17347 " auto ii = 0;\n"
17348 " float j = 0;\n"
17349 " return 0;\n"
17350 "};\n"
17351 "int i = 0;\n"
17352 "float i2 = 0;\n"
17353 "auto v = type{\n"
17354 " i = 1, //\n"
17355 " (i = 2), //\n"
17356 " i = 3 //\n"
17357 "};",
17358 Style);
17360 Style.AlignConsecutiveMacros.Enabled = false;
17361 Style.ColumnLimit = 20;
17363 verifyFormat("#define a \\\n"
17364 " \"aabbbbbbbbbbbb\"\n"
17365 "#define D \\\n"
17366 " \"aabbbbbbbbbbbb\" \\\n"
17367 " \"ccddeeeeeeeee\"\n"
17368 "#define B \\\n"
17369 " \"QQQQQQQQQQQQQ\" \\\n"
17370 " \"FFFFFFFFFFFFF\" \\\n"
17371 " \"LLLLLLLL\"",
17372 Style);
17374 Style.AlignConsecutiveMacros.Enabled = true;
17375 verifyFormat("#define a \\\n"
17376 " \"aabbbbbbbbbbbb\"\n"
17377 "#define D \\\n"
17378 " \"aabbbbbbbbbbbb\" \\\n"
17379 " \"ccddeeeeeeeee\"\n"
17380 "#define B \\\n"
17381 " \"QQQQQQQQQQQQQ\" \\\n"
17382 " \"FFFFFFFFFFFFF\" \\\n"
17383 " \"LLLLLLLL\"",
17384 Style);
17386 // Test across comments
17387 Style.MaxEmptyLinesToKeep = 10;
17388 Style.ReflowComments = false;
17389 Style.AlignConsecutiveMacros.AcrossComments = true;
17390 verifyFormat("#define a 3\n"
17391 "// line comment\n"
17392 "#define bbbb 4\n"
17393 "#define ccc (5)",
17394 "#define a 3\n"
17395 "// line comment\n"
17396 "#define bbbb 4\n"
17397 "#define ccc (5)",
17398 Style);
17400 verifyFormat("#define a 3\n"
17401 "/* block comment */\n"
17402 "#define bbbb 4\n"
17403 "#define ccc (5)",
17404 "#define a 3\n"
17405 "/* block comment */\n"
17406 "#define bbbb 4\n"
17407 "#define ccc (5)",
17408 Style);
17410 verifyFormat("#define a 3\n"
17411 "/* multi-line *\n"
17412 " * block comment */\n"
17413 "#define bbbb 4\n"
17414 "#define ccc (5)",
17415 "#define a 3\n"
17416 "/* multi-line *\n"
17417 " * block comment */\n"
17418 "#define bbbb 4\n"
17419 "#define ccc (5)",
17420 Style);
17422 verifyFormat("#define a 3\n"
17423 "// multi-line line comment\n"
17424 "//\n"
17425 "#define bbbb 4\n"
17426 "#define ccc (5)",
17427 "#define a 3\n"
17428 "// multi-line line comment\n"
17429 "//\n"
17430 "#define bbbb 4\n"
17431 "#define ccc (5)",
17432 Style);
17434 verifyFormat("#define a 3\n"
17435 "// empty lines still break.\n"
17436 "\n"
17437 "#define bbbb 4\n"
17438 "#define ccc (5)",
17439 "#define a 3\n"
17440 "// empty lines still break.\n"
17441 "\n"
17442 "#define bbbb 4\n"
17443 "#define ccc (5)",
17444 Style);
17446 // Test across empty lines
17447 Style.AlignConsecutiveMacros.AcrossComments = false;
17448 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
17449 verifyFormat("#define a 3\n"
17450 "\n"
17451 "#define bbbb 4\n"
17452 "#define ccc (5)",
17453 "#define a 3\n"
17454 "\n"
17455 "#define bbbb 4\n"
17456 "#define ccc (5)",
17457 Style);
17459 verifyFormat("#define a 3\n"
17460 "\n"
17461 "\n"
17462 "\n"
17463 "#define bbbb 4\n"
17464 "#define ccc (5)",
17465 "#define a 3\n"
17466 "\n"
17467 "\n"
17468 "\n"
17469 "#define bbbb 4\n"
17470 "#define ccc (5)",
17471 Style);
17473 verifyFormat("#define a 3\n"
17474 "// comments should break alignment\n"
17475 "//\n"
17476 "#define bbbb 4\n"
17477 "#define ccc (5)",
17478 "#define a 3\n"
17479 "// comments should break alignment\n"
17480 "//\n"
17481 "#define bbbb 4\n"
17482 "#define ccc (5)",
17483 Style);
17485 // Test across empty lines and comments
17486 Style.AlignConsecutiveMacros.AcrossComments = true;
17487 verifyFormat("#define a 3\n"
17488 "\n"
17489 "// line comment\n"
17490 "#define bbbb 4\n"
17491 "#define ccc (5)",
17492 Style);
17494 verifyFormat("#define a 3\n"
17495 "\n"
17496 "\n"
17497 "/* multi-line *\n"
17498 " * block comment */\n"
17499 "\n"
17500 "\n"
17501 "#define bbbb 4\n"
17502 "#define ccc (5)",
17503 "#define a 3\n"
17504 "\n"
17505 "\n"
17506 "/* multi-line *\n"
17507 " * block comment */\n"
17508 "\n"
17509 "\n"
17510 "#define bbbb 4\n"
17511 "#define ccc (5)",
17512 Style);
17514 verifyFormat("#define a 3\n"
17515 "\n"
17516 "\n"
17517 "/* multi-line *\n"
17518 " * block comment */\n"
17519 "\n"
17520 "\n"
17521 "#define bbbb 4\n"
17522 "#define ccc (5)",
17523 "#define a 3\n"
17524 "\n"
17525 "\n"
17526 "/* multi-line *\n"
17527 " * block comment */\n"
17528 "\n"
17529 "\n"
17530 "#define bbbb 4\n"
17531 "#define ccc (5)",
17532 Style);
17535 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
17536 FormatStyle Alignment = getLLVMStyle();
17537 Alignment.AlignConsecutiveMacros.Enabled = true;
17538 Alignment.AlignConsecutiveAssignments.Enabled = true;
17539 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17541 Alignment.MaxEmptyLinesToKeep = 10;
17542 /* Test alignment across empty lines */
17543 verifyFormat("int a = 5;\n"
17544 "\n"
17545 "int oneTwoThree = 123;",
17546 "int a = 5;\n"
17547 "\n"
17548 "int oneTwoThree= 123;",
17549 Alignment);
17550 verifyFormat("int a = 5;\n"
17551 "int one = 1;\n"
17552 "\n"
17553 "int oneTwoThree = 123;",
17554 "int a = 5;\n"
17555 "int one = 1;\n"
17556 "\n"
17557 "int oneTwoThree = 123;",
17558 Alignment);
17559 verifyFormat("int a = 5;\n"
17560 "int one = 1;\n"
17561 "\n"
17562 "int oneTwoThree = 123;\n"
17563 "int oneTwo = 12;",
17564 "int a = 5;\n"
17565 "int one = 1;\n"
17566 "\n"
17567 "int oneTwoThree = 123;\n"
17568 "int oneTwo = 12;",
17569 Alignment);
17571 /* Test across comments */
17572 verifyFormat("int a = 5;\n"
17573 "/* block comment */\n"
17574 "int oneTwoThree = 123;",
17575 "int a = 5;\n"
17576 "/* block comment */\n"
17577 "int oneTwoThree=123;",
17578 Alignment);
17580 verifyFormat("int a = 5;\n"
17581 "// line comment\n"
17582 "int oneTwoThree = 123;",
17583 "int a = 5;\n"
17584 "// line comment\n"
17585 "int oneTwoThree=123;",
17586 Alignment);
17588 /* Test across comments and newlines */
17589 verifyFormat("int a = 5;\n"
17590 "\n"
17591 "/* block comment */\n"
17592 "int oneTwoThree = 123;",
17593 "int a = 5;\n"
17594 "\n"
17595 "/* block comment */\n"
17596 "int oneTwoThree=123;",
17597 Alignment);
17599 verifyFormat("int a = 5;\n"
17600 "\n"
17601 "// line comment\n"
17602 "int oneTwoThree = 123;",
17603 "int a = 5;\n"
17604 "\n"
17605 "// line comment\n"
17606 "int oneTwoThree=123;",
17607 Alignment);
17610 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
17611 FormatStyle Alignment = getLLVMStyle();
17612 Alignment.AlignConsecutiveDeclarations.Enabled = true;
17613 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
17614 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
17616 Alignment.MaxEmptyLinesToKeep = 10;
17617 /* Test alignment across empty lines */
17618 verifyFormat("int a = 5;\n"
17619 "\n"
17620 "float const oneTwoThree = 123;",
17621 "int a = 5;\n"
17622 "\n"
17623 "float const oneTwoThree = 123;",
17624 Alignment);
17625 verifyFormat("int a = 5;\n"
17626 "float const one = 1;\n"
17627 "\n"
17628 "int oneTwoThree = 123;",
17629 "int a = 5;\n"
17630 "float const one = 1;\n"
17631 "\n"
17632 "int oneTwoThree = 123;",
17633 Alignment);
17635 /* Test across comments */
17636 verifyFormat("float const a = 5;\n"
17637 "/* block comment */\n"
17638 "int oneTwoThree = 123;",
17639 "float const a = 5;\n"
17640 "/* block comment */\n"
17641 "int oneTwoThree=123;",
17642 Alignment);
17644 verifyFormat("float const a = 5;\n"
17645 "// line comment\n"
17646 "int oneTwoThree = 123;",
17647 "float const a = 5;\n"
17648 "// line comment\n"
17649 "int oneTwoThree=123;",
17650 Alignment);
17652 /* Test across comments and newlines */
17653 verifyFormat("float const a = 5;\n"
17654 "\n"
17655 "/* block comment */\n"
17656 "int oneTwoThree = 123;",
17657 "float const a = 5;\n"
17658 "\n"
17659 "/* block comment */\n"
17660 "int oneTwoThree=123;",
17661 Alignment);
17663 verifyFormat("float const a = 5;\n"
17664 "\n"
17665 "// line comment\n"
17666 "int oneTwoThree = 123;",
17667 "float const a = 5;\n"
17668 "\n"
17669 "// line comment\n"
17670 "int oneTwoThree=123;",
17671 Alignment);
17674 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
17675 FormatStyle Alignment = getLLVMStyle();
17676 Alignment.AlignConsecutiveBitFields.Enabled = true;
17677 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
17678 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
17680 Alignment.MaxEmptyLinesToKeep = 10;
17681 /* Test alignment across empty lines */
17682 verifyFormat("int a : 5;\n"
17683 "\n"
17684 "int longbitfield : 6;",
17685 "int a : 5;\n"
17686 "\n"
17687 "int longbitfield : 6;",
17688 Alignment);
17689 verifyFormat("int a : 5;\n"
17690 "int one : 1;\n"
17691 "\n"
17692 "int longbitfield : 6;",
17693 "int a : 5;\n"
17694 "int one : 1;\n"
17695 "\n"
17696 "int longbitfield : 6;",
17697 Alignment);
17699 /* Test across comments */
17700 verifyFormat("int a : 5;\n"
17701 "/* block comment */\n"
17702 "int longbitfield : 6;",
17703 "int a : 5;\n"
17704 "/* block comment */\n"
17705 "int longbitfield : 6;",
17706 Alignment);
17707 verifyFormat("int a : 5;\n"
17708 "int one : 1;\n"
17709 "// line comment\n"
17710 "int longbitfield : 6;",
17711 "int a : 5;\n"
17712 "int one : 1;\n"
17713 "// line comment\n"
17714 "int longbitfield : 6;",
17715 Alignment);
17717 /* Test across comments and newlines */
17718 verifyFormat("int a : 5;\n"
17719 "/* block comment */\n"
17720 "\n"
17721 "int longbitfield : 6;",
17722 "int a : 5;\n"
17723 "/* block comment */\n"
17724 "\n"
17725 "int longbitfield : 6;",
17726 Alignment);
17727 verifyFormat("int a : 5;\n"
17728 "int one : 1;\n"
17729 "\n"
17730 "// line comment\n"
17731 "\n"
17732 "int longbitfield : 6;",
17733 "int a : 5;\n"
17734 "int one : 1;\n"
17735 "\n"
17736 "// line comment \n"
17737 "\n"
17738 "int longbitfield : 6;",
17739 Alignment);
17742 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
17743 FormatStyle Alignment = getLLVMStyle();
17744 Alignment.AlignConsecutiveMacros.Enabled = true;
17745 Alignment.AlignConsecutiveAssignments.Enabled = true;
17746 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
17748 Alignment.MaxEmptyLinesToKeep = 10;
17749 /* Test alignment across empty lines */
17750 verifyFormat("int a = 5;\n"
17751 "\n"
17752 "int oneTwoThree = 123;",
17753 "int a = 5;\n"
17754 "\n"
17755 "int oneTwoThree= 123;",
17756 Alignment);
17757 verifyFormat("int a = 5;\n"
17758 "int one = 1;\n"
17759 "\n"
17760 "int oneTwoThree = 123;",
17761 "int a = 5;\n"
17762 "int one = 1;\n"
17763 "\n"
17764 "int oneTwoThree = 123;",
17765 Alignment);
17767 /* Test across comments */
17768 verifyFormat("int a = 5;\n"
17769 "/* block comment */\n"
17770 "int oneTwoThree = 123;",
17771 "int a = 5;\n"
17772 "/* block comment */\n"
17773 "int oneTwoThree=123;",
17774 Alignment);
17776 verifyFormat("int a = 5;\n"
17777 "// line comment\n"
17778 "int oneTwoThree = 123;",
17779 "int a = 5;\n"
17780 "// line comment\n"
17781 "int oneTwoThree=123;",
17782 Alignment);
17784 verifyFormat("int a = 5;\n"
17785 "/*\n"
17786 " * multi-line block comment\n"
17787 " */\n"
17788 "int oneTwoThree = 123;",
17789 "int a = 5;\n"
17790 "/*\n"
17791 " * multi-line block comment\n"
17792 " */\n"
17793 "int oneTwoThree=123;",
17794 Alignment);
17796 verifyFormat("int a = 5;\n"
17797 "//\n"
17798 "// multi-line line comment\n"
17799 "//\n"
17800 "int oneTwoThree = 123;",
17801 "int a = 5;\n"
17802 "//\n"
17803 "// multi-line line comment\n"
17804 "//\n"
17805 "int oneTwoThree=123;",
17806 Alignment);
17808 /* Test across comments and newlines */
17809 verifyFormat("int a = 5;\n"
17810 "\n"
17811 "/* block comment */\n"
17812 "int oneTwoThree = 123;",
17813 "int a = 5;\n"
17814 "\n"
17815 "/* block comment */\n"
17816 "int oneTwoThree=123;",
17817 Alignment);
17819 verifyFormat("int a = 5;\n"
17820 "\n"
17821 "// line comment\n"
17822 "int oneTwoThree = 123;",
17823 "int a = 5;\n"
17824 "\n"
17825 "// line comment\n"
17826 "int oneTwoThree=123;",
17827 Alignment);
17830 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
17831 FormatStyle Alignment = getLLVMStyle();
17832 Alignment.AlignConsecutiveMacros.Enabled = true;
17833 Alignment.AlignConsecutiveAssignments.Enabled = true;
17834 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17835 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
17836 verifyFormat("int a = 5;\n"
17837 "int oneTwoThree = 123;",
17838 Alignment);
17839 verifyFormat("int a = method();\n"
17840 "int oneTwoThree = 133;",
17841 Alignment);
17842 verifyFormat("a &= 5;\n"
17843 "bcd *= 5;\n"
17844 "ghtyf += 5;\n"
17845 "dvfvdb -= 5;\n"
17846 "a /= 5;\n"
17847 "vdsvsv %= 5;\n"
17848 "sfdbddfbdfbb ^= 5;\n"
17849 "dvsdsv |= 5;\n"
17850 "int dsvvdvsdvvv = 123;",
17851 Alignment);
17852 verifyFormat("int i = 1, j = 10;\n"
17853 "something = 2000;",
17854 Alignment);
17855 verifyFormat("something = 2000;\n"
17856 "int i = 1, j = 10;",
17857 Alignment);
17858 verifyFormat("something = 2000;\n"
17859 "another = 911;\n"
17860 "int i = 1, j = 10;\n"
17861 "oneMore = 1;\n"
17862 "i = 2;",
17863 Alignment);
17864 verifyFormat("int a = 5;\n"
17865 "int one = 1;\n"
17866 "method();\n"
17867 "int oneTwoThree = 123;\n"
17868 "int oneTwo = 12;",
17869 Alignment);
17870 verifyFormat("int oneTwoThree = 123;\n"
17871 "int oneTwo = 12;\n"
17872 "method();",
17873 Alignment);
17874 verifyFormat("int oneTwoThree = 123; // comment\n"
17875 "int oneTwo = 12; // comment",
17876 Alignment);
17878 // Bug 25167
17879 /* Uncomment when fixed
17880 verifyFormat("#if A\n"
17881 "#else\n"
17882 "int aaaaaaaa = 12;\n"
17883 "#endif\n"
17884 "#if B\n"
17885 "#else\n"
17886 "int a = 12;\n"
17887 "#endif",
17888 Alignment);
17889 verifyFormat("enum foo {\n"
17890 "#if A\n"
17891 "#else\n"
17892 " aaaaaaaa = 12;\n"
17893 "#endif\n"
17894 "#if B\n"
17895 "#else\n"
17896 " a = 12;\n"
17897 "#endif\n"
17898 "};",
17899 Alignment);
17902 Alignment.MaxEmptyLinesToKeep = 10;
17903 /* Test alignment across empty lines */
17904 verifyFormat("int a = 5;\n"
17905 "\n"
17906 "int oneTwoThree = 123;",
17907 "int a = 5;\n"
17908 "\n"
17909 "int oneTwoThree= 123;",
17910 Alignment);
17911 verifyFormat("int a = 5;\n"
17912 "int one = 1;\n"
17913 "\n"
17914 "int oneTwoThree = 123;",
17915 "int a = 5;\n"
17916 "int one = 1;\n"
17917 "\n"
17918 "int oneTwoThree = 123;",
17919 Alignment);
17920 verifyFormat("int a = 5;\n"
17921 "int one = 1;\n"
17922 "\n"
17923 "int oneTwoThree = 123;\n"
17924 "int oneTwo = 12;",
17925 "int a = 5;\n"
17926 "int one = 1;\n"
17927 "\n"
17928 "int oneTwoThree = 123;\n"
17929 "int oneTwo = 12;",
17930 Alignment);
17932 /* Test across comments */
17933 verifyFormat("int a = 5;\n"
17934 "/* block comment */\n"
17935 "int oneTwoThree = 123;",
17936 "int a = 5;\n"
17937 "/* block comment */\n"
17938 "int oneTwoThree=123;",
17939 Alignment);
17941 verifyFormat("int a = 5;\n"
17942 "// line comment\n"
17943 "int oneTwoThree = 123;",
17944 "int a = 5;\n"
17945 "// line comment\n"
17946 "int oneTwoThree=123;",
17947 Alignment);
17949 /* Test across comments and newlines */
17950 verifyFormat("int a = 5;\n"
17951 "\n"
17952 "/* block comment */\n"
17953 "int oneTwoThree = 123;",
17954 "int a = 5;\n"
17955 "\n"
17956 "/* block comment */\n"
17957 "int oneTwoThree=123;",
17958 Alignment);
17960 verifyFormat("int a = 5;\n"
17961 "\n"
17962 "// line comment\n"
17963 "int oneTwoThree = 123;",
17964 "int a = 5;\n"
17965 "\n"
17966 "// line comment\n"
17967 "int oneTwoThree=123;",
17968 Alignment);
17970 verifyFormat("int a = 5;\n"
17971 "//\n"
17972 "// multi-line line comment\n"
17973 "//\n"
17974 "int oneTwoThree = 123;",
17975 "int a = 5;\n"
17976 "//\n"
17977 "// multi-line line comment\n"
17978 "//\n"
17979 "int oneTwoThree=123;",
17980 Alignment);
17982 verifyFormat("int a = 5;\n"
17983 "/*\n"
17984 " * multi-line block comment\n"
17985 " */\n"
17986 "int oneTwoThree = 123;",
17987 "int a = 5;\n"
17988 "/*\n"
17989 " * multi-line block comment\n"
17990 " */\n"
17991 "int oneTwoThree=123;",
17992 Alignment);
17994 verifyFormat("int a = 5;\n"
17995 "\n"
17996 "/* block comment */\n"
17997 "\n"
17998 "\n"
17999 "\n"
18000 "int oneTwoThree = 123;",
18001 "int a = 5;\n"
18002 "\n"
18003 "/* block comment */\n"
18004 "\n"
18005 "\n"
18006 "\n"
18007 "int oneTwoThree=123;",
18008 Alignment);
18010 verifyFormat("int a = 5;\n"
18011 "\n"
18012 "// line comment\n"
18013 "\n"
18014 "\n"
18015 "\n"
18016 "int oneTwoThree = 123;",
18017 "int a = 5;\n"
18018 "\n"
18019 "// line comment\n"
18020 "\n"
18021 "\n"
18022 "\n"
18023 "int oneTwoThree=123;",
18024 Alignment);
18026 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18027 verifyFormat("#define A \\\n"
18028 " int aaaa = 12; \\\n"
18029 " int b = 23; \\\n"
18030 " int ccc = 234; \\\n"
18031 " int dddddddddd = 2345;",
18032 Alignment);
18033 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18034 verifyFormat("#define A \\\n"
18035 " int aaaa = 12; \\\n"
18036 " int b = 23; \\\n"
18037 " int ccc = 234; \\\n"
18038 " int dddddddddd = 2345;",
18039 Alignment);
18040 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18041 verifyFormat("#define A "
18042 " \\\n"
18043 " int aaaa = 12; "
18044 " \\\n"
18045 " int b = 23; "
18046 " \\\n"
18047 " int ccc = 234; "
18048 " \\\n"
18049 " int dddddddddd = 2345;",
18050 Alignment);
18051 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18052 "k = 4, int l = 5,\n"
18053 " int m = 6) {\n"
18054 " int j = 10;\n"
18055 " otherThing = 1;\n"
18056 "}",
18057 Alignment);
18058 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18059 " int i = 1;\n"
18060 " int j = 2;\n"
18061 " int big = 10000;\n"
18062 "}",
18063 Alignment);
18064 verifyFormat("class C {\n"
18065 "public:\n"
18066 " int i = 1;\n"
18067 " virtual void f() = 0;\n"
18068 "};",
18069 Alignment);
18070 verifyFormat("int i = 1;\n"
18071 "if (SomeType t = getSomething()) {\n"
18072 "}\n"
18073 "int j = 2;\n"
18074 "int big = 10000;",
18075 Alignment);
18076 verifyFormat("int j = 7;\n"
18077 "for (int k = 0; k < N; ++k) {\n"
18078 "}\n"
18079 "int j = 2;\n"
18080 "int big = 10000;\n"
18081 "}",
18082 Alignment);
18083 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18084 verifyFormat("int i = 1;\n"
18085 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18086 " = someLooooooooooooooooongFunction();\n"
18087 "int j = 2;",
18088 Alignment);
18089 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18090 verifyFormat("int i = 1;\n"
18091 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18092 " someLooooooooooooooooongFunction();\n"
18093 "int j = 2;",
18094 Alignment);
18096 verifyFormat("auto lambda = []() {\n"
18097 " auto i = 0;\n"
18098 " return 0;\n"
18099 "};\n"
18100 "int i = 0;\n"
18101 "auto v = type{\n"
18102 " i = 1, //\n"
18103 " (i = 2), //\n"
18104 " i = 3 //\n"
18105 "};",
18106 Alignment);
18108 verifyFormat(
18109 "int i = 1;\n"
18110 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18111 " loooooooooooooooooooooongParameterB);\n"
18112 "int j = 2;",
18113 Alignment);
18115 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18116 " typename B = very_long_type_name_1,\n"
18117 " typename T_2 = very_long_type_name_2>\n"
18118 "auto foo() {}",
18119 Alignment);
18120 verifyFormat("int a, b = 1;\n"
18121 "int c = 2;\n"
18122 "int dd = 3;",
18123 Alignment);
18124 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18125 "float b[1][] = {{3.f}};",
18126 Alignment);
18127 verifyFormat("for (int i = 0; i < 1; i++)\n"
18128 " int x = 1;",
18129 Alignment);
18130 verifyFormat("for (i = 0; i < 1; i++)\n"
18131 " x = 1;\n"
18132 "y = 1;",
18133 Alignment);
18135 Alignment.ReflowComments = true;
18136 Alignment.ColumnLimit = 50;
18137 verifyFormat("int x = 0;\n"
18138 "int yy = 1; /// specificlennospace\n"
18139 "int zzz = 2;",
18140 "int x = 0;\n"
18141 "int yy = 1; ///specificlennospace\n"
18142 "int zzz = 2;",
18143 Alignment);
18146 TEST_F(FormatTest, AlignCompoundAssignments) {
18147 FormatStyle Alignment = getLLVMStyle();
18148 Alignment.AlignConsecutiveAssignments.Enabled = true;
18149 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
18150 Alignment.AlignConsecutiveAssignments.PadOperators = false;
18151 verifyFormat("sfdbddfbdfbb = 5;\n"
18152 "dvsdsv = 5;\n"
18153 "int dsvvdvsdvvv = 123;",
18154 Alignment);
18155 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18156 "dvsdsv |= 5;\n"
18157 "int dsvvdvsdvvv = 123;",
18158 Alignment);
18159 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18160 "dvsdsv <<= 5;\n"
18161 "int dsvvdvsdvvv = 123;",
18162 Alignment);
18163 verifyFormat("int xxx = 5;\n"
18164 "xxx = 5;\n"
18165 "{\n"
18166 " int yyy = 6;\n"
18167 " yyy = 6;\n"
18168 "}",
18169 Alignment);
18170 verifyFormat("int xxx = 5;\n"
18171 "xxx += 5;\n"
18172 "{\n"
18173 " int yyy = 6;\n"
18174 " yyy += 6;\n"
18175 "}",
18176 Alignment);
18177 // Test that `<=` is not treated as a compound assignment.
18178 verifyFormat("aa &= 5;\n"
18179 "b <= 10;\n"
18180 "c = 15;",
18181 Alignment);
18182 Alignment.AlignConsecutiveAssignments.PadOperators = true;
18183 verifyFormat("sfdbddfbdfbb = 5;\n"
18184 "dvsdsv = 5;\n"
18185 "int dsvvdvsdvvv = 123;",
18186 Alignment);
18187 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18188 "dvsdsv |= 5;\n"
18189 "int dsvvdvsdvvv = 123;",
18190 Alignment);
18191 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18192 "dvsdsv <<= 5;\n"
18193 "int dsvvdvsdvvv = 123;",
18194 Alignment);
18195 verifyFormat("a += 5;\n"
18196 "one = 1;\n"
18197 "\n"
18198 "oneTwoThree = 123;",
18199 "a += 5;\n"
18200 "one = 1;\n"
18201 "\n"
18202 "oneTwoThree = 123;",
18203 Alignment);
18204 verifyFormat("a += 5;\n"
18205 "one = 1;\n"
18206 "//\n"
18207 "oneTwoThree = 123;",
18208 "a += 5;\n"
18209 "one = 1;\n"
18210 "//\n"
18211 "oneTwoThree = 123;",
18212 Alignment);
18213 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18214 verifyFormat("a += 5;\n"
18215 "one = 1;\n"
18216 "\n"
18217 "oneTwoThree = 123;",
18218 "a += 5;\n"
18219 "one = 1;\n"
18220 "\n"
18221 "oneTwoThree = 123;",
18222 Alignment);
18223 verifyFormat("a += 5;\n"
18224 "one = 1;\n"
18225 "//\n"
18226 "oneTwoThree = 123;",
18227 "a += 5;\n"
18228 "one = 1;\n"
18229 "//\n"
18230 "oneTwoThree = 123;",
18231 Alignment);
18232 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
18233 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18234 verifyFormat("a += 5;\n"
18235 "one = 1;\n"
18236 "\n"
18237 "oneTwoThree = 123;",
18238 "a += 5;\n"
18239 "one = 1;\n"
18240 "\n"
18241 "oneTwoThree = 123;",
18242 Alignment);
18243 verifyFormat("a += 5;\n"
18244 "one = 1;\n"
18245 "//\n"
18246 "oneTwoThree = 123;",
18247 "a += 5;\n"
18248 "one = 1;\n"
18249 "//\n"
18250 "oneTwoThree = 123;",
18251 Alignment);
18252 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18253 verifyFormat("a += 5;\n"
18254 "one >>= 1;\n"
18255 "\n"
18256 "oneTwoThree = 123;",
18257 "a += 5;\n"
18258 "one >>= 1;\n"
18259 "\n"
18260 "oneTwoThree = 123;",
18261 Alignment);
18262 verifyFormat("a += 5;\n"
18263 "one = 1;\n"
18264 "//\n"
18265 "oneTwoThree <<= 123;",
18266 "a += 5;\n"
18267 "one = 1;\n"
18268 "//\n"
18269 "oneTwoThree <<= 123;",
18270 Alignment);
18273 TEST_F(FormatTest, AlignConsecutiveAssignments) {
18274 FormatStyle Alignment = getLLVMStyle();
18275 Alignment.AlignConsecutiveMacros.Enabled = true;
18276 verifyFormat("int a = 5;\n"
18277 "int oneTwoThree = 123;",
18278 Alignment);
18279 verifyFormat("int a = 5;\n"
18280 "int oneTwoThree = 123;",
18281 Alignment);
18283 Alignment.AlignConsecutiveAssignments.Enabled = true;
18284 verifyFormat("int a = 5;\n"
18285 "int oneTwoThree = 123;",
18286 Alignment);
18287 verifyFormat("int a = method();\n"
18288 "int oneTwoThree = 133;",
18289 Alignment);
18290 verifyFormat("aa <= 5;\n"
18291 "a &= 5;\n"
18292 "bcd *= 5;\n"
18293 "ghtyf += 5;\n"
18294 "dvfvdb -= 5;\n"
18295 "a /= 5;\n"
18296 "vdsvsv %= 5;\n"
18297 "sfdbddfbdfbb ^= 5;\n"
18298 "dvsdsv |= 5;\n"
18299 "int dsvvdvsdvvv = 123;",
18300 Alignment);
18301 verifyFormat("int i = 1, j = 10;\n"
18302 "something = 2000;",
18303 Alignment);
18304 verifyFormat("something = 2000;\n"
18305 "int i = 1, j = 10;",
18306 Alignment);
18307 verifyFormat("something = 2000;\n"
18308 "another = 911;\n"
18309 "int i = 1, j = 10;\n"
18310 "oneMore = 1;\n"
18311 "i = 2;",
18312 Alignment);
18313 verifyFormat("int a = 5;\n"
18314 "int one = 1;\n"
18315 "method();\n"
18316 "int oneTwoThree = 123;\n"
18317 "int oneTwo = 12;",
18318 Alignment);
18319 verifyFormat("int oneTwoThree = 123;\n"
18320 "int oneTwo = 12;\n"
18321 "method();",
18322 Alignment);
18323 verifyFormat("int oneTwoThree = 123; // comment\n"
18324 "int oneTwo = 12; // comment",
18325 Alignment);
18326 verifyFormat("int f() = default;\n"
18327 "int &operator() = default;\n"
18328 "int &operator=() {",
18329 Alignment);
18330 verifyFormat("int f() = delete;\n"
18331 "int &operator() = delete;\n"
18332 "int &operator=() {",
18333 Alignment);
18334 verifyFormat("int f() = default; // comment\n"
18335 "int &operator() = default; // comment\n"
18336 "int &operator=() {",
18337 Alignment);
18338 verifyFormat("int f() = default;\n"
18339 "int &operator() = default;\n"
18340 "int &operator==() {",
18341 Alignment);
18342 verifyFormat("int f() = default;\n"
18343 "int &operator() = default;\n"
18344 "int &operator<=() {",
18345 Alignment);
18346 verifyFormat("int f() = default;\n"
18347 "int &operator() = default;\n"
18348 "int &operator!=() {",
18349 Alignment);
18350 verifyFormat("int f() = default;\n"
18351 "int &operator() = default;\n"
18352 "int &operator=();",
18353 Alignment);
18354 verifyFormat("int f() = delete;\n"
18355 "int &operator() = delete;\n"
18356 "int &operator=();",
18357 Alignment);
18358 verifyFormat("/* long long padding */ int f() = default;\n"
18359 "int &operator() = default;\n"
18360 "int &operator/**/ =();",
18361 Alignment);
18362 // https://llvm.org/PR33697
18363 FormatStyle AlignmentWithPenalty = getLLVMStyle();
18364 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
18365 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
18366 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
18367 " void f() = delete;\n"
18368 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
18369 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
18370 "};",
18371 AlignmentWithPenalty);
18373 // Bug 25167
18374 /* Uncomment when fixed
18375 verifyFormat("#if A\n"
18376 "#else\n"
18377 "int aaaaaaaa = 12;\n"
18378 "#endif\n"
18379 "#if B\n"
18380 "#else\n"
18381 "int a = 12;\n"
18382 "#endif",
18383 Alignment);
18384 verifyFormat("enum foo {\n"
18385 "#if A\n"
18386 "#else\n"
18387 " aaaaaaaa = 12;\n"
18388 "#endif\n"
18389 "#if B\n"
18390 "#else\n"
18391 " a = 12;\n"
18392 "#endif\n"
18393 "};",
18394 Alignment);
18397 verifyFormat("int a = 5;\n"
18398 "\n"
18399 "int oneTwoThree = 123;",
18400 "int a = 5;\n"
18401 "\n"
18402 "int oneTwoThree= 123;",
18403 Alignment);
18404 verifyFormat("int a = 5;\n"
18405 "int one = 1;\n"
18406 "\n"
18407 "int oneTwoThree = 123;",
18408 "int a = 5;\n"
18409 "int one = 1;\n"
18410 "\n"
18411 "int oneTwoThree = 123;",
18412 Alignment);
18413 verifyFormat("int a = 5;\n"
18414 "int one = 1;\n"
18415 "\n"
18416 "int oneTwoThree = 123;\n"
18417 "int oneTwo = 12;",
18418 "int a = 5;\n"
18419 "int one = 1;\n"
18420 "\n"
18421 "int oneTwoThree = 123;\n"
18422 "int oneTwo = 12;",
18423 Alignment);
18424 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18425 verifyFormat("#define A \\\n"
18426 " int aaaa = 12; \\\n"
18427 " int b = 23; \\\n"
18428 " int ccc = 234; \\\n"
18429 " int dddddddddd = 2345;",
18430 Alignment);
18431 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18432 verifyFormat("#define A \\\n"
18433 " int aaaa = 12; \\\n"
18434 " int b = 23; \\\n"
18435 " int ccc = 234; \\\n"
18436 " int dddddddddd = 2345;",
18437 Alignment);
18438 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18439 verifyFormat("#define A "
18440 " \\\n"
18441 " int aaaa = 12; "
18442 " \\\n"
18443 " int b = 23; "
18444 " \\\n"
18445 " int ccc = 234; "
18446 " \\\n"
18447 " int dddddddddd = 2345;",
18448 Alignment);
18449 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18450 "k = 4, int l = 5,\n"
18451 " int m = 6) {\n"
18452 " int j = 10;\n"
18453 " otherThing = 1;\n"
18454 "}",
18455 Alignment);
18456 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18457 " int i = 1;\n"
18458 " int j = 2;\n"
18459 " int big = 10000;\n"
18460 "}",
18461 Alignment);
18462 verifyFormat("class C {\n"
18463 "public:\n"
18464 " int i = 1;\n"
18465 " virtual void f() = 0;\n"
18466 "};",
18467 Alignment);
18468 verifyFormat("int i = 1;\n"
18469 "if (SomeType t = getSomething()) {\n"
18470 "}\n"
18471 "int j = 2;\n"
18472 "int big = 10000;",
18473 Alignment);
18474 verifyFormat("int j = 7;\n"
18475 "for (int k = 0; k < N; ++k) {\n"
18476 "}\n"
18477 "int j = 2;\n"
18478 "int big = 10000;\n"
18479 "}",
18480 Alignment);
18481 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18482 verifyFormat("int i = 1;\n"
18483 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18484 " = someLooooooooooooooooongFunction();\n"
18485 "int j = 2;",
18486 Alignment);
18487 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18488 verifyFormat("int i = 1;\n"
18489 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18490 " someLooooooooooooooooongFunction();\n"
18491 "int j = 2;",
18492 Alignment);
18494 verifyFormat("auto lambda = []() {\n"
18495 " auto i = 0;\n"
18496 " return 0;\n"
18497 "};\n"
18498 "int i = 0;\n"
18499 "auto v = type{\n"
18500 " i = 1, //\n"
18501 " (i = 2), //\n"
18502 " i = 3 //\n"
18503 "};",
18504 Alignment);
18506 verifyFormat(
18507 "int i = 1;\n"
18508 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18509 " loooooooooooooooooooooongParameterB);\n"
18510 "int j = 2;",
18511 Alignment);
18513 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18514 " typename B = very_long_type_name_1,\n"
18515 " typename T_2 = very_long_type_name_2>\n"
18516 "auto foo() {}",
18517 Alignment);
18518 verifyFormat("int a, b = 1;\n"
18519 "int c = 2;\n"
18520 "int dd = 3;",
18521 Alignment);
18522 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18523 "float b[1][] = {{3.f}};",
18524 Alignment);
18525 verifyFormat("for (int i = 0; i < 1; i++)\n"
18526 " int x = 1;",
18527 Alignment);
18528 verifyFormat("for (i = 0; i < 1; i++)\n"
18529 " x = 1;\n"
18530 "y = 1;",
18531 Alignment);
18533 EXPECT_EQ(Alignment.ReflowComments, true);
18534 Alignment.ColumnLimit = 50;
18535 verifyFormat("int x = 0;\n"
18536 "int yy = 1; /// specificlennospace\n"
18537 "int zzz = 2;",
18538 "int x = 0;\n"
18539 "int yy = 1; ///specificlennospace\n"
18540 "int zzz = 2;",
18541 Alignment);
18543 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18544 "auto b = [] {\n"
18545 " f();\n"
18546 " return;\n"
18547 "};",
18548 Alignment);
18549 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18550 "auto b = g([] {\n"
18551 " f();\n"
18552 " return;\n"
18553 "});",
18554 Alignment);
18555 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18556 "auto b = g(param, [] {\n"
18557 " f();\n"
18558 " return;\n"
18559 "});",
18560 Alignment);
18561 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18562 "auto b = [] {\n"
18563 " if (condition) {\n"
18564 " return;\n"
18565 " }\n"
18566 "};",
18567 Alignment);
18569 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18570 " ccc ? aaaaa : bbbbb,\n"
18571 " dddddddddddddddddddddddddd);",
18572 Alignment);
18573 // FIXME: https://llvm.org/PR53497
18574 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
18575 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18576 // " ccc ? aaaaa : bbbbb,\n"
18577 // " dddddddddddddddddddddddddd);",
18578 // Alignment);
18580 // Confirm proper handling of AlignConsecutiveAssignments with
18581 // BinPackArguments.
18582 // See https://llvm.org/PR55360
18583 Alignment = getLLVMStyleWithColumns(50);
18584 Alignment.AlignConsecutiveAssignments.Enabled = true;
18585 Alignment.BinPackArguments = false;
18586 verifyFormat("int a_long_name = 1;\n"
18587 "auto b = B({a_long_name, a_long_name},\n"
18588 " {a_longer_name_for_wrap,\n"
18589 " a_longer_name_for_wrap});",
18590 Alignment);
18591 verifyFormat("int a_long_name = 1;\n"
18592 "auto b = B{{a_long_name, a_long_name},\n"
18593 " {a_longer_name_for_wrap,\n"
18594 " a_longer_name_for_wrap}};",
18595 Alignment);
18597 Alignment = getLLVMStyleWithColumns(60);
18598 Alignment.AlignConsecutiveAssignments.Enabled = true;
18599 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
18600 "using I = std::conditional_t<II::value >= 0,\n"
18601 " std::ic<int, II::value + 1>,\n"
18602 " std::ic<int, -1>>;",
18603 Alignment);
18604 verifyFormat("SomeName = Foo;\n"
18605 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
18606 " arrrrrrrrrrg);",
18607 Alignment);
18610 TEST_F(FormatTest, AlignConsecutiveBitFields) {
18611 FormatStyle Alignment = getLLVMStyle();
18612 Alignment.AlignConsecutiveBitFields.Enabled = true;
18613 verifyFormat("int const a : 5;\n"
18614 "int oneTwoThree : 23;",
18615 Alignment);
18617 // Initializers are allowed starting with c++2a
18618 verifyFormat("int const a : 5 = 1;\n"
18619 "int oneTwoThree : 23 = 0;",
18620 Alignment);
18622 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18623 verifyFormat("int const a : 5;\n"
18624 "int oneTwoThree : 23;",
18625 Alignment);
18627 verifyFormat("int const a : 5; // comment\n"
18628 "int oneTwoThree : 23; // comment",
18629 Alignment);
18631 verifyFormat("int const a : 5 = 1;\n"
18632 "int oneTwoThree : 23 = 0;",
18633 Alignment);
18635 Alignment.AlignConsecutiveAssignments.Enabled = true;
18636 verifyFormat("int const a : 5 = 1;\n"
18637 "int oneTwoThree : 23 = 0;",
18638 Alignment);
18639 verifyFormat("int const a : 5 = {1};\n"
18640 "int oneTwoThree : 23 = 0;",
18641 Alignment);
18643 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
18644 verifyFormat("int const a :5;\n"
18645 "int oneTwoThree:23;",
18646 Alignment);
18648 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
18649 verifyFormat("int const a :5;\n"
18650 "int oneTwoThree :23;",
18651 Alignment);
18653 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
18654 verifyFormat("int const a : 5;\n"
18655 "int oneTwoThree: 23;",
18656 Alignment);
18658 // Known limitations: ':' is only recognized as a bitfield colon when
18659 // followed by a number.
18661 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
18662 "int a : 5;",
18663 Alignment);
18667 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
18668 FormatStyle Alignment = getLLVMStyle();
18669 Alignment.AlignConsecutiveMacros.Enabled = true;
18670 Alignment.PointerAlignment = FormatStyle::PAS_Right;
18671 verifyFormat("float const a = 5;\n"
18672 "int oneTwoThree = 123;",
18673 Alignment);
18674 verifyFormat("int a = 5;\n"
18675 "float const oneTwoThree = 123;",
18676 Alignment);
18678 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18679 verifyFormat("float const a = 5;\n"
18680 "int oneTwoThree = 123;",
18681 Alignment);
18682 verifyFormat("int a = method();\n"
18683 "float const oneTwoThree = 133;",
18684 Alignment);
18685 verifyFormat("int i = 1, j = 10;\n"
18686 "something = 2000;",
18687 Alignment);
18688 verifyFormat("something = 2000;\n"
18689 "int i = 1, j = 10;",
18690 Alignment);
18691 verifyFormat("float something = 2000;\n"
18692 "double another = 911;\n"
18693 "int i = 1, j = 10;\n"
18694 "const int *oneMore = 1;\n"
18695 "unsigned i = 2;",
18696 Alignment);
18697 verifyFormat("float a = 5;\n"
18698 "int one = 1;\n"
18699 "method();\n"
18700 "const double oneTwoThree = 123;\n"
18701 "const unsigned int oneTwo = 12;",
18702 Alignment);
18703 verifyFormat("int oneTwoThree{0}; // comment\n"
18704 "unsigned oneTwo; // comment",
18705 Alignment);
18706 verifyFormat("unsigned int *a;\n"
18707 "int *b;\n"
18708 "unsigned int Const *c;\n"
18709 "unsigned int const *d;\n"
18710 "unsigned int Const &e;\n"
18711 "unsigned int const &f;",
18712 Alignment);
18713 verifyFormat("Const unsigned int *c;\n"
18714 "const unsigned int *d;\n"
18715 "Const unsigned int &e;\n"
18716 "const unsigned int &f;\n"
18717 "const unsigned g;\n"
18718 "Const unsigned h;",
18719 Alignment);
18720 verifyFormat("float const a = 5;\n"
18721 "\n"
18722 "int oneTwoThree = 123;",
18723 "float const a = 5;\n"
18724 "\n"
18725 "int oneTwoThree= 123;",
18726 Alignment);
18727 verifyFormat("float a = 5;\n"
18728 "int one = 1;\n"
18729 "\n"
18730 "unsigned oneTwoThree = 123;",
18731 "float a = 5;\n"
18732 "int one = 1;\n"
18733 "\n"
18734 "unsigned oneTwoThree = 123;",
18735 Alignment);
18736 verifyFormat("float a = 5;\n"
18737 "int one = 1;\n"
18738 "\n"
18739 "unsigned oneTwoThree = 123;\n"
18740 "int oneTwo = 12;",
18741 "float a = 5;\n"
18742 "int one = 1;\n"
18743 "\n"
18744 "unsigned oneTwoThree = 123;\n"
18745 "int oneTwo = 12;",
18746 Alignment);
18747 // Function prototype alignment
18748 verifyFormat("int a();\n"
18749 "double b();",
18750 Alignment);
18751 verifyFormat("int a(int x);\n"
18752 "double b();",
18753 Alignment);
18754 verifyFormat("struct Test {\n"
18755 " Test(const Test &) = default;\n"
18756 " ~Test() = default;\n"
18757 " Test &operator=(const Test &) = default;\n"
18758 "};",
18759 Alignment);
18760 unsigned OldColumnLimit = Alignment.ColumnLimit;
18761 // We need to set ColumnLimit to zero, in order to stress nested alignments,
18762 // otherwise the function parameters will be re-flowed onto a single line.
18763 Alignment.ColumnLimit = 0;
18764 verifyFormat("int a(int x,\n"
18765 " float y);\n"
18766 "double b(int x,\n"
18767 " double y);",
18768 "int a(int x,\n"
18769 " float y);\n"
18770 "double b(int x,\n"
18771 " double y);",
18772 Alignment);
18773 // This ensures that function parameters of function declarations are
18774 // correctly indented when their owning functions are indented.
18775 // The failure case here is for 'double y' to not be indented enough.
18776 verifyFormat("double a(int x);\n"
18777 "int b(int y,\n"
18778 " double z);",
18779 "double a(int x);\n"
18780 "int b(int y,\n"
18781 " double z);",
18782 Alignment);
18783 // Set ColumnLimit low so that we induce wrapping immediately after
18784 // the function name and opening paren.
18785 Alignment.ColumnLimit = 13;
18786 verifyFormat("int function(\n"
18787 " int x,\n"
18788 " bool y);",
18789 Alignment);
18790 Alignment.ColumnLimit = OldColumnLimit;
18791 // Ensure function pointers don't screw up recursive alignment
18792 verifyFormat("int a(int x, void (*fp)(int y));\n"
18793 "double b();",
18794 Alignment);
18795 Alignment.AlignConsecutiveAssignments.Enabled = true;
18796 verifyFormat("struct Test {\n"
18797 " Test(const Test &) = default;\n"
18798 " ~Test() = default;\n"
18799 " Test &operator=(const Test &) = default;\n"
18800 "};",
18801 Alignment);
18802 // Ensure recursive alignment is broken by function braces, so that the
18803 // "a = 1" does not align with subsequent assignments inside the function
18804 // body.
18805 verifyFormat("int func(int a = 1) {\n"
18806 " int b = 2;\n"
18807 " int cc = 3;\n"
18808 "}",
18809 Alignment);
18810 verifyFormat("float something = 2000;\n"
18811 "double another = 911;\n"
18812 "int i = 1, j = 10;\n"
18813 "const int *oneMore = 1;\n"
18814 "unsigned i = 2;",
18815 Alignment);
18816 verifyFormat("int oneTwoThree = {0}; // comment\n"
18817 "unsigned oneTwo = 0; // comment",
18818 Alignment);
18819 // Make sure that scope is correctly tracked, in the absence of braces
18820 verifyFormat("for (int i = 0; i < n; i++)\n"
18821 " j = i;\n"
18822 "double x = 1;",
18823 Alignment);
18824 verifyFormat("if (int i = 0)\n"
18825 " j = i;\n"
18826 "double x = 1;",
18827 Alignment);
18828 // Ensure operator[] and operator() are comprehended
18829 verifyFormat("struct test {\n"
18830 " long long int foo();\n"
18831 " int operator[](int a);\n"
18832 " double bar();\n"
18833 "};",
18834 Alignment);
18835 verifyFormat("struct test {\n"
18836 " long long int foo();\n"
18837 " int operator()(int a);\n"
18838 " double bar();\n"
18839 "};",
18840 Alignment);
18841 // http://llvm.org/PR52914
18842 verifyFormat("char *a[] = {\"a\", // comment\n"
18843 " \"bb\"};\n"
18844 "int bbbbbbb = 0;",
18845 Alignment);
18847 // PAS_Right
18848 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18849 " int const i = 1;\n"
18850 " int *j = 2;\n"
18851 " int big = 10000;\n"
18852 "\n"
18853 " unsigned oneTwoThree = 123;\n"
18854 " int oneTwo = 12;\n"
18855 " method();\n"
18856 " float k = 2;\n"
18857 " int ll = 10000;\n"
18858 "}",
18859 "void SomeFunction(int parameter= 0) {\n"
18860 " int const i= 1;\n"
18861 " int *j=2;\n"
18862 " int big = 10000;\n"
18863 "\n"
18864 "unsigned oneTwoThree =123;\n"
18865 "int oneTwo = 12;\n"
18866 " method();\n"
18867 "float k= 2;\n"
18868 "int ll=10000;\n"
18869 "}",
18870 Alignment);
18871 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18872 " int const i = 1;\n"
18873 " int **j = 2, ***k;\n"
18874 " int &k = i;\n"
18875 " int &&l = i + j;\n"
18876 " int big = 10000;\n"
18877 "\n"
18878 " unsigned oneTwoThree = 123;\n"
18879 " int oneTwo = 12;\n"
18880 " method();\n"
18881 " float k = 2;\n"
18882 " int ll = 10000;\n"
18883 "}",
18884 "void SomeFunction(int parameter= 0) {\n"
18885 " int const i= 1;\n"
18886 " int **j=2,***k;\n"
18887 "int &k=i;\n"
18888 "int &&l=i+j;\n"
18889 " int big = 10000;\n"
18890 "\n"
18891 "unsigned oneTwoThree =123;\n"
18892 "int oneTwo = 12;\n"
18893 " method();\n"
18894 "float k= 2;\n"
18895 "int ll=10000;\n"
18896 "}",
18897 Alignment);
18898 // variables are aligned at their name, pointers are at the right most
18899 // position
18900 verifyFormat("int *a;\n"
18901 "int **b;\n"
18902 "int ***c;\n"
18903 "int foobar;",
18904 Alignment);
18906 // PAS_Left
18907 FormatStyle AlignmentLeft = Alignment;
18908 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
18909 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18910 " int const i = 1;\n"
18911 " int* j = 2;\n"
18912 " int big = 10000;\n"
18913 "\n"
18914 " unsigned oneTwoThree = 123;\n"
18915 " int oneTwo = 12;\n"
18916 " method();\n"
18917 " float k = 2;\n"
18918 " int ll = 10000;\n"
18919 "}",
18920 "void SomeFunction(int parameter= 0) {\n"
18921 " int const i= 1;\n"
18922 " int *j=2;\n"
18923 " int big = 10000;\n"
18924 "\n"
18925 "unsigned oneTwoThree =123;\n"
18926 "int oneTwo = 12;\n"
18927 " method();\n"
18928 "float k= 2;\n"
18929 "int ll=10000;\n"
18930 "}",
18931 AlignmentLeft);
18932 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18933 " int const i = 1;\n"
18934 " int** j = 2;\n"
18935 " int& k = i;\n"
18936 " int&& l = i + j;\n"
18937 " int big = 10000;\n"
18938 "\n"
18939 " unsigned oneTwoThree = 123;\n"
18940 " int oneTwo = 12;\n"
18941 " method();\n"
18942 " float k = 2;\n"
18943 " int ll = 10000;\n"
18944 "}",
18945 "void SomeFunction(int parameter= 0) {\n"
18946 " int const i= 1;\n"
18947 " int **j=2;\n"
18948 "int &k=i;\n"
18949 "int &&l=i+j;\n"
18950 " int big = 10000;\n"
18951 "\n"
18952 "unsigned oneTwoThree =123;\n"
18953 "int oneTwo = 12;\n"
18954 " method();\n"
18955 "float k= 2;\n"
18956 "int ll=10000;\n"
18957 "}",
18958 AlignmentLeft);
18959 // variables are aligned at their name, pointers are at the left most position
18960 verifyFormat("int* a;\n"
18961 "int** b;\n"
18962 "int*** c;\n"
18963 "int foobar;",
18964 AlignmentLeft);
18966 // PAS_Middle
18967 FormatStyle AlignmentMiddle = Alignment;
18968 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
18969 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18970 " int const i = 1;\n"
18971 " int * j = 2;\n"
18972 " int big = 10000;\n"
18973 "\n"
18974 " unsigned oneTwoThree = 123;\n"
18975 " int oneTwo = 12;\n"
18976 " method();\n"
18977 " float k = 2;\n"
18978 " int ll = 10000;\n"
18979 "}",
18980 "void SomeFunction(int parameter= 0) {\n"
18981 " int const i= 1;\n"
18982 " int *j=2;\n"
18983 " int big = 10000;\n"
18984 "\n"
18985 "unsigned oneTwoThree =123;\n"
18986 "int oneTwo = 12;\n"
18987 " method();\n"
18988 "float k= 2;\n"
18989 "int ll=10000;\n"
18990 "}",
18991 AlignmentMiddle);
18992 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18993 " int const i = 1;\n"
18994 " int ** j = 2, ***k;\n"
18995 " int & k = i;\n"
18996 " int && l = i + j;\n"
18997 " int big = 10000;\n"
18998 "\n"
18999 " unsigned oneTwoThree = 123;\n"
19000 " int oneTwo = 12;\n"
19001 " method();\n"
19002 " float k = 2;\n"
19003 " int ll = 10000;\n"
19004 "}",
19005 "void SomeFunction(int parameter= 0) {\n"
19006 " int const i= 1;\n"
19007 " int **j=2,***k;\n"
19008 "int &k=i;\n"
19009 "int &&l=i+j;\n"
19010 " int big = 10000;\n"
19011 "\n"
19012 "unsigned oneTwoThree =123;\n"
19013 "int oneTwo = 12;\n"
19014 " method();\n"
19015 "float k= 2;\n"
19016 "int ll=10000;\n"
19017 "}",
19018 AlignmentMiddle);
19019 // variables are aligned at their name, pointers are in the middle
19020 verifyFormat("int * a;\n"
19021 "int * b;\n"
19022 "int *** c;\n"
19023 "int foobar;",
19024 AlignmentMiddle);
19026 Alignment.AlignConsecutiveAssignments.Enabled = false;
19027 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19028 verifyFormat("#define A \\\n"
19029 " int aaaa = 12; \\\n"
19030 " float b = 23; \\\n"
19031 " const int ccc = 234; \\\n"
19032 " unsigned dddddddddd = 2345;",
19033 Alignment);
19034 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19035 verifyFormat("#define A \\\n"
19036 " int aaaa = 12; \\\n"
19037 " float b = 23; \\\n"
19038 " const int ccc = 234; \\\n"
19039 " unsigned dddddddddd = 2345;",
19040 Alignment);
19041 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19042 Alignment.ColumnLimit = 30;
19043 verifyFormat("#define A \\\n"
19044 " int aaaa = 12; \\\n"
19045 " float b = 23; \\\n"
19046 " const int ccc = 234; \\\n"
19047 " int dddddddddd = 2345;",
19048 Alignment);
19049 Alignment.ColumnLimit = 80;
19050 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19051 "k = 4, int l = 5,\n"
19052 " int m = 6) {\n"
19053 " const int j = 10;\n"
19054 " otherThing = 1;\n"
19055 "}",
19056 Alignment);
19057 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19058 " int const i = 1;\n"
19059 " int *j = 2;\n"
19060 " int big = 10000;\n"
19061 "}",
19062 Alignment);
19063 verifyFormat("class C {\n"
19064 "public:\n"
19065 " int i = 1;\n"
19066 " virtual void f() = 0;\n"
19067 "};",
19068 Alignment);
19069 verifyFormat("float i = 1;\n"
19070 "if (SomeType t = getSomething()) {\n"
19071 "}\n"
19072 "const unsigned j = 2;\n"
19073 "int big = 10000;",
19074 Alignment);
19075 verifyFormat("float j = 7;\n"
19076 "for (int k = 0; k < N; ++k) {\n"
19077 "}\n"
19078 "unsigned j = 2;\n"
19079 "int big = 10000;\n"
19080 "}",
19081 Alignment);
19082 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19083 verifyFormat("float i = 1;\n"
19084 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19085 " = someLooooooooooooooooongFunction();\n"
19086 "int j = 2;",
19087 Alignment);
19088 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19089 verifyFormat("int i = 1;\n"
19090 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19091 " someLooooooooooooooooongFunction();\n"
19092 "int j = 2;",
19093 Alignment);
19095 Alignment.AlignConsecutiveAssignments.Enabled = true;
19096 verifyFormat("auto lambda = []() {\n"
19097 " auto ii = 0;\n"
19098 " float j = 0;\n"
19099 " return 0;\n"
19100 "};\n"
19101 "int i = 0;\n"
19102 "float i2 = 0;\n"
19103 "auto v = type{\n"
19104 " i = 1, //\n"
19105 " (i = 2), //\n"
19106 " i = 3 //\n"
19107 "};",
19108 Alignment);
19109 Alignment.AlignConsecutiveAssignments.Enabled = false;
19111 verifyFormat(
19112 "int i = 1;\n"
19113 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19114 " loooooooooooooooooooooongParameterB);\n"
19115 "int j = 2;",
19116 Alignment);
19118 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19119 // We expect declarations and assignments to align, as long as it doesn't
19120 // exceed the column limit, starting a new alignment sequence whenever it
19121 // happens.
19122 Alignment.AlignConsecutiveAssignments.Enabled = true;
19123 Alignment.ColumnLimit = 30;
19124 verifyFormat("float ii = 1;\n"
19125 "unsigned j = 2;\n"
19126 "int someVerylongVariable = 1;\n"
19127 "AnotherLongType ll = 123456;\n"
19128 "VeryVeryLongType k = 2;\n"
19129 "int myvar = 1;",
19130 Alignment);
19131 Alignment.ColumnLimit = 80;
19132 Alignment.AlignConsecutiveAssignments.Enabled = false;
19134 verifyFormat(
19135 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19136 " typename LongType, typename B>\n"
19137 "auto foo() {}",
19138 Alignment);
19139 verifyFormat("float a, b = 1;\n"
19140 "int c = 2;\n"
19141 "int dd = 3;",
19142 Alignment);
19143 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19144 "float b[1][] = {{3.f}};",
19145 Alignment);
19146 Alignment.AlignConsecutiveAssignments.Enabled = true;
19147 verifyFormat("float a, b = 1;\n"
19148 "int c = 2;\n"
19149 "int dd = 3;",
19150 Alignment);
19151 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19152 "float b[1][] = {{3.f}};",
19153 Alignment);
19154 Alignment.AlignConsecutiveAssignments.Enabled = false;
19156 Alignment.ColumnLimit = 30;
19157 Alignment.BinPackParameters = false;
19158 verifyFormat("void foo(float a,\n"
19159 " float b,\n"
19160 " int c,\n"
19161 " uint32_t *d) {\n"
19162 " int *e = 0;\n"
19163 " float f = 0;\n"
19164 " double g = 0;\n"
19165 "}\n"
19166 "void bar(ino_t a,\n"
19167 " int b,\n"
19168 " uint32_t *c,\n"
19169 " bool d) {}",
19170 Alignment);
19171 Alignment.BinPackParameters = true;
19172 Alignment.ColumnLimit = 80;
19174 // Bug 33507
19175 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19176 verifyFormat(
19177 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19178 " static const Version verVs2017;\n"
19179 " return true;\n"
19180 "});",
19181 Alignment);
19182 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19184 // See llvm.org/PR35641
19185 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19186 verifyFormat("int func() { //\n"
19187 " int b;\n"
19188 " unsigned c;\n"
19189 "}",
19190 Alignment);
19192 // See PR37175
19193 FormatStyle Style = getMozillaStyle();
19194 Style.AlignConsecutiveDeclarations.Enabled = true;
19195 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19196 "foo(int a);",
19197 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
19199 Alignment.PointerAlignment = FormatStyle::PAS_Left;
19200 verifyFormat("unsigned int* a;\n"
19201 "int* b;\n"
19202 "unsigned int Const* c;\n"
19203 "unsigned int const* d;\n"
19204 "unsigned int Const& e;\n"
19205 "unsigned int const& f;",
19206 Alignment);
19207 verifyFormat("Const unsigned int* c;\n"
19208 "const unsigned int* d;\n"
19209 "Const unsigned int& e;\n"
19210 "const unsigned int& f;\n"
19211 "const unsigned g;\n"
19212 "Const unsigned h;",
19213 Alignment);
19215 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19216 verifyFormat("unsigned int * a;\n"
19217 "int * b;\n"
19218 "unsigned int Const * c;\n"
19219 "unsigned int const * d;\n"
19220 "unsigned int Const & e;\n"
19221 "unsigned int const & f;",
19222 Alignment);
19223 verifyFormat("Const unsigned int * c;\n"
19224 "const unsigned int * d;\n"
19225 "Const unsigned int & e;\n"
19226 "const unsigned int & f;\n"
19227 "const unsigned g;\n"
19228 "Const unsigned h;",
19229 Alignment);
19231 // See PR46529
19232 FormatStyle BracedAlign = getLLVMStyle();
19233 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
19234 verifyFormat("const auto result{[]() {\n"
19235 " const auto something = 1;\n"
19236 " return 2;\n"
19237 "}};",
19238 BracedAlign);
19239 verifyFormat("int foo{[]() {\n"
19240 " int bar{0};\n"
19241 " return 0;\n"
19242 "}()};",
19243 BracedAlign);
19244 BracedAlign.Cpp11BracedListStyle = false;
19245 verifyFormat("const auto result{ []() {\n"
19246 " const auto something = 1;\n"
19247 " return 2;\n"
19248 "} };",
19249 BracedAlign);
19250 verifyFormat("int foo{ []() {\n"
19251 " int bar{ 0 };\n"
19252 " return 0;\n"
19253 "}() };",
19254 BracedAlign);
19257 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
19258 FormatStyle Alignment = getLLVMStyle();
19259 Alignment.AllowShortCaseLabelsOnASingleLine = true;
19260 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
19262 verifyFormat("switch (level) {\n"
19263 "case log::info: return \"info\";\n"
19264 "case log::warning: return \"warning\";\n"
19265 "default: return \"default\";\n"
19266 "}",
19267 Alignment);
19269 verifyFormat("switch (level) {\n"
19270 "case log::info: return \"info\";\n"
19271 "case log::warning: return \"warning\";\n"
19272 "}",
19273 "switch (level) {\n"
19274 "case log::info: return \"info\";\n"
19275 "case log::warning:\n"
19276 " return \"warning\";\n"
19277 "}",
19278 Alignment);
19280 // Empty case statements push out the alignment, but non-short case labels
19281 // don't.
19282 verifyFormat("switch (level) {\n"
19283 "case log::info: return \"info\";\n"
19284 "case log::critical:\n"
19285 "case log::warning:\n"
19286 "case log::severe: return \"severe\";\n"
19287 "case log::extra_severe:\n"
19288 " // comment\n"
19289 " return \"extra_severe\";\n"
19290 "}",
19291 Alignment);
19293 // Verify comments and empty lines break the alignment.
19294 verifyNoChange("switch (level) {\n"
19295 "case log::info: return \"info\";\n"
19296 "case log::warning: return \"warning\";\n"
19297 "// comment\n"
19298 "case log::critical: return \"critical\";\n"
19299 "default: return \"default\";\n"
19300 "\n"
19301 "case log::severe: return \"severe\";\n"
19302 "}",
19303 Alignment);
19305 // Empty case statements don't break the alignment, and potentially push it
19306 // out.
19307 verifyFormat("switch (level) {\n"
19308 "case log::info: return \"info\";\n"
19309 "case log::warning:\n"
19310 "case log::critical:\n"
19311 "default: return \"default\";\n"
19312 "}",
19313 Alignment);
19315 // Implicit fallthrough cases can be aligned with either a comment or
19316 // [[fallthrough]]
19317 verifyFormat("switch (level) {\n"
19318 "case log::info: return \"info\";\n"
19319 "case log::warning: // fallthrough\n"
19320 "case log::error: return \"error\";\n"
19321 "case log::critical: /*fallthrough*/\n"
19322 "case log::severe: return \"severe\";\n"
19323 "case log::diag: [[fallthrough]];\n"
19324 "default: return \"default\";\n"
19325 "}",
19326 Alignment);
19328 // Verify trailing comment that needs a reflow also gets aligned properly.
19329 verifyFormat("switch (level) {\n"
19330 "case log::info: return \"info\";\n"
19331 "case log::warning: // fallthrough\n"
19332 "case log::error: return \"error\";\n"
19333 "}",
19334 "switch (level) {\n"
19335 "case log::info: return \"info\";\n"
19336 "case log::warning: //fallthrough\n"
19337 "case log::error: return \"error\";\n"
19338 "}",
19339 Alignment);
19341 // Verify adjacent non-short case statements don't change the alignment, and
19342 // properly break the set of consecutive statements.
19343 verifyFormat("switch (level) {\n"
19344 "case log::critical:\n"
19345 " // comment\n"
19346 " return \"critical\";\n"
19347 "case log::info: return \"info\";\n"
19348 "case log::warning: return \"warning\";\n"
19349 "default:\n"
19350 " // comment\n"
19351 " return \"\";\n"
19352 "case log::error: return \"error\";\n"
19353 "case log::severe: return \"severe\";\n"
19354 "case log::extra_critical:\n"
19355 " // comment\n"
19356 " return \"extra critical\";\n"
19357 "}",
19358 Alignment);
19360 Alignment.SpaceBeforeCaseColon = true;
19361 verifyFormat("switch (level) {\n"
19362 "case log::info : return \"info\";\n"
19363 "case log::warning : return \"warning\";\n"
19364 "default : return \"default\";\n"
19365 "}",
19366 Alignment);
19367 Alignment.SpaceBeforeCaseColon = false;
19369 // Make sure we don't incorrectly align correctly across nested switch cases.
19370 verifyFormat("switch (level) {\n"
19371 "case log::info: return \"info\";\n"
19372 "case log::warning: return \"warning\";\n"
19373 "case log::other:\n"
19374 " switch (sublevel) {\n"
19375 " case log::info: return \"info\";\n"
19376 " case log::warning: return \"warning\";\n"
19377 " }\n"
19378 " break;\n"
19379 "case log::error: return \"error\";\n"
19380 "default: return \"default\";\n"
19381 "}",
19382 "switch (level) {\n"
19383 "case log::info: return \"info\";\n"
19384 "case log::warning: return \"warning\";\n"
19385 "case log::other: switch (sublevel) {\n"
19386 " case log::info: return \"info\";\n"
19387 " case log::warning: return \"warning\";\n"
19388 "}\n"
19389 "break;\n"
19390 "case log::error: return \"error\";\n"
19391 "default: return \"default\";\n"
19392 "}",
19393 Alignment);
19395 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
19397 verifyFormat("switch (level) {\n"
19398 "case log::info: return \"info\";\n"
19399 "\n"
19400 "case log::warning: return \"warning\";\n"
19401 "}",
19402 "switch (level) {\n"
19403 "case log::info: return \"info\";\n"
19404 "\n"
19405 "case log::warning: return \"warning\";\n"
19406 "}",
19407 Alignment);
19409 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
19411 verifyNoChange("switch (level) {\n"
19412 "case log::info: return \"info\";\n"
19413 "\n"
19414 "/* block comment */\n"
19415 "\n"
19416 "// line comment\n"
19417 "case log::warning: return \"warning\";\n"
19418 "}",
19419 Alignment);
19421 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
19423 verifyFormat("switch (level) {\n"
19424 "case log::info: return \"info\";\n"
19425 "//\n"
19426 "case log::warning: return \"warning\";\n"
19427 "}",
19428 Alignment);
19430 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
19432 verifyFormat("switch (level) {\n"
19433 "case log::info : return \"info\";\n"
19434 "case log::warning: return \"warning\";\n"
19435 "default : return \"default\";\n"
19436 "}",
19437 Alignment);
19439 // With AlignCaseColons, empty case statements don't break alignment of
19440 // consecutive case statements (and are aligned).
19441 verifyFormat("switch (level) {\n"
19442 "case log::info : return \"info\";\n"
19443 "case log::warning :\n"
19444 "case log::critical:\n"
19445 "default : return \"default\";\n"
19446 "}",
19447 Alignment);
19449 // Final non-short case labels shouldn't have their colon aligned
19450 verifyFormat("switch (level) {\n"
19451 "case log::info : return \"info\";\n"
19452 "case log::warning :\n"
19453 "case log::critical:\n"
19454 "case log::severe : return \"severe\";\n"
19455 "default:\n"
19456 " // comment\n"
19457 " return \"default\";\n"
19458 "}",
19459 Alignment);
19461 // Verify adjacent non-short case statements break the set of consecutive
19462 // alignments and aren't aligned with adjacent non-short case statements if
19463 // AlignCaseColons is set.
19464 verifyFormat("switch (level) {\n"
19465 "case log::critical:\n"
19466 " // comment\n"
19467 " return \"critical\";\n"
19468 "case log::info : return \"info\";\n"
19469 "case log::warning: return \"warning\";\n"
19470 "default:\n"
19471 " // comment\n"
19472 " return \"\";\n"
19473 "case log::error : return \"error\";\n"
19474 "case log::severe: return \"severe\";\n"
19475 "case log::extra_critical:\n"
19476 " // comment\n"
19477 " return \"extra critical\";\n"
19478 "}",
19479 Alignment);
19481 Alignment.SpaceBeforeCaseColon = true;
19482 verifyFormat("switch (level) {\n"
19483 "case log::info : return \"info\";\n"
19484 "case log::warning : return \"warning\";\n"
19485 "case log::error :\n"
19486 "default : return \"default\";\n"
19487 "}",
19488 Alignment);
19491 TEST_F(FormatTest, AlignWithLineBreaks) {
19492 auto Style = getLLVMStyleWithColumns(120);
19494 EXPECT_EQ(Style.AlignConsecutiveAssignments,
19495 FormatStyle::AlignConsecutiveStyle(
19496 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
19497 /*AcrossComments=*/false, /*AlignCompound=*/false,
19498 /*PadOperators=*/true}));
19499 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
19500 FormatStyle::AlignConsecutiveStyle({}));
19501 verifyFormat("void foo() {\n"
19502 " int myVar = 5;\n"
19503 " double x = 3.14;\n"
19504 " auto str = \"Hello \"\n"
19505 " \"World\";\n"
19506 " auto s = \"Hello \"\n"
19507 " \"Again\";\n"
19508 "}",
19509 Style);
19511 // clang-format off
19512 verifyFormat("void foo() {\n"
19513 " const int capacityBefore = Entries.capacity();\n"
19514 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19515 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19516 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19517 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19518 "}",
19519 Style);
19520 // clang-format on
19522 Style.AlignConsecutiveAssignments.Enabled = true;
19523 verifyFormat("void foo() {\n"
19524 " int myVar = 5;\n"
19525 " double x = 3.14;\n"
19526 " auto str = \"Hello \"\n"
19527 " \"World\";\n"
19528 " auto s = \"Hello \"\n"
19529 " \"Again\";\n"
19530 "}",
19531 Style);
19533 // clang-format off
19534 verifyFormat("void foo() {\n"
19535 " const int capacityBefore = Entries.capacity();\n"
19536 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19537 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19538 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19539 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19540 "}",
19541 Style);
19542 // clang-format on
19544 Style.AlignConsecutiveAssignments.Enabled = false;
19545 Style.AlignConsecutiveDeclarations.Enabled = true;
19546 verifyFormat("void foo() {\n"
19547 " int myVar = 5;\n"
19548 " double x = 3.14;\n"
19549 " auto str = \"Hello \"\n"
19550 " \"World\";\n"
19551 " auto s = \"Hello \"\n"
19552 " \"Again\";\n"
19553 "}",
19554 Style);
19556 // clang-format off
19557 verifyFormat("void foo() {\n"
19558 " const int capacityBefore = Entries.capacity();\n"
19559 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19560 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19561 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19562 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19563 "}",
19564 Style);
19565 // clang-format on
19567 Style.AlignConsecutiveAssignments.Enabled = true;
19568 Style.AlignConsecutiveDeclarations.Enabled = true;
19570 verifyFormat("void foo() {\n"
19571 " int myVar = 5;\n"
19572 " double x = 3.14;\n"
19573 " auto str = \"Hello \"\n"
19574 " \"World\";\n"
19575 " auto s = \"Hello \"\n"
19576 " \"Again\";\n"
19577 "}",
19578 Style);
19580 // clang-format off
19581 verifyFormat("void foo() {\n"
19582 " const int capacityBefore = Entries.capacity();\n"
19583 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19584 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19585 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19586 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19587 "}",
19588 Style);
19589 // clang-format on
19591 Style = getLLVMStyleWithColumns(20);
19592 Style.AlignConsecutiveAssignments.Enabled = true;
19593 Style.IndentWidth = 4;
19595 verifyFormat("void foo() {\n"
19596 " int i1 = 1;\n"
19597 " int j = 0;\n"
19598 " int k = bar(\n"
19599 " argument1,\n"
19600 " argument2);\n"
19601 "}",
19602 Style);
19604 verifyFormat("unsigned i = 0;\n"
19605 "int a[] = {\n"
19606 " 1234567890,\n"
19607 " -1234567890};",
19608 Style);
19610 Style.ColumnLimit = 120;
19612 // clang-format off
19613 verifyFormat("void SomeFunc() {\n"
19614 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
19615 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19616 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
19617 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19618 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
19619 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19620 "}",
19621 Style);
19622 // clang-format on
19624 Style.BinPackArguments = false;
19626 // clang-format off
19627 verifyFormat("void SomeFunc() {\n"
19628 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
19629 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19630 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
19631 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19632 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
19633 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19634 "}",
19635 Style);
19636 // clang-format on
19639 TEST_F(FormatTest, AlignWithInitializerPeriods) {
19640 auto Style = getLLVMStyleWithColumns(60);
19642 verifyFormat("void foo1(void) {\n"
19643 " BYTE p[1] = 1;\n"
19644 " A B = {.one_foooooooooooooooo = 2,\n"
19645 " .two_fooooooooooooo = 3,\n"
19646 " .three_fooooooooooooo = 4};\n"
19647 " BYTE payload = 2;\n"
19648 "}",
19649 Style);
19651 Style.AlignConsecutiveAssignments.Enabled = true;
19652 Style.AlignConsecutiveDeclarations.Enabled = false;
19653 verifyFormat("void foo2(void) {\n"
19654 " BYTE p[1] = 1;\n"
19655 " A B = {.one_foooooooooooooooo = 2,\n"
19656 " .two_fooooooooooooo = 3,\n"
19657 " .three_fooooooooooooo = 4};\n"
19658 " BYTE payload = 2;\n"
19659 "}",
19660 Style);
19662 Style.AlignConsecutiveAssignments.Enabled = false;
19663 Style.AlignConsecutiveDeclarations.Enabled = true;
19664 verifyFormat("void foo3(void) {\n"
19665 " BYTE p[1] = 1;\n"
19666 " A B = {.one_foooooooooooooooo = 2,\n"
19667 " .two_fooooooooooooo = 3,\n"
19668 " .three_fooooooooooooo = 4};\n"
19669 " BYTE payload = 2;\n"
19670 "}",
19671 Style);
19673 Style.AlignConsecutiveAssignments.Enabled = true;
19674 Style.AlignConsecutiveDeclarations.Enabled = true;
19675 verifyFormat("void foo4(void) {\n"
19676 " BYTE p[1] = 1;\n"
19677 " A B = {.one_foooooooooooooooo = 2,\n"
19678 " .two_fooooooooooooo = 3,\n"
19679 " .three_fooooooooooooo = 4};\n"
19680 " BYTE payload = 2;\n"
19681 "}",
19682 Style);
19685 TEST_F(FormatTest, LinuxBraceBreaking) {
19686 FormatStyle LinuxBraceStyle = getLLVMStyle();
19687 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
19688 verifyFormat("namespace a\n"
19689 "{\n"
19690 "class A\n"
19691 "{\n"
19692 " void f()\n"
19693 " {\n"
19694 " if (true) {\n"
19695 " a();\n"
19696 " b();\n"
19697 " } else {\n"
19698 " a();\n"
19699 " }\n"
19700 " }\n"
19701 " void g() { return; }\n"
19702 "};\n"
19703 "struct B {\n"
19704 " int x;\n"
19705 "};\n"
19706 "} // namespace a",
19707 LinuxBraceStyle);
19708 verifyFormat("enum X {\n"
19709 " Y = 0,\n"
19710 "}",
19711 LinuxBraceStyle);
19712 verifyFormat("struct S {\n"
19713 " int Type;\n"
19714 " union {\n"
19715 " int x;\n"
19716 " double y;\n"
19717 " } Value;\n"
19718 " class C\n"
19719 " {\n"
19720 " MyFavoriteType Value;\n"
19721 " } Class;\n"
19722 "}",
19723 LinuxBraceStyle);
19726 TEST_F(FormatTest, MozillaBraceBreaking) {
19727 FormatStyle MozillaBraceStyle = getLLVMStyle();
19728 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
19729 MozillaBraceStyle.FixNamespaceComments = false;
19730 verifyFormat("namespace a {\n"
19731 "class A\n"
19732 "{\n"
19733 " void f()\n"
19734 " {\n"
19735 " if (true) {\n"
19736 " a();\n"
19737 " b();\n"
19738 " }\n"
19739 " }\n"
19740 " void g() { return; }\n"
19741 "};\n"
19742 "enum E\n"
19743 "{\n"
19744 " A,\n"
19745 " // foo\n"
19746 " B,\n"
19747 " C\n"
19748 "};\n"
19749 "struct B\n"
19750 "{\n"
19751 " int x;\n"
19752 "};\n"
19753 "}",
19754 MozillaBraceStyle);
19755 verifyFormat("struct S\n"
19756 "{\n"
19757 " int Type;\n"
19758 " union\n"
19759 " {\n"
19760 " int x;\n"
19761 " double y;\n"
19762 " } Value;\n"
19763 " class C\n"
19764 " {\n"
19765 " MyFavoriteType Value;\n"
19766 " } Class;\n"
19767 "}",
19768 MozillaBraceStyle);
19771 TEST_F(FormatTest, StroustrupBraceBreaking) {
19772 FormatStyle StroustrupBraceStyle = getLLVMStyle();
19773 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
19774 verifyFormat("namespace a {\n"
19775 "class A {\n"
19776 " void f()\n"
19777 " {\n"
19778 " if (true) {\n"
19779 " a();\n"
19780 " b();\n"
19781 " }\n"
19782 " }\n"
19783 " void g() { return; }\n"
19784 "};\n"
19785 "struct B {\n"
19786 " int x;\n"
19787 "};\n"
19788 "} // namespace a",
19789 StroustrupBraceStyle);
19791 verifyFormat("void foo()\n"
19792 "{\n"
19793 " if (a) {\n"
19794 " a();\n"
19795 " }\n"
19796 " else {\n"
19797 " b();\n"
19798 " }\n"
19799 "}",
19800 StroustrupBraceStyle);
19802 verifyFormat("#ifdef _DEBUG\n"
19803 "int foo(int i = 0)\n"
19804 "#else\n"
19805 "int foo(int i = 5)\n"
19806 "#endif\n"
19807 "{\n"
19808 " return i;\n"
19809 "}",
19810 StroustrupBraceStyle);
19812 verifyFormat("void foo() {}\n"
19813 "void bar()\n"
19814 "#ifdef _DEBUG\n"
19815 "{\n"
19816 " foo();\n"
19817 "}\n"
19818 "#else\n"
19819 "{\n"
19820 "}\n"
19821 "#endif",
19822 StroustrupBraceStyle);
19824 verifyFormat("void foobar() { int i = 5; }\n"
19825 "#ifdef _DEBUG\n"
19826 "void bar() {}\n"
19827 "#else\n"
19828 "void bar() { foobar(); }\n"
19829 "#endif",
19830 StroustrupBraceStyle);
19833 TEST_F(FormatTest, AllmanBraceBreaking) {
19834 FormatStyle AllmanBraceStyle = getLLVMStyle();
19835 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
19837 verifyFormat("namespace a\n"
19838 "{\n"
19839 "void f();\n"
19840 "void g();\n"
19841 "} // namespace a",
19842 "namespace a\n"
19843 "{\n"
19844 "void f();\n"
19845 "void g();\n"
19846 "}",
19847 AllmanBraceStyle);
19849 verifyFormat("namespace a\n"
19850 "{\n"
19851 "class A\n"
19852 "{\n"
19853 " void f()\n"
19854 " {\n"
19855 " if (true)\n"
19856 " {\n"
19857 " a();\n"
19858 " b();\n"
19859 " }\n"
19860 " }\n"
19861 " void g() { return; }\n"
19862 "};\n"
19863 "struct B\n"
19864 "{\n"
19865 " int x;\n"
19866 "};\n"
19867 "union C\n"
19868 "{\n"
19869 "};\n"
19870 "} // namespace a",
19871 AllmanBraceStyle);
19873 verifyFormat("void f()\n"
19874 "{\n"
19875 " if (true)\n"
19876 " {\n"
19877 " a();\n"
19878 " }\n"
19879 " else if (false)\n"
19880 " {\n"
19881 " b();\n"
19882 " }\n"
19883 " else\n"
19884 " {\n"
19885 " c();\n"
19886 " }\n"
19887 "}",
19888 AllmanBraceStyle);
19890 verifyFormat("void f()\n"
19891 "{\n"
19892 " for (int i = 0; i < 10; ++i)\n"
19893 " {\n"
19894 " a();\n"
19895 " }\n"
19896 " while (false)\n"
19897 " {\n"
19898 " b();\n"
19899 " }\n"
19900 " do\n"
19901 " {\n"
19902 " c();\n"
19903 " } while (false)\n"
19904 "}",
19905 AllmanBraceStyle);
19907 verifyFormat("void f(int a)\n"
19908 "{\n"
19909 " switch (a)\n"
19910 " {\n"
19911 " case 0:\n"
19912 " break;\n"
19913 " case 1:\n"
19914 " {\n"
19915 " break;\n"
19916 " }\n"
19917 " case 2:\n"
19918 " {\n"
19919 " }\n"
19920 " break;\n"
19921 " default:\n"
19922 " break;\n"
19923 " }\n"
19924 "}",
19925 AllmanBraceStyle);
19927 verifyFormat("enum X\n"
19928 "{\n"
19929 " Y = 0,\n"
19930 "}",
19931 AllmanBraceStyle);
19932 verifyFormat("enum X\n"
19933 "{\n"
19934 " Y = 0\n"
19935 "}",
19936 AllmanBraceStyle);
19938 verifyFormat("@interface BSApplicationController ()\n"
19939 "{\n"
19940 "@private\n"
19941 " id _extraIvar;\n"
19942 "}\n"
19943 "@end",
19944 AllmanBraceStyle);
19946 verifyFormat("#ifdef _DEBUG\n"
19947 "int foo(int i = 0)\n"
19948 "#else\n"
19949 "int foo(int i = 5)\n"
19950 "#endif\n"
19951 "{\n"
19952 " return i;\n"
19953 "}",
19954 AllmanBraceStyle);
19956 verifyFormat("void foo() {}\n"
19957 "void bar()\n"
19958 "#ifdef _DEBUG\n"
19959 "{\n"
19960 " foo();\n"
19961 "}\n"
19962 "#else\n"
19963 "{\n"
19964 "}\n"
19965 "#endif",
19966 AllmanBraceStyle);
19968 verifyFormat("void foobar() { int i = 5; }\n"
19969 "#ifdef _DEBUG\n"
19970 "void bar() {}\n"
19971 "#else\n"
19972 "void bar() { foobar(); }\n"
19973 "#endif",
19974 AllmanBraceStyle);
19976 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
19977 FormatStyle::SLS_All);
19979 verifyFormat("[](int i) { return i + 2; };\n"
19980 "[](int i, int j)\n"
19981 "{\n"
19982 " auto x = i + j;\n"
19983 " auto y = i * j;\n"
19984 " return x ^ y;\n"
19985 "};\n"
19986 "void foo()\n"
19987 "{\n"
19988 " auto shortLambda = [](int i) { return i + 2; };\n"
19989 " auto longLambda = [](int i, int j)\n"
19990 " {\n"
19991 " auto x = i + j;\n"
19992 " auto y = i * j;\n"
19993 " return x ^ y;\n"
19994 " };\n"
19995 "}",
19996 AllmanBraceStyle);
19998 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20000 verifyFormat("[](int i)\n"
20001 "{\n"
20002 " return i + 2;\n"
20003 "};\n"
20004 "[](int i, int j)\n"
20005 "{\n"
20006 " auto x = i + j;\n"
20007 " auto y = i * j;\n"
20008 " return x ^ y;\n"
20009 "};\n"
20010 "void foo()\n"
20011 "{\n"
20012 " auto shortLambda = [](int i)\n"
20013 " {\n"
20014 " return i + 2;\n"
20015 " };\n"
20016 " auto longLambda = [](int i, int j)\n"
20017 " {\n"
20018 " auto x = i + j;\n"
20019 " auto y = i * j;\n"
20020 " return x ^ y;\n"
20021 " };\n"
20022 "}",
20023 AllmanBraceStyle);
20025 // Reset
20026 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
20028 // This shouldn't affect ObjC blocks..
20029 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20030 " // ...\n"
20031 " int i;\n"
20032 "}];",
20033 AllmanBraceStyle);
20034 verifyFormat("void (^block)(void) = ^{\n"
20035 " // ...\n"
20036 " int i;\n"
20037 "};",
20038 AllmanBraceStyle);
20039 // .. or dict literals.
20040 verifyFormat("void f()\n"
20041 "{\n"
20042 " // ...\n"
20043 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20044 "}",
20045 AllmanBraceStyle);
20046 verifyFormat("void f()\n"
20047 "{\n"
20048 " // ...\n"
20049 " [object someMethod:@{a : @\"b\"}];\n"
20050 "}",
20051 AllmanBraceStyle);
20052 verifyFormat("int f()\n"
20053 "{ // comment\n"
20054 " return 42;\n"
20055 "}",
20056 AllmanBraceStyle);
20058 AllmanBraceStyle.ColumnLimit = 19;
20059 verifyFormat("void f() { int i; }", AllmanBraceStyle);
20060 AllmanBraceStyle.ColumnLimit = 18;
20061 verifyFormat("void f()\n"
20062 "{\n"
20063 " int i;\n"
20064 "}",
20065 AllmanBraceStyle);
20066 AllmanBraceStyle.ColumnLimit = 80;
20068 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
20069 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20070 FormatStyle::SIS_WithoutElse;
20071 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20072 verifyFormat("void f(bool b)\n"
20073 "{\n"
20074 " if (b)\n"
20075 " {\n"
20076 " return;\n"
20077 " }\n"
20078 "}",
20079 BreakBeforeBraceShortIfs);
20080 verifyFormat("void f(bool b)\n"
20081 "{\n"
20082 " if constexpr (b)\n"
20083 " {\n"
20084 " return;\n"
20085 " }\n"
20086 "}",
20087 BreakBeforeBraceShortIfs);
20088 verifyFormat("void f(bool b)\n"
20089 "{\n"
20090 " if CONSTEXPR (b)\n"
20091 " {\n"
20092 " return;\n"
20093 " }\n"
20094 "}",
20095 BreakBeforeBraceShortIfs);
20096 verifyFormat("void f(bool b)\n"
20097 "{\n"
20098 " if (b) return;\n"
20099 "}",
20100 BreakBeforeBraceShortIfs);
20101 verifyFormat("void f(bool b)\n"
20102 "{\n"
20103 " if constexpr (b) return;\n"
20104 "}",
20105 BreakBeforeBraceShortIfs);
20106 verifyFormat("void f(bool b)\n"
20107 "{\n"
20108 " if CONSTEXPR (b) return;\n"
20109 "}",
20110 BreakBeforeBraceShortIfs);
20111 verifyFormat("void f(bool b)\n"
20112 "{\n"
20113 " while (b)\n"
20114 " {\n"
20115 " return;\n"
20116 " }\n"
20117 "}",
20118 BreakBeforeBraceShortIfs);
20121 TEST_F(FormatTest, WhitesmithsBraceBreaking) {
20122 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0);
20123 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
20125 // Make a few changes to the style for testing purposes
20126 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
20127 FormatStyle::SFS_Empty;
20128 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20130 // FIXME: this test case can't decide whether there should be a blank line
20131 // after the ~D() line or not. It adds one if one doesn't exist in the test
20132 // and it removes the line if one exists.
20134 verifyFormat("class A;\n"
20135 "namespace B\n"
20136 " {\n"
20137 "class C;\n"
20138 "// Comment\n"
20139 "class D\n"
20140 " {\n"
20141 "public:\n"
20142 " D();\n"
20143 " ~D() {}\n"
20144 "private:\n"
20145 " enum E\n"
20146 " {\n"
20147 " F\n"
20148 " }\n"
20149 " };\n"
20150 " } // namespace B",
20151 WhitesmithsBraceStyle);
20154 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
20155 verifyFormat("namespace a\n"
20156 " {\n"
20157 "class A\n"
20158 " {\n"
20159 " void f()\n"
20160 " {\n"
20161 " if (true)\n"
20162 " {\n"
20163 " a();\n"
20164 " b();\n"
20165 " }\n"
20166 " }\n"
20167 " void g()\n"
20168 " {\n"
20169 " return;\n"
20170 " }\n"
20171 " };\n"
20172 "struct B\n"
20173 " {\n"
20174 " int x;\n"
20175 " };\n"
20176 " } // namespace a",
20177 WhitesmithsBraceStyle);
20179 verifyFormat("namespace a\n"
20180 " {\n"
20181 "namespace b\n"
20182 " {\n"
20183 "class A\n"
20184 " {\n"
20185 " void f()\n"
20186 " {\n"
20187 " if (true)\n"
20188 " {\n"
20189 " a();\n"
20190 " b();\n"
20191 " }\n"
20192 " }\n"
20193 " void g()\n"
20194 " {\n"
20195 " return;\n"
20196 " }\n"
20197 " };\n"
20198 "struct B\n"
20199 " {\n"
20200 " int x;\n"
20201 " };\n"
20202 " } // namespace b\n"
20203 " } // namespace a",
20204 WhitesmithsBraceStyle);
20206 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
20207 verifyFormat("namespace a\n"
20208 " {\n"
20209 "namespace b\n"
20210 " {\n"
20211 " class A\n"
20212 " {\n"
20213 " void f()\n"
20214 " {\n"
20215 " if (true)\n"
20216 " {\n"
20217 " a();\n"
20218 " b();\n"
20219 " }\n"
20220 " }\n"
20221 " void g()\n"
20222 " {\n"
20223 " return;\n"
20224 " }\n"
20225 " };\n"
20226 " struct B\n"
20227 " {\n"
20228 " int x;\n"
20229 " };\n"
20230 " } // namespace b\n"
20231 " } // namespace a",
20232 WhitesmithsBraceStyle);
20234 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
20235 verifyFormat("namespace a\n"
20236 " {\n"
20237 " namespace b\n"
20238 " {\n"
20239 " class A\n"
20240 " {\n"
20241 " void f()\n"
20242 " {\n"
20243 " if (true)\n"
20244 " {\n"
20245 " a();\n"
20246 " b();\n"
20247 " }\n"
20248 " }\n"
20249 " void g()\n"
20250 " {\n"
20251 " return;\n"
20252 " }\n"
20253 " };\n"
20254 " struct B\n"
20255 " {\n"
20256 " int x;\n"
20257 " };\n"
20258 " } // namespace b\n"
20259 " } // namespace a",
20260 WhitesmithsBraceStyle);
20262 verifyFormat("void f()\n"
20263 " {\n"
20264 " if (true)\n"
20265 " {\n"
20266 " a();\n"
20267 " }\n"
20268 " else if (false)\n"
20269 " {\n"
20270 " b();\n"
20271 " }\n"
20272 " else\n"
20273 " {\n"
20274 " c();\n"
20275 " }\n"
20276 " }",
20277 WhitesmithsBraceStyle);
20279 verifyFormat("void f()\n"
20280 " {\n"
20281 " for (int i = 0; i < 10; ++i)\n"
20282 " {\n"
20283 " a();\n"
20284 " }\n"
20285 " while (false)\n"
20286 " {\n"
20287 " b();\n"
20288 " }\n"
20289 " do\n"
20290 " {\n"
20291 " c();\n"
20292 " } while (false)\n"
20293 " }",
20294 WhitesmithsBraceStyle);
20296 WhitesmithsBraceStyle.IndentCaseLabels = true;
20297 verifyFormat("void switchTest1(int a)\n"
20298 " {\n"
20299 " switch (a)\n"
20300 " {\n"
20301 " case 2:\n"
20302 " {\n"
20303 " }\n"
20304 " break;\n"
20305 " }\n"
20306 " }",
20307 WhitesmithsBraceStyle);
20309 verifyFormat("void switchTest2(int a)\n"
20310 " {\n"
20311 " switch (a)\n"
20312 " {\n"
20313 " case 0:\n"
20314 " break;\n"
20315 " case 1:\n"
20316 " {\n"
20317 " break;\n"
20318 " }\n"
20319 " case 2:\n"
20320 " {\n"
20321 " }\n"
20322 " break;\n"
20323 " default:\n"
20324 " break;\n"
20325 " }\n"
20326 " }",
20327 WhitesmithsBraceStyle);
20329 verifyFormat("void switchTest3(int a)\n"
20330 " {\n"
20331 " switch (a)\n"
20332 " {\n"
20333 " case 0:\n"
20334 " {\n"
20335 " foo(x);\n"
20336 " }\n"
20337 " break;\n"
20338 " default:\n"
20339 " {\n"
20340 " foo(1);\n"
20341 " }\n"
20342 " break;\n"
20343 " }\n"
20344 " }",
20345 WhitesmithsBraceStyle);
20347 WhitesmithsBraceStyle.IndentCaseLabels = false;
20349 verifyFormat("void switchTest4(int a)\n"
20350 " {\n"
20351 " switch (a)\n"
20352 " {\n"
20353 " case 2:\n"
20354 " {\n"
20355 " }\n"
20356 " break;\n"
20357 " }\n"
20358 " }",
20359 WhitesmithsBraceStyle);
20361 verifyFormat("void switchTest5(int a)\n"
20362 " {\n"
20363 " switch (a)\n"
20364 " {\n"
20365 " case 0:\n"
20366 " break;\n"
20367 " case 1:\n"
20368 " {\n"
20369 " foo();\n"
20370 " break;\n"
20371 " }\n"
20372 " case 2:\n"
20373 " {\n"
20374 " }\n"
20375 " break;\n"
20376 " default:\n"
20377 " break;\n"
20378 " }\n"
20379 " }",
20380 WhitesmithsBraceStyle);
20382 verifyFormat("void switchTest6(int a)\n"
20383 " {\n"
20384 " switch (a)\n"
20385 " {\n"
20386 " case 0:\n"
20387 " {\n"
20388 " foo(x);\n"
20389 " }\n"
20390 " break;\n"
20391 " default:\n"
20392 " {\n"
20393 " foo(1);\n"
20394 " }\n"
20395 " break;\n"
20396 " }\n"
20397 " }",
20398 WhitesmithsBraceStyle);
20400 verifyFormat("enum X\n"
20401 " {\n"
20402 " Y = 0, // testing\n"
20403 " }",
20404 WhitesmithsBraceStyle);
20406 verifyFormat("enum X\n"
20407 " {\n"
20408 " Y = 0\n"
20409 " }",
20410 WhitesmithsBraceStyle);
20411 verifyFormat("enum X\n"
20412 " {\n"
20413 " Y = 0,\n"
20414 " Z = 1\n"
20415 " };",
20416 WhitesmithsBraceStyle);
20418 verifyFormat("@interface BSApplicationController ()\n"
20419 " {\n"
20420 "@private\n"
20421 " id _extraIvar;\n"
20422 " }\n"
20423 "@end",
20424 WhitesmithsBraceStyle);
20426 verifyFormat("#ifdef _DEBUG\n"
20427 "int foo(int i = 0)\n"
20428 "#else\n"
20429 "int foo(int i = 5)\n"
20430 "#endif\n"
20431 " {\n"
20432 " return i;\n"
20433 " }",
20434 WhitesmithsBraceStyle);
20436 verifyFormat("void foo() {}\n"
20437 "void bar()\n"
20438 "#ifdef _DEBUG\n"
20439 " {\n"
20440 " foo();\n"
20441 " }\n"
20442 "#else\n"
20443 " {\n"
20444 " }\n"
20445 "#endif",
20446 WhitesmithsBraceStyle);
20448 verifyFormat("void foobar()\n"
20449 " {\n"
20450 " int i = 5;\n"
20451 " }\n"
20452 "#ifdef _DEBUG\n"
20453 "void bar() {}\n"
20454 "#else\n"
20455 "void bar()\n"
20456 " {\n"
20457 " foobar();\n"
20458 " }\n"
20459 "#endif",
20460 WhitesmithsBraceStyle);
20462 // This shouldn't affect ObjC blocks..
20463 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20464 " // ...\n"
20465 " int i;\n"
20466 "}];",
20467 WhitesmithsBraceStyle);
20468 verifyFormat("void (^block)(void) = ^{\n"
20469 " // ...\n"
20470 " int i;\n"
20471 "};",
20472 WhitesmithsBraceStyle);
20473 // .. or dict literals.
20474 verifyFormat("void f()\n"
20475 " {\n"
20476 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20477 " }",
20478 WhitesmithsBraceStyle);
20480 verifyFormat("int f()\n"
20481 " { // comment\n"
20482 " return 42;\n"
20483 " }",
20484 WhitesmithsBraceStyle);
20486 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
20487 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20488 FormatStyle::SIS_OnlyFirstIf;
20489 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20490 verifyFormat("void f(bool b)\n"
20491 " {\n"
20492 " if (b)\n"
20493 " {\n"
20494 " return;\n"
20495 " }\n"
20496 " }",
20497 BreakBeforeBraceShortIfs);
20498 verifyFormat("void f(bool b)\n"
20499 " {\n"
20500 " if (b) return;\n"
20501 " }",
20502 BreakBeforeBraceShortIfs);
20503 verifyFormat("void f(bool b)\n"
20504 " {\n"
20505 " while (b)\n"
20506 " {\n"
20507 " return;\n"
20508 " }\n"
20509 " }",
20510 BreakBeforeBraceShortIfs);
20513 TEST_F(FormatTest, GNUBraceBreaking) {
20514 FormatStyle GNUBraceStyle = getLLVMStyle();
20515 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
20516 verifyFormat("namespace a\n"
20517 "{\n"
20518 "class A\n"
20519 "{\n"
20520 " void f()\n"
20521 " {\n"
20522 " int a;\n"
20523 " {\n"
20524 " int b;\n"
20525 " }\n"
20526 " if (true)\n"
20527 " {\n"
20528 " a();\n"
20529 " b();\n"
20530 " }\n"
20531 " }\n"
20532 " void g() { return; }\n"
20533 "}\n"
20534 "} // namespace a",
20535 GNUBraceStyle);
20537 verifyFormat("void f()\n"
20538 "{\n"
20539 " if (true)\n"
20540 " {\n"
20541 " a();\n"
20542 " }\n"
20543 " else if (false)\n"
20544 " {\n"
20545 " b();\n"
20546 " }\n"
20547 " else\n"
20548 " {\n"
20549 " c();\n"
20550 " }\n"
20551 "}",
20552 GNUBraceStyle);
20554 verifyFormat("void f()\n"
20555 "{\n"
20556 " for (int i = 0; i < 10; ++i)\n"
20557 " {\n"
20558 " a();\n"
20559 " }\n"
20560 " while (false)\n"
20561 " {\n"
20562 " b();\n"
20563 " }\n"
20564 " do\n"
20565 " {\n"
20566 " c();\n"
20567 " }\n"
20568 " while (false);\n"
20569 "}",
20570 GNUBraceStyle);
20572 verifyFormat("void f(int a)\n"
20573 "{\n"
20574 " switch (a)\n"
20575 " {\n"
20576 " case 0:\n"
20577 " break;\n"
20578 " case 1:\n"
20579 " {\n"
20580 " break;\n"
20581 " }\n"
20582 " case 2:\n"
20583 " {\n"
20584 " }\n"
20585 " break;\n"
20586 " default:\n"
20587 " break;\n"
20588 " }\n"
20589 "}",
20590 GNUBraceStyle);
20592 verifyFormat("enum X\n"
20593 "{\n"
20594 " Y = 0,\n"
20595 "}",
20596 GNUBraceStyle);
20598 verifyFormat("@interface BSApplicationController ()\n"
20599 "{\n"
20600 "@private\n"
20601 " id _extraIvar;\n"
20602 "}\n"
20603 "@end",
20604 GNUBraceStyle);
20606 verifyFormat("#ifdef _DEBUG\n"
20607 "int foo(int i = 0)\n"
20608 "#else\n"
20609 "int foo(int i = 5)\n"
20610 "#endif\n"
20611 "{\n"
20612 " return i;\n"
20613 "}",
20614 GNUBraceStyle);
20616 verifyFormat("void foo() {}\n"
20617 "void bar()\n"
20618 "#ifdef _DEBUG\n"
20619 "{\n"
20620 " foo();\n"
20621 "}\n"
20622 "#else\n"
20623 "{\n"
20624 "}\n"
20625 "#endif",
20626 GNUBraceStyle);
20628 verifyFormat("void foobar() { int i = 5; }\n"
20629 "#ifdef _DEBUG\n"
20630 "void bar() {}\n"
20631 "#else\n"
20632 "void bar() { foobar(); }\n"
20633 "#endif",
20634 GNUBraceStyle);
20637 TEST_F(FormatTest, WebKitBraceBreaking) {
20638 FormatStyle WebKitBraceStyle = getLLVMStyle();
20639 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
20640 WebKitBraceStyle.FixNamespaceComments = false;
20641 verifyFormat("namespace a {\n"
20642 "class A {\n"
20643 " void f()\n"
20644 " {\n"
20645 " if (true) {\n"
20646 " a();\n"
20647 " b();\n"
20648 " }\n"
20649 " }\n"
20650 " void g() { return; }\n"
20651 "};\n"
20652 "enum E {\n"
20653 " A,\n"
20654 " // foo\n"
20655 " B,\n"
20656 " C\n"
20657 "};\n"
20658 "struct B {\n"
20659 " int x;\n"
20660 "};\n"
20661 "}",
20662 WebKitBraceStyle);
20663 verifyFormat("struct S {\n"
20664 " int Type;\n"
20665 " union {\n"
20666 " int x;\n"
20667 " double y;\n"
20668 " } Value;\n"
20669 " class C {\n"
20670 " MyFavoriteType Value;\n"
20671 " } Class;\n"
20672 "};",
20673 WebKitBraceStyle);
20676 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
20677 verifyFormat("void f() {\n"
20678 " try {\n"
20679 " } catch (const Exception &e) {\n"
20680 " }\n"
20681 "}");
20684 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
20685 auto Style = getLLVMStyle();
20686 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20687 Style.AlignConsecutiveAssignments.Enabled = true;
20688 Style.AlignConsecutiveDeclarations.Enabled = true;
20689 verifyFormat("struct test demo[] = {\n"
20690 " {56, 23, \"hello\"},\n"
20691 " {-1, 93463, \"world\"},\n"
20692 " { 7, 5, \"!!\"}\n"
20693 "};",
20694 Style);
20696 verifyFormat("struct test demo[] = {\n"
20697 " {56, 23, \"hello\"}, // first line\n"
20698 " {-1, 93463, \"world\"}, // second line\n"
20699 " { 7, 5, \"!!\"} // third line\n"
20700 "};",
20701 Style);
20703 verifyFormat("struct test demo[4] = {\n"
20704 " { 56, 23, 21, \"oh\"}, // first line\n"
20705 " { -1, 93463, 22, \"my\"}, // second line\n"
20706 " { 7, 5, 1, \"goodness\"} // third line\n"
20707 " {234, 5, 1, \"gracious\"} // fourth line\n"
20708 "};",
20709 Style);
20711 verifyFormat("struct test demo[3] = {\n"
20712 " {56, 23, \"hello\"},\n"
20713 " {-1, 93463, \"world\"},\n"
20714 " { 7, 5, \"!!\"}\n"
20715 "};",
20716 Style);
20718 verifyFormat("struct test demo[3] = {\n"
20719 " {int{56}, 23, \"hello\"},\n"
20720 " {int{-1}, 93463, \"world\"},\n"
20721 " { int{7}, 5, \"!!\"}\n"
20722 "};",
20723 Style);
20725 verifyFormat("struct test demo[] = {\n"
20726 " {56, 23, \"hello\"},\n"
20727 " {-1, 93463, \"world\"},\n"
20728 " { 7, 5, \"!!\"},\n"
20729 "};",
20730 Style);
20732 verifyFormat("test demo[] = {\n"
20733 " {56, 23, \"hello\"},\n"
20734 " {-1, 93463, \"world\"},\n"
20735 " { 7, 5, \"!!\"},\n"
20736 "};",
20737 Style);
20739 verifyFormat("demo = std::array<struct test, 3>{\n"
20740 " test{56, 23, \"hello\"},\n"
20741 " test{-1, 93463, \"world\"},\n"
20742 " test{ 7, 5, \"!!\"},\n"
20743 "};",
20744 Style);
20746 verifyFormat("test demo[] = {\n"
20747 " {56, 23, \"hello\"},\n"
20748 "#if X\n"
20749 " {-1, 93463, \"world\"},\n"
20750 "#endif\n"
20751 " { 7, 5, \"!!\"}\n"
20752 "};",
20753 Style);
20755 verifyFormat(
20756 "test demo[] = {\n"
20757 " { 7, 23,\n"
20758 " \"hello world i am a very long line that really, in any\"\n"
20759 " \"just world, ought to be split over multiple lines\"},\n"
20760 " {-1, 93463, \"world\"},\n"
20761 " {56, 5, \"!!\"}\n"
20762 "};",
20763 Style);
20765 verifyFormat("return GradForUnaryCwise(g, {\n"
20766 " {{\"sign\"}, \"Sign\", "
20767 " {\"x\", \"dy\"}},\n"
20768 " { {\"dx\"}, \"Mul\", {\"dy\""
20769 ", \"sign\"}},\n"
20770 "});",
20771 Style);
20773 Style.Cpp11BracedListStyle = false;
20774 verifyFormat("struct test demo[] = {\n"
20775 " { 56, 23, \"hello\" },\n"
20776 " { -1, 93463, \"world\" },\n"
20777 " { 7, 5, \"!!\" }\n"
20778 "};",
20779 Style);
20780 Style.Cpp11BracedListStyle = true;
20782 Style.ColumnLimit = 0;
20783 verifyFormat(
20784 "test demo[] = {\n"
20785 " {56, 23, \"hello world i am a very long line that really, "
20786 "in any just world, ought to be split over multiple lines\"},\n"
20787 " {-1, 93463, "
20788 " \"world\"},\n"
20789 " { 7, 5, "
20790 " \"!!\"},\n"
20791 "};",
20792 "test demo[] = {{56, 23, \"hello world i am a very long line "
20793 "that really, in any just world, ought to be split over multiple "
20794 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20795 Style);
20797 Style.ColumnLimit = 80;
20798 verifyFormat("test demo[] = {\n"
20799 " {56, 23, /* a comment */ \"hello\"},\n"
20800 " {-1, 93463, \"world\"},\n"
20801 " { 7, 5, \"!!\"}\n"
20802 "};",
20803 Style);
20805 verifyFormat("test demo[] = {\n"
20806 " {56, 23, \"hello\"},\n"
20807 " {-1, 93463, \"world\" /* comment here */},\n"
20808 " { 7, 5, \"!!\"}\n"
20809 "};",
20810 Style);
20812 verifyFormat("test demo[] = {\n"
20813 " {56, /* a comment */ 23, \"hello\"},\n"
20814 " {-1, 93463, \"world\"},\n"
20815 " { 7, 5, \"!!\"}\n"
20816 "};",
20817 Style);
20819 Style.ColumnLimit = 20;
20820 verifyFormat("demo = std::array<\n"
20821 " struct test, 3>{\n"
20822 " test{\n"
20823 " 56, 23,\n"
20824 " \"hello \"\n"
20825 " \"world i \"\n"
20826 " \"am a very \"\n"
20827 " \"long line \"\n"
20828 " \"that \"\n"
20829 " \"really, \"\n"
20830 " \"in any \"\n"
20831 " \"just \"\n"
20832 " \"world, \"\n"
20833 " \"ought to \"\n"
20834 " \"be split \"\n"
20835 " \"over \"\n"
20836 " \"multiple \"\n"
20837 " \"lines\"},\n"
20838 " test{-1, 93463,\n"
20839 " \"world\"},\n"
20840 " test{ 7, 5,\n"
20841 " \"!!\" },\n"
20842 "};",
20843 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
20844 "i am a very long line that really, in any just world, ought "
20845 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
20846 "test{7, 5, \"!!\"},};",
20847 Style);
20848 // This caused a core dump by enabling Alignment in the LLVMStyle globally
20849 Style = getLLVMStyleWithColumns(50);
20850 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20851 verifyFormat("static A x = {\n"
20852 " {{init1, init2, init3, init4},\n"
20853 " {init1, init2, init3, init4}}\n"
20854 "};",
20855 Style);
20856 // TODO: Fix the indentations below when this option is fully functional.
20857 verifyFormat("int a[][] = {\n"
20858 " {\n"
20859 " {0, 2}, //\n"
20860 " {1, 2} //\n"
20861 " }\n"
20862 "};",
20863 Style);
20864 Style.ColumnLimit = 100;
20865 verifyFormat(
20866 "test demo[] = {\n"
20867 " {56, 23,\n"
20868 " \"hello world i am a very long line that really, in any just world"
20869 ", ought to be split over \"\n"
20870 " \"multiple lines\" },\n"
20871 " {-1, 93463, \"world\"},\n"
20872 " { 7, 5, \"!!\"},\n"
20873 "};",
20874 "test demo[] = {{56, 23, \"hello world i am a very long line "
20875 "that really, in any just world, ought to be split over multiple "
20876 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20877 Style);
20879 Style = getLLVMStyleWithColumns(50);
20880 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
20881 verifyFormat("struct test demo[] = {\n"
20882 " {56, 23, \"hello\"},\n"
20883 " {-1, 93463, \"world\"},\n"
20884 " { 7, 5, \"!!\"}\n"
20885 "};\n"
20886 "static A x = {\n"
20887 " {{init1, init2, init3, init4},\n"
20888 " {init1, init2, init3, init4}}\n"
20889 "};",
20890 Style);
20891 Style.ColumnLimit = 100;
20892 Style.AlignConsecutiveAssignments.AcrossComments = true;
20893 Style.AlignConsecutiveDeclarations.AcrossComments = true;
20894 verifyFormat("struct test demo[] = {\n"
20895 " {56, 23, \"hello\"},\n"
20896 " {-1, 93463, \"world\"},\n"
20897 " { 7, 5, \"!!\"}\n"
20898 "};\n"
20899 "struct test demo[4] = {\n"
20900 " { 56, 23, 21, \"oh\"}, // first line\n"
20901 " { -1, 93463, 22, \"my\"}, // second line\n"
20902 " { 7, 5, 1, \"goodness\"} // third line\n"
20903 " {234, 5, 1, \"gracious\"} // fourth line\n"
20904 "};",
20905 Style);
20906 verifyFormat(
20907 "test demo[] = {\n"
20908 " {56,\n"
20909 " \"hello world i am a very long line that really, in any just world"
20910 ", ought to be split over \"\n"
20911 " \"multiple lines\", 23},\n"
20912 " {-1, \"world\", 93463},\n"
20913 " { 7, \"!!\", 5},\n"
20914 "};",
20915 "test demo[] = {{56, \"hello world i am a very long line "
20916 "that really, in any just world, ought to be split over multiple "
20917 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
20918 Style);
20921 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
20922 auto Style = getLLVMStyle();
20923 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
20924 /* FIXME: This case gets misformatted.
20925 verifyFormat("auto foo = Items{\n"
20926 " Section{0, bar(), },\n"
20927 " Section{1, boo() }\n"
20928 "};",
20929 Style);
20931 verifyFormat("auto foo = Items{\n"
20932 " Section{\n"
20933 " 0, bar(),\n"
20934 " }\n"
20935 "};",
20936 Style);
20937 verifyFormat("struct test demo[] = {\n"
20938 " {56, 23, \"hello\"},\n"
20939 " {-1, 93463, \"world\"},\n"
20940 " {7, 5, \"!!\" }\n"
20941 "};",
20942 Style);
20943 verifyFormat("struct test demo[] = {\n"
20944 " {56, 23, \"hello\"}, // first line\n"
20945 " {-1, 93463, \"world\"}, // second line\n"
20946 " {7, 5, \"!!\" } // third line\n"
20947 "};",
20948 Style);
20949 verifyFormat("struct test demo[4] = {\n"
20950 " {56, 23, 21, \"oh\" }, // first line\n"
20951 " {-1, 93463, 22, \"my\" }, // second line\n"
20952 " {7, 5, 1, \"goodness\"} // third line\n"
20953 " {234, 5, 1, \"gracious\"} // fourth line\n"
20954 "};",
20955 Style);
20956 verifyFormat("struct test demo[3] = {\n"
20957 " {56, 23, \"hello\"},\n"
20958 " {-1, 93463, \"world\"},\n"
20959 " {7, 5, \"!!\" }\n"
20960 "};",
20961 Style);
20963 verifyFormat("struct test demo[3] = {\n"
20964 " {int{56}, 23, \"hello\"},\n"
20965 " {int{-1}, 93463, \"world\"},\n"
20966 " {int{7}, 5, \"!!\" }\n"
20967 "};",
20968 Style);
20969 verifyFormat("struct test demo[] = {\n"
20970 " {56, 23, \"hello\"},\n"
20971 " {-1, 93463, \"world\"},\n"
20972 " {7, 5, \"!!\" },\n"
20973 "};",
20974 Style);
20975 verifyFormat("test demo[] = {\n"
20976 " {56, 23, \"hello\"},\n"
20977 " {-1, 93463, \"world\"},\n"
20978 " {7, 5, \"!!\" },\n"
20979 "};",
20980 Style);
20981 verifyFormat("demo = std::array<struct test, 3>{\n"
20982 " test{56, 23, \"hello\"},\n"
20983 " test{-1, 93463, \"world\"},\n"
20984 " test{7, 5, \"!!\" },\n"
20985 "};",
20986 Style);
20987 verifyFormat("test demo[] = {\n"
20988 " {56, 23, \"hello\"},\n"
20989 "#if X\n"
20990 " {-1, 93463, \"world\"},\n"
20991 "#endif\n"
20992 " {7, 5, \"!!\" }\n"
20993 "};",
20994 Style);
20995 verifyFormat(
20996 "test demo[] = {\n"
20997 " {7, 23,\n"
20998 " \"hello world i am a very long line that really, in any\"\n"
20999 " \"just world, ought to be split over multiple lines\"},\n"
21000 " {-1, 93463, \"world\" },\n"
21001 " {56, 5, \"!!\" }\n"
21002 "};",
21003 Style);
21005 verifyFormat("return GradForUnaryCwise(g, {\n"
21006 " {{\"sign\"}, \"Sign\", {\"x\", "
21007 "\"dy\"} },\n"
21008 " {{\"dx\"}, \"Mul\", "
21009 "{\"dy\", \"sign\"}},\n"
21010 "});",
21011 Style);
21013 Style.Cpp11BracedListStyle = false;
21014 verifyFormat("struct test demo[] = {\n"
21015 " { 56, 23, \"hello\" },\n"
21016 " { -1, 93463, \"world\" },\n"
21017 " { 7, 5, \"!!\" }\n"
21018 "};",
21019 Style);
21020 Style.Cpp11BracedListStyle = true;
21022 Style.ColumnLimit = 0;
21023 verifyFormat(
21024 "test demo[] = {\n"
21025 " {56, 23, \"hello world i am a very long line that really, in any "
21026 "just world, ought to be split over multiple lines\"},\n"
21027 " {-1, 93463, \"world\" "
21028 " },\n"
21029 " {7, 5, \"!!\" "
21030 " },\n"
21031 "};",
21032 "test demo[] = {{56, 23, \"hello world i am a very long line "
21033 "that really, in any just world, ought to be split over multiple "
21034 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21035 Style);
21037 Style.ColumnLimit = 80;
21038 verifyFormat("test demo[] = {\n"
21039 " {56, 23, /* a comment */ \"hello\"},\n"
21040 " {-1, 93463, \"world\" },\n"
21041 " {7, 5, \"!!\" }\n"
21042 "};",
21043 Style);
21045 verifyFormat("test demo[] = {\n"
21046 " {56, 23, \"hello\" },\n"
21047 " {-1, 93463, \"world\" /* comment here */},\n"
21048 " {7, 5, \"!!\" }\n"
21049 "};",
21050 Style);
21052 verifyFormat("test demo[] = {\n"
21053 " {56, /* a comment */ 23, \"hello\"},\n"
21054 " {-1, 93463, \"world\"},\n"
21055 " {7, 5, \"!!\" }\n"
21056 "};",
21057 Style);
21059 Style.ColumnLimit = 20;
21060 // FIXME: unstable test case
21061 EXPECT_EQ(
21062 "demo = std::array<\n"
21063 " struct test, 3>{\n"
21064 " test{\n"
21065 " 56, 23,\n"
21066 " \"hello \"\n"
21067 " \"world i \"\n"
21068 " \"am a very \"\n"
21069 " \"long line \"\n"
21070 " \"that \"\n"
21071 " \"really, \"\n"
21072 " \"in any \"\n"
21073 " \"just \"\n"
21074 " \"world, \"\n"
21075 " \"ought to \"\n"
21076 " \"be split \"\n"
21077 " \"over \"\n"
21078 " \"multiple \"\n"
21079 " \"lines\"},\n"
21080 " test{-1, 93463,\n"
21081 " \"world\"},\n"
21082 " test{7, 5,\n"
21083 " \"!!\" },\n"
21084 "};",
21085 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21086 "i am a very long line that really, in any just world, ought "
21087 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21088 "test{7, 5, \"!!\"},};",
21089 Style));
21091 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21092 Style = getLLVMStyleWithColumns(50);
21093 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21094 verifyFormat("static A x = {\n"
21095 " {{init1, init2, init3, init4},\n"
21096 " {init1, init2, init3, init4}}\n"
21097 "};",
21098 Style);
21099 Style.ColumnLimit = 100;
21100 verifyFormat(
21101 "test demo[] = {\n"
21102 " {56, 23,\n"
21103 " \"hello world i am a very long line that really, in any just world"
21104 ", ought to be split over \"\n"
21105 " \"multiple lines\" },\n"
21106 " {-1, 93463, \"world\"},\n"
21107 " {7, 5, \"!!\" },\n"
21108 "};",
21109 "test demo[] = {{56, 23, \"hello world i am a very long line "
21110 "that really, in any just world, ought to be split over multiple "
21111 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21112 Style);
21115 TEST_F(FormatTest, UnderstandsPragmas) {
21116 verifyFormat("#pragma omp reduction(| : var)");
21117 verifyFormat("#pragma omp reduction(+ : var)");
21119 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21120 "(including parentheses).",
21121 "#pragma mark Any non-hyphenated or hyphenated string "
21122 "(including parentheses).");
21124 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21125 "(including parentheses).",
21126 "#pragma mark Any non-hyphenated or hyphenated string "
21127 "(including parentheses).");
21129 verifyFormat("#pragma comment(linker, \\\n"
21130 " \"argument\" \\\n"
21131 " \"argument\"",
21132 "#pragma comment(linker, \\\n"
21133 " \"argument\" \\\n"
21134 " \"argument\"",
21135 getStyleWithColumns(
21136 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32));
21139 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
21140 verifyFormat("#pragma omp target map(to : var)");
21141 verifyFormat("#pragma omp target map(to : var[ : N])");
21142 verifyFormat("#pragma omp target map(to : var[0 : N])");
21143 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
21145 verifyFormat(
21146 "#pragma omp target \\\n"
21147 " reduction(+ : var) \\\n"
21148 " map(to : A[0 : N]) \\\n"
21149 " map(to : B[0 : N]) \\\n"
21150 " map(from : C[0 : N]) \\\n"
21151 " firstprivate(i) \\\n"
21152 " firstprivate(j) \\\n"
21153 " firstprivate(k)",
21154 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
21155 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
21156 getLLVMStyleWithColumns(26));
21159 TEST_F(FormatTest, UnderstandPragmaOption) {
21160 verifyFormat("#pragma option -C -A");
21162 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
21165 TEST_F(FormatTest, UnderstandPragmaRegion) {
21166 auto Style = getLLVMStyleWithColumns(0);
21167 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
21168 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
21171 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
21172 FormatStyle Style = getLLVMStyleWithColumns(20);
21174 // See PR41213
21175 verifyFormat("/*\n"
21176 " *\t9012345\n"
21177 " * /8901\n"
21178 " */",
21179 "/*\n"
21180 " *\t9012345 /8901\n"
21181 " */",
21182 Style);
21183 verifyFormat("/*\n"
21184 " *345678\n"
21185 " *\t/8901\n"
21186 " */",
21187 "/*\n"
21188 " *345678\t/8901\n"
21189 " */",
21190 Style);
21192 verifyFormat("int a; // the\n"
21193 " // comment",
21194 Style);
21195 verifyNoChange("int a; /* first line\n"
21196 " * second\n"
21197 " * line third\n"
21198 " * line\n"
21199 " */",
21200 Style);
21201 verifyFormat("int a; // first line\n"
21202 " // second\n"
21203 " // line third\n"
21204 " // line",
21205 "int a; // first line\n"
21206 " // second line\n"
21207 " // third line",
21208 Style);
21210 Style.PenaltyExcessCharacter = 90;
21211 verifyFormat("int a; // the comment", Style);
21212 verifyFormat("int a; // the comment\n"
21213 " // aaa",
21214 "int a; // the comment aaa", Style);
21215 verifyNoChange("int a; /* first line\n"
21216 " * second line\n"
21217 " * third line\n"
21218 " */",
21219 Style);
21220 verifyFormat("int a; // first line\n"
21221 " // second line\n"
21222 " // third line",
21223 Style);
21224 // FIXME: Investigate why this is not getting the same layout as the test
21225 // above.
21226 verifyFormat("int a; /* first line\n"
21227 " * second line\n"
21228 " * third line\n"
21229 " */",
21230 "int a; /* first line second line third line"
21231 "\n*/",
21232 Style);
21234 verifyFormat("// foo bar baz bazfoo\n"
21235 "// foo bar foo bar",
21236 "// foo bar baz bazfoo\n"
21237 "// foo bar foo bar",
21238 Style);
21239 verifyFormat("// foo bar baz bazfoo\n"
21240 "// foo bar foo bar",
21241 "// foo bar baz bazfoo\n"
21242 "// foo bar foo bar",
21243 Style);
21245 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
21246 // next one.
21247 verifyFormat("// foo bar baz bazfoo\n"
21248 "// bar foo bar",
21249 "// foo bar baz bazfoo bar\n"
21250 "// foo bar",
21251 Style);
21253 // FIXME: unstable test case
21254 EXPECT_EQ("// foo bar baz bazfoo\n"
21255 "// foo bar baz bazfoo\n"
21256 "// bar foo bar",
21257 format("// foo bar baz bazfoo\n"
21258 "// foo bar baz bazfoo bar\n"
21259 "// foo bar",
21260 Style));
21262 // FIXME: unstable test case
21263 EXPECT_EQ("// foo bar baz bazfoo\n"
21264 "// foo bar baz bazfoo\n"
21265 "// bar foo bar",
21266 format("// foo bar baz bazfoo\n"
21267 "// foo bar baz bazfoo bar\n"
21268 "// foo bar",
21269 Style));
21271 // Make sure we do not keep protruding characters if strict mode reflow is
21272 // cheaper than keeping protruding characters.
21273 Style.ColumnLimit = 21;
21274 verifyFormat("// foo foo foo foo\n"
21275 "// foo foo foo foo\n"
21276 "// foo foo foo foo",
21277 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
21279 verifyFormat("int a = /* long block\n"
21280 " comment */\n"
21281 " 42;",
21282 "int a = /* long block comment */ 42;", Style);
21285 TEST_F(FormatTest, BreakPenaltyAfterLParen) {
21286 FormatStyle Style = getLLVMStyle();
21287 Style.ColumnLimit = 8;
21288 Style.PenaltyExcessCharacter = 15;
21289 verifyFormat("int foo(\n"
21290 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
21291 Style);
21292 Style.PenaltyBreakOpenParenthesis = 200;
21293 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
21294 "int foo(\n"
21295 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
21296 Style);
21299 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
21300 FormatStyle Style = getLLVMStyle();
21301 Style.ColumnLimit = 5;
21302 Style.PenaltyExcessCharacter = 150;
21303 verifyFormat("foo((\n"
21304 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
21306 Style);
21307 Style.PenaltyBreakOpenParenthesis = 100000;
21308 verifyFormat("foo((int)\n"
21309 " aaaaaaaaaaaaaaaaaaaaaaaa);",
21310 "foo((\n"
21311 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
21312 Style);
21315 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
21316 FormatStyle Style = getLLVMStyle();
21317 Style.ColumnLimit = 4;
21318 Style.PenaltyExcessCharacter = 100;
21319 verifyFormat("for (\n"
21320 " int iiiiiiiiiiiiiiiii =\n"
21321 " 0;\n"
21322 " iiiiiiiiiiiiiiiii <\n"
21323 " 2;\n"
21324 " iiiiiiiiiiiiiiiii++) {\n"
21325 "}",
21327 Style);
21328 Style.PenaltyBreakOpenParenthesis = 1250;
21329 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
21330 " 0;\n"
21331 " iiiiiiiiiiiiiiiii <\n"
21332 " 2;\n"
21333 " iiiiiiiiiiiiiiiii++) {\n"
21334 "}",
21335 "for (\n"
21336 " int iiiiiiiiiiiiiiiii =\n"
21337 " 0;\n"
21338 " iiiiiiiiiiiiiiiii <\n"
21339 " 2;\n"
21340 " iiiiiiiiiiiiiiiii++) {\n"
21341 "}",
21342 Style);
21345 TEST_F(FormatTest, WorksFor8bitEncodings) {
21346 // FIXME: unstable test case
21347 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
21348 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
21349 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
21350 "\"\xef\xee\xf0\xf3...\"",
21351 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
21352 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
21353 "\xef\xee\xf0\xf3...\"",
21354 getLLVMStyleWithColumns(12)));
21357 TEST_F(FormatTest, HandlesUTF8BOM) {
21358 verifyFormat("\xef\xbb\xbf");
21359 verifyFormat("\xef\xbb\xbf#include <iostream>");
21360 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
21363 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
21364 #if !defined(_MSC_VER)
21366 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
21367 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
21368 getLLVMStyleWithColumns(35));
21369 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
21370 getLLVMStyleWithColumns(31));
21371 verifyFormat("// Однажды в студёную зимнюю пору...",
21372 getLLVMStyleWithColumns(36));
21373 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
21374 verifyFormat("/* Однажды в студёную зимнюю пору... */",
21375 getLLVMStyleWithColumns(39));
21376 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
21377 getLLVMStyleWithColumns(35));
21380 TEST_F(FormatTest, SplitsUTF8Strings) {
21381 // Non-printable characters' width is currently considered to be the length in
21382 // bytes in UTF8. The characters can be displayed in very different manner
21383 // (zero-width, single width with a substitution glyph, expanded to their code
21384 // (e.g. "<8d>"), so there's no single correct way to handle them.
21385 // FIXME: unstable test case
21386 EXPECT_EQ("\"aaaaÄ\"\n"
21387 "\"\xc2\x8d\";",
21388 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
21389 // FIXME: unstable test case
21390 EXPECT_EQ("\"aaaaaaaÄ\"\n"
21391 "\"\xc2\x8d\";",
21392 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
21393 // FIXME: unstable test case
21394 EXPECT_EQ("\"Однажды, в \"\n"
21395 "\"студёную \"\n"
21396 "\"зимнюю \"\n"
21397 "\"пору,\"",
21398 format("\"Однажды, в студёную зимнюю пору,\"",
21399 getLLVMStyleWithColumns(13)));
21400 // FIXME: unstable test case
21401 EXPECT_EQ(
21402 "\"一 二 三 \"\n"
21403 "\"四 五六 \"\n"
21404 "\"七 八 九 \"\n"
21405 "\"\"",
21406 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
21407 // FIXME: unstable test case
21408 EXPECT_EQ("\"\t\"\n"
21409 "\" \t\"\n"
21410 "\"三 四 \"\n"
21411 "\"\t\"\n"
21412 "\" \t\"\n"
21413 "\" \"\n"
21414 "\"八九十\tqq\"",
21415 format("\"\t \t三 四 五\t \t七 八九十\tqq\"",
21416 getLLVMStyleWithColumns(11)));
21418 // UTF8 character in an escape sequence.
21419 // FIXME: unstable test case
21420 EXPECT_EQ("\"aaaaaa\"\n"
21421 "\"\\\xC2\x8D\"",
21422 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
21425 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
21426 verifyFormat("const char *sssss =\n"
21427 " \"一二三四五六七八\\\n"
21428 " 九 十\";",
21429 "const char *sssss = \"一二三四五六七八\\\n"
21430 " 九 十\";",
21431 getLLVMStyleWithColumns(30));
21434 TEST_F(FormatTest, SplitsUTF8LineComments) {
21435 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
21436 verifyFormat("// Я из лесу\n"
21437 "// вышел; был\n"
21438 "// сильный\n"
21439 "// мороз.",
21440 "// Я из лесу вышел; был сильный мороз.",
21441 getLLVMStyleWithColumns(13));
21442 verifyFormat("// 一二三\n"
21443 "// 四五六七\n"
21444 "// 八 九\n"
21445 "// 十",
21446 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
21449 TEST_F(FormatTest, SplitsUTF8BlockComments) {
21450 verifyFormat("/* Гляжу,\n"
21451 " * поднимается\n"
21452 " * медленно в\n"
21453 " * гору\n"
21454 " * Лошадка,\n"
21455 " * везущая\n"
21456 " * хворосту\n"
21457 " * воз. */",
21458 "/* Гляжу, поднимается медленно в гору\n"
21459 " * Лошадка, везущая хворосту воз. */",
21460 getLLVMStyleWithColumns(13));
21461 verifyFormat("/* 一二三\n"
21462 " * 四五六七\n"
21463 " * 八 九\n"
21464 " * 十 */",
21465 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
21466 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
21467 " * 𝕓𝕪𝕥𝕖\n"
21468 " * 𝖀𝕿𝕱-𝟠 */",
21469 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
21472 #endif // _MSC_VER
21474 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
21475 FormatStyle Style = getLLVMStyle();
21477 Style.ConstructorInitializerIndentWidth = 4;
21478 verifyFormat(
21479 "SomeClass::Constructor()\n"
21480 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
21481 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
21482 Style);
21484 Style.ConstructorInitializerIndentWidth = 2;
21485 verifyFormat(
21486 "SomeClass::Constructor()\n"
21487 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
21488 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
21489 Style);
21491 Style.ConstructorInitializerIndentWidth = 0;
21492 verifyFormat(
21493 "SomeClass::Constructor()\n"
21494 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
21495 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
21496 Style);
21497 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
21498 verifyFormat(
21499 "SomeLongTemplateVariableName<\n"
21500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
21501 Style);
21502 verifyFormat("bool smaller = 1 < "
21503 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
21505 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
21506 Style);
21508 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
21509 verifyFormat("SomeClass::Constructor() :\n"
21510 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
21511 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
21512 Style);
21515 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
21516 FormatStyle Style = getLLVMStyle();
21517 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
21518 Style.ConstructorInitializerIndentWidth = 4;
21519 verifyFormat("SomeClass::Constructor()\n"
21520 " : a(a)\n"
21521 " , b(b)\n"
21522 " , c(c) {}",
21523 Style);
21524 verifyFormat("SomeClass::Constructor()\n"
21525 " : a(a) {}",
21526 Style);
21528 Style.ColumnLimit = 0;
21529 verifyFormat("SomeClass::Constructor()\n"
21530 " : a(a) {}",
21531 Style);
21532 verifyFormat("SomeClass::Constructor() noexcept\n"
21533 " : a(a) {}",
21534 Style);
21535 verifyFormat("SomeClass::Constructor()\n"
21536 " : a(a)\n"
21537 " , b(b)\n"
21538 " , c(c) {}",
21539 Style);
21540 verifyFormat("SomeClass::Constructor()\n"
21541 " : a(a) {\n"
21542 " foo();\n"
21543 " bar();\n"
21544 "}",
21545 Style);
21547 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
21548 verifyFormat("SomeClass::Constructor()\n"
21549 " : a(a)\n"
21550 " , b(b)\n"
21551 " , c(c) {\n}",
21552 Style);
21553 verifyFormat("SomeClass::Constructor()\n"
21554 " : a(a) {\n}",
21555 Style);
21557 Style.ColumnLimit = 80;
21558 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
21559 Style.ConstructorInitializerIndentWidth = 2;
21560 verifyFormat("SomeClass::Constructor()\n"
21561 " : a(a)\n"
21562 " , b(b)\n"
21563 " , c(c) {}",
21564 Style);
21566 Style.ConstructorInitializerIndentWidth = 0;
21567 verifyFormat("SomeClass::Constructor()\n"
21568 ": a(a)\n"
21569 ", b(b)\n"
21570 ", c(c) {}",
21571 Style);
21573 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
21574 Style.ConstructorInitializerIndentWidth = 4;
21575 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
21576 verifyFormat(
21577 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
21578 Style);
21579 verifyFormat(
21580 "SomeClass::Constructor()\n"
21581 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
21582 Style);
21583 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
21584 verifyFormat("SomeClass::Constructor()\n"
21585 " : aaaaaaaa(aaaaaaaa) {}",
21586 Style);
21587 verifyFormat("SomeClass::Constructor()\n"
21588 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
21589 Style);
21590 verifyFormat(
21591 "SomeClass::Constructor()\n"
21592 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
21593 Style);
21595 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
21596 Style.ConstructorInitializerIndentWidth = 4;
21597 Style.ColumnLimit = 60;
21598 verifyFormat("SomeClass::Constructor()\n"
21599 " : aaaaaaaa(aaaaaaaa)\n"
21600 " , aaaaaaaa(aaaaaaaa)\n"
21601 " , aaaaaaaa(aaaaaaaa) {}",
21602 Style);
21603 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
21604 verifyFormat("SomeClass::Constructor()\n"
21605 " : aaaaaaaa(aaaaaaaa)\n"
21606 " , aaaaaaaa(aaaaaaaa)\n"
21607 " , aaaaaaaa(aaaaaaaa) {}",
21608 Style);
21611 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
21612 FormatStyle Style = getLLVMStyle();
21613 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
21614 Style.ConstructorInitializerIndentWidth = 4;
21615 verifyFormat("SomeClass::Constructor()\n"
21616 " : a{a}\n"
21617 " , b{b} {}",
21618 Style);
21619 verifyFormat("SomeClass::Constructor()\n"
21620 " : a{a}\n"
21621 "#if CONDITION\n"
21622 " , b{b}\n"
21623 "#endif\n"
21624 "{\n}",
21625 Style);
21626 Style.ConstructorInitializerIndentWidth = 2;
21627 verifyFormat("SomeClass::Constructor()\n"
21628 "#if CONDITION\n"
21629 " : a{a}\n"
21630 "#endif\n"
21631 " , b{b}\n"
21632 " , c{c} {\n}",
21633 Style);
21634 Style.ConstructorInitializerIndentWidth = 0;
21635 verifyFormat("SomeClass::Constructor()\n"
21636 ": a{a}\n"
21637 "#ifdef CONDITION\n"
21638 ", b{b}\n"
21639 "#else\n"
21640 ", c{c}\n"
21641 "#endif\n"
21642 ", d{d} {\n}",
21643 Style);
21644 Style.ConstructorInitializerIndentWidth = 4;
21645 verifyFormat("SomeClass::Constructor()\n"
21646 " : a{a}\n"
21647 "#if WINDOWS\n"
21648 "#if DEBUG\n"
21649 " , b{0}\n"
21650 "#else\n"
21651 " , b{1}\n"
21652 "#endif\n"
21653 "#else\n"
21654 "#if DEBUG\n"
21655 " , b{2}\n"
21656 "#else\n"
21657 " , b{3}\n"
21658 "#endif\n"
21659 "#endif\n"
21660 "{\n}",
21661 Style);
21662 verifyFormat("SomeClass::Constructor()\n"
21663 " : a{a}\n"
21664 "#if WINDOWS\n"
21665 " , b{0}\n"
21666 "#if DEBUG\n"
21667 " , c{0}\n"
21668 "#else\n"
21669 " , c{1}\n"
21670 "#endif\n"
21671 "#else\n"
21672 "#if DEBUG\n"
21673 " , c{2}\n"
21674 "#else\n"
21675 " , c{3}\n"
21676 "#endif\n"
21677 " , b{1}\n"
21678 "#endif\n"
21679 "{\n}",
21680 Style);
21683 TEST_F(FormatTest, Destructors) {
21684 verifyFormat("void F(int &i) { i.~int(); }");
21685 verifyFormat("void F(int &i) { i->~int(); }");
21688 TEST_F(FormatTest, FormatsWithWebKitStyle) {
21689 FormatStyle Style = getWebKitStyle();
21691 // Don't indent in outer namespaces.
21692 verifyFormat("namespace outer {\n"
21693 "int i;\n"
21694 "namespace inner {\n"
21695 " int i;\n"
21696 "} // namespace inner\n"
21697 "} // namespace outer\n"
21698 "namespace other_outer {\n"
21699 "int i;\n"
21700 "}",
21701 Style);
21703 // Don't indent case labels.
21704 verifyFormat("switch (variable) {\n"
21705 "case 1:\n"
21706 "case 2:\n"
21707 " doSomething();\n"
21708 " break;\n"
21709 "default:\n"
21710 " ++variable;\n"
21711 "}",
21712 Style);
21714 // Wrap before binary operators.
21715 verifyFormat(
21716 "void f()\n"
21717 "{\n"
21718 " if (aaaaaaaaaaaaaaaa\n"
21719 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
21720 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
21721 " return;\n"
21722 "}",
21723 "void f() {\n"
21724 "if (aaaaaaaaaaaaaaaa\n"
21725 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
21726 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
21727 "return;\n"
21728 "}",
21729 Style);
21731 // Allow functions on a single line.
21732 verifyFormat("void f() { return; }", Style);
21734 // Allow empty blocks on a single line and insert a space in empty blocks.
21735 verifyFormat("void f() { }", "void f() {}", Style);
21736 verifyFormat("while (true) { }", "while (true) {}", Style);
21737 // However, don't merge non-empty short loops.
21738 verifyFormat("while (true) {\n"
21739 " continue;\n"
21740 "}",
21741 "while (true) { continue; }", Style);
21743 // Constructor initializers are formatted one per line with the "," on the
21744 // new line.
21745 verifyFormat("Constructor()\n"
21746 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
21747 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
21748 " aaaaaaaaaaaaaa)\n"
21749 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
21750 "{\n"
21751 "}",
21752 Style);
21753 verifyFormat("SomeClass::Constructor()\n"
21754 " : a(a)\n"
21755 "{\n"
21756 "}",
21757 Style);
21758 verifyFormat("SomeClass::Constructor()\n"
21759 " : a(a)\n"
21760 "{\n"
21761 "}",
21762 "SomeClass::Constructor():a(a){}", Style);
21763 verifyFormat("SomeClass::Constructor()\n"
21764 " : a(a)\n"
21765 " , b(b)\n"
21766 " , c(c)\n"
21767 "{\n"
21768 "}",
21769 Style);
21770 verifyFormat("SomeClass::Constructor()\n"
21771 " : a(a)\n"
21772 "{\n"
21773 " foo();\n"
21774 " bar();\n"
21775 "}",
21776 Style);
21778 // Access specifiers should be aligned left.
21779 verifyFormat("class C {\n"
21780 "public:\n"
21781 " int i;\n"
21782 "};",
21783 Style);
21785 // Do not align comments.
21786 verifyFormat("int a; // Do not\n"
21787 "double b; // align comments.",
21788 Style);
21790 // Do not align operands.
21791 verifyFormat("ASSERT(aaaa\n"
21792 " || bbbb);",
21793 "ASSERT ( aaaa\n||bbbb);", Style);
21795 // Accept input's line breaks.
21796 verifyFormat("if (aaaaaaaaaaaaaaa\n"
21797 " || bbbbbbbbbbbbbbb) {\n"
21798 " i++;\n"
21799 "}",
21800 "if (aaaaaaaaaaaaaaa\n"
21801 "|| bbbbbbbbbbbbbbb) { i++; }",
21802 Style);
21803 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
21804 " i++;\n"
21805 "}",
21806 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
21808 // Don't automatically break all macro definitions (llvm.org/PR17842).
21809 verifyFormat("#define aNumber 10", Style);
21810 // However, generally keep the line breaks that the user authored.
21811 verifyFormat("#define aNumber \\\n"
21812 " 10",
21813 "#define aNumber \\\n"
21814 " 10",
21815 Style);
21817 // Keep empty and one-element array literals on a single line.
21818 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
21819 " copyItems:YES];",
21820 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
21821 "copyItems:YES];",
21822 Style);
21823 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
21824 " copyItems:YES];",
21825 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
21826 " copyItems:YES];",
21827 Style);
21828 // FIXME: This does not seem right, there should be more indentation before
21829 // the array literal's entries. Nested blocks have the same problem.
21830 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
21831 " @\"a\",\n"
21832 " @\"a\"\n"
21833 "]\n"
21834 " copyItems:YES];",
21835 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
21836 " @\"a\",\n"
21837 " @\"a\"\n"
21838 " ]\n"
21839 " copyItems:YES];",
21840 Style);
21841 verifyFormat(
21842 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
21843 " copyItems:YES];",
21844 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
21845 " copyItems:YES];",
21846 Style);
21848 verifyFormat("[self.a b:c c:d];", Style);
21849 verifyFormat("[self.a b:c\n"
21850 " c:d];",
21851 "[self.a b:c\n"
21852 "c:d];",
21853 Style);
21856 TEST_F(FormatTest, FormatsLambdas) {
21857 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
21858 verifyFormat(
21859 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
21860 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
21861 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
21862 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
21863 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
21864 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
21865 verifyFormat("auto c = [a = [b = 42] {}] {};");
21866 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
21867 verifyFormat("int x = f(*+[] {});");
21868 verifyFormat("void f() {\n"
21869 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
21870 "}");
21871 verifyFormat("void f() {\n"
21872 " other(x.begin(), //\n"
21873 " x.end(), //\n"
21874 " [&](int, int) { return 1; });\n"
21875 "}");
21876 verifyFormat("void f() {\n"
21877 " other.other.other.other.other(\n"
21878 " x.begin(), x.end(),\n"
21879 " [something, rather](int, int, int, int, int, int, int) { "
21880 "return 1; });\n"
21881 "}");
21882 verifyFormat(
21883 "void f() {\n"
21884 " other.other.other.other.other(\n"
21885 " x.begin(), x.end(),\n"
21886 " [something, rather](int, int, int, int, int, int, int) {\n"
21887 " //\n"
21888 " });\n"
21889 "}");
21890 verifyFormat("SomeFunction([]() { // A cool function...\n"
21891 " return 43;\n"
21892 "});");
21893 verifyFormat("SomeFunction([]() {\n"
21894 "#define A a\n"
21895 " return 43;\n"
21896 "});",
21897 "SomeFunction([](){\n"
21898 "#define A a\n"
21899 "return 43;\n"
21900 "});");
21901 verifyFormat("void f() {\n"
21902 " SomeFunction([](decltype(x), A *a) {});\n"
21903 " SomeFunction([](typeof(x), A *a) {});\n"
21904 " SomeFunction([](_Atomic(x), A *a) {});\n"
21905 " SomeFunction([](__underlying_type(x), A *a) {});\n"
21906 "}");
21907 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21908 " [](const aaaaaaaaaa &a) { return a; });");
21909 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
21910 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
21911 "});");
21912 verifyFormat("Constructor()\n"
21913 " : Field([] { // comment\n"
21914 " int i;\n"
21915 " }) {}");
21916 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
21917 " return some_parameter.size();\n"
21918 "};");
21919 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
21920 " [](const string &s) { return s; };");
21921 verifyFormat("int i = aaaaaa ? 1 //\n"
21922 " : [] {\n"
21923 " return 2; //\n"
21924 " }();");
21925 verifyFormat("llvm::errs() << \"number of twos is \"\n"
21926 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
21927 " return x == 2; // force break\n"
21928 " });");
21929 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21930 " [=](int iiiiiiiiiiii) {\n"
21931 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
21932 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
21933 " });",
21934 getLLVMStyleWithColumns(60));
21936 verifyFormat("SomeFunction({[&] {\n"
21937 " // comment\n"
21938 " },\n"
21939 " [&] {\n"
21940 " // comment\n"
21941 " }});");
21942 verifyFormat("SomeFunction({[&] {\n"
21943 " // comment\n"
21944 "}});");
21945 verifyFormat(
21946 "virtual aaaaaaaaaaaaaaaa(\n"
21947 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
21948 " aaaaa aaaaaaaaa);");
21950 // Lambdas with return types.
21951 verifyFormat("int c = []() -> int { return 2; }();");
21952 verifyFormat("int c = []() -> int * { return 2; }();");
21953 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
21954 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
21955 verifyFormat("foo([]() noexcept -> int {});");
21956 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
21957 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
21958 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
21959 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
21960 verifyFormat("[a, a]() -> a<1> {};");
21961 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
21962 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
21963 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
21964 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
21965 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
21966 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
21967 verifyFormat("[]() -> foo<!5> { return {}; };");
21968 verifyFormat("[]() -> foo<~5> { return {}; };");
21969 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
21970 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
21971 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
21972 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
21973 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
21974 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
21975 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
21976 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
21977 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
21978 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
21979 verifyFormat("namespace bar {\n"
21980 "// broken:\n"
21981 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
21982 "} // namespace bar");
21983 verifyFormat("namespace bar {\n"
21984 "// broken:\n"
21985 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
21986 "} // namespace bar");
21987 verifyFormat("namespace bar {\n"
21988 "// broken:\n"
21989 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
21990 "} // namespace bar");
21991 verifyFormat("namespace bar {\n"
21992 "// broken:\n"
21993 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
21994 "} // namespace bar");
21995 verifyFormat("namespace bar {\n"
21996 "// broken:\n"
21997 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
21998 "} // namespace bar");
21999 verifyFormat("namespace bar {\n"
22000 "// broken:\n"
22001 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22002 "} // namespace bar");
22003 verifyFormat("namespace bar {\n"
22004 "// broken:\n"
22005 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22006 "} // namespace bar");
22007 verifyFormat("namespace bar {\n"
22008 "// broken:\n"
22009 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22010 "} // namespace bar");
22011 verifyFormat("namespace bar {\n"
22012 "// broken:\n"
22013 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22014 "} // namespace bar");
22015 verifyFormat("namespace bar {\n"
22016 "// broken:\n"
22017 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22018 "} // namespace bar");
22019 verifyFormat("namespace bar {\n"
22020 "// broken:\n"
22021 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22022 "} // namespace bar");
22023 verifyFormat("namespace bar {\n"
22024 "// broken:\n"
22025 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22026 "} // namespace bar");
22027 verifyFormat("namespace bar {\n"
22028 "// broken:\n"
22029 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22030 "} // namespace bar");
22031 verifyFormat("namespace bar {\n"
22032 "// broken:\n"
22033 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22034 "} // namespace bar");
22035 verifyFormat("namespace bar {\n"
22036 "// broken:\n"
22037 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22038 "} // namespace bar");
22039 verifyFormat("namespace bar {\n"
22040 "// broken:\n"
22041 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22042 "} // namespace bar");
22043 verifyFormat("namespace bar {\n"
22044 "// broken:\n"
22045 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22046 "} // namespace bar");
22047 verifyFormat("namespace bar {\n"
22048 "// broken:\n"
22049 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22050 "} // namespace bar");
22051 verifyFormat("[]() -> a<1> {};");
22052 verifyFormat("[]() -> a<1> { ; };");
22053 verifyFormat("[]() -> a<1> { ; }();");
22054 verifyFormat("[a, a]() -> a<true> {};");
22055 verifyFormat("[]() -> a<true> {};");
22056 verifyFormat("[]() -> a<true> { ; };");
22057 verifyFormat("[]() -> a<true> { ; }();");
22058 verifyFormat("[a, a]() -> a<false> {};");
22059 verifyFormat("[]() -> a<false> {};");
22060 verifyFormat("[]() -> a<false> { ; };");
22061 verifyFormat("[]() -> a<false> { ; }();");
22062 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22063 verifyFormat("namespace bar {\n"
22064 "auto foo{[]() -> foo<false> { ; }};\n"
22065 "} // namespace bar");
22066 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22067 " int j) -> int {\n"
22068 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22069 "};");
22070 verifyFormat(
22071 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22072 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22073 " return aaaaaaaaaaaaaaaaa;\n"
22074 " });",
22075 getLLVMStyleWithColumns(70));
22076 verifyFormat("[]() //\n"
22077 " -> int {\n"
22078 " return 1; //\n"
22079 "};");
22080 verifyFormat("[]() -> Void<T...> {};");
22081 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22082 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22083 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22084 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22085 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22086 verifyFormat("return int{[x = x]() { return x; }()};");
22088 // Lambdas with explicit template argument lists.
22089 verifyFormat(
22090 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22091 verifyFormat("auto L = []<class T>(T) {\n"
22092 " {\n"
22093 " f();\n"
22094 " g();\n"
22095 " }\n"
22096 "};");
22097 verifyFormat("auto L = []<class... T>(T...) {\n"
22098 " {\n"
22099 " f();\n"
22100 " g();\n"
22101 " }\n"
22102 "};");
22103 verifyFormat("auto L = []<typename... T>(T...) {\n"
22104 " {\n"
22105 " f();\n"
22106 " g();\n"
22107 " }\n"
22108 "};");
22109 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
22110 " {\n"
22111 " f();\n"
22112 " g();\n"
22113 " }\n"
22114 "};");
22115 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
22116 " {\n"
22117 " f();\n"
22118 " g();\n"
22119 " }\n"
22120 "};");
22121 verifyFormat("auto L = []<int... T>(T...) {\n"
22122 " {\n"
22123 " f();\n"
22124 " g();\n"
22125 " }\n"
22126 "};");
22127 verifyFormat("auto L = []<Foo... T>(T...) {\n"
22128 " {\n"
22129 " f();\n"
22130 " g();\n"
22131 " }\n"
22132 "};");
22134 // Lambdas that fit on a single line within an argument list are not forced
22135 // onto new lines.
22136 verifyFormat("SomeFunction([] {});");
22137 verifyFormat("SomeFunction(0, [] {});");
22138 verifyFormat("SomeFunction([] {}, 0);");
22139 verifyFormat("SomeFunction(0, [] {}, 0);");
22140 verifyFormat("SomeFunction([] { return 0; }, 0);");
22141 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
22142 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
22143 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
22144 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
22145 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
22146 // Exceeded column limit. We need to break.
22147 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
22148 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
22149 "return 0; }, b);");
22151 // Multiple multi-line lambdas in the same parentheses change indentation
22152 // rules. These lambdas are always forced to start on new lines.
22153 verifyFormat("SomeFunction(\n"
22154 " []() {\n"
22155 " //\n"
22156 " },\n"
22157 " []() {\n"
22158 " //\n"
22159 " });");
22161 // A multi-line lambda passed as arg0 is always pushed to the next line.
22162 verifyFormat("SomeFunction(\n"
22163 " [this] {\n"
22164 " //\n"
22165 " },\n"
22166 " 1);");
22168 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
22169 // the arg0 case above.
22170 auto Style = getGoogleStyle();
22171 Style.BinPackArguments = false;
22172 verifyFormat("SomeFunction(\n"
22173 " a,\n"
22174 " [this] {\n"
22175 " //\n"
22176 " },\n"
22177 " b);",
22178 Style);
22179 verifyFormat("SomeFunction(\n"
22180 " a,\n"
22181 " [this] {\n"
22182 " //\n"
22183 " },\n"
22184 " b);");
22186 // A lambda with a very long line forces arg0 to be pushed out irrespective of
22187 // the BinPackArguments value (as long as the code is wide enough).
22188 verifyFormat(
22189 "something->SomeFunction(\n"
22190 " a,\n"
22191 " [this] {\n"
22193 "D0000000000000000000000000000000000000000000000000000000000001();\n"
22194 " },\n"
22195 " b);");
22197 // A multi-line lambda is pulled up as long as the introducer fits on the
22198 // previous line and there are no further args.
22199 verifyFormat("function(1, [this, that] {\n"
22200 " //\n"
22201 "});");
22202 verifyFormat("function([this, that] {\n"
22203 " //\n"
22204 "});");
22205 // FIXME: this format is not ideal and we should consider forcing the first
22206 // arg onto its own line.
22207 verifyFormat("function(a, b, c, //\n"
22208 " d, [this, that] {\n"
22209 " //\n"
22210 " });");
22212 // Multiple lambdas are treated correctly even when there is a short arg0.
22213 verifyFormat("SomeFunction(\n"
22214 " 1,\n"
22215 " [this] {\n"
22216 " //\n"
22217 " },\n"
22218 " [this] {\n"
22219 " //\n"
22220 " },\n"
22221 " 1);");
22223 // More complex introducers.
22224 verifyFormat("return [i, args...] {};");
22226 // Not lambdas.
22227 verifyFormat("constexpr char hello[]{\"hello\"};");
22228 verifyFormat("double &operator[](int i) { return 0; }\n"
22229 "int i;");
22230 verifyFormat("std::unique_ptr<int[]> foo() {}");
22231 verifyFormat("int i = a[a][a]->f();");
22232 verifyFormat("int i = (*b)[a]->f();");
22234 // Other corner cases.
22235 verifyFormat("void f() {\n"
22236 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
22237 " );\n"
22238 "}");
22239 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
22241 // Lambdas created through weird macros.
22242 verifyFormat("void f() {\n"
22243 " MACRO((const AA &a) { return 1; });\n"
22244 " MACRO((AA &a) { return 1; });\n"
22245 "}");
22247 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
22248 " doo_dah();\n"
22249 " doo_dah();\n"
22250 " })) {\n"
22251 "}");
22252 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
22253 " doo_dah();\n"
22254 " doo_dah();\n"
22255 " })) {\n"
22256 "}");
22257 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
22258 " doo_dah();\n"
22259 " doo_dah();\n"
22260 " })) {\n"
22261 "}");
22262 verifyFormat("auto lambda = []() {\n"
22263 " int a = 2\n"
22264 "#if A\n"
22265 " + 2\n"
22266 "#endif\n"
22267 " ;\n"
22268 "};");
22270 // Lambdas with complex multiline introducers.
22271 verifyFormat(
22272 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22273 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
22274 " -> ::std::unordered_set<\n"
22275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
22276 " //\n"
22277 " });");
22279 FormatStyle DoNotMerge = getLLVMStyle();
22280 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
22281 verifyFormat("auto c = []() {\n"
22282 " return b;\n"
22283 "};",
22284 "auto c = []() { return b; };", DoNotMerge);
22285 verifyFormat("auto c = []() {\n"
22286 "};",
22287 " auto c = []() {};", DoNotMerge);
22289 FormatStyle MergeEmptyOnly = getLLVMStyle();
22290 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
22291 verifyFormat("auto c = []() {\n"
22292 " return b;\n"
22293 "};",
22294 "auto c = []() {\n"
22295 " return b;\n"
22296 " };",
22297 MergeEmptyOnly);
22298 verifyFormat("auto c = []() {};",
22299 "auto c = []() {\n"
22300 "};",
22301 MergeEmptyOnly);
22303 FormatStyle MergeInline = getLLVMStyle();
22304 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
22305 verifyFormat("auto c = []() {\n"
22306 " return b;\n"
22307 "};",
22308 "auto c = []() { return b; };", MergeInline);
22309 verifyFormat("function([]() { return b; })", MergeInline);
22310 verifyFormat("function([]() { return b; }, a)", MergeInline);
22311 verifyFormat("function(a, []() { return b; })", MergeInline);
22313 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
22314 // AllowShortLambdasOnASingleLine
22315 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
22316 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
22317 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
22318 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22319 FormatStyle::ShortLambdaStyle::SLS_None;
22320 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
22321 " []()\n"
22322 " {\n"
22323 " return 17;\n"
22324 " });",
22325 LLVMWithBeforeLambdaBody);
22326 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
22327 " []()\n"
22328 " {\n"
22329 " });",
22330 LLVMWithBeforeLambdaBody);
22331 verifyFormat("auto fct_SLS_None = []()\n"
22332 "{\n"
22333 " return 17;\n"
22334 "};",
22335 LLVMWithBeforeLambdaBody);
22336 verifyFormat("TwoNestedLambdas_SLS_None(\n"
22337 " []()\n"
22338 " {\n"
22339 " return Call(\n"
22340 " []()\n"
22341 " {\n"
22342 " return 17;\n"
22343 " });\n"
22344 " });",
22345 LLVMWithBeforeLambdaBody);
22346 verifyFormat("void Fct() {\n"
22347 " return {[]()\n"
22348 " {\n"
22349 " return 17;\n"
22350 " }};\n"
22351 "}",
22352 LLVMWithBeforeLambdaBody);
22354 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22355 FormatStyle::ShortLambdaStyle::SLS_Empty;
22356 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
22357 " []()\n"
22358 " {\n"
22359 " return 17;\n"
22360 " });",
22361 LLVMWithBeforeLambdaBody);
22362 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
22363 LLVMWithBeforeLambdaBody);
22364 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
22365 "ongFunctionName_SLS_Empty(\n"
22366 " []() {});",
22367 LLVMWithBeforeLambdaBody);
22368 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
22369 " []()\n"
22370 " {\n"
22371 " return 17;\n"
22372 " });",
22373 LLVMWithBeforeLambdaBody);
22374 verifyFormat("auto fct_SLS_Empty = []()\n"
22375 "{\n"
22376 " return 17;\n"
22377 "};",
22378 LLVMWithBeforeLambdaBody);
22379 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
22380 " []()\n"
22381 " {\n"
22382 " return Call([]() {});\n"
22383 " });",
22384 LLVMWithBeforeLambdaBody);
22385 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
22386 " []()\n"
22387 " {\n"
22388 " return Call([]() {});\n"
22389 " });",
22390 LLVMWithBeforeLambdaBody);
22391 verifyFormat(
22392 "FctWithLongLineInLambda_SLS_Empty(\n"
22393 " []()\n"
22394 " {\n"
22395 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22396 " AndShouldNotBeConsiderAsInline,\n"
22397 " LambdaBodyMustBeBreak);\n"
22398 " });",
22399 LLVMWithBeforeLambdaBody);
22401 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22402 FormatStyle::ShortLambdaStyle::SLS_Inline;
22403 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
22404 LLVMWithBeforeLambdaBody);
22405 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
22406 LLVMWithBeforeLambdaBody);
22407 verifyFormat("auto fct_SLS_Inline = []()\n"
22408 "{\n"
22409 " return 17;\n"
22410 "};",
22411 LLVMWithBeforeLambdaBody);
22412 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
22413 "17; }); });",
22414 LLVMWithBeforeLambdaBody);
22415 verifyFormat(
22416 "FctWithLongLineInLambda_SLS_Inline(\n"
22417 " []()\n"
22418 " {\n"
22419 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22420 " AndShouldNotBeConsiderAsInline,\n"
22421 " LambdaBodyMustBeBreak);\n"
22422 " });",
22423 LLVMWithBeforeLambdaBody);
22424 verifyFormat("FctWithMultipleParams_SLS_Inline("
22425 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
22426 " []() { return 17; });",
22427 LLVMWithBeforeLambdaBody);
22428 verifyFormat(
22429 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
22430 LLVMWithBeforeLambdaBody);
22432 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22433 FormatStyle::ShortLambdaStyle::SLS_All;
22434 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
22435 LLVMWithBeforeLambdaBody);
22436 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
22437 LLVMWithBeforeLambdaBody);
22438 verifyFormat("auto fct_SLS_All = []() { return 17; };",
22439 LLVMWithBeforeLambdaBody);
22440 verifyFormat("FctWithOneParam_SLS_All(\n"
22441 " []()\n"
22442 " {\n"
22443 " // A cool function...\n"
22444 " return 43;\n"
22445 " });",
22446 LLVMWithBeforeLambdaBody);
22447 verifyFormat("FctWithMultipleParams_SLS_All("
22448 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
22449 " []() { return 17; });",
22450 LLVMWithBeforeLambdaBody);
22451 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
22452 LLVMWithBeforeLambdaBody);
22453 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
22454 LLVMWithBeforeLambdaBody);
22455 verifyFormat(
22456 "FctWithLongLineInLambda_SLS_All(\n"
22457 " []()\n"
22458 " {\n"
22459 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22460 " AndShouldNotBeConsiderAsInline,\n"
22461 " LambdaBodyMustBeBreak);\n"
22462 " });",
22463 LLVMWithBeforeLambdaBody);
22464 verifyFormat(
22465 "auto fct_SLS_All = []()\n"
22466 "{\n"
22467 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22468 " AndShouldNotBeConsiderAsInline,\n"
22469 " LambdaBodyMustBeBreak);\n"
22470 "};",
22471 LLVMWithBeforeLambdaBody);
22472 LLVMWithBeforeLambdaBody.BinPackParameters = false;
22473 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
22474 LLVMWithBeforeLambdaBody);
22475 verifyFormat(
22476 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
22477 " FirstParam,\n"
22478 " SecondParam,\n"
22479 " ThirdParam,\n"
22480 " FourthParam);",
22481 LLVMWithBeforeLambdaBody);
22482 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
22483 " []() { return "
22484 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
22485 " FirstParam,\n"
22486 " SecondParam,\n"
22487 " ThirdParam,\n"
22488 " FourthParam);",
22489 LLVMWithBeforeLambdaBody);
22490 verifyFormat(
22491 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
22492 " SecondParam,\n"
22493 " ThirdParam,\n"
22494 " FourthParam,\n"
22495 " []() { return SomeValueNotSoLong; });",
22496 LLVMWithBeforeLambdaBody);
22497 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
22498 " []()\n"
22499 " {\n"
22500 " return "
22501 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
22502 "eConsiderAsInline;\n"
22503 " });",
22504 LLVMWithBeforeLambdaBody);
22505 verifyFormat(
22506 "FctWithLongLineInLambda_SLS_All(\n"
22507 " []()\n"
22508 " {\n"
22509 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22510 " AndShouldNotBeConsiderAsInline,\n"
22511 " LambdaBodyMustBeBreak);\n"
22512 " });",
22513 LLVMWithBeforeLambdaBody);
22514 verifyFormat("FctWithTwoParams_SLS_All(\n"
22515 " []()\n"
22516 " {\n"
22517 " // A cool function...\n"
22518 " return 43;\n"
22519 " },\n"
22520 " 87);",
22521 LLVMWithBeforeLambdaBody);
22522 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
22523 LLVMWithBeforeLambdaBody);
22524 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
22525 LLVMWithBeforeLambdaBody);
22526 verifyFormat(
22527 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
22528 LLVMWithBeforeLambdaBody);
22529 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
22530 "}); }, x);",
22531 LLVMWithBeforeLambdaBody);
22532 verifyFormat("TwoNestedLambdas_SLS_All(\n"
22533 " []()\n"
22534 " {\n"
22535 " // A cool function...\n"
22536 " return Call([]() { return 17; });\n"
22537 " });",
22538 LLVMWithBeforeLambdaBody);
22539 verifyFormat("TwoNestedLambdas_SLS_All(\n"
22540 " []()\n"
22541 " {\n"
22542 " return Call(\n"
22543 " []()\n"
22544 " {\n"
22545 " // A cool function...\n"
22546 " return 17;\n"
22547 " });\n"
22548 " });",
22549 LLVMWithBeforeLambdaBody);
22551 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22552 FormatStyle::ShortLambdaStyle::SLS_None;
22554 verifyFormat("auto select = [this]() -> const Library::Object *\n"
22555 "{\n"
22556 " return MyAssignment::SelectFromList(this);\n"
22557 "};",
22558 LLVMWithBeforeLambdaBody);
22560 verifyFormat("auto select = [this]() -> const Library::Object &\n"
22561 "{\n"
22562 " return MyAssignment::SelectFromList(this);\n"
22563 "};",
22564 LLVMWithBeforeLambdaBody);
22566 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
22567 "{\n"
22568 " return MyAssignment::SelectFromList(this);\n"
22569 "};",
22570 LLVMWithBeforeLambdaBody);
22572 verifyFormat("namespace test {\n"
22573 "class Test {\n"
22574 "public:\n"
22575 " Test() = default;\n"
22576 "};\n"
22577 "} // namespace test",
22578 LLVMWithBeforeLambdaBody);
22580 // Lambdas with different indentation styles.
22581 Style = getLLVMStyleWithColumns(60);
22582 verifyFormat("Result doSomething(Promise promise) {\n"
22583 " return promise.then(\n"
22584 " [this, obj = std::move(s)](int bar) mutable {\n"
22585 " return someObject.startAsyncAction().then(\n"
22586 " [this, &obj](Result result) mutable {\n"
22587 " result.processMore();\n"
22588 " });\n"
22589 " });\n"
22590 "}",
22591 Style);
22592 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
22593 verifyFormat("Result doSomething(Promise promise) {\n"
22594 " return promise.then(\n"
22595 " [this, obj = std::move(s)](int bar) mutable {\n"
22596 " return obj.startAsyncAction().then(\n"
22597 " [this, &obj](Result result) mutable {\n"
22598 " result.processMore();\n"
22599 " });\n"
22600 " });\n"
22601 "}",
22602 Style);
22603 verifyFormat("Result doSomething(Promise promise) {\n"
22604 " return promise.then([this, obj = std::move(s)] {\n"
22605 " return obj.startAsyncAction().then(\n"
22606 " [this, &obj](Result result) mutable {\n"
22607 " result.processMore();\n"
22608 " });\n"
22609 " });\n"
22610 "}",
22611 Style);
22612 verifyFormat("void test() {\n"
22613 " ([]() -> auto {\n"
22614 " int b = 32;\n"
22615 " return 3;\n"
22616 " }).foo();\n"
22617 "}",
22618 Style);
22619 verifyFormat("void test() {\n"
22620 " []() -> auto {\n"
22621 " int b = 32;\n"
22622 " return 3;\n"
22623 " }\n"
22624 "}",
22625 Style);
22626 verifyFormat("void test() {\n"
22627 " std::sort(v.begin(), v.end(),\n"
22628 " [](const auto &foo, const auto &bar) {\n"
22629 " return foo.baz < bar.baz;\n"
22630 " });\n"
22631 "};",
22632 Style);
22633 verifyFormat("void test() {\n"
22634 " (\n"
22635 " []() -> auto {\n"
22636 " int b = 32;\n"
22637 " return 3;\n"
22638 " }, foo, bar)\n"
22639 " .foo();\n"
22640 "}",
22641 Style);
22642 verifyFormat("void test() {\n"
22643 " ([]() -> auto {\n"
22644 " int b = 32;\n"
22645 " return 3;\n"
22646 " })\n"
22647 " .foo()\n"
22648 " .bar();\n"
22649 "}",
22650 Style);
22651 verifyFormat("#define A \\\n"
22652 " [] { \\\n"
22653 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
22654 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
22655 " }",
22656 Style);
22657 verifyFormat("#define SORT(v) \\\n"
22658 " std::sort(v.begin(), v.end(), \\\n"
22659 " [](const auto &foo, const auto &bar) { \\\n"
22660 " return foo.baz < bar.baz; \\\n"
22661 " });",
22662 Style);
22663 verifyFormat("void foo() {\n"
22664 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
22665 " auto f = e(d);\n"
22666 " return f;\n"
22667 " })));\n"
22668 "}",
22669 Style);
22670 verifyFormat("void foo() {\n"
22671 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
22672 " auto f = e(foo, [&] {\n"
22673 " auto g = h();\n"
22674 " return g;\n"
22675 " }, qux, [&] -> Bar {\n"
22676 " auto i = j();\n"
22677 " return i;\n"
22678 " });\n"
22679 " return f;\n"
22680 " })));\n"
22681 "}",
22682 Style);
22683 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22684 " AnotherLongClassName baz)\n"
22685 " : baz{baz}, func{[&] {\n"
22686 " auto qux = bar;\n"
22687 " return aFunkyFunctionCall(qux);\n"
22688 " }} {}",
22689 Style);
22690 verifyFormat("void foo() {\n"
22691 " class Foo {\n"
22692 " public:\n"
22693 " Foo()\n"
22694 " : qux{[](int quux) {\n"
22695 " auto tmp = quux;\n"
22696 " return tmp;\n"
22697 " }} {}\n"
22698 "\n"
22699 " private:\n"
22700 " std::function<void(int quux)> qux;\n"
22701 " };\n"
22702 "}",
22703 Style);
22704 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22705 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22706 " AnotherLongClassName baz) :\n"
22707 " baz{baz}, func{[&] {\n"
22708 " auto qux = bar;\n"
22709 " return aFunkyFunctionCall(qux);\n"
22710 " }} {}",
22711 Style);
22712 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
22713 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22714 " AnotherLongClassName baz) :\n"
22715 " baz{baz},\n"
22716 " func{[&] {\n"
22717 " auto qux = bar;\n"
22718 " return aFunkyFunctionCall(qux);\n"
22719 " }} {}",
22720 Style);
22721 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
22722 // FIXME: The following test should pass, but fails at the time of writing.
22723 #if 0
22724 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
22725 // doesn't force an initial line break, even if lambdas span multiple lines.
22726 verifyFormat("void foo() {\n"
22727 " aFunction(\n"
22728 " [](d) -> Foo {\n"
22729 " auto f = e(d);\n"
22730 " return f;\n"
22731 " }, foo, Bar{}, [] {\n"
22732 " auto g = h();\n"
22733 " return g;\n"
22734 " }, baz);\n"
22735 "}",
22736 Style);
22737 #endif
22738 // A long non-lambda argument forces arguments to span multiple lines and thus
22739 // forces an initial line break when using AlwaysBreak.
22740 verifyFormat("void foo() {\n"
22741 " aFunction(\n"
22742 " 1,\n"
22743 " [](d) -> Foo {\n"
22744 " auto f = e(d);\n"
22745 " return f;\n"
22746 " }, foo, Bar{},\n"
22747 " [] {\n"
22748 " auto g = h();\n"
22749 " return g;\n"
22750 " }, bazzzzz,\n"
22751 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
22752 "}",
22753 Style);
22754 Style.BinPackArguments = false;
22755 verifyFormat("void foo() {\n"
22756 " aFunction(\n"
22757 " 1,\n"
22758 " [](d) -> Foo {\n"
22759 " auto f = e(d);\n"
22760 " return f;\n"
22761 " },\n"
22762 " foo,\n"
22763 " Bar{},\n"
22764 " [] {\n"
22765 " auto g = h();\n"
22766 " return g;\n"
22767 " },\n"
22768 " bazzzzz,\n"
22769 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
22770 "}",
22771 Style);
22772 Style.BinPackArguments = true;
22773 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
22774 Style.BraceWrapping.BeforeLambdaBody = true;
22775 verifyFormat("void foo() {\n"
22776 " aFunction(\n"
22777 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
22778 " {\n"
22779 " auto f = e(\n"
22780 " [&]\n"
22781 " {\n"
22782 " auto g = h();\n"
22783 " return g;\n"
22784 " }, qux, [&] -> Bar\n"
22785 " {\n"
22786 " auto i = j();\n"
22787 " return i;\n"
22788 " });\n"
22789 " return f;\n"
22790 " })));\n"
22791 "}",
22792 Style);
22795 TEST_F(FormatTest, LambdaWithLineComments) {
22796 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
22797 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
22798 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
22799 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22800 FormatStyle::ShortLambdaStyle::SLS_All;
22802 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
22803 verifyFormat("auto k = []() // comment\n"
22804 "{ return; }",
22805 LLVMWithBeforeLambdaBody);
22806 verifyFormat("auto k = []() /* comment */ { return; }",
22807 LLVMWithBeforeLambdaBody);
22808 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
22809 LLVMWithBeforeLambdaBody);
22810 verifyFormat("auto k = []() // X\n"
22811 "{ return; }",
22812 LLVMWithBeforeLambdaBody);
22813 verifyFormat(
22814 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
22815 "{ return; }",
22816 LLVMWithBeforeLambdaBody);
22818 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
22820 verifyFormat("foo([]()\n"
22821 " {\n"
22822 " bar(); //\n"
22823 " return 1; // comment\n"
22824 " }());",
22825 "foo([]() {\n"
22826 " bar(); //\n"
22827 " return 1; // comment\n"
22828 "}());",
22829 LLVMWithBeforeLambdaBody);
22830 verifyFormat("foo(\n"
22831 " 1, MACRO {\n"
22832 " baz();\n"
22833 " bar(); // comment\n"
22834 " },\n"
22835 " []() {});",
22836 "foo(\n"
22837 " 1, MACRO { baz(); bar(); // comment\n"
22838 " }, []() {}\n"
22839 ");",
22840 LLVMWithBeforeLambdaBody);
22843 TEST_F(FormatTest, EmptyLinesInLambdas) {
22844 verifyFormat("auto lambda = []() {\n"
22845 " x(); //\n"
22846 "};",
22847 "auto lambda = []() {\n"
22848 "\n"
22849 " x(); //\n"
22850 "\n"
22851 "};");
22854 TEST_F(FormatTest, FormatsBlocks) {
22855 FormatStyle ShortBlocks = getLLVMStyle();
22856 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
22857 verifyFormat("int (^Block)(int, int);", ShortBlocks);
22858 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
22859 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
22860 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
22861 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
22862 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
22864 verifyFormat("foo(^{ bar(); });", ShortBlocks);
22865 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
22866 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
22868 verifyFormat("[operation setCompletionBlock:^{\n"
22869 " [self onOperationDone];\n"
22870 "}];");
22871 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
22872 " [self onOperationDone];\n"
22873 "}]};");
22874 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
22875 " f();\n"
22876 "}];");
22877 verifyFormat("int a = [operation block:^int(int *i) {\n"
22878 " return 1;\n"
22879 "}];");
22880 verifyFormat("[myObject doSomethingWith:arg1\n"
22881 " aaa:^int(int *a) {\n"
22882 " return 1;\n"
22883 " }\n"
22884 " bbb:f(a * bbbbbbbb)];");
22886 verifyFormat("[operation setCompletionBlock:^{\n"
22887 " [self.delegate newDataAvailable];\n"
22888 "}];",
22889 getLLVMStyleWithColumns(60));
22890 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
22891 " NSString *path = [self sessionFilePath];\n"
22892 " if (path) {\n"
22893 " // ...\n"
22894 " }\n"
22895 "});");
22896 verifyFormat("[[SessionService sharedService]\n"
22897 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22898 " if (window) {\n"
22899 " [self windowDidLoad:window];\n"
22900 " } else {\n"
22901 " [self errorLoadingWindow];\n"
22902 " }\n"
22903 " }];");
22904 verifyFormat("void (^largeBlock)(void) = ^{\n"
22905 " // ...\n"
22906 "};",
22907 getLLVMStyleWithColumns(40));
22908 verifyFormat("[[SessionService sharedService]\n"
22909 " loadWindowWithCompletionBlock: //\n"
22910 " ^(SessionWindow *window) {\n"
22911 " if (window) {\n"
22912 " [self windowDidLoad:window];\n"
22913 " } else {\n"
22914 " [self errorLoadingWindow];\n"
22915 " }\n"
22916 " }];",
22917 getLLVMStyleWithColumns(60));
22918 verifyFormat("[myObject doSomethingWith:arg1\n"
22919 " firstBlock:^(Foo *a) {\n"
22920 " // ...\n"
22921 " int i;\n"
22922 " }\n"
22923 " secondBlock:^(Bar *b) {\n"
22924 " // ...\n"
22925 " int i;\n"
22926 " }\n"
22927 " thirdBlock:^Foo(Bar *b) {\n"
22928 " // ...\n"
22929 " int i;\n"
22930 " }];");
22931 verifyFormat("[myObject doSomethingWith:arg1\n"
22932 " firstBlock:-1\n"
22933 " secondBlock:^(Bar *b) {\n"
22934 " // ...\n"
22935 " int i;\n"
22936 " }];");
22938 verifyFormat("f(^{\n"
22939 " @autoreleasepool {\n"
22940 " if (a) {\n"
22941 " g();\n"
22942 " }\n"
22943 " }\n"
22944 "});");
22945 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
22946 "};");
22947 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
22948 "};");
22950 FormatStyle FourIndent = getLLVMStyle();
22951 FourIndent.ObjCBlockIndentWidth = 4;
22952 verifyFormat("[operation setCompletionBlock:^{\n"
22953 " [self onOperationDone];\n"
22954 "}];",
22955 FourIndent);
22958 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
22959 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0);
22961 verifyFormat("[[SessionService sharedService] "
22962 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22963 " if (window) {\n"
22964 " [self windowDidLoad:window];\n"
22965 " } else {\n"
22966 " [self errorLoadingWindow];\n"
22967 " }\n"
22968 "}];",
22969 ZeroColumn);
22970 verifyFormat("[[SessionService sharedService]\n"
22971 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22972 " if (window) {\n"
22973 " [self windowDidLoad:window];\n"
22974 " } else {\n"
22975 " [self errorLoadingWindow];\n"
22976 " }\n"
22977 " }];",
22978 "[[SessionService sharedService]\n"
22979 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22980 " if (window) {\n"
22981 " [self windowDidLoad:window];\n"
22982 " } else {\n"
22983 " [self errorLoadingWindow];\n"
22984 " }\n"
22985 "}];",
22986 ZeroColumn);
22987 verifyFormat("[myObject doSomethingWith:arg1\n"
22988 " firstBlock:^(Foo *a) {\n"
22989 " // ...\n"
22990 " int i;\n"
22991 " }\n"
22992 " secondBlock:^(Bar *b) {\n"
22993 " // ...\n"
22994 " int i;\n"
22995 " }\n"
22996 " thirdBlock:^Foo(Bar *b) {\n"
22997 " // ...\n"
22998 " int i;\n"
22999 " }];",
23000 ZeroColumn);
23001 verifyFormat("f(^{\n"
23002 " @autoreleasepool {\n"
23003 " if (a) {\n"
23004 " g();\n"
23005 " }\n"
23006 " }\n"
23007 "});",
23008 ZeroColumn);
23009 verifyFormat("void (^largeBlock)(void) = ^{\n"
23010 " // ...\n"
23011 "};",
23012 ZeroColumn);
23014 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23015 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23016 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23017 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23018 verifyFormat("void (^largeBlock)(void) = ^{\n"
23019 " int i;\n"
23020 "};",
23021 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23024 TEST_F(FormatTest, SupportsCRLF) {
23025 verifyFormat("int a;\r\n"
23026 "int b;\r\n"
23027 "int c;",
23028 "int a;\r\n"
23029 " int b;\r\n"
23030 " int c;");
23031 verifyFormat("int a;\r\n"
23032 "int b;\r\n"
23033 "int c;\r\n",
23034 "int a;\r\n"
23035 " int b;\n"
23036 " int c;\r\n");
23037 verifyFormat("int a;\n"
23038 "int b;\n"
23039 "int c;",
23040 "int a;\r\n"
23041 " int b;\n"
23042 " int c;");
23043 // FIXME: unstable test case
23044 EXPECT_EQ("\"aaaaaaa \"\r\n"
23045 "\"bbbbbbb\";\r\n",
23046 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23047 verifyFormat("#define A \\\r\n"
23048 " b; \\\r\n"
23049 " c; \\\r\n"
23050 " d;",
23051 "#define A \\\r\n"
23052 " b; \\\r\n"
23053 " c; d; ",
23054 getGoogleStyle());
23056 verifyNoChange("/*\r\n"
23057 "multi line block comments\r\n"
23058 "should not introduce\r\n"
23059 "an extra carriage return\r\n"
23060 "*/");
23061 verifyFormat("/*\r\n"
23062 "\r\n"
23063 "*/",
23064 "/*\r\n"
23065 " \r\r\r\n"
23066 "*/");
23068 FormatStyle style = getLLVMStyle();
23070 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
23071 verifyFormat("union FooBarBazQux {\n"
23072 " int foo;\n"
23073 " int bar;\n"
23074 " int baz;\n"
23075 "};",
23076 "union FooBarBazQux {\r\n"
23077 " int foo;\n"
23078 " int bar;\r\n"
23079 " int baz;\n"
23080 "};",
23081 style);
23082 style.LineEnding = FormatStyle::LE_DeriveCRLF;
23083 verifyFormat("union FooBarBazQux {\r\n"
23084 " int foo;\r\n"
23085 " int bar;\r\n"
23086 " int baz;\r\n"
23087 "};",
23088 "union FooBarBazQux {\r\n"
23089 " int foo;\n"
23090 " int bar;\r\n"
23091 " int baz;\n"
23092 "};",
23093 style);
23095 style.LineEnding = FormatStyle::LE_LF;
23096 verifyFormat("union FooBarBazQux {\n"
23097 " int foo;\n"
23098 " int bar;\n"
23099 " int baz;\n"
23100 " int qux;\n"
23101 "};",
23102 "union FooBarBazQux {\r\n"
23103 " int foo;\n"
23104 " int bar;\r\n"
23105 " int baz;\n"
23106 " int qux;\r\n"
23107 "};",
23108 style);
23109 style.LineEnding = FormatStyle::LE_CRLF;
23110 verifyFormat("union FooBarBazQux {\r\n"
23111 " int foo;\r\n"
23112 " int bar;\r\n"
23113 " int baz;\r\n"
23114 " int qux;\r\n"
23115 "};",
23116 "union FooBarBazQux {\r\n"
23117 " int foo;\n"
23118 " int bar;\r\n"
23119 " int baz;\n"
23120 " int qux;\n"
23121 "};",
23122 style);
23124 style.LineEnding = FormatStyle::LE_DeriveLF;
23125 verifyFormat("union FooBarBazQux {\r\n"
23126 " int foo;\r\n"
23127 " int bar;\r\n"
23128 " int baz;\r\n"
23129 " int qux;\r\n"
23130 "};",
23131 "union FooBarBazQux {\r\n"
23132 " int foo;\n"
23133 " int bar;\r\n"
23134 " int baz;\n"
23135 " int qux;\r\n"
23136 "};",
23137 style);
23138 style.LineEnding = FormatStyle::LE_DeriveCRLF;
23139 verifyFormat("union FooBarBazQux {\n"
23140 " int foo;\n"
23141 " int bar;\n"
23142 " int baz;\n"
23143 " int qux;\n"
23144 "};",
23145 "union FooBarBazQux {\r\n"
23146 " int foo;\n"
23147 " int bar;\r\n"
23148 " int baz;\n"
23149 " int qux;\n"
23150 "};",
23151 style);
23154 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
23155 verifyFormat("MY_CLASS(C) {\n"
23156 " int i;\n"
23157 " int j;\n"
23158 "};");
23161 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
23162 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
23163 TwoIndent.ContinuationIndentWidth = 2;
23165 verifyFormat("int i =\n"
23166 " longFunction(\n"
23167 " arg);",
23168 "int i = longFunction(arg);", TwoIndent);
23170 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
23171 SixIndent.ContinuationIndentWidth = 6;
23173 verifyFormat("int i =\n"
23174 " longFunction(\n"
23175 " arg);",
23176 "int i = longFunction(arg);", SixIndent);
23179 TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
23180 FormatStyle Style = getLLVMStyle();
23181 verifyFormat("int Foo::getter(\n"
23182 " //\n"
23183 ") const {\n"
23184 " return foo;\n"
23185 "}",
23186 Style);
23187 verifyFormat("void Foo::setter(\n"
23188 " //\n"
23189 ") {\n"
23190 " foo = 1;\n"
23191 "}",
23192 Style);
23195 TEST_F(FormatTest, SpacesInAngles) {
23196 FormatStyle Spaces = getLLVMStyle();
23197 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23199 verifyFormat("vector< ::std::string > x1;", Spaces);
23200 verifyFormat("Foo< int, Bar > x2;", Spaces);
23201 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
23203 verifyFormat("static_cast< int >(arg);", Spaces);
23204 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
23205 verifyFormat("f< int, float >();", Spaces);
23206 verifyFormat("template <> g() {}", Spaces);
23207 verifyFormat("template < std::vector< int > > f() {}", Spaces);
23208 verifyFormat("std::function< void(int, int) > fct;", Spaces);
23209 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
23210 Spaces);
23212 Spaces.Standard = FormatStyle::LS_Cpp03;
23213 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23214 verifyFormat("A< A< int > >();", Spaces);
23216 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
23217 verifyFormat("A<A<int> >();", Spaces);
23219 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
23220 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
23221 Spaces);
23222 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
23223 Spaces);
23225 verifyFormat("A<A<int> >();", Spaces);
23226 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
23227 verifyFormat("A< A< int > >();", Spaces);
23229 Spaces.Standard = FormatStyle::LS_Cpp11;
23230 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23231 verifyFormat("A< A< int > >();", Spaces);
23233 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
23234 verifyFormat("vector<::std::string> x4;", Spaces);
23235 verifyFormat("vector<int> x5;", Spaces);
23236 verifyFormat("Foo<int, Bar> x6;", Spaces);
23237 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
23239 verifyFormat("A<A<int>>();", Spaces);
23241 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
23242 verifyFormat("vector<::std::string> x4;", Spaces);
23243 verifyFormat("vector< ::std::string > x4;", Spaces);
23244 verifyFormat("vector<int> x5;", Spaces);
23245 verifyFormat("vector< int > x5;", Spaces);
23246 verifyFormat("Foo<int, Bar> x6;", Spaces);
23247 verifyFormat("Foo< int, Bar > x6;", Spaces);
23248 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
23249 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
23251 verifyFormat("A<A<int>>();", Spaces);
23252 verifyFormat("A< A< int > >();", Spaces);
23253 verifyFormat("A<A<int > >();", Spaces);
23254 verifyFormat("A< A< int>>();", Spaces);
23256 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23257 verifyFormat("// clang-format off\n"
23258 "foo<<<1, 1>>>();\n"
23259 "// clang-format on",
23260 Spaces);
23261 verifyFormat("// clang-format off\n"
23262 "foo< < <1, 1> > >();\n"
23263 "// clang-format on",
23264 Spaces);
23267 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
23268 FormatStyle Style = getLLVMStyle();
23269 Style.SpaceAfterTemplateKeyword = false;
23270 verifyFormat("template<int> void foo();", Style);
23273 TEST_F(FormatTest, TripleAngleBrackets) {
23274 verifyFormat("f<<<1, 1>>>();");
23275 verifyFormat("f<<<1, 1, 1, s>>>();");
23276 verifyFormat("f<<<a, b, c, d>>>();");
23277 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
23278 verifyFormat("f<param><<<1, 1>>>();");
23279 verifyFormat("f<1><<<1, 1>>>();");
23280 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
23281 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23282 "aaaaaaaaaaa<<<\n 1, 1>>>();");
23283 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
23284 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
23287 TEST_F(FormatTest, MergeLessLessAtEnd) {
23288 verifyFormat("<<");
23289 verifyFormat("< < <", "\\\n<<<");
23290 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23291 "aaallvm::outs() <<");
23292 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23293 "aaaallvm::outs()\n <<");
23296 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
23297 std::string code = "#if A\n"
23298 "#if B\n"
23299 "a.\n"
23300 "#endif\n"
23301 " a = 1;\n"
23302 "#else\n"
23303 "#endif\n"
23304 "#if C\n"
23305 "#else\n"
23306 "#endif\n";
23307 verifyFormat(code);
23310 TEST_F(FormatTest, HandleConflictMarkers) {
23311 // Git/SVN conflict markers.
23312 verifyFormat("int a;\n"
23313 "void f() {\n"
23314 " callme(some(parameter1,\n"
23315 "<<<<<<< text by the vcs\n"
23316 " parameter2),\n"
23317 "||||||| text by the vcs\n"
23318 " parameter2),\n"
23319 " parameter3,\n"
23320 "======= text by the vcs\n"
23321 " parameter2, parameter3),\n"
23322 ">>>>>>> text by the vcs\n"
23323 " otherparameter);",
23324 "int a;\n"
23325 "void f() {\n"
23326 " callme(some(parameter1,\n"
23327 "<<<<<<< text by the vcs\n"
23328 " parameter2),\n"
23329 "||||||| text by the vcs\n"
23330 " parameter2),\n"
23331 " parameter3,\n"
23332 "======= text by the vcs\n"
23333 " parameter2,\n"
23334 " parameter3),\n"
23335 ">>>>>>> text by the vcs\n"
23336 " otherparameter);");
23338 // Perforce markers.
23339 verifyFormat("void f() {\n"
23340 " function(\n"
23341 ">>>> text by the vcs\n"
23342 " parameter,\n"
23343 "==== text by the vcs\n"
23344 " parameter,\n"
23345 "==== text by the vcs\n"
23346 " parameter,\n"
23347 "<<<< text by the vcs\n"
23348 " parameter);",
23349 "void f() {\n"
23350 " function(\n"
23351 ">>>> text by the vcs\n"
23352 " parameter,\n"
23353 "==== text by the vcs\n"
23354 " parameter,\n"
23355 "==== text by the vcs\n"
23356 " parameter,\n"
23357 "<<<< text by the vcs\n"
23358 " parameter);");
23360 verifyNoChange("<<<<<<<\n"
23361 "|||||||\n"
23362 "=======\n"
23363 ">>>>>>>");
23365 verifyNoChange("<<<<<<<\n"
23366 "|||||||\n"
23367 "int i;\n"
23368 "=======\n"
23369 ">>>>>>>");
23371 // FIXME: Handle parsing of macros around conflict markers correctly:
23372 verifyFormat("#define Macro \\\n"
23373 "<<<<<<<\n"
23374 "Something \\\n"
23375 "|||||||\n"
23376 "Else \\\n"
23377 "=======\n"
23378 "Other \\\n"
23379 ">>>>>>>\n"
23380 " End int i;",
23381 "#define Macro \\\n"
23382 "<<<<<<<\n"
23383 " Something \\\n"
23384 "|||||||\n"
23385 " Else \\\n"
23386 "=======\n"
23387 " Other \\\n"
23388 ">>>>>>>\n"
23389 " End\n"
23390 "int i;");
23392 verifyFormat(R"(====
23393 #ifdef A
23395 #else
23397 #endif
23398 )");
23401 TEST_F(FormatTest, DisableRegions) {
23402 verifyFormat("int i;\n"
23403 "// clang-format off\n"
23404 " int j;\n"
23405 "// clang-format on\n"
23406 "int k;",
23407 " int i;\n"
23408 " // clang-format off\n"
23409 " int j;\n"
23410 " // clang-format on\n"
23411 " int k;");
23412 verifyFormat("int i;\n"
23413 "/* clang-format off */\n"
23414 " int j;\n"
23415 "/* clang-format on */\n"
23416 "int k;",
23417 " int i;\n"
23418 " /* clang-format off */\n"
23419 " int j;\n"
23420 " /* clang-format on */\n"
23421 " int k;");
23423 // Don't reflow comments within disabled regions.
23424 verifyFormat("// clang-format off\n"
23425 "// long long long long long long line\n"
23426 "/* clang-format on */\n"
23427 "/* long long long\n"
23428 " * long long long\n"
23429 " * line */\n"
23430 "int i;\n"
23431 "/* clang-format off */\n"
23432 "/* long long long long long long line */",
23433 "// clang-format off\n"
23434 "// long long long long long long line\n"
23435 "/* clang-format on */\n"
23436 "/* long long long long long long line */\n"
23437 "int i;\n"
23438 "/* clang-format off */\n"
23439 "/* long long long long long long line */",
23440 getLLVMStyleWithColumns(20));
23442 verifyFormat("int *i;\n"
23443 "// clang-format off:\n"
23444 "int* j;\n"
23445 "// clang-format on: 1\n"
23446 "int *k;",
23447 "int* i;\n"
23448 "// clang-format off:\n"
23449 "int* j;\n"
23450 "// clang-format on: 1\n"
23451 "int* k;");
23453 verifyFormat("int *i;\n"
23454 "// clang-format off:0\n"
23455 "int* j;\n"
23456 "// clang-format only\n"
23457 "int* k;",
23458 "int* i;\n"
23459 "// clang-format off:0\n"
23460 "int* j;\n"
23461 "// clang-format only\n"
23462 "int* k;");
23464 verifyNoChange("// clang-format off\n"
23465 "#if 0\n"
23466 " #if SHOULD_STAY_INDENTED\n"
23467 " #endif\n"
23468 "#endif\n"
23469 "// clang-format on");
23472 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
23473 format("? ) =");
23474 verifyNoCrash("#define a\\\n /**/}");
23477 TEST_F(FormatTest, FormatsTableGenCode) {
23478 FormatStyle Style = getLLVMStyle();
23479 Style.Language = FormatStyle::LK_TableGen;
23480 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
23483 TEST_F(FormatTest, ArrayOfTemplates) {
23484 verifyFormat("auto a = new unique_ptr<int>[10];",
23485 "auto a = new unique_ptr<int > [ 10];");
23487 FormatStyle Spaces = getLLVMStyle();
23488 Spaces.SpacesInSquareBrackets = true;
23489 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
23490 "auto a = new unique_ptr<int > [10];", Spaces);
23493 TEST_F(FormatTest, ArrayAsTemplateType) {
23494 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
23495 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
23497 FormatStyle Spaces = getLLVMStyle();
23498 Spaces.SpacesInSquareBrackets = true;
23499 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
23500 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
23503 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
23505 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
23506 verifyFormat("using std::cin;\n"
23507 "using std::cout;",
23508 "using std::cout;\n"
23509 "using std::cin;",
23510 getGoogleStyle());
23513 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
23514 FormatStyle Style = getLLVMStyle();
23515 Style.Standard = FormatStyle::LS_Cpp03;
23516 // cpp03 recognize this string as identifier u8 and literal character 'a'
23517 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
23520 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
23521 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
23522 // all modes, including C++11, C++14 and C++17
23523 verifyFormat("auto c = u8'a';");
23526 TEST_F(FormatTest, DoNotFormatLikelyXml) {
23527 verifyGoogleFormat("<!-- ;> -->");
23528 verifyNoChange(" <!-- >; -->", getGoogleStyle());
23531 TEST_F(FormatTest, StructuredBindings) {
23532 // Structured bindings is a C++17 feature.
23533 // all modes, including C++11, C++14 and C++17
23534 verifyFormat("auto [a, b] = f();");
23535 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
23536 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
23537 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
23538 verifyFormat("auto const volatile [a, b] = f();",
23539 "auto const volatile[a, b] = f();");
23540 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
23541 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
23542 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
23543 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
23544 verifyFormat("auto const volatile &&[a, b] = f();",
23545 "auto const volatile &&[a, b] = f();");
23546 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
23547 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
23548 verifyFormat("const auto volatile &&[a, b] = f();",
23549 "const auto volatile &&[a, b] = f();");
23550 verifyFormat("volatile const auto &&[a, b] = f();",
23551 "volatile const auto &&[a, b] = f();");
23552 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
23554 // Make sure we don't mistake structured bindings for lambdas.
23555 FormatStyle PointerMiddle = getLLVMStyle();
23556 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
23557 verifyGoogleFormat("auto [a1, b]{A * i};");
23558 verifyFormat("auto [a2, b]{A * i};");
23559 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
23560 verifyGoogleFormat("auto const [a1, b]{A * i};");
23561 verifyFormat("auto const [a2, b]{A * i};");
23562 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
23563 verifyGoogleFormat("auto const& [a1, b]{A * i};");
23564 verifyFormat("auto const &[a2, b]{A * i};");
23565 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
23566 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
23567 verifyFormat("auto const &&[a2, b]{A * i};");
23568 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
23570 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
23571 "for (const auto && [a, b] : some_range) {\n}");
23572 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
23573 "for (const auto & [a, b] : some_range) {\n}");
23574 verifyFormat("for (const auto [a, b] : some_range) {\n}",
23575 "for (const auto[a, b] : some_range) {\n}");
23576 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
23577 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
23578 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
23579 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
23580 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
23581 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
23582 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
23583 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
23585 FormatStyle Spaces = getLLVMStyle();
23586 Spaces.SpacesInSquareBrackets = true;
23587 verifyFormat("auto [ a, b ] = f();", Spaces);
23588 verifyFormat("auto &&[ a, b ] = f();", Spaces);
23589 verifyFormat("auto &[ a, b ] = f();", Spaces);
23590 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
23591 verifyFormat("auto const &[ a, b ] = f();", Spaces);
23594 TEST_F(FormatTest, FileAndCode) {
23595 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
23596 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
23597 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
23598 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
23599 EXPECT_EQ(FormatStyle::LK_ObjC,
23600 guessLanguage("foo.h", "@interface Foo\n@end"));
23601 EXPECT_EQ(
23602 FormatStyle::LK_ObjC,
23603 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
23604 EXPECT_EQ(FormatStyle::LK_ObjC,
23605 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
23606 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
23607 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
23608 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
23609 EXPECT_EQ(FormatStyle::LK_ObjC,
23610 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
23611 EXPECT_EQ(FormatStyle::LK_ObjC,
23612 guessLanguage(
23613 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
23614 EXPECT_EQ(
23615 FormatStyle::LK_Cpp,
23616 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
23617 // Only one of the two preprocessor regions has ObjC-like code.
23618 EXPECT_EQ(FormatStyle::LK_ObjC,
23619 guessLanguage("foo.h", "#if A\n"
23620 "#define B() C\n"
23621 "#else\n"
23622 "#define B() [NSString a:@\"\"]\n"
23623 "#endif"));
23626 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
23627 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
23628 EXPECT_EQ(FormatStyle::LK_ObjC,
23629 guessLanguage("foo.h", "array[[calculator getIndex]];"));
23630 EXPECT_EQ(FormatStyle::LK_Cpp,
23631 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
23632 EXPECT_EQ(
23633 FormatStyle::LK_Cpp,
23634 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
23635 EXPECT_EQ(FormatStyle::LK_ObjC,
23636 guessLanguage("foo.h", "[[noreturn foo] bar];"));
23637 EXPECT_EQ(FormatStyle::LK_Cpp,
23638 guessLanguage("foo.h", "[[clang::fallthrough]];"));
23639 EXPECT_EQ(FormatStyle::LK_ObjC,
23640 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
23641 EXPECT_EQ(FormatStyle::LK_Cpp,
23642 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
23643 EXPECT_EQ(FormatStyle::LK_Cpp,
23644 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
23645 EXPECT_EQ(FormatStyle::LK_ObjC,
23646 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
23647 EXPECT_EQ(FormatStyle::LK_Cpp,
23648 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
23649 EXPECT_EQ(
23650 FormatStyle::LK_Cpp,
23651 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
23652 EXPECT_EQ(
23653 FormatStyle::LK_Cpp,
23654 guessLanguage("foo.h",
23655 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
23656 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
23659 TEST_F(FormatTest, GuessLanguageWithCaret) {
23660 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
23661 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
23662 EXPECT_EQ(FormatStyle::LK_ObjC,
23663 guessLanguage("foo.h", "int(^)(char, float);"));
23664 EXPECT_EQ(FormatStyle::LK_ObjC,
23665 guessLanguage("foo.h", "int(^foo)(char, float);"));
23666 EXPECT_EQ(FormatStyle::LK_ObjC,
23667 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
23668 EXPECT_EQ(FormatStyle::LK_ObjC,
23669 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
23670 EXPECT_EQ(
23671 FormatStyle::LK_ObjC,
23672 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
23675 TEST_F(FormatTest, GuessLanguageWithPragmas) {
23676 EXPECT_EQ(FormatStyle::LK_Cpp,
23677 guessLanguage("foo.h", "__pragma(warning(disable:))"));
23678 EXPECT_EQ(FormatStyle::LK_Cpp,
23679 guessLanguage("foo.h", "#pragma(warning(disable:))"));
23680 EXPECT_EQ(FormatStyle::LK_Cpp,
23681 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
23684 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
23685 // ASM symbolic names are identifiers that must be surrounded by [] without
23686 // space in between:
23687 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
23689 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
23690 verifyFormat(R"(//
23691 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
23692 )");
23694 // A list of several ASM symbolic names.
23695 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
23697 // ASM symbolic names in inline ASM with inputs and outputs.
23698 verifyFormat(R"(//
23699 asm("cmoveq %1, %2, %[result]"
23700 : [result] "=r"(result)
23701 : "r"(test), "r"(new), "[result]"(old));
23702 )");
23704 // ASM symbolic names in inline ASM with no outputs.
23705 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
23708 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
23709 EXPECT_EQ(FormatStyle::LK_Cpp,
23710 guessLanguage("foo.h", "void f() {\n"
23711 " asm (\"mov %[e], %[d]\"\n"
23712 " : [d] \"=rm\" (d)\n"
23713 " [e] \"rm\" (*e));\n"
23714 "}"));
23715 EXPECT_EQ(FormatStyle::LK_Cpp,
23716 guessLanguage("foo.h", "void f() {\n"
23717 " _asm (\"mov %[e], %[d]\"\n"
23718 " : [d] \"=rm\" (d)\n"
23719 " [e] \"rm\" (*e));\n"
23720 "}"));
23721 EXPECT_EQ(FormatStyle::LK_Cpp,
23722 guessLanguage("foo.h", "void f() {\n"
23723 " __asm (\"mov %[e], %[d]\"\n"
23724 " : [d] \"=rm\" (d)\n"
23725 " [e] \"rm\" (*e));\n"
23726 "}"));
23727 EXPECT_EQ(FormatStyle::LK_Cpp,
23728 guessLanguage("foo.h", "void f() {\n"
23729 " __asm__ (\"mov %[e], %[d]\"\n"
23730 " : [d] \"=rm\" (d)\n"
23731 " [e] \"rm\" (*e));\n"
23732 "}"));
23733 EXPECT_EQ(FormatStyle::LK_Cpp,
23734 guessLanguage("foo.h", "void f() {\n"
23735 " asm (\"mov %[e], %[d]\"\n"
23736 " : [d] \"=rm\" (d),\n"
23737 " [e] \"rm\" (*e));\n"
23738 "}"));
23739 EXPECT_EQ(FormatStyle::LK_Cpp,
23740 guessLanguage("foo.h", "void f() {\n"
23741 " asm volatile (\"mov %[e], %[d]\"\n"
23742 " : [d] \"=rm\" (d)\n"
23743 " [e] \"rm\" (*e));\n"
23744 "}"));
23747 TEST_F(FormatTest, GuessLanguageWithChildLines) {
23748 EXPECT_EQ(FormatStyle::LK_Cpp,
23749 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
23750 EXPECT_EQ(FormatStyle::LK_ObjC,
23751 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
23752 EXPECT_EQ(
23753 FormatStyle::LK_Cpp,
23754 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
23755 EXPECT_EQ(
23756 FormatStyle::LK_ObjC,
23757 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
23760 TEST_F(FormatTest, TypenameMacros) {
23761 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
23763 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
23764 FormatStyle Google = getGoogleStyleWithColumns(0);
23765 Google.TypenameMacros = TypenameMacros;
23766 verifyFormat("struct foo {\n"
23767 " int bar;\n"
23768 " TAILQ_ENTRY(a) bleh;\n"
23769 "};",
23770 Google);
23772 FormatStyle Macros = getLLVMStyle();
23773 Macros.TypenameMacros = TypenameMacros;
23775 verifyFormat("STACK_OF(int) a;", Macros);
23776 verifyFormat("STACK_OF(int) *a;", Macros);
23777 verifyFormat("STACK_OF(int const *) *a;", Macros);
23778 verifyFormat("STACK_OF(int *const) *a;", Macros);
23779 verifyFormat("STACK_OF(int, string) a;", Macros);
23780 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
23781 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
23782 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
23783 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
23784 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
23785 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
23787 Macros.PointerAlignment = FormatStyle::PAS_Left;
23788 verifyFormat("STACK_OF(int)* a;", Macros);
23789 verifyFormat("STACK_OF(int*)* a;", Macros);
23790 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
23791 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
23792 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
23795 TEST_F(FormatTest, AtomicQualifier) {
23796 // Check that we treate _Atomic as a type and not a function call
23797 FormatStyle Google = getGoogleStyleWithColumns(0);
23798 verifyFormat("struct foo {\n"
23799 " int a1;\n"
23800 " _Atomic(a) a2;\n"
23801 " _Atomic(_Atomic(int) *const) a3;\n"
23802 "};",
23803 Google);
23804 verifyFormat("_Atomic(uint64_t) a;");
23805 verifyFormat("_Atomic(uint64_t) *a;");
23806 verifyFormat("_Atomic(uint64_t const *) *a;");
23807 verifyFormat("_Atomic(uint64_t *const) *a;");
23808 verifyFormat("_Atomic(const uint64_t *) *a;");
23809 verifyFormat("_Atomic(uint64_t) a;");
23810 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
23811 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
23812 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
23813 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
23815 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
23816 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
23817 FormatStyle Style = getLLVMStyle();
23818 Style.PointerAlignment = FormatStyle::PAS_Left;
23819 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
23820 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
23821 verifyFormat("_Atomic(int)* a;", Style);
23822 verifyFormat("_Atomic(int*)* a;", Style);
23823 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
23825 Style.SpacesInParens = FormatStyle::SIPO_Custom;
23826 Style.SpacesInParensOptions.InCStyleCasts = true;
23827 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
23828 Style.SpacesInParensOptions.InCStyleCasts = false;
23829 Style.SpacesInParensOptions.Other = true;
23830 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
23831 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
23834 TEST_F(FormatTest, C11Generic) {
23835 verifyFormat("_Generic(x, int: 1, default: 0)");
23836 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
23837 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
23838 verifyFormat("_Generic(x, int: f1, const int: f2)();");
23839 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
23841 verifyFormat("_Generic(x,\n"
23842 " float: f,\n"
23843 " default: d,\n"
23844 " long double: ld,\n"
23845 " float _Complex: fc,\n"
23846 " double _Complex: dc,\n"
23847 " long double _Complex: ldc)");
23849 FormatStyle Style = getLLVMStyle();
23850 Style.ColumnLimit = 40;
23851 verifyFormat("#define LIMIT_MAX(T) \\\n"
23852 " _Generic(((T)0), \\\n"
23853 " unsigned int: UINT_MAX, \\\n"
23854 " unsigned long: ULONG_MAX, \\\n"
23855 " unsigned long long: ULLONG_MAX)",
23856 Style);
23857 verifyFormat("_Generic(x,\n"
23858 " struct A: 1,\n"
23859 " void (*)(void): 2);",
23860 Style);
23862 Style.ContinuationIndentWidth = 2;
23863 verifyFormat("_Generic(x,\n"
23864 " struct A: 1,\n"
23865 " void (*)(void): 2);",
23866 Style);
23869 TEST_F(FormatTest, AmbersandInLamda) {
23870 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
23871 FormatStyle AlignStyle = getLLVMStyle();
23872 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
23873 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
23874 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
23875 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
23878 TEST_F(FormatTest, TrailingReturnTypeAuto) {
23879 FormatStyle Style = getLLVMStyle();
23880 verifyFormat("[]() -> auto { return Val; }", Style);
23881 verifyFormat("[]() -> auto * { return Val; }", Style);
23882 verifyFormat("[]() -> auto & { return Val; }", Style);
23883 verifyFormat("auto foo() -> auto { return Val; }", Style);
23884 verifyFormat("auto foo() -> auto * { return Val; }", Style);
23885 verifyFormat("auto foo() -> auto & { return Val; }", Style);
23888 TEST_F(FormatTest, SpacesInConditionalStatement) {
23889 FormatStyle Spaces = getLLVMStyle();
23890 Spaces.IfMacros.clear();
23891 Spaces.IfMacros.push_back("MYIF");
23892 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
23893 Spaces.SpacesInParensOptions.InConditionalStatements = true;
23894 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
23895 verifyFormat("if ( !a )\n return;", Spaces);
23896 verifyFormat("if ( a )\n return;", Spaces);
23897 verifyFormat("if constexpr ( a )\n return;", Spaces);
23898 verifyFormat("MYIF ( a )\n return;", Spaces);
23899 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
23900 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
23901 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
23902 verifyFormat("while ( a )\n return;", Spaces);
23903 verifyFormat("while ( (a && b) )\n return;", Spaces);
23904 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
23905 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
23906 // Check that space on the left of "::" is inserted as expected at beginning
23907 // of condition.
23908 verifyFormat("while ( ::func() )\n return;", Spaces);
23910 // Check impact of ControlStatementsExceptControlMacros is honored.
23911 Spaces.SpaceBeforeParens =
23912 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
23913 verifyFormat("MYIF( a )\n return;", Spaces);
23914 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
23915 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
23918 TEST_F(FormatTest, AlternativeOperators) {
23919 // Test case for ensuring alternate operators are not
23920 // combined with their right most neighbour.
23921 verifyFormat("int a and b;");
23922 verifyFormat("int a and_eq b;");
23923 verifyFormat("int a bitand b;");
23924 verifyFormat("int a bitor b;");
23925 verifyFormat("int a compl b;");
23926 verifyFormat("int a not b;");
23927 verifyFormat("int a not_eq b;");
23928 verifyFormat("int a or b;");
23929 verifyFormat("int a xor b;");
23930 verifyFormat("int a xor_eq b;");
23931 verifyFormat("return this not_eq bitand other;");
23932 verifyFormat("bool operator not_eq(const X bitand other)");
23934 verifyFormat("int a and 5;");
23935 verifyFormat("int a and_eq 5;");
23936 verifyFormat("int a bitand 5;");
23937 verifyFormat("int a bitor 5;");
23938 verifyFormat("int a compl 5;");
23939 verifyFormat("int a not 5;");
23940 verifyFormat("int a not_eq 5;");
23941 verifyFormat("int a or 5;");
23942 verifyFormat("int a xor 5;");
23943 verifyFormat("int a xor_eq 5;");
23945 verifyFormat("int a compl(5);");
23946 verifyFormat("int a not(5);");
23948 /* FIXME handle alternate tokens
23949 * https://en.cppreference.com/w/cpp/language/operator_alternative
23950 // alternative tokens
23951 verifyFormat("compl foo();"); // ~foo();
23952 verifyFormat("foo() <%%>;"); // foo();
23953 verifyFormat("void foo() <%%>;"); // void foo(){}
23954 verifyFormat("int a <:1:>;"); // int a[1];[
23955 verifyFormat("%:define ABC abc"); // #define ABC abc
23956 verifyFormat("%:%:"); // ##
23960 TEST_F(FormatTest, STLWhileNotDefineChed) {
23961 verifyFormat("#if defined(while)\n"
23962 "#define while EMIT WARNING C4005\n"
23963 "#endif // while");
23966 TEST_F(FormatTest, OperatorSpacing) {
23967 FormatStyle Style = getLLVMStyle();
23968 Style.PointerAlignment = FormatStyle::PAS_Right;
23969 verifyFormat("Foo::operator*();", Style);
23970 verifyFormat("Foo::operator void *();", Style);
23971 verifyFormat("Foo::operator void **();", Style);
23972 verifyFormat("Foo::operator void *&();", Style);
23973 verifyFormat("Foo::operator void *&&();", Style);
23974 verifyFormat("Foo::operator void const *();", Style);
23975 verifyFormat("Foo::operator void const **();", Style);
23976 verifyFormat("Foo::operator void const *&();", Style);
23977 verifyFormat("Foo::operator void const *&&();", Style);
23978 verifyFormat("Foo::operator()(void *);", Style);
23979 verifyFormat("Foo::operator*(void *);", Style);
23980 verifyFormat("Foo::operator*();", Style);
23981 verifyFormat("Foo::operator**();", Style);
23982 verifyFormat("Foo::operator&();", Style);
23983 verifyFormat("Foo::operator<int> *();", Style);
23984 verifyFormat("Foo::operator<Foo> *();", Style);
23985 verifyFormat("Foo::operator<int> **();", Style);
23986 verifyFormat("Foo::operator<Foo> **();", Style);
23987 verifyFormat("Foo::operator<int> &();", Style);
23988 verifyFormat("Foo::operator<Foo> &();", Style);
23989 verifyFormat("Foo::operator<int> &&();", Style);
23990 verifyFormat("Foo::operator<Foo> &&();", Style);
23991 verifyFormat("Foo::operator<int> *&();", Style);
23992 verifyFormat("Foo::operator<Foo> *&();", Style);
23993 verifyFormat("Foo::operator<int> *&&();", Style);
23994 verifyFormat("Foo::operator<Foo> *&&();", Style);
23995 verifyFormat("operator*(int (*)(), class Foo);", Style);
23997 verifyFormat("Foo::operator&();", Style);
23998 verifyFormat("Foo::operator void &();", Style);
23999 verifyFormat("Foo::operator void const &();", Style);
24000 verifyFormat("Foo::operator()(void &);", Style);
24001 verifyFormat("Foo::operator&(void &);", Style);
24002 verifyFormat("Foo::operator&();", Style);
24003 verifyFormat("operator&(int (&)(), class Foo);", Style);
24004 verifyFormat("operator&&(int (&)(), class Foo);", Style);
24006 verifyFormat("Foo::operator&&();", Style);
24007 verifyFormat("Foo::operator**();", Style);
24008 verifyFormat("Foo::operator void &&();", Style);
24009 verifyFormat("Foo::operator void const &&();", Style);
24010 verifyFormat("Foo::operator()(void &&);", Style);
24011 verifyFormat("Foo::operator&&(void &&);", Style);
24012 verifyFormat("Foo::operator&&();", Style);
24013 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24014 verifyFormat("operator const nsTArrayRight<E> &()", Style);
24015 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24016 Style);
24017 verifyFormat("operator void **()", Style);
24018 verifyFormat("operator const FooRight<Object> &()", Style);
24019 verifyFormat("operator const FooRight<Object> *()", Style);
24020 verifyFormat("operator const FooRight<Object> **()", Style);
24021 verifyFormat("operator const FooRight<Object> *&()", Style);
24022 verifyFormat("operator const FooRight<Object> *&&()", Style);
24024 Style.PointerAlignment = FormatStyle::PAS_Left;
24025 verifyFormat("Foo::operator*();", Style);
24026 verifyFormat("Foo::operator**();", Style);
24027 verifyFormat("Foo::operator void*();", Style);
24028 verifyFormat("Foo::operator void**();", Style);
24029 verifyFormat("Foo::operator void*&();", Style);
24030 verifyFormat("Foo::operator void*&&();", Style);
24031 verifyFormat("Foo::operator void const*();", Style);
24032 verifyFormat("Foo::operator void const**();", Style);
24033 verifyFormat("Foo::operator void const*&();", Style);
24034 verifyFormat("Foo::operator void const*&&();", Style);
24035 verifyFormat("Foo::operator/*comment*/ void*();", Style);
24036 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
24037 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
24038 verifyFormat("Foo::operator()(void*);", Style);
24039 verifyFormat("Foo::operator*(void*);", Style);
24040 verifyFormat("Foo::operator*();", Style);
24041 verifyFormat("Foo::operator<int>*();", Style);
24042 verifyFormat("Foo::operator<Foo>*();", Style);
24043 verifyFormat("Foo::operator<int>**();", Style);
24044 verifyFormat("Foo::operator<Foo>**();", Style);
24045 verifyFormat("Foo::operator<Foo>*&();", Style);
24046 verifyFormat("Foo::operator<int>&();", Style);
24047 verifyFormat("Foo::operator<Foo>&();", Style);
24048 verifyFormat("Foo::operator<int>&&();", Style);
24049 verifyFormat("Foo::operator<Foo>&&();", Style);
24050 verifyFormat("Foo::operator<int>*&();", Style);
24051 verifyFormat("Foo::operator<Foo>*&();", Style);
24052 verifyFormat("operator*(int (*)(), class Foo);", Style);
24054 verifyFormat("Foo::operator&();", Style);
24055 verifyFormat("Foo::operator void&();", Style);
24056 verifyFormat("Foo::operator void const&();", Style);
24057 verifyFormat("Foo::operator/*comment*/ void&();", Style);
24058 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
24059 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
24060 verifyFormat("Foo::operator()(void&);", Style);
24061 verifyFormat("Foo::operator&(void&);", Style);
24062 verifyFormat("Foo::operator&();", Style);
24063 verifyFormat("operator&(int (&)(), class Foo);", Style);
24064 verifyFormat("operator&(int (&&)(), class Foo);", Style);
24065 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24067 verifyFormat("Foo::operator&&();", Style);
24068 verifyFormat("Foo::operator void&&();", Style);
24069 verifyFormat("Foo::operator void const&&();", Style);
24070 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
24071 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
24072 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
24073 verifyFormat("Foo::operator()(void&&);", Style);
24074 verifyFormat("Foo::operator&&(void&&);", Style);
24075 verifyFormat("Foo::operator&&();", Style);
24076 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24077 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
24078 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
24079 Style);
24080 verifyFormat("operator void**()", Style);
24081 verifyFormat("operator const FooLeft<Object>&()", Style);
24082 verifyFormat("operator const FooLeft<Object>*()", Style);
24083 verifyFormat("operator const FooLeft<Object>**()", Style);
24084 verifyFormat("operator const FooLeft<Object>*&()", Style);
24085 verifyFormat("operator const FooLeft<Object>*&&()", Style);
24087 // PR45107
24088 verifyFormat("operator Vector<String>&();", Style);
24089 verifyFormat("operator const Vector<String>&();", Style);
24090 verifyFormat("operator foo::Bar*();", Style);
24091 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
24092 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
24093 Style);
24095 Style.PointerAlignment = FormatStyle::PAS_Middle;
24096 verifyFormat("Foo::operator*();", Style);
24097 verifyFormat("Foo::operator void *();", Style);
24098 verifyFormat("Foo::operator()(void *);", Style);
24099 verifyFormat("Foo::operator*(void *);", Style);
24100 verifyFormat("Foo::operator*();", Style);
24101 verifyFormat("operator*(int (*)(), class Foo);", Style);
24103 verifyFormat("Foo::operator&();", Style);
24104 verifyFormat("Foo::operator void &();", Style);
24105 verifyFormat("Foo::operator void const &();", Style);
24106 verifyFormat("Foo::operator()(void &);", Style);
24107 verifyFormat("Foo::operator&(void &);", Style);
24108 verifyFormat("Foo::operator&();", Style);
24109 verifyFormat("operator&(int (&)(), class Foo);", Style);
24111 verifyFormat("Foo::operator&&();", Style);
24112 verifyFormat("Foo::operator void &&();", Style);
24113 verifyFormat("Foo::operator void const &&();", Style);
24114 verifyFormat("Foo::operator()(void &&);", Style);
24115 verifyFormat("Foo::operator&&(void &&);", Style);
24116 verifyFormat("Foo::operator&&();", Style);
24117 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24120 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
24121 FormatStyle Style = getLLVMStyle();
24122 // PR46157
24123 verifyFormat("foo(operator+, -42);", Style);
24124 verifyFormat("foo(operator++, -42);", Style);
24125 verifyFormat("foo(operator--, -42);", Style);
24126 verifyFormat("foo(-42, operator--);", Style);
24127 verifyFormat("foo(-42, operator, );", Style);
24128 verifyFormat("foo(operator, , -42);", Style);
24131 TEST_F(FormatTest, WhitespaceSensitiveMacros) {
24132 FormatStyle Style = getLLVMStyle();
24133 Style.WhitespaceSensitiveMacros.push_back("FOO");
24135 // Newlines are important here.
24136 verifyNoChange("FOO(1+2 )\n", Style);
24137 verifyNoChange("FOO(a:b:c)\n", Style);
24139 // Don't use the helpers here, since 'mess up' will change the whitespace
24140 // and these are all whitespace sensitive by definition
24141 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
24142 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
24143 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
24144 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
24145 " Still=Intentional);",
24146 Style);
24147 Style.AlignConsecutiveAssignments.Enabled = true;
24148 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
24149 " Still=Intentional);",
24150 Style);
24152 Style.ColumnLimit = 21;
24153 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
24156 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
24157 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
24158 // test its interaction with line wrapping
24159 FormatStyle Style = getLLVMStyleWithColumns(80);
24160 verifyFormat("namespace {\n"
24161 "int i;\n"
24162 "int j;\n"
24163 "} // namespace",
24164 Style);
24166 verifyFormat("namespace AAA {\n"
24167 "int i;\n"
24168 "int j;\n"
24169 "} // namespace AAA",
24170 Style);
24172 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
24173 "int i;\n"
24174 "int j;\n"
24175 "} // namespace Averyveryveryverylongnamespace",
24176 "namespace Averyveryveryverylongnamespace {\n"
24177 "int i;\n"
24178 "int j;\n"
24179 "}",
24180 Style);
24182 verifyFormat(
24183 "namespace "
24184 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
24185 " went::mad::now {\n"
24186 "int i;\n"
24187 "int j;\n"
24188 "} // namespace\n"
24189 " // "
24190 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
24191 "went::mad::now",
24192 "namespace "
24193 "would::it::save::you::a::lot::of::time::if_::i::"
24194 "just::gave::up::and_::went::mad::now {\n"
24195 "int i;\n"
24196 "int j;\n"
24197 "}",
24198 Style);
24200 // This used to duplicate the comment again and again on subsequent runs
24201 verifyFormat(
24202 "namespace "
24203 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
24204 " went::mad::now {\n"
24205 "int i;\n"
24206 "int j;\n"
24207 "} // namespace\n"
24208 " // "
24209 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
24210 "went::mad::now",
24211 "namespace "
24212 "would::it::save::you::a::lot::of::time::if_::i::"
24213 "just::gave::up::and_::went::mad::now {\n"
24214 "int i;\n"
24215 "int j;\n"
24216 "} // namespace\n"
24217 " // "
24218 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
24219 "and_::went::mad::now",
24220 Style);
24223 TEST_F(FormatTest, LikelyUnlikely) {
24224 FormatStyle Style = getLLVMStyle();
24226 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24227 " return 29;\n"
24228 "}",
24229 Style);
24231 verifyFormat("if (argc > 5) [[likely]] {\n"
24232 " return 29;\n"
24233 "}",
24234 Style);
24236 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24237 " return 29;\n"
24238 "} else [[likely]] {\n"
24239 " return 42;\n"
24240 "}",
24241 Style);
24243 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24244 " return 29;\n"
24245 "} else if (argc > 10) [[likely]] {\n"
24246 " return 99;\n"
24247 "} else {\n"
24248 " return 42;\n"
24249 "}",
24250 Style);
24252 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
24253 " return 29;\n"
24254 "}",
24255 Style);
24257 verifyFormat("if (argc > 5) [[unlikely]]\n"
24258 " return 29;",
24259 Style);
24260 verifyFormat("if (argc > 5) [[likely]]\n"
24261 " return 29;",
24262 Style);
24264 verifyFormat("while (limit > 0) [[unlikely]] {\n"
24265 " --limit;\n"
24266 "}",
24267 Style);
24268 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
24269 " --limit;\n"
24270 "}",
24271 Style);
24273 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
24274 " --limit;",
24275 Style);
24276 verifyFormat("while (limit > 0) [[likely]]\n"
24277 " --limit;",
24278 Style);
24280 Style.AttributeMacros.push_back("UNLIKELY");
24281 Style.AttributeMacros.push_back("LIKELY");
24282 verifyFormat("if (argc > 5) UNLIKELY\n"
24283 " return 29;",
24284 Style);
24286 verifyFormat("if (argc > 5) UNLIKELY {\n"
24287 " return 29;\n"
24288 "}",
24289 Style);
24290 verifyFormat("if (argc > 5) UNLIKELY {\n"
24291 " return 29;\n"
24292 "} else [[likely]] {\n"
24293 " return 42;\n"
24294 "}",
24295 Style);
24296 verifyFormat("if (argc > 5) UNLIKELY {\n"
24297 " return 29;\n"
24298 "} else LIKELY {\n"
24299 " return 42;\n"
24300 "}",
24301 Style);
24302 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24303 " return 29;\n"
24304 "} else LIKELY {\n"
24305 " return 42;\n"
24306 "}",
24307 Style);
24309 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
24310 " --limit;\n"
24311 "}",
24312 Style);
24313 verifyFormat("while (limit > 0) LIKELY {\n"
24314 " --limit;\n"
24315 "}",
24316 Style);
24318 verifyFormat("while (limit > 0) UNLIKELY\n"
24319 " --limit;",
24320 Style);
24321 verifyFormat("for (auto &limit : limits) LIKELY\n"
24322 " --limit;",
24323 Style);
24326 TEST_F(FormatTest, PenaltyIndentedWhitespace) {
24327 verifyFormat("Constructor()\n"
24328 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
24329 " aaaa(aaaaaaaaaaaaaaaaaa, "
24330 "aaaaaaaaaaaaaaaaaat))");
24331 verifyFormat("Constructor()\n"
24332 " : aaaaaaaaaaaaa(aaaaaa), "
24333 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
24335 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
24336 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
24337 verifyFormat("Constructor()\n"
24338 " : aaaaaa(aaaaaa),\n"
24339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
24340 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
24341 StyleWithWhitespacePenalty);
24342 verifyFormat("Constructor()\n"
24343 " : aaaaaaaaaaaaa(aaaaaa), "
24344 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
24345 StyleWithWhitespacePenalty);
24348 TEST_F(FormatTest, LLVMDefaultStyle) {
24349 FormatStyle Style = getLLVMStyle();
24350 verifyFormat("extern \"C\" {\n"
24351 "int foo();\n"
24352 "}",
24353 Style);
24355 TEST_F(FormatTest, GNUDefaultStyle) {
24356 FormatStyle Style = getGNUStyle();
24357 verifyFormat("extern \"C\"\n"
24358 "{\n"
24359 " int foo ();\n"
24360 "}",
24361 Style);
24363 TEST_F(FormatTest, MozillaDefaultStyle) {
24364 FormatStyle Style = getMozillaStyle();
24365 verifyFormat("extern \"C\"\n"
24366 "{\n"
24367 " int foo();\n"
24368 "}",
24369 Style);
24371 TEST_F(FormatTest, GoogleDefaultStyle) {
24372 FormatStyle Style = getGoogleStyle();
24373 verifyFormat("extern \"C\" {\n"
24374 "int foo();\n"
24375 "}",
24376 Style);
24378 TEST_F(FormatTest, ChromiumDefaultStyle) {
24379 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp);
24380 verifyFormat("extern \"C\" {\n"
24381 "int foo();\n"
24382 "}",
24383 Style);
24385 TEST_F(FormatTest, MicrosoftDefaultStyle) {
24386 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp);
24387 verifyFormat("extern \"C\"\n"
24388 "{\n"
24389 " int foo();\n"
24390 "}",
24391 Style);
24393 TEST_F(FormatTest, WebKitDefaultStyle) {
24394 FormatStyle Style = getWebKitStyle();
24395 verifyFormat("extern \"C\" {\n"
24396 "int foo();\n"
24397 "}",
24398 Style);
24401 TEST_F(FormatTest, Concepts) {
24402 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
24403 FormatStyle::BBCDS_Always);
24405 // The default in LLVM style is REI_OuterScope, but these tests were written
24406 // when the default was REI_Keyword.
24407 FormatStyle Style = getLLVMStyle();
24408 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
24410 verifyFormat("template <typename T>\n"
24411 "concept True = true;");
24413 verifyFormat("template <typename T>\n"
24414 "concept C = ((false || foo()) && C2<T>) ||\n"
24415 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
24416 getLLVMStyleWithColumns(60));
24418 verifyFormat("template <typename T>\n"
24419 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
24420 "sizeof(T) <= 8;");
24422 verifyFormat("template <typename T>\n"
24423 "concept DelayedCheck = true && requires(T t) {\n"
24424 " t.bar();\n"
24425 " t.baz();\n"
24426 " } && sizeof(T) <= 8;",
24427 Style);
24429 verifyFormat("template <typename T>\n"
24430 "concept DelayedCheck = true && requires(T t) { // Comment\n"
24431 " t.bar();\n"
24432 " t.baz();\n"
24433 " } && sizeof(T) <= 8;",
24434 Style);
24436 verifyFormat("template <typename T>\n"
24437 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
24438 "sizeof(T) <= 8;");
24440 verifyFormat("template <typename T>\n"
24441 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
24443 verifyFormat("template <typename T>\n"
24444 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
24446 verifyFormat("template <typename T>\n"
24447 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
24449 verifyFormat("template <typename T>\n"
24450 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
24451 "&& sizeof(T) <= 8;");
24453 verifyFormat("template <typename T>\n"
24454 "concept DelayedCheck =\n"
24455 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
24456 "sizeof(T) <= 8;");
24458 verifyFormat("template <typename T>\n"
24459 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
24460 "&& sizeof(T) <= 8;");
24462 verifyFormat(
24463 "template <typename T>\n"
24464 "concept DelayedCheck =\n"
24465 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
24467 verifyFormat("template <typename T>\n"
24468 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
24469 "&& sizeof(T) <= 8;");
24471 verifyFormat("template <typename T>\n"
24472 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
24473 "sizeof(T) <= 8;");
24475 verifyFormat("template <typename T>\n"
24476 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
24477 " requires(T t) {\n"
24478 " t.bar();\n"
24479 " t.baz();\n"
24480 " } && sizeof(T) <= 8 && !(4 < 3);",
24481 getLLVMStyleWithColumns(60));
24483 verifyFormat("template <typename T>\n"
24484 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
24486 verifyFormat("template <typename T>\n"
24487 "concept C = foo();");
24489 verifyFormat("template <typename T>\n"
24490 "concept C = foo(T());");
24492 verifyFormat("template <typename T>\n"
24493 "concept C = foo(T{});");
24495 verifyFormat("template <typename T>\n"
24496 "concept Size = V<sizeof(T)>::Value > 5;");
24498 verifyFormat("template <typename T>\n"
24499 "concept True = S<T>::Value;");
24501 verifyFormat("template <S T>\n"
24502 "concept True = T.field;");
24504 verifyFormat(
24505 "template <typename T>\n"
24506 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
24507 " sizeof(T) <= 8;");
24509 // FIXME: This is misformatted because the fake l paren starts at bool, not at
24510 // the lambda l square.
24511 verifyFormat("template <typename T>\n"
24512 "concept C = [] -> bool { return true; }() && requires(T t) { "
24513 "t.bar(); } &&\n"
24514 " sizeof(T) <= 8;");
24516 verifyFormat(
24517 "template <typename T>\n"
24518 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
24519 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
24521 verifyFormat("template <typename T>\n"
24522 "concept C = decltype([]() { return std::true_type{}; "
24523 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24524 getLLVMStyleWithColumns(120));
24526 verifyFormat("template <typename T>\n"
24527 "concept C = decltype([]() -> std::true_type { return {}; "
24528 "}())::value &&\n"
24529 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
24531 verifyFormat("template <typename T>\n"
24532 "concept C = true;\n"
24533 "Foo Bar;");
24535 verifyFormat("template <typename T>\n"
24536 "concept Hashable = requires(T a) {\n"
24537 " { std::hash<T>{}(a) } -> "
24538 "std::convertible_to<std::size_t>;\n"
24539 " };",
24540 Style);
24542 verifyFormat(
24543 "template <typename T>\n"
24544 "concept EqualityComparable = requires(T a, T b) {\n"
24545 " { a == b } -> std::same_as<bool>;\n"
24546 " };",
24547 Style);
24549 verifyFormat(
24550 "template <typename T>\n"
24551 "concept EqualityComparable = requires(T a, T b) {\n"
24552 " { a == b } -> std::same_as<bool>;\n"
24553 " { a != b } -> std::same_as<bool>;\n"
24554 " };",
24555 Style);
24557 verifyFormat("template <typename T>\n"
24558 "concept WeakEqualityComparable = requires(T a, T b) {\n"
24559 " { a == b };\n"
24560 " { a != b };\n"
24561 " };",
24562 Style);
24564 verifyFormat("template <typename T>\n"
24565 "concept HasSizeT = requires { typename T::size_t; };");
24567 verifyFormat("template <typename T>\n"
24568 "concept Semiregular =\n"
24569 " DefaultConstructible<T> && CopyConstructible<T> && "
24570 "CopyAssignable<T> &&\n"
24571 " requires(T a, std::size_t n) {\n"
24572 " requires Same<T *, decltype(&a)>;\n"
24573 " { a.~T() } noexcept;\n"
24574 " requires Same<T *, decltype(new T)>;\n"
24575 " requires Same<T *, decltype(new T[n])>;\n"
24576 " { delete new T; };\n"
24577 " { delete new T[n]; };\n"
24578 " };",
24579 Style);
24581 verifyFormat("template <typename T>\n"
24582 "concept Semiregular =\n"
24583 " requires(T a, std::size_t n) {\n"
24584 " requires Same<T *, decltype(&a)>;\n"
24585 " { a.~T() } noexcept;\n"
24586 " requires Same<T *, decltype(new T)>;\n"
24587 " requires Same<T *, decltype(new T[n])>;\n"
24588 " { delete new T; };\n"
24589 " { delete new T[n]; };\n"
24590 " { new T } -> std::same_as<T *>;\n"
24591 " } && DefaultConstructible<T> && CopyConstructible<T> && "
24592 "CopyAssignable<T>;",
24593 Style);
24595 verifyFormat(
24596 "template <typename T>\n"
24597 "concept Semiregular =\n"
24598 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
24599 " requires Same<T *, decltype(&a)>;\n"
24600 " { a.~T() } noexcept;\n"
24601 " requires Same<T *, decltype(new T)>;\n"
24602 " requires Same<T *, decltype(new "
24603 "T[n])>;\n"
24604 " { delete new T; };\n"
24605 " { delete new T[n]; };\n"
24606 " } && CopyConstructible<T> && "
24607 "CopyAssignable<T>;",
24608 Style);
24610 verifyFormat("template <typename T>\n"
24611 "concept Two = requires(T t) {\n"
24612 " { t.foo() } -> std::same_as<Bar>;\n"
24613 " } && requires(T &&t) {\n"
24614 " { t.foo() } -> std::same_as<Bar &&>;\n"
24615 " };",
24616 Style);
24618 verifyFormat(
24619 "template <typename T>\n"
24620 "concept C = requires(T x) {\n"
24621 " { *x } -> std::convertible_to<typename T::inner>;\n"
24622 " { x + 1 } noexcept -> std::same_as<int>;\n"
24623 " { x * 1 } -> std::convertible_to<T>;\n"
24624 " };",
24625 Style);
24627 verifyFormat("template <typename T>\n"
24628 "concept C = requires(T x) {\n"
24629 " {\n"
24630 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
24631 " } -> long_long_concept_name<T>;\n"
24632 " {\n"
24633 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
24634 " } noexcept -> long_long_concept_name<T>;\n"
24635 " };",
24636 Style);
24638 verifyFormat(
24639 "template <typename T, typename U = T>\n"
24640 "concept Swappable = requires(T &&t, U &&u) {\n"
24641 " swap(std::forward<T>(t), std::forward<U>(u));\n"
24642 " swap(std::forward<U>(u), std::forward<T>(t));\n"
24643 " };",
24644 Style);
24646 verifyFormat("template <typename T, typename U>\n"
24647 "concept Common = requires(T &&t, U &&u) {\n"
24648 " typename CommonType<T, U>;\n"
24649 " { CommonType<T, U>(std::forward<T>(t)) };\n"
24650 " };",
24651 Style);
24653 verifyFormat("template <typename T, typename U>\n"
24654 "concept Common = requires(T &&t, U &&u) {\n"
24655 " typename CommonType<T, U>;\n"
24656 " { CommonType<T, U>{std::forward<T>(t)} };\n"
24657 " };",
24658 Style);
24660 verifyFormat(
24661 "template <typename T>\n"
24662 "concept C = requires(T t) {\n"
24663 " requires Bar<T> && Foo<T>;\n"
24664 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24665 " };",
24666 Style);
24668 verifyFormat("template <typename T>\n"
24669 "concept HasFoo = requires(T t) {\n"
24670 " { t.foo() };\n"
24671 " t.foo();\n"
24672 " };\n"
24673 "template <typename T>\n"
24674 "concept HasBar = requires(T t) {\n"
24675 " { t.bar() };\n"
24676 " t.bar();\n"
24677 " };",
24678 Style);
24680 verifyFormat("template <typename T>\n"
24681 "concept Large = sizeof(T) > 10;");
24683 verifyFormat("template <typename T, typename U>\n"
24684 "concept FooableWith = requires(T t, U u) {\n"
24685 " typename T::foo_type;\n"
24686 " { t.foo(u) } -> typename T::foo_type;\n"
24687 " t++;\n"
24688 " };\n"
24689 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
24690 Style);
24692 verifyFormat("template <typename T>\n"
24693 "concept Context = is_specialization_of_v<context, T>;");
24695 verifyFormat("template <typename T>\n"
24696 "concept Node = std::is_object_v<T>;");
24698 verifyFormat("template <class T>\n"
24699 "concept integral = __is_integral(T);");
24701 verifyFormat("template <class T>\n"
24702 "concept is2D = __array_extent(T, 1) == 2;");
24704 verifyFormat("template <class T>\n"
24705 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
24707 verifyFormat("template <class T, class T2>\n"
24708 "concept Same = __is_same_as<T, T2>;");
24710 verifyFormat(
24711 "template <class _InIt, class _OutIt>\n"
24712 "concept _Can_reread_dest =\n"
24713 " std::forward_iterator<_OutIt> &&\n"
24714 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
24716 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
24718 verifyFormat(
24719 "template <typename T>\n"
24720 "concept C = requires(T t) {\n"
24721 " requires Bar<T> && Foo<T>;\n"
24722 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24723 " };",
24724 Style);
24726 verifyFormat("template <typename T>\n"
24727 "concept HasFoo = requires(T t) {\n"
24728 " { t.foo() };\n"
24729 " t.foo();\n"
24730 " };\n"
24731 "template <typename T>\n"
24732 "concept HasBar = requires(T t) {\n"
24733 " { t.bar() };\n"
24734 " t.bar();\n"
24735 " };",
24736 Style);
24738 verifyFormat("template <typename T> concept True = true;", Style);
24740 verifyFormat("template <typename T>\n"
24741 "concept C = decltype([]() -> std::true_type { return {}; "
24742 "}())::value &&\n"
24743 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24744 Style);
24746 verifyFormat("template <typename T>\n"
24747 "concept Semiregular =\n"
24748 " DefaultConstructible<T> && CopyConstructible<T> && "
24749 "CopyAssignable<T> &&\n"
24750 " requires(T a, std::size_t n) {\n"
24751 " requires Same<T *, decltype(&a)>;\n"
24752 " { a.~T() } noexcept;\n"
24753 " requires Same<T *, decltype(new T)>;\n"
24754 " requires Same<T *, decltype(new T[n])>;\n"
24755 " { delete new T; };\n"
24756 " { delete new T[n]; };\n"
24757 " };",
24758 Style);
24760 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
24762 verifyFormat("template <typename T> concept C =\n"
24763 " requires(T t) {\n"
24764 " requires Bar<T> && Foo<T>;\n"
24765 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24766 " };",
24767 Style);
24769 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
24770 " { t.foo() };\n"
24771 " t.foo();\n"
24772 " };\n"
24773 "template <typename T> concept HasBar = requires(T t) {\n"
24774 " { t.bar() };\n"
24775 " t.bar();\n"
24776 " };",
24777 Style);
24779 verifyFormat("template <typename T> concept True = true;", Style);
24781 verifyFormat(
24782 "template <typename T> concept C =\n"
24783 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
24784 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24785 Style);
24787 verifyFormat("template <typename T> concept Semiregular =\n"
24788 " DefaultConstructible<T> && CopyConstructible<T> && "
24789 "CopyAssignable<T> &&\n"
24790 " requires(T a, std::size_t n) {\n"
24791 " requires Same<T *, decltype(&a)>;\n"
24792 " { a.~T() } noexcept;\n"
24793 " requires Same<T *, decltype(new T)>;\n"
24794 " requires Same<T *, decltype(new T[n])>;\n"
24795 " { delete new T; };\n"
24796 " { delete new T[n]; };\n"
24797 " };",
24798 Style);
24800 // The following tests are invalid C++, we just want to make sure we don't
24801 // assert.
24802 verifyNoCrash("template <typename T>\n"
24803 "concept C = requires C2<T>;");
24805 verifyNoCrash("template <typename T>\n"
24806 "concept C = 5 + 4;");
24808 verifyNoCrash("template <typename T>\n"
24809 "concept C = class X;");
24811 verifyNoCrash("template <typename T>\n"
24812 "concept C = [] && true;");
24814 verifyNoCrash("template <typename T>\n"
24815 "concept C = [] && requires(T t) { typename T::size_type; };");
24818 TEST_F(FormatTest, RequiresClausesPositions) {
24819 auto Style = getLLVMStyle();
24820 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
24821 EXPECT_EQ(Style.IndentRequiresClause, true);
24823 // The default in LLVM style is REI_OuterScope, but these tests were written
24824 // when the default was REI_Keyword.
24825 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
24827 verifyFormat("template <typename T>\n"
24828 " requires(Foo<T> && std::trait<T>)\n"
24829 "struct Bar;",
24830 Style);
24832 verifyFormat("template <typename T>\n"
24833 " requires(Foo<T> && std::trait<T>)\n"
24834 "class Bar {\n"
24835 "public:\n"
24836 " Bar(T t);\n"
24837 " bool baz();\n"
24838 "};",
24839 Style);
24841 verifyFormat(
24842 "template <typename T>\n"
24843 " requires requires(T &&t) {\n"
24844 " typename T::I;\n"
24845 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
24846 " }\n"
24847 "Bar(T) -> Bar<typename T::I>;",
24848 Style);
24850 verifyFormat("template <typename T>\n"
24851 " requires(Foo<T> && std::trait<T>)\n"
24852 "constexpr T MyGlobal;",
24853 Style);
24855 verifyFormat("template <typename T>\n"
24856 " requires Foo<T> && requires(T t) {\n"
24857 " { t.baz() } -> std::same_as<bool>;\n"
24858 " requires std::same_as<T::Factor, int>;\n"
24859 " }\n"
24860 "inline int bar(T t) {\n"
24861 " return t.baz() ? T::Factor : 5;\n"
24862 "}",
24863 Style);
24865 verifyFormat("template <typename T>\n"
24866 "inline int bar(T t)\n"
24867 " requires Foo<T> && requires(T t) {\n"
24868 " { t.baz() } -> std::same_as<bool>;\n"
24869 " requires std::same_as<T::Factor, int>;\n"
24870 " }\n"
24871 "{\n"
24872 " return t.baz() ? T::Factor : 5;\n"
24873 "}",
24874 Style);
24876 verifyFormat("template <typename T>\n"
24877 " requires F<T>\n"
24878 "int bar(T t) {\n"
24879 " return 5;\n"
24880 "}",
24881 Style);
24883 verifyFormat("template <typename T>\n"
24884 "int bar(T t)\n"
24885 " requires F<T>\n"
24886 "{\n"
24887 " return 5;\n"
24888 "}",
24889 Style);
24891 verifyFormat("template <typename T>\n"
24892 "int S::bar(T t) &&\n"
24893 " requires F<T>\n"
24894 "{\n"
24895 " return 5;\n"
24896 "}",
24897 Style);
24899 verifyFormat("template <typename T>\n"
24900 "int bar(T t)\n"
24901 " requires F<T>;",
24902 Style);
24904 Style.IndentRequiresClause = false;
24905 verifyFormat("template <typename T>\n"
24906 "requires F<T>\n"
24907 "int bar(T t) {\n"
24908 " return 5;\n"
24909 "}",
24910 Style);
24912 verifyFormat("template <typename T>\n"
24913 "int S::bar(T t) &&\n"
24914 "requires F<T>\n"
24915 "{\n"
24916 " return 5;\n"
24917 "}",
24918 Style);
24920 verifyFormat("template <typename T>\n"
24921 "int bar(T t)\n"
24922 "requires F<T>\n"
24923 "{\n"
24924 " return 5;\n"
24925 "}",
24926 Style);
24928 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
24929 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
24930 "template <typename T> requires Foo<T> void bar() {}\n"
24931 "template <typename T> void bar() requires Foo<T> {}\n"
24932 "template <typename T> void bar() requires Foo<T>;\n"
24933 "template <typename T> void S::bar() && requires Foo<T> {}\n"
24934 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
24935 Style);
24937 auto ColumnStyle = Style;
24938 ColumnStyle.ColumnLimit = 40;
24939 verifyFormat("template <typename AAAAAAA>\n"
24940 "requires Foo<T> struct Bar {};\n"
24941 "template <typename AAAAAAA>\n"
24942 "requires Foo<T> void bar() {}\n"
24943 "template <typename AAAAAAA>\n"
24944 "void bar() requires Foo<T> {}\n"
24945 "template <typename T>\n"
24946 "void S::bar() && requires Foo<T> {}\n"
24947 "template <typename AAAAAAA>\n"
24948 "requires Foo<T> Baz(T) -> Baz<T>;",
24949 ColumnStyle);
24951 verifyFormat("template <typename T>\n"
24952 "requires Foo<AAAAAAA> struct Bar {};\n"
24953 "template <typename T>\n"
24954 "requires Foo<AAAAAAA> void bar() {}\n"
24955 "template <typename T>\n"
24956 "void bar() requires Foo<AAAAAAA> {}\n"
24957 "template <typename T>\n"
24958 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
24959 ColumnStyle);
24961 verifyFormat("template <typename AAAAAAA>\n"
24962 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24963 "struct Bar {};\n"
24964 "template <typename AAAAAAA>\n"
24965 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24966 "void bar() {}\n"
24967 "template <typename AAAAAAA>\n"
24968 "void bar()\n"
24969 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
24970 "template <typename AAAAAAA>\n"
24971 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
24972 "template <typename AAAAAAA>\n"
24973 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24974 "Bar(T) -> Bar<T>;",
24975 ColumnStyle);
24977 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
24978 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
24980 verifyFormat("template <typename T>\n"
24981 "requires Foo<T> struct Bar {};\n"
24982 "template <typename T>\n"
24983 "requires Foo<T> void bar() {}\n"
24984 "template <typename T>\n"
24985 "void bar()\n"
24986 "requires Foo<T> {}\n"
24987 "template <typename T>\n"
24988 "void bar()\n"
24989 "requires Foo<T>;\n"
24990 "template <typename T>\n"
24991 "void S::bar() &&\n"
24992 "requires Foo<T> {}\n"
24993 "template <typename T>\n"
24994 "requires Foo<T> Bar(T) -> Bar<T>;",
24995 Style);
24997 verifyFormat("template <typename AAAAAAA>\n"
24998 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24999 "struct Bar {};\n"
25000 "template <typename AAAAAAA>\n"
25001 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25002 "void bar() {}\n"
25003 "template <typename AAAAAAA>\n"
25004 "void bar()\n"
25005 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25006 "template <typename AAAAAAA>\n"
25007 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
25008 "template <typename AAAAAAA>\n"
25009 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25010 "Bar(T) -> Bar<T>;",
25011 ColumnStyle);
25013 Style.IndentRequiresClause = true;
25014 ColumnStyle.IndentRequiresClause = true;
25016 verifyFormat("template <typename T>\n"
25017 " requires Foo<T> struct Bar {};\n"
25018 "template <typename T>\n"
25019 " requires Foo<T> void bar() {}\n"
25020 "template <typename T>\n"
25021 "void bar()\n"
25022 " requires Foo<T> {}\n"
25023 "template <typename T>\n"
25024 "void S::bar() &&\n"
25025 " requires Foo<T> {}\n"
25026 "template <typename T>\n"
25027 " requires Foo<T> Bar(T) -> Bar<T>;",
25028 Style);
25030 verifyFormat("template <typename AAAAAAA>\n"
25031 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25032 "struct Bar {};\n"
25033 "template <typename AAAAAAA>\n"
25034 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25035 "void bar() {}\n"
25036 "template <typename AAAAAAA>\n"
25037 "void bar()\n"
25038 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25039 "template <typename AAAAAAA>\n"
25040 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
25041 "template <typename AAAAAAA>\n"
25042 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25043 "Bar(T) -> Bar<T>;",
25044 ColumnStyle);
25046 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
25047 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
25049 verifyFormat("template <typename T> requires Foo<T>\n"
25050 "struct Bar {};\n"
25051 "template <typename T> requires Foo<T>\n"
25052 "void bar() {}\n"
25053 "template <typename T>\n"
25054 "void bar() requires Foo<T>\n"
25055 "{}\n"
25056 "template <typename T> void bar() requires Foo<T>;\n"
25057 "template <typename T>\n"
25058 "void S::bar() && requires Foo<T>\n"
25059 "{}\n"
25060 "template <typename T> requires Foo<T>\n"
25061 "Bar(T) -> Bar<T>;",
25062 Style);
25064 verifyFormat("template <typename AAAAAAA>\n"
25065 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25066 "struct Bar {};\n"
25067 "template <typename AAAAAAA>\n"
25068 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25069 "void bar() {}\n"
25070 "template <typename AAAAAAA>\n"
25071 "void bar()\n"
25072 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25073 "{}\n"
25074 "template <typename AAAAAAA>\n"
25075 "requires Foo<AAAAAAAA>\n"
25076 "Bar(T) -> Bar<T>;\n"
25077 "template <typename AAAAAAA>\n"
25078 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25079 "Bar(T) -> Bar<T>;",
25080 ColumnStyle);
25083 TEST_F(FormatTest, RequiresClauses) {
25084 verifyFormat("struct [[nodiscard]] zero_t {\n"
25085 " template <class T>\n"
25086 " requires requires { number_zero_v<T>; }\n"
25087 " [[nodiscard]] constexpr operator T() const {\n"
25088 " return number_zero_v<T>;\n"
25089 " }\n"
25090 "};");
25092 verifyFormat("template <class T>\n"
25093 " requires(std::same_as<int, T>)\n"
25094 "decltype(auto) fun() {}");
25096 auto Style = getLLVMStyle();
25098 verifyFormat(
25099 "template <typename T>\n"
25100 " requires is_default_constructible_v<hash<T>> and\n"
25101 " is_copy_constructible_v<hash<T>> and\n"
25102 " is_move_constructible_v<hash<T>> and\n"
25103 " is_copy_assignable_v<hash<T>> and "
25104 "is_move_assignable_v<hash<T>> and\n"
25105 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
25106 " is_callable_v<hash<T>(T)> and\n"
25107 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
25108 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
25109 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
25110 "struct S {};",
25111 Style);
25113 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
25114 verifyFormat(
25115 "template <typename T>\n"
25116 " requires is_default_constructible_v<hash<T>>\n"
25117 " and is_copy_constructible_v<hash<T>>\n"
25118 " and is_move_constructible_v<hash<T>>\n"
25119 " and is_copy_assignable_v<hash<T>> and "
25120 "is_move_assignable_v<hash<T>>\n"
25121 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
25122 " and is_callable_v<hash<T>(T)>\n"
25123 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
25124 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
25125 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
25126 "&>()))>\n"
25127 "struct S {};",
25128 Style);
25130 Style = getLLVMStyle();
25131 Style.ConstructorInitializerIndentWidth = 4;
25132 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
25133 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
25134 verifyFormat("constexpr Foo(Foo const &other)\n"
25135 " requires std::is_copy_constructible<T>\n"
25136 " : value{other.value} {\n"
25137 " do_magic();\n"
25138 " do_more_magic();\n"
25139 "}",
25140 Style);
25142 // Not a clause, but we once hit an assert.
25143 verifyFormat("#if 0\n"
25144 "#else\n"
25145 "foo();\n"
25146 "#endif\n"
25147 "bar(requires);");
25150 TEST_F(FormatTest, RequiresExpressionIndentation) {
25151 auto Style = getLLVMStyle();
25152 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
25154 verifyFormat("template <typename T>\n"
25155 "concept C = requires(T t) {\n"
25156 " typename T::value;\n"
25157 " requires requires(typename T::value v) {\n"
25158 " { t == v } -> std::same_as<bool>;\n"
25159 " };\n"
25160 "};",
25161 Style);
25163 verifyFormat("template <typename T>\n"
25164 "void bar(T)\n"
25165 " requires Foo<T> && requires(T t) {\n"
25166 " { t.foo() } -> std::same_as<int>;\n"
25167 " } && requires(T t) {\n"
25168 " { t.bar() } -> std::same_as<bool>;\n"
25169 " --t;\n"
25170 " };",
25171 Style);
25173 verifyFormat("template <typename T>\n"
25174 " requires Foo<T> &&\n"
25175 " requires(T t) {\n"
25176 " { t.foo() } -> std::same_as<int>;\n"
25177 " } && requires(T t) {\n"
25178 " { t.bar() } -> std::same_as<bool>;\n"
25179 " --t;\n"
25180 " }\n"
25181 "void bar(T);",
25182 Style);
25184 verifyFormat("template <typename T> void f() {\n"
25185 " if constexpr (requires(T t) {\n"
25186 " { t.bar() } -> std::same_as<bool>;\n"
25187 " }) {\n"
25188 " }\n"
25189 "}",
25190 Style);
25192 verifyFormat("template <typename T> void f() {\n"
25193 " if constexpr (condition && requires(T t) {\n"
25194 " { t.bar() } -> std::same_as<bool>;\n"
25195 " }) {\n"
25196 " }\n"
25197 "}",
25198 Style);
25200 verifyFormat("template <typename T> struct C {\n"
25201 " void f()\n"
25202 " requires requires(T t) {\n"
25203 " { t.bar() } -> std::same_as<bool>;\n"
25204 " };\n"
25205 "};",
25206 Style);
25208 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25210 verifyFormat("template <typename T>\n"
25211 "concept C = requires(T t) {\n"
25212 " typename T::value;\n"
25213 " requires requires(typename T::value v) {\n"
25214 " { t == v } -> std::same_as<bool>;\n"
25215 " };\n"
25216 " };",
25217 Style);
25219 verifyFormat(
25220 "template <typename T>\n"
25221 "void bar(T)\n"
25222 " requires Foo<T> && requires(T t) {\n"
25223 " { t.foo() } -> std::same_as<int>;\n"
25224 " } && requires(T t) {\n"
25225 " { t.bar() } -> std::same_as<bool>;\n"
25226 " --t;\n"
25227 " };",
25228 Style);
25230 verifyFormat("template <typename T>\n"
25231 " requires Foo<T> &&\n"
25232 " requires(T t) {\n"
25233 " { t.foo() } -> std::same_as<int>;\n"
25234 " } && requires(T t) {\n"
25235 " { t.bar() } -> std::same_as<bool>;\n"
25236 " --t;\n"
25237 " }\n"
25238 "void bar(T);",
25239 Style);
25241 verifyFormat("template <typename T> void f() {\n"
25242 " if constexpr (requires(T t) {\n"
25243 " { t.bar() } -> std::same_as<bool>;\n"
25244 " }) {\n"
25245 " }\n"
25246 "}",
25247 Style);
25249 verifyFormat(
25250 "template <typename T> void f() {\n"
25251 " if constexpr (condition && requires(T t) {\n"
25252 " { t.bar() } -> std::same_as<bool>;\n"
25253 " }) {\n"
25254 " }\n"
25255 "}",
25256 Style);
25258 verifyFormat("template <typename T> struct C {\n"
25259 " void f()\n"
25260 " requires requires(T t) {\n"
25261 " { t.bar() } -> std::same_as<bool>;\n"
25262 " };\n"
25263 "};",
25264 Style);
25267 TEST_F(FormatTest, StatementAttributeLikeMacros) {
25268 FormatStyle Style = getLLVMStyle();
25269 StringRef Source = "void Foo::slot() {\n"
25270 " unsigned char MyChar = 'x';\n"
25271 " emit signal(MyChar);\n"
25272 " Q_EMIT signal(MyChar);\n"
25273 "}";
25275 verifyFormat(Source, Style);
25277 Style.AlignConsecutiveDeclarations.Enabled = true;
25278 verifyFormat("void Foo::slot() {\n"
25279 " unsigned char MyChar = 'x';\n"
25280 " emit signal(MyChar);\n"
25281 " Q_EMIT signal(MyChar);\n"
25282 "}",
25283 Source, Style);
25285 Style.StatementAttributeLikeMacros.push_back("emit");
25286 verifyFormat(Source, Style);
25288 Style.StatementAttributeLikeMacros = {};
25289 verifyFormat("void Foo::slot() {\n"
25290 " unsigned char MyChar = 'x';\n"
25291 " emit signal(MyChar);\n"
25292 " Q_EMIT signal(MyChar);\n"
25293 "}",
25294 Source, Style);
25297 TEST_F(FormatTest, IndentAccessModifiers) {
25298 FormatStyle Style = getLLVMStyle();
25299 Style.IndentAccessModifiers = true;
25300 // Members are *two* levels below the record;
25301 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
25302 verifyFormat("class C {\n"
25303 " int i;\n"
25304 "};",
25305 Style);
25306 verifyFormat("union C {\n"
25307 " int i;\n"
25308 " unsigned u;\n"
25309 "};",
25310 Style);
25311 // Access modifiers should be indented one level below the record.
25312 verifyFormat("class C {\n"
25313 " public:\n"
25314 " int i;\n"
25315 "};",
25316 Style);
25317 verifyFormat("class C {\n"
25318 " public /* comment */:\n"
25319 " int i;\n"
25320 "};",
25321 Style);
25322 verifyFormat("struct S {\n"
25323 " private:\n"
25324 " class C {\n"
25325 " int j;\n"
25326 "\n"
25327 " public:\n"
25328 " C();\n"
25329 " };\n"
25330 "\n"
25331 " public:\n"
25332 " int i;\n"
25333 "};",
25334 Style);
25335 // Enumerations are not records and should be unaffected.
25336 Style.AllowShortEnumsOnASingleLine = false;
25337 verifyFormat("enum class E {\n"
25338 " A,\n"
25339 " B\n"
25340 "};",
25341 Style);
25342 // Test with a different indentation width;
25343 // also proves that the result is Style.AccessModifierOffset agnostic.
25344 Style.IndentWidth = 3;
25345 verifyFormat("class C {\n"
25346 " public:\n"
25347 " int i;\n"
25348 "};",
25349 Style);
25350 verifyFormat("class C {\n"
25351 " public /**/:\n"
25352 " int i;\n"
25353 "};",
25354 Style);
25357 TEST_F(FormatTest, LimitlessStringsAndComments) {
25358 auto Style = getLLVMStyleWithColumns(0);
25359 constexpr StringRef Code =
25360 "/**\n"
25361 " * This is a multiline comment with quite some long lines, at least for "
25362 "the LLVM Style.\n"
25363 " * We will redo this with strings and line comments. Just to check if "
25364 "everything is working.\n"
25365 " */\n"
25366 "bool foo() {\n"
25367 " /* Single line multi line comment. */\n"
25368 " const std::string String = \"This is a multiline string with quite "
25369 "some long lines, at least for the LLVM Style.\"\n"
25370 " \"We already did it with multi line "
25371 "comments, and we will do it with line comments. Just to check if "
25372 "everything is working.\";\n"
25373 " // This is a line comment (block) with quite some long lines, at "
25374 "least for the LLVM Style.\n"
25375 " // We already did this with multi line comments and strings. Just to "
25376 "check if everything is working.\n"
25377 " const std::string SmallString = \"Hello World\";\n"
25378 " // Small line comment\n"
25379 " return String.size() > SmallString.size();\n"
25380 "}";
25381 verifyNoChange(Code, Style);
25384 TEST_F(FormatTest, FormatDecayCopy) {
25385 // error cases from unit tests
25386 verifyFormat("foo(auto())");
25387 verifyFormat("foo(auto{})");
25388 verifyFormat("foo(auto({}))");
25389 verifyFormat("foo(auto{{}})");
25391 verifyFormat("foo(auto(1))");
25392 verifyFormat("foo(auto{1})");
25393 verifyFormat("foo(new auto(1))");
25394 verifyFormat("foo(new auto{1})");
25395 verifyFormat("decltype(auto(1)) x;");
25396 verifyFormat("decltype(auto{1}) x;");
25397 verifyFormat("auto(x);");
25398 verifyFormat("auto{x};");
25399 verifyFormat("new auto{x};");
25400 verifyFormat("auto{x} = y;");
25401 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
25402 // the user's own fault
25403 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
25404 // clearly the user's own fault
25405 verifyFormat("auto (*p)() = f;");
25408 TEST_F(FormatTest, Cpp20ModulesSupport) {
25409 FormatStyle Style = getLLVMStyle();
25410 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
25411 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
25413 verifyFormat("export import foo;", Style);
25414 verifyFormat("export import foo:bar;", Style);
25415 verifyFormat("export import foo.bar;", Style);
25416 verifyFormat("export import foo.bar:baz;", Style);
25417 verifyFormat("export import :bar;", Style);
25418 verifyFormat("export module foo:bar;", Style);
25419 verifyFormat("export module foo;", Style);
25420 verifyFormat("export module foo.bar;", Style);
25421 verifyFormat("export module foo.bar:baz;", Style);
25422 verifyFormat("export import <string_view>;", Style);
25423 verifyFormat("export import <Foo/Bar>;", Style);
25425 verifyFormat("export type_name var;", Style);
25426 verifyFormat("template <class T> export using A = B<T>;", Style);
25427 verifyFormat("export using A = B;", Style);
25428 verifyFormat("export int func() {\n"
25429 " foo();\n"
25430 "}",
25431 Style);
25432 verifyFormat("export struct {\n"
25433 " int foo;\n"
25434 "};",
25435 Style);
25436 verifyFormat("export {\n"
25437 " int foo;\n"
25438 "};",
25439 Style);
25440 verifyFormat("export export char const *hello() { return \"hello\"; }");
25442 verifyFormat("import bar;", Style);
25443 verifyFormat("import foo.bar;", Style);
25444 verifyFormat("import foo:bar;", Style);
25445 verifyFormat("import :bar;", Style);
25446 verifyFormat("import /* module partition */ :bar;", Style);
25447 verifyFormat("import <ctime>;", Style);
25448 verifyFormat("import \"header\";", Style);
25450 verifyFormat("module foo;", Style);
25451 verifyFormat("module foo:bar;", Style);
25452 verifyFormat("module foo.bar;", Style);
25453 verifyFormat("module;", Style);
25455 verifyFormat("export namespace hi {\n"
25456 "const char *sayhi();\n"
25457 "}",
25458 Style);
25460 verifyFormat("module :private;", Style);
25461 verifyFormat("import <foo/bar.h>;", Style);
25462 verifyFormat("import foo...bar;", Style);
25463 verifyFormat("import ..........;", Style);
25464 verifyFormat("module foo:private;", Style);
25465 verifyFormat("import a", Style);
25466 verifyFormat("module a", Style);
25467 verifyFormat("export import a", Style);
25468 verifyFormat("export module a", Style);
25470 verifyFormat("import", Style);
25471 verifyFormat("module", Style);
25472 verifyFormat("export", Style);
25474 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
25477 TEST_F(FormatTest, CoroutineForCoawait) {
25478 FormatStyle Style = getLLVMStyle();
25479 verifyFormat("for co_await (auto x : range())\n ;");
25480 verifyFormat("for (auto i : arr) {\n"
25481 "}",
25482 Style);
25483 verifyFormat("for co_await (auto i : arr) {\n"
25484 "}",
25485 Style);
25486 verifyFormat("for co_await (auto i : foo(T{})) {\n"
25487 "}",
25488 Style);
25491 TEST_F(FormatTest, CoroutineCoAwait) {
25492 verifyFormat("int x = co_await foo();");
25493 verifyFormat("int x = (co_await foo());");
25494 verifyFormat("co_await (42);");
25495 verifyFormat("void operator co_await(int);");
25496 verifyFormat("void operator co_await(a);");
25497 verifyFormat("co_await a;");
25498 verifyFormat("co_await missing_await_resume{};");
25499 verifyFormat("co_await a; // comment");
25500 verifyFormat("void test0() { co_await a; }");
25501 verifyFormat("co_await co_await co_await foo();");
25502 verifyFormat("co_await foo().bar();");
25503 verifyFormat("co_await [this]() -> Task { co_return x; }");
25504 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
25505 "foo(); }(x, y);");
25507 FormatStyle Style = getLLVMStyleWithColumns(40);
25508 verifyFormat("co_await [this](int a, int b) -> Task {\n"
25509 " co_return co_await foo();\n"
25510 "}(x, y);",
25511 Style);
25512 verifyFormat("co_await;");
25515 TEST_F(FormatTest, CoroutineCoYield) {
25516 verifyFormat("int x = co_yield foo();");
25517 verifyFormat("int x = (co_yield foo());");
25518 verifyFormat("co_yield (42);");
25519 verifyFormat("co_yield {42};");
25520 verifyFormat("co_yield 42;");
25521 verifyFormat("co_yield n++;");
25522 verifyFormat("co_yield ++n;");
25523 verifyFormat("co_yield;");
25526 TEST_F(FormatTest, CoroutineCoReturn) {
25527 verifyFormat("co_return (42);");
25528 verifyFormat("co_return;");
25529 verifyFormat("co_return {};");
25530 verifyFormat("co_return x;");
25531 verifyFormat("co_return co_await foo();");
25532 verifyFormat("co_return co_yield foo();");
25535 TEST_F(FormatTest, EmptyShortBlock) {
25536 auto Style = getLLVMStyle();
25537 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
25539 verifyFormat("try {\n"
25540 " doA();\n"
25541 "} catch (Exception &e) {\n"
25542 " e.printStackTrace();\n"
25543 "}",
25544 Style);
25546 verifyFormat("try {\n"
25547 " doA();\n"
25548 "} catch (Exception &e) {}",
25549 Style);
25552 TEST_F(FormatTest, ShortTemplatedArgumentLists) {
25553 auto Style = getLLVMStyle();
25555 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
25556 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
25557 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
25558 verifyFormat("struct Y<[] { return 0; }> {};", Style);
25560 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
25561 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
25564 TEST_F(FormatTest, MultilineLambdaInConditional) {
25565 auto Style = getLLVMStyleWithColumns(70);
25566 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
25567 " ;\n"
25568 " return 5;\n"
25569 "}()\n"
25570 " : 2;",
25571 Style);
25572 verifyFormat(
25573 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
25574 " ;\n"
25575 " return 5;\n"
25576 "}();",
25577 Style);
25579 Style = getLLVMStyleWithColumns(60);
25580 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
25581 " ? []() {\n"
25582 " ;\n"
25583 " return 5;\n"
25584 " }()\n"
25585 " : 2;",
25586 Style);
25587 verifyFormat("auto aLengthyIdentifier =\n"
25588 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
25589 " ;\n"
25590 " return 5;\n"
25591 " }();",
25592 Style);
25594 Style = getLLVMStyleWithColumns(40);
25595 verifyFormat("auto aLengthyIdentifier =\n"
25596 " oneExpressionSoThatWeBreak ? []() {\n"
25597 " ;\n"
25598 " return 5;\n"
25599 " }()\n"
25600 " : 2;",
25601 Style);
25602 verifyFormat("auto aLengthyIdentifier =\n"
25603 " oneExpressionSoThatWeBreak\n"
25604 " ? 2\n"
25605 " : []() {\n"
25606 " ;\n"
25607 " return 5;\n"
25608 " };",
25609 Style);
25612 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
25613 auto Style = getLLVMStyle();
25615 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
25616 "void functionDecl(int a, int b, int c);";
25618 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
25619 "paramF, paramG, paramH, paramI);\n"
25620 "void functionDecl(int argumentA, int argumentB, int "
25621 "argumentC, int argumentD, int argumentE);";
25623 verifyFormat(Short, Style);
25625 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
25626 "paramF, paramG, paramH,\n"
25627 " paramI);\n"
25628 "void functionDecl(int argumentA, int argumentB, int "
25629 "argumentC, int argumentD,\n"
25630 " int argumentE);";
25632 verifyFormat(NoBreak, Medium, Style);
25633 verifyFormat(NoBreak,
25634 "functionCall(\n"
25635 " paramA,\n"
25636 " paramB,\n"
25637 " paramC,\n"
25638 " paramD,\n"
25639 " paramE,\n"
25640 " paramF,\n"
25641 " paramG,\n"
25642 " paramH,\n"
25643 " paramI\n"
25644 ");\n"
25645 "void functionDecl(\n"
25646 " int argumentA,\n"
25647 " int argumentB,\n"
25648 " int argumentC,\n"
25649 " int argumentD,\n"
25650 " int argumentE\n"
25651 ");",
25652 Style);
25654 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
25655 " nestedLongFunctionCall(argument1, "
25656 "argument2, argument3,\n"
25657 " argument4, "
25658 "argument5));",
25659 Style);
25661 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
25663 verifyFormat(Short, Style);
25664 verifyFormat(
25665 "functionCall(\n"
25666 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
25667 "paramI\n"
25668 ");\n"
25669 "void functionDecl(\n"
25670 " int argumentA, int argumentB, int argumentC, int argumentD, int "
25671 "argumentE\n"
25672 ");",
25673 Medium, Style);
25675 Style.AllowAllArgumentsOnNextLine = false;
25676 Style.AllowAllParametersOfDeclarationOnNextLine = false;
25678 verifyFormat(Short, Style);
25679 verifyFormat(
25680 "functionCall(\n"
25681 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
25682 "paramI\n"
25683 ");\n"
25684 "void functionDecl(\n"
25685 " int argumentA, int argumentB, int argumentC, int argumentD, int "
25686 "argumentE\n"
25687 ");",
25688 Medium, Style);
25690 Style.BinPackArguments = false;
25691 Style.BinPackParameters = false;
25693 verifyFormat(Short, Style);
25695 verifyFormat("functionCall(\n"
25696 " paramA,\n"
25697 " paramB,\n"
25698 " paramC,\n"
25699 " paramD,\n"
25700 " paramE,\n"
25701 " paramF,\n"
25702 " paramG,\n"
25703 " paramH,\n"
25704 " paramI\n"
25705 ");\n"
25706 "void functionDecl(\n"
25707 " int argumentA,\n"
25708 " int argumentB,\n"
25709 " int argumentC,\n"
25710 " int argumentD,\n"
25711 " int argumentE\n"
25712 ");",
25713 Medium, Style);
25715 verifyFormat("outerFunctionCall(\n"
25716 " nestedFunctionCall(argument1),\n"
25717 " nestedLongFunctionCall(\n"
25718 " argument1,\n"
25719 " argument2,\n"
25720 " argument3,\n"
25721 " argument4,\n"
25722 " argument5\n"
25723 " )\n"
25724 ");",
25725 Style);
25727 verifyFormat("int a = (int)b;", Style);
25728 verifyFormat("int a = (int)b;",
25729 "int a = (\n"
25730 " int\n"
25731 ") b;",
25732 Style);
25734 verifyFormat("return (true);", Style);
25735 verifyFormat("return (true);",
25736 "return (\n"
25737 " true\n"
25738 ");",
25739 Style);
25741 verifyFormat("void foo();", Style);
25742 verifyFormat("void foo();",
25743 "void foo(\n"
25744 ");",
25745 Style);
25747 verifyFormat("void foo() {}", Style);
25748 verifyFormat("void foo() {}",
25749 "void foo(\n"
25750 ") {\n"
25751 "}",
25752 Style);
25754 verifyFormat("auto string = std::string();", Style);
25755 verifyFormat("auto string = std::string();",
25756 "auto string = std::string(\n"
25757 ");",
25758 Style);
25760 verifyFormat("void (*functionPointer)() = nullptr;", Style);
25761 verifyFormat("void (*functionPointer)() = nullptr;",
25762 "void (\n"
25763 " *functionPointer\n"
25764 ")\n"
25765 "(\n"
25766 ") = nullptr;",
25767 Style);
25770 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
25771 auto Style = getLLVMStyle();
25773 verifyFormat("if (foo()) {\n"
25774 " return;\n"
25775 "}",
25776 Style);
25778 verifyFormat("if (quitelongarg !=\n"
25779 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25780 "comment\n"
25781 " return;\n"
25782 "}",
25783 Style);
25785 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
25787 verifyFormat("if (foo()) {\n"
25788 " return;\n"
25789 "}",
25790 Style);
25792 verifyFormat("if (quitelongarg !=\n"
25793 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25794 "comment\n"
25795 " return;\n"
25796 "}",
25797 Style);
25800 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
25801 auto Style = getLLVMStyle();
25803 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
25804 " doSomething();\n"
25805 "}",
25806 Style);
25808 verifyFormat("for (int myReallyLongCountVariable = 0; "
25809 "myReallyLongCountVariable < count;\n"
25810 " myReallyLongCountVariable++) {\n"
25811 " doSomething();\n"
25812 "}",
25813 Style);
25815 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
25817 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
25818 " doSomething();\n"
25819 "}",
25820 Style);
25822 verifyFormat("for (int myReallyLongCountVariable = 0; "
25823 "myReallyLongCountVariable < count;\n"
25824 " myReallyLongCountVariable++) {\n"
25825 " doSomething();\n"
25826 "}",
25827 Style);
25830 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
25831 auto Style = getLLVMStyleWithColumns(60);
25832 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
25833 // Aggregate initialization.
25834 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
25835 " 10000000, 20000000\n"
25836 "};",
25837 Style);
25838 verifyFormat("SomeStruct s{\n"
25839 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
25840 " \"zzzzzzzzzzzzzzzz\"\n"
25841 "};",
25842 Style);
25843 // Designated initializers.
25844 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
25845 " [0] = 10000000, [1] = 20000000\n"
25846 "};",
25847 Style);
25848 verifyFormat("SomeStruct s{\n"
25849 " .foo = \"xxxxxxxxxxxxx\",\n"
25850 " .bar = \"yyyyyyyyyyyyy\",\n"
25851 " .baz = \"zzzzzzzzzzzzz\"\n"
25852 "};",
25853 Style);
25854 // List initialization.
25855 verifyFormat("SomeStruct s{\n"
25856 " \"xxxxxxxxxxxxx\",\n"
25857 " \"yyyyyyyyyyyyy\",\n"
25858 " \"zzzzzzzzzzzzz\",\n"
25859 "};",
25860 Style);
25861 verifyFormat("SomeStruct{\n"
25862 " \"xxxxxxxxxxxxx\",\n"
25863 " \"yyyyyyyyyyyyy\",\n"
25864 " \"zzzzzzzzzzzzz\",\n"
25865 "};",
25866 Style);
25867 verifyFormat("new SomeStruct{\n"
25868 " \"xxxxxxxxxxxxx\",\n"
25869 " \"yyyyyyyyyyyyy\",\n"
25870 " \"zzzzzzzzzzzzz\",\n"
25871 "};",
25872 Style);
25873 // Member initializer.
25874 verifyFormat("class SomeClass {\n"
25875 " SomeStruct s{\n"
25876 " \"xxxxxxxxxxxxx\",\n"
25877 " \"yyyyyyyyyyyyy\",\n"
25878 " \"zzzzzzzzzzzzz\",\n"
25879 " };\n"
25880 "};",
25881 Style);
25882 // Constructor member initializer.
25883 verifyFormat("SomeClass::SomeClass : strct{\n"
25884 " \"xxxxxxxxxxxxx\",\n"
25885 " \"yyyyyyyyyyyyy\",\n"
25886 " \"zzzzzzzzzzzzz\",\n"
25887 " } {}",
25888 Style);
25889 // Copy initialization.
25890 verifyFormat("SomeStruct s = SomeStruct{\n"
25891 " \"xxxxxxxxxxxxx\",\n"
25892 " \"yyyyyyyyyyyyy\",\n"
25893 " \"zzzzzzzzzzzzz\",\n"
25894 "};",
25895 Style);
25896 // Copy list initialization.
25897 verifyFormat("SomeStruct s = {\n"
25898 " \"xxxxxxxxxxxxx\",\n"
25899 " \"yyyyyyyyyyyyy\",\n"
25900 " \"zzzzzzzzzzzzz\",\n"
25901 "};",
25902 Style);
25903 // Assignment operand initialization.
25904 verifyFormat("s = {\n"
25905 " \"xxxxxxxxxxxxx\",\n"
25906 " \"yyyyyyyyyyyyy\",\n"
25907 " \"zzzzzzzzzzzzz\",\n"
25908 "};",
25909 Style);
25910 // Returned object initialization.
25911 verifyFormat("return {\n"
25912 " \"xxxxxxxxxxxxx\",\n"
25913 " \"yyyyyyyyyyyyy\",\n"
25914 " \"zzzzzzzzzzzzz\",\n"
25915 "};",
25916 Style);
25917 // Initializer list.
25918 verifyFormat("auto initializerList = {\n"
25919 " \"xxxxxxxxxxxxx\",\n"
25920 " \"yyyyyyyyyyyyy\",\n"
25921 " \"zzzzzzzzzzzzz\",\n"
25922 "};",
25923 Style);
25924 // Function parameter initialization.
25925 verifyFormat("func({\n"
25926 " \"xxxxxxxxxxxxx\",\n"
25927 " \"yyyyyyyyyyyyy\",\n"
25928 " \"zzzzzzzzzzzzz\",\n"
25929 "});",
25930 Style);
25931 // Nested init lists.
25932 verifyFormat("SomeStruct s = {\n"
25933 " {{init1, init2, init3, init4, init5},\n"
25934 " {init1, init2, init3, init4, init5}}\n"
25935 "};",
25936 Style);
25937 verifyFormat("SomeStruct s = {\n"
25938 " {{\n"
25939 " .init1 = 1,\n"
25940 " .init2 = 2,\n"
25941 " .init3 = 3,\n"
25942 " .init4 = 4,\n"
25943 " .init5 = 5,\n"
25944 " },\n"
25945 " {init1, init2, init3, init4, init5}}\n"
25946 "};",
25947 Style);
25948 verifyFormat("SomeArrayT a[3] = {\n"
25949 " {\n"
25950 " foo,\n"
25951 " bar,\n"
25952 " },\n"
25953 " {\n"
25954 " foo,\n"
25955 " bar,\n"
25956 " },\n"
25957 " SomeArrayT{},\n"
25958 "};",
25959 Style);
25960 verifyFormat("SomeArrayT a[3] = {\n"
25961 " {foo},\n"
25962 " {\n"
25963 " {\n"
25964 " init1,\n"
25965 " init2,\n"
25966 " init3,\n"
25967 " },\n"
25968 " {\n"
25969 " init1,\n"
25970 " init2,\n"
25971 " init3,\n"
25972 " },\n"
25973 " },\n"
25974 " {baz},\n"
25975 "};",
25976 Style);
25979 TEST_F(FormatTest, UnderstandsDigraphs) {
25980 verifyFormat("int arr<:5:> = {};");
25981 verifyFormat("int arr[5] = <%%>;");
25982 verifyFormat("int arr<:::qualified_variable:> = {};");
25983 verifyFormat("int arr[::qualified_variable] = <%%>;");
25984 verifyFormat("%:include <header>");
25985 verifyFormat("%:define A x##y");
25986 verifyFormat("#define A x%:%:y");
25989 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
25990 auto Style = getLLVMStyle();
25991 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
25992 Style.AlignConsecutiveAssignments.Enabled = true;
25993 Style.AlignConsecutiveDeclarations.Enabled = true;
25995 // The AlignArray code is incorrect for non square Arrays and can cause
25996 // crashes, these tests assert that the array is not changed but will
25997 // also act as regression tests for when it is properly fixed
25998 verifyFormat("struct test demo[] = {\n"
25999 " {1, 2},\n"
26000 " {3, 4, 5},\n"
26001 " {6, 7, 8}\n"
26002 "};",
26003 Style);
26004 verifyFormat("struct test demo[] = {\n"
26005 " {1, 2, 3, 4, 5},\n"
26006 " {3, 4, 5},\n"
26007 " {6, 7, 8}\n"
26008 "};",
26009 Style);
26010 verifyFormat("struct test demo[] = {\n"
26011 " {1, 2, 3, 4, 5},\n"
26012 " {3, 4, 5},\n"
26013 " {6, 7, 8, 9, 10, 11, 12}\n"
26014 "};",
26015 Style);
26016 verifyFormat("struct test demo[] = {\n"
26017 " {1, 2, 3},\n"
26018 " {3, 4, 5},\n"
26019 " {6, 7, 8, 9, 10, 11, 12}\n"
26020 "};",
26021 Style);
26023 verifyFormat("S{\n"
26024 " {},\n"
26025 " {},\n"
26026 " {a, b}\n"
26027 "};",
26028 Style);
26029 verifyFormat("S{\n"
26030 " {},\n"
26031 " {},\n"
26032 " {a, b},\n"
26033 "};",
26034 Style);
26035 verifyFormat("void foo() {\n"
26036 " auto thing = test{\n"
26037 " {\n"
26038 " {13}, {something}, // A\n"
26039 " }\n"
26040 " };\n"
26041 "}",
26042 "void foo() {\n"
26043 " auto thing = test{\n"
26044 " {\n"
26045 " {13},\n"
26046 " {something}, // A\n"
26047 " }\n"
26048 " };\n"
26049 "}",
26050 Style);
26053 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
26054 auto Style = getLLVMStyle();
26055 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
26056 Style.AlignConsecutiveAssignments.Enabled = true;
26057 Style.AlignConsecutiveDeclarations.Enabled = true;
26059 // The AlignArray code is incorrect for non square Arrays and can cause
26060 // crashes, these tests assert that the array is not changed but will
26061 // also act as regression tests for when it is properly fixed
26062 verifyFormat("struct test demo[] = {\n"
26063 " {1, 2},\n"
26064 " {3, 4, 5},\n"
26065 " {6, 7, 8}\n"
26066 "};",
26067 Style);
26068 verifyFormat("struct test demo[] = {\n"
26069 " {1, 2, 3, 4, 5},\n"
26070 " {3, 4, 5},\n"
26071 " {6, 7, 8}\n"
26072 "};",
26073 Style);
26074 verifyFormat("struct test demo[] = {\n"
26075 " {1, 2, 3, 4, 5},\n"
26076 " {3, 4, 5},\n"
26077 " {6, 7, 8, 9, 10, 11, 12}\n"
26078 "};",
26079 Style);
26080 verifyFormat("struct test demo[] = {\n"
26081 " {1, 2, 3},\n"
26082 " {3, 4, 5},\n"
26083 " {6, 7, 8, 9, 10, 11, 12}\n"
26084 "};",
26085 Style);
26087 verifyFormat("S{\n"
26088 " {},\n"
26089 " {},\n"
26090 " {a, b}\n"
26091 "};",
26092 Style);
26093 verifyFormat("S{\n"
26094 " {},\n"
26095 " {},\n"
26096 " {a, b},\n"
26097 "};",
26098 Style);
26099 verifyFormat("void foo() {\n"
26100 " auto thing = test{\n"
26101 " {\n"
26102 " {13}, {something}, // A\n"
26103 " }\n"
26104 " };\n"
26105 "}",
26106 "void foo() {\n"
26107 " auto thing = test{\n"
26108 " {\n"
26109 " {13},\n"
26110 " {something}, // A\n"
26111 " }\n"
26112 " };\n"
26113 "}",
26114 Style);
26117 TEST_F(FormatTest, FormatsVariableTemplates) {
26118 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
26119 verifyFormat("template <typename T> "
26120 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
26123 TEST_F(FormatTest, RemoveSemicolon) {
26124 FormatStyle Style = getLLVMStyle();
26125 Style.RemoveSemicolon = true;
26127 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
26128 "int max(int a, int b) { return a > b ? a : b; };", Style);
26130 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
26131 "int max(int a, int b) { return a > b ? a : b; };;", Style);
26133 verifyFormat("class Foo {\n"
26134 " int getSomething() const { return something; }\n"
26135 "};",
26136 "class Foo {\n"
26137 " int getSomething() const { return something; };\n"
26138 "};",
26139 Style);
26141 verifyFormat("class Foo {\n"
26142 " int getSomething() const { return something; }\n"
26143 "};",
26144 "class Foo {\n"
26145 " int getSomething() const { return something; };;\n"
26146 "};",
26147 Style);
26149 verifyFormat("for (;;) {\n"
26150 "}",
26151 Style);
26153 verifyFormat("class [[deprecated(\"\")]] C {\n"
26154 " int i;\n"
26155 "};",
26156 Style);
26158 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
26159 " int i;\n"
26160 "};",
26161 Style);
26163 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
26165 // These tests are here to show a problem that may not be easily
26166 // solved, our implementation to remove semicolons is only as good
26167 // as our FunctionLBrace detection and this fails for empty braces
26168 // because we can't distringuish this from a bracelist.
26169 // We will enable when that is resolved.
26170 #if 0
26171 verifyFormat("void main() {}", "void main() {};", Style);
26172 verifyFormat("void foo() {} //\n"
26173 "int bar;",
26174 "void foo() {}; //\n"
26175 "; int bar;",
26176 Style);
26177 #endif
26180 TEST_F(FormatTest, BreakAfterAttributes) {
26181 FormatStyle Style = getLLVMStyle();
26183 constexpr StringRef Code("[[nodiscard]] inline int f(int &i);\n"
26184 "[[foo([[]])]] [[nodiscard]]\n"
26185 "int g(int &i);\n"
26186 "[[nodiscard]]\n"
26187 "inline int f(int &i) {\n"
26188 " i = 1;\n"
26189 " return 0;\n"
26190 "}\n"
26191 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26192 " i = 0;\n"
26193 " return 1;\n"
26194 "}");
26196 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
26197 verifyNoChange(Code, Style);
26199 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
26200 verifyFormat("[[nodiscard]] inline int f(int &i);\n"
26201 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
26202 "[[nodiscard]] inline int f(int &i) {\n"
26203 " i = 1;\n"
26204 " return 0;\n"
26205 "}\n"
26206 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26207 " i = 0;\n"
26208 " return 1;\n"
26209 "}",
26210 Code, Style);
26212 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
26213 verifyFormat("[[nodiscard]]\n"
26214 "inline int f(int &i);\n"
26215 "[[foo([[]])]] [[nodiscard]]\n"
26216 "int g(int &i);\n"
26217 "[[nodiscard]]\n"
26218 "inline int f(int &i) {\n"
26219 " i = 1;\n"
26220 " return 0;\n"
26221 "}\n"
26222 "[[foo([[]])]] [[nodiscard]]\n"
26223 "int g(int &i) {\n"
26224 " i = 0;\n"
26225 " return 1;\n"
26226 "}",
26227 Code, Style);
26229 constexpr StringRef CtorDtorCode("struct Foo {\n"
26230 " [[deprecated]] Foo();\n"
26231 " [[deprecated]] Foo() {}\n"
26232 " [[deprecated]] ~Foo();\n"
26233 " [[deprecated]] ~Foo() {}\n"
26234 " [[deprecated]] void f();\n"
26235 " [[deprecated]] void f() {}\n"
26236 "};\n"
26237 "[[deprecated]] Bar::Bar() {}\n"
26238 "[[deprecated]] Bar::~Bar() {}\n"
26239 "[[deprecated]] void g() {}");
26240 verifyFormat("struct Foo {\n"
26241 " [[deprecated]]\n"
26242 " Foo();\n"
26243 " [[deprecated]]\n"
26244 " Foo() {}\n"
26245 " [[deprecated]]\n"
26246 " ~Foo();\n"
26247 " [[deprecated]]\n"
26248 " ~Foo() {}\n"
26249 " [[deprecated]]\n"
26250 " void f();\n"
26251 " [[deprecated]]\n"
26252 " void f() {}\n"
26253 "};\n"
26254 "[[deprecated]]\n"
26255 "Bar::Bar() {}\n"
26256 "[[deprecated]]\n"
26257 "Bar::~Bar() {}\n"
26258 "[[deprecated]]\n"
26259 "void g() {}",
26260 CtorDtorCode, Style);
26262 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
26263 verifyFormat("struct Foo {\n"
26264 " [[deprecated]]\n"
26265 " Foo();\n"
26266 " [[deprecated]]\n"
26267 " Foo()\n"
26268 " {\n"
26269 " }\n"
26270 " [[deprecated]]\n"
26271 " ~Foo();\n"
26272 " [[deprecated]]\n"
26273 " ~Foo()\n"
26274 " {\n"
26275 " }\n"
26276 " [[deprecated]]\n"
26277 " void f();\n"
26278 " [[deprecated]]\n"
26279 " void f()\n"
26280 " {\n"
26281 " }\n"
26282 "};\n"
26283 "[[deprecated]]\n"
26284 "Bar::Bar()\n"
26285 "{\n"
26286 "}\n"
26287 "[[deprecated]]\n"
26288 "Bar::~Bar()\n"
26289 "{\n"
26290 "}\n"
26291 "[[deprecated]]\n"
26292 "void g()\n"
26293 "{\n"
26294 "}",
26295 CtorDtorCode, Style);
26298 TEST_F(FormatTest, InsertNewlineAtEOF) {
26299 FormatStyle Style = getLLVMStyle();
26300 Style.InsertNewlineAtEOF = true;
26302 verifyNoChange("int i;\n", Style);
26303 verifyFormat("int i;\n", "int i;", Style);
26306 TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
26307 FormatStyle Style = getLLVMStyle();
26308 Style.KeepEmptyLinesAtEOF = true;
26310 const StringRef Code{"int i;\n\n"};
26311 verifyNoChange(Code, Style);
26312 verifyFormat(Code, "int i;\n\n\n", Style);
26315 TEST_F(FormatTest, SpaceAfterUDL) {
26316 verifyFormat("auto c = (4s).count();");
26317 verifyFormat("auto x = 5s .count() == 5;");
26320 TEST_F(FormatTest, InterfaceAsClassMemberName) {
26321 verifyFormat("class Foo {\n"
26322 " int interface;\n"
26323 " Foo::Foo(int iface) : interface{iface} {}\n"
26324 "}");
26327 TEST_F(FormatTest, PreprocessorOverlappingRegions) {
26328 verifyFormat("#ifdef\n\n"
26329 "#else\n"
26330 "#endif",
26331 "#ifdef \n"
26332 " \n"
26333 "\n"
26334 "#else \n"
26335 "#endif ",
26336 getGoogleStyle());
26339 TEST_F(FormatTest, RemoveParentheses) {
26340 FormatStyle Style = getLLVMStyle();
26341 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
26343 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
26344 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
26345 verifyFormat("decltype((foo->bar)) baz;", Style);
26346 verifyFormat("class __declspec(dllimport) X {};",
26347 "class __declspec((dllimport)) X {};", Style);
26348 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
26349 verifyFormat("while (a)\n"
26350 " b;",
26351 "while (((a)))\n"
26352 " b;",
26353 Style);
26354 verifyFormat("while ((a = b))\n"
26355 " c;",
26356 "while (((a = b)))\n"
26357 " c;",
26358 Style);
26359 verifyFormat("if (a)\n"
26360 " b;",
26361 "if (((a)))\n"
26362 " b;",
26363 Style);
26364 verifyFormat("if constexpr ((a = b))\n"
26365 " c;",
26366 "if constexpr (((a = b)))\n"
26367 " c;",
26368 Style);
26369 verifyFormat("if (({ a; }))\n"
26370 " b;",
26371 "if ((({ a; })))\n"
26372 " b;",
26373 Style);
26374 verifyFormat("return (0);", "return (((0)));", Style);
26375 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
26377 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
26378 verifyFormat("return 0;", "return (0);", Style);
26379 verifyFormat("co_return 0;", "co_return ((0));", Style);
26380 verifyFormat("return 0;", "return (((0)));", Style);
26381 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
26382 verifyFormat("inline decltype(auto) f() {\n"
26383 " if (a) {\n"
26384 " return (a);\n"
26385 " }\n"
26386 " return (b);\n"
26387 "}",
26388 "inline decltype(auto) f() {\n"
26389 " if (a) {\n"
26390 " return ((a));\n"
26391 " }\n"
26392 " return ((b));\n"
26393 "}",
26394 Style);
26395 verifyFormat("auto g() {\n"
26396 " decltype(auto) x = [] {\n"
26397 " auto y = [] {\n"
26398 " if (a) {\n"
26399 " return a;\n"
26400 " }\n"
26401 " return b;\n"
26402 " };\n"
26403 " if (c) {\n"
26404 " return (c);\n"
26405 " }\n"
26406 " return (d);\n"
26407 " };\n"
26408 " if (e) {\n"
26409 " return e;\n"
26410 " }\n"
26411 " return f;\n"
26412 "}",
26413 "auto g() {\n"
26414 " decltype(auto) x = [] {\n"
26415 " auto y = [] {\n"
26416 " if (a) {\n"
26417 " return ((a));\n"
26418 " }\n"
26419 " return ((b));\n"
26420 " };\n"
26421 " if (c) {\n"
26422 " return ((c));\n"
26423 " }\n"
26424 " return ((d));\n"
26425 " };\n"
26426 " if (e) {\n"
26427 " return ((e));\n"
26428 " }\n"
26429 " return ((f));\n"
26430 "}",
26431 Style);
26433 Style.ColumnLimit = 25;
26434 verifyFormat("return (a + b) - (c + d);",
26435 "return (((a + b)) -\n"
26436 " ((c + d)));",
26437 Style);
26440 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
26441 auto Style = getLLVMStyleWithColumns(35);
26443 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
26444 verifyFormat("void foo(int arg1,\n"
26445 " double arg2) noexcept;",
26446 Style);
26448 // The following line does not fit within the 35 column limit, but that's what
26449 // happens with no break allowed.
26450 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
26451 " noexcept(baz(arg1)) &&\n"
26452 " noexcept(baz(arg2)));",
26453 Style);
26455 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
26456 Style);
26458 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
26459 verifyFormat("void foo(int arg1,\n"
26460 " double arg2) noexcept;",
26461 Style);
26463 verifyFormat("void bar(int arg1, double arg2)\n"
26464 " noexcept(noexcept(baz(arg1)) &&\n"
26465 " noexcept(baz(arg2)));",
26466 Style);
26468 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
26469 " noexcept;",
26470 Style);
26472 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
26473 verifyFormat("void foo(int arg1,\n"
26474 " double arg2) noexcept;",
26475 Style);
26477 verifyFormat("void bar(int arg1, double arg2)\n"
26478 " noexcept(noexcept(baz(arg1)) &&\n"
26479 " noexcept(baz(arg2)));",
26480 Style);
26482 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
26483 Style);
26486 TEST_F(FormatTest, PPBranchesInBracedInit) {
26487 verifyFormat("A a_{kFlag1,\n"
26488 "#if BUILD_FLAG\n"
26489 " kFlag2,\n"
26490 "#else\n"
26491 " kFlag3,\n"
26492 "#endif\n"
26493 " kFlag4};",
26494 "A a_{\n"
26495 " kFlag1,\n"
26496 "#if BUILD_FLAG\n"
26497 " kFlag2,\n"
26498 "#else\n"
26499 " kFlag3,\n"
26500 "#endif\n"
26501 " kFlag4\n"
26502 "};");
26505 TEST_F(FormatTest, StreamOutputOperator) {
26506 verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
26509 } // namespace
26510 } // namespace test
26511 } // namespace format
26512 } // namespace clang