1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test"
18 class FormatTest
: public test::FormatTestBase
{};
20 TEST_F(FormatTest
, MessUp
) {
21 EXPECT_EQ("1 2 3", messUp("1 2 3"));
22 EXPECT_EQ("1 2 3", messUp("1\n2\n3"));
23 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
24 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
25 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
28 TEST_F(FormatTest
, DefaultLLVMStyleIsCpp
) {
29 EXPECT_EQ(FormatStyle::LK_Cpp
, getLLVMStyle().Language
);
32 TEST_F(FormatTest
, LLVMStyleOverride
) {
33 EXPECT_EQ(FormatStyle::LK_Proto
,
34 getLLVMStyle(FormatStyle::LK_Proto
).Language
);
37 //===----------------------------------------------------------------------===//
38 // Basic function tests.
39 //===----------------------------------------------------------------------===//
41 TEST_F(FormatTest
, DoesNotChangeCorrectlyFormattedCode
) { verifyFormat(";"); }
43 TEST_F(FormatTest
, FormatsGlobalStatementsAt0
) {
44 verifyFormat("int i;", " int i;");
45 verifyFormat("\nint i;", " \n\t \v \f int i;");
46 verifyFormat("int i;\nint j;", " int i; int j;");
47 verifyFormat("int i;\nint j;", " int i;\n int j;");
49 auto Style
= getLLVMStyle();
50 Style
.KeepEmptyLines
.AtStartOfFile
= false;
51 verifyFormat("int i;", " \n\t \v \f int i;", Style
);
54 TEST_F(FormatTest
, FormatsUnwrappedLinesAtFirstFormat
) {
55 verifyFormat("int i;", "int\ni;");
58 TEST_F(FormatTest
, FormatsNestedBlockStatements
) {
68 TEST_F(FormatTest
, FormatsNestedCall
) {
69 verifyFormat("Method(f1, f2(f3));");
70 verifyFormat("Method(f1(f2, f3()));");
71 verifyFormat("Method(f1(f2, (f3())));");
74 TEST_F(FormatTest
, NestedNameSpecifiers
) {
75 verifyFormat("vector<::Type> v;");
76 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
77 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
78 verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
79 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
80 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
81 verifyFormat("bool a = 2 < ::SomeFunction();");
82 verifyFormat("ALWAYS_INLINE ::std::string getName();");
83 verifyFormat("some::string getName();");
86 TEST_F(FormatTest
, OnlyGeneratesNecessaryReplacements
) {
87 verifyFormat("if (a) {\n"
91 EXPECT_EQ(4, ReplacementCount
);
92 verifyNoChange("if (a) {\n"
95 EXPECT_EQ(0, ReplacementCount
);
96 verifyNoChange("/*\r\n"
99 EXPECT_EQ(0, ReplacementCount
);
102 TEST_F(FormatTest
, RemovesEmptyLines
) {
103 verifyFormat("class C {\n"
111 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
112 verifyFormat("namespace N {\n"
121 verifyFormat("/* something */ namespace N {\n"
125 "/* something */ namespace N {\n"
130 verifyFormat("inline namespace N {\n"
134 "inline namespace N {\n"
139 verifyFormat("/* something */ inline namespace N {\n"
143 "/* something */ inline namespace N {\n"
148 verifyFormat("export namespace N {\n"
152 "export namespace N {\n"
157 verifyFormat("extern /**/ \"C\" /**/ {\n"
161 "extern /**/ \"C\" /**/ {\n"
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"
183 verifyFormat("/* something */ namespace N\n"
188 "/* something */ namespace N {\n"
194 verifyFormat("inline namespace N\n"
199 "inline namespace N\n"
206 verifyFormat("/* something */ inline namespace N\n"
211 "/* something */ inline namespace N\n"
217 verifyFormat("export namespace N\n"
222 "export namespace N\n"
228 verifyFormat("namespace a\n"
249 verifyFormat("namespace A /* comment */\n"
253 "namespace A /* comment */ { class B {} }", CustomStyle
);
254 verifyFormat("namespace A\n"
258 "namespace A {/* comment */ class B {} }", CustomStyle
);
259 verifyFormat("namespace A\n"
266 "namespace A { /* comment */\n"
274 verifyFormat("namespace A /* comment */\n"
280 "namespace A/* comment */ {\n"
289 // ...but do keep inlining and removing empty lines for non-block extern "C"
291 verifyGoogleFormat("extern \"C\" int f() { return 42; }");
292 verifyFormat("extern \"C\" int f() {\n"
296 "extern \"C\" int f() {\n"
303 // Remove empty lines at the beginning and end of blocks.
304 verifyFormat("void f() {\n"
320 verifyFormat("void f() {\n"
336 // Don't remove empty lines in more complex control statements.
337 verifyFormat("void f() {\n"
356 // Don't remove empty lines before namespace endings.
357 FormatStyle LLVMWithNoNamespaceFix
= getLLVMStyle();
358 LLVMWithNoNamespaceFix
.FixNamespaceComments
= false;
359 verifyNoChange("namespace {\n"
363 LLVMWithNoNamespaceFix
);
364 verifyFormat("namespace {\n"
367 LLVMWithNoNamespaceFix
);
368 verifyNoChange("namespace {\n"
372 LLVMWithNoNamespaceFix
);
373 verifyFormat("namespace {\n"
376 LLVMWithNoNamespaceFix
);
377 verifyNoChange("namespace {\n"
381 verifyFormat("namespace {\n"
390 FormatStyle Style
= getLLVMStyle();
391 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
392 Style
.MaxEmptyLinesToKeep
= 2;
393 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
394 Style
.BraceWrapping
.AfterClass
= true;
395 Style
.BraceWrapping
.AfterFunction
= true;
396 Style
.KeepEmptyLines
.AtStartOfBlock
= false;
398 verifyFormat("class Foo\n"
415 TEST_F(FormatTest
, RecognizesBinaryOperatorKeywords
) {
416 verifyFormat("x = (a) and (b);");
417 verifyFormat("x = (a) or (b);");
418 verifyFormat("x = (a) bitand (b);");
419 verifyFormat("x = (a) bitor (b);");
420 verifyFormat("x = (a) not_eq (b);");
421 verifyFormat("x = (a) and_eq (b);");
422 verifyFormat("x = (a) or_eq (b);");
423 verifyFormat("x = (a) xor (b);");
426 TEST_F(FormatTest
, RecognizesUnaryOperatorKeywords
) {
427 verifyFormat("x = compl(a);");
428 verifyFormat("x = not(a);");
429 verifyFormat("x = bitand(a);");
430 // Unary operator must not be merged with the next identifier
431 verifyFormat("x = compl a;");
432 verifyFormat("x = not a;");
433 verifyFormat("x = bitand a;");
436 //===----------------------------------------------------------------------===//
437 // Tests for control statements.
438 //===----------------------------------------------------------------------===//
440 TEST_F(FormatTest
, FormatIfWithoutCompoundStatement
) {
441 verifyFormat("if (true)\n f();\ng();");
442 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
443 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
444 verifyFormat("if constexpr (true)\n"
446 verifyFormat("if CONSTEXPR (true)\n"
448 verifyFormat("if constexpr (a)\n"
449 " if constexpr (b)\n"
450 " if constexpr (c)\n"
453 verifyFormat("if CONSTEXPR (a)\n"
454 " if CONSTEXPR (b)\n"
455 " if CONSTEXPR (c)\n"
458 verifyFormat("if constexpr (a)\n"
459 " if constexpr (b) {\n"
463 verifyFormat("if CONSTEXPR (a)\n"
464 " if CONSTEXPR (b) {\n"
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"
477 verifyFormat("if !consteval {\n"
480 verifyFormat("if consteval {\n"
485 verifyFormat("if CONSTEVAL {\n"
488 verifyFormat("if !CONSTEVAL {\n"
492 verifyFormat("if (a)\n"
494 verifyFormat("if (a) {\n"
497 verifyFormat("if (a)\n"
501 verifyFormat("if (a) {\n"
505 verifyFormat("if (a)\n"
510 verifyFormat("if (a) {\n"
515 verifyFormat("if (a)\n"
521 verifyFormat("if (a) {\n"
527 verifyFormat("if (a)\n"
533 verifyFormat("if (a)\n"
540 verifyFormat("if (a)\n"
547 verifyFormat("if (a) {\n"
555 FormatStyle AllowsMergedIf
= getLLVMStyle();
556 AllowsMergedIf
.IfMacros
.push_back("MYIF");
557 AllowsMergedIf
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
558 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
559 FormatStyle::SIS_WithoutElse
;
560 verifyFormat("if (a)\n"
570 verifyFormat("#define A \\\n"
575 verifyFormat("if (a)\n"
578 verifyFormat("if (a)\n"
582 verifyFormat("if (a) // Can't merge this\n"
585 verifyFormat("if (a) /* still don't merge */\n"
588 verifyFormat("if (a) { // Never merge this\n"
592 verifyFormat("if (a) { /* Never merge this */\n"
596 verifyFormat("MYIF (a)\n"
606 verifyFormat("#define A \\\n"
611 verifyFormat("MYIF (a)\n"
614 verifyFormat("MYIF (a)\n"
618 verifyFormat("MYIF (a) // Can't merge this\n"
621 verifyFormat("MYIF (a) /* still don't merge */\n"
624 verifyFormat("MYIF (a) { // Never merge this\n"
628 verifyFormat("MYIF (a) { /* Never merge this */\n"
633 AllowsMergedIf
.ColumnLimit
= 14;
634 // Where line-lengths matter, a 2-letter synonym that maintains line length.
635 // Not IF to avoid any confusion that IF is somehow special.
636 AllowsMergedIf
.IfMacros
.push_back("FI");
637 verifyFormat("if (a) return;", AllowsMergedIf
);
638 verifyFormat("if (aaaaaaaaa)\n"
641 verifyFormat("FI (a) return;", AllowsMergedIf
);
642 verifyFormat("FI (aaaaaaaaa)\n"
646 AllowsMergedIf
.ColumnLimit
= 13;
647 verifyFormat("if (a)\n return;", AllowsMergedIf
);
648 verifyFormat("FI (a)\n return;", AllowsMergedIf
);
650 FormatStyle AllowsMergedIfElse
= getLLVMStyle();
651 AllowsMergedIfElse
.IfMacros
.push_back("MYIF");
652 AllowsMergedIfElse
.AllowShortIfStatementsOnASingleLine
=
653 FormatStyle::SIS_AllIfsAndElse
;
654 verifyFormat("if (a)\n"
670 verifyFormat("if (a)\n"
675 verifyFormat("if (a) {\n"
679 verifyFormat("if (a) return;\n"
680 "else if (b) return;\n"
683 verifyFormat("if (a) {\n"
686 verifyFormat("if (a) {\n"
687 "} else if (b) return;\n"
690 verifyFormat("if (a) return;\n"
694 verifyFormat("if (a)\n"
698 verifyFormat("if constexpr (a)\n"
699 " if constexpr (b) return;\n"
700 " else if constexpr (c) return;\n"
703 verifyFormat("MYIF (a)\n"
719 verifyFormat("MYIF (a)\n"
724 verifyFormat("MYIF (a) {\n"
728 verifyFormat("MYIF (a) return;\n"
729 "else MYIF (b) return;\n"
732 verifyFormat("MYIF (a) {\n"
735 verifyFormat("MYIF (a) {\n"
736 "} else MYIF (b) return;\n"
739 verifyFormat("MYIF (a) return;\n"
743 verifyFormat("MYIF (a)\n"
744 " MYIF (b) return;\n"
747 verifyFormat("MYIF constexpr (a)\n"
748 " MYIF constexpr (b) return;\n"
749 " else MYIF constexpr (c) return;\n"
754 TEST_F(FormatTest
, FormatIfWithoutCompoundStatementButElseWith
) {
755 FormatStyle AllowsMergedIf
= getLLVMStyle();
756 AllowsMergedIf
.IfMacros
.push_back("MYIF");
757 AllowsMergedIf
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
758 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
759 FormatStyle::SIS_WithoutElse
;
760 verifyFormat("if (a)\n"
766 verifyFormat("if (a)\n"
772 verifyFormat("if (a) g();", AllowsMergedIf
);
773 verifyFormat("if (a) {\n"
777 verifyFormat("if (a)\n"
782 verifyFormat("if (a) {\n"
787 verifyFormat("if (a)\n"
793 verifyFormat("if (a) {\n"
799 verifyFormat("if (a)\n"
806 verifyFormat("if (a) {\n"
813 verifyFormat("if (a)\n"
820 verifyFormat("if (a)\n"
828 verifyFormat("if (a)\n"
836 verifyFormat("if (a) {\n"
844 verifyFormat("MYIF (a)\n"
850 verifyFormat("MYIF (a)\n"
856 verifyFormat("MYIF (a) g();", AllowsMergedIf
);
857 verifyFormat("MYIF (a) {\n"
861 verifyFormat("MYIF (a)\n"
866 verifyFormat("MYIF (a) {\n"
871 verifyFormat("MYIF (a)\n"
877 verifyFormat("MYIF (a) {\n"
883 verifyFormat("MYIF (a)\n"
890 verifyFormat("MYIF (a)\n"
897 verifyFormat("MYIF (a) {\n"
904 verifyFormat("MYIF (a) {\n"
911 verifyFormat("MYIF (a)\n"
918 verifyFormat("MYIF (a)\n"
925 verifyFormat("MYIF (a)\n"
933 verifyFormat("MYIF (a)\n"
941 verifyFormat("MYIF (a)\n"
949 verifyFormat("MYIF (a)\n"
957 verifyFormat("MYIF (a) {\n"
959 "} else MYIF (b) {\n"
965 verifyFormat("MYIF (a) {\n"
974 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
975 FormatStyle::SIS_OnlyFirstIf
;
977 verifyFormat("if (a) f();\n"
982 verifyFormat("if (a) f();\n"
992 verifyFormat("if (a) g();", AllowsMergedIf
);
993 verifyFormat("if (a) {\n"
997 verifyFormat("if (a) g();\n"
1001 verifyFormat("if (a) {\n"
1006 verifyFormat("if (a) g();\n"
1011 verifyFormat("if (a) {\n"
1017 verifyFormat("if (a) g();\n"
1023 verifyFormat("if (a) {\n"
1030 verifyFormat("if (a) g();\n"
1036 verifyFormat("if (a) g();\n"
1043 verifyFormat("if (a) g();\n"
1050 verifyFormat("if (a) {\n"
1058 verifyFormat("MYIF (a) f();\n"
1063 verifyFormat("MYIF (a) f();\n"
1073 verifyFormat("MYIF (a) g();", AllowsMergedIf
);
1074 verifyFormat("MYIF (a) {\n"
1078 verifyFormat("MYIF (a) g();\n"
1082 verifyFormat("MYIF (a) {\n"
1087 verifyFormat("MYIF (a) g();\n"
1092 verifyFormat("MYIF (a) {\n"
1098 verifyFormat("MYIF (a) g();\n"
1104 verifyFormat("MYIF (a) g();\n"
1110 verifyFormat("MYIF (a) {\n"
1117 verifyFormat("MYIF (a) {\n"
1124 verifyFormat("MYIF (a) g();\n"
1130 verifyFormat("MYIF (a) g();\n"
1136 verifyFormat("MYIF (a) g();\n"
1143 verifyFormat("MYIF (a) g();\n"
1150 verifyFormat("MYIF (a) g();\n"
1157 verifyFormat("MYIF (a) g();\n"
1164 verifyFormat("MYIF (a) {\n"
1166 "} else MYIF (b) {\n"
1172 verifyFormat("MYIF (a) {\n"
1181 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
1182 FormatStyle::SIS_AllIfsAndElse
;
1184 verifyFormat("if (a) f();\n"
1189 verifyFormat("if (a) f();\n"
1199 verifyFormat("if (a) g();", AllowsMergedIf
);
1200 verifyFormat("if (a) {\n"
1204 verifyFormat("if (a) g();\n"
1207 verifyFormat("if (a) {\n"
1211 verifyFormat("if (a) g();\n"
1216 verifyFormat("if (a) {\n"
1222 verifyFormat("if (a) g();\n"
1223 "else if (b) g();\n"
1226 verifyFormat("if (a) {\n"
1228 "} else if (b) g();\n"
1231 verifyFormat("if (a) g();\n"
1236 verifyFormat("if (a) g();\n"
1237 "else if (b) g();\n"
1242 verifyFormat("if (a) g();\n"
1249 verifyFormat("if (a) {\n"
1257 verifyFormat("MYIF (a) f();\n"
1262 verifyFormat("MYIF (a) f();\n"
1272 verifyFormat("MYIF (a) g();", AllowsMergedIf
);
1273 verifyFormat("MYIF (a) {\n"
1277 verifyFormat("MYIF (a) g();\n"
1280 verifyFormat("MYIF (a) {\n"
1284 verifyFormat("MYIF (a) g();\n"
1289 verifyFormat("MYIF (a) {\n"
1295 verifyFormat("MYIF (a) g();\n"
1296 "else MYIF (b) g();\n"
1299 verifyFormat("MYIF (a) g();\n"
1300 "else if (b) g();\n"
1303 verifyFormat("MYIF (a) {\n"
1305 "} else MYIF (b) g();\n"
1308 verifyFormat("MYIF (a) {\n"
1310 "} else if (b) g();\n"
1313 verifyFormat("MYIF (a) g();\n"
1318 verifyFormat("MYIF (a) g();\n"
1323 verifyFormat("MYIF (a) g();\n"
1324 "else MYIF (b) g();\n"
1329 verifyFormat("MYIF (a) g();\n"
1330 "else if (b) g();\n"
1335 verifyFormat("MYIF (a) g();\n"
1342 verifyFormat("MYIF (a) g();\n"
1349 verifyFormat("MYIF (a) {\n"
1351 "} else MYIF (b) {\n"
1357 verifyFormat("MYIF (a) {\n"
1367 TEST_F(FormatTest
, FormatLoopsWithoutCompoundStatement
) {
1368 verifyFormat("while (true)\n"
1370 verifyFormat("for (;;)\n"
1373 FormatStyle AllowsMergedLoops
= getLLVMStyle();
1374 AllowsMergedLoops
.AllowShortLoopsOnASingleLine
= true;
1376 verifyFormat("while (true) continue;", AllowsMergedLoops
);
1377 verifyFormat("for (;;) continue;", AllowsMergedLoops
);
1378 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops
);
1379 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops
);
1380 verifyFormat("while (true);", AllowsMergedLoops
);
1381 verifyFormat("for (;;);", AllowsMergedLoops
);
1382 verifyFormat("for (;;)\n"
1383 " for (;;) continue;",
1385 verifyFormat("for (;;)\n"
1386 " while (true) continue;",
1388 verifyFormat("while (true)\n"
1389 " for (;;) continue;",
1391 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1392 " for (;;) continue;",
1394 verifyFormat("for (;;)\n"
1395 " BOOST_FOREACH (int &v, vec) continue;",
1397 verifyFormat("for (;;) // Can't merge this\n"
1400 verifyFormat("for (;;) /* still don't merge */\n"
1403 verifyFormat("do a++;\n"
1406 verifyFormat("do /* Don't merge */\n"
1410 verifyFormat("do // Don't merge\n"
1420 // Without braces labels are interpreted differently.
1429 // Don't merge if there are comments before the null statement.
1430 verifyFormat("while (1) //\n"
1433 verifyFormat("for (;;) /**/\n"
1436 verifyFormat("while (true) /**/\n"
1438 "while (true) /**/;", AllowsMergedLoops
);
1441 TEST_F(FormatTest
, FormatShortBracedStatements
) {
1442 FormatStyle AllowSimpleBracedStatements
= getLLVMStyle();
1443 EXPECT_EQ(AllowSimpleBracedStatements
.AllowShortBlocksOnASingleLine
, false);
1444 EXPECT_EQ(AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
,
1445 FormatStyle::SIS_Never
);
1446 EXPECT_EQ(AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
, false);
1447 EXPECT_EQ(AllowSimpleBracedStatements
.BraceWrapping
.AfterFunction
, false);
1448 verifyFormat("for (;;) {\n"
1451 verifyFormat("/*comment*/ for (;;) {\n"
1454 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1457 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1460 verifyFormat("while (true) {\n"
1463 verifyFormat("/*comment*/ while (true) {\n"
1466 verifyFormat("if (true) {\n"
1469 verifyFormat("/*comment*/ if (true) {\n"
1473 AllowSimpleBracedStatements
.AllowShortBlocksOnASingleLine
=
1474 FormatStyle::SBS_Empty
;
1475 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1476 FormatStyle::SIS_WithoutElse
;
1477 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1478 verifyFormat("if (i) break;", AllowSimpleBracedStatements
);
1479 verifyFormat("if (i > 0) {\n"
1482 AllowSimpleBracedStatements
);
1484 AllowSimpleBracedStatements
.IfMacros
.push_back("MYIF");
1485 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1486 // Not IF to avoid any confusion that IF is somehow special.
1487 AllowSimpleBracedStatements
.IfMacros
.push_back("FI");
1488 AllowSimpleBracedStatements
.ColumnLimit
= 40;
1489 AllowSimpleBracedStatements
.AllowShortBlocksOnASingleLine
=
1490 FormatStyle::SBS_Always
;
1491 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= true;
1492 AllowSimpleBracedStatements
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
1493 AllowSimpleBracedStatements
.BraceWrapping
.AfterFunction
= true;
1494 AllowSimpleBracedStatements
.BraceWrapping
.SplitEmptyRecord
= false;
1496 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1497 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements
);
1498 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1499 verifyFormat("if consteval {}", AllowSimpleBracedStatements
);
1500 verifyFormat("if !consteval {}", AllowSimpleBracedStatements
);
1501 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements
);
1502 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1503 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements
);
1504 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1505 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1506 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1507 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements
);
1508 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1509 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1510 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements
);
1511 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements
);
1512 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements
);
1513 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1514 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1515 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements
);
1516 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements
);
1517 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements
);
1518 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements
);
1519 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1520 AllowSimpleBracedStatements
);
1521 verifyFormat("if (true) {\n"
1522 " ffffffffffffffffffffffff();\n"
1524 AllowSimpleBracedStatements
);
1525 verifyFormat("if (true) {\n"
1526 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1528 AllowSimpleBracedStatements
);
1529 verifyFormat("if (true) { //\n"
1532 AllowSimpleBracedStatements
);
1533 verifyFormat("if (true) {\n"
1537 AllowSimpleBracedStatements
);
1538 verifyFormat("if (true) {\n"
1543 AllowSimpleBracedStatements
);
1544 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1545 AllowSimpleBracedStatements
);
1546 verifyFormat("MYIF (true) {\n"
1547 " ffffffffffffffffffffffff();\n"
1549 AllowSimpleBracedStatements
);
1550 verifyFormat("MYIF (true) {\n"
1551 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1553 AllowSimpleBracedStatements
);
1554 verifyFormat("MYIF (true) { //\n"
1557 AllowSimpleBracedStatements
);
1558 verifyFormat("MYIF (true) {\n"
1562 AllowSimpleBracedStatements
);
1563 verifyFormat("MYIF (true) {\n"
1568 AllowSimpleBracedStatements
);
1570 verifyFormat("struct A2 {\n"
1573 AllowSimpleBracedStatements
);
1574 verifyFormat("typedef struct A2 {\n"
1577 AllowSimpleBracedStatements
);
1578 verifyFormat("template <int> struct A2 {\n"
1581 AllowSimpleBracedStatements
);
1583 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1584 FormatStyle::SIS_Never
;
1585 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1586 verifyFormat("if (true) {\n"
1589 AllowSimpleBracedStatements
);
1590 verifyFormat("if (true) {\n"
1595 AllowSimpleBracedStatements
);
1596 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1597 verifyFormat("MYIF (true) {\n"
1600 AllowSimpleBracedStatements
);
1601 verifyFormat("MYIF (true) {\n"
1606 AllowSimpleBracedStatements
);
1608 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= false;
1609 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1610 verifyFormat("while (true) {\n"
1613 AllowSimpleBracedStatements
);
1614 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1615 verifyFormat("for (;;) {\n"
1618 AllowSimpleBracedStatements
);
1619 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements
);
1620 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1623 AllowSimpleBracedStatements
);
1625 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1626 FormatStyle::SIS_WithoutElse
;
1627 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= true;
1628 AllowSimpleBracedStatements
.BraceWrapping
.AfterControlStatement
=
1629 FormatStyle::BWACS_Always
;
1631 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1632 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements
);
1633 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1634 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1635 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements
);
1636 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1637 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1638 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1639 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements
);
1640 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1641 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1642 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements
);
1643 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1644 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1645 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements
);
1646 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements
);
1647 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1648 AllowSimpleBracedStatements
);
1649 verifyFormat("if (true)\n"
1651 " ffffffffffffffffffffffff();\n"
1653 AllowSimpleBracedStatements
);
1654 verifyFormat("if (true)\n"
1656 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1658 AllowSimpleBracedStatements
);
1659 verifyFormat("if (true)\n"
1663 AllowSimpleBracedStatements
);
1664 verifyFormat("if (true)\n"
1669 AllowSimpleBracedStatements
);
1670 verifyFormat("if (true)\n"
1677 AllowSimpleBracedStatements
);
1678 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1679 AllowSimpleBracedStatements
);
1680 verifyFormat("MYIF (true)\n"
1682 " ffffffffffffffffffffffff();\n"
1684 AllowSimpleBracedStatements
);
1685 verifyFormat("MYIF (true)\n"
1687 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1689 AllowSimpleBracedStatements
);
1690 verifyFormat("MYIF (true)\n"
1694 AllowSimpleBracedStatements
);
1695 verifyFormat("MYIF (true)\n"
1700 AllowSimpleBracedStatements
);
1701 verifyFormat("MYIF (true)\n"
1708 AllowSimpleBracedStatements
);
1710 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1711 FormatStyle::SIS_Never
;
1712 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1713 verifyFormat("if (true)\n"
1717 AllowSimpleBracedStatements
);
1718 verifyFormat("if (true)\n"
1725 AllowSimpleBracedStatements
);
1726 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1727 verifyFormat("MYIF (true)\n"
1731 AllowSimpleBracedStatements
);
1732 verifyFormat("MYIF (true)\n"
1739 AllowSimpleBracedStatements
);
1741 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= false;
1742 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1743 verifyFormat("while (true)\n"
1747 AllowSimpleBracedStatements
);
1748 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1749 verifyFormat("for (;;)\n"
1753 AllowSimpleBracedStatements
);
1754 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements
);
1755 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1759 AllowSimpleBracedStatements
);
1761 FormatStyle Style
= getLLVMStyle();
1762 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
1763 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
1765 verifyFormat("while (i > 0)\n"
1771 verifyFormat("if (a)\n"
1777 verifyFormat("if (a)\n"
1786 verifyFormat("if (a)\n"
1798 Style
.BraceWrapping
.BeforeElse
= true;
1800 verifyFormat("if (a)\n"
1810 verifyFormat("if (a)\n"
1825 TEST_F(FormatTest
, UnderstandsMacros
) {
1826 verifyFormat("#define A (parentheses)");
1827 verifyFormat("/* comment */ #define A (parentheses)");
1828 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1829 // Even the partial code should never be merged.
1830 verifyNoChange("/* comment */ #define A (parentheses)\n"
1832 verifyFormat("/* comment */ #define A (parentheses)\n"
1834 verifyFormat("/* comment */ #define A (parentheses)\n"
1835 "#define B (parentheses)");
1836 verifyFormat("#define true ((int)1)");
1837 verifyFormat("#define and(x)");
1838 verifyFormat("#define if(x) x");
1839 verifyFormat("#define return(x) (x)");
1840 verifyFormat("#define while(x) for (; x;)");
1841 verifyFormat("#define xor(x) (^(x))");
1842 verifyFormat("#define __except(x)");
1843 verifyFormat("#define __try(x)");
1845 // https://llvm.org/PR54348.
1852 FormatStyle Style
= getLLVMStyle();
1853 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
1854 Style
.BraceWrapping
.AfterFunction
= true;
1855 // Test that a macro definition never gets merged with the following
1857 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1858 verifyFormat("#define AAA "
1865 // verifyFormat("#define AAA N { //", Style);
1867 verifyFormat("MACRO(return)");
1868 verifyFormat("MACRO(co_await)");
1869 verifyFormat("MACRO(co_return)");
1870 verifyFormat("MACRO(co_yield)");
1871 verifyFormat("MACRO(return, something)");
1872 verifyFormat("MACRO(co_return, something)");
1873 verifyFormat("MACRO(something##something)");
1874 verifyFormat("MACRO(return##something)");
1875 verifyFormat("MACRO(co_return##something)");
1877 verifyFormat("#define A x:");
1879 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1883 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n"
1887 TEST_F(FormatTest
, ShortBlocksInMacrosDontMergeWithCodeAfterMacro
) {
1888 FormatStyle Style
= getLLVMStyleWithColumns(60);
1889 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
1890 Style
.AllowShortIfStatementsOnASingleLine
= FormatStyle::SIS_WithoutElse
;
1891 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
1892 verifyFormat("#define A \\\n"
1893 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1895 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1899 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1900 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1906 TEST_F(FormatTest
, ParseIfElse
) {
1907 verifyFormat("if (true)\n"
1917 verifyFormat("if (true)\n"
1930 verifyFormat("if (true)\n"
1931 " if constexpr (true)\n"
1933 " if constexpr (true)\n"
1943 verifyFormat("if (true)\n"
1944 " if CONSTEXPR (true)\n"
1946 " if CONSTEXPR (true)\n"
1956 verifyFormat("void f() {\n"
1963 TEST_F(FormatTest
, ElseIf
) {
1964 verifyFormat("if (a) {\n} else if (b) {\n}");
1965 verifyFormat("if (a)\n"
1971 verifyFormat("if (a)\n"
1978 verifyFormat("if constexpr (a)\n"
1980 "else if constexpr (b)\n"
1984 verifyFormat("if CONSTEXPR (a)\n"
1986 "else if CONSTEXPR (b)\n"
1990 verifyFormat("if (a) {\n"
1998 verifyFormat("if (a) {\n"
1999 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2002 verifyFormat("if (a) {\n"
2003 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2006 verifyFormat("if (a) {\n"
2007 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
2010 verifyFormat("if (a) {\n"
2012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2014 getLLVMStyleWithColumns(62));
2015 verifyFormat("if (a) {\n"
2016 "} else if constexpr (\n"
2017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2019 getLLVMStyleWithColumns(62));
2020 verifyFormat("if (a) {\n"
2021 "} else if CONSTEXPR (\n"
2022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2024 getLLVMStyleWithColumns(62));
2027 TEST_F(FormatTest
, SeparatePointerReferenceAlignment
) {
2028 FormatStyle Style
= getLLVMStyle();
2029 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
2030 EXPECT_EQ(Style
.ReferenceAlignment
, FormatStyle::RAS_Pointer
);
2031 verifyFormat("int *f1(int *a, int &b, int &&c);", Style
);
2032 verifyFormat("int &f2(int &&c, int *a, int &b);", Style
);
2033 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style
);
2034 verifyFormat("int *f1(int &a) const &;", Style
);
2035 verifyFormat("int *f1(int &a) const & = 0;", Style
);
2036 verifyFormat("int *a = f1();", Style
);
2037 verifyFormat("int &b = f2();", Style
);
2038 verifyFormat("int &&c = f3();", Style
);
2039 verifyFormat("int f3() { return sizeof(Foo &); }", Style
);
2040 verifyFormat("int f4() { return sizeof(Foo &&); }", Style
);
2041 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style
);
2042 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style
);
2043 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style
);
2044 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style
);
2045 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style
);
2046 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style
);
2047 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style
);
2048 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style
);
2049 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style
);
2050 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style
);
2051 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style
);
2052 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style
);
2053 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style
);
2054 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style
);
2055 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style
);
2056 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style
);
2057 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style
);
2058 verifyFormat("for (f(); int &c : {1, 2, 3})", Style
);
2060 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2061 " res2 = [](int &a) { return 0000000000000; };",
2064 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2065 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= true;
2066 verifyFormat("Const unsigned int *c;\n"
2067 "const unsigned int *d;\n"
2068 "Const unsigned int &e;\n"
2069 "const unsigned int &f;\n"
2070 "int *f1(int *a, int &b, int &&c);\n"
2071 "double *(*f2)(int *a, double &&b);\n"
2072 "const unsigned &&g;\n"
2073 "Const unsigned h;",
2075 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= false;
2076 verifyFormat("Const unsigned int *c;\n"
2077 "const unsigned int *d;\n"
2078 "Const unsigned int &e;\n"
2079 "const unsigned int &f;\n"
2080 "int *f1(int *a, int &b, int &&c);\n"
2081 "double *(*f2)(int *a, double &&b);\n"
2082 "const unsigned &&g;\n"
2083 "Const unsigned h;",
2086 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
2087 Style
.ReferenceAlignment
= FormatStyle::RAS_Pointer
;
2088 verifyFormat("int* f1(int* a, int& b, int&& c);", Style
);
2089 verifyFormat("int& f2(int&& c, int* a, int& b);", Style
);
2090 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style
);
2091 verifyFormat("int* f1(int& a) const& = 0;", Style
);
2092 verifyFormat("int* a = f1();", Style
);
2093 verifyFormat("int& b = f2();", Style
);
2094 verifyFormat("int&& c = f3();", Style
);
2095 verifyFormat("int f3() { return sizeof(Foo&); }", Style
);
2096 verifyFormat("int f4() { return sizeof(Foo&&); }", Style
);
2097 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style
);
2098 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style
);
2099 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style
);
2100 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style
);
2101 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style
);
2102 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style
);
2103 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style
);
2104 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style
);
2105 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style
);
2106 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style
);
2107 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style
);
2108 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style
);
2109 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style
);
2110 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style
);
2111 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style
);
2112 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style
);
2113 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style
);
2114 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style
);
2115 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style
);
2116 verifyFormat("for (f(); int& c : {1, 2, 3})", Style
);
2118 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2119 " res2 = [](int& a) { return 0000000000000; };",
2122 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2123 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= true;
2124 verifyFormat("Const unsigned int* c;\n"
2125 "const unsigned int* d;\n"
2126 "Const unsigned int& e;\n"
2127 "const unsigned int& f;\n"
2128 "int* f1(int* a, int& b, int&& c);\n"
2129 "double* (*f2)(int* a, double&& b);\n"
2130 "const unsigned&& g;\n"
2131 "Const unsigned h;",
2133 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= false;
2134 verifyFormat("Const unsigned int* c;\n"
2135 "const unsigned int* d;\n"
2136 "Const unsigned int& e;\n"
2137 "const unsigned int& f;\n"
2138 "int* f1(int* a, int& b, int&& c);\n"
2139 "double* (*f2)(int* a, double&& b);\n"
2140 "const unsigned&& g;\n"
2141 "Const unsigned h;",
2144 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
2145 Style
.ReferenceAlignment
= FormatStyle::RAS_Left
;
2146 verifyFormat("int *f1(int *a, int& b, int&& c);", Style
);
2147 verifyFormat("int& f2(int&& c, int *a, int& b);", Style
);
2148 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style
);
2149 verifyFormat("int *a = f1();", Style
);
2150 verifyFormat("int& b = f2();", Style
);
2151 verifyFormat("int&& c = f3();", Style
);
2152 verifyFormat("int f3() { return sizeof(Foo&); }", Style
);
2153 verifyFormat("int f4() { return sizeof(Foo&&); }", Style
);
2154 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style
);
2155 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style
);
2156 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style
);
2157 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style
);
2158 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style
);
2160 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2161 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= true;
2162 verifyFormat("Const unsigned int *c;\n"
2163 "const unsigned int *d;\n"
2164 "Const unsigned int& e;\n"
2165 "const unsigned int& f;\n"
2166 "int *f1(int *a, int& b, int&& c);\n"
2167 "double *(*f2)(int *a, double&& b);\n"
2168 "const unsigned&& g;\n"
2169 "Const unsigned h;",
2171 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= false;
2172 verifyFormat("Const unsigned int *c;\n"
2173 "const unsigned int *d;\n"
2174 "Const unsigned int& e;\n"
2175 "const unsigned int& f;\n"
2176 "int *f1(int *a, int& b, int&& c);\n"
2177 "double *(*f2)(int *a, double&& b);\n"
2178 "const unsigned&& g;\n"
2179 "Const unsigned h;",
2182 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
2183 Style
.ReferenceAlignment
= FormatStyle::RAS_Middle
;
2184 verifyFormat("int* f1(int* a, int & b, int && c);", Style
);
2185 verifyFormat("int & f2(int && c, int* a, int & b);", Style
);
2186 verifyFormat("int && f3(int & b, int && c, int* a);", Style
);
2187 verifyFormat("int* a = f1();", Style
);
2188 verifyFormat("int & b = f2();", Style
);
2189 verifyFormat("int && c = f3();", Style
);
2190 verifyFormat("int f3() { return sizeof(Foo &); }", Style
);
2191 verifyFormat("int f4() { return sizeof(Foo &&); }", Style
);
2192 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style
);
2193 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style
);
2194 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style
);
2195 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style
);
2196 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style
);
2197 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style
);
2198 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style
);
2199 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style
);
2200 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style
);
2201 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style
);
2202 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style
);
2203 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style
);
2204 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style
);
2205 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style
);
2206 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style
);
2207 verifyFormat("for (f(); int & c : {1, 2, 3})", Style
);
2209 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2210 " res2 = [](int & a) { return 0000000000000; };",
2213 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2214 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= true;
2215 verifyFormat("Const unsigned int* c;\n"
2216 "const unsigned int* d;\n"
2217 "Const unsigned int & e;\n"
2218 "const unsigned int & f;\n"
2219 "int* f1(int* a, int & b, int && c);\n"
2220 "double* (*f2)(int* a, double && b);\n"
2221 "const unsigned && g;\n"
2222 "Const unsigned h;",
2224 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= false;
2225 verifyFormat("Const unsigned int* c;\n"
2226 "const unsigned int* d;\n"
2227 "Const unsigned int & e;\n"
2228 "const unsigned int & f;\n"
2229 "int* f1(int* a, int & b, int && c);\n"
2230 "double* (*f2)(int* a, double && b);\n"
2231 "const unsigned && g;\n"
2232 "Const unsigned h;",
2235 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
2236 Style
.ReferenceAlignment
= FormatStyle::RAS_Right
;
2237 verifyFormat("int * f1(int * a, int &b, int &&c);", Style
);
2238 verifyFormat("int &f2(int &&c, int * a, int &b);", Style
);
2239 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style
);
2240 verifyFormat("int * a = f1();", Style
);
2241 verifyFormat("int &b = f2();", Style
);
2242 verifyFormat("int &&c = f3();", Style
);
2243 verifyFormat("int f3() { return sizeof(Foo &); }", Style
);
2244 verifyFormat("int f4() { return sizeof(Foo &&); }", Style
);
2245 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style
);
2246 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style
);
2247 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style
);
2248 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style
);
2249 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style
);
2251 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2252 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= true;
2253 verifyFormat("Const unsigned int * c;\n"
2254 "const unsigned int * d;\n"
2255 "Const unsigned int &e;\n"
2256 "const unsigned int &f;\n"
2257 "int * f1(int * a, int &b, int &&c);\n"
2258 "double * (*f2)(int * a, double &&b);\n"
2259 "const unsigned &&g;\n"
2260 "Const unsigned h;",
2262 Style
.AlignConsecutiveDeclarations
.AlignFunctionPointers
= false;
2263 verifyFormat("Const unsigned int * c;\n"
2264 "const unsigned int * d;\n"
2265 "Const unsigned int &e;\n"
2266 "const unsigned int &f;\n"
2267 "int * f1(int * a, int &b, int &&c);\n"
2268 "double * (*f2)(int * a, double &&b);\n"
2269 "const unsigned &&g;\n"
2270 "Const unsigned h;",
2273 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2274 // specifically handled
2275 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2278 TEST_F(FormatTest
, FormatsForLoop
) {
2280 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2281 " ++VeryVeryLongLoopVariable)\n"
2283 verifyFormat("for (;;)\n"
2285 verifyFormat("for (;;) {\n}");
2286 verifyFormat("for (;;) {\n"
2289 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2292 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2293 " E = UnwrappedLines.end();\n"
2294 " I != E; ++I) {\n}");
2297 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2299 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2300 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2301 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2302 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2303 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2304 " E = FD->getDeclsInPrototypeScope().end();\n"
2305 " I != E; ++I) {\n}");
2306 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2307 " I = Container.begin(),\n"
2308 " E = Container.end();\n"
2309 " I != E; ++I) {\n}",
2310 getLLVMStyleWithColumns(76));
2313 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2317 " ++aaaaaaaaaaa) {\n}");
2318 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2319 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2321 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2322 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2324 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2326 " iter; ++iter) {\n"
2328 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2330 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2331 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2333 // These should not be formatted as Objective-C for-in loops.
2334 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2335 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2336 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2338 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2340 FormatStyle NoBinPacking
= getLLVMStyle();
2341 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
2342 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2343 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2344 " aaaaaaaaaaaaaaaa,\n"
2345 " aaaaaaaaaaaaaaaa,\n"
2346 " aaaaaaaaaaaaaaaa);\n"
2347 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2351 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2352 " E = UnwrappedLines.end();\n"
2357 FormatStyle AlignLeft
= getLLVMStyle();
2358 AlignLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
2359 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft
);
2362 TEST_F(FormatTest
, RangeBasedForLoops
) {
2363 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2365 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2366 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2367 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2369 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2370 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2373 TEST_F(FormatTest
, ForEachLoops
) {
2374 FormatStyle Style
= getLLVMStyle();
2375 EXPECT_EQ(Style
.AllowShortBlocksOnASingleLine
, FormatStyle::SBS_Never
);
2376 EXPECT_EQ(Style
.AllowShortLoopsOnASingleLine
, false);
2377 verifyFormat("void f() {\n"
2380 " foreach (Item *item, itemlist) {\n"
2382 " Q_FOREACH (Item *item, itemlist) {\n"
2384 " BOOST_FOREACH (Item *item, itemlist) {\n"
2386 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2389 verifyFormat("void f() {\n"
2392 " Q_FOREACH (int v, vec)\n"
2397 " Q_FOREACH (int v, vec) {\n"
2403 FormatStyle ShortBlocks
= getLLVMStyle();
2404 ShortBlocks
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
2405 EXPECT_EQ(ShortBlocks
.AllowShortLoopsOnASingleLine
, false);
2406 verifyFormat("void f() {\n"
2409 " Q_FOREACH (int &v, vec)\n"
2414 " Q_FOREACH (int &v, vec) {\n"
2420 FormatStyle ShortLoops
= getLLVMStyle();
2421 ShortLoops
.AllowShortLoopsOnASingleLine
= true;
2422 EXPECT_EQ(ShortLoops
.AllowShortBlocksOnASingleLine
, FormatStyle::SBS_Never
);
2423 verifyFormat("void f() {\n"
2424 " for (;;) int j = 1;\n"
2425 " Q_FOREACH (int &v, vec) int j = 1;\n"
2429 " Q_FOREACH (int &v, vec) {\n"
2435 FormatStyle ShortBlocksAndLoops
= getLLVMStyle();
2436 ShortBlocksAndLoops
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
2437 ShortBlocksAndLoops
.AllowShortLoopsOnASingleLine
= true;
2438 verifyFormat("void f() {\n"
2439 " for (;;) int j = 1;\n"
2440 " Q_FOREACH (int &v, vec) int j = 1;\n"
2441 " for (;;) { int j = 1; }\n"
2442 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2444 ShortBlocksAndLoops
);
2446 Style
.SpaceBeforeParens
=
2447 FormatStyle::SBPO_ControlStatementsExceptControlMacros
;
2448 verifyFormat("void f() {\n"
2451 " foreach(Item *item, itemlist) {\n"
2453 " Q_FOREACH(Item *item, itemlist) {\n"
2455 " BOOST_FOREACH(Item *item, itemlist) {\n"
2457 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2461 // As function-like macros.
2462 verifyFormat("#define foreach(x, y)\n"
2463 "#define Q_FOREACH(x, y)\n"
2464 "#define BOOST_FOREACH(x, y)\n"
2465 "#define UNKNOWN_FOREACH(x, y)");
2467 // Not as function-like macros.
2468 verifyFormat("#define foreach (x, y)\n"
2469 "#define Q_FOREACH (x, y)\n"
2470 "#define BOOST_FOREACH (x, y)\n"
2471 "#define UNKNOWN_FOREACH (x, y)");
2473 // handle microsoft non standard extension
2474 verifyFormat("for each (char c in x->MyStringProperty)");
2477 TEST_F(FormatTest
, FormatsWhileLoop
) {
2478 verifyFormat("while (true) {\n}");
2479 verifyFormat("while (true)\n"
2481 verifyFormat("while () {\n}");
2482 verifyFormat("while () {\n"
2487 TEST_F(FormatTest
, FormatsDoWhile
) {
2488 verifyFormat("do {\n"
2489 " do_something();\n"
2490 "} while (something());");
2492 " do_something();\n"
2493 "while (something());");
2496 TEST_F(FormatTest
, FormatsSwitchStatement
) {
2497 verifyFormat("switch (x) {\n"
2509 verifyFormat("switch (x) {\n"
2518 verifyFormat("switch (x) {\n"
2528 verifyFormat("switch (x) {\n"
2538 verifyFormat("switch (x) {\n"
2544 verifyFormat("switch (test)\n"
2546 verifyFormat("switch (x) {\n"
2551 verifyFormat("switch (x) {\n"
2557 verifyFormat("switch (x) {\n"
2559 " // Do amazing stuff\n"
2566 verifyFormat("#define A \\\n"
2567 " switch (x) { \\\n"
2571 getLLVMStyleWithColumns(20));
2572 verifyFormat("#define OPERATION_CASE(name) \\\n"
2573 " case OP_name: \\\n"
2574 " return operations::Operation##name",
2575 getLLVMStyleWithColumns(40));
2576 verifyFormat("switch (x) {\n"
2582 verifyGoogleFormat("switch (x) {\n"
2594 verifyGoogleFormat("switch (x) {\n"
2600 verifyGoogleFormat("switch (test)\n"
2603 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2604 " case OP_name: \\\n"
2605 " return operations::Operation##name");
2606 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2607 " // Get the correction operation class.\n"
2608 " switch (OpCode) {\n"
2610 " CASE(Subtract);\n"
2612 " return operations::Unknown;\n"
2614 "#undef OPERATION_CASE\n"
2616 verifyFormat("DEBUG({\n"
2627 verifyNoChange("DEBUG({\n"
2638 verifyFormat("switch (n) {\n"
2655 verifyFormat("switch (a) {\n"
2660 verifyFormat("switch (a) {\n"
2661 "case some_namespace::\n"
2665 getLLVMStyleWithColumns(34));
2667 verifyFormat("switch (a) {\n"
2668 "[[likely]] case 1:\n"
2671 verifyFormat("switch (a) {\n"
2672 "[[likely]] [[other::likely]] case 1:\n"
2675 verifyFormat("switch (x) {\n"
2678 "[[likely]] case 2:\n"
2681 verifyFormat("switch (a) {\n"
2683 "[[likely]] case 2:\n"
2686 FormatStyle Attributes
= getLLVMStyle();
2687 Attributes
.AttributeMacros
.push_back("LIKELY");
2688 Attributes
.AttributeMacros
.push_back("OTHER_LIKELY");
2689 verifyFormat("switch (a) {\n"
2694 verifyFormat("switch (a) {\n"
2695 "LIKELY OTHER_LIKELY() case b:\n"
2699 verifyFormat("switch (a) {\n"
2706 verifyFormat("switch (a) {\n"
2713 FormatStyle Style
= getLLVMStyle();
2714 Style
.IndentCaseLabels
= true;
2715 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
2716 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
2717 Style
.BraceWrapping
.AfterCaseLabel
= true;
2718 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
2719 verifyFormat("switch (n)\n"
2739 Style
.BraceWrapping
.AfterCaseLabel
= false;
2740 verifyFormat("switch (n)\n"
2760 Style
.IndentCaseLabels
= false;
2761 Style
.IndentCaseBlocks
= true;
2762 verifyFormat("switch (n)\n"
2786 Style
.IndentCaseLabels
= true;
2787 Style
.IndentCaseBlocks
= true;
2788 verifyFormat("switch (n)\n"
2814 TEST_F(FormatTest
, CaseRanges
) {
2815 verifyFormat("switch (x) {\n"
2816 "case 'A' ... 'Z':\n"
2823 TEST_F(FormatTest
, ShortEnums
) {
2824 FormatStyle Style
= getLLVMStyle();
2825 EXPECT_TRUE(Style
.AllowShortEnumsOnASingleLine
);
2826 EXPECT_FALSE(Style
.BraceWrapping
.AfterEnum
);
2827 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style
);
2828 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style
);
2829 Style
.AllowShortEnumsOnASingleLine
= false;
2830 verifyFormat("enum {\n"
2834 "} ShortEnum1, ShortEnum2;",
2836 verifyFormat("typedef enum {\n"
2840 "} ShortEnum1, ShortEnum2;",
2842 verifyFormat("enum {\n"
2844 "} ShortEnum1, ShortEnum2;",
2846 verifyFormat("typedef enum {\n"
2848 "} ShortEnum1, ShortEnum2;",
2850 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
2851 Style
.BraceWrapping
.AfterEnum
= true;
2852 verifyFormat("enum\n"
2857 "} ShortEnum1, ShortEnum2;",
2859 verifyFormat("typedef enum\n"
2864 "} ShortEnum1, ShortEnum2;",
2868 TEST_F(FormatTest
, ShortCompoundRequirement
) {
2869 FormatStyle Style
= getLLVMStyle();
2870 EXPECT_TRUE(Style
.AllowShortCompoundRequirementOnASingleLine
);
2871 verifyFormat("template <typename T>\n"
2872 "concept c = requires(T x) {\n"
2873 " { x + 1 } -> std::same_as<int>;\n"
2876 verifyFormat("template <typename T>\n"
2877 "concept c = requires(T x) {\n"
2878 " { x + 1 } -> std::same_as<int>;\n"
2879 " { x + 2 } -> std::same_as<int>;\n"
2882 Style
.AllowShortCompoundRequirementOnASingleLine
= false;
2883 verifyFormat("template <typename T>\n"
2884 "concept c = requires(T x) {\n"
2887 " } -> std::same_as<int>;\n"
2890 verifyFormat("template <typename T>\n"
2891 "concept c = requires(T x) {\n"
2894 " } -> std::same_as<int>;\n"
2897 " } -> std::same_as<int>;\n"
2902 TEST_F(FormatTest
, ShortCaseLabels
) {
2903 FormatStyle Style
= getLLVMStyle();
2904 Style
.AllowShortCaseLabelsOnASingleLine
= true;
2905 verifyFormat("switch (a) {\n"
2906 "case 1: x = 1; break;\n"
2911 "case 6: // comment\n"
2917 " x = 8; // comment\n"
2919 "default: y = 1; break;\n"
2922 verifyFormat("switch (a) {\n"
2923 "case 0: return; // comment\n"
2924 "case 1: break; // comment\n"
2931 "case 4: break; /* comment */\n"
2935 "case 6: /* comment */ x = 1; break;\n"
2936 "case 7: x = /* comment */ 1; break;\n"
2938 " x = 1; /* comment */\n"
2941 " break; // comment line 1\n"
2942 " // comment line 2\n"
2945 verifyFormat("switch (a) {\n"
2948 " // fall through\n"
2952 " return; /* comment line 1\n"
2953 " * comment line 2 */\n"
2955 "// something else\n"
2962 " // fall through\n"
2967 " return; /* comment line 1\n"
2968 " * comment line 2 */\n"
2971 "// something else\n"
2977 verifyFormat("switch (a) {\n"
2979 " return; // long long long long long long long long long long "
2980 "long long comment\n"
2984 "case 0: return; // long long long long long long long long "
2985 "long long long long comment line\n"
2988 verifyFormat("switch (a) {\n"
2990 " return; /* long long long long long long long long long long "
2991 "long long comment\n"
2995 "case 0: return; /* long long long long long long long long "
2996 "long long long long comment line */\n"
2999 verifyFormat("switch (a) {\n"
3001 "case 0: return 0;\n"
3005 verifyFormat("switch (a) {\n"
3020 Style
.ColumnLimit
= 21;
3021 verifyFormat("#define X \\\n"
3025 verifyFormat("switch (a) {\n"
3026 "case 1: x = 1; break;\n"
3036 Style
.ColumnLimit
= 80;
3037 Style
.AllowShortCaseLabelsOnASingleLine
= false;
3038 Style
.IndentCaseLabels
= true;
3039 verifyFormat("switch (n) {\n"
3040 " default /*comments*/:\n"
3046 "default/*comments*/:\n"
3052 Style
.AllowShortCaseLabelsOnASingleLine
= true;
3053 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
3054 Style
.BraceWrapping
.AfterCaseLabel
= true;
3055 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
3056 verifyFormat("switch (n)\n"
3079 TEST_F(FormatTest
, FormatsLabels
) {
3080 verifyFormat("void f() {\n"
3083 " some_other_code();\n"
3085 " some_more_code();\n"
3087 " some_more_code();\n"
3093 " some_other_code();\n"
3102 "test_label: { some_other_code(); }\n"
3107 " some_other_code();\n"
3108 " some_other_code();\n"
3114 "[[bar]] [[baz]] L2:\n"
3119 "[[bar]] [[baz]] L2:\n"
3127 " [[bar]] [[baz]] L2:\n"
3132 FormatStyle Style
= getLLVMStyle();
3133 Style
.IndentGotoLabels
= false;
3134 verifyFormat("void f() {\n"
3137 " some_other_code();\n"
3139 " some_more_code();\n"
3141 " some_more_code();\n"
3148 " some_other_code();\n"
3159 "test_label: { some_other_code(); }\n"
3166 "[[bar]] [[baz]] L2:\n"
3172 Style
.ColumnLimit
= 15;
3173 verifyFormat("#define FOO \\\n"
3178 // The opening brace may either be on the same unwrapped line as the colon or
3179 // on a separate one. The formatter should recognize both.
3180 Style
= getLLVMStyle();
3181 Style
.BreakBeforeBraces
= FormatStyle::BraceBreakingStyle::BS_Allman
;
3186 " some_other_code();\n"
3193 "[[bar]] [[baz]] L2:\n"
3200 TEST_F(FormatTest
, MultiLineControlStatements
) {
3201 FormatStyle Style
= getLLVMStyleWithColumns(20);
3202 Style
.BreakBeforeBraces
= FormatStyle::BraceBreakingStyle::BS_Custom
;
3203 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_MultiLine
;
3204 // Short lines should keep opening brace on same line.
3205 verifyFormat("if (foo) {\n"
3208 "if(foo){bar();}", Style
);
3209 verifyFormat("if (foo) {\n"
3214 "if(foo){bar();}else{baz();}", Style
);
3215 verifyFormat("if (foo && bar) {\n"
3218 "if(foo&&bar){baz();}", Style
);
3219 verifyFormat("if (foo) {\n"
3221 "} else if (baz) {\n"
3224 "if(foo){bar();}else if(baz){quux();}", Style
);
3225 verifyFormat("if (foo) {\n"
3227 "} else if (baz) {\n"
3232 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style
);
3233 verifyFormat("for (;;) {\n"
3237 verifyFormat("while (1) {\n"
3240 "while(1){foo();}", Style
);
3241 verifyFormat("switch (foo) {\n"
3245 "switch(foo){case bar:return;}", Style
);
3246 verifyFormat("try {\n"
3251 "try{foo();}catch(...){bar();}", Style
);
3252 verifyFormat("do {\n"
3256 "do{foo();}while(bar&&baz);", Style
);
3257 // Long lines should put opening brace on new line.
3258 verifyFormat("void f() {\n"
3259 " if (a1 && a2 &&\n"
3265 "void f(){if(a1&&a2&&a3){quux();}}", Style
);
3266 verifyFormat("if (foo && bar &&\n"
3271 "if(foo&&bar&&baz){quux();}", Style
);
3272 verifyFormat("if (foo && bar &&\n"
3277 "if (foo && bar &&\n"
3282 verifyFormat("if (foo) {\n"
3284 "} else if (baz ||\n"
3289 "if(foo){bar();}else if(baz||quux){foobar();}", Style
);
3290 verifyFormat("if (foo) {\n"
3292 "} else if (baz ||\n"
3299 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3301 verifyFormat("for (int i = 0;\n"
3306 "for(int i=0;i<10;++i){foo();}", Style
);
3307 verifyFormat("foreach (int i,\n"
3312 "foreach(int i, list){foo();}", Style
);
3314 40; // to concentrate at brace wrapping, not line wrap due to column limit
3315 verifyFormat("foreach (int i, list) {\n"
3318 "foreach(int i, list){foo();}", Style
);
3320 20; // to concentrate at brace wrapping, not line wrap due to column limit
3321 verifyFormat("while (foo || bar ||\n"
3326 "while(foo||bar||baz){quux();}", Style
);
3327 verifyFormat("switch (\n"
3333 "switch(foo=barbaz){case quux:return;}", Style
);
3334 verifyFormat("try {\n"
3337 " Exception &bar)\n"
3341 "try{foo();}catch(Exception&bar){baz();}", Style
);
3343 40; // to concentrate at brace wrapping, not line wrap due to column limit
3344 verifyFormat("try {\n"
3346 "} catch (Exception &bar) {\n"
3349 "try{foo();}catch(Exception&bar){baz();}", Style
);
3351 20; // to concentrate at brace wrapping, not line wrap due to column limit
3353 Style
.BraceWrapping
.BeforeElse
= true;
3354 verifyFormat("if (foo) {\n"
3365 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3368 Style
.BraceWrapping
.BeforeCatch
= true;
3369 verifyFormat("try {\n"
3375 "try{foo();}catch(...){baz();}", Style
);
3377 Style
.BraceWrapping
.AfterFunction
= true;
3378 Style
.BraceWrapping
.AfterStruct
= false;
3379 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_MultiLine
;
3380 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
3381 Style
.ColumnLimit
= 80;
3382 verifyFormat("void shortfunction() { bar(); }", Style
);
3383 verifyFormat("struct T shortfunction() { return bar(); }", Style
);
3384 verifyFormat("struct T {};", Style
);
3386 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
3387 verifyFormat("void shortfunction()\n"
3392 verifyFormat("struct T shortfunction()\n"
3397 verifyFormat("struct T {};", Style
);
3399 Style
.BraceWrapping
.AfterFunction
= false;
3400 Style
.BraceWrapping
.AfterStruct
= true;
3401 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
3402 verifyFormat("void shortfunction() { bar(); }", Style
);
3403 verifyFormat("struct T shortfunction() { return bar(); }", Style
);
3404 verifyFormat("struct T\n"
3409 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
3410 verifyFormat("void shortfunction() {\n"
3414 verifyFormat("struct T shortfunction() {\n"
3418 verifyFormat("struct T\n"
3424 TEST_F(FormatTest
, BeforeWhile
) {
3425 FormatStyle Style
= getLLVMStyle();
3426 Style
.BreakBeforeBraces
= FormatStyle::BraceBreakingStyle::BS_Custom
;
3428 verifyFormat("do {\n"
3432 Style
.BraceWrapping
.BeforeWhile
= true;
3433 verifyFormat("do {\n"
3440 //===----------------------------------------------------------------------===//
3441 // Tests for classes, namespaces, etc.
3442 //===----------------------------------------------------------------------===//
3444 TEST_F(FormatTest
, DoesNotBreakSemiAfterClassDecl
) {
3445 verifyFormat("class A {};");
3448 TEST_F(FormatTest
, UnderstandsAccessSpecifiers
) {
3449 verifyFormat("class A {\n"
3451 "public: // comment\n"
3456 verifyFormat("export class A {\n"
3458 "public: // comment\n"
3463 verifyGoogleFormat("class A {\n"
3469 verifyGoogleFormat("export class A {\n"
3475 verifyFormat("class A {\n"
3480 "protected slots:\n"
3482 "protected Q_SLOTS:\n"
3486 "private Q_SLOTS:\n"
3494 // Don't interpret 'signals' the wrong way.
3495 verifyFormat("signals.set();");
3496 verifyFormat("for (Signals signals : f()) {\n}");
3498 " signals.set(); // This needs indentation.\n"
3500 verifyFormat("void f() {\n"
3504 verifyFormat("private[1];");
3505 verifyFormat("testArray[public] = 1;");
3506 verifyFormat("public();");
3507 verifyFormat("myFunc(public);");
3508 verifyFormat("std::vector<int> testVec = {private};");
3509 verifyFormat("private.p = 1;");
3510 verifyFormat("void function(private...) {};");
3511 verifyFormat("if (private && public)");
3512 verifyFormat("private &= true;");
3513 verifyFormat("int x = private * public;");
3514 verifyFormat("public *= private;");
3515 verifyFormat("int x = public + private;");
3516 verifyFormat("private++;");
3517 verifyFormat("++private;");
3518 verifyFormat("public += private;");
3519 verifyFormat("public = public - private;");
3520 verifyFormat("public->foo();");
3521 verifyFormat("private--;");
3522 verifyFormat("--private;");
3523 verifyFormat("public -= 1;");
3524 verifyFormat("if (!private && !public)");
3525 verifyFormat("public != private;");
3526 verifyFormat("int x = public / private;");
3527 verifyFormat("public /= 2;");
3528 verifyFormat("public = public % 2;");
3529 verifyFormat("public %= 2;");
3530 verifyFormat("if (public < private)");
3531 verifyFormat("public << private;");
3532 verifyFormat("public <<= private;");
3533 verifyFormat("if (public > private)");
3534 verifyFormat("public >> private;");
3535 verifyFormat("public >>= private;");
3536 verifyFormat("public ^ private;");
3537 verifyFormat("public ^= private;");
3538 verifyFormat("public | private;");
3539 verifyFormat("public |= private;");
3540 verifyFormat("auto x = private ? 1 : 2;");
3541 verifyFormat("if (public == private)");
3542 verifyFormat("void foo(public, private)");
3543 verifyFormat("public::foo();");
3545 verifyFormat("class A {\n"
3547 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3554 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3561 TEST_F(FormatTest
, SeparatesLogicalBlocks
) {
3562 verifyFormat("class A {\n"
3581 verifyFormat("class A {\n"
3594 // Even ensure proper spacing inside macros.
3595 verifyFormat("#define B \\\n"
3610 // But don't remove empty lines after macros ending in access specifiers.
3611 verifyFormat("#define A private:\n"
3614 "#define A private:\n"
3619 TEST_F(FormatTest
, FormatsClasses
) {
3620 verifyFormat("class A : public B {};");
3621 verifyFormat("class A : public ::B {};");
3624 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3625 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3626 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3627 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3628 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3630 "class A : public B, public C, public D, public E, public F {};");
3631 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3638 verifyFormat("class\n"
3639 " ReallyReallyLongClassName {\n"
3642 getLLVMStyleWithColumns(32));
3643 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3644 " aaaaaaaaaaaaaaaa> {};");
3645 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3646 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3647 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3648 verifyFormat("template <class R, class C>\n"
3649 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3650 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3651 verifyFormat("class ::A::B {};");
3654 TEST_F(FormatTest
, BreakInheritanceStyle
) {
3655 FormatStyle StyleWithInheritanceBreakBeforeComma
= getLLVMStyle();
3656 StyleWithInheritanceBreakBeforeComma
.BreakInheritanceList
=
3657 FormatStyle::BILS_BeforeComma
;
3658 verifyFormat("class MyClass : public X {};",
3659 StyleWithInheritanceBreakBeforeComma
);
3660 verifyFormat("class MyClass\n"
3663 StyleWithInheritanceBreakBeforeComma
);
3664 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3665 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3666 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3667 StyleWithInheritanceBreakBeforeComma
);
3668 verifyFormat("struct aaaaaaaaaaaaa\n"
3669 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3670 " aaaaaaaaaaaaaaaa> {};",
3671 StyleWithInheritanceBreakBeforeComma
);
3673 FormatStyle StyleWithInheritanceBreakAfterColon
= getLLVMStyle();
3674 StyleWithInheritanceBreakAfterColon
.BreakInheritanceList
=
3675 FormatStyle::BILS_AfterColon
;
3676 verifyFormat("class MyClass : public X {};",
3677 StyleWithInheritanceBreakAfterColon
);
3678 verifyFormat("class MyClass : public X, public Y {};",
3679 StyleWithInheritanceBreakAfterColon
);
3680 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3681 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3682 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3683 StyleWithInheritanceBreakAfterColon
);
3684 verifyFormat("struct aaaaaaaaaaaaa :\n"
3685 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3686 " aaaaaaaaaaaaaaaa> {};",
3687 StyleWithInheritanceBreakAfterColon
);
3689 FormatStyle StyleWithInheritanceBreakAfterComma
= getLLVMStyle();
3690 StyleWithInheritanceBreakAfterComma
.BreakInheritanceList
=
3691 FormatStyle::BILS_AfterComma
;
3692 verifyFormat("class MyClass : public X {};",
3693 StyleWithInheritanceBreakAfterComma
);
3694 verifyFormat("class MyClass : public X,\n"
3696 StyleWithInheritanceBreakAfterComma
);
3698 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3699 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3701 StyleWithInheritanceBreakAfterComma
);
3702 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3703 " aaaaaaaaaaaaaaaa> {};",
3704 StyleWithInheritanceBreakAfterComma
);
3705 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3706 " : public OnceBreak,\n"
3707 " public AlwaysBreak,\n"
3708 " EvenBasesFitInOneLine {};",
3709 StyleWithInheritanceBreakAfterComma
);
3712 TEST_F(FormatTest
, FormatsVariableDeclarationsAfterRecord
) {
3713 verifyFormat("class A {\n} a, b;");
3714 verifyFormat("struct A {\n} a, b;");
3715 verifyFormat("union A {\n} a, b;");
3717 verifyFormat("constexpr class A {\n} a, b;");
3718 verifyFormat("constexpr struct A {\n} a, b;");
3719 verifyFormat("constexpr union A {\n} a, b;");
3721 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3722 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3723 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3725 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3726 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3727 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3729 verifyFormat("namespace ns {\n"
3732 "} // namespace ns");
3733 verifyFormat("namespace ns {\n"
3736 "} // namespace ns");
3737 verifyFormat("namespace ns {\n"
3738 "constexpr class C {\n"
3740 "} // namespace ns");
3741 verifyFormat("namespace ns {\n"
3742 "class { /* comment */\n"
3744 "} // namespace ns");
3745 verifyFormat("namespace ns {\n"
3746 "const class { /* comment */\n"
3748 "} // namespace ns");
3751 TEST_F(FormatTest
, FormatsEnum
) {
3752 verifyFormat("enum {\n"
3756 " Three = (One + Two),\n"
3757 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3758 " Five = (One, Two, Three, Four, 5)\n"
3760 verifyGoogleFormat("enum {\n"
3764 " Three = (One + Two),\n"
3765 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3766 " Five = (One, Two, Three, Four, 5)\n"
3768 verifyFormat("enum Enum {};");
3769 verifyFormat("enum {};");
3770 verifyFormat("enum X E {} d;");
3771 verifyFormat("enum __attribute__((...)) E {} d;");
3772 verifyFormat("enum __declspec__((...)) E {} d;");
3773 verifyFormat("enum [[nodiscard]] E {} d;");
3774 verifyFormat("enum {\n"
3775 " Bar = Foo<int, int>::value\n"
3777 getLLVMStyleWithColumns(30));
3779 verifyFormat("enum ShortEnum { A, B, C };");
3780 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3782 verifyFormat("enum KeepEmptyLines {\n"
3789 "enum KeepEmptyLines {\n"
3797 verifyFormat("enum E { // comment\n"
3803 FormatStyle EightIndent
= getLLVMStyle();
3804 EightIndent
.IndentWidth
= 8;
3805 verifyFormat("enum {\n"
3820 verifyFormat("enum [[nodiscard]] E {\n"
3824 verifyFormat("enum [[nodiscard]] E {\n"
3830 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n"
3836 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n"
3842 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n"
3847 "enum [[clang::enum_extensibility(open)]] E{// foo\n"
3853 verifyFormat("enum X f() {\n"
3857 verifyFormat("enum X Type::f() {\n"
3861 verifyFormat("enum ::X f() {\n"
3865 verifyFormat("enum ns::X f() {\n"
3871 TEST_F(FormatTest
, FormatsEnumsWithErrors
) {
3872 verifyFormat("enum Type {\n"
3873 " One = 0; // These semicolons should be commas.\n"
3876 verifyFormat("namespace n {\n"
3879 " Two, // missing };\n"
3885 TEST_F(FormatTest
, FormatsEnumStruct
) {
3886 verifyFormat("enum struct {\n"
3890 " Three = (One + Two),\n"
3891 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3892 " Five = (One, Two, Three, Four, 5)\n"
3894 verifyFormat("enum struct Enum {};");
3895 verifyFormat("enum struct {};");
3896 verifyFormat("enum struct X E {} d;");
3897 verifyFormat("enum struct __attribute__((...)) E {} d;");
3898 verifyFormat("enum struct __declspec__((...)) E {} d;");
3899 verifyFormat("enum struct [[nodiscard]] E {} d;");
3900 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3902 verifyFormat("enum struct [[nodiscard]] E {\n"
3906 verifyFormat("enum struct [[nodiscard]] E {\n"
3914 TEST_F(FormatTest
, FormatsEnumClass
) {
3915 verifyFormat("enum class {\n"
3919 " Three = (One + Two),\n"
3920 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3921 " Five = (One, Two, Three, Four, 5)\n"
3923 verifyFormat("enum class Enum {};");
3924 verifyFormat("enum class {};");
3925 verifyFormat("enum class X E {} d;");
3926 verifyFormat("enum class __attribute__((...)) E {} d;");
3927 verifyFormat("enum class __declspec__((...)) E {} d;");
3928 verifyFormat("enum class [[nodiscard]] E {} d;");
3929 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3931 verifyFormat("enum class [[nodiscard]] E {\n"
3935 verifyFormat("enum class [[nodiscard]] E {\n"
3943 TEST_F(FormatTest
, FormatsEnumTypes
) {
3944 verifyFormat("enum X : int {\n"
3945 " A, // Force multiple lines.\n"
3948 verifyFormat("enum X : int { A, B };");
3949 verifyFormat("enum X : std::uint32_t { A, B };");
3952 TEST_F(FormatTest
, FormatsTypedefEnum
) {
3953 FormatStyle Style
= getLLVMStyleWithColumns(40);
3954 verifyFormat("typedef enum {} EmptyEnum;");
3955 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3956 verifyFormat("typedef enum {\n"
3963 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
3964 Style
.BraceWrapping
.AfterEnum
= true;
3965 verifyFormat("typedef enum {} EmptyEnum;");
3966 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3967 verifyFormat("typedef enum\n"
3977 TEST_F(FormatTest
, FormatsNSEnums
) {
3978 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3980 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3981 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3982 " // Information about someDecentlyLongValue.\n"
3983 " someDecentlyLongValue,\n"
3984 " // Information about anotherDecentlyLongValue.\n"
3985 " anotherDecentlyLongValue,\n"
3986 " // Information about aThirdDecentlyLongValue.\n"
3987 " aThirdDecentlyLongValue\n"
3989 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3990 " // Information about someDecentlyLongValue.\n"
3991 " someDecentlyLongValue,\n"
3992 " // Information about anotherDecentlyLongValue.\n"
3993 " anotherDecentlyLongValue,\n"
3994 " // Information about aThirdDecentlyLongValue.\n"
3995 " aThirdDecentlyLongValue\n"
3997 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
4002 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
4007 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
4012 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
4019 TEST_F(FormatTest
, FormatsBitfields
) {
4020 verifyFormat("struct Bitfields {\n"
4021 " unsigned sClass : 8;\n"
4022 " unsigned ValueKind : 2;\n"
4024 verifyFormat("struct A {\n"
4025 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
4026 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
4028 verifyFormat("struct MyStruct {\n"
4034 FormatStyle Style
= getLLVMStyle();
4035 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_None
;
4036 verifyFormat("struct Bitfields {\n"
4037 " unsigned sClass:8;\n"
4038 " unsigned ValueKind:2;\n"
4042 verifyFormat("struct A {\n"
4043 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
4044 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
4047 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_Before
;
4048 verifyFormat("struct Bitfields {\n"
4049 " unsigned sClass :8;\n"
4050 " unsigned ValueKind :2;\n"
4054 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_After
;
4055 verifyFormat("struct Bitfields {\n"
4056 " unsigned sClass: 8;\n"
4057 " unsigned ValueKind: 2;\n"
4063 TEST_F(FormatTest
, FormatsNamespaces
) {
4064 FormatStyle LLVMWithNoNamespaceFix
= getLLVMStyle();
4065 LLVMWithNoNamespaceFix
.FixNamespaceComments
= false;
4067 verifyFormat("namespace some_namespace {\n"
4069 "void f() { f(); }\n"
4071 LLVMWithNoNamespaceFix
);
4072 verifyFormat("#define M(x) x##x\n"
4073 "namespace M(x) {\n"
4075 "void f() { f(); }\n"
4077 LLVMWithNoNamespaceFix
);
4078 verifyFormat("#define M(x) x##x\n"
4079 "namespace N::inline M(x) {\n"
4081 "void f() { f(); }\n"
4083 LLVMWithNoNamespaceFix
);
4084 verifyFormat("#define M(x) x##x\n"
4085 "namespace M(x)::inline N {\n"
4087 "void f() { f(); }\n"
4089 LLVMWithNoNamespaceFix
);
4090 verifyFormat("#define M(x) x##x\n"
4091 "namespace N::M(x) {\n"
4093 "void f() { f(); }\n"
4095 LLVMWithNoNamespaceFix
);
4096 verifyFormat("#define M(x) x##x\n"
4097 "namespace M::N(x) {\n"
4099 "void f() { f(); }\n"
4101 LLVMWithNoNamespaceFix
);
4102 verifyFormat("namespace N::inline D {\n"
4104 "void f() { f(); }\n"
4106 LLVMWithNoNamespaceFix
);
4107 verifyFormat("namespace N::inline D::E {\n"
4109 "void f() { f(); }\n"
4111 LLVMWithNoNamespaceFix
);
4112 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
4114 "void f() { f(); }\n"
4116 LLVMWithNoNamespaceFix
);
4117 verifyFormat("/* something */ namespace some_namespace {\n"
4119 "void f() { f(); }\n"
4121 LLVMWithNoNamespaceFix
);
4122 verifyFormat("namespace {\n"
4124 "void f() { f(); }\n"
4126 LLVMWithNoNamespaceFix
);
4127 verifyFormat("/* something */ namespace {\n"
4129 "void f() { f(); }\n"
4131 LLVMWithNoNamespaceFix
);
4132 verifyFormat("inline namespace X {\n"
4134 "void f() { f(); }\n"
4136 LLVMWithNoNamespaceFix
);
4137 verifyFormat("/* something */ inline namespace X {\n"
4139 "void f() { f(); }\n"
4141 LLVMWithNoNamespaceFix
);
4142 verifyFormat("export namespace X {\n"
4144 "void f() { f(); }\n"
4146 LLVMWithNoNamespaceFix
);
4147 verifyFormat("using namespace some_namespace;\n"
4149 "void f() { f(); }",
4150 LLVMWithNoNamespaceFix
);
4152 // This code is more common than we thought; if we
4153 // layout this correctly the semicolon will go into
4154 // its own line, which is undesirable.
4155 verifyFormat("namespace {};", LLVMWithNoNamespaceFix
);
4156 verifyFormat("namespace {\n"
4159 LLVMWithNoNamespaceFix
);
4161 verifyFormat("namespace {\n"
4162 "int SomeVariable = 0; // comment\n"
4164 LLVMWithNoNamespaceFix
);
4165 verifyFormat("#ifndef HEADER_GUARD\n"
4166 "#define HEADER_GUARD\n"
4167 "namespace my_namespace {\n"
4169 "} // my_namespace\n"
4170 "#endif // HEADER_GUARD",
4171 "#ifndef HEADER_GUARD\n"
4172 " #define HEADER_GUARD\n"
4173 " namespace my_namespace {\n"
4175 "} // my_namespace\n"
4176 "#endif // HEADER_GUARD",
4177 LLVMWithNoNamespaceFix
);
4179 verifyFormat("namespace A::B {\n"
4182 LLVMWithNoNamespaceFix
);
4184 FormatStyle Style
= getLLVMStyle();
4185 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
4186 verifyFormat("namespace out {\n"
4190 " } // namespace in\n"
4191 "} // namespace out",
4196 "} // namespace in\n"
4197 "} // namespace out",
4200 FormatStyle ShortInlineFunctions
= getLLVMStyle();
4201 ShortInlineFunctions
.NamespaceIndentation
= FormatStyle::NI_All
;
4202 ShortInlineFunctions
.AllowShortFunctionsOnASingleLine
=
4203 FormatStyle::SFS_Inline
;
4204 verifyFormat("namespace {\n"
4209 ShortInlineFunctions
);
4210 verifyFormat("namespace { /* comment */\n"
4215 ShortInlineFunctions
);
4216 verifyFormat("namespace { // comment\n"
4221 ShortInlineFunctions
);
4222 verifyFormat("namespace {\n"
4228 ShortInlineFunctions
);
4229 verifyFormat("namespace interface {\n"
4233 "} // namespace interface",
4234 ShortInlineFunctions
);
4235 verifyFormat("namespace {\n"
4237 " void f() { return; }\n"
4240 ShortInlineFunctions
);
4241 verifyFormat("namespace {\n"
4242 " class X { /* comment */\n"
4243 " void f() { return; }\n"
4246 ShortInlineFunctions
);
4247 verifyFormat("namespace {\n"
4248 " class X { // comment\n"
4249 " void f() { return; }\n"
4252 ShortInlineFunctions
);
4253 verifyFormat("namespace {\n"
4255 " void f() { return; }\n"
4258 ShortInlineFunctions
);
4259 verifyFormat("namespace {\n"
4261 " void f() { return; }\n"
4264 ShortInlineFunctions
);
4265 verifyFormat("extern \"C\" {\n"
4270 ShortInlineFunctions
);
4271 verifyFormat("namespace {\n"
4273 " void f() { return; }\n"
4276 ShortInlineFunctions
);
4277 verifyFormat("namespace {\n"
4278 " [[nodiscard]] class X {\n"
4279 " void f() { return; }\n"
4282 ShortInlineFunctions
);
4283 verifyFormat("namespace {\n"
4284 " static class X {\n"
4285 " void f() { return; }\n"
4288 ShortInlineFunctions
);
4289 verifyFormat("namespace {\n"
4290 " constexpr class X {\n"
4291 " void f() { return; }\n"
4294 ShortInlineFunctions
);
4296 ShortInlineFunctions
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4297 verifyFormat("extern \"C\" {\n"
4302 ShortInlineFunctions
);
4304 Style
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4305 verifyFormat("namespace out {\n"
4309 "} // namespace in\n"
4310 "} // namespace out",
4315 "} // namespace in\n"
4316 "} // namespace out",
4319 Style
.NamespaceIndentation
= FormatStyle::NI_None
;
4320 verifyFormat("template <class T>\n"
4321 "concept a_concept = X<>;\n"
4323 "struct b_struct {};\n"
4326 verifyFormat("template <int I>\n"
4327 "constexpr void foo()\n"
4328 " requires(I == 42)\n"
4332 "} // namespace ns",
4335 FormatStyle LLVMWithCompactInnerNamespace
= getLLVMStyle();
4336 LLVMWithCompactInnerNamespace
.CompactNamespaces
= true;
4337 LLVMWithCompactInnerNamespace
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4338 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4339 "// block for debug mode\n"
4342 "}}} // namespace ns1::ns2::ns3",
4343 LLVMWithCompactInnerNamespace
);
4346 TEST_F(FormatTest
, NamespaceMacros
) {
4347 FormatStyle Style
= getLLVMStyle();
4348 Style
.NamespaceMacros
.push_back("TESTSUITE");
4350 verifyFormat("TESTSUITE(A) {\n"
4352 "} // TESTSUITE(A)",
4355 verifyFormat("TESTSUITE(A, B) {\n"
4357 "} // TESTSUITE(A)",
4360 // Properly indent according to NamespaceIndentation style
4361 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
4362 verifyFormat("TESTSUITE(A) {\n"
4364 "} // TESTSUITE(A)",
4366 verifyFormat("TESTSUITE(A) {\n"
4369 " } // namespace B\n"
4370 "} // TESTSUITE(A)",
4372 verifyFormat("namespace A {\n"
4375 " } // TESTSUITE(B)\n"
4379 Style
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4380 verifyFormat("TESTSUITE(A) {\n"
4383 "} // TESTSUITE(B)\n"
4384 "} // TESTSUITE(A)",
4386 verifyFormat("TESTSUITE(A) {\n"
4389 "} // namespace B\n"
4390 "} // TESTSUITE(A)",
4392 verifyFormat("namespace A {\n"
4395 "} // TESTSUITE(B)\n"
4399 // Properly merge namespace-macros blocks in CompactNamespaces mode
4400 Style
.NamespaceIndentation
= FormatStyle::NI_None
;
4401 Style
.CompactNamespaces
= true;
4402 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4403 "}} // TESTSUITE(A::B)",
4406 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4407 "}} // TESTSUITE(out::in)",
4408 "TESTSUITE(out) {\n"
4410 "} // TESTSUITE(in)\n"
4411 "} // TESTSUITE(out)",
4414 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4415 "}} // TESTSUITE(out::in)",
4416 "TESTSUITE(out) {\n"
4418 "} // TESTSUITE(in)\n"
4419 "} // TESTSUITE(out)",
4422 // Do not merge different namespaces/macros
4423 verifyFormat("namespace out {\n"
4425 "} // TESTSUITE(in)\n"
4426 "} // namespace out",
4428 verifyFormat("TESTSUITE(out) {\n"
4430 "} // namespace in\n"
4431 "} // TESTSUITE(out)",
4433 Style
.NamespaceMacros
.push_back("FOOBAR");
4434 verifyFormat("TESTSUITE(out) {\n"
4437 "} // TESTSUITE(out)",
4441 TEST_F(FormatTest
, FormatsCompactNamespaces
) {
4442 FormatStyle Style
= getLLVMStyle();
4443 Style
.CompactNamespaces
= true;
4444 Style
.NamespaceMacros
.push_back("TESTSUITE");
4446 verifyFormat("namespace A { namespace B {\n"
4447 "}} // namespace A::B",
4450 verifyFormat("namespace out { namespace in {\n"
4451 "}} // namespace out::in",
4454 "} // namespace in\n"
4455 "} // namespace out",
4458 // Only namespaces which have both consecutive opening and end get compacted
4459 verifyFormat("namespace out {\n"
4461 "} // namespace in1\n"
4463 "} // namespace in2\n"
4464 "} // namespace out",
4467 verifyFormat("namespace out {\n"
4471 "} // namespace in\n"
4473 "} // namespace out",
4474 "namespace out { int i;\n"
4475 "namespace in { int j; } // namespace in\n"
4476 "int k; } // namespace out",
4479 verifyFormat("namespace A { namespace B { namespace C {\n"
4480 "}}} // namespace A::B::C",
4481 "namespace A { namespace B {\n"
4483 "}} // namespace B::C\n"
4487 Style
.ColumnLimit
= 40;
4488 verifyFormat("namespace aaaaaaaaaa {\n"
4489 "namespace bbbbbbbbbb {\n"
4490 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4491 "namespace aaaaaaaaaa {\n"
4492 "namespace bbbbbbbbbb {\n"
4493 "} // namespace bbbbbbbbbb\n"
4494 "} // namespace aaaaaaaaaa",
4497 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4498 "namespace cccccc {\n"
4499 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4500 "namespace aaaaaa {\n"
4501 "namespace bbbbbb {\n"
4502 "namespace cccccc {\n"
4503 "} // namespace cccccc\n"
4504 "} // namespace bbbbbb\n"
4505 "} // namespace aaaaaa",
4507 Style
.ColumnLimit
= 80;
4509 // Extra semicolon after 'inner' closing brace prevents merging
4510 verifyFormat("namespace out { namespace in {\n"
4511 "}; } // namespace out::in",
4514 "}; // namespace in\n"
4515 "} // namespace out",
4518 // Extra semicolon after 'outer' closing brace is conserved
4519 verifyFormat("namespace out { namespace in {\n"
4520 "}}; // namespace out::in",
4523 "} // namespace in\n"
4524 "}; // namespace out",
4527 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
4528 verifyFormat("namespace out { namespace in {\n"
4530 "}} // namespace out::in",
4534 "} // namespace in\n"
4535 "} // namespace out",
4537 verifyFormat("namespace out { namespace mid {\n"
4540 " } // namespace in\n"
4542 "}} // namespace out::mid",
4543 "namespace out { namespace mid {\n"
4544 "namespace in { int j; } // namespace in\n"
4545 "int k; }} // namespace out::mid",
4548 verifyFormat("namespace A { namespace B { namespace C {\n"
4550 "}}} // namespace A::B::C\n"
4555 "namespace A { namespace B {\n"
4558 "}} // namespace B::C\n"
4559 "} // namespace A\n"
4566 verifyFormat("namespace A { namespace B { namespace C {\n"
4570 "}}} // namespace A::B::C\n"
4575 "namespace A { namespace B {\n"
4580 "}} // namespace B::C\n"
4581 "} // namespace A\n"
4588 Style
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4589 verifyFormat("namespace out { namespace in {\n"
4591 "}} // namespace out::in",
4595 "} // namespace in\n"
4596 "} // namespace out",
4598 verifyFormat("namespace out { namespace mid { namespace in {\n"
4600 "}}} // namespace out::mid::in",
4605 "} // namespace in\n"
4606 "} // namespace mid\n"
4607 "} // namespace out",
4610 Style
.CompactNamespaces
= true;
4611 Style
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
4612 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4613 Style
.BraceWrapping
.BeforeLambdaBody
= true;
4614 verifyFormat("namespace out { namespace in {\n"
4615 "}} // namespace out::in",
4617 verifyFormat("namespace out { namespace in {\n"
4618 "}} // namespace out::in",
4621 "} // namespace in\n"
4622 "} // namespace out",
4626 TEST_F(FormatTest
, FormatsExternC
) {
4627 verifyFormat("extern \"C\" {\nint a;");
4628 verifyFormat("extern \"C\" {}");
4629 verifyFormat("extern \"C\" {\n"
4632 verifyFormat("extern \"C\" int foo() {}");
4633 verifyFormat("extern \"C\" int foo();");
4634 verifyFormat("extern \"C\" int foo() {\n"
4639 FormatStyle Style
= getLLVMStyle();
4640 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4641 Style
.BraceWrapping
.AfterFunction
= true;
4642 verifyFormat("extern \"C\" int foo() {}", Style
);
4643 verifyFormat("extern \"C\" int foo();", Style
);
4644 verifyFormat("extern \"C\" int foo()\n"
4651 Style
.BraceWrapping
.AfterExternBlock
= true;
4652 Style
.BraceWrapping
.SplitEmptyRecord
= false;
4653 verifyFormat("extern \"C\"\n"
4656 verifyFormat("extern \"C\"\n"
4663 TEST_F(FormatTest
, IndentExternBlockStyle
) {
4664 FormatStyle Style
= getLLVMStyle();
4665 Style
.IndentWidth
= 2;
4667 Style
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4668 verifyFormat("extern \"C\" { /*9*/\n"
4671 verifyFormat("extern \"C\" {\n"
4676 Style
.IndentExternBlock
= FormatStyle::IEBS_NoIndent
;
4677 verifyFormat("extern \"C\" { /*11*/\n"
4680 verifyFormat("extern \"C\" {\n"
4685 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
4686 verifyFormat("extern \"C\"\n"
4692 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4693 Style
.BraceWrapping
.AfterExternBlock
= true;
4694 Style
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4695 verifyFormat("extern \"C\"\n"
4699 verifyFormat("extern \"C\"\n{\n"
4704 Style
.BraceWrapping
.AfterExternBlock
= false;
4705 Style
.IndentExternBlock
= FormatStyle::IEBS_NoIndent
;
4706 verifyFormat("extern \"C\" { /*15*/\n"
4709 verifyFormat("extern \"C\" {\n"
4714 Style
.BraceWrapping
.AfterExternBlock
= true;
4715 verifyFormat("extern \"C\"\n"
4719 verifyFormat("extern \"C\"\n"
4725 Style
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4726 verifyFormat("extern \"C\"\n"
4730 verifyFormat("extern \"C\"\n"
4737 TEST_F(FormatTest
, FormatsInlineASM
) {
4738 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4739 verifyFormat("asm(\"nop\" ::: \"memory\");");
4741 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4742 " \"cpuid\\n\\t\"\n"
4743 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4744 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4745 " : \"a\"(value));");
4747 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4749 " mov edx,[that] // vtable in edx\n"
4750 " mov eax,methodIndex\n"
4751 " call [edx][eax*4] // stdcall\n"
4754 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4756 " mov edx,[that] // vtable in edx\n"
4757 " mov eax,methodIndex\n"
4758 " call [edx][eax*4] // stdcall\n"
4761 verifyNoChange("_asm {\n"
4765 verifyFormat("void function() {\n"
4769 verifyFormat("__asm {\n"
4776 auto Style
= getLLVMStyleWithColumns(0);
4777 const StringRef Code1
{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4778 const StringRef Code2
{"asm(\"xyz\"\n"
4779 " : \"=a\"(a), \"=d\"(b)\n"
4780 " : \"a\"(data));"};
4781 const StringRef Code3
{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4782 " : \"a\"(data));"};
4784 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_OnlyMultiline
;
4785 verifyFormat(Code1
, Style
);
4786 verifyNoChange(Code2
, Style
);
4787 verifyNoChange(Code3
, Style
);
4789 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Always
;
4790 verifyFormat(Code2
, Code1
, Style
);
4791 verifyNoChange(Code2
, Style
);
4792 verifyFormat(Code2
, Code3
, Style
);
4795 TEST_F(FormatTest
, FormatTryCatch
) {
4796 verifyFormat("try {\n"
4798 "} catch (int a) {\n"
4804 // Function-level try statements.
4805 verifyFormat("int f() try { return 4; } catch (...) {\n"
4808 verifyFormat("class A {\n"
4810 " A() try : a(0) {\n"
4811 " } catch (...) {\n"
4815 verifyFormat("class A {\n"
4817 " A() try : a(0), b{1} {\n"
4818 " } catch (...) {\n"
4822 verifyFormat("class A {\n"
4824 " A() try : a(0), b{1}, c{2} {\n"
4825 " } catch (...) {\n"
4829 verifyFormat("class A {\n"
4831 " A() try : a(0), b{1}, c{2} {\n"
4832 " { // New scope.\n"
4834 " } catch (...) {\n"
4839 // Incomplete try-catch blocks.
4840 verifyIncompleteFormat("try {} catch (");
4843 TEST_F(FormatTest
, FormatTryAsAVariable
) {
4844 verifyFormat("int try;");
4845 verifyFormat("int try, size;");
4846 verifyFormat("try = foo();");
4847 verifyFormat("if (try < size) {\n return true;\n}");
4849 verifyFormat("int catch;");
4850 verifyFormat("int catch, size;");
4851 verifyFormat("catch = foo();");
4852 verifyFormat("if (catch < size) {\n return true;\n}");
4854 FormatStyle Style
= getLLVMStyle();
4855 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4856 Style
.BraceWrapping
.AfterFunction
= true;
4857 Style
.BraceWrapping
.BeforeCatch
= true;
4858 verifyFormat("try {\n"
4865 verifyFormat("#if NO_EX\n"
4874 verifyFormat("try /* abc */ {\n"
4881 verifyFormat("try\n"
4892 TEST_F(FormatTest
, FormatSEHTryCatch
) {
4893 verifyFormat("__try {\n"
4895 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4899 verifyFormat("__try {\n"
4905 verifyFormat("DEBUG({\n"
4912 TEST_F(FormatTest
, IncompleteTryCatchBlocks
) {
4913 verifyFormat("try {\n"
4918 verifyFormat("try {\n"
4920 "} catch (A a) MACRO(x) {\n"
4922 "} catch (B b) MACRO(x) {\n"
4927 TEST_F(FormatTest
, FormatTryCatchBraceStyles
) {
4928 FormatStyle Style
= getLLVMStyle();
4929 for (auto BraceStyle
: {FormatStyle::BS_Attach
, FormatStyle::BS_Mozilla
,
4930 FormatStyle::BS_WebKit
}) {
4931 Style
.BreakBeforeBraces
= BraceStyle
;
4932 verifyFormat("try {\n"
4939 Style
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
4940 verifyFormat("try {\n"
4947 verifyFormat("__try {\n"
4954 verifyFormat("@try {\n"
4961 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
4962 verifyFormat("try\n"
4971 Style
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
4972 verifyFormat("try\n"
4974 " // something white\n"
4978 " // something white\n"
4981 Style
.BreakBeforeBraces
= FormatStyle::BS_GNU
;
4982 verifyFormat("try\n"
4991 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4992 Style
.BraceWrapping
.BeforeCatch
= true;
4993 verifyFormat("try {\n"
5002 TEST_F(FormatTest
, StaticInitializers
) {
5003 verifyFormat("static SomeClass SC = {1, 'a'};");
5005 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
5007 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
5009 // Here, everything other than the "}" would fit on a line.
5010 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
5011 " 10000000000000000000000000};");
5012 verifyFormat("S s = {a,\n"
5021 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
5022 // line. However, the formatting looks a bit off and this probably doesn't
5023 // happen often in practice.
5024 verifyFormat("static int Variable[1] = {\n"
5025 " {1000000000000000000000000000000000000}};",
5026 getLLVMStyleWithColumns(40));
5029 TEST_F(FormatTest
, DesignatedInitializers
) {
5030 verifyFormat("const struct A a = {.a = 1, .b = 2};");
5031 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
5032 " .bbbbbbbbbb = 2,\n"
5033 " .cccccccccc = 3,\n"
5034 " .dddddddddd = 4,\n"
5035 " .eeeeeeeeee = 5};");
5036 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
5038 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
5039 " .ccccccccccccccccccccccccccc = 3,\n"
5040 " .ddddddddddddddddddddddddddd = 4,\n"
5041 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
5043 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
5045 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
5046 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
5047 " [2] = bbbbbbbbbb,\n"
5048 " [3] = cccccccccc,\n"
5049 " [4] = dddddddddd,\n"
5050 " [5] = eeeeeeeeee};");
5051 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
5052 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5054 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
5055 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
5056 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5058 verifyFormat("for (const TestCase &test_case : {\n"
5071 TEST_F(FormatTest
, BracedInitializerIndentWidth
) {
5072 auto Style
= getLLVMStyleWithColumns(60);
5073 Style
.BinPackArguments
= true;
5074 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
5075 Style
.BracedInitializerIndentWidth
= 6;
5077 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
5078 verifyFormat("enum class {\n"
5083 verifyFormat("class Foo {\n"
5088 verifyFormat("void foo() {\n"
5089 " auto bar = baz;\n"
5093 verifyFormat("auto foo = [&] {\n"
5094 " auto bar = baz;\n"
5099 " auto bar = baz;\n"
5103 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
5104 verifyFormat("SomeClass clazz(\n"
5105 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
5106 " \"zzzzzzzzzzzzzzzzzz\");",
5109 // The following types of initialization are all affected by
5110 // BracedInitializerIndentWidth. Aggregate initialization.
5111 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5112 " 10000000, 20000000};",
5114 verifyFormat("SomeStruct s{\n"
5115 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
5116 " \"zzzzzzzzzzzzzzzz\"};",
5118 // Designated initializers.
5119 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
5120 " [0] = 10000000, [1] = 20000000};",
5122 verifyFormat("SomeStruct s{\n"
5123 " .foo = \"xxxxxxxxxxxxx\",\n"
5124 " .bar = \"yyyyyyyyyyyyy\",\n"
5125 " .baz = \"zzzzzzzzzzzzz\"};",
5127 // List initialization.
5128 verifyFormat("SomeStruct s{\n"
5129 " \"xxxxxxxxxxxxx\",\n"
5130 " \"yyyyyyyyyyyyy\",\n"
5131 " \"zzzzzzzzzzzzz\",\n"
5134 verifyFormat("SomeStruct{\n"
5135 " \"xxxxxxxxxxxxx\",\n"
5136 " \"yyyyyyyyyyyyy\",\n"
5137 " \"zzzzzzzzzzzzz\",\n"
5140 verifyFormat("new SomeStruct{\n"
5141 " \"xxxxxxxxxxxxx\",\n"
5142 " \"yyyyyyyyyyyyy\",\n"
5143 " \"zzzzzzzzzzzzz\",\n"
5146 // Member initializer.
5147 verifyFormat("class SomeClass {\n"
5149 " \"xxxxxxxxxxxxx\",\n"
5150 " \"yyyyyyyyyyyyy\",\n"
5151 " \"zzzzzzzzzzzzz\",\n"
5155 // Constructor member initializer.
5156 verifyFormat("SomeClass::SomeClass : strct{\n"
5157 " \"xxxxxxxxxxxxx\",\n"
5158 " \"yyyyyyyyyyyyy\",\n"
5159 " \"zzzzzzzzzzzzz\",\n"
5162 // Copy initialization.
5163 verifyFormat("SomeStruct s = SomeStruct{\n"
5164 " \"xxxxxxxxxxxxx\",\n"
5165 " \"yyyyyyyyyyyyy\",\n"
5166 " \"zzzzzzzzzzzzz\",\n"
5169 // Copy list initialization.
5170 verifyFormat("SomeStruct s = {\n"
5171 " \"xxxxxxxxxxxxx\",\n"
5172 " \"yyyyyyyyyyyyy\",\n"
5173 " \"zzzzzzzzzzzzz\",\n"
5176 // Assignment operand initialization.
5177 verifyFormat("s = {\n"
5178 " \"xxxxxxxxxxxxx\",\n"
5179 " \"yyyyyyyyyyyyy\",\n"
5180 " \"zzzzzzzzzzzzz\",\n"
5183 // Returned object initialization.
5184 verifyFormat("return {\n"
5185 " \"xxxxxxxxxxxxx\",\n"
5186 " \"yyyyyyyyyyyyy\",\n"
5187 " \"zzzzzzzzzzzzz\",\n"
5190 // Initializer list.
5191 verifyFormat("auto initializerList = {\n"
5192 " \"xxxxxxxxxxxxx\",\n"
5193 " \"yyyyyyyyyyyyy\",\n"
5194 " \"zzzzzzzzzzzzz\",\n"
5197 // Function parameter initialization.
5198 verifyFormat("func({\n"
5199 " \"xxxxxxxxxxxxx\",\n"
5200 " \"yyyyyyyyyyyyy\",\n"
5201 " \"zzzzzzzzzzzzz\",\n"
5204 // Nested init lists.
5205 verifyFormat("SomeStruct s = {\n"
5206 " {{init1, init2, init3, init4, init5},\n"
5207 " {init1, init2, init3, init4, init5}}};",
5209 verifyFormat("SomeStruct s = {\n"
5217 " {init1, init2, init3, init4, init5}}};",
5219 verifyFormat("SomeArrayT a[3] = {\n"
5231 verifyFormat("SomeArrayT a[3] = {\n"
5249 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5250 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
5251 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5252 " \"zzzzzzzzzzzzz\"};",
5256 TEST_F(FormatTest
, NestedStaticInitializers
) {
5257 verifyFormat("static A x = {{{}}};");
5258 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5259 " {init1, init2, init3, init4}}};",
5260 getLLVMStyleWithColumns(50));
5262 verifyFormat("somes Status::global_reps[3] = {\n"
5263 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5264 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5265 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5266 getLLVMStyleWithColumns(60));
5267 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5268 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5269 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5270 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5271 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5272 " {rect.fRight - rect.fLeft, rect.fBottom - "
5276 "SomeArrayOfSomeType a = {\n"
5279 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5280 " 333333333333333333333333333333},\n"
5284 "SomeArrayOfSomeType a = {\n"
5287 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5288 " 333333333333333333333333333333}},\n"
5292 verifyFormat("struct {\n"
5294 " const char *const name;\n"
5295 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5296 " {kOsWin, \"Windows\"},\n"
5297 " {kOsLinux, \"Linux\"},\n"
5298 " {kOsCrOS, \"Chrome OS\"}};");
5299 verifyFormat("struct {\n"
5301 " const char *const name;\n"
5302 "} kBitsToOs[] = {\n"
5303 " {kOsMac, \"Mac\"},\n"
5304 " {kOsWin, \"Windows\"},\n"
5305 " {kOsLinux, \"Linux\"},\n"
5306 " {kOsCrOS, \"Chrome OS\"},\n"
5310 TEST_F(FormatTest
, FormatsSmallMacroDefinitionsInSingleLine
) {
5311 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5313 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5316 TEST_F(FormatTest
, DoesNotBreakPureVirtualFunctionDefinition
) {
5317 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5318 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5320 // Do break defaulted and deleted functions.
5321 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5323 getLLVMStyleWithColumns(40));
5324 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5326 getLLVMStyleWithColumns(40));
5329 TEST_F(FormatTest
, BreaksStringLiteralsOnlyInDefine
) {
5330 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5331 getLLVMStyleWithColumns(40));
5332 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5333 getLLVMStyleWithColumns(40));
5334 verifyFormat("#define Q \\\n"
5335 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5336 " \"aaaaaaaa.cpp\"",
5337 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5338 getLLVMStyleWithColumns(40));
5341 TEST_F(FormatTest
, UnderstandsLinePPDirective
) {
5342 verifyFormat("# 123 \"A string literal\"",
5343 " # 123 \"A string literal\"");
5346 TEST_F(FormatTest
, LayoutUnknownPPDirective
) {
5348 verifyFormat("#\n;\n;\n;");
5351 TEST_F(FormatTest
, UnescapedEndOfLineEndsPPDirective
) {
5352 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5353 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5354 getLLVMStyleWithColumns(12));
5357 TEST_F(FormatTest
, EndOfFileEndsPPDirective
) {
5358 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5359 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5362 TEST_F(FormatTest
, DoesntRemoveUnknownTokens
) {
5363 verifyFormat("#define A \\x20");
5364 verifyFormat("#define A \\ x20");
5365 verifyFormat("#define A \\ x20", "#define A \\ x20");
5366 verifyFormat("#define A ''");
5367 verifyFormat("#define A ''qqq");
5368 verifyFormat("#define A `qqq");
5369 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5370 verifyFormat("const char *c = STRINGIFY(\n"
5372 "const char * c = STRINGIFY(\n"
5375 verifyFormat("a\r\\");
5376 verifyFormat("a\v\\");
5377 verifyFormat("a\f\\");
5380 TEST_F(FormatTest
, IndentsPPDirectiveWithPPIndentWidth
) {
5381 FormatStyle style
= getChromiumStyle(FormatStyle::LK_Cpp
);
5382 style
.IndentWidth
= 4;
5383 style
.PPIndentWidth
= 1;
5385 style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
5386 verifyFormat("#ifdef __linux__\n"
5397 style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
5398 verifyFormat("#ifdef __linux__\n"
5402 "# define FOO foo\n"
5409 style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
5410 verifyFormat("#ifdef __linux__\n"
5414 " #define FOO foo\n"
5420 verifyFormat("#if 1\n"
5421 " // some comments\n"
5424 "// not a define comment\n"
5430 "// some comments\n"
5433 "// not a define comment\n"
5440 style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
5441 verifyFormat("#ifdef foo\n"
5442 "#define bar() \\\n"
5449 verifyFormat("if (emacs) {\n"
5453 " return duh(); \\\n"
5458 verifyFormat("#if abc\n"
5460 "#define bar() \\\n"
5470 verifyFormat("#ifndef foo\n"
5476 " return duh(); \\\n"
5482 verifyFormat("#if 1\n"
5490 verifyFormat("#define X \\\n"
5497 style
.PPIndentWidth
= 2;
5498 verifyFormat("#ifdef foo\n"
5499 "#define bar() \\\n"
5506 style
.IndentWidth
= 8;
5507 verifyFormat("#ifdef foo\n"
5508 "#define bar() \\\n"
5516 style
.IndentWidth
= 1;
5517 style
.PPIndentWidth
= 4;
5518 verifyFormat("#if 1\n"
5526 verifyFormat("#define X \\\n"
5533 style
.IndentWidth
= 4;
5534 style
.PPIndentWidth
= 1;
5535 style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
5536 verifyFormat("#ifdef foo\n"
5537 "# define bar() \\\n"
5544 verifyFormat("#if abc\n"
5546 "# define bar() \\\n"
5556 verifyFormat("#ifndef foo\n"
5562 " return duh(); \\\n"
5568 verifyFormat("#define X \\\n"
5575 style
.PPIndentWidth
= 2;
5576 style
.IndentWidth
= 8;
5577 verifyFormat("#ifdef foo\n"
5578 "# define bar() \\\n"
5586 style
.PPIndentWidth
= 4;
5587 style
.IndentWidth
= 1;
5588 verifyFormat("#define X \\\n"
5595 style
.IndentWidth
= 4;
5596 style
.PPIndentWidth
= 1;
5597 style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
5598 verifyFormat("if (emacs) {\n"
5602 " return duh(); \\\n"
5607 verifyFormat("#if abc\n"
5609 " #define bar() \\\n"
5617 verifyFormat("#if 1\n"
5626 style
.PPIndentWidth
= 2;
5627 verifyFormat("#ifdef foo\n"
5628 " #define bar() \\\n"
5636 style
.PPIndentWidth
= 4;
5637 style
.IndentWidth
= 1;
5638 verifyFormat("#if 1\n"
5648 TEST_F(FormatTest
, IndentsPPDirectiveInReducedSpace
) {
5649 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5650 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5651 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5652 // FIXME: We never break before the macro name.
5653 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5655 verifyFormat("#define A A\n#define A A");
5656 verifyFormat("#define A(X) A\n#define A A");
5658 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5659 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5662 TEST_F(FormatTest
, HandlePreprocessorDirectiveContext
) {
5663 verifyFormat("// somecomment\n"
5664 "#include \"a.h\"\n"
5667 "#include \"b.h\"\n"
5670 " #include \"a.h\"\n"
5673 " #include \"b.h\"\n"
5675 getLLVMStyleWithColumns(13));
5678 TEST_F(FormatTest
, LayoutSingleHash
) { verifyFormat("#\na;"); }
5680 TEST_F(FormatTest
, LayoutCodeInMacroDefinitions
) {
5681 verifyFormat("#define A \\\n"
5687 getLLVMStyleWithColumns(14));
5690 TEST_F(FormatTest
, LayoutRemainingTokens
) {
5695 TEST_F(FormatTest
, MacroDefinitionInsideStatement
) {
5696 verifyFormat("int x,\n"
5699 "int x,\n#define A\ny;");
5702 TEST_F(FormatTest
, HashInMacroDefinition
) {
5703 verifyFormat("#define A(c) L#c");
5704 verifyFormat("#define A(c) u#c");
5705 verifyFormat("#define A(c) U#c");
5706 verifyFormat("#define A(c) u8#c");
5707 verifyFormat("#define A(c) LR#c");
5708 verifyFormat("#define A(c) uR#c");
5709 verifyFormat("#define A(c) UR#c");
5710 verifyFormat("#define A(c) u8R#c");
5711 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5712 verifyFormat("#define A \\\n"
5716 getLLVMStyleWithColumns(11));
5718 verifyFormat("#define A(X) \\\n"
5719 " void function##X()",
5720 getLLVMStyleWithColumns(22));
5722 verifyFormat("#define A(a, b, c) \\\n"
5724 getLLVMStyleWithColumns(22));
5726 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5729 TEST_F(FormatTest
, RespectWhitespaceInMacroDefinitions
) {
5730 verifyFormat("#define A (x)");
5731 verifyFormat("#define A(x)");
5733 FormatStyle Style
= getLLVMStyle();
5734 Style
.SpaceBeforeParens
= FormatStyle::SBPO_Never
;
5735 verifyFormat("#define true ((foo)1)", Style
);
5736 Style
.SpaceBeforeParens
= FormatStyle::SBPO_Always
;
5737 verifyFormat("#define false((foo)0)", Style
);
5740 TEST_F(FormatTest
, EmptyLinesInMacroDefinitions
) {
5741 verifyFormat("#define A b;",
5745 getLLVMStyleWithColumns(25));
5746 verifyNoChange("#define A \\\n"
5750 getLLVMStyleWithColumns(11));
5751 verifyNoChange("#define A \\\n"
5755 getLLVMStyleWithColumns(11));
5758 TEST_F(FormatTest
, MacroDefinitionsWithIncompleteCode
) {
5759 verifyIncompleteFormat("#define A :");
5760 verifyFormat("#define SOMECASES \\\n"
5763 getLLVMStyleWithColumns(20));
5764 verifyFormat("#define MACRO(a) \\\n"
5769 getLLVMStyleWithColumns(18));
5770 verifyFormat("#define A template <typename T>");
5771 verifyIncompleteFormat("#define STR(x) #x\n"
5772 "f(STR(this_is_a_string_literal{));");
5773 verifyFormat("#pragma omp threadprivate( \\\n"
5774 " y)), // expected-warning",
5775 getLLVMStyleWithColumns(28));
5776 verifyFormat("#d, = };");
5777 verifyFormat("#if \"a");
5778 verifyIncompleteFormat("({\n"
5783 getLLVMStyleWithColumns(15));
5784 verifyFormat("#define A \\\n"
5790 getLLVMStyleWithColumns(15));
5791 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5792 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5793 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5794 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5795 verifyNoCrash("#else\n"
5799 verifyNoCrash("#else\n"
5803 verifyNoCrash("#else\n"
5807 verifyNoCrash("#if X\n"
5812 verifyNoCrash("#if X\n"
5817 verifyNoCrash("#endif\n"
5819 verifyNoCrash("#endif\n"
5821 verifyNoCrash("#endif\n"
5825 TEST_F(FormatTest
, MacrosWithoutTrailingSemicolon
) {
5826 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5827 verifyFormat("class A : public QObject {\n"
5832 "class A : public QObject {\n"
5837 verifyFormat("MACRO\n"
5838 "/*static*/ int i;",
5840 " /*static*/ int i;");
5841 verifyFormat("SOME_MACRO\n"
5849 // Only if the identifier contains at least 5 characters.
5850 verifyFormat("HTTP f();", "HTTP\nf();");
5851 verifyNoChange("MACRO\nf();");
5852 // Only if everything is upper case.
5853 verifyFormat("class A : public QObject {\n"
5854 " Q_Object A() {}\n"
5856 "class A : public QObject {\n"
5861 // Only if the next line can actually start an unwrapped line.
5862 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5865 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5867 getChromiumStyle(FormatStyle::LK_Cpp
));
5870 verifyNoChange("/**/ FOO(a)\n"
5874 TEST_F(FormatTest
, MacroCallsWithoutTrailingSemicolon
) {
5875 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5876 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5877 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5879 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5880 "int *createScopDetectionPass() { return 0; }",
5881 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5882 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5883 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5885 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5886 " int *createScopDetectionPass() { return 0; }");
5887 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5888 // braces, so that inner block is indented one level more.
5889 verifyFormat("int q() {\n"
5890 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5891 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5892 " IPC_END_MESSAGE_MAP()\n"
5895 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5896 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5897 " IPC_END_MESSAGE_MAP()\n"
5900 // Same inside macros.
5901 verifyFormat("#define LIST(L) \\\n"
5905 "#define LIST(L) \\\n"
5911 // These must not be recognized as macros.
5912 verifyFormat("int q() {\n"
5930 " LOG(INFO) << x;\n"
5931 " ifstream(x) >> x;\n"
5951 " LOG(INFO)\n << x;\n"
5952 " ifstream(x)\n >> x;\n"
5954 verifyFormat("int q() {\n"
5966 " } catch (...) {\n"
5977 "try { Q(); } catch (...) {}\n"
5979 verifyFormat("class A {\n"
5981 " A(int i) noexcept() : {}\n"
5982 " A(X x)\n" // FIXME: function-level try blocks are broken.
5984 " } catch (...) {\n"
5988 " A()\n : t(0) {}\n"
5989 " A(int i)\n noexcept() : {}\n"
5991 " try : t(0) {} catch (...) {}\n"
5993 FormatStyle Style
= getLLVMStyle();
5994 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
5995 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
5996 Style
.BraceWrapping
.AfterFunction
= true;
5997 verifyFormat("void f()\n"
6004 verifyFormat("class SomeClass {\n"
6006 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6008 "class SomeClass {\n"
6011 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6013 verifyFormat("class SomeClass {\n"
6016 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6018 "class SomeClass {\n"
6021 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6023 getLLVMStyleWithColumns(40));
6025 verifyFormat("MACRO(>)");
6027 // Some macros contain an implicit semicolon.
6028 Style
= getLLVMStyle();
6029 Style
.StatementMacros
.push_back("FOO");
6030 verifyFormat("FOO(a) int b = 0;");
6031 verifyFormat("FOO(a)\n"
6034 verifyFormat("FOO(a);\n"
6037 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6040 verifyFormat("FOO()\n"
6043 verifyFormat("FOO\n"
6046 verifyFormat("void f() {\n"
6051 verifyFormat("FOO(a)\n"
6054 verifyFormat("int a = 0;\n"
6058 verifyFormat("int a = 0;\n"
6062 verifyFormat("void foo(int a) { FOO(a) }\n"
6063 "uint32_t bar() {}",
6067 TEST_F(FormatTest
, FormatsMacrosWithZeroColumnWidth
) {
6068 FormatStyle ZeroColumn
= getLLVMStyleWithColumns(0);
6070 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6074 TEST_F(FormatTest
, LayoutMacroDefinitionsStatementsSpanningBlocks
) {
6075 verifyFormat("#define A \\\n"
6079 getLLVMStyleWithColumns(11));
6082 TEST_F(FormatTest
, IndentPreprocessorDirectives
) {
6083 FormatStyle Style
= getLLVMStyleWithColumns(40);
6084 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
6085 verifyFormat("#ifdef _WIN32\n"
6089 "#include <someheader.h>\n"
6090 "#define MACRO \\\n"
6091 " some_very_long_func_aaaaaaaaaa();\n"
6097 Style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
6098 verifyFormat("#if 1\n"
6099 "# define __STR(x) #x\n"
6102 verifyFormat("#ifdef _WIN32\n"
6106 "# include <someheader.h>\n"
6107 "# define MACRO \\\n"
6108 " some_very_long_func_aaaaaaaaaa();\n"
6114 verifyFormat("#if A\n"
6115 "# define MACRO \\\n"
6116 " void a(int x) { \\\n"
6125 // Comments before include guard.
6126 verifyFormat("// file comment\n"
6128 "#ifndef HEADER_H\n"
6129 "#define HEADER_H\n"
6133 // Test with include guards.
6134 verifyFormat("#ifndef HEADER_H\n"
6135 "#define HEADER_H\n"
6139 // Include guards must have a #define with the same variable immediately
6141 verifyFormat("#ifndef NOT_GUARD\n"
6147 // Include guards must cover the entire file.
6148 verifyFormat("code();\n"
6150 "#ifndef NOT_GUARD\n"
6151 "# define NOT_GUARD\n"
6155 verifyFormat("#ifndef NOT_GUARD\n"
6156 "# define NOT_GUARD\n"
6161 // Test with trailing blank lines.
6162 verifyFormat("#ifndef HEADER_H\n"
6163 "#define HEADER_H\n"
6167 // Include guards don't have #else.
6168 verifyFormat("#ifndef NOT_GUARD\n"
6169 "# define NOT_GUARD\n"
6174 verifyFormat("#ifndef NOT_GUARD\n"
6175 "# define NOT_GUARD\n"
6180 // Non-identifier #define after potential include guard.
6181 verifyFormat("#ifndef FOO\n"
6185 // #if closes past last non-preprocessor line.
6186 verifyFormat("#ifndef FOO\n"
6194 // Don't crash if there is an #elif directive without a condition.
6195 verifyFormat("#if 1\n"
6203 // FIXME: This doesn't handle the case where there's code between the
6204 // #ifndef and #define but all other conditions hold. This is because when
6205 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6206 // previous code line yet, so we can't detect it.
6207 verifyFormat("#ifndef NOT_GUARD\n"
6209 "#define NOT_GUARD\n"
6212 "#ifndef NOT_GUARD\n"
6214 "# define NOT_GUARD\n"
6218 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6219 // be outside an include guard. Examples are #pragma once and
6220 // #pragma GCC diagnostic, or anything else that does not change the meaning
6221 // of the file if it's included multiple times.
6222 verifyFormat("#ifdef WIN32\n"
6225 "#ifndef HEADER_H\n"
6226 "# define HEADER_H\n"
6232 "#ifndef HEADER_H\n"
6233 "#define HEADER_H\n"
6237 // FIXME: This does not detect when there is a single non-preprocessor line
6238 // in front of an include-guard-like structure where other conditions hold
6239 // because ScopedLineState hides the line.
6240 verifyFormat("code();\n"
6241 "#ifndef HEADER_H\n"
6242 "#define HEADER_H\n"
6246 "#ifndef HEADER_H\n"
6247 "# define HEADER_H\n"
6251 // Keep comments aligned with #, otherwise indent comments normally. These
6252 // tests cannot use verifyFormat because messUp manipulates leading
6255 const char *Expected
= ""
6258 "// Preprocessor aligned.\n"
6260 " // Code. Separated by blank line.\n"
6263 " // Code. Not aligned with #\n"
6266 const char *ToFormat
= ""
6269 "// Preprocessor aligned.\n"
6271 "// Code. Separated by blank line.\n"
6274 " // Code. Not aligned with #\n"
6277 verifyFormat(Expected
, ToFormat
, Style
);
6278 verifyNoChange(Expected
, Style
);
6280 // Keep block quotes aligned.
6282 const char *Expected
= ""
6285 "/* Preprocessor aligned. */\n"
6287 " /* Code. Separated by blank line. */\n"
6290 " /* Code. Not aligned with # */\n"
6293 const char *ToFormat
= ""
6296 "/* Preprocessor aligned. */\n"
6298 "/* Code. Separated by blank line. */\n"
6301 " /* Code. Not aligned with # */\n"
6304 verifyFormat(Expected
, ToFormat
, Style
);
6305 verifyNoChange(Expected
, Style
);
6307 // Keep comments aligned with un-indented directives.
6309 const char *Expected
= ""
6311 "// Preprocessor aligned.\n"
6313 " // Code. Separated by blank line.\n"
6316 " // Code. Not aligned with #\n"
6318 const char *ToFormat
= ""
6320 "// Preprocessor aligned.\n"
6322 "// Code. Separated by blank line.\n"
6325 " // Code. Not aligned with #\n"
6327 verifyFormat(Expected
, ToFormat
, Style
);
6328 verifyNoChange(Expected
, Style
);
6330 // Test AfterHash with tabs.
6332 FormatStyle Tabbed
= Style
;
6333 Tabbed
.UseTab
= FormatStyle::UT_Always
;
6334 Tabbed
.IndentWidth
= 8;
6335 Tabbed
.TabWidth
= 8;
6336 verifyFormat("#ifdef _WIN32\n"
6340 "#\t\tinclude <someheader.h>\n"
6341 "#\t\tdefine MACRO \\\n"
6342 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6350 // Regression test: Multiline-macro inside include guards.
6351 verifyFormat("#ifndef HEADER_H\n"
6352 "#define HEADER_H\n"
6356 "#endif // HEADER_H",
6357 getLLVMStyleWithColumns(20));
6359 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
6360 // Basic before hash indent tests
6361 verifyFormat("#ifdef _WIN32\n"
6365 " #include <someheader.h>\n"
6366 " #define MACRO \\\n"
6367 " some_very_long_func_aaaaaaaaaa();\n"
6373 verifyFormat("#if A\n"
6374 " #define MACRO \\\n"
6375 " void a(int x) { \\\n"
6384 // Keep comments aligned with indented directives. These
6385 // tests cannot use verifyFormat because messUp manipulates leading
6388 const char *Expected
= "void f() {\n"
6389 "// Aligned to preprocessor.\n"
6391 " // Aligned to code.\n"
6394 " // Aligned to preprocessor.\n"
6396 " // Aligned to code.\n"
6401 const char *ToFormat
= "void f() {\n"
6402 "// Aligned to preprocessor.\n"
6404 "// Aligned to code.\n"
6407 "// Aligned to preprocessor.\n"
6409 "// Aligned to code.\n"
6414 verifyFormat(Expected
, ToFormat
, Style
);
6415 verifyNoChange(Expected
, Style
);
6418 const char *Expected
= "void f() {\n"
6419 "/* Aligned to preprocessor. */\n"
6421 " /* Aligned to code. */\n"
6424 " /* Aligned to preprocessor. */\n"
6426 " /* Aligned to code. */\n"
6431 const char *ToFormat
= "void f() {\n"
6432 "/* Aligned to preprocessor. */\n"
6434 "/* Aligned to code. */\n"
6437 "/* Aligned to preprocessor. */\n"
6439 "/* Aligned to code. */\n"
6444 verifyFormat(Expected
, ToFormat
, Style
);
6445 verifyNoChange(Expected
, Style
);
6448 // Test single comment before preprocessor
6449 verifyFormat("// Comment\n"
6456 TEST_F(FormatTest
, FormatAlignInsidePreprocessorElseBlock
) {
6457 FormatStyle Style
= getLLVMStyle();
6458 Style
.AlignConsecutiveAssignments
.Enabled
= true;
6459 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
6461 // Test with just #if blocks.
6462 verifyFormat("void f1() {\n"
6465 " int foobar = 2;\n"
6473 " char *foobarbaz = \"foobarbaz\";\n"
6478 // Test with just #else blocks.
6479 verifyFormat("void f1() {\n"
6483 " int foobar = 2;\n"
6493 " char *foobarbaz = \"foobarbaz\";\n"
6497 verifyFormat("auto foo = [] { return; };\n"
6505 // Test with a mix of #if and #else blocks.
6506 verifyFormat("void f1() {\n"
6510 " int foobar = 2;\n"
6519 " // prevent alignment with #else in f1\n"
6520 " char *foobarbaz = \"foobarbaz\";\n"
6525 // Test with nested #if and #else blocks.
6526 verifyFormat("void f1() {\n"
6532 " int foobar = 2;\n"
6546 " // prevent alignment with #else in f1\n"
6547 " char *foobarbaz = \"foobarbaz\";\n"
6554 verifyFormat("#if FOO\n"
6566 verifyFormat("void f() {\n"
6577 " bool abcd = true;\n"
6583 verifyFormat("void f() {\n"
6600 TEST_F(FormatTest
, FormatHashIfNotAtStartOfLine
) {
6608 TEST_F(FormatTest
, FormatUnbalancedStructuralElements
) {
6609 verifyFormat("#define A \\\n { \\\n {\nint i;",
6610 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6611 verifyFormat("#define A \\\n } \\\n }\nint i;",
6612 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6615 TEST_F(FormatTest
, EscapedNewlines
) {
6616 FormatStyle Narrow
= getLLVMStyleWithColumns(11);
6617 verifyFormat("#define A \\\n int i; \\\n int j;",
6618 "#define A \\\nint i;\\\n int j;", Narrow
);
6619 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6620 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6621 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6622 verifyNoChange("<a\n\\\\\n>");
6624 FormatStyle AlignLeft
= getLLVMStyle();
6625 AlignLeft
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
6626 verifyFormat("#define MACRO(x) \\\n"
6631 // CRLF line endings
6632 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6633 "#define A \\\r\nint i;\\\r\n int j;", Narrow
);
6634 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6635 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6636 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6637 verifyNoChange("<a\r\n\\\\\r\n>");
6638 verifyFormat("#define MACRO(x) \\\r\n"
6643 constexpr StringRef Code
{"#define A \\\n"
6647 verifyFormat(Code
, AlignLeft
);
6649 constexpr StringRef Code2
{"#define A \\\n"
6653 auto LastLine
= getLLVMStyle();
6654 LastLine
.AlignEscapedNewlines
= FormatStyle::ENAS_LeftWithLastLine
;
6655 verifyFormat(Code2
, LastLine
);
6657 LastLine
.ColumnLimit
= 13;
6658 verifyFormat(Code
, LastLine
);
6660 LastLine
.ColumnLimit
= 0;
6661 verifyFormat(Code2
, LastLine
);
6663 FormatStyle DontAlign
= getLLVMStyle();
6664 DontAlign
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
6665 DontAlign
.MaxEmptyLinesToKeep
= 3;
6666 // FIXME: can't use verifyFormat here because the newline before
6667 // "public:" is not inserted the first time it's reformatted
6668 verifyNoChange("#define A \\\n"
6680 TEST_F(FormatTest
, CalculateSpaceOnConsecutiveLinesInMacro
) {
6681 verifyFormat("#define A \\\n"
6685 getLLVMStyleWithColumns(11));
6688 TEST_F(FormatTest
, MixingPreprocessorDirectivesAndNormalCode
) {
6689 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6691 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6693 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6694 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6695 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6697 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6699 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6700 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6703 TEST_F(FormatTest
, LayoutStatementsAroundPreprocessorDirectives
) {
6704 verifyFormat("int\n"
6707 "int\n#define A\na;");
6708 verifyFormat("functionCallTo(\n"
6709 " someOtherFunction(\n"
6710 " withSomeParameters, whichInSequence,\n"
6711 " areLongerThanALine(andAnotherCall,\n"
6713 " withMoreParamters,\n"
6714 " whichStronglyInfluenceTheLayout),\n"
6715 " andMoreParameters),\n"
6717 getLLVMStyleWithColumns(69));
6718 verifyFormat("Foo::Foo()\n"
6724 verifyFormat("void f() {\n"
6734 verifyFormat("void f(param1, param2,\n"
6755 getLLVMStyleWithColumns(28));
6756 verifyFormat("#if 1\n"
6758 verifyFormat("#if 1\n"
6763 verifyFormat("DEBUG({\n"
6764 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6771 verifyIncompleteFormat("void f(\n"
6777 // Verify that indentation is correct when there is an `#if 0` with an
6779 verifyFormat("#if 0\n"
6787 verifyFormat("#if 0\n"
6790 "int something_fairly_long; // Align here please\n"
6791 "#endif // Should be aligned");
6793 verifyFormat("#if 0\n"
6800 verifyFormat("void SomeFunction(int param1,\n"
6815 TEST_F(FormatTest
, GraciouslyHandleIncorrectPreprocessorConditions
) {
6816 verifyFormat("#endif\n"
6820 TEST_F(FormatTest
, FormatsJoinedLinesOnSubsequentRuns
) {
6821 FormatStyle SingleLine
= getLLVMStyle();
6822 SingleLine
.AllowShortIfStatementsOnASingleLine
= FormatStyle::SIS_WithoutElse
;
6823 verifyFormat("#if 0\n"
6827 " if (test) foo2();\n"
6832 TEST_F(FormatTest
, LayoutBlockInsideParens
) {
6833 verifyFormat("functionCall({ int i; });");
6834 verifyFormat("functionCall({\n"
6838 verifyFormat("functionCall(\n"
6843 " aaaa, bbbb, cccc);");
6844 verifyFormat("functionA(functionB({\n"
6848 " aaaa, bbbb, cccc);");
6849 verifyFormat("functionCall(\n"
6854 " aaaa, bbbb, // comment\n"
6856 verifyFormat("functionA(functionB({\n"
6860 " aaaa, bbbb, // comment\n"
6862 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6863 verifyFormat("functionCall(aaaa, bbbb, {\n"
6868 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6870 " int i; // break\n"
6872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6873 " ccccccccccccccccc));");
6874 verifyFormat("DEBUG({\n"
6880 TEST_F(FormatTest
, LayoutBlockInsideStatement
) {
6881 verifyFormat("SOME_MACRO { int i; }\n"
6883 " SOME_MACRO {int i;} int i;");
6886 TEST_F(FormatTest
, LayoutNestedBlocks
) {
6887 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6891 " s kBitsToOs[] = {{10}};\n"
6892 " for (int i = 0; i < 10; ++i)\n"
6895 verifyFormat("call(parameter, {\n"
6897 " // Comment using all columns.\n"
6898 " somethingelse();\n"
6900 getLLVMStyleWithColumns(40));
6901 verifyFormat("DEBUG( //\n"
6903 verifyFormat("DEBUG( //\n"
6909 verifyFormat("call(parameter, {\n"
6912 " // looooooooooong.\n"
6913 " somethingElse();\n"
6915 "call(parameter, {\n"
6917 " // Comment too looooooooooong.\n"
6918 " somethingElse();\n"
6920 getLLVMStyleWithColumns(29));
6921 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6922 verifyFormat("DEBUG({ // comment\n"
6925 "DEBUG({ // comment\n"
6928 verifyFormat("DEBUG({\n"
6941 verifyFormat("DEBUG({\n"
6945 verifyGoogleFormat("DEBUG({\n"
6948 FormatStyle Style
= getGoogleStyle();
6949 Style
.ColumnLimit
= 45;
6950 verifyFormat("Debug(\n"
6953 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6958 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6960 verifyNoCrash("^{v^{a}}");
6963 TEST_F(FormatTest
, FormatNestedBlocksInMacros
) {
6964 verifyFormat("#define MACRO() \\\n"
6965 " Debug(aaa, /* force line break */ \\\n"
6970 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6971 " { int i; int j; })",
6974 verifyFormat("#define A \\\n"
6976 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6977 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6979 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6980 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6984 TEST_F(FormatTest
, PutEmptyBlocksIntoOneLine
) {
6985 verifyFormat("enum E {};");
6986 verifyFormat("enum E {}");
6987 FormatStyle Style
= getLLVMStyle();
6988 Style
.SpaceInEmptyBlock
= true;
6989 verifyFormat("void f() { }", "void f() {}", Style
);
6990 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Empty
;
6991 verifyFormat("{ }", Style
);
6992 verifyFormat("while (true) { }", "while (true) {}", Style
);
6993 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
6994 Style
.BraceWrapping
.BeforeElse
= false;
6995 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
6996 verifyFormat("if (a)\n"
7003 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Never
;
7004 verifyFormat("if (a) {\n"
7009 Style
.BraceWrapping
.BeforeElse
= true;
7010 verifyFormat("if (a) { }\n"
7015 Style
= getLLVMStyle(FormatStyle::LK_CSharp
);
7016 Style
.SpaceInEmptyBlock
= true;
7017 verifyFormat("Event += () => { };", Style
);
7020 TEST_F(FormatTest
, FormatBeginBlockEndMacros
) {
7021 FormatStyle Style
= getLLVMStyle();
7022 Style
.MacroBlockBegin
= "^[A-Z_]+_BEGIN$";
7023 Style
.MacroBlockEnd
= "^[A-Z_]+_END$";
7024 verifyFormat("FOO_BEGIN\n"
7028 verifyFormat("FOO_BEGIN\n"
7029 " NESTED_FOO_BEGIN\n"
7030 " NESTED_FOO_ENTRY\n"
7034 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7040 Style
.RemoveBracesLLVM
= true;
7041 verifyNoCrash("for (;;)\n"
7048 //===----------------------------------------------------------------------===//
7049 // Line break tests.
7050 //===----------------------------------------------------------------------===//
7052 TEST_F(FormatTest
, PreventConfusingIndents
) {
7055 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7056 " parameter, parameter, parameter)),\n"
7057 " SecondLongCall(parameter));\n"
7060 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7061 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7063 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7065 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7066 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7067 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7068 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7070 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7071 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7073 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7074 verifyFormat("int a = bbbb && ccc &&\n"
7076 "#define A Just forcing a new line\n"
7080 TEST_F(FormatTest
, LineBreakingInBinaryExpressions
) {
7083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7090 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7092 " ccccccccc == ddddddddddd;");
7093 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7095 " ccccccccc == ddddddddddd;");
7097 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7099 " ccccccccc == ddddddddddd;");
7101 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7103 " bbbbbb && cccccc;");
7104 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7107 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7108 " SourceMgr.getSpellingColumnNumber(\n"
7109 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7112 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7113 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7115 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7116 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7118 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7119 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7121 verifyFormat("b = a &&\n"
7125 // If the LHS of a comparison is not a binary expression itself, the
7126 // additional linebreak confuses many people.
7128 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7132 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7136 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7140 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7143 // Even explicit parentheses stress the precedence enough to make the
7144 // additional break unnecessary.
7145 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7148 // This cases is borderline, but with the indentation it is still readable.
7150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7151 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7154 getLLVMStyleWithColumns(75));
7156 // If the LHS is a binary expression, we should still use the additional break
7157 // as otherwise the formatting hides the operator precedence.
7158 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7162 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7167 FormatStyle OnePerLine
= getLLVMStyle();
7168 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7170 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7175 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7176 " .aaa(aaaaaaaaaaaaa) *\n"
7179 getLLVMStyleWithColumns(40));
7182 TEST_F(FormatTest
, ExpressionIndentation
) {
7183 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7185 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7187 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7188 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7191 " ccccccccccccccccccccccccccccccccccccccccc;");
7192 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7194 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7195 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7196 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7199 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7200 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7203 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7204 verifyFormat("if () {\n"
7205 "} else if (aaaaa && bbbbb > // break\n"
7208 verifyFormat("if () {\n"
7209 "} else if constexpr (aaaaa && bbbbb > // break\n"
7212 verifyFormat("if () {\n"
7213 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7216 verifyFormat("if () {\n"
7217 "} else if (aaaaa &&\n"
7218 " bbbbb > // break\n"
7223 // Presence of a trailing comment used to change indentation of b.
7224 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7226 "return aaaaaaaaaaaaaaaaaaa +\n"
7228 getLLVMStyleWithColumns(30));
7231 TEST_F(FormatTest
, ExpressionIndentationBreakingBeforeOperators
) {
7232 // Not sure what the best system is here. Like this, the LHS can be found
7233 // immediately above an operator (everything with the same or a higher
7234 // indent). The RHS is aligned right of the operator and so compasses
7235 // everything until something with the same indent as the operator is found.
7236 // FIXME: Is this a good system?
7237 FormatStyle Style
= getLLVMStyle();
7238 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7240 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7241 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7242 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7243 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7244 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7245 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7246 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7247 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7248 " > ccccccccccccccccccccccccccccccccccccccccc;",
7250 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7251 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7252 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7253 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7255 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7256 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7257 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7258 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7260 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7261 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7262 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7265 verifyFormat("if () {\n"
7266 "} else if (aaaaa\n"
7267 " && bbbbb // break\n"
7271 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7272 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7274 verifyFormat("return (a)\n"
7279 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7284 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7285 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7288 // Forced by comments.
7290 "unsigned ContentSize =\n"
7291 " sizeof(int16_t) // DWARF ARange version number\n"
7292 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7293 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7294 " + sizeof(int8_t); // Segment Size (in bytes)");
7296 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7297 " == boost::fusion::at_c<1>(iiii).second;",
7300 Style
.ColumnLimit
= 60;
7301 verifyFormat("zzzzzzzzzz\n"
7302 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7303 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7306 Style
.ColumnLimit
= 80;
7307 Style
.IndentWidth
= 4;
7309 Style
.UseTab
= FormatStyle::UT_Always
;
7310 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7311 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
7312 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7313 "\t&& (someOtherLongishConditionPart1\n"
7314 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7315 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7316 "(someOtherLongishConditionPart1 || "
7317 "someOtherEvenLongerNestedConditionPart2);",
7320 Style
= getLLVMStyleWithColumns(20);
7321 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
7322 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7323 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7324 Style
.ContinuationIndentWidth
= 2;
7325 verifyFormat("struct Foo {\n"
7334 verifyFormat("return abc\n"
7344 TEST_F(FormatTest
, ExpressionIndentationStrictAlign
) {
7345 FormatStyle Style
= getLLVMStyle();
7346 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7347 Style
.AlignOperands
= FormatStyle::OAS_AlignAfterOperator
;
7349 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7350 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7351 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7352 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7353 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7354 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7355 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7356 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7357 " > ccccccccccccccccccccccccccccccccccccccccc;",
7359 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7360 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7361 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7362 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7364 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7365 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7366 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7367 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7369 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7370 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7372 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7374 verifyFormat("if () {\n"
7375 "} else if (aaaaa\n"
7376 " && bbbbb // break\n"
7380 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7381 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7383 verifyFormat("return (a)\n"
7388 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7389 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7392 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7393 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7394 " : 3333333333333333;",
7397 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7398 " : ccccccccccccccc ? dddddddddddddddddd\n"
7399 " : eeeeeeeeeeeeeeeeee)\n"
7400 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7401 " : 3333333333333333;",
7403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7404 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7407 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7408 " == boost::fusion::at_c<1>(iiii).second;",
7411 Style
.ColumnLimit
= 60;
7412 verifyFormat("zzzzzzzzzzzzz\n"
7413 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7414 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7417 // Forced by comments.
7418 Style
.ColumnLimit
= 80;
7420 "unsigned ContentSize\n"
7421 " = sizeof(int16_t) // DWARF ARange version number\n"
7422 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7423 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7424 " + sizeof(int8_t); // Segment Size (in bytes)",
7427 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7429 "unsigned ContentSize =\n"
7430 " sizeof(int16_t) // DWARF ARange version number\n"
7431 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7432 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7433 " + sizeof(int8_t); // Segment Size (in bytes)",
7436 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
7438 "unsigned ContentSize =\n"
7439 " sizeof(int16_t) // DWARF ARange version number\n"
7440 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7441 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7442 " + sizeof(int8_t); // Segment Size (in bytes)",
7446 TEST_F(FormatTest
, EnforcedOperatorWraps
) {
7447 // Here we'd like to wrap after the || operators, but a comment is forcing an
7449 verifyFormat("bool x = aaaaa //\n"
7455 TEST_F(FormatTest
, NoOperandAlignment
) {
7456 FormatStyle Style
= getLLVMStyle();
7457 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
7458 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7462 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7463 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7464 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7465 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7466 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7467 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7468 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7469 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7470 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7471 " > ccccccccccccccccccccccccccccccccccccccccc;",
7474 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7475 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7478 verifyFormat("int a = aa\n"
7479 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7480 " * cccccccccccccccccccccccccccccccccccc;",
7483 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7484 verifyFormat("return (a > b\n"
7491 TEST_F(FormatTest
, BreakingBeforeNonAssigmentOperators
) {
7492 FormatStyle Style
= getLLVMStyle();
7493 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7494 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7496 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7500 TEST_F(FormatTest
, AllowBinPackingInsideArguments
) {
7501 FormatStyle Style
= getLLVMStyleWithColumns(40);
7502 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7503 Style
.BinPackArguments
= false;
7504 verifyFormat("void test() {\n"
7506 " this + argument + is + quite\n"
7507 " + long + so + it + gets + wrapped\n"
7508 " + but + remains + bin - packed);\n"
7511 verifyFormat("void test() {\n"
7512 " someFunction(arg1,\n"
7513 " this + argument + is\n"
7514 " + quite + long + so\n"
7515 " + it + gets + wrapped\n"
7516 " + but + remains + bin\n"
7521 verifyFormat("void test() {\n"
7524 " this + argument + has\n"
7525 " + anotherFunc(nested,\n"
7531 " + to + being + bin - packed,\n"
7536 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
7537 verifyFormat("void test() {\n"
7540 " this + argument + has +\n"
7541 " anotherFunc(nested,\n"
7542 " calls + whose +\n"
7546 " in + addition) +\n"
7547 " to + being + bin - packed,\n"
7553 TEST_F(FormatTest
, BreakBinaryOperatorsInPresenceOfTemplates
) {
7554 auto Style
= getLLVMStyleWithColumns(45);
7555 EXPECT_EQ(Style
.BreakBeforeBinaryOperators
, FormatStyle::BOS_None
);
7556 verifyFormat("bool b =\n"
7557 " is_default_constructible_v<hash<T>> and\n"
7558 " is_copy_constructible_v<hash<T>> and\n"
7559 " is_move_constructible_v<hash<T>> and\n"
7560 " is_copy_assignable_v<hash<T>> and\n"
7561 " is_move_assignable_v<hash<T>> and\n"
7562 " is_destructible_v<hash<T>> and\n"
7563 " is_swappable_v<hash<T>> and\n"
7564 " is_callable_v<hash<T>(T)>;",
7567 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7568 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7569 " and is_copy_constructible_v<hash<T>>\n"
7570 " and is_move_constructible_v<hash<T>>\n"
7571 " and is_copy_assignable_v<hash<T>>\n"
7572 " and is_move_assignable_v<hash<T>>\n"
7573 " and is_destructible_v<hash<T>>\n"
7574 " and is_swappable_v<hash<T>>\n"
7575 " and is_callable_v<hash<T>(T)>;",
7578 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7579 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7580 " and is_copy_constructible_v<hash<T>>\n"
7581 " and is_move_constructible_v<hash<T>>\n"
7582 " and is_copy_assignable_v<hash<T>>\n"
7583 " and is_move_assignable_v<hash<T>>\n"
7584 " and is_destructible_v<hash<T>>\n"
7585 " and is_swappable_v<hash<T>>\n"
7586 " and is_callable_v<hash<T>(T)>;",
7590 TEST_F(FormatTest
, ConstructorInitializers
) {
7591 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7592 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7593 getLLVMStyleWithColumns(45));
7594 verifyFormat("Constructor()\n"
7595 " : Inttializer(FitsOnTheLine) {}",
7596 getLLVMStyleWithColumns(44));
7597 verifyFormat("Constructor()\n"
7598 " : Inttializer(FitsOnTheLine) {}",
7599 getLLVMStyleWithColumns(43));
7601 verifyFormat("template <typename T>\n"
7602 "Constructor() : Initializer(FitsOnTheLine) {}",
7603 getLLVMStyleWithColumns(45));
7606 "SomeClass::Constructor()\n"
7607 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7610 "SomeClass::Constructor()\n"
7611 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7612 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7614 "SomeClass::Constructor()\n"
7615 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7616 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7617 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7618 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7619 " : aaaaaaaaaa(aaaaaa) {}");
7621 verifyFormat("Constructor()\n"
7622 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7623 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7624 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7625 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7627 verifyFormat("Constructor()\n"
7628 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7631 verifyFormat("Constructor(int Parameter = 0)\n"
7632 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7633 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7634 verifyFormat("Constructor()\n"
7635 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7637 getLLVMStyleWithColumns(60));
7638 verifyFormat("Constructor()\n"
7639 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7640 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7642 // Here a line could be saved by splitting the second initializer onto two
7643 // lines, but that is not desirable.
7644 verifyFormat("Constructor()\n"
7645 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7646 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7647 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7649 FormatStyle OnePerLine
= getLLVMStyle();
7650 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
7651 verifyFormat("MyClass::MyClass()\n"
7656 verifyFormat("MyClass::MyClass()\n"
7657 " : a(a), // comment\n"
7661 verifyFormat("MyClass::MyClass(int a)\n"
7662 " : b(a), // comment\n"
7663 " c(a + 1) { // lined up\n"
7666 verifyFormat("Constructor()\n"
7669 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7670 OnePerLine
.AllowAllParametersOfDeclarationOnNextLine
= false;
7671 verifyFormat("SomeClass::Constructor()\n"
7672 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7673 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7674 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7676 verifyFormat("SomeClass::Constructor()\n"
7677 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7678 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7679 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7681 verifyFormat("MyClass::MyClass(int var)\n"
7682 " : some_var_(var), // 4 space indent\n"
7683 " some_other_var_(var + 1) { // lined up\n"
7686 verifyFormat("Constructor()\n"
7687 " : aaaaa(aaaaaa),\n"
7691 " aaaaa(aaaaaa) {}",
7693 verifyFormat("Constructor()\n"
7694 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7695 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7697 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7700 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7701 " aaaaaaaaaaa().aaa(),\n"
7702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7704 OnePerLine
.ColumnLimit
= 60;
7705 verifyFormat("Constructor()\n"
7706 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7707 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7710 verifyFormat("Constructor()\n"
7711 " : // Comment forcing unwanted break.\n"
7714 " // Comment forcing unwanted break.\n"
7718 TEST_F(FormatTest
, AllowAllConstructorInitializersOnNextLine
) {
7719 FormatStyle Style
= getLLVMStyleWithColumns(60);
7720 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7721 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7723 for (int i
= 0; i
< 4; ++i
) {
7724 // Test all combinations of parameters that should not have an effect.
7725 Style
.AllowAllParametersOfDeclarationOnNextLine
= i
& 1;
7726 Style
.AllowAllArgumentsOnNextLine
= i
& 2;
7728 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7729 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7730 verifyFormat("Constructor()\n"
7731 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7733 verifyFormat("Constructor() : a(a), b(b) {}", Style
);
7735 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7736 verifyFormat("Constructor()\n"
7737 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7738 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7740 verifyFormat("Constructor() : a(a), b(b) {}", Style
);
7742 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7743 verifyFormat("Constructor()\n"
7744 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7746 verifyFormat("Constructor()\n"
7749 verifyFormat("Constructor()\n"
7750 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7751 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7752 " , cccccccccccccccccccccc(c) {}",
7755 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
7756 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7757 verifyFormat("Constructor()\n"
7758 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7761 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7762 verifyFormat("Constructor()\n"
7763 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7764 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7767 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7768 verifyFormat("Constructor()\n"
7769 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7771 verifyFormat("Constructor()\n"
7774 verifyFormat("Constructor()\n"
7775 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7776 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7777 " cccccccccccccccccccccc(c) {}",
7780 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7781 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7782 verifyFormat("Constructor() :\n"
7783 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7786 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7787 verifyFormat("Constructor() :\n"
7788 " aaaaaaaaaaaaaaaaaa(a),\n"
7789 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7792 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7793 verifyFormat("Constructor() :\n"
7794 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7796 verifyFormat("Constructor() :\n"
7799 verifyFormat("Constructor() :\n"
7800 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7801 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7802 " cccccccccccccccccccccc(c) {}",
7806 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7807 // AllowAllConstructorInitializersOnNextLine in all
7808 // BreakConstructorInitializers modes
7809 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7810 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7811 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7812 verifyFormat("SomeClassWithALongName::Constructor(\n"
7813 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7814 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7815 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7818 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7819 verifyFormat("SomeClassWithALongName::Constructor(\n"
7820 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7821 " int bbbbbbbbbbbbb,\n"
7822 " int cccccccccccccccc)\n"
7823 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7826 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7827 verifyFormat("SomeClassWithALongName::Constructor(\n"
7828 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7829 " int bbbbbbbbbbbbb,\n"
7830 " int cccccccccccccccc)\n"
7831 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7834 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7835 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7836 verifyFormat("SomeClassWithALongName::Constructor(\n"
7837 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7838 " int bbbbbbbbbbbbb)\n"
7839 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7840 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7843 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
7845 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7846 verifyFormat("SomeClassWithALongName::Constructor(\n"
7847 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7848 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7849 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7852 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7853 verifyFormat("SomeClassWithALongName::Constructor(\n"
7854 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7855 " int bbbbbbbbbbbbb,\n"
7856 " int cccccccccccccccc)\n"
7857 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7860 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7861 verifyFormat("SomeClassWithALongName::Constructor(\n"
7862 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7863 " int bbbbbbbbbbbbb,\n"
7864 " int cccccccccccccccc)\n"
7865 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7868 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7869 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7870 verifyFormat("SomeClassWithALongName::Constructor(\n"
7871 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7872 " int bbbbbbbbbbbbb)\n"
7873 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7874 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7877 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7878 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7879 verifyFormat("SomeClassWithALongName::Constructor(\n"
7880 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7881 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7882 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7885 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7886 verifyFormat("SomeClassWithALongName::Constructor(\n"
7887 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7888 " int bbbbbbbbbbbbb,\n"
7889 " int cccccccccccccccc) :\n"
7890 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7893 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7894 verifyFormat("SomeClassWithALongName::Constructor(\n"
7895 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7896 " int bbbbbbbbbbbbb,\n"
7897 " int cccccccccccccccc) :\n"
7898 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7901 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7902 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7903 verifyFormat("SomeClassWithALongName::Constructor(\n"
7904 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7905 " int bbbbbbbbbbbbb) :\n"
7906 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7907 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7910 Style
= getLLVMStyleWithColumns(0);
7911 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7912 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style
);
7913 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7914 " : bar(bar), baz(baz) {}",
7918 TEST_F(FormatTest
, AllowAllArgumentsOnNextLine
) {
7919 FormatStyle Style
= getLLVMStyleWithColumns(60);
7920 Style
.BinPackArguments
= false;
7921 for (int i
= 0; i
< 4; ++i
) {
7922 // Test all combinations of parameters that should not have an effect.
7923 Style
.AllowAllParametersOfDeclarationOnNextLine
= i
& 1;
7924 Style
.PackConstructorInitializers
=
7925 i
& 2 ? FormatStyle::PCIS_BinPack
: FormatStyle::PCIS_Never
;
7927 Style
.AllowAllArgumentsOnNextLine
= true;
7928 verifyFormat("void foo() {\n"
7929 " FunctionCallWithReallyLongName(\n"
7930 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7933 Style
.AllowAllArgumentsOnNextLine
= false;
7934 verifyFormat("void foo() {\n"
7935 " FunctionCallWithReallyLongName(\n"
7936 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7941 Style
.AllowAllArgumentsOnNextLine
= true;
7942 verifyFormat("void foo() {\n"
7943 " auto VariableWithReallyLongName = {\n"
7944 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7947 Style
.AllowAllArgumentsOnNextLine
= false;
7948 verifyFormat("void foo() {\n"
7949 " auto VariableWithReallyLongName = {\n"
7950 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7956 // This parameter should not affect declarations.
7957 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7958 Style
.AllowAllArgumentsOnNextLine
= false;
7959 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7960 verifyFormat("void FunctionCallWithReallyLongName(\n"
7961 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7963 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7964 verifyFormat("void FunctionCallWithReallyLongName(\n"
7965 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7966 " int bbbbbbbbbbbb);",
7970 TEST_F(FormatTest
, AllowAllArgumentsOnNextLineDontAlign
) {
7971 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7973 FormatStyle Style
= getLLVMStyleWithColumns(35);
7974 StringRef Input
= "functionCall(paramA, paramB, paramC);\n"
7975 "void functionDecl(int A, int B, int C);";
7976 Style
.AllowAllArgumentsOnNextLine
= false;
7977 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7978 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7980 "void functionDecl(int A, int B,\n"
7983 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
7984 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7986 "void functionDecl(int A, int B,\n"
7989 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7990 // AllowAllArgumentsOnNextLine.
7991 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
7992 verifyFormat(StringRef("functionCall(\n"
7993 " paramA, paramB, paramC);\n"
7994 "void functionDecl(\n"
7995 " int A, int B, int C);"),
7997 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
7998 verifyFormat("functionCall(\n"
7999 " paramA, paramB, paramC\n"
8001 "void functionDecl(\n"
8002 " int A, int B, int C\n"
8006 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8008 Style
.AllowAllArgumentsOnNextLine
= true;
8009 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
8010 verifyFormat(StringRef("functionCall(\n"
8011 " paramA, paramB, paramC);\n"
8012 "void functionDecl(\n"
8013 " int A, int B, int C);"),
8015 // It wouldn't fit on one line with aligned parameters so this setting
8016 // doesn't change anything for BAS_Align.
8017 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
8018 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8020 "void functionDecl(int A, int B,\n"
8023 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
8024 verifyFormat(StringRef("functionCall(\n"
8025 " paramA, paramB, paramC);\n"
8026 "void functionDecl(\n"
8027 " int A, int B, int C);"),
8031 TEST_F(FormatTest
, BreakFunctionDefinitionParameters
) {
8032 StringRef Input
= "void functionDecl(paramA, paramB, paramC);\n"
8033 "void emptyFunctionDefinition() {}\n"
8034 "void functionDefinition(int A, int B, int C) {}\n"
8035 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8036 verifyFormat(Input
);
8038 FormatStyle Style
= getLLVMStyle();
8039 EXPECT_FALSE(Style
.BreakFunctionDefinitionParameters
);
8040 Style
.BreakFunctionDefinitionParameters
= true;
8041 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8042 "void emptyFunctionDefinition() {}\n"
8043 "void functionDefinition(\n"
8044 " int A, int B, int C) {}\n"
8047 " : m_A(A), m_B(B) {}",
8050 // Test the style where all parameters are on their own lines.
8051 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
8052 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8053 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8054 "void emptyFunctionDefinition() {}\n"
8055 "void functionDefinition(\n"
8062 " : m_A(A), m_B(B) {}",
8066 TEST_F(FormatTest
, BreakBeforeInlineASMColon
) {
8067 FormatStyle Style
= getLLVMStyle();
8068 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Never
;
8069 /* Test the behaviour with long lines */
8070 Style
.ColumnLimit
= 40;
8071 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8074 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8077 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8078 " \"cpuid\\n\\t\"\n"
8079 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8080 " : \"=a\" : \"a\");",
8082 Style
.ColumnLimit
= 80;
8083 verifyFormat("asm volatile(\"string\", : : val);", Style
);
8084 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style
);
8086 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Always
;
8087 verifyFormat("asm volatile(\"string\",\n"
8091 verifyFormat("asm volatile(\"string\",\n"
8095 /* Test the behaviour with long lines */
8096 Style
.ColumnLimit
= 40;
8097 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8098 " \"cpuid\\n\\t\"\n"
8099 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8100 " : \"=a\"(*rEAX)\n"
8101 " : \"a\"(value));",
8103 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8104 " \"cpuid\\n\\t\"\n"
8105 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8107 " : \"a\"(value));",
8109 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8113 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8119 TEST_F(FormatTest
, BreakConstructorInitializersAfterColon
) {
8120 FormatStyle Style
= getLLVMStyle();
8121 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
8123 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8124 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8125 getStyleWithColumns(Style
, 45));
8126 verifyFormat("Constructor() :\n"
8127 " Initializer(FitsOnTheLine) {}",
8128 getStyleWithColumns(Style
, 44));
8129 verifyFormat("Constructor() :\n"
8130 " Initializer(FitsOnTheLine) {}",
8131 getStyleWithColumns(Style
, 43));
8133 verifyFormat("template <typename T>\n"
8134 "Constructor() : Initializer(FitsOnTheLine) {}",
8135 getStyleWithColumns(Style
, 50));
8137 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8138 " int mooooooooooooore) noexcept :\n"
8139 " Super{some, arguments}, Member{5}, Member2{2} {}",
8141 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
8143 "SomeClass::Constructor() :\n"
8144 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8147 "SomeClass::Constructor() : // NOLINT\n"
8148 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8151 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
8153 "SomeClass::Constructor() :\n"
8154 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8157 "SomeClass::Constructor() : // NOLINT\n"
8158 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8161 Style
.PackConstructorInitializers
= FormatStyle::PCIS_BinPack
;
8163 "SomeClass::Constructor() :\n"
8164 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8168 "SomeClass::Constructor() :\n"
8169 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8170 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8173 "SomeClass::Constructor() :\n"
8174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8175 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8178 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8179 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8182 verifyFormat("Constructor() :\n"
8183 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8184 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8185 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8186 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8189 verifyFormat("Constructor() :\n"
8190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8194 verifyFormat("Constructor(int Parameter = 0) :\n"
8195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8196 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8198 verifyFormat("Constructor() :\n"
8199 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8201 getStyleWithColumns(Style
, 60));
8202 verifyFormat("Constructor() :\n"
8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8204 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8207 // Here a line could be saved by splitting the second initializer onto two
8208 // lines, but that is not desirable.
8209 verifyFormat("Constructor() :\n"
8210 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8211 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8212 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8215 FormatStyle OnePerLine
= Style
;
8216 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
8217 verifyFormat("SomeClass::Constructor() :\n"
8218 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8219 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8220 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8222 verifyFormat("SomeClass::Constructor() :\n"
8223 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8224 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8225 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8227 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8228 " i(i), // comment\n"
8231 verifyFormat("MyClass::MyClass(int var) :\n"
8232 " some_var_(var), // 4 space indent\n"
8233 " some_other_var_(var + 1) { // lined up\n"
8236 verifyFormat("Constructor() :\n"
8241 " aaaaa(aaaaaa) {}",
8243 verifyFormat("Constructor() :\n"
8244 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8245 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8247 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8248 verifyFormat("Constructor() :\n"
8249 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8250 " aaaaaaaaaaa().aaa(),\n"
8251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8253 OnePerLine
.ColumnLimit
= 60;
8254 verifyFormat("Constructor() :\n"
8255 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8256 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8259 verifyFormat("Constructor() :\n"
8260 " // Comment forcing unwanted break.\n"
8263 verifyFormat("Constructor() : // NOLINT\n"
8266 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8270 "Constructor() : // A very long trailing comment that cannot fit"
8271 " on a single line.\n"
8275 Style
.ColumnLimit
= 0;
8276 verifyFormat("SomeClass::Constructor() :\n"
8279 verifyFormat("SomeClass::Constructor() noexcept :\n"
8282 verifyFormat("SomeClass::Constructor() :\n"
8283 " a(a), b(b), c(c) {}",
8285 verifyFormat("SomeClass::Constructor() :\n"
8292 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
8293 verifyFormat("SomeClass::Constructor() :\n"
8294 " a(a), b(b), c(c) {\n"
8297 verifyFormat("SomeClass::Constructor() :\n"
8302 Style
.ColumnLimit
= 80;
8303 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
8304 Style
.ConstructorInitializerIndentWidth
= 2;
8305 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style
);
8306 verifyFormat("SomeClass::Constructor() :\n"
8307 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8308 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8311 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8313 Style
.BreakInheritanceList
= FormatStyle::BILS_BeforeColon
;
8316 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8317 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8319 Style
.BreakInheritanceList
= FormatStyle::BILS_BeforeComma
;
8322 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8323 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8325 Style
.BreakInheritanceList
= FormatStyle::BILS_AfterColon
;
8327 "class SomeClass :\n"
8328 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8329 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8331 Style
.BreakInheritanceList
= FormatStyle::BILS_AfterComma
;
8334 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8335 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8339 #ifndef EXPENSIVE_CHECKS
8340 // Expensive checks enables libstdc++ checking which includes validating the
8341 // state of ranges used in std::priority_queue - this blows out the
8342 // runtime/scalability of the function and makes this test unacceptably slow.
8343 TEST_F(FormatTest
, MemoizationTests
) {
8344 // This breaks if the memoization lookup does not take \c Indent and
8345 // \c LastSpace into account.
8347 "extern CFRunLoopTimerRef\n"
8348 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8349 " CFTimeInterval interval, CFOptionFlags flags,\n"
8350 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8351 " CFRunLoopTimerContext *context) {}");
8353 // Deep nesting somewhat works around our memoization.
8355 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8356 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8357 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8358 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8359 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8360 getLLVMStyleWithColumns(65));
8386 " aaaaa))))))))))));",
8387 getLLVMStyleWithColumns(65));
8389 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
8407 getLLVMStyleWithColumns(65));
8409 // This test takes VERY long when memoization is broken.
8410 FormatStyle OnePerLine
= getLLVMStyle();
8411 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
8412 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8413 std::string input
= "Constructor()\n"
8415 for (unsigned i
= 0, e
= 80; i
!= e
; ++i
)
8418 verifyFormat(input
, OnePerLine
);
8419 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
8420 verifyFormat(input
, OnePerLine
);
8424 TEST_F(FormatTest
, BreaksAsHighAsPossible
) {
8427 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8428 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8431 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8432 " Intervals[i - 1].getRange().getLast()) {\n}");
8435 TEST_F(FormatTest
, BreaksFunctionDeclarations
) {
8436 // Principially, we break function declarations in a certain order:
8437 // 1) break amongst arguments.
8438 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8439 " Cccccccccccccc cccccccccccccc);");
8440 verifyFormat("template <class TemplateIt>\n"
8441 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8442 " TemplateIt *stop) {}");
8444 // 2) break after return type.
8446 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8447 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8449 // 3) break after (.
8451 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8452 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8454 // 4) break before after nested name specifiers.
8456 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8457 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8458 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8460 // However, there are exceptions, if a sufficient amount of lines can be
8462 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8464 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8465 " Cccccccccccccc cccccccccc,\n"
8466 " Cccccccccccccc cccccccccc,\n"
8467 " Cccccccccccccc cccccccccc,\n"
8468 " Cccccccccccccc cccccccccc);");
8470 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8471 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8472 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8473 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8475 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8476 " Cccccccccccccc cccccccccc,\n"
8477 " Cccccccccccccc cccccccccc,\n"
8478 " Cccccccccccccc cccccccccc,\n"
8479 " Cccccccccccccc cccccccccc,\n"
8480 " Cccccccccccccc cccccccccc,\n"
8481 " Cccccccccccccc cccccccccc);");
8482 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8483 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8484 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8485 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8486 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8488 // Break after multi-line parameters.
8489 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8493 verifyFormat("void SomeLoooooooooooongFunction(\n"
8494 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8495 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8496 " int bbbbbbbbbbbbb);");
8498 // Treat overloaded operators like other functions.
8499 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8500 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8501 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8502 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8503 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8504 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8506 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8507 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8509 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8510 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8511 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8512 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8513 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8514 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8516 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8517 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8518 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8519 verifyGoogleFormat("template <typename T>\n"
8520 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8521 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8522 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8524 FormatStyle Style
= getLLVMStyle();
8525 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
8526 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8527 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8529 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8534 TEST_F(FormatTest
, DontBreakBeforeQualifiedOperator
) {
8535 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8536 // Prefer keeping `::` followed by `operator` together.
8537 verifyFormat("const aaaa::bbbbbbb &\n"
8538 "ccccccccc::operator++() {\n"
8541 "const aaaa::bbbbbbb\n"
8542 "&ccccccccc::operator++() { stuff(); }",
8543 getLLVMStyleWithColumns(40));
8546 TEST_F(FormatTest
, TrailingReturnType
) {
8547 verifyFormat("auto foo() -> int;");
8548 // correct trailing return type spacing
8549 verifyFormat("auto operator->() -> int;");
8550 verifyFormat("auto operator++(int) -> int;");
8552 verifyFormat("struct S {\n"
8553 " auto bar() const -> int;\n"
8555 verifyFormat("template <size_t Order, typename T>\n"
8556 "auto load_img(const std::string &filename)\n"
8557 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8558 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8559 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8560 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8561 verifyFormat("template <typename T>\n"
8562 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8563 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8565 FormatStyle Style
= getLLVMStyleWithColumns(60);
8566 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8567 " auto NAME() -> int { return 42; }",
8570 // Not trailing return types.
8571 verifyFormat("void f() { auto a = b->c(); }");
8572 verifyFormat("auto a = p->foo();");
8573 verifyFormat("int a = p->foo();");
8574 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8577 TEST_F(FormatTest
, DeductionGuides
) {
8578 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8579 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8580 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8582 "template <class... T>\n"
8583 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8584 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8585 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8586 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8587 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8588 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8589 verifyFormat("template <class T> x() -> x<1>;");
8590 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8592 verifyFormat("A(const char *) -> A<string &>;");
8593 verifyFormat("A() -> A<int>;");
8595 // Ensure not deduction guides.
8596 verifyFormat("c()->f<int>();");
8597 verifyFormat("x()->foo<1>;");
8598 verifyFormat("x = p->foo<3>();");
8599 verifyFormat("x()->x<1>();");
8602 TEST_F(FormatTest
, BreaksFunctionDeclarationsWithTrailingTokens
) {
8603 // Avoid breaking before trailing 'const' or other trailing annotations, if
8604 // they are not function-like.
8605 FormatStyle Style
= getGoogleStyleWithColumns(47);
8606 verifyFormat("void someLongFunction(\n"
8607 " int someLoooooooooooooongParameter) const {\n}",
8608 getLLVMStyleWithColumns(47));
8609 verifyFormat("LoooooongReturnType\n"
8610 "someLoooooooongFunction() const {}",
8611 getLLVMStyleWithColumns(47));
8612 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8615 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8616 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8617 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8618 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8619 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8620 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8621 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8622 " aaaaaaaaaaa aaaaa) const override;");
8624 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8625 " const override;");
8627 // Even if the first parameter has to be wrapped.
8628 verifyFormat("void someLongFunction(\n"
8629 " int someLongParameter) const {}",
8630 getLLVMStyleWithColumns(46));
8631 verifyFormat("void someLongFunction(\n"
8632 " int someLongParameter) const {}",
8634 verifyFormat("void someLongFunction(\n"
8635 " int someLongParameter) override {}",
8637 verifyFormat("void someLongFunction(\n"
8638 " int someLongParameter) OVERRIDE {}",
8640 verifyFormat("void someLongFunction(\n"
8641 " int someLongParameter) final {}",
8643 verifyFormat("void someLongFunction(\n"
8644 " int someLongParameter) FINAL {}",
8646 verifyFormat("void someLongFunction(\n"
8647 " int parameter) const override {}",
8650 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
8651 verifyFormat("void someLongFunction(\n"
8652 " int someLongParameter) const\n"
8657 Style
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
8658 verifyFormat("void someLongFunction(\n"
8659 " int someLongParameter) const\n"
8664 // Unless these are unknown annotations.
8665 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8666 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8667 " LONG_AND_UGLY_ANNOTATION;");
8669 // Breaking before function-like trailing annotations is fine to keep them
8670 // close to their arguments.
8671 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8672 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8673 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8674 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8675 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8676 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8677 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8678 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8679 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8682 "void aaaaaaaaaaaaaaaaaa()\n"
8683 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8684 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8685 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8686 " __attribute__((unused));");
8688 Style
= getGoogleStyle();
8691 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8692 " GUARDED_BY(aaaaaaaaaaaa);",
8695 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8696 " GUARDED_BY(aaaaaaaaaaaa);",
8699 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8700 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8703 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8704 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8708 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8709 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8712 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8713 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8716 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8717 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8720 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8721 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8725 TEST_F(FormatTest
, FunctionAnnotations
) {
8726 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8727 "int OldFunction(const string ¶meter) {}");
8728 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8729 "string OldFunction(const string ¶meter) {}");
8730 verifyFormat("template <typename T>\n"
8731 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8732 "string OldFunction(const string ¶meter) {}");
8734 // Not function annotations.
8735 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8736 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8737 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8738 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8739 verifyFormat("MACRO(abc).function() // wrap\n"
8741 verifyFormat("MACRO(abc)->function() // wrap\n"
8743 verifyFormat("MACRO(abc)::function() // wrap\n"
8745 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8748 TEST_F(FormatTest
, BreaksDesireably
) {
8749 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8750 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8751 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8752 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8757 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8760 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8765 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8766 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8768 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8771 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8772 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8780 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8783 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8787 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8790 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8794 // Indent consistently independent of call expression and unary operator.
8795 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8796 " dddddddddddddddddddddddddddddd));");
8797 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8798 " dddddddddddddddddddddddddddddd));");
8799 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8800 " dddddddddddddddddddddddddddddd));");
8802 // This test case breaks on an incorrect memoization, i.e. an optimization not
8803 // taking into account the StopAt value.
8805 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8806 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8807 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8808 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8810 verifyFormat("{\n {\n {\n"
8811 " Annotation.SpaceRequiredBefore =\n"
8812 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8813 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8816 // Break on an outer level if there was a break on an inner level.
8817 verifyFormat("f(g(h(a, // comment\n"
8821 "f(g(h(a, // comment\n"
8822 " b, c), d, e), x, y);");
8824 // Prefer breaking similar line breaks.
8826 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8827 " NSTrackingMouseEnteredAndExited |\n"
8828 " NSTrackingActiveAlways;");
8831 TEST_F(FormatTest
, FormatsDeclarationsOnePerLine
) {
8832 FormatStyle NoBinPacking
= getGoogleStyle();
8833 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8834 NoBinPacking
.BinPackArguments
= true;
8835 verifyFormat("void f() {\n"
8836 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8840 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8841 " int aaaaaaaaaaaaaaaaaaaa,\n"
8842 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8845 NoBinPacking
.AllowAllParametersOfDeclarationOnNextLine
= false;
8846 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8847 " vector<int> bbbbbbbbbbbbbbb);",
8849 // FIXME: This behavior difference is probably not wanted. However, currently
8850 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8851 // template arguments from BreakBeforeParameter being set because of the
8852 // one-per-line formatting.
8854 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8855 " aaaaaaaaaa> aaaaaaaaaa);",
8858 "void fffffffffff(\n"
8859 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8863 TEST_F(FormatTest
, FormatsOneParameterPerLineIfNecessary
) {
8864 FormatStyle NoBinPacking
= getGoogleStyle();
8865 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8866 NoBinPacking
.BinPackArguments
= false;
8867 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8868 " aaaaaaaaaaaaaaaaaaaa,\n"
8869 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8871 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8873 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8876 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8877 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8879 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8882 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8883 " .aaaaaaaaaaaaaaaaaa();",
8885 verifyFormat("void f() {\n"
8886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8887 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8892 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8897 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8898 " ddddddddddddddddddddddddddddd),\n"
8902 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8903 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8904 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8905 " aaaaaaaaaaaaaaaaaa;",
8907 verifyFormat("a(\"a\"\n"
8911 NoBinPacking
.AllowAllParametersOfDeclarationOnNextLine
= false;
8912 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8918 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8923 "template <class SomeType, class SomeOtherType>\n"
8924 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8928 TEST_F(FormatTest
, FormatsDeclarationBreakAlways
) {
8929 FormatStyle BreakAlways
= getGoogleStyle();
8930 BreakAlways
.BinPackParameters
= FormatStyle::BPPS_AlwaysOnePerLine
;
8931 verifyFormat("void f(int a,\n"
8934 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8935 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8936 " int cccccccccccccccccccccccc);",
8939 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8940 // to BPPS_AlwaysOnePerLine.
8941 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
8943 "void someLongFunctionName(\n"
8944 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8947 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
8949 "void someLongFunctionName(\n"
8950 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8956 TEST_F(FormatTest
, FormatsDefinitionBreakAlways
) {
8957 FormatStyle BreakAlways
= getGoogleStyle();
8958 BreakAlways
.BinPackParameters
= FormatStyle::BPPS_AlwaysOnePerLine
;
8959 verifyFormat("void f(int a,\n"
8965 // Ensure BinPackArguments interact correctly when BinPackParameters is set to
8966 // BPPS_AlwaysOnePerLine.
8967 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8968 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8969 " int cccccccccccccccccccccccc) {\n"
8970 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8971 " cccccccccccccccccccccccc);\n"
8974 BreakAlways
.BinPackArguments
= false;
8975 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8976 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8977 " int cccccccccccccccccccccccc) {\n"
8978 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8979 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8980 " cccccccccccccccccccccccc);\n"
8984 // Ensure BreakFunctionDefinitionParameters interacts correctly when
8985 // BinPackParameters is set to BPPS_AlwaysOnePerLine.
8986 BreakAlways
.BreakFunctionDefinitionParameters
= true;
8987 verifyFormat("void f(\n"
8993 BreakAlways
.BreakFunctionDefinitionParameters
= false;
8995 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8996 // to BPPS_AlwaysOnePerLine.
8997 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
8999 "void someLongFunctionName(\n"
9000 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9002 " someLongFunctionName(\n"
9003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
9006 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
9008 "void someLongFunctionName(\n"
9009 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9012 " someLongFunctionName(\n"
9013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n"
9019 TEST_F(FormatTest
, AdaptiveOnePerLineFormatting
) {
9020 FormatStyle Style
= getLLVMStyleWithColumns(15);
9021 Style
.ExperimentalAutoDetectBinPacking
= true;
9022 verifyFormat("aaa(aaaa,\n"
9028 "aaa(aaaa,\n" // one-per-line
9031 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9033 verifyFormat("aaa(aaaa, aaaa,\n"
9037 "aaa(aaaa, aaaa,\n" // bin-packed
9039 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9043 TEST_F(FormatTest
, FormatsBuilderPattern
) {
9044 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9045 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9046 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9047 " .StartsWith(\".init\", ORDER_INIT)\n"
9048 " .StartsWith(\".fini\", ORDER_FINI)\n"
9049 " .StartsWith(\".hash\", ORDER_HASH)\n"
9050 " .Default(ORDER_TEXT);");
9052 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9053 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9054 verifyFormat("aaaaaaa->aaaaaaa\n"
9055 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9057 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9059 "aaaaaaa->aaaaaaa\n"
9060 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9061 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9063 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9064 " aaaaaaaaaaaaaa);");
9066 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9067 " aaaaaa->aaaaaaaaaaaa()\n"
9068 " ->aaaaaaaaaaaaaaaa(\n"
9069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9070 " ->aaaaaaaaaaaaaaaaa();");
9073 " someo->Add((new util::filetools::Handler(dir))\n"
9074 " ->OnEvent1(NewPermanentCallback(\n"
9075 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9076 " ->OnEvent2(NewPermanentCallback(\n"
9077 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9078 " ->OnEvent3(NewPermanentCallback(\n"
9079 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9080 " ->OnEvent5(NewPermanentCallback(\n"
9081 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9082 " ->OnEvent6(NewPermanentCallback(\n"
9083 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9087 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9088 verifyFormat("aaaaaaaaaaaaaaa()\n"
9089 " .aaaaaaaaaaaaaaa()\n"
9090 " .aaaaaaaaaaaaaaa()\n"
9091 " .aaaaaaaaaaaaaaa()\n"
9092 " .aaaaaaaaaaaaaaa();");
9093 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9094 " .aaaaaaaaaaaaaaa()\n"
9095 " .aaaaaaaaaaaaaaa()\n"
9096 " .aaaaaaaaaaaaaaa();");
9097 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9098 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9099 " .aaaaaaaaaaaaaaa();");
9100 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9101 " ->aaaaaaaaaaaaaae(0)\n"
9102 " ->aaaaaaaaaaaaaaa();");
9104 // Don't linewrap after very short segments.
9105 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9106 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9107 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9108 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9109 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9110 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9111 verifyFormat("aaa()\n"
9112 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9113 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9114 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9116 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9117 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9118 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9119 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9120 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9123 // Prefer not to break after empty parentheses.
9124 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9125 " First->LastNewlineOffset);");
9127 // Prefer not to create "hanging" indents.
9129 "return !soooooooooooooome_map\n"
9130 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9133 "return aaaaaaaaaaaaaaaa\n"
9134 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9135 " .aaaa(aaaaaaaaaaaaaa);");
9136 // No hanging indent here.
9137 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9139 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9141 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9142 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9143 getLLVMStyleWithColumns(60));
9144 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9145 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9146 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9147 getLLVMStyleWithColumns(59));
9148 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9150 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9152 // Dont break if only closing statements before member call
9153 verifyFormat("test() {\n"
9159 verifyFormat("test() {\n"
9168 verifyFormat("test() {\n"
9176 verifyFormat("test() {\n"
9181 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9184 getLLVMStyleWithColumns(30));
9187 TEST_F(FormatTest
, BreaksAccordingToOperatorPrecedence
) {
9189 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9190 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9192 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9193 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9195 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9196 " ccccccccccccccccccccccccc) {\n}");
9197 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9198 " ccccccccccccccccccccccccc) {\n}");
9200 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9201 " ccccccccccccccccccccccccc) {\n}");
9202 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9203 " ccccccccccccccccccccccccc) {\n}");
9206 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9207 " ccccccccccccccccccccccccc) {\n}");
9209 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9210 " ccccccccccccccccccccccccc) {\n}");
9212 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9213 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9214 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9215 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9216 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9217 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9218 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9219 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9221 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9222 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9223 " aaaaaaaaaaaaaaa != aa) {\n}");
9224 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9225 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9226 " aaaaaaaaaaaaaaa != aa) {\n}");
9229 TEST_F(FormatTest
, BreaksAfterAssignments
) {
9232 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9233 " SI->getPointerAddressSpaceee());");
9235 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9236 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9239 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9240 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9241 verifyFormat("unsigned OriginalStartColumn =\n"
9242 " SourceMgr.getSpellingColumnNumber(\n"
9243 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9247 TEST_F(FormatTest
, ConfigurableBreakAssignmentPenalty
) {
9248 FormatStyle Style
= getLLVMStyle();
9249 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9250 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9253 Style
.PenaltyBreakAssignment
= 20;
9254 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9255 " cccccccccccccccccccccccccc;",
9259 TEST_F(FormatTest
, AlignsAfterAssignments
) {
9261 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9262 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9264 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9265 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9267 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9268 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9270 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9271 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9273 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9274 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9275 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9278 TEST_F(FormatTest
, AlignsAfterReturn
) {
9280 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9281 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9283 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9284 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9286 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9287 " aaaaaaaaaaaaaaaaaaaaaa();");
9289 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9290 " aaaaaaaaaaaaaaaaaaaaaa());");
9291 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9293 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9296 verifyFormat("return\n"
9297 " // true if code is one of a or b.\n"
9298 " code == a || code == b;");
9301 TEST_F(FormatTest
, AlignsAfterOpenBracket
) {
9303 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9304 " aaaaaaaaa aaaaaaa) {}");
9306 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9307 " aaaaaaaaaaa aaaaaaaaa);");
9309 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9310 " aaaaaaaaaaaaaaaaaaaaa));");
9311 FormatStyle Style
= getLLVMStyle();
9312 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9313 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9314 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9316 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9317 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9319 verifyFormat("SomeLongVariableName->someFunction(\n"
9320 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9323 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9324 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9327 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9328 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9331 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9332 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9335 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9336 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9340 Style
.ColumnLimit
= 30;
9341 verifyFormat("for (int foo = 0; foo < FOO;\n"
9346 Style
.ColumnLimit
= 80;
9348 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
9349 Style
.BinPackArguments
= false;
9350 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
9351 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9352 " aaaaaaaaaaa aaaaaaaa,\n"
9353 " aaaaaaaaa aaaaaaa,\n"
9354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9356 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9357 " aaaaaaaaaaa aaaaaaaaa,\n"
9358 " aaaaaaaaaaa aaaaaaaaa,\n"
9359 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9361 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9362 " aaaaaaaaaaaaaaa,\n"
9363 " aaaaaaaaaaaaaaaaaaaaa,\n"
9364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9367 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9368 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9371 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9372 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9375 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9376 " aaaaaaaaaaaaaaaaaaaaa(\n"
9377 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9378 " aaaaaaaaaaaaaaaa);",
9381 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9382 " aaaaaaaaaaaaaaaaaaaaa(\n"
9383 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9384 " aaaaaaaaaaaaaaaa);",
9387 "fooooooooooo(new BARRRRRRRRR(\n"
9388 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9391 "fooooooooooo(::new BARRRRRRRRR(\n"
9392 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9395 "fooooooooooo(new FOO::BARRRR(\n"
9396 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9399 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
9400 Style
.BinPackArguments
= false;
9401 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
9402 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9403 " aaaaaaaaaaa aaaaaaaa,\n"
9404 " aaaaaaaaa aaaaaaa,\n"
9405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9408 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9409 " aaaaaaaaaaa aaaaaaaaa,\n"
9410 " aaaaaaaaaaa aaaaaaaaa,\n"
9411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9414 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9415 " aaaaaaaaaaaaaaa,\n"
9416 " aaaaaaaaaaaaaaaaaaaaa,\n"
9417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9420 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9421 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9424 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9425 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9429 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9430 " aaaaaaaaaaaaaaaaaaaaa(\n"
9431 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9433 " aaaaaaaaaaaaaaaa\n"
9437 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9438 " aaaaaaaaaaaaaaaaaaaaa(\n"
9439 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9441 " aaaaaaaaaaaaaaaa\n"
9444 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
9445 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9449 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9450 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
9455 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9456 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n"
9459 verifyFormat("void aaaaaaaaa(\n"
9460 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n"
9461 ") const noexcept -> std::vector<of_very_long_type>;",
9464 "x = aaaaaaaaaaaaaaa(\n"
9465 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
9468 Style
.ColumnLimit
= 60;
9469 verifyFormat("auto lambda =\n"
9471 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9476 TEST_F(FormatTest
, ParenthesesAndOperandAlignment
) {
9477 FormatStyle Style
= getLLVMStyleWithColumns(40);
9478 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9479 " bbbbbbbbbbbbbbbbbbbbbb);",
9481 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
9482 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9483 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9484 " bbbbbbbbbbbbbbbbbbbbbb);",
9486 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9487 Style
.AlignOperands
= FormatStyle::OAS_Align
;
9488 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9489 " bbbbbbbbbbbbbbbbbbbbbb);",
9491 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9492 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9493 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9494 " bbbbbbbbbbbbbbbbbbbbbb);",
9498 TEST_F(FormatTest
, BreaksConditionalExpressions
) {
9500 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9501 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9502 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9504 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9505 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9506 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9508 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9509 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9510 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9511 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9512 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9514 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9515 " : aaaaaaaaaaaaa);");
9517 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9518 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9519 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9520 " aaaaaaaaaaaaa);");
9522 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9523 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9524 " aaaaaaaaaaaaa);");
9525 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9526 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9528 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9530 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9532 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9534 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9536 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9537 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9539 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9541 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9542 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9543 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9544 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9545 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9547 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9548 " : aaaaaaaaaaaaaaaa;");
9550 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9551 " ? aaaaaaaaaaaaaaa\n"
9552 " : aaaaaaaaaaaaaaa;");
9553 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9557 verifyFormat("return aaaa == bbbb\n"
9561 verifyFormat("unsigned Indent =\n"
9562 " format(TheLine.First,\n"
9563 " IndentForLevel[TheLine.Level] >= 0\n"
9564 " ? IndentForLevel[TheLine.Level]\n"
9566 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9567 getLLVMStyleWithColumns(60));
9568 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9569 " ? aaaaaaaaaaaaaaa\n"
9570 " : bbbbbbbbbbbbbbb //\n"
9571 " ? ccccccccccccccc\n"
9572 " : ddddddddddddddd;");
9573 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9574 " ? aaaaaaaaaaaaaaa\n"
9575 " : (bbbbbbbbbbbbbbb //\n"
9576 " ? ccccccccccccccc\n"
9577 " : ddddddddddddddd);");
9579 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9580 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9581 " aaaaaaaaaaaaaaaaaaaaa +\n"
9582 " aaaaaaaaaaaaaaaaaaaaa\n"
9585 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9586 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9589 FormatStyle NoBinPacking
= getLLVMStyle();
9590 NoBinPacking
.BinPackArguments
= false;
9594 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9596 " ? aaaaaaaaaaaaaaa\n"
9597 " : aaaaaaaaaaaaaaa);\n"
9603 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9605 " ?: aaaaaaaaaaaaaaa);\n"
9609 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9611 " ccccccccccccccccccccccccccccccccccccccc\n"
9612 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9613 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9615 // Assignments in conditional expressions. Apparently not uncommon :-(.
9616 verifyFormat("return a != b\n"
9620 verifyFormat("return a != b\n"
9627 verifyFormat("return a != b\n"
9635 // Chained conditionals
9636 FormatStyle Style
= getLLVMStyleWithColumns(70);
9637 Style
.AlignOperands
= FormatStyle::OAS_Align
;
9638 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9639 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9640 " : 3333333333333333;",
9642 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9643 " : bbbbbbbbbb ? 2222222222222222\n"
9644 " : 3333333333333333;",
9646 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9647 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9648 " : 3333333333333333;",
9650 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9651 " : bbbbbbbbbbbbbb ? 222222\n"
9654 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9655 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9656 " : cccccccccccccc ? 3333333333333333\n"
9657 " : 4444444444444444;",
9659 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9660 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9661 " : 3333333333333333;",
9663 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9664 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9665 " : (aaa ? bbb : ccc);",
9668 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9669 " : cccccccccccccccccc)\n"
9670 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9671 " : 3333333333333333;",
9674 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9675 " : cccccccccccccccccc)\n"
9676 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9677 " : 3333333333333333;",
9680 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9681 " : dddddddddddddddddd)\n"
9682 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9683 " : 3333333333333333;",
9686 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9687 " : dddddddddddddddddd)\n"
9688 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9689 " : 3333333333333333;",
9692 "return aaaaaaaaa ? 1111111111111111\n"
9693 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9694 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9695 " : dddddddddddddddddd)",
9698 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9699 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9700 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9701 " : cccccccccccccccccc);",
9704 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9705 " : ccccccccccccccc ? dddddddddddddddddd\n"
9706 " : eeeeeeeeeeeeeeeeee)\n"
9707 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9708 " : 3333333333333333;",
9711 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9712 " : ccccccccccccccc ? dddddddddddddddddd\n"
9713 " : eeeeeeeeeeeeeeeeee)\n"
9714 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9715 " : 3333333333333333;",
9718 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9719 " : cccccccccccc ? dddddddddddddddddd\n"
9720 " : eeeeeeeeeeeeeeeeee)\n"
9721 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9722 " : 3333333333333333;",
9725 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9726 " : cccccccccccccccccc\n"
9727 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9728 " : 3333333333333333;",
9731 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9732 " : cccccccccccccccc ? dddddddddddddddddd\n"
9733 " : eeeeeeeeeeeeeeeeee\n"
9734 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9735 " : 3333333333333333;",
9737 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9738 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9739 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9740 " : eeeeeeeeeeeeeeeeee)\n"
9741 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9742 " : 3333333333333333;",
9744 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9745 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9746 " : cccccccccccccccc ? dddddddddddddddddd\n"
9747 " : eeeeeeeeeeeeeeeeee\n"
9748 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9749 " : 3333333333333333;",
9752 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9753 Style
.BreakBeforeTernaryOperators
= false;
9754 // FIXME: Aligning the question marks is weird given DontAlign.
9755 // Consider disabling this alignment in this case. Also check whether this
9756 // will render the adjustment from https://reviews.llvm.org/D82199
9758 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9759 " bbbb ? cccccccccccccccccc :\n"
9764 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9769 " return JJJJJJJJJJJJJJ(\n"
9770 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9774 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9779 " return JJJJJJJJJJJJJJ(\n"
9780 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9784 getGoogleStyle(FormatStyle::LK_JavaScript
));
9787 TEST_F(FormatTest
, BreaksConditionalExpressionsAfterOperator
) {
9788 FormatStyle Style
= getLLVMStyleWithColumns(70);
9789 Style
.BreakBeforeTernaryOperators
= false;
9791 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9796 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9797 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9801 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9804 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9805 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9809 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9813 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9814 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9819 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9820 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9823 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9829 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9835 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9837 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9841 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9843 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9844 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9845 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9847 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9853 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9854 " aaaaaaaaaaaaaaa :\n"
9855 " aaaaaaaaaaaaaaa;",
9857 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9862 verifyFormat("unsigned Indent =\n"
9863 " format(TheLine.First,\n"
9864 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9865 " IndentForLevel[TheLine.Level] :\n"
9867 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9869 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9870 " aaaaaaaaaaaaaaa :\n"
9871 " bbbbbbbbbbbbbbb ? //\n"
9872 " ccccccccccccccc :\n"
9873 " ddddddddddddddd;",
9875 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9876 " aaaaaaaaaaaaaaa :\n"
9877 " (bbbbbbbbbbbbbbb ? //\n"
9878 " ccccccccccccccc :\n"
9879 " ddddddddddddddd);",
9881 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9882 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9883 " ccccccccccccccccccccccccccc;",
9885 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9887 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9890 // Chained conditionals
9891 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9892 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9893 " 3333333333333333;",
9895 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9896 " bbbbbbbbbb ? 2222222222222222 :\n"
9897 " 3333333333333333;",
9899 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9900 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9901 " 3333333333333333;",
9903 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9904 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9907 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9908 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9909 " cccccccccccccccc ? 3333333333333333 :\n"
9910 " 4444444444444444;",
9912 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9913 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9914 " 3333333333333333;",
9916 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9917 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9918 " (aaa ? bbb : ccc);",
9921 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9922 " cccccccccccccccccc) :\n"
9923 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9924 " 3333333333333333;",
9927 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9928 " cccccccccccccccccc) :\n"
9929 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9930 " 3333333333333333;",
9933 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9934 " dddddddddddddddddd) :\n"
9935 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9936 " 3333333333333333;",
9939 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9940 " dddddddddddddddddd) :\n"
9941 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9942 " 3333333333333333;",
9945 "return aaaaaaaaa ? 1111111111111111 :\n"
9946 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9947 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9948 " dddddddddddddddddd)",
9951 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9952 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9953 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9954 " cccccccccccccccccc);",
9957 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9958 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9959 " eeeeeeeeeeeeeeeeee) :\n"
9960 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9961 " 3333333333333333;",
9964 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9965 " ccccccccccccc ? dddddddddddddddddd :\n"
9966 " eeeeeeeeeeeeeeeeee) :\n"
9967 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9968 " 3333333333333333;",
9971 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9972 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9973 " eeeeeeeeeeeeeeeeee) :\n"
9974 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9975 " 3333333333333333;",
9978 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9979 " cccccccccccccccccc :\n"
9980 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9981 " 3333333333333333;",
9984 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9985 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9986 " eeeeeeeeeeeeeeeeee :\n"
9987 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9988 " 3333333333333333;",
9990 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9991 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9992 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9993 " eeeeeeeeeeeeeeeeee) :\n"
9994 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9995 " 3333333333333333;",
9997 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9998 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9999 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
10000 " eeeeeeeeeeeeeeeeee :\n"
10001 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10002 " 3333333333333333;",
10006 TEST_F(FormatTest
, DeclarationsOfMultipleVariables
) {
10007 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
10008 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
10009 verifyFormat("bool a = true, b = false;");
10011 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
10013 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
10014 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
10016 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
10017 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
10019 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
10020 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
10021 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10022 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
10023 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
10024 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
10026 FormatStyle Style
= getGoogleStyle();
10027 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
10028 Style
.DerivePointerAlignment
= false;
10029 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10030 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
10031 " *b = bbbbbbbbbbbbbbbbbbb;",
10033 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10034 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
10036 verifyFormat("vector<int*> a, b;", Style
);
10037 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style
);
10038 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style
);
10039 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style
);
10040 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
10042 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
10045 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
10048 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style
);
10049 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style
);
10050 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style
);
10051 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style
);
10052 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
10056 TEST_F(FormatTest
, ConditionalExpressionsInBrackets
) {
10057 verifyFormat("arr[foo ? bar : baz];");
10058 verifyFormat("f()[foo ? bar : baz];");
10059 verifyFormat("(a + b)[foo ? bar : baz];");
10060 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10063 TEST_F(FormatTest
, AlignsStringLiterals
) {
10064 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10065 " \"short literal\");");
10067 "looooooooooooooooooooooooongFunction(\n"
10068 " \"short literal\"\n"
10069 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10070 verifyFormat("someFunction(\"Always break between multi-line\"\n"
10071 " \" string literals\",\n"
10072 " also, other, parameters);");
10073 verifyFormat("fun + \"1243\" /* comment */\n"
10075 "fun + \"1243\" /* comment */\n"
10077 getLLVMStyleWithColumns(28));
10079 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10080 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10081 " \"aaaaaaaaaaaaaaaa\";",
10083 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10084 "aaaaaaaaaaaaaaaaaaaaa\" "
10085 "\"aaaaaaaaaaaaaaaa\";");
10086 verifyFormat("a = a + \"a\"\n"
10089 verifyFormat("f(\"a\", \"b\"\n"
10093 "#define LL_FORMAT \"ll\"\n"
10094 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10095 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10097 verifyFormat("#define A(X) \\\n"
10098 " \"aaaaa\" #X \"bbbbbb\" \\\n"
10100 getLLVMStyleWithColumns(23));
10101 verifyFormat("#define A \"def\"\n"
10102 "f(\"abc\" A \"ghi\"\n"
10105 verifyFormat("f(L\"a\"\n"
10107 verifyFormat("#define A(X) \\\n"
10108 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10110 getLLVMStyleWithColumns(25));
10112 verifyFormat("f(@\"a\"\n"
10114 verifyFormat("NSString s = @\"a\"\n"
10117 verifyFormat("NSString s = @\"a\"\n"
10122 TEST_F(FormatTest
, ReturnTypeBreakingStyle
) {
10123 FormatStyle Style
= getLLVMStyle();
10124 Style
.ColumnLimit
= 60;
10126 // No declarations or definitions should be moved to own line.
10127 Style
.BreakAfterReturnType
= FormatStyle::RTBS_None
;
10128 verifyFormat("class A {\n"
10129 " int f() { return 1; }\n"
10132 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10134 "int f() { return 1; }\n"
10136 "int foooooooooooooooooooooooooooo::\n"
10137 " baaaaaaaaaaaaaaaaaaaaar();",
10140 // It is now allowed to break after a short return type if necessary.
10141 Style
.BreakAfterReturnType
= FormatStyle::RTBS_Automatic
;
10142 verifyFormat("class A {\n"
10143 " int f() { return 1; }\n"
10146 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10148 "int f() { return 1; }\n"
10151 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10154 // It now must never break after a short return type.
10155 Style
.BreakAfterReturnType
= FormatStyle::RTBS_ExceptShortType
;
10156 verifyFormat("class A {\n"
10157 " int f() { return 1; }\n"
10159 " long foooooooooooooooooooooooooooo::\n"
10160 " baaaaaaaaaaaaaaaaaaaar();\n"
10162 "int f() { return 1; }\n"
10164 "int foooooooooooooooooooooooooooo::\n"
10165 " baaaaaaaaaaaaaaaaaaaaar();",
10168 // All declarations and definitions should have the return type moved to its
10170 Style
.BreakAfterReturnType
= FormatStyle::RTBS_All
;
10171 Style
.TypenameMacros
= {"LIST"};
10172 verifyFormat("SomeType\n"
10173 "funcdecl(LIST(uint64_t));",
10175 verifyFormat("class E {\n"
10183 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10192 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10195 // Top-level definitions, and no kinds of declarations should have the
10196 // return type moved to its own line.
10197 Style
.BreakAfterReturnType
= FormatStyle::RTBS_TopLevelDefinitions
;
10198 verifyFormat("class B {\n"
10199 " int f() { return 1; }\n"
10209 // Top-level definitions and declarations should have the return type moved
10210 // to its own line.
10211 Style
.BreakAfterReturnType
= FormatStyle::RTBS_TopLevel
;
10212 verifyFormat("class C {\n"
10213 " int f() { return 1; }\n"
10223 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10226 // All definitions should have the return type moved to its own line, but no
10227 // kinds of declarations.
10228 Style
.BreakAfterReturnType
= FormatStyle::RTBS_AllDefinitions
;
10229 verifyFormat("class D {\n"
10242 verifyFormat("const char *\n"
10243 "f(void) {\n" // Break here.
10246 "const char *bar(void);", // No break here.
10248 verifyFormat("template <class T>\n"
10250 "f(T &c) {\n" // Break here.
10253 "template <class T> T *f(T &c);", // No break here.
10255 verifyFormat("class C {\n"
10261 " operator()() {\n"
10266 verifyFormat("void\n"
10267 "A::operator()() {}\n"
10269 "A::operator>>() {}\n"
10271 "A::operator+() {}\n"
10273 "A::operator*() {}\n"
10275 "A::operator->() {}\n"
10277 "A::operator void *() {}\n"
10279 "A::operator void &() {}\n"
10281 "A::operator void &&() {}\n"
10283 "A::operator char *() {}\n"
10285 "A::operator[]() {}\n"
10287 "A::operator!() {}\n"
10289 "A::operator**() {}\n"
10291 "A::operator<Foo> *() {}\n"
10293 "A::operator<Foo> **() {}\n"
10295 "A::operator<Foo> &() {}\n"
10297 "A::operator void **() {}",
10299 verifyFormat("constexpr auto\n"
10300 "operator()() const -> reference {}\n"
10302 "operator>>() const -> reference {}\n"
10304 "operator+() const -> reference {}\n"
10306 "operator*() const -> reference {}\n"
10308 "operator->() const -> reference {}\n"
10310 "operator++() const -> reference {}\n"
10312 "operator void *() const -> reference {}\n"
10314 "operator void **() const -> reference {}\n"
10316 "operator void *() const -> reference {}\n"
10318 "operator void &() const -> reference {}\n"
10320 "operator void &&() const -> reference {}\n"
10322 "operator char *() const -> reference {}\n"
10324 "operator!() const -> reference {}\n"
10326 "operator[]() const -> reference {}",
10328 verifyFormat("void *operator new(std::size_t s);", // No break here.
10330 verifyFormat("void *\n"
10331 "operator new(std::size_t s) {}",
10333 verifyFormat("void *\n"
10334 "operator delete[](void *ptr) {}",
10336 Style
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
10337 verifyFormat("const char *\n"
10338 "f(void)\n" // Break here.
10342 "const char *bar(void);", // No break here.
10344 verifyFormat("template <class T>\n"
10345 "T *\n" // Problem here: no line break
10346 "f(T &c)\n" // Break here.
10350 "template <class T> T *f(T &c);", // No break here.
10352 verifyFormat("int\n"
10358 verifyFormat("int\n"
10364 verifyFormat("int\n"
10365 "foo(A<B<bool>, 8> a)\n"
10370 verifyFormat("int\n"
10371 "foo(A<B<8>, bool> a)\n"
10376 verifyFormat("int\n"
10377 "foo(A<B<bool>, bool> a)\n"
10382 verifyFormat("int\n"
10383 "foo(A<B<8>, 8> a)\n"
10389 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
10390 Style
.BraceWrapping
.AfterFunction
= true;
10391 verifyFormat("int f(i);\n" // No break here.
10392 "int\n" // Break here.
10397 "int\n" // Break here.
10403 verifyFormat("int f(a, b, c);\n" // No break here.
10404 "int\n" // Break here.
10405 "f(a, b, c)\n" // Break here.
10409 " return a + b < c;\n"
10411 "int\n" // Break here.
10412 "f(a, b, c)\n" // Break here.
10416 " return a + b < c;\n"
10419 verifyFormat("byte *\n" // Break here.
10420 "f(a)\n" // Break here.
10426 verifyFormat("byte *\n"
10428 "byte /* K&R C */ a[];\n"
10434 "byte /* K&R C */ *p;\n"
10439 verifyFormat("bool f(int a, int) override;\n"
10440 "Bar g(int a, Bar) final;\n"
10441 "Bar h(a, Bar) final;",
10443 verifyFormat("int\n"
10446 verifyFormat("bool\n"
10447 "f(size_t = 0, bool b = false)\n"
10453 // The return breaking style doesn't affect:
10454 // * function and object definitions with attribute-like macros
10455 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10456 " ABSL_GUARDED_BY(mutex) = {};",
10457 getGoogleStyleWithColumns(40));
10458 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10459 " ABSL_GUARDED_BY(mutex); // comment",
10460 getGoogleStyleWithColumns(40));
10461 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10462 " ABSL_GUARDED_BY(mutex1)\n"
10463 " ABSL_GUARDED_BY(mutex2);",
10464 getGoogleStyleWithColumns(40));
10465 verifyFormat("Tttttt f(int a, int b)\n"
10466 " ABSL_GUARDED_BY(mutex1)\n"
10467 " ABSL_GUARDED_BY(mutex2);",
10468 getGoogleStyleWithColumns(40));
10470 verifyGoogleFormat("typedef ATTR(X) char x;");
10472 Style
= getGNUStyle();
10474 // Test for comments at the end of function declarations.
10475 verifyFormat("void\n"
10476 "foo (int a, /*abc*/ int b) // def\n"
10481 verifyFormat("void\n"
10482 "foo (int a, /* abc */ int b) /* def */\n"
10487 // Definitions that should not break after return type
10488 verifyFormat("void foo (int a, int b); // def", Style
);
10489 verifyFormat("void foo (int a, int b); /* def */", Style
);
10490 verifyFormat("void foo (int a, int b);", Style
);
10493 TEST_F(FormatTest
, AlwaysBreakBeforeMultilineStrings
) {
10494 FormatStyle NoBreak
= getLLVMStyle();
10495 NoBreak
.AlwaysBreakBeforeMultilineStrings
= false;
10496 FormatStyle Break
= getLLVMStyle();
10497 Break
.AlwaysBreakBeforeMultilineStrings
= true;
10498 verifyFormat("aaaa = \"bbbb\"\n"
10501 verifyFormat("aaaa =\n"
10505 verifyFormat("aaaa(\"bbbb\"\n"
10508 verifyFormat("aaaa(\n"
10512 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10515 verifyFormat("aaaa(qqq,\n"
10519 verifyFormat("aaaa(qqq,\n"
10523 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10526 verifyFormat("string s = someFunction(\n"
10531 // As we break before unary operators, breaking right after them is bad.
10532 verifyFormat("string foo = abc ? \"x\"\n"
10533 " \"blah blah blah blah blah blah\"\n"
10537 // Don't break if there is no column gain.
10538 verifyFormat("f(\"aaaa\"\n"
10542 // Treat literals with escaped newlines like multi-line string literals.
10543 verifyNoChange("x = \"a\\\n"
10547 verifyFormat("xxxx =\n"
10556 verifyFormat("NSString *const kString =\n"
10559 "NSString *const kString = @\"aaaa\"\n"
10563 Break
.ColumnLimit
= 0;
10564 verifyFormat("const char *hello = \"hello llvm\";", Break
);
10567 TEST_F(FormatTest
, AlignsPipes
) {
10569 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10570 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10571 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10573 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10574 " << aaaaaaaaaaaaaaaaaaaa;");
10576 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10577 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10579 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10580 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10582 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10583 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10584 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10586 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10587 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10588 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10589 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10592 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10593 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10594 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10596 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10599 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10600 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10602 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10603 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10604 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10606 " aaaaaaaaaaaaaaaaaaaaa)\n"
10607 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10608 verifyFormat("LOG_IF(aaa == //\n"
10612 // But sometimes, breaking before the first "<<" is desirable.
10613 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10614 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10615 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10616 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10617 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10618 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10619 " << BEF << IsTemplate << Description << E->getType();");
10620 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10621 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10623 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10624 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10629 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10630 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10632 // Incomplete string literal.
10633 verifyFormat("llvm::errs() << \"\n"
10635 "llvm::errs() << \"\n<<a;");
10637 verifyFormat("void f() {\n"
10638 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10639 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10643 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10644 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10645 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10648 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10649 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10650 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10651 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10652 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10653 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10654 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10657 TEST_F(FormatTest
, KeepStringLabelValuePairsOnALine
) {
10658 verifyFormat("return out << \"somepacket = {\\n\"\n"
10659 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10660 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10661 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10662 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10665 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10666 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10667 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10669 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10670 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10671 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10672 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10673 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10674 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10675 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10678 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10679 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10682 // Breaking before the first "<<" is generally not desirable.
10685 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10686 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10687 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10688 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10689 getLLVMStyleWithColumns(70));
10690 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10691 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10692 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10693 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10694 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10695 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10696 getLLVMStyleWithColumns(70));
10698 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10699 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10700 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10701 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10702 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10703 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10704 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10706 getLLVMStyleWithColumns(40));
10707 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10708 " (aaaaaaa + aaaaa));",
10709 getLLVMStyleWithColumns(40));
10711 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10712 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10713 " bbbbbbbbbbbbbbbbbbbbbbb);");
10716 TEST_F(FormatTest
, WrapBeforeInsertionOperatorbetweenStringLiterals
) {
10717 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10719 verifyNoChange("QStringList() << \"foo\"\n"
10722 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10723 "log_error(log, \"foo\"\n"
10727 TEST_F(FormatTest
, UnderstandsEquals
) {
10729 "aaaaaaaaaaaaaaaaa =\n"
10730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10732 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10737 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10741 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10742 " 100000000 + 10000000) {\n}");
10745 TEST_F(FormatTest
, WrapsAtFunctionCallsIfNecessary
) {
10746 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10747 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10749 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10750 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10753 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10757 "ShortObject->shortFunction(\n"
10758 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10759 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10761 verifyFormat("loooooooooooooongFunction(\n"
10762 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10765 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10766 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10768 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10769 " .WillRepeatedly(Return(SomeValue));");
10770 verifyFormat("void f() {\n"
10771 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10773 " .WillRepeatedly(Return(SomeValue));\n"
10775 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10776 " ccccccccccccccccccccccc);");
10777 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10779 " .aaaaa(aaaaa),\n"
10780 " aaaaaaaaaaaaaaaaaaaaa);");
10781 verifyFormat("void f() {\n"
10782 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10783 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10785 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10787 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10788 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10789 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10790 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10791 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10792 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10793 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10796 // Here, it is not necessary to wrap at "." or "->".
10797 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10798 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10800 "aaaaaaaaaaa->aaaaaaaaa(\n"
10801 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10802 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10805 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10807 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10808 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10809 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10810 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10812 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10816 FormatStyle NoBinPacking
= getLLVMStyle();
10817 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
10818 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10819 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10820 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10821 " aaaaaaaaaaaaaaaaaaa,\n"
10822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10825 // If there is a subsequent call, change to hanging indentation.
10827 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10828 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10829 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10831 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10832 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10833 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10835 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10836 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10838 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10841 TEST_F(FormatTest
, WrapsTemplateDeclarations
) {
10842 verifyFormat("template <typename T>\n"
10843 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10844 verifyFormat("template <typename T>\n"
10845 "// T should be one of {A, B}.\n"
10846 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10848 "template <typename T>\n"
10849 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10850 verifyFormat("template <typename T>\n"
10851 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10852 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10854 "template <typename T>\n"
10855 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10856 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10858 "template <typename T>\n"
10859 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10860 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10862 verifyFormat("template <typename T>\n"
10863 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10864 " int aaaaaaaaaaaaaaaaaaaaaa);");
10866 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10867 " typename T4 = char>\n"
10869 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10870 " template <typename> class cccccccccccccccccccccc,\n"
10871 " typename ddddddddddddd>\n"
10874 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10877 verifyFormat("void f() {\n"
10878 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10879 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10882 verifyFormat("template <typename T> class C {};");
10883 verifyFormat("template <typename T> void f();");
10884 verifyFormat("template <typename T> void f() {}");
10886 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10889 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10892 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10893 getLLVMStyleWithColumns(72));
10894 verifyFormat("static_cast<A< //\n"
10898 "static_cast<A<//\n"
10902 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10903 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10905 FormatStyle AlwaysBreak
= getLLVMStyle();
10906 AlwaysBreak
.BreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
10907 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak
);
10908 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak
);
10909 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak
);
10910 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10911 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10912 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10913 verifyFormat("template <template <typename> class Fooooooo,\n"
10914 " template <typename> class Baaaaaaar>\n"
10917 verifyFormat("template <typename T> // T can be A, B or C.\n"
10920 verifyFormat("template <typename T>\n"
10923 verifyFormat("template <typename T>\n"
10924 "ClassName(T) noexcept;",
10926 verifyFormat("template <typename T>\n"
10927 "POOR_NAME(T) noexcept;",
10929 verifyFormat("template <enum E> class A {\n"
10934 FormatStyle NeverBreak
= getLLVMStyle();
10935 NeverBreak
.BreakTemplateDeclarations
= FormatStyle::BTDS_No
;
10936 verifyFormat("template <typename T> class C {};", NeverBreak
);
10937 verifyFormat("template <typename T> void f();", NeverBreak
);
10938 verifyFormat("template <typename T> void f() {}", NeverBreak
);
10939 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak
);
10940 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak
);
10941 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak
);
10942 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10943 "bbbbbbbbbbbbbbbbbbbb) {}",
10945 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10946 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10947 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10949 verifyFormat("template <template <typename> class Fooooooo,\n"
10950 " template <typename> class Baaaaaaar>\n"
10953 verifyFormat("template <typename T> // T can be A, B or C.\n"
10956 verifyFormat("template <enum E> class A {\n"
10961 NeverBreak
.PenaltyBreakTemplateDeclaration
= 100;
10962 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10963 "bbbbbbbbbbbbbbbbbbbb) {}",
10966 auto Style
= getLLVMStyle();
10967 Style
.BreakTemplateDeclarations
= FormatStyle::BTDS_Leave
;
10969 verifyNoChange("template <typename T>\n"
10972 verifyFormat("template <typename T> class C {};", Style
);
10974 verifyNoChange("template <typename T>\n"
10977 verifyFormat("template <typename T> void f();", Style
);
10979 verifyNoChange("template <typename T>\n"
10982 verifyFormat("template <typename T> void f() {}", Style
);
10984 verifyNoChange("template <typename T>\n"
10985 "// T can be A, B or C.\n"
10988 verifyFormat("template <typename T> // T can be A, B or C.\n"
10992 verifyNoChange("template <typename T>\n"
10995 verifyFormat("template <typename T> C(T) noexcept;", Style
);
10997 verifyNoChange("template <enum E>\n"
11003 verifyFormat("template <enum E> class A {\n"
11009 verifyNoChange("template <auto x>\n"
11010 "constexpr int simple(int) {\n"
11015 verifyFormat("template <auto x> constexpr int simple(int) {\n"
11021 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
11022 verifyNoChange("template <auto x>\n"
11023 "requires(x > 1)\n"
11024 "constexpr int with_req(int) {\n"
11028 verifyFormat("template <auto x> requires(x > 1)\n"
11029 "constexpr int with_req(int) {\n"
11035 TEST_F(FormatTest
, WrapsTemplateDeclarationsWithComments
) {
11036 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Cpp
);
11037 Style
.ColumnLimit
= 60;
11038 verifyFormat("// Baseline - no comments.\n"
11040 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
11044 verifyFormat("template <\n"
11045 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11048 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11054 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11056 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11060 verifyFormat("template <\n"
11061 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11065 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11071 "template <typename aaaaaaaaaa<\n"
11072 " bbbbbbbbbbbb>::value> // trailing loooong\n"
11075 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11080 TEST_F(FormatTest
, WrapsTemplateParameters
) {
11081 FormatStyle Style
= getLLVMStyle();
11082 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
11083 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
11085 "template <typename... a> struct q {};\n"
11086 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11087 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11090 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
11091 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
11093 "template <typename... a> struct r {};\n"
11094 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11095 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11098 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
11099 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
11100 verifyFormat("template <typename... a> struct s {};\n"
11102 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11103 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11104 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11105 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11108 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
11109 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
11110 verifyFormat("template <typename... a> struct t {};\n"
11112 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11113 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11114 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11115 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11120 TEST_F(FormatTest
, WrapsAtNestedNameSpecifiers
) {
11122 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11125 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11129 // FIXME: Should we have the extra indent after the second break?
11131 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11136 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11137 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11139 // Breaking at nested name specifiers is generally not desirable.
11141 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11142 " aaaaaaaaaaaaaaaaaaaaaaa);");
11144 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11147 " aaaaaaaaaaaaaaaaaaaaa);",
11148 getLLVMStyleWithColumns(74));
11150 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11152 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11155 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11156 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11157 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11158 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11161 TEST_F(FormatTest
, UnderstandsTemplateParameters
) {
11162 verifyFormat("A<int> a;");
11163 verifyFormat("A<A<A<int>>> a;");
11164 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11165 verifyFormat("bool x = a < 1 || 2 > a;");
11166 verifyFormat("bool x = 5 < f<int>();");
11167 verifyFormat("bool x = f<int>() > 5;");
11168 verifyFormat("bool x = 5 < a<int>::x;");
11169 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11170 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11172 verifyGoogleFormat("A<A<int>> a;");
11173 verifyGoogleFormat("A<A<A<int>>> a;");
11174 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11175 verifyGoogleFormat("A<A<int> > a;");
11176 verifyGoogleFormat("A<A<A<int> > > a;");
11177 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11178 verifyGoogleFormat("A<::A<int>> a;");
11179 verifyGoogleFormat("A<::A> a;");
11180 verifyGoogleFormat("A< ::A> a;");
11181 verifyGoogleFormat("A< ::A<int> > a;");
11182 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11183 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11184 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11185 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11186 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11189 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp
));
11191 // template closer followed by a token that starts with > or =
11192 verifyFormat("bool b = a<1> > 1;");
11193 verifyFormat("bool b = a<1> >= 1;");
11194 verifyFormat("int i = a<1> >> 1;");
11195 FormatStyle Style
= getLLVMStyle();
11196 Style
.SpaceBeforeAssignmentOperators
= false;
11197 verifyFormat("bool b= a<1> == 1;", Style
);
11198 verifyFormat("a<int> = 1;", Style
);
11199 verifyFormat("a<int> >>= 1;", Style
);
11201 verifyFormat("test < a | b >> c;");
11202 verifyFormat("test<test<a | b>> c;");
11203 verifyFormat("test >> a >> b;");
11204 verifyFormat("test << a >> b;");
11206 verifyFormat("f<int>();");
11207 verifyFormat("template <typename T> void f() {}");
11208 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11209 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11210 "sizeof(char)>::type>;");
11211 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11212 verifyFormat("f(a.operator()<A>());");
11213 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11214 " .template operator()<A>());",
11215 getLLVMStyleWithColumns(35));
11216 verifyFormat("bool_constant<a && noexcept(f())>;");
11217 verifyFormat("bool_constant<a || noexcept(f())>;");
11219 verifyFormat("if (std::tuple_size_v<T> > 0)");
11221 // Not template parameters.
11222 verifyFormat("return a < b && c > d;");
11223 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11224 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11225 verifyFormat("void f() {\n"
11226 " while (a < b && c > d) {\n"
11229 verifyFormat("template <typename... Types>\n"
11230 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11232 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11233 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11234 getLLVMStyleWithColumns(60));
11235 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11236 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11237 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11238 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11240 verifyFormat("#define FOO(typeName, realClass) \\\n"
11241 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11242 getLLVMStyleWithColumns(60));
11245 TEST_F(FormatTest
, UnderstandsShiftOperators
) {
11246 verifyFormat("if (i < x >> 1)");
11247 verifyFormat("while (i < x >> 1)");
11248 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11249 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11251 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11252 verifyFormat("Foo.call<Bar<Function>>()");
11253 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11254 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11255 "++i, v = v >> 1)");
11256 verifyFormat("if (w<u<v<x>>, 1>::t)");
11259 TEST_F(FormatTest
, BitshiftOperatorWidth
) {
11260 verifyFormat("int a = 1 << 2; /* foo\n"
11262 "int a=1<<2; /* foo\n"
11265 verifyFormat("int b = 256 >> 1; /* foo\n"
11267 "int b =256>>1 ; /* foo\n"
11271 TEST_F(FormatTest
, UnderstandsBinaryOperators
) {
11272 verifyFormat("COMPARE(a, ==, b);");
11273 verifyFormat("auto s = sizeof...(Ts) - 1;");
11276 TEST_F(FormatTest
, UnderstandsPointersToMembers
) {
11277 verifyFormat("int A::*x;");
11278 verifyFormat("int (S::*func)(void *);");
11279 verifyFormat("void f() { int (S::*func)(void *); }");
11280 verifyFormat("typedef bool *(Class::*Member)() const;");
11281 verifyFormat("void f() {\n"
11288 verifyFormat("void f() {\n"
11289 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11290 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11293 "(aaaaaaaaaa->*bbbbbbb)(\n"
11294 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11296 FormatStyle Style
= getLLVMStyle();
11297 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
11298 verifyFormat("typedef bool *(Class::*Member)() const;", Style
);
11299 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style
);
11301 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11302 verifyFormat("typedef bool* (Class::*Member)() const;", Style
);
11303 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style
);
11305 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11306 verifyFormat("typedef bool * (Class::*Member)() const;", Style
);
11307 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style
);
11310 TEST_F(FormatTest
, UnderstandsUnaryOperators
) {
11311 verifyFormat("int a = -2;");
11312 verifyFormat("f(-1, -2, -3);");
11313 verifyFormat("a[-1] = 5;");
11314 verifyFormat("int a = 5 + -2;");
11315 verifyFormat("if (i == -1) {\n}");
11316 verifyFormat("if (i != -1) {\n}");
11317 verifyFormat("if (i > -1) {\n}");
11318 verifyFormat("if (i < -1) {\n}");
11319 verifyFormat("++(a->f());");
11320 verifyFormat("--(a->f());");
11321 verifyFormat("(a->f())++;");
11322 verifyFormat("a[42]++;");
11323 verifyFormat("if (!(a->f())) {\n}");
11324 verifyFormat("if (!+i) {\n}");
11325 verifyFormat("~&a;");
11326 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11327 verifyFormat("sizeof -x");
11328 verifyFormat("sizeof +x");
11329 verifyFormat("sizeof *x");
11330 verifyFormat("sizeof &x");
11331 verifyFormat("delete +x;");
11332 verifyFormat("co_await +x;");
11333 verifyFormat("case *x:");
11334 verifyFormat("case &x:");
11336 verifyFormat("a-- > b;");
11337 verifyFormat("b ? -a : c;");
11338 verifyFormat("n * sizeof char16;");
11339 verifyGoogleFormat("n * alignof char16;");
11340 verifyFormat("sizeof(char);");
11341 verifyGoogleFormat("alignof(char);");
11343 verifyFormat("return -1;");
11344 verifyFormat("throw -1;");
11345 verifyFormat("switch (a) {\n"
11349 verifyFormat("#define X -1");
11350 verifyFormat("#define X -kConstant");
11352 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11353 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11355 verifyFormat("int a = /* confusing comment */ -1;");
11356 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11357 verifyFormat("int a = i /* confusing comment */++;");
11359 verifyFormat("co_yield -1;");
11360 verifyFormat("co_return -1;");
11362 // Check that * is not treated as a binary operator when we set
11363 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11364 FormatStyle PASLeftStyle
= getLLVMStyle();
11365 PASLeftStyle
.PointerAlignment
= FormatStyle::PAS_Left
;
11366 verifyFormat("co_return *a;", PASLeftStyle
);
11367 verifyFormat("co_await *a;", PASLeftStyle
);
11368 verifyFormat("co_yield *a", PASLeftStyle
);
11369 verifyFormat("return *a;", PASLeftStyle
);
11372 TEST_F(FormatTest
, DoesNotIndentRelativeToUnaryOperators
) {
11373 verifyFormat("if (!aaaaaaaaaa( // break\n"
11376 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11378 verifyFormat("*aaa = aaaaaaa( // break\n"
11382 TEST_F(FormatTest
, UnderstandsOverloadedOperators
) {
11383 verifyFormat("bool operator<();");
11384 verifyFormat("bool operator>();");
11385 verifyFormat("bool operator=();");
11386 verifyFormat("bool operator==();");
11387 verifyFormat("bool operator!=();");
11388 verifyFormat("int operator+();");
11389 verifyFormat("int operator++();");
11390 verifyFormat("int operator++(int) volatile noexcept;");
11391 verifyFormat("bool operator,();");
11392 verifyFormat("bool operator();");
11393 verifyFormat("bool operator()();");
11394 verifyFormat("bool operator[]();");
11395 verifyFormat("operator bool();");
11396 verifyFormat("operator int();");
11397 verifyFormat("operator void *();");
11398 verifyFormat("operator SomeType<int>();");
11399 verifyFormat("operator SomeType<int, int>();");
11400 verifyFormat("operator SomeType<SomeType<int>>();");
11401 verifyFormat("operator< <>();");
11402 verifyFormat("operator<< <>();");
11403 verifyFormat("< <>");
11405 verifyFormat("void *operator new(std::size_t size);");
11406 verifyFormat("void *operator new[](std::size_t size);");
11407 verifyFormat("void operator delete(void *ptr);");
11408 verifyFormat("void operator delete[](void *ptr);");
11409 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11410 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11411 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11412 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11415 "ostream &operator<<(ostream &OutputStream,\n"
11416 " SomeReallyLongType WithSomeReallyLongValue);");
11417 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11418 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11419 " return left.group < right.group;\n"
11421 verifyFormat("SomeType &operator=(const SomeType &S);");
11422 verifyFormat("f.template operator()<int>();");
11424 verifyGoogleFormat("operator void*();");
11425 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11426 verifyGoogleFormat("operator ::A();");
11428 verifyFormat("using A::operator+;");
11429 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11432 // Calling an operator as a member function.
11433 verifyFormat("void f() { a.operator*(); }");
11434 verifyFormat("void f() { a.operator*(b & b); }");
11435 verifyFormat("void f() { a->operator&(a * b); }");
11436 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11437 verifyFormat("void f() { operator*(a & a); }");
11438 verifyFormat("void f() { operator&(a, b * b); }");
11440 verifyFormat("void f() { return operator()(x) * b; }");
11441 verifyFormat("void f() { return operator[](x) * b; }");
11442 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11443 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11444 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11445 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11446 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11448 verifyFormat("::operator delete(foo);");
11449 verifyFormat("::operator new(n * sizeof(foo));");
11450 verifyFormat("foo() { ::operator delete(foo); }");
11451 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11454 TEST_F(FormatTest
, SpaceBeforeTemplateCloser
) {
11455 verifyFormat("C<&operator- > minus;");
11456 verifyFormat("C<&operator> > gt;");
11457 verifyFormat("C<&operator>= > ge;");
11458 verifyFormat("C<&operator<= > le;");
11459 verifyFormat("C<&operator< <X>> lt;");
11462 TEST_F(FormatTest
, UnderstandsFunctionRefQualification
) {
11463 verifyFormat("void A::b() && {}");
11464 verifyFormat("void A::b() && noexcept {}");
11465 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11466 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11467 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11468 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11469 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11470 verifyFormat("Deleted &operator=(const Deleted &) &;");
11471 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11472 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11473 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11474 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11475 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11476 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11477 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11478 verifyFormat("void Fn(T const &) const &;");
11479 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11480 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11481 verifyGoogleFormat("template <typename T>\n"
11482 "void F(T) && = delete;");
11483 verifyFormat("template <typename T> void operator=(T) &;");
11484 verifyFormat("template <typename T> void operator=(T) const &;");
11485 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11486 verifyFormat("template <typename T> void operator=(T) & = default;");
11487 verifyFormat("template <typename T> void operator=(T) &&;");
11488 verifyFormat("template <typename T> void operator=(T) && = delete;");
11489 verifyFormat("template <typename T> void operator=(T) & {}");
11490 verifyFormat("template <typename T> void operator=(T) && {}");
11492 FormatStyle AlignLeft
= getLLVMStyle();
11493 AlignLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
11494 verifyFormat("void A::b() && {}", AlignLeft
);
11495 verifyFormat("void A::b() && noexcept {}", AlignLeft
);
11496 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft
);
11497 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11499 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11501 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft
);
11502 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft
);
11503 verifyFormat("auto Function(T t) & -> void {}", AlignLeft
);
11504 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft
);
11505 verifyFormat("auto Function(T) & -> void {}", AlignLeft
);
11506 verifyFormat("auto Function(T) & -> void;", AlignLeft
);
11507 verifyFormat("void Fn(T const&) const&;", AlignLeft
);
11508 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft
);
11509 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11511 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft
);
11512 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft
);
11513 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11515 verifyFormat("template <typename T> void operator=(T) & = default;",
11517 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft
);
11518 verifyFormat("template <typename T> void operator=(T) && = delete;",
11520 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft
);
11521 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft
);
11522 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft
);
11524 FormatStyle AlignMiddle
= getLLVMStyle();
11525 AlignMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
11526 verifyFormat("void A::b() && {}", AlignMiddle
);
11527 verifyFormat("void A::b() && noexcept {}", AlignMiddle
);
11528 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11530 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11532 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11534 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle
);
11535 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle
);
11536 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle
);
11537 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle
);
11538 verifyFormat("auto Function(T) & -> void {}", AlignMiddle
);
11539 verifyFormat("auto Function(T) & -> void;", AlignMiddle
);
11540 verifyFormat("void Fn(T const &) const &;", AlignMiddle
);
11541 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle
);
11542 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11544 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle
);
11545 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle
);
11546 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11548 verifyFormat("template <typename T> void operator=(T) & = default;",
11550 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle
);
11551 verifyFormat("template <typename T> void operator=(T) && = delete;",
11553 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle
);
11554 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle
);
11556 FormatStyle Spaces
= getLLVMStyle();
11557 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
11558 Spaces
.SpacesInParensOptions
= {};
11559 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
11560 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces
);
11561 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces
);
11562 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces
);
11563 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces
);
11565 Spaces
.SpacesInParensOptions
.InCStyleCasts
= false;
11566 Spaces
.SpacesInParensOptions
.Other
= true;
11567 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces
);
11568 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11570 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces
);
11571 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces
);
11573 FormatStyle BreakTemplate
= getLLVMStyle();
11574 BreakTemplate
.BreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
11576 verifyFormat("struct f {\n"
11577 " template <class T>\n"
11578 " int &foo(const std::string &str) & noexcept {}\n"
11582 verifyFormat("struct f {\n"
11583 " template <class T>\n"
11584 " int &foo(const std::string &str) && noexcept {}\n"
11588 verifyFormat("struct f {\n"
11589 " template <class T>\n"
11590 " int &foo(const std::string &str) const & noexcept {}\n"
11594 verifyFormat("struct f {\n"
11595 " template <class T>\n"
11596 " int &foo(const std::string &str) const & noexcept {}\n"
11600 verifyFormat("struct f {\n"
11601 " template <class T>\n"
11602 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11606 FormatStyle AlignLeftBreakTemplate
= getLLVMStyle();
11607 AlignLeftBreakTemplate
.BreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
11608 AlignLeftBreakTemplate
.PointerAlignment
= FormatStyle::PAS_Left
;
11610 verifyFormat("struct f {\n"
11611 " template <class T>\n"
11612 " int& foo(const std::string& str) & noexcept {}\n"
11614 AlignLeftBreakTemplate
);
11616 verifyFormat("struct f {\n"
11617 " template <class T>\n"
11618 " int& foo(const std::string& str) && noexcept {}\n"
11620 AlignLeftBreakTemplate
);
11622 verifyFormat("struct f {\n"
11623 " template <class T>\n"
11624 " int& foo(const std::string& str) const& noexcept {}\n"
11626 AlignLeftBreakTemplate
);
11628 verifyFormat("struct f {\n"
11629 " template <class T>\n"
11630 " int& foo(const std::string& str) const&& noexcept {}\n"
11632 AlignLeftBreakTemplate
);
11634 verifyFormat("struct f {\n"
11635 " template <class T>\n"
11636 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11638 AlignLeftBreakTemplate
);
11640 // The `&` in `Type&` should not be confused with a trailing `&` of
11641 // DEPRECATED(reason) member function.
11642 verifyFormat("struct f {\n"
11643 " template <class T>\n"
11644 " DEPRECATED(reason)\n"
11645 " Type &foo(arguments) {}\n"
11649 verifyFormat("struct f {\n"
11650 " template <class T>\n"
11651 " DEPRECATED(reason)\n"
11652 " Type& foo(arguments) {}\n"
11654 AlignLeftBreakTemplate
);
11656 verifyFormat("void (*foopt)(int) = &func;");
11658 FormatStyle DerivePointerAlignment
= getLLVMStyle();
11659 DerivePointerAlignment
.DerivePointerAlignment
= true;
11660 // There's always a space between the function and its trailing qualifiers.
11661 // This isn't evidence for PAS_Right (or for PAS_Left).
11662 std::string Prefix
= "void a() &;\n"
11664 verifyFormat(Prefix
+ "int* x;", DerivePointerAlignment
);
11665 verifyFormat(Prefix
+ "int *x;", DerivePointerAlignment
);
11666 // Same if the function is an overloaded operator, and with &&.
11667 Prefix
= "void operator()() &&;\n"
11668 "void operator()() &&;\n";
11669 verifyFormat(Prefix
+ "int* x;", DerivePointerAlignment
);
11670 verifyFormat(Prefix
+ "int *x;", DerivePointerAlignment
);
11671 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11672 Prefix
= "void a() const &;\n"
11673 "void b() const &;\n";
11674 verifyFormat(Prefix
+ "int *x;", Prefix
+ "int* x;", DerivePointerAlignment
);
11677 TEST_F(FormatTest
, PointerAlignmentFallback
) {
11678 FormatStyle Style
= getLLVMStyle();
11679 Style
.DerivePointerAlignment
= true;
11681 const StringRef
Code("int* p;\n"
11685 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
11686 verifyFormat("int *p;\n"
11691 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11692 verifyFormat("int* p;\n"
11697 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11698 verifyFormat("int * p;\n"
11704 TEST_F(FormatTest
, UnderstandsNewAndDelete
) {
11705 verifyFormat("void f() {\n"
11707 " A *a = new (placement) A;\n"
11709 " delete (A *)a;\n"
11711 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11712 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11713 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11714 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11715 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11716 verifyFormat("delete[] h->p;");
11717 verifyFormat("delete[] (void *)p;");
11719 verifyFormat("void operator delete(void *foo) ATTRIB;");
11720 verifyFormat("void operator new(void *foo) ATTRIB;");
11721 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11722 verifyFormat("void operator delete(void *ptr) noexcept;");
11724 verifyFormat("void new(link p);\n"
11725 "void delete(link p);",
11726 "void new (link p);\n"
11727 "void delete (link p);");
11742 FormatStyle AfterPlacementOperator
= getLLVMStyle();
11743 AfterPlacementOperator
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
11745 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
);
11746 verifyFormat("new (buf) int;", AfterPlacementOperator
);
11747 verifyFormat("struct A {\n"
11749 " A(int *p) : a(new (p) int) {\n"
11751 " int *b = new (p) int;\n"
11752 " int *c = new (p) int(3);\n"
11756 AfterPlacementOperator
);
11757 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator
);
11758 verifyFormat("delete (int *)p;", AfterPlacementOperator
);
11760 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
=
11762 verifyFormat("new(buf) int;", AfterPlacementOperator
);
11763 verifyFormat("struct A {\n"
11765 " A(int *p) : a(new(p) int) {\n"
11767 " int *b = new(p) int;\n"
11768 " int *c = new(p) int(3);\n"
11772 AfterPlacementOperator
);
11773 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator
);
11774 verifyFormat("delete (int *)p;", AfterPlacementOperator
);
11777 TEST_F(FormatTest
, UnderstandsUsesOfStarAndAmp
) {
11778 verifyFormat("int *f(int *a) {}");
11779 verifyFormat("int main(int argc, char **argv) {}");
11780 verifyFormat("Test::Test(int b) : a(b * b) {}");
11781 verifyIndependentOfContext("f(a, *a);");
11782 verifyFormat("void g() { f(*a); }");
11783 verifyIndependentOfContext("int a = b * 10;");
11784 verifyIndependentOfContext("int a = 10 * b;");
11785 verifyIndependentOfContext("int a = b * c;");
11786 verifyIndependentOfContext("int a += b * c;");
11787 verifyIndependentOfContext("int a -= b * c;");
11788 verifyIndependentOfContext("int a *= b * c;");
11789 verifyIndependentOfContext("int a /= b * c;");
11790 verifyIndependentOfContext("int a = *b;");
11791 verifyIndependentOfContext("int a = *b * c;");
11792 verifyIndependentOfContext("int a = b * *c;");
11793 verifyIndependentOfContext("int a = b * (10);");
11794 verifyIndependentOfContext("S << b * (10);");
11795 verifyIndependentOfContext("return 10 * b;");
11796 verifyIndependentOfContext("return *b * *c;");
11797 verifyIndependentOfContext("return a & ~b;");
11798 verifyIndependentOfContext("f(b ? *c : *d);");
11799 verifyIndependentOfContext("int a = b ? *c : *d;");
11800 verifyIndependentOfContext("*b = a;");
11801 verifyIndependentOfContext("a * ~b;");
11802 verifyIndependentOfContext("a * !b;");
11803 verifyIndependentOfContext("a * +b;");
11804 verifyIndependentOfContext("a * -b;");
11805 verifyIndependentOfContext("a * ++b;");
11806 verifyIndependentOfContext("a * --b;");
11807 verifyIndependentOfContext("a[4] * b;");
11808 verifyIndependentOfContext("a[a * a] = 1;");
11809 verifyIndependentOfContext("f() * b;");
11810 verifyIndependentOfContext("a * [self dostuff];");
11811 verifyIndependentOfContext("int x = a * (a + b);");
11812 verifyIndependentOfContext("(a *)(a + b);");
11813 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11814 verifyIndependentOfContext("int *pa = (int *)&a;");
11815 verifyIndependentOfContext("return sizeof(int **);");
11816 verifyIndependentOfContext("return sizeof(int ******);");
11817 verifyIndependentOfContext("return (int **&)a;");
11818 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11819 verifyFormat("void f(Type (*parameter)[10]) {}");
11820 verifyFormat("void f(Type (¶meter)[10]) {}");
11821 verifyGoogleFormat("return sizeof(int**);");
11822 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11823 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11824 verifyFormat("auto a = [](int **&, int ***) {};");
11825 verifyFormat("auto PointerBinding = [](const char *S) {};");
11826 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11827 verifyFormat("[](const decltype(*a) &value) {}");
11828 verifyFormat("[](const typeof(*a) &value) {}");
11829 verifyFormat("[](const _Atomic(a *) &value) {}");
11830 verifyFormat("[](const __underlying_type(a) &value) {}");
11831 verifyFormat("decltype(a * b) F();");
11832 verifyFormat("typeof(a * b) F();");
11833 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11834 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11835 verifyIndependentOfContext("typedef void (*f)(int *a);");
11836 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11837 verifyIndependentOfContext("int i{a * b};");
11838 verifyIndependentOfContext("aaa && aaa->f();");
11839 verifyIndependentOfContext("int x = ~*p;");
11840 verifyFormat("Constructor() : a(a), area(width * height) {}");
11841 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11842 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11843 verifyFormat("void f() { f(a, c * d); }");
11844 verifyFormat("void f() { f(new a(), c * d); }");
11845 verifyFormat("void f(const MyOverride &override);");
11846 verifyFormat("void f(const MyFinal &final);");
11847 verifyIndependentOfContext("bool a = f() && override.f();");
11848 verifyIndependentOfContext("bool a = f() && final.f();");
11850 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11852 verifyIndependentOfContext("A<int *> a;");
11853 verifyIndependentOfContext("A<int **> a;");
11854 verifyIndependentOfContext("A<int *, int *> a;");
11855 verifyIndependentOfContext("A<int *[]> a;");
11856 verifyIndependentOfContext(
11857 "const char *const p = reinterpret_cast<const char *const>(q);");
11858 verifyIndependentOfContext("A<int **, int **> a;");
11859 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11860 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11861 verifyFormat("for (; a && b;) {\n}");
11862 verifyFormat("bool foo = true && [] { return false; }();");
11865 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11868 verifyGoogleFormat("int const* a = &b;");
11869 verifyGoogleFormat("**outparam = 1;");
11870 verifyGoogleFormat("*outparam = a * b;");
11871 verifyGoogleFormat("int main(int argc, char** argv) {}");
11872 verifyGoogleFormat("A<int*> a;");
11873 verifyGoogleFormat("A<int**> a;");
11874 verifyGoogleFormat("A<int*, int*> a;");
11875 verifyGoogleFormat("A<int**, int**> a;");
11876 verifyGoogleFormat("f(b ? *c : *d);");
11877 verifyGoogleFormat("int a = b ? *c : *d;");
11878 verifyGoogleFormat("Type* t = **x;");
11879 verifyGoogleFormat("Type* t = *++*x;");
11880 verifyGoogleFormat("*++*x;");
11881 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11882 verifyGoogleFormat("Type* t = x++ * y;");
11883 verifyGoogleFormat(
11884 "const char* const p = reinterpret_cast<const char* const>(q);");
11885 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11886 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11887 verifyGoogleFormat("template <typename T>\n"
11888 "void f(int i = 0, SomeType** temps = NULL);");
11890 FormatStyle Left
= getLLVMStyle();
11891 Left
.PointerAlignment
= FormatStyle::PAS_Left
;
11892 verifyFormat("x = *a(x) = *a(y);", Left
);
11893 verifyFormat("for (;; *a = b) {\n}", Left
);
11894 verifyFormat("return *this += 1;", Left
);
11895 verifyFormat("throw *x;", Left
);
11896 verifyFormat("delete *x;", Left
);
11897 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left
);
11898 verifyFormat("[](const decltype(*a)* ptr) {}", Left
);
11899 verifyFormat("[](const typeof(*a)* ptr) {}", Left
);
11900 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left
);
11901 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left
);
11902 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left
);
11903 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left
);
11904 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left
);
11905 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left
);
11907 verifyIndependentOfContext("a = *(x + y);");
11908 verifyIndependentOfContext("a = &(x + y);");
11909 verifyIndependentOfContext("*(x + y).call();");
11910 verifyIndependentOfContext("&(x + y)->call();");
11911 verifyFormat("void f() { &(*I).first; }");
11913 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11914 verifyFormat("f(* /* confusing comment */ foo);");
11915 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11916 verifyFormat("void foo(int * // this is the first paramters\n"
11919 verifyFormat("double term = a * // first\n"
11922 "int *MyValues = {\n"
11923 " *A, // Operator detection might be confused by the '{'\n"
11924 " *BB // Operator detection might be confused by previous comment\n"
11927 verifyIndependentOfContext("if (int *a = &b)");
11928 verifyIndependentOfContext("if (int &a = *b)");
11929 verifyIndependentOfContext("if (a & b[i])");
11930 verifyIndependentOfContext("if constexpr (a & b[i])");
11931 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11932 verifyIndependentOfContext("if (a * (b * c))");
11933 verifyIndependentOfContext("if constexpr (a * (b * c))");
11934 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11935 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11936 verifyIndependentOfContext("if (*b[i])");
11937 verifyIndependentOfContext("if (int *a = (&b))");
11938 verifyIndependentOfContext("while (int *a = &b)");
11939 verifyIndependentOfContext("while (a * (b * c))");
11940 verifyIndependentOfContext("size = sizeof *a;");
11941 verifyIndependentOfContext("if (a && (b = c))");
11942 verifyFormat("void f() {\n"
11943 " for (const int &v : Values) {\n"
11946 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11947 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11948 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11950 verifyFormat("#define A (!a * b)");
11951 verifyFormat("#define MACRO \\\n"
11952 " int *i = a * b; \\\n"
11954 getLLVMStyleWithColumns(19));
11956 verifyIndependentOfContext("A = new SomeType *[Length];");
11957 verifyIndependentOfContext("A = new SomeType *[Length]();");
11958 verifyIndependentOfContext("T **t = new T *;");
11959 verifyIndependentOfContext("T **t = new T *();");
11960 verifyGoogleFormat("A = new SomeType*[Length]();");
11961 verifyGoogleFormat("A = new SomeType*[Length];");
11962 verifyGoogleFormat("T** t = new T*;");
11963 verifyGoogleFormat("T** t = new T*();");
11965 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11966 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11967 verifyFormat("template <bool a, bool b> "
11968 "typename t::if<x && y>::type f() {}");
11969 verifyFormat("template <int *y> f() {}");
11970 verifyFormat("vector<int *> v;");
11971 verifyFormat("vector<int *const> v;");
11972 verifyFormat("vector<int *const **const *> v;");
11973 verifyFormat("vector<int *volatile> v;");
11974 verifyFormat("vector<a *_Nonnull> v;");
11975 verifyFormat("vector<a *_Nullable> v;");
11976 verifyFormat("vector<a *_Null_unspecified> v;");
11977 verifyFormat("vector<a *__ptr32> v;");
11978 verifyFormat("vector<a *__ptr64> v;");
11979 verifyFormat("vector<a *__capability> v;");
11980 FormatStyle TypeMacros
= getLLVMStyle();
11981 TypeMacros
.TypenameMacros
= {"LIST"};
11982 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros
);
11983 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros
);
11984 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros
);
11985 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros
);
11986 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros
); // multiplication
11988 FormatStyle CustomQualifier
= getLLVMStyle();
11989 // Add identifiers that should not be parsed as a qualifier by default.
11990 CustomQualifier
.AttributeMacros
.push_back("__my_qualifier");
11991 CustomQualifier
.AttributeMacros
.push_back("_My_qualifier");
11992 CustomQualifier
.AttributeMacros
.push_back("my_other_qualifier");
11993 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11994 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier
);
11995 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11996 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier
);
11997 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11998 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier
);
11999 verifyFormat("vector<a * _NotAQualifier> v;");
12000 verifyFormat("vector<a * __not_a_qualifier> v;");
12001 verifyFormat("vector<a * b> v;");
12002 verifyFormat("foo<b && false>();");
12003 verifyFormat("foo<b & 1>();");
12004 verifyFormat("foo<b & (1)>();");
12005 verifyFormat("foo<b & (~0)>();");
12006 verifyFormat("foo<b & (true)>();");
12007 verifyFormat("foo<b & ((1))>();");
12008 verifyFormat("foo<b & (/*comment*/ 1)>();");
12009 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12010 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12011 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12012 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12014 "template <class T, class = typename std::enable_if<\n"
12015 " std::is_integral<T>::value &&\n"
12016 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12018 getLLVMStyleWithColumns(70));
12019 verifyFormat("template <class T,\n"
12020 " class = typename std::enable_if<\n"
12021 " std::is_integral<T>::value &&\n"
12022 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12025 getLLVMStyleWithColumns(70));
12027 "template <class T,\n"
12028 " class = typename ::std::enable_if<\n"
12029 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12031 getGoogleStyleWithColumns(68));
12033 FormatStyle Style
= getLLVMStyle();
12034 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
12035 verifyFormat("struct {\n"
12038 verifyFormat("union {\n"
12041 verifyFormat("class {\n"
12044 // Don't confuse a multiplication after a brace-initialized expression with
12045 // a class pointer.
12046 verifyFormat("int i = int{42} * 34;", Style
);
12047 verifyFormat("struct {\n"
12050 verifyFormat("union {\n"
12053 verifyFormat("class {\n"
12056 verifyFormat("bool b = 3 == int{3} && true;");
12058 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
12059 verifyFormat("struct {\n"
12062 verifyFormat("union {\n"
12065 verifyFormat("class {\n"
12068 verifyFormat("struct {\n"
12071 verifyFormat("union {\n"
12074 verifyFormat("class {\n"
12078 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
12079 verifyFormat("struct {\n"
12082 verifyFormat("union {\n"
12085 verifyFormat("class {\n"
12088 verifyFormat("struct {\n"
12091 verifyFormat("union {\n"
12094 verifyFormat("class {\n"
12098 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
12099 verifyFormat("delete[] *ptr;", Style
);
12100 verifyFormat("delete[] **ptr;", Style
);
12101 verifyFormat("delete[] *(ptr);", Style
);
12103 verifyIndependentOfContext("MACRO(int *i);");
12104 verifyIndependentOfContext("MACRO(auto *a);");
12105 verifyIndependentOfContext("MACRO(const A *a);");
12106 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12107 verifyIndependentOfContext("MACRO(decltype(A) *a);");
12108 verifyIndependentOfContext("MACRO(typeof(A) *a);");
12109 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12110 verifyIndependentOfContext("MACRO(A *const a);");
12111 verifyIndependentOfContext("MACRO(A *restrict a);");
12112 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12113 verifyIndependentOfContext("MACRO(A *__restrict a);");
12114 verifyIndependentOfContext("MACRO(A *volatile a);");
12115 verifyIndependentOfContext("MACRO(A *__volatile a);");
12116 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12117 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12118 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12119 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12120 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12121 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12122 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12123 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12124 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12125 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12126 verifyIndependentOfContext("MACRO(A *__capability);");
12127 verifyIndependentOfContext("MACRO(A &__capability);");
12128 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12129 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12130 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12131 // a type declaration:
12132 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier
);
12133 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier
);
12134 // Also check that TypenameMacros prevents parsing it as multiplication:
12135 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12136 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros
); // type
12138 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12139 verifyFormat("void f() { f(float{1}, a * a); }");
12140 verifyFormat("void f() { f(float(1), a * a); }");
12142 verifyFormat("f((void (*)(int))g);");
12143 verifyFormat("f((void (&)(int))g);");
12144 verifyFormat("f((void (^)(int))g);");
12146 // FIXME: Is there a way to make this work?
12147 // verifyIndependentOfContext("MACRO(A *a);");
12148 verifyFormat("MACRO(A &B);");
12149 verifyFormat("MACRO(A *B);");
12150 verifyFormat("void f() { MACRO(A * B); }");
12151 verifyFormat("void f() { MACRO(A & B); }");
12153 // This lambda was mis-formatted after D88956 (treating it as a binop):
12154 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12155 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12156 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12157 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12159 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12160 verifyFormat("return options != nullptr && operator==(*options);");
12162 verifyFormat("#define OP(x) \\\n"
12163 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12164 " return s << a.DebugString(); \\\n"
12166 "#define OP(x) \\\n"
12167 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12168 " return s << a.DebugString(); \\\n"
12170 getLLVMStyleWithColumns(50));
12172 verifyFormat("#define FOO \\\n"
12173 " void foo() { \\\n"
12174 " operator+(a * b); \\\n"
12176 getLLVMStyleWithColumns(25));
12178 // FIXME: We cannot handle this case yet; we might be able to figure out that
12179 // foo<x> d > v; doesn't make sense.
12180 verifyFormat("foo<a<b && c> d> v;");
12182 FormatStyle PointerMiddle
= getLLVMStyle();
12183 PointerMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
12184 verifyFormat("delete *x;", PointerMiddle
);
12185 verifyFormat("int * x;", PointerMiddle
);
12186 verifyFormat("int *[] x;", PointerMiddle
);
12187 verifyFormat("template <int * y> f() {}", PointerMiddle
);
12188 verifyFormat("int * f(int * a) {}", PointerMiddle
);
12189 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle
);
12190 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle
);
12191 verifyFormat("A<int *> a;", PointerMiddle
);
12192 verifyFormat("A<int **> a;", PointerMiddle
);
12193 verifyFormat("A<int *, int *> a;", PointerMiddle
);
12194 verifyFormat("A<int *[]> a;", PointerMiddle
);
12195 verifyFormat("A = new SomeType *[Length]();", PointerMiddle
);
12196 verifyFormat("A = new SomeType *[Length];", PointerMiddle
);
12197 verifyFormat("T ** t = new T *;", PointerMiddle
);
12199 // Member function reference qualifiers aren't binary operators.
12200 verifyFormat("string // break\n"
12201 "operator()() & {}");
12202 verifyFormat("string // break\n"
12203 "operator()() && {}");
12204 verifyGoogleFormat("template <typename T>\n"
12205 "auto x() & -> int {}");
12207 // Should be binary operators when used as an argument expression (overloaded
12208 // operator invoked as a member function).
12209 verifyFormat("void f() { a.operator()(a * a); }");
12210 verifyFormat("void f() { a->operator()(a & a); }");
12211 verifyFormat("void f() { a.operator()(*a & *a); }");
12212 verifyFormat("void f() { a->operator()(*a * *a); }");
12214 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12215 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12217 verifyFormat("val1 & val2;");
12218 verifyFormat("val1 & val2 & val3;");
12219 verifyFormat("class c {\n"
12220 " void func(type &a) { a & member; }\n"
12221 " anotherType &member;\n"
12225 TEST_F(FormatTest
, UnderstandsAttributes
) {
12226 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12227 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12228 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12229 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12230 FormatStyle AfterType
= getLLVMStyle();
12231 AfterType
.BreakAfterReturnType
= FormatStyle::RTBS_All
;
12232 verifyFormat("__attribute__((nodebug)) void\n"
12235 verifyFormat("__unused void\n"
12239 FormatStyle CustomAttrs
= getLLVMStyle();
12240 CustomAttrs
.AttributeMacros
.push_back("__unused");
12241 CustomAttrs
.AttributeMacros
.push_back("__attr1");
12242 CustomAttrs
.AttributeMacros
.push_back("__attr2");
12243 CustomAttrs
.AttributeMacros
.push_back("no_underscore_attr");
12244 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12245 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12246 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12247 // Check that it is parsed as a multiplication without AttributeMacros and
12248 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12249 verifyFormat("vector<SomeType * __attr1> v;");
12250 verifyFormat("vector<SomeType __attr1 *> v;");
12251 verifyFormat("vector<SomeType __attr1 *const> v;");
12252 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12253 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs
);
12254 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs
);
12255 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs
);
12256 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs
);
12257 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs
);
12258 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs
);
12259 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs
);
12260 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs
);
12261 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs
);
12262 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs
);
12264 // Check that these are not parsed as function declarations:
12265 CustomAttrs
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
12266 CustomAttrs
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
12267 verifyFormat("SomeType s(InitValue);", CustomAttrs
);
12268 verifyFormat("SomeType s{InitValue};", CustomAttrs
);
12269 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs
);
12270 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs
);
12271 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs
);
12272 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs
);
12273 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs
);
12274 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs
);
12277 TEST_F(FormatTest
, UnderstandsPointerQualifiersInCast
) {
12278 // Check that qualifiers on pointers don't break parsing of casts.
12279 verifyFormat("x = (foo *const)*v;");
12280 verifyFormat("x = (foo *volatile)*v;");
12281 verifyFormat("x = (foo *restrict)*v;");
12282 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12283 verifyFormat("x = (foo *_Nonnull)*v;");
12284 verifyFormat("x = (foo *_Nullable)*v;");
12285 verifyFormat("x = (foo *_Null_unspecified)*v;");
12286 verifyFormat("x = (foo *_Nonnull)*v;");
12287 verifyFormat("x = (foo *[[clang::attr]])*v;");
12288 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12289 verifyFormat("x = (foo *__ptr32)*v;");
12290 verifyFormat("x = (foo *__ptr64)*v;");
12291 verifyFormat("x = (foo *__capability)*v;");
12293 // Check that we handle multiple trailing qualifiers and skip them all to
12294 // determine that the expression is a cast to a pointer type.
12295 FormatStyle LongPointerRight
= getLLVMStyleWithColumns(999);
12296 FormatStyle LongPointerLeft
= getLLVMStyleWithColumns(999);
12297 LongPointerLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
12298 StringRef AllQualifiers
=
12299 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12300 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12301 verifyFormat(("x = (foo *" + AllQualifiers
+ ")*v;").str(), LongPointerRight
);
12302 verifyFormat(("x = (foo* " + AllQualifiers
+ ")*v;").str(), LongPointerLeft
);
12304 // Also check that address-of is not parsed as a binary bitwise-and:
12305 verifyFormat("x = (foo *const)&v;");
12306 verifyFormat(("x = (foo *" + AllQualifiers
+ ")&v;").str(), LongPointerRight
);
12307 verifyFormat(("x = (foo* " + AllQualifiers
+ ")&v;").str(), LongPointerLeft
);
12309 // Check custom qualifiers:
12310 FormatStyle CustomQualifier
= getLLVMStyleWithColumns(999);
12311 CustomQualifier
.AttributeMacros
.push_back("__my_qualifier");
12312 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12313 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier
);
12314 verifyFormat(("x = (foo *" + AllQualifiers
+ " __my_qualifier)*v;").str(),
12316 verifyFormat(("x = (foo *" + AllQualifiers
+ " __my_qualifier)&v;").str(),
12319 // Check that unknown identifiers result in binary operator parsing:
12320 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12321 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12324 TEST_F(FormatTest
, UnderstandsSquareAttributes
) {
12325 verifyFormat("SomeType s [[unused]] (InitValue);");
12326 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12327 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12328 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12329 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12330 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12331 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12332 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12333 verifyFormat("[[nodiscard]] bool f() { return false; }");
12334 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12335 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12336 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12337 verifyFormat("[[nodiscard]] ::qualified_type f();");
12339 // Make sure we do not mistake attributes for array subscripts.
12340 verifyFormat("int a() {}\n"
12341 "[[unused]] int b() {}");
12342 verifyFormat("NSArray *arr;\n"
12343 "arr[[Foo() bar]];");
12345 // On the other hand, we still need to correctly find array subscripts.
12346 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12348 // Make sure that we do not mistake Objective-C method inside array literals
12349 // as attributes, even if those method names are also keywords.
12350 verifyFormat("@[ [foo bar] ];");
12351 verifyFormat("@[ [NSArray class] ];");
12352 verifyFormat("@[ [foo enum] ];");
12354 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12356 // Make sure we do not parse attributes as lambda introducers.
12357 FormatStyle MultiLineFunctions
= getLLVMStyle();
12358 MultiLineFunctions
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
12359 verifyFormat("[[unused]] int b() {\n"
12362 MultiLineFunctions
);
12365 TEST_F(FormatTest
, AttributeClass
) {
12366 FormatStyle Style
= getChromiumStyle(FormatStyle::LK_Cpp
);
12367 verifyFormat("class S {\n"
12368 " S(S&&) = default;\n"
12371 verifyFormat("class [[nodiscard]] S {\n"
12372 " S(S&&) = default;\n"
12375 verifyFormat("class __attribute((maybeunused)) S {\n"
12376 " S(S&&) = default;\n"
12379 verifyFormat("struct S {\n"
12380 " S(S&&) = default;\n"
12383 verifyFormat("struct [[nodiscard]] S {\n"
12384 " S(S&&) = default;\n"
12389 TEST_F(FormatTest
, AttributesAfterMacro
) {
12390 FormatStyle Style
= getLLVMStyle();
12391 verifyFormat("MACRO;\n"
12392 "__attribute__((maybe_unused)) int foo() {\n"
12396 verifyFormat("MACRO;\n"
12397 "[[nodiscard]] int foo() {\n"
12401 verifyNoChange("MACRO\n\n"
12402 "__attribute__((maybe_unused)) int foo() {\n"
12406 verifyNoChange("MACRO\n\n"
12407 "[[nodiscard]] int foo() {\n"
12412 TEST_F(FormatTest
, AttributePenaltyBreaking
) {
12413 FormatStyle Style
= getLLVMStyle();
12414 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12415 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12417 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12418 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12420 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12421 "shared_ptr<ALongTypeName> &C d) {\n}",
12425 TEST_F(FormatTest
, UnderstandsEllipsis
) {
12426 FormatStyle Style
= getLLVMStyle();
12427 verifyFormat("int printf(const char *fmt, ...);");
12428 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12429 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12431 verifyFormat("template <int *...PP> a;", Style
);
12433 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
12434 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style
);
12436 verifyFormat("template <int*... PP> a;", Style
);
12438 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
12439 verifyFormat("template <int *... PP> a;", Style
);
12442 TEST_F(FormatTest
, AdaptivelyFormatsPointersAndReferences
) {
12443 verifyFormat("int *a;\n"
12450 verifyFormat("int* a;\n"
12457 verifyFormat("int *a;\n"
12464 verifyFormat("auto x = [] {\n"
12469 "auto x=[]{int *a;\n"
12475 TEST_F(FormatTest
, UnderstandsRvalueReferences
) {
12476 verifyFormat("int f(int &&a) {}");
12477 verifyFormat("int f(int a, char &&b) {}");
12478 verifyFormat("void f() { int &&a = b; }");
12479 verifyGoogleFormat("int f(int a, char&& b) {}");
12480 verifyGoogleFormat("void f() { int&& a = b; }");
12482 verifyIndependentOfContext("A<int &&> a;");
12483 verifyIndependentOfContext("A<int &&, int &&> a;");
12484 verifyGoogleFormat("A<int&&> a;");
12485 verifyGoogleFormat("A<int&&, int&&> a;");
12487 // Not rvalue references:
12488 verifyFormat("template <bool B, bool C> class A {\n"
12489 " static_assert(B && C, \"Something is wrong\");\n"
12491 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12492 verifyFormat("template <typename T> struct S {\n"
12493 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12495 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12496 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12497 verifyFormat("#define A(a, b) (a && b)");
12500 TEST_F(FormatTest
, FormatsBinaryOperatorsPrecedingEquals
) {
12501 verifyFormat("void f() {\n"
12505 getLLVMStyleWithColumns(15));
12508 TEST_F(FormatTest
, FormatsCasts
) {
12509 verifyFormat("Type *A = static_cast<Type *>(P);");
12510 verifyFormat("static_cast<Type *>(P);");
12511 verifyFormat("static_cast<Type &>(Fun)(Args);");
12512 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12513 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12514 // Check that static_cast<...>(...) does not require the next token to be on
12516 verifyFormat("some_loooong_output << something_something__ << "
12517 "static_cast<const void *>(R)\n"
12519 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12520 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12521 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12522 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12523 verifyFormat("Type *A = (Type *)P;");
12524 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12525 verifyFormat("int a = (int)(2.0f);");
12526 verifyFormat("int a = (int)2.0f;");
12527 verifyFormat("x[(int32)y];");
12528 verifyFormat("x = (int32)y;");
12529 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12530 verifyFormat("int a = (int)*b;");
12531 verifyFormat("int a = (int)2.0f;");
12532 verifyFormat("int a = (int)~0;");
12533 verifyFormat("int a = (int)++a;");
12534 verifyFormat("int a = (int)sizeof(int);");
12535 verifyFormat("int a = (int)+2;");
12536 verifyFormat("my_int a = (my_int)2.0f;");
12537 verifyFormat("my_int a = (my_int)sizeof(int);");
12538 verifyFormat("return (my_int)aaa;");
12539 verifyFormat("throw (my_int)aaa;");
12540 verifyFormat("#define x ((int)-1)");
12541 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12542 verifyFormat("#define p(q) ((int *)&q)");
12543 verifyFormat("fn(a)(b) + 1;");
12545 verifyFormat("void f() { my_int a = (my_int)*b; }");
12546 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12547 verifyFormat("my_int a = (my_int)~0;");
12548 verifyFormat("my_int a = (my_int)++a;");
12549 verifyFormat("my_int a = (my_int)-2;");
12550 verifyFormat("my_int a = (my_int)1;");
12551 verifyFormat("my_int a = (my_int *)1;");
12552 verifyFormat("my_int a = (const my_int)-1;");
12553 verifyFormat("my_int a = (const my_int *)-1;");
12554 verifyFormat("my_int a = (my_int)(my_int)-1;");
12555 verifyFormat("my_int a = (ns::my_int)-2;");
12556 verifyFormat("case (my_int)ONE:");
12557 verifyFormat("auto x = (X)this;");
12558 // Casts in Obj-C style calls used to not be recognized as such.
12559 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12561 // FIXME: single value wrapped with paren will be treated as cast.
12562 verifyFormat("void f(int i = (kValue)*kMask) {}");
12568 // Don't break after a cast's
12569 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12570 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12571 " bbbbbbbbbbbbbbbbbbbbbb);");
12573 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12574 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12575 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12576 verifyFormat("bool *y = (bool *)(void *)(x);");
12577 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12578 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12579 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12580 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12582 // These are not casts.
12583 verifyFormat("void f(int *) {}");
12584 verifyFormat("f(foo)->b;");
12585 verifyFormat("f(foo).b;");
12586 verifyFormat("f(foo)(b);");
12587 verifyFormat("f(foo)[b];");
12588 verifyFormat("[](foo) { return 4; }(bar);");
12589 verifyFormat("(*funptr)(foo)[4];");
12590 verifyFormat("funptrs[4](foo)[4];");
12591 verifyFormat("void f(int *);");
12592 verifyFormat("void f(int *) = 0;");
12593 verifyFormat("void f(SmallVector<int>) {}");
12594 verifyFormat("void f(SmallVector<int>);");
12595 verifyFormat("void f(SmallVector<int>) = 0;");
12596 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12597 verifyFormat("int a = sizeof(int) * b;");
12598 verifyGoogleFormat("int a = alignof(int) * b;");
12599 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12600 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12601 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12603 // These are not casts, but at some point were confused with casts.
12604 verifyFormat("virtual void foo(int *) override;");
12605 verifyFormat("virtual void foo(char &) const;");
12606 verifyFormat("virtual void foo(int *a, char *) const;");
12607 verifyFormat("int a = sizeof(int *) + b;");
12608 verifyGoogleFormat("int a = alignof(int *) + b;");
12609 verifyFormat("bool b = f(g<int>) && c;");
12610 verifyFormat("typedef void (*f)(int i) func;");
12611 verifyFormat("void operator++(int) noexcept;");
12612 verifyFormat("void operator++(int &) noexcept;");
12613 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12616 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12617 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12618 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12619 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12620 verifyFormat("void operator delete(foo &) noexcept;");
12621 verifyFormat("void operator delete(foo) noexcept;");
12622 verifyFormat("void operator delete(int) noexcept;");
12623 verifyFormat("void operator delete(int &) noexcept;");
12624 verifyFormat("void operator delete(int &) volatile noexcept;");
12625 verifyFormat("void operator delete(int &) const");
12626 verifyFormat("void operator delete(int &) = default");
12627 verifyFormat("void operator delete(int &) = delete");
12628 verifyFormat("void operator delete(int &) [[noreturn]]");
12629 verifyFormat("void operator delete(int &) throw();");
12630 verifyFormat("void operator delete(int &) throw(int);");
12631 verifyFormat("auto operator delete(int &) -> int;");
12632 verifyFormat("auto operator delete(int &) override");
12633 verifyFormat("auto operator delete(int &) final");
12635 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12636 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12637 // FIXME: The indentation here is not ideal.
12639 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12640 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12641 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12644 TEST_F(FormatTest
, FormatsFunctionTypes
) {
12645 verifyFormat("A<bool()> a;");
12646 verifyFormat("A<SomeType()> a;");
12647 verifyFormat("A<void (*)(int, std::string)> a;");
12648 verifyFormat("A<void *(int)>;");
12649 verifyFormat("void *(*a)(int *, SomeType *);");
12650 verifyFormat("int (*func)(void *);");
12651 verifyFormat("void f() { int (*func)(void *); }");
12652 verifyFormat("template <class CallbackClass>\n"
12653 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12655 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12656 verifyGoogleFormat("void* (*a)(int);");
12657 verifyGoogleFormat(
12658 "template <class CallbackClass>\n"
12659 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12661 // Other constructs can look somewhat like function types:
12662 verifyFormat("A<sizeof(*x)> a;");
12663 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12664 verifyFormat("some_var = function(*some_pointer_var)[0];");
12665 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12666 verifyFormat("int x = f(&h)();");
12667 verifyFormat("returnsFunction(¶m1, ¶m2)(param);");
12668 verifyFormat("std::function<\n"
12669 " LooooooooooongTemplatedType<\n"
12671 " LooooooooooooooooongType type)>\n"
12673 getGoogleStyleWithColumns(40));
12676 TEST_F(FormatTest
, FormatsPointersToArrayTypes
) {
12677 verifyFormat("A (*foo_)[6];");
12678 verifyFormat("vector<int> (*foo_)[6];");
12681 TEST_F(FormatTest
, BreaksLongVariableDeclarations
) {
12682 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12683 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12684 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12685 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12686 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12687 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12689 // Different ways of ()-initializiation.
12690 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12691 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12692 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12693 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12694 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12695 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12696 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12697 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12699 // Lambdas should not confuse the variable declaration heuristic.
12700 verifyFormat("LooooooooooooooooongType\n"
12701 " variable(nullptr, [](A *a) {});",
12702 getLLVMStyleWithColumns(40));
12705 TEST_F(FormatTest
, BreaksLongDeclarations
) {
12706 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12707 " AnotherNameForTheLongType;");
12708 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12710 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12711 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12712 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12713 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12714 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12715 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12716 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12717 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12718 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12719 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12720 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12721 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12722 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12723 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12724 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12725 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12726 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12727 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12728 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12729 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12730 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12731 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12732 FormatStyle Indented
= getLLVMStyle();
12733 Indented
.IndentWrappedFunctionNames
= true;
12734 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12735 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12738 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12739 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12742 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12743 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12746 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12747 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12750 // FIXME: Without the comment, this breaks after "(".
12751 verifyGoogleFormat(
12752 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12753 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12755 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12756 " int LoooooooooooooooooooongParam2) {}");
12758 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12759 " SourceLocation L, IdentifierIn *II,\n"
12761 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12762 "ReallyReaaallyLongFunctionName(\n"
12763 " const std::string &SomeParameter,\n"
12764 " const SomeType<string, SomeOtherTemplateParameter>\n"
12765 " &ReallyReallyLongParameterName,\n"
12766 " const SomeType<string, SomeOtherTemplateParameter>\n"
12767 " &AnotherLongParameterName) {}");
12768 verifyFormat("template <typename A>\n"
12769 "SomeLoooooooooooooooooooooongType<\n"
12770 " typename some_namespace::SomeOtherType<A>::Type>\n"
12773 verifyGoogleFormat(
12774 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12775 " aaaaaaaaaaaaaaaaaaaaaaa;");
12776 verifyGoogleFormat(
12777 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12778 " SourceLocation L) {}");
12779 verifyGoogleFormat(
12780 "some_namespace::LongReturnType\n"
12781 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12782 " int first_long_parameter, int second_parameter) {}");
12784 verifyGoogleFormat("template <typename T>\n"
12785 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12786 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12787 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12788 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12790 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12791 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12792 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12793 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12794 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12795 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12796 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12797 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12801 verifyFormat("template <typename T> // Templates on own line.\n"
12802 "static int // Some comment.\n"
12803 "MyFunction(int a);");
12806 TEST_F(FormatTest
, FormatsAccessModifiers
) {
12807 FormatStyle Style
= getLLVMStyle();
12808 EXPECT_EQ(Style
.EmptyLineBeforeAccessModifier
,
12809 FormatStyle::ELBAMS_LogicalBlock
);
12810 verifyFormat("struct foo {\n"
12821 verifyFormat("struct foo {\n"
12840 verifyFormat("struct foo { /* comment */\n"
12848 verifyFormat("struct foo {\n"
12859 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
12860 verifyFormat("struct foo {\n"
12869 verifyFormat("struct foo {\n"
12889 verifyFormat("struct foo { /* comment */\n"
12896 "struct foo { /* comment */\n"
12906 verifyFormat("struct foo {\n"
12929 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
12930 verifyFormat("struct foo {\n"
12941 verifyFormat("struct foo {\n"
12960 verifyFormat("struct foo { /* comment */\n"
12969 verifyFormat("struct foo {\n"
12992 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
12993 verifyNoChange("struct foo {\n"
13005 verifyFormat("struct foo {\n"
13014 verifyNoChange("struct foo { /* comment */\n"
13024 verifyFormat("struct foo { /* comment */\n"
13032 verifyNoChange("struct foo {\n"
13045 verifyFormat("struct foo {\n"
13056 Style
.AttributeMacros
.push_back("FOO");
13057 Style
.AttributeMacros
.push_back("BAR");
13058 verifyFormat("struct foo {\n"
13061 "BAR(x) protected:\n"
13066 FormatStyle NoEmptyLines
= getLLVMStyle();
13067 NoEmptyLines
.MaxEmptyLinesToKeep
= 0;
13068 verifyFormat("struct foo {\n"
13081 NoEmptyLines
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13082 verifyFormat("struct foo {\n"
13093 NoEmptyLines
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13094 verifyFormat("struct foo {\n"
13109 TEST_F(FormatTest
, FormatsAfterAccessModifiers
) {
13111 FormatStyle Style
= getLLVMStyle();
13112 EXPECT_EQ(Style
.EmptyLineAfterAccessModifier
, FormatStyle::ELAAMS_Never
);
13113 verifyFormat("struct foo {\n"
13125 // Check if lines are removed.
13126 verifyFormat("struct foo {\n"
13151 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13152 verifyFormat("struct foo {\n"
13167 // Check if lines are added.
13168 verifyFormat("struct foo {\n"
13193 // Leave tests rely on the code layout, test::messUp can not be used.
13194 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13195 Style
.MaxEmptyLinesToKeep
= 0u;
13196 verifyFormat("struct foo {\n"
13208 // Check if MaxEmptyLinesToKeep is respected.
13209 verifyFormat("struct foo {\n"
13234 Style
.MaxEmptyLinesToKeep
= 1u;
13235 verifyNoChange("struct foo {\n"
13249 // Check if no lines are kept.
13250 verifyFormat("struct foo {\n"
13261 // Check if MaxEmptyLinesToKeep is respected.
13262 verifyFormat("struct foo {\n"
13290 Style
.MaxEmptyLinesToKeep
= 10u;
13291 verifyNoChange("struct foo {\n"
13306 // Test with comments.
13307 Style
= getLLVMStyle();
13308 verifyFormat("struct foo {\n"
13313 "private: /* comment */\n"
13317 verifyFormat("struct foo {\n"
13322 "private: /* comment */\n"
13331 "private: /* comment */\n"
13337 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13338 verifyFormat("struct foo {\n"
13344 "private: /* comment */\n"
13353 "private: /* comment */\n"
13357 verifyFormat("struct foo {\n"
13363 "private: /* comment */\n"
13369 // Test with preprocessor defines.
13370 Style
= getLLVMStyle();
13371 verifyFormat("struct foo {\n"
13378 verifyFormat("struct foo {\n"
13392 verifyNoChange("struct foo {\n"
13400 verifyFormat("struct foo {\n"
13416 verifyFormat("struct foo {\n"
13431 verifyFormat("struct foo {\n"
13451 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13452 verifyFormat("struct foo {\n"
13466 verifyFormat("struct foo {\n"
13476 TEST_F(FormatTest
, FormatsAfterAndBeforeAccessModifiersInteraction
) {
13477 // Combined tests of EmptyLineAfterAccessModifier and
13478 // EmptyLineBeforeAccessModifier.
13479 FormatStyle Style
= getLLVMStyle();
13480 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13481 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13482 verifyFormat("struct foo {\n"
13489 Style
.MaxEmptyLinesToKeep
= 10u;
13490 // Both remove all new lines.
13491 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13492 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13493 verifyFormat("struct foo {\n"
13504 // Leave tests rely on the code layout, test::messUp can not be used.
13505 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13506 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13507 Style
.MaxEmptyLinesToKeep
= 10u;
13508 verifyNoChange("struct foo {\n"
13514 Style
.MaxEmptyLinesToKeep
= 3u;
13515 verifyNoChange("struct foo {\n"
13521 Style
.MaxEmptyLinesToKeep
= 1u;
13522 verifyNoChange("struct foo {\n"
13527 Style
); // Based on new lines in original document and not
13530 Style
.MaxEmptyLinesToKeep
= 10u;
13531 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13532 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13533 // Newlines are kept if they are greater than zero,
13534 // test::messUp removes all new lines which changes the logic
13535 verifyNoChange("struct foo {\n"
13542 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13543 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13544 // test::messUp removes all new lines which changes the logic
13545 verifyNoChange("struct foo {\n"
13552 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13553 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13554 verifyNoChange("struct foo {\n"
13559 Style
); // test::messUp removes all new lines which changes
13562 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13563 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13564 verifyFormat("struct foo {\n"
13575 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13576 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13577 verifyNoChange("struct foo {\n"
13582 Style
); // test::messUp removes all new lines which changes
13585 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13586 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13587 verifyFormat("struct foo {\n"
13598 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13599 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13600 verifyFormat("struct foo {\n"
13611 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13612 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13613 verifyFormat("struct foo {\n"
13624 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13625 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13626 verifyFormat("struct foo {\n"
13638 TEST_F(FormatTest
, FormatsArrays
) {
13639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13640 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13641 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13642 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13643 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13644 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13645 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13646 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13647 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13648 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13649 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13650 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13651 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13653 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13654 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13655 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13656 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13657 " .aaaaaaaaaaaaaaaaaaaaaa();");
13659 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13662 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13664 " .aaaaaaaaaaaaaaaaaaaaaa();");
13665 verifyFormat("a[::b::c];");
13667 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13669 FormatStyle NoColumnLimit
= getLLVMStyleWithColumns(0);
13670 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit
);
13673 TEST_F(FormatTest
, LineStartsWithSpecialCharacter
) {
13674 verifyFormat("(a)->b();");
13675 verifyFormat("--a;");
13678 TEST_F(FormatTest
, HandlesIncludeDirectives
) {
13679 verifyFormat("#include <string>\n"
13680 "#include <a/b/c.h>\n"
13681 "#include \"a/b/string\"\n"
13682 "#include \"string.h\"\n"
13683 "#include \"string.h\"\n"
13685 "#include < path with space >\n"
13686 "#include_next <test.h>"
13687 "#include \"abc.h\" // this is included for ABC\n"
13688 "#include \"some long include\" // with a comment\n"
13689 "#include \"some very long include path\"\n"
13690 "#include <some/very/long/include/path>",
13691 getLLVMStyleWithColumns(35));
13692 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13693 verifyFormat("#include <a>", "#include<a>");
13695 verifyFormat("#import <string>");
13696 verifyFormat("#import <a/b/c.h>");
13697 verifyFormat("#import \"a/b/string\"");
13698 verifyFormat("#import \"string.h\"");
13699 verifyFormat("#import \"string.h\"");
13700 verifyFormat("#if __has_include(<strstream>)\n"
13701 "#include <strstream>\n"
13704 verifyFormat("#define MY_IMPORT <a/b>");
13706 verifyFormat("#if __has_include(<a/b>)");
13707 verifyFormat("#if __has_include_next(<a/b>)");
13708 verifyFormat("#define F __has_include(<a/b>)");
13709 verifyFormat("#define F __has_include_next(<a/b>)");
13711 // Protocol buffer definition or missing "#".
13712 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13713 getLLVMStyleWithColumns(30));
13715 FormatStyle Style
= getLLVMStyle();
13716 Style
.AlwaysBreakBeforeMultilineStrings
= true;
13717 Style
.ColumnLimit
= 0;
13718 verifyFormat("#import \"abc.h\"", Style
);
13720 // But 'import' might also be a regular C++ namespace.
13721 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13723 verifyFormat("import::Bar foo(val ? 2 : 1);");
13726 //===----------------------------------------------------------------------===//
13727 // Error recovery tests.
13728 //===----------------------------------------------------------------------===//
13730 TEST_F(FormatTest
, IncompleteParameterLists
) {
13731 FormatStyle NoBinPacking
= getLLVMStyle();
13732 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
13733 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13734 " double *min_x,\n"
13735 " double *max_x,\n"
13736 " double *min_y,\n"
13737 " double *max_y,\n"
13738 " double *min_z,\n"
13739 " double *max_z, ) {}",
13743 TEST_F(FormatTest
, IncorrectCodeTrailingStuff
) {
13744 verifyFormat("void f() { return; }\n42");
13745 verifyFormat("void f() {\n"
13750 verifyFormat("void f() { return }\n42");
13751 verifyFormat("void f() {\n"
13758 TEST_F(FormatTest
, IncorrectCodeMissingSemicolon
) {
13759 verifyFormat("void f() { return }", "void f ( ) { return }");
13760 verifyFormat("void f() {\n"
13764 "void f ( ) { if ( a ) return }");
13765 verifyFormat("namespace N {\n"
13768 "namespace N { void f() }");
13769 verifyFormat("namespace N {\n"
13772 "} // namespace N",
13773 "namespace N { void f( ) { } void g( ) }");
13776 TEST_F(FormatTest
, IndentationWithinColumnLimitNotPossible
) {
13777 verifyFormat("int aaaaaaaa =\n"
13778 " // Overlylongcomment\n"
13780 getLLVMStyleWithColumns(20));
13781 verifyFormat("function(\n"
13782 " ShortArgument,\n"
13783 " LoooooooooooongArgument);",
13784 getLLVMStyleWithColumns(20));
13787 TEST_F(FormatTest
, IncorrectAccessSpecifier
) {
13788 verifyFormat("public:");
13789 verifyFormat("class A {\n"
13793 verifyFormat("public\n"
13795 verifyFormat("public\n"
13797 verifyFormat("public\n"
13800 verifyFormat("public\n"
13804 TEST_F(FormatTest
, IncorrectCodeUnbalancedBraces
) {
13806 verifyFormat("#})");
13807 verifyNoCrash("(/**/[:!] ?[).");
13808 verifyNoCrash("struct X {\n"
13809 " operator iunt(\n"
13811 verifyNoCrash("struct Foo {\n"
13812 " operator foo(bar\n"
13816 TEST_F(FormatTest
, IncorrectUnbalancedBracesInMacrosWithUnicode
) {
13817 // Found by oss-fuzz:
13818 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13819 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Cpp
);
13820 Style
.ColumnLimit
= 60;
13822 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13823 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13824 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13828 TEST_F(FormatTest
, IncorrectCodeDoNoWhile
) {
13829 verifyFormat("do {\n}");
13830 verifyFormat("do {\n}\n"
13832 verifyFormat("do {\n}\n"
13834 verifyFormat("do {\n"
13839 TEST_F(FormatTest
, IncorrectCodeMissingParens
) {
13840 verifyFormat("if {\n foo;\n foo();\n}");
13841 verifyFormat("switch {\n foo;\n foo();\n}");
13842 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13843 verifyIncompleteFormat("ERROR: for target;");
13844 verifyFormat("while {\n foo;\n foo();\n}");
13845 verifyFormat("do {\n foo;\n foo();\n} while;");
13848 TEST_F(FormatTest
, DoesNotTouchUnwrappedLinesWithErrors
) {
13849 verifyIncompleteFormat("namespace {\n"
13850 "class Foo { Foo (\n"
13855 TEST_F(FormatTest
, IncorrectCodeErrorDetection
) {
13891 getLLVMStyleWithColumns(10));
13894 TEST_F(FormatTest
, LayoutCallsInsideBraceInitializers
) {
13895 verifyFormat("int x = {\n"
13897 " b(alongervariable)};",
13898 getLLVMStyleWithColumns(25));
13901 TEST_F(FormatTest
, LayoutBraceInitializersInReturnStatement
) {
13902 verifyFormat("return (a)(b){1, 2, 3};");
13905 TEST_F(FormatTest
, LayoutCxx11BraceInitializers
) {
13906 verifyFormat("vector<int> x{1, 2, 3, 4};");
13907 verifyFormat("vector<int> x{\n"
13913 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13914 verifyFormat("f({1, 2});");
13915 verifyFormat("auto v = Foo{-1};");
13916 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13917 verifyFormat("Class::Class : member{1, 2, 3} {}");
13918 verifyFormat("new vector<int>{1, 2, 3};");
13919 verifyFormat("new int[3]{1, 2, 3};");
13920 verifyFormat("new int{1};");
13921 verifyFormat("return {arg1, arg2};");
13922 verifyFormat("return {arg1, SomeType{parameter}};");
13923 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13924 verifyFormat("new T{arg1, arg2};");
13925 verifyFormat("f(MyMap[{composite, key}]);");
13926 verifyFormat("class Class {\n"
13927 " T member = {arg1, arg2};\n"
13929 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13930 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13931 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13932 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13933 verifyFormat("int a = std::is_integral<int>{} + 0;");
13935 verifyFormat("int foo(int i) { return fo1{}(i); }");
13936 verifyFormat("int foo(int i) { return fo1{}(i); }");
13937 verifyFormat("auto i = decltype(x){};");
13938 verifyFormat("auto i = typeof(x){};");
13939 verifyFormat("auto i = _Atomic(x){};");
13940 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13941 verifyFormat("Node n{1, Node{1000}, //\n"
13943 verifyFormat("Aaaa aaaaaaa{\n"
13948 verifyFormat("class C : public D {\n"
13949 " SomeClass SC{2};\n"
13951 verifyFormat("class C : public A {\n"
13952 " class D : public B {\n"
13953 " void f() { int i{2}; }\n"
13956 verifyFormat("#define A {a, a},");
13957 // Don't confuse braced list initializers with compound statements.
13961 " A() : Base<int>{} {}\n"
13962 " A() : Base<Foo<int>>{} {}\n"
13963 " A(int b) : b(b) {}\n"
13964 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13966 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13967 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13971 // Avoid breaking between equal sign and opening brace
13972 FormatStyle AvoidBreakingFirstArgument
= getLLVMStyle();
13973 AvoidBreakingFirstArgument
.PenaltyBreakBeforeFirstCallParameter
= 200;
13974 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13975 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13976 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13977 " {\"ccccccccccccccccccccc\", 2}};",
13978 AvoidBreakingFirstArgument
);
13980 // Binpacking only if there is no trailing comma
13981 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13982 " cccccccccc, dddddddddd};",
13983 getLLVMStyleWithColumns(50));
13984 verifyFormat("const Aaaaaa aaaaa = {\n"
13990 getLLVMStyleWithColumns(50));
13992 // Cases where distinguising braced lists and blocks is hard.
13993 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13994 verifyFormat("void f() {\n"
13995 " return; // comment\n"
13998 verifyFormat("void f() {\n"
14005 // In combination with BinPackArguments = false.
14006 FormatStyle NoBinPacking
= getLLVMStyle();
14007 NoBinPacking
.BinPackArguments
= false;
14008 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14020 verifyFormat("const Aaaaaa aaaaa = {\n"
14035 "const Aaaaaa aaaaa = {\n"
14036 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
14037 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
14038 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14042 NoBinPacking
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
14043 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14044 " CDDDP83848_BMCR_REGISTER,\n"
14045 " CDDDP83848_BMSR_REGISTER,\n"
14046 " CDDDP83848_RBR_REGISTER};",
14047 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14048 " CDDDP83848_BMSR_REGISTER,\n"
14049 " CDDDP83848_RBR_REGISTER};",
14052 // FIXME: The alignment of these trailing comments might be bad. Then again,
14053 // this might be utterly useless in real code.
14054 verifyFormat("Constructor::Constructor()\n"
14055 " : some_value{ //\n"
14059 // In braced lists, the first comment is always assumed to belong to the
14060 // first element. Thus, it can be moved to the next or previous line as
14062 verifyFormat("function({// First element:\n"
14064 " // Second element:\n"
14067 " // First element:\n"
14069 " // Second element:\n"
14071 verifyFormat("std::vector<int> MyNumbers{\n"
14072 " // First element:\n"
14074 " // Second element:\n"
14076 "std::vector<int> MyNumbers{// First element:\n"
14078 " // Second element:\n"
14080 getLLVMStyleWithColumns(30));
14081 // A trailing comma should still lead to an enforced line break and no
14083 verifyFormat("vector<int> SomeVector = {\n"
14088 "vector<int> SomeVector = { // aaa\n"
14091 // C++11 brace initializer list l-braces should not be treated any differently
14092 // when breaking before lambda bodies is enabled
14093 FormatStyle BreakBeforeLambdaBody
= getLLVMStyle();
14094 BreakBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14095 BreakBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
14096 BreakBeforeLambdaBody
.AlwaysBreakBeforeMultilineStrings
= true;
14098 "std::runtime_error{\n"
14099 " \"Long string which will force a break onto the next line...\"};",
14100 BreakBeforeLambdaBody
);
14102 FormatStyle ExtraSpaces
= getLLVMStyle();
14103 ExtraSpaces
.Cpp11BracedListStyle
= false;
14104 ExtraSpaces
.ColumnLimit
= 75;
14105 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces
);
14106 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces
);
14107 verifyFormat("f({ 1, 2 });", ExtraSpaces
);
14108 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces
);
14109 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces
);
14110 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces
);
14111 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces
);
14112 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces
);
14113 verifyFormat("return { arg1, arg2 };", ExtraSpaces
);
14114 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces
);
14115 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces
);
14116 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces
);
14117 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces
);
14118 verifyFormat("class Class {\n"
14119 " T member = { arg1, arg2 };\n"
14123 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14124 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14125 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14126 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14128 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces
);
14129 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14132 "someFunction(OtherParam,\n"
14133 " BracedList{ // comment 1 (Forcing interesting break)\n"
14134 " param1, param2,\n"
14136 " param3, param4 });",
14139 "std::this_thread::sleep_for(\n"
14140 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14142 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14146 " aaaaaaaaaaaaaaa,\n"
14150 " aaaaaaaaaaaaaaaaaaaaa,\n"
14152 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14155 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces
);
14156 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces
);
14157 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces
);
14159 // Avoid breaking between initializer/equal sign and opening brace
14160 ExtraSpaces
.PenaltyBreakBeforeFirstCallParameter
= 200;
14161 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14162 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14163 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14164 " { \"ccccccccccccccccccccc\", 2 }\n"
14167 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14168 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14169 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14170 " { \"ccccccccccccccccccccc\", 2 }\n"
14174 FormatStyle SpaceBeforeBrace
= getLLVMStyle();
14175 SpaceBeforeBrace
.SpaceBeforeCpp11BracedList
= true;
14176 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace
);
14177 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace
);
14179 FormatStyle SpaceBetweenBraces
= getLLVMStyle();
14180 SpaceBetweenBraces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
14181 SpaceBetweenBraces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
14182 SpaceBetweenBraces
.SpacesInParensOptions
.Other
= true;
14183 SpaceBetweenBraces
.SpacesInSquareBrackets
= true;
14184 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces
);
14185 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces
);
14186 verifyFormat("vector< int > x{ // comment 1\n"
14188 SpaceBetweenBraces
);
14189 SpaceBetweenBraces
.ColumnLimit
= 20;
14190 verifyFormat("vector< int > x{\n"
14192 "vector<int>x{1,2,3,4};", SpaceBetweenBraces
);
14193 SpaceBetweenBraces
.ColumnLimit
= 24;
14194 verifyFormat("vector< int > x{ 1, 2,\n"
14196 "vector<int>x{1,2,3,4};", SpaceBetweenBraces
);
14197 verifyFormat("vector< int > x{\n"
14203 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces
);
14204 verifyFormat("vector< int > x{};", SpaceBetweenBraces
);
14205 SpaceBetweenBraces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
14206 SpaceBetweenBraces
.SpacesInParensOptions
.InEmptyParentheses
= true;
14207 verifyFormat("vector< int > x{ };", SpaceBetweenBraces
);
14210 TEST_F(FormatTest
, FormatsBracedListsInColumnLayout
) {
14211 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14212 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14213 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14214 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14215 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14216 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14217 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14218 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14219 " 1, 22, 333, 4444, 55555, //\n"
14220 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14221 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14223 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14224 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14225 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14226 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14227 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14228 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14230 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14231 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14232 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14233 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14234 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14235 " // Separating comment.\n"
14236 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14237 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14238 " // Leading comment\n"
14239 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14240 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14241 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14243 getLLVMStyleWithColumns(39));
14244 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14246 getLLVMStyleWithColumns(38));
14247 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14248 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14249 getLLVMStyleWithColumns(43));
14251 "static unsigned SomeValues[10][3] = {\n"
14252 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14253 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14254 verifyFormat("static auto fields = new vector<string>{\n"
14255 " \"aaaaaaaaaaaaa\",\n"
14256 " \"aaaaaaaaaaaaa\",\n"
14257 " \"aaaaaaaaaaaa\",\n"
14258 " \"aaaaaaaaaaaaaa\",\n"
14259 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14260 " \"aaaaaaaaaaaa\",\n"
14261 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14263 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14264 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14265 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14266 " 3, cccccccccccccccccccccc};",
14267 getLLVMStyleWithColumns(60));
14269 // Trailing commas.
14270 verifyFormat("vector<int> x = {\n"
14271 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14273 getLLVMStyleWithColumns(39));
14274 verifyFormat("vector<int> x = {\n"
14275 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14277 getLLVMStyleWithColumns(39));
14278 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14281 getLLVMStyleWithColumns(39));
14283 // Trailing comment in the first line.
14284 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14285 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14286 " 111111111, 222222222, 3333333333, 444444444, //\n"
14287 " 11111111, 22222222, 333333333, 44444444};");
14288 // Trailing comment in the last line.
14289 verifyFormat("int aaaaa[] = {\n"
14290 " 1, 2, 3, // comment\n"
14291 " 4, 5, 6 // comment\n"
14294 // With nested lists, we should either format one item per line or all nested
14295 // lists one on line.
14296 // FIXME: For some nested lists, we can do better.
14297 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14298 " {aaaaaaaaaaaaaaaaaaa},\n"
14299 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14300 " {aaaaaaaaaaaaaaaaa}};",
14301 getLLVMStyleWithColumns(60));
14303 "SomeStruct my_struct_array = {\n"
14304 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14305 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14308 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14309 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14310 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14312 // No column layout should be used here.
14313 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14314 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14316 verifyNoCrash("a<,");
14318 // No braced initializer here.
14319 verifyFormat("void f() {\n"
14320 " struct Dummy {};\n"
14323 verifyFormat("void foo() {\n"
14335 verifyFormat("namespace n {\n"
14347 "} // namespace n");
14349 // Long lists should be formatted in columns even if they are nested.
14351 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14352 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14353 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14354 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14355 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14356 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14358 // Allow "single-column" layout even if that violates the column limit. There
14359 // isn't going to be a better way.
14360 verifyFormat("std::vector<int> a = {\n"
14367 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14368 getLLVMStyleWithColumns(30));
14369 verifyFormat("vector<int> aaaa = {\n"
14370 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14371 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14372 " aaaaaa.aaaaaaa,\n"
14373 " aaaaaa.aaaaaaa,\n"
14374 " aaaaaa.aaaaaaa,\n"
14375 " aaaaaa.aaaaaaa,\n"
14378 // Don't create hanging lists.
14379 verifyFormat("someFunction(Param, {List1, List2,\n"
14381 getLLVMStyleWithColumns(35));
14382 verifyFormat("someFunction(Param, Param,\n"
14383 " {List1, List2,\n"
14385 getLLVMStyleWithColumns(35));
14386 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14387 " aaaaaaaaaaaaaaaaaaaaaaa);");
14389 // No possible column formats, don't want the optimal paths penalized.
14391 "waarudo::unit desk = {\n"
14392 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14393 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14394 "Output{1, 2}; },\n"
14395 " [](const Input &i) -> Output { return "
14396 "Output{1, 2}; });");
14397 FormatStyle NoBinPacking
= getLLVMStyle();
14398 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
14399 verifyFormat("waarudo::unit desk = {\n"
14400 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14401 "1, 1} * w::m; }};",
14405 TEST_F(FormatTest
, PullTrivialFunctionDefinitionsIntoSingleLine
) {
14406 FormatStyle DoNotMerge
= getLLVMStyle();
14407 DoNotMerge
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14409 verifyFormat("void f() { return 42; }");
14410 verifyFormat("void f() {\n"
14414 verifyFormat("void f() {\n"
14425 verifyFormat("void f() {} // comment");
14426 verifyFormat("void f() { int a; } // comment");
14427 verifyFormat("void f() {\n"
14430 verifyFormat("void f() {\n"
14434 verifyFormat("void f() {\n"
14436 getLLVMStyleWithColumns(15));
14438 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14439 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14441 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14442 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14443 verifyGoogleFormat("class C {\n"
14445 " : iiiiiiii(nullptr),\n"
14446 " kkkkkkk(nullptr),\n"
14447 " mmmmmmm(nullptr),\n"
14448 " nnnnnnn(nullptr) {}\n"
14451 FormatStyle NoColumnLimit
= getLLVMStyleWithColumns(0);
14452 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit
);
14453 verifyFormat("class C {\n"
14456 "class C{A():b(0){}};", NoColumnLimit
);
14457 verifyFormat("A()\n"
14460 "A()\n:b(0)\n{\n}", NoColumnLimit
);
14462 FormatStyle NoColumnLimitWrapAfterFunction
= NoColumnLimit
;
14463 NoColumnLimitWrapAfterFunction
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14464 NoColumnLimitWrapAfterFunction
.BraceWrapping
.AfterFunction
= true;
14465 verifyFormat("class C {\n"
14467 " int foo { return 0; }\n"
14469 NoColumnLimitWrapAfterFunction
);
14470 verifyFormat("class C {\n"
14474 NoColumnLimitWrapAfterFunction
);
14476 FormatStyle DoNotMergeNoColumnLimit
= NoColumnLimit
;
14477 DoNotMergeNoColumnLimit
.AllowShortFunctionsOnASingleLine
=
14478 FormatStyle::SFS_None
;
14479 verifyFormat("A()\n"
14482 "A():b(0){}", DoNotMergeNoColumnLimit
);
14483 verifyFormat("A()\n"
14486 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit
);
14488 verifyFormat("#define A \\\n"
14492 getLLVMStyleWithColumns(20));
14493 verifyFormat("#define A \\\n"
14494 " void f() { int i; }",
14495 getLLVMStyleWithColumns(21));
14496 verifyFormat("#define A \\\n"
14501 getLLVMStyleWithColumns(22));
14502 verifyFormat("#define A \\\n"
14503 " void f() { int i; } \\\n"
14505 getLLVMStyleWithColumns(23));
14508 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14509 " aaaaaaaaaaaaaaaaaa,\n"
14510 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14512 constexpr StringRef Code
{"void foo() { /* Empty */ }"};
14513 verifyFormat(Code
);
14514 verifyFormat(Code
, "void foo() { /* Empty */\n"
14516 verifyFormat(Code
, "void foo() {\n"
14521 TEST_F(FormatTest
, PullEmptyFunctionDefinitionsIntoSingleLine
) {
14522 FormatStyle MergeEmptyOnly
= getLLVMStyle();
14523 MergeEmptyOnly
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Empty
;
14524 verifyFormat("class C {\n"
14528 verifyFormat("class C {\n"
14534 verifyFormat("int f() {}", MergeEmptyOnly
);
14535 verifyFormat("int f() {\n"
14540 // Also verify behavior when BraceWrapping.AfterFunction = true
14541 MergeEmptyOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14542 MergeEmptyOnly
.BraceWrapping
.AfterFunction
= true;
14543 verifyFormat("int f() {}", MergeEmptyOnly
);
14544 verifyFormat("class C {\n"
14550 TEST_F(FormatTest
, PullInlineFunctionDefinitionsIntoSingleLine
) {
14551 FormatStyle MergeInlineOnly
= getLLVMStyle();
14552 MergeInlineOnly
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Inline
;
14553 verifyFormat("class C {\n"
14554 " int f() { return 42; }\n"
14557 verifyFormat("int f() {\n"
14562 // SFS_Inline implies SFS_Empty
14563 verifyFormat("class C {\n"
14567 verifyFormat("int f() {}", MergeInlineOnly
);
14568 // https://llvm.org/PR54147
14569 verifyFormat("auto lambda = []() {\n"
14576 verifyFormat("class C {\n"
14578 " int f() { return 42; }\n"
14583 verifyFormat("struct S {\n"
14586 " int foo() { bar(); }\n"
14591 // Also verify behavior when BraceWrapping.AfterFunction = true
14592 MergeInlineOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14593 MergeInlineOnly
.BraceWrapping
.AfterFunction
= true;
14594 verifyFormat("class C {\n"
14595 " int f() { return 42; }\n"
14598 verifyFormat("int f()\n"
14604 // SFS_Inline implies SFS_Empty
14605 verifyFormat("int f() {}", MergeInlineOnly
);
14606 verifyFormat("class C {\n"
14611 MergeInlineOnly
.BraceWrapping
.AfterClass
= true;
14612 MergeInlineOnly
.BraceWrapping
.AfterStruct
= true;
14613 verifyFormat("class C\n"
14615 " int f() { return 42; }\n"
14618 verifyFormat("struct C\n"
14620 " int f() { return 42; }\n"
14623 verifyFormat("int f()\n"
14628 verifyFormat("int f() {}", MergeInlineOnly
);
14629 verifyFormat("class C\n"
14631 " int f() { return 42; }\n"
14634 verifyFormat("struct C\n"
14636 " int f() { return 42; }\n"
14639 verifyFormat("struct C\n"
14644 " int f() { return 42; }\n"
14647 verifyFormat("/* comment */ struct C\n"
14649 " int f() { return 42; }\n"
14654 TEST_F(FormatTest
, PullInlineOnlyFunctionDefinitionsIntoSingleLine
) {
14655 FormatStyle MergeInlineOnly
= getLLVMStyle();
14656 MergeInlineOnly
.AllowShortFunctionsOnASingleLine
=
14657 FormatStyle::SFS_InlineOnly
;
14658 verifyFormat("class C {\n"
14659 " int f() { return 42; }\n"
14662 verifyFormat("int f() {\n"
14667 // SFS_InlineOnly does not imply SFS_Empty
14668 verifyFormat("class C {\n"
14672 verifyFormat("int f() {\n"
14676 // Also verify behavior when BraceWrapping.AfterFunction = true
14677 MergeInlineOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14678 MergeInlineOnly
.BraceWrapping
.AfterFunction
= true;
14679 verifyFormat("class C {\n"
14680 " int f() { return 42; }\n"
14683 verifyFormat("int f()\n"
14689 // SFS_InlineOnly does not imply SFS_Empty
14690 verifyFormat("int f()\n"
14694 verifyFormat("class C {\n"
14700 TEST_F(FormatTest
, SplitEmptyFunction
) {
14701 FormatStyle Style
= getLLVMStyleWithColumns(40);
14702 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14703 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14704 Style
.BraceWrapping
.AfterFunction
= true;
14705 Style
.BraceWrapping
.SplitEmptyFunction
= false;
14707 verifyFormat("int f()\n"
14710 verifyFormat("int f()\n"
14715 verifyFormat("int f()\n"
14717 " // some comment\n"
14721 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Empty
;
14722 verifyFormat("int f() {}", Style
);
14723 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14726 verifyFormat("int f()\n"
14732 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Inline
;
14733 verifyFormat("class Foo {\n"
14737 verifyFormat("class Foo {\n"
14738 " int f() { return 0; }\n"
14741 verifyFormat("class Foo {\n"
14742 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14746 verifyFormat("class Foo {\n"
14747 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14754 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
14755 verifyFormat("int f() {}", Style
);
14756 verifyFormat("int f() { return 0; }", Style
);
14757 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14760 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14767 TEST_F(FormatTest
, SplitEmptyFunctionButNotRecord
) {
14768 FormatStyle Style
= getLLVMStyleWithColumns(40);
14769 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14770 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14771 Style
.BraceWrapping
.AfterFunction
= true;
14772 Style
.BraceWrapping
.SplitEmptyFunction
= true;
14773 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14775 verifyFormat("class C {};", Style
);
14776 verifyFormat("struct C {};", Style
);
14777 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14778 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14782 verifyFormat("class C {\n"
14784 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14785 " bbbbbbbbbbbbbbbbbbb()\n"
14789 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14790 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14797 TEST_F(FormatTest
, KeepShortFunctionAfterPPElse
) {
14798 FormatStyle Style
= getLLVMStyle();
14799 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
14800 verifyFormat("#ifdef A\n"
14808 TEST_F(FormatTest
, SplitEmptyClass
) {
14809 FormatStyle Style
= getLLVMStyle();
14810 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14811 Style
.BraceWrapping
.AfterClass
= true;
14812 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14814 verifyFormat("class Foo\n"
14817 verifyFormat("/* something */ class Foo\n"
14820 verifyFormat("template <typename X> class Foo\n"
14823 verifyFormat("class Foo\n"
14828 verifyFormat("typedef class Foo\n"
14833 Style
.BraceWrapping
.SplitEmptyRecord
= true;
14834 Style
.BraceWrapping
.AfterStruct
= true;
14835 verifyFormat("class rep\n"
14839 verifyFormat("struct rep\n"
14843 verifyFormat("template <typename T> class rep\n"
14847 verifyFormat("template <typename T> struct rep\n"
14851 verifyFormat("class rep\n"
14856 verifyFormat("struct rep\n"
14861 verifyFormat("template <typename T> class rep\n"
14866 verifyFormat("template <typename T> struct rep\n"
14871 verifyFormat("template <typename T> class rep // Foo\n"
14876 verifyFormat("template <typename T> struct rep // Bar\n"
14882 verifyFormat("template <typename T> class rep<T>\n"
14888 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14893 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14898 verifyFormat("#include \"stdint.h\"\n"
14899 "namespace rep {}",
14901 verifyFormat("#include <stdint.h>\n"
14902 "namespace rep {}",
14904 verifyFormat("#include <stdint.h>\n"
14905 "namespace rep {}",
14906 "#include <stdint.h>\n"
14907 "namespace rep {\n"
14914 TEST_F(FormatTest
, SplitEmptyStruct
) {
14915 FormatStyle Style
= getLLVMStyle();
14916 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14917 Style
.BraceWrapping
.AfterStruct
= true;
14918 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14920 verifyFormat("struct Foo\n"
14923 verifyFormat("/* something */ struct Foo\n"
14926 verifyFormat("template <typename X> struct Foo\n"
14929 verifyFormat("struct Foo\n"
14934 verifyFormat("typedef struct Foo\n"
14938 // typedef struct Bar {} Bar_t;
14941 TEST_F(FormatTest
, SplitEmptyUnion
) {
14942 FormatStyle Style
= getLLVMStyle();
14943 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14944 Style
.BraceWrapping
.AfterUnion
= true;
14945 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14947 verifyFormat("union Foo\n"
14950 verifyFormat("/* something */ union Foo\n"
14953 verifyFormat("union Foo\n"
14958 verifyFormat("typedef union Foo\n"
14964 TEST_F(FormatTest
, SplitEmptyNamespace
) {
14965 FormatStyle Style
= getLLVMStyle();
14966 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14967 Style
.BraceWrapping
.AfterNamespace
= true;
14968 Style
.BraceWrapping
.SplitEmptyNamespace
= false;
14970 verifyFormat("namespace Foo\n"
14973 verifyFormat("/* something */ namespace Foo\n"
14976 verifyFormat("inline namespace Foo\n"
14979 verifyFormat("/* something */ inline namespace Foo\n"
14982 verifyFormat("export namespace Foo\n"
14985 verifyFormat("namespace Foo\n"
14992 TEST_F(FormatTest
, NeverMergeShortRecords
) {
14993 FormatStyle Style
= getLLVMStyle();
14995 verifyFormat("class Foo {\n"
14999 verifyFormat("typedef class Foo {\n"
15003 verifyFormat("struct Foo {\n"
15007 verifyFormat("typedef struct Foo {\n"
15011 verifyFormat("union Foo {\n"
15015 verifyFormat("typedef union Foo {\n"
15019 verifyFormat("namespace Foo {\n"
15024 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
15025 Style
.BraceWrapping
.AfterClass
= true;
15026 Style
.BraceWrapping
.AfterStruct
= true;
15027 Style
.BraceWrapping
.AfterUnion
= true;
15028 Style
.BraceWrapping
.AfterNamespace
= true;
15029 verifyFormat("class Foo\n"
15034 verifyFormat("typedef class Foo\n"
15039 verifyFormat("struct Foo\n"
15044 verifyFormat("typedef struct Foo\n"
15049 verifyFormat("union Foo\n"
15054 verifyFormat("typedef union Foo\n"
15059 verifyFormat("namespace Foo\n"
15066 TEST_F(FormatTest
, UnderstandContextOfRecordTypeKeywords
) {
15067 // Elaborate type variable declarations.
15068 verifyFormat("struct foo a = {bar};\nint n;");
15069 verifyFormat("class foo a = {bar};\nint n;");
15070 verifyFormat("union foo a = {bar};\nint n;");
15072 // Elaborate types inside function definitions.
15073 verifyFormat("struct foo f() {}\nint n;");
15074 verifyFormat("class foo f() {}\nint n;");
15075 verifyFormat("union foo f() {}\nint n;");
15078 verifyFormat("template <class X> void f() {}\nint n;");
15079 verifyFormat("template <struct X> void f() {}\nint n;");
15080 verifyFormat("template <union X> void f() {}\nint n;");
15082 // Actual definitions...
15083 verifyFormat("struct {\n} n;");
15085 "template <template <class T, class Y>, class Z> class X {\n} n;");
15086 verifyFormat("union Z {\n int n;\n} x;");
15087 verifyFormat("class MACRO Z {\n} n;");
15088 verifyFormat("class MACRO(X) Z {\n} n;");
15089 verifyFormat("class __attribute__((X)) Z {\n} n;");
15090 verifyFormat("class __declspec(X) Z {\n} n;");
15091 verifyFormat("class A##B##C {\n} n;");
15092 verifyFormat("class alignas(16) Z {\n} n;");
15093 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15094 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15096 // Redefinition from nested context:
15097 verifyFormat("class A::B::C {\n} n;");
15099 // Template definitions.
15101 "template <typename F>\n"
15102 "Matcher(const Matcher<F> &Other,\n"
15103 " typename enable_if_c<is_base_of<F, T>::value &&\n"
15104 " !is_same<F, T>::value>::type * = 0)\n"
15105 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15107 // FIXME: This is still incorrectly handled at the formatter side.
15108 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15109 verifyFormat("int i = SomeFunction(a<b, a> b);");
15111 verifyFormat("class A<int> f() {}\n"
15113 verifyFormat("template <typename T> class A<T> f() {}\n"
15116 verifyFormat("template <> class Foo<int> F() {\n"
15119 // Elaborate types where incorrectly parsing the structural element would
15120 // break the indent.
15121 verifyFormat("if (true)\n"
15126 // This is simply incomplete. Formatting is not important, but must not crash.
15127 verifyFormat("class A:");
15130 TEST_F(FormatTest
, DoNotInterfereWithErrorAndWarning
) {
15131 verifyNoChange("#error Leave all white!!!!! space* alone!");
15132 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15133 verifyFormat("#error 1", " # error 1");
15134 verifyFormat("#warning 1", " # warning 1");
15137 TEST_F(FormatTest
, FormatHashIfExpressions
) {
15138 verifyFormat("#if AAAA && BBBB");
15139 verifyFormat("#if (AAAA && BBBB)");
15140 verifyFormat("#elif (AAAA && BBBB)");
15141 // FIXME: Come up with a better indentation for #elif.
15143 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15144 " defined(BBBBBBBB)\n"
15145 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15146 " defined(BBBBBBBB)\n"
15148 getLLVMStyleWithColumns(65));
15151 TEST_F(FormatTest
, MergeHandlingInTheFaceOfPreprocessorDirectives
) {
15152 FormatStyle AllowsMergedIf
= getGoogleStyle();
15153 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
15154 FormatStyle::SIS_WithoutElse
;
15155 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf
);
15156 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf
);
15157 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf
);
15158 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf
);
15159 FormatStyle ShortMergedIf
= AllowsMergedIf
;
15160 ShortMergedIf
.ColumnLimit
= 25;
15161 verifyFormat("#define A \\\n"
15162 " if (true) return 42;",
15164 verifyFormat("#define A \\\n"
15169 verifyFormat("#define A \\\n"
15177 " if (true) continue;\n"
15180 " if (true) continue;\n"
15183 ShortMergedIf
.ColumnLimit
= 33;
15184 verifyFormat("#define A \\\n"
15185 " if constexpr (true) return 42;",
15187 verifyFormat("#define A \\\n"
15188 " if CONSTEXPR (true) return 42;",
15190 ShortMergedIf
.ColumnLimit
= 29;
15191 verifyFormat("#define A \\\n"
15192 " if (aaaaaaaaaa) return 1; \\\n"
15195 ShortMergedIf
.ColumnLimit
= 28;
15196 verifyFormat("#define A \\\n"
15197 " if (aaaaaaaaaa) \\\n"
15201 verifyFormat("#define A \\\n"
15202 " if constexpr (aaaaaaa) \\\n"
15206 verifyFormat("#define A \\\n"
15207 " if CONSTEXPR (aaaaaaa) \\\n"
15212 verifyFormat("//\n"
15216 getChromiumStyle(FormatStyle::LK_Cpp
));
15219 TEST_F(FormatTest
, FormatStarDependingOnContext
) {
15220 verifyFormat("void f(int *a);");
15221 verifyFormat("void f() { f(fint * b); }");
15222 verifyFormat("class A {\n void f(int *a);\n};");
15223 verifyFormat("class A {\n int *a;\n};");
15224 verifyFormat("namespace a {\n"
15230 "} // namespace b\n"
15231 "} // namespace a");
15234 TEST_F(FormatTest
, SpecialTokensAtEndOfLine
) {
15235 verifyFormat("while");
15236 verifyFormat("operator");
15239 TEST_F(FormatTest
, SkipsDeeplyNestedLines
) {
15240 // This code would be painfully slow to format if we didn't skip it.
15241 std::string
Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
15242 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15243 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15244 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15245 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15247 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15248 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15249 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15250 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15251 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15252 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15253 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15254 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15255 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15256 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15257 // Deeply nested part is untouched, rest is formatted.
15258 EXPECT_EQ(std::string("int i;") + Code
+ "int j;",
15259 format(std::string("int i;") + Code
+ "int j;",
15260 getLLVMStyle(), SC_ExpectIncomplete
));
15263 //===----------------------------------------------------------------------===//
15264 // Objective-C tests.
15265 //===----------------------------------------------------------------------===//
15267 TEST_F(FormatTest
, FormatForObjectiveCMethodDecls
) {
15268 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15269 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15270 "-(NSUInteger)indexOfObject:(id)anObject;");
15271 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15272 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15273 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15274 "-(NSInteger)Method3:(id)anObject;");
15275 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15276 "-(NSInteger)Method4:(id)anObject;");
15277 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15278 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15279 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15280 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15281 "forAllCells:(BOOL)flag;");
15283 // Very long objectiveC method declaration.
15284 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15285 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15286 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15287 " inRange:(NSRange)range\n"
15288 " outRange:(NSRange)out_range\n"
15289 " outRange1:(NSRange)out_range1\n"
15290 " outRange2:(NSRange)out_range2\n"
15291 " outRange3:(NSRange)out_range3\n"
15292 " outRange4:(NSRange)out_range4\n"
15293 " outRange5:(NSRange)out_range5\n"
15294 " outRange6:(NSRange)out_range6\n"
15295 " outRange7:(NSRange)out_range7\n"
15296 " outRange8:(NSRange)out_range8\n"
15297 " outRange9:(NSRange)out_range9;");
15299 // When the function name has to be wrapped.
15300 FormatStyle Style
= getLLVMStyle();
15301 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15302 // and always indents instead.
15303 Style
.IndentWrappedFunctionNames
= false;
15304 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15305 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15306 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15309 Style
.IndentWrappedFunctionNames
= true;
15310 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15311 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15312 " anotherName:(NSString)dddddddddddddd {\n"
15316 verifyFormat("- (int)sum:(vector<int>)numbers;");
15317 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15318 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15319 // protocol lists (but not for template classes):
15320 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15322 verifyFormat("- (int (*)())foo:(int (*)())f;");
15323 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15325 // If there's no return type (very rare in practice!), LLVM and Google style
15327 verifyFormat("- foo;");
15328 verifyFormat("- foo:(int)f;");
15329 verifyGoogleFormat("- foo:(int)foo;");
15332 TEST_F(FormatTest
, BreaksStringLiterals
) {
15333 // FIXME: unstable test case
15334 EXPECT_EQ("\"some text \"\n"
15336 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15337 // FIXME: unstable test case
15338 EXPECT_EQ("\"some text \"\n"
15340 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15341 verifyFormat("#define A \\\n"
15345 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15346 verifyFormat("#define A \\\n"
15350 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15352 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15353 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15354 // FIXME: unstable test case
15355 EXPECT_EQ("\"some \"\n"
15357 format("\"some text\"", getLLVMStyleWithColumns(10)));
15358 // FIXME: unstable test case
15359 EXPECT_EQ("\"some \"\n"
15361 format("\"some text\"", getLLVMStyleWithColumns(7)));
15362 // FIXME: unstable test case
15363 EXPECT_EQ("\"some\"\n"
15366 format("\"some text\"", getLLVMStyleWithColumns(6)));
15367 // FIXME: unstable test case
15368 EXPECT_EQ("\"some\"\n"
15371 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15372 // FIXME: unstable test case
15373 EXPECT_EQ("\"some\"\n"
15376 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15378 verifyFormat("variable =\n"
15379 " \"long string \"\n"
15381 "variable = \"long string literal\";",
15382 getLLVMStyleWithColumns(20));
15384 verifyFormat("variable = f(\n"
15385 " \"long string \"\n"
15388 " loooooooooooooooooooong);",
15389 "variable = f(\"long string literal\", short, "
15390 "loooooooooooooooooooong);",
15391 getLLVMStyleWithColumns(20));
15393 verifyFormat("f(g(\"long string \"\n"
15396 "f(g(\"long string literal\"), b);",
15397 getLLVMStyleWithColumns(20));
15398 verifyFormat("f(g(\"long string \"\n"
15402 "f(g(\"long string literal\", a), b);",
15403 getLLVMStyleWithColumns(20));
15404 verifyFormat("f(\"one two\".split(\n"
15406 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15407 verifyFormat("f(\"one two three four five six \"\n"
15408 " \"seven\".split(\n"
15409 " really_looooong_variable));",
15410 "f(\"one two three four five six seven\"."
15411 "split(really_looooong_variable));",
15412 getLLVMStyleWithColumns(33));
15414 verifyFormat("f(\"some \"\n"
15417 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15419 // Only break as a last resort.
15421 "aaaaaaaaaaaaaaaaaaaa(\n"
15422 " aaaaaaaaaaaaaaaaaaaa,\n"
15423 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15425 // FIXME: unstable test case
15426 EXPECT_EQ("\"splitmea\"\n"
15429 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15431 // FIXME: unstable test case
15432 EXPECT_EQ("\"split/\"\n"
15435 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15437 // FIXME: unstable test case
15438 EXPECT_EQ("\"split/\"\n"
15441 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15442 // FIXME: unstable test case
15443 EXPECT_EQ("\"split at \"\n"
15445 "\"slashes.at.any$\"\n"
15446 "\"non-alphanumeric%\"\n"
15447 "\"1111111111characte\"\n"
15449 format("\"split at "
15454 "1111111111characte"
15456 getLLVMStyleWithColumns(20)));
15458 // Verify that splitting the strings understands
15459 // Style::AlwaysBreakBeforeMultilineStrings.
15460 verifyFormat("aaaaaaaaaaaa(\n"
15461 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15462 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15463 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15464 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15465 "aaaaaaaaaaaaaaaaaaaaaa\");",
15467 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15468 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15469 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15470 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15471 "aaaaaaaaaaaaaaaaaaaaaa\";",
15473 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15474 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15476 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15477 "aaaaaaaaaaaaaaaaaaa\";");
15478 verifyFormat("ffff(\n"
15479 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15480 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15481 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15482 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15485 FormatStyle Style
= getLLVMStyleWithColumns(12);
15486 Style
.BreakStringLiterals
= false;
15487 verifyFormat("\"some text other\";", Style
);
15489 FormatStyle AlignLeft
= getLLVMStyleWithColumns(12);
15490 AlignLeft
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
15491 verifyFormat("#define A \\\n"
15495 "#define A \"some text other\";", AlignLeft
);
15498 TEST_F(FormatTest
, BreaksStringLiteralsAtColumnLimit
) {
15499 verifyFormat("C a = \"some more \"\n"
15501 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15504 TEST_F(FormatTest
, FullyRemoveEmptyLines
) {
15505 FormatStyle NoEmptyLines
= getLLVMStyleWithColumns(80);
15506 NoEmptyLines
.MaxEmptyLinesToKeep
= 0;
15507 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15511 TEST_F(FormatTest
, BreaksStringLiteralsWithTabs
) {
15512 // FIXME: unstable test case
15514 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15517 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15522 TEST_F(FormatTest
, BreaksWideAndNSStringLiterals
) {
15523 // FIXME: unstable test case
15525 "u8\"utf8 string \"\n"
15527 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15528 // FIXME: unstable test case
15530 "u\"utf16 string \"\n"
15532 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15533 // FIXME: unstable test case
15535 "U\"utf32 string \"\n"
15537 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15538 // FIXME: unstable test case
15539 EXPECT_EQ("L\"wide string \"\n"
15541 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15542 verifyFormat("@\"NSString \"\n"
15544 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15545 verifyFormat(R
"(NSString *s = @"那那那那
";)", getLLVMStyleWithColumns(26));
15547 // This input makes clang-format try to split the incomplete unicode escape
15548 // sequence, which used to lead to a crasher.
15550 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15551 getLLVMStyleWithColumns(60));
15554 TEST_F(FormatTest
, DoesNotBreakRawStringLiterals
) {
15555 FormatStyle Style
= getGoogleStyleWithColumns(15);
15556 verifyFormat("R\"x(raw literal)x\";", Style
);
15557 verifyFormat("uR\"x(raw literal)x\";", Style
);
15558 verifyFormat("LR\"x(raw literal)x\";", Style
);
15559 verifyFormat("UR\"x(raw literal)x\";", Style
);
15560 verifyFormat("u8R\"x(raw literal)x\";", Style
);
15563 TEST_F(FormatTest
, BreaksStringLiteralsWithin_TMacro
) {
15564 FormatStyle Style
= getLLVMStyleWithColumns(20);
15565 // FIXME: unstable test case
15567 "_T(\"aaaaaaaaaaaaaa\")\n"
15568 "_T(\"aaaaaaaaaaaaaa\")\n"
15569 "_T(\"aaaaaaaaaaaa\")",
15570 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style
));
15571 verifyFormat("f(x,\n"
15572 " _T(\"aaaaaaaaaaaa\")\n"
15575 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style
);
15577 // FIXME: Handle embedded spaces in one iteration.
15578 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15579 // "_T(\"aaaaaaaaaaaaa\")\n"
15580 // "_T(\"aaaaaaaaaaaaa\")\n"
15582 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15583 // getLLVMStyleWithColumns(20)));
15584 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15585 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style
);
15586 verifyFormat("f(\n"
15588 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15593 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15596 verifyFormat("f(\n"
15598 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15601 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15602 // Regression test for accessing tokens past the end of a vector in the
15604 verifyNoCrash(R
"(_T(
15610 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15611 // In a function call with two operands, the second can be broken with no line
15612 // break before it.
15613 verifyFormat("func(a
, \"long long \"\n"
15614 " \"long long\");",
15615 "func(a
, \"long long long long\");",
15616 getLLVMStyleWithColumns(24));
15617 // In a function call with three operands, the second must be broken with a
15618 // line break before it.
15619 verifyFormat("func(a
,\n"
15620 " \"long long long \"\n"
15623 "func(a
, \"long long long long\", c
);",
15624 getLLVMStyleWithColumns(24));
15625 // In a function call with three operands, the third must be broken with a
15626 // line break before it.
15627 verifyFormat("func(a
, b
,\n"
15628 " \"long long long \"\n"
15630 "func(a
, b
, \"long long long long\");",
15631 getLLVMStyleWithColumns(24));
15632 // In a function call with three operands, both the second and the third must
15633 // be broken with a line break before them.
15634 verifyFormat("func(a
,\n"
15635 " \"long long long \"\n"
15637 " \"long long long \"\n"
15639 "func(a
, \"long long long long\", \"long long long long\");",
15640 getLLVMStyleWithColumns(24));
15641 // In a chain of << with two operands, the second can be broken with no line
15642 // break before it.
15643 verifyFormat("a
<< \"line line
\"\n"
15645 "a
<< \"line line line
\";", getLLVMStyleWithColumns(20));
15646 // In a chain of << with three operands, the second can be broken with no line
15647 // break before it.
15648 verifyFormat("abcde
<< \"line
\"\n"
15651 "abcde
<< \"line line line
\" << c
;",
15652 getLLVMStyleWithColumns(20));
15653 // In a chain of << with three operands, the third must be broken with a line
15654 // break before it.
15655 verifyFormat("a
<< b
\n"
15656 " << \"line line
\"\n"
15658 "a
<< b
<< \"line line line
\";", getLLVMStyleWithColumns(20));
15659 // In a chain of << with three operands, the second can be broken with no line
15660 // break before it and the third must be broken with a line break before it.
15661 verifyFormat("abcd
<< \"line line
\"\n"
15663 " << \"line line
\"\n"
15665 "abcd
<< \"line line line
\" << \"line line line
\";",
15666 getLLVMStyleWithColumns(20));
15667 // In a chain of binary operators with two operands, the second can be broken
15668 // with no line break before it.
15669 verifyFormat("abcd
+ \"line line
\"\n"
15671 "abcd
+ \"line line line line
\";", getLLVMStyleWithColumns(20));
15672 // In a chain of binary operators with three operands, the second must be
15673 // broken with a line break before it.
15674 verifyFormat("abcd
+\n"
15675 " \"line line
\"\n"
15676 " \"line line
\" +\n"
15678 "abcd
+ \"line line line line
\" + e
;",
15679 getLLVMStyleWithColumns(20));
15680 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15681 // the first must be broken with a line break before it.
15682 FormatStyle Style = getLLVMStyleWithColumns(25);
15683 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15684 verifyFormat("someFunction(\n"
15685 " \"long long long \"\n"
15688 "someFunction(\"long long long long\", a
);", Style);
15689 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15690 verifyFormat("someFunction(\n"
15691 " \"long long long \"\n"
15698 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15699 verifyFormat("aaaaaaaaaaa
= \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\";",
15702 "aaaaaaaaaaa
= \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\";");
15707 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15708 verifyFormat("f(g(R
\"x(raw literal
)x
\", a
), b
);",
15709 "f(g(R
\"x(raw literal
)x
\", a
), b
);", getGoogleStyle());
15710 verifyFormat("fffffffffff(g(R
\"x(\n"
15711 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15715 "fffffffffff(g(R
\"x(\n"
15716 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15718 getGoogleStyleWithColumns(20));
15719 verifyFormat("fffffffffff(\n"
15721 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15725 "fffffffffff(g(R
\"x(qqq
\n"
15726 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15728 getGoogleStyleWithColumns(20));
15730 verifyNoChange("fffffffffff(R
\"x(\n"
15731 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15733 getGoogleStyleWithColumns(20));
15734 verifyFormat("fffffffffff(R
\"x(\n"
15735 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15737 "fffffffffff(R
\"x(\n"
15738 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15740 getGoogleStyleWithColumns(20));
15741 verifyFormat("fffffffffff(\n"
15743 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15748 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15750 getGoogleStyleWithColumns(20));
15751 verifyFormat("fffffffffff(R
\"(single line raw string
)\" + bbbbbb
);",
15753 " R
\"(single line raw string
)\" + bbbbbb
);");
15756 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15757 verifyFormat("string a
= \"unterminated
;");
15758 verifyFormat("function(\"unterminated
,\n"
15759 " OtherParameter
);",
15760 "function( \"unterminated
,\n"
15761 " OtherParameter
);");
15764 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15765 FormatStyle Style = getLLVMStyle();
15766 Style.Standard = FormatStyle::LS_Cpp03;
15767 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15768 "#define x(_a) printf(\"foo\"_a);", Style);
15771 TEST_F(FormatTest, CppLexVersion) {
15772 FormatStyle Style = getLLVMStyle();
15773 // Formatting of x * y differs if x is a type.
15774 verifyFormat("void foo() { MACRO(a * b); }", Style);
15775 verifyFormat("void foo() { MACRO(int *b); }", Style);
15777 // LLVM style uses latest lexer.
15778 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15779 Style.Standard = FormatStyle::LS_Cpp17;
15780 // But in c++17, char8_t isn't a keyword.
15781 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15784 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15786 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15787 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15789 "someFunction(\"aaabbbcccdddeeefff\");",
15790 getLLVMStyleWithColumns(25));
15791 verifyFormat("someFunction1234567890(\n"
15792 " \"aaabbbcccdddeeefff\");",
15793 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15794 getLLVMStyleWithColumns(26));
15795 verifyFormat("someFunction1234567890(\n"
15796 " \"aaabbbcccdddeeeff\"\n"
15798 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15799 getLLVMStyleWithColumns(25));
15800 verifyFormat("someFunction1234567890(\n"
15801 " \"aaabbbcccdddeeeff\"\n"
15803 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15804 getLLVMStyleWithColumns(24));
15805 verifyFormat("someFunction(\n"
15806 " \"aaabbbcc ddde \"\n"
15808 "someFunction(\"aaabbbcc ddde efff\");",
15809 getLLVMStyleWithColumns(25));
15810 verifyFormat("someFunction(\"aaabbbccc \"\n"
15812 "someFunction(\"aaabbbccc ddeeefff\");",
15813 getLLVMStyleWithColumns(25));
15814 verifyFormat("someFunction1234567890(\n"
15816 " \"cccdddeeefff\");",
15817 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15818 getLLVMStyleWithColumns(25));
15819 verifyFormat("#define A \\\n"
15821 " \"123456789\" \\\n"
15824 "#define A string s = \"1234567890\"; int i;",
15825 getLLVMStyleWithColumns(20));
15826 verifyFormat("someFunction(\n"
15828 " \"dddeeefff\");",
15829 "someFunction(\"aaabbbcc dddeeefff\");",
15830 getLLVMStyleWithColumns(25));
15833 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15834 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15835 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15836 // FIXME: unstable test case
15837 EXPECT_EQ("\"test\"\n"
15839 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15840 // FIXME: unstable test case
15841 EXPECT_EQ("\"tes\\\\\"\n"
15843 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15844 // FIXME: unstable test case
15845 EXPECT_EQ("\"\\\\\\\\\"\n"
15847 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15848 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15849 // FIXME: unstable test case
15850 EXPECT_EQ("\"\\uff01\"\n"
15852 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15853 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15854 // FIXME: unstable test case
15855 EXPECT_EQ("\"\\x000000000001\"\n"
15857 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15858 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15859 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15860 // FIXME: unstable test case
15861 EXPECT_EQ("\"test\"\n"
15864 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15865 // FIXME: unstable test case
15866 EXPECT_EQ("\"test\\000\"\n"
15869 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15872 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15873 verifyFormat("void f() {\n"
15876 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15881 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15883 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15886 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15887 verifyFormat("class X {\n"
15891 getLLVMStyleWithColumns(12));
15894 TEST_F(FormatTest, ConfigurableIndentWidth) {
15895 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15896 EightIndent.IndentWidth = 8;
15897 EightIndent.ContinuationIndentWidth = 8;
15898 verifyFormat("void f() {\n"
15899 " someFunction();\n"
15905 verifyFormat("class X {\n"
15910 verifyFormat("int x[] = {\n"
15916 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15917 verifyFormat("double\n"
15919 getLLVMStyleWithColumns(8));
15922 TEST_F(FormatTest, ConfigurableUseOfTab) {
15923 FormatStyle Tab = getLLVMStyleWithColumns(42);
15924 Tab.IndentWidth = 8;
15925 Tab.UseTab = FormatStyle::UT_Always;
15926 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15928 verifyFormat("if (aaaaaaaa && // q\n"
15931 "if (aaaaaaaa &&// q\n"
15935 verifyFormat("if (aaa && bbb) // w\n"
15937 "if(aaa&&bbb)// w\n"
15941 verifyFormat("class X {\n"
15943 "\t\tsomeFunction(parameter1,\n"
15944 "\t\t\t parameter2);\n"
15948 verifyFormat("#define A \\\n"
15949 "\tvoid f() { \\\n"
15950 "\t\tsomeFunction( \\\n"
15951 "\t\t parameter1, \\\n"
15952 "\t\t parameter2); \\\n"
15955 verifyFormat("int a;\t // x\n"
15956 "int bbbbbbbb; // x",
15959 FormatStyle TabAlignment
= Tab
;
15960 TabAlignment
.AlignConsecutiveDeclarations
.Enabled
= true;
15961 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Left
;
15962 verifyFormat("unsigned long long big;\n"
15965 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
15966 verifyFormat("unsigned long long big;\n"
15969 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Right
;
15970 verifyFormat("unsigned long long big;\n"
15975 Tab
.IndentWidth
= 8;
15976 verifyFormat("class TabWidth4Indent8 {\n"
15978 "\t\t\t\tsomeFunction(parameter1,\n"
15979 "\t\t\t\t\t\t\t parameter2);\n"
15985 Tab
.IndentWidth
= 4;
15986 verifyFormat("class TabWidth4Indent4 {\n"
15988 "\t\tsomeFunction(parameter1,\n"
15989 "\t\t\t\t\t parameter2);\n"
15995 Tab
.IndentWidth
= 4;
15996 verifyFormat("class TabWidth8Indent4 {\n"
15998 "\tsomeFunction(parameter1,\n"
15999 "\t\t parameter2);\n"
16005 Tab
.IndentWidth
= 8;
16006 verifyFormat("/*\n"
16007 "\t a\t\tcomment\n"
16008 "\t in multiple lines\n"
16011 " \t \t a\t\tcomment\t \t\n"
16012 " \t \t in multiple lines\t\n"
16016 TabAlignment
.UseTab
= FormatStyle::UT_ForIndentation
;
16017 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Left
;
16018 verifyFormat("void f() {\n"
16019 "\tunsigned long long big;\n"
16023 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
16024 verifyFormat("void f() {\n"
16025 "\tunsigned long long big;\n"
16029 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Right
;
16030 verifyFormat("void f() {\n"
16031 "\tunsigned long long big;\n"
16036 Tab
.UseTab
= FormatStyle::UT_ForIndentation
;
16038 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16039 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16040 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16041 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16042 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16043 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16046 verifyFormat("enum AA {\n"
16047 "\ta1, // Force multiple lines\n"
16052 verifyFormat("if (aaaaaaaa && // q\n"
16055 "if (aaaaaaaa &&// q\n"
16059 verifyFormat("class X {\n"
16061 "\t\tsomeFunction(parameter1,\n"
16062 "\t\t parameter2);\n"
16070 "\t\t someFunction(aaaaaaaa,\n"
16087 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16088 "\t bbbbbbbbbbbbb\n"
16093 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16098 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16099 "\t// bbbbbbbbbbbbb\n"
16102 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16107 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16108 "\t bbbbbbbbbbbbb\n"
16113 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16117 verifyNoChange("{\n"
16123 verifyNoChange("{\n"
16130 verifyFormat("void f() {\n"
16131 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16132 "\t : bbbbbbbbbbbbbbbbbb\n"
16135 FormatStyle TabNoBreak
= Tab
;
16136 TabNoBreak
.BreakBeforeTernaryOperators
= false;
16137 verifyFormat("void f() {\n"
16138 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16139 "\t bbbbbbbbbbbbbbbbbb\n"
16142 verifyFormat("void f() {\n"
16143 "\treturn true ?\n"
16144 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16145 "\t bbbbbbbbbbbbbbbbbbbb\n"
16149 Tab
.UseTab
= FormatStyle::UT_Never
;
16150 verifyFormat("/*\n"
16152 " in multiple lines\n"
16155 " \t \t a\t\tcomment\t \t\n"
16156 " \t \t in multiple lines\t\n"
16159 verifyFormat("/* some\n"
16162 " \t \t comment */",
16164 verifyFormat("int a; /* some\n"
16166 " \t \t int a; /* some\n"
16167 " \t \t comment */",
16170 verifyFormat("int a; /* some\n"
16172 " \t \t int\ta; /* some\n"
16173 " \t \t comment */",
16175 verifyFormat("f(\"\t\t\"); /* some\n"
16177 " \t \t f(\"\t\t\"); /* some\n"
16178 " \t \t comment */",
16194 Tab
.UseTab
= FormatStyle::UT_ForContinuationAndIndentation
;
16196 Tab
.IndentWidth
= 8;
16197 verifyFormat("if (aaaaaaaa && // q\n"
16200 "if (aaaaaaaa &&// q\n"
16204 verifyFormat("if (aaa && bbb) // w\n"
16206 "if(aaa&&bbb)// w\n"
16209 verifyFormat("class X {\n"
16211 "\t\tsomeFunction(parameter1,\n"
16212 "\t\t\t parameter2);\n"
16216 verifyFormat("#define A \\\n"
16217 "\tvoid f() { \\\n"
16218 "\t\tsomeFunction( \\\n"
16219 "\t\t parameter1, \\\n"
16220 "\t\t parameter2); \\\n"
16224 Tab
.IndentWidth
= 8;
16225 verifyFormat("class TabWidth4Indent8 {\n"
16227 "\t\t\t\tsomeFunction(parameter1,\n"
16228 "\t\t\t\t\t\t\t parameter2);\n"
16233 Tab
.IndentWidth
= 4;
16234 verifyFormat("class TabWidth4Indent4 {\n"
16236 "\t\tsomeFunction(parameter1,\n"
16237 "\t\t\t\t\t parameter2);\n"
16242 Tab
.IndentWidth
= 4;
16243 verifyFormat("class TabWidth8Indent4 {\n"
16245 "\tsomeFunction(parameter1,\n"
16246 "\t\t parameter2);\n"
16251 Tab
.IndentWidth
= 8;
16252 verifyFormat("/*\n"
16253 "\t a\t\tcomment\n"
16254 "\t in multiple lines\n"
16257 " \t \t a\t\tcomment\t \t\n"
16258 " \t \t in multiple lines\t\n"
16262 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16263 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16264 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16265 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16266 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16267 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16270 verifyFormat("enum AA {\n"
16271 "\ta1, // Force multiple lines\n"
16276 verifyFormat("if (aaaaaaaa && // q\n"
16279 "if (aaaaaaaa &&// q\n"
16283 verifyFormat("class X {\n"
16285 "\t\tsomeFunction(parameter1,\n"
16286 "\t\t\t parameter2);\n"
16294 "\t\t someFunction(aaaaaaaa,\n"
16295 "\t\t\t\t bbbbbbb);\n"
16311 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16312 "\t bbbbbbbbbbbbb\n"
16317 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16322 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16323 "\t// bbbbbbbbbbbbb\n"
16326 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16331 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16332 "\t bbbbbbbbbbbbb\n"
16337 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16341 verifyNoChange("{\n"
16347 verifyNoChange("{\n"
16353 verifyFormat("/* some\n"
16356 " \t \t comment */",
16358 verifyFormat("int a; /* some\n"
16360 " \t \t int a; /* some\n"
16361 " \t \t comment */",
16363 verifyFormat("int a; /* some\n"
16365 " \t \t int\ta; /* some\n"
16366 " \t \t comment */",
16368 verifyFormat("f(\"\t\t\"); /* some\n"
16370 " \t \t f(\"\t\t\"); /* some\n"
16371 " \t \t comment */",
16387 Tab
.IndentWidth
= 2;
16399 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16400 "\t\tbbbbbbbbbbbbb\n"
16405 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16409 Tab
.AlignConsecutiveAssignments
.Enabled
= true;
16410 Tab
.AlignConsecutiveDeclarations
.Enabled
= true;
16412 Tab
.IndentWidth
= 4;
16413 verifyFormat("class Assign {\n"
16415 "\t\tint x = 123;\n"
16416 "\t\tint random = 4;\n"
16417 "\t\tstd::string alphabet =\n"
16418 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16423 Tab
.UseTab
= FormatStyle::UT_AlignWithSpaces
;
16425 Tab
.IndentWidth
= 8;
16426 verifyFormat("if (aaaaaaaa && // q\n"
16429 "if (aaaaaaaa &&// q\n"
16433 verifyFormat("if (aaa && bbb) // w\n"
16435 "if(aaa&&bbb)// w\n"
16438 verifyFormat("class X {\n"
16440 "\t\tsomeFunction(parameter1,\n"
16441 "\t\t parameter2);\n"
16445 verifyFormat("#define A \\\n"
16446 "\tvoid f() { \\\n"
16447 "\t\tsomeFunction( \\\n"
16448 "\t\t parameter1, \\\n"
16449 "\t\t parameter2); \\\n"
16453 Tab
.IndentWidth
= 8;
16454 verifyFormat("class TabWidth4Indent8 {\n"
16456 "\t\t\t\tsomeFunction(parameter1,\n"
16457 "\t\t\t\t parameter2);\n"
16462 Tab
.IndentWidth
= 4;
16463 verifyFormat("class TabWidth4Indent4 {\n"
16465 "\t\tsomeFunction(parameter1,\n"
16466 "\t\t parameter2);\n"
16471 Tab
.IndentWidth
= 4;
16472 verifyFormat("class TabWidth8Indent4 {\n"
16474 "\tsomeFunction(parameter1,\n"
16475 "\t parameter2);\n"
16480 Tab
.IndentWidth
= 8;
16481 verifyFormat("/*\n"
16483 " in multiple lines\n"
16486 " \t \t a\t\tcomment\t \t\n"
16487 " \t \t in multiple lines\t\n"
16491 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16492 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16493 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16494 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16495 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16496 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16499 verifyFormat("enum AA {\n"
16500 "\ta1, // Force multiple lines\n"
16505 verifyFormat("if (aaaaaaaa && // q\n"
16508 "if (aaaaaaaa &&// q\n"
16512 verifyFormat("class X {\n"
16514 "\t\tsomeFunction(parameter1,\n"
16515 "\t\t parameter2);\n"
16523 "\t\t someFunction(aaaaaaaa,\n"
16540 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16541 "\t bbbbbbbbbbbbb\n"
16546 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16551 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16552 "\t// bbbbbbbbbbbbb\n"
16555 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16560 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16561 "\t bbbbbbbbbbbbb\n"
16566 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16570 verifyNoChange("{\n"
16576 verifyNoChange("{\n"
16582 verifyFormat("/* some\n"
16585 " \t \t comment */",
16587 verifyFormat("int a; /* some\n"
16589 " \t \t int a; /* some\n"
16590 " \t \t comment */",
16592 verifyFormat("int a; /* some\n"
16594 " \t \t int\ta; /* some\n"
16595 " \t \t comment */",
16597 verifyFormat("f(\"\t\t\"); /* some\n"
16599 " \t \t f(\"\t\t\"); /* some\n"
16600 " \t \t comment */",
16616 Tab
.IndentWidth
= 2;
16628 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16629 "\t bbbbbbbbbbbbb\n"
16634 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16638 Tab
.AlignConsecutiveAssignments
.Enabled
= true;
16639 Tab
.AlignConsecutiveDeclarations
.Enabled
= true;
16641 Tab
.IndentWidth
= 4;
16642 verifyFormat("class Assign {\n"
16644 "\t\tint x = 123;\n"
16645 "\t\tint random = 4;\n"
16646 "\t\tstd::string alphabet =\n"
16647 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16651 Tab
.AlignOperands
= FormatStyle::OAS_Align
;
16652 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16653 " cccccccccccccccccccc;",
16656 verifyFormat("int aaaaaaaaaa =\n"
16657 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16659 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16660 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16661 " : 333333333333333;",
16663 Tab
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
16664 Tab
.AlignOperands
= FormatStyle::OAS_AlignAfterOperator
;
16665 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16666 " + cccccccccccccccccccc;",
16670 TEST_F(FormatTest
, ZeroTabWidth
) {
16671 FormatStyle Tab
= getLLVMStyleWithColumns(42);
16672 Tab
.IndentWidth
= 8;
16673 Tab
.UseTab
= FormatStyle::UT_Never
;
16675 verifyFormat("void a() {\n"
16676 " // line starts with '\t'\n"
16679 "\t// line starts with '\t'\n"
16683 verifyFormat("void a() {\n"
16684 " // line starts with '\t'\n"
16687 "\t\t// line starts with '\t'\n"
16691 Tab
.UseTab
= FormatStyle::UT_ForIndentation
;
16692 verifyFormat("void a() {\n"
16693 " // line starts with '\t'\n"
16696 "\t// line starts with '\t'\n"
16700 verifyFormat("void a() {\n"
16701 " // line starts with '\t'\n"
16704 "\t\t// line starts with '\t'\n"
16708 Tab
.UseTab
= FormatStyle::UT_ForContinuationAndIndentation
;
16709 verifyFormat("void a() {\n"
16710 " // line starts with '\t'\n"
16713 "\t// line starts with '\t'\n"
16717 verifyFormat("void a() {\n"
16718 " // line starts with '\t'\n"
16721 "\t\t// line starts with '\t'\n"
16725 Tab
.UseTab
= FormatStyle::UT_AlignWithSpaces
;
16726 verifyFormat("void a() {\n"
16727 " // line starts with '\t'\n"
16730 "\t// line starts with '\t'\n"
16734 verifyFormat("void a() {\n"
16735 " // line starts with '\t'\n"
16738 "\t\t// line starts with '\t'\n"
16742 Tab
.UseTab
= FormatStyle::UT_Always
;
16743 verifyFormat("void a() {\n"
16744 "// line starts with '\t'\n"
16747 "\t// line starts with '\t'\n"
16751 verifyFormat("void a() {\n"
16752 "// line starts with '\t'\n"
16755 "\t\t// line starts with '\t'\n"
16760 TEST_F(FormatTest
, CalculatesOriginalColumn
) {
16761 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16764 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16767 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16770 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16773 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16777 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16781 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16784 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16789 TEST_F(FormatTest
, ConfigurableSpaceBeforeParens
) {
16790 FormatStyle NoSpace
= getLLVMStyle();
16791 NoSpace
.SpaceBeforeParens
= FormatStyle::SBPO_Never
;
16793 verifyFormat("while(true)\n"
16796 verifyFormat("for(;;)\n"
16799 verifyFormat("if(true)\n"
16804 verifyFormat("do {\n"
16805 " do_something();\n"
16806 "} while(something());",
16808 verifyFormat("switch(x) {\n"
16813 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace
);
16814 verifyFormat("size_t x = sizeof(x);", NoSpace
);
16815 verifyFormat("auto f(int x) -> decltype(x);", NoSpace
);
16816 verifyFormat("auto f(int x) -> typeof(x);", NoSpace
);
16817 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace
);
16818 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace
);
16819 verifyFormat("int f(T x) noexcept(x.create());", NoSpace
);
16820 verifyFormat("alignas(128) char a[128];", NoSpace
);
16821 verifyFormat("size_t x = alignof(MyType);", NoSpace
);
16822 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace
);
16823 verifyFormat("int f() throw(Deprecated);", NoSpace
);
16824 verifyFormat("typedef void (*cb)(int);", NoSpace
);
16825 verifyFormat("T A::operator()();", NoSpace
);
16826 verifyFormat("X A::operator++(T);", NoSpace
);
16827 verifyFormat("auto lambda = []() { return 0; };", NoSpace
);
16828 verifyFormat("#if (foo || bar) && baz\n"
16829 "#elif ((a || b) && c) || d\n"
16833 FormatStyle Space
= getLLVMStyle();
16834 Space
.SpaceBeforeParens
= FormatStyle::SBPO_Always
;
16836 verifyFormat("int f ();", Space
);
16837 verifyFormat("bool operator< ();", Space
);
16838 verifyFormat("bool operator> ();", Space
);
16839 verifyFormat("void f (int a, T b) {\n"
16844 verifyFormat("if (true)\n"
16849 verifyFormat("do {\n"
16850 " do_something ();\n"
16851 "} while (something ());",
16853 verifyFormat("switch (x) {\n"
16858 verifyFormat("A::A () : a (1) {}", Space
);
16859 verifyFormat("void f () __attribute__ ((asdf));", Space
);
16860 verifyFormat("*(&a + 1);\n"
16862 "a[(b + c) * d];\n"
16863 "(((a + 1) * 2) + 3) * 4;",
16865 verifyFormat("#define A(x) x", Space
);
16866 verifyFormat("#define A (x) x", Space
);
16867 verifyFormat("#if defined(x)\n"
16870 verifyFormat("auto i = std::make_unique<int> (5);", Space
);
16871 verifyFormat("size_t x = sizeof (x);", Space
);
16872 verifyFormat("auto f (int x) -> decltype (x);", Space
);
16873 verifyFormat("auto f (int x) -> typeof (x);", Space
);
16874 verifyFormat("auto f (int x) -> _Atomic (x);", Space
);
16875 verifyFormat("auto f (int x) -> __underlying_type (x);", Space
);
16876 verifyFormat("int f (T x) noexcept (x.create ());", Space
);
16877 verifyFormat("alignas (128) char a[128];", Space
);
16878 verifyFormat("size_t x = alignof (MyType);", Space
);
16879 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space
);
16880 verifyFormat("int f () throw (Deprecated);", Space
);
16881 verifyFormat("typedef void (*cb) (int);", Space
);
16882 verifyFormat("T A::operator() ();", Space
);
16883 verifyFormat("X A::operator++ (T);", Space
);
16884 verifyFormat("auto lambda = [] () { return 0; };", Space
);
16885 verifyFormat("int x = int (y);", Space
);
16886 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space
);
16887 verifyFormat("__builtin_LINE ()", Space
);
16888 verifyFormat("__builtin_UNKNOWN ()", Space
);
16890 FormatStyle SomeSpace
= getLLVMStyle();
16891 SomeSpace
.SpaceBeforeParens
= FormatStyle::SBPO_NonEmptyParentheses
;
16893 verifyFormat("[]() -> float {}", SomeSpace
);
16894 verifyFormat("[] (auto foo) {}", SomeSpace
);
16895 verifyFormat("[foo]() -> int {}", SomeSpace
);
16896 verifyFormat("int f();", SomeSpace
);
16897 verifyFormat("void f (int a, T b) {\n"
16902 verifyFormat("if (true)\n"
16907 verifyFormat("do {\n"
16908 " do_something();\n"
16909 "} while (something());",
16911 verifyFormat("switch (x) {\n"
16916 verifyFormat("A::A() : a (1) {}", SomeSpace
);
16917 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace
);
16918 verifyFormat("*(&a + 1);\n"
16920 "a[(b + c) * d];\n"
16921 "(((a + 1) * 2) + 3) * 4;",
16923 verifyFormat("#define A(x) x", SomeSpace
);
16924 verifyFormat("#define A (x) x", SomeSpace
);
16925 verifyFormat("#if defined(x)\n"
16928 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace
);
16929 verifyFormat("size_t x = sizeof (x);", SomeSpace
);
16930 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace
);
16931 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace
);
16932 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace
);
16933 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace
);
16934 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace
);
16935 verifyFormat("alignas (128) char a[128];", SomeSpace
);
16936 verifyFormat("size_t x = alignof (MyType);", SomeSpace
);
16937 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16939 verifyFormat("int f() throw (Deprecated);", SomeSpace
);
16940 verifyFormat("typedef void (*cb) (int);", SomeSpace
);
16941 verifyFormat("T A::operator()();", SomeSpace
);
16942 verifyFormat("X A::operator++ (T);", SomeSpace
);
16943 verifyFormat("int x = int (y);", SomeSpace
);
16944 verifyFormat("auto lambda = []() { return 0; };", SomeSpace
);
16946 FormatStyle SpaceControlStatements
= getLLVMStyle();
16947 SpaceControlStatements
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16948 SpaceControlStatements
.SpaceBeforeParensOptions
.AfterControlStatements
= true;
16950 verifyFormat("while (true)\n"
16952 SpaceControlStatements
);
16953 verifyFormat("if (true)\n"
16957 SpaceControlStatements
);
16958 verifyFormat("for (;;) {\n"
16959 " do_something();\n"
16961 SpaceControlStatements
);
16962 verifyFormat("do {\n"
16963 " do_something();\n"
16964 "} while (something());",
16965 SpaceControlStatements
);
16966 verifyFormat("switch (x) {\n"
16970 SpaceControlStatements
);
16972 FormatStyle SpaceFuncDecl
= getLLVMStyle();
16973 SpaceFuncDecl
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16974 SpaceFuncDecl
.SpaceBeforeParensOptions
.AfterFunctionDeclarationName
= true;
16976 verifyFormat("int f ();", SpaceFuncDecl
);
16977 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl
);
16978 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl
);
16979 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl
);
16980 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl
);
16981 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl
);
16982 verifyFormat("#define A(x) x", SpaceFuncDecl
);
16983 verifyFormat("#define A (x) x", SpaceFuncDecl
);
16984 verifyFormat("#if defined(x)\n"
16987 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl
);
16988 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl
);
16989 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl
);
16990 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl
);
16991 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl
);
16992 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl
);
16993 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl
);
16994 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl
);
16995 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl
);
16996 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16998 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl
);
16999 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl
);
17000 verifyFormat("T A::operator()();", SpaceFuncDecl
);
17001 verifyFormat("X A::operator++(T);", SpaceFuncDecl
);
17002 verifyFormat("T A::operator()() {}", SpaceFuncDecl
);
17003 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl
);
17004 verifyFormat("int x = int(y);", SpaceFuncDecl
);
17005 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17008 FormatStyle SpaceFuncDef
= getLLVMStyle();
17009 SpaceFuncDef
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17010 SpaceFuncDef
.SpaceBeforeParensOptions
.AfterFunctionDefinitionName
= true;
17012 verifyFormat("int f();", SpaceFuncDef
);
17013 verifyFormat("void f (int a, T b) {}", SpaceFuncDef
);
17014 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef
);
17015 verifyFormat("A::A () : a(1) {}", SpaceFuncDef
);
17016 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef
);
17017 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef
);
17018 verifyFormat("#define A(x) x", SpaceFuncDef
);
17019 verifyFormat("#define A (x) x", SpaceFuncDef
);
17020 verifyFormat("#if defined(x)\n"
17023 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef
);
17024 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef
);
17025 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef
);
17026 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef
);
17027 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef
);
17028 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef
);
17029 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef
);
17030 verifyFormat("alignas(128) char a[128];", SpaceFuncDef
);
17031 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef
);
17032 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17034 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef
);
17035 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef
);
17036 verifyFormat("T A::operator()();", SpaceFuncDef
);
17037 verifyFormat("X A::operator++(T);", SpaceFuncDef
);
17038 verifyFormat("T A::operator()() {}", SpaceFuncDef
);
17039 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef
);
17040 verifyFormat("int x = int(y);", SpaceFuncDef
);
17041 verifyFormat("void foo::bar () {}", SpaceFuncDef
);
17042 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17045 FormatStyle SpaceIfMacros
= getLLVMStyle();
17046 SpaceIfMacros
.IfMacros
.clear();
17047 SpaceIfMacros
.IfMacros
.push_back("MYIF");
17048 SpaceIfMacros
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17049 SpaceIfMacros
.SpaceBeforeParensOptions
.AfterIfMacros
= true;
17050 verifyFormat("MYIF (a)\n return;", SpaceIfMacros
);
17051 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros
);
17052 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros
);
17054 FormatStyle SpaceForeachMacros
= getLLVMStyle();
17055 EXPECT_EQ(SpaceForeachMacros
.AllowShortBlocksOnASingleLine
,
17056 FormatStyle::SBS_Never
);
17057 EXPECT_EQ(SpaceForeachMacros
.AllowShortLoopsOnASingleLine
, false);
17058 SpaceForeachMacros
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17059 SpaceForeachMacros
.SpaceBeforeParensOptions
.AfterForeachMacros
= true;
17060 verifyFormat("for (;;) {\n"
17062 SpaceForeachMacros
);
17063 verifyFormat("foreach (Item *item, itemlist) {\n"
17065 SpaceForeachMacros
);
17066 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17068 SpaceForeachMacros
);
17069 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17071 SpaceForeachMacros
);
17072 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros
);
17074 FormatStyle SomeSpace2
= getLLVMStyle();
17075 SomeSpace2
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17076 SomeSpace2
.SpaceBeforeParensOptions
.BeforeNonEmptyParentheses
= true;
17077 verifyFormat("[]() -> float {}", SomeSpace2
);
17078 verifyFormat("[] (auto foo) {}", SomeSpace2
);
17079 verifyFormat("[foo]() -> int {}", SomeSpace2
);
17080 verifyFormat("int f();", SomeSpace2
);
17081 verifyFormat("void f (int a, T b) {\n"
17086 verifyFormat("if (true)\n"
17091 verifyFormat("do {\n"
17092 " do_something();\n"
17093 "} while (something());",
17095 verifyFormat("switch (x) {\n"
17100 verifyFormat("A::A() : a (1) {}", SomeSpace2
);
17101 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2
);
17102 verifyFormat("*(&a + 1);\n"
17104 "a[(b + c) * d];\n"
17105 "(((a + 1) * 2) + 3) * 4;",
17107 verifyFormat("#define A(x) x", SomeSpace2
);
17108 verifyFormat("#define A (x) x", SomeSpace2
);
17109 verifyFormat("#if defined(x)\n"
17112 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2
);
17113 verifyFormat("size_t x = sizeof (x);", SomeSpace2
);
17114 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2
);
17115 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2
);
17116 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2
);
17117 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2
);
17118 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2
);
17119 verifyFormat("alignas (128) char a[128];", SomeSpace2
);
17120 verifyFormat("size_t x = alignof (MyType);", SomeSpace2
);
17121 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17123 verifyFormat("int f() throw (Deprecated);", SomeSpace2
);
17124 verifyFormat("typedef void (*cb) (int);", SomeSpace2
);
17125 verifyFormat("T A::operator()();", SomeSpace2
);
17126 verifyFormat("X A::operator++ (T);", SomeSpace2
);
17127 verifyFormat("int x = int (y);", SomeSpace2
);
17128 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2
);
17130 FormatStyle SpaceAfterOverloadedOperator
= getLLVMStyle();
17131 SpaceAfterOverloadedOperator
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17132 SpaceAfterOverloadedOperator
.SpaceBeforeParensOptions
17133 .AfterOverloadedOperator
= true;
17135 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator
);
17136 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator
);
17137 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator
);
17138 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator
);
17140 SpaceAfterOverloadedOperator
.SpaceBeforeParensOptions
17141 .AfterOverloadedOperator
= false;
17143 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator
);
17144 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator
);
17145 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator
);
17146 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator
);
17148 auto SpaceAfterRequires
= getLLVMStyle();
17149 SpaceAfterRequires
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17151 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
);
17153 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInExpression
);
17154 verifyFormat("void f(auto x)\n"
17155 " requires requires(int i) { x + i; }\n"
17157 SpaceAfterRequires
);
17158 verifyFormat("void f(auto x)\n"
17159 " requires(requires(int i) { x + i; })\n"
17161 SpaceAfterRequires
);
17162 verifyFormat("if (requires(int i) { x + i; })\n"
17164 SpaceAfterRequires
);
17165 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires
);
17166 verifyFormat("template <typename T>\n"
17167 " requires(Foo<T>)\n"
17169 SpaceAfterRequires
);
17171 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= true;
17172 verifyFormat("void f(auto x)\n"
17173 " requires requires(int i) { x + i; }\n"
17175 SpaceAfterRequires
);
17176 verifyFormat("void f(auto x)\n"
17177 " requires (requires(int i) { x + i; })\n"
17179 SpaceAfterRequires
);
17180 verifyFormat("if (requires(int i) { x + i; })\n"
17182 SpaceAfterRequires
);
17183 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires
);
17184 verifyFormat("template <typename T>\n"
17185 " requires (Foo<T>)\n"
17187 SpaceAfterRequires
);
17189 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= false;
17190 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInExpression
= true;
17191 verifyFormat("void f(auto x)\n"
17192 " requires requires (int i) { x + i; }\n"
17194 SpaceAfterRequires
);
17195 verifyFormat("void f(auto x)\n"
17196 " requires(requires (int i) { x + i; })\n"
17198 SpaceAfterRequires
);
17199 verifyFormat("if (requires (int i) { x + i; })\n"
17201 SpaceAfterRequires
);
17202 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires
);
17203 verifyFormat("template <typename T>\n"
17204 " requires(Foo<T>)\n"
17206 SpaceAfterRequires
);
17208 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= true;
17209 verifyFormat("void f(auto x)\n"
17210 " requires requires (int i) { x + i; }\n"
17212 SpaceAfterRequires
);
17213 verifyFormat("void f(auto x)\n"
17214 " requires (requires (int i) { x + i; })\n"
17216 SpaceAfterRequires
);
17217 verifyFormat("if (requires (int i) { x + i; })\n"
17219 SpaceAfterRequires
);
17220 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires
);
17221 verifyFormat("template <typename T>\n"
17222 " requires (Foo<T>)\n"
17224 SpaceAfterRequires
);
17227 TEST_F(FormatTest
, SpaceAfterLogicalNot
) {
17228 FormatStyle Spaces
= getLLVMStyle();
17229 Spaces
.SpaceAfterLogicalNot
= true;
17231 verifyFormat("bool x = ! y", Spaces
);
17232 verifyFormat("if (! isFailure())", Spaces
);
17233 verifyFormat("if (! (a && b))", Spaces
);
17234 verifyFormat("\"Error!\"", Spaces
);
17235 verifyFormat("! ! x", Spaces
);
17238 TEST_F(FormatTest
, ConfigurableSpacesInParens
) {
17239 FormatStyle Spaces
= getLLVMStyle();
17241 verifyFormat("do_something(::globalVar);", Spaces
);
17242 verifyFormat("call(x, y, z);", Spaces
);
17243 verifyFormat("call();", Spaces
);
17244 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
17245 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17247 verifyFormat("while ((bool)1)\n"
17250 verifyFormat("for (;;)\n"
17253 verifyFormat("if (true)\n"
17258 verifyFormat("do {\n"
17259 " do_something((int)i);\n"
17260 "} while (something());",
17262 verifyFormat("switch (x) {\n"
17267 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17268 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17269 verifyFormat("void f() __attribute__((asdf));", Spaces
);
17270 verifyFormat("x = (int32)y;", Spaces
);
17271 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces
);
17272 verifyFormat("decltype(x) y = 42;", Spaces
);
17273 verifyFormat("decltype((x)) y = z;", Spaces
);
17274 verifyFormat("decltype((foo())) a = foo();", Spaces
);
17275 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces
);
17276 verifyFormat("if ((x - y) && (a ^ b))\n"
17279 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17282 verifyFormat("switch (x / (y + z)) {\n"
17288 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17289 Spaces
.SpacesInParensOptions
= {};
17290 Spaces
.SpacesInParensOptions
.Other
= true;
17292 EXPECT_FALSE(Spaces
.SpacesInParensOptions
.InConditionalStatements
);
17293 verifyFormat("if (a)\n"
17297 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
17298 verifyFormat("do_something( ::globalVar );", Spaces
);
17299 verifyFormat("call( x, y, z );", Spaces
);
17300 verifyFormat("call();", Spaces
);
17301 verifyFormat("std::function<void( int, int )> callback;", Spaces
);
17302 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17304 verifyFormat("while ( (bool)1 )\n"
17307 verifyFormat("for ( ;; )\n"
17310 verifyFormat("if ( true )\n"
17312 "else if ( true )\n"
17315 verifyFormat("do {\n"
17316 " do_something( (int)i );\n"
17317 "} while ( something() );",
17319 verifyFormat("switch ( x ) {\n"
17324 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces
);
17325 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces
);
17326 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces
);
17327 verifyFormat("x = (int32)y;", Spaces
);
17328 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces
);
17329 verifyFormat("decltype( x ) y = 42;", Spaces
);
17330 verifyFormat("decltype( ( x ) ) y = z;", Spaces
);
17331 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces
);
17332 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces
);
17333 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17336 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17339 verifyFormat("switch ( x / ( y + z ) ) {\n"
17345 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17346 Spaces
.SpacesInParensOptions
= {};
17347 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
17348 verifyFormat("Type *A = ( Type * )P;", Spaces
);
17349 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces
);
17350 verifyFormat("x = ( int32 )y;", Spaces
);
17351 verifyFormat("throw ( int32 )x;", Spaces
);
17352 verifyFormat("int a = ( int )(2.0f);", Spaces
);
17353 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces
);
17354 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces
);
17355 verifyFormat("#define x (( int )-1)", Spaces
);
17356 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces
);
17358 // Run the first set of tests again with:
17359 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17360 Spaces
.SpacesInParensOptions
= {};
17361 Spaces
.SpacesInParensOptions
.InEmptyParentheses
= true;
17362 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
17363 verifyFormat("call(x, y, z);", Spaces
);
17364 verifyFormat("call( );", Spaces
);
17365 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
17366 verifyFormat("while (( bool )1)\n"
17369 verifyFormat("for (;;)\n"
17372 verifyFormat("if (true)\n"
17377 verifyFormat("do {\n"
17378 " do_something(( int )i);\n"
17379 "} while (something( ));",
17381 verifyFormat("switch (x) {\n"
17386 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17387 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17388 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
17389 verifyFormat("x = ( int32 )y;", Spaces
);
17390 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces
);
17391 verifyFormat("decltype(x) y = 42;", Spaces
);
17392 verifyFormat("decltype((x)) y = z;", Spaces
);
17393 verifyFormat("decltype((foo( ))) a = foo( );", Spaces
);
17394 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces
);
17395 verifyFormat("if ((x - y) && (a ^ b))\n"
17398 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17401 verifyFormat("switch (x / (y + z)) {\n"
17407 // Run the first set of tests again with:
17408 Spaces
.SpaceAfterCStyleCast
= true;
17409 verifyFormat("call(x, y, z);", Spaces
);
17410 verifyFormat("call( );", Spaces
);
17411 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
17412 verifyFormat("while (( bool ) 1)\n"
17415 verifyFormat("for (;;)\n"
17418 verifyFormat("if (true)\n"
17423 verifyFormat("do {\n"
17424 " do_something(( int ) i);\n"
17425 "} while (something( ));",
17427 verifyFormat("switch (x) {\n"
17432 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces
);
17433 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces
);
17434 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces
);
17435 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces
);
17436 verifyFormat("bool *y = ( bool * ) (x);", Spaces
);
17437 verifyFormat("throw ( int32 ) x;", Spaces
);
17438 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17439 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17440 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
17442 // Run subset of tests again with:
17443 Spaces
.SpacesInParensOptions
.InCStyleCasts
= false;
17444 Spaces
.SpaceAfterCStyleCast
= true;
17445 verifyFormat("while ((bool) 1)\n"
17448 verifyFormat("do {\n"
17449 " do_something((int) i);\n"
17450 "} while (something( ));",
17453 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces
);
17454 verifyFormat("size_t idx = (size_t) a;", Spaces
);
17455 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces
);
17456 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
17457 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
17458 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
17459 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
17460 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces
);
17461 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces
);
17462 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces
);
17463 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces
);
17464 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces
);
17465 verifyFormat("throw (int32) x;", Spaces
);
17466 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17467 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17468 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
17470 Spaces
.ColumnLimit
= 80;
17471 Spaces
.IndentWidth
= 4;
17472 Spaces
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
17473 verifyFormat("void foo( ) {\n"
17474 " size_t foo = (*(function))(\n"
17475 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17476 "BarrrrrrrrrrrrLong,\n"
17477 " FoooooooooLooooong);\n"
17480 Spaces
.SpaceAfterCStyleCast
= false;
17481 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces
);
17482 verifyFormat("size_t idx = (size_t)a;", Spaces
);
17483 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces
);
17484 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
17485 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
17486 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
17487 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
17489 verifyFormat("void foo( ) {\n"
17490 " size_t foo = (*(function))(\n"
17491 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17492 "BarrrrrrrrrrrrLong,\n"
17493 " FoooooooooLooooong);\n"
17497 Spaces
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
17498 verifyFormat("void foo( ) {\n"
17499 " size_t foo = (*(function))(\n"
17500 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17501 "BarrrrrrrrrrrrLong,\n"
17502 " FoooooooooLooooong\n"
17506 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces
);
17507 verifyFormat("size_t idx = (size_t)a;", Spaces
);
17508 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces
);
17509 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
17510 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
17511 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
17512 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
17514 // Check ExceptDoubleParentheses spaces
17515 Spaces
.IndentWidth
= 2;
17516 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17517 Spaces
.SpacesInParensOptions
= {};
17518 Spaces
.SpacesInParensOptions
.Other
= true;
17519 Spaces
.SpacesInParensOptions
.ExceptDoubleParentheses
= true;
17520 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces
);
17521 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces
);
17522 verifyFormat("void f() __attribute__(( asdf ));", Spaces
);
17523 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces
);
17524 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces
);
17525 verifyFormat("class __declspec( dllimport ) X {};", Spaces
);
17526 verifyFormat("class __declspec(( dllimport )) X {};", Spaces
);
17527 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces
);
17528 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces
);
17529 verifyFormat("decltype( x ) y = 42;", Spaces
);
17530 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces
);
17531 verifyFormat("if (( i = j ))\n"
17532 " do_something( i );",
17535 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17536 Spaces
.SpacesInParensOptions
= {};
17537 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
17538 Spaces
.SpacesInParensOptions
.ExceptDoubleParentheses
= true;
17539 verifyFormat("while ( (bool)1 )\n"
17542 verifyFormat("while ((i = j))\n"
17545 verifyFormat("do {\n"
17546 " do_something((int)i);\n"
17547 "} while ( something() );",
17549 verifyFormat("do {\n"
17550 " do_something((int)i);\n"
17551 "} while ((i = i + 1));",
17553 verifyFormat("if ( (x - y) && (a ^ b) )\n"
17556 verifyFormat("if ((i = j))\n"
17557 " do_something(i);",
17559 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
17562 verifyFormat("switch ( x / (y + z) ) {\n"
17567 verifyFormat("if constexpr ((a = b))\n"
17572 TEST_F(FormatTest
, ConfigurableSpacesInSquareBrackets
) {
17573 verifyFormat("int a[5];");
17574 verifyFormat("a[3] += 42;");
17576 FormatStyle Spaces
= getLLVMStyle();
17577 Spaces
.SpacesInSquareBrackets
= true;
17579 verifyFormat("int a[ 5 ];", Spaces
);
17580 verifyFormat("a[ 3 ] += 42;", Spaces
);
17581 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces
);
17582 verifyFormat("double &operator[](int i) { return 0; }\n"
17585 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces
);
17586 verifyFormat("int i = a[ a ][ a ]->f();", Spaces
);
17587 verifyFormat("int i = (*b)[ a ]->f();", Spaces
);
17589 verifyFormat("int c = []() -> int { return 2; }();", Spaces
);
17590 verifyFormat("return [ i, args... ] {};", Spaces
);
17591 verifyFormat("int foo = [ &bar ]() {};", Spaces
);
17592 verifyFormat("int foo = [ = ]() {};", Spaces
);
17593 verifyFormat("int foo = [ & ]() {};", Spaces
);
17594 verifyFormat("int foo = [ =, &bar ]() {};", Spaces
);
17595 verifyFormat("int foo = [ &bar, = ]() {};", Spaces
);
17598 TEST_F(FormatTest
, ConfigurableSpaceBeforeBrackets
) {
17599 FormatStyle NoSpaceStyle
= getLLVMStyle();
17600 verifyFormat("int a[5];", NoSpaceStyle
);
17601 verifyFormat("a[3] += 42;", NoSpaceStyle
);
17603 verifyFormat("int a[1];", NoSpaceStyle
);
17604 verifyFormat("int 1 [a];", NoSpaceStyle
);
17605 verifyFormat("int a[1][2];", NoSpaceStyle
);
17606 verifyFormat("a[7] = 5;", NoSpaceStyle
);
17607 verifyFormat("int a = (f())[23];", NoSpaceStyle
);
17608 verifyFormat("f([] {})", NoSpaceStyle
);
17610 FormatStyle Space
= getLLVMStyle();
17611 Space
.SpaceBeforeSquareBrackets
= true;
17612 verifyFormat("int c = []() -> int { return 2; }();", Space
);
17613 verifyFormat("return [i, args...] {};", Space
);
17615 verifyFormat("int a [5];", Space
);
17616 verifyFormat("a [3] += 42;", Space
);
17617 verifyFormat("constexpr char hello []{\"hello\"};", Space
);
17618 verifyFormat("double &operator[](int i) { return 0; }\n"
17621 verifyFormat("std::unique_ptr<int []> foo() {}", Space
);
17622 verifyFormat("int i = a [a][a]->f();", Space
);
17623 verifyFormat("int i = (*b) [a]->f();", Space
);
17625 verifyFormat("int a [1];", Space
);
17626 verifyFormat("int 1 [a];", Space
);
17627 verifyFormat("int a [1][2];", Space
);
17628 verifyFormat("a [7] = 5;", Space
);
17629 verifyFormat("int a = (f()) [23];", Space
);
17630 verifyFormat("f([] {})", Space
);
17633 TEST_F(FormatTest
, ConfigurableSpaceBeforeAssignmentOperators
) {
17634 verifyFormat("int a = 5;");
17635 verifyFormat("a += 42;");
17636 verifyFormat("a or_eq 8;");
17637 verifyFormat("xor = foo;");
17639 FormatStyle Spaces
= getLLVMStyle();
17640 Spaces
.SpaceBeforeAssignmentOperators
= false;
17641 verifyFormat("int a= 5;", Spaces
);
17642 verifyFormat("a+= 42;", Spaces
);
17643 verifyFormat("a or_eq 8;", Spaces
);
17644 verifyFormat("xor= foo;", Spaces
);
17647 TEST_F(FormatTest
, ConfigurableSpaceBeforeColon
) {
17648 verifyFormat("class Foo : public Bar {};");
17649 verifyFormat("Foo::Foo() : foo(1) {}");
17650 verifyFormat("for (auto a : b) {\n}");
17651 verifyFormat("int x = a ? b : c;");
17656 verifyFormat("switch (x) {\n"
17660 verifyFormat("switch (allBraces) {\n"
17665 " [[fallthrough]];\n"
17672 FormatStyle CtorInitializerStyle
= getLLVMStyleWithColumns(30);
17673 CtorInitializerStyle
.SpaceBeforeCtorInitializerColon
= false;
17674 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle
);
17675 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle
);
17676 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle
);
17677 verifyFormat("int x = a ? b : c;", CtorInitializerStyle
);
17682 CtorInitializerStyle
);
17683 verifyFormat("switch (x) {\n"
17687 CtorInitializerStyle
);
17688 verifyFormat("switch (allBraces) {\n"
17693 " [[fallthrough]];\n"
17699 CtorInitializerStyle
);
17700 CtorInitializerStyle
.BreakConstructorInitializers
=
17701 FormatStyle::BCIS_AfterColon
;
17702 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17703 " aaaaaaaaaaaaaaaa(1),\n"
17704 " bbbbbbbbbbbbbbbb(2) {}",
17705 CtorInitializerStyle
);
17706 CtorInitializerStyle
.BreakConstructorInitializers
=
17707 FormatStyle::BCIS_BeforeComma
;
17708 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17709 " : aaaaaaaaaaaaaaaa(1)\n"
17710 " , bbbbbbbbbbbbbbbb(2) {}",
17711 CtorInitializerStyle
);
17712 CtorInitializerStyle
.BreakConstructorInitializers
=
17713 FormatStyle::BCIS_BeforeColon
;
17714 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17715 " : aaaaaaaaaaaaaaaa(1),\n"
17716 " bbbbbbbbbbbbbbbb(2) {}",
17717 CtorInitializerStyle
);
17718 CtorInitializerStyle
.ConstructorInitializerIndentWidth
= 0;
17719 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17720 ": aaaaaaaaaaaaaaaa(1),\n"
17721 " bbbbbbbbbbbbbbbb(2) {}",
17722 CtorInitializerStyle
);
17724 FormatStyle InheritanceStyle
= getLLVMStyleWithColumns(30);
17725 InheritanceStyle
.SpaceBeforeInheritanceColon
= false;
17726 verifyFormat("class Foo: public Bar {};", InheritanceStyle
);
17727 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle
);
17728 verifyFormat("for (auto a : b) {\n}", InheritanceStyle
);
17729 verifyFormat("int x = a ? b : c;", InheritanceStyle
);
17735 verifyFormat("switch (x) {\n"
17740 verifyFormat("switch (allBraces) {\n"
17745 " [[fallthrough]];\n"
17752 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_AfterComma
;
17753 verifyFormat("class Foooooooooooooooooooooo\n"
17754 " : public aaaaaaaaaaaaaaaaaa,\n"
17755 " public bbbbbbbbbbbbbbbbbb {\n"
17758 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_AfterColon
;
17759 verifyFormat("class Foooooooooooooooooooooo:\n"
17760 " public aaaaaaaaaaaaaaaaaa,\n"
17761 " public bbbbbbbbbbbbbbbbbb {\n"
17764 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_BeforeComma
;
17765 verifyFormat("class Foooooooooooooooooooooo\n"
17766 " : public aaaaaaaaaaaaaaaaaa\n"
17767 " , public bbbbbbbbbbbbbbbbbb {\n"
17770 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_BeforeColon
;
17771 verifyFormat("class Foooooooooooooooooooooo\n"
17772 " : public aaaaaaaaaaaaaaaaaa,\n"
17773 " public bbbbbbbbbbbbbbbbbb {\n"
17776 InheritanceStyle
.ConstructorInitializerIndentWidth
= 0;
17777 verifyFormat("class Foooooooooooooooooooooo\n"
17778 ": public aaaaaaaaaaaaaaaaaa,\n"
17779 " public bbbbbbbbbbbbbbbbbb {}",
17782 FormatStyle ForLoopStyle
= getLLVMStyle();
17783 ForLoopStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17784 verifyFormat("class Foo : public Bar {};", ForLoopStyle
);
17785 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle
);
17786 verifyFormat("for (auto a: b) {\n}", ForLoopStyle
);
17787 verifyFormat("int x = a ? b : c;", ForLoopStyle
);
17793 verifyFormat("switch (x) {\n"
17798 verifyFormat("switch (allBraces) {\n"
17803 " [[fallthrough]];\n"
17811 FormatStyle CaseStyle
= getLLVMStyle();
17812 CaseStyle
.SpaceBeforeCaseColon
= true;
17813 verifyFormat("class Foo : public Bar {};", CaseStyle
);
17814 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle
);
17815 verifyFormat("for (auto a : b) {\n}", CaseStyle
);
17816 verifyFormat("int x = a ? b : c;", CaseStyle
);
17817 verifyFormat("switch (x) {\n"
17822 verifyFormat("switch (allBraces) {\n"
17827 " [[fallthrough]];\n"
17834 // Goto labels should not be affected.
17835 verifyFormat("switch (x) {\n"
17840 verifyFormat("switch (x) {\n"
17841 "goto_label: { break; }\n"
17848 FormatStyle NoSpaceStyle
= getLLVMStyle();
17849 EXPECT_EQ(NoSpaceStyle
.SpaceBeforeCaseColon
, false);
17850 NoSpaceStyle
.SpaceBeforeCtorInitializerColon
= false;
17851 NoSpaceStyle
.SpaceBeforeInheritanceColon
= false;
17852 NoSpaceStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17853 verifyFormat("class Foo: public Bar {};", NoSpaceStyle
);
17854 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle
);
17855 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle
);
17856 verifyFormat("int x = a ? b : c;", NoSpaceStyle
);
17862 verifyFormat("switch (x) {\n"
17867 verifyFormat("switch (allBraces) {\n"
17872 " [[fallthrough]];\n"
17880 FormatStyle InvertedSpaceStyle
= getLLVMStyle();
17881 InvertedSpaceStyle
.SpaceBeforeCaseColon
= true;
17882 InvertedSpaceStyle
.SpaceBeforeCtorInitializerColon
= false;
17883 InvertedSpaceStyle
.SpaceBeforeInheritanceColon
= false;
17884 InvertedSpaceStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17885 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle
);
17886 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle
);
17887 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle
);
17888 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle
);
17893 InvertedSpaceStyle
);
17894 verifyFormat("switch (x) {\n"
17902 InvertedSpaceStyle
);
17903 verifyFormat("switch (allBraces) {\n"
17908 " [[fallthrough]];\n"
17914 InvertedSpaceStyle
);
17917 TEST_F(FormatTest
, ConfigurableSpaceAroundPointerQualifiers
) {
17918 FormatStyle Style
= getLLVMStyle();
17920 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
17921 Style
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Default
;
17922 verifyFormat("void* const* x = NULL;", Style
);
17924 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17926 Style.PointerAlignment = FormatStyle::Pointers; \
17927 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17928 verifyFormat(Code, Style); \
17931 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left
, SAPQ_Default
);
17932 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right
, SAPQ_Default
);
17933 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Default
);
17935 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left
, SAPQ_Before
);
17936 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right
, SAPQ_Before
);
17937 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Before
);
17939 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left
, SAPQ_After
);
17940 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right
, SAPQ_After
);
17941 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_After
);
17943 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left
, SAPQ_Both
);
17944 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right
, SAPQ_Both
);
17945 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Both
);
17947 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left
, SAPQ_Default
);
17948 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
,
17950 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17953 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left
, SAPQ_Before
);
17954 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
,
17956 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17959 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left
, SAPQ_After
);
17960 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
, SAPQ_After
);
17961 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17964 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left
, SAPQ_Both
);
17965 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
, SAPQ_Both
);
17966 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
, SAPQ_Both
);
17968 #undef verifyQualifierSpaces
17970 FormatStyle Spaces
= getLLVMStyle();
17971 Spaces
.AttributeMacros
.push_back("qualified");
17972 Spaces
.PointerAlignment
= FormatStyle::PAS_Right
;
17973 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Default
;
17974 verifyFormat("SomeType *volatile *a = NULL;", Spaces
);
17975 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17976 verifyFormat("std::vector<SomeType *const *> x;", Spaces
);
17977 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces
);
17978 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17979 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Before
;
17980 verifyFormat("SomeType * volatile *a = NULL;", Spaces
);
17981 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces
);
17982 verifyFormat("std::vector<SomeType * const *> x;", Spaces
);
17983 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces
);
17984 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17986 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17987 Spaces
.PointerAlignment
= FormatStyle::PAS_Left
;
17988 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Before
;
17989 verifyFormat("SomeType* volatile* a = NULL;", Spaces
);
17990 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces
);
17991 verifyFormat("std::vector<SomeType* const*> x;", Spaces
);
17992 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces
);
17993 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17994 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17995 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_After
;
17996 verifyFormat("SomeType* volatile * a = NULL;", Spaces
);
17997 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces
);
17998 verifyFormat("std::vector<SomeType* const *> x;", Spaces
);
17999 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces
);
18000 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18002 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18003 Spaces
.PointerAlignment
= FormatStyle::PAS_Middle
;
18004 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_After
;
18005 verifyFormat("SomeType * volatile * a = NULL;", Spaces
);
18006 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces
);
18007 verifyFormat("std::vector<SomeType * const *> x;", Spaces
);
18008 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces
);
18009 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18012 TEST_F(FormatTest
, AlignConsecutiveMacros
) {
18013 FormatStyle Style
= getLLVMStyle();
18014 Style
.AlignConsecutiveAssignments
.Enabled
= true;
18015 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
18017 verifyFormat("#define a 3\n"
18022 verifyFormat("#define f(x) (x * x)\n"
18023 "#define fff(x, y, z) (x * y + z)\n"
18024 "#define ffff(x, y) (x - y)",
18027 verifyFormat("#define foo(x, y) (x + y)\n"
18028 "#define bar (5, 6)(2 + 2)",
18031 verifyFormat("#define a 3\n"
18033 "#define ccc (5)\n"
18034 "#define f(x) (x * x)\n"
18035 "#define fff(x, y, z) (x * y + z)\n"
18036 "#define ffff(x, y) (x - y)",
18039 Style
.AlignConsecutiveMacros
.Enabled
= true;
18040 verifyFormat("#define a 3\n"
18045 verifyFormat("#define true 1\n"
18049 verifyFormat("#define f(x) (x * x)\n"
18050 "#define fff(x, y, z) (x * y + z)\n"
18051 "#define ffff(x, y) (x - y)",
18054 verifyFormat("#define foo(x, y) (x + y)\n"
18055 "#define bar (5, 6)(2 + 2)",
18058 verifyFormat("#define a 3\n"
18060 "#define ccc (5)\n"
18061 "#define f(x) (x * x)\n"
18062 "#define fff(x, y, z) (x * y + z)\n"
18063 "#define ffff(x, y) (x - y)",
18066 verifyFormat("#define a 5\n"
18067 "#define foo(x, y) (x + y)\n"
18068 "#define CCC (6)\n"
18069 "auto lambda = []() {\n"
18083 Style
.AlignConsecutiveMacros
.Enabled
= false;
18084 Style
.ColumnLimit
= 20;
18086 verifyFormat("#define a \\\n"
18087 " \"aabbbbbbbbbbbb\"\n"
18089 " \"aabbbbbbbbbbbb\" \\\n"
18090 " \"ccddeeeeeeeee\"\n"
18092 " \"QQQQQQQQQQQQQ\" \\\n"
18093 " \"FFFFFFFFFFFFF\" \\\n"
18097 Style
.AlignConsecutiveMacros
.Enabled
= true;
18098 verifyFormat("#define a \\\n"
18099 " \"aabbbbbbbbbbbb\"\n"
18101 " \"aabbbbbbbbbbbb\" \\\n"
18102 " \"ccddeeeeeeeee\"\n"
18104 " \"QQQQQQQQQQQQQ\" \\\n"
18105 " \"FFFFFFFFFFFFF\" \\\n"
18109 // Test across comments
18110 Style
.MaxEmptyLinesToKeep
= 10;
18111 Style
.ReflowComments
= FormatStyle::RCS_Never
;
18112 Style
.AlignConsecutiveMacros
.AcrossComments
= true;
18113 verifyFormat("#define a 3\n"
18114 "// line comment\n"
18118 "// line comment\n"
18123 verifyFormat("#define a 3\n"
18124 "/* block comment */\n"
18128 "/* block comment */\n"
18133 verifyFormat("#define a 3\n"
18134 "/* multi-line *\n"
18135 " * block comment */\n"
18139 "/* multi-line *\n"
18140 " * block comment */\n"
18145 verifyFormat("#define a 3\n"
18146 "// multi-line line comment\n"
18151 "// multi-line line comment\n"
18157 verifyFormat("#define a 3\n"
18158 "// empty lines still break.\n"
18163 "// empty lines still break.\n"
18169 // Test across empty lines
18170 Style
.AlignConsecutiveMacros
.AcrossComments
= false;
18171 Style
.AlignConsecutiveMacros
.AcrossEmptyLines
= true;
18172 verifyFormat("#define a 3\n"
18182 verifyFormat("#define a 3\n"
18196 verifyFormat("#define a 3\n"
18197 "// comments should break alignment\n"
18202 "// comments should break alignment\n"
18208 // Test across empty lines and comments
18209 Style
.AlignConsecutiveMacros
.AcrossComments
= true;
18210 verifyFormat("#define a 3\n"
18212 "// line comment\n"
18217 verifyFormat("#define a 3\n"
18220 "/* multi-line *\n"
18221 " * block comment */\n"
18229 "/* multi-line *\n"
18230 " * block comment */\n"
18237 verifyFormat("#define a 3\n"
18240 "/* multi-line *\n"
18241 " * block comment */\n"
18249 "/* multi-line *\n"
18250 " * block comment */\n"
18258 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossEmptyLines
) {
18259 FormatStyle Alignment
= getLLVMStyle();
18260 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18261 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18262 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18264 Alignment
.MaxEmptyLinesToKeep
= 10;
18265 /* Test alignment across empty lines */
18266 verifyFormat("int a = 5;\n"
18268 "int oneTwoThree = 123;",
18271 "int oneTwoThree= 123;",
18273 verifyFormat("int a = 5;\n"
18276 "int oneTwoThree = 123;",
18280 "int oneTwoThree = 123;",
18282 verifyFormat("int a = 5;\n"
18285 "int oneTwoThree = 123;\n"
18286 "int oneTwo = 12;",
18290 "int oneTwoThree = 123;\n"
18291 "int oneTwo = 12;",
18294 /* Test across comments */
18295 verifyFormat("int a = 5;\n"
18296 "/* block comment */\n"
18297 "int oneTwoThree = 123;",
18299 "/* block comment */\n"
18300 "int oneTwoThree=123;",
18303 verifyFormat("int a = 5;\n"
18304 "// line comment\n"
18305 "int oneTwoThree = 123;",
18307 "// line comment\n"
18308 "int oneTwoThree=123;",
18311 /* Test across comments and newlines */
18312 verifyFormat("int a = 5;\n"
18314 "/* block comment */\n"
18315 "int oneTwoThree = 123;",
18318 "/* block comment */\n"
18319 "int oneTwoThree=123;",
18322 verifyFormat("int a = 5;\n"
18324 "// line comment\n"
18325 "int oneTwoThree = 123;",
18328 "// line comment\n"
18329 "int oneTwoThree=123;",
18333 TEST_F(FormatTest
, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments
) {
18334 FormatStyle Alignment
= getLLVMStyle();
18335 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
18336 Alignment
.AlignConsecutiveDeclarations
.AcrossEmptyLines
= true;
18337 Alignment
.AlignConsecutiveDeclarations
.AcrossComments
= true;
18339 Alignment
.MaxEmptyLinesToKeep
= 10;
18340 /* Test alignment across empty lines */
18341 verifyFormat("int a = 5;\n"
18343 "float const oneTwoThree = 123;",
18346 "float const oneTwoThree = 123;",
18348 verifyFormat("int a = 5;\n"
18349 "float const one = 1;\n"
18351 "int oneTwoThree = 123;",
18353 "float const one = 1;\n"
18355 "int oneTwoThree = 123;",
18358 /* Test across comments */
18359 verifyFormat("float const a = 5;\n"
18360 "/* block comment */\n"
18361 "int oneTwoThree = 123;",
18362 "float const a = 5;\n"
18363 "/* block comment */\n"
18364 "int oneTwoThree=123;",
18367 verifyFormat("float const a = 5;\n"
18368 "// line comment\n"
18369 "int oneTwoThree = 123;",
18370 "float const a = 5;\n"
18371 "// line comment\n"
18372 "int oneTwoThree=123;",
18375 /* Test across comments and newlines */
18376 verifyFormat("float const a = 5;\n"
18378 "/* block comment */\n"
18379 "int oneTwoThree = 123;",
18380 "float const a = 5;\n"
18382 "/* block comment */\n"
18383 "int oneTwoThree=123;",
18386 verifyFormat("float const a = 5;\n"
18388 "// line comment\n"
18389 "int oneTwoThree = 123;",
18390 "float const a = 5;\n"
18392 "// line comment\n"
18393 "int oneTwoThree=123;",
18397 TEST_F(FormatTest
, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments
) {
18398 FormatStyle Alignment
= getLLVMStyle();
18399 Alignment
.AlignConsecutiveBitFields
.Enabled
= true;
18400 Alignment
.AlignConsecutiveBitFields
.AcrossEmptyLines
= true;
18401 Alignment
.AlignConsecutiveBitFields
.AcrossComments
= true;
18403 Alignment
.MaxEmptyLinesToKeep
= 10;
18404 /* Test alignment across empty lines */
18405 verifyFormat("int a : 5;\n"
18407 "int longbitfield : 6;",
18410 "int longbitfield : 6;",
18412 verifyFormat("int a : 5;\n"
18415 "int longbitfield : 6;",
18419 "int longbitfield : 6;",
18422 /* Test across comments */
18423 verifyFormat("int a : 5;\n"
18424 "/* block comment */\n"
18425 "int longbitfield : 6;",
18427 "/* block comment */\n"
18428 "int longbitfield : 6;",
18430 verifyFormat("int a : 5;\n"
18432 "// line comment\n"
18433 "int longbitfield : 6;",
18436 "// line comment\n"
18437 "int longbitfield : 6;",
18440 /* Test across comments and newlines */
18441 verifyFormat("int a : 5;\n"
18442 "/* block comment */\n"
18444 "int longbitfield : 6;",
18446 "/* block comment */\n"
18448 "int longbitfield : 6;",
18450 verifyFormat("int a : 5;\n"
18453 "// line comment\n"
18455 "int longbitfield : 6;",
18459 "// line comment \n"
18461 "int longbitfield : 6;",
18465 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossComments
) {
18466 FormatStyle Alignment
= getLLVMStyle();
18467 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18468 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18469 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18471 Alignment
.MaxEmptyLinesToKeep
= 10;
18472 /* Test alignment across empty lines */
18473 verifyFormat("int a = 5;\n"
18475 "int oneTwoThree = 123;",
18478 "int oneTwoThree= 123;",
18480 verifyFormat("int a = 5;\n"
18483 "int oneTwoThree = 123;",
18487 "int oneTwoThree = 123;",
18490 /* Test across comments */
18491 verifyFormat("int a = 5;\n"
18492 "/* block comment */\n"
18493 "int oneTwoThree = 123;",
18495 "/* block comment */\n"
18496 "int oneTwoThree=123;",
18499 verifyFormat("int a = 5;\n"
18500 "// line comment\n"
18501 "int oneTwoThree = 123;",
18503 "// line comment\n"
18504 "int oneTwoThree=123;",
18507 verifyFormat("int a = 5;\n"
18509 " * multi-line block comment\n"
18511 "int oneTwoThree = 123;",
18514 " * multi-line block comment\n"
18516 "int oneTwoThree=123;",
18519 verifyFormat("int a = 5;\n"
18521 "// multi-line line comment\n"
18523 "int oneTwoThree = 123;",
18526 "// multi-line line comment\n"
18528 "int oneTwoThree=123;",
18531 /* Test across comments and newlines */
18532 verifyFormat("int a = 5;\n"
18534 "/* block comment */\n"
18535 "int oneTwoThree = 123;",
18538 "/* block comment */\n"
18539 "int oneTwoThree=123;",
18542 verifyFormat("int a = 5;\n"
18544 "// line comment\n"
18545 "int oneTwoThree = 123;",
18548 "// line comment\n"
18549 "int oneTwoThree=123;",
18553 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments
) {
18554 FormatStyle Alignment
= getLLVMStyle();
18555 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18556 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18557 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18558 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18559 verifyFormat("int a = 5;\n"
18560 "int oneTwoThree = 123;",
18562 verifyFormat("int a = method();\n"
18563 "int oneTwoThree = 133;",
18565 verifyFormat("a &= 5;\n"
18571 "sfdbddfbdfbb ^= 5;\n"
18573 "int dsvvdvsdvvv = 123;",
18575 verifyFormat("int i = 1, j = 10;\n"
18576 "something = 2000;",
18578 verifyFormat("something = 2000;\n"
18579 "int i = 1, j = 10;",
18581 verifyFormat("something = 2000;\n"
18583 "int i = 1, j = 10;\n"
18587 verifyFormat("int a = 5;\n"
18590 "int oneTwoThree = 123;\n"
18591 "int oneTwo = 12;",
18593 verifyFormat("int oneTwoThree = 123;\n"
18594 "int oneTwo = 12;\n"
18597 verifyFormat("int oneTwoThree = 123; // comment\n"
18598 "int oneTwo = 12; // comment",
18602 /* Uncomment when fixed
18603 verifyFormat("#if A\n"
18605 "int aaaaaaaa = 12;\n"
18612 verifyFormat("enum foo {\n"
18615 " aaaaaaaa = 12;\n"
18625 Alignment
.MaxEmptyLinesToKeep
= 10;
18626 /* Test alignment across empty lines */
18627 verifyFormat("int a = 5;\n"
18629 "int oneTwoThree = 123;",
18632 "int oneTwoThree= 123;",
18634 verifyFormat("int a = 5;\n"
18637 "int oneTwoThree = 123;",
18641 "int oneTwoThree = 123;",
18643 verifyFormat("int a = 5;\n"
18646 "int oneTwoThree = 123;\n"
18647 "int oneTwo = 12;",
18651 "int oneTwoThree = 123;\n"
18652 "int oneTwo = 12;",
18655 /* Test across comments */
18656 verifyFormat("int a = 5;\n"
18657 "/* block comment */\n"
18658 "int oneTwoThree = 123;",
18660 "/* block comment */\n"
18661 "int oneTwoThree=123;",
18664 verifyFormat("int a = 5;\n"
18665 "// line comment\n"
18666 "int oneTwoThree = 123;",
18668 "// line comment\n"
18669 "int oneTwoThree=123;",
18672 /* Test across comments and newlines */
18673 verifyFormat("int a = 5;\n"
18675 "/* block comment */\n"
18676 "int oneTwoThree = 123;",
18679 "/* block comment */\n"
18680 "int oneTwoThree=123;",
18683 verifyFormat("int a = 5;\n"
18685 "// line comment\n"
18686 "int oneTwoThree = 123;",
18689 "// line comment\n"
18690 "int oneTwoThree=123;",
18693 verifyFormat("int a = 5;\n"
18695 "// multi-line line comment\n"
18697 "int oneTwoThree = 123;",
18700 "// multi-line line comment\n"
18702 "int oneTwoThree=123;",
18705 verifyFormat("int a = 5;\n"
18707 " * multi-line block comment\n"
18709 "int oneTwoThree = 123;",
18712 " * multi-line block comment\n"
18714 "int oneTwoThree=123;",
18717 verifyFormat("int a = 5;\n"
18719 "/* block comment */\n"
18723 "int oneTwoThree = 123;",
18726 "/* block comment */\n"
18730 "int oneTwoThree=123;",
18733 verifyFormat("int a = 5;\n"
18735 "// line comment\n"
18739 "int oneTwoThree = 123;",
18742 "// line comment\n"
18746 "int oneTwoThree=123;",
18749 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
18750 verifyFormat("#define A \\\n"
18751 " int aaaa = 12; \\\n"
18752 " int b = 23; \\\n"
18753 " int ccc = 234; \\\n"
18754 " int dddddddddd = 2345;",
18756 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
18757 verifyFormat("#define A \\\n"
18758 " int aaaa = 12; \\\n"
18759 " int b = 23; \\\n"
18760 " int ccc = 234; \\\n"
18761 " int dddddddddd = 2345;",
18763 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
18764 verifyFormat("#define A "
18772 " int dddddddddd = 2345;",
18774 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18775 "k = 4, int l = 5,\n"
18778 " otherThing = 1;\n"
18781 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18784 " int big = 10000;\n"
18787 verifyFormat("class C {\n"
18790 " virtual void f() = 0;\n"
18793 verifyFormat("int i = 1;\n"
18794 "if (SomeType t = getSomething()) {\n"
18797 "int big = 10000;",
18799 verifyFormat("int j = 7;\n"
18800 "for (int k = 0; k < N; ++k) {\n"
18803 "int big = 10000;\n"
18806 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
18807 verifyFormat("int i = 1;\n"
18808 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18809 " = someLooooooooooooooooongFunction();\n"
18812 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
18813 verifyFormat("int i = 1;\n"
18814 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18815 " someLooooooooooooooooongFunction();\n"
18819 verifyFormat("auto lambda = []() {\n"
18833 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18834 " loooooooooooooooooooooongParameterB);\n"
18838 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18839 " typename B = very_long_type_name_1,\n"
18840 " typename T_2 = very_long_type_name_2>\n"
18843 verifyFormat("int a, b = 1;\n"
18847 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18848 "float b[1][] = {{3.f}};",
18850 verifyFormat("for (int i = 0; i < 1; i++)\n"
18853 verifyFormat("for (i = 0; i < 1; i++)\n"
18858 Alignment
.ReflowComments
= FormatStyle::RCS_Always
;
18859 Alignment
.ColumnLimit
= 50;
18860 verifyFormat("int x = 0;\n"
18861 "int yy = 1; /// specificlennospace\n"
18864 "int yy = 1; ///specificlennospace\n"
18869 TEST_F(FormatTest
, AlignCompoundAssignments
) {
18870 FormatStyle Alignment
= getLLVMStyle();
18871 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18872 Alignment
.AlignConsecutiveAssignments
.AlignCompound
= true;
18873 Alignment
.AlignConsecutiveAssignments
.PadOperators
= false;
18874 verifyFormat("sfdbddfbdfbb = 5;\n"
18876 "int dsvvdvsdvvv = 123;",
18878 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18880 "int dsvvdvsdvvv = 123;",
18882 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18884 "int dsvvdvsdvvv = 123;",
18886 verifyFormat("int xxx = 5;\n"
18893 verifyFormat("int xxx = 5;\n"
18900 // Test that `<=` is not treated as a compound assignment.
18901 verifyFormat("aa &= 5;\n"
18905 Alignment
.AlignConsecutiveAssignments
.PadOperators
= true;
18906 verifyFormat("sfdbddfbdfbb = 5;\n"
18908 "int dsvvdvsdvvv = 123;",
18910 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18912 "int dsvvdvsdvvv = 123;",
18914 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18916 "int dsvvdvsdvvv = 123;",
18918 verifyFormat("a += 5;\n"
18921 "oneTwoThree = 123;",
18925 "oneTwoThree = 123;",
18927 verifyFormat("a += 5;\n"
18930 "oneTwoThree = 123;",
18934 "oneTwoThree = 123;",
18936 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18937 verifyFormat("a += 5;\n"
18940 "oneTwoThree = 123;",
18944 "oneTwoThree = 123;",
18946 verifyFormat("a += 5;\n"
18949 "oneTwoThree = 123;",
18953 "oneTwoThree = 123;",
18955 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= false;
18956 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18957 verifyFormat("a += 5;\n"
18960 "oneTwoThree = 123;",
18964 "oneTwoThree = 123;",
18966 verifyFormat("a += 5;\n"
18969 "oneTwoThree = 123;",
18973 "oneTwoThree = 123;",
18975 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18976 verifyFormat("a += 5;\n"
18979 "oneTwoThree = 123;",
18983 "oneTwoThree = 123;",
18985 verifyFormat("a += 5;\n"
18988 "oneTwoThree <<= 123;",
18992 "oneTwoThree <<= 123;",
18996 TEST_F(FormatTest
, AlignConsecutiveAssignments
) {
18997 FormatStyle Alignment
= getLLVMStyle();
18998 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18999 verifyFormat("int a = 5;\n"
19000 "int oneTwoThree = 123;",
19002 verifyFormat("int a = 5;\n"
19003 "int oneTwoThree = 123;",
19006 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19007 verifyFormat("int a = 5;\n"
19008 "int oneTwoThree = 123;",
19010 verifyFormat("int a = method();\n"
19011 "int oneTwoThree = 133;",
19013 verifyFormat("aa <= 5;\n"
19020 "sfdbddfbdfbb ^= 5;\n"
19022 "int dsvvdvsdvvv = 123;",
19024 verifyFormat("int i = 1, j = 10;\n"
19025 "something = 2000;",
19027 verifyFormat("something = 2000;\n"
19028 "int i = 1, j = 10;",
19030 verifyFormat("something = 2000;\n"
19032 "int i = 1, j = 10;\n"
19036 verifyFormat("int a = 5;\n"
19039 "int oneTwoThree = 123;\n"
19040 "int oneTwo = 12;",
19042 verifyFormat("int oneTwoThree = 123;\n"
19043 "int oneTwo = 12;\n"
19046 verifyFormat("int oneTwoThree = 123; // comment\n"
19047 "int oneTwo = 12; // comment",
19049 verifyFormat("int f() = default;\n"
19050 "int &operator() = default;\n"
19051 "int &operator=() {",
19053 verifyFormat("int f() = delete;\n"
19054 "int &operator() = delete;\n"
19055 "int &operator=() {",
19057 verifyFormat("int f() = default; // comment\n"
19058 "int &operator() = default; // comment\n"
19059 "int &operator=() {",
19061 verifyFormat("int f() = default;\n"
19062 "int &operator() = default;\n"
19063 "int &operator==() {",
19065 verifyFormat("int f() = default;\n"
19066 "int &operator() = default;\n"
19067 "int &operator<=() {",
19069 verifyFormat("int f() = default;\n"
19070 "int &operator() = default;\n"
19071 "int &operator!=() {",
19073 verifyFormat("int f() = default;\n"
19074 "int &operator() = default;\n"
19075 "int &operator=();",
19077 verifyFormat("int f() = delete;\n"
19078 "int &operator() = delete;\n"
19079 "int &operator=();",
19081 verifyFormat("/* long long padding */ int f() = default;\n"
19082 "int &operator() = default;\n"
19083 "int &operator/**/ =();",
19085 // https://llvm.org/PR33697
19086 FormatStyle AlignmentWithPenalty
= getLLVMStyle();
19087 AlignmentWithPenalty
.AlignConsecutiveAssignments
.Enabled
= true;
19088 AlignmentWithPenalty
.PenaltyReturnTypeOnItsOwnLine
= 5000;
19089 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19090 " void f() = delete;\n"
19091 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19092 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19094 AlignmentWithPenalty
);
19097 /* Uncomment when fixed
19098 verifyFormat("#if A\n"
19100 "int aaaaaaaa = 12;\n"
19107 verifyFormat("enum foo {\n"
19110 " aaaaaaaa = 12;\n"
19120 verifyFormat("int a = 5;\n"
19122 "int oneTwoThree = 123;",
19125 "int oneTwoThree= 123;",
19127 verifyFormat("int a = 5;\n"
19130 "int oneTwoThree = 123;",
19134 "int oneTwoThree = 123;",
19136 verifyFormat("int a = 5;\n"
19139 "int oneTwoThree = 123;\n"
19140 "int oneTwo = 12;",
19144 "int oneTwoThree = 123;\n"
19145 "int oneTwo = 12;",
19147 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
19148 verifyFormat("#define A \\\n"
19149 " int aaaa = 12; \\\n"
19150 " int b = 23; \\\n"
19151 " int ccc = 234; \\\n"
19152 " int dddddddddd = 2345;",
19154 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
19155 verifyFormat("#define A \\\n"
19156 " int aaaa = 12; \\\n"
19157 " int b = 23; \\\n"
19158 " int ccc = 234; \\\n"
19159 " int dddddddddd = 2345;",
19161 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
19162 verifyFormat("#define A "
19170 " int dddddddddd = 2345;",
19172 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19173 "k = 4, int l = 5,\n"
19176 " otherThing = 1;\n"
19179 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19182 " int big = 10000;\n"
19185 verifyFormat("class C {\n"
19188 " virtual void f() = 0;\n"
19191 verifyFormat("int i = 1;\n"
19192 "if (SomeType t = getSomething()) {\n"
19195 "int big = 10000;",
19197 verifyFormat("int j = 7;\n"
19198 "for (int k = 0; k < N; ++k) {\n"
19201 "int big = 10000;\n"
19204 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
19205 verifyFormat("int i = 1;\n"
19206 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19207 " = someLooooooooooooooooongFunction();\n"
19210 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
19211 verifyFormat("int i = 1;\n"
19212 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19213 " someLooooooooooooooooongFunction();\n"
19217 verifyFormat("auto lambda = []() {\n"
19231 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19232 " loooooooooooooooooooooongParameterB);\n"
19236 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19237 " typename B = very_long_type_name_1,\n"
19238 " typename T_2 = very_long_type_name_2>\n"
19241 verifyFormat("int a, b = 1;\n"
19245 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19246 "float b[1][] = {{3.f}};",
19248 verifyFormat("for (int i = 0; i < 1; i++)\n"
19251 verifyFormat("for (i = 0; i < 1; i++)\n"
19256 EXPECT_EQ(Alignment
.ReflowComments
, FormatStyle::RCS_Always
);
19257 Alignment
.ColumnLimit
= 50;
19258 verifyFormat("int x = 0;\n"
19259 "int yy = 1; /// specificlennospace\n"
19262 "int yy = 1; ///specificlennospace\n"
19266 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19272 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19273 "auto b = g([] {\n"
19278 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19279 "auto b = g(param, [] {\n"
19284 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19286 " if (condition) {\n"
19292 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19293 " ccc ? aaaaa : bbbbb,\n"
19294 " dddddddddddddddddddddddddd);",
19296 // FIXME: https://llvm.org/PR53497
19297 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19298 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19299 // " ccc ? aaaaa : bbbbb,\n"
19300 // " dddddddddddddddddddddddddd);",
19303 // Confirm proper handling of AlignConsecutiveAssignments with
19304 // BinPackArguments.
19305 // See https://llvm.org/PR55360
19306 Alignment
= getLLVMStyleWithColumns(50);
19307 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19308 Alignment
.BinPackArguments
= false;
19309 verifyFormat("int a_long_name = 1;\n"
19310 "auto b = B({a_long_name, a_long_name},\n"
19311 " {a_longer_name_for_wrap,\n"
19312 " a_longer_name_for_wrap});",
19314 verifyFormat("int a_long_name = 1;\n"
19315 "auto b = B{{a_long_name, a_long_name},\n"
19316 " {a_longer_name_for_wrap,\n"
19317 " a_longer_name_for_wrap}};",
19320 Alignment
= getLLVMStyleWithColumns(60);
19321 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19322 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19323 "using I = std::conditional_t<II::value >= 0,\n"
19324 " std::ic<int, II::value + 1>,\n"
19325 " std::ic<int, -1>>;",
19327 verifyFormat("SomeName = Foo;\n"
19328 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19332 Alignment
.ColumnLimit
= 80;
19333 Alignment
.SpacesInAngles
= FormatStyle::SIAS_Always
;
19334 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19335 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19337 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19338 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19341 Alignment
.SpacesInParens
= FormatStyle::SIPO_Custom
;
19342 Alignment
.SpacesInParensOptions
.InCStyleCasts
= true;
19343 verifyFormat("void **ptr = ( void ** )unkn;\n"
19344 "ptr = ( void ** )ptr[0];",
19346 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19347 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19351 TEST_F(FormatTest
, AlignConsecutiveBitFields
) {
19352 FormatStyle Alignment
= getLLVMStyle();
19353 Alignment
.AlignConsecutiveBitFields
.Enabled
= true;
19354 verifyFormat("int const a : 5;\n"
19355 "int oneTwoThree : 23;",
19358 // Initializers are allowed starting with c++2a
19359 verifyFormat("int const a : 5 = 1;\n"
19360 "int oneTwoThree : 23 = 0;",
19363 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19364 verifyFormat("int const a : 5;\n"
19365 "int oneTwoThree : 23;",
19368 verifyFormat("int const a : 5; // comment\n"
19369 "int oneTwoThree : 23; // comment",
19372 verifyFormat("int const a : 5 = 1;\n"
19373 "int oneTwoThree : 23 = 0;",
19376 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19377 verifyFormat("int const a : 5 = 1;\n"
19378 "int oneTwoThree : 23 = 0;",
19380 verifyFormat("int const a : 5 = {1};\n"
19381 "int oneTwoThree : 23 = 0;",
19384 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_None
;
19385 verifyFormat("int const a :5;\n"
19386 "int oneTwoThree:23;",
19389 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_Before
;
19390 verifyFormat("int const a :5;\n"
19391 "int oneTwoThree :23;",
19394 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_After
;
19395 verifyFormat("int const a : 5;\n"
19396 "int oneTwoThree: 23;",
19399 // Known limitations: ':' is only recognized as a bitfield colon when
19400 // followed by a number.
19402 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19408 TEST_F(FormatTest
, AlignConsecutiveDeclarations
) {
19409 FormatStyle Alignment
= getLLVMStyle();
19410 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
19411 Alignment
.PointerAlignment
= FormatStyle::PAS_Right
;
19412 verifyFormat("float const a = 5;\n"
19413 "int oneTwoThree = 123;",
19415 verifyFormat("int a = 5;\n"
19416 "float const oneTwoThree = 123;",
19419 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19420 verifyFormat("float const a = 5;\n"
19421 "int oneTwoThree = 123;",
19423 verifyFormat("int a = method();\n"
19424 "float const oneTwoThree = 133;",
19426 verifyFormat("int i = 1, j = 10;\n"
19427 "something = 2000;",
19429 verifyFormat("something = 2000;\n"
19430 "int i = 1, j = 10;",
19432 verifyFormat("float something = 2000;\n"
19433 "double another = 911;\n"
19434 "int i = 1, j = 10;\n"
19435 "const int *oneMore = 1;\n"
19438 verifyFormat("float a = 5;\n"
19441 "const double oneTwoThree = 123;\n"
19442 "const unsigned int oneTwo = 12;",
19444 verifyFormat("int oneTwoThree{0}; // comment\n"
19445 "unsigned oneTwo; // comment",
19447 verifyFormat("unsigned int *a;\n"
19449 "unsigned int Const *c;\n"
19450 "unsigned int const *d;\n"
19451 "unsigned int Const &e;\n"
19452 "unsigned int const &f;",
19454 verifyFormat("Const unsigned int *c;\n"
19455 "const unsigned int *d;\n"
19456 "Const unsigned int &e;\n"
19457 "const unsigned int &f;\n"
19458 "const unsigned g;\n"
19459 "Const unsigned h;",
19461 verifyFormat("float const a = 5;\n"
19463 "int oneTwoThree = 123;",
19464 "float const a = 5;\n"
19466 "int oneTwoThree= 123;",
19468 verifyFormat("float a = 5;\n"
19471 "unsigned oneTwoThree = 123;",
19475 "unsigned oneTwoThree = 123;",
19477 verifyFormat("float a = 5;\n"
19480 "unsigned oneTwoThree = 123;\n"
19481 "int oneTwo = 12;",
19485 "unsigned oneTwoThree = 123;\n"
19486 "int oneTwo = 12;",
19488 // Function prototype alignment
19489 verifyFormat("int a();\n"
19492 verifyFormat("int a(int x);\n"
19495 verifyFormat("int a(const Test & = Test());\n"
19496 "int a1(int &foo, const Test & = Test());\n"
19497 "int a2(int &foo, const Test &name = Test());\n"
19500 verifyFormat("struct Test {\n"
19501 " Test(const Test &) = default;\n"
19502 " ~Test() = default;\n"
19503 " Test &operator=(const Test &) = default;\n"
19506 unsigned OldColumnLimit
= Alignment
.ColumnLimit
;
19507 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19508 // otherwise the function parameters will be re-flowed onto a single line.
19509 Alignment
.ColumnLimit
= 0;
19510 verifyFormat("int a(int x,\n"
19512 "double b(int x,\n"
19516 "double b(int x,\n"
19519 // This ensures that function parameters of function declarations are
19520 // correctly indented when their owning functions are indented.
19521 // The failure case here is for 'double y' to not be indented enough.
19522 verifyFormat("double a(int x);\n"
19525 "double a(int x);\n"
19529 // Set ColumnLimit low so that we induce wrapping immediately after
19530 // the function name and opening paren.
19531 Alignment
.ColumnLimit
= 13;
19532 verifyFormat("int function(\n"
19536 // Set ColumnLimit low so that we break the argument list in multiple lines.
19537 Alignment
.ColumnLimit
= 35;
19538 verifyFormat("int a3(SomeTypeName1 &x,\n"
19539 " SomeTypeName2 &y,\n"
19540 " const Test & = Test());\n"
19543 Alignment
.ColumnLimit
= OldColumnLimit
;
19544 // Ensure function pointers don't screw up recursive alignment
19545 verifyFormat("int a(int x, void (*fp)(int y));\n"
19548 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19549 verifyFormat("struct Test {\n"
19550 " Test(const Test &) = default;\n"
19551 " ~Test() = default;\n"
19552 " Test &operator=(const Test &) = default;\n"
19555 // Ensure recursive alignment is broken by function braces, so that the
19556 // "a = 1" does not align with subsequent assignments inside the function
19558 verifyFormat("int func(int a = 1) {\n"
19563 verifyFormat("float something = 2000;\n"
19564 "double another = 911;\n"
19565 "int i = 1, j = 10;\n"
19566 "const int *oneMore = 1;\n"
19569 verifyFormat("int oneTwoThree = {0}; // comment\n"
19570 "unsigned oneTwo = 0; // comment",
19572 // Make sure that scope is correctly tracked, in the absence of braces
19573 verifyFormat("for (int i = 0; i < n; i++)\n"
19577 verifyFormat("if (int i = 0)\n"
19581 // Ensure operator[] and operator() are comprehended
19582 verifyFormat("struct test {\n"
19583 " long long int foo();\n"
19584 " int operator[](int a);\n"
19588 verifyFormat("struct test {\n"
19589 " long long int foo();\n"
19590 " int operator()(int a);\n"
19594 // http://llvm.org/PR52914
19595 verifyFormat("char *a[] = {\"a\", // comment\n"
19597 "int bbbbbbb = 0;",
19599 // http://llvm.org/PR68079
19600 verifyFormat("using Fn = int (A::*)();\n"
19601 "using RFn = int (A::*)() &;\n"
19602 "using RRFn = int (A::*)() &&;",
19604 verifyFormat("using Fn = int (A::*)();\n"
19605 "using RFn = int *(A::*)() &;\n"
19606 "using RRFn = double (A::*)() &&;",
19610 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19611 " int const i = 1;\n"
19613 " int big = 10000;\n"
19615 " unsigned oneTwoThree = 123;\n"
19616 " int oneTwo = 12;\n"
19619 " int ll = 10000;\n"
19621 "void SomeFunction(int parameter= 0) {\n"
19622 " int const i= 1;\n"
19624 " int big = 10000;\n"
19626 "unsigned oneTwoThree =123;\n"
19627 "int oneTwo = 12;\n"
19633 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19634 " int const i = 1;\n"
19635 " int **j = 2, ***k;\n"
19637 " int &&l = i + j;\n"
19638 " int big = 10000;\n"
19640 " unsigned oneTwoThree = 123;\n"
19641 " int oneTwo = 12;\n"
19644 " int ll = 10000;\n"
19646 "void SomeFunction(int parameter= 0) {\n"
19647 " int const i= 1;\n"
19648 " int **j=2,***k;\n"
19651 " int big = 10000;\n"
19653 "unsigned oneTwoThree =123;\n"
19654 "int oneTwo = 12;\n"
19660 // variables are aligned at their name, pointers are at the right most
19662 verifyFormat("int *a;\n"
19669 FormatStyle AlignmentLeft
= Alignment
;
19670 AlignmentLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
19671 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19672 " int const i = 1;\n"
19674 " int big = 10000;\n"
19676 " unsigned oneTwoThree = 123;\n"
19677 " int oneTwo = 12;\n"
19680 " int ll = 10000;\n"
19682 "void SomeFunction(int parameter= 0) {\n"
19683 " int const i= 1;\n"
19685 " int big = 10000;\n"
19687 "unsigned oneTwoThree =123;\n"
19688 "int oneTwo = 12;\n"
19694 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19695 " int const i = 1;\n"
19698 " int&& l = i + j;\n"
19699 " int big = 10000;\n"
19701 " unsigned oneTwoThree = 123;\n"
19702 " int oneTwo = 12;\n"
19705 " int ll = 10000;\n"
19707 "void SomeFunction(int parameter= 0) {\n"
19708 " int const i= 1;\n"
19712 " int big = 10000;\n"
19714 "unsigned oneTwoThree =123;\n"
19715 "int oneTwo = 12;\n"
19721 // variables are aligned at their name, pointers are at the left most position
19722 verifyFormat("int* a;\n"
19728 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19733 FormatStyle AlignmentMiddle
= Alignment
;
19734 AlignmentMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
19735 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19736 " int const i = 1;\n"
19738 " int big = 10000;\n"
19740 " unsigned oneTwoThree = 123;\n"
19741 " int oneTwo = 12;\n"
19744 " int ll = 10000;\n"
19746 "void SomeFunction(int parameter= 0) {\n"
19747 " int const i= 1;\n"
19749 " int big = 10000;\n"
19751 "unsigned oneTwoThree =123;\n"
19752 "int oneTwo = 12;\n"
19758 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19759 " int const i = 1;\n"
19760 " int ** j = 2, ***k;\n"
19762 " int && l = i + j;\n"
19763 " int big = 10000;\n"
19765 " unsigned oneTwoThree = 123;\n"
19766 " int oneTwo = 12;\n"
19769 " int ll = 10000;\n"
19771 "void SomeFunction(int parameter= 0) {\n"
19772 " int const i= 1;\n"
19773 " int **j=2,***k;\n"
19776 " int big = 10000;\n"
19778 "unsigned oneTwoThree =123;\n"
19779 "int oneTwo = 12;\n"
19785 // variables are aligned at their name, pointers are in the middle
19786 verifyFormat("int * a;\n"
19792 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19796 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19797 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
19798 verifyFormat("#define A \\\n"
19799 " int aaaa = 12; \\\n"
19800 " float b = 23; \\\n"
19801 " const int ccc = 234; \\\n"
19802 " unsigned dddddddddd = 2345;",
19804 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
19805 verifyFormat("#define A \\\n"
19806 " int aaaa = 12; \\\n"
19807 " float b = 23; \\\n"
19808 " const int ccc = 234; \\\n"
19809 " unsigned dddddddddd = 2345;",
19811 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
19812 Alignment
.ColumnLimit
= 30;
19813 verifyFormat("#define A \\\n"
19814 " int aaaa = 12; \\\n"
19815 " float b = 23; \\\n"
19816 " const int ccc = 234; \\\n"
19817 " int dddddddddd = 2345;",
19819 Alignment
.ColumnLimit
= 80;
19820 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19821 "k = 4, int l = 5,\n"
19823 " const int j = 10;\n"
19824 " otherThing = 1;\n"
19827 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19828 " int const i = 1;\n"
19830 " int big = 10000;\n"
19833 verifyFormat("class C {\n"
19836 " virtual void f() = 0;\n"
19839 verifyFormat("float i = 1;\n"
19840 "if (SomeType t = getSomething()) {\n"
19842 "const unsigned j = 2;\n"
19843 "int big = 10000;",
19845 verifyFormat("float j = 7;\n"
19846 "for (int k = 0; k < N; ++k) {\n"
19848 "unsigned j = 2;\n"
19849 "int big = 10000;\n"
19852 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
19853 verifyFormat("float i = 1;\n"
19854 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19855 " = someLooooooooooooooooongFunction();\n"
19858 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
19859 verifyFormat("int i = 1;\n"
19860 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19861 " someLooooooooooooooooongFunction();\n"
19865 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19866 verifyFormat("auto lambda = []() {\n"
19879 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19883 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19884 " loooooooooooooooooooooongParameterB);\n"
19888 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19889 // We expect declarations and assignments to align, as long as it doesn't
19890 // exceed the column limit, starting a new alignment sequence whenever it
19892 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19893 Alignment
.ColumnLimit
= 30;
19894 verifyFormat("float ii = 1;\n"
19895 "unsigned j = 2;\n"
19896 "int someVerylongVariable = 1;\n"
19897 "AnotherLongType ll = 123456;\n"
19898 "VeryVeryLongType k = 2;\n"
19901 Alignment
.ColumnLimit
= 80;
19902 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19905 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19906 " typename LongType, typename B>\n"
19909 verifyFormat("float a, b = 1;\n"
19913 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19914 "float b[1][] = {{3.f}};",
19916 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19917 verifyFormat("float a, b = 1;\n"
19921 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19922 "float b[1][] = {{3.f}};",
19924 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19926 Alignment
.ColumnLimit
= 30;
19927 Alignment
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
19928 verifyFormat("void foo(float a,\n"
19931 " uint32_t *d) {\n"
19936 "void bar(ino_t a,\n"
19941 Alignment
.BinPackParameters
= FormatStyle::BPPS_BinPack
;
19942 Alignment
.ColumnLimit
= 80;
19945 Alignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
19947 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19948 " static const Version verVs2017;\n"
19952 Alignment
.PointerAlignment
= FormatStyle::PAS_Right
;
19954 // See llvm.org/PR35641
19955 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19956 verifyFormat("int func() { //\n"
19963 FormatStyle Style
= getMozillaStyle();
19964 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19965 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19967 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style
);
19969 Alignment
.PointerAlignment
= FormatStyle::PAS_Left
;
19970 verifyFormat("unsigned int* a;\n"
19972 "unsigned int Const* c;\n"
19973 "unsigned int const* d;\n"
19974 "unsigned int Const& e;\n"
19975 "unsigned int const& f;",
19977 verifyFormat("Const unsigned int* c;\n"
19978 "const unsigned int* d;\n"
19979 "Const unsigned int& e;\n"
19980 "const unsigned int& f;\n"
19981 "const unsigned g;\n"
19982 "Const unsigned h;",
19985 Alignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
19986 verifyFormat("unsigned int * a;\n"
19988 "unsigned int Const * c;\n"
19989 "unsigned int const * d;\n"
19990 "unsigned int Const & e;\n"
19991 "unsigned int const & f;",
19993 verifyFormat("Const unsigned int * c;\n"
19994 "const unsigned int * d;\n"
19995 "Const unsigned int & e;\n"
19996 "const unsigned int & f;\n"
19997 "const unsigned g;\n"
19998 "Const unsigned h;",
20002 FormatStyle BracedAlign
= getLLVMStyle();
20003 BracedAlign
.AlignConsecutiveDeclarations
.Enabled
= true;
20004 verifyFormat("const auto result{[]() {\n"
20005 " const auto something = 1;\n"
20009 verifyFormat("int foo{[]() {\n"
20014 BracedAlign
.Cpp11BracedListStyle
= false;
20015 verifyFormat("const auto result{ []() {\n"
20016 " const auto something = 1;\n"
20020 verifyFormat("int foo{ []() {\n"
20026 Alignment
.AlignConsecutiveDeclarations
.AlignFunctionDeclarations
= false;
20027 verifyFormat("unsigned int f1(void);\n"
20029 "size_t f3(void);",
20033 TEST_F(FormatTest
, AlignConsecutiveShortCaseStatements
) {
20034 FormatStyle Alignment
= getLLVMStyle();
20035 Alignment
.AllowShortCaseLabelsOnASingleLine
= true;
20036 Alignment
.AlignConsecutiveShortCaseStatements
.Enabled
= true;
20038 verifyFormat("switch (level) {\n"
20039 "case log::info: return \"info\";\n"
20040 "case log::warning: return \"warning\";\n"
20041 "default: return \"default\";\n"
20045 verifyFormat("switch (level) {\n"
20046 "case log::info: return \"info\";\n"
20047 "case log::warning: return \"warning\";\n"
20049 "switch (level) {\n"
20050 "case log::info: return \"info\";\n"
20051 "case log::warning:\n"
20052 " return \"warning\";\n"
20056 // Empty case statements push out the alignment, but non-short case labels
20058 verifyFormat("switch (level) {\n"
20059 "case log::info: return \"info\";\n"
20060 "case log::critical:\n"
20061 "case log::warning:\n"
20062 "case log::severe: return \"severe\";\n"
20063 "case log::extra_severe:\n"
20065 " return \"extra_severe\";\n"
20069 // Verify comments and empty lines break the alignment.
20070 verifyNoChange("switch (level) {\n"
20071 "case log::info: return \"info\";\n"
20072 "case log::warning: return \"warning\";\n"
20074 "case log::critical: return \"critical\";\n"
20075 "default: return \"default\";\n"
20077 "case log::severe: return \"severe\";\n"
20081 // Empty case statements don't break the alignment, and potentially push it
20083 verifyFormat("switch (level) {\n"
20084 "case log::info: return \"info\";\n"
20085 "case log::warning:\n"
20086 "case log::critical:\n"
20087 "default: return \"default\";\n"
20091 // Implicit fallthrough cases can be aligned with either a comment or
20093 verifyFormat("switch (level) {\n"
20094 "case log::info: return \"info\";\n"
20095 "case log::warning: // fallthrough\n"
20096 "case log::error: return \"error\";\n"
20097 "case log::critical: /*fallthrough*/\n"
20098 "case log::severe: return \"severe\";\n"
20099 "case log::diag: [[fallthrough]];\n"
20100 "default: return \"default\";\n"
20104 // Verify trailing comment that needs a reflow also gets aligned properly.
20105 verifyFormat("switch (level) {\n"
20106 "case log::info: return \"info\";\n"
20107 "case log::warning: // fallthrough\n"
20108 "case log::error: return \"error\";\n"
20110 "switch (level) {\n"
20111 "case log::info: return \"info\";\n"
20112 "case log::warning: //fallthrough\n"
20113 "case log::error: return \"error\";\n"
20117 // Verify adjacent non-short case statements don't change the alignment, and
20118 // properly break the set of consecutive statements.
20119 verifyFormat("switch (level) {\n"
20120 "case log::critical:\n"
20122 " return \"critical\";\n"
20123 "case log::info: return \"info\";\n"
20124 "case log::warning: return \"warning\";\n"
20128 "case log::error: return \"error\";\n"
20129 "case log::severe: return \"severe\";\n"
20130 "case log::extra_critical:\n"
20132 " return \"extra critical\";\n"
20136 Alignment
.SpaceBeforeCaseColon
= true;
20137 verifyFormat("switch (level) {\n"
20138 "case log::info : return \"info\";\n"
20139 "case log::warning : return \"warning\";\n"
20140 "default : return \"default\";\n"
20143 Alignment
.SpaceBeforeCaseColon
= false;
20145 // Make sure we don't incorrectly align correctly across nested switch cases.
20146 verifyFormat("switch (level) {\n"
20147 "case log::info: return \"info\";\n"
20148 "case log::warning: return \"warning\";\n"
20149 "case log::other:\n"
20150 " switch (sublevel) {\n"
20151 " case log::info: return \"info\";\n"
20152 " case log::warning: return \"warning\";\n"
20155 "case log::error: return \"error\";\n"
20156 "default: return \"default\";\n"
20158 "switch (level) {\n"
20159 "case log::info: return \"info\";\n"
20160 "case log::warning: return \"warning\";\n"
20161 "case log::other: switch (sublevel) {\n"
20162 " case log::info: return \"info\";\n"
20163 " case log::warning: return \"warning\";\n"
20166 "case log::error: return \"error\";\n"
20167 "default: return \"default\";\n"
20171 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossEmptyLines
= true;
20173 verifyFormat("switch (level) {\n"
20174 "case log::info: return \"info\";\n"
20176 "case log::warning: return \"warning\";\n"
20178 "switch (level) {\n"
20179 "case log::info: return \"info\";\n"
20181 "case log::warning: return \"warning\";\n"
20185 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossComments
= true;
20187 verifyNoChange("switch (level) {\n"
20188 "case log::info: return \"info\";\n"
20190 "/* block comment */\n"
20192 "// line comment\n"
20193 "case log::warning: return \"warning\";\n"
20197 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossEmptyLines
= false;
20199 verifyFormat("switch (level) {\n"
20200 "case log::info: return \"info\";\n"
20202 "case log::warning: return \"warning\";\n"
20206 Alignment
.AlignConsecutiveShortCaseStatements
.AlignCaseColons
= true;
20208 verifyFormat("switch (level) {\n"
20209 "case log::info : return \"info\";\n"
20210 "case log::warning: return \"warning\";\n"
20211 "default : return \"default\";\n"
20215 // With AlignCaseColons, empty case statements don't break alignment of
20216 // consecutive case statements (and are aligned).
20217 verifyFormat("switch (level) {\n"
20218 "case log::info : return \"info\";\n"
20219 "case log::warning :\n"
20220 "case log::critical:\n"
20221 "default : return \"default\";\n"
20225 // Final non-short case labels shouldn't have their colon aligned
20226 verifyFormat("switch (level) {\n"
20227 "case log::info : return \"info\";\n"
20228 "case log::warning :\n"
20229 "case log::critical:\n"
20230 "case log::severe : return \"severe\";\n"
20233 " return \"default\";\n"
20237 // Verify adjacent non-short case statements break the set of consecutive
20238 // alignments and aren't aligned with adjacent non-short case statements if
20239 // AlignCaseColons is set.
20240 verifyFormat("switch (level) {\n"
20241 "case log::critical:\n"
20243 " return \"critical\";\n"
20244 "case log::info : return \"info\";\n"
20245 "case log::warning: return \"warning\";\n"
20249 "case log::error : return \"error\";\n"
20250 "case log::severe: return \"severe\";\n"
20251 "case log::extra_critical:\n"
20253 " return \"extra critical\";\n"
20257 Alignment
.SpaceBeforeCaseColon
= true;
20258 verifyFormat("switch (level) {\n"
20259 "case log::info : return \"info\";\n"
20260 "case log::warning : return \"warning\";\n"
20261 "case log::error :\n"
20262 "default : return \"default\";\n"
20267 TEST_F(FormatTest
, AlignWithLineBreaks
) {
20268 auto Style
= getLLVMStyleWithColumns(120);
20270 EXPECT_EQ(Style
.AlignConsecutiveAssignments
,
20271 FormatStyle::AlignConsecutiveStyle(
20272 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20273 /*AcrossComments=*/false, /*AlignCompound=*/false,
20274 /*AlignFunctionDeclarations=*/false,
20275 /*AlignFunctionPointers=*/false,
20276 /*PadOperators=*/true}));
20277 EXPECT_EQ(Style
.AlignConsecutiveDeclarations
,
20278 FormatStyle::AlignConsecutiveStyle(
20279 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20280 /*AcrossComments=*/false, /*AlignCompound=*/false,
20281 /*AlignFunctionDeclarations=*/true,
20282 /*AlignFunctionPointers=*/false,
20283 /*PadOperators=*/false}));
20284 verifyFormat("void foo() {\n"
20285 " int myVar = 5;\n"
20286 " double x = 3.14;\n"
20287 " auto str = \"Hello \"\n"
20289 " auto s = \"Hello \"\n"
20294 // clang-format off
20295 verifyFormat("void foo() {\n"
20296 " const int capacityBefore = Entries.capacity();\n"
20297 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20298 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20299 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20300 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20305 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20306 verifyFormat("void foo() {\n"
20307 " int myVar = 5;\n"
20308 " double x = 3.14;\n"
20309 " auto str = \"Hello \"\n"
20311 " auto s = \"Hello \"\n"
20316 // clang-format off
20317 verifyFormat("void foo() {\n"
20318 " const int capacityBefore = Entries.capacity();\n"
20319 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20320 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20321 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20322 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20327 Style
.AlignConsecutiveAssignments
.Enabled
= false;
20328 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20329 verifyFormat("void foo() {\n"
20330 " int myVar = 5;\n"
20331 " double x = 3.14;\n"
20332 " auto str = \"Hello \"\n"
20334 " auto s = \"Hello \"\n"
20339 // clang-format off
20340 verifyFormat("void foo() {\n"
20341 " const int capacityBefore = Entries.capacity();\n"
20342 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20343 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20344 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20345 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20350 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20351 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20353 verifyFormat("void foo() {\n"
20354 " int myVar = 5;\n"
20355 " double x = 3.14;\n"
20356 " auto str = \"Hello \"\n"
20358 " auto s = \"Hello \"\n"
20363 // clang-format off
20364 verifyFormat("void foo() {\n"
20365 " const int capacityBefore = Entries.capacity();\n"
20366 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20367 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20368 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20369 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20374 Style
= getLLVMStyleWithColumns(20);
20375 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20376 Style
.IndentWidth
= 4;
20378 verifyFormat("void foo() {\n"
20387 verifyFormat("unsigned i = 0;\n"
20393 Style
.ColumnLimit
= 120;
20395 // clang-format off
20396 verifyFormat("void SomeFunc() {\n"
20397 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20398 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20399 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20400 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20401 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20402 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20407 Style
.BinPackArguments
= false;
20409 // clang-format off
20410 verifyFormat("void SomeFunc() {\n"
20411 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20412 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20413 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20414 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20415 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20416 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20422 TEST_F(FormatTest
, AlignWithInitializerPeriods
) {
20423 auto Style
= getLLVMStyleWithColumns(60);
20425 verifyFormat("void foo1(void) {\n"
20426 " BYTE p[1] = 1;\n"
20427 " A B = {.one_foooooooooooooooo = 2,\n"
20428 " .two_fooooooooooooo = 3,\n"
20429 " .three_fooooooooooooo = 4};\n"
20430 " BYTE payload = 2;\n"
20434 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20435 Style
.AlignConsecutiveDeclarations
.Enabled
= false;
20436 verifyFormat("void foo2(void) {\n"
20437 " BYTE p[1] = 1;\n"
20438 " A B = {.one_foooooooooooooooo = 2,\n"
20439 " .two_fooooooooooooo = 3,\n"
20440 " .three_fooooooooooooo = 4};\n"
20441 " BYTE payload = 2;\n"
20445 Style
.AlignConsecutiveAssignments
.Enabled
= false;
20446 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20447 verifyFormat("void foo3(void) {\n"
20448 " BYTE p[1] = 1;\n"
20449 " A B = {.one_foooooooooooooooo = 2,\n"
20450 " .two_fooooooooooooo = 3,\n"
20451 " .three_fooooooooooooo = 4};\n"
20452 " BYTE payload = 2;\n"
20456 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20457 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20458 verifyFormat("void foo4(void) {\n"
20459 " BYTE p[1] = 1;\n"
20460 " A B = {.one_foooooooooooooooo = 2,\n"
20461 " .two_fooooooooooooo = 3,\n"
20462 " .three_fooooooooooooo = 4};\n"
20463 " BYTE payload = 2;\n"
20468 TEST_F(FormatTest
, LinuxBraceBreaking
) {
20469 FormatStyle LinuxBraceStyle
= getLLVMStyle();
20470 LinuxBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Linux
;
20471 verifyFormat("namespace a\n"
20484 " void g() { return; }\n"
20489 "} // namespace a",
20491 verifyFormat("enum X {\n"
20495 verifyFormat("struct S {\n"
20503 " MyFavoriteType Value;\n"
20509 TEST_F(FormatTest
, MozillaBraceBreaking
) {
20510 FormatStyle MozillaBraceStyle
= getLLVMStyle();
20511 MozillaBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Mozilla
;
20512 MozillaBraceStyle
.FixNamespaceComments
= false;
20513 verifyFormat("namespace a {\n"
20523 " void g() { return; }\n"
20537 MozillaBraceStyle
);
20538 verifyFormat("struct S\n"
20548 " MyFavoriteType Value;\n"
20551 MozillaBraceStyle
);
20554 TEST_F(FormatTest
, StroustrupBraceBreaking
) {
20555 FormatStyle StroustrupBraceStyle
= getLLVMStyle();
20556 StroustrupBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
20557 verifyFormat("namespace a {\n"
20566 " void g() { return; }\n"
20571 "} // namespace a",
20572 StroustrupBraceStyle
);
20574 verifyFormat("void foo()\n"
20583 StroustrupBraceStyle
);
20585 verifyFormat("#ifdef _DEBUG\n"
20586 "int foo(int i = 0)\n"
20588 "int foo(int i = 5)\n"
20593 StroustrupBraceStyle
);
20595 verifyFormat("void foo() {}\n"
20605 StroustrupBraceStyle
);
20607 verifyFormat("void foobar() { int i = 5; }\n"
20611 "void bar() { foobar(); }\n"
20613 StroustrupBraceStyle
);
20616 TEST_F(FormatTest
, AllmanBraceBreaking
) {
20617 FormatStyle AllmanBraceStyle
= getLLVMStyle();
20618 AllmanBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
20620 verifyFormat("namespace a\n"
20624 "} // namespace a",
20632 verifyFormat("namespace a\n"
20644 " void g() { return; }\n"
20653 "} // namespace a",
20656 verifyFormat("void f()\n"
20662 " else if (false)\n"
20673 verifyFormat("void f()\n"
20675 " for (int i = 0; i < 10; ++i)\n"
20686 " } while (false)\n"
20690 verifyFormat("void f(int a)\n"
20710 verifyFormat("enum X\n"
20715 verifyFormat("enum X\n"
20721 verifyFormat("@interface BSApplicationController ()\n"
20724 " id _extraIvar;\n"
20729 verifyFormat("#ifdef _DEBUG\n"
20730 "int foo(int i = 0)\n"
20732 "int foo(int i = 5)\n"
20739 verifyFormat("void foo() {}\n"
20751 verifyFormat("void foobar() { int i = 5; }\n"
20755 "void bar() { foobar(); }\n"
20759 EXPECT_EQ(AllmanBraceStyle
.AllowShortLambdasOnASingleLine
,
20760 FormatStyle::SLS_All
);
20762 verifyFormat("[](int i) { return i + 2; };\n"
20763 "[](int i, int j)\n"
20765 " auto x = i + j;\n"
20766 " auto y = i * j;\n"
20771 " auto shortLambda = [](int i) { return i + 2; };\n"
20772 " auto longLambda = [](int i, int j)\n"
20774 " auto x = i + j;\n"
20775 " auto y = i * j;\n"
20781 AllmanBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
20783 verifyFormat("[](int i)\n"
20787 "[](int i, int j)\n"
20789 " auto x = i + j;\n"
20790 " auto y = i * j;\n"
20795 " auto shortLambda = [](int i)\n"
20799 " auto longLambda = [](int i, int j)\n"
20801 " auto x = i + j;\n"
20802 " auto y = i * j;\n"
20809 AllmanBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_All
;
20811 // This shouldn't affect ObjC blocks..
20812 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20817 verifyFormat("void (^block)(void) = ^{\n"
20822 // .. or dict literals.
20823 verifyFormat("void f()\n"
20826 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20829 verifyFormat("void f()\n"
20832 " [object someMethod:@{a : @\"b\"}];\n"
20835 verifyFormat("int f()\n"
20841 AllmanBraceStyle
.ColumnLimit
= 19;
20842 verifyFormat("void f() { int i; }", AllmanBraceStyle
);
20843 AllmanBraceStyle
.ColumnLimit
= 18;
20844 verifyFormat("void f()\n"
20849 AllmanBraceStyle
.ColumnLimit
= 80;
20851 FormatStyle BreakBeforeBraceShortIfs
= AllmanBraceStyle
;
20852 BreakBeforeBraceShortIfs
.AllowShortIfStatementsOnASingleLine
=
20853 FormatStyle::SIS_WithoutElse
;
20854 BreakBeforeBraceShortIfs
.AllowShortLoopsOnASingleLine
= true;
20855 verifyFormat("void f(bool b)\n"
20862 BreakBeforeBraceShortIfs
);
20863 verifyFormat("void f(bool b)\n"
20865 " if constexpr (b)\n"
20870 BreakBeforeBraceShortIfs
);
20871 verifyFormat("void f(bool b)\n"
20873 " if CONSTEXPR (b)\n"
20878 BreakBeforeBraceShortIfs
);
20879 verifyFormat("void f(bool b)\n"
20881 " if (b) return;\n"
20883 BreakBeforeBraceShortIfs
);
20884 verifyFormat("void f(bool b)\n"
20886 " if constexpr (b) return;\n"
20888 BreakBeforeBraceShortIfs
);
20889 verifyFormat("void f(bool b)\n"
20891 " if CONSTEXPR (b) return;\n"
20893 BreakBeforeBraceShortIfs
);
20894 verifyFormat("void f(bool b)\n"
20901 BreakBeforeBraceShortIfs
);
20904 TEST_F(FormatTest
, WhitesmithsBraceBreaking
) {
20905 FormatStyle WhitesmithsBraceStyle
= getLLVMStyleWithColumns(0);
20906 WhitesmithsBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
20908 // Make a few changes to the style for testing purposes
20909 WhitesmithsBraceStyle
.AllowShortFunctionsOnASingleLine
=
20910 FormatStyle::SFS_Empty
;
20911 WhitesmithsBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
20913 // FIXME: this test case can't decide whether there should be a blank line
20914 // after the ~D() line or not. It adds one if one doesn't exist in the test
20915 // and it removes the line if one exists.
20917 verifyFormat("class A;\n"
20933 " } // namespace B",
20934 WhitesmithsBraceStyle);
20937 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_None
;
20938 verifyFormat("namespace a\n"
20959 " } // namespace a",
20960 WhitesmithsBraceStyle
);
20962 verifyFormat("namespace a\n"
20985 " } // namespace b\n"
20986 " } // namespace a",
20987 WhitesmithsBraceStyle
);
20989 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_Inner
;
20990 verifyFormat("namespace a\n"
21013 " } // namespace b\n"
21014 " } // namespace a",
21015 WhitesmithsBraceStyle
);
21017 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_All
;
21018 verifyFormat("namespace a\n"
21041 " } // namespace b\n"
21042 " } // namespace a",
21043 WhitesmithsBraceStyle
);
21045 verifyFormat("void f()\n"
21051 " else if (false)\n"
21060 WhitesmithsBraceStyle
);
21062 verifyFormat("void f()\n"
21064 " for (int i = 0; i < 10; ++i)\n"
21075 " } while (false)\n"
21077 WhitesmithsBraceStyle
);
21079 WhitesmithsBraceStyle
.IndentCaseLabels
= true;
21080 verifyFormat("void switchTest1(int a)\n"
21090 WhitesmithsBraceStyle
);
21092 verifyFormat("void switchTest2(int a)\n"
21110 WhitesmithsBraceStyle
);
21112 verifyFormat("void switchTest3(int a)\n"
21128 WhitesmithsBraceStyle
);
21130 WhitesmithsBraceStyle
.IndentCaseLabels
= false;
21132 verifyFormat("void switchTest4(int a)\n"
21142 WhitesmithsBraceStyle
);
21144 verifyFormat("void switchTest5(int a)\n"
21163 WhitesmithsBraceStyle
);
21165 verifyFormat("void switchTest6(int a)\n"
21181 WhitesmithsBraceStyle
);
21183 verifyFormat("enum X\n"
21185 " Y = 0, // testing\n"
21187 WhitesmithsBraceStyle
);
21189 verifyFormat("enum X\n"
21193 WhitesmithsBraceStyle
);
21194 verifyFormat("enum X\n"
21199 WhitesmithsBraceStyle
);
21201 verifyFormat("@interface BSApplicationController ()\n"
21204 " id _extraIvar;\n"
21207 WhitesmithsBraceStyle
);
21209 verifyFormat("#ifdef _DEBUG\n"
21210 "int foo(int i = 0)\n"
21212 "int foo(int i = 5)\n"
21217 WhitesmithsBraceStyle
);
21219 verifyFormat("void foo() {}\n"
21229 WhitesmithsBraceStyle
);
21231 verifyFormat("void foobar()\n"
21243 WhitesmithsBraceStyle
);
21245 // This shouldn't affect ObjC blocks..
21246 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21250 WhitesmithsBraceStyle
);
21251 verifyFormat("void (^block)(void) = ^{\n"
21255 WhitesmithsBraceStyle
);
21256 // .. or dict literals.
21257 verifyFormat("void f()\n"
21259 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21261 WhitesmithsBraceStyle
);
21263 verifyFormat("int f()\n"
21267 WhitesmithsBraceStyle
);
21269 FormatStyle BreakBeforeBraceShortIfs
= WhitesmithsBraceStyle
;
21270 BreakBeforeBraceShortIfs
.AllowShortIfStatementsOnASingleLine
=
21271 FormatStyle::SIS_OnlyFirstIf
;
21272 BreakBeforeBraceShortIfs
.AllowShortLoopsOnASingleLine
= true;
21273 verifyFormat("void f(bool b)\n"
21280 BreakBeforeBraceShortIfs
);
21281 verifyFormat("void f(bool b)\n"
21283 " if (b) return;\n"
21285 BreakBeforeBraceShortIfs
);
21286 verifyFormat("void f(bool b)\n"
21293 BreakBeforeBraceShortIfs
);
21296 TEST_F(FormatTest
, GNUBraceBreaking
) {
21297 FormatStyle GNUBraceStyle
= getLLVMStyle();
21298 GNUBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_GNU
;
21299 verifyFormat("namespace a\n"
21315 " void g() { return; }\n"
21317 "} // namespace a",
21320 verifyFormat("void f()\n"
21326 " else if (false)\n"
21337 verifyFormat("void f()\n"
21339 " for (int i = 0; i < 10; ++i)\n"
21351 " while (false);\n"
21355 verifyFormat("void f(int a)\n"
21375 verifyFormat("enum X\n"
21381 verifyFormat("@interface BSApplicationController ()\n"
21384 " id _extraIvar;\n"
21389 verifyFormat("#ifdef _DEBUG\n"
21390 "int foo(int i = 0)\n"
21392 "int foo(int i = 5)\n"
21399 verifyFormat("void foo() {}\n"
21411 verifyFormat("void foobar() { int i = 5; }\n"
21415 "void bar() { foobar(); }\n"
21420 TEST_F(FormatTest
, WebKitBraceBreaking
) {
21421 FormatStyle WebKitBraceStyle
= getLLVMStyle();
21422 WebKitBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_WebKit
;
21423 WebKitBraceStyle
.FixNamespaceComments
= false;
21424 verifyFormat("namespace a {\n"
21433 " void g() { return; }\n"
21446 verifyFormat("struct S {\n"
21453 " MyFavoriteType Value;\n"
21459 TEST_F(FormatTest
, CatchExceptionReferenceBinding
) {
21460 verifyFormat("void f() {\n"
21462 " } catch (const Exception &e) {\n"
21467 TEST_F(FormatTest
, CatchAlignArrayOfStructuresRightAlignment
) {
21468 auto Style
= getLLVMStyle();
21469 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
21470 verifyNoCrash("f({\n"
21471 "table({}, table({{\"\", false}}, {}))\n"
21475 Style
.AlignConsecutiveAssignments
.Enabled
= true;
21476 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
21477 verifyFormat("struct test demo[] = {\n"
21478 " {56, 23, \"hello\"},\n"
21479 " {-1, 93463, \"world\"},\n"
21480 " { 7, 5, \"!!\"}\n"
21484 verifyFormat("struct test demo[] = {\n"
21485 " {56, 23, \"hello\"}, // first line\n"
21486 " {-1, 93463, \"world\"}, // second line\n"
21487 " { 7, 5, \"!!\"} // third line\n"
21491 verifyFormat("struct test demo[4] = {\n"
21492 " { 56, 23, 21, \"oh\"}, // first line\n"
21493 " { -1, 93463, 22, \"my\"}, // second line\n"
21494 " { 7, 5, 1, \"goodness\"} // third line\n"
21495 " {234, 5, 1, \"gracious\"} // fourth line\n"
21499 verifyFormat("struct test demo[3] = {\n"
21500 " {56, 23, \"hello\"},\n"
21501 " {-1, 93463, \"world\"},\n"
21502 " { 7, 5, \"!!\"}\n"
21506 verifyFormat("struct test demo[3] = {\n"
21507 " {int{56}, 23, \"hello\"},\n"
21508 " {int{-1}, 93463, \"world\"},\n"
21509 " { int{7}, 5, \"!!\"}\n"
21513 verifyFormat("struct test demo[] = {\n"
21514 " {56, 23, \"hello\"},\n"
21515 " {-1, 93463, \"world\"},\n"
21516 " { 7, 5, \"!!\"},\n"
21520 verifyFormat("test demo[] = {\n"
21521 " {56, 23, \"hello\"},\n"
21522 " {-1, 93463, \"world\"},\n"
21523 " { 7, 5, \"!!\"},\n"
21527 verifyFormat("demo = std::array<struct test, 3>{\n"
21528 " test{56, 23, \"hello\"},\n"
21529 " test{-1, 93463, \"world\"},\n"
21530 " test{ 7, 5, \"!!\"},\n"
21534 verifyFormat("test demo[] = {\n"
21535 " {56, 23, \"hello\"},\n"
21537 " {-1, 93463, \"world\"},\n"
21539 " { 7, 5, \"!!\"}\n"
21544 "test demo[] = {\n"
21546 " \"hello world i am a very long line that really, in any\"\n"
21547 " \"just world, ought to be split over multiple lines\"},\n"
21548 " {-1, 93463, \"world\"},\n"
21549 " {56, 5, \"!!\"}\n"
21553 verifyNoCrash("Foo f[] = {\n"
21558 verifyNoCrash("Foo foo[] = {\n"
21560 " [1] { 1, 1, },\n"
21561 " [2] { 1, 1, },\n"
21564 verifyNoCrash("test arr[] = {\n"
21565 "#define FOO(i) {i, i},\n"
21566 "SOME_GENERATOR(FOO)\n"
21571 verifyFormat("return GradForUnaryCwise(g, {\n"
21572 " {{\"sign\"}, \"Sign\", "
21573 " {\"x\", \"dy\"}},\n"
21574 " { {\"dx\"}, \"Mul\", {\"dy\""
21579 Style
.Cpp11BracedListStyle
= false;
21580 verifyFormat("struct test demo[] = {\n"
21581 " { 56, 23, \"hello\" },\n"
21582 " { -1, 93463, \"world\" },\n"
21583 " { 7, 5, \"!!\" }\n"
21586 Style
.Cpp11BracedListStyle
= true;
21588 Style
.ColumnLimit
= 0;
21590 "test demo[] = {\n"
21591 " {56, 23, \"hello world i am a very long line that really, "
21592 "in any just world, ought to be split over multiple lines\"},\n"
21598 "test demo[] = {{56, 23, \"hello world i am a very long line "
21599 "that really, in any just world, ought to be split over multiple "
21600 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21603 Style
.ColumnLimit
= 80;
21604 verifyFormat("test demo[] = {\n"
21605 " {56, 23, /* a comment */ \"hello\"},\n"
21606 " {-1, 93463, \"world\"},\n"
21607 " { 7, 5, \"!!\"}\n"
21611 verifyFormat("test demo[] = {\n"
21612 " {56, 23, \"hello\"},\n"
21613 " {-1, 93463, \"world\" /* comment here */},\n"
21614 " { 7, 5, \"!!\"}\n"
21618 verifyFormat("test demo[] = {\n"
21619 " {56, /* a comment */ 23, \"hello\"},\n"
21620 " {-1, 93463, \"world\"},\n"
21621 " { 7, 5, \"!!\"}\n"
21625 Style
.ColumnLimit
= 20;
21626 verifyFormat("demo = std::array<\n"
21627 " struct test, 3>{\n"
21632 " \"am a very \"\n"
21633 " \"long line \"\n"
21644 " test{-1, 93463,\n"
21649 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21650 "i am a very long line that really, in any just world, ought "
21651 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21652 "test{7, 5, \"!!\"},};",
21654 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21655 Style
= getLLVMStyleWithColumns(50);
21656 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
21657 verifyFormat("static A x = {\n"
21658 " {{init1, init2, init3, init4},\n"
21659 " {init1, init2, init3, init4}}\n"
21662 // TODO: Fix the indentations below when this option is fully functional.
21664 verifyFormat("int a[][] = {\n"
21672 Style
.ColumnLimit
= 100;
21674 "test demo[] = {\n"
21676 " \"hello world i am a very long line that really, in any just world"
21677 ", ought to be split over \"\n"
21678 " \"multiple lines\" },\n"
21679 " {-1, 93463, \"world\"},\n"
21680 " { 7, 5, \"!!\"},\n"
21682 "test demo[] = {{56, 23, \"hello world i am a very long line "
21683 "that really, in any just world, ought to be split over multiple "
21684 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21687 Style
= getLLVMStyleWithColumns(50);
21688 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
21689 verifyFormat("struct test demo[] = {\n"
21690 " {56, 23, \"hello\"},\n"
21691 " {-1, 93463, \"world\"},\n"
21692 " { 7, 5, \"!!\"}\n"
21695 " {{init1, init2, init3, init4},\n"
21696 " {init1, init2, init3, init4}}\n"
21699 Style
.ColumnLimit
= 100;
21700 Style
.AlignConsecutiveAssignments
.AcrossComments
= true;
21701 Style
.AlignConsecutiveDeclarations
.AcrossComments
= true;
21702 verifyFormat("struct test demo[] = {\n"
21703 " {56, 23, \"hello\"},\n"
21704 " {-1, 93463, \"world\"},\n"
21705 " { 7, 5, \"!!\"}\n"
21707 "struct test demo[4] = {\n"
21708 " { 56, 23, 21, \"oh\"}, // first line\n"
21709 " { -1, 93463, 22, \"my\"}, // second line\n"
21710 " { 7, 5, 1, \"goodness\"} // third line\n"
21711 " {234, 5, 1, \"gracious\"} // fourth line\n"
21715 "test demo[] = {\n"
21717 " \"hello world i am a very long line that really, in any just world"
21718 ", ought to be split over \"\n"
21719 " \"multiple lines\", 23},\n"
21720 " {-1, \"world\", 93463},\n"
21721 " { 7, \"!!\", 5},\n"
21723 "test demo[] = {{56, \"hello world i am a very long line "
21724 "that really, in any just world, ought to be split over multiple "
21725 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21729 TEST_F(FormatTest
, CatchAlignArrayOfStructuresLeftAlignment
) {
21730 auto Style
= getLLVMStyle();
21731 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
21732 /* FIXME: This case gets misformatted.
21733 verifyFormat("auto foo = Items{\n"
21734 " Section{0, bar(), },\n"
21735 " Section{1, boo() }\n"
21739 verifyFormat("auto foo = Items{\n"
21745 verifyFormat("struct test demo[] = {\n"
21746 " {56, 23, \"hello\"},\n"
21747 " {-1, 93463, \"world\"},\n"
21748 " {7, 5, \"!!\" }\n"
21751 verifyFormat("struct test demo[] = {\n"
21752 " {56, 23, \"hello\"}, // first line\n"
21753 " {-1, 93463, \"world\"}, // second line\n"
21754 " {7, 5, \"!!\" } // third line\n"
21757 verifyFormat("struct test demo[4] = {\n"
21758 " {56, 23, 21, \"oh\" }, // first line\n"
21759 " {-1, 93463, 22, \"my\" }, // second line\n"
21760 " {7, 5, 1, \"goodness\"} // third line\n"
21761 " {234, 5, 1, \"gracious\"} // fourth line\n"
21764 verifyFormat("struct test demo[3] = {\n"
21765 " {56, 23, \"hello\"},\n"
21766 " {-1, 93463, \"world\"},\n"
21767 " {7, 5, \"!!\" }\n"
21771 verifyFormat("struct test demo[3] = {\n"
21772 " {int{56}, 23, \"hello\"},\n"
21773 " {int{-1}, 93463, \"world\"},\n"
21774 " {int{7}, 5, \"!!\" }\n"
21777 verifyFormat("struct test demo[] = {\n"
21778 " {56, 23, \"hello\"},\n"
21779 " {-1, 93463, \"world\"},\n"
21780 " {7, 5, \"!!\" },\n"
21783 verifyFormat("test demo[] = {\n"
21784 " {56, 23, \"hello\"},\n"
21785 " {-1, 93463, \"world\"},\n"
21786 " {7, 5, \"!!\" },\n"
21789 verifyFormat("demo = std::array<struct test, 3>{\n"
21790 " test{56, 23, \"hello\"},\n"
21791 " test{-1, 93463, \"world\"},\n"
21792 " test{7, 5, \"!!\" },\n"
21795 verifyFormat("test demo[] = {\n"
21796 " {56, 23, \"hello\"},\n"
21798 " {-1, 93463, \"world\"},\n"
21800 " {7, 5, \"!!\" }\n"
21804 "test demo[] = {\n"
21806 " \"hello world i am a very long line that really, in any\"\n"
21807 " \"just world, ought to be split over multiple lines\"},\n"
21808 " {-1, 93463, \"world\" },\n"
21809 " {56, 5, \"!!\" }\n"
21813 verifyNoCrash("Foo f[] = {\n"
21818 verifyNoCrash("Foo foo[] = {\n"
21820 " [1] { 1, 1, },\n"
21821 " [2] { 1, 1, },\n"
21824 verifyNoCrash("test arr[] = {\n"
21825 "#define FOO(i) {i, i},\n"
21826 "SOME_GENERATOR(FOO)\n"
21831 verifyFormat("return GradForUnaryCwise(g, {\n"
21832 " {{\"sign\"}, \"Sign\", {\"x\", "
21834 " {{\"dx\"}, \"Mul\", "
21835 "{\"dy\", \"sign\"}},\n"
21839 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
21840 verifyFormat("#define FOO \\\n"
21841 " int foo[][2] = { \\\n"
21846 Style
.Cpp11BracedListStyle
= false;
21847 verifyFormat("struct test demo[] = {\n"
21848 " { 56, 23, \"hello\" },\n"
21849 " { -1, 93463, \"world\" },\n"
21850 " { 7, 5, \"!!\" }\n"
21853 Style
.Cpp11BracedListStyle
= true;
21855 Style
.ColumnLimit
= 0;
21857 "test demo[] = {\n"
21858 " {56, 23, \"hello world i am a very long line that really, in any "
21859 "just world, ought to be split over multiple lines\"},\n"
21860 " {-1, 93463, \"world\" "
21865 "test demo[] = {{56, 23, \"hello world i am a very long line "
21866 "that really, in any just world, ought to be split over multiple "
21867 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21870 Style
.ColumnLimit
= 80;
21871 verifyFormat("test demo[] = {\n"
21872 " {56, 23, /* a comment */ \"hello\"},\n"
21873 " {-1, 93463, \"world\" },\n"
21874 " {7, 5, \"!!\" }\n"
21878 verifyFormat("test demo[] = {\n"
21879 " {56, 23, \"hello\" },\n"
21880 " {-1, 93463, \"world\" /* comment here */},\n"
21881 " {7, 5, \"!!\" }\n"
21885 verifyFormat("test demo[] = {\n"
21886 " {56, /* a comment */ 23, \"hello\"},\n"
21887 " {-1, 93463, \"world\"},\n"
21888 " {7, 5, \"!!\" }\n"
21891 verifyFormat("Foo foo = {\n"
21897 Style
.ColumnLimit
= 20;
21898 // FIXME: unstable test case
21900 "demo = std::array<\n"
21901 " struct test, 3>{\n"
21906 " \"am a very \"\n"
21907 " \"long line \"\n"
21918 " test{-1, 93463,\n"
21923 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21924 "i am a very long line that really, in any just world, ought "
21925 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21926 "test{7, 5, \"!!\"},};",
21929 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21930 Style
= getLLVMStyleWithColumns(50);
21931 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
21932 verifyFormat("static A x = {\n"
21933 " {{init1, init2, init3, init4},\n"
21934 " {init1, init2, init3, init4}}\n"
21937 Style
.ColumnLimit
= 100;
21939 "test demo[] = {\n"
21941 " \"hello world i am a very long line that really, in any just world"
21942 ", ought to be split over \"\n"
21943 " \"multiple lines\" },\n"
21944 " {-1, 93463, \"world\"},\n"
21945 " {7, 5, \"!!\" },\n"
21947 "test demo[] = {{56, 23, \"hello world i am a very long line "
21948 "that really, in any just world, ought to be split over multiple "
21949 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21952 Style
.ColumnLimit
= 25;
21953 verifyNoCrash("Type foo{\n"
21962 verifyNoCrash("Type object[X][Y] = {\n"
21963 " {{val}, {val}, {val}},\n"
21964 " {{val}, {val}, // some comment\n"
21969 Style
.ColumnLimit
= 120;
21972 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
21973 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
21974 "\"00000000000000000000000000000000000000000000000000000000"
21975 "00000000000000000000000000000000000000000000000000000000\" },\n"
21979 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
21980 Style
.SpacesInParensOptions
.Other
= true;
21981 verifyFormat("Foo foo[] = {\n"
21988 TEST_F(FormatTest
, UnderstandsPragmas
) {
21989 verifyFormat("#pragma omp reduction(| : var)");
21990 verifyFormat("#pragma omp reduction(+ : var)");
21992 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21993 "(including parentheses).",
21994 "#pragma mark Any non-hyphenated or hyphenated string "
21995 "(including parentheses).");
21997 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21998 "(including parentheses).",
21999 "#pragma mark Any non-hyphenated or hyphenated string "
22000 "(including parentheses).");
22002 verifyFormat("#pragma comment(linker, \\\n"
22003 " \"argument\" \\\n"
22005 "#pragma comment(linker, \\\n"
22006 " \"argument\" \\\n"
22008 getStyleWithColumns(
22009 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp
), 32));
22012 TEST_F(FormatTest
, UnderstandsPragmaOmpTarget
) {
22013 verifyFormat("#pragma omp target map(to : var)");
22014 verifyFormat("#pragma omp target map(to : var[ : N])");
22015 verifyFormat("#pragma omp target map(to : var[0 : N])");
22016 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22019 "#pragma omp target \\\n"
22020 " reduction(+ : var) \\\n"
22021 " map(to : A[0 : N]) \\\n"
22022 " map(to : B[0 : N]) \\\n"
22023 " map(from : C[0 : N]) \\\n"
22024 " firstprivate(i) \\\n"
22025 " firstprivate(j) \\\n"
22026 " firstprivate(k)",
22027 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22028 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22029 getLLVMStyleWithColumns(26));
22032 TEST_F(FormatTest
, UnderstandPragmaOption
) {
22033 verifyFormat("#pragma option -C -A");
22035 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
22038 TEST_F(FormatTest
, UnderstandPragmaRegion
) {
22039 auto Style
= getLLVMStyleWithColumns(0);
22040 verifyFormat("#pragma region TEST(FOO : BAR)", Style
);
22041 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style
);
22044 TEST_F(FormatTest
, OptimizeBreakPenaltyVsExcess
) {
22045 FormatStyle Style
= getLLVMStyleWithColumns(20);
22048 verifyFormat("/*\n"
22053 " *\t9012345 /8901\n"
22056 verifyFormat("/*\n"
22061 " *345678\t/8901\n"
22065 verifyFormat("int a; // the\n"
22068 verifyNoChange("int a; /* first line\n"
22074 verifyFormat("int a; // first line\n"
22078 "int a; // first line\n"
22079 " // second line\n"
22083 Style
.PenaltyExcessCharacter
= 90;
22084 verifyFormat("int a; // the comment", Style
);
22085 verifyFormat("int a; // the comment\n"
22087 "int a; // the comment aaa", Style
);
22088 verifyNoChange("int a; /* first line\n"
22093 verifyFormat("int a; // first line\n"
22094 " // second line\n"
22097 // FIXME: Investigate why this is not getting the same layout as the test
22099 verifyFormat("int a; /* first line\n"
22103 "int a; /* first line second line third line"
22107 verifyFormat("// foo bar baz bazfoo\n"
22108 "// foo bar foo bar",
22109 "// foo bar baz bazfoo\n"
22110 "// foo bar foo bar",
22112 verifyFormat("// foo bar baz bazfoo\n"
22113 "// foo bar foo bar",
22114 "// foo bar baz bazfoo\n"
22115 "// foo bar foo bar",
22118 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22120 verifyFormat("// foo bar baz bazfoo\n"
22122 "// foo bar baz bazfoo bar\n"
22126 // FIXME: unstable test case
22127 EXPECT_EQ("// foo bar baz bazfoo\n"
22128 "// foo bar baz bazfoo\n"
22130 format("// foo bar baz bazfoo\n"
22131 "// foo bar baz bazfoo bar\n"
22135 // FIXME: unstable test case
22136 EXPECT_EQ("// foo bar baz bazfoo\n"
22137 "// foo bar baz bazfoo\n"
22139 format("// foo bar baz bazfoo\n"
22140 "// foo bar baz bazfoo bar\n"
22144 // Make sure we do not keep protruding characters if strict mode reflow is
22145 // cheaper than keeping protruding characters.
22146 Style
.ColumnLimit
= 21;
22147 verifyFormat("// foo foo foo foo\n"
22148 "// foo foo foo foo\n"
22149 "// foo foo foo foo",
22150 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style
);
22152 verifyFormat("int a = /* long block\n"
22155 "int a = /* long block comment */ 42;", Style
);
22158 TEST_F(FormatTest
, BreakPenaltyAfterLParen
) {
22159 FormatStyle Style
= getLLVMStyle();
22160 Style
.ColumnLimit
= 8;
22161 Style
.PenaltyExcessCharacter
= 15;
22162 verifyFormat("int foo(\n"
22163 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22165 Style
.PenaltyBreakOpenParenthesis
= 200;
22166 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22168 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22172 TEST_F(FormatTest
, BreakPenaltyAfterCastLParen
) {
22173 FormatStyle Style
= getLLVMStyle();
22174 Style
.ColumnLimit
= 5;
22175 Style
.PenaltyExcessCharacter
= 150;
22176 verifyFormat("foo((\n"
22177 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22180 Style
.PenaltyBreakOpenParenthesis
= 100'000;
22181 verifyFormat("foo((int)\n"
22182 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22184 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22188 TEST_F(FormatTest
, BreakPenaltyAfterForLoopLParen
) {
22189 FormatStyle Style
= getLLVMStyle();
22190 Style
.ColumnLimit
= 4;
22191 Style
.PenaltyExcessCharacter
= 100;
22192 verifyFormat("for (\n"
22193 " int iiiiiiiiiiiiiiiii =\n"
22195 " iiiiiiiiiiiiiiiii <\n"
22197 " iiiiiiiiiiiiiiiii++) {\n"
22201 Style
.PenaltyBreakOpenParenthesis
= 1250;
22202 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22204 " iiiiiiiiiiiiiiiii <\n"
22206 " iiiiiiiiiiiiiiiii++) {\n"
22209 " int iiiiiiiiiiiiiiiii =\n"
22211 " iiiiiiiiiiiiiiiii <\n"
22213 " iiiiiiiiiiiiiiiii++) {\n"
22218 TEST_F(FormatTest
, BreakPenaltyScopeResolution
) {
22219 FormatStyle Style
= getLLVMStyle();
22220 Style
.ColumnLimit
= 20;
22221 Style
.PenaltyExcessCharacter
= 100;
22222 verifyFormat("unsigned long\n"
22225 Style
.PenaltyBreakScopeResolution
= 10;
22226 verifyFormat("unsigned long foo::\n"
22231 TEST_F(FormatTest
, WorksFor8bitEncodings
) {
22232 // FIXME: unstable test case
22233 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22234 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22235 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22236 "\"\xef\xee\xf0\xf3...\"",
22237 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22238 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22239 "\xef\xee\xf0\xf3...\"",
22240 getLLVMStyleWithColumns(12)));
22243 TEST_F(FormatTest
, HandlesUTF8BOM
) {
22244 verifyFormat("\xef\xbb\xbf");
22245 verifyFormat("\xef\xbb\xbf#include <iostream>");
22246 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22248 auto Style
= getLLVMStyle();
22249 Style
.KeepEmptyLines
.AtStartOfFile
= false;
22250 verifyFormat("\xef\xbb\xbf#include <iostream>",
22251 "\xef\xbb\xbf\n#include <iostream>", Style
);
22254 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22255 #if !defined(_MSC_VER)
22257 TEST_F(FormatTest
, CountsUTF8CharactersProperly
) {
22258 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22259 getLLVMStyleWithColumns(35));
22260 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22261 getLLVMStyleWithColumns(31));
22262 verifyFormat("// Однажды в студёную зимнюю пору...",
22263 getLLVMStyleWithColumns(36));
22264 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22265 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22266 getLLVMStyleWithColumns(39));
22267 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22268 getLLVMStyleWithColumns(35));
22271 TEST_F(FormatTest
, SplitsUTF8Strings
) {
22272 // Non-printable characters' width is currently considered to be the length in
22273 // bytes in UTF8. The characters can be displayed in very different manner
22274 // (zero-width, single width with a substitution glyph, expanded to their code
22275 // (e.g. "<8d>"), so there's no single correct way to handle them.
22276 // FIXME: unstable test case
22277 EXPECT_EQ("\"aaaaÄ\"\n"
22279 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22280 // FIXME: unstable test case
22281 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22283 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22284 // FIXME: unstable test case
22285 EXPECT_EQ("\"Однажды, в \"\n"
22289 format("\"Однажды, в студёную зимнюю пору,\"",
22290 getLLVMStyleWithColumns(13)));
22291 // FIXME: unstable test case
22297 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22298 // FIXME: unstable test case
22299 EXPECT_EQ("\"一\t\"\n"
22306 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
22307 getLLVMStyleWithColumns(11)));
22309 // UTF8 character in an escape sequence.
22310 // FIXME: unstable test case
22311 EXPECT_EQ("\"aaaaaa\"\n"
22313 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22316 TEST_F(FormatTest
, HandlesDoubleWidthCharsInMultiLineStrings
) {
22317 verifyFormat("const char *sssss =\n"
22320 "const char *sssss = \"一二三四五六七八\\\n"
22322 getLLVMStyleWithColumns(30));
22325 TEST_F(FormatTest
, SplitsUTF8LineComments
) {
22326 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22327 verifyFormat("// Я из лесу\n"
22331 "// Я из лесу вышел; был сильный мороз.",
22332 getLLVMStyleWithColumns(13));
22333 verifyFormat("// 一二三\n"
22337 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22340 TEST_F(FormatTest
, SplitsUTF8BlockComments
) {
22341 verifyFormat("/* Гляжу,\n"
22349 "/* Гляжу, поднимается медленно в гору\n"
22350 " * Лошадка, везущая хворосту воз. */",
22351 getLLVMStyleWithColumns(13));
22352 verifyFormat("/* 一二三\n"
22356 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22357 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22360 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22365 TEST_F(FormatTest
, ConstructorInitializerIndentWidth
) {
22366 FormatStyle Style
= getLLVMStyle();
22368 Style
.ConstructorInitializerIndentWidth
= 4;
22370 "SomeClass::Constructor()\n"
22371 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22372 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22375 Style
.ConstructorInitializerIndentWidth
= 2;
22377 "SomeClass::Constructor()\n"
22378 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22379 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22382 Style
.ConstructorInitializerIndentWidth
= 0;
22384 "SomeClass::Constructor()\n"
22385 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22386 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22388 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
22390 "SomeLongTemplateVariableName<\n"
22391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22393 verifyFormat("bool smaller = 1 < "
22394 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22396 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22399 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
22400 verifyFormat("SomeClass::Constructor() :\n"
22401 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22402 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22406 TEST_F(FormatTest
, BreakConstructorInitializersBeforeComma
) {
22407 FormatStyle Style
= getLLVMStyle();
22408 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
22409 Style
.ConstructorInitializerIndentWidth
= 4;
22410 verifyFormat("SomeClass::Constructor()\n"
22415 verifyFormat("SomeClass::Constructor()\n"
22419 Style
.ColumnLimit
= 0;
22420 verifyFormat("SomeClass::Constructor()\n"
22423 verifyFormat("SomeClass::Constructor() noexcept\n"
22426 verifyFormat("SomeClass::Constructor()\n"
22431 verifyFormat("SomeClass::Constructor()\n"
22438 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
22439 verifyFormat("SomeClass::Constructor()\n"
22444 verifyFormat("SomeClass::Constructor()\n"
22448 Style
.ColumnLimit
= 80;
22449 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
22450 Style
.ConstructorInitializerIndentWidth
= 2;
22451 verifyFormat("SomeClass::Constructor()\n"
22457 Style
.ConstructorInitializerIndentWidth
= 0;
22458 verifyFormat("SomeClass::Constructor()\n"
22464 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
22465 Style
.ConstructorInitializerIndentWidth
= 4;
22466 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style
);
22468 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22471 "SomeClass::Constructor()\n"
22472 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22474 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
22475 verifyFormat("SomeClass::Constructor()\n"
22476 " : aaaaaaaa(aaaaaaaa) {}",
22478 verifyFormat("SomeClass::Constructor()\n"
22479 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22482 "SomeClass::Constructor()\n"
22483 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22486 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
22487 Style
.ConstructorInitializerIndentWidth
= 4;
22488 Style
.ColumnLimit
= 60;
22489 verifyFormat("SomeClass::Constructor()\n"
22490 " : aaaaaaaa(aaaaaaaa)\n"
22491 " , aaaaaaaa(aaaaaaaa)\n"
22492 " , aaaaaaaa(aaaaaaaa) {}",
22494 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
22495 verifyFormat("SomeClass::Constructor()\n"
22496 " : aaaaaaaa(aaaaaaaa)\n"
22497 " , aaaaaaaa(aaaaaaaa)\n"
22498 " , aaaaaaaa(aaaaaaaa) {}",
22502 TEST_F(FormatTest
, ConstructorInitializersWithPreprocessorDirective
) {
22503 FormatStyle Style
= getLLVMStyle();
22504 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
22505 Style
.ConstructorInitializerIndentWidth
= 4;
22506 verifyFormat("SomeClass::Constructor()\n"
22510 verifyFormat("SomeClass::Constructor()\n"
22517 Style
.ConstructorInitializerIndentWidth
= 2;
22518 verifyFormat("SomeClass::Constructor()\n"
22525 Style
.ConstructorInitializerIndentWidth
= 0;
22526 verifyFormat("SomeClass::Constructor()\n"
22528 "#ifdef CONDITION\n"
22535 Style
.ConstructorInitializerIndentWidth
= 4;
22536 verifyFormat("SomeClass::Constructor()\n"
22553 verifyFormat("SomeClass::Constructor()\n"
22574 TEST_F(FormatTest
, Destructors
) {
22575 verifyFormat("void F(int &i) { i.~int(); }");
22576 verifyFormat("void F(int &i) { i->~int(); }");
22579 TEST_F(FormatTest
, FormatsWithWebKitStyle
) {
22580 FormatStyle Style
= getWebKitStyle();
22582 // Don't indent in outer namespaces.
22583 verifyFormat("namespace outer {\n"
22585 "namespace inner {\n"
22587 "} // namespace inner\n"
22588 "} // namespace outer\n"
22589 "namespace other_outer {\n"
22594 // Don't indent case labels.
22595 verifyFormat("switch (variable) {\n"
22598 " doSomething();\n"
22605 // Wrap before binary operators.
22609 " if (aaaaaaaaaaaaaaaa\n"
22610 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22611 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22615 "if (aaaaaaaaaaaaaaaa\n"
22616 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22617 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22622 // Allow functions on a single line.
22623 verifyFormat("void f() { return; }", Style
);
22625 // Allow empty blocks on a single line and insert a space in empty blocks.
22626 verifyFormat("void f() { }", "void f() {}", Style
);
22627 verifyFormat("while (true) { }", "while (true) {}", Style
);
22628 // However, don't merge non-empty short loops.
22629 verifyFormat("while (true) {\n"
22632 "while (true) { continue; }", Style
);
22634 // Constructor initializers are formatted one per line with the "," on the
22636 verifyFormat("Constructor()\n"
22637 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22638 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22639 " aaaaaaaaaaaaaa)\n"
22640 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22644 verifyFormat("SomeClass::Constructor()\n"
22649 verifyFormat("SomeClass::Constructor()\n"
22653 "SomeClass::Constructor():a(a){}", Style
);
22654 verifyFormat("SomeClass::Constructor()\n"
22661 verifyFormat("SomeClass::Constructor()\n"
22669 // Access specifiers should be aligned left.
22670 verifyFormat("class C {\n"
22676 // Do not align comments.
22677 verifyFormat("int a; // Do not\n"
22678 "double b; // align comments.",
22681 // Do not align operands.
22682 verifyFormat("ASSERT(aaaa\n"
22684 "ASSERT ( aaaa\n||bbbb);", Style
);
22686 // Accept input's line breaks.
22687 verifyFormat("if (aaaaaaaaaaaaaaa\n"
22688 " || bbbbbbbbbbbbbbb) {\n"
22691 "if (aaaaaaaaaaaaaaa\n"
22692 "|| bbbbbbbbbbbbbbb) { i++; }",
22694 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22697 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style
);
22699 // Don't automatically break all macro definitions (llvm.org/PR17842).
22700 verifyFormat("#define aNumber 10", Style
);
22701 // However, generally keep the line breaks that the user authored.
22702 verifyFormat("#define aNumber \\\n"
22704 "#define aNumber \\\n"
22708 // Keep empty and one-element array literals on a single line.
22709 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22710 " copyItems:YES];",
22711 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22714 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22715 " copyItems:YES];",
22716 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22717 " copyItems:YES];",
22719 // FIXME: This does not seem right, there should be more indentation before
22720 // the array literal's entries. Nested blocks have the same problem.
22721 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22725 " copyItems:YES];",
22726 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22730 " copyItems:YES];",
22733 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22734 " copyItems:YES];",
22735 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22736 " copyItems:YES];",
22739 verifyFormat("[self.a b:c c:d];", Style
);
22740 verifyFormat("[self.a b:c\n"
22747 TEST_F(FormatTest
, FormatsLambdas
) {
22748 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22750 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22751 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22752 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22753 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22754 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22755 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22756 verifyFormat("auto c = [a = [b = 42] {}] {};");
22757 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22758 verifyFormat("int x = f(*+[] {});");
22759 verifyFormat("void f() {\n"
22760 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22762 verifyFormat("void f() {\n"
22763 " other(x.begin(), //\n"
22765 " [&](int, int) { return 1; });\n"
22767 verifyFormat("void f() {\n"
22768 " other.other.other.other.other(\n"
22769 " x.begin(), x.end(),\n"
22770 " [something, rather](int, int, int, int, int, int, int) { "
22775 " other.other.other.other.other(\n"
22776 " x.begin(), x.end(),\n"
22777 " [something, rather](int, int, int, int, int, int, int) {\n"
22781 verifyFormat("SomeFunction([]() { // A cool function...\n"
22784 verifyFormat("SomeFunction([]() {\n"
22788 "SomeFunction([](){\n"
22792 verifyFormat("void f() {\n"
22793 " SomeFunction([](decltype(x), A *a) {});\n"
22794 " SomeFunction([](typeof(x), A *a) {});\n"
22795 " SomeFunction([](_Atomic(x), A *a) {});\n"
22796 " SomeFunction([](__underlying_type(x), A *a) {});\n"
22798 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22799 " [](const aaaaaaaaaa &a) { return a; });");
22800 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22801 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
22803 verifyFormat("Constructor()\n"
22804 " : Field([] { // comment\n"
22807 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22808 " return some_parameter.size();\n"
22810 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22811 " [](const string &s) { return s; };");
22812 verifyFormat("int i = aaaaaa ? 1 //\n"
22816 verifyFormat("llvm::errs() << \"number of twos is \"\n"
22817 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
22818 " return x == 2; // force break\n"
22820 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22821 " [=](int iiiiiiiiiiii) {\n"
22822 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22823 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
22825 getLLVMStyleWithColumns(60));
22827 verifyFormat("SomeFunction({[&] {\n"
22833 verifyFormat("SomeFunction({[&] {\n"
22837 "virtual aaaaaaaaaaaaaaaa(\n"
22838 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22839 " aaaaa aaaaaaaaa);");
22841 // Lambdas with return types.
22842 verifyFormat("int c = []() -> int { return 2; }();");
22843 verifyFormat("int c = []() -> int * { return 2; }();");
22844 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22845 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22846 verifyFormat("foo([]() noexcept -> int {});");
22847 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22848 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22849 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22850 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22851 verifyFormat("[a, a]() -> a<1> {};");
22852 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22853 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22854 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22855 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22856 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22857 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22858 verifyFormat("[]() -> foo<!5> { return {}; };");
22859 verifyFormat("[]() -> foo<~5> { return {}; };");
22860 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22861 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22862 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22863 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22864 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22865 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22866 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22867 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22868 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22869 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22870 verifyFormat("namespace bar {\n"
22872 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22873 "} // namespace bar");
22874 verifyFormat("namespace bar {\n"
22876 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22877 "} // namespace bar");
22878 verifyFormat("namespace bar {\n"
22880 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22881 "} // namespace bar");
22882 verifyFormat("namespace bar {\n"
22884 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22885 "} // namespace bar");
22886 verifyFormat("namespace bar {\n"
22888 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22889 "} // namespace bar");
22890 verifyFormat("namespace bar {\n"
22892 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22893 "} // namespace bar");
22894 verifyFormat("namespace bar {\n"
22896 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22897 "} // namespace bar");
22898 verifyFormat("namespace bar {\n"
22900 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22901 "} // namespace bar");
22902 verifyFormat("namespace bar {\n"
22904 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22905 "} // namespace bar");
22906 verifyFormat("namespace bar {\n"
22908 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22909 "} // namespace bar");
22910 verifyFormat("namespace bar {\n"
22912 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22913 "} // namespace bar");
22914 verifyFormat("namespace bar {\n"
22916 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22917 "} // namespace bar");
22918 verifyFormat("namespace bar {\n"
22920 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22921 "} // namespace bar");
22922 verifyFormat("namespace bar {\n"
22924 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22925 "} // namespace bar");
22926 verifyFormat("namespace bar {\n"
22928 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22929 "} // namespace bar");
22930 verifyFormat("namespace bar {\n"
22932 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22933 "} // namespace bar");
22934 verifyFormat("namespace bar {\n"
22936 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22937 "} // namespace bar");
22938 verifyFormat("namespace bar {\n"
22940 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22941 "} // namespace bar");
22942 verifyFormat("[]() -> a<1> {};");
22943 verifyFormat("[]() -> a<1> { ; };");
22944 verifyFormat("[]() -> a<1> { ; }();");
22945 verifyFormat("[a, a]() -> a<true> {};");
22946 verifyFormat("[]() -> a<true> {};");
22947 verifyFormat("[]() -> a<true> { ; };");
22948 verifyFormat("[]() -> a<true> { ; }();");
22949 verifyFormat("[a, a]() -> a<false> {};");
22950 verifyFormat("[]() -> a<false> {};");
22951 verifyFormat("[]() -> a<false> { ; };");
22952 verifyFormat("[]() -> a<false> { ; }();");
22953 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22954 verifyFormat("namespace bar {\n"
22955 "auto foo{[]() -> foo<false> { ; }};\n"
22956 "} // namespace bar");
22957 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22958 " int j) -> int {\n"
22959 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22962 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22963 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22964 " return aaaaaaaaaaaaaaaaa;\n"
22966 getLLVMStyleWithColumns(70));
22967 verifyFormat("[]() //\n"
22971 verifyFormat("[]() -> Void<T...> {};");
22972 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22973 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22974 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22975 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22976 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22977 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
22978 verifyFormat("return int{[x = x]() { return x; }()};");
22980 // Lambdas with explicit template argument lists.
22982 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22983 verifyFormat("auto L = []<class T>(T) {\n"
22989 verifyFormat("auto L = []<class... T>(T...) {\n"
22995 verifyFormat("auto L = []<typename... T>(T...) {\n"
23001 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23007 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23013 verifyFormat("auto L = []<int... T>(T...) {\n"
23019 verifyFormat("auto L = []<Foo... T>(T...) {\n"
23026 // Lambdas that fit on a single line within an argument list are not forced
23028 verifyFormat("SomeFunction([] {});");
23029 verifyFormat("SomeFunction(0, [] {});");
23030 verifyFormat("SomeFunction([] {}, 0);");
23031 verifyFormat("SomeFunction(0, [] {}, 0);");
23032 verifyFormat("SomeFunction([] { return 0; }, 0);");
23033 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23034 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23035 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23036 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23037 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
23038 // Exceeded column limit. We need to break.
23039 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23040 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
23041 "return 0; }, b);");
23043 // Multiple multi-line lambdas in the same parentheses change indentation
23044 // rules. These lambdas are always forced to start on new lines.
23045 verifyFormat("SomeFunction(\n"
23053 // A multi-line lambda passed as arg0 is always pushed to the next line.
23054 verifyFormat("SomeFunction(\n"
23060 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23061 // the arg0 case above.
23062 auto Style
= getGoogleStyle();
23063 Style
.BinPackArguments
= false;
23064 verifyFormat("SomeFunction(\n"
23071 verifyFormat("SomeFunction(\n"
23078 // A lambda with a very long line forces arg0 to be pushed out irrespective of
23079 // the BinPackArguments value (as long as the code is wide enough).
23081 "something->SomeFunction(\n"
23085 "D0000000000000000000000000000000000000000000000000000000000001();\n"
23089 // A multi-line lambda is pulled up as long as the introducer fits on the
23090 // previous line and there are no further args.
23091 verifyFormat("function(1, [this, that] {\n"
23094 verifyFormat("function([this, that] {\n"
23097 // FIXME: this format is not ideal and we should consider forcing the first
23098 // arg onto its own line.
23099 verifyFormat("function(a, b, c, //\n"
23100 " d, [this, that] {\n"
23104 // Multiple lambdas are treated correctly even when there is a short arg0.
23105 verifyFormat("SomeFunction(\n"
23115 // More complex introducers.
23116 verifyFormat("return [i, args...] {};");
23119 verifyFormat("constexpr char hello[]{\"hello\"};");
23120 verifyFormat("double &operator[](int i) { return 0; }\n"
23122 verifyFormat("std::unique_ptr<int[]> foo() {}");
23123 verifyFormat("int i = a[a][a]->f();");
23124 verifyFormat("int i = (*b)[a]->f();");
23126 // Other corner cases.
23127 verifyFormat("void f() {\n"
23128 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23131 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23133 // Lambdas created through weird macros.
23134 verifyFormat("void f() {\n"
23135 " MACRO((const AA &a) { return 1; });\n"
23136 " MACRO((AA &a) { return 1; });\n"
23139 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23144 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23149 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23154 verifyFormat("auto lambda = []() {\n"
23162 // Lambdas with complex multiline introducers.
23164 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23165 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23166 " -> ::std::unordered_set<\n"
23167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23171 FormatStyle LLVMStyle
= getLLVMStyleWithColumns(60);
23172 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23173 " [](auto n) noexcept [[back_attr]]\n"
23174 " -> std::unordered_map<very_long_type_name_A,\n"
23175 " very_long_type_name_B> {\n"
23176 " really_do_something();\n"
23179 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23180 " [](auto n) constexpr\n"
23181 " -> std::unordered_map<very_long_type_name_A,\n"
23182 " very_long_type_name_B> {\n"
23183 " really_do_something();\n"
23187 FormatStyle DoNotMerge
= getLLVMStyle();
23188 DoNotMerge
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
23189 verifyFormat("auto c = []() {\n"
23192 "auto c = []() { return b; };", DoNotMerge
);
23193 verifyFormat("auto c = []() {\n"
23195 " auto c = []() {};", DoNotMerge
);
23197 FormatStyle MergeEmptyOnly
= getLLVMStyle();
23198 MergeEmptyOnly
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_Empty
;
23199 verifyFormat("auto c = []() {\n"
23202 "auto c = []() {\n"
23206 verifyFormat("auto c = []() {};",
23207 "auto c = []() {\n"
23211 FormatStyle MergeInline
= getLLVMStyle();
23212 MergeInline
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_Inline
;
23213 verifyFormat("auto c = []() {\n"
23216 "auto c = []() { return b; };", MergeInline
);
23217 verifyFormat("function([]() { return b; })", MergeInline
);
23218 verifyFormat("function([]() { return b; }, a)", MergeInline
);
23219 verifyFormat("function(a, []() { return b; })", MergeInline
);
23221 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23222 // AllowShortLambdasOnASingleLine
23223 FormatStyle LLVMWithBeforeLambdaBody
= getLLVMStyle();
23224 LLVMWithBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
23225 LLVMWithBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
23226 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23227 FormatStyle::ShortLambdaStyle::SLS_None
;
23228 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23233 LLVMWithBeforeLambdaBody
);
23234 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23238 LLVMWithBeforeLambdaBody
);
23239 verifyFormat("auto fct_SLS_None = []()\n"
23243 LLVMWithBeforeLambdaBody
);
23244 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23253 LLVMWithBeforeLambdaBody
);
23254 verifyFormat("void Fct() {\n"
23260 LLVMWithBeforeLambdaBody
);
23262 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23263 FormatStyle::ShortLambdaStyle::SLS_Empty
;
23264 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23269 LLVMWithBeforeLambdaBody
);
23270 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23271 LLVMWithBeforeLambdaBody
);
23272 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23273 "ongFunctionName_SLS_Empty(\n"
23275 LLVMWithBeforeLambdaBody
);
23276 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23281 LLVMWithBeforeLambdaBody
);
23282 verifyFormat("auto fct_SLS_Empty = []()\n"
23286 LLVMWithBeforeLambdaBody
);
23287 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23290 " return Call([]() {});\n"
23292 LLVMWithBeforeLambdaBody
);
23293 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23296 " return Call([]() {});\n"
23298 LLVMWithBeforeLambdaBody
);
23300 "FctWithLongLineInLambda_SLS_Empty(\n"
23303 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23304 " AndShouldNotBeConsiderAsInline,\n"
23305 " LambdaBodyMustBeBreak);\n"
23307 LLVMWithBeforeLambdaBody
);
23309 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23310 FormatStyle::ShortLambdaStyle::SLS_Inline
;
23311 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23312 LLVMWithBeforeLambdaBody
);
23313 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23314 LLVMWithBeforeLambdaBody
);
23315 verifyFormat("auto fct_SLS_Inline = []()\n"
23319 LLVMWithBeforeLambdaBody
);
23320 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23322 LLVMWithBeforeLambdaBody
);
23324 "FctWithLongLineInLambda_SLS_Inline(\n"
23327 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23328 " AndShouldNotBeConsiderAsInline,\n"
23329 " LambdaBodyMustBeBreak);\n"
23331 LLVMWithBeforeLambdaBody
);
23332 verifyFormat("FctWithMultipleParams_SLS_Inline("
23333 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23334 " []() { return 17; });",
23335 LLVMWithBeforeLambdaBody
);
23337 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23338 LLVMWithBeforeLambdaBody
);
23340 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23341 FormatStyle::ShortLambdaStyle::SLS_All
;
23342 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23343 LLVMWithBeforeLambdaBody
);
23344 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23345 LLVMWithBeforeLambdaBody
);
23346 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23347 LLVMWithBeforeLambdaBody
);
23348 verifyFormat("FctWithOneParam_SLS_All(\n"
23351 " // A cool function...\n"
23354 LLVMWithBeforeLambdaBody
);
23355 verifyFormat("FctWithMultipleParams_SLS_All("
23356 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23357 " []() { return 17; });",
23358 LLVMWithBeforeLambdaBody
);
23359 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23360 LLVMWithBeforeLambdaBody
);
23361 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23362 LLVMWithBeforeLambdaBody
);
23364 "FctWithLongLineInLambda_SLS_All(\n"
23367 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23368 " AndShouldNotBeConsiderAsInline,\n"
23369 " LambdaBodyMustBeBreak);\n"
23371 LLVMWithBeforeLambdaBody
);
23373 "auto fct_SLS_All = []()\n"
23375 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23376 " AndShouldNotBeConsiderAsInline,\n"
23377 " LambdaBodyMustBeBreak);\n"
23379 LLVMWithBeforeLambdaBody
);
23380 LLVMWithBeforeLambdaBody
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
23381 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23382 LLVMWithBeforeLambdaBody
);
23384 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23389 LLVMWithBeforeLambdaBody
);
23390 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23392 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23397 LLVMWithBeforeLambdaBody
);
23399 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23403 " []() { return SomeValueNotSoLong; });",
23404 LLVMWithBeforeLambdaBody
);
23405 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23409 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23410 "eConsiderAsInline;\n"
23412 LLVMWithBeforeLambdaBody
);
23414 "FctWithLongLineInLambda_SLS_All(\n"
23417 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23418 " AndShouldNotBeConsiderAsInline,\n"
23419 " LambdaBodyMustBeBreak);\n"
23421 LLVMWithBeforeLambdaBody
);
23422 verifyFormat("FctWithTwoParams_SLS_All(\n"
23425 " // A cool function...\n"
23429 LLVMWithBeforeLambdaBody
);
23430 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23431 LLVMWithBeforeLambdaBody
);
23433 "FctWithTwoParams_SLS_All(\n"
23434 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23435 LLVMWithBeforeLambdaBody
);
23437 "FctWithTwoParams_SLS_All(\n"
23442 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23444 LLVMWithBeforeLambdaBody
);
23445 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23446 LLVMWithBeforeLambdaBody
);
23448 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23449 LLVMWithBeforeLambdaBody
);
23450 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23452 LLVMWithBeforeLambdaBody
);
23453 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23456 " // A cool function...\n"
23457 " return Call([]() { return 17; });\n"
23459 LLVMWithBeforeLambdaBody
);
23460 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23466 " // A cool function...\n"
23470 LLVMWithBeforeLambdaBody
);
23472 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23473 FormatStyle::ShortLambdaStyle::SLS_None
;
23475 verifyFormat("auto select = [this]() -> const Library::Object *\n"
23477 " return MyAssignment::SelectFromList(this);\n"
23479 LLVMWithBeforeLambdaBody
);
23481 verifyFormat("auto select = [this]() -> const Library::Object &\n"
23483 " return MyAssignment::SelectFromList(this);\n"
23485 LLVMWithBeforeLambdaBody
);
23487 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23489 " return MyAssignment::SelectFromList(this);\n"
23491 LLVMWithBeforeLambdaBody
);
23493 verifyFormat("namespace test {\n"
23496 " Test() = default;\n"
23498 "} // namespace test",
23499 LLVMWithBeforeLambdaBody
);
23501 // Lambdas with different indentation styles.
23502 Style
= getLLVMStyleWithColumns(60);
23503 verifyFormat("Result doSomething(Promise promise) {\n"
23504 " return promise.then(\n"
23505 " [this, obj = std::move(s)](int bar) mutable {\n"
23506 " return someObject.startAsyncAction().then(\n"
23507 " [this, &obj](Result result) mutable {\n"
23508 " result.processMore();\n"
23513 Style
.LambdaBodyIndentation
= FormatStyle::LBI_OuterScope
;
23514 verifyFormat("Result doSomething(Promise promise) {\n"
23515 " return promise.then(\n"
23516 " [this, obj = std::move(s)](int bar) mutable {\n"
23517 " return obj.startAsyncAction().then(\n"
23518 " [this, &obj](Result result) mutable {\n"
23519 " result.processMore();\n"
23524 verifyFormat("Result doSomething(Promise promise) {\n"
23525 " return promise.then([this, obj = std::move(s)] {\n"
23526 " return obj.startAsyncAction().then(\n"
23527 " [this, &obj](Result result) mutable {\n"
23528 " result.processMore();\n"
23533 verifyFormat("void test() {\n"
23534 " ([]() -> auto {\n"
23540 verifyFormat("void test() {\n"
23541 " []() -> auto {\n"
23547 verifyFormat("void test() {\n"
23548 " std::sort(v.begin(), v.end(),\n"
23549 " [](const auto &foo, const auto &bar) {\n"
23550 " return foo.baz < bar.baz;\n"
23554 verifyFormat("void test() {\n"
23556 " []() -> auto {\n"
23563 verifyFormat("void test() {\n"
23564 " ([]() -> auto {\n"
23572 verifyFormat("#define A \\\n"
23574 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
23575 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
23578 verifyFormat("#define SORT(v) \\\n"
23579 " std::sort(v.begin(), v.end(), \\\n"
23580 " [](const auto &foo, const auto &bar) { \\\n"
23581 " return foo.baz < bar.baz; \\\n"
23584 verifyFormat("void foo() {\n"
23585 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23586 " auto f = e(d);\n"
23591 verifyFormat("void foo() {\n"
23592 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23593 " auto f = e(foo, [&] {\n"
23596 " }, qux, [&] -> Bar {\n"
23604 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23605 " AnotherLongClassName baz)\n"
23606 " : baz{baz}, func{[&] {\n"
23607 " auto qux = bar;\n"
23608 " return aFunkyFunctionCall(qux);\n"
23611 verifyFormat("void foo() {\n"
23615 " : qux{[](int quux) {\n"
23616 " auto tmp = quux;\n"
23621 " std::function<void(int quux)> qux;\n"
23625 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
23626 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23627 " AnotherLongClassName baz) :\n"
23628 " baz{baz}, func{[&] {\n"
23629 " auto qux = bar;\n"
23630 " return aFunkyFunctionCall(qux);\n"
23633 Style
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
23634 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23635 " AnotherLongClassName baz) :\n"
23638 " auto qux = bar;\n"
23639 " return aFunkyFunctionCall(qux);\n"
23642 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
23643 // FIXME: The following test should pass, but fails at the time of writing.
23645 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23646 // doesn't force an initial line break, even if lambdas span multiple lines.
23647 verifyFormat("void foo() {\n"
23649 " [](d) -> Foo {\n"
23650 " auto f = e(d);\n"
23652 " }, foo, Bar{}, [] {\n"
23659 // A long non-lambda argument forces arguments to span multiple lines and thus
23660 // forces an initial line break when using AlwaysBreak.
23661 verifyFormat("void foo() {\n"
23664 " [](d) -> Foo {\n"
23665 " auto f = e(d);\n"
23667 " }, foo, Bar{},\n"
23672 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23675 Style
.BinPackArguments
= false;
23676 verifyFormat("void foo() {\n"
23679 " [](d) -> Foo {\n"
23680 " auto f = e(d);\n"
23690 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23693 Style
.BinPackArguments
= true;
23694 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
23695 Style
.BraceWrapping
.BeforeLambdaBody
= true;
23696 verifyFormat("void foo() {\n"
23698 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23705 " }, qux, [&] -> Bar\n"
23716 TEST_F(FormatTest
, LambdaWithLineComments
) {
23717 FormatStyle LLVMWithBeforeLambdaBody
= getLLVMStyle();
23718 LLVMWithBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
23719 LLVMWithBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
23720 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23721 FormatStyle::ShortLambdaStyle::SLS_All
;
23723 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody
);
23724 verifyFormat("auto k = []() // comment\n"
23726 LLVMWithBeforeLambdaBody
);
23727 verifyFormat("auto k = []() /* comment */ { return; }",
23728 LLVMWithBeforeLambdaBody
);
23729 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23730 LLVMWithBeforeLambdaBody
);
23731 verifyFormat("auto k = []() // X\n"
23733 LLVMWithBeforeLambdaBody
);
23735 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23737 LLVMWithBeforeLambdaBody
);
23739 LLVMWithBeforeLambdaBody
.ColumnLimit
= 0;
23741 verifyFormat("foo([]()\n"
23744 " return 1; // comment\n"
23748 " return 1; // comment\n"
23750 LLVMWithBeforeLambdaBody
);
23751 verifyFormat("foo(\n"
23754 " bar(); // comment\n"
23758 " 1, MACRO { baz(); bar(); // comment\n"
23761 LLVMWithBeforeLambdaBody
);
23764 TEST_F(FormatTest
, EmptyLinesInLambdas
) {
23765 verifyFormat("auto lambda = []() {\n"
23768 "auto lambda = []() {\n"
23775 TEST_F(FormatTest
, FormatsBlocks
) {
23776 FormatStyle ShortBlocks
= getLLVMStyle();
23777 ShortBlocks
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
23778 verifyFormat("int (^Block)(int, int);", ShortBlocks
);
23779 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks
);
23780 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks
);
23781 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks
);
23782 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks
);
23783 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks
);
23785 verifyFormat("foo(^{ bar(); });", ShortBlocks
);
23786 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks
);
23787 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks
);
23789 verifyFormat("[operation setCompletionBlock:^{\n"
23790 " [self onOperationDone];\n"
23792 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23793 " [self onOperationDone];\n"
23795 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23798 verifyFormat("int a = [operation block:^int(int *i) {\n"
23801 verifyFormat("[myObject doSomethingWith:arg1\n"
23802 " aaa:^int(int *a) {\n"
23805 " bbb:f(a * bbbbbbbb)];");
23807 verifyFormat("[operation setCompletionBlock:^{\n"
23808 " [self.delegate newDataAvailable];\n"
23810 getLLVMStyleWithColumns(60));
23811 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23812 " NSString *path = [self sessionFilePath];\n"
23817 verifyFormat("[[SessionService sharedService]\n"
23818 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23820 " [self windowDidLoad:window];\n"
23822 " [self errorLoadingWindow];\n"
23825 verifyFormat("void (^largeBlock)(void) = ^{\n"
23828 getLLVMStyleWithColumns(40));
23829 verifyFormat("[[SessionService sharedService]\n"
23830 " loadWindowWithCompletionBlock: //\n"
23831 " ^(SessionWindow *window) {\n"
23833 " [self windowDidLoad:window];\n"
23835 " [self errorLoadingWindow];\n"
23838 getLLVMStyleWithColumns(60));
23839 verifyFormat("[myObject doSomethingWith:arg1\n"
23840 " firstBlock:^(Foo *a) {\n"
23844 " secondBlock:^(Bar *b) {\n"
23848 " thirdBlock:^Foo(Bar *b) {\n"
23852 verifyFormat("[myObject doSomethingWith:arg1\n"
23854 " secondBlock:^(Bar *b) {\n"
23859 verifyFormat("f(^{\n"
23860 " @autoreleasepool {\n"
23866 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23868 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23871 FormatStyle FourIndent
= getLLVMStyle();
23872 FourIndent
.ObjCBlockIndentWidth
= 4;
23873 verifyFormat("[operation setCompletionBlock:^{\n"
23874 " [self onOperationDone];\n"
23879 TEST_F(FormatTest
, FormatsBlocksWithZeroColumnWidth
) {
23880 FormatStyle ZeroColumn
= getLLVMStyleWithColumns(0);
23882 verifyFormat("[[SessionService sharedService] "
23883 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23885 " [self windowDidLoad:window];\n"
23887 " [self errorLoadingWindow];\n"
23891 verifyFormat("[[SessionService sharedService]\n"
23892 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23894 " [self windowDidLoad:window];\n"
23896 " [self errorLoadingWindow];\n"
23899 "[[SessionService sharedService]\n"
23900 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23902 " [self windowDidLoad:window];\n"
23904 " [self errorLoadingWindow];\n"
23908 verifyFormat("[myObject doSomethingWith:arg1\n"
23909 " firstBlock:^(Foo *a) {\n"
23913 " secondBlock:^(Bar *b) {\n"
23917 " thirdBlock:^Foo(Bar *b) {\n"
23922 verifyFormat("f(^{\n"
23923 " @autoreleasepool {\n"
23930 verifyFormat("void (^largeBlock)(void) = ^{\n"
23935 ZeroColumn
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
23936 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23937 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn
);
23938 ZeroColumn
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
23939 verifyFormat("void (^largeBlock)(void) = ^{\n"
23942 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn
);
23945 TEST_F(FormatTest
, SupportsCRLF
) {
23946 verifyFormat("int a;\r\n"
23952 verifyFormat("int a;\r\n"
23958 verifyFormat("int a;\n"
23964 // FIXME: unstable test case
23965 EXPECT_EQ("\"aaaaaaa \"\r\n"
23966 "\"bbbbbbb\";\r\n",
23967 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23968 verifyFormat("#define A \\\r\n"
23977 verifyNoChange("/*\r\n"
23978 "multi line block comments\r\n"
23979 "should not introduce\r\n"
23980 "an extra carriage return\r\n"
23982 verifyFormat("/*\r\n"
23989 FormatStyle style
= getLLVMStyle();
23991 EXPECT_EQ(style
.LineEnding
, FormatStyle::LE_DeriveLF
);
23992 verifyFormat("union FooBarBazQux {\n"
23997 "union FooBarBazQux {\r\n"
24003 style
.LineEnding
= FormatStyle::LE_DeriveCRLF
;
24004 verifyFormat("union FooBarBazQux {\r\n"
24009 "union FooBarBazQux {\r\n"
24016 style
.LineEnding
= FormatStyle::LE_LF
;
24017 verifyFormat("union FooBarBazQux {\n"
24023 "union FooBarBazQux {\r\n"
24030 style
.LineEnding
= FormatStyle::LE_CRLF
;
24031 verifyFormat("union FooBarBazQux {\r\n"
24037 "union FooBarBazQux {\r\n"
24045 style
.LineEnding
= FormatStyle::LE_DeriveLF
;
24046 verifyFormat("union FooBarBazQux {\r\n"
24052 "union FooBarBazQux {\r\n"
24059 style
.LineEnding
= FormatStyle::LE_DeriveCRLF
;
24060 verifyFormat("union FooBarBazQux {\n"
24066 "union FooBarBazQux {\r\n"
24075 TEST_F(FormatTest
, MunchSemicolonAfterBlocks
) {
24076 verifyFormat("MY_CLASS(C) {\n"
24082 TEST_F(FormatTest
, ConfigurableContinuationIndentWidth
) {
24083 FormatStyle TwoIndent
= getLLVMStyleWithColumns(15);
24084 TwoIndent
.ContinuationIndentWidth
= 2;
24086 verifyFormat("int i =\n"
24089 "int i = longFunction(arg);", TwoIndent
);
24091 FormatStyle SixIndent
= getLLVMStyleWithColumns(20);
24092 SixIndent
.ContinuationIndentWidth
= 6;
24094 verifyFormat("int i =\n"
24097 "int i = longFunction(arg);", SixIndent
);
24100 TEST_F(FormatTest
, WrappedClosingParenthesisIndent
) {
24101 FormatStyle Style
= getLLVMStyle();
24102 verifyFormat("int Foo::getter(\n"
24108 verifyFormat("void Foo::setter(\n"
24116 TEST_F(FormatTest
, SpacesInAngles
) {
24117 FormatStyle Spaces
= getLLVMStyle();
24118 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24120 verifyFormat("vector< ::std::string > x1;", Spaces
);
24121 verifyFormat("Foo< int, Bar > x2;", Spaces
);
24122 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces
);
24124 verifyFormat("static_cast< int >(arg);", Spaces
);
24125 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces
);
24126 verifyFormat("f< int, float >();", Spaces
);
24127 verifyFormat("template <> g() {}", Spaces
);
24128 verifyFormat("template < std::vector< int > > f() {}", Spaces
);
24129 verifyFormat("std::function< void(int, int) > fct;", Spaces
);
24130 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24133 Spaces
.Standard
= FormatStyle::LS_Cpp03
;
24134 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24135 verifyFormat("A< A< int > >();", Spaces
);
24137 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Never
;
24138 verifyFormat("A<A<int> >();", Spaces
);
24140 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Leave
;
24141 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24143 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24146 verifyFormat("A<A<int> >();", Spaces
);
24147 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces
);
24148 verifyFormat("A< A< int > >();", Spaces
);
24150 Spaces
.Standard
= FormatStyle::LS_Cpp11
;
24151 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24152 verifyFormat("A< A< int > >();", Spaces
);
24154 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Never
;
24155 verifyFormat("vector<::std::string> x4;", Spaces
);
24156 verifyFormat("vector<int> x5;", Spaces
);
24157 verifyFormat("Foo<int, Bar> x6;", Spaces
);
24158 verifyFormat("Foo<::int, ::Bar> x7;", Spaces
);
24160 verifyFormat("A<A<int>>();", Spaces
);
24162 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Leave
;
24163 verifyFormat("vector<::std::string> x4;", Spaces
);
24164 verifyFormat("vector< ::std::string > x4;", Spaces
);
24165 verifyFormat("vector<int> x5;", Spaces
);
24166 verifyFormat("vector< int > x5;", Spaces
);
24167 verifyFormat("Foo<int, Bar> x6;", Spaces
);
24168 verifyFormat("Foo< int, Bar > x6;", Spaces
);
24169 verifyFormat("Foo<::int, ::Bar> x7;", Spaces
);
24170 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces
);
24172 verifyFormat("A<A<int>>();", Spaces
);
24173 verifyFormat("A< A< int > >();", Spaces
);
24174 verifyFormat("A<A<int > >();", Spaces
);
24175 verifyFormat("A< A< int>>();", Spaces
);
24177 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24178 verifyFormat("// clang-format off\n"
24179 "foo<<<1, 1>>>();\n"
24180 "// clang-format on",
24182 verifyFormat("// clang-format off\n"
24183 "foo< < <1, 1> > >();\n"
24184 "// clang-format on",
24188 TEST_F(FormatTest
, SpaceAfterTemplateKeyword
) {
24189 FormatStyle Style
= getLLVMStyle();
24190 Style
.SpaceAfterTemplateKeyword
= false;
24191 verifyFormat("template<int> void foo();", Style
);
24194 TEST_F(FormatTest
, TripleAngleBrackets
) {
24195 verifyFormat("f<<<1, 1>>>();");
24196 verifyFormat("f<<<1, 1, 1, s>>>();");
24197 verifyFormat("f<<<a, b, c, d>>>();");
24198 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24199 verifyFormat("f<param><<<1, 1>>>();");
24200 verifyFormat("f<1><<<1, 1>>>();");
24201 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24202 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24203 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24204 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24205 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24208 TEST_F(FormatTest
, MergeLessLessAtEnd
) {
24209 verifyFormat("<<");
24210 verifyFormat("< < <", "\\\n<<<");
24211 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24212 "aaallvm::outs() <<");
24213 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24214 "aaaallvm::outs()\n <<");
24217 TEST_F(FormatTest
, HandleUnbalancedImplicitBracesAcrossPPBranches
) {
24218 std::string code
= "#if A\n"
24228 verifyFormat(code
);
24231 TEST_F(FormatTest
, HandleConflictMarkers
) {
24232 // Git/SVN conflict markers.
24233 verifyFormat("int a;\n"
24235 " callme(some(parameter1,\n"
24236 "<<<<<<< text by the vcs\n"
24238 "||||||| text by the vcs\n"
24241 "======= text by the vcs\n"
24242 " parameter2, parameter3),\n"
24243 ">>>>>>> text by the vcs\n"
24244 " otherparameter);",
24247 " callme(some(parameter1,\n"
24248 "<<<<<<< text by the vcs\n"
24250 "||||||| text by the vcs\n"
24253 "======= text by the vcs\n"
24256 ">>>>>>> text by the vcs\n"
24257 " otherparameter);");
24259 // Perforce markers.
24260 verifyFormat("void f() {\n"
24262 ">>>> text by the vcs\n"
24264 "==== text by the vcs\n"
24266 "==== text by the vcs\n"
24268 "<<<< text by the vcs\n"
24272 ">>>> text by the vcs\n"
24274 "==== text by the vcs\n"
24276 "==== text by the vcs\n"
24278 "<<<< text by the vcs\n"
24281 verifyNoChange("<<<<<<<\n"
24286 verifyNoChange("<<<<<<<\n"
24292 // FIXME: Handle parsing of macros around conflict markers correctly:
24293 verifyFormat("#define Macro \\\n"
24302 "#define Macro \\\n"
24313 verifyFormat(R
"(====
24322 TEST_F(FormatTest
, DisableRegions
) {
24323 verifyFormat("int i;\n"
24324 "// clang-format off\n"
24326 "// clang-format on\n"
24329 " // clang-format off\n"
24331 " // clang-format on\n"
24333 verifyFormat("int i;\n"
24334 "/* clang-format off */\n"
24336 "/* clang-format on */\n"
24339 " /* clang-format off */\n"
24341 " /* clang-format on */\n"
24344 // Don't reflow comments within disabled regions.
24345 verifyFormat("// clang-format off\n"
24346 "// long long long long long long line\n"
24347 "/* clang-format on */\n"
24348 "/* long long long\n"
24349 " * long long long\n"
24352 "/* clang-format off */\n"
24353 "/* long long long long long long line */",
24354 "// clang-format off\n"
24355 "// long long long long long long line\n"
24356 "/* clang-format on */\n"
24357 "/* long long long long long long line */\n"
24359 "/* clang-format off */\n"
24360 "/* long long long long long long line */",
24361 getLLVMStyleWithColumns(20));
24363 verifyFormat("int *i;\n"
24364 "// clang-format off:\n"
24366 "// clang-format on: 1\n"
24369 "// clang-format off:\n"
24371 "// clang-format on: 1\n"
24374 verifyFormat("int *i;\n"
24375 "// clang-format off:0\n"
24377 "// clang-format only\n"
24380 "// clang-format off:0\n"
24382 "// clang-format only\n"
24385 verifyNoChange("// clang-format off\n"
24387 " #if SHOULD_STAY_INDENTED\n"
24390 "// clang-format on");
24393 TEST_F(FormatTest
, DoNotCrashOnInvalidInput
) {
24395 verifyNoCrash("#define a\\\n /**/}");
24396 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n"
24397 "LY52: ! [internal]");
24400 TEST_F(FormatTest
, FormatsTableGenCode
) {
24401 FormatStyle Style
= getLLVMStyle();
24402 Style
.Language
= FormatStyle::LK_TableGen
;
24403 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style
);
24406 TEST_F(FormatTest
, ArrayOfTemplates
) {
24407 verifyFormat("auto a = new unique_ptr<int>[10];",
24408 "auto a = new unique_ptr<int > [ 10];");
24410 FormatStyle Spaces
= getLLVMStyle();
24411 Spaces
.SpacesInSquareBrackets
= true;
24412 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24413 "auto a = new unique_ptr<int > [10];", Spaces
);
24416 TEST_F(FormatTest
, ArrayAsTemplateType
) {
24417 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24418 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24420 FormatStyle Spaces
= getLLVMStyle();
24421 Spaces
.SpacesInSquareBrackets
= true;
24422 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24423 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces
);
24426 TEST_F(FormatTest
, NoSpaceAfterSuper
) { verifyFormat("__super::FooBar();"); }
24428 TEST_F(FormatTest
, FormatSortsUsingDeclarations
) {
24429 verifyFormat("using std::cin;\n"
24430 "using std::cout;",
24431 "using std::cout;\n"
24436 TEST_F(FormatTest
, UTF8CharacterLiteralCpp03
) {
24437 FormatStyle Style
= getLLVMStyle();
24438 Style
.Standard
= FormatStyle::LS_Cpp03
;
24439 // cpp03 recognize this string as identifier u8 and literal character 'a'
24440 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style
);
24443 TEST_F(FormatTest
, UTF8CharacterLiteralCpp11
) {
24444 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24445 // all modes, including C++11, C++14 and C++17
24446 verifyFormat("auto c = u8'a';");
24449 TEST_F(FormatTest
, DoNotFormatLikelyXml
) {
24450 verifyGoogleFormat("<!-- ;> -->");
24451 verifyNoChange(" <!-- >; -->", getGoogleStyle());
24454 TEST_F(FormatTest
, StructuredBindings
) {
24455 // Structured bindings is a C++17 feature.
24456 // all modes, including C++11, C++14 and C++17
24457 verifyFormat("auto [a, b] = f();");
24458 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24459 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
24460 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
24461 verifyFormat("auto const volatile [a, b] = f();",
24462 "auto const volatile[a, b] = f();");
24463 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
24464 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
24465 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
24466 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
24467 verifyFormat("auto const volatile &&[a, b] = f();",
24468 "auto const volatile &&[a, b] = f();");
24469 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
24470 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
24471 verifyFormat("const auto volatile &&[a, b] = f();",
24472 "const auto volatile &&[a, b] = f();");
24473 verifyFormat("volatile const auto &&[a, b] = f();",
24474 "volatile const auto &&[a, b] = f();");
24475 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
24477 // Make sure we don't mistake structured bindings for lambdas.
24478 FormatStyle PointerMiddle
= getLLVMStyle();
24479 PointerMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
24480 verifyGoogleFormat("auto [a1, b]{A * i};");
24481 verifyFormat("auto [a2, b]{A * i};");
24482 verifyFormat("auto [a3, b]{A * i};", PointerMiddle
);
24483 verifyGoogleFormat("auto const [a1, b]{A * i};");
24484 verifyFormat("auto const [a2, b]{A * i};");
24485 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle
);
24486 verifyGoogleFormat("auto const& [a1, b]{A * i};");
24487 verifyFormat("auto const &[a2, b]{A * i};");
24488 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle
);
24489 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24490 verifyFormat("auto const &&[a2, b]{A * i};");
24491 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle
);
24493 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24494 "for (const auto && [a, b] : some_range) {\n}");
24495 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24496 "for (const auto & [a, b] : some_range) {\n}");
24497 verifyFormat("for (const auto [a, b] : some_range) {\n}",
24498 "for (const auto[a, b] : some_range) {\n}");
24499 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
24500 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
24501 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
24502 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
24503 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
24504 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
24505 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
24506 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
24508 FormatStyle Spaces
= getLLVMStyle();
24509 Spaces
.SpacesInSquareBrackets
= true;
24510 verifyFormat("auto [ a, b ] = f();", Spaces
);
24511 verifyFormat("auto &&[ a, b ] = f();", Spaces
);
24512 verifyFormat("auto &[ a, b ] = f();", Spaces
);
24513 verifyFormat("auto const &&[ a, b ] = f();", Spaces
);
24514 verifyFormat("auto const &[ a, b ] = f();", Spaces
);
24517 TEST_F(FormatTest
, FileAndCode
) {
24518 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.cc", ""));
24519 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.m", ""));
24520 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.mm", ""));
24521 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", ""));
24522 EXPECT_EQ(FormatStyle::LK_ObjC
,
24523 guessLanguage("foo.h", "@interface Foo\n@end"));
24525 FormatStyle::LK_ObjC
,
24526 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24527 EXPECT_EQ(FormatStyle::LK_ObjC
,
24528 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24529 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.h", "@class Foo;"));
24530 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo", ""));
24531 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo", "@interface Foo\n@end"));
24532 EXPECT_EQ(FormatStyle::LK_ObjC
,
24533 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24534 EXPECT_EQ(FormatStyle::LK_ObjC
,
24536 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24538 FormatStyle::LK_Cpp
,
24539 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24540 // Only one of the two preprocessor regions has ObjC-like code.
24541 EXPECT_EQ(FormatStyle::LK_ObjC
,
24542 guessLanguage("foo.h", "#if A\n"
24545 "#define B() [NSString a:@\"\"]\n"
24549 TEST_F(FormatTest
, GuessLanguageWithCpp11AttributeSpecifiers
) {
24550 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "[[noreturn]];"));
24551 EXPECT_EQ(FormatStyle::LK_ObjC
,
24552 guessLanguage("foo.h", "array[[calculator getIndex]];"));
24553 EXPECT_EQ(FormatStyle::LK_Cpp
,
24554 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24556 FormatStyle::LK_Cpp
,
24557 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24558 EXPECT_EQ(FormatStyle::LK_ObjC
,
24559 guessLanguage("foo.h", "[[noreturn foo] bar];"));
24560 EXPECT_EQ(FormatStyle::LK_Cpp
,
24561 guessLanguage("foo.h", "[[clang::fallthrough]];"));
24562 EXPECT_EQ(FormatStyle::LK_ObjC
,
24563 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24564 EXPECT_EQ(FormatStyle::LK_Cpp
,
24565 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24566 EXPECT_EQ(FormatStyle::LK_Cpp
,
24567 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24568 EXPECT_EQ(FormatStyle::LK_ObjC
,
24569 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24570 EXPECT_EQ(FormatStyle::LK_Cpp
,
24571 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24573 FormatStyle::LK_Cpp
,
24574 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24576 FormatStyle::LK_Cpp
,
24577 guessLanguage("foo.h",
24578 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24579 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24582 TEST_F(FormatTest
, GuessLanguageWithCaret
) {
24583 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "FOO(^);"));
24584 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "FOO(^, Bar);"));
24585 EXPECT_EQ(FormatStyle::LK_ObjC
,
24586 guessLanguage("foo.h", "int(^)(char, float);"));
24587 EXPECT_EQ(FormatStyle::LK_ObjC
,
24588 guessLanguage("foo.h", "int(^foo)(char, float);"));
24589 EXPECT_EQ(FormatStyle::LK_ObjC
,
24590 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24591 EXPECT_EQ(FormatStyle::LK_ObjC
,
24592 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24594 FormatStyle::LK_ObjC
,
24595 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24598 TEST_F(FormatTest
, GuessLanguageWithPragmas
) {
24599 EXPECT_EQ(FormatStyle::LK_Cpp
,
24600 guessLanguage("foo.h", "__pragma(warning(disable:))"));
24601 EXPECT_EQ(FormatStyle::LK_Cpp
,
24602 guessLanguage("foo.h", "#pragma(warning(disable:))"));
24603 EXPECT_EQ(FormatStyle::LK_Cpp
,
24604 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24607 TEST_F(FormatTest
, FormatsInlineAsmSymbolicNames
) {
24608 // ASM symbolic names are identifiers that must be surrounded by [] without
24609 // space in between:
24610 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24612 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24614 asm volatile("mrs
%x
[result
], FPCR
" : [result] "=r
"(result));
24617 // A list of several ASM symbolic names.
24618 verifyFormat(R
"(asm("mov
%[e
], %[d
]" : [d] "=rm
"(d), [e] "rm
"(*e));)");
24620 // ASM symbolic names in inline ASM with inputs and outputs.
24622 asm("cmoveq
%1, %2, %[result
]"
24623 : [result] "=r
"(result)
24624 : "r
"(test), "r
"(new), "[result
]"(old));
24627 // ASM symbolic names in inline ASM with no outputs.
24628 verifyFormat(R
"(asm("mov
%[e
], %[d
]" : : [d] "=rm
"(d), [e] "rm
"(*e));)");
24631 TEST_F(FormatTest
, GuessedLanguageWithInlineAsmClobbers
) {
24632 EXPECT_EQ(FormatStyle::LK_Cpp
,
24633 guessLanguage("foo.h", "void f() {\n"
24634 " asm (\"mov %[e], %[d]\"\n"
24635 " : [d] \"=rm\" (d)\n"
24636 " [e] \"rm\" (*e));\n"
24638 EXPECT_EQ(FormatStyle::LK_Cpp
,
24639 guessLanguage("foo.h", "void f() {\n"
24640 " _asm (\"mov %[e], %[d]\"\n"
24641 " : [d] \"=rm\" (d)\n"
24642 " [e] \"rm\" (*e));\n"
24644 EXPECT_EQ(FormatStyle::LK_Cpp
,
24645 guessLanguage("foo.h", "void f() {\n"
24646 " __asm (\"mov %[e], %[d]\"\n"
24647 " : [d] \"=rm\" (d)\n"
24648 " [e] \"rm\" (*e));\n"
24650 EXPECT_EQ(FormatStyle::LK_Cpp
,
24651 guessLanguage("foo.h", "void f() {\n"
24652 " __asm__ (\"mov %[e], %[d]\"\n"
24653 " : [d] \"=rm\" (d)\n"
24654 " [e] \"rm\" (*e));\n"
24656 EXPECT_EQ(FormatStyle::LK_Cpp
,
24657 guessLanguage("foo.h", "void f() {\n"
24658 " asm (\"mov %[e], %[d]\"\n"
24659 " : [d] \"=rm\" (d),\n"
24660 " [e] \"rm\" (*e));\n"
24662 EXPECT_EQ(FormatStyle::LK_Cpp
,
24663 guessLanguage("foo.h", "void f() {\n"
24664 " asm volatile (\"mov %[e], %[d]\"\n"
24665 " : [d] \"=rm\" (d)\n"
24666 " [e] \"rm\" (*e));\n"
24670 TEST_F(FormatTest
, GuessLanguageWithChildLines
) {
24671 EXPECT_EQ(FormatStyle::LK_Cpp
,
24672 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24673 EXPECT_EQ(FormatStyle::LK_ObjC
,
24674 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24676 FormatStyle::LK_Cpp
,
24677 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24679 FormatStyle::LK_ObjC
,
24680 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24683 TEST_F(FormatTest
, TypenameMacros
) {
24684 std::vector
<std::string
> TypenameMacros
= {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24686 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24687 FormatStyle Google
= getGoogleStyleWithColumns(0);
24688 Google
.TypenameMacros
= TypenameMacros
;
24689 verifyFormat("struct foo {\n"
24691 " TAILQ_ENTRY(a) bleh;\n"
24695 FormatStyle Macros
= getLLVMStyle();
24696 Macros
.TypenameMacros
= TypenameMacros
;
24698 verifyFormat("STACK_OF(int) a;", Macros
);
24699 verifyFormat("STACK_OF(int) *a;", Macros
);
24700 verifyFormat("STACK_OF(int const *) *a;", Macros
);
24701 verifyFormat("STACK_OF(int *const) *a;", Macros
);
24702 verifyFormat("STACK_OF(int, string) a;", Macros
);
24703 verifyFormat("STACK_OF(LIST(int)) a;", Macros
);
24704 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros
);
24705 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros
);
24706 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros
);
24707 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros
);
24708 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros
);
24710 Macros
.PointerAlignment
= FormatStyle::PAS_Left
;
24711 verifyFormat("STACK_OF(int)* a;", Macros
);
24712 verifyFormat("STACK_OF(int*)* a;", Macros
);
24713 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros
);
24714 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros
);
24715 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros
);
24718 TEST_F(FormatTest
, AtomicQualifier
) {
24719 // Check that we treate _Atomic as a type and not a function call
24720 FormatStyle Google
= getGoogleStyleWithColumns(0);
24721 verifyFormat("struct foo {\n"
24723 " _Atomic(a) a2;\n"
24724 " _Atomic(_Atomic(int) *const) a3;\n"
24727 verifyFormat("_Atomic(uint64_t) a;");
24728 verifyFormat("_Atomic(uint64_t) *a;");
24729 verifyFormat("_Atomic(uint64_t const *) *a;");
24730 verifyFormat("_Atomic(uint64_t *const) *a;");
24731 verifyFormat("_Atomic(const uint64_t *) *a;");
24732 verifyFormat("_Atomic(uint64_t) a;");
24733 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24734 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24735 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24736 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24738 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24739 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24740 FormatStyle Style
= getLLVMStyle();
24741 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
24742 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style
);
24743 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style
);
24744 verifyFormat("_Atomic(int)* a;", Style
);
24745 verifyFormat("_Atomic(int*)* a;", Style
);
24746 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style
);
24748 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
24749 Style
.SpacesInParensOptions
.InCStyleCasts
= true;
24750 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style
);
24751 Style
.SpacesInParensOptions
.InCStyleCasts
= false;
24752 Style
.SpacesInParensOptions
.Other
= true;
24753 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style
);
24754 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style
);
24757 TEST_F(FormatTest
, C11Generic
) {
24758 verifyFormat("_Generic(x, int: 1, default: 0)");
24759 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24760 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24761 verifyFormat("_Generic(x, int: f1, const int: f2)();");
24762 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24764 verifyFormat("_Generic(x,\n"
24767 " long double: ld,\n"
24768 " float _Complex: fc,\n"
24769 " double _Complex: dc,\n"
24770 " long double _Complex: ldc)");
24772 verifyFormat("while (_Generic(x, //\n"
24773 " long: x)(x) > x) {\n"
24775 verifyFormat("while (_Generic(x, //\n"
24776 " long: x)(x)) {\n"
24778 verifyFormat("x(_Generic(x, //\n"
24781 FormatStyle Style
= getLLVMStyle();
24782 Style
.ColumnLimit
= 40;
24783 verifyFormat("#define LIMIT_MAX(T) \\\n"
24784 " _Generic(((T)0), \\\n"
24785 " unsigned int: UINT_MAX, \\\n"
24786 " unsigned long: ULONG_MAX, \\\n"
24787 " unsigned long long: ULLONG_MAX)",
24789 verifyFormat("_Generic(x,\n"
24791 " void (*)(void): 2);",
24794 Style
.ContinuationIndentWidth
= 2;
24795 verifyFormat("_Generic(x,\n"
24797 " void (*)(void): 2);",
24801 TEST_F(FormatTest
, AmbersandInLamda
) {
24802 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24803 FormatStyle AlignStyle
= getLLVMStyle();
24804 AlignStyle
.PointerAlignment
= FormatStyle::PAS_Left
;
24805 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle
);
24806 AlignStyle
.PointerAlignment
= FormatStyle::PAS_Right
;
24807 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle
);
24810 TEST_F(FormatTest
, TrailingReturnTypeAuto
) {
24811 FormatStyle Style
= getLLVMStyle();
24812 verifyFormat("[]() -> auto { return Val; }", Style
);
24813 verifyFormat("[]() -> auto * { return Val; }", Style
);
24814 verifyFormat("[]() -> auto & { return Val; }", Style
);
24815 verifyFormat("auto foo() -> auto { return Val; }", Style
);
24816 verifyFormat("auto foo() -> auto * { return Val; }", Style
);
24817 verifyFormat("auto foo() -> auto & { return Val; }", Style
);
24820 TEST_F(FormatTest
, SpacesInConditionalStatement
) {
24821 FormatStyle Spaces
= getLLVMStyle();
24822 Spaces
.IfMacros
.clear();
24823 Spaces
.IfMacros
.push_back("MYIF");
24824 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
24825 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
24826 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces
);
24827 verifyFormat("if ( !a )\n return;", Spaces
);
24828 verifyFormat("if ( a )\n return;", Spaces
);
24829 verifyFormat("if constexpr ( a )\n return;", Spaces
);
24830 verifyFormat("MYIF ( a )\n return;", Spaces
);
24831 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces
);
24832 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces
);
24833 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces
);
24834 verifyFormat("while ( a )\n return;", Spaces
);
24835 verifyFormat("while ( (a && b) )\n return;", Spaces
);
24836 verifyFormat("do {\n} while ( 1 != 0 );", Spaces
);
24837 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces
);
24838 // Check that space on the left of "::" is inserted as expected at beginning
24840 verifyFormat("while ( ::func() )\n return;", Spaces
);
24842 // Check impact of ControlStatementsExceptControlMacros is honored.
24843 Spaces
.SpaceBeforeParens
=
24844 FormatStyle::SBPO_ControlStatementsExceptControlMacros
;
24845 verifyFormat("MYIF( a )\n return;", Spaces
);
24846 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces
);
24847 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces
);
24850 TEST_F(FormatTest
, AlternativeOperators
) {
24851 // Test case for ensuring alternate operators are not
24852 // combined with their right most neighbour.
24853 verifyFormat("int a and b;");
24854 verifyFormat("int a and_eq b;");
24855 verifyFormat("int a bitand b;");
24856 verifyFormat("int a bitor b;");
24857 verifyFormat("int a compl b;");
24858 verifyFormat("int a not b;");
24859 verifyFormat("int a not_eq b;");
24860 verifyFormat("int a or b;");
24861 verifyFormat("int a xor b;");
24862 verifyFormat("int a xor_eq b;");
24863 verifyFormat("return this not_eq bitand other;");
24864 verifyFormat("bool operator not_eq(const X bitand other)");
24866 verifyFormat("int a and 5;");
24867 verifyFormat("int a and_eq 5;");
24868 verifyFormat("int a bitand 5;");
24869 verifyFormat("int a bitor 5;");
24870 verifyFormat("int a compl 5;");
24871 verifyFormat("int a not 5;");
24872 verifyFormat("int a not_eq 5;");
24873 verifyFormat("int a or 5;");
24874 verifyFormat("int a xor 5;");
24875 verifyFormat("int a xor_eq 5;");
24877 verifyFormat("int a compl(5);");
24878 verifyFormat("int a not(5);");
24880 verifyFormat("compl foo();"); // ~foo();
24881 verifyFormat("foo() <%%>"); // foo() {}
24882 verifyFormat("void foo() <%%>"); // void foo() {}
24883 verifyFormat("int a<:1:>;"); // int a[1];
24884 verifyFormat("%:define ABC abc"); // #define ABC abc
24885 verifyFormat("%:%:"); // ##
24887 verifyFormat("a = v(not;);\n"
24891 "e = v(not 123.f);");
24893 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
24898 getLLVMStyleWithColumns(40));
24901 TEST_F(FormatTest
, STLWhileNotDefineChed
) {
24902 verifyFormat("#if defined(while)\n"
24903 "#define while EMIT WARNING C4005\n"
24904 "#endif // while");
24907 TEST_F(FormatTest
, OperatorSpacing
) {
24908 FormatStyle Style
= getLLVMStyle();
24909 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
24910 verifyFormat("Foo::operator*();", Style
);
24911 verifyFormat("Foo::operator void *();", Style
);
24912 verifyFormat("Foo::operator void **();", Style
);
24913 verifyFormat("Foo::operator void *&();", Style
);
24914 verifyFormat("Foo::operator void *&&();", Style
);
24915 verifyFormat("Foo::operator void const *();", Style
);
24916 verifyFormat("Foo::operator void const **();", Style
);
24917 verifyFormat("Foo::operator void const *&();", Style
);
24918 verifyFormat("Foo::operator void const *&&();", Style
);
24919 verifyFormat("Foo::operator()(void *);", Style
);
24920 verifyFormat("Foo::operator*(void *);", Style
);
24921 verifyFormat("Foo::operator*();", Style
);
24922 verifyFormat("Foo::operator**();", Style
);
24923 verifyFormat("Foo::operator&();", Style
);
24924 verifyFormat("Foo::operator<int> *();", Style
);
24925 verifyFormat("Foo::operator<Foo> *();", Style
);
24926 verifyFormat("Foo::operator<int> **();", Style
);
24927 verifyFormat("Foo::operator<Foo> **();", Style
);
24928 verifyFormat("Foo::operator<int> &();", Style
);
24929 verifyFormat("Foo::operator<Foo> &();", Style
);
24930 verifyFormat("Foo::operator<int> &&();", Style
);
24931 verifyFormat("Foo::operator<Foo> &&();", Style
);
24932 verifyFormat("Foo::operator<int> *&();", Style
);
24933 verifyFormat("Foo::operator<Foo> *&();", Style
);
24934 verifyFormat("Foo::operator<int> *&&();", Style
);
24935 verifyFormat("Foo::operator<Foo> *&&();", Style
);
24936 verifyFormat("operator*(int (*)(), class Foo);", Style
);
24938 verifyFormat("Foo::operator&();", Style
);
24939 verifyFormat("Foo::operator void &();", Style
);
24940 verifyFormat("Foo::operator void const &();", Style
);
24941 verifyFormat("Foo::operator()(void &);", Style
);
24942 verifyFormat("Foo::operator&(void &);", Style
);
24943 verifyFormat("Foo::operator&();", Style
);
24944 verifyFormat("operator&(int (&)(), class Foo);", Style
);
24945 verifyFormat("operator&&(int (&)(), class Foo);", Style
);
24947 verifyFormat("Foo::operator&&();", Style
);
24948 verifyFormat("Foo::operator**();", Style
);
24949 verifyFormat("Foo::operator void &&();", Style
);
24950 verifyFormat("Foo::operator void const &&();", Style
);
24951 verifyFormat("Foo::operator()(void &&);", Style
);
24952 verifyFormat("Foo::operator&&(void &&);", Style
);
24953 verifyFormat("Foo::operator&&();", Style
);
24954 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
24955 verifyFormat("operator const nsTArrayRight<E> &()", Style
);
24956 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24958 verifyFormat("operator void **()", Style
);
24959 verifyFormat("operator const FooRight<Object> &()", Style
);
24960 verifyFormat("operator const FooRight<Object> *()", Style
);
24961 verifyFormat("operator const FooRight<Object> **()", Style
);
24962 verifyFormat("operator const FooRight<Object> *&()", Style
);
24963 verifyFormat("operator const FooRight<Object> *&&()", Style
);
24965 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
24966 verifyFormat("Foo::operator*();", Style
);
24967 verifyFormat("Foo::operator**();", Style
);
24968 verifyFormat("Foo::operator void*();", Style
);
24969 verifyFormat("Foo::operator void**();", Style
);
24970 verifyFormat("Foo::operator void*&();", Style
);
24971 verifyFormat("Foo::operator void*&&();", Style
);
24972 verifyFormat("Foo::operator void const*();", Style
);
24973 verifyFormat("Foo::operator void const**();", Style
);
24974 verifyFormat("Foo::operator void const*&();", Style
);
24975 verifyFormat("Foo::operator void const*&&();", Style
);
24976 verifyFormat("Foo::operator/*comment*/ void*();", Style
);
24977 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style
);
24978 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style
);
24979 verifyFormat("Foo::operator()(void*);", Style
);
24980 verifyFormat("Foo::operator*(void*);", Style
);
24981 verifyFormat("Foo::operator*();", Style
);
24982 verifyFormat("Foo::operator<int>*();", Style
);
24983 verifyFormat("Foo::operator<Foo>*();", Style
);
24984 verifyFormat("Foo::operator<int>**();", Style
);
24985 verifyFormat("Foo::operator<Foo>**();", Style
);
24986 verifyFormat("Foo::operator<Foo>*&();", Style
);
24987 verifyFormat("Foo::operator<int>&();", Style
);
24988 verifyFormat("Foo::operator<Foo>&();", Style
);
24989 verifyFormat("Foo::operator<int>&&();", Style
);
24990 verifyFormat("Foo::operator<Foo>&&();", Style
);
24991 verifyFormat("Foo::operator<int>*&();", Style
);
24992 verifyFormat("Foo::operator<Foo>*&();", Style
);
24993 verifyFormat("operator*(int (*)(), class Foo);", Style
);
24995 verifyFormat("Foo::operator&();", Style
);
24996 verifyFormat("Foo::operator void&();", Style
);
24997 verifyFormat("Foo::operator void const&();", Style
);
24998 verifyFormat("Foo::operator/*comment*/ void&();", Style
);
24999 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style
);
25000 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style
);
25001 verifyFormat("Foo::operator()(void&);", Style
);
25002 verifyFormat("Foo::operator&(void&);", Style
);
25003 verifyFormat("Foo::operator&();", Style
);
25004 verifyFormat("operator&(int (&)(), class Foo);", Style
);
25005 verifyFormat("operator&(int (&&)(), class Foo);", Style
);
25006 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
25008 verifyFormat("Foo::operator&&();", Style
);
25009 verifyFormat("Foo::operator void&&();", Style
);
25010 verifyFormat("Foo::operator void const&&();", Style
);
25011 verifyFormat("Foo::operator/*comment*/ void&&();", Style
);
25012 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style
);
25013 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style
);
25014 verifyFormat("Foo::operator()(void&&);", Style
);
25015 verifyFormat("Foo::operator&&(void&&);", Style
);
25016 verifyFormat("Foo::operator&&();", Style
);
25017 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
25018 verifyFormat("operator const nsTArrayLeft<E>&()", Style
);
25019 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25021 verifyFormat("operator void**()", Style
);
25022 verifyFormat("operator const FooLeft<Object>&()", Style
);
25023 verifyFormat("operator const FooLeft<Object>*()", Style
);
25024 verifyFormat("operator const FooLeft<Object>**()", Style
);
25025 verifyFormat("operator const FooLeft<Object>*&()", Style
);
25026 verifyFormat("operator const FooLeft<Object>*&&()", Style
);
25029 verifyFormat("operator Vector<String>&();", Style
);
25030 verifyFormat("operator const Vector<String>&();", Style
);
25031 verifyFormat("operator foo::Bar*();", Style
);
25032 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style
);
25033 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25036 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
25037 verifyFormat("Foo::operator*();", Style
);
25038 verifyFormat("Foo::operator void *();", Style
);
25039 verifyFormat("Foo::operator()(void *);", Style
);
25040 verifyFormat("Foo::operator*(void *);", Style
);
25041 verifyFormat("Foo::operator*();", Style
);
25042 verifyFormat("operator*(int (*)(), class Foo);", Style
);
25044 verifyFormat("Foo::operator&();", Style
);
25045 verifyFormat("Foo::operator void &();", Style
);
25046 verifyFormat("Foo::operator void const &();", Style
);
25047 verifyFormat("Foo::operator()(void &);", Style
);
25048 verifyFormat("Foo::operator&(void &);", Style
);
25049 verifyFormat("Foo::operator&();", Style
);
25050 verifyFormat("operator&(int (&)(), class Foo);", Style
);
25052 verifyFormat("Foo::operator&&();", Style
);
25053 verifyFormat("Foo::operator void &&();", Style
);
25054 verifyFormat("Foo::operator void const &&();", Style
);
25055 verifyFormat("Foo::operator()(void &&);", Style
);
25056 verifyFormat("Foo::operator&&(void &&);", Style
);
25057 verifyFormat("Foo::operator&&();", Style
);
25058 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
25061 TEST_F(FormatTest
, OperatorPassedAsAFunctionPtr
) {
25062 FormatStyle Style
= getLLVMStyle();
25064 verifyFormat("foo(operator+, -42);", Style
);
25065 verifyFormat("foo(operator++, -42);", Style
);
25066 verifyFormat("foo(operator--, -42);", Style
);
25067 verifyFormat("foo(-42, operator--);", Style
);
25068 verifyFormat("foo(-42, operator, );", Style
);
25069 verifyFormat("foo(operator, , -42);", Style
);
25072 TEST_F(FormatTest
, WhitespaceSensitiveMacros
) {
25073 FormatStyle Style
= getLLVMStyle();
25074 Style
.WhitespaceSensitiveMacros
.push_back("FOO");
25076 // Newlines are important here.
25077 verifyNoChange("FOO(1+2 )\n", Style
);
25078 verifyNoChange("FOO(a:b:c)\n", Style
);
25080 // Don't use the helpers here, since 'mess up' will change the whitespace
25081 // and these are all whitespace sensitive by definition
25082 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style
);
25083 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style
);
25084 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style
);
25085 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25086 " Still=Intentional);",
25088 Style
.AlignConsecutiveAssignments
.Enabled
= true;
25089 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25090 " Still=Intentional);",
25093 Style
.ColumnLimit
= 21;
25094 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style
);
25097 TEST_F(FormatTest
, SkipMacroDefinitionBody
) {
25098 auto Style
= getLLVMStyle();
25099 Style
.SkipMacroDefinitionBody
= true;
25101 verifyFormat("#define A", "#define A", Style
);
25102 verifyFormat("#define A a aa", "#define A a aa", Style
);
25103 verifyNoChange("#define A b", Style
);
25104 verifyNoChange("#define A ( args )", Style
);
25105 verifyNoChange("#define A ( args ) = func ( args )", Style
);
25106 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style
);
25107 verifyNoChange("#define A ( args ) \\\n"
25113 verifyNoChange("#define A x:", Style
);
25114 verifyNoChange("#define A a. b", Style
);
25116 // Surrounded with formatted code.
25117 verifyFormat("int a;\n"
25125 // Columns are not broken when a limit is set.
25126 Style
.ColumnLimit
= 10;
25127 verifyFormat("#define A a a a a", " # define A a a a a ", Style
);
25128 verifyNoChange("#define A a a a a", Style
);
25130 Style
.ColumnLimit
= 15;
25131 verifyFormat("#define A // a\n"
25135 "#define A //a very long comment", Style
);
25136 Style
.ColumnLimit
= 0;
25138 // Multiline definition.
25139 verifyNoChange("#define A \\\n"
25140 "Line one with spaces . \\\n"
25143 verifyNoChange("#define A \\\n"
25148 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
25149 verifyNoChange("#define A \\\n"
25154 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
25155 verifyNoChange("#define A \\\n"
25161 // Adjust indendations but don't change the definition.
25162 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
25163 verifyNoChange("#if A\n"
25167 verifyFormat("#if A\n"
25174 Style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
25175 verifyNoChange("#if A\n"
25179 verifyFormat("#if A\n"
25186 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
25187 verifyNoChange("#if A\n"
25191 verifyFormat("#if A\n"
25199 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
25200 // SkipMacroDefinitionBody should not affect other PP directives
25201 verifyFormat("#if !defined(A)\n"
25204 "#if ! defined ( A )\n"
25210 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25212 verifyNoChange("/* */ #define A a // a a", Style
);
25214 verifyFormat("int a; // a\n"
25223 "#define MACRO_WITH_COMMENTS() \\\n"
25225 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
25226 " static MyType getClassTypeId(); \\\n"
25227 " /** Normal comment for the following method. */ \\\n"
25228 " virtual MyType getTypeId() const;",
25231 // multiline macro definitions
25232 verifyNoChange("#define A a\\\n"
25238 TEST_F(FormatTest
, VeryLongNamespaceCommentSplit
) {
25239 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
25240 // test its interaction with line wrapping
25241 FormatStyle Style
= getLLVMStyleWithColumns(80);
25242 verifyFormat("namespace {\n"
25248 verifyFormat("namespace AAA {\n"
25251 "} // namespace AAA",
25254 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
25257 "} // namespace Averyveryveryverylongnamespace",
25258 "namespace Averyveryveryverylongnamespace {\n"
25266 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25267 " went::mad::now {\n"
25272 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25275 "would::it::save::you::a::lot::of::time::if_::i::"
25276 "just::gave::up::and_::went::mad::now {\n"
25282 // This used to duplicate the comment again and again on subsequent runs
25285 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25286 " went::mad::now {\n"
25291 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25294 "would::it::save::you::a::lot::of::time::if_::i::"
25295 "just::gave::up::and_::went::mad::now {\n"
25300 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
25301 "and_::went::mad::now",
25305 TEST_F(FormatTest
, LikelyUnlikely
) {
25306 FormatStyle Style
= getLLVMStyle();
25308 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25313 verifyFormat("if (argc > 5) [[likely]] {\n"
25318 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25320 "} else [[likely]] {\n"
25325 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25327 "} else if (argc > 10) [[likely]] {\n"
25334 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
25339 verifyFormat("if (argc > 5) [[unlikely]]\n"
25342 verifyFormat("if (argc > 5) [[likely]]\n"
25346 verifyFormat("while (limit > 0) [[unlikely]] {\n"
25350 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
25355 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
25358 verifyFormat("while (limit > 0) [[likely]]\n"
25362 Style
.AttributeMacros
.push_back("UNLIKELY");
25363 Style
.AttributeMacros
.push_back("LIKELY");
25364 verifyFormat("if (argc > 5) UNLIKELY\n"
25368 verifyFormat("if (argc > 5) UNLIKELY {\n"
25372 verifyFormat("if (argc > 5) UNLIKELY {\n"
25374 "} else [[likely]] {\n"
25378 verifyFormat("if (argc > 5) UNLIKELY {\n"
25380 "} else LIKELY {\n"
25384 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25386 "} else LIKELY {\n"
25391 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25395 verifyFormat("while (limit > 0) LIKELY {\n"
25400 verifyFormat("while (limit > 0) UNLIKELY\n"
25403 verifyFormat("for (auto &limit : limits) LIKELY\n"
25408 TEST_F(FormatTest
, PenaltyIndentedWhitespace
) {
25409 verifyFormat("Constructor()\n"
25410 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25411 " aaaa(aaaaaaaaaaaaaaaaaa, "
25412 "aaaaaaaaaaaaaaaaaat))");
25413 verifyFormat("Constructor()\n"
25414 " : aaaaaaaaaaaaa(aaaaaa), "
25415 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25417 FormatStyle StyleWithWhitespacePenalty
= getLLVMStyle();
25418 StyleWithWhitespacePenalty
.PenaltyIndentedWhitespace
= 5;
25419 verifyFormat("Constructor()\n"
25420 " : aaaaaa(aaaaaa),\n"
25421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25422 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25423 StyleWithWhitespacePenalty
);
25424 verifyFormat("Constructor()\n"
25425 " : aaaaaaaaaaaaa(aaaaaa), "
25426 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25427 StyleWithWhitespacePenalty
);
25430 TEST_F(FormatTest
, LLVMDefaultStyle
) {
25431 FormatStyle Style
= getLLVMStyle();
25432 verifyFormat("extern \"C\" {\n"
25437 TEST_F(FormatTest
, GNUDefaultStyle
) {
25438 FormatStyle Style
= getGNUStyle();
25439 verifyFormat("extern \"C\"\n"
25445 TEST_F(FormatTest
, MozillaDefaultStyle
) {
25446 FormatStyle Style
= getMozillaStyle();
25447 verifyFormat("extern \"C\"\n"
25453 TEST_F(FormatTest
, GoogleDefaultStyle
) {
25454 FormatStyle Style
= getGoogleStyle();
25455 verifyFormat("extern \"C\" {\n"
25460 TEST_F(FormatTest
, ChromiumDefaultStyle
) {
25461 FormatStyle Style
= getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp
);
25462 verifyFormat("extern \"C\" {\n"
25467 TEST_F(FormatTest
, MicrosoftDefaultStyle
) {
25468 FormatStyle Style
= getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp
);
25469 verifyFormat("extern \"C\"\n"
25475 TEST_F(FormatTest
, WebKitDefaultStyle
) {
25476 FormatStyle Style
= getWebKitStyle();
25477 verifyFormat("extern \"C\" {\n"
25483 TEST_F(FormatTest
, Concepts
) {
25484 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations
,
25485 FormatStyle::BBCDS_Always
);
25487 // The default in LLVM style is REI_OuterScope, but these tests were written
25488 // when the default was REI_Keyword.
25489 FormatStyle Style
= getLLVMStyle();
25490 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
25492 verifyFormat("template <typename T>\n"
25493 "concept True = true;");
25495 verifyFormat("template <typename T>\n"
25496 "concept C = ((false || foo()) && C2<T>) ||\n"
25497 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25498 getLLVMStyleWithColumns(60));
25500 verifyFormat("template <typename T>\n"
25501 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25502 "sizeof(T) <= 8;");
25504 verifyFormat("template <typename T>\n"
25505 "concept DelayedCheck = true && requires(T t) {\n"
25508 " } && sizeof(T) <= 8;",
25511 verifyFormat("template <typename T>\n"
25512 "concept DelayedCheck = true && requires(T t) { // Comment\n"
25515 " } && sizeof(T) <= 8;",
25518 verifyFormat("template <typename T>\n"
25519 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25520 "sizeof(T) <= 8;");
25522 verifyFormat("template <typename T>\n"
25523 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25525 verifyFormat("template <typename T>\n"
25526 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25528 verifyFormat("template <typename T>\n"
25529 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25531 verifyFormat("template <typename T>\n"
25532 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25533 "&& sizeof(T) <= 8;");
25535 verifyFormat("template <typename T>\n"
25536 "concept DelayedCheck =\n"
25537 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25538 "sizeof(T) <= 8;");
25540 verifyFormat("template <typename T>\n"
25541 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25542 "&& sizeof(T) <= 8;");
25545 "template <typename T>\n"
25546 "concept DelayedCheck =\n"
25547 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25549 verifyFormat("template <typename T>\n"
25550 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25551 "&& sizeof(T) <= 8;");
25553 verifyFormat("template <typename T>\n"
25554 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25555 "sizeof(T) <= 8;");
25557 verifyFormat("template <typename T>\n"
25558 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25559 " requires(T t) {\n"
25562 " } && sizeof(T) <= 8 && !(4 < 3);",
25563 getLLVMStyleWithColumns(60));
25565 verifyFormat("template <typename T>\n"
25566 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25568 verifyFormat("template <typename T>\n"
25569 "concept C = foo();");
25571 verifyFormat("template <typename T>\n"
25572 "concept C = foo(T());");
25574 verifyFormat("template <typename T>\n"
25575 "concept C = foo(T{});");
25577 verifyFormat("template <typename T>\n"
25578 "concept Size = V<sizeof(T)>::Value > 5;");
25580 verifyFormat("template <typename T>\n"
25581 "concept True = S<T>::Value;");
25583 verifyFormat("template <S T>\n"
25584 "concept True = T.field;");
25587 "template <typename T>\n"
25588 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25589 " sizeof(T) <= 8;");
25591 // FIXME: This is misformatted because the fake l paren starts at bool, not at
25592 // the lambda l square.
25593 verifyFormat("template <typename T>\n"
25594 "concept C = [] -> bool { return true; }() && requires(T t) { "
25596 " sizeof(T) <= 8;");
25599 "template <typename T>\n"
25600 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25601 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25603 verifyFormat("template <typename T>\n"
25604 "concept C = decltype([]() { return std::true_type{}; "
25605 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25606 getLLVMStyleWithColumns(120));
25608 verifyFormat("template <typename T>\n"
25609 "concept C = decltype([]() -> std::true_type { return {}; "
25611 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25613 verifyFormat("template <typename T>\n"
25614 "concept C = true;\n"
25617 verifyFormat("template <typename T>\n"
25618 "concept Hashable = requires(T a) {\n"
25619 " { std::hash<T>{}(a) } -> "
25620 "std::convertible_to<std::size_t>;\n"
25625 "template <typename T>\n"
25626 "concept EqualityComparable = requires(T a, T b) {\n"
25627 " { a == b } -> std::same_as<bool>;\n"
25632 "template <typename T>\n"
25633 "concept EqualityComparable = requires(T a, T b) {\n"
25634 " { a == b } -> std::same_as<bool>;\n"
25635 " { a != b } -> std::same_as<bool>;\n"
25639 verifyFormat("template <typename T>\n"
25640 "concept WeakEqualityComparable = requires(T a, T b) {\n"
25646 verifyFormat("template <typename T>\n"
25647 "concept HasSizeT = requires { typename T::size_t; };");
25649 verifyFormat("template <typename T>\n"
25650 "concept Semiregular =\n"
25651 " DefaultConstructible<T> && CopyConstructible<T> && "
25652 "CopyAssignable<T> &&\n"
25653 " requires(T a, std::size_t n) {\n"
25654 " requires Same<T *, decltype(&a)>;\n"
25655 " { a.~T() } noexcept;\n"
25656 " requires Same<T *, decltype(new T)>;\n"
25657 " requires Same<T *, decltype(new T[n])>;\n"
25658 " { delete new T; };\n"
25659 " { delete new T[n]; };\n"
25663 verifyFormat("template <typename T>\n"
25664 "concept Semiregular =\n"
25665 " requires(T a, std::size_t n) {\n"
25666 " requires Same<T *, decltype(&a)>;\n"
25667 " { a.~T() } noexcept;\n"
25668 " requires Same<T *, decltype(new T)>;\n"
25669 " requires Same<T *, decltype(new T[n])>;\n"
25670 " { delete new T; };\n"
25671 " { delete new T[n]; };\n"
25672 " { new T } -> std::same_as<T *>;\n"
25673 " } && DefaultConstructible<T> && CopyConstructible<T> && "
25674 "CopyAssignable<T>;",
25678 "template <typename T>\n"
25679 "concept Semiregular =\n"
25680 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
25681 " requires Same<T *, decltype(&a)>;\n"
25682 " { a.~T() } noexcept;\n"
25683 " requires Same<T *, decltype(new T)>;\n"
25684 " requires Same<T *, decltype(new "
25686 " { delete new T; };\n"
25687 " { delete new T[n]; };\n"
25688 " } && CopyConstructible<T> && "
25689 "CopyAssignable<T>;",
25692 verifyFormat("template <typename T>\n"
25693 "concept Two = requires(T t) {\n"
25694 " { t.foo() } -> std::same_as<Bar>;\n"
25695 " } && requires(T &&t) {\n"
25696 " { t.foo() } -> std::same_as<Bar &&>;\n"
25701 "template <typename T>\n"
25702 "concept C = requires(T x) {\n"
25703 " { *x } -> std::convertible_to<typename T::inner>;\n"
25704 " { x + 1 } noexcept -> std::same_as<int>;\n"
25705 " { x * 1 } -> std::convertible_to<T>;\n"
25709 verifyFormat("template <typename T>\n"
25710 "concept C = requires(T x) {\n"
25712 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25713 " } -> long_long_concept_name<T>;\n"
25715 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25716 " } noexcept -> long_long_concept_name<T>;\n"
25721 "template <typename T, typename U = T>\n"
25722 "concept Swappable = requires(T &&t, U &&u) {\n"
25723 " swap(std::forward<T>(t), std::forward<U>(u));\n"
25724 " swap(std::forward<U>(u), std::forward<T>(t));\n"
25728 verifyFormat("template <typename T, typename U>\n"
25729 "concept Common = requires(T &&t, U &&u) {\n"
25730 " typename CommonType<T, U>;\n"
25731 " { CommonType<T, U>(std::forward<T>(t)) };\n"
25735 verifyFormat("template <typename T, typename U>\n"
25736 "concept Common = requires(T &&t, U &&u) {\n"
25737 " typename CommonType<T, U>;\n"
25738 " { CommonType<T, U>{std::forward<T>(t)} };\n"
25743 "template <typename T>\n"
25744 "concept C = requires(T t) {\n"
25745 " requires Bar<T> && Foo<T>;\n"
25746 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25750 verifyFormat("template <typename T>\n"
25751 "concept HasFoo = requires(T t) {\n"
25755 "template <typename T>\n"
25756 "concept HasBar = requires(T t) {\n"
25762 verifyFormat("template <typename T>\n"
25763 "concept Large = sizeof(T) > 10;");
25765 verifyFormat("template <typename T, typename U>\n"
25766 "concept FooableWith = requires(T t, U u) {\n"
25767 " typename T::foo_type;\n"
25768 " { t.foo(u) } -> typename T::foo_type;\n"
25771 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25774 verifyFormat("template <typename T>\n"
25775 "concept Context = is_specialization_of_v<context, T>;");
25777 verifyFormat("template <typename T>\n"
25778 "concept Node = std::is_object_v<T>;");
25780 verifyFormat("template <class T>\n"
25781 "concept integral = __is_integral(T);");
25783 verifyFormat("template <class T>\n"
25784 "concept is2D = __array_extent(T, 1) == 2;");
25786 verifyFormat("template <class T>\n"
25787 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25789 verifyFormat("template <class T, class T2>\n"
25790 "concept Same = __is_same_as<T, T2>;");
25793 "template <class _InIt, class _OutIt>\n"
25794 "concept _Can_reread_dest =\n"
25795 " std::forward_iterator<_OutIt> &&\n"
25796 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25798 Style
.BreakBeforeConceptDeclarations
= FormatStyle::BBCDS_Allowed
;
25801 "template <typename T>\n"
25802 "concept C = requires(T t) {\n"
25803 " requires Bar<T> && Foo<T>;\n"
25804 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25808 verifyFormat("template <typename T>\n"
25809 "concept HasFoo = requires(T t) {\n"
25813 "template <typename T>\n"
25814 "concept HasBar = requires(T t) {\n"
25820 verifyFormat("template <typename T> concept True = true;", Style
);
25822 verifyFormat("template <typename T>\n"
25823 "concept C = decltype([]() -> std::true_type { return {}; "
25825 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25828 verifyFormat("template <typename T>\n"
25829 "concept Semiregular =\n"
25830 " DefaultConstructible<T> && CopyConstructible<T> && "
25831 "CopyAssignable<T> &&\n"
25832 " requires(T a, std::size_t n) {\n"
25833 " requires Same<T *, decltype(&a)>;\n"
25834 " { a.~T() } noexcept;\n"
25835 " requires Same<T *, decltype(new T)>;\n"
25836 " requires Same<T *, decltype(new T[n])>;\n"
25837 " { delete new T; };\n"
25838 " { delete new T[n]; };\n"
25842 Style
.BreakBeforeConceptDeclarations
= FormatStyle::BBCDS_Never
;
25844 verifyFormat("template <typename T> concept C =\n"
25845 " requires(T t) {\n"
25846 " requires Bar<T> && Foo<T>;\n"
25847 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25851 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25855 "template <typename T> concept HasBar = requires(T t) {\n"
25861 verifyFormat("template <typename T> concept True = true;", Style
);
25864 "template <typename T> concept C =\n"
25865 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
25866 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25869 verifyFormat("template <typename T> concept Semiregular =\n"
25870 " DefaultConstructible<T> && CopyConstructible<T> && "
25871 "CopyAssignable<T> &&\n"
25872 " requires(T a, std::size_t n) {\n"
25873 " requires Same<T *, decltype(&a)>;\n"
25874 " { a.~T() } noexcept;\n"
25875 " requires Same<T *, decltype(new T)>;\n"
25876 " requires Same<T *, decltype(new T[n])>;\n"
25877 " { delete new T; };\n"
25878 " { delete new T[n]; };\n"
25882 // The following tests are invalid C++, we just want to make sure we don't
25884 verifyNoCrash("template <typename T>\n"
25885 "concept C = requires C2<T>;");
25887 verifyNoCrash("template <typename T>\n"
25888 "concept C = 5 + 4;");
25890 verifyNoCrash("template <typename T>\n"
25891 "concept C = class X;");
25893 verifyNoCrash("template <typename T>\n"
25894 "concept C = [] && true;");
25896 verifyNoCrash("template <typename T>\n"
25897 "concept C = [] && requires(T t) { typename T::size_type; };");
25900 TEST_F(FormatTest
, RequiresClausesPositions
) {
25901 auto Style
= getLLVMStyle();
25902 EXPECT_EQ(Style
.RequiresClausePosition
, FormatStyle::RCPS_OwnLine
);
25903 EXPECT_EQ(Style
.IndentRequiresClause
, true);
25905 // The default in LLVM style is REI_OuterScope, but these tests were written
25906 // when the default was REI_Keyword.
25907 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
25909 verifyFormat("template <typename T>\n"
25910 " requires(Foo<T> && std::trait<T>)\n"
25914 verifyFormat("template <typename T>\n"
25915 " requires(Foo<T> && std::trait<T>)\n"
25924 "template <typename T>\n"
25925 " requires requires(T &&t) {\n"
25926 " typename T::I;\n"
25927 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
25929 "Bar(T) -> Bar<typename T::I>;",
25932 verifyFormat("template <typename T>\n"
25933 " requires(Foo<T> && std::trait<T>)\n"
25934 "constexpr T MyGlobal;",
25937 verifyFormat("template <typename T>\n"
25938 " requires Foo<T> && requires(T t) {\n"
25939 " { t.baz() } -> std::same_as<bool>;\n"
25940 " requires std::same_as<T::Factor, int>;\n"
25942 "inline int bar(T t) {\n"
25943 " return t.baz() ? T::Factor : 5;\n"
25947 verifyFormat("template <typename T>\n"
25948 "inline int bar(T t)\n"
25949 " requires Foo<T> && requires(T t) {\n"
25950 " { t.baz() } -> std::same_as<bool>;\n"
25951 " requires std::same_as<T::Factor, int>;\n"
25954 " return t.baz() ? T::Factor : 5;\n"
25958 verifyFormat("template <typename T>\n"
25965 verifyFormat("template <typename T>\n"
25973 verifyFormat("template <typename T>\n"
25974 "int S::bar(T t) &&\n"
25981 verifyFormat("template <typename T>\n"
25986 Style
.IndentRequiresClause
= false;
25987 verifyFormat("template <typename T>\n"
25994 verifyFormat("template <typename T>\n"
25995 "int S::bar(T t) &&\n"
26002 verifyFormat("template <typename T>\n"
26010 Style
.RequiresClausePosition
= FormatStyle::RCPS_OwnLineWithBrace
;
26011 Style
.IndentRequiresClause
= true;
26013 verifyFormat("template <typename T>\n"
26014 " requires(Foo<T> && std::trait<T>)\n"
26018 verifyFormat("template <typename T>\n"
26019 " requires(Foo<T> && std::trait<T>)\n"
26028 "template <typename T>\n"
26029 " requires requires(T &&t) {\n"
26030 " typename T::I;\n"
26031 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26033 "Bar(T) -> Bar<typename T::I>;",
26036 verifyFormat("template <typename T>\n"
26037 " requires(Foo<T> && std::trait<T>)\n"
26038 "constexpr T MyGlobal;",
26041 verifyFormat("template <typename T>\n"
26042 " requires Foo<T> && requires(T t) {\n"
26043 " { t.baz() } -> std::same_as<bool>;\n"
26044 " requires std::same_as<T::Factor, int>;\n"
26046 "inline int bar(T t) {\n"
26047 " return t.baz() ? T::Factor : 5;\n"
26051 verifyFormat("template <typename T>\n"
26052 "inline int bar(T t)\n"
26053 " requires Foo<T> && requires(T t) {\n"
26054 " { t.baz() } -> std::same_as<bool>;\n"
26055 " requires std::same_as<T::Factor, int>;\n"
26057 " return t.baz() ? T::Factor : 5;\n"
26061 verifyFormat("template <typename T>\n"
26068 verifyFormat("template <typename T>\n"
26070 " requires F<T> {\n"
26075 verifyFormat("template <typename T>\n"
26076 "int S::bar(T t) &&\n"
26077 " requires F<T> {\n"
26082 verifyFormat("template <typename T>\n"
26087 verifyFormat("template <typename T>\n"
26089 " requires F<T> {}",
26092 Style
.RequiresClausePosition
= FormatStyle::RCPS_SingleLine
;
26093 Style
.IndentRequiresClause
= false;
26094 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26095 "template <typename T> requires Foo<T> void bar() {}\n"
26096 "template <typename T> void bar() requires Foo<T> {}\n"
26097 "template <typename T> void bar() requires Foo<T>;\n"
26098 "template <typename T> void S::bar() && requires Foo<T> {}\n"
26099 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26102 auto ColumnStyle
= Style
;
26103 ColumnStyle
.ColumnLimit
= 40;
26104 verifyFormat("template <typename AAAAAAA>\n"
26105 "requires Foo<T> struct Bar {};\n"
26106 "template <typename AAAAAAA>\n"
26107 "requires Foo<T> void bar() {}\n"
26108 "template <typename AAAAAAA>\n"
26109 "void bar() requires Foo<T> {}\n"
26110 "template <typename T>\n"
26111 "void S::bar() && requires Foo<T> {}\n"
26112 "template <typename AAAAAAA>\n"
26113 "requires Foo<T> Baz(T) -> Baz<T>;",
26116 verifyFormat("template <typename T>\n"
26117 "requires Foo<AAAAAAA> struct Bar {};\n"
26118 "template <typename T>\n"
26119 "requires Foo<AAAAAAA> void bar() {}\n"
26120 "template <typename T>\n"
26121 "void bar() requires Foo<AAAAAAA> {}\n"
26122 "template <typename T>\n"
26123 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26126 verifyFormat("template <typename AAAAAAA>\n"
26127 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26129 "template <typename AAAAAAA>\n"
26130 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26132 "template <typename AAAAAAA>\n"
26134 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26135 "template <typename AAAAAAA>\n"
26136 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26137 "template <typename AAAAAAA>\n"
26138 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26139 "Bar(T) -> Bar<T>;",
26142 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithFollowing
;
26143 ColumnStyle
.RequiresClausePosition
= FormatStyle::RCPS_WithFollowing
;
26145 verifyFormat("template <typename T>\n"
26146 "requires Foo<T> struct Bar {};\n"
26147 "template <typename T>\n"
26148 "requires Foo<T> void bar() {}\n"
26149 "template <typename T>\n"
26151 "requires Foo<T> {}\n"
26152 "template <typename T>\n"
26154 "requires Foo<T>;\n"
26155 "template <typename T>\n"
26156 "void S::bar() &&\n"
26157 "requires Foo<T> {}\n"
26158 "template <typename T>\n"
26159 "requires Foo<T> Bar(T) -> Bar<T>;",
26162 verifyFormat("template <typename AAAAAAA>\n"
26163 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26165 "template <typename AAAAAAA>\n"
26166 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26168 "template <typename AAAAAAA>\n"
26170 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26171 "template <typename AAAAAAA>\n"
26172 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26173 "template <typename AAAAAAA>\n"
26174 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26175 "Bar(T) -> Bar<T>;",
26178 Style
.IndentRequiresClause
= true;
26179 ColumnStyle
.IndentRequiresClause
= true;
26181 verifyFormat("template <typename T>\n"
26182 " requires Foo<T> struct Bar {};\n"
26183 "template <typename T>\n"
26184 " requires Foo<T> void bar() {}\n"
26185 "template <typename T>\n"
26187 " requires Foo<T> {}\n"
26188 "template <typename T>\n"
26189 "void S::bar() &&\n"
26190 " requires Foo<T> {}\n"
26191 "template <typename T>\n"
26192 " requires Foo<T> Bar(T) -> Bar<T>;",
26195 verifyFormat("template <typename AAAAAAA>\n"
26196 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26198 "template <typename AAAAAAA>\n"
26199 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26201 "template <typename AAAAAAA>\n"
26203 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26204 "template <typename AAAAAAA>\n"
26205 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26206 "template <typename AAAAAAA>\n"
26207 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26208 "Bar(T) -> Bar<T>;",
26211 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
26212 ColumnStyle
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
26214 verifyFormat("template <typename T> requires Foo<T>\n"
26216 "template <typename T> requires Foo<T>\n"
26218 "template <typename T>\n"
26219 "void bar() requires Foo<T>\n"
26221 "template <typename T> void bar() requires Foo<T>;\n"
26222 "template <typename T>\n"
26223 "void S::bar() && requires Foo<T>\n"
26225 "template <typename T> requires Foo<T>\n"
26226 "Bar(T) -> Bar<T>;",
26229 verifyFormat("template <typename AAAAAAA>\n"
26230 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26232 "template <typename AAAAAAA>\n"
26233 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26235 "template <typename AAAAAAA>\n"
26237 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26239 "template <typename AAAAAAA>\n"
26240 "requires Foo<AAAAAAAA>\n"
26241 "Bar(T) -> Bar<T>;\n"
26242 "template <typename AAAAAAA>\n"
26243 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26244 "Bar(T) -> Bar<T>;",
26248 TEST_F(FormatTest
, RequiresClauses
) {
26249 verifyFormat("struct [[nodiscard]] zero_t {\n"
26250 " template <class T>\n"
26251 " requires requires { number_zero_v<T>; }\n"
26252 " [[nodiscard]] constexpr operator T() const {\n"
26253 " return number_zero_v<T>;\n"
26257 verifyFormat("template <class T>\n"
26258 " requires(std::same_as<int, T>)\n"
26259 "decltype(auto) fun() {}");
26261 auto Style
= getLLVMStyle();
26264 "template <typename T>\n"
26265 " requires is_default_constructible_v<hash<T>> and\n"
26266 " is_copy_constructible_v<hash<T>> and\n"
26267 " is_move_constructible_v<hash<T>> and\n"
26268 " is_copy_assignable_v<hash<T>> and "
26269 "is_move_assignable_v<hash<T>> and\n"
26270 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
26271 " is_callable_v<hash<T>(T)> and\n"
26272 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
26273 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
26274 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
26278 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
26280 "template <typename T>\n"
26281 " requires is_default_constructible_v<hash<T>>\n"
26282 " and is_copy_constructible_v<hash<T>>\n"
26283 " and is_move_constructible_v<hash<T>>\n"
26284 " and is_copy_assignable_v<hash<T>> and "
26285 "is_move_assignable_v<hash<T>>\n"
26286 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
26287 " and is_callable_v<hash<T>(T)>\n"
26288 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
26289 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
26290 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
26295 Style
= getLLVMStyle();
26296 Style
.ConstructorInitializerIndentWidth
= 4;
26297 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
26298 Style
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
26299 verifyFormat("constexpr Foo(Foo const &other)\n"
26300 " requires std::is_copy_constructible<T>\n"
26301 " : value{other.value} {\n"
26303 " do_more_magic();\n"
26307 // Not a clause, but we once hit an assert.
26308 verifyFormat("#if 0\n"
26315 TEST_F(FormatTest
, RequiresExpressionIndentation
) {
26316 auto Style
= getLLVMStyle();
26317 EXPECT_EQ(Style
.RequiresExpressionIndentation
, FormatStyle::REI_OuterScope
);
26319 verifyFormat("template <typename T>\n"
26320 "concept C = requires(T t) {\n"
26321 " typename T::value;\n"
26322 " requires requires(typename T::value v) {\n"
26323 " { t == v } -> std::same_as<bool>;\n"
26328 verifyFormat("template <typename T>\n"
26330 " requires Foo<T> && requires(T t) {\n"
26331 " { t.foo() } -> std::same_as<int>;\n"
26332 " } && requires(T t) {\n"
26333 " { t.bar() } -> std::same_as<bool>;\n"
26338 verifyFormat("template <typename T>\n"
26339 " requires Foo<T> &&\n"
26340 " requires(T t) {\n"
26341 " { t.foo() } -> std::same_as<int>;\n"
26342 " } && requires(T t) {\n"
26343 " { t.bar() } -> std::same_as<bool>;\n"
26349 verifyFormat("template <typename T> void f() {\n"
26350 " if constexpr (requires(T t) {\n"
26351 " { t.bar() } -> std::same_as<bool>;\n"
26357 verifyFormat("template <typename T> void f() {\n"
26358 " if constexpr (condition && requires(T t) {\n"
26359 " { t.bar() } -> std::same_as<bool>;\n"
26365 verifyFormat("template <typename T> struct C {\n"
26367 " requires requires(T t) {\n"
26368 " { t.bar() } -> std::same_as<bool>;\n"
26373 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
26375 verifyFormat("template <typename T>\n"
26376 "concept C = requires(T t) {\n"
26377 " typename T::value;\n"
26378 " requires requires(typename T::value v) {\n"
26379 " { t == v } -> std::same_as<bool>;\n"
26385 "template <typename T>\n"
26387 " requires Foo<T> && requires(T t) {\n"
26388 " { t.foo() } -> std::same_as<int>;\n"
26389 " } && requires(T t) {\n"
26390 " { t.bar() } -> std::same_as<bool>;\n"
26395 verifyFormat("template <typename T>\n"
26396 " requires Foo<T> &&\n"
26397 " requires(T t) {\n"
26398 " { t.foo() } -> std::same_as<int>;\n"
26399 " } && requires(T t) {\n"
26400 " { t.bar() } -> std::same_as<bool>;\n"
26406 verifyFormat("template <typename T> void f() {\n"
26407 " if constexpr (requires(T t) {\n"
26408 " { t.bar() } -> std::same_as<bool>;\n"
26415 "template <typename T> void f() {\n"
26416 " if constexpr (condition && requires(T t) {\n"
26417 " { t.bar() } -> std::same_as<bool>;\n"
26423 verifyFormat("template <typename T> struct C {\n"
26425 " requires requires(T t) {\n"
26426 " { t.bar() } -> std::same_as<bool>;\n"
26432 TEST_F(FormatTest
, StatementAttributeLikeMacros
) {
26433 FormatStyle Style
= getLLVMStyle();
26434 StringRef Source
= "void Foo::slot() {\n"
26435 " unsigned char MyChar = 'x';\n"
26436 " emit signal(MyChar);\n"
26437 " Q_EMIT signal(MyChar);\n"
26440 verifyFormat(Source
, Style
);
26442 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
26443 verifyFormat("void Foo::slot() {\n"
26444 " unsigned char MyChar = 'x';\n"
26445 " emit signal(MyChar);\n"
26446 " Q_EMIT signal(MyChar);\n"
26450 Style
.StatementAttributeLikeMacros
.push_back("emit");
26451 verifyFormat(Source
, Style
);
26453 Style
.StatementAttributeLikeMacros
= {};
26454 verifyFormat("void Foo::slot() {\n"
26455 " unsigned char MyChar = 'x';\n"
26456 " emit signal(MyChar);\n"
26457 " Q_EMIT signal(MyChar);\n"
26462 TEST_F(FormatTest
, IndentAccessModifiers
) {
26463 FormatStyle Style
= getLLVMStyle();
26464 Style
.IndentAccessModifiers
= true;
26465 // Members are *two* levels below the record;
26466 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
26467 verifyFormat("class C {\n"
26471 verifyFormat("union C {\n"
26476 // Access modifiers should be indented one level below the record.
26477 verifyFormat("class C {\n"
26482 verifyFormat("class C {\n"
26483 " public /* comment */:\n"
26487 verifyFormat("struct S {\n"
26500 // Enumerations are not records and should be unaffected.
26501 Style
.AllowShortEnumsOnASingleLine
= false;
26502 verifyFormat("enum class E {\n"
26507 // Test with a different indentation width;
26508 // also proves that the result is Style.AccessModifierOffset agnostic.
26509 Style
.IndentWidth
= 3;
26510 verifyFormat("class C {\n"
26515 verifyFormat("class C {\n"
26520 Style
.AttributeMacros
.push_back("FOO");
26521 verifyFormat("class C {\n"
26528 TEST_F(FormatTest
, LimitlessStringsAndComments
) {
26529 auto Style
= getLLVMStyleWithColumns(0);
26530 constexpr StringRef Code
=
26532 " * This is a multiline comment with quite some long lines, at least for "
26533 "the LLVM Style.\n"
26534 " * We will redo this with strings and line comments. Just to check if "
26535 "everything is working.\n"
26538 " /* Single line multi line comment. */\n"
26539 " const std::string String = \"This is a multiline string with quite "
26540 "some long lines, at least for the LLVM Style.\"\n"
26541 " \"We already did it with multi line "
26542 "comments, and we will do it with line comments. Just to check if "
26543 "everything is working.\";\n"
26544 " // This is a line comment (block) with quite some long lines, at "
26545 "least for the LLVM Style.\n"
26546 " // We already did this with multi line comments and strings. Just to "
26547 "check if everything is working.\n"
26548 " const std::string SmallString = \"Hello World\";\n"
26549 " // Small line comment\n"
26550 " return String.size() > SmallString.size();\n"
26552 verifyNoChange(Code
, Style
);
26555 TEST_F(FormatTest
, FormatDecayCopy
) {
26556 // error cases from unit tests
26557 verifyFormat("foo(auto())");
26558 verifyFormat("foo(auto{})");
26559 verifyFormat("foo(auto({}))");
26560 verifyFormat("foo(auto{{}})");
26562 verifyFormat("foo(auto(1))");
26563 verifyFormat("foo(auto{1})");
26564 verifyFormat("foo(new auto(1))");
26565 verifyFormat("foo(new auto{1})");
26566 verifyFormat("decltype(auto(1)) x;");
26567 verifyFormat("decltype(auto{1}) x;");
26568 verifyFormat("auto(x);");
26569 verifyFormat("auto{x};");
26570 verifyFormat("new auto{x};");
26571 verifyFormat("auto{x} = y;");
26572 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26573 // the user's own fault
26574 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26575 // clearly the user's own fault
26576 verifyFormat("auto (*p)() = f;");
26579 TEST_F(FormatTest
, Cpp20ModulesSupport
) {
26580 FormatStyle Style
= getLLVMStyle();
26581 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
26582 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
26584 verifyFormat("export import foo;", Style
);
26585 verifyFormat("export import foo:bar;", Style
);
26586 verifyFormat("export import foo.bar;", Style
);
26587 verifyFormat("export import foo.bar:baz;", Style
);
26588 verifyFormat("export import :bar;", Style
);
26589 verifyFormat("export module foo:bar;", Style
);
26590 verifyFormat("export module foo;", Style
);
26591 verifyFormat("export module foo.bar;", Style
);
26592 verifyFormat("export module foo.bar:baz;", Style
);
26593 verifyFormat("export import <string_view>;", Style
);
26594 verifyFormat("export import <Foo/Bar>;", Style
);
26596 verifyFormat("export type_name var;", Style
);
26597 verifyFormat("template <class T> export using A = B<T>;", Style
);
26598 verifyFormat("export using A = B;", Style
);
26599 verifyFormat("export int func() {\n"
26603 verifyFormat("export struct {\n"
26607 verifyFormat("export {\n"
26611 verifyFormat("export export char const *hello() { return \"hello\"; }");
26613 verifyFormat("import bar;", Style
);
26614 verifyFormat("import foo.bar;", Style
);
26615 verifyFormat("import foo:bar;", Style
);
26616 verifyFormat("import :bar;", Style
);
26617 verifyFormat("import /* module partition */ :bar;", Style
);
26618 verifyFormat("import <ctime>;", Style
);
26619 verifyFormat("import \"header\";", Style
);
26621 verifyFormat("module foo;", Style
);
26622 verifyFormat("module foo:bar;", Style
);
26623 verifyFormat("module foo.bar;", Style
);
26624 verifyFormat("module;", Style
);
26626 verifyFormat("export namespace hi {\n"
26627 "const char *sayhi();\n"
26631 verifyFormat("module :private;", Style
);
26632 verifyFormat("import <foo/bar.h>;", Style
);
26633 verifyFormat("import foo...bar;", Style
);
26634 verifyFormat("import ..........;", Style
);
26635 verifyFormat("module foo:private;", Style
);
26636 verifyFormat("import a", Style
);
26637 verifyFormat("module a", Style
);
26638 verifyFormat("export import a", Style
);
26639 verifyFormat("export module a", Style
);
26641 verifyFormat("import", Style
);
26642 verifyFormat("module", Style
);
26643 verifyFormat("export", Style
);
26645 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26648 TEST_F(FormatTest
, CoroutineForCoawait
) {
26649 FormatStyle Style
= getLLVMStyle();
26650 verifyFormat("for co_await (auto x : range())\n ;");
26651 verifyFormat("for (auto i : arr) {\n"
26654 verifyFormat("for co_await (auto i : arr) {\n"
26657 verifyFormat("for co_await (auto i : foo(T{})) {\n"
26662 TEST_F(FormatTest
, CoroutineCoAwait
) {
26663 verifyFormat("int x = co_await foo();");
26664 verifyFormat("int x = (co_await foo());");
26665 verifyFormat("co_await (42);");
26666 verifyFormat("void operator co_await(int);");
26667 verifyFormat("void operator co_await(a);");
26668 verifyFormat("co_await a;");
26669 verifyFormat("co_await missing_await_resume{};");
26670 verifyFormat("co_await a; // comment");
26671 verifyFormat("void test0() { co_await a; }");
26672 verifyFormat("co_await co_await co_await foo();");
26673 verifyFormat("co_await foo().bar();");
26674 verifyFormat("co_await [this]() -> Task { co_return x; }");
26675 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26676 "foo(); }(x, y);");
26678 FormatStyle Style
= getLLVMStyleWithColumns(40);
26679 verifyFormat("co_await [this](int a, int b) -> Task {\n"
26680 " co_return co_await foo();\n"
26683 verifyFormat("co_await;");
26686 TEST_F(FormatTest
, CoroutineCoYield
) {
26687 verifyFormat("int x = co_yield foo();");
26688 verifyFormat("int x = (co_yield foo());");
26689 verifyFormat("co_yield (42);");
26690 verifyFormat("co_yield {42};");
26691 verifyFormat("co_yield 42;");
26692 verifyFormat("co_yield n++;");
26693 verifyFormat("co_yield ++n;");
26694 verifyFormat("co_yield;");
26697 TEST_F(FormatTest
, CoroutineCoReturn
) {
26698 verifyFormat("co_return (42);");
26699 verifyFormat("co_return;");
26700 verifyFormat("co_return {};");
26701 verifyFormat("co_return x;");
26702 verifyFormat("co_return co_await foo();");
26703 verifyFormat("co_return co_yield foo();");
26706 TEST_F(FormatTest
, EmptyShortBlock
) {
26707 auto Style
= getLLVMStyle();
26708 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Empty
;
26710 verifyFormat("try {\n"
26712 "} catch (Exception &e) {\n"
26713 " e.printStackTrace();\n"
26717 verifyFormat("try {\n"
26719 "} catch (Exception &e) {}",
26723 TEST_F(FormatTest
, ShortTemplatedArgumentLists
) {
26724 auto Style
= getLLVMStyle();
26726 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style
);
26727 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style
);
26728 verifyFormat("struct Y : X<[] { return 0; }> {};", Style
);
26729 verifyFormat("struct Y<[] { return 0; }> {};", Style
);
26731 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style
);
26732 verifyFormat("template <int N> struct Foo<char[N]> {};", Style
);
26735 TEST_F(FormatTest
, MultilineLambdaInConditional
) {
26736 auto Style
= getLLVMStyleWithColumns(70);
26737 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26744 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26750 Style
= getLLVMStyleWithColumns(60);
26751 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26758 verifyFormat("auto aLengthyIdentifier =\n"
26759 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
26765 Style
= getLLVMStyleWithColumns(40);
26766 verifyFormat("auto aLengthyIdentifier =\n"
26767 " oneExpressionSoThatWeBreak ? []() {\n"
26773 verifyFormat("auto aLengthyIdentifier =\n"
26774 " oneExpressionSoThatWeBreak\n"
26783 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndent
) {
26784 auto Style
= getLLVMStyle();
26786 StringRef Short
= "functionCall(paramA, paramB, paramC);\n"
26787 "void functionDecl(int a, int b, int c);";
26789 StringRef Medium
= "functionCall(paramA, paramB, paramC, paramD, paramE, "
26790 "paramF, paramG, paramH, paramI);\n"
26791 "void functionDecl(int argumentA, int argumentB, int "
26792 "argumentC, int argumentD, int argumentE);";
26794 verifyFormat(Short
, Style
);
26796 StringRef NoBreak
= "functionCall(paramA, paramB, paramC, paramD, paramE, "
26797 "paramF, paramG, paramH,\n"
26799 "void functionDecl(int argumentA, int argumentB, int "
26800 "argumentC, int argumentD,\n"
26801 " int argumentE);";
26803 verifyFormat(NoBreak
, Medium
, Style
);
26804 verifyFormat(NoBreak
,
26816 "void functionDecl(\n"
26817 " int argumentA,\n"
26818 " int argumentB,\n"
26819 " int argumentC,\n"
26820 " int argumentD,\n"
26825 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26826 " nestedLongFunctionCall(argument1, "
26827 "argument2, argument3,\n"
26832 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
26834 verifyFormat(Short
, Style
);
26837 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26840 "void functionDecl(\n"
26841 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26846 Style
.AllowAllArgumentsOnNextLine
= false;
26847 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
26849 verifyFormat(Short
, Style
);
26852 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26855 "void functionDecl(\n"
26856 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26861 Style
.BinPackArguments
= false;
26862 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
26864 verifyFormat(Short
, Style
);
26866 verifyFormat("functionCall(\n"
26877 "void functionDecl(\n"
26878 " int argumentA,\n"
26879 " int argumentB,\n"
26880 " int argumentC,\n"
26881 " int argumentD,\n"
26886 verifyFormat("outerFunctionCall(\n"
26887 " nestedFunctionCall(argument1),\n"
26888 " nestedLongFunctionCall(\n"
26898 verifyFormat("int a = (int)b;", Style
);
26899 verifyFormat("int a = (int)b;",
26905 verifyFormat("return (true);", Style
);
26906 verifyFormat("return (true);",
26912 verifyFormat("void foo();", Style
);
26913 verifyFormat("void foo();",
26918 verifyFormat("void foo() {}", Style
);
26919 verifyFormat("void foo() {}",
26925 verifyFormat("auto string = std::string();", Style
);
26926 verifyFormat("auto string = std::string();",
26927 "auto string = std::string(\n"
26931 verifyFormat("void (*functionPointer)() = nullptr;", Style
);
26932 verifyFormat("void (*functionPointer)() = nullptr;",
26934 " *functionPointer\n"
26941 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentIfStatement
) {
26942 auto Style
= getLLVMStyle();
26944 verifyFormat("if (foo()) {\n"
26949 verifyFormat("if (quiteLongArg !=\n"
26950 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26956 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
26958 verifyFormat("if (foo()) {\n"
26963 verifyFormat("if (quiteLongArg !=\n"
26964 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26970 verifyFormat("void foo() {\n"
26971 " if (camelCaseName < alsoLongName ||\n"
26972 " anotherEvenLongerName <=\n"
26973 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
26975 " otherName < thisLastName) {\n"
26977 " } else if (quiteLongName < alsoLongName ||\n"
26978 " anotherEvenLongerName <=\n"
26979 " thisReallyReallyReallyReallyReallyReallyLonger"
26981 " otherName < thisLastName) {\n"
26987 Style
.ContinuationIndentWidth
= 2;
26988 verifyFormat("void foo() {\n"
26989 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
26990 " ontoMultipleLines && whenFormattedCorrectly) {\n"
26993 " } else if (thisIsRatherALongIfClause && "
26994 "thatIExpectToBeBroken ||\n"
26995 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27003 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentForStatement
) {
27004 auto Style
= getLLVMStyle();
27006 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27007 " doSomething();\n"
27011 verifyFormat("for (int myReallyLongCountVariable = 0; "
27012 "myReallyLongCountVariable < count;\n"
27013 " myReallyLongCountVariable++) {\n"
27014 " doSomething();\n"
27018 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
27020 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27021 " doSomething();\n"
27025 verifyFormat("for (int myReallyLongCountVariable = 0; "
27026 "myReallyLongCountVariable < count;\n"
27027 " myReallyLongCountVariable++) {\n"
27028 " doSomething();\n"
27033 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentInitializers
) {
27034 auto Style
= getLLVMStyleWithColumns(60);
27035 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
27036 // Aggregate initialization.
27037 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27038 " 10000000, 20000000\n"
27041 verifyFormat("SomeStruct s{\n"
27042 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
27043 " \"zzzzzzzzzzzzzzzz\"\n"
27046 // Designated initializers.
27047 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27048 " [0] = 10000000, [1] = 20000000\n"
27051 verifyFormat("SomeStruct s{\n"
27052 " .foo = \"xxxxxxxxxxxxx\",\n"
27053 " .bar = \"yyyyyyyyyyyyy\",\n"
27054 " .baz = \"zzzzzzzzzzzzz\"\n"
27057 // List initialization.
27058 verifyFormat("SomeStruct s{\n"
27059 " \"xxxxxxxxxxxxx\",\n"
27060 " \"yyyyyyyyyyyyy\",\n"
27061 " \"zzzzzzzzzzzzz\",\n"
27064 verifyFormat("SomeStruct{\n"
27065 " \"xxxxxxxxxxxxx\",\n"
27066 " \"yyyyyyyyyyyyy\",\n"
27067 " \"zzzzzzzzzzzzz\",\n"
27070 verifyFormat("new SomeStruct{\n"
27071 " \"xxxxxxxxxxxxx\",\n"
27072 " \"yyyyyyyyyyyyy\",\n"
27073 " \"zzzzzzzzzzzzz\",\n"
27076 // Member initializer.
27077 verifyFormat("class SomeClass {\n"
27079 " \"xxxxxxxxxxxxx\",\n"
27080 " \"yyyyyyyyyyyyy\",\n"
27081 " \"zzzzzzzzzzzzz\",\n"
27085 // Constructor member initializer.
27086 verifyFormat("SomeClass::SomeClass : strct{\n"
27087 " \"xxxxxxxxxxxxx\",\n"
27088 " \"yyyyyyyyyyyyy\",\n"
27089 " \"zzzzzzzzzzzzz\",\n"
27092 // Copy initialization.
27093 verifyFormat("SomeStruct s = SomeStruct{\n"
27094 " \"xxxxxxxxxxxxx\",\n"
27095 " \"yyyyyyyyyyyyy\",\n"
27096 " \"zzzzzzzzzzzzz\",\n"
27099 // Copy list initialization.
27100 verifyFormat("SomeStruct s = {\n"
27101 " \"xxxxxxxxxxxxx\",\n"
27102 " \"yyyyyyyyyyyyy\",\n"
27103 " \"zzzzzzzzzzzzz\",\n"
27106 // Assignment operand initialization.
27107 verifyFormat("s = {\n"
27108 " \"xxxxxxxxxxxxx\",\n"
27109 " \"yyyyyyyyyyyyy\",\n"
27110 " \"zzzzzzzzzzzzz\",\n"
27113 // Returned object initialization.
27114 verifyFormat("return {\n"
27115 " \"xxxxxxxxxxxxx\",\n"
27116 " \"yyyyyyyyyyyyy\",\n"
27117 " \"zzzzzzzzzzzzz\",\n"
27120 // Initializer list.
27121 verifyFormat("auto initializerList = {\n"
27122 " \"xxxxxxxxxxxxx\",\n"
27123 " \"yyyyyyyyyyyyy\",\n"
27124 " \"zzzzzzzzzzzzz\",\n"
27127 // Function parameter initialization.
27128 verifyFormat("func({\n"
27129 " \"xxxxxxxxxxxxx\",\n"
27130 " \"yyyyyyyyyyyyy\",\n"
27131 " \"zzzzzzzzzzzzz\",\n"
27134 // Nested init lists.
27135 verifyFormat("SomeStruct s = {\n"
27136 " {{init1, init2, init3, init4, init5},\n"
27137 " {init1, init2, init3, init4, init5}}\n"
27140 verifyFormat("SomeStruct s = {\n"
27148 " {init1, init2, init3, init4, init5}}\n"
27151 verifyFormat("SomeArrayT a[3] = {\n"
27163 verifyFormat("SomeArrayT a[3] = {\n"
27182 TEST_F(FormatTest
, UnderstandsDigraphs
) {
27183 verifyFormat("int arr<:5:> = {};");
27184 verifyFormat("int arr[5] = <%%>;");
27185 verifyFormat("int arr<:::qualified_variable:> = {};");
27186 verifyFormat("int arr[::qualified_variable] = <%%>;");
27187 verifyFormat("%:include <header>");
27188 verifyFormat("%:define A x##y");
27189 verifyFormat("#define A x%:%:y");
27192 TEST_F(FormatTest
, AlignArrayOfStructuresLeftAlignmentNonSquare
) {
27193 auto Style
= getLLVMStyle();
27194 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
27195 Style
.AlignConsecutiveAssignments
.Enabled
= true;
27196 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
27198 // The AlignArray code is incorrect for non square Arrays and can cause
27199 // crashes, these tests assert that the array is not changed but will
27200 // also act as regression tests for when it is properly fixed
27201 verifyFormat("struct test demo[] = {\n"
27207 verifyFormat("struct test demo[] = {\n"
27208 " {1, 2, 3, 4, 5},\n"
27213 verifyFormat("struct test demo[] = {\n"
27214 " {1, 2, 3, 4, 5},\n"
27216 " {6, 7, 8, 9, 10, 11, 12}\n"
27219 verifyFormat("struct test demo[] = {\n"
27222 " {6, 7, 8, 9, 10, 11, 12}\n"
27226 verifyFormat("S{\n"
27232 verifyFormat("S{\n"
27238 verifyFormat("void foo() {\n"
27239 " auto thing = test{\n"
27241 " {13}, {something}, // A\n"
27246 " auto thing = test{\n"
27249 " {something}, // A\n"
27256 TEST_F(FormatTest
, AlignArrayOfStructuresRightAlignmentNonSquare
) {
27257 auto Style
= getLLVMStyle();
27258 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
27259 Style
.AlignConsecutiveAssignments
.Enabled
= true;
27260 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
27262 // The AlignArray code is incorrect for non square Arrays and can cause
27263 // crashes, these tests assert that the array is not changed but will
27264 // also act as regression tests for when it is properly fixed
27265 verifyFormat("struct test demo[] = {\n"
27271 verifyFormat("struct test demo[] = {\n"
27272 " {1, 2, 3, 4, 5},\n"
27277 verifyFormat("struct test demo[] = {\n"
27278 " {1, 2, 3, 4, 5},\n"
27280 " {6, 7, 8, 9, 10, 11, 12}\n"
27283 verifyFormat("struct test demo[] = {\n"
27286 " {6, 7, 8, 9, 10, 11, 12}\n"
27290 verifyFormat("S{\n"
27296 verifyFormat("S{\n"
27302 verifyFormat("void foo() {\n"
27303 " auto thing = test{\n"
27305 " {13}, {something}, // A\n"
27310 " auto thing = test{\n"
27313 " {something}, // A\n"
27320 TEST_F(FormatTest
, FormatsVariableTemplates
) {
27321 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27322 verifyFormat("template <typename T> "
27323 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27326 TEST_F(FormatTest
, RemoveSemicolon
) {
27327 FormatStyle Style
= getLLVMStyle();
27328 Style
.RemoveSemicolon
= true;
27330 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27331 "int max(int a, int b) { return a > b ? a : b; };", Style
);
27333 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27334 "int max(int a, int b) { return a > b ? a : b; };;", Style
);
27336 verifyFormat("class Foo {\n"
27337 " int getSomething() const { return something; }\n"
27340 " int getSomething() const { return something; };\n"
27344 verifyFormat("class Foo {\n"
27345 " int getSomething() const { return something; }\n"
27348 " int getSomething() const { return something; };;\n"
27352 verifyFormat("for (;;) {\n"
27356 verifyFormat("class [[deprecated(\"\")]] C {\n"
27361 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27366 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style
);
27368 verifyFormat("void main() {}", "void main() {};", Style
);
27370 verifyFormat("struct Foo {\n"
27380 // We can't (and probably shouldn't) support the following.
27382 verifyFormat("void foo() {} //\n"
27384 "void foo() {}; //\n"
27389 Style
.TypenameMacros
.push_back("STRUCT");
27390 verifyFormat("STRUCT(T, B) { int i; };", Style
);
27393 TEST_F(FormatTest
, BreakAfterAttributes
) {
27394 constexpr StringRef
Code("[[maybe_unused]] const int i;\n"
27395 "[[foo([[]])]] [[maybe_unused]]\n"
27397 "[[maybe_unused]]\n"
27399 "[[nodiscard]] inline int f(int &i);\n"
27400 "[[foo([[]])]] [[nodiscard]]\n"
27403 "inline int f(int &i) {\n"
27407 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27412 FormatStyle Style
= getLLVMStyle();
27413 EXPECT_EQ(Style
.BreakAfterAttributes
, FormatStyle::ABS_Leave
);
27414 verifyNoChange(Code
, Style
);
27416 Style
.BreakAfterAttributes
= FormatStyle::ABS_Never
;
27417 verifyFormat("[[maybe_unused]] const int i;\n"
27418 "[[foo([[]])]] [[maybe_unused]] int j;\n"
27419 "[[maybe_unused]] foo<int> k;\n"
27420 "[[nodiscard]] inline int f(int &i);\n"
27421 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27422 "[[nodiscard]] inline int f(int &i) {\n"
27426 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27432 Style
.BreakAfterAttributes
= FormatStyle::ABS_Always
;
27433 verifyFormat("[[maybe_unused]]\n"
27435 "[[foo([[]])]] [[maybe_unused]]\n"
27437 "[[maybe_unused]]\n"
27440 "inline int f(int &i);\n"
27441 "[[foo([[]])]] [[nodiscard]]\n"
27444 "inline int f(int &i) {\n"
27448 "[[foo([[]])]] [[nodiscard]]\n"
27449 "int g(int &i) {\n"
27455 constexpr StringRef
CtrlStmtCode("[[likely]] if (a)\n"
27461 "[[unlikely]] case 1:\n"
27468 "[[unlikely]] for (; c > 0; --c)\n"
27474 Style
.BreakAfterAttributes
= FormatStyle::ABS_Leave
;
27475 verifyNoChange(CtrlStmtCode
, Style
);
27477 Style
.BreakAfterAttributes
= FormatStyle::ABS_Never
;
27478 verifyFormat("[[likely]] if (a)\n"
27482 "[[foo([[]])]] switch (b) {\n"
27483 "[[unlikely]] case 1:\n"
27486 "[[likely]] default:\n"
27489 "[[unlikely]] for (; c > 0; --c)\n"
27491 "[[likely]] while (d > 0)\n"
27493 CtrlStmtCode
, Style
);
27495 Style
.BreakAfterAttributes
= FormatStyle::ABS_Always
;
27496 verifyFormat("[[likely]]\n"
27512 "for (; c > 0; --c)\n"
27517 CtrlStmtCode
, Style
);
27519 constexpr StringRef
CtorDtorCode("struct Foo {\n"
27520 " [[deprecated]] Foo();\n"
27521 " [[deprecated]] Foo() {}\n"
27522 " [[deprecated]] ~Foo();\n"
27523 " [[deprecated]] ~Foo() {}\n"
27524 " [[deprecated]] void f();\n"
27525 " [[deprecated]] void f() {}\n"
27527 "[[deprecated]] Bar::Bar() {}\n"
27528 "[[deprecated]] Bar::~Bar() {}\n"
27529 "[[deprecated]] void g() {}");
27530 verifyFormat("struct Foo {\n"
27531 " [[deprecated]]\n"
27533 " [[deprecated]]\n"
27535 " [[deprecated]]\n"
27537 " [[deprecated]]\n"
27539 " [[deprecated]]\n"
27541 " [[deprecated]]\n"
27550 CtorDtorCode
, Style
);
27552 Style
.BreakBeforeBraces
= FormatStyle::BS_Linux
;
27553 verifyFormat("struct Foo {\n"
27554 " [[deprecated]]\n"
27556 " [[deprecated]]\n"
27560 " [[deprecated]]\n"
27562 " [[deprecated]]\n"
27566 " [[deprecated]]\n"
27568 " [[deprecated]]\n"
27585 CtorDtorCode
, Style
);
27587 verifyFormat("struct Foo {\n"
27588 " [[maybe_unused]]\n"
27589 " void operator+();\n"
27592 "Foo &operator-(Foo &);",
27595 Style
.ReferenceAlignment
= FormatStyle::ReferenceAlignmentStyle::RAS_Left
;
27596 verifyFormat("[[nodiscard]]\n"
27597 "Foo& operator-(Foo&);",
27601 TEST_F(FormatTest
, InsertNewlineAtEOF
) {
27602 FormatStyle Style
= getLLVMStyle();
27603 Style
.InsertNewlineAtEOF
= true;
27605 verifyNoChange("int i;\n", Style
);
27606 verifyFormat("int i;\n", "int i;", Style
);
27608 constexpr StringRef Code
{"namespace {\n"
27611 verifyFormat(Code
.str() + '\n', Code
, Style
,
27612 {tooling::Range(19, 13)}); // line 3
27615 TEST_F(FormatTest
, KeepEmptyLinesAtEOF
) {
27616 FormatStyle Style
= getLLVMStyle();
27617 Style
.KeepEmptyLines
.AtEndOfFile
= true;
27619 const StringRef Code
{"int i;\n\n"};
27620 verifyNoChange(Code
, Style
);
27621 verifyFormat(Code
, "int i;\n\n\n", Style
);
27624 TEST_F(FormatTest
, SpaceAfterUDL
) {
27625 verifyFormat("auto c = (4s).count();");
27626 verifyFormat("auto x = 5s .count() == 5;");
27629 TEST_F(FormatTest
, InterfaceAsClassMemberName
) {
27630 verifyFormat("class Foo {\n"
27631 " int interface;\n"
27632 " Foo::Foo(int iface) : interface{iface} {}\n"
27636 TEST_F(FormatTest
, PreprocessorOverlappingRegions
) {
27637 verifyFormat("#ifdef\n\n"
27648 TEST_F(FormatTest
, RemoveParentheses
) {
27649 FormatStyle Style
= getLLVMStyle();
27650 EXPECT_EQ(Style
.RemoveParentheses
, FormatStyle::RPS_Leave
);
27652 Style
.RemoveParentheses
= FormatStyle::RPS_MultipleParentheses
;
27653 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style
);
27654 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style
);
27655 verifyFormat("decltype((foo->bar)) baz;", Style
);
27656 verifyFormat("class __declspec(dllimport) X {};",
27657 "class __declspec((dllimport)) X {};", Style
);
27658 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style
);
27659 verifyFormat("while (a)\n"
27664 verifyFormat("while ((a = b))\n"
27666 "while (((a = b)))\n"
27669 verifyFormat("if (a)\n"
27674 verifyFormat("if constexpr ((a = b))\n"
27676 "if constexpr (((a = b)))\n"
27679 verifyFormat("if (({ a; }))\n"
27681 "if ((({ a; })))\n"
27684 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27685 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27687 verifyFormat("foo((a, b));", "foo(((a, b)));", Style
);
27688 verifyFormat("foo((a, b));", "foo(((a), b));", Style
);
27689 verifyFormat("foo((a, b));", "foo((a, (b)));", Style
);
27690 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style
);
27691 verifyFormat("return (0);", "return (((0)));", Style
);
27692 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style
);
27693 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27694 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27697 Style
.RemoveParentheses
= FormatStyle::RPS_ReturnStatement
;
27698 verifyFormat("#define Return0 return (0);", Style
);
27699 verifyFormat("return 0;", "return (0);", Style
);
27700 verifyFormat("co_return 0;", "co_return ((0));", Style
);
27701 verifyFormat("return 0;", "return (((0)));", Style
);
27702 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style
);
27703 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27704 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27706 verifyFormat("inline decltype(auto) f() {\n"
27712 "inline decltype(auto) f() {\n"
27719 verifyFormat("auto g() {\n"
27720 " decltype(auto) x = [] {\n"
27738 " decltype(auto) x = [] {\n"
27757 Style
.ColumnLimit
= 25;
27758 verifyFormat("return (a + b) - (c + d);",
27759 "return (((a + b)) -\n"
27764 TEST_F(FormatTest
, AllowBreakBeforeNoexceptSpecifier
) {
27765 auto Style
= getLLVMStyleWithColumns(35);
27767 EXPECT_EQ(Style
.AllowBreakBeforeNoexceptSpecifier
, FormatStyle::BBNSS_Never
);
27768 verifyFormat("void foo(int arg1,\n"
27769 " double arg2) noexcept;",
27772 // The following line does not fit within the 35 column limit, but that's what
27773 // happens with no break allowed.
27774 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27775 " noexcept(baz(arg1)) &&\n"
27776 " noexcept(baz(arg2)));",
27779 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27782 Style
.AllowBreakBeforeNoexceptSpecifier
= FormatStyle::BBNSS_Always
;
27783 verifyFormat("void foo(int arg1,\n"
27784 " double arg2) noexcept;",
27787 verifyFormat("void bar(int arg1, double arg2)\n"
27788 " noexcept(noexcept(baz(arg1)) &&\n"
27789 " noexcept(baz(arg2)));",
27792 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27796 Style
.AllowBreakBeforeNoexceptSpecifier
= FormatStyle::BBNSS_OnlyWithParen
;
27797 verifyFormat("void foo(int arg1,\n"
27798 " double arg2) noexcept;",
27801 verifyFormat("void bar(int arg1, double arg2)\n"
27802 " noexcept(noexcept(baz(arg1)) &&\n"
27803 " noexcept(baz(arg2)));",
27806 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27810 TEST_F(FormatTest
, PPBranchesInBracedInit
) {
27811 verifyFormat("A a_{kFlag1,\n"
27829 TEST_F(FormatTest
, PPDirectivesAndCommentsInBracedInit
) {
27832 " /* abc */ \"abc\",\n"
27834 " /* xyz */ \"xyz\",\n"
27836 " /* last */ \"last\"};\n"
27838 getLLVMStyleWithColumns(30));
27841 TEST_F(FormatTest
, BreakAdjacentStringLiterals
) {
27842 constexpr StringRef Code
{
27843 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27845 verifyFormat("return \"Code\"\n"
27846 " \"\\0\\52\\26\\55\\55\\0\"\n"
27851 auto Style
= getLLVMStyle();
27852 Style
.BreakAdjacentStringLiterals
= false;
27853 verifyFormat(Code
, Style
);
27856 TEST_F(FormatTest
, AlignUTFCommentsAndStringLiterals
) {
27858 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
27859 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
27860 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
27862 auto Style
= getLLVMStyle();
27863 Style
.ColumnLimit
= 15;
27864 verifyNoChange("#define test \\\n"
27870 Style
.ColumnLimit
= 25;
27871 verifyFormat("struct foo {\n"
27872 " int iiiiii; ///< iiiiii\n"
27873 " int b; ///< ыыы\n"
27874 " int c; ///< ыыыы\n"
27878 Style
.ColumnLimit
= 35;
27879 verifyFormat("#define SENSOR_DESC_1 \\\n"
27881 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
27885 Style
.ColumnLimit
= 80;
27886 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
27887 verifyFormat("Languages languages = {\n"
27888 " Language{{'e', 'n'}, U\"Test English\" },\n"
27889 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
27890 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
27895 TEST_F(FormatTest
, SpaceBetweenKeywordAndLiteral
) {
27896 verifyFormat("return .5;");
27897 verifyFormat("return not '5';");
27898 verifyFormat("return sizeof \"5\";");
27901 TEST_F(FormatTest
, BreakBinaryOperations
) {
27902 auto Style
= getLLVMStyleWithColumns(60);
27903 EXPECT_EQ(Style
.BreakBinaryOperations
, FormatStyle::BBO_Never
);
27905 // Logical operations
27906 verifyFormat("if (condition1 && condition2) {\n"
27910 verifyFormat("if (condition1 && condition2 &&\n"
27911 " (condition3 || condition4) && condition5 &&\n"
27916 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27917 " loooooooooooooooooooooongcondition2) {\n"
27922 verifyFormat("const int result = lhs + rhs;", Style
);
27924 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
27925 " loooooooooooooooooooooongop3;",
27928 verifyFormat("result = longOperand1 + longOperand2 -\n"
27929 " (longOperand3 + longOperand4) -\n"
27930 " longOperand5 * longOperand6;",
27933 verifyFormat("const int result =\n"
27934 " operand1 + operand2 - (operand3 + operand4);",
27937 Style
.BreakBinaryOperations
= FormatStyle::BBO_OnePerLine
;
27939 // Logical operations
27940 verifyFormat("if (condition1 && condition2) {\n"
27944 verifyFormat("if (condition1 && // comment\n"
27946 " (condition3 || condition4) && // comment\n"
27952 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27953 " loooooooooooooooooooooongcondition2) {\n"
27958 verifyFormat("const int result = lhs + rhs;", Style
);
27960 verifyFormat("result = loooooooooooooooooooooongop1 +\n"
27961 " loooooooooooooooooooooongop2 +\n"
27962 " loooooooooooooooooooooongop3;",
27965 verifyFormat("const int result =\n"
27966 " operand1 + operand2 - (operand3 + operand4);",
27969 verifyFormat("result = longOperand1 +\n"
27970 " longOperand2 -\n"
27971 " (longOperand3 + longOperand4) -\n"
27972 " longOperand5 +\n"
27976 verifyFormat("result = operand1 +\n"
27984 // Ensure mixed precedence operations are handled properly
27985 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
27987 verifyFormat("result = operand1 +\n"
27995 verifyFormat("result = operand1 *\n"
28003 verifyFormat("result = operand1 *\n"
28004 " (operand2 - operand3 * operand4) -\n"
28009 verifyFormat("result = operand1.member *\n"
28010 " (operand2.member() - operand3->mem * operand4) -\n"
28011 " operand5.member() +\n"
28012 " operand6->member;",
28015 Style
.BreakBinaryOperations
= FormatStyle::BBO_RespectPrecedence
;
28016 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28018 verifyFormat("result = operand1 +\n"
28019 " operand2 / operand3 +\n"
28020 " operand4 / operand5 * operand6;",
28023 verifyFormat("result = operand1 * operand2 -\n"
28024 " operand3 * operand4 -\n"
28029 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28034 verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28035 " byte_buffer[1] << 8 |\n"
28036 " byte_buffer[2] << 16 |\n"
28037 " byte_buffer[3] << 24;",
28040 Style
.BreakBinaryOperations
= FormatStyle::BBO_OnePerLine
;
28041 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
28043 // Logical operations
28044 verifyFormat("if (condition1 && condition2) {\n"
28048 verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28049 " && loooooooooooooooooooooongcondition2) {\n"
28054 verifyFormat("const int result = lhs + rhs;", Style
);
28056 verifyFormat("result = loooooooooooooooooooooongop1\n"
28057 " + loooooooooooooooooooooongop2\n"
28058 " + loooooooooooooooooooooongop3;",
28061 verifyFormat("const int result =\n"
28062 " operand1 + operand2 - (operand3 + operand4);",
28065 verifyFormat("result = longOperand1\n"
28066 " + longOperand2\n"
28067 " - (longOperand3 + longOperand4)\n"
28068 " - longOperand5\n"
28069 " + longOperand6;",
28072 verifyFormat("result = operand1\n"
28080 // Ensure mixed precedence operations are handled properly
28081 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28083 verifyFormat("result = operand1\n"
28091 verifyFormat("result = operand1\n"
28099 verifyFormat("result = operand1\n"
28100 " * (operand2 - operand3 * operand4)\n"
28105 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28106 " | byte_buffer[1]\n"
28108 " | byte_buffer[2]\n"
28110 " | byte_buffer[3]\n"
28114 Style
.BreakBinaryOperations
= FormatStyle::BBO_RespectPrecedence
;
28115 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28117 verifyFormat("result = operand1\n"
28118 " + operand2 / operand3\n"
28119 " + operand4 / operand5 * operand6;",
28122 verifyFormat("result = operand1 * operand2\n"
28123 " - operand3 * operand4\n"
28128 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
28133 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28134 " | byte_buffer[1] << 8\n"
28135 " | byte_buffer[2] << 16\n"
28136 " | byte_buffer[3] << 24;",
28140 TEST_F(FormatTest
, RemoveEmptyLinesInUnwrappedLines
) {
28141 auto Style
= getLLVMStyle();
28142 Style
.RemoveEmptyLinesInUnwrappedLines
= true;
28144 verifyFormat("int c = a + b;",
28150 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
28151 "enum : unsigned\n"
28159 verifyFormat("class B : public E {\n"
28162 "class B : public E\n"
28170 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
28171 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
28173 " 23, \"hello\"},\n"
28174 " {7, 5, \"!!\"}};",
28177 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
28178 "int myFunction(\n"
28180 " int aaaaaaaaaaaaa,\n"
28182 " int ccccccccccccc, int d);",
28185 verifyFormat("switch (e) {\n"
28201 verifyFormat("while (true) {\n"
28209 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
28210 " std::map<int, std::string> *outputMap);",
28211 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
28213 " (std::map<int, std::string> *outputMap);",
28217 TEST_F(FormatTest
, KeepFormFeed
) {
28218 auto Style
= getLLVMStyle();
28219 Style
.KeepFormFeed
= true;
28221 constexpr StringRef NoFormFeed
{"int i;\n"
28224 verifyFormat(NoFormFeed
,
28229 verifyFormat(NoFormFeed
,
28234 verifyFormat(NoFormFeed
,
28239 verifyFormat(NoFormFeed
,
28245 constexpr StringRef FormFeed
{"int i;\n"
28248 verifyNoChange(FormFeed
, Style
);
28250 Style
.LineEnding
= FormatStyle::LE_LF
;
28251 verifyFormat(FormFeed
,
28257 constexpr StringRef FormFeedBeforeEmptyLine
{"int i;\n"
28261 Style
.MaxEmptyLinesToKeep
= 2;
28262 verifyFormat(FormFeedBeforeEmptyLine
,
28268 verifyFormat(FormFeedBeforeEmptyLine
,
28277 } // namespace test
28278 } // namespace format
28279 } // namespace clang