1//===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "FormatTestBase.h"
10
11#define DEBUG_TYPE "format-test"
12
13namespace clang {
14namespace format {
15namespace test {
16namespace {
17
18class FormatTest : public test::FormatTestBase {};
19
20TEST_F(FormatTest, MessUp) {
21 EXPECT_EQ("1 2 3", messUp("1 2 3"));
22 EXPECT_EQ("1 2 3", messUp("1\n2\n3"));
23 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
24 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
25 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
26}
27
28TEST_F(FormatTest, DefaultLLVMStyleIsCpp) {
29 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language);
30}
31
32TEST_F(FormatTest, LLVMStyleOverride) {
33 EXPECT_EQ(FormatStyle::LK_Proto,
34 getLLVMStyle(FormatStyle::LK_Proto).Language);
35}
36
37//===----------------------------------------------------------------------===//
38// Basic function tests.
39//===----------------------------------------------------------------------===//
40
41TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); }
42
43TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
44 verifyFormat("int i;", " int i;");
45 verifyFormat("\nint i;", " \n\t \v \f int i;");
46 verifyFormat("int i;\nint j;", " int i; int j;");
47 verifyFormat("int i;\nint j;", " int i;\n int j;");
48}
49
50TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
51 verifyFormat("int i;", "int\ni;");
52}
53
54TEST_F(FormatTest, FormatsNestedBlockStatements) {
55 verifyFormat("{\n"
56 " {\n"
57 " {\n"
58 " }\n"
59 " }\n"
60 "}",
61 "{{{}}}");
62}
63
64TEST_F(FormatTest, FormatsNestedCall) {
65 verifyFormat("Method(f1, f2(f3));");
66 verifyFormat("Method(f1(f2, f3()));");
67 verifyFormat("Method(f1(f2, (f3())));");
68}
69
70TEST_F(FormatTest, NestedNameSpecifiers) {
71 verifyFormat("vector<::Type> v;");
72 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
73 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
74 verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
75 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
76 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
77 verifyFormat("bool a = 2 < ::SomeFunction();");
78 verifyFormat("ALWAYS_INLINE ::std::string getName();");
79 verifyFormat("some::string getName();");
80}
81
82TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
83 verifyFormat("if (a) {\n"
84 " f();\n"
85 "}",
86 "if(a){f();}");
87 EXPECT_EQ(4, ReplacementCount);
88 verifyNoChange("if (a) {\n"
89 " f();\n"
90 "}");
91 EXPECT_EQ(0, ReplacementCount);
92 verifyNoChange("/*\r\n"
93 "\r\n"
94 "*/");
95 EXPECT_EQ(0, ReplacementCount);
96}
97
98TEST_F(FormatTest, RemovesEmptyLines) {
99 verifyFormat("class C {\n"
100 " int i;\n"
101 "};",
102 "class C {\n"
103 " int i;\n"
104 "\n"
105 "};");
106
107 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
108 verifyFormat("namespace N {\n"
109 "\n"
110 "int i;\n"
111 "}",
112 "namespace N {\n"
113 "\n"
114 "int i;\n"
115 "}",
116 getGoogleStyle());
117 verifyFormat("/* something */ namespace N {\n"
118 "\n"
119 "int i;\n"
120 "}",
121 "/* something */ namespace N {\n"
122 "\n"
123 "int i;\n"
124 "}",
125 getGoogleStyle());
126 verifyFormat("inline namespace N {\n"
127 "\n"
128 "int i;\n"
129 "}",
130 "inline namespace N {\n"
131 "\n"
132 "int i;\n"
133 "}",
134 getGoogleStyle());
135 verifyFormat("/* something */ inline namespace N {\n"
136 "\n"
137 "int i;\n"
138 "}",
139 "/* something */ inline namespace N {\n"
140 "\n"
141 "int i;\n"
142 "}",
143 getGoogleStyle());
144 verifyFormat("export namespace N {\n"
145 "\n"
146 "int i;\n"
147 "}",
148 "export namespace N {\n"
149 "\n"
150 "int i;\n"
151 "}",
152 getGoogleStyle());
153 verifyFormat("extern /**/ \"C\" /**/ {\n"
154 "\n"
155 "int i;\n"
156 "}",
157 "extern /**/ \"C\" /**/ {\n"
158 "\n"
159 "int i;\n"
160 "}",
161 getGoogleStyle());
162
163 auto CustomStyle = getLLVMStyle();
164 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
165 CustomStyle.BraceWrapping.AfterNamespace = true;
166 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
167 verifyFormat("namespace N\n"
168 "{\n"
169 "\n"
170 "int i;\n"
171 "}",
172 "namespace N\n"
173 "{\n"
174 "\n"
175 "\n"
176 "int i;\n"
177 "}",
178 CustomStyle);
179 verifyFormat("/* something */ namespace N\n"
180 "{\n"
181 "\n"
182 "int i;\n"
183 "}",
184 "/* something */ namespace N {\n"
185 "\n"
186 "\n"
187 "int i;\n"
188 "}",
189 CustomStyle);
190 verifyFormat("inline namespace N\n"
191 "{\n"
192 "\n"
193 "int i;\n"
194 "}",
195 "inline namespace N\n"
196 "{\n"
197 "\n"
198 "\n"
199 "int i;\n"
200 "}",
201 CustomStyle);
202 verifyFormat("/* something */ inline namespace N\n"
203 "{\n"
204 "\n"
205 "int i;\n"
206 "}",
207 "/* something */ inline namespace N\n"
208 "{\n"
209 "\n"
210 "int i;\n"
211 "}",
212 CustomStyle);
213 verifyFormat("export namespace N\n"
214 "{\n"
215 "\n"
216 "int i;\n"
217 "}",
218 "export namespace N\n"
219 "{\n"
220 "\n"
221 "int i;\n"
222 "}",
223 CustomStyle);
224 verifyFormat("namespace a\n"
225 "{\n"
226 "namespace b\n"
227 "{\n"
228 "\n"
229 "class AA {};\n"
230 "\n"
231 "} // namespace b\n"
232 "} // namespace a",
233 "namespace a\n"
234 "{\n"
235 "namespace b\n"
236 "{\n"
237 "\n"
238 "\n"
239 "class AA {};\n"
240 "\n"
241 "\n"
242 "}\n"
243 "}",
244 CustomStyle);
245 verifyFormat("namespace A /* comment */\n"
246 "{\n"
247 "class B {}\n"
248 "} // namespace A",
249 "namespace A /* comment */ { class B {} }", CustomStyle);
250 verifyFormat("namespace A\n"
251 "{ /* comment */\n"
252 "class B {}\n"
253 "} // namespace A",
254 "namespace A {/* comment */ class B {} }", CustomStyle);
255 verifyFormat("namespace A\n"
256 "{ /* comment */\n"
257 "\n"
258 "class B {}\n"
259 "\n"
260 ""
261 "} // namespace A",
262 "namespace A { /* comment */\n"
263 "\n"
264 "\n"
265 "class B {}\n"
266 "\n"
267 "\n"
268 "}",
269 CustomStyle);
270 verifyFormat("namespace A /* comment */\n"
271 "{\n"
272 "\n"
273 "class B {}\n"
274 "\n"
275 "} // namespace A",
276 "namespace A/* comment */ {\n"
277 "\n"
278 "\n"
279 "class B {}\n"
280 "\n"
281 "\n"
282 "}",
283 CustomStyle);
284
285 // ...but do keep inlining and removing empty lines for non-block extern "C"
286 // functions.
287 verifyGoogleFormat("extern \"C\" int f() { return 42; }");
288 verifyFormat("extern \"C\" int f() {\n"
289 " int i = 42;\n"
290 " return i;\n"
291 "}",
292 "extern \"C\" int f() {\n"
293 "\n"
294 " int i = 42;\n"
295 " return i;\n"
296 "}",
297 getGoogleStyle());
298
299 // Remove empty lines at the beginning and end of blocks.
300 verifyFormat("void f() {\n"
301 "\n"
302 " if (a) {\n"
303 "\n"
304 " f();\n"
305 " }\n"
306 "}",
307 "void f() {\n"
308 "\n"
309 " if (a) {\n"
310 "\n"
311 " f();\n"
312 "\n"
313 " }\n"
314 "\n"
315 "}");
316 verifyFormat("void f() {\n"
317 " if (a) {\n"
318 " f();\n"
319 " }\n"
320 "}",
321 "void f() {\n"
322 "\n"
323 " if (a) {\n"
324 "\n"
325 " f();\n"
326 "\n"
327 " }\n"
328 "\n"
329 "}",
330 getGoogleStyle());
331
332 // Don't remove empty lines in more complex control statements.
333 verifyFormat("void f() {\n"
334 " if (a) {\n"
335 " f();\n"
336 "\n"
337 " } else if (b) {\n"
338 " f();\n"
339 " }\n"
340 "}",
341 "void f() {\n"
342 " if (a) {\n"
343 " f();\n"
344 "\n"
345 " } else if (b) {\n"
346 " f();\n"
347 "\n"
348 " }\n"
349 "\n"
350 "}");
351
352 // Don't remove empty lines before namespace endings.
353 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
354 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
355 verifyNoChange("namespace {\n"
356 "int i;\n"
357 "\n"
358 "}",
359 LLVMWithNoNamespaceFix);
360 verifyFormat("namespace {\n"
361 "int i;\n"
362 "}",
363 LLVMWithNoNamespaceFix);
364 verifyNoChange("namespace {\n"
365 "int i;\n"
366 "\n"
367 "};",
368 LLVMWithNoNamespaceFix);
369 verifyFormat("namespace {\n"
370 "int i;\n"
371 "};",
372 LLVMWithNoNamespaceFix);
373 verifyNoChange("namespace {\n"
374 "int i;\n"
375 "\n"
376 "}");
377 verifyFormat("namespace {\n"
378 "int i;\n"
379 "\n"
380 "} // namespace",
381 "namespace {\n"
382 "int i;\n"
383 "\n"
384 "} // namespace");
385
386 FormatStyle Style = getLLVMStyle();
387 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
388 Style.MaxEmptyLinesToKeep = 2;
389 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
390 Style.BraceWrapping.AfterClass = true;
391 Style.BraceWrapping.AfterFunction = true;
392 Style.KeepEmptyLinesAtTheStartOfBlocks = false;
393
394 verifyFormat("class Foo\n"
395 "{\n"
396 " Foo() {}\n"
397 "\n"
398 " void funk() {}\n"
399 "};",
400 "class Foo\n"
401 "{\n"
402 " Foo()\n"
403 " {\n"
404 " }\n"
405 "\n"
406 " void funk() {}\n"
407 "};",
408 Style);
409}
410
411TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
412 verifyFormat("x = (a) and (b);");
413 verifyFormat("x = (a) or (b);");
414 verifyFormat("x = (a) bitand (b);");
415 verifyFormat("x = (a) bitor (b);");
416 verifyFormat("x = (a) not_eq (b);");
417 verifyFormat("x = (a) and_eq (b);");
418 verifyFormat("x = (a) or_eq (b);");
419 verifyFormat("x = (a) xor (b);");
420}
421
422TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
423 verifyFormat("x = compl(a);");
424 verifyFormat("x = not(a);");
425 verifyFormat("x = bitand(a);");
426 // Unary operator must not be merged with the next identifier
427 verifyFormat("x = compl a;");
428 verifyFormat("x = not a;");
429 verifyFormat("x = bitand a;");
430}
431
432//===----------------------------------------------------------------------===//
433// Tests for control statements.
434//===----------------------------------------------------------------------===//
435
436TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
437 verifyFormat("if (true)\n f();\ng();");
438 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
439 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
440 verifyFormat("if constexpr (true)\n"
441 " f();\ng();");
442 verifyFormat("if CONSTEXPR (true)\n"
443 " f();\ng();");
444 verifyFormat("if constexpr (a)\n"
445 " if constexpr (b)\n"
446 " if constexpr (c)\n"
447 " g();\n"
448 "h();");
449 verifyFormat("if CONSTEXPR (a)\n"
450 " if CONSTEXPR (b)\n"
451 " if CONSTEXPR (c)\n"
452 " g();\n"
453 "h();");
454 verifyFormat("if constexpr (a)\n"
455 " if constexpr (b) {\n"
456 " f();\n"
457 " }\n"
458 "g();");
459 verifyFormat("if CONSTEXPR (a)\n"
460 " if CONSTEXPR (b) {\n"
461 " f();\n"
462 " }\n"
463 "g();");
464
465 verifyFormat("if consteval {\n}");
466 verifyFormat("if !consteval {\n}");
467 verifyFormat("if not consteval {\n}");
468 verifyFormat("if consteval {\n} else {\n}");
469 verifyFormat("if !consteval {\n} else {\n}");
470 verifyFormat("if consteval {\n"
471 " f();\n"
472 "}");
473 verifyFormat("if !consteval {\n"
474 " f();\n"
475 "}");
476 verifyFormat("if consteval {\n"
477 " f();\n"
478 "} else {\n"
479 " g();\n"
480 "}");
481 verifyFormat("if CONSTEVAL {\n"
482 " f();\n"
483 "}");
484 verifyFormat("if !CONSTEVAL {\n"
485 " f();\n"
486 "}");
487
488 verifyFormat("if (a)\n"
489 " g();");
490 verifyFormat("if (a) {\n"
491 " g()\n"
492 "};");
493 verifyFormat("if (a)\n"
494 " g();\n"
495 "else\n"
496 " g();");
497 verifyFormat("if (a) {\n"
498 " g();\n"
499 "} else\n"
500 " g();");
501 verifyFormat("if (a)\n"
502 " g();\n"
503 "else {\n"
504 " g();\n"
505 "}");
506 verifyFormat("if (a) {\n"
507 " g();\n"
508 "} else {\n"
509 " g();\n"
510 "}");
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();");
529 verifyFormat("if (a)\n"
530 " g();\n"
531 "else if (b)\n"
532 " g();\n"
533 "else {\n"
534 " g();\n"
535 "}");
536 verifyFormat("if (a)\n"
537 " g();\n"
538 "else if (b) {\n"
539 " g();\n"
540 "} else {\n"
541 " g();\n"
542 "}");
543 verifyFormat("if (a) {\n"
544 " g();\n"
545 "} else if (b) {\n"
546 " g();\n"
547 "} else {\n"
548 " g();\n"
549 "}");
550
551 FormatStyle AllowsMergedIf = getLLVMStyle();
552 AllowsMergedIf.IfMacros.push_back(x: "MYIF");
553 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
554 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
555 FormatStyle::SIS_WithoutElse;
556 verifyFormat("if (a)\n"
557 " // comment\n"
558 " f();",
559 AllowsMergedIf);
560 verifyFormat("{\n"
561 " if (a)\n"
562 " label:\n"
563 " f();\n"
564 "}",
565 AllowsMergedIf);
566 verifyFormat("#define A \\\n"
567 " if (a) \\\n"
568 " label: \\\n"
569 " f()",
570 AllowsMergedIf);
571 verifyFormat("if (a)\n"
572 " ;",
573 AllowsMergedIf);
574 verifyFormat("if (a)\n"
575 " if (b) return;",
576 AllowsMergedIf);
577
578 verifyFormat("if (a) // Can't merge this\n"
579 " f();",
580 AllowsMergedIf);
581 verifyFormat("if (a) /* still don't merge */\n"
582 " f();",
583 AllowsMergedIf);
584 verifyFormat("if (a) { // Never merge this\n"
585 " f();\n"
586 "}",
587 AllowsMergedIf);
588 verifyFormat("if (a) { /* Never merge this */\n"
589 " f();\n"
590 "}",
591 AllowsMergedIf);
592 verifyFormat("MYIF (a)\n"
593 " // comment\n"
594 " f();",
595 AllowsMergedIf);
596 verifyFormat("{\n"
597 " MYIF (a)\n"
598 " label:\n"
599 " f();\n"
600 "}",
601 AllowsMergedIf);
602 verifyFormat("#define A \\\n"
603 " MYIF (a) \\\n"
604 " label: \\\n"
605 " f()",
606 AllowsMergedIf);
607 verifyFormat("MYIF (a)\n"
608 " ;",
609 AllowsMergedIf);
610 verifyFormat("MYIF (a)\n"
611 " MYIF (b) return;",
612 AllowsMergedIf);
613
614 verifyFormat("MYIF (a) // Can't merge this\n"
615 " f();",
616 AllowsMergedIf);
617 verifyFormat("MYIF (a) /* still don't merge */\n"
618 " f();",
619 AllowsMergedIf);
620 verifyFormat("MYIF (a) { // Never merge this\n"
621 " f();\n"
622 "}",
623 AllowsMergedIf);
624 verifyFormat("MYIF (a) { /* Never merge this */\n"
625 " f();\n"
626 "}",
627 AllowsMergedIf);
628
629 AllowsMergedIf.ColumnLimit = 14;
630 // Where line-lengths matter, a 2-letter synonym that maintains line length.
631 // Not IF to avoid any confusion that IF is somehow special.
632 AllowsMergedIf.IfMacros.push_back(x: "FI");
633 verifyFormat("if (a) return;", AllowsMergedIf);
634 verifyFormat("if (aaaaaaaaa)\n"
635 " return;",
636 AllowsMergedIf);
637 verifyFormat("FI (a) return;", AllowsMergedIf);
638 verifyFormat("FI (aaaaaaaaa)\n"
639 " return;",
640 AllowsMergedIf);
641
642 AllowsMergedIf.ColumnLimit = 13;
643 verifyFormat("if (a)\n return;", AllowsMergedIf);
644 verifyFormat("FI (a)\n return;", AllowsMergedIf);
645
646 FormatStyle AllowsMergedIfElse = getLLVMStyle();
647 AllowsMergedIfElse.IfMacros.push_back(x: "MYIF");
648 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
649 FormatStyle::SIS_AllIfsAndElse;
650 verifyFormat("if (a)\n"
651 " // comment\n"
652 " f();\n"
653 "else\n"
654 " // comment\n"
655 " f();",
656 AllowsMergedIfElse);
657 verifyFormat("{\n"
658 " if (a)\n"
659 " label:\n"
660 " f();\n"
661 " else\n"
662 " label:\n"
663 " f();\n"
664 "}",
665 AllowsMergedIfElse);
666 verifyFormat("if (a)\n"
667 " ;\n"
668 "else\n"
669 " ;",
670 AllowsMergedIfElse);
671 verifyFormat("if (a) {\n"
672 "} else {\n"
673 "}",
674 AllowsMergedIfElse);
675 verifyFormat("if (a) return;\n"
676 "else if (b) return;\n"
677 "else return;",
678 AllowsMergedIfElse);
679 verifyFormat("if (a) {\n"
680 "} else return;",
681 AllowsMergedIfElse);
682 verifyFormat("if (a) {\n"
683 "} else if (b) return;\n"
684 "else return;",
685 AllowsMergedIfElse);
686 verifyFormat("if (a) return;\n"
687 "else if (b) {\n"
688 "} else return;",
689 AllowsMergedIfElse);
690 verifyFormat("if (a)\n"
691 " if (b) return;\n"
692 " else return;",
693 AllowsMergedIfElse);
694 verifyFormat("if constexpr (a)\n"
695 " if constexpr (b) return;\n"
696 " else if constexpr (c) return;\n"
697 " else return;",
698 AllowsMergedIfElse);
699 verifyFormat("MYIF (a)\n"
700 " // comment\n"
701 " f();\n"
702 "else\n"
703 " // comment\n"
704 " f();",
705 AllowsMergedIfElse);
706 verifyFormat("{\n"
707 " MYIF (a)\n"
708 " label:\n"
709 " f();\n"
710 " else\n"
711 " label:\n"
712 " f();\n"
713 "}",
714 AllowsMergedIfElse);
715 verifyFormat("MYIF (a)\n"
716 " ;\n"
717 "else\n"
718 " ;",
719 AllowsMergedIfElse);
720 verifyFormat("MYIF (a) {\n"
721 "} else {\n"
722 "}",
723 AllowsMergedIfElse);
724 verifyFormat("MYIF (a) return;\n"
725 "else MYIF (b) return;\n"
726 "else return;",
727 AllowsMergedIfElse);
728 verifyFormat("MYIF (a) {\n"
729 "} else return;",
730 AllowsMergedIfElse);
731 verifyFormat("MYIF (a) {\n"
732 "} else MYIF (b) return;\n"
733 "else return;",
734 AllowsMergedIfElse);
735 verifyFormat("MYIF (a) return;\n"
736 "else MYIF (b) {\n"
737 "} else return;",
738 AllowsMergedIfElse);
739 verifyFormat("MYIF (a)\n"
740 " MYIF (b) return;\n"
741 " else return;",
742 AllowsMergedIfElse);
743 verifyFormat("MYIF constexpr (a)\n"
744 " MYIF constexpr (b) return;\n"
745 " else MYIF constexpr (c) return;\n"
746 " else return;",
747 AllowsMergedIfElse);
748}
749
750TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
751 FormatStyle AllowsMergedIf = getLLVMStyle();
752 AllowsMergedIf.IfMacros.push_back(x: "MYIF");
753 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
754 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
755 FormatStyle::SIS_WithoutElse;
756 verifyFormat("if (a)\n"
757 " f();\n"
758 "else {\n"
759 " g();\n"
760 "}",
761 AllowsMergedIf);
762 verifyFormat("if (a)\n"
763 " f();\n"
764 "else\n"
765 " g();",
766 AllowsMergedIf);
767
768 verifyFormat("if (a) g();", AllowsMergedIf);
769 verifyFormat("if (a) {\n"
770 " g()\n"
771 "};",
772 AllowsMergedIf);
773 verifyFormat("if (a)\n"
774 " g();\n"
775 "else\n"
776 " g();",
777 AllowsMergedIf);
778 verifyFormat("if (a) {\n"
779 " g();\n"
780 "} else\n"
781 " g();",
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 {\n"
792 " g();\n"
793 "}",
794 AllowsMergedIf);
795 verifyFormat("if (a)\n"
796 " g();\n"
797 "else if (b)\n"
798 " g();\n"
799 "else\n"
800 " g();",
801 AllowsMergedIf);
802 verifyFormat("if (a) {\n"
803 " g();\n"
804 "} else if (b)\n"
805 " g();\n"
806 "else\n"
807 " g();",
808 AllowsMergedIf);
809 verifyFormat("if (a)\n"
810 " g();\n"
811 "else if (b) {\n"
812 " g();\n"
813 "} else\n"
814 " g();",
815 AllowsMergedIf);
816 verifyFormat("if (a)\n"
817 " g();\n"
818 "else if (b)\n"
819 " g();\n"
820 "else {\n"
821 " g();\n"
822 "}",
823 AllowsMergedIf);
824 verifyFormat("if (a)\n"
825 " g();\n"
826 "else if (b) {\n"
827 " g();\n"
828 "} else {\n"
829 " g();\n"
830 "}",
831 AllowsMergedIf);
832 verifyFormat("if (a) {\n"
833 " g();\n"
834 "} else if (b) {\n"
835 " g();\n"
836 "} else {\n"
837 " g();\n"
838 "}",
839 AllowsMergedIf);
840 verifyFormat("MYIF (a)\n"
841 " f();\n"
842 "else {\n"
843 " g();\n"
844 "}",
845 AllowsMergedIf);
846 verifyFormat("MYIF (a)\n"
847 " f();\n"
848 "else\n"
849 " g();",
850 AllowsMergedIf);
851
852 verifyFormat("MYIF (a) g();", AllowsMergedIf);
853 verifyFormat("MYIF (a) {\n"
854 " g()\n"
855 "};",
856 AllowsMergedIf);
857 verifyFormat("MYIF (a)\n"
858 " g();\n"
859 "else\n"
860 " g();",
861 AllowsMergedIf);
862 verifyFormat("MYIF (a) {\n"
863 " g();\n"
864 "} else\n"
865 " g();",
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 {\n"
876 " g();\n"
877 "}",
878 AllowsMergedIf);
879 verifyFormat("MYIF (a)\n"
880 " g();\n"
881 "else MYIF (b)\n"
882 " g();\n"
883 "else\n"
884 " g();",
885 AllowsMergedIf);
886 verifyFormat("MYIF (a)\n"
887 " g();\n"
888 "else if (b)\n"
889 " g();\n"
890 "else\n"
891 " g();",
892 AllowsMergedIf);
893 verifyFormat("MYIF (a) {\n"
894 " g();\n"
895 "} else MYIF (b)\n"
896 " g();\n"
897 "else\n"
898 " g();",
899 AllowsMergedIf);
900 verifyFormat("MYIF (a) {\n"
901 " g();\n"
902 "} else if (b)\n"
903 " g();\n"
904 "else\n"
905 " g();",
906 AllowsMergedIf);
907 verifyFormat("MYIF (a)\n"
908 " g();\n"
909 "else MYIF (b) {\n"
910 " g();\n"
911 "} else\n"
912 " g();",
913 AllowsMergedIf);
914 verifyFormat("MYIF (a)\n"
915 " g();\n"
916 "else if (b) {\n"
917 " g();\n"
918 "} else\n"
919 " g();",
920 AllowsMergedIf);
921 verifyFormat("MYIF (a)\n"
922 " g();\n"
923 "else MYIF (b)\n"
924 " g();\n"
925 "else {\n"
926 " g();\n"
927 "}",
928 AllowsMergedIf);
929 verifyFormat("MYIF (a)\n"
930 " g();\n"
931 "else if (b)\n"
932 " g();\n"
933 "else {\n"
934 " g();\n"
935 "}",
936 AllowsMergedIf);
937 verifyFormat("MYIF (a)\n"
938 " g();\n"
939 "else MYIF (b) {\n"
940 " g();\n"
941 "} else {\n"
942 " g();\n"
943 "}",
944 AllowsMergedIf);
945 verifyFormat("MYIF (a)\n"
946 " g();\n"
947 "else if (b) {\n"
948 " g();\n"
949 "} else {\n"
950 " g();\n"
951 "}",
952 AllowsMergedIf);
953 verifyFormat("MYIF (a) {\n"
954 " g();\n"
955 "} else MYIF (b) {\n"
956 " g();\n"
957 "} else {\n"
958 " g();\n"
959 "}",
960 AllowsMergedIf);
961 verifyFormat("MYIF (a) {\n"
962 " g();\n"
963 "} else if (b) {\n"
964 " g();\n"
965 "} else {\n"
966 " g();\n"
967 "}",
968 AllowsMergedIf);
969
970 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
971 FormatStyle::SIS_OnlyFirstIf;
972
973 verifyFormat("if (a) f();\n"
974 "else {\n"
975 " g();\n"
976 "}",
977 AllowsMergedIf);
978 verifyFormat("if (a) f();\n"
979 "else {\n"
980 " if (a) f();\n"
981 " else {\n"
982 " g();\n"
983 " }\n"
984 " g();\n"
985 "}",
986 AllowsMergedIf);
987
988 verifyFormat("if (a) g();", AllowsMergedIf);
989 verifyFormat("if (a) {\n"
990 " g()\n"
991 "};",
992 AllowsMergedIf);
993 verifyFormat("if (a) g();\n"
994 "else\n"
995 " g();",
996 AllowsMergedIf);
997 verifyFormat("if (a) {\n"
998 " g();\n"
999 "} else\n"
1000 " g();",
1001 AllowsMergedIf);
1002 verifyFormat("if (a) g();\n"
1003 "else {\n"
1004 " g();\n"
1005 "}",
1006 AllowsMergedIf);
1007 verifyFormat("if (a) {\n"
1008 " g();\n"
1009 "} else {\n"
1010 " g();\n"
1011 "}",
1012 AllowsMergedIf);
1013 verifyFormat("if (a) g();\n"
1014 "else if (b)\n"
1015 " g();\n"
1016 "else\n"
1017 " g();",
1018 AllowsMergedIf);
1019 verifyFormat("if (a) {\n"
1020 " 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();",
1031 AllowsMergedIf);
1032 verifyFormat("if (a) g();\n"
1033 "else if (b)\n"
1034 " g();\n"
1035 "else {\n"
1036 " g();\n"
1037 "}",
1038 AllowsMergedIf);
1039 verifyFormat("if (a) g();\n"
1040 "else if (b) {\n"
1041 " g();\n"
1042 "} else {\n"
1043 " g();\n"
1044 "}",
1045 AllowsMergedIf);
1046 verifyFormat("if (a) {\n"
1047 " g();\n"
1048 "} else if (b) {\n"
1049 " g();\n"
1050 "} else {\n"
1051 " g();\n"
1052 "}",
1053 AllowsMergedIf);
1054 verifyFormat("MYIF (a) f();\n"
1055 "else {\n"
1056 " g();\n"
1057 "}",
1058 AllowsMergedIf);
1059 verifyFormat("MYIF (a) f();\n"
1060 "else {\n"
1061 " if (a) f();\n"
1062 " else {\n"
1063 " g();\n"
1064 " }\n"
1065 " g();\n"
1066 "}",
1067 AllowsMergedIf);
1068
1069 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1070 verifyFormat("MYIF (a) {\n"
1071 " g()\n"
1072 "};",
1073 AllowsMergedIf);
1074 verifyFormat("MYIF (a) g();\n"
1075 "else\n"
1076 " g();",
1077 AllowsMergedIf);
1078 verifyFormat("MYIF (a) {\n"
1079 " g();\n"
1080 "} else\n"
1081 " g();",
1082 AllowsMergedIf);
1083 verifyFormat("MYIF (a) g();\n"
1084 "else {\n"
1085 " g();\n"
1086 "}",
1087 AllowsMergedIf);
1088 verifyFormat("MYIF (a) {\n"
1089 " g();\n"
1090 "} else {\n"
1091 " g();\n"
1092 "}",
1093 AllowsMergedIf);
1094 verifyFormat("MYIF (a) g();\n"
1095 "else MYIF (b)\n"
1096 " g();\n"
1097 "else\n"
1098 " g();",
1099 AllowsMergedIf);
1100 verifyFormat("MYIF (a) g();\n"
1101 "else if (b)\n"
1102 " g();\n"
1103 "else\n"
1104 " g();",
1105 AllowsMergedIf);
1106 verifyFormat("MYIF (a) {\n"
1107 " g();\n"
1108 "} else MYIF (b)\n"
1109 " g();\n"
1110 "else\n"
1111 " g();",
1112 AllowsMergedIf);
1113 verifyFormat("MYIF (a) {\n"
1114 " g();\n"
1115 "} else if (b)\n"
1116 " g();\n"
1117 "else\n"
1118 " g();",
1119 AllowsMergedIf);
1120 verifyFormat("MYIF (a) g();\n"
1121 "else MYIF (b) {\n"
1122 " g();\n"
1123 "} else\n"
1124 " g();",
1125 AllowsMergedIf);
1126 verifyFormat("MYIF (a) g();\n"
1127 "else if (b) {\n"
1128 " g();\n"
1129 "} else\n"
1130 " g();",
1131 AllowsMergedIf);
1132 verifyFormat("MYIF (a) g();\n"
1133 "else MYIF (b)\n"
1134 " g();\n"
1135 "else {\n"
1136 " g();\n"
1137 "}",
1138 AllowsMergedIf);
1139 verifyFormat("MYIF (a) g();\n"
1140 "else if (b)\n"
1141 " g();\n"
1142 "else {\n"
1143 " g();\n"
1144 "}",
1145 AllowsMergedIf);
1146 verifyFormat("MYIF (a) g();\n"
1147 "else MYIF (b) {\n"
1148 " g();\n"
1149 "} else {\n"
1150 " g();\n"
1151 "}",
1152 AllowsMergedIf);
1153 verifyFormat("MYIF (a) g();\n"
1154 "else if (b) {\n"
1155 " g();\n"
1156 "} else {\n"
1157 " g();\n"
1158 "}",
1159 AllowsMergedIf);
1160 verifyFormat("MYIF (a) {\n"
1161 " g();\n"
1162 "} else MYIF (b) {\n"
1163 " g();\n"
1164 "} else {\n"
1165 " g();\n"
1166 "}",
1167 AllowsMergedIf);
1168 verifyFormat("MYIF (a) {\n"
1169 " g();\n"
1170 "} else if (b) {\n"
1171 " g();\n"
1172 "} else {\n"
1173 " g();\n"
1174 "}",
1175 AllowsMergedIf);
1176
1177 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1178 FormatStyle::SIS_AllIfsAndElse;
1179
1180 verifyFormat("if (a) f();\n"
1181 "else {\n"
1182 " g();\n"
1183 "}",
1184 AllowsMergedIf);
1185 verifyFormat("if (a) f();\n"
1186 "else {\n"
1187 " if (a) f();\n"
1188 " else {\n"
1189 " g();\n"
1190 " }\n"
1191 " g();\n"
1192 "}",
1193 AllowsMergedIf);
1194
1195 verifyFormat("if (a) g();", AllowsMergedIf);
1196 verifyFormat("if (a) {\n"
1197 " g()\n"
1198 "};",
1199 AllowsMergedIf);
1200 verifyFormat("if (a) g();\n"
1201 "else g();",
1202 AllowsMergedIf);
1203 verifyFormat("if (a) {\n"
1204 " g();\n"
1205 "} else g();",
1206 AllowsMergedIf);
1207 verifyFormat("if (a) g();\n"
1208 "else {\n"
1209 " g();\n"
1210 "}",
1211 AllowsMergedIf);
1212 verifyFormat("if (a) {\n"
1213 " g();\n"
1214 "} else {\n"
1215 " g();\n"
1216 "}",
1217 AllowsMergedIf);
1218 verifyFormat("if (a) g();\n"
1219 "else if (b) g();\n"
1220 "else g();",
1221 AllowsMergedIf);
1222 verifyFormat("if (a) {\n"
1223 " g();\n"
1224 "} else if (b) g();\n"
1225 "else g();",
1226 AllowsMergedIf);
1227 verifyFormat("if (a) g();\n"
1228 "else if (b) {\n"
1229 " g();\n"
1230 "} else g();",
1231 AllowsMergedIf);
1232 verifyFormat("if (a) g();\n"
1233 "else if (b) g();\n"
1234 "else {\n"
1235 " g();\n"
1236 "}",
1237 AllowsMergedIf);
1238 verifyFormat("if (a) g();\n"
1239 "else if (b) {\n"
1240 " g();\n"
1241 "} else {\n"
1242 " g();\n"
1243 "}",
1244 AllowsMergedIf);
1245 verifyFormat("if (a) {\n"
1246 " g();\n"
1247 "} else if (b) {\n"
1248 " g();\n"
1249 "} else {\n"
1250 " g();\n"
1251 "}",
1252 AllowsMergedIf);
1253 verifyFormat("MYIF (a) f();\n"
1254 "else {\n"
1255 " g();\n"
1256 "}",
1257 AllowsMergedIf);
1258 verifyFormat("MYIF (a) f();\n"
1259 "else {\n"
1260 " if (a) f();\n"
1261 " else {\n"
1262 " g();\n"
1263 " }\n"
1264 " g();\n"
1265 "}",
1266 AllowsMergedIf);
1267
1268 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1269 verifyFormat("MYIF (a) {\n"
1270 " g()\n"
1271 "};",
1272 AllowsMergedIf);
1273 verifyFormat("MYIF (a) g();\n"
1274 "else g();",
1275 AllowsMergedIf);
1276 verifyFormat("MYIF (a) {\n"
1277 " g();\n"
1278 "} else g();",
1279 AllowsMergedIf);
1280 verifyFormat("MYIF (a) g();\n"
1281 "else {\n"
1282 " g();\n"
1283 "}",
1284 AllowsMergedIf);
1285 verifyFormat("MYIF (a) {\n"
1286 " g();\n"
1287 "} else {\n"
1288 " g();\n"
1289 "}",
1290 AllowsMergedIf);
1291 verifyFormat("MYIF (a) g();\n"
1292 "else MYIF (b) g();\n"
1293 "else g();",
1294 AllowsMergedIf);
1295 verifyFormat("MYIF (a) g();\n"
1296 "else if (b) g();\n"
1297 "else g();",
1298 AllowsMergedIf);
1299 verifyFormat("MYIF (a) {\n"
1300 " g();\n"
1301 "} else MYIF (b) g();\n"
1302 "else g();",
1303 AllowsMergedIf);
1304 verifyFormat("MYIF (a) {\n"
1305 " g();\n"
1306 "} else if (b) g();\n"
1307 "else g();",
1308 AllowsMergedIf);
1309 verifyFormat("MYIF (a) g();\n"
1310 "else MYIF (b) {\n"
1311 " g();\n"
1312 "} else g();",
1313 AllowsMergedIf);
1314 verifyFormat("MYIF (a) g();\n"
1315 "else if (b) {\n"
1316 " g();\n"
1317 "} else g();",
1318 AllowsMergedIf);
1319 verifyFormat("MYIF (a) g();\n"
1320 "else MYIF (b) g();\n"
1321 "else {\n"
1322 " g();\n"
1323 "}",
1324 AllowsMergedIf);
1325 verifyFormat("MYIF (a) g();\n"
1326 "else if (b) g();\n"
1327 "else {\n"
1328 " g();\n"
1329 "}",
1330 AllowsMergedIf);
1331 verifyFormat("MYIF (a) g();\n"
1332 "else MYIF (b) {\n"
1333 " g();\n"
1334 "} else {\n"
1335 " g();\n"
1336 "}",
1337 AllowsMergedIf);
1338 verifyFormat("MYIF (a) g();\n"
1339 "else if (b) {\n"
1340 " g();\n"
1341 "} else {\n"
1342 " g();\n"
1343 "}",
1344 AllowsMergedIf);
1345 verifyFormat("MYIF (a) {\n"
1346 " g();\n"
1347 "} else MYIF (b) {\n"
1348 " g();\n"
1349 "} else {\n"
1350 " g();\n"
1351 "}",
1352 AllowsMergedIf);
1353 verifyFormat("MYIF (a) {\n"
1354 " g();\n"
1355 "} else if (b) {\n"
1356 " g();\n"
1357 "} else {\n"
1358 " g();\n"
1359 "}",
1360 AllowsMergedIf);
1361}
1362
1363TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1364 verifyFormat("while (true)\n"
1365 " ;");
1366 verifyFormat("for (;;)\n"
1367 " ;");
1368
1369 FormatStyle AllowsMergedLoops = getLLVMStyle();
1370 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1371
1372 verifyFormat("while (true) continue;", AllowsMergedLoops);
1373 verifyFormat("for (;;) continue;", AllowsMergedLoops);
1374 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1375 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1376 verifyFormat("while (true);", AllowsMergedLoops);
1377 verifyFormat("for (;;);", AllowsMergedLoops);
1378 verifyFormat("for (;;)\n"
1379 " for (;;) continue;",
1380 AllowsMergedLoops);
1381 verifyFormat("for (;;)\n"
1382 " while (true) continue;",
1383 AllowsMergedLoops);
1384 verifyFormat("while (true)\n"
1385 " for (;;) continue;",
1386 AllowsMergedLoops);
1387 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1388 " for (;;) continue;",
1389 AllowsMergedLoops);
1390 verifyFormat("for (;;)\n"
1391 " BOOST_FOREACH (int &v, vec) continue;",
1392 AllowsMergedLoops);
1393 verifyFormat("for (;;) // Can't merge this\n"
1394 " continue;",
1395 AllowsMergedLoops);
1396 verifyFormat("for (;;) /* still don't merge */\n"
1397 " continue;",
1398 AllowsMergedLoops);
1399 verifyFormat("do a++;\n"
1400 "while (true);",
1401 AllowsMergedLoops);
1402 verifyFormat("do /* Don't merge */\n"
1403 " a++;\n"
1404 "while (true);",
1405 AllowsMergedLoops);
1406 verifyFormat("do // Don't merge\n"
1407 " a++;\n"
1408 "while (true);",
1409 AllowsMergedLoops);
1410 verifyFormat("do\n"
1411 " // Don't merge\n"
1412 " a++;\n"
1413 "while (true);",
1414 AllowsMergedLoops);
1415
1416 // Without braces labels are interpreted differently.
1417 verifyFormat("{\n"
1418 " do\n"
1419 " label:\n"
1420 " a++;\n"
1421 " while (true);\n"
1422 "}",
1423 AllowsMergedLoops);
1424
1425 // Don't merge if there are comments before the null statement.
1426 verifyFormat("while (1) //\n"
1427 " ;",
1428 AllowsMergedLoops);
1429 verifyFormat("for (;;) /**/\n"
1430 " ;",
1431 AllowsMergedLoops);
1432 verifyFormat("while (true) /**/\n"
1433 " ;",
1434 "while (true) /**/;", AllowsMergedLoops);
1435}
1436
1437TEST_F(FormatTest, FormatShortBracedStatements) {
1438 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1439 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1440 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1441 FormatStyle::SIS_Never);
1442 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1443 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1444 verifyFormat("for (;;) {\n"
1445 " f();\n"
1446 "}");
1447 verifyFormat("/*comment*/ for (;;) {\n"
1448 " f();\n"
1449 "}");
1450 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1451 " f();\n"
1452 "}");
1453 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1454 " f();\n"
1455 "}");
1456 verifyFormat("while (true) {\n"
1457 " f();\n"
1458 "}");
1459 verifyFormat("/*comment*/ while (true) {\n"
1460 " f();\n"
1461 "}");
1462 verifyFormat("if (true) {\n"
1463 " f();\n"
1464 "}");
1465 verifyFormat("/*comment*/ if (true) {\n"
1466 " f();\n"
1467 "}");
1468
1469 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1470 FormatStyle::SBS_Empty;
1471 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1472 FormatStyle::SIS_WithoutElse;
1473 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1474 verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1475 verifyFormat("if (i > 0) {\n"
1476 " return i;\n"
1477 "}",
1478 AllowSimpleBracedStatements);
1479
1480 AllowSimpleBracedStatements.IfMacros.push_back(x: "MYIF");
1481 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1482 // Not IF to avoid any confusion that IF is somehow special.
1483 AllowSimpleBracedStatements.IfMacros.push_back(x: "FI");
1484 AllowSimpleBracedStatements.ColumnLimit = 40;
1485 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1486 FormatStyle::SBS_Always;
1487 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1488 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1489 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1490 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1491
1492 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1493 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1494 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1495 verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1496 verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1497 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1498 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1499 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1500 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1501 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1502 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1503 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1504 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1505 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1506 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1507 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1508 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1509 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1510 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1511 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1512 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1513 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1514 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1515 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1516 AllowSimpleBracedStatements);
1517 verifyFormat("if (true) {\n"
1518 " ffffffffffffffffffffffff();\n"
1519 "}",
1520 AllowSimpleBracedStatements);
1521 verifyFormat("if (true) {\n"
1522 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1523 "}",
1524 AllowSimpleBracedStatements);
1525 verifyFormat("if (true) { //\n"
1526 " f();\n"
1527 "}",
1528 AllowSimpleBracedStatements);
1529 verifyFormat("if (true) {\n"
1530 " f();\n"
1531 " f();\n"
1532 "}",
1533 AllowSimpleBracedStatements);
1534 verifyFormat("if (true) {\n"
1535 " f();\n"
1536 "} else {\n"
1537 " f();\n"
1538 "}",
1539 AllowSimpleBracedStatements);
1540 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1541 AllowSimpleBracedStatements);
1542 verifyFormat("MYIF (true) {\n"
1543 " ffffffffffffffffffffffff();\n"
1544 "}",
1545 AllowSimpleBracedStatements);
1546 verifyFormat("MYIF (true) {\n"
1547 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1548 "}",
1549 AllowSimpleBracedStatements);
1550 verifyFormat("MYIF (true) { //\n"
1551 " f();\n"
1552 "}",
1553 AllowSimpleBracedStatements);
1554 verifyFormat("MYIF (true) {\n"
1555 " f();\n"
1556 " f();\n"
1557 "}",
1558 AllowSimpleBracedStatements);
1559 verifyFormat("MYIF (true) {\n"
1560 " f();\n"
1561 "} else {\n"
1562 " f();\n"
1563 "}",
1564 AllowSimpleBracedStatements);
1565
1566 verifyFormat("struct A2 {\n"
1567 " int X;\n"
1568 "};",
1569 AllowSimpleBracedStatements);
1570 verifyFormat("typedef struct A2 {\n"
1571 " int X;\n"
1572 "} A2_t;",
1573 AllowSimpleBracedStatements);
1574 verifyFormat("template <int> struct A2 {\n"
1575 " struct B {};\n"
1576 "};",
1577 AllowSimpleBracedStatements);
1578
1579 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1580 FormatStyle::SIS_Never;
1581 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1582 verifyFormat("if (true) {\n"
1583 " f();\n"
1584 "}",
1585 AllowSimpleBracedStatements);
1586 verifyFormat("if (true) {\n"
1587 " f();\n"
1588 "} else {\n"
1589 " f();\n"
1590 "}",
1591 AllowSimpleBracedStatements);
1592 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1593 verifyFormat("MYIF (true) {\n"
1594 " f();\n"
1595 "}",
1596 AllowSimpleBracedStatements);
1597 verifyFormat("MYIF (true) {\n"
1598 " f();\n"
1599 "} else {\n"
1600 " f();\n"
1601 "}",
1602 AllowSimpleBracedStatements);
1603
1604 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1605 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1606 verifyFormat("while (true) {\n"
1607 " f();\n"
1608 "}",
1609 AllowSimpleBracedStatements);
1610 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1611 verifyFormat("for (;;) {\n"
1612 " f();\n"
1613 "}",
1614 AllowSimpleBracedStatements);
1615 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1616 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1617 " f();\n"
1618 "}",
1619 AllowSimpleBracedStatements);
1620
1621 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1622 FormatStyle::SIS_WithoutElse;
1623 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1624 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1625 FormatStyle::BWACS_Always;
1626
1627 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1628 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1629 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1630 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1631 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1632 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1633 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1634 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1635 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1636 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1637 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1638 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1639 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1640 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1641 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1642 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1643 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1644 AllowSimpleBracedStatements);
1645 verifyFormat("if (true)\n"
1646 "{\n"
1647 " ffffffffffffffffffffffff();\n"
1648 "}",
1649 AllowSimpleBracedStatements);
1650 verifyFormat("if (true)\n"
1651 "{\n"
1652 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1653 "}",
1654 AllowSimpleBracedStatements);
1655 verifyFormat("if (true)\n"
1656 "{ //\n"
1657 " f();\n"
1658 "}",
1659 AllowSimpleBracedStatements);
1660 verifyFormat("if (true)\n"
1661 "{\n"
1662 " f();\n"
1663 " f();\n"
1664 "}",
1665 AllowSimpleBracedStatements);
1666 verifyFormat("if (true)\n"
1667 "{\n"
1668 " f();\n"
1669 "} else\n"
1670 "{\n"
1671 " f();\n"
1672 "}",
1673 AllowSimpleBracedStatements);
1674 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1675 AllowSimpleBracedStatements);
1676 verifyFormat("MYIF (true)\n"
1677 "{\n"
1678 " ffffffffffffffffffffffff();\n"
1679 "}",
1680 AllowSimpleBracedStatements);
1681 verifyFormat("MYIF (true)\n"
1682 "{\n"
1683 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1684 "}",
1685 AllowSimpleBracedStatements);
1686 verifyFormat("MYIF (true)\n"
1687 "{ //\n"
1688 " f();\n"
1689 "}",
1690 AllowSimpleBracedStatements);
1691 verifyFormat("MYIF (true)\n"
1692 "{\n"
1693 " f();\n"
1694 " f();\n"
1695 "}",
1696 AllowSimpleBracedStatements);
1697 verifyFormat("MYIF (true)\n"
1698 "{\n"
1699 " f();\n"
1700 "} else\n"
1701 "{\n"
1702 " f();\n"
1703 "}",
1704 AllowSimpleBracedStatements);
1705
1706 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1707 FormatStyle::SIS_Never;
1708 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1709 verifyFormat("if (true)\n"
1710 "{\n"
1711 " f();\n"
1712 "}",
1713 AllowSimpleBracedStatements);
1714 verifyFormat("if (true)\n"
1715 "{\n"
1716 " f();\n"
1717 "} else\n"
1718 "{\n"
1719 " f();\n"
1720 "}",
1721 AllowSimpleBracedStatements);
1722 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1723 verifyFormat("MYIF (true)\n"
1724 "{\n"
1725 " f();\n"
1726 "}",
1727 AllowSimpleBracedStatements);
1728 verifyFormat("MYIF (true)\n"
1729 "{\n"
1730 " f();\n"
1731 "} else\n"
1732 "{\n"
1733 " f();\n"
1734 "}",
1735 AllowSimpleBracedStatements);
1736
1737 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1738 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1739 verifyFormat("while (true)\n"
1740 "{\n"
1741 " f();\n"
1742 "}",
1743 AllowSimpleBracedStatements);
1744 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1745 verifyFormat("for (;;)\n"
1746 "{\n"
1747 " f();\n"
1748 "}",
1749 AllowSimpleBracedStatements);
1750 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1751 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1752 "{\n"
1753 " f();\n"
1754 "}",
1755 AllowSimpleBracedStatements);
1756
1757 FormatStyle Style = getLLVMStyle();
1758 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1759 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1760
1761 verifyFormat("while (i > 0)\n"
1762 "{\n"
1763 " --i;\n"
1764 "}",
1765 Style);
1766
1767 verifyFormat("if (a)\n"
1768 "{\n"
1769 " ++b;\n"
1770 "}",
1771 Style);
1772
1773 verifyFormat("if (a)\n"
1774 "{\n"
1775 " b = 1;\n"
1776 "} else\n"
1777 "{\n"
1778 " b = 0;\n"
1779 "}",
1780 Style);
1781
1782 verifyFormat("if (a)\n"
1783 "{\n"
1784 " b = 1;\n"
1785 "} else if (c)\n"
1786 "{\n"
1787 " b = 2;\n"
1788 "} else\n"
1789 "{\n"
1790 " b = 0;\n"
1791 "}",
1792 Style);
1793
1794 Style.BraceWrapping.BeforeElse = true;
1795
1796 verifyFormat("if (a)\n"
1797 "{\n"
1798 " b = 1;\n"
1799 "}\n"
1800 "else\n"
1801 "{\n"
1802 " b = 0;\n"
1803 "}",
1804 Style);
1805
1806 verifyFormat("if (a)\n"
1807 "{\n"
1808 " b = 1;\n"
1809 "}\n"
1810 "else if (c)\n"
1811 "{\n"
1812 " b = 2;\n"
1813 "}\n"
1814 "else\n"
1815 "{\n"
1816 " b = 0;\n"
1817 "}",
1818 Style);
1819}
1820
1821TEST_F(FormatTest, UnderstandsMacros) {
1822 verifyFormat("#define A (parentheses)");
1823 verifyFormat("/* comment */ #define A (parentheses)");
1824 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1825 // Even the partial code should never be merged.
1826 verifyNoChange("/* comment */ #define A (parentheses)\n"
1827 "#");
1828 verifyFormat("/* comment */ #define A (parentheses)\n"
1829 "#\n");
1830 verifyFormat("/* comment */ #define A (parentheses)\n"
1831 "#define B (parentheses)");
1832 verifyFormat("#define true ((int)1)");
1833 verifyFormat("#define and(x)");
1834 verifyFormat("#define if(x) x");
1835 verifyFormat("#define return(x) (x)");
1836 verifyFormat("#define while(x) for (; x;)");
1837 verifyFormat("#define xor(x) (^(x))");
1838 verifyFormat("#define __except(x)");
1839 verifyFormat("#define __try(x)");
1840
1841 // https://llvm.org/PR54348.
1842 verifyFormat(
1843 "#define A"
1844 " "
1845 "\\\n"
1846 " class & {}");
1847
1848 FormatStyle Style = getLLVMStyle();
1849 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1850 Style.BraceWrapping.AfterFunction = true;
1851 // Test that a macro definition never gets merged with the following
1852 // definition.
1853 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1854 verifyFormat("#define AAA "
1855 " \\\n"
1856 " N "
1857 " \\\n"
1858 " {\n"
1859 "#define BBB }",
1860 Style);
1861 // verifyFormat("#define AAA N { //", Style);
1862
1863 verifyFormat("MACRO(return)");
1864 verifyFormat("MACRO(co_await)");
1865 verifyFormat("MACRO(co_return)");
1866 verifyFormat("MACRO(co_yield)");
1867 verifyFormat("MACRO(return, something)");
1868 verifyFormat("MACRO(co_return, something)");
1869 verifyFormat("MACRO(something##something)");
1870 verifyFormat("MACRO(return##something)");
1871 verifyFormat("MACRO(co_return##something)");
1872
1873 verifyFormat("#define A x:");
1874
1875 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1876 " { \\\n"
1877 " #Bar \\\n"
1878 " }");
1879 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1880 " { #Bar }");
1881}
1882
1883TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1884 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
1885 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1886 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1887 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1888 verifyFormat("#define A \\\n"
1889 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1890 " { \\\n"
1891 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1892 " }\n"
1893 "X;",
1894 "#define A \\\n"
1895 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1896 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1897 " }\n"
1898 "X;",
1899 Style);
1900}
1901
1902TEST_F(FormatTest, ParseIfElse) {
1903 verifyFormat("if (true)\n"
1904 " if (true)\n"
1905 " if (true)\n"
1906 " f();\n"
1907 " else\n"
1908 " g();\n"
1909 " else\n"
1910 " h();\n"
1911 "else\n"
1912 " i();");
1913 verifyFormat("if (true)\n"
1914 " if (true)\n"
1915 " if (true) {\n"
1916 " if (true)\n"
1917 " f();\n"
1918 " } else {\n"
1919 " g();\n"
1920 " }\n"
1921 " else\n"
1922 " h();\n"
1923 "else {\n"
1924 " i();\n"
1925 "}");
1926 verifyFormat("if (true)\n"
1927 " if constexpr (true)\n"
1928 " if (true) {\n"
1929 " if constexpr (true)\n"
1930 " f();\n"
1931 " } else {\n"
1932 " g();\n"
1933 " }\n"
1934 " else\n"
1935 " h();\n"
1936 "else {\n"
1937 " i();\n"
1938 "}");
1939 verifyFormat("if (true)\n"
1940 " if CONSTEXPR (true)\n"
1941 " if (true) {\n"
1942 " if CONSTEXPR (true)\n"
1943 " f();\n"
1944 " } else {\n"
1945 " g();\n"
1946 " }\n"
1947 " else\n"
1948 " h();\n"
1949 "else {\n"
1950 " i();\n"
1951 "}");
1952 verifyFormat("void f() {\n"
1953 " if (a) {\n"
1954 " } else {\n"
1955 " }\n"
1956 "}");
1957}
1958
1959TEST_F(FormatTest, ElseIf) {
1960 verifyFormat("if (a) {\n} else if (b) {\n}");
1961 verifyFormat("if (a)\n"
1962 " f();\n"
1963 "else if (b)\n"
1964 " g();\n"
1965 "else\n"
1966 " h();");
1967 verifyFormat("if (a)\n"
1968 " f();\n"
1969 "else // comment\n"
1970 " if (b) {\n"
1971 " g();\n"
1972 " h();\n"
1973 " }");
1974 verifyFormat("if constexpr (a)\n"
1975 " f();\n"
1976 "else if constexpr (b)\n"
1977 " g();\n"
1978 "else\n"
1979 " h();");
1980 verifyFormat("if CONSTEXPR (a)\n"
1981 " f();\n"
1982 "else if CONSTEXPR (b)\n"
1983 " g();\n"
1984 "else\n"
1985 " h();");
1986 verifyFormat("if (a) {\n"
1987 " f();\n"
1988 "}\n"
1989 "// or else ..\n"
1990 "else {\n"
1991 " g()\n"
1992 "}");
1993
1994 verifyFormat("if (a) {\n"
1995 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1997 "}");
1998 verifyFormat("if (a) {\n"
1999 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2001 "}");
2002 verifyFormat("if (a) {\n"
2003 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2005 "}");
2006 verifyFormat("if (a) {\n"
2007 "} else if (\n"
2008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2009 "}",
2010 getLLVMStyleWithColumns(62));
2011 verifyFormat("if (a) {\n"
2012 "} else if constexpr (\n"
2013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2014 "}",
2015 getLLVMStyleWithColumns(62));
2016 verifyFormat("if (a) {\n"
2017 "} else if CONSTEXPR (\n"
2018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2019 "}",
2020 getLLVMStyleWithColumns(62));
2021}
2022
2023TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2024 FormatStyle Style = getLLVMStyle();
2025 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2026 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2027 verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2028 verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2029 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2030 verifyFormat("int *f1(int &a) const &;", Style);
2031 verifyFormat("int *f1(int &a) const & = 0;", Style);
2032 verifyFormat("int *a = f1();", Style);
2033 verifyFormat("int &b = f2();", Style);
2034 verifyFormat("int &&c = f3();", Style);
2035 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2036 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2037 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2038 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2039 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2040 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2041 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2042 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2043 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2044 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2045 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2046 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2047 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2048 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2049 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2050 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2051 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2052 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2053 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2054 verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2055 verifyFormat(
2056 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2057 " res2 = [](int &a) { return 0000000000000; };",
2058 Style);
2059
2060 Style.AlignConsecutiveDeclarations.Enabled = true;
2061 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2062 verifyFormat("Const unsigned int *c;\n"
2063 "const unsigned int *d;\n"
2064 "Const unsigned int &e;\n"
2065 "const unsigned int &f;\n"
2066 "int *f1(int *a, int &b, int &&c);\n"
2067 "double *(*f2)(int *a, double &&b);\n"
2068 "const unsigned &&g;\n"
2069 "Const unsigned h;",
2070 Style);
2071 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2072 verifyFormat("Const unsigned int *c;\n"
2073 "const unsigned int *d;\n"
2074 "Const unsigned int &e;\n"
2075 "const unsigned int &f;\n"
2076 "int *f1(int *a, int &b, int &&c);\n"
2077 "double *(*f2)(int *a, double &&b);\n"
2078 "const unsigned &&g;\n"
2079 "Const unsigned h;",
2080 Style);
2081
2082 Style.PointerAlignment = FormatStyle::PAS_Left;
2083 Style.ReferenceAlignment = FormatStyle::RAS_Pointer;
2084 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2085 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2086 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2087 verifyFormat("int* f1(int& a) const& = 0;", Style);
2088 verifyFormat("int* a = f1();", Style);
2089 verifyFormat("int& b = f2();", Style);
2090 verifyFormat("int&& c = f3();", Style);
2091 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2092 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2093 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2094 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2095 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2096 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2097 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2098 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2099 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2100 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2101 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2102 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2103 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2104 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2105 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2106 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2107 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2108 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2109 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2110 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2111 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2112 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2113 verifyFormat(
2114 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2115 " res2 = [](int& a) { return 0000000000000; };",
2116 Style);
2117
2118 Style.AlignConsecutiveDeclarations.Enabled = true;
2119 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2120 verifyFormat("Const unsigned int* c;\n"
2121 "const unsigned int* d;\n"
2122 "Const unsigned int& e;\n"
2123 "const unsigned int& f;\n"
2124 "int* f1(int* a, int& b, int&& c);\n"
2125 "double* (*f2)(int* a, double&& b);\n"
2126 "const unsigned&& g;\n"
2127 "Const unsigned h;",
2128 Style);
2129 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2130 verifyFormat("Const unsigned int* c;\n"
2131 "const unsigned int* d;\n"
2132 "Const unsigned int& e;\n"
2133 "const unsigned int& f;\n"
2134 "int* f1(int* a, int& b, int&& c);\n"
2135 "double* (*f2)(int* a, double&& b);\n"
2136 "const unsigned&& g;\n"
2137 "Const unsigned h;",
2138 Style);
2139
2140 Style.PointerAlignment = FormatStyle::PAS_Right;
2141 Style.ReferenceAlignment = FormatStyle::RAS_Left;
2142 verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2143 verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2144 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2145 verifyFormat("int *a = f1();", Style);
2146 verifyFormat("int& b = f2();", Style);
2147 verifyFormat("int&& c = f3();", Style);
2148 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2149 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2150 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2151 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2152 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2153 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2154 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2155
2156 Style.AlignConsecutiveDeclarations.Enabled = true;
2157 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2158 verifyFormat("Const unsigned int *c;\n"
2159 "const unsigned int *d;\n"
2160 "Const unsigned int& e;\n"
2161 "const unsigned int& f;\n"
2162 "int *f1(int *a, int& b, int&& c);\n"
2163 "double *(*f2)(int *a, double&& b);\n"
2164 "const unsigned&& g;\n"
2165 "Const unsigned h;",
2166 Style);
2167 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2168 verifyFormat("Const unsigned int *c;\n"
2169 "const unsigned int *d;\n"
2170 "Const unsigned int& e;\n"
2171 "const unsigned int& f;\n"
2172 "int *f1(int *a, int& b, int&& c);\n"
2173 "double *(*f2)(int *a, double&& b);\n"
2174 "const unsigned&& g;\n"
2175 "Const unsigned h;",
2176 Style);
2177
2178 Style.PointerAlignment = FormatStyle::PAS_Left;
2179 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2180 verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2181 verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2182 verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2183 verifyFormat("int* a = f1();", Style);
2184 verifyFormat("int & b = f2();", Style);
2185 verifyFormat("int && c = f3();", Style);
2186 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2187 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2188 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2189 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2190 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2191 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2192 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2193 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2194 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2195 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2196 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2197 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2198 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2199 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2200 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2201 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2202 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2203 verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2204 verifyFormat(
2205 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2206 " res2 = [](int & a) { return 0000000000000; };",
2207 Style);
2208
2209 Style.AlignConsecutiveDeclarations.Enabled = true;
2210 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2211 verifyFormat("Const unsigned int* c;\n"
2212 "const unsigned int* d;\n"
2213 "Const unsigned int & e;\n"
2214 "const unsigned int & f;\n"
2215 "int* f1(int* a, int & b, int && c);\n"
2216 "double* (*f2)(int* a, double && b);\n"
2217 "const unsigned && g;\n"
2218 "Const unsigned h;",
2219 Style);
2220 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2221 verifyFormat("Const unsigned int* c;\n"
2222 "const unsigned int* d;\n"
2223 "Const unsigned int & e;\n"
2224 "const unsigned int & f;\n"
2225 "int* f1(int* a, int & b, int && c);\n"
2226 "double* (*f2)(int* a, double && b);\n"
2227 "const unsigned && g;\n"
2228 "Const unsigned h;",
2229 Style);
2230
2231 Style.PointerAlignment = FormatStyle::PAS_Middle;
2232 Style.ReferenceAlignment = FormatStyle::RAS_Right;
2233 verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2234 verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2235 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2236 verifyFormat("int * a = f1();", Style);
2237 verifyFormat("int &b = f2();", Style);
2238 verifyFormat("int &&c = f3();", Style);
2239 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2240 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2241 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2242 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2243 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2244 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2245 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2246
2247 Style.AlignConsecutiveDeclarations.Enabled = true;
2248 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2249 verifyFormat("Const unsigned int * c;\n"
2250 "const unsigned int * d;\n"
2251 "Const unsigned int &e;\n"
2252 "const unsigned int &f;\n"
2253 "int * f1(int * a, int &b, int &&c);\n"
2254 "double * (*f2)(int * a, double &&b);\n"
2255 "const unsigned &&g;\n"
2256 "Const unsigned h;",
2257 Style);
2258 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2259 verifyFormat("Const unsigned int * c;\n"
2260 "const unsigned int * d;\n"
2261 "Const unsigned int &e;\n"
2262 "const unsigned int &f;\n"
2263 "int * f1(int * a, int &b, int &&c);\n"
2264 "double * (*f2)(int * a, double &&b);\n"
2265 "const unsigned &&g;\n"
2266 "Const unsigned h;",
2267 Style);
2268
2269 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2270 // specifically handled
2271 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2272}
2273
2274TEST_F(FormatTest, FormatsForLoop) {
2275 verifyFormat(
2276 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2277 " ++VeryVeryLongLoopVariable)\n"
2278 " ;");
2279 verifyFormat("for (;;)\n"
2280 " f();");
2281 verifyFormat("for (;;) {\n}");
2282 verifyFormat("for (;;) {\n"
2283 " f();\n"
2284 "}");
2285 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2286
2287 verifyFormat(
2288 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2289 " E = UnwrappedLines.end();\n"
2290 " I != E; ++I) {\n}");
2291
2292 verifyFormat(
2293 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2294 " ++IIIII) {\n}");
2295 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2296 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2297 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2298 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2299 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2300 " E = FD->getDeclsInPrototypeScope().end();\n"
2301 " I != E; ++I) {\n}");
2302 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2303 " I = Container.begin(),\n"
2304 " E = Container.end();\n"
2305 " I != E; ++I) {\n}",
2306 getLLVMStyleWithColumns(76));
2307
2308 verifyFormat(
2309 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2313 " ++aaaaaaaaaaa) {\n}");
2314 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2315 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2316 " ++i) {\n}");
2317 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2318 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2319 "}");
2320 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2321 " aaaaaaaaaa);\n"
2322 " iter; ++iter) {\n"
2323 "}");
2324 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2326 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2327 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2328
2329 // These should not be formatted as Objective-C for-in loops.
2330 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2331 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2332 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2333 verifyFormat(
2334 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2335
2336 FormatStyle NoBinPacking = getLLVMStyle();
2337 NoBinPacking.BinPackParameters = false;
2338 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2339 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2340 " aaaaaaaaaaaaaaaa,\n"
2341 " aaaaaaaaaaaaaaaa,\n"
2342 " aaaaaaaaaaaaaaaa);\n"
2343 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2344 "}",
2345 NoBinPacking);
2346 verifyFormat(
2347 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2348 " E = UnwrappedLines.end();\n"
2349 " I != E;\n"
2350 " ++I) {\n}",
2351 NoBinPacking);
2352
2353 FormatStyle AlignLeft = getLLVMStyle();
2354 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2355 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2356}
2357
2358TEST_F(FormatTest, RangeBasedForLoops) {
2359 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2361 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2362 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2363 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2365 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2366 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2367}
2368
2369TEST_F(FormatTest, ForEachLoops) {
2370 FormatStyle Style = getLLVMStyle();
2371 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2372 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2373 verifyFormat("void f() {\n"
2374 " for (;;) {\n"
2375 " }\n"
2376 " foreach (Item *item, itemlist) {\n"
2377 " }\n"
2378 " Q_FOREACH (Item *item, itemlist) {\n"
2379 " }\n"
2380 " BOOST_FOREACH (Item *item, itemlist) {\n"
2381 " }\n"
2382 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2383 "}",
2384 Style);
2385 verifyFormat("void f() {\n"
2386 " for (;;)\n"
2387 " int j = 1;\n"
2388 " Q_FOREACH (int v, vec)\n"
2389 " v *= 2;\n"
2390 " for (;;) {\n"
2391 " int j = 1;\n"
2392 " }\n"
2393 " Q_FOREACH (int v, vec) {\n"
2394 " v *= 2;\n"
2395 " }\n"
2396 "}",
2397 Style);
2398
2399 FormatStyle ShortBlocks = getLLVMStyle();
2400 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2401 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2402 verifyFormat("void f() {\n"
2403 " for (;;)\n"
2404 " int j = 1;\n"
2405 " Q_FOREACH (int &v, vec)\n"
2406 " v *= 2;\n"
2407 " for (;;) {\n"
2408 " int j = 1;\n"
2409 " }\n"
2410 " Q_FOREACH (int &v, vec) {\n"
2411 " int j = 1;\n"
2412 " }\n"
2413 "}",
2414 ShortBlocks);
2415
2416 FormatStyle ShortLoops = getLLVMStyle();
2417 ShortLoops.AllowShortLoopsOnASingleLine = true;
2418 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2419 verifyFormat("void f() {\n"
2420 " for (;;) int j = 1;\n"
2421 " Q_FOREACH (int &v, vec) int j = 1;\n"
2422 " for (;;) {\n"
2423 " int j = 1;\n"
2424 " }\n"
2425 " Q_FOREACH (int &v, vec) {\n"
2426 " int j = 1;\n"
2427 " }\n"
2428 "}",
2429 ShortLoops);
2430
2431 FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2432 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2433 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2434 verifyFormat("void f() {\n"
2435 " for (;;) int j = 1;\n"
2436 " Q_FOREACH (int &v, vec) int j = 1;\n"
2437 " for (;;) { int j = 1; }\n"
2438 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2439 "}",
2440 ShortBlocksAndLoops);
2441
2442 Style.SpaceBeforeParens =
2443 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2444 verifyFormat("void f() {\n"
2445 " for (;;) {\n"
2446 " }\n"
2447 " foreach(Item *item, itemlist) {\n"
2448 " }\n"
2449 " Q_FOREACH(Item *item, itemlist) {\n"
2450 " }\n"
2451 " BOOST_FOREACH(Item *item, itemlist) {\n"
2452 " }\n"
2453 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2454 "}",
2455 Style);
2456
2457 // As function-like macros.
2458 verifyFormat("#define foreach(x, y)\n"
2459 "#define Q_FOREACH(x, y)\n"
2460 "#define BOOST_FOREACH(x, y)\n"
2461 "#define UNKNOWN_FOREACH(x, y)");
2462
2463 // Not as function-like macros.
2464 verifyFormat("#define foreach (x, y)\n"
2465 "#define Q_FOREACH (x, y)\n"
2466 "#define BOOST_FOREACH (x, y)\n"
2467 "#define UNKNOWN_FOREACH (x, y)");
2468
2469 // handle microsoft non standard extension
2470 verifyFormat("for each (char c in x->MyStringProperty)");
2471}
2472
2473TEST_F(FormatTest, FormatsWhileLoop) {
2474 verifyFormat("while (true) {\n}");
2475 verifyFormat("while (true)\n"
2476 " f();");
2477 verifyFormat("while () {\n}");
2478 verifyFormat("while () {\n"
2479 " f();\n"
2480 "}");
2481}
2482
2483TEST_F(FormatTest, FormatsDoWhile) {
2484 verifyFormat("do {\n"
2485 " do_something();\n"
2486 "} while (something());");
2487 verifyFormat("do\n"
2488 " do_something();\n"
2489 "while (something());");
2490}
2491
2492TEST_F(FormatTest, FormatsSwitchStatement) {
2493 verifyFormat("switch (x) {\n"
2494 "case 1:\n"
2495 " f();\n"
2496 " break;\n"
2497 "case kFoo:\n"
2498 "case ns::kBar:\n"
2499 "case kBaz:\n"
2500 " break;\n"
2501 "default:\n"
2502 " g();\n"
2503 " break;\n"
2504 "}");
2505 verifyFormat("switch (x) {\n"
2506 "case 1: {\n"
2507 " f();\n"
2508 " break;\n"
2509 "}\n"
2510 "case 2: {\n"
2511 " break;\n"
2512 "}\n"
2513 "}");
2514 verifyFormat("switch (x) {\n"
2515 "case 1: {\n"
2516 " f();\n"
2517 " {\n"
2518 " g();\n"
2519 " h();\n"
2520 " }\n"
2521 " break;\n"
2522 "}\n"
2523 "}");
2524 verifyFormat("switch (x) {\n"
2525 "case 1: {\n"
2526 " f();\n"
2527 " if (foo) {\n"
2528 " g();\n"
2529 " h();\n"
2530 " }\n"
2531 " break;\n"
2532 "}\n"
2533 "}");
2534 verifyFormat("switch (x) {\n"
2535 "case 1: {\n"
2536 " f();\n"
2537 " g();\n"
2538 "} break;\n"
2539 "}");
2540 verifyFormat("switch (test)\n"
2541 " ;");
2542 verifyFormat("switch (x) {\n"
2543 "default: {\n"
2544 " // Do nothing.\n"
2545 "}\n"
2546 "}");
2547 verifyFormat("switch (x) {\n"
2548 "// comment\n"
2549 "// if 1, do f()\n"
2550 "case 1:\n"
2551 " f();\n"
2552 "}");
2553 verifyFormat("switch (x) {\n"
2554 "case 1:\n"
2555 " // Do amazing stuff\n"
2556 " {\n"
2557 " f();\n"
2558 " g();\n"
2559 " }\n"
2560 " break;\n"
2561 "}");
2562 verifyFormat("#define A \\\n"
2563 " switch (x) { \\\n"
2564 " case a: \\\n"
2565 " foo = b; \\\n"
2566 " }",
2567 getLLVMStyleWithColumns(20));
2568 verifyFormat("#define OPERATION_CASE(name) \\\n"
2569 " case OP_name: \\\n"
2570 " return operations::Operation##name",
2571 getLLVMStyleWithColumns(40));
2572 verifyFormat("switch (x) {\n"
2573 "case 1:;\n"
2574 "default:;\n"
2575 " int i;\n"
2576 "}");
2577
2578 verifyGoogleFormat("switch (x) {\n"
2579 " case 1:\n"
2580 " f();\n"
2581 " break;\n"
2582 " case kFoo:\n"
2583 " case ns::kBar:\n"
2584 " case kBaz:\n"
2585 " break;\n"
2586 " default:\n"
2587 " g();\n"
2588 " break;\n"
2589 "}");
2590 verifyGoogleFormat("switch (x) {\n"
2591 " case 1: {\n"
2592 " f();\n"
2593 " break;\n"
2594 " }\n"
2595 "}");
2596 verifyGoogleFormat("switch (test)\n"
2597 " ;");
2598
2599 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2600 " case OP_name: \\\n"
2601 " return operations::Operation##name");
2602 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2603 " // Get the correction operation class.\n"
2604 " switch (OpCode) {\n"
2605 " CASE(Add);\n"
2606 " CASE(Subtract);\n"
2607 " default:\n"
2608 " return operations::Unknown;\n"
2609 " }\n"
2610 "#undef OPERATION_CASE\n"
2611 "}");
2612 verifyFormat("DEBUG({\n"
2613 " switch (x) {\n"
2614 " case A:\n"
2615 " f();\n"
2616 " break;\n"
2617 " // fallthrough\n"
2618 " case B:\n"
2619 " g();\n"
2620 " break;\n"
2621 " }\n"
2622 "});");
2623 verifyNoChange("DEBUG({\n"
2624 " switch (x) {\n"
2625 " case A:\n"
2626 " f();\n"
2627 " break;\n"
2628 " // On B:\n"
2629 " case B:\n"
2630 " g();\n"
2631 " break;\n"
2632 " }\n"
2633 "});");
2634 verifyFormat("switch (n) {\n"
2635 "case 0: {\n"
2636 " return false;\n"
2637 "}\n"
2638 "default: {\n"
2639 " return true;\n"
2640 "}\n"
2641 "}",
2642 "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 verifyFormat("switch (a) {\n"
2652 "case (b):\n"
2653 " return;\n"
2654 "}");
2655
2656 verifyFormat("switch (a) {\n"
2657 "case some_namespace::\n"
2658 " some_constant:\n"
2659 " return;\n"
2660 "}",
2661 getLLVMStyleWithColumns(34));
2662
2663 verifyFormat("switch (a) {\n"
2664 "[[likely]] case 1:\n"
2665 " return;\n"
2666 "}");
2667 verifyFormat("switch (a) {\n"
2668 "[[likely]] [[other::likely]] case 1:\n"
2669 " return;\n"
2670 "}");
2671 verifyFormat("switch (x) {\n"
2672 "case 1:\n"
2673 " return;\n"
2674 "[[likely]] case 2:\n"
2675 " return;\n"
2676 "}");
2677 verifyFormat("switch (a) {\n"
2678 "case 1:\n"
2679 "[[likely]] case 2:\n"
2680 " return;\n"
2681 "}");
2682 FormatStyle Attributes = getLLVMStyle();
2683 Attributes.AttributeMacros.push_back(x: "LIKELY");
2684 Attributes.AttributeMacros.push_back(x: "OTHER_LIKELY");
2685 verifyFormat("switch (a) {\n"
2686 "LIKELY case b:\n"
2687 " return;\n"
2688 "}",
2689 Attributes);
2690 verifyFormat("switch (a) {\n"
2691 "LIKELY OTHER_LIKELY() case b:\n"
2692 " return;\n"
2693 "}",
2694 Attributes);
2695 verifyFormat("switch (a) {\n"
2696 "case 1:\n"
2697 " return;\n"
2698 "LIKELY case 2:\n"
2699 " return;\n"
2700 "}",
2701 Attributes);
2702 verifyFormat("switch (a) {\n"
2703 "case 1:\n"
2704 "LIKELY case 2:\n"
2705 " return;\n"
2706 "}",
2707 Attributes);
2708
2709 FormatStyle Style = getLLVMStyle();
2710 Style.IndentCaseLabels = true;
2711 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2712 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2713 Style.BraceWrapping.AfterCaseLabel = true;
2714 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2715 verifyFormat("switch (n)\n"
2716 "{\n"
2717 " case 0:\n"
2718 " {\n"
2719 " return false;\n"
2720 " }\n"
2721 " default:\n"
2722 " {\n"
2723 " return true;\n"
2724 " }\n"
2725 "}",
2726 "switch (n) {\n"
2727 " case 0: {\n"
2728 " return false;\n"
2729 " }\n"
2730 " default: {\n"
2731 " return true;\n"
2732 " }\n"
2733 "}",
2734 Style);
2735 Style.BraceWrapping.AfterCaseLabel = false;
2736 verifyFormat("switch (n)\n"
2737 "{\n"
2738 " case 0: {\n"
2739 " return false;\n"
2740 " }\n"
2741 " default: {\n"
2742 " return true;\n"
2743 " }\n"
2744 "}",
2745 "switch (n) {\n"
2746 " case 0:\n"
2747 " {\n"
2748 " return false;\n"
2749 " }\n"
2750 " default:\n"
2751 " {\n"
2752 " return true;\n"
2753 " }\n"
2754 "}",
2755 Style);
2756 Style.IndentCaseLabels = false;
2757 Style.IndentCaseBlocks = true;
2758 verifyFormat("switch (n)\n"
2759 "{\n"
2760 "case 0:\n"
2761 " {\n"
2762 " return false;\n"
2763 " }\n"
2764 "case 1:\n"
2765 " break;\n"
2766 "default:\n"
2767 " {\n"
2768 " return true;\n"
2769 " }\n"
2770 "}",
2771 "switch (n) {\n"
2772 "case 0: {\n"
2773 " return false;\n"
2774 "}\n"
2775 "case 1:\n"
2776 " break;\n"
2777 "default: {\n"
2778 " return true;\n"
2779 "}\n"
2780 "}",
2781 Style);
2782 Style.IndentCaseLabels = true;
2783 Style.IndentCaseBlocks = true;
2784 verifyFormat("switch (n)\n"
2785 "{\n"
2786 " case 0:\n"
2787 " {\n"
2788 " return false;\n"
2789 " }\n"
2790 " case 1:\n"
2791 " break;\n"
2792 " default:\n"
2793 " {\n"
2794 " return true;\n"
2795 " }\n"
2796 "}",
2797 "switch (n) {\n"
2798 "case 0: {\n"
2799 " return false;\n"
2800 "}\n"
2801 "case 1:\n"
2802 " break;\n"
2803 "default: {\n"
2804 " return true;\n"
2805 "}\n"
2806 "}",
2807 Style);
2808}
2809
2810TEST_F(FormatTest, CaseRanges) {
2811 verifyFormat("switch (x) {\n"
2812 "case 'A' ... 'Z':\n"
2813 "case 1 ... 5:\n"
2814 "case a ... b:\n"
2815 " break;\n"
2816 "}");
2817}
2818
2819TEST_F(FormatTest, ShortEnums) {
2820 FormatStyle Style = getLLVMStyle();
2821 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2822 EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2823 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2824 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2825 Style.AllowShortEnumsOnASingleLine = false;
2826 verifyFormat("enum {\n"
2827 " A,\n"
2828 " B,\n"
2829 " C\n"
2830 "} ShortEnum1, ShortEnum2;",
2831 Style);
2832 verifyFormat("typedef enum {\n"
2833 " A,\n"
2834 " B,\n"
2835 " C\n"
2836 "} ShortEnum1, ShortEnum2;",
2837 Style);
2838 verifyFormat("enum {\n"
2839 " A,\n"
2840 "} ShortEnum1, ShortEnum2;",
2841 Style);
2842 verifyFormat("typedef enum {\n"
2843 " A,\n"
2844 "} ShortEnum1, ShortEnum2;",
2845 Style);
2846 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2847 Style.BraceWrapping.AfterEnum = true;
2848 verifyFormat("enum\n"
2849 "{\n"
2850 " A,\n"
2851 " B,\n"
2852 " C\n"
2853 "} ShortEnum1, ShortEnum2;",
2854 Style);
2855 verifyFormat("typedef enum\n"
2856 "{\n"
2857 " A,\n"
2858 " B,\n"
2859 " C\n"
2860 "} ShortEnum1, ShortEnum2;",
2861 Style);
2862}
2863
2864TEST_F(FormatTest, ShortCompoundRequirement) {
2865 FormatStyle Style = getLLVMStyle();
2866 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2867 verifyFormat("template <typename T>\n"
2868 "concept c = requires(T x) {\n"
2869 " { x + 1 } -> std::same_as<int>;\n"
2870 "};",
2871 Style);
2872 verifyFormat("template <typename T>\n"
2873 "concept c = requires(T x) {\n"
2874 " { x + 1 } -> std::same_as<int>;\n"
2875 " { x + 2 } -> std::same_as<int>;\n"
2876 "};",
2877 Style);
2878 Style.AllowShortCompoundRequirementOnASingleLine = false;
2879 verifyFormat("template <typename T>\n"
2880 "concept c = requires(T x) {\n"
2881 " {\n"
2882 " x + 1\n"
2883 " } -> std::same_as<int>;\n"
2884 "};",
2885 Style);
2886 verifyFormat("template <typename T>\n"
2887 "concept c = requires(T x) {\n"
2888 " {\n"
2889 " x + 1\n"
2890 " } -> std::same_as<int>;\n"
2891 " {\n"
2892 " x + 2\n"
2893 " } -> std::same_as<int>;\n"
2894 "};",
2895 Style);
2896}
2897
2898TEST_F(FormatTest, ShortCaseLabels) {
2899 FormatStyle Style = getLLVMStyle();
2900 Style.AllowShortCaseLabelsOnASingleLine = true;
2901 verifyFormat("switch (a) {\n"
2902 "case 1: x = 1; break;\n"
2903 "case 2: return;\n"
2904 "case 3:\n"
2905 "case 4:\n"
2906 "case 5: return;\n"
2907 "case 6: // comment\n"
2908 " return;\n"
2909 "case 7:\n"
2910 " // comment\n"
2911 " return;\n"
2912 "case 8:\n"
2913 " x = 8; // comment\n"
2914 " break;\n"
2915 "default: y = 1; break;\n"
2916 "}",
2917 Style);
2918 verifyFormat("switch (a) {\n"
2919 "case 0: return; // comment\n"
2920 "case 1: break; // comment\n"
2921 "case 2: return;\n"
2922 "// comment\n"
2923 "case 3: return;\n"
2924 "// comment 1\n"
2925 "// comment 2\n"
2926 "// comment 3\n"
2927 "case 4: break; /* comment */\n"
2928 "case 5:\n"
2929 " // comment\n"
2930 " break;\n"
2931 "case 6: /* comment */ x = 1; break;\n"
2932 "case 7: x = /* comment */ 1; break;\n"
2933 "case 8:\n"
2934 " x = 1; /* comment */\n"
2935 " break;\n"
2936 "case 9:\n"
2937 " break; // comment line 1\n"
2938 " // comment line 2\n"
2939 "}",
2940 Style);
2941 verifyFormat("switch (a) {\n"
2942 "case 1:\n"
2943 " x = 8;\n"
2944 " // fall through\n"
2945 "case 2: x = 8;\n"
2946 "// comment\n"
2947 "case 3:\n"
2948 " return; /* comment line 1\n"
2949 " * comment line 2 */\n"
2950 "case 4: i = 8;\n"
2951 "// something else\n"
2952 "#if FOO\n"
2953 "case 5: break;\n"
2954 "#endif\n"
2955 "}",
2956 "switch (a) {\n"
2957 "case 1: x = 8;\n"
2958 " // fall through\n"
2959 "case 2:\n"
2960 " x = 8;\n"
2961 "// comment\n"
2962 "case 3:\n"
2963 " return; /* comment line 1\n"
2964 " * comment line 2 */\n"
2965 "case 4:\n"
2966 " i = 8;\n"
2967 "// something else\n"
2968 "#if FOO\n"
2969 "case 5: break;\n"
2970 "#endif\n"
2971 "}",
2972 Style);
2973 verifyFormat("switch (a) {\n"
2974 "case 0:\n"
2975 " return; // long long long long long long long long long long "
2976 "long long comment\n"
2977 " // line\n"
2978 "}",
2979 "switch (a) {\n"
2980 "case 0: return; // long long long long long long long long "
2981 "long long long long comment line\n"
2982 "}",
2983 Style);
2984 verifyFormat("switch (a) {\n"
2985 "case 0:\n"
2986 " return; /* long long long long long long long long long long "
2987 "long long comment\n"
2988 " line */\n"
2989 "}",
2990 "switch (a) {\n"
2991 "case 0: return; /* long long long long long long long long "
2992 "long long long long comment line */\n"
2993 "}",
2994 Style);
2995 verifyFormat("switch (a) {\n"
2996 "#if FOO\n"
2997 "case 0: return 0;\n"
2998 "#endif\n"
2999 "}",
3000 Style);
3001 verifyFormat("switch (a) {\n"
3002 "case 1: {\n"
3003 "}\n"
3004 "case 2: {\n"
3005 " return;\n"
3006 "}\n"
3007 "case 3: {\n"
3008 " x = 1;\n"
3009 " return;\n"
3010 "}\n"
3011 "case 4:\n"
3012 " if (x)\n"
3013 " return;\n"
3014 "}",
3015 Style);
3016 Style.ColumnLimit = 21;
3017 verifyFormat("#define X \\\n"
3018 " case 0: break;\n"
3019 "#include \"f\"",
3020 Style);
3021 verifyFormat("switch (a) {\n"
3022 "case 1: x = 1; break;\n"
3023 "case 2: return;\n"
3024 "case 3:\n"
3025 "case 4:\n"
3026 "case 5: return;\n"
3027 "default:\n"
3028 " y = 1;\n"
3029 " break;\n"
3030 "}",
3031 Style);
3032 Style.ColumnLimit = 80;
3033 Style.AllowShortCaseLabelsOnASingleLine = false;
3034 Style.IndentCaseLabels = true;
3035 verifyFormat("switch (n) {\n"
3036 " default /*comments*/:\n"
3037 " return true;\n"
3038 " case 0:\n"
3039 " return false;\n"
3040 "}",
3041 "switch (n) {\n"
3042 "default/*comments*/:\n"
3043 " return true;\n"
3044 "case 0:\n"
3045 " return false;\n"
3046 "}",
3047 Style);
3048 Style.AllowShortCaseLabelsOnASingleLine = true;
3049 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3050 Style.BraceWrapping.AfterCaseLabel = true;
3051 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3052 verifyFormat("switch (n)\n"
3053 "{\n"
3054 " case 0:\n"
3055 " {\n"
3056 " return false;\n"
3057 " }\n"
3058 " default:\n"
3059 " {\n"
3060 " return true;\n"
3061 " }\n"
3062 "}",
3063 "switch (n) {\n"
3064 " case 0: {\n"
3065 " return false;\n"
3066 " }\n"
3067 " default:\n"
3068 " {\n"
3069 " return true;\n"
3070 " }\n"
3071 "}",
3072 Style);
3073}
3074
3075TEST_F(FormatTest, FormatsLabels) {
3076 verifyFormat("void f() {\n"
3077 " some_code();\n"
3078 "test_label:\n"
3079 " some_other_code();\n"
3080 " {\n"
3081 " some_more_code();\n"
3082 " another_label:\n"
3083 " some_more_code();\n"
3084 " }\n"
3085 "}");
3086 verifyFormat("{\n"
3087 " some_code();\n"
3088 "test_label:\n"
3089 " some_other_code();\n"
3090 "}");
3091 verifyFormat("{\n"
3092 " some_code();\n"
3093 "test_label:;\n"
3094 " int i = 0;\n"
3095 "}");
3096 verifyFormat("{\n"
3097 " some_code();\n"
3098 "test_label: { some_other_code(); }\n"
3099 "}");
3100 verifyFormat("{\n"
3101 " some_code();\n"
3102 "test_label: {\n"
3103 " some_other_code();\n"
3104 " some_other_code();\n"
3105 "}\n"
3106 "}");
3107 verifyFormat("{\n"
3108 "L0:\n"
3109 "[[foo]] L1:\n"
3110 "[[bar]] [[baz]] L2:\n"
3111 " g();\n"
3112 "}");
3113 verifyFormat("{\n"
3114 "[[foo]] L1: {\n"
3115 "[[bar]] [[baz]] L2:\n"
3116 " g();\n"
3117 "}\n"
3118 "}");
3119 verifyFormat("{\n"
3120 "[[foo]] L1:\n"
3121 " f();\n"
3122 " {\n"
3123 " [[bar]] [[baz]] L2:\n"
3124 " g();\n"
3125 " }\n"
3126 "}");
3127 FormatStyle Style = getLLVMStyle();
3128 Style.IndentGotoLabels = false;
3129 verifyFormat("void f() {\n"
3130 " some_code();\n"
3131 "test_label:\n"
3132 " some_other_code();\n"
3133 " {\n"
3134 " some_more_code();\n"
3135 "another_label:\n"
3136 " some_more_code();\n"
3137 " }\n"
3138 "}",
3139 Style);
3140 verifyFormat("{\n"
3141 " some_code();\n"
3142 "test_label:\n"
3143 " some_other_code();\n"
3144 "}",
3145 Style);
3146 verifyFormat("{\n"
3147 " some_code();\n"
3148 "test_label:;\n"
3149 " int i = 0;\n"
3150 "}",
3151 Style);
3152 verifyFormat("{\n"
3153 " some_code();\n"
3154 "test_label: { some_other_code(); }\n"
3155 "}",
3156 Style);
3157 verifyFormat("{\n"
3158 "[[foo]] L1:\n"
3159 " f();\n"
3160 " {\n"
3161 "[[bar]] [[baz]] L2:\n"
3162 " g();\n"
3163 " }\n"
3164 "}",
3165 Style);
3166 // The opening brace may either be on the same unwrapped line as the colon or
3167 // on a separate one. The formatter should recognize both.
3168 Style = getLLVMStyle();
3169 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman;
3170 verifyFormat("{\n"
3171 " some_code();\n"
3172 "test_label:\n"
3173 "{\n"
3174 " some_other_code();\n"
3175 "}\n"
3176 "}",
3177 Style);
3178 verifyFormat("{\n"
3179 "[[foo]] L1:\n"
3180 "{\n"
3181 "[[bar]] [[baz]] L2:\n"
3182 " g();\n"
3183 "}\n"
3184 "}",
3185 Style);
3186}
3187
3188TEST_F(FormatTest, MultiLineControlStatements) {
3189 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20);
3190 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3191 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3192 // Short lines should keep opening brace on same line.
3193 verifyFormat("if (foo) {\n"
3194 " bar();\n"
3195 "}",
3196 "if(foo){bar();}", Style);
3197 verifyFormat("if (foo) {\n"
3198 " bar();\n"
3199 "} else {\n"
3200 " baz();\n"
3201 "}",
3202 "if(foo){bar();}else{baz();}", Style);
3203 verifyFormat("if (foo && bar) {\n"
3204 " baz();\n"
3205 "}",
3206 "if(foo&&bar){baz();}", Style);
3207 verifyFormat("if (foo) {\n"
3208 " bar();\n"
3209 "} else if (baz) {\n"
3210 " quux();\n"
3211 "}",
3212 "if(foo){bar();}else if(baz){quux();}", Style);
3213 verifyFormat("if (foo) {\n"
3214 " bar();\n"
3215 "} else if (baz) {\n"
3216 " quux();\n"
3217 "} else {\n"
3218 " foobar();\n"
3219 "}",
3220 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3221 verifyFormat("for (;;) {\n"
3222 " foo();\n"
3223 "}",
3224 "for(;;){foo();}");
3225 verifyFormat("while (1) {\n"
3226 " foo();\n"
3227 "}",
3228 "while(1){foo();}", Style);
3229 verifyFormat("switch (foo) {\n"
3230 "case bar:\n"
3231 " return;\n"
3232 "}",
3233 "switch(foo){case bar:return;}", Style);
3234 verifyFormat("try {\n"
3235 " foo();\n"
3236 "} catch (...) {\n"
3237 " bar();\n"
3238 "}",
3239 "try{foo();}catch(...){bar();}", Style);
3240 verifyFormat("do {\n"
3241 " foo();\n"
3242 "} while (bar &&\n"
3243 " baz);",
3244 "do{foo();}while(bar&&baz);", Style);
3245 // Long lines should put opening brace on new line.
3246 verifyFormat("void f() {\n"
3247 " if (a1 && a2 &&\n"
3248 " a3)\n"
3249 " {\n"
3250 " quux();\n"
3251 " }\n"
3252 "}",
3253 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3254 verifyFormat("if (foo && bar &&\n"
3255 " baz)\n"
3256 "{\n"
3257 " quux();\n"
3258 "}",
3259 "if(foo&&bar&&baz){quux();}", Style);
3260 verifyFormat("if (foo && bar &&\n"
3261 " baz)\n"
3262 "{\n"
3263 " quux();\n"
3264 "}",
3265 "if (foo && bar &&\n"
3266 " baz) {\n"
3267 " quux();\n"
3268 "}",
3269 Style);
3270 verifyFormat("if (foo) {\n"
3271 " bar();\n"
3272 "} else if (baz ||\n"
3273 " quux)\n"
3274 "{\n"
3275 " foobar();\n"
3276 "}",
3277 "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3278 verifyFormat("if (foo) {\n"
3279 " bar();\n"
3280 "} else if (baz ||\n"
3281 " quux)\n"
3282 "{\n"
3283 " foobar();\n"
3284 "} else {\n"
3285 " barbaz();\n"
3286 "}",
3287 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3288 Style);
3289 verifyFormat("for (int i = 0;\n"
3290 " i < 10; ++i)\n"
3291 "{\n"
3292 " foo();\n"
3293 "}",
3294 "for(int i=0;i<10;++i){foo();}", Style);
3295 verifyFormat("foreach (int i,\n"
3296 " list)\n"
3297 "{\n"
3298 " foo();\n"
3299 "}",
3300 "foreach(int i, list){foo();}", Style);
3301 Style.ColumnLimit =
3302 40; // to concentrate at brace wrapping, not line wrap due to column limit
3303 verifyFormat("foreach (int i, list) {\n"
3304 " foo();\n"
3305 "}",
3306 "foreach(int i, list){foo();}", Style);
3307 Style.ColumnLimit =
3308 20; // to concentrate at brace wrapping, not line wrap due to column limit
3309 verifyFormat("while (foo || bar ||\n"
3310 " baz)\n"
3311 "{\n"
3312 " quux();\n"
3313 "}",
3314 "while(foo||bar||baz){quux();}", Style);
3315 verifyFormat("switch (\n"
3316 " foo = barbaz)\n"
3317 "{\n"
3318 "case quux:\n"
3319 " return;\n"
3320 "}",
3321 "switch(foo=barbaz){case quux:return;}", Style);
3322 verifyFormat("try {\n"
3323 " foo();\n"
3324 "} catch (\n"
3325 " Exception &bar)\n"
3326 "{\n"
3327 " baz();\n"
3328 "}",
3329 "try{foo();}catch(Exception&bar){baz();}", Style);
3330 Style.ColumnLimit =
3331 40; // to concentrate at brace wrapping, not line wrap due to column limit
3332 verifyFormat("try {\n"
3333 " foo();\n"
3334 "} catch (Exception &bar) {\n"
3335 " baz();\n"
3336 "}",
3337 "try{foo();}catch(Exception&bar){baz();}", Style);
3338 Style.ColumnLimit =
3339 20; // to concentrate at brace wrapping, not line wrap due to column limit
3340
3341 Style.BraceWrapping.BeforeElse = true;
3342 verifyFormat("if (foo) {\n"
3343 " bar();\n"
3344 "}\n"
3345 "else if (baz ||\n"
3346 " quux)\n"
3347 "{\n"
3348 " foobar();\n"
3349 "}\n"
3350 "else {\n"
3351 " barbaz();\n"
3352 "}",
3353 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3354 Style);
3355
3356 Style.BraceWrapping.BeforeCatch = true;
3357 verifyFormat("try {\n"
3358 " foo();\n"
3359 "}\n"
3360 "catch (...) {\n"
3361 " baz();\n"
3362 "}",
3363 "try{foo();}catch(...){baz();}", Style);
3364
3365 Style.BraceWrapping.AfterFunction = true;
3366 Style.BraceWrapping.AfterStruct = false;
3367 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3368 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3369 Style.ColumnLimit = 80;
3370 verifyFormat("void shortfunction() { bar(); }", Style);
3371 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3372 verifyFormat("struct T {};", Style);
3373
3374 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3375 verifyFormat("void shortfunction()\n"
3376 "{\n"
3377 " bar();\n"
3378 "}",
3379 Style);
3380 verifyFormat("struct T shortfunction()\n"
3381 "{\n"
3382 " return bar();\n"
3383 "}",
3384 Style);
3385 verifyFormat("struct T {};", Style);
3386
3387 Style.BraceWrapping.AfterFunction = false;
3388 Style.BraceWrapping.AfterStruct = true;
3389 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3390 verifyFormat("void shortfunction() { bar(); }", Style);
3391 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3392 verifyFormat("struct T\n"
3393 "{\n"
3394 "};",
3395 Style);
3396
3397 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3398 verifyFormat("void shortfunction() {\n"
3399 " bar();\n"
3400 "}",
3401 Style);
3402 verifyFormat("struct T shortfunction() {\n"
3403 " return bar();\n"
3404 "}",
3405 Style);
3406 verifyFormat("struct T\n"
3407 "{\n"
3408 "};",
3409 Style);
3410}
3411
3412TEST_F(FormatTest, BeforeWhile) {
3413 FormatStyle Style = getLLVMStyle();
3414 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3415
3416 verifyFormat("do {\n"
3417 " foo();\n"
3418 "} while (1);",
3419 Style);
3420 Style.BraceWrapping.BeforeWhile = true;
3421 verifyFormat("do {\n"
3422 " foo();\n"
3423 "}\n"
3424 "while (1);",
3425 Style);
3426}
3427
3428//===----------------------------------------------------------------------===//
3429// Tests for classes, namespaces, etc.
3430//===----------------------------------------------------------------------===//
3431
3432TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3433 verifyFormat("class A {};");
3434}
3435
3436TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3437 verifyFormat("class A {\n"
3438 "public:\n"
3439 "public: // comment\n"
3440 "protected:\n"
3441 "private:\n"
3442 " void f() {}\n"
3443 "};");
3444 verifyFormat("export class A {\n"
3445 "public:\n"
3446 "public: // comment\n"
3447 "protected:\n"
3448 "private:\n"
3449 " void f() {}\n"
3450 "};");
3451 verifyGoogleFormat("class A {\n"
3452 " public:\n"
3453 " protected:\n"
3454 " private:\n"
3455 " void f() {}\n"
3456 "};");
3457 verifyGoogleFormat("export class A {\n"
3458 " public:\n"
3459 " protected:\n"
3460 " private:\n"
3461 " void f() {}\n"
3462 "};");
3463 verifyFormat("class A {\n"
3464 "public slots:\n"
3465 " void f1() {}\n"
3466 "public Q_SLOTS:\n"
3467 " void f2() {}\n"
3468 "protected slots:\n"
3469 " void f3() {}\n"
3470 "protected Q_SLOTS:\n"
3471 " void f4() {}\n"
3472 "private slots:\n"
3473 " void f5() {}\n"
3474 "private Q_SLOTS:\n"
3475 " void f6() {}\n"
3476 "signals:\n"
3477 " void g1();\n"
3478 "Q_SIGNALS:\n"
3479 " void g2();\n"
3480 "};");
3481
3482 // Don't interpret 'signals' the wrong way.
3483 verifyFormat("signals.set();");
3484 verifyFormat("for (Signals signals : f()) {\n}");
3485 verifyFormat("{\n"
3486 " signals.set(); // This needs indentation.\n"
3487 "}");
3488 verifyFormat("void f() {\n"
3489 "label:\n"
3490 " signals.baz();\n"
3491 "}");
3492 verifyFormat("private[1];");
3493 verifyFormat("testArray[public] = 1;");
3494 verifyFormat("public();");
3495 verifyFormat("myFunc(public);");
3496 verifyFormat("std::vector<int> testVec = {private};");
3497 verifyFormat("private.p = 1;");
3498 verifyFormat("void function(private...) {};");
3499 verifyFormat("if (private && public)");
3500 verifyFormat("private &= true;");
3501 verifyFormat("int x = private * public;");
3502 verifyFormat("public *= private;");
3503 verifyFormat("int x = public + private;");
3504 verifyFormat("private++;");
3505 verifyFormat("++private;");
3506 verifyFormat("public += private;");
3507 verifyFormat("public = public - private;");
3508 verifyFormat("public->foo();");
3509 verifyFormat("private--;");
3510 verifyFormat("--private;");
3511 verifyFormat("public -= 1;");
3512 verifyFormat("if (!private && !public)");
3513 verifyFormat("public != private;");
3514 verifyFormat("int x = public / private;");
3515 verifyFormat("public /= 2;");
3516 verifyFormat("public = public % 2;");
3517 verifyFormat("public %= 2;");
3518 verifyFormat("if (public < private)");
3519 verifyFormat("public << private;");
3520 verifyFormat("public <<= private;");
3521 verifyFormat("if (public > private)");
3522 verifyFormat("public >> private;");
3523 verifyFormat("public >>= private;");
3524 verifyFormat("public ^ private;");
3525 verifyFormat("public ^= private;");
3526 verifyFormat("public | private;");
3527 verifyFormat("public |= private;");
3528 verifyFormat("auto x = private ? 1 : 2;");
3529 verifyFormat("if (public == private)");
3530 verifyFormat("void foo(public, private)");
3531 verifyFormat("public::foo();");
3532
3533 verifyFormat("class A {\n"
3534 "public:\n"
3535 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3536 "\n"
3537 "private:\n"
3538 " int c;\n"
3539 "};\n"
3540 "class B {\n"
3541 "public:\n"
3542 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3543 "\n"
3544 "private:\n"
3545 " int c;\n"
3546 "};");
3547}
3548
3549TEST_F(FormatTest, SeparatesLogicalBlocks) {
3550 verifyFormat("class A {\n"
3551 "public:\n"
3552 " void f();\n"
3553 "\n"
3554 "private:\n"
3555 " void g() {}\n"
3556 " // test\n"
3557 "protected:\n"
3558 " int h;\n"
3559 "};",
3560 "class A {\n"
3561 "public:\n"
3562 "void f();\n"
3563 "private:\n"
3564 "void g() {}\n"
3565 "// test\n"
3566 "protected:\n"
3567 "int h;\n"
3568 "};");
3569 verifyFormat("class A {\n"
3570 "protected:\n"
3571 "public:\n"
3572 " void f();\n"
3573 "};",
3574 "class A {\n"
3575 "protected:\n"
3576 "\n"
3577 "public:\n"
3578 "\n"
3579 " void f();\n"
3580 "};");
3581
3582 // Even ensure proper spacing inside macros.
3583 verifyFormat("#define B \\\n"
3584 " class A { \\\n"
3585 " protected: \\\n"
3586 " public: \\\n"
3587 " void f(); \\\n"
3588 " };",
3589 "#define B \\\n"
3590 " class A { \\\n"
3591 " protected: \\\n"
3592 " \\\n"
3593 " public: \\\n"
3594 " \\\n"
3595 " void f(); \\\n"
3596 " };",
3597 getGoogleStyle());
3598 // But don't remove empty lines after macros ending in access specifiers.
3599 verifyFormat("#define A private:\n"
3600 "\n"
3601 "int i;",
3602 "#define A private:\n"
3603 "\n"
3604 "int i;");
3605}
3606
3607TEST_F(FormatTest, FormatsClasses) {
3608 verifyFormat("class A : public B {};");
3609 verifyFormat("class A : public ::B {};");
3610
3611 verifyFormat(
3612 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3613 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3614 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3615 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3616 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3617 verifyFormat(
3618 "class A : public B, public C, public D, public E, public F {};");
3619 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3620 " public C,\n"
3621 " public D,\n"
3622 " public E,\n"
3623 " public F,\n"
3624 " public G {};");
3625
3626 verifyFormat("class\n"
3627 " ReallyReallyLongClassName {\n"
3628 " int i;\n"
3629 "};",
3630 getLLVMStyleWithColumns(32));
3631 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3632 " aaaaaaaaaaaaaaaa> {};");
3633 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3634 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3635 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3636 verifyFormat("template <class R, class C>\n"
3637 "struct Aaaaaaaaaaaaaaaaa<R (C:: *)(int) const>\n"
3638 " : Aaaaaaaaaaaaaaaaa<R (C:: *)(int)> {};");
3639 verifyFormat("class ::A::B {};");
3640}
3641
3642TEST_F(FormatTest, BreakInheritanceStyle) {
3643 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3644 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3645 FormatStyle::BILS_BeforeComma;
3646 verifyFormat("class MyClass : public X {};",
3647 StyleWithInheritanceBreakBeforeComma);
3648 verifyFormat("class MyClass\n"
3649 " : public X\n"
3650 " , public Y {};",
3651 StyleWithInheritanceBreakBeforeComma);
3652 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3653 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3654 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3655 StyleWithInheritanceBreakBeforeComma);
3656 verifyFormat("struct aaaaaaaaaaaaa\n"
3657 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3658 " aaaaaaaaaaaaaaaa> {};",
3659 StyleWithInheritanceBreakBeforeComma);
3660
3661 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3662 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3663 FormatStyle::BILS_AfterColon;
3664 verifyFormat("class MyClass : public X {};",
3665 StyleWithInheritanceBreakAfterColon);
3666 verifyFormat("class MyClass : public X, public Y {};",
3667 StyleWithInheritanceBreakAfterColon);
3668 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3669 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3670 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3671 StyleWithInheritanceBreakAfterColon);
3672 verifyFormat("struct aaaaaaaaaaaaa :\n"
3673 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3674 " aaaaaaaaaaaaaaaa> {};",
3675 StyleWithInheritanceBreakAfterColon);
3676
3677 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3678 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3679 FormatStyle::BILS_AfterComma;
3680 verifyFormat("class MyClass : public X {};",
3681 StyleWithInheritanceBreakAfterComma);
3682 verifyFormat("class MyClass : public X,\n"
3683 " public Y {};",
3684 StyleWithInheritanceBreakAfterComma);
3685 verifyFormat(
3686 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3687 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3688 "{};",
3689 StyleWithInheritanceBreakAfterComma);
3690 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3691 " aaaaaaaaaaaaaaaa> {};",
3692 StyleWithInheritanceBreakAfterComma);
3693 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3694 " : public OnceBreak,\n"
3695 " public AlwaysBreak,\n"
3696 " EvenBasesFitInOneLine {};",
3697 StyleWithInheritanceBreakAfterComma);
3698}
3699
3700TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3701 verifyFormat("class A {\n} a, b;");
3702 verifyFormat("struct A {\n} a, b;");
3703 verifyFormat("union A {\n} a, b;");
3704
3705 verifyFormat("constexpr class A {\n} a, b;");
3706 verifyFormat("constexpr struct A {\n} a, b;");
3707 verifyFormat("constexpr union A {\n} a, b;");
3708
3709 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3710 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3711 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3712
3713 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3714 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3715 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3716
3717 verifyFormat("namespace ns {\n"
3718 "class {\n"
3719 "} a, b;\n"
3720 "} // namespace ns");
3721 verifyFormat("namespace ns {\n"
3722 "const class {\n"
3723 "} a, b;\n"
3724 "} // namespace ns");
3725 verifyFormat("namespace ns {\n"
3726 "constexpr class C {\n"
3727 "} a, b;\n"
3728 "} // namespace ns");
3729 verifyFormat("namespace ns {\n"
3730 "class { /* comment */\n"
3731 "} a, b;\n"
3732 "} // namespace ns");
3733 verifyFormat("namespace ns {\n"
3734 "const class { /* comment */\n"
3735 "} a, b;\n"
3736 "} // namespace ns");
3737}
3738
3739TEST_F(FormatTest, FormatsEnum) {
3740 verifyFormat("enum {\n"
3741 " Zero,\n"
3742 " One = 1,\n"
3743 " Two = One + 1,\n"
3744 " Three = (One + Two),\n"
3745 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3746 " Five = (One, Two, Three, Four, 5)\n"
3747 "};");
3748 verifyGoogleFormat("enum {\n"
3749 " Zero,\n"
3750 " One = 1,\n"
3751 " Two = One + 1,\n"
3752 " Three = (One + Two),\n"
3753 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3754 " Five = (One, Two, Three, Four, 5)\n"
3755 "};");
3756 verifyFormat("enum Enum {};");
3757 verifyFormat("enum {};");
3758 verifyFormat("enum X E {} d;");
3759 verifyFormat("enum __attribute__((...)) E {} d;");
3760 verifyFormat("enum __declspec__((...)) E {} d;");
3761 verifyFormat("enum [[nodiscard]] E {} d;");
3762 verifyFormat("enum {\n"
3763 " Bar = Foo<int, int>::value\n"
3764 "};",
3765 getLLVMStyleWithColumns(30));
3766
3767 verifyFormat("enum ShortEnum { A, B, C };");
3768 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3769
3770 verifyFormat("enum KeepEmptyLines {\n"
3771 " ONE,\n"
3772 "\n"
3773 " TWO,\n"
3774 "\n"
3775 " THREE\n"
3776 "}",
3777 "enum KeepEmptyLines {\n"
3778 " ONE,\n"
3779 "\n"
3780 " TWO,\n"
3781 "\n"
3782 "\n"
3783 " THREE\n"
3784 "}");
3785 verifyFormat("enum E { // comment\n"
3786 " ONE,\n"
3787 " TWO\n"
3788 "};\n"
3789 "int i;");
3790
3791 FormatStyle EightIndent = getLLVMStyle();
3792 EightIndent.IndentWidth = 8;
3793 verifyFormat("enum {\n"
3794 " VOID,\n"
3795 " CHAR,\n"
3796 " SHORT,\n"
3797 " INT,\n"
3798 " LONG,\n"
3799 " SIGNED,\n"
3800 " UNSIGNED,\n"
3801 " BOOL,\n"
3802 " FLOAT,\n"
3803 " DOUBLE,\n"
3804 " COMPLEX\n"
3805 "};",
3806 EightIndent);
3807
3808 verifyFormat("enum [[nodiscard]] E {\n"
3809 " ONE,\n"
3810 " TWO,\n"
3811 "};");
3812 verifyFormat("enum [[nodiscard]] E {\n"
3813 " // Comment 1\n"
3814 " ONE,\n"
3815 " // Comment 2\n"
3816 " TWO,\n"
3817 "};");
3818 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3819 " // Comment 1\n"
3820 " ONE,\n"
3821 " // Comment 2\n"
3822 " TWO\n"
3823 "};");
3824 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3825 " // Comment 1\n"
3826 " ONE,\n"
3827 " // Comment 2\n"
3828 " TWO\n"
3829 "};");
3830 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3831 " A,\n"
3832 " // bar\n"
3833 " B\n"
3834 "};",
3835 "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3836 " A,\n"
3837 " // bar\n"
3838 " B};");
3839
3840 // Not enums.
3841 verifyFormat("enum X f() {\n"
3842 " a();\n"
3843 " return 42;\n"
3844 "}");
3845 verifyFormat("enum X Type::f() {\n"
3846 " a();\n"
3847 " return 42;\n"
3848 "}");
3849 verifyFormat("enum ::X f() {\n"
3850 " a();\n"
3851 " return 42;\n"
3852 "}");
3853 verifyFormat("enum ns::X f() {\n"
3854 " a();\n"
3855 " return 42;\n"
3856 "}");
3857}
3858
3859TEST_F(FormatTest, FormatsEnumsWithErrors) {
3860 verifyFormat("enum Type {\n"
3861 " One = 0; // These semicolons should be commas.\n"
3862 " Two = 1;\n"
3863 "};");
3864 verifyFormat("namespace n {\n"
3865 "enum Type {\n"
3866 " One,\n"
3867 " Two, // missing };\n"
3868 " int i;\n"
3869 "}\n"
3870 "void g() {}");
3871}
3872
3873TEST_F(FormatTest, FormatsEnumStruct) {
3874 verifyFormat("enum struct {\n"
3875 " Zero,\n"
3876 " One = 1,\n"
3877 " Two = One + 1,\n"
3878 " Three = (One + Two),\n"
3879 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3880 " Five = (One, Two, Three, Four, 5)\n"
3881 "};");
3882 verifyFormat("enum struct Enum {};");
3883 verifyFormat("enum struct {};");
3884 verifyFormat("enum struct X E {} d;");
3885 verifyFormat("enum struct __attribute__((...)) E {} d;");
3886 verifyFormat("enum struct __declspec__((...)) E {} d;");
3887 verifyFormat("enum struct [[nodiscard]] E {} d;");
3888 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3889
3890 verifyFormat("enum struct [[nodiscard]] E {\n"
3891 " ONE,\n"
3892 " TWO,\n"
3893 "};");
3894 verifyFormat("enum struct [[nodiscard]] E {\n"
3895 " // Comment 1\n"
3896 " ONE,\n"
3897 " // Comment 2\n"
3898 " TWO,\n"
3899 "};");
3900}
3901
3902TEST_F(FormatTest, FormatsEnumClass) {
3903 verifyFormat("enum class {\n"
3904 " Zero,\n"
3905 " One = 1,\n"
3906 " Two = One + 1,\n"
3907 " Three = (One + Two),\n"
3908 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3909 " Five = (One, Two, Three, Four, 5)\n"
3910 "};");
3911 verifyFormat("enum class Enum {};");
3912 verifyFormat("enum class {};");
3913 verifyFormat("enum class X E {} d;");
3914 verifyFormat("enum class __attribute__((...)) E {} d;");
3915 verifyFormat("enum class __declspec__((...)) E {} d;");
3916 verifyFormat("enum class [[nodiscard]] E {} d;");
3917 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3918
3919 verifyFormat("enum class [[nodiscard]] E {\n"
3920 " ONE,\n"
3921 " TWO,\n"
3922 "};");
3923 verifyFormat("enum class [[nodiscard]] E {\n"
3924 " // Comment 1\n"
3925 " ONE,\n"
3926 " // Comment 2\n"
3927 " TWO,\n"
3928 "};");
3929}
3930
3931TEST_F(FormatTest, FormatsEnumTypes) {
3932 verifyFormat("enum X : int {\n"
3933 " A, // Force multiple lines.\n"
3934 " B\n"
3935 "};");
3936 verifyFormat("enum X : int { A, B };");
3937 verifyFormat("enum X : std::uint32_t { A, B };");
3938}
3939
3940TEST_F(FormatTest, FormatsTypedefEnum) {
3941 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
3942 verifyFormat("typedef enum {} EmptyEnum;");
3943 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3944 verifyFormat("typedef enum {\n"
3945 " ZERO = 0,\n"
3946 " ONE = 1,\n"
3947 " TWO = 2,\n"
3948 " THREE = 3\n"
3949 "} LongEnum;",
3950 Style);
3951 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3952 Style.BraceWrapping.AfterEnum = true;
3953 verifyFormat("typedef enum {} EmptyEnum;");
3954 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3955 verifyFormat("typedef enum\n"
3956 "{\n"
3957 " ZERO = 0,\n"
3958 " ONE = 1,\n"
3959 " TWO = 2,\n"
3960 " THREE = 3\n"
3961 "} LongEnum;",
3962 Style);
3963}
3964
3965TEST_F(FormatTest, FormatsNSEnums) {
3966 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3967 verifyGoogleFormat(
3968 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3969 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3970 " // Information about someDecentlyLongValue.\n"
3971 " someDecentlyLongValue,\n"
3972 " // Information about anotherDecentlyLongValue.\n"
3973 " anotherDecentlyLongValue,\n"
3974 " // Information about aThirdDecentlyLongValue.\n"
3975 " aThirdDecentlyLongValue\n"
3976 "};");
3977 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3978 " // Information about someDecentlyLongValue.\n"
3979 " someDecentlyLongValue,\n"
3980 " // Information about anotherDecentlyLongValue.\n"
3981 " anotherDecentlyLongValue,\n"
3982 " // Information about aThirdDecentlyLongValue.\n"
3983 " aThirdDecentlyLongValue\n"
3984 "};");
3985 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3986 " a = 1,\n"
3987 " b = 2,\n"
3988 " c = 3,\n"
3989 "};");
3990 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3991 " a = 1,\n"
3992 " b = 2,\n"
3993 " c = 3,\n"
3994 "};");
3995 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3996 " a = 1,\n"
3997 " b = 2,\n"
3998 " c = 3,\n"
3999 "};");
4000 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4001 " a = 1,\n"
4002 " b = 2,\n"
4003 " c = 3,\n"
4004 "};");
4005}
4006
4007TEST_F(FormatTest, FormatsBitfields) {
4008 verifyFormat("struct Bitfields {\n"
4009 " unsigned sClass : 8;\n"
4010 " unsigned ValueKind : 2;\n"
4011 "};");
4012 verifyFormat("struct A {\n"
4013 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4014 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4015 "};");
4016 verifyFormat("struct MyStruct {\n"
4017 " uchar data;\n"
4018 " uchar : 8;\n"
4019 " uchar : 8;\n"
4020 " uchar other;\n"
4021 "};");
4022 FormatStyle Style = getLLVMStyle();
4023 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
4024 verifyFormat("struct Bitfields {\n"
4025 " unsigned sClass:8;\n"
4026 " unsigned ValueKind:2;\n"
4027 " uchar other;\n"
4028 "};",
4029 Style);
4030 verifyFormat("struct A {\n"
4031 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4032 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4033 "};",
4034 Style);
4035 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
4036 verifyFormat("struct Bitfields {\n"
4037 " unsigned sClass :8;\n"
4038 " unsigned ValueKind :2;\n"
4039 " uchar other;\n"
4040 "};",
4041 Style);
4042 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
4043 verifyFormat("struct Bitfields {\n"
4044 " unsigned sClass: 8;\n"
4045 " unsigned ValueKind: 2;\n"
4046 " uchar other;\n"
4047 "};",
4048 Style);
4049}
4050
4051TEST_F(FormatTest, FormatsNamespaces) {
4052 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
4053 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
4054
4055 verifyFormat("namespace some_namespace {\n"
4056 "class A {};\n"
4057 "void f() { f(); }\n"
4058 "}",
4059 LLVMWithNoNamespaceFix);
4060 verifyFormat("#define M(x) x##x\n"
4061 "namespace M(x) {\n"
4062 "class A {};\n"
4063 "void f() { f(); }\n"
4064 "}",
4065 LLVMWithNoNamespaceFix);
4066 verifyFormat("#define M(x) x##x\n"
4067 "namespace N::inline M(x) {\n"
4068 "class A {};\n"
4069 "void f() { f(); }\n"
4070 "}",
4071 LLVMWithNoNamespaceFix);
4072 verifyFormat("#define M(x) x##x\n"
4073 "namespace M(x)::inline N {\n"
4074 "class A {};\n"
4075 "void f() { f(); }\n"
4076 "}",
4077 LLVMWithNoNamespaceFix);
4078 verifyFormat("#define M(x) x##x\n"
4079 "namespace N::M(x) {\n"
4080 "class A {};\n"
4081 "void f() { f(); }\n"
4082 "}",
4083 LLVMWithNoNamespaceFix);
4084 verifyFormat("#define M(x) x##x\n"
4085 "namespace M::N(x) {\n"
4086 "class A {};\n"
4087 "void f() { f(); }\n"
4088 "}",
4089 LLVMWithNoNamespaceFix);
4090 verifyFormat("namespace N::inline D {\n"
4091 "class A {};\n"
4092 "void f() { f(); }\n"
4093 "}",
4094 LLVMWithNoNamespaceFix);
4095 verifyFormat("namespace N::inline D::E {\n"
4096 "class A {};\n"
4097 "void f() { f(); }\n"
4098 "}",
4099 LLVMWithNoNamespaceFix);
4100 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4101 "class A {};\n"
4102 "void f() { f(); }\n"
4103 "}",
4104 LLVMWithNoNamespaceFix);
4105 verifyFormat("/* something */ namespace some_namespace {\n"
4106 "class A {};\n"
4107 "void f() { f(); }\n"
4108 "}",
4109 LLVMWithNoNamespaceFix);
4110 verifyFormat("namespace {\n"
4111 "class A {};\n"
4112 "void f() { f(); }\n"
4113 "}",
4114 LLVMWithNoNamespaceFix);
4115 verifyFormat("/* something */ namespace {\n"
4116 "class A {};\n"
4117 "void f() { f(); }\n"
4118 "}",
4119 LLVMWithNoNamespaceFix);
4120 verifyFormat("inline namespace X {\n"
4121 "class A {};\n"
4122 "void f() { f(); }\n"
4123 "}",
4124 LLVMWithNoNamespaceFix);
4125 verifyFormat("/* something */ inline namespace X {\n"
4126 "class A {};\n"
4127 "void f() { f(); }\n"
4128 "}",
4129 LLVMWithNoNamespaceFix);
4130 verifyFormat("export namespace X {\n"
4131 "class A {};\n"
4132 "void f() { f(); }\n"
4133 "}",
4134 LLVMWithNoNamespaceFix);
4135 verifyFormat("using namespace some_namespace;\n"
4136 "class A {};\n"
4137 "void f() { f(); }",
4138 LLVMWithNoNamespaceFix);
4139
4140 // This code is more common than we thought; if we
4141 // layout this correctly the semicolon will go into
4142 // its own line, which is undesirable.
4143 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4144 verifyFormat("namespace {\n"
4145 "class A {};\n"
4146 "};",
4147 LLVMWithNoNamespaceFix);
4148
4149 verifyFormat("namespace {\n"
4150 "int SomeVariable = 0; // comment\n"
4151 "} // namespace",
4152 LLVMWithNoNamespaceFix);
4153 verifyFormat("#ifndef HEADER_GUARD\n"
4154 "#define HEADER_GUARD\n"
4155 "namespace my_namespace {\n"
4156 "int i;\n"
4157 "} // my_namespace\n"
4158 "#endif // HEADER_GUARD",
4159 "#ifndef HEADER_GUARD\n"
4160 " #define HEADER_GUARD\n"
4161 " namespace my_namespace {\n"
4162 "int i;\n"
4163 "} // my_namespace\n"
4164 "#endif // HEADER_GUARD",
4165 LLVMWithNoNamespaceFix);
4166
4167 verifyFormat("namespace A::B {\n"
4168 "class C {};\n"
4169 "}",
4170 LLVMWithNoNamespaceFix);
4171
4172 FormatStyle Style = getLLVMStyle();
4173 Style.NamespaceIndentation = FormatStyle::NI_All;
4174 verifyFormat("namespace out {\n"
4175 " int i;\n"
4176 " namespace in {\n"
4177 " int i;\n"
4178 " } // namespace in\n"
4179 "} // namespace out",
4180 "namespace out {\n"
4181 "int i;\n"
4182 "namespace in {\n"
4183 "int i;\n"
4184 "} // namespace in\n"
4185 "} // namespace out",
4186 Style);
4187
4188 FormatStyle ShortInlineFunctions = getLLVMStyle();
4189 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4190 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4191 FormatStyle::SFS_Inline;
4192 verifyFormat("namespace {\n"
4193 " void f() {\n"
4194 " return;\n"
4195 " }\n"
4196 "} // namespace",
4197 ShortInlineFunctions);
4198 verifyFormat("namespace { /* comment */\n"
4199 " void f() {\n"
4200 " return;\n"
4201 " }\n"
4202 "} // namespace",
4203 ShortInlineFunctions);
4204 verifyFormat("namespace { // comment\n"
4205 " void f() {\n"
4206 " return;\n"
4207 " }\n"
4208 "} // namespace",
4209 ShortInlineFunctions);
4210 verifyFormat("namespace {\n"
4211 " int some_int;\n"
4212 " void f() {\n"
4213 " return;\n"
4214 " }\n"
4215 "} // namespace",
4216 ShortInlineFunctions);
4217 verifyFormat("namespace interface {\n"
4218 " void f() {\n"
4219 " return;\n"
4220 " }\n"
4221 "} // namespace interface",
4222 ShortInlineFunctions);
4223 verifyFormat("namespace {\n"
4224 " class X {\n"
4225 " void f() { return; }\n"
4226 " };\n"
4227 "} // namespace",
4228 ShortInlineFunctions);
4229 verifyFormat("namespace {\n"
4230 " class X { /* comment */\n"
4231 " void f() { return; }\n"
4232 " };\n"
4233 "} // namespace",
4234 ShortInlineFunctions);
4235 verifyFormat("namespace {\n"
4236 " class X { // comment\n"
4237 " void f() { return; }\n"
4238 " };\n"
4239 "} // namespace",
4240 ShortInlineFunctions);
4241 verifyFormat("namespace {\n"
4242 " struct X {\n"
4243 " void f() { return; }\n"
4244 " };\n"
4245 "} // namespace",
4246 ShortInlineFunctions);
4247 verifyFormat("namespace {\n"
4248 " union X {\n"
4249 " void f() { return; }\n"
4250 " };\n"
4251 "} // namespace",
4252 ShortInlineFunctions);
4253 verifyFormat("extern \"C\" {\n"
4254 "void f() {\n"
4255 " return;\n"
4256 "}\n"
4257 "} // namespace",
4258 ShortInlineFunctions);
4259 verifyFormat("namespace {\n"
4260 " class X {\n"
4261 " void f() { return; }\n"
4262 " } x;\n"
4263 "} // namespace",
4264 ShortInlineFunctions);
4265 verifyFormat("namespace {\n"
4266 " [[nodiscard]] class X {\n"
4267 " void f() { return; }\n"
4268 " };\n"
4269 "} // namespace",
4270 ShortInlineFunctions);
4271 verifyFormat("namespace {\n"
4272 " static class X {\n"
4273 " void f() { return; }\n"
4274 " } x;\n"
4275 "} // namespace",
4276 ShortInlineFunctions);
4277 verifyFormat("namespace {\n"
4278 " constexpr class X {\n"
4279 " void f() { return; }\n"
4280 " } x;\n"
4281 "} // namespace",
4282 ShortInlineFunctions);
4283
4284 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4285 verifyFormat("extern \"C\" {\n"
4286 " void f() {\n"
4287 " return;\n"
4288 " }\n"
4289 "} // namespace",
4290 ShortInlineFunctions);
4291
4292 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4293 verifyFormat("namespace out {\n"
4294 "int i;\n"
4295 "namespace in {\n"
4296 " int i;\n"
4297 "} // namespace in\n"
4298 "} // namespace out",
4299 "namespace out {\n"
4300 "int i;\n"
4301 "namespace in {\n"
4302 "int i;\n"
4303 "} // namespace in\n"
4304 "} // namespace out",
4305 Style);
4306
4307 Style.NamespaceIndentation = FormatStyle::NI_None;
4308 verifyFormat("template <class T>\n"
4309 "concept a_concept = X<>;\n"
4310 "namespace B {\n"
4311 "struct b_struct {};\n"
4312 "} // namespace B",
4313 Style);
4314 verifyFormat("template <int I>\n"
4315 "constexpr void foo()\n"
4316 " requires(I == 42)\n"
4317 "{}\n"
4318 "namespace ns {\n"
4319 "void foo() {}\n"
4320 "} // namespace ns",
4321 Style);
4322
4323 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4324 LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4325 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4326 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4327 "// block for debug mode\n"
4328 "#ifndef NDEBUG\n"
4329 "#endif\n"
4330 "}}} // namespace ns1::ns2::ns3",
4331 LLVMWithCompactInnerNamespace);
4332}
4333
4334TEST_F(FormatTest, NamespaceMacros) {
4335 FormatStyle Style = getLLVMStyle();
4336 Style.NamespaceMacros.push_back(x: "TESTSUITE");
4337
4338 verifyFormat("TESTSUITE(A) {\n"
4339 "int foo();\n"
4340 "} // TESTSUITE(A)",
4341 Style);
4342
4343 verifyFormat("TESTSUITE(A, B) {\n"
4344 "int foo();\n"
4345 "} // TESTSUITE(A)",
4346 Style);
4347
4348 // Properly indent according to NamespaceIndentation style
4349 Style.NamespaceIndentation = FormatStyle::NI_All;
4350 verifyFormat("TESTSUITE(A) {\n"
4351 " int foo();\n"
4352 "} // TESTSUITE(A)",
4353 Style);
4354 verifyFormat("TESTSUITE(A) {\n"
4355 " namespace B {\n"
4356 " int foo();\n"
4357 " } // namespace B\n"
4358 "} // TESTSUITE(A)",
4359 Style);
4360 verifyFormat("namespace A {\n"
4361 " TESTSUITE(B) {\n"
4362 " int foo();\n"
4363 " } // TESTSUITE(B)\n"
4364 "} // namespace A",
4365 Style);
4366
4367 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4368 verifyFormat("TESTSUITE(A) {\n"
4369 "TESTSUITE(B) {\n"
4370 " int foo();\n"
4371 "} // TESTSUITE(B)\n"
4372 "} // TESTSUITE(A)",
4373 Style);
4374 verifyFormat("TESTSUITE(A) {\n"
4375 "namespace B {\n"
4376 " int foo();\n"
4377 "} // namespace B\n"
4378 "} // TESTSUITE(A)",
4379 Style);
4380 verifyFormat("namespace A {\n"
4381 "TESTSUITE(B) {\n"
4382 " int foo();\n"
4383 "} // TESTSUITE(B)\n"
4384 "} // namespace A",
4385 Style);
4386
4387 // Properly merge namespace-macros blocks in CompactNamespaces mode
4388 Style.NamespaceIndentation = FormatStyle::NI_None;
4389 Style.CompactNamespaces = true;
4390 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4391 "}} // TESTSUITE(A::B)",
4392 Style);
4393
4394 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4395 "}} // TESTSUITE(out::in)",
4396 "TESTSUITE(out) {\n"
4397 "TESTSUITE(in) {\n"
4398 "} // TESTSUITE(in)\n"
4399 "} // TESTSUITE(out)",
4400 Style);
4401
4402 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4403 "}} // TESTSUITE(out::in)",
4404 "TESTSUITE(out) {\n"
4405 "TESTSUITE(in) {\n"
4406 "} // TESTSUITE(in)\n"
4407 "} // TESTSUITE(out)",
4408 Style);
4409
4410 // Do not merge different namespaces/macros
4411 verifyFormat("namespace out {\n"
4412 "TESTSUITE(in) {\n"
4413 "} // TESTSUITE(in)\n"
4414 "} // namespace out",
4415 Style);
4416 verifyFormat("TESTSUITE(out) {\n"
4417 "namespace in {\n"
4418 "} // namespace in\n"
4419 "} // TESTSUITE(out)",
4420 Style);
4421 Style.NamespaceMacros.push_back(x: "FOOBAR");
4422 verifyFormat("TESTSUITE(out) {\n"
4423 "FOOBAR(in) {\n"
4424 "} // FOOBAR(in)\n"
4425 "} // TESTSUITE(out)",
4426 Style);
4427}
4428
4429TEST_F(FormatTest, FormatsCompactNamespaces) {
4430 FormatStyle Style = getLLVMStyle();
4431 Style.CompactNamespaces = true;
4432 Style.NamespaceMacros.push_back(x: "TESTSUITE");
4433
4434 verifyFormat("namespace A { namespace B {\n"
4435 "}} // namespace A::B",
4436 Style);
4437
4438 verifyFormat("namespace out { namespace in {\n"
4439 "}} // namespace out::in",
4440 "namespace out {\n"
4441 "namespace in {\n"
4442 "} // namespace in\n"
4443 "} // namespace out",
4444 Style);
4445
4446 // Only namespaces which have both consecutive opening and end get compacted
4447 verifyFormat("namespace out {\n"
4448 "namespace in1 {\n"
4449 "} // namespace in1\n"
4450 "namespace in2 {\n"
4451 "} // namespace in2\n"
4452 "} // namespace out",
4453 Style);
4454
4455 verifyFormat("namespace out {\n"
4456 "int i;\n"
4457 "namespace in {\n"
4458 "int j;\n"
4459 "} // namespace in\n"
4460 "int k;\n"
4461 "} // namespace out",
4462 "namespace out { int i;\n"
4463 "namespace in { int j; } // namespace in\n"
4464 "int k; } // namespace out",
4465 Style);
4466
4467 verifyFormat("namespace A { namespace B { namespace C {\n"
4468 "}}} // namespace A::B::C",
4469 "namespace A { namespace B {\n"
4470 "namespace C {\n"
4471 "}} // namespace B::C\n"
4472 "} // namespace A",
4473 Style);
4474
4475 Style.ColumnLimit = 40;
4476 verifyFormat("namespace aaaaaaaaaa {\n"
4477 "namespace bbbbbbbbbb {\n"
4478 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4479 "namespace aaaaaaaaaa {\n"
4480 "namespace bbbbbbbbbb {\n"
4481 "} // namespace bbbbbbbbbb\n"
4482 "} // namespace aaaaaaaaaa",
4483 Style);
4484
4485 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4486 "namespace cccccc {\n"
4487 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4488 "namespace aaaaaa {\n"
4489 "namespace bbbbbb {\n"
4490 "namespace cccccc {\n"
4491 "} // namespace cccccc\n"
4492 "} // namespace bbbbbb\n"
4493 "} // namespace aaaaaa",
4494 Style);
4495 Style.ColumnLimit = 80;
4496
4497 // Extra semicolon after 'inner' closing brace prevents merging
4498 verifyFormat("namespace out { namespace in {\n"
4499 "}; } // namespace out::in",
4500 "namespace out {\n"
4501 "namespace in {\n"
4502 "}; // namespace in\n"
4503 "} // namespace out",
4504 Style);
4505
4506 // Extra semicolon after 'outer' closing brace is conserved
4507 verifyFormat("namespace out { namespace in {\n"
4508 "}}; // namespace out::in",
4509 "namespace out {\n"
4510 "namespace in {\n"
4511 "} // namespace in\n"
4512 "}; // namespace out",
4513 Style);
4514
4515 Style.NamespaceIndentation = FormatStyle::NI_All;
4516 verifyFormat("namespace out { namespace in {\n"
4517 " int i;\n"
4518 "}} // namespace out::in",
4519 "namespace out {\n"
4520 "namespace in {\n"
4521 "int i;\n"
4522 "} // namespace in\n"
4523 "} // namespace out",
4524 Style);
4525 verifyFormat("namespace out { namespace mid {\n"
4526 " namespace in {\n"
4527 " int j;\n"
4528 " } // namespace in\n"
4529 " int k;\n"
4530 "}} // namespace out::mid",
4531 "namespace out { namespace mid {\n"
4532 "namespace in { int j; } // namespace in\n"
4533 "int k; }} // namespace out::mid",
4534 Style);
4535
4536 verifyFormat("namespace A { namespace B { namespace C {\n"
4537 " int i;\n"
4538 "}}} // namespace A::B::C\n"
4539 "int main() {\n"
4540 " if (true)\n"
4541 " return 0;\n"
4542 "}",
4543 "namespace A { namespace B {\n"
4544 "namespace C {\n"
4545 " int i;\n"
4546 "}} // namespace B::C\n"
4547 "} // namespace A\n"
4548 "int main() {\n"
4549 " if (true)\n"
4550 " return 0;\n"
4551 "}",
4552 Style);
4553
4554 verifyFormat("namespace A { namespace B { namespace C {\n"
4555 "#ifdef FOO\n"
4556 " int i;\n"
4557 "#endif\n"
4558 "}}} // namespace A::B::C\n"
4559 "int main() {\n"
4560 " if (true)\n"
4561 " return 0;\n"
4562 "}",
4563 "namespace A { namespace B {\n"
4564 "namespace C {\n"
4565 "#ifdef FOO\n"
4566 " int i;\n"
4567 "#endif\n"
4568 "}} // namespace B::C\n"
4569 "} // namespace A\n"
4570 "int main() {\n"
4571 " if (true)\n"
4572 " return 0;\n"
4573 "}",
4574 Style);
4575
4576 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4577 verifyFormat("namespace out { namespace in {\n"
4578 " int i;\n"
4579 "}} // namespace out::in",
4580 "namespace out {\n"
4581 "namespace in {\n"
4582 "int i;\n"
4583 "} // namespace in\n"
4584 "} // namespace out",
4585 Style);
4586 verifyFormat("namespace out { namespace mid { namespace in {\n"
4587 " int i;\n"
4588 "}}} // namespace out::mid::in",
4589 "namespace out {\n"
4590 "namespace mid {\n"
4591 "namespace in {\n"
4592 "int i;\n"
4593 "} // namespace in\n"
4594 "} // namespace mid\n"
4595 "} // namespace out",
4596 Style);
4597
4598 Style.CompactNamespaces = true;
4599 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4600 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4601 Style.BraceWrapping.BeforeLambdaBody = true;
4602 verifyFormat("namespace out { namespace in {\n"
4603 "}} // namespace out::in",
4604 Style);
4605 verifyFormat("namespace out { namespace in {\n"
4606 "}} // namespace out::in",
4607 "namespace out {\n"
4608 "namespace in {\n"
4609 "} // namespace in\n"
4610 "} // namespace out",
4611 Style);
4612}
4613
4614TEST_F(FormatTest, FormatsExternC) {
4615 verifyFormat("extern \"C\" {\nint a;");
4616 verifyFormat("extern \"C\" {}");
4617 verifyFormat("extern \"C\" {\n"
4618 "int foo();\n"
4619 "}");
4620 verifyFormat("extern \"C\" int foo() {}");
4621 verifyFormat("extern \"C\" int foo();");
4622 verifyFormat("extern \"C\" int foo() {\n"
4623 " int i = 42;\n"
4624 " return i;\n"
4625 "}");
4626
4627 FormatStyle Style = getLLVMStyle();
4628 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4629 Style.BraceWrapping.AfterFunction = true;
4630 verifyFormat("extern \"C\" int foo() {}", Style);
4631 verifyFormat("extern \"C\" int foo();", Style);
4632 verifyFormat("extern \"C\" int foo()\n"
4633 "{\n"
4634 " int i = 42;\n"
4635 " return i;\n"
4636 "}",
4637 Style);
4638
4639 Style.BraceWrapping.AfterExternBlock = true;
4640 Style.BraceWrapping.SplitEmptyRecord = false;
4641 verifyFormat("extern \"C\"\n"
4642 "{}",
4643 Style);
4644 verifyFormat("extern \"C\"\n"
4645 "{\n"
4646 " int foo();\n"
4647 "}",
4648 Style);
4649}
4650
4651TEST_F(FormatTest, IndentExternBlockStyle) {
4652 FormatStyle Style = getLLVMStyle();
4653 Style.IndentWidth = 2;
4654
4655 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4656 verifyFormat("extern \"C\" { /*9*/\n"
4657 "}",
4658 Style);
4659 verifyFormat("extern \"C\" {\n"
4660 " int foo10();\n"
4661 "}",
4662 Style);
4663
4664 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4665 verifyFormat("extern \"C\" { /*11*/\n"
4666 "}",
4667 Style);
4668 verifyFormat("extern \"C\" {\n"
4669 "int foo12();\n"
4670 "}",
4671 Style);
4672
4673 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4674 verifyFormat("extern \"C\"\n"
4675 "{\n"
4676 "int i;\n"
4677 "}",
4678 Style);
4679
4680 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4681 Style.BraceWrapping.AfterExternBlock = true;
4682 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4683 verifyFormat("extern \"C\"\n"
4684 "{ /*13*/\n"
4685 "}",
4686 Style);
4687 verifyFormat("extern \"C\"\n{\n"
4688 " int foo14();\n"
4689 "}",
4690 Style);
4691
4692 Style.BraceWrapping.AfterExternBlock = false;
4693 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4694 verifyFormat("extern \"C\" { /*15*/\n"
4695 "}",
4696 Style);
4697 verifyFormat("extern \"C\" {\n"
4698 "int foo16();\n"
4699 "}",
4700 Style);
4701
4702 Style.BraceWrapping.AfterExternBlock = true;
4703 verifyFormat("extern \"C\"\n"
4704 "{ /*13*/\n"
4705 "}",
4706 Style);
4707 verifyFormat("extern \"C\"\n"
4708 "{\n"
4709 "int foo14();\n"
4710 "}",
4711 Style);
4712
4713 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4714 verifyFormat("extern \"C\"\n"
4715 "{ /*13*/\n"
4716 "}",
4717 Style);
4718 verifyFormat("extern \"C\"\n"
4719 "{\n"
4720 " int foo14();\n"
4721 "}",
4722 Style);
4723}
4724
4725TEST_F(FormatTest, FormatsInlineASM) {
4726 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4727 verifyFormat("asm(\"nop\" ::: \"memory\");");
4728 verifyFormat(
4729 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4730 " \"cpuid\\n\\t\"\n"
4731 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4732 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4733 " : \"a\"(value));");
4734 verifyFormat(
4735 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4736 " __asm {\n"
4737 " mov edx,[that] // vtable in edx\n"
4738 " mov eax,methodIndex\n"
4739 " call [edx][eax*4] // stdcall\n"
4740 " }\n"
4741 "}",
4742 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4743 " __asm {\n"
4744 " mov edx,[that] // vtable in edx\n"
4745 " mov eax,methodIndex\n"
4746 " call [edx][eax*4] // stdcall\n"
4747 " }\n"
4748 "}");
4749 verifyNoChange("_asm {\n"
4750 " xor eax, eax;\n"
4751 " cpuid;\n"
4752 "}");
4753 verifyFormat("void function() {\n"
4754 " // comment\n"
4755 " asm(\"\");\n"
4756 "}");
4757 verifyFormat("__asm {\n"
4758 "}\n"
4759 "int i;",
4760 "__asm {\n"
4761 "}\n"
4762 "int i;");
4763
4764 auto Style = getLLVMStyleWithColumns(ColumnLimit: 0);
4765 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4766 const StringRef Code2{"asm(\"xyz\"\n"
4767 " : \"=a\"(a), \"=d\"(b)\n"
4768 " : \"a\"(data));"};
4769 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4770 " : \"a\"(data));"};
4771
4772 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4773 verifyFormat(Code1, Style);
4774 verifyNoChange(Code2, Style);
4775 verifyNoChange(Code3, Style);
4776
4777 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4778 verifyFormat(Code2, Code1, Style);
4779 verifyNoChange(Code2, Style);
4780 verifyFormat(Code2, Code3, Style);
4781}
4782
4783TEST_F(FormatTest, FormatTryCatch) {
4784 verifyFormat("try {\n"
4785 " throw a * b;\n"
4786 "} catch (int a) {\n"
4787 " // Do nothing.\n"
4788 "} catch (...) {\n"
4789 " exit(42);\n"
4790 "}");
4791
4792 // Function-level try statements.
4793 verifyFormat("int f() try { return 4; } catch (...) {\n"
4794 " return 5;\n"
4795 "}");
4796 verifyFormat("class A {\n"
4797 " int a;\n"
4798 " A() try : a(0) {\n"
4799 " } catch (...) {\n"
4800 " throw;\n"
4801 " }\n"
4802 "};");
4803 verifyFormat("class A {\n"
4804 " int a;\n"
4805 " A() try : a(0), b{1} {\n"
4806 " } catch (...) {\n"
4807 " throw;\n"
4808 " }\n"
4809 "};");
4810 verifyFormat("class A {\n"
4811 " int a;\n"
4812 " A() try : a(0), b{1}, c{2} {\n"
4813 " } catch (...) {\n"
4814 " throw;\n"
4815 " }\n"
4816 "};");
4817 verifyFormat("class A {\n"
4818 " int a;\n"
4819 " A() try : a(0), b{1}, c{2} {\n"
4820 " { // New scope.\n"
4821 " }\n"
4822 " } catch (...) {\n"
4823 " throw;\n"
4824 " }\n"
4825 "};");
4826
4827 // Incomplete try-catch blocks.
4828 verifyIncompleteFormat("try {} catch (");
4829}
4830
4831TEST_F(FormatTest, FormatTryAsAVariable) {
4832 verifyFormat("int try;");
4833 verifyFormat("int try, size;");
4834 verifyFormat("try = foo();");
4835 verifyFormat("if (try < size) {\n return true;\n}");
4836
4837 verifyFormat("int catch;");
4838 verifyFormat("int catch, size;");
4839 verifyFormat("catch = foo();");
4840 verifyFormat("if (catch < size) {\n return true;\n}");
4841
4842 FormatStyle Style = getLLVMStyle();
4843 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4844 Style.BraceWrapping.AfterFunction = true;
4845 Style.BraceWrapping.BeforeCatch = true;
4846 verifyFormat("try {\n"
4847 " int bar = 1;\n"
4848 "}\n"
4849 "catch (...) {\n"
4850 " int bar = 1;\n"
4851 "}",
4852 Style);
4853 verifyFormat("#if NO_EX\n"
4854 "try\n"
4855 "#endif\n"
4856 "{\n"
4857 "}\n"
4858 "#if NO_EX\n"
4859 "catch (...) {\n"
4860 "}",
4861 Style);
4862 verifyFormat("try /* abc */ {\n"
4863 " int bar = 1;\n"
4864 "}\n"
4865 "catch (...) {\n"
4866 " int bar = 1;\n"
4867 "}",
4868 Style);
4869 verifyFormat("try\n"
4870 "// abc\n"
4871 "{\n"
4872 " int bar = 1;\n"
4873 "}\n"
4874 "catch (...) {\n"
4875 " int bar = 1;\n"
4876 "}",
4877 Style);
4878}
4879
4880TEST_F(FormatTest, FormatSEHTryCatch) {
4881 verifyFormat("__try {\n"
4882 " int a = b * c;\n"
4883 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4884 " // Do nothing.\n"
4885 "}");
4886
4887 verifyFormat("__try {\n"
4888 " int a = b * c;\n"
4889 "} __finally {\n"
4890 " // Do nothing.\n"
4891 "}");
4892
4893 verifyFormat("DEBUG({\n"
4894 " __try {\n"
4895 " } __finally {\n"
4896 " }\n"
4897 "});");
4898}
4899
4900TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4901 verifyFormat("try {\n"
4902 " f();\n"
4903 "} catch {\n"
4904 " g();\n"
4905 "}");
4906 verifyFormat("try {\n"
4907 " f();\n"
4908 "} catch (A a) MACRO(x) {\n"
4909 " g();\n"
4910 "} catch (B b) MACRO(x) {\n"
4911 " g();\n"
4912 "}");
4913}
4914
4915TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4916 FormatStyle Style = getLLVMStyle();
4917 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4918 FormatStyle::BS_WebKit}) {
4919 Style.BreakBeforeBraces = BraceStyle;
4920 verifyFormat("try {\n"
4921 " // something\n"
4922 "} catch (...) {\n"
4923 " // something\n"
4924 "}",
4925 Style);
4926 }
4927 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4928 verifyFormat("try {\n"
4929 " // something\n"
4930 "}\n"
4931 "catch (...) {\n"
4932 " // something\n"
4933 "}",
4934 Style);
4935 verifyFormat("__try {\n"
4936 " // something\n"
4937 "}\n"
4938 "__finally {\n"
4939 " // something\n"
4940 "}",
4941 Style);
4942 verifyFormat("@try {\n"
4943 " // something\n"
4944 "}\n"
4945 "@finally {\n"
4946 " // something\n"
4947 "}",
4948 Style);
4949 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4950 verifyFormat("try\n"
4951 "{\n"
4952 " // something\n"
4953 "}\n"
4954 "catch (...)\n"
4955 "{\n"
4956 " // something\n"
4957 "}",
4958 Style);
4959 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
4960 verifyFormat("try\n"
4961 " {\n"
4962 " // something white\n"
4963 " }\n"
4964 "catch (...)\n"
4965 " {\n"
4966 " // something white\n"
4967 " }",
4968 Style);
4969 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
4970 verifyFormat("try\n"
4971 " {\n"
4972 " // something\n"
4973 " }\n"
4974 "catch (...)\n"
4975 " {\n"
4976 " // something\n"
4977 " }",
4978 Style);
4979 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4980 Style.BraceWrapping.BeforeCatch = true;
4981 verifyFormat("try {\n"
4982 " // something\n"
4983 "}\n"
4984 "catch (...) {\n"
4985 " // something\n"
4986 "}",
4987 Style);
4988}
4989
4990TEST_F(FormatTest, StaticInitializers) {
4991 verifyFormat("static SomeClass SC = {1, 'a'};");
4992
4993 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4994 " 100000000, "
4995 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4996
4997 // Here, everything other than the "}" would fit on a line.
4998 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4999 " 10000000000000000000000000};");
5000 verifyFormat("S s = {a,\n"
5001 "\n"
5002 " b};",
5003 "S s = {\n"
5004 " a,\n"
5005 "\n"
5006 " b\n"
5007 "};");
5008
5009 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5010 // line. However, the formatting looks a bit off and this probably doesn't
5011 // happen often in practice.
5012 verifyFormat("static int Variable[1] = {\n"
5013 " {1000000000000000000000000000000000000}};",
5014 getLLVMStyleWithColumns(40));
5015}
5016
5017TEST_F(FormatTest, DesignatedInitializers) {
5018 verifyFormat("const struct A a = {.a = 1, .b = 2};");
5019 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5020 " .bbbbbbbbbb = 2,\n"
5021 " .cccccccccc = 3,\n"
5022 " .dddddddddd = 4,\n"
5023 " .eeeeeeeeee = 5};");
5024 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5025 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5026 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5027 " .ccccccccccccccccccccccccccc = 3,\n"
5028 " .ddddddddddddddddddddddddddd = 4,\n"
5029 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5030
5031 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5032
5033 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5034 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5035 " [2] = bbbbbbbbbb,\n"
5036 " [3] = cccccccccc,\n"
5037 " [4] = dddddddddd,\n"
5038 " [5] = eeeeeeeeee};");
5039 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5040 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5041 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5042 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5043 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5044 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5045
5046 verifyFormat("for (const TestCase &test_case : {\n"
5047 " TestCase{\n"
5048 " .a = 1,\n"
5049 " .b = 1,\n"
5050 " },\n"
5051 " TestCase{\n"
5052 " .a = 2,\n"
5053 " .b = 2,\n"
5054 " },\n"
5055 " }) {\n"
5056 "}");
5057}
5058
5059TEST_F(FormatTest, BracedInitializerIndentWidth) {
5060 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
5061 Style.BinPackArguments = true;
5062 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5063 Style.BracedInitializerIndentWidth = 6;
5064
5065 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5066 verifyFormat("enum class {\n"
5067 " One,\n"
5068 " Two,\n"
5069 "};",
5070 Style);
5071 verifyFormat("class Foo {\n"
5072 " Foo() {}\n"
5073 " void bar();\n"
5074 "};",
5075 Style);
5076 verifyFormat("void foo() {\n"
5077 " auto bar = baz;\n"
5078 " return baz;\n"
5079 "};",
5080 Style);
5081 verifyFormat("auto foo = [&] {\n"
5082 " auto bar = baz;\n"
5083 " return baz;\n"
5084 "};",
5085 Style);
5086 verifyFormat("{\n"
5087 " auto bar = baz;\n"
5088 " return baz;\n"
5089 "};",
5090 Style);
5091 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5092 verifyFormat("SomeClass clazz(\n"
5093 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5094 " \"zzzzzzzzzzzzzzzzzz\");",
5095 Style);
5096
5097 // The following types of initialization are all affected by
5098 // BracedInitializerIndentWidth. Aggregate initialization.
5099 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5100 " 10000000, 20000000};",
5101 Style);
5102 verifyFormat("SomeStruct s{\n"
5103 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5104 " \"zzzzzzzzzzzzzzzz\"};",
5105 Style);
5106 // Designated initializers.
5107 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5108 " [0] = 10000000, [1] = 20000000};",
5109 Style);
5110 verifyFormat("SomeStruct s{\n"
5111 " .foo = \"xxxxxxxxxxxxx\",\n"
5112 " .bar = \"yyyyyyyyyyyyy\",\n"
5113 " .baz = \"zzzzzzzzzzzzz\"};",
5114 Style);
5115 // List initialization.
5116 verifyFormat("SomeStruct s{\n"
5117 " \"xxxxxxxxxxxxx\",\n"
5118 " \"yyyyyyyyyyyyy\",\n"
5119 " \"zzzzzzzzzzzzz\",\n"
5120 "};",
5121 Style);
5122 verifyFormat("SomeStruct{\n"
5123 " \"xxxxxxxxxxxxx\",\n"
5124 " \"yyyyyyyyyyyyy\",\n"
5125 " \"zzzzzzzzzzzzz\",\n"
5126 "};",
5127 Style);
5128 verifyFormat("new SomeStruct{\n"
5129 " \"xxxxxxxxxxxxx\",\n"
5130 " \"yyyyyyyyyyyyy\",\n"
5131 " \"zzzzzzzzzzzzz\",\n"
5132 "};",
5133 Style);
5134 // Member initializer.
5135 verifyFormat("class SomeClass {\n"
5136 " SomeStruct s{\n"
5137 " \"xxxxxxxxxxxxx\",\n"
5138 " \"yyyyyyyyyyyyy\",\n"
5139 " \"zzzzzzzzzzzzz\",\n"
5140 " };\n"
5141 "};",
5142 Style);
5143 // Constructor member initializer.
5144 verifyFormat("SomeClass::SomeClass : strct{\n"
5145 " \"xxxxxxxxxxxxx\",\n"
5146 " \"yyyyyyyyyyyyy\",\n"
5147 " \"zzzzzzzzzzzzz\",\n"
5148 " } {}",
5149 Style);
5150 // Copy initialization.
5151 verifyFormat("SomeStruct s = SomeStruct{\n"
5152 " \"xxxxxxxxxxxxx\",\n"
5153 " \"yyyyyyyyyyyyy\",\n"
5154 " \"zzzzzzzzzzzzz\",\n"
5155 "};",
5156 Style);
5157 // Copy list initialization.
5158 verifyFormat("SomeStruct s = {\n"
5159 " \"xxxxxxxxxxxxx\",\n"
5160 " \"yyyyyyyyyyyyy\",\n"
5161 " \"zzzzzzzzzzzzz\",\n"
5162 "};",
5163 Style);
5164 // Assignment operand initialization.
5165 verifyFormat("s = {\n"
5166 " \"xxxxxxxxxxxxx\",\n"
5167 " \"yyyyyyyyyyyyy\",\n"
5168 " \"zzzzzzzzzzzzz\",\n"
5169 "};",
5170 Style);
5171 // Returned object initialization.
5172 verifyFormat("return {\n"
5173 " \"xxxxxxxxxxxxx\",\n"
5174 " \"yyyyyyyyyyyyy\",\n"
5175 " \"zzzzzzzzzzzzz\",\n"
5176 "};",
5177 Style);
5178 // Initializer list.
5179 verifyFormat("auto initializerList = {\n"
5180 " \"xxxxxxxxxxxxx\",\n"
5181 " \"yyyyyyyyyyyyy\",\n"
5182 " \"zzzzzzzzzzzzz\",\n"
5183 "};",
5184 Style);
5185 // Function parameter initialization.
5186 verifyFormat("func({\n"
5187 " \"xxxxxxxxxxxxx\",\n"
5188 " \"yyyyyyyyyyyyy\",\n"
5189 " \"zzzzzzzzzzzzz\",\n"
5190 "});",
5191 Style);
5192 // Nested init lists.
5193 verifyFormat("SomeStruct s = {\n"
5194 " {{init1, init2, init3, init4, init5},\n"
5195 " {init1, init2, init3, init4, init5}}};",
5196 Style);
5197 verifyFormat("SomeStruct s = {\n"
5198 " {{\n"
5199 " .init1 = 1,\n"
5200 " .init2 = 2,\n"
5201 " .init3 = 3,\n"
5202 " .init4 = 4,\n"
5203 " .init5 = 5,\n"
5204 " },\n"
5205 " {init1, init2, init3, init4, init5}}};",
5206 Style);
5207 verifyFormat("SomeArrayT a[3] = {\n"
5208 " {\n"
5209 " foo,\n"
5210 " bar,\n"
5211 " },\n"
5212 " {\n"
5213 " foo,\n"
5214 " bar,\n"
5215 " },\n"
5216 " SomeArrayT{},\n"
5217 "};",
5218 Style);
5219 verifyFormat("SomeArrayT a[3] = {\n"
5220 " {foo},\n"
5221 " {\n"
5222 " {\n"
5223 " init1,\n"
5224 " init2,\n"
5225 " init3,\n"
5226 " },\n"
5227 " {\n"
5228 " init1,\n"
5229 " init2,\n"
5230 " init3,\n"
5231 " },\n"
5232 " },\n"
5233 " {baz},\n"
5234 "};",
5235 Style);
5236
5237 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5238 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5239 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5240 " \"zzzzzzzzzzzzz\"};",
5241 Style);
5242}
5243
5244TEST_F(FormatTest, NestedStaticInitializers) {
5245 verifyFormat("static A x = {{{}}};");
5246 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5247 " {init1, init2, init3, init4}}};",
5248 getLLVMStyleWithColumns(50));
5249
5250 verifyFormat("somes Status::global_reps[3] = {\n"
5251 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5252 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5253 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5254 getLLVMStyleWithColumns(60));
5255 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5256 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5257 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5258 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5259 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5260 " {rect.fRight - rect.fLeft, rect.fBottom - "
5261 "rect.fTop}};");
5262
5263 verifyFormat(
5264 "SomeArrayOfSomeType a = {\n"
5265 " {{1, 2, 3},\n"
5266 " {1, 2, 3},\n"
5267 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5268 " 333333333333333333333333333333},\n"
5269 " {1, 2, 3},\n"
5270 " {1, 2, 3}}};");
5271 verifyFormat(
5272 "SomeArrayOfSomeType a = {\n"
5273 " {{1, 2, 3}},\n"
5274 " {{1, 2, 3}},\n"
5275 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5276 " 333333333333333333333333333333}},\n"
5277 " {{1, 2, 3}},\n"
5278 " {{1, 2, 3}}};");
5279
5280 verifyFormat("struct {\n"
5281 " unsigned bit;\n"
5282 " const char *const name;\n"
5283 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5284 " {kOsWin, \"Windows\"},\n"
5285 " {kOsLinux, \"Linux\"},\n"
5286 " {kOsCrOS, \"Chrome OS\"}};");
5287 verifyFormat("struct {\n"
5288 " unsigned bit;\n"
5289 " const char *const name;\n"
5290 "} kBitsToOs[] = {\n"
5291 " {kOsMac, \"Mac\"},\n"
5292 " {kOsWin, \"Windows\"},\n"
5293 " {kOsLinux, \"Linux\"},\n"
5294 " {kOsCrOS, \"Chrome OS\"},\n"
5295 "};");
5296}
5297
5298TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5299 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5300 " \\\n"
5301 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5302}
5303
5304TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5305 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5306 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5307
5308 // Do break defaulted and deleted functions.
5309 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5310 " default;",
5311 getLLVMStyleWithColumns(40));
5312 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5313 " delete;",
5314 getLLVMStyleWithColumns(40));
5315}
5316
5317TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5318 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5319 getLLVMStyleWithColumns(40));
5320 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5321 getLLVMStyleWithColumns(40));
5322 verifyFormat("#define Q \\\n"
5323 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5324 " \"aaaaaaaa.cpp\"",
5325 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5326 getLLVMStyleWithColumns(40));
5327}
5328
5329TEST_F(FormatTest, UnderstandsLinePPDirective) {
5330 verifyFormat("# 123 \"A string literal\"",
5331 " # 123 \"A string literal\"");
5332}
5333
5334TEST_F(FormatTest, LayoutUnknownPPDirective) {
5335 verifyFormat("#;");
5336 verifyFormat("#\n;\n;\n;");
5337}
5338
5339TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5340 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5341 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5342 getLLVMStyleWithColumns(12));
5343}
5344
5345TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5346 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5347 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5348}
5349
5350TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5351 verifyFormat("#define A \\x20");
5352 verifyFormat("#define A \\ x20");
5353 verifyFormat("#define A \\ x20", "#define A \\ x20");
5354 verifyFormat("#define A ''");
5355 verifyFormat("#define A ''qqq");
5356 verifyFormat("#define A `qqq");
5357 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5358 verifyFormat("const char *c = STRINGIFY(\n"
5359 "\\na : b);",
5360 "const char * c = STRINGIFY(\n"
5361 "\\na : b);");
5362
5363 verifyFormat("a\r\\");
5364 verifyFormat("a\v\\");
5365 verifyFormat("a\f\\");
5366}
5367
5368TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5369 FormatStyle style = getChromiumStyle(Language: FormatStyle::LK_Cpp);
5370 style.IndentWidth = 4;
5371 style.PPIndentWidth = 1;
5372
5373 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5374 verifyFormat("#ifdef __linux__\n"
5375 "void foo() {\n"
5376 " int x = 0;\n"
5377 "}\n"
5378 "#define FOO\n"
5379 "#endif\n"
5380 "void bar() {\n"
5381 " int y = 0;\n"
5382 "}",
5383 style);
5384
5385 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5386 verifyFormat("#ifdef __linux__\n"
5387 "void foo() {\n"
5388 " int x = 0;\n"
5389 "}\n"
5390 "# define FOO foo\n"
5391 "#endif\n"
5392 "void bar() {\n"
5393 " int y = 0;\n"
5394 "}",
5395 style);
5396
5397 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5398 verifyFormat("#ifdef __linux__\n"
5399 "void foo() {\n"
5400 " int x = 0;\n"
5401 "}\n"
5402 " #define FOO foo\n"
5403 "#endif\n"
5404 "void bar() {\n"
5405 " int y = 0;\n"
5406 "}",
5407 style);
5408 verifyFormat("#if 1\n"
5409 " // some comments\n"
5410 " // another\n"
5411 " #define foo 1\n"
5412 "// not a define comment\n"
5413 "void bar() {\n"
5414 " // comment\n"
5415 " int y = 0;\n"
5416 "}",
5417 "#if 1\n"
5418 "// some comments\n"
5419 "// another\n"
5420 "#define foo 1\n"
5421 "// not a define comment\n"
5422 "void bar() {\n"
5423 " // comment\n"
5424 " int y = 0;\n"
5425 "}",
5426 style);
5427
5428 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5429 verifyFormat("#ifdef foo\n"
5430 "#define bar() \\\n"
5431 " if (A) { \\\n"
5432 " B(); \\\n"
5433 " } \\\n"
5434 " C();\n"
5435 "#endif",
5436 style);
5437 verifyFormat("if (emacs) {\n"
5438 "#ifdef is\n"
5439 "#define lit \\\n"
5440 " if (af) { \\\n"
5441 " return duh(); \\\n"
5442 " }\n"
5443 "#endif\n"
5444 "}",
5445 style);
5446 verifyFormat("#if abc\n"
5447 "#ifdef foo\n"
5448 "#define bar() \\\n"
5449 " if (A) { \\\n"
5450 " if (B) { \\\n"
5451 " C(); \\\n"
5452 " } \\\n"
5453 " } \\\n"
5454 " D();\n"
5455 "#endif\n"
5456 "#endif",
5457 style);
5458 verifyFormat("#ifndef foo\n"
5459 "#define foo\n"
5460 "if (emacs) {\n"
5461 "#ifdef is\n"
5462 "#define lit \\\n"
5463 " if (af) { \\\n"
5464 " return duh(); \\\n"
5465 " }\n"
5466 "#endif\n"
5467 "}\n"
5468 "#endif",
5469 style);
5470 verifyFormat("#if 1\n"
5471 "#define X \\\n"
5472 " { \\\n"
5473 " x; \\\n"
5474 " x; \\\n"
5475 " }\n"
5476 "#endif",
5477 style);
5478 verifyFormat("#define X \\\n"
5479 " { \\\n"
5480 " x; \\\n"
5481 " x; \\\n"
5482 " }",
5483 style);
5484
5485 style.PPIndentWidth = 2;
5486 verifyFormat("#ifdef foo\n"
5487 "#define bar() \\\n"
5488 " if (A) { \\\n"
5489 " B(); \\\n"
5490 " } \\\n"
5491 " C();\n"
5492 "#endif",
5493 style);
5494 style.IndentWidth = 8;
5495 verifyFormat("#ifdef foo\n"
5496 "#define bar() \\\n"
5497 " if (A) { \\\n"
5498 " B(); \\\n"
5499 " } \\\n"
5500 " C();\n"
5501 "#endif",
5502 style);
5503
5504 style.IndentWidth = 1;
5505 style.PPIndentWidth = 4;
5506 verifyFormat("#if 1\n"
5507 "#define X \\\n"
5508 " { \\\n"
5509 " x; \\\n"
5510 " x; \\\n"
5511 " }\n"
5512 "#endif",
5513 style);
5514 verifyFormat("#define X \\\n"
5515 " { \\\n"
5516 " x; \\\n"
5517 " x; \\\n"
5518 " }",
5519 style);
5520
5521 style.IndentWidth = 4;
5522 style.PPIndentWidth = 1;
5523 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5524 verifyFormat("#ifdef foo\n"
5525 "# define bar() \\\n"
5526 " if (A) { \\\n"
5527 " B(); \\\n"
5528 " } \\\n"
5529 " C();\n"
5530 "#endif",
5531 style);
5532 verifyFormat("#if abc\n"
5533 "# ifdef foo\n"
5534 "# define bar() \\\n"
5535 " if (A) { \\\n"
5536 " if (B) { \\\n"
5537 " C(); \\\n"
5538 " } \\\n"
5539 " } \\\n"
5540 " D();\n"
5541 "# endif\n"
5542 "#endif",
5543 style);
5544 verifyFormat("#ifndef foo\n"
5545 "#define foo\n"
5546 "if (emacs) {\n"
5547 "#ifdef is\n"
5548 "# define lit \\\n"
5549 " if (af) { \\\n"
5550 " return duh(); \\\n"
5551 " }\n"
5552 "#endif\n"
5553 "}\n"
5554 "#endif",
5555 style);
5556 verifyFormat("#define X \\\n"
5557 " { \\\n"
5558 " x; \\\n"
5559 " x; \\\n"
5560 " }",
5561 style);
5562
5563 style.PPIndentWidth = 2;
5564 style.IndentWidth = 8;
5565 verifyFormat("#ifdef foo\n"
5566 "# define bar() \\\n"
5567 " if (A) { \\\n"
5568 " B(); \\\n"
5569 " } \\\n"
5570 " C();\n"
5571 "#endif",
5572 style);
5573
5574 style.PPIndentWidth = 4;
5575 style.IndentWidth = 1;
5576 verifyFormat("#define X \\\n"
5577 " { \\\n"
5578 " x; \\\n"
5579 " x; \\\n"
5580 " }",
5581 style);
5582
5583 style.IndentWidth = 4;
5584 style.PPIndentWidth = 1;
5585 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5586 verifyFormat("if (emacs) {\n"
5587 "#ifdef is\n"
5588 " #define lit \\\n"
5589 " if (af) { \\\n"
5590 " return duh(); \\\n"
5591 " }\n"
5592 "#endif\n"
5593 "}",
5594 style);
5595 verifyFormat("#if abc\n"
5596 " #ifdef foo\n"
5597 " #define bar() \\\n"
5598 " if (A) { \\\n"
5599 " B(); \\\n"
5600 " } \\\n"
5601 " C();\n"
5602 " #endif\n"
5603 "#endif",
5604 style);
5605 verifyFormat("#if 1\n"
5606 " #define X \\\n"
5607 " { \\\n"
5608 " x; \\\n"
5609 " x; \\\n"
5610 " }\n"
5611 "#endif",
5612 style);
5613
5614 style.PPIndentWidth = 2;
5615 verifyFormat("#ifdef foo\n"
5616 " #define bar() \\\n"
5617 " if (A) { \\\n"
5618 " B(); \\\n"
5619 " } \\\n"
5620 " C();\n"
5621 "#endif",
5622 style);
5623
5624 style.PPIndentWidth = 4;
5625 style.IndentWidth = 1;
5626 verifyFormat("#if 1\n"
5627 " #define X \\\n"
5628 " { \\\n"
5629 " x; \\\n"
5630 " x; \\\n"
5631 " }\n"
5632 "#endif",
5633 style);
5634}
5635
5636TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5637 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5638 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5639 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5640 // FIXME: We never break before the macro name.
5641 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5642
5643 verifyFormat("#define A A\n#define A A");
5644 verifyFormat("#define A(X) A\n#define A A");
5645
5646 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5647 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5648}
5649
5650TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5651 verifyFormat("// somecomment\n"
5652 "#include \"a.h\"\n"
5653 "#define A( \\\n"
5654 " A, B)\n"
5655 "#include \"b.h\"\n"
5656 "// somecomment",
5657 " // somecomment\n"
5658 " #include \"a.h\"\n"
5659 "#define A(A,\\\n"
5660 " B)\n"
5661 " #include \"b.h\"\n"
5662 " // somecomment",
5663 getLLVMStyleWithColumns(13));
5664}
5665
5666TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5667
5668TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5669 verifyFormat("#define A \\\n"
5670 " c; \\\n"
5671 " e;\n"
5672 "f;",
5673 "#define A c; e;\n"
5674 "f;",
5675 getLLVMStyleWithColumns(14));
5676}
5677
5678TEST_F(FormatTest, LayoutRemainingTokens) {
5679 verifyFormat("{\n"
5680 "}");
5681}
5682
5683TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5684 verifyFormat("int x,\n"
5685 "#define A\n"
5686 " y;",
5687 "int x,\n#define A\ny;");
5688}
5689
5690TEST_F(FormatTest, HashInMacroDefinition) {
5691 verifyFormat("#define A(c) L#c");
5692 verifyFormat("#define A(c) u#c");
5693 verifyFormat("#define A(c) U#c");
5694 verifyFormat("#define A(c) u8#c");
5695 verifyFormat("#define A(c) LR#c");
5696 verifyFormat("#define A(c) uR#c");
5697 verifyFormat("#define A(c) UR#c");
5698 verifyFormat("#define A(c) u8R#c");
5699 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5700 verifyFormat("#define A \\\n"
5701 " { \\\n"
5702 " f(#c); \\\n"
5703 " }",
5704 getLLVMStyleWithColumns(11));
5705
5706 verifyFormat("#define A(X) \\\n"
5707 " void function##X()",
5708 getLLVMStyleWithColumns(22));
5709
5710 verifyFormat("#define A(a, b, c) \\\n"
5711 " void a##b##c()",
5712 getLLVMStyleWithColumns(22));
5713
5714 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5715}
5716
5717TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5718 verifyFormat("#define A (x)");
5719 verifyFormat("#define A(x)");
5720
5721 FormatStyle Style = getLLVMStyle();
5722 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5723 verifyFormat("#define true ((foo)1)", Style);
5724 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5725 verifyFormat("#define false((foo)0)", Style);
5726}
5727
5728TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5729 verifyFormat("#define A b;",
5730 "#define A \\\n"
5731 " \\\n"
5732 " b;",
5733 getLLVMStyleWithColumns(25));
5734 verifyNoChange("#define A \\\n"
5735 " \\\n"
5736 " a; \\\n"
5737 " b;",
5738 getLLVMStyleWithColumns(11));
5739 verifyNoChange("#define A \\\n"
5740 " a; \\\n"
5741 " \\\n"
5742 " b;",
5743 getLLVMStyleWithColumns(11));
5744}
5745
5746TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5747 verifyIncompleteFormat("#define A :");
5748 verifyFormat("#define SOMECASES \\\n"
5749 " case 1: \\\n"
5750 " case 2",
5751 getLLVMStyleWithColumns(20));
5752 verifyFormat("#define MACRO(a) \\\n"
5753 " if (a) \\\n"
5754 " f(); \\\n"
5755 " else \\\n"
5756 " g()",
5757 getLLVMStyleWithColumns(18));
5758 verifyFormat("#define A template <typename T>");
5759 verifyIncompleteFormat("#define STR(x) #x\n"
5760 "f(STR(this_is_a_string_literal{));");
5761 verifyFormat("#pragma omp threadprivate( \\\n"
5762 " y)), // expected-warning",
5763 getLLVMStyleWithColumns(28));
5764 verifyFormat("#d, = };");
5765 verifyFormat("#if \"a");
5766 verifyIncompleteFormat("({\n"
5767 "#define b \\\n"
5768 " } \\\n"
5769 " a\n"
5770 "a",
5771 getLLVMStyleWithColumns(15));
5772 verifyFormat("#define A \\\n"
5773 " { \\\n"
5774 " {\n"
5775 "#define B \\\n"
5776 " } \\\n"
5777 " }",
5778 getLLVMStyleWithColumns(15));
5779 verifyNoCrash(Code: "#if a\na(\n#else\n#endif\n{a");
5780 verifyNoCrash(Code: "a={0,1\n#if a\n#else\n;\n#endif\n}");
5781 verifyNoCrash(Code: "#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5782 verifyNoCrash(Code: "#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5783 verifyNoCrash(Code: "#else\n"
5784 "#else\n"
5785 "#endif\n"
5786 "#endif");
5787 verifyNoCrash(Code: "#else\n"
5788 "#if X\n"
5789 "#endif\n"
5790 "#endif");
5791 verifyNoCrash(Code: "#else\n"
5792 "#endif\n"
5793 "#if X\n"
5794 "#endif");
5795 verifyNoCrash(Code: "#if X\n"
5796 "#else\n"
5797 "#else\n"
5798 "#endif\n"
5799 "#endif");
5800 verifyNoCrash(Code: "#if X\n"
5801 "#elif Y\n"
5802 "#elif Y\n"
5803 "#endif\n"
5804 "#endif");
5805 verifyNoCrash(Code: "#endif\n"
5806 "#endif");
5807 verifyNoCrash(Code: "#endif\n"
5808 "#else");
5809 verifyNoCrash(Code: "#endif\n"
5810 "#elif Y");
5811}
5812
5813TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5814 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5815 verifyFormat("class A : public QObject {\n"
5816 " Q_OBJECT\n"
5817 "\n"
5818 " A() {}\n"
5819 "};",
5820 "class A : public QObject {\n"
5821 " Q_OBJECT\n"
5822 "\n"
5823 " A() {\n}\n"
5824 "} ;");
5825 verifyFormat("MACRO\n"
5826 "/*static*/ int i;",
5827 "MACRO\n"
5828 " /*static*/ int i;");
5829 verifyFormat("SOME_MACRO\n"
5830 "namespace {\n"
5831 "void f();\n"
5832 "} // namespace",
5833 "SOME_MACRO\n"
5834 " namespace {\n"
5835 "void f( );\n"
5836 "} // namespace");
5837 // Only if the identifier contains at least 5 characters.
5838 verifyFormat("HTTP f();", "HTTP\nf();");
5839 verifyNoChange("MACRO\nf();");
5840 // Only if everything is upper case.
5841 verifyFormat("class A : public QObject {\n"
5842 " Q_Object A() {}\n"
5843 "};",
5844 "class A : public QObject {\n"
5845 " Q_Object\n"
5846 " A() {\n}\n"
5847 "} ;");
5848
5849 // Only if the next line can actually start an unwrapped line.
5850 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5851 "<< SomeThing;");
5852
5853 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5854 "(n, buffers))",
5855 getChromiumStyle(FormatStyle::LK_Cpp));
5856
5857 // See PR41483
5858 verifyNoChange("/**/ FOO(a)\n"
5859 "FOO(b)");
5860}
5861
5862TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5863 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5864 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5865 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5866 "class X {};\n"
5867 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5868 "int *createScopDetectionPass() { return 0; }",
5869 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5870 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5871 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5872 " class X {};\n"
5873 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5874 " int *createScopDetectionPass() { return 0; }");
5875 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5876 // braces, so that inner block is indented one level more.
5877 verifyFormat("int q() {\n"
5878 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5879 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5880 " IPC_END_MESSAGE_MAP()\n"
5881 "}",
5882 "int q() {\n"
5883 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5884 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5885 " IPC_END_MESSAGE_MAP()\n"
5886 "}");
5887
5888 // Same inside macros.
5889 verifyFormat("#define LIST(L) \\\n"
5890 " L(A) \\\n"
5891 " L(B) \\\n"
5892 " L(C)",
5893 "#define LIST(L) \\\n"
5894 " L(A) \\\n"
5895 " L(B) \\\n"
5896 " L(C)",
5897 getGoogleStyle());
5898
5899 // These must not be recognized as macros.
5900 verifyFormat("int q() {\n"
5901 " f(x);\n"
5902 " f(x) {}\n"
5903 " f(x)->g();\n"
5904 " f(x)->*g();\n"
5905 " f(x).g();\n"
5906 " f(x) = x;\n"
5907 " f(x) += x;\n"
5908 " f(x) -= x;\n"
5909 " f(x) *= x;\n"
5910 " f(x) /= x;\n"
5911 " f(x) %= x;\n"
5912 " f(x) &= x;\n"
5913 " f(x) |= x;\n"
5914 " f(x) ^= x;\n"
5915 " f(x) >>= x;\n"
5916 " f(x) <<= x;\n"
5917 " f(x)[y].z();\n"
5918 " LOG(INFO) << x;\n"
5919 " ifstream(x) >> x;\n"
5920 "}",
5921 "int q() {\n"
5922 " f(x)\n;\n"
5923 " f(x)\n {}\n"
5924 " f(x)\n->g();\n"
5925 " f(x)\n->*g();\n"
5926 " f(x)\n.g();\n"
5927 " f(x)\n = x;\n"
5928 " f(x)\n += x;\n"
5929 " f(x)\n -= x;\n"
5930 " f(x)\n *= x;\n"
5931 " f(x)\n /= x;\n"
5932 " f(x)\n %= x;\n"
5933 " f(x)\n &= x;\n"
5934 " f(x)\n |= x;\n"
5935 " f(x)\n ^= x;\n"
5936 " f(x)\n >>= x;\n"
5937 " f(x)\n <<= x;\n"
5938 " f(x)\n[y].z();\n"
5939 " LOG(INFO)\n << x;\n"
5940 " ifstream(x)\n >> x;\n"
5941 "}");
5942 verifyFormat("int q() {\n"
5943 " F(x)\n"
5944 " if (1) {\n"
5945 " }\n"
5946 " F(x)\n"
5947 " while (1) {\n"
5948 " }\n"
5949 " F(x)\n"
5950 " G(x);\n"
5951 " F(x)\n"
5952 " try {\n"
5953 " Q();\n"
5954 " } catch (...) {\n"
5955 " }\n"
5956 "}",
5957 "int q() {\n"
5958 "F(x)\n"
5959 "if (1) {}\n"
5960 "F(x)\n"
5961 "while (1) {}\n"
5962 "F(x)\n"
5963 "G(x);\n"
5964 "F(x)\n"
5965 "try { Q(); } catch (...) {}\n"
5966 "}");
5967 verifyFormat("class A {\n"
5968 " A() : t(0) {}\n"
5969 " A(int i) noexcept() : {}\n"
5970 " A(X x)\n" // FIXME: function-level try blocks are broken.
5971 " try : t(0) {\n"
5972 " } catch (...) {\n"
5973 " }\n"
5974 "};",
5975 "class A {\n"
5976 " A()\n : t(0) {}\n"
5977 " A(int i)\n noexcept() : {}\n"
5978 " A(X x)\n"
5979 " try : t(0) {} catch (...) {}\n"
5980 "};");
5981 FormatStyle Style = getLLVMStyle();
5982 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5983 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
5984 Style.BraceWrapping.AfterFunction = true;
5985 verifyFormat("void f()\n"
5986 "try\n"
5987 "{\n"
5988 "}",
5989 "void f() try {\n"
5990 "}",
5991 Style);
5992 verifyFormat("class SomeClass {\n"
5993 "public:\n"
5994 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5995 "};",
5996 "class SomeClass {\n"
5997 "public:\n"
5998 " SomeClass()\n"
5999 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6000 "};");
6001 verifyFormat("class SomeClass {\n"
6002 "public:\n"
6003 " SomeClass()\n"
6004 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6005 "};",
6006 "class SomeClass {\n"
6007 "public:\n"
6008 " SomeClass()\n"
6009 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6010 "};",
6011 getLLVMStyleWithColumns(40));
6012
6013 verifyFormat("MACRO(>)");
6014
6015 // Some macros contain an implicit semicolon.
6016 Style = getLLVMStyle();
6017 Style.StatementMacros.push_back(x: "FOO");
6018 verifyFormat("FOO(a) int b = 0;");
6019 verifyFormat("FOO(a)\n"
6020 "int b = 0;",
6021 Style);
6022 verifyFormat("FOO(a);\n"
6023 "int b = 0;",
6024 Style);
6025 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6026 "int b = 0;",
6027 Style);
6028 verifyFormat("FOO()\n"
6029 "int b = 0;",
6030 Style);
6031 verifyFormat("FOO\n"
6032 "int b = 0;",
6033 Style);
6034 verifyFormat("void f() {\n"
6035 " FOO(a)\n"
6036 " return a;\n"
6037 "}",
6038 Style);
6039 verifyFormat("FOO(a)\n"
6040 "FOO(b)",
6041 Style);
6042 verifyFormat("int a = 0;\n"
6043 "FOO(b)\n"
6044 "int c = 0;",
6045 Style);
6046 verifyFormat("int a = 0;\n"
6047 "int x = FOO(a)\n"
6048 "int b = 0;",
6049 Style);
6050 verifyFormat("void foo(int a) { FOO(a) }\n"
6051 "uint32_t bar() {}",
6052 Style);
6053}
6054
6055TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6056 FormatStyle ZeroColumn = getLLVMStyleWithColumns(ColumnLimit: 0);
6057
6058 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6059 ZeroColumn);
6060}
6061
6062TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6063 verifyFormat("#define A \\\n"
6064 " f({ \\\n"
6065 " g(); \\\n"
6066 " });",
6067 getLLVMStyleWithColumns(11));
6068}
6069
6070TEST_F(FormatTest, IndentPreprocessorDirectives) {
6071 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
6072 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6073 verifyFormat("#ifdef _WIN32\n"
6074 "#define A 0\n"
6075 "#ifdef VAR2\n"
6076 "#define B 1\n"
6077 "#include <someheader.h>\n"
6078 "#define MACRO \\\n"
6079 " some_very_long_func_aaaaaaaaaa();\n"
6080 "#endif\n"
6081 "#else\n"
6082 "#define A 1\n"
6083 "#endif",
6084 Style);
6085 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6086 verifyFormat("#if 1\n"
6087 "# define __STR(x) #x\n"
6088 "#endif",
6089 Style);
6090 verifyFormat("#ifdef _WIN32\n"
6091 "# define A 0\n"
6092 "# ifdef VAR2\n"
6093 "# define B 1\n"
6094 "# include <someheader.h>\n"
6095 "# define MACRO \\\n"
6096 " some_very_long_func_aaaaaaaaaa();\n"
6097 "# endif\n"
6098 "#else\n"
6099 "# define A 1\n"
6100 "#endif",
6101 Style);
6102 verifyFormat("#if A\n"
6103 "# define MACRO \\\n"
6104 " void a(int x) { \\\n"
6105 " b(); \\\n"
6106 " c(); \\\n"
6107 " d(); \\\n"
6108 " e(); \\\n"
6109 " f(); \\\n"
6110 " }\n"
6111 "#endif",
6112 Style);
6113 // Comments before include guard.
6114 verifyFormat("// file comment\n"
6115 "// file comment\n"
6116 "#ifndef HEADER_H\n"
6117 "#define HEADER_H\n"
6118 "code();\n"
6119 "#endif",
6120 Style);
6121 // Test with include guards.
6122 verifyFormat("#ifndef HEADER_H\n"
6123 "#define HEADER_H\n"
6124 "code();\n"
6125 "#endif",
6126 Style);
6127 // Include guards must have a #define with the same variable immediately
6128 // after #ifndef.
6129 verifyFormat("#ifndef NOT_GUARD\n"
6130 "# define FOO\n"
6131 "code();\n"
6132 "#endif",
6133 Style);
6134
6135 // Include guards must cover the entire file.
6136 verifyFormat("code();\n"
6137 "code();\n"
6138 "#ifndef NOT_GUARD\n"
6139 "# define NOT_GUARD\n"
6140 "code();\n"
6141 "#endif",
6142 Style);
6143 verifyFormat("#ifndef NOT_GUARD\n"
6144 "# define NOT_GUARD\n"
6145 "code();\n"
6146 "#endif\n"
6147 "code();",
6148 Style);
6149 // Test with trailing blank lines.
6150 verifyFormat("#ifndef HEADER_H\n"
6151 "#define HEADER_H\n"
6152 "code();\n"
6153 "#endif",
6154 Style);
6155 // Include guards don't have #else.
6156 verifyFormat("#ifndef NOT_GUARD\n"
6157 "# define NOT_GUARD\n"
6158 "code();\n"
6159 "#else\n"
6160 "#endif",
6161 Style);
6162 verifyFormat("#ifndef NOT_GUARD\n"
6163 "# define NOT_GUARD\n"
6164 "code();\n"
6165 "#elif FOO\n"
6166 "#endif",
6167 Style);
6168 // Non-identifier #define after potential include guard.
6169 verifyFormat("#ifndef FOO\n"
6170 "# define 1\n"
6171 "#endif",
6172 Style);
6173 // #if closes past last non-preprocessor line.
6174 verifyFormat("#ifndef FOO\n"
6175 "#define FOO\n"
6176 "#if 1\n"
6177 "int i;\n"
6178 "# define A 0\n"
6179 "#endif\n"
6180 "#endif",
6181 Style);
6182 // Don't crash if there is an #elif directive without a condition.
6183 verifyFormat("#if 1\n"
6184 "int x;\n"
6185 "#elif\n"
6186 "int y;\n"
6187 "#else\n"
6188 "int z;\n"
6189 "#endif",
6190 Style);
6191 // FIXME: This doesn't handle the case where there's code between the
6192 // #ifndef and #define but all other conditions hold. This is because when
6193 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6194 // previous code line yet, so we can't detect it.
6195 verifyFormat("#ifndef NOT_GUARD\n"
6196 "code();\n"
6197 "#define NOT_GUARD\n"
6198 "code();\n"
6199 "#endif",
6200 "#ifndef NOT_GUARD\n"
6201 "code();\n"
6202 "# define NOT_GUARD\n"
6203 "code();\n"
6204 "#endif",
6205 Style);
6206 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6207 // be outside an include guard. Examples are #pragma once and
6208 // #pragma GCC diagnostic, or anything else that does not change the meaning
6209 // of the file if it's included multiple times.
6210 verifyFormat("#ifdef WIN32\n"
6211 "# pragma once\n"
6212 "#endif\n"
6213 "#ifndef HEADER_H\n"
6214 "# define HEADER_H\n"
6215 "code();\n"
6216 "#endif",
6217 "#ifdef WIN32\n"
6218 "# pragma once\n"
6219 "#endif\n"
6220 "#ifndef HEADER_H\n"
6221 "#define HEADER_H\n"
6222 "code();\n"
6223 "#endif",
6224 Style);
6225 // FIXME: This does not detect when there is a single non-preprocessor line
6226 // in front of an include-guard-like structure where other conditions hold
6227 // because ScopedLineState hides the line.
6228 verifyFormat("code();\n"
6229 "#ifndef HEADER_H\n"
6230 "#define HEADER_H\n"
6231 "code();\n"
6232 "#endif",
6233 "code();\n"
6234 "#ifndef HEADER_H\n"
6235 "# define HEADER_H\n"
6236 "code();\n"
6237 "#endif",
6238 Style);
6239 // Keep comments aligned with #, otherwise indent comments normally. These
6240 // tests cannot use verifyFormat because messUp manipulates leading
6241 // whitespace.
6242 {
6243 const char *Expected = ""
6244 "void f() {\n"
6245 "#if 1\n"
6246 "// Preprocessor aligned.\n"
6247 "# define A 0\n"
6248 " // Code. Separated by blank line.\n"
6249 "\n"
6250 "# define B 0\n"
6251 " // Code. Not aligned with #\n"
6252 "# define C 0\n"
6253 "#endif";
6254 const char *ToFormat = ""
6255 "void f() {\n"
6256 "#if 1\n"
6257 "// Preprocessor aligned.\n"
6258 "# define A 0\n"
6259 "// Code. Separated by blank line.\n"
6260 "\n"
6261 "# define B 0\n"
6262 " // Code. Not aligned with #\n"
6263 "# define C 0\n"
6264 "#endif";
6265 verifyFormat(Expected, ToFormat, Style);
6266 verifyNoChange(Expected, Style);
6267 }
6268 // Keep block quotes aligned.
6269 {
6270 const char *Expected = ""
6271 "void f() {\n"
6272 "#if 1\n"
6273 "/* Preprocessor aligned. */\n"
6274 "# define A 0\n"
6275 " /* Code. Separated by blank line. */\n"
6276 "\n"
6277 "# define B 0\n"
6278 " /* Code. Not aligned with # */\n"
6279 "# define C 0\n"
6280 "#endif";
6281 const char *ToFormat = ""
6282 "void f() {\n"
6283 "#if 1\n"
6284 "/* Preprocessor aligned. */\n"
6285 "# define A 0\n"
6286 "/* Code. Separated by blank line. */\n"
6287 "\n"
6288 "# define B 0\n"
6289 " /* Code. Not aligned with # */\n"
6290 "# define C 0\n"
6291 "#endif";
6292 verifyFormat(Expected, ToFormat, Style);
6293 verifyNoChange(Expected, Style);
6294 }
6295 // Keep comments aligned with un-indented directives.
6296 {
6297 const char *Expected = ""
6298 "void f() {\n"
6299 "// Preprocessor aligned.\n"
6300 "#define A 0\n"
6301 " // Code. Separated by blank line.\n"
6302 "\n"
6303 "#define B 0\n"
6304 " // Code. Not aligned with #\n"
6305 "#define C 0\n";
6306 const char *ToFormat = ""
6307 "void f() {\n"
6308 "// Preprocessor aligned.\n"
6309 "#define A 0\n"
6310 "// Code. Separated by blank line.\n"
6311 "\n"
6312 "#define B 0\n"
6313 " // Code. Not aligned with #\n"
6314 "#define C 0\n";
6315 verifyFormat(Expected, ToFormat, Style);
6316 verifyNoChange(Expected, Style);
6317 }
6318 // Test AfterHash with tabs.
6319 {
6320 FormatStyle Tabbed = Style;
6321 Tabbed.UseTab = FormatStyle::UT_Always;
6322 Tabbed.IndentWidth = 8;
6323 Tabbed.TabWidth = 8;
6324 verifyFormat("#ifdef _WIN32\n"
6325 "#\tdefine A 0\n"
6326 "#\tifdef VAR2\n"
6327 "#\t\tdefine B 1\n"
6328 "#\t\tinclude <someheader.h>\n"
6329 "#\t\tdefine MACRO \\\n"
6330 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6331 "#\tendif\n"
6332 "#else\n"
6333 "#\tdefine A 1\n"
6334 "#endif",
6335 Tabbed);
6336 }
6337
6338 // Regression test: Multiline-macro inside include guards.
6339 verifyFormat("#ifndef HEADER_H\n"
6340 "#define HEADER_H\n"
6341 "#define A() \\\n"
6342 " int i; \\\n"
6343 " int j;\n"
6344 "#endif // HEADER_H",
6345 getLLVMStyleWithColumns(20));
6346
6347 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6348 // Basic before hash indent tests
6349 verifyFormat("#ifdef _WIN32\n"
6350 " #define A 0\n"
6351 " #ifdef VAR2\n"
6352 " #define B 1\n"
6353 " #include <someheader.h>\n"
6354 " #define MACRO \\\n"
6355 " some_very_long_func_aaaaaaaaaa();\n"
6356 " #endif\n"
6357 "#else\n"
6358 " #define A 1\n"
6359 "#endif",
6360 Style);
6361 verifyFormat("#if A\n"
6362 " #define MACRO \\\n"
6363 " void a(int x) { \\\n"
6364 " b(); \\\n"
6365 " c(); \\\n"
6366 " d(); \\\n"
6367 " e(); \\\n"
6368 " f(); \\\n"
6369 " }\n"
6370 "#endif",
6371 Style);
6372 // Keep comments aligned with indented directives. These
6373 // tests cannot use verifyFormat because messUp manipulates leading
6374 // whitespace.
6375 {
6376 const char *Expected = "void f() {\n"
6377 "// Aligned to preprocessor.\n"
6378 "#if 1\n"
6379 " // Aligned to code.\n"
6380 " int a;\n"
6381 " #if 1\n"
6382 " // Aligned to preprocessor.\n"
6383 " #define A 0\n"
6384 " // Aligned to code.\n"
6385 " int b;\n"
6386 " #endif\n"
6387 "#endif\n"
6388 "}";
6389 const char *ToFormat = "void f() {\n"
6390 "// Aligned to preprocessor.\n"
6391 "#if 1\n"
6392 "// Aligned to code.\n"
6393 "int a;\n"
6394 "#if 1\n"
6395 "// Aligned to preprocessor.\n"
6396 "#define A 0\n"
6397 "// Aligned to code.\n"
6398 "int b;\n"
6399 "#endif\n"
6400 "#endif\n"
6401 "}";
6402 verifyFormat(Expected, ToFormat, Style);
6403 verifyNoChange(Expected, Style);
6404 }
6405 {
6406 const char *Expected = "void f() {\n"
6407 "/* Aligned to preprocessor. */\n"
6408 "#if 1\n"
6409 " /* Aligned to code. */\n"
6410 " int a;\n"
6411 " #if 1\n"
6412 " /* Aligned to preprocessor. */\n"
6413 " #define A 0\n"
6414 " /* Aligned to code. */\n"
6415 " int b;\n"
6416 " #endif\n"
6417 "#endif\n"
6418 "}";
6419 const char *ToFormat = "void f() {\n"
6420 "/* Aligned to preprocessor. */\n"
6421 "#if 1\n"
6422 "/* Aligned to code. */\n"
6423 "int a;\n"
6424 "#if 1\n"
6425 "/* Aligned to preprocessor. */\n"
6426 "#define A 0\n"
6427 "/* Aligned to code. */\n"
6428 "int b;\n"
6429 "#endif\n"
6430 "#endif\n"
6431 "}";
6432 verifyFormat(Expected, ToFormat, Style);
6433 verifyNoChange(Expected, Style);
6434 }
6435
6436 // Test single comment before preprocessor
6437 verifyFormat("// Comment\n"
6438 "\n"
6439 "#if 1\n"
6440 "#endif",
6441 Style);
6442}
6443
6444TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6445 FormatStyle Style = getLLVMStyle();
6446 Style.AlignConsecutiveAssignments.Enabled = true;
6447 Style.AlignConsecutiveDeclarations.Enabled = true;
6448
6449 // Test with just #if blocks.
6450 verifyFormat("void f1() {\n"
6451 "#if 1\n"
6452 " int foo = 1;\n"
6453 " int foobar = 2;\n"
6454 "#endif\n"
6455 "}\n"
6456 "#if 1\n"
6457 "int baz = 3;\n"
6458 "#endif\n"
6459 "void f2() {\n"
6460 "#if 1\n"
6461 " char *foobarbaz = \"foobarbaz\";\n"
6462 " int quux = 4;\n"
6463 "}",
6464 Style);
6465
6466 // Test with just #else blocks.
6467 verifyFormat("void f1() {\n"
6468 "#if 1\n"
6469 "#else\n"
6470 " int foo = 1;\n"
6471 " int foobar = 2;\n"
6472 "#endif\n"
6473 "}\n"
6474 "#if 1\n"
6475 "#else\n"
6476 "int baz = 3;\n"
6477 "#endif\n"
6478 "void f2() {\n"
6479 "#if 1\n"
6480 "#else\n"
6481 " char *foobarbaz = \"foobarbaz\";\n"
6482 " int quux = 4;\n"
6483 "}",
6484 Style);
6485 verifyFormat("auto foo = [] { return; };\n"
6486 "#if FOO\n"
6487 "#else\n"
6488 "count = bar;\n"
6489 "mbid = bid;\n"
6490 "#endif",
6491 Style);
6492
6493 // Test with a mix of #if and #else blocks.
6494 verifyFormat("void f1() {\n"
6495 "#if 1\n"
6496 "#else\n"
6497 " int foo = 1;\n"
6498 " int foobar = 2;\n"
6499 "#endif\n"
6500 "}\n"
6501 "#if 1\n"
6502 "int baz = 3;\n"
6503 "#endif\n"
6504 "void f2() {\n"
6505 "#if 1\n"
6506 "#else\n"
6507 " // prevent alignment with #else in f1\n"
6508 " char *foobarbaz = \"foobarbaz\";\n"
6509 " int quux = 4;\n"
6510 "}",
6511 Style);
6512
6513 // Test with nested #if and #else blocks.
6514 verifyFormat("void f1() {\n"
6515 "#if 1\n"
6516 "#else\n"
6517 "#if 2\n"
6518 "#else\n"
6519 " int foo = 1;\n"
6520 " int foobar = 2;\n"
6521 "#endif\n"
6522 "#endif\n"
6523 "}\n"
6524 "#if 1\n"
6525 "#else\n"
6526 "#if 2\n"
6527 "int baz = 3;\n"
6528 "#endif\n"
6529 "#endif\n"
6530 "void f2() {\n"
6531 "#if 1\n"
6532 "#if 2\n"
6533 "#else\n"
6534 " // prevent alignment with #else in f1\n"
6535 " char *foobarbaz = \"foobarbaz\";\n"
6536 " int quux = 4;\n"
6537 "#endif\n"
6538 "#endif\n"
6539 "}",
6540 Style);
6541
6542 verifyFormat("#if FOO\n"
6543 "int a = 1;\n"
6544 "#else\n"
6545 "int ab = 2;\n"
6546 "#endif\n"
6547 "#ifdef BAR\n"
6548 "int abc = 3;\n"
6549 "#elifdef BAZ\n"
6550 "int abcd = 4;\n"
6551 "#endif",
6552 Style);
6553
6554 verifyFormat("void f() {\n"
6555 " if (foo) {\n"
6556 "#if FOO\n"
6557 " int a = 1;\n"
6558 "#else\n"
6559 " bool a = true;\n"
6560 "#endif\n"
6561 " int abc = 3;\n"
6562 "#ifndef BAR\n"
6563 " int abcd = 4;\n"
6564 "#elif BAZ\n"
6565 " bool abcd = true;\n"
6566 "#endif\n"
6567 " }\n"
6568 "}",
6569 Style);
6570
6571 verifyFormat("void f() {\n"
6572 "#if FOO\n"
6573 " a = 1;\n"
6574 "#else\n"
6575 " ab = 2;\n"
6576 "#endif\n"
6577 "}\n"
6578 "void g() {\n"
6579 "#if BAR\n"
6580 " abc = 3;\n"
6581 "#elifndef BAZ\n"
6582 " abcd = 4;\n"
6583 "#endif\n"
6584 "}",
6585 Style);
6586}
6587
6588TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6589 verifyFormat("{\n"
6590 " {\n"
6591 " a #c;\n"
6592 " }\n"
6593 "}");
6594}
6595
6596TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6597 verifyFormat("#define A \\\n { \\\n {\nint i;",
6598 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6599 verifyFormat("#define A \\\n } \\\n }\nint i;",
6600 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6601}
6602
6603TEST_F(FormatTest, EscapedNewlines) {
6604 FormatStyle Narrow = getLLVMStyleWithColumns(ColumnLimit: 11);
6605 verifyFormat("#define A \\\n int i; \\\n int j;",
6606 "#define A \\\nint i;\\\n int j;", Narrow);
6607 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6608 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6609 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6610 verifyNoChange("<a\n\\\\\n>");
6611
6612 FormatStyle AlignLeft = getLLVMStyle();
6613 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6614 verifyFormat("#define MACRO(x) \\\n"
6615 "private: \\\n"
6616 " int x(int a);",
6617 AlignLeft);
6618
6619 // CRLF line endings
6620 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6621 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6622 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6623 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6624 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6625 verifyNoChange("<a\r\n\\\\\r\n>");
6626 verifyFormat("#define MACRO(x) \\\r\n"
6627 "private: \\\r\n"
6628 " int x(int a);",
6629 AlignLeft);
6630
6631 FormatStyle DontAlign = getLLVMStyle();
6632 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6633 DontAlign.MaxEmptyLinesToKeep = 3;
6634 // FIXME: can't use verifyFormat here because the newline before
6635 // "public:" is not inserted the first time it's reformatted
6636 verifyNoChange("#define A \\\n"
6637 " class Foo { \\\n"
6638 " void bar(); \\\n"
6639 "\\\n"
6640 "\\\n"
6641 "\\\n"
6642 " public: \\\n"
6643 " void baz(); \\\n"
6644 " };",
6645 DontAlign);
6646}
6647
6648TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6649 verifyFormat("#define A \\\n"
6650 " int v( \\\n"
6651 " a); \\\n"
6652 " int i;",
6653 getLLVMStyleWithColumns(11));
6654}
6655
6656TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6657 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6658 " \\\n"
6659 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6660 "\n"
6661 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6662 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6663 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6664 "\\\n"
6665 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6666 " \n"
6667 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6668 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6669}
6670
6671TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6672 verifyFormat("int\n"
6673 "#define A\n"
6674 " a;",
6675 "int\n#define A\na;");
6676 verifyFormat("functionCallTo(\n"
6677 " someOtherFunction(\n"
6678 " withSomeParameters, whichInSequence,\n"
6679 " areLongerThanALine(andAnotherCall,\n"
6680 "#define A B\n"
6681 " withMoreParamters,\n"
6682 " whichStronglyInfluenceTheLayout),\n"
6683 " andMoreParameters),\n"
6684 " trailing);",
6685 getLLVMStyleWithColumns(69));
6686 verifyFormat("Foo::Foo()\n"
6687 "#ifdef BAR\n"
6688 " : baz(0)\n"
6689 "#endif\n"
6690 "{\n"
6691 "}");
6692 verifyFormat("void f() {\n"
6693 " if (true)\n"
6694 "#ifdef A\n"
6695 " f(42);\n"
6696 " x();\n"
6697 "#else\n"
6698 " g();\n"
6699 " x();\n"
6700 "#endif\n"
6701 "}");
6702 verifyFormat("void f(param1, param2,\n"
6703 " param3,\n"
6704 "#ifdef A\n"
6705 " param4(param5,\n"
6706 "#ifdef A1\n"
6707 " param6,\n"
6708 "#ifdef A2\n"
6709 " param7),\n"
6710 "#else\n"
6711 " param8),\n"
6712 " param9,\n"
6713 "#endif\n"
6714 " param10,\n"
6715 "#endif\n"
6716 " param11)\n"
6717 "#else\n"
6718 " param12)\n"
6719 "#endif\n"
6720 "{\n"
6721 " x();\n"
6722 "}",
6723 getLLVMStyleWithColumns(28));
6724 verifyFormat("#if 1\n"
6725 "int i;");
6726 verifyFormat("#if 1\n"
6727 "#endif\n"
6728 "#if 1\n"
6729 "#else\n"
6730 "#endif");
6731 verifyFormat("DEBUG({\n"
6732 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6734 "});\n"
6735 "#if a\n"
6736 "#else\n"
6737 "#endif");
6738
6739 verifyIncompleteFormat("void f(\n"
6740 "#if A\n"
6741 ");\n"
6742 "#else\n"
6743 "#endif");
6744
6745 // Verify that indentation is correct when there is an `#if 0` with an
6746 // `#else`.
6747 verifyFormat("#if 0\n"
6748 "{\n"
6749 "#else\n"
6750 "{\n"
6751 "#endif\n"
6752 " x;\n"
6753 "}");
6754
6755 verifyFormat("#if 0\n"
6756 "#endif\n"
6757 "#if X\n"
6758 "int something_fairly_long; // Align here please\n"
6759 "#endif // Should be aligned");
6760
6761 verifyFormat("#if 0\n"
6762 "#endif\n"
6763 "#if X\n"
6764 "#else // Align\n"
6765 ";\n"
6766 "#endif // Align");
6767
6768 verifyFormat("void SomeFunction(int param1,\n"
6769 " template <\n"
6770 "#ifdef A\n"
6771 "#if 0\n"
6772 "#endif\n"
6773 " MyType<Some>>\n"
6774 "#else\n"
6775 " Type1, Type2>\n"
6776 "#endif\n"
6777 " param2,\n"
6778 " param3) {\n"
6779 " f();\n"
6780 "}");
6781}
6782
6783TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6784 verifyFormat("#endif\n"
6785 "#if B");
6786}
6787
6788TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6789 FormatStyle SingleLine = getLLVMStyle();
6790 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6791 verifyFormat("#if 0\n"
6792 "#elif 1\n"
6793 "#endif\n"
6794 "void foo() {\n"
6795 " if (test) foo2();\n"
6796 "}",
6797 SingleLine);
6798}
6799
6800TEST_F(FormatTest, LayoutBlockInsideParens) {
6801 verifyFormat("functionCall({ int i; });");
6802 verifyFormat("functionCall({\n"
6803 " int i;\n"
6804 " int j;\n"
6805 "});");
6806 verifyFormat("functionCall(\n"
6807 " {\n"
6808 " int i;\n"
6809 " int j;\n"
6810 " },\n"
6811 " aaaa, bbbb, cccc);");
6812 verifyFormat("functionA(functionB({\n"
6813 " int i;\n"
6814 " int j;\n"
6815 " }),\n"
6816 " aaaa, bbbb, cccc);");
6817 verifyFormat("functionCall(\n"
6818 " {\n"
6819 " int i;\n"
6820 " int j;\n"
6821 " },\n"
6822 " aaaa, bbbb, // comment\n"
6823 " cccc);");
6824 verifyFormat("functionA(functionB({\n"
6825 " int i;\n"
6826 " int j;\n"
6827 " }),\n"
6828 " aaaa, bbbb, // comment\n"
6829 " cccc);");
6830 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6831 verifyFormat("functionCall(aaaa, bbbb, {\n"
6832 " int i;\n"
6833 " int j;\n"
6834 "});");
6835 verifyFormat(
6836 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6837 " {\n"
6838 " int i; // break\n"
6839 " },\n"
6840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6841 " ccccccccccccccccc));");
6842 verifyFormat("DEBUG({\n"
6843 " if (a)\n"
6844 " f();\n"
6845 "});");
6846}
6847
6848TEST_F(FormatTest, LayoutBlockInsideStatement) {
6849 verifyFormat("SOME_MACRO { int i; }\n"
6850 "int i;",
6851 " SOME_MACRO {int i;} int i;");
6852}
6853
6854TEST_F(FormatTest, LayoutNestedBlocks) {
6855 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6856 " struct s {\n"
6857 " int i;\n"
6858 " };\n"
6859 " s kBitsToOs[] = {{10}};\n"
6860 " for (int i = 0; i < 10; ++i)\n"
6861 " return;\n"
6862 "}");
6863 verifyFormat("call(parameter, {\n"
6864 " something();\n"
6865 " // Comment using all columns.\n"
6866 " somethingelse();\n"
6867 "});",
6868 getLLVMStyleWithColumns(40));
6869 verifyFormat("DEBUG( //\n"
6870 " { f(); }, a);");
6871 verifyFormat("DEBUG( //\n"
6872 " {\n"
6873 " f(); //\n"
6874 " },\n"
6875 " a);");
6876
6877 verifyFormat("call(parameter, {\n"
6878 " something();\n"
6879 " // Comment too\n"
6880 " // looooooooooong.\n"
6881 " somethingElse();\n"
6882 "});",
6883 "call(parameter, {\n"
6884 " something();\n"
6885 " // Comment too looooooooooong.\n"
6886 " somethingElse();\n"
6887 "});",
6888 getLLVMStyleWithColumns(29));
6889 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6890 verifyFormat("DEBUG({ // comment\n"
6891 " int i;\n"
6892 "});",
6893 "DEBUG({ // comment\n"
6894 "int i;\n"
6895 "});");
6896 verifyFormat("DEBUG({\n"
6897 " int i;\n"
6898 "\n"
6899 " // comment\n"
6900 " int j;\n"
6901 "});",
6902 "DEBUG({\n"
6903 " int i;\n"
6904 "\n"
6905 " // comment\n"
6906 " int j;\n"
6907 "});");
6908
6909 verifyFormat("DEBUG({\n"
6910 " if (a)\n"
6911 " return;\n"
6912 "});");
6913 verifyGoogleFormat("DEBUG({\n"
6914 " if (a) return;\n"
6915 "});");
6916 FormatStyle Style = getGoogleStyle();
6917 Style.ColumnLimit = 45;
6918 verifyFormat("Debug(\n"
6919 " aaaaa,\n"
6920 " {\n"
6921 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6922 " },\n"
6923 " a);",
6924 Style);
6925
6926 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6927
6928 verifyNoCrash(Code: "^{v^{a}}");
6929}
6930
6931TEST_F(FormatTest, FormatNestedBlocksInMacros) {
6932 verifyFormat("#define MACRO() \\\n"
6933 " Debug(aaa, /* force line break */ \\\n"
6934 " { \\\n"
6935 " int i; \\\n"
6936 " int j; \\\n"
6937 " })",
6938 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6939 " { int i; int j; })",
6940 getGoogleStyle());
6941
6942 verifyFormat("#define A \\\n"
6943 " [] { \\\n"
6944 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6945 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6946 " }",
6947 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6948 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6949 getGoogleStyle());
6950}
6951
6952TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
6953 verifyFormat("enum E {};");
6954 verifyFormat("enum E {}");
6955 FormatStyle Style = getLLVMStyle();
6956 Style.SpaceInEmptyBlock = true;
6957 verifyFormat("void f() { }", "void f() {}", Style);
6958 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
6959 verifyFormat("{ }", Style);
6960 verifyFormat("while (true) { }", "while (true) {}", Style);
6961 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6962 Style.BraceWrapping.BeforeElse = false;
6963 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6964 verifyFormat("if (a)\n"
6965 "{\n"
6966 "} else if (b)\n"
6967 "{\n"
6968 "} else\n"
6969 "{ }",
6970 Style);
6971 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
6972 verifyFormat("if (a) {\n"
6973 "} else if (b) {\n"
6974 "} else {\n"
6975 "}",
6976 Style);
6977 Style.BraceWrapping.BeforeElse = true;
6978 verifyFormat("if (a) { }\n"
6979 "else if (b) { }\n"
6980 "else { }",
6981 Style);
6982
6983 Style = getLLVMStyle(Language: FormatStyle::LK_CSharp);
6984 Style.SpaceInEmptyBlock = true;
6985 verifyFormat("Event += () => { };", Style);
6986}
6987
6988TEST_F(FormatTest, FormatBeginBlockEndMacros) {
6989 FormatStyle Style = getLLVMStyle();
6990 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
6991 Style.MacroBlockEnd = "^[A-Z_]+_END$";
6992 verifyFormat("FOO_BEGIN\n"
6993 " FOO_ENTRY\n"
6994 "FOO_END",
6995 Style);
6996 verifyFormat("FOO_BEGIN\n"
6997 " NESTED_FOO_BEGIN\n"
6998 " NESTED_FOO_ENTRY\n"
6999 " NESTED_FOO_END\n"
7000 "FOO_END",
7001 Style);
7002 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7003 " int x;\n"
7004 " x = 1;\n"
7005 "FOO_END(Baz)",
7006 Style);
7007
7008 Style.RemoveBracesLLVM = true;
7009 verifyNoCrash(Code: "for (;;)\n"
7010 " FOO_BEGIN\n"
7011 " foo();\n"
7012 " FOO_END",
7013 Style);
7014}
7015
7016//===----------------------------------------------------------------------===//
7017// Line break tests.
7018//===----------------------------------------------------------------------===//
7019
7020TEST_F(FormatTest, PreventConfusingIndents) {
7021 verifyFormat(
7022 "void f() {\n"
7023 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7024 " parameter, parameter, parameter)),\n"
7025 " SecondLongCall(parameter));\n"
7026 "}");
7027 verifyFormat(
7028 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7029 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7031 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7032 verifyFormat(
7033 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7034 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7035 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7036 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7037 verifyFormat(
7038 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7039 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7041 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7042 verifyFormat("int a = bbbb && ccc &&\n"
7043 " fffff(\n"
7044 "#define A Just forcing a new line\n"
7045 " ddd);");
7046}
7047
7048TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7049 verifyFormat(
7050 "bool aaaaaaa =\n"
7051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7052 " bbbbbbbb();");
7053 verifyFormat(
7054 "bool aaaaaaa =\n"
7055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7056 " bbbbbbbb();");
7057
7058 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7060 " ccccccccc == ddddddddddd;");
7061 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7063 " ccccccccc == ddddddddddd;");
7064 verifyFormat(
7065 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7067 " ccccccccc == ddddddddddd;");
7068
7069 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7070 " aaaaaa) &&\n"
7071 " bbbbbb && cccccc;");
7072 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7073 " aaaaaa) >>\n"
7074 " bbbbbb;");
7075 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7076 " SourceMgr.getSpellingColumnNumber(\n"
7077 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7078 " 1);");
7079
7080 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7081 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7082 " cccccc) {\n}");
7083 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7084 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7085 " cccccc) {\n}");
7086 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7087 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7088 " cccccc) {\n}");
7089 verifyFormat("b = a &&\n"
7090 " // Comment\n"
7091 " b.c && d;");
7092
7093 // If the LHS of a comparison is not a binary expression itself, the
7094 // additional linebreak confuses many people.
7095 verifyFormat(
7096 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7098 "}");
7099 verifyFormat(
7100 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7102 "}");
7103 verifyFormat(
7104 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7106 "}");
7107 verifyFormat(
7108 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7110 "}");
7111 // Even explicit parentheses stress the precedence enough to make the
7112 // additional break unnecessary.
7113 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7115 "}");
7116 // This cases is borderline, but with the indentation it is still readable.
7117 verifyFormat(
7118 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7119 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7121 "}",
7122 getLLVMStyleWithColumns(75));
7123
7124 // If the LHS is a binary expression, we should still use the additional break
7125 // as otherwise the formatting hides the operator precedence.
7126 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7128 " 5) {\n"
7129 "}");
7130 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7132 " 5) {\n"
7133 "}");
7134
7135 FormatStyle OnePerLine = getLLVMStyle();
7136 OnePerLine.BinPackParameters = false;
7137 verifyFormat(
7138 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7141 OnePerLine);
7142
7143 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7144 " .aaa(aaaaaaaaaaaaa) *\n"
7145 " aaaaaaa +\n"
7146 " aaaaaaa;",
7147 getLLVMStyleWithColumns(40));
7148}
7149
7150TEST_F(FormatTest, ExpressionIndentation) {
7151 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7155 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7156 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7159 " ccccccccccccccccccccccccccccccccccccccccc;");
7160 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7163 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7164 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7167 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7168 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7169 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7171 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7172 verifyFormat("if () {\n"
7173 "} else if (aaaaa && bbbbb > // break\n"
7174 " ccccc) {\n"
7175 "}");
7176 verifyFormat("if () {\n"
7177 "} else if constexpr (aaaaa && bbbbb > // break\n"
7178 " ccccc) {\n"
7179 "}");
7180 verifyFormat("if () {\n"
7181 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7182 " ccccc) {\n"
7183 "}");
7184 verifyFormat("if () {\n"
7185 "} else if (aaaaa &&\n"
7186 " bbbbb > // break\n"
7187 " ccccc &&\n"
7188 " ddddd) {\n"
7189 "}");
7190
7191 // Presence of a trailing comment used to change indentation of b.
7192 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7193 " b;\n"
7194 "return aaaaaaaaaaaaaaaaaaa +\n"
7195 " b; //",
7196 getLLVMStyleWithColumns(30));
7197}
7198
7199TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7200 // Not sure what the best system is here. Like this, the LHS can be found
7201 // immediately above an operator (everything with the same or a higher
7202 // indent). The RHS is aligned right of the operator and so compasses
7203 // everything until something with the same indent as the operator is found.
7204 // FIXME: Is this a good system?
7205 FormatStyle Style = getLLVMStyle();
7206 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7207 verifyFormat(
7208 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7209 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7210 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7211 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7212 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7213 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7214 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7215 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7216 " > ccccccccccccccccccccccccccccccccccccccccc;",
7217 Style);
7218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7219 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7220 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7221 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7222 Style);
7223 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7224 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7225 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7226 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7227 Style);
7228 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7229 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7230 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7231 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7232 Style);
7233 verifyFormat("if () {\n"
7234 "} else if (aaaaa\n"
7235 " && bbbbb // break\n"
7236 " > ccccc) {\n"
7237 "}",
7238 Style);
7239 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7240 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7241 Style);
7242 verifyFormat("return (a)\n"
7243 " // comment\n"
7244 " + b;",
7245 Style);
7246 verifyFormat(
7247 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7248 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7249 " + cc;",
7250 Style);
7251
7252 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7253 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7254 Style);
7255
7256 // Forced by comments.
7257 verifyFormat(
7258 "unsigned ContentSize =\n"
7259 " sizeof(int16_t) // DWARF ARange version number\n"
7260 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7261 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7262 " + sizeof(int8_t); // Segment Size (in bytes)");
7263
7264 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7265 " == boost::fusion::at_c<1>(iiii).second;",
7266 Style);
7267
7268 Style.ColumnLimit = 60;
7269 verifyFormat("zzzzzzzzzz\n"
7270 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7271 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7272 Style);
7273
7274 Style.ColumnLimit = 80;
7275 Style.IndentWidth = 4;
7276 Style.TabWidth = 4;
7277 Style.UseTab = FormatStyle::UT_Always;
7278 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7279 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7280 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7281 "\t&& (someOtherLongishConditionPart1\n"
7282 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7283 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7284 "(someOtherLongishConditionPart1 || "
7285 "someOtherEvenLongerNestedConditionPart2);",
7286 Style);
7287
7288 Style = getLLVMStyleWithColumns(ColumnLimit: 20);
7289 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7290 Style.BinPackParameters = false;
7291 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7292 Style.ContinuationIndentWidth = 2;
7293 verifyFormat("struct Foo {\n"
7294 " Foo(\n"
7295 " int arg1,\n"
7296 " int arg2)\n"
7297 " : Base(\n"
7298 " arg1,\n"
7299 " arg2) {}\n"
7300 "};",
7301 Style);
7302 verifyFormat("return abc\n"
7303 " ? foo(\n"
7304 " a,\n"
7305 " b,\n"
7306 " bar(\n"
7307 " abc))\n"
7308 " : g(abc);",
7309 Style);
7310}
7311
7312TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7313 FormatStyle Style = getLLVMStyle();
7314 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7315 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7316
7317 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7318 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7319 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7320 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7321 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7322 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7323 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7324 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7325 " > ccccccccccccccccccccccccccccccccccccccccc;",
7326 Style);
7327 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7328 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7329 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7330 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7331 Style);
7332 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7333 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7334 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7335 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7336 Style);
7337 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7338 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7339 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7340 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7341 Style);
7342 verifyFormat("if () {\n"
7343 "} else if (aaaaa\n"
7344 " && bbbbb // break\n"
7345 " > ccccc) {\n"
7346 "}",
7347 Style);
7348 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7349 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7350 Style);
7351 verifyFormat("return (a)\n"
7352 " // comment\n"
7353 " + b;",
7354 Style);
7355 verifyFormat(
7356 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7357 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7358 " + cc;",
7359 Style);
7360 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7361 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7362 " : 3333333333333333;",
7363 Style);
7364 verifyFormat(
7365 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7366 " : ccccccccccccccc ? dddddddddddddddddd\n"
7367 " : eeeeeeeeeeeeeeeeee)\n"
7368 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7369 " : 3333333333333333;",
7370 Style);
7371 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7372 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7373 Style);
7374
7375 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7376 " == boost::fusion::at_c<1>(iiii).second;",
7377 Style);
7378
7379 Style.ColumnLimit = 60;
7380 verifyFormat("zzzzzzzzzzzzz\n"
7381 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7382 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7383 Style);
7384
7385 // Forced by comments.
7386 Style.ColumnLimit = 80;
7387 verifyFormat(
7388 "unsigned ContentSize\n"
7389 " = sizeof(int16_t) // DWARF ARange version number\n"
7390 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7391 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7392 " + sizeof(int8_t); // Segment Size (in bytes)",
7393 Style);
7394
7395 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7396 verifyFormat(
7397 "unsigned ContentSize =\n"
7398 " sizeof(int16_t) // DWARF ARange version number\n"
7399 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7400 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7401 " + sizeof(int8_t); // Segment Size (in bytes)",
7402 Style);
7403
7404 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7405 verifyFormat(
7406 "unsigned ContentSize =\n"
7407 " sizeof(int16_t) // DWARF ARange version number\n"
7408 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7409 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7410 " + sizeof(int8_t); // Segment Size (in bytes)",
7411 Style);
7412}
7413
7414TEST_F(FormatTest, EnforcedOperatorWraps) {
7415 // Here we'd like to wrap after the || operators, but a comment is forcing an
7416 // earlier wrap.
7417 verifyFormat("bool x = aaaaa //\n"
7418 " || bbbbb\n"
7419 " //\n"
7420 " || cccc;");
7421}
7422
7423TEST_F(FormatTest, NoOperandAlignment) {
7424 FormatStyle Style = getLLVMStyle();
7425 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7426 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7429 Style);
7430 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7431 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7432 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7433 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7434 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7435 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7436 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7437 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7438 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7439 " > ccccccccccccccccccccccccccccccccccccccccc;",
7440 Style);
7441
7442 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7443 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7444 " + cc;",
7445 Style);
7446 verifyFormat("int a = aa\n"
7447 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7448 " * cccccccccccccccccccccccccccccccccccc;",
7449 Style);
7450
7451 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7452 verifyFormat("return (a > b\n"
7453 " // comment1\n"
7454 " // comment2\n"
7455 " || c);",
7456 Style);
7457}
7458
7459TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7460 FormatStyle Style = getLLVMStyle();
7461 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7462 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7464 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7465 Style);
7466}
7467
7468TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7469 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
7470 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7471 Style.BinPackArguments = false;
7472 verifyFormat("void test() {\n"
7473 " someFunction(\n"
7474 " this + argument + is + quite\n"
7475 " + long + so + it + gets + wrapped\n"
7476 " + but + remains + bin - packed);\n"
7477 "}",
7478 Style);
7479 verifyFormat("void test() {\n"
7480 " someFunction(arg1,\n"
7481 " this + argument + is\n"
7482 " + quite + long + so\n"
7483 " + it + gets + wrapped\n"
7484 " + but + remains + bin\n"
7485 " - packed,\n"
7486 " arg3);\n"
7487 "}",
7488 Style);
7489 verifyFormat("void test() {\n"
7490 " someFunction(\n"
7491 " arg1,\n"
7492 " this + argument + has\n"
7493 " + anotherFunc(nested,\n"
7494 " calls + whose\n"
7495 " + arguments\n"
7496 " + are + also\n"
7497 " + wrapped,\n"
7498 " in + addition)\n"
7499 " + to + being + bin - packed,\n"
7500 " arg3);\n"
7501 "}",
7502 Style);
7503
7504 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7505 verifyFormat("void test() {\n"
7506 " someFunction(\n"
7507 " arg1,\n"
7508 " this + argument + has +\n"
7509 " anotherFunc(nested,\n"
7510 " calls + whose +\n"
7511 " arguments +\n"
7512 " are + also +\n"
7513 " wrapped,\n"
7514 " in + addition) +\n"
7515 " to + being + bin - packed,\n"
7516 " arg3);\n"
7517 "}",
7518 Style);
7519}
7520
7521TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7522 auto Style = getLLVMStyleWithColumns(ColumnLimit: 45);
7523 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7524 verifyFormat("bool b =\n"
7525 " is_default_constructible_v<hash<T>> and\n"
7526 " is_copy_constructible_v<hash<T>> and\n"
7527 " is_move_constructible_v<hash<T>> and\n"
7528 " is_copy_assignable_v<hash<T>> and\n"
7529 " is_move_assignable_v<hash<T>> and\n"
7530 " is_destructible_v<hash<T>> and\n"
7531 " is_swappable_v<hash<T>> and\n"
7532 " is_callable_v<hash<T>(T)>;",
7533 Style);
7534
7535 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7536 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7537 " and is_copy_constructible_v<hash<T>>\n"
7538 " and is_move_constructible_v<hash<T>>\n"
7539 " and is_copy_assignable_v<hash<T>>\n"
7540 " and is_move_assignable_v<hash<T>>\n"
7541 " and is_destructible_v<hash<T>>\n"
7542 " and is_swappable_v<hash<T>>\n"
7543 " and is_callable_v<hash<T>(T)>;",
7544 Style);
7545
7546 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7547 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7548 " and is_copy_constructible_v<hash<T>>\n"
7549 " and is_move_constructible_v<hash<T>>\n"
7550 " and is_copy_assignable_v<hash<T>>\n"
7551 " and is_move_assignable_v<hash<T>>\n"
7552 " and is_destructible_v<hash<T>>\n"
7553 " and is_swappable_v<hash<T>>\n"
7554 " and is_callable_v<hash<T>(T)>;",
7555 Style);
7556}
7557
7558TEST_F(FormatTest, ConstructorInitializers) {
7559 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7560 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7561 getLLVMStyleWithColumns(45));
7562 verifyFormat("Constructor()\n"
7563 " : Inttializer(FitsOnTheLine) {}",
7564 getLLVMStyleWithColumns(44));
7565 verifyFormat("Constructor()\n"
7566 " : Inttializer(FitsOnTheLine) {}",
7567 getLLVMStyleWithColumns(43));
7568
7569 verifyFormat("template <typename T>\n"
7570 "Constructor() : Initializer(FitsOnTheLine) {}",
7571 getLLVMStyleWithColumns(45));
7572
7573 verifyFormat(
7574 "SomeClass::Constructor()\n"
7575 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7576
7577 verifyFormat(
7578 "SomeClass::Constructor()\n"
7579 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7580 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7581 verifyFormat(
7582 "SomeClass::Constructor()\n"
7583 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7584 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7585 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7586 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7587 " : aaaaaaaaaa(aaaaaa) {}");
7588
7589 verifyFormat("Constructor()\n"
7590 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7591 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7592 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7593 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7594
7595 verifyFormat("Constructor()\n"
7596 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7598
7599 verifyFormat("Constructor(int Parameter = 0)\n"
7600 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7601 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7602 verifyFormat("Constructor()\n"
7603 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7604 "}",
7605 getLLVMStyleWithColumns(60));
7606 verifyFormat("Constructor()\n"
7607 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7608 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7609
7610 // Here a line could be saved by splitting the second initializer onto two
7611 // lines, but that is not desirable.
7612 verifyFormat("Constructor()\n"
7613 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7614 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7615 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7616
7617 FormatStyle OnePerLine = getLLVMStyle();
7618 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7619 verifyFormat("MyClass::MyClass()\n"
7620 " : a(a),\n"
7621 " b(b),\n"
7622 " c(c) {}",
7623 OnePerLine);
7624 verifyFormat("MyClass::MyClass()\n"
7625 " : a(a), // comment\n"
7626 " b(b),\n"
7627 " c(c) {}",
7628 OnePerLine);
7629 verifyFormat("MyClass::MyClass(int a)\n"
7630 " : b(a), // comment\n"
7631 " c(a + 1) { // lined up\n"
7632 "}",
7633 OnePerLine);
7634 verifyFormat("Constructor()\n"
7635 " : a(b, b, b) {}",
7636 OnePerLine);
7637 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7638 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7639 verifyFormat("SomeClass::Constructor()\n"
7640 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7641 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7642 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7643 OnePerLine);
7644 verifyFormat("SomeClass::Constructor()\n"
7645 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7646 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7647 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7648 OnePerLine);
7649 verifyFormat("MyClass::MyClass(int var)\n"
7650 " : some_var_(var), // 4 space indent\n"
7651 " some_other_var_(var + 1) { // lined up\n"
7652 "}",
7653 OnePerLine);
7654 verifyFormat("Constructor()\n"
7655 " : aaaaa(aaaaaa),\n"
7656 " aaaaa(aaaaaa),\n"
7657 " aaaaa(aaaaaa),\n"
7658 " aaaaa(aaaaaa),\n"
7659 " aaaaa(aaaaaa) {}",
7660 OnePerLine);
7661 verifyFormat("Constructor()\n"
7662 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7663 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7664 OnePerLine);
7665 OnePerLine.BinPackParameters = false;
7666 verifyFormat(
7667 "Constructor()\n"
7668 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7669 " aaaaaaaaaaa().aaa(),\n"
7670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7671 OnePerLine);
7672 OnePerLine.ColumnLimit = 60;
7673 verifyFormat("Constructor()\n"
7674 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7675 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7676 OnePerLine);
7677
7678 verifyFormat("Constructor()\n"
7679 " : // Comment forcing unwanted break.\n"
7680 " aaaa(aaaa) {}",
7681 "Constructor() :\n"
7682 " // Comment forcing unwanted break.\n"
7683 " aaaa(aaaa) {}");
7684}
7685
7686TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7687 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
7688 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7689 Style.BinPackParameters = false;
7690
7691 for (int i = 0; i < 4; ++i) {
7692 // Test all combinations of parameters that should not have an effect.
7693 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7694 Style.AllowAllArgumentsOnNextLine = i & 2;
7695
7696 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7697 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7698 verifyFormat("Constructor()\n"
7699 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7700 Style);
7701 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7702
7703 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7704 verifyFormat("Constructor()\n"
7705 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7706 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7707 Style);
7708 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7709
7710 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7711 verifyFormat("Constructor()\n"
7712 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7713 Style);
7714 verifyFormat("Constructor()\n"
7715 " : a(a), b(b) {}",
7716 Style);
7717 verifyFormat("Constructor()\n"
7718 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7719 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7720 " , cccccccccccccccccccccc(c) {}",
7721 Style);
7722
7723 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7724 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7725 verifyFormat("Constructor()\n"
7726 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7727 Style);
7728
7729 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7730 verifyFormat("Constructor()\n"
7731 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7732 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7733 Style);
7734
7735 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7736 verifyFormat("Constructor()\n"
7737 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7738 Style);
7739 verifyFormat("Constructor()\n"
7740 " : a(a), b(b) {}",
7741 Style);
7742 verifyFormat("Constructor()\n"
7743 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7744 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7745 " cccccccccccccccccccccc(c) {}",
7746 Style);
7747
7748 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7749 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7750 verifyFormat("Constructor() :\n"
7751 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7752 Style);
7753
7754 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7755 verifyFormat("Constructor() :\n"
7756 " aaaaaaaaaaaaaaaaaa(a),\n"
7757 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7758 Style);
7759
7760 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7761 verifyFormat("Constructor() :\n"
7762 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7763 Style);
7764 verifyFormat("Constructor() :\n"
7765 " a(a), b(b) {}",
7766 Style);
7767 verifyFormat("Constructor() :\n"
7768 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7769 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7770 " cccccccccccccccccccccc(c) {}",
7771 Style);
7772 }
7773
7774 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7775 // AllowAllConstructorInitializersOnNextLine in all
7776 // BreakConstructorInitializers modes
7777 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7778 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7779 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7780 verifyFormat("SomeClassWithALongName::Constructor(\n"
7781 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7782 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7783 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7784 Style);
7785
7786 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7787 verifyFormat("SomeClassWithALongName::Constructor(\n"
7788 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7789 " int bbbbbbbbbbbbb,\n"
7790 " int cccccccccccccccc)\n"
7791 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7792 Style);
7793
7794 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7795 verifyFormat("SomeClassWithALongName::Constructor(\n"
7796 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7797 " int bbbbbbbbbbbbb,\n"
7798 " int cccccccccccccccc)\n"
7799 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7800 Style);
7801
7802 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7803 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7804 verifyFormat("SomeClassWithALongName::Constructor(\n"
7805 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7806 " int bbbbbbbbbbbbb)\n"
7807 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7808 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7809 Style);
7810
7811 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7812
7813 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7814 verifyFormat("SomeClassWithALongName::Constructor(\n"
7815 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7816 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7817 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7818 Style);
7819
7820 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7821 verifyFormat("SomeClassWithALongName::Constructor(\n"
7822 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7823 " int bbbbbbbbbbbbb,\n"
7824 " int cccccccccccccccc)\n"
7825 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7826 Style);
7827
7828 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7829 verifyFormat("SomeClassWithALongName::Constructor(\n"
7830 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7831 " int bbbbbbbbbbbbb,\n"
7832 " int cccccccccccccccc)\n"
7833 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7834 Style);
7835
7836 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7837 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7838 verifyFormat("SomeClassWithALongName::Constructor(\n"
7839 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7840 " int bbbbbbbbbbbbb)\n"
7841 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7842 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7843 Style);
7844
7845 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7846 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7847 verifyFormat("SomeClassWithALongName::Constructor(\n"
7848 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7849 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7850 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7851 Style);
7852
7853 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7854 verifyFormat("SomeClassWithALongName::Constructor(\n"
7855 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7856 " int bbbbbbbbbbbbb,\n"
7857 " int cccccccccccccccc) :\n"
7858 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7859 Style);
7860
7861 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7862 verifyFormat("SomeClassWithALongName::Constructor(\n"
7863 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7864 " int bbbbbbbbbbbbb,\n"
7865 " int cccccccccccccccc) :\n"
7866 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7867 Style);
7868
7869 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7870 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7871 verifyFormat("SomeClassWithALongName::Constructor(\n"
7872 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7873 " int bbbbbbbbbbbbb) :\n"
7874 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7875 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7876 Style);
7877
7878 Style = getLLVMStyleWithColumns(ColumnLimit: 0);
7879 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7880 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7881 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7882 " : bar(bar), baz(baz) {}",
7883 Style);
7884}
7885
7886TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
7887 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
7888 Style.BinPackArguments = false;
7889 for (int i = 0; i < 4; ++i) {
7890 // Test all combinations of parameters that should not have an effect.
7891 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7892 Style.PackConstructorInitializers =
7893 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
7894
7895 Style.AllowAllArgumentsOnNextLine = true;
7896 verifyFormat("void foo() {\n"
7897 " FunctionCallWithReallyLongName(\n"
7898 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7899 "}",
7900 Style);
7901 Style.AllowAllArgumentsOnNextLine = false;
7902 verifyFormat("void foo() {\n"
7903 " FunctionCallWithReallyLongName(\n"
7904 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7905 " bbbbbbbbbbbb);\n"
7906 "}",
7907 Style);
7908
7909 Style.AllowAllArgumentsOnNextLine = true;
7910 verifyFormat("void foo() {\n"
7911 " auto VariableWithReallyLongName = {\n"
7912 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7913 "}",
7914 Style);
7915 Style.AllowAllArgumentsOnNextLine = false;
7916 verifyFormat("void foo() {\n"
7917 " auto VariableWithReallyLongName = {\n"
7918 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7919 " bbbbbbbbbbbb};\n"
7920 "}",
7921 Style);
7922 }
7923
7924 // This parameter should not affect declarations.
7925 Style.BinPackParameters = false;
7926 Style.AllowAllArgumentsOnNextLine = false;
7927 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7928 verifyFormat("void FunctionCallWithReallyLongName(\n"
7929 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7930 Style);
7931 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7932 verifyFormat("void FunctionCallWithReallyLongName(\n"
7933 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7934 " int bbbbbbbbbbbb);",
7935 Style);
7936}
7937
7938TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
7939 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7940 // and BAS_Align.
7941 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 35);
7942 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
7943 "void functionDecl(int A, int B, int C);";
7944 Style.AllowAllArgumentsOnNextLine = false;
7945 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7946 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7947 " paramC);\n"
7948 "void functionDecl(int A, int B,\n"
7949 " int C);"),
7950 Input, Style);
7951 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7952 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7953 " paramC);\n"
7954 "void functionDecl(int A, int B,\n"
7955 " int C);"),
7956 Input, Style);
7957 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7958 // AllowAllArgumentsOnNextLine.
7959 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7960 verifyFormat(StringRef("functionCall(\n"
7961 " paramA, paramB, paramC);\n"
7962 "void functionDecl(\n"
7963 " int A, int B, int C);"),
7964 Input, Style);
7965 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
7966 verifyFormat("functionCall(\n"
7967 " paramA, paramB, paramC\n"
7968 ");\n"
7969 "void functionDecl(\n"
7970 " int A, int B, int C\n"
7971 ");",
7972 Input, Style);
7973
7974 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
7975 // first argument.
7976 Style.AllowAllArgumentsOnNextLine = true;
7977 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7978 verifyFormat(StringRef("functionCall(\n"
7979 " paramA, paramB, paramC);\n"
7980 "void functionDecl(\n"
7981 " int A, int B, int C);"),
7982 Input, Style);
7983 // It wouldn't fit on one line with aligned parameters so this setting
7984 // doesn't change anything for BAS_Align.
7985 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
7986 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7987 " paramC);\n"
7988 "void functionDecl(int A, int B,\n"
7989 " int C);"),
7990 Input, Style);
7991 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7992 verifyFormat(StringRef("functionCall(\n"
7993 " paramA, paramB, paramC);\n"
7994 "void functionDecl(\n"
7995 " int A, int B, int C);"),
7996 Input, Style);
7997}
7998
7999TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8000 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8001 "void emptyFunctionDefinition() {}\n"
8002 "void functionDefinition(int A, int B, int C) {}\n"
8003 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8004 verifyFormat(Input);
8005
8006 FormatStyle Style = getLLVMStyle();
8007 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8008 Style.BreakFunctionDefinitionParameters = true;
8009 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8010 "void emptyFunctionDefinition() {}\n"
8011 "void functionDefinition(\n"
8012 " int A, int B, int C) {}\n"
8013 "Class::Class(\n"
8014 " int A, int B)\n"
8015 " : m_A(A), m_B(B) {}",
8016 Input, Style);
8017
8018 // Test the style where all parameters are on their own lines.
8019 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8020 Style.BinPackParameters = false;
8021 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8022 "void emptyFunctionDefinition() {}\n"
8023 "void functionDefinition(\n"
8024 " int A,\n"
8025 " int B,\n"
8026 " int C) {}\n"
8027 "Class::Class(\n"
8028 " int A,\n"
8029 " int B)\n"
8030 " : m_A(A), m_B(B) {}",
8031 Input, Style);
8032}
8033
8034TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8035 FormatStyle Style = getLLVMStyle();
8036 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8037 /* Test the behaviour with long lines */
8038 Style.ColumnLimit = 40;
8039 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8040 " : : val);",
8041 Style);
8042 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8043 " : val1 : val2);",
8044 Style);
8045 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8046 " \"cpuid\\n\\t\"\n"
8047 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8048 " : \"=a\" : \"a\");",
8049 Style);
8050 Style.ColumnLimit = 80;
8051 verifyFormat("asm volatile(\"string\", : : val);", Style);
8052 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8053
8054 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8055 verifyFormat("asm volatile(\"string\",\n"
8056 " :\n"
8057 " : val);",
8058 Style);
8059 verifyFormat("asm volatile(\"string\",\n"
8060 " : val1\n"
8061 " : val2);",
8062 Style);
8063 /* Test the behaviour with long lines */
8064 Style.ColumnLimit = 40;
8065 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8066 " \"cpuid\\n\\t\"\n"
8067 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8068 " : \"=a\"(*rEAX)\n"
8069 " : \"a\"(value));",
8070 Style);
8071 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8072 " \"cpuid\\n\\t\"\n"
8073 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8074 " :\n"
8075 " : \"a\"(value));",
8076 Style);
8077 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8078 " :\n"
8079 " : val);",
8080 Style);
8081 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8082 " : val1\n"
8083 " : val2);",
8084 Style);
8085}
8086
8087TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8088 FormatStyle Style = getLLVMStyle();
8089 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8090
8091 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8092 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8093 getStyleWithColumns(Style, 45));
8094 verifyFormat("Constructor() :\n"
8095 " Initializer(FitsOnTheLine) {}",
8096 getStyleWithColumns(Style, 44));
8097 verifyFormat("Constructor() :\n"
8098 " Initializer(FitsOnTheLine) {}",
8099 getStyleWithColumns(Style, 43));
8100
8101 verifyFormat("template <typename T>\n"
8102 "Constructor() : Initializer(FitsOnTheLine) {}",
8103 getStyleWithColumns(Style, 50));
8104 verifyFormat(
8105 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8106 " int mooooooooooooore) noexcept :\n"
8107 " Super{some, arguments}, Member{5}, Member2{2} {}",
8108 Style);
8109 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8110 verifyFormat(
8111 "SomeClass::Constructor() :\n"
8112 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8113 Style);
8114 verifyFormat(
8115 "SomeClass::Constructor() : // NOLINT\n"
8116 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8117 Style);
8118
8119 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8120 verifyFormat(
8121 "SomeClass::Constructor() :\n"
8122 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8123 Style);
8124 verifyFormat(
8125 "SomeClass::Constructor() : // NOLINT\n"
8126 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8127 Style);
8128
8129 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8130 verifyFormat(
8131 "SomeClass::Constructor() :\n"
8132 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8133 Style);
8134
8135 verifyFormat(
8136 "SomeClass::Constructor() :\n"
8137 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8138 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8139 Style);
8140 verifyFormat(
8141 "SomeClass::Constructor() :\n"
8142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8143 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8144 Style);
8145 verifyFormat(
8146 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8147 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8148 Style);
8149
8150 verifyFormat("Constructor() :\n"
8151 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8152 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8153 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8154 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8155 Style);
8156
8157 verifyFormat("Constructor() :\n"
8158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8160 Style);
8161
8162 verifyFormat("Constructor(int Parameter = 0) :\n"
8163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8164 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8165 Style);
8166 verifyFormat("Constructor() :\n"
8167 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8168 "}",
8169 getStyleWithColumns(Style, 60));
8170 verifyFormat("Constructor() :\n"
8171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8172 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8173 Style);
8174
8175 // Here a line could be saved by splitting the second initializer onto two
8176 // lines, but that is not desirable.
8177 verifyFormat("Constructor() :\n"
8178 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8179 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8180 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8181 Style);
8182
8183 FormatStyle OnePerLine = Style;
8184 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8185 verifyFormat("SomeClass::Constructor() :\n"
8186 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8187 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8189 OnePerLine);
8190 verifyFormat("SomeClass::Constructor() :\n"
8191 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8192 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8193 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8194 OnePerLine);
8195 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8196 " i(i), // comment\n"
8197 " j(j) {}",
8198 OnePerLine);
8199 verifyFormat("MyClass::MyClass(int var) :\n"
8200 " some_var_(var), // 4 space indent\n"
8201 " some_other_var_(var + 1) { // lined up\n"
8202 "}",
8203 OnePerLine);
8204 verifyFormat("Constructor() :\n"
8205 " aaaaa(aaaaaa),\n"
8206 " aaaaa(aaaaaa),\n"
8207 " aaaaa(aaaaaa),\n"
8208 " aaaaa(aaaaaa),\n"
8209 " aaaaa(aaaaaa) {}",
8210 OnePerLine);
8211 verifyFormat("Constructor() :\n"
8212 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8213 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8214 OnePerLine);
8215 OnePerLine.BinPackParameters = false;
8216 verifyFormat("Constructor() :\n"
8217 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8218 " aaaaaaaaaaa().aaa(),\n"
8219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8220 OnePerLine);
8221 OnePerLine.ColumnLimit = 60;
8222 verifyFormat("Constructor() :\n"
8223 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8224 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8225 OnePerLine);
8226
8227 verifyFormat("Constructor() :\n"
8228 " // Comment forcing unwanted break.\n"
8229 " aaaa(aaaa) {}",
8230 Style);
8231 verifyFormat("Constructor() : // NOLINT\n"
8232 " aaaa(aaaa) {}",
8233 Style);
8234 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8235 " on a single\n"
8236 " // line.\n"
8237 " aaaa(aaaa) {}",
8238 "Constructor() : // A very long trailing comment that cannot fit"
8239 " on a single line.\n"
8240 " aaaa(aaaa) {}",
8241 Style);
8242
8243 Style.ColumnLimit = 0;
8244 verifyFormat("SomeClass::Constructor() :\n"
8245 " a(a) {}",
8246 Style);
8247 verifyFormat("SomeClass::Constructor() noexcept :\n"
8248 " a(a) {}",
8249 Style);
8250 verifyFormat("SomeClass::Constructor() :\n"
8251 " a(a), b(b), c(c) {}",
8252 Style);
8253 verifyFormat("SomeClass::Constructor() :\n"
8254 " a(a) {\n"
8255 " foo();\n"
8256 " bar();\n"
8257 "}",
8258 Style);
8259
8260 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8261 verifyFormat("SomeClass::Constructor() :\n"
8262 " a(a), b(b), c(c) {\n"
8263 "}",
8264 Style);
8265 verifyFormat("SomeClass::Constructor() :\n"
8266 " a(a) {\n"
8267 "}",
8268 Style);
8269
8270 Style.ColumnLimit = 80;
8271 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8272 Style.ConstructorInitializerIndentWidth = 2;
8273 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8274 verifyFormat("SomeClass::Constructor() :\n"
8275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8276 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8277 Style);
8278
8279 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8280 // well
8281 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8282 verifyFormat(
8283 "class SomeClass\n"
8284 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8285 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8286 Style);
8287 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8288 verifyFormat(
8289 "class SomeClass\n"
8290 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8291 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8292 Style);
8293 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8294 verifyFormat(
8295 "class SomeClass :\n"
8296 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8297 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8298 Style);
8299 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8300 verifyFormat(
8301 "class SomeClass\n"
8302 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8303 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8304 Style);
8305}
8306
8307#ifndef EXPENSIVE_CHECKS
8308// Expensive checks enables libstdc++ checking which includes validating the
8309// state of ranges used in std::priority_queue - this blows out the
8310// runtime/scalability of the function and makes this test unacceptably slow.
8311TEST_F(FormatTest, MemoizationTests) {
8312 // This breaks if the memoization lookup does not take \c Indent and
8313 // \c LastSpace into account.
8314 verifyFormat(
8315 "extern CFRunLoopTimerRef\n"
8316 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8317 " CFTimeInterval interval, CFOptionFlags flags,\n"
8318 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8319 " CFRunLoopTimerContext *context) {}");
8320
8321 // Deep nesting somewhat works around our memoization.
8322 verifyFormat(
8323 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8324 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8325 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8326 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8327 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8328 getLLVMStyleWithColumns(65));
8329 verifyFormat(
8330 "aaaaa(\n"
8331 " aaaaa,\n"
8332 " aaaaa(\n"
8333 " aaaaa,\n"
8334 " aaaaa(\n"
8335 " aaaaa,\n"
8336 " aaaaa(\n"
8337 " aaaaa,\n"
8338 " aaaaa(\n"
8339 " aaaaa,\n"
8340 " aaaaa(\n"
8341 " aaaaa,\n"
8342 " aaaaa(\n"
8343 " aaaaa,\n"
8344 " aaaaa(\n"
8345 " aaaaa,\n"
8346 " aaaaa(\n"
8347 " aaaaa,\n"
8348 " aaaaa(\n"
8349 " aaaaa,\n"
8350 " aaaaa(\n"
8351 " aaaaa,\n"
8352 " aaaaa(\n"
8353 " aaaaa,\n"
8354 " aaaaa))))))))))));",
8355 getLLVMStyleWithColumns(65));
8356 verifyFormat(
8357 "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"
8358 " a),\n"
8359 " a),\n"
8360 " a),\n"
8361 " a),\n"
8362 " a),\n"
8363 " a),\n"
8364 " a),\n"
8365 " a),\n"
8366 " a),\n"
8367 " a),\n"
8368 " a),\n"
8369 " a),\n"
8370 " a),\n"
8371 " a),\n"
8372 " a),\n"
8373 " a),\n"
8374 " a)",
8375 getLLVMStyleWithColumns(65));
8376
8377 // This test takes VERY long when memoization is broken.
8378 FormatStyle OnePerLine = getLLVMStyle();
8379 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8380 OnePerLine.BinPackParameters = false;
8381 std::string input = "Constructor()\n"
8382 " : aaaa(a,\n";
8383 for (unsigned i = 0, e = 80; i != e; ++i)
8384 input += " a,\n";
8385 input += " a) {}";
8386 verifyFormat(input, OnePerLine);
8387 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8388 verifyFormat(input, OnePerLine);
8389}
8390#endif
8391
8392TEST_F(FormatTest, BreaksAsHighAsPossible) {
8393 verifyFormat(
8394 "void f() {\n"
8395 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8396 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8397 " f();\n"
8398 "}");
8399 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8400 " Intervals[i - 1].getRange().getLast()) {\n}");
8401}
8402
8403TEST_F(FormatTest, BreaksFunctionDeclarations) {
8404 // Principially, we break function declarations in a certain order:
8405 // 1) break amongst arguments.
8406 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8407 " Cccccccccccccc cccccccccccccc);");
8408 verifyFormat("template <class TemplateIt>\n"
8409 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8410 " TemplateIt *stop) {}");
8411
8412 // 2) break after return type.
8413 verifyGoogleFormat(
8414 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8415 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8416
8417 // 3) break after (.
8418 verifyGoogleFormat(
8419 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8420 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8421
8422 // 4) break before after nested name specifiers.
8423 verifyGoogleFormat(
8424 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8425 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8426 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8427
8428 // However, there are exceptions, if a sufficient amount of lines can be
8429 // saved.
8430 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8431 // more adjusting.
8432 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8433 " Cccccccccccccc cccccccccc,\n"
8434 " Cccccccccccccc cccccccccc,\n"
8435 " Cccccccccccccc cccccccccc,\n"
8436 " Cccccccccccccc cccccccccc);");
8437 verifyGoogleFormat(
8438 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8439 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8440 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8441 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8442 verifyFormat(
8443 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8444 " Cccccccccccccc cccccccccc,\n"
8445 " Cccccccccccccc cccccccccc,\n"
8446 " Cccccccccccccc cccccccccc,\n"
8447 " Cccccccccccccc cccccccccc,\n"
8448 " Cccccccccccccc cccccccccc,\n"
8449 " Cccccccccccccc cccccccccc);");
8450 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8451 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8452 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8453 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8454 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8455
8456 // Break after multi-line parameters.
8457 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8460 " bbbb bbbb);");
8461 verifyFormat("void SomeLoooooooooooongFunction(\n"
8462 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8463 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8464 " int bbbbbbbbbbbbb);");
8465
8466 // Treat overloaded operators like other functions.
8467 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8468 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8469 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8470 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8471 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8472 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8473 verifyGoogleFormat(
8474 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8475 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8476 verifyGoogleFormat(
8477 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8478 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8479 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8480 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8481 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8482 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8483 verifyGoogleFormat(
8484 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8485 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8486 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8487 verifyGoogleFormat("template <typename T>\n"
8488 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8489 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8490 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8491
8492 FormatStyle Style = getLLVMStyle();
8493 Style.PointerAlignment = FormatStyle::PAS_Left;
8494 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8495 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8496 Style);
8497 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8499 Style);
8500}
8501
8502TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8503 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8504 // Prefer keeping `::` followed by `operator` together.
8505 verifyFormat("const aaaa::bbbbbbb &\n"
8506 "ccccccccc::operator++() {\n"
8507 " stuff();\n"
8508 "}",
8509 "const aaaa::bbbbbbb\n"
8510 "&ccccccccc::operator++() { stuff(); }",
8511 getLLVMStyleWithColumns(40));
8512}
8513
8514TEST_F(FormatTest, TrailingReturnType) {
8515 verifyFormat("auto foo() -> int;");
8516 // correct trailing return type spacing
8517 verifyFormat("auto operator->() -> int;");
8518 verifyFormat("auto operator++(int) -> int;");
8519
8520 verifyFormat("struct S {\n"
8521 " auto bar() const -> int;\n"
8522 "};");
8523 verifyFormat("template <size_t Order, typename T>\n"
8524 "auto load_img(const std::string &filename)\n"
8525 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8526 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8527 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8528 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8529 verifyFormat("template <typename T>\n"
8530 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8531 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8532
8533 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
8534 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8535 " auto NAME() -> int { return 42; }",
8536 Style);
8537
8538 // Not trailing return types.
8539 verifyFormat("void f() { auto a = b->c(); }");
8540 verifyFormat("auto a = p->foo();");
8541 verifyFormat("int a = p->foo();");
8542 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8543}
8544
8545TEST_F(FormatTest, DeductionGuides) {
8546 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8547 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8548 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8549 verifyFormat(
8550 "template <class... T>\n"
8551 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8552 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8553 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8554 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8555 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8556 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8557 verifyFormat("template <class T> x() -> x<1>;");
8558 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8559
8560 verifyFormat("A(const char *) -> A<string &>;");
8561 verifyFormat("A() -> A<int>;");
8562
8563 // Ensure not deduction guides.
8564 verifyFormat("c()->f<int>();");
8565 verifyFormat("x()->foo<1>;");
8566 verifyFormat("x = p->foo<3>();");
8567 verifyFormat("x()->x<1>();");
8568}
8569
8570TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8571 // Avoid breaking before trailing 'const' or other trailing annotations, if
8572 // they are not function-like.
8573 FormatStyle Style = getGoogleStyleWithColumns(ColumnLimit: 47);
8574 verifyFormat("void someLongFunction(\n"
8575 " int someLoooooooooooooongParameter) const {\n}",
8576 getLLVMStyleWithColumns(47));
8577 verifyFormat("LoooooongReturnType\n"
8578 "someLoooooooongFunction() const {}",
8579 getLLVMStyleWithColumns(47));
8580 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8581 " const {}",
8582 Style);
8583 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8584 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8585 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8586 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8587 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8588 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8589 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8590 " aaaaaaaaaaa aaaaa) const override;");
8591 verifyGoogleFormat(
8592 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8593 " const override;");
8594
8595 // Even if the first parameter has to be wrapped.
8596 verifyFormat("void someLongFunction(\n"
8597 " int someLongParameter) const {}",
8598 getLLVMStyleWithColumns(46));
8599 verifyFormat("void someLongFunction(\n"
8600 " int someLongParameter) const {}",
8601 Style);
8602 verifyFormat("void someLongFunction(\n"
8603 " int someLongParameter) override {}",
8604 Style);
8605 verifyFormat("void someLongFunction(\n"
8606 " int someLongParameter) OVERRIDE {}",
8607 Style);
8608 verifyFormat("void someLongFunction(\n"
8609 " int someLongParameter) final {}",
8610 Style);
8611 verifyFormat("void someLongFunction(\n"
8612 " int someLongParameter) FINAL {}",
8613 Style);
8614 verifyFormat("void someLongFunction(\n"
8615 " int parameter) const override {}",
8616 Style);
8617
8618 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8619 verifyFormat("void someLongFunction(\n"
8620 " int someLongParameter) const\n"
8621 "{\n"
8622 "}",
8623 Style);
8624
8625 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8626 verifyFormat("void someLongFunction(\n"
8627 " int someLongParameter) const\n"
8628 " {\n"
8629 " }",
8630 Style);
8631
8632 // Unless these are unknown annotations.
8633 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8634 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8635 " LONG_AND_UGLY_ANNOTATION;");
8636
8637 // Breaking before function-like trailing annotations is fine to keep them
8638 // close to their arguments.
8639 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8640 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8641 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8642 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8643 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8644 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8645 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8646 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8647 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8648
8649 verifyFormat(
8650 "void aaaaaaaaaaaaaaaaaa()\n"
8651 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8652 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8653 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8654 " __attribute__((unused));");
8655
8656 Style = getGoogleStyle();
8657
8658 verifyFormat(
8659 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8660 " GUARDED_BY(aaaaaaaaaaaa);",
8661 Style);
8662 verifyFormat(
8663 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8664 " GUARDED_BY(aaaaaaaaaaaa);",
8665 Style);
8666 verifyFormat(
8667 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8668 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8669 Style);
8670 verifyFormat(
8671 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8672 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8673 Style);
8674
8675 verifyFormat(
8676 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8677 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8678 Style);
8679 verifyFormat(
8680 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8681 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8682 Style);
8683 verifyFormat(
8684 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8685 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8686 Style);
8687 verifyFormat(
8688 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8689 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8690 Style);
8691}
8692
8693TEST_F(FormatTest, FunctionAnnotations) {
8694 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8695 "int OldFunction(const string &parameter) {}");
8696 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8697 "string OldFunction(const string &parameter) {}");
8698 verifyFormat("template <typename T>\n"
8699 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8700 "string OldFunction(const string &parameter) {}");
8701
8702 // Not function annotations.
8703 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8704 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8705 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8706 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8707 verifyFormat("MACRO(abc).function() // wrap\n"
8708 " << abc;");
8709 verifyFormat("MACRO(abc)->function() // wrap\n"
8710 " << abc;");
8711 verifyFormat("MACRO(abc)::function() // wrap\n"
8712 " << abc;");
8713}
8714
8715TEST_F(FormatTest, BreaksDesireably) {
8716 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8717 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8718 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8719 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8721 "}");
8722
8723 verifyFormat(
8724 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8726
8727 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8730
8731 verifyFormat(
8732 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8733 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8735 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8737
8738 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8739 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8740
8741 verifyFormat(
8742 "void f() {\n"
8743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8745 "}");
8746 verifyFormat(
8747 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8749 verifyFormat(
8750 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8752 verifyFormat(
8753 "aaaaaa(aaa,\n"
8754 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8755 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8756 " aaaa);");
8757 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8760
8761 // Indent consistently independent of call expression and unary operator.
8762 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8763 " dddddddddddddddddddddddddddddd));");
8764 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8765 " dddddddddddddddddddddddddddddd));");
8766 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8767 " dddddddddddddddddddddddddddddd));");
8768
8769 // This test case breaks on an incorrect memoization, i.e. an optimization not
8770 // taking into account the StopAt value.
8771 verifyFormat(
8772 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8773 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8774 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8775 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8776
8777 verifyFormat("{\n {\n {\n"
8778 " Annotation.SpaceRequiredBefore =\n"
8779 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8780 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8781 " }\n }\n}");
8782
8783 // Break on an outer level if there was a break on an inner level.
8784 verifyFormat("f(g(h(a, // comment\n"
8785 " b, c),\n"
8786 " d, e),\n"
8787 " x, y);",
8788 "f(g(h(a, // comment\n"
8789 " b, c), d, e), x, y);");
8790
8791 // Prefer breaking similar line breaks.
8792 verifyFormat(
8793 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8794 " NSTrackingMouseEnteredAndExited |\n"
8795 " NSTrackingActiveAlways;");
8796}
8797
8798TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8799 FormatStyle NoBinPacking = getGoogleStyle();
8800 NoBinPacking.BinPackParameters = false;
8801 NoBinPacking.BinPackArguments = true;
8802 verifyFormat("void f() {\n"
8803 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8804 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8805 "}",
8806 NoBinPacking);
8807 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8808 " int aaaaaaaaaaaaaaaaaaaa,\n"
8809 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8810 NoBinPacking);
8811
8812 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8813 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8814 " vector<int> bbbbbbbbbbbbbbb);",
8815 NoBinPacking);
8816 // FIXME: This behavior difference is probably not wanted. However, currently
8817 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8818 // template arguments from BreakBeforeParameter being set because of the
8819 // one-per-line formatting.
8820 verifyFormat(
8821 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8822 " aaaaaaaaaa> aaaaaaaaaa);",
8823 NoBinPacking);
8824 verifyFormat(
8825 "void fffffffffff(\n"
8826 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8827 " aaaaaaaaaa);");
8828}
8829
8830TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8831 FormatStyle NoBinPacking = getGoogleStyle();
8832 NoBinPacking.BinPackParameters = false;
8833 NoBinPacking.BinPackArguments = false;
8834 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8835 " aaaaaaaaaaaaaaaaaaaa,\n"
8836 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8837 NoBinPacking);
8838 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8839 " aaaaaaaaaaaaa,\n"
8840 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8841 NoBinPacking);
8842 verifyFormat(
8843 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8844 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8846 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8848 NoBinPacking);
8849 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8850 " .aaaaaaaaaaaaaaaaaa();",
8851 NoBinPacking);
8852 verifyFormat("void f() {\n"
8853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8854 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8855 "}",
8856 NoBinPacking);
8857
8858 verifyFormat(
8859 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8860 " aaaaaaaaaaaa,\n"
8861 " aaaaaaaaaaaa);",
8862 NoBinPacking);
8863 verifyFormat(
8864 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8865 " ddddddddddddddddddddddddddddd),\n"
8866 " test);",
8867 NoBinPacking);
8868
8869 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8870 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8871 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8872 " aaaaaaaaaaaaaaaaaa;",
8873 NoBinPacking);
8874 verifyFormat("a(\"a\"\n"
8875 " \"a\",\n"
8876 " a);");
8877
8878 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8879 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8880 " aaaaaaaaa,\n"
8881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8882 NoBinPacking);
8883 verifyFormat(
8884 "void f() {\n"
8885 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8886 " .aaaaaaa();\n"
8887 "}",
8888 NoBinPacking);
8889 verifyFormat(
8890 "template <class SomeType, class SomeOtherType>\n"
8891 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8892 NoBinPacking);
8893}
8894
8895TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
8896 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 15);
8897 Style.ExperimentalAutoDetectBinPacking = true;
8898 verifyFormat("aaa(aaaa,\n"
8899 " aaaa,\n"
8900 " aaaa);\n"
8901 "aaa(aaaa,\n"
8902 " aaaa,\n"
8903 " aaaa);",
8904 "aaa(aaaa,\n" // one-per-line
8905 " aaaa,\n"
8906 " aaaa );\n"
8907 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8908 Style);
8909 verifyFormat("aaa(aaaa, aaaa,\n"
8910 " aaaa);\n"
8911 "aaa(aaaa, aaaa,\n"
8912 " aaaa);",
8913 "aaa(aaaa, aaaa,\n" // bin-packed
8914 " aaaa );\n"
8915 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8916 Style);
8917}
8918
8919TEST_F(FormatTest, FormatsBuilderPattern) {
8920 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
8921 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
8922 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
8923 " .StartsWith(\".init\", ORDER_INIT)\n"
8924 " .StartsWith(\".fini\", ORDER_FINI)\n"
8925 " .StartsWith(\".hash\", ORDER_HASH)\n"
8926 " .Default(ORDER_TEXT);");
8927
8928 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
8929 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
8930 verifyFormat("aaaaaaa->aaaaaaa\n"
8931 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8933 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8934 verifyFormat(
8935 "aaaaaaa->aaaaaaa\n"
8936 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8937 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8938 verifyFormat(
8939 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
8940 " aaaaaaaaaaaaaa);");
8941 verifyFormat(
8942 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
8943 " aaaaaa->aaaaaaaaaaaa()\n"
8944 " ->aaaaaaaaaaaaaaaa(\n"
8945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8946 " ->aaaaaaaaaaaaaaaaa();");
8947 verifyGoogleFormat(
8948 "void f() {\n"
8949 " someo->Add((new util::filetools::Handler(dir))\n"
8950 " ->OnEvent1(NewPermanentCallback(\n"
8951 " this, &HandlerHolderClass::EventHandlerCBA))\n"
8952 " ->OnEvent2(NewPermanentCallback(\n"
8953 " this, &HandlerHolderClass::EventHandlerCBB))\n"
8954 " ->OnEvent3(NewPermanentCallback(\n"
8955 " this, &HandlerHolderClass::EventHandlerCBC))\n"
8956 " ->OnEvent5(NewPermanentCallback(\n"
8957 " this, &HandlerHolderClass::EventHandlerCBD))\n"
8958 " ->OnEvent6(NewPermanentCallback(\n"
8959 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
8960 "}");
8961
8962 verifyFormat(
8963 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
8964 verifyFormat("aaaaaaaaaaaaaaa()\n"
8965 " .aaaaaaaaaaaaaaa()\n"
8966 " .aaaaaaaaaaaaaaa()\n"
8967 " .aaaaaaaaaaaaaaa()\n"
8968 " .aaaaaaaaaaaaaaa();");
8969 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8970 " .aaaaaaaaaaaaaaa()\n"
8971 " .aaaaaaaaaaaaaaa()\n"
8972 " .aaaaaaaaaaaaaaa();");
8973 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8974 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8975 " .aaaaaaaaaaaaaaa();");
8976 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
8977 " ->aaaaaaaaaaaaaae(0)\n"
8978 " ->aaaaaaaaaaaaaaa();");
8979
8980 // Don't linewrap after very short segments.
8981 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8982 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8983 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8984 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8985 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8986 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8987 verifyFormat("aaa()\n"
8988 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8989 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8990 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8991
8992 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8993 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8994 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
8995 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8996 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
8997 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
8998
8999 // Prefer not to break after empty parentheses.
9000 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9001 " First->LastNewlineOffset);");
9002
9003 // Prefer not to create "hanging" indents.
9004 verifyFormat(
9005 "return !soooooooooooooome_map\n"
9006 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9007 " .second;");
9008 verifyFormat(
9009 "return aaaaaaaaaaaaaaaa\n"
9010 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9011 " .aaaa(aaaaaaaaaaaaaa);");
9012 // No hanging indent here.
9013 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9015 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9017 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9018 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9019 getLLVMStyleWithColumns(60));
9020 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9021 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9022 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9023 getLLVMStyleWithColumns(59));
9024 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9026 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9027
9028 // Dont break if only closing statements before member call
9029 verifyFormat("test() {\n"
9030 " ([]() -> {\n"
9031 " int b = 32;\n"
9032 " return 3;\n"
9033 " }).foo();\n"
9034 "}");
9035 verifyFormat("test() {\n"
9036 " (\n"
9037 " []() -> {\n"
9038 " int b = 32;\n"
9039 " return 3;\n"
9040 " },\n"
9041 " foo, bar)\n"
9042 " .foo();\n"
9043 "}");
9044 verifyFormat("test() {\n"
9045 " ([]() -> {\n"
9046 " int b = 32;\n"
9047 " return 3;\n"
9048 " })\n"
9049 " .foo()\n"
9050 " .bar();\n"
9051 "}");
9052 verifyFormat("test() {\n"
9053 " ([]() -> {\n"
9054 " int b = 32;\n"
9055 " return 3;\n"
9056 " })\n"
9057 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9058 " \"bbbb\");\n"
9059 "}",
9060 getLLVMStyleWithColumns(30));
9061}
9062
9063TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9064 verifyFormat(
9065 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9066 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9067 verifyFormat(
9068 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9069 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9070
9071 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9072 " ccccccccccccccccccccccccc) {\n}");
9073 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9074 " ccccccccccccccccccccccccc) {\n}");
9075
9076 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9077 " ccccccccccccccccccccccccc) {\n}");
9078 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9079 " ccccccccccccccccccccccccc) {\n}");
9080
9081 verifyFormat(
9082 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9083 " ccccccccccccccccccccccccc) {\n}");
9084 verifyFormat(
9085 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9086 " ccccccccccccccccccccccccc) {\n}");
9087
9088 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9089 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9090 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9091 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9092 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9093 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9094 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9095 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9096
9097 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9098 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9099 " aaaaaaaaaaaaaaa != aa) {\n}");
9100 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9101 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9102 " aaaaaaaaaaaaaaa != aa) {\n}");
9103}
9104
9105TEST_F(FormatTest, BreaksAfterAssignments) {
9106 verifyFormat(
9107 "unsigned Cost =\n"
9108 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9109 " SI->getPointerAddressSpaceee());");
9110 verifyFormat(
9111 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9112 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9113
9114 verifyFormat(
9115 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9116 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9117 verifyFormat("unsigned OriginalStartColumn =\n"
9118 " SourceMgr.getSpellingColumnNumber(\n"
9119 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9120 " 1;");
9121}
9122
9123TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9124 FormatStyle Style = getLLVMStyle();
9125 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9126 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9127 Style);
9128
9129 Style.PenaltyBreakAssignment = 20;
9130 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9131 " cccccccccccccccccccccccccc;",
9132 Style);
9133}
9134
9135TEST_F(FormatTest, AlignsAfterAssignments) {
9136 verifyFormat(
9137 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9138 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9139 verifyFormat(
9140 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9141 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9142 verifyFormat(
9143 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9144 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9145 verifyFormat(
9146 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9147 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9148 verifyFormat(
9149 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9150 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9151 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9152}
9153
9154TEST_F(FormatTest, AlignsAfterReturn) {
9155 verifyFormat(
9156 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9157 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9158 verifyFormat(
9159 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9160 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9161 verifyFormat(
9162 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9163 " aaaaaaaaaaaaaaaaaaaaaa();");
9164 verifyFormat(
9165 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9166 " aaaaaaaaaaaaaaaaaaaaaa());");
9167 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9169 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9172 verifyFormat("return\n"
9173 " // true if code is one of a or b.\n"
9174 " code == a || code == b;");
9175}
9176
9177TEST_F(FormatTest, AlignsAfterOpenBracket) {
9178 verifyFormat(
9179 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9180 " aaaaaaaaa aaaaaaa) {}");
9181 verifyFormat(
9182 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9183 " aaaaaaaaaaa aaaaaaaaa);");
9184 verifyFormat(
9185 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9186 " aaaaaaaaaaaaaaaaaaaaa));");
9187 FormatStyle Style = getLLVMStyle();
9188 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9189 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9190 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9191 Style);
9192 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9193 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9194 Style);
9195 verifyFormat("SomeLongVariableName->someFunction(\n"
9196 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9197 Style);
9198 verifyFormat(
9199 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9200 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9201 Style);
9202 verifyFormat(
9203 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9204 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9205 Style);
9206 verifyFormat(
9207 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9208 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9209 Style);
9210
9211 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9212 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9213 " b));",
9214 Style);
9215
9216 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9217 Style.BinPackArguments = false;
9218 Style.BinPackParameters = false;
9219 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9220 " aaaaaaaaaaa aaaaaaaa,\n"
9221 " aaaaaaaaa aaaaaaa,\n"
9222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9223 Style);
9224 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9225 " aaaaaaaaaaa aaaaaaaaa,\n"
9226 " aaaaaaaaaaa aaaaaaaaa,\n"
9227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9228 Style);
9229 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9230 " aaaaaaaaaaaaaaa,\n"
9231 " aaaaaaaaaaaaaaaaaaaaa,\n"
9232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9233 Style);
9234 verifyFormat(
9235 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9236 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9237 Style);
9238 verifyFormat(
9239 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9240 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9241 Style);
9242 verifyFormat(
9243 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9244 " aaaaaaaaaaaaaaaaaaaaa(\n"
9245 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9246 " aaaaaaaaaaaaaaaa);",
9247 Style);
9248 verifyFormat(
9249 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9250 " aaaaaaaaaaaaaaaaaaaaa(\n"
9251 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9252 " aaaaaaaaaaaaaaaa);",
9253 Style);
9254
9255 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9256 Style.BinPackArguments = false;
9257 Style.BinPackParameters = false;
9258 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9259 " aaaaaaaaaaa aaaaaaaa,\n"
9260 " aaaaaaaaa aaaaaaa,\n"
9261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9262 ") {}",
9263 Style);
9264 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9265 " aaaaaaaaaaa aaaaaaaaa,\n"
9266 " aaaaaaaaaaa aaaaaaaaa,\n"
9267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9268 ");",
9269 Style);
9270 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9271 " aaaaaaaaaaaaaaa,\n"
9272 " aaaaaaaaaaaaaaaaaaaaa,\n"
9273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9274 "));",
9275 Style);
9276 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9277 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9278 "));",
9279 Style);
9280 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9281 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9282 "));",
9283 Style);
9284 verifyFormat(
9285 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9286 " aaaaaaaaaaaaaaaaaaaaa(\n"
9287 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9288 " ),\n"
9289 " aaaaaaaaaaaaaaaa\n"
9290 ");",
9291 Style);
9292 verifyFormat(
9293 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9294 " aaaaaaaaaaaaaaaaaaaaa(\n"
9295 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9296 " ) &&\n"
9297 " aaaaaaaaaaaaaaaa\n"
9298 ");",
9299 Style);
9300}
9301
9302TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9303 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
9304 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9305 " bbbbbbbbbbbbbbbbbbbbbb);",
9306 Style);
9307 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9308 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9309 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9310 " bbbbbbbbbbbbbbbbbbbbbb);",
9311 Style);
9312 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9313 Style.AlignOperands = FormatStyle::OAS_Align;
9314 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9315 " bbbbbbbbbbbbbbbbbbbbbb);",
9316 Style);
9317 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9318 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9319 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9320 " bbbbbbbbbbbbbbbbbbbbbb);",
9321 Style);
9322}
9323
9324TEST_F(FormatTest, BreaksConditionalExpressions) {
9325 verifyFormat(
9326 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9327 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9328 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9329 verifyFormat(
9330 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9331 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9332 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9333 verifyFormat(
9334 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9335 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9336 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9337 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9338 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9339 verifyFormat(
9340 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9341 " : aaaaaaaaaaaaa);");
9342 verifyFormat(
9343 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9344 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9345 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9346 " aaaaaaaaaaaaa);");
9347 verifyFormat(
9348 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9349 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9350 " aaaaaaaaaaaaa);");
9351 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9352 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9354 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9356 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9358 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9360 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9362 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9363 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9365 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9367 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9368 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9369 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9370 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9371 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9373 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9374 " : aaaaaaaaaaaaaaaa;");
9375 verifyFormat(
9376 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9377 " ? aaaaaaaaaaaaaaa\n"
9378 " : aaaaaaaaaaaaaaa;");
9379 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9380 " aaaaaaaaa\n"
9381 " ? b\n"
9382 " : c);");
9383 verifyFormat("return aaaa == bbbb\n"
9384 " // comment\n"
9385 " ? aaaa\n"
9386 " : bbbb;");
9387 verifyFormat("unsigned Indent =\n"
9388 " format(TheLine.First,\n"
9389 " IndentForLevel[TheLine.Level] >= 0\n"
9390 " ? IndentForLevel[TheLine.Level]\n"
9391 " : TheLine * 2,\n"
9392 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9393 getLLVMStyleWithColumns(60));
9394 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9395 " ? aaaaaaaaaaaaaaa\n"
9396 " : bbbbbbbbbbbbbbb //\n"
9397 " ? ccccccccccccccc\n"
9398 " : ddddddddddddddd;");
9399 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9400 " ? aaaaaaaaaaaaaaa\n"
9401 " : (bbbbbbbbbbbbbbb //\n"
9402 " ? ccccccccccccccc\n"
9403 " : ddddddddddddddd);");
9404 verifyFormat(
9405 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9406 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9407 " aaaaaaaaaaaaaaaaaaaaa +\n"
9408 " aaaaaaaaaaaaaaaaaaaaa\n"
9409 " : aaaaaaaaaa;");
9410 verifyFormat(
9411 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9412 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9413 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9414
9415 FormatStyle NoBinPacking = getLLVMStyle();
9416 NoBinPacking.BinPackArguments = false;
9417 verifyFormat(
9418 "void f() {\n"
9419 " g(aaa,\n"
9420 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9422 " ? aaaaaaaaaaaaaaa\n"
9423 " : aaaaaaaaaaaaaaa);\n"
9424 "}",
9425 NoBinPacking);
9426 verifyFormat(
9427 "void f() {\n"
9428 " g(aaa,\n"
9429 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9431 " ?: aaaaaaaaaaaaaaa);\n"
9432 "}",
9433 NoBinPacking);
9434
9435 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9436 " // comment.\n"
9437 " ccccccccccccccccccccccccccccccccccccccc\n"
9438 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9439 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9440
9441 // Assignments in conditional expressions. Apparently not uncommon :-(.
9442 verifyFormat("return a != b\n"
9443 " // comment\n"
9444 " ? a = b\n"
9445 " : a = b;");
9446 verifyFormat("return a != b\n"
9447 " // comment\n"
9448 " ? a = a != b\n"
9449 " // comment\n"
9450 " ? a = b\n"
9451 " : a\n"
9452 " : a;");
9453 verifyFormat("return a != b\n"
9454 " // comment\n"
9455 " ? a\n"
9456 " : a = a != b\n"
9457 " // comment\n"
9458 " ? a = b\n"
9459 " : a;");
9460
9461 // Chained conditionals
9462 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 70);
9463 Style.AlignOperands = FormatStyle::OAS_Align;
9464 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9465 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9466 " : 3333333333333333;",
9467 Style);
9468 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9469 " : bbbbbbbbbb ? 2222222222222222\n"
9470 " : 3333333333333333;",
9471 Style);
9472 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9473 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9474 " : 3333333333333333;",
9475 Style);
9476 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9477 " : bbbbbbbbbbbbbb ? 222222\n"
9478 " : 333333;",
9479 Style);
9480 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9481 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9482 " : cccccccccccccc ? 3333333333333333\n"
9483 " : 4444444444444444;",
9484 Style);
9485 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9486 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9487 " : 3333333333333333;",
9488 Style);
9489 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9490 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9491 " : (aaa ? bbb : ccc);",
9492 Style);
9493 verifyFormat(
9494 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9495 " : cccccccccccccccccc)\n"
9496 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9497 " : 3333333333333333;",
9498 Style);
9499 verifyFormat(
9500 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9501 " : cccccccccccccccccc)\n"
9502 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9503 " : 3333333333333333;",
9504 Style);
9505 verifyFormat(
9506 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9507 " : dddddddddddddddddd)\n"
9508 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9509 " : 3333333333333333;",
9510 Style);
9511 verifyFormat(
9512 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9513 " : dddddddddddddddddd)\n"
9514 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9515 " : 3333333333333333;",
9516 Style);
9517 verifyFormat(
9518 "return aaaaaaaaa ? 1111111111111111\n"
9519 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9520 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9521 " : dddddddddddddddddd)",
9522 Style);
9523 verifyFormat(
9524 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9525 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9526 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9527 " : cccccccccccccccccc);",
9528 Style);
9529 verifyFormat(
9530 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9531 " : ccccccccccccccc ? dddddddddddddddddd\n"
9532 " : eeeeeeeeeeeeeeeeee)\n"
9533 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9534 " : 3333333333333333;",
9535 Style);
9536 verifyFormat(
9537 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9538 " : ccccccccccccccc ? dddddddddddddddddd\n"
9539 " : eeeeeeeeeeeeeeeeee)\n"
9540 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9541 " : 3333333333333333;",
9542 Style);
9543 verifyFormat(
9544 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9545 " : cccccccccccc ? dddddddddddddddddd\n"
9546 " : eeeeeeeeeeeeeeeeee)\n"
9547 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9548 " : 3333333333333333;",
9549 Style);
9550 verifyFormat(
9551 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9552 " : cccccccccccccccccc\n"
9553 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9554 " : 3333333333333333;",
9555 Style);
9556 verifyFormat(
9557 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9558 " : cccccccccccccccc ? dddddddddddddddddd\n"
9559 " : eeeeeeeeeeeeeeeeee\n"
9560 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9561 " : 3333333333333333;",
9562 Style);
9563 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9564 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9565 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9566 " : eeeeeeeeeeeeeeeeee)\n"
9567 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9568 " : 3333333333333333;",
9569 Style);
9570 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9571 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9572 " : cccccccccccccccc ? dddddddddddddddddd\n"
9573 " : eeeeeeeeeeeeeeeeee\n"
9574 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9575 " : 3333333333333333;",
9576 Style);
9577
9578 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9579 Style.BreakBeforeTernaryOperators = false;
9580 // FIXME: Aligning the question marks is weird given DontAlign.
9581 // Consider disabling this alignment in this case. Also check whether this
9582 // will render the adjustment from https://reviews.llvm.org/D82199
9583 // unnecessary.
9584 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9585 " bbbb ? cccccccccccccccccc :\n"
9586 " ddddd;",
9587 Style);
9588
9589 verifyFormat(
9590 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9591 " /*\n"
9592 " */\n"
9593 " function() {\n"
9594 " try {\n"
9595 " return JJJJJJJJJJJJJJ(\n"
9596 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9597 " }\n"
9598 " } :\n"
9599 " function() {};",
9600 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9601 " /*\n"
9602 " */\n"
9603 " function() {\n"
9604 " try {\n"
9605 " return JJJJJJJJJJJJJJ(\n"
9606 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9607 " }\n"
9608 " } :\n"
9609 " function() {};",
9610 getGoogleStyle(FormatStyle::LK_JavaScript));
9611}
9612
9613TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
9614 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 70);
9615 Style.BreakBeforeTernaryOperators = false;
9616 verifyFormat(
9617 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9620 Style);
9621 verifyFormat(
9622 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9623 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9625 Style);
9626 verifyFormat(
9627 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9629 Style);
9630 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9631 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9633 Style);
9634 verifyFormat(
9635 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9636 " aaaaaaaaaaaaa);",
9637 Style);
9638 verifyFormat(
9639 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9640 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9642 " aaaaaaaaaaaaa);",
9643 Style);
9644 verifyFormat(
9645 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9646 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9647 " aaaaaaaaaaaaa);",
9648 Style);
9649 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9654 Style);
9655 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9661 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9662 Style);
9663 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9667 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9668 Style);
9669 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9670 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9671 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9672 Style);
9673 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9677 Style);
9678 verifyFormat(
9679 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9680 " aaaaaaaaaaaaaaa :\n"
9681 " aaaaaaaaaaaaaaa;",
9682 Style);
9683 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9684 " aaaaaaaaa ?\n"
9685 " b :\n"
9686 " c);",
9687 Style);
9688 verifyFormat("unsigned Indent =\n"
9689 " format(TheLine.First,\n"
9690 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9691 " IndentForLevel[TheLine.Level] :\n"
9692 " TheLine * 2,\n"
9693 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9694 Style);
9695 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9696 " aaaaaaaaaaaaaaa :\n"
9697 " bbbbbbbbbbbbbbb ? //\n"
9698 " ccccccccccccccc :\n"
9699 " ddddddddddddddd;",
9700 Style);
9701 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9702 " aaaaaaaaaaaaaaa :\n"
9703 " (bbbbbbbbbbbbbbb ? //\n"
9704 " ccccccccccccccc :\n"
9705 " ddddddddddddddd);",
9706 Style);
9707 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9708 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9709 " ccccccccccccccccccccccccccc;",
9710 Style);
9711 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9712 " aaaaa :\n"
9713 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9714 Style);
9715
9716 // Chained conditionals
9717 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9718 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9719 " 3333333333333333;",
9720 Style);
9721 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9722 " bbbbbbbbbb ? 2222222222222222 :\n"
9723 " 3333333333333333;",
9724 Style);
9725 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9726 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9727 " 3333333333333333;",
9728 Style);
9729 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9730 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9731 " 333333;",
9732 Style);
9733 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9734 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9735 " cccccccccccccccc ? 3333333333333333 :\n"
9736 " 4444444444444444;",
9737 Style);
9738 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9739 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9740 " 3333333333333333;",
9741 Style);
9742 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9743 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9744 " (aaa ? bbb : ccc);",
9745 Style);
9746 verifyFormat(
9747 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9748 " cccccccccccccccccc) :\n"
9749 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9750 " 3333333333333333;",
9751 Style);
9752 verifyFormat(
9753 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9754 " cccccccccccccccccc) :\n"
9755 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9756 " 3333333333333333;",
9757 Style);
9758 verifyFormat(
9759 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9760 " dddddddddddddddddd) :\n"
9761 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9762 " 3333333333333333;",
9763 Style);
9764 verifyFormat(
9765 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9766 " dddddddddddddddddd) :\n"
9767 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9768 " 3333333333333333;",
9769 Style);
9770 verifyFormat(
9771 "return aaaaaaaaa ? 1111111111111111 :\n"
9772 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9773 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9774 " dddddddddddddddddd)",
9775 Style);
9776 verifyFormat(
9777 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9778 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9779 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9780 " cccccccccccccccccc);",
9781 Style);
9782 verifyFormat(
9783 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9784 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9785 " eeeeeeeeeeeeeeeeee) :\n"
9786 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9787 " 3333333333333333;",
9788 Style);
9789 verifyFormat(
9790 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9791 " ccccccccccccc ? dddddddddddddddddd :\n"
9792 " eeeeeeeeeeeeeeeeee) :\n"
9793 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9794 " 3333333333333333;",
9795 Style);
9796 verifyFormat(
9797 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9798 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9799 " eeeeeeeeeeeeeeeeee) :\n"
9800 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9801 " 3333333333333333;",
9802 Style);
9803 verifyFormat(
9804 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9805 " cccccccccccccccccc :\n"
9806 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9807 " 3333333333333333;",
9808 Style);
9809 verifyFormat(
9810 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9811 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9812 " eeeeeeeeeeeeeeeeee :\n"
9813 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9814 " 3333333333333333;",
9815 Style);
9816 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9817 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9818 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9819 " eeeeeeeeeeeeeeeeee) :\n"
9820 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9821 " 3333333333333333;",
9822 Style);
9823 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9824 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9825 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
9826 " eeeeeeeeeeeeeeeeee :\n"
9827 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9828 " 3333333333333333;",
9829 Style);
9830}
9831
9832TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
9833 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
9834 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
9835 verifyFormat("bool a = true, b = false;");
9836
9837 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
9839 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
9840 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
9841 verifyFormat(
9842 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
9843 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
9844 " d = e && f;");
9845 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
9846 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
9847 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9848 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
9849 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
9850 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
9851
9852 FormatStyle Style = getGoogleStyle();
9853 Style.PointerAlignment = FormatStyle::PAS_Left;
9854 Style.DerivePointerAlignment = false;
9855 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9856 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
9857 " *b = bbbbbbbbbbbbbbbbbbb;",
9858 Style);
9859 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9860 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
9861 Style);
9862 verifyFormat("vector<int*> a, b;", Style);
9863 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
9864 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
9865 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
9866 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
9867 Style);
9868 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
9869 Style);
9870 verifyFormat(
9871 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
9872 Style);
9873
9874 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
9875 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
9876 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
9877 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
9878 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
9879 Style);
9880}
9881
9882TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
9883 verifyFormat("arr[foo ? bar : baz];");
9884 verifyFormat("f()[foo ? bar : baz];");
9885 verifyFormat("(a + b)[foo ? bar : baz];");
9886 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
9887}
9888
9889TEST_F(FormatTest, AlignsStringLiterals) {
9890 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
9891 " \"short literal\");");
9892 verifyFormat(
9893 "looooooooooooooooooooooooongFunction(\n"
9894 " \"short literal\"\n"
9895 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
9896 verifyFormat("someFunction(\"Always break between multi-line\"\n"
9897 " \" string literals\",\n"
9898 " also, other, parameters);");
9899 verifyFormat("fun + \"1243\" /* comment */\n"
9900 " \"5678\";",
9901 "fun + \"1243\" /* comment */\n"
9902 " \"5678\";",
9903 getLLVMStyleWithColumns(28));
9904 verifyFormat(
9905 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
9906 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
9907 " \"aaaaaaaaaaaaaaaa\";",
9908 "aaaaaa ="
9909 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
9910 "aaaaaaaaaaaaaaaaaaaaa\" "
9911 "\"aaaaaaaaaaaaaaaa\";");
9912 verifyFormat("a = a + \"a\"\n"
9913 " \"a\"\n"
9914 " \"a\";");
9915 verifyFormat("f(\"a\", \"b\"\n"
9916 " \"c\");");
9917
9918 verifyFormat(
9919 "#define LL_FORMAT \"ll\"\n"
9920 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
9921 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
9922
9923 verifyFormat("#define A(X) \\\n"
9924 " \"aaaaa\" #X \"bbbbbb\" \\\n"
9925 " \"ccccc\"",
9926 getLLVMStyleWithColumns(23));
9927 verifyFormat("#define A \"def\"\n"
9928 "f(\"abc\" A \"ghi\"\n"
9929 " \"jkl\");");
9930
9931 verifyFormat("f(L\"a\"\n"
9932 " L\"b\");");
9933 verifyFormat("#define A(X) \\\n"
9934 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
9935 " L\"ccccc\"",
9936 getLLVMStyleWithColumns(25));
9937
9938 verifyFormat("f(@\"a\"\n"
9939 " @\"b\");");
9940 verifyFormat("NSString s = @\"a\"\n"
9941 " @\"b\"\n"
9942 " @\"c\";");
9943 verifyFormat("NSString s = @\"a\"\n"
9944 " \"b\"\n"
9945 " \"c\";");
9946}
9947
9948TEST_F(FormatTest, ReturnTypeBreakingStyle) {
9949 FormatStyle Style = getLLVMStyle();
9950 Style.ColumnLimit = 60;
9951
9952 // No declarations or definitions should be moved to own line.
9953 Style.BreakAfterReturnType = FormatStyle::RTBS_None;
9954 verifyFormat("class A {\n"
9955 " int f() { return 1; }\n"
9956 " int g();\n"
9957 " long\n"
9958 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
9959 "};\n"
9960 "int f() { return 1; }\n"
9961 "int g();\n"
9962 "int foooooooooooooooooooooooooooo::\n"
9963 " baaaaaaaaaaaaaaaaaaaaar();",
9964 Style);
9965
9966 // It is now allowed to break after a short return type if necessary.
9967 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
9968 verifyFormat("class A {\n"
9969 " int f() { return 1; }\n"
9970 " int g();\n"
9971 " long\n"
9972 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
9973 "};\n"
9974 "int f() { return 1; }\n"
9975 "int g();\n"
9976 "int\n"
9977 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
9978 Style);
9979
9980 // It now must never break after a short return type.
9981 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
9982 verifyFormat("class A {\n"
9983 " int f() { return 1; }\n"
9984 " int g();\n"
9985 " long foooooooooooooooooooooooooooo::\n"
9986 " baaaaaaaaaaaaaaaaaaaar();\n"
9987 "};\n"
9988 "int f() { return 1; }\n"
9989 "int g();\n"
9990 "int foooooooooooooooooooooooooooo::\n"
9991 " baaaaaaaaaaaaaaaaaaaaar();",
9992 Style);
9993
9994 // All declarations and definitions should have the return type moved to its
9995 // own line.
9996 Style.BreakAfterReturnType = FormatStyle::RTBS_All;
9997 Style.TypenameMacros = {"LIST"};
9998 verifyFormat("SomeType\n"
9999 "funcdecl(LIST(uint64_t));",
10000 Style);
10001 verifyFormat("class E {\n"
10002 " int\n"
10003 " f() {\n"
10004 " return 1;\n"
10005 " }\n"
10006 " int\n"
10007 " g();\n"
10008 " long\n"
10009 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10010 "};\n"
10011 "int\n"
10012 "f() {\n"
10013 " return 1;\n"
10014 "}\n"
10015 "int\n"
10016 "g();\n"
10017 "int\n"
10018 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10019 Style);
10020
10021 // Top-level definitions, and no kinds of declarations should have the
10022 // return type moved to its own line.
10023 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10024 verifyFormat("class B {\n"
10025 " int f() { return 1; }\n"
10026 " int g();\n"
10027 "};\n"
10028 "int\n"
10029 "f() {\n"
10030 " return 1;\n"
10031 "}\n"
10032 "int g();",
10033 Style);
10034
10035 // Top-level definitions and declarations should have the return type moved
10036 // to its own line.
10037 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10038 verifyFormat("class C {\n"
10039 " int f() { return 1; }\n"
10040 " int g();\n"
10041 "};\n"
10042 "int\n"
10043 "f() {\n"
10044 " return 1;\n"
10045 "}\n"
10046 "int\n"
10047 "g();\n"
10048 "int\n"
10049 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10050 Style);
10051
10052 // All definitions should have the return type moved to its own line, but no
10053 // kinds of declarations.
10054 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10055 verifyFormat("class D {\n"
10056 " int\n"
10057 " f() {\n"
10058 " return 1;\n"
10059 " }\n"
10060 " int g();\n"
10061 "};\n"
10062 "int\n"
10063 "f() {\n"
10064 " return 1;\n"
10065 "}\n"
10066 "int g();",
10067 Style);
10068 verifyFormat("const char *\n"
10069 "f(void) {\n" // Break here.
10070 " return \"\";\n"
10071 "}\n"
10072 "const char *bar(void);", // No break here.
10073 Style);
10074 verifyFormat("template <class T>\n"
10075 "T *\n"
10076 "f(T &c) {\n" // Break here.
10077 " return NULL;\n"
10078 "}\n"
10079 "template <class T> T *f(T &c);", // No break here.
10080 Style);
10081 verifyFormat("class C {\n"
10082 " int\n"
10083 " operator+() {\n"
10084 " return 1;\n"
10085 " }\n"
10086 " int\n"
10087 " operator()() {\n"
10088 " return 1;\n"
10089 " }\n"
10090 "};",
10091 Style);
10092 verifyFormat("void\n"
10093 "A::operator()() {}\n"
10094 "void\n"
10095 "A::operator>>() {}\n"
10096 "void\n"
10097 "A::operator+() {}\n"
10098 "void\n"
10099 "A::operator*() {}\n"
10100 "void\n"
10101 "A::operator->() {}\n"
10102 "void\n"
10103 "A::operator void *() {}\n"
10104 "void\n"
10105 "A::operator void &() {}\n"
10106 "void\n"
10107 "A::operator void &&() {}\n"
10108 "void\n"
10109 "A::operator char *() {}\n"
10110 "void\n"
10111 "A::operator[]() {}\n"
10112 "void\n"
10113 "A::operator!() {}\n"
10114 "void\n"
10115 "A::operator**() {}\n"
10116 "void\n"
10117 "A::operator<Foo> *() {}\n"
10118 "void\n"
10119 "A::operator<Foo> **() {}\n"
10120 "void\n"
10121 "A::operator<Foo> &() {}\n"
10122 "void\n"
10123 "A::operator void **() {}",
10124 Style);
10125 verifyFormat("constexpr auto\n"
10126 "operator()() const -> reference {}\n"
10127 "constexpr auto\n"
10128 "operator>>() const -> reference {}\n"
10129 "constexpr auto\n"
10130 "operator+() const -> reference {}\n"
10131 "constexpr auto\n"
10132 "operator*() const -> reference {}\n"
10133 "constexpr auto\n"
10134 "operator->() const -> reference {}\n"
10135 "constexpr auto\n"
10136 "operator++() const -> reference {}\n"
10137 "constexpr auto\n"
10138 "operator void *() const -> reference {}\n"
10139 "constexpr auto\n"
10140 "operator void **() const -> reference {}\n"
10141 "constexpr auto\n"
10142 "operator void *() const -> reference {}\n"
10143 "constexpr auto\n"
10144 "operator void &() const -> reference {}\n"
10145 "constexpr auto\n"
10146 "operator void &&() const -> reference {}\n"
10147 "constexpr auto\n"
10148 "operator char *() const -> reference {}\n"
10149 "constexpr auto\n"
10150 "operator!() const -> reference {}\n"
10151 "constexpr auto\n"
10152 "operator[]() const -> reference {}",
10153 Style);
10154 verifyFormat("void *operator new(std::size_t s);", // No break here.
10155 Style);
10156 verifyFormat("void *\n"
10157 "operator new(std::size_t s) {}",
10158 Style);
10159 verifyFormat("void *\n"
10160 "operator delete[](void *ptr) {}",
10161 Style);
10162 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10163 verifyFormat("const char *\n"
10164 "f(void)\n" // Break here.
10165 "{\n"
10166 " return \"\";\n"
10167 "}\n"
10168 "const char *bar(void);", // No break here.
10169 Style);
10170 verifyFormat("template <class T>\n"
10171 "T *\n" // Problem here: no line break
10172 "f(T &c)\n" // Break here.
10173 "{\n"
10174 " return NULL;\n"
10175 "}\n"
10176 "template <class T> T *f(T &c);", // No break here.
10177 Style);
10178 verifyFormat("int\n"
10179 "foo(A<bool> a)\n"
10180 "{\n"
10181 " return a;\n"
10182 "}",
10183 Style);
10184 verifyFormat("int\n"
10185 "foo(A<8> a)\n"
10186 "{\n"
10187 " return a;\n"
10188 "}",
10189 Style);
10190 verifyFormat("int\n"
10191 "foo(A<B<bool>, 8> a)\n"
10192 "{\n"
10193 " return a;\n"
10194 "}",
10195 Style);
10196 verifyFormat("int\n"
10197 "foo(A<B<8>, bool> a)\n"
10198 "{\n"
10199 " return a;\n"
10200 "}",
10201 Style);
10202 verifyFormat("int\n"
10203 "foo(A<B<bool>, bool> a)\n"
10204 "{\n"
10205 " return a;\n"
10206 "}",
10207 Style);
10208 verifyFormat("int\n"
10209 "foo(A<B<8>, 8> a)\n"
10210 "{\n"
10211 " return a;\n"
10212 "}",
10213 Style);
10214
10215 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10216 Style.BraceWrapping.AfterFunction = true;
10217 verifyFormat("int f(i);\n" // No break here.
10218 "int\n" // Break here.
10219 "f(i)\n"
10220 "{\n"
10221 " return i + 1;\n"
10222 "}\n"
10223 "int\n" // Break here.
10224 "f(i)\n"
10225 "{\n"
10226 " return i + 1;\n"
10227 "};",
10228 Style);
10229 verifyFormat("int f(a, b, c);\n" // No break here.
10230 "int\n" // Break here.
10231 "f(a, b, c)\n" // Break here.
10232 "short a, b;\n"
10233 "float c;\n"
10234 "{\n"
10235 " return a + b < c;\n"
10236 "}\n"
10237 "int\n" // Break here.
10238 "f(a, b, c)\n" // Break here.
10239 "short a, b;\n"
10240 "float c;\n"
10241 "{\n"
10242 " return a + b < c;\n"
10243 "};",
10244 Style);
10245 verifyFormat("byte *\n" // Break here.
10246 "f(a)\n" // Break here.
10247 "byte a[];\n"
10248 "{\n"
10249 " return a;\n"
10250 "}",
10251 Style);
10252 verifyFormat("byte *\n"
10253 "f(a)\n"
10254 "byte /* K&R C */ a[];\n"
10255 "{\n"
10256 " return a;\n"
10257 "}\n"
10258 "byte *\n"
10259 "g(p)\n"
10260 "byte /* K&R C */ *p;\n"
10261 "{\n"
10262 " return p;\n"
10263 "}",
10264 Style);
10265 verifyFormat("bool f(int a, int) override;\n"
10266 "Bar g(int a, Bar) final;\n"
10267 "Bar h(a, Bar) final;",
10268 Style);
10269 verifyFormat("int\n"
10270 "f(a)",
10271 Style);
10272 verifyFormat("bool\n"
10273 "f(size_t = 0, bool b = false)\n"
10274 "{\n"
10275 " return !b;\n"
10276 "}",
10277 Style);
10278
10279 // The return breaking style doesn't affect:
10280 // * function and object definitions with attribute-like macros
10281 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10282 " ABSL_GUARDED_BY(mutex) = {};",
10283 getGoogleStyleWithColumns(40));
10284 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10285 " ABSL_GUARDED_BY(mutex); // comment",
10286 getGoogleStyleWithColumns(40));
10287 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10288 " ABSL_GUARDED_BY(mutex1)\n"
10289 " ABSL_GUARDED_BY(mutex2);",
10290 getGoogleStyleWithColumns(40));
10291 verifyFormat("Tttttt f(int a, int b)\n"
10292 " ABSL_GUARDED_BY(mutex1)\n"
10293 " ABSL_GUARDED_BY(mutex2);",
10294 getGoogleStyleWithColumns(40));
10295 // * typedefs
10296 verifyGoogleFormat("typedef ATTR(X) char x;");
10297
10298 Style = getGNUStyle();
10299
10300 // Test for comments at the end of function declarations.
10301 verifyFormat("void\n"
10302 "foo (int a, /*abc*/ int b) // def\n"
10303 "{\n"
10304 "}",
10305 Style);
10306
10307 verifyFormat("void\n"
10308 "foo (int a, /* abc */ int b) /* def */\n"
10309 "{\n"
10310 "}",
10311 Style);
10312
10313 // Definitions that should not break after return type
10314 verifyFormat("void foo (int a, int b); // def", Style);
10315 verifyFormat("void foo (int a, int b); /* def */", Style);
10316 verifyFormat("void foo (int a, int b);", Style);
10317}
10318
10319TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10320 FormatStyle NoBreak = getLLVMStyle();
10321 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10322 FormatStyle Break = getLLVMStyle();
10323 Break.AlwaysBreakBeforeMultilineStrings = true;
10324 verifyFormat("aaaa = \"bbbb\"\n"
10325 " \"cccc\";",
10326 NoBreak);
10327 verifyFormat("aaaa =\n"
10328 " \"bbbb\"\n"
10329 " \"cccc\";",
10330 Break);
10331 verifyFormat("aaaa(\"bbbb\"\n"
10332 " \"cccc\");",
10333 NoBreak);
10334 verifyFormat("aaaa(\n"
10335 " \"bbbb\"\n"
10336 " \"cccc\");",
10337 Break);
10338 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10339 " \"cccc\");",
10340 NoBreak);
10341 verifyFormat("aaaa(qqq,\n"
10342 " \"bbbb\"\n"
10343 " \"cccc\");",
10344 Break);
10345 verifyFormat("aaaa(qqq,\n"
10346 " L\"bbbb\"\n"
10347 " L\"cccc\");",
10348 Break);
10349 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10350 " \"bbbb\"));",
10351 Break);
10352 verifyFormat("string s = someFunction(\n"
10353 " \"abc\"\n"
10354 " \"abc\");",
10355 Break);
10356
10357 // As we break before unary operators, breaking right after them is bad.
10358 verifyFormat("string foo = abc ? \"x\"\n"
10359 " \"blah blah blah blah blah blah\"\n"
10360 " : \"y\";",
10361 Break);
10362
10363 // Don't break if there is no column gain.
10364 verifyFormat("f(\"aaaa\"\n"
10365 " \"bbbb\");",
10366 Break);
10367
10368 // Treat literals with escaped newlines like multi-line string literals.
10369 verifyNoChange("x = \"a\\\n"
10370 "b\\\n"
10371 "c\";",
10372 NoBreak);
10373 verifyFormat("xxxx =\n"
10374 " \"a\\\n"
10375 "b\\\n"
10376 "c\";",
10377 "xxxx = \"a\\\n"
10378 "b\\\n"
10379 "c\";",
10380 Break);
10381
10382 verifyFormat("NSString *const kString =\n"
10383 " @\"aaaa\"\n"
10384 " @\"bbbb\";",
10385 "NSString *const kString = @\"aaaa\"\n"
10386 "@\"bbbb\";",
10387 Break);
10388
10389 Break.ColumnLimit = 0;
10390 verifyFormat("const char *hello = \"hello llvm\";", Break);
10391}
10392
10393TEST_F(FormatTest, AlignsPipes) {
10394 verifyFormat(
10395 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10396 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10397 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10398 verifyFormat(
10399 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10400 " << aaaaaaaaaaaaaaaaaaaa;");
10401 verifyFormat(
10402 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10403 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10404 verifyFormat(
10405 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10406 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10407 verifyFormat(
10408 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10409 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10410 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10411 verifyFormat(
10412 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10413 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10414 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10415 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10418 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10419 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10420 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10421 verifyFormat(
10422 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10424 verifyFormat(
10425 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10426 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10427
10428 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10429 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10430 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10432 " aaaaaaaaaaaaaaaaaaaaa)\n"
10433 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10434 verifyFormat("LOG_IF(aaa == //\n"
10435 " bbb)\n"
10436 " << a << b;");
10437
10438 // But sometimes, breaking before the first "<<" is desirable.
10439 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10440 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10441 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10442 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10443 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10444 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10445 " << BEF << IsTemplate << Description << E->getType();");
10446 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10447 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10449 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10450 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10452 " << aaa;");
10453
10454 verifyFormat(
10455 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10456 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10457
10458 // Incomplete string literal.
10459 verifyFormat("llvm::errs() << \"\n"
10460 " << a;",
10461 "llvm::errs() << \"\n<<a;");
10462
10463 verifyFormat("void f() {\n"
10464 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10465 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10466 "}");
10467
10468 // Handle 'endl'.
10469 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10470 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10471 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10472
10473 // Handle '\n'.
10474 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10475 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10476 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10477 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10478 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10479 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10480 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10481}
10482
10483TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10484 verifyFormat("return out << \"somepacket = {\\n\"\n"
10485 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10486 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10487 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10488 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10489 " << \"}\";");
10490
10491 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10492 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10493 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10494 verifyFormat(
10495 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10496 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10497 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10498 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10499 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10500 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10501 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10502 verifyFormat(
10503 "void f() {\n"
10504 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10505 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10506 "}");
10507
10508 // Breaking before the first "<<" is generally not desirable.
10509 verifyFormat(
10510 "llvm::errs()\n"
10511 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10512 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10513 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10514 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10515 getLLVMStyleWithColumns(70));
10516 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10517 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10518 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10519 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10520 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10521 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10522 getLLVMStyleWithColumns(70));
10523
10524 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10525 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10526 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10527 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10528 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10529 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10530 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10531 " (aaaa + aaaa);",
10532 getLLVMStyleWithColumns(40));
10533 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10534 " (aaaaaaa + aaaaa));",
10535 getLLVMStyleWithColumns(40));
10536 verifyFormat(
10537 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10538 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10539 " bbbbbbbbbbbbbbbbbbbbbbb);");
10540}
10541
10542TEST_F(FormatTest, UnderstandsEquals) {
10543 verifyFormat(
10544 "aaaaaaaaaaaaaaaaa =\n"
10545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10546 verifyFormat(
10547 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10549 verifyFormat(
10550 "if (a) {\n"
10551 " f();\n"
10552 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10554 "}");
10555
10556 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10557 " 100000000 + 10000000) {\n}");
10558}
10559
10560TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10561 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10562 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10563
10564 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10565 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10566
10567 verifyFormat(
10568 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10569 " Parameter2);");
10570
10571 verifyFormat(
10572 "ShortObject->shortFunction(\n"
10573 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10574 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10575
10576 verifyFormat("loooooooooooooongFunction(\n"
10577 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10578
10579 verifyFormat(
10580 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10581 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10582
10583 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10584 " .WillRepeatedly(Return(SomeValue));");
10585 verifyFormat("void f() {\n"
10586 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10587 " .Times(2)\n"
10588 " .WillRepeatedly(Return(SomeValue));\n"
10589 "}");
10590 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10591 " ccccccccccccccccccccccc);");
10592 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10594 " .aaaaa(aaaaa),\n"
10595 " aaaaaaaaaaaaaaaaaaaaa);");
10596 verifyFormat("void f() {\n"
10597 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10598 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10599 "}");
10600 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10602 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10603 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10604 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10605 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10606 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10607 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10608 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10609 "}");
10610
10611 // Here, it is not necessary to wrap at "." or "->".
10612 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10613 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10614 verifyFormat(
10615 "aaaaaaaaaaa->aaaaaaaaa(\n"
10616 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10617 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10618
10619 verifyFormat(
10620 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10622 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10623 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10624 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10625 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10626
10627 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10629 " .a();");
10630
10631 FormatStyle NoBinPacking = getLLVMStyle();
10632 NoBinPacking.BinPackParameters = false;
10633 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10634 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10635 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10636 " aaaaaaaaaaaaaaaaaaa,\n"
10637 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10638 NoBinPacking);
10639
10640 // If there is a subsequent call, change to hanging indentation.
10641 verifyFormat(
10642 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10643 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10644 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10645 verifyFormat(
10646 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10647 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10648 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10650 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10651 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10653 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10654}
10655
10656TEST_F(FormatTest, WrapsTemplateDeclarations) {
10657 verifyFormat("template <typename T>\n"
10658 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10659 verifyFormat("template <typename T>\n"
10660 "// T should be one of {A, B}.\n"
10661 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10662 verifyFormat(
10663 "template <typename T>\n"
10664 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10665 verifyFormat("template <typename T>\n"
10666 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10667 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10668 verifyFormat(
10669 "template <typename T>\n"
10670 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10671 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10672 verifyFormat(
10673 "template <typename T>\n"
10674 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10675 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10677 verifyFormat("template <typename T>\n"
10678 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10679 " int aaaaaaaaaaaaaaaaaaaaaa);");
10680 verifyFormat(
10681 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10682 " typename T4 = char>\n"
10683 "void f();");
10684 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10685 " template <typename> class cccccccccccccccccccccc,\n"
10686 " typename ddddddddddddd>\n"
10687 "class C {};");
10688 verifyFormat(
10689 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10691
10692 verifyFormat("void f() {\n"
10693 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10694 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10695 "}");
10696
10697 verifyFormat("template <typename T> class C {};");
10698 verifyFormat("template <typename T> void f();");
10699 verifyFormat("template <typename T> void f() {}");
10700 verifyFormat(
10701 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10704 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10707 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10708 getLLVMStyleWithColumns(72));
10709 verifyFormat("static_cast<A< //\n"
10710 " B> *>(\n"
10711 "\n"
10712 ");",
10713 "static_cast<A<//\n"
10714 " B>*>(\n"
10715 "\n"
10716 " );");
10717 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10718 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10719
10720 FormatStyle AlwaysBreak = getLLVMStyle();
10721 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
10722 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
10723 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
10724 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
10725 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10726 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10727 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10728 verifyFormat("template <template <typename> class Fooooooo,\n"
10729 " template <typename> class Baaaaaaar>\n"
10730 "struct C {};",
10731 AlwaysBreak);
10732 verifyFormat("template <typename T> // T can be A, B or C.\n"
10733 "struct C {};",
10734 AlwaysBreak);
10735 verifyFormat("template <typename T>\n"
10736 "C(T) noexcept;",
10737 AlwaysBreak);
10738 verifyFormat("template <typename T>\n"
10739 "ClassName(T) noexcept;",
10740 AlwaysBreak);
10741 verifyFormat("template <typename T>\n"
10742 "POOR_NAME(T) noexcept;",
10743 AlwaysBreak);
10744 verifyFormat("template <enum E> class A {\n"
10745 "public:\n"
10746 " E *f();\n"
10747 "};");
10748
10749 FormatStyle NeverBreak = getLLVMStyle();
10750 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
10751 verifyFormat("template <typename T> class C {};", NeverBreak);
10752 verifyFormat("template <typename T> void f();", NeverBreak);
10753 verifyFormat("template <typename T> void f() {}", NeverBreak);
10754 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
10755 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
10756 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
10757 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10758 "bbbbbbbbbbbbbbbbbbbb) {}",
10759 NeverBreak);
10760 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10761 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10762 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10763 NeverBreak);
10764 verifyFormat("template <template <typename> class Fooooooo,\n"
10765 " template <typename> class Baaaaaaar>\n"
10766 "struct C {};",
10767 NeverBreak);
10768 verifyFormat("template <typename T> // T can be A, B or C.\n"
10769 "struct C {};",
10770 NeverBreak);
10771 verifyFormat("template <enum E> class A {\n"
10772 "public:\n"
10773 " E *f();\n"
10774 "};",
10775 NeverBreak);
10776 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
10777 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10778 "bbbbbbbbbbbbbbbbbbbb) {}",
10779 NeverBreak);
10780
10781 auto Style = getLLVMStyle();
10782 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
10783
10784 verifyNoChange("template <typename T>\n"
10785 "class C {};",
10786 Style);
10787 verifyFormat("template <typename T> class C {};", Style);
10788
10789 verifyNoChange("template <typename T>\n"
10790 "void f();",
10791 Style);
10792 verifyFormat("template <typename T> void f();", Style);
10793
10794 verifyNoChange("template <typename T>\n"
10795 "void f() {}",
10796 Style);
10797 verifyFormat("template <typename T> void f() {}", Style);
10798
10799 verifyNoChange("template <typename T>\n"
10800 "// T can be A, B or C.\n"
10801 "struct C {};",
10802 Style);
10803 verifyFormat("template <typename T> // T can be A, B or C.\n"
10804 "struct C {};",
10805 Style);
10806
10807 verifyNoChange("template <typename T>\n"
10808 "C(T) noexcept;",
10809 Style);
10810 verifyFormat("template <typename T> C(T) noexcept;", Style);
10811
10812 verifyNoChange("template <enum E>\n"
10813 "class A {\n"
10814 "public:\n"
10815 " E *f();\n"
10816 "};",
10817 Style);
10818 verifyFormat("template <enum E> class A {\n"
10819 "public:\n"
10820 " E *f();\n"
10821 "};",
10822 Style);
10823
10824 verifyNoChange("template <auto x>\n"
10825 "constexpr int simple(int) {\n"
10826 " char c;\n"
10827 " return 1;\n"
10828 "}",
10829 Style);
10830 verifyFormat("template <auto x> constexpr int simple(int) {\n"
10831 " char c;\n"
10832 " return 1;\n"
10833 "}",
10834 Style);
10835
10836 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
10837 verifyNoChange("template <auto x>\n"
10838 "requires(x > 1)\n"
10839 "constexpr int with_req(int) {\n"
10840 " return 1;\n"
10841 "}",
10842 Style);
10843 verifyFormat("template <auto x> requires(x > 1)\n"
10844 "constexpr int with_req(int) {\n"
10845 " return 1;\n"
10846 "}",
10847 Style);
10848}
10849
10850TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
10851 FormatStyle Style = getGoogleStyle(Language: FormatStyle::LK_Cpp);
10852 Style.ColumnLimit = 60;
10853 verifyFormat("// Baseline - no comments.\n"
10854 "template <\n"
10855 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10856 "void f() {}",
10857 Style);
10858
10859 verifyFormat("template <\n"
10860 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10861 "void f() {}",
10862 "template <\n"
10863 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10864 "void f() {}",
10865 Style);
10866
10867 verifyFormat(
10868 "template <\n"
10869 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10870 "void f() {}",
10871 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10872 "void f() {}",
10873 Style);
10874
10875 verifyFormat("template <\n"
10876 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10877 " // multiline\n"
10878 "void f() {}",
10879 "template <\n"
10880 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10881 " // multiline\n"
10882 "void f() {}",
10883 Style);
10884
10885 verifyFormat(
10886 "template <typename aaaaaaaaaa<\n"
10887 " bbbbbbbbbbbb>::value> // trailing loooong\n"
10888 "void f() {}",
10889 "template <\n"
10890 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
10891 "void f() {}",
10892 Style);
10893}
10894
10895TEST_F(FormatTest, WrapsTemplateParameters) {
10896 FormatStyle Style = getLLVMStyle();
10897 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10898 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10899 verifyFormat(
10900 "template <typename... a> struct q {};\n"
10901 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10902 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10903 " y;",
10904 Style);
10905 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
10906 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10907 verifyFormat(
10908 "template <typename... a> struct r {};\n"
10909 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10910 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10911 " y;",
10912 Style);
10913 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10914 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
10915 verifyFormat("template <typename... a> struct s {};\n"
10916 "extern s<\n"
10917 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10918 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10919 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10920 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10921 " y;",
10922 Style);
10923 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10924 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10925 verifyFormat("template <typename... a> struct t {};\n"
10926 "extern t<\n"
10927 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10928 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10929 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10930 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10931 " y;",
10932 Style);
10933}
10934
10935TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
10936 verifyFormat(
10937 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10939 verifyFormat(
10940 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10943
10944 // FIXME: Should we have the extra indent after the second break?
10945 verifyFormat(
10946 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10949
10950 verifyFormat(
10951 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
10952 " cccccccccccccccccccccccccccccccccccccccccccccc());");
10953
10954 // Breaking at nested name specifiers is generally not desirable.
10955 verifyFormat(
10956 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10957 " aaaaaaaaaaaaaaaaaaaaaaa);");
10958
10959 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
10960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10961 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10962 " aaaaaaaaaaaaaaaaaaaaa);",
10963 getLLVMStyleWithColumns(74));
10964
10965 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10966 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10967 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10968
10969 verifyFormat(
10970 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10971 " AndAnotherLongClassNameToShowTheIssue() {}\n"
10972 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10973 " ~AndAnotherLongClassNameToShowTheIssue() {}");
10974}
10975
10976TEST_F(FormatTest, UnderstandsTemplateParameters) {
10977 verifyFormat("A<int> a;");
10978 verifyFormat("A<A<A<int>>> a;");
10979 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
10980 verifyFormat("bool x = a < 1 || 2 > a;");
10981 verifyFormat("bool x = 5 < f<int>();");
10982 verifyFormat("bool x = f<int>() > 5;");
10983 verifyFormat("bool x = 5 < a<int>::x;");
10984 verifyFormat("bool x = a < 4 ? a > 2 : false;");
10985 verifyFormat("bool x = f() ? a < 2 : a > 2;");
10986
10987 verifyGoogleFormat("A<A<int>> a;");
10988 verifyGoogleFormat("A<A<A<int>>> a;");
10989 verifyGoogleFormat("A<A<A<A<int>>>> a;");
10990 verifyGoogleFormat("A<A<int> > a;");
10991 verifyGoogleFormat("A<A<A<int> > > a;");
10992 verifyGoogleFormat("A<A<A<A<int> > > > a;");
10993 verifyGoogleFormat("A<::A<int>> a;");
10994 verifyGoogleFormat("A<::A> a;");
10995 verifyGoogleFormat("A< ::A> a;");
10996 verifyGoogleFormat("A< ::A<int> > a;");
10997 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
10998 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
10999 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11000 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11001 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11002 getGoogleStyle());
11003
11004 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11005
11006 // template closer followed by a token that starts with > or =
11007 verifyFormat("bool b = a<1> > 1;");
11008 verifyFormat("bool b = a<1> >= 1;");
11009 verifyFormat("int i = a<1> >> 1;");
11010 FormatStyle Style = getLLVMStyle();
11011 Style.SpaceBeforeAssignmentOperators = false;
11012 verifyFormat("bool b= a<1> == 1;", Style);
11013 verifyFormat("a<int> = 1;", Style);
11014 verifyFormat("a<int> >>= 1;", Style);
11015
11016 verifyFormat("test < a | b >> c;");
11017 verifyFormat("test<test<a | b>> c;");
11018 verifyFormat("test >> a >> b;");
11019 verifyFormat("test << a >> b;");
11020
11021 verifyFormat("f<int>();");
11022 verifyFormat("template <typename T> void f() {}");
11023 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11024 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11025 "sizeof(char)>::type>;");
11026 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11027 verifyFormat("f(a.operator()<A>());");
11028 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11029 " .template operator()<A>());",
11030 getLLVMStyleWithColumns(35));
11031 verifyFormat("bool_constant<a && noexcept(f())>;");
11032 verifyFormat("bool_constant<a || noexcept(f())>;");
11033
11034 verifyFormat("if (std::tuple_size_v<T> > 0)");
11035
11036 // Not template parameters.
11037 verifyFormat("return a < b && c > d;");
11038 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11039 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11040 verifyFormat("void f() {\n"
11041 " while (a < b && c > d) {\n"
11042 " }\n"
11043 "}");
11044 verifyFormat("template <typename... Types>\n"
11045 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11046
11047 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11049 getLLVMStyleWithColumns(60));
11050 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11051 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11052 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11053 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11054
11055 verifyFormat("#define FOO(typeName, realClass) \\\n"
11056 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11057 getLLVMStyleWithColumns(60));
11058}
11059
11060TEST_F(FormatTest, UnderstandsShiftOperators) {
11061 verifyFormat("if (i < x >> 1)");
11062 verifyFormat("while (i < x >> 1)");
11063 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11064 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11065 verifyFormat(
11066 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11067 verifyFormat("Foo.call<Bar<Function>>()");
11068 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11069 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11070 "++i, v = v >> 1)");
11071 verifyFormat("if (w<u<v<x>>, 1>::t)");
11072}
11073
11074TEST_F(FormatTest, BitshiftOperatorWidth) {
11075 verifyFormat("int a = 1 << 2; /* foo\n"
11076 " bar */",
11077 "int a=1<<2; /* foo\n"
11078 " bar */");
11079
11080 verifyFormat("int b = 256 >> 1; /* foo\n"
11081 " bar */",
11082 "int b =256>>1 ; /* foo\n"
11083 " bar */");
11084}
11085
11086TEST_F(FormatTest, UnderstandsBinaryOperators) {
11087 verifyFormat("COMPARE(a, ==, b);");
11088 verifyFormat("auto s = sizeof...(Ts) - 1;");
11089}
11090
11091TEST_F(FormatTest, UnderstandsPointersToMembers) {
11092 verifyFormat("int A:: *x;");
11093 verifyFormat("int (S:: *func)(void *);");
11094 verifyFormat("void f() { int (S:: *func)(void *); }");
11095 verifyFormat("typedef bool *(Class:: *Member)() const;");
11096 verifyFormat("void f() {\n"
11097 " (a->*f)();\n"
11098 " a->*x;\n"
11099 " (a.*f)();\n"
11100 " ((*a).*f)();\n"
11101 " a.*x;\n"
11102 "}");
11103 verifyFormat("void f() {\n"
11104 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11105 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11106 "}");
11107 verifyFormat(
11108 "(aaaaaaaaaa->*bbbbbbb)(\n"
11109 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11110
11111 FormatStyle Style = getLLVMStyle();
11112 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11113 verifyFormat("typedef bool *(Class:: *Member)() const;", Style);
11114 verifyFormat("void f(int A:: *p) { int A:: *v = &A::B; }", Style);
11115
11116 Style.PointerAlignment = FormatStyle::PAS_Left;
11117 verifyFormat("typedef bool* (Class::* Member)() const;", Style);
11118 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11119
11120 Style.PointerAlignment = FormatStyle::PAS_Middle;
11121 verifyFormat("typedef bool * (Class:: * Member)() const;", Style);
11122 verifyFormat("void f(int A:: * p) { int A:: * v = &A::B; }", Style);
11123}
11124
11125TEST_F(FormatTest, UnderstandsUnaryOperators) {
11126 verifyFormat("int a = -2;");
11127 verifyFormat("f(-1, -2, -3);");
11128 verifyFormat("a[-1] = 5;");
11129 verifyFormat("int a = 5 + -2;");
11130 verifyFormat("if (i == -1) {\n}");
11131 verifyFormat("if (i != -1) {\n}");
11132 verifyFormat("if (i > -1) {\n}");
11133 verifyFormat("if (i < -1) {\n}");
11134 verifyFormat("++(a->f());");
11135 verifyFormat("--(a->f());");
11136 verifyFormat("(a->f())++;");
11137 verifyFormat("a[42]++;");
11138 verifyFormat("if (!(a->f())) {\n}");
11139 verifyFormat("if (!+i) {\n}");
11140 verifyFormat("~&a;");
11141 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11142 verifyFormat("sizeof -x");
11143 verifyFormat("sizeof +x");
11144 verifyFormat("sizeof *x");
11145 verifyFormat("sizeof &x");
11146 verifyFormat("delete +x;");
11147 verifyFormat("co_await +x;");
11148 verifyFormat("case *x:");
11149 verifyFormat("case &x:");
11150
11151 verifyFormat("a-- > b;");
11152 verifyFormat("b ? -a : c;");
11153 verifyFormat("n * sizeof char16;");
11154 verifyGoogleFormat("n * alignof char16;");
11155 verifyFormat("sizeof(char);");
11156 verifyGoogleFormat("alignof(char);");
11157
11158 verifyFormat("return -1;");
11159 verifyFormat("throw -1;");
11160 verifyFormat("switch (a) {\n"
11161 "case -1:\n"
11162 " break;\n"
11163 "}");
11164 verifyFormat("#define X -1");
11165 verifyFormat("#define X -kConstant");
11166
11167 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11168 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11169
11170 verifyFormat("int a = /* confusing comment */ -1;");
11171 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11172 verifyFormat("int a = i /* confusing comment */++;");
11173
11174 verifyFormat("co_yield -1;");
11175 verifyFormat("co_return -1;");
11176
11177 // Check that * is not treated as a binary operator when we set
11178 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11179 FormatStyle PASLeftStyle = getLLVMStyle();
11180 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11181 verifyFormat("co_return *a;", PASLeftStyle);
11182 verifyFormat("co_await *a;", PASLeftStyle);
11183 verifyFormat("co_yield *a", PASLeftStyle);
11184 verifyFormat("return *a;", PASLeftStyle);
11185}
11186
11187TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11188 verifyFormat("if (!aaaaaaaaaa( // break\n"
11189 " aaaaa)) {\n"
11190 "}");
11191 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11192 " aaaaa));");
11193 verifyFormat("*aaa = aaaaaaa( // break\n"
11194 " bbbbbb);");
11195}
11196
11197TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11198 verifyFormat("bool operator<();");
11199 verifyFormat("bool operator>();");
11200 verifyFormat("bool operator=();");
11201 verifyFormat("bool operator==();");
11202 verifyFormat("bool operator!=();");
11203 verifyFormat("int operator+();");
11204 verifyFormat("int operator++();");
11205 verifyFormat("int operator++(int) volatile noexcept;");
11206 verifyFormat("bool operator,();");
11207 verifyFormat("bool operator();");
11208 verifyFormat("bool operator()();");
11209 verifyFormat("bool operator[]();");
11210 verifyFormat("operator bool();");
11211 verifyFormat("operator int();");
11212 verifyFormat("operator void *();");
11213 verifyFormat("operator SomeType<int>();");
11214 verifyFormat("operator SomeType<int, int>();");
11215 verifyFormat("operator SomeType<SomeType<int>>();");
11216 verifyFormat("operator< <>();");
11217 verifyFormat("operator<< <>();");
11218 verifyFormat("< <>");
11219
11220 verifyFormat("void *operator new(std::size_t size);");
11221 verifyFormat("void *operator new[](std::size_t size);");
11222 verifyFormat("void operator delete(void *ptr);");
11223 verifyFormat("void operator delete[](void *ptr);");
11224 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11225 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11226 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11227 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11228
11229 verifyFormat(
11230 "ostream &operator<<(ostream &OutputStream,\n"
11231 " SomeReallyLongType WithSomeReallyLongValue);");
11232 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11233 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11234 " return left.group < right.group;\n"
11235 "}");
11236 verifyFormat("SomeType &operator=(const SomeType &S);");
11237 verifyFormat("f.template operator()<int>();");
11238
11239 verifyGoogleFormat("operator void*();");
11240 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11241 verifyGoogleFormat("operator ::A();");
11242
11243 verifyFormat("using A::operator+;");
11244 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11245 "int i;");
11246
11247 // Calling an operator as a member function.
11248 verifyFormat("void f() { a.operator*(); }");
11249 verifyFormat("void f() { a.operator*(b & b); }");
11250 verifyFormat("void f() { a->operator&(a * b); }");
11251 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11252 verifyFormat("void f() { operator*(a & a); }");
11253 verifyFormat("void f() { operator&(a, b * b); }");
11254
11255 verifyFormat("void f() { return operator()(x) * b; }");
11256 verifyFormat("void f() { return operator[](x) * b; }");
11257 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11258 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11259 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11260 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11261 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11262
11263 verifyFormat("::operator delete(foo);");
11264 verifyFormat("::operator new(n * sizeof(foo));");
11265 verifyFormat("foo() { ::operator delete(foo); }");
11266 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11267}
11268
11269TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11270 verifyFormat("C<&operator- > minus;");
11271 verifyFormat("C<&operator> > gt;");
11272 verifyFormat("C<&operator>= > ge;");
11273 verifyFormat("C<&operator<= > le;");
11274 verifyFormat("C<&operator< <X>> lt;");
11275}
11276
11277TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11278 verifyFormat("void A::b() && {}");
11279 verifyFormat("void A::b() && noexcept {}");
11280 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11281 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11282 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11283 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11284 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11285 verifyFormat("Deleted &operator=(const Deleted &) &;");
11286 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11287 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11288 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11289 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11290 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11291 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11292 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11293 verifyFormat("void Fn(T const &) const &;");
11294 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11295 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11296 verifyGoogleFormat("template <typename T>\n"
11297 "void F(T) && = delete;");
11298 verifyFormat("template <typename T> void operator=(T) &;");
11299 verifyFormat("template <typename T> void operator=(T) const &;");
11300 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11301 verifyFormat("template <typename T> void operator=(T) & = default;");
11302 verifyFormat("template <typename T> void operator=(T) &&;");
11303 verifyFormat("template <typename T> void operator=(T) && = delete;");
11304 verifyFormat("template <typename T> void operator=(T) & {}");
11305 verifyFormat("template <typename T> void operator=(T) && {}");
11306
11307 FormatStyle AlignLeft = getLLVMStyle();
11308 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11309 verifyFormat("void A::b() && {}", AlignLeft);
11310 verifyFormat("void A::b() && noexcept {}", AlignLeft);
11311 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11312 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11313 AlignLeft);
11314 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11315 AlignLeft);
11316 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11317 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11318 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11319 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11320 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11321 verifyFormat("auto Function(T) & -> void;", AlignLeft);
11322 verifyFormat("void Fn(T const&) const&;", AlignLeft);
11323 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11324 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11325 AlignLeft);
11326 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11327 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11328 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11329 AlignLeft);
11330 verifyFormat("template <typename T> void operator=(T) & = default;",
11331 AlignLeft);
11332 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11333 verifyFormat("template <typename T> void operator=(T) && = delete;",
11334 AlignLeft);
11335 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11336 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11337
11338 FormatStyle AlignMiddle = getLLVMStyle();
11339 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11340 verifyFormat("void A::b() && {}", AlignMiddle);
11341 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11342 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11343 AlignMiddle);
11344 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11345 AlignMiddle);
11346 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11347 AlignMiddle);
11348 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
11349 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
11350 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
11351 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
11352 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
11353 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
11354 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
11355 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
11356 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11357 AlignMiddle);
11358 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
11359 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
11360 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11361 AlignMiddle);
11362 verifyFormat("template <typename T> void operator=(T) & = default;",
11363 AlignMiddle);
11364 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
11365 verifyFormat("template <typename T> void operator=(T) && = delete;",
11366 AlignMiddle);
11367 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
11368 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
11369
11370 FormatStyle Spaces = getLLVMStyle();
11371 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
11372 Spaces.SpacesInParensOptions = {};
11373 Spaces.SpacesInParensOptions.InCStyleCasts = true;
11374 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
11375 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
11376 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
11377 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
11378
11379 Spaces.SpacesInParensOptions.InCStyleCasts = false;
11380 Spaces.SpacesInParensOptions.Other = true;
11381 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
11382 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11383 Spaces);
11384 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
11385 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
11386
11387 FormatStyle BreakTemplate = getLLVMStyle();
11388 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11389
11390 verifyFormat("struct f {\n"
11391 " template <class T>\n"
11392 " int &foo(const std::string &str) & noexcept {}\n"
11393 "};",
11394 BreakTemplate);
11395
11396 verifyFormat("struct f {\n"
11397 " template <class T>\n"
11398 " int &foo(const std::string &str) && noexcept {}\n"
11399 "};",
11400 BreakTemplate);
11401
11402 verifyFormat("struct f {\n"
11403 " template <class T>\n"
11404 " int &foo(const std::string &str) const & noexcept {}\n"
11405 "};",
11406 BreakTemplate);
11407
11408 verifyFormat("struct f {\n"
11409 " template <class T>\n"
11410 " int &foo(const std::string &str) const & noexcept {}\n"
11411 "};",
11412 BreakTemplate);
11413
11414 verifyFormat("struct f {\n"
11415 " template <class T>\n"
11416 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11417 "};",
11418 BreakTemplate);
11419
11420 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
11421 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11422 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
11423
11424 verifyFormat("struct f {\n"
11425 " template <class T>\n"
11426 " int& foo(const std::string& str) & noexcept {}\n"
11427 "};",
11428 AlignLeftBreakTemplate);
11429
11430 verifyFormat("struct f {\n"
11431 " template <class T>\n"
11432 " int& foo(const std::string& str) && noexcept {}\n"
11433 "};",
11434 AlignLeftBreakTemplate);
11435
11436 verifyFormat("struct f {\n"
11437 " template <class T>\n"
11438 " int& foo(const std::string& str) const& noexcept {}\n"
11439 "};",
11440 AlignLeftBreakTemplate);
11441
11442 verifyFormat("struct f {\n"
11443 " template <class T>\n"
11444 " int& foo(const std::string& str) const&& noexcept {}\n"
11445 "};",
11446 AlignLeftBreakTemplate);
11447
11448 verifyFormat("struct f {\n"
11449 " template <class T>\n"
11450 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11451 "};",
11452 AlignLeftBreakTemplate);
11453
11454 // The `&` in `Type&` should not be confused with a trailing `&` of
11455 // DEPRECATED(reason) member function.
11456 verifyFormat("struct f {\n"
11457 " template <class T>\n"
11458 " DEPRECATED(reason)\n"
11459 " Type &foo(arguments) {}\n"
11460 "};",
11461 BreakTemplate);
11462
11463 verifyFormat("struct f {\n"
11464 " template <class T>\n"
11465 " DEPRECATED(reason)\n"
11466 " Type& foo(arguments) {}\n"
11467 "};",
11468 AlignLeftBreakTemplate);
11469
11470 verifyFormat("void (*foopt)(int) = &func;");
11471
11472 FormatStyle DerivePointerAlignment = getLLVMStyle();
11473 DerivePointerAlignment.DerivePointerAlignment = true;
11474 // There's always a space between the function and its trailing qualifiers.
11475 // This isn't evidence for PAS_Right (or for PAS_Left).
11476 std::string Prefix = "void a() &;\n"
11477 "void b() &;\n";
11478 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11479 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11480 // Same if the function is an overloaded operator, and with &&.
11481 Prefix = "void operator()() &&;\n"
11482 "void operator()() &&;\n";
11483 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
11484 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
11485 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11486 Prefix = "void a() const &;\n"
11487 "void b() const &;\n";
11488 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
11489}
11490
11491TEST_F(FormatTest, PointerAlignmentFallback) {
11492 FormatStyle Style = getLLVMStyle();
11493 Style.DerivePointerAlignment = true;
11494
11495 const StringRef Code("int* p;\n"
11496 "int *q;\n"
11497 "int * r;");
11498
11499 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11500 verifyFormat("int *p;\n"
11501 "int *q;\n"
11502 "int *r;",
11503 Code, Style);
11504
11505 Style.PointerAlignment = FormatStyle::PAS_Left;
11506 verifyFormat("int* p;\n"
11507 "int* q;\n"
11508 "int* r;",
11509 Code, Style);
11510
11511 Style.PointerAlignment = FormatStyle::PAS_Middle;
11512 verifyFormat("int * p;\n"
11513 "int * q;\n"
11514 "int * r;",
11515 Code, Style);
11516}
11517
11518TEST_F(FormatTest, UnderstandsNewAndDelete) {
11519 verifyFormat("void f() {\n"
11520 " A *a = new A;\n"
11521 " A *a = new (placement) A;\n"
11522 " delete a;\n"
11523 " delete (A *)a;\n"
11524 "}");
11525 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11526 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11527 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11528 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11529 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11530 verifyFormat("delete[] h->p;");
11531 verifyFormat("delete[] (void *)p;");
11532
11533 verifyFormat("void operator delete(void *foo) ATTRIB;");
11534 verifyFormat("void operator new(void *foo) ATTRIB;");
11535 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11536 verifyFormat("void operator delete(void *ptr) noexcept;");
11537
11538 verifyFormat("void new(link p);\n"
11539 "void delete(link p);",
11540 "void new (link p);\n"
11541 "void delete (link p);");
11542
11543 verifyFormat("{\n"
11544 " p->new();\n"
11545 "}\n"
11546 "{\n"
11547 " p->delete();\n"
11548 "}",
11549 "{\n"
11550 " p->new ();\n"
11551 "}\n"
11552 "{\n"
11553 " p->delete ();\n"
11554 "}");
11555
11556 FormatStyle AfterPlacementOperator = getLLVMStyle();
11557 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11558 EXPECT_TRUE(
11559 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
11560 verifyFormat("new (buf) int;", AfterPlacementOperator);
11561 verifyFormat("struct A {\n"
11562 " int *a;\n"
11563 " A(int *p) : a(new (p) int) {\n"
11564 " new (p) int;\n"
11565 " int *b = new (p) int;\n"
11566 " int *c = new (p) int(3);\n"
11567 " delete (b);\n"
11568 " }\n"
11569 "};",
11570 AfterPlacementOperator);
11571 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11572
11573 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11574 false;
11575 verifyFormat("new(buf) int;", AfterPlacementOperator);
11576 verifyFormat("struct A {\n"
11577 " int *a;\n"
11578 " A(int *p) : a(new(p) int) {\n"
11579 " new(p) int;\n"
11580 " int *b = new(p) int;\n"
11581 " int *c = new(p) int(3);\n"
11582 " delete(b);\n"
11583 " }\n"
11584 "};",
11585 AfterPlacementOperator);
11586 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
11587}
11588
11589TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
11590 verifyFormat("int *f(int *a) {}");
11591 verifyFormat("int main(int argc, char **argv) {}");
11592 verifyFormat("Test::Test(int b) : a(b * b) {}");
11593 verifyIndependentOfContext("f(a, *a);");
11594 verifyFormat("void g() { f(*a); }");
11595 verifyIndependentOfContext("int a = b * 10;");
11596 verifyIndependentOfContext("int a = 10 * b;");
11597 verifyIndependentOfContext("int a = b * c;");
11598 verifyIndependentOfContext("int a += b * c;");
11599 verifyIndependentOfContext("int a -= b * c;");
11600 verifyIndependentOfContext("int a *= b * c;");
11601 verifyIndependentOfContext("int a /= b * c;");
11602 verifyIndependentOfContext("int a = *b;");
11603 verifyIndependentOfContext("int a = *b * c;");
11604 verifyIndependentOfContext("int a = b * *c;");
11605 verifyIndependentOfContext("int a = b * (10);");
11606 verifyIndependentOfContext("S << b * (10);");
11607 verifyIndependentOfContext("return 10 * b;");
11608 verifyIndependentOfContext("return *b * *c;");
11609 verifyIndependentOfContext("return a & ~b;");
11610 verifyIndependentOfContext("f(b ? *c : *d);");
11611 verifyIndependentOfContext("int a = b ? *c : *d;");
11612 verifyIndependentOfContext("*b = a;");
11613 verifyIndependentOfContext("a * ~b;");
11614 verifyIndependentOfContext("a * !b;");
11615 verifyIndependentOfContext("a * +b;");
11616 verifyIndependentOfContext("a * -b;");
11617 verifyIndependentOfContext("a * ++b;");
11618 verifyIndependentOfContext("a * --b;");
11619 verifyIndependentOfContext("a[4] * b;");
11620 verifyIndependentOfContext("a[a * a] = 1;");
11621 verifyIndependentOfContext("f() * b;");
11622 verifyIndependentOfContext("a * [self dostuff];");
11623 verifyIndependentOfContext("int x = a * (a + b);");
11624 verifyIndependentOfContext("(a *)(a + b);");
11625 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11626 verifyIndependentOfContext("int *pa = (int *)&a;");
11627 verifyIndependentOfContext("return sizeof(int **);");
11628 verifyIndependentOfContext("return sizeof(int ******);");
11629 verifyIndependentOfContext("return (int **&)a;");
11630 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11631 verifyFormat("void f(Type (*parameter)[10]) {}");
11632 verifyFormat("void f(Type (&parameter)[10]) {}");
11633 verifyGoogleFormat("return sizeof(int**);");
11634 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11635 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11636 verifyFormat("auto a = [](int **&, int ***) {};");
11637 verifyFormat("auto PointerBinding = [](const char *S) {};");
11638 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11639 verifyFormat("[](const decltype(*a) &value) {}");
11640 verifyFormat("[](const typeof(*a) &value) {}");
11641 verifyFormat("[](const _Atomic(a *) &value) {}");
11642 verifyFormat("[](const __underlying_type(a) &value) {}");
11643 verifyFormat("decltype(a * b) F();");
11644 verifyFormat("typeof(a * b) F();");
11645 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11646 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11647 verifyIndependentOfContext("typedef void (*f)(int *a);");
11648 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11649 verifyIndependentOfContext("int i{a * b};");
11650 verifyIndependentOfContext("aaa && aaa->f();");
11651 verifyIndependentOfContext("int x = ~*p;");
11652 verifyFormat("Constructor() : a(a), area(width * height) {}");
11653 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11654 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11655 verifyFormat("void f() { f(a, c * d); }");
11656 verifyFormat("void f() { f(new a(), c * d); }");
11657 verifyFormat("void f(const MyOverride &override);");
11658 verifyFormat("void f(const MyFinal &final);");
11659 verifyIndependentOfContext("bool a = f() && override.f();");
11660 verifyIndependentOfContext("bool a = f() && final.f();");
11661
11662 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11663
11664 verifyIndependentOfContext("A<int *> a;");
11665 verifyIndependentOfContext("A<int **> a;");
11666 verifyIndependentOfContext("A<int *, int *> a;");
11667 verifyIndependentOfContext("A<int *[]> a;");
11668 verifyIndependentOfContext(
11669 "const char *const p = reinterpret_cast<const char *const>(q);");
11670 verifyIndependentOfContext("A<int **, int **> a;");
11671 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11672 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11673 verifyFormat("for (; a && b;) {\n}");
11674 verifyFormat("bool foo = true && [] { return false; }();");
11675
11676 verifyFormat(
11677 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11679
11680 verifyGoogleFormat("int const* a = &b;");
11681 verifyGoogleFormat("**outparam = 1;");
11682 verifyGoogleFormat("*outparam = a * b;");
11683 verifyGoogleFormat("int main(int argc, char** argv) {}");
11684 verifyGoogleFormat("A<int*> a;");
11685 verifyGoogleFormat("A<int**> a;");
11686 verifyGoogleFormat("A<int*, int*> a;");
11687 verifyGoogleFormat("A<int**, int**> a;");
11688 verifyGoogleFormat("f(b ? *c : *d);");
11689 verifyGoogleFormat("int a = b ? *c : *d;");
11690 verifyGoogleFormat("Type* t = **x;");
11691 verifyGoogleFormat("Type* t = *++*x;");
11692 verifyGoogleFormat("*++*x;");
11693 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11694 verifyGoogleFormat("Type* t = x++ * y;");
11695 verifyGoogleFormat(
11696 "const char* const p = reinterpret_cast<const char* const>(q);");
11697 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11698 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11699 verifyGoogleFormat("template <typename T>\n"
11700 "void f(int i = 0, SomeType** temps = NULL);");
11701
11702 FormatStyle Left = getLLVMStyle();
11703 Left.PointerAlignment = FormatStyle::PAS_Left;
11704 verifyFormat("x = *a(x) = *a(y);", Left);
11705 verifyFormat("for (;; *a = b) {\n}", Left);
11706 verifyFormat("return *this += 1;", Left);
11707 verifyFormat("throw *x;", Left);
11708 verifyFormat("delete *x;", Left);
11709 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
11710 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
11711 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
11712 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
11713 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
11714 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
11715 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
11716 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
11717 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
11718
11719 verifyIndependentOfContext("a = *(x + y);");
11720 verifyIndependentOfContext("a = &(x + y);");
11721 verifyIndependentOfContext("*(x + y).call();");
11722 verifyIndependentOfContext("&(x + y)->call();");
11723 verifyFormat("void f() { &(*I).first; }");
11724
11725 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11726 verifyFormat("f(* /* confusing comment */ foo);");
11727 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11728 verifyFormat("void foo(int * // this is the first paramters\n"
11729 " ,\n"
11730 " int second);");
11731 verifyFormat("double term = a * // first\n"
11732 " b;");
11733 verifyFormat(
11734 "int *MyValues = {\n"
11735 " *A, // Operator detection might be confused by the '{'\n"
11736 " *BB // Operator detection might be confused by previous comment\n"
11737 "};");
11738
11739 verifyIndependentOfContext("if (int *a = &b)");
11740 verifyIndependentOfContext("if (int &a = *b)");
11741 verifyIndependentOfContext("if (a & b[i])");
11742 verifyIndependentOfContext("if constexpr (a & b[i])");
11743 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11744 verifyIndependentOfContext("if (a * (b * c))");
11745 verifyIndependentOfContext("if constexpr (a * (b * c))");
11746 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11747 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11748 verifyIndependentOfContext("if (*b[i])");
11749 verifyIndependentOfContext("if (int *a = (&b))");
11750 verifyIndependentOfContext("while (int *a = &b)");
11751 verifyIndependentOfContext("while (a * (b * c))");
11752 verifyIndependentOfContext("size = sizeof *a;");
11753 verifyIndependentOfContext("if (a && (b = c))");
11754 verifyFormat("void f() {\n"
11755 " for (const int &v : Values) {\n"
11756 " }\n"
11757 "}");
11758 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11759 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11760 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11761
11762 verifyFormat("#define A (!a * b)");
11763 verifyFormat("#define MACRO \\\n"
11764 " int *i = a * b; \\\n"
11765 " void f(a *b);",
11766 getLLVMStyleWithColumns(19));
11767
11768 verifyIndependentOfContext("A = new SomeType *[Length];");
11769 verifyIndependentOfContext("A = new SomeType *[Length]();");
11770 verifyIndependentOfContext("T **t = new T *;");
11771 verifyIndependentOfContext("T **t = new T *();");
11772 verifyGoogleFormat("A = new SomeType*[Length]();");
11773 verifyGoogleFormat("A = new SomeType*[Length];");
11774 verifyGoogleFormat("T** t = new T*;");
11775 verifyGoogleFormat("T** t = new T*();");
11776
11777 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11778 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11779 verifyFormat("template <bool a, bool b> "
11780 "typename t::if<x && y>::type f() {}");
11781 verifyFormat("template <int *y> f() {}");
11782 verifyFormat("vector<int *> v;");
11783 verifyFormat("vector<int *const> v;");
11784 verifyFormat("vector<int *const **const *> v;");
11785 verifyFormat("vector<int *volatile> v;");
11786 verifyFormat("vector<a *_Nonnull> v;");
11787 verifyFormat("vector<a *_Nullable> v;");
11788 verifyFormat("vector<a *_Null_unspecified> v;");
11789 verifyFormat("vector<a *__ptr32> v;");
11790 verifyFormat("vector<a *__ptr64> v;");
11791 verifyFormat("vector<a *__capability> v;");
11792 FormatStyle TypeMacros = getLLVMStyle();
11793 TypeMacros.TypenameMacros = {"LIST"};
11794 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
11795 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
11796 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
11797 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
11798 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
11799
11800 FormatStyle CustomQualifier = getLLVMStyle();
11801 // Add identifiers that should not be parsed as a qualifier by default.
11802 CustomQualifier.AttributeMacros.push_back(x: "__my_qualifier");
11803 CustomQualifier.AttributeMacros.push_back(x: "_My_qualifier");
11804 CustomQualifier.AttributeMacros.push_back(x: "my_other_qualifier");
11805 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11806 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
11807 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11808 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
11809 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11810 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
11811 verifyFormat("vector<a * _NotAQualifier> v;");
11812 verifyFormat("vector<a * __not_a_qualifier> v;");
11813 verifyFormat("vector<a * b> v;");
11814 verifyFormat("foo<b && false>();");
11815 verifyFormat("foo<b & 1>();");
11816 verifyFormat("foo<b & (1)>();");
11817 verifyFormat("foo<b & (~0)>();");
11818 verifyFormat("foo<b & (true)>();");
11819 verifyFormat("foo<b & ((1))>();");
11820 verifyFormat("foo<b & (/*comment*/ 1)>();");
11821 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
11822 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
11823 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
11824 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
11825 verifyFormat(
11826 "template <class T, class = typename std::enable_if<\n"
11827 " std::is_integral<T>::value &&\n"
11828 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
11829 "void F();",
11830 getLLVMStyleWithColumns(70));
11831 verifyFormat("template <class T,\n"
11832 " class = typename std::enable_if<\n"
11833 " std::is_integral<T>::value &&\n"
11834 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
11835 " class U>\n"
11836 "void F();",
11837 getLLVMStyleWithColumns(70));
11838 verifyFormat(
11839 "template <class T,\n"
11840 " class = typename ::std::enable_if<\n"
11841 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
11842 "void F();",
11843 getGoogleStyleWithColumns(68));
11844
11845 FormatStyle Style = getLLVMStyle();
11846 Style.PointerAlignment = FormatStyle::PAS_Left;
11847 verifyFormat("struct {\n"
11848 "}* ptr;",
11849 Style);
11850 verifyFormat("union {\n"
11851 "}* ptr;",
11852 Style);
11853 verifyFormat("class {\n"
11854 "}* ptr;",
11855 Style);
11856 // Don't confuse a multiplication after a brace-initialized expression with
11857 // a class pointer.
11858 verifyFormat("int i = int{42} * 34;", Style);
11859 verifyFormat("struct {\n"
11860 "}&& ptr = {};",
11861 Style);
11862 verifyFormat("union {\n"
11863 "}&& ptr = {};",
11864 Style);
11865 verifyFormat("class {\n"
11866 "}&& ptr = {};",
11867 Style);
11868 verifyFormat("bool b = 3 == int{3} && true;");
11869
11870 Style.PointerAlignment = FormatStyle::PAS_Middle;
11871 verifyFormat("struct {\n"
11872 "} * ptr;",
11873 Style);
11874 verifyFormat("union {\n"
11875 "} * ptr;",
11876 Style);
11877 verifyFormat("class {\n"
11878 "} * ptr;",
11879 Style);
11880 verifyFormat("struct {\n"
11881 "} && ptr = {};",
11882 Style);
11883 verifyFormat("union {\n"
11884 "} && ptr = {};",
11885 Style);
11886 verifyFormat("class {\n"
11887 "} && ptr = {};",
11888 Style);
11889
11890 Style.PointerAlignment = FormatStyle::PAS_Right;
11891 verifyFormat("struct {\n"
11892 "} *ptr;",
11893 Style);
11894 verifyFormat("union {\n"
11895 "} *ptr;",
11896 Style);
11897 verifyFormat("class {\n"
11898 "} *ptr;",
11899 Style);
11900 verifyFormat("struct {\n"
11901 "} &&ptr = {};",
11902 Style);
11903 verifyFormat("union {\n"
11904 "} &&ptr = {};",
11905 Style);
11906 verifyFormat("class {\n"
11907 "} &&ptr = {};",
11908 Style);
11909
11910 Style.PointerAlignment = FormatStyle::PAS_Left;
11911 verifyFormat("delete[] *ptr;", Style);
11912 verifyFormat("delete[] **ptr;", Style);
11913 verifyFormat("delete[] *(ptr);", Style);
11914
11915 verifyIndependentOfContext("MACRO(int *i);");
11916 verifyIndependentOfContext("MACRO(auto *a);");
11917 verifyIndependentOfContext("MACRO(const A *a);");
11918 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
11919 verifyIndependentOfContext("MACRO(decltype(A) *a);");
11920 verifyIndependentOfContext("MACRO(typeof(A) *a);");
11921 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
11922 verifyIndependentOfContext("MACRO(A *const a);");
11923 verifyIndependentOfContext("MACRO(A *restrict a);");
11924 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
11925 verifyIndependentOfContext("MACRO(A *__restrict a);");
11926 verifyIndependentOfContext("MACRO(A *volatile a);");
11927 verifyIndependentOfContext("MACRO(A *__volatile a);");
11928 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
11929 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
11930 verifyIndependentOfContext("MACRO(A *_Nullable a);");
11931 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
11932 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
11933 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
11934 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
11935 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
11936 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
11937 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
11938 verifyIndependentOfContext("MACRO(A *__capability);");
11939 verifyIndependentOfContext("MACRO(A &__capability);");
11940 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
11941 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
11942 // If we add __my_qualifier to AttributeMacros it should always be parsed as
11943 // a type declaration:
11944 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
11945 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
11946 // Also check that TypenameMacros prevents parsing it as multiplication:
11947 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
11948 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
11949
11950 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
11951 verifyFormat("void f() { f(float{1}, a * a); }");
11952 verifyFormat("void f() { f(float(1), a * a); }");
11953
11954 verifyFormat("f((void (*)(int))g);");
11955 verifyFormat("f((void (&)(int))g);");
11956 verifyFormat("f((void (^)(int))g);");
11957
11958 // FIXME: Is there a way to make this work?
11959 // verifyIndependentOfContext("MACRO(A *a);");
11960 verifyFormat("MACRO(A &B);");
11961 verifyFormat("MACRO(A *B);");
11962 verifyFormat("void f() { MACRO(A * B); }");
11963 verifyFormat("void f() { MACRO(A & B); }");
11964
11965 // This lambda was mis-formatted after D88956 (treating it as a binop):
11966 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
11967 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
11968 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
11969 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
11970
11971 verifyFormat("DatumHandle const *operator->() const { return input_; }");
11972 verifyFormat("return options != nullptr && operator==(*options);");
11973
11974 verifyFormat("#define OP(x) \\\n"
11975 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11976 " return s << a.DebugString(); \\\n"
11977 " }",
11978 "#define OP(x) \\\n"
11979 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11980 " return s << a.DebugString(); \\\n"
11981 " }",
11982 getLLVMStyleWithColumns(50));
11983
11984 verifyFormat("#define FOO \\\n"
11985 " void foo() { \\\n"
11986 " operator+(a * b); \\\n"
11987 " }",
11988 getLLVMStyleWithColumns(25));
11989
11990 // FIXME: We cannot handle this case yet; we might be able to figure out that
11991 // foo<x> d > v; doesn't make sense.
11992 verifyFormat("foo<a<b && c> d> v;");
11993
11994 FormatStyle PointerMiddle = getLLVMStyle();
11995 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11996 verifyFormat("delete *x;", PointerMiddle);
11997 verifyFormat("int * x;", PointerMiddle);
11998 verifyFormat("int *[] x;", PointerMiddle);
11999 verifyFormat("template <int * y> f() {}", PointerMiddle);
12000 verifyFormat("int * f(int * a) {}", PointerMiddle);
12001 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12002 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12003 verifyFormat("A<int *> a;", PointerMiddle);
12004 verifyFormat("A<int **> a;", PointerMiddle);
12005 verifyFormat("A<int *, int *> a;", PointerMiddle);
12006 verifyFormat("A<int *[]> a;", PointerMiddle);
12007 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12008 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12009 verifyFormat("T ** t = new T *;", PointerMiddle);
12010
12011 // Member function reference qualifiers aren't binary operators.
12012 verifyFormat("string // break\n"
12013 "operator()() & {}");
12014 verifyFormat("string // break\n"
12015 "operator()() && {}");
12016 verifyGoogleFormat("template <typename T>\n"
12017 "auto x() & -> int {}");
12018
12019 // Should be binary operators when used as an argument expression (overloaded
12020 // operator invoked as a member function).
12021 verifyFormat("void f() { a.operator()(a * a); }");
12022 verifyFormat("void f() { a->operator()(a & a); }");
12023 verifyFormat("void f() { a.operator()(*a & *a); }");
12024 verifyFormat("void f() { a->operator()(*a * *a); }");
12025
12026 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12027 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12028
12029 verifyFormat("val1 & val2;");
12030 verifyFormat("val1 & val2 & val3;");
12031 verifyFormat("class c {\n"
12032 " void func(type &a) { a & member; }\n"
12033 " anotherType &member;\n"
12034 "}");
12035}
12036
12037TEST_F(FormatTest, UnderstandsAttributes) {
12038 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12039 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12040 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12041 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12042 FormatStyle AfterType = getLLVMStyle();
12043 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12044 verifyFormat("__attribute__((nodebug)) void\n"
12045 "foo() {}",
12046 AfterType);
12047 verifyFormat("__unused void\n"
12048 "foo() {}",
12049 AfterType);
12050
12051 FormatStyle CustomAttrs = getLLVMStyle();
12052 CustomAttrs.AttributeMacros.push_back(x: "__unused");
12053 CustomAttrs.AttributeMacros.push_back(x: "__attr1");
12054 CustomAttrs.AttributeMacros.push_back(x: "__attr2");
12055 CustomAttrs.AttributeMacros.push_back(x: "no_underscore_attr");
12056 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12057 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12058 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12059 // Check that it is parsed as a multiplication without AttributeMacros and
12060 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12061 verifyFormat("vector<SomeType * __attr1> v;");
12062 verifyFormat("vector<SomeType __attr1 *> v;");
12063 verifyFormat("vector<SomeType __attr1 *const> v;");
12064 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12065 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12066 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12067 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12068 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12069 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12070 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12071 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12072 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12073 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12074 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12075
12076 // Check that these are not parsed as function declarations:
12077 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12078 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12079 verifyFormat("SomeType s(InitValue);", CustomAttrs);
12080 verifyFormat("SomeType s{InitValue};", CustomAttrs);
12081 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12082 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12083 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12084 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12085 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12086 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12087}
12088
12089TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12090 // Check that qualifiers on pointers don't break parsing of casts.
12091 verifyFormat("x = (foo *const)*v;");
12092 verifyFormat("x = (foo *volatile)*v;");
12093 verifyFormat("x = (foo *restrict)*v;");
12094 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12095 verifyFormat("x = (foo *_Nonnull)*v;");
12096 verifyFormat("x = (foo *_Nullable)*v;");
12097 verifyFormat("x = (foo *_Null_unspecified)*v;");
12098 verifyFormat("x = (foo *_Nonnull)*v;");
12099 verifyFormat("x = (foo *[[clang::attr]])*v;");
12100 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12101 verifyFormat("x = (foo *__ptr32)*v;");
12102 verifyFormat("x = (foo *__ptr64)*v;");
12103 verifyFormat("x = (foo *__capability)*v;");
12104
12105 // Check that we handle multiple trailing qualifiers and skip them all to
12106 // determine that the expression is a cast to a pointer type.
12107 FormatStyle LongPointerRight = getLLVMStyleWithColumns(ColumnLimit: 999);
12108 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(ColumnLimit: 999);
12109 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12110 StringRef AllQualifiers =
12111 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12112 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12113 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12114 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12115
12116 // Also check that address-of is not parsed as a binary bitwise-and:
12117 verifyFormat("x = (foo *const)&v;");
12118 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12119 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12120
12121 // Check custom qualifiers:
12122 FormatStyle CustomQualifier = getLLVMStyleWithColumns(ColumnLimit: 999);
12123 CustomQualifier.AttributeMacros.push_back(x: "__my_qualifier");
12124 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12125 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12126 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12127 CustomQualifier);
12128 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12129 CustomQualifier);
12130
12131 // Check that unknown identifiers result in binary operator parsing:
12132 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12133 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12134}
12135
12136TEST_F(FormatTest, UnderstandsSquareAttributes) {
12137 verifyFormat("SomeType s [[unused]] (InitValue);");
12138 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12139 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12140 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12141 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12142 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12143 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12144 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12145 verifyFormat("[[nodiscard]] bool f() { return false; }");
12146 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12147 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12148 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12149 verifyFormat("[[nodiscard]] ::qualified_type f();");
12150
12151 // Make sure we do not mistake attributes for array subscripts.
12152 verifyFormat("int a() {}\n"
12153 "[[unused]] int b() {}");
12154 verifyFormat("NSArray *arr;\n"
12155 "arr[[Foo() bar]];");
12156
12157 // On the other hand, we still need to correctly find array subscripts.
12158 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12159
12160 // Make sure that we do not mistake Objective-C method inside array literals
12161 // as attributes, even if those method names are also keywords.
12162 verifyFormat("@[ [foo bar] ];");
12163 verifyFormat("@[ [NSArray class] ];");
12164 verifyFormat("@[ [foo enum] ];");
12165
12166 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12167
12168 // Make sure we do not parse attributes as lambda introducers.
12169 FormatStyle MultiLineFunctions = getLLVMStyle();
12170 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12171 verifyFormat("[[unused]] int b() {\n"
12172 " return 42;\n"
12173 "}",
12174 MultiLineFunctions);
12175}
12176
12177TEST_F(FormatTest, AttributeClass) {
12178 FormatStyle Style = getChromiumStyle(Language: FormatStyle::LK_Cpp);
12179 verifyFormat("class S {\n"
12180 " S(S&&) = default;\n"
12181 "};",
12182 Style);
12183 verifyFormat("class [[nodiscard]] S {\n"
12184 " S(S&&) = default;\n"
12185 "};",
12186 Style);
12187 verifyFormat("class __attribute((maybeunused)) S {\n"
12188 " S(S&&) = default;\n"
12189 "};",
12190 Style);
12191 verifyFormat("struct S {\n"
12192 " S(S&&) = default;\n"
12193 "};",
12194 Style);
12195 verifyFormat("struct [[nodiscard]] S {\n"
12196 " S(S&&) = default;\n"
12197 "};",
12198 Style);
12199}
12200
12201TEST_F(FormatTest, AttributesAfterMacro) {
12202 FormatStyle Style = getLLVMStyle();
12203 verifyFormat("MACRO;\n"
12204 "__attribute__((maybe_unused)) int foo() {\n"
12205 " //...\n"
12206 "}");
12207
12208 verifyFormat("MACRO;\n"
12209 "[[nodiscard]] int foo() {\n"
12210 " //...\n"
12211 "}");
12212
12213 verifyNoChange("MACRO\n\n"
12214 "__attribute__((maybe_unused)) int foo() {\n"
12215 " //...\n"
12216 "}");
12217
12218 verifyNoChange("MACRO\n\n"
12219 "[[nodiscard]] int foo() {\n"
12220 " //...\n"
12221 "}");
12222}
12223
12224TEST_F(FormatTest, AttributePenaltyBreaking) {
12225 FormatStyle Style = getLLVMStyle();
12226 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12227 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12228 Style);
12229 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12230 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12231 Style);
12232 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12233 "shared_ptr<ALongTypeName> &C d) {\n}",
12234 Style);
12235}
12236
12237TEST_F(FormatTest, UnderstandsEllipsis) {
12238 FormatStyle Style = getLLVMStyle();
12239 verifyFormat("int printf(const char *fmt, ...);");
12240 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12241 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12242
12243 verifyFormat("template <int *...PP> a;", Style);
12244
12245 Style.PointerAlignment = FormatStyle::PAS_Left;
12246 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12247
12248 verifyFormat("template <int*... PP> a;", Style);
12249
12250 Style.PointerAlignment = FormatStyle::PAS_Middle;
12251 verifyFormat("template <int *... PP> a;", Style);
12252}
12253
12254TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12255 verifyFormat("int *a;\n"
12256 "int *a;\n"
12257 "int *a;",
12258 "int *a;\n"
12259 "int* a;\n"
12260 "int *a;",
12261 getGoogleStyle());
12262 verifyFormat("int* a;\n"
12263 "int* a;\n"
12264 "int* a;",
12265 "int* a;\n"
12266 "int* a;\n"
12267 "int *a;",
12268 getGoogleStyle());
12269 verifyFormat("int *a;\n"
12270 "int *a;\n"
12271 "int *a;",
12272 "int *a;\n"
12273 "int * a;\n"
12274 "int * a;",
12275 getGoogleStyle());
12276 verifyFormat("auto x = [] {\n"
12277 " int *a;\n"
12278 " int *a;\n"
12279 " int *a;\n"
12280 "};",
12281 "auto x=[]{int *a;\n"
12282 "int * a;\n"
12283 "int * a;};",
12284 getGoogleStyle());
12285}
12286
12287TEST_F(FormatTest, UnderstandsRvalueReferences) {
12288 verifyFormat("int f(int &&a) {}");
12289 verifyFormat("int f(int a, char &&b) {}");
12290 verifyFormat("void f() { int &&a = b; }");
12291 verifyGoogleFormat("int f(int a, char&& b) {}");
12292 verifyGoogleFormat("void f() { int&& a = b; }");
12293
12294 verifyIndependentOfContext("A<int &&> a;");
12295 verifyIndependentOfContext("A<int &&, int &&> a;");
12296 verifyGoogleFormat("A<int&&> a;");
12297 verifyGoogleFormat("A<int&&, int&&> a;");
12298
12299 // Not rvalue references:
12300 verifyFormat("template <bool B, bool C> class A {\n"
12301 " static_assert(B && C, \"Something is wrong\");\n"
12302 "};");
12303 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12304 verifyFormat("template <typename T> struct S {\n"
12305 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12306 "};");
12307 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12308 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12309 verifyFormat("#define A(a, b) (a && b)");
12310}
12311
12312TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
12313 verifyFormat("void f() {\n"
12314 " x[aaaaaaaaa -\n"
12315 " b] = 23;\n"
12316 "}",
12317 getLLVMStyleWithColumns(15));
12318}
12319
12320TEST_F(FormatTest, FormatsCasts) {
12321 verifyFormat("Type *A = static_cast<Type *>(P);");
12322 verifyFormat("static_cast<Type *>(P);");
12323 verifyFormat("static_cast<Type &>(Fun)(Args);");
12324 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12325 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12326 // Check that static_cast<...>(...) does not require the next token to be on
12327 // the same line.
12328 verifyFormat("some_loooong_output << something_something__ << "
12329 "static_cast<const void *>(R)\n"
12330 " << something;");
12331 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12332 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12333 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12334 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12335 verifyFormat("Type *A = (Type *)P;");
12336 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12337 verifyFormat("int a = (int)(2.0f);");
12338 verifyFormat("int a = (int)2.0f;");
12339 verifyFormat("x[(int32)y];");
12340 verifyFormat("x = (int32)y;");
12341 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12342 verifyFormat("int a = (int)*b;");
12343 verifyFormat("int a = (int)2.0f;");
12344 verifyFormat("int a = (int)~0;");
12345 verifyFormat("int a = (int)++a;");
12346 verifyFormat("int a = (int)sizeof(int);");
12347 verifyFormat("int a = (int)+2;");
12348 verifyFormat("my_int a = (my_int)2.0f;");
12349 verifyFormat("my_int a = (my_int)sizeof(int);");
12350 verifyFormat("return (my_int)aaa;");
12351 verifyFormat("throw (my_int)aaa;");
12352 verifyFormat("#define x ((int)-1)");
12353 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12354 verifyFormat("#define p(q) ((int *)&q)");
12355 verifyFormat("fn(a)(b) + 1;");
12356
12357 verifyFormat("void f() { my_int a = (my_int)*b; }");
12358 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12359 verifyFormat("my_int a = (my_int)~0;");
12360 verifyFormat("my_int a = (my_int)++a;");
12361 verifyFormat("my_int a = (my_int)-2;");
12362 verifyFormat("my_int a = (my_int)1;");
12363 verifyFormat("my_int a = (my_int *)1;");
12364 verifyFormat("my_int a = (const my_int)-1;");
12365 verifyFormat("my_int a = (const my_int *)-1;");
12366 verifyFormat("my_int a = (my_int)(my_int)-1;");
12367 verifyFormat("my_int a = (ns::my_int)-2;");
12368 verifyFormat("case (my_int)ONE:");
12369 verifyFormat("auto x = (X)this;");
12370 // Casts in Obj-C style calls used to not be recognized as such.
12371 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12372
12373 // FIXME: single value wrapped with paren will be treated as cast.
12374 verifyFormat("void f(int i = (kValue)*kMask) {}");
12375
12376 verifyFormat("{\n"
12377 " (void)F;\n"
12378 "}");
12379
12380 // Don't break after a cast's
12381 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12382 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12383 " bbbbbbbbbbbbbbbbbbbbbb);");
12384
12385 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12386 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12387 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12388 verifyFormat("bool *y = (bool *)(void *)(x);");
12389 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12390 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12391 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12392 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12393
12394 // These are not casts.
12395 verifyFormat("void f(int *) {}");
12396 verifyFormat("f(foo)->b;");
12397 verifyFormat("f(foo).b;");
12398 verifyFormat("f(foo)(b);");
12399 verifyFormat("f(foo)[b];");
12400 verifyFormat("[](foo) { return 4; }(bar);");
12401 verifyFormat("(*funptr)(foo)[4];");
12402 verifyFormat("funptrs[4](foo)[4];");
12403 verifyFormat("void f(int *);");
12404 verifyFormat("void f(int *) = 0;");
12405 verifyFormat("void f(SmallVector<int>) {}");
12406 verifyFormat("void f(SmallVector<int>);");
12407 verifyFormat("void f(SmallVector<int>) = 0;");
12408 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12409 verifyFormat("int a = sizeof(int) * b;");
12410 verifyGoogleFormat("int a = alignof(int) * b;");
12411 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12412 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12413 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12414
12415 // These are not casts, but at some point were confused with casts.
12416 verifyFormat("virtual void foo(int *) override;");
12417 verifyFormat("virtual void foo(char &) const;");
12418 verifyFormat("virtual void foo(int *a, char *) const;");
12419 verifyFormat("int a = sizeof(int *) + b;");
12420 verifyGoogleFormat("int a = alignof(int *) + b;");
12421 verifyFormat("bool b = f(g<int>) && c;");
12422 verifyFormat("typedef void (*f)(int i) func;");
12423 verifyFormat("void operator++(int) noexcept;");
12424 verifyFormat("void operator++(int &) noexcept;");
12425 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12426 "&) noexcept;");
12427 verifyFormat(
12428 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12429 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12430 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12431 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12432 verifyFormat("void operator delete(foo &) noexcept;");
12433 verifyFormat("void operator delete(foo) noexcept;");
12434 verifyFormat("void operator delete(int) noexcept;");
12435 verifyFormat("void operator delete(int &) noexcept;");
12436 verifyFormat("void operator delete(int &) volatile noexcept;");
12437 verifyFormat("void operator delete(int &) const");
12438 verifyFormat("void operator delete(int &) = default");
12439 verifyFormat("void operator delete(int &) = delete");
12440 verifyFormat("void operator delete(int &) [[noreturn]]");
12441 verifyFormat("void operator delete(int &) throw();");
12442 verifyFormat("void operator delete(int &) throw(int);");
12443 verifyFormat("auto operator delete(int &) -> int;");
12444 verifyFormat("auto operator delete(int &) override");
12445 verifyFormat("auto operator delete(int &) final");
12446
12447 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12448 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12449 // FIXME: The indentation here is not ideal.
12450 verifyFormat(
12451 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12452 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12453 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12454}
12455
12456TEST_F(FormatTest, FormatsFunctionTypes) {
12457 verifyFormat("A<bool()> a;");
12458 verifyFormat("A<SomeType()> a;");
12459 verifyFormat("A<void (*)(int, std::string)> a;");
12460 verifyFormat("A<void *(int)>;");
12461 verifyFormat("void *(*a)(int *, SomeType *);");
12462 verifyFormat("int (*func)(void *);");
12463 verifyFormat("void f() { int (*func)(void *); }");
12464 verifyFormat("template <class CallbackClass>\n"
12465 "using Callback = void (CallbackClass:: *)(SomeObject *Data);");
12466
12467 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12468 verifyGoogleFormat("void* (*a)(int);");
12469 verifyGoogleFormat(
12470 "template <class CallbackClass>\n"
12471 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12472
12473 // Other constructs can look somewhat like function types:
12474 verifyFormat("A<sizeof(*x)> a;");
12475 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12476 verifyFormat("some_var = function(*some_pointer_var)[0];");
12477 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12478 verifyFormat("int x = f(&h)();");
12479 verifyFormat("returnsFunction(&param1, &param2)(param);");
12480 verifyFormat("std::function<\n"
12481 " LooooooooooongTemplatedType<\n"
12482 " SomeType>*(\n"
12483 " LooooooooooooooooongType type)>\n"
12484 " function;",
12485 getGoogleStyleWithColumns(40));
12486}
12487
12488TEST_F(FormatTest, FormatsPointersToArrayTypes) {
12489 verifyFormat("A (*foo_)[6];");
12490 verifyFormat("vector<int> (*foo_)[6];");
12491}
12492
12493TEST_F(FormatTest, BreaksLongVariableDeclarations) {
12494 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12495 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12496 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12497 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12498 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12499 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12500
12501 // Different ways of ()-initializiation.
12502 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12503 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12504 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12505 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12506 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12507 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12508 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12509 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12510
12511 // Lambdas should not confuse the variable declaration heuristic.
12512 verifyFormat("LooooooooooooooooongType\n"
12513 " variable(nullptr, [](A *a) {});",
12514 getLLVMStyleWithColumns(40));
12515}
12516
12517TEST_F(FormatTest, BreaksLongDeclarations) {
12518 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12519 " AnotherNameForTheLongType;");
12520 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12521 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12522 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12523 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12524 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12525 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12526 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12527 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12528 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12529 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12530 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12531 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12532 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12533 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12534 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12535 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12536 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12537 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12538 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12539 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12540 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12541 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12542 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12543 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12544 FormatStyle Indented = getLLVMStyle();
12545 Indented.IndentWrappedFunctionNames = true;
12546 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12547 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12548 Indented);
12549 verifyFormat(
12550 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12551 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12552 Indented);
12553 verifyFormat(
12554 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12555 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12556 Indented);
12557 verifyFormat(
12558 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12559 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12560 Indented);
12561
12562 // FIXME: Without the comment, this breaks after "(".
12563 verifyGoogleFormat(
12564 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12565 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12566
12567 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12568 " int LoooooooooooooooooooongParam2) {}");
12569 verifyFormat(
12570 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12571 " SourceLocation L, IdentifierIn *II,\n"
12572 " Type *T) {}");
12573 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12574 "ReallyReaaallyLongFunctionName(\n"
12575 " const std::string &SomeParameter,\n"
12576 " const SomeType<string, SomeOtherTemplateParameter>\n"
12577 " &ReallyReallyLongParameterName,\n"
12578 " const SomeType<string, SomeOtherTemplateParameter>\n"
12579 " &AnotherLongParameterName) {}");
12580 verifyFormat("template <typename A>\n"
12581 "SomeLoooooooooooooooooooooongType<\n"
12582 " typename some_namespace::SomeOtherType<A>::Type>\n"
12583 "Function() {}");
12584
12585 verifyGoogleFormat(
12586 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12587 " aaaaaaaaaaaaaaaaaaaaaaa;");
12588 verifyGoogleFormat(
12589 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12590 " SourceLocation L) {}");
12591 verifyGoogleFormat(
12592 "some_namespace::LongReturnType\n"
12593 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12594 " int first_long_parameter, int second_parameter) {}");
12595
12596 verifyGoogleFormat("template <typename T>\n"
12597 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12598 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12599 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12600 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12601
12602 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12603 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12604 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12605 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12606 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12607 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12608 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12609 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12612
12613 verifyFormat("template <typename T> // Templates on own line.\n"
12614 "static int // Some comment.\n"
12615 "MyFunction(int a);");
12616}
12617
12618TEST_F(FormatTest, FormatsAccessModifiers) {
12619 FormatStyle Style = getLLVMStyle();
12620 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
12621 FormatStyle::ELBAMS_LogicalBlock);
12622 verifyFormat("struct foo {\n"
12623 "private:\n"
12624 " void f() {}\n"
12625 "\n"
12626 "private:\n"
12627 " int i;\n"
12628 "\n"
12629 "protected:\n"
12630 " int j;\n"
12631 "};",
12632 Style);
12633 verifyFormat("struct foo {\n"
12634 "private:\n"
12635 " void f() {}\n"
12636 "\n"
12637 "private:\n"
12638 " int i;\n"
12639 "\n"
12640 "protected:\n"
12641 " int j;\n"
12642 "};",
12643 "struct foo {\n"
12644 "private:\n"
12645 " void f() {}\n"
12646 "private:\n"
12647 " int i;\n"
12648 "protected:\n"
12649 " int j;\n"
12650 "};",
12651 Style);
12652 verifyFormat("struct foo { /* comment */\n"
12653 "private:\n"
12654 " int i;\n"
12655 " // comment\n"
12656 "private:\n"
12657 " int j;\n"
12658 "};",
12659 Style);
12660 verifyFormat("struct foo {\n"
12661 "#ifdef FOO\n"
12662 "#endif\n"
12663 "private:\n"
12664 " int i;\n"
12665 "#ifdef FOO\n"
12666 "private:\n"
12667 "#endif\n"
12668 " int j;\n"
12669 "};",
12670 Style);
12671 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12672 verifyFormat("struct foo {\n"
12673 "private:\n"
12674 " void f() {}\n"
12675 "private:\n"
12676 " int i;\n"
12677 "protected:\n"
12678 " int j;\n"
12679 "};",
12680 Style);
12681 verifyFormat("struct foo {\n"
12682 "private:\n"
12683 " void f() {}\n"
12684 "private:\n"
12685 " int i;\n"
12686 "protected:\n"
12687 " int j;\n"
12688 "};",
12689 "struct foo {\n"
12690 "\n"
12691 "private:\n"
12692 " void f() {}\n"
12693 "\n"
12694 "private:\n"
12695 " int i;\n"
12696 "\n"
12697 "protected:\n"
12698 " int j;\n"
12699 "};",
12700 Style);
12701 verifyFormat("struct foo { /* comment */\n"
12702 "private:\n"
12703 " int i;\n"
12704 " // comment\n"
12705 "private:\n"
12706 " int j;\n"
12707 "};",
12708 "struct foo { /* comment */\n"
12709 "\n"
12710 "private:\n"
12711 " int i;\n"
12712 " // comment\n"
12713 "\n"
12714 "private:\n"
12715 " int j;\n"
12716 "};",
12717 Style);
12718 verifyFormat("struct foo {\n"
12719 "#ifdef FOO\n"
12720 "#endif\n"
12721 "private:\n"
12722 " int i;\n"
12723 "#ifdef FOO\n"
12724 "private:\n"
12725 "#endif\n"
12726 " int j;\n"
12727 "};",
12728 "struct foo {\n"
12729 "#ifdef FOO\n"
12730 "#endif\n"
12731 "\n"
12732 "private:\n"
12733 " int i;\n"
12734 "#ifdef FOO\n"
12735 "\n"
12736 "private:\n"
12737 "#endif\n"
12738 " int j;\n"
12739 "};",
12740 Style);
12741 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12742 verifyFormat("struct foo {\n"
12743 "private:\n"
12744 " void f() {}\n"
12745 "\n"
12746 "private:\n"
12747 " int i;\n"
12748 "\n"
12749 "protected:\n"
12750 " int j;\n"
12751 "};",
12752 Style);
12753 verifyFormat("struct foo {\n"
12754 "private:\n"
12755 " void f() {}\n"
12756 "\n"
12757 "private:\n"
12758 " int i;\n"
12759 "\n"
12760 "protected:\n"
12761 " int j;\n"
12762 "};",
12763 "struct foo {\n"
12764 "private:\n"
12765 " void f() {}\n"
12766 "private:\n"
12767 " int i;\n"
12768 "protected:\n"
12769 " int j;\n"
12770 "};",
12771 Style);
12772 verifyFormat("struct foo { /* comment */\n"
12773 "private:\n"
12774 " int i;\n"
12775 " // comment\n"
12776 "\n"
12777 "private:\n"
12778 " int j;\n"
12779 "};",
12780 Style);
12781 verifyFormat("struct foo {\n"
12782 "#ifdef FOO\n"
12783 "#endif\n"
12784 "\n"
12785 "private:\n"
12786 " int i;\n"
12787 "#ifdef FOO\n"
12788 "\n"
12789 "private:\n"
12790 "#endif\n"
12791 " int j;\n"
12792 "};",
12793 "struct foo {\n"
12794 "#ifdef FOO\n"
12795 "#endif\n"
12796 "private:\n"
12797 " int i;\n"
12798 "#ifdef FOO\n"
12799 "private:\n"
12800 "#endif\n"
12801 " int j;\n"
12802 "};",
12803 Style);
12804 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
12805 verifyNoChange("struct foo {\n"
12806 "\n"
12807 "private:\n"
12808 " void f() {}\n"
12809 "\n"
12810 "private:\n"
12811 " int i;\n"
12812 "\n"
12813 "protected:\n"
12814 " int j;\n"
12815 "};",
12816 Style);
12817 verifyFormat("struct foo {\n"
12818 "private:\n"
12819 " void f() {}\n"
12820 "private:\n"
12821 " int i;\n"
12822 "protected:\n"
12823 " int j;\n"
12824 "};",
12825 Style);
12826 verifyNoChange("struct foo { /* comment */\n"
12827 "\n"
12828 "private:\n"
12829 " int i;\n"
12830 " // comment\n"
12831 "\n"
12832 "private:\n"
12833 " int j;\n"
12834 "};",
12835 Style);
12836 verifyFormat("struct foo { /* comment */\n"
12837 "private:\n"
12838 " int i;\n"
12839 " // comment\n"
12840 "private:\n"
12841 " int j;\n"
12842 "};",
12843 Style);
12844 verifyNoChange("struct foo {\n"
12845 "#ifdef FOO\n"
12846 "#endif\n"
12847 "\n"
12848 "private:\n"
12849 " int i;\n"
12850 "#ifdef FOO\n"
12851 "\n"
12852 "private:\n"
12853 "#endif\n"
12854 " int j;\n"
12855 "};",
12856 Style);
12857 verifyFormat("struct foo {\n"
12858 "#ifdef FOO\n"
12859 "#endif\n"
12860 "private:\n"
12861 " int i;\n"
12862 "#ifdef FOO\n"
12863 "private:\n"
12864 "#endif\n"
12865 " int j;\n"
12866 "};",
12867 Style);
12868
12869 FormatStyle NoEmptyLines = getLLVMStyle();
12870 NoEmptyLines.MaxEmptyLinesToKeep = 0;
12871 verifyFormat("struct foo {\n"
12872 "private:\n"
12873 " void f() {}\n"
12874 "\n"
12875 "private:\n"
12876 " int i;\n"
12877 "\n"
12878 "public:\n"
12879 "protected:\n"
12880 " int j;\n"
12881 "};",
12882 NoEmptyLines);
12883
12884 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
12885 verifyFormat("struct foo {\n"
12886 "private:\n"
12887 " void f() {}\n"
12888 "private:\n"
12889 " int i;\n"
12890 "public:\n"
12891 "protected:\n"
12892 " int j;\n"
12893 "};",
12894 NoEmptyLines);
12895
12896 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
12897 verifyFormat("struct foo {\n"
12898 "private:\n"
12899 " void f() {}\n"
12900 "\n"
12901 "private:\n"
12902 " int i;\n"
12903 "\n"
12904 "public:\n"
12905 "\n"
12906 "protected:\n"
12907 " int j;\n"
12908 "};",
12909 NoEmptyLines);
12910}
12911
12912TEST_F(FormatTest, FormatsAfterAccessModifiers) {
12913
12914 FormatStyle Style = getLLVMStyle();
12915 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
12916 verifyFormat("struct foo {\n"
12917 "private:\n"
12918 " void f() {}\n"
12919 "\n"
12920 "private:\n"
12921 " int i;\n"
12922 "\n"
12923 "protected:\n"
12924 " int j;\n"
12925 "};",
12926 Style);
12927
12928 // Check if lines are removed.
12929 verifyFormat("struct foo {\n"
12930 "private:\n"
12931 " void f() {}\n"
12932 "\n"
12933 "private:\n"
12934 " int i;\n"
12935 "\n"
12936 "protected:\n"
12937 " int j;\n"
12938 "};",
12939 "struct foo {\n"
12940 "private:\n"
12941 "\n"
12942 " void f() {}\n"
12943 "\n"
12944 "private:\n"
12945 "\n"
12946 " int i;\n"
12947 "\n"
12948 "protected:\n"
12949 "\n"
12950 " int j;\n"
12951 "};",
12952 Style);
12953
12954 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
12955 verifyFormat("struct foo {\n"
12956 "private:\n"
12957 "\n"
12958 " void f() {}\n"
12959 "\n"
12960 "private:\n"
12961 "\n"
12962 " int i;\n"
12963 "\n"
12964 "protected:\n"
12965 "\n"
12966 " int j;\n"
12967 "};",
12968 Style);
12969
12970 // Check if lines are added.
12971 verifyFormat("struct foo {\n"
12972 "private:\n"
12973 "\n"
12974 " void f() {}\n"
12975 "\n"
12976 "private:\n"
12977 "\n"
12978 " int i;\n"
12979 "\n"
12980 "protected:\n"
12981 "\n"
12982 " int j;\n"
12983 "};",
12984 "struct foo {\n"
12985 "private:\n"
12986 " void f() {}\n"
12987 "\n"
12988 "private:\n"
12989 " int i;\n"
12990 "\n"
12991 "protected:\n"
12992 " int j;\n"
12993 "};",
12994 Style);
12995
12996 // Leave tests rely on the code layout, test::messUp can not be used.
12997 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
12998 Style.MaxEmptyLinesToKeep = 0u;
12999 verifyFormat("struct foo {\n"
13000 "private:\n"
13001 " void f() {}\n"
13002 "\n"
13003 "private:\n"
13004 " int i;\n"
13005 "\n"
13006 "protected:\n"
13007 " int j;\n"
13008 "};",
13009 Style);
13010
13011 // Check if MaxEmptyLinesToKeep is respected.
13012 verifyFormat("struct foo {\n"
13013 "private:\n"
13014 " void f() {}\n"
13015 "\n"
13016 "private:\n"
13017 " int i;\n"
13018 "\n"
13019 "protected:\n"
13020 " int j;\n"
13021 "};",
13022 "struct foo {\n"
13023 "private:\n"
13024 "\n\n\n"
13025 " void f() {}\n"
13026 "\n"
13027 "private:\n"
13028 "\n\n\n"
13029 " int i;\n"
13030 "\n"
13031 "protected:\n"
13032 "\n\n\n"
13033 " int j;\n"
13034 "};",
13035 Style);
13036
13037 Style.MaxEmptyLinesToKeep = 1u;
13038 verifyNoChange("struct foo {\n"
13039 "private:\n"
13040 "\n"
13041 " void f() {}\n"
13042 "\n"
13043 "private:\n"
13044 "\n"
13045 " int i;\n"
13046 "\n"
13047 "protected:\n"
13048 "\n"
13049 " int j;\n"
13050 "};",
13051 Style);
13052 // Check if no lines are kept.
13053 verifyFormat("struct foo {\n"
13054 "private:\n"
13055 " void f() {}\n"
13056 "\n"
13057 "private:\n"
13058 " int i;\n"
13059 "\n"
13060 "protected:\n"
13061 " int j;\n"
13062 "};",
13063 Style);
13064 // Check if MaxEmptyLinesToKeep is respected.
13065 verifyFormat("struct foo {\n"
13066 "private:\n"
13067 "\n"
13068 " void f() {}\n"
13069 "\n"
13070 "private:\n"
13071 "\n"
13072 " int i;\n"
13073 "\n"
13074 "protected:\n"
13075 "\n"
13076 " int j;\n"
13077 "};",
13078 "struct foo {\n"
13079 "private:\n"
13080 "\n\n\n"
13081 " void f() {}\n"
13082 "\n"
13083 "private:\n"
13084 "\n\n\n"
13085 " int i;\n"
13086 "\n"
13087 "protected:\n"
13088 "\n\n\n"
13089 " int j;\n"
13090 "};",
13091 Style);
13092
13093 Style.MaxEmptyLinesToKeep = 10u;
13094 verifyNoChange("struct foo {\n"
13095 "private:\n"
13096 "\n\n\n"
13097 " void f() {}\n"
13098 "\n"
13099 "private:\n"
13100 "\n\n\n"
13101 " int i;\n"
13102 "\n"
13103 "protected:\n"
13104 "\n\n\n"
13105 " int j;\n"
13106 "};",
13107 Style);
13108
13109 // Test with comments.
13110 Style = getLLVMStyle();
13111 verifyFormat("struct foo {\n"
13112 "private:\n"
13113 " // comment\n"
13114 " void f() {}\n"
13115 "\n"
13116 "private: /* comment */\n"
13117 " int i;\n"
13118 "};",
13119 Style);
13120 verifyFormat("struct foo {\n"
13121 "private:\n"
13122 " // comment\n"
13123 " void f() {}\n"
13124 "\n"
13125 "private: /* comment */\n"
13126 " int i;\n"
13127 "};",
13128 "struct foo {\n"
13129 "private:\n"
13130 "\n"
13131 " // comment\n"
13132 " void f() {}\n"
13133 "\n"
13134 "private: /* comment */\n"
13135 "\n"
13136 " int i;\n"
13137 "};",
13138 Style);
13139
13140 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13141 verifyFormat("struct foo {\n"
13142 "private:\n"
13143 "\n"
13144 " // comment\n"
13145 " void f() {}\n"
13146 "\n"
13147 "private: /* comment */\n"
13148 "\n"
13149 " int i;\n"
13150 "};",
13151 "struct foo {\n"
13152 "private:\n"
13153 " // comment\n"
13154 " void f() {}\n"
13155 "\n"
13156 "private: /* comment */\n"
13157 " int i;\n"
13158 "};",
13159 Style);
13160 verifyFormat("struct foo {\n"
13161 "private:\n"
13162 "\n"
13163 " // comment\n"
13164 " void f() {}\n"
13165 "\n"
13166 "private: /* comment */\n"
13167 "\n"
13168 " int i;\n"
13169 "};",
13170 Style);
13171
13172 // Test with preprocessor defines.
13173 Style = getLLVMStyle();
13174 verifyFormat("struct foo {\n"
13175 "private:\n"
13176 "#ifdef FOO\n"
13177 "#endif\n"
13178 " void f() {}\n"
13179 "};",
13180 Style);
13181 verifyFormat("struct foo {\n"
13182 "private:\n"
13183 "#ifdef FOO\n"
13184 "#endif\n"
13185 " void f() {}\n"
13186 "};",
13187 "struct foo {\n"
13188 "private:\n"
13189 "\n"
13190 "#ifdef FOO\n"
13191 "#endif\n"
13192 " void f() {}\n"
13193 "};",
13194 Style);
13195 verifyNoChange("struct foo {\n"
13196 "#ifdef FOO\n"
13197 "#else\n"
13198 "private:\n"
13199 "\n"
13200 "#endif\n"
13201 "};",
13202 Style);
13203 verifyFormat("struct foo {\n"
13204 "#ifdef FOO\n"
13205 "#else\n"
13206 "private:\n"
13207 "\n"
13208 "#endif\n"
13209 "};",
13210 "struct foo {\n"
13211 "#ifdef FOO\n"
13212 "#else\n"
13213 "private:\n"
13214 "\n"
13215 "\n"
13216 "#endif\n"
13217 "};",
13218 Style);
13219 verifyFormat("struct foo {\n"
13220 "#ifdef FOO\n"
13221 "private:\n"
13222 "#else\n"
13223 "#endif\n"
13224 "};",
13225 "struct foo {\n"
13226 "#ifdef FOO\n"
13227 "private:\n"
13228 "\n"
13229 "\n"
13230 "#else\n"
13231 "#endif\n"
13232 "};",
13233 Style);
13234 verifyFormat("struct foo {\n"
13235 "#if 0\n"
13236 "#else\n"
13237 "#endif\n"
13238 "#ifdef FOO\n"
13239 "private:\n"
13240 "#endif\n"
13241 "};",
13242 "struct foo {\n"
13243 "#if 0\n"
13244 "#else\n"
13245 "#endif\n"
13246 "#ifdef FOO\n"
13247 "private:\n"
13248 "\n"
13249 "\n"
13250 "#endif\n"
13251 "};",
13252 Style);
13253
13254 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13255 verifyFormat("struct foo {\n"
13256 "private:\n"
13257 "\n"
13258 "#ifdef FOO\n"
13259 "#endif\n"
13260 " void f() {}\n"
13261 "};",
13262 "struct foo {\n"
13263 "private:\n"
13264 "#ifdef FOO\n"
13265 "#endif\n"
13266 " void f() {}\n"
13267 "};",
13268 Style);
13269 verifyFormat("struct foo {\n"
13270 "private:\n"
13271 "\n"
13272 "#ifdef FOO\n"
13273 "#endif\n"
13274 " void f() {}\n"
13275 "};",
13276 Style);
13277}
13278
13279TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13280 // Combined tests of EmptyLineAfterAccessModifier and
13281 // EmptyLineBeforeAccessModifier.
13282 FormatStyle Style = getLLVMStyle();
13283 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13284 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13285 verifyFormat("struct foo {\n"
13286 "private:\n"
13287 "\n"
13288 "protected:\n"
13289 "};",
13290 Style);
13291
13292 Style.MaxEmptyLinesToKeep = 10u;
13293 // Both remove all new lines.
13294 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13295 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13296 verifyFormat("struct foo {\n"
13297 "private:\n"
13298 "protected:\n"
13299 "};",
13300 "struct foo {\n"
13301 "private:\n"
13302 "\n\n\n"
13303 "protected:\n"
13304 "};",
13305 Style);
13306
13307 // Leave tests rely on the code layout, test::messUp can not be used.
13308 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13309 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13310 Style.MaxEmptyLinesToKeep = 10u;
13311 verifyNoChange("struct foo {\n"
13312 "private:\n"
13313 "\n\n\n"
13314 "protected:\n"
13315 "};",
13316 Style);
13317 Style.MaxEmptyLinesToKeep = 3u;
13318 verifyNoChange("struct foo {\n"
13319 "private:\n"
13320 "\n\n\n"
13321 "protected:\n"
13322 "};",
13323 Style);
13324 Style.MaxEmptyLinesToKeep = 1u;
13325 verifyNoChange("struct foo {\n"
13326 "private:\n"
13327 "\n\n\n"
13328 "protected:\n"
13329 "};",
13330 Style); // Based on new lines in original document and not
13331 // on the setting.
13332
13333 Style.MaxEmptyLinesToKeep = 10u;
13334 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13335 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13336 // Newlines are kept if they are greater than zero,
13337 // test::messUp removes all new lines which changes the logic
13338 verifyNoChange("struct foo {\n"
13339 "private:\n"
13340 "\n\n\n"
13341 "protected:\n"
13342 "};",
13343 Style);
13344
13345 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13346 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13347 // test::messUp removes all new lines which changes the logic
13348 verifyNoChange("struct foo {\n"
13349 "private:\n"
13350 "\n\n\n"
13351 "protected:\n"
13352 "};",
13353 Style);
13354
13355 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13356 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13357 verifyNoChange("struct foo {\n"
13358 "private:\n"
13359 "\n\n\n"
13360 "protected:\n"
13361 "};",
13362 Style); // test::messUp removes all new lines which changes
13363 // the logic.
13364
13365 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13366 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13367 verifyFormat("struct foo {\n"
13368 "private:\n"
13369 "protected:\n"
13370 "};",
13371 "struct foo {\n"
13372 "private:\n"
13373 "\n\n\n"
13374 "protected:\n"
13375 "};",
13376 Style);
13377
13378 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13379 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13380 verifyNoChange("struct foo {\n"
13381 "private:\n"
13382 "\n\n\n"
13383 "protected:\n"
13384 "};",
13385 Style); // test::messUp removes all new lines which changes
13386 // the logic.
13387
13388 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13389 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13390 verifyFormat("struct foo {\n"
13391 "private:\n"
13392 "protected:\n"
13393 "};",
13394 "struct foo {\n"
13395 "private:\n"
13396 "\n\n\n"
13397 "protected:\n"
13398 "};",
13399 Style);
13400
13401 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13402 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13403 verifyFormat("struct foo {\n"
13404 "private:\n"
13405 "protected:\n"
13406 "};",
13407 "struct foo {\n"
13408 "private:\n"
13409 "\n\n\n"
13410 "protected:\n"
13411 "};",
13412 Style);
13413
13414 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13415 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13416 verifyFormat("struct foo {\n"
13417 "private:\n"
13418 "protected:\n"
13419 "};",
13420 "struct foo {\n"
13421 "private:\n"
13422 "\n\n\n"
13423 "protected:\n"
13424 "};",
13425 Style);
13426
13427 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
13428 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13429 verifyFormat("struct foo {\n"
13430 "private:\n"
13431 "protected:\n"
13432 "};",
13433 "struct foo {\n"
13434 "private:\n"
13435 "\n\n\n"
13436 "protected:\n"
13437 "};",
13438 Style);
13439}
13440
13441TEST_F(FormatTest, FormatsArrays) {
13442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13443 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13444 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13445 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13446 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13447 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13448 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13449 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13450 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13451 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13452 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13453 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13454 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13455 verifyFormat(
13456 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13457 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13458 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13459 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13460 " .aaaaaaaaaaaaaaaaaaaaaa();");
13461
13462 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13464 verifyFormat(
13465 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13466 " .aaaaaaa[0]\n"
13467 " .aaaaaaaaaaaaaaaaaaaaaa();");
13468 verifyFormat("a[::b::c];");
13469
13470 verifyNoCrash(Code: "a[,Y?)]", Style: getLLVMStyleWithColumns(ColumnLimit: 10));
13471
13472 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(ColumnLimit: 0);
13473 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
13474}
13475
13476TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
13477 verifyFormat("(a)->b();");
13478 verifyFormat("--a;");
13479}
13480
13481TEST_F(FormatTest, HandlesIncludeDirectives) {
13482 verifyFormat("#include <string>\n"
13483 "#include <a/b/c.h>\n"
13484 "#include \"a/b/string\"\n"
13485 "#include \"string.h\"\n"
13486 "#include \"string.h\"\n"
13487 "#include <a-a>\n"
13488 "#include < path with space >\n"
13489 "#include_next <test.h>"
13490 "#include \"abc.h\" // this is included for ABC\n"
13491 "#include \"some long include\" // with a comment\n"
13492 "#include \"some very long include path\"\n"
13493 "#include <some/very/long/include/path>",
13494 getLLVMStyleWithColumns(35));
13495 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13496 verifyFormat("#include <a>", "#include<a>");
13497
13498 verifyFormat("#import <string>");
13499 verifyFormat("#import <a/b/c.h>");
13500 verifyFormat("#import \"a/b/string\"");
13501 verifyFormat("#import \"string.h\"");
13502 verifyFormat("#import \"string.h\"");
13503 verifyFormat("#if __has_include(<strstream>)\n"
13504 "#include <strstream>\n"
13505 "#endif");
13506
13507 verifyFormat("#define MY_IMPORT <a/b>");
13508
13509 verifyFormat("#if __has_include(<a/b>)");
13510 verifyFormat("#if __has_include_next(<a/b>)");
13511 verifyFormat("#define F __has_include(<a/b>)");
13512 verifyFormat("#define F __has_include_next(<a/b>)");
13513
13514 // Protocol buffer definition or missing "#".
13515 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13516 getLLVMStyleWithColumns(30));
13517
13518 FormatStyle Style = getLLVMStyle();
13519 Style.AlwaysBreakBeforeMultilineStrings = true;
13520 Style.ColumnLimit = 0;
13521 verifyFormat("#import \"abc.h\"", Style);
13522
13523 // But 'import' might also be a regular C++ namespace.
13524 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13526 verifyFormat("import::Bar foo(val ? 2 : 1);");
13527}
13528
13529//===----------------------------------------------------------------------===//
13530// Error recovery tests.
13531//===----------------------------------------------------------------------===//
13532
13533TEST_F(FormatTest, IncompleteParameterLists) {
13534 FormatStyle NoBinPacking = getLLVMStyle();
13535 NoBinPacking.BinPackParameters = false;
13536 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13537 " double *min_x,\n"
13538 " double *max_x,\n"
13539 " double *min_y,\n"
13540 " double *max_y,\n"
13541 " double *min_z,\n"
13542 " double *max_z, ) {}",
13543 NoBinPacking);
13544}
13545
13546TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
13547 verifyFormat("void f() { return; }\n42");
13548 verifyFormat("void f() {\n"
13549 " if (0)\n"
13550 " return;\n"
13551 "}\n"
13552 "42");
13553 verifyFormat("void f() { return }\n42");
13554 verifyFormat("void f() {\n"
13555 " if (0)\n"
13556 " return\n"
13557 "}\n"
13558 "42");
13559}
13560
13561TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
13562 verifyFormat("void f() { return }", "void f ( ) { return }");
13563 verifyFormat("void f() {\n"
13564 " if (a)\n"
13565 " return\n"
13566 "}",
13567 "void f ( ) { if ( a ) return }");
13568 verifyFormat("namespace N {\n"
13569 "void f()\n"
13570 "}",
13571 "namespace N { void f() }");
13572 verifyFormat("namespace N {\n"
13573 "void f() {}\n"
13574 "void g()\n"
13575 "} // namespace N",
13576 "namespace N { void f( ) { } void g( ) }");
13577}
13578
13579TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
13580 verifyFormat("int aaaaaaaa =\n"
13581 " // Overlylongcomment\n"
13582 " b;",
13583 getLLVMStyleWithColumns(20));
13584 verifyFormat("function(\n"
13585 " ShortArgument,\n"
13586 " LoooooooooooongArgument);",
13587 getLLVMStyleWithColumns(20));
13588}
13589
13590TEST_F(FormatTest, IncorrectAccessSpecifier) {
13591 verifyFormat("public:");
13592 verifyFormat("class A {\n"
13593 "public\n"
13594 " void f() {}\n"
13595 "};");
13596 verifyFormat("public\n"
13597 "int qwerty;");
13598 verifyFormat("public\n"
13599 "B {}");
13600 verifyFormat("public\n"
13601 "{\n"
13602 "}");
13603 verifyFormat("public\n"
13604 "B { int x; }");
13605}
13606
13607TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
13608 verifyFormat("{");
13609 verifyFormat("#})");
13610 verifyNoCrash(Code: "(/**/[:!] ?[).");
13611 verifyNoCrash(Code: "struct X {\n"
13612 " operator iunt(\n"
13613 "};");
13614 verifyNoCrash(Code: "struct Foo {\n"
13615 " operator foo(bar\n"
13616 "};");
13617}
13618
13619TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
13620 // Found by oss-fuzz:
13621 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13622 FormatStyle Style = getGoogleStyle(Language: FormatStyle::LK_Cpp);
13623 Style.ColumnLimit = 60;
13624 verifyNoCrash(
13625 Code: "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13626 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13627 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13628 Style);
13629}
13630
13631TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
13632 verifyFormat("do {\n}");
13633 verifyFormat("do {\n}\n"
13634 "f();");
13635 verifyFormat("do {\n}\n"
13636 "wheeee(fun);");
13637 verifyFormat("do {\n"
13638 " f();\n"
13639 "}");
13640}
13641
13642TEST_F(FormatTest, IncorrectCodeMissingParens) {
13643 verifyFormat("if {\n foo;\n foo();\n}");
13644 verifyFormat("switch {\n foo;\n foo();\n}");
13645 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13646 verifyIncompleteFormat("ERROR: for target;");
13647 verifyFormat("while {\n foo;\n foo();\n}");
13648 verifyFormat("do {\n foo;\n foo();\n} while;");
13649}
13650
13651TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
13652 verifyIncompleteFormat("namespace {\n"
13653 "class Foo { Foo (\n"
13654 "};\n"
13655 "} // namespace");
13656}
13657
13658TEST_F(FormatTest, IncorrectCodeErrorDetection) {
13659 verifyFormat("{\n"
13660 " {\n"
13661 " }",
13662 "{\n"
13663 "{\n"
13664 "}");
13665 verifyFormat("{\n"
13666 " {\n"
13667 " }",
13668 "{\n"
13669 " {\n"
13670 "}");
13671 verifyFormat("{\n"
13672 " {\n"
13673 " }");
13674 verifyFormat("{\n"
13675 " {\n"
13676 " }\n"
13677 "}\n"
13678 "}",
13679 "{\n"
13680 " {\n"
13681 " }\n"
13682 " }\n"
13683 "}");
13684
13685 verifyFormat("{\n"
13686 " {\n"
13687 " breakme(\n"
13688 " qwe);\n"
13689 " }",
13690 "{\n"
13691 " {\n"
13692 " breakme(qwe);\n"
13693 "}",
13694 getLLVMStyleWithColumns(10));
13695}
13696
13697TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
13698 verifyFormat("int x = {\n"
13699 " avariable,\n"
13700 " b(alongervariable)};",
13701 getLLVMStyleWithColumns(25));
13702}
13703
13704TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
13705 verifyFormat("return (a)(b){1, 2, 3};");
13706}
13707
13708TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
13709 verifyFormat("vector<int> x{1, 2, 3, 4};");
13710 verifyFormat("vector<int> x{\n"
13711 " 1,\n"
13712 " 2,\n"
13713 " 3,\n"
13714 " 4,\n"
13715 "};");
13716 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13717 verifyFormat("f({1, 2});");
13718 verifyFormat("auto v = Foo{-1};");
13719 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13720 verifyFormat("Class::Class : member{1, 2, 3} {}");
13721 verifyFormat("new vector<int>{1, 2, 3};");
13722 verifyFormat("new int[3]{1, 2, 3};");
13723 verifyFormat("new int{1};");
13724 verifyFormat("return {arg1, arg2};");
13725 verifyFormat("return {arg1, SomeType{parameter}};");
13726 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13727 verifyFormat("new T{arg1, arg2};");
13728 verifyFormat("f(MyMap[{composite, key}]);");
13729 verifyFormat("class Class {\n"
13730 " T member = {arg1, arg2};\n"
13731 "};");
13732 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13733 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13734 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13735 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13736 verifyFormat("int a = std::is_integral<int>{} + 0;");
13737
13738 verifyFormat("int foo(int i) { return fo1{}(i); }");
13739 verifyFormat("int foo(int i) { return fo1{}(i); }");
13740 verifyFormat("auto i = decltype(x){};");
13741 verifyFormat("auto i = typeof(x){};");
13742 verifyFormat("auto i = _Atomic(x){};");
13743 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13744 verifyFormat("Node n{1, Node{1000}, //\n"
13745 " 2};");
13746 verifyFormat("Aaaa aaaaaaa{\n"
13747 " {\n"
13748 " aaaa,\n"
13749 " },\n"
13750 "};");
13751 verifyFormat("class C : public D {\n"
13752 " SomeClass SC{2};\n"
13753 "};");
13754 verifyFormat("class C : public A {\n"
13755 " class D : public B {\n"
13756 " void f() { int i{2}; }\n"
13757 " };\n"
13758 "};");
13759 verifyFormat("#define A {a, a},");
13760 // Don't confuse braced list initializers with compound statements.
13761 verifyFormat(
13762 "class A {\n"
13763 " A() : a{} {}\n"
13764 " A() : Base<int>{} {}\n"
13765 " A() : Base<Foo<int>>{} {}\n"
13766 " A(int b) : b(b) {}\n"
13767 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13768 " int a, b;\n"
13769 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13770 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13771 "{}\n"
13772 "};");
13773
13774 // Avoid breaking between equal sign and opening brace
13775 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
13776 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
13777 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13778 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13779 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13780 " {\"ccccccccccccccccccccc\", 2}};",
13781 AvoidBreakingFirstArgument);
13782
13783 // Binpacking only if there is no trailing comma
13784 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13785 " cccccccccc, dddddddddd};",
13786 getLLVMStyleWithColumns(50));
13787 verifyFormat("const Aaaaaa aaaaa = {\n"
13788 " aaaaaaaaaaa,\n"
13789 " bbbbbbbbbbb,\n"
13790 " ccccccccccc,\n"
13791 " ddddddddddd,\n"
13792 "};",
13793 getLLVMStyleWithColumns(50));
13794
13795 // Cases where distinguising braced lists and blocks is hard.
13796 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13797 verifyFormat("void f() {\n"
13798 " return; // comment\n"
13799 "}\n"
13800 "SomeType t;");
13801 verifyFormat("void f() {\n"
13802 " if (a) {\n"
13803 " f();\n"
13804 " }\n"
13805 "}\n"
13806 "SomeType t;");
13807
13808 // In combination with BinPackArguments = false.
13809 FormatStyle NoBinPacking = getLLVMStyle();
13810 NoBinPacking.BinPackArguments = false;
13811 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
13812 " bbbbb,\n"
13813 " ccccc,\n"
13814 " ddddd,\n"
13815 " eeeee,\n"
13816 " ffffff,\n"
13817 " ggggg,\n"
13818 " hhhhhh,\n"
13819 " iiiiii,\n"
13820 " jjjjjj,\n"
13821 " kkkkkk};",
13822 NoBinPacking);
13823 verifyFormat("const Aaaaaa aaaaa = {\n"
13824 " aaaaa,\n"
13825 " bbbbb,\n"
13826 " ccccc,\n"
13827 " ddddd,\n"
13828 " eeeee,\n"
13829 " ffffff,\n"
13830 " ggggg,\n"
13831 " hhhhhh,\n"
13832 " iiiiii,\n"
13833 " jjjjjj,\n"
13834 " kkkkkk,\n"
13835 "};",
13836 NoBinPacking);
13837 verifyFormat(
13838 "const Aaaaaa aaaaa = {\n"
13839 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
13840 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
13841 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
13842 "};",
13843 NoBinPacking);
13844
13845 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
13846 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
13847 " CDDDP83848_BMCR_REGISTER,\n"
13848 " CDDDP83848_BMSR_REGISTER,\n"
13849 " CDDDP83848_RBR_REGISTER};",
13850 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
13851 " CDDDP83848_BMSR_REGISTER,\n"
13852 " CDDDP83848_RBR_REGISTER};",
13853 NoBinPacking);
13854
13855 // FIXME: The alignment of these trailing comments might be bad. Then again,
13856 // this might be utterly useless in real code.
13857 verifyFormat("Constructor::Constructor()\n"
13858 " : some_value{ //\n"
13859 " aaaaaaa, //\n"
13860 " bbbbbbb} {}");
13861
13862 // In braced lists, the first comment is always assumed to belong to the
13863 // first element. Thus, it can be moved to the next or previous line as
13864 // appropriate.
13865 verifyFormat("function({// First element:\n"
13866 " 1,\n"
13867 " // Second element:\n"
13868 " 2});",
13869 "function({\n"
13870 " // First element:\n"
13871 " 1,\n"
13872 " // Second element:\n"
13873 " 2});");
13874 verifyFormat("std::vector<int> MyNumbers{\n"
13875 " // First element:\n"
13876 " 1,\n"
13877 " // Second element:\n"
13878 " 2};",
13879 "std::vector<int> MyNumbers{// First element:\n"
13880 " 1,\n"
13881 " // Second element:\n"
13882 " 2};",
13883 getLLVMStyleWithColumns(30));
13884 // A trailing comma should still lead to an enforced line break and no
13885 // binpacking.
13886 verifyFormat("vector<int> SomeVector = {\n"
13887 " // aaa\n"
13888 " 1,\n"
13889 " 2,\n"
13890 "};",
13891 "vector<int> SomeVector = { // aaa\n"
13892 " 1, 2, };");
13893
13894 // C++11 brace initializer list l-braces should not be treated any differently
13895 // when breaking before lambda bodies is enabled
13896 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
13897 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
13898 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
13899 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
13900 verifyFormat(
13901 "std::runtime_error{\n"
13902 " \"Long string which will force a break onto the next line...\"};",
13903 BreakBeforeLambdaBody);
13904
13905 FormatStyle ExtraSpaces = getLLVMStyle();
13906 ExtraSpaces.Cpp11BracedListStyle = false;
13907 ExtraSpaces.ColumnLimit = 75;
13908 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
13909 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
13910 verifyFormat("f({ 1, 2 });", ExtraSpaces);
13911 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
13912 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
13913 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
13914 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
13915 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
13916 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
13917 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
13918 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
13919 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
13920 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
13921 verifyFormat("class Class {\n"
13922 " T member = { arg1, arg2 };\n"
13923 "};",
13924 ExtraSpaces);
13925 verifyFormat(
13926 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13927 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
13928 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
13929 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
13930 ExtraSpaces);
13931 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
13932 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
13933 ExtraSpaces);
13934 verifyFormat(
13935 "someFunction(OtherParam,\n"
13936 " BracedList{ // comment 1 (Forcing interesting break)\n"
13937 " param1, param2,\n"
13938 " // comment 2\n"
13939 " param3, param4 });",
13940 ExtraSpaces);
13941 verifyFormat(
13942 "std::this_thread::sleep_for(\n"
13943 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
13944 ExtraSpaces);
13945 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
13946 " aaaaaaa,\n"
13947 " aaaaaaaaaa,\n"
13948 " aaaaa,\n"
13949 " aaaaaaaaaaaaaaa,\n"
13950 " aaa,\n"
13951 " aaaaaaaaaa,\n"
13952 " a,\n"
13953 " aaaaaaaaaaaaaaaaaaaaa,\n"
13954 " aaaaaaaaaaaa,\n"
13955 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
13956 " aaaaaaa,\n"
13957 " a};");
13958 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
13959 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
13960 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
13961
13962 // Avoid breaking between initializer/equal sign and opening brace
13963 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
13964 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
13965 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13966 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13967 " { \"ccccccccccccccccccccc\", 2 }\n"
13968 "};",
13969 ExtraSpaces);
13970 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
13971 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13972 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13973 " { \"ccccccccccccccccccccc\", 2 }\n"
13974 "};",
13975 ExtraSpaces);
13976
13977 FormatStyle SpaceBeforeBrace = getLLVMStyle();
13978 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
13979 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
13980 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
13981
13982 FormatStyle SpaceBetweenBraces = getLLVMStyle();
13983 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
13984 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
13985 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
13986 SpaceBetweenBraces.SpacesInSquareBrackets = true;
13987 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
13988 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
13989 verifyFormat("vector< int > x{ // comment 1\n"
13990 " 1, 2, 3, 4 };",
13991 SpaceBetweenBraces);
13992 SpaceBetweenBraces.ColumnLimit = 20;
13993 verifyFormat("vector< int > x{\n"
13994 " 1, 2, 3, 4 };",
13995 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
13996 SpaceBetweenBraces.ColumnLimit = 24;
13997 verifyFormat("vector< int > x{ 1, 2,\n"
13998 " 3, 4 };",
13999 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14000 verifyFormat("vector< int > x{\n"
14001 " 1,\n"
14002 " 2,\n"
14003 " 3,\n"
14004 " 4,\n"
14005 "};",
14006 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14007 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14008 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14009 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14010 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14011}
14012
14013TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14014 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14015 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14016 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14017 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14018 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14019 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14020 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14021 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14022 " 1, 22, 333, 4444, 55555, //\n"
14023 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14024 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14025 verifyFormat(
14026 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14027 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14028 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14029 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14030 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14031 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14032 " 7777777};");
14033 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14034 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14035 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14036 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14037 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14038 " // Separating comment.\n"
14039 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14040 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14041 " // Leading comment\n"
14042 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14043 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14044 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14045 " 1, 1, 1, 1};",
14046 getLLVMStyleWithColumns(39));
14047 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14048 " 1, 1, 1, 1};",
14049 getLLVMStyleWithColumns(38));
14050 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14051 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14052 getLLVMStyleWithColumns(43));
14053 verifyFormat(
14054 "static unsigned SomeValues[10][3] = {\n"
14055 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14056 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14057 verifyFormat("static auto fields = new vector<string>{\n"
14058 " \"aaaaaaaaaaaaa\",\n"
14059 " \"aaaaaaaaaaaaa\",\n"
14060 " \"aaaaaaaaaaaa\",\n"
14061 " \"aaaaaaaaaaaaaa\",\n"
14062 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14063 " \"aaaaaaaaaaaa\",\n"
14064 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14065 "};");
14066 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14067 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14068 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14069 " 3, cccccccccccccccccccccc};",
14070 getLLVMStyleWithColumns(60));
14071
14072 // Trailing commas.
14073 verifyFormat("vector<int> x = {\n"
14074 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14075 "};",
14076 getLLVMStyleWithColumns(39));
14077 verifyFormat("vector<int> x = {\n"
14078 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14079 "};",
14080 getLLVMStyleWithColumns(39));
14081 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14082 " 1, 1, 1, 1,\n"
14083 " /**/ /**/};",
14084 getLLVMStyleWithColumns(39));
14085
14086 // Trailing comment in the first line.
14087 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14088 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14089 " 111111111, 222222222, 3333333333, 444444444, //\n"
14090 " 11111111, 22222222, 333333333, 44444444};");
14091 // Trailing comment in the last line.
14092 verifyFormat("int aaaaa[] = {\n"
14093 " 1, 2, 3, // comment\n"
14094 " 4, 5, 6 // comment\n"
14095 "};");
14096
14097 // With nested lists, we should either format one item per line or all nested
14098 // lists one on line.
14099 // FIXME: For some nested lists, we can do better.
14100 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14101 " {aaaaaaaaaaaaaaaaaaa},\n"
14102 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14103 " {aaaaaaaaaaaaaaaaa}};",
14104 getLLVMStyleWithColumns(60));
14105 verifyFormat(
14106 "SomeStruct my_struct_array = {\n"
14107 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14108 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14109 " {aaa, aaa},\n"
14110 " {aaa, aaa},\n"
14111 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14112 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14113 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14114
14115 // No column layout should be used here.
14116 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14117 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14118
14119 verifyNoCrash(Code: "a<,");
14120
14121 // No braced initializer here.
14122 verifyFormat("void f() {\n"
14123 " struct Dummy {};\n"
14124 " f(v);\n"
14125 "}");
14126 verifyFormat("void foo() {\n"
14127 " { // asdf\n"
14128 " {\n"
14129 " int a;\n"
14130 " }\n"
14131 " }\n"
14132 " {\n"
14133 " {\n"
14134 " int b;\n"
14135 " }\n"
14136 " }\n"
14137 "}");
14138 verifyFormat("namespace n {\n"
14139 "void foo() {\n"
14140 " {\n"
14141 " {\n"
14142 " statement();\n"
14143 " if (false) {\n"
14144 " }\n"
14145 " }\n"
14146 " }\n"
14147 " {\n"
14148 " }\n"
14149 "}\n"
14150 "} // namespace n");
14151
14152 // Long lists should be formatted in columns even if they are nested.
14153 verifyFormat(
14154 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14155 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14156 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14157 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14158 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14159 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14160
14161 // Allow "single-column" layout even if that violates the column limit. There
14162 // isn't going to be a better way.
14163 verifyFormat("std::vector<int> a = {\n"
14164 " aaaaaaaa,\n"
14165 " aaaaaaaa,\n"
14166 " aaaaaaaa,\n"
14167 " aaaaaaaa,\n"
14168 " aaaaaaaaaa,\n"
14169 " aaaaaaaa,\n"
14170 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14171 getLLVMStyleWithColumns(30));
14172 verifyFormat("vector<int> aaaa = {\n"
14173 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14174 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14175 " aaaaaa.aaaaaaa,\n"
14176 " aaaaaa.aaaaaaa,\n"
14177 " aaaaaa.aaaaaaa,\n"
14178 " aaaaaa.aaaaaaa,\n"
14179 "};");
14180
14181 // Don't create hanging lists.
14182 verifyFormat("someFunction(Param, {List1, List2,\n"
14183 " List3});",
14184 getLLVMStyleWithColumns(35));
14185 verifyFormat("someFunction(Param, Param,\n"
14186 " {List1, List2,\n"
14187 " List3});",
14188 getLLVMStyleWithColumns(35));
14189 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14190 " aaaaaaaaaaaaaaaaaaaaaaa);");
14191
14192 // No possible column formats, don't want the optimal paths penalized.
14193 verifyFormat(
14194 "waarudo::unit desk = {\n"
14195 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14196 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14197 "Output{1, 2}; },\n"
14198 " [](const Input &i) -> Output { return "
14199 "Output{1, 2}; });");
14200 FormatStyle NoBinPacking = getLLVMStyle();
14201 NoBinPacking.BinPackParameters = false;
14202 verifyFormat("waarudo::unit desk = {\n"
14203 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14204 "1, 1} * w::m; }};",
14205 NoBinPacking);
14206}
14207
14208TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14209 FormatStyle DoNotMerge = getLLVMStyle();
14210 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14211
14212 verifyFormat("void f() { return 42; }");
14213 verifyFormat("void f() {\n"
14214 " return 42;\n"
14215 "}",
14216 DoNotMerge);
14217 verifyFormat("void f() {\n"
14218 " // Comment\n"
14219 "}");
14220 verifyFormat("{\n"
14221 "#error {\n"
14222 " int a;\n"
14223 "}");
14224 verifyFormat("{\n"
14225 " int a;\n"
14226 "#error {\n"
14227 "}");
14228 verifyFormat("void f() {} // comment");
14229 verifyFormat("void f() { int a; } // comment");
14230 verifyFormat("void f() {\n"
14231 "} // comment",
14232 DoNotMerge);
14233 verifyFormat("void f() {\n"
14234 " int a;\n"
14235 "} // comment",
14236 DoNotMerge);
14237 verifyFormat("void f() {\n"
14238 "} // comment",
14239 getLLVMStyleWithColumns(15));
14240
14241 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14242 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14243
14244 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14245 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14246 verifyGoogleFormat("class C {\n"
14247 " C()\n"
14248 " : iiiiiiii(nullptr),\n"
14249 " kkkkkkk(nullptr),\n"
14250 " mmmmmmm(nullptr),\n"
14251 " nnnnnnn(nullptr) {}\n"
14252 "};");
14253
14254 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(ColumnLimit: 0);
14255 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
14256 verifyFormat("class C {\n"
14257 " A() : b(0) {}\n"
14258 "};",
14259 "class C{A():b(0){}};", NoColumnLimit);
14260 verifyFormat("A()\n"
14261 " : b(0) {\n"
14262 "}",
14263 "A()\n:b(0)\n{\n}", NoColumnLimit);
14264
14265 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
14266 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
14267 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
14268 verifyFormat("class C {\n"
14269 "#pragma foo\n"
14270 " int foo { return 0; }\n"
14271 "};",
14272 NoColumnLimitWrapAfterFunction);
14273 verifyFormat("class C {\n"
14274 "#pragma foo\n"
14275 " void foo {}\n"
14276 "};",
14277 NoColumnLimitWrapAfterFunction);
14278
14279 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
14280 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
14281 FormatStyle::SFS_None;
14282 verifyFormat("A()\n"
14283 " : b(0) {\n"
14284 "}",
14285 "A():b(0){}", DoNotMergeNoColumnLimit);
14286 verifyFormat("A()\n"
14287 " : b(0) {\n"
14288 "}",
14289 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
14290
14291 verifyFormat("#define A \\\n"
14292 " void f() { \\\n"
14293 " int i; \\\n"
14294 " }",
14295 getLLVMStyleWithColumns(20));
14296 verifyFormat("#define A \\\n"
14297 " void f() { int i; }",
14298 getLLVMStyleWithColumns(21));
14299 verifyFormat("#define A \\\n"
14300 " void f() { \\\n"
14301 " int i; \\\n"
14302 " } \\\n"
14303 " int j;",
14304 getLLVMStyleWithColumns(22));
14305 verifyFormat("#define A \\\n"
14306 " void f() { int i; } \\\n"
14307 " int j;",
14308 getLLVMStyleWithColumns(23));
14309
14310 verifyFormat(
14311 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14312 " aaaaaaaaaaaaaaaaaa,\n"
14313 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14314
14315 constexpr StringRef Code{"void foo() { /* Empty */ }"};
14316 verifyFormat(Code);
14317 verifyFormat(Code, "void foo() { /* Empty */\n"
14318 "}");
14319 verifyFormat(Code, "void foo() {\n"
14320 "/* Empty */\n"
14321 "}");
14322}
14323
14324TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
14325 FormatStyle MergeEmptyOnly = getLLVMStyle();
14326 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14327 verifyFormat("class C {\n"
14328 " int f() {}\n"
14329 "};",
14330 MergeEmptyOnly);
14331 verifyFormat("class C {\n"
14332 " int f() {\n"
14333 " return 42;\n"
14334 " }\n"
14335 "};",
14336 MergeEmptyOnly);
14337 verifyFormat("int f() {}", MergeEmptyOnly);
14338 verifyFormat("int f() {\n"
14339 " return 42;\n"
14340 "}",
14341 MergeEmptyOnly);
14342
14343 // Also verify behavior when BraceWrapping.AfterFunction = true
14344 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14345 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
14346 verifyFormat("int f() {}", MergeEmptyOnly);
14347 verifyFormat("class C {\n"
14348 " int f() {}\n"
14349 "};",
14350 MergeEmptyOnly);
14351}
14352
14353TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
14354 FormatStyle MergeInlineOnly = getLLVMStyle();
14355 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14356 verifyFormat("class C {\n"
14357 " int f() { return 42; }\n"
14358 "};",
14359 MergeInlineOnly);
14360 verifyFormat("int f() {\n"
14361 " return 42;\n"
14362 "}",
14363 MergeInlineOnly);
14364
14365 // SFS_Inline implies SFS_Empty
14366 verifyFormat("class C {\n"
14367 " int f() {}\n"
14368 "};",
14369 MergeInlineOnly);
14370 verifyFormat("int f() {}", MergeInlineOnly);
14371 // https://llvm.org/PR54147
14372 verifyFormat("auto lambda = []() {\n"
14373 " // comment\n"
14374 " f();\n"
14375 " g();\n"
14376 "};",
14377 MergeInlineOnly);
14378
14379 verifyFormat("class C {\n"
14380 "#ifdef A\n"
14381 " int f() { return 42; }\n"
14382 "#endif\n"
14383 "};",
14384 MergeInlineOnly);
14385
14386 verifyFormat("struct S {\n"
14387 "// comment\n"
14388 "#ifdef FOO\n"
14389 " int foo() { bar(); }\n"
14390 "#endif\n"
14391 "};",
14392 MergeInlineOnly);
14393
14394 // Also verify behavior when BraceWrapping.AfterFunction = true
14395 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14396 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14397 verifyFormat("class C {\n"
14398 " int f() { return 42; }\n"
14399 "};",
14400 MergeInlineOnly);
14401 verifyFormat("int f()\n"
14402 "{\n"
14403 " return 42;\n"
14404 "}",
14405 MergeInlineOnly);
14406
14407 // SFS_Inline implies SFS_Empty
14408 verifyFormat("int f() {}", MergeInlineOnly);
14409 verifyFormat("class C {\n"
14410 " int f() {}\n"
14411 "};",
14412 MergeInlineOnly);
14413
14414 MergeInlineOnly.BraceWrapping.AfterClass = true;
14415 MergeInlineOnly.BraceWrapping.AfterStruct = true;
14416 verifyFormat("class C\n"
14417 "{\n"
14418 " int f() { return 42; }\n"
14419 "};",
14420 MergeInlineOnly);
14421 verifyFormat("struct C\n"
14422 "{\n"
14423 " int f() { return 42; }\n"
14424 "};",
14425 MergeInlineOnly);
14426 verifyFormat("int f()\n"
14427 "{\n"
14428 " return 42;\n"
14429 "}",
14430 MergeInlineOnly);
14431 verifyFormat("int f() {}", MergeInlineOnly);
14432 verifyFormat("class C\n"
14433 "{\n"
14434 " int f() { return 42; }\n"
14435 "};",
14436 MergeInlineOnly);
14437 verifyFormat("struct C\n"
14438 "{\n"
14439 " int f() { return 42; }\n"
14440 "};",
14441 MergeInlineOnly);
14442 verifyFormat("struct C\n"
14443 "// comment\n"
14444 "/* comment */\n"
14445 "// comment\n"
14446 "{\n"
14447 " int f() { return 42; }\n"
14448 "};",
14449 MergeInlineOnly);
14450 verifyFormat("/* comment */ struct C\n"
14451 "{\n"
14452 " int f() { return 42; }\n"
14453 "};",
14454 MergeInlineOnly);
14455}
14456
14457TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
14458 FormatStyle MergeInlineOnly = getLLVMStyle();
14459 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
14460 FormatStyle::SFS_InlineOnly;
14461 verifyFormat("class C {\n"
14462 " int f() { return 42; }\n"
14463 "};",
14464 MergeInlineOnly);
14465 verifyFormat("int f() {\n"
14466 " return 42;\n"
14467 "}",
14468 MergeInlineOnly);
14469
14470 // SFS_InlineOnly does not imply SFS_Empty
14471 verifyFormat("class C {\n"
14472 " int f() {}\n"
14473 "};",
14474 MergeInlineOnly);
14475 verifyFormat("int f() {\n"
14476 "}",
14477 MergeInlineOnly);
14478
14479 // Also verify behavior when BraceWrapping.AfterFunction = true
14480 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
14481 MergeInlineOnly.BraceWrapping.AfterFunction = true;
14482 verifyFormat("class C {\n"
14483 " int f() { return 42; }\n"
14484 "};",
14485 MergeInlineOnly);
14486 verifyFormat("int f()\n"
14487 "{\n"
14488 " return 42;\n"
14489 "}",
14490 MergeInlineOnly);
14491
14492 // SFS_InlineOnly does not imply SFS_Empty
14493 verifyFormat("int f()\n"
14494 "{\n"
14495 "}",
14496 MergeInlineOnly);
14497 verifyFormat("class C {\n"
14498 " int f() {}\n"
14499 "};",
14500 MergeInlineOnly);
14501}
14502
14503TEST_F(FormatTest, SplitEmptyFunction) {
14504 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
14505 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14506 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14507 Style.BraceWrapping.AfterFunction = true;
14508 Style.BraceWrapping.SplitEmptyFunction = false;
14509
14510 verifyFormat("int f()\n"
14511 "{}",
14512 Style);
14513 verifyFormat("int f()\n"
14514 "{\n"
14515 " return 42;\n"
14516 "}",
14517 Style);
14518 verifyFormat("int f()\n"
14519 "{\n"
14520 " // some comment\n"
14521 "}",
14522 Style);
14523
14524 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
14525 verifyFormat("int f() {}", Style);
14526 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14527 "{}",
14528 Style);
14529 verifyFormat("int f()\n"
14530 "{\n"
14531 " return 0;\n"
14532 "}",
14533 Style);
14534
14535 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
14536 verifyFormat("class Foo {\n"
14537 " int f() {}\n"
14538 "};",
14539 Style);
14540 verifyFormat("class Foo {\n"
14541 " int f() { return 0; }\n"
14542 "};",
14543 Style);
14544 verifyFormat("class Foo {\n"
14545 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14546 " {}\n"
14547 "};",
14548 Style);
14549 verifyFormat("class Foo {\n"
14550 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14551 " {\n"
14552 " return 0;\n"
14553 " }\n"
14554 "};",
14555 Style);
14556
14557 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14558 verifyFormat("int f() {}", Style);
14559 verifyFormat("int f() { return 0; }", Style);
14560 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14561 "{}",
14562 Style);
14563 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14564 "{\n"
14565 " return 0;\n"
14566 "}",
14567 Style);
14568}
14569
14570TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
14571 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
14572 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14573 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14574 Style.BraceWrapping.AfterFunction = true;
14575 Style.BraceWrapping.SplitEmptyFunction = true;
14576 Style.BraceWrapping.SplitEmptyRecord = false;
14577
14578 verifyFormat("class C {};", Style);
14579 verifyFormat("struct C {};", Style);
14580 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14581 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14582 "{\n"
14583 "}",
14584 Style);
14585 verifyFormat("class C {\n"
14586 " C()\n"
14587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14588 " bbbbbbbbbbbbbbbbbbb()\n"
14589 " {\n"
14590 " }\n"
14591 " void\n"
14592 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14593 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14594 " {\n"
14595 " }\n"
14596 "};",
14597 Style);
14598}
14599
14600TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
14601 FormatStyle Style = getLLVMStyle();
14602 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14603 verifyFormat("#ifdef A\n"
14604 "int f() {}\n"
14605 "#else\n"
14606 "int g() {}\n"
14607 "#endif",
14608 Style);
14609}
14610
14611TEST_F(FormatTest, SplitEmptyClass) {
14612 FormatStyle Style = getLLVMStyle();
14613 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14614 Style.BraceWrapping.AfterClass = true;
14615 Style.BraceWrapping.SplitEmptyRecord = false;
14616
14617 verifyFormat("class Foo\n"
14618 "{};",
14619 Style);
14620 verifyFormat("/* something */ class Foo\n"
14621 "{};",
14622 Style);
14623 verifyFormat("template <typename X> class Foo\n"
14624 "{};",
14625 Style);
14626 verifyFormat("class Foo\n"
14627 "{\n"
14628 " Foo();\n"
14629 "};",
14630 Style);
14631 verifyFormat("typedef class Foo\n"
14632 "{\n"
14633 "} Foo_t;",
14634 Style);
14635
14636 Style.BraceWrapping.SplitEmptyRecord = true;
14637 Style.BraceWrapping.AfterStruct = true;
14638 verifyFormat("class rep\n"
14639 "{\n"
14640 "};",
14641 Style);
14642 verifyFormat("struct rep\n"
14643 "{\n"
14644 "};",
14645 Style);
14646 verifyFormat("template <typename T> class rep\n"
14647 "{\n"
14648 "};",
14649 Style);
14650 verifyFormat("template <typename T> struct rep\n"
14651 "{\n"
14652 "};",
14653 Style);
14654 verifyFormat("class rep\n"
14655 "{\n"
14656 " int x;\n"
14657 "};",
14658 Style);
14659 verifyFormat("struct rep\n"
14660 "{\n"
14661 " int x;\n"
14662 "};",
14663 Style);
14664 verifyFormat("template <typename T> class rep\n"
14665 "{\n"
14666 " int x;\n"
14667 "};",
14668 Style);
14669 verifyFormat("template <typename T> struct rep\n"
14670 "{\n"
14671 " int x;\n"
14672 "};",
14673 Style);
14674 verifyFormat("template <typename T> class rep // Foo\n"
14675 "{\n"
14676 " int x;\n"
14677 "};",
14678 Style);
14679 verifyFormat("template <typename T> struct rep // Bar\n"
14680 "{\n"
14681 " int x;\n"
14682 "};",
14683 Style);
14684
14685 verifyFormat("template <typename T> class rep<T>\n"
14686 "{\n"
14687 " int x;\n"
14688 "};",
14689 Style);
14690
14691 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14692 "{\n"
14693 " int x;\n"
14694 "};",
14695 Style);
14696 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14697 "{\n"
14698 "};",
14699 Style);
14700
14701 verifyFormat("#include \"stdint.h\"\n"
14702 "namespace rep {}",
14703 Style);
14704 verifyFormat("#include <stdint.h>\n"
14705 "namespace rep {}",
14706 Style);
14707 verifyFormat("#include <stdint.h>\n"
14708 "namespace rep {}",
14709 "#include <stdint.h>\n"
14710 "namespace rep {\n"
14711 "\n"
14712 "\n"
14713 "}",
14714 Style);
14715}
14716
14717TEST_F(FormatTest, SplitEmptyStruct) {
14718 FormatStyle Style = getLLVMStyle();
14719 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14720 Style.BraceWrapping.AfterStruct = true;
14721 Style.BraceWrapping.SplitEmptyRecord = false;
14722
14723 verifyFormat("struct Foo\n"
14724 "{};",
14725 Style);
14726 verifyFormat("/* something */ struct Foo\n"
14727 "{};",
14728 Style);
14729 verifyFormat("template <typename X> struct Foo\n"
14730 "{};",
14731 Style);
14732 verifyFormat("struct Foo\n"
14733 "{\n"
14734 " Foo();\n"
14735 "};",
14736 Style);
14737 verifyFormat("typedef struct Foo\n"
14738 "{\n"
14739 "} Foo_t;",
14740 Style);
14741 // typedef struct Bar {} Bar_t;
14742}
14743
14744TEST_F(FormatTest, SplitEmptyUnion) {
14745 FormatStyle Style = getLLVMStyle();
14746 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14747 Style.BraceWrapping.AfterUnion = true;
14748 Style.BraceWrapping.SplitEmptyRecord = false;
14749
14750 verifyFormat("union Foo\n"
14751 "{};",
14752 Style);
14753 verifyFormat("/* something */ union Foo\n"
14754 "{};",
14755 Style);
14756 verifyFormat("union Foo\n"
14757 "{\n"
14758 " A,\n"
14759 "};",
14760 Style);
14761 verifyFormat("typedef union Foo\n"
14762 "{\n"
14763 "} Foo_t;",
14764 Style);
14765}
14766
14767TEST_F(FormatTest, SplitEmptyNamespace) {
14768 FormatStyle Style = getLLVMStyle();
14769 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14770 Style.BraceWrapping.AfterNamespace = true;
14771 Style.BraceWrapping.SplitEmptyNamespace = false;
14772
14773 verifyFormat("namespace Foo\n"
14774 "{};",
14775 Style);
14776 verifyFormat("/* something */ namespace Foo\n"
14777 "{};",
14778 Style);
14779 verifyFormat("inline namespace Foo\n"
14780 "{};",
14781 Style);
14782 verifyFormat("/* something */ inline namespace Foo\n"
14783 "{};",
14784 Style);
14785 verifyFormat("export namespace Foo\n"
14786 "{};",
14787 Style);
14788 verifyFormat("namespace Foo\n"
14789 "{\n"
14790 "void Bar();\n"
14791 "};",
14792 Style);
14793}
14794
14795TEST_F(FormatTest, NeverMergeShortRecords) {
14796 FormatStyle Style = getLLVMStyle();
14797
14798 verifyFormat("class Foo {\n"
14799 " Foo();\n"
14800 "};",
14801 Style);
14802 verifyFormat("typedef class Foo {\n"
14803 " Foo();\n"
14804 "} Foo_t;",
14805 Style);
14806 verifyFormat("struct Foo {\n"
14807 " Foo();\n"
14808 "};",
14809 Style);
14810 verifyFormat("typedef struct Foo {\n"
14811 " Foo();\n"
14812 "} Foo_t;",
14813 Style);
14814 verifyFormat("union Foo {\n"
14815 " A,\n"
14816 "};",
14817 Style);
14818 verifyFormat("typedef union Foo {\n"
14819 " A,\n"
14820 "} Foo_t;",
14821 Style);
14822 verifyFormat("namespace Foo {\n"
14823 "void Bar();\n"
14824 "};",
14825 Style);
14826
14827 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
14828 Style.BraceWrapping.AfterClass = true;
14829 Style.BraceWrapping.AfterStruct = true;
14830 Style.BraceWrapping.AfterUnion = true;
14831 Style.BraceWrapping.AfterNamespace = true;
14832 verifyFormat("class Foo\n"
14833 "{\n"
14834 " Foo();\n"
14835 "};",
14836 Style);
14837 verifyFormat("typedef class Foo\n"
14838 "{\n"
14839 " Foo();\n"
14840 "} Foo_t;",
14841 Style);
14842 verifyFormat("struct Foo\n"
14843 "{\n"
14844 " Foo();\n"
14845 "};",
14846 Style);
14847 verifyFormat("typedef struct Foo\n"
14848 "{\n"
14849 " Foo();\n"
14850 "} Foo_t;",
14851 Style);
14852 verifyFormat("union Foo\n"
14853 "{\n"
14854 " A,\n"
14855 "};",
14856 Style);
14857 verifyFormat("typedef union Foo\n"
14858 "{\n"
14859 " A,\n"
14860 "} Foo_t;",
14861 Style);
14862 verifyFormat("namespace Foo\n"
14863 "{\n"
14864 "void Bar();\n"
14865 "};",
14866 Style);
14867}
14868
14869TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
14870 // Elaborate type variable declarations.
14871 verifyFormat("struct foo a = {bar};\nint n;");
14872 verifyFormat("class foo a = {bar};\nint n;");
14873 verifyFormat("union foo a = {bar};\nint n;");
14874
14875 // Elaborate types inside function definitions.
14876 verifyFormat("struct foo f() {}\nint n;");
14877 verifyFormat("class foo f() {}\nint n;");
14878 verifyFormat("union foo f() {}\nint n;");
14879
14880 // Templates.
14881 verifyFormat("template <class X> void f() {}\nint n;");
14882 verifyFormat("template <struct X> void f() {}\nint n;");
14883 verifyFormat("template <union X> void f() {}\nint n;");
14884
14885 // Actual definitions...
14886 verifyFormat("struct {\n} n;");
14887 verifyFormat(
14888 "template <template <class T, class Y>, class Z> class X {\n} n;");
14889 verifyFormat("union Z {\n int n;\n} x;");
14890 verifyFormat("class MACRO Z {\n} n;");
14891 verifyFormat("class MACRO(X) Z {\n} n;");
14892 verifyFormat("class __attribute__(X) Z {\n} n;");
14893 verifyFormat("class __declspec(X) Z {\n} n;");
14894 verifyFormat("class A##B##C {\n} n;");
14895 verifyFormat("class alignas(16) Z {\n} n;");
14896 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
14897 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
14898
14899 // Redefinition from nested context:
14900 verifyFormat("class A::B::C {\n} n;");
14901
14902 // Template definitions.
14903 verifyFormat(
14904 "template <typename F>\n"
14905 "Matcher(const Matcher<F> &Other,\n"
14906 " typename enable_if_c<is_base_of<F, T>::value &&\n"
14907 " !is_same<F, T>::value>::type * = 0)\n"
14908 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
14909
14910 // FIXME: This is still incorrectly handled at the formatter side.
14911 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
14912 verifyFormat("int i = SomeFunction(a<b, a> b);");
14913
14914 verifyFormat("class A<int> f() {}\n"
14915 "int n;");
14916 verifyFormat("template <typename T> class A<T> f() {}\n"
14917 "int n;");
14918
14919 verifyFormat("template <> class Foo<int> F() {\n"
14920 "} n;");
14921
14922 // Elaborate types where incorrectly parsing the structural element would
14923 // break the indent.
14924 verifyFormat("if (true)\n"
14925 " class X x;\n"
14926 "else\n"
14927 " f();");
14928
14929 // This is simply incomplete. Formatting is not important, but must not crash.
14930 verifyFormat("class A:");
14931}
14932
14933TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
14934 verifyNoChange("#error Leave all white!!!!! space* alone!");
14935 verifyNoChange("#warning Leave all white!!!!! space* alone!");
14936 verifyFormat("#error 1", " # error 1");
14937 verifyFormat("#warning 1", " # warning 1");
14938}
14939
14940TEST_F(FormatTest, FormatHashIfExpressions) {
14941 verifyFormat("#if AAAA && BBBB");
14942 verifyFormat("#if (AAAA && BBBB)");
14943 verifyFormat("#elif (AAAA && BBBB)");
14944 // FIXME: Come up with a better indentation for #elif.
14945 verifyFormat(
14946 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
14947 " defined(BBBBBBBB)\n"
14948 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
14949 " defined(BBBBBBBB)\n"
14950 "#endif",
14951 getLLVMStyleWithColumns(65));
14952}
14953
14954TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
14955 FormatStyle AllowsMergedIf = getGoogleStyle();
14956 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
14957 FormatStyle::SIS_WithoutElse;
14958 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
14959 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
14960 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
14961 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
14962 FormatStyle ShortMergedIf = AllowsMergedIf;
14963 ShortMergedIf.ColumnLimit = 25;
14964 verifyFormat("#define A \\\n"
14965 " if (true) return 42;",
14966 ShortMergedIf);
14967 verifyFormat("#define A \\\n"
14968 " f(); \\\n"
14969 " if (true)\n"
14970 "#define B",
14971 ShortMergedIf);
14972 verifyFormat("#define A \\\n"
14973 " f(); \\\n"
14974 " if (true)\n"
14975 "g();",
14976 ShortMergedIf);
14977 verifyFormat("{\n"
14978 "#ifdef A\n"
14979 " // Comment\n"
14980 " if (true) continue;\n"
14981 "#endif\n"
14982 " // Comment\n"
14983 " if (true) continue;\n"
14984 "}",
14985 ShortMergedIf);
14986 ShortMergedIf.ColumnLimit = 33;
14987 verifyFormat("#define A \\\n"
14988 " if constexpr (true) return 42;",
14989 ShortMergedIf);
14990 verifyFormat("#define A \\\n"
14991 " if CONSTEXPR (true) return 42;",
14992 ShortMergedIf);
14993 ShortMergedIf.ColumnLimit = 29;
14994 verifyFormat("#define A \\\n"
14995 " if (aaaaaaaaaa) return 1; \\\n"
14996 " return 2;",
14997 ShortMergedIf);
14998 ShortMergedIf.ColumnLimit = 28;
14999 verifyFormat("#define A \\\n"
15000 " if (aaaaaaaaaa) \\\n"
15001 " return 1; \\\n"
15002 " return 2;",
15003 ShortMergedIf);
15004 verifyFormat("#define A \\\n"
15005 " if constexpr (aaaaaaa) \\\n"
15006 " return 1; \\\n"
15007 " return 2;",
15008 ShortMergedIf);
15009 verifyFormat("#define A \\\n"
15010 " if CONSTEXPR (aaaaaaa) \\\n"
15011 " return 1; \\\n"
15012 " return 2;",
15013 ShortMergedIf);
15014
15015 verifyFormat("//\n"
15016 "#define a \\\n"
15017 " if \\\n"
15018 " 0",
15019 getChromiumStyle(FormatStyle::LK_Cpp));
15020}
15021
15022TEST_F(FormatTest, FormatStarDependingOnContext) {
15023 verifyFormat("void f(int *a);");
15024 verifyFormat("void f() { f(fint * b); }");
15025 verifyFormat("class A {\n void f(int *a);\n};");
15026 verifyFormat("class A {\n int *a;\n};");
15027 verifyFormat("namespace a {\n"
15028 "namespace b {\n"
15029 "class A {\n"
15030 " void f() {}\n"
15031 " int *a;\n"
15032 "};\n"
15033 "} // namespace b\n"
15034 "} // namespace a");
15035}
15036
15037TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15038 verifyFormat("while");
15039 verifyFormat("operator");
15040}
15041
15042TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15043 // This code would be painfully slow to format if we didn't skip it.
15044 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
15045 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15046 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15047 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15048 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15049 "A(1, 1)\n"
15050 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15051 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15052 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15053 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15054 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15055 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15056 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15057 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15058 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15059 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15060 // Deeply nested part is untouched, rest is formatted.
15061 EXPECT_EQ(std::string("int i;") + Code + "int j;",
15062 format(std::string("int i;") + Code + "int j;",
15063 getLLVMStyle(), SC_ExpectIncomplete));
15064}
15065
15066//===----------------------------------------------------------------------===//
15067// Objective-C tests.
15068//===----------------------------------------------------------------------===//
15069
15070TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15071 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15072 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15073 "-(NSUInteger)indexOfObject:(id)anObject;");
15074 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15075 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15076 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15077 "-(NSInteger)Method3:(id)anObject;");
15078 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15079 "-(NSInteger)Method4:(id)anObject;");
15080 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15081 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15082 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15083 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15084 "forAllCells:(BOOL)flag;");
15085
15086 // Very long objectiveC method declaration.
15087 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15088 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15089 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15090 " inRange:(NSRange)range\n"
15091 " outRange:(NSRange)out_range\n"
15092 " outRange1:(NSRange)out_range1\n"
15093 " outRange2:(NSRange)out_range2\n"
15094 " outRange3:(NSRange)out_range3\n"
15095 " outRange4:(NSRange)out_range4\n"
15096 " outRange5:(NSRange)out_range5\n"
15097 " outRange6:(NSRange)out_range6\n"
15098 " outRange7:(NSRange)out_range7\n"
15099 " outRange8:(NSRange)out_range8\n"
15100 " outRange9:(NSRange)out_range9;");
15101
15102 // When the function name has to be wrapped.
15103 FormatStyle Style = getLLVMStyle();
15104 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15105 // and always indents instead.
15106 Style.IndentWrappedFunctionNames = false;
15107 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15108 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15109 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15110 "}",
15111 Style);
15112 Style.IndentWrappedFunctionNames = true;
15113 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15114 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15115 " anotherName:(NSString)dddddddddddddd {\n"
15116 "}",
15117 Style);
15118
15119 verifyFormat("- (int)sum:(vector<int>)numbers;");
15120 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15121 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15122 // protocol lists (but not for template classes):
15123 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15124
15125 verifyFormat("- (int (*)())foo:(int (*)())f;");
15126 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15127
15128 // If there's no return type (very rare in practice!), LLVM and Google style
15129 // agree.
15130 verifyFormat("- foo;");
15131 verifyFormat("- foo:(int)f;");
15132 verifyGoogleFormat("- foo:(int)foo;");
15133}
15134
15135TEST_F(FormatTest, BreaksStringLiterals) {
15136 // FIXME: unstable test case
15137 EXPECT_EQ("\"some text \"\n"
15138 "\"other\";",
15139 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15140 // FIXME: unstable test case
15141 EXPECT_EQ("\"some text \"\n"
15142 "\"other\";",
15143 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15144 verifyFormat("#define A \\\n"
15145 " \"some \" \\\n"
15146 " \"text \" \\\n"
15147 " \"other\";",
15148 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15149 verifyFormat("#define A \\\n"
15150 " \"so \" \\\n"
15151 " \"text \" \\\n"
15152 " \"other\";",
15153 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15154
15155 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15156 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15157 // FIXME: unstable test case
15158 EXPECT_EQ("\"some \"\n"
15159 "\"text\"",
15160 format("\"some text\"", getLLVMStyleWithColumns(10)));
15161 // FIXME: unstable test case
15162 EXPECT_EQ("\"some \"\n"
15163 "\"text\"",
15164 format("\"some text\"", getLLVMStyleWithColumns(7)));
15165 // FIXME: unstable test case
15166 EXPECT_EQ("\"some\"\n"
15167 "\" tex\"\n"
15168 "\"t\"",
15169 format("\"some text\"", getLLVMStyleWithColumns(6)));
15170 // FIXME: unstable test case
15171 EXPECT_EQ("\"some\"\n"
15172 "\" tex\"\n"
15173 "\" and\"",
15174 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15175 // FIXME: unstable test case
15176 EXPECT_EQ("\"some\"\n"
15177 "\"/tex\"\n"
15178 "\"/and\"",
15179 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15180
15181 verifyFormat("variable =\n"
15182 " \"long string \"\n"
15183 " \"literal\";",
15184 "variable = \"long string literal\";",
15185 getLLVMStyleWithColumns(20));
15186
15187 verifyFormat("variable = f(\n"
15188 " \"long string \"\n"
15189 " \"literal\",\n"
15190 " short,\n"
15191 " loooooooooooooooooooong);",
15192 "variable = f(\"long string literal\", short, "
15193 "loooooooooooooooooooong);",
15194 getLLVMStyleWithColumns(20));
15195
15196 verifyFormat("f(g(\"long string \"\n"
15197 " \"literal\"),\n"
15198 " b);",
15199 "f(g(\"long string literal\"), b);",
15200 getLLVMStyleWithColumns(20));
15201 verifyFormat("f(g(\"long string \"\n"
15202 " \"literal\",\n"
15203 " a),\n"
15204 " b);",
15205 "f(g(\"long string literal\", a), b);",
15206 getLLVMStyleWithColumns(20));
15207 verifyFormat("f(\"one two\".split(\n"
15208 " variable));",
15209 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15210 verifyFormat("f(\"one two three four five six \"\n"
15211 " \"seven\".split(\n"
15212 " really_looooong_variable));",
15213 "f(\"one two three four five six seven\"."
15214 "split(really_looooong_variable));",
15215 getLLVMStyleWithColumns(33));
15216
15217 verifyFormat("f(\"some \"\n"
15218 " \"text\",\n"
15219 " other);",
15220 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15221
15222 // Only break as a last resort.
15223 verifyFormat(
15224 "aaaaaaaaaaaaaaaaaaaa(\n"
15225 " aaaaaaaaaaaaaaaaaaaa,\n"
15226 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15227
15228 // FIXME: unstable test case
15229 EXPECT_EQ("\"splitmea\"\n"
15230 "\"trandomp\"\n"
15231 "\"oint\"",
15232 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15233
15234 // FIXME: unstable test case
15235 EXPECT_EQ("\"split/\"\n"
15236 "\"pathat/\"\n"
15237 "\"slashes\"",
15238 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15239
15240 // FIXME: unstable test case
15241 EXPECT_EQ("\"split/\"\n"
15242 "\"pathat/\"\n"
15243 "\"slashes\"",
15244 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15245 // FIXME: unstable test case
15246 EXPECT_EQ("\"split at \"\n"
15247 "\"spaces/at/\"\n"
15248 "\"slashes.at.any$\"\n"
15249 "\"non-alphanumeric%\"\n"
15250 "\"1111111111characte\"\n"
15251 "\"rs\"",
15252 format("\"split at "
15253 "spaces/at/"
15254 "slashes.at."
15255 "any$non-"
15256 "alphanumeric%"
15257 "1111111111characte"
15258 "rs\"",
15259 getLLVMStyleWithColumns(20)));
15260
15261 // Verify that splitting the strings understands
15262 // Style::AlwaysBreakBeforeMultilineStrings.
15263 verifyFormat("aaaaaaaaaaaa(\n"
15264 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15265 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15266 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15267 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15268 "aaaaaaaaaaaaaaaaaaaaaa\");",
15269 getGoogleStyle());
15270 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15271 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15272 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15273 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15274 "aaaaaaaaaaaaaaaaaaaaaa\";",
15275 getGoogleStyle());
15276 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15277 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15278 "llvm::outs() << "
15279 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15280 "aaaaaaaaaaaaaaaaaaa\";");
15281 verifyFormat("ffff(\n"
15282 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15283 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15284 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15285 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15286 getGoogleStyle());
15287
15288 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 12);
15289 Style.BreakStringLiterals = false;
15290 verifyFormat("\"some text other\";", Style);
15291
15292 FormatStyle AlignLeft = getLLVMStyleWithColumns(ColumnLimit: 12);
15293 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15294 verifyFormat("#define A \\\n"
15295 " \"some \" \\\n"
15296 " \"text \" \\\n"
15297 " \"other\";",
15298 "#define A \"some text other\";", AlignLeft);
15299}
15300
15301TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
15302 verifyFormat("C a = \"some more \"\n"
15303 " \"text\";",
15304 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15305}
15306
15307TEST_F(FormatTest, FullyRemoveEmptyLines) {
15308 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(ColumnLimit: 80);
15309 NoEmptyLines.MaxEmptyLinesToKeep = 0;
15310 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15311 NoEmptyLines);
15312}
15313
15314TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
15315 // FIXME: unstable test case
15316 EXPECT_EQ(
15317 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15318 "(\n"
15319 " \"x\t\");",
15320 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15321 "aaaaaaa("
15322 "\"x\t\");"));
15323}
15324
15325TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
15326 // FIXME: unstable test case
15327 EXPECT_EQ(
15328 "u8\"utf8 string \"\n"
15329 "u8\"literal\";",
15330 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15331 // FIXME: unstable test case
15332 EXPECT_EQ(
15333 "u\"utf16 string \"\n"
15334 "u\"literal\";",
15335 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15336 // FIXME: unstable test case
15337 EXPECT_EQ(
15338 "U\"utf32 string \"\n"
15339 "U\"literal\";",
15340 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15341 // FIXME: unstable test case
15342 EXPECT_EQ("L\"wide string \"\n"
15343 "L\"literal\";",
15344 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15345 verifyFormat("@\"NSString \"\n"
15346 "@\"literal\";",
15347 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15348 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
15349
15350 // This input makes clang-format try to split the incomplete unicode escape
15351 // sequence, which used to lead to a crasher.
15352 verifyNoCrash(
15353 Code: "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15354 Style: getLLVMStyleWithColumns(ColumnLimit: 60));
15355}
15356
15357TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
15358 FormatStyle Style = getGoogleStyleWithColumns(ColumnLimit: 15);
15359 verifyFormat("R\"x(raw literal)x\";", Style);
15360 verifyFormat("uR\"x(raw literal)x\";", Style);
15361 verifyFormat("LR\"x(raw literal)x\";", Style);
15362 verifyFormat("UR\"x(raw literal)x\";", Style);
15363 verifyFormat("u8R\"x(raw literal)x\";", Style);
15364}
15365
15366TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
15367 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20);
15368 // FIXME: unstable test case
15369 EXPECT_EQ(
15370 "_T(\"aaaaaaaaaaaaaa\")\n"
15371 "_T(\"aaaaaaaaaaaaaa\")\n"
15372 "_T(\"aaaaaaaaaaaa\")",
15373 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
15374 verifyFormat("f(x,\n"
15375 " _T(\"aaaaaaaaaaaa\")\n"
15376 " _T(\"aaa\"),\n"
15377 " z);",
15378 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
15379
15380 // FIXME: Handle embedded spaces in one iteration.
15381 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15382 // "_T(\"aaaaaaaaaaaaa\")\n"
15383 // "_T(\"aaaaaaaaaaaaa\")\n"
15384 // "_T(\"a\")",
15385 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15386 // getLLVMStyleWithColumns(20)));
15387 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15388 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
15389 verifyFormat("f(\n"
15390 "#if !TEST\n"
15391 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15392 "#endif\n"
15393 ");",
15394 "f(\n"
15395 "#if !TEST\n"
15396 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15397 "#endif\n"
15398 ");");
15399 verifyFormat("f(\n"
15400 "\n"
15401 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15402 "f(\n"
15403 "\n"
15404 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15405 // Regression test for accessing tokens past the end of a vector in the
15406 // TokenLexer.
15407 verifyNoCrash(Code: R"(_T(
15408"
15409)
15410)");
15411}
15412
15413TEST_F(FormatTest, BreaksStringLiteralOperands) {
15414 // In a function call with two operands, the second can be broken with no line
15415 // break before it.
15416 verifyFormat("func(a, \"long long \"\n"
15417 " \"long long\");",
15418 "func(a, \"long long long long\");",
15419 getLLVMStyleWithColumns(24));
15420 // In a function call with three operands, the second must be broken with a
15421 // line break before it.
15422 verifyFormat("func(a,\n"
15423 " \"long long long \"\n"
15424 " \"long\",\n"
15425 " c);",
15426 "func(a, \"long long long long\", c);",
15427 getLLVMStyleWithColumns(24));
15428 // In a function call with three operands, the third must be broken with a
15429 // line break before it.
15430 verifyFormat("func(a, b,\n"
15431 " \"long long long \"\n"
15432 " \"long\");",
15433 "func(a, b, \"long long long long\");",
15434 getLLVMStyleWithColumns(24));
15435 // In a function call with three operands, both the second and the third must
15436 // be broken with a line break before them.
15437 verifyFormat("func(a,\n"
15438 " \"long long long \"\n"
15439 " \"long\",\n"
15440 " \"long long long \"\n"
15441 " \"long\");",
15442 "func(a, \"long long long long\", \"long long long long\");",
15443 getLLVMStyleWithColumns(24));
15444 // In a chain of << with two operands, the second can be broken with no line
15445 // break before it.
15446 verifyFormat("a << \"line line \"\n"
15447 " \"line\";",
15448 "a << \"line line line\";", getLLVMStyleWithColumns(20));
15449 // In a chain of << with three operands, the second can be broken with no line
15450 // break before it.
15451 verifyFormat("abcde << \"line \"\n"
15452 " \"line line\"\n"
15453 " << c;",
15454 "abcde << \"line line line\" << c;",
15455 getLLVMStyleWithColumns(20));
15456 // In a chain of << with three operands, the third must be broken with a line
15457 // break before it.
15458 verifyFormat("a << b\n"
15459 " << \"line line \"\n"
15460 " \"line\";",
15461 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
15462 // In a chain of << with three operands, the second can be broken with no line
15463 // break before it and the third must be broken with a line break before it.
15464 verifyFormat("abcd << \"line line \"\n"
15465 " \"line\"\n"
15466 " << \"line line \"\n"
15467 " \"line\";",
15468 "abcd << \"line line line\" << \"line line line\";",
15469 getLLVMStyleWithColumns(20));
15470 // In a chain of binary operators with two operands, the second can be broken
15471 // with no line break before it.
15472 verifyFormat("abcd + \"line line \"\n"
15473 " \"line line\";",
15474 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
15475 // In a chain of binary operators with three operands, the second must be
15476 // broken with a line break before it.
15477 verifyFormat("abcd +\n"
15478 " \"line line \"\n"
15479 " \"line line\" +\n"
15480 " e;",
15481 "abcd + \"line line line line\" + e;",
15482 getLLVMStyleWithColumns(20));
15483 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15484 // the first must be broken with a line break before it.
15485 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 25);
15486 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15487 verifyFormat("someFunction(\n"
15488 " \"long long long \"\n"
15489 " \"long\",\n"
15490 " a);",
15491 "someFunction(\"long long long long\", a);", Style);
15492 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15493 verifyFormat("someFunction(\n"
15494 " \"long long long \"\n"
15495 " \"long\",\n"
15496 " a\n"
15497 ");",
15498 Style);
15499}
15500
15501TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15502 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15505 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
15507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
15508}
15509
15510TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15511 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
15512 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
15513 verifyFormat("fffffffffff(g(R\"x(\n"
15514 "multiline raw string literal xxxxxxxxxxxxxx\n"
15515 ")x\",\n"
15516 " a),\n"
15517 " b);",
15518 "fffffffffff(g(R\"x(\n"
15519 "multiline raw string literal xxxxxxxxxxxxxx\n"
15520 ")x\", a), b);",
15521 getGoogleStyleWithColumns(20));
15522 verifyFormat("fffffffffff(\n"
15523 " g(R\"x(qqq\n"
15524 "multiline raw string literal xxxxxxxxxxxxxx\n"
15525 ")x\",\n"
15526 " a),\n"
15527 " b);",
15528 "fffffffffff(g(R\"x(qqq\n"
15529 "multiline raw string literal xxxxxxxxxxxxxx\n"
15530 ")x\", a), b);",
15531 getGoogleStyleWithColumns(20));
15532
15533 verifyNoChange("fffffffffff(R\"x(\n"
15534 "multiline raw string literal xxxxxxxxxxxxxx\n"
15535 ")x\");",
15536 getGoogleStyleWithColumns(20));
15537 verifyFormat("fffffffffff(R\"x(\n"
15538 "multiline raw string literal xxxxxxxxxxxxxx\n"
15539 ")x\" + bbbbbb);",
15540 "fffffffffff(R\"x(\n"
15541 "multiline raw string literal xxxxxxxxxxxxxx\n"
15542 ")x\" + bbbbbb);",
15543 getGoogleStyleWithColumns(20));
15544 verifyFormat("fffffffffff(\n"
15545 " R\"x(\n"
15546 "multiline raw string literal xxxxxxxxxxxxxx\n"
15547 ")x\" +\n"
15548 " bbbbbb);",
15549 "fffffffffff(\n"
15550 " R\"x(\n"
15551 "multiline raw string literal xxxxxxxxxxxxxx\n"
15552 ")x\" + bbbbbb);",
15553 getGoogleStyleWithColumns(20));
15554 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
15555 "fffffffffff(\n"
15556 " R\"(single line raw string)\" + bbbbbb);");
15557}
15558
15559TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15560 verifyFormat("string a = \"unterminated;");
15561 verifyFormat("function(\"unterminated,\n"
15562 " OtherParameter);",
15563 "function( \"unterminated,\n"
15564 " OtherParameter);");
15565}
15566
15567TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15568 FormatStyle Style = getLLVMStyle();
15569 Style.Standard = FormatStyle::LS_Cpp03;
15570 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15571 "#define x(_a) printf(\"foo\"_a);", Style);
15572}
15573
15574TEST_F(FormatTest, CppLexVersion) {
15575 FormatStyle Style = getLLVMStyle();
15576 // Formatting of x * y differs if x is a type.
15577 verifyFormat("void foo() { MACRO(a * b); }", Style);
15578 verifyFormat("void foo() { MACRO(int *b); }", Style);
15579
15580 // LLVM style uses latest lexer.
15581 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15582 Style.Standard = FormatStyle::LS_Cpp17;
15583 // But in c++17, char8_t isn't a keyword.
15584 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15585}
15586
15587TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15588
15589TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15590 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15591 " \"ddeeefff\");",
15592 "someFunction(\"aaabbbcccdddeeefff\");",
15593 getLLVMStyleWithColumns(25));
15594 verifyFormat("someFunction1234567890(\n"
15595 " \"aaabbbcccdddeeefff\");",
15596 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15597 getLLVMStyleWithColumns(26));
15598 verifyFormat("someFunction1234567890(\n"
15599 " \"aaabbbcccdddeeeff\"\n"
15600 " \"f\");",
15601 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15602 getLLVMStyleWithColumns(25));
15603 verifyFormat("someFunction1234567890(\n"
15604 " \"aaabbbcccdddeeeff\"\n"
15605 " \"f\");",
15606 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15607 getLLVMStyleWithColumns(24));
15608 verifyFormat("someFunction(\n"
15609 " \"aaabbbcc ddde \"\n"
15610 " \"efff\");",
15611 "someFunction(\"aaabbbcc ddde efff\");",
15612 getLLVMStyleWithColumns(25));
15613 verifyFormat("someFunction(\"aaabbbccc \"\n"
15614 " \"ddeeefff\");",
15615 "someFunction(\"aaabbbccc ddeeefff\");",
15616 getLLVMStyleWithColumns(25));
15617 verifyFormat("someFunction1234567890(\n"
15618 " \"aaabb \"\n"
15619 " \"cccdddeeefff\");",
15620 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15621 getLLVMStyleWithColumns(25));
15622 verifyFormat("#define A \\\n"
15623 " string s = \\\n"
15624 " \"123456789\" \\\n"
15625 " \"0\"; \\\n"
15626 " int i;",
15627 "#define A string s = \"1234567890\"; int i;",
15628 getLLVMStyleWithColumns(20));
15629 verifyFormat("someFunction(\n"
15630 " \"aaabbbcc \"\n"
15631 " \"dddeeefff\");",
15632 "someFunction(\"aaabbbcc dddeeefff\");",
15633 getLLVMStyleWithColumns(25));
15634}
15635
15636TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15637 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15638 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15639 // FIXME: unstable test case
15640 EXPECT_EQ("\"test\"\n"
15641 "\"\\n\"",
15642 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15643 // FIXME: unstable test case
15644 EXPECT_EQ("\"tes\\\\\"\n"
15645 "\"n\"",
15646 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15647 // FIXME: unstable test case
15648 EXPECT_EQ("\"\\\\\\\\\"\n"
15649 "\"\\n\"",
15650 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15651 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15652 // FIXME: unstable test case
15653 EXPECT_EQ("\"\\uff01\"\n"
15654 "\"test\"",
15655 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15656 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15657 // FIXME: unstable test case
15658 EXPECT_EQ("\"\\x000000000001\"\n"
15659 "\"next\"",
15660 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15661 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15662 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15663 // FIXME: unstable test case
15664 EXPECT_EQ("\"test\"\n"
15665 "\"\\000000\"\n"
15666 "\"000001\"",
15667 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15668 // FIXME: unstable test case
15669 EXPECT_EQ("\"test\\000\"\n"
15670 "\"00000000\"\n"
15671 "\"1\"",
15672 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15673}
15674
15675TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15676 verifyFormat("void f() {\n"
15677 " return g() {}\n"
15678 " void h() {}");
15679 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15680 "g();\n"
15681 "}");
15682}
15683
15684TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15685 verifyFormat(
15686 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15687}
15688
15689TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15690 verifyFormat("class X {\n"
15691 " void f() {\n"
15692 " }\n"
15693 "};",
15694 getLLVMStyleWithColumns(12));
15695}
15696
15697TEST_F(FormatTest, ConfigurableIndentWidth) {
15698 FormatStyle EightIndent = getLLVMStyleWithColumns(ColumnLimit: 18);
15699 EightIndent.IndentWidth = 8;
15700 EightIndent.ContinuationIndentWidth = 8;
15701 verifyFormat("void f() {\n"
15702 " someFunction();\n"
15703 " if (true) {\n"
15704 " f();\n"
15705 " }\n"
15706 "}",
15707 EightIndent);
15708 verifyFormat("class X {\n"
15709 " void f() {\n"
15710 " }\n"
15711 "};",
15712 EightIndent);
15713 verifyFormat("int x[] = {\n"
15714 " call(),\n"
15715 " call()};",
15716 EightIndent);
15717}
15718
15719TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15720 verifyFormat("double\n"
15721 "f();",
15722 getLLVMStyleWithColumns(8));
15723}
15724
15725TEST_F(FormatTest, ConfigurableUseOfTab) {
15726 FormatStyle Tab = getLLVMStyleWithColumns(ColumnLimit: 42);
15727 Tab.IndentWidth = 8;
15728 Tab.UseTab = FormatStyle::UT_Always;
15729 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15730
15731 verifyFormat("if (aaaaaaaa && // q\n"
15732 " bb)\t\t// w\n"
15733 "\t;",
15734 "if (aaaaaaaa &&// q\n"
15735 "bb)// w\n"
15736 ";",
15737 Tab);
15738 verifyFormat("if (aaa && bbb) // w\n"
15739 "\t;",
15740 "if(aaa&&bbb)// w\n"
15741 ";",
15742 Tab);
15743
15744 verifyFormat("class X {\n"
15745 "\tvoid f() {\n"
15746 "\t\tsomeFunction(parameter1,\n"
15747 "\t\t\t parameter2);\n"
15748 "\t}\n"
15749 "};",
15750 Tab);
15751 verifyFormat("#define A \\\n"
15752 "\tvoid f() { \\\n"
15753 "\t\tsomeFunction( \\\n"
15754 "\t\t parameter1, \\\n"
15755 "\t\t parameter2); \\\n"
15756 "\t}",
15757 Tab);
15758 verifyFormat("int a;\t // x\n"
15759 "int bbbbbbbb; // x",
15760 Tab);
15761
15762 FormatStyle TabAlignment = Tab;
15763 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
15764 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15765 verifyFormat("unsigned long long big;\n"
15766 "char*\t\t ptr;",
15767 TabAlignment);
15768 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15769 verifyFormat("unsigned long long big;\n"
15770 "char *\t\t ptr;",
15771 TabAlignment);
15772 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15773 verifyFormat("unsigned long long big;\n"
15774 "char\t\t *ptr;",
15775 TabAlignment);
15776
15777 Tab.TabWidth = 4;
15778 Tab.IndentWidth = 8;
15779 verifyFormat("class TabWidth4Indent8 {\n"
15780 "\t\tvoid f() {\n"
15781 "\t\t\t\tsomeFunction(parameter1,\n"
15782 "\t\t\t\t\t\t\t parameter2);\n"
15783 "\t\t}\n"
15784 "};",
15785 Tab);
15786
15787 Tab.TabWidth = 4;
15788 Tab.IndentWidth = 4;
15789 verifyFormat("class TabWidth4Indent4 {\n"
15790 "\tvoid f() {\n"
15791 "\t\tsomeFunction(parameter1,\n"
15792 "\t\t\t\t\t parameter2);\n"
15793 "\t}\n"
15794 "};",
15795 Tab);
15796
15797 Tab.TabWidth = 8;
15798 Tab.IndentWidth = 4;
15799 verifyFormat("class TabWidth8Indent4 {\n"
15800 " void f() {\n"
15801 "\tsomeFunction(parameter1,\n"
15802 "\t\t parameter2);\n"
15803 " }\n"
15804 "};",
15805 Tab);
15806
15807 Tab.TabWidth = 8;
15808 Tab.IndentWidth = 8;
15809 verifyFormat("/*\n"
15810 "\t a\t\tcomment\n"
15811 "\t in multiple lines\n"
15812 " */",
15813 " /*\t \t \n"
15814 " \t \t a\t\tcomment\t \t\n"
15815 " \t \t in multiple lines\t\n"
15816 " \t */",
15817 Tab);
15818
15819 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
15820 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
15821 verifyFormat("void f() {\n"
15822 "\tunsigned long long big;\n"
15823 "\tchar* ptr;\n"
15824 "}",
15825 TabAlignment);
15826 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
15827 verifyFormat("void f() {\n"
15828 "\tunsigned long long big;\n"
15829 "\tchar * ptr;\n"
15830 "}",
15831 TabAlignment);
15832 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
15833 verifyFormat("void f() {\n"
15834 "\tunsigned long long big;\n"
15835 "\tchar *ptr;\n"
15836 "}",
15837 TabAlignment);
15838
15839 Tab.UseTab = FormatStyle::UT_ForIndentation;
15840 verifyFormat("{\n"
15841 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15842 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15843 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15844 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15845 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15846 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15847 "};",
15848 Tab);
15849 verifyFormat("enum AA {\n"
15850 "\ta1, // Force multiple lines\n"
15851 "\ta2,\n"
15852 "\ta3\n"
15853 "};",
15854 Tab);
15855 verifyFormat("if (aaaaaaaa && // q\n"
15856 " bb) // w\n"
15857 "\t;",
15858 "if (aaaaaaaa &&// q\n"
15859 "bb)// w\n"
15860 ";",
15861 Tab);
15862 verifyFormat("class X {\n"
15863 "\tvoid f() {\n"
15864 "\t\tsomeFunction(parameter1,\n"
15865 "\t\t parameter2);\n"
15866 "\t}\n"
15867 "};",
15868 Tab);
15869 verifyFormat("{\n"
15870 "\tQ(\n"
15871 "\t {\n"
15872 "\t\t int a;\n"
15873 "\t\t someFunction(aaaaaaaa,\n"
15874 "\t\t bbbbbbb);\n"
15875 "\t },\n"
15876 "\t p);\n"
15877 "}",
15878 Tab);
15879 verifyFormat("{\n"
15880 "\t/* aaaa\n"
15881 "\t bbbb */\n"
15882 "}",
15883 "{\n"
15884 "/* aaaa\n"
15885 " bbbb */\n"
15886 "}",
15887 Tab);
15888 verifyFormat("{\n"
15889 "\t/*\n"
15890 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15891 "\t bbbbbbbbbbbbb\n"
15892 "\t*/\n"
15893 "}",
15894 "{\n"
15895 "/*\n"
15896 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15897 "*/\n"
15898 "}",
15899 Tab);
15900 verifyFormat("{\n"
15901 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15902 "\t// bbbbbbbbbbbbb\n"
15903 "}",
15904 "{\n"
15905 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15906 "}",
15907 Tab);
15908 verifyFormat("{\n"
15909 "\t/*\n"
15910 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15911 "\t bbbbbbbbbbbbb\n"
15912 "\t*/\n"
15913 "}",
15914 "{\n"
15915 "\t/*\n"
15916 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15917 "\t*/\n"
15918 "}",
15919 Tab);
15920 verifyNoChange("{\n"
15921 "\t/*\n"
15922 "\n"
15923 "\t*/\n"
15924 "}",
15925 Tab);
15926 verifyNoChange("{\n"
15927 "\t/*\n"
15928 " asdf\n"
15929 "\t*/\n"
15930 "}",
15931 Tab);
15932
15933 verifyFormat("void f() {\n"
15934 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
15935 "\t : bbbbbbbbbbbbbbbbbb\n"
15936 "}",
15937 Tab);
15938 FormatStyle TabNoBreak = Tab;
15939 TabNoBreak.BreakBeforeTernaryOperators = false;
15940 verifyFormat("void f() {\n"
15941 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
15942 "\t bbbbbbbbbbbbbbbbbb\n"
15943 "}",
15944 TabNoBreak);
15945 verifyFormat("void f() {\n"
15946 "\treturn true ?\n"
15947 "\t aaaaaaaaaaaaaaaaaaaa :\n"
15948 "\t bbbbbbbbbbbbbbbbbbbb\n"
15949 "}",
15950 TabNoBreak);
15951
15952 Tab.UseTab = FormatStyle::UT_Never;
15953 verifyFormat("/*\n"
15954 " a\t\tcomment\n"
15955 " in multiple lines\n"
15956 " */",
15957 " /*\t \t \n"
15958 " \t \t a\t\tcomment\t \t\n"
15959 " \t \t in multiple lines\t\n"
15960 " \t */",
15961 Tab);
15962 verifyFormat("/* some\n"
15963 " comment */",
15964 " \t \t /* some\n"
15965 " \t \t comment */",
15966 Tab);
15967 verifyFormat("int a; /* some\n"
15968 " comment */",
15969 " \t \t int a; /* some\n"
15970 " \t \t comment */",
15971 Tab);
15972
15973 verifyFormat("int a; /* some\n"
15974 "comment */",
15975 " \t \t int\ta; /* some\n"
15976 " \t \t comment */",
15977 Tab);
15978 verifyFormat("f(\"\t\t\"); /* some\n"
15979 " comment */",
15980 " \t \t f(\"\t\t\"); /* some\n"
15981 " \t \t comment */",
15982 Tab);
15983 verifyFormat("{\n"
15984 " /*\n"
15985 " * Comment\n"
15986 " */\n"
15987 " int i;\n"
15988 "}",
15989 "{\n"
15990 "\t/*\n"
15991 "\t * Comment\n"
15992 "\t */\n"
15993 "\t int i;\n"
15994 "}",
15995 Tab);
15996
15997 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
15998 Tab.TabWidth = 8;
15999 Tab.IndentWidth = 8;
16000 verifyFormat("if (aaaaaaaa && // q\n"
16001 " bb) // w\n"
16002 "\t;",
16003 "if (aaaaaaaa &&// q\n"
16004 "bb)// w\n"
16005 ";",
16006 Tab);
16007 verifyFormat("if (aaa && bbb) // w\n"
16008 "\t;",
16009 "if(aaa&&bbb)// w\n"
16010 ";",
16011 Tab);
16012 verifyFormat("class X {\n"
16013 "\tvoid f() {\n"
16014 "\t\tsomeFunction(parameter1,\n"
16015 "\t\t\t parameter2);\n"
16016 "\t}\n"
16017 "};",
16018 Tab);
16019 verifyFormat("#define A \\\n"
16020 "\tvoid f() { \\\n"
16021 "\t\tsomeFunction( \\\n"
16022 "\t\t parameter1, \\\n"
16023 "\t\t parameter2); \\\n"
16024 "\t}",
16025 Tab);
16026 Tab.TabWidth = 4;
16027 Tab.IndentWidth = 8;
16028 verifyFormat("class TabWidth4Indent8 {\n"
16029 "\t\tvoid f() {\n"
16030 "\t\t\t\tsomeFunction(parameter1,\n"
16031 "\t\t\t\t\t\t\t parameter2);\n"
16032 "\t\t}\n"
16033 "};",
16034 Tab);
16035 Tab.TabWidth = 4;
16036 Tab.IndentWidth = 4;
16037 verifyFormat("class TabWidth4Indent4 {\n"
16038 "\tvoid f() {\n"
16039 "\t\tsomeFunction(parameter1,\n"
16040 "\t\t\t\t\t parameter2);\n"
16041 "\t}\n"
16042 "};",
16043 Tab);
16044 Tab.TabWidth = 8;
16045 Tab.IndentWidth = 4;
16046 verifyFormat("class TabWidth8Indent4 {\n"
16047 " void f() {\n"
16048 "\tsomeFunction(parameter1,\n"
16049 "\t\t parameter2);\n"
16050 " }\n"
16051 "};",
16052 Tab);
16053 Tab.TabWidth = 8;
16054 Tab.IndentWidth = 8;
16055 verifyFormat("/*\n"
16056 "\t a\t\tcomment\n"
16057 "\t in multiple lines\n"
16058 " */",
16059 " /*\t \t \n"
16060 " \t \t a\t\tcomment\t \t\n"
16061 " \t \t in multiple lines\t\n"
16062 " \t */",
16063 Tab);
16064 verifyFormat("{\n"
16065 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16066 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16067 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16068 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16069 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16070 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16071 "};",
16072 Tab);
16073 verifyFormat("enum AA {\n"
16074 "\ta1, // Force multiple lines\n"
16075 "\ta2,\n"
16076 "\ta3\n"
16077 "};",
16078 Tab);
16079 verifyFormat("if (aaaaaaaa && // q\n"
16080 " bb) // w\n"
16081 "\t;",
16082 "if (aaaaaaaa &&// q\n"
16083 "bb)// w\n"
16084 ";",
16085 Tab);
16086 verifyFormat("class X {\n"
16087 "\tvoid f() {\n"
16088 "\t\tsomeFunction(parameter1,\n"
16089 "\t\t\t parameter2);\n"
16090 "\t}\n"
16091 "};",
16092 Tab);
16093 verifyFormat("{\n"
16094 "\tQ(\n"
16095 "\t {\n"
16096 "\t\t int a;\n"
16097 "\t\t someFunction(aaaaaaaa,\n"
16098 "\t\t\t\t bbbbbbb);\n"
16099 "\t },\n"
16100 "\t p);\n"
16101 "}",
16102 Tab);
16103 verifyFormat("{\n"
16104 "\t/* aaaa\n"
16105 "\t bbbb */\n"
16106 "}",
16107 "{\n"
16108 "/* aaaa\n"
16109 " bbbb */\n"
16110 "}",
16111 Tab);
16112 verifyFormat("{\n"
16113 "\t/*\n"
16114 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16115 "\t bbbbbbbbbbbbb\n"
16116 "\t*/\n"
16117 "}",
16118 "{\n"
16119 "/*\n"
16120 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16121 "*/\n"
16122 "}",
16123 Tab);
16124 verifyFormat("{\n"
16125 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16126 "\t// bbbbbbbbbbbbb\n"
16127 "}",
16128 "{\n"
16129 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16130 "}",
16131 Tab);
16132 verifyFormat("{\n"
16133 "\t/*\n"
16134 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16135 "\t bbbbbbbbbbbbb\n"
16136 "\t*/\n"
16137 "}",
16138 "{\n"
16139 "\t/*\n"
16140 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16141 "\t*/\n"
16142 "}",
16143 Tab);
16144 verifyNoChange("{\n"
16145 "\t/*\n"
16146 "\n"
16147 "\t*/\n"
16148 "}",
16149 Tab);
16150 verifyNoChange("{\n"
16151 "\t/*\n"
16152 " asdf\n"
16153 "\t*/\n"
16154 "}",
16155 Tab);
16156 verifyFormat("/* some\n"
16157 " comment */",
16158 " \t \t /* some\n"
16159 " \t \t comment */",
16160 Tab);
16161 verifyFormat("int a; /* some\n"
16162 " comment */",
16163 " \t \t int a; /* some\n"
16164 " \t \t comment */",
16165 Tab);
16166 verifyFormat("int a; /* some\n"
16167 "comment */",
16168 " \t \t int\ta; /* some\n"
16169 " \t \t comment */",
16170 Tab);
16171 verifyFormat("f(\"\t\t\"); /* some\n"
16172 " comment */",
16173 " \t \t f(\"\t\t\"); /* some\n"
16174 " \t \t comment */",
16175 Tab);
16176 verifyFormat("{\n"
16177 "\t/*\n"
16178 "\t * Comment\n"
16179 "\t */\n"
16180 "\tint i;\n"
16181 "}",
16182 "{\n"
16183 "\t/*\n"
16184 "\t * Comment\n"
16185 "\t */\n"
16186 "\t int i;\n"
16187 "}",
16188 Tab);
16189 Tab.TabWidth = 2;
16190 Tab.IndentWidth = 2;
16191 verifyFormat("{\n"
16192 "\t/* aaaa\n"
16193 "\t\t bbbb */\n"
16194 "}",
16195 "{\n"
16196 "/* aaaa\n"
16197 "\t bbbb */\n"
16198 "}",
16199 Tab);
16200 verifyFormat("{\n"
16201 "\t/*\n"
16202 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16203 "\t\tbbbbbbbbbbbbb\n"
16204 "\t*/\n"
16205 "}",
16206 "{\n"
16207 "/*\n"
16208 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16209 "*/\n"
16210 "}",
16211 Tab);
16212 Tab.AlignConsecutiveAssignments.Enabled = true;
16213 Tab.AlignConsecutiveDeclarations.Enabled = true;
16214 Tab.TabWidth = 4;
16215 Tab.IndentWidth = 4;
16216 verifyFormat("class Assign {\n"
16217 "\tvoid f() {\n"
16218 "\t\tint x = 123;\n"
16219 "\t\tint random = 4;\n"
16220 "\t\tstd::string alphabet =\n"
16221 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16222 "\t}\n"
16223 "};",
16224 Tab);
16225
16226 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16227 Tab.TabWidth = 8;
16228 Tab.IndentWidth = 8;
16229 verifyFormat("if (aaaaaaaa && // q\n"
16230 " bb) // w\n"
16231 "\t;",
16232 "if (aaaaaaaa &&// q\n"
16233 "bb)// w\n"
16234 ";",
16235 Tab);
16236 verifyFormat("if (aaa && bbb) // w\n"
16237 "\t;",
16238 "if(aaa&&bbb)// w\n"
16239 ";",
16240 Tab);
16241 verifyFormat("class X {\n"
16242 "\tvoid f() {\n"
16243 "\t\tsomeFunction(parameter1,\n"
16244 "\t\t parameter2);\n"
16245 "\t}\n"
16246 "};",
16247 Tab);
16248 verifyFormat("#define A \\\n"
16249 "\tvoid f() { \\\n"
16250 "\t\tsomeFunction( \\\n"
16251 "\t\t parameter1, \\\n"
16252 "\t\t parameter2); \\\n"
16253 "\t}",
16254 Tab);
16255 Tab.TabWidth = 4;
16256 Tab.IndentWidth = 8;
16257 verifyFormat("class TabWidth4Indent8 {\n"
16258 "\t\tvoid f() {\n"
16259 "\t\t\t\tsomeFunction(parameter1,\n"
16260 "\t\t\t\t parameter2);\n"
16261 "\t\t}\n"
16262 "};",
16263 Tab);
16264 Tab.TabWidth = 4;
16265 Tab.IndentWidth = 4;
16266 verifyFormat("class TabWidth4Indent4 {\n"
16267 "\tvoid f() {\n"
16268 "\t\tsomeFunction(parameter1,\n"
16269 "\t\t parameter2);\n"
16270 "\t}\n"
16271 "};",
16272 Tab);
16273 Tab.TabWidth = 8;
16274 Tab.IndentWidth = 4;
16275 verifyFormat("class TabWidth8Indent4 {\n"
16276 " void f() {\n"
16277 "\tsomeFunction(parameter1,\n"
16278 "\t parameter2);\n"
16279 " }\n"
16280 "};",
16281 Tab);
16282 Tab.TabWidth = 8;
16283 Tab.IndentWidth = 8;
16284 verifyFormat("/*\n"
16285 " a\t\tcomment\n"
16286 " in multiple lines\n"
16287 " */",
16288 " /*\t \t \n"
16289 " \t \t a\t\tcomment\t \t\n"
16290 " \t \t in multiple lines\t\n"
16291 " \t */",
16292 Tab);
16293 verifyFormat("{\n"
16294 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16295 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16296 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16297 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16298 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16299 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16300 "};",
16301 Tab);
16302 verifyFormat("enum AA {\n"
16303 "\ta1, // Force multiple lines\n"
16304 "\ta2,\n"
16305 "\ta3\n"
16306 "};",
16307 Tab);
16308 verifyFormat("if (aaaaaaaa && // q\n"
16309 " bb) // w\n"
16310 "\t;",
16311 "if (aaaaaaaa &&// q\n"
16312 "bb)// w\n"
16313 ";",
16314 Tab);
16315 verifyFormat("class X {\n"
16316 "\tvoid f() {\n"
16317 "\t\tsomeFunction(parameter1,\n"
16318 "\t\t parameter2);\n"
16319 "\t}\n"
16320 "};",
16321 Tab);
16322 verifyFormat("{\n"
16323 "\tQ(\n"
16324 "\t {\n"
16325 "\t\t int a;\n"
16326 "\t\t someFunction(aaaaaaaa,\n"
16327 "\t\t bbbbbbb);\n"
16328 "\t },\n"
16329 "\t p);\n"
16330 "}",
16331 Tab);
16332 verifyFormat("{\n"
16333 "\t/* aaaa\n"
16334 "\t bbbb */\n"
16335 "}",
16336 "{\n"
16337 "/* aaaa\n"
16338 " bbbb */\n"
16339 "}",
16340 Tab);
16341 verifyFormat("{\n"
16342 "\t/*\n"
16343 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16344 "\t bbbbbbbbbbbbb\n"
16345 "\t*/\n"
16346 "}",
16347 "{\n"
16348 "/*\n"
16349 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16350 "*/\n"
16351 "}",
16352 Tab);
16353 verifyFormat("{\n"
16354 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16355 "\t// bbbbbbbbbbbbb\n"
16356 "}",
16357 "{\n"
16358 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16359 "}",
16360 Tab);
16361 verifyFormat("{\n"
16362 "\t/*\n"
16363 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16364 "\t bbbbbbbbbbbbb\n"
16365 "\t*/\n"
16366 "}",
16367 "{\n"
16368 "\t/*\n"
16369 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16370 "\t*/\n"
16371 "}",
16372 Tab);
16373 verifyNoChange("{\n"
16374 "\t/*\n"
16375 "\n"
16376 "\t*/\n"
16377 "}",
16378 Tab);
16379 verifyNoChange("{\n"
16380 "\t/*\n"
16381 " asdf\n"
16382 "\t*/\n"
16383 "}",
16384 Tab);
16385 verifyFormat("/* some\n"
16386 " comment */",
16387 " \t \t /* some\n"
16388 " \t \t comment */",
16389 Tab);
16390 verifyFormat("int a; /* some\n"
16391 " comment */",
16392 " \t \t int a; /* some\n"
16393 " \t \t comment */",
16394 Tab);
16395 verifyFormat("int a; /* some\n"
16396 "comment */",
16397 " \t \t int\ta; /* some\n"
16398 " \t \t comment */",
16399 Tab);
16400 verifyFormat("f(\"\t\t\"); /* some\n"
16401 " comment */",
16402 " \t \t f(\"\t\t\"); /* some\n"
16403 " \t \t comment */",
16404 Tab);
16405 verifyFormat("{\n"
16406 "\t/*\n"
16407 "\t * Comment\n"
16408 "\t */\n"
16409 "\tint i;\n"
16410 "}",
16411 "{\n"
16412 "\t/*\n"
16413 "\t * Comment\n"
16414 "\t */\n"
16415 "\t int i;\n"
16416 "}",
16417 Tab);
16418 Tab.TabWidth = 2;
16419 Tab.IndentWidth = 2;
16420 verifyFormat("{\n"
16421 "\t/* aaaa\n"
16422 "\t bbbb */\n"
16423 "}",
16424 "{\n"
16425 "/* aaaa\n"
16426 " bbbb */\n"
16427 "}",
16428 Tab);
16429 verifyFormat("{\n"
16430 "\t/*\n"
16431 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16432 "\t bbbbbbbbbbbbb\n"
16433 "\t*/\n"
16434 "}",
16435 "{\n"
16436 "/*\n"
16437 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16438 "*/\n"
16439 "}",
16440 Tab);
16441 Tab.AlignConsecutiveAssignments.Enabled = true;
16442 Tab.AlignConsecutiveDeclarations.Enabled = true;
16443 Tab.TabWidth = 4;
16444 Tab.IndentWidth = 4;
16445 verifyFormat("class Assign {\n"
16446 "\tvoid f() {\n"
16447 "\t\tint x = 123;\n"
16448 "\t\tint random = 4;\n"
16449 "\t\tstd::string alphabet =\n"
16450 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16451 "\t}\n"
16452 "};",
16453 Tab);
16454 Tab.AlignOperands = FormatStyle::OAS_Align;
16455 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16456 " cccccccccccccccccccc;",
16457 Tab);
16458 // no alignment
16459 verifyFormat("int aaaaaaaaaa =\n"
16460 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16461 Tab);
16462 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16463 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16464 " : 333333333333333;",
16465 Tab);
16466 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
16467 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
16468 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16469 " + cccccccccccccccccccc;",
16470 Tab);
16471}
16472
16473TEST_F(FormatTest, ZeroTabWidth) {
16474 FormatStyle Tab = getLLVMStyleWithColumns(ColumnLimit: 42);
16475 Tab.IndentWidth = 8;
16476 Tab.UseTab = FormatStyle::UT_Never;
16477 Tab.TabWidth = 0;
16478 verifyFormat("void a() {\n"
16479 " // line starts with '\t'\n"
16480 "};",
16481 "void a(){\n"
16482 "\t// line starts with '\t'\n"
16483 "};",
16484 Tab);
16485
16486 verifyFormat("void a() {\n"
16487 " // line starts with '\t'\n"
16488 "};",
16489 "void a(){\n"
16490 "\t\t// line starts with '\t'\n"
16491 "};",
16492 Tab);
16493
16494 Tab.UseTab = FormatStyle::UT_ForIndentation;
16495 verifyFormat("void a() {\n"
16496 " // line starts with '\t'\n"
16497 "};",
16498 "void a(){\n"
16499 "\t// line starts with '\t'\n"
16500 "};",
16501 Tab);
16502
16503 verifyFormat("void a() {\n"
16504 " // line starts with '\t'\n"
16505 "};",
16506 "void a(){\n"
16507 "\t\t// line starts with '\t'\n"
16508 "};",
16509 Tab);
16510
16511 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16512 verifyFormat("void a() {\n"
16513 " // line starts with '\t'\n"
16514 "};",
16515 "void a(){\n"
16516 "\t// line starts with '\t'\n"
16517 "};",
16518 Tab);
16519
16520 verifyFormat("void a() {\n"
16521 " // line starts with '\t'\n"
16522 "};",
16523 "void a(){\n"
16524 "\t\t// line starts with '\t'\n"
16525 "};",
16526 Tab);
16527
16528 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16529 verifyFormat("void a() {\n"
16530 " // line starts with '\t'\n"
16531 "};",
16532 "void a(){\n"
16533 "\t// line starts with '\t'\n"
16534 "};",
16535 Tab);
16536
16537 verifyFormat("void a() {\n"
16538 " // line starts with '\t'\n"
16539 "};",
16540 "void a(){\n"
16541 "\t\t// line starts with '\t'\n"
16542 "};",
16543 Tab);
16544
16545 Tab.UseTab = FormatStyle::UT_Always;
16546 verifyFormat("void a() {\n"
16547 "// line starts with '\t'\n"
16548 "};",
16549 "void a(){\n"
16550 "\t// line starts with '\t'\n"
16551 "};",
16552 Tab);
16553
16554 verifyFormat("void a() {\n"
16555 "// line starts with '\t'\n"
16556 "};",
16557 "void a(){\n"
16558 "\t\t// line starts with '\t'\n"
16559 "};",
16560 Tab);
16561}
16562
16563TEST_F(FormatTest, CalculatesOriginalColumn) {
16564 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16565 "q\"; /* some\n"
16566 " comment */",
16567 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16568 "q\"; /* some\n"
16569 " comment */");
16570 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16571 "/* some\n"
16572 " comment */",
16573 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16574 " /* some\n"
16575 " comment */");
16576 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16577 "qqq\n"
16578 "/* some\n"
16579 " comment */",
16580 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16581 "qqq\n"
16582 " /* some\n"
16583 " comment */");
16584 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16585 "wwww; /* some\n"
16586 " comment */",
16587 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16588 "wwww; /* some\n"
16589 " comment */");
16590}
16591
16592TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
16593 FormatStyle NoSpace = getLLVMStyle();
16594 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
16595
16596 verifyFormat("while(true)\n"
16597 " continue;",
16598 NoSpace);
16599 verifyFormat("for(;;)\n"
16600 " continue;",
16601 NoSpace);
16602 verifyFormat("if(true)\n"
16603 " f();\n"
16604 "else if(true)\n"
16605 " f();",
16606 NoSpace);
16607 verifyFormat("do {\n"
16608 " do_something();\n"
16609 "} while(something());",
16610 NoSpace);
16611 verifyFormat("switch(x) {\n"
16612 "default:\n"
16613 " break;\n"
16614 "}",
16615 NoSpace);
16616 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
16617 verifyFormat("size_t x = sizeof(x);", NoSpace);
16618 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
16619 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
16620 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
16621 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
16622 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
16623 verifyFormat("alignas(128) char a[128];", NoSpace);
16624 verifyFormat("size_t x = alignof(MyType);", NoSpace);
16625 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
16626 verifyFormat("int f() throw(Deprecated);", NoSpace);
16627 verifyFormat("typedef void (*cb)(int);", NoSpace);
16628 verifyFormat("T A::operator()();", NoSpace);
16629 verifyFormat("X A::operator++(T);", NoSpace);
16630 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
16631 verifyFormat("#if (foo || bar) && baz\n"
16632 "#elif ((a || b) && c) || d\n"
16633 "#endif",
16634 NoSpace);
16635
16636 FormatStyle Space = getLLVMStyle();
16637 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
16638
16639 verifyFormat("int f ();", Space);
16640 verifyFormat("bool operator< ();", Space);
16641 verifyFormat("bool operator> ();", Space);
16642 verifyFormat("void f (int a, T b) {\n"
16643 " while (true)\n"
16644 " continue;\n"
16645 "}",
16646 Space);
16647 verifyFormat("if (true)\n"
16648 " f ();\n"
16649 "else if (true)\n"
16650 " f ();",
16651 Space);
16652 verifyFormat("do {\n"
16653 " do_something ();\n"
16654 "} while (something ());",
16655 Space);
16656 verifyFormat("switch (x) {\n"
16657 "default:\n"
16658 " break;\n"
16659 "}",
16660 Space);
16661 verifyFormat("A::A () : a (1) {}", Space);
16662 verifyFormat("void f () __attribute__ ((asdf));", Space);
16663 verifyFormat("*(&a + 1);\n"
16664 "&((&a)[1]);\n"
16665 "a[(b + c) * d];\n"
16666 "(((a + 1) * 2) + 3) * 4;",
16667 Space);
16668 verifyFormat("#define A(x) x", Space);
16669 verifyFormat("#define A (x) x", Space);
16670 verifyFormat("#if defined(x)\n"
16671 "#endif",
16672 Space);
16673 verifyFormat("auto i = std::make_unique<int> (5);", Space);
16674 verifyFormat("size_t x = sizeof (x);", Space);
16675 verifyFormat("auto f (int x) -> decltype (x);", Space);
16676 verifyFormat("auto f (int x) -> typeof (x);", Space);
16677 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
16678 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
16679 verifyFormat("int f (T x) noexcept (x.create ());", Space);
16680 verifyFormat("alignas (128) char a[128];", Space);
16681 verifyFormat("size_t x = alignof (MyType);", Space);
16682 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
16683 verifyFormat("int f () throw (Deprecated);", Space);
16684 verifyFormat("typedef void (*cb) (int);", Space);
16685 verifyFormat("T A::operator() ();", Space);
16686 verifyFormat("X A::operator++ (T);", Space);
16687 verifyFormat("auto lambda = [] () { return 0; };", Space);
16688 verifyFormat("int x = int (y);", Space);
16689 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
16690 verifyFormat("__builtin_LINE ()", Space);
16691 verifyFormat("__builtin_UNKNOWN ()", Space);
16692
16693 FormatStyle SomeSpace = getLLVMStyle();
16694 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
16695
16696 verifyFormat("[]() -> float {}", SomeSpace);
16697 verifyFormat("[] (auto foo) {}", SomeSpace);
16698 verifyFormat("[foo]() -> int {}", SomeSpace);
16699 verifyFormat("int f();", SomeSpace);
16700 verifyFormat("void f (int a, T b) {\n"
16701 " while (true)\n"
16702 " continue;\n"
16703 "}",
16704 SomeSpace);
16705 verifyFormat("if (true)\n"
16706 " f();\n"
16707 "else if (true)\n"
16708 " f();",
16709 SomeSpace);
16710 verifyFormat("do {\n"
16711 " do_something();\n"
16712 "} while (something());",
16713 SomeSpace);
16714 verifyFormat("switch (x) {\n"
16715 "default:\n"
16716 " break;\n"
16717 "}",
16718 SomeSpace);
16719 verifyFormat("A::A() : a (1) {}", SomeSpace);
16720 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
16721 verifyFormat("*(&a + 1);\n"
16722 "&((&a)[1]);\n"
16723 "a[(b + c) * d];\n"
16724 "(((a + 1) * 2) + 3) * 4;",
16725 SomeSpace);
16726 verifyFormat("#define A(x) x", SomeSpace);
16727 verifyFormat("#define A (x) x", SomeSpace);
16728 verifyFormat("#if defined(x)\n"
16729 "#endif",
16730 SomeSpace);
16731 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
16732 verifyFormat("size_t x = sizeof (x);", SomeSpace);
16733 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
16734 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
16735 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
16736 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
16737 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
16738 verifyFormat("alignas (128) char a[128];", SomeSpace);
16739 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
16740 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16741 SomeSpace);
16742 verifyFormat("int f() throw (Deprecated);", SomeSpace);
16743 verifyFormat("typedef void (*cb) (int);", SomeSpace);
16744 verifyFormat("T A::operator()();", SomeSpace);
16745 verifyFormat("X A::operator++ (T);", SomeSpace);
16746 verifyFormat("int x = int (y);", SomeSpace);
16747 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
16748
16749 FormatStyle SpaceControlStatements = getLLVMStyle();
16750 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16751 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
16752
16753 verifyFormat("while (true)\n"
16754 " continue;",
16755 SpaceControlStatements);
16756 verifyFormat("if (true)\n"
16757 " f();\n"
16758 "else if (true)\n"
16759 " f();",
16760 SpaceControlStatements);
16761 verifyFormat("for (;;) {\n"
16762 " do_something();\n"
16763 "}",
16764 SpaceControlStatements);
16765 verifyFormat("do {\n"
16766 " do_something();\n"
16767 "} while (something());",
16768 SpaceControlStatements);
16769 verifyFormat("switch (x) {\n"
16770 "default:\n"
16771 " break;\n"
16772 "}",
16773 SpaceControlStatements);
16774
16775 FormatStyle SpaceFuncDecl = getLLVMStyle();
16776 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16777 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
16778
16779 verifyFormat("int f ();", SpaceFuncDecl);
16780 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
16781 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
16782 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
16783 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
16784 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
16785 verifyFormat("#define A(x) x", SpaceFuncDecl);
16786 verifyFormat("#define A (x) x", SpaceFuncDecl);
16787 verifyFormat("#if defined(x)\n"
16788 "#endif",
16789 SpaceFuncDecl);
16790 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
16791 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
16792 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
16793 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
16794 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
16795 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
16796 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
16797 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
16798 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
16799 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16800 SpaceFuncDecl);
16801 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
16802 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
16803 verifyFormat("T A::operator()();", SpaceFuncDecl);
16804 verifyFormat("X A::operator++(T);", SpaceFuncDecl);
16805 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
16806 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
16807 verifyFormat("int x = int(y);", SpaceFuncDecl);
16808 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16809 SpaceFuncDecl);
16810
16811 FormatStyle SpaceFuncDef = getLLVMStyle();
16812 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16813 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
16814
16815 verifyFormat("int f();", SpaceFuncDef);
16816 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16817 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
16818 verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
16819 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
16820 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
16821 verifyFormat("#define A(x) x", SpaceFuncDef);
16822 verifyFormat("#define A (x) x", SpaceFuncDef);
16823 verifyFormat("#if defined(x)\n"
16824 "#endif",
16825 SpaceFuncDef);
16826 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
16827 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
16828 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
16829 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
16830 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
16831 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
16832 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
16833 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
16834 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
16835 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16836 SpaceFuncDef);
16837 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
16838 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
16839 verifyFormat("T A::operator()();", SpaceFuncDef);
16840 verifyFormat("X A::operator++(T);", SpaceFuncDef);
16841 verifyFormat("T A::operator()() {}", SpaceFuncDef);
16842 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
16843 verifyFormat("int x = int(y);", SpaceFuncDef);
16844 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16845 SpaceFuncDef);
16846
16847 FormatStyle SpaceIfMacros = getLLVMStyle();
16848 SpaceIfMacros.IfMacros.clear();
16849 SpaceIfMacros.IfMacros.push_back(x: "MYIF");
16850 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16851 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
16852 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
16853 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
16854 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
16855
16856 FormatStyle SpaceForeachMacros = getLLVMStyle();
16857 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
16858 FormatStyle::SBS_Never);
16859 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
16860 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16861 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
16862 verifyFormat("for (;;) {\n"
16863 "}",
16864 SpaceForeachMacros);
16865 verifyFormat("foreach (Item *item, itemlist) {\n"
16866 "}",
16867 SpaceForeachMacros);
16868 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
16869 "}",
16870 SpaceForeachMacros);
16871 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
16872 "}",
16873 SpaceForeachMacros);
16874 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
16875
16876 FormatStyle SomeSpace2 = getLLVMStyle();
16877 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16878 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
16879 verifyFormat("[]() -> float {}", SomeSpace2);
16880 verifyFormat("[] (auto foo) {}", SomeSpace2);
16881 verifyFormat("[foo]() -> int {}", SomeSpace2);
16882 verifyFormat("int f();", SomeSpace2);
16883 verifyFormat("void f (int a, T b) {\n"
16884 " while (true)\n"
16885 " continue;\n"
16886 "}",
16887 SomeSpace2);
16888 verifyFormat("if (true)\n"
16889 " f();\n"
16890 "else if (true)\n"
16891 " f();",
16892 SomeSpace2);
16893 verifyFormat("do {\n"
16894 " do_something();\n"
16895 "} while (something());",
16896 SomeSpace2);
16897 verifyFormat("switch (x) {\n"
16898 "default:\n"
16899 " break;\n"
16900 "}",
16901 SomeSpace2);
16902 verifyFormat("A::A() : a (1) {}", SomeSpace2);
16903 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
16904 verifyFormat("*(&a + 1);\n"
16905 "&((&a)[1]);\n"
16906 "a[(b + c) * d];\n"
16907 "(((a + 1) * 2) + 3) * 4;",
16908 SomeSpace2);
16909 verifyFormat("#define A(x) x", SomeSpace2);
16910 verifyFormat("#define A (x) x", SomeSpace2);
16911 verifyFormat("#if defined(x)\n"
16912 "#endif",
16913 SomeSpace2);
16914 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
16915 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
16916 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
16917 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
16918 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
16919 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
16920 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
16921 verifyFormat("alignas (128) char a[128];", SomeSpace2);
16922 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
16923 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16924 SomeSpace2);
16925 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
16926 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
16927 verifyFormat("T A::operator()();", SomeSpace2);
16928 verifyFormat("X A::operator++ (T);", SomeSpace2);
16929 verifyFormat("int x = int (y);", SomeSpace2);
16930 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
16931
16932 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
16933 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16934 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
16935 .AfterOverloadedOperator = true;
16936
16937 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
16938 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
16939 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
16940 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
16941
16942 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
16943 .AfterOverloadedOperator = false;
16944
16945 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
16946 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
16947 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
16948 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
16949
16950 auto SpaceAfterRequires = getLLVMStyle();
16951 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
16952 EXPECT_FALSE(
16953 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
16954 EXPECT_FALSE(
16955 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
16956 verifyFormat("void f(auto x)\n"
16957 " requires requires(int i) { x + i; }\n"
16958 "{}",
16959 SpaceAfterRequires);
16960 verifyFormat("void f(auto x)\n"
16961 " requires(requires(int i) { x + i; })\n"
16962 "{}",
16963 SpaceAfterRequires);
16964 verifyFormat("if (requires(int i) { x + i; })\n"
16965 " return;",
16966 SpaceAfterRequires);
16967 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
16968 verifyFormat("template <typename T>\n"
16969 " requires(Foo<T>)\n"
16970 "class Bar;",
16971 SpaceAfterRequires);
16972
16973 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
16974 verifyFormat("void f(auto x)\n"
16975 " requires requires(int i) { x + i; }\n"
16976 "{}",
16977 SpaceAfterRequires);
16978 verifyFormat("void f(auto x)\n"
16979 " requires (requires(int i) { x + i; })\n"
16980 "{}",
16981 SpaceAfterRequires);
16982 verifyFormat("if (requires(int i) { x + i; })\n"
16983 " return;",
16984 SpaceAfterRequires);
16985 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
16986 verifyFormat("template <typename T>\n"
16987 " requires (Foo<T>)\n"
16988 "class Bar;",
16989 SpaceAfterRequires);
16990
16991 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
16992 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
16993 verifyFormat("void f(auto x)\n"
16994 " requires requires (int i) { x + i; }\n"
16995 "{}",
16996 SpaceAfterRequires);
16997 verifyFormat("void f(auto x)\n"
16998 " requires(requires (int i) { x + i; })\n"
16999 "{}",
17000 SpaceAfterRequires);
17001 verifyFormat("if (requires (int i) { x + i; })\n"
17002 " return;",
17003 SpaceAfterRequires);
17004 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17005 verifyFormat("template <typename T>\n"
17006 " requires(Foo<T>)\n"
17007 "class Bar;",
17008 SpaceAfterRequires);
17009
17010 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17011 verifyFormat("void f(auto x)\n"
17012 " requires requires (int i) { x + i; }\n"
17013 "{}",
17014 SpaceAfterRequires);
17015 verifyFormat("void f(auto x)\n"
17016 " requires (requires (int i) { x + i; })\n"
17017 "{}",
17018 SpaceAfterRequires);
17019 verifyFormat("if (requires (int i) { x + i; })\n"
17020 " return;",
17021 SpaceAfterRequires);
17022 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17023 verifyFormat("template <typename T>\n"
17024 " requires (Foo<T>)\n"
17025 "class Bar;",
17026 SpaceAfterRequires);
17027}
17028
17029TEST_F(FormatTest, SpaceAfterLogicalNot) {
17030 FormatStyle Spaces = getLLVMStyle();
17031 Spaces.SpaceAfterLogicalNot = true;
17032
17033 verifyFormat("bool x = ! y", Spaces);
17034 verifyFormat("if (! isFailure())", Spaces);
17035 verifyFormat("if (! (a && b))", Spaces);
17036 verifyFormat("\"Error!\"", Spaces);
17037 verifyFormat("! ! x", Spaces);
17038}
17039
17040TEST_F(FormatTest, ConfigurableSpacesInParens) {
17041 FormatStyle Spaces = getLLVMStyle();
17042
17043 verifyFormat("do_something(::globalVar);", Spaces);
17044 verifyFormat("call(x, y, z);", Spaces);
17045 verifyFormat("call();", Spaces);
17046 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17047 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17048 Spaces);
17049 verifyFormat("while ((bool)1)\n"
17050 " continue;",
17051 Spaces);
17052 verifyFormat("for (;;)\n"
17053 " continue;",
17054 Spaces);
17055 verifyFormat("if (true)\n"
17056 " f();\n"
17057 "else if (true)\n"
17058 " f();",
17059 Spaces);
17060 verifyFormat("do {\n"
17061 " do_something((int)i);\n"
17062 "} while (something());",
17063 Spaces);
17064 verifyFormat("switch (x) {\n"
17065 "default:\n"
17066 " break;\n"
17067 "}",
17068 Spaces);
17069 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17070 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17071 verifyFormat("void f() __attribute__((asdf));", Spaces);
17072
17073 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17074 Spaces.SpacesInParensOptions = {};
17075 Spaces.SpacesInParensOptions.Other = true;
17076 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17077 verifyFormat("do_something( ::globalVar );", Spaces);
17078 verifyFormat("call( x, y, z );", Spaces);
17079 verifyFormat("call();", Spaces);
17080 verifyFormat("std::function<void( int, int )> callback;", Spaces);
17081 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17082 Spaces);
17083 verifyFormat("while ( (bool)1 )\n"
17084 " continue;",
17085 Spaces);
17086 verifyFormat("for ( ;; )\n"
17087 " continue;",
17088 Spaces);
17089 verifyFormat("if ( true )\n"
17090 " f();\n"
17091 "else if ( true )\n"
17092 " f();",
17093 Spaces);
17094 verifyFormat("do {\n"
17095 " do_something( (int)i );\n"
17096 "} while ( something() );",
17097 Spaces);
17098 verifyFormat("switch ( x ) {\n"
17099 "default:\n"
17100 " break;\n"
17101 "}",
17102 Spaces);
17103 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17104 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17105 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17106
17107 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17108 Spaces.SpacesInParensOptions = {};
17109 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17110 verifyFormat("Type *A = ( Type * )P;", Spaces);
17111 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17112 verifyFormat("x = ( int32 )y;", Spaces);
17113 verifyFormat("throw ( int32 )x;", Spaces);
17114 verifyFormat("int a = ( int )(2.0f);", Spaces);
17115 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17116 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17117 verifyFormat("#define x (( int )-1)", Spaces);
17118
17119 // Run the first set of tests again with:
17120 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17121 Spaces.SpacesInParensOptions = {};
17122 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17123 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17124 verifyFormat("call(x, y, z);", Spaces);
17125 verifyFormat("call( );", Spaces);
17126 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17127 verifyFormat("while (( bool )1)\n"
17128 " continue;",
17129 Spaces);
17130 verifyFormat("for (;;)\n"
17131 " continue;",
17132 Spaces);
17133 verifyFormat("if (true)\n"
17134 " f( );\n"
17135 "else if (true)\n"
17136 " f( );",
17137 Spaces);
17138 verifyFormat("do {\n"
17139 " do_something(( int )i);\n"
17140 "} while (something( ));",
17141 Spaces);
17142 verifyFormat("switch (x) {\n"
17143 "default:\n"
17144 " break;\n"
17145 "}",
17146 Spaces);
17147 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17148 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17149 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17150
17151 // Run the first set of tests again with:
17152 Spaces.SpaceAfterCStyleCast = true;
17153 verifyFormat("call(x, y, z);", Spaces);
17154 verifyFormat("call( );", Spaces);
17155 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17156 verifyFormat("while (( bool ) 1)\n"
17157 " continue;",
17158 Spaces);
17159 verifyFormat("for (;;)\n"
17160 " continue;",
17161 Spaces);
17162 verifyFormat("if (true)\n"
17163 " f( );\n"
17164 "else if (true)\n"
17165 " f( );",
17166 Spaces);
17167 verifyFormat("do {\n"
17168 " do_something(( int ) i);\n"
17169 "} while (something( ));",
17170 Spaces);
17171 verifyFormat("switch (x) {\n"
17172 "default:\n"
17173 " break;\n"
17174 "}",
17175 Spaces);
17176 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
17177 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
17178 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
17179 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
17180 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
17181 verifyFormat("throw ( int32 ) x;", Spaces);
17182 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17183 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17184 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17185
17186 // Run subset of tests again with:
17187 Spaces.SpacesInParensOptions.InCStyleCasts = false;
17188 Spaces.SpaceAfterCStyleCast = true;
17189 verifyFormat("while ((bool) 1)\n"
17190 " continue;",
17191 Spaces);
17192 verifyFormat("do {\n"
17193 " do_something((int) i);\n"
17194 "} while (something( ));",
17195 Spaces);
17196
17197 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
17198 verifyFormat("size_t idx = (size_t) a;", Spaces);
17199 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
17200 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17201 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17202 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17203 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17204 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
17205 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
17206 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
17207 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
17208 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
17209 verifyFormat("throw (int32) x;", Spaces);
17210 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17211 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17212 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17213
17214 Spaces.ColumnLimit = 80;
17215 Spaces.IndentWidth = 4;
17216 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
17217 verifyFormat("void foo( ) {\n"
17218 " size_t foo = (*(function))(\n"
17219 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17220 "BarrrrrrrrrrrrLong,\n"
17221 " FoooooooooLooooong);\n"
17222 "}",
17223 Spaces);
17224 Spaces.SpaceAfterCStyleCast = false;
17225 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17226 verifyFormat("size_t idx = (size_t)a;", Spaces);
17227 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17228 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17229 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17230 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17231 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17232
17233 verifyFormat("void foo( ) {\n"
17234 " size_t foo = (*(function))(\n"
17235 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17236 "BarrrrrrrrrrrrLong,\n"
17237 " FoooooooooLooooong);\n"
17238 "}",
17239 Spaces);
17240
17241 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
17242 verifyFormat("void foo( ) {\n"
17243 " size_t foo = (*(function))(\n"
17244 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17245 "BarrrrrrrrrrrrLong,\n"
17246 " FoooooooooLooooong\n"
17247 " );\n"
17248 "}",
17249 Spaces);
17250 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
17251 verifyFormat("size_t idx = (size_t)a;", Spaces);
17252 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
17253 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
17254 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
17255 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
17256 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
17257}
17258
17259TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
17260 verifyFormat("int a[5];");
17261 verifyFormat("a[3] += 42;");
17262
17263 FormatStyle Spaces = getLLVMStyle();
17264 Spaces.SpacesInSquareBrackets = true;
17265 // Not lambdas.
17266 verifyFormat("int a[ 5 ];", Spaces);
17267 verifyFormat("a[ 3 ] += 42;", Spaces);
17268 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
17269 verifyFormat("double &operator[](int i) { return 0; }\n"
17270 "int i;",
17271 Spaces);
17272 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
17273 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
17274 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
17275 // Lambdas.
17276 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
17277 verifyFormat("return [ i, args... ] {};", Spaces);
17278 verifyFormat("int foo = [ &bar ]() {};", Spaces);
17279 verifyFormat("int foo = [ = ]() {};", Spaces);
17280 verifyFormat("int foo = [ & ]() {};", Spaces);
17281 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
17282 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
17283}
17284
17285TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
17286 FormatStyle NoSpaceStyle = getLLVMStyle();
17287 verifyFormat("int a[5];", NoSpaceStyle);
17288 verifyFormat("a[3] += 42;", NoSpaceStyle);
17289
17290 verifyFormat("int a[1];", NoSpaceStyle);
17291 verifyFormat("int 1 [a];", NoSpaceStyle);
17292 verifyFormat("int a[1][2];", NoSpaceStyle);
17293 verifyFormat("a[7] = 5;", NoSpaceStyle);
17294 verifyFormat("int a = (f())[23];", NoSpaceStyle);
17295 verifyFormat("f([] {})", NoSpaceStyle);
17296
17297 FormatStyle Space = getLLVMStyle();
17298 Space.SpaceBeforeSquareBrackets = true;
17299 verifyFormat("int c = []() -> int { return 2; }();", Space);
17300 verifyFormat("return [i, args...] {};", Space);
17301
17302 verifyFormat("int a [5];", Space);
17303 verifyFormat("a [3] += 42;", Space);
17304 verifyFormat("constexpr char hello []{\"hello\"};", Space);
17305 verifyFormat("double &operator[](int i) { return 0; }\n"
17306 "int i;",
17307 Space);
17308 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
17309 verifyFormat("int i = a [a][a]->f();", Space);
17310 verifyFormat("int i = (*b) [a]->f();", Space);
17311
17312 verifyFormat("int a [1];", Space);
17313 verifyFormat("int 1 [a];", Space);
17314 verifyFormat("int a [1][2];", Space);
17315 verifyFormat("a [7] = 5;", Space);
17316 verifyFormat("int a = (f()) [23];", Space);
17317 verifyFormat("f([] {})", Space);
17318}
17319
17320TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
17321 verifyFormat("int a = 5;");
17322 verifyFormat("a += 42;");
17323 verifyFormat("a or_eq 8;");
17324
17325 FormatStyle Spaces = getLLVMStyle();
17326 Spaces.SpaceBeforeAssignmentOperators = false;
17327 verifyFormat("int a= 5;", Spaces);
17328 verifyFormat("a+= 42;", Spaces);
17329 verifyFormat("a or_eq 8;", Spaces);
17330}
17331
17332TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
17333 verifyFormat("class Foo : public Bar {};");
17334 verifyFormat("Foo::Foo() : foo(1) {}");
17335 verifyFormat("for (auto a : b) {\n}");
17336 verifyFormat("int x = a ? b : c;");
17337 verifyFormat("{\n"
17338 "label0:\n"
17339 " int x = 0;\n"
17340 "}");
17341 verifyFormat("switch (x) {\n"
17342 "case 1:\n"
17343 "default:\n"
17344 "}");
17345 verifyFormat("switch (allBraces) {\n"
17346 "case 1: {\n"
17347 " break;\n"
17348 "}\n"
17349 "case 2: {\n"
17350 " [[fallthrough]];\n"
17351 "}\n"
17352 "default: {\n"
17353 " break;\n"
17354 "}\n"
17355 "}");
17356
17357 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(ColumnLimit: 30);
17358 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
17359 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
17360 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
17361 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
17362 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
17363 verifyFormat("{\n"
17364 "label1:\n"
17365 " int x = 0;\n"
17366 "}",
17367 CtorInitializerStyle);
17368 verifyFormat("switch (x) {\n"
17369 "case 1:\n"
17370 "default:\n"
17371 "}",
17372 CtorInitializerStyle);
17373 verifyFormat("switch (allBraces) {\n"
17374 "case 1: {\n"
17375 " break;\n"
17376 "}\n"
17377 "case 2: {\n"
17378 " [[fallthrough]];\n"
17379 "}\n"
17380 "default: {\n"
17381 " break;\n"
17382 "}\n"
17383 "}",
17384 CtorInitializerStyle);
17385 CtorInitializerStyle.BreakConstructorInitializers =
17386 FormatStyle::BCIS_AfterColon;
17387 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17388 " aaaaaaaaaaaaaaaa(1),\n"
17389 " bbbbbbbbbbbbbbbb(2) {}",
17390 CtorInitializerStyle);
17391 CtorInitializerStyle.BreakConstructorInitializers =
17392 FormatStyle::BCIS_BeforeComma;
17393 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17394 " : aaaaaaaaaaaaaaaa(1)\n"
17395 " , bbbbbbbbbbbbbbbb(2) {}",
17396 CtorInitializerStyle);
17397 CtorInitializerStyle.BreakConstructorInitializers =
17398 FormatStyle::BCIS_BeforeColon;
17399 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17400 " : aaaaaaaaaaaaaaaa(1),\n"
17401 " bbbbbbbbbbbbbbbb(2) {}",
17402 CtorInitializerStyle);
17403 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
17404 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17405 ": aaaaaaaaaaaaaaaa(1),\n"
17406 " bbbbbbbbbbbbbbbb(2) {}",
17407 CtorInitializerStyle);
17408
17409 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(ColumnLimit: 30);
17410 InheritanceStyle.SpaceBeforeInheritanceColon = false;
17411 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
17412 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
17413 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
17414 verifyFormat("int x = a ? b : c;", InheritanceStyle);
17415 verifyFormat("{\n"
17416 "label2:\n"
17417 " int x = 0;\n"
17418 "}",
17419 InheritanceStyle);
17420 verifyFormat("switch (x) {\n"
17421 "case 1:\n"
17422 "default:\n"
17423 "}",
17424 InheritanceStyle);
17425 verifyFormat("switch (allBraces) {\n"
17426 "case 1: {\n"
17427 " break;\n"
17428 "}\n"
17429 "case 2: {\n"
17430 " [[fallthrough]];\n"
17431 "}\n"
17432 "default: {\n"
17433 " break;\n"
17434 "}\n"
17435 "}",
17436 InheritanceStyle);
17437 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
17438 verifyFormat("class Foooooooooooooooooooooo\n"
17439 " : public aaaaaaaaaaaaaaaaaa,\n"
17440 " public bbbbbbbbbbbbbbbbbb {\n"
17441 "}",
17442 InheritanceStyle);
17443 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
17444 verifyFormat("class Foooooooooooooooooooooo:\n"
17445 " public aaaaaaaaaaaaaaaaaa,\n"
17446 " public bbbbbbbbbbbbbbbbbb {\n"
17447 "}",
17448 InheritanceStyle);
17449 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
17450 verifyFormat("class Foooooooooooooooooooooo\n"
17451 " : public aaaaaaaaaaaaaaaaaa\n"
17452 " , public bbbbbbbbbbbbbbbbbb {\n"
17453 "}",
17454 InheritanceStyle);
17455 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
17456 verifyFormat("class Foooooooooooooooooooooo\n"
17457 " : public aaaaaaaaaaaaaaaaaa,\n"
17458 " public bbbbbbbbbbbbbbbbbb {\n"
17459 "}",
17460 InheritanceStyle);
17461 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
17462 verifyFormat("class Foooooooooooooooooooooo\n"
17463 ": public aaaaaaaaaaaaaaaaaa,\n"
17464 " public bbbbbbbbbbbbbbbbbb {}",
17465 InheritanceStyle);
17466
17467 FormatStyle ForLoopStyle = getLLVMStyle();
17468 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
17469 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
17470 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
17471 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
17472 verifyFormat("int x = a ? b : c;", ForLoopStyle);
17473 verifyFormat("{\n"
17474 "label2:\n"
17475 " int x = 0;\n"
17476 "}",
17477 ForLoopStyle);
17478 verifyFormat("switch (x) {\n"
17479 "case 1:\n"
17480 "default:\n"
17481 "}",
17482 ForLoopStyle);
17483 verifyFormat("switch (allBraces) {\n"
17484 "case 1: {\n"
17485 " break;\n"
17486 "}\n"
17487 "case 2: {\n"
17488 " [[fallthrough]];\n"
17489 "}\n"
17490 "default: {\n"
17491 " break;\n"
17492 "}\n"
17493 "}",
17494 ForLoopStyle);
17495
17496 FormatStyle CaseStyle = getLLVMStyle();
17497 CaseStyle.SpaceBeforeCaseColon = true;
17498 verifyFormat("class Foo : public Bar {};", CaseStyle);
17499 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
17500 verifyFormat("for (auto a : b) {\n}", CaseStyle);
17501 verifyFormat("int x = a ? b : c;", CaseStyle);
17502 verifyFormat("switch (x) {\n"
17503 "case 1 :\n"
17504 "default :\n"
17505 "}",
17506 CaseStyle);
17507 verifyFormat("switch (allBraces) {\n"
17508 "case 1 : {\n"
17509 " break;\n"
17510 "}\n"
17511 "case 2 : {\n"
17512 " [[fallthrough]];\n"
17513 "}\n"
17514 "default : {\n"
17515 " break;\n"
17516 "}\n"
17517 "}",
17518 CaseStyle);
17519 // Goto labels should not be affected.
17520 verifyFormat("switch (x) {\n"
17521 "goto_label:\n"
17522 "default :\n"
17523 "}",
17524 CaseStyle);
17525 verifyFormat("switch (x) {\n"
17526 "goto_label: { break; }\n"
17527 "default : {\n"
17528 " break;\n"
17529 "}\n"
17530 "}",
17531 CaseStyle);
17532
17533 FormatStyle NoSpaceStyle = getLLVMStyle();
17534 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
17535 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17536 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
17537 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17538 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
17539 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
17540 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
17541 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
17542 verifyFormat("{\n"
17543 "label3:\n"
17544 " int x = 0;\n"
17545 "}",
17546 NoSpaceStyle);
17547 verifyFormat("switch (x) {\n"
17548 "case 1:\n"
17549 "default:\n"
17550 "}",
17551 NoSpaceStyle);
17552 verifyFormat("switch (allBraces) {\n"
17553 "case 1: {\n"
17554 " break;\n"
17555 "}\n"
17556 "case 2: {\n"
17557 " [[fallthrough]];\n"
17558 "}\n"
17559 "default: {\n"
17560 " break;\n"
17561 "}\n"
17562 "}",
17563 NoSpaceStyle);
17564
17565 FormatStyle InvertedSpaceStyle = getLLVMStyle();
17566 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
17567 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
17568 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
17569 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
17570 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
17571 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
17572 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
17573 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
17574 verifyFormat("{\n"
17575 "label3:\n"
17576 " int x = 0;\n"
17577 "}",
17578 InvertedSpaceStyle);
17579 verifyFormat("switch (x) {\n"
17580 "case 1 :\n"
17581 "case 2 : {\n"
17582 " break;\n"
17583 "}\n"
17584 "default :\n"
17585 " break;\n"
17586 "}",
17587 InvertedSpaceStyle);
17588 verifyFormat("switch (allBraces) {\n"
17589 "case 1 : {\n"
17590 " break;\n"
17591 "}\n"
17592 "case 2 : {\n"
17593 " [[fallthrough]];\n"
17594 "}\n"
17595 "default : {\n"
17596 " break;\n"
17597 "}\n"
17598 "}",
17599 InvertedSpaceStyle);
17600}
17601
17602TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
17603 FormatStyle Style = getLLVMStyle();
17604
17605 Style.PointerAlignment = FormatStyle::PAS_Left;
17606 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17607 verifyFormat("void* const* x = NULL;", Style);
17608
17609#define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17610 do { \
17611 Style.PointerAlignment = FormatStyle::Pointers; \
17612 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17613 verifyFormat(Code, Style); \
17614 } while (false)
17615
17616 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
17617 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
17618 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
17619
17620 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
17621 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
17622 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
17623
17624 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
17625 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
17626 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
17627
17628 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
17629 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
17630 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
17631
17632 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
17633 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17634 SAPQ_Default);
17635 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17636 SAPQ_Default);
17637
17638 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
17639 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
17640 SAPQ_Before);
17641 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17642 SAPQ_Before);
17643
17644 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
17645 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
17646 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
17647 SAPQ_After);
17648
17649 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
17650 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
17651 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
17652
17653#undef verifyQualifierSpaces
17654
17655 FormatStyle Spaces = getLLVMStyle();
17656 Spaces.AttributeMacros.push_back(x: "qualified");
17657 Spaces.PointerAlignment = FormatStyle::PAS_Right;
17658 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
17659 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
17660 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17661 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
17662 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
17663 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17664 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17665 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
17666 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
17667 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17668 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17669 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17670
17671 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17672 Spaces.PointerAlignment = FormatStyle::PAS_Left;
17673 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
17674 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
17675 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
17676 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
17677 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
17678 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17679 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17680 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17681 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
17682 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
17683 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
17684 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
17685 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17686
17687 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
17688 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
17689 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
17690 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
17691 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
17692 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
17693 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
17694 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
17695}
17696
17697TEST_F(FormatTest, AlignConsecutiveMacros) {
17698 FormatStyle Style = getLLVMStyle();
17699 Style.AlignConsecutiveAssignments.Enabled = true;
17700 Style.AlignConsecutiveDeclarations.Enabled = true;
17701
17702 verifyFormat("#define a 3\n"
17703 "#define bbbb 4\n"
17704 "#define ccc (5)",
17705 Style);
17706
17707 verifyFormat("#define f(x) (x * x)\n"
17708 "#define fff(x, y, z) (x * y + z)\n"
17709 "#define ffff(x, y) (x - y)",
17710 Style);
17711
17712 verifyFormat("#define foo(x, y) (x + y)\n"
17713 "#define bar (5, 6)(2 + 2)",
17714 Style);
17715
17716 verifyFormat("#define a 3\n"
17717 "#define bbbb 4\n"
17718 "#define ccc (5)\n"
17719 "#define f(x) (x * x)\n"
17720 "#define fff(x, y, z) (x * y + z)\n"
17721 "#define ffff(x, y) (x - y)",
17722 Style);
17723
17724 Style.AlignConsecutiveMacros.Enabled = true;
17725 verifyFormat("#define a 3\n"
17726 "#define bbbb 4\n"
17727 "#define ccc (5)",
17728 Style);
17729
17730 verifyFormat("#define true 1\n"
17731 "#define false 0",
17732 Style);
17733
17734 verifyFormat("#define f(x) (x * x)\n"
17735 "#define fff(x, y, z) (x * y + z)\n"
17736 "#define ffff(x, y) (x - y)",
17737 Style);
17738
17739 verifyFormat("#define foo(x, y) (x + y)\n"
17740 "#define bar (5, 6)(2 + 2)",
17741 Style);
17742
17743 verifyFormat("#define a 3\n"
17744 "#define bbbb 4\n"
17745 "#define ccc (5)\n"
17746 "#define f(x) (x * x)\n"
17747 "#define fff(x, y, z) (x * y + z)\n"
17748 "#define ffff(x, y) (x - y)",
17749 Style);
17750
17751 verifyFormat("#define a 5\n"
17752 "#define foo(x, y) (x + y)\n"
17753 "#define CCC (6)\n"
17754 "auto lambda = []() {\n"
17755 " auto ii = 0;\n"
17756 " float j = 0;\n"
17757 " return 0;\n"
17758 "};\n"
17759 "int i = 0;\n"
17760 "float i2 = 0;\n"
17761 "auto v = type{\n"
17762 " i = 1, //\n"
17763 " (i = 2), //\n"
17764 " i = 3 //\n"
17765 "};",
17766 Style);
17767
17768 Style.AlignConsecutiveMacros.Enabled = false;
17769 Style.ColumnLimit = 20;
17770
17771 verifyFormat("#define a \\\n"
17772 " \"aabbbbbbbbbbbb\"\n"
17773 "#define D \\\n"
17774 " \"aabbbbbbbbbbbb\" \\\n"
17775 " \"ccddeeeeeeeee\"\n"
17776 "#define B \\\n"
17777 " \"QQQQQQQQQQQQQ\" \\\n"
17778 " \"FFFFFFFFFFFFF\" \\\n"
17779 " \"LLLLLLLL\"",
17780 Style);
17781
17782 Style.AlignConsecutiveMacros.Enabled = true;
17783 verifyFormat("#define a \\\n"
17784 " \"aabbbbbbbbbbbb\"\n"
17785 "#define D \\\n"
17786 " \"aabbbbbbbbbbbb\" \\\n"
17787 " \"ccddeeeeeeeee\"\n"
17788 "#define B \\\n"
17789 " \"QQQQQQQQQQQQQ\" \\\n"
17790 " \"FFFFFFFFFFFFF\" \\\n"
17791 " \"LLLLLLLL\"",
17792 Style);
17793
17794 // Test across comments
17795 Style.MaxEmptyLinesToKeep = 10;
17796 Style.ReflowComments = false;
17797 Style.AlignConsecutiveMacros.AcrossComments = true;
17798 verifyFormat("#define a 3\n"
17799 "// line comment\n"
17800 "#define bbbb 4\n"
17801 "#define ccc (5)",
17802 "#define a 3\n"
17803 "// line comment\n"
17804 "#define bbbb 4\n"
17805 "#define ccc (5)",
17806 Style);
17807
17808 verifyFormat("#define a 3\n"
17809 "/* block comment */\n"
17810 "#define bbbb 4\n"
17811 "#define ccc (5)",
17812 "#define a 3\n"
17813 "/* block comment */\n"
17814 "#define bbbb 4\n"
17815 "#define ccc (5)",
17816 Style);
17817
17818 verifyFormat("#define a 3\n"
17819 "/* multi-line *\n"
17820 " * block comment */\n"
17821 "#define bbbb 4\n"
17822 "#define ccc (5)",
17823 "#define a 3\n"
17824 "/* multi-line *\n"
17825 " * block comment */\n"
17826 "#define bbbb 4\n"
17827 "#define ccc (5)",
17828 Style);
17829
17830 verifyFormat("#define a 3\n"
17831 "// multi-line line comment\n"
17832 "//\n"
17833 "#define bbbb 4\n"
17834 "#define ccc (5)",
17835 "#define a 3\n"
17836 "// multi-line line comment\n"
17837 "//\n"
17838 "#define bbbb 4\n"
17839 "#define ccc (5)",
17840 Style);
17841
17842 verifyFormat("#define a 3\n"
17843 "// empty lines still break.\n"
17844 "\n"
17845 "#define bbbb 4\n"
17846 "#define ccc (5)",
17847 "#define a 3\n"
17848 "// empty lines still break.\n"
17849 "\n"
17850 "#define bbbb 4\n"
17851 "#define ccc (5)",
17852 Style);
17853
17854 // Test across empty lines
17855 Style.AlignConsecutiveMacros.AcrossComments = false;
17856 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
17857 verifyFormat("#define a 3\n"
17858 "\n"
17859 "#define bbbb 4\n"
17860 "#define ccc (5)",
17861 "#define a 3\n"
17862 "\n"
17863 "#define bbbb 4\n"
17864 "#define ccc (5)",
17865 Style);
17866
17867 verifyFormat("#define a 3\n"
17868 "\n"
17869 "\n"
17870 "\n"
17871 "#define bbbb 4\n"
17872 "#define ccc (5)",
17873 "#define a 3\n"
17874 "\n"
17875 "\n"
17876 "\n"
17877 "#define bbbb 4\n"
17878 "#define ccc (5)",
17879 Style);
17880
17881 verifyFormat("#define a 3\n"
17882 "// comments should break alignment\n"
17883 "//\n"
17884 "#define bbbb 4\n"
17885 "#define ccc (5)",
17886 "#define a 3\n"
17887 "// comments should break alignment\n"
17888 "//\n"
17889 "#define bbbb 4\n"
17890 "#define ccc (5)",
17891 Style);
17892
17893 // Test across empty lines and comments
17894 Style.AlignConsecutiveMacros.AcrossComments = true;
17895 verifyFormat("#define a 3\n"
17896 "\n"
17897 "// line comment\n"
17898 "#define bbbb 4\n"
17899 "#define ccc (5)",
17900 Style);
17901
17902 verifyFormat("#define a 3\n"
17903 "\n"
17904 "\n"
17905 "/* multi-line *\n"
17906 " * block comment */\n"
17907 "\n"
17908 "\n"
17909 "#define bbbb 4\n"
17910 "#define ccc (5)",
17911 "#define a 3\n"
17912 "\n"
17913 "\n"
17914 "/* multi-line *\n"
17915 " * block comment */\n"
17916 "\n"
17917 "\n"
17918 "#define bbbb 4\n"
17919 "#define ccc (5)",
17920 Style);
17921
17922 verifyFormat("#define a 3\n"
17923 "\n"
17924 "\n"
17925 "/* multi-line *\n"
17926 " * block comment */\n"
17927 "\n"
17928 "\n"
17929 "#define bbbb 4\n"
17930 "#define ccc (5)",
17931 "#define a 3\n"
17932 "\n"
17933 "\n"
17934 "/* multi-line *\n"
17935 " * block comment */\n"
17936 "\n"
17937 "\n"
17938 "#define bbbb 4\n"
17939 "#define ccc (5)",
17940 Style);
17941}
17942
17943TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
17944 FormatStyle Alignment = getLLVMStyle();
17945 Alignment.AlignConsecutiveMacros.Enabled = true;
17946 Alignment.AlignConsecutiveAssignments.Enabled = true;
17947 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
17948
17949 Alignment.MaxEmptyLinesToKeep = 10;
17950 /* Test alignment across empty lines */
17951 verifyFormat("int a = 5;\n"
17952 "\n"
17953 "int oneTwoThree = 123;",
17954 "int a = 5;\n"
17955 "\n"
17956 "int oneTwoThree= 123;",
17957 Alignment);
17958 verifyFormat("int a = 5;\n"
17959 "int one = 1;\n"
17960 "\n"
17961 "int oneTwoThree = 123;",
17962 "int a = 5;\n"
17963 "int one = 1;\n"
17964 "\n"
17965 "int oneTwoThree = 123;",
17966 Alignment);
17967 verifyFormat("int a = 5;\n"
17968 "int one = 1;\n"
17969 "\n"
17970 "int oneTwoThree = 123;\n"
17971 "int oneTwo = 12;",
17972 "int a = 5;\n"
17973 "int one = 1;\n"
17974 "\n"
17975 "int oneTwoThree = 123;\n"
17976 "int oneTwo = 12;",
17977 Alignment);
17978
17979 /* Test across comments */
17980 verifyFormat("int a = 5;\n"
17981 "/* block comment */\n"
17982 "int oneTwoThree = 123;",
17983 "int a = 5;\n"
17984 "/* block comment */\n"
17985 "int oneTwoThree=123;",
17986 Alignment);
17987
17988 verifyFormat("int a = 5;\n"
17989 "// line comment\n"
17990 "int oneTwoThree = 123;",
17991 "int a = 5;\n"
17992 "// line comment\n"
17993 "int oneTwoThree=123;",
17994 Alignment);
17995
17996 /* Test across comments and newlines */
17997 verifyFormat("int a = 5;\n"
17998 "\n"
17999 "/* block comment */\n"
18000 "int oneTwoThree = 123;",
18001 "int a = 5;\n"
18002 "\n"
18003 "/* block comment */\n"
18004 "int oneTwoThree=123;",
18005 Alignment);
18006
18007 verifyFormat("int a = 5;\n"
18008 "\n"
18009 "// line comment\n"
18010 "int oneTwoThree = 123;",
18011 "int a = 5;\n"
18012 "\n"
18013 "// line comment\n"
18014 "int oneTwoThree=123;",
18015 Alignment);
18016}
18017
18018TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18019 FormatStyle Alignment = getLLVMStyle();
18020 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18021 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18022 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18023
18024 Alignment.MaxEmptyLinesToKeep = 10;
18025 /* Test alignment across empty lines */
18026 verifyFormat("int a = 5;\n"
18027 "\n"
18028 "float const oneTwoThree = 123;",
18029 "int a = 5;\n"
18030 "\n"
18031 "float const oneTwoThree = 123;",
18032 Alignment);
18033 verifyFormat("int a = 5;\n"
18034 "float const one = 1;\n"
18035 "\n"
18036 "int oneTwoThree = 123;",
18037 "int a = 5;\n"
18038 "float const one = 1;\n"
18039 "\n"
18040 "int oneTwoThree = 123;",
18041 Alignment);
18042
18043 /* Test across comments */
18044 verifyFormat("float const a = 5;\n"
18045 "/* block comment */\n"
18046 "int oneTwoThree = 123;",
18047 "float const a = 5;\n"
18048 "/* block comment */\n"
18049 "int oneTwoThree=123;",
18050 Alignment);
18051
18052 verifyFormat("float const a = 5;\n"
18053 "// line comment\n"
18054 "int oneTwoThree = 123;",
18055 "float const a = 5;\n"
18056 "// line comment\n"
18057 "int oneTwoThree=123;",
18058 Alignment);
18059
18060 /* Test across comments and newlines */
18061 verifyFormat("float const a = 5;\n"
18062 "\n"
18063 "/* block comment */\n"
18064 "int oneTwoThree = 123;",
18065 "float const a = 5;\n"
18066 "\n"
18067 "/* block comment */\n"
18068 "int oneTwoThree=123;",
18069 Alignment);
18070
18071 verifyFormat("float const a = 5;\n"
18072 "\n"
18073 "// line comment\n"
18074 "int oneTwoThree = 123;",
18075 "float const a = 5;\n"
18076 "\n"
18077 "// line comment\n"
18078 "int oneTwoThree=123;",
18079 Alignment);
18080}
18081
18082TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18083 FormatStyle Alignment = getLLVMStyle();
18084 Alignment.AlignConsecutiveBitFields.Enabled = true;
18085 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18086 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18087
18088 Alignment.MaxEmptyLinesToKeep = 10;
18089 /* Test alignment across empty lines */
18090 verifyFormat("int a : 5;\n"
18091 "\n"
18092 "int longbitfield : 6;",
18093 "int a : 5;\n"
18094 "\n"
18095 "int longbitfield : 6;",
18096 Alignment);
18097 verifyFormat("int a : 5;\n"
18098 "int one : 1;\n"
18099 "\n"
18100 "int longbitfield : 6;",
18101 "int a : 5;\n"
18102 "int one : 1;\n"
18103 "\n"
18104 "int longbitfield : 6;",
18105 Alignment);
18106
18107 /* Test across comments */
18108 verifyFormat("int a : 5;\n"
18109 "/* block comment */\n"
18110 "int longbitfield : 6;",
18111 "int a : 5;\n"
18112 "/* block comment */\n"
18113 "int longbitfield : 6;",
18114 Alignment);
18115 verifyFormat("int a : 5;\n"
18116 "int one : 1;\n"
18117 "// line comment\n"
18118 "int longbitfield : 6;",
18119 "int a : 5;\n"
18120 "int one : 1;\n"
18121 "// line comment\n"
18122 "int longbitfield : 6;",
18123 Alignment);
18124
18125 /* Test across comments and newlines */
18126 verifyFormat("int a : 5;\n"
18127 "/* block comment */\n"
18128 "\n"
18129 "int longbitfield : 6;",
18130 "int a : 5;\n"
18131 "/* block comment */\n"
18132 "\n"
18133 "int longbitfield : 6;",
18134 Alignment);
18135 verifyFormat("int a : 5;\n"
18136 "int one : 1;\n"
18137 "\n"
18138 "// line comment\n"
18139 "\n"
18140 "int longbitfield : 6;",
18141 "int a : 5;\n"
18142 "int one : 1;\n"
18143 "\n"
18144 "// line comment \n"
18145 "\n"
18146 "int longbitfield : 6;",
18147 Alignment);
18148}
18149
18150TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
18151 FormatStyle Alignment = getLLVMStyle();
18152 Alignment.AlignConsecutiveMacros.Enabled = true;
18153 Alignment.AlignConsecutiveAssignments.Enabled = true;
18154 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18155
18156 Alignment.MaxEmptyLinesToKeep = 10;
18157 /* Test alignment across empty lines */
18158 verifyFormat("int a = 5;\n"
18159 "\n"
18160 "int oneTwoThree = 123;",
18161 "int a = 5;\n"
18162 "\n"
18163 "int oneTwoThree= 123;",
18164 Alignment);
18165 verifyFormat("int a = 5;\n"
18166 "int one = 1;\n"
18167 "\n"
18168 "int oneTwoThree = 123;",
18169 "int a = 5;\n"
18170 "int one = 1;\n"
18171 "\n"
18172 "int oneTwoThree = 123;",
18173 Alignment);
18174
18175 /* Test across comments */
18176 verifyFormat("int a = 5;\n"
18177 "/* block comment */\n"
18178 "int oneTwoThree = 123;",
18179 "int a = 5;\n"
18180 "/* block comment */\n"
18181 "int oneTwoThree=123;",
18182 Alignment);
18183
18184 verifyFormat("int a = 5;\n"
18185 "// line comment\n"
18186 "int oneTwoThree = 123;",
18187 "int a = 5;\n"
18188 "// line comment\n"
18189 "int oneTwoThree=123;",
18190 Alignment);
18191
18192 verifyFormat("int a = 5;\n"
18193 "/*\n"
18194 " * multi-line block comment\n"
18195 " */\n"
18196 "int oneTwoThree = 123;",
18197 "int a = 5;\n"
18198 "/*\n"
18199 " * multi-line block comment\n"
18200 " */\n"
18201 "int oneTwoThree=123;",
18202 Alignment);
18203
18204 verifyFormat("int a = 5;\n"
18205 "//\n"
18206 "// multi-line line comment\n"
18207 "//\n"
18208 "int oneTwoThree = 123;",
18209 "int a = 5;\n"
18210 "//\n"
18211 "// multi-line line comment\n"
18212 "//\n"
18213 "int oneTwoThree=123;",
18214 Alignment);
18215
18216 /* Test across comments and newlines */
18217 verifyFormat("int a = 5;\n"
18218 "\n"
18219 "/* block comment */\n"
18220 "int oneTwoThree = 123;",
18221 "int a = 5;\n"
18222 "\n"
18223 "/* block comment */\n"
18224 "int oneTwoThree=123;",
18225 Alignment);
18226
18227 verifyFormat("int a = 5;\n"
18228 "\n"
18229 "// line comment\n"
18230 "int oneTwoThree = 123;",
18231 "int a = 5;\n"
18232 "\n"
18233 "// line comment\n"
18234 "int oneTwoThree=123;",
18235 Alignment);
18236}
18237
18238TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
18239 FormatStyle Alignment = getLLVMStyle();
18240 Alignment.AlignConsecutiveMacros.Enabled = true;
18241 Alignment.AlignConsecutiveAssignments.Enabled = true;
18242 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18243 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18244 verifyFormat("int a = 5;\n"
18245 "int oneTwoThree = 123;",
18246 Alignment);
18247 verifyFormat("int a = method();\n"
18248 "int oneTwoThree = 133;",
18249 Alignment);
18250 verifyFormat("a &= 5;\n"
18251 "bcd *= 5;\n"
18252 "ghtyf += 5;\n"
18253 "dvfvdb -= 5;\n"
18254 "a /= 5;\n"
18255 "vdsvsv %= 5;\n"
18256 "sfdbddfbdfbb ^= 5;\n"
18257 "dvsdsv |= 5;\n"
18258 "int dsvvdvsdvvv = 123;",
18259 Alignment);
18260 verifyFormat("int i = 1, j = 10;\n"
18261 "something = 2000;",
18262 Alignment);
18263 verifyFormat("something = 2000;\n"
18264 "int i = 1, j = 10;",
18265 Alignment);
18266 verifyFormat("something = 2000;\n"
18267 "another = 911;\n"
18268 "int i = 1, j = 10;\n"
18269 "oneMore = 1;\n"
18270 "i = 2;",
18271 Alignment);
18272 verifyFormat("int a = 5;\n"
18273 "int one = 1;\n"
18274 "method();\n"
18275 "int oneTwoThree = 123;\n"
18276 "int oneTwo = 12;",
18277 Alignment);
18278 verifyFormat("int oneTwoThree = 123;\n"
18279 "int oneTwo = 12;\n"
18280 "method();",
18281 Alignment);
18282 verifyFormat("int oneTwoThree = 123; // comment\n"
18283 "int oneTwo = 12; // comment",
18284 Alignment);
18285
18286 // Bug 25167
18287 /* Uncomment when fixed
18288 verifyFormat("#if A\n"
18289 "#else\n"
18290 "int aaaaaaaa = 12;\n"
18291 "#endif\n"
18292 "#if B\n"
18293 "#else\n"
18294 "int a = 12;\n"
18295 "#endif",
18296 Alignment);
18297 verifyFormat("enum foo {\n"
18298 "#if A\n"
18299 "#else\n"
18300 " aaaaaaaa = 12;\n"
18301 "#endif\n"
18302 "#if B\n"
18303 "#else\n"
18304 " a = 12;\n"
18305 "#endif\n"
18306 "};",
18307 Alignment);
18308 */
18309
18310 Alignment.MaxEmptyLinesToKeep = 10;
18311 /* Test alignment across empty lines */
18312 verifyFormat("int a = 5;\n"
18313 "\n"
18314 "int oneTwoThree = 123;",
18315 "int a = 5;\n"
18316 "\n"
18317 "int oneTwoThree= 123;",
18318 Alignment);
18319 verifyFormat("int a = 5;\n"
18320 "int one = 1;\n"
18321 "\n"
18322 "int oneTwoThree = 123;",
18323 "int a = 5;\n"
18324 "int one = 1;\n"
18325 "\n"
18326 "int oneTwoThree = 123;",
18327 Alignment);
18328 verifyFormat("int a = 5;\n"
18329 "int one = 1;\n"
18330 "\n"
18331 "int oneTwoThree = 123;\n"
18332 "int oneTwo = 12;",
18333 "int a = 5;\n"
18334 "int one = 1;\n"
18335 "\n"
18336 "int oneTwoThree = 123;\n"
18337 "int oneTwo = 12;",
18338 Alignment);
18339
18340 /* Test across comments */
18341 verifyFormat("int a = 5;\n"
18342 "/* block comment */\n"
18343 "int oneTwoThree = 123;",
18344 "int a = 5;\n"
18345 "/* block comment */\n"
18346 "int oneTwoThree=123;",
18347 Alignment);
18348
18349 verifyFormat("int a = 5;\n"
18350 "// line comment\n"
18351 "int oneTwoThree = 123;",
18352 "int a = 5;\n"
18353 "// line comment\n"
18354 "int oneTwoThree=123;",
18355 Alignment);
18356
18357 /* Test across comments and newlines */
18358 verifyFormat("int a = 5;\n"
18359 "\n"
18360 "/* block comment */\n"
18361 "int oneTwoThree = 123;",
18362 "int a = 5;\n"
18363 "\n"
18364 "/* block comment */\n"
18365 "int oneTwoThree=123;",
18366 Alignment);
18367
18368 verifyFormat("int a = 5;\n"
18369 "\n"
18370 "// line comment\n"
18371 "int oneTwoThree = 123;",
18372 "int a = 5;\n"
18373 "\n"
18374 "// line comment\n"
18375 "int oneTwoThree=123;",
18376 Alignment);
18377
18378 verifyFormat("int a = 5;\n"
18379 "//\n"
18380 "// multi-line line comment\n"
18381 "//\n"
18382 "int oneTwoThree = 123;",
18383 "int a = 5;\n"
18384 "//\n"
18385 "// multi-line line comment\n"
18386 "//\n"
18387 "int oneTwoThree=123;",
18388 Alignment);
18389
18390 verifyFormat("int a = 5;\n"
18391 "/*\n"
18392 " * multi-line block comment\n"
18393 " */\n"
18394 "int oneTwoThree = 123;",
18395 "int a = 5;\n"
18396 "/*\n"
18397 " * multi-line block comment\n"
18398 " */\n"
18399 "int oneTwoThree=123;",
18400 Alignment);
18401
18402 verifyFormat("int a = 5;\n"
18403 "\n"
18404 "/* block comment */\n"
18405 "\n"
18406 "\n"
18407 "\n"
18408 "int oneTwoThree = 123;",
18409 "int a = 5;\n"
18410 "\n"
18411 "/* block comment */\n"
18412 "\n"
18413 "\n"
18414 "\n"
18415 "int oneTwoThree=123;",
18416 Alignment);
18417
18418 verifyFormat("int a = 5;\n"
18419 "\n"
18420 "// line comment\n"
18421 "\n"
18422 "\n"
18423 "\n"
18424 "int oneTwoThree = 123;",
18425 "int a = 5;\n"
18426 "\n"
18427 "// line comment\n"
18428 "\n"
18429 "\n"
18430 "\n"
18431 "int oneTwoThree=123;",
18432 Alignment);
18433
18434 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18435 verifyFormat("#define A \\\n"
18436 " int aaaa = 12; \\\n"
18437 " int b = 23; \\\n"
18438 " int ccc = 234; \\\n"
18439 " int dddddddddd = 2345;",
18440 Alignment);
18441 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18442 verifyFormat("#define A \\\n"
18443 " int aaaa = 12; \\\n"
18444 " int b = 23; \\\n"
18445 " int ccc = 234; \\\n"
18446 " int dddddddddd = 2345;",
18447 Alignment);
18448 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18449 verifyFormat("#define A "
18450 " \\\n"
18451 " int aaaa = 12; "
18452 " \\\n"
18453 " int b = 23; "
18454 " \\\n"
18455 " int ccc = 234; "
18456 " \\\n"
18457 " int dddddddddd = 2345;",
18458 Alignment);
18459 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18460 "k = 4, int l = 5,\n"
18461 " int m = 6) {\n"
18462 " int j = 10;\n"
18463 " otherThing = 1;\n"
18464 "}",
18465 Alignment);
18466 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18467 " int i = 1;\n"
18468 " int j = 2;\n"
18469 " int big = 10000;\n"
18470 "}",
18471 Alignment);
18472 verifyFormat("class C {\n"
18473 "public:\n"
18474 " int i = 1;\n"
18475 " virtual void f() = 0;\n"
18476 "};",
18477 Alignment);
18478 verifyFormat("int i = 1;\n"
18479 "if (SomeType t = getSomething()) {\n"
18480 "}\n"
18481 "int j = 2;\n"
18482 "int big = 10000;",
18483 Alignment);
18484 verifyFormat("int j = 7;\n"
18485 "for (int k = 0; k < N; ++k) {\n"
18486 "}\n"
18487 "int j = 2;\n"
18488 "int big = 10000;\n"
18489 "}",
18490 Alignment);
18491 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18492 verifyFormat("int i = 1;\n"
18493 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18494 " = someLooooooooooooooooongFunction();\n"
18495 "int j = 2;",
18496 Alignment);
18497 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18498 verifyFormat("int i = 1;\n"
18499 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18500 " someLooooooooooooooooongFunction();\n"
18501 "int j = 2;",
18502 Alignment);
18503
18504 verifyFormat("auto lambda = []() {\n"
18505 " auto i = 0;\n"
18506 " return 0;\n"
18507 "};\n"
18508 "int i = 0;\n"
18509 "auto v = type{\n"
18510 " i = 1, //\n"
18511 " (i = 2), //\n"
18512 " i = 3 //\n"
18513 "};",
18514 Alignment);
18515
18516 verifyFormat(
18517 "int i = 1;\n"
18518 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18519 " loooooooooooooooooooooongParameterB);\n"
18520 "int j = 2;",
18521 Alignment);
18522
18523 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18524 " typename B = very_long_type_name_1,\n"
18525 " typename T_2 = very_long_type_name_2>\n"
18526 "auto foo() {}",
18527 Alignment);
18528 verifyFormat("int a, b = 1;\n"
18529 "int c = 2;\n"
18530 "int dd = 3;",
18531 Alignment);
18532 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18533 "float b[1][] = {{3.f}};",
18534 Alignment);
18535 verifyFormat("for (int i = 0; i < 1; i++)\n"
18536 " int x = 1;",
18537 Alignment);
18538 verifyFormat("for (i = 0; i < 1; i++)\n"
18539 " x = 1;\n"
18540 "y = 1;",
18541 Alignment);
18542
18543 Alignment.ReflowComments = true;
18544 Alignment.ColumnLimit = 50;
18545 verifyFormat("int x = 0;\n"
18546 "int yy = 1; /// specificlennospace\n"
18547 "int zzz = 2;",
18548 "int x = 0;\n"
18549 "int yy = 1; ///specificlennospace\n"
18550 "int zzz = 2;",
18551 Alignment);
18552}
18553
18554TEST_F(FormatTest, AlignCompoundAssignments) {
18555 FormatStyle Alignment = getLLVMStyle();
18556 Alignment.AlignConsecutiveAssignments.Enabled = true;
18557 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
18558 Alignment.AlignConsecutiveAssignments.PadOperators = false;
18559 verifyFormat("sfdbddfbdfbb = 5;\n"
18560 "dvsdsv = 5;\n"
18561 "int dsvvdvsdvvv = 123;",
18562 Alignment);
18563 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18564 "dvsdsv |= 5;\n"
18565 "int dsvvdvsdvvv = 123;",
18566 Alignment);
18567 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18568 "dvsdsv <<= 5;\n"
18569 "int dsvvdvsdvvv = 123;",
18570 Alignment);
18571 verifyFormat("int xxx = 5;\n"
18572 "xxx = 5;\n"
18573 "{\n"
18574 " int yyy = 6;\n"
18575 " yyy = 6;\n"
18576 "}",
18577 Alignment);
18578 verifyFormat("int xxx = 5;\n"
18579 "xxx += 5;\n"
18580 "{\n"
18581 " int yyy = 6;\n"
18582 " yyy += 6;\n"
18583 "}",
18584 Alignment);
18585 // Test that `<=` is not treated as a compound assignment.
18586 verifyFormat("aa &= 5;\n"
18587 "b <= 10;\n"
18588 "c = 15;",
18589 Alignment);
18590 Alignment.AlignConsecutiveAssignments.PadOperators = true;
18591 verifyFormat("sfdbddfbdfbb = 5;\n"
18592 "dvsdsv = 5;\n"
18593 "int dsvvdvsdvvv = 123;",
18594 Alignment);
18595 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18596 "dvsdsv |= 5;\n"
18597 "int dsvvdvsdvvv = 123;",
18598 Alignment);
18599 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18600 "dvsdsv <<= 5;\n"
18601 "int dsvvdvsdvvv = 123;",
18602 Alignment);
18603 verifyFormat("a += 5;\n"
18604 "one = 1;\n"
18605 "\n"
18606 "oneTwoThree = 123;",
18607 "a += 5;\n"
18608 "one = 1;\n"
18609 "\n"
18610 "oneTwoThree = 123;",
18611 Alignment);
18612 verifyFormat("a += 5;\n"
18613 "one = 1;\n"
18614 "//\n"
18615 "oneTwoThree = 123;",
18616 "a += 5;\n"
18617 "one = 1;\n"
18618 "//\n"
18619 "oneTwoThree = 123;",
18620 Alignment);
18621 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18622 verifyFormat("a += 5;\n"
18623 "one = 1;\n"
18624 "\n"
18625 "oneTwoThree = 123;",
18626 "a += 5;\n"
18627 "one = 1;\n"
18628 "\n"
18629 "oneTwoThree = 123;",
18630 Alignment);
18631 verifyFormat("a += 5;\n"
18632 "one = 1;\n"
18633 "//\n"
18634 "oneTwoThree = 123;",
18635 "a += 5;\n"
18636 "one = 1;\n"
18637 "//\n"
18638 "oneTwoThree = 123;",
18639 Alignment);
18640 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
18641 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
18642 verifyFormat("a += 5;\n"
18643 "one = 1;\n"
18644 "\n"
18645 "oneTwoThree = 123;",
18646 "a += 5;\n"
18647 "one = 1;\n"
18648 "\n"
18649 "oneTwoThree = 123;",
18650 Alignment);
18651 verifyFormat("a += 5;\n"
18652 "one = 1;\n"
18653 "//\n"
18654 "oneTwoThree = 123;",
18655 "a += 5;\n"
18656 "one = 1;\n"
18657 "//\n"
18658 "oneTwoThree = 123;",
18659 Alignment);
18660 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18661 verifyFormat("a += 5;\n"
18662 "one >>= 1;\n"
18663 "\n"
18664 "oneTwoThree = 123;",
18665 "a += 5;\n"
18666 "one >>= 1;\n"
18667 "\n"
18668 "oneTwoThree = 123;",
18669 Alignment);
18670 verifyFormat("a += 5;\n"
18671 "one = 1;\n"
18672 "//\n"
18673 "oneTwoThree <<= 123;",
18674 "a += 5;\n"
18675 "one = 1;\n"
18676 "//\n"
18677 "oneTwoThree <<= 123;",
18678 Alignment);
18679}
18680
18681TEST_F(FormatTest, AlignConsecutiveAssignments) {
18682 FormatStyle Alignment = getLLVMStyle();
18683 Alignment.AlignConsecutiveMacros.Enabled = true;
18684 verifyFormat("int a = 5;\n"
18685 "int oneTwoThree = 123;",
18686 Alignment);
18687 verifyFormat("int a = 5;\n"
18688 "int oneTwoThree = 123;",
18689 Alignment);
18690
18691 Alignment.AlignConsecutiveAssignments.Enabled = true;
18692 verifyFormat("int a = 5;\n"
18693 "int oneTwoThree = 123;",
18694 Alignment);
18695 verifyFormat("int a = method();\n"
18696 "int oneTwoThree = 133;",
18697 Alignment);
18698 verifyFormat("aa <= 5;\n"
18699 "a &= 5;\n"
18700 "bcd *= 5;\n"
18701 "ghtyf += 5;\n"
18702 "dvfvdb -= 5;\n"
18703 "a /= 5;\n"
18704 "vdsvsv %= 5;\n"
18705 "sfdbddfbdfbb ^= 5;\n"
18706 "dvsdsv |= 5;\n"
18707 "int dsvvdvsdvvv = 123;",
18708 Alignment);
18709 verifyFormat("int i = 1, j = 10;\n"
18710 "something = 2000;",
18711 Alignment);
18712 verifyFormat("something = 2000;\n"
18713 "int i = 1, j = 10;",
18714 Alignment);
18715 verifyFormat("something = 2000;\n"
18716 "another = 911;\n"
18717 "int i = 1, j = 10;\n"
18718 "oneMore = 1;\n"
18719 "i = 2;",
18720 Alignment);
18721 verifyFormat("int a = 5;\n"
18722 "int one = 1;\n"
18723 "method();\n"
18724 "int oneTwoThree = 123;\n"
18725 "int oneTwo = 12;",
18726 Alignment);
18727 verifyFormat("int oneTwoThree = 123;\n"
18728 "int oneTwo = 12;\n"
18729 "method();",
18730 Alignment);
18731 verifyFormat("int oneTwoThree = 123; // comment\n"
18732 "int oneTwo = 12; // comment",
18733 Alignment);
18734 verifyFormat("int f() = default;\n"
18735 "int &operator() = default;\n"
18736 "int &operator=() {",
18737 Alignment);
18738 verifyFormat("int f() = delete;\n"
18739 "int &operator() = delete;\n"
18740 "int &operator=() {",
18741 Alignment);
18742 verifyFormat("int f() = default; // comment\n"
18743 "int &operator() = default; // comment\n"
18744 "int &operator=() {",
18745 Alignment);
18746 verifyFormat("int f() = default;\n"
18747 "int &operator() = default;\n"
18748 "int &operator==() {",
18749 Alignment);
18750 verifyFormat("int f() = default;\n"
18751 "int &operator() = default;\n"
18752 "int &operator<=() {",
18753 Alignment);
18754 verifyFormat("int f() = default;\n"
18755 "int &operator() = default;\n"
18756 "int &operator!=() {",
18757 Alignment);
18758 verifyFormat("int f() = default;\n"
18759 "int &operator() = default;\n"
18760 "int &operator=();",
18761 Alignment);
18762 verifyFormat("int f() = delete;\n"
18763 "int &operator() = delete;\n"
18764 "int &operator=();",
18765 Alignment);
18766 verifyFormat("/* long long padding */ int f() = default;\n"
18767 "int &operator() = default;\n"
18768 "int &operator/**/ =();",
18769 Alignment);
18770 // https://llvm.org/PR33697
18771 FormatStyle AlignmentWithPenalty = getLLVMStyle();
18772 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
18773 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
18774 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
18775 " void f() = delete;\n"
18776 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
18777 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
18778 "};",
18779 AlignmentWithPenalty);
18780
18781 // Bug 25167
18782 /* Uncomment when fixed
18783 verifyFormat("#if A\n"
18784 "#else\n"
18785 "int aaaaaaaa = 12;\n"
18786 "#endif\n"
18787 "#if B\n"
18788 "#else\n"
18789 "int a = 12;\n"
18790 "#endif",
18791 Alignment);
18792 verifyFormat("enum foo {\n"
18793 "#if A\n"
18794 "#else\n"
18795 " aaaaaaaa = 12;\n"
18796 "#endif\n"
18797 "#if B\n"
18798 "#else\n"
18799 " a = 12;\n"
18800 "#endif\n"
18801 "};",
18802 Alignment);
18803 */
18804
18805 verifyFormat("int a = 5;\n"
18806 "\n"
18807 "int oneTwoThree = 123;",
18808 "int a = 5;\n"
18809 "\n"
18810 "int oneTwoThree= 123;",
18811 Alignment);
18812 verifyFormat("int a = 5;\n"
18813 "int one = 1;\n"
18814 "\n"
18815 "int oneTwoThree = 123;",
18816 "int a = 5;\n"
18817 "int one = 1;\n"
18818 "\n"
18819 "int oneTwoThree = 123;",
18820 Alignment);
18821 verifyFormat("int a = 5;\n"
18822 "int one = 1;\n"
18823 "\n"
18824 "int oneTwoThree = 123;\n"
18825 "int oneTwo = 12;",
18826 "int a = 5;\n"
18827 "int one = 1;\n"
18828 "\n"
18829 "int oneTwoThree = 123;\n"
18830 "int oneTwo = 12;",
18831 Alignment);
18832 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
18833 verifyFormat("#define A \\\n"
18834 " int aaaa = 12; \\\n"
18835 " int b = 23; \\\n"
18836 " int ccc = 234; \\\n"
18837 " int dddddddddd = 2345;",
18838 Alignment);
18839 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
18840 verifyFormat("#define A \\\n"
18841 " int aaaa = 12; \\\n"
18842 " int b = 23; \\\n"
18843 " int ccc = 234; \\\n"
18844 " int dddddddddd = 2345;",
18845 Alignment);
18846 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
18847 verifyFormat("#define A "
18848 " \\\n"
18849 " int aaaa = 12; "
18850 " \\\n"
18851 " int b = 23; "
18852 " \\\n"
18853 " int ccc = 234; "
18854 " \\\n"
18855 " int dddddddddd = 2345;",
18856 Alignment);
18857 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18858 "k = 4, int l = 5,\n"
18859 " int m = 6) {\n"
18860 " int j = 10;\n"
18861 " otherThing = 1;\n"
18862 "}",
18863 Alignment);
18864 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18865 " int i = 1;\n"
18866 " int j = 2;\n"
18867 " int big = 10000;\n"
18868 "}",
18869 Alignment);
18870 verifyFormat("class C {\n"
18871 "public:\n"
18872 " int i = 1;\n"
18873 " virtual void f() = 0;\n"
18874 "};",
18875 Alignment);
18876 verifyFormat("int i = 1;\n"
18877 "if (SomeType t = getSomething()) {\n"
18878 "}\n"
18879 "int j = 2;\n"
18880 "int big = 10000;",
18881 Alignment);
18882 verifyFormat("int j = 7;\n"
18883 "for (int k = 0; k < N; ++k) {\n"
18884 "}\n"
18885 "int j = 2;\n"
18886 "int big = 10000;\n"
18887 "}",
18888 Alignment);
18889 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18890 verifyFormat("int i = 1;\n"
18891 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18892 " = someLooooooooooooooooongFunction();\n"
18893 "int j = 2;",
18894 Alignment);
18895 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
18896 verifyFormat("int i = 1;\n"
18897 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18898 " someLooooooooooooooooongFunction();\n"
18899 "int j = 2;",
18900 Alignment);
18901
18902 verifyFormat("auto lambda = []() {\n"
18903 " auto i = 0;\n"
18904 " return 0;\n"
18905 "};\n"
18906 "int i = 0;\n"
18907 "auto v = type{\n"
18908 " i = 1, //\n"
18909 " (i = 2), //\n"
18910 " i = 3 //\n"
18911 "};",
18912 Alignment);
18913
18914 verifyFormat(
18915 "int i = 1;\n"
18916 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18917 " loooooooooooooooooooooongParameterB);\n"
18918 "int j = 2;",
18919 Alignment);
18920
18921 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18922 " typename B = very_long_type_name_1,\n"
18923 " typename T_2 = very_long_type_name_2>\n"
18924 "auto foo() {}",
18925 Alignment);
18926 verifyFormat("int a, b = 1;\n"
18927 "int c = 2;\n"
18928 "int dd = 3;",
18929 Alignment);
18930 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18931 "float b[1][] = {{3.f}};",
18932 Alignment);
18933 verifyFormat("for (int i = 0; i < 1; i++)\n"
18934 " int x = 1;",
18935 Alignment);
18936 verifyFormat("for (i = 0; i < 1; i++)\n"
18937 " x = 1;\n"
18938 "y = 1;",
18939 Alignment);
18940
18941 EXPECT_EQ(Alignment.ReflowComments, true);
18942 Alignment.ColumnLimit = 50;
18943 verifyFormat("int x = 0;\n"
18944 "int yy = 1; /// specificlennospace\n"
18945 "int zzz = 2;",
18946 "int x = 0;\n"
18947 "int yy = 1; ///specificlennospace\n"
18948 "int zzz = 2;",
18949 Alignment);
18950
18951 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18952 "auto b = [] {\n"
18953 " f();\n"
18954 " return;\n"
18955 "};",
18956 Alignment);
18957 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18958 "auto b = g([] {\n"
18959 " f();\n"
18960 " return;\n"
18961 "});",
18962 Alignment);
18963 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18964 "auto b = g(param, [] {\n"
18965 " f();\n"
18966 " return;\n"
18967 "});",
18968 Alignment);
18969 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18970 "auto b = [] {\n"
18971 " if (condition) {\n"
18972 " return;\n"
18973 " }\n"
18974 "};",
18975 Alignment);
18976
18977 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18978 " ccc ? aaaaa : bbbbb,\n"
18979 " dddddddddddddddddddddddddd);",
18980 Alignment);
18981 // FIXME: https://llvm.org/PR53497
18982 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
18983 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18984 // " ccc ? aaaaa : bbbbb,\n"
18985 // " dddddddddddddddddddddddddd);",
18986 // Alignment);
18987
18988 // Confirm proper handling of AlignConsecutiveAssignments with
18989 // BinPackArguments.
18990 // See https://llvm.org/PR55360
18991 Alignment = getLLVMStyleWithColumns(ColumnLimit: 50);
18992 Alignment.AlignConsecutiveAssignments.Enabled = true;
18993 Alignment.BinPackArguments = false;
18994 verifyFormat("int a_long_name = 1;\n"
18995 "auto b = B({a_long_name, a_long_name},\n"
18996 " {a_longer_name_for_wrap,\n"
18997 " a_longer_name_for_wrap});",
18998 Alignment);
18999 verifyFormat("int a_long_name = 1;\n"
19000 "auto b = B{{a_long_name, a_long_name},\n"
19001 " {a_longer_name_for_wrap,\n"
19002 " a_longer_name_for_wrap}};",
19003 Alignment);
19004
19005 Alignment = getLLVMStyleWithColumns(ColumnLimit: 60);
19006 Alignment.AlignConsecutiveAssignments.Enabled = true;
19007 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19008 "using I = std::conditional_t<II::value >= 0,\n"
19009 " std::ic<int, II::value + 1>,\n"
19010 " std::ic<int, -1>>;",
19011 Alignment);
19012 verifyFormat("SomeName = Foo;\n"
19013 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19014 " arrrrrrrrrrg);",
19015 Alignment);
19016}
19017
19018TEST_F(FormatTest, AlignConsecutiveBitFields) {
19019 FormatStyle Alignment = getLLVMStyle();
19020 Alignment.AlignConsecutiveBitFields.Enabled = true;
19021 verifyFormat("int const a : 5;\n"
19022 "int oneTwoThree : 23;",
19023 Alignment);
19024
19025 // Initializers are allowed starting with c++2a
19026 verifyFormat("int const a : 5 = 1;\n"
19027 "int oneTwoThree : 23 = 0;",
19028 Alignment);
19029
19030 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19031 verifyFormat("int const a : 5;\n"
19032 "int oneTwoThree : 23;",
19033 Alignment);
19034
19035 verifyFormat("int const a : 5; // comment\n"
19036 "int oneTwoThree : 23; // comment",
19037 Alignment);
19038
19039 verifyFormat("int const a : 5 = 1;\n"
19040 "int oneTwoThree : 23 = 0;",
19041 Alignment);
19042
19043 Alignment.AlignConsecutiveAssignments.Enabled = true;
19044 verifyFormat("int const a : 5 = 1;\n"
19045 "int oneTwoThree : 23 = 0;",
19046 Alignment);
19047 verifyFormat("int const a : 5 = {1};\n"
19048 "int oneTwoThree : 23 = 0;",
19049 Alignment);
19050
19051 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19052 verifyFormat("int const a :5;\n"
19053 "int oneTwoThree:23;",
19054 Alignment);
19055
19056 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19057 verifyFormat("int const a :5;\n"
19058 "int oneTwoThree :23;",
19059 Alignment);
19060
19061 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19062 verifyFormat("int const a : 5;\n"
19063 "int oneTwoThree: 23;",
19064 Alignment);
19065
19066 // Known limitations: ':' is only recognized as a bitfield colon when
19067 // followed by a number.
19068 /*
19069 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19070 "int a : 5;",
19071 Alignment);
19072 */
19073}
19074
19075TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19076 FormatStyle Alignment = getLLVMStyle();
19077 Alignment.AlignConsecutiveMacros.Enabled = true;
19078 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19079 verifyFormat("float const a = 5;\n"
19080 "int oneTwoThree = 123;",
19081 Alignment);
19082 verifyFormat("int a = 5;\n"
19083 "float const oneTwoThree = 123;",
19084 Alignment);
19085
19086 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19087 verifyFormat("float const a = 5;\n"
19088 "int oneTwoThree = 123;",
19089 Alignment);
19090 verifyFormat("int a = method();\n"
19091 "float const oneTwoThree = 133;",
19092 Alignment);
19093 verifyFormat("int i = 1, j = 10;\n"
19094 "something = 2000;",
19095 Alignment);
19096 verifyFormat("something = 2000;\n"
19097 "int i = 1, j = 10;",
19098 Alignment);
19099 verifyFormat("float something = 2000;\n"
19100 "double another = 911;\n"
19101 "int i = 1, j = 10;\n"
19102 "const int *oneMore = 1;\n"
19103 "unsigned i = 2;",
19104 Alignment);
19105 verifyFormat("float a = 5;\n"
19106 "int one = 1;\n"
19107 "method();\n"
19108 "const double oneTwoThree = 123;\n"
19109 "const unsigned int oneTwo = 12;",
19110 Alignment);
19111 verifyFormat("int oneTwoThree{0}; // comment\n"
19112 "unsigned oneTwo; // comment",
19113 Alignment);
19114 verifyFormat("unsigned int *a;\n"
19115 "int *b;\n"
19116 "unsigned int Const *c;\n"
19117 "unsigned int const *d;\n"
19118 "unsigned int Const &e;\n"
19119 "unsigned int const &f;",
19120 Alignment);
19121 verifyFormat("Const unsigned int *c;\n"
19122 "const unsigned int *d;\n"
19123 "Const unsigned int &e;\n"
19124 "const unsigned int &f;\n"
19125 "const unsigned g;\n"
19126 "Const unsigned h;",
19127 Alignment);
19128 verifyFormat("float const a = 5;\n"
19129 "\n"
19130 "int oneTwoThree = 123;",
19131 "float const a = 5;\n"
19132 "\n"
19133 "int oneTwoThree= 123;",
19134 Alignment);
19135 verifyFormat("float a = 5;\n"
19136 "int one = 1;\n"
19137 "\n"
19138 "unsigned oneTwoThree = 123;",
19139 "float a = 5;\n"
19140 "int one = 1;\n"
19141 "\n"
19142 "unsigned oneTwoThree = 123;",
19143 Alignment);
19144 verifyFormat("float a = 5;\n"
19145 "int one = 1;\n"
19146 "\n"
19147 "unsigned oneTwoThree = 123;\n"
19148 "int oneTwo = 12;",
19149 "float a = 5;\n"
19150 "int one = 1;\n"
19151 "\n"
19152 "unsigned oneTwoThree = 123;\n"
19153 "int oneTwo = 12;",
19154 Alignment);
19155 // Function prototype alignment
19156 verifyFormat("int a();\n"
19157 "double b();",
19158 Alignment);
19159 verifyFormat("int a(int x);\n"
19160 "double b();",
19161 Alignment);
19162 verifyFormat("int a(const Test & = Test());\n"
19163 "int a1(int &foo, const Test & = Test());\n"
19164 "int a2(int &foo, const Test &name = Test());\n"
19165 "double b();",
19166 Alignment);
19167 verifyFormat("struct Test {\n"
19168 " Test(const Test &) = default;\n"
19169 " ~Test() = default;\n"
19170 " Test &operator=(const Test &) = default;\n"
19171 "};",
19172 Alignment);
19173 unsigned OldColumnLimit = Alignment.ColumnLimit;
19174 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19175 // otherwise the function parameters will be re-flowed onto a single line.
19176 Alignment.ColumnLimit = 0;
19177 verifyFormat("int a(int x,\n"
19178 " float y);\n"
19179 "double b(int x,\n"
19180 " double y);",
19181 "int a(int x,\n"
19182 " float y);\n"
19183 "double b(int x,\n"
19184 " double y);",
19185 Alignment);
19186 // This ensures that function parameters of function declarations are
19187 // correctly indented when their owning functions are indented.
19188 // The failure case here is for 'double y' to not be indented enough.
19189 verifyFormat("double a(int x);\n"
19190 "int b(int y,\n"
19191 " double z);",
19192 "double a(int x);\n"
19193 "int b(int y,\n"
19194 " double z);",
19195 Alignment);
19196 // Set ColumnLimit low so that we induce wrapping immediately after
19197 // the function name and opening paren.
19198 Alignment.ColumnLimit = 13;
19199 verifyFormat("int function(\n"
19200 " int x,\n"
19201 " bool y);",
19202 Alignment);
19203 // Set ColumnLimit low so that we break the argument list in multiple lines.
19204 Alignment.ColumnLimit = 35;
19205 verifyFormat("int a3(SomeTypeName1 &x,\n"
19206 " SomeTypeName2 &y,\n"
19207 " const Test & = Test());\n"
19208 "double b();",
19209 Alignment);
19210 Alignment.ColumnLimit = OldColumnLimit;
19211 // Ensure function pointers don't screw up recursive alignment
19212 verifyFormat("int a(int x, void (*fp)(int y));\n"
19213 "double b();",
19214 Alignment);
19215 Alignment.AlignConsecutiveAssignments.Enabled = true;
19216 verifyFormat("struct Test {\n"
19217 " Test(const Test &) = default;\n"
19218 " ~Test() = default;\n"
19219 " Test &operator=(const Test &) = default;\n"
19220 "};",
19221 Alignment);
19222 // Ensure recursive alignment is broken by function braces, so that the
19223 // "a = 1" does not align with subsequent assignments inside the function
19224 // body.
19225 verifyFormat("int func(int a = 1) {\n"
19226 " int b = 2;\n"
19227 " int cc = 3;\n"
19228 "}",
19229 Alignment);
19230 verifyFormat("float something = 2000;\n"
19231 "double another = 911;\n"
19232 "int i = 1, j = 10;\n"
19233 "const int *oneMore = 1;\n"
19234 "unsigned i = 2;",
19235 Alignment);
19236 verifyFormat("int oneTwoThree = {0}; // comment\n"
19237 "unsigned oneTwo = 0; // comment",
19238 Alignment);
19239 // Make sure that scope is correctly tracked, in the absence of braces
19240 verifyFormat("for (int i = 0; i < n; i++)\n"
19241 " j = i;\n"
19242 "double x = 1;",
19243 Alignment);
19244 verifyFormat("if (int i = 0)\n"
19245 " j = i;\n"
19246 "double x = 1;",
19247 Alignment);
19248 // Ensure operator[] and operator() are comprehended
19249 verifyFormat("struct test {\n"
19250 " long long int foo();\n"
19251 " int operator[](int a);\n"
19252 " double bar();\n"
19253 "};",
19254 Alignment);
19255 verifyFormat("struct test {\n"
19256 " long long int foo();\n"
19257 " int operator()(int a);\n"
19258 " double bar();\n"
19259 "};",
19260 Alignment);
19261 // http://llvm.org/PR52914
19262 verifyFormat("char *a[] = {\"a\", // comment\n"
19263 " \"bb\"};\n"
19264 "int bbbbbbb = 0;",
19265 Alignment);
19266 // http://llvm.org/PR68079
19267 verifyFormat("using Fn = int (A:: *)();\n"
19268 "using RFn = int (A:: *)() &;\n"
19269 "using RRFn = int (A:: *)() &&;",
19270 Alignment);
19271 verifyFormat("using Fn = int (A:: *)();\n"
19272 "using RFn = int *(A:: *)() &;\n"
19273 "using RRFn = double (A:: *)() &&;",
19274 Alignment);
19275
19276 // PAS_Right
19277 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19278 " int const i = 1;\n"
19279 " int *j = 2;\n"
19280 " int big = 10000;\n"
19281 "\n"
19282 " unsigned oneTwoThree = 123;\n"
19283 " int oneTwo = 12;\n"
19284 " method();\n"
19285 " float k = 2;\n"
19286 " int ll = 10000;\n"
19287 "}",
19288 "void SomeFunction(int parameter= 0) {\n"
19289 " int const i= 1;\n"
19290 " int *j=2;\n"
19291 " int big = 10000;\n"
19292 "\n"
19293 "unsigned oneTwoThree =123;\n"
19294 "int oneTwo = 12;\n"
19295 " method();\n"
19296 "float k= 2;\n"
19297 "int ll=10000;\n"
19298 "}",
19299 Alignment);
19300 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19301 " int const i = 1;\n"
19302 " int **j = 2, ***k;\n"
19303 " int &k = i;\n"
19304 " int &&l = i + j;\n"
19305 " int big = 10000;\n"
19306 "\n"
19307 " unsigned oneTwoThree = 123;\n"
19308 " int oneTwo = 12;\n"
19309 " method();\n"
19310 " float k = 2;\n"
19311 " int ll = 10000;\n"
19312 "}",
19313 "void SomeFunction(int parameter= 0) {\n"
19314 " int const i= 1;\n"
19315 " int **j=2,***k;\n"
19316 "int &k=i;\n"
19317 "int &&l=i+j;\n"
19318 " int big = 10000;\n"
19319 "\n"
19320 "unsigned oneTwoThree =123;\n"
19321 "int oneTwo = 12;\n"
19322 " method();\n"
19323 "float k= 2;\n"
19324 "int ll=10000;\n"
19325 "}",
19326 Alignment);
19327 // variables are aligned at their name, pointers are at the right most
19328 // position
19329 verifyFormat("int *a;\n"
19330 "int **b;\n"
19331 "int ***c;\n"
19332 "int foobar;",
19333 Alignment);
19334
19335 // PAS_Left
19336 FormatStyle AlignmentLeft = Alignment;
19337 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
19338 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19339 " int const i = 1;\n"
19340 " int* j = 2;\n"
19341 " int big = 10000;\n"
19342 "\n"
19343 " unsigned oneTwoThree = 123;\n"
19344 " int oneTwo = 12;\n"
19345 " method();\n"
19346 " float k = 2;\n"
19347 " int ll = 10000;\n"
19348 "}",
19349 "void SomeFunction(int parameter= 0) {\n"
19350 " int const i= 1;\n"
19351 " int *j=2;\n"
19352 " int big = 10000;\n"
19353 "\n"
19354 "unsigned oneTwoThree =123;\n"
19355 "int oneTwo = 12;\n"
19356 " method();\n"
19357 "float k= 2;\n"
19358 "int ll=10000;\n"
19359 "}",
19360 AlignmentLeft);
19361 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19362 " int const i = 1;\n"
19363 " int** j = 2;\n"
19364 " int& k = i;\n"
19365 " int&& l = i + j;\n"
19366 " int big = 10000;\n"
19367 "\n"
19368 " unsigned oneTwoThree = 123;\n"
19369 " int oneTwo = 12;\n"
19370 " method();\n"
19371 " float k = 2;\n"
19372 " int ll = 10000;\n"
19373 "}",
19374 "void SomeFunction(int parameter= 0) {\n"
19375 " int const i= 1;\n"
19376 " int **j=2;\n"
19377 "int &k=i;\n"
19378 "int &&l=i+j;\n"
19379 " int big = 10000;\n"
19380 "\n"
19381 "unsigned oneTwoThree =123;\n"
19382 "int oneTwo = 12;\n"
19383 " method();\n"
19384 "float k= 2;\n"
19385 "int ll=10000;\n"
19386 "}",
19387 AlignmentLeft);
19388 // variables are aligned at their name, pointers are at the left most position
19389 verifyFormat("int* a;\n"
19390 "int** b;\n"
19391 "int*** c;\n"
19392 "int foobar;",
19393 AlignmentLeft);
19394
19395 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19396 "double b();",
19397 AlignmentLeft);
19398
19399 // PAS_Middle
19400 FormatStyle AlignmentMiddle = Alignment;
19401 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
19402 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19403 " int const i = 1;\n"
19404 " int * j = 2;\n"
19405 " int big = 10000;\n"
19406 "\n"
19407 " unsigned oneTwoThree = 123;\n"
19408 " int oneTwo = 12;\n"
19409 " method();\n"
19410 " float k = 2;\n"
19411 " int ll = 10000;\n"
19412 "}",
19413 "void SomeFunction(int parameter= 0) {\n"
19414 " int const i= 1;\n"
19415 " int *j=2;\n"
19416 " int big = 10000;\n"
19417 "\n"
19418 "unsigned oneTwoThree =123;\n"
19419 "int oneTwo = 12;\n"
19420 " method();\n"
19421 "float k= 2;\n"
19422 "int ll=10000;\n"
19423 "}",
19424 AlignmentMiddle);
19425 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19426 " int const i = 1;\n"
19427 " int ** j = 2, ***k;\n"
19428 " int & k = i;\n"
19429 " int && l = i + j;\n"
19430 " int big = 10000;\n"
19431 "\n"
19432 " unsigned oneTwoThree = 123;\n"
19433 " int oneTwo = 12;\n"
19434 " method();\n"
19435 " float k = 2;\n"
19436 " int ll = 10000;\n"
19437 "}",
19438 "void SomeFunction(int parameter= 0) {\n"
19439 " int const i= 1;\n"
19440 " int **j=2,***k;\n"
19441 "int &k=i;\n"
19442 "int &&l=i+j;\n"
19443 " int big = 10000;\n"
19444 "\n"
19445 "unsigned oneTwoThree =123;\n"
19446 "int oneTwo = 12;\n"
19447 " method();\n"
19448 "float k= 2;\n"
19449 "int ll=10000;\n"
19450 "}",
19451 AlignmentMiddle);
19452 // variables are aligned at their name, pointers are in the middle
19453 verifyFormat("int * a;\n"
19454 "int * b;\n"
19455 "int *** c;\n"
19456 "int foobar;",
19457 AlignmentMiddle);
19458
19459 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19460 "double b();",
19461 AlignmentMiddle);
19462
19463 Alignment.AlignConsecutiveAssignments.Enabled = false;
19464 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19465 verifyFormat("#define A \\\n"
19466 " int aaaa = 12; \\\n"
19467 " float b = 23; \\\n"
19468 " const int ccc = 234; \\\n"
19469 " unsigned dddddddddd = 2345;",
19470 Alignment);
19471 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19472 verifyFormat("#define A \\\n"
19473 " int aaaa = 12; \\\n"
19474 " float b = 23; \\\n"
19475 " const int ccc = 234; \\\n"
19476 " unsigned dddddddddd = 2345;",
19477 Alignment);
19478 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19479 Alignment.ColumnLimit = 30;
19480 verifyFormat("#define A \\\n"
19481 " int aaaa = 12; \\\n"
19482 " float b = 23; \\\n"
19483 " const int ccc = 234; \\\n"
19484 " int dddddddddd = 2345;",
19485 Alignment);
19486 Alignment.ColumnLimit = 80;
19487 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19488 "k = 4, int l = 5,\n"
19489 " int m = 6) {\n"
19490 " const int j = 10;\n"
19491 " otherThing = 1;\n"
19492 "}",
19493 Alignment);
19494 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19495 " int const i = 1;\n"
19496 " int *j = 2;\n"
19497 " int big = 10000;\n"
19498 "}",
19499 Alignment);
19500 verifyFormat("class C {\n"
19501 "public:\n"
19502 " int i = 1;\n"
19503 " virtual void f() = 0;\n"
19504 "};",
19505 Alignment);
19506 verifyFormat("float i = 1;\n"
19507 "if (SomeType t = getSomething()) {\n"
19508 "}\n"
19509 "const unsigned j = 2;\n"
19510 "int big = 10000;",
19511 Alignment);
19512 verifyFormat("float j = 7;\n"
19513 "for (int k = 0; k < N; ++k) {\n"
19514 "}\n"
19515 "unsigned j = 2;\n"
19516 "int big = 10000;\n"
19517 "}",
19518 Alignment);
19519 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19520 verifyFormat("float i = 1;\n"
19521 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19522 " = someLooooooooooooooooongFunction();\n"
19523 "int j = 2;",
19524 Alignment);
19525 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19526 verifyFormat("int i = 1;\n"
19527 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19528 " someLooooooooooooooooongFunction();\n"
19529 "int j = 2;",
19530 Alignment);
19531
19532 Alignment.AlignConsecutiveAssignments.Enabled = true;
19533 verifyFormat("auto lambda = []() {\n"
19534 " auto ii = 0;\n"
19535 " float j = 0;\n"
19536 " return 0;\n"
19537 "};\n"
19538 "int i = 0;\n"
19539 "float i2 = 0;\n"
19540 "auto v = type{\n"
19541 " i = 1, //\n"
19542 " (i = 2), //\n"
19543 " i = 3 //\n"
19544 "};",
19545 Alignment);
19546 Alignment.AlignConsecutiveAssignments.Enabled = false;
19547
19548 verifyFormat(
19549 "int i = 1;\n"
19550 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19551 " loooooooooooooooooooooongParameterB);\n"
19552 "int j = 2;",
19553 Alignment);
19554
19555 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19556 // We expect declarations and assignments to align, as long as it doesn't
19557 // exceed the column limit, starting a new alignment sequence whenever it
19558 // happens.
19559 Alignment.AlignConsecutiveAssignments.Enabled = true;
19560 Alignment.ColumnLimit = 30;
19561 verifyFormat("float ii = 1;\n"
19562 "unsigned j = 2;\n"
19563 "int someVerylongVariable = 1;\n"
19564 "AnotherLongType ll = 123456;\n"
19565 "VeryVeryLongType k = 2;\n"
19566 "int myvar = 1;",
19567 Alignment);
19568 Alignment.ColumnLimit = 80;
19569 Alignment.AlignConsecutiveAssignments.Enabled = false;
19570
19571 verifyFormat(
19572 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19573 " typename LongType, typename B>\n"
19574 "auto foo() {}",
19575 Alignment);
19576 verifyFormat("float a, b = 1;\n"
19577 "int c = 2;\n"
19578 "int dd = 3;",
19579 Alignment);
19580 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19581 "float b[1][] = {{3.f}};",
19582 Alignment);
19583 Alignment.AlignConsecutiveAssignments.Enabled = true;
19584 verifyFormat("float a, b = 1;\n"
19585 "int c = 2;\n"
19586 "int dd = 3;",
19587 Alignment);
19588 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19589 "float b[1][] = {{3.f}};",
19590 Alignment);
19591 Alignment.AlignConsecutiveAssignments.Enabled = false;
19592
19593 Alignment.ColumnLimit = 30;
19594 Alignment.BinPackParameters = false;
19595 verifyFormat("void foo(float a,\n"
19596 " float b,\n"
19597 " int c,\n"
19598 " uint32_t *d) {\n"
19599 " int *e = 0;\n"
19600 " float f = 0;\n"
19601 " double g = 0;\n"
19602 "}\n"
19603 "void bar(ino_t a,\n"
19604 " int b,\n"
19605 " uint32_t *c,\n"
19606 " bool d) {}",
19607 Alignment);
19608 Alignment.BinPackParameters = true;
19609 Alignment.ColumnLimit = 80;
19610
19611 // Bug 33507
19612 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19613 verifyFormat(
19614 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19615 " static const Version verVs2017;\n"
19616 " return true;\n"
19617 "});",
19618 Alignment);
19619 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19620
19621 // See llvm.org/PR35641
19622 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19623 verifyFormat("int func() { //\n"
19624 " int b;\n"
19625 " unsigned c;\n"
19626 "}",
19627 Alignment);
19628
19629 // See PR37175
19630 FormatStyle Style = getMozillaStyle();
19631 Style.AlignConsecutiveDeclarations.Enabled = true;
19632 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19633 "foo(int a);",
19634 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
19635
19636 Alignment.PointerAlignment = FormatStyle::PAS_Left;
19637 verifyFormat("unsigned int* a;\n"
19638 "int* b;\n"
19639 "unsigned int Const* c;\n"
19640 "unsigned int const* d;\n"
19641 "unsigned int Const& e;\n"
19642 "unsigned int const& f;",
19643 Alignment);
19644 verifyFormat("Const unsigned int* c;\n"
19645 "const unsigned int* d;\n"
19646 "Const unsigned int& e;\n"
19647 "const unsigned int& f;\n"
19648 "const unsigned g;\n"
19649 "Const unsigned h;",
19650 Alignment);
19651
19652 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
19653 verifyFormat("unsigned int * a;\n"
19654 "int * b;\n"
19655 "unsigned int Const * c;\n"
19656 "unsigned int const * d;\n"
19657 "unsigned int Const & e;\n"
19658 "unsigned int const & f;",
19659 Alignment);
19660 verifyFormat("Const unsigned int * c;\n"
19661 "const unsigned int * d;\n"
19662 "Const unsigned int & e;\n"
19663 "const unsigned int & f;\n"
19664 "const unsigned g;\n"
19665 "Const unsigned h;",
19666 Alignment);
19667
19668 // See PR46529
19669 FormatStyle BracedAlign = getLLVMStyle();
19670 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
19671 verifyFormat("const auto result{[]() {\n"
19672 " const auto something = 1;\n"
19673 " return 2;\n"
19674 "}};",
19675 BracedAlign);
19676 verifyFormat("int foo{[]() {\n"
19677 " int bar{0};\n"
19678 " return 0;\n"
19679 "}()};",
19680 BracedAlign);
19681 BracedAlign.Cpp11BracedListStyle = false;
19682 verifyFormat("const auto result{ []() {\n"
19683 " const auto something = 1;\n"
19684 " return 2;\n"
19685 "} };",
19686 BracedAlign);
19687 verifyFormat("int foo{ []() {\n"
19688 " int bar{ 0 };\n"
19689 " return 0;\n"
19690 "}() };",
19691 BracedAlign);
19692}
19693
19694TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
19695 FormatStyle Alignment = getLLVMStyle();
19696 Alignment.AllowShortCaseLabelsOnASingleLine = true;
19697 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
19698
19699 verifyFormat("switch (level) {\n"
19700 "case log::info: return \"info\";\n"
19701 "case log::warning: return \"warning\";\n"
19702 "default: return \"default\";\n"
19703 "}",
19704 Alignment);
19705
19706 verifyFormat("switch (level) {\n"
19707 "case log::info: return \"info\";\n"
19708 "case log::warning: return \"warning\";\n"
19709 "}",
19710 "switch (level) {\n"
19711 "case log::info: return \"info\";\n"
19712 "case log::warning:\n"
19713 " return \"warning\";\n"
19714 "}",
19715 Alignment);
19716
19717 // Empty case statements push out the alignment, but non-short case labels
19718 // don't.
19719 verifyFormat("switch (level) {\n"
19720 "case log::info: return \"info\";\n"
19721 "case log::critical:\n"
19722 "case log::warning:\n"
19723 "case log::severe: return \"severe\";\n"
19724 "case log::extra_severe:\n"
19725 " // comment\n"
19726 " return \"extra_severe\";\n"
19727 "}",
19728 Alignment);
19729
19730 // Verify comments and empty lines break the alignment.
19731 verifyNoChange("switch (level) {\n"
19732 "case log::info: return \"info\";\n"
19733 "case log::warning: return \"warning\";\n"
19734 "// comment\n"
19735 "case log::critical: return \"critical\";\n"
19736 "default: return \"default\";\n"
19737 "\n"
19738 "case log::severe: return \"severe\";\n"
19739 "}",
19740 Alignment);
19741
19742 // Empty case statements don't break the alignment, and potentially push it
19743 // out.
19744 verifyFormat("switch (level) {\n"
19745 "case log::info: return \"info\";\n"
19746 "case log::warning:\n"
19747 "case log::critical:\n"
19748 "default: return \"default\";\n"
19749 "}",
19750 Alignment);
19751
19752 // Implicit fallthrough cases can be aligned with either a comment or
19753 // [[fallthrough]]
19754 verifyFormat("switch (level) {\n"
19755 "case log::info: return \"info\";\n"
19756 "case log::warning: // fallthrough\n"
19757 "case log::error: return \"error\";\n"
19758 "case log::critical: /*fallthrough*/\n"
19759 "case log::severe: return \"severe\";\n"
19760 "case log::diag: [[fallthrough]];\n"
19761 "default: return \"default\";\n"
19762 "}",
19763 Alignment);
19764
19765 // Verify trailing comment that needs a reflow also gets aligned properly.
19766 verifyFormat("switch (level) {\n"
19767 "case log::info: return \"info\";\n"
19768 "case log::warning: // fallthrough\n"
19769 "case log::error: return \"error\";\n"
19770 "}",
19771 "switch (level) {\n"
19772 "case log::info: return \"info\";\n"
19773 "case log::warning: //fallthrough\n"
19774 "case log::error: return \"error\";\n"
19775 "}",
19776 Alignment);
19777
19778 // Verify adjacent non-short case statements don't change the alignment, and
19779 // properly break the set of consecutive statements.
19780 verifyFormat("switch (level) {\n"
19781 "case log::critical:\n"
19782 " // comment\n"
19783 " return \"critical\";\n"
19784 "case log::info: return \"info\";\n"
19785 "case log::warning: return \"warning\";\n"
19786 "default:\n"
19787 " // comment\n"
19788 " return \"\";\n"
19789 "case log::error: return \"error\";\n"
19790 "case log::severe: return \"severe\";\n"
19791 "case log::extra_critical:\n"
19792 " // comment\n"
19793 " return \"extra critical\";\n"
19794 "}",
19795 Alignment);
19796
19797 Alignment.SpaceBeforeCaseColon = true;
19798 verifyFormat("switch (level) {\n"
19799 "case log::info : return \"info\";\n"
19800 "case log::warning : return \"warning\";\n"
19801 "default : return \"default\";\n"
19802 "}",
19803 Alignment);
19804 Alignment.SpaceBeforeCaseColon = false;
19805
19806 // Make sure we don't incorrectly align correctly across nested switch cases.
19807 verifyFormat("switch (level) {\n"
19808 "case log::info: return \"info\";\n"
19809 "case log::warning: return \"warning\";\n"
19810 "case log::other:\n"
19811 " switch (sublevel) {\n"
19812 " case log::info: return \"info\";\n"
19813 " case log::warning: return \"warning\";\n"
19814 " }\n"
19815 " break;\n"
19816 "case log::error: return \"error\";\n"
19817 "default: return \"default\";\n"
19818 "}",
19819 "switch (level) {\n"
19820 "case log::info: return \"info\";\n"
19821 "case log::warning: return \"warning\";\n"
19822 "case log::other: switch (sublevel) {\n"
19823 " case log::info: return \"info\";\n"
19824 " case log::warning: return \"warning\";\n"
19825 "}\n"
19826 "break;\n"
19827 "case log::error: return \"error\";\n"
19828 "default: return \"default\";\n"
19829 "}",
19830 Alignment);
19831
19832 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
19833
19834 verifyFormat("switch (level) {\n"
19835 "case log::info: return \"info\";\n"
19836 "\n"
19837 "case log::warning: return \"warning\";\n"
19838 "}",
19839 "switch (level) {\n"
19840 "case log::info: return \"info\";\n"
19841 "\n"
19842 "case log::warning: return \"warning\";\n"
19843 "}",
19844 Alignment);
19845
19846 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
19847
19848 verifyNoChange("switch (level) {\n"
19849 "case log::info: return \"info\";\n"
19850 "\n"
19851 "/* block comment */\n"
19852 "\n"
19853 "// line comment\n"
19854 "case log::warning: return \"warning\";\n"
19855 "}",
19856 Alignment);
19857
19858 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
19859
19860 verifyFormat("switch (level) {\n"
19861 "case log::info: return \"info\";\n"
19862 "//\n"
19863 "case log::warning: return \"warning\";\n"
19864 "}",
19865 Alignment);
19866
19867 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
19868
19869 verifyFormat("switch (level) {\n"
19870 "case log::info : return \"info\";\n"
19871 "case log::warning: return \"warning\";\n"
19872 "default : return \"default\";\n"
19873 "}",
19874 Alignment);
19875
19876 // With AlignCaseColons, empty case statements don't break alignment of
19877 // consecutive case statements (and are aligned).
19878 verifyFormat("switch (level) {\n"
19879 "case log::info : return \"info\";\n"
19880 "case log::warning :\n"
19881 "case log::critical:\n"
19882 "default : return \"default\";\n"
19883 "}",
19884 Alignment);
19885
19886 // Final non-short case labels shouldn't have their colon aligned
19887 verifyFormat("switch (level) {\n"
19888 "case log::info : return \"info\";\n"
19889 "case log::warning :\n"
19890 "case log::critical:\n"
19891 "case log::severe : return \"severe\";\n"
19892 "default:\n"
19893 " // comment\n"
19894 " return \"default\";\n"
19895 "}",
19896 Alignment);
19897
19898 // Verify adjacent non-short case statements break the set of consecutive
19899 // alignments and aren't aligned with adjacent non-short case statements if
19900 // AlignCaseColons is set.
19901 verifyFormat("switch (level) {\n"
19902 "case log::critical:\n"
19903 " // comment\n"
19904 " return \"critical\";\n"
19905 "case log::info : return \"info\";\n"
19906 "case log::warning: return \"warning\";\n"
19907 "default:\n"
19908 " // comment\n"
19909 " return \"\";\n"
19910 "case log::error : return \"error\";\n"
19911 "case log::severe: return \"severe\";\n"
19912 "case log::extra_critical:\n"
19913 " // comment\n"
19914 " return \"extra critical\";\n"
19915 "}",
19916 Alignment);
19917
19918 Alignment.SpaceBeforeCaseColon = true;
19919 verifyFormat("switch (level) {\n"
19920 "case log::info : return \"info\";\n"
19921 "case log::warning : return \"warning\";\n"
19922 "case log::error :\n"
19923 "default : return \"default\";\n"
19924 "}",
19925 Alignment);
19926}
19927
19928TEST_F(FormatTest, AlignWithLineBreaks) {
19929 auto Style = getLLVMStyleWithColumns(ColumnLimit: 120);
19930
19931 EXPECT_EQ(Style.AlignConsecutiveAssignments,
19932 FormatStyle::AlignConsecutiveStyle(
19933 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
19934 /*AcrossComments=*/false, /*AlignCompound=*/false,
19935 /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
19936 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
19937 FormatStyle::AlignConsecutiveStyle({}));
19938 verifyFormat("void foo() {\n"
19939 " int myVar = 5;\n"
19940 " double x = 3.14;\n"
19941 " auto str = \"Hello \"\n"
19942 " \"World\";\n"
19943 " auto s = \"Hello \"\n"
19944 " \"Again\";\n"
19945 "}",
19946 Style);
19947
19948 // clang-format off
19949 verifyFormat("void foo() {\n"
19950 " const int capacityBefore = Entries.capacity();\n"
19951 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19952 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19953 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19954 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19955 "}",
19956 Style);
19957 // clang-format on
19958
19959 Style.AlignConsecutiveAssignments.Enabled = true;
19960 verifyFormat("void foo() {\n"
19961 " int myVar = 5;\n"
19962 " double x = 3.14;\n"
19963 " auto str = \"Hello \"\n"
19964 " \"World\";\n"
19965 " auto s = \"Hello \"\n"
19966 " \"Again\";\n"
19967 "}",
19968 Style);
19969
19970 // clang-format off
19971 verifyFormat("void foo() {\n"
19972 " const int capacityBefore = Entries.capacity();\n"
19973 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19974 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19975 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19976 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19977 "}",
19978 Style);
19979 // clang-format on
19980
19981 Style.AlignConsecutiveAssignments.Enabled = false;
19982 Style.AlignConsecutiveDeclarations.Enabled = true;
19983 verifyFormat("void foo() {\n"
19984 " int myVar = 5;\n"
19985 " double x = 3.14;\n"
19986 " auto str = \"Hello \"\n"
19987 " \"World\";\n"
19988 " auto s = \"Hello \"\n"
19989 " \"Again\";\n"
19990 "}",
19991 Style);
19992
19993 // clang-format off
19994 verifyFormat("void foo() {\n"
19995 " const int capacityBefore = Entries.capacity();\n"
19996 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19997 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19998 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19999 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20000 "}",
20001 Style);
20002 // clang-format on
20003
20004 Style.AlignConsecutiveAssignments.Enabled = true;
20005 Style.AlignConsecutiveDeclarations.Enabled = true;
20006
20007 verifyFormat("void foo() {\n"
20008 " int myVar = 5;\n"
20009 " double x = 3.14;\n"
20010 " auto str = \"Hello \"\n"
20011 " \"World\";\n"
20012 " auto s = \"Hello \"\n"
20013 " \"Again\";\n"
20014 "}",
20015 Style);
20016
20017 // clang-format off
20018 verifyFormat("void foo() {\n"
20019 " const int capacityBefore = Entries.capacity();\n"
20020 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20021 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20022 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20023 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20024 "}",
20025 Style);
20026 // clang-format on
20027
20028 Style = getLLVMStyleWithColumns(ColumnLimit: 20);
20029 Style.AlignConsecutiveAssignments.Enabled = true;
20030 Style.IndentWidth = 4;
20031
20032 verifyFormat("void foo() {\n"
20033 " int i1 = 1;\n"
20034 " int j = 0;\n"
20035 " int k = bar(\n"
20036 " argument1,\n"
20037 " argument2);\n"
20038 "}",
20039 Style);
20040
20041 verifyFormat("unsigned i = 0;\n"
20042 "int a[] = {\n"
20043 " 1234567890,\n"
20044 " -1234567890};",
20045 Style);
20046
20047 Style.ColumnLimit = 120;
20048
20049 // clang-format off
20050 verifyFormat("void SomeFunc() {\n"
20051 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20052 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20053 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20054 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20055 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20056 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20057 "}",
20058 Style);
20059 // clang-format on
20060
20061 Style.BinPackArguments = false;
20062
20063 // clang-format off
20064 verifyFormat("void SomeFunc() {\n"
20065 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20066 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20067 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20068 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20069 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20070 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20071 "}",
20072 Style);
20073 // clang-format on
20074}
20075
20076TEST_F(FormatTest, AlignWithInitializerPeriods) {
20077 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
20078
20079 verifyFormat("void foo1(void) {\n"
20080 " BYTE p[1] = 1;\n"
20081 " A B = {.one_foooooooooooooooo = 2,\n"
20082 " .two_fooooooooooooo = 3,\n"
20083 " .three_fooooooooooooo = 4};\n"
20084 " BYTE payload = 2;\n"
20085 "}",
20086 Style);
20087
20088 Style.AlignConsecutiveAssignments.Enabled = true;
20089 Style.AlignConsecutiveDeclarations.Enabled = false;
20090 verifyFormat("void foo2(void) {\n"
20091 " BYTE p[1] = 1;\n"
20092 " A B = {.one_foooooooooooooooo = 2,\n"
20093 " .two_fooooooooooooo = 3,\n"
20094 " .three_fooooooooooooo = 4};\n"
20095 " BYTE payload = 2;\n"
20096 "}",
20097 Style);
20098
20099 Style.AlignConsecutiveAssignments.Enabled = false;
20100 Style.AlignConsecutiveDeclarations.Enabled = true;
20101 verifyFormat("void foo3(void) {\n"
20102 " BYTE p[1] = 1;\n"
20103 " A B = {.one_foooooooooooooooo = 2,\n"
20104 " .two_fooooooooooooo = 3,\n"
20105 " .three_fooooooooooooo = 4};\n"
20106 " BYTE payload = 2;\n"
20107 "}",
20108 Style);
20109
20110 Style.AlignConsecutiveAssignments.Enabled = true;
20111 Style.AlignConsecutiveDeclarations.Enabled = true;
20112 verifyFormat("void foo4(void) {\n"
20113 " BYTE p[1] = 1;\n"
20114 " A B = {.one_foooooooooooooooo = 2,\n"
20115 " .two_fooooooooooooo = 3,\n"
20116 " .three_fooooooooooooo = 4};\n"
20117 " BYTE payload = 2;\n"
20118 "}",
20119 Style);
20120}
20121
20122TEST_F(FormatTest, LinuxBraceBreaking) {
20123 FormatStyle LinuxBraceStyle = getLLVMStyle();
20124 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
20125 verifyFormat("namespace a\n"
20126 "{\n"
20127 "class A\n"
20128 "{\n"
20129 " void f()\n"
20130 " {\n"
20131 " if (true) {\n"
20132 " a();\n"
20133 " b();\n"
20134 " } else {\n"
20135 " a();\n"
20136 " }\n"
20137 " }\n"
20138 " void g() { return; }\n"
20139 "};\n"
20140 "struct B {\n"
20141 " int x;\n"
20142 "};\n"
20143 "} // namespace a",
20144 LinuxBraceStyle);
20145 verifyFormat("enum X {\n"
20146 " Y = 0,\n"
20147 "}",
20148 LinuxBraceStyle);
20149 verifyFormat("struct S {\n"
20150 " int Type;\n"
20151 " union {\n"
20152 " int x;\n"
20153 " double y;\n"
20154 " } Value;\n"
20155 " class C\n"
20156 " {\n"
20157 " MyFavoriteType Value;\n"
20158 " } Class;\n"
20159 "}",
20160 LinuxBraceStyle);
20161}
20162
20163TEST_F(FormatTest, MozillaBraceBreaking) {
20164 FormatStyle MozillaBraceStyle = getLLVMStyle();
20165 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
20166 MozillaBraceStyle.FixNamespaceComments = false;
20167 verifyFormat("namespace a {\n"
20168 "class A\n"
20169 "{\n"
20170 " void f()\n"
20171 " {\n"
20172 " if (true) {\n"
20173 " a();\n"
20174 " b();\n"
20175 " }\n"
20176 " }\n"
20177 " void g() { return; }\n"
20178 "};\n"
20179 "enum E\n"
20180 "{\n"
20181 " A,\n"
20182 " // foo\n"
20183 " B,\n"
20184 " C\n"
20185 "};\n"
20186 "struct B\n"
20187 "{\n"
20188 " int x;\n"
20189 "};\n"
20190 "}",
20191 MozillaBraceStyle);
20192 verifyFormat("struct S\n"
20193 "{\n"
20194 " int Type;\n"
20195 " union\n"
20196 " {\n"
20197 " int x;\n"
20198 " double y;\n"
20199 " } Value;\n"
20200 " class C\n"
20201 " {\n"
20202 " MyFavoriteType Value;\n"
20203 " } Class;\n"
20204 "}",
20205 MozillaBraceStyle);
20206}
20207
20208TEST_F(FormatTest, StroustrupBraceBreaking) {
20209 FormatStyle StroustrupBraceStyle = getLLVMStyle();
20210 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
20211 verifyFormat("namespace a {\n"
20212 "class A {\n"
20213 " void f()\n"
20214 " {\n"
20215 " if (true) {\n"
20216 " a();\n"
20217 " b();\n"
20218 " }\n"
20219 " }\n"
20220 " void g() { return; }\n"
20221 "};\n"
20222 "struct B {\n"
20223 " int x;\n"
20224 "};\n"
20225 "} // namespace a",
20226 StroustrupBraceStyle);
20227
20228 verifyFormat("void foo()\n"
20229 "{\n"
20230 " if (a) {\n"
20231 " a();\n"
20232 " }\n"
20233 " else {\n"
20234 " b();\n"
20235 " }\n"
20236 "}",
20237 StroustrupBraceStyle);
20238
20239 verifyFormat("#ifdef _DEBUG\n"
20240 "int foo(int i = 0)\n"
20241 "#else\n"
20242 "int foo(int i = 5)\n"
20243 "#endif\n"
20244 "{\n"
20245 " return i;\n"
20246 "}",
20247 StroustrupBraceStyle);
20248
20249 verifyFormat("void foo() {}\n"
20250 "void bar()\n"
20251 "#ifdef _DEBUG\n"
20252 "{\n"
20253 " foo();\n"
20254 "}\n"
20255 "#else\n"
20256 "{\n"
20257 "}\n"
20258 "#endif",
20259 StroustrupBraceStyle);
20260
20261 verifyFormat("void foobar() { int i = 5; }\n"
20262 "#ifdef _DEBUG\n"
20263 "void bar() {}\n"
20264 "#else\n"
20265 "void bar() { foobar(); }\n"
20266 "#endif",
20267 StroustrupBraceStyle);
20268}
20269
20270TEST_F(FormatTest, AllmanBraceBreaking) {
20271 FormatStyle AllmanBraceStyle = getLLVMStyle();
20272 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
20273
20274 verifyFormat("namespace a\n"
20275 "{\n"
20276 "void f();\n"
20277 "void g();\n"
20278 "} // namespace a",
20279 "namespace a\n"
20280 "{\n"
20281 "void f();\n"
20282 "void g();\n"
20283 "}",
20284 AllmanBraceStyle);
20285
20286 verifyFormat("namespace a\n"
20287 "{\n"
20288 "class A\n"
20289 "{\n"
20290 " void f()\n"
20291 " {\n"
20292 " if (true)\n"
20293 " {\n"
20294 " a();\n"
20295 " b();\n"
20296 " }\n"
20297 " }\n"
20298 " void g() { return; }\n"
20299 "};\n"
20300 "struct B\n"
20301 "{\n"
20302 " int x;\n"
20303 "};\n"
20304 "union C\n"
20305 "{\n"
20306 "};\n"
20307 "} // namespace a",
20308 AllmanBraceStyle);
20309
20310 verifyFormat("void f()\n"
20311 "{\n"
20312 " if (true)\n"
20313 " {\n"
20314 " a();\n"
20315 " }\n"
20316 " else if (false)\n"
20317 " {\n"
20318 " b();\n"
20319 " }\n"
20320 " else\n"
20321 " {\n"
20322 " c();\n"
20323 " }\n"
20324 "}",
20325 AllmanBraceStyle);
20326
20327 verifyFormat("void f()\n"
20328 "{\n"
20329 " for (int i = 0; i < 10; ++i)\n"
20330 " {\n"
20331 " a();\n"
20332 " }\n"
20333 " while (false)\n"
20334 " {\n"
20335 " b();\n"
20336 " }\n"
20337 " do\n"
20338 " {\n"
20339 " c();\n"
20340 " } while (false)\n"
20341 "}",
20342 AllmanBraceStyle);
20343
20344 verifyFormat("void f(int a)\n"
20345 "{\n"
20346 " switch (a)\n"
20347 " {\n"
20348 " case 0:\n"
20349 " break;\n"
20350 " case 1:\n"
20351 " {\n"
20352 " break;\n"
20353 " }\n"
20354 " case 2:\n"
20355 " {\n"
20356 " }\n"
20357 " break;\n"
20358 " default:\n"
20359 " break;\n"
20360 " }\n"
20361 "}",
20362 AllmanBraceStyle);
20363
20364 verifyFormat("enum X\n"
20365 "{\n"
20366 " Y = 0,\n"
20367 "}",
20368 AllmanBraceStyle);
20369 verifyFormat("enum X\n"
20370 "{\n"
20371 " Y = 0\n"
20372 "}",
20373 AllmanBraceStyle);
20374
20375 verifyFormat("@interface BSApplicationController ()\n"
20376 "{\n"
20377 "@private\n"
20378 " id _extraIvar;\n"
20379 "}\n"
20380 "@end",
20381 AllmanBraceStyle);
20382
20383 verifyFormat("#ifdef _DEBUG\n"
20384 "int foo(int i = 0)\n"
20385 "#else\n"
20386 "int foo(int i = 5)\n"
20387 "#endif\n"
20388 "{\n"
20389 " return i;\n"
20390 "}",
20391 AllmanBraceStyle);
20392
20393 verifyFormat("void foo() {}\n"
20394 "void bar()\n"
20395 "#ifdef _DEBUG\n"
20396 "{\n"
20397 " foo();\n"
20398 "}\n"
20399 "#else\n"
20400 "{\n"
20401 "}\n"
20402 "#endif",
20403 AllmanBraceStyle);
20404
20405 verifyFormat("void foobar() { int i = 5; }\n"
20406 "#ifdef _DEBUG\n"
20407 "void bar() {}\n"
20408 "#else\n"
20409 "void bar() { foobar(); }\n"
20410 "#endif",
20411 AllmanBraceStyle);
20412
20413 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
20414 FormatStyle::SLS_All);
20415
20416 verifyFormat("[](int i) { return i + 2; };\n"
20417 "[](int i, int j)\n"
20418 "{\n"
20419 " auto x = i + j;\n"
20420 " auto y = i * j;\n"
20421 " return x ^ y;\n"
20422 "};\n"
20423 "void foo()\n"
20424 "{\n"
20425 " auto shortLambda = [](int i) { return i + 2; };\n"
20426 " auto longLambda = [](int i, int j)\n"
20427 " {\n"
20428 " auto x = i + j;\n"
20429 " auto y = i * j;\n"
20430 " return x ^ y;\n"
20431 " };\n"
20432 "}",
20433 AllmanBraceStyle);
20434
20435 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20436
20437 verifyFormat("[](int i)\n"
20438 "{\n"
20439 " return i + 2;\n"
20440 "};\n"
20441 "[](int i, int j)\n"
20442 "{\n"
20443 " auto x = i + j;\n"
20444 " auto y = i * j;\n"
20445 " return x ^ y;\n"
20446 "};\n"
20447 "void foo()\n"
20448 "{\n"
20449 " auto shortLambda = [](int i)\n"
20450 " {\n"
20451 " return i + 2;\n"
20452 " };\n"
20453 " auto longLambda = [](int i, int j)\n"
20454 " {\n"
20455 " auto x = i + j;\n"
20456 " auto y = i * j;\n"
20457 " return x ^ y;\n"
20458 " };\n"
20459 "}",
20460 AllmanBraceStyle);
20461
20462 // Reset
20463 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
20464
20465 // This shouldn't affect ObjC blocks..
20466 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20467 " // ...\n"
20468 " int i;\n"
20469 "}];",
20470 AllmanBraceStyle);
20471 verifyFormat("void (^block)(void) = ^{\n"
20472 " // ...\n"
20473 " int i;\n"
20474 "};",
20475 AllmanBraceStyle);
20476 // .. or dict literals.
20477 verifyFormat("void f()\n"
20478 "{\n"
20479 " // ...\n"
20480 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20481 "}",
20482 AllmanBraceStyle);
20483 verifyFormat("void f()\n"
20484 "{\n"
20485 " // ...\n"
20486 " [object someMethod:@{a : @\"b\"}];\n"
20487 "}",
20488 AllmanBraceStyle);
20489 verifyFormat("int f()\n"
20490 "{ // comment\n"
20491 " return 42;\n"
20492 "}",
20493 AllmanBraceStyle);
20494
20495 AllmanBraceStyle.ColumnLimit = 19;
20496 verifyFormat("void f() { int i; }", AllmanBraceStyle);
20497 AllmanBraceStyle.ColumnLimit = 18;
20498 verifyFormat("void f()\n"
20499 "{\n"
20500 " int i;\n"
20501 "}",
20502 AllmanBraceStyle);
20503 AllmanBraceStyle.ColumnLimit = 80;
20504
20505 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
20506 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20507 FormatStyle::SIS_WithoutElse;
20508 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20509 verifyFormat("void f(bool b)\n"
20510 "{\n"
20511 " if (b)\n"
20512 " {\n"
20513 " return;\n"
20514 " }\n"
20515 "}",
20516 BreakBeforeBraceShortIfs);
20517 verifyFormat("void f(bool b)\n"
20518 "{\n"
20519 " if constexpr (b)\n"
20520 " {\n"
20521 " return;\n"
20522 " }\n"
20523 "}",
20524 BreakBeforeBraceShortIfs);
20525 verifyFormat("void f(bool b)\n"
20526 "{\n"
20527 " if CONSTEXPR (b)\n"
20528 " {\n"
20529 " return;\n"
20530 " }\n"
20531 "}",
20532 BreakBeforeBraceShortIfs);
20533 verifyFormat("void f(bool b)\n"
20534 "{\n"
20535 " if (b) return;\n"
20536 "}",
20537 BreakBeforeBraceShortIfs);
20538 verifyFormat("void f(bool b)\n"
20539 "{\n"
20540 " if constexpr (b) return;\n"
20541 "}",
20542 BreakBeforeBraceShortIfs);
20543 verifyFormat("void f(bool b)\n"
20544 "{\n"
20545 " if CONSTEXPR (b) return;\n"
20546 "}",
20547 BreakBeforeBraceShortIfs);
20548 verifyFormat("void f(bool b)\n"
20549 "{\n"
20550 " while (b)\n"
20551 " {\n"
20552 " return;\n"
20553 " }\n"
20554 "}",
20555 BreakBeforeBraceShortIfs);
20556}
20557
20558TEST_F(FormatTest, WhitesmithsBraceBreaking) {
20559 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(ColumnLimit: 0);
20560 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
20561
20562 // Make a few changes to the style for testing purposes
20563 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
20564 FormatStyle::SFS_Empty;
20565 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
20566
20567 // FIXME: this test case can't decide whether there should be a blank line
20568 // after the ~D() line or not. It adds one if one doesn't exist in the test
20569 // and it removes the line if one exists.
20570 /*
20571 verifyFormat("class A;\n"
20572 "namespace B\n"
20573 " {\n"
20574 "class C;\n"
20575 "// Comment\n"
20576 "class D\n"
20577 " {\n"
20578 "public:\n"
20579 " D();\n"
20580 " ~D() {}\n"
20581 "private:\n"
20582 " enum E\n"
20583 " {\n"
20584 " F\n"
20585 " }\n"
20586 " };\n"
20587 " } // namespace B",
20588 WhitesmithsBraceStyle);
20589 */
20590
20591 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
20592 verifyFormat("namespace a\n"
20593 " {\n"
20594 "class A\n"
20595 " {\n"
20596 " void f()\n"
20597 " {\n"
20598 " if (true)\n"
20599 " {\n"
20600 " a();\n"
20601 " b();\n"
20602 " }\n"
20603 " }\n"
20604 " void g()\n"
20605 " {\n"
20606 " return;\n"
20607 " }\n"
20608 " };\n"
20609 "struct B\n"
20610 " {\n"
20611 " int x;\n"
20612 " };\n"
20613 " } // namespace a",
20614 WhitesmithsBraceStyle);
20615
20616 verifyFormat("namespace a\n"
20617 " {\n"
20618 "namespace b\n"
20619 " {\n"
20620 "class A\n"
20621 " {\n"
20622 " void f()\n"
20623 " {\n"
20624 " if (true)\n"
20625 " {\n"
20626 " a();\n"
20627 " b();\n"
20628 " }\n"
20629 " }\n"
20630 " void g()\n"
20631 " {\n"
20632 " return;\n"
20633 " }\n"
20634 " };\n"
20635 "struct B\n"
20636 " {\n"
20637 " int x;\n"
20638 " };\n"
20639 " } // namespace b\n"
20640 " } // namespace a",
20641 WhitesmithsBraceStyle);
20642
20643 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
20644 verifyFormat("namespace a\n"
20645 " {\n"
20646 "namespace b\n"
20647 " {\n"
20648 " class A\n"
20649 " {\n"
20650 " void f()\n"
20651 " {\n"
20652 " if (true)\n"
20653 " {\n"
20654 " a();\n"
20655 " b();\n"
20656 " }\n"
20657 " }\n"
20658 " void g()\n"
20659 " {\n"
20660 " return;\n"
20661 " }\n"
20662 " };\n"
20663 " struct B\n"
20664 " {\n"
20665 " int x;\n"
20666 " };\n"
20667 " } // namespace b\n"
20668 " } // namespace a",
20669 WhitesmithsBraceStyle);
20670
20671 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
20672 verifyFormat("namespace a\n"
20673 " {\n"
20674 " namespace b\n"
20675 " {\n"
20676 " class A\n"
20677 " {\n"
20678 " void f()\n"
20679 " {\n"
20680 " if (true)\n"
20681 " {\n"
20682 " a();\n"
20683 " b();\n"
20684 " }\n"
20685 " }\n"
20686 " void g()\n"
20687 " {\n"
20688 " return;\n"
20689 " }\n"
20690 " };\n"
20691 " struct B\n"
20692 " {\n"
20693 " int x;\n"
20694 " };\n"
20695 " } // namespace b\n"
20696 " } // namespace a",
20697 WhitesmithsBraceStyle);
20698
20699 verifyFormat("void f()\n"
20700 " {\n"
20701 " if (true)\n"
20702 " {\n"
20703 " a();\n"
20704 " }\n"
20705 " else if (false)\n"
20706 " {\n"
20707 " b();\n"
20708 " }\n"
20709 " else\n"
20710 " {\n"
20711 " c();\n"
20712 " }\n"
20713 " }",
20714 WhitesmithsBraceStyle);
20715
20716 verifyFormat("void f()\n"
20717 " {\n"
20718 " for (int i = 0; i < 10; ++i)\n"
20719 " {\n"
20720 " a();\n"
20721 " }\n"
20722 " while (false)\n"
20723 " {\n"
20724 " b();\n"
20725 " }\n"
20726 " do\n"
20727 " {\n"
20728 " c();\n"
20729 " } while (false)\n"
20730 " }",
20731 WhitesmithsBraceStyle);
20732
20733 WhitesmithsBraceStyle.IndentCaseLabels = true;
20734 verifyFormat("void switchTest1(int a)\n"
20735 " {\n"
20736 " switch (a)\n"
20737 " {\n"
20738 " case 2:\n"
20739 " {\n"
20740 " }\n"
20741 " break;\n"
20742 " }\n"
20743 " }",
20744 WhitesmithsBraceStyle);
20745
20746 verifyFormat("void switchTest2(int a)\n"
20747 " {\n"
20748 " switch (a)\n"
20749 " {\n"
20750 " case 0:\n"
20751 " break;\n"
20752 " case 1:\n"
20753 " {\n"
20754 " break;\n"
20755 " }\n"
20756 " case 2:\n"
20757 " {\n"
20758 " }\n"
20759 " break;\n"
20760 " default:\n"
20761 " break;\n"
20762 " }\n"
20763 " }",
20764 WhitesmithsBraceStyle);
20765
20766 verifyFormat("void switchTest3(int a)\n"
20767 " {\n"
20768 " switch (a)\n"
20769 " {\n"
20770 " case 0:\n"
20771 " {\n"
20772 " foo(x);\n"
20773 " }\n"
20774 " break;\n"
20775 " default:\n"
20776 " {\n"
20777 " foo(1);\n"
20778 " }\n"
20779 " break;\n"
20780 " }\n"
20781 " }",
20782 WhitesmithsBraceStyle);
20783
20784 WhitesmithsBraceStyle.IndentCaseLabels = false;
20785
20786 verifyFormat("void switchTest4(int a)\n"
20787 " {\n"
20788 " switch (a)\n"
20789 " {\n"
20790 " case 2:\n"
20791 " {\n"
20792 " }\n"
20793 " break;\n"
20794 " }\n"
20795 " }",
20796 WhitesmithsBraceStyle);
20797
20798 verifyFormat("void switchTest5(int a)\n"
20799 " {\n"
20800 " switch (a)\n"
20801 " {\n"
20802 " case 0:\n"
20803 " break;\n"
20804 " case 1:\n"
20805 " {\n"
20806 " foo();\n"
20807 " break;\n"
20808 " }\n"
20809 " case 2:\n"
20810 " {\n"
20811 " }\n"
20812 " break;\n"
20813 " default:\n"
20814 " break;\n"
20815 " }\n"
20816 " }",
20817 WhitesmithsBraceStyle);
20818
20819 verifyFormat("void switchTest6(int a)\n"
20820 " {\n"
20821 " switch (a)\n"
20822 " {\n"
20823 " case 0:\n"
20824 " {\n"
20825 " foo(x);\n"
20826 " }\n"
20827 " break;\n"
20828 " default:\n"
20829 " {\n"
20830 " foo(1);\n"
20831 " }\n"
20832 " break;\n"
20833 " }\n"
20834 " }",
20835 WhitesmithsBraceStyle);
20836
20837 verifyFormat("enum X\n"
20838 " {\n"
20839 " Y = 0, // testing\n"
20840 " }",
20841 WhitesmithsBraceStyle);
20842
20843 verifyFormat("enum X\n"
20844 " {\n"
20845 " Y = 0\n"
20846 " }",
20847 WhitesmithsBraceStyle);
20848 verifyFormat("enum X\n"
20849 " {\n"
20850 " Y = 0,\n"
20851 " Z = 1\n"
20852 " };",
20853 WhitesmithsBraceStyle);
20854
20855 verifyFormat("@interface BSApplicationController ()\n"
20856 " {\n"
20857 "@private\n"
20858 " id _extraIvar;\n"
20859 " }\n"
20860 "@end",
20861 WhitesmithsBraceStyle);
20862
20863 verifyFormat("#ifdef _DEBUG\n"
20864 "int foo(int i = 0)\n"
20865 "#else\n"
20866 "int foo(int i = 5)\n"
20867 "#endif\n"
20868 " {\n"
20869 " return i;\n"
20870 " }",
20871 WhitesmithsBraceStyle);
20872
20873 verifyFormat("void foo() {}\n"
20874 "void bar()\n"
20875 "#ifdef _DEBUG\n"
20876 " {\n"
20877 " foo();\n"
20878 " }\n"
20879 "#else\n"
20880 " {\n"
20881 " }\n"
20882 "#endif",
20883 WhitesmithsBraceStyle);
20884
20885 verifyFormat("void foobar()\n"
20886 " {\n"
20887 " int i = 5;\n"
20888 " }\n"
20889 "#ifdef _DEBUG\n"
20890 "void bar() {}\n"
20891 "#else\n"
20892 "void bar()\n"
20893 " {\n"
20894 " foobar();\n"
20895 " }\n"
20896 "#endif",
20897 WhitesmithsBraceStyle);
20898
20899 // This shouldn't affect ObjC blocks..
20900 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20901 " // ...\n"
20902 " int i;\n"
20903 "}];",
20904 WhitesmithsBraceStyle);
20905 verifyFormat("void (^block)(void) = ^{\n"
20906 " // ...\n"
20907 " int i;\n"
20908 "};",
20909 WhitesmithsBraceStyle);
20910 // .. or dict literals.
20911 verifyFormat("void f()\n"
20912 " {\n"
20913 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20914 " }",
20915 WhitesmithsBraceStyle);
20916
20917 verifyFormat("int f()\n"
20918 " { // comment\n"
20919 " return 42;\n"
20920 " }",
20921 WhitesmithsBraceStyle);
20922
20923 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
20924 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
20925 FormatStyle::SIS_OnlyFirstIf;
20926 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
20927 verifyFormat("void f(bool b)\n"
20928 " {\n"
20929 " if (b)\n"
20930 " {\n"
20931 " return;\n"
20932 " }\n"
20933 " }",
20934 BreakBeforeBraceShortIfs);
20935 verifyFormat("void f(bool b)\n"
20936 " {\n"
20937 " if (b) return;\n"
20938 " }",
20939 BreakBeforeBraceShortIfs);
20940 verifyFormat("void f(bool b)\n"
20941 " {\n"
20942 " while (b)\n"
20943 " {\n"
20944 " return;\n"
20945 " }\n"
20946 " }",
20947 BreakBeforeBraceShortIfs);
20948}
20949
20950TEST_F(FormatTest, GNUBraceBreaking) {
20951 FormatStyle GNUBraceStyle = getLLVMStyle();
20952 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
20953 verifyFormat("namespace a\n"
20954 "{\n"
20955 "class A\n"
20956 "{\n"
20957 " void f()\n"
20958 " {\n"
20959 " int a;\n"
20960 " {\n"
20961 " int b;\n"
20962 " }\n"
20963 " if (true)\n"
20964 " {\n"
20965 " a();\n"
20966 " b();\n"
20967 " }\n"
20968 " }\n"
20969 " void g() { return; }\n"
20970 "}\n"
20971 "} // namespace a",
20972 GNUBraceStyle);
20973
20974 verifyFormat("void f()\n"
20975 "{\n"
20976 " if (true)\n"
20977 " {\n"
20978 " a();\n"
20979 " }\n"
20980 " else if (false)\n"
20981 " {\n"
20982 " b();\n"
20983 " }\n"
20984 " else\n"
20985 " {\n"
20986 " c();\n"
20987 " }\n"
20988 "}",
20989 GNUBraceStyle);
20990
20991 verifyFormat("void f()\n"
20992 "{\n"
20993 " for (int i = 0; i < 10; ++i)\n"
20994 " {\n"
20995 " a();\n"
20996 " }\n"
20997 " while (false)\n"
20998 " {\n"
20999 " b();\n"
21000 " }\n"
21001 " do\n"
21002 " {\n"
21003 " c();\n"
21004 " }\n"
21005 " while (false);\n"
21006 "}",
21007 GNUBraceStyle);
21008
21009 verifyFormat("void f(int a)\n"
21010 "{\n"
21011 " switch (a)\n"
21012 " {\n"
21013 " case 0:\n"
21014 " break;\n"
21015 " case 1:\n"
21016 " {\n"
21017 " break;\n"
21018 " }\n"
21019 " case 2:\n"
21020 " {\n"
21021 " }\n"
21022 " break;\n"
21023 " default:\n"
21024 " break;\n"
21025 " }\n"
21026 "}",
21027 GNUBraceStyle);
21028
21029 verifyFormat("enum X\n"
21030 "{\n"
21031 " Y = 0,\n"
21032 "}",
21033 GNUBraceStyle);
21034
21035 verifyFormat("@interface BSApplicationController ()\n"
21036 "{\n"
21037 "@private\n"
21038 " id _extraIvar;\n"
21039 "}\n"
21040 "@end",
21041 GNUBraceStyle);
21042
21043 verifyFormat("#ifdef _DEBUG\n"
21044 "int foo(int i = 0)\n"
21045 "#else\n"
21046 "int foo(int i = 5)\n"
21047 "#endif\n"
21048 "{\n"
21049 " return i;\n"
21050 "}",
21051 GNUBraceStyle);
21052
21053 verifyFormat("void foo() {}\n"
21054 "void bar()\n"
21055 "#ifdef _DEBUG\n"
21056 "{\n"
21057 " foo();\n"
21058 "}\n"
21059 "#else\n"
21060 "{\n"
21061 "}\n"
21062 "#endif",
21063 GNUBraceStyle);
21064
21065 verifyFormat("void foobar() { int i = 5; }\n"
21066 "#ifdef _DEBUG\n"
21067 "void bar() {}\n"
21068 "#else\n"
21069 "void bar() { foobar(); }\n"
21070 "#endif",
21071 GNUBraceStyle);
21072}
21073
21074TEST_F(FormatTest, WebKitBraceBreaking) {
21075 FormatStyle WebKitBraceStyle = getLLVMStyle();
21076 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
21077 WebKitBraceStyle.FixNamespaceComments = false;
21078 verifyFormat("namespace a {\n"
21079 "class A {\n"
21080 " void f()\n"
21081 " {\n"
21082 " if (true) {\n"
21083 " a();\n"
21084 " b();\n"
21085 " }\n"
21086 " }\n"
21087 " void g() { return; }\n"
21088 "};\n"
21089 "enum E {\n"
21090 " A,\n"
21091 " // foo\n"
21092 " B,\n"
21093 " C\n"
21094 "};\n"
21095 "struct B {\n"
21096 " int x;\n"
21097 "};\n"
21098 "}",
21099 WebKitBraceStyle);
21100 verifyFormat("struct S {\n"
21101 " int Type;\n"
21102 " union {\n"
21103 " int x;\n"
21104 " double y;\n"
21105 " } Value;\n"
21106 " class C {\n"
21107 " MyFavoriteType Value;\n"
21108 " } Class;\n"
21109 "};",
21110 WebKitBraceStyle);
21111}
21112
21113TEST_F(FormatTest, CatchExceptionReferenceBinding) {
21114 verifyFormat("void f() {\n"
21115 " try {\n"
21116 " } catch (const Exception &e) {\n"
21117 " }\n"
21118 "}");
21119}
21120
21121TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
21122 auto Style = getLLVMStyle();
21123 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21124 verifyNoCrash(Code: "f({\n"
21125 "table({}, table({{\"\", false}}, {}))\n"
21126 "});",
21127 Style);
21128
21129 Style.AlignConsecutiveAssignments.Enabled = true;
21130 Style.AlignConsecutiveDeclarations.Enabled = true;
21131 verifyFormat("struct test demo[] = {\n"
21132 " {56, 23, \"hello\"},\n"
21133 " {-1, 93463, \"world\"},\n"
21134 " { 7, 5, \"!!\"}\n"
21135 "};",
21136 Style);
21137
21138 verifyFormat("struct test demo[] = {\n"
21139 " {56, 23, \"hello\"}, // first line\n"
21140 " {-1, 93463, \"world\"}, // second line\n"
21141 " { 7, 5, \"!!\"} // third line\n"
21142 "};",
21143 Style);
21144
21145 verifyFormat("struct test demo[4] = {\n"
21146 " { 56, 23, 21, \"oh\"}, // first line\n"
21147 " { -1, 93463, 22, \"my\"}, // second line\n"
21148 " { 7, 5, 1, \"goodness\"} // third line\n"
21149 " {234, 5, 1, \"gracious\"} // fourth line\n"
21150 "};",
21151 Style);
21152
21153 verifyFormat("struct test demo[3] = {\n"
21154 " {56, 23, \"hello\"},\n"
21155 " {-1, 93463, \"world\"},\n"
21156 " { 7, 5, \"!!\"}\n"
21157 "};",
21158 Style);
21159
21160 verifyFormat("struct test demo[3] = {\n"
21161 " {int{56}, 23, \"hello\"},\n"
21162 " {int{-1}, 93463, \"world\"},\n"
21163 " { int{7}, 5, \"!!\"}\n"
21164 "};",
21165 Style);
21166
21167 verifyFormat("struct test demo[] = {\n"
21168 " {56, 23, \"hello\"},\n"
21169 " {-1, 93463, \"world\"},\n"
21170 " { 7, 5, \"!!\"},\n"
21171 "};",
21172 Style);
21173
21174 verifyFormat("test demo[] = {\n"
21175 " {56, 23, \"hello\"},\n"
21176 " {-1, 93463, \"world\"},\n"
21177 " { 7, 5, \"!!\"},\n"
21178 "};",
21179 Style);
21180
21181 verifyFormat("demo = std::array<struct test, 3>{\n"
21182 " test{56, 23, \"hello\"},\n"
21183 " test{-1, 93463, \"world\"},\n"
21184 " test{ 7, 5, \"!!\"},\n"
21185 "};",
21186 Style);
21187
21188 verifyFormat("test demo[] = {\n"
21189 " {56, 23, \"hello\"},\n"
21190 "#if X\n"
21191 " {-1, 93463, \"world\"},\n"
21192 "#endif\n"
21193 " { 7, 5, \"!!\"}\n"
21194 "};",
21195 Style);
21196
21197 verifyFormat(
21198 "test demo[] = {\n"
21199 " { 7, 23,\n"
21200 " \"hello world i am a very long line that really, in any\"\n"
21201 " \"just world, ought to be split over multiple lines\"},\n"
21202 " {-1, 93463, \"world\"},\n"
21203 " {56, 5, \"!!\"}\n"
21204 "};",
21205 Style);
21206
21207 verifyNoCrash(Code: "Foo f[] = {\n"
21208 " [0] = { 1, },\n"
21209 " [i] { 1, },\n"
21210 "};",
21211 Style);
21212 verifyNoCrash(Code: "Foo foo[] = {\n"
21213 " [0] = {1, 1},\n"
21214 " [1] { 1, 1, },\n"
21215 " [2] { 1, 1, },\n"
21216 "};",
21217 Style);
21218 verifyNoCrash(Code: "test arr[] = {\n"
21219 "#define FOO(i) {i, i},\n"
21220 "SOME_GENERATOR(FOO)\n"
21221 "{2, 2}\n"
21222 "};",
21223 Style);
21224
21225 verifyFormat("return GradForUnaryCwise(g, {\n"
21226 " {{\"sign\"}, \"Sign\", "
21227 " {\"x\", \"dy\"}},\n"
21228 " { {\"dx\"}, \"Mul\", {\"dy\""
21229 ", \"sign\"}},\n"
21230 "});",
21231 Style);
21232
21233 Style.Cpp11BracedListStyle = false;
21234 verifyFormat("struct test demo[] = {\n"
21235 " { 56, 23, \"hello\" },\n"
21236 " { -1, 93463, \"world\" },\n"
21237 " { 7, 5, \"!!\" }\n"
21238 "};",
21239 Style);
21240 Style.Cpp11BracedListStyle = true;
21241
21242 Style.ColumnLimit = 0;
21243 verifyFormat(
21244 "test demo[] = {\n"
21245 " {56, 23, \"hello world i am a very long line that really, "
21246 "in any just world, ought to be split over multiple lines\"},\n"
21247 " {-1, 93463, "
21248 " \"world\"},\n"
21249 " { 7, 5, "
21250 " \"!!\"},\n"
21251 "};",
21252 "test demo[] = {{56, 23, \"hello world i am a very long line "
21253 "that really, in any just world, ought to be split over multiple "
21254 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21255 Style);
21256
21257 Style.ColumnLimit = 80;
21258 verifyFormat("test demo[] = {\n"
21259 " {56, 23, /* a comment */ \"hello\"},\n"
21260 " {-1, 93463, \"world\"},\n"
21261 " { 7, 5, \"!!\"}\n"
21262 "};",
21263 Style);
21264
21265 verifyFormat("test demo[] = {\n"
21266 " {56, 23, \"hello\"},\n"
21267 " {-1, 93463, \"world\" /* comment here */},\n"
21268 " { 7, 5, \"!!\"}\n"
21269 "};",
21270 Style);
21271
21272 verifyFormat("test demo[] = {\n"
21273 " {56, /* a comment */ 23, \"hello\"},\n"
21274 " {-1, 93463, \"world\"},\n"
21275 " { 7, 5, \"!!\"}\n"
21276 "};",
21277 Style);
21278
21279 Style.ColumnLimit = 20;
21280 verifyFormat("demo = std::array<\n"
21281 " struct test, 3>{\n"
21282 " test{\n"
21283 " 56, 23,\n"
21284 " \"hello \"\n"
21285 " \"world i \"\n"
21286 " \"am a very \"\n"
21287 " \"long line \"\n"
21288 " \"that \"\n"
21289 " \"really, \"\n"
21290 " \"in any \"\n"
21291 " \"just \"\n"
21292 " \"world, \"\n"
21293 " \"ought to \"\n"
21294 " \"be split \"\n"
21295 " \"over \"\n"
21296 " \"multiple \"\n"
21297 " \"lines\"},\n"
21298 " test{-1, 93463,\n"
21299 " \"world\"},\n"
21300 " test{ 7, 5,\n"
21301 " \"!!\" },\n"
21302 "};",
21303 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21304 "i am a very long line that really, in any just world, ought "
21305 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21306 "test{7, 5, \"!!\"},};",
21307 Style);
21308 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21309 Style = getLLVMStyleWithColumns(ColumnLimit: 50);
21310 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21311 verifyFormat("static A x = {\n"
21312 " {{init1, init2, init3, init4},\n"
21313 " {init1, init2, init3, init4}}\n"
21314 "};",
21315 Style);
21316 // TODO: Fix the indentations below when this option is fully functional.
21317#if 0
21318 verifyFormat("int a[][] = {\n"
21319 " {\n"
21320 " {0, 2}, //\n"
21321 " {1, 2} //\n"
21322 " }\n"
21323 "};",
21324 Style);
21325#endif
21326 Style.ColumnLimit = 100;
21327 verifyFormat(
21328 "test demo[] = {\n"
21329 " {56, 23,\n"
21330 " \"hello world i am a very long line that really, in any just world"
21331 ", ought to be split over \"\n"
21332 " \"multiple lines\" },\n"
21333 " {-1, 93463, \"world\"},\n"
21334 " { 7, 5, \"!!\"},\n"
21335 "};",
21336 "test demo[] = {{56, 23, \"hello world i am a very long line "
21337 "that really, in any just world, ought to be split over multiple "
21338 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21339 Style);
21340
21341 Style = getLLVMStyleWithColumns(ColumnLimit: 50);
21342 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
21343 verifyFormat("struct test demo[] = {\n"
21344 " {56, 23, \"hello\"},\n"
21345 " {-1, 93463, \"world\"},\n"
21346 " { 7, 5, \"!!\"}\n"
21347 "};\n"
21348 "static A x = {\n"
21349 " {{init1, init2, init3, init4},\n"
21350 " {init1, init2, init3, init4}}\n"
21351 "};",
21352 Style);
21353 Style.ColumnLimit = 100;
21354 Style.AlignConsecutiveAssignments.AcrossComments = true;
21355 Style.AlignConsecutiveDeclarations.AcrossComments = true;
21356 verifyFormat("struct test demo[] = {\n"
21357 " {56, 23, \"hello\"},\n"
21358 " {-1, 93463, \"world\"},\n"
21359 " { 7, 5, \"!!\"}\n"
21360 "};\n"
21361 "struct test demo[4] = {\n"
21362 " { 56, 23, 21, \"oh\"}, // first line\n"
21363 " { -1, 93463, 22, \"my\"}, // second line\n"
21364 " { 7, 5, 1, \"goodness\"} // third line\n"
21365 " {234, 5, 1, \"gracious\"} // fourth line\n"
21366 "};",
21367 Style);
21368 verifyFormat(
21369 "test demo[] = {\n"
21370 " {56,\n"
21371 " \"hello world i am a very long line that really, in any just world"
21372 ", ought to be split over \"\n"
21373 " \"multiple lines\", 23},\n"
21374 " {-1, \"world\", 93463},\n"
21375 " { 7, \"!!\", 5},\n"
21376 "};",
21377 "test demo[] = {{56, \"hello world i am a very long line "
21378 "that really, in any just world, ought to be split over multiple "
21379 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21380 Style);
21381}
21382
21383TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
21384 auto Style = getLLVMStyle();
21385 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21386 /* FIXME: This case gets misformatted.
21387 verifyFormat("auto foo = Items{\n"
21388 " Section{0, bar(), },\n"
21389 " Section{1, boo() }\n"
21390 "};",
21391 Style);
21392 */
21393 verifyFormat("auto foo = Items{\n"
21394 " Section{\n"
21395 " 0, bar(),\n"
21396 " }\n"
21397 "};",
21398 Style);
21399 verifyFormat("struct test demo[] = {\n"
21400 " {56, 23, \"hello\"},\n"
21401 " {-1, 93463, \"world\"},\n"
21402 " {7, 5, \"!!\" }\n"
21403 "};",
21404 Style);
21405 verifyFormat("struct test demo[] = {\n"
21406 " {56, 23, \"hello\"}, // first line\n"
21407 " {-1, 93463, \"world\"}, // second line\n"
21408 " {7, 5, \"!!\" } // third line\n"
21409 "};",
21410 Style);
21411 verifyFormat("struct test demo[4] = {\n"
21412 " {56, 23, 21, \"oh\" }, // first line\n"
21413 " {-1, 93463, 22, \"my\" }, // second line\n"
21414 " {7, 5, 1, \"goodness\"} // third line\n"
21415 " {234, 5, 1, \"gracious\"} // fourth line\n"
21416 "};",
21417 Style);
21418 verifyFormat("struct test demo[3] = {\n"
21419 " {56, 23, \"hello\"},\n"
21420 " {-1, 93463, \"world\"},\n"
21421 " {7, 5, \"!!\" }\n"
21422 "};",
21423 Style);
21424
21425 verifyFormat("struct test demo[3] = {\n"
21426 " {int{56}, 23, \"hello\"},\n"
21427 " {int{-1}, 93463, \"world\"},\n"
21428 " {int{7}, 5, \"!!\" }\n"
21429 "};",
21430 Style);
21431 verifyFormat("struct test demo[] = {\n"
21432 " {56, 23, \"hello\"},\n"
21433 " {-1, 93463, \"world\"},\n"
21434 " {7, 5, \"!!\" },\n"
21435 "};",
21436 Style);
21437 verifyFormat("test demo[] = {\n"
21438 " {56, 23, \"hello\"},\n"
21439 " {-1, 93463, \"world\"},\n"
21440 " {7, 5, \"!!\" },\n"
21441 "};",
21442 Style);
21443 verifyFormat("demo = std::array<struct test, 3>{\n"
21444 " test{56, 23, \"hello\"},\n"
21445 " test{-1, 93463, \"world\"},\n"
21446 " test{7, 5, \"!!\" },\n"
21447 "};",
21448 Style);
21449 verifyFormat("test demo[] = {\n"
21450 " {56, 23, \"hello\"},\n"
21451 "#if X\n"
21452 " {-1, 93463, \"world\"},\n"
21453 "#endif\n"
21454 " {7, 5, \"!!\" }\n"
21455 "};",
21456 Style);
21457 verifyFormat(
21458 "test demo[] = {\n"
21459 " {7, 23,\n"
21460 " \"hello world i am a very long line that really, in any\"\n"
21461 " \"just world, ought to be split over multiple lines\"},\n"
21462 " {-1, 93463, \"world\" },\n"
21463 " {56, 5, \"!!\" }\n"
21464 "};",
21465 Style);
21466
21467 verifyNoCrash(Code: "Foo f[] = {\n"
21468 " [0] = { 1, },\n"
21469 " [i] { 1, },\n"
21470 "};",
21471 Style);
21472 verifyNoCrash(Code: "Foo foo[] = {\n"
21473 " [0] = {1, 1},\n"
21474 " [1] { 1, 1, },\n"
21475 " [2] { 1, 1, },\n"
21476 "};",
21477 Style);
21478 verifyNoCrash(Code: "test arr[] = {\n"
21479 "#define FOO(i) {i, i},\n"
21480 "SOME_GENERATOR(FOO)\n"
21481 "{2, 2}\n"
21482 "};",
21483 Style);
21484
21485 verifyFormat("return GradForUnaryCwise(g, {\n"
21486 " {{\"sign\"}, \"Sign\", {\"x\", "
21487 "\"dy\"} },\n"
21488 " {{\"dx\"}, \"Mul\", "
21489 "{\"dy\", \"sign\"}},\n"
21490 "});",
21491 Style);
21492
21493 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
21494 verifyFormat("#define FOO \\\n"
21495 " int foo[][2] = { \\\n"
21496 " {0, 1} \\\n"
21497 " };",
21498 Style);
21499
21500 Style.Cpp11BracedListStyle = false;
21501 verifyFormat("struct test demo[] = {\n"
21502 " { 56, 23, \"hello\" },\n"
21503 " { -1, 93463, \"world\" },\n"
21504 " { 7, 5, \"!!\" }\n"
21505 "};",
21506 Style);
21507 Style.Cpp11BracedListStyle = true;
21508
21509 Style.ColumnLimit = 0;
21510 verifyFormat(
21511 "test demo[] = {\n"
21512 " {56, 23, \"hello world i am a very long line that really, in any "
21513 "just world, ought to be split over multiple lines\"},\n"
21514 " {-1, 93463, \"world\" "
21515 " },\n"
21516 " {7, 5, \"!!\" "
21517 " },\n"
21518 "};",
21519 "test demo[] = {{56, 23, \"hello world i am a very long line "
21520 "that really, in any just world, ought to be split over multiple "
21521 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21522 Style);
21523
21524 Style.ColumnLimit = 80;
21525 verifyFormat("test demo[] = {\n"
21526 " {56, 23, /* a comment */ \"hello\"},\n"
21527 " {-1, 93463, \"world\" },\n"
21528 " {7, 5, \"!!\" }\n"
21529 "};",
21530 Style);
21531
21532 verifyFormat("test demo[] = {\n"
21533 " {56, 23, \"hello\" },\n"
21534 " {-1, 93463, \"world\" /* comment here */},\n"
21535 " {7, 5, \"!!\" }\n"
21536 "};",
21537 Style);
21538
21539 verifyFormat("test demo[] = {\n"
21540 " {56, /* a comment */ 23, \"hello\"},\n"
21541 " {-1, 93463, \"world\"},\n"
21542 " {7, 5, \"!!\" }\n"
21543 "};",
21544 Style);
21545 verifyFormat("Foo foo = {\n"
21546 " // comment\n"
21547 " {1, 2}\n"
21548 "};",
21549 Style);
21550
21551 Style.ColumnLimit = 20;
21552 // FIXME: unstable test case
21553 EXPECT_EQ(
21554 "demo = std::array<\n"
21555 " struct test, 3>{\n"
21556 " test{\n"
21557 " 56, 23,\n"
21558 " \"hello \"\n"
21559 " \"world i \"\n"
21560 " \"am a very \"\n"
21561 " \"long line \"\n"
21562 " \"that \"\n"
21563 " \"really, \"\n"
21564 " \"in any \"\n"
21565 " \"just \"\n"
21566 " \"world, \"\n"
21567 " \"ought to \"\n"
21568 " \"be split \"\n"
21569 " \"over \"\n"
21570 " \"multiple \"\n"
21571 " \"lines\"},\n"
21572 " test{-1, 93463,\n"
21573 " \"world\"},\n"
21574 " test{7, 5,\n"
21575 " \"!!\" },\n"
21576 "};",
21577 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21578 "i am a very long line that really, in any just world, ought "
21579 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21580 "test{7, 5, \"!!\"},};",
21581 Style));
21582
21583 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21584 Style = getLLVMStyleWithColumns(ColumnLimit: 50);
21585 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
21586 verifyFormat("static A x = {\n"
21587 " {{init1, init2, init3, init4},\n"
21588 " {init1, init2, init3, init4}}\n"
21589 "};",
21590 Style);
21591 Style.ColumnLimit = 100;
21592 verifyFormat(
21593 "test demo[] = {\n"
21594 " {56, 23,\n"
21595 " \"hello world i am a very long line that really, in any just world"
21596 ", ought to be split over \"\n"
21597 " \"multiple lines\" },\n"
21598 " {-1, 93463, \"world\"},\n"
21599 " {7, 5, \"!!\" },\n"
21600 "};",
21601 "test demo[] = {{56, 23, \"hello world i am a very long line "
21602 "that really, in any just world, ought to be split over multiple "
21603 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21604 Style);
21605
21606 Style.ColumnLimit = 25;
21607 verifyNoCrash(Code: "Type foo{\n"
21608 " {\n"
21609 " 1, // A\n"
21610 " 2, // B\n"
21611 " 3, // C\n"
21612 " },\n"
21613 " \"hello\",\n"
21614 "};",
21615 Style);
21616 verifyNoCrash(Code: "Type object[X][Y] = {\n"
21617 " {{val}, {val}, {val}},\n"
21618 " {{val}, {val}, // some comment\n"
21619 " {val}}\n"
21620 "};",
21621 Style);
21622
21623 Style.ColumnLimit = 120;
21624 verifyNoCrash(
21625 Code: "T v[] {\n"
21626 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21627 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21628 "\"00000000000000000000000000000000000000000000000000000000"
21629 "00000000000000000000000000000000000000000000000000000000\" },\n"
21630 "};",
21631 Style);
21632
21633 Style.SpacesInParens = FormatStyle::SIPO_Custom;
21634 Style.SpacesInParensOptions.Other = true;
21635 verifyFormat("Foo foo[] = {\n"
21636 " {1, 1},\n"
21637 " {1, 1},\n"
21638 "};",
21639 Style);
21640}
21641
21642TEST_F(FormatTest, UnderstandsPragmas) {
21643 verifyFormat("#pragma omp reduction(| : var)");
21644 verifyFormat("#pragma omp reduction(+ : var)");
21645
21646 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21647 "(including parentheses).",
21648 "#pragma mark Any non-hyphenated or hyphenated string "
21649 "(including parentheses).");
21650
21651 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21652 "(including parentheses).",
21653 "#pragma mark Any non-hyphenated or hyphenated string "
21654 "(including parentheses).");
21655
21656 verifyFormat("#pragma comment(linker, \\\n"
21657 " \"argument\" \\\n"
21658 " \"argument\"",
21659 "#pragma comment(linker, \\\n"
21660 " \"argument\" \\\n"
21661 " \"argument\"",
21662 getStyleWithColumns(
21663 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32));
21664}
21665
21666TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
21667 verifyFormat("#pragma omp target map(to : var)");
21668 verifyFormat("#pragma omp target map(to : var[ : N])");
21669 verifyFormat("#pragma omp target map(to : var[0 : N])");
21670 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
21671
21672 verifyFormat(
21673 "#pragma omp target \\\n"
21674 " reduction(+ : var) \\\n"
21675 " map(to : A[0 : N]) \\\n"
21676 " map(to : B[0 : N]) \\\n"
21677 " map(from : C[0 : N]) \\\n"
21678 " firstprivate(i) \\\n"
21679 " firstprivate(j) \\\n"
21680 " firstprivate(k)",
21681 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
21682 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
21683 getLLVMStyleWithColumns(26));
21684}
21685
21686TEST_F(FormatTest, UnderstandPragmaOption) {
21687 verifyFormat("#pragma option -C -A");
21688
21689 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
21690}
21691
21692TEST_F(FormatTest, UnderstandPragmaRegion) {
21693 auto Style = getLLVMStyleWithColumns(ColumnLimit: 0);
21694 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
21695 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
21696}
21697
21698TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
21699 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20);
21700
21701 // See PR41213
21702 verifyFormat("/*\n"
21703 " *\t9012345\n"
21704 " * /8901\n"
21705 " */",
21706 "/*\n"
21707 " *\t9012345 /8901\n"
21708 " */",
21709 Style);
21710 verifyFormat("/*\n"
21711 " *345678\n"
21712 " *\t/8901\n"
21713 " */",
21714 "/*\n"
21715 " *345678\t/8901\n"
21716 " */",
21717 Style);
21718
21719 verifyFormat("int a; // the\n"
21720 " // comment",
21721 Style);
21722 verifyNoChange("int a; /* first line\n"
21723 " * second\n"
21724 " * line third\n"
21725 " * line\n"
21726 " */",
21727 Style);
21728 verifyFormat("int a; // first line\n"
21729 " // second\n"
21730 " // line third\n"
21731 " // line",
21732 "int a; // first line\n"
21733 " // second line\n"
21734 " // third line",
21735 Style);
21736
21737 Style.PenaltyExcessCharacter = 90;
21738 verifyFormat("int a; // the comment", Style);
21739 verifyFormat("int a; // the comment\n"
21740 " // aaa",
21741 "int a; // the comment aaa", Style);
21742 verifyNoChange("int a; /* first line\n"
21743 " * second line\n"
21744 " * third line\n"
21745 " */",
21746 Style);
21747 verifyFormat("int a; // first line\n"
21748 " // second line\n"
21749 " // third line",
21750 Style);
21751 // FIXME: Investigate why this is not getting the same layout as the test
21752 // above.
21753 verifyFormat("int a; /* first line\n"
21754 " * second line\n"
21755 " * third line\n"
21756 " */",
21757 "int a; /* first line second line third line"
21758 "\n*/",
21759 Style);
21760
21761 verifyFormat("// foo bar baz bazfoo\n"
21762 "// foo bar foo bar",
21763 "// foo bar baz bazfoo\n"
21764 "// foo bar foo bar",
21765 Style);
21766 verifyFormat("// foo bar baz bazfoo\n"
21767 "// foo bar foo bar",
21768 "// foo bar baz bazfoo\n"
21769 "// foo bar foo bar",
21770 Style);
21771
21772 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
21773 // next one.
21774 verifyFormat("// foo bar baz bazfoo\n"
21775 "// bar foo bar",
21776 "// foo bar baz bazfoo bar\n"
21777 "// foo bar",
21778 Style);
21779
21780 // FIXME: unstable test case
21781 EXPECT_EQ("// foo bar baz bazfoo\n"
21782 "// foo bar baz bazfoo\n"
21783 "// bar foo bar",
21784 format("// foo bar baz bazfoo\n"
21785 "// foo bar baz bazfoo bar\n"
21786 "// foo bar",
21787 Style));
21788
21789 // FIXME: unstable test case
21790 EXPECT_EQ("// foo bar baz bazfoo\n"
21791 "// foo bar baz bazfoo\n"
21792 "// bar foo bar",
21793 format("// foo bar baz bazfoo\n"
21794 "// foo bar baz bazfoo bar\n"
21795 "// foo bar",
21796 Style));
21797
21798 // Make sure we do not keep protruding characters if strict mode reflow is
21799 // cheaper than keeping protruding characters.
21800 Style.ColumnLimit = 21;
21801 verifyFormat("// foo foo foo foo\n"
21802 "// foo foo foo foo\n"
21803 "// foo foo foo foo",
21804 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
21805
21806 verifyFormat("int a = /* long block\n"
21807 " comment */\n"
21808 " 42;",
21809 "int a = /* long block comment */ 42;", Style);
21810}
21811
21812TEST_F(FormatTest, BreakPenaltyAfterLParen) {
21813 FormatStyle Style = getLLVMStyle();
21814 Style.ColumnLimit = 8;
21815 Style.PenaltyExcessCharacter = 15;
21816 verifyFormat("int foo(\n"
21817 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
21818 Style);
21819 Style.PenaltyBreakOpenParenthesis = 200;
21820 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
21821 "int foo(\n"
21822 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
21823 Style);
21824}
21825
21826TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
21827 FormatStyle Style = getLLVMStyle();
21828 Style.ColumnLimit = 5;
21829 Style.PenaltyExcessCharacter = 150;
21830 verifyFormat("foo((\n"
21831 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
21832
21833 Style);
21834 Style.PenaltyBreakOpenParenthesis = 100'000;
21835 verifyFormat("foo((int)\n"
21836 " aaaaaaaaaaaaaaaaaaaaaaaa);",
21837 "foo((\n"
21838 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
21839 Style);
21840}
21841
21842TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
21843 FormatStyle Style = getLLVMStyle();
21844 Style.ColumnLimit = 4;
21845 Style.PenaltyExcessCharacter = 100;
21846 verifyFormat("for (\n"
21847 " int iiiiiiiiiiiiiiiii =\n"
21848 " 0;\n"
21849 " iiiiiiiiiiiiiiiii <\n"
21850 " 2;\n"
21851 " iiiiiiiiiiiiiiiii++) {\n"
21852 "}",
21853
21854 Style);
21855 Style.PenaltyBreakOpenParenthesis = 1250;
21856 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
21857 " 0;\n"
21858 " iiiiiiiiiiiiiiiii <\n"
21859 " 2;\n"
21860 " iiiiiiiiiiiiiiiii++) {\n"
21861 "}",
21862 "for (\n"
21863 " int iiiiiiiiiiiiiiiii =\n"
21864 " 0;\n"
21865 " iiiiiiiiiiiiiiiii <\n"
21866 " 2;\n"
21867 " iiiiiiiiiiiiiiiii++) {\n"
21868 "}",
21869 Style);
21870}
21871
21872TEST_F(FormatTest, BreakPenaltyScopeResolution) {
21873 FormatStyle Style = getLLVMStyle();
21874 Style.ColumnLimit = 20;
21875 Style.PenaltyExcessCharacter = 100;
21876 verifyFormat("unsigned long\n"
21877 "foo::bar();",
21878 Style);
21879 Style.PenaltyBreakScopeResolution = 10;
21880 verifyFormat("unsigned long foo::\n"
21881 " bar();",
21882 Style);
21883}
21884
21885TEST_F(FormatTest, WorksFor8bitEncodings) {
21886 // FIXME: unstable test case
21887 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
21888 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
21889 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
21890 "\"\xef\xee\xf0\xf3...\"",
21891 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
21892 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
21893 "\xef\xee\xf0\xf3...\"",
21894 getLLVMStyleWithColumns(12)));
21895}
21896
21897TEST_F(FormatTest, HandlesUTF8BOM) {
21898 verifyFormat("\xef\xbb\xbf");
21899 verifyFormat("\xef\xbb\xbf#include <iostream>");
21900 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
21901}
21902
21903// FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
21904#if !defined(_MSC_VER)
21905
21906TEST_F(FormatTest, CountsUTF8CharactersProperly) {
21907 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
21908 getLLVMStyleWithColumns(35));
21909 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
21910 getLLVMStyleWithColumns(31));
21911 verifyFormat("// Однажды в студёную зимнюю пору...",
21912 getLLVMStyleWithColumns(36));
21913 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
21914 verifyFormat("/* Однажды в студёную зимнюю пору... */",
21915 getLLVMStyleWithColumns(39));
21916 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
21917 getLLVMStyleWithColumns(35));
21918}
21919
21920TEST_F(FormatTest, SplitsUTF8Strings) {
21921 // Non-printable characters' width is currently considered to be the length in
21922 // bytes in UTF8. The characters can be displayed in very different manner
21923 // (zero-width, single width with a substitution glyph, expanded to their code
21924 // (e.g. "<8d>"), so there's no single correct way to handle them.
21925 // FIXME: unstable test case
21926 EXPECT_EQ("\"aaaaÄ\"\n"
21927 "\"\xc2\x8d\";",
21928 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
21929 // FIXME: unstable test case
21930 EXPECT_EQ("\"aaaaaaaÄ\"\n"
21931 "\"\xc2\x8d\";",
21932 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
21933 // FIXME: unstable test case
21934 EXPECT_EQ("\"Однажды, в \"\n"
21935 "\"студёную \"\n"
21936 "\"зимнюю \"\n"
21937 "\"пору,\"",
21938 format("\"Однажды, в студёную зимнюю пору,\"",
21939 getLLVMStyleWithColumns(13)));
21940 // FIXME: unstable test case
21941 EXPECT_EQ(
21942 "\"一 二 三 \"\n"
21943 "\"四 五六 \"\n"
21944 "\"七 八 九 \"\n"
21945 "\"十\"",
21946 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
21947 // FIXME: unstable test case
21948 EXPECT_EQ("\"一\t\"\n"
21949 "\"二 \t\"\n"
21950 "\"三 四 \"\n"
21951 "\"五\t\"\n"
21952 "\"六 \t\"\n"
21953 "\"七 \"\n"
21954 "\"八九十\tqq\"",
21955 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
21956 getLLVMStyleWithColumns(11)));
21957
21958 // UTF8 character in an escape sequence.
21959 // FIXME: unstable test case
21960 EXPECT_EQ("\"aaaaaa\"\n"
21961 "\"\\\xC2\x8D\"",
21962 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
21963}
21964
21965TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
21966 verifyFormat("const char *sssss =\n"
21967 " \"一二三四五六七八\\\n"
21968 " 九 十\";",
21969 "const char *sssss = \"一二三四五六七八\\\n"
21970 " 九 十\";",
21971 getLLVMStyleWithColumns(30));
21972}
21973
21974TEST_F(FormatTest, SplitsUTF8LineComments) {
21975 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
21976 verifyFormat("// Я из лесу\n"
21977 "// вышел; был\n"
21978 "// сильный\n"
21979 "// мороз.",
21980 "// Я из лесу вышел; был сильный мороз.",
21981 getLLVMStyleWithColumns(13));
21982 verifyFormat("// 一二三\n"
21983 "// 四五六七\n"
21984 "// 八 九\n"
21985 "// 十",
21986 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
21987}
21988
21989TEST_F(FormatTest, SplitsUTF8BlockComments) {
21990 verifyFormat("/* Гляжу,\n"
21991 " * поднимается\n"
21992 " * медленно в\n"
21993 " * гору\n"
21994 " * Лошадка,\n"
21995 " * везущая\n"
21996 " * хворосту\n"
21997 " * воз. */",
21998 "/* Гляжу, поднимается медленно в гору\n"
21999 " * Лошадка, везущая хворосту воз. */",
22000 getLLVMStyleWithColumns(13));
22001 verifyFormat("/* 一二三\n"
22002 " * 四五六七\n"
22003 " * 八 九\n"
22004 " * 十 */",
22005 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22006 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22007 " * 𝕓𝕪𝕥𝕖\n"
22008 " * 𝖀𝕿𝕱-𝟠 */",
22009 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22010}
22011
22012#endif // _MSC_VER
22013
22014TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22015 FormatStyle Style = getLLVMStyle();
22016
22017 Style.ConstructorInitializerIndentWidth = 4;
22018 verifyFormat(
22019 "SomeClass::Constructor()\n"
22020 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22021 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22022 Style);
22023
22024 Style.ConstructorInitializerIndentWidth = 2;
22025 verifyFormat(
22026 "SomeClass::Constructor()\n"
22027 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22028 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22029 Style);
22030
22031 Style.ConstructorInitializerIndentWidth = 0;
22032 verifyFormat(
22033 "SomeClass::Constructor()\n"
22034 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22035 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22036 Style);
22037 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
22038 verifyFormat(
22039 "SomeLongTemplateVariableName<\n"
22040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22041 Style);
22042 verifyFormat("bool smaller = 1 < "
22043 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22044 " "
22045 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22046 Style);
22047
22048 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22049 verifyFormat("SomeClass::Constructor() :\n"
22050 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22051 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22052 Style);
22053}
22054
22055TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
22056 FormatStyle Style = getLLVMStyle();
22057 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22058 Style.ConstructorInitializerIndentWidth = 4;
22059 verifyFormat("SomeClass::Constructor()\n"
22060 " : a(a)\n"
22061 " , b(b)\n"
22062 " , c(c) {}",
22063 Style);
22064 verifyFormat("SomeClass::Constructor()\n"
22065 " : a(a) {}",
22066 Style);
22067
22068 Style.ColumnLimit = 0;
22069 verifyFormat("SomeClass::Constructor()\n"
22070 " : a(a) {}",
22071 Style);
22072 verifyFormat("SomeClass::Constructor() noexcept\n"
22073 " : a(a) {}",
22074 Style);
22075 verifyFormat("SomeClass::Constructor()\n"
22076 " : a(a)\n"
22077 " , b(b)\n"
22078 " , c(c) {}",
22079 Style);
22080 verifyFormat("SomeClass::Constructor()\n"
22081 " : a(a) {\n"
22082 " foo();\n"
22083 " bar();\n"
22084 "}",
22085 Style);
22086
22087 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
22088 verifyFormat("SomeClass::Constructor()\n"
22089 " : a(a)\n"
22090 " , b(b)\n"
22091 " , c(c) {\n}",
22092 Style);
22093 verifyFormat("SomeClass::Constructor()\n"
22094 " : a(a) {\n}",
22095 Style);
22096
22097 Style.ColumnLimit = 80;
22098 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
22099 Style.ConstructorInitializerIndentWidth = 2;
22100 verifyFormat("SomeClass::Constructor()\n"
22101 " : a(a)\n"
22102 " , b(b)\n"
22103 " , c(c) {}",
22104 Style);
22105
22106 Style.ConstructorInitializerIndentWidth = 0;
22107 verifyFormat("SomeClass::Constructor()\n"
22108 ": a(a)\n"
22109 ", b(b)\n"
22110 ", c(c) {}",
22111 Style);
22112
22113 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22114 Style.ConstructorInitializerIndentWidth = 4;
22115 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
22116 verifyFormat(
22117 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22118 Style);
22119 verifyFormat(
22120 "SomeClass::Constructor()\n"
22121 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22122 Style);
22123 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22124 verifyFormat("SomeClass::Constructor()\n"
22125 " : aaaaaaaa(aaaaaaaa) {}",
22126 Style);
22127 verifyFormat("SomeClass::Constructor()\n"
22128 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22129 Style);
22130 verifyFormat(
22131 "SomeClass::Constructor()\n"
22132 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22133 Style);
22134
22135 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
22136 Style.ConstructorInitializerIndentWidth = 4;
22137 Style.ColumnLimit = 60;
22138 verifyFormat("SomeClass::Constructor()\n"
22139 " : aaaaaaaa(aaaaaaaa)\n"
22140 " , aaaaaaaa(aaaaaaaa)\n"
22141 " , aaaaaaaa(aaaaaaaa) {}",
22142 Style);
22143 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
22144 verifyFormat("SomeClass::Constructor()\n"
22145 " : aaaaaaaa(aaaaaaaa)\n"
22146 " , aaaaaaaa(aaaaaaaa)\n"
22147 " , aaaaaaaa(aaaaaaaa) {}",
22148 Style);
22149}
22150
22151TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
22152 FormatStyle Style = getLLVMStyle();
22153 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
22154 Style.ConstructorInitializerIndentWidth = 4;
22155 verifyFormat("SomeClass::Constructor()\n"
22156 " : a{a}\n"
22157 " , b{b} {}",
22158 Style);
22159 verifyFormat("SomeClass::Constructor()\n"
22160 " : a{a}\n"
22161 "#if CONDITION\n"
22162 " , b{b}\n"
22163 "#endif\n"
22164 "{\n}",
22165 Style);
22166 Style.ConstructorInitializerIndentWidth = 2;
22167 verifyFormat("SomeClass::Constructor()\n"
22168 "#if CONDITION\n"
22169 " : a{a}\n"
22170 "#endif\n"
22171 " , b{b}\n"
22172 " , c{c} {\n}",
22173 Style);
22174 Style.ConstructorInitializerIndentWidth = 0;
22175 verifyFormat("SomeClass::Constructor()\n"
22176 ": a{a}\n"
22177 "#ifdef CONDITION\n"
22178 ", b{b}\n"
22179 "#else\n"
22180 ", c{c}\n"
22181 "#endif\n"
22182 ", d{d} {\n}",
22183 Style);
22184 Style.ConstructorInitializerIndentWidth = 4;
22185 verifyFormat("SomeClass::Constructor()\n"
22186 " : a{a}\n"
22187 "#if WINDOWS\n"
22188 "#if DEBUG\n"
22189 " , b{0}\n"
22190 "#else\n"
22191 " , b{1}\n"
22192 "#endif\n"
22193 "#else\n"
22194 "#if DEBUG\n"
22195 " , b{2}\n"
22196 "#else\n"
22197 " , b{3}\n"
22198 "#endif\n"
22199 "#endif\n"
22200 "{\n}",
22201 Style);
22202 verifyFormat("SomeClass::Constructor()\n"
22203 " : a{a}\n"
22204 "#if WINDOWS\n"
22205 " , b{0}\n"
22206 "#if DEBUG\n"
22207 " , c{0}\n"
22208 "#else\n"
22209 " , c{1}\n"
22210 "#endif\n"
22211 "#else\n"
22212 "#if DEBUG\n"
22213 " , c{2}\n"
22214 "#else\n"
22215 " , c{3}\n"
22216 "#endif\n"
22217 " , b{1}\n"
22218 "#endif\n"
22219 "{\n}",
22220 Style);
22221}
22222
22223TEST_F(FormatTest, Destructors) {
22224 verifyFormat("void F(int &i) { i.~int(); }");
22225 verifyFormat("void F(int &i) { i->~int(); }");
22226}
22227
22228TEST_F(FormatTest, FormatsWithWebKitStyle) {
22229 FormatStyle Style = getWebKitStyle();
22230
22231 // Don't indent in outer namespaces.
22232 verifyFormat("namespace outer {\n"
22233 "int i;\n"
22234 "namespace inner {\n"
22235 " int i;\n"
22236 "} // namespace inner\n"
22237 "} // namespace outer\n"
22238 "namespace other_outer {\n"
22239 "int i;\n"
22240 "}",
22241 Style);
22242
22243 // Don't indent case labels.
22244 verifyFormat("switch (variable) {\n"
22245 "case 1:\n"
22246 "case 2:\n"
22247 " doSomething();\n"
22248 " break;\n"
22249 "default:\n"
22250 " ++variable;\n"
22251 "}",
22252 Style);
22253
22254 // Wrap before binary operators.
22255 verifyFormat(
22256 "void f()\n"
22257 "{\n"
22258 " if (aaaaaaaaaaaaaaaa\n"
22259 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22260 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22261 " return;\n"
22262 "}",
22263 "void f() {\n"
22264 "if (aaaaaaaaaaaaaaaa\n"
22265 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22266 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22267 "return;\n"
22268 "}",
22269 Style);
22270
22271 // Allow functions on a single line.
22272 verifyFormat("void f() { return; }", Style);
22273
22274 // Allow empty blocks on a single line and insert a space in empty blocks.
22275 verifyFormat("void f() { }", "void f() {}", Style);
22276 verifyFormat("while (true) { }", "while (true) {}", Style);
22277 // However, don't merge non-empty short loops.
22278 verifyFormat("while (true) {\n"
22279 " continue;\n"
22280 "}",
22281 "while (true) { continue; }", Style);
22282
22283 // Constructor initializers are formatted one per line with the "," on the
22284 // new line.
22285 verifyFormat("Constructor()\n"
22286 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22287 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22288 " aaaaaaaaaaaaaa)\n"
22289 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22290 "{\n"
22291 "}",
22292 Style);
22293 verifyFormat("SomeClass::Constructor()\n"
22294 " : a(a)\n"
22295 "{\n"
22296 "}",
22297 Style);
22298 verifyFormat("SomeClass::Constructor()\n"
22299 " : a(a)\n"
22300 "{\n"
22301 "}",
22302 "SomeClass::Constructor():a(a){}", Style);
22303 verifyFormat("SomeClass::Constructor()\n"
22304 " : a(a)\n"
22305 " , b(b)\n"
22306 " , c(c)\n"
22307 "{\n"
22308 "}",
22309 Style);
22310 verifyFormat("SomeClass::Constructor()\n"
22311 " : a(a)\n"
22312 "{\n"
22313 " foo();\n"
22314 " bar();\n"
22315 "}",
22316 Style);
22317
22318 // Access specifiers should be aligned left.
22319 verifyFormat("class C {\n"
22320 "public:\n"
22321 " int i;\n"
22322 "};",
22323 Style);
22324
22325 // Do not align comments.
22326 verifyFormat("int a; // Do not\n"
22327 "double b; // align comments.",
22328 Style);
22329
22330 // Do not align operands.
22331 verifyFormat("ASSERT(aaaa\n"
22332 " || bbbb);",
22333 "ASSERT ( aaaa\n||bbbb);", Style);
22334
22335 // Accept input's line breaks.
22336 verifyFormat("if (aaaaaaaaaaaaaaa\n"
22337 " || bbbbbbbbbbbbbbb) {\n"
22338 " i++;\n"
22339 "}",
22340 "if (aaaaaaaaaaaaaaa\n"
22341 "|| bbbbbbbbbbbbbbb) { i++; }",
22342 Style);
22343 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22344 " i++;\n"
22345 "}",
22346 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
22347
22348 // Don't automatically break all macro definitions (llvm.org/PR17842).
22349 verifyFormat("#define aNumber 10", Style);
22350 // However, generally keep the line breaks that the user authored.
22351 verifyFormat("#define aNumber \\\n"
22352 " 10",
22353 "#define aNumber \\\n"
22354 " 10",
22355 Style);
22356
22357 // Keep empty and one-element array literals on a single line.
22358 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22359 " copyItems:YES];",
22360 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22361 "copyItems:YES];",
22362 Style);
22363 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22364 " copyItems:YES];",
22365 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22366 " copyItems:YES];",
22367 Style);
22368 // FIXME: This does not seem right, there should be more indentation before
22369 // the array literal's entries. Nested blocks have the same problem.
22370 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22371 " @\"a\",\n"
22372 " @\"a\"\n"
22373 "]\n"
22374 " copyItems:YES];",
22375 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22376 " @\"a\",\n"
22377 " @\"a\"\n"
22378 " ]\n"
22379 " copyItems:YES];",
22380 Style);
22381 verifyFormat(
22382 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22383 " copyItems:YES];",
22384 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22385 " copyItems:YES];",
22386 Style);
22387
22388 verifyFormat("[self.a b:c c:d];", Style);
22389 verifyFormat("[self.a b:c\n"
22390 " c:d];",
22391 "[self.a b:c\n"
22392 "c:d];",
22393 Style);
22394}
22395
22396TEST_F(FormatTest, FormatsLambdas) {
22397 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22398 verifyFormat(
22399 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22400 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22401 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22402 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22403 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22404 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22405 verifyFormat("auto c = [a = [b = 42] {}] {};");
22406 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22407 verifyFormat("int x = f(*+[] {});");
22408 verifyFormat("void f() {\n"
22409 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22410 "}");
22411 verifyFormat("void f() {\n"
22412 " other(x.begin(), //\n"
22413 " x.end(), //\n"
22414 " [&](int, int) { return 1; });\n"
22415 "}");
22416 verifyFormat("void f() {\n"
22417 " other.other.other.other.other(\n"
22418 " x.begin(), x.end(),\n"
22419 " [something, rather](int, int, int, int, int, int, int) { "
22420 "return 1; });\n"
22421 "}");
22422 verifyFormat(
22423 "void f() {\n"
22424 " other.other.other.other.other(\n"
22425 " x.begin(), x.end(),\n"
22426 " [something, rather](int, int, int, int, int, int, int) {\n"
22427 " //\n"
22428 " });\n"
22429 "}");
22430 verifyFormat("SomeFunction([]() { // A cool function...\n"
22431 " return 43;\n"
22432 "});");
22433 verifyFormat("SomeFunction([]() {\n"
22434 "#define A a\n"
22435 " return 43;\n"
22436 "});",
22437 "SomeFunction([](){\n"
22438 "#define A a\n"
22439 "return 43;\n"
22440 "});");
22441 verifyFormat("void f() {\n"
22442 " SomeFunction([](decltype(x), A *a) {});\n"
22443 " SomeFunction([](typeof(x), A *a) {});\n"
22444 " SomeFunction([](_Atomic(x), A *a) {});\n"
22445 " SomeFunction([](__underlying_type(x), A *a) {});\n"
22446 "}");
22447 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22448 " [](const aaaaaaaaaa &a) { return a; });");
22449 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22450 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
22451 "});");
22452 verifyFormat("Constructor()\n"
22453 " : Field([] { // comment\n"
22454 " int i;\n"
22455 " }) {}");
22456 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22457 " return some_parameter.size();\n"
22458 "};");
22459 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22460 " [](const string &s) { return s; };");
22461 verifyFormat("int i = aaaaaa ? 1 //\n"
22462 " : [] {\n"
22463 " return 2; //\n"
22464 " }();");
22465 verifyFormat("llvm::errs() << \"number of twos is \"\n"
22466 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
22467 " return x == 2; // force break\n"
22468 " });");
22469 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22470 " [=](int iiiiiiiiiiii) {\n"
22471 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22472 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
22473 " });",
22474 getLLVMStyleWithColumns(60));
22475
22476 verifyFormat("SomeFunction({[&] {\n"
22477 " // comment\n"
22478 " },\n"
22479 " [&] {\n"
22480 " // comment\n"
22481 " }});");
22482 verifyFormat("SomeFunction({[&] {\n"
22483 " // comment\n"
22484 "}});");
22485 verifyFormat(
22486 "virtual aaaaaaaaaaaaaaaa(\n"
22487 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22488 " aaaaa aaaaaaaaa);");
22489
22490 // Lambdas with return types.
22491 verifyFormat("int c = []() -> int { return 2; }();");
22492 verifyFormat("int c = []() -> int * { return 2; }();");
22493 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22494 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22495 verifyFormat("foo([]() noexcept -> int {});");
22496 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22497 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22498 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22499 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22500 verifyFormat("[a, a]() -> a<1> {};");
22501 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22502 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22503 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22504 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22505 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22506 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22507 verifyFormat("[]() -> foo<!5> { return {}; };");
22508 verifyFormat("[]() -> foo<~5> { return {}; };");
22509 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22510 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22511 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22512 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22513 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22514 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22515 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22516 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22517 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22518 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22519 verifyFormat("namespace bar {\n"
22520 "// broken:\n"
22521 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22522 "} // namespace bar");
22523 verifyFormat("namespace bar {\n"
22524 "// broken:\n"
22525 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22526 "} // namespace bar");
22527 verifyFormat("namespace bar {\n"
22528 "// broken:\n"
22529 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22530 "} // namespace bar");
22531 verifyFormat("namespace bar {\n"
22532 "// broken:\n"
22533 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22534 "} // namespace bar");
22535 verifyFormat("namespace bar {\n"
22536 "// broken:\n"
22537 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22538 "} // namespace bar");
22539 verifyFormat("namespace bar {\n"
22540 "// broken:\n"
22541 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22542 "} // namespace bar");
22543 verifyFormat("namespace bar {\n"
22544 "// broken:\n"
22545 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22546 "} // namespace bar");
22547 verifyFormat("namespace bar {\n"
22548 "// broken:\n"
22549 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22550 "} // namespace bar");
22551 verifyFormat("namespace bar {\n"
22552 "// broken:\n"
22553 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22554 "} // namespace bar");
22555 verifyFormat("namespace bar {\n"
22556 "// broken:\n"
22557 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22558 "} // namespace bar");
22559 verifyFormat("namespace bar {\n"
22560 "// broken:\n"
22561 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22562 "} // namespace bar");
22563 verifyFormat("namespace bar {\n"
22564 "// broken:\n"
22565 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22566 "} // namespace bar");
22567 verifyFormat("namespace bar {\n"
22568 "// broken:\n"
22569 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22570 "} // namespace bar");
22571 verifyFormat("namespace bar {\n"
22572 "// broken:\n"
22573 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22574 "} // namespace bar");
22575 verifyFormat("namespace bar {\n"
22576 "// broken:\n"
22577 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22578 "} // namespace bar");
22579 verifyFormat("namespace bar {\n"
22580 "// broken:\n"
22581 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22582 "} // namespace bar");
22583 verifyFormat("namespace bar {\n"
22584 "// broken:\n"
22585 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22586 "} // namespace bar");
22587 verifyFormat("namespace bar {\n"
22588 "// broken:\n"
22589 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22590 "} // namespace bar");
22591 verifyFormat("[]() -> a<1> {};");
22592 verifyFormat("[]() -> a<1> { ; };");
22593 verifyFormat("[]() -> a<1> { ; }();");
22594 verifyFormat("[a, a]() -> a<true> {};");
22595 verifyFormat("[]() -> a<true> {};");
22596 verifyFormat("[]() -> a<true> { ; };");
22597 verifyFormat("[]() -> a<true> { ; }();");
22598 verifyFormat("[a, a]() -> a<false> {};");
22599 verifyFormat("[]() -> a<false> {};");
22600 verifyFormat("[]() -> a<false> { ; };");
22601 verifyFormat("[]() -> a<false> { ; }();");
22602 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22603 verifyFormat("namespace bar {\n"
22604 "auto foo{[]() -> foo<false> { ; }};\n"
22605 "} // namespace bar");
22606 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22607 " int j) -> int {\n"
22608 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22609 "};");
22610 verifyFormat(
22611 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22612 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22613 " return aaaaaaaaaaaaaaaaa;\n"
22614 " });",
22615 getLLVMStyleWithColumns(70));
22616 verifyFormat("[]() //\n"
22617 " -> int {\n"
22618 " return 1; //\n"
22619 "};");
22620 verifyFormat("[]() -> Void<T...> {};");
22621 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22622 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22623 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22624 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22625 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22626 verifyFormat("return int{[x = x]() { return x; }()};");
22627
22628 // Lambdas with explicit template argument lists.
22629 verifyFormat(
22630 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22631 verifyFormat("auto L = []<class T>(T) {\n"
22632 " {\n"
22633 " f();\n"
22634 " g();\n"
22635 " }\n"
22636 "};");
22637 verifyFormat("auto L = []<class... T>(T...) {\n"
22638 " {\n"
22639 " f();\n"
22640 " g();\n"
22641 " }\n"
22642 "};");
22643 verifyFormat("auto L = []<typename... T>(T...) {\n"
22644 " {\n"
22645 " f();\n"
22646 " g();\n"
22647 " }\n"
22648 "};");
22649 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
22650 " {\n"
22651 " f();\n"
22652 " g();\n"
22653 " }\n"
22654 "};");
22655 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
22656 " {\n"
22657 " f();\n"
22658 " g();\n"
22659 " }\n"
22660 "};");
22661 verifyFormat("auto L = []<int... T>(T...) {\n"
22662 " {\n"
22663 " f();\n"
22664 " g();\n"
22665 " }\n"
22666 "};");
22667 verifyFormat("auto L = []<Foo... T>(T...) {\n"
22668 " {\n"
22669 " f();\n"
22670 " g();\n"
22671 " }\n"
22672 "};");
22673
22674 // Lambdas that fit on a single line within an argument list are not forced
22675 // onto new lines.
22676 verifyFormat("SomeFunction([] {});");
22677 verifyFormat("SomeFunction(0, [] {});");
22678 verifyFormat("SomeFunction([] {}, 0);");
22679 verifyFormat("SomeFunction(0, [] {}, 0);");
22680 verifyFormat("SomeFunction([] { return 0; }, 0);");
22681 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
22682 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
22683 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
22684 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
22685 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
22686 // Exceeded column limit. We need to break.
22687 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
22688 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
22689 "return 0; }, b);");
22690
22691 // Multiple multi-line lambdas in the same parentheses change indentation
22692 // rules. These lambdas are always forced to start on new lines.
22693 verifyFormat("SomeFunction(\n"
22694 " []() {\n"
22695 " //\n"
22696 " },\n"
22697 " []() {\n"
22698 " //\n"
22699 " });");
22700
22701 // A multi-line lambda passed as arg0 is always pushed to the next line.
22702 verifyFormat("SomeFunction(\n"
22703 " [this] {\n"
22704 " //\n"
22705 " },\n"
22706 " 1);");
22707
22708 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
22709 // the arg0 case above.
22710 auto Style = getGoogleStyle();
22711 Style.BinPackArguments = false;
22712 verifyFormat("SomeFunction(\n"
22713 " a,\n"
22714 " [this] {\n"
22715 " //\n"
22716 " },\n"
22717 " b);",
22718 Style);
22719 verifyFormat("SomeFunction(\n"
22720 " a,\n"
22721 " [this] {\n"
22722 " //\n"
22723 " },\n"
22724 " b);");
22725
22726 // A lambda with a very long line forces arg0 to be pushed out irrespective of
22727 // the BinPackArguments value (as long as the code is wide enough).
22728 verifyFormat(
22729 "something->SomeFunction(\n"
22730 " a,\n"
22731 " [this] {\n"
22732 " "
22733 "D0000000000000000000000000000000000000000000000000000000000001();\n"
22734 " },\n"
22735 " b);");
22736
22737 // A multi-line lambda is pulled up as long as the introducer fits on the
22738 // previous line and there are no further args.
22739 verifyFormat("function(1, [this, that] {\n"
22740 " //\n"
22741 "});");
22742 verifyFormat("function([this, that] {\n"
22743 " //\n"
22744 "});");
22745 // FIXME: this format is not ideal and we should consider forcing the first
22746 // arg onto its own line.
22747 verifyFormat("function(a, b, c, //\n"
22748 " d, [this, that] {\n"
22749 " //\n"
22750 " });");
22751
22752 // Multiple lambdas are treated correctly even when there is a short arg0.
22753 verifyFormat("SomeFunction(\n"
22754 " 1,\n"
22755 " [this] {\n"
22756 " //\n"
22757 " },\n"
22758 " [this] {\n"
22759 " //\n"
22760 " },\n"
22761 " 1);");
22762
22763 // More complex introducers.
22764 verifyFormat("return [i, args...] {};");
22765
22766 // Not lambdas.
22767 verifyFormat("constexpr char hello[]{\"hello\"};");
22768 verifyFormat("double &operator[](int i) { return 0; }\n"
22769 "int i;");
22770 verifyFormat("std::unique_ptr<int[]> foo() {}");
22771 verifyFormat("int i = a[a][a]->f();");
22772 verifyFormat("int i = (*b)[a]->f();");
22773
22774 // Other corner cases.
22775 verifyFormat("void f() {\n"
22776 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
22777 " );\n"
22778 "}");
22779 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
22780
22781 // Lambdas created through weird macros.
22782 verifyFormat("void f() {\n"
22783 " MACRO((const AA &a) { return 1; });\n"
22784 " MACRO((AA &a) { return 1; });\n"
22785 "}");
22786
22787 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
22788 " doo_dah();\n"
22789 " doo_dah();\n"
22790 " })) {\n"
22791 "}");
22792 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
22793 " doo_dah();\n"
22794 " doo_dah();\n"
22795 " })) {\n"
22796 "}");
22797 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
22798 " doo_dah();\n"
22799 " doo_dah();\n"
22800 " })) {\n"
22801 "}");
22802 verifyFormat("auto lambda = []() {\n"
22803 " int a = 2\n"
22804 "#if A\n"
22805 " + 2\n"
22806 "#endif\n"
22807 " ;\n"
22808 "};");
22809
22810 // Lambdas with complex multiline introducers.
22811 verifyFormat(
22812 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22813 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
22814 " -> ::std::unordered_set<\n"
22815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
22816 " //\n"
22817 " });");
22818
22819 FormatStyle DoNotMerge = getLLVMStyle();
22820 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
22821 verifyFormat("auto c = []() {\n"
22822 " return b;\n"
22823 "};",
22824 "auto c = []() { return b; };", DoNotMerge);
22825 verifyFormat("auto c = []() {\n"
22826 "};",
22827 " auto c = []() {};", DoNotMerge);
22828
22829 FormatStyle MergeEmptyOnly = getLLVMStyle();
22830 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
22831 verifyFormat("auto c = []() {\n"
22832 " return b;\n"
22833 "};",
22834 "auto c = []() {\n"
22835 " return b;\n"
22836 " };",
22837 MergeEmptyOnly);
22838 verifyFormat("auto c = []() {};",
22839 "auto c = []() {\n"
22840 "};",
22841 MergeEmptyOnly);
22842
22843 FormatStyle MergeInline = getLLVMStyle();
22844 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
22845 verifyFormat("auto c = []() {\n"
22846 " return b;\n"
22847 "};",
22848 "auto c = []() { return b; };", MergeInline);
22849 verifyFormat("function([]() { return b; })", MergeInline);
22850 verifyFormat("function([]() { return b; }, a)", MergeInline);
22851 verifyFormat("function(a, []() { return b; })", MergeInline);
22852
22853 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
22854 // AllowShortLambdasOnASingleLine
22855 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
22856 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
22857 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
22858 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22859 FormatStyle::ShortLambdaStyle::SLS_None;
22860 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
22861 " []()\n"
22862 " {\n"
22863 " return 17;\n"
22864 " });",
22865 LLVMWithBeforeLambdaBody);
22866 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
22867 " []()\n"
22868 " {\n"
22869 " });",
22870 LLVMWithBeforeLambdaBody);
22871 verifyFormat("auto fct_SLS_None = []()\n"
22872 "{\n"
22873 " return 17;\n"
22874 "};",
22875 LLVMWithBeforeLambdaBody);
22876 verifyFormat("TwoNestedLambdas_SLS_None(\n"
22877 " []()\n"
22878 " {\n"
22879 " return Call(\n"
22880 " []()\n"
22881 " {\n"
22882 " return 17;\n"
22883 " });\n"
22884 " });",
22885 LLVMWithBeforeLambdaBody);
22886 verifyFormat("void Fct() {\n"
22887 " return {[]()\n"
22888 " {\n"
22889 " return 17;\n"
22890 " }};\n"
22891 "}",
22892 LLVMWithBeforeLambdaBody);
22893
22894 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22895 FormatStyle::ShortLambdaStyle::SLS_Empty;
22896 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
22897 " []()\n"
22898 " {\n"
22899 " return 17;\n"
22900 " });",
22901 LLVMWithBeforeLambdaBody);
22902 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
22903 LLVMWithBeforeLambdaBody);
22904 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
22905 "ongFunctionName_SLS_Empty(\n"
22906 " []() {});",
22907 LLVMWithBeforeLambdaBody);
22908 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
22909 " []()\n"
22910 " {\n"
22911 " return 17;\n"
22912 " });",
22913 LLVMWithBeforeLambdaBody);
22914 verifyFormat("auto fct_SLS_Empty = []()\n"
22915 "{\n"
22916 " return 17;\n"
22917 "};",
22918 LLVMWithBeforeLambdaBody);
22919 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
22920 " []()\n"
22921 " {\n"
22922 " return Call([]() {});\n"
22923 " });",
22924 LLVMWithBeforeLambdaBody);
22925 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
22926 " []()\n"
22927 " {\n"
22928 " return Call([]() {});\n"
22929 " });",
22930 LLVMWithBeforeLambdaBody);
22931 verifyFormat(
22932 "FctWithLongLineInLambda_SLS_Empty(\n"
22933 " []()\n"
22934 " {\n"
22935 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22936 " AndShouldNotBeConsiderAsInline,\n"
22937 " LambdaBodyMustBeBreak);\n"
22938 " });",
22939 LLVMWithBeforeLambdaBody);
22940
22941 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22942 FormatStyle::ShortLambdaStyle::SLS_Inline;
22943 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
22944 LLVMWithBeforeLambdaBody);
22945 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
22946 LLVMWithBeforeLambdaBody);
22947 verifyFormat("auto fct_SLS_Inline = []()\n"
22948 "{\n"
22949 " return 17;\n"
22950 "};",
22951 LLVMWithBeforeLambdaBody);
22952 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
22953 "17; }); });",
22954 LLVMWithBeforeLambdaBody);
22955 verifyFormat(
22956 "FctWithLongLineInLambda_SLS_Inline(\n"
22957 " []()\n"
22958 " {\n"
22959 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22960 " AndShouldNotBeConsiderAsInline,\n"
22961 " LambdaBodyMustBeBreak);\n"
22962 " });",
22963 LLVMWithBeforeLambdaBody);
22964 verifyFormat("FctWithMultipleParams_SLS_Inline("
22965 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
22966 " []() { return 17; });",
22967 LLVMWithBeforeLambdaBody);
22968 verifyFormat(
22969 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
22970 LLVMWithBeforeLambdaBody);
22971
22972 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
22973 FormatStyle::ShortLambdaStyle::SLS_All;
22974 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
22975 LLVMWithBeforeLambdaBody);
22976 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
22977 LLVMWithBeforeLambdaBody);
22978 verifyFormat("auto fct_SLS_All = []() { return 17; };",
22979 LLVMWithBeforeLambdaBody);
22980 verifyFormat("FctWithOneParam_SLS_All(\n"
22981 " []()\n"
22982 " {\n"
22983 " // A cool function...\n"
22984 " return 43;\n"
22985 " });",
22986 LLVMWithBeforeLambdaBody);
22987 verifyFormat("FctWithMultipleParams_SLS_All("
22988 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
22989 " []() { return 17; });",
22990 LLVMWithBeforeLambdaBody);
22991 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
22992 LLVMWithBeforeLambdaBody);
22993 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
22994 LLVMWithBeforeLambdaBody);
22995 verifyFormat(
22996 "FctWithLongLineInLambda_SLS_All(\n"
22997 " []()\n"
22998 " {\n"
22999 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23000 " AndShouldNotBeConsiderAsInline,\n"
23001 " LambdaBodyMustBeBreak);\n"
23002 " });",
23003 LLVMWithBeforeLambdaBody);
23004 verifyFormat(
23005 "auto fct_SLS_All = []()\n"
23006 "{\n"
23007 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23008 " AndShouldNotBeConsiderAsInline,\n"
23009 " LambdaBodyMustBeBreak);\n"
23010 "};",
23011 LLVMWithBeforeLambdaBody);
23012 LLVMWithBeforeLambdaBody.BinPackParameters = false;
23013 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23014 LLVMWithBeforeLambdaBody);
23015 verifyFormat(
23016 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23017 " FirstParam,\n"
23018 " SecondParam,\n"
23019 " ThirdParam,\n"
23020 " FourthParam);",
23021 LLVMWithBeforeLambdaBody);
23022 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23023 " []() { return "
23024 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23025 " FirstParam,\n"
23026 " SecondParam,\n"
23027 " ThirdParam,\n"
23028 " FourthParam);",
23029 LLVMWithBeforeLambdaBody);
23030 verifyFormat(
23031 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23032 " SecondParam,\n"
23033 " ThirdParam,\n"
23034 " FourthParam,\n"
23035 " []() { return SomeValueNotSoLong; });",
23036 LLVMWithBeforeLambdaBody);
23037 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23038 " []()\n"
23039 " {\n"
23040 " return "
23041 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23042 "eConsiderAsInline;\n"
23043 " });",
23044 LLVMWithBeforeLambdaBody);
23045 verifyFormat(
23046 "FctWithLongLineInLambda_SLS_All(\n"
23047 " []()\n"
23048 " {\n"
23049 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23050 " AndShouldNotBeConsiderAsInline,\n"
23051 " LambdaBodyMustBeBreak);\n"
23052 " });",
23053 LLVMWithBeforeLambdaBody);
23054 verifyFormat("FctWithTwoParams_SLS_All(\n"
23055 " []()\n"
23056 " {\n"
23057 " // A cool function...\n"
23058 " return 43;\n"
23059 " },\n"
23060 " 87);",
23061 LLVMWithBeforeLambdaBody);
23062 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23063 LLVMWithBeforeLambdaBody);
23064 verifyFormat(
23065 "FctWithTwoParams_SLS_All(\n"
23066 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23067 LLVMWithBeforeLambdaBody);
23068 verifyFormat(
23069 "FctWithTwoParams_SLS_All(\n"
23070 " 87,\n"
23071 " []()\n"
23072 " {\n"
23073 " return "
23074 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23075 " });",
23076 LLVMWithBeforeLambdaBody);
23077 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23078 LLVMWithBeforeLambdaBody);
23079 verifyFormat(
23080 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23081 LLVMWithBeforeLambdaBody);
23082 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23083 "}); }, x);",
23084 LLVMWithBeforeLambdaBody);
23085 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23086 " []()\n"
23087 " {\n"
23088 " // A cool function...\n"
23089 " return Call([]() { return 17; });\n"
23090 " });",
23091 LLVMWithBeforeLambdaBody);
23092 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23093 " []()\n"
23094 " {\n"
23095 " return Call(\n"
23096 " []()\n"
23097 " {\n"
23098 " // A cool function...\n"
23099 " return 17;\n"
23100 " });\n"
23101 " });",
23102 LLVMWithBeforeLambdaBody);
23103
23104 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23105 FormatStyle::ShortLambdaStyle::SLS_None;
23106
23107 verifyFormat("auto select = [this]() -> const Library::Object *\n"
23108 "{\n"
23109 " return MyAssignment::SelectFromList(this);\n"
23110 "};",
23111 LLVMWithBeforeLambdaBody);
23112
23113 verifyFormat("auto select = [this]() -> const Library::Object &\n"
23114 "{\n"
23115 " return MyAssignment::SelectFromList(this);\n"
23116 "};",
23117 LLVMWithBeforeLambdaBody);
23118
23119 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23120 "{\n"
23121 " return MyAssignment::SelectFromList(this);\n"
23122 "};",
23123 LLVMWithBeforeLambdaBody);
23124
23125 verifyFormat("namespace test {\n"
23126 "class Test {\n"
23127 "public:\n"
23128 " Test() = default;\n"
23129 "};\n"
23130 "} // namespace test",
23131 LLVMWithBeforeLambdaBody);
23132
23133 // Lambdas with different indentation styles.
23134 Style = getLLVMStyleWithColumns(ColumnLimit: 60);
23135 verifyFormat("Result doSomething(Promise promise) {\n"
23136 " return promise.then(\n"
23137 " [this, obj = std::move(s)](int bar) mutable {\n"
23138 " return someObject.startAsyncAction().then(\n"
23139 " [this, &obj](Result result) mutable {\n"
23140 " result.processMore();\n"
23141 " });\n"
23142 " });\n"
23143 "}",
23144 Style);
23145 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
23146 verifyFormat("Result doSomething(Promise promise) {\n"
23147 " return promise.then(\n"
23148 " [this, obj = std::move(s)](int bar) mutable {\n"
23149 " return obj.startAsyncAction().then(\n"
23150 " [this, &obj](Result result) mutable {\n"
23151 " result.processMore();\n"
23152 " });\n"
23153 " });\n"
23154 "}",
23155 Style);
23156 verifyFormat("Result doSomething(Promise promise) {\n"
23157 " return promise.then([this, obj = std::move(s)] {\n"
23158 " return obj.startAsyncAction().then(\n"
23159 " [this, &obj](Result result) mutable {\n"
23160 " result.processMore();\n"
23161 " });\n"
23162 " });\n"
23163 "}",
23164 Style);
23165 verifyFormat("void test() {\n"
23166 " ([]() -> auto {\n"
23167 " int b = 32;\n"
23168 " return 3;\n"
23169 " }).foo();\n"
23170 "}",
23171 Style);
23172 verifyFormat("void test() {\n"
23173 " []() -> auto {\n"
23174 " int b = 32;\n"
23175 " return 3;\n"
23176 " }\n"
23177 "}",
23178 Style);
23179 verifyFormat("void test() {\n"
23180 " std::sort(v.begin(), v.end(),\n"
23181 " [](const auto &foo, const auto &bar) {\n"
23182 " return foo.baz < bar.baz;\n"
23183 " });\n"
23184 "};",
23185 Style);
23186 verifyFormat("void test() {\n"
23187 " (\n"
23188 " []() -> auto {\n"
23189 " int b = 32;\n"
23190 " return 3;\n"
23191 " }, foo, bar)\n"
23192 " .foo();\n"
23193 "}",
23194 Style);
23195 verifyFormat("void test() {\n"
23196 " ([]() -> auto {\n"
23197 " int b = 32;\n"
23198 " return 3;\n"
23199 " })\n"
23200 " .foo()\n"
23201 " .bar();\n"
23202 "}",
23203 Style);
23204 verifyFormat("#define A \\\n"
23205 " [] { \\\n"
23206 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
23207 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
23208 " }",
23209 Style);
23210 verifyFormat("#define SORT(v) \\\n"
23211 " std::sort(v.begin(), v.end(), \\\n"
23212 " [](const auto &foo, const auto &bar) { \\\n"
23213 " return foo.baz < bar.baz; \\\n"
23214 " });",
23215 Style);
23216 verifyFormat("void foo() {\n"
23217 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23218 " auto f = e(d);\n"
23219 " return f;\n"
23220 " })));\n"
23221 "}",
23222 Style);
23223 verifyFormat("void foo() {\n"
23224 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23225 " auto f = e(foo, [&] {\n"
23226 " auto g = h();\n"
23227 " return g;\n"
23228 " }, qux, [&] -> Bar {\n"
23229 " auto i = j();\n"
23230 " return i;\n"
23231 " });\n"
23232 " return f;\n"
23233 " })));\n"
23234 "}",
23235 Style);
23236 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23237 " AnotherLongClassName baz)\n"
23238 " : baz{baz}, func{[&] {\n"
23239 " auto qux = bar;\n"
23240 " return aFunkyFunctionCall(qux);\n"
23241 " }} {}",
23242 Style);
23243 verifyFormat("void foo() {\n"
23244 " class Foo {\n"
23245 " public:\n"
23246 " Foo()\n"
23247 " : qux{[](int quux) {\n"
23248 " auto tmp = quux;\n"
23249 " return tmp;\n"
23250 " }} {}\n"
23251 "\n"
23252 " private:\n"
23253 " std::function<void(int quux)> qux;\n"
23254 " };\n"
23255 "}",
23256 Style);
23257 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
23258 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23259 " AnotherLongClassName baz) :\n"
23260 " baz{baz}, func{[&] {\n"
23261 " auto qux = bar;\n"
23262 " return aFunkyFunctionCall(qux);\n"
23263 " }} {}",
23264 Style);
23265 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
23266 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23267 " AnotherLongClassName baz) :\n"
23268 " baz{baz},\n"
23269 " func{[&] {\n"
23270 " auto qux = bar;\n"
23271 " return aFunkyFunctionCall(qux);\n"
23272 " }} {}",
23273 Style);
23274 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
23275 // FIXME: The following test should pass, but fails at the time of writing.
23276#if 0
23277 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23278 // doesn't force an initial line break, even if lambdas span multiple lines.
23279 verifyFormat("void foo() {\n"
23280 " aFunction(\n"
23281 " [](d) -> Foo {\n"
23282 " auto f = e(d);\n"
23283 " return f;\n"
23284 " }, foo, Bar{}, [] {\n"
23285 " auto g = h();\n"
23286 " return g;\n"
23287 " }, baz);\n"
23288 "}",
23289 Style);
23290#endif
23291 // A long non-lambda argument forces arguments to span multiple lines and thus
23292 // forces an initial line break when using AlwaysBreak.
23293 verifyFormat("void foo() {\n"
23294 " aFunction(\n"
23295 " 1,\n"
23296 " [](d) -> Foo {\n"
23297 " auto f = e(d);\n"
23298 " return f;\n"
23299 " }, foo, Bar{},\n"
23300 " [] {\n"
23301 " auto g = h();\n"
23302 " return g;\n"
23303 " }, bazzzzz,\n"
23304 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23305 "}",
23306 Style);
23307 Style.BinPackArguments = false;
23308 verifyFormat("void foo() {\n"
23309 " aFunction(\n"
23310 " 1,\n"
23311 " [](d) -> Foo {\n"
23312 " auto f = e(d);\n"
23313 " return f;\n"
23314 " },\n"
23315 " foo,\n"
23316 " Bar{},\n"
23317 " [] {\n"
23318 " auto g = h();\n"
23319 " return g;\n"
23320 " },\n"
23321 " bazzzzz,\n"
23322 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23323 "}",
23324 Style);
23325 Style.BinPackArguments = true;
23326 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
23327 Style.BraceWrapping.BeforeLambdaBody = true;
23328 verifyFormat("void foo() {\n"
23329 " aFunction(\n"
23330 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23331 " {\n"
23332 " auto f = e(\n"
23333 " [&]\n"
23334 " {\n"
23335 " auto g = h();\n"
23336 " return g;\n"
23337 " }, qux, [&] -> Bar\n"
23338 " {\n"
23339 " auto i = j();\n"
23340 " return i;\n"
23341 " });\n"
23342 " return f;\n"
23343 " })));\n"
23344 "}",
23345 Style);
23346}
23347
23348TEST_F(FormatTest, LambdaWithLineComments) {
23349 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23350 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23351 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23352 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23353 FormatStyle::ShortLambdaStyle::SLS_All;
23354
23355 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
23356 verifyFormat("auto k = []() // comment\n"
23357 "{ return; }",
23358 LLVMWithBeforeLambdaBody);
23359 verifyFormat("auto k = []() /* comment */ { return; }",
23360 LLVMWithBeforeLambdaBody);
23361 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23362 LLVMWithBeforeLambdaBody);
23363 verifyFormat("auto k = []() // X\n"
23364 "{ return; }",
23365 LLVMWithBeforeLambdaBody);
23366 verifyFormat(
23367 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23368 "{ return; }",
23369 LLVMWithBeforeLambdaBody);
23370
23371 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
23372
23373 verifyFormat("foo([]()\n"
23374 " {\n"
23375 " bar(); //\n"
23376 " return 1; // comment\n"
23377 " }());",
23378 "foo([]() {\n"
23379 " bar(); //\n"
23380 " return 1; // comment\n"
23381 "}());",
23382 LLVMWithBeforeLambdaBody);
23383 verifyFormat("foo(\n"
23384 " 1, MACRO {\n"
23385 " baz();\n"
23386 " bar(); // comment\n"
23387 " },\n"
23388 " []() {});",
23389 "foo(\n"
23390 " 1, MACRO { baz(); bar(); // comment\n"
23391 " }, []() {}\n"
23392 ");",
23393 LLVMWithBeforeLambdaBody);
23394}
23395
23396TEST_F(FormatTest, EmptyLinesInLambdas) {
23397 verifyFormat("auto lambda = []() {\n"
23398 " x(); //\n"
23399 "};",
23400 "auto lambda = []() {\n"
23401 "\n"
23402 " x(); //\n"
23403 "\n"
23404 "};");
23405}
23406
23407TEST_F(FormatTest, FormatsBlocks) {
23408 FormatStyle ShortBlocks = getLLVMStyle();
23409 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23410 verifyFormat("int (^Block)(int, int);", ShortBlocks);
23411 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
23412 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
23413 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
23414 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
23415 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
23416
23417 verifyFormat("foo(^{ bar(); });", ShortBlocks);
23418 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
23419 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
23420
23421 verifyFormat("[operation setCompletionBlock:^{\n"
23422 " [self onOperationDone];\n"
23423 "}];");
23424 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23425 " [self onOperationDone];\n"
23426 "}]};");
23427 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23428 " f();\n"
23429 "}];");
23430 verifyFormat("int a = [operation block:^int(int *i) {\n"
23431 " return 1;\n"
23432 "}];");
23433 verifyFormat("[myObject doSomethingWith:arg1\n"
23434 " aaa:^int(int *a) {\n"
23435 " return 1;\n"
23436 " }\n"
23437 " bbb:f(a * bbbbbbbb)];");
23438
23439 verifyFormat("[operation setCompletionBlock:^{\n"
23440 " [self.delegate newDataAvailable];\n"
23441 "}];",
23442 getLLVMStyleWithColumns(60));
23443 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23444 " NSString *path = [self sessionFilePath];\n"
23445 " if (path) {\n"
23446 " // ...\n"
23447 " }\n"
23448 "});");
23449 verifyFormat("[[SessionService sharedService]\n"
23450 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23451 " if (window) {\n"
23452 " [self windowDidLoad:window];\n"
23453 " } else {\n"
23454 " [self errorLoadingWindow];\n"
23455 " }\n"
23456 " }];");
23457 verifyFormat("void (^largeBlock)(void) = ^{\n"
23458 " // ...\n"
23459 "};",
23460 getLLVMStyleWithColumns(40));
23461 verifyFormat("[[SessionService sharedService]\n"
23462 " loadWindowWithCompletionBlock: //\n"
23463 " ^(SessionWindow *window) {\n"
23464 " if (window) {\n"
23465 " [self windowDidLoad:window];\n"
23466 " } else {\n"
23467 " [self errorLoadingWindow];\n"
23468 " }\n"
23469 " }];",
23470 getLLVMStyleWithColumns(60));
23471 verifyFormat("[myObject doSomethingWith:arg1\n"
23472 " firstBlock:^(Foo *a) {\n"
23473 " // ...\n"
23474 " int i;\n"
23475 " }\n"
23476 " secondBlock:^(Bar *b) {\n"
23477 " // ...\n"
23478 " int i;\n"
23479 " }\n"
23480 " thirdBlock:^Foo(Bar *b) {\n"
23481 " // ...\n"
23482 " int i;\n"
23483 " }];");
23484 verifyFormat("[myObject doSomethingWith:arg1\n"
23485 " firstBlock:-1\n"
23486 " secondBlock:^(Bar *b) {\n"
23487 " // ...\n"
23488 " int i;\n"
23489 " }];");
23490
23491 verifyFormat("f(^{\n"
23492 " @autoreleasepool {\n"
23493 " if (a) {\n"
23494 " g();\n"
23495 " }\n"
23496 " }\n"
23497 "});");
23498 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23499 "};");
23500 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23501 "};");
23502
23503 FormatStyle FourIndent = getLLVMStyle();
23504 FourIndent.ObjCBlockIndentWidth = 4;
23505 verifyFormat("[operation setCompletionBlock:^{\n"
23506 " [self onOperationDone];\n"
23507 "}];",
23508 FourIndent);
23509}
23510
23511TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
23512 FormatStyle ZeroColumn = getLLVMStyleWithColumns(ColumnLimit: 0);
23513
23514 verifyFormat("[[SessionService sharedService] "
23515 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23516 " if (window) {\n"
23517 " [self windowDidLoad:window];\n"
23518 " } else {\n"
23519 " [self errorLoadingWindow];\n"
23520 " }\n"
23521 "}];",
23522 ZeroColumn);
23523 verifyFormat("[[SessionService sharedService]\n"
23524 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23525 " if (window) {\n"
23526 " [self windowDidLoad:window];\n"
23527 " } else {\n"
23528 " [self errorLoadingWindow];\n"
23529 " }\n"
23530 " }];",
23531 "[[SessionService sharedService]\n"
23532 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23533 " if (window) {\n"
23534 " [self windowDidLoad:window];\n"
23535 " } else {\n"
23536 " [self errorLoadingWindow];\n"
23537 " }\n"
23538 "}];",
23539 ZeroColumn);
23540 verifyFormat("[myObject doSomethingWith:arg1\n"
23541 " firstBlock:^(Foo *a) {\n"
23542 " // ...\n"
23543 " int i;\n"
23544 " }\n"
23545 " secondBlock:^(Bar *b) {\n"
23546 " // ...\n"
23547 " int i;\n"
23548 " }\n"
23549 " thirdBlock:^Foo(Bar *b) {\n"
23550 " // ...\n"
23551 " int i;\n"
23552 " }];",
23553 ZeroColumn);
23554 verifyFormat("f(^{\n"
23555 " @autoreleasepool {\n"
23556 " if (a) {\n"
23557 " g();\n"
23558 " }\n"
23559 " }\n"
23560 "});",
23561 ZeroColumn);
23562 verifyFormat("void (^largeBlock)(void) = ^{\n"
23563 " // ...\n"
23564 "};",
23565 ZeroColumn);
23566
23567 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
23568 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23569 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23570 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
23571 verifyFormat("void (^largeBlock)(void) = ^{\n"
23572 " int i;\n"
23573 "};",
23574 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
23575}
23576
23577TEST_F(FormatTest, SupportsCRLF) {
23578 verifyFormat("int a;\r\n"
23579 "int b;\r\n"
23580 "int c;",
23581 "int a;\r\n"
23582 " int b;\r\n"
23583 " int c;");
23584 verifyFormat("int a;\r\n"
23585 "int b;\r\n"
23586 "int c;\r\n",
23587 "int a;\r\n"
23588 " int b;\n"
23589 " int c;\r\n");
23590 verifyFormat("int a;\n"
23591 "int b;\n"
23592 "int c;",
23593 "int a;\r\n"
23594 " int b;\n"
23595 " int c;");
23596 // FIXME: unstable test case
23597 EXPECT_EQ("\"aaaaaaa \"\r\n"
23598 "\"bbbbbbb\";\r\n",
23599 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23600 verifyFormat("#define A \\\r\n"
23601 " b; \\\r\n"
23602 " c; \\\r\n"
23603 " d;",
23604 "#define A \\\r\n"
23605 " b; \\\r\n"
23606 " c; d; ",
23607 getGoogleStyle());
23608
23609 verifyNoChange("/*\r\n"
23610 "multi line block comments\r\n"
23611 "should not introduce\r\n"
23612 "an extra carriage return\r\n"
23613 "*/");
23614 verifyFormat("/*\r\n"
23615 "\r\n"
23616 "*/",
23617 "/*\r\n"
23618 " \r\r\r\n"
23619 "*/");
23620
23621 FormatStyle style = getLLVMStyle();
23622
23623 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
23624 verifyFormat("union FooBarBazQux {\n"
23625 " int foo;\n"
23626 " int bar;\n"
23627 " int baz;\n"
23628 "};",
23629 "union FooBarBazQux {\r\n"
23630 " int foo;\n"
23631 " int bar;\r\n"
23632 " int baz;\n"
23633 "};",
23634 style);
23635 style.LineEnding = FormatStyle::LE_DeriveCRLF;
23636 verifyFormat("union FooBarBazQux {\r\n"
23637 " int foo;\r\n"
23638 " int bar;\r\n"
23639 " int baz;\r\n"
23640 "};",
23641 "union FooBarBazQux {\r\n"
23642 " int foo;\n"
23643 " int bar;\r\n"
23644 " int baz;\n"
23645 "};",
23646 style);
23647
23648 style.LineEnding = FormatStyle::LE_LF;
23649 verifyFormat("union FooBarBazQux {\n"
23650 " int foo;\n"
23651 " int bar;\n"
23652 " int baz;\n"
23653 " int qux;\n"
23654 "};",
23655 "union FooBarBazQux {\r\n"
23656 " int foo;\n"
23657 " int bar;\r\n"
23658 " int baz;\n"
23659 " int qux;\r\n"
23660 "};",
23661 style);
23662 style.LineEnding = FormatStyle::LE_CRLF;
23663 verifyFormat("union FooBarBazQux {\r\n"
23664 " int foo;\r\n"
23665 " int bar;\r\n"
23666 " int baz;\r\n"
23667 " int qux;\r\n"
23668 "};",
23669 "union FooBarBazQux {\r\n"
23670 " int foo;\n"
23671 " int bar;\r\n"
23672 " int baz;\n"
23673 " int qux;\n"
23674 "};",
23675 style);
23676
23677 style.LineEnding = FormatStyle::LE_DeriveLF;
23678 verifyFormat("union FooBarBazQux {\r\n"
23679 " int foo;\r\n"
23680 " int bar;\r\n"
23681 " int baz;\r\n"
23682 " int qux;\r\n"
23683 "};",
23684 "union FooBarBazQux {\r\n"
23685 " int foo;\n"
23686 " int bar;\r\n"
23687 " int baz;\n"
23688 " int qux;\r\n"
23689 "};",
23690 style);
23691 style.LineEnding = FormatStyle::LE_DeriveCRLF;
23692 verifyFormat("union FooBarBazQux {\n"
23693 " int foo;\n"
23694 " int bar;\n"
23695 " int baz;\n"
23696 " int qux;\n"
23697 "};",
23698 "union FooBarBazQux {\r\n"
23699 " int foo;\n"
23700 " int bar;\r\n"
23701 " int baz;\n"
23702 " int qux;\n"
23703 "};",
23704 style);
23705}
23706
23707TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
23708 verifyFormat("MY_CLASS(C) {\n"
23709 " int i;\n"
23710 " int j;\n"
23711 "};");
23712}
23713
23714TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
23715 FormatStyle TwoIndent = getLLVMStyleWithColumns(ColumnLimit: 15);
23716 TwoIndent.ContinuationIndentWidth = 2;
23717
23718 verifyFormat("int i =\n"
23719 " longFunction(\n"
23720 " arg);",
23721 "int i = longFunction(arg);", TwoIndent);
23722
23723 FormatStyle SixIndent = getLLVMStyleWithColumns(ColumnLimit: 20);
23724 SixIndent.ContinuationIndentWidth = 6;
23725
23726 verifyFormat("int i =\n"
23727 " longFunction(\n"
23728 " arg);",
23729 "int i = longFunction(arg);", SixIndent);
23730}
23731
23732TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
23733 FormatStyle Style = getLLVMStyle();
23734 verifyFormat("int Foo::getter(\n"
23735 " //\n"
23736 ") const {\n"
23737 " return foo;\n"
23738 "}",
23739 Style);
23740 verifyFormat("void Foo::setter(\n"
23741 " //\n"
23742 ") {\n"
23743 " foo = 1;\n"
23744 "}",
23745 Style);
23746}
23747
23748TEST_F(FormatTest, SpacesInAngles) {
23749 FormatStyle Spaces = getLLVMStyle();
23750 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23751
23752 verifyFormat("vector< ::std::string > x1;", Spaces);
23753 verifyFormat("Foo< int, Bar > x2;", Spaces);
23754 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
23755
23756 verifyFormat("static_cast< int >(arg);", Spaces);
23757 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
23758 verifyFormat("f< int, float >();", Spaces);
23759 verifyFormat("template <> g() {}", Spaces);
23760 verifyFormat("template < std::vector< int > > f() {}", Spaces);
23761 verifyFormat("std::function< void(int, int) > fct;", Spaces);
23762 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
23763 Spaces);
23764
23765 Spaces.Standard = FormatStyle::LS_Cpp03;
23766 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23767 verifyFormat("A< A< int > >();", Spaces);
23768
23769 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
23770 verifyFormat("A<A<int> >();", Spaces);
23771
23772 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
23773 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
23774 Spaces);
23775 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
23776 Spaces);
23777
23778 verifyFormat("A<A<int> >();", Spaces);
23779 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
23780 verifyFormat("A< A< int > >();", Spaces);
23781
23782 Spaces.Standard = FormatStyle::LS_Cpp11;
23783 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23784 verifyFormat("A< A< int > >();", Spaces);
23785
23786 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
23787 verifyFormat("vector<::std::string> x4;", Spaces);
23788 verifyFormat("vector<int> x5;", Spaces);
23789 verifyFormat("Foo<int, Bar> x6;", Spaces);
23790 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
23791
23792 verifyFormat("A<A<int>>();", Spaces);
23793
23794 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
23795 verifyFormat("vector<::std::string> x4;", Spaces);
23796 verifyFormat("vector< ::std::string > x4;", Spaces);
23797 verifyFormat("vector<int> x5;", Spaces);
23798 verifyFormat("vector< int > x5;", Spaces);
23799 verifyFormat("Foo<int, Bar> x6;", Spaces);
23800 verifyFormat("Foo< int, Bar > x6;", Spaces);
23801 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
23802 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
23803
23804 verifyFormat("A<A<int>>();", Spaces);
23805 verifyFormat("A< A< int > >();", Spaces);
23806 verifyFormat("A<A<int > >();", Spaces);
23807 verifyFormat("A< A< int>>();", Spaces);
23808
23809 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
23810 verifyFormat("// clang-format off\n"
23811 "foo<<<1, 1>>>();\n"
23812 "// clang-format on",
23813 Spaces);
23814 verifyFormat("// clang-format off\n"
23815 "foo< < <1, 1> > >();\n"
23816 "// clang-format on",
23817 Spaces);
23818}
23819
23820TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
23821 FormatStyle Style = getLLVMStyle();
23822 Style.SpaceAfterTemplateKeyword = false;
23823 verifyFormat("template<int> void foo();", Style);
23824}
23825
23826TEST_F(FormatTest, TripleAngleBrackets) {
23827 verifyFormat("f<<<1, 1>>>();");
23828 verifyFormat("f<<<1, 1, 1, s>>>();");
23829 verifyFormat("f<<<a, b, c, d>>>();");
23830 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
23831 verifyFormat("f<param><<<1, 1>>>();");
23832 verifyFormat("f<1><<<1, 1>>>();");
23833 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
23834 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23835 "aaaaaaaaaaa<<<\n 1, 1>>>();");
23836 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
23837 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
23838}
23839
23840TEST_F(FormatTest, MergeLessLessAtEnd) {
23841 verifyFormat("<<");
23842 verifyFormat("< < <", "\\\n<<<");
23843 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23844 "aaallvm::outs() <<");
23845 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23846 "aaaallvm::outs()\n <<");
23847}
23848
23849TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
23850 std::string code = "#if A\n"
23851 "#if B\n"
23852 "a.\n"
23853 "#endif\n"
23854 " a = 1;\n"
23855 "#else\n"
23856 "#endif\n"
23857 "#if C\n"
23858 "#else\n"
23859 "#endif\n";
23860 verifyFormat(code);
23861}
23862
23863TEST_F(FormatTest, HandleConflictMarkers) {
23864 // Git/SVN conflict markers.
23865 verifyFormat("int a;\n"
23866 "void f() {\n"
23867 " callme(some(parameter1,\n"
23868 "<<<<<<< text by the vcs\n"
23869 " parameter2),\n"
23870 "||||||| text by the vcs\n"
23871 " parameter2),\n"
23872 " parameter3,\n"
23873 "======= text by the vcs\n"
23874 " parameter2, parameter3),\n"
23875 ">>>>>>> text by the vcs\n"
23876 " otherparameter);",
23877 "int a;\n"
23878 "void f() {\n"
23879 " callme(some(parameter1,\n"
23880 "<<<<<<< text by the vcs\n"
23881 " parameter2),\n"
23882 "||||||| text by the vcs\n"
23883 " parameter2),\n"
23884 " parameter3,\n"
23885 "======= text by the vcs\n"
23886 " parameter2,\n"
23887 " parameter3),\n"
23888 ">>>>>>> text by the vcs\n"
23889 " otherparameter);");
23890
23891 // Perforce markers.
23892 verifyFormat("void f() {\n"
23893 " function(\n"
23894 ">>>> text by the vcs\n"
23895 " parameter,\n"
23896 "==== text by the vcs\n"
23897 " parameter,\n"
23898 "==== text by the vcs\n"
23899 " parameter,\n"
23900 "<<<< text by the vcs\n"
23901 " parameter);",
23902 "void f() {\n"
23903 " function(\n"
23904 ">>>> text by the vcs\n"
23905 " parameter,\n"
23906 "==== text by the vcs\n"
23907 " parameter,\n"
23908 "==== text by the vcs\n"
23909 " parameter,\n"
23910 "<<<< text by the vcs\n"
23911 " parameter);");
23912
23913 verifyNoChange("<<<<<<<\n"
23914 "|||||||\n"
23915 "=======\n"
23916 ">>>>>>>");
23917
23918 verifyNoChange("<<<<<<<\n"
23919 "|||||||\n"
23920 "int i;\n"
23921 "=======\n"
23922 ">>>>>>>");
23923
23924 // FIXME: Handle parsing of macros around conflict markers correctly:
23925 verifyFormat("#define Macro \\\n"
23926 "<<<<<<<\n"
23927 "Something \\\n"
23928 "|||||||\n"
23929 "Else \\\n"
23930 "=======\n"
23931 "Other \\\n"
23932 ">>>>>>>\n"
23933 " End int i;",
23934 "#define Macro \\\n"
23935 "<<<<<<<\n"
23936 " Something \\\n"
23937 "|||||||\n"
23938 " Else \\\n"
23939 "=======\n"
23940 " Other \\\n"
23941 ">>>>>>>\n"
23942 " End\n"
23943 "int i;");
23944
23945 verifyFormat(R"(====
23946#ifdef A
23947a
23948#else
23949b
23950#endif
23951)");
23952}
23953
23954TEST_F(FormatTest, DisableRegions) {
23955 verifyFormat("int i;\n"
23956 "// clang-format off\n"
23957 " int j;\n"
23958 "// clang-format on\n"
23959 "int k;",
23960 " int i;\n"
23961 " // clang-format off\n"
23962 " int j;\n"
23963 " // clang-format on\n"
23964 " int k;");
23965 verifyFormat("int i;\n"
23966 "/* clang-format off */\n"
23967 " int j;\n"
23968 "/* clang-format on */\n"
23969 "int k;",
23970 " int i;\n"
23971 " /* clang-format off */\n"
23972 " int j;\n"
23973 " /* clang-format on */\n"
23974 " int k;");
23975
23976 // Don't reflow comments within disabled regions.
23977 verifyFormat("// clang-format off\n"
23978 "// long long long long long long line\n"
23979 "/* clang-format on */\n"
23980 "/* long long long\n"
23981 " * long long long\n"
23982 " * line */\n"
23983 "int i;\n"
23984 "/* clang-format off */\n"
23985 "/* long long long long long long line */",
23986 "// clang-format off\n"
23987 "// long long long long long long line\n"
23988 "/* clang-format on */\n"
23989 "/* long long long long long long line */\n"
23990 "int i;\n"
23991 "/* clang-format off */\n"
23992 "/* long long long long long long line */",
23993 getLLVMStyleWithColumns(20));
23994
23995 verifyFormat("int *i;\n"
23996 "// clang-format off:\n"
23997 "int* j;\n"
23998 "// clang-format on: 1\n"
23999 "int *k;",
24000 "int* i;\n"
24001 "// clang-format off:\n"
24002 "int* j;\n"
24003 "// clang-format on: 1\n"
24004 "int* k;");
24005
24006 verifyFormat("int *i;\n"
24007 "// clang-format off:0\n"
24008 "int* j;\n"
24009 "// clang-format only\n"
24010 "int* k;",
24011 "int* i;\n"
24012 "// clang-format off:0\n"
24013 "int* j;\n"
24014 "// clang-format only\n"
24015 "int* k;");
24016
24017 verifyNoChange("// clang-format off\n"
24018 "#if 0\n"
24019 " #if SHOULD_STAY_INDENTED\n"
24020 " #endif\n"
24021 "#endif\n"
24022 "// clang-format on");
24023}
24024
24025TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
24026 format(Code: "? ) =");
24027 verifyNoCrash(Code: "#define a\\\n /**/}");
24028}
24029
24030TEST_F(FormatTest, FormatsTableGenCode) {
24031 FormatStyle Style = getLLVMStyle();
24032 Style.Language = FormatStyle::LK_TableGen;
24033 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
24034}
24035
24036TEST_F(FormatTest, ArrayOfTemplates) {
24037 verifyFormat("auto a = new unique_ptr<int>[10];",
24038 "auto a = new unique_ptr<int > [ 10];");
24039
24040 FormatStyle Spaces = getLLVMStyle();
24041 Spaces.SpacesInSquareBrackets = true;
24042 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24043 "auto a = new unique_ptr<int > [10];", Spaces);
24044}
24045
24046TEST_F(FormatTest, ArrayAsTemplateType) {
24047 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24048 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24049
24050 FormatStyle Spaces = getLLVMStyle();
24051 Spaces.SpacesInSquareBrackets = true;
24052 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24053 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
24054}
24055
24056TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
24057
24058TEST_F(FormatTest, FormatSortsUsingDeclarations) {
24059 verifyFormat("using std::cin;\n"
24060 "using std::cout;",
24061 "using std::cout;\n"
24062 "using std::cin;",
24063 getGoogleStyle());
24064}
24065
24066TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
24067 FormatStyle Style = getLLVMStyle();
24068 Style.Standard = FormatStyle::LS_Cpp03;
24069 // cpp03 recognize this string as identifier u8 and literal character 'a'
24070 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
24071}
24072
24073TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
24074 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24075 // all modes, including C++11, C++14 and C++17
24076 verifyFormat("auto c = u8'a';");
24077}
24078
24079TEST_F(FormatTest, DoNotFormatLikelyXml) {
24080 verifyGoogleFormat("<!-- ;> -->");
24081 verifyNoChange(" <!-- >; -->", getGoogleStyle());
24082}
24083
24084TEST_F(FormatTest, StructuredBindings) {
24085 // Structured bindings is a C++17 feature.
24086 // all modes, including C++11, C++14 and C++17
24087 verifyFormat("auto [a, b] = f();");
24088 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24089 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
24090 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
24091 verifyFormat("auto const volatile [a, b] = f();",
24092 "auto const volatile[a, b] = f();");
24093 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
24094 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
24095 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
24096 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
24097 verifyFormat("auto const volatile &&[a, b] = f();",
24098 "auto const volatile &&[a, b] = f();");
24099 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
24100 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
24101 verifyFormat("const auto volatile &&[a, b] = f();",
24102 "const auto volatile &&[a, b] = f();");
24103 verifyFormat("volatile const auto &&[a, b] = f();",
24104 "volatile const auto &&[a, b] = f();");
24105 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
24106
24107 // Make sure we don't mistake structured bindings for lambdas.
24108 FormatStyle PointerMiddle = getLLVMStyle();
24109 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
24110 verifyGoogleFormat("auto [a1, b]{A * i};");
24111 verifyFormat("auto [a2, b]{A * i};");
24112 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
24113 verifyGoogleFormat("auto const [a1, b]{A * i};");
24114 verifyFormat("auto const [a2, b]{A * i};");
24115 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
24116 verifyGoogleFormat("auto const& [a1, b]{A * i};");
24117 verifyFormat("auto const &[a2, b]{A * i};");
24118 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
24119 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24120 verifyFormat("auto const &&[a2, b]{A * i};");
24121 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
24122
24123 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24124 "for (const auto && [a, b] : some_range) {\n}");
24125 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24126 "for (const auto & [a, b] : some_range) {\n}");
24127 verifyFormat("for (const auto [a, b] : some_range) {\n}",
24128 "for (const auto[a, b] : some_range) {\n}");
24129 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
24130 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
24131 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
24132 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
24133 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
24134 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
24135 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
24136 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
24137
24138 FormatStyle Spaces = getLLVMStyle();
24139 Spaces.SpacesInSquareBrackets = true;
24140 verifyFormat("auto [ a, b ] = f();", Spaces);
24141 verifyFormat("auto &&[ a, b ] = f();", Spaces);
24142 verifyFormat("auto &[ a, b ] = f();", Spaces);
24143 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
24144 verifyFormat("auto const &[ a, b ] = f();", Spaces);
24145}
24146
24147TEST_F(FormatTest, FileAndCode) {
24148 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
24149 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
24150 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
24151 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
24152 EXPECT_EQ(FormatStyle::LK_ObjC,
24153 guessLanguage("foo.h", "@interface Foo\n@end"));
24154 EXPECT_EQ(
24155 FormatStyle::LK_ObjC,
24156 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24157 EXPECT_EQ(FormatStyle::LK_ObjC,
24158 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24159 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
24160 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
24161 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
24162 EXPECT_EQ(FormatStyle::LK_ObjC,
24163 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24164 EXPECT_EQ(FormatStyle::LK_ObjC,
24165 guessLanguage(
24166 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24167 EXPECT_EQ(
24168 FormatStyle::LK_Cpp,
24169 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24170 // Only one of the two preprocessor regions has ObjC-like code.
24171 EXPECT_EQ(FormatStyle::LK_ObjC,
24172 guessLanguage("foo.h", "#if A\n"
24173 "#define B() C\n"
24174 "#else\n"
24175 "#define B() [NSString a:@\"\"]\n"
24176 "#endif"));
24177}
24178
24179TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
24180 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
24181 EXPECT_EQ(FormatStyle::LK_ObjC,
24182 guessLanguage("foo.h", "array[[calculator getIndex]];"));
24183 EXPECT_EQ(FormatStyle::LK_Cpp,
24184 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24185 EXPECT_EQ(
24186 FormatStyle::LK_Cpp,
24187 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24188 EXPECT_EQ(FormatStyle::LK_ObjC,
24189 guessLanguage("foo.h", "[[noreturn foo] bar];"));
24190 EXPECT_EQ(FormatStyle::LK_Cpp,
24191 guessLanguage("foo.h", "[[clang::fallthrough]];"));
24192 EXPECT_EQ(FormatStyle::LK_ObjC,
24193 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24194 EXPECT_EQ(FormatStyle::LK_Cpp,
24195 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24196 EXPECT_EQ(FormatStyle::LK_Cpp,
24197 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24198 EXPECT_EQ(FormatStyle::LK_ObjC,
24199 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24200 EXPECT_EQ(FormatStyle::LK_Cpp,
24201 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24202 EXPECT_EQ(
24203 FormatStyle::LK_Cpp,
24204 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24205 EXPECT_EQ(
24206 FormatStyle::LK_Cpp,
24207 guessLanguage("foo.h",
24208 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24209 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24210}
24211
24212TEST_F(FormatTest, GuessLanguageWithCaret) {
24213 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
24214 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
24215 EXPECT_EQ(FormatStyle::LK_ObjC,
24216 guessLanguage("foo.h", "int(^)(char, float);"));
24217 EXPECT_EQ(FormatStyle::LK_ObjC,
24218 guessLanguage("foo.h", "int(^foo)(char, float);"));
24219 EXPECT_EQ(FormatStyle::LK_ObjC,
24220 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24221 EXPECT_EQ(FormatStyle::LK_ObjC,
24222 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24223 EXPECT_EQ(
24224 FormatStyle::LK_ObjC,
24225 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24226}
24227
24228TEST_F(FormatTest, GuessLanguageWithPragmas) {
24229 EXPECT_EQ(FormatStyle::LK_Cpp,
24230 guessLanguage("foo.h", "__pragma(warning(disable:))"));
24231 EXPECT_EQ(FormatStyle::LK_Cpp,
24232 guessLanguage("foo.h", "#pragma(warning(disable:))"));
24233 EXPECT_EQ(FormatStyle::LK_Cpp,
24234 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24235}
24236
24237TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
24238 // ASM symbolic names are identifiers that must be surrounded by [] without
24239 // space in between:
24240 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24241
24242 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24243 verifyFormat(R"(//
24244asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
24245)");
24246
24247 // A list of several ASM symbolic names.
24248 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
24249
24250 // ASM symbolic names in inline ASM with inputs and outputs.
24251 verifyFormat(R"(//
24252asm("cmoveq %1, %2, %[result]"
24253 : [result] "=r"(result)
24254 : "r"(test), "r"(new), "[result]"(old));
24255)");
24256
24257 // ASM symbolic names in inline ASM with no outputs.
24258 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
24259}
24260
24261TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
24262 EXPECT_EQ(FormatStyle::LK_Cpp,
24263 guessLanguage("foo.h", "void f() {\n"
24264 " asm (\"mov %[e], %[d]\"\n"
24265 " : [d] \"=rm\" (d)\n"
24266 " [e] \"rm\" (*e));\n"
24267 "}"));
24268 EXPECT_EQ(FormatStyle::LK_Cpp,
24269 guessLanguage("foo.h", "void f() {\n"
24270 " _asm (\"mov %[e], %[d]\"\n"
24271 " : [d] \"=rm\" (d)\n"
24272 " [e] \"rm\" (*e));\n"
24273 "}"));
24274 EXPECT_EQ(FormatStyle::LK_Cpp,
24275 guessLanguage("foo.h", "void f() {\n"
24276 " __asm (\"mov %[e], %[d]\"\n"
24277 " : [d] \"=rm\" (d)\n"
24278 " [e] \"rm\" (*e));\n"
24279 "}"));
24280 EXPECT_EQ(FormatStyle::LK_Cpp,
24281 guessLanguage("foo.h", "void f() {\n"
24282 " __asm__ (\"mov %[e], %[d]\"\n"
24283 " : [d] \"=rm\" (d)\n"
24284 " [e] \"rm\" (*e));\n"
24285 "}"));
24286 EXPECT_EQ(FormatStyle::LK_Cpp,
24287 guessLanguage("foo.h", "void f() {\n"
24288 " asm (\"mov %[e], %[d]\"\n"
24289 " : [d] \"=rm\" (d),\n"
24290 " [e] \"rm\" (*e));\n"
24291 "}"));
24292 EXPECT_EQ(FormatStyle::LK_Cpp,
24293 guessLanguage("foo.h", "void f() {\n"
24294 " asm volatile (\"mov %[e], %[d]\"\n"
24295 " : [d] \"=rm\" (d)\n"
24296 " [e] \"rm\" (*e));\n"
24297 "}"));
24298}
24299
24300TEST_F(FormatTest, GuessLanguageWithChildLines) {
24301 EXPECT_EQ(FormatStyle::LK_Cpp,
24302 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24303 EXPECT_EQ(FormatStyle::LK_ObjC,
24304 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24305 EXPECT_EQ(
24306 FormatStyle::LK_Cpp,
24307 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24308 EXPECT_EQ(
24309 FormatStyle::LK_ObjC,
24310 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24311}
24312
24313TEST_F(FormatTest, TypenameMacros) {
24314 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24315
24316 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24317 FormatStyle Google = getGoogleStyleWithColumns(ColumnLimit: 0);
24318 Google.TypenameMacros = TypenameMacros;
24319 verifyFormat("struct foo {\n"
24320 " int bar;\n"
24321 " TAILQ_ENTRY(a) bleh;\n"
24322 "};",
24323 Google);
24324
24325 FormatStyle Macros = getLLVMStyle();
24326 Macros.TypenameMacros = TypenameMacros;
24327
24328 verifyFormat("STACK_OF(int) a;", Macros);
24329 verifyFormat("STACK_OF(int) *a;", Macros);
24330 verifyFormat("STACK_OF(int const *) *a;", Macros);
24331 verifyFormat("STACK_OF(int *const) *a;", Macros);
24332 verifyFormat("STACK_OF(int, string) a;", Macros);
24333 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
24334 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
24335 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
24336 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
24337 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
24338 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
24339
24340 Macros.PointerAlignment = FormatStyle::PAS_Left;
24341 verifyFormat("STACK_OF(int)* a;", Macros);
24342 verifyFormat("STACK_OF(int*)* a;", Macros);
24343 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
24344 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
24345 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
24346}
24347
24348TEST_F(FormatTest, AtomicQualifier) {
24349 // Check that we treate _Atomic as a type and not a function call
24350 FormatStyle Google = getGoogleStyleWithColumns(ColumnLimit: 0);
24351 verifyFormat("struct foo {\n"
24352 " int a1;\n"
24353 " _Atomic(a) a2;\n"
24354 " _Atomic(_Atomic(int) *const) a3;\n"
24355 "};",
24356 Google);
24357 verifyFormat("_Atomic(uint64_t) a;");
24358 verifyFormat("_Atomic(uint64_t) *a;");
24359 verifyFormat("_Atomic(uint64_t const *) *a;");
24360 verifyFormat("_Atomic(uint64_t *const) *a;");
24361 verifyFormat("_Atomic(const uint64_t *) *a;");
24362 verifyFormat("_Atomic(uint64_t) a;");
24363 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24364 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24365 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24366 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24367
24368 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24369 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24370 FormatStyle Style = getLLVMStyle();
24371 Style.PointerAlignment = FormatStyle::PAS_Left;
24372 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
24373 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
24374 verifyFormat("_Atomic(int)* a;", Style);
24375 verifyFormat("_Atomic(int*)* a;", Style);
24376 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
24377
24378 Style.SpacesInParens = FormatStyle::SIPO_Custom;
24379 Style.SpacesInParensOptions.InCStyleCasts = true;
24380 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
24381 Style.SpacesInParensOptions.InCStyleCasts = false;
24382 Style.SpacesInParensOptions.Other = true;
24383 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
24384 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
24385}
24386
24387TEST_F(FormatTest, C11Generic) {
24388 verifyFormat("_Generic(x, int: 1, default: 0)");
24389 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24390 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24391 verifyFormat("_Generic(x, int: f1, const int: f2)();");
24392 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24393
24394 verifyFormat("_Generic(x,\n"
24395 " float: f,\n"
24396 " default: d,\n"
24397 " long double: ld,\n"
24398 " float _Complex: fc,\n"
24399 " double _Complex: dc,\n"
24400 " long double _Complex: ldc)");
24401
24402 verifyFormat("while (_Generic(x, //\n"
24403 " long: x)(x) > x) {\n"
24404 "}");
24405 verifyFormat("while (_Generic(x, //\n"
24406 " long: x)(x)) {\n"
24407 "}");
24408 verifyFormat("x(_Generic(x, //\n"
24409 " long: x)(x));");
24410
24411 FormatStyle Style = getLLVMStyle();
24412 Style.ColumnLimit = 40;
24413 verifyFormat("#define LIMIT_MAX(T) \\\n"
24414 " _Generic(((T)0), \\\n"
24415 " unsigned int: UINT_MAX, \\\n"
24416 " unsigned long: ULONG_MAX, \\\n"
24417 " unsigned long long: ULLONG_MAX)",
24418 Style);
24419 verifyFormat("_Generic(x,\n"
24420 " struct A: 1,\n"
24421 " void (*)(void): 2);",
24422 Style);
24423
24424 Style.ContinuationIndentWidth = 2;
24425 verifyFormat("_Generic(x,\n"
24426 " struct A: 1,\n"
24427 " void (*)(void): 2);",
24428 Style);
24429}
24430
24431TEST_F(FormatTest, AmbersandInLamda) {
24432 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24433 FormatStyle AlignStyle = getLLVMStyle();
24434 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
24435 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24436 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
24437 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
24438}
24439
24440TEST_F(FormatTest, TrailingReturnTypeAuto) {
24441 FormatStyle Style = getLLVMStyle();
24442 verifyFormat("[]() -> auto { return Val; }", Style);
24443 verifyFormat("[]() -> auto * { return Val; }", Style);
24444 verifyFormat("[]() -> auto & { return Val; }", Style);
24445 verifyFormat("auto foo() -> auto { return Val; }", Style);
24446 verifyFormat("auto foo() -> auto * { return Val; }", Style);
24447 verifyFormat("auto foo() -> auto & { return Val; }", Style);
24448}
24449
24450TEST_F(FormatTest, SpacesInConditionalStatement) {
24451 FormatStyle Spaces = getLLVMStyle();
24452 Spaces.IfMacros.clear();
24453 Spaces.IfMacros.push_back(x: "MYIF");
24454 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
24455 Spaces.SpacesInParensOptions.InConditionalStatements = true;
24456 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
24457 verifyFormat("if ( !a )\n return;", Spaces);
24458 verifyFormat("if ( a )\n return;", Spaces);
24459 verifyFormat("if constexpr ( a )\n return;", Spaces);
24460 verifyFormat("MYIF ( a )\n return;", Spaces);
24461 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
24462 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
24463 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
24464 verifyFormat("while ( a )\n return;", Spaces);
24465 verifyFormat("while ( (a && b) )\n return;", Spaces);
24466 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
24467 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
24468 // Check that space on the left of "::" is inserted as expected at beginning
24469 // of condition.
24470 verifyFormat("while ( ::func() )\n return;", Spaces);
24471
24472 // Check impact of ControlStatementsExceptControlMacros is honored.
24473 Spaces.SpaceBeforeParens =
24474 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
24475 verifyFormat("MYIF( a )\n return;", Spaces);
24476 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
24477 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
24478}
24479
24480TEST_F(FormatTest, AlternativeOperators) {
24481 // Test case for ensuring alternate operators are not
24482 // combined with their right most neighbour.
24483 verifyFormat("int a and b;");
24484 verifyFormat("int a and_eq b;");
24485 verifyFormat("int a bitand b;");
24486 verifyFormat("int a bitor b;");
24487 verifyFormat("int a compl b;");
24488 verifyFormat("int a not b;");
24489 verifyFormat("int a not_eq b;");
24490 verifyFormat("int a or b;");
24491 verifyFormat("int a xor b;");
24492 verifyFormat("int a xor_eq b;");
24493 verifyFormat("return this not_eq bitand other;");
24494 verifyFormat("bool operator not_eq(const X bitand other)");
24495
24496 verifyFormat("int a and 5;");
24497 verifyFormat("int a and_eq 5;");
24498 verifyFormat("int a bitand 5;");
24499 verifyFormat("int a bitor 5;");
24500 verifyFormat("int a compl 5;");
24501 verifyFormat("int a not 5;");
24502 verifyFormat("int a not_eq 5;");
24503 verifyFormat("int a or 5;");
24504 verifyFormat("int a xor 5;");
24505 verifyFormat("int a xor_eq 5;");
24506
24507 verifyFormat("int a compl(5);");
24508 verifyFormat("int a not(5);");
24509
24510 /* FIXME handle alternate tokens
24511 * https://en.cppreference.com/w/cpp/language/operator_alternative
24512 // alternative tokens
24513 verifyFormat("compl foo();"); // ~foo();
24514 verifyFormat("foo() <%%>;"); // foo();
24515 verifyFormat("void foo() <%%>;"); // void foo(){}
24516 verifyFormat("int a <:1:>;"); // int a[1];[
24517 verifyFormat("%:define ABC abc"); // #define ABC abc
24518 verifyFormat("%:%:"); // ##
24519 */
24520}
24521
24522TEST_F(FormatTest, STLWhileNotDefineChed) {
24523 verifyFormat("#if defined(while)\n"
24524 "#define while EMIT WARNING C4005\n"
24525 "#endif // while");
24526}
24527
24528TEST_F(FormatTest, OperatorSpacing) {
24529 FormatStyle Style = getLLVMStyle();
24530 Style.PointerAlignment = FormatStyle::PAS_Right;
24531 verifyFormat("Foo::operator*();", Style);
24532 verifyFormat("Foo::operator void *();", Style);
24533 verifyFormat("Foo::operator void **();", Style);
24534 verifyFormat("Foo::operator void *&();", Style);
24535 verifyFormat("Foo::operator void *&&();", Style);
24536 verifyFormat("Foo::operator void const *();", Style);
24537 verifyFormat("Foo::operator void const **();", Style);
24538 verifyFormat("Foo::operator void const *&();", Style);
24539 verifyFormat("Foo::operator void const *&&();", Style);
24540 verifyFormat("Foo::operator()(void *);", Style);
24541 verifyFormat("Foo::operator*(void *);", Style);
24542 verifyFormat("Foo::operator*();", Style);
24543 verifyFormat("Foo::operator**();", Style);
24544 verifyFormat("Foo::operator&();", Style);
24545 verifyFormat("Foo::operator<int> *();", Style);
24546 verifyFormat("Foo::operator<Foo> *();", Style);
24547 verifyFormat("Foo::operator<int> **();", Style);
24548 verifyFormat("Foo::operator<Foo> **();", Style);
24549 verifyFormat("Foo::operator<int> &();", Style);
24550 verifyFormat("Foo::operator<Foo> &();", Style);
24551 verifyFormat("Foo::operator<int> &&();", Style);
24552 verifyFormat("Foo::operator<Foo> &&();", Style);
24553 verifyFormat("Foo::operator<int> *&();", Style);
24554 verifyFormat("Foo::operator<Foo> *&();", Style);
24555 verifyFormat("Foo::operator<int> *&&();", Style);
24556 verifyFormat("Foo::operator<Foo> *&&();", Style);
24557 verifyFormat("operator*(int (*)(), class Foo);", Style);
24558
24559 verifyFormat("Foo::operator&();", Style);
24560 verifyFormat("Foo::operator void &();", Style);
24561 verifyFormat("Foo::operator void const &();", Style);
24562 verifyFormat("Foo::operator()(void &);", Style);
24563 verifyFormat("Foo::operator&(void &);", Style);
24564 verifyFormat("Foo::operator&();", Style);
24565 verifyFormat("operator&(int (&)(), class Foo);", Style);
24566 verifyFormat("operator&&(int (&)(), class Foo);", Style);
24567
24568 verifyFormat("Foo::operator&&();", Style);
24569 verifyFormat("Foo::operator**();", Style);
24570 verifyFormat("Foo::operator void &&();", Style);
24571 verifyFormat("Foo::operator void const &&();", Style);
24572 verifyFormat("Foo::operator()(void &&);", Style);
24573 verifyFormat("Foo::operator&&(void &&);", Style);
24574 verifyFormat("Foo::operator&&();", Style);
24575 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24576 verifyFormat("operator const nsTArrayRight<E> &()", Style);
24577 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24578 Style);
24579 verifyFormat("operator void **()", Style);
24580 verifyFormat("operator const FooRight<Object> &()", Style);
24581 verifyFormat("operator const FooRight<Object> *()", Style);
24582 verifyFormat("operator const FooRight<Object> **()", Style);
24583 verifyFormat("operator const FooRight<Object> *&()", Style);
24584 verifyFormat("operator const FooRight<Object> *&&()", Style);
24585
24586 Style.PointerAlignment = FormatStyle::PAS_Left;
24587 verifyFormat("Foo::operator*();", Style);
24588 verifyFormat("Foo::operator**();", Style);
24589 verifyFormat("Foo::operator void*();", Style);
24590 verifyFormat("Foo::operator void**();", Style);
24591 verifyFormat("Foo::operator void*&();", Style);
24592 verifyFormat("Foo::operator void*&&();", Style);
24593 verifyFormat("Foo::operator void const*();", Style);
24594 verifyFormat("Foo::operator void const**();", Style);
24595 verifyFormat("Foo::operator void const*&();", Style);
24596 verifyFormat("Foo::operator void const*&&();", Style);
24597 verifyFormat("Foo::operator/*comment*/ void*();", Style);
24598 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
24599 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
24600 verifyFormat("Foo::operator()(void*);", Style);
24601 verifyFormat("Foo::operator*(void*);", Style);
24602 verifyFormat("Foo::operator*();", Style);
24603 verifyFormat("Foo::operator<int>*();", Style);
24604 verifyFormat("Foo::operator<Foo>*();", Style);
24605 verifyFormat("Foo::operator<int>**();", Style);
24606 verifyFormat("Foo::operator<Foo>**();", Style);
24607 verifyFormat("Foo::operator<Foo>*&();", Style);
24608 verifyFormat("Foo::operator<int>&();", Style);
24609 verifyFormat("Foo::operator<Foo>&();", Style);
24610 verifyFormat("Foo::operator<int>&&();", Style);
24611 verifyFormat("Foo::operator<Foo>&&();", Style);
24612 verifyFormat("Foo::operator<int>*&();", Style);
24613 verifyFormat("Foo::operator<Foo>*&();", Style);
24614 verifyFormat("operator*(int (*)(), class Foo);", Style);
24615
24616 verifyFormat("Foo::operator&();", Style);
24617 verifyFormat("Foo::operator void&();", Style);
24618 verifyFormat("Foo::operator void const&();", Style);
24619 verifyFormat("Foo::operator/*comment*/ void&();", Style);
24620 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
24621 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
24622 verifyFormat("Foo::operator()(void&);", Style);
24623 verifyFormat("Foo::operator&(void&);", Style);
24624 verifyFormat("Foo::operator&();", Style);
24625 verifyFormat("operator&(int (&)(), class Foo);", Style);
24626 verifyFormat("operator&(int (&&)(), class Foo);", Style);
24627 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24628
24629 verifyFormat("Foo::operator&&();", Style);
24630 verifyFormat("Foo::operator void&&();", Style);
24631 verifyFormat("Foo::operator void const&&();", Style);
24632 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
24633 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
24634 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
24635 verifyFormat("Foo::operator()(void&&);", Style);
24636 verifyFormat("Foo::operator&&(void&&);", Style);
24637 verifyFormat("Foo::operator&&();", Style);
24638 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24639 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
24640 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
24641 Style);
24642 verifyFormat("operator void**()", Style);
24643 verifyFormat("operator const FooLeft<Object>&()", Style);
24644 verifyFormat("operator const FooLeft<Object>*()", Style);
24645 verifyFormat("operator const FooLeft<Object>**()", Style);
24646 verifyFormat("operator const FooLeft<Object>*&()", Style);
24647 verifyFormat("operator const FooLeft<Object>*&&()", Style);
24648
24649 // PR45107
24650 verifyFormat("operator Vector<String>&();", Style);
24651 verifyFormat("operator const Vector<String>&();", Style);
24652 verifyFormat("operator foo::Bar*();", Style);
24653 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
24654 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
24655 Style);
24656
24657 Style.PointerAlignment = FormatStyle::PAS_Middle;
24658 verifyFormat("Foo::operator*();", Style);
24659 verifyFormat("Foo::operator void *();", Style);
24660 verifyFormat("Foo::operator()(void *);", Style);
24661 verifyFormat("Foo::operator*(void *);", Style);
24662 verifyFormat("Foo::operator*();", Style);
24663 verifyFormat("operator*(int (*)(), class Foo);", Style);
24664
24665 verifyFormat("Foo::operator&();", Style);
24666 verifyFormat("Foo::operator void &();", Style);
24667 verifyFormat("Foo::operator void const &();", Style);
24668 verifyFormat("Foo::operator()(void &);", Style);
24669 verifyFormat("Foo::operator&(void &);", Style);
24670 verifyFormat("Foo::operator&();", Style);
24671 verifyFormat("operator&(int (&)(), class Foo);", Style);
24672
24673 verifyFormat("Foo::operator&&();", Style);
24674 verifyFormat("Foo::operator void &&();", Style);
24675 verifyFormat("Foo::operator void const &&();", Style);
24676 verifyFormat("Foo::operator()(void &&);", Style);
24677 verifyFormat("Foo::operator&&(void &&);", Style);
24678 verifyFormat("Foo::operator&&();", Style);
24679 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
24680}
24681
24682TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
24683 FormatStyle Style = getLLVMStyle();
24684 // PR46157
24685 verifyFormat("foo(operator+, -42);", Style);
24686 verifyFormat("foo(operator++, -42);", Style);
24687 verifyFormat("foo(operator--, -42);", Style);
24688 verifyFormat("foo(-42, operator--);", Style);
24689 verifyFormat("foo(-42, operator, );", Style);
24690 verifyFormat("foo(operator, , -42);", Style);
24691}
24692
24693TEST_F(FormatTest, WhitespaceSensitiveMacros) {
24694 FormatStyle Style = getLLVMStyle();
24695 Style.WhitespaceSensitiveMacros.push_back(x: "FOO");
24696
24697 // Newlines are important here.
24698 verifyNoChange("FOO(1+2 )\n", Style);
24699 verifyNoChange("FOO(a:b:c)\n", Style);
24700
24701 // Don't use the helpers here, since 'mess up' will change the whitespace
24702 // and these are all whitespace sensitive by definition
24703 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
24704 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
24705 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
24706 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
24707 " Still=Intentional);",
24708 Style);
24709 Style.AlignConsecutiveAssignments.Enabled = true;
24710 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
24711 " Still=Intentional);",
24712 Style);
24713
24714 Style.ColumnLimit = 21;
24715 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
24716}
24717
24718TEST_F(FormatTest, SkipMacroDefinitionBody) {
24719 auto Style = getLLVMStyle();
24720 Style.SkipMacroDefinitionBody = true;
24721
24722 verifyFormat("#define A", "#define A", Style);
24723 verifyFormat("#define A a aa", "#define A a aa", Style);
24724 verifyNoChange("#define A b", Style);
24725 verifyNoChange("#define A ( args )", Style);
24726 verifyNoChange("#define A ( args ) = func ( args )", Style);
24727 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
24728 verifyNoChange("#define A ( args ) \\\n"
24729 " {\\\n"
24730 " int a = 1 ;\\\n"
24731 "}",
24732 Style);
24733
24734 verifyNoChange("#define A x:", Style);
24735 verifyNoChange("#define A a. b", Style);
24736
24737 // Surrounded with formatted code.
24738 verifyFormat("int a;\n"
24739 "#define A a\n"
24740 "int a;",
24741 "int a ;\n"
24742 "#define A a\n"
24743 "int a ;",
24744 Style);
24745
24746 // Columns are not broken when a limit is set.
24747 Style.ColumnLimit = 10;
24748 verifyFormat("#define A a a a a", " # define A a a a a ", Style);
24749 verifyNoChange("#define A a a a a", Style);
24750
24751 Style.ColumnLimit = 15;
24752 verifyFormat("#define A // a\n"
24753 " // very\n"
24754 " // long\n"
24755 " // comment",
24756 "#define A //a very long comment", Style);
24757 Style.ColumnLimit = 0;
24758
24759 // Multiline definition.
24760 verifyNoChange("#define A \\\n"
24761 "Line one with spaces . \\\n"
24762 " Line two.",
24763 Style);
24764 verifyNoChange("#define A \\\n"
24765 "a a \\\n"
24766 "a \\\n"
24767 "a",
24768 Style);
24769 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
24770 verifyNoChange("#define A \\\n"
24771 "a a \\\n"
24772 "a \\\n"
24773 "a",
24774 Style);
24775 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
24776 verifyNoChange("#define A \\\n"
24777 "a a \\\n"
24778 "a \\\n"
24779 "a",
24780 Style);
24781
24782 // Adjust indendations but don't change the definition.
24783 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
24784 verifyNoChange("#if A\n"
24785 "#define A a\n"
24786 "#endif",
24787 Style);
24788 verifyFormat("#if A\n"
24789 "#define A a\n"
24790 "#endif",
24791 "#if A\n"
24792 " #define A a\n"
24793 "#endif",
24794 Style);
24795 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
24796 verifyNoChange("#if A\n"
24797 "# define A a\n"
24798 "#endif",
24799 Style);
24800 verifyFormat("#if A\n"
24801 "# define A a\n"
24802 "#endif",
24803 "#if A\n"
24804 " #define A a\n"
24805 "#endif",
24806 Style);
24807 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
24808 verifyNoChange("#if A\n"
24809 " #define A a\n"
24810 "#endif",
24811 Style);
24812 verifyFormat("#if A\n"
24813 " #define A a\n"
24814 "#endif",
24815 "#if A\n"
24816 " # define A a\n"
24817 "#endif",
24818 Style);
24819
24820 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
24821 // SkipMacroDefinitionBody should not affect other PP directives
24822 verifyFormat("#if !defined(A)\n"
24823 "#define A a\n"
24824 "#endif",
24825 "#if ! defined ( A )\n"
24826 " #define A a\n"
24827 "#endif",
24828 Style);
24829
24830 // With comments.
24831 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
24832 Style);
24833 verifyNoChange("/* */ #define A a // a a", Style);
24834
24835 verifyFormat("int a; // a\n"
24836 "#define A // a\n"
24837 "int aaa; // a",
24838 "int a; // a\n"
24839 "#define A // a\n"
24840 "int aaa; // a",
24841 Style);
24842
24843 // multiline macro definitions
24844 verifyNoChange("#define A a\\\n"
24845 " A a \\\n "
24846 " A a",
24847 Style);
24848}
24849
24850TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
24851 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
24852 // test its interaction with line wrapping
24853 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80);
24854 verifyFormat("namespace {\n"
24855 "int i;\n"
24856 "int j;\n"
24857 "} // namespace",
24858 Style);
24859
24860 verifyFormat("namespace AAA {\n"
24861 "int i;\n"
24862 "int j;\n"
24863 "} // namespace AAA",
24864 Style);
24865
24866 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
24867 "int i;\n"
24868 "int j;\n"
24869 "} // namespace Averyveryveryverylongnamespace",
24870 "namespace Averyveryveryverylongnamespace {\n"
24871 "int i;\n"
24872 "int j;\n"
24873 "}",
24874 Style);
24875
24876 verifyFormat(
24877 "namespace "
24878 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
24879 " went::mad::now {\n"
24880 "int i;\n"
24881 "int j;\n"
24882 "} // namespace\n"
24883 " // "
24884 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
24885 "went::mad::now",
24886 "namespace "
24887 "would::it::save::you::a::lot::of::time::if_::i::"
24888 "just::gave::up::and_::went::mad::now {\n"
24889 "int i;\n"
24890 "int j;\n"
24891 "}",
24892 Style);
24893
24894 // This used to duplicate the comment again and again on subsequent runs
24895 verifyFormat(
24896 "namespace "
24897 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
24898 " went::mad::now {\n"
24899 "int i;\n"
24900 "int j;\n"
24901 "} // namespace\n"
24902 " // "
24903 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
24904 "went::mad::now",
24905 "namespace "
24906 "would::it::save::you::a::lot::of::time::if_::i::"
24907 "just::gave::up::and_::went::mad::now {\n"
24908 "int i;\n"
24909 "int j;\n"
24910 "} // namespace\n"
24911 " // "
24912 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
24913 "and_::went::mad::now",
24914 Style);
24915}
24916
24917TEST_F(FormatTest, LikelyUnlikely) {
24918 FormatStyle Style = getLLVMStyle();
24919
24920 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24921 " return 29;\n"
24922 "}",
24923 Style);
24924
24925 verifyFormat("if (argc > 5) [[likely]] {\n"
24926 " return 29;\n"
24927 "}",
24928 Style);
24929
24930 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24931 " return 29;\n"
24932 "} else [[likely]] {\n"
24933 " return 42;\n"
24934 "}",
24935 Style);
24936
24937 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24938 " return 29;\n"
24939 "} else if (argc > 10) [[likely]] {\n"
24940 " return 99;\n"
24941 "} else {\n"
24942 " return 42;\n"
24943 "}",
24944 Style);
24945
24946 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
24947 " return 29;\n"
24948 "}",
24949 Style);
24950
24951 verifyFormat("if (argc > 5) [[unlikely]]\n"
24952 " return 29;",
24953 Style);
24954 verifyFormat("if (argc > 5) [[likely]]\n"
24955 " return 29;",
24956 Style);
24957
24958 verifyFormat("while (limit > 0) [[unlikely]] {\n"
24959 " --limit;\n"
24960 "}",
24961 Style);
24962 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
24963 " --limit;\n"
24964 "}",
24965 Style);
24966
24967 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
24968 " --limit;",
24969 Style);
24970 verifyFormat("while (limit > 0) [[likely]]\n"
24971 " --limit;",
24972 Style);
24973
24974 Style.AttributeMacros.push_back(x: "UNLIKELY");
24975 Style.AttributeMacros.push_back(x: "LIKELY");
24976 verifyFormat("if (argc > 5) UNLIKELY\n"
24977 " return 29;",
24978 Style);
24979
24980 verifyFormat("if (argc > 5) UNLIKELY {\n"
24981 " return 29;\n"
24982 "}",
24983 Style);
24984 verifyFormat("if (argc > 5) UNLIKELY {\n"
24985 " return 29;\n"
24986 "} else [[likely]] {\n"
24987 " return 42;\n"
24988 "}",
24989 Style);
24990 verifyFormat("if (argc > 5) UNLIKELY {\n"
24991 " return 29;\n"
24992 "} else LIKELY {\n"
24993 " return 42;\n"
24994 "}",
24995 Style);
24996 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24997 " return 29;\n"
24998 "} else LIKELY {\n"
24999 " return 42;\n"
25000 "}",
25001 Style);
25002
25003 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25004 " --limit;\n"
25005 "}",
25006 Style);
25007 verifyFormat("while (limit > 0) LIKELY {\n"
25008 " --limit;\n"
25009 "}",
25010 Style);
25011
25012 verifyFormat("while (limit > 0) UNLIKELY\n"
25013 " --limit;",
25014 Style);
25015 verifyFormat("for (auto &limit : limits) LIKELY\n"
25016 " --limit;",
25017 Style);
25018}
25019
25020TEST_F(FormatTest, PenaltyIndentedWhitespace) {
25021 verifyFormat("Constructor()\n"
25022 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25023 " aaaa(aaaaaaaaaaaaaaaaaa, "
25024 "aaaaaaaaaaaaaaaaaat))");
25025 verifyFormat("Constructor()\n"
25026 " : aaaaaaaaaaaaa(aaaaaa), "
25027 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25028
25029 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
25030 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
25031 verifyFormat("Constructor()\n"
25032 " : aaaaaa(aaaaaa),\n"
25033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25034 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25035 StyleWithWhitespacePenalty);
25036 verifyFormat("Constructor()\n"
25037 " : aaaaaaaaaaaaa(aaaaaa), "
25038 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25039 StyleWithWhitespacePenalty);
25040}
25041
25042TEST_F(FormatTest, LLVMDefaultStyle) {
25043 FormatStyle Style = getLLVMStyle();
25044 verifyFormat("extern \"C\" {\n"
25045 "int foo();\n"
25046 "}",
25047 Style);
25048}
25049TEST_F(FormatTest, GNUDefaultStyle) {
25050 FormatStyle Style = getGNUStyle();
25051 verifyFormat("extern \"C\"\n"
25052 "{\n"
25053 " int foo ();\n"
25054 "}",
25055 Style);
25056}
25057TEST_F(FormatTest, MozillaDefaultStyle) {
25058 FormatStyle Style = getMozillaStyle();
25059 verifyFormat("extern \"C\"\n"
25060 "{\n"
25061 " int foo();\n"
25062 "}",
25063 Style);
25064}
25065TEST_F(FormatTest, GoogleDefaultStyle) {
25066 FormatStyle Style = getGoogleStyle();
25067 verifyFormat("extern \"C\" {\n"
25068 "int foo();\n"
25069 "}",
25070 Style);
25071}
25072TEST_F(FormatTest, ChromiumDefaultStyle) {
25073 FormatStyle Style = getChromiumStyle(Language: FormatStyle::LanguageKind::LK_Cpp);
25074 verifyFormat("extern \"C\" {\n"
25075 "int foo();\n"
25076 "}",
25077 Style);
25078}
25079TEST_F(FormatTest, MicrosoftDefaultStyle) {
25080 FormatStyle Style = getMicrosoftStyle(Language: FormatStyle::LanguageKind::LK_Cpp);
25081 verifyFormat("extern \"C\"\n"
25082 "{\n"
25083 " int foo();\n"
25084 "}",
25085 Style);
25086}
25087TEST_F(FormatTest, WebKitDefaultStyle) {
25088 FormatStyle Style = getWebKitStyle();
25089 verifyFormat("extern \"C\" {\n"
25090 "int foo();\n"
25091 "}",
25092 Style);
25093}
25094
25095TEST_F(FormatTest, Concepts) {
25096 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
25097 FormatStyle::BBCDS_Always);
25098
25099 // The default in LLVM style is REI_OuterScope, but these tests were written
25100 // when the default was REI_Keyword.
25101 FormatStyle Style = getLLVMStyle();
25102 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25103
25104 verifyFormat("template <typename T>\n"
25105 "concept True = true;");
25106
25107 verifyFormat("template <typename T>\n"
25108 "concept C = ((false || foo()) && C2<T>) ||\n"
25109 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25110 getLLVMStyleWithColumns(60));
25111
25112 verifyFormat("template <typename T>\n"
25113 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25114 "sizeof(T) <= 8;");
25115
25116 verifyFormat("template <typename T>\n"
25117 "concept DelayedCheck = true && requires(T t) {\n"
25118 " t.bar();\n"
25119 " t.baz();\n"
25120 " } && sizeof(T) <= 8;",
25121 Style);
25122
25123 verifyFormat("template <typename T>\n"
25124 "concept DelayedCheck = true && requires(T t) { // Comment\n"
25125 " t.bar();\n"
25126 " t.baz();\n"
25127 " } && sizeof(T) <= 8;",
25128 Style);
25129
25130 verifyFormat("template <typename T>\n"
25131 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25132 "sizeof(T) <= 8;");
25133
25134 verifyFormat("template <typename T>\n"
25135 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25136
25137 verifyFormat("template <typename T>\n"
25138 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25139
25140 verifyFormat("template <typename T>\n"
25141 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25142
25143 verifyFormat("template <typename T>\n"
25144 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25145 "&& sizeof(T) <= 8;");
25146
25147 verifyFormat("template <typename T>\n"
25148 "concept DelayedCheck =\n"
25149 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25150 "sizeof(T) <= 8;");
25151
25152 verifyFormat("template <typename T>\n"
25153 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25154 "&& sizeof(T) <= 8;");
25155
25156 verifyFormat(
25157 "template <typename T>\n"
25158 "concept DelayedCheck =\n"
25159 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25160
25161 verifyFormat("template <typename T>\n"
25162 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25163 "&& sizeof(T) <= 8;");
25164
25165 verifyFormat("template <typename T>\n"
25166 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25167 "sizeof(T) <= 8;");
25168
25169 verifyFormat("template <typename T>\n"
25170 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25171 " requires(T t) {\n"
25172 " t.bar();\n"
25173 " t.baz();\n"
25174 " } && sizeof(T) <= 8 && !(4 < 3);",
25175 getLLVMStyleWithColumns(60));
25176
25177 verifyFormat("template <typename T>\n"
25178 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25179
25180 verifyFormat("template <typename T>\n"
25181 "concept C = foo();");
25182
25183 verifyFormat("template <typename T>\n"
25184 "concept C = foo(T());");
25185
25186 verifyFormat("template <typename T>\n"
25187 "concept C = foo(T{});");
25188
25189 verifyFormat("template <typename T>\n"
25190 "concept Size = V<sizeof(T)>::Value > 5;");
25191
25192 verifyFormat("template <typename T>\n"
25193 "concept True = S<T>::Value;");
25194
25195 verifyFormat("template <S T>\n"
25196 "concept True = T.field;");
25197
25198 verifyFormat(
25199 "template <typename T>\n"
25200 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25201 " sizeof(T) <= 8;");
25202
25203 // FIXME: This is misformatted because the fake l paren starts at bool, not at
25204 // the lambda l square.
25205 verifyFormat("template <typename T>\n"
25206 "concept C = [] -> bool { return true; }() && requires(T t) { "
25207 "t.bar(); } &&\n"
25208 " sizeof(T) <= 8;");
25209
25210 verifyFormat(
25211 "template <typename T>\n"
25212 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25213 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25214
25215 verifyFormat("template <typename T>\n"
25216 "concept C = decltype([]() { return std::true_type{}; "
25217 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25218 getLLVMStyleWithColumns(120));
25219
25220 verifyFormat("template <typename T>\n"
25221 "concept C = decltype([]() -> std::true_type { return {}; "
25222 "}())::value &&\n"
25223 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25224
25225 verifyFormat("template <typename T>\n"
25226 "concept C = true;\n"
25227 "Foo Bar;");
25228
25229 verifyFormat("template <typename T>\n"
25230 "concept Hashable = requires(T a) {\n"
25231 " { std::hash<T>{}(a) } -> "
25232 "std::convertible_to<std::size_t>;\n"
25233 " };",
25234 Style);
25235
25236 verifyFormat(
25237 "template <typename T>\n"
25238 "concept EqualityComparable = requires(T a, T b) {\n"
25239 " { a == b } -> std::same_as<bool>;\n"
25240 " };",
25241 Style);
25242
25243 verifyFormat(
25244 "template <typename T>\n"
25245 "concept EqualityComparable = requires(T a, T b) {\n"
25246 " { a == b } -> std::same_as<bool>;\n"
25247 " { a != b } -> std::same_as<bool>;\n"
25248 " };",
25249 Style);
25250
25251 verifyFormat("template <typename T>\n"
25252 "concept WeakEqualityComparable = requires(T a, T b) {\n"
25253 " { a == b };\n"
25254 " { a != b };\n"
25255 " };",
25256 Style);
25257
25258 verifyFormat("template <typename T>\n"
25259 "concept HasSizeT = requires { typename T::size_t; };");
25260
25261 verifyFormat("template <typename T>\n"
25262 "concept Semiregular =\n"
25263 " DefaultConstructible<T> && CopyConstructible<T> && "
25264 "CopyAssignable<T> &&\n"
25265 " requires(T a, std::size_t n) {\n"
25266 " requires Same<T *, decltype(&a)>;\n"
25267 " { a.~T() } noexcept;\n"
25268 " requires Same<T *, decltype(new T)>;\n"
25269 " requires Same<T *, decltype(new T[n])>;\n"
25270 " { delete new T; };\n"
25271 " { delete new T[n]; };\n"
25272 " };",
25273 Style);
25274
25275 verifyFormat("template <typename T>\n"
25276 "concept Semiregular =\n"
25277 " requires(T a, std::size_t n) {\n"
25278 " requires Same<T *, decltype(&a)>;\n"
25279 " { a.~T() } noexcept;\n"
25280 " requires Same<T *, decltype(new T)>;\n"
25281 " requires Same<T *, decltype(new T[n])>;\n"
25282 " { delete new T; };\n"
25283 " { delete new T[n]; };\n"
25284 " { new T } -> std::same_as<T *>;\n"
25285 " } && DefaultConstructible<T> && CopyConstructible<T> && "
25286 "CopyAssignable<T>;",
25287 Style);
25288
25289 verifyFormat(
25290 "template <typename T>\n"
25291 "concept Semiregular =\n"
25292 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
25293 " requires Same<T *, decltype(&a)>;\n"
25294 " { a.~T() } noexcept;\n"
25295 " requires Same<T *, decltype(new T)>;\n"
25296 " requires Same<T *, decltype(new "
25297 "T[n])>;\n"
25298 " { delete new T; };\n"
25299 " { delete new T[n]; };\n"
25300 " } && CopyConstructible<T> && "
25301 "CopyAssignable<T>;",
25302 Style);
25303
25304 verifyFormat("template <typename T>\n"
25305 "concept Two = requires(T t) {\n"
25306 " { t.foo() } -> std::same_as<Bar>;\n"
25307 " } && requires(T &&t) {\n"
25308 " { t.foo() } -> std::same_as<Bar &&>;\n"
25309 " };",
25310 Style);
25311
25312 verifyFormat(
25313 "template <typename T>\n"
25314 "concept C = requires(T x) {\n"
25315 " { *x } -> std::convertible_to<typename T::inner>;\n"
25316 " { x + 1 } noexcept -> std::same_as<int>;\n"
25317 " { x * 1 } -> std::convertible_to<T>;\n"
25318 " };",
25319 Style);
25320
25321 verifyFormat("template <typename T>\n"
25322 "concept C = requires(T x) {\n"
25323 " {\n"
25324 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25325 " } -> long_long_concept_name<T>;\n"
25326 " {\n"
25327 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25328 " } noexcept -> long_long_concept_name<T>;\n"
25329 " };",
25330 Style);
25331
25332 verifyFormat(
25333 "template <typename T, typename U = T>\n"
25334 "concept Swappable = requires(T &&t, U &&u) {\n"
25335 " swap(std::forward<T>(t), std::forward<U>(u));\n"
25336 " swap(std::forward<U>(u), std::forward<T>(t));\n"
25337 " };",
25338 Style);
25339
25340 verifyFormat("template <typename T, typename U>\n"
25341 "concept Common = requires(T &&t, U &&u) {\n"
25342 " typename CommonType<T, U>;\n"
25343 " { CommonType<T, U>(std::forward<T>(t)) };\n"
25344 " };",
25345 Style);
25346
25347 verifyFormat("template <typename T, typename U>\n"
25348 "concept Common = requires(T &&t, U &&u) {\n"
25349 " typename CommonType<T, U>;\n"
25350 " { CommonType<T, U>{std::forward<T>(t)} };\n"
25351 " };",
25352 Style);
25353
25354 verifyFormat(
25355 "template <typename T>\n"
25356 "concept C = requires(T t) {\n"
25357 " requires Bar<T> && Foo<T>;\n"
25358 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25359 " };",
25360 Style);
25361
25362 verifyFormat("template <typename T>\n"
25363 "concept HasFoo = requires(T t) {\n"
25364 " { t.foo() };\n"
25365 " t.foo();\n"
25366 " };\n"
25367 "template <typename T>\n"
25368 "concept HasBar = requires(T t) {\n"
25369 " { t.bar() };\n"
25370 " t.bar();\n"
25371 " };",
25372 Style);
25373
25374 verifyFormat("template <typename T>\n"
25375 "concept Large = sizeof(T) > 10;");
25376
25377 verifyFormat("template <typename T, typename U>\n"
25378 "concept FooableWith = requires(T t, U u) {\n"
25379 " typename T::foo_type;\n"
25380 " { t.foo(u) } -> typename T::foo_type;\n"
25381 " t++;\n"
25382 " };\n"
25383 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25384 Style);
25385
25386 verifyFormat("template <typename T>\n"
25387 "concept Context = is_specialization_of_v<context, T>;");
25388
25389 verifyFormat("template <typename T>\n"
25390 "concept Node = std::is_object_v<T>;");
25391
25392 verifyFormat("template <class T>\n"
25393 "concept integral = __is_integral(T);");
25394
25395 verifyFormat("template <class T>\n"
25396 "concept is2D = __array_extent(T, 1) == 2;");
25397
25398 verifyFormat("template <class T>\n"
25399 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25400
25401 verifyFormat("template <class T, class T2>\n"
25402 "concept Same = __is_same_as<T, T2>;");
25403
25404 verifyFormat(
25405 "template <class _InIt, class _OutIt>\n"
25406 "concept _Can_reread_dest =\n"
25407 " std::forward_iterator<_OutIt> &&\n"
25408 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25409
25410 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
25411
25412 verifyFormat(
25413 "template <typename T>\n"
25414 "concept C = requires(T t) {\n"
25415 " requires Bar<T> && Foo<T>;\n"
25416 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25417 " };",
25418 Style);
25419
25420 verifyFormat("template <typename T>\n"
25421 "concept HasFoo = requires(T t) {\n"
25422 " { t.foo() };\n"
25423 " t.foo();\n"
25424 " };\n"
25425 "template <typename T>\n"
25426 "concept HasBar = requires(T t) {\n"
25427 " { t.bar() };\n"
25428 " t.bar();\n"
25429 " };",
25430 Style);
25431
25432 verifyFormat("template <typename T> concept True = true;", Style);
25433
25434 verifyFormat("template <typename T>\n"
25435 "concept C = decltype([]() -> std::true_type { return {}; "
25436 "}())::value &&\n"
25437 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25438 Style);
25439
25440 verifyFormat("template <typename T>\n"
25441 "concept Semiregular =\n"
25442 " DefaultConstructible<T> && CopyConstructible<T> && "
25443 "CopyAssignable<T> &&\n"
25444 " requires(T a, std::size_t n) {\n"
25445 " requires Same<T *, decltype(&a)>;\n"
25446 " { a.~T() } noexcept;\n"
25447 " requires Same<T *, decltype(new T)>;\n"
25448 " requires Same<T *, decltype(new T[n])>;\n"
25449 " { delete new T; };\n"
25450 " { delete new T[n]; };\n"
25451 " };",
25452 Style);
25453
25454 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
25455
25456 verifyFormat("template <typename T> concept C =\n"
25457 " requires(T t) {\n"
25458 " requires Bar<T> && Foo<T>;\n"
25459 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25460 " };",
25461 Style);
25462
25463 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25464 " { t.foo() };\n"
25465 " t.foo();\n"
25466 " };\n"
25467 "template <typename T> concept HasBar = requires(T t) {\n"
25468 " { t.bar() };\n"
25469 " t.bar();\n"
25470 " };",
25471 Style);
25472
25473 verifyFormat("template <typename T> concept True = true;", Style);
25474
25475 verifyFormat(
25476 "template <typename T> concept C =\n"
25477 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
25478 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25479 Style);
25480
25481 verifyFormat("template <typename T> concept Semiregular =\n"
25482 " DefaultConstructible<T> && CopyConstructible<T> && "
25483 "CopyAssignable<T> &&\n"
25484 " requires(T a, std::size_t n) {\n"
25485 " requires Same<T *, decltype(&a)>;\n"
25486 " { a.~T() } noexcept;\n"
25487 " requires Same<T *, decltype(new T)>;\n"
25488 " requires Same<T *, decltype(new T[n])>;\n"
25489 " { delete new T; };\n"
25490 " { delete new T[n]; };\n"
25491 " };",
25492 Style);
25493
25494 // The following tests are invalid C++, we just want to make sure we don't
25495 // assert.
25496 verifyNoCrash(Code: "template <typename T>\n"
25497 "concept C = requires C2<T>;");
25498
25499 verifyNoCrash(Code: "template <typename T>\n"
25500 "concept C = 5 + 4;");
25501
25502 verifyNoCrash(Code: "template <typename T>\n"
25503 "concept C = class X;");
25504
25505 verifyNoCrash(Code: "template <typename T>\n"
25506 "concept C = [] && true;");
25507
25508 verifyNoCrash(Code: "template <typename T>\n"
25509 "concept C = [] && requires(T t) { typename T::size_type; };");
25510}
25511
25512TEST_F(FormatTest, RequiresClausesPositions) {
25513 auto Style = getLLVMStyle();
25514 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
25515 EXPECT_EQ(Style.IndentRequiresClause, true);
25516
25517 // The default in LLVM style is REI_OuterScope, but these tests were written
25518 // when the default was REI_Keyword.
25519 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25520
25521 verifyFormat("template <typename T>\n"
25522 " requires(Foo<T> && std::trait<T>)\n"
25523 "struct Bar;",
25524 Style);
25525
25526 verifyFormat("template <typename T>\n"
25527 " requires(Foo<T> && std::trait<T>)\n"
25528 "class Bar {\n"
25529 "public:\n"
25530 " Bar(T t);\n"
25531 " bool baz();\n"
25532 "};",
25533 Style);
25534
25535 verifyFormat(
25536 "template <typename T>\n"
25537 " requires requires(T &&t) {\n"
25538 " typename T::I;\n"
25539 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
25540 " }\n"
25541 "Bar(T) -> Bar<typename T::I>;",
25542 Style);
25543
25544 verifyFormat("template <typename T>\n"
25545 " requires(Foo<T> && std::trait<T>)\n"
25546 "constexpr T MyGlobal;",
25547 Style);
25548
25549 verifyFormat("template <typename T>\n"
25550 " requires Foo<T> && requires(T t) {\n"
25551 " { t.baz() } -> std::same_as<bool>;\n"
25552 " requires std::same_as<T::Factor, int>;\n"
25553 " }\n"
25554 "inline int bar(T t) {\n"
25555 " return t.baz() ? T::Factor : 5;\n"
25556 "}",
25557 Style);
25558
25559 verifyFormat("template <typename T>\n"
25560 "inline int bar(T t)\n"
25561 " requires Foo<T> && requires(T t) {\n"
25562 " { t.baz() } -> std::same_as<bool>;\n"
25563 " requires std::same_as<T::Factor, int>;\n"
25564 " }\n"
25565 "{\n"
25566 " return t.baz() ? T::Factor : 5;\n"
25567 "}",
25568 Style);
25569
25570 verifyFormat("template <typename T>\n"
25571 " requires F<T>\n"
25572 "int bar(T t) {\n"
25573 " return 5;\n"
25574 "}",
25575 Style);
25576
25577 verifyFormat("template <typename T>\n"
25578 "int bar(T t)\n"
25579 " requires F<T>\n"
25580 "{\n"
25581 " return 5;\n"
25582 "}",
25583 Style);
25584
25585 verifyFormat("template <typename T>\n"
25586 "int S::bar(T t) &&\n"
25587 " requires F<T>\n"
25588 "{\n"
25589 " return 5;\n"
25590 "}",
25591 Style);
25592
25593 verifyFormat("template <typename T>\n"
25594 "int bar(T t)\n"
25595 " requires F<T>;",
25596 Style);
25597
25598 Style.IndentRequiresClause = false;
25599 verifyFormat("template <typename T>\n"
25600 "requires F<T>\n"
25601 "int bar(T t) {\n"
25602 " return 5;\n"
25603 "}",
25604 Style);
25605
25606 verifyFormat("template <typename T>\n"
25607 "int S::bar(T t) &&\n"
25608 "requires F<T>\n"
25609 "{\n"
25610 " return 5;\n"
25611 "}",
25612 Style);
25613
25614 verifyFormat("template <typename T>\n"
25615 "int bar(T t)\n"
25616 "requires F<T>\n"
25617 "{\n"
25618 " return 5;\n"
25619 "}",
25620 Style);
25621
25622 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
25623 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
25624 "template <typename T> requires Foo<T> void bar() {}\n"
25625 "template <typename T> void bar() requires Foo<T> {}\n"
25626 "template <typename T> void bar() requires Foo<T>;\n"
25627 "template <typename T> void S::bar() && requires Foo<T> {}\n"
25628 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
25629 Style);
25630
25631 auto ColumnStyle = Style;
25632 ColumnStyle.ColumnLimit = 40;
25633 verifyFormat("template <typename AAAAAAA>\n"
25634 "requires Foo<T> struct Bar {};\n"
25635 "template <typename AAAAAAA>\n"
25636 "requires Foo<T> void bar() {}\n"
25637 "template <typename AAAAAAA>\n"
25638 "void bar() requires Foo<T> {}\n"
25639 "template <typename T>\n"
25640 "void S::bar() && requires Foo<T> {}\n"
25641 "template <typename AAAAAAA>\n"
25642 "requires Foo<T> Baz(T) -> Baz<T>;",
25643 ColumnStyle);
25644
25645 verifyFormat("template <typename T>\n"
25646 "requires Foo<AAAAAAA> struct Bar {};\n"
25647 "template <typename T>\n"
25648 "requires Foo<AAAAAAA> void bar() {}\n"
25649 "template <typename T>\n"
25650 "void bar() requires Foo<AAAAAAA> {}\n"
25651 "template <typename T>\n"
25652 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
25653 ColumnStyle);
25654
25655 verifyFormat("template <typename AAAAAAA>\n"
25656 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25657 "struct Bar {};\n"
25658 "template <typename AAAAAAA>\n"
25659 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25660 "void bar() {}\n"
25661 "template <typename AAAAAAA>\n"
25662 "void bar()\n"
25663 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25664 "template <typename AAAAAAA>\n"
25665 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
25666 "template <typename AAAAAAA>\n"
25667 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25668 "Bar(T) -> Bar<T>;",
25669 ColumnStyle);
25670
25671 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
25672 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
25673
25674 verifyFormat("template <typename T>\n"
25675 "requires Foo<T> struct Bar {};\n"
25676 "template <typename T>\n"
25677 "requires Foo<T> void bar() {}\n"
25678 "template <typename T>\n"
25679 "void bar()\n"
25680 "requires Foo<T> {}\n"
25681 "template <typename T>\n"
25682 "void bar()\n"
25683 "requires Foo<T>;\n"
25684 "template <typename T>\n"
25685 "void S::bar() &&\n"
25686 "requires Foo<T> {}\n"
25687 "template <typename T>\n"
25688 "requires Foo<T> Bar(T) -> Bar<T>;",
25689 Style);
25690
25691 verifyFormat("template <typename AAAAAAA>\n"
25692 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25693 "struct Bar {};\n"
25694 "template <typename AAAAAAA>\n"
25695 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25696 "void bar() {}\n"
25697 "template <typename AAAAAAA>\n"
25698 "void bar()\n"
25699 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25700 "template <typename AAAAAAA>\n"
25701 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
25702 "template <typename AAAAAAA>\n"
25703 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25704 "Bar(T) -> Bar<T>;",
25705 ColumnStyle);
25706
25707 Style.IndentRequiresClause = true;
25708 ColumnStyle.IndentRequiresClause = true;
25709
25710 verifyFormat("template <typename T>\n"
25711 " requires Foo<T> struct Bar {};\n"
25712 "template <typename T>\n"
25713 " requires Foo<T> void bar() {}\n"
25714 "template <typename T>\n"
25715 "void bar()\n"
25716 " requires Foo<T> {}\n"
25717 "template <typename T>\n"
25718 "void S::bar() &&\n"
25719 " requires Foo<T> {}\n"
25720 "template <typename T>\n"
25721 " requires Foo<T> Bar(T) -> Bar<T>;",
25722 Style);
25723
25724 verifyFormat("template <typename AAAAAAA>\n"
25725 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25726 "struct Bar {};\n"
25727 "template <typename AAAAAAA>\n"
25728 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25729 "void bar() {}\n"
25730 "template <typename AAAAAAA>\n"
25731 "void bar()\n"
25732 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25733 "template <typename AAAAAAA>\n"
25734 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
25735 "template <typename AAAAAAA>\n"
25736 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25737 "Bar(T) -> Bar<T>;",
25738 ColumnStyle);
25739
25740 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
25741 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
25742
25743 verifyFormat("template <typename T> requires Foo<T>\n"
25744 "struct Bar {};\n"
25745 "template <typename T> requires Foo<T>\n"
25746 "void bar() {}\n"
25747 "template <typename T>\n"
25748 "void bar() requires Foo<T>\n"
25749 "{}\n"
25750 "template <typename T> void bar() requires Foo<T>;\n"
25751 "template <typename T>\n"
25752 "void S::bar() && requires Foo<T>\n"
25753 "{}\n"
25754 "template <typename T> requires Foo<T>\n"
25755 "Bar(T) -> Bar<T>;",
25756 Style);
25757
25758 verifyFormat("template <typename AAAAAAA>\n"
25759 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25760 "struct Bar {};\n"
25761 "template <typename AAAAAAA>\n"
25762 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25763 "void bar() {}\n"
25764 "template <typename AAAAAAA>\n"
25765 "void bar()\n"
25766 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25767 "{}\n"
25768 "template <typename AAAAAAA>\n"
25769 "requires Foo<AAAAAAAA>\n"
25770 "Bar(T) -> Bar<T>;\n"
25771 "template <typename AAAAAAA>\n"
25772 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25773 "Bar(T) -> Bar<T>;",
25774 ColumnStyle);
25775}
25776
25777TEST_F(FormatTest, RequiresClauses) {
25778 verifyFormat("struct [[nodiscard]] zero_t {\n"
25779 " template <class T>\n"
25780 " requires requires { number_zero_v<T>; }\n"
25781 " [[nodiscard]] constexpr operator T() const {\n"
25782 " return number_zero_v<T>;\n"
25783 " }\n"
25784 "};");
25785
25786 verifyFormat("template <class T>\n"
25787 " requires(std::same_as<int, T>)\n"
25788 "decltype(auto) fun() {}");
25789
25790 auto Style = getLLVMStyle();
25791
25792 verifyFormat(
25793 "template <typename T>\n"
25794 " requires is_default_constructible_v<hash<T>> and\n"
25795 " is_copy_constructible_v<hash<T>> and\n"
25796 " is_move_constructible_v<hash<T>> and\n"
25797 " is_copy_assignable_v<hash<T>> and "
25798 "is_move_assignable_v<hash<T>> and\n"
25799 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
25800 " is_callable_v<hash<T>(T)> and\n"
25801 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
25802 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
25803 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
25804 "struct S {};",
25805 Style);
25806
25807 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
25808 verifyFormat(
25809 "template <typename T>\n"
25810 " requires is_default_constructible_v<hash<T>>\n"
25811 " and is_copy_constructible_v<hash<T>>\n"
25812 " and is_move_constructible_v<hash<T>>\n"
25813 " and is_copy_assignable_v<hash<T>> and "
25814 "is_move_assignable_v<hash<T>>\n"
25815 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
25816 " and is_callable_v<hash<T>(T)>\n"
25817 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
25818 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
25819 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
25820 "&>()))>\n"
25821 "struct S {};",
25822 Style);
25823
25824 Style = getLLVMStyle();
25825 Style.ConstructorInitializerIndentWidth = 4;
25826 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
25827 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
25828 verifyFormat("constexpr Foo(Foo const &other)\n"
25829 " requires std::is_copy_constructible<T>\n"
25830 " : value{other.value} {\n"
25831 " do_magic();\n"
25832 " do_more_magic();\n"
25833 "}",
25834 Style);
25835
25836 // Not a clause, but we once hit an assert.
25837 verifyFormat("#if 0\n"
25838 "#else\n"
25839 "foo();\n"
25840 "#endif\n"
25841 "bar(requires);");
25842}
25843
25844TEST_F(FormatTest, RequiresExpressionIndentation) {
25845 auto Style = getLLVMStyle();
25846 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
25847
25848 verifyFormat("template <typename T>\n"
25849 "concept C = requires(T t) {\n"
25850 " typename T::value;\n"
25851 " requires requires(typename T::value v) {\n"
25852 " { t == v } -> std::same_as<bool>;\n"
25853 " };\n"
25854 "};",
25855 Style);
25856
25857 verifyFormat("template <typename T>\n"
25858 "void bar(T)\n"
25859 " requires Foo<T> && requires(T t) {\n"
25860 " { t.foo() } -> std::same_as<int>;\n"
25861 " } && requires(T t) {\n"
25862 " { t.bar() } -> std::same_as<bool>;\n"
25863 " --t;\n"
25864 " };",
25865 Style);
25866
25867 verifyFormat("template <typename T>\n"
25868 " requires Foo<T> &&\n"
25869 " requires(T t) {\n"
25870 " { t.foo() } -> std::same_as<int>;\n"
25871 " } && requires(T t) {\n"
25872 " { t.bar() } -> std::same_as<bool>;\n"
25873 " --t;\n"
25874 " }\n"
25875 "void bar(T);",
25876 Style);
25877
25878 verifyFormat("template <typename T> void f() {\n"
25879 " if constexpr (requires(T t) {\n"
25880 " { t.bar() } -> std::same_as<bool>;\n"
25881 " }) {\n"
25882 " }\n"
25883 "}",
25884 Style);
25885
25886 verifyFormat("template <typename T> void f() {\n"
25887 " if constexpr (condition && requires(T t) {\n"
25888 " { t.bar() } -> std::same_as<bool>;\n"
25889 " }) {\n"
25890 " }\n"
25891 "}",
25892 Style);
25893
25894 verifyFormat("template <typename T> struct C {\n"
25895 " void f()\n"
25896 " requires requires(T t) {\n"
25897 " { t.bar() } -> std::same_as<bool>;\n"
25898 " };\n"
25899 "};",
25900 Style);
25901
25902 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
25903
25904 verifyFormat("template <typename T>\n"
25905 "concept C = requires(T t) {\n"
25906 " typename T::value;\n"
25907 " requires requires(typename T::value v) {\n"
25908 " { t == v } -> std::same_as<bool>;\n"
25909 " };\n"
25910 " };",
25911 Style);
25912
25913 verifyFormat(
25914 "template <typename T>\n"
25915 "void bar(T)\n"
25916 " requires Foo<T> && requires(T t) {\n"
25917 " { t.foo() } -> std::same_as<int>;\n"
25918 " } && requires(T t) {\n"
25919 " { t.bar() } -> std::same_as<bool>;\n"
25920 " --t;\n"
25921 " };",
25922 Style);
25923
25924 verifyFormat("template <typename T>\n"
25925 " requires Foo<T> &&\n"
25926 " requires(T t) {\n"
25927 " { t.foo() } -> std::same_as<int>;\n"
25928 " } && requires(T t) {\n"
25929 " { t.bar() } -> std::same_as<bool>;\n"
25930 " --t;\n"
25931 " }\n"
25932 "void bar(T);",
25933 Style);
25934
25935 verifyFormat("template <typename T> void f() {\n"
25936 " if constexpr (requires(T t) {\n"
25937 " { t.bar() } -> std::same_as<bool>;\n"
25938 " }) {\n"
25939 " }\n"
25940 "}",
25941 Style);
25942
25943 verifyFormat(
25944 "template <typename T> void f() {\n"
25945 " if constexpr (condition && requires(T t) {\n"
25946 " { t.bar() } -> std::same_as<bool>;\n"
25947 " }) {\n"
25948 " }\n"
25949 "}",
25950 Style);
25951
25952 verifyFormat("template <typename T> struct C {\n"
25953 " void f()\n"
25954 " requires requires(T t) {\n"
25955 " { t.bar() } -> std::same_as<bool>;\n"
25956 " };\n"
25957 "};",
25958 Style);
25959}
25960
25961TEST_F(FormatTest, StatementAttributeLikeMacros) {
25962 FormatStyle Style = getLLVMStyle();
25963 StringRef Source = "void Foo::slot() {\n"
25964 " unsigned char MyChar = 'x';\n"
25965 " emit signal(MyChar);\n"
25966 " Q_EMIT signal(MyChar);\n"
25967 "}";
25968
25969 verifyFormat(Source, Style);
25970
25971 Style.AlignConsecutiveDeclarations.Enabled = true;
25972 verifyFormat("void Foo::slot() {\n"
25973 " unsigned char MyChar = 'x';\n"
25974 " emit signal(MyChar);\n"
25975 " Q_EMIT signal(MyChar);\n"
25976 "}",
25977 Source, Style);
25978
25979 Style.StatementAttributeLikeMacros.push_back(x: "emit");
25980 verifyFormat(Source, Style);
25981
25982 Style.StatementAttributeLikeMacros = {};
25983 verifyFormat("void Foo::slot() {\n"
25984 " unsigned char MyChar = 'x';\n"
25985 " emit signal(MyChar);\n"
25986 " Q_EMIT signal(MyChar);\n"
25987 "}",
25988 Source, Style);
25989}
25990
25991TEST_F(FormatTest, IndentAccessModifiers) {
25992 FormatStyle Style = getLLVMStyle();
25993 Style.IndentAccessModifiers = true;
25994 // Members are *two* levels below the record;
25995 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
25996 verifyFormat("class C {\n"
25997 " int i;\n"
25998 "};",
25999 Style);
26000 verifyFormat("union C {\n"
26001 " int i;\n"
26002 " unsigned u;\n"
26003 "};",
26004 Style);
26005 // Access modifiers should be indented one level below the record.
26006 verifyFormat("class C {\n"
26007 " public:\n"
26008 " int i;\n"
26009 "};",
26010 Style);
26011 verifyFormat("class C {\n"
26012 " public /* comment */:\n"
26013 " int i;\n"
26014 "};",
26015 Style);
26016 verifyFormat("struct S {\n"
26017 " private:\n"
26018 " class C {\n"
26019 " int j;\n"
26020 "\n"
26021 " public:\n"
26022 " C();\n"
26023 " };\n"
26024 "\n"
26025 " public:\n"
26026 " int i;\n"
26027 "};",
26028 Style);
26029 // Enumerations are not records and should be unaffected.
26030 Style.AllowShortEnumsOnASingleLine = false;
26031 verifyFormat("enum class E {\n"
26032 " A,\n"
26033 " B\n"
26034 "};",
26035 Style);
26036 // Test with a different indentation width;
26037 // also proves that the result is Style.AccessModifierOffset agnostic.
26038 Style.IndentWidth = 3;
26039 verifyFormat("class C {\n"
26040 " public:\n"
26041 " int i;\n"
26042 "};",
26043 Style);
26044 verifyFormat("class C {\n"
26045 " public /**/:\n"
26046 " int i;\n"
26047 "};",
26048 Style);
26049}
26050
26051TEST_F(FormatTest, LimitlessStringsAndComments) {
26052 auto Style = getLLVMStyleWithColumns(ColumnLimit: 0);
26053 constexpr StringRef Code =
26054 "/**\n"
26055 " * This is a multiline comment with quite some long lines, at least for "
26056 "the LLVM Style.\n"
26057 " * We will redo this with strings and line comments. Just to check if "
26058 "everything is working.\n"
26059 " */\n"
26060 "bool foo() {\n"
26061 " /* Single line multi line comment. */\n"
26062 " const std::string String = \"This is a multiline string with quite "
26063 "some long lines, at least for the LLVM Style.\"\n"
26064 " \"We already did it with multi line "
26065 "comments, and we will do it with line comments. Just to check if "
26066 "everything is working.\";\n"
26067 " // This is a line comment (block) with quite some long lines, at "
26068 "least for the LLVM Style.\n"
26069 " // We already did this with multi line comments and strings. Just to "
26070 "check if everything is working.\n"
26071 " const std::string SmallString = \"Hello World\";\n"
26072 " // Small line comment\n"
26073 " return String.size() > SmallString.size();\n"
26074 "}";
26075 verifyNoChange(Code, Style);
26076}
26077
26078TEST_F(FormatTest, FormatDecayCopy) {
26079 // error cases from unit tests
26080 verifyFormat("foo(auto())");
26081 verifyFormat("foo(auto{})");
26082 verifyFormat("foo(auto({}))");
26083 verifyFormat("foo(auto{{}})");
26084
26085 verifyFormat("foo(auto(1))");
26086 verifyFormat("foo(auto{1})");
26087 verifyFormat("foo(new auto(1))");
26088 verifyFormat("foo(new auto{1})");
26089 verifyFormat("decltype(auto(1)) x;");
26090 verifyFormat("decltype(auto{1}) x;");
26091 verifyFormat("auto(x);");
26092 verifyFormat("auto{x};");
26093 verifyFormat("new auto{x};");
26094 verifyFormat("auto{x} = y;");
26095 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26096 // the user's own fault
26097 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26098 // clearly the user's own fault
26099 verifyFormat("auto (*p)() = f;");
26100}
26101
26102TEST_F(FormatTest, Cpp20ModulesSupport) {
26103 FormatStyle Style = getLLVMStyle();
26104 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
26105 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
26106
26107 verifyFormat("export import foo;", Style);
26108 verifyFormat("export import foo:bar;", Style);
26109 verifyFormat("export import foo.bar;", Style);
26110 verifyFormat("export import foo.bar:baz;", Style);
26111 verifyFormat("export import :bar;", Style);
26112 verifyFormat("export module foo:bar;", Style);
26113 verifyFormat("export module foo;", Style);
26114 verifyFormat("export module foo.bar;", Style);
26115 verifyFormat("export module foo.bar:baz;", Style);
26116 verifyFormat("export import <string_view>;", Style);
26117 verifyFormat("export import <Foo/Bar>;", Style);
26118
26119 verifyFormat("export type_name var;", Style);
26120 verifyFormat("template <class T> export using A = B<T>;", Style);
26121 verifyFormat("export using A = B;", Style);
26122 verifyFormat("export int func() {\n"
26123 " foo();\n"
26124 "}",
26125 Style);
26126 verifyFormat("export struct {\n"
26127 " int foo;\n"
26128 "};",
26129 Style);
26130 verifyFormat("export {\n"
26131 " int foo;\n"
26132 "};",
26133 Style);
26134 verifyFormat("export export char const *hello() { return \"hello\"; }");
26135
26136 verifyFormat("import bar;", Style);
26137 verifyFormat("import foo.bar;", Style);
26138 verifyFormat("import foo:bar;", Style);
26139 verifyFormat("import :bar;", Style);
26140 verifyFormat("import /* module partition */ :bar;", Style);
26141 verifyFormat("import <ctime>;", Style);
26142 verifyFormat("import \"header\";", Style);
26143
26144 verifyFormat("module foo;", Style);
26145 verifyFormat("module foo:bar;", Style);
26146 verifyFormat("module foo.bar;", Style);
26147 verifyFormat("module;", Style);
26148
26149 verifyFormat("export namespace hi {\n"
26150 "const char *sayhi();\n"
26151 "}",
26152 Style);
26153
26154 verifyFormat("module :private;", Style);
26155 verifyFormat("import <foo/bar.h>;", Style);
26156 verifyFormat("import foo...bar;", Style);
26157 verifyFormat("import ..........;", Style);
26158 verifyFormat("module foo:private;", Style);
26159 verifyFormat("import a", Style);
26160 verifyFormat("module a", Style);
26161 verifyFormat("export import a", Style);
26162 verifyFormat("export module a", Style);
26163
26164 verifyFormat("import", Style);
26165 verifyFormat("module", Style);
26166 verifyFormat("export", Style);
26167
26168 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26169}
26170
26171TEST_F(FormatTest, CoroutineForCoawait) {
26172 FormatStyle Style = getLLVMStyle();
26173 verifyFormat("for co_await (auto x : range())\n ;");
26174 verifyFormat("for (auto i : arr) {\n"
26175 "}",
26176 Style);
26177 verifyFormat("for co_await (auto i : arr) {\n"
26178 "}",
26179 Style);
26180 verifyFormat("for co_await (auto i : foo(T{})) {\n"
26181 "}",
26182 Style);
26183}
26184
26185TEST_F(FormatTest, CoroutineCoAwait) {
26186 verifyFormat("int x = co_await foo();");
26187 verifyFormat("int x = (co_await foo());");
26188 verifyFormat("co_await (42);");
26189 verifyFormat("void operator co_await(int);");
26190 verifyFormat("void operator co_await(a);");
26191 verifyFormat("co_await a;");
26192 verifyFormat("co_await missing_await_resume{};");
26193 verifyFormat("co_await a; // comment");
26194 verifyFormat("void test0() { co_await a; }");
26195 verifyFormat("co_await co_await co_await foo();");
26196 verifyFormat("co_await foo().bar();");
26197 verifyFormat("co_await [this]() -> Task { co_return x; }");
26198 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26199 "foo(); }(x, y);");
26200
26201 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
26202 verifyFormat("co_await [this](int a, int b) -> Task {\n"
26203 " co_return co_await foo();\n"
26204 "}(x, y);",
26205 Style);
26206 verifyFormat("co_await;");
26207}
26208
26209TEST_F(FormatTest, CoroutineCoYield) {
26210 verifyFormat("int x = co_yield foo();");
26211 verifyFormat("int x = (co_yield foo());");
26212 verifyFormat("co_yield (42);");
26213 verifyFormat("co_yield {42};");
26214 verifyFormat("co_yield 42;");
26215 verifyFormat("co_yield n++;");
26216 verifyFormat("co_yield ++n;");
26217 verifyFormat("co_yield;");
26218}
26219
26220TEST_F(FormatTest, CoroutineCoReturn) {
26221 verifyFormat("co_return (42);");
26222 verifyFormat("co_return;");
26223 verifyFormat("co_return {};");
26224 verifyFormat("co_return x;");
26225 verifyFormat("co_return co_await foo();");
26226 verifyFormat("co_return co_yield foo();");
26227}
26228
26229TEST_F(FormatTest, EmptyShortBlock) {
26230 auto Style = getLLVMStyle();
26231 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
26232
26233 verifyFormat("try {\n"
26234 " doA();\n"
26235 "} catch (Exception &e) {\n"
26236 " e.printStackTrace();\n"
26237 "}",
26238 Style);
26239
26240 verifyFormat("try {\n"
26241 " doA();\n"
26242 "} catch (Exception &e) {}",
26243 Style);
26244}
26245
26246TEST_F(FormatTest, ShortTemplatedArgumentLists) {
26247 auto Style = getLLVMStyle();
26248
26249 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
26250 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
26251 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
26252 verifyFormat("struct Y<[] { return 0; }> {};", Style);
26253
26254 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
26255 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
26256}
26257
26258TEST_F(FormatTest, MultilineLambdaInConditional) {
26259 auto Style = getLLVMStyleWithColumns(ColumnLimit: 70);
26260 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26261 " ;\n"
26262 " return 5;\n"
26263 "}()\n"
26264 " : 2;",
26265 Style);
26266 verifyFormat(
26267 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26268 " ;\n"
26269 " return 5;\n"
26270 "}();",
26271 Style);
26272
26273 Style = getLLVMStyleWithColumns(ColumnLimit: 60);
26274 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26275 " ? []() {\n"
26276 " ;\n"
26277 " return 5;\n"
26278 " }()\n"
26279 " : 2;",
26280 Style);
26281 verifyFormat("auto aLengthyIdentifier =\n"
26282 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
26283 " ;\n"
26284 " return 5;\n"
26285 " }();",
26286 Style);
26287
26288 Style = getLLVMStyleWithColumns(ColumnLimit: 40);
26289 verifyFormat("auto aLengthyIdentifier =\n"
26290 " oneExpressionSoThatWeBreak ? []() {\n"
26291 " ;\n"
26292 " return 5;\n"
26293 " }()\n"
26294 " : 2;",
26295 Style);
26296 verifyFormat("auto aLengthyIdentifier =\n"
26297 " oneExpressionSoThatWeBreak\n"
26298 " ? 2\n"
26299 " : []() {\n"
26300 " ;\n"
26301 " return 5;\n"
26302 " };",
26303 Style);
26304}
26305
26306TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
26307 auto Style = getLLVMStyle();
26308
26309 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
26310 "void functionDecl(int a, int b, int c);";
26311
26312 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26313 "paramF, paramG, paramH, paramI);\n"
26314 "void functionDecl(int argumentA, int argumentB, int "
26315 "argumentC, int argumentD, int argumentE);";
26316
26317 verifyFormat(Short, Style);
26318
26319 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
26320 "paramF, paramG, paramH,\n"
26321 " paramI);\n"
26322 "void functionDecl(int argumentA, int argumentB, int "
26323 "argumentC, int argumentD,\n"
26324 " int argumentE);";
26325
26326 verifyFormat(NoBreak, Medium, Style);
26327 verifyFormat(NoBreak,
26328 "functionCall(\n"
26329 " paramA,\n"
26330 " paramB,\n"
26331 " paramC,\n"
26332 " paramD,\n"
26333 " paramE,\n"
26334 " paramF,\n"
26335 " paramG,\n"
26336 " paramH,\n"
26337 " paramI\n"
26338 ");\n"
26339 "void functionDecl(\n"
26340 " int argumentA,\n"
26341 " int argumentB,\n"
26342 " int argumentC,\n"
26343 " int argumentD,\n"
26344 " int argumentE\n"
26345 ");",
26346 Style);
26347
26348 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26349 " nestedLongFunctionCall(argument1, "
26350 "argument2, argument3,\n"
26351 " argument4, "
26352 "argument5));",
26353 Style);
26354
26355 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26356
26357 verifyFormat(Short, Style);
26358 verifyFormat(
26359 "functionCall(\n"
26360 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26361 "paramI\n"
26362 ");\n"
26363 "void functionDecl(\n"
26364 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26365 "argumentE\n"
26366 ");",
26367 Medium, Style);
26368
26369 Style.AllowAllArgumentsOnNextLine = false;
26370 Style.AllowAllParametersOfDeclarationOnNextLine = false;
26371
26372 verifyFormat(Short, Style);
26373 verifyFormat(
26374 "functionCall(\n"
26375 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26376 "paramI\n"
26377 ");\n"
26378 "void functionDecl(\n"
26379 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26380 "argumentE\n"
26381 ");",
26382 Medium, Style);
26383
26384 Style.BinPackArguments = false;
26385 Style.BinPackParameters = false;
26386
26387 verifyFormat(Short, Style);
26388
26389 verifyFormat("functionCall(\n"
26390 " paramA,\n"
26391 " paramB,\n"
26392 " paramC,\n"
26393 " paramD,\n"
26394 " paramE,\n"
26395 " paramF,\n"
26396 " paramG,\n"
26397 " paramH,\n"
26398 " paramI\n"
26399 ");\n"
26400 "void functionDecl(\n"
26401 " int argumentA,\n"
26402 " int argumentB,\n"
26403 " int argumentC,\n"
26404 " int argumentD,\n"
26405 " int argumentE\n"
26406 ");",
26407 Medium, Style);
26408
26409 verifyFormat("outerFunctionCall(\n"
26410 " nestedFunctionCall(argument1),\n"
26411 " nestedLongFunctionCall(\n"
26412 " argument1,\n"
26413 " argument2,\n"
26414 " argument3,\n"
26415 " argument4,\n"
26416 " argument5\n"
26417 " )\n"
26418 ");",
26419 Style);
26420
26421 verifyFormat("int a = (int)b;", Style);
26422 verifyFormat("int a = (int)b;",
26423 "int a = (\n"
26424 " int\n"
26425 ") b;",
26426 Style);
26427
26428 verifyFormat("return (true);", Style);
26429 verifyFormat("return (true);",
26430 "return (\n"
26431 " true\n"
26432 ");",
26433 Style);
26434
26435 verifyFormat("void foo();", Style);
26436 verifyFormat("void foo();",
26437 "void foo(\n"
26438 ");",
26439 Style);
26440
26441 verifyFormat("void foo() {}", Style);
26442 verifyFormat("void foo() {}",
26443 "void foo(\n"
26444 ") {\n"
26445 "}",
26446 Style);
26447
26448 verifyFormat("auto string = std::string();", Style);
26449 verifyFormat("auto string = std::string();",
26450 "auto string = std::string(\n"
26451 ");",
26452 Style);
26453
26454 verifyFormat("void (*functionPointer)() = nullptr;", Style);
26455 verifyFormat("void (*functionPointer)() = nullptr;",
26456 "void (\n"
26457 " *functionPointer\n"
26458 ")\n"
26459 "(\n"
26460 ") = nullptr;",
26461 Style);
26462}
26463
26464TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
26465 auto Style = getLLVMStyle();
26466
26467 verifyFormat("if (foo()) {\n"
26468 " return;\n"
26469 "}",
26470 Style);
26471
26472 verifyFormat("if (quiteLongArg !=\n"
26473 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26474 "comment\n"
26475 " return;\n"
26476 "}",
26477 Style);
26478
26479 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26480
26481 verifyFormat("if (foo()) {\n"
26482 " return;\n"
26483 "}",
26484 Style);
26485
26486 verifyFormat("if (quiteLongArg !=\n"
26487 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26488 "comment\n"
26489 " return;\n"
26490 "}",
26491 Style);
26492
26493 verifyFormat("void foo() {\n"
26494 " if (camelCaseName < alsoLongName ||\n"
26495 " anotherEvenLongerName <=\n"
26496 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
26497 "\n"
26498 " otherName < thisLastName) {\n"
26499 " return;\n"
26500 " } else if (quiteLongName < alsoLongName ||\n"
26501 " anotherEvenLongerName <=\n"
26502 " thisReallyReallyReallyReallyReallyReallyLonger"
26503 "Name ||\n"
26504 " otherName < thisLastName) {\n"
26505 " return;\n"
26506 " }\n"
26507 "}",
26508 Style);
26509
26510 Style.ContinuationIndentWidth = 2;
26511 verifyFormat("void foo() {\n"
26512 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
26513 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26514 " if (false) {\n"
26515 " return;\n"
26516 " } else if (thisIsRatherALongIfClause && "
26517 "thatIExpectToBeBroken ||\n"
26518 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26519 " return;\n"
26520 " }\n"
26521 " }\n"
26522 "}",
26523 Style);
26524}
26525
26526TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
26527 auto Style = getLLVMStyle();
26528
26529 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
26530 " doSomething();\n"
26531 "}",
26532 Style);
26533
26534 verifyFormat("for (int myReallyLongCountVariable = 0; "
26535 "myReallyLongCountVariable < count;\n"
26536 " myReallyLongCountVariable++) {\n"
26537 " doSomething();\n"
26538 "}",
26539 Style);
26540
26541 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26542
26543 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
26544 " doSomething();\n"
26545 "}",
26546 Style);
26547
26548 verifyFormat("for (int myReallyLongCountVariable = 0; "
26549 "myReallyLongCountVariable < count;\n"
26550 " myReallyLongCountVariable++) {\n"
26551 " doSomething();\n"
26552 "}",
26553 Style);
26554}
26555
26556TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
26557 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
26558 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
26559 // Aggregate initialization.
26560 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
26561 " 10000000, 20000000\n"
26562 "};",
26563 Style);
26564 verifyFormat("SomeStruct s{\n"
26565 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
26566 " \"zzzzzzzzzzzzzzzz\"\n"
26567 "};",
26568 Style);
26569 // Designated initializers.
26570 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
26571 " [0] = 10000000, [1] = 20000000\n"
26572 "};",
26573 Style);
26574 verifyFormat("SomeStruct s{\n"
26575 " .foo = \"xxxxxxxxxxxxx\",\n"
26576 " .bar = \"yyyyyyyyyyyyy\",\n"
26577 " .baz = \"zzzzzzzzzzzzz\"\n"
26578 "};",
26579 Style);
26580 // List initialization.
26581 verifyFormat("SomeStruct s{\n"
26582 " \"xxxxxxxxxxxxx\",\n"
26583 " \"yyyyyyyyyyyyy\",\n"
26584 " \"zzzzzzzzzzzzz\",\n"
26585 "};",
26586 Style);
26587 verifyFormat("SomeStruct{\n"
26588 " \"xxxxxxxxxxxxx\",\n"
26589 " \"yyyyyyyyyyyyy\",\n"
26590 " \"zzzzzzzzzzzzz\",\n"
26591 "};",
26592 Style);
26593 verifyFormat("new SomeStruct{\n"
26594 " \"xxxxxxxxxxxxx\",\n"
26595 " \"yyyyyyyyyyyyy\",\n"
26596 " \"zzzzzzzzzzzzz\",\n"
26597 "};",
26598 Style);
26599 // Member initializer.
26600 verifyFormat("class SomeClass {\n"
26601 " SomeStruct s{\n"
26602 " \"xxxxxxxxxxxxx\",\n"
26603 " \"yyyyyyyyyyyyy\",\n"
26604 " \"zzzzzzzzzzzzz\",\n"
26605 " };\n"
26606 "};",
26607 Style);
26608 // Constructor member initializer.
26609 verifyFormat("SomeClass::SomeClass : strct{\n"
26610 " \"xxxxxxxxxxxxx\",\n"
26611 " \"yyyyyyyyyyyyy\",\n"
26612 " \"zzzzzzzzzzzzz\",\n"
26613 " } {}",
26614 Style);
26615 // Copy initialization.
26616 verifyFormat("SomeStruct s = SomeStruct{\n"
26617 " \"xxxxxxxxxxxxx\",\n"
26618 " \"yyyyyyyyyyyyy\",\n"
26619 " \"zzzzzzzzzzzzz\",\n"
26620 "};",
26621 Style);
26622 // Copy list initialization.
26623 verifyFormat("SomeStruct s = {\n"
26624 " \"xxxxxxxxxxxxx\",\n"
26625 " \"yyyyyyyyyyyyy\",\n"
26626 " \"zzzzzzzzzzzzz\",\n"
26627 "};",
26628 Style);
26629 // Assignment operand initialization.
26630 verifyFormat("s = {\n"
26631 " \"xxxxxxxxxxxxx\",\n"
26632 " \"yyyyyyyyyyyyy\",\n"
26633 " \"zzzzzzzzzzzzz\",\n"
26634 "};",
26635 Style);
26636 // Returned object initialization.
26637 verifyFormat("return {\n"
26638 " \"xxxxxxxxxxxxx\",\n"
26639 " \"yyyyyyyyyyyyy\",\n"
26640 " \"zzzzzzzzzzzzz\",\n"
26641 "};",
26642 Style);
26643 // Initializer list.
26644 verifyFormat("auto initializerList = {\n"
26645 " \"xxxxxxxxxxxxx\",\n"
26646 " \"yyyyyyyyyyyyy\",\n"
26647 " \"zzzzzzzzzzzzz\",\n"
26648 "};",
26649 Style);
26650 // Function parameter initialization.
26651 verifyFormat("func({\n"
26652 " \"xxxxxxxxxxxxx\",\n"
26653 " \"yyyyyyyyyyyyy\",\n"
26654 " \"zzzzzzzzzzzzz\",\n"
26655 "});",
26656 Style);
26657 // Nested init lists.
26658 verifyFormat("SomeStruct s = {\n"
26659 " {{init1, init2, init3, init4, init5},\n"
26660 " {init1, init2, init3, init4, init5}}\n"
26661 "};",
26662 Style);
26663 verifyFormat("SomeStruct s = {\n"
26664 " {{\n"
26665 " .init1 = 1,\n"
26666 " .init2 = 2,\n"
26667 " .init3 = 3,\n"
26668 " .init4 = 4,\n"
26669 " .init5 = 5,\n"
26670 " },\n"
26671 " {init1, init2, init3, init4, init5}}\n"
26672 "};",
26673 Style);
26674 verifyFormat("SomeArrayT a[3] = {\n"
26675 " {\n"
26676 " foo,\n"
26677 " bar,\n"
26678 " },\n"
26679 " {\n"
26680 " foo,\n"
26681 " bar,\n"
26682 " },\n"
26683 " SomeArrayT{},\n"
26684 "};",
26685 Style);
26686 verifyFormat("SomeArrayT a[3] = {\n"
26687 " {foo},\n"
26688 " {\n"
26689 " {\n"
26690 " init1,\n"
26691 " init2,\n"
26692 " init3,\n"
26693 " },\n"
26694 " {\n"
26695 " init1,\n"
26696 " init2,\n"
26697 " init3,\n"
26698 " },\n"
26699 " },\n"
26700 " {baz},\n"
26701 "};",
26702 Style);
26703}
26704
26705TEST_F(FormatTest, UnderstandsDigraphs) {
26706 verifyFormat("int arr<:5:> = {};");
26707 verifyFormat("int arr[5] = <%%>;");
26708 verifyFormat("int arr<:::qualified_variable:> = {};");
26709 verifyFormat("int arr[::qualified_variable] = <%%>;");
26710 verifyFormat("%:include <header>");
26711 verifyFormat("%:define A x##y");
26712 verifyFormat("#define A x%:%:y");
26713}
26714
26715TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
26716 auto Style = getLLVMStyle();
26717 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
26718 Style.AlignConsecutiveAssignments.Enabled = true;
26719 Style.AlignConsecutiveDeclarations.Enabled = true;
26720
26721 // The AlignArray code is incorrect for non square Arrays and can cause
26722 // crashes, these tests assert that the array is not changed but will
26723 // also act as regression tests for when it is properly fixed
26724 verifyFormat("struct test demo[] = {\n"
26725 " {1, 2},\n"
26726 " {3, 4, 5},\n"
26727 " {6, 7, 8}\n"
26728 "};",
26729 Style);
26730 verifyFormat("struct test demo[] = {\n"
26731 " {1, 2, 3, 4, 5},\n"
26732 " {3, 4, 5},\n"
26733 " {6, 7, 8}\n"
26734 "};",
26735 Style);
26736 verifyFormat("struct test demo[] = {\n"
26737 " {1, 2, 3, 4, 5},\n"
26738 " {3, 4, 5},\n"
26739 " {6, 7, 8, 9, 10, 11, 12}\n"
26740 "};",
26741 Style);
26742 verifyFormat("struct test demo[] = {\n"
26743 " {1, 2, 3},\n"
26744 " {3, 4, 5},\n"
26745 " {6, 7, 8, 9, 10, 11, 12}\n"
26746 "};",
26747 Style);
26748
26749 verifyFormat("S{\n"
26750 " {},\n"
26751 " {},\n"
26752 " {a, b}\n"
26753 "};",
26754 Style);
26755 verifyFormat("S{\n"
26756 " {},\n"
26757 " {},\n"
26758 " {a, b},\n"
26759 "};",
26760 Style);
26761 verifyFormat("void foo() {\n"
26762 " auto thing = test{\n"
26763 " {\n"
26764 " {13}, {something}, // A\n"
26765 " }\n"
26766 " };\n"
26767 "}",
26768 "void foo() {\n"
26769 " auto thing = test{\n"
26770 " {\n"
26771 " {13},\n"
26772 " {something}, // A\n"
26773 " }\n"
26774 " };\n"
26775 "}",
26776 Style);
26777}
26778
26779TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
26780 auto Style = getLLVMStyle();
26781 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
26782 Style.AlignConsecutiveAssignments.Enabled = true;
26783 Style.AlignConsecutiveDeclarations.Enabled = true;
26784
26785 // The AlignArray code is incorrect for non square Arrays and can cause
26786 // crashes, these tests assert that the array is not changed but will
26787 // also act as regression tests for when it is properly fixed
26788 verifyFormat("struct test demo[] = {\n"
26789 " {1, 2},\n"
26790 " {3, 4, 5},\n"
26791 " {6, 7, 8}\n"
26792 "};",
26793 Style);
26794 verifyFormat("struct test demo[] = {\n"
26795 " {1, 2, 3, 4, 5},\n"
26796 " {3, 4, 5},\n"
26797 " {6, 7, 8}\n"
26798 "};",
26799 Style);
26800 verifyFormat("struct test demo[] = {\n"
26801 " {1, 2, 3, 4, 5},\n"
26802 " {3, 4, 5},\n"
26803 " {6, 7, 8, 9, 10, 11, 12}\n"
26804 "};",
26805 Style);
26806 verifyFormat("struct test demo[] = {\n"
26807 " {1, 2, 3},\n"
26808 " {3, 4, 5},\n"
26809 " {6, 7, 8, 9, 10, 11, 12}\n"
26810 "};",
26811 Style);
26812
26813 verifyFormat("S{\n"
26814 " {},\n"
26815 " {},\n"
26816 " {a, b}\n"
26817 "};",
26818 Style);
26819 verifyFormat("S{\n"
26820 " {},\n"
26821 " {},\n"
26822 " {a, b},\n"
26823 "};",
26824 Style);
26825 verifyFormat("void foo() {\n"
26826 " auto thing = test{\n"
26827 " {\n"
26828 " {13}, {something}, // A\n"
26829 " }\n"
26830 " };\n"
26831 "}",
26832 "void foo() {\n"
26833 " auto thing = test{\n"
26834 " {\n"
26835 " {13},\n"
26836 " {something}, // A\n"
26837 " }\n"
26838 " };\n"
26839 "}",
26840 Style);
26841}
26842
26843TEST_F(FormatTest, FormatsVariableTemplates) {
26844 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
26845 verifyFormat("template <typename T> "
26846 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
26847}
26848
26849TEST_F(FormatTest, RemoveSemicolon) {
26850 FormatStyle Style = getLLVMStyle();
26851 Style.RemoveSemicolon = true;
26852
26853 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
26854 "int max(int a, int b) { return a > b ? a : b; };", Style);
26855
26856 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
26857 "int max(int a, int b) { return a > b ? a : b; };;", Style);
26858
26859 verifyFormat("class Foo {\n"
26860 " int getSomething() const { return something; }\n"
26861 "};",
26862 "class Foo {\n"
26863 " int getSomething() const { return something; };\n"
26864 "};",
26865 Style);
26866
26867 verifyFormat("class Foo {\n"
26868 " int getSomething() const { return something; }\n"
26869 "};",
26870 "class Foo {\n"
26871 " int getSomething() const { return something; };;\n"
26872 "};",
26873 Style);
26874
26875 verifyFormat("for (;;) {\n"
26876 "}",
26877 Style);
26878
26879 verifyFormat("class [[deprecated(\"\")]] C {\n"
26880 " int i;\n"
26881 "};",
26882 Style);
26883
26884 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
26885 " int i;\n"
26886 "};",
26887 Style);
26888
26889 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
26890
26891 verifyFormat("void main() {}", "void main() {};", Style);
26892
26893 verifyFormat("struct Foo {\n"
26894 " Foo() {}\n"
26895 " ~Foo() {}\n"
26896 "};",
26897 "struct Foo {\n"
26898 " Foo() {};\n"
26899 " ~Foo() {};\n"
26900 "};",
26901 Style);
26902
26903// We can't (and probably shouldn't) support the following.
26904#if 0
26905 verifyFormat("void foo() {} //\n"
26906 "int bar;",
26907 "void foo() {}; //\n"
26908 "; int bar;",
26909 Style);
26910#endif
26911}
26912
26913TEST_F(FormatTest, BreakAfterAttributes) {
26914 constexpr StringRef Code("[[maybe_unused]] const int i;\n"
26915 "[[foo([[]])]] [[maybe_unused]]\n"
26916 "int j;\n"
26917 "[[maybe_unused]]\n"
26918 "foo<int> k;\n"
26919 "[[nodiscard]] inline int f(int &i);\n"
26920 "[[foo([[]])]] [[nodiscard]]\n"
26921 "int g(int &i);\n"
26922 "[[nodiscard]]\n"
26923 "inline int f(int &i) {\n"
26924 " i = 1;\n"
26925 " return 0;\n"
26926 "}\n"
26927 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26928 " i = 0;\n"
26929 " return 1;\n"
26930 "}");
26931
26932 FormatStyle Style = getLLVMStyle();
26933 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
26934 verifyNoChange(Code, Style);
26935
26936 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
26937 verifyFormat("[[maybe_unused]] const int i;\n"
26938 "[[foo([[]])]] [[maybe_unused]] int j;\n"
26939 "[[maybe_unused]] foo<int> k;\n"
26940 "[[nodiscard]] inline int f(int &i);\n"
26941 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
26942 "[[nodiscard]] inline int f(int &i) {\n"
26943 " i = 1;\n"
26944 " return 0;\n"
26945 "}\n"
26946 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26947 " i = 0;\n"
26948 " return 1;\n"
26949 "}",
26950 Code, Style);
26951
26952 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
26953 verifyFormat("[[maybe_unused]]\n"
26954 "const int i;\n"
26955 "[[foo([[]])]] [[maybe_unused]]\n"
26956 "int j;\n"
26957 "[[maybe_unused]]\n"
26958 "foo<int> k;\n"
26959 "[[nodiscard]]\n"
26960 "inline int f(int &i);\n"
26961 "[[foo([[]])]] [[nodiscard]]\n"
26962 "int g(int &i);\n"
26963 "[[nodiscard]]\n"
26964 "inline int f(int &i) {\n"
26965 " i = 1;\n"
26966 " return 0;\n"
26967 "}\n"
26968 "[[foo([[]])]] [[nodiscard]]\n"
26969 "int g(int &i) {\n"
26970 " i = 0;\n"
26971 " return 1;\n"
26972 "}",
26973 Code, Style);
26974
26975 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
26976 " f();\n"
26977 "else\n"
26978 " g();\n"
26979 "[[foo([[]])]]\n"
26980 "switch (b) {\n"
26981 "[[unlikely]] case 1:\n"
26982 " ++b;\n"
26983 " break;\n"
26984 "[[likely]]\n"
26985 "default:\n"
26986 " return;\n"
26987 "}\n"
26988 "[[unlikely]] for (; c > 0; --c)\n"
26989 " h();\n"
26990 "[[likely]]\n"
26991 "while (d > 0)\n"
26992 " --d;");
26993
26994 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
26995 verifyNoChange(CtrlStmtCode, Style);
26996
26997 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
26998 verifyFormat("[[likely]] if (a)\n"
26999 " f();\n"
27000 "else\n"
27001 " g();\n"
27002 "[[foo([[]])]] switch (b) {\n"
27003 "[[unlikely]] case 1:\n"
27004 " ++b;\n"
27005 " break;\n"
27006 "[[likely]] default:\n"
27007 " return;\n"
27008 "}\n"
27009 "[[unlikely]] for (; c > 0; --c)\n"
27010 " h();\n"
27011 "[[likely]] while (d > 0)\n"
27012 " --d;",
27013 CtrlStmtCode, Style);
27014
27015 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
27016 verifyFormat("[[likely]]\n"
27017 "if (a)\n"
27018 " f();\n"
27019 "else\n"
27020 " g();\n"
27021 "[[foo([[]])]]\n"
27022 "switch (b) {\n"
27023 "[[unlikely]]\n"
27024 "case 1:\n"
27025 " ++b;\n"
27026 " break;\n"
27027 "[[likely]]\n"
27028 "default:\n"
27029 " return;\n"
27030 "}\n"
27031 "[[unlikely]]\n"
27032 "for (; c > 0; --c)\n"
27033 " h();\n"
27034 "[[likely]]\n"
27035 "while (d > 0)\n"
27036 " --d;",
27037 CtrlStmtCode, Style);
27038
27039 constexpr StringRef CtorDtorCode("struct Foo {\n"
27040 " [[deprecated]] Foo();\n"
27041 " [[deprecated]] Foo() {}\n"
27042 " [[deprecated]] ~Foo();\n"
27043 " [[deprecated]] ~Foo() {}\n"
27044 " [[deprecated]] void f();\n"
27045 " [[deprecated]] void f() {}\n"
27046 "};\n"
27047 "[[deprecated]] Bar::Bar() {}\n"
27048 "[[deprecated]] Bar::~Bar() {}\n"
27049 "[[deprecated]] void g() {}");
27050 verifyFormat("struct Foo {\n"
27051 " [[deprecated]]\n"
27052 " Foo();\n"
27053 " [[deprecated]]\n"
27054 " Foo() {}\n"
27055 " [[deprecated]]\n"
27056 " ~Foo();\n"
27057 " [[deprecated]]\n"
27058 " ~Foo() {}\n"
27059 " [[deprecated]]\n"
27060 " void f();\n"
27061 " [[deprecated]]\n"
27062 " void f() {}\n"
27063 "};\n"
27064 "[[deprecated]]\n"
27065 "Bar::Bar() {}\n"
27066 "[[deprecated]]\n"
27067 "Bar::~Bar() {}\n"
27068 "[[deprecated]]\n"
27069 "void g() {}",
27070 CtorDtorCode, Style);
27071
27072 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
27073 verifyFormat("struct Foo {\n"
27074 " [[deprecated]]\n"
27075 " Foo();\n"
27076 " [[deprecated]]\n"
27077 " Foo()\n"
27078 " {\n"
27079 " }\n"
27080 " [[deprecated]]\n"
27081 " ~Foo();\n"
27082 " [[deprecated]]\n"
27083 " ~Foo()\n"
27084 " {\n"
27085 " }\n"
27086 " [[deprecated]]\n"
27087 " void f();\n"
27088 " [[deprecated]]\n"
27089 " void f()\n"
27090 " {\n"
27091 " }\n"
27092 "};\n"
27093 "[[deprecated]]\n"
27094 "Bar::Bar()\n"
27095 "{\n"
27096 "}\n"
27097 "[[deprecated]]\n"
27098 "Bar::~Bar()\n"
27099 "{\n"
27100 "}\n"
27101 "[[deprecated]]\n"
27102 "void g()\n"
27103 "{\n"
27104 "}",
27105 CtorDtorCode, Style);
27106
27107 verifyFormat("struct Foo {\n"
27108 " [[maybe_unused]]\n"
27109 " void operator+();\n"
27110 "};\n"
27111 "[[nodiscard]]\n"
27112 "Foo &operator-(Foo &);",
27113 Style);
27114
27115 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
27116 verifyFormat("[[nodiscard]]\n"
27117 "Foo& operator-(Foo&);",
27118 Style);
27119}
27120
27121TEST_F(FormatTest, InsertNewlineAtEOF) {
27122 FormatStyle Style = getLLVMStyle();
27123 Style.InsertNewlineAtEOF = true;
27124
27125 verifyNoChange("int i;\n", Style);
27126 verifyFormat("int i;\n", "int i;", Style);
27127}
27128
27129TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
27130 FormatStyle Style = getLLVMStyle();
27131 Style.KeepEmptyLinesAtEOF = true;
27132
27133 const StringRef Code{"int i;\n\n"};
27134 verifyNoChange(Code, Style);
27135 verifyFormat(Code, "int i;\n\n\n", Style);
27136}
27137
27138TEST_F(FormatTest, SpaceAfterUDL) {
27139 verifyFormat("auto c = (4s).count();");
27140 verifyFormat("auto x = 5s .count() == 5;");
27141}
27142
27143TEST_F(FormatTest, InterfaceAsClassMemberName) {
27144 verifyFormat("class Foo {\n"
27145 " int interface;\n"
27146 " Foo::Foo(int iface) : interface{iface} {}\n"
27147 "}");
27148}
27149
27150TEST_F(FormatTest, PreprocessorOverlappingRegions) {
27151 verifyFormat("#ifdef\n\n"
27152 "#else\n"
27153 "#endif",
27154 "#ifdef \n"
27155 " \n"
27156 "\n"
27157 "#else \n"
27158 "#endif ",
27159 getGoogleStyle());
27160}
27161
27162TEST_F(FormatTest, RemoveParentheses) {
27163 FormatStyle Style = getLLVMStyle();
27164 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
27165
27166 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
27167 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
27168 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
27169 verifyFormat("decltype((foo->bar)) baz;", Style);
27170 verifyFormat("class __declspec(dllimport) X {};",
27171 "class __declspec((dllimport)) X {};", Style);
27172 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
27173 verifyFormat("while (a)\n"
27174 " b;",
27175 "while (((a)))\n"
27176 " b;",
27177 Style);
27178 verifyFormat("while ((a = b))\n"
27179 " c;",
27180 "while (((a = b)))\n"
27181 " c;",
27182 Style);
27183 verifyFormat("if (a)\n"
27184 " b;",
27185 "if (((a)))\n"
27186 " b;",
27187 Style);
27188 verifyFormat("if constexpr ((a = b))\n"
27189 " c;",
27190 "if constexpr (((a = b)))\n"
27191 " c;",
27192 Style);
27193 verifyFormat("if (({ a; }))\n"
27194 " b;",
27195 "if ((({ a; })))\n"
27196 " b;",
27197 Style);
27198 verifyFormat("return (0);", "return (((0)));", Style);
27199 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
27200
27201 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
27202 verifyFormat("#define Return0 return (0);", Style);
27203 verifyFormat("return 0;", "return (0);", Style);
27204 verifyFormat("co_return 0;", "co_return ((0));", Style);
27205 verifyFormat("return 0;", "return (((0)));", Style);
27206 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
27207 verifyFormat("inline decltype(auto) f() {\n"
27208 " if (a) {\n"
27209 " return (a);\n"
27210 " }\n"
27211 " return (b);\n"
27212 "}",
27213 "inline decltype(auto) f() {\n"
27214 " if (a) {\n"
27215 " return ((a));\n"
27216 " }\n"
27217 " return ((b));\n"
27218 "}",
27219 Style);
27220 verifyFormat("auto g() {\n"
27221 " decltype(auto) x = [] {\n"
27222 " auto y = [] {\n"
27223 " if (a) {\n"
27224 " return a;\n"
27225 " }\n"
27226 " return b;\n"
27227 " };\n"
27228 " if (c) {\n"
27229 " return (c);\n"
27230 " }\n"
27231 " return (d);\n"
27232 " };\n"
27233 " if (e) {\n"
27234 " return e;\n"
27235 " }\n"
27236 " return f;\n"
27237 "}",
27238 "auto g() {\n"
27239 " decltype(auto) x = [] {\n"
27240 " auto y = [] {\n"
27241 " if (a) {\n"
27242 " return ((a));\n"
27243 " }\n"
27244 " return ((b));\n"
27245 " };\n"
27246 " if (c) {\n"
27247 " return ((c));\n"
27248 " }\n"
27249 " return ((d));\n"
27250 " };\n"
27251 " if (e) {\n"
27252 " return ((e));\n"
27253 " }\n"
27254 " return ((f));\n"
27255 "}",
27256 Style);
27257
27258 Style.ColumnLimit = 25;
27259 verifyFormat("return (a + b) - (c + d);",
27260 "return (((a + b)) -\n"
27261 " ((c + d)));",
27262 Style);
27263}
27264
27265TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
27266 auto Style = getLLVMStyleWithColumns(ColumnLimit: 35);
27267
27268 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
27269 verifyFormat("void foo(int arg1,\n"
27270 " double arg2) noexcept;",
27271 Style);
27272
27273 // The following line does not fit within the 35 column limit, but that's what
27274 // happens with no break allowed.
27275 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27276 " noexcept(baz(arg1)) &&\n"
27277 " noexcept(baz(arg2)));",
27278 Style);
27279
27280 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27281 Style);
27282
27283 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
27284 verifyFormat("void foo(int arg1,\n"
27285 " double arg2) noexcept;",
27286 Style);
27287
27288 verifyFormat("void bar(int arg1, double arg2)\n"
27289 " noexcept(noexcept(baz(arg1)) &&\n"
27290 " noexcept(baz(arg2)));",
27291 Style);
27292
27293 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27294 " noexcept;",
27295 Style);
27296
27297 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
27298 verifyFormat("void foo(int arg1,\n"
27299 " double arg2) noexcept;",
27300 Style);
27301
27302 verifyFormat("void bar(int arg1, double arg2)\n"
27303 " noexcept(noexcept(baz(arg1)) &&\n"
27304 " noexcept(baz(arg2)));",
27305 Style);
27306
27307 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27308 Style);
27309}
27310
27311TEST_F(FormatTest, PPBranchesInBracedInit) {
27312 verifyFormat("A a_{kFlag1,\n"
27313 "#if BUILD_FLAG\n"
27314 " kFlag2,\n"
27315 "#else\n"
27316 " kFlag3,\n"
27317 "#endif\n"
27318 " kFlag4};",
27319 "A a_{\n"
27320 " kFlag1,\n"
27321 "#if BUILD_FLAG\n"
27322 " kFlag2,\n"
27323 "#else\n"
27324 " kFlag3,\n"
27325 "#endif\n"
27326 " kFlag4\n"
27327 "};");
27328}
27329
27330TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
27331 verifyFormat("{\n"
27332 " char *a[] = {\n"
27333 " /* abc */ \"abc\",\n"
27334 "#if FOO\n"
27335 " /* xyz */ \"xyz\",\n"
27336 "#endif\n"
27337 " /* last */ \"last\"};\n"
27338 "}",
27339 getLLVMStyleWithColumns(30));
27340}
27341
27342TEST_F(FormatTest, BreakAdjacentStringLiterals) {
27343 constexpr StringRef Code{
27344 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27345
27346 verifyFormat("return \"Code\"\n"
27347 " \"\\0\\52\\26\\55\\55\\0\"\n"
27348 " \"x013\"\n"
27349 " \"\\02\\xBA\";",
27350 Code);
27351
27352 auto Style = getLLVMStyle();
27353 Style.BreakAdjacentStringLiterals = false;
27354 verifyFormat(Code, Style);
27355}
27356
27357} // namespace
27358} // namespace test
27359} // namespace format
27360} // namespace clang
27361

source code of clang/unittests/Format/FormatTest.cpp