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 auto Style = getLLVMStyle();
50 Style.KeepEmptyLines.AtStartOfFile = false;
51 verifyFormat("int i;", " \n\t \v \f int i;", Style);
52}
53
54TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
55 verifyFormat("int i;", "int\ni;");
56}
57
58TEST_F(FormatTest, FormatsNestedBlockStatements) {
59 verifyFormat("{\n"
60 " {\n"
61 " {\n"
62 " }\n"
63 " }\n"
64 "}",
65 "{{{}}}");
66}
67
68TEST_F(FormatTest, FormatsNestedCall) {
69 verifyFormat("Method(f1, f2(f3));");
70 verifyFormat("Method(f1(f2, f3()));");
71 verifyFormat("Method(f1(f2, (f3())));");
72}
73
74TEST_F(FormatTest, NestedNameSpecifiers) {
75 verifyFormat("vector<::Type> v;");
76 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
77 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
78 verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
79 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
80 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
81 verifyFormat("bool a = 2 < ::SomeFunction();");
82 verifyFormat("ALWAYS_INLINE ::std::string getName();");
83 verifyFormat("some::string getName();");
84}
85
86TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
87 verifyFormat("if (a) {\n"
88 " f();\n"
89 "}",
90 "if(a){f();}");
91 EXPECT_EQ(4, ReplacementCount);
92 verifyNoChange("if (a) {\n"
93 " f();\n"
94 "}");
95 EXPECT_EQ(0, ReplacementCount);
96 verifyNoChange("/*\r\n"
97 "\r\n"
98 "*/");
99 EXPECT_EQ(0, ReplacementCount);
100}
101
102TEST_F(FormatTest, RemovesEmptyLines) {
103 verifyFormat("class C {\n"
104 " int i;\n"
105 "};",
106 "class C {\n"
107 " int i;\n"
108 "\n"
109 "};");
110
111 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
112 verifyFormat("namespace N {\n"
113 "\n"
114 "int i;\n"
115 "}",
116 "namespace N {\n"
117 "\n"
118 "int i;\n"
119 "}",
120 getGoogleStyle());
121 verifyFormat("/* something */ namespace N {\n"
122 "\n"
123 "int i;\n"
124 "}",
125 "/* something */ namespace N {\n"
126 "\n"
127 "int i;\n"
128 "}",
129 getGoogleStyle());
130 verifyFormat("inline namespace N {\n"
131 "\n"
132 "int i;\n"
133 "}",
134 "inline namespace N {\n"
135 "\n"
136 "int i;\n"
137 "}",
138 getGoogleStyle());
139 verifyFormat("/* something */ inline namespace N {\n"
140 "\n"
141 "int i;\n"
142 "}",
143 "/* something */ inline namespace N {\n"
144 "\n"
145 "int i;\n"
146 "}",
147 getGoogleStyle());
148 verifyFormat("export namespace N {\n"
149 "\n"
150 "int i;\n"
151 "}",
152 "export namespace N {\n"
153 "\n"
154 "int i;\n"
155 "}",
156 getGoogleStyle());
157 verifyFormat("extern /**/ \"C\" /**/ {\n"
158 "\n"
159 "int i;\n"
160 "}",
161 "extern /**/ \"C\" /**/ {\n"
162 "\n"
163 "int i;\n"
164 "}",
165 getGoogleStyle());
166
167 auto CustomStyle = getLLVMStyle();
168 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
169 CustomStyle.BraceWrapping.AfterNamespace = true;
170 CustomStyle.KeepEmptyLines.AtStartOfBlock = false;
171 verifyFormat("namespace N\n"
172 "{\n"
173 "\n"
174 "int i;\n"
175 "}",
176 "namespace N\n"
177 "{\n"
178 "\n"
179 "\n"
180 "int i;\n"
181 "}",
182 CustomStyle);
183 verifyFormat("/* something */ namespace N\n"
184 "{\n"
185 "\n"
186 "int i;\n"
187 "}",
188 "/* something */ namespace N {\n"
189 "\n"
190 "\n"
191 "int i;\n"
192 "}",
193 CustomStyle);
194 verifyFormat("inline namespace N\n"
195 "{\n"
196 "\n"
197 "int i;\n"
198 "}",
199 "inline namespace N\n"
200 "{\n"
201 "\n"
202 "\n"
203 "int i;\n"
204 "}",
205 CustomStyle);
206 verifyFormat("/* something */ inline namespace N\n"
207 "{\n"
208 "\n"
209 "int i;\n"
210 "}",
211 "/* something */ inline namespace N\n"
212 "{\n"
213 "\n"
214 "int i;\n"
215 "}",
216 CustomStyle);
217 verifyFormat("export namespace N\n"
218 "{\n"
219 "\n"
220 "int i;\n"
221 "}",
222 "export namespace N\n"
223 "{\n"
224 "\n"
225 "int i;\n"
226 "}",
227 CustomStyle);
228 verifyFormat("namespace a\n"
229 "{\n"
230 "namespace b\n"
231 "{\n"
232 "\n"
233 "class AA {};\n"
234 "\n"
235 "} // namespace b\n"
236 "} // namespace a",
237 "namespace a\n"
238 "{\n"
239 "namespace b\n"
240 "{\n"
241 "\n"
242 "\n"
243 "class AA {};\n"
244 "\n"
245 "\n"
246 "}\n"
247 "}",
248 CustomStyle);
249 verifyFormat("namespace A /* comment */\n"
250 "{\n"
251 "class B {}\n"
252 "} // namespace A",
253 "namespace A /* comment */ { class B {} }", CustomStyle);
254 verifyFormat("namespace A\n"
255 "{ /* comment */\n"
256 "class B {}\n"
257 "} // namespace A",
258 "namespace A {/* comment */ class B {} }", CustomStyle);
259 verifyFormat("namespace A\n"
260 "{ /* comment */\n"
261 "\n"
262 "class B {}\n"
263 "\n"
264 ""
265 "} // namespace A",
266 "namespace A { /* comment */\n"
267 "\n"
268 "\n"
269 "class B {}\n"
270 "\n"
271 "\n"
272 "}",
273 CustomStyle);
274 verifyFormat("namespace A /* comment */\n"
275 "{\n"
276 "\n"
277 "class B {}\n"
278 "\n"
279 "} // namespace A",
280 "namespace A/* comment */ {\n"
281 "\n"
282 "\n"
283 "class B {}\n"
284 "\n"
285 "\n"
286 "}",
287 CustomStyle);
288
289 // ...but do keep inlining and removing empty lines for non-block extern "C"
290 // functions.
291 verifyGoogleFormat("extern \"C\" int f() { return 42; }");
292 verifyFormat("extern \"C\" int f() {\n"
293 " int i = 42;\n"
294 " return i;\n"
295 "}",
296 "extern \"C\" int f() {\n"
297 "\n"
298 " int i = 42;\n"
299 " return i;\n"
300 "}",
301 getGoogleStyle());
302
303 // Remove empty lines at the beginning and end of blocks.
304 verifyFormat("void f() {\n"
305 "\n"
306 " if (a) {\n"
307 "\n"
308 " f();\n"
309 " }\n"
310 "}",
311 "void f() {\n"
312 "\n"
313 " if (a) {\n"
314 "\n"
315 " f();\n"
316 "\n"
317 " }\n"
318 "\n"
319 "}");
320 verifyFormat("void f() {\n"
321 " if (a) {\n"
322 " f();\n"
323 " }\n"
324 "}",
325 "void f() {\n"
326 "\n"
327 " if (a) {\n"
328 "\n"
329 " f();\n"
330 "\n"
331 " }\n"
332 "\n"
333 "}",
334 getGoogleStyle());
335
336 // Don't remove empty lines in more complex control statements.
337 verifyFormat("void f() {\n"
338 " if (a) {\n"
339 " f();\n"
340 "\n"
341 " } else if (b) {\n"
342 " f();\n"
343 " }\n"
344 "}",
345 "void f() {\n"
346 " if (a) {\n"
347 " f();\n"
348 "\n"
349 " } else if (b) {\n"
350 " f();\n"
351 "\n"
352 " }\n"
353 "\n"
354 "}");
355
356 // Don't remove empty lines before namespace endings.
357 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
358 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
359 verifyNoChange("namespace {\n"
360 "int i;\n"
361 "\n"
362 "}",
363 LLVMWithNoNamespaceFix);
364 verifyFormat("namespace {\n"
365 "int i;\n"
366 "}",
367 LLVMWithNoNamespaceFix);
368 verifyNoChange("namespace {\n"
369 "int i;\n"
370 "\n"
371 "};",
372 LLVMWithNoNamespaceFix);
373 verifyFormat("namespace {\n"
374 "int i;\n"
375 "};",
376 LLVMWithNoNamespaceFix);
377 verifyNoChange("namespace {\n"
378 "int i;\n"
379 "\n"
380 "}");
381 verifyFormat("namespace {\n"
382 "int i;\n"
383 "\n"
384 "} // namespace",
385 "namespace {\n"
386 "int i;\n"
387 "\n"
388 "} // namespace");
389
390 FormatStyle Style = getLLVMStyle();
391 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
392 Style.MaxEmptyLinesToKeep = 2;
393 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
394 Style.BraceWrapping.AfterClass = true;
395 Style.BraceWrapping.AfterFunction = true;
396 Style.KeepEmptyLines.AtStartOfBlock = false;
397
398 verifyFormat("class Foo\n"
399 "{\n"
400 " Foo() {}\n"
401 "\n"
402 " void funk() {}\n"
403 "};",
404 "class Foo\n"
405 "{\n"
406 " Foo()\n"
407 " {\n"
408 " }\n"
409 "\n"
410 " void funk() {}\n"
411 "};",
412 Style);
413}
414
415TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
416 verifyFormat("x = (a) and (b);");
417 verifyFormat("x = (a) or (b);");
418 verifyFormat("x = (a) bitand (b);");
419 verifyFormat("x = (a) bitor (b);");
420 verifyFormat("x = (a) not_eq (b);");
421 verifyFormat("x = (a) and_eq (b);");
422 verifyFormat("x = (a) or_eq (b);");
423 verifyFormat("x = (a) xor (b);");
424}
425
426TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
427 verifyFormat("x = compl(a);");
428 verifyFormat("x = not(a);");
429 verifyFormat("x = bitand(a);");
430 // Unary operator must not be merged with the next identifier
431 verifyFormat("x = compl a;");
432 verifyFormat("x = not a;");
433 verifyFormat("x = bitand a;");
434}
435
436//===----------------------------------------------------------------------===//
437// Tests for control statements.
438//===----------------------------------------------------------------------===//
439
440TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
441 verifyFormat("if (true)\n f();\ng();");
442 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
443 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
444 verifyFormat("if constexpr (true)\n"
445 " f();\ng();");
446 verifyFormat("if CONSTEXPR (true)\n"
447 " f();\ng();");
448 verifyFormat("if constexpr (a)\n"
449 " if constexpr (b)\n"
450 " if constexpr (c)\n"
451 " g();\n"
452 "h();");
453 verifyFormat("if CONSTEXPR (a)\n"
454 " if CONSTEXPR (b)\n"
455 " if CONSTEXPR (c)\n"
456 " g();\n"
457 "h();");
458 verifyFormat("if constexpr (a)\n"
459 " if constexpr (b) {\n"
460 " f();\n"
461 " }\n"
462 "g();");
463 verifyFormat("if CONSTEXPR (a)\n"
464 " if CONSTEXPR (b) {\n"
465 " f();\n"
466 " }\n"
467 "g();");
468
469 verifyFormat("if consteval {\n}");
470 verifyFormat("if !consteval {\n}");
471 verifyFormat("if not consteval {\n}");
472 verifyFormat("if consteval {\n} else {\n}");
473 verifyFormat("if !consteval {\n} else {\n}");
474 verifyFormat("if consteval {\n"
475 " f();\n"
476 "}");
477 verifyFormat("if !consteval {\n"
478 " f();\n"
479 "}");
480 verifyFormat("if consteval {\n"
481 " f();\n"
482 "} else {\n"
483 " g();\n"
484 "}");
485 verifyFormat("if CONSTEVAL {\n"
486 " f();\n"
487 "}");
488 verifyFormat("if !CONSTEVAL {\n"
489 " f();\n"
490 "}");
491
492 verifyFormat("if (a)\n"
493 " g();");
494 verifyFormat("if (a) {\n"
495 " g()\n"
496 "};");
497 verifyFormat("if (a)\n"
498 " g();\n"
499 "else\n"
500 " g();");
501 verifyFormat("if (a) {\n"
502 " g();\n"
503 "} else\n"
504 " g();");
505 verifyFormat("if (a)\n"
506 " g();\n"
507 "else {\n"
508 " g();\n"
509 "}");
510 verifyFormat("if (a) {\n"
511 " g();\n"
512 "} else {\n"
513 " g();\n"
514 "}");
515 verifyFormat("if (a)\n"
516 " g();\n"
517 "else if (b)\n"
518 " g();\n"
519 "else\n"
520 " g();");
521 verifyFormat("if (a) {\n"
522 " g();\n"
523 "} else if (b)\n"
524 " g();\n"
525 "else\n"
526 " g();");
527 verifyFormat("if (a)\n"
528 " g();\n"
529 "else if (b) {\n"
530 " g();\n"
531 "} else\n"
532 " g();");
533 verifyFormat("if (a)\n"
534 " g();\n"
535 "else if (b)\n"
536 " g();\n"
537 "else {\n"
538 " g();\n"
539 "}");
540 verifyFormat("if (a)\n"
541 " g();\n"
542 "else if (b) {\n"
543 " g();\n"
544 "} else {\n"
545 " g();\n"
546 "}");
547 verifyFormat("if (a) {\n"
548 " g();\n"
549 "} else if (b) {\n"
550 " g();\n"
551 "} else {\n"
552 " g();\n"
553 "}");
554
555 FormatStyle AllowsMergedIf = getLLVMStyle();
556 AllowsMergedIf.IfMacros.push_back(x: "MYIF");
557 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
558 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
559 FormatStyle::SIS_WithoutElse;
560 verifyFormat("if (a)\n"
561 " // comment\n"
562 " f();",
563 AllowsMergedIf);
564 verifyFormat("{\n"
565 " if (a)\n"
566 " label:\n"
567 " f();\n"
568 "}",
569 AllowsMergedIf);
570 verifyFormat("#define A \\\n"
571 " if (a) \\\n"
572 " label: \\\n"
573 " f()",
574 AllowsMergedIf);
575 verifyFormat("if (a)\n"
576 " ;",
577 AllowsMergedIf);
578 verifyFormat("if (a)\n"
579 " if (b) return;",
580 AllowsMergedIf);
581
582 verifyFormat("if (a) // Can't merge this\n"
583 " f();",
584 AllowsMergedIf);
585 verifyFormat("if (a) /* still don't merge */\n"
586 " f();",
587 AllowsMergedIf);
588 verifyFormat("if (a) { // Never merge this\n"
589 " f();\n"
590 "}",
591 AllowsMergedIf);
592 verifyFormat("if (a) { /* Never merge this */\n"
593 " f();\n"
594 "}",
595 AllowsMergedIf);
596 verifyFormat("MYIF (a)\n"
597 " // comment\n"
598 " f();",
599 AllowsMergedIf);
600 verifyFormat("{\n"
601 " MYIF (a)\n"
602 " label:\n"
603 " f();\n"
604 "}",
605 AllowsMergedIf);
606 verifyFormat("#define A \\\n"
607 " MYIF (a) \\\n"
608 " label: \\\n"
609 " f()",
610 AllowsMergedIf);
611 verifyFormat("MYIF (a)\n"
612 " ;",
613 AllowsMergedIf);
614 verifyFormat("MYIF (a)\n"
615 " MYIF (b) return;",
616 AllowsMergedIf);
617
618 verifyFormat("MYIF (a) // Can't merge this\n"
619 " f();",
620 AllowsMergedIf);
621 verifyFormat("MYIF (a) /* still don't merge */\n"
622 " f();",
623 AllowsMergedIf);
624 verifyFormat("MYIF (a) { // Never merge this\n"
625 " f();\n"
626 "}",
627 AllowsMergedIf);
628 verifyFormat("MYIF (a) { /* Never merge this */\n"
629 " f();\n"
630 "}",
631 AllowsMergedIf);
632
633 AllowsMergedIf.ColumnLimit = 14;
634 // Where line-lengths matter, a 2-letter synonym that maintains line length.
635 // Not IF to avoid any confusion that IF is somehow special.
636 AllowsMergedIf.IfMacros.push_back(x: "FI");
637 verifyFormat("if (a) return;", AllowsMergedIf);
638 verifyFormat("if (aaaaaaaaa)\n"
639 " return;",
640 AllowsMergedIf);
641 verifyFormat("FI (a) return;", AllowsMergedIf);
642 verifyFormat("FI (aaaaaaaaa)\n"
643 " return;",
644 AllowsMergedIf);
645
646 AllowsMergedIf.ColumnLimit = 13;
647 verifyFormat("if (a)\n return;", AllowsMergedIf);
648 verifyFormat("FI (a)\n return;", AllowsMergedIf);
649
650 FormatStyle AllowsMergedIfElse = getLLVMStyle();
651 AllowsMergedIfElse.IfMacros.push_back(x: "MYIF");
652 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine =
653 FormatStyle::SIS_AllIfsAndElse;
654 verifyFormat("if (a)\n"
655 " // comment\n"
656 " f();\n"
657 "else\n"
658 " // comment\n"
659 " f();",
660 AllowsMergedIfElse);
661 verifyFormat("{\n"
662 " if (a)\n"
663 " label:\n"
664 " f();\n"
665 " else\n"
666 " label:\n"
667 " f();\n"
668 "}",
669 AllowsMergedIfElse);
670 verifyFormat("if (a)\n"
671 " ;\n"
672 "else\n"
673 " ;",
674 AllowsMergedIfElse);
675 verifyFormat("if (a) {\n"
676 "} else {\n"
677 "}",
678 AllowsMergedIfElse);
679 verifyFormat("if (a) return;\n"
680 "else if (b) return;\n"
681 "else return;",
682 AllowsMergedIfElse);
683 verifyFormat("if (a) {\n"
684 "} else return;",
685 AllowsMergedIfElse);
686 verifyFormat("if (a) {\n"
687 "} else if (b) return;\n"
688 "else return;",
689 AllowsMergedIfElse);
690 verifyFormat("if (a) return;\n"
691 "else if (b) {\n"
692 "} else return;",
693 AllowsMergedIfElse);
694 verifyFormat("if (a)\n"
695 " if (b) return;\n"
696 " else return;",
697 AllowsMergedIfElse);
698 verifyFormat("if constexpr (a)\n"
699 " if constexpr (b) return;\n"
700 " else if constexpr (c) return;\n"
701 " else return;",
702 AllowsMergedIfElse);
703 verifyFormat("MYIF (a)\n"
704 " // comment\n"
705 " f();\n"
706 "else\n"
707 " // comment\n"
708 " f();",
709 AllowsMergedIfElse);
710 verifyFormat("{\n"
711 " MYIF (a)\n"
712 " label:\n"
713 " f();\n"
714 " else\n"
715 " label:\n"
716 " f();\n"
717 "}",
718 AllowsMergedIfElse);
719 verifyFormat("MYIF (a)\n"
720 " ;\n"
721 "else\n"
722 " ;",
723 AllowsMergedIfElse);
724 verifyFormat("MYIF (a) {\n"
725 "} else {\n"
726 "}",
727 AllowsMergedIfElse);
728 verifyFormat("MYIF (a) return;\n"
729 "else MYIF (b) return;\n"
730 "else return;",
731 AllowsMergedIfElse);
732 verifyFormat("MYIF (a) {\n"
733 "} else return;",
734 AllowsMergedIfElse);
735 verifyFormat("MYIF (a) {\n"
736 "} else MYIF (b) return;\n"
737 "else return;",
738 AllowsMergedIfElse);
739 verifyFormat("MYIF (a) return;\n"
740 "else MYIF (b) {\n"
741 "} else return;",
742 AllowsMergedIfElse);
743 verifyFormat("MYIF (a)\n"
744 " MYIF (b) return;\n"
745 " else return;",
746 AllowsMergedIfElse);
747 verifyFormat("MYIF constexpr (a)\n"
748 " MYIF constexpr (b) return;\n"
749 " else MYIF constexpr (c) return;\n"
750 " else return;",
751 AllowsMergedIfElse);
752}
753
754TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
755 FormatStyle AllowsMergedIf = getLLVMStyle();
756 AllowsMergedIf.IfMacros.push_back(x: "MYIF");
757 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
758 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
759 FormatStyle::SIS_WithoutElse;
760 verifyFormat("if (a)\n"
761 " f();\n"
762 "else {\n"
763 " g();\n"
764 "}",
765 AllowsMergedIf);
766 verifyFormat("if (a)\n"
767 " f();\n"
768 "else\n"
769 " g();",
770 AllowsMergedIf);
771
772 verifyFormat("if (a) g();", AllowsMergedIf);
773 verifyFormat("if (a) {\n"
774 " g()\n"
775 "};",
776 AllowsMergedIf);
777 verifyFormat("if (a)\n"
778 " g();\n"
779 "else\n"
780 " g();",
781 AllowsMergedIf);
782 verifyFormat("if (a) {\n"
783 " g();\n"
784 "} else\n"
785 " g();",
786 AllowsMergedIf);
787 verifyFormat("if (a)\n"
788 " g();\n"
789 "else {\n"
790 " g();\n"
791 "}",
792 AllowsMergedIf);
793 verifyFormat("if (a) {\n"
794 " g();\n"
795 "} else {\n"
796 " g();\n"
797 "}",
798 AllowsMergedIf);
799 verifyFormat("if (a)\n"
800 " g();\n"
801 "else if (b)\n"
802 " g();\n"
803 "else\n"
804 " g();",
805 AllowsMergedIf);
806 verifyFormat("if (a) {\n"
807 " g();\n"
808 "} else if (b)\n"
809 " g();\n"
810 "else\n"
811 " g();",
812 AllowsMergedIf);
813 verifyFormat("if (a)\n"
814 " g();\n"
815 "else if (b) {\n"
816 " g();\n"
817 "} else\n"
818 " g();",
819 AllowsMergedIf);
820 verifyFormat("if (a)\n"
821 " g();\n"
822 "else if (b)\n"
823 " g();\n"
824 "else {\n"
825 " g();\n"
826 "}",
827 AllowsMergedIf);
828 verifyFormat("if (a)\n"
829 " g();\n"
830 "else if (b) {\n"
831 " g();\n"
832 "} else {\n"
833 " g();\n"
834 "}",
835 AllowsMergedIf);
836 verifyFormat("if (a) {\n"
837 " g();\n"
838 "} else if (b) {\n"
839 " g();\n"
840 "} else {\n"
841 " g();\n"
842 "}",
843 AllowsMergedIf);
844 verifyFormat("MYIF (a)\n"
845 " f();\n"
846 "else {\n"
847 " g();\n"
848 "}",
849 AllowsMergedIf);
850 verifyFormat("MYIF (a)\n"
851 " f();\n"
852 "else\n"
853 " g();",
854 AllowsMergedIf);
855
856 verifyFormat("MYIF (a) g();", AllowsMergedIf);
857 verifyFormat("MYIF (a) {\n"
858 " g()\n"
859 "};",
860 AllowsMergedIf);
861 verifyFormat("MYIF (a)\n"
862 " g();\n"
863 "else\n"
864 " g();",
865 AllowsMergedIf);
866 verifyFormat("MYIF (a) {\n"
867 " g();\n"
868 "} else\n"
869 " g();",
870 AllowsMergedIf);
871 verifyFormat("MYIF (a)\n"
872 " g();\n"
873 "else {\n"
874 " g();\n"
875 "}",
876 AllowsMergedIf);
877 verifyFormat("MYIF (a) {\n"
878 " g();\n"
879 "} else {\n"
880 " g();\n"
881 "}",
882 AllowsMergedIf);
883 verifyFormat("MYIF (a)\n"
884 " g();\n"
885 "else MYIF (b)\n"
886 " g();\n"
887 "else\n"
888 " g();",
889 AllowsMergedIf);
890 verifyFormat("MYIF (a)\n"
891 " g();\n"
892 "else if (b)\n"
893 " g();\n"
894 "else\n"
895 " g();",
896 AllowsMergedIf);
897 verifyFormat("MYIF (a) {\n"
898 " g();\n"
899 "} else MYIF (b)\n"
900 " g();\n"
901 "else\n"
902 " g();",
903 AllowsMergedIf);
904 verifyFormat("MYIF (a) {\n"
905 " g();\n"
906 "} else if (b)\n"
907 " g();\n"
908 "else\n"
909 " g();",
910 AllowsMergedIf);
911 verifyFormat("MYIF (a)\n"
912 " g();\n"
913 "else MYIF (b) {\n"
914 " g();\n"
915 "} else\n"
916 " g();",
917 AllowsMergedIf);
918 verifyFormat("MYIF (a)\n"
919 " g();\n"
920 "else if (b) {\n"
921 " g();\n"
922 "} else\n"
923 " g();",
924 AllowsMergedIf);
925 verifyFormat("MYIF (a)\n"
926 " g();\n"
927 "else MYIF (b)\n"
928 " g();\n"
929 "else {\n"
930 " g();\n"
931 "}",
932 AllowsMergedIf);
933 verifyFormat("MYIF (a)\n"
934 " g();\n"
935 "else if (b)\n"
936 " g();\n"
937 "else {\n"
938 " g();\n"
939 "}",
940 AllowsMergedIf);
941 verifyFormat("MYIF (a)\n"
942 " g();\n"
943 "else MYIF (b) {\n"
944 " g();\n"
945 "} else {\n"
946 " g();\n"
947 "}",
948 AllowsMergedIf);
949 verifyFormat("MYIF (a)\n"
950 " g();\n"
951 "else if (b) {\n"
952 " g();\n"
953 "} else {\n"
954 " g();\n"
955 "}",
956 AllowsMergedIf);
957 verifyFormat("MYIF (a) {\n"
958 " g();\n"
959 "} else MYIF (b) {\n"
960 " g();\n"
961 "} else {\n"
962 " g();\n"
963 "}",
964 AllowsMergedIf);
965 verifyFormat("MYIF (a) {\n"
966 " g();\n"
967 "} else if (b) {\n"
968 " g();\n"
969 "} else {\n"
970 " g();\n"
971 "}",
972 AllowsMergedIf);
973
974 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
975 FormatStyle::SIS_OnlyFirstIf;
976
977 verifyFormat("if (a) f();\n"
978 "else {\n"
979 " g();\n"
980 "}",
981 AllowsMergedIf);
982 verifyFormat("if (a) f();\n"
983 "else {\n"
984 " if (a) f();\n"
985 " else {\n"
986 " g();\n"
987 " }\n"
988 " g();\n"
989 "}",
990 AllowsMergedIf);
991
992 verifyFormat("if (a) g();", AllowsMergedIf);
993 verifyFormat("if (a) {\n"
994 " g()\n"
995 "};",
996 AllowsMergedIf);
997 verifyFormat("if (a) g();\n"
998 "else\n"
999 " g();",
1000 AllowsMergedIf);
1001 verifyFormat("if (a) {\n"
1002 " g();\n"
1003 "} else\n"
1004 " g();",
1005 AllowsMergedIf);
1006 verifyFormat("if (a) g();\n"
1007 "else {\n"
1008 " g();\n"
1009 "}",
1010 AllowsMergedIf);
1011 verifyFormat("if (a) {\n"
1012 " g();\n"
1013 "} else {\n"
1014 " g();\n"
1015 "}",
1016 AllowsMergedIf);
1017 verifyFormat("if (a) g();\n"
1018 "else if (b)\n"
1019 " g();\n"
1020 "else\n"
1021 " g();",
1022 AllowsMergedIf);
1023 verifyFormat("if (a) {\n"
1024 " g();\n"
1025 "} else if (b)\n"
1026 " g();\n"
1027 "else\n"
1028 " g();",
1029 AllowsMergedIf);
1030 verifyFormat("if (a) g();\n"
1031 "else if (b) {\n"
1032 " g();\n"
1033 "} else\n"
1034 " g();",
1035 AllowsMergedIf);
1036 verifyFormat("if (a) g();\n"
1037 "else if (b)\n"
1038 " g();\n"
1039 "else {\n"
1040 " g();\n"
1041 "}",
1042 AllowsMergedIf);
1043 verifyFormat("if (a) g();\n"
1044 "else if (b) {\n"
1045 " g();\n"
1046 "} else {\n"
1047 " g();\n"
1048 "}",
1049 AllowsMergedIf);
1050 verifyFormat("if (a) {\n"
1051 " g();\n"
1052 "} else if (b) {\n"
1053 " g();\n"
1054 "} else {\n"
1055 " g();\n"
1056 "}",
1057 AllowsMergedIf);
1058 verifyFormat("MYIF (a) f();\n"
1059 "else {\n"
1060 " g();\n"
1061 "}",
1062 AllowsMergedIf);
1063 verifyFormat("MYIF (a) f();\n"
1064 "else {\n"
1065 " if (a) f();\n"
1066 " else {\n"
1067 " g();\n"
1068 " }\n"
1069 " g();\n"
1070 "}",
1071 AllowsMergedIf);
1072
1073 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1074 verifyFormat("MYIF (a) {\n"
1075 " g()\n"
1076 "};",
1077 AllowsMergedIf);
1078 verifyFormat("MYIF (a) g();\n"
1079 "else\n"
1080 " g();",
1081 AllowsMergedIf);
1082 verifyFormat("MYIF (a) {\n"
1083 " g();\n"
1084 "} else\n"
1085 " g();",
1086 AllowsMergedIf);
1087 verifyFormat("MYIF (a) g();\n"
1088 "else {\n"
1089 " g();\n"
1090 "}",
1091 AllowsMergedIf);
1092 verifyFormat("MYIF (a) {\n"
1093 " g();\n"
1094 "} else {\n"
1095 " g();\n"
1096 "}",
1097 AllowsMergedIf);
1098 verifyFormat("MYIF (a) g();\n"
1099 "else MYIF (b)\n"
1100 " g();\n"
1101 "else\n"
1102 " g();",
1103 AllowsMergedIf);
1104 verifyFormat("MYIF (a) g();\n"
1105 "else if (b)\n"
1106 " g();\n"
1107 "else\n"
1108 " g();",
1109 AllowsMergedIf);
1110 verifyFormat("MYIF (a) {\n"
1111 " g();\n"
1112 "} else MYIF (b)\n"
1113 " g();\n"
1114 "else\n"
1115 " g();",
1116 AllowsMergedIf);
1117 verifyFormat("MYIF (a) {\n"
1118 " g();\n"
1119 "} else if (b)\n"
1120 " g();\n"
1121 "else\n"
1122 " g();",
1123 AllowsMergedIf);
1124 verifyFormat("MYIF (a) g();\n"
1125 "else MYIF (b) {\n"
1126 " g();\n"
1127 "} else\n"
1128 " g();",
1129 AllowsMergedIf);
1130 verifyFormat("MYIF (a) g();\n"
1131 "else if (b) {\n"
1132 " g();\n"
1133 "} else\n"
1134 " g();",
1135 AllowsMergedIf);
1136 verifyFormat("MYIF (a) g();\n"
1137 "else MYIF (b)\n"
1138 " g();\n"
1139 "else {\n"
1140 " g();\n"
1141 "}",
1142 AllowsMergedIf);
1143 verifyFormat("MYIF (a) g();\n"
1144 "else if (b)\n"
1145 " g();\n"
1146 "else {\n"
1147 " g();\n"
1148 "}",
1149 AllowsMergedIf);
1150 verifyFormat("MYIF (a) g();\n"
1151 "else MYIF (b) {\n"
1152 " g();\n"
1153 "} else {\n"
1154 " g();\n"
1155 "}",
1156 AllowsMergedIf);
1157 verifyFormat("MYIF (a) g();\n"
1158 "else if (b) {\n"
1159 " g();\n"
1160 "} else {\n"
1161 " g();\n"
1162 "}",
1163 AllowsMergedIf);
1164 verifyFormat("MYIF (a) {\n"
1165 " g();\n"
1166 "} else MYIF (b) {\n"
1167 " g();\n"
1168 "} else {\n"
1169 " g();\n"
1170 "}",
1171 AllowsMergedIf);
1172 verifyFormat("MYIF (a) {\n"
1173 " g();\n"
1174 "} else if (b) {\n"
1175 " g();\n"
1176 "} else {\n"
1177 " g();\n"
1178 "}",
1179 AllowsMergedIf);
1180
1181 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
1182 FormatStyle::SIS_AllIfsAndElse;
1183
1184 verifyFormat("if (a) f();\n"
1185 "else {\n"
1186 " g();\n"
1187 "}",
1188 AllowsMergedIf);
1189 verifyFormat("if (a) f();\n"
1190 "else {\n"
1191 " if (a) f();\n"
1192 " else {\n"
1193 " g();\n"
1194 " }\n"
1195 " g();\n"
1196 "}",
1197 AllowsMergedIf);
1198
1199 verifyFormat("if (a) g();", AllowsMergedIf);
1200 verifyFormat("if (a) {\n"
1201 " g()\n"
1202 "};",
1203 AllowsMergedIf);
1204 verifyFormat("if (a) g();\n"
1205 "else g();",
1206 AllowsMergedIf);
1207 verifyFormat("if (a) {\n"
1208 " g();\n"
1209 "} else g();",
1210 AllowsMergedIf);
1211 verifyFormat("if (a) g();\n"
1212 "else {\n"
1213 " g();\n"
1214 "}",
1215 AllowsMergedIf);
1216 verifyFormat("if (a) {\n"
1217 " g();\n"
1218 "} else {\n"
1219 " g();\n"
1220 "}",
1221 AllowsMergedIf);
1222 verifyFormat("if (a) g();\n"
1223 "else if (b) g();\n"
1224 "else g();",
1225 AllowsMergedIf);
1226 verifyFormat("if (a) {\n"
1227 " g();\n"
1228 "} else if (b) g();\n"
1229 "else g();",
1230 AllowsMergedIf);
1231 verifyFormat("if (a) g();\n"
1232 "else if (b) {\n"
1233 " g();\n"
1234 "} else g();",
1235 AllowsMergedIf);
1236 verifyFormat("if (a) g();\n"
1237 "else if (b) g();\n"
1238 "else {\n"
1239 " g();\n"
1240 "}",
1241 AllowsMergedIf);
1242 verifyFormat("if (a) g();\n"
1243 "else if (b) {\n"
1244 " g();\n"
1245 "} else {\n"
1246 " g();\n"
1247 "}",
1248 AllowsMergedIf);
1249 verifyFormat("if (a) {\n"
1250 " g();\n"
1251 "} else if (b) {\n"
1252 " g();\n"
1253 "} else {\n"
1254 " g();\n"
1255 "}",
1256 AllowsMergedIf);
1257 verifyFormat("MYIF (a) f();\n"
1258 "else {\n"
1259 " g();\n"
1260 "}",
1261 AllowsMergedIf);
1262 verifyFormat("MYIF (a) f();\n"
1263 "else {\n"
1264 " if (a) f();\n"
1265 " else {\n"
1266 " g();\n"
1267 " }\n"
1268 " g();\n"
1269 "}",
1270 AllowsMergedIf);
1271
1272 verifyFormat("MYIF (a) g();", AllowsMergedIf);
1273 verifyFormat("MYIF (a) {\n"
1274 " g()\n"
1275 "};",
1276 AllowsMergedIf);
1277 verifyFormat("MYIF (a) g();\n"
1278 "else g();",
1279 AllowsMergedIf);
1280 verifyFormat("MYIF (a) {\n"
1281 " g();\n"
1282 "} else g();",
1283 AllowsMergedIf);
1284 verifyFormat("MYIF (a) g();\n"
1285 "else {\n"
1286 " g();\n"
1287 "}",
1288 AllowsMergedIf);
1289 verifyFormat("MYIF (a) {\n"
1290 " g();\n"
1291 "} else {\n"
1292 " g();\n"
1293 "}",
1294 AllowsMergedIf);
1295 verifyFormat("MYIF (a) g();\n"
1296 "else MYIF (b) g();\n"
1297 "else g();",
1298 AllowsMergedIf);
1299 verifyFormat("MYIF (a) g();\n"
1300 "else if (b) g();\n"
1301 "else g();",
1302 AllowsMergedIf);
1303 verifyFormat("MYIF (a) {\n"
1304 " g();\n"
1305 "} else MYIF (b) g();\n"
1306 "else g();",
1307 AllowsMergedIf);
1308 verifyFormat("MYIF (a) {\n"
1309 " g();\n"
1310 "} else if (b) g();\n"
1311 "else g();",
1312 AllowsMergedIf);
1313 verifyFormat("MYIF (a) g();\n"
1314 "else MYIF (b) {\n"
1315 " g();\n"
1316 "} else g();",
1317 AllowsMergedIf);
1318 verifyFormat("MYIF (a) g();\n"
1319 "else if (b) {\n"
1320 " g();\n"
1321 "} else g();",
1322 AllowsMergedIf);
1323 verifyFormat("MYIF (a) g();\n"
1324 "else MYIF (b) g();\n"
1325 "else {\n"
1326 " g();\n"
1327 "}",
1328 AllowsMergedIf);
1329 verifyFormat("MYIF (a) g();\n"
1330 "else if (b) g();\n"
1331 "else {\n"
1332 " g();\n"
1333 "}",
1334 AllowsMergedIf);
1335 verifyFormat("MYIF (a) g();\n"
1336 "else MYIF (b) {\n"
1337 " g();\n"
1338 "} else {\n"
1339 " g();\n"
1340 "}",
1341 AllowsMergedIf);
1342 verifyFormat("MYIF (a) g();\n"
1343 "else if (b) {\n"
1344 " g();\n"
1345 "} else {\n"
1346 " g();\n"
1347 "}",
1348 AllowsMergedIf);
1349 verifyFormat("MYIF (a) {\n"
1350 " g();\n"
1351 "} else MYIF (b) {\n"
1352 " g();\n"
1353 "} else {\n"
1354 " g();\n"
1355 "}",
1356 AllowsMergedIf);
1357 verifyFormat("MYIF (a) {\n"
1358 " g();\n"
1359 "} else if (b) {\n"
1360 " g();\n"
1361 "} else {\n"
1362 " g();\n"
1363 "}",
1364 AllowsMergedIf);
1365}
1366
1367TEST_F(FormatTest, WrapMultipleStatementIfAndElseBraces) {
1368 auto Style = getLLVMStyle();
1369 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1370 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_AllIfsAndElse;
1371 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1372 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1373 Style.BraceWrapping.BeforeElse = true;
1374
1375 verifyFormat("if (x)\n"
1376 "{\n"
1377 " ++x;\n"
1378 " --y;\n"
1379 "}\n"
1380 "else\n"
1381 "{\n"
1382 " --x;\n"
1383 " ++y;\n"
1384 "}",
1385 Style);
1386}
1387
1388TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1389 verifyFormat("while (true)\n"
1390 " ;");
1391 verifyFormat("for (;;)\n"
1392 " ;");
1393
1394 FormatStyle AllowsMergedLoops = getLLVMStyle();
1395 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1396
1397 verifyFormat("while (true) continue;", AllowsMergedLoops);
1398 verifyFormat("for (;;) continue;", AllowsMergedLoops);
1399 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
1400 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1401 verifyFormat("while (true);", AllowsMergedLoops);
1402 verifyFormat("for (;;);", AllowsMergedLoops);
1403 verifyFormat("for (;;)\n"
1404 " for (;;) continue;",
1405 AllowsMergedLoops);
1406 verifyFormat("for (;;)\n"
1407 " while (true) continue;",
1408 AllowsMergedLoops);
1409 verifyFormat("while (true)\n"
1410 " for (;;) continue;",
1411 AllowsMergedLoops);
1412 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1413 " for (;;) continue;",
1414 AllowsMergedLoops);
1415 verifyFormat("for (;;)\n"
1416 " BOOST_FOREACH (int &v, vec) continue;",
1417 AllowsMergedLoops);
1418 verifyFormat("for (;;) // Can't merge this\n"
1419 " continue;",
1420 AllowsMergedLoops);
1421 verifyFormat("for (;;) /* still don't merge */\n"
1422 " continue;",
1423 AllowsMergedLoops);
1424 verifyFormat("do a++;\n"
1425 "while (true);",
1426 AllowsMergedLoops);
1427 verifyFormat("do /* Don't merge */\n"
1428 " a++;\n"
1429 "while (true);",
1430 AllowsMergedLoops);
1431 verifyFormat("do // Don't merge\n"
1432 " a++;\n"
1433 "while (true);",
1434 AllowsMergedLoops);
1435 verifyFormat("do\n"
1436 " // Don't merge\n"
1437 " a++;\n"
1438 "while (true);",
1439 AllowsMergedLoops);
1440
1441 // Without braces labels are interpreted differently.
1442 verifyFormat("{\n"
1443 " do\n"
1444 " label:\n"
1445 " a++;\n"
1446 " while (true);\n"
1447 "}",
1448 AllowsMergedLoops);
1449
1450 // Don't merge if there are comments before the null statement.
1451 verifyFormat("while (1) //\n"
1452 " ;",
1453 AllowsMergedLoops);
1454 verifyFormat("for (;;) /**/\n"
1455 " ;",
1456 AllowsMergedLoops);
1457 verifyFormat("while (true) /**/\n"
1458 " ;",
1459 "while (true) /**/;", AllowsMergedLoops);
1460}
1461
1462TEST_F(FormatTest, FormatShortBracedStatements) {
1463 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
1464 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
1465 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
1466 FormatStyle::SIS_Never);
1467 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
1468 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
1469 verifyFormat("for (;;) {\n"
1470 " f();\n"
1471 "}");
1472 verifyFormat("/*comment*/ for (;;) {\n"
1473 " f();\n"
1474 "}");
1475 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1476 " f();\n"
1477 "}");
1478 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1479 " f();\n"
1480 "}");
1481 verifyFormat("while (true) {\n"
1482 " f();\n"
1483 "}");
1484 verifyFormat("/*comment*/ while (true) {\n"
1485 " f();\n"
1486 "}");
1487 verifyFormat("if (true) {\n"
1488 " f();\n"
1489 "}");
1490 verifyFormat("/*comment*/ if (true) {\n"
1491 " f();\n"
1492 "}");
1493
1494 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1495 FormatStyle::SBS_Empty;
1496 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1497 FormatStyle::SIS_WithoutElse;
1498 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1499 verifyFormat("if (i) break;", AllowSimpleBracedStatements);
1500 verifyFormat("if (i > 0) {\n"
1501 " return i;\n"
1502 "}",
1503 AllowSimpleBracedStatements);
1504
1505 AllowSimpleBracedStatements.IfMacros.push_back(x: "MYIF");
1506 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1507 // Not IF to avoid any confusion that IF is somehow special.
1508 AllowSimpleBracedStatements.IfMacros.push_back(x: "FI");
1509 AllowSimpleBracedStatements.ColumnLimit = 40;
1510 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
1511 FormatStyle::SBS_Always;
1512 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1513 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
1514 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
1515 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
1516
1517 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1518 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1519 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1520 verifyFormat("if consteval {}", AllowSimpleBracedStatements);
1521 verifyFormat("if !consteval {}", AllowSimpleBracedStatements);
1522 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements);
1523 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1524 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1525 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1526 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1527 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1528 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1529 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1530 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1531 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements);
1532 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1533 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1534 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1535 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1536 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements);
1537 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements);
1538 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1539 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1540 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1541 AllowSimpleBracedStatements);
1542 verifyFormat("if (true) {\n"
1543 " ffffffffffffffffffffffff();\n"
1544 "}",
1545 AllowSimpleBracedStatements);
1546 verifyFormat("if (true) {\n"
1547 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1548 "}",
1549 AllowSimpleBracedStatements);
1550 verifyFormat("if (true) { //\n"
1551 " f();\n"
1552 "}",
1553 AllowSimpleBracedStatements);
1554 verifyFormat("if (true) {\n"
1555 " f();\n"
1556 " f();\n"
1557 "}",
1558 AllowSimpleBracedStatements);
1559 verifyFormat("if (true) {\n"
1560 " f();\n"
1561 "} else {\n"
1562 " f();\n"
1563 "}",
1564 AllowSimpleBracedStatements);
1565 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1566 AllowSimpleBracedStatements);
1567 verifyFormat("MYIF (true) {\n"
1568 " ffffffffffffffffffffffff();\n"
1569 "}",
1570 AllowSimpleBracedStatements);
1571 verifyFormat("MYIF (true) {\n"
1572 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1573 "}",
1574 AllowSimpleBracedStatements);
1575 verifyFormat("MYIF (true) { //\n"
1576 " f();\n"
1577 "}",
1578 AllowSimpleBracedStatements);
1579 verifyFormat("MYIF (true) {\n"
1580 " f();\n"
1581 " f();\n"
1582 "}",
1583 AllowSimpleBracedStatements);
1584 verifyFormat("MYIF (true) {\n"
1585 " f();\n"
1586 "} else {\n"
1587 " f();\n"
1588 "}",
1589 AllowSimpleBracedStatements);
1590
1591 verifyFormat("struct A2 {\n"
1592 " int X;\n"
1593 "};",
1594 AllowSimpleBracedStatements);
1595 verifyFormat("typedef struct A2 {\n"
1596 " int X;\n"
1597 "} A2_t;",
1598 AllowSimpleBracedStatements);
1599 verifyFormat("template <int> struct A2 {\n"
1600 " struct B {};\n"
1601 "};",
1602 AllowSimpleBracedStatements);
1603
1604 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1605 FormatStyle::SIS_Never;
1606 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1607 verifyFormat("if (true) {\n"
1608 " f();\n"
1609 "}",
1610 AllowSimpleBracedStatements);
1611 verifyFormat("if (true) {\n"
1612 " f();\n"
1613 "} else {\n"
1614 " f();\n"
1615 "}",
1616 AllowSimpleBracedStatements);
1617 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1618 verifyFormat("MYIF (true) {\n"
1619 " f();\n"
1620 "}",
1621 AllowSimpleBracedStatements);
1622 verifyFormat("MYIF (true) {\n"
1623 " f();\n"
1624 "} else {\n"
1625 " f();\n"
1626 "}",
1627 AllowSimpleBracedStatements);
1628
1629 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1630 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1631 verifyFormat("while (true) {\n"
1632 " f();\n"
1633 "}",
1634 AllowSimpleBracedStatements);
1635 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1636 verifyFormat("for (;;) {\n"
1637 " f();\n"
1638 "}",
1639 AllowSimpleBracedStatements);
1640 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1641 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1642 " f();\n"
1643 "}",
1644 AllowSimpleBracedStatements);
1645
1646 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1647 FormatStyle::SIS_WithoutElse;
1648 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
1649 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement =
1650 FormatStyle::BWACS_Always;
1651
1652 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1653 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
1654 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1655 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1656 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements);
1657 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements);
1658 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1659 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1660 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
1661 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
1662 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1663 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements);
1664 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements);
1665 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements);
1666 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
1667 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
1668 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1669 AllowSimpleBracedStatements);
1670 verifyFormat("if (true)\n"
1671 "{\n"
1672 " ffffffffffffffffffffffff();\n"
1673 "}",
1674 AllowSimpleBracedStatements);
1675 verifyFormat("if (true)\n"
1676 "{\n"
1677 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1678 "}",
1679 AllowSimpleBracedStatements);
1680 verifyFormat("if (true)\n"
1681 "{ //\n"
1682 " f();\n"
1683 "}",
1684 AllowSimpleBracedStatements);
1685 verifyFormat("if (true)\n"
1686 "{\n"
1687 " f();\n"
1688 " f();\n"
1689 "}",
1690 AllowSimpleBracedStatements);
1691 verifyFormat("if (true)\n"
1692 "{\n"
1693 " f();\n"
1694 "} else\n"
1695 "{\n"
1696 " f();\n"
1697 "}",
1698 AllowSimpleBracedStatements);
1699 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1700 AllowSimpleBracedStatements);
1701 verifyFormat("MYIF (true)\n"
1702 "{\n"
1703 " ffffffffffffffffffffffff();\n"
1704 "}",
1705 AllowSimpleBracedStatements);
1706 verifyFormat("MYIF (true)\n"
1707 "{\n"
1708 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1709 "}",
1710 AllowSimpleBracedStatements);
1711 verifyFormat("MYIF (true)\n"
1712 "{ //\n"
1713 " f();\n"
1714 "}",
1715 AllowSimpleBracedStatements);
1716 verifyFormat("MYIF (true)\n"
1717 "{\n"
1718 " f();\n"
1719 " f();\n"
1720 "}",
1721 AllowSimpleBracedStatements);
1722 verifyFormat("MYIF (true)\n"
1723 "{\n"
1724 " f();\n"
1725 "} else\n"
1726 "{\n"
1727 " f();\n"
1728 "}",
1729 AllowSimpleBracedStatements);
1730
1731 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
1732 FormatStyle::SIS_Never;
1733 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
1734 verifyFormat("if (true)\n"
1735 "{\n"
1736 " f();\n"
1737 "}",
1738 AllowSimpleBracedStatements);
1739 verifyFormat("if (true)\n"
1740 "{\n"
1741 " f();\n"
1742 "} else\n"
1743 "{\n"
1744 " f();\n"
1745 "}",
1746 AllowSimpleBracedStatements);
1747 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements);
1748 verifyFormat("MYIF (true)\n"
1749 "{\n"
1750 " f();\n"
1751 "}",
1752 AllowSimpleBracedStatements);
1753 verifyFormat("MYIF (true)\n"
1754 "{\n"
1755 " f();\n"
1756 "} else\n"
1757 "{\n"
1758 " f();\n"
1759 "}",
1760 AllowSimpleBracedStatements);
1761
1762 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
1763 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
1764 verifyFormat("while (true)\n"
1765 "{\n"
1766 " f();\n"
1767 "}",
1768 AllowSimpleBracedStatements);
1769 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
1770 verifyFormat("for (;;)\n"
1771 "{\n"
1772 " f();\n"
1773 "}",
1774 AllowSimpleBracedStatements);
1775 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements);
1776 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1777 "{\n"
1778 " f();\n"
1779 "}",
1780 AllowSimpleBracedStatements);
1781
1782 FormatStyle Style = getLLVMStyle();
1783 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1784 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
1785
1786 verifyFormat("while (i > 0)\n"
1787 "{\n"
1788 " --i;\n"
1789 "}",
1790 Style);
1791
1792 verifyFormat("if (a)\n"
1793 "{\n"
1794 " ++b;\n"
1795 "}",
1796 Style);
1797
1798 verifyFormat("if (a)\n"
1799 "{\n"
1800 " b = 1;\n"
1801 "} else\n"
1802 "{\n"
1803 " b = 0;\n"
1804 "}",
1805 Style);
1806
1807 verifyFormat("if (a)\n"
1808 "{\n"
1809 " b = 1;\n"
1810 "} else if (c)\n"
1811 "{\n"
1812 " b = 2;\n"
1813 "} else\n"
1814 "{\n"
1815 " b = 0;\n"
1816 "}",
1817 Style);
1818
1819 Style.BraceWrapping.BeforeElse = true;
1820
1821 verifyFormat("if (a)\n"
1822 "{\n"
1823 " b = 1;\n"
1824 "}\n"
1825 "else\n"
1826 "{\n"
1827 " b = 0;\n"
1828 "}",
1829 Style);
1830
1831 verifyFormat("if (a)\n"
1832 "{\n"
1833 " b = 1;\n"
1834 "}\n"
1835 "else if (c)\n"
1836 "{\n"
1837 " b = 2;\n"
1838 "}\n"
1839 "else\n"
1840 "{\n"
1841 " b = 0;\n"
1842 "}",
1843 Style);
1844}
1845
1846TEST_F(FormatTest, UnderstandsMacros) {
1847 verifyFormat("#define A (parentheses)");
1848 verifyFormat("/* comment */ #define A (parentheses)");
1849 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1850 // Even the partial code should never be merged.
1851 verifyNoChange("/* comment */ #define A (parentheses)\n"
1852 "#");
1853 verifyFormat("/* comment */ #define A (parentheses)\n"
1854 "#\n");
1855 verifyFormat("/* comment */ #define A (parentheses)\n"
1856 "#define B (parentheses)");
1857 verifyFormat("#define true ((int)1)");
1858 verifyFormat("#define and(x)");
1859 verifyFormat("#define if(x) x");
1860 verifyFormat("#define return(x) (x)");
1861 verifyFormat("#define while(x) for (; x;)");
1862 verifyFormat("#define xor(x) (^(x))");
1863 verifyFormat("#define __except(x)");
1864 verifyFormat("#define __try(x)");
1865
1866 // https://llvm.org/PR54348.
1867 verifyFormat(
1868 "#define A"
1869 " "
1870 "\\\n"
1871 " class & {}");
1872
1873 FormatStyle Style = getLLVMStyle();
1874 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1875 Style.BraceWrapping.AfterFunction = true;
1876 // Test that a macro definition never gets merged with the following
1877 // definition.
1878 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1879 verifyFormat("#define AAA "
1880 " \\\n"
1881 " N "
1882 " \\\n"
1883 " {\n"
1884 "#define BBB }",
1885 Style);
1886 // verifyFormat("#define AAA N { //", Style);
1887
1888 verifyFormat("MACRO(return)");
1889 verifyFormat("MACRO(co_await)");
1890 verifyFormat("MACRO(co_return)");
1891 verifyFormat("MACRO(co_yield)");
1892 verifyFormat("MACRO(return, something)");
1893 verifyFormat("MACRO(co_return, something)");
1894 verifyFormat("MACRO(something##something)");
1895 verifyFormat("MACRO(return##something)");
1896 verifyFormat("MACRO(co_return##something)");
1897
1898 verifyFormat("#define A x:");
1899
1900 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1901 " { \\\n"
1902 " #Bar \\\n"
1903 " }");
1904 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1905 " { #Bar }");
1906}
1907
1908TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
1909 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
1910 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
1911 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
1912 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1913 verifyFormat("#define A \\\n"
1914 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1915 " { \\\n"
1916 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1917 " }\n"
1918 "X;",
1919 "#define A \\\n"
1920 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1921 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1922 " }\n"
1923 "X;",
1924 Style);
1925}
1926
1927TEST_F(FormatTest, ParseIfElse) {
1928 verifyFormat("if (true)\n"
1929 " if (true)\n"
1930 " if (true)\n"
1931 " f();\n"
1932 " else\n"
1933 " g();\n"
1934 " else\n"
1935 " h();\n"
1936 "else\n"
1937 " i();");
1938 verifyFormat("if (true)\n"
1939 " if (true)\n"
1940 " if (true) {\n"
1941 " if (true)\n"
1942 " f();\n"
1943 " } else {\n"
1944 " g();\n"
1945 " }\n"
1946 " else\n"
1947 " h();\n"
1948 "else {\n"
1949 " i();\n"
1950 "}");
1951 verifyFormat("if (true)\n"
1952 " if constexpr (true)\n"
1953 " if (true) {\n"
1954 " if constexpr (true)\n"
1955 " f();\n"
1956 " } else {\n"
1957 " g();\n"
1958 " }\n"
1959 " else\n"
1960 " h();\n"
1961 "else {\n"
1962 " i();\n"
1963 "}");
1964 verifyFormat("if (true)\n"
1965 " if CONSTEXPR (true)\n"
1966 " if (true) {\n"
1967 " if CONSTEXPR (true)\n"
1968 " f();\n"
1969 " } else {\n"
1970 " g();\n"
1971 " }\n"
1972 " else\n"
1973 " h();\n"
1974 "else {\n"
1975 " i();\n"
1976 "}");
1977 verifyFormat("void f() {\n"
1978 " if (a) {\n"
1979 " } else {\n"
1980 " }\n"
1981 "}");
1982}
1983
1984TEST_F(FormatTest, ElseIf) {
1985 verifyFormat("if (a) {\n} else if (b) {\n}");
1986 verifyFormat("if (a)\n"
1987 " f();\n"
1988 "else if (b)\n"
1989 " g();\n"
1990 "else\n"
1991 " h();");
1992 verifyFormat("if (a)\n"
1993 " f();\n"
1994 "else // comment\n"
1995 " if (b) {\n"
1996 " g();\n"
1997 " h();\n"
1998 " }");
1999 verifyFormat("if constexpr (a)\n"
2000 " f();\n"
2001 "else if constexpr (b)\n"
2002 " g();\n"
2003 "else\n"
2004 " h();");
2005 verifyFormat("if CONSTEXPR (a)\n"
2006 " f();\n"
2007 "else if CONSTEXPR (b)\n"
2008 " g();\n"
2009 "else\n"
2010 " h();");
2011 verifyFormat("if (a) {\n"
2012 " f();\n"
2013 "}\n"
2014 "// or else ..\n"
2015 "else {\n"
2016 " g()\n"
2017 "}");
2018
2019 verifyFormat("if (a) {\n"
2020 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2022 "}");
2023 verifyFormat("if (a) {\n"
2024 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2026 "}");
2027 verifyFormat("if (a) {\n"
2028 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2030 "}");
2031 verifyFormat("if (a) {\n"
2032 "} else if (\n"
2033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2034 "}",
2035 getLLVMStyleWithColumns(62));
2036 verifyFormat("if (a) {\n"
2037 "} else if constexpr (\n"
2038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2039 "}",
2040 getLLVMStyleWithColumns(62));
2041 verifyFormat("if (a) {\n"
2042 "} else if CONSTEXPR (\n"
2043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2044 "}",
2045 getLLVMStyleWithColumns(62));
2046}
2047
2048TEST_F(FormatTest, SeparatePointerReferenceAlignment) {
2049 FormatStyle Style = getLLVMStyle();
2050 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
2051 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer);
2052 verifyFormat("int *f1(int *a, int &b, int &&c);", Style);
2053 verifyFormat("int &f2(int &&c, int *a, int &b);", Style);
2054 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style);
2055 verifyFormat("int *f1(int &a) const &;", Style);
2056 verifyFormat("int *f1(int &a) const & = 0;", Style);
2057 verifyFormat("int *a = f1();", Style);
2058 verifyFormat("int &b = f2();", Style);
2059 verifyFormat("int &&c = f3();", Style);
2060 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2061 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2062 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2063 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2064 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2065 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2066 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2067 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2068 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style);
2069 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style);
2070 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style);
2071 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style);
2072 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style);
2073 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style);
2074 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style);
2075 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style);
2076 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style);
2077 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style);
2078 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style);
2079 verifyFormat("for (f(); int &c : {1, 2, 3})", Style);
2080 verifyFormat(
2081 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2082 " res2 = [](int &a) { return 0000000000000; };",
2083 Style);
2084
2085 Style.AlignConsecutiveDeclarations.Enabled = true;
2086 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2087 verifyFormat("Const unsigned int *c;\n"
2088 "const unsigned int *d;\n"
2089 "Const unsigned int &e;\n"
2090 "const unsigned int &f;\n"
2091 "int *f1(int *a, int &b, int &&c);\n"
2092 "double *(*f2)(int *a, double &&b);\n"
2093 "const unsigned &&g;\n"
2094 "Const unsigned h;",
2095 Style);
2096 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2097 verifyFormat("Const unsigned int *c;\n"
2098 "const unsigned int *d;\n"
2099 "Const unsigned int &e;\n"
2100 "const unsigned int &f;\n"
2101 "int *f1(int *a, int &b, int &&c);\n"
2102 "double *(*f2)(int *a, double &&b);\n"
2103 "const unsigned &&g;\n"
2104 "Const unsigned h;",
2105 Style);
2106
2107 Style.PointerAlignment = FormatStyle::PAS_Left;
2108 verifyFormat("int* f1(int* a, int& b, int&& c);", Style);
2109 verifyFormat("int& f2(int&& c, int* a, int& b);", Style);
2110 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style);
2111 verifyFormat("int* f1(int& a) const& = 0;", Style);
2112 verifyFormat("int* a = f1();", Style);
2113 verifyFormat("int& b = f2();", Style);
2114 verifyFormat("int&& c = f3();", Style);
2115 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2116 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2117 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2118 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2119 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2120 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2121 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2122 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2123 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style);
2124 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style);
2125 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style);
2126 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2127 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style);
2128 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style);
2129 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style);
2130 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2131 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style);
2132 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style);
2133 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style);
2134 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style);
2135 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style);
2136 verifyFormat("for (f(); int& c : {1, 2, 3})", Style);
2137 verifyFormat(
2138 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2139 " res2 = [](int& a) { return 0000000000000; };",
2140 Style);
2141 verifyFormat("[](decltype(foo)& Bar) {}", Style);
2142
2143 Style.AlignConsecutiveDeclarations.Enabled = true;
2144 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2145 verifyFormat("Const unsigned int* c;\n"
2146 "const unsigned int* d;\n"
2147 "Const unsigned int& e;\n"
2148 "const unsigned int& f;\n"
2149 "int* f1(int* a, int& b, int&& c);\n"
2150 "double* (*f2)(int* a, double&& b);\n"
2151 "const unsigned&& g;\n"
2152 "Const unsigned h;",
2153 Style);
2154 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2155 verifyFormat("Const unsigned int* c;\n"
2156 "const unsigned int* d;\n"
2157 "Const unsigned int& e;\n"
2158 "const unsigned int& f;\n"
2159 "int* f1(int* a, int& b, int&& c);\n"
2160 "double* (*f2)(int* a, double&& b);\n"
2161 "const unsigned&& g;\n"
2162 "Const unsigned h;",
2163 Style);
2164
2165 Style.PointerAlignment = FormatStyle::PAS_Right;
2166 Style.ReferenceAlignment = FormatStyle::RAS_Left;
2167 verifyFormat("int *f1(int *a, int& b, int&& c);", Style);
2168 verifyFormat("int& f2(int&& c, int *a, int& b);", Style);
2169 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style);
2170 verifyFormat("int *a = f1();", Style);
2171 verifyFormat("int& b = f2();", Style);
2172 verifyFormat("int&& c = f3();", Style);
2173 verifyFormat("int f3() { return sizeof(Foo&); }", Style);
2174 verifyFormat("int f4() { return sizeof(Foo&&); }", Style);
2175 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style);
2176 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style);
2177 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2178 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style);
2179 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style);
2180
2181 Style.AlignConsecutiveDeclarations.Enabled = true;
2182 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2183 verifyFormat("Const unsigned int *c;\n"
2184 "const unsigned int *d;\n"
2185 "Const unsigned int& e;\n"
2186 "const unsigned int& f;\n"
2187 "int *f1(int *a, int& b, int&& c);\n"
2188 "double *(*f2)(int *a, double&& b);\n"
2189 "const unsigned&& g;\n"
2190 "Const unsigned h;",
2191 Style);
2192 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2193 verifyFormat("Const unsigned int *c;\n"
2194 "const unsigned int *d;\n"
2195 "Const unsigned int& e;\n"
2196 "const unsigned int& f;\n"
2197 "int *f1(int *a, int& b, int&& c);\n"
2198 "double *(*f2)(int *a, double&& b);\n"
2199 "const unsigned&& g;\n"
2200 "Const unsigned h;",
2201 Style);
2202
2203 Style.PointerAlignment = FormatStyle::PAS_Left;
2204 Style.ReferenceAlignment = FormatStyle::RAS_Middle;
2205 verifyFormat("int* f1(int* a, int & b, int && c);", Style);
2206 verifyFormat("int & f2(int && c, int* a, int & b);", Style);
2207 verifyFormat("int && f3(int & b, int && c, int* a);", Style);
2208 verifyFormat("int* a = f1();", Style);
2209 verifyFormat("int & b = f2();", Style);
2210 verifyFormat("int && c = f3();", Style);
2211 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2212 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2213 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2214 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2215 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style);
2216 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style);
2217 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style);
2218 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style);
2219 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style);
2220 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style);
2221 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style);
2222 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style);
2223 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style);
2224 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style);
2225 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style);
2226 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style);
2227 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style);
2228 verifyFormat("for (f(); int & c : {1, 2, 3})", Style);
2229 verifyFormat(
2230 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2231 " res2 = [](int & a) { return 0000000000000; };",
2232 Style);
2233
2234 Style.AlignConsecutiveDeclarations.Enabled = true;
2235 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2236 verifyFormat("Const unsigned int* c;\n"
2237 "const unsigned int* d;\n"
2238 "Const unsigned int & e;\n"
2239 "const unsigned int & f;\n"
2240 "int* f1(int* a, int & b, int && c);\n"
2241 "double* (*f2)(int* a, double && b);\n"
2242 "const unsigned && g;\n"
2243 "Const unsigned h;",
2244 Style);
2245 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2246 verifyFormat("Const unsigned int* c;\n"
2247 "const unsigned int* d;\n"
2248 "Const unsigned int & e;\n"
2249 "const unsigned int & f;\n"
2250 "int* f1(int* a, int & b, int && c);\n"
2251 "double* (*f2)(int* a, double && b);\n"
2252 "const unsigned && g;\n"
2253 "Const unsigned h;",
2254 Style);
2255
2256 Style.PointerAlignment = FormatStyle::PAS_Middle;
2257 Style.ReferenceAlignment = FormatStyle::RAS_Right;
2258 verifyFormat("int * f1(int * a, int &b, int &&c);", Style);
2259 verifyFormat("int &f2(int &&c, int * a, int &b);", Style);
2260 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style);
2261 verifyFormat("int * a = f1();", Style);
2262 verifyFormat("int &b = f2();", Style);
2263 verifyFormat("int &&c = f3();", Style);
2264 verifyFormat("int f3() { return sizeof(Foo &); }", Style);
2265 verifyFormat("int f4() { return sizeof(Foo &&); }", Style);
2266 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style);
2267 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style);
2268 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2269 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style);
2270 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style);
2271
2272 Style.AlignConsecutiveDeclarations.Enabled = true;
2273 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
2274 verifyFormat("Const unsigned int * c;\n"
2275 "const unsigned int * d;\n"
2276 "Const unsigned int &e;\n"
2277 "const unsigned int &f;\n"
2278 "int * f1(int * a, int &b, int &&c);\n"
2279 "double * (*f2)(int * a, double &&b);\n"
2280 "const unsigned &&g;\n"
2281 "Const unsigned h;",
2282 Style);
2283 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false;
2284 verifyFormat("Const unsigned int * c;\n"
2285 "const unsigned int * d;\n"
2286 "Const unsigned int &e;\n"
2287 "const unsigned int &f;\n"
2288 "int * f1(int * a, int &b, int &&c);\n"
2289 "double * (*f2)(int * a, double &&b);\n"
2290 "const unsigned &&g;\n"
2291 "Const unsigned h;",
2292 Style);
2293
2294 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2295 // specifically handled
2296 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2297}
2298
2299TEST_F(FormatTest, FormatsForLoop) {
2300 verifyFormat(
2301 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2302 " ++VeryVeryLongLoopVariable)\n"
2303 " ;");
2304 verifyFormat("for (;;)\n"
2305 " f();");
2306 verifyFormat("for (;;) {\n}");
2307 verifyFormat("for (;;) {\n"
2308 " f();\n"
2309 "}");
2310 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2311
2312 verifyFormat(
2313 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2314 " E = UnwrappedLines.end();\n"
2315 " I != E; ++I) {\n}");
2316
2317 verifyFormat(
2318 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2319 " ++IIIII) {\n}");
2320 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2321 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2322 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2323 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2324 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2325 " E = FD->getDeclsInPrototypeScope().end();\n"
2326 " I != E; ++I) {\n}");
2327 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2328 " I = Container.begin(),\n"
2329 " E = Container.end();\n"
2330 " I != E; ++I) {\n}",
2331 getLLVMStyleWithColumns(76));
2332
2333 verifyFormat(
2334 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2338 " ++aaaaaaaaaaa) {\n}");
2339 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2340 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2341 " ++i) {\n}");
2342 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2343 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2344 "}");
2345 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2346 " aaaaaaaaaa);\n"
2347 " iter; ++iter) {\n"
2348 "}");
2349 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2351 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2352 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2353
2354 // These should not be formatted as Objective-C for-in loops.
2355 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2356 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2357 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2358 verifyFormat(
2359 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2360
2361 FormatStyle NoBinPacking = getLLVMStyle();
2362 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
2363 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2364 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2365 " aaaaaaaaaaaaaaaa,\n"
2366 " aaaaaaaaaaaaaaaa,\n"
2367 " aaaaaaaaaaaaaaaa);\n"
2368 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2369 "}",
2370 NoBinPacking);
2371 verifyFormat(
2372 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2373 " E = UnwrappedLines.end();\n"
2374 " I != E;\n"
2375 " ++I) {\n}",
2376 NoBinPacking);
2377
2378 FormatStyle AlignLeft = getLLVMStyle();
2379 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
2380 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
2381}
2382
2383TEST_F(FormatTest, RangeBasedForLoops) {
2384 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2386 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2387 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2388 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2390 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2391 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2392}
2393
2394TEST_F(FormatTest, ForEachLoops) {
2395 FormatStyle Style = getLLVMStyle();
2396 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2397 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false);
2398 verifyFormat("void f() {\n"
2399 " for (;;) {\n"
2400 " }\n"
2401 " foreach (Item *item, itemlist) {\n"
2402 " }\n"
2403 " Q_FOREACH (Item *item, itemlist) {\n"
2404 " }\n"
2405 " BOOST_FOREACH (Item *item, itemlist) {\n"
2406 " }\n"
2407 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2408 "}",
2409 Style);
2410 verifyFormat("void f() {\n"
2411 " for (;;)\n"
2412 " int j = 1;\n"
2413 " Q_FOREACH (int v, vec)\n"
2414 " v *= 2;\n"
2415 " for (;;) {\n"
2416 " int j = 1;\n"
2417 " }\n"
2418 " Q_FOREACH (int v, vec) {\n"
2419 " v *= 2;\n"
2420 " }\n"
2421 "}",
2422 Style);
2423
2424 FormatStyle ShortBlocks = getLLVMStyle();
2425 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2426 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false);
2427 verifyFormat("void f() {\n"
2428 " for (;;)\n"
2429 " int j = 1;\n"
2430 " Q_FOREACH (int &v, vec)\n"
2431 " v *= 2;\n"
2432 " for (;;) {\n"
2433 " int j = 1;\n"
2434 " }\n"
2435 " Q_FOREACH (int &v, vec) {\n"
2436 " int j = 1;\n"
2437 " }\n"
2438 "}",
2439 ShortBlocks);
2440
2441 FormatStyle ShortLoops = getLLVMStyle();
2442 ShortLoops.AllowShortLoopsOnASingleLine = true;
2443 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
2444 verifyFormat("void f() {\n"
2445 " for (;;) int j = 1;\n"
2446 " Q_FOREACH (int &v, vec) int j = 1;\n"
2447 " for (;;) {\n"
2448 " int j = 1;\n"
2449 " }\n"
2450 " Q_FOREACH (int &v, vec) {\n"
2451 " int j = 1;\n"
2452 " }\n"
2453 "}",
2454 ShortLoops);
2455
2456 FormatStyle ShortBlocksAndLoops = getLLVMStyle();
2457 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
2458 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true;
2459 verifyFormat("void f() {\n"
2460 " for (;;) int j = 1;\n"
2461 " Q_FOREACH (int &v, vec) int j = 1;\n"
2462 " for (;;) { int j = 1; }\n"
2463 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2464 "}",
2465 ShortBlocksAndLoops);
2466
2467 Style.SpaceBeforeParens =
2468 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
2469 verifyFormat("void f() {\n"
2470 " for (;;) {\n"
2471 " }\n"
2472 " foreach(Item *item, itemlist) {\n"
2473 " }\n"
2474 " Q_FOREACH(Item *item, itemlist) {\n"
2475 " }\n"
2476 " BOOST_FOREACH(Item *item, itemlist) {\n"
2477 " }\n"
2478 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2479 "}",
2480 Style);
2481
2482 // As function-like macros.
2483 verifyFormat("#define foreach(x, y)\n"
2484 "#define Q_FOREACH(x, y)\n"
2485 "#define BOOST_FOREACH(x, y)\n"
2486 "#define UNKNOWN_FOREACH(x, y)");
2487
2488 // Not as function-like macros.
2489 verifyFormat("#define foreach (x, y)\n"
2490 "#define Q_FOREACH (x, y)\n"
2491 "#define BOOST_FOREACH (x, y)\n"
2492 "#define UNKNOWN_FOREACH (x, y)");
2493
2494 // handle microsoft non standard extension
2495 verifyFormat("for each (char c in x->MyStringProperty)");
2496}
2497
2498TEST_F(FormatTest, FormatsWhileLoop) {
2499 verifyFormat("while (true) {\n}");
2500 verifyFormat("while (true)\n"
2501 " f();");
2502 verifyFormat("while () {\n}");
2503 verifyFormat("while () {\n"
2504 " f();\n"
2505 "}");
2506}
2507
2508TEST_F(FormatTest, FormatsDoWhile) {
2509 verifyFormat("do {\n"
2510 " do_something();\n"
2511 "} while (something());");
2512 verifyFormat("do\n"
2513 " do_something();\n"
2514 "while (something());");
2515}
2516
2517TEST_F(FormatTest, FormatsSwitchStatement) {
2518 verifyFormat("switch (x) {\n"
2519 "case 1:\n"
2520 " f();\n"
2521 " break;\n"
2522 "case kFoo:\n"
2523 "case ns::kBar:\n"
2524 "case kBaz:\n"
2525 " break;\n"
2526 "default:\n"
2527 " g();\n"
2528 " break;\n"
2529 "}");
2530 verifyFormat("switch (x) {\n"
2531 "case 1: {\n"
2532 " f();\n"
2533 " break;\n"
2534 "}\n"
2535 "case 2: {\n"
2536 " break;\n"
2537 "}\n"
2538 "}");
2539 verifyFormat("switch (x) {\n"
2540 "case 1: {\n"
2541 " f();\n"
2542 " {\n"
2543 " g();\n"
2544 " h();\n"
2545 " }\n"
2546 " break;\n"
2547 "}\n"
2548 "}");
2549 verifyFormat("switch (x) {\n"
2550 "case 1: {\n"
2551 " f();\n"
2552 " if (foo) {\n"
2553 " g();\n"
2554 " h();\n"
2555 " }\n"
2556 " break;\n"
2557 "}\n"
2558 "}");
2559 verifyFormat("switch (x) {\n"
2560 "case 1: {\n"
2561 " f();\n"
2562 " g();\n"
2563 "} break;\n"
2564 "}");
2565 verifyFormat("switch (test)\n"
2566 " ;");
2567 verifyFormat("switch (x) {\n"
2568 "default: {\n"
2569 " // Do nothing.\n"
2570 "}\n"
2571 "}");
2572 verifyFormat("switch (x) {\n"
2573 "// comment\n"
2574 "// if 1, do f()\n"
2575 "case 1:\n"
2576 " f();\n"
2577 "}");
2578 verifyFormat("switch (x) {\n"
2579 "case 1:\n"
2580 " // Do amazing stuff\n"
2581 " {\n"
2582 " f();\n"
2583 " g();\n"
2584 " }\n"
2585 " break;\n"
2586 "}");
2587 verifyFormat("#define A \\\n"
2588 " switch (x) { \\\n"
2589 " case a: \\\n"
2590 " foo = b; \\\n"
2591 " }",
2592 getLLVMStyleWithColumns(20));
2593 verifyFormat("#define OPERATION_CASE(name) \\\n"
2594 " case OP_name: \\\n"
2595 " return operations::Operation##name",
2596 getLLVMStyleWithColumns(40));
2597 verifyFormat("switch (x) {\n"
2598 "case 1:;\n"
2599 "default:;\n"
2600 " int i;\n"
2601 "}");
2602
2603 verifyGoogleFormat("switch (x) {\n"
2604 " case 1:\n"
2605 " f();\n"
2606 " break;\n"
2607 " case kFoo:\n"
2608 " case ns::kBar:\n"
2609 " case kBaz:\n"
2610 " break;\n"
2611 " default:\n"
2612 " g();\n"
2613 " break;\n"
2614 "}");
2615 verifyGoogleFormat("switch (x) {\n"
2616 " case 1: {\n"
2617 " f();\n"
2618 " break;\n"
2619 " }\n"
2620 "}");
2621 verifyGoogleFormat("switch (test)\n"
2622 " ;");
2623
2624 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2625 " case OP_name: \\\n"
2626 " return operations::Operation##name");
2627 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2628 " // Get the correction operation class.\n"
2629 " switch (OpCode) {\n"
2630 " CASE(Add);\n"
2631 " CASE(Subtract);\n"
2632 " default:\n"
2633 " return operations::Unknown;\n"
2634 " }\n"
2635 "#undef OPERATION_CASE\n"
2636 "}");
2637 verifyFormat("DEBUG({\n"
2638 " switch (x) {\n"
2639 " case A:\n"
2640 " f();\n"
2641 " break;\n"
2642 " // fallthrough\n"
2643 " case B:\n"
2644 " g();\n"
2645 " break;\n"
2646 " }\n"
2647 "});");
2648 verifyNoChange("DEBUG({\n"
2649 " switch (x) {\n"
2650 " case A:\n"
2651 " f();\n"
2652 " break;\n"
2653 " // On B:\n"
2654 " case B:\n"
2655 " g();\n"
2656 " break;\n"
2657 " }\n"
2658 "});");
2659 verifyFormat("switch (n) {\n"
2660 "case 0: {\n"
2661 " return false;\n"
2662 "}\n"
2663 "default: {\n"
2664 " return true;\n"
2665 "}\n"
2666 "}",
2667 "switch (n)\n"
2668 "{\n"
2669 "case 0: {\n"
2670 " return false;\n"
2671 "}\n"
2672 "default: {\n"
2673 " return true;\n"
2674 "}\n"
2675 "}");
2676 verifyFormat("switch (a) {\n"
2677 "case (b):\n"
2678 " return;\n"
2679 "}");
2680
2681 verifyFormat("switch (a) {\n"
2682 "case some_namespace::\n"
2683 " some_constant:\n"
2684 " return;\n"
2685 "}",
2686 getLLVMStyleWithColumns(34));
2687
2688 verifyFormat("switch (a) {\n"
2689 "[[likely]] case 1:\n"
2690 " return;\n"
2691 "}");
2692 verifyFormat("switch (a) {\n"
2693 "[[likely]] [[other::likely]] case 1:\n"
2694 " return;\n"
2695 "}");
2696 verifyFormat("switch (x) {\n"
2697 "case 1:\n"
2698 " return;\n"
2699 "[[likely]] case 2:\n"
2700 " return;\n"
2701 "}");
2702 verifyFormat("switch (a) {\n"
2703 "case 1:\n"
2704 "[[likely]] case 2:\n"
2705 " return;\n"
2706 "}");
2707 FormatStyle Attributes = getLLVMStyle();
2708 Attributes.AttributeMacros.push_back(x: "LIKELY");
2709 Attributes.AttributeMacros.push_back(x: "OTHER_LIKELY");
2710 verifyFormat("switch (a) {\n"
2711 "LIKELY case b:\n"
2712 " return;\n"
2713 "}",
2714 Attributes);
2715 verifyFormat("switch (a) {\n"
2716 "LIKELY OTHER_LIKELY() case b:\n"
2717 " return;\n"
2718 "}",
2719 Attributes);
2720 verifyFormat("switch (a) {\n"
2721 "case 1:\n"
2722 " return;\n"
2723 "LIKELY case 2:\n"
2724 " return;\n"
2725 "}",
2726 Attributes);
2727 verifyFormat("switch (a) {\n"
2728 "case 1:\n"
2729 "LIKELY case 2:\n"
2730 " return;\n"
2731 "}",
2732 Attributes);
2733
2734 FormatStyle Style = getLLVMStyle();
2735 Style.IndentCaseLabels = true;
2736 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
2737 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2738 Style.BraceWrapping.AfterCaseLabel = true;
2739 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
2740 verifyFormat("switch (n)\n"
2741 "{\n"
2742 " case 0:\n"
2743 " {\n"
2744 " return false;\n"
2745 " }\n"
2746 " default:\n"
2747 " {\n"
2748 " return true;\n"
2749 " }\n"
2750 "}",
2751 "switch (n) {\n"
2752 " case 0: {\n"
2753 " return false;\n"
2754 " }\n"
2755 " default: {\n"
2756 " return true;\n"
2757 " }\n"
2758 "}",
2759 Style);
2760 Style.BraceWrapping.AfterCaseLabel = false;
2761 verifyFormat("switch (n)\n"
2762 "{\n"
2763 " case 0: {\n"
2764 " return false;\n"
2765 " }\n"
2766 " default: {\n"
2767 " return true;\n"
2768 " }\n"
2769 "}",
2770 "switch (n) {\n"
2771 " case 0:\n"
2772 " {\n"
2773 " return false;\n"
2774 " }\n"
2775 " default:\n"
2776 " {\n"
2777 " return true;\n"
2778 " }\n"
2779 "}",
2780 Style);
2781 Style.IndentCaseLabels = false;
2782 Style.IndentCaseBlocks = true;
2783 verifyFormat("switch (n)\n"
2784 "{\n"
2785 "case 0:\n"
2786 " {\n"
2787 " return false;\n"
2788 " }\n"
2789 "case 1:\n"
2790 " break;\n"
2791 "default:\n"
2792 " {\n"
2793 " return true;\n"
2794 " }\n"
2795 "}",
2796 "switch (n) {\n"
2797 "case 0: {\n"
2798 " return false;\n"
2799 "}\n"
2800 "case 1:\n"
2801 " break;\n"
2802 "default: {\n"
2803 " return true;\n"
2804 "}\n"
2805 "}",
2806 Style);
2807 Style.IndentCaseLabels = true;
2808 Style.IndentCaseBlocks = true;
2809 verifyFormat("switch (n)\n"
2810 "{\n"
2811 " case 0:\n"
2812 " {\n"
2813 " return false;\n"
2814 " }\n"
2815 " case 1:\n"
2816 " break;\n"
2817 " default:\n"
2818 " {\n"
2819 " return true;\n"
2820 " }\n"
2821 "}",
2822 "switch (n) {\n"
2823 "case 0: {\n"
2824 " return false;\n"
2825 "}\n"
2826 "case 1:\n"
2827 " break;\n"
2828 "default: {\n"
2829 " return true;\n"
2830 "}\n"
2831 "}",
2832 Style);
2833}
2834
2835TEST_F(FormatTest, CaseRanges) {
2836 verifyFormat("switch (x) {\n"
2837 "case 'A' ... 'Z':\n"
2838 "case 1 ... 5:\n"
2839 "case a ... b:\n"
2840 " break;\n"
2841 "}");
2842}
2843
2844TEST_F(FormatTest, ShortEnums) {
2845 FormatStyle Style = getLLVMStyle();
2846 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
2847 EXPECT_FALSE(Style.BraceWrapping.AfterEnum);
2848 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2849 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
2850 Style.AllowShortEnumsOnASingleLine = false;
2851 verifyFormat("enum {\n"
2852 " A,\n"
2853 " B,\n"
2854 " C\n"
2855 "} ShortEnum1, ShortEnum2;",
2856 Style);
2857 verifyFormat("typedef enum {\n"
2858 " A,\n"
2859 " B,\n"
2860 " C\n"
2861 "} ShortEnum1, ShortEnum2;",
2862 Style);
2863 verifyFormat("enum {\n"
2864 " A,\n"
2865 "} ShortEnum1, ShortEnum2;",
2866 Style);
2867 verifyFormat("typedef enum {\n"
2868 " A,\n"
2869 "} ShortEnum1, ShortEnum2;",
2870 Style);
2871 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2872 Style.BraceWrapping.AfterEnum = true;
2873 verifyFormat("enum\n"
2874 "{\n"
2875 " A,\n"
2876 " B,\n"
2877 " C\n"
2878 "} ShortEnum1, ShortEnum2;",
2879 Style);
2880 verifyFormat("typedef enum\n"
2881 "{\n"
2882 " A,\n"
2883 " B,\n"
2884 " C\n"
2885 "} ShortEnum1, ShortEnum2;",
2886 Style);
2887}
2888
2889TEST_F(FormatTest, ShortCompoundRequirement) {
2890 constexpr StringRef Code("template <typename T>\n"
2891 "concept c = requires(T x) {\n"
2892 " { x + 1 } -> std::same_as<int>;\n"
2893 "};");
2894
2895 FormatStyle Style = getLLVMStyle();
2896 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2897 verifyFormat(Code, Style);
2898 verifyFormat("template <typename T>\n"
2899 "concept c = requires(T x) {\n"
2900 " { x + 1 } -> std::same_as<int>;\n"
2901 " { x + 2 } -> std::same_as<int>;\n"
2902 "};",
2903 Style);
2904
2905 Style.AllowShortCompoundRequirementOnASingleLine = false;
2906 verifyFormat("template <typename T>\n"
2907 "concept c = requires(T x) {\n"
2908 " {\n"
2909 " x + 1\n"
2910 " } -> std::same_as<int>;\n"
2911 "};",
2912 Code, Style);
2913 verifyFormat("template <typename T>\n"
2914 "concept c = requires(T x) {\n"
2915 " {\n"
2916 " x + 1\n"
2917 " } -> std::same_as<int>;\n"
2918 " {\n"
2919 " x + 2\n"
2920 " } -> std::same_as<int>;\n"
2921 "};",
2922 Style);
2923
2924 Style.AllowShortCompoundRequirementOnASingleLine = true;
2925 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2926 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
2927 verifyFormat(Code, Style);
2928}
2929
2930TEST_F(FormatTest, ShortCaseLabels) {
2931 FormatStyle Style = getLLVMStyle();
2932 Style.AllowShortCaseLabelsOnASingleLine = true;
2933 verifyFormat("switch (a) {\n"
2934 "case 1: x = 1; break;\n"
2935 "case 2: return;\n"
2936 "case 3:\n"
2937 "case 4:\n"
2938 "case 5: return;\n"
2939 "case 6: // comment\n"
2940 " return;\n"
2941 "case 7:\n"
2942 " // comment\n"
2943 " return;\n"
2944 "case 8:\n"
2945 " x = 8; // comment\n"
2946 " break;\n"
2947 "default: y = 1; break;\n"
2948 "}",
2949 Style);
2950 verifyFormat("switch (a) {\n"
2951 "case 0: return; // comment\n"
2952 "case 1: break; // comment\n"
2953 "case 2: return;\n"
2954 "// comment\n"
2955 "case 3: return;\n"
2956 "// comment 1\n"
2957 "// comment 2\n"
2958 "// comment 3\n"
2959 "case 4: break; /* comment */\n"
2960 "case 5:\n"
2961 " // comment\n"
2962 " break;\n"
2963 "case 6: /* comment */ x = 1; break;\n"
2964 "case 7: x = /* comment */ 1; break;\n"
2965 "case 8:\n"
2966 " x = 1; /* comment */\n"
2967 " break;\n"
2968 "case 9:\n"
2969 " break; // comment line 1\n"
2970 " // comment line 2\n"
2971 "}",
2972 Style);
2973 verifyFormat("switch (a) {\n"
2974 "case 1:\n"
2975 " x = 8;\n"
2976 " // fall through\n"
2977 "case 2: x = 8;\n"
2978 "// comment\n"
2979 "case 3:\n"
2980 " return; /* comment line 1\n"
2981 " * comment line 2 */\n"
2982 "case 4: i = 8;\n"
2983 "// something else\n"
2984 "#if FOO\n"
2985 "case 5: break;\n"
2986 "#endif\n"
2987 "}",
2988 "switch (a) {\n"
2989 "case 1: x = 8;\n"
2990 " // fall through\n"
2991 "case 2:\n"
2992 " x = 8;\n"
2993 "// comment\n"
2994 "case 3:\n"
2995 " return; /* comment line 1\n"
2996 " * comment line 2 */\n"
2997 "case 4:\n"
2998 " i = 8;\n"
2999 "// something else\n"
3000 "#if FOO\n"
3001 "case 5: break;\n"
3002 "#endif\n"
3003 "}",
3004 Style);
3005 verifyFormat("switch (a) {\n"
3006 "case 0:\n"
3007 " return; // long long long long long long long long long long "
3008 "long long comment\n"
3009 " // line\n"
3010 "}",
3011 "switch (a) {\n"
3012 "case 0: return; // long long long long long long long long "
3013 "long long long long comment line\n"
3014 "}",
3015 Style);
3016 verifyFormat("switch (a) {\n"
3017 "case 0:\n"
3018 " return; /* long long long long long long long long long long "
3019 "long long comment\n"
3020 " line */\n"
3021 "}",
3022 "switch (a) {\n"
3023 "case 0: return; /* long long long long long long long long "
3024 "long long long long comment line */\n"
3025 "}",
3026 Style);
3027 verifyFormat("switch (a) {\n"
3028 "#if FOO\n"
3029 "case 0: return 0;\n"
3030 "#endif\n"
3031 "}",
3032 Style);
3033 verifyFormat("switch (a) {\n"
3034 "case 1: {\n"
3035 "}\n"
3036 "case 2: {\n"
3037 " return;\n"
3038 "}\n"
3039 "case 3: {\n"
3040 " x = 1;\n"
3041 " return;\n"
3042 "}\n"
3043 "case 4:\n"
3044 " if (x)\n"
3045 " return;\n"
3046 "}",
3047 Style);
3048 Style.ColumnLimit = 21;
3049 verifyFormat("#define X \\\n"
3050 " case 0: break;\n"
3051 "#include \"f\"",
3052 Style);
3053 verifyFormat("switch (a) {\n"
3054 "case 1: x = 1; break;\n"
3055 "case 2: return;\n"
3056 "case 3:\n"
3057 "case 4:\n"
3058 "case 5: return;\n"
3059 "default:\n"
3060 " y = 1;\n"
3061 " break;\n"
3062 "}",
3063 Style);
3064 Style.ColumnLimit = 80;
3065 Style.AllowShortCaseLabelsOnASingleLine = false;
3066 Style.IndentCaseLabels = true;
3067 verifyFormat("switch (n) {\n"
3068 " default /*comments*/:\n"
3069 " return true;\n"
3070 " case 0:\n"
3071 " return false;\n"
3072 "}",
3073 "switch (n) {\n"
3074 "default/*comments*/:\n"
3075 " return true;\n"
3076 "case 0:\n"
3077 " return false;\n"
3078 "}",
3079 Style);
3080 Style.AllowShortCaseLabelsOnASingleLine = true;
3081 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3082 Style.BraceWrapping.AfterCaseLabel = true;
3083 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
3084 verifyFormat("switch (n)\n"
3085 "{\n"
3086 " case 0:\n"
3087 " {\n"
3088 " return false;\n"
3089 " }\n"
3090 " default:\n"
3091 " {\n"
3092 " return true;\n"
3093 " }\n"
3094 "}",
3095 "switch (n) {\n"
3096 " case 0: {\n"
3097 " return false;\n"
3098 " }\n"
3099 " default:\n"
3100 " {\n"
3101 " return true;\n"
3102 " }\n"
3103 "}",
3104 Style);
3105}
3106
3107TEST_F(FormatTest, FormatsLabels) {
3108 verifyFormat("void f() {\n"
3109 " some_code();\n"
3110 "test_label:\n"
3111 " some_other_code();\n"
3112 " {\n"
3113 " some_more_code();\n"
3114 " another_label:\n"
3115 " some_more_code();\n"
3116 " }\n"
3117 "}");
3118 verifyFormat("{\n"
3119 " some_code();\n"
3120 "test_label:\n"
3121 " some_other_code();\n"
3122 "}");
3123 verifyFormat("{\n"
3124 " some_code();\n"
3125 "test_label:;\n"
3126 " int i = 0;\n"
3127 "}");
3128 verifyFormat("{\n"
3129 " some_code();\n"
3130 "test_label: { some_other_code(); }\n"
3131 "}");
3132 verifyFormat("{\n"
3133 " some_code();\n"
3134 "test_label: {\n"
3135 " some_other_code();\n"
3136 " some_other_code();\n"
3137 "}\n"
3138 "}");
3139 verifyFormat("{\n"
3140 "L0:\n"
3141 "[[foo]] L1:\n"
3142 "[[bar]] [[baz]] L2:\n"
3143 " g();\n"
3144 "}");
3145 verifyFormat("{\n"
3146 "[[foo]] L1: {\n"
3147 "[[bar]] [[baz]] L2:\n"
3148 " g();\n"
3149 "}\n"
3150 "}");
3151 verifyFormat("{\n"
3152 "[[foo]] L1:\n"
3153 " f();\n"
3154 " {\n"
3155 " [[bar]] [[baz]] L2:\n"
3156 " g();\n"
3157 " }\n"
3158 "}");
3159
3160 FormatStyle Style = getLLVMStyle();
3161 Style.IndentGotoLabels = false;
3162 verifyFormat("void f() {\n"
3163 " some_code();\n"
3164 "test_label:\n"
3165 " some_other_code();\n"
3166 " {\n"
3167 " some_more_code();\n"
3168 "another_label:\n"
3169 " some_more_code();\n"
3170 " }\n"
3171 "}",
3172 Style);
3173 verifyFormat("{\n"
3174 " some_code();\n"
3175 "test_label:\n"
3176 " some_other_code();\n"
3177 "}",
3178 Style);
3179 verifyFormat("{\n"
3180 " some_code();\n"
3181 "test_label:;\n"
3182 " int i = 0;\n"
3183 "}",
3184 Style);
3185 verifyFormat("{\n"
3186 " some_code();\n"
3187 "test_label: { some_other_code(); }\n"
3188 "}",
3189 Style);
3190 verifyFormat("{\n"
3191 "[[foo]] L1:\n"
3192 " f();\n"
3193 " {\n"
3194 "[[bar]] [[baz]] L2:\n"
3195 " g();\n"
3196 " }\n"
3197 "}",
3198 Style);
3199
3200 Style.ColumnLimit = 15;
3201 verifyFormat("#define FOO \\\n"
3202 "label: \\\n"
3203 " break;",
3204 Style);
3205
3206 // The opening brace may either be on the same unwrapped line as the colon or
3207 // on a separate one. The formatter should recognize both.
3208 Style = getLLVMStyle();
3209 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman;
3210 verifyFormat("{\n"
3211 " some_code();\n"
3212 "test_label:\n"
3213 "{\n"
3214 " some_other_code();\n"
3215 "}\n"
3216 "}",
3217 Style);
3218 verifyFormat("{\n"
3219 "[[foo]] L1:\n"
3220 "{\n"
3221 "[[bar]] [[baz]] L2:\n"
3222 " g();\n"
3223 "}\n"
3224 "}",
3225 Style);
3226}
3227
3228TEST_F(FormatTest, MultiLineControlStatements) {
3229 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20);
3230 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3231 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3232 // Short lines should keep opening brace on same line.
3233 verifyFormat("if (foo) {\n"
3234 " bar();\n"
3235 "}",
3236 "if(foo){bar();}", Style);
3237 verifyFormat("if (foo) {\n"
3238 " bar();\n"
3239 "} else {\n"
3240 " baz();\n"
3241 "}",
3242 "if(foo){bar();}else{baz();}", Style);
3243 verifyFormat("if (foo && bar) {\n"
3244 " baz();\n"
3245 "}",
3246 "if(foo&&bar){baz();}", Style);
3247 verifyFormat("if (foo) {\n"
3248 " bar();\n"
3249 "} else if (baz) {\n"
3250 " quux();\n"
3251 "}",
3252 "if(foo){bar();}else if(baz){quux();}", Style);
3253 verifyFormat("if (foo) {\n"
3254 " bar();\n"
3255 "} else if (baz) {\n"
3256 " quux();\n"
3257 "} else {\n"
3258 " foobar();\n"
3259 "}",
3260 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style);
3261 verifyFormat("for (;;) {\n"
3262 " foo();\n"
3263 "}",
3264 "for(;;){foo();}");
3265 verifyFormat("while (1) {\n"
3266 " foo();\n"
3267 "}",
3268 "while(1){foo();}", Style);
3269 verifyFormat("switch (foo) {\n"
3270 "case bar:\n"
3271 " return;\n"
3272 "}",
3273 "switch(foo){case bar:return;}", Style);
3274 verifyFormat("try {\n"
3275 " foo();\n"
3276 "} catch (...) {\n"
3277 " bar();\n"
3278 "}",
3279 "try{foo();}catch(...){bar();}", Style);
3280 verifyFormat("do {\n"
3281 " foo();\n"
3282 "} while (bar &&\n"
3283 " baz);",
3284 "do{foo();}while(bar&&baz);", Style);
3285 // Long lines should put opening brace on new line.
3286 verifyFormat("void f() {\n"
3287 " if (a1 && a2 &&\n"
3288 " a3)\n"
3289 " {\n"
3290 " quux();\n"
3291 " }\n"
3292 "}",
3293 "void f(){if(a1&&a2&&a3){quux();}}", Style);
3294 verifyFormat("if (foo && bar &&\n"
3295 " baz)\n"
3296 "{\n"
3297 " quux();\n"
3298 "}",
3299 "if(foo&&bar&&baz){quux();}", Style);
3300 verifyFormat("if (foo && bar &&\n"
3301 " baz)\n"
3302 "{\n"
3303 " quux();\n"
3304 "}",
3305 "if (foo && bar &&\n"
3306 " baz) {\n"
3307 " quux();\n"
3308 "}",
3309 Style);
3310 verifyFormat("if (foo) {\n"
3311 " bar();\n"
3312 "} else if (baz ||\n"
3313 " quux)\n"
3314 "{\n"
3315 " foobar();\n"
3316 "}",
3317 "if(foo){bar();}else if(baz||quux){foobar();}", Style);
3318 verifyFormat("if (foo) {\n"
3319 " bar();\n"
3320 "} else if (baz ||\n"
3321 " quux)\n"
3322 "{\n"
3323 " foobar();\n"
3324 "} else {\n"
3325 " barbaz();\n"
3326 "}",
3327 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3328 Style);
3329 verifyFormat("for (int i = 0;\n"
3330 " i < 10; ++i)\n"
3331 "{\n"
3332 " foo();\n"
3333 "}",
3334 "for(int i=0;i<10;++i){foo();}", Style);
3335 verifyFormat("foreach (int i,\n"
3336 " list)\n"
3337 "{\n"
3338 " foo();\n"
3339 "}",
3340 "foreach(int i, list){foo();}", Style);
3341 Style.ColumnLimit =
3342 40; // to concentrate at brace wrapping, not line wrap due to column limit
3343 verifyFormat("foreach (int i, list) {\n"
3344 " foo();\n"
3345 "}",
3346 "foreach(int i, list){foo();}", Style);
3347 Style.ColumnLimit =
3348 20; // to concentrate at brace wrapping, not line wrap due to column limit
3349 verifyFormat("while (foo || bar ||\n"
3350 " baz)\n"
3351 "{\n"
3352 " quux();\n"
3353 "}",
3354 "while(foo||bar||baz){quux();}", Style);
3355 verifyFormat("switch (\n"
3356 " foo = barbaz)\n"
3357 "{\n"
3358 "case quux:\n"
3359 " return;\n"
3360 "}",
3361 "switch(foo=barbaz){case quux:return;}", Style);
3362 verifyFormat("try {\n"
3363 " foo();\n"
3364 "} catch (\n"
3365 " Exception &bar)\n"
3366 "{\n"
3367 " baz();\n"
3368 "}",
3369 "try{foo();}catch(Exception&bar){baz();}", Style);
3370 Style.ColumnLimit =
3371 40; // to concentrate at brace wrapping, not line wrap due to column limit
3372 verifyFormat("try {\n"
3373 " foo();\n"
3374 "} catch (Exception &bar) {\n"
3375 " baz();\n"
3376 "}",
3377 "try{foo();}catch(Exception&bar){baz();}", Style);
3378 Style.ColumnLimit =
3379 20; // to concentrate at brace wrapping, not line wrap due to column limit
3380
3381 Style.BraceWrapping.BeforeElse = true;
3382 verifyFormat("if (foo) {\n"
3383 " bar();\n"
3384 "}\n"
3385 "else if (baz ||\n"
3386 " quux)\n"
3387 "{\n"
3388 " foobar();\n"
3389 "}\n"
3390 "else {\n"
3391 " barbaz();\n"
3392 "}",
3393 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3394 Style);
3395
3396 Style.BraceWrapping.BeforeCatch = true;
3397 verifyFormat("try {\n"
3398 " foo();\n"
3399 "}\n"
3400 "catch (...) {\n"
3401 " baz();\n"
3402 "}",
3403 "try{foo();}catch(...){baz();}", Style);
3404
3405 Style.BraceWrapping.AfterFunction = true;
3406 Style.BraceWrapping.AfterStruct = false;
3407 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3408 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3409 Style.ColumnLimit = 80;
3410 verifyFormat("void shortfunction() { bar(); }", Style);
3411 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3412 verifyFormat("struct T {};", Style);
3413
3414 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3415 verifyFormat("void shortfunction()\n"
3416 "{\n"
3417 " bar();\n"
3418 "}",
3419 Style);
3420 verifyFormat("struct T shortfunction()\n"
3421 "{\n"
3422 " return bar();\n"
3423 "}",
3424 Style);
3425 verifyFormat("struct T {};", Style);
3426
3427 Style.BraceWrapping.AfterFunction = false;
3428 Style.BraceWrapping.AfterStruct = true;
3429 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3430 verifyFormat("void shortfunction() { bar(); }", Style);
3431 verifyFormat("struct T shortfunction() { return bar(); }", Style);
3432 verifyFormat("struct T\n"
3433 "{\n"
3434 "};",
3435 Style);
3436
3437 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3438 verifyFormat("void shortfunction() {\n"
3439 " bar();\n"
3440 "}",
3441 Style);
3442 verifyFormat("struct T shortfunction() {\n"
3443 " return bar();\n"
3444 "}",
3445 Style);
3446 verifyFormat("struct T\n"
3447 "{\n"
3448 "};",
3449 Style);
3450
3451 Style = getLLVMStyle();
3452 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
3453 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
3454 Style.AllowShortLoopsOnASingleLine = true;
3455 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
3456 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine;
3457 verifyFormat("if (true) { return; }", Style);
3458 verifyFormat("while (true) { return; }", Style);
3459 // Failing test in https://reviews.llvm.org/D114521#3151727
3460 verifyFormat("for (;;) { bar(); }", Style);
3461}
3462
3463TEST_F(FormatTest, BeforeWhile) {
3464 FormatStyle Style = getLLVMStyle();
3465 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
3466
3467 verifyFormat("do {\n"
3468 " foo();\n"
3469 "} while (1);",
3470 Style);
3471 Style.BraceWrapping.BeforeWhile = true;
3472 verifyFormat("do {\n"
3473 " foo();\n"
3474 "}\n"
3475 "while (1);",
3476 Style);
3477}
3478
3479//===----------------------------------------------------------------------===//
3480// Tests for classes, namespaces, etc.
3481//===----------------------------------------------------------------------===//
3482
3483TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
3484 verifyFormat("class A {};");
3485}
3486
3487TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
3488 verifyFormat("class A {\n"
3489 "public:\n"
3490 "public: // comment\n"
3491 "protected:\n"
3492 "private:\n"
3493 " void f() {}\n"
3494 "};");
3495 verifyFormat("export class A {\n"
3496 "public:\n"
3497 "public: // comment\n"
3498 "protected:\n"
3499 "private:\n"
3500 " void f() {}\n"
3501 "};");
3502 verifyGoogleFormat("class A {\n"
3503 " public:\n"
3504 " protected:\n"
3505 " private:\n"
3506 " void f() {}\n"
3507 "};");
3508 verifyGoogleFormat("export class A {\n"
3509 " public:\n"
3510 " protected:\n"
3511 " private:\n"
3512 " void f() {}\n"
3513 "};");
3514 verifyFormat("class A {\n"
3515 "public slots:\n"
3516 " void f1() {}\n"
3517 "public Q_SLOTS:\n"
3518 " void f2() {}\n"
3519 "protected slots:\n"
3520 " void f3() {}\n"
3521 "protected Q_SLOTS:\n"
3522 " void f4() {}\n"
3523 "private slots:\n"
3524 " void f5() {}\n"
3525 "private Q_SLOTS:\n"
3526 " void f6() {}\n"
3527 "signals:\n"
3528 " void g1();\n"
3529 "Q_SIGNALS:\n"
3530 " void g2();\n"
3531 "};");
3532
3533 // Don't interpret 'signals' the wrong way.
3534 verifyFormat("signals.set();");
3535 verifyFormat("for (Signals signals : f()) {\n}");
3536 verifyFormat("{\n"
3537 " signals.set(); // This needs indentation.\n"
3538 "}");
3539 verifyFormat("void f() {\n"
3540 "label:\n"
3541 " signals.baz();\n"
3542 "}");
3543
3544 const auto Style = getLLVMStyle(Language: FormatStyle::LK_C);
3545 verifyFormat("private[1];", Style);
3546 verifyFormat("testArray[public] = 1;");
3547 verifyFormat("public();", Style);
3548 verifyFormat("myFunc(public);");
3549 verifyFormat("std::vector<int> testVec = {private};");
3550 verifyFormat("private.p = 1;", Style);
3551 verifyFormat("void function(private...) {};");
3552 verifyFormat("if (private && public)");
3553 verifyFormat("private &= true;", Style);
3554 verifyFormat("int x = private * public;");
3555 verifyFormat("public *= private;", Style);
3556 verifyFormat("int x = public + private;");
3557 verifyFormat("private++;", Style);
3558 verifyFormat("++private;");
3559 verifyFormat("public += private;", Style);
3560 verifyFormat("public = public - private;", Style);
3561 verifyFormat("public->foo();", Style);
3562 verifyFormat("private--;", Style);
3563 verifyFormat("--private;");
3564 verifyFormat("public -= 1;", Style);
3565 verifyFormat("if (!private && !public)");
3566 verifyFormat("public != private;", Style);
3567 verifyFormat("int x = public / private;");
3568 verifyFormat("public /= 2;", Style);
3569 verifyFormat("public = public % 2;", Style);
3570 verifyFormat("public %= 2;", Style);
3571 verifyFormat("if (public < private)");
3572 verifyFormat("public << private;", Style);
3573 verifyFormat("public <<= private;", Style);
3574 verifyFormat("if (public > private)");
3575 verifyFormat("public >> private;", Style);
3576 verifyFormat("public >>= private;", Style);
3577 verifyFormat("public ^ private;", Style);
3578 verifyFormat("public ^= private;", Style);
3579 verifyFormat("public | private;", Style);
3580 verifyFormat("public |= private;", Style);
3581 verifyFormat("auto x = private ? 1 : 2;");
3582 verifyFormat("if (public == private)");
3583 verifyFormat("void foo(public, private)");
3584
3585 verifyFormat("class A {\n"
3586 "public:\n"
3587 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3588 "\n"
3589 "private:\n"
3590 " int c;\n"
3591 "};\n"
3592 "class B {\n"
3593 "public:\n"
3594 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3595 "\n"
3596 "private:\n"
3597 " int c;\n"
3598 "};");
3599}
3600
3601TEST_F(FormatTest, SeparatesLogicalBlocks) {
3602 verifyFormat("class A {\n"
3603 "public:\n"
3604 " void f();\n"
3605 "\n"
3606 "private:\n"
3607 " void g() {}\n"
3608 " // test\n"
3609 "protected:\n"
3610 " int h;\n"
3611 "};",
3612 "class A {\n"
3613 "public:\n"
3614 "void f();\n"
3615 "private:\n"
3616 "void g() {}\n"
3617 "// test\n"
3618 "protected:\n"
3619 "int h;\n"
3620 "};");
3621 verifyFormat("class A {\n"
3622 "protected:\n"
3623 "public:\n"
3624 " void f();\n"
3625 "};",
3626 "class A {\n"
3627 "protected:\n"
3628 "\n"
3629 "public:\n"
3630 "\n"
3631 " void f();\n"
3632 "};");
3633
3634 // Even ensure proper spacing inside macros.
3635 verifyFormat("#define B \\\n"
3636 " class A { \\\n"
3637 " protected: \\\n"
3638 " public: \\\n"
3639 " void f(); \\\n"
3640 " };",
3641 "#define B \\\n"
3642 " class A { \\\n"
3643 " protected: \\\n"
3644 " \\\n"
3645 " public: \\\n"
3646 " \\\n"
3647 " void f(); \\\n"
3648 " };",
3649 getGoogleStyle());
3650 // But don't remove empty lines after macros ending in access specifiers.
3651 verifyFormat("#define A private:\n"
3652 "\n"
3653 "int i;",
3654 "#define A private:\n"
3655 "\n"
3656 "int i;");
3657}
3658
3659TEST_F(FormatTest, FormatsClasses) {
3660 verifyFormat("class A : public B {};");
3661 verifyFormat("class A : public ::B {};");
3662
3663 verifyFormat(
3664 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3665 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3666 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3667 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3668 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3669 verifyFormat(
3670 "class A : public B, public C, public D, public E, public F {};");
3671 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3672 " public C,\n"
3673 " public D,\n"
3674 " public E,\n"
3675 " public F,\n"
3676 " public G {};");
3677
3678 verifyFormat("class\n"
3679 " ReallyReallyLongClassName {\n"
3680 " int i;\n"
3681 "};",
3682 getLLVMStyleWithColumns(32));
3683 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3684 " aaaaaaaaaaaaaaaa> {};");
3685 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3686 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3687 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3688 verifyFormat("template <class R, class C>\n"
3689 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3690 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3691 verifyFormat("class ::A::B {};");
3692}
3693
3694TEST_F(FormatTest, BreakInheritanceStyle) {
3695 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle();
3696 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList =
3697 FormatStyle::BILS_BeforeComma;
3698 verifyFormat("class MyClass : public X {};",
3699 StyleWithInheritanceBreakBeforeComma);
3700 verifyFormat("class MyClass\n"
3701 " : public X\n"
3702 " , public Y {};",
3703 StyleWithInheritanceBreakBeforeComma);
3704 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3705 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3706 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3707 StyleWithInheritanceBreakBeforeComma);
3708 verifyFormat("struct aaaaaaaaaaaaa\n"
3709 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3710 " aaaaaaaaaaaaaaaa> {};",
3711 StyleWithInheritanceBreakBeforeComma);
3712
3713 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle();
3714 StyleWithInheritanceBreakAfterColon.BreakInheritanceList =
3715 FormatStyle::BILS_AfterColon;
3716 verifyFormat("class MyClass : public X {};",
3717 StyleWithInheritanceBreakAfterColon);
3718 verifyFormat("class MyClass : public X, public Y {};",
3719 StyleWithInheritanceBreakAfterColon);
3720 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3721 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3722 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3723 StyleWithInheritanceBreakAfterColon);
3724 verifyFormat("struct aaaaaaaaaaaaa :\n"
3725 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3726 " aaaaaaaaaaaaaaaa> {};",
3727 StyleWithInheritanceBreakAfterColon);
3728
3729 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle();
3730 StyleWithInheritanceBreakAfterComma.BreakInheritanceList =
3731 FormatStyle::BILS_AfterComma;
3732 verifyFormat("class MyClass : public X {};",
3733 StyleWithInheritanceBreakAfterComma);
3734 verifyFormat("class MyClass : public X,\n"
3735 " public Y {};",
3736 StyleWithInheritanceBreakAfterComma);
3737 verifyFormat(
3738 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3739 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3740 "{};",
3741 StyleWithInheritanceBreakAfterComma);
3742 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3743 " aaaaaaaaaaaaaaaa> {};",
3744 StyleWithInheritanceBreakAfterComma);
3745 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3746 " : public OnceBreak,\n"
3747 " public AlwaysBreak,\n"
3748 " EvenBasesFitInOneLine {};",
3749 StyleWithInheritanceBreakAfterComma);
3750}
3751
3752TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) {
3753 verifyFormat("class A {\n} a, b;");
3754 verifyFormat("struct A {\n} a, b;");
3755 verifyFormat("union A {\n} a, b;");
3756
3757 verifyFormat("constexpr class A {\n} a, b;");
3758 verifyFormat("constexpr struct A {\n} a, b;");
3759 verifyFormat("constexpr union A {\n} a, b;");
3760
3761 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3762 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3763 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3764
3765 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3766 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3767 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3768
3769 verifyFormat("namespace ns {\n"
3770 "class {\n"
3771 "} a, b;\n"
3772 "} // namespace ns");
3773 verifyFormat("namespace ns {\n"
3774 "const class {\n"
3775 "} a, b;\n"
3776 "} // namespace ns");
3777 verifyFormat("namespace ns {\n"
3778 "constexpr class C {\n"
3779 "} a, b;\n"
3780 "} // namespace ns");
3781 verifyFormat("namespace ns {\n"
3782 "class { /* comment */\n"
3783 "} a, b;\n"
3784 "} // namespace ns");
3785 verifyFormat("namespace ns {\n"
3786 "const class { /* comment */\n"
3787 "} a, b;\n"
3788 "} // namespace ns");
3789}
3790
3791TEST_F(FormatTest, FormatsEnum) {
3792 verifyFormat("enum {\n"
3793 " Zero,\n"
3794 " One = 1,\n"
3795 " Two = One + 1,\n"
3796 " Three = (One + Two),\n"
3797 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3798 " Five = (One, Two, Three, Four, 5)\n"
3799 "};");
3800 verifyGoogleFormat("enum {\n"
3801 " Zero,\n"
3802 " One = 1,\n"
3803 " Two = One + 1,\n"
3804 " Three = (One + Two),\n"
3805 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3806 " Five = (One, Two, Three, Four, 5)\n"
3807 "};");
3808 verifyFormat("enum Enum {};");
3809 verifyFormat("enum {};");
3810 verifyFormat("enum X E {} d;");
3811 verifyFormat("enum __attribute__((...)) E {} d;");
3812 verifyFormat("enum __declspec__((...)) E {} d;");
3813 verifyFormat("enum [[nodiscard]] E {} d;");
3814 verifyFormat("enum {\n"
3815 " Bar = Foo<int, int>::value\n"
3816 "};",
3817 getLLVMStyleWithColumns(30));
3818
3819 verifyFormat("enum ShortEnum { A, B, C };");
3820 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3821
3822 verifyFormat("enum KeepEmptyLines {\n"
3823 " ONE,\n"
3824 "\n"
3825 " TWO,\n"
3826 "\n"
3827 " THREE\n"
3828 "}",
3829 "enum KeepEmptyLines {\n"
3830 " ONE,\n"
3831 "\n"
3832 " TWO,\n"
3833 "\n"
3834 "\n"
3835 " THREE\n"
3836 "}");
3837 verifyFormat("enum E { // comment\n"
3838 " ONE,\n"
3839 " TWO\n"
3840 "};\n"
3841 "int i;");
3842
3843 FormatStyle EightIndent = getLLVMStyle();
3844 EightIndent.IndentWidth = 8;
3845 verifyFormat("enum {\n"
3846 " VOID,\n"
3847 " CHAR,\n"
3848 " SHORT,\n"
3849 " INT,\n"
3850 " LONG,\n"
3851 " SIGNED,\n"
3852 " UNSIGNED,\n"
3853 " BOOL,\n"
3854 " FLOAT,\n"
3855 " DOUBLE,\n"
3856 " COMPLEX\n"
3857 "};",
3858 EightIndent);
3859
3860 verifyFormat("enum [[nodiscard]] E {\n"
3861 " ONE,\n"
3862 " TWO,\n"
3863 "};");
3864 verifyFormat("enum [[nodiscard]] E {\n"
3865 " // Comment 1\n"
3866 " ONE,\n"
3867 " // Comment 2\n"
3868 " TWO,\n"
3869 "};");
3870 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3871 " // Comment 1\n"
3872 " ONE,\n"
3873 " // Comment 2\n"
3874 " TWO\n"
3875 "};");
3876 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3877 " // Comment 1\n"
3878 " ONE,\n"
3879 " // Comment 2\n"
3880 " TWO\n"
3881 "};");
3882 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3883 " A,\n"
3884 " // bar\n"
3885 " B\n"
3886 "};",
3887 "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3888 " A,\n"
3889 " // bar\n"
3890 " B};");
3891
3892 // Not enums.
3893 verifyFormat("enum X f() {\n"
3894 " a();\n"
3895 " return 42;\n"
3896 "}");
3897 verifyFormat("enum X Type::f() {\n"
3898 " a();\n"
3899 " return 42;\n"
3900 "}");
3901 verifyFormat("enum ::X f() {\n"
3902 " a();\n"
3903 " return 42;\n"
3904 "}");
3905 verifyFormat("enum ns::X f() {\n"
3906 " a();\n"
3907 " return 42;\n"
3908 "}");
3909}
3910
3911TEST_F(FormatTest, FormatsEnumsWithErrors) {
3912 verifyFormat("enum Type {\n"
3913 " One = 0; // These semicolons should be commas.\n"
3914 " Two = 1;\n"
3915 "};");
3916 verifyFormat("namespace n {\n"
3917 "enum Type {\n"
3918 " One,\n"
3919 " Two, // missing };\n"
3920 " int i;\n"
3921 "}\n"
3922 "void g() {}");
3923}
3924
3925TEST_F(FormatTest, FormatsEnumStruct) {
3926 verifyFormat("enum struct {\n"
3927 " Zero,\n"
3928 " One = 1,\n"
3929 " Two = One + 1,\n"
3930 " Three = (One + Two),\n"
3931 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3932 " Five = (One, Two, Three, Four, 5)\n"
3933 "};");
3934 verifyFormat("enum struct Enum {};");
3935 verifyFormat("enum struct {};");
3936 verifyFormat("enum struct X E {} d;");
3937 verifyFormat("enum struct __attribute__((...)) E {} d;");
3938 verifyFormat("enum struct __declspec__((...)) E {} d;");
3939 verifyFormat("enum struct [[nodiscard]] E {} d;");
3940 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3941
3942 verifyFormat("enum struct [[nodiscard]] E {\n"
3943 " ONE,\n"
3944 " TWO,\n"
3945 "};");
3946 verifyFormat("enum struct [[nodiscard]] E {\n"
3947 " // Comment 1\n"
3948 " ONE,\n"
3949 " // Comment 2\n"
3950 " TWO,\n"
3951 "};");
3952}
3953
3954TEST_F(FormatTest, FormatsEnumClass) {
3955 verifyFormat("enum class {\n"
3956 " Zero,\n"
3957 " One = 1,\n"
3958 " Two = One + 1,\n"
3959 " Three = (One + Two),\n"
3960 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3961 " Five = (One, Two, Three, Four, 5)\n"
3962 "};");
3963 verifyFormat("enum class Enum {};");
3964 verifyFormat("enum class {};");
3965 verifyFormat("enum class X E {} d;");
3966 verifyFormat("enum class __attribute__((...)) E {} d;");
3967 verifyFormat("enum class __declspec__((...)) E {} d;");
3968 verifyFormat("enum class [[nodiscard]] E {} d;");
3969 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3970
3971 verifyFormat("enum class [[nodiscard]] E {\n"
3972 " ONE,\n"
3973 " TWO,\n"
3974 "};");
3975 verifyFormat("enum class [[nodiscard]] E {\n"
3976 " // Comment 1\n"
3977 " ONE,\n"
3978 " // Comment 2\n"
3979 " TWO,\n"
3980 "};");
3981}
3982
3983TEST_F(FormatTest, FormatsEnumTypes) {
3984 verifyFormat("enum X : int {\n"
3985 " A, // Force multiple lines.\n"
3986 " B\n"
3987 "};");
3988 verifyFormat("enum X : int { A, B };");
3989 verifyFormat("enum X : std::uint32_t { A, B };");
3990}
3991
3992TEST_F(FormatTest, FormatsTypedefEnum) {
3993 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
3994 verifyFormat("typedef enum {} EmptyEnum;");
3995 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3996 verifyFormat("typedef enum {\n"
3997 " ZERO = 0,\n"
3998 " ONE = 1,\n"
3999 " TWO = 2,\n"
4000 " THREE = 3\n"
4001 "} LongEnum;",
4002 Style);
4003 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4004 Style.BraceWrapping.AfterEnum = true;
4005 verifyFormat("typedef enum {} EmptyEnum;");
4006 verifyFormat("typedef enum { A, B, C } ShortEnum;");
4007 verifyFormat("typedef enum\n"
4008 "{\n"
4009 " ZERO = 0,\n"
4010 " ONE = 1,\n"
4011 " TWO = 2,\n"
4012 " THREE = 3\n"
4013 "} LongEnum;",
4014 Style);
4015}
4016
4017TEST_F(FormatTest, FormatsNSEnums) {
4018 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
4019 verifyGoogleFormat(
4020 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
4021 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
4022 " // Information about someDecentlyLongValue.\n"
4023 " someDecentlyLongValue,\n"
4024 " // Information about anotherDecentlyLongValue.\n"
4025 " anotherDecentlyLongValue,\n"
4026 " // Information about aThirdDecentlyLongValue.\n"
4027 " aThirdDecentlyLongValue\n"
4028 "};");
4029 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
4030 " // Information about someDecentlyLongValue.\n"
4031 " someDecentlyLongValue,\n"
4032 " // Information about anotherDecentlyLongValue.\n"
4033 " anotherDecentlyLongValue,\n"
4034 " // Information about aThirdDecentlyLongValue.\n"
4035 " aThirdDecentlyLongValue\n"
4036 "};");
4037 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
4038 " a = 1,\n"
4039 " b = 2,\n"
4040 " c = 3,\n"
4041 "};");
4042 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
4043 " a = 1,\n"
4044 " b = 2,\n"
4045 " c = 3,\n"
4046 "};");
4047 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
4048 " a = 1,\n"
4049 " b = 2,\n"
4050 " c = 3,\n"
4051 "};");
4052 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4053 " a = 1,\n"
4054 " b = 2,\n"
4055 " c = 3,\n"
4056 "};");
4057}
4058
4059TEST_F(FormatTest, FormatsBitfields) {
4060 verifyFormat("struct Bitfields {\n"
4061 " unsigned sClass : 8;\n"
4062 " unsigned ValueKind : 2;\n"
4063 "};");
4064 verifyFormat("struct A {\n"
4065 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4066 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4067 "};");
4068 verifyFormat("struct MyStruct {\n"
4069 " uchar data;\n"
4070 " uchar : 8;\n"
4071 " uchar : 8;\n"
4072 " uchar other;\n"
4073 "};");
4074 verifyFormat("struct foo {\n"
4075 " uint8_t i_am_a_bit_field_this_long\n"
4076 " : struct_with_constexpr::i_am_a_constexpr_lengthhhhh;\n"
4077 "};");
4078 FormatStyle Style = getLLVMStyle();
4079 Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
4080 verifyFormat("struct Bitfields {\n"
4081 " unsigned sClass:8;\n"
4082 " unsigned ValueKind:2;\n"
4083 " uchar other;\n"
4084 "};",
4085 Style);
4086 verifyFormat("struct A {\n"
4087 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4088 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4089 "};",
4090 Style);
4091 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before;
4092 verifyFormat("struct Bitfields {\n"
4093 " unsigned sClass :8;\n"
4094 " unsigned ValueKind :2;\n"
4095 " uchar other;\n"
4096 "};",
4097 Style);
4098 Style.BitFieldColonSpacing = FormatStyle::BFCS_After;
4099 verifyFormat("struct Bitfields {\n"
4100 " unsigned sClass: 8;\n"
4101 " unsigned ValueKind: 2;\n"
4102 " uchar other;\n"
4103 "};",
4104 Style);
4105}
4106
4107TEST_F(FormatTest, FormatsNamespaces) {
4108 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
4109 LLVMWithNoNamespaceFix.FixNamespaceComments = false;
4110
4111 verifyFormat("namespace some_namespace {\n"
4112 "class A {};\n"
4113 "void f() { f(); }\n"
4114 "}",
4115 LLVMWithNoNamespaceFix);
4116 verifyFormat("#define M(x) x##x\n"
4117 "namespace M(x) {\n"
4118 "class A {};\n"
4119 "void f() { f(); }\n"
4120 "}",
4121 LLVMWithNoNamespaceFix);
4122 verifyFormat("#define M(x) x##x\n"
4123 "namespace N::inline M(x) {\n"
4124 "class A {};\n"
4125 "void f() { f(); }\n"
4126 "}",
4127 LLVMWithNoNamespaceFix);
4128 verifyFormat("#define M(x) x##x\n"
4129 "namespace M(x)::inline N {\n"
4130 "class A {};\n"
4131 "void f() { f(); }\n"
4132 "}",
4133 LLVMWithNoNamespaceFix);
4134 verifyFormat("#define M(x) x##x\n"
4135 "namespace N::M(x) {\n"
4136 "class A {};\n"
4137 "void f() { f(); }\n"
4138 "}",
4139 LLVMWithNoNamespaceFix);
4140 verifyFormat("#define M(x) x##x\n"
4141 "namespace M::N(x) {\n"
4142 "class A {};\n"
4143 "void f() { f(); }\n"
4144 "}",
4145 LLVMWithNoNamespaceFix);
4146 verifyFormat("namespace N::inline D {\n"
4147 "class A {};\n"
4148 "void f() { f(); }\n"
4149 "}",
4150 LLVMWithNoNamespaceFix);
4151 verifyFormat("namespace N::inline D::E {\n"
4152 "class A {};\n"
4153 "void f() { f(); }\n"
4154 "}",
4155 LLVMWithNoNamespaceFix);
4156 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4157 "class A {};\n"
4158 "void f() { f(); }\n"
4159 "}",
4160 LLVMWithNoNamespaceFix);
4161 verifyFormat("/* something */ namespace some_namespace {\n"
4162 "class A {};\n"
4163 "void f() { f(); }\n"
4164 "}",
4165 LLVMWithNoNamespaceFix);
4166 verifyFormat("namespace {\n"
4167 "class A {};\n"
4168 "void f() { f(); }\n"
4169 "}",
4170 LLVMWithNoNamespaceFix);
4171 verifyFormat("/* something */ namespace {\n"
4172 "class A {};\n"
4173 "void f() { f(); }\n"
4174 "}",
4175 LLVMWithNoNamespaceFix);
4176 verifyFormat("inline namespace X {\n"
4177 "class A {};\n"
4178 "void f() { f(); }\n"
4179 "}",
4180 LLVMWithNoNamespaceFix);
4181 verifyFormat("/* something */ inline namespace X {\n"
4182 "class A {};\n"
4183 "void f() { f(); }\n"
4184 "}",
4185 LLVMWithNoNamespaceFix);
4186 verifyFormat("export namespace X {\n"
4187 "class A {};\n"
4188 "void f() { f(); }\n"
4189 "}",
4190 LLVMWithNoNamespaceFix);
4191 verifyFormat("using namespace some_namespace;\n"
4192 "class A {};\n"
4193 "void f() { f(); }",
4194 LLVMWithNoNamespaceFix);
4195
4196 // This code is more common than we thought; if we
4197 // layout this correctly the semicolon will go into
4198 // its own line, which is undesirable.
4199 verifyFormat("namespace {};", LLVMWithNoNamespaceFix);
4200 verifyFormat("namespace {\n"
4201 "class A {};\n"
4202 "};",
4203 LLVMWithNoNamespaceFix);
4204
4205 verifyFormat("namespace {\n"
4206 "int SomeVariable = 0; // comment\n"
4207 "} // namespace",
4208 LLVMWithNoNamespaceFix);
4209 verifyFormat("#ifndef HEADER_GUARD\n"
4210 "#define HEADER_GUARD\n"
4211 "namespace my_namespace {\n"
4212 "int i;\n"
4213 "} // my_namespace\n"
4214 "#endif // HEADER_GUARD",
4215 "#ifndef HEADER_GUARD\n"
4216 " #define HEADER_GUARD\n"
4217 " namespace my_namespace {\n"
4218 "int i;\n"
4219 "} // my_namespace\n"
4220 "#endif // HEADER_GUARD",
4221 LLVMWithNoNamespaceFix);
4222
4223 verifyFormat("namespace A::B {\n"
4224 "class C {};\n"
4225 "}",
4226 LLVMWithNoNamespaceFix);
4227
4228 FormatStyle Style = getLLVMStyle();
4229 Style.NamespaceIndentation = FormatStyle::NI_All;
4230 verifyFormat("namespace out {\n"
4231 " int i;\n"
4232 " namespace in {\n"
4233 " int i;\n"
4234 " } // namespace in\n"
4235 "} // namespace out",
4236 "namespace out {\n"
4237 "int i;\n"
4238 "namespace in {\n"
4239 "int i;\n"
4240 "} // namespace in\n"
4241 "} // namespace out",
4242 Style);
4243
4244 FormatStyle ShortInlineFunctions = getLLVMStyle();
4245 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All;
4246 ShortInlineFunctions.AllowShortFunctionsOnASingleLine =
4247 FormatStyle::SFS_Inline;
4248 verifyFormat("namespace {\n"
4249 " void f() {\n"
4250 " return;\n"
4251 " }\n"
4252 "} // namespace",
4253 ShortInlineFunctions);
4254 verifyFormat("namespace { /* comment */\n"
4255 " void f() {\n"
4256 " return;\n"
4257 " }\n"
4258 "} // namespace",
4259 ShortInlineFunctions);
4260 verifyFormat("namespace { // comment\n"
4261 " void f() {\n"
4262 " return;\n"
4263 " }\n"
4264 "} // namespace",
4265 ShortInlineFunctions);
4266 verifyFormat("namespace {\n"
4267 " int some_int;\n"
4268 " void f() {\n"
4269 " return;\n"
4270 " }\n"
4271 "} // namespace",
4272 ShortInlineFunctions);
4273 verifyFormat("namespace interface {\n"
4274 " void f() {\n"
4275 " return;\n"
4276 " }\n"
4277 "} // namespace interface",
4278 ShortInlineFunctions);
4279 verifyFormat("namespace {\n"
4280 " class X {\n"
4281 " void f() { return; }\n"
4282 " };\n"
4283 "} // namespace",
4284 ShortInlineFunctions);
4285 verifyFormat("namespace {\n"
4286 " class X { /* comment */\n"
4287 " void f() { return; }\n"
4288 " };\n"
4289 "} // namespace",
4290 ShortInlineFunctions);
4291 verifyFormat("namespace {\n"
4292 " class X { // comment\n"
4293 " void f() { return; }\n"
4294 " };\n"
4295 "} // namespace",
4296 ShortInlineFunctions);
4297 verifyFormat("namespace {\n"
4298 " struct X {\n"
4299 " void f() { return; }\n"
4300 " };\n"
4301 "} // namespace",
4302 ShortInlineFunctions);
4303 verifyFormat("namespace {\n"
4304 " union X {\n"
4305 " void f() { return; }\n"
4306 " };\n"
4307 "} // namespace",
4308 ShortInlineFunctions);
4309 verifyFormat("extern \"C\" {\n"
4310 "void f() {\n"
4311 " return;\n"
4312 "}\n"
4313 "} // namespace",
4314 ShortInlineFunctions);
4315 verifyFormat("namespace {\n"
4316 " class X {\n"
4317 " void f() { return; }\n"
4318 " } x;\n"
4319 "} // namespace",
4320 ShortInlineFunctions);
4321 verifyFormat("namespace {\n"
4322 " [[nodiscard]] class X {\n"
4323 " void f() { return; }\n"
4324 " };\n"
4325 "} // namespace",
4326 ShortInlineFunctions);
4327 verifyFormat("namespace {\n"
4328 " static class X {\n"
4329 " void f() { return; }\n"
4330 " } x;\n"
4331 "} // namespace",
4332 ShortInlineFunctions);
4333 verifyFormat("namespace {\n"
4334 " constexpr class X {\n"
4335 " void f() { return; }\n"
4336 " } x;\n"
4337 "} // namespace",
4338 ShortInlineFunctions);
4339
4340 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent;
4341 verifyFormat("extern \"C\" {\n"
4342 " void f() {\n"
4343 " return;\n"
4344 " }\n"
4345 "} // namespace",
4346 ShortInlineFunctions);
4347
4348 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4349 verifyFormat("namespace out {\n"
4350 "int i;\n"
4351 "namespace in {\n"
4352 " int i;\n"
4353 "} // namespace in\n"
4354 "} // namespace out",
4355 "namespace out {\n"
4356 "int i;\n"
4357 "namespace in {\n"
4358 "int i;\n"
4359 "} // namespace in\n"
4360 "} // namespace out",
4361 Style);
4362
4363 Style.NamespaceIndentation = FormatStyle::NI_None;
4364 verifyFormat("template <class T>\n"
4365 "concept a_concept = X<>;\n"
4366 "namespace B {\n"
4367 "struct b_struct {};\n"
4368 "} // namespace B",
4369 Style);
4370 verifyFormat("template <int I>\n"
4371 "constexpr void foo()\n"
4372 " requires(I == 42)\n"
4373 "{}\n"
4374 "namespace ns {\n"
4375 "void foo() {}\n"
4376 "} // namespace ns",
4377 Style);
4378
4379 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle();
4380 LLVMWithCompactInnerNamespace.CompactNamespaces = true;
4381 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner;
4382 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4383 "// block for debug mode\n"
4384 "#ifndef NDEBUG\n"
4385 "#endif\n"
4386 "}}} // namespace ns1::ns2::ns3",
4387 LLVMWithCompactInnerNamespace);
4388}
4389
4390TEST_F(FormatTest, NamespaceMacros) {
4391 FormatStyle Style = getLLVMStyle();
4392 Style.NamespaceMacros.push_back(x: "TESTSUITE");
4393
4394 verifyFormat("TESTSUITE(A) {\n"
4395 "int foo();\n"
4396 "} // TESTSUITE(A)",
4397 Style);
4398
4399 verifyFormat("TESTSUITE(A, B) {\n"
4400 "int foo();\n"
4401 "} // TESTSUITE(A)",
4402 Style);
4403
4404 // Properly indent according to NamespaceIndentation style
4405 Style.NamespaceIndentation = FormatStyle::NI_All;
4406 verifyFormat("TESTSUITE(A) {\n"
4407 " int foo();\n"
4408 "} // TESTSUITE(A)",
4409 Style);
4410 verifyFormat("TESTSUITE(A) {\n"
4411 " namespace B {\n"
4412 " int foo();\n"
4413 " } // namespace B\n"
4414 "} // TESTSUITE(A)",
4415 Style);
4416 verifyFormat("namespace A {\n"
4417 " TESTSUITE(B) {\n"
4418 " int foo();\n"
4419 " } // TESTSUITE(B)\n"
4420 "} // namespace A",
4421 Style);
4422
4423 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4424 verifyFormat("TESTSUITE(A) {\n"
4425 "TESTSUITE(B) {\n"
4426 " int foo();\n"
4427 "} // TESTSUITE(B)\n"
4428 "} // TESTSUITE(A)",
4429 Style);
4430 verifyFormat("TESTSUITE(A) {\n"
4431 "namespace B {\n"
4432 " int foo();\n"
4433 "} // namespace B\n"
4434 "} // TESTSUITE(A)",
4435 Style);
4436 verifyFormat("namespace A {\n"
4437 "TESTSUITE(B) {\n"
4438 " int foo();\n"
4439 "} // TESTSUITE(B)\n"
4440 "} // namespace A",
4441 Style);
4442
4443 // Properly merge namespace-macros blocks in CompactNamespaces mode
4444 Style.NamespaceIndentation = FormatStyle::NI_None;
4445 Style.CompactNamespaces = true;
4446 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4447 "}} // TESTSUITE(A::B)",
4448 Style);
4449
4450 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4451 "}} // TESTSUITE(out::in)",
4452 "TESTSUITE(out) {\n"
4453 "TESTSUITE(in) {\n"
4454 "} // TESTSUITE(in)\n"
4455 "} // TESTSUITE(out)",
4456 Style);
4457
4458 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4459 "}} // TESTSUITE(out::in)",
4460 "TESTSUITE(out) {\n"
4461 "TESTSUITE(in) {\n"
4462 "} // TESTSUITE(in)\n"
4463 "} // TESTSUITE(out)",
4464 Style);
4465
4466 // Do not merge different namespaces/macros
4467 verifyFormat("namespace out {\n"
4468 "TESTSUITE(in) {\n"
4469 "} // TESTSUITE(in)\n"
4470 "} // namespace out",
4471 Style);
4472 verifyFormat("TESTSUITE(out) {\n"
4473 "namespace in {\n"
4474 "} // namespace in\n"
4475 "} // TESTSUITE(out)",
4476 Style);
4477 Style.NamespaceMacros.push_back(x: "FOOBAR");
4478 verifyFormat("TESTSUITE(out) {\n"
4479 "FOOBAR(in) {\n"
4480 "} // FOOBAR(in)\n"
4481 "} // TESTSUITE(out)",
4482 Style);
4483}
4484
4485TEST_F(FormatTest, FormatsCompactNamespaces) {
4486 FormatStyle Style = getLLVMStyle();
4487 Style.CompactNamespaces = true;
4488 Style.NamespaceMacros.push_back(x: "TESTSUITE");
4489
4490 verifyFormat("namespace A { namespace B {\n"
4491 "}} // namespace A::B",
4492 Style);
4493
4494 verifyFormat("namespace out { namespace in {\n"
4495 "}} // namespace out::in",
4496 "namespace out {\n"
4497 "namespace in {\n"
4498 "} // namespace in\n"
4499 "} // namespace out",
4500 Style);
4501
4502 // Only namespaces which have both consecutive opening and end get compacted
4503 verifyFormat("namespace out {\n"
4504 "namespace in1 {\n"
4505 "} // namespace in1\n"
4506 "namespace in2 {\n"
4507 "} // namespace in2\n"
4508 "} // namespace out",
4509 Style);
4510
4511 verifyFormat("namespace out {\n"
4512 "int i;\n"
4513 "namespace in {\n"
4514 "int j;\n"
4515 "} // namespace in\n"
4516 "int k;\n"
4517 "} // namespace out",
4518 "namespace out { int i;\n"
4519 "namespace in { int j; } // namespace in\n"
4520 "int k; } // namespace out",
4521 Style);
4522
4523 Style.ColumnLimit = 41;
4524 verifyFormat("namespace A { namespace B { namespace C {\n"
4525 "}}} // namespace A::B::C",
4526 "namespace A { namespace B {\n"
4527 "namespace C {\n"
4528 "}} // namespace B::C\n"
4529 "} // namespace A",
4530 Style);
4531
4532 Style.ColumnLimit = 40;
4533 verifyFormat("namespace aaaaaaaaaa {\n"
4534 "namespace bbbbbbbbbb {\n"
4535 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4536 "namespace aaaaaaaaaa {\n"
4537 "namespace bbbbbbbbbb {\n"
4538 "} // namespace bbbbbbbbbb\n"
4539 "} // namespace aaaaaaaaaa",
4540 Style);
4541
4542 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4543 "namespace cccccc {\n"
4544 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4545 "namespace aaaaaa {\n"
4546 "namespace bbbbbb {\n"
4547 "namespace cccccc {\n"
4548 "} // namespace cccccc\n"
4549 "} // namespace bbbbbb\n"
4550 "} // namespace aaaaaa",
4551 Style);
4552
4553 verifyFormat("namespace a { namespace b {\n"
4554 "namespace c {\n"
4555 "}}} // namespace a::b::c",
4556 Style);
4557
4558 Style.ColumnLimit = 80;
4559
4560 // Extra semicolon after 'inner' closing brace prevents merging
4561 verifyFormat("namespace out { namespace in {\n"
4562 "}; } // namespace out::in",
4563 "namespace out {\n"
4564 "namespace in {\n"
4565 "}; // namespace in\n"
4566 "} // namespace out",
4567 Style);
4568
4569 // Extra semicolon after 'outer' closing brace is conserved
4570 verifyFormat("namespace out { namespace in {\n"
4571 "}}; // namespace out::in",
4572 "namespace out {\n"
4573 "namespace in {\n"
4574 "} // namespace in\n"
4575 "}; // namespace out",
4576 Style);
4577
4578 Style.NamespaceIndentation = FormatStyle::NI_All;
4579 verifyFormat("namespace out { namespace in {\n"
4580 " int i;\n"
4581 "}} // namespace out::in",
4582 "namespace out {\n"
4583 "namespace in {\n"
4584 "int i;\n"
4585 "} // namespace in\n"
4586 "} // namespace out",
4587 Style);
4588 verifyFormat("namespace out { namespace mid {\n"
4589 " namespace in {\n"
4590 " int j;\n"
4591 " } // namespace in\n"
4592 " int k;\n"
4593 "}} // namespace out::mid",
4594 "namespace out { namespace mid {\n"
4595 "namespace in { int j; } // namespace in\n"
4596 "int k; }} // namespace out::mid",
4597 Style);
4598
4599 verifyFormat("namespace A { namespace B { namespace C {\n"
4600 " int i;\n"
4601 "}}} // namespace A::B::C\n"
4602 "int main() {\n"
4603 " if (true)\n"
4604 " return 0;\n"
4605 "}",
4606 "namespace A { namespace B {\n"
4607 "namespace C {\n"
4608 " int i;\n"
4609 "}} // namespace B::C\n"
4610 "} // namespace A\n"
4611 "int main() {\n"
4612 " if (true)\n"
4613 " return 0;\n"
4614 "}",
4615 Style);
4616
4617 verifyFormat("namespace A { namespace B { namespace C {\n"
4618 "#ifdef FOO\n"
4619 " int i;\n"
4620 "#endif\n"
4621 "}}} // namespace A::B::C\n"
4622 "int main() {\n"
4623 " if (true)\n"
4624 " return 0;\n"
4625 "}",
4626 "namespace A { namespace B {\n"
4627 "namespace C {\n"
4628 "#ifdef FOO\n"
4629 " int i;\n"
4630 "#endif\n"
4631 "}} // namespace B::C\n"
4632 "} // namespace A\n"
4633 "int main() {\n"
4634 " if (true)\n"
4635 " return 0;\n"
4636 "}",
4637 Style);
4638
4639 Style.NamespaceIndentation = FormatStyle::NI_Inner;
4640 verifyFormat("namespace out { namespace in {\n"
4641 " int i;\n"
4642 "}} // namespace out::in",
4643 "namespace out {\n"
4644 "namespace in {\n"
4645 "int i;\n"
4646 "} // namespace in\n"
4647 "} // namespace out",
4648 Style);
4649 verifyFormat("namespace out { namespace mid { namespace in {\n"
4650 " int i;\n"
4651 "}}} // namespace out::mid::in",
4652 "namespace out {\n"
4653 "namespace mid {\n"
4654 "namespace in {\n"
4655 "int i;\n"
4656 "} // namespace in\n"
4657 "} // namespace mid\n"
4658 "} // namespace out",
4659 Style);
4660
4661 Style.CompactNamespaces = true;
4662 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
4663 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4664 Style.BraceWrapping.BeforeLambdaBody = true;
4665 verifyFormat("namespace out { namespace in {\n"
4666 "}} // namespace out::in",
4667 Style);
4668 verifyFormat("namespace out { namespace in {\n"
4669 "}} // namespace out::in",
4670 "namespace out {\n"
4671 "namespace in {\n"
4672 "} // namespace in\n"
4673 "} // namespace out",
4674 Style);
4675}
4676
4677TEST_F(FormatTest, FormatsExternC) {
4678 verifyFormat("extern \"C\" {\nint a;");
4679 verifyFormat("extern \"C\" {}");
4680 verifyFormat("extern \"C\" {\n"
4681 "int foo();\n"
4682 "}");
4683 verifyFormat("extern \"C\" int foo() {}");
4684 verifyFormat("extern \"C\" int foo();");
4685 verifyFormat("extern \"C\" int foo() {\n"
4686 " int i = 42;\n"
4687 " return i;\n"
4688 "}");
4689 verifyFormat(
4690 "extern \"C\" char const *const\n"
4691 " OpenCL_source_OpenCLRunTime_test_attribute_opencl_unroll_hint;");
4692
4693 FormatStyle Style = getLLVMStyle();
4694 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4695 Style.BraceWrapping.AfterFunction = true;
4696 verifyFormat("extern \"C\" int foo() {}", Style);
4697 verifyFormat("extern \"C\" int foo();", Style);
4698 verifyFormat("extern \"C\" int foo()\n"
4699 "{\n"
4700 " int i = 42;\n"
4701 " return i;\n"
4702 "}",
4703 Style);
4704
4705 Style.BraceWrapping.AfterExternBlock = true;
4706 Style.BraceWrapping.SplitEmptyRecord = false;
4707 verifyFormat("extern \"C\"\n"
4708 "{}",
4709 Style);
4710 verifyFormat("extern \"C\"\n"
4711 "{\n"
4712 " int foo();\n"
4713 "}",
4714 Style);
4715}
4716
4717TEST_F(FormatTest, IndentExternBlockStyle) {
4718 FormatStyle Style = getLLVMStyle();
4719 Style.IndentWidth = 2;
4720
4721 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4722 verifyFormat("extern \"C\" { /*9*/\n"
4723 "}",
4724 Style);
4725 verifyFormat("extern \"C\" {\n"
4726 " int foo10();\n"
4727 "}",
4728 Style);
4729
4730 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4731 verifyFormat("extern \"C\" { /*11*/\n"
4732 "}",
4733 Style);
4734 verifyFormat("extern \"C\" {\n"
4735 "int foo12();\n"
4736 "}",
4737 Style);
4738
4739 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
4740 verifyFormat("extern \"C\"\n"
4741 "{\n"
4742 "int i;\n"
4743 "}",
4744 Style);
4745
4746 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4747 Style.BraceWrapping.AfterExternBlock = true;
4748 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4749 verifyFormat("extern \"C\"\n"
4750 "{ /*13*/\n"
4751 "}",
4752 Style);
4753 verifyFormat("extern \"C\"\n{\n"
4754 " int foo14();\n"
4755 "}",
4756 Style);
4757
4758 Style.BraceWrapping.AfterExternBlock = false;
4759 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent;
4760 verifyFormat("extern \"C\" { /*15*/\n"
4761 "}",
4762 Style);
4763 verifyFormat("extern \"C\" {\n"
4764 "int foo16();\n"
4765 "}",
4766 Style);
4767
4768 Style.BraceWrapping.AfterExternBlock = true;
4769 verifyFormat("extern \"C\"\n"
4770 "{ /*13*/\n"
4771 "}",
4772 Style);
4773 verifyFormat("extern \"C\"\n"
4774 "{\n"
4775 "int foo14();\n"
4776 "}",
4777 Style);
4778
4779 Style.IndentExternBlock = FormatStyle::IEBS_Indent;
4780 verifyFormat("extern \"C\"\n"
4781 "{ /*13*/\n"
4782 "}",
4783 Style);
4784 verifyFormat("extern \"C\"\n"
4785 "{\n"
4786 " int foo14();\n"
4787 "}",
4788 Style);
4789}
4790
4791TEST_F(FormatTest, FormatsInlineASM) {
4792 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4793 verifyFormat("asm(\"nop\" ::: \"memory\");");
4794 verifyFormat(
4795 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4796 " \"cpuid\\n\\t\"\n"
4797 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4798 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4799 " : \"a\"(value));");
4800 verifyFormat(
4801 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4802 " __asm {\n"
4803 " mov edx,[that] // vtable in edx\n"
4804 " mov eax,methodIndex\n"
4805 " call [edx][eax*4] // stdcall\n"
4806 " }\n"
4807 "}",
4808 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4809 " __asm {\n"
4810 " mov edx,[that] // vtable in edx\n"
4811 " mov eax,methodIndex\n"
4812 " call [edx][eax*4] // stdcall\n"
4813 " }\n"
4814 "}");
4815 verifyNoChange("_asm {\n"
4816 " xor eax, eax;\n"
4817 " cpuid;\n"
4818 "}");
4819 verifyFormat("void function() {\n"
4820 " // comment\n"
4821 " asm(\"\");\n"
4822 "}");
4823 verifyFormat("__asm {\n"
4824 "}\n"
4825 "int i;",
4826 "__asm {\n"
4827 "}\n"
4828 "int i;");
4829
4830 auto Style = getLLVMStyleWithColumns(ColumnLimit: 0);
4831 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4832 const StringRef Code2{"asm(\"xyz\"\n"
4833 " : \"=a\"(a), \"=d\"(b)\n"
4834 " : \"a\"(data));"};
4835 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4836 " : \"a\"(data));"};
4837
4838 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
4839 verifyFormat(Code1, Style);
4840 verifyNoChange(Code2, Style);
4841 verifyNoChange(Code3, Style);
4842
4843 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
4844 verifyFormat(Code2, Code1, Style);
4845 verifyNoChange(Code2, Style);
4846 verifyFormat(Code2, Code3, Style);
4847}
4848
4849TEST_F(FormatTest, FormatTryCatch) {
4850 verifyFormat("try {\n"
4851 " throw a * b;\n"
4852 "} catch (int a) {\n"
4853 " // Do nothing.\n"
4854 "} catch (...) {\n"
4855 " exit(42);\n"
4856 "}");
4857
4858 // Function-level try statements.
4859 verifyFormat("int f() try { return 4; } catch (...) {\n"
4860 " return 5;\n"
4861 "}");
4862 verifyFormat("class A {\n"
4863 " int a;\n"
4864 " A() try : a(0) {\n"
4865 " } catch (...) {\n"
4866 " throw;\n"
4867 " }\n"
4868 "};");
4869 verifyFormat("class A {\n"
4870 " int a;\n"
4871 " A() try : a(0), b{1} {\n"
4872 " } catch (...) {\n"
4873 " throw;\n"
4874 " }\n"
4875 "};");
4876 verifyFormat("class A {\n"
4877 " int a;\n"
4878 " A() try : a(0), b{1}, c{2} {\n"
4879 " } catch (...) {\n"
4880 " throw;\n"
4881 " }\n"
4882 "};");
4883 verifyFormat("class A {\n"
4884 " int a;\n"
4885 " A() try : a(0), b{1}, c{2} {\n"
4886 " { // New scope.\n"
4887 " }\n"
4888 " } catch (...) {\n"
4889 " throw;\n"
4890 " }\n"
4891 "};");
4892
4893 // Incomplete try-catch blocks.
4894 verifyIncompleteFormat("try {} catch (");
4895}
4896
4897TEST_F(FormatTest, FormatTryAsAVariable) {
4898 verifyFormat("int try;");
4899 verifyFormat("int try, size;");
4900 verifyFormat("try = foo();");
4901 verifyFormat("if (try < size) {\n return true;\n}");
4902
4903 verifyFormat("int catch;");
4904 verifyFormat("int catch, size;");
4905 verifyFormat("catch = foo();");
4906 verifyFormat("if (catch < size) {\n return true;\n}");
4907
4908 FormatStyle Style = getLLVMStyle();
4909 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
4910 Style.BraceWrapping.AfterFunction = true;
4911 Style.BraceWrapping.BeforeCatch = true;
4912 verifyFormat("try {\n"
4913 " int bar = 1;\n"
4914 "}\n"
4915 "catch (...) {\n"
4916 " int bar = 1;\n"
4917 "}",
4918 Style);
4919 verifyFormat("#if NO_EX\n"
4920 "try\n"
4921 "#endif\n"
4922 "{\n"
4923 "}\n"
4924 "#if NO_EX\n"
4925 "catch (...) {\n"
4926 "}",
4927 Style);
4928 verifyFormat("try /* abc */ {\n"
4929 " int bar = 1;\n"
4930 "}\n"
4931 "catch (...) {\n"
4932 " int bar = 1;\n"
4933 "}",
4934 Style);
4935 verifyFormat("try\n"
4936 "// abc\n"
4937 "{\n"
4938 " int bar = 1;\n"
4939 "}\n"
4940 "catch (...) {\n"
4941 " int bar = 1;\n"
4942 "}",
4943 Style);
4944}
4945
4946TEST_F(FormatTest, FormatSEHTryCatch) {
4947 verifyFormat("__try {\n"
4948 " int a = b * c;\n"
4949 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4950 " // Do nothing.\n"
4951 "}");
4952
4953 verifyFormat("__try {\n"
4954 " int a = b * c;\n"
4955 "} __finally {\n"
4956 " // Do nothing.\n"
4957 "}");
4958
4959 verifyFormat("DEBUG({\n"
4960 " __try {\n"
4961 " } __finally {\n"
4962 " }\n"
4963 "});");
4964}
4965
4966TEST_F(FormatTest, IncompleteTryCatchBlocks) {
4967 verifyFormat("try {\n"
4968 " f();\n"
4969 "} catch {\n"
4970 " g();\n"
4971 "}");
4972 verifyFormat("try {\n"
4973 " f();\n"
4974 "} catch (A a) MACRO(x) {\n"
4975 " g();\n"
4976 "} catch (B b) MACRO(x) {\n"
4977 " g();\n"
4978 "}");
4979}
4980
4981TEST_F(FormatTest, FormatTryCatchBraceStyles) {
4982 FormatStyle Style = getLLVMStyle();
4983 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
4984 FormatStyle::BS_WebKit}) {
4985 Style.BreakBeforeBraces = BraceStyle;
4986 verifyFormat("try {\n"
4987 " // something\n"
4988 "} catch (...) {\n"
4989 " // something\n"
4990 "}",
4991 Style);
4992 }
4993 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4994 verifyFormat("try {\n"
4995 " // something\n"
4996 "}\n"
4997 "catch (...) {\n"
4998 " // something\n"
4999 "}",
5000 Style);
5001 verifyFormat("__try {\n"
5002 " // something\n"
5003 "}\n"
5004 "__finally {\n"
5005 " // something\n"
5006 "}",
5007 Style);
5008 verifyFormat("@try {\n"
5009 " // something\n"
5010 "}\n"
5011 "@finally {\n"
5012 " // something\n"
5013 "}",
5014 Style);
5015 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
5016 verifyFormat("try\n"
5017 "{\n"
5018 " // something\n"
5019 "}\n"
5020 "catch (...)\n"
5021 "{\n"
5022 " // something\n"
5023 "}",
5024 Style);
5025 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
5026 verifyFormat("try\n"
5027 " {\n"
5028 " // something white\n"
5029 " }\n"
5030 "catch (...)\n"
5031 " {\n"
5032 " // something white\n"
5033 " }",
5034 Style);
5035 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
5036 verifyFormat("try\n"
5037 " {\n"
5038 " // something\n"
5039 " }\n"
5040 "catch (...)\n"
5041 " {\n"
5042 " // something\n"
5043 " }",
5044 Style);
5045 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
5046 Style.BraceWrapping.BeforeCatch = true;
5047 verifyFormat("try {\n"
5048 " // something\n"
5049 "}\n"
5050 "catch (...) {\n"
5051 " // something\n"
5052 "}",
5053 Style);
5054}
5055
5056TEST_F(FormatTest, StaticInitializers) {
5057 verifyFormat("static SomeClass SC = {1, 'a'};");
5058
5059 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
5060 " 100000000, "
5061 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
5062
5063 // Here, everything other than the "}" would fit on a line.
5064 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
5065 " 10000000000000000000000000};");
5066 verifyFormat("S s = {a,\n"
5067 "\n"
5068 " b};",
5069 "S s = {\n"
5070 " a,\n"
5071 "\n"
5072 " b\n"
5073 "};");
5074
5075 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5076 // line. However, the formatting looks a bit off and this probably doesn't
5077 // happen often in practice.
5078 verifyFormat("static int Variable[1] = {\n"
5079 " {1000000000000000000000000000000000000}};",
5080 getLLVMStyleWithColumns(40));
5081}
5082
5083TEST_F(FormatTest, DesignatedInitializers) {
5084 verifyFormat("const struct A a = {.a = 1, .b = 2};");
5085 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5086 " .bbbbbbbbbb = 2,\n"
5087 " .cccccccccc = 3,\n"
5088 " .dddddddddd = 4,\n"
5089 " .eeeeeeeeee = 5};");
5090 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5091 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5092 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5093 " .ccccccccccccccccccccccccccc = 3,\n"
5094 " .ddddddddddddddddddddddddddd = 4,\n"
5095 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5096
5097 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5098
5099 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5100 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5101 " [2] = bbbbbbbbbb,\n"
5102 " [3] = cccccccccc,\n"
5103 " [4] = dddddddddd,\n"
5104 " [5] = eeeeeeeeee};");
5105 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5106 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5107 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5108 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5109 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5110 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5111
5112 verifyFormat("for (const TestCase &test_case : {\n"
5113 " TestCase{\n"
5114 " .a = 1,\n"
5115 " .b = 1,\n"
5116 " },\n"
5117 " TestCase{\n"
5118 " .a = 2,\n"
5119 " .b = 2,\n"
5120 " },\n"
5121 " }) {\n"
5122 "}");
5123}
5124
5125TEST_F(FormatTest, BracedInitializerIndentWidth) {
5126 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
5127 Style.BinPackArguments = true;
5128 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5129 Style.BracedInitializerIndentWidth = 6;
5130
5131 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5132 verifyFormat("enum class {\n"
5133 " One,\n"
5134 " Two,\n"
5135 "};",
5136 Style);
5137 verifyFormat("class Foo {\n"
5138 " Foo() {}\n"
5139 " void bar();\n"
5140 "};",
5141 Style);
5142 verifyFormat("void foo() {\n"
5143 " auto bar = baz;\n"
5144 " return baz;\n"
5145 "};",
5146 Style);
5147 verifyFormat("auto foo = [&] {\n"
5148 " auto bar = baz;\n"
5149 " return baz;\n"
5150 "};",
5151 Style);
5152 verifyFormat("{\n"
5153 " auto bar = baz;\n"
5154 " return baz;\n"
5155 "};",
5156 Style);
5157 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5158 verifyFormat("SomeClass clazz(\n"
5159 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5160 " \"zzzzzzzzzzzzzzzzzz\");",
5161 Style);
5162
5163 // The following types of initialization are all affected by
5164 // BracedInitializerIndentWidth. Aggregate initialization.
5165 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5166 " 10000000, 20000000};",
5167 Style);
5168 verifyFormat("SomeStruct s{\n"
5169 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5170 " \"zzzzzzzzzzzzzzzz\"};",
5171 Style);
5172 // Designated initializers.
5173 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5174 " [0] = 10000000, [1] = 20000000};",
5175 Style);
5176 verifyFormat("SomeStruct s{\n"
5177 " .foo = \"xxxxxxxxxxxxx\",\n"
5178 " .bar = \"yyyyyyyyyyyyy\",\n"
5179 " .baz = \"zzzzzzzzzzzzz\"};",
5180 Style);
5181 // List initialization.
5182 verifyFormat("SomeStruct s{\n"
5183 " \"xxxxxxxxxxxxx\",\n"
5184 " \"yyyyyyyyyyyyy\",\n"
5185 " \"zzzzzzzzzzzzz\",\n"
5186 "};",
5187 Style);
5188 verifyFormat("SomeStruct{\n"
5189 " \"xxxxxxxxxxxxx\",\n"
5190 " \"yyyyyyyyyyyyy\",\n"
5191 " \"zzzzzzzzzzzzz\",\n"
5192 "};",
5193 Style);
5194 verifyFormat("new SomeStruct{\n"
5195 " \"xxxxxxxxxxxxx\",\n"
5196 " \"yyyyyyyyyyyyy\",\n"
5197 " \"zzzzzzzzzzzzz\",\n"
5198 "};",
5199 Style);
5200 // Member initializer.
5201 verifyFormat("class SomeClass {\n"
5202 " SomeStruct s{\n"
5203 " \"xxxxxxxxxxxxx\",\n"
5204 " \"yyyyyyyyyyyyy\",\n"
5205 " \"zzzzzzzzzzzzz\",\n"
5206 " };\n"
5207 "};",
5208 Style);
5209 // Constructor member initializer.
5210 verifyFormat("SomeClass::SomeClass : strct{\n"
5211 " \"xxxxxxxxxxxxx\",\n"
5212 " \"yyyyyyyyyyyyy\",\n"
5213 " \"zzzzzzzzzzzzz\",\n"
5214 " } {}",
5215 Style);
5216 // Copy initialization.
5217 verifyFormat("SomeStruct s = SomeStruct{\n"
5218 " \"xxxxxxxxxxxxx\",\n"
5219 " \"yyyyyyyyyyyyy\",\n"
5220 " \"zzzzzzzzzzzzz\",\n"
5221 "};",
5222 Style);
5223 // Copy list initialization.
5224 verifyFormat("SomeStruct s = {\n"
5225 " \"xxxxxxxxxxxxx\",\n"
5226 " \"yyyyyyyyyyyyy\",\n"
5227 " \"zzzzzzzzzzzzz\",\n"
5228 "};",
5229 Style);
5230 // Assignment operand initialization.
5231 verifyFormat("s = {\n"
5232 " \"xxxxxxxxxxxxx\",\n"
5233 " \"yyyyyyyyyyyyy\",\n"
5234 " \"zzzzzzzzzzzzz\",\n"
5235 "};",
5236 Style);
5237 // Returned object initialization.
5238 verifyFormat("return {\n"
5239 " \"xxxxxxxxxxxxx\",\n"
5240 " \"yyyyyyyyyyyyy\",\n"
5241 " \"zzzzzzzzzzzzz\",\n"
5242 "};",
5243 Style);
5244 // Initializer list.
5245 verifyFormat("auto initializerList = {\n"
5246 " \"xxxxxxxxxxxxx\",\n"
5247 " \"yyyyyyyyyyyyy\",\n"
5248 " \"zzzzzzzzzzzzz\",\n"
5249 "};",
5250 Style);
5251 // Function parameter initialization.
5252 verifyFormat("func({\n"
5253 " \"xxxxxxxxxxxxx\",\n"
5254 " \"yyyyyyyyyyyyy\",\n"
5255 " \"zzzzzzzzzzzzz\",\n"
5256 "});",
5257 Style);
5258 // Nested init lists.
5259 verifyFormat("SomeStruct s = {\n"
5260 " {{init1, init2, init3, init4, init5},\n"
5261 " {init1, init2, init3, init4, init5}}};",
5262 Style);
5263 verifyFormat("SomeStruct s = {\n"
5264 " {{\n"
5265 " .init1 = 1,\n"
5266 " .init2 = 2,\n"
5267 " .init3 = 3,\n"
5268 " .init4 = 4,\n"
5269 " .init5 = 5,\n"
5270 " },\n"
5271 " {init1, init2, init3, init4, init5}}};",
5272 Style);
5273 verifyFormat("SomeArrayT a[3] = {\n"
5274 " {\n"
5275 " foo,\n"
5276 " bar,\n"
5277 " },\n"
5278 " {\n"
5279 " foo,\n"
5280 " bar,\n"
5281 " },\n"
5282 " SomeArrayT{},\n"
5283 "};",
5284 Style);
5285 verifyFormat("SomeArrayT a[3] = {\n"
5286 " {foo},\n"
5287 " {\n"
5288 " {\n"
5289 " init1,\n"
5290 " init2,\n"
5291 " init3,\n"
5292 " },\n"
5293 " {\n"
5294 " init1,\n"
5295 " init2,\n"
5296 " init3,\n"
5297 " },\n"
5298 " },\n"
5299 " {baz},\n"
5300 "};",
5301 Style);
5302
5303 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5304 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
5305 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5306 " \"zzzzzzzzzzzzz\"};",
5307 Style);
5308}
5309
5310TEST_F(FormatTest, NestedStaticInitializers) {
5311 verifyFormat("static A x = {{{}}};");
5312 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5313 " {init1, init2, init3, init4}}};",
5314 getLLVMStyleWithColumns(50));
5315
5316 verifyFormat("somes Status::global_reps[3] = {\n"
5317 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5318 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5319 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5320 getLLVMStyleWithColumns(60));
5321 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5322 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5323 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5324 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5325 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5326 " {rect.fRight - rect.fLeft, rect.fBottom - "
5327 "rect.fTop}};");
5328
5329 verifyFormat(
5330 "SomeArrayOfSomeType a = {\n"
5331 " {{1, 2, 3},\n"
5332 " {1, 2, 3},\n"
5333 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5334 " 333333333333333333333333333333},\n"
5335 " {1, 2, 3},\n"
5336 " {1, 2, 3}}};");
5337 verifyFormat(
5338 "SomeArrayOfSomeType a = {\n"
5339 " {{1, 2, 3}},\n"
5340 " {{1, 2, 3}},\n"
5341 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5342 " 333333333333333333333333333333}},\n"
5343 " {{1, 2, 3}},\n"
5344 " {{1, 2, 3}}};");
5345
5346 verifyFormat("struct {\n"
5347 " unsigned bit;\n"
5348 " const char *const name;\n"
5349 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5350 " {kOsWin, \"Windows\"},\n"
5351 " {kOsLinux, \"Linux\"},\n"
5352 " {kOsCrOS, \"Chrome OS\"}};");
5353 verifyFormat("struct {\n"
5354 " unsigned bit;\n"
5355 " const char *const name;\n"
5356 "} kBitsToOs[] = {\n"
5357 " {kOsMac, \"Mac\"},\n"
5358 " {kOsWin, \"Windows\"},\n"
5359 " {kOsLinux, \"Linux\"},\n"
5360 " {kOsCrOS, \"Chrome OS\"},\n"
5361 "};");
5362}
5363
5364TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
5365 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5366 " \\\n"
5367 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5368}
5369
5370TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
5371 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5372 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5373
5374 // Do break defaulted and deleted functions.
5375 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5376 " default;",
5377 getLLVMStyleWithColumns(40));
5378 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5379 " delete;",
5380 getLLVMStyleWithColumns(40));
5381}
5382
5383TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
5384 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5385 getLLVMStyleWithColumns(40));
5386 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5387 getLLVMStyleWithColumns(40));
5388 verifyFormat("#define Q \\\n"
5389 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5390 " \"aaaaaaaa.cpp\"",
5391 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5392 getLLVMStyleWithColumns(40));
5393}
5394
5395TEST_F(FormatTest, UnderstandsLinePPDirective) {
5396 verifyFormat("# 123 \"A string literal\"",
5397 " # 123 \"A string literal\"");
5398}
5399
5400TEST_F(FormatTest, LayoutUnknownPPDirective) {
5401 verifyFormat("#;");
5402 verifyFormat("#\n;\n;\n;");
5403}
5404
5405TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
5406 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5407 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5408 getLLVMStyleWithColumns(12));
5409}
5410
5411TEST_F(FormatTest, EndOfFileEndsPPDirective) {
5412 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5413 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5414}
5415
5416TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
5417 verifyFormat("#define A \\x20");
5418 verifyFormat("#define A \\ x20");
5419 verifyFormat("#define A \\ x20", "#define A \\ x20");
5420 verifyFormat("#define A ''");
5421 verifyFormat("#define A ''qqq");
5422 verifyFormat("#define A `qqq");
5423 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5424 verifyFormat("const char *c = STRINGIFY(\n"
5425 "\\na : b);",
5426 "const char * c = STRINGIFY(\n"
5427 "\\na : b);");
5428
5429 verifyFormat("a\r\\");
5430 verifyFormat("a\v\\");
5431 verifyFormat("a\f\\");
5432}
5433
5434TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) {
5435 FormatStyle style = getChromiumStyle(Language: FormatStyle::LK_Cpp);
5436 style.IndentWidth = 4;
5437 style.PPIndentWidth = 1;
5438
5439 style.IndentPPDirectives = FormatStyle::PPDIS_None;
5440 verifyFormat("#ifdef __linux__\n"
5441 "void foo() {\n"
5442 " int x = 0;\n"
5443 "}\n"
5444 "#define FOO\n"
5445 "#endif\n"
5446 "void bar() {\n"
5447 " int y = 0;\n"
5448 "}",
5449 style);
5450
5451 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5452 verifyFormat("#ifdef __linux__\n"
5453 "void foo() {\n"
5454 " int x = 0;\n"
5455 "}\n"
5456 "# define FOO foo\n"
5457 "#endif\n"
5458 "void bar() {\n"
5459 " int y = 0;\n"
5460 "}",
5461 style);
5462
5463 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5464 verifyFormat("#ifdef __linux__\n"
5465 "void foo() {\n"
5466 " int x = 0;\n"
5467 "}\n"
5468 " #define FOO foo\n"
5469 "#endif\n"
5470 "void bar() {\n"
5471 " int y = 0;\n"
5472 "}",
5473 style);
5474 verifyFormat("#if 1\n"
5475 " // some comments\n"
5476 " // another\n"
5477 " #define foo 1\n"
5478 "// not a define comment\n"
5479 "void bar() {\n"
5480 " // comment\n"
5481 " int y = 0;\n"
5482 "}",
5483 "#if 1\n"
5484 "// some comments\n"
5485 "// another\n"
5486 "#define foo 1\n"
5487 "// not a define comment\n"
5488 "void bar() {\n"
5489 " // comment\n"
5490 " int y = 0;\n"
5491 "}",
5492 style);
5493
5494 style.IndentPPDirectives = FormatStyle::PPDIS_None;
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 verifyFormat("if (emacs) {\n"
5504 "#ifdef is\n"
5505 "#define lit \\\n"
5506 " if (af) { \\\n"
5507 " return duh(); \\\n"
5508 " }\n"
5509 "#endif\n"
5510 "}",
5511 style);
5512 verifyFormat("#if abc\n"
5513 "#ifdef foo\n"
5514 "#define bar() \\\n"
5515 " if (A) { \\\n"
5516 " if (B) { \\\n"
5517 " C(); \\\n"
5518 " } \\\n"
5519 " } \\\n"
5520 " D();\n"
5521 "#endif\n"
5522 "#endif",
5523 style);
5524 verifyFormat("#ifndef foo\n"
5525 "#define foo\n"
5526 "if (emacs) {\n"
5527 "#ifdef is\n"
5528 "#define lit \\\n"
5529 " if (af) { \\\n"
5530 " return duh(); \\\n"
5531 " }\n"
5532 "#endif\n"
5533 "}\n"
5534 "#endif",
5535 style);
5536 verifyFormat("#if 1\n"
5537 "#define X \\\n"
5538 " { \\\n"
5539 " x; \\\n"
5540 " x; \\\n"
5541 " }\n"
5542 "#endif",
5543 style);
5544 verifyFormat("#define X \\\n"
5545 " { \\\n"
5546 " x; \\\n"
5547 " x; \\\n"
5548 " }",
5549 style);
5550
5551 style.PPIndentWidth = 2;
5552 verifyFormat("#ifdef foo\n"
5553 "#define bar() \\\n"
5554 " if (A) { \\\n"
5555 " B(); \\\n"
5556 " } \\\n"
5557 " C();\n"
5558 "#endif",
5559 style);
5560 style.IndentWidth = 8;
5561 verifyFormat("#ifdef foo\n"
5562 "#define bar() \\\n"
5563 " if (A) { \\\n"
5564 " B(); \\\n"
5565 " } \\\n"
5566 " C();\n"
5567 "#endif",
5568 style);
5569
5570 style.IndentWidth = 1;
5571 style.PPIndentWidth = 4;
5572 verifyFormat("#if 1\n"
5573 "#define X \\\n"
5574 " { \\\n"
5575 " x; \\\n"
5576 " x; \\\n"
5577 " }\n"
5578 "#endif",
5579 style);
5580 verifyFormat("#define X \\\n"
5581 " { \\\n"
5582 " x; \\\n"
5583 " x; \\\n"
5584 " }",
5585 style);
5586
5587 style.IndentWidth = 4;
5588 style.PPIndentWidth = 1;
5589 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
5590 verifyFormat("#ifdef foo\n"
5591 "# define bar() \\\n"
5592 " if (A) { \\\n"
5593 " B(); \\\n"
5594 " } \\\n"
5595 " C();\n"
5596 "#endif",
5597 style);
5598 verifyFormat("#if abc\n"
5599 "# ifdef foo\n"
5600 "# define bar() \\\n"
5601 " if (A) { \\\n"
5602 " if (B) { \\\n"
5603 " C(); \\\n"
5604 " } \\\n"
5605 " } \\\n"
5606 " D();\n"
5607 "# endif\n"
5608 "#endif",
5609 style);
5610 verifyFormat("#ifndef foo\n"
5611 "#define foo\n"
5612 "if (emacs) {\n"
5613 "#ifdef is\n"
5614 "# define lit \\\n"
5615 " if (af) { \\\n"
5616 " return duh(); \\\n"
5617 " }\n"
5618 "#endif\n"
5619 "}\n"
5620 "#endif",
5621 style);
5622 verifyFormat("#define X \\\n"
5623 " { \\\n"
5624 " x; \\\n"
5625 " x; \\\n"
5626 " }",
5627 style);
5628
5629 style.PPIndentWidth = 2;
5630 style.IndentWidth = 8;
5631 verifyFormat("#ifdef foo\n"
5632 "# define bar() \\\n"
5633 " if (A) { \\\n"
5634 " B(); \\\n"
5635 " } \\\n"
5636 " C();\n"
5637 "#endif",
5638 style);
5639
5640 style.PPIndentWidth = 4;
5641 style.IndentWidth = 1;
5642 verifyFormat("#define X \\\n"
5643 " { \\\n"
5644 " x; \\\n"
5645 " x; \\\n"
5646 " }",
5647 style);
5648
5649 style.IndentWidth = 4;
5650 style.PPIndentWidth = 1;
5651 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
5652 verifyFormat("if (emacs) {\n"
5653 "#ifdef is\n"
5654 " #define lit \\\n"
5655 " if (af) { \\\n"
5656 " return duh(); \\\n"
5657 " }\n"
5658 "#endif\n"
5659 "}",
5660 style);
5661 verifyFormat("#if abc\n"
5662 " #ifdef foo\n"
5663 " #define bar() \\\n"
5664 " if (A) { \\\n"
5665 " B(); \\\n"
5666 " } \\\n"
5667 " C();\n"
5668 " #endif\n"
5669 "#endif",
5670 style);
5671 verifyFormat("#if 1\n"
5672 " #define X \\\n"
5673 " { \\\n"
5674 " x; \\\n"
5675 " x; \\\n"
5676 " }\n"
5677 "#endif",
5678 style);
5679
5680 style.PPIndentWidth = 2;
5681 verifyFormat("#ifdef foo\n"
5682 " #define bar() \\\n"
5683 " if (A) { \\\n"
5684 " B(); \\\n"
5685 " } \\\n"
5686 " C();\n"
5687 "#endif",
5688 style);
5689
5690 style.PPIndentWidth = 4;
5691 style.IndentWidth = 1;
5692 verifyFormat("#if 1\n"
5693 " #define X \\\n"
5694 " { \\\n"
5695 " x; \\\n"
5696 " x; \\\n"
5697 " }\n"
5698 "#endif",
5699 style);
5700}
5701
5702TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
5703 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5704 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5705 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5706 // FIXME: We never break before the macro name.
5707 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5708
5709 verifyFormat("#define A A\n#define A A");
5710 verifyFormat("#define A(X) A\n#define A A");
5711
5712 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5713 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5714}
5715
5716TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
5717 verifyFormat("// somecomment\n"
5718 "#include \"a.h\"\n"
5719 "#define A( \\\n"
5720 " A, B)\n"
5721 "#include \"b.h\"\n"
5722 "// somecomment",
5723 " // somecomment\n"
5724 " #include \"a.h\"\n"
5725 "#define A(A,\\\n"
5726 " B)\n"
5727 " #include \"b.h\"\n"
5728 " // somecomment",
5729 getLLVMStyleWithColumns(13));
5730}
5731
5732TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }
5733
5734TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
5735 verifyFormat("#define A \\\n"
5736 " c; \\\n"
5737 " e;\n"
5738 "f;",
5739 "#define A c; e;\n"
5740 "f;",
5741 getLLVMStyleWithColumns(14));
5742}
5743
5744TEST_F(FormatTest, LayoutRemainingTokens) {
5745 verifyFormat("{\n"
5746 "}");
5747}
5748
5749TEST_F(FormatTest, MacroDefinitionInsideStatement) {
5750 verifyFormat("int x,\n"
5751 "#define A\n"
5752 " y;",
5753 "int x,\n#define A\ny;");
5754}
5755
5756TEST_F(FormatTest, HashInMacroDefinition) {
5757 verifyFormat("#define A(c) L#c");
5758 verifyFormat("#define A(c) u#c");
5759 verifyFormat("#define A(c) U#c");
5760 verifyFormat("#define A(c) u8#c");
5761 verifyFormat("#define A(c) LR#c");
5762 verifyFormat("#define A(c) uR#c");
5763 verifyFormat("#define A(c) UR#c");
5764 verifyFormat("#define A(c) u8R#c");
5765 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5766 verifyFormat("#define A \\\n"
5767 " { \\\n"
5768 " f(#c); \\\n"
5769 " }",
5770 getLLVMStyleWithColumns(11));
5771
5772 verifyFormat("#define A(X) \\\n"
5773 " void function##X()",
5774 getLLVMStyleWithColumns(22));
5775
5776 verifyFormat("#define A(a, b, c) \\\n"
5777 " void a##b##c()",
5778 getLLVMStyleWithColumns(22));
5779
5780 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5781
5782 verifyFormat("{\n"
5783 " {\n"
5784 "#define GEN_ID(_x) char *_x{#_x}\n"
5785 " GEN_ID(one);\n"
5786 " }\n"
5787 "}");
5788}
5789
5790TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
5791 verifyFormat("#define A (x)");
5792 verifyFormat("#define A(x)");
5793
5794 FormatStyle Style = getLLVMStyle();
5795 Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
5796 verifyFormat("#define true ((foo)1)", Style);
5797 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
5798 verifyFormat("#define false((foo)0)", Style);
5799}
5800
5801TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
5802 verifyFormat("#define A b;",
5803 "#define A \\\n"
5804 " \\\n"
5805 " b;",
5806 getLLVMStyleWithColumns(25));
5807 verifyNoChange("#define A \\\n"
5808 " \\\n"
5809 " a; \\\n"
5810 " b;",
5811 getLLVMStyleWithColumns(11));
5812 verifyNoChange("#define A \\\n"
5813 " a; \\\n"
5814 " \\\n"
5815 " b;",
5816 getLLVMStyleWithColumns(11));
5817}
5818
5819TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
5820 verifyIncompleteFormat("#define A :");
5821 verifyFormat("#define SOMECASES \\\n"
5822 " case 1: \\\n"
5823 " case 2",
5824 getLLVMStyleWithColumns(20));
5825 verifyFormat("#define MACRO(a) \\\n"
5826 " if (a) \\\n"
5827 " f(); \\\n"
5828 " else \\\n"
5829 " g()",
5830 getLLVMStyleWithColumns(18));
5831 verifyFormat("#define A template <typename T>");
5832 verifyIncompleteFormat("#define STR(x) #x\n"
5833 "f(STR(this_is_a_string_literal{));");
5834 verifyFormat("#pragma omp threadprivate( \\\n"
5835 " y)), // expected-warning",
5836 getLLVMStyleWithColumns(28));
5837 verifyFormat("#d, = };");
5838 verifyFormat("#if \"a");
5839 verifyIncompleteFormat("({\n"
5840 "#define b \\\n"
5841 " } \\\n"
5842 " a\n"
5843 "a",
5844 getLLVMStyleWithColumns(15));
5845 verifyFormat("#define A \\\n"
5846 " { \\\n"
5847 " {\n"
5848 "#define B \\\n"
5849 " } \\\n"
5850 " }",
5851 getLLVMStyleWithColumns(15));
5852 verifyNoCrash(Code: "#if a\na(\n#else\n#endif\n{a");
5853 verifyNoCrash(Code: "a={0,1\n#if a\n#else\n;\n#endif\n}");
5854 verifyNoCrash(Code: "#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5855 verifyNoCrash(Code: "#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5856 verifyNoCrash(Code: "#else\n"
5857 "#else\n"
5858 "#endif\n"
5859 "#endif");
5860 verifyNoCrash(Code: "#else\n"
5861 "#if X\n"
5862 "#endif\n"
5863 "#endif");
5864 verifyNoCrash(Code: "#else\n"
5865 "#endif\n"
5866 "#if X\n"
5867 "#endif");
5868 verifyNoCrash(Code: "#if X\n"
5869 "#else\n"
5870 "#else\n"
5871 "#endif\n"
5872 "#endif");
5873 verifyNoCrash(Code: "#if X\n"
5874 "#elif Y\n"
5875 "#elif Y\n"
5876 "#endif\n"
5877 "#endif");
5878 verifyNoCrash(Code: "#endif\n"
5879 "#endif");
5880 verifyNoCrash(Code: "#endif\n"
5881 "#else");
5882 verifyNoCrash(Code: "#endif\n"
5883 "#elif Y");
5884}
5885
5886TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
5887 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5888 verifyFormat("class A : public QObject {\n"
5889 " Q_OBJECT\n"
5890 "\n"
5891 " A() {}\n"
5892 "};",
5893 "class A : public QObject {\n"
5894 " Q_OBJECT\n"
5895 "\n"
5896 " A() {\n}\n"
5897 "} ;");
5898 verifyFormat("MACRO\n"
5899 "/*static*/ int i;",
5900 "MACRO\n"
5901 " /*static*/ int i;");
5902 verifyFormat("SOME_MACRO\n"
5903 "namespace {\n"
5904 "void f();\n"
5905 "} // namespace",
5906 "SOME_MACRO\n"
5907 " namespace {\n"
5908 "void f( );\n"
5909 "} // namespace");
5910 // Only if the identifier contains at least 5 characters.
5911 verifyFormat("HTTP f();", "HTTP\nf();");
5912 verifyNoChange("MACRO\nf();");
5913 // Only if everything is upper case.
5914 verifyFormat("class A : public QObject {\n"
5915 " Q_Object A() {}\n"
5916 "};",
5917 "class A : public QObject {\n"
5918 " Q_Object\n"
5919 " A() {\n}\n"
5920 "} ;");
5921
5922 // Only if the next line can actually start an unwrapped line.
5923 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5924 "<< SomeThing;");
5925
5926 verifyFormat("GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)(foo);",
5927 "GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)\n"
5928 "(foo);",
5929 getLLVMStyleWithColumns(60));
5930
5931 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5932 "(n, buffers))",
5933 getChromiumStyle(FormatStyle::LK_Cpp));
5934
5935 // See PR41483
5936 verifyNoChange("/**/ FOO(a)\n"
5937 "FOO(b)");
5938}
5939
5940TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
5941 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5942 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5943 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5944 "class X {};\n"
5945 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5946 "int *createScopDetectionPass() { return 0; }",
5947 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5948 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5949 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5950 " class X {};\n"
5951 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5952 " int *createScopDetectionPass() { return 0; }");
5953 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5954 // braces, so that inner block is indented one level more.
5955 verifyFormat("int q() {\n"
5956 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5957 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5958 " IPC_END_MESSAGE_MAP()\n"
5959 "}",
5960 "int q() {\n"
5961 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5962 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5963 " IPC_END_MESSAGE_MAP()\n"
5964 "}");
5965
5966 // Same inside macros.
5967 verifyFormat("#define LIST(L) \\\n"
5968 " L(A) \\\n"
5969 " L(B) \\\n"
5970 " L(C)",
5971 "#define LIST(L) \\\n"
5972 " L(A) \\\n"
5973 " L(B) \\\n"
5974 " L(C)",
5975 getGoogleStyle());
5976
5977 // These must not be recognized as macros.
5978 verifyFormat("int q() {\n"
5979 " f(x);\n"
5980 " f(x) {}\n"
5981 " f(x)->g();\n"
5982 " f(x)->*g();\n"
5983 " f(x).g();\n"
5984 " f(x) = x;\n"
5985 " f(x) += x;\n"
5986 " f(x) -= x;\n"
5987 " f(x) *= x;\n"
5988 " f(x) /= x;\n"
5989 " f(x) %= x;\n"
5990 " f(x) &= x;\n"
5991 " f(x) |= x;\n"
5992 " f(x) ^= x;\n"
5993 " f(x) >>= x;\n"
5994 " f(x) <<= x;\n"
5995 " f(x)[y].z();\n"
5996 " LOG(INFO) << x;\n"
5997 " ifstream(x) >> x;\n"
5998 "}",
5999 "int q() {\n"
6000 " f(x)\n;\n"
6001 " f(x)\n {}\n"
6002 " f(x)\n->g();\n"
6003 " f(x)\n->*g();\n"
6004 " f(x)\n.g();\n"
6005 " f(x)\n = x;\n"
6006 " f(x)\n += x;\n"
6007 " f(x)\n -= x;\n"
6008 " f(x)\n *= x;\n"
6009 " f(x)\n /= x;\n"
6010 " f(x)\n %= x;\n"
6011 " f(x)\n &= x;\n"
6012 " f(x)\n |= x;\n"
6013 " f(x)\n ^= x;\n"
6014 " f(x)\n >>= x;\n"
6015 " f(x)\n <<= x;\n"
6016 " f(x)\n[y].z();\n"
6017 " LOG(INFO)\n << x;\n"
6018 " ifstream(x)\n >> x;\n"
6019 "}");
6020 verifyFormat("int q() {\n"
6021 " F(x)\n"
6022 " if (1) {\n"
6023 " }\n"
6024 " F(x)\n"
6025 " while (1) {\n"
6026 " }\n"
6027 " F(x)\n"
6028 " G(x);\n"
6029 " F(x)\n"
6030 " try {\n"
6031 " Q();\n"
6032 " } catch (...) {\n"
6033 " }\n"
6034 "}",
6035 "int q() {\n"
6036 "F(x)\n"
6037 "if (1) {}\n"
6038 "F(x)\n"
6039 "while (1) {}\n"
6040 "F(x)\n"
6041 "G(x);\n"
6042 "F(x)\n"
6043 "try { Q(); } catch (...) {}\n"
6044 "}");
6045 verifyFormat("class A {\n"
6046 " A() : t(0) {}\n"
6047 " A(int i) noexcept() : {}\n"
6048 " A(X x)\n" // FIXME: function-level try blocks are broken.
6049 " try : t(0) {\n"
6050 " } catch (...) {\n"
6051 " }\n"
6052 "};",
6053 "class A {\n"
6054 " A()\n : t(0) {}\n"
6055 " A(int i)\n noexcept() : {}\n"
6056 " A(X x)\n"
6057 " try : t(0) {} catch (...) {}\n"
6058 "};");
6059 FormatStyle Style = getLLVMStyle();
6060 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6061 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
6062 Style.BraceWrapping.AfterFunction = true;
6063 verifyFormat("void f()\n"
6064 "try\n"
6065 "{\n"
6066 "}",
6067 "void f() try {\n"
6068 "}",
6069 Style);
6070 verifyFormat("class SomeClass {\n"
6071 "public:\n"
6072 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6073 "};",
6074 "class SomeClass {\n"
6075 "public:\n"
6076 " SomeClass()\n"
6077 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6078 "};");
6079 verifyFormat("class SomeClass {\n"
6080 "public:\n"
6081 " SomeClass()\n"
6082 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6083 "};",
6084 "class SomeClass {\n"
6085 "public:\n"
6086 " SomeClass()\n"
6087 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6088 "};",
6089 getLLVMStyleWithColumns(40));
6090
6091 verifyFormat("MACRO(>)");
6092
6093 // Some macros contain an implicit semicolon.
6094 Style = getLLVMStyle();
6095 Style.StatementMacros.push_back(x: "FOO");
6096 verifyFormat("FOO(a) int b = 0;");
6097 verifyFormat("FOO(a)\n"
6098 "int b = 0;",
6099 Style);
6100 verifyFormat("FOO(a);\n"
6101 "int b = 0;",
6102 Style);
6103 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6104 "int b = 0;",
6105 Style);
6106 verifyFormat("FOO()\n"
6107 "int b = 0;",
6108 Style);
6109 verifyFormat("FOO\n"
6110 "int b = 0;",
6111 Style);
6112 verifyFormat("void f() {\n"
6113 " FOO(a)\n"
6114 " return a;\n"
6115 "}",
6116 Style);
6117 verifyFormat("FOO(a)\n"
6118 "FOO(b)",
6119 Style);
6120 verifyFormat("int a = 0;\n"
6121 "FOO(b)\n"
6122 "int c = 0;",
6123 Style);
6124 verifyFormat("int a = 0;\n"
6125 "int x = FOO(a)\n"
6126 "int b = 0;",
6127 Style);
6128 verifyFormat("void foo(int a) { FOO(a) }\n"
6129 "uint32_t bar() {}",
6130 Style);
6131}
6132
6133TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) {
6134 FormatStyle ZeroColumn = getLLVMStyleWithColumns(ColumnLimit: 0);
6135
6136 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6137 ZeroColumn);
6138}
6139
6140TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
6141 verifyFormat("#define A \\\n"
6142 " f({ \\\n"
6143 " g(); \\\n"
6144 " });",
6145 getLLVMStyleWithColumns(11));
6146}
6147
6148TEST_F(FormatTest, IndentPreprocessorDirectives) {
6149 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
6150 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
6151 verifyFormat("#ifdef _WIN32\n"
6152 "#define A 0\n"
6153 "#ifdef VAR2\n"
6154 "#define B 1\n"
6155 "#include <someheader.h>\n"
6156 "#define MACRO \\\n"
6157 " some_very_long_func_aaaaaaaaaa();\n"
6158 "#endif\n"
6159 "#else\n"
6160 "#define A 1\n"
6161 "#endif",
6162 Style);
6163 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
6164 verifyFormat("#if 1\n"
6165 "# define __STR(x) #x\n"
6166 "#endif",
6167 Style);
6168 verifyFormat("#ifdef _WIN32\n"
6169 "# define A 0\n"
6170 "# ifdef VAR2\n"
6171 "# define B 1\n"
6172 "# include <someheader.h>\n"
6173 "# define MACRO \\\n"
6174 " some_very_long_func_aaaaaaaaaa();\n"
6175 "# endif\n"
6176 "#else\n"
6177 "# define A 1\n"
6178 "#endif",
6179 Style);
6180 verifyFormat("#if A\n"
6181 "# define MACRO \\\n"
6182 " void a(int x) { \\\n"
6183 " b(); \\\n"
6184 " c(); \\\n"
6185 " d(); \\\n"
6186 " e(); \\\n"
6187 " f(); \\\n"
6188 " }\n"
6189 "#endif",
6190 Style);
6191 // Comments before include guard.
6192 verifyFormat("// file comment\n"
6193 "// file comment\n"
6194 "#ifndef HEADER_H\n"
6195 "#define HEADER_H\n"
6196 "code();\n"
6197 "#endif",
6198 Style);
6199 // Test with include guards.
6200 verifyFormat("#ifndef HEADER_H\n"
6201 "#define HEADER_H\n"
6202 "code();\n"
6203 "#endif",
6204 Style);
6205 // Include guards must have a #define with the same variable immediately
6206 // after #ifndef.
6207 verifyFormat("#ifndef NOT_GUARD\n"
6208 "# define FOO\n"
6209 "code();\n"
6210 "#endif",
6211 Style);
6212
6213 // Include guards must cover the entire file.
6214 verifyFormat("code();\n"
6215 "code();\n"
6216 "#ifndef NOT_GUARD\n"
6217 "# define NOT_GUARD\n"
6218 "code();\n"
6219 "#endif",
6220 Style);
6221 verifyFormat("#ifndef NOT_GUARD\n"
6222 "# define NOT_GUARD\n"
6223 "code();\n"
6224 "#endif\n"
6225 "code();",
6226 Style);
6227 // Test with trailing blank lines.
6228 verifyFormat("#ifndef HEADER_H\n"
6229 "#define HEADER_H\n"
6230 "code();\n"
6231 "#endif",
6232 Style);
6233 // Include guards don't have #else.
6234 verifyFormat("#ifndef NOT_GUARD\n"
6235 "# define NOT_GUARD\n"
6236 "code();\n"
6237 "#else\n"
6238 "#endif",
6239 Style);
6240 verifyFormat("#ifndef NOT_GUARD\n"
6241 "# define NOT_GUARD\n"
6242 "code();\n"
6243 "#elif FOO\n"
6244 "#endif",
6245 Style);
6246 // Non-identifier #define after potential include guard.
6247 verifyFormat("#ifndef FOO\n"
6248 "# define 1\n"
6249 "#endif",
6250 Style);
6251 // #if closes past last non-preprocessor line.
6252 verifyFormat("#ifndef FOO\n"
6253 "#define FOO\n"
6254 "#if 1\n"
6255 "int i;\n"
6256 "# define A 0\n"
6257 "#endif\n"
6258 "#endif",
6259 Style);
6260 // Don't crash if there is an #elif directive without a condition.
6261 verifyFormat("#if 1\n"
6262 "int x;\n"
6263 "#elif\n"
6264 "int y;\n"
6265 "#else\n"
6266 "int z;\n"
6267 "#endif",
6268 Style);
6269 // FIXME: This doesn't handle the case where there's code between the
6270 // #ifndef and #define but all other conditions hold. This is because when
6271 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6272 // previous code line yet, so we can't detect it.
6273 verifyFormat("#ifndef NOT_GUARD\n"
6274 "code();\n"
6275 "#define NOT_GUARD\n"
6276 "code();\n"
6277 "#endif",
6278 "#ifndef NOT_GUARD\n"
6279 "code();\n"
6280 "# define NOT_GUARD\n"
6281 "code();\n"
6282 "#endif",
6283 Style);
6284 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6285 // be outside an include guard. Examples are #pragma once and
6286 // #pragma GCC diagnostic, or anything else that does not change the meaning
6287 // of the file if it's included multiple times.
6288 verifyFormat("#ifdef WIN32\n"
6289 "# pragma once\n"
6290 "#endif\n"
6291 "#ifndef HEADER_H\n"
6292 "# define HEADER_H\n"
6293 "code();\n"
6294 "#endif",
6295 "#ifdef WIN32\n"
6296 "# pragma once\n"
6297 "#endif\n"
6298 "#ifndef HEADER_H\n"
6299 "#define HEADER_H\n"
6300 "code();\n"
6301 "#endif",
6302 Style);
6303 // FIXME: This does not detect when there is a single non-preprocessor line
6304 // in front of an include-guard-like structure where other conditions hold
6305 // because ScopedLineState hides the line.
6306 verifyFormat("code();\n"
6307 "#ifndef HEADER_H\n"
6308 "#define HEADER_H\n"
6309 "code();\n"
6310 "#endif",
6311 "code();\n"
6312 "#ifndef HEADER_H\n"
6313 "# define HEADER_H\n"
6314 "code();\n"
6315 "#endif",
6316 Style);
6317 // Keep comments aligned with #, otherwise indent comments normally. These
6318 // tests cannot use verifyFormat because messUp manipulates leading
6319 // whitespace.
6320 {
6321 const char *Expected = ""
6322 "void f() {\n"
6323 "#if 1\n"
6324 "// Preprocessor aligned.\n"
6325 "# define A 0\n"
6326 " // Code. Separated by blank line.\n"
6327 "\n"
6328 "# define B 0\n"
6329 " // Code. Not aligned with #\n"
6330 "# define C 0\n"
6331 "#endif";
6332 const char *ToFormat = ""
6333 "void f() {\n"
6334 "#if 1\n"
6335 "// Preprocessor aligned.\n"
6336 "# define A 0\n"
6337 "// Code. Separated by blank line.\n"
6338 "\n"
6339 "# define B 0\n"
6340 " // Code. Not aligned with #\n"
6341 "# define C 0\n"
6342 "#endif";
6343 verifyFormat(Expected, ToFormat, Style);
6344 verifyNoChange(Expected, Style);
6345 }
6346 // Keep block quotes aligned.
6347 {
6348 const char *Expected = ""
6349 "void f() {\n"
6350 "#if 1\n"
6351 "/* Preprocessor aligned. */\n"
6352 "# define A 0\n"
6353 " /* Code. Separated by blank line. */\n"
6354 "\n"
6355 "# define B 0\n"
6356 " /* Code. Not aligned with # */\n"
6357 "# define C 0\n"
6358 "#endif";
6359 const char *ToFormat = ""
6360 "void f() {\n"
6361 "#if 1\n"
6362 "/* Preprocessor aligned. */\n"
6363 "# define A 0\n"
6364 "/* Code. Separated by blank line. */\n"
6365 "\n"
6366 "# define B 0\n"
6367 " /* Code. Not aligned with # */\n"
6368 "# define C 0\n"
6369 "#endif";
6370 verifyFormat(Expected, ToFormat, Style);
6371 verifyNoChange(Expected, Style);
6372 }
6373 // Keep comments aligned with un-indented directives.
6374 {
6375 const char *Expected = ""
6376 "void f() {\n"
6377 "// Preprocessor aligned.\n"
6378 "#define A 0\n"
6379 " // Code. Separated by blank line.\n"
6380 "\n"
6381 "#define B 0\n"
6382 " // Code. Not aligned with #\n"
6383 "#define C 0\n";
6384 const char *ToFormat = ""
6385 "void f() {\n"
6386 "// Preprocessor aligned.\n"
6387 "#define A 0\n"
6388 "// Code. Separated by blank line.\n"
6389 "\n"
6390 "#define B 0\n"
6391 " // Code. Not aligned with #\n"
6392 "#define C 0\n";
6393 verifyFormat(Expected, ToFormat, Style);
6394 verifyNoChange(Expected, Style);
6395 }
6396 // Test AfterHash with tabs.
6397 {
6398 FormatStyle Tabbed = Style;
6399 Tabbed.UseTab = FormatStyle::UT_Always;
6400 Tabbed.IndentWidth = 8;
6401 Tabbed.TabWidth = 8;
6402 verifyFormat("#ifdef _WIN32\n"
6403 "#\tdefine A 0\n"
6404 "#\tifdef VAR2\n"
6405 "#\t\tdefine B 1\n"
6406 "#\t\tinclude <someheader.h>\n"
6407 "#\t\tdefine MACRO \\\n"
6408 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6409 "#\tendif\n"
6410 "#else\n"
6411 "#\tdefine A 1\n"
6412 "#endif",
6413 Tabbed);
6414 }
6415
6416 // Regression test: Multiline-macro inside include guards.
6417 verifyFormat("#ifndef HEADER_H\n"
6418 "#define HEADER_H\n"
6419 "#define A() \\\n"
6420 " int i; \\\n"
6421 " int j;\n"
6422 "#endif // HEADER_H",
6423 getLLVMStyleWithColumns(20));
6424
6425 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
6426 // Basic before hash indent tests
6427 verifyFormat("#ifdef _WIN32\n"
6428 " #define A 0\n"
6429 " #ifdef VAR2\n"
6430 " #define B 1\n"
6431 " #include <someheader.h>\n"
6432 " #define MACRO \\\n"
6433 " some_very_long_func_aaaaaaaaaa();\n"
6434 " #endif\n"
6435 "#else\n"
6436 " #define A 1\n"
6437 "#endif",
6438 Style);
6439 verifyFormat("#if A\n"
6440 " #define MACRO \\\n"
6441 " void a(int x) { \\\n"
6442 " b(); \\\n"
6443 " c(); \\\n"
6444 " d(); \\\n"
6445 " e(); \\\n"
6446 " f(); \\\n"
6447 " }\n"
6448 "#endif",
6449 Style);
6450 // Keep comments aligned with indented directives. These
6451 // tests cannot use verifyFormat because messUp manipulates leading
6452 // whitespace.
6453 {
6454 const char *Expected = "void f() {\n"
6455 "// Aligned to preprocessor.\n"
6456 "#if 1\n"
6457 " // Aligned to code.\n"
6458 " int a;\n"
6459 " #if 1\n"
6460 " // Aligned to preprocessor.\n"
6461 " #define A 0\n"
6462 " // Aligned to code.\n"
6463 " int b;\n"
6464 " #endif\n"
6465 "#endif\n"
6466 "}";
6467 const char *ToFormat = "void f() {\n"
6468 "// Aligned to preprocessor.\n"
6469 "#if 1\n"
6470 "// Aligned to code.\n"
6471 "int a;\n"
6472 "#if 1\n"
6473 "// Aligned to preprocessor.\n"
6474 "#define A 0\n"
6475 "// Aligned to code.\n"
6476 "int b;\n"
6477 "#endif\n"
6478 "#endif\n"
6479 "}";
6480 verifyFormat(Expected, ToFormat, Style);
6481 verifyNoChange(Expected, Style);
6482 }
6483 {
6484 const char *Expected = "void f() {\n"
6485 "/* Aligned to preprocessor. */\n"
6486 "#if 1\n"
6487 " /* Aligned to code. */\n"
6488 " int a;\n"
6489 " #if 1\n"
6490 " /* Aligned to preprocessor. */\n"
6491 " #define A 0\n"
6492 " /* Aligned to code. */\n"
6493 " int b;\n"
6494 " #endif\n"
6495 "#endif\n"
6496 "}";
6497 const char *ToFormat = "void f() {\n"
6498 "/* Aligned to preprocessor. */\n"
6499 "#if 1\n"
6500 "/* Aligned to code. */\n"
6501 "int a;\n"
6502 "#if 1\n"
6503 "/* Aligned to preprocessor. */\n"
6504 "#define A 0\n"
6505 "/* Aligned to code. */\n"
6506 "int b;\n"
6507 "#endif\n"
6508 "#endif\n"
6509 "}";
6510 verifyFormat(Expected, ToFormat, Style);
6511 verifyNoChange(Expected, Style);
6512 }
6513
6514 // Test single comment before preprocessor
6515 verifyFormat("// Comment\n"
6516 "\n"
6517 "#if 1\n"
6518 "#endif",
6519 Style);
6520
6521 verifyFormat("#ifndef ABCDE\n"
6522 " #define ABCDE 0\n"
6523 "#endif\n"
6524 "\n"
6525 "#define FGHIJK",
6526 "#ifndef ABCDE\n"
6527 "#define ABCDE 0\n"
6528 "#endif\n"
6529 "\n"
6530 "#define FGHIJK",
6531 Style);
6532}
6533
6534TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) {
6535 FormatStyle Style = getLLVMStyle();
6536 Style.AlignConsecutiveAssignments.Enabled = true;
6537 Style.AlignConsecutiveDeclarations.Enabled = true;
6538
6539 // Test with just #if blocks.
6540 verifyFormat("void f1() {\n"
6541 "#if 1\n"
6542 " int foo = 1;\n"
6543 " int foobar = 2;\n"
6544 "#endif\n"
6545 "}\n"
6546 "#if 1\n"
6547 "int baz = 3;\n"
6548 "#endif\n"
6549 "void f2() {\n"
6550 "#if 1\n"
6551 " char *foobarbaz = \"foobarbaz\";\n"
6552 " int quux = 4;\n"
6553 "}",
6554 Style);
6555
6556 // Test with just #else blocks.
6557 verifyFormat("void f1() {\n"
6558 "#if 1\n"
6559 "#else\n"
6560 " int foo = 1;\n"
6561 " int foobar = 2;\n"
6562 "#endif\n"
6563 "}\n"
6564 "#if 1\n"
6565 "#else\n"
6566 "int baz = 3;\n"
6567 "#endif\n"
6568 "void f2() {\n"
6569 "#if 1\n"
6570 "#else\n"
6571 " char *foobarbaz = \"foobarbaz\";\n"
6572 " int quux = 4;\n"
6573 "}",
6574 Style);
6575 verifyFormat("auto foo = [] { return; };\n"
6576 "#if FOO\n"
6577 "#else\n"
6578 "count = bar;\n"
6579 "mbid = bid;\n"
6580 "#endif",
6581 Style);
6582
6583 // Test with a mix of #if and #else blocks.
6584 verifyFormat("void f1() {\n"
6585 "#if 1\n"
6586 "#else\n"
6587 " int foo = 1;\n"
6588 " int foobar = 2;\n"
6589 "#endif\n"
6590 "}\n"
6591 "#if 1\n"
6592 "int baz = 3;\n"
6593 "#endif\n"
6594 "void f2() {\n"
6595 "#if 1\n"
6596 "#else\n"
6597 " // prevent alignment with #else in f1\n"
6598 " char *foobarbaz = \"foobarbaz\";\n"
6599 " int quux = 4;\n"
6600 "}",
6601 Style);
6602
6603 // Test with nested #if and #else blocks.
6604 verifyFormat("void f1() {\n"
6605 "#if 1\n"
6606 "#else\n"
6607 "#if 2\n"
6608 "#else\n"
6609 " int foo = 1;\n"
6610 " int foobar = 2;\n"
6611 "#endif\n"
6612 "#endif\n"
6613 "}\n"
6614 "#if 1\n"
6615 "#else\n"
6616 "#if 2\n"
6617 "int baz = 3;\n"
6618 "#endif\n"
6619 "#endif\n"
6620 "void f2() {\n"
6621 "#if 1\n"
6622 "#if 2\n"
6623 "#else\n"
6624 " // prevent alignment with #else in f1\n"
6625 " char *foobarbaz = \"foobarbaz\";\n"
6626 " int quux = 4;\n"
6627 "#endif\n"
6628 "#endif\n"
6629 "}",
6630 Style);
6631
6632 verifyFormat("#if FOO\n"
6633 "int a = 1;\n"
6634 "#else\n"
6635 "int ab = 2;\n"
6636 "#endif\n"
6637 "#ifdef BAR\n"
6638 "int abc = 3;\n"
6639 "#elifdef BAZ\n"
6640 "int abcd = 4;\n"
6641 "#endif",
6642 Style);
6643
6644 verifyFormat("void f() {\n"
6645 " if (foo) {\n"
6646 "#if FOO\n"
6647 " int a = 1;\n"
6648 "#else\n"
6649 " bool a = true;\n"
6650 "#endif\n"
6651 " int abc = 3;\n"
6652 "#ifndef BAR\n"
6653 " int abcd = 4;\n"
6654 "#elif BAZ\n"
6655 " bool abcd = true;\n"
6656 "#endif\n"
6657 " }\n"
6658 "}",
6659 Style);
6660
6661 verifyFormat("void f() {\n"
6662 "#if FOO\n"
6663 " a = 1;\n"
6664 "#else\n"
6665 " ab = 2;\n"
6666 "#endif\n"
6667 "}\n"
6668 "void g() {\n"
6669 "#if BAR\n"
6670 " abc = 3;\n"
6671 "#elifndef BAZ\n"
6672 " abcd = 4;\n"
6673 "#endif\n"
6674 "}",
6675 Style);
6676}
6677
6678TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
6679 verifyFormat("{\n"
6680 " {\n"
6681 " a #c;\n"
6682 " }\n"
6683 "}");
6684}
6685
6686TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
6687 verifyFormat("#define A \\\n { \\\n {\nint i;",
6688 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6689 verifyFormat("#define A \\\n } \\\n }\nint i;",
6690 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6691}
6692
6693TEST_F(FormatTest, EscapedNewlines) {
6694 FormatStyle Narrow = getLLVMStyleWithColumns(ColumnLimit: 11);
6695 verifyFormat("#define A \\\n int i; \\\n int j;",
6696 "#define A \\\nint i;\\\n int j;", Narrow);
6697 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6698 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6699 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6700 verifyNoChange("<a\n\\\\\n>");
6701
6702 FormatStyle AlignLeft = getLLVMStyle();
6703 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
6704 verifyFormat("#define MACRO(x) \\\n"
6705 "private: \\\n"
6706 " int x(int a);",
6707 AlignLeft);
6708
6709 // CRLF line endings
6710 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6711 "#define A \\\r\nint i;\\\r\n int j;", Narrow);
6712 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6713 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6714 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6715 verifyNoChange("<a\r\n\\\\\r\n>");
6716 verifyFormat("#define MACRO(x) \\\r\n"
6717 "private: \\\r\n"
6718 " int x(int a);",
6719 AlignLeft);
6720
6721 constexpr StringRef Code{"#define A \\\n"
6722 " int a123; \\\n"
6723 " int a; \\\n"
6724 " int a1234;"};
6725 verifyFormat(Code, AlignLeft);
6726
6727 constexpr StringRef Code2{"#define A \\\n"
6728 " int a123; \\\n"
6729 " int a; \\\n"
6730 " int a1234;"};
6731 auto LastLine = getLLVMStyle();
6732 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine;
6733 verifyFormat(Code2, LastLine);
6734
6735 LastLine.ColumnLimit = 13;
6736 verifyFormat(Code, LastLine);
6737
6738 LastLine.ColumnLimit = 0;
6739 verifyFormat(Code2, LastLine);
6740
6741 FormatStyle DontAlign = getLLVMStyle();
6742 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
6743 DontAlign.MaxEmptyLinesToKeep = 3;
6744 // FIXME: can't use verifyFormat here because the newline before
6745 // "public:" is not inserted the first time it's reformatted
6746 verifyNoChange("#define A \\\n"
6747 " class Foo { \\\n"
6748 " void bar(); \\\n"
6749 "\\\n"
6750 "\\\n"
6751 "\\\n"
6752 " public: \\\n"
6753 " void baz(); \\\n"
6754 " };",
6755 DontAlign);
6756}
6757
6758TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
6759 verifyFormat("#define A \\\n"
6760 " int v( \\\n"
6761 " a); \\\n"
6762 " int i;",
6763 getLLVMStyleWithColumns(11));
6764}
6765
6766TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
6767 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6768 " \\\n"
6769 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6770 "\n"
6771 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6772 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6773 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6774 "\\\n"
6775 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6776 " \n"
6777 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6778 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6779}
6780
6781TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
6782 verifyFormat("int\n"
6783 "#define A\n"
6784 " a;",
6785 "int\n#define A\na;");
6786 verifyFormat("functionCallTo(\n"
6787 " someOtherFunction(\n"
6788 " withSomeParameters, whichInSequence,\n"
6789 " areLongerThanALine(andAnotherCall,\n"
6790 "#define A B\n"
6791 " withMoreParamters,\n"
6792 " whichStronglyInfluenceTheLayout),\n"
6793 " andMoreParameters),\n"
6794 " trailing);",
6795 getLLVMStyleWithColumns(69));
6796 verifyFormat("Foo::Foo()\n"
6797 "#ifdef BAR\n"
6798 " : baz(0)\n"
6799 "#endif\n"
6800 "{\n"
6801 "}");
6802 verifyFormat("void f() {\n"
6803 " if (true)\n"
6804 "#ifdef A\n"
6805 " f(42);\n"
6806 " x();\n"
6807 "#else\n"
6808 " g();\n"
6809 " x();\n"
6810 "#endif\n"
6811 "}");
6812 verifyFormat("void f(param1, param2,\n"
6813 " param3,\n"
6814 "#ifdef A\n"
6815 " param4(param5,\n"
6816 "#ifdef A1\n"
6817 " param6,\n"
6818 "#ifdef A2\n"
6819 " param7),\n"
6820 "#else\n"
6821 " param8),\n"
6822 " param9,\n"
6823 "#endif\n"
6824 " param10,\n"
6825 "#endif\n"
6826 " param11)\n"
6827 "#else\n"
6828 " param12)\n"
6829 "#endif\n"
6830 "{\n"
6831 " x();\n"
6832 "}",
6833 getLLVMStyleWithColumns(28));
6834 verifyFormat("#if 1\n"
6835 "int i;");
6836 verifyFormat("#if 1\n"
6837 "#endif\n"
6838 "#if 1\n"
6839 "#else\n"
6840 "#endif");
6841 verifyFormat("DEBUG({\n"
6842 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6844 "});\n"
6845 "#if a\n"
6846 "#else\n"
6847 "#endif");
6848
6849 verifyIncompleteFormat("void f(\n"
6850 "#if A\n"
6851 ");\n"
6852 "#else\n"
6853 "#endif");
6854
6855 // Verify that indentation is correct when there is an `#if 0` with an
6856 // `#else`.
6857 verifyFormat("#if 0\n"
6858 "{\n"
6859 "#else\n"
6860 "{\n"
6861 "#endif\n"
6862 " x;\n"
6863 "}");
6864
6865 verifyFormat("#if 0\n"
6866 "#endif\n"
6867 "#if X\n"
6868 "int something_fairly_long; // Align here please\n"
6869 "#endif // Should be aligned");
6870
6871 verifyFormat("#if 0\n"
6872 "#endif\n"
6873 "#if X\n"
6874 "#else // Align\n"
6875 ";\n"
6876 "#endif // Align");
6877
6878 verifyFormat("void SomeFunction(int param1,\n"
6879 " template <\n"
6880 "#ifdef A\n"
6881 "#if 0\n"
6882 "#endif\n"
6883 " MyType<Some>>\n"
6884 "#else\n"
6885 " Type1, Type2>\n"
6886 "#endif\n"
6887 " param2,\n"
6888 " param3) {\n"
6889 " f();\n"
6890 "}");
6891
6892 verifyFormat("#ifdef __cplusplus\n"
6893 "extern \"C\"\n"
6894 "#endif\n"
6895 " void f();");
6896}
6897
6898TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
6899 verifyFormat("#endif\n"
6900 "#if B");
6901}
6902
6903TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
6904 FormatStyle SingleLine = getLLVMStyle();
6905 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
6906 verifyFormat("#if 0\n"
6907 "#elif 1\n"
6908 "#endif\n"
6909 "void foo() {\n"
6910 " if (test) foo2();\n"
6911 "}",
6912 SingleLine);
6913}
6914
6915TEST_F(FormatTest, LayoutBlockInsideParens) {
6916 verifyFormat("functionCall({ int i; });");
6917 verifyFormat("functionCall({\n"
6918 " int i;\n"
6919 " int j;\n"
6920 "});");
6921 verifyFormat("functionCall(\n"
6922 " {\n"
6923 " int i;\n"
6924 " int j;\n"
6925 " },\n"
6926 " aaaa, bbbb, cccc);");
6927 verifyFormat("functionA(functionB({\n"
6928 " int i;\n"
6929 " int j;\n"
6930 " }),\n"
6931 " aaaa, bbbb, cccc);");
6932 verifyFormat("functionCall(\n"
6933 " {\n"
6934 " int i;\n"
6935 " int j;\n"
6936 " },\n"
6937 " aaaa, bbbb, // comment\n"
6938 " cccc);");
6939 verifyFormat("functionA(functionB({\n"
6940 " int i;\n"
6941 " int j;\n"
6942 " }),\n"
6943 " aaaa, bbbb, // comment\n"
6944 " cccc);");
6945 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6946 verifyFormat("functionCall(aaaa, bbbb, {\n"
6947 " int i;\n"
6948 " int j;\n"
6949 "});");
6950 verifyFormat(
6951 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6952 " {\n"
6953 " int i; // break\n"
6954 " },\n"
6955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6956 " ccccccccccccccccc));");
6957 verifyFormat("DEBUG({\n"
6958 " if (a)\n"
6959 " f();\n"
6960 "});");
6961}
6962
6963TEST_F(FormatTest, LayoutBlockInsideStatement) {
6964 verifyFormat("SOME_MACRO { int i; }\n"
6965 "int i;",
6966 " SOME_MACRO {int i;} int i;");
6967}
6968
6969TEST_F(FormatTest, LayoutNestedBlocks) {
6970 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6971 " struct s {\n"
6972 " int i;\n"
6973 " };\n"
6974 " s kBitsToOs[] = {{10}};\n"
6975 " for (int i = 0; i < 10; ++i)\n"
6976 " return;\n"
6977 "}");
6978 verifyFormat("call(parameter, {\n"
6979 " something();\n"
6980 " // Comment using all columns.\n"
6981 " somethingelse();\n"
6982 "});",
6983 getLLVMStyleWithColumns(40));
6984 verifyFormat("DEBUG( //\n"
6985 " { f(); }, a);");
6986 verifyFormat("DEBUG( //\n"
6987 " {\n"
6988 " f(); //\n"
6989 " },\n"
6990 " a);");
6991
6992 verifyFormat("call(parameter, {\n"
6993 " something();\n"
6994 " // Comment too\n"
6995 " // looooooooooong.\n"
6996 " somethingElse();\n"
6997 "});",
6998 "call(parameter, {\n"
6999 " something();\n"
7000 " // Comment too looooooooooong.\n"
7001 " somethingElse();\n"
7002 "});",
7003 getLLVMStyleWithColumns(29));
7004 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
7005 verifyFormat("DEBUG({ // comment\n"
7006 " int i;\n"
7007 "});",
7008 "DEBUG({ // comment\n"
7009 "int i;\n"
7010 "});");
7011 verifyFormat("DEBUG({\n"
7012 " int i;\n"
7013 "\n"
7014 " // comment\n"
7015 " int j;\n"
7016 "});",
7017 "DEBUG({\n"
7018 " int i;\n"
7019 "\n"
7020 " // comment\n"
7021 " int j;\n"
7022 "});");
7023
7024 verifyFormat("DEBUG({\n"
7025 " if (a)\n"
7026 " return;\n"
7027 "});");
7028 verifyGoogleFormat("DEBUG({\n"
7029 " if (a) return;\n"
7030 "});");
7031 FormatStyle Style = getGoogleStyle();
7032 Style.ColumnLimit = 45;
7033 verifyFormat("Debug(\n"
7034 " aaaaa,\n"
7035 " {\n"
7036 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
7037 " },\n"
7038 " a);",
7039 Style);
7040
7041 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
7042
7043 verifyNoCrash(Code: "^{v^{a}}");
7044}
7045
7046TEST_F(FormatTest, FormatNestedBlocksInMacros) {
7047 verifyFormat("#define MACRO() \\\n"
7048 " Debug(aaa, /* force line break */ \\\n"
7049 " { \\\n"
7050 " int i; \\\n"
7051 " int j; \\\n"
7052 " })",
7053 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
7054 " { int i; int j; })",
7055 getGoogleStyle());
7056
7057 verifyFormat("#define A \\\n"
7058 " [] { \\\n"
7059 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
7060 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
7061 " }",
7062 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
7063 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
7064 getGoogleStyle());
7065}
7066
7067TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
7068 verifyFormat("enum E {};");
7069 verifyFormat("enum E {}");
7070 FormatStyle Style = getLLVMStyle();
7071 Style.SpaceInEmptyBlock = true;
7072 verifyFormat("void f() { }", "void f() {}", Style);
7073 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
7074 verifyFormat("{ }", Style);
7075 verifyFormat("while (true) { }", "while (true) {}", Style);
7076 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7077 Style.BraceWrapping.BeforeElse = false;
7078 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
7079 verifyFormat("if (a)\n"
7080 "{\n"
7081 "} else if (b)\n"
7082 "{\n"
7083 "} else\n"
7084 "{ }",
7085 Style);
7086 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
7087 verifyFormat("if (a) {\n"
7088 "} else if (b) {\n"
7089 "} else {\n"
7090 "}",
7091 Style);
7092 Style.BraceWrapping.BeforeElse = true;
7093 verifyFormat("if (a) { }\n"
7094 "else if (b) { }\n"
7095 "else { }",
7096 Style);
7097
7098 Style = getLLVMStyle(Language: FormatStyle::LK_CSharp);
7099 Style.SpaceInEmptyBlock = true;
7100 verifyFormat("Event += () => { };", Style);
7101}
7102
7103TEST_F(FormatTest, FormatBeginBlockEndMacros) {
7104 FormatStyle Style = getLLVMStyle();
7105 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
7106 Style.MacroBlockEnd = "^[A-Z_]+_END$";
7107 verifyFormat("FOO_BEGIN\n"
7108 " FOO_ENTRY\n"
7109 "FOO_END",
7110 Style);
7111 verifyFormat("FOO_BEGIN\n"
7112 " NESTED_FOO_BEGIN\n"
7113 " NESTED_FOO_ENTRY\n"
7114 " NESTED_FOO_END\n"
7115 "FOO_END",
7116 Style);
7117 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7118 " int x;\n"
7119 " x = 1;\n"
7120 "FOO_END(Baz)",
7121 Style);
7122
7123 Style.RemoveBracesLLVM = true;
7124 verifyNoCrash(Code: "for (;;)\n"
7125 " FOO_BEGIN\n"
7126 " foo();\n"
7127 " FOO_END",
7128 Style);
7129}
7130
7131//===----------------------------------------------------------------------===//
7132// Line break tests.
7133//===----------------------------------------------------------------------===//
7134
7135TEST_F(FormatTest, PreventConfusingIndents) {
7136 verifyFormat(
7137 "void f() {\n"
7138 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7139 " parameter, parameter, parameter)),\n"
7140 " SecondLongCall(parameter));\n"
7141 "}");
7142 verifyFormat(
7143 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7144 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7146 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7147 verifyFormat(
7148 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7149 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7150 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7151 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7152 verifyFormat(
7153 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7154 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7156 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7157 verifyFormat("int a = bbbb && ccc &&\n"
7158 " fffff(\n"
7159 "#define A Just forcing a new line\n"
7160 " ddd);");
7161}
7162
7163TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
7164 verifyFormat(
7165 "bool aaaaaaa =\n"
7166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7167 " bbbbbbbb();");
7168 verifyFormat(
7169 "bool aaaaaaa =\n"
7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7171 " bbbbbbbb();");
7172
7173 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7175 " ccccccccc == ddddddddddd;");
7176 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7178 " ccccccccc == ddddddddddd;");
7179 verifyFormat(
7180 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7182 " ccccccccc == ddddddddddd;");
7183
7184 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7185 " aaaaaa) &&\n"
7186 " bbbbbb && cccccc;");
7187 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7188 " aaaaaa) >>\n"
7189 " bbbbbb;");
7190 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7191 " SourceMgr.getSpellingColumnNumber(\n"
7192 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7193 " 1);");
7194
7195 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7196 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7197 " cccccc) {\n}");
7198 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7199 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7200 " cccccc) {\n}");
7201 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7202 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7203 " cccccc) {\n}");
7204 verifyFormat("b = a &&\n"
7205 " // Comment\n"
7206 " b.c && d;");
7207
7208 // If the LHS of a comparison is not a binary expression itself, the
7209 // additional linebreak confuses many people.
7210 verifyFormat(
7211 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7213 "}");
7214 verifyFormat(
7215 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7217 "}");
7218 verifyFormat(
7219 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7221 "}");
7222 verifyFormat(
7223 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7224 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7225 "}");
7226 // Even explicit parentheses stress the precedence enough to make the
7227 // additional break unnecessary.
7228 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7230 "}");
7231 // This cases is borderline, but with the indentation it is still readable.
7232 verifyFormat(
7233 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7234 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7236 "}",
7237 getLLVMStyleWithColumns(75));
7238
7239 // If the LHS is a binary expression, we should still use the additional break
7240 // as otherwise the formatting hides the operator precedence.
7241 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7243 " 5) {\n"
7244 "}");
7245 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7247 " 5) {\n"
7248 "}");
7249
7250 FormatStyle OnePerLine = getLLVMStyle();
7251 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7252 verifyFormat(
7253 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7256 OnePerLine);
7257
7258 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7259 " .aaa(aaaaaaaaaaaaa) *\n"
7260 " aaaaaaa +\n"
7261 " aaaaaaa;",
7262 getLLVMStyleWithColumns(40));
7263}
7264
7265TEST_F(FormatTest, ExpressionIndentation) {
7266 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7270 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7271 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7274 " ccccccccccccccccccccccccccccccccccccccccc;");
7275 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7278 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7279 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7282 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7283 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7286 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7287 verifyFormat("if () {\n"
7288 "} else if (aaaaa && bbbbb > // break\n"
7289 " ccccc) {\n"
7290 "}");
7291 verifyFormat("if () {\n"
7292 "} else if constexpr (aaaaa && bbbbb > // break\n"
7293 " ccccc) {\n"
7294 "}");
7295 verifyFormat("if () {\n"
7296 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7297 " ccccc) {\n"
7298 "}");
7299 verifyFormat("if () {\n"
7300 "} else if (aaaaa &&\n"
7301 " bbbbb > // break\n"
7302 " ccccc &&\n"
7303 " ddddd) {\n"
7304 "}");
7305
7306 // Presence of a trailing comment used to change indentation of b.
7307 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7308 " b;\n"
7309 "return aaaaaaaaaaaaaaaaaaa +\n"
7310 " b; //",
7311 getLLVMStyleWithColumns(30));
7312}
7313
7314TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
7315 // Not sure what the best system is here. Like this, the LHS can be found
7316 // immediately above an operator (everything with the same or a higher
7317 // indent). The RHS is aligned right of the operator and so compasses
7318 // everything until something with the same indent as the operator is found.
7319 // FIXME: Is this a good system?
7320 FormatStyle Style = getLLVMStyle();
7321 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7322 verifyFormat(
7323 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7324 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7325 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7326 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7327 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7328 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7329 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7330 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7331 " > ccccccccccccccccccccccccccccccccccccccccc;",
7332 Style);
7333 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7334 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7335 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7336 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7337 Style);
7338 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7339 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7340 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7341 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7342 Style);
7343 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7344 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7345 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7346 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7347 Style);
7348 verifyFormat("if () {\n"
7349 "} else if (aaaaa\n"
7350 " && bbbbb // break\n"
7351 " > ccccc) {\n"
7352 "}",
7353 Style);
7354 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7355 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7356 Style);
7357 verifyFormat("return (a)\n"
7358 " // comment\n"
7359 " + b;",
7360 Style);
7361 verifyFormat(
7362 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7363 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7364 " + cc;",
7365 Style);
7366
7367 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7368 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7369 Style);
7370
7371 // Forced by comments.
7372 verifyFormat(
7373 "unsigned ContentSize =\n"
7374 " sizeof(int16_t) // DWARF ARange version number\n"
7375 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7376 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7377 " + sizeof(int8_t); // Segment Size (in bytes)");
7378
7379 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7380 " == boost::fusion::at_c<1>(iiii).second;",
7381 Style);
7382
7383 Style.ColumnLimit = 60;
7384 verifyFormat("zzzzzzzzzz\n"
7385 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7386 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7387 Style);
7388
7389 Style.ColumnLimit = 80;
7390 Style.IndentWidth = 4;
7391 Style.TabWidth = 4;
7392 Style.UseTab = FormatStyle::UT_Always;
7393 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7394 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7395 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7396 "\t&& (someOtherLongishConditionPart1\n"
7397 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7398 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7399 "(someOtherLongishConditionPart1 || "
7400 "someOtherEvenLongerNestedConditionPart2);",
7401 Style);
7402
7403 Style = getLLVMStyleWithColumns(ColumnLimit: 20);
7404 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7405 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7406 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7407 Style.ContinuationIndentWidth = 2;
7408 verifyFormat("struct Foo {\n"
7409 " Foo(\n"
7410 " int arg1,\n"
7411 " int arg2)\n"
7412 " : Base(\n"
7413 " arg1,\n"
7414 " arg2) {}\n"
7415 "};",
7416 Style);
7417 verifyFormat("return abc\n"
7418 " ? foo(\n"
7419 " a,\n"
7420 " b,\n"
7421 " bar(\n"
7422 " abc))\n"
7423 " : g(abc);",
7424 Style);
7425}
7426
7427TEST_F(FormatTest, ExpressionIndentationStrictAlign) {
7428 FormatStyle Style = getLLVMStyle();
7429 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7430 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
7431
7432 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7433 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7434 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7435 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7436 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7437 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7438 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7439 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7440 " > ccccccccccccccccccccccccccccccccccccccccc;",
7441 Style);
7442 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7443 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7444 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7445 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7446 Style);
7447 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7448 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7449 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7450 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7451 Style);
7452 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7453 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7454 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7455 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7456 Style);
7457 verifyFormat("if () {\n"
7458 "} else if (aaaaa\n"
7459 " && bbbbb // break\n"
7460 " > ccccc) {\n"
7461 "}",
7462 Style);
7463 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7464 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7465 Style);
7466 verifyFormat("return (a)\n"
7467 " // comment\n"
7468 " + b;",
7469 Style);
7470 verifyFormat(
7471 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7472 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7473 " + cc;",
7474 Style);
7475 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7476 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7477 " : 3333333333333333;",
7478 Style);
7479 verifyFormat(
7480 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7481 " : ccccccccccccccc ? dddddddddddddddddd\n"
7482 " : eeeeeeeeeeeeeeeeee)\n"
7483 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7484 " : 3333333333333333;",
7485 Style);
7486 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7487 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7488 Style);
7489
7490 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7491 " == boost::fusion::at_c<1>(iiii).second;",
7492 Style);
7493
7494 Style.ColumnLimit = 60;
7495 verifyFormat("zzzzzzzzzzzzz\n"
7496 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7497 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7498 Style);
7499
7500 // Forced by comments.
7501 Style.ColumnLimit = 80;
7502 verifyFormat(
7503 "unsigned ContentSize\n"
7504 " = sizeof(int16_t) // DWARF ARange version number\n"
7505 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7506 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7507 " + sizeof(int8_t); // Segment Size (in bytes)",
7508 Style);
7509
7510 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7511 verifyFormat(
7512 "unsigned ContentSize =\n"
7513 " sizeof(int16_t) // DWARF ARange version number\n"
7514 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7515 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7516 " + sizeof(int8_t); // Segment Size (in bytes)",
7517 Style);
7518
7519 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7520 verifyFormat(
7521 "unsigned ContentSize =\n"
7522 " sizeof(int16_t) // DWARF ARange version number\n"
7523 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7524 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7525 " + sizeof(int8_t); // Segment Size (in bytes)",
7526 Style);
7527}
7528
7529TEST_F(FormatTest, EnforcedOperatorWraps) {
7530 // Here we'd like to wrap after the || operators, but a comment is forcing an
7531 // earlier wrap.
7532 verifyFormat("bool x = aaaaa //\n"
7533 " || bbbbb\n"
7534 " //\n"
7535 " || cccc;");
7536}
7537
7538TEST_F(FormatTest, NoOperandAlignment) {
7539 FormatStyle Style = getLLVMStyle();
7540 Style.AlignOperands = FormatStyle::OAS_DontAlign;
7541 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7544 Style);
7545 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7546 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7547 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7548 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7549 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7550 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7551 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7552 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7553 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7554 " > ccccccccccccccccccccccccccccccccccccccccc;",
7555 Style);
7556
7557 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7558 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7559 " + cc;",
7560 Style);
7561 verifyFormat("int a = aa\n"
7562 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7563 " * cccccccccccccccccccccccccccccccccccc;",
7564 Style);
7565
7566 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
7567 verifyFormat("return (a > b\n"
7568 " // comment1\n"
7569 " // comment2\n"
7570 " || c);",
7571 Style);
7572}
7573
7574TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
7575 FormatStyle Style = getLLVMStyle();
7576 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7577 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7579 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7580 Style);
7581}
7582
7583TEST_F(FormatTest, AllowBinPackingInsideArguments) {
7584 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
7585 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7586 Style.BinPackArguments = false;
7587 verifyFormat("void test() {\n"
7588 " someFunction(\n"
7589 " this + argument + is + quite\n"
7590 " + long + so + it + gets + wrapped\n"
7591 " + but + remains + bin - packed);\n"
7592 "}",
7593 Style);
7594 verifyFormat("void test() {\n"
7595 " someFunction(arg1,\n"
7596 " this + argument + is\n"
7597 " + quite + long + so\n"
7598 " + it + gets + wrapped\n"
7599 " + but + remains + bin\n"
7600 " - packed,\n"
7601 " arg3);\n"
7602 "}",
7603 Style);
7604 verifyFormat("void test() {\n"
7605 " someFunction(\n"
7606 " arg1,\n"
7607 " this + argument + has\n"
7608 " + anotherFunc(nested,\n"
7609 " calls + whose\n"
7610 " + arguments\n"
7611 " + are + also\n"
7612 " + wrapped,\n"
7613 " in + addition)\n"
7614 " + to + being + bin - packed,\n"
7615 " arg3);\n"
7616 "}",
7617 Style);
7618
7619 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7620 verifyFormat("void test() {\n"
7621 " someFunction(\n"
7622 " arg1,\n"
7623 " this + argument + has +\n"
7624 " anotherFunc(nested,\n"
7625 " calls + whose +\n"
7626 " arguments +\n"
7627 " are + also +\n"
7628 " wrapped,\n"
7629 " in + addition) +\n"
7630 " to + being + bin - packed,\n"
7631 " arg3);\n"
7632 "}",
7633 Style);
7634}
7635
7636TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) {
7637 auto Style = getLLVMStyleWithColumns(ColumnLimit: 45);
7638 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None);
7639 verifyFormat("bool b =\n"
7640 " is_default_constructible_v<hash<T>> and\n"
7641 " is_copy_constructible_v<hash<T>> and\n"
7642 " is_move_constructible_v<hash<T>> and\n"
7643 " is_copy_assignable_v<hash<T>> and\n"
7644 " is_move_assignable_v<hash<T>> and\n"
7645 " is_destructible_v<hash<T>> and\n"
7646 " is_swappable_v<hash<T>> and\n"
7647 " is_callable_v<hash<T>(T)>;",
7648 Style);
7649
7650 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
7651 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7652 " and is_copy_constructible_v<hash<T>>\n"
7653 " and is_move_constructible_v<hash<T>>\n"
7654 " and is_copy_assignable_v<hash<T>>\n"
7655 " and is_move_assignable_v<hash<T>>\n"
7656 " and is_destructible_v<hash<T>>\n"
7657 " and is_swappable_v<hash<T>>\n"
7658 " and is_callable_v<hash<T>(T)>;",
7659 Style);
7660
7661 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7662 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7663 " and is_copy_constructible_v<hash<T>>\n"
7664 " and is_move_constructible_v<hash<T>>\n"
7665 " and is_copy_assignable_v<hash<T>>\n"
7666 " and is_move_assignable_v<hash<T>>\n"
7667 " and is_destructible_v<hash<T>>\n"
7668 " and is_swappable_v<hash<T>>\n"
7669 " and is_callable_v<hash<T>(T)>;",
7670 Style);
7671}
7672
7673TEST_F(FormatTest, ConstructorInitializers) {
7674 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7675 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7676 getLLVMStyleWithColumns(45));
7677 verifyFormat("Constructor()\n"
7678 " : Inttializer(FitsOnTheLine) {}",
7679 getLLVMStyleWithColumns(44));
7680 verifyFormat("Constructor()\n"
7681 " : Inttializer(FitsOnTheLine) {}",
7682 getLLVMStyleWithColumns(43));
7683
7684 verifyFormat("template <typename T>\n"
7685 "Constructor() : Initializer(FitsOnTheLine) {}",
7686 getLLVMStyleWithColumns(45));
7687
7688 verifyFormat(
7689 "SomeClass::Constructor()\n"
7690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7691
7692 verifyFormat(
7693 "SomeClass::Constructor()\n"
7694 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7695 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7696 verifyFormat(
7697 "SomeClass::Constructor()\n"
7698 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7699 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7700 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7701 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7702 " : aaaaaaaaaa(aaaaaa) {}");
7703
7704 verifyFormat("Constructor()\n"
7705 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7706 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7707 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7708 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7709
7710 verifyFormat("Constructor()\n"
7711 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7713
7714 verifyFormat("Constructor(int Parameter = 0)\n"
7715 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7716 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7717 verifyFormat("Constructor()\n"
7718 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7719 "}",
7720 getLLVMStyleWithColumns(60));
7721 verifyFormat("Constructor()\n"
7722 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7723 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7724
7725 // Here a line could be saved by splitting the second initializer onto two
7726 // lines, but that is not desirable.
7727 verifyFormat("Constructor()\n"
7728 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7729 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7730 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7731
7732 FormatStyle OnePerLine = getLLVMStyle();
7733 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never;
7734 verifyFormat("MyClass::MyClass()\n"
7735 " : a(a),\n"
7736 " b(b),\n"
7737 " c(c) {}",
7738 OnePerLine);
7739 verifyFormat("MyClass::MyClass()\n"
7740 " : a(a), // comment\n"
7741 " b(b),\n"
7742 " c(c) {}",
7743 OnePerLine);
7744 verifyFormat("MyClass::MyClass(int a)\n"
7745 " : b(a), // comment\n"
7746 " c(a + 1) { // lined up\n"
7747 "}",
7748 OnePerLine);
7749 verifyFormat("Constructor()\n"
7750 " : a(b, b, b) {}",
7751 OnePerLine);
7752 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7753 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
7754 verifyFormat("SomeClass::Constructor()\n"
7755 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7756 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7757 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7758 OnePerLine);
7759 verifyFormat("SomeClass::Constructor()\n"
7760 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7761 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7762 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7763 OnePerLine);
7764 verifyFormat("MyClass::MyClass(int var)\n"
7765 " : some_var_(var), // 4 space indent\n"
7766 " some_other_var_(var + 1) { // lined up\n"
7767 "}",
7768 OnePerLine);
7769 verifyFormat("Constructor()\n"
7770 " : aaaaa(aaaaaa),\n"
7771 " aaaaa(aaaaaa),\n"
7772 " aaaaa(aaaaaa),\n"
7773 " aaaaa(aaaaaa),\n"
7774 " aaaaa(aaaaaa) {}",
7775 OnePerLine);
7776 verifyFormat("Constructor()\n"
7777 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7778 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7779 OnePerLine);
7780 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7781 verifyFormat(
7782 "Constructor()\n"
7783 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7784 " aaaaaaaaaaa().aaa(),\n"
7785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7786 OnePerLine);
7787 OnePerLine.ColumnLimit = 60;
7788 verifyFormat("Constructor()\n"
7789 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7790 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7791 OnePerLine);
7792
7793 verifyFormat("Constructor()\n"
7794 " : // Comment forcing unwanted break.\n"
7795 " aaaa(aaaa) {}",
7796 "Constructor() :\n"
7797 " // Comment forcing unwanted break.\n"
7798 " aaaa(aaaa) {}");
7799}
7800
7801TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) {
7802 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
7803 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7804 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
7805
7806 for (int i = 0; i < 4; ++i) {
7807 // Test all combinations of parameters that should not have an effect.
7808 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
7809 Style.AllowAllArgumentsOnNextLine = i & 2;
7810
7811 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7812 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7813 verifyFormat("Constructor()\n"
7814 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7815 Style);
7816 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7817
7818 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7819 verifyFormat("Constructor()\n"
7820 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7821 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7822 Style);
7823 verifyFormat("Constructor() : a(a), b(b) {}", Style);
7824
7825 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7826 verifyFormat("Constructor()\n"
7827 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7828 Style);
7829 verifyFormat("Constructor()\n"
7830 " : a(a), b(b) {}",
7831 Style);
7832 verifyFormat("Constructor()\n"
7833 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7834 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7835 " , cccccccccccccccccccccc(c) {}",
7836 Style);
7837
7838 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7839 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7840 verifyFormat("Constructor()\n"
7841 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7842 Style);
7843
7844 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7845 verifyFormat("Constructor()\n"
7846 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7847 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7848 Style);
7849
7850 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7851 verifyFormat("Constructor()\n"
7852 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7853 Style);
7854 verifyFormat("Constructor()\n"
7855 " : a(a), b(b) {}",
7856 Style);
7857 verifyFormat("Constructor()\n"
7858 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7859 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7860 " cccccccccccccccccccccc(c) {}",
7861 Style);
7862
7863 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7864 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7865 verifyFormat("Constructor() :\n"
7866 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7867 Style);
7868
7869 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7870 verifyFormat("Constructor() :\n"
7871 " aaaaaaaaaaaaaaaaaa(a),\n"
7872 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7873 Style);
7874
7875 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7876 verifyFormat("Constructor() :\n"
7877 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7878 Style);
7879 verifyFormat("Constructor() :\n"
7880 " a(a), b(b) {}",
7881 Style);
7882 verifyFormat("Constructor() :\n"
7883 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7884 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7885 " cccccccccccccccccccccc(c) {}",
7886 Style);
7887 }
7888
7889 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7890 // AllowAllConstructorInitializersOnNextLine in all
7891 // BreakConstructorInitializers modes
7892 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
7893 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7894 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7895 verifyFormat("SomeClassWithALongName::Constructor(\n"
7896 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7897 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7898 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7899 Style);
7900
7901 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7902 verifyFormat("SomeClassWithALongName::Constructor(\n"
7903 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7904 " int bbbbbbbbbbbbb,\n"
7905 " int cccccccccccccccc)\n"
7906 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7907 Style);
7908
7909 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7910 verifyFormat("SomeClassWithALongName::Constructor(\n"
7911 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7912 " int bbbbbbbbbbbbb,\n"
7913 " int cccccccccccccccc)\n"
7914 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7915 Style);
7916
7917 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7918 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7919 verifyFormat("SomeClassWithALongName::Constructor(\n"
7920 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7921 " int bbbbbbbbbbbbb)\n"
7922 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7923 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7924 Style);
7925
7926 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
7927
7928 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7929 verifyFormat("SomeClassWithALongName::Constructor(\n"
7930 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7931 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7932 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7933 Style);
7934
7935 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7936 verifyFormat("SomeClassWithALongName::Constructor(\n"
7937 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7938 " int bbbbbbbbbbbbb,\n"
7939 " int cccccccccccccccc)\n"
7940 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7941 Style);
7942
7943 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7944 verifyFormat("SomeClassWithALongName::Constructor(\n"
7945 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7946 " int bbbbbbbbbbbbb,\n"
7947 " int cccccccccccccccc)\n"
7948 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7949 Style);
7950
7951 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7952 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7953 verifyFormat("SomeClassWithALongName::Constructor(\n"
7954 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7955 " int bbbbbbbbbbbbb)\n"
7956 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7957 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7958 Style);
7959
7960 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
7961 Style.AllowAllParametersOfDeclarationOnNextLine = true;
7962 verifyFormat("SomeClassWithALongName::Constructor(\n"
7963 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7964 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7965 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7966 Style);
7967
7968 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
7969 verifyFormat("SomeClassWithALongName::Constructor(\n"
7970 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7971 " int bbbbbbbbbbbbb,\n"
7972 " int cccccccccccccccc) :\n"
7973 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7974 Style);
7975
7976 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
7977 verifyFormat("SomeClassWithALongName::Constructor(\n"
7978 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7979 " int bbbbbbbbbbbbb,\n"
7980 " int cccccccccccccccc) :\n"
7981 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7982 Style);
7983
7984 Style.AllowAllParametersOfDeclarationOnNextLine = false;
7985 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7986 verifyFormat("SomeClassWithALongName::Constructor(\n"
7987 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7988 " int bbbbbbbbbbbbb) :\n"
7989 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7990 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7991 Style);
7992
7993 Style = getLLVMStyleWithColumns(ColumnLimit: 0);
7994 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
7995 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style);
7996 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7997 " : bar(bar), baz(baz) {}",
7998 Style);
7999}
8000
8001TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
8002 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
8003 Style.BinPackArguments = false;
8004 for (int i = 0; i < 4; ++i) {
8005 // Test all combinations of parameters that should not have an effect.
8006 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1;
8007 Style.PackConstructorInitializers =
8008 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never;
8009
8010 Style.AllowAllArgumentsOnNextLine = true;
8011 verifyFormat("void foo() {\n"
8012 " FunctionCallWithReallyLongName(\n"
8013 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
8014 "}",
8015 Style);
8016 Style.AllowAllArgumentsOnNextLine = false;
8017 verifyFormat("void foo() {\n"
8018 " FunctionCallWithReallyLongName(\n"
8019 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8020 " bbbbbbbbbbbb);\n"
8021 "}",
8022 Style);
8023
8024 Style.AllowAllArgumentsOnNextLine = true;
8025 verifyFormat("void foo() {\n"
8026 " auto VariableWithReallyLongName = {\n"
8027 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
8028 "}",
8029 Style);
8030 Style.AllowAllArgumentsOnNextLine = false;
8031 verifyFormat("void foo() {\n"
8032 " auto VariableWithReallyLongName = {\n"
8033 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8034 " bbbbbbbbbbbb};\n"
8035 "}",
8036 Style);
8037 }
8038
8039 // This parameter should not affect declarations.
8040 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8041 Style.AllowAllArgumentsOnNextLine = false;
8042 Style.AllowAllParametersOfDeclarationOnNextLine = true;
8043 verifyFormat("void FunctionCallWithReallyLongName(\n"
8044 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
8045 Style);
8046 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8047 verifyFormat("void FunctionCallWithReallyLongName(\n"
8048 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
8049 " int bbbbbbbbbbbb);",
8050 Style);
8051}
8052
8053TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
8054 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
8055 // and BAS_Align.
8056 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 35);
8057 StringRef Input = "functionCall(paramA, paramB, paramC);\n"
8058 "void functionDecl(int A, int B, int C);";
8059 Style.AllowAllArgumentsOnNextLine = false;
8060 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8061 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8062 " paramC);\n"
8063 "void functionDecl(int A, int B,\n"
8064 " int C);"),
8065 Input, Style);
8066 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8067 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8068 " paramC);\n"
8069 "void functionDecl(int A, int B,\n"
8070 " int C);"),
8071 Input, Style);
8072 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
8073 // AllowAllArgumentsOnNextLine.
8074 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8075 verifyFormat(StringRef("functionCall(\n"
8076 " paramA, paramB, paramC);\n"
8077 "void functionDecl(\n"
8078 " int A, int B, int C);"),
8079 Input, Style);
8080 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
8081 verifyFormat("functionCall(\n"
8082 " paramA, paramB, paramC\n"
8083 ");\n"
8084 "void functionDecl(\n"
8085 " int A, int B, int C\n"
8086 ");",
8087 Input, Style);
8088
8089 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8090 // first argument.
8091 Style.AllowAllArgumentsOnNextLine = true;
8092 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8093 verifyFormat(StringRef("functionCall(\n"
8094 " paramA, paramB, paramC);\n"
8095 "void functionDecl(\n"
8096 " int A, int B, int C);"),
8097 Input, Style);
8098 // It wouldn't fit on one line with aligned parameters so this setting
8099 // doesn't change anything for BAS_Align.
8100 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
8101 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8102 " paramC);\n"
8103 "void functionDecl(int A, int B,\n"
8104 " int C);"),
8105 Input, Style);
8106 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
8107 verifyFormat(StringRef("functionCall(\n"
8108 " paramA, paramB, paramC);\n"
8109 "void functionDecl(\n"
8110 " int A, int B, int C);"),
8111 Input, Style);
8112}
8113
8114TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
8115 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
8116 "void emptyFunctionDefinition() {}\n"
8117 "void functionDefinition(int A, int B, int C) {}\n"
8118 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8119 verifyFormat(Input);
8120
8121 FormatStyle Style = getLLVMStyle();
8122 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters);
8123 Style.BreakFunctionDefinitionParameters = true;
8124 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8125 "void emptyFunctionDefinition() {}\n"
8126 "void functionDefinition(\n"
8127 " int A, int B, int C) {}\n"
8128 "Class::Class(\n"
8129 " int A, int B)\n"
8130 " : m_A(A), m_B(B) {}",
8131 Input, Style);
8132
8133 // Test the style where all parameters are on their own lines.
8134 Style.AllowAllParametersOfDeclarationOnNextLine = false;
8135 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8136 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8137 "void emptyFunctionDefinition() {}\n"
8138 "void functionDefinition(\n"
8139 " int A,\n"
8140 " int B,\n"
8141 " int C) {}\n"
8142 "Class::Class(\n"
8143 " int A,\n"
8144 " int B)\n"
8145 " : m_A(A), m_B(B) {}",
8146 Input, Style);
8147}
8148
8149TEST_F(FormatTest, BreakBeforeInlineASMColon) {
8150 FormatStyle Style = getLLVMStyle();
8151 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
8152 /* Test the behaviour with long lines */
8153 Style.ColumnLimit = 40;
8154 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8155 " : : val);",
8156 Style);
8157 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8158 " : val1 : val2);",
8159 Style);
8160 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8161 " \"cpuid\\n\\t\"\n"
8162 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8163 " : \"=a\" : \"a\");",
8164 Style);
8165 Style.ColumnLimit = 80;
8166 verifyFormat("asm volatile(\"string\", : : val);", Style);
8167 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style);
8168
8169 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always;
8170 verifyFormat("asm volatile(\"string\",\n"
8171 " :\n"
8172 " : val);",
8173 Style);
8174 verifyFormat("asm volatile(\"string\",\n"
8175 " : val1\n"
8176 " : val2);",
8177 Style);
8178 /* Test the behaviour with long lines */
8179 Style.ColumnLimit = 40;
8180 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8181 " \"cpuid\\n\\t\"\n"
8182 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8183 " : \"=a\"(*rEAX)\n"
8184 " : \"a\"(value));",
8185 Style);
8186 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8187 " \"cpuid\\n\\t\"\n"
8188 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8189 " :\n"
8190 " : \"a\"(value));",
8191 Style);
8192 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8193 " :\n"
8194 " : val);",
8195 Style);
8196 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8197 " : val1\n"
8198 " : val2);",
8199 Style);
8200}
8201
8202TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
8203 FormatStyle Style = getLLVMStyle();
8204 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
8205
8206 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8207 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8208 getStyleWithColumns(Style, 45));
8209 verifyFormat("Constructor() :\n"
8210 " Initializer(FitsOnTheLine) {}",
8211 getStyleWithColumns(Style, 44));
8212 verifyFormat("Constructor() :\n"
8213 " Initializer(FitsOnTheLine) {}",
8214 getStyleWithColumns(Style, 43));
8215
8216 verifyFormat("template <typename T>\n"
8217 "Constructor() : Initializer(FitsOnTheLine) {}",
8218 getStyleWithColumns(Style, 50));
8219 verifyFormat(
8220 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8221 " int mooooooooooooore) noexcept :\n"
8222 " Super{some, arguments}, Member{5}, Member2{2} {}",
8223 Style);
8224 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8225 verifyFormat(
8226 "SomeClass::Constructor() :\n"
8227 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8228 Style);
8229 verifyFormat(
8230 "SomeClass::Constructor() : // NOLINT\n"
8231 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8232 Style);
8233
8234 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8235 verifyFormat(
8236 "SomeClass::Constructor() :\n"
8237 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8238 Style);
8239 verifyFormat(
8240 "SomeClass::Constructor() : // NOLINT\n"
8241 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8242 Style);
8243
8244 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
8245 verifyFormat(
8246 "SomeClass::Constructor() :\n"
8247 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8248 Style);
8249
8250 verifyFormat(
8251 "SomeClass::Constructor() :\n"
8252 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8253 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8254 Style);
8255 verifyFormat(
8256 "SomeClass::Constructor() :\n"
8257 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8258 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8259 Style);
8260 verifyFormat(
8261 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8262 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8263 Style);
8264
8265 verifyFormat("Constructor() :\n"
8266 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8267 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8268 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8269 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8270 Style);
8271
8272 verifyFormat("Constructor() :\n"
8273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8275 Style);
8276
8277 verifyFormat("Constructor(int Parameter = 0) :\n"
8278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8279 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8280 Style);
8281 verifyFormat("Constructor() :\n"
8282 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8283 "}",
8284 getStyleWithColumns(Style, 60));
8285 verifyFormat("Constructor() :\n"
8286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8287 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8288 Style);
8289
8290 // Here a line could be saved by splitting the second initializer onto two
8291 // lines, but that is not desirable.
8292 verifyFormat("Constructor() :\n"
8293 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8294 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8295 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8296 Style);
8297
8298 FormatStyle OnePerLine = Style;
8299 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
8300 verifyFormat("SomeClass::Constructor() :\n"
8301 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8302 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8303 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8304 OnePerLine);
8305 verifyFormat("SomeClass::Constructor() :\n"
8306 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8307 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8308 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8309 OnePerLine);
8310 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8311 " i(i), // comment\n"
8312 " j(j) {}",
8313 OnePerLine);
8314 verifyFormat("MyClass::MyClass(int var) :\n"
8315 " some_var_(var), // 4 space indent\n"
8316 " some_other_var_(var + 1) { // lined up\n"
8317 "}",
8318 OnePerLine);
8319 verifyFormat("Constructor() :\n"
8320 " aaaaa(aaaaaa),\n"
8321 " aaaaa(aaaaaa),\n"
8322 " aaaaa(aaaaaa),\n"
8323 " aaaaa(aaaaaa),\n"
8324 " aaaaa(aaaaaa) {}",
8325 OnePerLine);
8326 verifyFormat("Constructor() :\n"
8327 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8328 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8329 OnePerLine);
8330 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8331 verifyFormat("Constructor() :\n"
8332 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8333 " aaaaaaaaaaa().aaa(),\n"
8334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8335 OnePerLine);
8336 OnePerLine.ColumnLimit = 60;
8337 verifyFormat("Constructor() :\n"
8338 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8339 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8340 OnePerLine);
8341
8342 verifyFormat("Constructor() :\n"
8343 " // Comment forcing unwanted break.\n"
8344 " aaaa(aaaa) {}",
8345 Style);
8346 verifyFormat("Constructor() : // NOLINT\n"
8347 " aaaa(aaaa) {}",
8348 Style);
8349 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8350 " on a single\n"
8351 " // line.\n"
8352 " aaaa(aaaa) {}",
8353 "Constructor() : // A very long trailing comment that cannot fit"
8354 " on a single line.\n"
8355 " aaaa(aaaa) {}",
8356 Style);
8357
8358 Style.ColumnLimit = 0;
8359 verifyNoChange("SomeClass::Constructor() :\n"
8360 " a(a) {}",
8361 Style);
8362 verifyNoChange("SomeClass::Constructor() noexcept :\n"
8363 " a(a) {}",
8364 Style);
8365 verifyNoChange("SomeClass::Constructor() :\n"
8366 " a(a), b(b), c(c) {}",
8367 Style);
8368 verifyNoChange("SomeClass::Constructor() :\n"
8369 " a(a) {\n"
8370 " foo();\n"
8371 " bar();\n"
8372 "}",
8373 Style);
8374 verifyFormat("struct Foo {\n"
8375 " int x;\n"
8376 " Foo() : x(0) {}\n"
8377 "};",
8378 "struct Foo {\n"
8379 " int x;\n"
8380 " Foo():x(0) {}\n"
8381 "};",
8382 Style);
8383
8384 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
8385 verifyNoChange("SomeClass::Constructor() :\n"
8386 " a(a), b(b), c(c) {\n"
8387 "}",
8388 Style);
8389 verifyNoChange("SomeClass::Constructor() :\n"
8390 " a(a) {\n"
8391 "}",
8392 Style);
8393
8394 Style.ColumnLimit = 80;
8395 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
8396 Style.ConstructorInitializerIndentWidth = 2;
8397 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style);
8398 verifyFormat("SomeClass::Constructor() :\n"
8399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8400 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8401 Style);
8402
8403 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8404 // well
8405 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
8406 verifyFormat(
8407 "class SomeClass\n"
8408 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8409 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8410 Style);
8411 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
8412 verifyFormat(
8413 "class SomeClass\n"
8414 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8415 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8416 Style);
8417 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon;
8418 verifyFormat(
8419 "class SomeClass :\n"
8420 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8421 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8422 Style);
8423 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma;
8424 verifyFormat(
8425 "class SomeClass\n"
8426 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8427 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8428 Style);
8429}
8430
8431#ifndef EXPENSIVE_CHECKS
8432// Expensive checks enables libstdc++ checking which includes validating the
8433// state of ranges used in std::priority_queue - this blows out the
8434// runtime/scalability of the function and makes this test unacceptably slow.
8435TEST_F(FormatTest, MemoizationTests) {
8436 // This breaks if the memoization lookup does not take \c Indent and
8437 // \c LastSpace into account.
8438 verifyFormat(
8439 "extern CFRunLoopTimerRef\n"
8440 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8441 " CFTimeInterval interval, CFOptionFlags flags,\n"
8442 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8443 " CFRunLoopTimerContext *context) {}");
8444
8445 // Deep nesting somewhat works around our memoization.
8446 verifyFormat(
8447 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8448 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8449 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8450 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8451 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8452 getLLVMStyleWithColumns(65));
8453 verifyFormat(
8454 "aaaaa(\n"
8455 " aaaaa,\n"
8456 " aaaaa(\n"
8457 " aaaaa,\n"
8458 " aaaaa(\n"
8459 " aaaaa,\n"
8460 " aaaaa(\n"
8461 " aaaaa,\n"
8462 " aaaaa(\n"
8463 " aaaaa,\n"
8464 " aaaaa(\n"
8465 " aaaaa,\n"
8466 " aaaaa(\n"
8467 " aaaaa,\n"
8468 " aaaaa(\n"
8469 " aaaaa,\n"
8470 " aaaaa(\n"
8471 " aaaaa,\n"
8472 " aaaaa(\n"
8473 " aaaaa,\n"
8474 " aaaaa(\n"
8475 " aaaaa,\n"
8476 " aaaaa(\n"
8477 " aaaaa,\n"
8478 " aaaaa))))))))))));",
8479 getLLVMStyleWithColumns(65));
8480 verifyFormat(
8481 "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"
8482 " a),\n"
8483 " a),\n"
8484 " a),\n"
8485 " a),\n"
8486 " a),\n"
8487 " a),\n"
8488 " a),\n"
8489 " a),\n"
8490 " a),\n"
8491 " a),\n"
8492 " a),\n"
8493 " a),\n"
8494 " a),\n"
8495 " a),\n"
8496 " a),\n"
8497 " a),\n"
8498 " a)",
8499 getLLVMStyleWithColumns(65));
8500
8501 // This test takes VERY long when memoization is broken.
8502 FormatStyle OnePerLine = getLLVMStyle();
8503 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
8504 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8505 std::string input = "Constructor()\n"
8506 " : aaaa(a,\n";
8507 for (unsigned i = 0, e = 80; i != e; ++i)
8508 input += " a,\n";
8509 input += " a) {}";
8510 verifyFormat(input, OnePerLine);
8511 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
8512 verifyFormat(input, OnePerLine);
8513}
8514#endif
8515
8516TEST_F(FormatTest, BreaksAsHighAsPossible) {
8517 verifyFormat(
8518 "void f() {\n"
8519 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8520 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8521 " f();\n"
8522 "}");
8523 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8524 " Intervals[i - 1].getRange().getLast()) {\n}");
8525}
8526
8527TEST_F(FormatTest, BreaksFunctionDeclarations) {
8528 // Principially, we break function declarations in a certain order:
8529 // 1) break amongst arguments.
8530 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8531 " Cccccccccccccc cccccccccccccc);");
8532 verifyFormat("template <class TemplateIt>\n"
8533 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8534 " TemplateIt *stop) {}");
8535
8536 // 2) break after return type.
8537 verifyGoogleFormat(
8538 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8539 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8540
8541 // 3) break after (.
8542 verifyGoogleFormat(
8543 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8544 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8545
8546 // 4) break before after nested name specifiers.
8547 verifyGoogleFormat(
8548 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8549 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8550 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8551
8552 // However, there are exceptions, if a sufficient amount of lines can be
8553 // saved.
8554 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8555 // more adjusting.
8556 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8557 " Cccccccccccccc cccccccccc,\n"
8558 " Cccccccccccccc cccccccccc,\n"
8559 " Cccccccccccccc cccccccccc,\n"
8560 " Cccccccccccccc cccccccccc);");
8561 verifyGoogleFormat(
8562 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8563 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8564 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8565 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8566 verifyFormat(
8567 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8568 " Cccccccccccccc cccccccccc,\n"
8569 " Cccccccccccccc cccccccccc,\n"
8570 " Cccccccccccccc cccccccccc,\n"
8571 " Cccccccccccccc cccccccccc,\n"
8572 " Cccccccccccccc cccccccccc,\n"
8573 " Cccccccccccccc cccccccccc);");
8574 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8575 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8576 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8577 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8578 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8579
8580 // Break after multi-line parameters.
8581 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8584 " bbbb bbbb);");
8585 verifyFormat("void SomeLoooooooooooongFunction(\n"
8586 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8587 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8588 " int bbbbbbbbbbbbb);");
8589
8590 // Treat overloaded operators like other functions.
8591 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8592 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8593 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8594 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8595 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8596 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8597 verifyGoogleFormat(
8598 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8599 " const SomeLooooooooogType& a, const SomeLooooooooogType& b);");
8600 verifyGoogleFormat(
8601 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8602 " const SomeLooooooooogType& a, const SomeLooooooooogType& b);");
8603
8604 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8605 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8606 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8607 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8608 verifyGoogleFormat(
8609 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8610 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8611 " bool* aaaaaaaaaaaaaaaaaa, bool* aa) {}");
8612 verifyGoogleFormat("template <typename T>\n"
8613 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8614 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8615 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8616
8617 verifyFormat("extern \"C\" //\n"
8618 " void f();");
8619
8620 auto Style = getLLVMStyle();
8621 Style.PointerAlignment = FormatStyle::PAS_Left;
8622 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8623 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8624 Style);
8625 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8627 Style);
8628
8629 Style = getLLVMStyleWithColumns(ColumnLimit: 45);
8630 Style.PenaltyReturnTypeOnItsOwnLine = 400;
8631 verifyFormat("template <bool abool, // a comment\n"
8632 " bool anotherbool>\n"
8633 "static inline std::pair<size_t, MyCustomType>\n"
8634 "myfunc(const char *buf, const char *&err);",
8635 Style);
8636}
8637
8638TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
8639 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8640 // Prefer keeping `::` followed by `operator` together.
8641 verifyFormat("const aaaa::bbbbbbb &\n"
8642 "ccccccccc::operator++() {\n"
8643 " stuff();\n"
8644 "}",
8645 "const aaaa::bbbbbbb\n"
8646 "&ccccccccc::operator++() { stuff(); }",
8647 getLLVMStyleWithColumns(40));
8648}
8649
8650TEST_F(FormatTest, TrailingReturnType) {
8651 verifyFormat("auto foo() -> int;");
8652 // correct trailing return type spacing
8653 verifyFormat("auto operator->() -> int;");
8654 verifyFormat("auto operator++(int) -> int;");
8655
8656 verifyFormat("struct S {\n"
8657 " auto bar() const -> int;\n"
8658 "};");
8659 verifyFormat("template <size_t Order, typename T>\n"
8660 "auto load_img(const std::string &filename)\n"
8661 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8662 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8663 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8664 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8665 verifyFormat("template <typename T>\n"
8666 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8667 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8668
8669 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 60);
8670 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8671 " auto NAME() -> int { return 42; }",
8672 Style);
8673
8674 // Not trailing return types.
8675 verifyFormat("void f() { auto a = b->c(); }");
8676 verifyFormat("auto a = p->foo();");
8677 verifyFormat("int a = p->foo();");
8678 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8679}
8680
8681TEST_F(FormatTest, DeductionGuides) {
8682 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8683 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8684 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8685 verifyFormat(
8686 "template <class... T>\n"
8687 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8688 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8689 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8690 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8691 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8692 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8693 verifyFormat("template <class T> x() -> x<1>;");
8694 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8695
8696 verifyFormat("A(const char *) -> A<string &>;");
8697 verifyFormat("A() -> A<int>;");
8698
8699 // Ensure not deduction guides.
8700 verifyFormat("c()->f<int>();");
8701 verifyFormat("x()->foo<1>;");
8702 verifyFormat("x = p->foo<3>();");
8703 verifyFormat("x()->x<1>();");
8704}
8705
8706TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
8707 // Avoid breaking before trailing 'const' or other trailing annotations, if
8708 // they are not function-like.
8709 FormatStyle Style = getGoogleStyleWithColumns(ColumnLimit: 47);
8710 verifyFormat("void someLongFunction(\n"
8711 " int someLoooooooooooooongParameter) const {\n}",
8712 getLLVMStyleWithColumns(47));
8713 verifyFormat("LoooooongReturnType\n"
8714 "someLoooooooongFunction() const {}",
8715 getLLVMStyleWithColumns(47));
8716 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8717 " const {}",
8718 Style);
8719 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8720 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8721 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8722 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8723 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8724 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8725 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8726 " aaaaaaaaaaa aaaaa) const override;");
8727 verifyGoogleFormat(
8728 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8729 " const override;");
8730
8731 // Even if the first parameter has to be wrapped.
8732 verifyFormat("void someLongFunction(\n"
8733 " int someLongParameter) const {}",
8734 getLLVMStyleWithColumns(46));
8735 verifyFormat("void someLongFunction(\n"
8736 " int someLongParameter) const {}",
8737 Style);
8738 verifyFormat("void someLongFunction(\n"
8739 " int someLongParameter) override {}",
8740 Style);
8741 verifyFormat("void someLongFunction(\n"
8742 " int someLongParameter) OVERRIDE {}",
8743 Style);
8744 verifyFormat("void someLongFunction(\n"
8745 " int someLongParameter) final {}",
8746 Style);
8747 verifyFormat("void someLongFunction(\n"
8748 " int someLongParameter) FINAL {}",
8749 Style);
8750 verifyFormat("void someLongFunction(\n"
8751 " int parameter) const override {}",
8752 Style);
8753
8754 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
8755 verifyFormat("void someLongFunction(\n"
8756 " int someLongParameter) const\n"
8757 "{\n"
8758 "}",
8759 Style);
8760
8761 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
8762 verifyFormat("void someLongFunction(\n"
8763 " int someLongParameter) const\n"
8764 " {\n"
8765 " }",
8766 Style);
8767
8768 // Unless these are unknown annotations.
8769 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8770 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8771 " LONG_AND_UGLY_ANNOTATION;");
8772
8773 // Breaking before function-like trailing annotations is fine to keep them
8774 // close to their arguments.
8775 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8776 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8777 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8778 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8779 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8780 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8781 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8782 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8783 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8784
8785 verifyFormat(
8786 "void aaaaaaaaaaaaaaaaaa()\n"
8787 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8788 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8789 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8790 " __attribute__((unused));");
8791
8792 Style = getGoogleStyle();
8793
8794 verifyFormat(
8795 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8796 " GUARDED_BY(aaaaaaaaaaaa);",
8797 Style);
8798 verifyFormat(
8799 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8800 " GUARDED_BY(aaaaaaaaaaaa);",
8801 Style);
8802 verifyFormat(
8803 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8804 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8805 Style);
8806 verifyFormat(
8807 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8808 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8809 Style);
8810
8811 verifyFormat(
8812 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8813 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8814 Style);
8815 verifyFormat(
8816 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8817 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8818 Style);
8819 verifyFormat(
8820 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8821 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8822 Style);
8823 verifyFormat(
8824 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8825 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8826 Style);
8827}
8828
8829TEST_F(FormatTest, FunctionAnnotations) {
8830 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8831 "int OldFunction(const string &parameter) {}");
8832 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8833 "string OldFunction(const string &parameter) {}");
8834 verifyFormat("template <typename T>\n"
8835 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8836 "string OldFunction(const string &parameter) {}");
8837
8838 // Not function annotations.
8839 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8840 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8841 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8842 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8843 verifyFormat("MACRO(abc).function() // wrap\n"
8844 " << abc;");
8845 verifyFormat("MACRO(abc)->function() // wrap\n"
8846 " << abc;");
8847 verifyFormat("MACRO(abc)::function() // wrap\n"
8848 " << abc;");
8849 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8850}
8851
8852TEST_F(FormatTest, BreaksDesireably) {
8853 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8854 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8855 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8856 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8858 "}");
8859
8860 verifyFormat(
8861 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8863
8864 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8867
8868 verifyFormat(
8869 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8870 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8872 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8874
8875 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8876 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8877
8878 verifyFormat(
8879 "void f() {\n"
8880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8882 "}");
8883 verifyFormat(
8884 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8886 verifyFormat(
8887 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8889 verifyFormat(
8890 "aaaaaa(aaa,\n"
8891 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8893 " aaaa);");
8894 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8897
8898 // Indent consistently independent of call expression and unary operator.
8899 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8900 " dddddddddddddddddddddddddddddd));");
8901 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8902 " dddddddddddddddddddddddddddddd));");
8903 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8904 " dddddddddddddddddddddddddddddd));");
8905
8906 // This test case breaks on an incorrect memoization, i.e. an optimization not
8907 // taking into account the StopAt value.
8908 verifyFormat(
8909 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8910 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8911 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8912 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8913
8914 verifyFormat("{\n {\n {\n"
8915 " Annotation.SpaceRequiredBefore =\n"
8916 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8917 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8918 " }\n }\n}");
8919
8920 // Break on an outer level if there was a break on an inner level.
8921 verifyFormat("f(g(h(a, // comment\n"
8922 " b, c),\n"
8923 " d, e),\n"
8924 " x, y);",
8925 "f(g(h(a, // comment\n"
8926 " b, c), d, e), x, y);");
8927
8928 // Prefer breaking similar line breaks.
8929 verifyFormat(
8930 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8931 " NSTrackingMouseEnteredAndExited |\n"
8932 " NSTrackingActiveAlways;");
8933}
8934
8935TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
8936 FormatStyle NoBinPacking = getGoogleStyle();
8937 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8938 NoBinPacking.BinPackArguments = true;
8939 verifyFormat("void f() {\n"
8940 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8942 "}",
8943 NoBinPacking);
8944 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8945 " int aaaaaaaaaaaaaaaaaaaa,\n"
8946 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8947 NoBinPacking);
8948
8949 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
8950 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8951 " vector<int> bbbbbbbbbbbbbbb);",
8952 NoBinPacking);
8953 // FIXME: This behavior difference is probably not wanted. However, currently
8954 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8955 // template arguments from BreakBeforeParameter being set because of the
8956 // one-per-line formatting.
8957 verifyFormat(
8958 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8959 " aaaaaaaaaa> aaaaaaaaaa);",
8960 NoBinPacking);
8961 verifyFormat(
8962 "void fffffffffff(\n"
8963 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8964 " aaaaaaaaaa);");
8965}
8966
8967TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
8968 FormatStyle NoBinPacking = getGoogleStyle();
8969 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
8970 NoBinPacking.BinPackArguments = false;
8971 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8972 " aaaaaaaaaaaaaaaaaaaa,\n"
8973 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8974 NoBinPacking);
8975 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8976 " aaaaaaaaaaaaa,\n"
8977 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8978 NoBinPacking);
8979 verifyFormat(
8980 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8981 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8983 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8985 NoBinPacking);
8986 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8987 " .aaaaaaaaaaaaaaaaaa();",
8988 NoBinPacking);
8989 verifyFormat("void f() {\n"
8990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8991 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8992 "}",
8993 NoBinPacking);
8994
8995 verifyFormat(
8996 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8997 " aaaaaaaaaaaa,\n"
8998 " aaaaaaaaaaaa);",
8999 NoBinPacking);
9000 verifyFormat(
9001 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
9002 " ddddddddddddddddddddddddddddd),\n"
9003 " test);",
9004 NoBinPacking);
9005
9006 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
9007 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
9008 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
9009 " aaaaaaaaaaaaaaaaaa;",
9010 NoBinPacking);
9011 verifyFormat("a(\"a\"\n"
9012 " \"a\",\n"
9013 " a);");
9014
9015 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
9016 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
9017 " aaaaaaaaa,\n"
9018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9019 NoBinPacking);
9020 verifyFormat(
9021 "void f() {\n"
9022 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
9023 " .aaaaaaa();\n"
9024 "}",
9025 NoBinPacking);
9026 verifyFormat(
9027 "template <class SomeType, class SomeOtherType>\n"
9028 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
9029 NoBinPacking);
9030}
9031
9032TEST_F(FormatTest, FormatsDeclarationBreakAlways) {
9033 FormatStyle BreakAlways = getGoogleStyle();
9034 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
9035 verifyFormat("void f(int a,\n"
9036 " int b);",
9037 BreakAlways);
9038 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9039 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
9040 " int cccccccccccccccccccccccc);",
9041 BreakAlways);
9042
9043 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
9044 // to BPPS_AlwaysOnePerLine.
9045 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9046 verifyFormat(
9047 "void someLongFunctionName(\n"
9048 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9049 " int b);",
9050 BreakAlways);
9051 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9052 verifyFormat(
9053 "void someLongFunctionName(\n"
9054 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9055 " int b\n"
9056 ");",
9057 BreakAlways);
9058}
9059
9060TEST_F(FormatTest, FormatsDefinitionBreakAlways) {
9061 FormatStyle BreakAlways = getGoogleStyle();
9062 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine;
9063 verifyFormat("void f(int a,\n"
9064 " int b) {\n"
9065 " f(a, b);\n"
9066 "}",
9067 BreakAlways);
9068
9069 // Ensure BinPackArguments interact correctly when BinPackParameters is set to
9070 // BPPS_AlwaysOnePerLine.
9071 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9072 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
9073 " int cccccccccccccccccccccccc) {\n"
9074 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
9075 " cccccccccccccccccccccccc);\n"
9076 "}",
9077 BreakAlways);
9078 BreakAlways.BinPackArguments = false;
9079 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9080 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
9081 " int cccccccccccccccccccccccc) {\n"
9082 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9083 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
9084 " cccccccccccccccccccccccc);\n"
9085 "}",
9086 BreakAlways);
9087
9088 // Ensure BreakFunctionDefinitionParameters interacts correctly when
9089 // BinPackParameters is set to BPPS_AlwaysOnePerLine.
9090 BreakAlways.BreakFunctionDefinitionParameters = true;
9091 verifyFormat("void f(\n"
9092 " int a,\n"
9093 " int b) {\n"
9094 " f(a, b);\n"
9095 "}",
9096 BreakAlways);
9097 BreakAlways.BreakFunctionDefinitionParameters = false;
9098
9099 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
9100 // to BPPS_AlwaysOnePerLine.
9101 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9102 verifyFormat(
9103 "void someLongFunctionName(\n"
9104 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9105 " int b) {\n"
9106 " someLongFunctionName(\n"
9107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
9108 "}",
9109 BreakAlways);
9110 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9111 verifyFormat(
9112 "void someLongFunctionName(\n"
9113 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9114 " int b\n"
9115 ") {\n"
9116 " someLongFunctionName(\n"
9117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n"
9118 " );\n"
9119 "}",
9120 BreakAlways);
9121}
9122
9123TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
9124 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 15);
9125 Style.ExperimentalAutoDetectBinPacking = true;
9126 verifyFormat("aaa(aaaa,\n"
9127 " aaaa,\n"
9128 " aaaa);\n"
9129 "aaa(aaaa,\n"
9130 " aaaa,\n"
9131 " aaaa);",
9132 "aaa(aaaa,\n" // one-per-line
9133 " aaaa,\n"
9134 " aaaa );\n"
9135 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9136 Style);
9137 verifyFormat("aaa(aaaa, aaaa,\n"
9138 " aaaa);\n"
9139 "aaa(aaaa, aaaa,\n"
9140 " aaaa);",
9141 "aaa(aaaa, aaaa,\n" // bin-packed
9142 " aaaa );\n"
9143 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9144 Style);
9145}
9146
9147TEST_F(FormatTest, IndentExportBlock) {
9148 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80);
9149 Style.IndentExportBlock = true;
9150 verifyFormat("export {\n"
9151 " int x;\n"
9152 " int y;\n"
9153 "}",
9154 "export {\n"
9155 "int x;\n"
9156 "int y;\n"
9157 "}",
9158 Style);
9159
9160 Style.IndentExportBlock = false;
9161 verifyFormat("export {\n"
9162 "int x;\n"
9163 "int y;\n"
9164 "}",
9165 "export {\n"
9166 " int x;\n"
9167 " int y;\n"
9168 "}",
9169 Style);
9170}
9171
9172TEST_F(FormatTest, ShortExportBlocks) {
9173 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80);
9174 Style.IndentExportBlock = false;
9175
9176 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
9177 verifyFormat("export {\n"
9178 "}",
9179 Style);
9180
9181 verifyFormat("export {\n"
9182 "int x;\n"
9183 "}",
9184 Style);
9185
9186 verifyFormat("export {\n"
9187 "int x;\n"
9188 "}",
9189 "export\n"
9190 "{\n"
9191 "int x;\n"
9192 "}",
9193 Style);
9194
9195 verifyFormat("export {\n"
9196 "}",
9197 "export {}", Style);
9198
9199 verifyFormat("export {\n"
9200 "int x;\n"
9201 "}",
9202 "export { int x; }", Style);
9203
9204 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
9205 verifyFormat("export {}",
9206 "export {\n"
9207 "}",
9208 Style);
9209
9210 verifyFormat("export { int x; }",
9211 "export {\n"
9212 "int x;\n"
9213 "}",
9214 Style);
9215
9216 verifyFormat("export { int x; }",
9217 "export\n"
9218 "{\n"
9219 "int x;\n"
9220 "}",
9221 Style);
9222
9223 verifyFormat("export {}",
9224 "export {\n"
9225 "}",
9226 Style);
9227
9228 verifyFormat("export { int x; }",
9229 "export {\n"
9230 "int x;\n"
9231 "}",
9232 Style);
9233
9234 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
9235 verifyFormat("export {}",
9236 "export {\n"
9237 "}",
9238 Style);
9239
9240 verifyFormat("export {\n"
9241 "int x;\n"
9242 "}",
9243 Style);
9244
9245 verifyFormat("export {\n"
9246 "int x;\n"
9247 "}",
9248 "export\n"
9249 "{\n"
9250 "int x;\n"
9251 "}",
9252 Style);
9253
9254 verifyFormat("export {}", Style);
9255
9256 verifyFormat("export {\n"
9257 "int x;\n"
9258 "}",
9259 "export { int x; }", Style);
9260}
9261
9262TEST_F(FormatTest, FormatsBuilderPattern) {
9263 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9264 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9265 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9266 " .StartsWith(\".init\", ORDER_INIT)\n"
9267 " .StartsWith(\".fini\", ORDER_FINI)\n"
9268 " .StartsWith(\".hash\", ORDER_HASH)\n"
9269 " .Default(ORDER_TEXT);");
9270
9271 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9272 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9273 verifyFormat("aaaaaaa->aaaaaaa\n"
9274 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9276 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9277 verifyFormat(
9278 "aaaaaaa->aaaaaaa\n"
9279 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9280 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9281 verifyFormat(
9282 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9283 " aaaaaaaaaaaaaa);");
9284 verifyFormat(
9285 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9286 " aaaaaa->aaaaaaaaaaaa()\n"
9287 " ->aaaaaaaaaaaaaaaa(\n"
9288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9289 " ->aaaaaaaaaaaaaaaaa();");
9290 verifyGoogleFormat(
9291 "void f() {\n"
9292 " someo->Add((new util::filetools::Handler(dir))\n"
9293 " ->OnEvent1(NewPermanentCallback(\n"
9294 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9295 " ->OnEvent2(NewPermanentCallback(\n"
9296 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9297 " ->OnEvent3(NewPermanentCallback(\n"
9298 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9299 " ->OnEvent5(NewPermanentCallback(\n"
9300 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9301 " ->OnEvent6(NewPermanentCallback(\n"
9302 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9303 "}");
9304
9305 verifyFormat(
9306 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9307 verifyFormat("aaaaaaaaaaaaaaa()\n"
9308 " .aaaaaaaaaaaaaaa()\n"
9309 " .aaaaaaaaaaaaaaa()\n"
9310 " .aaaaaaaaaaaaaaa()\n"
9311 " .aaaaaaaaaaaaaaa();");
9312 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9313 " .aaaaaaaaaaaaaaa()\n"
9314 " .aaaaaaaaaaaaaaa()\n"
9315 " .aaaaaaaaaaaaaaa();");
9316 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9317 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9318 " .aaaaaaaaaaaaaaa();");
9319 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9320 " ->aaaaaaaaaaaaaae(0)\n"
9321 " ->aaaaaaaaaaaaaaa();");
9322
9323 // Don't linewrap after very short segments.
9324 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9325 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9326 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9327 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9328 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9329 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9330 verifyFormat("aaa()\n"
9331 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9332 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9333 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9334
9335 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9336 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9337 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9338 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9339 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9341
9342 // Prefer not to break after empty parentheses.
9343 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9344 " First->LastNewlineOffset);");
9345
9346 // Prefer not to create "hanging" indents.
9347 verifyFormat(
9348 "return !soooooooooooooome_map\n"
9349 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9350 " .second;");
9351 verifyFormat(
9352 "return aaaaaaaaaaaaaaaa\n"
9353 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9354 " .aaaa(aaaaaaaaaaaaaa);");
9355 // No hanging indent here.
9356 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9358 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9360 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9361 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9362 getLLVMStyleWithColumns(60));
9363 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9364 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9365 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9366 getLLVMStyleWithColumns(59));
9367 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9369 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9370
9371 // Dont break if only closing statements before member call
9372 verifyFormat("test() {\n"
9373 " ([]() -> {\n"
9374 " int b = 32;\n"
9375 " return 3;\n"
9376 " }).foo();\n"
9377 "}");
9378 verifyFormat("test() {\n"
9379 " (\n"
9380 " []() -> {\n"
9381 " int b = 32;\n"
9382 " return 3;\n"
9383 " },\n"
9384 " foo, bar)\n"
9385 " .foo();\n"
9386 "}");
9387 verifyFormat("test() {\n"
9388 " ([]() -> {\n"
9389 " int b = 32;\n"
9390 " return 3;\n"
9391 " })\n"
9392 " .foo()\n"
9393 " .bar();\n"
9394 "}");
9395 verifyFormat("test() {\n"
9396 " ([]() -> {\n"
9397 " int b = 32;\n"
9398 " return 3;\n"
9399 " })\n"
9400 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9401 " \"bbbb\");\n"
9402 "}",
9403 getLLVMStyleWithColumns(30));
9404}
9405
9406TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
9407 verifyFormat(
9408 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9409 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9410 verifyFormat(
9411 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9412 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9413
9414 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9415 " ccccccccccccccccccccccccc) {\n}");
9416 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9417 " ccccccccccccccccccccccccc) {\n}");
9418
9419 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9420 " ccccccccccccccccccccccccc) {\n}");
9421 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9422 " ccccccccccccccccccccccccc) {\n}");
9423
9424 verifyFormat(
9425 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9426 " ccccccccccccccccccccccccc) {\n}");
9427 verifyFormat(
9428 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9429 " ccccccccccccccccccccccccc) {\n}");
9430
9431 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9432 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9433 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9434 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9435 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9436 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9437 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9438 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9439
9440 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9441 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9442 " aaaaaaaaaaaaaaa != aa) {\n}");
9443 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9444 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9445 " aaaaaaaaaaaaaaa != aa) {\n}");
9446}
9447
9448TEST_F(FormatTest, BreaksAfterAssignments) {
9449 verifyFormat(
9450 "unsigned Cost =\n"
9451 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9452 " SI->getPointerAddressSpaceee());");
9453 verifyFormat(
9454 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9455 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9456
9457 verifyFormat(
9458 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9459 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9460 verifyFormat("unsigned OriginalStartColumn =\n"
9461 " SourceMgr.getSpellingColumnNumber(\n"
9462 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9463 " 1;");
9464}
9465
9466TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
9467 FormatStyle Style = getLLVMStyle();
9468 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9469 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9470 Style);
9471
9472 Style.PenaltyBreakAssignment = 20;
9473 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9474 " cccccccccccccccccccccccccc;",
9475 Style);
9476}
9477
9478TEST_F(FormatTest, AlignsAfterAssignments) {
9479 verifyFormat(
9480 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9481 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9482 verifyFormat(
9483 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9484 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9485 verifyFormat(
9486 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9487 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9488 verifyFormat(
9489 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9490 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9491 verifyFormat(
9492 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9493 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9494 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9495}
9496
9497TEST_F(FormatTest, AlignsAfterReturn) {
9498 verifyFormat(
9499 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9500 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9501 verifyFormat(
9502 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9503 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9504 verifyFormat(
9505 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9506 " aaaaaaaaaaaaaaaaaaaaaa();");
9507 verifyFormat(
9508 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9509 " aaaaaaaaaaaaaaaaaaaaaa());");
9510 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9511 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9512 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9515 verifyFormat("return\n"
9516 " // true if code is one of a or b.\n"
9517 " code == a || code == b;");
9518}
9519
9520TEST_F(FormatTest, AlignsAfterOpenBracket) {
9521 verifyFormat(
9522 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9523 " aaaaaaaaa aaaaaaa) {}");
9524 verifyFormat(
9525 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9526 " aaaaaaaaaaa aaaaaaaaa);");
9527 verifyFormat(
9528 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9529 " aaaaaaaaaaaaaaaaaaaaa));");
9530 FormatStyle Style = getLLVMStyle();
9531 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9532 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9533 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9534 Style);
9535 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9536 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9537 Style);
9538 verifyFormat("SomeLongVariableName->someFunction(\n"
9539 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9540 Style);
9541 verifyFormat(
9542 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9543 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9544 Style);
9545 verifyFormat(
9546 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9547 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9548 Style);
9549 verifyFormat(
9550 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9551 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9552 Style);
9553
9554 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9555 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9556 " b));",
9557 Style);
9558
9559 Style.ColumnLimit = 30;
9560 verifyFormat("for (int foo = 0; foo < FOO;\n"
9561 " ++foo) {\n"
9562 " bar(foo);\n"
9563 "}",
9564 Style);
9565 Style.ColumnLimit = 80;
9566
9567 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9568 Style.BinPackArguments = false;
9569 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9570 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9571 " aaaaaaaaaaa aaaaaaaa,\n"
9572 " aaaaaaaaa aaaaaaa,\n"
9573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9574 Style);
9575 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9576 " aaaaaaaaaaa aaaaaaaaa,\n"
9577 " aaaaaaaaaaa aaaaaaaaa,\n"
9578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9579 Style);
9580 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9581 " aaaaaaaaaaaaaaa,\n"
9582 " aaaaaaaaaaaaaaaaaaaaa,\n"
9583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9584 Style);
9585 verifyFormat(
9586 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9587 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9588 Style);
9589 verifyFormat(
9590 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9591 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9592 Style);
9593 verifyFormat(
9594 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9595 " aaaaaaaaaaaaaaaaaaaaa(\n"
9596 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9597 " aaaaaaaaaaaaaaaa);",
9598 Style);
9599 verifyFormat(
9600 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9601 " aaaaaaaaaaaaaaaaaaaaa(\n"
9602 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9603 " aaaaaaaaaaaaaaaa);",
9604 Style);
9605 verifyFormat(
9606 "fooooooooooo(new BARRRRRRRRR(\n"
9607 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9608 Style);
9609 verifyFormat(
9610 "fooooooooooo(::new BARRRRRRRRR(\n"
9611 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9612 Style);
9613 verifyFormat(
9614 "fooooooooooo(new FOO::BARRRR(\n"
9615 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9616 Style);
9617
9618 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9619 Style.BinPackArguments = false;
9620 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
9621 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9622 " aaaaaaaaaaa aaaaaaaa,\n"
9623 " aaaaaaaaa aaaaaaa,\n"
9624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9625 ") {}",
9626 Style);
9627 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9628 " aaaaaaaaaaa aaaaaaaaa,\n"
9629 " aaaaaaaaaaa aaaaaaaaa,\n"
9630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9631 ");",
9632 Style);
9633 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9634 " aaaaaaaaaaaaaaa,\n"
9635 " aaaaaaaaaaaaaaaaaaaaa,\n"
9636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9637 "));",
9638 Style);
9639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9640 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9641 "));",
9642 Style);
9643 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9644 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9645 "));",
9646 Style);
9647 verifyFormat(
9648 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9649 " aaaaaaaaaaaaaaaaaaaaa(\n"
9650 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9651 " ),\n"
9652 " aaaaaaaaaaaaaaaa\n"
9653 ");",
9654 Style);
9655 verifyFormat(
9656 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9657 " aaaaaaaaaaaaaaaaaaaaa(\n"
9658 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9659 " ) &&\n"
9660 " aaaaaaaaaaaaaaaa\n"
9661 ");",
9662 Style);
9663 verifyFormat("void foo(\n"
9664 " void (*foobarpntr)(\n"
9665 " aaaaaaaaaaaaaaaaaa *,\n"
9666 " bbbbbbbbbbbbbb *,\n"
9667 " cccccccccccccccccccc *,\n"
9668 " dddddddddddddddddd *\n"
9669 " )\n"
9670 ");",
9671 Style);
9672 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
9673 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9674 "};",
9675 Style);
9676
9677 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9678 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
9679 ") const {\n"
9680 " return true;\n"
9681 "}",
9682 Style);
9683 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9684 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n"
9685 ") const;",
9686 Style);
9687 verifyFormat("void aaaaaaaaa(\n"
9688 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n"
9689 ") const noexcept -> std::vector<of_very_long_type>;",
9690 Style);
9691 verifyFormat(
9692 "x = aaaaaaaaaaaaaaa(\n"
9693 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
9694 ");",
9695 Style);
9696 Style.ColumnLimit = 60;
9697 verifyFormat("auto lambda =\n"
9698 " [&b](\n"
9699 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9700 " ) {};",
9701 Style);
9702 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9703 " &bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
9704 ");",
9705 Style);
9706}
9707
9708TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
9709 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
9710 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9711 " bbbbbbbbbbbbbbbbbbbbbb);",
9712 Style);
9713 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
9714 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9715 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9716 " bbbbbbbbbbbbbbbbbbbbbb);",
9717 Style);
9718 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9719 Style.AlignOperands = FormatStyle::OAS_Align;
9720 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9721 " bbbbbbbbbbbbbbbbbbbbbb);",
9722 Style);
9723 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
9724 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9725 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9726 " bbbbbbbbbbbbbbbbbbbbbb);",
9727 Style);
9728}
9729
9730TEST_F(FormatTest, BlockIndentAndNamespace) {
9731 auto Style = getLLVMStyleWithColumns(ColumnLimit: 120);
9732 Style.AllowShortNamespacesOnASingleLine = true;
9733 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9734
9735 verifyNoCrash(
9736 Code: "namespace {\n"
9737 "void xxxxxxxxxxxxxxxxxxxxx(nnnnn::TTTTTTTTTTTTT const *mmmm,\n"
9738 " YYYYYYYYYYYYYYYYY &yyyyyyyyyyyyyy);\n"
9739 "} //",
9740 Style);
9741}
9742
9743TEST_F(FormatTest, BreaksConditionalExpressions) {
9744 verifyFormat(
9745 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9746 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9747 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9748 verifyFormat(
9749 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9750 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9751 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9752 verifyFormat(
9753 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9754 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9755 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9756 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9757 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9758 verifyFormat(
9759 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9760 " : aaaaaaaaaaaaa);");
9761 verifyFormat(
9762 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9763 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9764 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9765 " aaaaaaaaaaaaa);");
9766 verifyFormat(
9767 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9768 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9769 " aaaaaaaaaaaaa);");
9770 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9771 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9773 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9775 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9777 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9779 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9781 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9782 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9784 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9786 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9787 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9788 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9789 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9790 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9792 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9793 " : aaaaaaaaaaaaaaaa;");
9794 verifyFormat(
9795 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9796 " ? aaaaaaaaaaaaaaa\n"
9797 " : aaaaaaaaaaaaaaa;");
9798 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9799 " aaaaaaaaa\n"
9800 " ? b\n"
9801 " : c);");
9802 verifyFormat("return aaaa == bbbb\n"
9803 " // comment\n"
9804 " ? aaaa\n"
9805 " : bbbb;");
9806 verifyFormat("unsigned Indent =\n"
9807 " format(TheLine.First,\n"
9808 " IndentForLevel[TheLine.Level] >= 0\n"
9809 " ? IndentForLevel[TheLine.Level]\n"
9810 " : TheLine * 2,\n"
9811 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9812 getLLVMStyleWithColumns(60));
9813 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9814 " ? aaaaaaaaaaaaaaa\n"
9815 " : bbbbbbbbbbbbbbb //\n"
9816 " ? ccccccccccccccc\n"
9817 " : ddddddddddddddd;");
9818 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9819 " ? aaaaaaaaaaaaaaa\n"
9820 " : (bbbbbbbbbbbbbbb //\n"
9821 " ? ccccccccccccccc\n"
9822 " : ddddddddddddddd);");
9823 verifyFormat(
9824 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9825 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9826 " aaaaaaaaaaaaaaaaaaaaa +\n"
9827 " aaaaaaaaaaaaaaaaaaaaa\n"
9828 " : aaaaaaaaaa;");
9829 verifyFormat(
9830 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9831 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9832 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9833
9834 FormatStyle NoBinPacking = getLLVMStyle();
9835 NoBinPacking.BinPackArguments = false;
9836 verifyFormat(
9837 "void f() {\n"
9838 " g(aaa,\n"
9839 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9841 " ? aaaaaaaaaaaaaaa\n"
9842 " : aaaaaaaaaaaaaaa);\n"
9843 "}",
9844 NoBinPacking);
9845 verifyFormat(
9846 "void f() {\n"
9847 " g(aaa,\n"
9848 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9850 " ?: aaaaaaaaaaaaaaa);\n"
9851 "}",
9852 NoBinPacking);
9853
9854 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9855 " // comment.\n"
9856 " ccccccccccccccccccccccccccccccccccccccc\n"
9857 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9858 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9859
9860 // Assignments in conditional expressions. Apparently not uncommon :-(.
9861 verifyFormat("return a != b\n"
9862 " // comment\n"
9863 " ? a = b\n"
9864 " : a = b;");
9865 verifyFormat("return a != b\n"
9866 " // comment\n"
9867 " ? a = a != b\n"
9868 " // comment\n"
9869 " ? a = b\n"
9870 " : a\n"
9871 " : a;");
9872 verifyFormat("return a != b\n"
9873 " // comment\n"
9874 " ? a\n"
9875 " : a = a != b\n"
9876 " // comment\n"
9877 " ? a = b\n"
9878 " : a;");
9879
9880 // Chained conditionals
9881 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 70);
9882 Style.AlignOperands = FormatStyle::OAS_Align;
9883 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9884 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9885 " : 3333333333333333;",
9886 Style);
9887 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9888 " : bbbbbbbbbb ? 2222222222222222\n"
9889 " : 3333333333333333;",
9890 Style);
9891 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9892 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9893 " : 3333333333333333;",
9894 Style);
9895 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9896 " : bbbbbbbbbbbbbb ? 222222\n"
9897 " : 333333;",
9898 Style);
9899 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9900 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9901 " : cccccccccccccc ? 3333333333333333\n"
9902 " : 4444444444444444;",
9903 Style);
9904 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9905 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9906 " : 3333333333333333;",
9907 Style);
9908 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9909 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9910 " : (aaa ? bbb : ccc);",
9911 Style);
9912 verifyFormat(
9913 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9914 " : cccccccccccccccccc)\n"
9915 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9916 " : 3333333333333333;",
9917 Style);
9918 verifyFormat(
9919 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9920 " : cccccccccccccccccc)\n"
9921 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9922 " : 3333333333333333;",
9923 Style);
9924 verifyFormat(
9925 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9926 " : dddddddddddddddddd)\n"
9927 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9928 " : 3333333333333333;",
9929 Style);
9930 verifyFormat(
9931 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9932 " : dddddddddddddddddd)\n"
9933 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9934 " : 3333333333333333;",
9935 Style);
9936 verifyFormat(
9937 "return aaaaaaaaa ? 1111111111111111\n"
9938 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9939 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9940 " : dddddddddddddddddd)",
9941 Style);
9942 verifyFormat(
9943 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9944 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9945 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9946 " : cccccccccccccccccc);",
9947 Style);
9948 verifyFormat(
9949 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9950 " : ccccccccccccccc ? dddddddddddddddddd\n"
9951 " : eeeeeeeeeeeeeeeeee)\n"
9952 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9953 " : 3333333333333333;",
9954 Style);
9955 verifyFormat(
9956 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9957 " : ccccccccccccccc ? dddddddddddddddddd\n"
9958 " : eeeeeeeeeeeeeeeeee)\n"
9959 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9960 " : 3333333333333333;",
9961 Style);
9962 verifyFormat(
9963 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9964 " : cccccccccccc ? dddddddddddddddddd\n"
9965 " : eeeeeeeeeeeeeeeeee)\n"
9966 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9967 " : 3333333333333333;",
9968 Style);
9969 verifyFormat(
9970 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9971 " : cccccccccccccccccc\n"
9972 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9973 " : 3333333333333333;",
9974 Style);
9975 verifyFormat(
9976 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9977 " : cccccccccccccccc ? dddddddddddddddddd\n"
9978 " : eeeeeeeeeeeeeeeeee\n"
9979 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9980 " : 3333333333333333;",
9981 Style);
9982 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9983 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9984 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9985 " : eeeeeeeeeeeeeeeeee)\n"
9986 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9987 " : 3333333333333333;",
9988 Style);
9989 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9990 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9991 " : cccccccccccccccc ? dddddddddddddddddd\n"
9992 " : eeeeeeeeeeeeeeeeee\n"
9993 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9994 " : 3333333333333333;",
9995 Style);
9996
9997 Style.AlignOperands = FormatStyle::OAS_DontAlign;
9998 Style.BreakBeforeTernaryOperators = false;
9999 // FIXME: Aligning the question marks is weird given DontAlign.
10000 // Consider disabling this alignment in this case. Also check whether this
10001 // will render the adjustment from https://reviews.llvm.org/D82199
10002 // unnecessary.
10003 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
10004 " bbbb ? cccccccccccccccccc :\n"
10005 " ddddd;",
10006 Style);
10007
10008 verifyFormat(
10009 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
10010 " /*\n"
10011 " */\n"
10012 " function() {\n"
10013 " try {\n"
10014 " return JJJJJJJJJJJJJJ(\n"
10015 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
10016 " }\n"
10017 " } :\n"
10018 " function() {};",
10019 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
10020 " /*\n"
10021 " */\n"
10022 " function() {\n"
10023 " try {\n"
10024 " return JJJJJJJJJJJJJJ(\n"
10025 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
10026 " }\n"
10027 " } :\n"
10028 " function() {};",
10029 getGoogleStyle(FormatStyle::LK_JavaScript));
10030}
10031
10032TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
10033 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 70);
10034 Style.BreakBeforeTernaryOperators = false;
10035 verifyFormat(
10036 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10039 Style);
10040 verifyFormat(
10041 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
10042 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10044 Style);
10045 verifyFormat(
10046 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10048 Style);
10049 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
10050 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10052 Style);
10053 verifyFormat(
10054 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
10055 " aaaaaaaaaaaaa);",
10056 Style);
10057 verifyFormat(
10058 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10059 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10061 " aaaaaaaaaaaaa);",
10062 Style);
10063 verifyFormat(
10064 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10065 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10066 " aaaaaaaaaaaaa);",
10067 Style);
10068 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
10071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10073 Style);
10074 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
10078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
10080 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10081 Style);
10082 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
10084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
10086 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10087 Style);
10088 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10089 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10090 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10091 Style);
10092 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
10093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
10095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10096 Style);
10097 verifyFormat(
10098 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10099 " aaaaaaaaaaaaaaa :\n"
10100 " aaaaaaaaaaaaaaa;",
10101 Style);
10102 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
10103 " aaaaaaaaa ?\n"
10104 " b :\n"
10105 " c);",
10106 Style);
10107 verifyFormat("unsigned Indent =\n"
10108 " format(TheLine.First,\n"
10109 " IndentForLevel[TheLine.Level] >= 0 ?\n"
10110 " IndentForLevel[TheLine.Level] :\n"
10111 " TheLine * 2,\n"
10112 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
10113 Style);
10114 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
10115 " aaaaaaaaaaaaaaa :\n"
10116 " bbbbbbbbbbbbbbb ? //\n"
10117 " ccccccccccccccc :\n"
10118 " ddddddddddddddd;",
10119 Style);
10120 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
10121 " aaaaaaaaaaaaaaa :\n"
10122 " (bbbbbbbbbbbbbbb ? //\n"
10123 " ccccccccccccccc :\n"
10124 " ddddddddddddddd);",
10125 Style);
10126 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10127 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
10128 " ccccccccccccccccccccccccccc;",
10129 Style);
10130 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
10131 " aaaaa :\n"
10132 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
10133 Style);
10134
10135 // Chained conditionals
10136 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
10137 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10138 " 3333333333333333;",
10139 Style);
10140 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
10141 " bbbbbbbbbb ? 2222222222222222 :\n"
10142 " 3333333333333333;",
10143 Style);
10144 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
10145 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10146 " 3333333333333333;",
10147 Style);
10148 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
10149 " bbbbbbbbbbbbbbbb ? 222222 :\n"
10150 " 333333;",
10151 Style);
10152 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
10153 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10154 " cccccccccccccccc ? 3333333333333333 :\n"
10155 " 4444444444444444;",
10156 Style);
10157 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
10158 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10159 " 3333333333333333;",
10160 Style);
10161 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
10162 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10163 " (aaa ? bbb : ccc);",
10164 Style);
10165 verifyFormat(
10166 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10167 " cccccccccccccccccc) :\n"
10168 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10169 " 3333333333333333;",
10170 Style);
10171 verifyFormat(
10172 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10173 " cccccccccccccccccc) :\n"
10174 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10175 " 3333333333333333;",
10176 Style);
10177 verifyFormat(
10178 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10179 " dddddddddddddddddd) :\n"
10180 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10181 " 3333333333333333;",
10182 Style);
10183 verifyFormat(
10184 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10185 " dddddddddddddddddd) :\n"
10186 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10187 " 3333333333333333;",
10188 Style);
10189 verifyFormat(
10190 "return aaaaaaaaa ? 1111111111111111 :\n"
10191 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10192 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10193 " dddddddddddddddddd)",
10194 Style);
10195 verifyFormat(
10196 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
10197 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10198 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10199 " cccccccccccccccccc);",
10200 Style);
10201 verifyFormat(
10202 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10203 " ccccccccccccccccc ? dddddddddddddddddd :\n"
10204 " eeeeeeeeeeeeeeeeee) :\n"
10205 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10206 " 3333333333333333;",
10207 Style);
10208 verifyFormat(
10209 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10210 " ccccccccccccc ? dddddddddddddddddd :\n"
10211 " eeeeeeeeeeeeeeeeee) :\n"
10212 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10213 " 3333333333333333;",
10214 Style);
10215 verifyFormat(
10216 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10217 " ccccccccccccccccc ? dddddddddddddddddd :\n"
10218 " eeeeeeeeeeeeeeeeee) :\n"
10219 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10220 " 3333333333333333;",
10221 Style);
10222 verifyFormat(
10223 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10224 " cccccccccccccccccc :\n"
10225 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10226 " 3333333333333333;",
10227 Style);
10228 verifyFormat(
10229 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10230 " cccccccccccccccccc ? dddddddddddddddddd :\n"
10231 " eeeeeeeeeeeeeeeeee :\n"
10232 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10233 " 3333333333333333;",
10234 Style);
10235 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
10236 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10237 " cccccccccccccccccc ? dddddddddddddddddd :\n"
10238 " eeeeeeeeeeeeeeeeee) :\n"
10239 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10240 " 3333333333333333;",
10241 Style);
10242 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
10243 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10244 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
10245 " eeeeeeeeeeeeeeeeee :\n"
10246 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10247 " 3333333333333333;",
10248 Style);
10249}
10250
10251TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
10252 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
10253 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
10254 verifyFormat("bool a = true, b = false;");
10255
10256 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10257 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
10258 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
10259 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
10260 verifyFormat(
10261 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
10262 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
10263 " d = e && f;");
10264 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
10265 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
10266 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10267 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
10268 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
10269 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
10270
10271 FormatStyle Style = getGoogleStyle();
10272 Style.PointerAlignment = FormatStyle::PAS_Left;
10273 Style.DerivePointerAlignment = false;
10274 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10275 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
10276 " *b = bbbbbbbbbbbbbbbbbbb;",
10277 Style);
10278 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10279 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
10280 Style);
10281 verifyFormat("vector<int*> a, b;", Style);
10282 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
10283 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style);
10284 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style);
10285 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
10286 Style);
10287 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
10288 Style);
10289 verifyFormat(
10290 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
10291 Style);
10292
10293 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
10294 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
10295 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
10296 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
10297 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
10298 Style);
10299}
10300
10301TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
10302 verifyFormat("arr[foo ? bar : baz];");
10303 verifyFormat("f()[foo ? bar : baz];");
10304 verifyFormat("(a + b)[foo ? bar : baz];");
10305 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10306}
10307
10308TEST_F(FormatTest, AlignsStringLiterals) {
10309 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10310 " \"short literal\");");
10311 verifyFormat(
10312 "looooooooooooooooooooooooongFunction(\n"
10313 " \"short literal\"\n"
10314 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10315 verifyFormat("someFunction(\"Always break between multi-line\"\n"
10316 " \" string literals\",\n"
10317 " also, other, parameters);");
10318 verifyFormat("fun + \"1243\" /* comment */\n"
10319 " \"5678\";",
10320 "fun + \"1243\" /* comment */\n"
10321 " \"5678\";",
10322 getLLVMStyleWithColumns(28));
10323 verifyFormat(
10324 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10325 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10326 " \"aaaaaaaaaaaaaaaa\";",
10327 "aaaaaa ="
10328 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10329 "aaaaaaaaaaaaaaaaaaaaa\" "
10330 "\"aaaaaaaaaaaaaaaa\";");
10331 verifyFormat("a = a + \"a\"\n"
10332 " \"a\"\n"
10333 " \"a\";");
10334 verifyFormat("f(\"a\", \"b\"\n"
10335 " \"c\");");
10336
10337 verifyFormat(
10338 "#define LL_FORMAT \"ll\"\n"
10339 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10340 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10341
10342 verifyFormat("#define A(X) \\\n"
10343 " \"aaaaa\" #X \"bbbbbb\" \\\n"
10344 " \"ccccc\"",
10345 getLLVMStyleWithColumns(23));
10346 verifyFormat("#define A \"def\"\n"
10347 "f(\"abc\" A \"ghi\"\n"
10348 " \"jkl\");");
10349
10350 verifyFormat("f(L\"a\"\n"
10351 " L\"b\");");
10352 verifyFormat("#define A(X) \\\n"
10353 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10354 " L\"ccccc\"",
10355 getLLVMStyleWithColumns(25));
10356
10357 verifyFormat("f(@\"a\"\n"
10358 " @\"b\");");
10359 verifyFormat("NSString s = @\"a\"\n"
10360 " @\"b\"\n"
10361 " @\"c\";");
10362 verifyFormat("NSString s = @\"a\"\n"
10363 " \"b\"\n"
10364 " \"c\";");
10365}
10366
10367TEST_F(FormatTest, ReturnTypeBreakingStyle) {
10368 FormatStyle Style = getLLVMStyle();
10369 Style.ColumnLimit = 60;
10370
10371 // No declarations or definitions should be moved to own line.
10372 Style.BreakAfterReturnType = FormatStyle::RTBS_None;
10373 verifyFormat("class A {\n"
10374 " int f() { return 1; }\n"
10375 " int g();\n"
10376 " long\n"
10377 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10378 "};\n"
10379 "int f() { return 1; }\n"
10380 "int g();\n"
10381 "int foooooooooooooooooooooooooooo::\n"
10382 " baaaaaaaaaaaaaaaaaaaaar();",
10383 Style);
10384
10385 // It is now allowed to break after a short return type if necessary.
10386 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
10387 verifyFormat("class A {\n"
10388 " int f() { return 1; }\n"
10389 " int g();\n"
10390 " long\n"
10391 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10392 "};\n"
10393 "int f() { return 1; }\n"
10394 "int g();\n"
10395 "int\n"
10396 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10397 Style);
10398
10399 // It now must never break after a short return type.
10400 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
10401 verifyFormat("class A {\n"
10402 " int f() { return 1; }\n"
10403 " int g();\n"
10404 " long foooooooooooooooooooooooooooo::\n"
10405 " baaaaaaaaaaaaaaaaaaaar();\n"
10406 "};\n"
10407 "int f() { return 1; }\n"
10408 "int g();\n"
10409 "int foooooooooooooooooooooooooooo::\n"
10410 " baaaaaaaaaaaaaaaaaaaaar();",
10411 Style);
10412
10413 // All declarations and definitions should have the return type moved to its
10414 // own line.
10415 Style.BreakAfterReturnType = FormatStyle::RTBS_All;
10416 Style.TypenameMacros = {"LIST"};
10417 verifyFormat("SomeType\n"
10418 "funcdecl(LIST(uint64_t));",
10419 Style);
10420 verifyFormat("class E {\n"
10421 " int\n"
10422 " f() {\n"
10423 " return 1;\n"
10424 " }\n"
10425 " int\n"
10426 " g();\n"
10427 " long\n"
10428 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10429 "};\n"
10430 "int\n"
10431 "f() {\n"
10432 " return 1;\n"
10433 "}\n"
10434 "int\n"
10435 "g();\n"
10436 "int\n"
10437 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10438 Style);
10439
10440 // Top-level definitions, and no kinds of declarations should have the
10441 // return type moved to its own line.
10442 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
10443 verifyFormat("class B {\n"
10444 " int f() { return 1; }\n"
10445 " int g();\n"
10446 "};\n"
10447 "int\n"
10448 "f() {\n"
10449 " return 1;\n"
10450 "}\n"
10451 "int g();",
10452 Style);
10453
10454 // Top-level definitions and declarations should have the return type moved
10455 // to its own line.
10456 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
10457 verifyFormat("class C {\n"
10458 " int f() { return 1; }\n"
10459 " int g();\n"
10460 "};\n"
10461 "int\n"
10462 "f() {\n"
10463 " return 1;\n"
10464 "}\n"
10465 "int\n"
10466 "g();\n"
10467 "int\n"
10468 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10469 Style);
10470
10471 // All definitions should have the return type moved to its own line, but no
10472 // kinds of declarations.
10473 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
10474 verifyFormat("class D {\n"
10475 " int\n"
10476 " f() {\n"
10477 " return 1;\n"
10478 " }\n"
10479 " int g();\n"
10480 "};\n"
10481 "int\n"
10482 "f() {\n"
10483 " return 1;\n"
10484 "}\n"
10485 "int g();",
10486 Style);
10487 verifyFormat("const char *\n"
10488 "f(void) {\n" // Break here.
10489 " return \"\";\n"
10490 "}\n"
10491 "const char *bar(void);", // No break here.
10492 Style);
10493 verifyFormat("template <class T>\n"
10494 "T *\n"
10495 "f(T &c) {\n" // Break here.
10496 " return NULL;\n"
10497 "}\n"
10498 "template <class T> T *f(T &c);", // No break here.
10499 Style);
10500 verifyFormat("class C {\n"
10501 " int\n"
10502 " operator+() {\n"
10503 " return 1;\n"
10504 " }\n"
10505 " int\n"
10506 " operator()() {\n"
10507 " return 1;\n"
10508 " }\n"
10509 "};",
10510 Style);
10511 verifyFormat("void\n"
10512 "A::operator()() {}\n"
10513 "void\n"
10514 "A::operator>>() {}\n"
10515 "void\n"
10516 "A::operator+() {}\n"
10517 "void\n"
10518 "A::operator*() {}\n"
10519 "void\n"
10520 "A::operator->() {}\n"
10521 "void\n"
10522 "A::operator&() {}\n"
10523 "void\n"
10524 "A::operator&&() {}\n"
10525 "void\n"
10526 "A::operator[]() {}\n"
10527 "void\n"
10528 "A::operator!() {}\n"
10529 "void\n"
10530 "A::operator<Foo> *() {}\n"
10531 "void\n"
10532 "A::operator<Foo> &() {}\n",
10533 Style);
10534 verifyFormat("constexpr auto\n"
10535 "operator()() const -> reference {}\n"
10536 "constexpr auto\n"
10537 "operator>>() const -> reference {}\n"
10538 "constexpr auto\n"
10539 "operator+() const -> reference {}\n"
10540 "constexpr auto\n"
10541 "operator*() const -> reference {}\n"
10542 "constexpr auto\n"
10543 "operator->() const -> reference {}\n"
10544 "constexpr auto\n"
10545 "operator++() const -> reference {}\n"
10546 "constexpr auto\n"
10547 "operator void *() const -> reference {}\n"
10548 "constexpr auto\n"
10549 "operator void **() const -> reference {}\n"
10550 "constexpr auto\n"
10551 "operator void *() const -> reference {}\n"
10552 "constexpr auto\n"
10553 "operator void &() const -> reference {}\n"
10554 "constexpr auto\n"
10555 "operator&&() const -> reference {}\n"
10556 "constexpr auto\n"
10557 "operator char *() const -> reference {}\n"
10558 "constexpr auto\n"
10559 "operator!() const -> reference {}\n"
10560 "constexpr auto\n"
10561 "operator[]() const -> reference {}",
10562 Style);
10563 verifyFormat("void *operator new(std::size_t s);", // No break here.
10564 Style);
10565 verifyFormat("void *\n"
10566 "operator new(std::size_t s) {}",
10567 Style);
10568 verifyFormat("void *\n"
10569 "operator delete[](void *ptr) {}",
10570 Style);
10571 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10572 verifyFormat("const char *\n"
10573 "f(void)\n" // Break here.
10574 "{\n"
10575 " return \"\";\n"
10576 "}\n"
10577 "const char *bar(void);", // No break here.
10578 Style);
10579 verifyFormat("template <class T>\n"
10580 "T *\n" // Problem here: no line break
10581 "f(T &c)\n" // Break here.
10582 "{\n"
10583 " return NULL;\n"
10584 "}\n"
10585 "template <class T> T *f(T &c);", // No break here.
10586 Style);
10587 verifyFormat("int\n"
10588 "foo(A<bool> a)\n"
10589 "{\n"
10590 " return a;\n"
10591 "}",
10592 Style);
10593 verifyFormat("int\n"
10594 "foo(A<8> a)\n"
10595 "{\n"
10596 " return a;\n"
10597 "}",
10598 Style);
10599 verifyFormat("int\n"
10600 "foo(A<B<bool>, 8> a)\n"
10601 "{\n"
10602 " return a;\n"
10603 "}",
10604 Style);
10605 verifyFormat("int\n"
10606 "foo(A<B<8>, bool> a)\n"
10607 "{\n"
10608 " return a;\n"
10609 "}",
10610 Style);
10611 verifyFormat("int\n"
10612 "foo(A<B<bool>, bool> a)\n"
10613 "{\n"
10614 " return a;\n"
10615 "}",
10616 Style);
10617 verifyFormat("int\n"
10618 "foo(A<B<8>, 8> a)\n"
10619 "{\n"
10620 " return a;\n"
10621 "}",
10622 Style);
10623
10624 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
10625 Style.BraceWrapping.AfterFunction = true;
10626 verifyFormat("int f(i);\n" // No break here.
10627 "int\n" // Break here.
10628 "f(i)\n"
10629 "{\n"
10630 " return i + 1;\n"
10631 "}\n"
10632 "int\n" // Break here.
10633 "f(i)\n"
10634 "{\n"
10635 " return i + 1;\n"
10636 "};",
10637 Style);
10638 verifyFormat("int f(a, b, c);\n" // No break here.
10639 "int\n" // Break here.
10640 "f(a, b, c)\n" // Break here.
10641 "short a, b;\n"
10642 "float c;\n"
10643 "{\n"
10644 " return a + b < c;\n"
10645 "}\n"
10646 "int\n" // Break here.
10647 "f(a, b, c)\n" // Break here.
10648 "short a, b;\n"
10649 "float c;\n"
10650 "{\n"
10651 " return a + b < c;\n"
10652 "};",
10653 Style);
10654 verifyFormat("byte *\n" // Break here.
10655 "f(a)\n" // Break here.
10656 "byte a[];\n"
10657 "{\n"
10658 " return a;\n"
10659 "}",
10660 Style);
10661 verifyFormat("byte *\n"
10662 "f(a)\n"
10663 "byte /* K&R C */ a[];\n"
10664 "{\n"
10665 " return a;\n"
10666 "}\n"
10667 "byte *\n"
10668 "g(p)\n"
10669 "byte /* K&R C */ *p;\n"
10670 "{\n"
10671 " return p;\n"
10672 "}",
10673 Style);
10674 verifyFormat("bool f(int a, int) override;\n"
10675 "Bar g(int a, Bar) final;\n"
10676 "Bar h(a, Bar) final;",
10677 Style);
10678 verifyFormat("int\n"
10679 "f(a)",
10680 Style);
10681 verifyFormat("bool\n"
10682 "f(size_t = 0, bool b = false)\n"
10683 "{\n"
10684 " return !b;\n"
10685 "}",
10686 Style);
10687
10688 // The return breaking style doesn't affect:
10689 // * function and object definitions with attribute-like macros
10690 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10691 " ABSL_GUARDED_BY(mutex) = {};",
10692 getGoogleStyleWithColumns(40));
10693 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10694 " ABSL_GUARDED_BY(mutex); // comment",
10695 getGoogleStyleWithColumns(40));
10696 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10697 " ABSL_GUARDED_BY(mutex1)\n"
10698 " ABSL_GUARDED_BY(mutex2);",
10699 getGoogleStyleWithColumns(40));
10700 verifyFormat("Tttttt f(int a, int b)\n"
10701 " ABSL_GUARDED_BY(mutex1)\n"
10702 " ABSL_GUARDED_BY(mutex2);",
10703 getGoogleStyleWithColumns(40));
10704 // * typedefs
10705 verifyGoogleFormat("typedef ATTR(X) char x;");
10706
10707 Style = getGNUStyle();
10708
10709 // Test for comments at the end of function declarations.
10710 verifyFormat("void\n"
10711 "foo (int a, /*abc*/ int b) // def\n"
10712 "{\n"
10713 "}",
10714 Style);
10715
10716 verifyFormat("void\n"
10717 "foo (int a, /* abc */ int b) /* def */\n"
10718 "{\n"
10719 "}",
10720 Style);
10721
10722 // Definitions that should not break after return type
10723 verifyFormat("void foo (int a, int b); // def", Style);
10724 verifyFormat("void foo (int a, int b); /* def */", Style);
10725 verifyFormat("void foo (int a, int b);", Style);
10726}
10727
10728TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
10729 FormatStyle NoBreak = getLLVMStyle();
10730 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
10731 FormatStyle Break = getLLVMStyle();
10732 Break.AlwaysBreakBeforeMultilineStrings = true;
10733 verifyFormat("aaaa = \"bbbb\"\n"
10734 " \"cccc\";",
10735 NoBreak);
10736 verifyFormat("aaaa =\n"
10737 " \"bbbb\"\n"
10738 " \"cccc\";",
10739 Break);
10740 verifyFormat("aaaa(\"bbbb\"\n"
10741 " \"cccc\");",
10742 NoBreak);
10743 verifyFormat("aaaa(\n"
10744 " \"bbbb\"\n"
10745 " \"cccc\");",
10746 Break);
10747 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10748 " \"cccc\");",
10749 NoBreak);
10750 verifyFormat("aaaa(qqq,\n"
10751 " \"bbbb\"\n"
10752 " \"cccc\");",
10753 Break);
10754 verifyFormat("aaaa(qqq,\n"
10755 " L\"bbbb\"\n"
10756 " L\"cccc\");",
10757 Break);
10758 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10759 " \"bbbb\"));",
10760 Break);
10761 verifyFormat("string s = someFunction(\n"
10762 " \"abc\"\n"
10763 " \"abc\");",
10764 Break);
10765
10766 // As we break before unary operators, breaking right after them is bad.
10767 verifyFormat("string foo = abc ? \"x\"\n"
10768 " \"blah blah blah blah blah blah\"\n"
10769 " : \"y\";",
10770 Break);
10771
10772 // Don't break if there is no column gain.
10773 verifyFormat("f(\"aaaa\"\n"
10774 " \"bbbb\");",
10775 Break);
10776
10777 // Treat literals with escaped newlines like multi-line string literals.
10778 verifyNoChange("x = \"a\\\n"
10779 "b\\\n"
10780 "c\";",
10781 NoBreak);
10782 verifyFormat("xxxx =\n"
10783 " \"a\\\n"
10784 "b\\\n"
10785 "c\";",
10786 "xxxx = \"a\\\n"
10787 "b\\\n"
10788 "c\";",
10789 Break);
10790
10791 verifyFormat("NSString *const kString =\n"
10792 " @\"aaaa\"\n"
10793 " @\"bbbb\";",
10794 "NSString *const kString = @\"aaaa\"\n"
10795 "@\"bbbb\";",
10796 Break);
10797
10798 Break.ColumnLimit = 0;
10799 verifyFormat("const char *hello = \"hello llvm\";", Break);
10800}
10801
10802TEST_F(FormatTest, AlignsPipes) {
10803 verifyFormat(
10804 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10805 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10806 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10807 verifyFormat(
10808 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10809 " << aaaaaaaaaaaaaaaaaaaa;");
10810 verifyFormat(
10811 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10812 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10813 verifyFormat(
10814 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10815 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10816 verifyFormat(
10817 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10818 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10819 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10820 verifyFormat(
10821 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10822 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10823 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10824 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10827 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10828 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10829 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10830 verifyFormat(
10831 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10833 verifyFormat(
10834 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10835 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10836
10837 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10838 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10839 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10841 " aaaaaaaaaaaaaaaaaaaaa)\n"
10842 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10843 verifyFormat("LOG_IF(aaa == //\n"
10844 " bbb)\n"
10845 " << a << b;");
10846
10847 // But sometimes, breaking before the first "<<" is desirable.
10848 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10849 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10850 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10851 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10852 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10853 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10854 " << BEF << IsTemplate << Description << E->getType();");
10855 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10856 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10858 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10859 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10861 " << aaa;");
10862
10863 verifyFormat(
10864 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10865 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10866
10867 // Incomplete string literal.
10868 verifyFormat("llvm::errs() << \"\n"
10869 " << a;",
10870 "llvm::errs() << \"\n<<a;");
10871
10872 verifyFormat("void f() {\n"
10873 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10874 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10875 "}");
10876
10877 // Handle 'endl'.
10878 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10879 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10880 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10881
10882 // Handle '\n'.
10883 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10884 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10885 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10886 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10887 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10888 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10889 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10890}
10891
10892TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
10893 verifyFormat("return out << \"somepacket = {\\n\"\n"
10894 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10895 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10896 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10897 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10898 " << \"}\";");
10899
10900 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10901 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10902 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10903 verifyFormat(
10904 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10905 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10906 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10907 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10908 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10909 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10910 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10911 verifyFormat(
10912 "void f() {\n"
10913 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10914 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10915 "}");
10916
10917 // Breaking before the first "<<" is generally not desirable.
10918 verifyFormat(
10919 "llvm::errs()\n"
10920 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10921 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10922 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10923 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10924 getLLVMStyleWithColumns(70));
10925 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10926 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10927 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10928 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10929 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10930 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10931 getLLVMStyleWithColumns(70));
10932
10933 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10934 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10935 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10936 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10937 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10938 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10939 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10940 " (aaaa + aaaa);",
10941 getLLVMStyleWithColumns(40));
10942 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10943 " (aaaaaaa + aaaaa));",
10944 getLLVMStyleWithColumns(40));
10945 verifyFormat(
10946 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10947 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10948 " bbbbbbbbbbbbbbbbbbbbbbb);");
10949}
10950
10951TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10952 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10953
10954 verifyNoChange("QStringList() << \"foo\"\n"
10955 " << \"bar\";");
10956
10957 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10958 "log_error(log, \"foo\"\n"
10959 " << \"bar\");");
10960}
10961
10962TEST_F(FormatTest, UnderstandsEquals) {
10963 verifyFormat(
10964 "aaaaaaaaaaaaaaaaa =\n"
10965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10966 verifyFormat(
10967 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10969 verifyFormat(
10970 "if (a) {\n"
10971 " f();\n"
10972 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10974 "}");
10975
10976 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10977 " 100000000 + 10000000) {\n}");
10978}
10979
10980TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
10981 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10982 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10983
10984 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10985 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10986
10987 verifyFormat(
10988 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10989 " Parameter2);");
10990
10991 verifyFormat(
10992 "ShortObject->shortFunction(\n"
10993 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10994 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10995
10996 verifyFormat("loooooooooooooongFunction(\n"
10997 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10998
10999 verifyFormat(
11000 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
11001 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
11002
11003 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
11004 " .WillRepeatedly(Return(SomeValue));");
11005 verifyFormat("void f() {\n"
11006 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
11007 " .Times(2)\n"
11008 " .WillRepeatedly(Return(SomeValue));\n"
11009 "}");
11010 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
11011 " ccccccccccccccccccccccc);");
11012 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11014 " .aaaaa(aaaaa),\n"
11015 " aaaaaaaaaaaaaaaaaaaaa);");
11016 verifyFormat("void f() {\n"
11017 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11018 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
11019 "}");
11020 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11022 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11023 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11024 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11025 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11026 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11027 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11028 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
11029 "}");
11030
11031 // Here, it is not necessary to wrap at "." or "->".
11032 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
11033 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
11034 verifyFormat(
11035 "aaaaaaaaaaa->aaaaaaaaa(\n"
11036 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11037 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
11038
11039 verifyFormat(
11040 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
11042 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
11043 " aaaaaaaaa()->aaaaaa()->aaaaa());");
11044 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
11045 " aaaaaaaaa()->aaaaaa()->aaaaa());");
11046
11047 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11049 " .a();");
11050
11051 FormatStyle NoBinPacking = getLLVMStyle();
11052 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
11053 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
11054 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
11055 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
11056 " aaaaaaaaaaaaaaaaaaa,\n"
11057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
11058 NoBinPacking);
11059
11060 // If there is a subsequent call, change to hanging indentation.
11061 verifyFormat(
11062 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11063 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
11064 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11065 verifyFormat(
11066 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11067 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
11068 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11070 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11071 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
11073 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11074}
11075
11076TEST_F(FormatTest, WrapsTemplateDeclarations) {
11077 verifyFormat("template <typename T>\n"
11078 "virtual void loooooooooooongFunction(int Param1, int Param2);");
11079 verifyFormat("template <typename T>\n"
11080 "// T should be one of {A, B}.\n"
11081 "virtual void loooooooooooongFunction(int Param1, int Param2);");
11082 verifyFormat(
11083 "template <typename T>\n"
11084 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
11085 verifyFormat("template <typename T>\n"
11086 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
11087 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
11088 verifyFormat(
11089 "template <typename T>\n"
11090 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
11091 " int Paaaaaaaaaaaaaaaaaaaaram2);");
11092 verifyFormat(
11093 "template <typename T>\n"
11094 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
11095 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
11096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11097 verifyFormat("template <typename T>\n"
11098 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11099 " int aaaaaaaaaaaaaaaaaaaaaa);");
11100 verifyFormat(
11101 "template <typename T1, typename T2 = char, typename T3 = char,\n"
11102 " typename T4 = char>\n"
11103 "void f();");
11104 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
11105 " template <typename> class cccccccccccccccccccccc,\n"
11106 " typename ddddddddddddd>\n"
11107 "class C {};");
11108 verifyFormat(
11109 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
11110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11111
11112 verifyFormat("void f() {\n"
11113 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
11114 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
11115 "}");
11116
11117 verifyFormat("template <typename T> class C {};");
11118 verifyFormat("template <typename T> void f();");
11119 verifyFormat("template <typename T> void f() {}");
11120 verifyFormat(
11121 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
11122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
11124 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
11125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
11127 " bbbbbbbbbbbbbbbbbbbbbbbb);",
11128 getLLVMStyleWithColumns(72));
11129 verifyFormat("static_cast<A< //\n"
11130 " B> *>(\n"
11131 "\n"
11132 ");",
11133 "static_cast<A<//\n"
11134 " B>*>(\n"
11135 "\n"
11136 " );");
11137 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11138 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
11139
11140 FormatStyle AlwaysBreak = getLLVMStyle();
11141 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
11142 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
11143 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
11144 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
11145 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11146 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
11147 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
11148 verifyFormat("template <template <typename> class Fooooooo,\n"
11149 " template <typename> class Baaaaaaar>\n"
11150 "struct C {};",
11151 AlwaysBreak);
11152 verifyFormat("template <typename T> // T can be A, B or C.\n"
11153 "struct C {};",
11154 AlwaysBreak);
11155 verifyFormat("template <typename T>\n"
11156 "C(T) noexcept;",
11157 AlwaysBreak);
11158 verifyFormat("template <typename T>\n"
11159 "ClassName(T) noexcept;",
11160 AlwaysBreak);
11161 verifyFormat("template <typename T>\n"
11162 "POOR_NAME(T) noexcept;",
11163 AlwaysBreak);
11164 verifyFormat("template <enum E> class A {\n"
11165 "public:\n"
11166 " E *f();\n"
11167 "};");
11168
11169 FormatStyle NeverBreak = getLLVMStyle();
11170 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No;
11171 verifyFormat("template <typename T> class C {};", NeverBreak);
11172 verifyFormat("template <typename T> void f();", NeverBreak);
11173 verifyFormat("template <typename T> void f() {}", NeverBreak);
11174 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak);
11175 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak);
11176 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak);
11177 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
11178 "bbbbbbbbbbbbbbbbbbbb) {}",
11179 NeverBreak);
11180 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11181 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
11182 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
11183 NeverBreak);
11184 verifyFormat("template <template <typename> class Fooooooo,\n"
11185 " template <typename> class Baaaaaaar>\n"
11186 "struct C {};",
11187 NeverBreak);
11188 verifyFormat("template <typename T> // T can be A, B or C.\n"
11189 "struct C {};",
11190 NeverBreak);
11191 verifyFormat("template <enum E> class A {\n"
11192 "public:\n"
11193 " E *f();\n"
11194 "};",
11195 NeverBreak);
11196 NeverBreak.PenaltyBreakTemplateDeclaration = 100;
11197 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
11198 "bbbbbbbbbbbbbbbbbbbb) {}",
11199 NeverBreak);
11200
11201 auto Style = getLLVMStyle();
11202 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave;
11203
11204 verifyNoChange("template <typename T>\n"
11205 "class C {};",
11206 Style);
11207 verifyFormat("template <typename T> class C {};", Style);
11208
11209 verifyNoChange("template <typename T>\n"
11210 "void f();",
11211 Style);
11212 verifyFormat("template <typename T> void f();", Style);
11213
11214 verifyNoChange("template <typename T>\n"
11215 "void f() {}",
11216 Style);
11217 verifyFormat("template <typename T> void f() {}", Style);
11218
11219 verifyNoChange("template <typename T>\n"
11220 "// T can be A, B or C.\n"
11221 "struct C {};",
11222 Style);
11223 verifyFormat("template <typename T> // T can be A, B or C.\n"
11224 "struct C {};",
11225 Style);
11226
11227 verifyNoChange("template <typename T>\n"
11228 "C(T) noexcept;",
11229 Style);
11230 verifyFormat("template <typename T> C(T) noexcept;", Style);
11231
11232 verifyNoChange("template <enum E>\n"
11233 "class A {\n"
11234 "public:\n"
11235 " E *f();\n"
11236 "};",
11237 Style);
11238 verifyFormat("template <enum E> class A {\n"
11239 "public:\n"
11240 " E *f();\n"
11241 "};",
11242 Style);
11243
11244 verifyNoChange("template <auto x>\n"
11245 "constexpr int simple(int) {\n"
11246 " char c;\n"
11247 " return 1;\n"
11248 "}",
11249 Style);
11250 verifyFormat("template <auto x> constexpr int simple(int) {\n"
11251 " char c;\n"
11252 " return 1;\n"
11253 "}",
11254 Style);
11255
11256 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
11257 verifyNoChange("template <auto x>\n"
11258 "requires(x > 1)\n"
11259 "constexpr int with_req(int) {\n"
11260 " return 1;\n"
11261 "}",
11262 Style);
11263 verifyFormat("template <auto x> requires(x > 1)\n"
11264 "constexpr int with_req(int) {\n"
11265 " return 1;\n"
11266 "}",
11267 Style);
11268}
11269
11270TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
11271 FormatStyle Style = getGoogleStyle(Language: FormatStyle::LK_Cpp);
11272 Style.ColumnLimit = 60;
11273 verifyFormat("// Baseline - no comments.\n"
11274 "template <\n"
11275 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
11276 "void f() {}",
11277 Style);
11278
11279 verifyFormat("template <\n"
11280 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11281 "void f() {}",
11282 "template <\n"
11283 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11284 "void f() {}",
11285 Style);
11286
11287 verifyFormat(
11288 "template <\n"
11289 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11290 "void f() {}",
11291 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11292 "void f() {}",
11293 Style);
11294
11295 verifyFormat("template <\n"
11296 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11297 " // multiline\n"
11298 "void f() {}",
11299 "template <\n"
11300 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11301 " // multiline\n"
11302 "void f() {}",
11303 Style);
11304
11305 verifyFormat(
11306 "template <typename aaaaaaaaaa<\n"
11307 " bbbbbbbbbbbb>::value> // trailing loooong\n"
11308 "void f() {}",
11309 "template <\n"
11310 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11311 "void f() {}",
11312 Style);
11313}
11314
11315TEST_F(FormatTest, BreakBeforeTemplateCloser) {
11316 auto Style = getLLVMStyle();
11317 // Begin with tests covering the case where there is no constraint on the
11318 // column limit.
11319 Style.ColumnLimit = 0;
11320 Style.BreakBeforeTemplateCloser = true;
11321 // BreakBeforeTemplateCloser should NOT force template declarations onto
11322 // multiple lines.
11323 verifyFormat("template <typename Foo>\n"
11324 "void foo() {}",
11325 Style);
11326 verifyFormat("template <typename Foo, typename Bar>\n"
11327 "void foo() {}",
11328 Style);
11329 // It should add a line break before > if not already present:
11330 verifyFormat("template <\n"
11331 " typename Foo\n"
11332 ">\n"
11333 "void foo() {}",
11334 "template <\n"
11335 " typename Foo>\n"
11336 "void foo() {}",
11337 Style);
11338 verifyFormat("template <\n"
11339 " typename Foo,\n"
11340 " typename Bar\n"
11341 ">\n"
11342 "void foo() {}",
11343 "template <\n"
11344 " typename Foo,\n"
11345 " typename Bar>\n"
11346 "void foo() {}",
11347 Style);
11348 // When within an indent scope, the > should be placed accordingly:
11349 verifyFormat("struct Baz {\n"
11350 " template <\n"
11351 " typename Foo,\n"
11352 " typename Bar\n"
11353 " >\n"
11354 " void foo() {}\n"
11355 "};",
11356 "struct Baz {\n"
11357 " template <\n"
11358 " typename Foo,\n"
11359 " typename Bar>\n"
11360 " void foo() {}\n"
11361 "};",
11362 Style);
11363
11364 // Test from https://github.com/llvm/llvm-project/issues/80049:
11365 verifyFormat(
11366 "using type = std::remove_cv_t<\n"
11367 " add_common_cv_reference<\n"
11368 " std::common_type_t<std::decay_t<T0>, std::decay_t<T1>>,\n"
11369 " T0,\n"
11370 " T1\n"
11371 " >\n"
11372 ">;",
11373 "using type = std::remove_cv_t<\n"
11374 " add_common_cv_reference<\n"
11375 " std::common_type_t<std::decay_t<T0>, std::decay_t<T1>>,\n"
11376 " T0,\n"
11377 " T1>>;",
11378 Style);
11379
11380 // Test lambda goes to next line:
11381 verifyFormat("void foo() {\n"
11382 " auto lambda = []<\n"
11383 " typename T\n"
11384 " >(T t) {\n"
11385 " };\n"
11386 "}",
11387 "void foo() {\n"
11388 " auto lambda = []<\n"
11389 " typename T>(T t){\n"
11390 " };\n"
11391 "}",
11392 Style);
11393 // With no column limit, two parameters can go on the same line:
11394 verifyFormat("void foo() {\n"
11395 " auto lambda = []<\n"
11396 " typename T, typename Foo\n"
11397 " >(T t) {\n"
11398 " };\n"
11399 "}",
11400 "void foo() {\n"
11401 " auto lambda = []<\n"
11402 " typename T, typename Foo>(T t){\n"
11403 " };\n"
11404 "}",
11405 Style);
11406 // Or on different lines:
11407 verifyFormat("void foo() {\n"
11408 " auto lambda = []<\n"
11409 " typename T,\n"
11410 " typename Foo\n"
11411 " >(T t) {\n"
11412 " };\n"
11413 "}",
11414 "void foo() {\n"
11415 " auto lambda = []<\n"
11416 " typename T,\n"
11417 " typename Foo>(T t){\n"
11418 " };\n"
11419 "}",
11420 Style);
11421
11422 // Test template usage goes to next line too:
11423 verifyFormat("void foo() {\n"
11424 " myFunc<\n"
11425 " T\n"
11426 " >();\n"
11427 "}",
11428 "void foo() {\n"
11429 " myFunc<\n"
11430 " T>();\n"
11431 "}",
11432 Style);
11433
11434 // Now test that it handles the cases when the column limit forces wrapping.
11435 Style.ColumnLimit = 40;
11436 // The typename goes on the first line if it fits:
11437 verifyFormat("template <typename Fooooooooooooooooooo,\n"
11438 " typename Bar>\n"
11439 "void foo() {}",
11440 Style);
11441 verifyFormat("template <typename Foo,\n"
11442 " typename Barrrrrrrrrrrrrrrrrr>\n"
11443 "void foo() {}",
11444 Style);
11445 // Long names should be split in one step:
11446 verifyFormat("template <\n"
11447 " typename Foo,\n"
11448 " typename Barrrrrrrrrrrrrrrrrrr\n"
11449 ">\n"
11450 "void foo() {}",
11451 "template <typename Foo, typename Barrrrrrrrrrrrrrrrrrr>\n"
11452 "void foo() {}",
11453 Style);
11454 verifyFormat("template <\n"
11455 " typename Foooooooooooooooooooo,\n"
11456 " typename Bar\n"
11457 ">\n"
11458 "void foo() {}",
11459 "template <typename Foooooooooooooooooooo, typename Bar>\n"
11460 "void foo() {}",
11461 Style);
11462 // Even when there is only one long name:
11463 verifyFormat("template <\n"
11464 " typename Foooooooooooooooooooo\n"
11465 ">\n"
11466 "void foo() {}",
11467 "template <typename Foooooooooooooooooooo>\n"
11468 "void foo() {}",
11469 Style);
11470 // Test lambda goes to next line if the type is looong:
11471 verifyFormat("void foo() {\n"
11472 " auto lambda =\n"
11473 " []<\n"
11474 " typename Loooooooooooooooooooooooooooooooooong\n"
11475 " >(T t) {};\n"
11476 " auto lambda =\n"
11477 " [looooooooooooooong]<\n"
11478 " typename Loooooooooooooooooooooooooooooooooong\n"
11479 " >(T t) {};\n"
11480 " auto lambda =\n"
11481 " []<\n"
11482 " typename T,\n"
11483 " typename Loooooooooooooooooooooooooooooooooong\n"
11484 " >(T t) {};\n"
11485 // Nested:
11486 " auto lambda =\n"
11487 " []<\n"
11488 " template <typename, typename>\n"
11489 " typename Looooooooooooooooooong\n"
11490 " >(T t) {};\n"
11491 // Same idea, the "T" is now short rather than Looong:
11492 " auto lambda =\n"
11493 " []<template <typename, typename>\n"
11494 " typename T>(T t) {};\n"
11495 // Nested with long capture forces the style to block indent:
11496 " auto lambda =\n"
11497 " [loooooooooooooooooooong]<\n"
11498 " template <typename, typename>\n"
11499 " typename Looooooooooooooooooong\n"
11500 " >(T t) {};\n"
11501 // But *now* it stays block indented even when T is short:
11502 " auto lambda =\n"
11503 " [loooooooooooooooooooong]<\n"
11504 " template <typename, typename>\n"
11505 " typename T\n"
11506 " >(T t) {};\n"
11507 // Nested, with long name and long captures:
11508 " auto lambda =\n"
11509 " [loooooooooooooooooooong]<\n"
11510 " template <\n"
11511 " typename Foooooooooooooooo,\n"
11512 " typename\n"
11513 " >\n"
11514 " typename T\n"
11515 " >(T t) {};\n"
11516 // Allow the nested template to be on the same line:
11517 " auto lambda =\n"
11518 " [loooooooooooooooooooong]<\n"
11519 " template <typename Fooooooooo,\n"
11520 " typename>\n"
11521 " typename T\n"
11522 " >(T t) {};\n"
11523 "}",
11524 Style);
11525
11526 // Test template usage goes to next line if the type is looong:
11527 verifyFormat("void foo() {\n"
11528 " myFunc<\n"
11529 " Looooooooooooooooooooooooong\n"
11530 " >();\n"
11531 "}",
11532 Style);
11533 // Even a single type in the middle is enough to force it to block indent
11534 // style:
11535 verifyFormat("void foo() {\n"
11536 " myFunc<\n"
11537 " Foo, Foo, Foo,\n"
11538 " Foooooooooooooooooooooooooooooo,\n"
11539 " Foo, Foo, Foo, Foo\n"
11540 " >();\n"
11541 "}",
11542 Style);
11543}
11544
11545TEST_F(FormatTest, WrapsTemplateParameters) {
11546 FormatStyle Style = getLLVMStyle();
11547 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11548 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11549 verifyFormat(
11550 "template <typename... a> struct q {};\n"
11551 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11552 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11553 " y;",
11554 Style);
11555 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
11556 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11557 verifyFormat(
11558 "template <typename... a> struct r {};\n"
11559 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11560 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11561 " y;",
11562 Style);
11563 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11564 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
11565 verifyFormat("template <typename... a> struct s {};\n"
11566 "extern s<\n"
11567 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11568 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11569 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11570 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11571 " y;",
11572 Style);
11573 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
11574 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
11575 verifyFormat("template <typename... a> struct t {};\n"
11576 "extern t<\n"
11577 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11578 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11579 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11580 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11581 " y;",
11582 Style);
11583}
11584
11585TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
11586 verifyFormat(
11587 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11589 verifyFormat(
11590 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11593
11594 // FIXME: Should we have the extra indent after the second break?
11595 verifyFormat(
11596 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11599
11600 verifyFormat(
11601 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11602 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11603
11604 // Breaking at nested name specifiers is generally not desirable.
11605 verifyFormat(
11606 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11607 " aaaaaaaaaaaaaaaaaaaaaaa);");
11608
11609 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11612 " aaaaaaaaaaaaaaaaaaaaa);",
11613 getLLVMStyleWithColumns(74));
11614
11615 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11617 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11618
11619 verifyFormat(
11620 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11621 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11622 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11623 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11624}
11625
11626TEST_F(FormatTest, UnderstandsTemplateParameters) {
11627 verifyFormat("A<int> a;");
11628 verifyFormat("A<A<A<int>>> a;");
11629 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11630 verifyFormat("bool x = a < 1 || 2 > a;");
11631 verifyFormat("bool x = 5 < f<int>();");
11632 verifyFormat("bool x = f<int>() > 5;");
11633 verifyFormat("bool x = 5 < a<int>::x;");
11634 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11635 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11636
11637 verifyGoogleFormat("A<A<int>> a;");
11638 verifyGoogleFormat("A<A<A<int>>> a;");
11639 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11640 verifyGoogleFormat("A<A<int> > a;");
11641 verifyGoogleFormat("A<A<A<int> > > a;");
11642 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11643 verifyGoogleFormat("A<::A<int>> a;");
11644 verifyGoogleFormat("A<::A> a;");
11645 verifyGoogleFormat("A< ::A> a;");
11646 verifyGoogleFormat("A< ::A<int> > a;");
11647 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11648 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11649 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11650 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11651 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11652 getGoogleStyle());
11653
11654 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
11655
11656 // template closer followed by a token that starts with > or =
11657 verifyFormat("bool b = a<1> > 1;");
11658 verifyFormat("bool b = a<1> >= 1;");
11659 verifyFormat("int i = a<1> >> 1;");
11660 FormatStyle Style = getLLVMStyle();
11661 Style.SpaceBeforeAssignmentOperators = false;
11662 verifyFormat("bool b= a<1> == 1;", Style);
11663 verifyFormat("a<int> = 1;", Style);
11664 verifyFormat("a<int> >>= 1;", Style);
11665
11666 verifyFormat("test < a | b >> c;");
11667 verifyFormat("test<test<a | b>> c;");
11668 verifyFormat("test >> a >> b;");
11669 verifyFormat("test << a >> b;");
11670
11671 verifyFormat("f<int>();");
11672 verifyFormat("template <typename T> void f() {}");
11673 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11674 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11675 "sizeof(char)>::type>;");
11676 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11677 verifyFormat("f(a.operator()<A>());");
11678 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11679 " .template operator()<A>());",
11680 getLLVMStyleWithColumns(35));
11681 verifyFormat("bool_constant<a && noexcept(f())>;");
11682 verifyFormat("bool_constant<a || noexcept(f())>;");
11683
11684 verifyFormat("if (std::tuple_size_v<T> > 0)");
11685
11686 // Not template parameters.
11687 verifyFormat("return a < b && c > d;");
11688 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11689 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11690 verifyFormat("void f() {\n"
11691 " while (a < b && c > d) {\n"
11692 " }\n"
11693 "}");
11694 verifyFormat("template <typename... Types>\n"
11695 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11696
11697 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11699 getLLVMStyleWithColumns(60));
11700 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11701 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11702 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11703 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11704
11705 verifyFormat("#define FOO(typeName, realClass) \\\n"
11706 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11707 getLLVMStyleWithColumns(60));
11708}
11709
11710TEST_F(FormatTest, UnderstandsShiftOperators) {
11711 verifyFormat("if (i < x >> 1)");
11712 verifyFormat("while (i < x >> 1)");
11713 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11714 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11715 verifyFormat(
11716 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11717 verifyFormat("Foo.call<Bar<Function>>()");
11718 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11719 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11720 "++i, v = v >> 1)");
11721 verifyFormat("if (w<u<v<x>>, 1>::t)");
11722}
11723
11724TEST_F(FormatTest, BitshiftOperatorWidth) {
11725 verifyFormat("int a = 1 << 2; /* foo\n"
11726 " bar */",
11727 "int a=1<<2; /* foo\n"
11728 " bar */");
11729
11730 verifyFormat("int b = 256 >> 1; /* foo\n"
11731 " bar */",
11732 "int b =256>>1 ; /* foo\n"
11733 " bar */");
11734}
11735
11736TEST_F(FormatTest, UnderstandsBinaryOperators) {
11737 verifyFormat("COMPARE(a, ==, b);");
11738 verifyFormat("auto s = sizeof...(Ts) - 1;");
11739}
11740
11741TEST_F(FormatTest, UnderstandsPointersToMembers) {
11742 verifyFormat("int A::*x;");
11743 verifyFormat("int (S::*func)(void *);");
11744 verifyFormat("void f() { int (S::*func)(void *); }");
11745 verifyFormat("typedef bool *(Class::*Member)() const;");
11746 verifyFormat("void f() {\n"
11747 " (a->*f)();\n"
11748 " a->*x;\n"
11749 " (a.*f)();\n"
11750 " ((*a).*f)();\n"
11751 " a.*x;\n"
11752 "}");
11753 verifyFormat("void f() {\n"
11754 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11755 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11756 "}");
11757 verifyFormat(
11758 "(aaaaaaaaaa->*bbbbbbb)(\n"
11759 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11760
11761 FormatStyle Style = getLLVMStyle();
11762 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
11763 verifyFormat("typedef bool *(Class::*Member)() const;", Style);
11764 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style);
11765
11766 Style.PointerAlignment = FormatStyle::PAS_Left;
11767 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
11768 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11769
11770 Style.PointerAlignment = FormatStyle::PAS_Middle;
11771 verifyFormat("typedef bool * (Class::*Member)() const;", Style);
11772 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style);
11773}
11774
11775TEST_F(FormatTest, UnderstandsUnaryOperators) {
11776 verifyFormat("int a = -2;");
11777 verifyFormat("f(-1, -2, -3);");
11778 verifyFormat("a[-1] = 5;");
11779 verifyFormat("int a = 5 + -2;");
11780 verifyFormat("if (i == -1) {\n}");
11781 verifyFormat("if (i != -1) {\n}");
11782 verifyFormat("if (i > -1) {\n}");
11783 verifyFormat("if (i < -1) {\n}");
11784 verifyFormat("++(a->f());");
11785 verifyFormat("--(a->f());");
11786 verifyFormat("(a->f())++;");
11787 verifyFormat("a[42]++;");
11788 verifyFormat("if (!(a->f())) {\n}");
11789 verifyFormat("if (!+i) {\n}");
11790 verifyFormat("~&a;");
11791 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11792 verifyFormat("sizeof -x");
11793 verifyFormat("sizeof +x");
11794 verifyFormat("sizeof *x");
11795 verifyFormat("sizeof &x");
11796 verifyFormat("delete +x;");
11797 verifyFormat("co_await +x;");
11798 verifyFormat("case *x:");
11799 verifyFormat("case &x:");
11800
11801 verifyFormat("a-- > b;");
11802 verifyFormat("b ? -a : c;");
11803 verifyFormat("n * sizeof char16;");
11804 verifyGoogleFormat("n * alignof char16;");
11805 verifyFormat("sizeof(char);");
11806 verifyGoogleFormat("alignof(char);");
11807
11808 verifyFormat("return -1;");
11809 verifyFormat("throw -1;");
11810 verifyFormat("switch (a) {\n"
11811 "case -1:\n"
11812 " break;\n"
11813 "}");
11814 verifyFormat("#define X -1");
11815 verifyFormat("#define X -kConstant");
11816
11817 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11818 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11819
11820 verifyFormat("int a = /* confusing comment */ -1;");
11821 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11822 verifyFormat("int a = i /* confusing comment */++;");
11823
11824 verifyFormat("co_yield -1;");
11825 verifyFormat("co_return -1;");
11826
11827 // Check that * is not treated as a binary operator when we set
11828 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11829 FormatStyle PASLeftStyle = getLLVMStyle();
11830 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left;
11831 verifyFormat("co_return *a;", PASLeftStyle);
11832 verifyFormat("co_await *a;", PASLeftStyle);
11833 verifyFormat("co_yield *a", PASLeftStyle);
11834 verifyFormat("return *a;", PASLeftStyle);
11835}
11836
11837TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
11838 verifyFormat("if (!aaaaaaaaaa( // break\n"
11839 " aaaaa)) {\n"
11840 "}");
11841 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11842 " aaaaa));");
11843 verifyFormat("*aaa = aaaaaaa( // break\n"
11844 " bbbbbb);");
11845}
11846
11847TEST_F(FormatTest, UnderstandsOverloadedOperators) {
11848 verifyFormat("bool operator<();");
11849 verifyFormat("bool operator>();");
11850 verifyFormat("bool operator=();");
11851 verifyFormat("bool operator==();");
11852 verifyFormat("bool operator!=();");
11853 verifyFormat("int operator+();");
11854 verifyFormat("int operator++();");
11855 verifyFormat("int operator++(int) volatile noexcept;");
11856 verifyFormat("bool operator,();");
11857 verifyFormat("bool operator();");
11858 verifyFormat("bool operator()();");
11859 verifyFormat("bool operator[]();");
11860 verifyFormat("operator bool();");
11861 verifyFormat("operator int();");
11862 verifyFormat("operator void *();");
11863 verifyFormat("operator SomeType<int>();");
11864 verifyFormat("operator SomeType<int, int>();");
11865 verifyFormat("operator SomeType<SomeType<int>>();");
11866 verifyFormat("operator< <>();");
11867 verifyFormat("operator<< <>();");
11868 verifyFormat("< <>");
11869
11870 verifyFormat("void *operator new(std::size_t size);");
11871 verifyFormat("void *operator new[](std::size_t size);");
11872 verifyFormat("void operator delete(void *ptr);");
11873 verifyFormat("void operator delete[](void *ptr);");
11874 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11875 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11876 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11877 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11878
11879 verifyFormat(
11880 "ostream &operator<<(ostream &OutputStream,\n"
11881 " SomeReallyLongType WithSomeReallyLongValue);");
11882 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11883 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11884 " return left.group < right.group;\n"
11885 "}");
11886 verifyFormat("SomeType &operator=(const SomeType &S);");
11887 verifyFormat("f.template operator()<int>();");
11888
11889 verifyGoogleFormat("operator void*();");
11890 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11891 verifyGoogleFormat("operator ::A();");
11892
11893 verifyFormat("using A::operator+;");
11894 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11895 "int i;");
11896
11897 // Calling an operator as a member function.
11898 verifyFormat("void f() { a.operator*(); }");
11899 verifyFormat("void f() { a.operator*(b & b); }");
11900 verifyFormat("void f() { a->operator&(a * b); }");
11901 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11902 verifyFormat("void f() { operator*(a & a); }");
11903 verifyFormat("void f() { operator&(a, b * b); }");
11904
11905 verifyFormat("void f() { return operator()(x) * b; }");
11906 verifyFormat("void f() { return operator[](x) * b; }");
11907 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11908 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11909 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11910 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11911 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11912
11913 verifyFormat("::operator delete(foo);");
11914 verifyFormat("::operator new(n * sizeof(foo));");
11915 verifyFormat("foo() { ::operator delete(foo); }");
11916 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11917}
11918
11919TEST_F(FormatTest, SpaceBeforeTemplateCloser) {
11920 verifyFormat("C<&operator- > minus;");
11921 verifyFormat("C<&operator> > gt;");
11922 verifyFormat("C<&operator>= > ge;");
11923 verifyFormat("C<&operator<= > le;");
11924 verifyFormat("C<&operator< <X>> lt;");
11925}
11926
11927TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
11928 verifyFormat("void A::b() && {}");
11929 verifyFormat("void A::b() && noexcept {}");
11930 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11931 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11932 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11933 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11934 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11935 verifyFormat("Deleted &operator=(const Deleted &) &;");
11936 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11937 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11938 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11939 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11940 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11941 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11942 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11943 verifyFormat("void Fn(T const &) const &;");
11944 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11945 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11946 verifyGoogleFormat("template <typename T>\n"
11947 "void F(T) && = delete;");
11948 verifyFormat("template <typename T> void operator=(T) &;");
11949 verifyFormat("template <typename T> void operator=(T) const &;");
11950 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11951 verifyFormat("template <typename T> void operator=(T) & = default;");
11952 verifyFormat("template <typename T> void operator=(T) &&;");
11953 verifyFormat("template <typename T> void operator=(T) && = delete;");
11954 verifyFormat("template <typename T> void operator=(T) & {}");
11955 verifyFormat("template <typename T> void operator=(T) && {}");
11956
11957 FormatStyle AlignLeft = getLLVMStyle();
11958 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
11959 verifyFormat("void A::b() && {}", AlignLeft);
11960 verifyFormat("void A::b() && noexcept {}", AlignLeft);
11961 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
11962 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11963 AlignLeft);
11964 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11965 AlignLeft);
11966 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
11967 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
11968 verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
11969 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
11970 verifyFormat("auto Function(T) & -> void {}", AlignLeft);
11971 verifyFormat("auto Function(T) & -> void;", AlignLeft);
11972 verifyFormat("void Fn(T const&) const&;", AlignLeft);
11973 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
11974 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11975 AlignLeft);
11976 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft);
11977 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft);
11978 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11979 AlignLeft);
11980 verifyFormat("template <typename T> void operator=(T) & = default;",
11981 AlignLeft);
11982 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft);
11983 verifyFormat("template <typename T> void operator=(T) && = delete;",
11984 AlignLeft);
11985 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft);
11986 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft);
11987 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft);
11988
11989 FormatStyle AlignMiddle = getLLVMStyle();
11990 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11991 verifyFormat("void A::b() && {}", AlignMiddle);
11992 verifyFormat("void A::b() && noexcept {}", AlignMiddle);
11993 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11994 AlignMiddle);
11995 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11996 AlignMiddle);
11997 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11998 AlignMiddle);
11999 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle);
12000 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle);
12001 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle);
12002 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle);
12003 verifyFormat("auto Function(T) & -> void {}", AlignMiddle);
12004 verifyFormat("auto Function(T) & -> void;", AlignMiddle);
12005 verifyFormat("void Fn(T const &) const &;", AlignMiddle);
12006 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle);
12007 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
12008 AlignMiddle);
12009 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle);
12010 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle);
12011 verifyFormat("template <typename T> void operator=(T) & noexcept;",
12012 AlignMiddle);
12013 verifyFormat("template <typename T> void operator=(T) & = default;",
12014 AlignMiddle);
12015 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle);
12016 verifyFormat("template <typename T> void operator=(T) && = delete;",
12017 AlignMiddle);
12018 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle);
12019 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
12020
12021 FormatStyle Spaces = getLLVMStyle();
12022 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
12023 Spaces.SpacesInParensOptions = {};
12024 Spaces.SpacesInParensOptions.InCStyleCasts = true;
12025 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
12026 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
12027 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
12028 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
12029
12030 Spaces.SpacesInParensOptions.InCStyleCasts = false;
12031 Spaces.SpacesInParensOptions.Other = true;
12032 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
12033 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
12034 Spaces);
12035 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
12036 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
12037
12038 FormatStyle BreakTemplate = getLLVMStyle();
12039 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
12040
12041 verifyFormat("struct f {\n"
12042 " template <class T>\n"
12043 " int &foo(const std::string &str) & noexcept {}\n"
12044 "};",
12045 BreakTemplate);
12046
12047 verifyFormat("struct f {\n"
12048 " template <class T>\n"
12049 " int &foo(const std::string &str) && noexcept {}\n"
12050 "};",
12051 BreakTemplate);
12052
12053 verifyFormat("struct f {\n"
12054 " template <class T>\n"
12055 " int &foo(const std::string &str) const & noexcept {}\n"
12056 "};",
12057 BreakTemplate);
12058
12059 verifyFormat("struct f {\n"
12060 " template <class T>\n"
12061 " int &foo(const std::string &str) const & noexcept {}\n"
12062 "};",
12063 BreakTemplate);
12064
12065 verifyFormat("struct f {\n"
12066 " template <class T>\n"
12067 " auto foo(const std::string &str) && noexcept -> int & {}\n"
12068 "};",
12069 BreakTemplate);
12070
12071 FormatStyle AlignLeftBreakTemplate = getLLVMStyle();
12072 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
12073 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left;
12074
12075 verifyFormat("struct f {\n"
12076 " template <class T>\n"
12077 " int& foo(const std::string& str) & noexcept {}\n"
12078 "};",
12079 AlignLeftBreakTemplate);
12080
12081 verifyFormat("struct f {\n"
12082 " template <class T>\n"
12083 " int& foo(const std::string& str) && noexcept {}\n"
12084 "};",
12085 AlignLeftBreakTemplate);
12086
12087 verifyFormat("struct f {\n"
12088 " template <class T>\n"
12089 " int& foo(const std::string& str) const& noexcept {}\n"
12090 "};",
12091 AlignLeftBreakTemplate);
12092
12093 verifyFormat("struct f {\n"
12094 " template <class T>\n"
12095 " int& foo(const std::string& str) const&& noexcept {}\n"
12096 "};",
12097 AlignLeftBreakTemplate);
12098
12099 verifyFormat("struct f {\n"
12100 " template <class T>\n"
12101 " auto foo(const std::string& str) && noexcept -> int& {}\n"
12102 "};",
12103 AlignLeftBreakTemplate);
12104
12105 // The `&` in `Type&` should not be confused with a trailing `&` of
12106 // DEPRECATED(reason) member function.
12107 verifyFormat("struct f {\n"
12108 " template <class T>\n"
12109 " DEPRECATED(reason)\n"
12110 " Type &foo(arguments) {}\n"
12111 "};",
12112 BreakTemplate);
12113
12114 verifyFormat("struct f {\n"
12115 " template <class T>\n"
12116 " DEPRECATED(reason)\n"
12117 " Type& foo(arguments) {}\n"
12118 "};",
12119 AlignLeftBreakTemplate);
12120
12121 verifyFormat("void (*foopt)(int) = &func;");
12122
12123 FormatStyle DerivePointerAlignment = getLLVMStyle();
12124 DerivePointerAlignment.DerivePointerAlignment = true;
12125 // There's always a space between the function and its trailing qualifiers.
12126 // This isn't evidence for PAS_Right (or for PAS_Left).
12127 std::string Prefix = "void a() &;\n"
12128 "void b() &;\n";
12129 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
12130 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
12131 // Same if the function is an overloaded operator, and with &&.
12132 Prefix = "void operator()() &&;\n"
12133 "void operator()() &&;\n";
12134 verifyFormat(Prefix + "int* x;", DerivePointerAlignment);
12135 verifyFormat(Prefix + "int *x;", DerivePointerAlignment);
12136 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
12137 Prefix = "void a() const &;\n"
12138 "void b() const &;\n";
12139 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment);
12140}
12141
12142TEST_F(FormatTest, PointerAlignmentFallback) {
12143 FormatStyle Style = getLLVMStyle();
12144 Style.DerivePointerAlignment = true;
12145
12146 const StringRef Code("int* p;\n"
12147 "int *q;\n"
12148 "int * r;");
12149
12150 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right);
12151 verifyFormat("int *p;\n"
12152 "int *q;\n"
12153 "int *r;",
12154 Code, Style);
12155
12156 Style.PointerAlignment = FormatStyle::PAS_Left;
12157 verifyFormat("int* p;\n"
12158 "int* q;\n"
12159 "int* r;",
12160 Code, Style);
12161
12162 Style.PointerAlignment = FormatStyle::PAS_Middle;
12163 verifyFormat("int * p;\n"
12164 "int * q;\n"
12165 "int * r;",
12166 Code, Style);
12167}
12168
12169TEST_F(FormatTest, UnderstandsNewAndDelete) {
12170 verifyFormat("A(void *p) : a(new (p) int) {}");
12171 verifyFormat("void f() {\n"
12172 " A *a = new A;\n"
12173 " A *a = new (placement) A;\n"
12174 " delete a;\n"
12175 " delete (A *)a;\n"
12176 "}");
12177 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
12178 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
12179 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12180 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
12181 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
12182 verifyFormat("delete[] h->p;");
12183 verifyFormat("delete[] (void *)p;");
12184
12185 verifyFormat("void operator delete(void *foo) ATTRIB;");
12186 verifyFormat("void operator new(void *foo) ATTRIB;");
12187 verifyFormat("void operator delete[](void *foo) ATTRIB;");
12188 verifyFormat("void operator delete(void *ptr) noexcept;");
12189
12190 verifyFormat("void new(link p);\n"
12191 "void delete(link p);",
12192 "void new (link p);\n"
12193 "void delete (link p);",
12194 getLLVMStyle(FormatStyle::LK_C));
12195
12196 verifyFormat("{\n"
12197 " p->new();\n"
12198 "}\n"
12199 "{\n"
12200 " p->delete();\n"
12201 "}",
12202 "{\n"
12203 " p->new ();\n"
12204 "}\n"
12205 "{\n"
12206 " p->delete ();\n"
12207 "}");
12208
12209 FormatStyle AfterPlacementOperator = getLLVMStyle();
12210 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
12211 EXPECT_TRUE(
12212 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
12213 verifyFormat("new (buf) int;", AfterPlacementOperator);
12214 verifyFormat("struct A {\n"
12215 " int *a;\n"
12216 " A(int *p) : a(new (p) int) {\n"
12217 " new (p) int;\n"
12218 " int *b = new (p) int;\n"
12219 " int *c = new (p) int(3);\n"
12220 " delete (b);\n"
12221 " }\n"
12222 "};",
12223 AfterPlacementOperator);
12224 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
12225 verifyFormat("delete (int *)p;", AfterPlacementOperator);
12226
12227 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
12228 false;
12229 verifyFormat("new(buf) int;", AfterPlacementOperator);
12230 verifyFormat("struct A {\n"
12231 " int *a;\n"
12232 " A(int *p) : a(new(p) int) {\n"
12233 " new(p) int;\n"
12234 " int *b = new(p) int;\n"
12235 " int *c = new(p) int(3);\n"
12236 " delete(b);\n"
12237 " }\n"
12238 "};",
12239 AfterPlacementOperator);
12240 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
12241 verifyFormat("delete (int *)p;", AfterPlacementOperator);
12242}
12243
12244TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
12245 verifyFormat("int *f(int *a) {}");
12246 verifyFormat("int main(int argc, char **argv) {}");
12247 verifyFormat("Test::Test(int b) : a(b * b) {}");
12248 verifyIndependentOfContext("f(a, *a);");
12249 verifyFormat("void g() { f(*a); }");
12250 verifyIndependentOfContext("int a = b * 10;");
12251 verifyIndependentOfContext("int a = 10 * b;");
12252 verifyIndependentOfContext("int a = b * c;");
12253 verifyIndependentOfContext("int a += b * c;");
12254 verifyIndependentOfContext("int a -= b * c;");
12255 verifyIndependentOfContext("int a *= b * c;");
12256 verifyIndependentOfContext("int a /= b * c;");
12257 verifyIndependentOfContext("int a = *b;");
12258 verifyIndependentOfContext("int a = *b * c;");
12259 verifyIndependentOfContext("int a = b * *c;");
12260 verifyIndependentOfContext("int a = b * (10);");
12261 verifyIndependentOfContext("S << b * (10);");
12262 verifyIndependentOfContext("return 10 * b;");
12263 verifyIndependentOfContext("return *b * *c;");
12264 verifyIndependentOfContext("return a & ~b;");
12265 verifyIndependentOfContext("f(b ? *c : *d);");
12266 verifyIndependentOfContext("int a = b ? *c : *d;");
12267 verifyIndependentOfContext("*b = a;");
12268 verifyIndependentOfContext("a * ~b;");
12269 verifyIndependentOfContext("a * !b;");
12270 verifyIndependentOfContext("a * +b;");
12271 verifyIndependentOfContext("a * -b;");
12272 verifyIndependentOfContext("a * ++b;");
12273 verifyIndependentOfContext("a * --b;");
12274 verifyIndependentOfContext("a[4] * b;");
12275 verifyIndependentOfContext("a[a * a] = 1;");
12276 verifyIndependentOfContext("f() * b;");
12277 verifyIndependentOfContext("a * [self dostuff];");
12278 verifyIndependentOfContext("int x = a * (a + b);");
12279 verifyIndependentOfContext("(a *)(a + b);");
12280 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
12281 verifyIndependentOfContext("int *pa = (int *)&a;");
12282 verifyIndependentOfContext("return sizeof(int **);");
12283 verifyIndependentOfContext("return sizeof(int ******);");
12284 verifyIndependentOfContext("return (int **&)a;");
12285 verifyIndependentOfContext("f((*PointerToArray)[10]);");
12286 verifyFormat("void f(Type (*parameter)[10]) {}");
12287 verifyFormat("void f(Type (&parameter)[10]) {}");
12288 verifyGoogleFormat("return sizeof(int**);");
12289 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
12290 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
12291 verifyFormat("auto a = [](int **&, int ***) {};");
12292 verifyFormat("auto PointerBinding = [](const char *S) {};");
12293 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
12294 verifyFormat("[](const decltype(*a) &value) {}");
12295 verifyFormat("[](const typeof(*a) &value) {}");
12296 verifyFormat("[](const _Atomic(a *) &value) {}");
12297 verifyFormat("[](const __underlying_type(a) &value) {}");
12298 verifyFormat("decltype(a * b) F();");
12299 verifyFormat("typeof(a * b) F();");
12300 verifyFormat("#define MACRO() [](A *a) { return 1; }");
12301 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
12302 verifyIndependentOfContext("typedef void (*f)(int *a);");
12303 verifyIndependentOfContext("typedef void (*f)(Type *a);");
12304 verifyIndependentOfContext("int i{a * b};");
12305 verifyIndependentOfContext("aaa && aaa->f();");
12306 verifyIndependentOfContext("int x = ~*p;");
12307 verifyFormat("Constructor() : a(a), area(width * height) {}");
12308 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
12309 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
12310 verifyFormat("void f() { f(a, c * d); }");
12311 verifyFormat("void f() { f(new a(), c * d); }");
12312 verifyFormat("void f(const MyOverride &override);");
12313 verifyFormat("void f(const MyFinal &final);");
12314 verifyIndependentOfContext("bool a = f() && override.f();");
12315 verifyIndependentOfContext("bool a = f() && final.f();");
12316
12317 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
12318
12319 verifyIndependentOfContext("A<int *> a;");
12320 verifyIndependentOfContext("A<int **> a;");
12321 verifyIndependentOfContext("A<int *, int *> a;");
12322 verifyIndependentOfContext("A<int *[]> a;");
12323 verifyIndependentOfContext(
12324 "const char *const p = reinterpret_cast<const char *const>(q);");
12325 verifyIndependentOfContext("A<int **, int **> a;");
12326 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
12327 verifyFormat("for (char **a = b; *a; ++a) {\n}");
12328 verifyFormat("for (; a && b;) {\n}");
12329 verifyFormat("bool foo = true && [] { return false; }();");
12330
12331 verifyFormat(
12332 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12334
12335 verifyGoogleFormat("int const* a = &b;");
12336 verifyGoogleFormat("**outparam = 1;");
12337 verifyGoogleFormat("*outparam = a * b;");
12338 verifyGoogleFormat("int main(int argc, char** argv) {}");
12339 verifyGoogleFormat("A<int*> a;");
12340 verifyGoogleFormat("A<int**> a;");
12341 verifyGoogleFormat("A<int*, int*> a;");
12342 verifyGoogleFormat("A<int**, int**> a;");
12343 verifyGoogleFormat("f(b ? *c : *d);");
12344 verifyGoogleFormat("int a = b ? *c : *d;");
12345 verifyGoogleFormat("Type* t = **x;");
12346 verifyGoogleFormat("Type* t = *++*x;");
12347 verifyGoogleFormat("*++*x;");
12348 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
12349 verifyGoogleFormat("Type* t = x++ * y;");
12350 verifyGoogleFormat(
12351 "const char* const p = reinterpret_cast<const char* const>(q);");
12352 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
12353 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
12354 verifyGoogleFormat("template <typename T>\n"
12355 "void f(int i = 0, SomeType** temps = NULL);");
12356
12357 FormatStyle Left = getLLVMStyle();
12358 Left.PointerAlignment = FormatStyle::PAS_Left;
12359 verifyFormat("x = *a(x) = *a(y);", Left);
12360 verifyFormat("for (;; *a = b) {\n}", Left);
12361 verifyFormat("return *this += 1;", Left);
12362 verifyFormat("throw *x;", Left);
12363 verifyFormat("delete *x;", Left);
12364 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
12365 verifyFormat("[](const decltype(*a)* ptr) {}", Left);
12366 verifyFormat("[](const typeof(*a)* ptr) {}", Left);
12367 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left);
12368 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left);
12369 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
12370 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left);
12371 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left);
12372 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left);
12373
12374 verifyIndependentOfContext("a = *(x + y);");
12375 verifyIndependentOfContext("a = &(x + y);");
12376 verifyIndependentOfContext("*(x + y).call();");
12377 verifyIndependentOfContext("&(x + y)->call();");
12378 verifyFormat("void f() { &(*I).first; }");
12379
12380 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
12381 verifyFormat("f(* /* confusing comment */ foo);");
12382 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
12383 verifyFormat("void foo(int * // this is the first paramters\n"
12384 " ,\n"
12385 " int second);");
12386 verifyFormat("double term = a * // first\n"
12387 " b;");
12388 verifyFormat(
12389 "int *MyValues = {\n"
12390 " *A, // Operator detection might be confused by the '{'\n"
12391 " *BB // Operator detection might be confused by previous comment\n"
12392 "};");
12393
12394 verifyIndependentOfContext("if (int *a = &b)");
12395 verifyIndependentOfContext("if (int &a = *b)");
12396 verifyIndependentOfContext("if (a & b[i])");
12397 verifyIndependentOfContext("if constexpr (a & b[i])");
12398 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
12399 verifyIndependentOfContext("if (a * (b * c))");
12400 verifyIndependentOfContext("if constexpr (a * (b * c))");
12401 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
12402 verifyIndependentOfContext("if (a::b::c::d & b[i])");
12403 verifyIndependentOfContext("if (*b[i])");
12404 verifyIndependentOfContext("if (int *a = (&b))");
12405 verifyIndependentOfContext("while (int *a = &b)");
12406 verifyIndependentOfContext("while (a * (b * c))");
12407 verifyIndependentOfContext("size = sizeof *a;");
12408 verifyIndependentOfContext("if (a && (b = c))");
12409 verifyFormat("void f() {\n"
12410 " for (const int &v : Values) {\n"
12411 " }\n"
12412 "}");
12413 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
12414 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
12415 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
12416
12417 verifyFormat("#define A (!a * b)");
12418 verifyFormat("#define MACRO \\\n"
12419 " int *i = a * b; \\\n"
12420 " void f(a *b);",
12421 getLLVMStyleWithColumns(19));
12422
12423 verifyIndependentOfContext("A = new SomeType *[Length];");
12424 verifyIndependentOfContext("A = new SomeType *[Length]();");
12425 verifyIndependentOfContext("T **t = new T *;");
12426 verifyIndependentOfContext("T **t = new T *();");
12427 verifyGoogleFormat("A = new SomeType*[Length]();");
12428 verifyGoogleFormat("A = new SomeType*[Length];");
12429 verifyGoogleFormat("T** t = new T*;");
12430 verifyGoogleFormat("T** t = new T*();");
12431
12432 verifyFormat("STATIC_ASSERT((a & b) == 0);");
12433 verifyFormat("STATIC_ASSERT(0 == (a & b));");
12434 verifyFormat("template <bool a, bool b> "
12435 "typename t::if<x && y>::type f() {}");
12436 verifyFormat("template <int *y> f() {}");
12437 verifyFormat("vector<int *> v;");
12438 verifyFormat("vector<int *const> v;");
12439 verifyFormat("vector<int *const **const *> v;");
12440 verifyFormat("vector<int *volatile> v;");
12441 verifyFormat("vector<a *_Nonnull> v;");
12442 verifyFormat("vector<a *_Nullable> v;");
12443 verifyFormat("vector<a *_Null_unspecified> v;");
12444 verifyGoogleFormat("vector<a* absl_nonnull> v;");
12445 verifyGoogleFormat("vector<a* absl_nullable> v;");
12446 verifyGoogleFormat("vector<a* absl_nullability_unknown> v;");
12447 verifyFormat("vector<a *__ptr32> v;");
12448 verifyFormat("vector<a *__ptr64> v;");
12449 verifyFormat("vector<a *__capability> v;");
12450 FormatStyle TypeMacros = getLLVMStyle();
12451 TypeMacros.TypenameMacros = {"LIST"};
12452 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros);
12453 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros);
12454 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros);
12455 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros);
12456 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication
12457
12458 FormatStyle CustomQualifier = getLLVMStyle();
12459 // Add identifiers that should not be parsed as a qualifier by default.
12460 CustomQualifier.AttributeMacros.push_back(x: "__my_qualifier");
12461 CustomQualifier.AttributeMacros.push_back(x: "_My_qualifier");
12462 CustomQualifier.AttributeMacros.push_back(x: "my_other_qualifier");
12463 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
12464 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier);
12465 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
12466 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier);
12467 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
12468 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier);
12469 verifyFormat("vector<a * _NotAQualifier> v;");
12470 verifyFormat("vector<a * __not_a_qualifier> v;");
12471 verifyFormat("vector<a * b> v;");
12472 verifyFormat("foo<b && false>();");
12473 verifyFormat("foo<b & 1>();");
12474 verifyFormat("foo<b & (1)>();");
12475 verifyFormat("foo<b & (~0)>();");
12476 verifyFormat("foo<b & (true)>();");
12477 verifyFormat("foo<b & ((1))>();");
12478 verifyFormat("foo<b & (/*comment*/ 1)>();");
12479 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12480 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12481 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12482 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12483 verifyFormat(
12484 "template <class T, class = typename std::enable_if<\n"
12485 " std::is_integral<T>::value &&\n"
12486 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12487 "void F();",
12488 getLLVMStyleWithColumns(70));
12489 verifyFormat("template <class T,\n"
12490 " class = typename std::enable_if<\n"
12491 " std::is_integral<T>::value &&\n"
12492 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12493 " class U>\n"
12494 "void F();",
12495 getLLVMStyleWithColumns(70));
12496 verifyFormat(
12497 "template <class T,\n"
12498 " class = typename ::std::enable_if<\n"
12499 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12500 "void F();",
12501 getGoogleStyleWithColumns(68));
12502
12503 FormatStyle Style = getLLVMStyle();
12504 Style.PointerAlignment = FormatStyle::PAS_Left;
12505 verifyFormat("struct {\n"
12506 "}* ptr;",
12507 Style);
12508 verifyFormat("union {\n"
12509 "}* ptr;",
12510 Style);
12511 verifyFormat("class {\n"
12512 "}* ptr;",
12513 Style);
12514 // Don't confuse a multiplication after a brace-initialized expression with
12515 // a class pointer.
12516 verifyFormat("int i = int{42} * 34;", Style);
12517 verifyFormat("struct {\n"
12518 "}&& ptr = {};",
12519 Style);
12520 verifyFormat("union {\n"
12521 "}&& ptr = {};",
12522 Style);
12523 verifyFormat("class {\n"
12524 "}&& ptr = {};",
12525 Style);
12526 verifyFormat("bool b = 3 == int{3} && true;");
12527
12528 Style.PointerAlignment = FormatStyle::PAS_Middle;
12529 verifyFormat("struct {\n"
12530 "} * ptr;",
12531 Style);
12532 verifyFormat("union {\n"
12533 "} * ptr;",
12534 Style);
12535 verifyFormat("class {\n"
12536 "} * ptr;",
12537 Style);
12538 verifyFormat("struct {\n"
12539 "} && ptr = {};",
12540 Style);
12541 verifyFormat("union {\n"
12542 "} && ptr = {};",
12543 Style);
12544 verifyFormat("class {\n"
12545 "} && ptr = {};",
12546 Style);
12547
12548 Style.PointerAlignment = FormatStyle::PAS_Right;
12549 verifyFormat("struct {\n"
12550 "} *ptr;",
12551 Style);
12552 verifyFormat("union {\n"
12553 "} *ptr;",
12554 Style);
12555 verifyFormat("class {\n"
12556 "} *ptr;",
12557 Style);
12558 verifyFormat("struct {\n"
12559 "} &&ptr = {};",
12560 Style);
12561 verifyFormat("union {\n"
12562 "} &&ptr = {};",
12563 Style);
12564 verifyFormat("class {\n"
12565 "} &&ptr = {};",
12566 Style);
12567
12568 Style.PointerAlignment = FormatStyle::PAS_Left;
12569 verifyFormat("delete[] *ptr;", Style);
12570 verifyFormat("delete[] **ptr;", Style);
12571 verifyFormat("delete[] *(ptr);", Style);
12572
12573 verifyIndependentOfContext("MACRO(int *i);");
12574 verifyIndependentOfContext("MACRO(auto *a);");
12575 verifyIndependentOfContext("MACRO(const A *a);");
12576 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12577 verifyIndependentOfContext("MACRO(decltype(A) *a);");
12578 verifyIndependentOfContext("MACRO(typeof(A) *a);");
12579 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12580 verifyIndependentOfContext("MACRO(A *const a);");
12581 verifyIndependentOfContext("MACRO(A *restrict a);");
12582 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12583 verifyIndependentOfContext("MACRO(A *__restrict a);");
12584 verifyIndependentOfContext("MACRO(A *volatile a);");
12585 verifyIndependentOfContext("MACRO(A *__volatile a);");
12586 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12587 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12588 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12589 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12590
12591 Style = getGoogleStyle();
12592 verifyIndependentOfContext("MACRO(A* absl_nonnull a);", Style);
12593 verifyIndependentOfContext("MACRO(A* absl_nullable a);", Style);
12594 verifyIndependentOfContext("MACRO(A* absl_nullability_unknown a);", Style);
12595
12596 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12597 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12598 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12599 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12600 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12601 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12602 verifyIndependentOfContext("MACRO(A *__capability);");
12603 verifyIndependentOfContext("MACRO(A &__capability);");
12604 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12605 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12606 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12607 // a type declaration:
12608 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier);
12609 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier);
12610 // Also check that TypenameMacros prevents parsing it as multiplication:
12611 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12612 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type
12613
12614 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12615 verifyFormat("void f() { f(float{1}, a * a); }");
12616 verifyFormat("void f() { f(float(1), a * a); }");
12617
12618 verifyFormat("f((void (*)(int))g);");
12619 verifyFormat("f((void (&)(int))g);");
12620 verifyFormat("f((void (^)(int))g);");
12621
12622 // FIXME: Is there a way to make this work?
12623 // verifyIndependentOfContext("MACRO(A *a);");
12624 verifyFormat("MACRO(A &B);");
12625 verifyFormat("MACRO(A *B);");
12626 verifyFormat("void f() { MACRO(A * B); }");
12627 verifyFormat("void f() { MACRO(A & B); }");
12628
12629 // This lambda was mis-formatted after D88956 (treating it as a binop):
12630 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12631 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12632 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12633 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12634
12635 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12636 verifyFormat("return options != nullptr && operator==(*options);");
12637
12638 verifyFormat("#define OP(x) \\\n"
12639 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12640 " return s << a.DebugString(); \\\n"
12641 " }",
12642 "#define OP(x) \\\n"
12643 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12644 " return s << a.DebugString(); \\\n"
12645 " }",
12646 getLLVMStyleWithColumns(50));
12647
12648 verifyFormat("#define FOO \\\n"
12649 " void foo() { \\\n"
12650 " operator+(a * b); \\\n"
12651 " }",
12652 getLLVMStyleWithColumns(25));
12653
12654 // FIXME: We cannot handle this case yet; we might be able to figure out that
12655 // foo<x> d > v; doesn't make sense.
12656 verifyFormat("foo<a<b && c> d> v;");
12657
12658 FormatStyle PointerMiddle = getLLVMStyle();
12659 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
12660 verifyFormat("delete *x;", PointerMiddle);
12661 verifyFormat("int * x;", PointerMiddle);
12662 verifyFormat("int *[] x;", PointerMiddle);
12663 verifyFormat("template <int * y> f() {}", PointerMiddle);
12664 verifyFormat("int * f(int * a) {}", PointerMiddle);
12665 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
12666 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
12667 verifyFormat("A<int *> a;", PointerMiddle);
12668 verifyFormat("A<int **> a;", PointerMiddle);
12669 verifyFormat("A<int *, int *> a;", PointerMiddle);
12670 verifyFormat("A<int *[]> a;", PointerMiddle);
12671 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
12672 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
12673 verifyFormat("T ** t = new T *;", PointerMiddle);
12674
12675 // Member function reference qualifiers aren't binary operators.
12676 verifyFormat("string // break\n"
12677 "operator()() & {}");
12678 verifyFormat("string // break\n"
12679 "operator()() && {}");
12680 verifyGoogleFormat("template <typename T>\n"
12681 "auto x() & -> int {}");
12682
12683 // Should be binary operators when used as an argument expression (overloaded
12684 // operator invoked as a member function).
12685 verifyFormat("void f() { a.operator()(a * a); }");
12686 verifyFormat("void f() { a->operator()(a & a); }");
12687 verifyFormat("void f() { a.operator()(*a & *a); }");
12688 verifyFormat("void f() { a->operator()(*a * *a); }");
12689
12690 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12691 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12692
12693 verifyFormat("val1 & val2;");
12694 verifyFormat("val1 & val2 & val3;");
12695 verifyFormat("class c {\n"
12696 " void func(type &a) { a & member; }\n"
12697 " anotherType &member;\n"
12698 "}");
12699}
12700
12701TEST_F(FormatTest, UnderstandsAttributes) {
12702 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12703 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12704 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12705 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12706 FormatStyle AfterType = getLLVMStyle();
12707 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
12708 verifyFormat("__attribute__((nodebug)) void\n"
12709 "foo() {}",
12710 AfterType);
12711 verifyFormat("__unused void\n"
12712 "foo() {}",
12713 AfterType);
12714
12715 FormatStyle CustomAttrs = getLLVMStyle();
12716 CustomAttrs.AttributeMacros.push_back(x: "my_attr_name");
12717 verifyFormat("void MyGoodOldFunction(\n"
12718 " void *const long_enough = nullptr,\n"
12719 " void *my_attr_name even_longeeeeeeeeeeeeeeeeer = nullptr);",
12720 CustomAttrs);
12721
12722 CustomAttrs.AttributeMacros.push_back(x: "__unused");
12723 CustomAttrs.AttributeMacros.push_back(x: "__attr1");
12724 CustomAttrs.AttributeMacros.push_back(x: "__attr2");
12725 CustomAttrs.AttributeMacros.push_back(x: "no_underscore_attr");
12726 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12727 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12728 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12729 // Check that it is parsed as a multiplication without AttributeMacros and
12730 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12731 verifyFormat("vector<SomeType * __attr1> v;");
12732 verifyFormat("vector<SomeType __attr1 *> v;");
12733 verifyFormat("vector<SomeType __attr1 *const> v;");
12734 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12735 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs);
12736 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs);
12737 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs);
12738 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs);
12739 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs);
12740 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs);
12741 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs);
12742 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs);
12743 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs);
12744 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs);
12745
12746 // Check that these are not parsed as function declarations:
12747 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12748 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman;
12749 verifyFormat("SomeType s(InitValue);", CustomAttrs);
12750 verifyFormat("SomeType s{InitValue};", CustomAttrs);
12751 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs);
12752 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs);
12753 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs);
12754 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
12755 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
12756 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12757 verifyGoogleFormat("SomeType* absl_nonnull s(InitValue);");
12758 verifyGoogleFormat("SomeType* absl_nonnull s{InitValue};");
12759 verifyGoogleFormat("SomeType* absl_nullable s(InitValue);");
12760 verifyGoogleFormat("SomeType* absl_nullable s{InitValue};");
12761 verifyGoogleFormat("SomeType* absl_nullability_unknown s(InitValue);");
12762 verifyGoogleFormat("SomeType* absl_nullability_unknown s{InitValue};");
12763
12764 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
12765 Style.AttributeMacros.push_back(x: "my_fancy_attr");
12766 Style.PointerAlignment = FormatStyle::PAS_Left;
12767 verifyFormat("void foo(const MyLongTypeNameeeeeeeeeeeee* my_fancy_attr\n"
12768 " testttttttttt);",
12769 Style);
12770}
12771
12772TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
12773 // Check that qualifiers on pointers don't break parsing of casts.
12774 verifyFormat("x = (foo *const)*v;");
12775 verifyFormat("x = (foo *volatile)*v;");
12776 verifyFormat("x = (foo *restrict)*v;");
12777 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12778 verifyFormat("x = (foo *_Nonnull)*v;");
12779 verifyFormat("x = (foo *_Nullable)*v;");
12780 verifyFormat("x = (foo *_Null_unspecified)*v;");
12781 verifyGoogleFormat("x = (foo* absl_nonnull)*v;");
12782 verifyGoogleFormat("x = (foo* absl_nullable)*v;");
12783 verifyGoogleFormat("x = (foo* absl_nullability_unknown)*v;");
12784 verifyFormat("x = (foo *[[clang::attr]])*v;");
12785 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12786 verifyFormat("x = (foo *__ptr32)*v;");
12787 verifyFormat("x = (foo *__ptr64)*v;");
12788 verifyFormat("x = (foo *__capability)*v;");
12789
12790 // Check that we handle multiple trailing qualifiers and skip them all to
12791 // determine that the expression is a cast to a pointer type.
12792 FormatStyle LongPointerRight = getLLVMStyleWithColumns(ColumnLimit: 999);
12793 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(ColumnLimit: 999);
12794 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left;
12795 StringRef AllQualifiers =
12796 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12797 "_Nullable [[clang::attr]] __ptr32 __ptr64 __capability";
12798 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight);
12799 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft);
12800
12801 // Also check that address-of is not parsed as a binary bitwise-and:
12802 verifyFormat("x = (foo *const)&v;");
12803 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight);
12804 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft);
12805
12806 // Check custom qualifiers:
12807 FormatStyle CustomQualifier = getLLVMStyleWithColumns(ColumnLimit: 999);
12808 CustomQualifier.AttributeMacros.push_back(x: "__my_qualifier");
12809 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12810 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier);
12811 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(),
12812 CustomQualifier);
12813 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(),
12814 CustomQualifier);
12815
12816 // Check that unknown identifiers result in binary operator parsing:
12817 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12818 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12819}
12820
12821TEST_F(FormatTest, UnderstandsSquareAttributes) {
12822 verifyFormat("SomeType s [[unused]] (InitValue);");
12823 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12824 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12825 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12826 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12827 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12828 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12829 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12830 verifyFormat("[[nodiscard]] bool f() { return false; }");
12831 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12832 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12833 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12834 verifyFormat("[[nodiscard]] ::qualified_type f();");
12835
12836 // Make sure we do not mistake attributes for array subscripts.
12837 verifyFormat("int a() {}\n"
12838 "[[unused]] int b() {}");
12839 verifyFormat("NSArray *arr;\n"
12840 "arr[[Foo() bar]];");
12841
12842 // On the other hand, we still need to correctly find array subscripts.
12843 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12844
12845 // Make sure that we do not mistake Objective-C method inside array literals
12846 // as attributes, even if those method names are also keywords.
12847 verifyFormat("@[ [foo bar] ];");
12848 verifyFormat("@[ [NSArray class] ];");
12849 verifyFormat("@[ [foo enum] ];");
12850
12851 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12852
12853 // Make sure we do not parse attributes as lambda introducers.
12854 FormatStyle MultiLineFunctions = getLLVMStyle();
12855 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
12856 verifyFormat("[[unused]] int b() {\n"
12857 " return 42;\n"
12858 "}",
12859 MultiLineFunctions);
12860}
12861
12862TEST_F(FormatTest, AttributeClass) {
12863 FormatStyle Style = getChromiumStyle(Language: FormatStyle::LK_Cpp);
12864 verifyFormat("class S {\n"
12865 " S(S&&) = default;\n"
12866 "};",
12867 Style);
12868 verifyFormat("class [[nodiscard]] S {\n"
12869 " S(S&&) = default;\n"
12870 "};",
12871 Style);
12872 verifyFormat("class __attribute((maybeunused)) S {\n"
12873 " S(S&&) = default;\n"
12874 "};",
12875 Style);
12876 verifyFormat("struct S {\n"
12877 " S(S&&) = default;\n"
12878 "};",
12879 Style);
12880 verifyFormat("struct [[nodiscard]] S {\n"
12881 " S(S&&) = default;\n"
12882 "};",
12883 Style);
12884}
12885
12886TEST_F(FormatTest, AttributesAfterMacro) {
12887 FormatStyle Style = getLLVMStyle();
12888 verifyFormat("MACRO;\n"
12889 "__attribute__((maybe_unused)) int foo() {\n"
12890 " //...\n"
12891 "}");
12892
12893 verifyFormat("MACRO;\n"
12894 "[[nodiscard]] int foo() {\n"
12895 " //...\n"
12896 "}");
12897
12898 verifyNoChange("MACRO\n\n"
12899 "__attribute__((maybe_unused)) int foo() {\n"
12900 " //...\n"
12901 "}");
12902
12903 verifyNoChange("MACRO\n\n"
12904 "[[nodiscard]] int foo() {\n"
12905 " //...\n"
12906 "}");
12907}
12908
12909TEST_F(FormatTest, AttributePenaltyBreaking) {
12910 FormatStyle Style = getLLVMStyle();
12911 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12912 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12913 Style);
12914 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12915 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12916 Style);
12917 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12918 "shared_ptr<ALongTypeName> &C d) {\n}",
12919 Style);
12920}
12921
12922TEST_F(FormatTest, UnderstandsEllipsis) {
12923 FormatStyle Style = getLLVMStyle();
12924 verifyFormat("int printf(const char *fmt, ...);");
12925 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12926 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12927
12928 verifyFormat("template <int *...PP> a;", Style);
12929
12930 Style.PointerAlignment = FormatStyle::PAS_Left;
12931 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style);
12932
12933 verifyFormat("template <int*... PP> a;", Style);
12934
12935 Style.PointerAlignment = FormatStyle::PAS_Middle;
12936 verifyFormat("template <int *... PP> a;", Style);
12937}
12938
12939TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
12940 auto Style = getGoogleStyle();
12941 EXPECT_FALSE(Style.DerivePointerAlignment);
12942 Style.DerivePointerAlignment = true;
12943
12944 verifyFormat("int *a;\n"
12945 "int *a;\n"
12946 "int *a;",
12947 "int *a;\n"
12948 "int* a;\n"
12949 "int *a;",
12950 Style);
12951 verifyFormat("int* a;\n"
12952 "int* a;\n"
12953 "int* a;",
12954 "int* a;\n"
12955 "int* a;\n"
12956 "int *a;",
12957 Style);
12958 verifyFormat("int *a;\n"
12959 "int *a;\n"
12960 "int *a;",
12961 "int *a;\n"
12962 "int * a;\n"
12963 "int * a;",
12964 Style);
12965 verifyFormat("auto x = [] {\n"
12966 " int *a;\n"
12967 " int *a;\n"
12968 " int *a;\n"
12969 "};",
12970 "auto x=[]{int *a;\n"
12971 "int * a;\n"
12972 "int * a;};",
12973 Style);
12974}
12975
12976TEST_F(FormatTest, UnderstandsRvalueReferences) {
12977 verifyFormat("int f(int &&a) {}");
12978 verifyFormat("int f(int a, char &&b) {}");
12979 verifyFormat("void f() { int &&a = b; }");
12980 verifyGoogleFormat("int f(int a, char&& b) {}");
12981 verifyGoogleFormat("void f() { int&& a = b; }");
12982
12983 verifyIndependentOfContext("A<int &&> a;");
12984 verifyIndependentOfContext("A<int &&, int &&> a;");
12985 verifyGoogleFormat("A<int&&> a;");
12986 verifyGoogleFormat("A<int&&, int&&> a;");
12987
12988 // Not rvalue references:
12989 verifyFormat("template <bool B, bool C> class A {\n"
12990 " static_assert(B && C, \"Something is wrong\");\n"
12991 "};");
12992 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12993 verifyFormat("template <typename T> struct S {\n"
12994 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12995 "};");
12996 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12997 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12998 verifyFormat("#define A(a, b) (a && b)");
12999}
13000
13001TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
13002 verifyFormat("void f() {\n"
13003 " x[aaaaaaaaa -\n"
13004 " b] = 23;\n"
13005 "}",
13006 getLLVMStyleWithColumns(15));
13007}
13008
13009TEST_F(FormatTest, FormatsCasts) {
13010 verifyFormat("Type *A = static_cast<Type *>(P);");
13011 verifyFormat("static_cast<Type *>(P);");
13012 verifyFormat("static_cast<Type &>(Fun)(Args);");
13013 verifyFormat("static_cast<Type &>(*Fun)(Args);");
13014 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
13015 // Check that static_cast<...>(...) does not require the next token to be on
13016 // the same line.
13017 verifyFormat("some_loooong_output << something_something__ << "
13018 "static_cast<const void *>(R)\n"
13019 " << something;");
13020 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
13021 verifyFormat("const_cast<Type &>(*Fun)(Args);");
13022 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
13023 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
13024 verifyFormat("Type *A = (Type *)P;");
13025 verifyFormat("Type *A = (vector<Type *, int *>)P;");
13026 verifyFormat("int a = (int)(2.0f);");
13027 verifyFormat("int a = (int)2.0f;");
13028 verifyFormat("x[(int32)y];");
13029 verifyFormat("x = (int32)y;");
13030 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
13031 verifyFormat("int a = (int)*b;");
13032 verifyFormat("int a = (int)2.0f;");
13033 verifyFormat("int a = (int)~0;");
13034 verifyFormat("int a = (int)++a;");
13035 verifyFormat("int a = (int)sizeof(int);");
13036 verifyFormat("int a = (int)+2;");
13037 verifyFormat("my_int a = (my_int)2.0f;");
13038 verifyFormat("my_int a = (my_int)sizeof(int);");
13039 verifyFormat("return (my_int)aaa;");
13040 verifyFormat("throw (my_int)aaa;");
13041 verifyFormat("#define x ((int)-1)");
13042 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
13043 verifyFormat("#define p(q) ((int *)&q)");
13044 verifyFormat("fn(a)(b) + 1;");
13045
13046 verifyFormat("void f() { my_int a = (my_int)*b; }");
13047 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
13048 verifyFormat("my_int a = (my_int)~0;");
13049 verifyFormat("my_int a = (my_int)++a;");
13050 verifyFormat("my_int a = (my_int)-2;");
13051 verifyFormat("my_int a = (my_int)1;");
13052 verifyFormat("my_int a = (my_int *)1;");
13053 verifyFormat("my_int a = (const my_int)-1;");
13054 verifyFormat("my_int a = (const my_int *)-1;");
13055 verifyFormat("my_int a = (my_int)(my_int)-1;");
13056 verifyFormat("my_int a = (ns::my_int)-2;");
13057 verifyFormat("case (my_int)ONE:");
13058 verifyFormat("auto x = (X)this;");
13059 // Casts in Obj-C style calls used to not be recognized as such.
13060 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
13061
13062 // FIXME: single value wrapped with paren will be treated as cast.
13063 verifyFormat("void f(int i = (kValue)*kMask) {}");
13064
13065 verifyFormat("{\n"
13066 " (void)F;\n"
13067 "}");
13068
13069 // Don't break after a cast's
13070 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
13071 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
13072 " bbbbbbbbbbbbbbbbbbbbbb);");
13073
13074 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
13075 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
13076 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
13077 verifyFormat("bool *y = (bool *)(void *)(x);");
13078 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
13079 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
13080 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
13081 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
13082
13083 // These are not casts.
13084 verifyFormat("void f(int *) {}");
13085 verifyFormat("f(foo)->b;");
13086 verifyFormat("f(foo).b;");
13087 verifyFormat("f(foo)(b);");
13088 verifyFormat("f(foo)[b];");
13089 verifyFormat("[](foo) { return 4; }(bar);");
13090 verifyFormat("(*funptr)(foo)[4];");
13091 verifyFormat("funptrs[4](foo)[4];");
13092 verifyFormat("void f(int *);");
13093 verifyFormat("void f(int *) = 0;");
13094 verifyFormat("void f(SmallVector<int>) {}");
13095 verifyFormat("void f(SmallVector<int>);");
13096 verifyFormat("void f(SmallVector<int>) = 0;");
13097 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
13098 verifyFormat("int a = sizeof(int) * b;");
13099 verifyGoogleFormat("int a = alignof(int) * b;");
13100 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
13101 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
13102 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
13103
13104 // These are not casts, but at some point were confused with casts.
13105 verifyFormat("virtual void foo(int *) override;");
13106 verifyFormat("virtual void foo(char &) const;");
13107 verifyFormat("virtual void foo(int *a, char *) const;");
13108 verifyFormat("int a = sizeof(int *) + b;");
13109 verifyGoogleFormat("int a = alignof(int*) + b;");
13110 verifyFormat("bool b = f(g<int>) && c;");
13111 verifyFormat("typedef void (*f)(int i) func;");
13112 verifyFormat("void operator++(int) noexcept;");
13113 verifyFormat("void operator++(int &) noexcept;");
13114 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
13115 "&) noexcept;");
13116 verifyFormat(
13117 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
13118 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
13119 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
13120 verifyFormat("void operator delete(nothrow_t &) noexcept;");
13121 verifyFormat("void operator delete(foo &) noexcept;");
13122 verifyFormat("void operator delete(foo) noexcept;");
13123 verifyFormat("void operator delete(int) noexcept;");
13124 verifyFormat("void operator delete(int &) noexcept;");
13125 verifyFormat("void operator delete(int &) volatile noexcept;");
13126 verifyFormat("void operator delete(int &) const");
13127 verifyFormat("void operator delete(int &) = default");
13128 verifyFormat("void operator delete(int &) = delete");
13129 verifyFormat("void operator delete(int &) [[noreturn]]");
13130 verifyFormat("void operator delete(int &) throw();");
13131 verifyFormat("void operator delete(int &) throw(int);");
13132 verifyFormat("auto operator delete(int &) -> int;");
13133 verifyFormat("auto operator delete(int &) override");
13134 verifyFormat("auto operator delete(int &) final");
13135
13136 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
13137 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
13138 // FIXME: The indentation here is not ideal.
13139 verifyFormat(
13140 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13141 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
13142 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
13143}
13144
13145TEST_F(FormatTest, FormatsFunctionTypes) {
13146 verifyFormat("A<bool()> a;");
13147 verifyFormat("A<SomeType()> a;");
13148 verifyFormat("A<void (*)(int, std::string)> a;");
13149 verifyFormat("A<void *(int)>;");
13150 verifyFormat("void *(*a)(int *, SomeType *);");
13151 verifyFormat("int (*func)(void *);");
13152 verifyFormat("void f() { int (*func)(void *); }");
13153 verifyFormat("template <class CallbackClass>\n"
13154 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
13155
13156 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
13157 verifyGoogleFormat("void* (*a)(int);");
13158 verifyGoogleFormat(
13159 "template <class CallbackClass>\n"
13160 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
13161
13162 // Other constructs can look somewhat like function types:
13163 verifyFormat("A<sizeof(*x)> a;");
13164 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
13165 verifyFormat("some_var = function(*some_pointer_var)[0];");
13166 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
13167 verifyFormat("int x = f(&h)();");
13168 verifyFormat("returnsFunction(&param1, &param2)(param);");
13169 verifyFormat("std::function<\n"
13170 " LooooooooooongTemplatedType<\n"
13171 " SomeType>*(\n"
13172 " LooooooooooooooooongType type)>\n"
13173 " function;",
13174 getGoogleStyleWithColumns(40));
13175}
13176
13177TEST_F(FormatTest, FormatsPointersToArrayTypes) {
13178 verifyFormat("A (*foo_)[6];");
13179 verifyFormat("vector<int> (*foo_)[6];");
13180}
13181
13182TEST_F(FormatTest, BreaksLongVariableDeclarations) {
13183 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
13184 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
13185 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
13186 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
13187 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
13188 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
13189
13190 // Different ways of ()-initializiation.
13191 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
13192 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
13193 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
13194 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
13195 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
13196 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
13197 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
13198 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
13199
13200 // Lambdas should not confuse the variable declaration heuristic.
13201 verifyFormat("LooooooooooooooooongType\n"
13202 " variable(nullptr, [](A *a) {});",
13203 getLLVMStyleWithColumns(40));
13204}
13205
13206TEST_F(FormatTest, BreaksLongDeclarations) {
13207 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
13208 " AnotherNameForTheLongType;");
13209 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
13210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
13211 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
13212 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
13213 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
13214 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
13215 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
13216 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13217 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
13218 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13219 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
13220 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13221 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
13222 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13223 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
13224 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13225 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
13226 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13227 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
13228 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
13229 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
13230 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
13231 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
13232 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
13233 FormatStyle Indented = getLLVMStyle();
13234 Indented.IndentWrappedFunctionNames = true;
13235 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
13236 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
13237 Indented);
13238 verifyFormat(
13239 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
13240 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
13241 Indented);
13242 verifyFormat(
13243 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
13244 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
13245 Indented);
13246 verifyFormat(
13247 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
13248 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
13249 Indented);
13250
13251 // FIXME: Without the comment, this breaks after "(".
13252 verifyGoogleFormat(
13253 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
13254 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
13255
13256 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
13257 " int LoooooooooooooooooooongParam2) {}");
13258 verifyFormat(
13259 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
13260 " SourceLocation L, IdentifierIn *II,\n"
13261 " Type *T) {}");
13262 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
13263 "ReallyReaaallyLongFunctionName(\n"
13264 " const std::string &SomeParameter,\n"
13265 " const SomeType<string, SomeOtherTemplateParameter>\n"
13266 " &ReallyReallyLongParameterName,\n"
13267 " const SomeType<string, SomeOtherTemplateParameter>\n"
13268 " &AnotherLongParameterName) {}");
13269 verifyFormat("template <typename A>\n"
13270 "SomeLoooooooooooooooooooooongType<\n"
13271 " typename some_namespace::SomeOtherType<A>::Type>\n"
13272 "Function() {}");
13273
13274 verifyGoogleFormat(
13275 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
13276 " aaaaaaaaaaaaaaaaaaaaaaa;");
13277 verifyGoogleFormat(
13278 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
13279 " SourceLocation L) {}");
13280 verifyGoogleFormat(
13281 "some_namespace::LongReturnType\n"
13282 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
13283 " int first_long_parameter, int second_parameter) {}");
13284
13285 verifyGoogleFormat("template <typename T>\n"
13286 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
13287 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
13288 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13289 " int aaaaaaaaaaaaaaaaaaaaaaa);");
13290
13291 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
13292 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13293 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13294 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
13295 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
13296 " aaaaaaaaaaaaaaaaaaaaaaaa);");
13297 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
13298 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
13299 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
13300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13301
13302 verifyFormat("template <typename T> // Templates on own line.\n"
13303 "static int // Some comment.\n"
13304 "MyFunction(int a);");
13305}
13306
13307TEST_F(FormatTest, FormatsAccessModifiers) {
13308 FormatStyle Style = getLLVMStyle();
13309 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
13310 FormatStyle::ELBAMS_LogicalBlock);
13311 verifyFormat("struct foo {\n"
13312 "private:\n"
13313 " void f() {}\n"
13314 "\n"
13315 "private:\n"
13316 " int i;\n"
13317 "\n"
13318 "protected:\n"
13319 " int j;\n"
13320 "};",
13321 Style);
13322 verifyFormat("struct foo {\n"
13323 "private:\n"
13324 " void f() {}\n"
13325 "\n"
13326 "private:\n"
13327 " int i;\n"
13328 "\n"
13329 "protected:\n"
13330 " int j;\n"
13331 "};",
13332 "struct foo {\n"
13333 "private:\n"
13334 " void f() {}\n"
13335 "private:\n"
13336 " int i;\n"
13337 "protected:\n"
13338 " int j;\n"
13339 "};",
13340 Style);
13341 verifyFormat("struct foo { /* comment */\n"
13342 "private:\n"
13343 " int i;\n"
13344 " // comment\n"
13345 "private:\n"
13346 " int j;\n"
13347 "};",
13348 Style);
13349 verifyFormat("struct foo {\n"
13350 "#ifdef FOO\n"
13351 "#endif\n"
13352 "private:\n"
13353 " int i;\n"
13354 "#ifdef FOO\n"
13355 "private:\n"
13356 "#endif\n"
13357 " int j;\n"
13358 "};",
13359 Style);
13360 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13361 verifyFormat("struct foo {\n"
13362 "private:\n"
13363 " void f() {}\n"
13364 "private:\n"
13365 " int i;\n"
13366 "protected:\n"
13367 " int j;\n"
13368 "};",
13369 Style);
13370 verifyFormat("struct foo {\n"
13371 "private:\n"
13372 " void f() {}\n"
13373 "private:\n"
13374 " int i;\n"
13375 "protected:\n"
13376 " int j;\n"
13377 "};",
13378 "struct foo {\n"
13379 "\n"
13380 "private:\n"
13381 " void f() {}\n"
13382 "\n"
13383 "private:\n"
13384 " int i;\n"
13385 "\n"
13386 "protected:\n"
13387 " int j;\n"
13388 "};",
13389 Style);
13390 verifyFormat("struct foo { /* comment */\n"
13391 "private:\n"
13392 " int i;\n"
13393 " // comment\n"
13394 "private:\n"
13395 " int j;\n"
13396 "};",
13397 "struct foo { /* comment */\n"
13398 "\n"
13399 "private:\n"
13400 " int i;\n"
13401 " // comment\n"
13402 "\n"
13403 "private:\n"
13404 " int j;\n"
13405 "};",
13406 Style);
13407 verifyFormat("struct foo {\n"
13408 "#ifdef FOO\n"
13409 "#endif\n"
13410 "private:\n"
13411 " int i;\n"
13412 "#ifdef FOO\n"
13413 "private:\n"
13414 "#endif\n"
13415 " int j;\n"
13416 "};",
13417 "struct foo {\n"
13418 "#ifdef FOO\n"
13419 "#endif\n"
13420 "\n"
13421 "private:\n"
13422 " int i;\n"
13423 "#ifdef FOO\n"
13424 "\n"
13425 "private:\n"
13426 "#endif\n"
13427 " int j;\n"
13428 "};",
13429 Style);
13430 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13431 verifyFormat("struct foo {\n"
13432 "private:\n"
13433 " void f() {}\n"
13434 "\n"
13435 "private:\n"
13436 " int i;\n"
13437 "\n"
13438 "protected:\n"
13439 " int j;\n"
13440 "};",
13441 Style);
13442 verifyFormat("struct foo {\n"
13443 "private:\n"
13444 " void f() {}\n"
13445 "\n"
13446 "private:\n"
13447 " int i;\n"
13448 "\n"
13449 "protected:\n"
13450 " int j;\n"
13451 "};",
13452 "struct foo {\n"
13453 "private:\n"
13454 " void f() {}\n"
13455 "private:\n"
13456 " int i;\n"
13457 "protected:\n"
13458 " int j;\n"
13459 "};",
13460 Style);
13461 verifyFormat("struct foo { /* comment */\n"
13462 "private:\n"
13463 " int i;\n"
13464 " // comment\n"
13465 "\n"
13466 "private:\n"
13467 " int j;\n"
13468 "};",
13469 Style);
13470 verifyFormat("struct foo {\n"
13471 "#ifdef FOO\n"
13472 "#endif\n"
13473 "\n"
13474 "private:\n"
13475 " int i;\n"
13476 "#ifdef FOO\n"
13477 "\n"
13478 "private:\n"
13479 "#endif\n"
13480 " int j;\n"
13481 "};",
13482 "struct foo {\n"
13483 "#ifdef FOO\n"
13484 "#endif\n"
13485 "private:\n"
13486 " int i;\n"
13487 "#ifdef FOO\n"
13488 "private:\n"
13489 "#endif\n"
13490 " int j;\n"
13491 "};",
13492 Style);
13493 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
13494 verifyNoChange("struct foo {\n"
13495 "\n"
13496 "private:\n"
13497 " void f() {}\n"
13498 "\n"
13499 "private:\n"
13500 " int i;\n"
13501 "\n"
13502 "protected:\n"
13503 " int j;\n"
13504 "};",
13505 Style);
13506 verifyFormat("struct foo {\n"
13507 "private:\n"
13508 " void f() {}\n"
13509 "private:\n"
13510 " int i;\n"
13511 "protected:\n"
13512 " int j;\n"
13513 "};",
13514 Style);
13515 verifyNoChange("struct foo { /* comment */\n"
13516 "\n"
13517 "private:\n"
13518 " int i;\n"
13519 " // comment\n"
13520 "\n"
13521 "private:\n"
13522 " int j;\n"
13523 "};",
13524 Style);
13525 verifyFormat("struct foo { /* comment */\n"
13526 "private:\n"
13527 " int i;\n"
13528 " // comment\n"
13529 "private:\n"
13530 " int j;\n"
13531 "};",
13532 Style);
13533 verifyNoChange("struct foo {\n"
13534 "#ifdef FOO\n"
13535 "#endif\n"
13536 "\n"
13537 "private:\n"
13538 " int i;\n"
13539 "#ifdef FOO\n"
13540 "\n"
13541 "private:\n"
13542 "#endif\n"
13543 " int j;\n"
13544 "};",
13545 Style);
13546 verifyFormat("struct foo {\n"
13547 "#ifdef FOO\n"
13548 "#endif\n"
13549 "private:\n"
13550 " int i;\n"
13551 "#ifdef FOO\n"
13552 "private:\n"
13553 "#endif\n"
13554 " int j;\n"
13555 "};",
13556 Style);
13557 Style.AttributeMacros.push_back(x: "FOO");
13558 Style.AttributeMacros.push_back(x: "BAR");
13559 verifyFormat("struct foo {\n"
13560 "FOO private:\n"
13561 " int i;\n"
13562 "BAR(x) protected:\n"
13563 " int j;\n"
13564 "};",
13565 Style);
13566
13567 FormatStyle NoEmptyLines = getLLVMStyle();
13568 NoEmptyLines.MaxEmptyLinesToKeep = 0;
13569 verifyFormat("struct foo {\n"
13570 "private:\n"
13571 " void f() {}\n"
13572 "\n"
13573 "private:\n"
13574 " int i;\n"
13575 "\n"
13576 "public:\n"
13577 "protected:\n"
13578 " int j;\n"
13579 "};",
13580 NoEmptyLines);
13581
13582 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13583 verifyFormat("struct foo {\n"
13584 "private:\n"
13585 " void f() {}\n"
13586 "private:\n"
13587 " int i;\n"
13588 "public:\n"
13589 "protected:\n"
13590 " int j;\n"
13591 "};",
13592 NoEmptyLines);
13593
13594 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13595 verifyFormat("struct foo {\n"
13596 "private:\n"
13597 " void f() {}\n"
13598 "\n"
13599 "private:\n"
13600 " int i;\n"
13601 "\n"
13602 "public:\n"
13603 "\n"
13604 "protected:\n"
13605 " int j;\n"
13606 "};",
13607 NoEmptyLines);
13608}
13609
13610TEST_F(FormatTest, FormatsAfterAccessModifiers) {
13611
13612 FormatStyle Style = getLLVMStyle();
13613 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never);
13614 verifyFormat("struct foo {\n"
13615 "private:\n"
13616 " void f() {}\n"
13617 "\n"
13618 "private:\n"
13619 " int i;\n"
13620 "\n"
13621 "protected:\n"
13622 " int j;\n"
13623 "};",
13624 Style);
13625
13626 // Check if lines are removed.
13627 verifyFormat("struct foo {\n"
13628 "private:\n"
13629 " void f() {}\n"
13630 "\n"
13631 "private:\n"
13632 " int i;\n"
13633 "\n"
13634 "protected:\n"
13635 " int j;\n"
13636 "};",
13637 "struct foo {\n"
13638 "private:\n"
13639 "\n"
13640 " void f() {}\n"
13641 "\n"
13642 "private:\n"
13643 "\n"
13644 " int i;\n"
13645 "\n"
13646 "protected:\n"
13647 "\n"
13648 " int j;\n"
13649 "};",
13650 Style);
13651
13652 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13653 verifyFormat("struct foo {\n"
13654 "private:\n"
13655 "\n"
13656 " void f() {}\n"
13657 "\n"
13658 "private:\n"
13659 "\n"
13660 " int i;\n"
13661 "\n"
13662 "protected:\n"
13663 "\n"
13664 " int j;\n"
13665 "};",
13666 Style);
13667
13668 // Check if lines are added.
13669 verifyFormat("struct foo {\n"
13670 "private:\n"
13671 "\n"
13672 " void f() {}\n"
13673 "\n"
13674 "private:\n"
13675 "\n"
13676 " int i;\n"
13677 "\n"
13678 "protected:\n"
13679 "\n"
13680 " int j;\n"
13681 "};",
13682 "struct foo {\n"
13683 "private:\n"
13684 " void f() {}\n"
13685 "\n"
13686 "private:\n"
13687 " int i;\n"
13688 "\n"
13689 "protected:\n"
13690 " int j;\n"
13691 "};",
13692 Style);
13693
13694 // Leave tests rely on the code layout, test::messUp can not be used.
13695 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
13696 Style.MaxEmptyLinesToKeep = 0u;
13697 verifyFormat("struct foo {\n"
13698 "private:\n"
13699 " void f() {}\n"
13700 "\n"
13701 "private:\n"
13702 " int i;\n"
13703 "\n"
13704 "protected:\n"
13705 " int j;\n"
13706 "};",
13707 Style);
13708
13709 // Check if MaxEmptyLinesToKeep is respected.
13710 verifyFormat("struct foo {\n"
13711 "private:\n"
13712 " void f() {}\n"
13713 "\n"
13714 "private:\n"
13715 " int i;\n"
13716 "\n"
13717 "protected:\n"
13718 " int j;\n"
13719 "};",
13720 "struct foo {\n"
13721 "private:\n"
13722 "\n\n\n"
13723 " void f() {}\n"
13724 "\n"
13725 "private:\n"
13726 "\n\n\n"
13727 " int i;\n"
13728 "\n"
13729 "protected:\n"
13730 "\n\n\n"
13731 " int j;\n"
13732 "};",
13733 Style);
13734
13735 Style.MaxEmptyLinesToKeep = 1u;
13736 verifyNoChange("struct foo {\n"
13737 "private:\n"
13738 "\n"
13739 " void f() {}\n"
13740 "\n"
13741 "private:\n"
13742 "\n"
13743 " int i;\n"
13744 "\n"
13745 "protected:\n"
13746 "\n"
13747 " int j;\n"
13748 "};",
13749 Style);
13750 // Check if no lines are kept.
13751 verifyFormat("struct foo {\n"
13752 "private:\n"
13753 " void f() {}\n"
13754 "\n"
13755 "private:\n"
13756 " int i;\n"
13757 "\n"
13758 "protected:\n"
13759 " int j;\n"
13760 "};",
13761 Style);
13762 // Check if MaxEmptyLinesToKeep is respected.
13763 verifyFormat("struct foo {\n"
13764 "private:\n"
13765 "\n"
13766 " void f() {}\n"
13767 "\n"
13768 "private:\n"
13769 "\n"
13770 " int i;\n"
13771 "\n"
13772 "protected:\n"
13773 "\n"
13774 " int j;\n"
13775 "};",
13776 "struct foo {\n"
13777 "private:\n"
13778 "\n\n\n"
13779 " void f() {}\n"
13780 "\n"
13781 "private:\n"
13782 "\n\n\n"
13783 " int i;\n"
13784 "\n"
13785 "protected:\n"
13786 "\n\n\n"
13787 " int j;\n"
13788 "};",
13789 Style);
13790
13791 Style.MaxEmptyLinesToKeep = 10u;
13792 verifyNoChange("struct foo {\n"
13793 "private:\n"
13794 "\n\n\n"
13795 " void f() {}\n"
13796 "\n"
13797 "private:\n"
13798 "\n\n\n"
13799 " int i;\n"
13800 "\n"
13801 "protected:\n"
13802 "\n\n\n"
13803 " int j;\n"
13804 "};",
13805 Style);
13806
13807 // Test with comments.
13808 Style = getLLVMStyle();
13809 verifyFormat("struct foo {\n"
13810 "private:\n"
13811 " // comment\n"
13812 " void f() {}\n"
13813 "\n"
13814 "private: /* comment */\n"
13815 " int i;\n"
13816 "};",
13817 Style);
13818 verifyFormat("struct foo {\n"
13819 "private:\n"
13820 " // comment\n"
13821 " void f() {}\n"
13822 "\n"
13823 "private: /* comment */\n"
13824 " int i;\n"
13825 "};",
13826 "struct foo {\n"
13827 "private:\n"
13828 "\n"
13829 " // comment\n"
13830 " void f() {}\n"
13831 "\n"
13832 "private: /* comment */\n"
13833 "\n"
13834 " int i;\n"
13835 "};",
13836 Style);
13837
13838 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13839 verifyFormat("struct foo {\n"
13840 "private:\n"
13841 "\n"
13842 " // comment\n"
13843 " void f() {}\n"
13844 "\n"
13845 "private: /* comment */\n"
13846 "\n"
13847 " int i;\n"
13848 "};",
13849 "struct foo {\n"
13850 "private:\n"
13851 " // comment\n"
13852 " void f() {}\n"
13853 "\n"
13854 "private: /* comment */\n"
13855 " int i;\n"
13856 "};",
13857 Style);
13858 verifyFormat("struct foo {\n"
13859 "private:\n"
13860 "\n"
13861 " // comment\n"
13862 " void f() {}\n"
13863 "\n"
13864 "private: /* comment */\n"
13865 "\n"
13866 " int i;\n"
13867 "};",
13868 Style);
13869
13870 // Test with preprocessor defines.
13871 Style = getLLVMStyle();
13872 verifyFormat("struct foo {\n"
13873 "private:\n"
13874 "#ifdef FOO\n"
13875 "#endif\n"
13876 " void f() {}\n"
13877 "};",
13878 Style);
13879 verifyFormat("struct foo {\n"
13880 "private:\n"
13881 "#ifdef FOO\n"
13882 "#endif\n"
13883 " void f() {}\n"
13884 "};",
13885 "struct foo {\n"
13886 "private:\n"
13887 "\n"
13888 "#ifdef FOO\n"
13889 "#endif\n"
13890 " void f() {}\n"
13891 "};",
13892 Style);
13893 verifyNoChange("struct foo {\n"
13894 "#ifdef FOO\n"
13895 "#else\n"
13896 "private:\n"
13897 "\n"
13898 "#endif\n"
13899 "};",
13900 Style);
13901 verifyFormat("struct foo {\n"
13902 "#ifdef FOO\n"
13903 "#else\n"
13904 "private:\n"
13905 "\n"
13906 "#endif\n"
13907 "};",
13908 "struct foo {\n"
13909 "#ifdef FOO\n"
13910 "#else\n"
13911 "private:\n"
13912 "\n"
13913 "\n"
13914 "#endif\n"
13915 "};",
13916 Style);
13917 verifyFormat("struct foo {\n"
13918 "#ifdef FOO\n"
13919 "private:\n"
13920 "#else\n"
13921 "#endif\n"
13922 "};",
13923 "struct foo {\n"
13924 "#ifdef FOO\n"
13925 "private:\n"
13926 "\n"
13927 "\n"
13928 "#else\n"
13929 "#endif\n"
13930 "};",
13931 Style);
13932 verifyFormat("struct foo {\n"
13933 "#if 0\n"
13934 "#else\n"
13935 "#endif\n"
13936 "#ifdef FOO\n"
13937 "private:\n"
13938 "#endif\n"
13939 "};",
13940 "struct foo {\n"
13941 "#if 0\n"
13942 "#else\n"
13943 "#endif\n"
13944 "#ifdef FOO\n"
13945 "private:\n"
13946 "\n"
13947 "\n"
13948 "#endif\n"
13949 "};",
13950 Style);
13951
13952 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13953 verifyFormat("struct foo {\n"
13954 "private:\n"
13955 "\n"
13956 "#ifdef FOO\n"
13957 "#endif\n"
13958 " void f() {}\n"
13959 "};",
13960 "struct foo {\n"
13961 "private:\n"
13962 "#ifdef FOO\n"
13963 "#endif\n"
13964 " void f() {}\n"
13965 "};",
13966 Style);
13967 verifyFormat("struct foo {\n"
13968 "private:\n"
13969 "\n"
13970 "#ifdef FOO\n"
13971 "#endif\n"
13972 " void f() {}\n"
13973 "};",
13974 Style);
13975}
13976
13977TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) {
13978 // Combined tests of EmptyLineAfterAccessModifier and
13979 // EmptyLineBeforeAccessModifier.
13980 FormatStyle Style = getLLVMStyle();
13981 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
13982 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
13983 verifyFormat("struct foo {\n"
13984 "private:\n"
13985 "\n"
13986 "protected:\n"
13987 "};",
13988 Style);
13989
13990 Style.MaxEmptyLinesToKeep = 10u;
13991 // Both remove all new lines.
13992 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
13993 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
13994 verifyFormat("struct foo {\n"
13995 "private:\n"
13996 "protected:\n"
13997 "};",
13998 "struct foo {\n"
13999 "private:\n"
14000 "\n\n\n"
14001 "protected:\n"
14002 "};",
14003 Style);
14004
14005 // Leave tests rely on the code layout, test::messUp can not be used.
14006 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
14007 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
14008 Style.MaxEmptyLinesToKeep = 10u;
14009 verifyNoChange("struct foo {\n"
14010 "private:\n"
14011 "\n\n\n"
14012 "protected:\n"
14013 "};",
14014 Style);
14015 Style.MaxEmptyLinesToKeep = 3u;
14016 verifyNoChange("struct foo {\n"
14017 "private:\n"
14018 "\n\n\n"
14019 "protected:\n"
14020 "};",
14021 Style);
14022 Style.MaxEmptyLinesToKeep = 1u;
14023 verifyNoChange("struct foo {\n"
14024 "private:\n"
14025 "\n\n\n"
14026 "protected:\n"
14027 "};",
14028 Style); // Based on new lines in original document and not
14029 // on the setting.
14030
14031 Style.MaxEmptyLinesToKeep = 10u;
14032 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
14033 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
14034 // Newlines are kept if they are greater than zero,
14035 // test::messUp removes all new lines which changes the logic
14036 verifyNoChange("struct foo {\n"
14037 "private:\n"
14038 "\n\n\n"
14039 "protected:\n"
14040 "};",
14041 Style);
14042
14043 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
14044 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
14045 // test::messUp removes all new lines which changes the logic
14046 verifyNoChange("struct foo {\n"
14047 "private:\n"
14048 "\n\n\n"
14049 "protected:\n"
14050 "};",
14051 Style);
14052
14053 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave;
14054 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
14055 verifyNoChange("struct foo {\n"
14056 "private:\n"
14057 "\n\n\n"
14058 "protected:\n"
14059 "};",
14060 Style); // test::messUp removes all new lines which changes
14061 // the logic.
14062
14063 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
14064 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
14065 verifyFormat("struct foo {\n"
14066 "private:\n"
14067 "protected:\n"
14068 "};",
14069 "struct foo {\n"
14070 "private:\n"
14071 "\n\n\n"
14072 "protected:\n"
14073 "};",
14074 Style);
14075
14076 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
14077 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
14078 verifyNoChange("struct foo {\n"
14079 "private:\n"
14080 "\n\n\n"
14081 "protected:\n"
14082 "};",
14083 Style); // test::messUp removes all new lines which changes
14084 // the logic.
14085
14086 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
14087 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
14088 verifyFormat("struct foo {\n"
14089 "private:\n"
14090 "protected:\n"
14091 "};",
14092 "struct foo {\n"
14093 "private:\n"
14094 "\n\n\n"
14095 "protected:\n"
14096 "};",
14097 Style);
14098
14099 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
14100 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always;
14101 verifyFormat("struct foo {\n"
14102 "private:\n"
14103 "protected:\n"
14104 "};",
14105 "struct foo {\n"
14106 "private:\n"
14107 "\n\n\n"
14108 "protected:\n"
14109 "};",
14110 Style);
14111
14112 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
14113 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave;
14114 verifyFormat("struct foo {\n"
14115 "private:\n"
14116 "protected:\n"
14117 "};",
14118 "struct foo {\n"
14119 "private:\n"
14120 "\n\n\n"
14121 "protected:\n"
14122 "};",
14123 Style);
14124
14125 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
14126 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
14127 verifyFormat("struct foo {\n"
14128 "private:\n"
14129 "protected:\n"
14130 "};",
14131 "struct foo {\n"
14132 "private:\n"
14133 "\n\n\n"
14134 "protected:\n"
14135 "};",
14136 Style);
14137}
14138
14139TEST_F(FormatTest, FormatsArrays) {
14140 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
14141 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
14142 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
14143 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
14144 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
14145 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
14146 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14147 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
14148 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14149 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
14150 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
14151 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
14152 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
14153 verifyFormat(
14154 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
14155 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
14156 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
14157 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
14158 " .aaaaaaaaaaaaaaaaaaaaaa();");
14159
14160 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
14161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
14162 verifyFormat(
14163 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
14164 " .aaaaaaa[0]\n"
14165 " .aaaaaaaaaaaaaaaaaaaaaa();");
14166 verifyFormat("a[::b::c];");
14167
14168 verifyFormat("{\n"
14169 " (*a)[0] = 1;\n"
14170 "}");
14171
14172 verifyNoCrash(Code: "a[,Y?)]", Style: getLLVMStyleWithColumns(ColumnLimit: 10));
14173
14174 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(ColumnLimit: 0);
14175 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
14176}
14177
14178TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
14179 verifyFormat("(a)->b();");
14180 verifyFormat("--a;");
14181}
14182
14183TEST_F(FormatTest, HandlesIncludeDirectives) {
14184 verifyFormat("#include <string>\n"
14185 "#include <a/b/c.h>\n"
14186 "#include \"a/b/string\"\n"
14187 "#include \"string.h\"\n"
14188 "#include \"string.h\"\n"
14189 "#include <a-a>\n"
14190 "#include < path with space >\n"
14191 "#include_next <test.h>"
14192 "#include \"abc.h\" // this is included for ABC\n"
14193 "#include \"some long include\" // with a comment\n"
14194 "#include \"some very long include path\"\n"
14195 "#include <some/very/long/include/path>",
14196 getLLVMStyleWithColumns(35));
14197 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
14198 verifyFormat("#include <a>", "#include<a>");
14199
14200 verifyFormat("#import <string>");
14201 verifyFormat("#import <a/b/c.h>");
14202 verifyFormat("#import \"a/b/string\"");
14203 verifyFormat("#import \"string.h\"");
14204 verifyFormat("#import \"string.h\"");
14205 verifyFormat("#if __has_include(<strstream>)\n"
14206 "#include <strstream>\n"
14207 "#endif");
14208
14209 verifyFormat("#define MY_IMPORT <a/b>");
14210
14211 verifyFormat("#if __has_include(<a/b>)");
14212 verifyFormat("#if __has_include_next(<a/b>)");
14213 verifyFormat("#define F __has_include(<a/b>)");
14214 verifyFormat("#define F __has_include_next(<a/b>)");
14215
14216 // Protocol buffer definition or missing "#".
14217 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
14218 getLLVMStyleWithColumns(30));
14219
14220 FormatStyle Style = getLLVMStyle();
14221 Style.AlwaysBreakBeforeMultilineStrings = true;
14222 Style.ColumnLimit = 0;
14223 verifyFormat("#import \"abc.h\"", Style);
14224
14225 // But 'import' might also be a regular C++ namespace.
14226 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
14228 verifyFormat("import::Bar foo(val ? 2 : 1);");
14229}
14230
14231//===----------------------------------------------------------------------===//
14232// Error recovery tests.
14233//===----------------------------------------------------------------------===//
14234
14235TEST_F(FormatTest, IncompleteParameterLists) {
14236 FormatStyle NoBinPacking = getLLVMStyle();
14237 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
14238 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
14239 " double *min_x,\n"
14240 " double *max_x,\n"
14241 " double *min_y,\n"
14242 " double *max_y,\n"
14243 " double *min_z,\n"
14244 " double *max_z, ) {}",
14245 NoBinPacking);
14246}
14247
14248TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
14249 verifyFormat("void f() { return; }\n42");
14250 verifyFormat("void f() {\n"
14251 " if (0)\n"
14252 " return;\n"
14253 "}\n"
14254 "42");
14255 verifyFormat("void f() { return }\n42");
14256 verifyFormat("void f() {\n"
14257 " if (0)\n"
14258 " return\n"
14259 "}\n"
14260 "42");
14261}
14262
14263TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
14264 verifyFormat("void f() { return }", "void f ( ) { return }");
14265 verifyFormat("void f() {\n"
14266 " if (a)\n"
14267 " return\n"
14268 "}",
14269 "void f ( ) { if ( a ) return }");
14270 verifyFormat("namespace N {\n"
14271 "void f()\n"
14272 "}",
14273 "namespace N { void f() }");
14274 verifyFormat("namespace N {\n"
14275 "void f() {}\n"
14276 "void g()\n"
14277 "} // namespace N",
14278 "namespace N { void f( ) { } void g( ) }");
14279}
14280
14281TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
14282 verifyFormat("int aaaaaaaa =\n"
14283 " // Overlylongcomment\n"
14284 " b;",
14285 getLLVMStyleWithColumns(20));
14286 verifyFormat("function(\n"
14287 " ShortArgument,\n"
14288 " LoooooooooooongArgument);",
14289 getLLVMStyleWithColumns(20));
14290}
14291
14292TEST_F(FormatTest, IncorrectAccessSpecifier) {
14293 verifyFormat("public:");
14294 verifyFormat("class A {\n"
14295 "public\n"
14296 " void f() {}\n"
14297 "};");
14298 verifyFormat("public\n"
14299 "int qwerty;");
14300 verifyFormat("public\n"
14301 "B {}");
14302 verifyFormat("public\n"
14303 "{\n"
14304 "}");
14305 verifyFormat("public\n"
14306 "B { int x; }");
14307}
14308
14309TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
14310 verifyFormat("{");
14311 verifyFormat("#})");
14312 verifyNoCrash(Code: "(/**/[:!] ?[).");
14313 verifyNoCrash(Code: "struct X {\n"
14314 " operator iunt(\n"
14315 "};");
14316 verifyNoCrash(Code: "struct Foo {\n"
14317 " operator foo(bar\n"
14318 "};");
14319 verifyNoCrash(Code: "decltype( {\n"
14320 " {");
14321}
14322
14323TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {
14324 // Found by oss-fuzz:
14325 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
14326 FormatStyle Style = getGoogleStyle(Language: FormatStyle::LK_Cpp);
14327 Style.ColumnLimit = 60;
14328 verifyNoCrash(
14329 Code: "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
14330 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
14331 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
14332 Style);
14333}
14334
14335TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
14336 verifyFormat("do {\n}");
14337 verifyFormat("do {\n}\n"
14338 "f();");
14339 verifyFormat("do {\n}\n"
14340 "wheeee(fun);");
14341 verifyFormat("do {\n"
14342 " f();\n"
14343 "}");
14344}
14345
14346TEST_F(FormatTest, IncorrectCodeMissingParens) {
14347 verifyFormat("if {\n foo;\n foo();\n}");
14348 verifyFormat("switch {\n foo;\n foo();\n}");
14349 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
14350 verifyIncompleteFormat("ERROR: for target;");
14351 verifyFormat("while {\n foo;\n foo();\n}");
14352 verifyFormat("do {\n foo;\n foo();\n} while;");
14353}
14354
14355TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
14356 verifyIncompleteFormat("namespace {\n"
14357 "class Foo { Foo (\n"
14358 "};\n"
14359 "} // namespace");
14360}
14361
14362TEST_F(FormatTest, IncorrectCodeErrorDetection) {
14363 verifyFormat("{\n"
14364 " {\n"
14365 " }",
14366 "{\n"
14367 "{\n"
14368 "}");
14369 verifyFormat("{\n"
14370 " {\n"
14371 " }",
14372 "{\n"
14373 " {\n"
14374 "}");
14375 verifyFormat("{\n"
14376 " {\n"
14377 " }");
14378 verifyFormat("{\n"
14379 " {\n"
14380 " }\n"
14381 "}\n"
14382 "}",
14383 "{\n"
14384 " {\n"
14385 " }\n"
14386 " }\n"
14387 "}");
14388
14389 verifyFormat("{\n"
14390 " {\n"
14391 " breakme(\n"
14392 " qwe);\n"
14393 " }",
14394 "{\n"
14395 " {\n"
14396 " breakme(qwe);\n"
14397 "}",
14398 getLLVMStyleWithColumns(10));
14399}
14400
14401TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
14402 verifyFormat("int x = {\n"
14403 " avariable,\n"
14404 " b(alongervariable)};",
14405 getLLVMStyleWithColumns(25));
14406}
14407
14408TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
14409 verifyFormat("return (a)(b){1, 2, 3};");
14410}
14411
14412TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
14413 verifyFormat("vector<int> x{1, 2, 3, 4};");
14414 verifyFormat("vector<int> x{\n"
14415 " 1,\n"
14416 " 2,\n"
14417 " 3,\n"
14418 " 4,\n"
14419 "};");
14420 verifyFormat("vector<T> x{{}, {}, {}, {}};");
14421 verifyFormat("f({1, 2});");
14422 verifyFormat("auto v = Foo{-1};");
14423 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
14424 verifyFormat("Class::Class : member{1, 2, 3} {}");
14425 verifyFormat("new vector<int>{1, 2, 3};");
14426 verifyFormat("new int[3]{1, 2, 3};");
14427 verifyFormat("new int{1};");
14428 verifyFormat("return {arg1, arg2};");
14429 verifyFormat("return {arg1, SomeType{parameter}};");
14430 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
14431 verifyFormat("new T{arg1, arg2};");
14432 verifyFormat("f(MyMap[{composite, key}]);");
14433 verifyFormat("class Class {\n"
14434 " T member = {arg1, arg2};\n"
14435 "};");
14436 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
14437 verifyFormat("const struct A a = {.a = 1, .b = 2};");
14438 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
14439 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
14440 verifyFormat("int a = std::is_integral<int>{} + 0;");
14441
14442 verifyFormat("int foo(int i) { return fo1{}(i); }");
14443 verifyFormat("int foo(int i) { return fo1{}(i); }");
14444 verifyFormat("auto i = decltype(x){};");
14445 verifyFormat("auto i = typeof(x){};");
14446 verifyFormat("auto i = _Atomic(x){};");
14447 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
14448 verifyFormat("Node n{1, Node{1000}, //\n"
14449 " 2};");
14450 verifyFormat("Aaaa aaaaaaa{\n"
14451 " {\n"
14452 " aaaa,\n"
14453 " },\n"
14454 "};");
14455 verifyFormat("class C : public D {\n"
14456 " SomeClass SC{2};\n"
14457 "};");
14458 verifyFormat("class C : public A {\n"
14459 " class D : public B {\n"
14460 " void f() { int i{2}; }\n"
14461 " };\n"
14462 "};");
14463 verifyFormat("#define A {a, a},");
14464 // Don't confuse braced list initializers with compound statements.
14465 verifyFormat(
14466 "class A {\n"
14467 " A() : a{} {}\n"
14468 " A() : Base<int>{} {}\n"
14469 " A() : Base<Foo<int>>{} {}\n"
14470 " A(int b) : b(b) {}\n"
14471 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
14472 " int a, b;\n"
14473 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
14474 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
14475 "{}\n"
14476 "};");
14477
14478 // Avoid breaking between equal sign and opening brace
14479 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
14480 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
14481 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
14482 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
14483 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
14484 " {\"ccccccccccccccccccccc\", 2}};",
14485 AvoidBreakingFirstArgument);
14486
14487 // Binpacking only if there is no trailing comma
14488 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
14489 " cccccccccc, dddddddddd};",
14490 getLLVMStyleWithColumns(50));
14491 verifyFormat("const Aaaaaa aaaaa = {\n"
14492 " aaaaaaaaaaa,\n"
14493 " bbbbbbbbbbb,\n"
14494 " ccccccccccc,\n"
14495 " ddddddddddd,\n"
14496 "};",
14497 getLLVMStyleWithColumns(50));
14498
14499 // Cases where distinguising braced lists and blocks is hard.
14500 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
14501 verifyFormat("void f() {\n"
14502 " return; // comment\n"
14503 "}\n"
14504 "SomeType t;");
14505 verifyFormat("void f() {\n"
14506 " if (a) {\n"
14507 " f();\n"
14508 " }\n"
14509 "}\n"
14510 "SomeType t;");
14511
14512 // In combination with BinPackArguments = false.
14513 FormatStyle NoBinPacking = getLLVMStyle();
14514 NoBinPacking.BinPackArguments = false;
14515 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14516 " bbbbb,\n"
14517 " ccccc,\n"
14518 " ddddd,\n"
14519 " eeeee,\n"
14520 " ffffff,\n"
14521 " ggggg,\n"
14522 " hhhhhh,\n"
14523 " iiiiii,\n"
14524 " jjjjjj,\n"
14525 " kkkkkk};",
14526 NoBinPacking);
14527 verifyFormat("const Aaaaaa aaaaa = {\n"
14528 " aaaaa,\n"
14529 " bbbbb,\n"
14530 " ccccc,\n"
14531 " ddddd,\n"
14532 " eeeee,\n"
14533 " ffffff,\n"
14534 " ggggg,\n"
14535 " hhhhhh,\n"
14536 " iiiiii,\n"
14537 " jjjjjj,\n"
14538 " kkkkkk,\n"
14539 "};",
14540 NoBinPacking);
14541 verifyFormat(
14542 "const Aaaaaa aaaaa = {\n"
14543 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
14544 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
14545 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14546 "};",
14547 NoBinPacking);
14548
14549 NoBinPacking.BinPackLongBracedList = false;
14550 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14551 " bbbbb,\n"
14552 " ccccc,\n"
14553 " ddddd,\n"
14554 " eeeee,\n"
14555 " ffffff,\n"
14556 " ggggg,\n"
14557 " hhhhhh,\n"
14558 " iiiiii,\n"
14559 " jjjjjj,\n"
14560 " kkkkkk,\n"
14561 " aaaaa,\n"
14562 " bbbbb,\n"
14563 " ccccc,\n"
14564 " ddddd,\n"
14565 " eeeee,\n"
14566 " ffffff,\n"
14567 " ggggg,\n"
14568 " hhhhhh,\n"
14569 " iiiiii};",
14570 NoBinPacking);
14571 verifyFormat("const Aaaaaa aaaaa = {\n"
14572 " aaaaa,\n"
14573 " bbbbb,\n"
14574 " ccccc,\n"
14575 " ddddd,\n"
14576 " eeeee,\n"
14577 " ffffff,\n"
14578 " ggggg,\n"
14579 " hhhhhh,\n"
14580 " iiiiii,\n"
14581 " jjjjjj,\n"
14582 " kkkkkk,\n"
14583 " aaaaa,\n"
14584 " bbbbb,\n"
14585 " ccccc,\n"
14586 " ddddd,\n"
14587 " eeeee,\n"
14588 " ffffff,\n"
14589 " ggggg,\n"
14590 " hhhhhh,\n"
14591 "};",
14592 NoBinPacking);
14593
14594 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
14595 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14596 " CDDDP83848_BMCR_REGISTER,\n"
14597 " CDDDP83848_BMSR_REGISTER,\n"
14598 " CDDDP83848_RBR_REGISTER};",
14599 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14600 " CDDDP83848_BMSR_REGISTER,\n"
14601 " CDDDP83848_RBR_REGISTER};",
14602 NoBinPacking);
14603
14604 // FIXME: The alignment of these trailing comments might be bad. Then again,
14605 // this might be utterly useless in real code.
14606 verifyFormat("Constructor::Constructor()\n"
14607 " : some_value{ //\n"
14608 " aaaaaaa, //\n"
14609 " bbbbbbb} {}");
14610
14611 // In braced lists, the first comment is always assumed to belong to the
14612 // first element. Thus, it can be moved to the next or previous line as
14613 // appropriate.
14614 verifyFormat("function({// First element:\n"
14615 " 1,\n"
14616 " // Second element:\n"
14617 " 2});",
14618 "function({\n"
14619 " // First element:\n"
14620 " 1,\n"
14621 " // Second element:\n"
14622 " 2});");
14623 verifyFormat("std::vector<int> MyNumbers{\n"
14624 " // First element:\n"
14625 " 1,\n"
14626 " // Second element:\n"
14627 " 2};",
14628 "std::vector<int> MyNumbers{// First element:\n"
14629 " 1,\n"
14630 " // Second element:\n"
14631 " 2};",
14632 getLLVMStyleWithColumns(30));
14633 // A trailing comma should still lead to an enforced line break and no
14634 // binpacking.
14635 verifyFormat("vector<int> SomeVector = {\n"
14636 " // aaa\n"
14637 " 1,\n"
14638 " 2,\n"
14639 "};",
14640 "vector<int> SomeVector = { // aaa\n"
14641 " 1, 2, };");
14642
14643 // C++11 brace initializer list l-braces should not be treated any differently
14644 // when breaking before lambda bodies is enabled
14645 FormatStyle BreakBeforeLambdaBody = getLLVMStyle();
14646 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
14647 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
14648 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true;
14649 verifyFormat(
14650 "std::runtime_error{\n"
14651 " \"Long string which will force a break onto the next line...\"};",
14652 BreakBeforeLambdaBody);
14653
14654 FormatStyle ExtraSpaces = getLLVMStyle();
14655 ExtraSpaces.Cpp11BracedListStyle = false;
14656 ExtraSpaces.ColumnLimit = 75;
14657 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
14658 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
14659 verifyFormat("f({ 1, 2 });", ExtraSpaces);
14660 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
14661 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
14662 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
14663 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
14664 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
14665 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
14666 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
14667 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
14668 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
14669 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
14670 verifyFormat("class Class {\n"
14671 " T member = { arg1, arg2 };\n"
14672 "};",
14673 ExtraSpaces);
14674 verifyFormat(
14675 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14676 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14677 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14678 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14679 ExtraSpaces);
14680 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
14681 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14682 ExtraSpaces);
14683 verifyFormat(
14684 "someFunction(OtherParam,\n"
14685 " BracedList{ // comment 1 (Forcing interesting break)\n"
14686 " param1, param2,\n"
14687 " // comment 2\n"
14688 " param3, param4 });",
14689 ExtraSpaces);
14690 verifyFormat(
14691 "std::this_thread::sleep_for(\n"
14692 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14693 ExtraSpaces);
14694 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14695 " aaaaaaa,\n"
14696 " aaaaaaaaaa,\n"
14697 " aaaaa,\n"
14698 " aaaaaaaaaaaaaaa,\n"
14699 " aaa,\n"
14700 " aaaaaaaaaa,\n"
14701 " a,\n"
14702 " aaaaaaaaaaaaaaaaaaaaa,\n"
14703 " aaaaaaaaaaaa,\n"
14704 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14705 " aaaaaaa,\n"
14706 " a};");
14707 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
14708 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
14709 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
14710
14711 // Avoid breaking between initializer/equal sign and opening brace
14712 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200;
14713 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14714 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14715 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14716 " { \"ccccccccccccccccccccc\", 2 }\n"
14717 "};",
14718 ExtraSpaces);
14719 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14720 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14721 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14722 " { \"ccccccccccccccccccccc\", 2 }\n"
14723 "};",
14724 ExtraSpaces);
14725
14726 FormatStyle SpaceBeforeBrace = getLLVMStyle();
14727 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true;
14728 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace);
14729 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace);
14730
14731 FormatStyle SpaceBetweenBraces = getLLVMStyle();
14732 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
14733 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14734 SpaceBetweenBraces.SpacesInParensOptions.Other = true;
14735 SpaceBetweenBraces.SpacesInSquareBrackets = true;
14736 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
14737 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
14738 verifyFormat("vector< int > x{ // comment 1\n"
14739 " 1, 2, 3, 4 };",
14740 SpaceBetweenBraces);
14741 SpaceBetweenBraces.ColumnLimit = 20;
14742 verifyFormat("vector< int > x{\n"
14743 " 1, 2, 3, 4 };",
14744 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14745 SpaceBetweenBraces.ColumnLimit = 24;
14746 verifyFormat("vector< int > x{ 1, 2,\n"
14747 " 3, 4 };",
14748 "vector<int>x{1,2,3,4};", SpaceBetweenBraces);
14749 verifyFormat("vector< int > x{\n"
14750 " 1,\n"
14751 " 2,\n"
14752 " 3,\n"
14753 " 4,\n"
14754 "};",
14755 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces);
14756 verifyFormat("vector< int > x{};", SpaceBetweenBraces);
14757 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
14758 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
14759 verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
14760}
14761
14762TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
14763 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14764 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14765 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14766 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14767 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14768 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14769 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14770 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14771 " 1, 22, 333, 4444, 55555, //\n"
14772 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14773 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14774 verifyFormat(
14775 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14776 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14777 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14778 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14779 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14780 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14781 " 7777777};");
14782 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14783 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14784 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14785 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14786 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14787 " // Separating comment.\n"
14788 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14789 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14790 " // Leading comment\n"
14791 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14792 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14793 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14794 " 1, 1, 1, 1};",
14795 getLLVMStyleWithColumns(39));
14796 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14797 " 1, 1, 1, 1};",
14798 getLLVMStyleWithColumns(38));
14799 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14800 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14801 getLLVMStyleWithColumns(43));
14802 verifyFormat(
14803 "static unsigned SomeValues[10][3] = {\n"
14804 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14805 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14806 verifyFormat("static auto fields = new vector<string>{\n"
14807 " \"aaaaaaaaaaaaa\",\n"
14808 " \"aaaaaaaaaaaaa\",\n"
14809 " \"aaaaaaaaaaaa\",\n"
14810 " \"aaaaaaaaaaaaaa\",\n"
14811 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14812 " \"aaaaaaaaaaaa\",\n"
14813 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14814 "};");
14815 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14816 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14817 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14818 " 3, cccccccccccccccccccccc};",
14819 getLLVMStyleWithColumns(60));
14820
14821 // Trailing commas.
14822 verifyFormat("vector<int> x = {\n"
14823 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14824 "};",
14825 getLLVMStyleWithColumns(39));
14826 verifyFormat("vector<int> x = {\n"
14827 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14828 "};",
14829 getLLVMStyleWithColumns(39));
14830 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14831 " 1, 1, 1, 1,\n"
14832 " /**/ /**/};",
14833 getLLVMStyleWithColumns(39));
14834
14835 // Trailing comment in the first line.
14836 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14837 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14838 " 111111111, 222222222, 3333333333, 444444444, //\n"
14839 " 11111111, 22222222, 333333333, 44444444};");
14840 // Trailing comment in the last line.
14841 verifyFormat("int aaaaa[] = {\n"
14842 " 1, 2, 3, // comment\n"
14843 " 4, 5, 6 // comment\n"
14844 "};");
14845
14846 // With nested lists, we should either format one item per line or all nested
14847 // lists one on line.
14848 // FIXME: For some nested lists, we can do better.
14849 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14850 " {aaaaaaaaaaaaaaaaaaa},\n"
14851 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14852 " {aaaaaaaaaaaaaaaaa}};",
14853 getLLVMStyleWithColumns(60));
14854 verifyFormat(
14855 "SomeStruct my_struct_array = {\n"
14856 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14857 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14858 " {aaa, aaa},\n"
14859 " {aaa, aaa},\n"
14860 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14861 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14862 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14863
14864 // No column layout should be used here.
14865 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14866 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14867
14868 verifyNoCrash(Code: "a<,");
14869
14870 // No braced initializer here.
14871 verifyFormat("void f() {\n"
14872 " struct Dummy {};\n"
14873 " f(v);\n"
14874 "}");
14875 verifyFormat("void foo() {\n"
14876 " { // asdf\n"
14877 " {\n"
14878 " int a;\n"
14879 " }\n"
14880 " }\n"
14881 " {\n"
14882 " {\n"
14883 " int b;\n"
14884 " }\n"
14885 " }\n"
14886 "}");
14887 verifyFormat("namespace n {\n"
14888 "void foo() {\n"
14889 " {\n"
14890 " {\n"
14891 " statement();\n"
14892 " if (false) {\n"
14893 " }\n"
14894 " }\n"
14895 " }\n"
14896 " {\n"
14897 " }\n"
14898 "}\n"
14899 "} // namespace n");
14900
14901 // Long lists should be formatted in columns even if they are nested.
14902 verifyFormat(
14903 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14904 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14905 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14906 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14907 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14908 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14909
14910 // Allow "single-column" layout even if that violates the column limit. There
14911 // isn't going to be a better way.
14912 verifyFormat("std::vector<int> a = {\n"
14913 " aaaaaaaa,\n"
14914 " aaaaaaaa,\n"
14915 " aaaaaaaa,\n"
14916 " aaaaaaaa,\n"
14917 " aaaaaaaaaa,\n"
14918 " aaaaaaaa,\n"
14919 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14920 getLLVMStyleWithColumns(30));
14921 verifyFormat("vector<int> aaaa = {\n"
14922 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14923 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14924 " aaaaaa.aaaaaaa,\n"
14925 " aaaaaa.aaaaaaa,\n"
14926 " aaaaaa.aaaaaaa,\n"
14927 " aaaaaa.aaaaaaa,\n"
14928 "};");
14929
14930 // Don't create hanging lists.
14931 verifyFormat("someFunction(Param, {List1, List2,\n"
14932 " List3});",
14933 getLLVMStyleWithColumns(35));
14934 verifyFormat("someFunction(Param, Param,\n"
14935 " {List1, List2,\n"
14936 " List3});",
14937 getLLVMStyleWithColumns(35));
14938 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14939 " aaaaaaaaaaaaaaaaaaaaaaa);");
14940
14941 // No possible column formats, don't want the optimal paths penalized.
14942 verifyFormat(
14943 "waarudo::unit desk = {\n"
14944 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14945 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14946 "Output{1, 2}; },\n"
14947 " [](const Input &i) -> Output { return "
14948 "Output{1, 2}; });");
14949 FormatStyle NoBinPacking = getLLVMStyle();
14950 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine;
14951 verifyFormat("waarudo::unit desk = {\n"
14952 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14953 "1, 1} * w::m; }};",
14954 NoBinPacking);
14955}
14956
14957TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
14958 FormatStyle DoNotMerge = getLLVMStyle();
14959 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
14960
14961 verifyFormat("void f() { return 42; }");
14962 verifyFormat("void f() {\n"
14963 " return 42;\n"
14964 "}",
14965 DoNotMerge);
14966 verifyFormat("void f() {\n"
14967 " // Comment\n"
14968 "}");
14969 verifyFormat("{\n"
14970 "#error {\n"
14971 " int a;\n"
14972 "}");
14973 verifyFormat("{\n"
14974 " int a;\n"
14975 "#error {\n"
14976 "}");
14977 verifyFormat("void f() {} // comment");
14978 verifyFormat("void f() { int a; } // comment");
14979 verifyFormat("void f() {\n"
14980 "} // comment",
14981 DoNotMerge);
14982 verifyFormat("void f() {\n"
14983 " int a;\n"
14984 "} // comment",
14985 DoNotMerge);
14986 verifyFormat("void f() {\n"
14987 "} // comment",
14988 getLLVMStyleWithColumns(15));
14989
14990 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14991 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14992
14993 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14994 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14995 verifyGoogleFormat("class C {\n"
14996 " C()\n"
14997 " : iiiiiiii(nullptr),\n"
14998 " kkkkkkk(nullptr),\n"
14999 " mmmmmmm(nullptr),\n"
15000 " nnnnnnn(nullptr) {}\n"
15001 "};");
15002
15003 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(ColumnLimit: 0);
15004 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit);
15005 verifyFormat("class C {\n"
15006 " A() : b(0) {}\n"
15007 "};",
15008 "class C{A():b(0){}};", NoColumnLimit);
15009 verifyFormat("A()\n"
15010 " : b(0) {\n"
15011 "}",
15012 "A()\n:b(0)\n{\n}", NoColumnLimit);
15013
15014 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit;
15015 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom;
15016 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true;
15017 verifyFormat("class C {\n"
15018 "#pragma foo\n"
15019 " int foo { return 0; }\n"
15020 "};",
15021 NoColumnLimitWrapAfterFunction);
15022 verifyFormat("class C {\n"
15023 "#pragma foo\n"
15024 " void foo {}\n"
15025 "};",
15026 NoColumnLimitWrapAfterFunction);
15027
15028 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
15029 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
15030 FormatStyle::SFS_None;
15031 verifyFormat("A() : b(0) {\n"
15032 "}",
15033 DoNotMergeNoColumnLimit);
15034 verifyNoChange("A()\n"
15035 " : b(0) {\n"
15036 "}",
15037 DoNotMergeNoColumnLimit);
15038 verifyFormat("A()\n"
15039 " : b(0) {\n"
15040 "}",
15041 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit);
15042
15043 verifyFormat("#define A \\\n"
15044 " void f() { \\\n"
15045 " int i; \\\n"
15046 " }",
15047 getLLVMStyleWithColumns(20));
15048 verifyFormat("#define A \\\n"
15049 " void f() { int i; }",
15050 getLLVMStyleWithColumns(21));
15051 verifyFormat("#define A \\\n"
15052 " void f() { \\\n"
15053 " int i; \\\n"
15054 " } \\\n"
15055 " int j;",
15056 getLLVMStyleWithColumns(22));
15057 verifyFormat("#define A \\\n"
15058 " void f() { int i; } \\\n"
15059 " int j;",
15060 getLLVMStyleWithColumns(23));
15061
15062 verifyFormat(
15063 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
15064 " aaaaaaaaaaaaaaaaaa,\n"
15065 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
15066
15067 constexpr StringRef Code{"void foo() { /* Empty */ }"};
15068 verifyFormat(Code);
15069 verifyFormat(Code, "void foo() { /* Empty */\n"
15070 "}");
15071 verifyFormat(Code, "void foo() {\n"
15072 "/* Empty */\n"
15073 "}");
15074}
15075
15076TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
15077 FormatStyle MergeEmptyOnly = getLLVMStyle();
15078 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
15079 verifyFormat("class C {\n"
15080 " int f() {}\n"
15081 "};",
15082 MergeEmptyOnly);
15083 verifyFormat("class C {\n"
15084 " int f() {\n"
15085 " return 42;\n"
15086 " }\n"
15087 "};",
15088 MergeEmptyOnly);
15089 verifyFormat("int f() {}", MergeEmptyOnly);
15090 verifyFormat("int f() {\n"
15091 " return 42;\n"
15092 "}",
15093 MergeEmptyOnly);
15094
15095 // Also verify behavior when BraceWrapping.AfterFunction = true
15096 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
15097 MergeEmptyOnly.BraceWrapping.AfterFunction = true;
15098 verifyFormat("int f() {}", MergeEmptyOnly);
15099 verifyFormat("class C {\n"
15100 " int f() {}\n"
15101 "};",
15102 MergeEmptyOnly);
15103}
15104
15105TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
15106 FormatStyle MergeInlineOnly = getLLVMStyle();
15107 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
15108 verifyFormat("class C {\n"
15109 " int f() { return 42; }\n"
15110 "};",
15111 MergeInlineOnly);
15112 verifyFormat("int f() {\n"
15113 " return 42;\n"
15114 "}",
15115 MergeInlineOnly);
15116
15117 // SFS_Inline implies SFS_Empty
15118 verifyFormat("class C {\n"
15119 " int f() {}\n"
15120 "};",
15121 MergeInlineOnly);
15122 verifyFormat("int f() {}", MergeInlineOnly);
15123 // https://llvm.org/PR54147
15124 verifyFormat("auto lambda = []() {\n"
15125 " // comment\n"
15126 " f();\n"
15127 " g();\n"
15128 "};",
15129 MergeInlineOnly);
15130
15131 verifyFormat("class C {\n"
15132 "#ifdef A\n"
15133 " int f() { return 42; }\n"
15134 "#endif\n"
15135 "};",
15136 MergeInlineOnly);
15137
15138 verifyFormat("struct S {\n"
15139 "// comment\n"
15140 "#ifdef FOO\n"
15141 " int foo() { bar(); }\n"
15142 "#endif\n"
15143 "};",
15144 MergeInlineOnly);
15145
15146 MergeInlineOnly.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15147 verifyFormat("#define Foo \\\n"
15148 " struct S { \\\n"
15149 " void foo() { return; } \\\n"
15150 " }",
15151 MergeInlineOnly);
15152
15153 // Also verify behavior when BraceWrapping.AfterFunction = true
15154 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
15155 MergeInlineOnly.BraceWrapping.AfterFunction = true;
15156 verifyFormat("class C {\n"
15157 " int f() { return 42; }\n"
15158 "};",
15159 MergeInlineOnly);
15160 verifyFormat("int f()\n"
15161 "{\n"
15162 " return 42;\n"
15163 "}",
15164 MergeInlineOnly);
15165
15166 // SFS_Inline implies SFS_Empty
15167 verifyFormat("int f() {}", MergeInlineOnly);
15168 verifyFormat("class C {\n"
15169 " int f() {}\n"
15170 "};",
15171 MergeInlineOnly);
15172
15173 MergeInlineOnly.BraceWrapping.AfterClass = true;
15174 MergeInlineOnly.BraceWrapping.AfterStruct = true;
15175 verifyFormat("class C\n"
15176 "{\n"
15177 " int f() { return 42; }\n"
15178 "};",
15179 MergeInlineOnly);
15180 verifyFormat("struct C\n"
15181 "{\n"
15182 " int f() { return 42; }\n"
15183 "};",
15184 MergeInlineOnly);
15185 verifyFormat("int f()\n"
15186 "{\n"
15187 " return 42;\n"
15188 "}",
15189 MergeInlineOnly);
15190 verifyFormat("int f() {}", MergeInlineOnly);
15191 verifyFormat("class C\n"
15192 "{\n"
15193 " int f() { return 42; }\n"
15194 "};",
15195 MergeInlineOnly);
15196 verifyFormat("struct C\n"
15197 "{\n"
15198 " int f() { return 42; }\n"
15199 "};",
15200 MergeInlineOnly);
15201 verifyFormat("struct C\n"
15202 "// comment\n"
15203 "/* comment */\n"
15204 "// comment\n"
15205 "{\n"
15206 " int f() { return 42; }\n"
15207 "};",
15208 MergeInlineOnly);
15209 verifyFormat("/* comment */ struct C\n"
15210 "{\n"
15211 " int f() { return 42; }\n"
15212 "};",
15213 MergeInlineOnly);
15214}
15215
15216TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
15217 FormatStyle MergeInlineOnly = getLLVMStyle();
15218 MergeInlineOnly.AllowShortFunctionsOnASingleLine =
15219 FormatStyle::SFS_InlineOnly;
15220 verifyFormat("class C {\n"
15221 " int f() { return 42; }\n"
15222 "};",
15223 MergeInlineOnly);
15224 verifyFormat("int f() {\n"
15225 " return 42;\n"
15226 "}",
15227 MergeInlineOnly);
15228
15229 // SFS_InlineOnly does not imply SFS_Empty
15230 verifyFormat("class C {\n"
15231 " int f() {}\n"
15232 "};",
15233 MergeInlineOnly);
15234 verifyFormat("int f() {\n"
15235 "}",
15236 MergeInlineOnly);
15237
15238 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
15239 verifyFormat("class Foo\n"
15240 " {\n"
15241 " void f() { foo(); }\n"
15242 " };",
15243 MergeInlineOnly);
15244
15245 // Also verify behavior when BraceWrapping.AfterFunction = true
15246 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
15247 MergeInlineOnly.BraceWrapping.AfterFunction = true;
15248 verifyFormat("class C {\n"
15249 " int f() { return 42; }\n"
15250 "};",
15251 MergeInlineOnly);
15252 verifyFormat("int f()\n"
15253 "{\n"
15254 " return 42;\n"
15255 "}",
15256 MergeInlineOnly);
15257
15258 // SFS_InlineOnly does not imply SFS_Empty
15259 verifyFormat("int f()\n"
15260 "{\n"
15261 "}",
15262 MergeInlineOnly);
15263 verifyFormat("class C {\n"
15264 " int f() {}\n"
15265 "};",
15266 MergeInlineOnly);
15267}
15268
15269TEST_F(FormatTest, SplitEmptyFunction) {
15270 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
15271 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
15272 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15273 Style.BraceWrapping.AfterFunction = true;
15274 Style.BraceWrapping.SplitEmptyFunction = false;
15275
15276 verifyFormat("int f()\n"
15277 "{}",
15278 Style);
15279 verifyFormat("int f()\n"
15280 "{\n"
15281 " return 42;\n"
15282 "}",
15283 Style);
15284 verifyFormat("int f()\n"
15285 "{\n"
15286 " // some comment\n"
15287 "}",
15288 Style);
15289
15290 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
15291 verifyFormat("int f() {}", Style);
15292 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15293 "{}",
15294 Style);
15295 verifyFormat("int f()\n"
15296 "{\n"
15297 " return 0;\n"
15298 "}",
15299 Style);
15300
15301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
15302 verifyFormat("class Foo {\n"
15303 " int f() {}\n"
15304 "};",
15305 Style);
15306 verifyFormat("class Foo {\n"
15307 " int f() { return 0; }\n"
15308 "};",
15309 Style);
15310 verifyFormat("class Foo {\n"
15311 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15312 " {}\n"
15313 "};",
15314 Style);
15315 verifyFormat("class Foo {\n"
15316 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15317 " {\n"
15318 " return 0;\n"
15319 " }\n"
15320 "};",
15321 Style);
15322
15323 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
15324 verifyFormat("int f() {}", Style);
15325 verifyFormat("int f() { return 0; }", Style);
15326 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15327 "{}",
15328 Style);
15329 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
15330 "{\n"
15331 " return 0;\n"
15332 "}",
15333 Style);
15334}
15335
15336TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
15337 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
15338 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
15339 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15340 Style.BraceWrapping.AfterFunction = true;
15341 Style.BraceWrapping.SplitEmptyFunction = true;
15342 Style.BraceWrapping.SplitEmptyRecord = false;
15343
15344 verifyFormat("class C {};", Style);
15345 verifyFormat("struct C {};", Style);
15346 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
15347 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
15348 "{\n"
15349 "}",
15350 Style);
15351 verifyFormat("class C {\n"
15352 " C()\n"
15353 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
15354 " bbbbbbbbbbbbbbbbbbb()\n"
15355 " {\n"
15356 " }\n"
15357 " void\n"
15358 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
15359 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
15360 " {\n"
15361 " }\n"
15362 "};",
15363 Style);
15364}
15365
15366TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
15367 FormatStyle Style = getLLVMStyle();
15368 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
15369 verifyFormat("#ifdef A\n"
15370 "int f() {}\n"
15371 "#else\n"
15372 "int g() {}\n"
15373 "#endif",
15374 Style);
15375}
15376
15377TEST_F(FormatTest, SplitEmptyClass) {
15378 FormatStyle Style = getLLVMStyle();
15379 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15380 Style.BraceWrapping.AfterClass = true;
15381 Style.BraceWrapping.SplitEmptyRecord = false;
15382
15383 verifyFormat("class Foo\n"
15384 "{};",
15385 Style);
15386 verifyFormat("/* something */ class Foo\n"
15387 "{};",
15388 Style);
15389 verifyFormat("template <typename X> class Foo\n"
15390 "{};",
15391 Style);
15392 verifyFormat("class Foo\n"
15393 "{\n"
15394 " Foo();\n"
15395 "};",
15396 Style);
15397 verifyFormat("typedef class Foo\n"
15398 "{\n"
15399 "} Foo_t;",
15400 Style);
15401
15402 Style.BraceWrapping.SplitEmptyRecord = true;
15403 Style.BraceWrapping.AfterStruct = true;
15404 verifyFormat("class rep\n"
15405 "{\n"
15406 "};",
15407 Style);
15408 verifyFormat("struct rep\n"
15409 "{\n"
15410 "};",
15411 Style);
15412 verifyFormat("template <typename T> class rep\n"
15413 "{\n"
15414 "};",
15415 Style);
15416 verifyFormat("template <typename T> struct rep\n"
15417 "{\n"
15418 "};",
15419 Style);
15420 verifyFormat("class rep\n"
15421 "{\n"
15422 " int x;\n"
15423 "};",
15424 Style);
15425 verifyFormat("struct rep\n"
15426 "{\n"
15427 " int x;\n"
15428 "};",
15429 Style);
15430 verifyFormat("template <typename T> class rep\n"
15431 "{\n"
15432 " int x;\n"
15433 "};",
15434 Style);
15435 verifyFormat("template <typename T> struct rep\n"
15436 "{\n"
15437 " int x;\n"
15438 "};",
15439 Style);
15440 verifyFormat("template <typename T> class rep // Foo\n"
15441 "{\n"
15442 " int x;\n"
15443 "};",
15444 Style);
15445 verifyFormat("template <typename T> struct rep // Bar\n"
15446 "{\n"
15447 " int x;\n"
15448 "};",
15449 Style);
15450
15451 verifyFormat("template <typename T> class rep<T>\n"
15452 "{\n"
15453 " int x;\n"
15454 "};",
15455 Style);
15456
15457 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
15458 "{\n"
15459 " int x;\n"
15460 "};",
15461 Style);
15462 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
15463 "{\n"
15464 "};",
15465 Style);
15466
15467 verifyFormat("#include \"stdint.h\"\n"
15468 "namespace rep {}",
15469 Style);
15470 verifyFormat("#include <stdint.h>\n"
15471 "namespace rep {}",
15472 Style);
15473 verifyFormat("#include <stdint.h>\n"
15474 "namespace rep {}",
15475 "#include <stdint.h>\n"
15476 "namespace rep {\n"
15477 "\n"
15478 "\n"
15479 "}",
15480 Style);
15481}
15482
15483TEST_F(FormatTest, SplitEmptyStruct) {
15484 FormatStyle Style = getLLVMStyle();
15485 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15486 Style.BraceWrapping.AfterStruct = true;
15487 Style.BraceWrapping.SplitEmptyRecord = false;
15488
15489 verifyFormat("struct Foo\n"
15490 "{};",
15491 Style);
15492 verifyFormat("/* something */ struct Foo\n"
15493 "{};",
15494 Style);
15495 verifyFormat("template <typename X> struct Foo\n"
15496 "{};",
15497 Style);
15498 verifyFormat("struct Foo\n"
15499 "{\n"
15500 " Foo();\n"
15501 "};",
15502 Style);
15503 verifyFormat("typedef struct Foo\n"
15504 "{\n"
15505 "} Foo_t;",
15506 Style);
15507 // typedef struct Bar {} Bar_t;
15508}
15509
15510TEST_F(FormatTest, SplitEmptyUnion) {
15511 FormatStyle Style = getLLVMStyle();
15512 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15513 Style.BraceWrapping.AfterUnion = true;
15514 Style.BraceWrapping.SplitEmptyRecord = false;
15515
15516 verifyFormat("union Foo\n"
15517 "{};",
15518 Style);
15519 verifyFormat("/* something */ union Foo\n"
15520 "{};",
15521 Style);
15522 verifyFormat("union Foo\n"
15523 "{\n"
15524 " A,\n"
15525 "};",
15526 Style);
15527 verifyFormat("typedef union Foo\n"
15528 "{\n"
15529 "} Foo_t;",
15530 Style);
15531}
15532
15533TEST_F(FormatTest, SplitEmptyNamespace) {
15534 FormatStyle Style = getLLVMStyle();
15535 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15536 Style.BraceWrapping.AfterNamespace = true;
15537 Style.BraceWrapping.SplitEmptyNamespace = false;
15538
15539 verifyFormat("namespace Foo\n"
15540 "{};",
15541 Style);
15542 verifyFormat("/* something */ namespace Foo\n"
15543 "{};",
15544 Style);
15545 verifyFormat("inline namespace Foo\n"
15546 "{};",
15547 Style);
15548 verifyFormat("/* something */ inline namespace Foo\n"
15549 "{};",
15550 Style);
15551 verifyFormat("export namespace Foo\n"
15552 "{};",
15553 Style);
15554 verifyFormat("namespace Foo\n"
15555 "{\n"
15556 "void Bar();\n"
15557 "};",
15558 Style);
15559}
15560
15561TEST_F(FormatTest, NeverMergeShortRecords) {
15562 FormatStyle Style = getLLVMStyle();
15563
15564 verifyFormat("class Foo {\n"
15565 " Foo();\n"
15566 "};",
15567 Style);
15568 verifyFormat("typedef class Foo {\n"
15569 " Foo();\n"
15570 "} Foo_t;",
15571 Style);
15572 verifyFormat("struct Foo {\n"
15573 " Foo();\n"
15574 "};",
15575 Style);
15576 verifyFormat("typedef struct Foo {\n"
15577 " Foo();\n"
15578 "} Foo_t;",
15579 Style);
15580 verifyFormat("union Foo {\n"
15581 " A,\n"
15582 "};",
15583 Style);
15584 verifyFormat("typedef union Foo {\n"
15585 " A,\n"
15586 "} Foo_t;",
15587 Style);
15588 verifyFormat("namespace Foo {\n"
15589 "void Bar();\n"
15590 "};",
15591 Style);
15592
15593 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15594 Style.BraceWrapping.AfterClass = true;
15595 Style.BraceWrapping.AfterStruct = true;
15596 Style.BraceWrapping.AfterUnion = true;
15597 Style.BraceWrapping.AfterNamespace = true;
15598 verifyFormat("class Foo\n"
15599 "{\n"
15600 " Foo();\n"
15601 "};",
15602 Style);
15603 verifyFormat("typedef class Foo\n"
15604 "{\n"
15605 " Foo();\n"
15606 "} Foo_t;",
15607 Style);
15608 verifyFormat("struct Foo\n"
15609 "{\n"
15610 " Foo();\n"
15611 "};",
15612 Style);
15613 verifyFormat("typedef struct Foo\n"
15614 "{\n"
15615 " Foo();\n"
15616 "} Foo_t;",
15617 Style);
15618 verifyFormat("union Foo\n"
15619 "{\n"
15620 " A,\n"
15621 "};",
15622 Style);
15623 verifyFormat("typedef union Foo\n"
15624 "{\n"
15625 " A,\n"
15626 "} Foo_t;",
15627 Style);
15628 verifyFormat("namespace Foo\n"
15629 "{\n"
15630 "void Bar();\n"
15631 "};",
15632 Style);
15633}
15634
15635TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
15636 // Elaborate type variable declarations.
15637 verifyFormat("struct foo a = {bar};\nint n;");
15638 verifyFormat("class foo a = {bar};\nint n;");
15639 verifyFormat("union foo a = {bar};\nint n;");
15640
15641 // Elaborate types inside function definitions.
15642 verifyFormat("struct foo f() {}\nint n;");
15643 verifyFormat("class foo f() {}\nint n;");
15644 verifyFormat("union foo f() {}\nint n;");
15645
15646 // Templates.
15647 verifyFormat("template <class X> void f() {}\nint n;");
15648 verifyFormat("template <struct X> void f() {}\nint n;");
15649 verifyFormat("template <union X> void f() {}\nint n;");
15650
15651 // Actual definitions...
15652 verifyFormat("struct {\n} n;");
15653 verifyFormat(
15654 "template <template <class T, class Y>, class Z> class X {\n} n;");
15655 verifyFormat("union Z {\n int n;\n} x;");
15656 verifyFormat("class MACRO Z {\n} n;");
15657 verifyFormat("class MACRO(X) Z {\n} n;");
15658 verifyFormat("class __attribute__((X)) Z {\n} n;");
15659 verifyFormat("class __declspec(X) Z {\n} n;");
15660 verifyFormat("class A##B##C {\n} n;");
15661 verifyFormat("class alignas(16) Z {\n} n;");
15662 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15663 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15664
15665 // Redefinition from nested context:
15666 verifyFormat("class A::B::C {\n} n;");
15667
15668 // Template definitions.
15669 verifyFormat(
15670 "template <typename F>\n"
15671 "Matcher(const Matcher<F> &Other,\n"
15672 " typename enable_if_c<is_base_of<F, T>::value &&\n"
15673 " !is_same<F, T>::value>::type * = 0)\n"
15674 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15675
15676 // FIXME: This is still incorrectly handled at the formatter side.
15677 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15678 verifyFormat("int i = SomeFunction(a<b, a> b);");
15679
15680 verifyFormat("class A<int> f() {}\n"
15681 "int n;");
15682 verifyFormat("template <typename T> class A<T> f() {}\n"
15683 "int n;");
15684
15685 verifyFormat("template <> class Foo<int> F() {\n"
15686 "} n;");
15687
15688 // Elaborate types where incorrectly parsing the structural element would
15689 // break the indent.
15690 verifyFormat("if (true)\n"
15691 " class X x;\n"
15692 "else\n"
15693 " f();");
15694
15695 // This is simply incomplete. Formatting is not important, but must not crash.
15696 verifyFormat("class A:");
15697}
15698
15699TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
15700 verifyNoChange("#error Leave all white!!!!! space* alone!");
15701 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15702 verifyFormat("#error 1", " # error 1");
15703 verifyFormat("#warning 1", " # warning 1");
15704}
15705
15706TEST_F(FormatTest, FormatHashIfExpressions) {
15707 verifyFormat("#if AAAA && BBBB");
15708 verifyFormat("#if (AAAA && BBBB)");
15709 verifyFormat("#elif (AAAA && BBBB)");
15710 // FIXME: Come up with a better indentation for #elif.
15711 verifyFormat(
15712 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15713 " defined(BBBBBBBB)\n"
15714 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15715 " defined(BBBBBBBB)\n"
15716 "#endif",
15717 getLLVMStyleWithColumns(65));
15718}
15719
15720TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
15721 FormatStyle AllowsMergedIf = getGoogleStyle();
15722 AllowsMergedIf.AllowShortIfStatementsOnASingleLine =
15723 FormatStyle::SIS_WithoutElse;
15724 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
15725 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
15726 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
15727 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf);
15728 FormatStyle ShortMergedIf = AllowsMergedIf;
15729 ShortMergedIf.ColumnLimit = 25;
15730 verifyFormat("#define A \\\n"
15731 " if (true) return 42;",
15732 ShortMergedIf);
15733 verifyFormat("#define A \\\n"
15734 " f(); \\\n"
15735 " if (true)\n"
15736 "#define B",
15737 ShortMergedIf);
15738 verifyFormat("#define A \\\n"
15739 " f(); \\\n"
15740 " if (true)\n"
15741 "g();",
15742 ShortMergedIf);
15743 verifyFormat("{\n"
15744 "#ifdef A\n"
15745 " // Comment\n"
15746 " if (true) continue;\n"
15747 "#endif\n"
15748 " // Comment\n"
15749 " if (true) continue;\n"
15750 "}",
15751 ShortMergedIf);
15752 ShortMergedIf.ColumnLimit = 33;
15753 verifyFormat("#define A \\\n"
15754 " if constexpr (true) return 42;",
15755 ShortMergedIf);
15756 verifyFormat("#define A \\\n"
15757 " if CONSTEXPR (true) return 42;",
15758 ShortMergedIf);
15759 ShortMergedIf.ColumnLimit = 29;
15760 verifyFormat("#define A \\\n"
15761 " if (aaaaaaaaaa) return 1; \\\n"
15762 " return 2;",
15763 ShortMergedIf);
15764 ShortMergedIf.ColumnLimit = 28;
15765 verifyFormat("#define A \\\n"
15766 " if (aaaaaaaaaa) \\\n"
15767 " return 1; \\\n"
15768 " return 2;",
15769 ShortMergedIf);
15770 verifyFormat("#define A \\\n"
15771 " if constexpr (aaaaaaa) \\\n"
15772 " return 1; \\\n"
15773 " return 2;",
15774 ShortMergedIf);
15775 verifyFormat("#define A \\\n"
15776 " if CONSTEXPR (aaaaaaa) \\\n"
15777 " return 1; \\\n"
15778 " return 2;",
15779 ShortMergedIf);
15780
15781 verifyFormat("//\n"
15782 "#define a \\\n"
15783 " if \\\n"
15784 " 0",
15785 getChromiumStyle(FormatStyle::LK_Cpp));
15786}
15787
15788TEST_F(FormatTest, FormatStarDependingOnContext) {
15789 verifyFormat("void f(int *a);");
15790 verifyFormat("void f() { f(fint * b); }");
15791 verifyFormat("class A {\n void f(int *a);\n};");
15792 verifyFormat("class A {\n int *a;\n};");
15793 verifyFormat("namespace a {\n"
15794 "namespace b {\n"
15795 "class A {\n"
15796 " void f() {}\n"
15797 " int *a;\n"
15798 "};\n"
15799 "} // namespace b\n"
15800 "} // namespace a");
15801}
15802
15803TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
15804 verifyFormat("while");
15805 verifyFormat("operator");
15806}
15807
15808TEST_F(FormatTest, SkipsDeeplyNestedLines) {
15809 // This code would be painfully slow to format if we didn't skip it.
15810 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
15811 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15812 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15813 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15814 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15815 "A(1, 1)\n"
15816 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15817 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15818 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15819 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15820 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15821 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15822 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15823 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15824 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15825 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15826 // Deeply nested part is untouched, rest is formatted.
15827 EXPECT_EQ(std::string("int i;") + Code + "int j;",
15828 format(std::string("int i;") + Code + "int j;",
15829 getLLVMStyle(), SC_ExpectIncomplete));
15830}
15831
15832//===----------------------------------------------------------------------===//
15833// Objective-C tests.
15834//===----------------------------------------------------------------------===//
15835
15836TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
15837 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15838 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15839 "-(NSUInteger)indexOfObject:(id)anObject;");
15840 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15841 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15842 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15843 "-(NSInteger)Method3:(id)anObject;");
15844 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15845 "-(NSInteger)Method4:(id)anObject;");
15846 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15847 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15848 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15849 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15850 "forAllCells:(BOOL)flag;");
15851
15852 // Very long objectiveC method declaration.
15853 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15854 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15855 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15856 " inRange:(NSRange)range\n"
15857 " outRange:(NSRange)out_range\n"
15858 " outRange1:(NSRange)out_range1\n"
15859 " outRange2:(NSRange)out_range2\n"
15860 " outRange3:(NSRange)out_range3\n"
15861 " outRange4:(NSRange)out_range4\n"
15862 " outRange5:(NSRange)out_range5\n"
15863 " outRange6:(NSRange)out_range6\n"
15864 " outRange7:(NSRange)out_range7\n"
15865 " outRange8:(NSRange)out_range8\n"
15866 " outRange9:(NSRange)out_range9;");
15867
15868 // When the function name has to be wrapped.
15869 FormatStyle Style = getLLVMStyle();
15870 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15871 // and always indents instead.
15872 Style.IndentWrappedFunctionNames = false;
15873 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15874 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15875 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15876 "}",
15877 Style);
15878 Style.IndentWrappedFunctionNames = true;
15879 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15880 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15881 " anotherName:(NSString)dddddddddddddd {\n"
15882 "}",
15883 Style);
15884
15885 verifyFormat("- (int)sum:(vector<int>)numbers;");
15886 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15887 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15888 // protocol lists (but not for template classes):
15889 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15890
15891 verifyFormat("- (int (*)())foo:(int (*)())f;");
15892 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15893
15894 // If there's no return type (very rare in practice!), LLVM and Google style
15895 // agree.
15896 verifyFormat("- foo;");
15897 verifyFormat("- foo:(int)f;");
15898 verifyGoogleFormat("- foo:(int)foo;");
15899}
15900
15901TEST_F(FormatTest, BreaksStringLiterals) {
15902 // FIXME: unstable test case
15903 EXPECT_EQ("\"some text \"\n"
15904 "\"other\";",
15905 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15906 // FIXME: unstable test case
15907 EXPECT_EQ("\"some text \"\n"
15908 "\"other\";",
15909 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15910 verifyFormat("#define A \\\n"
15911 " \"some \" \\\n"
15912 " \"text \" \\\n"
15913 " \"other\";",
15914 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15915 verifyFormat("#define A \\\n"
15916 " \"so \" \\\n"
15917 " \"text \" \\\n"
15918 " \"other\";",
15919 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15920
15921 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15922 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15923 // FIXME: unstable test case
15924 EXPECT_EQ("\"some \"\n"
15925 "\"text\"",
15926 format("\"some text\"", getLLVMStyleWithColumns(10)));
15927 // FIXME: unstable test case
15928 EXPECT_EQ("\"some \"\n"
15929 "\"text\"",
15930 format("\"some text\"", getLLVMStyleWithColumns(7)));
15931 // FIXME: unstable test case
15932 EXPECT_EQ("\"some\"\n"
15933 "\" tex\"\n"
15934 "\"t\"",
15935 format("\"some text\"", getLLVMStyleWithColumns(6)));
15936 // FIXME: unstable test case
15937 EXPECT_EQ("\"some\"\n"
15938 "\" tex\"\n"
15939 "\" and\"",
15940 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15941 // FIXME: unstable test case
15942 EXPECT_EQ("\"some\"\n"
15943 "\"/tex\"\n"
15944 "\"/and\"",
15945 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15946
15947 verifyFormat("variable =\n"
15948 " \"long string \"\n"
15949 " \"literal\";",
15950 "variable = \"long string literal\";",
15951 getLLVMStyleWithColumns(20));
15952
15953 verifyFormat("variable = f(\n"
15954 " \"long string \"\n"
15955 " \"literal\",\n"
15956 " short,\n"
15957 " loooooooooooooooooooong);",
15958 "variable = f(\"long string literal\", short, "
15959 "loooooooooooooooooooong);",
15960 getLLVMStyleWithColumns(20));
15961
15962 verifyFormat("f(g(\"long string \"\n"
15963 " \"literal\"),\n"
15964 " b);",
15965 "f(g(\"long string literal\"), b);",
15966 getLLVMStyleWithColumns(20));
15967 verifyFormat("f(g(\"long string \"\n"
15968 " \"literal\",\n"
15969 " a),\n"
15970 " b);",
15971 "f(g(\"long string literal\", a), b);",
15972 getLLVMStyleWithColumns(20));
15973 verifyFormat("f(\"one two\".split(\n"
15974 " variable));",
15975 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15976 verifyFormat("f(\"one two three four five six \"\n"
15977 " \"seven\".split(\n"
15978 " really_looooong_variable));",
15979 "f(\"one two three four five six seven\"."
15980 "split(really_looooong_variable));",
15981 getLLVMStyleWithColumns(33));
15982
15983 verifyFormat("f(\"some \"\n"
15984 " \"text\",\n"
15985 " other);",
15986 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15987
15988 // Only break as a last resort.
15989 verifyFormat(
15990 "aaaaaaaaaaaaaaaaaaaa(\n"
15991 " aaaaaaaaaaaaaaaaaaaa,\n"
15992 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15993
15994 // FIXME: unstable test case
15995 EXPECT_EQ("\"splitmea\"\n"
15996 "\"trandomp\"\n"
15997 "\"oint\"",
15998 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15999
16000 // FIXME: unstable test case
16001 EXPECT_EQ("\"split/\"\n"
16002 "\"pathat/\"\n"
16003 "\"slashes\"",
16004 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
16005
16006 // FIXME: unstable test case
16007 EXPECT_EQ("\"split/\"\n"
16008 "\"pathat/\"\n"
16009 "\"slashes\"",
16010 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
16011 // FIXME: unstable test case
16012 EXPECT_EQ("\"split at \"\n"
16013 "\"spaces/at/\"\n"
16014 "\"slashes.at.any$\"\n"
16015 "\"non-alphanumeric%\"\n"
16016 "\"1111111111characte\"\n"
16017 "\"rs\"",
16018 format("\"split at "
16019 "spaces/at/"
16020 "slashes.at."
16021 "any$non-"
16022 "alphanumeric%"
16023 "1111111111characte"
16024 "rs\"",
16025 getLLVMStyleWithColumns(20)));
16026
16027 // Verify that splitting the strings understands
16028 // Style::AlwaysBreakBeforeMultilineStrings.
16029 verifyFormat("aaaaaaaaaaaa(\n"
16030 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
16031 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
16032 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
16033 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
16034 "aaaaaaaaaaaaaaaaaaaaaa\");",
16035 getGoogleStyle());
16036 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
16037 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
16038 "return \"aaaaaaaaaaaaaaaaaaaaaa "
16039 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
16040 "aaaaaaaaaaaaaaaaaaaaaa\";",
16041 getGoogleStyle());
16042 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
16043 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
16044 "llvm::outs() << "
16045 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
16046 "aaaaaaaaaaaaaaaaaaa\";");
16047 verifyFormat("ffff(\n"
16048 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
16049 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
16050 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
16051 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
16052 getGoogleStyle());
16053
16054 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 12);
16055 Style.BreakStringLiterals = false;
16056 verifyFormat("\"some text other\";", Style);
16057
16058 FormatStyle AlignLeft = getLLVMStyleWithColumns(ColumnLimit: 12);
16059 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16060 verifyFormat("#define A \\\n"
16061 " \"some \" \\\n"
16062 " \"text \" \\\n"
16063 " \"other\";",
16064 "#define A \"some text other\";", AlignLeft);
16065}
16066
16067TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) {
16068 verifyFormat("C a = \"some more \"\n"
16069 " \"text\";",
16070 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
16071}
16072
16073TEST_F(FormatTest, FullyRemoveEmptyLines) {
16074 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(ColumnLimit: 80);
16075 NoEmptyLines.MaxEmptyLinesToKeep = 0;
16076 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
16077 NoEmptyLines);
16078}
16079
16080TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
16081 // FIXME: unstable test case
16082 EXPECT_EQ(
16083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16084 "(\n"
16085 " \"x\t\");",
16086 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16087 "aaaaaaa("
16088 "\"x\t\");"));
16089}
16090
16091TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
16092 // FIXME: unstable test case
16093 EXPECT_EQ(
16094 "u8\"utf8 string \"\n"
16095 "u8\"literal\";",
16096 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
16097 // FIXME: unstable test case
16098 EXPECT_EQ(
16099 "u\"utf16 string \"\n"
16100 "u\"literal\";",
16101 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
16102 // FIXME: unstable test case
16103 EXPECT_EQ(
16104 "U\"utf32 string \"\n"
16105 "U\"literal\";",
16106 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
16107 // FIXME: unstable test case
16108 EXPECT_EQ("L\"wide string \"\n"
16109 "L\"literal\";",
16110 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
16111 verifyFormat("@\"NSString \"\n"
16112 "@\"literal\";",
16113 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
16114 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
16115
16116 // This input makes clang-format try to split the incomplete unicode escape
16117 // sequence, which used to lead to a crasher.
16118 verifyNoCrash(
16119 Code: "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
16120 Style: getLLVMStyleWithColumns(ColumnLimit: 60));
16121}
16122
16123TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
16124 FormatStyle Style = getGoogleStyleWithColumns(ColumnLimit: 15);
16125 verifyFormat("R\"x(raw literal)x\";", Style);
16126 verifyFormat("uR\"x(raw literal)x\";", Style);
16127 verifyFormat("LR\"x(raw literal)x\";", Style);
16128 verifyFormat("UR\"x(raw literal)x\";", Style);
16129 verifyFormat("u8R\"x(raw literal)x\";", Style);
16130}
16131
16132TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
16133 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20);
16134 // FIXME: unstable test case
16135 EXPECT_EQ(
16136 "_T(\"aaaaaaaaaaaaaa\")\n"
16137 "_T(\"aaaaaaaaaaaaaa\")\n"
16138 "_T(\"aaaaaaaaaaaa\")",
16139 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
16140 verifyFormat("f(x,\n"
16141 " _T(\"aaaaaaaaaaaa\")\n"
16142 " _T(\"aaa\"),\n"
16143 " z);",
16144 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style);
16145
16146 // FIXME: Handle embedded spaces in one iteration.
16147 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
16148 // "_T(\"aaaaaaaaaaaaa\")\n"
16149 // "_T(\"aaaaaaaaaaaaa\")\n"
16150 // "_T(\"a\")",
16151 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
16152 // getLLVMStyleWithColumns(20)));
16153 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
16154 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style);
16155 verifyFormat("f(\n"
16156 "#if !TEST\n"
16157 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
16158 "#endif\n"
16159 ");",
16160 "f(\n"
16161 "#if !TEST\n"
16162 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
16163 "#endif\n"
16164 ");");
16165 verifyFormat("f(\n"
16166 "\n"
16167 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
16168 "f(\n"
16169 "\n"
16170 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
16171 // Regression test for accessing tokens past the end of a vector in the
16172 // TokenLexer.
16173 verifyNoCrash(Code: R"(_T(
16174"
16175)
16176)");
16177}
16178
16179TEST_F(FormatTest, BreaksStringLiteralOperands) {
16180 // In a function call with two operands, the second can be broken with no line
16181 // break before it.
16182 verifyFormat("func(a, \"long long \"\n"
16183 " \"long long\");",
16184 "func(a, \"long long long long\");",
16185 getLLVMStyleWithColumns(24));
16186 // In a function call with three operands, the second must be broken with a
16187 // line break before it.
16188 verifyFormat("func(a,\n"
16189 " \"long long long \"\n"
16190 " \"long\",\n"
16191 " c);",
16192 "func(a, \"long long long long\", c);",
16193 getLLVMStyleWithColumns(24));
16194 // In a function call with three operands, the third must be broken with a
16195 // line break before it.
16196 verifyFormat("func(a, b,\n"
16197 " \"long long long \"\n"
16198 " \"long\");",
16199 "func(a, b, \"long long long long\");",
16200 getLLVMStyleWithColumns(24));
16201 // In a function call with three operands, both the second and the third must
16202 // be broken with a line break before them.
16203 verifyFormat("func(a,\n"
16204 " \"long long long \"\n"
16205 " \"long\",\n"
16206 " \"long long long \"\n"
16207 " \"long\");",
16208 "func(a, \"long long long long\", \"long long long long\");",
16209 getLLVMStyleWithColumns(24));
16210 // In a chain of << with two operands, the second can be broken with no line
16211 // break before it.
16212 verifyFormat("a << \"line line \"\n"
16213 " \"line\";",
16214 "a << \"line line line\";", getLLVMStyleWithColumns(20));
16215 // In a chain of << with three operands, the second can be broken with no line
16216 // break before it.
16217 verifyFormat("abcde << \"line \"\n"
16218 " \"line line\"\n"
16219 " << c;",
16220 "abcde << \"line line line\" << c;",
16221 getLLVMStyleWithColumns(20));
16222 // In a chain of << with three operands, the third must be broken with a line
16223 // break before it.
16224 verifyFormat("a << b\n"
16225 " << \"line line \"\n"
16226 " \"line\";",
16227 "a << b << \"line line line\";", getLLVMStyleWithColumns(20));
16228 // In a chain of << with three operands, the second can be broken with no line
16229 // break before it and the third must be broken with a line break before it.
16230 verifyFormat("abcd << \"line line \"\n"
16231 " \"line\"\n"
16232 " << \"line line \"\n"
16233 " \"line\";",
16234 "abcd << \"line line line\" << \"line line line\";",
16235 getLLVMStyleWithColumns(20));
16236 // In a chain of binary operators with two operands, the second can be broken
16237 // with no line break before it.
16238 verifyFormat("abcd + \"line line \"\n"
16239 " \"line line\";",
16240 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20));
16241 // In a chain of binary operators with three operands, the second must be
16242 // broken with a line break before it.
16243 verifyFormat("abcd +\n"
16244 " \"line line \"\n"
16245 " \"line line\" +\n"
16246 " e;",
16247 "abcd + \"line line line line\" + e;",
16248 getLLVMStyleWithColumns(20));
16249 // In a function call with two operands, with AlignAfterOpenBracket enabled,
16250 // the first must be broken with a line break before it.
16251 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 25);
16252 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
16253 verifyFormat("someFunction(\n"
16254 " \"long long long \"\n"
16255 " \"long\",\n"
16256 " a);",
16257 "someFunction(\"long long long long\", a);", Style);
16258 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
16259 verifyFormat("someFunction(\n"
16260 " \"long long long \"\n"
16261 " \"long\",\n"
16262 " a\n"
16263 ");",
16264 Style);
16265}
16266
16267TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
16268 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
16269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
16270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
16271 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
16272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
16273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";");
16274}
16275
16276TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
16277 verifyFormat("f(g(R\"x(raw literal)x\", a), b);",
16278 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle());
16279 verifyFormat("fffffffffff(g(R\"x(\n"
16280 "multiline raw string literal xxxxxxxxxxxxxx\n"
16281 ")x\",\n"
16282 " a),\n"
16283 " b);",
16284 "fffffffffff(g(R\"x(\n"
16285 "multiline raw string literal xxxxxxxxxxxxxx\n"
16286 ")x\", a), b);",
16287 getGoogleStyleWithColumns(20));
16288 verifyFormat("fffffffffff(\n"
16289 " g(R\"x(qqq\n"
16290 "multiline raw string literal xxxxxxxxxxxxxx\n"
16291 ")x\",\n"
16292 " a),\n"
16293 " b);",
16294 "fffffffffff(g(R\"x(qqq\n"
16295 "multiline raw string literal xxxxxxxxxxxxxx\n"
16296 ")x\", a), b);",
16297 getGoogleStyleWithColumns(20));
16298
16299 verifyNoChange("fffffffffff(R\"x(\n"
16300 "multiline raw string literal xxxxxxxxxxxxxx\n"
16301 ")x\");",
16302 getGoogleStyleWithColumns(20));
16303 verifyFormat("fffffffffff(R\"x(\n"
16304 "multiline raw string literal xxxxxxxxxxxxxx\n"
16305 ")x\" + bbbbbb);",
16306 "fffffffffff(R\"x(\n"
16307 "multiline raw string literal xxxxxxxxxxxxxx\n"
16308 ")x\" + bbbbbb);",
16309 getGoogleStyleWithColumns(20));
16310 verifyFormat("fffffffffff(\n"
16311 " R\"x(\n"
16312 "multiline raw string literal xxxxxxxxxxxxxx\n"
16313 ")x\" +\n"
16314 " bbbbbb);",
16315 "fffffffffff(\n"
16316 " R\"x(\n"
16317 "multiline raw string literal xxxxxxxxxxxxxx\n"
16318 ")x\" + bbbbbb);",
16319 getGoogleStyleWithColumns(20));
16320 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);",
16321 "fffffffffff(\n"
16322 " R\"(single line raw string)\" + bbbbbb);");
16323}
16324
16325TEST_F(FormatTest, SkipsUnknownStringLiterals) {
16326 verifyFormat("string a = \"unterminated;");
16327 verifyFormat("function(\"unterminated,\n"
16328 " OtherParameter);",
16329 "function( \"unterminated,\n"
16330 " OtherParameter);");
16331}
16332
16333TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
16334 FormatStyle Style = getLLVMStyle();
16335 Style.Standard = FormatStyle::LS_Cpp03;
16336 verifyFormat("#define x(_a) printf(\"foo\" _a);",
16337 "#define x(_a) printf(\"foo\"_a);", Style);
16338}
16339
16340TEST_F(FormatTest, CppLexVersion) {
16341 FormatStyle Style = getLLVMStyle();
16342 // Formatting of x * y differs if x is a type.
16343 verifyFormat("void foo() { MACRO(a * b); }", Style);
16344 verifyFormat("void foo() { MACRO(int *b); }", Style);
16345
16346 // LLVM style uses latest lexer.
16347 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
16348 Style.Standard = FormatStyle::LS_Cpp17;
16349 // But in c++17, char8_t isn't a keyword.
16350 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
16351}
16352
16353TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
16354
16355TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
16356 verifyFormat("someFunction(\"aaabbbcccd\"\n"
16357 " \"ddeeefff\");",
16358 "someFunction(\"aaabbbcccdddeeefff\");",
16359 getLLVMStyleWithColumns(25));
16360 verifyFormat("someFunction1234567890(\n"
16361 " \"aaabbbcccdddeeefff\");",
16362 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
16363 getLLVMStyleWithColumns(26));
16364 verifyFormat("someFunction1234567890(\n"
16365 " \"aaabbbcccdddeeeff\"\n"
16366 " \"f\");",
16367 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
16368 getLLVMStyleWithColumns(25));
16369 verifyFormat("someFunction1234567890(\n"
16370 " \"aaabbbcccdddeeeff\"\n"
16371 " \"f\");",
16372 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
16373 getLLVMStyleWithColumns(24));
16374 verifyFormat("someFunction(\n"
16375 " \"aaabbbcc ddde \"\n"
16376 " \"efff\");",
16377 "someFunction(\"aaabbbcc ddde efff\");",
16378 getLLVMStyleWithColumns(25));
16379 verifyFormat("someFunction(\"aaabbbccc \"\n"
16380 " \"ddeeefff\");",
16381 "someFunction(\"aaabbbccc ddeeefff\");",
16382 getLLVMStyleWithColumns(25));
16383 verifyFormat("someFunction1234567890(\n"
16384 " \"aaabb \"\n"
16385 " \"cccdddeeefff\");",
16386 "someFunction1234567890(\"aaabb cccdddeeefff\");",
16387 getLLVMStyleWithColumns(25));
16388 verifyFormat("#define A \\\n"
16389 " string s = \\\n"
16390 " \"123456789\" \\\n"
16391 " \"0\"; \\\n"
16392 " int i;",
16393 "#define A string s = \"1234567890\"; int i;",
16394 getLLVMStyleWithColumns(20));
16395 verifyFormat("someFunction(\n"
16396 " \"aaabbbcc \"\n"
16397 " \"dddeeefff\");",
16398 "someFunction(\"aaabbbcc dddeeefff\");",
16399 getLLVMStyleWithColumns(25));
16400}
16401
16402TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
16403 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
16404 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
16405 // FIXME: unstable test case
16406 EXPECT_EQ("\"test\"\n"
16407 "\"\\n\"",
16408 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
16409 // FIXME: unstable test case
16410 EXPECT_EQ("\"tes\\\\\"\n"
16411 "\"n\"",
16412 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
16413 // FIXME: unstable test case
16414 EXPECT_EQ("\"\\\\\\\\\"\n"
16415 "\"\\n\"",
16416 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
16417 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
16418 // FIXME: unstable test case
16419 EXPECT_EQ("\"\\uff01\"\n"
16420 "\"test\"",
16421 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
16422 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
16423 // FIXME: unstable test case
16424 EXPECT_EQ("\"\\x000000000001\"\n"
16425 "\"next\"",
16426 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
16427 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
16428 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
16429 // FIXME: unstable test case
16430 EXPECT_EQ("\"test\"\n"
16431 "\"\\000000\"\n"
16432 "\"000001\"",
16433 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
16434 // FIXME: unstable test case
16435 EXPECT_EQ("\"test\\000\"\n"
16436 "\"00000000\"\n"
16437 "\"1\"",
16438 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
16439}
16440
16441TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
16442 verifyFormat("void f() {\n"
16443 " return g() {}\n"
16444 " void h() {}");
16445 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
16446 "g();\n"
16447 "}");
16448}
16449
16450TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
16451 verifyFormat(
16452 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
16453}
16454
16455TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
16456 verifyFormat("class X {\n"
16457 " void f() {\n"
16458 " }\n"
16459 "};",
16460 getLLVMStyleWithColumns(12));
16461}
16462
16463TEST_F(FormatTest, ConfigurableIndentWidth) {
16464 FormatStyle EightIndent = getLLVMStyleWithColumns(ColumnLimit: 18);
16465 EightIndent.IndentWidth = 8;
16466 EightIndent.ContinuationIndentWidth = 8;
16467 verifyFormat("void f() {\n"
16468 " someFunction();\n"
16469 " if (true) {\n"
16470 " f();\n"
16471 " }\n"
16472 "}",
16473 EightIndent);
16474 verifyFormat("class X {\n"
16475 " void f() {\n"
16476 " }\n"
16477 "};",
16478 EightIndent);
16479 verifyFormat("int x[] = {\n"
16480 " call(),\n"
16481 " call()};",
16482 EightIndent);
16483}
16484
16485TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
16486 verifyFormat("double\n"
16487 "f();",
16488 getLLVMStyleWithColumns(8));
16489}
16490
16491TEST_F(FormatTest, ConfigurableUseOfTab) {
16492 FormatStyle Tab = getLLVMStyleWithColumns(ColumnLimit: 42);
16493 Tab.IndentWidth = 8;
16494 Tab.UseTab = FormatStyle::UT_Always;
16495 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
16496
16497 verifyFormat("if (aaaaaaaa && // q\n"
16498 " bb)\t\t// w\n"
16499 "\t;",
16500 "if (aaaaaaaa &&// q\n"
16501 "bb)// w\n"
16502 ";",
16503 Tab);
16504 verifyFormat("if (aaa && bbb) // w\n"
16505 "\t;",
16506 "if(aaa&&bbb)// w\n"
16507 ";",
16508 Tab);
16509
16510 verifyFormat("class X {\n"
16511 "\tvoid f() {\n"
16512 "\t\tsomeFunction(parameter1,\n"
16513 "\t\t\t parameter2);\n"
16514 "\t}\n"
16515 "};",
16516 Tab);
16517 verifyFormat("#define A \\\n"
16518 "\tvoid f() { \\\n"
16519 "\t\tsomeFunction( \\\n"
16520 "\t\t parameter1, \\\n"
16521 "\t\t parameter2); \\\n"
16522 "\t}",
16523 Tab);
16524 verifyFormat("int a;\t // x\n"
16525 "int bbbbbbbb; // x",
16526 Tab);
16527
16528 FormatStyle TabAlignment = Tab;
16529 TabAlignment.AlignConsecutiveDeclarations.Enabled = true;
16530 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16531 verifyFormat("unsigned long long big;\n"
16532 "char*\t\t ptr;",
16533 TabAlignment);
16534 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16535 verifyFormat("unsigned long long big;\n"
16536 "char *\t\t ptr;",
16537 TabAlignment);
16538 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16539 verifyFormat("unsigned long long big;\n"
16540 "char\t\t *ptr;",
16541 TabAlignment);
16542
16543 Tab.TabWidth = 4;
16544 Tab.IndentWidth = 8;
16545 verifyFormat("class TabWidth4Indent8 {\n"
16546 "\t\tvoid f() {\n"
16547 "\t\t\t\tsomeFunction(parameter1,\n"
16548 "\t\t\t\t\t\t\t parameter2);\n"
16549 "\t\t}\n"
16550 "};",
16551 Tab);
16552
16553 Tab.TabWidth = 4;
16554 Tab.IndentWidth = 4;
16555 verifyFormat("class TabWidth4Indent4 {\n"
16556 "\tvoid f() {\n"
16557 "\t\tsomeFunction(parameter1,\n"
16558 "\t\t\t\t\t parameter2);\n"
16559 "\t}\n"
16560 "};",
16561 Tab);
16562
16563 Tab.TabWidth = 8;
16564 Tab.IndentWidth = 4;
16565 verifyFormat("class TabWidth8Indent4 {\n"
16566 " void f() {\n"
16567 "\tsomeFunction(parameter1,\n"
16568 "\t\t parameter2);\n"
16569 " }\n"
16570 "};",
16571 Tab);
16572
16573 Tab.TabWidth = 8;
16574 Tab.IndentWidth = 8;
16575 verifyFormat("/*\n"
16576 "\t a\t\tcomment\n"
16577 "\t in multiple lines\n"
16578 " */",
16579 " /*\t \t \n"
16580 " \t \t a\t\tcomment\t \t\n"
16581 " \t \t in multiple lines\t\n"
16582 " \t */",
16583 Tab);
16584
16585 TabAlignment.UseTab = FormatStyle::UT_ForIndentation;
16586 TabAlignment.PointerAlignment = FormatStyle::PAS_Left;
16587 verifyFormat("void f() {\n"
16588 "\tunsigned long long big;\n"
16589 "\tchar* ptr;\n"
16590 "}",
16591 TabAlignment);
16592 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle;
16593 verifyFormat("void f() {\n"
16594 "\tunsigned long long big;\n"
16595 "\tchar * ptr;\n"
16596 "}",
16597 TabAlignment);
16598 TabAlignment.PointerAlignment = FormatStyle::PAS_Right;
16599 verifyFormat("void f() {\n"
16600 "\tunsigned long long big;\n"
16601 "\tchar *ptr;\n"
16602 "}",
16603 TabAlignment);
16604
16605 Tab.UseTab = FormatStyle::UT_ForIndentation;
16606 verifyFormat("{\n"
16607 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16608 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16609 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16610 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16611 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16612 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16613 "};",
16614 Tab);
16615 verifyFormat("enum AA {\n"
16616 "\ta1, // Force multiple lines\n"
16617 "\ta2,\n"
16618 "\ta3\n"
16619 "};",
16620 Tab);
16621 verifyFormat("if (aaaaaaaa && // q\n"
16622 " bb) // w\n"
16623 "\t;",
16624 "if (aaaaaaaa &&// q\n"
16625 "bb)// w\n"
16626 ";",
16627 Tab);
16628 verifyFormat("class X {\n"
16629 "\tvoid f() {\n"
16630 "\t\tsomeFunction(parameter1,\n"
16631 "\t\t parameter2);\n"
16632 "\t}\n"
16633 "};",
16634 Tab);
16635 verifyFormat("{\n"
16636 "\tQ(\n"
16637 "\t {\n"
16638 "\t\t int a;\n"
16639 "\t\t someFunction(aaaaaaaa,\n"
16640 "\t\t bbbbbbb);\n"
16641 "\t },\n"
16642 "\t p);\n"
16643 "}",
16644 Tab);
16645 verifyFormat("{\n"
16646 "\t/* aaaa\n"
16647 "\t bbbb */\n"
16648 "}",
16649 "{\n"
16650 "/* aaaa\n"
16651 " bbbb */\n"
16652 "}",
16653 Tab);
16654 verifyFormat("{\n"
16655 "\t/*\n"
16656 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16657 "\t bbbbbbbbbbbbb\n"
16658 "\t*/\n"
16659 "}",
16660 "{\n"
16661 "/*\n"
16662 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16663 "*/\n"
16664 "}",
16665 Tab);
16666 verifyFormat("{\n"
16667 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16668 "\t// bbbbbbbbbbbbb\n"
16669 "}",
16670 "{\n"
16671 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16672 "}",
16673 Tab);
16674 verifyFormat("{\n"
16675 "\t/*\n"
16676 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16677 "\t bbbbbbbbbbbbb\n"
16678 "\t*/\n"
16679 "}",
16680 "{\n"
16681 "\t/*\n"
16682 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16683 "\t*/\n"
16684 "}",
16685 Tab);
16686 verifyNoChange("{\n"
16687 "\t/*\n"
16688 "\n"
16689 "\t*/\n"
16690 "}",
16691 Tab);
16692 verifyNoChange("{\n"
16693 "\t/*\n"
16694 " asdf\n"
16695 "\t*/\n"
16696 "}",
16697 Tab);
16698
16699 verifyFormat("void f() {\n"
16700 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16701 "\t : bbbbbbbbbbbbbbbbbb\n"
16702 "}",
16703 Tab);
16704 FormatStyle TabNoBreak = Tab;
16705 TabNoBreak.BreakBeforeTernaryOperators = false;
16706 verifyFormat("void f() {\n"
16707 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16708 "\t bbbbbbbbbbbbbbbbbb\n"
16709 "}",
16710 TabNoBreak);
16711 verifyFormat("void f() {\n"
16712 "\treturn true ?\n"
16713 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16714 "\t bbbbbbbbbbbbbbbbbbbb\n"
16715 "}",
16716 TabNoBreak);
16717
16718 Tab.UseTab = FormatStyle::UT_Never;
16719 verifyFormat("/*\n"
16720 " a\t\tcomment\n"
16721 " in multiple lines\n"
16722 " */",
16723 " /*\t \t \n"
16724 " \t \t a\t\tcomment\t \t\n"
16725 " \t \t in multiple lines\t\n"
16726 " \t */",
16727 Tab);
16728 verifyFormat("/* some\n"
16729 " comment */",
16730 " \t \t /* some\n"
16731 " \t \t comment */",
16732 Tab);
16733 verifyFormat("int a; /* some\n"
16734 " comment */",
16735 " \t \t int a; /* some\n"
16736 " \t \t comment */",
16737 Tab);
16738
16739 verifyFormat("int a; /* some\n"
16740 "comment */",
16741 " \t \t int\ta; /* some\n"
16742 " \t \t comment */",
16743 Tab);
16744 verifyFormat("f(\"\t\t\"); /* some\n"
16745 " comment */",
16746 " \t \t f(\"\t\t\"); /* some\n"
16747 " \t \t comment */",
16748 Tab);
16749 verifyFormat("{\n"
16750 " /*\n"
16751 " * Comment\n"
16752 " */\n"
16753 " int i;\n"
16754 "}",
16755 "{\n"
16756 "\t/*\n"
16757 "\t * Comment\n"
16758 "\t */\n"
16759 "\t int i;\n"
16760 "}",
16761 Tab);
16762
16763 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
16764 Tab.TabWidth = 8;
16765 Tab.IndentWidth = 8;
16766 verifyFormat("if (aaaaaaaa && // q\n"
16767 " bb) // w\n"
16768 "\t;",
16769 "if (aaaaaaaa &&// q\n"
16770 "bb)// w\n"
16771 ";",
16772 Tab);
16773 verifyFormat("if (aaa && bbb) // w\n"
16774 "\t;",
16775 "if(aaa&&bbb)// w\n"
16776 ";",
16777 Tab);
16778 verifyFormat("class X {\n"
16779 "\tvoid f() {\n"
16780 "\t\tsomeFunction(parameter1,\n"
16781 "\t\t\t parameter2);\n"
16782 "\t}\n"
16783 "};",
16784 Tab);
16785 verifyFormat("#define A \\\n"
16786 "\tvoid f() { \\\n"
16787 "\t\tsomeFunction( \\\n"
16788 "\t\t parameter1, \\\n"
16789 "\t\t parameter2); \\\n"
16790 "\t}",
16791 Tab);
16792 Tab.TabWidth = 4;
16793 Tab.IndentWidth = 8;
16794 verifyFormat("class TabWidth4Indent8 {\n"
16795 "\t\tvoid f() {\n"
16796 "\t\t\t\tsomeFunction(parameter1,\n"
16797 "\t\t\t\t\t\t\t parameter2);\n"
16798 "\t\t}\n"
16799 "};",
16800 Tab);
16801 Tab.TabWidth = 4;
16802 Tab.IndentWidth = 4;
16803 verifyFormat("class TabWidth4Indent4 {\n"
16804 "\tvoid f() {\n"
16805 "\t\tsomeFunction(parameter1,\n"
16806 "\t\t\t\t\t parameter2);\n"
16807 "\t}\n"
16808 "};",
16809 Tab);
16810 Tab.TabWidth = 8;
16811 Tab.IndentWidth = 4;
16812 verifyFormat("class TabWidth8Indent4 {\n"
16813 " void f() {\n"
16814 "\tsomeFunction(parameter1,\n"
16815 "\t\t parameter2);\n"
16816 " }\n"
16817 "};",
16818 Tab);
16819 Tab.TabWidth = 8;
16820 Tab.IndentWidth = 8;
16821 verifyFormat("/*\n"
16822 "\t a\t\tcomment\n"
16823 "\t in multiple lines\n"
16824 " */",
16825 " /*\t \t \n"
16826 " \t \t a\t\tcomment\t \t\n"
16827 " \t \t in multiple lines\t\n"
16828 " \t */",
16829 Tab);
16830 verifyFormat("{\n"
16831 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16832 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16833 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16834 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16835 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16836 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16837 "};",
16838 Tab);
16839 verifyFormat("enum AA {\n"
16840 "\ta1, // Force multiple lines\n"
16841 "\ta2,\n"
16842 "\ta3\n"
16843 "};",
16844 Tab);
16845 verifyFormat("if (aaaaaaaa && // q\n"
16846 " bb) // w\n"
16847 "\t;",
16848 "if (aaaaaaaa &&// q\n"
16849 "bb)// w\n"
16850 ";",
16851 Tab);
16852 verifyFormat("class X {\n"
16853 "\tvoid f() {\n"
16854 "\t\tsomeFunction(parameter1,\n"
16855 "\t\t\t parameter2);\n"
16856 "\t}\n"
16857 "};",
16858 Tab);
16859 verifyFormat("{\n"
16860 "\tQ(\n"
16861 "\t {\n"
16862 "\t\t int a;\n"
16863 "\t\t someFunction(aaaaaaaa,\n"
16864 "\t\t\t\t bbbbbbb);\n"
16865 "\t },\n"
16866 "\t p);\n"
16867 "}",
16868 Tab);
16869 verifyFormat("{\n"
16870 "\t/* aaaa\n"
16871 "\t bbbb */\n"
16872 "}",
16873 "{\n"
16874 "/* aaaa\n"
16875 " bbbb */\n"
16876 "}",
16877 Tab);
16878 verifyFormat("{\n"
16879 "\t/*\n"
16880 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16881 "\t bbbbbbbbbbbbb\n"
16882 "\t*/\n"
16883 "}",
16884 "{\n"
16885 "/*\n"
16886 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16887 "*/\n"
16888 "}",
16889 Tab);
16890 verifyFormat("{\n"
16891 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16892 "\t// bbbbbbbbbbbbb\n"
16893 "}",
16894 "{\n"
16895 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16896 "}",
16897 Tab);
16898 verifyFormat("{\n"
16899 "\t/*\n"
16900 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16901 "\t bbbbbbbbbbbbb\n"
16902 "\t*/\n"
16903 "}",
16904 "{\n"
16905 "\t/*\n"
16906 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16907 "\t*/\n"
16908 "}",
16909 Tab);
16910 verifyNoChange("{\n"
16911 "\t/*\n"
16912 "\n"
16913 "\t*/\n"
16914 "}",
16915 Tab);
16916 verifyNoChange("{\n"
16917 "\t/*\n"
16918 " asdf\n"
16919 "\t*/\n"
16920 "}",
16921 Tab);
16922 verifyFormat("/* some\n"
16923 " comment */",
16924 " \t \t /* some\n"
16925 " \t \t comment */",
16926 Tab);
16927 verifyFormat("int a; /* some\n"
16928 " comment */",
16929 " \t \t int a; /* some\n"
16930 " \t \t comment */",
16931 Tab);
16932 verifyFormat("int a; /* some\n"
16933 "comment */",
16934 " \t \t int\ta; /* some\n"
16935 " \t \t comment */",
16936 Tab);
16937 verifyFormat("f(\"\t\t\"); /* some\n"
16938 " comment */",
16939 " \t \t f(\"\t\t\"); /* some\n"
16940 " \t \t comment */",
16941 Tab);
16942 verifyFormat("{\n"
16943 "\t/*\n"
16944 "\t * Comment\n"
16945 "\t */\n"
16946 "\tint i;\n"
16947 "}",
16948 "{\n"
16949 "\t/*\n"
16950 "\t * Comment\n"
16951 "\t */\n"
16952 "\t int i;\n"
16953 "}",
16954 Tab);
16955 Tab.TabWidth = 2;
16956 Tab.IndentWidth = 2;
16957 verifyFormat("{\n"
16958 "\t/* aaaa\n"
16959 "\t\t bbbb */\n"
16960 "}",
16961 "{\n"
16962 "/* aaaa\n"
16963 "\t bbbb */\n"
16964 "}",
16965 Tab);
16966 verifyFormat("{\n"
16967 "\t/*\n"
16968 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16969 "\t\tbbbbbbbbbbbbb\n"
16970 "\t*/\n"
16971 "}",
16972 "{\n"
16973 "/*\n"
16974 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16975 "*/\n"
16976 "}",
16977 Tab);
16978 Tab.AlignConsecutiveAssignments.Enabled = true;
16979 Tab.AlignConsecutiveDeclarations.Enabled = true;
16980 Tab.TabWidth = 4;
16981 Tab.IndentWidth = 4;
16982 verifyFormat("class Assign {\n"
16983 "\tvoid f() {\n"
16984 "\t\tint x = 123;\n"
16985 "\t\tint random = 4;\n"
16986 "\t\tstd::string alphabet =\n"
16987 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16988 "\t}\n"
16989 "};",
16990 Tab);
16991
16992 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
16993 Tab.TabWidth = 8;
16994 Tab.IndentWidth = 8;
16995 verifyFormat("if (aaaaaaaa && // q\n"
16996 " bb) // w\n"
16997 "\t;",
16998 "if (aaaaaaaa &&// q\n"
16999 "bb)// w\n"
17000 ";",
17001 Tab);
17002 verifyFormat("if (aaa && bbb) // w\n"
17003 "\t;",
17004 "if(aaa&&bbb)// w\n"
17005 ";",
17006 Tab);
17007 verifyFormat("class X {\n"
17008 "\tvoid f() {\n"
17009 "\t\tsomeFunction(parameter1,\n"
17010 "\t\t parameter2);\n"
17011 "\t}\n"
17012 "};",
17013 Tab);
17014 verifyFormat("#define A \\\n"
17015 "\tvoid f() { \\\n"
17016 "\t\tsomeFunction( \\\n"
17017 "\t\t parameter1, \\\n"
17018 "\t\t parameter2); \\\n"
17019 "\t}",
17020 Tab);
17021 Tab.TabWidth = 4;
17022 Tab.IndentWidth = 8;
17023 verifyFormat("class TabWidth4Indent8 {\n"
17024 "\t\tvoid f() {\n"
17025 "\t\t\t\tsomeFunction(parameter1,\n"
17026 "\t\t\t\t parameter2);\n"
17027 "\t\t}\n"
17028 "};",
17029 Tab);
17030 Tab.TabWidth = 4;
17031 Tab.IndentWidth = 4;
17032 verifyFormat("class TabWidth4Indent4 {\n"
17033 "\tvoid f() {\n"
17034 "\t\tsomeFunction(parameter1,\n"
17035 "\t\t parameter2);\n"
17036 "\t}\n"
17037 "};",
17038 Tab);
17039 Tab.TabWidth = 8;
17040 Tab.IndentWidth = 4;
17041 verifyFormat("class TabWidth8Indent4 {\n"
17042 " void f() {\n"
17043 "\tsomeFunction(parameter1,\n"
17044 "\t parameter2);\n"
17045 " }\n"
17046 "};",
17047 Tab);
17048 Tab.TabWidth = 8;
17049 Tab.IndentWidth = 8;
17050 verifyFormat("/*\n"
17051 " a\t\tcomment\n"
17052 " in multiple lines\n"
17053 " */",
17054 " /*\t \t \n"
17055 " \t \t a\t\tcomment\t \t\n"
17056 " \t \t in multiple lines\t\n"
17057 " \t */",
17058 Tab);
17059 verifyFormat("{\n"
17060 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
17061 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
17062 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
17063 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
17064 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
17065 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
17066 "};",
17067 Tab);
17068 verifyFormat("enum AA {\n"
17069 "\ta1, // Force multiple lines\n"
17070 "\ta2,\n"
17071 "\ta3\n"
17072 "};",
17073 Tab);
17074 verifyFormat("if (aaaaaaaa && // q\n"
17075 " bb) // w\n"
17076 "\t;",
17077 "if (aaaaaaaa &&// q\n"
17078 "bb)// w\n"
17079 ";",
17080 Tab);
17081 verifyFormat("class X {\n"
17082 "\tvoid f() {\n"
17083 "\t\tsomeFunction(parameter1,\n"
17084 "\t\t parameter2);\n"
17085 "\t}\n"
17086 "};",
17087 Tab);
17088 verifyFormat("{\n"
17089 "\tQ(\n"
17090 "\t {\n"
17091 "\t\t int a;\n"
17092 "\t\t someFunction(aaaaaaaa,\n"
17093 "\t\t bbbbbbb);\n"
17094 "\t },\n"
17095 "\t p);\n"
17096 "}",
17097 Tab);
17098 verifyFormat("{\n"
17099 "\t/* aaaa\n"
17100 "\t bbbb */\n"
17101 "}",
17102 "{\n"
17103 "/* aaaa\n"
17104 " bbbb */\n"
17105 "}",
17106 Tab);
17107 verifyFormat("{\n"
17108 "\t/*\n"
17109 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
17110 "\t bbbbbbbbbbbbb\n"
17111 "\t*/\n"
17112 "}",
17113 "{\n"
17114 "/*\n"
17115 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
17116 "*/\n"
17117 "}",
17118 Tab);
17119 verifyFormat("{\n"
17120 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
17121 "\t// bbbbbbbbbbbbb\n"
17122 "}",
17123 "{\n"
17124 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
17125 "}",
17126 Tab);
17127 verifyFormat("{\n"
17128 "\t/*\n"
17129 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
17130 "\t bbbbbbbbbbbbb\n"
17131 "\t*/\n"
17132 "}",
17133 "{\n"
17134 "\t/*\n"
17135 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
17136 "\t*/\n"
17137 "}",
17138 Tab);
17139 verifyNoChange("{\n"
17140 "\t/*\n"
17141 "\n"
17142 "\t*/\n"
17143 "}",
17144 Tab);
17145 verifyNoChange("{\n"
17146 "\t/*\n"
17147 " asdf\n"
17148 "\t*/\n"
17149 "}",
17150 Tab);
17151 verifyFormat("/* some\n"
17152 " comment */",
17153 " \t \t /* some\n"
17154 " \t \t comment */",
17155 Tab);
17156 verifyFormat("int a; /* some\n"
17157 " comment */",
17158 " \t \t int a; /* some\n"
17159 " \t \t comment */",
17160 Tab);
17161 verifyFormat("int a; /* some\n"
17162 "comment */",
17163 " \t \t int\ta; /* some\n"
17164 " \t \t comment */",
17165 Tab);
17166 verifyFormat("f(\"\t\t\"); /* some\n"
17167 " comment */",
17168 " \t \t f(\"\t\t\"); /* some\n"
17169 " \t \t comment */",
17170 Tab);
17171 verifyFormat("{\n"
17172 "\t/*\n"
17173 "\t * Comment\n"
17174 "\t */\n"
17175 "\tint i;\n"
17176 "}",
17177 "{\n"
17178 "\t/*\n"
17179 "\t * Comment\n"
17180 "\t */\n"
17181 "\t int i;\n"
17182 "}",
17183 Tab);
17184 Tab.TabWidth = 2;
17185 Tab.IndentWidth = 2;
17186 verifyFormat("{\n"
17187 "\t/* aaaa\n"
17188 "\t bbbb */\n"
17189 "}",
17190 "{\n"
17191 "/* aaaa\n"
17192 " bbbb */\n"
17193 "}",
17194 Tab);
17195 verifyFormat("{\n"
17196 "\t/*\n"
17197 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
17198 "\t bbbbbbbbbbbbb\n"
17199 "\t*/\n"
17200 "}",
17201 "{\n"
17202 "/*\n"
17203 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
17204 "*/\n"
17205 "}",
17206 Tab);
17207 Tab.AlignConsecutiveAssignments.Enabled = true;
17208 Tab.AlignConsecutiveDeclarations.Enabled = true;
17209 Tab.TabWidth = 4;
17210 Tab.IndentWidth = 4;
17211 verifyFormat("class Assign {\n"
17212 "\tvoid f() {\n"
17213 "\t\tint x = 123;\n"
17214 "\t\tint random = 4;\n"
17215 "\t\tstd::string alphabet =\n"
17216 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
17217 "\t}\n"
17218 "};",
17219 Tab);
17220 Tab.AlignOperands = FormatStyle::OAS_Align;
17221 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
17222 " cccccccccccccccccccc;",
17223 Tab);
17224 // no alignment
17225 verifyFormat("int aaaaaaaaaa =\n"
17226 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
17227 Tab);
17228 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
17229 " : bbbbbbbbbbbbbb ? 222222222222222\n"
17230 " : 333333333333333;",
17231 Tab);
17232 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
17233 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator;
17234 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
17235 " + cccccccccccccccccccc;",
17236 Tab);
17237}
17238
17239TEST_F(FormatTest, ZeroTabWidth) {
17240 FormatStyle Tab = getLLVMStyleWithColumns(ColumnLimit: 42);
17241 Tab.IndentWidth = 8;
17242 Tab.UseTab = FormatStyle::UT_Never;
17243 Tab.TabWidth = 0;
17244 verifyFormat("void a() {\n"
17245 " // line starts with '\t'\n"
17246 "};",
17247 "void a(){\n"
17248 "\t// line starts with '\t'\n"
17249 "};",
17250 Tab);
17251
17252 verifyFormat("void a() {\n"
17253 " // line starts with '\t'\n"
17254 "};",
17255 "void a(){\n"
17256 "\t\t// line starts with '\t'\n"
17257 "};",
17258 Tab);
17259
17260 Tab.UseTab = FormatStyle::UT_ForIndentation;
17261 verifyFormat("void a() {\n"
17262 " // line starts with '\t'\n"
17263 "};",
17264 "void a(){\n"
17265 "\t// line starts with '\t'\n"
17266 "};",
17267 Tab);
17268
17269 verifyFormat("void a() {\n"
17270 " // line starts with '\t'\n"
17271 "};",
17272 "void a(){\n"
17273 "\t\t// line starts with '\t'\n"
17274 "};",
17275 Tab);
17276
17277 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
17278 verifyFormat("void a() {\n"
17279 " // line starts with '\t'\n"
17280 "};",
17281 "void a(){\n"
17282 "\t// line starts with '\t'\n"
17283 "};",
17284 Tab);
17285
17286 verifyFormat("void a() {\n"
17287 " // line starts with '\t'\n"
17288 "};",
17289 "void a(){\n"
17290 "\t\t// line starts with '\t'\n"
17291 "};",
17292 Tab);
17293
17294 Tab.UseTab = FormatStyle::UT_AlignWithSpaces;
17295 verifyFormat("void a() {\n"
17296 " // line starts with '\t'\n"
17297 "};",
17298 "void a(){\n"
17299 "\t// line starts with '\t'\n"
17300 "};",
17301 Tab);
17302
17303 verifyFormat("void a() {\n"
17304 " // line starts with '\t'\n"
17305 "};",
17306 "void a(){\n"
17307 "\t\t// line starts with '\t'\n"
17308 "};",
17309 Tab);
17310
17311 Tab.UseTab = FormatStyle::UT_Always;
17312 verifyFormat("void a() {\n"
17313 "// line starts with '\t'\n"
17314 "};",
17315 "void a(){\n"
17316 "\t// line starts with '\t'\n"
17317 "};",
17318 Tab);
17319
17320 verifyFormat("void a() {\n"
17321 "// line starts with '\t'\n"
17322 "};",
17323 "void a(){\n"
17324 "\t\t// line starts with '\t'\n"
17325 "};",
17326 Tab);
17327}
17328
17329TEST_F(FormatTest, CalculatesOriginalColumn) {
17330 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17331 "q\"; /* some\n"
17332 " comment */",
17333 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17334 "q\"; /* some\n"
17335 " comment */");
17336 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
17337 "/* some\n"
17338 " comment */",
17339 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
17340 " /* some\n"
17341 " comment */");
17342 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17343 "qqq\n"
17344 "/* some\n"
17345 " comment */",
17346 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17347 "qqq\n"
17348 " /* some\n"
17349 " comment */");
17350 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17351 "wwww; /* some\n"
17352 " comment */",
17353 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
17354 "wwww; /* some\n"
17355 " comment */");
17356}
17357
17358TEST_F(FormatTest, SpaceAfterOperatorKeyword) {
17359 auto SpaceAfterOperatorKeyword = getLLVMStyle();
17360 SpaceAfterOperatorKeyword.SpaceAfterOperatorKeyword = true;
17361 verifyFormat("bool operator ++(int a);", SpaceAfterOperatorKeyword);
17362}
17363
17364TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
17365 FormatStyle NoSpace = getLLVMStyle();
17366 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
17367
17368 verifyFormat("while(true)\n"
17369 " continue;",
17370 NoSpace);
17371 verifyFormat("for(;;)\n"
17372 " continue;",
17373 NoSpace);
17374 verifyFormat("if(true)\n"
17375 " f();\n"
17376 "else if(true)\n"
17377 " f();",
17378 NoSpace);
17379 verifyFormat("do {\n"
17380 " do_something();\n"
17381 "} while(something());",
17382 NoSpace);
17383 verifyFormat("switch(x) {\n"
17384 "default:\n"
17385 " break;\n"
17386 "}",
17387 NoSpace);
17388 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
17389 verifyFormat("size_t x = sizeof(x);", NoSpace);
17390 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
17391 verifyFormat("auto f(int x) -> typeof(x);", NoSpace);
17392 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace);
17393 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace);
17394 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
17395 verifyFormat("alignas(128) char a[128];", NoSpace);
17396 verifyFormat("size_t x = alignof(MyType);", NoSpace);
17397 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
17398 verifyFormat("int f() throw(Deprecated);", NoSpace);
17399 verifyFormat("typedef void (*cb)(int);", NoSpace);
17400 verifyFormat("T A::operator()();", NoSpace);
17401 verifyFormat("X A::operator++(T);", NoSpace);
17402 verifyFormat("auto lambda = []() { return 0; };", NoSpace);
17403 verifyFormat("#if (foo || bar) && baz\n"
17404 "#elif ((a || b) && c) || d\n"
17405 "#endif",
17406 NoSpace);
17407
17408 FormatStyle Space = getLLVMStyle();
17409 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
17410
17411 verifyFormat("int f ();", Space);
17412 verifyFormat("bool operator< ();", Space);
17413 verifyFormat("bool operator> ();", Space);
17414 verifyFormat("void f (int a, T b) {\n"
17415 " while (true)\n"
17416 " continue;\n"
17417 "}",
17418 Space);
17419 verifyFormat("if (true)\n"
17420 " f ();\n"
17421 "else if (true)\n"
17422 " f ();",
17423 Space);
17424 verifyFormat("do {\n"
17425 " do_something ();\n"
17426 "} while (something ());",
17427 Space);
17428 verifyFormat("switch (x) {\n"
17429 "default:\n"
17430 " break;\n"
17431 "}",
17432 Space);
17433 verifyFormat("A::A () : a (1) {}", Space);
17434 verifyFormat("void f () __attribute__ ((asdf));", Space);
17435 verifyFormat("*(&a + 1);\n"
17436 "&((&a)[1]);\n"
17437 "a[(b + c) * d];\n"
17438 "(((a + 1) * 2) + 3) * 4;",
17439 Space);
17440 verifyFormat("#define A(x) x", Space);
17441 verifyFormat("#define A (x) x", Space);
17442 verifyFormat("#if defined(x)\n"
17443 "#endif",
17444 Space);
17445 verifyFormat("auto i = std::make_unique<int> (5);", Space);
17446 verifyFormat("size_t x = sizeof (x);", Space);
17447 verifyFormat("auto f (int x) -> decltype (x);", Space);
17448 verifyFormat("auto f (int x) -> typeof (x);", Space);
17449 verifyFormat("auto f (int x) -> _Atomic (x);", Space);
17450 verifyFormat("auto f (int x) -> __underlying_type (x);", Space);
17451 verifyFormat("int f (T x) noexcept (x.create ());", Space);
17452 verifyFormat("alignas (128) char a[128];", Space);
17453 verifyFormat("size_t x = alignof (MyType);", Space);
17454 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
17455 verifyFormat("int f () throw (Deprecated);", Space);
17456 verifyFormat("typedef void (*cb) (int);", Space);
17457 verifyFormat("T A::operator() ();", Space);
17458 verifyFormat("X A::operator++ (T);", Space);
17459 verifyFormat("auto lambda = [] () { return 0; };", Space);
17460 verifyFormat("int x = int (y);", Space);
17461 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space);
17462 verifyFormat("__builtin_LINE ()", Space);
17463 verifyFormat("__builtin_UNKNOWN ()", Space);
17464
17465 FormatStyle SomeSpace = getLLVMStyle();
17466 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses;
17467
17468 verifyFormat("[]() -> float {}", SomeSpace);
17469 verifyFormat("[] (auto foo) {}", SomeSpace);
17470 verifyFormat("[foo]() -> int {}", SomeSpace);
17471 verifyFormat("int f();", SomeSpace);
17472 verifyFormat("void f (int a, T b) {\n"
17473 " while (true)\n"
17474 " continue;\n"
17475 "}",
17476 SomeSpace);
17477 verifyFormat("if (true)\n"
17478 " f();\n"
17479 "else if (true)\n"
17480 " f();",
17481 SomeSpace);
17482 verifyFormat("do {\n"
17483 " do_something();\n"
17484 "} while (something());",
17485 SomeSpace);
17486 verifyFormat("switch (x) {\n"
17487 "default:\n"
17488 " break;\n"
17489 "}",
17490 SomeSpace);
17491 verifyFormat("A::A() : a (1) {}", SomeSpace);
17492 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace);
17493 verifyFormat("*(&a + 1);\n"
17494 "&((&a)[1]);\n"
17495 "a[(b + c) * d];\n"
17496 "(((a + 1) * 2) + 3) * 4;",
17497 SomeSpace);
17498 verifyFormat("#define A(x) x", SomeSpace);
17499 verifyFormat("#define A (x) x", SomeSpace);
17500 verifyFormat("#if defined(x)\n"
17501 "#endif",
17502 SomeSpace);
17503 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace);
17504 verifyFormat("size_t x = sizeof (x);", SomeSpace);
17505 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace);
17506 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace);
17507 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace);
17508 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace);
17509 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace);
17510 verifyFormat("alignas (128) char a[128];", SomeSpace);
17511 verifyFormat("size_t x = alignof (MyType);", SomeSpace);
17512 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17513 SomeSpace);
17514 verifyFormat("int f() throw (Deprecated);", SomeSpace);
17515 verifyFormat("typedef void (*cb) (int);", SomeSpace);
17516 verifyFormat("T A::operator()();", SomeSpace);
17517 verifyFormat("X A::operator++ (T);", SomeSpace);
17518 verifyFormat("int x = int (y);", SomeSpace);
17519 verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
17520
17521 FormatStyle SpaceControlStatements = getLLVMStyle();
17522 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17523 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true;
17524
17525 verifyFormat("while (true)\n"
17526 " continue;",
17527 SpaceControlStatements);
17528 verifyFormat("if (true)\n"
17529 " f();\n"
17530 "else if (true)\n"
17531 " f();",
17532 SpaceControlStatements);
17533 verifyFormat("for (;;) {\n"
17534 " do_something();\n"
17535 "}",
17536 SpaceControlStatements);
17537 verifyFormat("do {\n"
17538 " do_something();\n"
17539 "} while (something());",
17540 SpaceControlStatements);
17541 verifyFormat("switch (x) {\n"
17542 "default:\n"
17543 " break;\n"
17544 "}",
17545 SpaceControlStatements);
17546
17547 FormatStyle SpaceFuncDecl = getLLVMStyle();
17548 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17549 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true;
17550
17551 verifyFormat("int f ();", SpaceFuncDecl);
17552 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
17553 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
17554 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
17555 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
17556 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
17557 verifyFormat("#define A(x) x", SpaceFuncDecl);
17558 verifyFormat("#define A (x) x", SpaceFuncDecl);
17559 verifyFormat("#if defined(x)\n"
17560 "#endif",
17561 SpaceFuncDecl);
17562 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl);
17563 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl);
17564 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl);
17565 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl);
17566 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl);
17567 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl);
17568 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl);
17569 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl);
17570 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl);
17571 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17572 SpaceFuncDecl);
17573 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl);
17574 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl);
17575 verifyFormat("T A::operator()();", SpaceFuncDecl);
17576 verifyFormat("X A::operator++(T);", SpaceFuncDecl);
17577 verifyFormat("T A::operator()() {}", SpaceFuncDecl);
17578 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl);
17579 verifyFormat("int x = int(y);", SpaceFuncDecl);
17580 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17581 SpaceFuncDecl);
17582
17583 FormatStyle SpaceFuncDef = getLLVMStyle();
17584 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17585 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true;
17586
17587 verifyFormat("int f();", SpaceFuncDef);
17588 verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
17589 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
17590 verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
17591 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
17592 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
17593 verifyFormat("#define A(x) x", SpaceFuncDef);
17594 verifyFormat("#define A (x) x", SpaceFuncDef);
17595 verifyFormat("#if defined(x)\n"
17596 "#endif",
17597 SpaceFuncDef);
17598 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef);
17599 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef);
17600 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef);
17601 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef);
17602 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef);
17603 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef);
17604 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef);
17605 verifyFormat("alignas(128) char a[128];", SpaceFuncDef);
17606 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef);
17607 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17608 SpaceFuncDef);
17609 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef);
17610 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef);
17611 verifyFormat("T A::operator()();", SpaceFuncDef);
17612 verifyFormat("X A::operator++(T);", SpaceFuncDef);
17613 verifyFormat("T A::operator()() {}", SpaceFuncDef);
17614 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
17615 verifyFormat("int x = int(y);", SpaceFuncDef);
17616 verifyFormat("void foo::bar () {}", SpaceFuncDef);
17617 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17618 SpaceFuncDef);
17619
17620 FormatStyle SpaceIfMacros = getLLVMStyle();
17621 SpaceIfMacros.IfMacros.clear();
17622 SpaceIfMacros.IfMacros.push_back(x: "MYIF");
17623 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17624 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true;
17625 verifyFormat("MYIF (a)\n return;", SpaceIfMacros);
17626 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros);
17627 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros);
17628
17629 FormatStyle SpaceForeachMacros = getLLVMStyle();
17630 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine,
17631 FormatStyle::SBS_Never);
17632 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false);
17633 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17634 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true;
17635 verifyFormat("for (;;) {\n"
17636 "}",
17637 SpaceForeachMacros);
17638 verifyFormat("foreach (Item *item, itemlist) {\n"
17639 "}",
17640 SpaceForeachMacros);
17641 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17642 "}",
17643 SpaceForeachMacros);
17644 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17645 "}",
17646 SpaceForeachMacros);
17647 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros);
17648
17649 FormatStyle SomeSpace2 = getLLVMStyle();
17650 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17651 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
17652 verifyFormat("[]() -> float {}", SomeSpace2);
17653 verifyFormat("[] (auto foo) {}", SomeSpace2);
17654 verifyFormat("[foo]() -> int {}", SomeSpace2);
17655 verifyFormat("int f();", SomeSpace2);
17656 verifyFormat("void f (int a, T b) {\n"
17657 " while (true)\n"
17658 " continue;\n"
17659 "}",
17660 SomeSpace2);
17661 verifyFormat("if (true)\n"
17662 " f();\n"
17663 "else if (true)\n"
17664 " f();",
17665 SomeSpace2);
17666 verifyFormat("do {\n"
17667 " do_something();\n"
17668 "} while (something());",
17669 SomeSpace2);
17670 verifyFormat("switch (x) {\n"
17671 "default:\n"
17672 " break;\n"
17673 "}",
17674 SomeSpace2);
17675 verifyFormat("A::A() : a (1) {}", SomeSpace2);
17676 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2);
17677 verifyFormat("*(&a + 1);\n"
17678 "&((&a)[1]);\n"
17679 "a[(b + c) * d];\n"
17680 "(((a + 1) * 2) + 3) * 4;",
17681 SomeSpace2);
17682 verifyFormat("#define A(x) x", SomeSpace2);
17683 verifyFormat("#define A (x) x", SomeSpace2);
17684 verifyFormat("#if defined(x)\n"
17685 "#endif",
17686 SomeSpace2);
17687 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2);
17688 verifyFormat("size_t x = sizeof (x);", SomeSpace2);
17689 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2);
17690 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2);
17691 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2);
17692 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2);
17693 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2);
17694 verifyFormat("alignas (128) char a[128];", SomeSpace2);
17695 verifyFormat("size_t x = alignof (MyType);", SomeSpace2);
17696 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17697 SomeSpace2);
17698 verifyFormat("int f() throw (Deprecated);", SomeSpace2);
17699 verifyFormat("typedef void (*cb) (int);", SomeSpace2);
17700 verifyFormat("T A::operator()();", SomeSpace2);
17701 verifyFormat("X A::operator++ (T);", SomeSpace2);
17702 verifyFormat("int x = int (y);", SomeSpace2);
17703 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
17704
17705 auto Style = getLLVMStyle();
17706 Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17707 EXPECT_FALSE(Style.SpaceBeforeParensOptions.AfterNot);
17708 Style.SpaceBeforeParensOptions.AfterNot = true;
17709 verifyFormat("return not (a || b);", Style);
17710
17711 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
17712 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17713 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17714 .AfterOverloadedOperator = true;
17715
17716 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator);
17717 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator);
17718 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator);
17719 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17720
17721 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
17722 .AfterOverloadedOperator = false;
17723
17724 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator);
17725 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator);
17726 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator);
17727 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator);
17728
17729 auto SpaceAfterRequires = getLLVMStyle();
17730 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17731 EXPECT_FALSE(
17732 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause);
17733 EXPECT_FALSE(
17734 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression);
17735 verifyFormat("void f(auto x)\n"
17736 " requires requires(int i) { x + i; }\n"
17737 "{}",
17738 SpaceAfterRequires);
17739 verifyFormat("void f(auto x)\n"
17740 " requires(requires(int i) { x + i; })\n"
17741 "{}",
17742 SpaceAfterRequires);
17743 verifyFormat("if (requires(int i) { x + i; })\n"
17744 " return;",
17745 SpaceAfterRequires);
17746 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17747 verifyFormat("template <typename T>\n"
17748 " requires(Foo<T>)\n"
17749 "class Bar;",
17750 SpaceAfterRequires);
17751
17752 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17753 verifyFormat("void f(auto x)\n"
17754 " requires requires(int i) { x + i; }\n"
17755 "{}",
17756 SpaceAfterRequires);
17757 verifyFormat("void f(auto x)\n"
17758 " requires (requires(int i) { x + i; })\n"
17759 "{}",
17760 SpaceAfterRequires);
17761 verifyFormat("if (requires(int i) { x + i; })\n"
17762 " return;",
17763 SpaceAfterRequires);
17764 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires);
17765 verifyFormat("template <typename T>\n"
17766 " requires (Foo<T>)\n"
17767 "class Bar;",
17768 SpaceAfterRequires);
17769
17770 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false;
17771 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true;
17772 verifyFormat("void f(auto x)\n"
17773 " requires requires (int i) { x + i; }\n"
17774 "{}",
17775 SpaceAfterRequires);
17776 verifyFormat("void f(auto x)\n"
17777 " requires(requires (int i) { x + i; })\n"
17778 "{}",
17779 SpaceAfterRequires);
17780 verifyFormat("if (requires (int i) { x + i; })\n"
17781 " return;",
17782 SpaceAfterRequires);
17783 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17784 verifyFormat("template <typename T>\n"
17785 " requires(Foo<T>)\n"
17786 "class Bar;",
17787 SpaceAfterRequires);
17788
17789 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true;
17790 verifyFormat("void f(auto x)\n"
17791 " requires requires (int i) { x + i; }\n"
17792 "{}",
17793 SpaceAfterRequires);
17794 verifyFormat("void f(auto x)\n"
17795 " requires (requires (int i) { x + i; })\n"
17796 "{}",
17797 SpaceAfterRequires);
17798 verifyFormat("if (requires (int i) { x + i; })\n"
17799 " return;",
17800 SpaceAfterRequires);
17801 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires);
17802 verifyFormat("template <typename T>\n"
17803 " requires (Foo<T>)\n"
17804 "class Bar;",
17805 SpaceAfterRequires);
17806}
17807
17808TEST_F(FormatTest, SpaceAfterLogicalNot) {
17809 FormatStyle Spaces = getLLVMStyle();
17810 Spaces.SpaceAfterLogicalNot = true;
17811
17812 verifyFormat("bool x = ! y", Spaces);
17813 verifyFormat("if (! isFailure())", Spaces);
17814 verifyFormat("if (! (a && b))", Spaces);
17815 verifyFormat("\"Error!\"", Spaces);
17816 verifyFormat("! ! x", Spaces);
17817}
17818
17819TEST_F(FormatTest, ConfigurableSpacesInParens) {
17820 FormatStyle Spaces = getLLVMStyle();
17821
17822 verifyFormat("do_something(::globalVar);", Spaces);
17823 verifyFormat("call(x, y, z);", Spaces);
17824 verifyFormat("call();", Spaces);
17825 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17826 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17827 Spaces);
17828 verifyFormat("while ((bool)1)\n"
17829 " continue;",
17830 Spaces);
17831 verifyFormat("for (;;)\n"
17832 " continue;",
17833 Spaces);
17834 verifyFormat("if (true)\n"
17835 " f();\n"
17836 "else if (true)\n"
17837 " f();",
17838 Spaces);
17839 verifyFormat("do {\n"
17840 " do_something((int)i);\n"
17841 "} while (something());",
17842 Spaces);
17843 verifyFormat("switch (x) {\n"
17844 "default:\n"
17845 " break;\n"
17846 "}",
17847 Spaces);
17848 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17849 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17850 verifyFormat("void f() __attribute__((asdf));", Spaces);
17851 verifyFormat("x = (int32)y;", Spaces);
17852 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces);
17853 verifyFormat("decltype(x) y = 42;", Spaces);
17854 verifyFormat("decltype((x)) y = z;", Spaces);
17855 verifyFormat("decltype((foo())) a = foo();", Spaces);
17856 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17857 verifyFormat("if ((x - y) && (a ^ b))\n"
17858 " f();",
17859 Spaces);
17860 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17861 " foo(i);",
17862 Spaces);
17863 verifyFormat("switch (x / (y + z)) {\n"
17864 "default:\n"
17865 " break;\n"
17866 "}",
17867 Spaces);
17868
17869 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17870 Spaces.SpacesInParensOptions = {};
17871 Spaces.SpacesInParensOptions.Other = true;
17872
17873 EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
17874 verifyFormat("if (a)\n"
17875 " return;",
17876 Spaces);
17877
17878 Spaces.SpacesInParensOptions.InConditionalStatements = true;
17879 verifyFormat("do_something( ::globalVar );", Spaces);
17880 verifyFormat("call( x, y, z );", Spaces);
17881 verifyFormat("call();", Spaces);
17882 verifyFormat("std::function<void( int, int )> callback;", Spaces);
17883 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17884 Spaces);
17885 verifyFormat("while ( (bool)1 )\n"
17886 " continue;",
17887 Spaces);
17888 verifyFormat("for ( ;; )\n"
17889 " continue;",
17890 Spaces);
17891 verifyFormat("if ( true )\n"
17892 " f();\n"
17893 "else if ( true )\n"
17894 " f();",
17895 Spaces);
17896 verifyFormat("do {\n"
17897 " do_something( (int)i );\n"
17898 "} while ( something() );",
17899 Spaces);
17900 verifyFormat("switch ( x ) {\n"
17901 "default:\n"
17902 " break;\n"
17903 "}",
17904 Spaces);
17905 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
17906 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
17907 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
17908 verifyFormat("x = (int32)y;", Spaces);
17909 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces);
17910 verifyFormat("decltype( x ) y = 42;", Spaces);
17911 verifyFormat("decltype( ( x ) ) y = z;", Spaces);
17912 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces);
17913 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces);
17914 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17915 " f();",
17916 Spaces);
17917 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17918 " foo( i );",
17919 Spaces);
17920 verifyFormat("switch ( x / ( y + z ) ) {\n"
17921 "default:\n"
17922 " break;\n"
17923 "}",
17924 Spaces);
17925
17926 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17927 Spaces.SpacesInParensOptions = {};
17928 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17929 verifyFormat("Type *A = ( Type * )P;", Spaces);
17930 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
17931 verifyFormat("x = ( int32 )y;", Spaces);
17932 verifyFormat("throw ( int32 )x;", Spaces);
17933 verifyFormat("int a = ( int )(2.0f);", Spaces);
17934 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
17935 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
17936 verifyFormat("#define x (( int )-1)", Spaces);
17937 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17938
17939 // Run the first set of tests again with:
17940 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
17941 Spaces.SpacesInParensOptions = {};
17942 Spaces.SpacesInParensOptions.InEmptyParentheses = true;
17943 Spaces.SpacesInParensOptions.InCStyleCasts = true;
17944 verifyFormat("call(x, y, z);", Spaces);
17945 verifyFormat("call( );", Spaces);
17946 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17947 verifyFormat("while (( bool )1)\n"
17948 " continue;",
17949 Spaces);
17950 verifyFormat("for (;;)\n"
17951 " continue;",
17952 Spaces);
17953 verifyFormat("if (true)\n"
17954 " f( );\n"
17955 "else if (true)\n"
17956 " f( );",
17957 Spaces);
17958 verifyFormat("do {\n"
17959 " do_something(( int )i);\n"
17960 "} while (something( ));",
17961 Spaces);
17962 verifyFormat("switch (x) {\n"
17963 "default:\n"
17964 " break;\n"
17965 "}",
17966 Spaces);
17967 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
17968 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
17969 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
17970 verifyFormat("x = ( int32 )y;", Spaces);
17971 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces);
17972 verifyFormat("decltype(x) y = 42;", Spaces);
17973 verifyFormat("decltype((x)) y = z;", Spaces);
17974 verifyFormat("decltype((foo( ))) a = foo( );", Spaces);
17975 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces);
17976 verifyFormat("if ((x - y) && (a ^ b))\n"
17977 " f( );",
17978 Spaces);
17979 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17980 " foo(i);",
17981 Spaces);
17982 verifyFormat("switch (x / (y + z)) {\n"
17983 "default:\n"
17984 " break;\n"
17985 "}",
17986 Spaces);
17987
17988 // Run the first set of tests again with:
17989 Spaces.SpaceAfterCStyleCast = true;
17990 verifyFormat("call(x, y, z);", Spaces);
17991 verifyFormat("call( );", Spaces);
17992 verifyFormat("std::function<void(int, int)> callback;", Spaces);
17993 verifyFormat("while (( bool ) 1)\n"
17994 " continue;",
17995 Spaces);
17996 verifyFormat("for (;;)\n"
17997 " continue;",
17998 Spaces);
17999 verifyFormat("if (true)\n"
18000 " f( );\n"
18001 "else if (true)\n"
18002 " f( );",
18003 Spaces);
18004 verifyFormat("do {\n"
18005 " do_something(( int ) i);\n"
18006 "} while (something( ));",
18007 Spaces);
18008 verifyFormat("switch (x) {\n"
18009 "default:\n"
18010 " break;\n"
18011 "}",
18012 Spaces);
18013 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces);
18014 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces);
18015 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces);
18016 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
18017 verifyFormat("bool *y = ( bool * ) (x);", Spaces);
18018 verifyFormat("throw ( int32 ) x;", Spaces);
18019 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
18020 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
18021 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
18022
18023 // Run subset of tests again with:
18024 Spaces.SpacesInParensOptions.InCStyleCasts = false;
18025 Spaces.SpaceAfterCStyleCast = true;
18026 verifyFormat("while ((bool) 1)\n"
18027 " continue;",
18028 Spaces);
18029 verifyFormat("do {\n"
18030 " do_something((int) i);\n"
18031 "} while (something( ));",
18032 Spaces);
18033
18034 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
18035 verifyFormat("size_t idx = (size_t) a;", Spaces);
18036 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
18037 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
18038 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
18039 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
18040 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
18041 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces);
18042 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces);
18043 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces);
18044 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
18045 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
18046 verifyFormat("throw (int32) x;", Spaces);
18047 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
18048 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
18049 verifyFormat("void f( ) __attribute__((asdf));", Spaces);
18050
18051 Spaces.ColumnLimit = 80;
18052 Spaces.IndentWidth = 4;
18053 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
18054 verifyFormat("void foo( ) {\n"
18055 " size_t foo = (*(function))(\n"
18056 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
18057 "BarrrrrrrrrrrrLong,\n"
18058 " FoooooooooLooooong);\n"
18059 "}",
18060 Spaces);
18061 Spaces.SpaceAfterCStyleCast = false;
18062 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
18063 verifyFormat("size_t idx = (size_t)a;", Spaces);
18064 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
18065 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
18066 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
18067 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
18068 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
18069
18070 verifyFormat("void foo( ) {\n"
18071 " size_t foo = (*(function))(\n"
18072 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
18073 "BarrrrrrrrrrrrLong,\n"
18074 " FoooooooooLooooong);\n"
18075 "}",
18076 Spaces);
18077
18078 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
18079 verifyFormat("void foo( ) {\n"
18080 " size_t foo = (*(function))(\n"
18081 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
18082 "BarrrrrrrrrrrrLong,\n"
18083 " FoooooooooLooooong\n"
18084 " );\n"
18085 "}",
18086 Spaces);
18087 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
18088 verifyFormat("size_t idx = (size_t)a;", Spaces);
18089 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
18090 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
18091 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
18092 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
18093 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces);
18094
18095 // Check ExceptDoubleParentheses spaces
18096 Spaces.IndentWidth = 2;
18097 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
18098 Spaces.SpacesInParensOptions = {};
18099 Spaces.SpacesInParensOptions.Other = true;
18100 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
18101 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces);
18102 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces);
18103 verifyFormat("void f() __attribute__(( asdf ));", Spaces);
18104 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces);
18105 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces);
18106 verifyFormat("class __declspec( dllimport ) X {};", Spaces);
18107 verifyFormat("class __declspec(( dllimport )) X {};", Spaces);
18108 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces);
18109 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces);
18110 verifyFormat("decltype( x ) y = 42;", Spaces);
18111 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces);
18112 verifyFormat("if (( i = j ))\n"
18113 " do_something( i );",
18114 Spaces);
18115
18116 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
18117 Spaces.SpacesInParensOptions = {};
18118 Spaces.SpacesInParensOptions.InConditionalStatements = true;
18119 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true;
18120 verifyFormat("while ( (bool)1 )\n"
18121 " continue;",
18122 Spaces);
18123 verifyFormat("while ((i = j))\n"
18124 " continue;",
18125 Spaces);
18126 verifyFormat("do {\n"
18127 " do_something((int)i);\n"
18128 "} while ( something() );",
18129 Spaces);
18130 verifyFormat("do {\n"
18131 " do_something((int)i);\n"
18132 "} while ((i = i + 1));",
18133 Spaces);
18134 verifyFormat("if ( (x - y) && (a ^ b) )\n"
18135 " f();",
18136 Spaces);
18137 verifyFormat("if ((i = j))\n"
18138 " do_something(i);",
18139 Spaces);
18140 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
18141 " foo(i);",
18142 Spaces);
18143 verifyFormat("switch ( x / (y + z) ) {\n"
18144 "default:\n"
18145 " break;\n"
18146 "}",
18147 Spaces);
18148 verifyFormat("if constexpr ((a = b))\n"
18149 " c;",
18150 Spaces);
18151}
18152
18153TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
18154 verifyFormat("int a[5];");
18155 verifyFormat("a[3] += 42;");
18156
18157 FormatStyle Spaces = getLLVMStyle();
18158 Spaces.SpacesInSquareBrackets = true;
18159 // Not lambdas.
18160 verifyFormat("int a[ 5 ];", Spaces);
18161 verifyFormat("a[ 3 ] += 42;", Spaces);
18162 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
18163 verifyFormat("double &operator[](int i) { return 0; }\n"
18164 "int i;",
18165 Spaces);
18166 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
18167 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
18168 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
18169 // Lambdas.
18170 verifyFormat("int c = []() -> int { return 2; }();", Spaces);
18171 verifyFormat("return [ i, args... ] {};", Spaces);
18172 verifyFormat("int foo = [ &bar ]() {};", Spaces);
18173 verifyFormat("int foo = [ = ]() {};", Spaces);
18174 verifyFormat("int foo = [ & ]() {};", Spaces);
18175 verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
18176 verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
18177}
18178
18179TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
18180 FormatStyle NoSpaceStyle = getLLVMStyle();
18181 verifyFormat("int a[5];", NoSpaceStyle);
18182 verifyFormat("a[3] += 42;", NoSpaceStyle);
18183
18184 verifyFormat("int a[1];", NoSpaceStyle);
18185 verifyFormat("int 1 [a];", NoSpaceStyle);
18186 verifyFormat("int a[1][2];", NoSpaceStyle);
18187 verifyFormat("a[7] = 5;", NoSpaceStyle);
18188 verifyFormat("int a = (f())[23];", NoSpaceStyle);
18189 verifyFormat("f([] {})", NoSpaceStyle);
18190
18191 FormatStyle Space = getLLVMStyle();
18192 Space.SpaceBeforeSquareBrackets = true;
18193 verifyFormat("int c = []() -> int { return 2; }();", Space);
18194 verifyFormat("return [i, args...] {};", Space);
18195
18196 verifyFormat("int a [5];", Space);
18197 verifyFormat("a [3] += 42;", Space);
18198 verifyFormat("constexpr char hello []{\"hello\"};", Space);
18199 verifyFormat("double &operator[](int i) { return 0; }\n"
18200 "int i;",
18201 Space);
18202 verifyFormat("std::unique_ptr<int []> foo() {}", Space);
18203 verifyFormat("int i = a [a][a]->f();", Space);
18204 verifyFormat("int i = (*b) [a]->f();", Space);
18205
18206 verifyFormat("int a [1];", Space);
18207 verifyFormat("int 1 [a];", Space);
18208 verifyFormat("int a [1][2];", Space);
18209 verifyFormat("a [7] = 5;", Space);
18210 verifyFormat("int a = (f()) [23];", Space);
18211 verifyFormat("f([] {})", Space);
18212}
18213
18214TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
18215 verifyFormat("int a = 5;");
18216 verifyFormat("a += 42;");
18217 verifyFormat("a or_eq 8;");
18218
18219 auto Spaces = getLLVMStyle(Language: FormatStyle::LK_C);
18220 verifyFormat("xor = foo;", Spaces);
18221
18222 Spaces.Language = FormatStyle::LK_Cpp;
18223 Spaces.SpaceBeforeAssignmentOperators = false;
18224 verifyFormat("int a= 5;", Spaces);
18225 verifyFormat("a+= 42;", Spaces);
18226 verifyFormat("a or_eq 8;", Spaces);
18227 verifyFormat("xor= foo;", Spaces);
18228}
18229
18230TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
18231 verifyFormat("class Foo : public Bar {};");
18232 verifyFormat("Foo::Foo() : foo(1) {}");
18233 verifyFormat("for (auto a : b) {\n}");
18234 verifyFormat("int x = a ? b : c;");
18235 verifyFormat("{\n"
18236 "label0:\n"
18237 " int x = 0;\n"
18238 "}");
18239 verifyFormat("switch (x) {\n"
18240 "case 1:\n"
18241 "default:\n"
18242 "}");
18243 verifyFormat("switch (allBraces) {\n"
18244 "case 1: {\n"
18245 " break;\n"
18246 "}\n"
18247 "case 2: {\n"
18248 " [[fallthrough]];\n"
18249 "}\n"
18250 "default: {\n"
18251 " break;\n"
18252 "}\n"
18253 "}");
18254
18255 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(ColumnLimit: 30);
18256 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false;
18257 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle);
18258 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle);
18259 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle);
18260 verifyFormat("int x = a ? b : c;", CtorInitializerStyle);
18261 verifyFormat("{\n"
18262 "label1:\n"
18263 " int x = 0;\n"
18264 "}",
18265 CtorInitializerStyle);
18266 verifyFormat("switch (x) {\n"
18267 "case 1:\n"
18268 "default:\n"
18269 "}",
18270 CtorInitializerStyle);
18271 verifyFormat("switch (allBraces) {\n"
18272 "case 1: {\n"
18273 " break;\n"
18274 "}\n"
18275 "case 2: {\n"
18276 " [[fallthrough]];\n"
18277 "}\n"
18278 "default: {\n"
18279 " break;\n"
18280 "}\n"
18281 "}",
18282 CtorInitializerStyle);
18283 CtorInitializerStyle.BreakConstructorInitializers =
18284 FormatStyle::BCIS_AfterColon;
18285 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
18286 " aaaaaaaaaaaaaaaa(1),\n"
18287 " bbbbbbbbbbbbbbbb(2) {}",
18288 CtorInitializerStyle);
18289 CtorInitializerStyle.BreakConstructorInitializers =
18290 FormatStyle::BCIS_BeforeComma;
18291 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
18292 " : aaaaaaaaaaaaaaaa(1)\n"
18293 " , bbbbbbbbbbbbbbbb(2) {}",
18294 CtorInitializerStyle);
18295 CtorInitializerStyle.BreakConstructorInitializers =
18296 FormatStyle::BCIS_BeforeColon;
18297 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
18298 " : aaaaaaaaaaaaaaaa(1),\n"
18299 " bbbbbbbbbbbbbbbb(2) {}",
18300 CtorInitializerStyle);
18301 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0;
18302 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
18303 ": aaaaaaaaaaaaaaaa(1),\n"
18304 " bbbbbbbbbbbbbbbb(2) {}",
18305 CtorInitializerStyle);
18306
18307 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(ColumnLimit: 30);
18308 InheritanceStyle.SpaceBeforeInheritanceColon = false;
18309 verifyFormat("class Foo: public Bar {};", InheritanceStyle);
18310 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle);
18311 verifyFormat("for (auto a : b) {\n}", InheritanceStyle);
18312 verifyFormat("int x = a ? b : c;", InheritanceStyle);
18313 verifyFormat("{\n"
18314 "label2:\n"
18315 " int x = 0;\n"
18316 "}",
18317 InheritanceStyle);
18318 verifyFormat("switch (x) {\n"
18319 "case 1:\n"
18320 "default:\n"
18321 "}",
18322 InheritanceStyle);
18323 verifyFormat("switch (allBraces) {\n"
18324 "case 1: {\n"
18325 " break;\n"
18326 "}\n"
18327 "case 2: {\n"
18328 " [[fallthrough]];\n"
18329 "}\n"
18330 "default: {\n"
18331 " break;\n"
18332 "}\n"
18333 "}",
18334 InheritanceStyle);
18335 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma;
18336 verifyFormat("class Foooooooooooooooooooooo\n"
18337 " : public aaaaaaaaaaaaaaaaaa,\n"
18338 " public bbbbbbbbbbbbbbbbbb {\n"
18339 "}",
18340 InheritanceStyle);
18341 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon;
18342 verifyFormat("class Foooooooooooooooooooooo:\n"
18343 " public aaaaaaaaaaaaaaaaaa,\n"
18344 " public bbbbbbbbbbbbbbbbbb {\n"
18345 "}",
18346 InheritanceStyle);
18347 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
18348 verifyFormat("class Foooooooooooooooooooooo\n"
18349 " : public aaaaaaaaaaaaaaaaaa\n"
18350 " , public bbbbbbbbbbbbbbbbbb {\n"
18351 "}",
18352 InheritanceStyle);
18353 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
18354 verifyFormat("class Foooooooooooooooooooooo\n"
18355 " : public aaaaaaaaaaaaaaaaaa,\n"
18356 " public bbbbbbbbbbbbbbbbbb {\n"
18357 "}",
18358 InheritanceStyle);
18359 InheritanceStyle.ConstructorInitializerIndentWidth = 0;
18360 verifyFormat("class Foooooooooooooooooooooo\n"
18361 ": public aaaaaaaaaaaaaaaaaa,\n"
18362 " public bbbbbbbbbbbbbbbbbb {}",
18363 InheritanceStyle);
18364
18365 FormatStyle ForLoopStyle = getLLVMStyle();
18366 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false;
18367 verifyFormat("class Foo : public Bar {};", ForLoopStyle);
18368 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle);
18369 verifyFormat("for (auto a: b) {\n}", ForLoopStyle);
18370 verifyFormat("int x = a ? b : c;", ForLoopStyle);
18371 verifyFormat("{\n"
18372 "label2:\n"
18373 " int x = 0;\n"
18374 "}",
18375 ForLoopStyle);
18376 verifyFormat("switch (x) {\n"
18377 "case 1:\n"
18378 "default:\n"
18379 "}",
18380 ForLoopStyle);
18381 verifyFormat("switch (allBraces) {\n"
18382 "case 1: {\n"
18383 " break;\n"
18384 "}\n"
18385 "case 2: {\n"
18386 " [[fallthrough]];\n"
18387 "}\n"
18388 "default: {\n"
18389 " break;\n"
18390 "}\n"
18391 "}",
18392 ForLoopStyle);
18393
18394 FormatStyle CaseStyle = getLLVMStyle();
18395 CaseStyle.SpaceBeforeCaseColon = true;
18396 verifyFormat("class Foo : public Bar {};", CaseStyle);
18397 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle);
18398 verifyFormat("for (auto a : b) {\n}", CaseStyle);
18399 verifyFormat("int x = a ? b : c;", CaseStyle);
18400 verifyFormat("switch (x) {\n"
18401 "case 1 :\n"
18402 "default :\n"
18403 "}",
18404 CaseStyle);
18405 verifyFormat("switch (allBraces) {\n"
18406 "case 1 : {\n"
18407 " break;\n"
18408 "}\n"
18409 "case 2 : {\n"
18410 " [[fallthrough]];\n"
18411 "}\n"
18412 "default : {\n"
18413 " break;\n"
18414 "}\n"
18415 "}",
18416 CaseStyle);
18417 // Goto labels should not be affected.
18418 verifyFormat("switch (x) {\n"
18419 "goto_label:\n"
18420 "default :\n"
18421 "}",
18422 CaseStyle);
18423 verifyFormat("switch (x) {\n"
18424 "goto_label: { break; }\n"
18425 "default : {\n"
18426 " break;\n"
18427 "}\n"
18428 "}",
18429 CaseStyle);
18430
18431 FormatStyle NoSpaceStyle = getLLVMStyle();
18432 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false);
18433 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false;
18434 NoSpaceStyle.SpaceBeforeInheritanceColon = false;
18435 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
18436 verifyFormat("class Foo: public Bar {};", NoSpaceStyle);
18437 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle);
18438 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle);
18439 verifyFormat("int x = a ? b : c;", NoSpaceStyle);
18440 verifyFormat("{\n"
18441 "label3:\n"
18442 " int x = 0;\n"
18443 "}",
18444 NoSpaceStyle);
18445 verifyFormat("switch (x) {\n"
18446 "case 1:\n"
18447 "default:\n"
18448 "}",
18449 NoSpaceStyle);
18450 verifyFormat("switch (allBraces) {\n"
18451 "case 1: {\n"
18452 " break;\n"
18453 "}\n"
18454 "case 2: {\n"
18455 " [[fallthrough]];\n"
18456 "}\n"
18457 "default: {\n"
18458 " break;\n"
18459 "}\n"
18460 "}",
18461 NoSpaceStyle);
18462
18463 FormatStyle InvertedSpaceStyle = getLLVMStyle();
18464 InvertedSpaceStyle.SpaceBeforeCaseColon = true;
18465 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false;
18466 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false;
18467 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false;
18468 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle);
18469 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle);
18470 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle);
18471 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle);
18472 verifyFormat("{\n"
18473 "label3:\n"
18474 " int x = 0;\n"
18475 "}",
18476 InvertedSpaceStyle);
18477 verifyFormat("switch (x) {\n"
18478 "case 1 :\n"
18479 "case 2 : {\n"
18480 " break;\n"
18481 "}\n"
18482 "default :\n"
18483 " break;\n"
18484 "}",
18485 InvertedSpaceStyle);
18486 verifyFormat("switch (allBraces) {\n"
18487 "case 1 : {\n"
18488 " break;\n"
18489 "}\n"
18490 "case 2 : {\n"
18491 " [[fallthrough]];\n"
18492 "}\n"
18493 "default : {\n"
18494 " break;\n"
18495 "}\n"
18496 "}",
18497 InvertedSpaceStyle);
18498}
18499
18500TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) {
18501 FormatStyle Style = getLLVMStyle();
18502
18503 Style.PointerAlignment = FormatStyle::PAS_Left;
18504 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
18505 verifyFormat("void* const* x = NULL;", Style);
18506
18507#define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
18508 do { \
18509 Style.PointerAlignment = FormatStyle::Pointers; \
18510 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
18511 verifyFormat(Code, Style); \
18512 } while (false)
18513
18514 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default);
18515 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default);
18516 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default);
18517
18518 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before);
18519 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before);
18520 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before);
18521
18522 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After);
18523 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After);
18524 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After);
18525
18526 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both);
18527 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both);
18528 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both);
18529
18530 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default);
18531 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
18532 SAPQ_Default);
18533 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
18534 SAPQ_Default);
18535
18536 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before);
18537 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right,
18538 SAPQ_Before);
18539 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
18540 SAPQ_Before);
18541
18542 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After);
18543 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After);
18544 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle,
18545 SAPQ_After);
18546
18547 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both);
18548 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both);
18549 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both);
18550
18551#undef verifyQualifierSpaces
18552
18553 FormatStyle Spaces = getLLVMStyle();
18554 Spaces.AttributeMacros.push_back(x: "qualified");
18555 Spaces.PointerAlignment = FormatStyle::PAS_Right;
18556 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default;
18557 verifyFormat("SomeType *volatile *a = NULL;", Spaces);
18558 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
18559 verifyFormat("std::vector<SomeType *const *> x;", Spaces);
18560 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces);
18561 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18562 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
18563 verifyFormat("SomeType * volatile *a = NULL;", Spaces);
18564 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces);
18565 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18566 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18567 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18568
18569 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
18570 Spaces.PointerAlignment = FormatStyle::PAS_Left;
18571 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before;
18572 verifyFormat("SomeType* volatile* a = NULL;", Spaces);
18573 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces);
18574 verifyFormat("std::vector<SomeType* const*> x;", Spaces);
18575 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces);
18576 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18577 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
18578 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18579 verifyFormat("SomeType* volatile * a = NULL;", Spaces);
18580 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces);
18581 verifyFormat("std::vector<SomeType* const *> x;", Spaces);
18582 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces);
18583 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18584
18585 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18586 Spaces.PointerAlignment = FormatStyle::PAS_Middle;
18587 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After;
18588 verifyFormat("SomeType * volatile * a = NULL;", Spaces);
18589 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces);
18590 verifyFormat("std::vector<SomeType * const *> x;", Spaces);
18591 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces);
18592 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces);
18593}
18594
18595TEST_F(FormatTest, AlignConsecutiveMacros) {
18596 FormatStyle Style = getLLVMStyle();
18597 Style.AlignConsecutiveAssignments.Enabled = true;
18598 Style.AlignConsecutiveDeclarations.Enabled = true;
18599
18600 verifyFormat("#define a 3\n"
18601 "#define bbbb 4\n"
18602 "#define ccc (5)",
18603 Style);
18604
18605 verifyFormat("#define f(x) (x * x)\n"
18606 "#define fff(x, y, z) (x * y + z)\n"
18607 "#define ffff(x, y) (x - y)",
18608 Style);
18609
18610 verifyFormat("#define foo(x, y) (x + y)\n"
18611 "#define bar (5, 6)(2 + 2)",
18612 Style);
18613
18614 verifyFormat("#define a 3\n"
18615 "#define bbbb 4\n"
18616 "#define ccc (5)\n"
18617 "#define f(x) (x * x)\n"
18618 "#define fff(x, y, z) (x * y + z)\n"
18619 "#define ffff(x, y) (x - y)",
18620 Style);
18621
18622 Style.AlignConsecutiveMacros.Enabled = true;
18623 verifyFormat("#define a 3\n"
18624 "#define bbbb 4\n"
18625 "#define ccc (5)",
18626 Style);
18627
18628 verifyFormat("#define true 1\n"
18629 "#define false 0",
18630 Style);
18631
18632 verifyFormat("#define f(x) (x * x)\n"
18633 "#define fff(x, y, z) (x * y + z)\n"
18634 "#define ffff(x, y) (x - y)",
18635 Style);
18636
18637 verifyFormat("#define foo(x, y) (x + y)\n"
18638 "#define bar (5, 6)(2 + 2)",
18639 Style);
18640
18641 verifyFormat("#define a 3\n"
18642 "#define bbbb 4\n"
18643 "#define ccc (5)\n"
18644 "#define f(x) (x * x)\n"
18645 "#define fff(x, y, z) (x * y + z)\n"
18646 "#define ffff(x, y) (x - y)",
18647 Style);
18648
18649 verifyFormat("#define a 5\n"
18650 "#define foo(x, y) (x + y)\n"
18651 "#define CCC (6)\n"
18652 "auto lambda = []() {\n"
18653 " auto ii = 0;\n"
18654 " float j = 0;\n"
18655 " return 0;\n"
18656 "};\n"
18657 "int i = 0;\n"
18658 "float i2 = 0;\n"
18659 "auto v = type{\n"
18660 " i = 1, //\n"
18661 " (i = 2), //\n"
18662 " i = 3 //\n"
18663 "};",
18664 Style);
18665
18666 Style.AlignConsecutiveMacros.Enabled = false;
18667 Style.ColumnLimit = 20;
18668
18669 verifyFormat("#define a \\\n"
18670 " \"aabbbbbbbbbbbb\"\n"
18671 "#define D \\\n"
18672 " \"aabbbbbbbbbbbb\" \\\n"
18673 " \"ccddeeeeeeeee\"\n"
18674 "#define B \\\n"
18675 " \"QQQQQQQQQQQQQ\" \\\n"
18676 " \"FFFFFFFFFFFFF\" \\\n"
18677 " \"LLLLLLLL\"",
18678 Style);
18679
18680 Style.AlignConsecutiveMacros.Enabled = true;
18681 verifyFormat("#define a \\\n"
18682 " \"aabbbbbbbbbbbb\"\n"
18683 "#define D \\\n"
18684 " \"aabbbbbbbbbbbb\" \\\n"
18685 " \"ccddeeeeeeeee\"\n"
18686 "#define B \\\n"
18687 " \"QQQQQQQQQQQQQ\" \\\n"
18688 " \"FFFFFFFFFFFFF\" \\\n"
18689 " \"LLLLLLLL\"",
18690 Style);
18691
18692 // Test across comments
18693 Style.MaxEmptyLinesToKeep = 10;
18694 Style.ReflowComments = FormatStyle::RCS_Never;
18695 Style.AlignConsecutiveMacros.AcrossComments = true;
18696 verifyFormat("#define a 3\n"
18697 "// line comment\n"
18698 "#define bbbb 4\n"
18699 "#define ccc (5)",
18700 "#define a 3\n"
18701 "// line comment\n"
18702 "#define bbbb 4\n"
18703 "#define ccc (5)",
18704 Style);
18705
18706 verifyFormat("#define a 3\n"
18707 "/* block comment */\n"
18708 "#define bbbb 4\n"
18709 "#define ccc (5)",
18710 "#define a 3\n"
18711 "/* block comment */\n"
18712 "#define bbbb 4\n"
18713 "#define ccc (5)",
18714 Style);
18715
18716 verifyFormat("#define a 3\n"
18717 "/* multi-line *\n"
18718 " * block comment */\n"
18719 "#define bbbb 4\n"
18720 "#define ccc (5)",
18721 "#define a 3\n"
18722 "/* multi-line *\n"
18723 " * block comment */\n"
18724 "#define bbbb 4\n"
18725 "#define ccc (5)",
18726 Style);
18727
18728 verifyFormat("#define a 3\n"
18729 "// multi-line line comment\n"
18730 "//\n"
18731 "#define bbbb 4\n"
18732 "#define ccc (5)",
18733 "#define a 3\n"
18734 "// multi-line line comment\n"
18735 "//\n"
18736 "#define bbbb 4\n"
18737 "#define ccc (5)",
18738 Style);
18739
18740 verifyFormat("#define a 3\n"
18741 "// empty lines still break.\n"
18742 "\n"
18743 "#define bbbb 4\n"
18744 "#define ccc (5)",
18745 "#define a 3\n"
18746 "// empty lines still break.\n"
18747 "\n"
18748 "#define bbbb 4\n"
18749 "#define ccc (5)",
18750 Style);
18751
18752 // Test across empty lines
18753 Style.AlignConsecutiveMacros.AcrossComments = false;
18754 Style.AlignConsecutiveMacros.AcrossEmptyLines = true;
18755 verifyFormat("#define a 3\n"
18756 "\n"
18757 "#define bbbb 4\n"
18758 "#define ccc (5)",
18759 "#define a 3\n"
18760 "\n"
18761 "#define bbbb 4\n"
18762 "#define ccc (5)",
18763 Style);
18764
18765 verifyFormat("#define a 3\n"
18766 "\n"
18767 "\n"
18768 "\n"
18769 "#define bbbb 4\n"
18770 "#define ccc (5)",
18771 "#define a 3\n"
18772 "\n"
18773 "\n"
18774 "\n"
18775 "#define bbbb 4\n"
18776 "#define ccc (5)",
18777 Style);
18778
18779 verifyFormat("#define a 3\n"
18780 "// comments should break alignment\n"
18781 "//\n"
18782 "#define bbbb 4\n"
18783 "#define ccc (5)",
18784 "#define a 3\n"
18785 "// comments should break alignment\n"
18786 "//\n"
18787 "#define bbbb 4\n"
18788 "#define ccc (5)",
18789 Style);
18790
18791 // Test across empty lines and comments
18792 Style.AlignConsecutiveMacros.AcrossComments = true;
18793 verifyFormat("#define a 3\n"
18794 "\n"
18795 "// line comment\n"
18796 "#define bbbb 4\n"
18797 "#define ccc (5)",
18798 Style);
18799
18800 verifyFormat("#define a 3\n"
18801 "\n"
18802 "\n"
18803 "/* multi-line *\n"
18804 " * block comment */\n"
18805 "\n"
18806 "\n"
18807 "#define bbbb 4\n"
18808 "#define ccc (5)",
18809 "#define a 3\n"
18810 "\n"
18811 "\n"
18812 "/* multi-line *\n"
18813 " * block comment */\n"
18814 "\n"
18815 "\n"
18816 "#define bbbb 4\n"
18817 "#define ccc (5)",
18818 Style);
18819
18820 verifyFormat("#define a 3\n"
18821 "\n"
18822 "\n"
18823 "/* multi-line *\n"
18824 " * block comment */\n"
18825 "\n"
18826 "\n"
18827 "#define bbbb 4\n"
18828 "#define ccc (5)",
18829 "#define a 3\n"
18830 "\n"
18831 "\n"
18832 "/* multi-line *\n"
18833 " * block comment */\n"
18834 "\n"
18835 "\n"
18836 "#define bbbb 4\n"
18837 "#define ccc (5)",
18838 Style);
18839}
18840
18841TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) {
18842 FormatStyle Alignment = getLLVMStyle();
18843 Alignment.AlignConsecutiveMacros.Enabled = true;
18844 Alignment.AlignConsecutiveAssignments.Enabled = true;
18845 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
18846
18847 Alignment.MaxEmptyLinesToKeep = 10;
18848 /* Test alignment across empty lines */
18849 verifyFormat("int a = 5;\n"
18850 "\n"
18851 "int oneTwoThree = 123;",
18852 "int a = 5;\n"
18853 "\n"
18854 "int oneTwoThree= 123;",
18855 Alignment);
18856 verifyFormat("int a = 5;\n"
18857 "int one = 1;\n"
18858 "\n"
18859 "int oneTwoThree = 123;",
18860 "int a = 5;\n"
18861 "int one = 1;\n"
18862 "\n"
18863 "int oneTwoThree = 123;",
18864 Alignment);
18865 verifyFormat("int a = 5;\n"
18866 "int one = 1;\n"
18867 "\n"
18868 "int oneTwoThree = 123;\n"
18869 "int oneTwo = 12;",
18870 "int a = 5;\n"
18871 "int one = 1;\n"
18872 "\n"
18873 "int oneTwoThree = 123;\n"
18874 "int oneTwo = 12;",
18875 Alignment);
18876
18877 /* Test across comments */
18878 verifyFormat("int a = 5;\n"
18879 "/* block comment */\n"
18880 "int oneTwoThree = 123;",
18881 "int a = 5;\n"
18882 "/* block comment */\n"
18883 "int oneTwoThree=123;",
18884 Alignment);
18885
18886 verifyFormat("int a = 5;\n"
18887 "// line comment\n"
18888 "int oneTwoThree = 123;",
18889 "int a = 5;\n"
18890 "// line comment\n"
18891 "int oneTwoThree=123;",
18892 Alignment);
18893
18894 /* Test across comments and newlines */
18895 verifyFormat("int a = 5;\n"
18896 "\n"
18897 "/* block comment */\n"
18898 "int oneTwoThree = 123;",
18899 "int a = 5;\n"
18900 "\n"
18901 "/* block comment */\n"
18902 "int oneTwoThree=123;",
18903 Alignment);
18904
18905 verifyFormat("int a = 5;\n"
18906 "\n"
18907 "// line comment\n"
18908 "int oneTwoThree = 123;",
18909 "int a = 5;\n"
18910 "\n"
18911 "// line comment\n"
18912 "int oneTwoThree=123;",
18913 Alignment);
18914}
18915
18916TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
18917 FormatStyle Alignment = getLLVMStyle();
18918 Alignment.AlignConsecutiveDeclarations.Enabled = true;
18919 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true;
18920 Alignment.AlignConsecutiveDeclarations.AcrossComments = true;
18921
18922 Alignment.MaxEmptyLinesToKeep = 10;
18923 /* Test alignment across empty lines */
18924 verifyFormat("int a = 5;\n"
18925 "\n"
18926 "float const oneTwoThree = 123;",
18927 "int a = 5;\n"
18928 "\n"
18929 "float const oneTwoThree = 123;",
18930 Alignment);
18931 verifyFormat("int a = 5;\n"
18932 "float const one = 1;\n"
18933 "\n"
18934 "int oneTwoThree = 123;",
18935 "int a = 5;\n"
18936 "float const one = 1;\n"
18937 "\n"
18938 "int oneTwoThree = 123;",
18939 Alignment);
18940
18941 /* Test across comments */
18942 verifyFormat("float const a = 5;\n"
18943 "/* block comment */\n"
18944 "int oneTwoThree = 123;",
18945 "float const a = 5;\n"
18946 "/* block comment */\n"
18947 "int oneTwoThree=123;",
18948 Alignment);
18949
18950 verifyFormat("float const a = 5;\n"
18951 "// line comment\n"
18952 "int oneTwoThree = 123;",
18953 "float const a = 5;\n"
18954 "// line comment\n"
18955 "int oneTwoThree=123;",
18956 Alignment);
18957
18958 /* Test across comments and newlines */
18959 verifyFormat("float const a = 5;\n"
18960 "\n"
18961 "/* block comment */\n"
18962 "int oneTwoThree = 123;",
18963 "float const a = 5;\n"
18964 "\n"
18965 "/* block comment */\n"
18966 "int oneTwoThree=123;",
18967 Alignment);
18968
18969 verifyFormat("float const a = 5;\n"
18970 "\n"
18971 "// line comment\n"
18972 "int oneTwoThree = 123;",
18973 "float const a = 5;\n"
18974 "\n"
18975 "// line comment\n"
18976 "int oneTwoThree=123;",
18977 Alignment);
18978}
18979
18980TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) {
18981 FormatStyle Alignment = getLLVMStyle();
18982 Alignment.AlignConsecutiveBitFields.Enabled = true;
18983 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true;
18984 Alignment.AlignConsecutiveBitFields.AcrossComments = true;
18985
18986 Alignment.MaxEmptyLinesToKeep = 10;
18987 /* Test alignment across empty lines */
18988 verifyFormat("int a : 5;\n"
18989 "\n"
18990 "int longbitfield : 6;",
18991 "int a : 5;\n"
18992 "\n"
18993 "int longbitfield : 6;",
18994 Alignment);
18995 verifyFormat("int a : 5;\n"
18996 "int one : 1;\n"
18997 "\n"
18998 "int longbitfield : 6;",
18999 "int a : 5;\n"
19000 "int one : 1;\n"
19001 "\n"
19002 "int longbitfield : 6;",
19003 Alignment);
19004
19005 /* Test across comments */
19006 verifyFormat("int a : 5;\n"
19007 "/* block comment */\n"
19008 "int longbitfield : 6;",
19009 "int a : 5;\n"
19010 "/* block comment */\n"
19011 "int longbitfield : 6;",
19012 Alignment);
19013 verifyFormat("int a : 5;\n"
19014 "int one : 1;\n"
19015 "// line comment\n"
19016 "int longbitfield : 6;",
19017 "int a : 5;\n"
19018 "int one : 1;\n"
19019 "// line comment\n"
19020 "int longbitfield : 6;",
19021 Alignment);
19022
19023 /* Test across comments and newlines */
19024 verifyFormat("int a : 5;\n"
19025 "/* block comment */\n"
19026 "\n"
19027 "int longbitfield : 6;",
19028 "int a : 5;\n"
19029 "/* block comment */\n"
19030 "\n"
19031 "int longbitfield : 6;",
19032 Alignment);
19033 verifyFormat("int a : 5;\n"
19034 "int one : 1;\n"
19035 "\n"
19036 "// line comment\n"
19037 "\n"
19038 "int longbitfield : 6;",
19039 "int a : 5;\n"
19040 "int one : 1;\n"
19041 "\n"
19042 "// line comment \n"
19043 "\n"
19044 "int longbitfield : 6;",
19045 Alignment);
19046}
19047
19048TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) {
19049 FormatStyle Alignment = getLLVMStyle();
19050 Alignment.AlignConsecutiveMacros.Enabled = true;
19051 Alignment.AlignConsecutiveAssignments.Enabled = true;
19052 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
19053
19054 Alignment.MaxEmptyLinesToKeep = 10;
19055 /* Test alignment across empty lines */
19056 verifyFormat("int a = 5;\n"
19057 "\n"
19058 "int oneTwoThree = 123;",
19059 "int a = 5;\n"
19060 "\n"
19061 "int oneTwoThree= 123;",
19062 Alignment);
19063 verifyFormat("int a = 5;\n"
19064 "int one = 1;\n"
19065 "\n"
19066 "int oneTwoThree = 123;",
19067 "int a = 5;\n"
19068 "int one = 1;\n"
19069 "\n"
19070 "int oneTwoThree = 123;",
19071 Alignment);
19072
19073 /* Test across comments */
19074 verifyFormat("int a = 5;\n"
19075 "/* block comment */\n"
19076 "int oneTwoThree = 123;",
19077 "int a = 5;\n"
19078 "/* block comment */\n"
19079 "int oneTwoThree=123;",
19080 Alignment);
19081
19082 verifyFormat("int a = 5;\n"
19083 "// line comment\n"
19084 "int oneTwoThree = 123;",
19085 "int a = 5;\n"
19086 "// line comment\n"
19087 "int oneTwoThree=123;",
19088 Alignment);
19089
19090 verifyFormat("int a = 5;\n"
19091 "/*\n"
19092 " * multi-line block comment\n"
19093 " */\n"
19094 "int oneTwoThree = 123;",
19095 "int a = 5;\n"
19096 "/*\n"
19097 " * multi-line block comment\n"
19098 " */\n"
19099 "int oneTwoThree=123;",
19100 Alignment);
19101
19102 verifyFormat("int a = 5;\n"
19103 "//\n"
19104 "// multi-line line comment\n"
19105 "//\n"
19106 "int oneTwoThree = 123;",
19107 "int a = 5;\n"
19108 "//\n"
19109 "// multi-line line comment\n"
19110 "//\n"
19111 "int oneTwoThree=123;",
19112 Alignment);
19113
19114 /* Test across comments and newlines */
19115 verifyFormat("int a = 5;\n"
19116 "\n"
19117 "/* block comment */\n"
19118 "int oneTwoThree = 123;",
19119 "int a = 5;\n"
19120 "\n"
19121 "/* block comment */\n"
19122 "int oneTwoThree=123;",
19123 Alignment);
19124
19125 verifyFormat("int a = 5;\n"
19126 "\n"
19127 "// line comment\n"
19128 "int oneTwoThree = 123;",
19129 "int a = 5;\n"
19130 "\n"
19131 "// line comment\n"
19132 "int oneTwoThree=123;",
19133 Alignment);
19134}
19135
19136TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) {
19137 FormatStyle Alignment = getLLVMStyle();
19138 Alignment.AlignConsecutiveMacros.Enabled = true;
19139 Alignment.AlignConsecutiveAssignments.Enabled = true;
19140 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
19141 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
19142 verifyFormat("int a = 5;\n"
19143 "int oneTwoThree = 123;",
19144 Alignment);
19145 verifyFormat("int a = method();\n"
19146 "int oneTwoThree = 133;",
19147 Alignment);
19148 verifyFormat("a &= 5;\n"
19149 "bcd *= 5;\n"
19150 "ghtyf += 5;\n"
19151 "dvfvdb -= 5;\n"
19152 "a /= 5;\n"
19153 "vdsvsv %= 5;\n"
19154 "sfdbddfbdfbb ^= 5;\n"
19155 "dvsdsv |= 5;\n"
19156 "int dsvvdvsdvvv = 123;",
19157 Alignment);
19158 verifyFormat("int i = 1, j = 10;\n"
19159 "something = 2000;",
19160 Alignment);
19161 verifyFormat("something = 2000;\n"
19162 "int i = 1, j = 10;",
19163 Alignment);
19164 verifyFormat("something = 2000;\n"
19165 "another = 911;\n"
19166 "int i = 1, j = 10;\n"
19167 "oneMore = 1;\n"
19168 "i = 2;",
19169 Alignment);
19170 verifyFormat("int a = 5;\n"
19171 "int one = 1;\n"
19172 "method();\n"
19173 "int oneTwoThree = 123;\n"
19174 "int oneTwo = 12;",
19175 Alignment);
19176 verifyFormat("int oneTwoThree = 123;\n"
19177 "int oneTwo = 12;\n"
19178 "method();",
19179 Alignment);
19180 verifyFormat("int oneTwoThree = 123; // comment\n"
19181 "int oneTwo = 12; // comment",
19182 Alignment);
19183
19184 // Bug 25167
19185 /* Uncomment when fixed
19186 verifyFormat("#if A\n"
19187 "#else\n"
19188 "int aaaaaaaa = 12;\n"
19189 "#endif\n"
19190 "#if B\n"
19191 "#else\n"
19192 "int a = 12;\n"
19193 "#endif",
19194 Alignment);
19195 verifyFormat("enum foo {\n"
19196 "#if A\n"
19197 "#else\n"
19198 " aaaaaaaa = 12;\n"
19199 "#endif\n"
19200 "#if B\n"
19201 "#else\n"
19202 " a = 12;\n"
19203 "#endif\n"
19204 "};",
19205 Alignment);
19206 */
19207
19208 Alignment.MaxEmptyLinesToKeep = 10;
19209 /* Test alignment across empty lines */
19210 verifyFormat("int a = 5;\n"
19211 "\n"
19212 "int oneTwoThree = 123;",
19213 "int a = 5;\n"
19214 "\n"
19215 "int oneTwoThree= 123;",
19216 Alignment);
19217 verifyFormat("int a = 5;\n"
19218 "int one = 1;\n"
19219 "\n"
19220 "int oneTwoThree = 123;",
19221 "int a = 5;\n"
19222 "int one = 1;\n"
19223 "\n"
19224 "int oneTwoThree = 123;",
19225 Alignment);
19226 verifyFormat("int a = 5;\n"
19227 "int one = 1;\n"
19228 "\n"
19229 "int oneTwoThree = 123;\n"
19230 "int oneTwo = 12;",
19231 "int a = 5;\n"
19232 "int one = 1;\n"
19233 "\n"
19234 "int oneTwoThree = 123;\n"
19235 "int oneTwo = 12;",
19236 Alignment);
19237
19238 /* Test across comments */
19239 verifyFormat("int a = 5;\n"
19240 "/* block comment */\n"
19241 "int oneTwoThree = 123;",
19242 "int a = 5;\n"
19243 "/* block comment */\n"
19244 "int oneTwoThree=123;",
19245 Alignment);
19246
19247 verifyFormat("int a = 5;\n"
19248 "// line comment\n"
19249 "int oneTwoThree = 123;",
19250 "int a = 5;\n"
19251 "// line comment\n"
19252 "int oneTwoThree=123;",
19253 Alignment);
19254
19255 /* Test across comments and newlines */
19256 verifyFormat("int a = 5;\n"
19257 "\n"
19258 "/* block comment */\n"
19259 "int oneTwoThree = 123;",
19260 "int a = 5;\n"
19261 "\n"
19262 "/* block comment */\n"
19263 "int oneTwoThree=123;",
19264 Alignment);
19265
19266 verifyFormat("int a = 5;\n"
19267 "\n"
19268 "// line comment\n"
19269 "int oneTwoThree = 123;",
19270 "int a = 5;\n"
19271 "\n"
19272 "// line comment\n"
19273 "int oneTwoThree=123;",
19274 Alignment);
19275
19276 verifyFormat("int a = 5;\n"
19277 "//\n"
19278 "// multi-line line comment\n"
19279 "//\n"
19280 "int oneTwoThree = 123;",
19281 "int a = 5;\n"
19282 "//\n"
19283 "// multi-line line comment\n"
19284 "//\n"
19285 "int oneTwoThree=123;",
19286 Alignment);
19287
19288 verifyFormat("int a = 5;\n"
19289 "/*\n"
19290 " * multi-line block comment\n"
19291 " */\n"
19292 "int oneTwoThree = 123;",
19293 "int a = 5;\n"
19294 "/*\n"
19295 " * multi-line block comment\n"
19296 " */\n"
19297 "int oneTwoThree=123;",
19298 Alignment);
19299
19300 verifyFormat("int a = 5;\n"
19301 "\n"
19302 "/* block comment */\n"
19303 "\n"
19304 "\n"
19305 "\n"
19306 "int oneTwoThree = 123;",
19307 "int a = 5;\n"
19308 "\n"
19309 "/* block comment */\n"
19310 "\n"
19311 "\n"
19312 "\n"
19313 "int oneTwoThree=123;",
19314 Alignment);
19315
19316 verifyFormat("int a = 5;\n"
19317 "\n"
19318 "// line comment\n"
19319 "\n"
19320 "\n"
19321 "\n"
19322 "int oneTwoThree = 123;",
19323 "int a = 5;\n"
19324 "\n"
19325 "// line comment\n"
19326 "\n"
19327 "\n"
19328 "\n"
19329 "int oneTwoThree=123;",
19330 Alignment);
19331
19332 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19333 verifyFormat("#define A \\\n"
19334 " int aaaa = 12; \\\n"
19335 " int b = 23; \\\n"
19336 " int ccc = 234; \\\n"
19337 " int dddddddddd = 2345;",
19338 Alignment);
19339 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19340 verifyFormat("#define A \\\n"
19341 " int aaaa = 12; \\\n"
19342 " int b = 23; \\\n"
19343 " int ccc = 234; \\\n"
19344 " int dddddddddd = 2345;",
19345 Alignment);
19346 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19347 verifyFormat("#define A "
19348 " \\\n"
19349 " int aaaa = 12; "
19350 " \\\n"
19351 " int b = 23; "
19352 " \\\n"
19353 " int ccc = 234; "
19354 " \\\n"
19355 " int dddddddddd = 2345;",
19356 Alignment);
19357 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19358 "k = 4, int l = 5,\n"
19359 " int m = 6) {\n"
19360 " int j = 10;\n"
19361 " otherThing = 1;\n"
19362 "}",
19363 Alignment);
19364 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19365 " int i = 1;\n"
19366 " int j = 2;\n"
19367 " int big = 10000;\n"
19368 "}",
19369 Alignment);
19370 verifyFormat("class C {\n"
19371 "public:\n"
19372 " int i = 1;\n"
19373 " virtual void f() = 0;\n"
19374 "};",
19375 Alignment);
19376 verifyFormat("int i = 1;\n"
19377 "if (SomeType t = getSomething()) {\n"
19378 "}\n"
19379 "int j = 2;\n"
19380 "int big = 10000;",
19381 Alignment);
19382 verifyFormat("int j = 7;\n"
19383 "for (int k = 0; k < N; ++k) {\n"
19384 "}\n"
19385 "int j = 2;\n"
19386 "int big = 10000;\n"
19387 "}",
19388 Alignment);
19389 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19390 verifyFormat("int i = 1;\n"
19391 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19392 " = someLooooooooooooooooongFunction();\n"
19393 "int j = 2;",
19394 Alignment);
19395 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19396 verifyFormat("int i = 1;\n"
19397 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19398 " someLooooooooooooooooongFunction();\n"
19399 "int j = 2;",
19400 Alignment);
19401
19402 verifyFormat("auto lambda = []() {\n"
19403 " auto i = 0;\n"
19404 " return 0;\n"
19405 "};\n"
19406 "int i = 0;\n"
19407 "auto v = type{\n"
19408 " i = 1, //\n"
19409 " (i = 2), //\n"
19410 " i = 3 //\n"
19411 "};",
19412 Alignment);
19413
19414 verifyFormat(
19415 "int i = 1;\n"
19416 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19417 " loooooooooooooooooooooongParameterB);\n"
19418 "int j = 2;",
19419 Alignment);
19420
19421 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19422 " typename B = very_long_type_name_1,\n"
19423 " typename T_2 = very_long_type_name_2>\n"
19424 "auto foo() {}",
19425 Alignment);
19426 verifyFormat("int a, b = 1;\n"
19427 "int c = 2;\n"
19428 "int dd = 3;",
19429 Alignment);
19430 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19431 "float b[1][] = {{3.f}};",
19432 Alignment);
19433 verifyFormat("for (int i = 0; i < 1; i++)\n"
19434 " int x = 1;",
19435 Alignment);
19436 verifyFormat("for (i = 0; i < 1; i++)\n"
19437 " x = 1;\n"
19438 "y = 1;",
19439 Alignment);
19440
19441 Alignment.ReflowComments = FormatStyle::RCS_Always;
19442 Alignment.ColumnLimit = 50;
19443 verifyFormat("int x = 0;\n"
19444 "int yy = 1; /// specificlennospace\n"
19445 "int zzz = 2;",
19446 "int x = 0;\n"
19447 "int yy = 1; ///specificlennospace\n"
19448 "int zzz = 2;",
19449 Alignment);
19450}
19451
19452TEST_F(FormatTest, AlignCompoundAssignments) {
19453 FormatStyle Alignment = getLLVMStyle();
19454 Alignment.AlignConsecutiveAssignments.Enabled = true;
19455 Alignment.AlignConsecutiveAssignments.AlignCompound = true;
19456 Alignment.AlignConsecutiveAssignments.PadOperators = false;
19457 verifyFormat("sfdbddfbdfbb = 5;\n"
19458 "dvsdsv = 5;\n"
19459 "int dsvvdvsdvvv = 123;",
19460 Alignment);
19461 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19462 "dvsdsv |= 5;\n"
19463 "int dsvvdvsdvvv = 123;",
19464 Alignment);
19465 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19466 "dvsdsv <<= 5;\n"
19467 "int dsvvdvsdvvv = 123;",
19468 Alignment);
19469 verifyFormat("int xxx = 5;\n"
19470 "xxx = 5;\n"
19471 "{\n"
19472 " int yyy = 6;\n"
19473 " yyy = 6;\n"
19474 "}",
19475 Alignment);
19476 verifyFormat("int xxx = 5;\n"
19477 "xxx += 5;\n"
19478 "{\n"
19479 " int yyy = 6;\n"
19480 " yyy += 6;\n"
19481 "}",
19482 Alignment);
19483 // Test that `<=` is not treated as a compound assignment.
19484 verifyFormat("aa &= 5;\n"
19485 "b <= 10;\n"
19486 "c = 15;",
19487 Alignment);
19488 Alignment.AlignConsecutiveAssignments.PadOperators = true;
19489 verifyFormat("sfdbddfbdfbb = 5;\n"
19490 "dvsdsv = 5;\n"
19491 "int dsvvdvsdvvv = 123;",
19492 Alignment);
19493 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19494 "dvsdsv |= 5;\n"
19495 "int dsvvdvsdvvv = 123;",
19496 Alignment);
19497 verifyFormat("sfdbddfbdfbb ^= 5;\n"
19498 "dvsdsv <<= 5;\n"
19499 "int dsvvdvsdvvv = 123;",
19500 Alignment);
19501 verifyFormat("a += 5;\n"
19502 "one = 1;\n"
19503 "\n"
19504 "oneTwoThree = 123;",
19505 "a += 5;\n"
19506 "one = 1;\n"
19507 "\n"
19508 "oneTwoThree = 123;",
19509 Alignment);
19510 verifyFormat("a += 5;\n"
19511 "one = 1;\n"
19512 "//\n"
19513 "oneTwoThree = 123;",
19514 "a += 5;\n"
19515 "one = 1;\n"
19516 "//\n"
19517 "oneTwoThree = 123;",
19518 Alignment);
19519 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
19520 verifyFormat("a += 5;\n"
19521 "one = 1;\n"
19522 "\n"
19523 "oneTwoThree = 123;",
19524 "a += 5;\n"
19525 "one = 1;\n"
19526 "\n"
19527 "oneTwoThree = 123;",
19528 Alignment);
19529 verifyFormat("a += 5;\n"
19530 "one = 1;\n"
19531 "//\n"
19532 "oneTwoThree = 123;",
19533 "a += 5;\n"
19534 "one = 1;\n"
19535 "//\n"
19536 "oneTwoThree = 123;",
19537 Alignment);
19538 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false;
19539 Alignment.AlignConsecutiveAssignments.AcrossComments = true;
19540 verifyFormat("a += 5;\n"
19541 "one = 1;\n"
19542 "\n"
19543 "oneTwoThree = 123;",
19544 "a += 5;\n"
19545 "one = 1;\n"
19546 "\n"
19547 "oneTwoThree = 123;",
19548 Alignment);
19549 verifyFormat("a += 5;\n"
19550 "one = 1;\n"
19551 "//\n"
19552 "oneTwoThree = 123;",
19553 "a += 5;\n"
19554 "one = 1;\n"
19555 "//\n"
19556 "oneTwoThree = 123;",
19557 Alignment);
19558 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true;
19559 verifyFormat("a += 5;\n"
19560 "one >>= 1;\n"
19561 "\n"
19562 "oneTwoThree = 123;",
19563 "a += 5;\n"
19564 "one >>= 1;\n"
19565 "\n"
19566 "oneTwoThree = 123;",
19567 Alignment);
19568 verifyFormat("a += 5;\n"
19569 "one = 1;\n"
19570 "//\n"
19571 "oneTwoThree <<= 123;",
19572 "a += 5;\n"
19573 "one = 1;\n"
19574 "//\n"
19575 "oneTwoThree <<= 123;",
19576 Alignment);
19577}
19578
19579TEST_F(FormatTest, AlignConsecutiveAssignments) {
19580 FormatStyle Alignment = getLLVMStyle();
19581 Alignment.AlignConsecutiveMacros.Enabled = true;
19582 verifyFormat("int a = 5;\n"
19583 "int oneTwoThree = 123;",
19584 Alignment);
19585 verifyFormat("int a = 5;\n"
19586 "int oneTwoThree = 123;",
19587 Alignment);
19588
19589 Alignment.AlignConsecutiveAssignments.Enabled = true;
19590 verifyFormat("int a = 5;\n"
19591 "int oneTwoThree = 123;",
19592 Alignment);
19593 verifyFormat("int a = method();\n"
19594 "int oneTwoThree = 133;",
19595 Alignment);
19596 verifyFormat("aa <= 5;\n"
19597 "a &= 5;\n"
19598 "bcd *= 5;\n"
19599 "ghtyf += 5;\n"
19600 "dvfvdb -= 5;\n"
19601 "a /= 5;\n"
19602 "vdsvsv %= 5;\n"
19603 "sfdbddfbdfbb ^= 5;\n"
19604 "dvsdsv |= 5;\n"
19605 "int dsvvdvsdvvv = 123;",
19606 Alignment);
19607 verifyFormat("int i = 1, j = 10;\n"
19608 "something = 2000;",
19609 Alignment);
19610 verifyFormat("something = 2000;\n"
19611 "int i = 1, j = 10;",
19612 Alignment);
19613 verifyFormat("something = 2000;\n"
19614 "another = 911;\n"
19615 "int i = 1, j = 10;\n"
19616 "oneMore = 1;\n"
19617 "i = 2;",
19618 Alignment);
19619 verifyFormat("int a = 5;\n"
19620 "int one = 1;\n"
19621 "method();\n"
19622 "int oneTwoThree = 123;\n"
19623 "int oneTwo = 12;",
19624 Alignment);
19625 verifyFormat("int oneTwoThree = 123;\n"
19626 "int oneTwo = 12;\n"
19627 "method();",
19628 Alignment);
19629 verifyFormat("int oneTwoThree = 123; // comment\n"
19630 "int oneTwo = 12; // comment",
19631 Alignment);
19632 verifyFormat("int f() = default;\n"
19633 "int &operator() = default;\n"
19634 "int &operator=() {",
19635 Alignment);
19636 verifyFormat("int f() = delete;\n"
19637 "int &operator() = delete;\n"
19638 "int &operator=() {",
19639 Alignment);
19640 verifyFormat("int f() = default; // comment\n"
19641 "int &operator() = default; // comment\n"
19642 "int &operator=() {",
19643 Alignment);
19644 verifyFormat("int f() = default;\n"
19645 "int &operator() = default;\n"
19646 "int &operator==() {",
19647 Alignment);
19648 verifyFormat("int f() = default;\n"
19649 "int &operator() = default;\n"
19650 "int &operator<=() {",
19651 Alignment);
19652 verifyFormat("int f() = default;\n"
19653 "int &operator() = default;\n"
19654 "int &operator!=() {",
19655 Alignment);
19656 verifyFormat("int f() = default;\n"
19657 "int &operator() = default;\n"
19658 "int &operator=();",
19659 Alignment);
19660 verifyFormat("int f() = delete;\n"
19661 "int &operator() = delete;\n"
19662 "int &operator=();",
19663 Alignment);
19664 verifyFormat("/* long long padding */ int f() = default;\n"
19665 "int &operator() = default;\n"
19666 "int &operator/**/ =();",
19667 Alignment);
19668 // https://llvm.org/PR33697
19669 FormatStyle AlignmentWithPenalty = getLLVMStyle();
19670 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true;
19671 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000;
19672 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19673 " void f() = delete;\n"
19674 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19675 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19676 "};",
19677 AlignmentWithPenalty);
19678
19679 // Bug 25167
19680 /* Uncomment when fixed
19681 verifyFormat("#if A\n"
19682 "#else\n"
19683 "int aaaaaaaa = 12;\n"
19684 "#endif\n"
19685 "#if B\n"
19686 "#else\n"
19687 "int a = 12;\n"
19688 "#endif",
19689 Alignment);
19690 verifyFormat("enum foo {\n"
19691 "#if A\n"
19692 "#else\n"
19693 " aaaaaaaa = 12;\n"
19694 "#endif\n"
19695 "#if B\n"
19696 "#else\n"
19697 " a = 12;\n"
19698 "#endif\n"
19699 "};",
19700 Alignment);
19701 */
19702
19703 verifyFormat("int a = 5;\n"
19704 "\n"
19705 "int oneTwoThree = 123;",
19706 "int a = 5;\n"
19707 "\n"
19708 "int oneTwoThree= 123;",
19709 Alignment);
19710 verifyFormat("int a = 5;\n"
19711 "int one = 1;\n"
19712 "\n"
19713 "int oneTwoThree = 123;",
19714 "int a = 5;\n"
19715 "int one = 1;\n"
19716 "\n"
19717 "int oneTwoThree = 123;",
19718 Alignment);
19719 verifyFormat("int a = 5;\n"
19720 "int one = 1;\n"
19721 "\n"
19722 "int oneTwoThree = 123;\n"
19723 "int oneTwo = 12;",
19724 "int a = 5;\n"
19725 "int one = 1;\n"
19726 "\n"
19727 "int oneTwoThree = 123;\n"
19728 "int oneTwo = 12;",
19729 Alignment);
19730 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
19731 verifyFormat("#define A \\\n"
19732 " int aaaa = 12; \\\n"
19733 " int b = 23; \\\n"
19734 " int ccc = 234; \\\n"
19735 " int dddddddddd = 2345;",
19736 Alignment);
19737 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
19738 verifyFormat("#define A \\\n"
19739 " int aaaa = 12; \\\n"
19740 " int b = 23; \\\n"
19741 " int ccc = 234; \\\n"
19742 " int dddddddddd = 2345;",
19743 Alignment);
19744 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
19745 verifyFormat("#define A "
19746 " \\\n"
19747 " int aaaa = 12; "
19748 " \\\n"
19749 " int b = 23; "
19750 " \\\n"
19751 " int ccc = 234; "
19752 " \\\n"
19753 " int dddddddddd = 2345;",
19754 Alignment);
19755 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19756 "k = 4, int l = 5,\n"
19757 " int m = 6) {\n"
19758 " int j = 10;\n"
19759 " otherThing = 1;\n"
19760 "}",
19761 Alignment);
19762 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19763 " int i = 1;\n"
19764 " int j = 2;\n"
19765 " int big = 10000;\n"
19766 "}",
19767 Alignment);
19768 verifyFormat("class C {\n"
19769 "public:\n"
19770 " int i = 1;\n"
19771 " virtual void f() = 0;\n"
19772 "};",
19773 Alignment);
19774 verifyFormat("int i = 1;\n"
19775 "if (SomeType t = getSomething()) {\n"
19776 "}\n"
19777 "int j = 2;\n"
19778 "int big = 10000;",
19779 Alignment);
19780 verifyFormat("int j = 7;\n"
19781 "for (int k = 0; k < N; ++k) {\n"
19782 "}\n"
19783 "int j = 2;\n"
19784 "int big = 10000;\n"
19785 "}",
19786 Alignment);
19787 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
19788 verifyFormat("int i = 1;\n"
19789 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19790 " = someLooooooooooooooooongFunction();\n"
19791 "int j = 2;",
19792 Alignment);
19793 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
19794 verifyFormat("int i = 1;\n"
19795 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19796 " someLooooooooooooooooongFunction();\n"
19797 "int j = 2;",
19798 Alignment);
19799
19800 verifyFormat("auto lambda = []() {\n"
19801 " auto i = 0;\n"
19802 " return 0;\n"
19803 "};\n"
19804 "int i = 0;\n"
19805 "auto v = type{\n"
19806 " i = 1, //\n"
19807 " (i = 2), //\n"
19808 " i = 3 //\n"
19809 "};",
19810 Alignment);
19811
19812 verifyFormat(
19813 "int i = 1;\n"
19814 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19815 " loooooooooooooooooooooongParameterB);\n"
19816 "int j = 2;",
19817 Alignment);
19818
19819 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19820 " typename B = very_long_type_name_1,\n"
19821 " typename T_2 = very_long_type_name_2>\n"
19822 "auto foo() {}",
19823 Alignment);
19824 verifyFormat("int a, b = 1;\n"
19825 "int c = 2;\n"
19826 "int dd = 3;",
19827 Alignment);
19828 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19829 "float b[1][] = {{3.f}};",
19830 Alignment);
19831 verifyFormat("for (int i = 0; i < 1; i++)\n"
19832 " int x = 1;",
19833 Alignment);
19834 verifyFormat("for (i = 0; i < 1; i++)\n"
19835 " x = 1;\n"
19836 "y = 1;",
19837 Alignment);
19838
19839 EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always);
19840 Alignment.ColumnLimit = 50;
19841 verifyFormat("int x = 0;\n"
19842 "int yy = 1; /// specificlennospace\n"
19843 "int zzz = 2;",
19844 "int x = 0;\n"
19845 "int yy = 1; ///specificlennospace\n"
19846 "int zzz = 2;",
19847 Alignment);
19848
19849 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19850 "auto b = [] {\n"
19851 " f();\n"
19852 " return;\n"
19853 "};",
19854 Alignment);
19855 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19856 "auto b = g([] {\n"
19857 " f();\n"
19858 " return;\n"
19859 "});",
19860 Alignment);
19861 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19862 "auto b = g(param, [] {\n"
19863 " f();\n"
19864 " return;\n"
19865 "});",
19866 Alignment);
19867 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19868 "auto b = [] {\n"
19869 " if (condition) {\n"
19870 " return;\n"
19871 " }\n"
19872 "};",
19873 Alignment);
19874
19875 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19876 " ccc ? aaaaa : bbbbb,\n"
19877 " dddddddddddddddddddddddddd);",
19878 Alignment);
19879 // FIXME: https://llvm.org/PR53497
19880 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19881 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19882 // " ccc ? aaaaa : bbbbb,\n"
19883 // " dddddddddddddddddddddddddd);",
19884 // Alignment);
19885
19886 // Confirm proper handling of AlignConsecutiveAssignments with
19887 // BinPackArguments.
19888 // See https://llvm.org/PR55360
19889 Alignment = getLLVMStyleWithColumns(ColumnLimit: 50);
19890 Alignment.AlignConsecutiveAssignments.Enabled = true;
19891 Alignment.BinPackArguments = false;
19892 verifyFormat("int a_long_name = 1;\n"
19893 "auto b = B({a_long_name, a_long_name},\n"
19894 " {a_longer_name_for_wrap,\n"
19895 " a_longer_name_for_wrap});",
19896 Alignment);
19897 verifyFormat("int a_long_name = 1;\n"
19898 "auto b = B{{a_long_name, a_long_name},\n"
19899 " {a_longer_name_for_wrap,\n"
19900 " a_longer_name_for_wrap}};",
19901 Alignment);
19902
19903 Alignment = getLLVMStyleWithColumns(ColumnLimit: 60);
19904 Alignment.AlignConsecutiveAssignments.Enabled = true;
19905 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19906 "using I = std::conditional_t<II::value >= 0,\n"
19907 " std::ic<int, II::value + 1>,\n"
19908 " std::ic<int, -1>>;",
19909 Alignment);
19910 verifyFormat("SomeName = Foo;\n"
19911 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19912 " arrrrrrrrrrg);",
19913 Alignment);
19914
19915 Alignment.ColumnLimit = 80;
19916 Alignment.SpacesInAngles = FormatStyle::SIAS_Always;
19917 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19918 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19919 Alignment);
19920 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19921 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19922 Alignment);
19923
19924 Alignment.SpacesInParens = FormatStyle::SIPO_Custom;
19925 Alignment.SpacesInParensOptions.InCStyleCasts = true;
19926 verifyFormat("void **ptr = ( void ** )unkn;\n"
19927 "ptr = ( void ** )ptr[0];",
19928 Alignment);
19929 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19930 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19931 Alignment);
19932}
19933
19934TEST_F(FormatTest, AlignConsecutiveBitFields) {
19935 FormatStyle Alignment = getLLVMStyle();
19936 Alignment.AlignConsecutiveBitFields.Enabled = true;
19937 verifyFormat("int const a : 5;\n"
19938 "int oneTwoThree : 23;",
19939 Alignment);
19940
19941 // Initializers are allowed starting with c++2a
19942 verifyFormat("int const a : 5 = 1;\n"
19943 "int oneTwoThree : 23 = 0;",
19944 Alignment);
19945
19946 Alignment.AlignConsecutiveDeclarations.Enabled = true;
19947 verifyFormat("int const a : 5;\n"
19948 "int oneTwoThree : 23;",
19949 Alignment);
19950
19951 verifyFormat("int const a : 5; // comment\n"
19952 "int oneTwoThree : 23; // comment",
19953 Alignment);
19954
19955 verifyFormat("int const a : 5 = 1;\n"
19956 "int oneTwoThree : 23 = 0;",
19957 Alignment);
19958
19959 Alignment.AlignConsecutiveAssignments.Enabled = true;
19960 verifyFormat("int const a : 5 = 1;\n"
19961 "int oneTwoThree : 23 = 0;",
19962 Alignment);
19963 verifyFormat("int const a : 5 = {1};\n"
19964 "int oneTwoThree : 23 = 0;",
19965 Alignment);
19966
19967 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None;
19968 verifyFormat("int const a :5;\n"
19969 "int oneTwoThree:23;",
19970 Alignment);
19971
19972 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before;
19973 verifyFormat("int const a :5;\n"
19974 "int oneTwoThree :23;",
19975 Alignment);
19976
19977 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After;
19978 verifyFormat("int const a : 5;\n"
19979 "int oneTwoThree: 23;",
19980 Alignment);
19981
19982 // Known limitations: ':' is only recognized as a bitfield colon when
19983 // followed by a number.
19984 /*
19985 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19986 "int a : 5;",
19987 Alignment);
19988 */
19989}
19990
19991TEST_F(FormatTest, AlignConsecutiveDeclarations) {
19992 FormatStyle Alignment = getLLVMStyle();
19993 Alignment.AlignConsecutiveMacros.Enabled = true;
19994 Alignment.PointerAlignment = FormatStyle::PAS_Right;
19995 verifyFormat("float const a = 5;\n"
19996 "int oneTwoThree = 123;",
19997 Alignment);
19998 verifyFormat("int a = 5;\n"
19999 "float const oneTwoThree = 123;",
20000 Alignment);
20001
20002 Alignment.AlignConsecutiveDeclarations.Enabled = true;
20003 verifyFormat("float const a = 5;\n"
20004 "int oneTwoThree = 123;",
20005 Alignment);
20006 verifyFormat("int a = method();\n"
20007 "float const oneTwoThree = 133;",
20008 Alignment);
20009 verifyFormat("int i = 1, j = 10;\n"
20010 "something = 2000;",
20011 Alignment);
20012 verifyFormat("something = 2000;\n"
20013 "int i = 1, j = 10;",
20014 Alignment);
20015 verifyFormat("float something = 2000;\n"
20016 "double another = 911;\n"
20017 "int i = 1, j = 10;\n"
20018 "const int *oneMore = 1;\n"
20019 "unsigned i = 2;",
20020 Alignment);
20021 verifyFormat("float a = 5;\n"
20022 "int one = 1;\n"
20023 "method();\n"
20024 "const double oneTwoThree = 123;\n"
20025 "const unsigned int oneTwo = 12;",
20026 Alignment);
20027 verifyFormat("int oneTwoThree{0}; // comment\n"
20028 "unsigned oneTwo; // comment",
20029 Alignment);
20030 verifyFormat("unsigned int *a;\n"
20031 "int *b;\n"
20032 "unsigned int Const *c;\n"
20033 "unsigned int const *d;\n"
20034 "unsigned int Const &e;\n"
20035 "unsigned int const &f;",
20036 Alignment);
20037 verifyFormat("Const unsigned int *c;\n"
20038 "const unsigned int *d;\n"
20039 "Const unsigned int &e;\n"
20040 "const unsigned int &f;\n"
20041 "const unsigned g;\n"
20042 "Const unsigned h;",
20043 Alignment);
20044 verifyFormat("float const a = 5;\n"
20045 "\n"
20046 "int oneTwoThree = 123;",
20047 "float const a = 5;\n"
20048 "\n"
20049 "int oneTwoThree= 123;",
20050 Alignment);
20051 verifyFormat("float a = 5;\n"
20052 "int one = 1;\n"
20053 "\n"
20054 "unsigned oneTwoThree = 123;",
20055 "float a = 5;\n"
20056 "int one = 1;\n"
20057 "\n"
20058 "unsigned oneTwoThree = 123;",
20059 Alignment);
20060 verifyFormat("float a = 5;\n"
20061 "int one = 1;\n"
20062 "\n"
20063 "unsigned oneTwoThree = 123;\n"
20064 "int oneTwo = 12;",
20065 "float a = 5;\n"
20066 "int one = 1;\n"
20067 "\n"
20068 "unsigned oneTwoThree = 123;\n"
20069 "int oneTwo = 12;",
20070 Alignment);
20071 // Function prototype alignment
20072 verifyFormat("int a();\n"
20073 "double b();",
20074 Alignment);
20075 verifyFormat("int a(int x);\n"
20076 "double b();",
20077 Alignment);
20078 verifyFormat("int a(const Test & = Test());\n"
20079 "int a1(int &foo, const Test & = Test());\n"
20080 "int a2(int &foo, const Test &name = Test());\n"
20081 "double b();",
20082 Alignment);
20083 verifyFormat("struct Test {\n"
20084 " Test(const Test &) = default;\n"
20085 " ~Test() = default;\n"
20086 " Test &operator=(const Test &) = default;\n"
20087 "};",
20088 Alignment);
20089 unsigned OldColumnLimit = Alignment.ColumnLimit;
20090 // We need to set ColumnLimit to zero, in order to stress nested alignments,
20091 // otherwise the function parameters will be re-flowed onto a single line.
20092 Alignment.ColumnLimit = 0;
20093 verifyFormat("int a(int x,\n"
20094 " float y);\n"
20095 "double b(int x,\n"
20096 " double y);",
20097 "int a(int x,\n"
20098 " float y);\n"
20099 "double b(int x,\n"
20100 " double y);",
20101 Alignment);
20102 // This ensures that function parameters of function declarations are
20103 // correctly indented when their owning functions are indented.
20104 // The failure case here is for 'double y' to not be indented enough.
20105 verifyFormat("double a(int x);\n"
20106 "int b(int y,\n"
20107 " double z);",
20108 "double a(int x);\n"
20109 "int b(int y,\n"
20110 " double z);",
20111 Alignment);
20112 // Set ColumnLimit low so that we induce wrapping immediately after
20113 // the function name and opening paren.
20114 Alignment.ColumnLimit = 13;
20115 verifyFormat("int function(\n"
20116 " int x,\n"
20117 " bool y);",
20118 Alignment);
20119 // Set ColumnLimit low so that we break the argument list in multiple lines.
20120 Alignment.ColumnLimit = 35;
20121 verifyFormat("int a3(SomeTypeName1 &x,\n"
20122 " SomeTypeName2 &y,\n"
20123 " const Test & = Test());\n"
20124 "double b();",
20125 Alignment);
20126 Alignment.ColumnLimit = OldColumnLimit;
20127 // Ensure function pointers don't screw up recursive alignment
20128 verifyFormat("int a(int x, void (*fp)(int y));\n"
20129 "double b();",
20130 Alignment);
20131 Alignment.AlignConsecutiveAssignments.Enabled = true;
20132 verifyFormat("struct Test {\n"
20133 " Test(const Test &) = default;\n"
20134 " ~Test() = default;\n"
20135 " Test &operator=(const Test &) = default;\n"
20136 "};",
20137 Alignment);
20138 // Ensure recursive alignment is broken by function braces, so that the
20139 // "a = 1" does not align with subsequent assignments inside the function
20140 // body.
20141 verifyFormat("int func(int a = 1) {\n"
20142 " int b = 2;\n"
20143 " int cc = 3;\n"
20144 "}",
20145 Alignment);
20146 verifyFormat("float something = 2000;\n"
20147 "double another = 911;\n"
20148 "int i = 1, j = 10;\n"
20149 "const int *oneMore = 1;\n"
20150 "unsigned i = 2;",
20151 Alignment);
20152 verifyFormat("int oneTwoThree = {0}; // comment\n"
20153 "unsigned oneTwo = 0; // comment",
20154 Alignment);
20155 // Make sure that scope is correctly tracked, in the absence of braces
20156 verifyFormat("for (int i = 0; i < n; i++)\n"
20157 " j = i;\n"
20158 "double x = 1;",
20159 Alignment);
20160 verifyFormat("if (int i = 0)\n"
20161 " j = i;\n"
20162 "double x = 1;",
20163 Alignment);
20164 // Ensure operator[] and operator() are comprehended
20165 verifyFormat("struct test {\n"
20166 " long long int foo();\n"
20167 " int operator[](int a);\n"
20168 " double bar();\n"
20169 "};",
20170 Alignment);
20171 verifyFormat("struct test {\n"
20172 " long long int foo();\n"
20173 " int operator()(int a);\n"
20174 " double bar();\n"
20175 "};",
20176 Alignment);
20177 // http://llvm.org/PR52914
20178 verifyFormat("char *a[] = {\"a\", // comment\n"
20179 " \"bb\"};\n"
20180 "int bbbbbbb = 0;",
20181 Alignment);
20182 // http://llvm.org/PR68079
20183 verifyFormat("using Fn = int (A::*)();\n"
20184 "using RFn = int (A::*)() &;\n"
20185 "using RRFn = int (A::*)() &&;",
20186 Alignment);
20187 verifyFormat("using Fn = int (A::*)();\n"
20188 "using RFn = int *(A::*)() &;\n"
20189 "using RRFn = double (A::*)() &&;",
20190 Alignment);
20191
20192 // PAS_Right
20193 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20194 " int const i = 1;\n"
20195 " int *j = 2;\n"
20196 " int big = 10000;\n"
20197 "\n"
20198 " unsigned oneTwoThree = 123;\n"
20199 " int oneTwo = 12;\n"
20200 " method();\n"
20201 " float k = 2;\n"
20202 " int ll = 10000;\n"
20203 "}",
20204 "void SomeFunction(int parameter= 0) {\n"
20205 " int const i= 1;\n"
20206 " int *j=2;\n"
20207 " int big = 10000;\n"
20208 "\n"
20209 "unsigned oneTwoThree =123;\n"
20210 "int oneTwo = 12;\n"
20211 " method();\n"
20212 "float k= 2;\n"
20213 "int ll=10000;\n"
20214 "}",
20215 Alignment);
20216 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20217 " int const i = 1;\n"
20218 " int **j = 2, ***k;\n"
20219 " int &k = i;\n"
20220 " int &&l = i + j;\n"
20221 " int big = 10000;\n"
20222 "\n"
20223 " unsigned oneTwoThree = 123;\n"
20224 " int oneTwo = 12;\n"
20225 " method();\n"
20226 " float k = 2;\n"
20227 " int ll = 10000;\n"
20228 "}",
20229 "void SomeFunction(int parameter= 0) {\n"
20230 " int const i= 1;\n"
20231 " int **j=2,***k;\n"
20232 "int &k=i;\n"
20233 "int &&l=i+j;\n"
20234 " int big = 10000;\n"
20235 "\n"
20236 "unsigned oneTwoThree =123;\n"
20237 "int oneTwo = 12;\n"
20238 " method();\n"
20239 "float k= 2;\n"
20240 "int ll=10000;\n"
20241 "}",
20242 Alignment);
20243 // variables are aligned at their name, pointers are at the right most
20244 // position
20245 verifyFormat("int *a;\n"
20246 "int **b;\n"
20247 "int ***c;\n"
20248 "int foobar;",
20249 Alignment);
20250
20251 // PAS_Left
20252 FormatStyle AlignmentLeft = Alignment;
20253 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left;
20254 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20255 " int const i = 1;\n"
20256 " int* j = 2;\n"
20257 " int big = 10000;\n"
20258 "\n"
20259 " unsigned oneTwoThree = 123;\n"
20260 " int oneTwo = 12;\n"
20261 " method();\n"
20262 " float k = 2;\n"
20263 " int ll = 10000;\n"
20264 "}",
20265 "void SomeFunction(int parameter= 0) {\n"
20266 " int const i= 1;\n"
20267 " int *j=2;\n"
20268 " int big = 10000;\n"
20269 "\n"
20270 "unsigned oneTwoThree =123;\n"
20271 "int oneTwo = 12;\n"
20272 " method();\n"
20273 "float k= 2;\n"
20274 "int ll=10000;\n"
20275 "}",
20276 AlignmentLeft);
20277 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20278 " int const i = 1;\n"
20279 " int** j = 2;\n"
20280 " int& k = i;\n"
20281 " int&& l = i + j;\n"
20282 " int big = 10000;\n"
20283 "\n"
20284 " unsigned oneTwoThree = 123;\n"
20285 " int oneTwo = 12;\n"
20286 " method();\n"
20287 " float k = 2;\n"
20288 " int ll = 10000;\n"
20289 "}",
20290 "void SomeFunction(int parameter= 0) {\n"
20291 " int const i= 1;\n"
20292 " int **j=2;\n"
20293 "int &k=i;\n"
20294 "int &&l=i+j;\n"
20295 " int big = 10000;\n"
20296 "\n"
20297 "unsigned oneTwoThree =123;\n"
20298 "int oneTwo = 12;\n"
20299 " method();\n"
20300 "float k= 2;\n"
20301 "int ll=10000;\n"
20302 "}",
20303 AlignmentLeft);
20304 // variables are aligned at their name, pointers are at the left most position
20305 verifyFormat("int* a;\n"
20306 "int** b;\n"
20307 "int*** c;\n"
20308 "int foobar;",
20309 AlignmentLeft);
20310
20311 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
20312 "double b();",
20313 AlignmentLeft);
20314
20315 auto Style = AlignmentLeft;
20316 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true;
20317 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
20318 verifyFormat("int function_name(const wchar_t* title,\n"
20319 " int x = 0,\n"
20320 " long extraStyle = 0,\n"
20321 " bool readOnly = false,\n"
20322 " FancyClassType* module = nullptr);",
20323 Style);
20324
20325 // PAS_Middle
20326 FormatStyle AlignmentMiddle = Alignment;
20327 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
20328 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20329 " int const i = 1;\n"
20330 " int * j = 2;\n"
20331 " int big = 10000;\n"
20332 "\n"
20333 " unsigned oneTwoThree = 123;\n"
20334 " int oneTwo = 12;\n"
20335 " method();\n"
20336 " float k = 2;\n"
20337 " int ll = 10000;\n"
20338 "}",
20339 "void SomeFunction(int parameter= 0) {\n"
20340 " int const i= 1;\n"
20341 " int *j=2;\n"
20342 " int big = 10000;\n"
20343 "\n"
20344 "unsigned oneTwoThree =123;\n"
20345 "int oneTwo = 12;\n"
20346 " method();\n"
20347 "float k= 2;\n"
20348 "int ll=10000;\n"
20349 "}",
20350 AlignmentMiddle);
20351 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20352 " int const i = 1;\n"
20353 " int ** j = 2, ***k;\n"
20354 " int & k = i;\n"
20355 " int && l = i + j;\n"
20356 " int big = 10000;\n"
20357 "\n"
20358 " unsigned oneTwoThree = 123;\n"
20359 " int oneTwo = 12;\n"
20360 " method();\n"
20361 " float k = 2;\n"
20362 " int ll = 10000;\n"
20363 "}",
20364 "void SomeFunction(int parameter= 0) {\n"
20365 " int const i= 1;\n"
20366 " int **j=2,***k;\n"
20367 "int &k=i;\n"
20368 "int &&l=i+j;\n"
20369 " int big = 10000;\n"
20370 "\n"
20371 "unsigned oneTwoThree =123;\n"
20372 "int oneTwo = 12;\n"
20373 " method();\n"
20374 "float k= 2;\n"
20375 "int ll=10000;\n"
20376 "}",
20377 AlignmentMiddle);
20378 // variables are aligned at their name, pointers are in the middle
20379 verifyFormat("int * a;\n"
20380 "int * b;\n"
20381 "int *** c;\n"
20382 "int foobar;",
20383 AlignmentMiddle);
20384
20385 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
20386 "double b();",
20387 AlignmentMiddle);
20388
20389 Alignment.AlignConsecutiveAssignments.Enabled = false;
20390 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
20391 verifyFormat("#define A \\\n"
20392 " int aaaa = 12; \\\n"
20393 " float b = 23; \\\n"
20394 " const int ccc = 234; \\\n"
20395 " unsigned dddddddddd = 2345;",
20396 Alignment);
20397 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
20398 verifyFormat("#define A \\\n"
20399 " int aaaa = 12; \\\n"
20400 " float b = 23; \\\n"
20401 " const int ccc = 234; \\\n"
20402 " unsigned dddddddddd = 2345;",
20403 Alignment);
20404 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
20405 Alignment.ColumnLimit = 30;
20406 verifyFormat("#define A \\\n"
20407 " int aaaa = 12; \\\n"
20408 " float b = 23; \\\n"
20409 " const int ccc = 234; \\\n"
20410 " int dddddddddd = 2345;",
20411 Alignment);
20412 Alignment.ColumnLimit = 80;
20413 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
20414 "k = 4, int l = 5,\n"
20415 " int m = 6) {\n"
20416 " const int j = 10;\n"
20417 " otherThing = 1;\n"
20418 "}",
20419 Alignment);
20420 verifyFormat("void SomeFunction(int parameter = 0) {\n"
20421 " int const i = 1;\n"
20422 " int *j = 2;\n"
20423 " int big = 10000;\n"
20424 "}",
20425 Alignment);
20426 verifyFormat("class C {\n"
20427 "public:\n"
20428 " int i = 1;\n"
20429 " virtual void f() = 0;\n"
20430 "};",
20431 Alignment);
20432 verifyFormat("float i = 1;\n"
20433 "if (SomeType t = getSomething()) {\n"
20434 "}\n"
20435 "const unsigned j = 2;\n"
20436 "int big = 10000;",
20437 Alignment);
20438 verifyFormat("float j = 7;\n"
20439 "for (int k = 0; k < N; ++k) {\n"
20440 "}\n"
20441 "unsigned j = 2;\n"
20442 "int big = 10000;\n"
20443 "}",
20444 Alignment);
20445 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
20446 verifyFormat("float i = 1;\n"
20447 "LooooooooooongType loooooooooooooooooooooongVariable\n"
20448 " = someLooooooooooooooooongFunction();\n"
20449 "int j = 2;",
20450 Alignment);
20451 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
20452 verifyFormat("int i = 1;\n"
20453 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
20454 " someLooooooooooooooooongFunction();\n"
20455 "int j = 2;",
20456 Alignment);
20457
20458 Alignment.AlignConsecutiveAssignments.Enabled = true;
20459 verifyFormat("auto lambda = []() {\n"
20460 " auto ii = 0;\n"
20461 " float j = 0;\n"
20462 " return 0;\n"
20463 "};\n"
20464 "int i = 0;\n"
20465 "float i2 = 0;\n"
20466 "auto v = type{\n"
20467 " i = 1, //\n"
20468 " (i = 2), //\n"
20469 " i = 3 //\n"
20470 "};",
20471 Alignment);
20472 Alignment.AlignConsecutiveAssignments.Enabled = false;
20473
20474 verifyFormat(
20475 "int i = 1;\n"
20476 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
20477 " loooooooooooooooooooooongParameterB);\n"
20478 "int j = 2;",
20479 Alignment);
20480
20481 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
20482 // We expect declarations and assignments to align, as long as it doesn't
20483 // exceed the column limit, starting a new alignment sequence whenever it
20484 // happens.
20485 Alignment.AlignConsecutiveAssignments.Enabled = true;
20486 Alignment.ColumnLimit = 30;
20487 verifyFormat("float ii = 1;\n"
20488 "unsigned j = 2;\n"
20489 "int someVerylongVariable = 1;\n"
20490 "AnotherLongType ll = 123456;\n"
20491 "VeryVeryLongType k = 2;\n"
20492 "int myvar = 1;",
20493 Alignment);
20494 Alignment.ColumnLimit = 80;
20495 Alignment.AlignConsecutiveAssignments.Enabled = false;
20496
20497 verifyFormat(
20498 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
20499 " typename LongType, typename B>\n"
20500 "auto foo() {}",
20501 Alignment);
20502 verifyFormat("float a, b = 1;\n"
20503 "int c = 2;\n"
20504 "int dd = 3;",
20505 Alignment);
20506 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
20507 "float b[1][] = {{3.f}};",
20508 Alignment);
20509 Alignment.AlignConsecutiveAssignments.Enabled = true;
20510 verifyFormat("float a, b = 1;\n"
20511 "int c = 2;\n"
20512 "int dd = 3;",
20513 Alignment);
20514 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
20515 "float b[1][] = {{3.f}};",
20516 Alignment);
20517 Alignment.AlignConsecutiveAssignments.Enabled = false;
20518
20519 Alignment.ColumnLimit = 30;
20520 Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine;
20521 verifyFormat("void foo(float a,\n"
20522 " float b,\n"
20523 " int c,\n"
20524 " uint32_t *d) {\n"
20525 " int *e = 0;\n"
20526 " float f = 0;\n"
20527 " double g = 0;\n"
20528 "}\n"
20529 "void bar(ino_t a,\n"
20530 " int b,\n"
20531 " uint32_t *c,\n"
20532 " bool d) {}",
20533 Alignment);
20534 Alignment.BinPackParameters = FormatStyle::BPPS_BinPack;
20535 Alignment.ColumnLimit = 80;
20536
20537 // Bug 33507
20538 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
20539 verifyFormat(
20540 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
20541 " static const Version verVs2017;\n"
20542 " return true;\n"
20543 "});",
20544 Alignment);
20545 Alignment.PointerAlignment = FormatStyle::PAS_Right;
20546
20547 // See llvm.org/PR35641
20548 Alignment.AlignConsecutiveDeclarations.Enabled = true;
20549 verifyFormat("int func() { //\n"
20550 " int b;\n"
20551 " unsigned c;\n"
20552 "}",
20553 Alignment);
20554
20555 // See PR37175
20556 Style = getMozillaStyle();
20557 Style.AlignConsecutiveDeclarations.Enabled = true;
20558 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
20559 "foo(int a);",
20560 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style);
20561
20562 Alignment.PointerAlignment = FormatStyle::PAS_Left;
20563 verifyFormat("unsigned int* a;\n"
20564 "int* b;\n"
20565 "unsigned int Const* c;\n"
20566 "unsigned int const* d;\n"
20567 "unsigned int Const& e;\n"
20568 "unsigned int const& f;",
20569 Alignment);
20570 verifyFormat("Const unsigned int* c;\n"
20571 "const unsigned int* d;\n"
20572 "Const unsigned int& e;\n"
20573 "const unsigned int& f;\n"
20574 "const unsigned g;\n"
20575 "Const unsigned h;",
20576 Alignment);
20577
20578 Alignment.PointerAlignment = FormatStyle::PAS_Middle;
20579 verifyFormat("unsigned int * a;\n"
20580 "int * b;\n"
20581 "unsigned int Const * c;\n"
20582 "unsigned int const * d;\n"
20583 "unsigned int Const & e;\n"
20584 "unsigned int const & f;",
20585 Alignment);
20586 verifyFormat("Const unsigned int * c;\n"
20587 "const unsigned int * d;\n"
20588 "Const unsigned int & e;\n"
20589 "const unsigned int & f;\n"
20590 "const unsigned g;\n"
20591 "Const unsigned h;",
20592 Alignment);
20593
20594 // See PR46529
20595 FormatStyle BracedAlign = getLLVMStyle();
20596 BracedAlign.AlignConsecutiveDeclarations.Enabled = true;
20597 verifyFormat("const auto result{[]() {\n"
20598 " const auto something = 1;\n"
20599 " return 2;\n"
20600 "}};",
20601 BracedAlign);
20602 verifyFormat("int foo{[]() {\n"
20603 " int bar{0};\n"
20604 " return 0;\n"
20605 "}()};",
20606 BracedAlign);
20607 BracedAlign.Cpp11BracedListStyle = false;
20608 verifyFormat("const auto result{ []() {\n"
20609 " const auto something = 1;\n"
20610 " return 2;\n"
20611 "} };",
20612 BracedAlign);
20613 verifyFormat("int foo{ []() {\n"
20614 " int bar{ 0 };\n"
20615 " return 0;\n"
20616 "}() };",
20617 BracedAlign);
20618
20619 Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
20620 verifyFormat("unsigned int f1(void);\n"
20621 "void f2(void);\n"
20622 "size_t f3(void);",
20623 Alignment);
20624}
20625
20626TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
20627 FormatStyle Alignment = getLLVMStyle();
20628 Alignment.AllowShortCaseLabelsOnASingleLine = true;
20629 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true;
20630
20631 verifyFormat("switch (level) {\n"
20632 "case log::info: return \"info\";\n"
20633 "case log::warning: return \"warning\";\n"
20634 "default: return \"default\";\n"
20635 "}",
20636 Alignment);
20637
20638 verifyFormat("switch (level) {\n"
20639 "case log::info: return \"info\";\n"
20640 "case log::warning: return \"warning\";\n"
20641 "}",
20642 "switch (level) {\n"
20643 "case log::info: return \"info\";\n"
20644 "case log::warning:\n"
20645 " return \"warning\";\n"
20646 "}",
20647 Alignment);
20648
20649 // Empty case statements push out the alignment, but non-short case labels
20650 // don't.
20651 verifyFormat("switch (level) {\n"
20652 "case log::info: return \"info\";\n"
20653 "case log::critical:\n"
20654 "case log::warning:\n"
20655 "case log::severe: return \"severe\";\n"
20656 "case log::extra_severe:\n"
20657 " // comment\n"
20658 " return \"extra_severe\";\n"
20659 "}",
20660 Alignment);
20661
20662 // Verify comments and empty lines break the alignment.
20663 verifyNoChange("switch (level) {\n"
20664 "case log::info: return \"info\";\n"
20665 "case log::warning: return \"warning\";\n"
20666 "// comment\n"
20667 "case log::critical: return \"critical\";\n"
20668 "default: return \"default\";\n"
20669 "\n"
20670 "case log::severe: return \"severe\";\n"
20671 "}",
20672 Alignment);
20673
20674 // Empty case statements don't break the alignment, and potentially push it
20675 // out.
20676 verifyFormat("switch (level) {\n"
20677 "case log::info: return \"info\";\n"
20678 "case log::warning:\n"
20679 "case log::critical:\n"
20680 "default: return \"default\";\n"
20681 "}",
20682 Alignment);
20683
20684 // Implicit fallthrough cases can be aligned with either a comment or
20685 // [[fallthrough]]
20686 verifyFormat("switch (level) {\n"
20687 "case log::info: return \"info\";\n"
20688 "case log::warning: // fallthrough\n"
20689 "case log::error: return \"error\";\n"
20690 "case log::critical: /*fallthrough*/\n"
20691 "case log::severe: return \"severe\";\n"
20692 "case log::diag: [[fallthrough]];\n"
20693 "default: return \"default\";\n"
20694 "}",
20695 Alignment);
20696
20697 // Verify trailing comment that needs a reflow also gets aligned properly.
20698 verifyFormat("switch (level) {\n"
20699 "case log::info: return \"info\";\n"
20700 "case log::warning: // fallthrough\n"
20701 "case log::error: return \"error\";\n"
20702 "}",
20703 "switch (level) {\n"
20704 "case log::info: return \"info\";\n"
20705 "case log::warning: //fallthrough\n"
20706 "case log::error: return \"error\";\n"
20707 "}",
20708 Alignment);
20709
20710 // Verify adjacent non-short case statements don't change the alignment, and
20711 // properly break the set of consecutive statements.
20712 verifyFormat("switch (level) {\n"
20713 "case log::critical:\n"
20714 " // comment\n"
20715 " return \"critical\";\n"
20716 "case log::info: return \"info\";\n"
20717 "case log::warning: return \"warning\";\n"
20718 "default:\n"
20719 " // comment\n"
20720 " return \"\";\n"
20721 "case log::error: return \"error\";\n"
20722 "case log::severe: return \"severe\";\n"
20723 "case log::extra_critical:\n"
20724 " // comment\n"
20725 " return \"extra critical\";\n"
20726 "}",
20727 Alignment);
20728
20729 Alignment.SpaceBeforeCaseColon = true;
20730 verifyFormat("switch (level) {\n"
20731 "case log::info : return \"info\";\n"
20732 "case log::warning : return \"warning\";\n"
20733 "default : return \"default\";\n"
20734 "}",
20735 Alignment);
20736 Alignment.SpaceBeforeCaseColon = false;
20737
20738 // Make sure we don't incorrectly align correctly across nested switch cases.
20739 verifyFormat("switch (level) {\n"
20740 "case log::info: return \"info\";\n"
20741 "case log::warning: return \"warning\";\n"
20742 "case log::other:\n"
20743 " switch (sublevel) {\n"
20744 " case log::info: return \"info\";\n"
20745 " case log::warning: return \"warning\";\n"
20746 " }\n"
20747 " break;\n"
20748 "case log::error: return \"error\";\n"
20749 "default: return \"default\";\n"
20750 "}",
20751 "switch (level) {\n"
20752 "case log::info: return \"info\";\n"
20753 "case log::warning: return \"warning\";\n"
20754 "case log::other: switch (sublevel) {\n"
20755 " case log::info: return \"info\";\n"
20756 " case log::warning: return \"warning\";\n"
20757 "}\n"
20758 "break;\n"
20759 "case log::error: return \"error\";\n"
20760 "default: return \"default\";\n"
20761 "}",
20762 Alignment);
20763
20764 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true;
20765
20766 verifyFormat("switch (level) {\n"
20767 "case log::info: return \"info\";\n"
20768 "\n"
20769 "case log::warning: return \"warning\";\n"
20770 "}",
20771 "switch (level) {\n"
20772 "case log::info: return \"info\";\n"
20773 "\n"
20774 "case log::warning: return \"warning\";\n"
20775 "}",
20776 Alignment);
20777
20778 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true;
20779
20780 verifyNoChange("switch (level) {\n"
20781 "case log::info: return \"info\";\n"
20782 "\n"
20783 "/* block comment */\n"
20784 "\n"
20785 "// line comment\n"
20786 "case log::warning: return \"warning\";\n"
20787 "}",
20788 Alignment);
20789
20790 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false;
20791
20792 verifyFormat("switch (level) {\n"
20793 "case log::info: return \"info\";\n"
20794 "//\n"
20795 "case log::warning: return \"warning\";\n"
20796 "}",
20797 Alignment);
20798
20799 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true;
20800
20801 verifyFormat("switch (level) {\n"
20802 "case log::info : return \"info\";\n"
20803 "case log::warning: return \"warning\";\n"
20804 "default : return \"default\";\n"
20805 "}",
20806 Alignment);
20807
20808 // With AlignCaseColons, empty case statements don't break alignment of
20809 // consecutive case statements (and are aligned).
20810 verifyFormat("switch (level) {\n"
20811 "case log::info : return \"info\";\n"
20812 "case log::warning :\n"
20813 "case log::critical:\n"
20814 "default : return \"default\";\n"
20815 "}",
20816 Alignment);
20817
20818 // Final non-short case labels shouldn't have their colon aligned
20819 verifyFormat("switch (level) {\n"
20820 "case log::info : return \"info\";\n"
20821 "case log::warning :\n"
20822 "case log::critical:\n"
20823 "case log::severe : return \"severe\";\n"
20824 "default:\n"
20825 " // comment\n"
20826 " return \"default\";\n"
20827 "}",
20828 Alignment);
20829
20830 // Verify adjacent non-short case statements break the set of consecutive
20831 // alignments and aren't aligned with adjacent non-short case statements if
20832 // AlignCaseColons is set.
20833 verifyFormat("switch (level) {\n"
20834 "case log::critical:\n"
20835 " // comment\n"
20836 " return \"critical\";\n"
20837 "case log::info : return \"info\";\n"
20838 "case log::warning: return \"warning\";\n"
20839 "default:\n"
20840 " // comment\n"
20841 " return \"\";\n"
20842 "case log::error : return \"error\";\n"
20843 "case log::severe: return \"severe\";\n"
20844 "case log::extra_critical:\n"
20845 " // comment\n"
20846 " return \"extra critical\";\n"
20847 "}",
20848 Alignment);
20849
20850 Alignment.SpaceBeforeCaseColon = true;
20851 verifyFormat("switch (level) {\n"
20852 "case log::info : return \"info\";\n"
20853 "case log::warning : return \"warning\";\n"
20854 "case log::error :\n"
20855 "default : return \"default\";\n"
20856 "}",
20857 Alignment);
20858}
20859
20860TEST_F(FormatTest, AlignWithLineBreaks) {
20861 auto Style = getLLVMStyleWithColumns(ColumnLimit: 120);
20862
20863 EXPECT_EQ(Style.AlignConsecutiveAssignments,
20864 FormatStyle::AlignConsecutiveStyle(
20865 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20866 /*AcrossComments=*/false, /*AlignCompound=*/false,
20867 /*AlignFunctionDeclarations=*/false,
20868 /*AlignFunctionPointers=*/false,
20869 /*PadOperators=*/true}));
20870 EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20871 FormatStyle::AlignConsecutiveStyle(
20872 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20873 /*AcrossComments=*/false, /*AlignCompound=*/false,
20874 /*AlignFunctionDeclarations=*/true,
20875 /*AlignFunctionPointers=*/false,
20876 /*PadOperators=*/false}));
20877 verifyFormat("void foo() {\n"
20878 " int myVar = 5;\n"
20879 " double x = 3.14;\n"
20880 " auto str = \"Hello \"\n"
20881 " \"World\";\n"
20882 " auto s = \"Hello \"\n"
20883 " \"Again\";\n"
20884 "}",
20885 Style);
20886
20887 // clang-format off
20888 verifyFormat("void foo() {\n"
20889 " const int capacityBefore = Entries.capacity();\n"
20890 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20891 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20892 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20893 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20894 "}",
20895 Style);
20896 // clang-format on
20897
20898 Style.AlignConsecutiveAssignments.Enabled = true;
20899 verifyFormat("void foo() {\n"
20900 " int myVar = 5;\n"
20901 " double x = 3.14;\n"
20902 " auto str = \"Hello \"\n"
20903 " \"World\";\n"
20904 " auto s = \"Hello \"\n"
20905 " \"Again\";\n"
20906 "}",
20907 Style);
20908
20909 // clang-format off
20910 verifyFormat("void foo() {\n"
20911 " const int capacityBefore = Entries.capacity();\n"
20912 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20913 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20914 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20915 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20916 "}",
20917 Style);
20918 // clang-format on
20919
20920 Style.AlignConsecutiveAssignments.Enabled = false;
20921 Style.AlignConsecutiveDeclarations.Enabled = true;
20922 verifyFormat("void foo() {\n"
20923 " int myVar = 5;\n"
20924 " double x = 3.14;\n"
20925 " auto str = \"Hello \"\n"
20926 " \"World\";\n"
20927 " auto s = \"Hello \"\n"
20928 " \"Again\";\n"
20929 "}",
20930 Style);
20931
20932 // clang-format off
20933 verifyFormat("void foo() {\n"
20934 " const int capacityBefore = Entries.capacity();\n"
20935 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20936 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20937 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20938 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20939 "}",
20940 Style);
20941 // clang-format on
20942
20943 Style.AlignConsecutiveAssignments.Enabled = true;
20944 Style.AlignConsecutiveDeclarations.Enabled = true;
20945
20946 verifyFormat("void foo() {\n"
20947 " int myVar = 5;\n"
20948 " double x = 3.14;\n"
20949 " auto str = \"Hello \"\n"
20950 " \"World\";\n"
20951 " auto s = \"Hello \"\n"
20952 " \"Again\";\n"
20953 "}",
20954 Style);
20955
20956 // clang-format off
20957 verifyFormat("void foo() {\n"
20958 " const int capacityBefore = Entries.capacity();\n"
20959 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20960 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20961 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20962 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20963 "}",
20964 Style);
20965 // clang-format on
20966
20967 Style = getLLVMStyleWithColumns(ColumnLimit: 20);
20968 Style.AlignConsecutiveAssignments.Enabled = true;
20969 Style.IndentWidth = 4;
20970
20971 verifyFormat("void foo() {\n"
20972 " int i1 = 1;\n"
20973 " int j = 0;\n"
20974 " int k = bar(\n"
20975 " argument1,\n"
20976 " argument2);\n"
20977 "}",
20978 Style);
20979
20980 verifyFormat("unsigned i = 0;\n"
20981 "int a[] = {\n"
20982 " 1234567890,\n"
20983 " -1234567890};",
20984 Style);
20985
20986 Style.ColumnLimit = 120;
20987
20988 // clang-format off
20989 verifyFormat("void SomeFunc() {\n"
20990 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20991 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20992 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20993 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20994 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20995 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20996 "}",
20997 Style);
20998 // clang-format on
20999
21000 Style.BinPackArguments = false;
21001
21002 // clang-format off
21003 verifyFormat("void SomeFunc() {\n"
21004 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
21005 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
21006 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
21007 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
21008 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
21009 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
21010 "}",
21011 Style);
21012 // clang-format on
21013}
21014
21015TEST_F(FormatTest, AlignWithInitializerPeriods) {
21016 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
21017
21018 verifyFormat("void foo1(void) {\n"
21019 " BYTE p[1] = 1;\n"
21020 " A B = {.one_foooooooooooooooo = 2,\n"
21021 " .two_fooooooooooooo = 3,\n"
21022 " .three_fooooooooooooo = 4};\n"
21023 " BYTE payload = 2;\n"
21024 "}",
21025 Style);
21026
21027 Style.AlignConsecutiveAssignments.Enabled = true;
21028 Style.AlignConsecutiveDeclarations.Enabled = false;
21029 verifyFormat("void foo2(void) {\n"
21030 " BYTE p[1] = 1;\n"
21031 " A B = {.one_foooooooooooooooo = 2,\n"
21032 " .two_fooooooooooooo = 3,\n"
21033 " .three_fooooooooooooo = 4};\n"
21034 " BYTE payload = 2;\n"
21035 "}",
21036 Style);
21037
21038 Style.AlignConsecutiveAssignments.Enabled = false;
21039 Style.AlignConsecutiveDeclarations.Enabled = true;
21040 verifyFormat("void foo3(void) {\n"
21041 " BYTE p[1] = 1;\n"
21042 " A B = {.one_foooooooooooooooo = 2,\n"
21043 " .two_fooooooooooooo = 3,\n"
21044 " .three_fooooooooooooo = 4};\n"
21045 " BYTE payload = 2;\n"
21046 "}",
21047 Style);
21048
21049 Style.AlignConsecutiveAssignments.Enabled = true;
21050 Style.AlignConsecutiveDeclarations.Enabled = true;
21051 verifyFormat("void foo4(void) {\n"
21052 " BYTE p[1] = 1;\n"
21053 " A B = {.one_foooooooooooooooo = 2,\n"
21054 " .two_fooooooooooooo = 3,\n"
21055 " .three_fooooooooooooo = 4};\n"
21056 " BYTE payload = 2;\n"
21057 "}",
21058 Style);
21059}
21060
21061TEST_F(FormatTest, LinuxBraceBreaking) {
21062 FormatStyle LinuxBraceStyle = getLLVMStyle();
21063 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
21064 verifyFormat("namespace a\n"
21065 "{\n"
21066 "class A\n"
21067 "{\n"
21068 " void f()\n"
21069 " {\n"
21070 " if (true) {\n"
21071 " a();\n"
21072 " b();\n"
21073 " } else {\n"
21074 " a();\n"
21075 " }\n"
21076 " }\n"
21077 " void g() { return; }\n"
21078 "};\n"
21079 "struct B {\n"
21080 " int x;\n"
21081 "};\n"
21082 "} // namespace a",
21083 LinuxBraceStyle);
21084 verifyFormat("enum X {\n"
21085 " Y = 0,\n"
21086 "}",
21087 LinuxBraceStyle);
21088 verifyFormat("struct S {\n"
21089 " int Type;\n"
21090 " union {\n"
21091 " int x;\n"
21092 " double y;\n"
21093 " } Value;\n"
21094 " class C\n"
21095 " {\n"
21096 " MyFavoriteType Value;\n"
21097 " } Class;\n"
21098 "}",
21099 LinuxBraceStyle);
21100}
21101
21102TEST_F(FormatTest, MozillaBraceBreaking) {
21103 FormatStyle MozillaBraceStyle = getLLVMStyle();
21104 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
21105 MozillaBraceStyle.FixNamespaceComments = false;
21106 verifyFormat("namespace a {\n"
21107 "class A\n"
21108 "{\n"
21109 " void f()\n"
21110 " {\n"
21111 " if (true) {\n"
21112 " a();\n"
21113 " b();\n"
21114 " }\n"
21115 " }\n"
21116 " void g() { return; }\n"
21117 "};\n"
21118 "enum E\n"
21119 "{\n"
21120 " A,\n"
21121 " // foo\n"
21122 " B,\n"
21123 " C\n"
21124 "};\n"
21125 "struct B\n"
21126 "{\n"
21127 " int x;\n"
21128 "};\n"
21129 "}",
21130 MozillaBraceStyle);
21131 verifyFormat("struct S\n"
21132 "{\n"
21133 " int Type;\n"
21134 " union\n"
21135 " {\n"
21136 " int x;\n"
21137 " double y;\n"
21138 " } Value;\n"
21139 " class C\n"
21140 " {\n"
21141 " MyFavoriteType Value;\n"
21142 " } Class;\n"
21143 "}",
21144 MozillaBraceStyle);
21145}
21146
21147TEST_F(FormatTest, StroustrupBraceBreaking) {
21148 FormatStyle StroustrupBraceStyle = getLLVMStyle();
21149 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
21150 verifyFormat("namespace a {\n"
21151 "class A {\n"
21152 " void f()\n"
21153 " {\n"
21154 " if (true) {\n"
21155 " a();\n"
21156 " b();\n"
21157 " }\n"
21158 " }\n"
21159 " void g() { return; }\n"
21160 "};\n"
21161 "struct B {\n"
21162 " int x;\n"
21163 "};\n"
21164 "} // namespace a",
21165 StroustrupBraceStyle);
21166
21167 verifyFormat("void foo()\n"
21168 "{\n"
21169 " if (a) {\n"
21170 " a();\n"
21171 " }\n"
21172 " else {\n"
21173 " b();\n"
21174 " }\n"
21175 "}",
21176 StroustrupBraceStyle);
21177
21178 verifyFormat("#ifdef _DEBUG\n"
21179 "int foo(int i = 0)\n"
21180 "#else\n"
21181 "int foo(int i = 5)\n"
21182 "#endif\n"
21183 "{\n"
21184 " return i;\n"
21185 "}",
21186 StroustrupBraceStyle);
21187
21188 verifyFormat("void foo() {}\n"
21189 "void bar()\n"
21190 "#ifdef _DEBUG\n"
21191 "{\n"
21192 " foo();\n"
21193 "}\n"
21194 "#else\n"
21195 "{\n"
21196 "}\n"
21197 "#endif",
21198 StroustrupBraceStyle);
21199
21200 verifyFormat("void foobar() { int i = 5; }\n"
21201 "#ifdef _DEBUG\n"
21202 "void bar() {}\n"
21203 "#else\n"
21204 "void bar() { foobar(); }\n"
21205 "#endif",
21206 StroustrupBraceStyle);
21207}
21208
21209TEST_F(FormatTest, AllmanBraceBreaking) {
21210 FormatStyle AllmanBraceStyle = getLLVMStyle();
21211 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
21212
21213 verifyFormat("namespace a\n"
21214 "{\n"
21215 "void f();\n"
21216 "void g();\n"
21217 "} // namespace a",
21218 "namespace a\n"
21219 "{\n"
21220 "void f();\n"
21221 "void g();\n"
21222 "}",
21223 AllmanBraceStyle);
21224
21225 verifyFormat("namespace a\n"
21226 "{\n"
21227 "class A\n"
21228 "{\n"
21229 " void f()\n"
21230 " {\n"
21231 " if (true)\n"
21232 " {\n"
21233 " a();\n"
21234 " b();\n"
21235 " }\n"
21236 " }\n"
21237 " void g() { return; }\n"
21238 "};\n"
21239 "struct B\n"
21240 "{\n"
21241 " int x;\n"
21242 "};\n"
21243 "union C\n"
21244 "{\n"
21245 "};\n"
21246 "} // namespace a",
21247 AllmanBraceStyle);
21248
21249 verifyFormat("void f()\n"
21250 "{\n"
21251 " if (true)\n"
21252 " {\n"
21253 " a();\n"
21254 " }\n"
21255 " else if (false)\n"
21256 " {\n"
21257 " b();\n"
21258 " }\n"
21259 " else\n"
21260 " {\n"
21261 " c();\n"
21262 " }\n"
21263 "}",
21264 AllmanBraceStyle);
21265
21266 verifyFormat("void f()\n"
21267 "{\n"
21268 " for (int i = 0; i < 10; ++i)\n"
21269 " {\n"
21270 " a();\n"
21271 " }\n"
21272 " while (false)\n"
21273 " {\n"
21274 " b();\n"
21275 " }\n"
21276 " do\n"
21277 " {\n"
21278 " c();\n"
21279 " } while (false)\n"
21280 "}",
21281 AllmanBraceStyle);
21282
21283 verifyFormat("void f(int a)\n"
21284 "{\n"
21285 " switch (a)\n"
21286 " {\n"
21287 " case 0:\n"
21288 " break;\n"
21289 " case 1:\n"
21290 " {\n"
21291 " break;\n"
21292 " }\n"
21293 " case 2:\n"
21294 " {\n"
21295 " }\n"
21296 " break;\n"
21297 " default:\n"
21298 " break;\n"
21299 " }\n"
21300 "}",
21301 AllmanBraceStyle);
21302
21303 verifyFormat("enum X\n"
21304 "{\n"
21305 " Y = 0,\n"
21306 "}",
21307 AllmanBraceStyle);
21308 verifyFormat("enum X\n"
21309 "{\n"
21310 " Y = 0\n"
21311 "}",
21312 AllmanBraceStyle);
21313
21314 verifyFormat("@interface BSApplicationController ()\n"
21315 "{\n"
21316 "@private\n"
21317 " id _extraIvar;\n"
21318 "}\n"
21319 "@end",
21320 AllmanBraceStyle);
21321
21322 verifyFormat("#ifdef _DEBUG\n"
21323 "int foo(int i = 0)\n"
21324 "#else\n"
21325 "int foo(int i = 5)\n"
21326 "#endif\n"
21327 "{\n"
21328 " return i;\n"
21329 "}",
21330 AllmanBraceStyle);
21331
21332 verifyFormat("void foo() {}\n"
21333 "void bar()\n"
21334 "#ifdef _DEBUG\n"
21335 "{\n"
21336 " foo();\n"
21337 "}\n"
21338 "#else\n"
21339 "{\n"
21340 "}\n"
21341 "#endif",
21342 AllmanBraceStyle);
21343
21344 verifyFormat("void foobar() { int i = 5; }\n"
21345 "#ifdef _DEBUG\n"
21346 "void bar() {}\n"
21347 "#else\n"
21348 "void bar() { foobar(); }\n"
21349 "#endif",
21350 AllmanBraceStyle);
21351
21352 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine,
21353 FormatStyle::SLS_All);
21354
21355 verifyFormat("[](int i) { return i + 2; };\n"
21356 "[](int i, int j)\n"
21357 "{\n"
21358 " auto x = i + j;\n"
21359 " auto y = i * j;\n"
21360 " return x ^ y;\n"
21361 "};\n"
21362 "void foo()\n"
21363 "{\n"
21364 " auto shortLambda = [](int i) { return i + 2; };\n"
21365 " auto longLambda = [](int i, int j)\n"
21366 " {\n"
21367 " auto x = i + j;\n"
21368 " auto y = i * j;\n"
21369 " return x ^ y;\n"
21370 " };\n"
21371 "}",
21372 AllmanBraceStyle);
21373
21374 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21375
21376 verifyFormat("[](int i)\n"
21377 "{\n"
21378 " return i + 2;\n"
21379 "};\n"
21380 "[](int i, int j)\n"
21381 "{\n"
21382 " auto x = i + j;\n"
21383 " auto y = i * j;\n"
21384 " return x ^ y;\n"
21385 "};\n"
21386 "void foo()\n"
21387 "{\n"
21388 " auto shortLambda = [](int i)\n"
21389 " {\n"
21390 " return i + 2;\n"
21391 " };\n"
21392 " auto longLambda = [](int i, int j)\n"
21393 " {\n"
21394 " auto x = i + j;\n"
21395 " auto y = i * j;\n"
21396 " return x ^ y;\n"
21397 " };\n"
21398 "}",
21399 AllmanBraceStyle);
21400
21401 // Reset
21402 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
21403
21404 // This shouldn't affect ObjC blocks..
21405 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21406 " // ...\n"
21407 " int i;\n"
21408 "}];",
21409 AllmanBraceStyle);
21410 verifyFormat("void (^block)(void) = ^{\n"
21411 " // ...\n"
21412 " int i;\n"
21413 "};",
21414 AllmanBraceStyle);
21415 // .. or dict literals.
21416 verifyFormat("void f()\n"
21417 "{\n"
21418 " // ...\n"
21419 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21420 "}",
21421 AllmanBraceStyle);
21422 verifyFormat("void f()\n"
21423 "{\n"
21424 " // ...\n"
21425 " [object someMethod:@{a : @\"b\"}];\n"
21426 "}",
21427 AllmanBraceStyle);
21428 verifyFormat("int f()\n"
21429 "{ // comment\n"
21430 " return 42;\n"
21431 "}",
21432 AllmanBraceStyle);
21433
21434 AllmanBraceStyle.ColumnLimit = 19;
21435 verifyFormat("void f() { int i; }", AllmanBraceStyle);
21436 AllmanBraceStyle.ColumnLimit = 18;
21437 verifyFormat("void f()\n"
21438 "{\n"
21439 " int i;\n"
21440 "}",
21441 AllmanBraceStyle);
21442 AllmanBraceStyle.ColumnLimit = 80;
21443
21444 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
21445 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21446 FormatStyle::SIS_WithoutElse;
21447 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21448 verifyFormat("void f(bool b)\n"
21449 "{\n"
21450 " if (b)\n"
21451 " {\n"
21452 " return;\n"
21453 " }\n"
21454 "}",
21455 BreakBeforeBraceShortIfs);
21456 verifyFormat("void f(bool b)\n"
21457 "{\n"
21458 " if constexpr (b)\n"
21459 " {\n"
21460 " return;\n"
21461 " }\n"
21462 "}",
21463 BreakBeforeBraceShortIfs);
21464 verifyFormat("void f(bool b)\n"
21465 "{\n"
21466 " if CONSTEXPR (b)\n"
21467 " {\n"
21468 " return;\n"
21469 " }\n"
21470 "}",
21471 BreakBeforeBraceShortIfs);
21472 verifyFormat("void f(bool b)\n"
21473 "{\n"
21474 " if (b) return;\n"
21475 "}",
21476 BreakBeforeBraceShortIfs);
21477 verifyFormat("void f(bool b)\n"
21478 "{\n"
21479 " if constexpr (b) return;\n"
21480 "}",
21481 BreakBeforeBraceShortIfs);
21482 verifyFormat("void f(bool b)\n"
21483 "{\n"
21484 " if CONSTEXPR (b) return;\n"
21485 "}",
21486 BreakBeforeBraceShortIfs);
21487 verifyFormat("void f(bool b)\n"
21488 "{\n"
21489 " while (b)\n"
21490 " {\n"
21491 " return;\n"
21492 " }\n"
21493 "}",
21494 BreakBeforeBraceShortIfs);
21495}
21496
21497TEST_F(FormatTest, WhitesmithsBraceBreaking) {
21498 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(ColumnLimit: 0);
21499 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
21500
21501 // Make a few changes to the style for testing purposes
21502 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine =
21503 FormatStyle::SFS_Empty;
21504 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
21505
21506 // FIXME: this test case can't decide whether there should be a blank line
21507 // after the ~D() line or not. It adds one if one doesn't exist in the test
21508 // and it removes the line if one exists.
21509 /*
21510 verifyFormat("class A;\n"
21511 "namespace B\n"
21512 " {\n"
21513 "class C;\n"
21514 "// Comment\n"
21515 "class D\n"
21516 " {\n"
21517 "public:\n"
21518 " D();\n"
21519 " ~D() {}\n"
21520 "private:\n"
21521 " enum E\n"
21522 " {\n"
21523 " F\n"
21524 " }\n"
21525 " };\n"
21526 " } // namespace B",
21527 WhitesmithsBraceStyle);
21528 */
21529
21530 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None;
21531 verifyFormat("namespace a\n"
21532 " {\n"
21533 "class A\n"
21534 " {\n"
21535 " void f()\n"
21536 " {\n"
21537 " if (true)\n"
21538 " {\n"
21539 " a();\n"
21540 " b();\n"
21541 " }\n"
21542 " }\n"
21543 " void g()\n"
21544 " {\n"
21545 " return;\n"
21546 " }\n"
21547 " };\n"
21548 "struct B\n"
21549 " {\n"
21550 " int x;\n"
21551 " };\n"
21552 " } // namespace a",
21553 WhitesmithsBraceStyle);
21554
21555 verifyFormat("namespace a\n"
21556 " {\n"
21557 "namespace b\n"
21558 " {\n"
21559 "class A\n"
21560 " {\n"
21561 " void f()\n"
21562 " {\n"
21563 " if (true)\n"
21564 " {\n"
21565 " a();\n"
21566 " b();\n"
21567 " }\n"
21568 " }\n"
21569 " void g()\n"
21570 " {\n"
21571 " return;\n"
21572 " }\n"
21573 " };\n"
21574 "struct B\n"
21575 " {\n"
21576 " int x;\n"
21577 " };\n"
21578 " } // namespace b\n"
21579 " } // namespace a",
21580 WhitesmithsBraceStyle);
21581
21582 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner;
21583 verifyFormat("namespace a\n"
21584 " {\n"
21585 "namespace b\n"
21586 " {\n"
21587 " class A\n"
21588 " {\n"
21589 " void f()\n"
21590 " {\n"
21591 " if (true)\n"
21592 " {\n"
21593 " a();\n"
21594 " b();\n"
21595 " }\n"
21596 " }\n"
21597 " void g()\n"
21598 " {\n"
21599 " return;\n"
21600 " }\n"
21601 " };\n"
21602 " struct B\n"
21603 " {\n"
21604 " int x;\n"
21605 " };\n"
21606 " } // namespace b\n"
21607 " } // namespace a",
21608 WhitesmithsBraceStyle);
21609
21610 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All;
21611 verifyFormat("namespace a\n"
21612 " {\n"
21613 " namespace b\n"
21614 " {\n"
21615 " class A\n"
21616 " {\n"
21617 " void f()\n"
21618 " {\n"
21619 " if (true)\n"
21620 " {\n"
21621 " a();\n"
21622 " b();\n"
21623 " }\n"
21624 " }\n"
21625 " void g()\n"
21626 " {\n"
21627 " return;\n"
21628 " }\n"
21629 " };\n"
21630 " struct B\n"
21631 " {\n"
21632 " int x;\n"
21633 " };\n"
21634 " } // namespace b\n"
21635 " } // namespace a",
21636 WhitesmithsBraceStyle);
21637
21638 verifyFormat("void f()\n"
21639 " {\n"
21640 " if (true)\n"
21641 " {\n"
21642 " a();\n"
21643 " }\n"
21644 " else if (false)\n"
21645 " {\n"
21646 " b();\n"
21647 " }\n"
21648 " else\n"
21649 " {\n"
21650 " c();\n"
21651 " }\n"
21652 " }",
21653 WhitesmithsBraceStyle);
21654
21655 verifyFormat("void f()\n"
21656 " {\n"
21657 " for (int i = 0; i < 10; ++i)\n"
21658 " {\n"
21659 " a();\n"
21660 " }\n"
21661 " while (false)\n"
21662 " {\n"
21663 " b();\n"
21664 " }\n"
21665 " do\n"
21666 " {\n"
21667 " c();\n"
21668 " } while (false)\n"
21669 " }",
21670 WhitesmithsBraceStyle);
21671
21672 WhitesmithsBraceStyle.IndentCaseLabels = true;
21673 verifyFormat("void switchTest1(int a)\n"
21674 " {\n"
21675 " switch (a)\n"
21676 " {\n"
21677 " case 2:\n"
21678 " {\n"
21679 " }\n"
21680 " break;\n"
21681 " }\n"
21682 " }",
21683 WhitesmithsBraceStyle);
21684
21685 verifyFormat("void switchTest2(int a)\n"
21686 " {\n"
21687 " switch (a)\n"
21688 " {\n"
21689 " case 0:\n"
21690 " break;\n"
21691 " case 1:\n"
21692 " {\n"
21693 " break;\n"
21694 " }\n"
21695 " case 2:\n"
21696 " {\n"
21697 " }\n"
21698 " break;\n"
21699 " default:\n"
21700 " break;\n"
21701 " }\n"
21702 " }",
21703 WhitesmithsBraceStyle);
21704
21705 verifyFormat("void switchTest3(int a)\n"
21706 " {\n"
21707 " switch (a)\n"
21708 " {\n"
21709 " case 0:\n"
21710 " {\n"
21711 " foo(x);\n"
21712 " }\n"
21713 " break;\n"
21714 " default:\n"
21715 " {\n"
21716 " foo(1);\n"
21717 " }\n"
21718 " break;\n"
21719 " }\n"
21720 " }",
21721 WhitesmithsBraceStyle);
21722
21723 WhitesmithsBraceStyle.IndentCaseLabels = false;
21724
21725 verifyFormat("void switchTest4(int a)\n"
21726 " {\n"
21727 " switch (a)\n"
21728 " {\n"
21729 " case 2:\n"
21730 " {\n"
21731 " }\n"
21732 " break;\n"
21733 " }\n"
21734 " }",
21735 WhitesmithsBraceStyle);
21736
21737 verifyFormat("void switchTest5(int a)\n"
21738 " {\n"
21739 " switch (a)\n"
21740 " {\n"
21741 " case 0:\n"
21742 " break;\n"
21743 " case 1:\n"
21744 " {\n"
21745 " foo();\n"
21746 " break;\n"
21747 " }\n"
21748 " case 2:\n"
21749 " {\n"
21750 " }\n"
21751 " break;\n"
21752 " default:\n"
21753 " break;\n"
21754 " }\n"
21755 " }",
21756 WhitesmithsBraceStyle);
21757
21758 verifyFormat("void switchTest6(int a)\n"
21759 " {\n"
21760 " switch (a)\n"
21761 " {\n"
21762 " case 0:\n"
21763 " {\n"
21764 " foo(x);\n"
21765 " }\n"
21766 " break;\n"
21767 " default:\n"
21768 " {\n"
21769 " foo(1);\n"
21770 " }\n"
21771 " break;\n"
21772 " }\n"
21773 " }",
21774 WhitesmithsBraceStyle);
21775
21776 verifyFormat("enum X\n"
21777 " {\n"
21778 " Y = 0, // testing\n"
21779 " }",
21780 WhitesmithsBraceStyle);
21781
21782 verifyFormat("enum X\n"
21783 " {\n"
21784 " Y = 0\n"
21785 " }",
21786 WhitesmithsBraceStyle);
21787 verifyFormat("enum X\n"
21788 " {\n"
21789 " Y = 0,\n"
21790 " Z = 1\n"
21791 " };",
21792 WhitesmithsBraceStyle);
21793
21794 verifyFormat("@interface BSApplicationController ()\n"
21795 " {\n"
21796 "@private\n"
21797 " id _extraIvar;\n"
21798 " }\n"
21799 "@end",
21800 WhitesmithsBraceStyle);
21801
21802 verifyFormat("#ifdef _DEBUG\n"
21803 "int foo(int i = 0)\n"
21804 "#else\n"
21805 "int foo(int i = 5)\n"
21806 "#endif\n"
21807 " {\n"
21808 " return i;\n"
21809 " }",
21810 WhitesmithsBraceStyle);
21811
21812 verifyFormat("void foo() {}\n"
21813 "void bar()\n"
21814 "#ifdef _DEBUG\n"
21815 " {\n"
21816 " foo();\n"
21817 " }\n"
21818 "#else\n"
21819 " {\n"
21820 " }\n"
21821 "#endif",
21822 WhitesmithsBraceStyle);
21823
21824 verifyFormat("void foobar()\n"
21825 " {\n"
21826 " int i = 5;\n"
21827 " }\n"
21828 "#ifdef _DEBUG\n"
21829 "void bar() {}\n"
21830 "#else\n"
21831 "void bar()\n"
21832 " {\n"
21833 " foobar();\n"
21834 " }\n"
21835 "#endif",
21836 WhitesmithsBraceStyle);
21837
21838 // This shouldn't affect ObjC blocks..
21839 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21840 " // ...\n"
21841 " int i;\n"
21842 "}];",
21843 WhitesmithsBraceStyle);
21844 verifyFormat("void (^block)(void) = ^{\n"
21845 " // ...\n"
21846 " int i;\n"
21847 "};",
21848 WhitesmithsBraceStyle);
21849 // .. or dict literals.
21850 verifyFormat("void f()\n"
21851 " {\n"
21852 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21853 " }",
21854 WhitesmithsBraceStyle);
21855
21856 verifyFormat("int f()\n"
21857 " { // comment\n"
21858 " return 42;\n"
21859 " }",
21860 WhitesmithsBraceStyle);
21861
21862 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
21863 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine =
21864 FormatStyle::SIS_OnlyFirstIf;
21865 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
21866 verifyFormat("void f(bool b)\n"
21867 " {\n"
21868 " if (b)\n"
21869 " {\n"
21870 " return;\n"
21871 " }\n"
21872 " }",
21873 BreakBeforeBraceShortIfs);
21874 verifyFormat("void f(bool b)\n"
21875 " {\n"
21876 " if (b) return;\n"
21877 " }",
21878 BreakBeforeBraceShortIfs);
21879 verifyFormat("void f(bool b)\n"
21880 " {\n"
21881 " while (b)\n"
21882 " {\n"
21883 " return;\n"
21884 " }\n"
21885 " }",
21886 BreakBeforeBraceShortIfs);
21887}
21888
21889TEST_F(FormatTest, GNUBraceBreaking) {
21890 FormatStyle GNUBraceStyle = getLLVMStyle();
21891 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
21892 verifyFormat("namespace a\n"
21893 "{\n"
21894 "class A\n"
21895 "{\n"
21896 " void f()\n"
21897 " {\n"
21898 " int a;\n"
21899 " {\n"
21900 " int b;\n"
21901 " }\n"
21902 " if (true)\n"
21903 " {\n"
21904 " a();\n"
21905 " b();\n"
21906 " }\n"
21907 " }\n"
21908 " void g() { return; }\n"
21909 "}\n"
21910 "} // namespace a",
21911 GNUBraceStyle);
21912
21913 verifyFormat("void f()\n"
21914 "{\n"
21915 " if (true)\n"
21916 " {\n"
21917 " a();\n"
21918 " }\n"
21919 " else if (false)\n"
21920 " {\n"
21921 " b();\n"
21922 " }\n"
21923 " else\n"
21924 " {\n"
21925 " c();\n"
21926 " }\n"
21927 "}",
21928 GNUBraceStyle);
21929
21930 verifyFormat("void f()\n"
21931 "{\n"
21932 " for (int i = 0; i < 10; ++i)\n"
21933 " {\n"
21934 " a();\n"
21935 " }\n"
21936 " while (false)\n"
21937 " {\n"
21938 " b();\n"
21939 " }\n"
21940 " do\n"
21941 " {\n"
21942 " c();\n"
21943 " }\n"
21944 " while (false);\n"
21945 "}",
21946 GNUBraceStyle);
21947
21948 verifyFormat("void f(int a)\n"
21949 "{\n"
21950 " switch (a)\n"
21951 " {\n"
21952 " case 0:\n"
21953 " break;\n"
21954 " case 1:\n"
21955 " {\n"
21956 " break;\n"
21957 " }\n"
21958 " case 2:\n"
21959 " {\n"
21960 " }\n"
21961 " break;\n"
21962 " default:\n"
21963 " break;\n"
21964 " }\n"
21965 "}",
21966 GNUBraceStyle);
21967
21968 verifyFormat("enum X\n"
21969 "{\n"
21970 " Y = 0,\n"
21971 "}",
21972 GNUBraceStyle);
21973
21974 verifyFormat("@interface BSApplicationController ()\n"
21975 "{\n"
21976 "@private\n"
21977 " id _extraIvar;\n"
21978 "}\n"
21979 "@end",
21980 GNUBraceStyle);
21981
21982 verifyFormat("#ifdef _DEBUG\n"
21983 "int foo(int i = 0)\n"
21984 "#else\n"
21985 "int foo(int i = 5)\n"
21986 "#endif\n"
21987 "{\n"
21988 " return i;\n"
21989 "}",
21990 GNUBraceStyle);
21991
21992 verifyFormat("void foo() {}\n"
21993 "void bar()\n"
21994 "#ifdef _DEBUG\n"
21995 "{\n"
21996 " foo();\n"
21997 "}\n"
21998 "#else\n"
21999 "{\n"
22000 "}\n"
22001 "#endif",
22002 GNUBraceStyle);
22003
22004 verifyFormat("void foobar() { int i = 5; }\n"
22005 "#ifdef _DEBUG\n"
22006 "void bar() {}\n"
22007 "#else\n"
22008 "void bar() { foobar(); }\n"
22009 "#endif",
22010 GNUBraceStyle);
22011}
22012
22013TEST_F(FormatTest, WebKitBraceBreaking) {
22014 FormatStyle WebKitBraceStyle = getLLVMStyle();
22015 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
22016 WebKitBraceStyle.FixNamespaceComments = false;
22017 verifyFormat("namespace a {\n"
22018 "class A {\n"
22019 " void f()\n"
22020 " {\n"
22021 " if (true) {\n"
22022 " a();\n"
22023 " b();\n"
22024 " }\n"
22025 " }\n"
22026 " void g() { return; }\n"
22027 "};\n"
22028 "enum E {\n"
22029 " A,\n"
22030 " // foo\n"
22031 " B,\n"
22032 " C\n"
22033 "};\n"
22034 "struct B {\n"
22035 " int x;\n"
22036 "};\n"
22037 "}",
22038 WebKitBraceStyle);
22039 verifyFormat("struct S {\n"
22040 " int Type;\n"
22041 " union {\n"
22042 " int x;\n"
22043 " double y;\n"
22044 " } Value;\n"
22045 " class C {\n"
22046 " MyFavoriteType Value;\n"
22047 " } Class;\n"
22048 "};",
22049 WebKitBraceStyle);
22050}
22051
22052TEST_F(FormatTest, CatchExceptionReferenceBinding) {
22053 verifyFormat("void f() {\n"
22054 " try {\n"
22055 " } catch (const Exception &e) {\n"
22056 " }\n"
22057 "}");
22058}
22059
22060TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
22061 auto Style = getLLVMStyle();
22062 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
22063 verifyNoCrash(Code: "f({\n"
22064 "table({}, table({{\"\", false}}, {}))\n"
22065 "});",
22066 Style);
22067
22068 Style.AlignConsecutiveAssignments.Enabled = true;
22069 Style.AlignConsecutiveDeclarations.Enabled = true;
22070 verifyFormat("struct test demo[] = {\n"
22071 " {56, 23, \"hello\"},\n"
22072 " {-1, 93463, \"world\"},\n"
22073 " { 7, 5, \"!!\"}\n"
22074 "};",
22075 Style);
22076
22077 verifyFormat("struct test demo[] = {\n"
22078 " {56, 23, \"hello\"}, // first line\n"
22079 " {-1, 93463, \"world\"}, // second line\n"
22080 " { 7, 5, \"!!\"} // third line\n"
22081 "};",
22082 Style);
22083
22084 verifyFormat("struct test demo[4] = {\n"
22085 " { 56, 23, 21, \"oh\"}, // first line\n"
22086 " { -1, 93463, 22, \"my\"}, // second line\n"
22087 " { 7, 5, 1, \"goodness\"} // third line\n"
22088 " {234, 5, 1, \"gracious\"} // fourth line\n"
22089 "};",
22090 Style);
22091
22092 verifyFormat("struct test demo[3] = {\n"
22093 " {56, 23, \"hello\"},\n"
22094 " {-1, 93463, \"world\"},\n"
22095 " { 7, 5, \"!!\"}\n"
22096 "};",
22097 Style);
22098
22099 verifyFormat("struct test demo[3] = {\n"
22100 " {int{56}, 23, \"hello\"},\n"
22101 " {int{-1}, 93463, \"world\"},\n"
22102 " { int{7}, 5, \"!!\"}\n"
22103 "};",
22104 Style);
22105
22106 verifyFormat("struct test demo[] = {\n"
22107 " {56, 23, \"hello\"},\n"
22108 " {-1, 93463, \"world\"},\n"
22109 " { 7, 5, \"!!\"},\n"
22110 "};",
22111 Style);
22112
22113 verifyFormat("test demo[] = {\n"
22114 " {56, 23, \"hello\"},\n"
22115 " {-1, 93463, \"world\"},\n"
22116 " { 7, 5, \"!!\"},\n"
22117 "};",
22118 Style);
22119
22120 verifyFormat("demo = std::array<struct test, 3>{\n"
22121 " test{56, 23, \"hello\"},\n"
22122 " test{-1, 93463, \"world\"},\n"
22123 " test{ 7, 5, \"!!\"},\n"
22124 "};",
22125 Style);
22126
22127 verifyFormat("test demo[] = {\n"
22128 " {56, 23, \"hello\"},\n"
22129 "#if X\n"
22130 " {-1, 93463, \"world\"},\n"
22131 "#endif\n"
22132 " { 7, 5, \"!!\"}\n"
22133 "};",
22134 Style);
22135
22136 verifyFormat(
22137 "test demo[] = {\n"
22138 " { 7, 23,\n"
22139 " \"hello world i am a very long line that really, in any\"\n"
22140 " \"just world, ought to be split over multiple lines\"},\n"
22141 " {-1, 93463, \"world\"},\n"
22142 " {56, 5, \"!!\"}\n"
22143 "};",
22144 Style);
22145
22146 verifyNoCrash(Code: "Foo f[] = {\n"
22147 " [0] = { 1, },\n"
22148 " [i] { 1, },\n"
22149 "};",
22150 Style);
22151 verifyNoCrash(Code: "Foo foo[] = {\n"
22152 " [0] = {1, 1},\n"
22153 " [1] { 1, 1, },\n"
22154 " [2] { 1, 1, },\n"
22155 "};",
22156 Style);
22157 verifyNoCrash(Code: "test arr[] = {\n"
22158 "#define FOO(i) {i, i},\n"
22159 "SOME_GENERATOR(FOO)\n"
22160 "{2, 2}\n"
22161 "};",
22162 Style);
22163
22164 verifyFormat("return GradForUnaryCwise(g, {\n"
22165 " {{\"sign\"}, \"Sign\", "
22166 " {\"x\", \"dy\"}},\n"
22167 " { {\"dx\"}, \"Mul\", {\"dy\""
22168 ", \"sign\"}},\n"
22169 "});",
22170 Style);
22171
22172 Style.Cpp11BracedListStyle = false;
22173 verifyFormat("struct test demo[] = {\n"
22174 " { 56, 23, \"hello\" },\n"
22175 " { -1, 93463, \"world\" },\n"
22176 " { 7, 5, \"!!\" }\n"
22177 "};",
22178 Style);
22179 Style.Cpp11BracedListStyle = true;
22180
22181 Style.ColumnLimit = 0;
22182 verifyFormat(
22183 "test demo[] = {\n"
22184 " {56, 23, \"hello world i am a very long line that really, "
22185 "in any just world, ought to be split over multiple lines\"},\n"
22186 " {-1, 93463, "
22187 " \"world\"},\n"
22188 " { 7, 5, "
22189 " \"!!\"},\n"
22190 "};",
22191 "test demo[] = {{56, 23, \"hello world i am a very long line "
22192 "that really, in any just world, ought to be split over multiple "
22193 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22194 Style);
22195
22196 Style.ColumnLimit = 80;
22197 verifyFormat("test demo[] = {\n"
22198 " {56, 23, /* a comment */ \"hello\"},\n"
22199 " {-1, 93463, \"world\"},\n"
22200 " { 7, 5, \"!!\"}\n"
22201 "};",
22202 Style);
22203
22204 verifyFormat("test demo[] = {\n"
22205 " {56, 23, \"hello\"},\n"
22206 " {-1, 93463, \"world\" /* comment here */},\n"
22207 " { 7, 5, \"!!\"}\n"
22208 "};",
22209 Style);
22210
22211 verifyFormat("test demo[] = {\n"
22212 " {56, /* a comment */ 23, \"hello\"},\n"
22213 " {-1, 93463, \"world\"},\n"
22214 " { 7, 5, \"!!\"}\n"
22215 "};",
22216 Style);
22217
22218 Style.ColumnLimit = 20;
22219 verifyFormat("demo = std::array<\n"
22220 " struct test, 3>{\n"
22221 " test{\n"
22222 " 56, 23,\n"
22223 " \"hello \"\n"
22224 " \"world i \"\n"
22225 " \"am a very \"\n"
22226 " \"long line \"\n"
22227 " \"that \"\n"
22228 " \"really, \"\n"
22229 " \"in any \"\n"
22230 " \"just \"\n"
22231 " \"world, \"\n"
22232 " \"ought to \"\n"
22233 " \"be split \"\n"
22234 " \"over \"\n"
22235 " \"multiple \"\n"
22236 " \"lines\"},\n"
22237 " test{-1, 93463,\n"
22238 " \"world\"},\n"
22239 " test{ 7, 5,\n"
22240 " \"!!\" },\n"
22241 "};",
22242 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
22243 "i am a very long line that really, in any just world, ought "
22244 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
22245 "test{7, 5, \"!!\"},};",
22246 Style);
22247 // This caused a core dump by enabling Alignment in the LLVMStyle globally
22248 Style = getLLVMStyleWithColumns(ColumnLimit: 50);
22249 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
22250 verifyFormat("static A x = {\n"
22251 " {{init1, init2, init3, init4},\n"
22252 " {init1, init2, init3, init4}}\n"
22253 "};",
22254 Style);
22255 // TODO: Fix the indentations below when this option is fully functional.
22256#if 0
22257 verifyFormat("int a[][] = {\n"
22258 " {\n"
22259 " {0, 2}, //\n"
22260 " {1, 2} //\n"
22261 " }\n"
22262 "};",
22263 Style);
22264#endif
22265 Style.ColumnLimit = 100;
22266 verifyFormat(
22267 "test demo[] = {\n"
22268 " {56, 23,\n"
22269 " \"hello world i am a very long line that really, in any just world"
22270 ", ought to be split over \"\n"
22271 " \"multiple lines\" },\n"
22272 " {-1, 93463, \"world\"},\n"
22273 " { 7, 5, \"!!\"},\n"
22274 "};",
22275 "test demo[] = {{56, 23, \"hello world i am a very long line "
22276 "that really, in any just world, ought to be split over multiple "
22277 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22278 Style);
22279
22280 Style = getLLVMStyleWithColumns(ColumnLimit: 50);
22281 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
22282 verifyFormat("struct test demo[] = {\n"
22283 " {56, 23, \"hello\"},\n"
22284 " {-1, 93463, \"world\"},\n"
22285 " { 7, 5, \"!!\"}\n"
22286 "};\n"
22287 "static A x = {\n"
22288 " {{init1, init2, init3, init4},\n"
22289 " {init1, init2, init3, init4}}\n"
22290 "};",
22291 Style);
22292 Style.ColumnLimit = 100;
22293 Style.AlignConsecutiveAssignments.AcrossComments = true;
22294 Style.AlignConsecutiveDeclarations.AcrossComments = true;
22295 verifyFormat("struct test demo[] = {\n"
22296 " {56, 23, \"hello\"},\n"
22297 " {-1, 93463, \"world\"},\n"
22298 " { 7, 5, \"!!\"}\n"
22299 "};\n"
22300 "struct test demo[4] = {\n"
22301 " { 56, 23, 21, \"oh\"}, // first line\n"
22302 " { -1, 93463, 22, \"my\"}, // second line\n"
22303 " { 7, 5, 1, \"goodness\"} // third line\n"
22304 " {234, 5, 1, \"gracious\"} // fourth line\n"
22305 "};",
22306 Style);
22307 verifyFormat(
22308 "test demo[] = {\n"
22309 " {56,\n"
22310 " \"hello world i am a very long line that really, in any just world"
22311 ", ought to be split over \"\n"
22312 " \"multiple lines\", 23},\n"
22313 " {-1, \"world\", 93463},\n"
22314 " { 7, \"!!\", 5},\n"
22315 "};",
22316 "test demo[] = {{56, \"hello world i am a very long line "
22317 "that really, in any just world, ought to be split over multiple "
22318 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
22319 Style);
22320}
22321
22322TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
22323 auto Style = getLLVMStyle();
22324 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
22325 /* FIXME: This case gets misformatted.
22326 verifyFormat("auto foo = Items{\n"
22327 " Section{0, bar(), },\n"
22328 " Section{1, boo() }\n"
22329 "};",
22330 Style);
22331 */
22332 verifyFormat("auto foo = Items{\n"
22333 " Section{\n"
22334 " 0, bar(),\n"
22335 " }\n"
22336 "};",
22337 Style);
22338 verifyFormat("struct test demo[] = {\n"
22339 " {56, 23, \"hello\"},\n"
22340 " {-1, 93463, \"world\"},\n"
22341 " {7, 5, \"!!\" }\n"
22342 "};",
22343 Style);
22344 verifyFormat("struct test demo[] = {\n"
22345 " {56, 23, \"hello\"}, // first line\n"
22346 " {-1, 93463, \"world\"}, // second line\n"
22347 " {7, 5, \"!!\" } // third line\n"
22348 "};",
22349 Style);
22350 verifyFormat("struct test demo[4] = {\n"
22351 " {56, 23, 21, \"oh\" }, // first line\n"
22352 " {-1, 93463, 22, \"my\" }, // second line\n"
22353 " {7, 5, 1, \"goodness\"} // third line\n"
22354 " {234, 5, 1, \"gracious\"} // fourth line\n"
22355 "};",
22356 Style);
22357 verifyFormat("struct test demo[3] = {\n"
22358 " {56, 23, \"hello\"},\n"
22359 " {-1, 93463, \"world\"},\n"
22360 " {7, 5, \"!!\" }\n"
22361 "};",
22362 Style);
22363
22364 verifyFormat("struct test demo[3] = {\n"
22365 " {int{56}, 23, \"hello\"},\n"
22366 " {int{-1}, 93463, \"world\"},\n"
22367 " {int{7}, 5, \"!!\" }\n"
22368 "};",
22369 Style);
22370 verifyFormat("struct test demo[] = {\n"
22371 " {56, 23, \"hello\"},\n"
22372 " {-1, 93463, \"world\"},\n"
22373 " {7, 5, \"!!\" },\n"
22374 "};",
22375 Style);
22376 verifyFormat("test demo[] = {\n"
22377 " {56, 23, \"hello\"},\n"
22378 " {-1, 93463, \"world\"},\n"
22379 " {7, 5, \"!!\" },\n"
22380 "};",
22381 Style);
22382 verifyFormat("demo = std::array<struct test, 3>{\n"
22383 " test{56, 23, \"hello\"},\n"
22384 " test{-1, 93463, \"world\"},\n"
22385 " test{7, 5, \"!!\" },\n"
22386 "};",
22387 Style);
22388 verifyFormat("test demo[] = {\n"
22389 " {56, 23, \"hello\"},\n"
22390 "#if X\n"
22391 " {-1, 93463, \"world\"},\n"
22392 "#endif\n"
22393 " {7, 5, \"!!\" }\n"
22394 "};",
22395 Style);
22396 verifyFormat(
22397 "test demo[] = {\n"
22398 " {7, 23,\n"
22399 " \"hello world i am a very long line that really, in any\"\n"
22400 " \"just world, ought to be split over multiple lines\"},\n"
22401 " {-1, 93463, \"world\" },\n"
22402 " {56, 5, \"!!\" }\n"
22403 "};",
22404 Style);
22405
22406 verifyNoCrash(Code: "Foo f[] = {\n"
22407 " [0] = { 1, },\n"
22408 " [i] { 1, },\n"
22409 "};",
22410 Style);
22411 verifyNoCrash(Code: "Foo foo[] = {\n"
22412 " [0] = {1, 1},\n"
22413 " [1] { 1, 1, },\n"
22414 " [2] { 1, 1, },\n"
22415 "};",
22416 Style);
22417 verifyNoCrash(Code: "test arr[] = {\n"
22418 "#define FOO(i) {i, i},\n"
22419 "SOME_GENERATOR(FOO)\n"
22420 "{2, 2}\n"
22421 "};",
22422 Style);
22423
22424 verifyFormat("return GradForUnaryCwise(g, {\n"
22425 " {{\"sign\"}, \"Sign\", {\"x\", "
22426 "\"dy\"} },\n"
22427 " {{\"dx\"}, \"Mul\", "
22428 "{\"dy\", \"sign\"}},\n"
22429 "});",
22430 Style);
22431
22432 verifyNoCrash(
22433 Code: "PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
22434 " {\n"
22435 " {PIC(0), PIC(0), PIC(99), PIC(81), 0}, // Backbox\n"
22436 " {PIC(1), PIC(83), PIC(191), PIC(137), 0}, // AK47\n"
22437 "\n"
22438 "#define PICALL1(a, b, c, d) \\\n"
22439 " { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
22440 "\n"
22441 " PICALL1(1, 1, 75, 50),\n"
22442 "};",
22443 Style);
22444
22445 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
22446 verifyFormat("#define FOO \\\n"
22447 " int foo[][2] = { \\\n"
22448 " {0, 1} \\\n"
22449 " };",
22450 Style);
22451
22452 Style.Cpp11BracedListStyle = false;
22453 verifyFormat("struct test demo[] = {\n"
22454 " { 56, 23, \"hello\" },\n"
22455 " { -1, 93463, \"world\" },\n"
22456 " { 7, 5, \"!!\" }\n"
22457 "};",
22458 Style);
22459 Style.Cpp11BracedListStyle = true;
22460
22461 Style.ColumnLimit = 0;
22462 verifyFormat(
22463 "test demo[] = {\n"
22464 " {56, 23, \"hello world i am a very long line that really, in any "
22465 "just world, ought to be split over multiple lines\"},\n"
22466 " {-1, 93463, \"world\" "
22467 " },\n"
22468 " {7, 5, \"!!\" "
22469 " },\n"
22470 "};",
22471 "test demo[] = {{56, 23, \"hello world i am a very long line "
22472 "that really, in any just world, ought to be split over multiple "
22473 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22474 Style);
22475
22476 Style.ColumnLimit = 80;
22477 verifyFormat("test demo[] = {\n"
22478 " {56, 23, /* a comment */ \"hello\"},\n"
22479 " {-1, 93463, \"world\" },\n"
22480 " {7, 5, \"!!\" }\n"
22481 "};",
22482 Style);
22483
22484 verifyFormat("test demo[] = {\n"
22485 " {56, 23, \"hello\" },\n"
22486 " {-1, 93463, \"world\" /* comment here */},\n"
22487 " {7, 5, \"!!\" }\n"
22488 "};",
22489 Style);
22490
22491 verifyFormat("test demo[] = {\n"
22492 " {56, /* a comment */ 23, \"hello\"},\n"
22493 " {-1, 93463, \"world\"},\n"
22494 " {7, 5, \"!!\" }\n"
22495 "};",
22496 Style);
22497 verifyFormat("Foo foo = {\n"
22498 " // comment\n"
22499 " {1, 2}\n"
22500 "};",
22501 Style);
22502
22503 Style.ColumnLimit = 20;
22504 // FIXME: unstable test case
22505 EXPECT_EQ(
22506 "demo = std::array<\n"
22507 " struct test, 3>{\n"
22508 " test{\n"
22509 " 56, 23,\n"
22510 " \"hello \"\n"
22511 " \"world i \"\n"
22512 " \"am a very \"\n"
22513 " \"long line \"\n"
22514 " \"that \"\n"
22515 " \"really, \"\n"
22516 " \"in any \"\n"
22517 " \"just \"\n"
22518 " \"world, \"\n"
22519 " \"ought to \"\n"
22520 " \"be split \"\n"
22521 " \"over \"\n"
22522 " \"multiple \"\n"
22523 " \"lines\"},\n"
22524 " test{-1, 93463,\n"
22525 " \"world\"},\n"
22526 " test{7, 5,\n"
22527 " \"!!\" },\n"
22528 "};",
22529 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
22530 "i am a very long line that really, in any just world, ought "
22531 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
22532 "test{7, 5, \"!!\"},};",
22533 Style));
22534
22535 // This caused a core dump by enabling Alignment in the LLVMStyle globally
22536 Style = getLLVMStyleWithColumns(ColumnLimit: 50);
22537 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
22538 verifyFormat("static A x = {\n"
22539 " {{init1, init2, init3, init4},\n"
22540 " {init1, init2, init3, init4}}\n"
22541 "};",
22542 Style);
22543 Style.ColumnLimit = 100;
22544 verifyFormat(
22545 "test demo[] = {\n"
22546 " {56, 23,\n"
22547 " \"hello world i am a very long line that really, in any just world"
22548 ", ought to be split over \"\n"
22549 " \"multiple lines\" },\n"
22550 " {-1, 93463, \"world\"},\n"
22551 " {7, 5, \"!!\" },\n"
22552 "};",
22553 "test demo[] = {{56, 23, \"hello world i am a very long line "
22554 "that really, in any just world, ought to be split over multiple "
22555 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
22556 Style);
22557
22558 Style.ColumnLimit = 25;
22559 verifyNoCrash(Code: "Type foo{\n"
22560 " {\n"
22561 " 1, // A\n"
22562 " 2, // B\n"
22563 " 3, // C\n"
22564 " },\n"
22565 " \"hello\",\n"
22566 "};",
22567 Style);
22568 verifyNoCrash(Code: "Type object[X][Y] = {\n"
22569 " {{val}, {val}, {val}},\n"
22570 " {{val}, {val}, // some comment\n"
22571 " {val}}\n"
22572 "};",
22573 Style);
22574
22575 Style.ColumnLimit = 120;
22576 verifyNoCrash(
22577 Code: "T v[] {\n"
22578 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
22579 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
22580 "\"00000000000000000000000000000000000000000000000000000000"
22581 "00000000000000000000000000000000000000000000000000000000\" },\n"
22582 "};",
22583 Style);
22584
22585 Style.SpacesInParens = FormatStyle::SIPO_Custom;
22586 Style.SpacesInParensOptions.Other = true;
22587 verifyFormat("Foo foo[] = {\n"
22588 " {1, 1},\n"
22589 " {1, 1},\n"
22590 "};",
22591 Style);
22592}
22593
22594TEST_F(FormatTest, UnderstandsPragmas) {
22595 verifyFormat("#pragma omp reduction(| : var)");
22596 verifyFormat("#pragma omp reduction(+ : var)");
22597
22598 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22599 "(including parentheses).",
22600 "#pragma mark Any non-hyphenated or hyphenated string "
22601 "(including parentheses).");
22602
22603 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22604 "(including parentheses).",
22605 "#pragma mark Any non-hyphenated or hyphenated string "
22606 "(including parentheses).");
22607
22608 verifyFormat("#pragma comment(linker, \\\n"
22609 " \"argument\" \\\n"
22610 " \"argument\"",
22611 "#pragma comment(linker, \\\n"
22612 " \"argument\" \\\n"
22613 " \"argument\"",
22614 getStyleWithColumns(getChromiumStyle(FormatStyle::LK_Cpp), 32));
22615}
22616
22617TEST_F(FormatTest, UnderstandsPragmaOmpTarget) {
22618 verifyFormat("#pragma omp target map(to : var)");
22619 verifyFormat("#pragma omp target map(to : var[ : N])");
22620 verifyFormat("#pragma omp target map(to : var[0 : N])");
22621 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22622
22623 verifyFormat(
22624 "#pragma omp target \\\n"
22625 " reduction(+ : var) \\\n"
22626 " map(to : A[0 : N]) \\\n"
22627 " map(to : B[0 : N]) \\\n"
22628 " map(from : C[0 : N]) \\\n"
22629 " firstprivate(i) \\\n"
22630 " firstprivate(j) \\\n"
22631 " firstprivate(k)",
22632 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22633 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22634 getLLVMStyleWithColumns(26));
22635}
22636
22637TEST_F(FormatTest, UnderstandPragmaOption) {
22638 verifyFormat("#pragma option -C -A");
22639
22640 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
22641}
22642
22643TEST_F(FormatTest, UnderstandPragmaRegion) {
22644 auto Style = getLLVMStyleWithColumns(ColumnLimit: 0);
22645 verifyFormat("#pragma region TEST(FOO : BAR)", Style);
22646 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style);
22647}
22648
22649TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) {
22650 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 20);
22651
22652 // See PR41213
22653 verifyFormat("/*\n"
22654 " *\t9012345\n"
22655 " * /8901\n"
22656 " */",
22657 "/*\n"
22658 " *\t9012345 /8901\n"
22659 " */",
22660 Style);
22661 verifyFormat("/*\n"
22662 " *345678\n"
22663 " *\t/8901\n"
22664 " */",
22665 "/*\n"
22666 " *345678\t/8901\n"
22667 " */",
22668 Style);
22669
22670 verifyFormat("int a; // the\n"
22671 " // comment",
22672 Style);
22673 verifyNoChange("int a; /* first line\n"
22674 " * second\n"
22675 " * line third\n"
22676 " * line\n"
22677 " */",
22678 Style);
22679 verifyFormat("int a; // first line\n"
22680 " // second\n"
22681 " // line third\n"
22682 " // line",
22683 "int a; // first line\n"
22684 " // second line\n"
22685 " // third line",
22686 Style);
22687
22688 Style.PenaltyExcessCharacter = 90;
22689 verifyFormat("int a; // the comment", Style);
22690 verifyFormat("int a; // the comment\n"
22691 " // aaa",
22692 "int a; // the comment aaa", Style);
22693 verifyNoChange("int a; /* first line\n"
22694 " * second line\n"
22695 " * third line\n"
22696 " */",
22697 Style);
22698 verifyFormat("int a; // first line\n"
22699 " // second line\n"
22700 " // third line",
22701 Style);
22702 // FIXME: Investigate why this is not getting the same layout as the test
22703 // above.
22704 verifyFormat("int a; /* first line\n"
22705 " * second line\n"
22706 " * third line\n"
22707 " */",
22708 "int a; /* first line second line third line"
22709 "\n*/",
22710 Style);
22711
22712 verifyFormat("// foo bar baz bazfoo\n"
22713 "// foo bar foo bar",
22714 "// foo bar baz bazfoo\n"
22715 "// foo bar foo bar",
22716 Style);
22717 verifyFormat("// foo bar baz bazfoo\n"
22718 "// foo bar foo bar",
22719 "// foo bar baz bazfoo\n"
22720 "// foo bar foo bar",
22721 Style);
22722
22723 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22724 // next one.
22725 verifyFormat("// foo bar baz bazfoo\n"
22726 "// bar foo bar",
22727 "// foo bar baz bazfoo bar\n"
22728 "// foo bar",
22729 Style);
22730
22731 // FIXME: unstable test case
22732 EXPECT_EQ("// foo bar baz bazfoo\n"
22733 "// foo bar baz bazfoo\n"
22734 "// bar foo bar",
22735 format("// foo bar baz bazfoo\n"
22736 "// foo bar baz bazfoo bar\n"
22737 "// foo bar",
22738 Style));
22739
22740 // FIXME: unstable test case
22741 EXPECT_EQ("// foo bar baz bazfoo\n"
22742 "// foo bar baz bazfoo\n"
22743 "// bar foo bar",
22744 format("// foo bar baz bazfoo\n"
22745 "// foo bar baz bazfoo bar\n"
22746 "// foo bar",
22747 Style));
22748
22749 // Make sure we do not keep protruding characters if strict mode reflow is
22750 // cheaper than keeping protruding characters.
22751 Style.ColumnLimit = 21;
22752 verifyFormat("// foo foo foo foo\n"
22753 "// foo foo foo foo\n"
22754 "// foo foo foo foo",
22755 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style);
22756
22757 verifyFormat("int a = /* long block\n"
22758 " comment */\n"
22759 " 42;",
22760 "int a = /* long block comment */ 42;", Style);
22761}
22762
22763TEST_F(FormatTest, BreakPenaltyAfterLParen) {
22764 FormatStyle Style = getLLVMStyle();
22765 Style.ColumnLimit = 8;
22766 Style.PenaltyExcessCharacter = 15;
22767 verifyFormat("int foo(\n"
22768 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22769 Style);
22770 Style.PenaltyBreakOpenParenthesis = 200;
22771 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22772 "int foo(\n"
22773 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22774 Style);
22775}
22776
22777TEST_F(FormatTest, BreakPenaltyAfterCastLParen) {
22778 FormatStyle Style = getLLVMStyle();
22779 Style.ColumnLimit = 5;
22780 Style.PenaltyExcessCharacter = 150;
22781 verifyFormat("foo((\n"
22782 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22783
22784 Style);
22785 Style.PenaltyBreakOpenParenthesis = 100'000;
22786 verifyFormat("foo((int)\n"
22787 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22788 "foo((\n"
22789 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22790 Style);
22791}
22792
22793TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) {
22794 FormatStyle Style = getLLVMStyle();
22795 Style.ColumnLimit = 4;
22796 Style.PenaltyExcessCharacter = 100;
22797 verifyFormat("for (\n"
22798 " int iiiiiiiiiiiiiiiii =\n"
22799 " 0;\n"
22800 " iiiiiiiiiiiiiiiii <\n"
22801 " 2;\n"
22802 " iiiiiiiiiiiiiiiii++) {\n"
22803 "}",
22804
22805 Style);
22806 Style.PenaltyBreakOpenParenthesis = 1250;
22807 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22808 " 0;\n"
22809 " iiiiiiiiiiiiiiiii <\n"
22810 " 2;\n"
22811 " iiiiiiiiiiiiiiiii++) {\n"
22812 "}",
22813 "for (\n"
22814 " int iiiiiiiiiiiiiiiii =\n"
22815 " 0;\n"
22816 " iiiiiiiiiiiiiiiii <\n"
22817 " 2;\n"
22818 " iiiiiiiiiiiiiiiii++) {\n"
22819 "}",
22820 Style);
22821}
22822
22823TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) {
22824 auto Style = getLLVMStyle();
22825 EXPECT_EQ(Style.PenaltyBreakBeforeMemberAccess, 150u);
22826
22827 Style.ColumnLimit = 60;
22828 Style.PenaltyBreakBeforeMemberAccess = 110;
22829 verifyFormat("aaaaaaaa.aaaaaaaa.bbbbbbbb()\n"
22830 " .ccccccccccccccccccccc(dddddddd);\n"
22831 "aaaaaaaa.aaaaaaaa\n"
22832 " .bbbbbbbb(cccccccccccccccccccccccccccccccc);",
22833 Style);
22834
22835 Style.ColumnLimit = 13;
22836 verifyFormat("foo->bar\n"
22837 " .b(a);",
22838 Style);
22839}
22840
22841TEST_F(FormatTest, BreakPenaltyScopeResolution) {
22842 FormatStyle Style = getLLVMStyle();
22843 Style.ColumnLimit = 20;
22844 Style.PenaltyExcessCharacter = 100;
22845 verifyFormat("unsigned long\n"
22846 "foo::bar();",
22847 Style);
22848 Style.PenaltyBreakScopeResolution = 10;
22849 verifyFormat("unsigned long foo::\n"
22850 " bar();",
22851 Style);
22852}
22853
22854TEST_F(FormatTest, WorksFor8bitEncodings) {
22855 // FIXME: unstable test case
22856 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22857 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22858 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22859 "\"\xef\xee\xf0\xf3...\"",
22860 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22861 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22862 "\xef\xee\xf0\xf3...\"",
22863 getLLVMStyleWithColumns(12)));
22864}
22865
22866TEST_F(FormatTest, HandlesUTF8BOM) {
22867 verifyFormat("\xef\xbb\xbf");
22868 verifyFormat("\xef\xbb\xbf#include <iostream>");
22869 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22870
22871 auto Style = getLLVMStyle();
22872 Style.KeepEmptyLines.AtStartOfFile = false;
22873 verifyFormat("\xef\xbb\xbf#include <iostream>",
22874 "\xef\xbb\xbf\n#include <iostream>", Style);
22875}
22876
22877// FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22878#if !defined(_MSC_VER)
22879
22880TEST_F(FormatTest, CountsUTF8CharactersProperly) {
22881 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22882 getLLVMStyleWithColumns(35));
22883 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22884 getLLVMStyleWithColumns(31));
22885 verifyFormat("// Однажды в студёную зимнюю пору...",
22886 getLLVMStyleWithColumns(36));
22887 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22888 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22889 getLLVMStyleWithColumns(39));
22890 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22891 getLLVMStyleWithColumns(35));
22892}
22893
22894TEST_F(FormatTest, SplitsUTF8Strings) {
22895 // Non-printable characters' width is currently considered to be the length in
22896 // bytes in UTF8. The characters can be displayed in very different manner
22897 // (zero-width, single width with a substitution glyph, expanded to their code
22898 // (e.g. "<8d>"), so there's no single correct way to handle them.
22899 // FIXME: unstable test case
22900 EXPECT_EQ("\"aaaaÄ\"\n"
22901 "\"\xc2\x8d\";",
22902 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22903 // FIXME: unstable test case
22904 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22905 "\"\xc2\x8d\";",
22906 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22907 // FIXME: unstable test case
22908 EXPECT_EQ("\"Однажды, в \"\n"
22909 "\"студёную \"\n"
22910 "\"зимнюю \"\n"
22911 "\"пору,\"",
22912 format("\"Однажды, в студёную зимнюю пору,\"",
22913 getLLVMStyleWithColumns(13)));
22914 // FIXME: unstable test case
22915 EXPECT_EQ(
22916 "\"一 二 三 \"\n"
22917 "\"四 五六 \"\n"
22918 "\"七 八 九 \"\n"
22919 "\"十\"",
22920 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22921 // FIXME: unstable test case
22922 EXPECT_EQ("\"一\t\"\n"
22923 "\"二 \t\"\n"
22924 "\"三 四 \"\n"
22925 "\"五\t\"\n"
22926 "\"六 \t\"\n"
22927 "\"七 \"\n"
22928 "\"八九十\tqq\"",
22929 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
22930 getLLVMStyleWithColumns(11)));
22931
22932 // UTF8 character in an escape sequence.
22933 // FIXME: unstable test case
22934 EXPECT_EQ("\"aaaaaa\"\n"
22935 "\"\\\xC2\x8D\"",
22936 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22937}
22938
22939TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
22940 verifyFormat("const char *sssss =\n"
22941 " \"一二三四五六七八\\\n"
22942 " 九 十\";",
22943 "const char *sssss = \"一二三四五六七八\\\n"
22944 " 九 十\";",
22945 getLLVMStyleWithColumns(30));
22946}
22947
22948TEST_F(FormatTest, SplitsUTF8LineComments) {
22949 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22950 verifyFormat("// Я из лесу\n"
22951 "// вышел; был\n"
22952 "// сильный\n"
22953 "// мороз.",
22954 "// Я из лесу вышел; был сильный мороз.",
22955 getLLVMStyleWithColumns(13));
22956 verifyFormat("// 一二三\n"
22957 "// 四五六七\n"
22958 "// 八 九\n"
22959 "// 十",
22960 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22961}
22962
22963TEST_F(FormatTest, SplitsUTF8BlockComments) {
22964 verifyFormat("/* Гляжу,\n"
22965 " * поднимается\n"
22966 " * медленно в\n"
22967 " * гору\n"
22968 " * Лошадка,\n"
22969 " * везущая\n"
22970 " * хворосту\n"
22971 " * воз. */",
22972 "/* Гляжу, поднимается медленно в гору\n"
22973 " * Лошадка, везущая хворосту воз. */",
22974 getLLVMStyleWithColumns(13));
22975 verifyFormat("/* 一二三\n"
22976 " * 四五六七\n"
22977 " * 八 九\n"
22978 " * 十 */",
22979 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22980 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22981 " * 𝕓𝕪𝕥𝕖\n"
22982 " * 𝖀𝕿𝕱-𝟠 */",
22983 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22984}
22985
22986#endif // _MSC_VER
22987
22988TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
22989 FormatStyle Style = getLLVMStyle();
22990
22991 Style.ConstructorInitializerIndentWidth = 4;
22992 verifyFormat(
22993 "SomeClass::Constructor()\n"
22994 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22995 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22996 Style);
22997
22998 Style.ConstructorInitializerIndentWidth = 2;
22999 verifyFormat(
23000 "SomeClass::Constructor()\n"
23001 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
23002 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
23003 Style);
23004
23005 Style.ConstructorInitializerIndentWidth = 0;
23006 verifyFormat(
23007 "SomeClass::Constructor()\n"
23008 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
23009 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
23010 Style);
23011 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
23012 verifyFormat(
23013 "SomeLongTemplateVariableName<\n"
23014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
23015 Style);
23016 verifyFormat("bool smaller = 1 < "
23017 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
23018 " "
23019 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
23020 Style);
23021
23022 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
23023 verifyFormat("SomeClass::Constructor() :\n"
23024 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
23025 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
23026 Style);
23027}
23028
23029TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
23030 FormatStyle Style = getLLVMStyle();
23031 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
23032 Style.ConstructorInitializerIndentWidth = 4;
23033 verifyFormat("SomeClass::Constructor()\n"
23034 " : a(a)\n"
23035 " , b(b)\n"
23036 " , c(c) {}",
23037 Style);
23038 verifyFormat("SomeClass::Constructor()\n"
23039 " : a(a) {}",
23040 Style);
23041
23042 Style.ColumnLimit = 0;
23043 verifyFormat("SomeClass::Constructor()\n"
23044 " : a(a) {}",
23045 Style);
23046 verifyFormat("SomeClass::Constructor() noexcept\n"
23047 " : a(a) {}",
23048 Style);
23049 verifyFormat("SomeClass::Constructor()\n"
23050 " : a(a)\n"
23051 " , b(b)\n"
23052 " , c(c) {}",
23053 Style);
23054 verifyFormat("SomeClass::Constructor()\n"
23055 " : a(a) {\n"
23056 " foo();\n"
23057 " bar();\n"
23058 "}",
23059 Style);
23060
23061 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
23062 verifyFormat("SomeClass::Constructor()\n"
23063 " : a(a)\n"
23064 " , b(b)\n"
23065 " , c(c) {\n}",
23066 Style);
23067 verifyFormat("SomeClass::Constructor()\n"
23068 " : a(a) {\n}",
23069 Style);
23070
23071 Style.ColumnLimit = 80;
23072 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
23073 Style.ConstructorInitializerIndentWidth = 2;
23074 verifyFormat("SomeClass::Constructor()\n"
23075 " : a(a)\n"
23076 " , b(b)\n"
23077 " , c(c) {}",
23078 Style);
23079
23080 Style.ConstructorInitializerIndentWidth = 0;
23081 verifyFormat("SomeClass::Constructor()\n"
23082 ": a(a)\n"
23083 ", b(b)\n"
23084 ", c(c) {}",
23085 Style);
23086
23087 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
23088 Style.ConstructorInitializerIndentWidth = 4;
23089 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
23090 verifyFormat(
23091 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
23092 Style);
23093 verifyFormat(
23094 "SomeClass::Constructor()\n"
23095 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
23096 Style);
23097 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
23098 verifyFormat("SomeClass::Constructor()\n"
23099 " : aaaaaaaa(aaaaaaaa) {}",
23100 Style);
23101 verifyFormat("SomeClass::Constructor()\n"
23102 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
23103 Style);
23104 verifyFormat(
23105 "SomeClass::Constructor()\n"
23106 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
23107 Style);
23108
23109 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
23110 Style.ConstructorInitializerIndentWidth = 4;
23111 Style.ColumnLimit = 60;
23112 verifyFormat("SomeClass::Constructor()\n"
23113 " : aaaaaaaa(aaaaaaaa)\n"
23114 " , aaaaaaaa(aaaaaaaa)\n"
23115 " , aaaaaaaa(aaaaaaaa) {}",
23116 Style);
23117 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly;
23118 verifyFormat("SomeClass::Constructor()\n"
23119 " : aaaaaaaa(aaaaaaaa)\n"
23120 " , aaaaaaaa(aaaaaaaa)\n"
23121 " , aaaaaaaa(aaaaaaaa) {}",
23122 Style);
23123}
23124
23125TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) {
23126 FormatStyle Style = getLLVMStyle();
23127 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
23128 Style.ConstructorInitializerIndentWidth = 4;
23129 verifyFormat("SomeClass::Constructor()\n"
23130 " : a{a}\n"
23131 " , b{b} {}",
23132 Style);
23133 verifyFormat("SomeClass::Constructor()\n"
23134 " : a{a}\n"
23135 "#if CONDITION\n"
23136 " , b{b}\n"
23137 "#endif\n"
23138 "{\n}",
23139 Style);
23140 Style.ConstructorInitializerIndentWidth = 2;
23141 verifyFormat("SomeClass::Constructor()\n"
23142 "#if CONDITION\n"
23143 " : a{a}\n"
23144 "#endif\n"
23145 " , b{b}\n"
23146 " , c{c} {\n}",
23147 Style);
23148 Style.ConstructorInitializerIndentWidth = 0;
23149 verifyFormat("SomeClass::Constructor()\n"
23150 ": a{a}\n"
23151 "#ifdef CONDITION\n"
23152 ", b{b}\n"
23153 "#else\n"
23154 ", c{c}\n"
23155 "#endif\n"
23156 ", d{d} {\n}",
23157 Style);
23158 Style.ConstructorInitializerIndentWidth = 4;
23159 verifyFormat("SomeClass::Constructor()\n"
23160 " : a{a}\n"
23161 "#if WINDOWS\n"
23162 "#if DEBUG\n"
23163 " , b{0}\n"
23164 "#else\n"
23165 " , b{1}\n"
23166 "#endif\n"
23167 "#else\n"
23168 "#if DEBUG\n"
23169 " , b{2}\n"
23170 "#else\n"
23171 " , b{3}\n"
23172 "#endif\n"
23173 "#endif\n"
23174 "{\n}",
23175 Style);
23176 verifyFormat("SomeClass::Constructor()\n"
23177 " : a{a}\n"
23178 "#if WINDOWS\n"
23179 " , b{0}\n"
23180 "#if DEBUG\n"
23181 " , c{0}\n"
23182 "#else\n"
23183 " , c{1}\n"
23184 "#endif\n"
23185 "#else\n"
23186 "#if DEBUG\n"
23187 " , c{2}\n"
23188 "#else\n"
23189 " , c{3}\n"
23190 "#endif\n"
23191 " , b{1}\n"
23192 "#endif\n"
23193 "{\n}",
23194 Style);
23195}
23196
23197TEST_F(FormatTest, Destructors) {
23198 verifyFormat("void F(int &i) { i.~int(); }");
23199 verifyFormat("void F(int &i) { i->~int(); }");
23200}
23201
23202TEST_F(FormatTest, FormatsWithWebKitStyle) {
23203 FormatStyle Style = getWebKitStyle();
23204
23205 // Don't indent in outer namespaces.
23206 verifyFormat("namespace outer {\n"
23207 "int i;\n"
23208 "namespace inner {\n"
23209 " int i;\n"
23210 "} // namespace inner\n"
23211 "} // namespace outer\n"
23212 "namespace other_outer {\n"
23213 "int i;\n"
23214 "}",
23215 Style);
23216
23217 // Don't indent case labels.
23218 verifyFormat("switch (variable) {\n"
23219 "case 1:\n"
23220 "case 2:\n"
23221 " doSomething();\n"
23222 " break;\n"
23223 "default:\n"
23224 " ++variable;\n"
23225 "}",
23226 Style);
23227
23228 // Wrap before binary operators.
23229 verifyFormat(
23230 "void f()\n"
23231 "{\n"
23232 " if (aaaaaaaaaaaaaaaa\n"
23233 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
23234 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
23235 " return;\n"
23236 "}",
23237 "void f() {\n"
23238 "if (aaaaaaaaaaaaaaaa\n"
23239 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
23240 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
23241 "return;\n"
23242 "}",
23243 Style);
23244
23245 // Allow functions on a single line.
23246 verifyFormat("void f() { return; }", Style);
23247
23248 // Allow empty blocks on a single line and insert a space in empty blocks.
23249 verifyFormat("void f() { }", "void f() {}", Style);
23250 verifyFormat("while (true) { }", "while (true) {}", Style);
23251 // However, don't merge non-empty short loops.
23252 verifyFormat("while (true) {\n"
23253 " continue;\n"
23254 "}",
23255 "while (true) { continue; }", Style);
23256
23257 // Constructor initializers are formatted one per line with the "," on the
23258 // new line.
23259 verifyFormat("Constructor()\n"
23260 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
23261 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
23262 " aaaaaaaaaaaaaa)\n"
23263 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
23264 "{\n"
23265 "}",
23266 Style);
23267 verifyFormat("SomeClass::Constructor()\n"
23268 " : a(a)\n"
23269 "{\n"
23270 "}",
23271 Style);
23272 verifyFormat("SomeClass::Constructor()\n"
23273 " : a(a)\n"
23274 "{\n"
23275 "}",
23276 "SomeClass::Constructor():a(a){}", Style);
23277 verifyFormat("SomeClass::Constructor()\n"
23278 " : a(a)\n"
23279 " , b(b)\n"
23280 " , c(c)\n"
23281 "{\n"
23282 "}",
23283 Style);
23284 verifyFormat("SomeClass::Constructor()\n"
23285 " : a(a)\n"
23286 "{\n"
23287 " foo();\n"
23288 " bar();\n"
23289 "}",
23290 Style);
23291
23292 // Access specifiers should be aligned left.
23293 verifyFormat("class C {\n"
23294 "public:\n"
23295 " int i;\n"
23296 "};",
23297 Style);
23298
23299 // Do not align comments.
23300 verifyFormat("int a; // Do not\n"
23301 "double b; // align comments.",
23302 Style);
23303
23304 // Do not align operands.
23305 verifyFormat("ASSERT(aaaa\n"
23306 " || bbbb);",
23307 "ASSERT ( aaaa\n||bbbb);", Style);
23308
23309 // Accept input's line breaks.
23310 verifyFormat("if (aaaaaaaaaaaaaaa\n"
23311 " || bbbbbbbbbbbbbbb) {\n"
23312 " i++;\n"
23313 "}",
23314 "if (aaaaaaaaaaaaaaa\n"
23315 "|| bbbbbbbbbbbbbbb) { i++; }",
23316 Style);
23317 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
23318 " i++;\n"
23319 "}",
23320 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style);
23321
23322 // Don't automatically break all macro definitions (llvm.org/PR17842).
23323 verifyFormat("#define aNumber 10", Style);
23324 // However, generally keep the line breaks that the user authored.
23325 verifyFormat("#define aNumber \\\n"
23326 " 10",
23327 "#define aNumber \\\n"
23328 " 10",
23329 Style);
23330
23331 // Keep empty and one-element array literals on a single line.
23332 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
23333 " copyItems:YES];",
23334 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
23335 "copyItems:YES];",
23336 Style);
23337 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
23338 " copyItems:YES];",
23339 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
23340 " copyItems:YES];",
23341 Style);
23342 // FIXME: This does not seem right, there should be more indentation before
23343 // the array literal's entries. Nested blocks have the same problem.
23344 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
23345 " @\"a\",\n"
23346 " @\"a\"\n"
23347 "]\n"
23348 " copyItems:YES];",
23349 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
23350 " @\"a\",\n"
23351 " @\"a\"\n"
23352 " ]\n"
23353 " copyItems:YES];",
23354 Style);
23355 verifyFormat(
23356 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
23357 " copyItems:YES];",
23358 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
23359 " copyItems:YES];",
23360 Style);
23361
23362 verifyFormat("[self.a b:c c:d];", Style);
23363 verifyFormat("[self.a b:c\n"
23364 " c:d];",
23365 "[self.a b:c\n"
23366 "c:d];",
23367 Style);
23368}
23369
23370TEST_F(FormatTest, FormatsLambdas) {
23371 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
23372 verifyFormat(
23373 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
23374 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
23375 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
23376 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
23377 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
23378 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
23379 verifyFormat("auto c = [a = [b = 42] {}] {};");
23380 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
23381 verifyFormat("int x = f(*+[] {});");
23382 verifyFormat("void f() {\n"
23383 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
23384 "}");
23385 verifyFormat("void f() {\n"
23386 " other(x.begin(), //\n"
23387 " x.end(), //\n"
23388 " [&](int, int) { return 1; });\n"
23389 "}");
23390 verifyFormat("void f() {\n"
23391 " other.other.other.other.other(\n"
23392 " x.begin(), x.end(),\n"
23393 " [something, rather](int, int, int, int, int, int, int) { "
23394 "return 1; });\n"
23395 "}");
23396 verifyFormat(
23397 "void f() {\n"
23398 " other.other.other.other.other(\n"
23399 " x.begin(), x.end(),\n"
23400 " [something, rather](int, int, int, int, int, int, int) {\n"
23401 " //\n"
23402 " });\n"
23403 "}");
23404 verifyFormat("SomeFunction([]() { // A cool function...\n"
23405 " return 43;\n"
23406 "});");
23407 verifyFormat("SomeFunction([]() {\n"
23408 "#define A a\n"
23409 " return 43;\n"
23410 "});",
23411 "SomeFunction([](){\n"
23412 "#define A a\n"
23413 "return 43;\n"
23414 "});");
23415 verifyFormat("void f() {\n"
23416 " SomeFunction([](decltype(x), A *a) {});\n"
23417 " SomeFunction([](typeof(x), A *a) {});\n"
23418 " SomeFunction([](_Atomic(x), A *a) {});\n"
23419 " SomeFunction([](__underlying_type(x), A *a) {});\n"
23420 "}");
23421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23422 " [](const aaaaaaaaaa &a) { return a; });");
23423 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
23424 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
23425 "});");
23426 verifyFormat("Constructor()\n"
23427 " : Field([] { // comment\n"
23428 " int i;\n"
23429 " }) {}");
23430 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
23431 " return some_parameter.size();\n"
23432 "};");
23433 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
23434 " [](const string &s) { return s; };");
23435 verifyFormat("int i = aaaaaa ? 1 //\n"
23436 " : [] {\n"
23437 " return 2; //\n"
23438 " }();");
23439 verifyFormat("llvm::errs() << \"number of twos is \"\n"
23440 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
23441 " return x == 2; // force break\n"
23442 " });");
23443 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23444 " [=](int iiiiiiiiiiii) {\n"
23445 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
23446 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
23447 " });",
23448 getLLVMStyleWithColumns(60));
23449
23450 verifyFormat("SomeFunction({[&] {\n"
23451 " // comment\n"
23452 " },\n"
23453 " [&] {\n"
23454 " // comment\n"
23455 " }});");
23456 verifyFormat("SomeFunction({[&] {\n"
23457 " // comment\n"
23458 "}});");
23459 verifyFormat(
23460 "virtual aaaaaaaaaaaaaaaa(\n"
23461 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
23462 " aaaaa aaaaaaaaa);");
23463
23464 // Lambdas with return types.
23465 verifyFormat("int c = []() -> int { return 2; }();");
23466 verifyFormat("int c = []() -> int * { return 2; }();");
23467 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
23468 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
23469 verifyFormat("foo([]() noexcept -> int {});");
23470 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
23471 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
23472 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
23473 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
23474 verifyFormat("[a, a]() -> a<1> {};");
23475 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
23476 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
23477 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
23478 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
23479 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
23480 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
23481 verifyFormat("[]() -> foo<!5> { return {}; };");
23482 verifyFormat("[]() -> foo<~5> { return {}; };");
23483 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
23484 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
23485 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
23486 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
23487 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
23488 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
23489 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
23490 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
23491 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
23492 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
23493 verifyFormat("namespace bar {\n"
23494 "// broken:\n"
23495 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
23496 "} // namespace bar");
23497 verifyFormat("namespace bar {\n"
23498 "// broken:\n"
23499 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
23500 "} // namespace bar");
23501 verifyFormat("namespace bar {\n"
23502 "// broken:\n"
23503 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
23504 "} // namespace bar");
23505 verifyFormat("namespace bar {\n"
23506 "// broken:\n"
23507 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
23508 "} // namespace bar");
23509 verifyFormat("namespace bar {\n"
23510 "// broken:\n"
23511 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
23512 "} // namespace bar");
23513 verifyFormat("namespace bar {\n"
23514 "// broken:\n"
23515 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
23516 "} // namespace bar");
23517 verifyFormat("namespace bar {\n"
23518 "// broken:\n"
23519 "auto foo{[]() -> foo<!5> { return {}; }};\n"
23520 "} // namespace bar");
23521 verifyFormat("namespace bar {\n"
23522 "// broken:\n"
23523 "auto foo{[]() -> foo<~5> { return {}; }};\n"
23524 "} // namespace bar");
23525 verifyFormat("namespace bar {\n"
23526 "// broken:\n"
23527 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
23528 "} // namespace bar");
23529 verifyFormat("namespace bar {\n"
23530 "// broken:\n"
23531 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
23532 "} // namespace bar");
23533 verifyFormat("namespace bar {\n"
23534 "// broken:\n"
23535 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
23536 "} // namespace bar");
23537 verifyFormat("namespace bar {\n"
23538 "// broken:\n"
23539 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
23540 "} // namespace bar");
23541 verifyFormat("namespace bar {\n"
23542 "// broken:\n"
23543 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
23544 "} // namespace bar");
23545 verifyFormat("namespace bar {\n"
23546 "// broken:\n"
23547 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
23548 "} // namespace bar");
23549 verifyFormat("namespace bar {\n"
23550 "// broken:\n"
23551 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
23552 "} // namespace bar");
23553 verifyFormat("namespace bar {\n"
23554 "// broken:\n"
23555 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
23556 "} // namespace bar");
23557 verifyFormat("namespace bar {\n"
23558 "// broken:\n"
23559 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
23560 "} // namespace bar");
23561 verifyFormat("namespace bar {\n"
23562 "// broken:\n"
23563 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
23564 "} // namespace bar");
23565 verifyFormat("[]() -> a<1> {};");
23566 verifyFormat("[]() -> a<1> { ; };");
23567 verifyFormat("[]() -> a<1> { ; }();");
23568 verifyFormat("[a, a]() -> a<true> {};");
23569 verifyFormat("[]() -> a<true> {};");
23570 verifyFormat("[]() -> a<true> { ; };");
23571 verifyFormat("[]() -> a<true> { ; }();");
23572 verifyFormat("[a, a]() -> a<false> {};");
23573 verifyFormat("[]() -> a<false> {};");
23574 verifyFormat("[]() -> a<false> { ; };");
23575 verifyFormat("[]() -> a<false> { ; }();");
23576 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
23577 verifyFormat("namespace bar {\n"
23578 "auto foo{[]() -> foo<false> { ; }};\n"
23579 "} // namespace bar");
23580 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
23581 " int j) -> int {\n"
23582 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
23583 "};");
23584 verifyFormat(
23585 "aaaaaaaaaaaaaaaaaaaaaa(\n"
23586 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
23587 " return aaaaaaaaaaaaaaaaa;\n"
23588 " });",
23589 getLLVMStyleWithColumns(70));
23590 verifyFormat("[]() //\n"
23591 " -> int {\n"
23592 " return 1; //\n"
23593 "};");
23594 verifyFormat("[]() -> Void<T...> {};");
23595 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
23596 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
23597 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
23598 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
23599 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
23600 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
23601 verifyFormat("return int{[x = x]() { return x; }()};");
23602
23603 // Lambdas with explicit template argument lists.
23604 verifyFormat(
23605 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
23606 verifyFormat("auto L = []<class T>(T) {\n"
23607 " {\n"
23608 " f();\n"
23609 " g();\n"
23610 " }\n"
23611 "};");
23612 verifyFormat("auto L = []<class... T>(T...) {\n"
23613 " {\n"
23614 " f();\n"
23615 " g();\n"
23616 " }\n"
23617 "};");
23618 verifyFormat("auto L = []<typename... T>(T...) {\n"
23619 " {\n"
23620 " f();\n"
23621 " g();\n"
23622 " }\n"
23623 "};");
23624 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23625 " {\n"
23626 " f();\n"
23627 " g();\n"
23628 " }\n"
23629 "};");
23630 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23631 " {\n"
23632 " f();\n"
23633 " g();\n"
23634 " }\n"
23635 "};");
23636 verifyFormat("auto L = []<int... T>(T...) {\n"
23637 " {\n"
23638 " f();\n"
23639 " g();\n"
23640 " }\n"
23641 "};");
23642 verifyFormat("auto L = []<Foo... T>(T...) {\n"
23643 " {\n"
23644 " f();\n"
23645 " g();\n"
23646 " }\n"
23647 "};");
23648
23649 // Lambdas that fit on a single line within an argument list are not forced
23650 // onto new lines.
23651 verifyFormat("SomeFunction([] {});");
23652 verifyFormat("SomeFunction(0, [] {});");
23653 verifyFormat("SomeFunction([] {}, 0);");
23654 verifyFormat("SomeFunction(0, [] {}, 0);");
23655 verifyFormat("SomeFunction([] { return 0; }, 0);");
23656 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23657 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23658 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23659 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23660 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
23661 // Exceeded column limit. We need to break.
23662 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23663 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
23664 "return 0; }, b);");
23665
23666 // Multiple multi-line lambdas in the same parentheses change indentation
23667 // rules. These lambdas are always forced to start on new lines.
23668 verifyFormat("SomeFunction(\n"
23669 " []() {\n"
23670 " //\n"
23671 " },\n"
23672 " []() {\n"
23673 " //\n"
23674 " });");
23675
23676 // A multi-line lambda passed as arg0 is always pushed to the next line.
23677 verifyFormat("SomeFunction(\n"
23678 " [this] {\n"
23679 " //\n"
23680 " },\n"
23681 " 1);");
23682
23683 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23684 // the arg0 case above.
23685 auto Style = getGoogleStyle();
23686 Style.BinPackArguments = false;
23687 verifyFormat("SomeFunction(\n"
23688 " a,\n"
23689 " [this] {\n"
23690 " //\n"
23691 " },\n"
23692 " b);",
23693 Style);
23694 verifyFormat("SomeFunction(\n"
23695 " a,\n"
23696 " [this] {\n"
23697 " //\n"
23698 " },\n"
23699 " b);");
23700
23701 // A lambda with a very long line forces arg0 to be pushed out irrespective of
23702 // the BinPackArguments value (as long as the code is wide enough).
23703 verifyFormat(
23704 "something->SomeFunction(\n"
23705 " a,\n"
23706 " [this] {\n"
23707 " "
23708 "D0000000000000000000000000000000000000000000000000000000000001();\n"
23709 " },\n"
23710 " b);");
23711
23712 // A multi-line lambda is pulled up as long as the introducer fits on the
23713 // previous line and there are no further args.
23714 verifyFormat("function(1, [this, that] {\n"
23715 " //\n"
23716 "});");
23717 verifyFormat("function([this, that] {\n"
23718 " //\n"
23719 "});");
23720 // FIXME: this format is not ideal and we should consider forcing the first
23721 // arg onto its own line.
23722 verifyFormat("function(a, b, c, //\n"
23723 " d, [this, that] {\n"
23724 " //\n"
23725 " });");
23726
23727 // Multiple lambdas are treated correctly even when there is a short arg0.
23728 verifyFormat("SomeFunction(\n"
23729 " 1,\n"
23730 " [this] {\n"
23731 " //\n"
23732 " },\n"
23733 " [this] {\n"
23734 " //\n"
23735 " },\n"
23736 " 1);");
23737
23738 // More complex introducers.
23739 verifyFormat("return [i, args...] {};");
23740
23741 // Not lambdas.
23742 verifyFormat("constexpr char hello[]{\"hello\"};");
23743 verifyFormat("double &operator[](int i) { return 0; }\n"
23744 "int i;");
23745 verifyFormat("std::unique_ptr<int[]> foo() {}");
23746 verifyFormat("int i = a[a][a]->f();");
23747 verifyFormat("int i = (*b)[a]->f();");
23748
23749 // Other corner cases.
23750 verifyFormat("void f() {\n"
23751 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23752 " );\n"
23753 "}");
23754 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23755
23756 // Lambdas created through weird macros.
23757 verifyFormat("void f() {\n"
23758 " MACRO((const AA &a) { return 1; });\n"
23759 " MACRO((AA &a) { return 1; });\n"
23760 "}");
23761
23762 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23763 " doo_dah();\n"
23764 " doo_dah();\n"
23765 " })) {\n"
23766 "}");
23767 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23768 " doo_dah();\n"
23769 " doo_dah();\n"
23770 " })) {\n"
23771 "}");
23772 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23773 " doo_dah();\n"
23774 " doo_dah();\n"
23775 " })) {\n"
23776 "}");
23777 verifyFormat("auto lambda = []() {\n"
23778 " int a = 2\n"
23779 "#if A\n"
23780 " + 2\n"
23781 "#endif\n"
23782 " ;\n"
23783 "};");
23784
23785 // Lambdas with complex multiline introducers.
23786 verifyFormat(
23787 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23788 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23789 " -> ::std::unordered_set<\n"
23790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23791 " //\n"
23792 " });");
23793
23794 FormatStyle LLVMStyle = getLLVMStyleWithColumns(ColumnLimit: 60);
23795 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23796 " [](auto n) noexcept [[back_attr]]\n"
23797 " -> std::unordered_map<very_long_type_name_A,\n"
23798 " very_long_type_name_B> {\n"
23799 " really_do_something();\n"
23800 " });",
23801 LLVMStyle);
23802 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23803 " [](auto n) constexpr\n"
23804 " -> std::unordered_map<very_long_type_name_A,\n"
23805 " very_long_type_name_B> {\n"
23806 " really_do_something();\n"
23807 " });",
23808 LLVMStyle);
23809
23810 FormatStyle DoNotMerge = getLLVMStyle();
23811 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
23812 verifyFormat("auto c = []() {\n"
23813 " return b;\n"
23814 "};",
23815 "auto c = []() { return b; };", DoNotMerge);
23816 verifyFormat("auto c = []() {\n"
23817 "};",
23818 " auto c = []() {};", DoNotMerge);
23819
23820 FormatStyle MergeEmptyOnly = getLLVMStyle();
23821 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty;
23822 verifyFormat("auto c = []() {\n"
23823 " return b;\n"
23824 "};",
23825 "auto c = []() {\n"
23826 " return b;\n"
23827 " };",
23828 MergeEmptyOnly);
23829 verifyFormat("auto c = []() {};",
23830 "auto c = []() {\n"
23831 "};",
23832 MergeEmptyOnly);
23833
23834 FormatStyle MergeInline = getLLVMStyle();
23835 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline;
23836 verifyFormat("auto c = []() {\n"
23837 " return b;\n"
23838 "};",
23839 "auto c = []() { return b; };", MergeInline);
23840 verifyFormat("function([]() { return b; })", MergeInline);
23841 verifyFormat("function([]() { return b; }, a)", MergeInline);
23842 verifyFormat("function(a, []() { return b; })", MergeInline);
23843 verifyFormat("auto guard = foo{[&] { exit_status = true; }};", MergeInline);
23844
23845 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23846 // AllowShortLambdasOnASingleLine
23847 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
23848 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
23849 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
23850 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23851 FormatStyle::ShortLambdaStyle::SLS_None;
23852 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23853 " []()\n"
23854 " {\n"
23855 " return 17;\n"
23856 " });",
23857 LLVMWithBeforeLambdaBody);
23858 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23859 " []()\n"
23860 " {\n"
23861 " });",
23862 LLVMWithBeforeLambdaBody);
23863 verifyFormat("auto fct_SLS_None = []()\n"
23864 "{\n"
23865 " return 17;\n"
23866 "};",
23867 LLVMWithBeforeLambdaBody);
23868 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23869 " []()\n"
23870 " {\n"
23871 " return Call(\n"
23872 " []()\n"
23873 " {\n"
23874 " return 17;\n"
23875 " });\n"
23876 " });",
23877 LLVMWithBeforeLambdaBody);
23878 verifyFormat("void Fct() {\n"
23879 " return {[]()\n"
23880 " {\n"
23881 " return 17;\n"
23882 " }};\n"
23883 "}",
23884 LLVMWithBeforeLambdaBody);
23885
23886 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23887 FormatStyle::ShortLambdaStyle::SLS_Empty;
23888 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23889 " []()\n"
23890 " {\n"
23891 " return 17;\n"
23892 " });",
23893 LLVMWithBeforeLambdaBody);
23894 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23895 LLVMWithBeforeLambdaBody);
23896 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23897 "ongFunctionName_SLS_Empty(\n"
23898 " []() {});",
23899 LLVMWithBeforeLambdaBody);
23900 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23901 " []()\n"
23902 " {\n"
23903 " return 17;\n"
23904 " });",
23905 LLVMWithBeforeLambdaBody);
23906 verifyFormat("auto fct_SLS_Empty = []()\n"
23907 "{\n"
23908 " return 17;\n"
23909 "};",
23910 LLVMWithBeforeLambdaBody);
23911 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23912 " []()\n"
23913 " {\n"
23914 " return Call([]() {});\n"
23915 " });",
23916 LLVMWithBeforeLambdaBody);
23917 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23918 " []()\n"
23919 " {\n"
23920 " return Call([]() {});\n"
23921 " });",
23922 LLVMWithBeforeLambdaBody);
23923 verifyFormat(
23924 "FctWithLongLineInLambda_SLS_Empty(\n"
23925 " []()\n"
23926 " {\n"
23927 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23928 " AndShouldNotBeConsiderAsInline,\n"
23929 " LambdaBodyMustBeBreak);\n"
23930 " });",
23931 LLVMWithBeforeLambdaBody);
23932
23933 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23934 FormatStyle::ShortLambdaStyle::SLS_Inline;
23935 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23936 LLVMWithBeforeLambdaBody);
23937 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23938 LLVMWithBeforeLambdaBody);
23939 verifyFormat("auto fct_SLS_Inline = []()\n"
23940 "{\n"
23941 " return 17;\n"
23942 "};",
23943 LLVMWithBeforeLambdaBody);
23944 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23945 "17; }); });",
23946 LLVMWithBeforeLambdaBody);
23947 verifyFormat(
23948 "FctWithLongLineInLambda_SLS_Inline(\n"
23949 " []()\n"
23950 " {\n"
23951 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23952 " AndShouldNotBeConsiderAsInline,\n"
23953 " LambdaBodyMustBeBreak);\n"
23954 " });",
23955 LLVMWithBeforeLambdaBody);
23956 verifyFormat("FctWithMultipleParams_SLS_Inline("
23957 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23958 " []() { return 17; });",
23959 LLVMWithBeforeLambdaBody);
23960 verifyFormat(
23961 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23962 LLVMWithBeforeLambdaBody);
23963
23964 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
23965 FormatStyle::ShortLambdaStyle::SLS_All;
23966 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23967 LLVMWithBeforeLambdaBody);
23968 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23969 LLVMWithBeforeLambdaBody);
23970 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23971 LLVMWithBeforeLambdaBody);
23972 verifyFormat("FctWithOneParam_SLS_All(\n"
23973 " []()\n"
23974 " {\n"
23975 " // A cool function...\n"
23976 " return 43;\n"
23977 " });",
23978 LLVMWithBeforeLambdaBody);
23979 verifyFormat("FctWithMultipleParams_SLS_All("
23980 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23981 " []() { return 17; });",
23982 LLVMWithBeforeLambdaBody);
23983 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23984 LLVMWithBeforeLambdaBody);
23985 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23986 LLVMWithBeforeLambdaBody);
23987 verifyFormat(
23988 "FctWithLongLineInLambda_SLS_All(\n"
23989 " []()\n"
23990 " {\n"
23991 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23992 " AndShouldNotBeConsiderAsInline,\n"
23993 " LambdaBodyMustBeBreak);\n"
23994 " });",
23995 LLVMWithBeforeLambdaBody);
23996 verifyFormat(
23997 "auto fct_SLS_All = []()\n"
23998 "{\n"
23999 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
24000 " AndShouldNotBeConsiderAsInline,\n"
24001 " LambdaBodyMustBeBreak);\n"
24002 "};",
24003 LLVMWithBeforeLambdaBody);
24004 LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine;
24005 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
24006 LLVMWithBeforeLambdaBody);
24007 verifyFormat(
24008 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
24009 " FirstParam,\n"
24010 " SecondParam,\n"
24011 " ThirdParam,\n"
24012 " FourthParam);",
24013 LLVMWithBeforeLambdaBody);
24014 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
24015 " []() { return "
24016 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
24017 " FirstParam,\n"
24018 " SecondParam,\n"
24019 " ThirdParam,\n"
24020 " FourthParam);",
24021 LLVMWithBeforeLambdaBody);
24022 verifyFormat(
24023 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
24024 " SecondParam,\n"
24025 " ThirdParam,\n"
24026 " FourthParam,\n"
24027 " []() { return SomeValueNotSoLong; });",
24028 LLVMWithBeforeLambdaBody);
24029 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
24030 " []()\n"
24031 " {\n"
24032 " return "
24033 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
24034 "eConsiderAsInline;\n"
24035 " });",
24036 LLVMWithBeforeLambdaBody);
24037 verifyFormat(
24038 "FctWithLongLineInLambda_SLS_All(\n"
24039 " []()\n"
24040 " {\n"
24041 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
24042 " AndShouldNotBeConsiderAsInline,\n"
24043 " LambdaBodyMustBeBreak);\n"
24044 " });",
24045 LLVMWithBeforeLambdaBody);
24046 verifyFormat("FctWithTwoParams_SLS_All(\n"
24047 " []()\n"
24048 " {\n"
24049 " // A cool function...\n"
24050 " return 43;\n"
24051 " },\n"
24052 " 87);",
24053 LLVMWithBeforeLambdaBody);
24054 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
24055 LLVMWithBeforeLambdaBody);
24056 verifyFormat(
24057 "FctWithTwoParams_SLS_All(\n"
24058 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
24059 LLVMWithBeforeLambdaBody);
24060 verifyFormat(
24061 "FctWithTwoParams_SLS_All(\n"
24062 " 87,\n"
24063 " []()\n"
24064 " {\n"
24065 " return "
24066 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
24067 " });",
24068 LLVMWithBeforeLambdaBody);
24069 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
24070 LLVMWithBeforeLambdaBody);
24071 verifyFormat(
24072 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
24073 LLVMWithBeforeLambdaBody);
24074 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
24075 "}); }, x);",
24076 LLVMWithBeforeLambdaBody);
24077 verifyFormat("TwoNestedLambdas_SLS_All(\n"
24078 " []()\n"
24079 " {\n"
24080 " // A cool function...\n"
24081 " return Call([]() { return 17; });\n"
24082 " });",
24083 LLVMWithBeforeLambdaBody);
24084 verifyFormat("TwoNestedLambdas_SLS_All(\n"
24085 " []()\n"
24086 " {\n"
24087 " return Call(\n"
24088 " []()\n"
24089 " {\n"
24090 " // A cool function...\n"
24091 " return 17;\n"
24092 " });\n"
24093 " });",
24094 LLVMWithBeforeLambdaBody);
24095
24096 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
24097 FormatStyle::ShortLambdaStyle::SLS_None;
24098
24099 verifyFormat("auto select = [this]() -> const Library::Object *\n"
24100 "{\n"
24101 " return MyAssignment::SelectFromList(this);\n"
24102 "};",
24103 LLVMWithBeforeLambdaBody);
24104
24105 verifyFormat("auto select = [this]() -> const Library::Object &\n"
24106 "{\n"
24107 " return MyAssignment::SelectFromList(this);\n"
24108 "};",
24109 LLVMWithBeforeLambdaBody);
24110
24111 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
24112 "{\n"
24113 " return MyAssignment::SelectFromList(this);\n"
24114 "};",
24115 LLVMWithBeforeLambdaBody);
24116
24117 verifyFormat("namespace test {\n"
24118 "class Test {\n"
24119 "public:\n"
24120 " Test() = default;\n"
24121 "};\n"
24122 "} // namespace test",
24123 LLVMWithBeforeLambdaBody);
24124
24125 // Lambdas with different indentation styles.
24126 Style = getLLVMStyleWithColumns(ColumnLimit: 60);
24127 verifyFormat("Result doSomething(Promise promise) {\n"
24128 " return promise.then(\n"
24129 " [this, obj = std::move(s)](int bar) mutable {\n"
24130 " return someObject.startAsyncAction().then(\n"
24131 " [this, &obj](Result result) mutable {\n"
24132 " result.processMore();\n"
24133 " });\n"
24134 " });\n"
24135 "}",
24136 Style);
24137 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24138 verifyFormat("Result doSomething(Promise promise) {\n"
24139 " return promise.then(\n"
24140 " [this, obj = std::move(s)](int bar) mutable {\n"
24141 " return obj.startAsyncAction().then(\n"
24142 " [this, &obj](Result result) mutable {\n"
24143 " result.processMore();\n"
24144 " });\n"
24145 " });\n"
24146 "}",
24147 Style);
24148 verifyFormat("Result doSomething(Promise promise) {\n"
24149 " return promise.then([this, obj = std::move(s)] {\n"
24150 " return obj.startAsyncAction().then(\n"
24151 " [this, &obj](Result result) mutable {\n"
24152 " result.processMore();\n"
24153 " });\n"
24154 " });\n"
24155 "}",
24156 Style);
24157 verifyFormat("void test() {\n"
24158 " ([]() -> auto {\n"
24159 " int b = 32;\n"
24160 " return 3;\n"
24161 " }).foo();\n"
24162 "}",
24163 Style);
24164 verifyFormat("void test() {\n"
24165 " []() -> auto {\n"
24166 " int b = 32;\n"
24167 " return 3;\n"
24168 " }\n"
24169 "}",
24170 Style);
24171 verifyFormat("void test() {\n"
24172 " std::sort(v.begin(), v.end(),\n"
24173 " [](const auto &foo, const auto &bar) {\n"
24174 " return foo.baz < bar.baz;\n"
24175 " });\n"
24176 "};",
24177 Style);
24178 verifyFormat("void test() {\n"
24179 " (\n"
24180 " []() -> auto {\n"
24181 " int b = 32;\n"
24182 " return 3;\n"
24183 " }, foo, bar)\n"
24184 " .foo();\n"
24185 "}",
24186 Style);
24187 verifyFormat("void test() {\n"
24188 " ([]() -> auto {\n"
24189 " int b = 32;\n"
24190 " return 3;\n"
24191 " })\n"
24192 " .foo()\n"
24193 " .bar();\n"
24194 "}",
24195 Style);
24196 verifyFormat("#define A \\\n"
24197 " [] { \\\n"
24198 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
24199 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
24200 " }",
24201 Style);
24202 verifyFormat("#define SORT(v) \\\n"
24203 " std::sort(v.begin(), v.end(), \\\n"
24204 " [](const auto &foo, const auto &bar) { \\\n"
24205 " return foo.baz < bar.baz; \\\n"
24206 " });",
24207 Style);
24208 verifyFormat("void foo() {\n"
24209 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
24210 " auto f = e(d);\n"
24211 " return f;\n"
24212 " })));\n"
24213 "}",
24214 Style);
24215 verifyFormat("void foo() {\n"
24216 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
24217 " auto f = e(foo, [&] {\n"
24218 " auto g = h();\n"
24219 " return g;\n"
24220 " }, qux, [&] -> Bar {\n"
24221 " auto i = j();\n"
24222 " return i;\n"
24223 " });\n"
24224 " return f;\n"
24225 " })));\n"
24226 "}",
24227 Style);
24228 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
24229 " AnotherLongClassName baz)\n"
24230 " : baz{baz}, func{[&] {\n"
24231 " auto qux = bar;\n"
24232 " return aFunkyFunctionCall(qux);\n"
24233 " }} {}",
24234 Style);
24235 verifyFormat("void foo() {\n"
24236 " class Foo {\n"
24237 " public:\n"
24238 " Foo()\n"
24239 " : qux{[](int quux) {\n"
24240 " auto tmp = quux;\n"
24241 " return tmp;\n"
24242 " }} {}\n"
24243 "\n"
24244 " private:\n"
24245 " std::function<void(int quux)> qux;\n"
24246 " };\n"
24247 "}",
24248 Style);
24249 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
24250 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
24251 " AnotherLongClassName baz) :\n"
24252 " baz{baz}, func{[&] {\n"
24253 " auto qux = bar;\n"
24254 " return aFunkyFunctionCall(qux);\n"
24255 " }} {}",
24256 Style);
24257 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
24258 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
24259 " AnotherLongClassName baz) :\n"
24260 " baz{baz},\n"
24261 " func{[&] {\n"
24262 " auto qux = bar;\n"
24263 " return aFunkyFunctionCall(qux);\n"
24264 " }} {}",
24265 Style);
24266 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
24267 // FIXME: The following test should pass, but fails at the time of writing.
24268#if 0
24269 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
24270 // doesn't force an initial line break, even if lambdas span multiple lines.
24271 verifyFormat("void foo() {\n"
24272 " aFunction(\n"
24273 " [](d) -> Foo {\n"
24274 " auto f = e(d);\n"
24275 " return f;\n"
24276 " }, foo, Bar{}, [] {\n"
24277 " auto g = h();\n"
24278 " return g;\n"
24279 " }, baz);\n"
24280 "}",
24281 Style);
24282#endif
24283 // A long non-lambda argument forces arguments to span multiple lines and thus
24284 // forces an initial line break when using AlwaysBreak.
24285 verifyFormat("void foo() {\n"
24286 " aFunction(\n"
24287 " 1,\n"
24288 " [](d) -> Foo {\n"
24289 " auto f = e(d);\n"
24290 " return f;\n"
24291 " }, foo, Bar{},\n"
24292 " [] {\n"
24293 " auto g = h();\n"
24294 " return g;\n"
24295 " }, bazzzzz,\n"
24296 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
24297 "}",
24298 Style);
24299 Style.BinPackArguments = false;
24300 verifyFormat("void foo() {\n"
24301 " aFunction(\n"
24302 " 1,\n"
24303 " [](d) -> Foo {\n"
24304 " auto f = e(d);\n"
24305 " return f;\n"
24306 " },\n"
24307 " foo,\n"
24308 " Bar{},\n"
24309 " [] {\n"
24310 " auto g = h();\n"
24311 " return g;\n"
24312 " },\n"
24313 " bazzzzz,\n"
24314 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
24315 "}",
24316 Style);
24317 Style.BinPackArguments = true;
24318 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
24319 Style.BraceWrapping.BeforeLambdaBody = true;
24320 verifyFormat("void foo() {\n"
24321 " aFunction(\n"
24322 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
24323 " {\n"
24324 " auto f = e(\n"
24325 " [&]\n"
24326 " {\n"
24327 " auto g = h();\n"
24328 " return g;\n"
24329 " }, qux, [&] -> Bar\n"
24330 " {\n"
24331 " auto i = j();\n"
24332 " return i;\n"
24333 " });\n"
24334 " return f;\n"
24335 " })));\n"
24336 "}",
24337 Style);
24338}
24339
24340TEST_F(FormatTest, LambdaWithLineComments) {
24341 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
24342 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
24343 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
24344 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
24345 FormatStyle::ShortLambdaStyle::SLS_All;
24346
24347 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
24348 verifyFormat("auto k = []() // comment\n"
24349 "{ return; }",
24350 LLVMWithBeforeLambdaBody);
24351 verifyFormat("auto k = []() /* comment */ { return; }",
24352 LLVMWithBeforeLambdaBody);
24353 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
24354 LLVMWithBeforeLambdaBody);
24355 verifyFormat("auto k = []() // X\n"
24356 "{ return; }",
24357 LLVMWithBeforeLambdaBody);
24358 verifyFormat(
24359 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
24360 "{ return; }",
24361 LLVMWithBeforeLambdaBody);
24362
24363 LLVMWithBeforeLambdaBody.ColumnLimit = 0;
24364
24365 verifyFormat("foo([]()\n"
24366 " {\n"
24367 " bar(); //\n"
24368 " return 1; // comment\n"
24369 " }());",
24370 "foo([]() {\n"
24371 " bar(); //\n"
24372 " return 1; // comment\n"
24373 "}());",
24374 LLVMWithBeforeLambdaBody);
24375 verifyFormat("foo(\n"
24376 " 1, MACRO {\n"
24377 " baz();\n"
24378 " bar(); // comment\n"
24379 " },\n"
24380 " []() {});",
24381 "foo(\n"
24382 " 1, MACRO { baz(); bar(); // comment\n"
24383 " }, []() {}\n"
24384 ");",
24385 LLVMWithBeforeLambdaBody);
24386}
24387
24388TEST_F(FormatTest, EmptyLinesInLambdas) {
24389 verifyFormat("auto lambda = []() {\n"
24390 " x(); //\n"
24391 "};",
24392 "auto lambda = []() {\n"
24393 "\n"
24394 " x(); //\n"
24395 "\n"
24396 "};");
24397}
24398
24399TEST_F(FormatTest, LambdaBracesInGNU) {
24400 auto Style = getGNUStyle();
24401 EXPECT_EQ(Style.LambdaBodyIndentation, FormatStyle::LBI_Signature);
24402
24403 constexpr StringRef Code("auto x = [&] ()\n"
24404 " {\n"
24405 " for (int i = 0; i < y; ++i)\n"
24406 " return 97;\n"
24407 " };");
24408 verifyFormat(Code, Style);
24409
24410 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope;
24411 verifyFormat(Code, Style);
24412 verifyFormat("for_each_thread ([] (thread_info *thread)\n"
24413 " {\n"
24414 " /* Lambda body. */\n"
24415 " });",
24416 "for_each_thread([](thread_info *thread) {\n"
24417 " /* Lambda body. */\n"
24418 "});",
24419 Style);
24420 verifyFormat("iterate_over_lwps (scope_ptid, [=] (struct lwp_info *info)\n"
24421 " {\n"
24422 " /* Lambda body. */\n"
24423 " });",
24424 "iterate_over_lwps(scope_ptid, [=](struct lwp_info *info) {\n"
24425 " /* Lambda body. */\n"
24426 "});",
24427 Style);
24428}
24429
24430TEST_F(FormatTest, FormatsBlocks) {
24431 FormatStyle ShortBlocks = getLLVMStyle();
24432 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
24433 verifyFormat("int (^Block)(int, int);", ShortBlocks);
24434 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
24435 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
24436 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
24437 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
24438 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
24439
24440 verifyFormat("foo(^{ bar(); });", ShortBlocks);
24441 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
24442 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
24443
24444 verifyFormat("[operation setCompletionBlock:^{\n"
24445 " [self onOperationDone];\n"
24446 "}];");
24447 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
24448 " [self onOperationDone];\n"
24449 "}]};");
24450 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
24451 " f();\n"
24452 "}];");
24453 verifyFormat("int a = [operation block:^int(int *i) {\n"
24454 " return 1;\n"
24455 "}];");
24456 verifyFormat("[myObject doSomethingWith:arg1\n"
24457 " aaa:^int(int *a) {\n"
24458 " return 1;\n"
24459 " }\n"
24460 " bbb:f(a * bbbbbbbb)];");
24461
24462 verifyFormat("[operation setCompletionBlock:^{\n"
24463 " [self.delegate newDataAvailable];\n"
24464 "}];",
24465 getLLVMStyleWithColumns(60));
24466 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
24467 " NSString *path = [self sessionFilePath];\n"
24468 " if (path) {\n"
24469 " // ...\n"
24470 " }\n"
24471 "});");
24472 verifyFormat("[[SessionService sharedService]\n"
24473 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24474 " if (window) {\n"
24475 " [self windowDidLoad:window];\n"
24476 " } else {\n"
24477 " [self errorLoadingWindow];\n"
24478 " }\n"
24479 " }];");
24480 verifyFormat("void (^largeBlock)(void) = ^{\n"
24481 " // ...\n"
24482 "};",
24483 getLLVMStyleWithColumns(40));
24484 verifyFormat("[[SessionService sharedService]\n"
24485 " loadWindowWithCompletionBlock: //\n"
24486 " ^(SessionWindow *window) {\n"
24487 " if (window) {\n"
24488 " [self windowDidLoad:window];\n"
24489 " } else {\n"
24490 " [self errorLoadingWindow];\n"
24491 " }\n"
24492 " }];",
24493 getLLVMStyleWithColumns(60));
24494 verifyFormat("[myObject doSomethingWith:arg1\n"
24495 " firstBlock:^(Foo *a) {\n"
24496 " // ...\n"
24497 " int i;\n"
24498 " }\n"
24499 " secondBlock:^(Bar *b) {\n"
24500 " // ...\n"
24501 " int i;\n"
24502 " }\n"
24503 " thirdBlock:^Foo(Bar *b) {\n"
24504 " // ...\n"
24505 " int i;\n"
24506 " }];");
24507 verifyFormat("[myObject doSomethingWith:arg1\n"
24508 " firstBlock:-1\n"
24509 " secondBlock:^(Bar *b) {\n"
24510 " // ...\n"
24511 " int i;\n"
24512 " }];");
24513
24514 verifyFormat("f(^{\n"
24515 " @autoreleasepool {\n"
24516 " if (a) {\n"
24517 " g();\n"
24518 " }\n"
24519 " }\n"
24520 "});");
24521 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
24522 "};");
24523 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
24524 "};");
24525
24526 FormatStyle FourIndent = getLLVMStyle();
24527 FourIndent.ObjCBlockIndentWidth = 4;
24528 verifyFormat("[operation setCompletionBlock:^{\n"
24529 " [self onOperationDone];\n"
24530 "}];",
24531 FourIndent);
24532}
24533
24534TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
24535 FormatStyle ZeroColumn = getLLVMStyleWithColumns(ColumnLimit: 0);
24536
24537 verifyFormat("[[SessionService sharedService] "
24538 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24539 " if (window) {\n"
24540 " [self windowDidLoad:window];\n"
24541 " } else {\n"
24542 " [self errorLoadingWindow];\n"
24543 " }\n"
24544 "}];",
24545 ZeroColumn);
24546 verifyFormat("[[SessionService sharedService]\n"
24547 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24548 " if (window) {\n"
24549 " [self windowDidLoad:window];\n"
24550 " } else {\n"
24551 " [self errorLoadingWindow];\n"
24552 " }\n"
24553 " }];",
24554 "[[SessionService sharedService]\n"
24555 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
24556 " if (window) {\n"
24557 " [self windowDidLoad:window];\n"
24558 " } else {\n"
24559 " [self errorLoadingWindow];\n"
24560 " }\n"
24561 "}];",
24562 ZeroColumn);
24563 verifyFormat("[myObject doSomethingWith:arg1\n"
24564 " firstBlock:^(Foo *a) {\n"
24565 " // ...\n"
24566 " int i;\n"
24567 " }\n"
24568 " secondBlock:^(Bar *b) {\n"
24569 " // ...\n"
24570 " int i;\n"
24571 " }\n"
24572 " thirdBlock:^Foo(Bar *b) {\n"
24573 " // ...\n"
24574 " int i;\n"
24575 " }];",
24576 ZeroColumn);
24577 verifyFormat("f(^{\n"
24578 " @autoreleasepool {\n"
24579 " if (a) {\n"
24580 " g();\n"
24581 " }\n"
24582 " }\n"
24583 "});",
24584 ZeroColumn);
24585 verifyFormat("void (^largeBlock)(void) = ^{\n"
24586 " // ...\n"
24587 "};",
24588 ZeroColumn);
24589
24590 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
24591 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
24592 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
24593 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
24594 verifyFormat("void (^largeBlock)(void) = ^{\n"
24595 " int i;\n"
24596 "};",
24597 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn);
24598}
24599
24600TEST_F(FormatTest, SupportsCRLF) {
24601 verifyFormat("int a;\r\n"
24602 "int b;\r\n"
24603 "int c;",
24604 "int a;\r\n"
24605 " int b;\r\n"
24606 " int c;");
24607 verifyFormat("int a;\r\n"
24608 "int b;\r\n"
24609 "int c;\r\n",
24610 "int a;\r\n"
24611 " int b;\n"
24612 " int c;\r\n");
24613 verifyFormat("int a;\n"
24614 "int b;\n"
24615 "int c;",
24616 "int a;\r\n"
24617 " int b;\n"
24618 " int c;");
24619 // FIXME: unstable test case
24620 EXPECT_EQ("\"aaaaaaa \"\r\n"
24621 "\"bbbbbbb\";\r\n",
24622 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
24623 verifyFormat("#define A \\\r\n"
24624 " b; \\\r\n"
24625 " c; \\\r\n"
24626 " d;",
24627 "#define A \\\r\n"
24628 " b; \\\r\n"
24629 " c; d; ",
24630 getGoogleStyle());
24631
24632 verifyNoChange("/*\r\n"
24633 "multi line block comments\r\n"
24634 "should not introduce\r\n"
24635 "an extra carriage return\r\n"
24636 "*/");
24637 verifyFormat("/*\r\n"
24638 "\r\n"
24639 "*/",
24640 "/*\r\n"
24641 " \r\r\r\n"
24642 "*/");
24643
24644 FormatStyle style = getLLVMStyle();
24645
24646 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
24647 verifyFormat("union FooBarBazQux {\n"
24648 " int foo;\n"
24649 " int bar;\n"
24650 " int baz;\n"
24651 "};",
24652 "union FooBarBazQux {\r\n"
24653 " int foo;\n"
24654 " int bar;\r\n"
24655 " int baz;\n"
24656 "};",
24657 style);
24658 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24659 verifyFormat("union FooBarBazQux {\r\n"
24660 " int foo;\r\n"
24661 " int bar;\r\n"
24662 " int baz;\r\n"
24663 "};",
24664 "union FooBarBazQux {\r\n"
24665 " int foo;\n"
24666 " int bar;\r\n"
24667 " int baz;\n"
24668 "};",
24669 style);
24670
24671 style.LineEnding = FormatStyle::LE_LF;
24672 verifyFormat("union FooBarBazQux {\n"
24673 " int foo;\n"
24674 " int bar;\n"
24675 " int baz;\n"
24676 " int qux;\n"
24677 "};",
24678 "union FooBarBazQux {\r\n"
24679 " int foo;\n"
24680 " int bar;\r\n"
24681 " int baz;\n"
24682 " int qux;\r\n"
24683 "};",
24684 style);
24685 style.LineEnding = FormatStyle::LE_CRLF;
24686 verifyFormat("union FooBarBazQux {\r\n"
24687 " int foo;\r\n"
24688 " int bar;\r\n"
24689 " int baz;\r\n"
24690 " int qux;\r\n"
24691 "};",
24692 "union FooBarBazQux {\r\n"
24693 " int foo;\n"
24694 " int bar;\r\n"
24695 " int baz;\n"
24696 " int qux;\n"
24697 "};",
24698 style);
24699
24700 style.LineEnding = FormatStyle::LE_DeriveLF;
24701 verifyFormat("union FooBarBazQux {\r\n"
24702 " int foo;\r\n"
24703 " int bar;\r\n"
24704 " int baz;\r\n"
24705 " int qux;\r\n"
24706 "};",
24707 "union FooBarBazQux {\r\n"
24708 " int foo;\n"
24709 " int bar;\r\n"
24710 " int baz;\n"
24711 " int qux;\r\n"
24712 "};",
24713 style);
24714 style.LineEnding = FormatStyle::LE_DeriveCRLF;
24715 verifyFormat("union FooBarBazQux {\n"
24716 " int foo;\n"
24717 " int bar;\n"
24718 " int baz;\n"
24719 " int qux;\n"
24720 "};",
24721 "union FooBarBazQux {\r\n"
24722 " int foo;\n"
24723 " int bar;\r\n"
24724 " int baz;\n"
24725 " int qux;\n"
24726 "};",
24727 style);
24728}
24729
24730TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
24731 verifyFormat("MY_CLASS(C) {\n"
24732 " int i;\n"
24733 " int j;\n"
24734 "};");
24735}
24736
24737TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
24738 FormatStyle TwoIndent = getLLVMStyleWithColumns(ColumnLimit: 15);
24739 TwoIndent.ContinuationIndentWidth = 2;
24740
24741 verifyFormat("int i =\n"
24742 " longFunction(\n"
24743 " arg);",
24744 "int i = longFunction(arg);", TwoIndent);
24745
24746 FormatStyle SixIndent = getLLVMStyleWithColumns(ColumnLimit: 20);
24747 SixIndent.ContinuationIndentWidth = 6;
24748
24749 verifyFormat("int i =\n"
24750 " longFunction(\n"
24751 " arg);",
24752 "int i = longFunction(arg);", SixIndent);
24753}
24754
24755TEST_F(FormatTest, WrappedClosingParenthesisIndent) {
24756 FormatStyle Style = getLLVMStyle();
24757 verifyFormat("int Foo::getter(\n"
24758 " //\n"
24759 ") const {\n"
24760 " return foo;\n"
24761 "}",
24762 Style);
24763 verifyFormat("void Foo::setter(\n"
24764 " //\n"
24765 ") {\n"
24766 " foo = 1;\n"
24767 "}",
24768 Style);
24769}
24770
24771TEST_F(FormatTest, SpacesInAngles) {
24772 FormatStyle Spaces = getLLVMStyle();
24773 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24774
24775 verifyFormat("vector< ::std::string > x1;", Spaces);
24776 verifyFormat("Foo< int, Bar > x2;", Spaces);
24777 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces);
24778
24779 verifyFormat("static_cast< int >(arg);", Spaces);
24780 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
24781 verifyFormat("f< int, float >();", Spaces);
24782 verifyFormat("template <> g() {}", Spaces);
24783 verifyFormat("template < std::vector< int > > f() {}", Spaces);
24784 verifyFormat("std::function< void(int, int) > fct;", Spaces);
24785 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24786 Spaces);
24787
24788 Spaces.Standard = FormatStyle::LS_Cpp03;
24789 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24790 verifyFormat("A< A< int > >();", Spaces);
24791
24792 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24793 verifyFormat("A<A<int> >();", Spaces);
24794
24795 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24796 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24797 Spaces);
24798 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24799 Spaces);
24800
24801 verifyFormat("A<A<int> >();", Spaces);
24802 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces);
24803 verifyFormat("A< A< int > >();", Spaces);
24804
24805 Spaces.Standard = FormatStyle::LS_Cpp11;
24806 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24807 verifyFormat("A< A< int > >();", Spaces);
24808
24809 Spaces.SpacesInAngles = FormatStyle::SIAS_Never;
24810 verifyFormat("vector<::std::string> x4;", Spaces);
24811 verifyFormat("vector<int> x5;", Spaces);
24812 verifyFormat("Foo<int, Bar> x6;", Spaces);
24813 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24814
24815 verifyFormat("A<A<int>>();", Spaces);
24816
24817 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave;
24818 verifyFormat("vector<::std::string> x4;", Spaces);
24819 verifyFormat("vector< ::std::string > x4;", Spaces);
24820 verifyFormat("vector<int> x5;", Spaces);
24821 verifyFormat("vector< int > x5;", Spaces);
24822 verifyFormat("Foo<int, Bar> x6;", Spaces);
24823 verifyFormat("Foo< int, Bar > x6;", Spaces);
24824 verifyFormat("Foo<::int, ::Bar> x7;", Spaces);
24825 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces);
24826
24827 verifyFormat("A<A<int>>();", Spaces);
24828 verifyFormat("A< A< int > >();", Spaces);
24829 verifyFormat("A<A<int > >();", Spaces);
24830 verifyFormat("A< A< int>>();", Spaces);
24831
24832 Spaces.SpacesInAngles = FormatStyle::SIAS_Always;
24833 verifyFormat("// clang-format off\n"
24834 "foo<<<1, 1>>>();\n"
24835 "// clang-format on",
24836 Spaces);
24837 verifyFormat("// clang-format off\n"
24838 "foo< < <1, 1> > >();\n"
24839 "// clang-format on",
24840 Spaces);
24841}
24842
24843TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
24844 FormatStyle Style = getLLVMStyle();
24845 Style.SpaceAfterTemplateKeyword = false;
24846 verifyFormat("template<int> void foo();", Style);
24847}
24848
24849TEST_F(FormatTest, TripleAngleBrackets) {
24850 verifyFormat("f<<<1, 1>>>();");
24851 verifyFormat("f<<<1, 1, 1, s>>>();");
24852 verifyFormat("f<<<a, b, c, d>>>();");
24853 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24854 verifyFormat("f<param><<<1, 1>>>();");
24855 verifyFormat("f<1><<<1, 1>>>();");
24856 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24857 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24858 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24859 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24860 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24861}
24862
24863TEST_F(FormatTest, MergeLessLessAtEnd) {
24864 verifyFormat("<<");
24865 verifyFormat("< < <", "\\\n<<<");
24866 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24867 "aaallvm::outs() <<");
24868 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24869 "aaaallvm::outs()\n <<");
24870}
24871
24872TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
24873 std::string code = "#if A\n"
24874 "#if B\n"
24875 "a.\n"
24876 "#endif\n"
24877 " a = 1;\n"
24878 "#else\n"
24879 "#endif\n"
24880 "#if C\n"
24881 "#else\n"
24882 "#endif\n";
24883 verifyFormat(code);
24884}
24885
24886TEST_F(FormatTest, HandleConflictMarkers) {
24887 // Git/SVN conflict markers.
24888 verifyFormat("int a;\n"
24889 "void f() {\n"
24890 " callme(some(parameter1,\n"
24891 "<<<<<<< text by the vcs\n"
24892 " parameter2),\n"
24893 "||||||| text by the vcs\n"
24894 " parameter2),\n"
24895 " parameter3,\n"
24896 "======= text by the vcs\n"
24897 " parameter2, parameter3),\n"
24898 ">>>>>>> text by the vcs\n"
24899 " otherparameter);",
24900 "int a;\n"
24901 "void f() {\n"
24902 " callme(some(parameter1,\n"
24903 "<<<<<<< text by the vcs\n"
24904 " parameter2),\n"
24905 "||||||| text by the vcs\n"
24906 " parameter2),\n"
24907 " parameter3,\n"
24908 "======= text by the vcs\n"
24909 " parameter2,\n"
24910 " parameter3),\n"
24911 ">>>>>>> text by the vcs\n"
24912 " otherparameter);");
24913
24914 // Perforce markers.
24915 verifyFormat("void f() {\n"
24916 " function(\n"
24917 ">>>> text by the vcs\n"
24918 " parameter,\n"
24919 "==== text by the vcs\n"
24920 " parameter,\n"
24921 "==== text by the vcs\n"
24922 " parameter,\n"
24923 "<<<< text by the vcs\n"
24924 " parameter);",
24925 "void f() {\n"
24926 " function(\n"
24927 ">>>> text by the vcs\n"
24928 " parameter,\n"
24929 "==== text by the vcs\n"
24930 " parameter,\n"
24931 "==== text by the vcs\n"
24932 " parameter,\n"
24933 "<<<< text by the vcs\n"
24934 " parameter);");
24935
24936 verifyNoChange("<<<<<<<\n"
24937 "|||||||\n"
24938 "=======\n"
24939 ">>>>>>>");
24940
24941 verifyNoChange("<<<<<<<\n"
24942 "|||||||\n"
24943 "int i;\n"
24944 "=======\n"
24945 ">>>>>>>");
24946
24947 // FIXME: Handle parsing of macros around conflict markers correctly:
24948 verifyFormat("#define Macro \\\n"
24949 "<<<<<<<\n"
24950 "Something \\\n"
24951 "|||||||\n"
24952 "Else \\\n"
24953 "=======\n"
24954 "Other \\\n"
24955 ">>>>>>>\n"
24956 " End int i;",
24957 "#define Macro \\\n"
24958 "<<<<<<<\n"
24959 " Something \\\n"
24960 "|||||||\n"
24961 " Else \\\n"
24962 "=======\n"
24963 " Other \\\n"
24964 ">>>>>>>\n"
24965 " End\n"
24966 "int i;");
24967
24968 verifyFormat(R"(====
24969#ifdef A
24970a
24971#else
24972b
24973#endif
24974)");
24975}
24976
24977TEST_F(FormatTest, DisableRegions) {
24978 verifyFormat("int i;\n"
24979 "// clang-format off\n"
24980 " int j;\n"
24981 "// clang-format on\n"
24982 "int k;",
24983 " int i;\n"
24984 " // clang-format off\n"
24985 " int j;\n"
24986 " // clang-format on\n"
24987 " int k;");
24988 verifyFormat("int i;\n"
24989 "/* clang-format off */\n"
24990 " int j;\n"
24991 "/* clang-format on */\n"
24992 "int k;",
24993 " int i;\n"
24994 " /* clang-format off */\n"
24995 " int j;\n"
24996 " /* clang-format on */\n"
24997 " int k;");
24998
24999 // Don't reflow comments within disabled regions.
25000 verifyFormat("// clang-format off\n"
25001 "// long long long long long long line\n"
25002 "/* clang-format on */\n"
25003 "/* long long long\n"
25004 " * long long long\n"
25005 " * line */\n"
25006 "int i;\n"
25007 "/* clang-format off */\n"
25008 "/* long long long long long long line */",
25009 "// clang-format off\n"
25010 "// long long long long long long line\n"
25011 "/* clang-format on */\n"
25012 "/* long long long long long long line */\n"
25013 "int i;\n"
25014 "/* clang-format off */\n"
25015 "/* long long long long long long line */",
25016 getLLVMStyleWithColumns(20));
25017
25018 verifyFormat("int *i;\n"
25019 "// clang-format off:\n"
25020 "int* j;\n"
25021 "// clang-format on: 1\n"
25022 "int *k;",
25023 "int* i;\n"
25024 "// clang-format off:\n"
25025 "int* j;\n"
25026 "// clang-format on: 1\n"
25027 "int* k;");
25028
25029 verifyFormat("int *i;\n"
25030 "// clang-format off:0\n"
25031 "int* j;\n"
25032 "// clang-format only\n"
25033 "int* k;",
25034 "int* i;\n"
25035 "// clang-format off:0\n"
25036 "int* j;\n"
25037 "// clang-format only\n"
25038 "int* k;");
25039
25040 verifyNoChange("// clang-format off\n"
25041 "#if 0\n"
25042 " #if SHOULD_STAY_INDENTED\n"
25043 " #endif\n"
25044 "#endif\n"
25045 "// clang-format on");
25046}
25047
25048TEST_F(FormatTest, OneLineFormatOffRegex) {
25049 auto Style = getLLVMStyle();
25050 Style.OneLineFormatOffRegex = "// format off$";
25051
25052 verifyFormat(" // format off\n"
25053 " int i ;\n"
25054 "int j;",
25055 " // format off\n"
25056 " int i ;\n"
25057 " int j ;",
25058 Style);
25059 verifyFormat("// format off?\n"
25060 "int i;",
25061 " // format off?\n"
25062 " int i ;",
25063 Style);
25064 verifyFormat("f(\"// format off\");", " f(\"// format off\") ;", Style);
25065
25066 verifyFormat("int i;\n"
25067 " // format off\n"
25068 " int j ;\n"
25069 "int k;",
25070 " int i ;\n"
25071 " // format off\n"
25072 " int j ;\n"
25073 " int k ;",
25074 Style);
25075
25076 verifyFormat(" // format off\n"
25077 "\n"
25078 "int i;",
25079 " // format off\n"
25080 " \n"
25081 " int i ;",
25082 Style);
25083
25084 verifyFormat("int i;\n"
25085 " int j ; // format off\n"
25086 "int k;",
25087 " int i ;\n"
25088 " int j ; // format off\n"
25089 " int k ;",
25090 Style);
25091
25092 verifyFormat("// clang-format off\n"
25093 " int i ;\n"
25094 " int j ; // format off\n"
25095 " int k ;\n"
25096 "// clang-format on\n"
25097 "f();",
25098 " // clang-format off\n"
25099 " int i ;\n"
25100 " int j ; // format off\n"
25101 " int k ;\n"
25102 " // clang-format on\n"
25103 " f() ;",
25104 Style);
25105
25106 Style.OneLineFormatOffRegex = "^/\\* format off \\*/";
25107 verifyFormat("int i;\n"
25108 " /* format off */ int j ;\n"
25109 "int k;",
25110 " int i ;\n"
25111 " /* format off */ int j ;\n"
25112 " int k ;",
25113 Style);
25114 verifyFormat("f(\"/* format off */\");", " f(\"/* format off */\") ;", Style);
25115
25116 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
25117 verifyFormat("#define A \\\n"
25118 " do { \\\n"
25119 " /* format off */\\\n"
25120 " f() ; \\\n"
25121 " g(); \\\n"
25122 " } while (0)",
25123 "# define A\\\n"
25124 " do{ \\\n"
25125 " /* format off */\\\n"
25126 " f() ; \\\n"
25127 " g() ;\\\n"
25128 " } while (0 )",
25129 Style);
25130
25131 Style.OneLineFormatOffRegex = "MACRO_TEST";
25132 verifyNoChange(" MACRO_TEST1 ( ) ;\n"
25133 " MACRO_TEST2( );",
25134 Style);
25135
25136 Style.ColumnLimit = 50;
25137 Style.OneLineFormatOffRegex = "^LogErrorPrint$";
25138 verifyFormat(" myproject::LogErrorPrint(logger, \"Don't split me!\");\n"
25139 "myproject::MyLogErrorPrinter(myLogger,\n"
25140 " \"Split me!\");",
25141 " myproject::LogErrorPrint(logger, \"Don't split me!\");\n"
25142 " myproject::MyLogErrorPrinter(myLogger, \"Split me!\");",
25143 Style);
25144
25145 Style.OneLineFormatOffRegex = "//(< clang-format off| NO_TRANSLATION)$";
25146 verifyNoChange(
25147 " int i ; //< clang-format off\n"
25148 " msg = sprintf(\"Long string with placeholders.\"); // NO_TRANSLATION",
25149 Style);
25150}
25151
25152TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
25153 format(Code: "? ) =");
25154 verifyNoCrash(Code: "#define a\\\n /**/}");
25155 verifyNoCrash(Code: " tst %o5 ! are we doing the gray case?\n"
25156 "LY52: ! [internal]");
25157}
25158
25159TEST_F(FormatTest, FormatsTableGenCode) {
25160 FormatStyle Style = getLLVMStyle();
25161 Style.Language = FormatStyle::LK_TableGen;
25162 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
25163}
25164
25165TEST_F(FormatTest, ArrayOfTemplates) {
25166 verifyFormat("auto a = new unique_ptr<int>[10];",
25167 "auto a = new unique_ptr<int > [ 10];");
25168
25169 FormatStyle Spaces = getLLVMStyle();
25170 Spaces.SpacesInSquareBrackets = true;
25171 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
25172 "auto a = new unique_ptr<int > [10];", Spaces);
25173}
25174
25175TEST_F(FormatTest, ArrayAsTemplateType) {
25176 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
25177 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
25178
25179 FormatStyle Spaces = getLLVMStyle();
25180 Spaces.SpacesInSquareBrackets = true;
25181 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
25182 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces);
25183}
25184
25185TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); }
25186
25187TEST_F(FormatTest, FormatSortsUsingDeclarations) {
25188 verifyFormat("using std::cin;\n"
25189 "using std::cout;",
25190 "using std::cout;\n"
25191 "using std::cin;",
25192 getGoogleStyle());
25193}
25194
25195TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
25196 FormatStyle Style = getLLVMStyle();
25197 Style.Standard = FormatStyle::LS_Cpp03;
25198 // cpp03 recognize this string as identifier u8 and literal character 'a'
25199 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style);
25200}
25201
25202TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
25203 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
25204 // all modes, including C++11, C++14 and C++17
25205 verifyFormat("auto c = u8'a';");
25206}
25207
25208TEST_F(FormatTest, DoNotFormatLikelyXml) {
25209 verifyGoogleFormat("<!-- ;> -->");
25210 verifyNoChange(" <!-- >; -->", getGoogleStyle());
25211}
25212
25213TEST_F(FormatTest, StructuredBindings) {
25214 // Structured bindings is a C++17 feature.
25215 // all modes, including C++11, C++14 and C++17
25216 verifyFormat("auto [a, b] = f();");
25217 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
25218 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
25219 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
25220 verifyFormat("auto const volatile [a, b] = f();",
25221 "auto const volatile[a, b] = f();");
25222 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
25223 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
25224 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
25225 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
25226 verifyFormat("auto const volatile &&[a, b] = f();",
25227 "auto const volatile &&[a, b] = f();");
25228 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
25229 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
25230 verifyFormat("const auto volatile &&[a, b] = f();",
25231 "const auto volatile &&[a, b] = f();");
25232 verifyFormat("volatile const auto &&[a, b] = f();",
25233 "volatile const auto &&[a, b] = f();");
25234 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
25235
25236 // Make sure we don't mistake structured bindings for lambdas.
25237 FormatStyle PointerMiddle = getLLVMStyle();
25238 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
25239 verifyGoogleFormat("auto [a1, b]{A * i};");
25240 verifyFormat("auto [a2, b]{A * i};");
25241 verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
25242 verifyGoogleFormat("auto const [a1, b]{A * i};");
25243 verifyFormat("auto const [a2, b]{A * i};");
25244 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
25245 verifyGoogleFormat("auto const& [a1, b]{A * i};");
25246 verifyFormat("auto const &[a2, b]{A * i};");
25247 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
25248 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
25249 verifyFormat("auto const &&[a2, b]{A * i};");
25250 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
25251
25252 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
25253 "for (const auto && [a, b] : some_range) {\n}");
25254 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
25255 "for (const auto & [a, b] : some_range) {\n}");
25256 verifyFormat("for (const auto [a, b] : some_range) {\n}",
25257 "for (const auto[a, b] : some_range) {\n}");
25258 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
25259 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
25260 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
25261 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
25262 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
25263 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
25264 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
25265 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
25266
25267 FormatStyle Spaces = getLLVMStyle();
25268 Spaces.SpacesInSquareBrackets = true;
25269 verifyFormat("auto [ a, b ] = f();", Spaces);
25270 verifyFormat("auto &&[ a, b ] = f();", Spaces);
25271 verifyFormat("auto &[ a, b ] = f();", Spaces);
25272 verifyFormat("auto const &&[ a, b ] = f();", Spaces);
25273 verifyFormat("auto const &[ a, b ] = f();", Spaces);
25274}
25275
25276TEST_F(FormatTest, FileAndCode) {
25277 EXPECT_EQ(FormatStyle::LK_C, guessLanguage("foo.c", ""));
25278 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
25279 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
25280 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
25281 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
25282 EXPECT_EQ(FormatStyle::LK_ObjC,
25283 guessLanguage("foo.h", "@interface Foo\n@end"));
25284 EXPECT_EQ(
25285 FormatStyle::LK_ObjC,
25286 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
25287 EXPECT_EQ(FormatStyle::LK_ObjC,
25288 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
25289 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
25290 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
25291 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end"));
25292 EXPECT_EQ(FormatStyle::LK_ObjC,
25293 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
25294 EXPECT_EQ(FormatStyle::LK_ObjC,
25295 guessLanguage(
25296 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
25297 EXPECT_EQ(
25298 FormatStyle::LK_Cpp,
25299 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
25300 // Only one of the two preprocessor regions has ObjC-like code.
25301 EXPECT_EQ(FormatStyle::LK_ObjC,
25302 guessLanguage("foo.h", "#if A\n"
25303 "#define B() C\n"
25304 "#else\n"
25305 "#define B() [NSString a:@\"\"]\n"
25306 "#endif"));
25307}
25308
25309TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) {
25310 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];"));
25311 EXPECT_EQ(FormatStyle::LK_ObjC,
25312 guessLanguage("foo.h", "array[[calculator getIndex]];"));
25313 EXPECT_EQ(FormatStyle::LK_Cpp,
25314 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
25315 EXPECT_EQ(
25316 FormatStyle::LK_Cpp,
25317 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
25318 EXPECT_EQ(FormatStyle::LK_ObjC,
25319 guessLanguage("foo.h", "[[noreturn foo] bar];"));
25320 EXPECT_EQ(FormatStyle::LK_Cpp,
25321 guessLanguage("foo.h", "[[clang::fallthrough]];"));
25322 EXPECT_EQ(FormatStyle::LK_ObjC,
25323 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
25324 EXPECT_EQ(FormatStyle::LK_Cpp,
25325 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
25326 EXPECT_EQ(FormatStyle::LK_Cpp,
25327 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
25328 EXPECT_EQ(FormatStyle::LK_ObjC,
25329 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
25330 EXPECT_EQ(FormatStyle::LK_Cpp,
25331 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
25332 EXPECT_EQ(
25333 FormatStyle::LK_Cpp,
25334 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
25335 EXPECT_EQ(
25336 FormatStyle::LK_Cpp,
25337 guessLanguage("foo.h",
25338 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
25339 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]"));
25340}
25341
25342TEST_F(FormatTest, GuessLanguageWithCaret) {
25343 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);"));
25344 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);"));
25345 EXPECT_EQ(FormatStyle::LK_ObjC,
25346 guessLanguage("foo.h", "int(^)(char, float);"));
25347 EXPECT_EQ(FormatStyle::LK_ObjC,
25348 guessLanguage("foo.h", "int(^foo)(char, float);"));
25349 EXPECT_EQ(FormatStyle::LK_ObjC,
25350 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
25351 EXPECT_EQ(FormatStyle::LK_ObjC,
25352 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
25353 EXPECT_EQ(
25354 FormatStyle::LK_ObjC,
25355 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
25356}
25357
25358TEST_F(FormatTest, GuessLanguageWithPragmas) {
25359 EXPECT_EQ(FormatStyle::LK_Cpp,
25360 guessLanguage("foo.h", "__pragma(warning(disable:))"));
25361 EXPECT_EQ(FormatStyle::LK_Cpp,
25362 guessLanguage("foo.h", "#pragma(warning(disable:))"));
25363 EXPECT_EQ(FormatStyle::LK_Cpp,
25364 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
25365}
25366
25367TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) {
25368 // ASM symbolic names are identifiers that must be surrounded by [] without
25369 // space in between:
25370 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
25371
25372 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
25373 verifyFormat(R"(//
25374asm volatile("mrs %x[result], FPCR" : [result] "=r"(result));
25375)");
25376
25377 // A list of several ASM symbolic names.
25378 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)");
25379
25380 // ASM symbolic names in inline ASM with inputs and outputs.
25381 verifyFormat(R"(//
25382asm("cmoveq %1, %2, %[result]"
25383 : [result] "=r"(result)
25384 : "r"(test), "r"(new), "[result]"(old));
25385)");
25386
25387 // ASM symbolic names in inline ASM with no outputs.
25388 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)");
25389}
25390
25391TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) {
25392 EXPECT_EQ(FormatStyle::LK_Cpp,
25393 guessLanguage("foo.h", "void f() {\n"
25394 " asm (\"mov %[e], %[d]\"\n"
25395 " : [d] \"=rm\" (d)\n"
25396 " [e] \"rm\" (*e));\n"
25397 "}"));
25398 EXPECT_EQ(FormatStyle::LK_Cpp,
25399 guessLanguage("foo.h", "void f() {\n"
25400 " _asm (\"mov %[e], %[d]\"\n"
25401 " : [d] \"=rm\" (d)\n"
25402 " [e] \"rm\" (*e));\n"
25403 "}"));
25404 EXPECT_EQ(FormatStyle::LK_Cpp,
25405 guessLanguage("foo.h", "void f() {\n"
25406 " __asm (\"mov %[e], %[d]\"\n"
25407 " : [d] \"=rm\" (d)\n"
25408 " [e] \"rm\" (*e));\n"
25409 "}"));
25410 EXPECT_EQ(FormatStyle::LK_Cpp,
25411 guessLanguage("foo.h", "void f() {\n"
25412 " __asm__ (\"mov %[e], %[d]\"\n"
25413 " : [d] \"=rm\" (d)\n"
25414 " [e] \"rm\" (*e));\n"
25415 "}"));
25416 EXPECT_EQ(FormatStyle::LK_Cpp,
25417 guessLanguage("foo.h", "void f() {\n"
25418 " asm (\"mov %[e], %[d]\"\n"
25419 " : [d] \"=rm\" (d),\n"
25420 " [e] \"rm\" (*e));\n"
25421 "}"));
25422 EXPECT_EQ(FormatStyle::LK_Cpp,
25423 guessLanguage("foo.h", "void f() {\n"
25424 " asm volatile (\"mov %[e], %[d]\"\n"
25425 " : [d] \"=rm\" (d)\n"
25426 " [e] \"rm\" (*e));\n"
25427 "}"));
25428}
25429
25430TEST_F(FormatTest, GuessLanguageWithChildLines) {
25431 EXPECT_EQ(FormatStyle::LK_Cpp,
25432 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
25433 EXPECT_EQ(FormatStyle::LK_ObjC,
25434 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
25435 EXPECT_EQ(
25436 FormatStyle::LK_Cpp,
25437 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
25438 EXPECT_EQ(
25439 FormatStyle::LK_ObjC,
25440 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
25441}
25442
25443TEST_F(FormatTest, GetLanguageByComment) {
25444 EXPECT_EQ(FormatStyle::LK_C,
25445 guessLanguage("foo.h", "// clang-format Language: C\n"
25446 "int i;"));
25447 EXPECT_EQ(FormatStyle::LK_Cpp,
25448 guessLanguage("foo.h", "// clang-format Language: Cpp\n"
25449 "int DoStuff(CGRect rect);"));
25450 EXPECT_EQ(FormatStyle::LK_ObjC,
25451 guessLanguage("foo.h", "// clang-format Language: ObjC\n"
25452 "int i;"));
25453}
25454
25455TEST_F(FormatTest, TypenameMacros) {
25456 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"};
25457
25458 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
25459 FormatStyle Google = getGoogleStyleWithColumns(ColumnLimit: 0);
25460 Google.TypenameMacros = TypenameMacros;
25461 verifyFormat("struct foo {\n"
25462 " int bar;\n"
25463 " TAILQ_ENTRY(a) bleh;\n"
25464 "};",
25465 Google);
25466
25467 FormatStyle Macros = getLLVMStyle();
25468 Macros.TypenameMacros = TypenameMacros;
25469
25470 verifyFormat("STACK_OF(int) a;", Macros);
25471 verifyFormat("STACK_OF(int) *a;", Macros);
25472 verifyFormat("STACK_OF(int const *) *a;", Macros);
25473 verifyFormat("STACK_OF(int *const) *a;", Macros);
25474 verifyFormat("STACK_OF(int, string) a;", Macros);
25475 verifyFormat("STACK_OF(LIST(int)) a;", Macros);
25476 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros);
25477 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros);
25478 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros);
25479 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros);
25480 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros);
25481
25482 Macros.PointerAlignment = FormatStyle::PAS_Left;
25483 verifyFormat("STACK_OF(int)* a;", Macros);
25484 verifyFormat("STACK_OF(int*)* a;", Macros);
25485 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
25486 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
25487 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros);
25488}
25489
25490TEST_F(FormatTest, AtomicQualifier) {
25491 // Check that we treate _Atomic as a type and not a function call
25492 FormatStyle Google = getGoogleStyleWithColumns(ColumnLimit: 0);
25493 verifyFormat("struct foo {\n"
25494 " int a1;\n"
25495 " _Atomic(a) a2;\n"
25496 " _Atomic(_Atomic(int)* const) a3;\n"
25497 "};",
25498 Google);
25499 verifyFormat("_Atomic(uint64_t) a;");
25500 verifyFormat("_Atomic(uint64_t) *a;");
25501 verifyFormat("_Atomic(uint64_t const *) *a;");
25502 verifyFormat("_Atomic(uint64_t *const) *a;");
25503 verifyFormat("_Atomic(const uint64_t *) *a;");
25504 verifyFormat("_Atomic(uint64_t) a;");
25505 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
25506 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
25507 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
25508 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
25509
25510 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
25511 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
25512 FormatStyle Style = getLLVMStyle();
25513 Style.PointerAlignment = FormatStyle::PAS_Left;
25514 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style);
25515 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style);
25516 verifyFormat("_Atomic(int)* a;", Style);
25517 verifyFormat("_Atomic(int*)* a;", Style);
25518 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
25519
25520 Style.SpacesInParens = FormatStyle::SIPO_Custom;
25521 Style.SpacesInParensOptions.InCStyleCasts = true;
25522 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
25523 Style.SpacesInParensOptions.InCStyleCasts = false;
25524 Style.SpacesInParensOptions.Other = true;
25525 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
25526 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
25527}
25528
25529TEST_F(FormatTest, C11Generic) {
25530 verifyFormat("_Generic(x, int: 1, default: 0)");
25531 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
25532 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
25533 verifyFormat("_Generic(x, int: f1, const int: f2)();");
25534 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
25535
25536 verifyFormat("_Generic(x,\n"
25537 " float: f,\n"
25538 " default: d,\n"
25539 " long double: ld,\n"
25540 " float _Complex: fc,\n"
25541 " double _Complex: dc,\n"
25542 " long double _Complex: ldc)");
25543
25544 verifyFormat("while (_Generic(x, //\n"
25545 " long: x)(x) > x) {\n"
25546 "}");
25547 verifyFormat("while (_Generic(x, //\n"
25548 " long: x)(x)) {\n"
25549 "}");
25550 verifyFormat("x(_Generic(x, //\n"
25551 " long: x)(x));");
25552
25553 FormatStyle Style = getLLVMStyle();
25554 Style.ColumnLimit = 40;
25555 verifyFormat("#define LIMIT_MAX(T) \\\n"
25556 " _Generic(((T)0), \\\n"
25557 " unsigned int: UINT_MAX, \\\n"
25558 " unsigned long: ULONG_MAX, \\\n"
25559 " unsigned long long: ULLONG_MAX)",
25560 Style);
25561 verifyFormat("_Generic(x,\n"
25562 " struct A: 1,\n"
25563 " void (*)(void): 2);",
25564 Style);
25565
25566 Style.ContinuationIndentWidth = 2;
25567 verifyFormat("_Generic(x,\n"
25568 " struct A: 1,\n"
25569 " void (*)(void): 2);",
25570 Style);
25571}
25572
25573TEST_F(FormatTest, AmbersandInLamda) {
25574 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
25575 FormatStyle AlignStyle = getLLVMStyle();
25576 AlignStyle.PointerAlignment = FormatStyle::PAS_Left;
25577 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
25578 AlignStyle.PointerAlignment = FormatStyle::PAS_Right;
25579 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
25580}
25581
25582TEST_F(FormatTest, TrailingReturnTypeAuto) {
25583 FormatStyle Style = getLLVMStyle();
25584 verifyFormat("[]() -> auto { return Val; }", Style);
25585 verifyFormat("[]() -> auto * { return Val; }", Style);
25586 verifyFormat("[]() -> auto & { return Val; }", Style);
25587 verifyFormat("auto foo() -> auto { return Val; }", Style);
25588 verifyFormat("auto foo() -> auto * { return Val; }", Style);
25589 verifyFormat("auto foo() -> auto & { return Val; }", Style);
25590}
25591
25592TEST_F(FormatTest, SpacesInConditionalStatement) {
25593 FormatStyle Spaces = getLLVMStyle();
25594 Spaces.IfMacros.clear();
25595 Spaces.IfMacros.push_back(x: "MYIF");
25596 Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
25597 Spaces.SpacesInParensOptions.InConditionalStatements = true;
25598 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
25599 verifyFormat("if ( !a )\n return;", Spaces);
25600 verifyFormat("if ( a )\n return;", Spaces);
25601 verifyFormat("if constexpr ( a )\n return;", Spaces);
25602 verifyFormat("MYIF ( a )\n return;", Spaces);
25603 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces);
25604 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces);
25605 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces);
25606 verifyFormat("while ( a )\n return;", Spaces);
25607 verifyFormat("while ( (a && b) )\n return;", Spaces);
25608 verifyFormat("do {\n} while ( 1 != 0 );", Spaces);
25609 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces);
25610 // Check that space on the left of "::" is inserted as expected at beginning
25611 // of condition.
25612 verifyFormat("while ( ::func() )\n return;", Spaces);
25613
25614 // Check impact of ControlStatementsExceptControlMacros is honored.
25615 Spaces.SpaceBeforeParens =
25616 FormatStyle::SBPO_ControlStatementsExceptControlMacros;
25617 verifyFormat("MYIF( a )\n return;", Spaces);
25618 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces);
25619 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces);
25620}
25621
25622TEST_F(FormatTest, AlternativeOperators) {
25623 // Test case for ensuring alternate operators are not
25624 // combined with their right most neighbour.
25625 verifyFormat("int a and b;");
25626 verifyFormat("int a and_eq b;");
25627 verifyFormat("int a bitand b;");
25628 verifyFormat("int a bitor b;");
25629 verifyFormat("int a compl b;");
25630 verifyFormat("int a not b;");
25631 verifyFormat("int a not_eq b;");
25632 verifyFormat("int a or b;");
25633 verifyFormat("int a xor b;");
25634 verifyFormat("int a xor_eq b;");
25635 verifyFormat("return this not_eq bitand other;");
25636 verifyFormat("bool operator not_eq(const X bitand other)");
25637
25638 verifyFormat("int a and 5;");
25639 verifyFormat("int a and_eq 5;");
25640 verifyFormat("int a bitand 5;");
25641 verifyFormat("int a bitor 5;");
25642 verifyFormat("int a compl 5;");
25643 verifyFormat("int a not 5;");
25644 verifyFormat("int a not_eq 5;");
25645 verifyFormat("int a or 5;");
25646 verifyFormat("int a xor 5;");
25647 verifyFormat("int a xor_eq 5;");
25648
25649 verifyFormat("int a compl(5);");
25650 verifyFormat("int a not(5);");
25651
25652 verifyFormat("compl foo();"); // ~foo();
25653 verifyFormat("foo() <%%>"); // foo() {}
25654 verifyFormat("void foo() <%%>"); // void foo() {}
25655 verifyFormat("int a<:1:>;"); // int a[1];
25656 verifyFormat("%:define ABC abc"); // #define ABC abc
25657 verifyFormat("%:%:"); // ##
25658
25659 verifyFormat("return not ::f();");
25660 verifyFormat("return not *foo;");
25661
25662 verifyFormat("a = v(not;);\n"
25663 "c = v(not x);\n"
25664 "d = v(not 1);\n"
25665 "e = v(not 123.f);");
25666
25667 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
25668 " V(and) \\\n"
25669 " V(not) \\\n"
25670 " V(other)",
25671 getLLVMStyleWithColumns(40));
25672}
25673
25674TEST_F(FormatTest, STLWhileNotDefineChed) {
25675 verifyFormat("#if defined(while)\n"
25676 "#define while EMIT WARNING C4005\n"
25677 "#endif // while");
25678}
25679
25680TEST_F(FormatTest, OperatorSpacing) {
25681 FormatStyle Style = getLLVMStyle();
25682 Style.PointerAlignment = FormatStyle::PAS_Right;
25683 verifyFormat("Foo::operator*();", Style);
25684 verifyFormat("Foo::operator void *();", Style);
25685 verifyFormat("Foo::operator void **();", Style);
25686 verifyFormat("Foo::operator void *&();", Style);
25687 verifyFormat("Foo::operator void *&&();", Style);
25688 verifyFormat("Foo::operator void const *();", Style);
25689 verifyFormat("Foo::operator void const **();", Style);
25690 verifyFormat("Foo::operator void const *&();", Style);
25691 verifyFormat("Foo::operator void const *&&();", Style);
25692 verifyFormat("Foo::operator()(void *);", Style);
25693 verifyFormat("Foo::operator*(void *);", Style);
25694 verifyFormat("Foo::operator*();", Style);
25695 verifyFormat("Foo::operator**();", Style);
25696 verifyFormat("Foo::operator&();", Style);
25697 verifyFormat("Foo::operator<int> *();", Style);
25698 verifyFormat("Foo::operator<Foo> *();", Style);
25699 verifyFormat("Foo::operator<int> **();", Style);
25700 verifyFormat("Foo::operator<Foo> **();", Style);
25701 verifyFormat("Foo::operator<int> &();", Style);
25702 verifyFormat("Foo::operator<Foo> &();", Style);
25703 verifyFormat("Foo::operator<int> &&();", Style);
25704 verifyFormat("Foo::operator<Foo> &&();", Style);
25705 verifyFormat("Foo::operator<int> *&();", Style);
25706 verifyFormat("Foo::operator<Foo> *&();", Style);
25707 verifyFormat("Foo::operator<int> *&&();", Style);
25708 verifyFormat("Foo::operator<Foo> *&&();", Style);
25709 verifyFormat("operator*(int (*)(), class Foo);", Style);
25710
25711 verifyFormat("Foo::operator&();", Style);
25712 verifyFormat("Foo::operator void &();", Style);
25713 verifyFormat("Foo::operator void const &();", Style);
25714 verifyFormat("Foo::operator()(void &);", Style);
25715 verifyFormat("Foo::operator&(void &);", Style);
25716 verifyFormat("Foo::operator&();", Style);
25717 verifyFormat("operator&(int (&)(), class Foo);", Style);
25718 verifyFormat("operator&&(int (&)(), class Foo);", Style);
25719
25720 verifyFormat("Foo::operator&&();", Style);
25721 verifyFormat("Foo::operator**();", Style);
25722 verifyFormat("Foo::operator void &&();", Style);
25723 verifyFormat("Foo::operator void const &&();", Style);
25724 verifyFormat("Foo::operator()(void &&);", Style);
25725 verifyFormat("Foo::operator&&(void &&);", Style);
25726 verifyFormat("Foo::operator&&();", Style);
25727 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25728 verifyFormat("operator const nsTArrayRight<E> &()", Style);
25729 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
25730 Style);
25731 verifyFormat("operator void **()", Style);
25732 verifyFormat("operator const FooRight<Object> &()", Style);
25733 verifyFormat("operator const FooRight<Object> *()", Style);
25734 verifyFormat("operator const FooRight<Object> **()", Style);
25735 verifyFormat("operator const FooRight<Object> *&()", Style);
25736 verifyFormat("operator const FooRight<Object> *&&()", Style);
25737
25738 Style.PointerAlignment = FormatStyle::PAS_Left;
25739 verifyFormat("Foo::operator*();", Style);
25740 verifyFormat("Foo::operator**();", Style);
25741 verifyFormat("Foo::operator void*();", Style);
25742 verifyFormat("Foo::operator void**();", Style);
25743 verifyFormat("Foo::operator void*&();", Style);
25744 verifyFormat("Foo::operator void*&&();", Style);
25745 verifyFormat("Foo::operator void const*();", Style);
25746 verifyFormat("Foo::operator void const**();", Style);
25747 verifyFormat("Foo::operator void const*&();", Style);
25748 verifyFormat("Foo::operator void const*&&();", Style);
25749 verifyFormat("Foo::operator/*comment*/ void*();", Style);
25750 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style);
25751 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style);
25752 verifyFormat("Foo::operator()(void*);", Style);
25753 verifyFormat("Foo::operator*(void*);", Style);
25754 verifyFormat("Foo::operator*();", Style);
25755 verifyFormat("Foo::operator<int>*();", Style);
25756 verifyFormat("Foo::operator<Foo>*();", Style);
25757 verifyFormat("Foo::operator<int>**();", Style);
25758 verifyFormat("Foo::operator<Foo>**();", Style);
25759 verifyFormat("Foo::operator<Foo>*&();", Style);
25760 verifyFormat("Foo::operator<int>&();", Style);
25761 verifyFormat("Foo::operator<Foo>&();", Style);
25762 verifyFormat("Foo::operator<int>&&();", Style);
25763 verifyFormat("Foo::operator<Foo>&&();", Style);
25764 verifyFormat("Foo::operator<int>*&();", Style);
25765 verifyFormat("Foo::operator<Foo>*&();", Style);
25766 verifyFormat("operator*(int (*)(), class Foo);", Style);
25767
25768 verifyFormat("Foo::operator&();", Style);
25769 verifyFormat("Foo::operator void&();", Style);
25770 verifyFormat("Foo::operator void const&();", Style);
25771 verifyFormat("Foo::operator/*comment*/ void&();", Style);
25772 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style);
25773 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style);
25774 verifyFormat("Foo::operator()(void&);", Style);
25775 verifyFormat("Foo::operator&(void&);", Style);
25776 verifyFormat("Foo::operator&();", Style);
25777 verifyFormat("operator&(int (&)(), class Foo);", Style);
25778 verifyFormat("operator&(int (&&)(), class Foo);", Style);
25779 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25780
25781 verifyFormat("Foo::operator&&();", Style);
25782 verifyFormat("Foo::operator void&&();", Style);
25783 verifyFormat("Foo::operator void const&&();", Style);
25784 verifyFormat("Foo::operator/*comment*/ void&&();", Style);
25785 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style);
25786 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style);
25787 verifyFormat("Foo::operator()(void&&);", Style);
25788 verifyFormat("Foo::operator&&(void&&);", Style);
25789 verifyFormat("Foo::operator&&();", Style);
25790 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25791 verifyFormat("operator const nsTArrayLeft<E>&()", Style);
25792 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25793 Style);
25794 verifyFormat("operator void**()", Style);
25795 verifyFormat("operator const FooLeft<Object>&()", Style);
25796 verifyFormat("operator const FooLeft<Object>*()", Style);
25797 verifyFormat("operator const FooLeft<Object>**()", Style);
25798 verifyFormat("operator const FooLeft<Object>*&()", Style);
25799 verifyFormat("operator const FooLeft<Object>*&&()", Style);
25800
25801 // PR45107
25802 verifyFormat("operator Vector<String>&();", Style);
25803 verifyFormat("operator const Vector<String>&();", Style);
25804 verifyFormat("operator foo::Bar*();", Style);
25805 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style);
25806 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25807 Style);
25808
25809 Style.PointerAlignment = FormatStyle::PAS_Middle;
25810 verifyFormat("Foo::operator*();", Style);
25811 verifyFormat("Foo::operator void *();", Style);
25812 verifyFormat("Foo::operator()(void *);", Style);
25813 verifyFormat("Foo::operator*(void *);", Style);
25814 verifyFormat("Foo::operator*();", Style);
25815 verifyFormat("operator*(int (*)(), class Foo);", Style);
25816
25817 verifyFormat("Foo::operator&();", Style);
25818 verifyFormat("Foo::operator void &();", Style);
25819 verifyFormat("Foo::operator void const &();", Style);
25820 verifyFormat("Foo::operator()(void &);", Style);
25821 verifyFormat("Foo::operator&(void &);", Style);
25822 verifyFormat("Foo::operator&();", Style);
25823 verifyFormat("operator&(int (&)(), class Foo);", Style);
25824
25825 verifyFormat("Foo::operator&&();", Style);
25826 verifyFormat("Foo::operator void &&();", Style);
25827 verifyFormat("Foo::operator void const &&();", Style);
25828 verifyFormat("Foo::operator()(void &&);", Style);
25829 verifyFormat("Foo::operator&&(void &&);", Style);
25830 verifyFormat("Foo::operator&&();", Style);
25831 verifyFormat("operator&&(int (&&)(), class Foo);", Style);
25832}
25833
25834TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
25835 FormatStyle Style = getLLVMStyle();
25836 // PR46157
25837 verifyFormat("foo(operator+, -42);", Style);
25838 verifyFormat("foo(operator++, -42);", Style);
25839 verifyFormat("foo(operator--, -42);", Style);
25840 verifyFormat("foo(-42, operator--);", Style);
25841 verifyFormat("foo(-42, operator, );", Style);
25842 verifyFormat("foo(operator, , -42);", Style);
25843}
25844
25845TEST_F(FormatTest, LineSpliceWithTrailingWhitespace) {
25846 auto Style = getLLVMStyle();
25847 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
25848 Style.UseTab = FormatStyle::UT_Never;
25849
25850 verifyFormat("int i;", " \\ \n"
25851 " int i;");
25852 verifyFormat("#define FOO(args) \\\n"
25853 " struct a {};",
25854 "#define FOO( args ) \\ \n"
25855 "struct a{\\\t\t\t\n"
25856 " };",
25857 Style);
25858}
25859
25860TEST_F(FormatTest, WhitespaceSensitiveMacros) {
25861 FormatStyle Style = getLLVMStyle();
25862 Style.WhitespaceSensitiveMacros.push_back(x: "FOO");
25863
25864 // Newlines are important here.
25865 verifyNoChange("FOO(1+2 )\n", Style);
25866 verifyNoChange("FOO(a:b:c)\n", Style);
25867
25868 // Don't use the helpers here, since 'mess up' will change the whitespace
25869 // and these are all whitespace sensitive by definition
25870 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
25871 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style);
25872 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
25873 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25874 " Still=Intentional);",
25875 Style);
25876 Style.AlignConsecutiveAssignments.Enabled = true;
25877 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25878 " Still=Intentional);",
25879 Style);
25880
25881 Style.ColumnLimit = 21;
25882 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
25883}
25884
25885TEST_F(FormatTest, SkipMacroDefinitionBody) {
25886 auto Style = getLLVMStyle();
25887 Style.SkipMacroDefinitionBody = true;
25888
25889 verifyFormat("#define A", "#define A", Style);
25890 verifyFormat("#define A a aa", "#define A a aa", Style);
25891 verifyNoChange("#define A b", Style);
25892 verifyNoChange("#define A ( args )", Style);
25893 verifyNoChange("#define A ( args ) = func ( args )", Style);
25894 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style);
25895 verifyNoChange("#define A ( args ) \\\n"
25896 " {\\\n"
25897 " int a = 1 ;\\\n"
25898 "}",
25899 Style);
25900
25901 verifyNoChange("#define A x:", Style);
25902 verifyNoChange("#define A a. b", Style);
25903
25904 // Surrounded with formatted code.
25905 verifyFormat("int a;\n"
25906 "#define A a\n"
25907 "int a;",
25908 "int a ;\n"
25909 "#define A a\n"
25910 "int a ;",
25911 Style);
25912
25913 // Columns are not broken when a limit is set.
25914 Style.ColumnLimit = 10;
25915 verifyFormat("#define A a a a a", " # define A a a a a ", Style);
25916 verifyNoChange("#define A a a a a", Style);
25917
25918 Style.ColumnLimit = 15;
25919 verifyFormat("#define A // a\n"
25920 " // very\n"
25921 " // long\n"
25922 " // comment",
25923 "#define A //a very long comment", Style);
25924 Style.ColumnLimit = 0;
25925
25926 // Multiline definition.
25927 verifyNoChange("#define A \\\n"
25928 "Line one with spaces . \\\n"
25929 " Line two.",
25930 Style);
25931 verifyNoChange("#define A \\\n"
25932 "a a \\\n"
25933 "a \\\n"
25934 "a",
25935 Style);
25936 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
25937 verifyNoChange("#define A \\\n"
25938 "a a \\\n"
25939 "a \\\n"
25940 "a",
25941 Style);
25942 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right;
25943 verifyNoChange("#define A \\\n"
25944 "a a \\\n"
25945 "a \\\n"
25946 "a",
25947 Style);
25948
25949 // Adjust indendations but don't change the definition.
25950 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25951 verifyNoChange("#if A\n"
25952 "#define A a\n"
25953 "#endif",
25954 Style);
25955 verifyFormat("#if A\n"
25956 "#define A a\n"
25957 "#endif",
25958 "#if A\n"
25959 " #define A a\n"
25960 "#endif",
25961 Style);
25962 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
25963 verifyNoChange("#if A\n"
25964 "# define A a\n"
25965 "#endif",
25966 Style);
25967 verifyFormat("#if A\n"
25968 "# define A a\n"
25969 "#endif",
25970 "#if A\n"
25971 " #define A a\n"
25972 "#endif",
25973 Style);
25974 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash;
25975 verifyNoChange("#if A\n"
25976 " #define A a\n"
25977 "#endif",
25978 Style);
25979 verifyFormat("#if A\n"
25980 " #define A a\n"
25981 "#endif",
25982 "#if A\n"
25983 " # define A a\n"
25984 "#endif",
25985 Style);
25986
25987 Style.IndentPPDirectives = FormatStyle::PPDIS_None;
25988 // SkipMacroDefinitionBody should not affect other PP directives
25989 verifyFormat("#if !defined(A)\n"
25990 "#define A a\n"
25991 "#endif",
25992 "#if ! defined ( A )\n"
25993 " #define A a\n"
25994 "#endif",
25995 Style);
25996
25997 // With comments.
25998 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25999 Style);
26000 verifyNoChange("/* */ #define A a // a a", Style);
26001
26002 verifyFormat("int a; // a\n"
26003 "#define A // a\n"
26004 "int aaa; // a",
26005 "int a; // a\n"
26006 "#define A // a\n"
26007 "int aaa; // a",
26008 Style);
26009
26010 verifyNoChange(
26011 "#define MACRO_WITH_COMMENTS() \\\n"
26012 " public: \\\n"
26013 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
26014 " static MyType getClassTypeId(); \\\n"
26015 " /** Normal comment for the following method. */ \\\n"
26016 " virtual MyType getTypeId() const;",
26017 Style);
26018
26019 // multiline macro definitions
26020 verifyNoChange("#define A a\\\n"
26021 " A a \\\n "
26022 " A a",
26023 Style);
26024}
26025
26026TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
26027 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
26028 // test its interaction with line wrapping
26029 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 80);
26030 verifyFormat("namespace {\n"
26031 "int i;\n"
26032 "int j;\n"
26033 "} // namespace",
26034 Style);
26035
26036 verifyFormat("namespace AAA {\n"
26037 "int i;\n"
26038 "int j;\n"
26039 "} // namespace AAA",
26040 Style);
26041
26042 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
26043 "int i;\n"
26044 "int j;\n"
26045 "} // namespace Averyveryveryverylongnamespace",
26046 "namespace Averyveryveryverylongnamespace {\n"
26047 "int i;\n"
26048 "int j;\n"
26049 "}",
26050 Style);
26051
26052 verifyFormat(
26053 "namespace "
26054 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
26055 " went::mad::now {\n"
26056 "int i;\n"
26057 "int j;\n"
26058 "} // namespace\n"
26059 " // "
26060 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
26061 "went::mad::now",
26062 "namespace "
26063 "would::it::save::you::a::lot::of::time::if_::i::"
26064 "just::gave::up::and_::went::mad::now {\n"
26065 "int i;\n"
26066 "int j;\n"
26067 "}",
26068 Style);
26069
26070 // This used to duplicate the comment again and again on subsequent runs
26071 verifyFormat(
26072 "namespace "
26073 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
26074 " went::mad::now {\n"
26075 "int i;\n"
26076 "int j;\n"
26077 "} // namespace\n"
26078 " // "
26079 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
26080 "went::mad::now",
26081 "namespace "
26082 "would::it::save::you::a::lot::of::time::if_::i::"
26083 "just::gave::up::and_::went::mad::now {\n"
26084 "int i;\n"
26085 "int j;\n"
26086 "} // namespace\n"
26087 " // "
26088 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
26089 "and_::went::mad::now",
26090 Style);
26091}
26092
26093TEST_F(FormatTest, LikelyUnlikely) {
26094 FormatStyle Style = getLLVMStyle();
26095
26096 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26097 " return 29;\n"
26098 "}",
26099 Style);
26100
26101 verifyFormat("if (argc > 5) [[likely]] {\n"
26102 " return 29;\n"
26103 "}",
26104 Style);
26105
26106 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26107 " return 29;\n"
26108 "} else [[likely]] {\n"
26109 " return 42;\n"
26110 "}",
26111 Style);
26112
26113 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26114 " return 29;\n"
26115 "} else if (argc > 10) [[likely]] {\n"
26116 " return 99;\n"
26117 "} else {\n"
26118 " return 42;\n"
26119 "}",
26120 Style);
26121
26122 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
26123 " return 29;\n"
26124 "}",
26125 Style);
26126
26127 verifyFormat("if (argc > 5) [[unlikely]]\n"
26128 " return 29;",
26129 Style);
26130 verifyFormat("if (argc > 5) [[likely]]\n"
26131 " return 29;",
26132 Style);
26133
26134 verifyFormat("while (limit > 0) [[unlikely]] {\n"
26135 " --limit;\n"
26136 "}",
26137 Style);
26138 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
26139 " --limit;\n"
26140 "}",
26141 Style);
26142
26143 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
26144 " --limit;",
26145 Style);
26146 verifyFormat("while (limit > 0) [[likely]]\n"
26147 " --limit;",
26148 Style);
26149
26150 Style.AttributeMacros.push_back(x: "UNLIKELY");
26151 Style.AttributeMacros.push_back(x: "LIKELY");
26152 verifyFormat("if (argc > 5) UNLIKELY\n"
26153 " return 29;",
26154 Style);
26155
26156 verifyFormat("if (argc > 5) UNLIKELY {\n"
26157 " return 29;\n"
26158 "}",
26159 Style);
26160 verifyFormat("if (argc > 5) UNLIKELY {\n"
26161 " return 29;\n"
26162 "} else [[likely]] {\n"
26163 " return 42;\n"
26164 "}",
26165 Style);
26166 verifyFormat("if (argc > 5) UNLIKELY {\n"
26167 " return 29;\n"
26168 "} else LIKELY {\n"
26169 " return 42;\n"
26170 "}",
26171 Style);
26172 verifyFormat("if (argc > 5) [[unlikely]] {\n"
26173 " return 29;\n"
26174 "} else LIKELY {\n"
26175 " return 42;\n"
26176 "}",
26177 Style);
26178
26179 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
26180 " --limit;\n"
26181 "}",
26182 Style);
26183 verifyFormat("while (limit > 0) LIKELY {\n"
26184 " --limit;\n"
26185 "}",
26186 Style);
26187
26188 verifyFormat("while (limit > 0) UNLIKELY\n"
26189 " --limit;",
26190 Style);
26191 verifyFormat("for (auto &limit : limits) LIKELY\n"
26192 " --limit;",
26193 Style);
26194}
26195
26196TEST_F(FormatTest, PenaltyIndentedWhitespace) {
26197 verifyFormat("Constructor()\n"
26198 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
26199 " aaaa(aaaaaaaaaaaaaaaaaa, "
26200 "aaaaaaaaaaaaaaaaaat))");
26201 verifyFormat("Constructor()\n"
26202 " : aaaaaaaaaaaaa(aaaaaa), "
26203 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
26204
26205 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle();
26206 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5;
26207 verifyFormat("Constructor()\n"
26208 " : aaaaaa(aaaaaa),\n"
26209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
26210 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
26211 StyleWithWhitespacePenalty);
26212 verifyFormat("Constructor()\n"
26213 " : aaaaaaaaaaaaa(aaaaaa), "
26214 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
26215 StyleWithWhitespacePenalty);
26216}
26217
26218TEST_F(FormatTest, LLVMDefaultStyle) {
26219 FormatStyle Style = getLLVMStyle();
26220 verifyFormat("extern \"C\" {\n"
26221 "int foo();\n"
26222 "}",
26223 Style);
26224}
26225TEST_F(FormatTest, GNUDefaultStyle) {
26226 FormatStyle Style = getGNUStyle();
26227 verifyFormat("extern \"C\"\n"
26228 "{\n"
26229 " int foo ();\n"
26230 "}",
26231 Style);
26232}
26233TEST_F(FormatTest, MozillaDefaultStyle) {
26234 FormatStyle Style = getMozillaStyle();
26235 verifyFormat("extern \"C\"\n"
26236 "{\n"
26237 " int foo();\n"
26238 "}",
26239 Style);
26240}
26241TEST_F(FormatTest, GoogleDefaultStyle) {
26242 FormatStyle Style = getGoogleStyle();
26243 verifyFormat("extern \"C\" {\n"
26244 "int foo();\n"
26245 "}",
26246 Style);
26247}
26248TEST_F(FormatTest, ChromiumDefaultStyle) {
26249 FormatStyle Style = getChromiumStyle(Language: FormatStyle::LK_Cpp);
26250 verifyFormat("extern \"C\" {\n"
26251 "int foo();\n"
26252 "}",
26253 Style);
26254}
26255TEST_F(FormatTest, MicrosoftDefaultStyle) {
26256 FormatStyle Style = getMicrosoftStyle(Language: FormatStyle::LK_Cpp);
26257 verifyFormat("extern \"C\"\n"
26258 "{\n"
26259 " int foo();\n"
26260 "}",
26261 Style);
26262}
26263TEST_F(FormatTest, WebKitDefaultStyle) {
26264 FormatStyle Style = getWebKitStyle();
26265 verifyFormat("extern \"C\" {\n"
26266 "int foo();\n"
26267 "}",
26268 Style);
26269}
26270
26271TEST_F(FormatTest, Concepts) {
26272 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations,
26273 FormatStyle::BBCDS_Always);
26274
26275 // The default in LLVM style is REI_OuterScope, but these tests were written
26276 // when the default was REI_Keyword.
26277 FormatStyle Style = getLLVMStyle();
26278 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26279
26280 verifyFormat("template <typename T>\n"
26281 "concept True = true;");
26282
26283 verifyFormat("template <typename T>\n"
26284 "concept C = ((false || foo()) && C2<T>) ||\n"
26285 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
26286 getLLVMStyleWithColumns(60));
26287
26288 verifyFormat("template <typename T>\n"
26289 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
26290 "sizeof(T) <= 8;");
26291
26292 verifyFormat("template <typename T>\n"
26293 "concept DelayedCheck = true && requires(T t) {\n"
26294 " t.bar();\n"
26295 " t.baz();\n"
26296 " } && sizeof(T) <= 8;",
26297 Style);
26298
26299 verifyFormat("template <typename T>\n"
26300 "concept DelayedCheck = true && requires(T t) { // Comment\n"
26301 " t.bar();\n"
26302 " t.baz();\n"
26303 " } && sizeof(T) <= 8;",
26304 Style);
26305
26306 verifyFormat("template <typename T>\n"
26307 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
26308 "sizeof(T) <= 8;");
26309
26310 verifyFormat("template <typename T>\n"
26311 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
26312
26313 verifyFormat("template <typename T>\n"
26314 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
26315
26316 verifyFormat("template <typename T>\n"
26317 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
26318
26319 verifyFormat("template <typename T>\n"
26320 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
26321 "&& sizeof(T) <= 8;");
26322
26323 verifyFormat("template <typename T>\n"
26324 "concept DelayedCheck =\n"
26325 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
26326 "sizeof(T) <= 8;");
26327
26328 verifyFormat("template <typename T>\n"
26329 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
26330 "&& sizeof(T) <= 8;");
26331
26332 verifyFormat(
26333 "template <typename T>\n"
26334 "concept DelayedCheck =\n"
26335 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
26336
26337 verifyFormat("template <typename T>\n"
26338 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
26339 "&& sizeof(T) <= 8;");
26340
26341 verifyFormat("template <typename T>\n"
26342 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
26343 "sizeof(T) <= 8;");
26344
26345 verifyFormat("template <typename T>\n"
26346 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
26347 " requires(T t) {\n"
26348 " t.bar();\n"
26349 " t.baz();\n"
26350 " } && sizeof(T) <= 8 && !(4 < 3);",
26351 getLLVMStyleWithColumns(60));
26352
26353 verifyFormat("template <typename T>\n"
26354 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
26355
26356 verifyFormat("template <typename T>\n"
26357 "concept C = foo();");
26358
26359 verifyFormat("template <typename T>\n"
26360 "concept C = foo(T());");
26361
26362 verifyFormat("template <typename T>\n"
26363 "concept C = foo(T{});");
26364
26365 verifyFormat("template <typename T>\n"
26366 "concept Size = V<sizeof(T)>::Value > 5;");
26367
26368 verifyFormat("template <typename T>\n"
26369 "concept True = S<T>::Value;");
26370
26371 verifyFormat("template <S T>\n"
26372 "concept True = T.field;");
26373
26374 verifyFormat(
26375 "template <typename T>\n"
26376 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
26377 " sizeof(T) <= 8;");
26378
26379 // FIXME: This is misformatted because the fake l paren starts at bool, not at
26380 // the lambda l square.
26381 verifyFormat("template <typename T>\n"
26382 "concept C = [] -> bool { return true; }() && requires(T t) { "
26383 "t.bar(); } &&\n"
26384 " sizeof(T) <= 8;");
26385
26386 verifyFormat(
26387 "template <typename T>\n"
26388 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
26389 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
26390
26391 verifyFormat("template <typename T>\n"
26392 "concept C = decltype([]() { return std::true_type{}; "
26393 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
26394 getLLVMStyleWithColumns(120));
26395
26396 verifyFormat("template <typename T>\n"
26397 "concept C = decltype([]() -> std::true_type { return {}; "
26398 "}())::value &&\n"
26399 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
26400
26401 verifyFormat("template <typename T>\n"
26402 "concept C = true;\n"
26403 "Foo Bar;");
26404
26405 verifyFormat("template <typename T>\n"
26406 "concept Hashable = requires(T a) {\n"
26407 " { std::hash<T>{}(a) } -> "
26408 "std::convertible_to<std::size_t>;\n"
26409 " };",
26410 Style);
26411
26412 verifyFormat(
26413 "template <typename T>\n"
26414 "concept EqualityComparable = requires(T a, T b) {\n"
26415 " { a == b } -> std::same_as<bool>;\n"
26416 " };",
26417 Style);
26418
26419 verifyFormat(
26420 "template <typename T>\n"
26421 "concept EqualityComparable = requires(T a, T b) {\n"
26422 " { a == b } -> std::same_as<bool>;\n"
26423 " { a != b } -> std::same_as<bool>;\n"
26424 " };",
26425 Style);
26426
26427 verifyFormat("template <typename T>\n"
26428 "concept WeakEqualityComparable = requires(T a, T b) {\n"
26429 " { a == b };\n"
26430 " { a != b };\n"
26431 " };",
26432 Style);
26433
26434 verifyFormat("template <typename T>\n"
26435 "concept HasSizeT = requires { typename T::size_t; };");
26436
26437 verifyFormat("template <typename T>\n"
26438 "concept Semiregular =\n"
26439 " DefaultConstructible<T> && CopyConstructible<T> && "
26440 "CopyAssignable<T> &&\n"
26441 " requires(T a, std::size_t n) {\n"
26442 " requires Same<T *, decltype(&a)>;\n"
26443 " { a.~T() } noexcept;\n"
26444 " requires Same<T *, decltype(new T)>;\n"
26445 " requires Same<T *, decltype(new T[n])>;\n"
26446 " { delete new T; };\n"
26447 " { delete new T[n]; };\n"
26448 " };",
26449 Style);
26450
26451 verifyFormat("template <typename T>\n"
26452 "concept Semiregular =\n"
26453 " requires(T a, std::size_t n) {\n"
26454 " requires Same<T *, decltype(&a)>;\n"
26455 " { a.~T() } noexcept;\n"
26456 " requires Same<T *, decltype(new T)>;\n"
26457 " requires Same<T *, decltype(new T[n])>;\n"
26458 " { delete new T; };\n"
26459 " { delete new T[n]; };\n"
26460 " { new T } -> std::same_as<T *>;\n"
26461 " } && DefaultConstructible<T> && CopyConstructible<T> && "
26462 "CopyAssignable<T>;",
26463 Style);
26464
26465 verifyFormat(
26466 "template <typename T>\n"
26467 "concept Semiregular =\n"
26468 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
26469 " requires Same<T *, decltype(&a)>;\n"
26470 " { a.~T() } noexcept;\n"
26471 " requires Same<T *, decltype(new T)>;\n"
26472 " requires Same<T *, decltype(new "
26473 "T[n])>;\n"
26474 " { delete new T; };\n"
26475 " { delete new T[n]; };\n"
26476 " } && CopyConstructible<T> && "
26477 "CopyAssignable<T>;",
26478 Style);
26479
26480 verifyFormat("template <typename T>\n"
26481 "concept Two = requires(T t) {\n"
26482 " { t.foo() } -> std::same_as<Bar>;\n"
26483 " } && requires(T &&t) {\n"
26484 " { t.foo() } -> std::same_as<Bar &&>;\n"
26485 " };",
26486 Style);
26487
26488 verifyFormat(
26489 "template <typename T>\n"
26490 "concept C = requires(T x) {\n"
26491 " { *x } -> std::convertible_to<typename T::inner>;\n"
26492 " { x + 1 } noexcept -> std::same_as<int>;\n"
26493 " { x * 1 } -> std::convertible_to<T>;\n"
26494 " };",
26495 Style);
26496
26497 verifyFormat("template <typename T>\n"
26498 "concept C = requires(T x) {\n"
26499 " {\n"
26500 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
26501 " } -> long_long_concept_name<T>;\n"
26502 " {\n"
26503 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
26504 " } noexcept -> long_long_concept_name<T>;\n"
26505 " };",
26506 Style);
26507
26508 verifyFormat(
26509 "template <typename T, typename U = T>\n"
26510 "concept Swappable = requires(T &&t, U &&u) {\n"
26511 " swap(std::forward<T>(t), std::forward<U>(u));\n"
26512 " swap(std::forward<U>(u), std::forward<T>(t));\n"
26513 " };",
26514 Style);
26515
26516 verifyFormat("template <typename T, typename U>\n"
26517 "concept Common = requires(T &&t, U &&u) {\n"
26518 " typename CommonType<T, U>;\n"
26519 " { CommonType<T, U>(std::forward<T>(t)) };\n"
26520 " };",
26521 Style);
26522
26523 verifyFormat("template <typename T, typename U>\n"
26524 "concept Common = requires(T &&t, U &&u) {\n"
26525 " typename CommonType<T, U>;\n"
26526 " { CommonType<T, U>{std::forward<T>(t)} };\n"
26527 " };",
26528 Style);
26529
26530 verifyFormat(
26531 "template <typename T>\n"
26532 "concept C = requires(T t) {\n"
26533 " requires Bar<T> && Foo<T>;\n"
26534 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
26535 " };",
26536 Style);
26537
26538 verifyFormat("template <typename T>\n"
26539 "concept HasFoo = requires(T t) {\n"
26540 " { t.foo() };\n"
26541 " t.foo();\n"
26542 " };\n"
26543 "template <typename T>\n"
26544 "concept HasBar = requires(T t) {\n"
26545 " { t.bar() };\n"
26546 " t.bar();\n"
26547 " };",
26548 Style);
26549
26550 verifyFormat("template <typename T>\n"
26551 "concept Large = sizeof(T) > 10;");
26552
26553 verifyFormat("template <typename T, typename U>\n"
26554 "concept FooableWith = requires(T t, U u) {\n"
26555 " typename T::foo_type;\n"
26556 " { t.foo(u) } -> typename T::foo_type;\n"
26557 " t++;\n"
26558 " };\n"
26559 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
26560 Style);
26561
26562 verifyFormat("template <typename T>\n"
26563 "concept Context = is_specialization_of_v<context, T>;");
26564
26565 verifyFormat("template <typename T>\n"
26566 "concept Node = std::is_object_v<T>;");
26567
26568 verifyFormat("template <class T>\n"
26569 "concept integral = __is_integral(T);");
26570
26571 verifyFormat("template <class T>\n"
26572 "concept is2D = __array_extent(T, 1) == 2;");
26573
26574 verifyFormat("template <class T>\n"
26575 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
26576
26577 verifyFormat("template <class T, class T2>\n"
26578 "concept Same = __is_same_as<T, T2>;");
26579
26580 verifyFormat(
26581 "template <class _InIt, class _OutIt>\n"
26582 "concept _Can_reread_dest =\n"
26583 " std::forward_iterator<_OutIt> &&\n"
26584 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
26585
26586 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed;
26587
26588 verifyFormat(
26589 "template <typename T>\n"
26590 "concept C = requires(T t) {\n"
26591 " requires Bar<T> && Foo<T>;\n"
26592 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
26593 " };",
26594 Style);
26595
26596 verifyFormat("template <typename T>\n"
26597 "concept HasFoo = requires(T t) {\n"
26598 " { t.foo() };\n"
26599 " t.foo();\n"
26600 " };\n"
26601 "template <typename T>\n"
26602 "concept HasBar = requires(T t) {\n"
26603 " { t.bar() };\n"
26604 " t.bar();\n"
26605 " };",
26606 Style);
26607
26608 verifyFormat("template <typename T> concept True = true;", Style);
26609
26610 verifyFormat("template <typename T>\n"
26611 "concept C = decltype([]() -> std::true_type { return {}; "
26612 "}())::value &&\n"
26613 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
26614 Style);
26615
26616 verifyFormat("template <typename T>\n"
26617 "concept Semiregular =\n"
26618 " DefaultConstructible<T> && CopyConstructible<T> && "
26619 "CopyAssignable<T> &&\n"
26620 " requires(T a, std::size_t n) {\n"
26621 " requires Same<T *, decltype(&a)>;\n"
26622 " { a.~T() } noexcept;\n"
26623 " requires Same<T *, decltype(new T)>;\n"
26624 " requires Same<T *, decltype(new T[n])>;\n"
26625 " { delete new T; };\n"
26626 " { delete new T[n]; };\n"
26627 " };",
26628 Style);
26629
26630 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never;
26631
26632 verifyFormat("template <typename T> concept C =\n"
26633 " requires(T t) {\n"
26634 " requires Bar<T> && Foo<T>;\n"
26635 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
26636 " };",
26637 Style);
26638
26639 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
26640 " { t.foo() };\n"
26641 " t.foo();\n"
26642 " };\n"
26643 "template <typename T> concept HasBar = requires(T t) {\n"
26644 " { t.bar() };\n"
26645 " t.bar();\n"
26646 " };",
26647 Style);
26648
26649 verifyFormat("template <typename T> concept True = true;", Style);
26650
26651 verifyFormat(
26652 "template <typename T> concept C =\n"
26653 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
26654 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
26655 Style);
26656
26657 verifyFormat("template <typename T> concept Semiregular =\n"
26658 " DefaultConstructible<T> && CopyConstructible<T> && "
26659 "CopyAssignable<T> &&\n"
26660 " requires(T a, std::size_t n) {\n"
26661 " requires Same<T *, decltype(&a)>;\n"
26662 " { a.~T() } noexcept;\n"
26663 " requires Same<T *, decltype(new T)>;\n"
26664 " requires Same<T *, decltype(new T[n])>;\n"
26665 " { delete new T; };\n"
26666 " { delete new T[n]; };\n"
26667 " };",
26668 Style);
26669
26670 // The following tests are invalid C++, we just want to make sure we don't
26671 // assert.
26672 verifyNoCrash(Code: "template <typename T>\n"
26673 "concept C = requires C2<T>;");
26674
26675 verifyNoCrash(Code: "template <typename T>\n"
26676 "concept C = 5 + 4;");
26677
26678 verifyNoCrash(Code: "template <typename T>\n"
26679 "concept C = class X;");
26680
26681 verifyNoCrash(Code: "template <typename T>\n"
26682 "concept C = [] && true;");
26683
26684 verifyNoCrash(Code: "template <typename T>\n"
26685 "concept C = [] && requires(T t) { typename T::size_type; };");
26686}
26687
26688TEST_F(FormatTest, RequiresClausesPositions) {
26689 auto Style = getLLVMStyle();
26690 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine);
26691 EXPECT_EQ(Style.IndentRequiresClause, true);
26692
26693 // The default in LLVM style is REI_OuterScope, but these tests were written
26694 // when the default was REI_Keyword.
26695 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
26696
26697 verifyFormat("template <typename T>\n"
26698 " requires(Foo<T> && std::trait<T>)\n"
26699 "struct Bar;",
26700 Style);
26701
26702 verifyFormat("template <typename T>\n"
26703 " requires(Foo<T> && std::trait<T>)\n"
26704 "class Bar {\n"
26705 "public:\n"
26706 " Bar(T t);\n"
26707 " bool baz();\n"
26708 "};",
26709 Style);
26710
26711 verifyFormat(
26712 "template <typename T>\n"
26713 " requires requires(T &&t) {\n"
26714 " typename T::I;\n"
26715 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26716 " }\n"
26717 "Bar(T) -> Bar<typename T::I>;",
26718 Style);
26719
26720 verifyFormat("template <typename T>\n"
26721 " requires(Foo<T> && std::trait<T>)\n"
26722 "constexpr T MyGlobal;",
26723 Style);
26724
26725 verifyFormat("template <typename T>\n"
26726 " requires Foo<T> && requires(T t) {\n"
26727 " { t.baz() } -> std::same_as<bool>;\n"
26728 " requires std::same_as<T::Factor, int>;\n"
26729 " }\n"
26730 "inline int bar(T t) {\n"
26731 " return t.baz() ? T::Factor : 5;\n"
26732 "}",
26733 Style);
26734
26735 verifyFormat("template <typename T>\n"
26736 "inline int bar(T t)\n"
26737 " requires Foo<T> && requires(T t) {\n"
26738 " { t.baz() } -> std::same_as<bool>;\n"
26739 " requires std::same_as<T::Factor, int>;\n"
26740 " }\n"
26741 "{\n"
26742 " return t.baz() ? T::Factor : 5;\n"
26743 "}",
26744 Style);
26745
26746 verifyFormat("template <typename T>\n"
26747 " requires F<T>\n"
26748 "int bar(T t) {\n"
26749 " return 5;\n"
26750 "}",
26751 Style);
26752
26753 verifyFormat("template <typename T>\n"
26754 "int bar(T t)\n"
26755 " requires F<T>\n"
26756 "{\n"
26757 " return 5;\n"
26758 "}",
26759 Style);
26760
26761 verifyFormat("template <typename T>\n"
26762 "int S::bar(T t) &&\n"
26763 " requires F<T>\n"
26764 "{\n"
26765 " return 5;\n"
26766 "}",
26767 Style);
26768
26769 verifyFormat("template <typename T>\n"
26770 "int bar(T t)\n"
26771 " requires F<T>;",
26772 Style);
26773
26774 Style.IndentRequiresClause = false;
26775 verifyFormat("template <typename T>\n"
26776 "requires F<T>\n"
26777 "int bar(T t) {\n"
26778 " return 5;\n"
26779 "}",
26780 Style);
26781
26782 verifyFormat("template <typename T>\n"
26783 "int S::bar(T t) &&\n"
26784 "requires F<T>\n"
26785 "{\n"
26786 " return 5;\n"
26787 "}",
26788 Style);
26789
26790 verifyFormat("template <typename T>\n"
26791 "int bar(T t)\n"
26792 "requires F<T>\n"
26793 "{\n"
26794 " return 5;\n"
26795 "}",
26796 Style);
26797
26798 Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace;
26799 Style.IndentRequiresClause = true;
26800
26801 verifyFormat("template <typename T>\n"
26802 " requires(Foo<T> && std::trait<T>)\n"
26803 "struct Bar;",
26804 Style);
26805
26806 verifyFormat("template <typename T>\n"
26807 " requires(Foo<T> && std::trait<T>)\n"
26808 "class Bar {\n"
26809 "public:\n"
26810 " Bar(T t);\n"
26811 " bool baz();\n"
26812 "};",
26813 Style);
26814
26815 verifyFormat(
26816 "template <typename T>\n"
26817 " requires requires(T &&t) {\n"
26818 " typename T::I;\n"
26819 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26820 " }\n"
26821 "Bar(T) -> Bar<typename T::I>;",
26822 Style);
26823
26824 verifyFormat("template <typename T>\n"
26825 " requires(Foo<T> && std::trait<T>)\n"
26826 "constexpr T MyGlobal;",
26827 Style);
26828
26829 verifyFormat("template <typename T>\n"
26830 " requires Foo<T> && requires(T t) {\n"
26831 " { t.baz() } -> std::same_as<bool>;\n"
26832 " requires std::same_as<T::Factor, int>;\n"
26833 " }\n"
26834 "inline int bar(T t) {\n"
26835 " return t.baz() ? T::Factor : 5;\n"
26836 "}",
26837 Style);
26838
26839 verifyFormat("template <typename T>\n"
26840 "inline int bar(T t)\n"
26841 " requires Foo<T> && requires(T t) {\n"
26842 " { t.baz() } -> std::same_as<bool>;\n"
26843 " requires std::same_as<T::Factor, int>;\n"
26844 " } {\n"
26845 " return t.baz() ? T::Factor : 5;\n"
26846 "}",
26847 Style);
26848
26849 verifyFormat("template <typename T>\n"
26850 " requires F<T>\n"
26851 "int bar(T t) {\n"
26852 " return 5;\n"
26853 "}",
26854 Style);
26855
26856 verifyFormat("template <typename T>\n"
26857 "int bar(T t)\n"
26858 " requires F<T> {\n"
26859 " return 5;\n"
26860 "}",
26861 Style);
26862
26863 verifyFormat("template <typename T>\n"
26864 "int S::bar(T t) &&\n"
26865 " requires F<T> {\n"
26866 " return 5;\n"
26867 "}",
26868 Style);
26869
26870 verifyFormat("template <typename T>\n"
26871 "int bar(T t)\n"
26872 " requires F<T>;",
26873 Style);
26874
26875 verifyFormat("template <typename T>\n"
26876 "int bar(T t)\n"
26877 " requires F<T> {}",
26878 Style);
26879
26880 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine;
26881 Style.IndentRequiresClause = false;
26882 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26883 "template <typename T> requires Foo<T> void bar() {}\n"
26884 "template <typename T> void bar() requires Foo<T> {}\n"
26885 "template <typename T> void bar() requires Foo<T>;\n"
26886 "template <typename T> void S::bar() && requires Foo<T> {}\n"
26887 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26888 Style);
26889
26890 auto ColumnStyle = Style;
26891 ColumnStyle.ColumnLimit = 40;
26892 verifyFormat("template <typename AAAAAAA>\n"
26893 "requires Foo<T> struct Bar {};\n"
26894 "template <typename AAAAAAA>\n"
26895 "requires Foo<T> void bar() {}\n"
26896 "template <typename AAAAAAA>\n"
26897 "void bar() requires Foo<T> {}\n"
26898 "template <typename T>\n"
26899 "void S::bar() && requires Foo<T> {}\n"
26900 "template <typename AAAAAAA>\n"
26901 "requires Foo<T> Baz(T) -> Baz<T>;",
26902 ColumnStyle);
26903
26904 verifyFormat("template <typename T>\n"
26905 "requires Foo<AAAAAAA> struct Bar {};\n"
26906 "template <typename T>\n"
26907 "requires Foo<AAAAAAA> void bar() {}\n"
26908 "template <typename T>\n"
26909 "void bar() requires Foo<AAAAAAA> {}\n"
26910 "template <typename T>\n"
26911 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26912 ColumnStyle);
26913
26914 verifyFormat("template <typename AAAAAAA>\n"
26915 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26916 "struct Bar {};\n"
26917 "template <typename AAAAAAA>\n"
26918 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26919 "void bar() {}\n"
26920 "template <typename AAAAAAA>\n"
26921 "void bar()\n"
26922 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26923 "template <typename AAAAAAA>\n"
26924 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26925 "template <typename AAAAAAA>\n"
26926 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26927 "Bar(T) -> Bar<T>;",
26928 ColumnStyle);
26929
26930 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26931 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing;
26932
26933 verifyFormat("template <typename T>\n"
26934 "requires Foo<T> struct Bar {};\n"
26935 "template <typename T>\n"
26936 "requires Foo<T> void bar() {}\n"
26937 "template <typename T>\n"
26938 "void bar()\n"
26939 "requires Foo<T> {}\n"
26940 "template <typename T>\n"
26941 "void bar()\n"
26942 "requires Foo<T>;\n"
26943 "template <typename T>\n"
26944 "void S::bar() &&\n"
26945 "requires Foo<T> {}\n"
26946 "template <typename T>\n"
26947 "requires Foo<T> Bar(T) -> Bar<T>;",
26948 Style);
26949
26950 verifyFormat("template <typename AAAAAAA>\n"
26951 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26952 "struct Bar {};\n"
26953 "template <typename AAAAAAA>\n"
26954 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26955 "void bar() {}\n"
26956 "template <typename AAAAAAA>\n"
26957 "void bar()\n"
26958 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26959 "template <typename AAAAAAA>\n"
26960 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26961 "template <typename AAAAAAA>\n"
26962 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26963 "Bar(T) -> Bar<T>;",
26964 ColumnStyle);
26965
26966 Style.IndentRequiresClause = true;
26967 ColumnStyle.IndentRequiresClause = true;
26968
26969 verifyFormat("template <typename T>\n"
26970 " requires Foo<T> struct Bar {};\n"
26971 "template <typename T>\n"
26972 " requires Foo<T> void bar() {}\n"
26973 "template <typename T>\n"
26974 "void bar()\n"
26975 " requires Foo<T> {}\n"
26976 "template <typename T>\n"
26977 "void S::bar() &&\n"
26978 " requires Foo<T> {}\n"
26979 "template <typename T>\n"
26980 " requires Foo<T> Bar(T) -> Bar<T>;",
26981 Style);
26982
26983 verifyFormat("template <typename AAAAAAA>\n"
26984 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26985 "struct Bar {};\n"
26986 "template <typename AAAAAAA>\n"
26987 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26988 "void bar() {}\n"
26989 "template <typename AAAAAAA>\n"
26990 "void bar()\n"
26991 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26992 "template <typename AAAAAAA>\n"
26993 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26994 "template <typename AAAAAAA>\n"
26995 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26996 "Bar(T) -> Bar<T>;",
26997 ColumnStyle);
26998
26999 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
27000 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding;
27001
27002 verifyFormat("template <typename T> requires Foo<T>\n"
27003 "struct Bar {};\n"
27004 "template <typename T> requires Foo<T>\n"
27005 "void bar() {}\n"
27006 "template <typename T>\n"
27007 "void bar() requires Foo<T>\n"
27008 "{}\n"
27009 "template <typename T> void bar() requires Foo<T>;\n"
27010 "template <typename T>\n"
27011 "void S::bar() && requires Foo<T>\n"
27012 "{}\n"
27013 "template <typename T> requires Foo<T>\n"
27014 "Bar(T) -> Bar<T>;",
27015 Style);
27016
27017 verifyFormat("template <typename AAAAAAA>\n"
27018 "requires Foo<AAAAAAAAAAAAAAAA>\n"
27019 "struct Bar {};\n"
27020 "template <typename AAAAAAA>\n"
27021 "requires Foo<AAAAAAAAAAAAAAAA>\n"
27022 "void bar() {}\n"
27023 "template <typename AAAAAAA>\n"
27024 "void bar()\n"
27025 " requires Foo<AAAAAAAAAAAAAAAA>\n"
27026 "{}\n"
27027 "template <typename AAAAAAA>\n"
27028 "requires Foo<AAAAAAAA>\n"
27029 "Bar(T) -> Bar<T>;\n"
27030 "template <typename AAAAAAA>\n"
27031 "requires Foo<AAAAAAAAAAAAAAAA>\n"
27032 "Bar(T) -> Bar<T>;",
27033 ColumnStyle);
27034}
27035
27036TEST_F(FormatTest, RequiresClauses) {
27037 verifyFormat("struct [[nodiscard]] zero_t {\n"
27038 " template <class T>\n"
27039 " requires requires { number_zero_v<T>; }\n"
27040 " [[nodiscard]] constexpr operator T() const {\n"
27041 " return number_zero_v<T>;\n"
27042 " }\n"
27043 "};");
27044
27045 verifyFormat("template <class T>\n"
27046 " requires(std::same_as<int, T>)\n"
27047 "decltype(auto) fun() {}");
27048
27049 auto Style = getLLVMStyle();
27050
27051 verifyFormat(
27052 "template <typename T>\n"
27053 " requires is_default_constructible_v<hash<T>> and\n"
27054 " is_copy_constructible_v<hash<T>> and\n"
27055 " is_move_constructible_v<hash<T>> and\n"
27056 " is_copy_assignable_v<hash<T>> and "
27057 "is_move_assignable_v<hash<T>> and\n"
27058 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
27059 " is_callable_v<hash<T>(T)> and\n"
27060 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
27061 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
27062 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
27063 "struct S {};",
27064 Style);
27065
27066 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
27067 verifyFormat(
27068 "template <typename T>\n"
27069 " requires is_default_constructible_v<hash<T>>\n"
27070 " and is_copy_constructible_v<hash<T>>\n"
27071 " and is_move_constructible_v<hash<T>>\n"
27072 " and is_copy_assignable_v<hash<T>> and "
27073 "is_move_assignable_v<hash<T>>\n"
27074 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
27075 " and is_callable_v<hash<T>(T)>\n"
27076 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
27077 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
27078 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
27079 "&>()))>\n"
27080 "struct S {};",
27081 Style);
27082
27083 Style = getLLVMStyle();
27084 Style.ConstructorInitializerIndentWidth = 4;
27085 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
27086 Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
27087 verifyFormat("constexpr Foo(Foo const &other)\n"
27088 " requires std::is_copy_constructible<T>\n"
27089 " : value{other.value} {\n"
27090 " do_magic();\n"
27091 " do_more_magic();\n"
27092 "}",
27093 Style);
27094
27095 // Not a clause, but we once hit an assert.
27096 verifyFormat("#if 0\n"
27097 "#else\n"
27098 "foo();\n"
27099 "#endif\n"
27100 "bar(requires);");
27101
27102 verifyNoCrash(Code: "template <class T>\n"
27103 " requires(requires { std::declval<T>()");
27104}
27105
27106TEST_F(FormatTest, RequiresExpressionIndentation) {
27107 auto Style = getLLVMStyle();
27108 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope);
27109
27110 verifyFormat("template <typename T>\n"
27111 "concept C = requires(T t) {\n"
27112 " typename T::value;\n"
27113 " requires requires(typename T::value v) {\n"
27114 " { t == v } -> std::same_as<bool>;\n"
27115 " };\n"
27116 "};",
27117 Style);
27118
27119 verifyFormat("template <typename T>\n"
27120 "void bar(T)\n"
27121 " requires Foo<T> && requires(T t) {\n"
27122 " { t.foo() } -> std::same_as<int>;\n"
27123 " } && requires(T t) {\n"
27124 " { t.bar() } -> std::same_as<bool>;\n"
27125 " --t;\n"
27126 " };",
27127 Style);
27128
27129 verifyFormat("template <typename T>\n"
27130 " requires Foo<T> &&\n"
27131 " requires(T t) {\n"
27132 " { t.foo() } -> std::same_as<int>;\n"
27133 " } && requires(T t) {\n"
27134 " { t.bar() } -> std::same_as<bool>;\n"
27135 " --t;\n"
27136 " }\n"
27137 "void bar(T);",
27138 Style);
27139
27140 verifyFormat("template <typename T> void f() {\n"
27141 " if constexpr (requires(T t) {\n"
27142 " { t.bar() } -> std::same_as<bool>;\n"
27143 " }) {\n"
27144 " }\n"
27145 "}",
27146 Style);
27147
27148 verifyFormat("template <typename T> void f() {\n"
27149 " if constexpr (condition && requires(T t) {\n"
27150 " { t.bar() } -> std::same_as<bool>;\n"
27151 " }) {\n"
27152 " }\n"
27153 "}",
27154 Style);
27155
27156 verifyFormat("template <typename T> struct C {\n"
27157 " void f()\n"
27158 " requires requires(T t) {\n"
27159 " { t.bar() } -> std::same_as<bool>;\n"
27160 " };\n"
27161 "};",
27162 Style);
27163
27164 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword;
27165
27166 verifyFormat("template <typename T>\n"
27167 "concept C = requires(T t) {\n"
27168 " typename T::value;\n"
27169 " requires requires(typename T::value v) {\n"
27170 " { t == v } -> std::same_as<bool>;\n"
27171 " };\n"
27172 " };",
27173 Style);
27174
27175 verifyFormat(
27176 "template <typename T>\n"
27177 "void bar(T)\n"
27178 " requires Foo<T> && requires(T t) {\n"
27179 " { t.foo() } -> std::same_as<int>;\n"
27180 " } && requires(T t) {\n"
27181 " { t.bar() } -> std::same_as<bool>;\n"
27182 " --t;\n"
27183 " };",
27184 Style);
27185
27186 verifyFormat("template <typename T>\n"
27187 " requires Foo<T> &&\n"
27188 " requires(T t) {\n"
27189 " { t.foo() } -> std::same_as<int>;\n"
27190 " } && requires(T t) {\n"
27191 " { t.bar() } -> std::same_as<bool>;\n"
27192 " --t;\n"
27193 " }\n"
27194 "void bar(T);",
27195 Style);
27196
27197 verifyFormat("template <typename T> void f() {\n"
27198 " if constexpr (requires(T t) {\n"
27199 " { t.bar() } -> std::same_as<bool>;\n"
27200 " }) {\n"
27201 " }\n"
27202 "}",
27203 Style);
27204
27205 verifyFormat(
27206 "template <typename T> void f() {\n"
27207 " if constexpr (condition && requires(T t) {\n"
27208 " { t.bar() } -> std::same_as<bool>;\n"
27209 " }) {\n"
27210 " }\n"
27211 "}",
27212 Style);
27213
27214 verifyFormat("template <typename T> struct C {\n"
27215 " void f()\n"
27216 " requires requires(T t) {\n"
27217 " { t.bar() } -> std::same_as<bool>;\n"
27218 " };\n"
27219 "};",
27220 Style);
27221}
27222
27223TEST_F(FormatTest, StatementAttributeLikeMacros) {
27224 FormatStyle Style = getLLVMStyle();
27225 StringRef Source = "void Foo::slot() {\n"
27226 " unsigned char MyChar = 'x';\n"
27227 " emit signal(MyChar);\n"
27228 " Q_EMIT signal(MyChar);\n"
27229 "}";
27230
27231 verifyFormat(Source, Style);
27232
27233 Style.AlignConsecutiveDeclarations.Enabled = true;
27234 verifyFormat("void Foo::slot() {\n"
27235 " unsigned char MyChar = 'x';\n"
27236 " emit signal(MyChar);\n"
27237 " Q_EMIT signal(MyChar);\n"
27238 "}",
27239 Source, Style);
27240
27241 Style.StatementAttributeLikeMacros.push_back(x: "emit");
27242 verifyFormat(Source, Style);
27243
27244 Style.StatementAttributeLikeMacros = {};
27245 verifyFormat("void Foo::slot() {\n"
27246 " unsigned char MyChar = 'x';\n"
27247 " emit signal(MyChar);\n"
27248 " Q_EMIT signal(MyChar);\n"
27249 "}",
27250 Source, Style);
27251}
27252
27253TEST_F(FormatTest, IndentAccessModifiers) {
27254 FormatStyle Style = getLLVMStyle();
27255 Style.IndentAccessModifiers = true;
27256 // Members are *two* levels below the record;
27257 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
27258 verifyFormat("class C {\n"
27259 " int i;\n"
27260 "};",
27261 Style);
27262 verifyFormat("union C {\n"
27263 " int i;\n"
27264 " unsigned u;\n"
27265 "};",
27266 Style);
27267 // Access modifiers should be indented one level below the record.
27268 verifyFormat("class C {\n"
27269 " public:\n"
27270 " int i;\n"
27271 "};",
27272 Style);
27273 verifyFormat("class C {\n"
27274 " public /* comment */:\n"
27275 " int i;\n"
27276 "};",
27277 Style);
27278 verifyFormat("struct S {\n"
27279 " private:\n"
27280 " class C {\n"
27281 " int j;\n"
27282 "\n"
27283 " public:\n"
27284 " C();\n"
27285 " };\n"
27286 "\n"
27287 " public:\n"
27288 " int i;\n"
27289 "};",
27290 Style);
27291 // Enumerations are not records and should be unaffected.
27292 Style.AllowShortEnumsOnASingleLine = false;
27293 verifyFormat("enum class E {\n"
27294 " A,\n"
27295 " B\n"
27296 "};",
27297 Style);
27298 // Test with a different indentation width;
27299 // also proves that the result is Style.AccessModifierOffset agnostic.
27300 Style.IndentWidth = 3;
27301 verifyFormat("class C {\n"
27302 " public:\n"
27303 " int i;\n"
27304 "};",
27305 Style);
27306 verifyFormat("class C {\n"
27307 " public /**/:\n"
27308 " int i;\n"
27309 "};",
27310 Style);
27311 Style.AttributeMacros.push_back(x: "FOO");
27312 verifyFormat("class C {\n"
27313 " FOO public:\n"
27314 " int i;\n"
27315 "};",
27316 Style);
27317}
27318
27319TEST_F(FormatTest, LimitlessStringsAndComments) {
27320 auto Style = getLLVMStyleWithColumns(ColumnLimit: 0);
27321 constexpr StringRef Code =
27322 "/**\n"
27323 " * This is a multiline comment with quite some long lines, at least for "
27324 "the LLVM Style.\n"
27325 " * We will redo this with strings and line comments. Just to check if "
27326 "everything is working.\n"
27327 " */\n"
27328 "bool foo() {\n"
27329 " /* Single line multi line comment. */\n"
27330 " const std::string String = \"This is a multiline string with quite "
27331 "some long lines, at least for the LLVM Style.\"\n"
27332 " \"We already did it with multi line "
27333 "comments, and we will do it with line comments. Just to check if "
27334 "everything is working.\";\n"
27335 " // This is a line comment (block) with quite some long lines, at "
27336 "least for the LLVM Style.\n"
27337 " // We already did this with multi line comments and strings. Just to "
27338 "check if everything is working.\n"
27339 " const std::string SmallString = \"Hello World\";\n"
27340 " // Small line comment\n"
27341 " return String.size() > SmallString.size();\n"
27342 "}";
27343 verifyNoChange(Code, Style);
27344}
27345
27346TEST_F(FormatTest, FormatDecayCopy) {
27347 // error cases from unit tests
27348 verifyFormat("foo(auto())");
27349 verifyFormat("foo(auto{})");
27350 verifyFormat("foo(auto({}))");
27351 verifyFormat("foo(auto{{}})");
27352
27353 verifyFormat("foo(auto(1))");
27354 verifyFormat("foo(auto{1})");
27355 verifyFormat("foo(new auto(1))");
27356 verifyFormat("foo(new auto{1})");
27357 verifyFormat("decltype(auto(1)) x;");
27358 verifyFormat("decltype(auto{1}) x;");
27359 verifyFormat("auto(x);");
27360 verifyFormat("auto{x};");
27361 verifyFormat("new auto{x};");
27362 verifyFormat("auto{x} = y;");
27363 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
27364 // the user's own fault
27365 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
27366 // clearly the user's own fault
27367 verifyFormat("auto (*p)() = f;");
27368}
27369
27370TEST_F(FormatTest, Cpp20ModulesSupport) {
27371 FormatStyle Style = getLLVMStyle();
27372 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
27373 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
27374
27375 verifyFormat("export import foo;", Style);
27376 verifyFormat("export import foo:bar;", Style);
27377 verifyFormat("export import foo.bar;", Style);
27378 verifyFormat("export import foo.bar:baz;", Style);
27379 verifyFormat("export import :bar;", Style);
27380 verifyFormat("export module foo:bar;", Style);
27381 verifyFormat("export module foo;", Style);
27382 verifyFormat("export module foo.bar;", Style);
27383 verifyFormat("export module foo.bar:baz;", Style);
27384 verifyFormat("export import <string_view>;", Style);
27385 verifyFormat("export import <Foo/Bar>;", Style);
27386
27387 verifyFormat("export type_name var;", Style);
27388 verifyFormat("template <class T> export using A = B<T>;", Style);
27389 verifyFormat("export using A = B;", Style);
27390 verifyFormat("export int func() {\n"
27391 " foo();\n"
27392 "}",
27393 Style);
27394 verifyFormat("export struct {\n"
27395 " int foo;\n"
27396 "};",
27397 Style);
27398 verifyFormat("export {\n"
27399 " int foo;\n"
27400 "};",
27401 Style);
27402 verifyFormat("export export char const *hello() { return \"hello\"; }");
27403
27404 verifyFormat("import bar;", Style);
27405 verifyFormat("import foo.bar;", Style);
27406 verifyFormat("import foo:bar;", Style);
27407 verifyFormat("import :bar;", Style);
27408 verifyFormat("import /* module partition */ :bar;", Style);
27409 verifyFormat("import <ctime>;", Style);
27410 verifyFormat("import \"header\";", Style);
27411
27412 verifyFormat("module foo;", Style);
27413 verifyFormat("module foo:bar;", Style);
27414 verifyFormat("module foo.bar;", Style);
27415 verifyFormat("module;", Style);
27416
27417 verifyFormat("export namespace hi {\n"
27418 "const char *sayhi();\n"
27419 "}",
27420 Style);
27421
27422 verifyFormat("module :private;", Style);
27423 verifyFormat("import <foo/bar.h>;", Style);
27424 verifyFormat("import foo...bar;", Style);
27425 verifyFormat("import ..........;", Style);
27426 verifyFormat("module foo:private;", Style);
27427 verifyFormat("import a", Style);
27428 verifyFormat("module a", Style);
27429 verifyFormat("export import a", Style);
27430 verifyFormat("export module a", Style);
27431
27432 verifyFormat("import", Style);
27433 verifyFormat("module", Style);
27434 verifyFormat("export", Style);
27435
27436 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
27437}
27438
27439TEST_F(FormatTest, CoroutineForCoawait) {
27440 FormatStyle Style = getLLVMStyle();
27441 verifyFormat("for co_await (auto x : range())\n ;");
27442 verifyFormat("for (auto i : arr) {\n"
27443 "}",
27444 Style);
27445 verifyFormat("for co_await (auto i : arr) {\n"
27446 "}",
27447 Style);
27448 verifyFormat("for co_await (auto i : foo(T{})) {\n"
27449 "}",
27450 Style);
27451}
27452
27453TEST_F(FormatTest, CoroutineCoAwait) {
27454 verifyFormat("int x = co_await foo();");
27455 verifyFormat("int x = (co_await foo());");
27456 verifyFormat("co_await (42);");
27457 verifyFormat("void operator co_await(int);");
27458 verifyFormat("void operator co_await(a);");
27459 verifyFormat("co_await a;");
27460 verifyFormat("co_await missing_await_resume{};");
27461 verifyFormat("co_await a; // comment");
27462 verifyFormat("void test0() { co_await a; }");
27463 verifyFormat("co_await co_await co_await foo();");
27464 verifyFormat("co_await foo().bar();");
27465 verifyFormat("co_await [this]() -> Task { co_return x; }");
27466 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
27467 "foo(); }(x, y);");
27468
27469 FormatStyle Style = getLLVMStyleWithColumns(ColumnLimit: 40);
27470 verifyFormat("co_await [this](int a, int b) -> Task {\n"
27471 " co_return co_await foo();\n"
27472 "}(x, y);",
27473 Style);
27474 verifyFormat("co_await;");
27475}
27476
27477TEST_F(FormatTest, CoroutineCoYield) {
27478 verifyFormat("int x = co_yield foo();");
27479 verifyFormat("int x = (co_yield foo());");
27480 verifyFormat("co_yield (42);");
27481 verifyFormat("co_yield {42};");
27482 verifyFormat("co_yield 42;");
27483 verifyFormat("co_yield n++;");
27484 verifyFormat("co_yield ++n;");
27485 verifyFormat("co_yield;");
27486}
27487
27488TEST_F(FormatTest, CoroutineCoReturn) {
27489 verifyFormat("co_return (42);");
27490 verifyFormat("co_return;");
27491 verifyFormat("co_return {};");
27492 verifyFormat("co_return x;");
27493 verifyFormat("co_return co_await foo();");
27494 verifyFormat("co_return co_yield foo();");
27495}
27496
27497TEST_F(FormatTest, EmptyShortBlock) {
27498 auto Style = getLLVMStyle();
27499 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
27500
27501 verifyFormat("try {\n"
27502 " doA();\n"
27503 "} catch (Exception &e) {\n"
27504 " e.printStackTrace();\n"
27505 "}",
27506 Style);
27507
27508 verifyFormat("try {\n"
27509 " doA();\n"
27510 "} catch (Exception &e) {}",
27511 Style);
27512}
27513
27514TEST_F(FormatTest, ShortTemplatedArgumentLists) {
27515 auto Style = getLLVMStyle();
27516
27517 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style);
27518 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style);
27519 verifyFormat("struct Y : X<[] { return 0; }> {};", Style);
27520 verifyFormat("struct Y<[] { return 0; }> {};", Style);
27521
27522 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style);
27523 verifyFormat("template <int N> struct Foo<char[N]> {};", Style);
27524}
27525
27526TEST_F(FormatTest, MultilineLambdaInConditional) {
27527 auto Style = getLLVMStyleWithColumns(ColumnLimit: 70);
27528 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
27529 " ;\n"
27530 " return 5;\n"
27531 "}()\n"
27532 " : 2;",
27533 Style);
27534 verifyFormat(
27535 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
27536 " ;\n"
27537 " return 5;\n"
27538 "}();",
27539 Style);
27540
27541 Style = getLLVMStyleWithColumns(ColumnLimit: 60);
27542 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
27543 " ? []() {\n"
27544 " ;\n"
27545 " return 5;\n"
27546 " }()\n"
27547 " : 2;",
27548 Style);
27549 verifyFormat("auto aLengthyIdentifier =\n"
27550 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
27551 " ;\n"
27552 " return 5;\n"
27553 " }();",
27554 Style);
27555
27556 Style = getLLVMStyleWithColumns(ColumnLimit: 40);
27557 verifyFormat("auto aLengthyIdentifier =\n"
27558 " oneExpressionSoThatWeBreak ? []() {\n"
27559 " ;\n"
27560 " return 5;\n"
27561 " }()\n"
27562 " : 2;",
27563 Style);
27564 verifyFormat("auto aLengthyIdentifier =\n"
27565 " oneExpressionSoThatWeBreak\n"
27566 " ? 2\n"
27567 " : []() {\n"
27568 " ;\n"
27569 " return 5;\n"
27570 " };",
27571 Style);
27572}
27573
27574TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
27575 auto Style = getLLVMStyle();
27576
27577 StringRef Short = "functionCall(paramA, paramB, paramC);\n"
27578 "void functionDecl(int a, int b, int c);";
27579
27580 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
27581 "paramF, paramG, paramH, paramI);\n"
27582 "void functionDecl(int argumentA, int argumentB, int "
27583 "argumentC, int argumentD, int argumentE);";
27584
27585 verifyFormat(Short, Style);
27586
27587 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
27588 "paramF, paramG, paramH,\n"
27589 " paramI);\n"
27590 "void functionDecl(int argumentA, int argumentB, int "
27591 "argumentC, int argumentD,\n"
27592 " int argumentE);";
27593
27594 verifyFormat(NoBreak, Medium, Style);
27595 verifyFormat(NoBreak,
27596 "functionCall(\n"
27597 " paramA,\n"
27598 " paramB,\n"
27599 " paramC,\n"
27600 " paramD,\n"
27601 " paramE,\n"
27602 " paramF,\n"
27603 " paramG,\n"
27604 " paramH,\n"
27605 " paramI\n"
27606 ");\n"
27607 "void functionDecl(\n"
27608 " int argumentA,\n"
27609 " int argumentB,\n"
27610 " int argumentC,\n"
27611 " int argumentD,\n"
27612 " int argumentE\n"
27613 ");",
27614 Style);
27615
27616 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
27617 " nestedLongFunctionCall(argument1, "
27618 "argument2, argument3,\n"
27619 " argument4, "
27620 "argument5));",
27621 Style);
27622
27623 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27624
27625 verifyFormat(Short, Style);
27626 verifyFormat(
27627 "functionCall(\n"
27628 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
27629 "paramI\n"
27630 ");\n"
27631 "void functionDecl(\n"
27632 " int argumentA, int argumentB, int argumentC, int argumentD, int "
27633 "argumentE\n"
27634 ");",
27635 Medium, Style);
27636
27637 Style.AllowAllArgumentsOnNextLine = false;
27638 Style.AllowAllParametersOfDeclarationOnNextLine = false;
27639
27640 verifyFormat(Short, Style);
27641 verifyFormat(
27642 "functionCall(\n"
27643 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
27644 "paramI\n"
27645 ");\n"
27646 "void functionDecl(\n"
27647 " int argumentA, int argumentB, int argumentC, int argumentD, int "
27648 "argumentE\n"
27649 ");",
27650 Medium, Style);
27651
27652 Style.BinPackArguments = false;
27653 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
27654
27655 verifyFormat(Short, Style);
27656
27657 verifyFormat("functionCall(\n"
27658 " paramA,\n"
27659 " paramB,\n"
27660 " paramC,\n"
27661 " paramD,\n"
27662 " paramE,\n"
27663 " paramF,\n"
27664 " paramG,\n"
27665 " paramH,\n"
27666 " paramI\n"
27667 ");\n"
27668 "void functionDecl(\n"
27669 " int argumentA,\n"
27670 " int argumentB,\n"
27671 " int argumentC,\n"
27672 " int argumentD,\n"
27673 " int argumentE\n"
27674 ");",
27675 Medium, Style);
27676
27677 verifyFormat("outerFunctionCall(\n"
27678 " nestedFunctionCall(argument1),\n"
27679 " nestedLongFunctionCall(\n"
27680 " argument1,\n"
27681 " argument2,\n"
27682 " argument3,\n"
27683 " argument4,\n"
27684 " argument5\n"
27685 " )\n"
27686 ");",
27687 Style);
27688
27689 verifyFormat("int a = (int)b;", Style);
27690 verifyFormat("int a = (int)b;",
27691 "int a = (\n"
27692 " int\n"
27693 ") b;",
27694 Style);
27695
27696 verifyFormat("return (true);", Style);
27697 verifyFormat("return (true);",
27698 "return (\n"
27699 " true\n"
27700 ");",
27701 Style);
27702
27703 verifyFormat("void foo();", Style);
27704 verifyFormat("void foo();",
27705 "void foo(\n"
27706 ");",
27707 Style);
27708
27709 verifyFormat("void foo() {}", Style);
27710 verifyFormat("void foo() {}",
27711 "void foo(\n"
27712 ") {\n"
27713 "}",
27714 Style);
27715
27716 verifyFormat("auto string = std::string();", Style);
27717 verifyFormat("auto string = std::string();",
27718 "auto string = std::string(\n"
27719 ");",
27720 Style);
27721
27722 verifyFormat("void (*functionPointer)() = nullptr;", Style);
27723 verifyFormat("void (*functionPointer)() = nullptr;",
27724 "void (\n"
27725 " *functionPointer\n"
27726 ")\n"
27727 "(\n"
27728 ") = nullptr;",
27729 Style);
27730}
27731
27732TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
27733 auto Style = getLLVMStyle();
27734
27735 verifyFormat("if (foo()) {\n"
27736 " return;\n"
27737 "}",
27738 Style);
27739
27740 verifyFormat("if (quiteLongArg !=\n"
27741 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
27742 "comment\n"
27743 " return;\n"
27744 "}",
27745 Style);
27746
27747 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27748
27749 verifyFormat("if (foo()) {\n"
27750 " return;\n"
27751 "}",
27752 Style);
27753
27754 verifyFormat("if (quiteLongArg !=\n"
27755 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
27756 "comment\n"
27757 " return;\n"
27758 "}",
27759 Style);
27760
27761 verifyFormat("void foo() {\n"
27762 " if (camelCaseName < alsoLongName ||\n"
27763 " anotherEvenLongerName <=\n"
27764 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
27765 "\n"
27766 " otherName < thisLastName) {\n"
27767 " return;\n"
27768 " } else if (quiteLongName < alsoLongName ||\n"
27769 " anotherEvenLongerName <=\n"
27770 " thisReallyReallyReallyReallyReallyReallyLonger"
27771 "Name ||\n"
27772 " otherName < thisLastName) {\n"
27773 " return;\n"
27774 " }\n"
27775 "}",
27776 Style);
27777
27778 Style.ContinuationIndentWidth = 2;
27779 verifyFormat("void foo() {\n"
27780 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
27781 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27782 " if (false) {\n"
27783 " return;\n"
27784 " } else if (thisIsRatherALongIfClause && "
27785 "thatIExpectToBeBroken ||\n"
27786 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27787 " return;\n"
27788 " }\n"
27789 " }\n"
27790 "}",
27791 Style);
27792}
27793
27794TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
27795 auto Style = getLLVMStyle();
27796
27797 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27798 " doSomething();\n"
27799 "}",
27800 Style);
27801
27802 verifyFormat("for (int myReallyLongCountVariable = 0; "
27803 "myReallyLongCountVariable < count;\n"
27804 " myReallyLongCountVariable++) {\n"
27805 " doSomething();\n"
27806 "}",
27807 Style);
27808
27809 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27810
27811 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27812 " doSomething();\n"
27813 "}",
27814 Style);
27815
27816 verifyFormat("for (int myReallyLongCountVariable = 0; "
27817 "myReallyLongCountVariable < count;\n"
27818 " myReallyLongCountVariable++) {\n"
27819 " doSomething();\n"
27820 "}",
27821 Style);
27822}
27823
27824TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
27825 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
27826 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
27827 // Aggregate initialization.
27828 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27829 " 10000000, 20000000\n"
27830 "};",
27831 Style);
27832 verifyFormat("SomeStruct s{\n"
27833 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
27834 " \"zzzzzzzzzzzzzzzz\"\n"
27835 "};",
27836 Style);
27837 // Designated initializers.
27838 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27839 " [0] = 10000000, [1] = 20000000\n"
27840 "};",
27841 Style);
27842 verifyFormat("SomeStruct s{\n"
27843 " .foo = \"xxxxxxxxxxxxx\",\n"
27844 " .bar = \"yyyyyyyyyyyyy\",\n"
27845 " .baz = \"zzzzzzzzzzzzz\"\n"
27846 "};",
27847 Style);
27848 // List initialization.
27849 verifyFormat("SomeStruct s{\n"
27850 " \"xxxxxxxxxxxxx\",\n"
27851 " \"yyyyyyyyyyyyy\",\n"
27852 " \"zzzzzzzzzzzzz\",\n"
27853 "};",
27854 Style);
27855 verifyFormat("SomeStruct{\n"
27856 " \"xxxxxxxxxxxxx\",\n"
27857 " \"yyyyyyyyyyyyy\",\n"
27858 " \"zzzzzzzzzzzzz\",\n"
27859 "};",
27860 Style);
27861 verifyFormat("new SomeStruct{\n"
27862 " \"xxxxxxxxxxxxx\",\n"
27863 " \"yyyyyyyyyyyyy\",\n"
27864 " \"zzzzzzzzzzzzz\",\n"
27865 "};",
27866 Style);
27867 // Member initializer.
27868 verifyFormat("class SomeClass {\n"
27869 " SomeStruct s{\n"
27870 " \"xxxxxxxxxxxxx\",\n"
27871 " \"yyyyyyyyyyyyy\",\n"
27872 " \"zzzzzzzzzzzzz\",\n"
27873 " };\n"
27874 "};",
27875 Style);
27876 // Constructor member initializer.
27877 verifyFormat("SomeClass::SomeClass : strct{\n"
27878 " \"xxxxxxxxxxxxx\",\n"
27879 " \"yyyyyyyyyyyyy\",\n"
27880 " \"zzzzzzzzzzzzz\",\n"
27881 " } {}",
27882 Style);
27883 // Copy initialization.
27884 verifyFormat("SomeStruct s = SomeStruct{\n"
27885 " \"xxxxxxxxxxxxx\",\n"
27886 " \"yyyyyyyyyyyyy\",\n"
27887 " \"zzzzzzzzzzzzz\",\n"
27888 "};",
27889 Style);
27890 // Copy list initialization.
27891 verifyFormat("SomeStruct s = {\n"
27892 " \"xxxxxxxxxxxxx\",\n"
27893 " \"yyyyyyyyyyyyy\",\n"
27894 " \"zzzzzzzzzzzzz\",\n"
27895 "};",
27896 Style);
27897 // Assignment operand initialization.
27898 verifyFormat("s = {\n"
27899 " \"xxxxxxxxxxxxx\",\n"
27900 " \"yyyyyyyyyyyyy\",\n"
27901 " \"zzzzzzzzzzzzz\",\n"
27902 "};",
27903 Style);
27904 // Returned object initialization.
27905 verifyFormat("return {\n"
27906 " \"xxxxxxxxxxxxx\",\n"
27907 " \"yyyyyyyyyyyyy\",\n"
27908 " \"zzzzzzzzzzzzz\",\n"
27909 "};",
27910 Style);
27911 // Initializer list.
27912 verifyFormat("auto initializerList = {\n"
27913 " \"xxxxxxxxxxxxx\",\n"
27914 " \"yyyyyyyyyyyyy\",\n"
27915 " \"zzzzzzzzzzzzz\",\n"
27916 "};",
27917 Style);
27918 // Function parameter initialization.
27919 verifyFormat("func({\n"
27920 " \"xxxxxxxxxxxxx\",\n"
27921 " \"yyyyyyyyyyyyy\",\n"
27922 " \"zzzzzzzzzzzzz\",\n"
27923 "});",
27924 Style);
27925 // Nested init lists.
27926 verifyFormat("SomeStruct s = {\n"
27927 " {{init1, init2, init3, init4, init5},\n"
27928 " {init1, init2, init3, init4, init5}}\n"
27929 "};",
27930 Style);
27931 verifyFormat("SomeStruct s = {\n"
27932 " {{\n"
27933 " .init1 = 1,\n"
27934 " .init2 = 2,\n"
27935 " .init3 = 3,\n"
27936 " .init4 = 4,\n"
27937 " .init5 = 5,\n"
27938 " },\n"
27939 " {init1, init2, init3, init4, init5}}\n"
27940 "};",
27941 Style);
27942 verifyFormat("SomeArrayT a[3] = {\n"
27943 " {\n"
27944 " foo,\n"
27945 " bar,\n"
27946 " },\n"
27947 " {\n"
27948 " foo,\n"
27949 " bar,\n"
27950 " },\n"
27951 " SomeArrayT{},\n"
27952 "};",
27953 Style);
27954 verifyFormat("SomeArrayT a[3] = {\n"
27955 " {foo},\n"
27956 " {\n"
27957 " {\n"
27958 " init1,\n"
27959 " init2,\n"
27960 " init3,\n"
27961 " },\n"
27962 " {\n"
27963 " init1,\n"
27964 " init2,\n"
27965 " init3,\n"
27966 " },\n"
27967 " },\n"
27968 " {baz},\n"
27969 "};",
27970 Style);
27971}
27972
27973TEST_F(FormatTest, UnderstandsDigraphs) {
27974 verifyFormat("int arr<:5:> = {};");
27975 verifyFormat("int arr[5] = <%%>;");
27976 verifyFormat("int arr<:::qualified_variable:> = {};");
27977 verifyFormat("int arr[::qualified_variable] = <%%>;");
27978 verifyFormat("%:include <header>");
27979 verifyFormat("%:define A x##y");
27980 verifyFormat("#define A x%:%:y");
27981}
27982
27983TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) {
27984 auto Style = getLLVMStyle();
27985 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
27986 Style.AlignConsecutiveAssignments.Enabled = true;
27987 Style.AlignConsecutiveDeclarations.Enabled = true;
27988
27989 // The AlignArray code is incorrect for non square Arrays and can cause
27990 // crashes, these tests assert that the array is not changed but will
27991 // also act as regression tests for when it is properly fixed
27992 verifyFormat("struct test demo[] = {\n"
27993 " {1, 2},\n"
27994 " {3, 4, 5},\n"
27995 " {6, 7, 8}\n"
27996 "};",
27997 Style);
27998 verifyFormat("struct test demo[] = {\n"
27999 " {1, 2, 3, 4, 5},\n"
28000 " {3, 4, 5},\n"
28001 " {6, 7, 8}\n"
28002 "};",
28003 Style);
28004 verifyFormat("struct test demo[] = {\n"
28005 " {1, 2, 3, 4, 5},\n"
28006 " {3, 4, 5},\n"
28007 " {6, 7, 8, 9, 10, 11, 12}\n"
28008 "};",
28009 Style);
28010 verifyFormat("struct test demo[] = {\n"
28011 " {1, 2, 3},\n"
28012 " {3, 4, 5},\n"
28013 " {6, 7, 8, 9, 10, 11, 12}\n"
28014 "};",
28015 Style);
28016
28017 verifyFormat("S{\n"
28018 " {},\n"
28019 " {},\n"
28020 " {a, b}\n"
28021 "};",
28022 Style);
28023 verifyFormat("S{\n"
28024 " {},\n"
28025 " {},\n"
28026 " {a, b},\n"
28027 "};",
28028 Style);
28029 verifyFormat("void foo() {\n"
28030 " auto thing = test{\n"
28031 " {\n"
28032 " {13},\n"
28033 " {something}, // A\n"
28034 " }\n"
28035 " };\n"
28036 "}",
28037 Style);
28038}
28039
28040TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) {
28041 auto Style = getLLVMStyle();
28042 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
28043 Style.AlignConsecutiveAssignments.Enabled = true;
28044 Style.AlignConsecutiveDeclarations.Enabled = true;
28045
28046 // The AlignArray code is incorrect for non square Arrays and can cause
28047 // crashes, these tests assert that the array is not changed but will
28048 // also act as regression tests for when it is properly fixed
28049 verifyFormat("struct test demo[] = {\n"
28050 " {1, 2},\n"
28051 " {3, 4, 5},\n"
28052 " {6, 7, 8}\n"
28053 "};",
28054 Style);
28055 verifyFormat("struct test demo[] = {\n"
28056 " {1, 2, 3, 4, 5},\n"
28057 " {3, 4, 5},\n"
28058 " {6, 7, 8}\n"
28059 "};",
28060 Style);
28061 verifyFormat("struct test demo[] = {\n"
28062 " {1, 2, 3, 4, 5},\n"
28063 " {3, 4, 5},\n"
28064 " {6, 7, 8, 9, 10, 11, 12}\n"
28065 "};",
28066 Style);
28067 verifyFormat("struct test demo[] = {\n"
28068 " {1, 2, 3},\n"
28069 " {3, 4, 5},\n"
28070 " {6, 7, 8, 9, 10, 11, 12}\n"
28071 "};",
28072 Style);
28073
28074 verifyFormat("S{\n"
28075 " {},\n"
28076 " {},\n"
28077 " {a, b}\n"
28078 "};",
28079 Style);
28080 verifyFormat("S{\n"
28081 " {},\n"
28082 " {},\n"
28083 " {a, b},\n"
28084 "};",
28085 Style);
28086 verifyFormat("void foo() {\n"
28087 " auto thing = test{\n"
28088 " {\n"
28089 " {13},\n"
28090 " {something}, // A\n"
28091 " }\n"
28092 " };\n"
28093 "}",
28094 Style);
28095}
28096
28097TEST_F(FormatTest, FormatsVariableTemplates) {
28098 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
28099 verifyFormat("template <typename T> "
28100 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
28101}
28102
28103TEST_F(FormatTest, RemoveSemicolon) {
28104 FormatStyle Style = getLLVMStyle();
28105 Style.RemoveSemicolon = true;
28106
28107 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
28108 "int max(int a, int b) { return a > b ? a : b; };", Style);
28109
28110 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
28111 "int max(int a, int b) { return a > b ? a : b; };;", Style);
28112
28113 verifyFormat("class Foo {\n"
28114 " int getSomething() const { return something; }\n"
28115 "};",
28116 "class Foo {\n"
28117 " int getSomething() const { return something; };\n"
28118 "};",
28119 Style);
28120
28121 verifyFormat("class Foo {\n"
28122 " int getSomething() const { return something; }\n"
28123 "};",
28124 "class Foo {\n"
28125 " int getSomething() const { return something; };;\n"
28126 "};",
28127 Style);
28128
28129 verifyFormat("for (;;) {\n"
28130 "}",
28131 Style);
28132
28133 verifyFormat("class [[deprecated(\"\")]] C {\n"
28134 " int i;\n"
28135 "};",
28136 Style);
28137
28138 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
28139 " int i;\n"
28140 "};",
28141 Style);
28142
28143 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style);
28144
28145 verifyFormat("void main() {}", "void main() {};", Style);
28146
28147 verifyFormat("struct Foo {\n"
28148 " Foo() {}\n"
28149 " ~Foo() {}\n"
28150 "};",
28151 "struct Foo {\n"
28152 " Foo() {};\n"
28153 " ~Foo() {};\n"
28154 "};",
28155 Style);
28156
28157// We can't (and probably shouldn't) support the following.
28158#if 0
28159 verifyFormat("void foo() {} //\n"
28160 "int bar;",
28161 "void foo() {}; //\n"
28162 "; int bar;",
28163 Style);
28164#endif
28165
28166 verifyFormat("auto sgf = [] {\n"
28167 " ogl = {\n"
28168 " a, b, c, d, e,\n"
28169 " };\n"
28170 "};",
28171 Style);
28172
28173 Style.TypenameMacros.push_back(x: "STRUCT");
28174 verifyFormat("STRUCT(T, B) { int i; };", Style);
28175}
28176
28177TEST_F(FormatTest, EnumTrailingComma) {
28178 constexpr StringRef Code("enum : int { /**/ };\n"
28179 "enum {\n"
28180 " a,\n"
28181 " b,\n"
28182 " c, //\n"
28183 "};\n"
28184 "enum Color { red, green, blue /**/ };");
28185 verifyFormat(Code);
28186
28187 auto Style = getLLVMStyle();
28188 Style.EnumTrailingComma = FormatStyle::ETC_Insert;
28189 verifyFormat("enum : int { /**/ };\n"
28190 "enum {\n"
28191 " a,\n"
28192 " b,\n"
28193 " c, //\n"
28194 "};\n"
28195 "enum Color { red, green, blue, /**/ };",
28196 Code, Style);
28197
28198 Style.EnumTrailingComma = FormatStyle::ETC_Remove;
28199 verifyFormat("enum : int { /**/ };\n"
28200 "enum {\n"
28201 " a,\n"
28202 " b,\n"
28203 " c //\n"
28204 "};\n"
28205 "enum Color { red, green, blue /**/ };",
28206 Code, Style);
28207
28208 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
28209 Style.AllowShortEnumsOnASingleLine = false;
28210
28211 constexpr StringRef Input("enum {\n"
28212 " //\n"
28213 " a,\n"
28214 " /**/\n"
28215 " b,\n"
28216 "};");
28217 verifyFormat(Input, Input, Style, {tooling::Range(12, 3)}); // line 3
28218 verifyFormat("enum {\n"
28219 " //\n"
28220 " a,\n"
28221 " /**/\n"
28222 " b\n"
28223 "};",
28224 Input, Style, {tooling::Range(24, 3)}); // line 5
28225
28226 Style.EnumTrailingComma = FormatStyle::ETC_Insert;
28227 verifyFormat("enum class MyEnum_E {\n"
28228 " MY_ENUM = 0U,\n"
28229 "};",
28230 "enum class MyEnum_E {\n"
28231 " MY_ENUM = 0U\n"
28232 "};",
28233 Style);
28234}
28235
28236TEST_F(FormatTest, BreakAfterAttributes) {
28237 constexpr StringRef Code("[[maybe_unused]] const int i;\n"
28238 "[[foo([[]])]] [[maybe_unused]]\n"
28239 "int j;\n"
28240 "[[maybe_unused]]\n"
28241 "foo<int> k;\n"
28242 "[[nodiscard]] inline int f(int &i);\n"
28243 "[[foo([[]])]] [[nodiscard]]\n"
28244 "int g(int &i);\n"
28245 "[[nodiscard]]\n"
28246 "inline int f(int &i) {\n"
28247 " i = 1;\n"
28248 " return 0;\n"
28249 "}\n"
28250 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
28251 " i = 0;\n"
28252 " return 1;\n"
28253 "}");
28254
28255 FormatStyle Style = getLLVMStyle();
28256 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave);
28257 verifyNoChange(Code, Style);
28258
28259 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
28260 verifyFormat("[[maybe_unused]] const int i;\n"
28261 "[[foo([[]])]] [[maybe_unused]] int j;\n"
28262 "[[maybe_unused]] foo<int> k;\n"
28263 "[[nodiscard]] inline int f(int &i);\n"
28264 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
28265 "[[nodiscard]] inline int f(int &i) {\n"
28266 " i = 1;\n"
28267 " return 0;\n"
28268 "}\n"
28269 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
28270 " i = 0;\n"
28271 " return 1;\n"
28272 "}",
28273 Code, Style);
28274
28275 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
28276 verifyFormat("[[maybe_unused]]\n"
28277 "const int i;\n"
28278 "[[foo([[]])]] [[maybe_unused]]\n"
28279 "int j;\n"
28280 "[[maybe_unused]]\n"
28281 "foo<int> k;\n"
28282 "[[nodiscard]]\n"
28283 "inline int f(int &i);\n"
28284 "[[foo([[]])]] [[nodiscard]]\n"
28285 "int g(int &i);\n"
28286 "[[nodiscard]]\n"
28287 "inline int f(int &i) {\n"
28288 " i = 1;\n"
28289 " return 0;\n"
28290 "}\n"
28291 "[[foo([[]])]] [[nodiscard]]\n"
28292 "int g(int &i) {\n"
28293 " i = 0;\n"
28294 " return 1;\n"
28295 "}",
28296 Code, Style);
28297
28298 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n"
28299 " f();\n"
28300 "else\n"
28301 " g();\n"
28302 "[[foo([[]])]]\n"
28303 "switch (b) {\n"
28304 "[[unlikely]] case 1:\n"
28305 " ++b;\n"
28306 " break;\n"
28307 "[[likely]]\n"
28308 "default:\n"
28309 " return;\n"
28310 "}\n"
28311 "[[unlikely]] for (; c > 0; --c)\n"
28312 " h();\n"
28313 "[[likely]]\n"
28314 "while (d > 0)\n"
28315 " --d;");
28316
28317 Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
28318 verifyNoChange(CtrlStmtCode, Style);
28319
28320 Style.BreakAfterAttributes = FormatStyle::ABS_Never;
28321 verifyFormat("[[likely]] if (a)\n"
28322 " f();\n"
28323 "else\n"
28324 " g();\n"
28325 "[[foo([[]])]] switch (b) {\n"
28326 "[[unlikely]] case 1:\n"
28327 " ++b;\n"
28328 " break;\n"
28329 "[[likely]] default:\n"
28330 " return;\n"
28331 "}\n"
28332 "[[unlikely]] for (; c > 0; --c)\n"
28333 " h();\n"
28334 "[[likely]] while (d > 0)\n"
28335 " --d;",
28336 CtrlStmtCode, Style);
28337
28338 Style.BreakAfterAttributes = FormatStyle::ABS_Always;
28339 verifyFormat("[[likely]]\n"
28340 "if (a)\n"
28341 " f();\n"
28342 "else\n"
28343 " g();\n"
28344 "[[foo([[]])]]\n"
28345 "switch (b) {\n"
28346 "[[unlikely]]\n"
28347 "case 1:\n"
28348 " ++b;\n"
28349 " break;\n"
28350 "[[likely]]\n"
28351 "default:\n"
28352 " return;\n"
28353 "}\n"
28354 "[[unlikely]]\n"
28355 "for (; c > 0; --c)\n"
28356 " h();\n"
28357 "[[likely]]\n"
28358 "while (d > 0)\n"
28359 " --d;",
28360 CtrlStmtCode, Style);
28361
28362 verifyFormat("[[nodiscard]]\n"
28363 "operator bool();\n"
28364 "[[nodiscard]]\n"
28365 "operator bool() {\n"
28366 " return true;\n"
28367 "}",
28368 "[[nodiscard]] operator bool();\n"
28369 "[[nodiscard]] operator bool() { return true; }",
28370 Style);
28371
28372 constexpr StringRef CtorDtorCode("struct Foo {\n"
28373 " [[deprecated]] Foo();\n"
28374 " [[deprecated]] Foo() {}\n"
28375 " [[deprecated]] ~Foo();\n"
28376 " [[deprecated]] ~Foo() {}\n"
28377 " [[deprecated]] void f();\n"
28378 " [[deprecated]] void f() {}\n"
28379 "};\n"
28380 "[[deprecated]] Bar::Bar() {}\n"
28381 "[[deprecated]] Bar::~Bar() {}\n"
28382 "[[deprecated]] void g() {}");
28383 verifyFormat("struct Foo {\n"
28384 " [[deprecated]]\n"
28385 " Foo();\n"
28386 " [[deprecated]]\n"
28387 " Foo() {}\n"
28388 " [[deprecated]]\n"
28389 " ~Foo();\n"
28390 " [[deprecated]]\n"
28391 " ~Foo() {}\n"
28392 " [[deprecated]]\n"
28393 " void f();\n"
28394 " [[deprecated]]\n"
28395 " void f() {}\n"
28396 "};\n"
28397 "[[deprecated]]\n"
28398 "Bar::Bar() {}\n"
28399 "[[deprecated]]\n"
28400 "Bar::~Bar() {}\n"
28401 "[[deprecated]]\n"
28402 "void g() {}",
28403 CtorDtorCode, Style);
28404
28405 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
28406 verifyFormat("struct Foo {\n"
28407 " [[deprecated]]\n"
28408 " Foo();\n"
28409 " [[deprecated]]\n"
28410 " Foo()\n"
28411 " {\n"
28412 " }\n"
28413 " [[deprecated]]\n"
28414 " ~Foo();\n"
28415 " [[deprecated]]\n"
28416 " ~Foo()\n"
28417 " {\n"
28418 " }\n"
28419 " [[deprecated]]\n"
28420 " void f();\n"
28421 " [[deprecated]]\n"
28422 " void f()\n"
28423 " {\n"
28424 " }\n"
28425 "};\n"
28426 "[[deprecated]]\n"
28427 "Bar::Bar()\n"
28428 "{\n"
28429 "}\n"
28430 "[[deprecated]]\n"
28431 "Bar::~Bar()\n"
28432 "{\n"
28433 "}\n"
28434 "[[deprecated]]\n"
28435 "void g()\n"
28436 "{\n"
28437 "}",
28438 CtorDtorCode, Style);
28439
28440 verifyFormat("struct Foo {\n"
28441 " [[maybe_unused]]\n"
28442 " void operator+();\n"
28443 "};\n"
28444 "[[nodiscard]]\n"
28445 "Foo &operator-(Foo &);",
28446 Style);
28447
28448 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left;
28449 verifyFormat("[[nodiscard]]\n"
28450 "Foo& operator-(Foo&);",
28451 Style);
28452}
28453
28454TEST_F(FormatTest, InsertNewlineAtEOF) {
28455 FormatStyle Style = getLLVMStyle();
28456 Style.InsertNewlineAtEOF = true;
28457
28458 verifyNoChange("int i;\n", Style);
28459 verifyFormat("int i;\n", "int i;", Style);
28460
28461 constexpr StringRef Code{"namespace {\n"
28462 "int i;\n"
28463 "} // namespace"};
28464 verifyFormat(Code.str() + '\n', Code, Style,
28465 {tooling::Range(19, 13)}); // line 3
28466}
28467
28468TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
28469 FormatStyle Style = getLLVMStyle();
28470 Style.KeepEmptyLines.AtEndOfFile = true;
28471
28472 const StringRef Code{"int i;\n\n"};
28473 verifyNoChange(Code, Style);
28474 verifyFormat(Code, "int i;\n\n\n", Style);
28475}
28476
28477TEST_F(FormatTest, SpaceAfterUDL) {
28478 verifyFormat("auto c = (4s).count();");
28479 verifyFormat("auto x = 5s .count() == 5;");
28480}
28481
28482TEST_F(FormatTest, InterfaceAsClassMemberName) {
28483 verifyFormat("class Foo {\n"
28484 " int interface;\n"
28485 " Foo::Foo(int iface) : interface{iface} {}\n"
28486 "}");
28487}
28488
28489TEST_F(FormatTest, PreprocessorOverlappingRegions) {
28490 verifyFormat("#ifdef\n\n"
28491 "#else\n"
28492 "#endif",
28493 "#ifdef \n"
28494 " \n"
28495 "\n"
28496 "#else \n"
28497 "#endif ",
28498 getGoogleStyle());
28499}
28500
28501TEST_F(FormatTest, RemoveParentheses) {
28502 FormatStyle Style = getLLVMStyle();
28503 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave);
28504
28505 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses;
28506 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style);
28507 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style);
28508 verifyFormat("decltype((foo->bar)) baz;", Style);
28509 verifyFormat("class __declspec(dllimport) X {};",
28510 "class __declspec((dllimport)) X {};", Style);
28511 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style);
28512 verifyFormat("while (a)\n"
28513 " b;",
28514 "while (((a)))\n"
28515 " b;",
28516 Style);
28517 verifyFormat("while ((a = b))\n"
28518 " c;",
28519 "while (((a = b)))\n"
28520 " c;",
28521 Style);
28522 verifyFormat("if (a)\n"
28523 " b;",
28524 "if (((a)))\n"
28525 " b;",
28526 Style);
28527 verifyFormat("if constexpr ((a = b))\n"
28528 " c;",
28529 "if constexpr (((a = b)))\n"
28530 " c;",
28531 Style);
28532 verifyFormat("if (({ a; }))\n"
28533 " b;",
28534 "if ((({ a; })))\n"
28535 " b;",
28536 Style);
28537 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
28538 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
28539 Style);
28540 verifyFormat("foo((a, b));", "foo(((a, b)));", Style);
28541 verifyFormat("foo((a, b));", "foo(((a), b));", Style);
28542 verifyFormat("foo((a, b));", "foo((a, (b)));", Style);
28543 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style);
28544 verifyFormat("(..., (hash_a = hash_combine(hash_a, hash_b)));",
28545 "(..., ((hash_a = hash_combine(hash_a, hash_b))));", Style);
28546 verifyFormat("((hash_a = hash_combine(hash_a, hash_b)), ...);",
28547 "(((hash_a = hash_combine(hash_a, hash_b))), ...);", Style);
28548 verifyFormat("return (0);", "return (((0)));", Style);
28549 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style);
28550 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
28551 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
28552 Style);
28553 verifyFormat("MOCK_METHOD(void, Function, (), override);",
28554 "MOCK_METHOD(void, Function, (), (override));", Style);
28555
28556 Style.MacrosSkippedByRemoveParentheses.push_back(x: "FOO");
28557 verifyFormat("FOO((a && b));", Style);
28558 verifyFormat("FOO((int), func, ((std::map<int, int>)), (override));", Style);
28559
28560 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement;
28561 verifyFormat("#define Return0 return (0);", Style);
28562 verifyFormat("return 0;", "return (0);", Style);
28563 verifyFormat("co_return 0;", "co_return ((0));", Style);
28564 verifyFormat("return 0;", "return (((0)));", Style);
28565 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style);
28566 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
28567 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
28568 Style);
28569 verifyFormat("inline decltype(auto) f() {\n"
28570 " if (a) {\n"
28571 " return (a);\n"
28572 " }\n"
28573 " return (b);\n"
28574 "}",
28575 "inline decltype(auto) f() {\n"
28576 " if (a) {\n"
28577 " return ((a));\n"
28578 " }\n"
28579 " return ((b));\n"
28580 "}",
28581 Style);
28582 verifyFormat("auto g() {\n"
28583 " decltype(auto) x = [] {\n"
28584 " auto y = [] {\n"
28585 " if (a) {\n"
28586 " return a;\n"
28587 " }\n"
28588 " return b;\n"
28589 " };\n"
28590 " if (c) {\n"
28591 " return (c);\n"
28592 " }\n"
28593 " return (d);\n"
28594 " };\n"
28595 " if (e) {\n"
28596 " return e;\n"
28597 " }\n"
28598 " return f;\n"
28599 "}",
28600 "auto g() {\n"
28601 " decltype(auto) x = [] {\n"
28602 " auto y = [] {\n"
28603 " if (a) {\n"
28604 " return ((a));\n"
28605 " }\n"
28606 " return ((b));\n"
28607 " };\n"
28608 " if (c) {\n"
28609 " return ((c));\n"
28610 " }\n"
28611 " return ((d));\n"
28612 " };\n"
28613 " if (e) {\n"
28614 " return ((e));\n"
28615 " }\n"
28616 " return ((f));\n"
28617 "}",
28618 Style);
28619
28620 Style.ColumnLimit = 25;
28621 verifyFormat("return (a + b) - (c + d);",
28622 "return (((a + b)) -\n"
28623 " ((c + d)));",
28624 Style);
28625}
28626
28627TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
28628 auto Style = getLLVMStyleWithColumns(ColumnLimit: 35);
28629
28630 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never);
28631 verifyFormat("void foo(int arg1,\n"
28632 " double arg2) noexcept;",
28633 Style);
28634
28635 // The following line does not fit within the 35 column limit, but that's what
28636 // happens with no break allowed.
28637 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
28638 " noexcept(baz(arg1)) &&\n"
28639 " noexcept(baz(arg2)));",
28640 Style);
28641
28642 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
28643 Style);
28644
28645 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always;
28646 verifyFormat("void foo(int arg1,\n"
28647 " double arg2) noexcept;",
28648 Style);
28649
28650 verifyFormat("void bar(int arg1, double arg2)\n"
28651 " noexcept(noexcept(baz(arg1)) &&\n"
28652 " noexcept(baz(arg2)));",
28653 Style);
28654
28655 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
28656 " noexcept;",
28657 Style);
28658
28659 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen;
28660 verifyFormat("void foo(int arg1,\n"
28661 " double arg2) noexcept;",
28662 Style);
28663
28664 verifyFormat("void bar(int arg1, double arg2)\n"
28665 " noexcept(noexcept(baz(arg1)) &&\n"
28666 " noexcept(baz(arg2)));",
28667 Style);
28668
28669 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
28670 Style);
28671}
28672
28673TEST_F(FormatTest, PPBranchesInBracedInit) {
28674 verifyFormat("A a_{kFlag1,\n"
28675 "#if BUILD_FLAG\n"
28676 " kFlag2,\n"
28677 "#else\n"
28678 " kFlag3,\n"
28679 "#endif\n"
28680 " kFlag4};",
28681 "A a_{\n"
28682 " kFlag1,\n"
28683 "#if BUILD_FLAG\n"
28684 " kFlag2,\n"
28685 "#else\n"
28686 " kFlag3,\n"
28687 "#endif\n"
28688 " kFlag4\n"
28689 "};");
28690}
28691
28692TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) {
28693 verifyFormat("{\n"
28694 " char *a[] = {\n"
28695 " /* abc */ \"abc\",\n"
28696 "#if FOO\n"
28697 " /* xyz */ \"xyz\",\n"
28698 "#endif\n"
28699 " /* last */ \"last\"};\n"
28700 "}",
28701 getLLVMStyleWithColumns(30));
28702}
28703
28704TEST_F(FormatTest, BreakAdjacentStringLiterals) {
28705 constexpr StringRef Code{
28706 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
28707
28708 verifyFormat("return \"Code\"\n"
28709 " \"\\0\\52\\26\\55\\55\\0\"\n"
28710 " \"x013\"\n"
28711 " \"\\02\\xBA\";",
28712 Code);
28713
28714 auto Style = getLLVMStyle();
28715 Style.BreakAdjacentStringLiterals = false;
28716 verifyFormat(Code, Style);
28717}
28718
28719TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
28720 verifyFormat(
28721 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
28722 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
28723 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
28724
28725 auto Style = getLLVMStyle();
28726 Style.ColumnLimit = 15;
28727 verifyNoChange("#define test \\\n"
28728 " /* 测试 */ \\\n"
28729 " \"aa\" \\\n"
28730 " \"bb\"",
28731 Style);
28732
28733 Style.ColumnLimit = 25;
28734 verifyFormat("struct foo {\n"
28735 " int iiiiii; ///< iiiiii\n"
28736 " int b; ///< ыыы\n"
28737 " int c; ///< ыыыы\n"
28738 "};",
28739 Style);
28740
28741 Style.ColumnLimit = 35;
28742 verifyFormat("#define SENSOR_DESC_1 \\\n"
28743 " \"{\" \\\n"
28744 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
28745 " \"}\"",
28746 Style);
28747
28748 Style.ColumnLimit = 80;
28749 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left;
28750 verifyFormat("Languages languages = {\n"
28751 " Language{{'e', 'n'}, U\"Test English\" },\n"
28752 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
28753 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
28754 "};",
28755 Style);
28756}
28757
28758TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) {
28759 verifyFormat("return .5;");
28760 verifyFormat("return not '5';");
28761 verifyFormat("return sizeof \"5\";");
28762}
28763
28764TEST_F(FormatTest, BreakBinaryOperations) {
28765 auto Style = getLLVMStyleWithColumns(ColumnLimit: 60);
28766 EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never);
28767
28768 // Logical operations
28769 verifyFormat("if (condition1 && condition2) {\n"
28770 "}",
28771 Style);
28772
28773 verifyFormat("if (condition1 && condition2 &&\n"
28774 " (condition3 || condition4) && condition5 &&\n"
28775 " condition6) {\n"
28776 "}",
28777 Style);
28778
28779 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
28780 " loooooooooooooooooooooongcondition2) {\n"
28781 "}",
28782 Style);
28783
28784 // Arithmetic
28785 verifyFormat("const int result = lhs + rhs;", Style);
28786
28787 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
28788 " loooooooooooooooooooooongop3;",
28789 Style);
28790
28791 verifyFormat("result = longOperand1 + longOperand2 -\n"
28792 " (longOperand3 + longOperand4) -\n"
28793 " longOperand5 * longOperand6;",
28794 Style);
28795
28796 verifyFormat("const int result =\n"
28797 " operand1 + operand2 - (operand3 + operand4);",
28798 Style);
28799
28800 // Check operator>> special case.
28801 verifyFormat("std::cin >> longOperand_1 >> longOperand_2 >>\n"
28802 " longOperand_3_;",
28803 Style);
28804
28805 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28806
28807 // Logical operations
28808 verifyFormat("if (condition1 && condition2) {\n"
28809 "}",
28810 Style);
28811
28812 verifyFormat("if (condition1 && // comment\n"
28813 " condition2 &&\n"
28814 " (condition3 || condition4) && // comment\n"
28815 " condition5 &&\n"
28816 " condition6) {\n"
28817 "}",
28818 Style);
28819
28820 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
28821 " loooooooooooooooooooooongcondition2) {\n"
28822 "}",
28823 Style);
28824
28825 // Arithmetic
28826 verifyFormat("const int result = lhs + rhs;", Style);
28827
28828 verifyFormat("result = loooooooooooooooooooooongop1 +\n"
28829 " loooooooooooooooooooooongop2 +\n"
28830 " loooooooooooooooooooooongop3;",
28831 Style);
28832
28833 verifyFormat("const int result =\n"
28834 " operand1 + operand2 - (operand3 + operand4);",
28835 Style);
28836
28837 verifyFormat("result = longOperand1 +\n"
28838 " longOperand2 -\n"
28839 " (longOperand3 + longOperand4) -\n"
28840 " longOperand5 +\n"
28841 " longOperand6;",
28842 Style);
28843
28844 verifyFormat("result = operand1 +\n"
28845 " operand2 -\n"
28846 " operand3 +\n"
28847 " operand4 -\n"
28848 " operand5 +\n"
28849 " operand6;",
28850 Style);
28851
28852 // Ensure mixed precedence operations are handled properly
28853 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28854
28855 verifyFormat("result = operand1 +\n"
28856 " operand2 /\n"
28857 " operand3 +\n"
28858 " operand4 /\n"
28859 " operand5 *\n"
28860 " operand6;",
28861 Style);
28862
28863 verifyFormat("result = operand1 *\n"
28864 " operand2 -\n"
28865 " operand3 *\n"
28866 " operand4 -\n"
28867 " operand5 +\n"
28868 " operand6;",
28869 Style);
28870
28871 verifyFormat("result = operand1 *\n"
28872 " (operand2 - operand3 * operand4) -\n"
28873 " operand5 +\n"
28874 " operand6;",
28875 Style);
28876
28877 verifyFormat("result = operand1.member *\n"
28878 " (operand2.member() - operand3->mem * operand4) -\n"
28879 " operand5.member() +\n"
28880 " operand6->member;",
28881 Style);
28882
28883 // Check operator>> special case.
28884 verifyFormat("std::cin >>\n"
28885 " longOperand_1 >>\n"
28886 " longOperand_2 >>\n"
28887 " longOperand_3_;",
28888 Style);
28889
28890 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
28891 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28892
28893 verifyFormat("result = operand1 +\n"
28894 " operand2 / operand3 +\n"
28895 " operand4 / operand5 * operand6;",
28896 Style);
28897
28898 verifyFormat("result = operand1 * operand2 -\n"
28899 " operand3 * operand4 -\n"
28900 " operand5 +\n"
28901 " operand6;",
28902 Style);
28903
28904 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28905 " operand5 +\n"
28906 " operand6;",
28907 Style);
28908
28909 verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28910 " byte_buffer[1] << 8 |\n"
28911 " byte_buffer[2] << 16 |\n"
28912 " byte_buffer[3] << 24;",
28913 Style);
28914
28915 // Check operator>> special case.
28916 verifyFormat("std::cin >>\n"
28917 " longOperand_1 >>\n"
28918 " longOperand_2 >>\n"
28919 " longOperand_3_;",
28920 Style);
28921
28922 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
28923 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
28924
28925 // Logical operations
28926 verifyFormat("if (condition1 && condition2) {\n"
28927 "}",
28928 Style);
28929
28930 verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28931 " && loooooooooooooooooooooongcondition2) {\n"
28932 "}",
28933 Style);
28934
28935 // Arithmetic
28936 verifyFormat("const int result = lhs + rhs;", Style);
28937
28938 verifyFormat("result = loooooooooooooooooooooongop1\n"
28939 " + loooooooooooooooooooooongop2\n"
28940 " + loooooooooooooooooooooongop3;",
28941 Style);
28942
28943 verifyFormat("const int result =\n"
28944 " operand1 + operand2 - (operand3 + operand4);",
28945 Style);
28946
28947 verifyFormat("result = longOperand1\n"
28948 " + longOperand2\n"
28949 " - (longOperand3 + longOperand4)\n"
28950 " - longOperand5\n"
28951 " + longOperand6;",
28952 Style);
28953
28954 verifyFormat("result = operand1\n"
28955 " + operand2\n"
28956 " - operand3\n"
28957 " + operand4\n"
28958 " - operand5\n"
28959 " + operand6;",
28960 Style);
28961
28962 // Ensure mixed precedence operations are handled properly
28963 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
28964
28965 verifyFormat("result = operand1\n"
28966 " + operand2\n"
28967 " / operand3\n"
28968 " + operand4\n"
28969 " / operand5\n"
28970 " * operand6;",
28971 Style);
28972
28973 verifyFormat("result = operand1\n"
28974 " * operand2\n"
28975 " - operand3\n"
28976 " * operand4\n"
28977 " - operand5\n"
28978 " + operand6;",
28979 Style);
28980
28981 verifyFormat("result = operand1\n"
28982 " * (operand2 - operand3 * operand4)\n"
28983 " - operand5\n"
28984 " + operand6;",
28985 Style);
28986
28987 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28988 " | byte_buffer[1]\n"
28989 " << 8\n"
28990 " | byte_buffer[2]\n"
28991 " << 16\n"
28992 " | byte_buffer[3]\n"
28993 " << 24;",
28994 Style);
28995
28996 // Check operator>> special case.
28997 verifyFormat("std::cin\n"
28998 " >> longOperand_1\n"
28999 " >> longOperand_2\n"
29000 " >> longOperand_3_;",
29001 Style);
29002
29003 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence;
29004 verifyFormat("result = op1 + op2 * op3 - op4;", Style);
29005
29006 verifyFormat("result = operand1\n"
29007 " + operand2 / operand3\n"
29008 " + operand4 / operand5 * operand6;",
29009 Style);
29010
29011 verifyFormat("result = operand1 * operand2\n"
29012 " - operand3 * operand4\n"
29013 " - operand5\n"
29014 " + operand6;",
29015 Style);
29016
29017 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
29018 " - operand5\n"
29019 " + operand6;",
29020 Style);
29021
29022 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
29023 " | byte_buffer[1] << 8\n"
29024 " | byte_buffer[2] << 16\n"
29025 " | byte_buffer[3] << 24;",
29026 Style);
29027
29028 // Check operator>> special case.
29029 verifyFormat("std::cin\n"
29030 " >> longOperand_1\n"
29031 " >> longOperand_2\n"
29032 " >> longOperand_3_;",
29033 Style);
29034}
29035
29036TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {
29037 auto Style = getLLVMStyle();
29038 Style.RemoveEmptyLinesInUnwrappedLines = true;
29039
29040 verifyFormat("int c = a + b;",
29041 "int c\n"
29042 "\n"
29043 " = a + b;",
29044 Style);
29045
29046 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
29047 "enum : unsigned\n"
29048 "\n"
29049 "{\n"
29050 " AA = 0,\n"
29051 " BB\n"
29052 "} myEnum;",
29053 Style);
29054
29055 verifyFormat("class B : public E {\n"
29056 "private:\n"
29057 "};",
29058 "class B : public E\n"
29059 "\n"
29060 "{\n"
29061 "private:\n"
29062 "};",
29063 Style);
29064
29065 verifyFormat(
29066 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
29067 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
29068 "\n"
29069 " 23, \"hello\"},\n"
29070 " {7, 5, \"!!\"}};",
29071 Style);
29072
29073 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
29074 "int myFunction(\n"
29075 "\n"
29076 " int aaaaaaaaaaaaa,\n"
29077 "\n"
29078 " int ccccccccccccc, int d);",
29079 Style);
29080
29081 verifyFormat("switch (e) {\n"
29082 "case 1:\n"
29083 " return e;\n"
29084 "case 2:\n"
29085 " return 2;\n"
29086 "}",
29087 "switch (\n"
29088 "\n"
29089 " e) {\n"
29090 "case 1:\n"
29091 " return e;\n"
29092 "case 2:\n"
29093 " return 2;\n"
29094 "}",
29095 Style);
29096
29097 verifyFormat("while (true) {\n"
29098 "}",
29099 "while (\n"
29100 "\n"
29101 " true) {\n"
29102 "}",
29103 Style);
29104
29105 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
29106 " std::map<int, std::string> *outputMap);",
29107 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
29108 "\n"
29109 " (std::map<int, std::string> *outputMap);",
29110 Style);
29111}
29112
29113TEST_F(FormatTest, KeepFormFeed) {
29114 auto Style = getLLVMStyle();
29115 Style.KeepFormFeed = true;
29116
29117 constexpr StringRef NoFormFeed{"int i;\n"
29118 "\n"
29119 "void f();"};
29120 verifyFormat(NoFormFeed,
29121 "int i;\n"
29122 " \f\n"
29123 "void f();",
29124 Style);
29125 verifyFormat(NoFormFeed,
29126 "int i;\n"
29127 "\n"
29128 "\fvoid f();",
29129 Style);
29130 verifyFormat(NoFormFeed,
29131 "\fint i;\n"
29132 "\n"
29133 "void f();",
29134 Style);
29135 verifyFormat(NoFormFeed,
29136 "int i;\n"
29137 "\n"
29138 "void f();\f",
29139 Style);
29140
29141 constexpr StringRef FormFeed{"int i;\n"
29142 "\f\n"
29143 "void f();"};
29144 verifyNoChange(FormFeed, Style);
29145
29146 Style.LineEnding = FormatStyle::LE_LF;
29147 verifyFormat(FormFeed,
29148 "int i;\r\n"
29149 "\f\r\n"
29150 "void f();",
29151 Style);
29152
29153 constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n"
29154 "\f\n"
29155 "\n"
29156 "void f();"};
29157 Style.MaxEmptyLinesToKeep = 2;
29158 verifyFormat(FormFeedBeforeEmptyLine,
29159 "int i;\n"
29160 "\n"
29161 "\f\n"
29162 "void f();",
29163 Style);
29164 verifyFormat(FormFeedBeforeEmptyLine,
29165 "int i;\n"
29166 "\f\n"
29167 "\f\n"
29168 "void f();",
29169 Style);
29170}
29171
29172TEST_F(FormatTest, ShortNamespacesOption) {
29173 auto Style = getLLVMStyle();
29174 Style.AllowShortNamespacesOnASingleLine = true;
29175 Style.CompactNamespaces = true;
29176 Style.FixNamespaceComments = false;
29177
29178 // Basic functionality.
29179 verifyFormat("namespace foo { class bar; }", Style);
29180 verifyFormat("namespace foo::bar { class baz; }", Style);
29181 verifyFormat("namespace { class bar; }", Style);
29182 verifyFormat("namespace foo {\n"
29183 "class bar;\n"
29184 "class baz;\n"
29185 "}",
29186 Style);
29187
29188 // Trailing comments prevent merging.
29189 verifyFormat("namespace foo { namespace baz {\n"
29190 "class qux;\n"
29191 "} // comment\n"
29192 "}",
29193 Style);
29194
29195 // Make sure code doesn't walk too far on unbalanced code.
29196 verifyFormat("namespace foo {", Style);
29197 verifyFormat("namespace foo {\n"
29198 "class baz;",
29199 Style);
29200 verifyFormat("namespace foo {\n"
29201 "namespace bar { class baz; }",
29202 Style);
29203
29204 // Nested namespaces.
29205 verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
29206
29207 // Without CompactNamespaces, we won't merge consecutive namespace
29208 // declarations.
29209 Style.CompactNamespaces = false;
29210 verifyFormat("namespace foo {\n"
29211 "namespace bar { class baz; }\n"
29212 "}",
29213 Style);
29214
29215 verifyFormat("namespace foo {\n"
29216 "namespace bar { class baz; }\n"
29217 "namespace qux { class quux; }\n"
29218 "}",
29219 Style);
29220
29221 Style.CompactNamespaces = true;
29222
29223 // Varying inner content.
29224 verifyFormat("namespace foo {\n"
29225 "int f() { return 5; }\n"
29226 "}",
29227 Style);
29228 verifyFormat("namespace foo { template <T> struct bar; }", Style);
29229 verifyFormat("namespace foo { constexpr int num = 42; }", Style);
29230
29231 // Validate nested namespace wrapping scenarios around the ColumnLimit.
29232 Style.ColumnLimit = 64;
29233
29234 // Validate just under the ColumnLimit.
29235 verifyFormat(
29236 "namespace foo { namespace bar { namespace baz { class qux; } } }",
29237 Style);
29238
29239 // Validate just over the ColumnLimit.
29240 verifyFormat("namespace foo { namespace baar { namespace baaz {\n"
29241 "class quux;\n"
29242 "}}}",
29243 Style);
29244
29245 verifyFormat(
29246 "namespace foo { namespace bar { namespace baz { namespace qux {\n"
29247 "class quux;\n"
29248 "}}}}",
29249 Style);
29250
29251 // Validate that the ColumnLimit logic accounts for trailing content as well.
29252 verifyFormat("namespace foo { namespace bar { class qux; } } // extra",
29253 Style);
29254
29255 verifyFormat("namespace foo { namespace bar { namespace baz {\n"
29256 "class qux;\n"
29257 "}}} // extra",
29258 Style);
29259
29260 // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing
29261 // namespace comment from 'FixNamespaceComments', as it's not really necessary
29262 // in this scenario, but the two options work at very different layers of the
29263 // formatter, so I'm not sure how to make them interact.
29264 //
29265 // As it stands, the trailing comment will be added and likely make the line
29266 // too long to fit within the ColumnLimit, reducing the how likely the line
29267 // will still fit on a single line. The recommendation for now is to use the
29268 // concatenated namespace syntax instead. e.g. 'namespace foo::bar'
29269 Style.FixNamespaceComments = true;
29270 verifyFormat(
29271 "namespace foo { namespace bar { namespace baz {\n"
29272 "class qux;\n"
29273 "}}} // namespace foo::bar::baz",
29274 "namespace foo { namespace bar { namespace baz { class qux; } } }",
29275 Style);
29276 Style.FixNamespaceComments = false;
29277
29278 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
29279 Style.BraceWrapping.AfterNamespace = true;
29280 verifyFormat("namespace foo { class bar; }", Style);
29281 verifyFormat("namespace foo { namespace bar { class baz; } }", Style);
29282 verifyFormat("namespace foo\n"
29283 "{ // comment\n"
29284 "class bar;\n"
29285 "}",
29286 Style);
29287 verifyFormat("namespace foo { class bar; }",
29288 "namespace foo {\n"
29289 "class bar;\n"
29290 "}",
29291 Style);
29292 verifyFormat("namespace foo\n"
29293 "{\n"
29294 "namespace bar\n"
29295 "{ // comment\n"
29296 "class baz;\n"
29297 "}\n"
29298 "}",
29299 Style);
29300 verifyFormat("namespace foo // comment\n"
29301 "{\n"
29302 "class baz;\n"
29303 "}",
29304 Style);
29305}
29306
29307TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
29308 auto Style = getLLVMStyle();
29309 Style.FixNamespaceComments = false;
29310 Style.MaxEmptyLinesToKeep = 2;
29311 Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never;
29312
29313 // Empty namespace.
29314 verifyFormat("namespace N {}", Style);
29315
29316 // Single namespace.
29317 verifyFormat("namespace N {\n"
29318 "int f1(int a) { return 2 * a; }\n"
29319 "}",
29320 "namespace N {\n"
29321 "\n"
29322 "\n"
29323 "int f1(int a) { return 2 * a; }\n"
29324 "\n"
29325 "\n"
29326 "}",
29327 Style);
29328
29329 // Nested namespace.
29330 verifyFormat("namespace N1 {\n"
29331 "namespace N2 {\n"
29332 "int a = 1;\n"
29333 "}\n"
29334 "}",
29335 "namespace N1 {\n"
29336 "\n"
29337 "\n"
29338 "namespace N2 {\n"
29339 "\n"
29340 "int a = 1;\n"
29341 "\n"
29342 "}\n"
29343 "\n"
29344 "\n"
29345 "}",
29346 Style);
29347
29348 Style.CompactNamespaces = true;
29349
29350 verifyFormat("namespace N1 { namespace N2 {\n"
29351 "int a = 1;\n"
29352 "}}",
29353 "namespace N1 { namespace N2 {\n"
29354 "\n"
29355 "\n"
29356 "int a = 1;\n"
29357 "\n"
29358 "\n"
29359 "}}",
29360 Style);
29361}
29362
29363TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
29364 auto Style = getLLVMStyle();
29365 Style.FixNamespaceComments = false;
29366 Style.MaxEmptyLinesToKeep = 2;
29367 Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always;
29368
29369 // Empty namespace.
29370 verifyFormat("namespace N {}", Style);
29371
29372 // Single namespace.
29373 verifyFormat("namespace N {\n"
29374 "\n"
29375 "int f1(int a) { return 2 * a; }\n"
29376 "\n"
29377 "}",
29378 "namespace N {\n"
29379 "int f1(int a) { return 2 * a; }\n"
29380 "}",
29381 Style);
29382
29383 // Nested namespace.
29384 verifyFormat("namespace N1 {\n"
29385 "namespace N2 {\n"
29386 "\n"
29387 "int a = 1;\n"
29388 "\n"
29389 "}\n"
29390 "}",
29391 "namespace N1 {\n"
29392 "namespace N2 {\n"
29393 "int a = 1;\n"
29394 "}\n"
29395 "}",
29396 Style);
29397
29398 verifyFormat("namespace N1 {\n"
29399 "\n"
29400 "namespace N2 {\n"
29401 "\n"
29402 "\n"
29403 "int a = 1;\n"
29404 "\n"
29405 "\n"
29406 "}\n"
29407 "\n"
29408 "}",
29409 "namespace N1 {\n"
29410 "\n"
29411 "namespace N2 {\n"
29412 "\n"
29413 "\n"
29414 "\n"
29415 "int a = 1;\n"
29416 "\n"
29417 "\n"
29418 "\n"
29419 "}\n"
29420 "\n"
29421 "}",
29422 Style);
29423
29424 Style.CompactNamespaces = true;
29425
29426 verifyFormat("namespace N1 { namespace N2 {\n"
29427 "\n"
29428 "int a = 1;\n"
29429 "\n"
29430 "}}",
29431 "namespace N1 { namespace N2 {\n"
29432 "int a = 1;\n"
29433 "}}",
29434 Style);
29435}
29436
29437TEST_F(FormatTest, BreakBeforeClassName) {
29438 verifyFormat("class ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_NULLABILITY_COMPATIBLE\n"
29439 " ArenaSafeUniquePtr {};");
29440}
29441
29442} // namespace
29443} // namespace test
29444} // namespace format
29445} // namespace clang
29446

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