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 // FIXME: The correct format is:
5732 "#define GEN_ID(_x) char *_x{#_x}\n"
5739 "#define GEN_ID(_x) \\\n"
5740 " char *_x { #_x }\n"
5747 TEST_F(FormatTest
, RespectWhitespaceInMacroDefinitions
) {
5748 verifyFormat("#define A (x)");
5749 verifyFormat("#define A(x)");
5751 FormatStyle Style
= getLLVMStyle();
5752 Style
.SpaceBeforeParens
= FormatStyle::SBPO_Never
;
5753 verifyFormat("#define true ((foo)1)", Style
);
5754 Style
.SpaceBeforeParens
= FormatStyle::SBPO_Always
;
5755 verifyFormat("#define false((foo)0)", Style
);
5758 TEST_F(FormatTest
, EmptyLinesInMacroDefinitions
) {
5759 verifyFormat("#define A b;",
5763 getLLVMStyleWithColumns(25));
5764 verifyNoChange("#define A \\\n"
5768 getLLVMStyleWithColumns(11));
5769 verifyNoChange("#define A \\\n"
5773 getLLVMStyleWithColumns(11));
5776 TEST_F(FormatTest
, MacroDefinitionsWithIncompleteCode
) {
5777 verifyIncompleteFormat("#define A :");
5778 verifyFormat("#define SOMECASES \\\n"
5781 getLLVMStyleWithColumns(20));
5782 verifyFormat("#define MACRO(a) \\\n"
5787 getLLVMStyleWithColumns(18));
5788 verifyFormat("#define A template <typename T>");
5789 verifyIncompleteFormat("#define STR(x) #x\n"
5790 "f(STR(this_is_a_string_literal{));");
5791 verifyFormat("#pragma omp threadprivate( \\\n"
5792 " y)), // expected-warning",
5793 getLLVMStyleWithColumns(28));
5794 verifyFormat("#d, = };");
5795 verifyFormat("#if \"a");
5796 verifyIncompleteFormat("({\n"
5801 getLLVMStyleWithColumns(15));
5802 verifyFormat("#define A \\\n"
5808 getLLVMStyleWithColumns(15));
5809 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5810 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5811 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5812 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5813 verifyNoCrash("#else\n"
5817 verifyNoCrash("#else\n"
5821 verifyNoCrash("#else\n"
5825 verifyNoCrash("#if X\n"
5830 verifyNoCrash("#if X\n"
5835 verifyNoCrash("#endif\n"
5837 verifyNoCrash("#endif\n"
5839 verifyNoCrash("#endif\n"
5843 TEST_F(FormatTest
, MacrosWithoutTrailingSemicolon
) {
5844 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5845 verifyFormat("class A : public QObject {\n"
5850 "class A : public QObject {\n"
5855 verifyFormat("MACRO\n"
5856 "/*static*/ int i;",
5858 " /*static*/ int i;");
5859 verifyFormat("SOME_MACRO\n"
5867 // Only if the identifier contains at least 5 characters.
5868 verifyFormat("HTTP f();", "HTTP\nf();");
5869 verifyNoChange("MACRO\nf();");
5870 // Only if everything is upper case.
5871 verifyFormat("class A : public QObject {\n"
5872 " Q_Object A() {}\n"
5874 "class A : public QObject {\n"
5879 // Only if the next line can actually start an unwrapped line.
5880 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5883 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5885 getChromiumStyle(FormatStyle::LK_Cpp
));
5888 verifyNoChange("/**/ FOO(a)\n"
5892 TEST_F(FormatTest
, MacroCallsWithoutTrailingSemicolon
) {
5893 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5894 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5895 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5897 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5898 "int *createScopDetectionPass() { return 0; }",
5899 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5900 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5901 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5903 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5904 " int *createScopDetectionPass() { return 0; }");
5905 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5906 // braces, so that inner block is indented one level more.
5907 verifyFormat("int q() {\n"
5908 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5909 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5910 " IPC_END_MESSAGE_MAP()\n"
5913 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5914 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5915 " IPC_END_MESSAGE_MAP()\n"
5918 // Same inside macros.
5919 verifyFormat("#define LIST(L) \\\n"
5923 "#define LIST(L) \\\n"
5929 // These must not be recognized as macros.
5930 verifyFormat("int q() {\n"
5948 " LOG(INFO) << x;\n"
5949 " ifstream(x) >> x;\n"
5969 " LOG(INFO)\n << x;\n"
5970 " ifstream(x)\n >> x;\n"
5972 verifyFormat("int q() {\n"
5984 " } catch (...) {\n"
5995 "try { Q(); } catch (...) {}\n"
5997 verifyFormat("class A {\n"
5999 " A(int i) noexcept() : {}\n"
6000 " A(X x)\n" // FIXME: function-level try blocks are broken.
6002 " } catch (...) {\n"
6006 " A()\n : t(0) {}\n"
6007 " A(int i)\n noexcept() : {}\n"
6009 " try : t(0) {} catch (...) {}\n"
6011 FormatStyle Style
= getLLVMStyle();
6012 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
6013 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
6014 Style
.BraceWrapping
.AfterFunction
= true;
6015 verifyFormat("void f()\n"
6022 verifyFormat("class SomeClass {\n"
6024 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6026 "class SomeClass {\n"
6029 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6031 verifyFormat("class SomeClass {\n"
6034 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6036 "class SomeClass {\n"
6039 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
6041 getLLVMStyleWithColumns(40));
6043 verifyFormat("MACRO(>)");
6045 // Some macros contain an implicit semicolon.
6046 Style
= getLLVMStyle();
6047 Style
.StatementMacros
.push_back("FOO");
6048 verifyFormat("FOO(a) int b = 0;");
6049 verifyFormat("FOO(a)\n"
6052 verifyFormat("FOO(a);\n"
6055 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
6058 verifyFormat("FOO()\n"
6061 verifyFormat("FOO\n"
6064 verifyFormat("void f() {\n"
6069 verifyFormat("FOO(a)\n"
6072 verifyFormat("int a = 0;\n"
6076 verifyFormat("int a = 0;\n"
6080 verifyFormat("void foo(int a) { FOO(a) }\n"
6081 "uint32_t bar() {}",
6085 TEST_F(FormatTest
, FormatsMacrosWithZeroColumnWidth
) {
6086 FormatStyle ZeroColumn
= getLLVMStyleWithColumns(0);
6088 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
6092 TEST_F(FormatTest
, LayoutMacroDefinitionsStatementsSpanningBlocks
) {
6093 verifyFormat("#define A \\\n"
6097 getLLVMStyleWithColumns(11));
6100 TEST_F(FormatTest
, IndentPreprocessorDirectives
) {
6101 FormatStyle Style
= getLLVMStyleWithColumns(40);
6102 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
6103 verifyFormat("#ifdef _WIN32\n"
6107 "#include <someheader.h>\n"
6108 "#define MACRO \\\n"
6109 " some_very_long_func_aaaaaaaaaa();\n"
6115 Style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
6116 verifyFormat("#if 1\n"
6117 "# define __STR(x) #x\n"
6120 verifyFormat("#ifdef _WIN32\n"
6124 "# include <someheader.h>\n"
6125 "# define MACRO \\\n"
6126 " some_very_long_func_aaaaaaaaaa();\n"
6132 verifyFormat("#if A\n"
6133 "# define MACRO \\\n"
6134 " void a(int x) { \\\n"
6143 // Comments before include guard.
6144 verifyFormat("// file comment\n"
6146 "#ifndef HEADER_H\n"
6147 "#define HEADER_H\n"
6151 // Test with include guards.
6152 verifyFormat("#ifndef HEADER_H\n"
6153 "#define HEADER_H\n"
6157 // Include guards must have a #define with the same variable immediately
6159 verifyFormat("#ifndef NOT_GUARD\n"
6165 // Include guards must cover the entire file.
6166 verifyFormat("code();\n"
6168 "#ifndef NOT_GUARD\n"
6169 "# define NOT_GUARD\n"
6173 verifyFormat("#ifndef NOT_GUARD\n"
6174 "# define NOT_GUARD\n"
6179 // Test with trailing blank lines.
6180 verifyFormat("#ifndef HEADER_H\n"
6181 "#define HEADER_H\n"
6185 // Include guards don't have #else.
6186 verifyFormat("#ifndef NOT_GUARD\n"
6187 "# define NOT_GUARD\n"
6192 verifyFormat("#ifndef NOT_GUARD\n"
6193 "# define NOT_GUARD\n"
6198 // Non-identifier #define after potential include guard.
6199 verifyFormat("#ifndef FOO\n"
6203 // #if closes past last non-preprocessor line.
6204 verifyFormat("#ifndef FOO\n"
6212 // Don't crash if there is an #elif directive without a condition.
6213 verifyFormat("#if 1\n"
6221 // FIXME: This doesn't handle the case where there's code between the
6222 // #ifndef and #define but all other conditions hold. This is because when
6223 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6224 // previous code line yet, so we can't detect it.
6225 verifyFormat("#ifndef NOT_GUARD\n"
6227 "#define NOT_GUARD\n"
6230 "#ifndef NOT_GUARD\n"
6232 "# define NOT_GUARD\n"
6236 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6237 // be outside an include guard. Examples are #pragma once and
6238 // #pragma GCC diagnostic, or anything else that does not change the meaning
6239 // of the file if it's included multiple times.
6240 verifyFormat("#ifdef WIN32\n"
6243 "#ifndef HEADER_H\n"
6244 "# define HEADER_H\n"
6250 "#ifndef HEADER_H\n"
6251 "#define HEADER_H\n"
6255 // FIXME: This does not detect when there is a single non-preprocessor line
6256 // in front of an include-guard-like structure where other conditions hold
6257 // because ScopedLineState hides the line.
6258 verifyFormat("code();\n"
6259 "#ifndef HEADER_H\n"
6260 "#define HEADER_H\n"
6264 "#ifndef HEADER_H\n"
6265 "# define HEADER_H\n"
6269 // Keep comments aligned with #, otherwise indent comments normally. These
6270 // tests cannot use verifyFormat because messUp manipulates leading
6273 const char *Expected
= ""
6276 "// Preprocessor aligned.\n"
6278 " // Code. Separated by blank line.\n"
6281 " // Code. Not aligned with #\n"
6284 const char *ToFormat
= ""
6287 "// Preprocessor aligned.\n"
6289 "// Code. Separated by blank line.\n"
6292 " // Code. Not aligned with #\n"
6295 verifyFormat(Expected
, ToFormat
, Style
);
6296 verifyNoChange(Expected
, Style
);
6298 // Keep block quotes aligned.
6300 const char *Expected
= ""
6303 "/* Preprocessor aligned. */\n"
6305 " /* Code. Separated by blank line. */\n"
6308 " /* Code. Not aligned with # */\n"
6311 const char *ToFormat
= ""
6314 "/* Preprocessor aligned. */\n"
6316 "/* Code. Separated by blank line. */\n"
6319 " /* Code. Not aligned with # */\n"
6322 verifyFormat(Expected
, ToFormat
, Style
);
6323 verifyNoChange(Expected
, Style
);
6325 // Keep comments aligned with un-indented directives.
6327 const char *Expected
= ""
6329 "// Preprocessor aligned.\n"
6331 " // Code. Separated by blank line.\n"
6334 " // Code. Not aligned with #\n"
6336 const char *ToFormat
= ""
6338 "// Preprocessor aligned.\n"
6340 "// Code. Separated by blank line.\n"
6343 " // Code. Not aligned with #\n"
6345 verifyFormat(Expected
, ToFormat
, Style
);
6346 verifyNoChange(Expected
, Style
);
6348 // Test AfterHash with tabs.
6350 FormatStyle Tabbed
= Style
;
6351 Tabbed
.UseTab
= FormatStyle::UT_Always
;
6352 Tabbed
.IndentWidth
= 8;
6353 Tabbed
.TabWidth
= 8;
6354 verifyFormat("#ifdef _WIN32\n"
6358 "#\t\tinclude <someheader.h>\n"
6359 "#\t\tdefine MACRO \\\n"
6360 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6368 // Regression test: Multiline-macro inside include guards.
6369 verifyFormat("#ifndef HEADER_H\n"
6370 "#define HEADER_H\n"
6374 "#endif // HEADER_H",
6375 getLLVMStyleWithColumns(20));
6377 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
6378 // Basic before hash indent tests
6379 verifyFormat("#ifdef _WIN32\n"
6383 " #include <someheader.h>\n"
6384 " #define MACRO \\\n"
6385 " some_very_long_func_aaaaaaaaaa();\n"
6391 verifyFormat("#if A\n"
6392 " #define MACRO \\\n"
6393 " void a(int x) { \\\n"
6402 // Keep comments aligned with indented directives. These
6403 // tests cannot use verifyFormat because messUp manipulates leading
6406 const char *Expected
= "void f() {\n"
6407 "// Aligned to preprocessor.\n"
6409 " // Aligned to code.\n"
6412 " // Aligned to preprocessor.\n"
6414 " // Aligned to code.\n"
6419 const char *ToFormat
= "void f() {\n"
6420 "// Aligned to preprocessor.\n"
6422 "// Aligned to code.\n"
6425 "// Aligned to preprocessor.\n"
6427 "// Aligned to code.\n"
6432 verifyFormat(Expected
, ToFormat
, Style
);
6433 verifyNoChange(Expected
, Style
);
6436 const char *Expected
= "void f() {\n"
6437 "/* Aligned to preprocessor. */\n"
6439 " /* Aligned to code. */\n"
6442 " /* Aligned to preprocessor. */\n"
6444 " /* Aligned to code. */\n"
6449 const char *ToFormat
= "void f() {\n"
6450 "/* Aligned to preprocessor. */\n"
6452 "/* Aligned to code. */\n"
6455 "/* Aligned to preprocessor. */\n"
6457 "/* Aligned to code. */\n"
6462 verifyFormat(Expected
, ToFormat
, Style
);
6463 verifyNoChange(Expected
, Style
);
6466 // Test single comment before preprocessor
6467 verifyFormat("// Comment\n"
6474 TEST_F(FormatTest
, FormatAlignInsidePreprocessorElseBlock
) {
6475 FormatStyle Style
= getLLVMStyle();
6476 Style
.AlignConsecutiveAssignments
.Enabled
= true;
6477 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
6479 // Test with just #if blocks.
6480 verifyFormat("void f1() {\n"
6483 " int foobar = 2;\n"
6491 " char *foobarbaz = \"foobarbaz\";\n"
6496 // Test with just #else blocks.
6497 verifyFormat("void f1() {\n"
6501 " int foobar = 2;\n"
6511 " char *foobarbaz = \"foobarbaz\";\n"
6515 verifyFormat("auto foo = [] { return; };\n"
6523 // Test with a mix of #if and #else blocks.
6524 verifyFormat("void f1() {\n"
6528 " int foobar = 2;\n"
6537 " // prevent alignment with #else in f1\n"
6538 " char *foobarbaz = \"foobarbaz\";\n"
6543 // Test with nested #if and #else blocks.
6544 verifyFormat("void f1() {\n"
6550 " int foobar = 2;\n"
6564 " // prevent alignment with #else in f1\n"
6565 " char *foobarbaz = \"foobarbaz\";\n"
6572 verifyFormat("#if FOO\n"
6584 verifyFormat("void f() {\n"
6595 " bool abcd = true;\n"
6601 verifyFormat("void f() {\n"
6618 TEST_F(FormatTest
, FormatHashIfNotAtStartOfLine
) {
6626 TEST_F(FormatTest
, FormatUnbalancedStructuralElements
) {
6627 verifyFormat("#define A \\\n { \\\n {\nint i;",
6628 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6629 verifyFormat("#define A \\\n } \\\n }\nint i;",
6630 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6633 TEST_F(FormatTest
, EscapedNewlines
) {
6634 FormatStyle Narrow
= getLLVMStyleWithColumns(11);
6635 verifyFormat("#define A \\\n int i; \\\n int j;",
6636 "#define A \\\nint i;\\\n int j;", Narrow
);
6637 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6638 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6639 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6640 verifyNoChange("<a\n\\\\\n>");
6642 FormatStyle AlignLeft
= getLLVMStyle();
6643 AlignLeft
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
6644 verifyFormat("#define MACRO(x) \\\n"
6649 // CRLF line endings
6650 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6651 "#define A \\\r\nint i;\\\r\n int j;", Narrow
);
6652 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6653 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6654 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6655 verifyNoChange("<a\r\n\\\\\r\n>");
6656 verifyFormat("#define MACRO(x) \\\r\n"
6661 constexpr StringRef Code
{"#define A \\\n"
6665 verifyFormat(Code
, AlignLeft
);
6667 constexpr StringRef Code2
{"#define A \\\n"
6671 auto LastLine
= getLLVMStyle();
6672 LastLine
.AlignEscapedNewlines
= FormatStyle::ENAS_LeftWithLastLine
;
6673 verifyFormat(Code2
, LastLine
);
6675 LastLine
.ColumnLimit
= 13;
6676 verifyFormat(Code
, LastLine
);
6678 LastLine
.ColumnLimit
= 0;
6679 verifyFormat(Code2
, LastLine
);
6681 FormatStyle DontAlign
= getLLVMStyle();
6682 DontAlign
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
6683 DontAlign
.MaxEmptyLinesToKeep
= 3;
6684 // FIXME: can't use verifyFormat here because the newline before
6685 // "public:" is not inserted the first time it's reformatted
6686 verifyNoChange("#define A \\\n"
6698 TEST_F(FormatTest
, CalculateSpaceOnConsecutiveLinesInMacro
) {
6699 verifyFormat("#define A \\\n"
6703 getLLVMStyleWithColumns(11));
6706 TEST_F(FormatTest
, MixingPreprocessorDirectivesAndNormalCode
) {
6707 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6709 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6711 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6712 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6713 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6715 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6717 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6718 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6721 TEST_F(FormatTest
, LayoutStatementsAroundPreprocessorDirectives
) {
6722 verifyFormat("int\n"
6725 "int\n#define A\na;");
6726 verifyFormat("functionCallTo(\n"
6727 " someOtherFunction(\n"
6728 " withSomeParameters, whichInSequence,\n"
6729 " areLongerThanALine(andAnotherCall,\n"
6731 " withMoreParamters,\n"
6732 " whichStronglyInfluenceTheLayout),\n"
6733 " andMoreParameters),\n"
6735 getLLVMStyleWithColumns(69));
6736 verifyFormat("Foo::Foo()\n"
6742 verifyFormat("void f() {\n"
6752 verifyFormat("void f(param1, param2,\n"
6773 getLLVMStyleWithColumns(28));
6774 verifyFormat("#if 1\n"
6776 verifyFormat("#if 1\n"
6781 verifyFormat("DEBUG({\n"
6782 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6789 verifyIncompleteFormat("void f(\n"
6795 // Verify that indentation is correct when there is an `#if 0` with an
6797 verifyFormat("#if 0\n"
6805 verifyFormat("#if 0\n"
6808 "int something_fairly_long; // Align here please\n"
6809 "#endif // Should be aligned");
6811 verifyFormat("#if 0\n"
6818 verifyFormat("void SomeFunction(int param1,\n"
6833 TEST_F(FormatTest
, GraciouslyHandleIncorrectPreprocessorConditions
) {
6834 verifyFormat("#endif\n"
6838 TEST_F(FormatTest
, FormatsJoinedLinesOnSubsequentRuns
) {
6839 FormatStyle SingleLine
= getLLVMStyle();
6840 SingleLine
.AllowShortIfStatementsOnASingleLine
= FormatStyle::SIS_WithoutElse
;
6841 verifyFormat("#if 0\n"
6845 " if (test) foo2();\n"
6850 TEST_F(FormatTest
, LayoutBlockInsideParens
) {
6851 verifyFormat("functionCall({ int i; });");
6852 verifyFormat("functionCall({\n"
6856 verifyFormat("functionCall(\n"
6861 " aaaa, bbbb, cccc);");
6862 verifyFormat("functionA(functionB({\n"
6866 " aaaa, bbbb, cccc);");
6867 verifyFormat("functionCall(\n"
6872 " aaaa, bbbb, // comment\n"
6874 verifyFormat("functionA(functionB({\n"
6878 " aaaa, bbbb, // comment\n"
6880 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6881 verifyFormat("functionCall(aaaa, bbbb, {\n"
6886 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6888 " int i; // break\n"
6890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6891 " ccccccccccccccccc));");
6892 verifyFormat("DEBUG({\n"
6898 TEST_F(FormatTest
, LayoutBlockInsideStatement
) {
6899 verifyFormat("SOME_MACRO { int i; }\n"
6901 " SOME_MACRO {int i;} int i;");
6904 TEST_F(FormatTest
, LayoutNestedBlocks
) {
6905 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6909 " s kBitsToOs[] = {{10}};\n"
6910 " for (int i = 0; i < 10; ++i)\n"
6913 verifyFormat("call(parameter, {\n"
6915 " // Comment using all columns.\n"
6916 " somethingelse();\n"
6918 getLLVMStyleWithColumns(40));
6919 verifyFormat("DEBUG( //\n"
6921 verifyFormat("DEBUG( //\n"
6927 verifyFormat("call(parameter, {\n"
6930 " // looooooooooong.\n"
6931 " somethingElse();\n"
6933 "call(parameter, {\n"
6935 " // Comment too looooooooooong.\n"
6936 " somethingElse();\n"
6938 getLLVMStyleWithColumns(29));
6939 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6940 verifyFormat("DEBUG({ // comment\n"
6943 "DEBUG({ // comment\n"
6946 verifyFormat("DEBUG({\n"
6959 verifyFormat("DEBUG({\n"
6963 verifyGoogleFormat("DEBUG({\n"
6966 FormatStyle Style
= getGoogleStyle();
6967 Style
.ColumnLimit
= 45;
6968 verifyFormat("Debug(\n"
6971 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6976 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6978 verifyNoCrash("^{v^{a}}");
6981 TEST_F(FormatTest
, FormatNestedBlocksInMacros
) {
6982 verifyFormat("#define MACRO() \\\n"
6983 " Debug(aaa, /* force line break */ \\\n"
6988 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6989 " { int i; int j; })",
6992 verifyFormat("#define A \\\n"
6994 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6995 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6997 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6998 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
7002 TEST_F(FormatTest
, PutEmptyBlocksIntoOneLine
) {
7003 verifyFormat("enum E {};");
7004 verifyFormat("enum E {}");
7005 FormatStyle Style
= getLLVMStyle();
7006 Style
.SpaceInEmptyBlock
= true;
7007 verifyFormat("void f() { }", "void f() {}", Style
);
7008 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Empty
;
7009 verifyFormat("{ }", Style
);
7010 verifyFormat("while (true) { }", "while (true) {}", Style
);
7011 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
7012 Style
.BraceWrapping
.BeforeElse
= false;
7013 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
7014 verifyFormat("if (a)\n"
7021 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Never
;
7022 verifyFormat("if (a) {\n"
7027 Style
.BraceWrapping
.BeforeElse
= true;
7028 verifyFormat("if (a) { }\n"
7033 Style
= getLLVMStyle(FormatStyle::LK_CSharp
);
7034 Style
.SpaceInEmptyBlock
= true;
7035 verifyFormat("Event += () => { };", Style
);
7038 TEST_F(FormatTest
, FormatBeginBlockEndMacros
) {
7039 FormatStyle Style
= getLLVMStyle();
7040 Style
.MacroBlockBegin
= "^[A-Z_]+_BEGIN$";
7041 Style
.MacroBlockEnd
= "^[A-Z_]+_END$";
7042 verifyFormat("FOO_BEGIN\n"
7046 verifyFormat("FOO_BEGIN\n"
7047 " NESTED_FOO_BEGIN\n"
7048 " NESTED_FOO_ENTRY\n"
7052 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
7058 Style
.RemoveBracesLLVM
= true;
7059 verifyNoCrash("for (;;)\n"
7066 //===----------------------------------------------------------------------===//
7067 // Line break tests.
7068 //===----------------------------------------------------------------------===//
7070 TEST_F(FormatTest
, PreventConfusingIndents
) {
7073 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
7074 " parameter, parameter, parameter)),\n"
7075 " SecondLongCall(parameter));\n"
7078 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7079 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7081 " aaaaaaaaaaaaaaaaaaaaaaaa);");
7083 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7084 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
7085 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
7086 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
7088 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
7089 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
7090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
7091 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
7092 verifyFormat("int a = bbbb && ccc &&\n"
7094 "#define A Just forcing a new line\n"
7098 TEST_F(FormatTest
, LineBreakingInBinaryExpressions
) {
7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
7105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
7108 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
7110 " ccccccccc == ddddddddddd;");
7111 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
7112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
7113 " ccccccccc == ddddddddddd;");
7115 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
7116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
7117 " ccccccccc == ddddddddddd;");
7119 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7121 " bbbbbb && cccccc;");
7122 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
7125 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
7126 " SourceMgr.getSpellingColumnNumber(\n"
7127 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
7130 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7131 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
7133 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7134 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7136 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7137 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
7139 verifyFormat("b = a &&\n"
7143 // If the LHS of a comparison is not a binary expression itself, the
7144 // additional linebreak confuses many people.
7146 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
7150 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7154 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
7155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7158 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
7161 // Even explicit parentheses stress the precedence enough to make the
7162 // additional break unnecessary.
7163 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
7166 // This cases is borderline, but with the indentation it is still readable.
7168 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7169 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
7172 getLLVMStyleWithColumns(75));
7174 // If the LHS is a binary expression, we should still use the additional break
7175 // as otherwise the formatting hides the operator precedence.
7176 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
7185 FormatStyle OnePerLine
= getLLVMStyle();
7186 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7188 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7189 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
7193 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
7194 " .aaa(aaaaaaaaaaaaa) *\n"
7197 getLLVMStyleWithColumns(40));
7200 TEST_F(FormatTest
, ExpressionIndentation
) {
7201 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7205 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7206 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7209 " ccccccccccccccccccccccccccccccccccccccccc;");
7210 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7213 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7214 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7221 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7222 verifyFormat("if () {\n"
7223 "} else if (aaaaa && bbbbb > // break\n"
7226 verifyFormat("if () {\n"
7227 "} else if constexpr (aaaaa && bbbbb > // break\n"
7230 verifyFormat("if () {\n"
7231 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7234 verifyFormat("if () {\n"
7235 "} else if (aaaaa &&\n"
7236 " bbbbb > // break\n"
7241 // Presence of a trailing comment used to change indentation of b.
7242 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7244 "return aaaaaaaaaaaaaaaaaaa +\n"
7246 getLLVMStyleWithColumns(30));
7249 TEST_F(FormatTest
, ExpressionIndentationBreakingBeforeOperators
) {
7250 // Not sure what the best system is here. Like this, the LHS can be found
7251 // immediately above an operator (everything with the same or a higher
7252 // indent). The RHS is aligned right of the operator and so compasses
7253 // everything until something with the same indent as the operator is found.
7254 // FIXME: Is this a good system?
7255 FormatStyle Style
= getLLVMStyle();
7256 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7258 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7259 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7260 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7261 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7262 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7263 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7264 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7265 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7266 " > ccccccccccccccccccccccccccccccccccccccccc;",
7268 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7269 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7270 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7271 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7273 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7274 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7275 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7276 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7278 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7279 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7281 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7283 verifyFormat("if () {\n"
7284 "} else if (aaaaa\n"
7285 " && bbbbb // break\n"
7289 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7290 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7292 verifyFormat("return (a)\n"
7297 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7298 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7303 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7306 // Forced by comments.
7308 "unsigned ContentSize =\n"
7309 " sizeof(int16_t) // DWARF ARange version number\n"
7310 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7311 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7312 " + sizeof(int8_t); // Segment Size (in bytes)");
7314 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7315 " == boost::fusion::at_c<1>(iiii).second;",
7318 Style
.ColumnLimit
= 60;
7319 verifyFormat("zzzzzzzzzz\n"
7320 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7321 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7324 Style
.ColumnLimit
= 80;
7325 Style
.IndentWidth
= 4;
7327 Style
.UseTab
= FormatStyle::UT_Always
;
7328 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7329 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
7330 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7331 "\t&& (someOtherLongishConditionPart1\n"
7332 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7333 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7334 "(someOtherLongishConditionPart1 || "
7335 "someOtherEvenLongerNestedConditionPart2);",
7338 Style
= getLLVMStyleWithColumns(20);
7339 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
7340 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7341 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7342 Style
.ContinuationIndentWidth
= 2;
7343 verifyFormat("struct Foo {\n"
7352 verifyFormat("return abc\n"
7362 TEST_F(FormatTest
, ExpressionIndentationStrictAlign
) {
7363 FormatStyle Style
= getLLVMStyle();
7364 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7365 Style
.AlignOperands
= FormatStyle::OAS_AlignAfterOperator
;
7367 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7368 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7369 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7370 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7372 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7373 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7374 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7375 " > ccccccccccccccccccccccccccccccccccccccccc;",
7377 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7378 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7379 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7380 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7382 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7383 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7384 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7385 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7387 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7388 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7389 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7390 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7392 verifyFormat("if () {\n"
7393 "} else if (aaaaa\n"
7394 " && bbbbb // break\n"
7398 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7399 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7401 verifyFormat("return (a)\n"
7406 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7407 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7410 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7411 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7412 " : 3333333333333333;",
7415 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7416 " : ccccccccccccccc ? dddddddddddddddddd\n"
7417 " : eeeeeeeeeeeeeeeeee)\n"
7418 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7419 " : 3333333333333333;",
7421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7422 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7425 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7426 " == boost::fusion::at_c<1>(iiii).second;",
7429 Style
.ColumnLimit
= 60;
7430 verifyFormat("zzzzzzzzzzzzz\n"
7431 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7432 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7435 // Forced by comments.
7436 Style
.ColumnLimit
= 80;
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)",
7445 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7447 "unsigned ContentSize =\n"
7448 " sizeof(int16_t) // DWARF ARange version number\n"
7449 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7450 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7451 " + sizeof(int8_t); // Segment Size (in bytes)",
7454 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
7456 "unsigned ContentSize =\n"
7457 " sizeof(int16_t) // DWARF ARange version number\n"
7458 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7459 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7460 " + sizeof(int8_t); // Segment Size (in bytes)",
7464 TEST_F(FormatTest
, EnforcedOperatorWraps
) {
7465 // Here we'd like to wrap after the || operators, but a comment is forcing an
7467 verifyFormat("bool x = aaaaa //\n"
7473 TEST_F(FormatTest
, NoOperandAlignment
) {
7474 FormatStyle Style
= getLLVMStyle();
7475 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
7476 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7480 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7481 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7482 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7483 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7484 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7485 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7486 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7487 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7488 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7489 " > ccccccccccccccccccccccccccccccccccccccccc;",
7492 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7493 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7496 verifyFormat("int a = aa\n"
7497 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7498 " * cccccccccccccccccccccccccccccccccccc;",
7501 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7502 verifyFormat("return (a > b\n"
7509 TEST_F(FormatTest
, BreakingBeforeNonAssigmentOperators
) {
7510 FormatStyle Style
= getLLVMStyle();
7511 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7512 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7514 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7518 TEST_F(FormatTest
, AllowBinPackingInsideArguments
) {
7519 FormatStyle Style
= getLLVMStyleWithColumns(40);
7520 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7521 Style
.BinPackArguments
= false;
7522 verifyFormat("void test() {\n"
7524 " this + argument + is + quite\n"
7525 " + long + so + it + gets + wrapped\n"
7526 " + but + remains + bin - packed);\n"
7529 verifyFormat("void test() {\n"
7530 " someFunction(arg1,\n"
7531 " this + argument + is\n"
7532 " + quite + long + so\n"
7533 " + it + gets + wrapped\n"
7534 " + but + remains + bin\n"
7539 verifyFormat("void test() {\n"
7542 " this + argument + has\n"
7543 " + anotherFunc(nested,\n"
7549 " + to + being + bin - packed,\n"
7554 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
7555 verifyFormat("void test() {\n"
7558 " this + argument + has +\n"
7559 " anotherFunc(nested,\n"
7560 " calls + whose +\n"
7564 " in + addition) +\n"
7565 " to + being + bin - packed,\n"
7571 TEST_F(FormatTest
, BreakBinaryOperatorsInPresenceOfTemplates
) {
7572 auto Style
= getLLVMStyleWithColumns(45);
7573 EXPECT_EQ(Style
.BreakBeforeBinaryOperators
, FormatStyle::BOS_None
);
7574 verifyFormat("bool b =\n"
7575 " is_default_constructible_v<hash<T>> and\n"
7576 " is_copy_constructible_v<hash<T>> and\n"
7577 " is_move_constructible_v<hash<T>> and\n"
7578 " is_copy_assignable_v<hash<T>> and\n"
7579 " is_move_assignable_v<hash<T>> and\n"
7580 " is_destructible_v<hash<T>> and\n"
7581 " is_swappable_v<hash<T>> and\n"
7582 " is_callable_v<hash<T>(T)>;",
7585 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7586 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7587 " and is_copy_constructible_v<hash<T>>\n"
7588 " and is_move_constructible_v<hash<T>>\n"
7589 " and is_copy_assignable_v<hash<T>>\n"
7590 " and is_move_assignable_v<hash<T>>\n"
7591 " and is_destructible_v<hash<T>>\n"
7592 " and is_swappable_v<hash<T>>\n"
7593 " and is_callable_v<hash<T>(T)>;",
7596 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7597 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7598 " and is_copy_constructible_v<hash<T>>\n"
7599 " and is_move_constructible_v<hash<T>>\n"
7600 " and is_copy_assignable_v<hash<T>>\n"
7601 " and is_move_assignable_v<hash<T>>\n"
7602 " and is_destructible_v<hash<T>>\n"
7603 " and is_swappable_v<hash<T>>\n"
7604 " and is_callable_v<hash<T>(T)>;",
7608 TEST_F(FormatTest
, ConstructorInitializers
) {
7609 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7610 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7611 getLLVMStyleWithColumns(45));
7612 verifyFormat("Constructor()\n"
7613 " : Inttializer(FitsOnTheLine) {}",
7614 getLLVMStyleWithColumns(44));
7615 verifyFormat("Constructor()\n"
7616 " : Inttializer(FitsOnTheLine) {}",
7617 getLLVMStyleWithColumns(43));
7619 verifyFormat("template <typename T>\n"
7620 "Constructor() : Initializer(FitsOnTheLine) {}",
7621 getLLVMStyleWithColumns(45));
7624 "SomeClass::Constructor()\n"
7625 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7628 "SomeClass::Constructor()\n"
7629 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7630 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7632 "SomeClass::Constructor()\n"
7633 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7634 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7635 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7636 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7637 " : aaaaaaaaaa(aaaaaa) {}");
7639 verifyFormat("Constructor()\n"
7640 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7641 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7642 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7643 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7645 verifyFormat("Constructor()\n"
7646 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7649 verifyFormat("Constructor(int Parameter = 0)\n"
7650 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7651 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7652 verifyFormat("Constructor()\n"
7653 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7655 getLLVMStyleWithColumns(60));
7656 verifyFormat("Constructor()\n"
7657 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7658 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7660 // Here a line could be saved by splitting the second initializer onto two
7661 // lines, but that is not desirable.
7662 verifyFormat("Constructor()\n"
7663 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7664 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7665 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7667 FormatStyle OnePerLine
= getLLVMStyle();
7668 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
7669 verifyFormat("MyClass::MyClass()\n"
7674 verifyFormat("MyClass::MyClass()\n"
7675 " : a(a), // comment\n"
7679 verifyFormat("MyClass::MyClass(int a)\n"
7680 " : b(a), // comment\n"
7681 " c(a + 1) { // lined up\n"
7684 verifyFormat("Constructor()\n"
7687 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7688 OnePerLine
.AllowAllParametersOfDeclarationOnNextLine
= false;
7689 verifyFormat("SomeClass::Constructor()\n"
7690 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7691 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7692 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7694 verifyFormat("SomeClass::Constructor()\n"
7695 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7696 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7697 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7699 verifyFormat("MyClass::MyClass(int var)\n"
7700 " : some_var_(var), // 4 space indent\n"
7701 " some_other_var_(var + 1) { // lined up\n"
7704 verifyFormat("Constructor()\n"
7705 " : aaaaa(aaaaaa),\n"
7709 " aaaaa(aaaaaa) {}",
7711 verifyFormat("Constructor()\n"
7712 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7713 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7715 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7718 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7719 " aaaaaaaaaaa().aaa(),\n"
7720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7722 OnePerLine
.ColumnLimit
= 60;
7723 verifyFormat("Constructor()\n"
7724 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7725 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7728 verifyFormat("Constructor()\n"
7729 " : // Comment forcing unwanted break.\n"
7732 " // Comment forcing unwanted break.\n"
7736 TEST_F(FormatTest
, AllowAllConstructorInitializersOnNextLine
) {
7737 FormatStyle Style
= getLLVMStyleWithColumns(60);
7738 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7739 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7741 for (int i
= 0; i
< 4; ++i
) {
7742 // Test all combinations of parameters that should not have an effect.
7743 Style
.AllowAllParametersOfDeclarationOnNextLine
= i
& 1;
7744 Style
.AllowAllArgumentsOnNextLine
= i
& 2;
7746 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7747 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7748 verifyFormat("Constructor()\n"
7749 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7751 verifyFormat("Constructor() : a(a), b(b) {}", Style
);
7753 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7754 verifyFormat("Constructor()\n"
7755 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7756 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7758 verifyFormat("Constructor() : a(a), b(b) {}", Style
);
7760 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7761 verifyFormat("Constructor()\n"
7762 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7764 verifyFormat("Constructor()\n"
7767 verifyFormat("Constructor()\n"
7768 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7769 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7770 " , cccccccccccccccccccccc(c) {}",
7773 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
7774 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7775 verifyFormat("Constructor()\n"
7776 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7779 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7780 verifyFormat("Constructor()\n"
7781 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7782 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7785 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7786 verifyFormat("Constructor()\n"
7787 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7789 verifyFormat("Constructor()\n"
7792 verifyFormat("Constructor()\n"
7793 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7794 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7795 " cccccccccccccccccccccc(c) {}",
7798 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7799 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7800 verifyFormat("Constructor() :\n"
7801 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7804 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7805 verifyFormat("Constructor() :\n"
7806 " aaaaaaaaaaaaaaaaaa(a),\n"
7807 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7810 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7811 verifyFormat("Constructor() :\n"
7812 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7814 verifyFormat("Constructor() :\n"
7817 verifyFormat("Constructor() :\n"
7818 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7819 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7820 " cccccccccccccccccccccc(c) {}",
7824 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7825 // AllowAllConstructorInitializersOnNextLine in all
7826 // BreakConstructorInitializers modes
7827 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7828 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7829 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7830 verifyFormat("SomeClassWithALongName::Constructor(\n"
7831 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7832 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7833 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7836 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7837 verifyFormat("SomeClassWithALongName::Constructor(\n"
7838 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7839 " int bbbbbbbbbbbbb,\n"
7840 " int cccccccccccccccc)\n"
7841 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7844 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7845 verifyFormat("SomeClassWithALongName::Constructor(\n"
7846 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7847 " int bbbbbbbbbbbbb,\n"
7848 " int cccccccccccccccc)\n"
7849 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7852 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7853 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7854 verifyFormat("SomeClassWithALongName::Constructor(\n"
7855 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7856 " int bbbbbbbbbbbbb)\n"
7857 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7858 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7861 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
7863 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7864 verifyFormat("SomeClassWithALongName::Constructor(\n"
7865 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7866 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7867 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7870 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7871 verifyFormat("SomeClassWithALongName::Constructor(\n"
7872 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7873 " int bbbbbbbbbbbbb,\n"
7874 " int cccccccccccccccc)\n"
7875 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7878 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7879 verifyFormat("SomeClassWithALongName::Constructor(\n"
7880 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7881 " int bbbbbbbbbbbbb,\n"
7882 " int cccccccccccccccc)\n"
7883 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7886 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7887 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7888 verifyFormat("SomeClassWithALongName::Constructor(\n"
7889 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7890 " int bbbbbbbbbbbbb)\n"
7891 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7892 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7895 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7896 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7897 verifyFormat("SomeClassWithALongName::Constructor(\n"
7898 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7899 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7900 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7903 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7904 verifyFormat("SomeClassWithALongName::Constructor(\n"
7905 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7906 " int bbbbbbbbbbbbb,\n"
7907 " int cccccccccccccccc) :\n"
7908 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7911 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7912 verifyFormat("SomeClassWithALongName::Constructor(\n"
7913 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7914 " int bbbbbbbbbbbbb,\n"
7915 " int cccccccccccccccc) :\n"
7916 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7919 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7920 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7921 verifyFormat("SomeClassWithALongName::Constructor(\n"
7922 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7923 " int bbbbbbbbbbbbb) :\n"
7924 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7925 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7928 Style
= getLLVMStyleWithColumns(0);
7929 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7930 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style
);
7931 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7932 " : bar(bar), baz(baz) {}",
7936 TEST_F(FormatTest
, AllowAllArgumentsOnNextLine
) {
7937 FormatStyle Style
= getLLVMStyleWithColumns(60);
7938 Style
.BinPackArguments
= false;
7939 for (int i
= 0; i
< 4; ++i
) {
7940 // Test all combinations of parameters that should not have an effect.
7941 Style
.AllowAllParametersOfDeclarationOnNextLine
= i
& 1;
7942 Style
.PackConstructorInitializers
=
7943 i
& 2 ? FormatStyle::PCIS_BinPack
: FormatStyle::PCIS_Never
;
7945 Style
.AllowAllArgumentsOnNextLine
= true;
7946 verifyFormat("void foo() {\n"
7947 " FunctionCallWithReallyLongName(\n"
7948 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7951 Style
.AllowAllArgumentsOnNextLine
= false;
7952 verifyFormat("void foo() {\n"
7953 " FunctionCallWithReallyLongName(\n"
7954 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7959 Style
.AllowAllArgumentsOnNextLine
= true;
7960 verifyFormat("void foo() {\n"
7961 " auto VariableWithReallyLongName = {\n"
7962 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7965 Style
.AllowAllArgumentsOnNextLine
= false;
7966 verifyFormat("void foo() {\n"
7967 " auto VariableWithReallyLongName = {\n"
7968 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7974 // This parameter should not affect declarations.
7975 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
7976 Style
.AllowAllArgumentsOnNextLine
= false;
7977 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7978 verifyFormat("void FunctionCallWithReallyLongName(\n"
7979 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7981 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7982 verifyFormat("void FunctionCallWithReallyLongName(\n"
7983 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7984 " int bbbbbbbbbbbb);",
7988 TEST_F(FormatTest
, AllowAllArgumentsOnNextLineDontAlign
) {
7989 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7991 FormatStyle Style
= getLLVMStyleWithColumns(35);
7992 StringRef Input
= "functionCall(paramA, paramB, paramC);\n"
7993 "void functionDecl(int A, int B, int C);";
7994 Style
.AllowAllArgumentsOnNextLine
= false;
7995 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7996 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7998 "void functionDecl(int A, int B,\n"
8001 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
8002 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8004 "void functionDecl(int A, int B,\n"
8007 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
8008 // AllowAllArgumentsOnNextLine.
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 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
8016 verifyFormat("functionCall(\n"
8017 " paramA, paramB, paramC\n"
8019 "void functionDecl(\n"
8020 " int A, int B, int C\n"
8024 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
8026 Style
.AllowAllArgumentsOnNextLine
= true;
8027 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
8028 verifyFormat(StringRef("functionCall(\n"
8029 " paramA, paramB, paramC);\n"
8030 "void functionDecl(\n"
8031 " int A, int B, int C);"),
8033 // It wouldn't fit on one line with aligned parameters so this setting
8034 // doesn't change anything for BAS_Align.
8035 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
8036 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
8038 "void functionDecl(int A, int B,\n"
8041 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
8042 verifyFormat(StringRef("functionCall(\n"
8043 " paramA, paramB, paramC);\n"
8044 "void functionDecl(\n"
8045 " int A, int B, int C);"),
8049 TEST_F(FormatTest
, BreakFunctionDefinitionParameters
) {
8050 StringRef Input
= "void functionDecl(paramA, paramB, paramC);\n"
8051 "void emptyFunctionDefinition() {}\n"
8052 "void functionDefinition(int A, int B, int C) {}\n"
8053 "Class::Class(int A, int B) : m_A(A), m_B(B) {}";
8054 verifyFormat(Input
);
8056 FormatStyle Style
= getLLVMStyle();
8057 EXPECT_FALSE(Style
.BreakFunctionDefinitionParameters
);
8058 Style
.BreakFunctionDefinitionParameters
= true;
8059 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8060 "void emptyFunctionDefinition() {}\n"
8061 "void functionDefinition(\n"
8062 " int A, int B, int C) {}\n"
8065 " : m_A(A), m_B(B) {}",
8068 // Test the style where all parameters are on their own lines.
8069 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
8070 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8071 verifyFormat("void functionDecl(paramA, paramB, paramC);\n"
8072 "void emptyFunctionDefinition() {}\n"
8073 "void functionDefinition(\n"
8080 " : m_A(A), m_B(B) {}",
8084 TEST_F(FormatTest
, BreakBeforeInlineASMColon
) {
8085 FormatStyle Style
= getLLVMStyle();
8086 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Never
;
8087 /* Test the behaviour with long lines */
8088 Style
.ColumnLimit
= 40;
8089 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8092 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8095 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8096 " \"cpuid\\n\\t\"\n"
8097 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
8098 " : \"=a\" : \"a\");",
8100 Style
.ColumnLimit
= 80;
8101 verifyFormat("asm volatile(\"string\", : : val);", Style
);
8102 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style
);
8104 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Always
;
8105 verifyFormat("asm volatile(\"string\",\n"
8109 verifyFormat("asm volatile(\"string\",\n"
8113 /* Test the behaviour with long lines */
8114 Style
.ColumnLimit
= 40;
8115 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8116 " \"cpuid\\n\\t\"\n"
8117 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8118 " : \"=a\"(*rEAX)\n"
8119 " : \"a\"(value));",
8121 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
8122 " \"cpuid\\n\\t\"\n"
8123 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
8125 " : \"a\"(value));",
8127 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8131 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
8137 TEST_F(FormatTest
, BreakConstructorInitializersAfterColon
) {
8138 FormatStyle Style
= getLLVMStyle();
8139 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
8141 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
8142 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
8143 getStyleWithColumns(Style
, 45));
8144 verifyFormat("Constructor() :\n"
8145 " Initializer(FitsOnTheLine) {}",
8146 getStyleWithColumns(Style
, 44));
8147 verifyFormat("Constructor() :\n"
8148 " Initializer(FitsOnTheLine) {}",
8149 getStyleWithColumns(Style
, 43));
8151 verifyFormat("template <typename T>\n"
8152 "Constructor() : Initializer(FitsOnTheLine) {}",
8153 getStyleWithColumns(Style
, 50));
8155 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
8156 " int mooooooooooooore) noexcept :\n"
8157 " Super{some, arguments}, Member{5}, Member2{2} {}",
8159 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
8161 "SomeClass::Constructor() :\n"
8162 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8165 "SomeClass::Constructor() : // NOLINT\n"
8166 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8169 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
8171 "SomeClass::Constructor() :\n"
8172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8175 "SomeClass::Constructor() : // NOLINT\n"
8176 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8179 Style
.PackConstructorInitializers
= FormatStyle::PCIS_BinPack
;
8181 "SomeClass::Constructor() :\n"
8182 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8186 "SomeClass::Constructor() :\n"
8187 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8191 "SomeClass::Constructor() :\n"
8192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8193 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
8196 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8197 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
8200 verifyFormat("Constructor() :\n"
8201 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8202 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8203 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8204 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
8207 verifyFormat("Constructor() :\n"
8208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8212 verifyFormat("Constructor(int Parameter = 0) :\n"
8213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
8214 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
8216 verifyFormat("Constructor() :\n"
8217 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
8219 getStyleWithColumns(Style
, 60));
8220 verifyFormat("Constructor() :\n"
8221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8222 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
8225 // Here a line could be saved by splitting the second initializer onto two
8226 // lines, but that is not desirable.
8227 verifyFormat("Constructor() :\n"
8228 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
8229 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
8230 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8233 FormatStyle OnePerLine
= Style
;
8234 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
8235 verifyFormat("SomeClass::Constructor() :\n"
8236 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8237 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8238 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8240 verifyFormat("SomeClass::Constructor() :\n"
8241 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8242 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8243 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8245 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8246 " i(i), // comment\n"
8249 verifyFormat("MyClass::MyClass(int var) :\n"
8250 " some_var_(var), // 4 space indent\n"
8251 " some_other_var_(var + 1) { // lined up\n"
8254 verifyFormat("Constructor() :\n"
8259 " aaaaa(aaaaaa) {}",
8261 verifyFormat("Constructor() :\n"
8262 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8263 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8265 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8266 verifyFormat("Constructor() :\n"
8267 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8268 " aaaaaaaaaaa().aaa(),\n"
8269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8271 OnePerLine
.ColumnLimit
= 60;
8272 verifyFormat("Constructor() :\n"
8273 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8274 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8277 verifyFormat("Constructor() :\n"
8278 " // Comment forcing unwanted break.\n"
8281 verifyFormat("Constructor() : // NOLINT\n"
8284 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8288 "Constructor() : // A very long trailing comment that cannot fit"
8289 " on a single line.\n"
8293 Style
.ColumnLimit
= 0;
8294 verifyFormat("SomeClass::Constructor() :\n"
8297 verifyFormat("SomeClass::Constructor() noexcept :\n"
8300 verifyFormat("SomeClass::Constructor() :\n"
8301 " a(a), b(b), c(c) {}",
8303 verifyFormat("SomeClass::Constructor() :\n"
8310 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
8311 verifyFormat("SomeClass::Constructor() :\n"
8312 " a(a), b(b), c(c) {\n"
8315 verifyFormat("SomeClass::Constructor() :\n"
8320 Style
.ColumnLimit
= 80;
8321 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
8322 Style
.ConstructorInitializerIndentWidth
= 2;
8323 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style
);
8324 verifyFormat("SomeClass::Constructor() :\n"
8325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8326 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8329 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8331 Style
.BreakInheritanceList
= FormatStyle::BILS_BeforeColon
;
8334 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8335 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8337 Style
.BreakInheritanceList
= FormatStyle::BILS_BeforeComma
;
8340 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8341 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8343 Style
.BreakInheritanceList
= FormatStyle::BILS_AfterColon
;
8345 "class SomeClass :\n"
8346 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8347 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8349 Style
.BreakInheritanceList
= FormatStyle::BILS_AfterComma
;
8352 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8353 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8357 #ifndef EXPENSIVE_CHECKS
8358 // Expensive checks enables libstdc++ checking which includes validating the
8359 // state of ranges used in std::priority_queue - this blows out the
8360 // runtime/scalability of the function and makes this test unacceptably slow.
8361 TEST_F(FormatTest
, MemoizationTests
) {
8362 // This breaks if the memoization lookup does not take \c Indent and
8363 // \c LastSpace into account.
8365 "extern CFRunLoopTimerRef\n"
8366 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8367 " CFTimeInterval interval, CFOptionFlags flags,\n"
8368 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8369 " CFRunLoopTimerContext *context) {}");
8371 // Deep nesting somewhat works around our memoization.
8373 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8374 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8375 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8376 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8377 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8378 getLLVMStyleWithColumns(65));
8404 " aaaaa))))))))))));",
8405 getLLVMStyleWithColumns(65));
8407 "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"
8425 getLLVMStyleWithColumns(65));
8427 // This test takes VERY long when memoization is broken.
8428 FormatStyle OnePerLine
= getLLVMStyle();
8429 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
8430 OnePerLine
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8431 std::string input
= "Constructor()\n"
8433 for (unsigned i
= 0, e
= 80; i
!= e
; ++i
)
8436 verifyFormat(input
, OnePerLine
);
8437 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
8438 verifyFormat(input
, OnePerLine
);
8442 TEST_F(FormatTest
, BreaksAsHighAsPossible
) {
8445 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8446 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8449 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8450 " Intervals[i - 1].getRange().getLast()) {\n}");
8453 TEST_F(FormatTest
, BreaksFunctionDeclarations
) {
8454 // Principially, we break function declarations in a certain order:
8455 // 1) break amongst arguments.
8456 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8457 " Cccccccccccccc cccccccccccccc);");
8458 verifyFormat("template <class TemplateIt>\n"
8459 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8460 " TemplateIt *stop) {}");
8462 // 2) break after return type.
8464 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8465 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8467 // 3) break after (.
8469 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8470 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8472 // 4) break before after nested name specifiers.
8474 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8475 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8476 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8478 // However, there are exceptions, if a sufficient amount of lines can be
8480 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8482 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8483 " Cccccccccccccc cccccccccc,\n"
8484 " Cccccccccccccc cccccccccc,\n"
8485 " Cccccccccccccc cccccccccc,\n"
8486 " Cccccccccccccc cccccccccc);");
8488 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8489 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8490 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8491 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8493 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8494 " Cccccccccccccc cccccccccc,\n"
8495 " Cccccccccccccc cccccccccc,\n"
8496 " Cccccccccccccc cccccccccc,\n"
8497 " Cccccccccccccc cccccccccc,\n"
8498 " Cccccccccccccc cccccccccc,\n"
8499 " Cccccccccccccc cccccccccc);");
8500 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8501 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8502 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8503 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8504 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8506 // Break after multi-line parameters.
8507 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8511 verifyFormat("void SomeLoooooooooooongFunction(\n"
8512 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8513 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8514 " int bbbbbbbbbbbbb);");
8516 // Treat overloaded operators like other functions.
8517 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8518 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8519 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8520 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8521 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8522 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8524 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8525 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8527 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8528 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8529 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8530 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8531 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8532 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8534 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8535 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8536 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8537 verifyGoogleFormat("template <typename T>\n"
8538 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8539 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8540 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8542 FormatStyle Style
= getLLVMStyle();
8543 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
8544 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8545 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8547 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8552 TEST_F(FormatTest
, DontBreakBeforeQualifiedOperator
) {
8553 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8554 // Prefer keeping `::` followed by `operator` together.
8555 verifyFormat("const aaaa::bbbbbbb &\n"
8556 "ccccccccc::operator++() {\n"
8559 "const aaaa::bbbbbbb\n"
8560 "&ccccccccc::operator++() { stuff(); }",
8561 getLLVMStyleWithColumns(40));
8564 TEST_F(FormatTest
, TrailingReturnType
) {
8565 verifyFormat("auto foo() -> int;");
8566 // correct trailing return type spacing
8567 verifyFormat("auto operator->() -> int;");
8568 verifyFormat("auto operator++(int) -> int;");
8570 verifyFormat("struct S {\n"
8571 " auto bar() const -> int;\n"
8573 verifyFormat("template <size_t Order, typename T>\n"
8574 "auto load_img(const std::string &filename)\n"
8575 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8576 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8577 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8578 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8579 verifyFormat("template <typename T>\n"
8580 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8581 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8583 FormatStyle Style
= getLLVMStyleWithColumns(60);
8584 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8585 " auto NAME() -> int { return 42; }",
8588 // Not trailing return types.
8589 verifyFormat("void f() { auto a = b->c(); }");
8590 verifyFormat("auto a = p->foo();");
8591 verifyFormat("int a = p->foo();");
8592 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8595 TEST_F(FormatTest
, DeductionGuides
) {
8596 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8597 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8598 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8600 "template <class... T>\n"
8601 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8602 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8603 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8604 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8605 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8606 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8607 verifyFormat("template <class T> x() -> x<1>;");
8608 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8610 verifyFormat("A(const char *) -> A<string &>;");
8611 verifyFormat("A() -> A<int>;");
8613 // Ensure not deduction guides.
8614 verifyFormat("c()->f<int>();");
8615 verifyFormat("x()->foo<1>;");
8616 verifyFormat("x = p->foo<3>();");
8617 verifyFormat("x()->x<1>();");
8620 TEST_F(FormatTest
, BreaksFunctionDeclarationsWithTrailingTokens
) {
8621 // Avoid breaking before trailing 'const' or other trailing annotations, if
8622 // they are not function-like.
8623 FormatStyle Style
= getGoogleStyleWithColumns(47);
8624 verifyFormat("void someLongFunction(\n"
8625 " int someLoooooooooooooongParameter) const {\n}",
8626 getLLVMStyleWithColumns(47));
8627 verifyFormat("LoooooongReturnType\n"
8628 "someLoooooooongFunction() const {}",
8629 getLLVMStyleWithColumns(47));
8630 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8633 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8634 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8635 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8636 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8637 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8638 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8639 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8640 " aaaaaaaaaaa aaaaa) const override;");
8642 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8643 " const override;");
8645 // Even if the first parameter has to be wrapped.
8646 verifyFormat("void someLongFunction(\n"
8647 " int someLongParameter) const {}",
8648 getLLVMStyleWithColumns(46));
8649 verifyFormat("void someLongFunction(\n"
8650 " int someLongParameter) const {}",
8652 verifyFormat("void someLongFunction(\n"
8653 " int someLongParameter) override {}",
8655 verifyFormat("void someLongFunction(\n"
8656 " int someLongParameter) OVERRIDE {}",
8658 verifyFormat("void someLongFunction(\n"
8659 " int someLongParameter) final {}",
8661 verifyFormat("void someLongFunction(\n"
8662 " int someLongParameter) FINAL {}",
8664 verifyFormat("void someLongFunction(\n"
8665 " int parameter) const override {}",
8668 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
8669 verifyFormat("void someLongFunction(\n"
8670 " int someLongParameter) const\n"
8675 Style
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
8676 verifyFormat("void someLongFunction(\n"
8677 " int someLongParameter) const\n"
8682 // Unless these are unknown annotations.
8683 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8684 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8685 " LONG_AND_UGLY_ANNOTATION;");
8687 // Breaking before function-like trailing annotations is fine to keep them
8688 // close to their arguments.
8689 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8690 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8691 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8692 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8693 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8694 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8695 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8696 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8697 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8700 "void aaaaaaaaaaaaaaaaaa()\n"
8701 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8702 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8703 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8704 " __attribute__((unused));");
8706 Style
= getGoogleStyle();
8709 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8710 " GUARDED_BY(aaaaaaaaaaaa);",
8713 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8714 " GUARDED_BY(aaaaaaaaaaaa);",
8717 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8718 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8721 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8722 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8726 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8727 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8730 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8731 " ABSL_GUARDED_BY(aaaaaaaaaaaa);",
8734 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8735 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
8738 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n"
8739 " aaaaaaaaaaaaaaaaaaaaaaaaa;",
8743 TEST_F(FormatTest
, FunctionAnnotations
) {
8744 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8745 "int OldFunction(const string ¶meter) {}");
8746 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8747 "string OldFunction(const string ¶meter) {}");
8748 verifyFormat("template <typename T>\n"
8749 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8750 "string OldFunction(const string ¶meter) {}");
8752 // Not function annotations.
8753 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8754 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8755 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8756 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8757 verifyFormat("MACRO(abc).function() // wrap\n"
8759 verifyFormat("MACRO(abc)->function() // wrap\n"
8761 verifyFormat("MACRO(abc)::function() // wrap\n"
8763 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0));
8766 TEST_F(FormatTest
, BreaksDesireably
) {
8767 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8768 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8769 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8775 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8778 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8783 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8784 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8786 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8790 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8798 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8801 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8802 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8805 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8806 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8808 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8812 // Indent consistently independent of call expression and unary operator.
8813 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8814 " dddddddddddddddddddddddddddddd));");
8815 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8816 " dddddddddddddddddddddddddddddd));");
8817 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8818 " dddddddddddddddddddddddddddddd));");
8820 // This test case breaks on an incorrect memoization, i.e. an optimization not
8821 // taking into account the StopAt value.
8823 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8824 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8825 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8826 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8828 verifyFormat("{\n {\n {\n"
8829 " Annotation.SpaceRequiredBefore =\n"
8830 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8831 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8834 // Break on an outer level if there was a break on an inner level.
8835 verifyFormat("f(g(h(a, // comment\n"
8839 "f(g(h(a, // comment\n"
8840 " b, c), d, e), x, y);");
8842 // Prefer breaking similar line breaks.
8844 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8845 " NSTrackingMouseEnteredAndExited |\n"
8846 " NSTrackingActiveAlways;");
8849 TEST_F(FormatTest
, FormatsDeclarationsOnePerLine
) {
8850 FormatStyle NoBinPacking
= getGoogleStyle();
8851 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8852 NoBinPacking
.BinPackArguments
= true;
8853 verifyFormat("void f() {\n"
8854 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8858 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8859 " int aaaaaaaaaaaaaaaaaaaa,\n"
8860 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8863 NoBinPacking
.AllowAllParametersOfDeclarationOnNextLine
= false;
8864 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8865 " vector<int> bbbbbbbbbbbbbbb);",
8867 // FIXME: This behavior difference is probably not wanted. However, currently
8868 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8869 // template arguments from BreakBeforeParameter being set because of the
8870 // one-per-line formatting.
8872 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8873 " aaaaaaaaaa> aaaaaaaaaa);",
8876 "void fffffffffff(\n"
8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8881 TEST_F(FormatTest
, FormatsOneParameterPerLineIfNecessary
) {
8882 FormatStyle NoBinPacking
= getGoogleStyle();
8883 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
8884 NoBinPacking
.BinPackArguments
= false;
8885 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8886 " aaaaaaaaaaaaaaaaaaaa,\n"
8887 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8889 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8891 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8894 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8895 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8897 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8900 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8901 " .aaaaaaaaaaaaaaaaaa();",
8903 verifyFormat("void f() {\n"
8904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8905 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8910 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8915 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8916 " ddddddddddddddddddddddddddddd),\n"
8920 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8921 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8922 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8923 " aaaaaaaaaaaaaaaaaa;",
8925 verifyFormat("a(\"a\"\n"
8929 NoBinPacking
.AllowAllParametersOfDeclarationOnNextLine
= false;
8930 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8936 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8941 "template <class SomeType, class SomeOtherType>\n"
8942 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8946 TEST_F(FormatTest
, FormatsDeclarationBreakAlways
) {
8947 FormatStyle BreakAlways
= getGoogleStyle();
8948 BreakAlways
.BinPackParameters
= FormatStyle::BPPS_AlwaysOnePerLine
;
8949 verifyFormat("void f(int a,\n"
8952 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8953 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8954 " int cccccccccccccccccccccccc);",
8957 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
8958 // to BPPS_AlwaysOnePerLine.
8959 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
8961 "void someLongFunctionName(\n"
8962 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8965 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
8967 "void someLongFunctionName(\n"
8968 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8974 TEST_F(FormatTest
, FormatsDefinitionBreakAlways
) {
8975 FormatStyle BreakAlways
= getGoogleStyle();
8976 BreakAlways
.BinPackParameters
= FormatStyle::BPPS_AlwaysOnePerLine
;
8977 verifyFormat("void f(int a,\n"
8983 // Ensure BinPackArguments interact correctly when BinPackParameters is set to
8984 // BPPS_AlwaysOnePerLine.
8985 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8986 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8987 " int cccccccccccccccccccccccc) {\n"
8988 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8989 " cccccccccccccccccccccccc);\n"
8992 BreakAlways
.BinPackArguments
= false;
8993 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8994 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8995 " int cccccccccccccccccccccccc) {\n"
8996 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8997 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n"
8998 " cccccccccccccccccccccccc);\n"
9002 // Ensure BreakFunctionDefinitionParameters interacts correctly when
9003 // BinPackParameters is set to BPPS_AlwaysOnePerLine.
9004 BreakAlways
.BreakFunctionDefinitionParameters
= true;
9005 verifyFormat("void f(\n"
9011 BreakAlways
.BreakFunctionDefinitionParameters
= false;
9013 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
9014 // to BPPS_AlwaysOnePerLine.
9015 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
9017 "void someLongFunctionName(\n"
9018 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9020 " someLongFunctionName(\n"
9021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
9024 BreakAlways
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
9026 "void someLongFunctionName(\n"
9027 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9030 " someLongFunctionName(\n"
9031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n"
9037 TEST_F(FormatTest
, AdaptiveOnePerLineFormatting
) {
9038 FormatStyle Style
= getLLVMStyleWithColumns(15);
9039 Style
.ExperimentalAutoDetectBinPacking
= true;
9040 verifyFormat("aaa(aaaa,\n"
9046 "aaa(aaaa,\n" // one-per-line
9049 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9051 verifyFormat("aaa(aaaa, aaaa,\n"
9055 "aaa(aaaa, aaaa,\n" // bin-packed
9057 "aaa(aaaa, aaaa, aaaa);", // inconclusive
9061 TEST_F(FormatTest
, FormatsBuilderPattern
) {
9062 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
9063 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
9064 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
9065 " .StartsWith(\".init\", ORDER_INIT)\n"
9066 " .StartsWith(\".fini\", ORDER_FINI)\n"
9067 " .StartsWith(\".hash\", ORDER_HASH)\n"
9068 " .Default(ORDER_TEXT);");
9070 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
9071 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
9072 verifyFormat("aaaaaaa->aaaaaaa\n"
9073 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9075 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9077 "aaaaaaa->aaaaaaa\n"
9078 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9079 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
9081 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
9082 " aaaaaaaaaaaaaa);");
9084 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
9085 " aaaaaa->aaaaaaaaaaaa()\n"
9086 " ->aaaaaaaaaaaaaaaa(\n"
9087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9088 " ->aaaaaaaaaaaaaaaaa();");
9091 " someo->Add((new util::filetools::Handler(dir))\n"
9092 " ->OnEvent1(NewPermanentCallback(\n"
9093 " this, &HandlerHolderClass::EventHandlerCBA))\n"
9094 " ->OnEvent2(NewPermanentCallback(\n"
9095 " this, &HandlerHolderClass::EventHandlerCBB))\n"
9096 " ->OnEvent3(NewPermanentCallback(\n"
9097 " this, &HandlerHolderClass::EventHandlerCBC))\n"
9098 " ->OnEvent5(NewPermanentCallback(\n"
9099 " this, &HandlerHolderClass::EventHandlerCBD))\n"
9100 " ->OnEvent6(NewPermanentCallback(\n"
9101 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
9105 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
9106 verifyFormat("aaaaaaaaaaaaaaa()\n"
9107 " .aaaaaaaaaaaaaaa()\n"
9108 " .aaaaaaaaaaaaaaa()\n"
9109 " .aaaaaaaaaaaaaaa()\n"
9110 " .aaaaaaaaaaaaaaa();");
9111 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9112 " .aaaaaaaaaaaaaaa()\n"
9113 " .aaaaaaaaaaaaaaa()\n"
9114 " .aaaaaaaaaaaaaaa();");
9115 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9116 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
9117 " .aaaaaaaaaaaaaaa();");
9118 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
9119 " ->aaaaaaaaaaaaaae(0)\n"
9120 " ->aaaaaaaaaaaaaaa();");
9122 // Don't linewrap after very short segments.
9123 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9124 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9125 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9126 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9127 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9128 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9129 verifyFormat("aaa()\n"
9130 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9131 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9132 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
9134 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9135 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
9136 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
9137 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
9138 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
9139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
9141 // Prefer not to break after empty parentheses.
9142 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
9143 " First->LastNewlineOffset);");
9145 // Prefer not to create "hanging" indents.
9147 "return !soooooooooooooome_map\n"
9148 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9151 "return aaaaaaaaaaaaaaaa\n"
9152 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
9153 " .aaaa(aaaaaaaaaaaaaa);");
9154 // No hanging indent here.
9155 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
9156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9157 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
9158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9159 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9160 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9161 getLLVMStyleWithColumns(60));
9162 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
9163 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
9164 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9165 getLLVMStyleWithColumns(59));
9166 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9168 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9170 // Dont break if only closing statements before member call
9171 verifyFormat("test() {\n"
9177 verifyFormat("test() {\n"
9186 verifyFormat("test() {\n"
9194 verifyFormat("test() {\n"
9199 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
9202 getLLVMStyleWithColumns(30));
9205 TEST_F(FormatTest
, BreaksAccordingToOperatorPrecedence
) {
9207 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
9208 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
9210 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
9211 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
9213 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9214 " ccccccccccccccccccccccccc) {\n}");
9215 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
9216 " ccccccccccccccccccccccccc) {\n}");
9218 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
9219 " ccccccccccccccccccccccccc) {\n}");
9220 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
9221 " ccccccccccccccccccccccccc) {\n}");
9224 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
9225 " ccccccccccccccccccccccccc) {\n}");
9227 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
9228 " ccccccccccccccccccccccccc) {\n}");
9230 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
9231 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
9232 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
9233 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9234 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
9235 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
9236 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
9237 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
9239 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
9240 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
9241 " aaaaaaaaaaaaaaa != aa) {\n}");
9242 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
9243 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
9244 " aaaaaaaaaaaaaaa != aa) {\n}");
9247 TEST_F(FormatTest
, BreaksAfterAssignments
) {
9250 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
9251 " SI->getPointerAddressSpaceee());");
9253 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
9254 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
9257 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
9258 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
9259 verifyFormat("unsigned OriginalStartColumn =\n"
9260 " SourceMgr.getSpellingColumnNumber(\n"
9261 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
9265 TEST_F(FormatTest
, ConfigurableBreakAssignmentPenalty
) {
9266 FormatStyle Style
= getLLVMStyle();
9267 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9268 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
9271 Style
.PenaltyBreakAssignment
= 20;
9272 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
9273 " cccccccccccccccccccccccccc;",
9277 TEST_F(FormatTest
, AlignsAfterAssignments
) {
9279 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9280 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9282 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9283 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9285 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9286 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9288 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9289 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9291 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9292 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
9293 " aaaaaaaaaaaaaaaaaaaaaaaa;");
9296 TEST_F(FormatTest
, AlignsAfterReturn
) {
9298 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9299 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
9301 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9302 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
9304 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9305 " aaaaaaaaaaaaaaaaaaaaaa();");
9307 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
9308 " aaaaaaaaaaaaaaaaaaaaaa());");
9309 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9311 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
9313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9314 verifyFormat("return\n"
9315 " // true if code is one of a or b.\n"
9316 " code == a || code == b;");
9319 TEST_F(FormatTest
, AlignsAfterOpenBracket
) {
9321 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9322 " aaaaaaaaa aaaaaaa) {}");
9324 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9325 " aaaaaaaaaaa aaaaaaaaa);");
9327 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9328 " aaaaaaaaaaaaaaaaaaaaa));");
9329 FormatStyle Style
= getLLVMStyle();
9330 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9331 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9332 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
9334 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9335 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
9337 verifyFormat("SomeLongVariableName->someFunction(\n"
9338 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
9341 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
9342 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9345 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
9346 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9349 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
9350 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9353 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9354 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9358 Style
.ColumnLimit
= 30;
9359 verifyFormat("for (int foo = 0; foo < FOO;\n"
9364 Style
.ColumnLimit
= 80;
9366 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
9367 Style
.BinPackArguments
= false;
9368 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
9369 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9370 " aaaaaaaaaaa aaaaaaaa,\n"
9371 " aaaaaaaaa aaaaaaa,\n"
9372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9374 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9375 " aaaaaaaaaaa aaaaaaaaa,\n"
9376 " aaaaaaaaaaa aaaaaaaaa,\n"
9377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9379 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9380 " aaaaaaaaaaaaaaa,\n"
9381 " aaaaaaaaaaaaaaaaaaaaa,\n"
9382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9385 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9386 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9389 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9390 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9393 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9394 " aaaaaaaaaaaaaaaaaaaaa(\n"
9395 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9396 " aaaaaaaaaaaaaaaa);",
9399 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9400 " aaaaaaaaaaaaaaaaaaaaa(\n"
9401 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9402 " aaaaaaaaaaaaaaaa);",
9405 "fooooooooooo(new BARRRRRRRRR(\n"
9406 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9409 "fooooooooooo(::new BARRRRRRRRR(\n"
9410 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9413 "fooooooooooo(new FOO::BARRRR(\n"
9414 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
9417 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
9418 Style
.BinPackArguments
= false;
9419 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
9420 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9421 " aaaaaaaaaaa aaaaaaaa,\n"
9422 " aaaaaaaaa aaaaaaa,\n"
9423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9426 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9427 " aaaaaaaaaaa aaaaaaaaa,\n"
9428 " aaaaaaaaaaa aaaaaaaaa,\n"
9429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9432 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9433 " aaaaaaaaaaaaaaa,\n"
9434 " aaaaaaaaaaaaaaaaaaaaa,\n"
9435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9439 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9443 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9447 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9448 " aaaaaaaaaaaaaaaaaaaaa(\n"
9449 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9451 " aaaaaaaaaaaaaaaa\n"
9455 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9456 " aaaaaaaaaaaaaaaaaaaaa(\n"
9457 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9459 " aaaaaaaaaaaaaaaa\n"
9462 verifyFormat("void foo(\n"
9463 " void (*foobarpntr)(\n"
9464 " aaaaaaaaaaaaaaaaaa *,\n"
9465 " bbbbbbbbbbbbbb *,\n"
9466 " cccccccccccccccccccc *,\n"
9467 " dddddddddddddddddd *\n"
9471 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n"
9472 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9476 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9477 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n"
9482 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9483 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n"
9486 verifyFormat("void aaaaaaaaa(\n"
9487 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n"
9488 ") const noexcept -> std::vector<of_very_long_type>;",
9491 "x = aaaaaaaaaaaaaaa(\n"
9492 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n"
9495 Style
.ColumnLimit
= 60;
9496 verifyFormat("auto lambda =\n"
9498 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9503 TEST_F(FormatTest
, ParenthesesAndOperandAlignment
) {
9504 FormatStyle Style
= getLLVMStyleWithColumns(40);
9505 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9506 " bbbbbbbbbbbbbbbbbbbbbb);",
9508 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
9509 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9510 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9511 " bbbbbbbbbbbbbbbbbbbbbb);",
9513 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9514 Style
.AlignOperands
= FormatStyle::OAS_Align
;
9515 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9516 " bbbbbbbbbbbbbbbbbbbbbb);",
9518 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9519 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9520 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9521 " bbbbbbbbbbbbbbbbbbbbbb);",
9525 TEST_F(FormatTest
, BreaksConditionalExpressions
) {
9527 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9528 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9529 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9531 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9532 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9533 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9535 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9536 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9537 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9538 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9539 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9542 " : aaaaaaaaaaaaa);");
9544 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9545 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9546 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9547 " aaaaaaaaaaaaa);");
9549 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9550 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9551 " aaaaaaaaaaaaa);");
9552 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9553 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9557 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9559 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9561 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9563 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9564 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9566 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9568 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9569 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9570 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9571 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9572 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9574 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9575 " : aaaaaaaaaaaaaaaa;");
9577 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9578 " ? aaaaaaaaaaaaaaa\n"
9579 " : aaaaaaaaaaaaaaa;");
9580 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9584 verifyFormat("return aaaa == bbbb\n"
9588 verifyFormat("unsigned Indent =\n"
9589 " format(TheLine.First,\n"
9590 " IndentForLevel[TheLine.Level] >= 0\n"
9591 " ? IndentForLevel[TheLine.Level]\n"
9593 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9594 getLLVMStyleWithColumns(60));
9595 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9596 " ? aaaaaaaaaaaaaaa\n"
9597 " : bbbbbbbbbbbbbbb //\n"
9598 " ? ccccccccccccccc\n"
9599 " : ddddddddddddddd;");
9600 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9601 " ? aaaaaaaaaaaaaaa\n"
9602 " : (bbbbbbbbbbbbbbb //\n"
9603 " ? ccccccccccccccc\n"
9604 " : ddddddddddddddd);");
9606 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9607 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9608 " aaaaaaaaaaaaaaaaaaaaa +\n"
9609 " aaaaaaaaaaaaaaaaaaaaa\n"
9612 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9613 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9614 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9616 FormatStyle NoBinPacking
= getLLVMStyle();
9617 NoBinPacking
.BinPackArguments
= false;
9621 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9623 " ? aaaaaaaaaaaaaaa\n"
9624 " : aaaaaaaaaaaaaaa);\n"
9630 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9632 " ?: aaaaaaaaaaaaaaa);\n"
9636 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9638 " ccccccccccccccccccccccccccccccccccccccc\n"
9639 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9640 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9642 // Assignments in conditional expressions. Apparently not uncommon :-(.
9643 verifyFormat("return a != b\n"
9647 verifyFormat("return a != b\n"
9654 verifyFormat("return a != b\n"
9662 // Chained conditionals
9663 FormatStyle Style
= getLLVMStyleWithColumns(70);
9664 Style
.AlignOperands
= FormatStyle::OAS_Align
;
9665 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9666 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9667 " : 3333333333333333;",
9669 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9670 " : bbbbbbbbbb ? 2222222222222222\n"
9671 " : 3333333333333333;",
9673 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9674 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9675 " : 3333333333333333;",
9677 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9678 " : bbbbbbbbbbbbbb ? 222222\n"
9681 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9682 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9683 " : cccccccccccccc ? 3333333333333333\n"
9684 " : 4444444444444444;",
9686 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9687 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9688 " : 3333333333333333;",
9690 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9691 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9692 " : (aaa ? bbb : ccc);",
9695 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9696 " : cccccccccccccccccc)\n"
9697 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9698 " : 3333333333333333;",
9701 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9702 " : cccccccccccccccccc)\n"
9703 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9704 " : 3333333333333333;",
9707 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9708 " : dddddddddddddddddd)\n"
9709 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9710 " : 3333333333333333;",
9713 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9714 " : dddddddddddddddddd)\n"
9715 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9716 " : 3333333333333333;",
9719 "return aaaaaaaaa ? 1111111111111111\n"
9720 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9721 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9722 " : dddddddddddddddddd)",
9725 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9726 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9727 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9728 " : cccccccccccccccccc);",
9731 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9732 " : ccccccccccccccc ? dddddddddddddddddd\n"
9733 " : eeeeeeeeeeeeeeeeee)\n"
9734 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9735 " : 3333333333333333;",
9738 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9739 " : ccccccccccccccc ? dddddddddddddddddd\n"
9740 " : eeeeeeeeeeeeeeeeee)\n"
9741 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9742 " : 3333333333333333;",
9745 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9746 " : cccccccccccc ? dddddddddddddddddd\n"
9747 " : eeeeeeeeeeeeeeeeee)\n"
9748 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9749 " : 3333333333333333;",
9752 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9753 " : cccccccccccccccccc\n"
9754 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9755 " : 3333333333333333;",
9758 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9759 " : cccccccccccccccc ? dddddddddddddddddd\n"
9760 " : eeeeeeeeeeeeeeeeee\n"
9761 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9762 " : 3333333333333333;",
9764 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9765 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9766 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9767 " : eeeeeeeeeeeeeeeeee)\n"
9768 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9769 " : 3333333333333333;",
9771 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9772 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9773 " : cccccccccccccccc ? dddddddddddddddddd\n"
9774 " : eeeeeeeeeeeeeeeeee\n"
9775 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9776 " : 3333333333333333;",
9779 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9780 Style
.BreakBeforeTernaryOperators
= false;
9781 // FIXME: Aligning the question marks is weird given DontAlign.
9782 // Consider disabling this alignment in this case. Also check whether this
9783 // will render the adjustment from https://reviews.llvm.org/D82199
9785 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9786 " bbbb ? cccccccccccccccccc :\n"
9791 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9796 " return JJJJJJJJJJJJJJ(\n"
9797 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9801 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9806 " return JJJJJJJJJJJJJJ(\n"
9807 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9811 getGoogleStyle(FormatStyle::LK_JavaScript
));
9814 TEST_F(FormatTest
, BreaksConditionalExpressionsAfterOperator
) {
9815 FormatStyle Style
= getLLVMStyleWithColumns(70);
9816 Style
.BreakBeforeTernaryOperators
= false;
9818 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9820 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9823 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9824 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9828 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9831 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9832 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9836 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9840 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9841 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9846 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9847 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9850 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9856 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9862 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9864 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9868 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9870 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9871 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9872 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9874 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9880 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9881 " aaaaaaaaaaaaaaa :\n"
9882 " aaaaaaaaaaaaaaa;",
9884 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9889 verifyFormat("unsigned Indent =\n"
9890 " format(TheLine.First,\n"
9891 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9892 " IndentForLevel[TheLine.Level] :\n"
9894 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9896 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9897 " aaaaaaaaaaaaaaa :\n"
9898 " bbbbbbbbbbbbbbb ? //\n"
9899 " ccccccccccccccc :\n"
9900 " ddddddddddddddd;",
9902 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9903 " aaaaaaaaaaaaaaa :\n"
9904 " (bbbbbbbbbbbbbbb ? //\n"
9905 " ccccccccccccccc :\n"
9906 " ddddddddddddddd);",
9908 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9909 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9910 " ccccccccccccccccccccccccccc;",
9912 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9914 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9917 // Chained conditionals
9918 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9919 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9920 " 3333333333333333;",
9922 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9923 " bbbbbbbbbb ? 2222222222222222 :\n"
9924 " 3333333333333333;",
9926 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9927 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9928 " 3333333333333333;",
9930 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9931 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9934 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9935 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9936 " cccccccccccccccc ? 3333333333333333 :\n"
9937 " 4444444444444444;",
9939 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9940 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9941 " 3333333333333333;",
9943 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9944 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9945 " (aaa ? bbb : ccc);",
9948 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9949 " cccccccccccccccccc) :\n"
9950 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9951 " 3333333333333333;",
9954 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9955 " cccccccccccccccccc) :\n"
9956 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9957 " 3333333333333333;",
9960 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9961 " dddddddddddddddddd) :\n"
9962 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9963 " 3333333333333333;",
9966 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9967 " dddddddddddddddddd) :\n"
9968 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9969 " 3333333333333333;",
9972 "return aaaaaaaaa ? 1111111111111111 :\n"
9973 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9974 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9975 " dddddddddddddddddd)",
9978 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9979 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9980 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9981 " cccccccccccccccccc);",
9984 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9985 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9986 " eeeeeeeeeeeeeeeeee) :\n"
9987 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9988 " 3333333333333333;",
9991 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9992 " ccccccccccccc ? dddddddddddddddddd :\n"
9993 " eeeeeeeeeeeeeeeeee) :\n"
9994 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9995 " 3333333333333333;",
9998 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9999 " ccccccccccccccccc ? dddddddddddddddddd :\n"
10000 " eeeeeeeeeeeeeeeeee) :\n"
10001 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10002 " 3333333333333333;",
10005 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10006 " cccccccccccccccccc :\n"
10007 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10008 " 3333333333333333;",
10011 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10012 " cccccccccccccccccc ? dddddddddddddddddd :\n"
10013 " eeeeeeeeeeeeeeeeee :\n"
10014 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10015 " 3333333333333333;",
10017 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
10018 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10019 " cccccccccccccccccc ? dddddddddddddddddd :\n"
10020 " eeeeeeeeeeeeeeeeee) :\n"
10021 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10022 " 3333333333333333;",
10024 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
10025 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
10026 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
10027 " eeeeeeeeeeeeeeeeee :\n"
10028 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
10029 " 3333333333333333;",
10033 TEST_F(FormatTest
, DeclarationsOfMultipleVariables
) {
10034 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
10035 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
10036 verifyFormat("bool a = true, b = false;");
10038 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
10040 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
10041 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
10043 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
10044 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
10046 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
10047 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
10048 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10049 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
10050 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
10051 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
10053 FormatStyle Style
= getGoogleStyle();
10054 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
10055 Style
.DerivePointerAlignment
= false;
10056 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10057 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
10058 " *b = bbbbbbbbbbbbbbbbbbb;",
10060 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
10061 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
10063 verifyFormat("vector<int*> a, b;", Style
);
10064 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style
);
10065 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style
);
10066 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style
);
10067 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
10069 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
10072 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
10075 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style
);
10076 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style
);
10077 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style
);
10078 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style
);
10079 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
10083 TEST_F(FormatTest
, ConditionalExpressionsInBrackets
) {
10084 verifyFormat("arr[foo ? bar : baz];");
10085 verifyFormat("f()[foo ? bar : baz];");
10086 verifyFormat("(a + b)[foo ? bar : baz];");
10087 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
10090 TEST_F(FormatTest
, AlignsStringLiterals
) {
10091 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
10092 " \"short literal\");");
10094 "looooooooooooooooooooooooongFunction(\n"
10095 " \"short literal\"\n"
10096 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
10097 verifyFormat("someFunction(\"Always break between multi-line\"\n"
10098 " \" string literals\",\n"
10099 " also, other, parameters);");
10100 verifyFormat("fun + \"1243\" /* comment */\n"
10102 "fun + \"1243\" /* comment */\n"
10104 getLLVMStyleWithColumns(28));
10106 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
10107 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
10108 " \"aaaaaaaaaaaaaaaa\";",
10110 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
10111 "aaaaaaaaaaaaaaaaaaaaa\" "
10112 "\"aaaaaaaaaaaaaaaa\";");
10113 verifyFormat("a = a + \"a\"\n"
10116 verifyFormat("f(\"a\", \"b\"\n"
10120 "#define LL_FORMAT \"ll\"\n"
10121 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
10122 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
10124 verifyFormat("#define A(X) \\\n"
10125 " \"aaaaa\" #X \"bbbbbb\" \\\n"
10127 getLLVMStyleWithColumns(23));
10128 verifyFormat("#define A \"def\"\n"
10129 "f(\"abc\" A \"ghi\"\n"
10132 verifyFormat("f(L\"a\"\n"
10134 verifyFormat("#define A(X) \\\n"
10135 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
10137 getLLVMStyleWithColumns(25));
10139 verifyFormat("f(@\"a\"\n"
10141 verifyFormat("NSString s = @\"a\"\n"
10144 verifyFormat("NSString s = @\"a\"\n"
10149 TEST_F(FormatTest
, ReturnTypeBreakingStyle
) {
10150 FormatStyle Style
= getLLVMStyle();
10151 Style
.ColumnLimit
= 60;
10153 // No declarations or definitions should be moved to own line.
10154 Style
.BreakAfterReturnType
= FormatStyle::RTBS_None
;
10155 verifyFormat("class A {\n"
10156 " int f() { return 1; }\n"
10159 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10161 "int f() { return 1; }\n"
10163 "int foooooooooooooooooooooooooooo::\n"
10164 " baaaaaaaaaaaaaaaaaaaaar();",
10167 // It is now allowed to break after a short return type if necessary.
10168 Style
.BreakAfterReturnType
= FormatStyle::RTBS_Automatic
;
10169 verifyFormat("class A {\n"
10170 " int f() { return 1; }\n"
10173 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10175 "int f() { return 1; }\n"
10178 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10181 // It now must never break after a short return type.
10182 Style
.BreakAfterReturnType
= FormatStyle::RTBS_ExceptShortType
;
10183 verifyFormat("class A {\n"
10184 " int f() { return 1; }\n"
10186 " long foooooooooooooooooooooooooooo::\n"
10187 " baaaaaaaaaaaaaaaaaaaar();\n"
10189 "int f() { return 1; }\n"
10191 "int foooooooooooooooooooooooooooo::\n"
10192 " baaaaaaaaaaaaaaaaaaaaar();",
10195 // All declarations and definitions should have the return type moved to its
10197 Style
.BreakAfterReturnType
= FormatStyle::RTBS_All
;
10198 Style
.TypenameMacros
= {"LIST"};
10199 verifyFormat("SomeType\n"
10200 "funcdecl(LIST(uint64_t));",
10202 verifyFormat("class E {\n"
10210 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n"
10219 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10222 // Top-level definitions, and no kinds of declarations should have the
10223 // return type moved to its own line.
10224 Style
.BreakAfterReturnType
= FormatStyle::RTBS_TopLevelDefinitions
;
10225 verifyFormat("class B {\n"
10226 " int f() { return 1; }\n"
10236 // Top-level definitions and declarations should have the return type moved
10237 // to its own line.
10238 Style
.BreakAfterReturnType
= FormatStyle::RTBS_TopLevel
;
10239 verifyFormat("class C {\n"
10240 " int f() { return 1; }\n"
10250 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();",
10253 // All definitions should have the return type moved to its own line, but no
10254 // kinds of declarations.
10255 Style
.BreakAfterReturnType
= FormatStyle::RTBS_AllDefinitions
;
10256 verifyFormat("class D {\n"
10269 verifyFormat("const char *\n"
10270 "f(void) {\n" // Break here.
10273 "const char *bar(void);", // No break here.
10275 verifyFormat("template <class T>\n"
10277 "f(T &c) {\n" // Break here.
10280 "template <class T> T *f(T &c);", // No break here.
10282 verifyFormat("class C {\n"
10288 " operator()() {\n"
10293 verifyFormat("void\n"
10294 "A::operator()() {}\n"
10296 "A::operator>>() {}\n"
10298 "A::operator+() {}\n"
10300 "A::operator*() {}\n"
10302 "A::operator->() {}\n"
10304 "A::operator void *() {}\n"
10306 "A::operator void &() {}\n"
10308 "A::operator void &&() {}\n"
10310 "A::operator char *() {}\n"
10312 "A::operator[]() {}\n"
10314 "A::operator!() {}\n"
10316 "A::operator**() {}\n"
10318 "A::operator<Foo> *() {}\n"
10320 "A::operator<Foo> **() {}\n"
10322 "A::operator<Foo> &() {}\n"
10324 "A::operator void **() {}",
10326 verifyFormat("constexpr auto\n"
10327 "operator()() const -> reference {}\n"
10329 "operator>>() const -> reference {}\n"
10331 "operator+() const -> reference {}\n"
10333 "operator*() const -> reference {}\n"
10335 "operator->() const -> reference {}\n"
10337 "operator++() const -> reference {}\n"
10339 "operator void *() const -> reference {}\n"
10341 "operator void **() const -> reference {}\n"
10343 "operator void *() const -> reference {}\n"
10345 "operator void &() const -> reference {}\n"
10347 "operator void &&() const -> reference {}\n"
10349 "operator char *() const -> reference {}\n"
10351 "operator!() const -> reference {}\n"
10353 "operator[]() const -> reference {}",
10355 verifyFormat("void *operator new(std::size_t s);", // No break here.
10357 verifyFormat("void *\n"
10358 "operator new(std::size_t s) {}",
10360 verifyFormat("void *\n"
10361 "operator delete[](void *ptr) {}",
10363 Style
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
10364 verifyFormat("const char *\n"
10365 "f(void)\n" // Break here.
10369 "const char *bar(void);", // No break here.
10371 verifyFormat("template <class T>\n"
10372 "T *\n" // Problem here: no line break
10373 "f(T &c)\n" // Break here.
10377 "template <class T> T *f(T &c);", // No break here.
10379 verifyFormat("int\n"
10385 verifyFormat("int\n"
10391 verifyFormat("int\n"
10392 "foo(A<B<bool>, 8> a)\n"
10397 verifyFormat("int\n"
10398 "foo(A<B<8>, bool> a)\n"
10403 verifyFormat("int\n"
10404 "foo(A<B<bool>, bool> a)\n"
10409 verifyFormat("int\n"
10410 "foo(A<B<8>, 8> a)\n"
10416 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
10417 Style
.BraceWrapping
.AfterFunction
= true;
10418 verifyFormat("int f(i);\n" // No break here.
10419 "int\n" // Break here.
10424 "int\n" // Break here.
10430 verifyFormat("int f(a, b, c);\n" // No break here.
10431 "int\n" // Break here.
10432 "f(a, b, c)\n" // Break here.
10436 " return a + b < c;\n"
10438 "int\n" // Break here.
10439 "f(a, b, c)\n" // Break here.
10443 " return a + b < c;\n"
10446 verifyFormat("byte *\n" // Break here.
10447 "f(a)\n" // Break here.
10453 verifyFormat("byte *\n"
10455 "byte /* K&R C */ a[];\n"
10461 "byte /* K&R C */ *p;\n"
10466 verifyFormat("bool f(int a, int) override;\n"
10467 "Bar g(int a, Bar) final;\n"
10468 "Bar h(a, Bar) final;",
10470 verifyFormat("int\n"
10473 verifyFormat("bool\n"
10474 "f(size_t = 0, bool b = false)\n"
10480 // The return breaking style doesn't affect:
10481 // * function and object definitions with attribute-like macros
10482 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10483 " ABSL_GUARDED_BY(mutex) = {};",
10484 getGoogleStyleWithColumns(40));
10485 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10486 " ABSL_GUARDED_BY(mutex); // comment",
10487 getGoogleStyleWithColumns(40));
10488 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10489 " ABSL_GUARDED_BY(mutex1)\n"
10490 " ABSL_GUARDED_BY(mutex2);",
10491 getGoogleStyleWithColumns(40));
10492 verifyFormat("Tttttt f(int a, int b)\n"
10493 " ABSL_GUARDED_BY(mutex1)\n"
10494 " ABSL_GUARDED_BY(mutex2);",
10495 getGoogleStyleWithColumns(40));
10497 verifyGoogleFormat("typedef ATTR(X) char x;");
10499 Style
= getGNUStyle();
10501 // Test for comments at the end of function declarations.
10502 verifyFormat("void\n"
10503 "foo (int a, /*abc*/ int b) // def\n"
10508 verifyFormat("void\n"
10509 "foo (int a, /* abc */ int b) /* def */\n"
10514 // Definitions that should not break after return type
10515 verifyFormat("void foo (int a, int b); // def", Style
);
10516 verifyFormat("void foo (int a, int b); /* def */", Style
);
10517 verifyFormat("void foo (int a, int b);", Style
);
10520 TEST_F(FormatTest
, AlwaysBreakBeforeMultilineStrings
) {
10521 FormatStyle NoBreak
= getLLVMStyle();
10522 NoBreak
.AlwaysBreakBeforeMultilineStrings
= false;
10523 FormatStyle Break
= getLLVMStyle();
10524 Break
.AlwaysBreakBeforeMultilineStrings
= true;
10525 verifyFormat("aaaa = \"bbbb\"\n"
10528 verifyFormat("aaaa =\n"
10532 verifyFormat("aaaa(\"bbbb\"\n"
10535 verifyFormat("aaaa(\n"
10539 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10542 verifyFormat("aaaa(qqq,\n"
10546 verifyFormat("aaaa(qqq,\n"
10550 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10553 verifyFormat("string s = someFunction(\n"
10558 // As we break before unary operators, breaking right after them is bad.
10559 verifyFormat("string foo = abc ? \"x\"\n"
10560 " \"blah blah blah blah blah blah\"\n"
10564 // Don't break if there is no column gain.
10565 verifyFormat("f(\"aaaa\"\n"
10569 // Treat literals with escaped newlines like multi-line string literals.
10570 verifyNoChange("x = \"a\\\n"
10574 verifyFormat("xxxx =\n"
10583 verifyFormat("NSString *const kString =\n"
10586 "NSString *const kString = @\"aaaa\"\n"
10590 Break
.ColumnLimit
= 0;
10591 verifyFormat("const char *hello = \"hello llvm\";", Break
);
10594 TEST_F(FormatTest
, AlignsPipes
) {
10596 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10597 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10598 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10600 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10601 " << aaaaaaaaaaaaaaaaaaaa;");
10603 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10604 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10606 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10607 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10609 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10610 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10611 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10613 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10614 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10615 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10616 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10618 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10619 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10620 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10621 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10623 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10626 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10627 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10629 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10630 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10631 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10633 " aaaaaaaaaaaaaaaaaaaaa)\n"
10634 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10635 verifyFormat("LOG_IF(aaa == //\n"
10639 // But sometimes, breaking before the first "<<" is desirable.
10640 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10641 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10642 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10643 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10644 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10645 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10646 " << BEF << IsTemplate << Description << E->getType();");
10647 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10648 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10650 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10651 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10656 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10657 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10659 // Incomplete string literal.
10660 verifyFormat("llvm::errs() << \"\n"
10662 "llvm::errs() << \"\n<<a;");
10664 verifyFormat("void f() {\n"
10665 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10666 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10670 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10671 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10672 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10675 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10676 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10677 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10678 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10679 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10680 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10681 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10684 TEST_F(FormatTest
, KeepStringLabelValuePairsOnALine
) {
10685 verifyFormat("return out << \"somepacket = {\\n\"\n"
10686 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10687 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10688 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10689 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10692 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10693 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10694 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10696 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10697 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10698 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10699 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10700 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10701 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10702 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10705 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10706 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10709 // Breaking before the first "<<" is generally not desirable.
10712 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10713 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10714 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10715 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10716 getLLVMStyleWithColumns(70));
10717 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10718 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10719 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10720 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10721 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10722 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10723 getLLVMStyleWithColumns(70));
10725 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10726 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10727 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10728 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10729 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10730 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10731 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10733 getLLVMStyleWithColumns(40));
10734 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10735 " (aaaaaaa + aaaaa));",
10736 getLLVMStyleWithColumns(40));
10738 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10739 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10740 " bbbbbbbbbbbbbbbbbbbbbbb);");
10743 TEST_F(FormatTest
, WrapBeforeInsertionOperatorbetweenStringLiterals
) {
10744 verifyFormat("QStringList() << \"foo\" << \"bar\";");
10746 verifyNoChange("QStringList() << \"foo\"\n"
10749 verifyFormat("log_error(log, \"foo\" << \"bar\");",
10750 "log_error(log, \"foo\"\n"
10754 TEST_F(FormatTest
, UnderstandsEquals
) {
10756 "aaaaaaaaaaaaaaaaa =\n"
10757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10759 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10764 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10768 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10769 " 100000000 + 10000000) {\n}");
10772 TEST_F(FormatTest
, WrapsAtFunctionCallsIfNecessary
) {
10773 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10774 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10776 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10777 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10780 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10784 "ShortObject->shortFunction(\n"
10785 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10786 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10788 verifyFormat("loooooooooooooongFunction(\n"
10789 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10792 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10793 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10795 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10796 " .WillRepeatedly(Return(SomeValue));");
10797 verifyFormat("void f() {\n"
10798 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10800 " .WillRepeatedly(Return(SomeValue));\n"
10802 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10803 " ccccccccccccccccccccccc);");
10804 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10806 " .aaaaa(aaaaa),\n"
10807 " aaaaaaaaaaaaaaaaaaaaa);");
10808 verifyFormat("void f() {\n"
10809 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10810 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10812 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10814 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10815 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10816 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10817 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10818 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10819 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10820 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10823 // Here, it is not necessary to wrap at "." or "->".
10824 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10825 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10827 "aaaaaaaaaaa->aaaaaaaaa(\n"
10828 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10829 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10832 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10834 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10835 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10836 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10837 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10839 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10843 FormatStyle NoBinPacking
= getLLVMStyle();
10844 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
10845 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10846 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10847 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10848 " aaaaaaaaaaaaaaaaaaa,\n"
10849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10852 // If there is a subsequent call, change to hanging indentation.
10854 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10855 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10856 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10858 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10859 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10860 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10862 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10863 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10865 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10868 TEST_F(FormatTest
, WrapsTemplateDeclarations
) {
10869 verifyFormat("template <typename T>\n"
10870 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10871 verifyFormat("template <typename T>\n"
10872 "// T should be one of {A, B}.\n"
10873 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10875 "template <typename T>\n"
10876 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10877 verifyFormat("template <typename T>\n"
10878 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10879 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10881 "template <typename T>\n"
10882 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10883 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10885 "template <typename T>\n"
10886 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10887 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10889 verifyFormat("template <typename T>\n"
10890 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10891 " int aaaaaaaaaaaaaaaaaaaaaa);");
10893 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10894 " typename T4 = char>\n"
10896 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10897 " template <typename> class cccccccccccccccccccccc,\n"
10898 " typename ddddddddddddd>\n"
10901 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10904 verifyFormat("void f() {\n"
10905 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10906 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10909 verifyFormat("template <typename T> class C {};");
10910 verifyFormat("template <typename T> void f();");
10911 verifyFormat("template <typename T> void f() {}");
10913 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10916 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10919 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10920 getLLVMStyleWithColumns(72));
10921 verifyFormat("static_cast<A< //\n"
10925 "static_cast<A<//\n"
10929 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10930 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10932 FormatStyle AlwaysBreak
= getLLVMStyle();
10933 AlwaysBreak
.BreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
10934 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak
);
10935 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak
);
10936 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak
);
10937 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10938 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10939 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10940 verifyFormat("template <template <typename> class Fooooooo,\n"
10941 " template <typename> class Baaaaaaar>\n"
10944 verifyFormat("template <typename T> // T can be A, B or C.\n"
10947 verifyFormat("template <typename T>\n"
10950 verifyFormat("template <typename T>\n"
10951 "ClassName(T) noexcept;",
10953 verifyFormat("template <typename T>\n"
10954 "POOR_NAME(T) noexcept;",
10956 verifyFormat("template <enum E> class A {\n"
10961 FormatStyle NeverBreak
= getLLVMStyle();
10962 NeverBreak
.BreakTemplateDeclarations
= FormatStyle::BTDS_No
;
10963 verifyFormat("template <typename T> class C {};", NeverBreak
);
10964 verifyFormat("template <typename T> void f();", NeverBreak
);
10965 verifyFormat("template <typename T> void f() {}", NeverBreak
);
10966 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak
);
10967 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak
);
10968 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak
);
10969 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10970 "bbbbbbbbbbbbbbbbbbbb) {}",
10972 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10973 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10974 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10976 verifyFormat("template <template <typename> class Fooooooo,\n"
10977 " template <typename> class Baaaaaaar>\n"
10980 verifyFormat("template <typename T> // T can be A, B or C.\n"
10983 verifyFormat("template <enum E> class A {\n"
10988 NeverBreak
.PenaltyBreakTemplateDeclaration
= 100;
10989 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10990 "bbbbbbbbbbbbbbbbbbbb) {}",
10993 auto Style
= getLLVMStyle();
10994 Style
.BreakTemplateDeclarations
= FormatStyle::BTDS_Leave
;
10996 verifyNoChange("template <typename T>\n"
10999 verifyFormat("template <typename T> class C {};", Style
);
11001 verifyNoChange("template <typename T>\n"
11004 verifyFormat("template <typename T> void f();", Style
);
11006 verifyNoChange("template <typename T>\n"
11009 verifyFormat("template <typename T> void f() {}", Style
);
11011 verifyNoChange("template <typename T>\n"
11012 "// T can be A, B or C.\n"
11015 verifyFormat("template <typename T> // T can be A, B or C.\n"
11019 verifyNoChange("template <typename T>\n"
11022 verifyFormat("template <typename T> C(T) noexcept;", Style
);
11024 verifyNoChange("template <enum E>\n"
11030 verifyFormat("template <enum E> class A {\n"
11036 verifyNoChange("template <auto x>\n"
11037 "constexpr int simple(int) {\n"
11042 verifyFormat("template <auto x> constexpr int simple(int) {\n"
11048 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
11049 verifyNoChange("template <auto x>\n"
11050 "requires(x > 1)\n"
11051 "constexpr int with_req(int) {\n"
11055 verifyFormat("template <auto x> requires(x > 1)\n"
11056 "constexpr int with_req(int) {\n"
11062 TEST_F(FormatTest
, WrapsTemplateDeclarationsWithComments
) {
11063 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Cpp
);
11064 Style
.ColumnLimit
= 60;
11065 verifyFormat("// Baseline - no comments.\n"
11067 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
11071 verifyFormat("template <\n"
11072 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11075 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11081 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11083 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
11087 verifyFormat("template <\n"
11088 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11092 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
11098 "template <typename aaaaaaaaaa<\n"
11099 " bbbbbbbbbbbb>::value> // trailing loooong\n"
11102 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
11107 TEST_F(FormatTest
, WrapsTemplateParameters
) {
11108 FormatStyle Style
= getLLVMStyle();
11109 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
11110 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
11112 "template <typename... a> struct q {};\n"
11113 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11114 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11117 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
11118 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
11120 "template <typename... a> struct r {};\n"
11121 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
11122 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
11125 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
11126 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
11127 verifyFormat("template <typename... a> struct s {};\n"
11129 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11130 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11131 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11132 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11135 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
11136 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
11137 verifyFormat("template <typename... a> struct t {};\n"
11139 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11140 "aaaaaaaaaaaaaaaaaaaaaa,\n"
11141 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
11142 "aaaaaaaaaaaaaaaaaaaaaa>\n"
11147 TEST_F(FormatTest
, WrapsAtNestedNameSpecifiers
) {
11149 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11152 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
11156 // FIXME: Should we have the extra indent after the second break?
11158 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11159 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11163 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
11164 " cccccccccccccccccccccccccccccccccccccccccccccc());");
11166 // Breaking at nested name specifiers is generally not desirable.
11168 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11169 " aaaaaaaaaaaaaaaaaaaaaaa);");
11171 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
11172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
11174 " aaaaaaaaaaaaaaaaaaaaa);",
11175 getLLVMStyleWithColumns(74));
11177 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
11178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11179 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
11182 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11183 " AndAnotherLongClassNameToShowTheIssue() {}\n"
11184 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
11185 " ~AndAnotherLongClassNameToShowTheIssue() {}");
11188 TEST_F(FormatTest
, UnderstandsTemplateParameters
) {
11189 verifyFormat("A<int> a;");
11190 verifyFormat("A<A<A<int>>> a;");
11191 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
11192 verifyFormat("bool x = a < 1 || 2 > a;");
11193 verifyFormat("bool x = 5 < f<int>();");
11194 verifyFormat("bool x = f<int>() > 5;");
11195 verifyFormat("bool x = 5 < a<int>::x;");
11196 verifyFormat("bool x = a < 4 ? a > 2 : false;");
11197 verifyFormat("bool x = f() ? a < 2 : a > 2;");
11199 verifyGoogleFormat("A<A<int>> a;");
11200 verifyGoogleFormat("A<A<A<int>>> a;");
11201 verifyGoogleFormat("A<A<A<A<int>>>> a;");
11202 verifyGoogleFormat("A<A<int> > a;");
11203 verifyGoogleFormat("A<A<A<int> > > a;");
11204 verifyGoogleFormat("A<A<A<A<int> > > > a;");
11205 verifyGoogleFormat("A<::A<int>> a;");
11206 verifyGoogleFormat("A<::A> a;");
11207 verifyGoogleFormat("A< ::A> a;");
11208 verifyGoogleFormat("A< ::A<int> > a;");
11209 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
11210 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
11211 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
11212 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
11213 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
11216 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp
));
11218 // template closer followed by a token that starts with > or =
11219 verifyFormat("bool b = a<1> > 1;");
11220 verifyFormat("bool b = a<1> >= 1;");
11221 verifyFormat("int i = a<1> >> 1;");
11222 FormatStyle Style
= getLLVMStyle();
11223 Style
.SpaceBeforeAssignmentOperators
= false;
11224 verifyFormat("bool b= a<1> == 1;", Style
);
11225 verifyFormat("a<int> = 1;", Style
);
11226 verifyFormat("a<int> >>= 1;", Style
);
11228 verifyFormat("test < a | b >> c;");
11229 verifyFormat("test<test<a | b>> c;");
11230 verifyFormat("test >> a >> b;");
11231 verifyFormat("test << a >> b;");
11233 verifyFormat("f<int>();");
11234 verifyFormat("template <typename T> void f() {}");
11235 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
11236 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
11237 "sizeof(char)>::type>;");
11238 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
11239 verifyFormat("f(a.operator()<A>());");
11240 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11241 " .template operator()<A>());",
11242 getLLVMStyleWithColumns(35));
11243 verifyFormat("bool_constant<a && noexcept(f())>;");
11244 verifyFormat("bool_constant<a || noexcept(f())>;");
11246 verifyFormat("if (std::tuple_size_v<T> > 0)");
11248 // Not template parameters.
11249 verifyFormat("return a < b && c > d;");
11250 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
11251 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
11252 verifyFormat("void f() {\n"
11253 " while (a < b && c > d) {\n"
11256 verifyFormat("template <typename... Types>\n"
11257 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
11259 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
11261 getLLVMStyleWithColumns(60));
11262 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
11263 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
11264 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
11265 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
11267 verifyFormat("#define FOO(typeName, realClass) \\\n"
11268 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}",
11269 getLLVMStyleWithColumns(60));
11272 TEST_F(FormatTest
, UnderstandsShiftOperators
) {
11273 verifyFormat("if (i < x >> 1)");
11274 verifyFormat("while (i < x >> 1)");
11275 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
11276 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
11278 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
11279 verifyFormat("Foo.call<Bar<Function>>()");
11280 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
11281 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
11282 "++i, v = v >> 1)");
11283 verifyFormat("if (w<u<v<x>>, 1>::t)");
11286 TEST_F(FormatTest
, BitshiftOperatorWidth
) {
11287 verifyFormat("int a = 1 << 2; /* foo\n"
11289 "int a=1<<2; /* foo\n"
11292 verifyFormat("int b = 256 >> 1; /* foo\n"
11294 "int b =256>>1 ; /* foo\n"
11298 TEST_F(FormatTest
, UnderstandsBinaryOperators
) {
11299 verifyFormat("COMPARE(a, ==, b);");
11300 verifyFormat("auto s = sizeof...(Ts) - 1;");
11303 TEST_F(FormatTest
, UnderstandsPointersToMembers
) {
11304 verifyFormat("int A::*x;");
11305 verifyFormat("int (S::*func)(void *);");
11306 verifyFormat("void f() { int (S::*func)(void *); }");
11307 verifyFormat("typedef bool *(Class::*Member)() const;");
11308 verifyFormat("void f() {\n"
11315 verifyFormat("void f() {\n"
11316 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
11317 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
11320 "(aaaaaaaaaa->*bbbbbbb)(\n"
11321 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
11323 FormatStyle Style
= getLLVMStyle();
11324 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
11325 verifyFormat("typedef bool *(Class::*Member)() const;", Style
);
11326 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style
);
11328 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11329 verifyFormat("typedef bool* (Class::*Member)() const;", Style
);
11330 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style
);
11332 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11333 verifyFormat("typedef bool * (Class::*Member)() const;", Style
);
11334 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style
);
11337 TEST_F(FormatTest
, UnderstandsUnaryOperators
) {
11338 verifyFormat("int a = -2;");
11339 verifyFormat("f(-1, -2, -3);");
11340 verifyFormat("a[-1] = 5;");
11341 verifyFormat("int a = 5 + -2;");
11342 verifyFormat("if (i == -1) {\n}");
11343 verifyFormat("if (i != -1) {\n}");
11344 verifyFormat("if (i > -1) {\n}");
11345 verifyFormat("if (i < -1) {\n}");
11346 verifyFormat("++(a->f());");
11347 verifyFormat("--(a->f());");
11348 verifyFormat("(a->f())++;");
11349 verifyFormat("a[42]++;");
11350 verifyFormat("if (!(a->f())) {\n}");
11351 verifyFormat("if (!+i) {\n}");
11352 verifyFormat("~&a;");
11353 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
11354 verifyFormat("sizeof -x");
11355 verifyFormat("sizeof +x");
11356 verifyFormat("sizeof *x");
11357 verifyFormat("sizeof &x");
11358 verifyFormat("delete +x;");
11359 verifyFormat("co_await +x;");
11360 verifyFormat("case *x:");
11361 verifyFormat("case &x:");
11363 verifyFormat("a-- > b;");
11364 verifyFormat("b ? -a : c;");
11365 verifyFormat("n * sizeof char16;");
11366 verifyGoogleFormat("n * alignof char16;");
11367 verifyFormat("sizeof(char);");
11368 verifyGoogleFormat("alignof(char);");
11370 verifyFormat("return -1;");
11371 verifyFormat("throw -1;");
11372 verifyFormat("switch (a) {\n"
11376 verifyFormat("#define X -1");
11377 verifyFormat("#define X -kConstant");
11379 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
11380 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
11382 verifyFormat("int a = /* confusing comment */ -1;");
11383 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
11384 verifyFormat("int a = i /* confusing comment */++;");
11386 verifyFormat("co_yield -1;");
11387 verifyFormat("co_return -1;");
11389 // Check that * is not treated as a binary operator when we set
11390 // PointerAlignment as PAS_Left after a keyword and not a declaration.
11391 FormatStyle PASLeftStyle
= getLLVMStyle();
11392 PASLeftStyle
.PointerAlignment
= FormatStyle::PAS_Left
;
11393 verifyFormat("co_return *a;", PASLeftStyle
);
11394 verifyFormat("co_await *a;", PASLeftStyle
);
11395 verifyFormat("co_yield *a", PASLeftStyle
);
11396 verifyFormat("return *a;", PASLeftStyle
);
11399 TEST_F(FormatTest
, DoesNotIndentRelativeToUnaryOperators
) {
11400 verifyFormat("if (!aaaaaaaaaa( // break\n"
11403 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
11405 verifyFormat("*aaa = aaaaaaa( // break\n"
11409 TEST_F(FormatTest
, UnderstandsOverloadedOperators
) {
11410 verifyFormat("bool operator<();");
11411 verifyFormat("bool operator>();");
11412 verifyFormat("bool operator=();");
11413 verifyFormat("bool operator==();");
11414 verifyFormat("bool operator!=();");
11415 verifyFormat("int operator+();");
11416 verifyFormat("int operator++();");
11417 verifyFormat("int operator++(int) volatile noexcept;");
11418 verifyFormat("bool operator,();");
11419 verifyFormat("bool operator();");
11420 verifyFormat("bool operator()();");
11421 verifyFormat("bool operator[]();");
11422 verifyFormat("operator bool();");
11423 verifyFormat("operator int();");
11424 verifyFormat("operator void *();");
11425 verifyFormat("operator SomeType<int>();");
11426 verifyFormat("operator SomeType<int, int>();");
11427 verifyFormat("operator SomeType<SomeType<int>>();");
11428 verifyFormat("operator< <>();");
11429 verifyFormat("operator<< <>();");
11430 verifyFormat("< <>");
11432 verifyFormat("void *operator new(std::size_t size);");
11433 verifyFormat("void *operator new[](std::size_t size);");
11434 verifyFormat("void operator delete(void *ptr);");
11435 verifyFormat("void operator delete[](void *ptr);");
11436 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
11437 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
11438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
11439 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
11442 "ostream &operator<<(ostream &OutputStream,\n"
11443 " SomeReallyLongType WithSomeReallyLongValue);");
11444 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
11445 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
11446 " return left.group < right.group;\n"
11448 verifyFormat("SomeType &operator=(const SomeType &S);");
11449 verifyFormat("f.template operator()<int>();");
11451 verifyGoogleFormat("operator void*();");
11452 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
11453 verifyGoogleFormat("operator ::A();");
11455 verifyFormat("using A::operator+;");
11456 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
11459 // Calling an operator as a member function.
11460 verifyFormat("void f() { a.operator*(); }");
11461 verifyFormat("void f() { a.operator*(b & b); }");
11462 verifyFormat("void f() { a->operator&(a * b); }");
11463 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
11464 verifyFormat("void f() { operator*(a & a); }");
11465 verifyFormat("void f() { operator&(a, b * b); }");
11467 verifyFormat("void f() { return operator()(x) * b; }");
11468 verifyFormat("void f() { return operator[](x) * b; }");
11469 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
11470 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
11471 verifyFormat("void f() { return operator\"\"s(x) * b; }");
11472 verifyFormat("void f() { return operator\"\" s(x) * b; }");
11473 verifyFormat("void f() { return operator\"\"if(x) * b; }");
11475 verifyFormat("::operator delete(foo);");
11476 verifyFormat("::operator new(n * sizeof(foo));");
11477 verifyFormat("foo() { ::operator delete(foo); }");
11478 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
11481 TEST_F(FormatTest
, SpaceBeforeTemplateCloser
) {
11482 verifyFormat("C<&operator- > minus;");
11483 verifyFormat("C<&operator> > gt;");
11484 verifyFormat("C<&operator>= > ge;");
11485 verifyFormat("C<&operator<= > le;");
11486 verifyFormat("C<&operator< <X>> lt;");
11489 TEST_F(FormatTest
, UnderstandsFunctionRefQualification
) {
11490 verifyFormat("void A::b() && {}");
11491 verifyFormat("void A::b() && noexcept {}");
11492 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
11493 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
11494 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
11495 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
11496 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
11497 verifyFormat("Deleted &operator=(const Deleted &) &;");
11498 verifyFormat("Deleted &operator=(const Deleted &) &&;");
11499 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
11500 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
11501 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
11502 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
11503 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
11504 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
11505 verifyFormat("void Fn(T const &) const &;");
11506 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
11507 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
11508 verifyGoogleFormat("template <typename T>\n"
11509 "void F(T) && = delete;");
11510 verifyFormat("template <typename T> void operator=(T) &;");
11511 verifyFormat("template <typename T> void operator=(T) const &;");
11512 verifyFormat("template <typename T> void operator=(T) & noexcept;");
11513 verifyFormat("template <typename T> void operator=(T) & = default;");
11514 verifyFormat("template <typename T> void operator=(T) &&;");
11515 verifyFormat("template <typename T> void operator=(T) && = delete;");
11516 verifyFormat("template <typename T> void operator=(T) & {}");
11517 verifyFormat("template <typename T> void operator=(T) && {}");
11519 FormatStyle AlignLeft
= getLLVMStyle();
11520 AlignLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
11521 verifyFormat("void A::b() && {}", AlignLeft
);
11522 verifyFormat("void A::b() && noexcept {}", AlignLeft
);
11523 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft
);
11524 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
11526 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
11528 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft
);
11529 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft
);
11530 verifyFormat("auto Function(T t) & -> void {}", AlignLeft
);
11531 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft
);
11532 verifyFormat("auto Function(T) & -> void {}", AlignLeft
);
11533 verifyFormat("auto Function(T) & -> void;", AlignLeft
);
11534 verifyFormat("void Fn(T const&) const&;", AlignLeft
);
11535 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft
);
11536 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
11538 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft
);
11539 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft
);
11540 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11542 verifyFormat("template <typename T> void operator=(T) & = default;",
11544 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft
);
11545 verifyFormat("template <typename T> void operator=(T) && = delete;",
11547 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft
);
11548 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft
);
11549 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft
);
11551 FormatStyle AlignMiddle
= getLLVMStyle();
11552 AlignMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
11553 verifyFormat("void A::b() && {}", AlignMiddle
);
11554 verifyFormat("void A::b() && noexcept {}", AlignMiddle
);
11555 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11557 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11559 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11561 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle
);
11562 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle
);
11563 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle
);
11564 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle
);
11565 verifyFormat("auto Function(T) & -> void {}", AlignMiddle
);
11566 verifyFormat("auto Function(T) & -> void;", AlignMiddle
);
11567 verifyFormat("void Fn(T const &) const &;", AlignMiddle
);
11568 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle
);
11569 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11571 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle
);
11572 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle
);
11573 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11575 verifyFormat("template <typename T> void operator=(T) & = default;",
11577 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle
);
11578 verifyFormat("template <typename T> void operator=(T) && = delete;",
11580 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle
);
11581 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle
);
11583 FormatStyle Spaces
= getLLVMStyle();
11584 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
11585 Spaces
.SpacesInParensOptions
= {};
11586 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
11587 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces
);
11588 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces
);
11589 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces
);
11590 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces
);
11592 Spaces
.SpacesInParensOptions
.InCStyleCasts
= false;
11593 Spaces
.SpacesInParensOptions
.Other
= true;
11594 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces
);
11595 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11597 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces
);
11598 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces
);
11600 FormatStyle BreakTemplate
= getLLVMStyle();
11601 BreakTemplate
.BreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
11603 verifyFormat("struct f {\n"
11604 " template <class T>\n"
11605 " int &foo(const std::string &str) & noexcept {}\n"
11609 verifyFormat("struct f {\n"
11610 " template <class T>\n"
11611 " int &foo(const std::string &str) && noexcept {}\n"
11615 verifyFormat("struct f {\n"
11616 " template <class T>\n"
11617 " int &foo(const std::string &str) const & noexcept {}\n"
11621 verifyFormat("struct f {\n"
11622 " template <class T>\n"
11623 " int &foo(const std::string &str) const & noexcept {}\n"
11627 verifyFormat("struct f {\n"
11628 " template <class T>\n"
11629 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11633 FormatStyle AlignLeftBreakTemplate
= getLLVMStyle();
11634 AlignLeftBreakTemplate
.BreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
11635 AlignLeftBreakTemplate
.PointerAlignment
= FormatStyle::PAS_Left
;
11637 verifyFormat("struct f {\n"
11638 " template <class T>\n"
11639 " int& foo(const std::string& str) & noexcept {}\n"
11641 AlignLeftBreakTemplate
);
11643 verifyFormat("struct f {\n"
11644 " template <class T>\n"
11645 " int& foo(const std::string& str) && noexcept {}\n"
11647 AlignLeftBreakTemplate
);
11649 verifyFormat("struct f {\n"
11650 " template <class T>\n"
11651 " int& foo(const std::string& str) const& noexcept {}\n"
11653 AlignLeftBreakTemplate
);
11655 verifyFormat("struct f {\n"
11656 " template <class T>\n"
11657 " int& foo(const std::string& str) const&& noexcept {}\n"
11659 AlignLeftBreakTemplate
);
11661 verifyFormat("struct f {\n"
11662 " template <class T>\n"
11663 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11665 AlignLeftBreakTemplate
);
11667 // The `&` in `Type&` should not be confused with a trailing `&` of
11668 // DEPRECATED(reason) member function.
11669 verifyFormat("struct f {\n"
11670 " template <class T>\n"
11671 " DEPRECATED(reason)\n"
11672 " Type &foo(arguments) {}\n"
11676 verifyFormat("struct f {\n"
11677 " template <class T>\n"
11678 " DEPRECATED(reason)\n"
11679 " Type& foo(arguments) {}\n"
11681 AlignLeftBreakTemplate
);
11683 verifyFormat("void (*foopt)(int) = &func;");
11685 FormatStyle DerivePointerAlignment
= getLLVMStyle();
11686 DerivePointerAlignment
.DerivePointerAlignment
= true;
11687 // There's always a space between the function and its trailing qualifiers.
11688 // This isn't evidence for PAS_Right (or for PAS_Left).
11689 std::string Prefix
= "void a() &;\n"
11691 verifyFormat(Prefix
+ "int* x;", DerivePointerAlignment
);
11692 verifyFormat(Prefix
+ "int *x;", DerivePointerAlignment
);
11693 // Same if the function is an overloaded operator, and with &&.
11694 Prefix
= "void operator()() &&;\n"
11695 "void operator()() &&;\n";
11696 verifyFormat(Prefix
+ "int* x;", DerivePointerAlignment
);
11697 verifyFormat(Prefix
+ "int *x;", DerivePointerAlignment
);
11698 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11699 Prefix
= "void a() const &;\n"
11700 "void b() const &;\n";
11701 verifyFormat(Prefix
+ "int *x;", Prefix
+ "int* x;", DerivePointerAlignment
);
11704 TEST_F(FormatTest
, PointerAlignmentFallback
) {
11705 FormatStyle Style
= getLLVMStyle();
11706 Style
.DerivePointerAlignment
= true;
11708 const StringRef
Code("int* p;\n"
11712 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
11713 verifyFormat("int *p;\n"
11718 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11719 verifyFormat("int* p;\n"
11724 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11725 verifyFormat("int * p;\n"
11731 TEST_F(FormatTest
, UnderstandsNewAndDelete
) {
11732 verifyFormat("void f() {\n"
11734 " A *a = new (placement) A;\n"
11736 " delete (A *)a;\n"
11738 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11739 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11740 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11741 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11742 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11743 verifyFormat("delete[] h->p;");
11744 verifyFormat("delete[] (void *)p;");
11746 verifyFormat("void operator delete(void *foo) ATTRIB;");
11747 verifyFormat("void operator new(void *foo) ATTRIB;");
11748 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11749 verifyFormat("void operator delete(void *ptr) noexcept;");
11751 verifyFormat("void new(link p);\n"
11752 "void delete(link p);",
11753 "void new (link p);\n"
11754 "void delete (link p);");
11769 FormatStyle AfterPlacementOperator
= getLLVMStyle();
11770 AfterPlacementOperator
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
11772 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
);
11773 verifyFormat("new (buf) int;", AfterPlacementOperator
);
11774 verifyFormat("struct A {\n"
11776 " A(int *p) : a(new (p) int) {\n"
11778 " int *b = new (p) int;\n"
11779 " int *c = new (p) int(3);\n"
11783 AfterPlacementOperator
);
11784 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator
);
11785 verifyFormat("delete (int *)p;", AfterPlacementOperator
);
11787 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
=
11789 verifyFormat("new(buf) int;", AfterPlacementOperator
);
11790 verifyFormat("struct A {\n"
11792 " A(int *p) : a(new(p) int) {\n"
11794 " int *b = new(p) int;\n"
11795 " int *c = new(p) int(3);\n"
11799 AfterPlacementOperator
);
11800 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator
);
11801 verifyFormat("delete (int *)p;", AfterPlacementOperator
);
11804 TEST_F(FormatTest
, UnderstandsUsesOfStarAndAmp
) {
11805 verifyFormat("int *f(int *a) {}");
11806 verifyFormat("int main(int argc, char **argv) {}");
11807 verifyFormat("Test::Test(int b) : a(b * b) {}");
11808 verifyIndependentOfContext("f(a, *a);");
11809 verifyFormat("void g() { f(*a); }");
11810 verifyIndependentOfContext("int a = b * 10;");
11811 verifyIndependentOfContext("int a = 10 * b;");
11812 verifyIndependentOfContext("int a = b * c;");
11813 verifyIndependentOfContext("int a += b * c;");
11814 verifyIndependentOfContext("int a -= b * c;");
11815 verifyIndependentOfContext("int a *= b * c;");
11816 verifyIndependentOfContext("int a /= b * c;");
11817 verifyIndependentOfContext("int a = *b;");
11818 verifyIndependentOfContext("int a = *b * c;");
11819 verifyIndependentOfContext("int a = b * *c;");
11820 verifyIndependentOfContext("int a = b * (10);");
11821 verifyIndependentOfContext("S << b * (10);");
11822 verifyIndependentOfContext("return 10 * b;");
11823 verifyIndependentOfContext("return *b * *c;");
11824 verifyIndependentOfContext("return a & ~b;");
11825 verifyIndependentOfContext("f(b ? *c : *d);");
11826 verifyIndependentOfContext("int a = b ? *c : *d;");
11827 verifyIndependentOfContext("*b = a;");
11828 verifyIndependentOfContext("a * ~b;");
11829 verifyIndependentOfContext("a * !b;");
11830 verifyIndependentOfContext("a * +b;");
11831 verifyIndependentOfContext("a * -b;");
11832 verifyIndependentOfContext("a * ++b;");
11833 verifyIndependentOfContext("a * --b;");
11834 verifyIndependentOfContext("a[4] * b;");
11835 verifyIndependentOfContext("a[a * a] = 1;");
11836 verifyIndependentOfContext("f() * b;");
11837 verifyIndependentOfContext("a * [self dostuff];");
11838 verifyIndependentOfContext("int x = a * (a + b);");
11839 verifyIndependentOfContext("(a *)(a + b);");
11840 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11841 verifyIndependentOfContext("int *pa = (int *)&a;");
11842 verifyIndependentOfContext("return sizeof(int **);");
11843 verifyIndependentOfContext("return sizeof(int ******);");
11844 verifyIndependentOfContext("return (int **&)a;");
11845 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11846 verifyFormat("void f(Type (*parameter)[10]) {}");
11847 verifyFormat("void f(Type (¶meter)[10]) {}");
11848 verifyGoogleFormat("return sizeof(int**);");
11849 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11850 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11851 verifyFormat("auto a = [](int **&, int ***) {};");
11852 verifyFormat("auto PointerBinding = [](const char *S) {};");
11853 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11854 verifyFormat("[](const decltype(*a) &value) {}");
11855 verifyFormat("[](const typeof(*a) &value) {}");
11856 verifyFormat("[](const _Atomic(a *) &value) {}");
11857 verifyFormat("[](const __underlying_type(a) &value) {}");
11858 verifyFormat("decltype(a * b) F();");
11859 verifyFormat("typeof(a * b) F();");
11860 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11861 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11862 verifyIndependentOfContext("typedef void (*f)(int *a);");
11863 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11864 verifyIndependentOfContext("int i{a * b};");
11865 verifyIndependentOfContext("aaa && aaa->f();");
11866 verifyIndependentOfContext("int x = ~*p;");
11867 verifyFormat("Constructor() : a(a), area(width * height) {}");
11868 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11869 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11870 verifyFormat("void f() { f(a, c * d); }");
11871 verifyFormat("void f() { f(new a(), c * d); }");
11872 verifyFormat("void f(const MyOverride &override);");
11873 verifyFormat("void f(const MyFinal &final);");
11874 verifyIndependentOfContext("bool a = f() && override.f();");
11875 verifyIndependentOfContext("bool a = f() && final.f();");
11877 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11879 verifyIndependentOfContext("A<int *> a;");
11880 verifyIndependentOfContext("A<int **> a;");
11881 verifyIndependentOfContext("A<int *, int *> a;");
11882 verifyIndependentOfContext("A<int *[]> a;");
11883 verifyIndependentOfContext(
11884 "const char *const p = reinterpret_cast<const char *const>(q);");
11885 verifyIndependentOfContext("A<int **, int **> a;");
11886 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11887 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11888 verifyFormat("for (; a && b;) {\n}");
11889 verifyFormat("bool foo = true && [] { return false; }();");
11892 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11893 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11895 verifyGoogleFormat("int const* a = &b;");
11896 verifyGoogleFormat("**outparam = 1;");
11897 verifyGoogleFormat("*outparam = a * b;");
11898 verifyGoogleFormat("int main(int argc, char** argv) {}");
11899 verifyGoogleFormat("A<int*> a;");
11900 verifyGoogleFormat("A<int**> a;");
11901 verifyGoogleFormat("A<int*, int*> a;");
11902 verifyGoogleFormat("A<int**, int**> a;");
11903 verifyGoogleFormat("f(b ? *c : *d);");
11904 verifyGoogleFormat("int a = b ? *c : *d;");
11905 verifyGoogleFormat("Type* t = **x;");
11906 verifyGoogleFormat("Type* t = *++*x;");
11907 verifyGoogleFormat("*++*x;");
11908 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11909 verifyGoogleFormat("Type* t = x++ * y;");
11910 verifyGoogleFormat(
11911 "const char* const p = reinterpret_cast<const char* const>(q);");
11912 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11913 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11914 verifyGoogleFormat("template <typename T>\n"
11915 "void f(int i = 0, SomeType** temps = NULL);");
11917 FormatStyle Left
= getLLVMStyle();
11918 Left
.PointerAlignment
= FormatStyle::PAS_Left
;
11919 verifyFormat("x = *a(x) = *a(y);", Left
);
11920 verifyFormat("for (;; *a = b) {\n}", Left
);
11921 verifyFormat("return *this += 1;", Left
);
11922 verifyFormat("throw *x;", Left
);
11923 verifyFormat("delete *x;", Left
);
11924 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left
);
11925 verifyFormat("[](const decltype(*a)* ptr) {}", Left
);
11926 verifyFormat("[](const typeof(*a)* ptr) {}", Left
);
11927 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left
);
11928 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left
);
11929 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left
);
11930 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left
);
11931 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left
);
11932 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left
);
11934 verifyIndependentOfContext("a = *(x + y);");
11935 verifyIndependentOfContext("a = &(x + y);");
11936 verifyIndependentOfContext("*(x + y).call();");
11937 verifyIndependentOfContext("&(x + y)->call();");
11938 verifyFormat("void f() { &(*I).first; }");
11940 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11941 verifyFormat("f(* /* confusing comment */ foo);");
11942 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11943 verifyFormat("void foo(int * // this is the first paramters\n"
11946 verifyFormat("double term = a * // first\n"
11949 "int *MyValues = {\n"
11950 " *A, // Operator detection might be confused by the '{'\n"
11951 " *BB // Operator detection might be confused by previous comment\n"
11954 verifyIndependentOfContext("if (int *a = &b)");
11955 verifyIndependentOfContext("if (int &a = *b)");
11956 verifyIndependentOfContext("if (a & b[i])");
11957 verifyIndependentOfContext("if constexpr (a & b[i])");
11958 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11959 verifyIndependentOfContext("if (a * (b * c))");
11960 verifyIndependentOfContext("if constexpr (a * (b * c))");
11961 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11962 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11963 verifyIndependentOfContext("if (*b[i])");
11964 verifyIndependentOfContext("if (int *a = (&b))");
11965 verifyIndependentOfContext("while (int *a = &b)");
11966 verifyIndependentOfContext("while (a * (b * c))");
11967 verifyIndependentOfContext("size = sizeof *a;");
11968 verifyIndependentOfContext("if (a && (b = c))");
11969 verifyFormat("void f() {\n"
11970 " for (const int &v : Values) {\n"
11973 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11974 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11975 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11977 verifyFormat("#define A (!a * b)");
11978 verifyFormat("#define MACRO \\\n"
11979 " int *i = a * b; \\\n"
11981 getLLVMStyleWithColumns(19));
11983 verifyIndependentOfContext("A = new SomeType *[Length];");
11984 verifyIndependentOfContext("A = new SomeType *[Length]();");
11985 verifyIndependentOfContext("T **t = new T *;");
11986 verifyIndependentOfContext("T **t = new T *();");
11987 verifyGoogleFormat("A = new SomeType*[Length]();");
11988 verifyGoogleFormat("A = new SomeType*[Length];");
11989 verifyGoogleFormat("T** t = new T*;");
11990 verifyGoogleFormat("T** t = new T*();");
11992 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11993 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11994 verifyFormat("template <bool a, bool b> "
11995 "typename t::if<x && y>::type f() {}");
11996 verifyFormat("template <int *y> f() {}");
11997 verifyFormat("vector<int *> v;");
11998 verifyFormat("vector<int *const> v;");
11999 verifyFormat("vector<int *const **const *> v;");
12000 verifyFormat("vector<int *volatile> v;");
12001 verifyFormat("vector<a *_Nonnull> v;");
12002 verifyFormat("vector<a *_Nullable> v;");
12003 verifyFormat("vector<a *_Null_unspecified> v;");
12004 verifyFormat("vector<a *__ptr32> v;");
12005 verifyFormat("vector<a *__ptr64> v;");
12006 verifyFormat("vector<a *__capability> v;");
12007 FormatStyle TypeMacros
= getLLVMStyle();
12008 TypeMacros
.TypenameMacros
= {"LIST"};
12009 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros
);
12010 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros
);
12011 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros
);
12012 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros
);
12013 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros
); // multiplication
12015 FormatStyle CustomQualifier
= getLLVMStyle();
12016 // Add identifiers that should not be parsed as a qualifier by default.
12017 CustomQualifier
.AttributeMacros
.push_back("__my_qualifier");
12018 CustomQualifier
.AttributeMacros
.push_back("_My_qualifier");
12019 CustomQualifier
.AttributeMacros
.push_back("my_other_qualifier");
12020 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
12021 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier
);
12022 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
12023 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier
);
12024 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
12025 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier
);
12026 verifyFormat("vector<a * _NotAQualifier> v;");
12027 verifyFormat("vector<a * __not_a_qualifier> v;");
12028 verifyFormat("vector<a * b> v;");
12029 verifyFormat("foo<b && false>();");
12030 verifyFormat("foo<b & 1>();");
12031 verifyFormat("foo<b & (1)>();");
12032 verifyFormat("foo<b & (~0)>();");
12033 verifyFormat("foo<b & (true)>();");
12034 verifyFormat("foo<b & ((1))>();");
12035 verifyFormat("foo<b & (/*comment*/ 1)>();");
12036 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
12037 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
12038 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
12039 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
12041 "template <class T, class = typename std::enable_if<\n"
12042 " std::is_integral<T>::value &&\n"
12043 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
12045 getLLVMStyleWithColumns(70));
12046 verifyFormat("template <class T,\n"
12047 " class = typename std::enable_if<\n"
12048 " std::is_integral<T>::value &&\n"
12049 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
12052 getLLVMStyleWithColumns(70));
12054 "template <class T,\n"
12055 " class = typename ::std::enable_if<\n"
12056 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
12058 getGoogleStyleWithColumns(68));
12060 FormatStyle Style
= getLLVMStyle();
12061 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
12062 verifyFormat("struct {\n"
12065 verifyFormat("union {\n"
12068 verifyFormat("class {\n"
12071 // Don't confuse a multiplication after a brace-initialized expression with
12072 // a class pointer.
12073 verifyFormat("int i = int{42} * 34;", Style
);
12074 verifyFormat("struct {\n"
12077 verifyFormat("union {\n"
12080 verifyFormat("class {\n"
12083 verifyFormat("bool b = 3 == int{3} && true;");
12085 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
12086 verifyFormat("struct {\n"
12089 verifyFormat("union {\n"
12092 verifyFormat("class {\n"
12095 verifyFormat("struct {\n"
12098 verifyFormat("union {\n"
12101 verifyFormat("class {\n"
12105 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
12106 verifyFormat("struct {\n"
12109 verifyFormat("union {\n"
12112 verifyFormat("class {\n"
12115 verifyFormat("struct {\n"
12118 verifyFormat("union {\n"
12121 verifyFormat("class {\n"
12125 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
12126 verifyFormat("delete[] *ptr;", Style
);
12127 verifyFormat("delete[] **ptr;", Style
);
12128 verifyFormat("delete[] *(ptr);", Style
);
12130 verifyIndependentOfContext("MACRO(int *i);");
12131 verifyIndependentOfContext("MACRO(auto *a);");
12132 verifyIndependentOfContext("MACRO(const A *a);");
12133 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
12134 verifyIndependentOfContext("MACRO(decltype(A) *a);");
12135 verifyIndependentOfContext("MACRO(typeof(A) *a);");
12136 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
12137 verifyIndependentOfContext("MACRO(A *const a);");
12138 verifyIndependentOfContext("MACRO(A *restrict a);");
12139 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
12140 verifyIndependentOfContext("MACRO(A *__restrict a);");
12141 verifyIndependentOfContext("MACRO(A *volatile a);");
12142 verifyIndependentOfContext("MACRO(A *__volatile a);");
12143 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
12144 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
12145 verifyIndependentOfContext("MACRO(A *_Nullable a);");
12146 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
12147 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
12148 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
12149 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
12150 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
12151 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
12152 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
12153 verifyIndependentOfContext("MACRO(A *__capability);");
12154 verifyIndependentOfContext("MACRO(A &__capability);");
12155 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
12156 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
12157 // If we add __my_qualifier to AttributeMacros it should always be parsed as
12158 // a type declaration:
12159 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier
);
12160 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier
);
12161 // Also check that TypenameMacros prevents parsing it as multiplication:
12162 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
12163 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros
); // type
12165 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
12166 verifyFormat("void f() { f(float{1}, a * a); }");
12167 verifyFormat("void f() { f(float(1), a * a); }");
12169 verifyFormat("f((void (*)(int))g);");
12170 verifyFormat("f((void (&)(int))g);");
12171 verifyFormat("f((void (^)(int))g);");
12173 // FIXME: Is there a way to make this work?
12174 // verifyIndependentOfContext("MACRO(A *a);");
12175 verifyFormat("MACRO(A &B);");
12176 verifyFormat("MACRO(A *B);");
12177 verifyFormat("void f() { MACRO(A * B); }");
12178 verifyFormat("void f() { MACRO(A & B); }");
12180 // This lambda was mis-formatted after D88956 (treating it as a binop):
12181 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
12182 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
12183 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
12184 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
12186 verifyFormat("DatumHandle const *operator->() const { return input_; }");
12187 verifyFormat("return options != nullptr && operator==(*options);");
12189 verifyFormat("#define OP(x) \\\n"
12190 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12191 " return s << a.DebugString(); \\\n"
12193 "#define OP(x) \\\n"
12194 " ostream &operator<<(ostream &s, const A &a) { \\\n"
12195 " return s << a.DebugString(); \\\n"
12197 getLLVMStyleWithColumns(50));
12199 verifyFormat("#define FOO \\\n"
12200 " void foo() { \\\n"
12201 " operator+(a * b); \\\n"
12203 getLLVMStyleWithColumns(25));
12205 // FIXME: We cannot handle this case yet; we might be able to figure out that
12206 // foo<x> d > v; doesn't make sense.
12207 verifyFormat("foo<a<b && c> d> v;");
12209 FormatStyle PointerMiddle
= getLLVMStyle();
12210 PointerMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
12211 verifyFormat("delete *x;", PointerMiddle
);
12212 verifyFormat("int * x;", PointerMiddle
);
12213 verifyFormat("int *[] x;", PointerMiddle
);
12214 verifyFormat("template <int * y> f() {}", PointerMiddle
);
12215 verifyFormat("int * f(int * a) {}", PointerMiddle
);
12216 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle
);
12217 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle
);
12218 verifyFormat("A<int *> a;", PointerMiddle
);
12219 verifyFormat("A<int **> a;", PointerMiddle
);
12220 verifyFormat("A<int *, int *> a;", PointerMiddle
);
12221 verifyFormat("A<int *[]> a;", PointerMiddle
);
12222 verifyFormat("A = new SomeType *[Length]();", PointerMiddle
);
12223 verifyFormat("A = new SomeType *[Length];", PointerMiddle
);
12224 verifyFormat("T ** t = new T *;", PointerMiddle
);
12226 // Member function reference qualifiers aren't binary operators.
12227 verifyFormat("string // break\n"
12228 "operator()() & {}");
12229 verifyFormat("string // break\n"
12230 "operator()() && {}");
12231 verifyGoogleFormat("template <typename T>\n"
12232 "auto x() & -> int {}");
12234 // Should be binary operators when used as an argument expression (overloaded
12235 // operator invoked as a member function).
12236 verifyFormat("void f() { a.operator()(a * a); }");
12237 verifyFormat("void f() { a->operator()(a & a); }");
12238 verifyFormat("void f() { a.operator()(*a & *a); }");
12239 verifyFormat("void f() { a->operator()(*a * *a); }");
12241 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
12242 verifyFormat("int operator()(T (&)[N]) { return 0; }");
12244 verifyFormat("val1 & val2;");
12245 verifyFormat("val1 & val2 & val3;");
12246 verifyFormat("class c {\n"
12247 " void func(type &a) { a & member; }\n"
12248 " anotherType &member;\n"
12252 TEST_F(FormatTest
, UnderstandsAttributes
) {
12253 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
12254 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
12255 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12256 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
12257 FormatStyle AfterType
= getLLVMStyle();
12258 AfterType
.BreakAfterReturnType
= FormatStyle::RTBS_All
;
12259 verifyFormat("__attribute__((nodebug)) void\n"
12262 verifyFormat("__unused void\n"
12266 FormatStyle CustomAttrs
= getLLVMStyle();
12267 CustomAttrs
.AttributeMacros
.push_back("__unused");
12268 CustomAttrs
.AttributeMacros
.push_back("__attr1");
12269 CustomAttrs
.AttributeMacros
.push_back("__attr2");
12270 CustomAttrs
.AttributeMacros
.push_back("no_underscore_attr");
12271 verifyFormat("vector<SomeType *__attribute((foo))> v;");
12272 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
12273 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
12274 // Check that it is parsed as a multiplication without AttributeMacros and
12275 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
12276 verifyFormat("vector<SomeType * __attr1> v;");
12277 verifyFormat("vector<SomeType __attr1 *> v;");
12278 verifyFormat("vector<SomeType __attr1 *const> v;");
12279 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
12280 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs
);
12281 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs
);
12282 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs
);
12283 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs
);
12284 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs
);
12285 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs
);
12286 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs
);
12287 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs
);
12288 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs
);
12289 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs
);
12291 // Check that these are not parsed as function declarations:
12292 CustomAttrs
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
12293 CustomAttrs
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
12294 verifyFormat("SomeType s(InitValue);", CustomAttrs
);
12295 verifyFormat("SomeType s{InitValue};", CustomAttrs
);
12296 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs
);
12297 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs
);
12298 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs
);
12299 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs
);
12300 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs
);
12301 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs
);
12304 TEST_F(FormatTest
, UnderstandsPointerQualifiersInCast
) {
12305 // Check that qualifiers on pointers don't break parsing of casts.
12306 verifyFormat("x = (foo *const)*v;");
12307 verifyFormat("x = (foo *volatile)*v;");
12308 verifyFormat("x = (foo *restrict)*v;");
12309 verifyFormat("x = (foo *__attribute__((foo)))*v;");
12310 verifyFormat("x = (foo *_Nonnull)*v;");
12311 verifyFormat("x = (foo *_Nullable)*v;");
12312 verifyFormat("x = (foo *_Null_unspecified)*v;");
12313 verifyFormat("x = (foo *_Nonnull)*v;");
12314 verifyFormat("x = (foo *[[clang::attr]])*v;");
12315 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
12316 verifyFormat("x = (foo *__ptr32)*v;");
12317 verifyFormat("x = (foo *__ptr64)*v;");
12318 verifyFormat("x = (foo *__capability)*v;");
12320 // Check that we handle multiple trailing qualifiers and skip them all to
12321 // determine that the expression is a cast to a pointer type.
12322 FormatStyle LongPointerRight
= getLLVMStyleWithColumns(999);
12323 FormatStyle LongPointerLeft
= getLLVMStyleWithColumns(999);
12324 LongPointerLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
12325 StringRef AllQualifiers
=
12326 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
12327 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
12328 verifyFormat(("x = (foo *" + AllQualifiers
+ ")*v;").str(), LongPointerRight
);
12329 verifyFormat(("x = (foo* " + AllQualifiers
+ ")*v;").str(), LongPointerLeft
);
12331 // Also check that address-of is not parsed as a binary bitwise-and:
12332 verifyFormat("x = (foo *const)&v;");
12333 verifyFormat(("x = (foo *" + AllQualifiers
+ ")&v;").str(), LongPointerRight
);
12334 verifyFormat(("x = (foo* " + AllQualifiers
+ ")&v;").str(), LongPointerLeft
);
12336 // Check custom qualifiers:
12337 FormatStyle CustomQualifier
= getLLVMStyleWithColumns(999);
12338 CustomQualifier
.AttributeMacros
.push_back("__my_qualifier");
12339 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
12340 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier
);
12341 verifyFormat(("x = (foo *" + AllQualifiers
+ " __my_qualifier)*v;").str(),
12343 verifyFormat(("x = (foo *" + AllQualifiers
+ " __my_qualifier)&v;").str(),
12346 // Check that unknown identifiers result in binary operator parsing:
12347 verifyFormat("x = (foo * __unknown_qualifier) * v;");
12348 verifyFormat("x = (foo * __unknown_qualifier) & v;");
12351 TEST_F(FormatTest
, UnderstandsSquareAttributes
) {
12352 verifyFormat("SomeType s [[unused]] (InitValue);");
12353 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
12354 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
12355 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
12356 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;");
12357 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
12358 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12359 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
12360 verifyFormat("[[nodiscard]] bool f() { return false; }");
12361 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
12362 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
12363 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
12364 verifyFormat("[[nodiscard]] ::qualified_type f();");
12366 // Make sure we do not mistake attributes for array subscripts.
12367 verifyFormat("int a() {}\n"
12368 "[[unused]] int b() {}");
12369 verifyFormat("NSArray *arr;\n"
12370 "arr[[Foo() bar]];");
12372 // On the other hand, we still need to correctly find array subscripts.
12373 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
12375 // Make sure that we do not mistake Objective-C method inside array literals
12376 // as attributes, even if those method names are also keywords.
12377 verifyFormat("@[ [foo bar] ];");
12378 verifyFormat("@[ [NSArray class] ];");
12379 verifyFormat("@[ [foo enum] ];");
12381 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
12383 // Make sure we do not parse attributes as lambda introducers.
12384 FormatStyle MultiLineFunctions
= getLLVMStyle();
12385 MultiLineFunctions
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
12386 verifyFormat("[[unused]] int b() {\n"
12389 MultiLineFunctions
);
12392 TEST_F(FormatTest
, AttributeClass
) {
12393 FormatStyle Style
= getChromiumStyle(FormatStyle::LK_Cpp
);
12394 verifyFormat("class S {\n"
12395 " S(S&&) = default;\n"
12398 verifyFormat("class [[nodiscard]] S {\n"
12399 " S(S&&) = default;\n"
12402 verifyFormat("class __attribute((maybeunused)) S {\n"
12403 " S(S&&) = default;\n"
12406 verifyFormat("struct S {\n"
12407 " S(S&&) = default;\n"
12410 verifyFormat("struct [[nodiscard]] S {\n"
12411 " S(S&&) = default;\n"
12416 TEST_F(FormatTest
, AttributesAfterMacro
) {
12417 FormatStyle Style
= getLLVMStyle();
12418 verifyFormat("MACRO;\n"
12419 "__attribute__((maybe_unused)) int foo() {\n"
12423 verifyFormat("MACRO;\n"
12424 "[[nodiscard]] int foo() {\n"
12428 verifyNoChange("MACRO\n\n"
12429 "__attribute__((maybe_unused)) int foo() {\n"
12433 verifyNoChange("MACRO\n\n"
12434 "[[nodiscard]] int foo() {\n"
12439 TEST_F(FormatTest
, AttributePenaltyBreaking
) {
12440 FormatStyle Style
= getLLVMStyle();
12441 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
12442 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12444 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
12445 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
12447 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
12448 "shared_ptr<ALongTypeName> &C d) {\n}",
12452 TEST_F(FormatTest
, UnderstandsEllipsis
) {
12453 FormatStyle Style
= getLLVMStyle();
12454 verifyFormat("int printf(const char *fmt, ...);");
12455 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
12456 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
12458 verifyFormat("template <int *...PP> a;", Style
);
12460 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
12461 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style
);
12463 verifyFormat("template <int*... PP> a;", Style
);
12465 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
12466 verifyFormat("template <int *... PP> a;", Style
);
12469 TEST_F(FormatTest
, AdaptivelyFormatsPointersAndReferences
) {
12470 verifyFormat("int *a;\n"
12477 verifyFormat("int* a;\n"
12484 verifyFormat("int *a;\n"
12491 verifyFormat("auto x = [] {\n"
12496 "auto x=[]{int *a;\n"
12502 TEST_F(FormatTest
, UnderstandsRvalueReferences
) {
12503 verifyFormat("int f(int &&a) {}");
12504 verifyFormat("int f(int a, char &&b) {}");
12505 verifyFormat("void f() { int &&a = b; }");
12506 verifyGoogleFormat("int f(int a, char&& b) {}");
12507 verifyGoogleFormat("void f() { int&& a = b; }");
12509 verifyIndependentOfContext("A<int &&> a;");
12510 verifyIndependentOfContext("A<int &&, int &&> a;");
12511 verifyGoogleFormat("A<int&&> a;");
12512 verifyGoogleFormat("A<int&&, int&&> a;");
12514 // Not rvalue references:
12515 verifyFormat("template <bool B, bool C> class A {\n"
12516 " static_assert(B && C, \"Something is wrong\");\n"
12518 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
12519 verifyFormat("template <typename T> struct S {\n"
12520 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
12522 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
12523 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
12524 verifyFormat("#define A(a, b) (a && b)");
12527 TEST_F(FormatTest
, FormatsBinaryOperatorsPrecedingEquals
) {
12528 verifyFormat("void f() {\n"
12532 getLLVMStyleWithColumns(15));
12535 TEST_F(FormatTest
, FormatsCasts
) {
12536 verifyFormat("Type *A = static_cast<Type *>(P);");
12537 verifyFormat("static_cast<Type *>(P);");
12538 verifyFormat("static_cast<Type &>(Fun)(Args);");
12539 verifyFormat("static_cast<Type &>(*Fun)(Args);");
12540 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
12541 // Check that static_cast<...>(...) does not require the next token to be on
12543 verifyFormat("some_loooong_output << something_something__ << "
12544 "static_cast<const void *>(R)\n"
12546 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
12547 verifyFormat("const_cast<Type &>(*Fun)(Args);");
12548 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
12549 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
12550 verifyFormat("Type *A = (Type *)P;");
12551 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12552 verifyFormat("int a = (int)(2.0f);");
12553 verifyFormat("int a = (int)2.0f;");
12554 verifyFormat("x[(int32)y];");
12555 verifyFormat("x = (int32)y;");
12556 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12557 verifyFormat("int a = (int)*b;");
12558 verifyFormat("int a = (int)2.0f;");
12559 verifyFormat("int a = (int)~0;");
12560 verifyFormat("int a = (int)++a;");
12561 verifyFormat("int a = (int)sizeof(int);");
12562 verifyFormat("int a = (int)+2;");
12563 verifyFormat("my_int a = (my_int)2.0f;");
12564 verifyFormat("my_int a = (my_int)sizeof(int);");
12565 verifyFormat("return (my_int)aaa;");
12566 verifyFormat("throw (my_int)aaa;");
12567 verifyFormat("#define x ((int)-1)");
12568 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12569 verifyFormat("#define p(q) ((int *)&q)");
12570 verifyFormat("fn(a)(b) + 1;");
12572 verifyFormat("void f() { my_int a = (my_int)*b; }");
12573 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12574 verifyFormat("my_int a = (my_int)~0;");
12575 verifyFormat("my_int a = (my_int)++a;");
12576 verifyFormat("my_int a = (my_int)-2;");
12577 verifyFormat("my_int a = (my_int)1;");
12578 verifyFormat("my_int a = (my_int *)1;");
12579 verifyFormat("my_int a = (const my_int)-1;");
12580 verifyFormat("my_int a = (const my_int *)-1;");
12581 verifyFormat("my_int a = (my_int)(my_int)-1;");
12582 verifyFormat("my_int a = (ns::my_int)-2;");
12583 verifyFormat("case (my_int)ONE:");
12584 verifyFormat("auto x = (X)this;");
12585 // Casts in Obj-C style calls used to not be recognized as such.
12586 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12588 // FIXME: single value wrapped with paren will be treated as cast.
12589 verifyFormat("void f(int i = (kValue)*kMask) {}");
12595 // Don't break after a cast's
12596 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12597 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12598 " bbbbbbbbbbbbbbbbbbbbbb);");
12600 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12601 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12602 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12603 verifyFormat("bool *y = (bool *)(void *)(x);");
12604 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12605 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12606 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12607 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12609 // These are not casts.
12610 verifyFormat("void f(int *) {}");
12611 verifyFormat("f(foo)->b;");
12612 verifyFormat("f(foo).b;");
12613 verifyFormat("f(foo)(b);");
12614 verifyFormat("f(foo)[b];");
12615 verifyFormat("[](foo) { return 4; }(bar);");
12616 verifyFormat("(*funptr)(foo)[4];");
12617 verifyFormat("funptrs[4](foo)[4];");
12618 verifyFormat("void f(int *);");
12619 verifyFormat("void f(int *) = 0;");
12620 verifyFormat("void f(SmallVector<int>) {}");
12621 verifyFormat("void f(SmallVector<int>);");
12622 verifyFormat("void f(SmallVector<int>) = 0;");
12623 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12624 verifyFormat("int a = sizeof(int) * b;");
12625 verifyGoogleFormat("int a = alignof(int) * b;");
12626 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12627 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12628 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12630 // These are not casts, but at some point were confused with casts.
12631 verifyFormat("virtual void foo(int *) override;");
12632 verifyFormat("virtual void foo(char &) const;");
12633 verifyFormat("virtual void foo(int *a, char *) const;");
12634 verifyFormat("int a = sizeof(int *) + b;");
12635 verifyGoogleFormat("int a = alignof(int *) + b;");
12636 verifyFormat("bool b = f(g<int>) && c;");
12637 verifyFormat("typedef void (*f)(int i) func;");
12638 verifyFormat("void operator++(int) noexcept;");
12639 verifyFormat("void operator++(int &) noexcept;");
12640 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12643 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12644 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12645 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12646 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12647 verifyFormat("void operator delete(foo &) noexcept;");
12648 verifyFormat("void operator delete(foo) noexcept;");
12649 verifyFormat("void operator delete(int) noexcept;");
12650 verifyFormat("void operator delete(int &) noexcept;");
12651 verifyFormat("void operator delete(int &) volatile noexcept;");
12652 verifyFormat("void operator delete(int &) const");
12653 verifyFormat("void operator delete(int &) = default");
12654 verifyFormat("void operator delete(int &) = delete");
12655 verifyFormat("void operator delete(int &) [[noreturn]]");
12656 verifyFormat("void operator delete(int &) throw();");
12657 verifyFormat("void operator delete(int &) throw(int);");
12658 verifyFormat("auto operator delete(int &) -> int;");
12659 verifyFormat("auto operator delete(int &) override");
12660 verifyFormat("auto operator delete(int &) final");
12662 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12663 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12664 // FIXME: The indentation here is not ideal.
12666 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12667 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12668 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12671 TEST_F(FormatTest
, FormatsFunctionTypes
) {
12672 verifyFormat("A<bool()> a;");
12673 verifyFormat("A<SomeType()> a;");
12674 verifyFormat("A<void (*)(int, std::string)> a;");
12675 verifyFormat("A<void *(int)>;");
12676 verifyFormat("void *(*a)(int *, SomeType *);");
12677 verifyFormat("int (*func)(void *);");
12678 verifyFormat("void f() { int (*func)(void *); }");
12679 verifyFormat("template <class CallbackClass>\n"
12680 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12682 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12683 verifyGoogleFormat("void* (*a)(int);");
12684 verifyGoogleFormat(
12685 "template <class CallbackClass>\n"
12686 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12688 // Other constructs can look somewhat like function types:
12689 verifyFormat("A<sizeof(*x)> a;");
12690 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12691 verifyFormat("some_var = function(*some_pointer_var)[0];");
12692 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12693 verifyFormat("int x = f(&h)();");
12694 verifyFormat("returnsFunction(¶m1, ¶m2)(param);");
12695 verifyFormat("std::function<\n"
12696 " LooooooooooongTemplatedType<\n"
12698 " LooooooooooooooooongType type)>\n"
12700 getGoogleStyleWithColumns(40));
12703 TEST_F(FormatTest
, FormatsPointersToArrayTypes
) {
12704 verifyFormat("A (*foo_)[6];");
12705 verifyFormat("vector<int> (*foo_)[6];");
12708 TEST_F(FormatTest
, BreaksLongVariableDeclarations
) {
12709 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12710 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12711 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12712 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12713 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12714 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12716 // Different ways of ()-initializiation.
12717 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12718 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12719 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12720 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12721 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12722 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12723 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12724 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12726 // Lambdas should not confuse the variable declaration heuristic.
12727 verifyFormat("LooooooooooooooooongType\n"
12728 " variable(nullptr, [](A *a) {});",
12729 getLLVMStyleWithColumns(40));
12732 TEST_F(FormatTest
, BreaksLongDeclarations
) {
12733 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12734 " AnotherNameForTheLongType;");
12735 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12737 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12738 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12739 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12740 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12741 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12742 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12743 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12744 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12745 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12746 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12747 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12748 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12749 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12750 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12751 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12752 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12753 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12754 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12755 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12756 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12757 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12758 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12759 FormatStyle Indented
= getLLVMStyle();
12760 Indented
.IndentWrappedFunctionNames
= true;
12761 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12762 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12765 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12766 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12769 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12770 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12773 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12774 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12777 // FIXME: Without the comment, this breaks after "(".
12778 verifyGoogleFormat(
12779 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12780 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12782 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12783 " int LoooooooooooooooooooongParam2) {}");
12785 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12786 " SourceLocation L, IdentifierIn *II,\n"
12788 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12789 "ReallyReaaallyLongFunctionName(\n"
12790 " const std::string &SomeParameter,\n"
12791 " const SomeType<string, SomeOtherTemplateParameter>\n"
12792 " &ReallyReallyLongParameterName,\n"
12793 " const SomeType<string, SomeOtherTemplateParameter>\n"
12794 " &AnotherLongParameterName) {}");
12795 verifyFormat("template <typename A>\n"
12796 "SomeLoooooooooooooooooooooongType<\n"
12797 " typename some_namespace::SomeOtherType<A>::Type>\n"
12800 verifyGoogleFormat(
12801 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12802 " aaaaaaaaaaaaaaaaaaaaaaa;");
12803 verifyGoogleFormat(
12804 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12805 " SourceLocation L) {}");
12806 verifyGoogleFormat(
12807 "some_namespace::LongReturnType\n"
12808 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12809 " int first_long_parameter, int second_parameter) {}");
12811 verifyGoogleFormat("template <typename T>\n"
12812 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12813 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12814 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12815 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12817 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12818 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12819 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12820 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12821 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12822 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12823 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12824 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12828 verifyFormat("template <typename T> // Templates on own line.\n"
12829 "static int // Some comment.\n"
12830 "MyFunction(int a);");
12833 TEST_F(FormatTest
, FormatsAccessModifiers
) {
12834 FormatStyle Style
= getLLVMStyle();
12835 EXPECT_EQ(Style
.EmptyLineBeforeAccessModifier
,
12836 FormatStyle::ELBAMS_LogicalBlock
);
12837 verifyFormat("struct foo {\n"
12848 verifyFormat("struct foo {\n"
12867 verifyFormat("struct foo { /* comment */\n"
12875 verifyFormat("struct foo {\n"
12886 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
12887 verifyFormat("struct foo {\n"
12896 verifyFormat("struct foo {\n"
12916 verifyFormat("struct foo { /* comment */\n"
12923 "struct foo { /* comment */\n"
12933 verifyFormat("struct foo {\n"
12956 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
12957 verifyFormat("struct foo {\n"
12968 verifyFormat("struct foo {\n"
12987 verifyFormat("struct foo { /* comment */\n"
12996 verifyFormat("struct foo {\n"
13019 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13020 verifyNoChange("struct foo {\n"
13032 verifyFormat("struct foo {\n"
13041 verifyNoChange("struct foo { /* comment */\n"
13051 verifyFormat("struct foo { /* comment */\n"
13059 verifyNoChange("struct foo {\n"
13072 verifyFormat("struct foo {\n"
13083 Style
.AttributeMacros
.push_back("FOO");
13084 Style
.AttributeMacros
.push_back("BAR");
13085 verifyFormat("struct foo {\n"
13088 "BAR(x) protected:\n"
13093 FormatStyle NoEmptyLines
= getLLVMStyle();
13094 NoEmptyLines
.MaxEmptyLinesToKeep
= 0;
13095 verifyFormat("struct foo {\n"
13108 NoEmptyLines
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13109 verifyFormat("struct foo {\n"
13120 NoEmptyLines
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13121 verifyFormat("struct foo {\n"
13136 TEST_F(FormatTest
, FormatsAfterAccessModifiers
) {
13138 FormatStyle Style
= getLLVMStyle();
13139 EXPECT_EQ(Style
.EmptyLineAfterAccessModifier
, FormatStyle::ELAAMS_Never
);
13140 verifyFormat("struct foo {\n"
13152 // Check if lines are removed.
13153 verifyFormat("struct foo {\n"
13178 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13179 verifyFormat("struct foo {\n"
13194 // Check if lines are added.
13195 verifyFormat("struct foo {\n"
13220 // Leave tests rely on the code layout, test::messUp can not be used.
13221 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13222 Style
.MaxEmptyLinesToKeep
= 0u;
13223 verifyFormat("struct foo {\n"
13235 // Check if MaxEmptyLinesToKeep is respected.
13236 verifyFormat("struct foo {\n"
13261 Style
.MaxEmptyLinesToKeep
= 1u;
13262 verifyNoChange("struct foo {\n"
13276 // Check if no lines are kept.
13277 verifyFormat("struct foo {\n"
13288 // Check if MaxEmptyLinesToKeep is respected.
13289 verifyFormat("struct foo {\n"
13317 Style
.MaxEmptyLinesToKeep
= 10u;
13318 verifyNoChange("struct foo {\n"
13333 // Test with comments.
13334 Style
= getLLVMStyle();
13335 verifyFormat("struct foo {\n"
13340 "private: /* comment */\n"
13344 verifyFormat("struct foo {\n"
13349 "private: /* comment */\n"
13358 "private: /* comment */\n"
13364 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13365 verifyFormat("struct foo {\n"
13371 "private: /* comment */\n"
13380 "private: /* comment */\n"
13384 verifyFormat("struct foo {\n"
13390 "private: /* comment */\n"
13396 // Test with preprocessor defines.
13397 Style
= getLLVMStyle();
13398 verifyFormat("struct foo {\n"
13405 verifyFormat("struct foo {\n"
13419 verifyNoChange("struct foo {\n"
13427 verifyFormat("struct foo {\n"
13443 verifyFormat("struct foo {\n"
13458 verifyFormat("struct foo {\n"
13478 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13479 verifyFormat("struct foo {\n"
13493 verifyFormat("struct foo {\n"
13503 TEST_F(FormatTest
, FormatsAfterAndBeforeAccessModifiersInteraction
) {
13504 // Combined tests of EmptyLineAfterAccessModifier and
13505 // EmptyLineBeforeAccessModifier.
13506 FormatStyle Style
= getLLVMStyle();
13507 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13508 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13509 verifyFormat("struct foo {\n"
13516 Style
.MaxEmptyLinesToKeep
= 10u;
13517 // Both remove all new lines.
13518 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13519 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13520 verifyFormat("struct foo {\n"
13531 // Leave tests rely on the code layout, test::messUp can not be used.
13532 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13533 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13534 Style
.MaxEmptyLinesToKeep
= 10u;
13535 verifyNoChange("struct foo {\n"
13541 Style
.MaxEmptyLinesToKeep
= 3u;
13542 verifyNoChange("struct foo {\n"
13548 Style
.MaxEmptyLinesToKeep
= 1u;
13549 verifyNoChange("struct foo {\n"
13554 Style
); // Based on new lines in original document and not
13557 Style
.MaxEmptyLinesToKeep
= 10u;
13558 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13559 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13560 // Newlines are kept if they are greater than zero,
13561 // test::messUp removes all new lines which changes the logic
13562 verifyNoChange("struct foo {\n"
13569 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13570 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13571 // test::messUp removes all new lines which changes the logic
13572 verifyNoChange("struct foo {\n"
13579 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13580 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13581 verifyNoChange("struct foo {\n"
13586 Style
); // test::messUp removes all new lines which changes
13589 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13590 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13591 verifyFormat("struct foo {\n"
13602 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13603 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13604 verifyNoChange("struct foo {\n"
13609 Style
); // test::messUp removes all new lines which changes
13612 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13613 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13614 verifyFormat("struct foo {\n"
13625 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13626 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13627 verifyFormat("struct foo {\n"
13638 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13639 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13640 verifyFormat("struct foo {\n"
13651 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13652 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13653 verifyFormat("struct foo {\n"
13665 TEST_F(FormatTest
, FormatsArrays
) {
13666 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13667 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13669 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13670 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13671 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13673 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13674 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13675 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13676 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13677 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13678 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13680 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13681 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13682 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13684 " .aaaaaaaaaaaaaaaaaaaaaa();");
13686 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13689 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13691 " .aaaaaaaaaaaaaaaaaaaaaa();");
13692 verifyFormat("a[::b::c];");
13694 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13696 FormatStyle NoColumnLimit
= getLLVMStyleWithColumns(0);
13697 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit
);
13700 TEST_F(FormatTest
, LineStartsWithSpecialCharacter
) {
13701 verifyFormat("(a)->b();");
13702 verifyFormat("--a;");
13705 TEST_F(FormatTest
, HandlesIncludeDirectives
) {
13706 verifyFormat("#include <string>\n"
13707 "#include <a/b/c.h>\n"
13708 "#include \"a/b/string\"\n"
13709 "#include \"string.h\"\n"
13710 "#include \"string.h\"\n"
13712 "#include < path with space >\n"
13713 "#include_next <test.h>"
13714 "#include \"abc.h\" // this is included for ABC\n"
13715 "#include \"some long include\" // with a comment\n"
13716 "#include \"some very long include path\"\n"
13717 "#include <some/very/long/include/path>",
13718 getLLVMStyleWithColumns(35));
13719 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13720 verifyFormat("#include <a>", "#include<a>");
13722 verifyFormat("#import <string>");
13723 verifyFormat("#import <a/b/c.h>");
13724 verifyFormat("#import \"a/b/string\"");
13725 verifyFormat("#import \"string.h\"");
13726 verifyFormat("#import \"string.h\"");
13727 verifyFormat("#if __has_include(<strstream>)\n"
13728 "#include <strstream>\n"
13731 verifyFormat("#define MY_IMPORT <a/b>");
13733 verifyFormat("#if __has_include(<a/b>)");
13734 verifyFormat("#if __has_include_next(<a/b>)");
13735 verifyFormat("#define F __has_include(<a/b>)");
13736 verifyFormat("#define F __has_include_next(<a/b>)");
13738 // Protocol buffer definition or missing "#".
13739 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13740 getLLVMStyleWithColumns(30));
13742 FormatStyle Style
= getLLVMStyle();
13743 Style
.AlwaysBreakBeforeMultilineStrings
= true;
13744 Style
.ColumnLimit
= 0;
13745 verifyFormat("#import \"abc.h\"", Style
);
13747 // But 'import' might also be a regular C++ namespace.
13748 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13750 verifyFormat("import::Bar foo(val ? 2 : 1);");
13753 //===----------------------------------------------------------------------===//
13754 // Error recovery tests.
13755 //===----------------------------------------------------------------------===//
13757 TEST_F(FormatTest
, IncompleteParameterLists
) {
13758 FormatStyle NoBinPacking
= getLLVMStyle();
13759 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
13760 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13761 " double *min_x,\n"
13762 " double *max_x,\n"
13763 " double *min_y,\n"
13764 " double *max_y,\n"
13765 " double *min_z,\n"
13766 " double *max_z, ) {}",
13770 TEST_F(FormatTest
, IncorrectCodeTrailingStuff
) {
13771 verifyFormat("void f() { return; }\n42");
13772 verifyFormat("void f() {\n"
13777 verifyFormat("void f() { return }\n42");
13778 verifyFormat("void f() {\n"
13785 TEST_F(FormatTest
, IncorrectCodeMissingSemicolon
) {
13786 verifyFormat("void f() { return }", "void f ( ) { return }");
13787 verifyFormat("void f() {\n"
13791 "void f ( ) { if ( a ) return }");
13792 verifyFormat("namespace N {\n"
13795 "namespace N { void f() }");
13796 verifyFormat("namespace N {\n"
13799 "} // namespace N",
13800 "namespace N { void f( ) { } void g( ) }");
13803 TEST_F(FormatTest
, IndentationWithinColumnLimitNotPossible
) {
13804 verifyFormat("int aaaaaaaa =\n"
13805 " // Overlylongcomment\n"
13807 getLLVMStyleWithColumns(20));
13808 verifyFormat("function(\n"
13809 " ShortArgument,\n"
13810 " LoooooooooooongArgument);",
13811 getLLVMStyleWithColumns(20));
13814 TEST_F(FormatTest
, IncorrectAccessSpecifier
) {
13815 verifyFormat("public:");
13816 verifyFormat("class A {\n"
13820 verifyFormat("public\n"
13822 verifyFormat("public\n"
13824 verifyFormat("public\n"
13827 verifyFormat("public\n"
13831 TEST_F(FormatTest
, IncorrectCodeUnbalancedBraces
) {
13833 verifyFormat("#})");
13834 verifyNoCrash("(/**/[:!] ?[).");
13835 verifyNoCrash("struct X {\n"
13836 " operator iunt(\n"
13838 verifyNoCrash("struct Foo {\n"
13839 " operator foo(bar\n"
13843 TEST_F(FormatTest
, IncorrectUnbalancedBracesInMacrosWithUnicode
) {
13844 // Found by oss-fuzz:
13845 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13846 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Cpp
);
13847 Style
.ColumnLimit
= 60;
13849 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13850 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13851 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13855 TEST_F(FormatTest
, IncorrectCodeDoNoWhile
) {
13856 verifyFormat("do {\n}");
13857 verifyFormat("do {\n}\n"
13859 verifyFormat("do {\n}\n"
13861 verifyFormat("do {\n"
13866 TEST_F(FormatTest
, IncorrectCodeMissingParens
) {
13867 verifyFormat("if {\n foo;\n foo();\n}");
13868 verifyFormat("switch {\n foo;\n foo();\n}");
13869 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13870 verifyIncompleteFormat("ERROR: for target;");
13871 verifyFormat("while {\n foo;\n foo();\n}");
13872 verifyFormat("do {\n foo;\n foo();\n} while;");
13875 TEST_F(FormatTest
, DoesNotTouchUnwrappedLinesWithErrors
) {
13876 verifyIncompleteFormat("namespace {\n"
13877 "class Foo { Foo (\n"
13882 TEST_F(FormatTest
, IncorrectCodeErrorDetection
) {
13918 getLLVMStyleWithColumns(10));
13921 TEST_F(FormatTest
, LayoutCallsInsideBraceInitializers
) {
13922 verifyFormat("int x = {\n"
13924 " b(alongervariable)};",
13925 getLLVMStyleWithColumns(25));
13928 TEST_F(FormatTest
, LayoutBraceInitializersInReturnStatement
) {
13929 verifyFormat("return (a)(b){1, 2, 3};");
13932 TEST_F(FormatTest
, LayoutCxx11BraceInitializers
) {
13933 verifyFormat("vector<int> x{1, 2, 3, 4};");
13934 verifyFormat("vector<int> x{\n"
13940 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13941 verifyFormat("f({1, 2});");
13942 verifyFormat("auto v = Foo{-1};");
13943 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13944 verifyFormat("Class::Class : member{1, 2, 3} {}");
13945 verifyFormat("new vector<int>{1, 2, 3};");
13946 verifyFormat("new int[3]{1, 2, 3};");
13947 verifyFormat("new int{1};");
13948 verifyFormat("return {arg1, arg2};");
13949 verifyFormat("return {arg1, SomeType{parameter}};");
13950 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13951 verifyFormat("new T{arg1, arg2};");
13952 verifyFormat("f(MyMap[{composite, key}]);");
13953 verifyFormat("class Class {\n"
13954 " T member = {arg1, arg2};\n"
13956 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13957 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13958 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13959 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13960 verifyFormat("int a = std::is_integral<int>{} + 0;");
13962 verifyFormat("int foo(int i) { return fo1{}(i); }");
13963 verifyFormat("int foo(int i) { return fo1{}(i); }");
13964 verifyFormat("auto i = decltype(x){};");
13965 verifyFormat("auto i = typeof(x){};");
13966 verifyFormat("auto i = _Atomic(x){};");
13967 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13968 verifyFormat("Node n{1, Node{1000}, //\n"
13970 verifyFormat("Aaaa aaaaaaa{\n"
13975 verifyFormat("class C : public D {\n"
13976 " SomeClass SC{2};\n"
13978 verifyFormat("class C : public A {\n"
13979 " class D : public B {\n"
13980 " void f() { int i{2}; }\n"
13983 verifyFormat("#define A {a, a},");
13984 // Don't confuse braced list initializers with compound statements.
13988 " A() : Base<int>{} {}\n"
13989 " A() : Base<Foo<int>>{} {}\n"
13990 " A(int b) : b(b) {}\n"
13991 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13993 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13994 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13998 // Avoid breaking between equal sign and opening brace
13999 FormatStyle AvoidBreakingFirstArgument
= getLLVMStyle();
14000 AvoidBreakingFirstArgument
.PenaltyBreakBeforeFirstCallParameter
= 200;
14001 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
14002 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
14003 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
14004 " {\"ccccccccccccccccccccc\", 2}};",
14005 AvoidBreakingFirstArgument
);
14007 // Binpacking only if there is no trailing comma
14008 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
14009 " cccccccccc, dddddddddd};",
14010 getLLVMStyleWithColumns(50));
14011 verifyFormat("const Aaaaaa aaaaa = {\n"
14017 getLLVMStyleWithColumns(50));
14019 // Cases where distinguising braced lists and blocks is hard.
14020 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
14021 verifyFormat("void f() {\n"
14022 " return; // comment\n"
14025 verifyFormat("void f() {\n"
14032 // In combination with BinPackArguments = false.
14033 FormatStyle NoBinPacking
= getLLVMStyle();
14034 NoBinPacking
.BinPackArguments
= false;
14035 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
14047 verifyFormat("const Aaaaaa aaaaa = {\n"
14062 "const Aaaaaa aaaaa = {\n"
14063 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
14064 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
14065 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
14069 NoBinPacking
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
14070 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
14071 " CDDDP83848_BMCR_REGISTER,\n"
14072 " CDDDP83848_BMSR_REGISTER,\n"
14073 " CDDDP83848_RBR_REGISTER};",
14074 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
14075 " CDDDP83848_BMSR_REGISTER,\n"
14076 " CDDDP83848_RBR_REGISTER};",
14079 // FIXME: The alignment of these trailing comments might be bad. Then again,
14080 // this might be utterly useless in real code.
14081 verifyFormat("Constructor::Constructor()\n"
14082 " : some_value{ //\n"
14086 // In braced lists, the first comment is always assumed to belong to the
14087 // first element. Thus, it can be moved to the next or previous line as
14089 verifyFormat("function({// First element:\n"
14091 " // Second element:\n"
14094 " // First element:\n"
14096 " // Second element:\n"
14098 verifyFormat("std::vector<int> MyNumbers{\n"
14099 " // First element:\n"
14101 " // Second element:\n"
14103 "std::vector<int> MyNumbers{// First element:\n"
14105 " // Second element:\n"
14107 getLLVMStyleWithColumns(30));
14108 // A trailing comma should still lead to an enforced line break and no
14110 verifyFormat("vector<int> SomeVector = {\n"
14115 "vector<int> SomeVector = { // aaa\n"
14118 // C++11 brace initializer list l-braces should not be treated any differently
14119 // when breaking before lambda bodies is enabled
14120 FormatStyle BreakBeforeLambdaBody
= getLLVMStyle();
14121 BreakBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14122 BreakBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
14123 BreakBeforeLambdaBody
.AlwaysBreakBeforeMultilineStrings
= true;
14125 "std::runtime_error{\n"
14126 " \"Long string which will force a break onto the next line...\"};",
14127 BreakBeforeLambdaBody
);
14129 FormatStyle ExtraSpaces
= getLLVMStyle();
14130 ExtraSpaces
.Cpp11BracedListStyle
= false;
14131 ExtraSpaces
.ColumnLimit
= 75;
14132 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces
);
14133 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces
);
14134 verifyFormat("f({ 1, 2 });", ExtraSpaces
);
14135 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces
);
14136 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces
);
14137 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces
);
14138 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces
);
14139 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces
);
14140 verifyFormat("return { arg1, arg2 };", ExtraSpaces
);
14141 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces
);
14142 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces
);
14143 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces
);
14144 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces
);
14145 verifyFormat("class Class {\n"
14146 " T member = { arg1, arg2 };\n"
14150 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14151 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
14152 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
14153 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
14155 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces
);
14156 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
14159 "someFunction(OtherParam,\n"
14160 " BracedList{ // comment 1 (Forcing interesting break)\n"
14161 " param1, param2,\n"
14163 " param3, param4 });",
14166 "std::this_thread::sleep_for(\n"
14167 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
14169 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
14173 " aaaaaaaaaaaaaaa,\n"
14177 " aaaaaaaaaaaaaaaaaaaaa,\n"
14179 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
14182 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces
);
14183 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces
);
14184 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces
);
14186 // Avoid breaking between initializer/equal sign and opening brace
14187 ExtraSpaces
.PenaltyBreakBeforeFirstCallParameter
= 200;
14188 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
14189 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14190 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14191 " { \"ccccccccccccccccccccc\", 2 }\n"
14194 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
14195 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
14196 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
14197 " { \"ccccccccccccccccccccc\", 2 }\n"
14201 FormatStyle SpaceBeforeBrace
= getLLVMStyle();
14202 SpaceBeforeBrace
.SpaceBeforeCpp11BracedList
= true;
14203 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace
);
14204 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace
);
14206 FormatStyle SpaceBetweenBraces
= getLLVMStyle();
14207 SpaceBetweenBraces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
14208 SpaceBetweenBraces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
14209 SpaceBetweenBraces
.SpacesInParensOptions
.Other
= true;
14210 SpaceBetweenBraces
.SpacesInSquareBrackets
= true;
14211 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces
);
14212 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces
);
14213 verifyFormat("vector< int > x{ // comment 1\n"
14215 SpaceBetweenBraces
);
14216 SpaceBetweenBraces
.ColumnLimit
= 20;
14217 verifyFormat("vector< int > x{\n"
14219 "vector<int>x{1,2,3,4};", SpaceBetweenBraces
);
14220 SpaceBetweenBraces
.ColumnLimit
= 24;
14221 verifyFormat("vector< int > x{ 1, 2,\n"
14223 "vector<int>x{1,2,3,4};", SpaceBetweenBraces
);
14224 verifyFormat("vector< int > x{\n"
14230 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces
);
14231 verifyFormat("vector< int > x{};", SpaceBetweenBraces
);
14232 SpaceBetweenBraces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
14233 SpaceBetweenBraces
.SpacesInParensOptions
.InEmptyParentheses
= true;
14234 verifyFormat("vector< int > x{ };", SpaceBetweenBraces
);
14237 TEST_F(FormatTest
, FormatsBracedListsInColumnLayout
) {
14238 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14239 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14240 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14241 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14242 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14243 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14244 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
14245 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14246 " 1, 22, 333, 4444, 55555, //\n"
14247 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14248 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
14250 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14251 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14252 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
14253 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14254 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14255 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
14257 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14258 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14259 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14260 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14261 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14262 " // Separating comment.\n"
14263 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14264 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
14265 " // Leading comment\n"
14266 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
14267 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
14268 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14270 getLLVMStyleWithColumns(39));
14271 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14273 getLLVMStyleWithColumns(38));
14274 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
14275 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
14276 getLLVMStyleWithColumns(43));
14278 "static unsigned SomeValues[10][3] = {\n"
14279 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
14280 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
14281 verifyFormat("static auto fields = new vector<string>{\n"
14282 " \"aaaaaaaaaaaaa\",\n"
14283 " \"aaaaaaaaaaaaa\",\n"
14284 " \"aaaaaaaaaaaa\",\n"
14285 " \"aaaaaaaaaaaaaa\",\n"
14286 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14287 " \"aaaaaaaaaaaa\",\n"
14288 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
14290 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
14291 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
14292 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
14293 " 3, cccccccccccccccccccccc};",
14294 getLLVMStyleWithColumns(60));
14296 // Trailing commas.
14297 verifyFormat("vector<int> x = {\n"
14298 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
14300 getLLVMStyleWithColumns(39));
14301 verifyFormat("vector<int> x = {\n"
14302 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
14304 getLLVMStyleWithColumns(39));
14305 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
14308 getLLVMStyleWithColumns(39));
14310 // Trailing comment in the first line.
14311 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
14312 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
14313 " 111111111, 222222222, 3333333333, 444444444, //\n"
14314 " 11111111, 22222222, 333333333, 44444444};");
14315 // Trailing comment in the last line.
14316 verifyFormat("int aaaaa[] = {\n"
14317 " 1, 2, 3, // comment\n"
14318 " 4, 5, 6 // comment\n"
14321 // With nested lists, we should either format one item per line or all nested
14322 // lists one on line.
14323 // FIXME: For some nested lists, we can do better.
14324 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
14325 " {aaaaaaaaaaaaaaaaaaa},\n"
14326 " {aaaaaaaaaaaaaaaaaaaaa},\n"
14327 " {aaaaaaaaaaaaaaaaa}};",
14328 getLLVMStyleWithColumns(60));
14330 "SomeStruct my_struct_array = {\n"
14331 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
14332 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
14335 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
14336 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
14337 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
14339 // No column layout should be used here.
14340 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
14341 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
14343 verifyNoCrash("a<,");
14345 // No braced initializer here.
14346 verifyFormat("void f() {\n"
14347 " struct Dummy {};\n"
14350 verifyFormat("void foo() {\n"
14362 verifyFormat("namespace n {\n"
14374 "} // namespace n");
14376 // Long lists should be formatted in columns even if they are nested.
14378 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14379 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14380 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14381 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14382 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
14383 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
14385 // Allow "single-column" layout even if that violates the column limit. There
14386 // isn't going to be a better way.
14387 verifyFormat("std::vector<int> a = {\n"
14394 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
14395 getLLVMStyleWithColumns(30));
14396 verifyFormat("vector<int> aaaa = {\n"
14397 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14398 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14399 " aaaaaa.aaaaaaa,\n"
14400 " aaaaaa.aaaaaaa,\n"
14401 " aaaaaa.aaaaaaa,\n"
14402 " aaaaaa.aaaaaaa,\n"
14405 // Don't create hanging lists.
14406 verifyFormat("someFunction(Param, {List1, List2,\n"
14408 getLLVMStyleWithColumns(35));
14409 verifyFormat("someFunction(Param, Param,\n"
14410 " {List1, List2,\n"
14412 getLLVMStyleWithColumns(35));
14413 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
14414 " aaaaaaaaaaaaaaaaaaaaaaa);");
14416 // No possible column formats, don't want the optimal paths penalized.
14418 "waarudo::unit desk = {\n"
14419 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};");
14420 verifyFormat("SomeType something1([](const Input &i) -> Output { return "
14421 "Output{1, 2}; },\n"
14422 " [](const Input &i) -> Output { return "
14423 "Output{1, 2}; });");
14424 FormatStyle NoBinPacking
= getLLVMStyle();
14425 NoBinPacking
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
14426 verifyFormat("waarudo::unit desk = {\n"
14427 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, "
14428 "1, 1} * w::m; }};",
14432 TEST_F(FormatTest
, PullTrivialFunctionDefinitionsIntoSingleLine
) {
14433 FormatStyle DoNotMerge
= getLLVMStyle();
14434 DoNotMerge
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14436 verifyFormat("void f() { return 42; }");
14437 verifyFormat("void f() {\n"
14441 verifyFormat("void f() {\n"
14452 verifyFormat("void f() {} // comment");
14453 verifyFormat("void f() { int a; } // comment");
14454 verifyFormat("void f() {\n"
14457 verifyFormat("void f() {\n"
14461 verifyFormat("void f() {\n"
14463 getLLVMStyleWithColumns(15));
14465 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
14466 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
14468 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
14469 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
14470 verifyGoogleFormat("class C {\n"
14472 " : iiiiiiii(nullptr),\n"
14473 " kkkkkkk(nullptr),\n"
14474 " mmmmmmm(nullptr),\n"
14475 " nnnnnnn(nullptr) {}\n"
14478 FormatStyle NoColumnLimit
= getLLVMStyleWithColumns(0);
14479 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit
);
14480 verifyFormat("class C {\n"
14483 "class C{A():b(0){}};", NoColumnLimit
);
14484 verifyFormat("A()\n"
14487 "A()\n:b(0)\n{\n}", NoColumnLimit
);
14489 FormatStyle NoColumnLimitWrapAfterFunction
= NoColumnLimit
;
14490 NoColumnLimitWrapAfterFunction
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14491 NoColumnLimitWrapAfterFunction
.BraceWrapping
.AfterFunction
= true;
14492 verifyFormat("class C {\n"
14494 " int foo { return 0; }\n"
14496 NoColumnLimitWrapAfterFunction
);
14497 verifyFormat("class C {\n"
14501 NoColumnLimitWrapAfterFunction
);
14503 FormatStyle DoNotMergeNoColumnLimit
= NoColumnLimit
;
14504 DoNotMergeNoColumnLimit
.AllowShortFunctionsOnASingleLine
=
14505 FormatStyle::SFS_None
;
14506 verifyFormat("A()\n"
14509 "A():b(0){}", DoNotMergeNoColumnLimit
);
14510 verifyFormat("A()\n"
14513 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit
);
14515 verifyFormat("#define A \\\n"
14519 getLLVMStyleWithColumns(20));
14520 verifyFormat("#define A \\\n"
14521 " void f() { int i; }",
14522 getLLVMStyleWithColumns(21));
14523 verifyFormat("#define A \\\n"
14528 getLLVMStyleWithColumns(22));
14529 verifyFormat("#define A \\\n"
14530 " void f() { int i; } \\\n"
14532 getLLVMStyleWithColumns(23));
14535 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
14536 " aaaaaaaaaaaaaaaaaa,\n"
14537 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");
14539 constexpr StringRef Code
{"void foo() { /* Empty */ }"};
14540 verifyFormat(Code
);
14541 verifyFormat(Code
, "void foo() { /* Empty */\n"
14543 verifyFormat(Code
, "void foo() {\n"
14548 TEST_F(FormatTest
, PullEmptyFunctionDefinitionsIntoSingleLine
) {
14549 FormatStyle MergeEmptyOnly
= getLLVMStyle();
14550 MergeEmptyOnly
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Empty
;
14551 verifyFormat("class C {\n"
14555 verifyFormat("class C {\n"
14561 verifyFormat("int f() {}", MergeEmptyOnly
);
14562 verifyFormat("int f() {\n"
14567 // Also verify behavior when BraceWrapping.AfterFunction = true
14568 MergeEmptyOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14569 MergeEmptyOnly
.BraceWrapping
.AfterFunction
= true;
14570 verifyFormat("int f() {}", MergeEmptyOnly
);
14571 verifyFormat("class C {\n"
14577 TEST_F(FormatTest
, PullInlineFunctionDefinitionsIntoSingleLine
) {
14578 FormatStyle MergeInlineOnly
= getLLVMStyle();
14579 MergeInlineOnly
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Inline
;
14580 verifyFormat("class C {\n"
14581 " int f() { return 42; }\n"
14584 verifyFormat("int f() {\n"
14589 // SFS_Inline implies SFS_Empty
14590 verifyFormat("class C {\n"
14594 verifyFormat("int f() {}", MergeInlineOnly
);
14595 // https://llvm.org/PR54147
14596 verifyFormat("auto lambda = []() {\n"
14603 verifyFormat("class C {\n"
14605 " int f() { return 42; }\n"
14610 verifyFormat("struct S {\n"
14613 " int foo() { bar(); }\n"
14618 // Also verify behavior when BraceWrapping.AfterFunction = true
14619 MergeInlineOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14620 MergeInlineOnly
.BraceWrapping
.AfterFunction
= true;
14621 verifyFormat("class C {\n"
14622 " int f() { return 42; }\n"
14625 verifyFormat("int f()\n"
14631 // SFS_Inline implies SFS_Empty
14632 verifyFormat("int f() {}", MergeInlineOnly
);
14633 verifyFormat("class C {\n"
14638 MergeInlineOnly
.BraceWrapping
.AfterClass
= true;
14639 MergeInlineOnly
.BraceWrapping
.AfterStruct
= true;
14640 verifyFormat("class C\n"
14642 " int f() { return 42; }\n"
14645 verifyFormat("struct C\n"
14647 " int f() { return 42; }\n"
14650 verifyFormat("int f()\n"
14655 verifyFormat("int f() {}", MergeInlineOnly
);
14656 verifyFormat("class C\n"
14658 " int f() { return 42; }\n"
14661 verifyFormat("struct C\n"
14663 " int f() { return 42; }\n"
14666 verifyFormat("struct C\n"
14671 " int f() { return 42; }\n"
14674 verifyFormat("/* comment */ struct C\n"
14676 " int f() { return 42; }\n"
14681 TEST_F(FormatTest
, PullInlineOnlyFunctionDefinitionsIntoSingleLine
) {
14682 FormatStyle MergeInlineOnly
= getLLVMStyle();
14683 MergeInlineOnly
.AllowShortFunctionsOnASingleLine
=
14684 FormatStyle::SFS_InlineOnly
;
14685 verifyFormat("class C {\n"
14686 " int f() { return 42; }\n"
14689 verifyFormat("int f() {\n"
14694 // SFS_InlineOnly does not imply SFS_Empty
14695 verifyFormat("class C {\n"
14699 verifyFormat("int f() {\n"
14703 // Also verify behavior when BraceWrapping.AfterFunction = true
14704 MergeInlineOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14705 MergeInlineOnly
.BraceWrapping
.AfterFunction
= true;
14706 verifyFormat("class C {\n"
14707 " int f() { return 42; }\n"
14710 verifyFormat("int f()\n"
14716 // SFS_InlineOnly does not imply SFS_Empty
14717 verifyFormat("int f()\n"
14721 verifyFormat("class C {\n"
14727 TEST_F(FormatTest
, SplitEmptyFunction
) {
14728 FormatStyle Style
= getLLVMStyleWithColumns(40);
14729 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14730 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14731 Style
.BraceWrapping
.AfterFunction
= true;
14732 Style
.BraceWrapping
.SplitEmptyFunction
= false;
14734 verifyFormat("int f()\n"
14737 verifyFormat("int f()\n"
14742 verifyFormat("int f()\n"
14744 " // some comment\n"
14748 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Empty
;
14749 verifyFormat("int f() {}", Style
);
14750 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14753 verifyFormat("int f()\n"
14759 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Inline
;
14760 verifyFormat("class Foo {\n"
14764 verifyFormat("class Foo {\n"
14765 " int f() { return 0; }\n"
14768 verifyFormat("class Foo {\n"
14769 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14773 verifyFormat("class Foo {\n"
14774 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14781 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
14782 verifyFormat("int f() {}", Style
);
14783 verifyFormat("int f() { return 0; }", Style
);
14784 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14787 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14794 TEST_F(FormatTest
, SplitEmptyFunctionButNotRecord
) {
14795 FormatStyle Style
= getLLVMStyleWithColumns(40);
14796 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14797 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14798 Style
.BraceWrapping
.AfterFunction
= true;
14799 Style
.BraceWrapping
.SplitEmptyFunction
= true;
14800 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14802 verifyFormat("class C {};", Style
);
14803 verifyFormat("struct C {};", Style
);
14804 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14805 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14809 verifyFormat("class C {\n"
14811 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14812 " bbbbbbbbbbbbbbbbbbb()\n"
14816 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14817 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14824 TEST_F(FormatTest
, KeepShortFunctionAfterPPElse
) {
14825 FormatStyle Style
= getLLVMStyle();
14826 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
14827 verifyFormat("#ifdef A\n"
14835 TEST_F(FormatTest
, SplitEmptyClass
) {
14836 FormatStyle Style
= getLLVMStyle();
14837 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14838 Style
.BraceWrapping
.AfterClass
= true;
14839 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14841 verifyFormat("class Foo\n"
14844 verifyFormat("/* something */ class Foo\n"
14847 verifyFormat("template <typename X> class Foo\n"
14850 verifyFormat("class Foo\n"
14855 verifyFormat("typedef class Foo\n"
14860 Style
.BraceWrapping
.SplitEmptyRecord
= true;
14861 Style
.BraceWrapping
.AfterStruct
= true;
14862 verifyFormat("class rep\n"
14866 verifyFormat("struct rep\n"
14870 verifyFormat("template <typename T> class rep\n"
14874 verifyFormat("template <typename T> struct rep\n"
14878 verifyFormat("class rep\n"
14883 verifyFormat("struct rep\n"
14888 verifyFormat("template <typename T> class rep\n"
14893 verifyFormat("template <typename T> struct rep\n"
14898 verifyFormat("template <typename T> class rep // Foo\n"
14903 verifyFormat("template <typename T> struct rep // Bar\n"
14909 verifyFormat("template <typename T> class rep<T>\n"
14915 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14920 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14925 verifyFormat("#include \"stdint.h\"\n"
14926 "namespace rep {}",
14928 verifyFormat("#include <stdint.h>\n"
14929 "namespace rep {}",
14931 verifyFormat("#include <stdint.h>\n"
14932 "namespace rep {}",
14933 "#include <stdint.h>\n"
14934 "namespace rep {\n"
14941 TEST_F(FormatTest
, SplitEmptyStruct
) {
14942 FormatStyle Style
= getLLVMStyle();
14943 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14944 Style
.BraceWrapping
.AfterStruct
= true;
14945 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14947 verifyFormat("struct Foo\n"
14950 verifyFormat("/* something */ struct Foo\n"
14953 verifyFormat("template <typename X> struct Foo\n"
14956 verifyFormat("struct Foo\n"
14961 verifyFormat("typedef struct Foo\n"
14965 // typedef struct Bar {} Bar_t;
14968 TEST_F(FormatTest
, SplitEmptyUnion
) {
14969 FormatStyle Style
= getLLVMStyle();
14970 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14971 Style
.BraceWrapping
.AfterUnion
= true;
14972 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14974 verifyFormat("union Foo\n"
14977 verifyFormat("/* something */ union Foo\n"
14980 verifyFormat("union Foo\n"
14985 verifyFormat("typedef union Foo\n"
14991 TEST_F(FormatTest
, SplitEmptyNamespace
) {
14992 FormatStyle Style
= getLLVMStyle();
14993 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14994 Style
.BraceWrapping
.AfterNamespace
= true;
14995 Style
.BraceWrapping
.SplitEmptyNamespace
= false;
14997 verifyFormat("namespace Foo\n"
15000 verifyFormat("/* something */ namespace Foo\n"
15003 verifyFormat("inline namespace Foo\n"
15006 verifyFormat("/* something */ inline namespace Foo\n"
15009 verifyFormat("export namespace Foo\n"
15012 verifyFormat("namespace Foo\n"
15019 TEST_F(FormatTest
, NeverMergeShortRecords
) {
15020 FormatStyle Style
= getLLVMStyle();
15022 verifyFormat("class Foo {\n"
15026 verifyFormat("typedef class Foo {\n"
15030 verifyFormat("struct Foo {\n"
15034 verifyFormat("typedef struct Foo {\n"
15038 verifyFormat("union Foo {\n"
15042 verifyFormat("typedef union Foo {\n"
15046 verifyFormat("namespace Foo {\n"
15051 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
15052 Style
.BraceWrapping
.AfterClass
= true;
15053 Style
.BraceWrapping
.AfterStruct
= true;
15054 Style
.BraceWrapping
.AfterUnion
= true;
15055 Style
.BraceWrapping
.AfterNamespace
= true;
15056 verifyFormat("class Foo\n"
15061 verifyFormat("typedef class Foo\n"
15066 verifyFormat("struct Foo\n"
15071 verifyFormat("typedef struct Foo\n"
15076 verifyFormat("union Foo\n"
15081 verifyFormat("typedef union Foo\n"
15086 verifyFormat("namespace Foo\n"
15093 TEST_F(FormatTest
, UnderstandContextOfRecordTypeKeywords
) {
15094 // Elaborate type variable declarations.
15095 verifyFormat("struct foo a = {bar};\nint n;");
15096 verifyFormat("class foo a = {bar};\nint n;");
15097 verifyFormat("union foo a = {bar};\nint n;");
15099 // Elaborate types inside function definitions.
15100 verifyFormat("struct foo f() {}\nint n;");
15101 verifyFormat("class foo f() {}\nint n;");
15102 verifyFormat("union foo f() {}\nint n;");
15105 verifyFormat("template <class X> void f() {}\nint n;");
15106 verifyFormat("template <struct X> void f() {}\nint n;");
15107 verifyFormat("template <union X> void f() {}\nint n;");
15109 // Actual definitions...
15110 verifyFormat("struct {\n} n;");
15112 "template <template <class T, class Y>, class Z> class X {\n} n;");
15113 verifyFormat("union Z {\n int n;\n} x;");
15114 verifyFormat("class MACRO Z {\n} n;");
15115 verifyFormat("class MACRO(X) Z {\n} n;");
15116 verifyFormat("class __attribute__((X)) Z {\n} n;");
15117 verifyFormat("class __declspec(X) Z {\n} n;");
15118 verifyFormat("class A##B##C {\n} n;");
15119 verifyFormat("class alignas(16) Z {\n} n;");
15120 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
15121 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
15123 // Redefinition from nested context:
15124 verifyFormat("class A::B::C {\n} n;");
15126 // Template definitions.
15128 "template <typename F>\n"
15129 "Matcher(const Matcher<F> &Other,\n"
15130 " typename enable_if_c<is_base_of<F, T>::value &&\n"
15131 " !is_same<F, T>::value>::type * = 0)\n"
15132 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
15134 // FIXME: This is still incorrectly handled at the formatter side.
15135 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
15136 verifyFormat("int i = SomeFunction(a<b, a> b);");
15138 verifyFormat("class A<int> f() {}\n"
15140 verifyFormat("template <typename T> class A<T> f() {}\n"
15143 verifyFormat("template <> class Foo<int> F() {\n"
15146 // Elaborate types where incorrectly parsing the structural element would
15147 // break the indent.
15148 verifyFormat("if (true)\n"
15153 // This is simply incomplete. Formatting is not important, but must not crash.
15154 verifyFormat("class A:");
15157 TEST_F(FormatTest
, DoNotInterfereWithErrorAndWarning
) {
15158 verifyNoChange("#error Leave all white!!!!! space* alone!");
15159 verifyNoChange("#warning Leave all white!!!!! space* alone!");
15160 verifyFormat("#error 1", " # error 1");
15161 verifyFormat("#warning 1", " # warning 1");
15164 TEST_F(FormatTest
, FormatHashIfExpressions
) {
15165 verifyFormat("#if AAAA && BBBB");
15166 verifyFormat("#if (AAAA && BBBB)");
15167 verifyFormat("#elif (AAAA && BBBB)");
15168 // FIXME: Come up with a better indentation for #elif.
15170 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
15171 " defined(BBBBBBBB)\n"
15172 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
15173 " defined(BBBBBBBB)\n"
15175 getLLVMStyleWithColumns(65));
15178 TEST_F(FormatTest
, MergeHandlingInTheFaceOfPreprocessorDirectives
) {
15179 FormatStyle AllowsMergedIf
= getGoogleStyle();
15180 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
15181 FormatStyle::SIS_WithoutElse
;
15182 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf
);
15183 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf
);
15184 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf
);
15185 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf
);
15186 FormatStyle ShortMergedIf
= AllowsMergedIf
;
15187 ShortMergedIf
.ColumnLimit
= 25;
15188 verifyFormat("#define A \\\n"
15189 " if (true) return 42;",
15191 verifyFormat("#define A \\\n"
15196 verifyFormat("#define A \\\n"
15204 " if (true) continue;\n"
15207 " if (true) continue;\n"
15210 ShortMergedIf
.ColumnLimit
= 33;
15211 verifyFormat("#define A \\\n"
15212 " if constexpr (true) return 42;",
15214 verifyFormat("#define A \\\n"
15215 " if CONSTEXPR (true) return 42;",
15217 ShortMergedIf
.ColumnLimit
= 29;
15218 verifyFormat("#define A \\\n"
15219 " if (aaaaaaaaaa) return 1; \\\n"
15222 ShortMergedIf
.ColumnLimit
= 28;
15223 verifyFormat("#define A \\\n"
15224 " if (aaaaaaaaaa) \\\n"
15228 verifyFormat("#define A \\\n"
15229 " if constexpr (aaaaaaa) \\\n"
15233 verifyFormat("#define A \\\n"
15234 " if CONSTEXPR (aaaaaaa) \\\n"
15239 verifyFormat("//\n"
15243 getChromiumStyle(FormatStyle::LK_Cpp
));
15246 TEST_F(FormatTest
, FormatStarDependingOnContext
) {
15247 verifyFormat("void f(int *a);");
15248 verifyFormat("void f() { f(fint * b); }");
15249 verifyFormat("class A {\n void f(int *a);\n};");
15250 verifyFormat("class A {\n int *a;\n};");
15251 verifyFormat("namespace a {\n"
15257 "} // namespace b\n"
15258 "} // namespace a");
15261 TEST_F(FormatTest
, SpecialTokensAtEndOfLine
) {
15262 verifyFormat("while");
15263 verifyFormat("operator");
15266 TEST_F(FormatTest
, SkipsDeeplyNestedLines
) {
15267 // This code would be painfully slow to format if we didn't skip it.
15268 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
15269 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15270 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15271 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15272 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
15274 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
15275 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15276 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15277 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15278 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15279 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15280 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15281 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15282 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
15283 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
15284 // Deeply nested part is untouched, rest is formatted.
15285 EXPECT_EQ(std::string("int i;") + Code
+ "int j;",
15286 format(std::string("int i;") + Code
+ "int j;",
15287 getLLVMStyle(), SC_ExpectIncomplete
));
15290 //===----------------------------------------------------------------------===//
15291 // Objective-C tests.
15292 //===----------------------------------------------------------------------===//
15294 TEST_F(FormatTest
, FormatForObjectiveCMethodDecls
) {
15295 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
15296 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
15297 "-(NSUInteger)indexOfObject:(id)anObject;");
15298 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
15299 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
15300 verifyFormat("- (NSInteger)Method3:(id)anObject;",
15301 "-(NSInteger)Method3:(id)anObject;");
15302 verifyFormat("- (NSInteger)Method4:(id)anObject;",
15303 "-(NSInteger)Method4:(id)anObject;");
15304 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
15305 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
15306 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
15307 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
15308 "forAllCells:(BOOL)flag;");
15310 // Very long objectiveC method declaration.
15311 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
15312 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
15313 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
15314 " inRange:(NSRange)range\n"
15315 " outRange:(NSRange)out_range\n"
15316 " outRange1:(NSRange)out_range1\n"
15317 " outRange2:(NSRange)out_range2\n"
15318 " outRange3:(NSRange)out_range3\n"
15319 " outRange4:(NSRange)out_range4\n"
15320 " outRange5:(NSRange)out_range5\n"
15321 " outRange6:(NSRange)out_range6\n"
15322 " outRange7:(NSRange)out_range7\n"
15323 " outRange8:(NSRange)out_range8\n"
15324 " outRange9:(NSRange)out_range9;");
15326 // When the function name has to be wrapped.
15327 FormatStyle Style
= getLLVMStyle();
15328 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
15329 // and always indents instead.
15330 Style
.IndentWrappedFunctionNames
= false;
15331 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15332 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
15333 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
15336 Style
.IndentWrappedFunctionNames
= true;
15337 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
15338 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
15339 " anotherName:(NSString)dddddddddddddd {\n"
15343 verifyFormat("- (int)sum:(vector<int>)numbers;");
15344 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
15345 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
15346 // protocol lists (but not for template classes):
15347 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
15349 verifyFormat("- (int (*)())foo:(int (*)())f;");
15350 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
15352 // If there's no return type (very rare in practice!), LLVM and Google style
15354 verifyFormat("- foo;");
15355 verifyFormat("- foo:(int)f;");
15356 verifyGoogleFormat("- foo:(int)foo;");
15359 TEST_F(FormatTest
, BreaksStringLiterals
) {
15360 // FIXME: unstable test case
15361 EXPECT_EQ("\"some text \"\n"
15363 format("\"some text other\";", getLLVMStyleWithColumns(12)));
15364 // FIXME: unstable test case
15365 EXPECT_EQ("\"some text \"\n"
15367 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
15368 verifyFormat("#define A \\\n"
15372 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
15373 verifyFormat("#define A \\\n"
15377 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
15379 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
15380 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
15381 // FIXME: unstable test case
15382 EXPECT_EQ("\"some \"\n"
15384 format("\"some text\"", getLLVMStyleWithColumns(10)));
15385 // FIXME: unstable test case
15386 EXPECT_EQ("\"some \"\n"
15388 format("\"some text\"", getLLVMStyleWithColumns(7)));
15389 // FIXME: unstable test case
15390 EXPECT_EQ("\"some\"\n"
15393 format("\"some text\"", getLLVMStyleWithColumns(6)));
15394 // FIXME: unstable test case
15395 EXPECT_EQ("\"some\"\n"
15398 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
15399 // FIXME: unstable test case
15400 EXPECT_EQ("\"some\"\n"
15403 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
15405 verifyFormat("variable =\n"
15406 " \"long string \"\n"
15408 "variable = \"long string literal\";",
15409 getLLVMStyleWithColumns(20));
15411 verifyFormat("variable = f(\n"
15412 " \"long string \"\n"
15415 " loooooooooooooooooooong);",
15416 "variable = f(\"long string literal\", short, "
15417 "loooooooooooooooooooong);",
15418 getLLVMStyleWithColumns(20));
15420 verifyFormat("f(g(\"long string \"\n"
15423 "f(g(\"long string literal\"), b);",
15424 getLLVMStyleWithColumns(20));
15425 verifyFormat("f(g(\"long string \"\n"
15429 "f(g(\"long string literal\", a), b);",
15430 getLLVMStyleWithColumns(20));
15431 verifyFormat("f(\"one two\".split(\n"
15433 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
15434 verifyFormat("f(\"one two three four five six \"\n"
15435 " \"seven\".split(\n"
15436 " really_looooong_variable));",
15437 "f(\"one two three four five six seven\"."
15438 "split(really_looooong_variable));",
15439 getLLVMStyleWithColumns(33));
15441 verifyFormat("f(\"some \"\n"
15444 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
15446 // Only break as a last resort.
15448 "aaaaaaaaaaaaaaaaaaaa(\n"
15449 " aaaaaaaaaaaaaaaaaaaa,\n"
15450 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
15452 // FIXME: unstable test case
15453 EXPECT_EQ("\"splitmea\"\n"
15456 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
15458 // FIXME: unstable test case
15459 EXPECT_EQ("\"split/\"\n"
15462 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15464 // FIXME: unstable test case
15465 EXPECT_EQ("\"split/\"\n"
15468 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
15469 // FIXME: unstable test case
15470 EXPECT_EQ("\"split at \"\n"
15472 "\"slashes.at.any$\"\n"
15473 "\"non-alphanumeric%\"\n"
15474 "\"1111111111characte\"\n"
15476 format("\"split at "
15481 "1111111111characte"
15483 getLLVMStyleWithColumns(20)));
15485 // Verify that splitting the strings understands
15486 // Style::AlwaysBreakBeforeMultilineStrings.
15487 verifyFormat("aaaaaaaaaaaa(\n"
15488 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
15489 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
15490 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
15491 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15492 "aaaaaaaaaaaaaaaaaaaaaa\");",
15494 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15495 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
15496 "return \"aaaaaaaaaaaaaaaaaaaaaa "
15497 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
15498 "aaaaaaaaaaaaaaaaaaaaaa\";",
15500 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15501 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
15503 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
15504 "aaaaaaaaaaaaaaaaaaa\";");
15505 verifyFormat("ffff(\n"
15506 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
15507 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15508 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
15509 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
15512 FormatStyle Style
= getLLVMStyleWithColumns(12);
15513 Style
.BreakStringLiterals
= false;
15514 verifyFormat("\"some text other\";", Style
);
15516 FormatStyle AlignLeft
= getLLVMStyleWithColumns(12);
15517 AlignLeft
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
15518 verifyFormat("#define A \\\n"
15522 "#define A \"some text other\";", AlignLeft
);
15525 TEST_F(FormatTest
, BreaksStringLiteralsAtColumnLimit
) {
15526 verifyFormat("C a = \"some more \"\n"
15528 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
15531 TEST_F(FormatTest
, FullyRemoveEmptyLines
) {
15532 FormatStyle NoEmptyLines
= getLLVMStyleWithColumns(80);
15533 NoEmptyLines
.MaxEmptyLinesToKeep
= 0;
15534 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
15538 TEST_F(FormatTest
, BreaksStringLiteralsWithTabs
) {
15539 // FIXME: unstable test case
15541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15544 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15549 TEST_F(FormatTest
, BreaksWideAndNSStringLiterals
) {
15550 // FIXME: unstable test case
15552 "u8\"utf8 string \"\n"
15554 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
15555 // FIXME: unstable test case
15557 "u\"utf16 string \"\n"
15559 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
15560 // FIXME: unstable test case
15562 "U\"utf32 string \"\n"
15564 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
15565 // FIXME: unstable test case
15566 EXPECT_EQ("L\"wide string \"\n"
15568 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
15569 verifyFormat("@\"NSString \"\n"
15571 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
15572 verifyFormat(R
"(NSString *s = @"那那那那
";)", getLLVMStyleWithColumns(26));
15574 // This input makes clang-format try to split the incomplete unicode escape
15575 // sequence, which used to lead to a crasher.
15577 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15578 getLLVMStyleWithColumns(60));
15581 TEST_F(FormatTest
, DoesNotBreakRawStringLiterals
) {
15582 FormatStyle Style
= getGoogleStyleWithColumns(15);
15583 verifyFormat("R\"x(raw literal)x\";", Style
);
15584 verifyFormat("uR\"x(raw literal)x\";", Style
);
15585 verifyFormat("LR\"x(raw literal)x\";", Style
);
15586 verifyFormat("UR\"x(raw literal)x\";", Style
);
15587 verifyFormat("u8R\"x(raw literal)x\";", Style
);
15590 TEST_F(FormatTest
, BreaksStringLiteralsWithin_TMacro
) {
15591 FormatStyle Style
= getLLVMStyleWithColumns(20);
15592 // FIXME: unstable test case
15594 "_T(\"aaaaaaaaaaaaaa\")\n"
15595 "_T(\"aaaaaaaaaaaaaa\")\n"
15596 "_T(\"aaaaaaaaaaaa\")",
15597 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style
));
15598 verifyFormat("f(x,\n"
15599 " _T(\"aaaaaaaaaaaa\")\n"
15602 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style
);
15604 // FIXME: Handle embedded spaces in one iteration.
15605 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
15606 // "_T(\"aaaaaaaaaaaaa\")\n"
15607 // "_T(\"aaaaaaaaaaaaa\")\n"
15609 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15610 // getLLVMStyleWithColumns(20)));
15611 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
15612 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style
);
15613 verifyFormat("f(\n"
15615 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15620 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
15623 verifyFormat("f(\n"
15625 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15628 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15629 // Regression test for accessing tokens past the end of a vector in the
15631 verifyNoCrash(R
"(_T(
15637 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15638 // In a function call with two operands, the second can be broken with no line
15639 // break before it.
15640 verifyFormat("func(a
, \"long long \"\n"
15641 " \"long long\");",
15642 "func(a
, \"long long long long\");",
15643 getLLVMStyleWithColumns(24));
15644 // In a function call with three operands, the second must be broken with a
15645 // line break before it.
15646 verifyFormat("func(a
,\n"
15647 " \"long long long \"\n"
15650 "func(a
, \"long long long long\", c
);",
15651 getLLVMStyleWithColumns(24));
15652 // In a function call with three operands, the third must be broken with a
15653 // line break before it.
15654 verifyFormat("func(a
, b
,\n"
15655 " \"long long long \"\n"
15657 "func(a
, b
, \"long long long long\");",
15658 getLLVMStyleWithColumns(24));
15659 // In a function call with three operands, both the second and the third must
15660 // be broken with a line break before them.
15661 verifyFormat("func(a
,\n"
15662 " \"long long long \"\n"
15664 " \"long long long \"\n"
15666 "func(a
, \"long long long long\", \"long long long long\");",
15667 getLLVMStyleWithColumns(24));
15668 // In a chain of << with two operands, the second can be broken with no line
15669 // break before it.
15670 verifyFormat("a
<< \"line line
\"\n"
15672 "a
<< \"line line line
\";", getLLVMStyleWithColumns(20));
15673 // In a chain of << with three operands, the second can be broken with no line
15674 // break before it.
15675 verifyFormat("abcde
<< \"line
\"\n"
15678 "abcde
<< \"line line line
\" << c
;",
15679 getLLVMStyleWithColumns(20));
15680 // In a chain of << with three operands, the third must be broken with a line
15681 // break before it.
15682 verifyFormat("a
<< b
\n"
15683 " << \"line line
\"\n"
15685 "a
<< b
<< \"line line line
\";", getLLVMStyleWithColumns(20));
15686 // In a chain of << with three operands, the second can be broken with no line
15687 // break before it and the third must be broken with a line break before it.
15688 verifyFormat("abcd
<< \"line line
\"\n"
15690 " << \"line line
\"\n"
15692 "abcd
<< \"line line line
\" << \"line line line
\";",
15693 getLLVMStyleWithColumns(20));
15694 // In a chain of binary operators with two operands, the second can be broken
15695 // with no line break before it.
15696 verifyFormat("abcd
+ \"line line
\"\n"
15698 "abcd
+ \"line line line line
\";", getLLVMStyleWithColumns(20));
15699 // In a chain of binary operators with three operands, the second must be
15700 // broken with a line break before it.
15701 verifyFormat("abcd
+\n"
15702 " \"line line
\"\n"
15703 " \"line line
\" +\n"
15705 "abcd
+ \"line line line line
\" + e
;",
15706 getLLVMStyleWithColumns(20));
15707 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15708 // the first must be broken with a line break before it.
15709 FormatStyle Style = getLLVMStyleWithColumns(25);
15710 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15711 verifyFormat("someFunction(\n"
15712 " \"long long long \"\n"
15715 "someFunction(\"long long long long\", a
);", Style);
15716 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15717 verifyFormat("someFunction(\n"
15718 " \"long long long \"\n"
15725 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15726 verifyFormat("aaaaaaaaaaa
= \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\";",
15729 "aaaaaaaaaaa
= \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\";");
15734 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15735 verifyFormat("f(g(R
\"x(raw literal
)x
\", a
), b
);",
15736 "f(g(R
\"x(raw literal
)x
\", a
), b
);", getGoogleStyle());
15737 verifyFormat("fffffffffff(g(R
\"x(\n"
15738 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15742 "fffffffffff(g(R
\"x(\n"
15743 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15745 getGoogleStyleWithColumns(20));
15746 verifyFormat("fffffffffff(\n"
15748 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15752 "fffffffffff(g(R
\"x(qqq
\n"
15753 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15755 getGoogleStyleWithColumns(20));
15757 verifyNoChange("fffffffffff(R
\"x(\n"
15758 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15760 getGoogleStyleWithColumns(20));
15761 verifyFormat("fffffffffff(R
\"x(\n"
15762 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15764 "fffffffffff(R
\"x(\n"
15765 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15767 getGoogleStyleWithColumns(20));
15768 verifyFormat("fffffffffff(\n"
15770 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15775 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15777 getGoogleStyleWithColumns(20));
15778 verifyFormat("fffffffffff(R
\"(single line raw string
)\" + bbbbbb
);",
15780 " R
\"(single line raw string
)\" + bbbbbb
);");
15783 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15784 verifyFormat("string a
= \"unterminated
;");
15785 verifyFormat("function(\"unterminated
,\n"
15786 " OtherParameter
);",
15787 "function( \"unterminated
,\n"
15788 " OtherParameter
);");
15791 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15792 FormatStyle Style = getLLVMStyle();
15793 Style.Standard = FormatStyle::LS_Cpp03;
15794 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15795 "#define x(_a) printf(\"foo\"_a);", Style);
15798 TEST_F(FormatTest, CppLexVersion) {
15799 FormatStyle Style = getLLVMStyle();
15800 // Formatting of x * y differs if x is a type.
15801 verifyFormat("void foo() { MACRO(a * b); }", Style);
15802 verifyFormat("void foo() { MACRO(int *b); }", Style);
15804 // LLVM style uses latest lexer.
15805 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15806 Style.Standard = FormatStyle::LS_Cpp17;
15807 // But in c++17, char8_t isn't a keyword.
15808 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15811 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15813 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15814 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15816 "someFunction(\"aaabbbcccdddeeefff\");",
15817 getLLVMStyleWithColumns(25));
15818 verifyFormat("someFunction1234567890(\n"
15819 " \"aaabbbcccdddeeefff\");",
15820 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15821 getLLVMStyleWithColumns(26));
15822 verifyFormat("someFunction1234567890(\n"
15823 " \"aaabbbcccdddeeeff\"\n"
15825 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15826 getLLVMStyleWithColumns(25));
15827 verifyFormat("someFunction1234567890(\n"
15828 " \"aaabbbcccdddeeeff\"\n"
15830 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15831 getLLVMStyleWithColumns(24));
15832 verifyFormat("someFunction(\n"
15833 " \"aaabbbcc ddde \"\n"
15835 "someFunction(\"aaabbbcc ddde efff\");",
15836 getLLVMStyleWithColumns(25));
15837 verifyFormat("someFunction(\"aaabbbccc \"\n"
15839 "someFunction(\"aaabbbccc ddeeefff\");",
15840 getLLVMStyleWithColumns(25));
15841 verifyFormat("someFunction1234567890(\n"
15843 " \"cccdddeeefff\");",
15844 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15845 getLLVMStyleWithColumns(25));
15846 verifyFormat("#define A \\\n"
15848 " \"123456789\" \\\n"
15851 "#define A string s = \"1234567890\"; int i;",
15852 getLLVMStyleWithColumns(20));
15853 verifyFormat("someFunction(\n"
15855 " \"dddeeefff\");",
15856 "someFunction(\"aaabbbcc dddeeefff\");",
15857 getLLVMStyleWithColumns(25));
15860 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15861 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15862 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15863 // FIXME: unstable test case
15864 EXPECT_EQ("\"test\"\n"
15866 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15867 // FIXME: unstable test case
15868 EXPECT_EQ("\"tes\\\\\"\n"
15870 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15871 // FIXME: unstable test case
15872 EXPECT_EQ("\"\\\\\\\\\"\n"
15874 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15875 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15876 // FIXME: unstable test case
15877 EXPECT_EQ("\"\\uff01\"\n"
15879 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15880 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15881 // FIXME: unstable test case
15882 EXPECT_EQ("\"\\x000000000001\"\n"
15884 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15885 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15886 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15887 // FIXME: unstable test case
15888 EXPECT_EQ("\"test\"\n"
15891 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15892 // FIXME: unstable test case
15893 EXPECT_EQ("\"test\\000\"\n"
15896 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15899 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15900 verifyFormat("void f() {\n"
15903 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15908 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15910 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15913 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15914 verifyFormat("class X {\n"
15918 getLLVMStyleWithColumns(12));
15921 TEST_F(FormatTest, ConfigurableIndentWidth) {
15922 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15923 EightIndent.IndentWidth = 8;
15924 EightIndent.ContinuationIndentWidth = 8;
15925 verifyFormat("void f() {\n"
15926 " someFunction();\n"
15932 verifyFormat("class X {\n"
15937 verifyFormat("int x[] = {\n"
15943 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15944 verifyFormat("double\n"
15946 getLLVMStyleWithColumns(8));
15949 TEST_F(FormatTest, ConfigurableUseOfTab) {
15950 FormatStyle Tab = getLLVMStyleWithColumns(42);
15951 Tab.IndentWidth = 8;
15952 Tab.UseTab = FormatStyle::UT_Always;
15953 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15955 verifyFormat("if (aaaaaaaa && // q\n"
15958 "if (aaaaaaaa &&// q\n"
15962 verifyFormat("if (aaa && bbb) // w\n"
15964 "if(aaa&&bbb)// w\n"
15968 verifyFormat("class X {\n"
15970 "\t\tsomeFunction(parameter1,\n"
15971 "\t\t\t parameter2);\n"
15975 verifyFormat("#define A \\\n"
15976 "\tvoid f() { \\\n"
15977 "\t\tsomeFunction( \\\n"
15978 "\t\t parameter1, \\\n"
15979 "\t\t parameter2); \\\n"
15982 verifyFormat("int a;\t // x\n"
15983 "int bbbbbbbb; // x",
15986 FormatStyle TabAlignment
= Tab
;
15987 TabAlignment
.AlignConsecutiveDeclarations
.Enabled
= true;
15988 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Left
;
15989 verifyFormat("unsigned long long big;\n"
15992 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
15993 verifyFormat("unsigned long long big;\n"
15996 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Right
;
15997 verifyFormat("unsigned long long big;\n"
16002 Tab
.IndentWidth
= 8;
16003 verifyFormat("class TabWidth4Indent8 {\n"
16005 "\t\t\t\tsomeFunction(parameter1,\n"
16006 "\t\t\t\t\t\t\t parameter2);\n"
16012 Tab
.IndentWidth
= 4;
16013 verifyFormat("class TabWidth4Indent4 {\n"
16015 "\t\tsomeFunction(parameter1,\n"
16016 "\t\t\t\t\t parameter2);\n"
16022 Tab
.IndentWidth
= 4;
16023 verifyFormat("class TabWidth8Indent4 {\n"
16025 "\tsomeFunction(parameter1,\n"
16026 "\t\t parameter2);\n"
16032 Tab
.IndentWidth
= 8;
16033 verifyFormat("/*\n"
16034 "\t a\t\tcomment\n"
16035 "\t in multiple lines\n"
16038 " \t \t a\t\tcomment\t \t\n"
16039 " \t \t in multiple lines\t\n"
16043 TabAlignment
.UseTab
= FormatStyle::UT_ForIndentation
;
16044 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Left
;
16045 verifyFormat("void f() {\n"
16046 "\tunsigned long long big;\n"
16050 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
16051 verifyFormat("void f() {\n"
16052 "\tunsigned long long big;\n"
16056 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Right
;
16057 verifyFormat("void f() {\n"
16058 "\tunsigned long long big;\n"
16063 Tab
.UseTab
= FormatStyle::UT_ForIndentation
;
16065 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16066 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16067 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16068 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16069 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16070 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16073 verifyFormat("enum AA {\n"
16074 "\ta1, // Force multiple lines\n"
16079 verifyFormat("if (aaaaaaaa && // q\n"
16082 "if (aaaaaaaa &&// q\n"
16086 verifyFormat("class X {\n"
16088 "\t\tsomeFunction(parameter1,\n"
16089 "\t\t parameter2);\n"
16097 "\t\t someFunction(aaaaaaaa,\n"
16114 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16115 "\t bbbbbbbbbbbbb\n"
16120 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16125 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16126 "\t// bbbbbbbbbbbbb\n"
16129 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16134 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16135 "\t bbbbbbbbbbbbb\n"
16140 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16144 verifyNoChange("{\n"
16150 verifyNoChange("{\n"
16157 verifyFormat("void f() {\n"
16158 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
16159 "\t : bbbbbbbbbbbbbbbbbb\n"
16162 FormatStyle TabNoBreak
= Tab
;
16163 TabNoBreak
.BreakBeforeTernaryOperators
= false;
16164 verifyFormat("void f() {\n"
16165 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
16166 "\t bbbbbbbbbbbbbbbbbb\n"
16169 verifyFormat("void f() {\n"
16170 "\treturn true ?\n"
16171 "\t aaaaaaaaaaaaaaaaaaaa :\n"
16172 "\t bbbbbbbbbbbbbbbbbbbb\n"
16176 Tab
.UseTab
= FormatStyle::UT_Never
;
16177 verifyFormat("/*\n"
16179 " in multiple lines\n"
16182 " \t \t a\t\tcomment\t \t\n"
16183 " \t \t in multiple lines\t\n"
16186 verifyFormat("/* some\n"
16189 " \t \t comment */",
16191 verifyFormat("int a; /* some\n"
16193 " \t \t int a; /* some\n"
16194 " \t \t comment */",
16197 verifyFormat("int a; /* some\n"
16199 " \t \t int\ta; /* some\n"
16200 " \t \t comment */",
16202 verifyFormat("f(\"\t\t\"); /* some\n"
16204 " \t \t f(\"\t\t\"); /* some\n"
16205 " \t \t comment */",
16221 Tab
.UseTab
= FormatStyle::UT_ForContinuationAndIndentation
;
16223 Tab
.IndentWidth
= 8;
16224 verifyFormat("if (aaaaaaaa && // q\n"
16227 "if (aaaaaaaa &&// q\n"
16231 verifyFormat("if (aaa && bbb) // w\n"
16233 "if(aaa&&bbb)// w\n"
16236 verifyFormat("class X {\n"
16238 "\t\tsomeFunction(parameter1,\n"
16239 "\t\t\t parameter2);\n"
16243 verifyFormat("#define A \\\n"
16244 "\tvoid f() { \\\n"
16245 "\t\tsomeFunction( \\\n"
16246 "\t\t parameter1, \\\n"
16247 "\t\t parameter2); \\\n"
16251 Tab
.IndentWidth
= 8;
16252 verifyFormat("class TabWidth4Indent8 {\n"
16254 "\t\t\t\tsomeFunction(parameter1,\n"
16255 "\t\t\t\t\t\t\t parameter2);\n"
16260 Tab
.IndentWidth
= 4;
16261 verifyFormat("class TabWidth4Indent4 {\n"
16263 "\t\tsomeFunction(parameter1,\n"
16264 "\t\t\t\t\t parameter2);\n"
16269 Tab
.IndentWidth
= 4;
16270 verifyFormat("class TabWidth8Indent4 {\n"
16272 "\tsomeFunction(parameter1,\n"
16273 "\t\t parameter2);\n"
16278 Tab
.IndentWidth
= 8;
16279 verifyFormat("/*\n"
16280 "\t a\t\tcomment\n"
16281 "\t in multiple lines\n"
16284 " \t \t a\t\tcomment\t \t\n"
16285 " \t \t in multiple lines\t\n"
16289 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16290 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16291 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16292 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16293 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16294 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16297 verifyFormat("enum AA {\n"
16298 "\ta1, // Force multiple lines\n"
16303 verifyFormat("if (aaaaaaaa && // q\n"
16306 "if (aaaaaaaa &&// q\n"
16310 verifyFormat("class X {\n"
16312 "\t\tsomeFunction(parameter1,\n"
16313 "\t\t\t parameter2);\n"
16321 "\t\t someFunction(aaaaaaaa,\n"
16322 "\t\t\t\t bbbbbbb);\n"
16338 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16339 "\t bbbbbbbbbbbbb\n"
16344 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16349 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16350 "\t// bbbbbbbbbbbbb\n"
16353 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16358 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16359 "\t bbbbbbbbbbbbb\n"
16364 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16368 verifyNoChange("{\n"
16374 verifyNoChange("{\n"
16380 verifyFormat("/* some\n"
16383 " \t \t comment */",
16385 verifyFormat("int a; /* some\n"
16387 " \t \t int a; /* some\n"
16388 " \t \t comment */",
16390 verifyFormat("int a; /* some\n"
16392 " \t \t int\ta; /* some\n"
16393 " \t \t comment */",
16395 verifyFormat("f(\"\t\t\"); /* some\n"
16397 " \t \t f(\"\t\t\"); /* some\n"
16398 " \t \t comment */",
16414 Tab
.IndentWidth
= 2;
16426 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16427 "\t\tbbbbbbbbbbbbb\n"
16432 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16436 Tab
.AlignConsecutiveAssignments
.Enabled
= true;
16437 Tab
.AlignConsecutiveDeclarations
.Enabled
= true;
16439 Tab
.IndentWidth
= 4;
16440 verifyFormat("class Assign {\n"
16442 "\t\tint x = 123;\n"
16443 "\t\tint random = 4;\n"
16444 "\t\tstd::string alphabet =\n"
16445 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16450 Tab
.UseTab
= FormatStyle::UT_AlignWithSpaces
;
16452 Tab
.IndentWidth
= 8;
16453 verifyFormat("if (aaaaaaaa && // q\n"
16456 "if (aaaaaaaa &&// q\n"
16460 verifyFormat("if (aaa && bbb) // w\n"
16462 "if(aaa&&bbb)// w\n"
16465 verifyFormat("class X {\n"
16467 "\t\tsomeFunction(parameter1,\n"
16468 "\t\t parameter2);\n"
16472 verifyFormat("#define A \\\n"
16473 "\tvoid f() { \\\n"
16474 "\t\tsomeFunction( \\\n"
16475 "\t\t parameter1, \\\n"
16476 "\t\t parameter2); \\\n"
16480 Tab
.IndentWidth
= 8;
16481 verifyFormat("class TabWidth4Indent8 {\n"
16483 "\t\t\t\tsomeFunction(parameter1,\n"
16484 "\t\t\t\t parameter2);\n"
16489 Tab
.IndentWidth
= 4;
16490 verifyFormat("class TabWidth4Indent4 {\n"
16492 "\t\tsomeFunction(parameter1,\n"
16493 "\t\t parameter2);\n"
16498 Tab
.IndentWidth
= 4;
16499 verifyFormat("class TabWidth8Indent4 {\n"
16501 "\tsomeFunction(parameter1,\n"
16502 "\t parameter2);\n"
16507 Tab
.IndentWidth
= 8;
16508 verifyFormat("/*\n"
16510 " in multiple lines\n"
16513 " \t \t a\t\tcomment\t \t\n"
16514 " \t \t in multiple lines\t\n"
16518 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16519 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16520 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16521 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16522 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16523 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
16526 verifyFormat("enum AA {\n"
16527 "\ta1, // Force multiple lines\n"
16532 verifyFormat("if (aaaaaaaa && // q\n"
16535 "if (aaaaaaaa &&// q\n"
16539 verifyFormat("class X {\n"
16541 "\t\tsomeFunction(parameter1,\n"
16542 "\t\t parameter2);\n"
16550 "\t\t someFunction(aaaaaaaa,\n"
16567 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16568 "\t bbbbbbbbbbbbb\n"
16573 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16578 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16579 "\t// bbbbbbbbbbbbb\n"
16582 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16587 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16588 "\t bbbbbbbbbbbbb\n"
16593 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16597 verifyNoChange("{\n"
16603 verifyNoChange("{\n"
16609 verifyFormat("/* some\n"
16612 " \t \t comment */",
16614 verifyFormat("int a; /* some\n"
16616 " \t \t int a; /* some\n"
16617 " \t \t comment */",
16619 verifyFormat("int a; /* some\n"
16621 " \t \t int\ta; /* some\n"
16622 " \t \t comment */",
16624 verifyFormat("f(\"\t\t\"); /* some\n"
16626 " \t \t f(\"\t\t\"); /* some\n"
16627 " \t \t comment */",
16643 Tab
.IndentWidth
= 2;
16655 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16656 "\t bbbbbbbbbbbbb\n"
16661 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16665 Tab
.AlignConsecutiveAssignments
.Enabled
= true;
16666 Tab
.AlignConsecutiveDeclarations
.Enabled
= true;
16668 Tab
.IndentWidth
= 4;
16669 verifyFormat("class Assign {\n"
16671 "\t\tint x = 123;\n"
16672 "\t\tint random = 4;\n"
16673 "\t\tstd::string alphabet =\n"
16674 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16678 Tab
.AlignOperands
= FormatStyle::OAS_Align
;
16679 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16680 " cccccccccccccccccccc;",
16683 verifyFormat("int aaaaaaaaaa =\n"
16684 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16686 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16687 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16688 " : 333333333333333;",
16690 Tab
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
16691 Tab
.AlignOperands
= FormatStyle::OAS_AlignAfterOperator
;
16692 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16693 " + cccccccccccccccccccc;",
16697 TEST_F(FormatTest
, ZeroTabWidth
) {
16698 FormatStyle Tab
= getLLVMStyleWithColumns(42);
16699 Tab
.IndentWidth
= 8;
16700 Tab
.UseTab
= FormatStyle::UT_Never
;
16702 verifyFormat("void a() {\n"
16703 " // line starts with '\t'\n"
16706 "\t// line starts with '\t'\n"
16710 verifyFormat("void a() {\n"
16711 " // line starts with '\t'\n"
16714 "\t\t// line starts with '\t'\n"
16718 Tab
.UseTab
= FormatStyle::UT_ForIndentation
;
16719 verifyFormat("void a() {\n"
16720 " // line starts with '\t'\n"
16723 "\t// line starts with '\t'\n"
16727 verifyFormat("void a() {\n"
16728 " // line starts with '\t'\n"
16731 "\t\t// line starts with '\t'\n"
16735 Tab
.UseTab
= FormatStyle::UT_ForContinuationAndIndentation
;
16736 verifyFormat("void a() {\n"
16737 " // line starts with '\t'\n"
16740 "\t// line starts with '\t'\n"
16744 verifyFormat("void a() {\n"
16745 " // line starts with '\t'\n"
16748 "\t\t// line starts with '\t'\n"
16752 Tab
.UseTab
= FormatStyle::UT_AlignWithSpaces
;
16753 verifyFormat("void a() {\n"
16754 " // line starts with '\t'\n"
16757 "\t// line starts with '\t'\n"
16761 verifyFormat("void a() {\n"
16762 " // line starts with '\t'\n"
16765 "\t\t// line starts with '\t'\n"
16769 Tab
.UseTab
= FormatStyle::UT_Always
;
16770 verifyFormat("void a() {\n"
16771 "// line starts with '\t'\n"
16774 "\t// line starts with '\t'\n"
16778 verifyFormat("void a() {\n"
16779 "// line starts with '\t'\n"
16782 "\t\t// line starts with '\t'\n"
16787 TEST_F(FormatTest
, CalculatesOriginalColumn
) {
16788 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16791 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16794 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16797 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16800 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16804 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16808 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16811 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16816 TEST_F(FormatTest
, ConfigurableSpaceBeforeParens
) {
16817 FormatStyle NoSpace
= getLLVMStyle();
16818 NoSpace
.SpaceBeforeParens
= FormatStyle::SBPO_Never
;
16820 verifyFormat("while(true)\n"
16823 verifyFormat("for(;;)\n"
16826 verifyFormat("if(true)\n"
16831 verifyFormat("do {\n"
16832 " do_something();\n"
16833 "} while(something());",
16835 verifyFormat("switch(x) {\n"
16840 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace
);
16841 verifyFormat("size_t x = sizeof(x);", NoSpace
);
16842 verifyFormat("auto f(int x) -> decltype(x);", NoSpace
);
16843 verifyFormat("auto f(int x) -> typeof(x);", NoSpace
);
16844 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace
);
16845 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace
);
16846 verifyFormat("int f(T x) noexcept(x.create());", NoSpace
);
16847 verifyFormat("alignas(128) char a[128];", NoSpace
);
16848 verifyFormat("size_t x = alignof(MyType);", NoSpace
);
16849 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace
);
16850 verifyFormat("int f() throw(Deprecated);", NoSpace
);
16851 verifyFormat("typedef void (*cb)(int);", NoSpace
);
16852 verifyFormat("T A::operator()();", NoSpace
);
16853 verifyFormat("X A::operator++(T);", NoSpace
);
16854 verifyFormat("auto lambda = []() { return 0; };", NoSpace
);
16855 verifyFormat("#if (foo || bar) && baz\n"
16856 "#elif ((a || b) && c) || d\n"
16860 FormatStyle Space
= getLLVMStyle();
16861 Space
.SpaceBeforeParens
= FormatStyle::SBPO_Always
;
16863 verifyFormat("int f ();", Space
);
16864 verifyFormat("bool operator< ();", Space
);
16865 verifyFormat("bool operator> ();", Space
);
16866 verifyFormat("void f (int a, T b) {\n"
16871 verifyFormat("if (true)\n"
16876 verifyFormat("do {\n"
16877 " do_something ();\n"
16878 "} while (something ());",
16880 verifyFormat("switch (x) {\n"
16885 verifyFormat("A::A () : a (1) {}", Space
);
16886 verifyFormat("void f () __attribute__ ((asdf));", Space
);
16887 verifyFormat("*(&a + 1);\n"
16889 "a[(b + c) * d];\n"
16890 "(((a + 1) * 2) + 3) * 4;",
16892 verifyFormat("#define A(x) x", Space
);
16893 verifyFormat("#define A (x) x", Space
);
16894 verifyFormat("#if defined(x)\n"
16897 verifyFormat("auto i = std::make_unique<int> (5);", Space
);
16898 verifyFormat("size_t x = sizeof (x);", Space
);
16899 verifyFormat("auto f (int x) -> decltype (x);", Space
);
16900 verifyFormat("auto f (int x) -> typeof (x);", Space
);
16901 verifyFormat("auto f (int x) -> _Atomic (x);", Space
);
16902 verifyFormat("auto f (int x) -> __underlying_type (x);", Space
);
16903 verifyFormat("int f (T x) noexcept (x.create ());", Space
);
16904 verifyFormat("alignas (128) char a[128];", Space
);
16905 verifyFormat("size_t x = alignof (MyType);", Space
);
16906 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space
);
16907 verifyFormat("int f () throw (Deprecated);", Space
);
16908 verifyFormat("typedef void (*cb) (int);", Space
);
16909 verifyFormat("T A::operator() ();", Space
);
16910 verifyFormat("X A::operator++ (T);", Space
);
16911 verifyFormat("auto lambda = [] () { return 0; };", Space
);
16912 verifyFormat("int x = int (y);", Space
);
16913 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space
);
16914 verifyFormat("__builtin_LINE ()", Space
);
16915 verifyFormat("__builtin_UNKNOWN ()", Space
);
16917 FormatStyle SomeSpace
= getLLVMStyle();
16918 SomeSpace
.SpaceBeforeParens
= FormatStyle::SBPO_NonEmptyParentheses
;
16920 verifyFormat("[]() -> float {}", SomeSpace
);
16921 verifyFormat("[] (auto foo) {}", SomeSpace
);
16922 verifyFormat("[foo]() -> int {}", SomeSpace
);
16923 verifyFormat("int f();", SomeSpace
);
16924 verifyFormat("void f (int a, T b) {\n"
16929 verifyFormat("if (true)\n"
16934 verifyFormat("do {\n"
16935 " do_something();\n"
16936 "} while (something());",
16938 verifyFormat("switch (x) {\n"
16943 verifyFormat("A::A() : a (1) {}", SomeSpace
);
16944 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace
);
16945 verifyFormat("*(&a + 1);\n"
16947 "a[(b + c) * d];\n"
16948 "(((a + 1) * 2) + 3) * 4;",
16950 verifyFormat("#define A(x) x", SomeSpace
);
16951 verifyFormat("#define A (x) x", SomeSpace
);
16952 verifyFormat("#if defined(x)\n"
16955 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace
);
16956 verifyFormat("size_t x = sizeof (x);", SomeSpace
);
16957 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace
);
16958 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace
);
16959 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace
);
16960 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace
);
16961 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace
);
16962 verifyFormat("alignas (128) char a[128];", SomeSpace
);
16963 verifyFormat("size_t x = alignof (MyType);", SomeSpace
);
16964 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16966 verifyFormat("int f() throw (Deprecated);", SomeSpace
);
16967 verifyFormat("typedef void (*cb) (int);", SomeSpace
);
16968 verifyFormat("T A::operator()();", SomeSpace
);
16969 verifyFormat("X A::operator++ (T);", SomeSpace
);
16970 verifyFormat("int x = int (y);", SomeSpace
);
16971 verifyFormat("auto lambda = []() { return 0; };", SomeSpace
);
16973 FormatStyle SpaceControlStatements
= getLLVMStyle();
16974 SpaceControlStatements
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16975 SpaceControlStatements
.SpaceBeforeParensOptions
.AfterControlStatements
= true;
16977 verifyFormat("while (true)\n"
16979 SpaceControlStatements
);
16980 verifyFormat("if (true)\n"
16984 SpaceControlStatements
);
16985 verifyFormat("for (;;) {\n"
16986 " do_something();\n"
16988 SpaceControlStatements
);
16989 verifyFormat("do {\n"
16990 " do_something();\n"
16991 "} while (something());",
16992 SpaceControlStatements
);
16993 verifyFormat("switch (x) {\n"
16997 SpaceControlStatements
);
16999 FormatStyle SpaceFuncDecl
= getLLVMStyle();
17000 SpaceFuncDecl
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17001 SpaceFuncDecl
.SpaceBeforeParensOptions
.AfterFunctionDeclarationName
= true;
17003 verifyFormat("int f ();", SpaceFuncDecl
);
17004 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl
);
17005 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl
);
17006 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl
);
17007 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl
);
17008 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl
);
17009 verifyFormat("#define A(x) x", SpaceFuncDecl
);
17010 verifyFormat("#define A (x) x", SpaceFuncDecl
);
17011 verifyFormat("#if defined(x)\n"
17014 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl
);
17015 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl
);
17016 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl
);
17017 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl
);
17018 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl
);
17019 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl
);
17020 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl
);
17021 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl
);
17022 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl
);
17023 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17025 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl
);
17026 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl
);
17027 verifyFormat("T A::operator()();", SpaceFuncDecl
);
17028 verifyFormat("X A::operator++(T);", SpaceFuncDecl
);
17029 verifyFormat("T A::operator()() {}", SpaceFuncDecl
);
17030 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl
);
17031 verifyFormat("int x = int(y);", SpaceFuncDecl
);
17032 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
17035 FormatStyle SpaceFuncDef
= getLLVMStyle();
17036 SpaceFuncDef
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17037 SpaceFuncDef
.SpaceBeforeParensOptions
.AfterFunctionDefinitionName
= true;
17039 verifyFormat("int f();", SpaceFuncDef
);
17040 verifyFormat("void f (int a, T b) {}", SpaceFuncDef
);
17041 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef
);
17042 verifyFormat("A::A () : a(1) {}", SpaceFuncDef
);
17043 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef
);
17044 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef
);
17045 verifyFormat("#define A(x) x", SpaceFuncDef
);
17046 verifyFormat("#define A (x) x", SpaceFuncDef
);
17047 verifyFormat("#if defined(x)\n"
17050 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef
);
17051 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef
);
17052 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef
);
17053 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef
);
17054 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef
);
17055 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef
);
17056 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef
);
17057 verifyFormat("alignas(128) char a[128];", SpaceFuncDef
);
17058 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef
);
17059 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
17061 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef
);
17062 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef
);
17063 verifyFormat("T A::operator()();", SpaceFuncDef
);
17064 verifyFormat("X A::operator++(T);", SpaceFuncDef
);
17065 verifyFormat("T A::operator()() {}", SpaceFuncDef
);
17066 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef
);
17067 verifyFormat("int x = int(y);", SpaceFuncDef
);
17068 verifyFormat("void foo::bar () {}", SpaceFuncDef
);
17069 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
17072 FormatStyle SpaceIfMacros
= getLLVMStyle();
17073 SpaceIfMacros
.IfMacros
.clear();
17074 SpaceIfMacros
.IfMacros
.push_back("MYIF");
17075 SpaceIfMacros
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17076 SpaceIfMacros
.SpaceBeforeParensOptions
.AfterIfMacros
= true;
17077 verifyFormat("MYIF (a)\n return;", SpaceIfMacros
);
17078 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros
);
17079 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros
);
17081 FormatStyle SpaceForeachMacros
= getLLVMStyle();
17082 EXPECT_EQ(SpaceForeachMacros
.AllowShortBlocksOnASingleLine
,
17083 FormatStyle::SBS_Never
);
17084 EXPECT_EQ(SpaceForeachMacros
.AllowShortLoopsOnASingleLine
, false);
17085 SpaceForeachMacros
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17086 SpaceForeachMacros
.SpaceBeforeParensOptions
.AfterForeachMacros
= true;
17087 verifyFormat("for (;;) {\n"
17089 SpaceForeachMacros
);
17090 verifyFormat("foreach (Item *item, itemlist) {\n"
17092 SpaceForeachMacros
);
17093 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
17095 SpaceForeachMacros
);
17096 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
17098 SpaceForeachMacros
);
17099 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros
);
17101 FormatStyle SomeSpace2
= getLLVMStyle();
17102 SomeSpace2
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17103 SomeSpace2
.SpaceBeforeParensOptions
.BeforeNonEmptyParentheses
= true;
17104 verifyFormat("[]() -> float {}", SomeSpace2
);
17105 verifyFormat("[] (auto foo) {}", SomeSpace2
);
17106 verifyFormat("[foo]() -> int {}", SomeSpace2
);
17107 verifyFormat("int f();", SomeSpace2
);
17108 verifyFormat("void f (int a, T b) {\n"
17113 verifyFormat("if (true)\n"
17118 verifyFormat("do {\n"
17119 " do_something();\n"
17120 "} while (something());",
17122 verifyFormat("switch (x) {\n"
17127 verifyFormat("A::A() : a (1) {}", SomeSpace2
);
17128 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2
);
17129 verifyFormat("*(&a + 1);\n"
17131 "a[(b + c) * d];\n"
17132 "(((a + 1) * 2) + 3) * 4;",
17134 verifyFormat("#define A(x) x", SomeSpace2
);
17135 verifyFormat("#define A (x) x", SomeSpace2
);
17136 verifyFormat("#if defined(x)\n"
17139 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2
);
17140 verifyFormat("size_t x = sizeof (x);", SomeSpace2
);
17141 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2
);
17142 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2
);
17143 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2
);
17144 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2
);
17145 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2
);
17146 verifyFormat("alignas (128) char a[128];", SomeSpace2
);
17147 verifyFormat("size_t x = alignof (MyType);", SomeSpace2
);
17148 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
17150 verifyFormat("int f() throw (Deprecated);", SomeSpace2
);
17151 verifyFormat("typedef void (*cb) (int);", SomeSpace2
);
17152 verifyFormat("T A::operator()();", SomeSpace2
);
17153 verifyFormat("X A::operator++ (T);", SomeSpace2
);
17154 verifyFormat("int x = int (y);", SomeSpace2
);
17155 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2
);
17157 FormatStyle SpaceAfterOverloadedOperator
= getLLVMStyle();
17158 SpaceAfterOverloadedOperator
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17159 SpaceAfterOverloadedOperator
.SpaceBeforeParensOptions
17160 .AfterOverloadedOperator
= true;
17162 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator
);
17163 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator
);
17164 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator
);
17165 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator
);
17167 SpaceAfterOverloadedOperator
.SpaceBeforeParensOptions
17168 .AfterOverloadedOperator
= false;
17170 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator
);
17171 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator
);
17172 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator
);
17173 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator
);
17175 auto SpaceAfterRequires
= getLLVMStyle();
17176 SpaceAfterRequires
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
17178 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
);
17180 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInExpression
);
17181 verifyFormat("void f(auto x)\n"
17182 " requires requires(int i) { x + i; }\n"
17184 SpaceAfterRequires
);
17185 verifyFormat("void f(auto x)\n"
17186 " requires(requires(int i) { x + i; })\n"
17188 SpaceAfterRequires
);
17189 verifyFormat("if (requires(int i) { x + i; })\n"
17191 SpaceAfterRequires
);
17192 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires
);
17193 verifyFormat("template <typename T>\n"
17194 " requires(Foo<T>)\n"
17196 SpaceAfterRequires
);
17198 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= true;
17199 verifyFormat("void f(auto x)\n"
17200 " requires requires(int i) { x + i; }\n"
17202 SpaceAfterRequires
);
17203 verifyFormat("void f(auto x)\n"
17204 " requires (requires(int i) { x + i; })\n"
17206 SpaceAfterRequires
);
17207 verifyFormat("if (requires(int i) { x + i; })\n"
17209 SpaceAfterRequires
);
17210 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires
);
17211 verifyFormat("template <typename T>\n"
17212 " requires (Foo<T>)\n"
17214 SpaceAfterRequires
);
17216 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= false;
17217 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInExpression
= true;
17218 verifyFormat("void f(auto x)\n"
17219 " requires requires (int i) { x + i; }\n"
17221 SpaceAfterRequires
);
17222 verifyFormat("void f(auto x)\n"
17223 " requires(requires (int i) { x + i; })\n"
17225 SpaceAfterRequires
);
17226 verifyFormat("if (requires (int i) { x + i; })\n"
17228 SpaceAfterRequires
);
17229 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires
);
17230 verifyFormat("template <typename T>\n"
17231 " requires(Foo<T>)\n"
17233 SpaceAfterRequires
);
17235 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= true;
17236 verifyFormat("void f(auto x)\n"
17237 " requires requires (int i) { x + i; }\n"
17239 SpaceAfterRequires
);
17240 verifyFormat("void f(auto x)\n"
17241 " requires (requires (int i) { x + i; })\n"
17243 SpaceAfterRequires
);
17244 verifyFormat("if (requires (int i) { x + i; })\n"
17246 SpaceAfterRequires
);
17247 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires
);
17248 verifyFormat("template <typename T>\n"
17249 " requires (Foo<T>)\n"
17251 SpaceAfterRequires
);
17254 TEST_F(FormatTest
, SpaceAfterLogicalNot
) {
17255 FormatStyle Spaces
= getLLVMStyle();
17256 Spaces
.SpaceAfterLogicalNot
= true;
17258 verifyFormat("bool x = ! y", Spaces
);
17259 verifyFormat("if (! isFailure())", Spaces
);
17260 verifyFormat("if (! (a && b))", Spaces
);
17261 verifyFormat("\"Error!\"", Spaces
);
17262 verifyFormat("! ! x", Spaces
);
17265 TEST_F(FormatTest
, ConfigurableSpacesInParens
) {
17266 FormatStyle Spaces
= getLLVMStyle();
17268 verifyFormat("do_something(::globalVar);", Spaces
);
17269 verifyFormat("call(x, y, z);", Spaces
);
17270 verifyFormat("call();", Spaces
);
17271 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
17272 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
17274 verifyFormat("while ((bool)1)\n"
17277 verifyFormat("for (;;)\n"
17280 verifyFormat("if (true)\n"
17285 verifyFormat("do {\n"
17286 " do_something((int)i);\n"
17287 "} while (something());",
17289 verifyFormat("switch (x) {\n"
17294 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17295 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17296 verifyFormat("void f() __attribute__((asdf));", Spaces
);
17297 verifyFormat("x = (int32)y;", Spaces
);
17298 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces
);
17299 verifyFormat("decltype(x) y = 42;", Spaces
);
17300 verifyFormat("decltype((x)) y = z;", Spaces
);
17301 verifyFormat("decltype((foo())) a = foo();", Spaces
);
17302 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces
);
17303 verifyFormat("if ((x - y) && (a ^ b))\n"
17306 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17309 verifyFormat("switch (x / (y + z)) {\n"
17315 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17316 Spaces
.SpacesInParensOptions
= {};
17317 Spaces
.SpacesInParensOptions
.Other
= true;
17319 EXPECT_FALSE(Spaces
.SpacesInParensOptions
.InConditionalStatements
);
17320 verifyFormat("if (a)\n"
17324 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
17325 verifyFormat("do_something( ::globalVar );", Spaces
);
17326 verifyFormat("call( x, y, z );", Spaces
);
17327 verifyFormat("call();", Spaces
);
17328 verifyFormat("std::function<void( int, int )> callback;", Spaces
);
17329 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
17331 verifyFormat("while ( (bool)1 )\n"
17334 verifyFormat("for ( ;; )\n"
17337 verifyFormat("if ( true )\n"
17339 "else if ( true )\n"
17342 verifyFormat("do {\n"
17343 " do_something( (int)i );\n"
17344 "} while ( something() );",
17346 verifyFormat("switch ( x ) {\n"
17351 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces
);
17352 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces
);
17353 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces
);
17354 verifyFormat("x = (int32)y;", Spaces
);
17355 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces
);
17356 verifyFormat("decltype( x ) y = 42;", Spaces
);
17357 verifyFormat("decltype( ( x ) ) y = z;", Spaces
);
17358 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces
);
17359 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces
);
17360 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n"
17363 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n"
17366 verifyFormat("switch ( x / ( y + z ) ) {\n"
17372 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17373 Spaces
.SpacesInParensOptions
= {};
17374 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
17375 verifyFormat("Type *A = ( Type * )P;", Spaces
);
17376 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces
);
17377 verifyFormat("x = ( int32 )y;", Spaces
);
17378 verifyFormat("throw ( int32 )x;", Spaces
);
17379 verifyFormat("int a = ( int )(2.0f);", Spaces
);
17380 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces
);
17381 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces
);
17382 verifyFormat("#define x (( int )-1)", Spaces
);
17383 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces
);
17385 // Run the first set of tests again with:
17386 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17387 Spaces
.SpacesInParensOptions
= {};
17388 Spaces
.SpacesInParensOptions
.InEmptyParentheses
= true;
17389 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
17390 verifyFormat("call(x, y, z);", Spaces
);
17391 verifyFormat("call( );", Spaces
);
17392 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
17393 verifyFormat("while (( bool )1)\n"
17396 verifyFormat("for (;;)\n"
17399 verifyFormat("if (true)\n"
17404 verifyFormat("do {\n"
17405 " do_something(( int )i);\n"
17406 "} while (something( ));",
17408 verifyFormat("switch (x) {\n"
17413 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17414 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17415 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
17416 verifyFormat("x = ( int32 )y;", Spaces
);
17417 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces
);
17418 verifyFormat("decltype(x) y = 42;", Spaces
);
17419 verifyFormat("decltype((x)) y = z;", Spaces
);
17420 verifyFormat("decltype((foo( ))) a = foo( );", Spaces
);
17421 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces
);
17422 verifyFormat("if ((x - y) && (a ^ b))\n"
17425 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n"
17428 verifyFormat("switch (x / (y + z)) {\n"
17434 // Run the first set of tests again with:
17435 Spaces
.SpaceAfterCStyleCast
= true;
17436 verifyFormat("call(x, y, z);", Spaces
);
17437 verifyFormat("call( );", Spaces
);
17438 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
17439 verifyFormat("while (( bool ) 1)\n"
17442 verifyFormat("for (;;)\n"
17445 verifyFormat("if (true)\n"
17450 verifyFormat("do {\n"
17451 " do_something(( int ) i);\n"
17452 "} while (something( ));",
17454 verifyFormat("switch (x) {\n"
17459 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces
);
17460 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces
);
17461 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces
);
17462 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces
);
17463 verifyFormat("bool *y = ( bool * ) (x);", Spaces
);
17464 verifyFormat("throw ( int32 ) x;", Spaces
);
17465 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17466 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17467 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
17469 // Run subset of tests again with:
17470 Spaces
.SpacesInParensOptions
.InCStyleCasts
= false;
17471 Spaces
.SpaceAfterCStyleCast
= true;
17472 verifyFormat("while ((bool) 1)\n"
17475 verifyFormat("do {\n"
17476 " do_something((int) i);\n"
17477 "} while (something( ));",
17480 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces
);
17481 verifyFormat("size_t idx = (size_t) a;", Spaces
);
17482 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces
);
17483 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
17484 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
17485 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
17486 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
17487 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces
);
17488 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces
);
17489 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces
);
17490 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces
);
17491 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces
);
17492 verifyFormat("throw (int32) x;", Spaces
);
17493 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17494 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
17495 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
17497 Spaces
.ColumnLimit
= 80;
17498 Spaces
.IndentWidth
= 4;
17499 Spaces
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
17500 verifyFormat("void foo( ) {\n"
17501 " size_t foo = (*(function))(\n"
17502 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17503 "BarrrrrrrrrrrrLong,\n"
17504 " FoooooooooLooooong);\n"
17507 Spaces
.SpaceAfterCStyleCast
= false;
17508 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces
);
17509 verifyFormat("size_t idx = (size_t)a;", Spaces
);
17510 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces
);
17511 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
17512 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
17513 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
17514 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
17516 verifyFormat("void foo( ) {\n"
17517 " size_t foo = (*(function))(\n"
17518 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17519 "BarrrrrrrrrrrrLong,\n"
17520 " FoooooooooLooooong);\n"
17524 Spaces
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
17525 verifyFormat("void foo( ) {\n"
17526 " size_t foo = (*(function))(\n"
17527 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
17528 "BarrrrrrrrrrrrLong,\n"
17529 " FoooooooooLooooong\n"
17533 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces
);
17534 verifyFormat("size_t idx = (size_t)a;", Spaces
);
17535 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces
);
17536 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
17537 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
17538 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
17539 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
17541 // Check ExceptDoubleParentheses spaces
17542 Spaces
.IndentWidth
= 2;
17543 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17544 Spaces
.SpacesInParensOptions
= {};
17545 Spaces
.SpacesInParensOptions
.Other
= true;
17546 Spaces
.SpacesInParensOptions
.ExceptDoubleParentheses
= true;
17547 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces
);
17548 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces
);
17549 verifyFormat("void f() __attribute__(( asdf ));", Spaces
);
17550 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces
);
17551 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces
);
17552 verifyFormat("class __declspec( dllimport ) X {};", Spaces
);
17553 verifyFormat("class __declspec(( dllimport )) X {};", Spaces
);
17554 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces
);
17555 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces
);
17556 verifyFormat("decltype( x ) y = 42;", Spaces
);
17557 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces
);
17558 verifyFormat("if (( i = j ))\n"
17559 " do_something( i );",
17562 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
17563 Spaces
.SpacesInParensOptions
= {};
17564 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
17565 Spaces
.SpacesInParensOptions
.ExceptDoubleParentheses
= true;
17566 verifyFormat("while ( (bool)1 )\n"
17569 verifyFormat("while ((i = j))\n"
17572 verifyFormat("do {\n"
17573 " do_something((int)i);\n"
17574 "} while ( something() );",
17576 verifyFormat("do {\n"
17577 " do_something((int)i);\n"
17578 "} while ((i = i + 1));",
17580 verifyFormat("if ( (x - y) && (a ^ b) )\n"
17583 verifyFormat("if ((i = j))\n"
17584 " do_something(i);",
17586 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n"
17589 verifyFormat("switch ( x / (y + z) ) {\n"
17594 verifyFormat("if constexpr ((a = b))\n"
17599 TEST_F(FormatTest
, ConfigurableSpacesInSquareBrackets
) {
17600 verifyFormat("int a[5];");
17601 verifyFormat("a[3] += 42;");
17603 FormatStyle Spaces
= getLLVMStyle();
17604 Spaces
.SpacesInSquareBrackets
= true;
17606 verifyFormat("int a[ 5 ];", Spaces
);
17607 verifyFormat("a[ 3 ] += 42;", Spaces
);
17608 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces
);
17609 verifyFormat("double &operator[](int i) { return 0; }\n"
17612 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces
);
17613 verifyFormat("int i = a[ a ][ a ]->f();", Spaces
);
17614 verifyFormat("int i = (*b)[ a ]->f();", Spaces
);
17616 verifyFormat("int c = []() -> int { return 2; }();", Spaces
);
17617 verifyFormat("return [ i, args... ] {};", Spaces
);
17618 verifyFormat("int foo = [ &bar ]() {};", Spaces
);
17619 verifyFormat("int foo = [ = ]() {};", Spaces
);
17620 verifyFormat("int foo = [ & ]() {};", Spaces
);
17621 verifyFormat("int foo = [ =, &bar ]() {};", Spaces
);
17622 verifyFormat("int foo = [ &bar, = ]() {};", Spaces
);
17625 TEST_F(FormatTest
, ConfigurableSpaceBeforeBrackets
) {
17626 FormatStyle NoSpaceStyle
= getLLVMStyle();
17627 verifyFormat("int a[5];", NoSpaceStyle
);
17628 verifyFormat("a[3] += 42;", NoSpaceStyle
);
17630 verifyFormat("int a[1];", NoSpaceStyle
);
17631 verifyFormat("int 1 [a];", NoSpaceStyle
);
17632 verifyFormat("int a[1][2];", NoSpaceStyle
);
17633 verifyFormat("a[7] = 5;", NoSpaceStyle
);
17634 verifyFormat("int a = (f())[23];", NoSpaceStyle
);
17635 verifyFormat("f([] {})", NoSpaceStyle
);
17637 FormatStyle Space
= getLLVMStyle();
17638 Space
.SpaceBeforeSquareBrackets
= true;
17639 verifyFormat("int c = []() -> int { return 2; }();", Space
);
17640 verifyFormat("return [i, args...] {};", Space
);
17642 verifyFormat("int a [5];", Space
);
17643 verifyFormat("a [3] += 42;", Space
);
17644 verifyFormat("constexpr char hello []{\"hello\"};", Space
);
17645 verifyFormat("double &operator[](int i) { return 0; }\n"
17648 verifyFormat("std::unique_ptr<int []> foo() {}", Space
);
17649 verifyFormat("int i = a [a][a]->f();", Space
);
17650 verifyFormat("int i = (*b) [a]->f();", Space
);
17652 verifyFormat("int a [1];", Space
);
17653 verifyFormat("int 1 [a];", Space
);
17654 verifyFormat("int a [1][2];", Space
);
17655 verifyFormat("a [7] = 5;", Space
);
17656 verifyFormat("int a = (f()) [23];", Space
);
17657 verifyFormat("f([] {})", Space
);
17660 TEST_F(FormatTest
, ConfigurableSpaceBeforeAssignmentOperators
) {
17661 verifyFormat("int a = 5;");
17662 verifyFormat("a += 42;");
17663 verifyFormat("a or_eq 8;");
17664 verifyFormat("xor = foo;");
17666 FormatStyle Spaces
= getLLVMStyle();
17667 Spaces
.SpaceBeforeAssignmentOperators
= false;
17668 verifyFormat("int a= 5;", Spaces
);
17669 verifyFormat("a+= 42;", Spaces
);
17670 verifyFormat("a or_eq 8;", Spaces
);
17671 verifyFormat("xor= foo;", Spaces
);
17674 TEST_F(FormatTest
, ConfigurableSpaceBeforeColon
) {
17675 verifyFormat("class Foo : public Bar {};");
17676 verifyFormat("Foo::Foo() : foo(1) {}");
17677 verifyFormat("for (auto a : b) {\n}");
17678 verifyFormat("int x = a ? b : c;");
17683 verifyFormat("switch (x) {\n"
17687 verifyFormat("switch (allBraces) {\n"
17692 " [[fallthrough]];\n"
17699 FormatStyle CtorInitializerStyle
= getLLVMStyleWithColumns(30);
17700 CtorInitializerStyle
.SpaceBeforeCtorInitializerColon
= false;
17701 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle
);
17702 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle
);
17703 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle
);
17704 verifyFormat("int x = a ? b : c;", CtorInitializerStyle
);
17709 CtorInitializerStyle
);
17710 verifyFormat("switch (x) {\n"
17714 CtorInitializerStyle
);
17715 verifyFormat("switch (allBraces) {\n"
17720 " [[fallthrough]];\n"
17726 CtorInitializerStyle
);
17727 CtorInitializerStyle
.BreakConstructorInitializers
=
17728 FormatStyle::BCIS_AfterColon
;
17729 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
17730 " aaaaaaaaaaaaaaaa(1),\n"
17731 " bbbbbbbbbbbbbbbb(2) {}",
17732 CtorInitializerStyle
);
17733 CtorInitializerStyle
.BreakConstructorInitializers
=
17734 FormatStyle::BCIS_BeforeComma
;
17735 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17736 " : aaaaaaaaaaaaaaaa(1)\n"
17737 " , bbbbbbbbbbbbbbbb(2) {}",
17738 CtorInitializerStyle
);
17739 CtorInitializerStyle
.BreakConstructorInitializers
=
17740 FormatStyle::BCIS_BeforeColon
;
17741 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17742 " : aaaaaaaaaaaaaaaa(1),\n"
17743 " bbbbbbbbbbbbbbbb(2) {}",
17744 CtorInitializerStyle
);
17745 CtorInitializerStyle
.ConstructorInitializerIndentWidth
= 0;
17746 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
17747 ": aaaaaaaaaaaaaaaa(1),\n"
17748 " bbbbbbbbbbbbbbbb(2) {}",
17749 CtorInitializerStyle
);
17751 FormatStyle InheritanceStyle
= getLLVMStyleWithColumns(30);
17752 InheritanceStyle
.SpaceBeforeInheritanceColon
= false;
17753 verifyFormat("class Foo: public Bar {};", InheritanceStyle
);
17754 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle
);
17755 verifyFormat("for (auto a : b) {\n}", InheritanceStyle
);
17756 verifyFormat("int x = a ? b : c;", InheritanceStyle
);
17762 verifyFormat("switch (x) {\n"
17767 verifyFormat("switch (allBraces) {\n"
17772 " [[fallthrough]];\n"
17779 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_AfterComma
;
17780 verifyFormat("class Foooooooooooooooooooooo\n"
17781 " : public aaaaaaaaaaaaaaaaaa,\n"
17782 " public bbbbbbbbbbbbbbbbbb {\n"
17785 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_AfterColon
;
17786 verifyFormat("class Foooooooooooooooooooooo:\n"
17787 " public aaaaaaaaaaaaaaaaaa,\n"
17788 " public bbbbbbbbbbbbbbbbbb {\n"
17791 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_BeforeComma
;
17792 verifyFormat("class Foooooooooooooooooooooo\n"
17793 " : public aaaaaaaaaaaaaaaaaa\n"
17794 " , public bbbbbbbbbbbbbbbbbb {\n"
17797 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_BeforeColon
;
17798 verifyFormat("class Foooooooooooooooooooooo\n"
17799 " : public aaaaaaaaaaaaaaaaaa,\n"
17800 " public bbbbbbbbbbbbbbbbbb {\n"
17803 InheritanceStyle
.ConstructorInitializerIndentWidth
= 0;
17804 verifyFormat("class Foooooooooooooooooooooo\n"
17805 ": public aaaaaaaaaaaaaaaaaa,\n"
17806 " public bbbbbbbbbbbbbbbbbb {}",
17809 FormatStyle ForLoopStyle
= getLLVMStyle();
17810 ForLoopStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17811 verifyFormat("class Foo : public Bar {};", ForLoopStyle
);
17812 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle
);
17813 verifyFormat("for (auto a: b) {\n}", ForLoopStyle
);
17814 verifyFormat("int x = a ? b : c;", ForLoopStyle
);
17820 verifyFormat("switch (x) {\n"
17825 verifyFormat("switch (allBraces) {\n"
17830 " [[fallthrough]];\n"
17838 FormatStyle CaseStyle
= getLLVMStyle();
17839 CaseStyle
.SpaceBeforeCaseColon
= true;
17840 verifyFormat("class Foo : public Bar {};", CaseStyle
);
17841 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle
);
17842 verifyFormat("for (auto a : b) {\n}", CaseStyle
);
17843 verifyFormat("int x = a ? b : c;", CaseStyle
);
17844 verifyFormat("switch (x) {\n"
17849 verifyFormat("switch (allBraces) {\n"
17854 " [[fallthrough]];\n"
17861 // Goto labels should not be affected.
17862 verifyFormat("switch (x) {\n"
17867 verifyFormat("switch (x) {\n"
17868 "goto_label: { break; }\n"
17875 FormatStyle NoSpaceStyle
= getLLVMStyle();
17876 EXPECT_EQ(NoSpaceStyle
.SpaceBeforeCaseColon
, false);
17877 NoSpaceStyle
.SpaceBeforeCtorInitializerColon
= false;
17878 NoSpaceStyle
.SpaceBeforeInheritanceColon
= false;
17879 NoSpaceStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17880 verifyFormat("class Foo: public Bar {};", NoSpaceStyle
);
17881 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle
);
17882 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle
);
17883 verifyFormat("int x = a ? b : c;", NoSpaceStyle
);
17889 verifyFormat("switch (x) {\n"
17894 verifyFormat("switch (allBraces) {\n"
17899 " [[fallthrough]];\n"
17907 FormatStyle InvertedSpaceStyle
= getLLVMStyle();
17908 InvertedSpaceStyle
.SpaceBeforeCaseColon
= true;
17909 InvertedSpaceStyle
.SpaceBeforeCtorInitializerColon
= false;
17910 InvertedSpaceStyle
.SpaceBeforeInheritanceColon
= false;
17911 InvertedSpaceStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17912 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle
);
17913 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle
);
17914 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle
);
17915 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle
);
17920 InvertedSpaceStyle
);
17921 verifyFormat("switch (x) {\n"
17929 InvertedSpaceStyle
);
17930 verifyFormat("switch (allBraces) {\n"
17935 " [[fallthrough]];\n"
17941 InvertedSpaceStyle
);
17944 TEST_F(FormatTest
, ConfigurableSpaceAroundPointerQualifiers
) {
17945 FormatStyle Style
= getLLVMStyle();
17947 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
17948 Style
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Default
;
17949 verifyFormat("void* const* x = NULL;", Style
);
17951 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17953 Style.PointerAlignment = FormatStyle::Pointers; \
17954 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17955 verifyFormat(Code, Style); \
17958 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left
, SAPQ_Default
);
17959 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right
, SAPQ_Default
);
17960 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Default
);
17962 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left
, SAPQ_Before
);
17963 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right
, SAPQ_Before
);
17964 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Before
);
17966 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left
, SAPQ_After
);
17967 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right
, SAPQ_After
);
17968 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_After
);
17970 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left
, SAPQ_Both
);
17971 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right
, SAPQ_Both
);
17972 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Both
);
17974 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left
, SAPQ_Default
);
17975 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
,
17977 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17980 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left
, SAPQ_Before
);
17981 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
,
17983 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17986 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left
, SAPQ_After
);
17987 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
, SAPQ_After
);
17988 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17991 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left
, SAPQ_Both
);
17992 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
, SAPQ_Both
);
17993 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
, SAPQ_Both
);
17995 #undef verifyQualifierSpaces
17997 FormatStyle Spaces
= getLLVMStyle();
17998 Spaces
.AttributeMacros
.push_back("qualified");
17999 Spaces
.PointerAlignment
= FormatStyle::PAS_Right
;
18000 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Default
;
18001 verifyFormat("SomeType *volatile *a = NULL;", Spaces
);
18002 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
18003 verifyFormat("std::vector<SomeType *const *> x;", Spaces
);
18004 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces
);
18005 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18006 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Before
;
18007 verifyFormat("SomeType * volatile *a = NULL;", Spaces
);
18008 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces
);
18009 verifyFormat("std::vector<SomeType * const *> x;", Spaces
);
18010 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces
);
18011 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18013 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
18014 Spaces
.PointerAlignment
= FormatStyle::PAS_Left
;
18015 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Before
;
18016 verifyFormat("SomeType* volatile* a = NULL;", Spaces
);
18017 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces
);
18018 verifyFormat("std::vector<SomeType* const*> x;", Spaces
);
18019 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces
);
18020 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18021 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
18022 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_After
;
18023 verifyFormat("SomeType* volatile * a = NULL;", Spaces
);
18024 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces
);
18025 verifyFormat("std::vector<SomeType* const *> x;", Spaces
);
18026 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces
);
18027 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18029 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
18030 Spaces
.PointerAlignment
= FormatStyle::PAS_Middle
;
18031 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_After
;
18032 verifyFormat("SomeType * volatile * a = NULL;", Spaces
);
18033 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces
);
18034 verifyFormat("std::vector<SomeType * const *> x;", Spaces
);
18035 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces
);
18036 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
18039 TEST_F(FormatTest
, AlignConsecutiveMacros
) {
18040 FormatStyle Style
= getLLVMStyle();
18041 Style
.AlignConsecutiveAssignments
.Enabled
= true;
18042 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
18044 verifyFormat("#define a 3\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 Style
.AlignConsecutiveMacros
.Enabled
= true;
18067 verifyFormat("#define a 3\n"
18072 verifyFormat("#define true 1\n"
18076 verifyFormat("#define f(x) (x * x)\n"
18077 "#define fff(x, y, z) (x * y + z)\n"
18078 "#define ffff(x, y) (x - y)",
18081 verifyFormat("#define foo(x, y) (x + y)\n"
18082 "#define bar (5, 6)(2 + 2)",
18085 verifyFormat("#define a 3\n"
18087 "#define ccc (5)\n"
18088 "#define f(x) (x * x)\n"
18089 "#define fff(x, y, z) (x * y + z)\n"
18090 "#define ffff(x, y) (x - y)",
18093 verifyFormat("#define a 5\n"
18094 "#define foo(x, y) (x + y)\n"
18095 "#define CCC (6)\n"
18096 "auto lambda = []() {\n"
18110 Style
.AlignConsecutiveMacros
.Enabled
= false;
18111 Style
.ColumnLimit
= 20;
18113 verifyFormat("#define a \\\n"
18114 " \"aabbbbbbbbbbbb\"\n"
18116 " \"aabbbbbbbbbbbb\" \\\n"
18117 " \"ccddeeeeeeeee\"\n"
18119 " \"QQQQQQQQQQQQQ\" \\\n"
18120 " \"FFFFFFFFFFFFF\" \\\n"
18124 Style
.AlignConsecutiveMacros
.Enabled
= true;
18125 verifyFormat("#define a \\\n"
18126 " \"aabbbbbbbbbbbb\"\n"
18128 " \"aabbbbbbbbbbbb\" \\\n"
18129 " \"ccddeeeeeeeee\"\n"
18131 " \"QQQQQQQQQQQQQ\" \\\n"
18132 " \"FFFFFFFFFFFFF\" \\\n"
18136 // Test across comments
18137 Style
.MaxEmptyLinesToKeep
= 10;
18138 Style
.ReflowComments
= FormatStyle::RCS_Never
;
18139 Style
.AlignConsecutiveMacros
.AcrossComments
= true;
18140 verifyFormat("#define a 3\n"
18141 "// line comment\n"
18145 "// line comment\n"
18150 verifyFormat("#define a 3\n"
18151 "/* block comment */\n"
18155 "/* block comment */\n"
18160 verifyFormat("#define a 3\n"
18161 "/* multi-line *\n"
18162 " * block comment */\n"
18166 "/* multi-line *\n"
18167 " * block comment */\n"
18172 verifyFormat("#define a 3\n"
18173 "// multi-line line comment\n"
18178 "// multi-line line comment\n"
18184 verifyFormat("#define a 3\n"
18185 "// empty lines still break.\n"
18190 "// empty lines still break.\n"
18196 // Test across empty lines
18197 Style
.AlignConsecutiveMacros
.AcrossComments
= false;
18198 Style
.AlignConsecutiveMacros
.AcrossEmptyLines
= true;
18199 verifyFormat("#define a 3\n"
18209 verifyFormat("#define a 3\n"
18223 verifyFormat("#define a 3\n"
18224 "// comments should break alignment\n"
18229 "// comments should break alignment\n"
18235 // Test across empty lines and comments
18236 Style
.AlignConsecutiveMacros
.AcrossComments
= true;
18237 verifyFormat("#define a 3\n"
18239 "// line comment\n"
18244 verifyFormat("#define a 3\n"
18247 "/* multi-line *\n"
18248 " * block comment */\n"
18256 "/* multi-line *\n"
18257 " * block comment */\n"
18264 verifyFormat("#define a 3\n"
18267 "/* multi-line *\n"
18268 " * block comment */\n"
18276 "/* multi-line *\n"
18277 " * block comment */\n"
18285 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossEmptyLines
) {
18286 FormatStyle Alignment
= getLLVMStyle();
18287 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18288 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18289 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18291 Alignment
.MaxEmptyLinesToKeep
= 10;
18292 /* Test alignment across empty lines */
18293 verifyFormat("int a = 5;\n"
18295 "int oneTwoThree = 123;",
18298 "int oneTwoThree= 123;",
18300 verifyFormat("int a = 5;\n"
18303 "int oneTwoThree = 123;",
18307 "int oneTwoThree = 123;",
18309 verifyFormat("int a = 5;\n"
18312 "int oneTwoThree = 123;\n"
18313 "int oneTwo = 12;",
18317 "int oneTwoThree = 123;\n"
18318 "int oneTwo = 12;",
18321 /* Test across comments */
18322 verifyFormat("int a = 5;\n"
18323 "/* block comment */\n"
18324 "int oneTwoThree = 123;",
18326 "/* block comment */\n"
18327 "int oneTwoThree=123;",
18330 verifyFormat("int a = 5;\n"
18331 "// line comment\n"
18332 "int oneTwoThree = 123;",
18334 "// line comment\n"
18335 "int oneTwoThree=123;",
18338 /* Test across comments and newlines */
18339 verifyFormat("int a = 5;\n"
18341 "/* block comment */\n"
18342 "int oneTwoThree = 123;",
18345 "/* block comment */\n"
18346 "int oneTwoThree=123;",
18349 verifyFormat("int a = 5;\n"
18351 "// line comment\n"
18352 "int oneTwoThree = 123;",
18355 "// line comment\n"
18356 "int oneTwoThree=123;",
18360 TEST_F(FormatTest
, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments
) {
18361 FormatStyle Alignment
= getLLVMStyle();
18362 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
18363 Alignment
.AlignConsecutiveDeclarations
.AcrossEmptyLines
= true;
18364 Alignment
.AlignConsecutiveDeclarations
.AcrossComments
= true;
18366 Alignment
.MaxEmptyLinesToKeep
= 10;
18367 /* Test alignment across empty lines */
18368 verifyFormat("int a = 5;\n"
18370 "float const oneTwoThree = 123;",
18373 "float const oneTwoThree = 123;",
18375 verifyFormat("int a = 5;\n"
18376 "float const one = 1;\n"
18378 "int oneTwoThree = 123;",
18380 "float const one = 1;\n"
18382 "int oneTwoThree = 123;",
18385 /* Test across comments */
18386 verifyFormat("float const a = 5;\n"
18387 "/* block comment */\n"
18388 "int oneTwoThree = 123;",
18389 "float const a = 5;\n"
18390 "/* block comment */\n"
18391 "int oneTwoThree=123;",
18394 verifyFormat("float const a = 5;\n"
18395 "// line comment\n"
18396 "int oneTwoThree = 123;",
18397 "float const a = 5;\n"
18398 "// line comment\n"
18399 "int oneTwoThree=123;",
18402 /* Test across comments and newlines */
18403 verifyFormat("float const a = 5;\n"
18405 "/* block comment */\n"
18406 "int oneTwoThree = 123;",
18407 "float const a = 5;\n"
18409 "/* block comment */\n"
18410 "int oneTwoThree=123;",
18413 verifyFormat("float const a = 5;\n"
18415 "// line comment\n"
18416 "int oneTwoThree = 123;",
18417 "float const a = 5;\n"
18419 "// line comment\n"
18420 "int oneTwoThree=123;",
18424 TEST_F(FormatTest
, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments
) {
18425 FormatStyle Alignment
= getLLVMStyle();
18426 Alignment
.AlignConsecutiveBitFields
.Enabled
= true;
18427 Alignment
.AlignConsecutiveBitFields
.AcrossEmptyLines
= true;
18428 Alignment
.AlignConsecutiveBitFields
.AcrossComments
= true;
18430 Alignment
.MaxEmptyLinesToKeep
= 10;
18431 /* Test alignment across empty lines */
18432 verifyFormat("int a : 5;\n"
18434 "int longbitfield : 6;",
18437 "int longbitfield : 6;",
18439 verifyFormat("int a : 5;\n"
18442 "int longbitfield : 6;",
18446 "int longbitfield : 6;",
18449 /* Test across comments */
18450 verifyFormat("int a : 5;\n"
18451 "/* block comment */\n"
18452 "int longbitfield : 6;",
18454 "/* block comment */\n"
18455 "int longbitfield : 6;",
18457 verifyFormat("int a : 5;\n"
18459 "// line comment\n"
18460 "int longbitfield : 6;",
18463 "// line comment\n"
18464 "int longbitfield : 6;",
18467 /* Test across comments and newlines */
18468 verifyFormat("int a : 5;\n"
18469 "/* block comment */\n"
18471 "int longbitfield : 6;",
18473 "/* block comment */\n"
18475 "int longbitfield : 6;",
18477 verifyFormat("int a : 5;\n"
18480 "// line comment\n"
18482 "int longbitfield : 6;",
18486 "// line comment \n"
18488 "int longbitfield : 6;",
18492 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossComments
) {
18493 FormatStyle Alignment
= getLLVMStyle();
18494 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18495 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18496 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18498 Alignment
.MaxEmptyLinesToKeep
= 10;
18499 /* Test alignment across empty lines */
18500 verifyFormat("int a = 5;\n"
18502 "int oneTwoThree = 123;",
18505 "int oneTwoThree= 123;",
18507 verifyFormat("int a = 5;\n"
18510 "int oneTwoThree = 123;",
18514 "int oneTwoThree = 123;",
18517 /* Test across comments */
18518 verifyFormat("int a = 5;\n"
18519 "/* block comment */\n"
18520 "int oneTwoThree = 123;",
18522 "/* block comment */\n"
18523 "int oneTwoThree=123;",
18526 verifyFormat("int a = 5;\n"
18527 "// line comment\n"
18528 "int oneTwoThree = 123;",
18530 "// line comment\n"
18531 "int oneTwoThree=123;",
18534 verifyFormat("int a = 5;\n"
18536 " * multi-line block comment\n"
18538 "int oneTwoThree = 123;",
18541 " * multi-line block comment\n"
18543 "int oneTwoThree=123;",
18546 verifyFormat("int a = 5;\n"
18548 "// multi-line line comment\n"
18550 "int oneTwoThree = 123;",
18553 "// multi-line line comment\n"
18555 "int oneTwoThree=123;",
18558 /* Test across comments and newlines */
18559 verifyFormat("int a = 5;\n"
18561 "/* block comment */\n"
18562 "int oneTwoThree = 123;",
18565 "/* block comment */\n"
18566 "int oneTwoThree=123;",
18569 verifyFormat("int a = 5;\n"
18571 "// line comment\n"
18572 "int oneTwoThree = 123;",
18575 "// line comment\n"
18576 "int oneTwoThree=123;",
18580 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments
) {
18581 FormatStyle Alignment
= getLLVMStyle();
18582 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18583 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18584 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18585 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18586 verifyFormat("int a = 5;\n"
18587 "int oneTwoThree = 123;",
18589 verifyFormat("int a = method();\n"
18590 "int oneTwoThree = 133;",
18592 verifyFormat("a &= 5;\n"
18598 "sfdbddfbdfbb ^= 5;\n"
18600 "int dsvvdvsdvvv = 123;",
18602 verifyFormat("int i = 1, j = 10;\n"
18603 "something = 2000;",
18605 verifyFormat("something = 2000;\n"
18606 "int i = 1, j = 10;",
18608 verifyFormat("something = 2000;\n"
18610 "int i = 1, j = 10;\n"
18614 verifyFormat("int a = 5;\n"
18617 "int oneTwoThree = 123;\n"
18618 "int oneTwo = 12;",
18620 verifyFormat("int oneTwoThree = 123;\n"
18621 "int oneTwo = 12;\n"
18624 verifyFormat("int oneTwoThree = 123; // comment\n"
18625 "int oneTwo = 12; // comment",
18629 /* Uncomment when fixed
18630 verifyFormat("#if A\n"
18632 "int aaaaaaaa = 12;\n"
18639 verifyFormat("enum foo {\n"
18642 " aaaaaaaa = 12;\n"
18652 Alignment
.MaxEmptyLinesToKeep
= 10;
18653 /* Test alignment across empty lines */
18654 verifyFormat("int a = 5;\n"
18656 "int oneTwoThree = 123;",
18659 "int oneTwoThree= 123;",
18661 verifyFormat("int a = 5;\n"
18664 "int oneTwoThree = 123;",
18668 "int oneTwoThree = 123;",
18670 verifyFormat("int a = 5;\n"
18673 "int oneTwoThree = 123;\n"
18674 "int oneTwo = 12;",
18678 "int oneTwoThree = 123;\n"
18679 "int oneTwo = 12;",
18682 /* Test across comments */
18683 verifyFormat("int a = 5;\n"
18684 "/* block comment */\n"
18685 "int oneTwoThree = 123;",
18687 "/* block comment */\n"
18688 "int oneTwoThree=123;",
18691 verifyFormat("int a = 5;\n"
18692 "// line comment\n"
18693 "int oneTwoThree = 123;",
18695 "// line comment\n"
18696 "int oneTwoThree=123;",
18699 /* Test across comments and newlines */
18700 verifyFormat("int a = 5;\n"
18702 "/* block comment */\n"
18703 "int oneTwoThree = 123;",
18706 "/* block comment */\n"
18707 "int oneTwoThree=123;",
18710 verifyFormat("int a = 5;\n"
18712 "// line comment\n"
18713 "int oneTwoThree = 123;",
18716 "// line comment\n"
18717 "int oneTwoThree=123;",
18720 verifyFormat("int a = 5;\n"
18722 "// multi-line line comment\n"
18724 "int oneTwoThree = 123;",
18727 "// multi-line line comment\n"
18729 "int oneTwoThree=123;",
18732 verifyFormat("int a = 5;\n"
18734 " * multi-line block comment\n"
18736 "int oneTwoThree = 123;",
18739 " * multi-line block comment\n"
18741 "int oneTwoThree=123;",
18744 verifyFormat("int a = 5;\n"
18746 "/* block comment */\n"
18750 "int oneTwoThree = 123;",
18753 "/* block comment */\n"
18757 "int oneTwoThree=123;",
18760 verifyFormat("int a = 5;\n"
18762 "// line comment\n"
18766 "int oneTwoThree = 123;",
18769 "// line comment\n"
18773 "int oneTwoThree=123;",
18776 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
18777 verifyFormat("#define A \\\n"
18778 " int aaaa = 12; \\\n"
18779 " int b = 23; \\\n"
18780 " int ccc = 234; \\\n"
18781 " int dddddddddd = 2345;",
18783 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
18784 verifyFormat("#define A \\\n"
18785 " int aaaa = 12; \\\n"
18786 " int b = 23; \\\n"
18787 " int ccc = 234; \\\n"
18788 " int dddddddddd = 2345;",
18790 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
18791 verifyFormat("#define A "
18799 " int dddddddddd = 2345;",
18801 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18802 "k = 4, int l = 5,\n"
18805 " otherThing = 1;\n"
18808 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18811 " int big = 10000;\n"
18814 verifyFormat("class C {\n"
18817 " virtual void f() = 0;\n"
18820 verifyFormat("int i = 1;\n"
18821 "if (SomeType t = getSomething()) {\n"
18824 "int big = 10000;",
18826 verifyFormat("int j = 7;\n"
18827 "for (int k = 0; k < N; ++k) {\n"
18830 "int big = 10000;\n"
18833 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
18834 verifyFormat("int i = 1;\n"
18835 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18836 " = someLooooooooooooooooongFunction();\n"
18839 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
18840 verifyFormat("int i = 1;\n"
18841 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18842 " someLooooooooooooooooongFunction();\n"
18846 verifyFormat("auto lambda = []() {\n"
18860 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18861 " loooooooooooooooooooooongParameterB);\n"
18865 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18866 " typename B = very_long_type_name_1,\n"
18867 " typename T_2 = very_long_type_name_2>\n"
18870 verifyFormat("int a, b = 1;\n"
18874 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18875 "float b[1][] = {{3.f}};",
18877 verifyFormat("for (int i = 0; i < 1; i++)\n"
18880 verifyFormat("for (i = 0; i < 1; i++)\n"
18885 Alignment
.ReflowComments
= FormatStyle::RCS_Always
;
18886 Alignment
.ColumnLimit
= 50;
18887 verifyFormat("int x = 0;\n"
18888 "int yy = 1; /// specificlennospace\n"
18891 "int yy = 1; ///specificlennospace\n"
18896 TEST_F(FormatTest
, AlignCompoundAssignments
) {
18897 FormatStyle Alignment
= getLLVMStyle();
18898 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18899 Alignment
.AlignConsecutiveAssignments
.AlignCompound
= true;
18900 Alignment
.AlignConsecutiveAssignments
.PadOperators
= false;
18901 verifyFormat("sfdbddfbdfbb = 5;\n"
18903 "int dsvvdvsdvvv = 123;",
18905 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18907 "int dsvvdvsdvvv = 123;",
18909 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18911 "int dsvvdvsdvvv = 123;",
18913 verifyFormat("int xxx = 5;\n"
18920 verifyFormat("int xxx = 5;\n"
18927 // Test that `<=` is not treated as a compound assignment.
18928 verifyFormat("aa &= 5;\n"
18932 Alignment
.AlignConsecutiveAssignments
.PadOperators
= true;
18933 verifyFormat("sfdbddfbdfbb = 5;\n"
18935 "int dsvvdvsdvvv = 123;",
18937 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18939 "int dsvvdvsdvvv = 123;",
18941 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18943 "int dsvvdvsdvvv = 123;",
18945 verifyFormat("a += 5;\n"
18948 "oneTwoThree = 123;",
18952 "oneTwoThree = 123;",
18954 verifyFormat("a += 5;\n"
18957 "oneTwoThree = 123;",
18961 "oneTwoThree = 123;",
18963 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18964 verifyFormat("a += 5;\n"
18967 "oneTwoThree = 123;",
18971 "oneTwoThree = 123;",
18973 verifyFormat("a += 5;\n"
18976 "oneTwoThree = 123;",
18980 "oneTwoThree = 123;",
18982 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= false;
18983 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18984 verifyFormat("a += 5;\n"
18987 "oneTwoThree = 123;",
18991 "oneTwoThree = 123;",
18993 verifyFormat("a += 5;\n"
18996 "oneTwoThree = 123;",
19000 "oneTwoThree = 123;",
19002 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
19003 verifyFormat("a += 5;\n"
19006 "oneTwoThree = 123;",
19010 "oneTwoThree = 123;",
19012 verifyFormat("a += 5;\n"
19015 "oneTwoThree <<= 123;",
19019 "oneTwoThree <<= 123;",
19023 TEST_F(FormatTest
, AlignConsecutiveAssignments
) {
19024 FormatStyle Alignment
= getLLVMStyle();
19025 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
19026 verifyFormat("int a = 5;\n"
19027 "int oneTwoThree = 123;",
19029 verifyFormat("int a = 5;\n"
19030 "int oneTwoThree = 123;",
19033 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19034 verifyFormat("int a = 5;\n"
19035 "int oneTwoThree = 123;",
19037 verifyFormat("int a = method();\n"
19038 "int oneTwoThree = 133;",
19040 verifyFormat("aa <= 5;\n"
19047 "sfdbddfbdfbb ^= 5;\n"
19049 "int dsvvdvsdvvv = 123;",
19051 verifyFormat("int i = 1, j = 10;\n"
19052 "something = 2000;",
19054 verifyFormat("something = 2000;\n"
19055 "int i = 1, j = 10;",
19057 verifyFormat("something = 2000;\n"
19059 "int i = 1, j = 10;\n"
19063 verifyFormat("int a = 5;\n"
19066 "int oneTwoThree = 123;\n"
19067 "int oneTwo = 12;",
19069 verifyFormat("int oneTwoThree = 123;\n"
19070 "int oneTwo = 12;\n"
19073 verifyFormat("int oneTwoThree = 123; // comment\n"
19074 "int oneTwo = 12; // comment",
19076 verifyFormat("int f() = default;\n"
19077 "int &operator() = default;\n"
19078 "int &operator=() {",
19080 verifyFormat("int f() = delete;\n"
19081 "int &operator() = delete;\n"
19082 "int &operator=() {",
19084 verifyFormat("int f() = default; // comment\n"
19085 "int &operator() = default; // comment\n"
19086 "int &operator=() {",
19088 verifyFormat("int f() = default;\n"
19089 "int &operator() = default;\n"
19090 "int &operator==() {",
19092 verifyFormat("int f() = default;\n"
19093 "int &operator() = default;\n"
19094 "int &operator<=() {",
19096 verifyFormat("int f() = default;\n"
19097 "int &operator() = default;\n"
19098 "int &operator!=() {",
19100 verifyFormat("int f() = default;\n"
19101 "int &operator() = default;\n"
19102 "int &operator=();",
19104 verifyFormat("int f() = delete;\n"
19105 "int &operator() = delete;\n"
19106 "int &operator=();",
19108 verifyFormat("/* long long padding */ int f() = default;\n"
19109 "int &operator() = default;\n"
19110 "int &operator/**/ =();",
19112 // https://llvm.org/PR33697
19113 FormatStyle AlignmentWithPenalty
= getLLVMStyle();
19114 AlignmentWithPenalty
.AlignConsecutiveAssignments
.Enabled
= true;
19115 AlignmentWithPenalty
.PenaltyReturnTypeOnItsOwnLine
= 5000;
19116 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
19117 " void f() = delete;\n"
19118 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
19119 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
19121 AlignmentWithPenalty
);
19124 /* Uncomment when fixed
19125 verifyFormat("#if A\n"
19127 "int aaaaaaaa = 12;\n"
19134 verifyFormat("enum foo {\n"
19137 " aaaaaaaa = 12;\n"
19147 verifyFormat("int a = 5;\n"
19149 "int oneTwoThree = 123;",
19152 "int oneTwoThree= 123;",
19154 verifyFormat("int a = 5;\n"
19157 "int oneTwoThree = 123;",
19161 "int oneTwoThree = 123;",
19163 verifyFormat("int a = 5;\n"
19166 "int oneTwoThree = 123;\n"
19167 "int oneTwo = 12;",
19171 "int oneTwoThree = 123;\n"
19172 "int oneTwo = 12;",
19174 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
19175 verifyFormat("#define A \\\n"
19176 " int aaaa = 12; \\\n"
19177 " int b = 23; \\\n"
19178 " int ccc = 234; \\\n"
19179 " int dddddddddd = 2345;",
19181 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
19182 verifyFormat("#define A \\\n"
19183 " int aaaa = 12; \\\n"
19184 " int b = 23; \\\n"
19185 " int ccc = 234; \\\n"
19186 " int dddddddddd = 2345;",
19188 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
19189 verifyFormat("#define A "
19197 " int dddddddddd = 2345;",
19199 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19200 "k = 4, int l = 5,\n"
19203 " otherThing = 1;\n"
19206 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19209 " int big = 10000;\n"
19212 verifyFormat("class C {\n"
19215 " virtual void f() = 0;\n"
19218 verifyFormat("int i = 1;\n"
19219 "if (SomeType t = getSomething()) {\n"
19222 "int big = 10000;",
19224 verifyFormat("int j = 7;\n"
19225 "for (int k = 0; k < N; ++k) {\n"
19228 "int big = 10000;\n"
19231 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
19232 verifyFormat("int i = 1;\n"
19233 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19234 " = someLooooooooooooooooongFunction();\n"
19237 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
19238 verifyFormat("int i = 1;\n"
19239 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19240 " someLooooooooooooooooongFunction();\n"
19244 verifyFormat("auto lambda = []() {\n"
19258 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19259 " loooooooooooooooooooooongParameterB);\n"
19263 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
19264 " typename B = very_long_type_name_1,\n"
19265 " typename T_2 = very_long_type_name_2>\n"
19268 verifyFormat("int a, b = 1;\n"
19272 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19273 "float b[1][] = {{3.f}};",
19275 verifyFormat("for (int i = 0; i < 1; i++)\n"
19278 verifyFormat("for (i = 0; i < 1; i++)\n"
19283 EXPECT_EQ(Alignment
.ReflowComments
, FormatStyle::RCS_Always
);
19284 Alignment
.ColumnLimit
= 50;
19285 verifyFormat("int x = 0;\n"
19286 "int yy = 1; /// specificlennospace\n"
19289 "int yy = 1; ///specificlennospace\n"
19293 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19299 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19300 "auto b = g([] {\n"
19305 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19306 "auto b = g(param, [] {\n"
19311 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
19313 " if (condition) {\n"
19319 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19320 " ccc ? aaaaa : bbbbb,\n"
19321 " dddddddddddddddddddddddddd);",
19323 // FIXME: https://llvm.org/PR53497
19324 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
19325 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
19326 // " ccc ? aaaaa : bbbbb,\n"
19327 // " dddddddddddddddddddddddddd);",
19330 // Confirm proper handling of AlignConsecutiveAssignments with
19331 // BinPackArguments.
19332 // See https://llvm.org/PR55360
19333 Alignment
= getLLVMStyleWithColumns(50);
19334 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19335 Alignment
.BinPackArguments
= false;
19336 verifyFormat("int a_long_name = 1;\n"
19337 "auto b = B({a_long_name, a_long_name},\n"
19338 " {a_longer_name_for_wrap,\n"
19339 " a_longer_name_for_wrap});",
19341 verifyFormat("int a_long_name = 1;\n"
19342 "auto b = B{{a_long_name, a_long_name},\n"
19343 " {a_longer_name_for_wrap,\n"
19344 " a_longer_name_for_wrap}};",
19347 Alignment
= getLLVMStyleWithColumns(60);
19348 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19349 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
19350 "using I = std::conditional_t<II::value >= 0,\n"
19351 " std::ic<int, II::value + 1>,\n"
19352 " std::ic<int, -1>>;",
19354 verifyFormat("SomeName = Foo;\n"
19355 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
19359 Alignment
.ColumnLimit
= 80;
19360 Alignment
.SpacesInAngles
= FormatStyle::SIAS_Always
;
19361 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n"
19362 "ptr = reinterpret_cast< void ** >(ptr[0]);",
19364 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n"
19365 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));",
19368 Alignment
.SpacesInParens
= FormatStyle::SIPO_Custom
;
19369 Alignment
.SpacesInParensOptions
.InCStyleCasts
= true;
19370 verifyFormat("void **ptr = ( void ** )unkn;\n"
19371 "ptr = ( void ** )ptr[0];",
19373 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n"
19374 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);",
19378 TEST_F(FormatTest
, AlignConsecutiveBitFields
) {
19379 FormatStyle Alignment
= getLLVMStyle();
19380 Alignment
.AlignConsecutiveBitFields
.Enabled
= true;
19381 verifyFormat("int const a : 5;\n"
19382 "int oneTwoThree : 23;",
19385 // Initializers are allowed starting with c++2a
19386 verifyFormat("int const a : 5 = 1;\n"
19387 "int oneTwoThree : 23 = 0;",
19390 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19391 verifyFormat("int const a : 5;\n"
19392 "int oneTwoThree : 23;",
19395 verifyFormat("int const a : 5; // comment\n"
19396 "int oneTwoThree : 23; // comment",
19399 verifyFormat("int const a : 5 = 1;\n"
19400 "int oneTwoThree : 23 = 0;",
19403 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19404 verifyFormat("int const a : 5 = 1;\n"
19405 "int oneTwoThree : 23 = 0;",
19407 verifyFormat("int const a : 5 = {1};\n"
19408 "int oneTwoThree : 23 = 0;",
19411 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_None
;
19412 verifyFormat("int const a :5;\n"
19413 "int oneTwoThree:23;",
19416 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_Before
;
19417 verifyFormat("int const a :5;\n"
19418 "int oneTwoThree :23;",
19421 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_After
;
19422 verifyFormat("int const a : 5;\n"
19423 "int oneTwoThree: 23;",
19426 // Known limitations: ':' is only recognized as a bitfield colon when
19427 // followed by a number.
19429 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
19435 TEST_F(FormatTest
, AlignConsecutiveDeclarations
) {
19436 FormatStyle Alignment
= getLLVMStyle();
19437 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
19438 Alignment
.PointerAlignment
= FormatStyle::PAS_Right
;
19439 verifyFormat("float const a = 5;\n"
19440 "int oneTwoThree = 123;",
19442 verifyFormat("int a = 5;\n"
19443 "float const oneTwoThree = 123;",
19446 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19447 verifyFormat("float const a = 5;\n"
19448 "int oneTwoThree = 123;",
19450 verifyFormat("int a = method();\n"
19451 "float const oneTwoThree = 133;",
19453 verifyFormat("int i = 1, j = 10;\n"
19454 "something = 2000;",
19456 verifyFormat("something = 2000;\n"
19457 "int i = 1, j = 10;",
19459 verifyFormat("float something = 2000;\n"
19460 "double another = 911;\n"
19461 "int i = 1, j = 10;\n"
19462 "const int *oneMore = 1;\n"
19465 verifyFormat("float a = 5;\n"
19468 "const double oneTwoThree = 123;\n"
19469 "const unsigned int oneTwo = 12;",
19471 verifyFormat("int oneTwoThree{0}; // comment\n"
19472 "unsigned oneTwo; // comment",
19474 verifyFormat("unsigned int *a;\n"
19476 "unsigned int Const *c;\n"
19477 "unsigned int const *d;\n"
19478 "unsigned int Const &e;\n"
19479 "unsigned int const &f;",
19481 verifyFormat("Const unsigned int *c;\n"
19482 "const unsigned int *d;\n"
19483 "Const unsigned int &e;\n"
19484 "const unsigned int &f;\n"
19485 "const unsigned g;\n"
19486 "Const unsigned h;",
19488 verifyFormat("float const a = 5;\n"
19490 "int oneTwoThree = 123;",
19491 "float const a = 5;\n"
19493 "int oneTwoThree= 123;",
19495 verifyFormat("float a = 5;\n"
19498 "unsigned oneTwoThree = 123;",
19502 "unsigned oneTwoThree = 123;",
19504 verifyFormat("float a = 5;\n"
19507 "unsigned oneTwoThree = 123;\n"
19508 "int oneTwo = 12;",
19512 "unsigned oneTwoThree = 123;\n"
19513 "int oneTwo = 12;",
19515 // Function prototype alignment
19516 verifyFormat("int a();\n"
19519 verifyFormat("int a(int x);\n"
19522 verifyFormat("int a(const Test & = Test());\n"
19523 "int a1(int &foo, const Test & = Test());\n"
19524 "int a2(int &foo, const Test &name = Test());\n"
19527 verifyFormat("struct Test {\n"
19528 " Test(const Test &) = default;\n"
19529 " ~Test() = default;\n"
19530 " Test &operator=(const Test &) = default;\n"
19533 unsigned OldColumnLimit
= Alignment
.ColumnLimit
;
19534 // We need to set ColumnLimit to zero, in order to stress nested alignments,
19535 // otherwise the function parameters will be re-flowed onto a single line.
19536 Alignment
.ColumnLimit
= 0;
19537 verifyFormat("int a(int x,\n"
19539 "double b(int x,\n"
19543 "double b(int x,\n"
19546 // This ensures that function parameters of function declarations are
19547 // correctly indented when their owning functions are indented.
19548 // The failure case here is for 'double y' to not be indented enough.
19549 verifyFormat("double a(int x);\n"
19552 "double a(int x);\n"
19556 // Set ColumnLimit low so that we induce wrapping immediately after
19557 // the function name and opening paren.
19558 Alignment
.ColumnLimit
= 13;
19559 verifyFormat("int function(\n"
19563 // Set ColumnLimit low so that we break the argument list in multiple lines.
19564 Alignment
.ColumnLimit
= 35;
19565 verifyFormat("int a3(SomeTypeName1 &x,\n"
19566 " SomeTypeName2 &y,\n"
19567 " const Test & = Test());\n"
19570 Alignment
.ColumnLimit
= OldColumnLimit
;
19571 // Ensure function pointers don't screw up recursive alignment
19572 verifyFormat("int a(int x, void (*fp)(int y));\n"
19575 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19576 verifyFormat("struct Test {\n"
19577 " Test(const Test &) = default;\n"
19578 " ~Test() = default;\n"
19579 " Test &operator=(const Test &) = default;\n"
19582 // Ensure recursive alignment is broken by function braces, so that the
19583 // "a = 1" does not align with subsequent assignments inside the function
19585 verifyFormat("int func(int a = 1) {\n"
19590 verifyFormat("float something = 2000;\n"
19591 "double another = 911;\n"
19592 "int i = 1, j = 10;\n"
19593 "const int *oneMore = 1;\n"
19596 verifyFormat("int oneTwoThree = {0}; // comment\n"
19597 "unsigned oneTwo = 0; // comment",
19599 // Make sure that scope is correctly tracked, in the absence of braces
19600 verifyFormat("for (int i = 0; i < n; i++)\n"
19604 verifyFormat("if (int i = 0)\n"
19608 // Ensure operator[] and operator() are comprehended
19609 verifyFormat("struct test {\n"
19610 " long long int foo();\n"
19611 " int operator[](int a);\n"
19615 verifyFormat("struct test {\n"
19616 " long long int foo();\n"
19617 " int operator()(int a);\n"
19621 // http://llvm.org/PR52914
19622 verifyFormat("char *a[] = {\"a\", // comment\n"
19624 "int bbbbbbb = 0;",
19626 // http://llvm.org/PR68079
19627 verifyFormat("using Fn = int (A::*)();\n"
19628 "using RFn = int (A::*)() &;\n"
19629 "using RRFn = int (A::*)() &&;",
19631 verifyFormat("using Fn = int (A::*)();\n"
19632 "using RFn = int *(A::*)() &;\n"
19633 "using RRFn = double (A::*)() &&;",
19637 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19638 " int const i = 1;\n"
19640 " int big = 10000;\n"
19642 " unsigned oneTwoThree = 123;\n"
19643 " int oneTwo = 12;\n"
19646 " int ll = 10000;\n"
19648 "void SomeFunction(int parameter= 0) {\n"
19649 " int const i= 1;\n"
19651 " int big = 10000;\n"
19653 "unsigned oneTwoThree =123;\n"
19654 "int oneTwo = 12;\n"
19660 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19661 " int const i = 1;\n"
19662 " int **j = 2, ***k;\n"
19664 " int &&l = i + j;\n"
19665 " int big = 10000;\n"
19667 " unsigned oneTwoThree = 123;\n"
19668 " int oneTwo = 12;\n"
19671 " int ll = 10000;\n"
19673 "void SomeFunction(int parameter= 0) {\n"
19674 " int const i= 1;\n"
19675 " int **j=2,***k;\n"
19678 " int big = 10000;\n"
19680 "unsigned oneTwoThree =123;\n"
19681 "int oneTwo = 12;\n"
19687 // variables are aligned at their name, pointers are at the right most
19689 verifyFormat("int *a;\n"
19696 FormatStyle AlignmentLeft
= Alignment
;
19697 AlignmentLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
19698 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19699 " int const i = 1;\n"
19701 " int big = 10000;\n"
19703 " unsigned oneTwoThree = 123;\n"
19704 " int oneTwo = 12;\n"
19707 " int ll = 10000;\n"
19709 "void SomeFunction(int parameter= 0) {\n"
19710 " int const i= 1;\n"
19712 " int big = 10000;\n"
19714 "unsigned oneTwoThree =123;\n"
19715 "int oneTwo = 12;\n"
19721 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19722 " int const i = 1;\n"
19725 " int&& l = i + j;\n"
19726 " int big = 10000;\n"
19728 " unsigned oneTwoThree = 123;\n"
19729 " int oneTwo = 12;\n"
19732 " int ll = 10000;\n"
19734 "void SomeFunction(int parameter= 0) {\n"
19735 " int const i= 1;\n"
19739 " int big = 10000;\n"
19741 "unsigned oneTwoThree =123;\n"
19742 "int oneTwo = 12;\n"
19748 // variables are aligned at their name, pointers are at the left most position
19749 verifyFormat("int* a;\n"
19755 verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19760 FormatStyle AlignmentMiddle
= Alignment
;
19761 AlignmentMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
19762 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19763 " int const i = 1;\n"
19765 " int big = 10000;\n"
19767 " unsigned oneTwoThree = 123;\n"
19768 " int oneTwo = 12;\n"
19771 " int ll = 10000;\n"
19773 "void SomeFunction(int parameter= 0) {\n"
19774 " int const i= 1;\n"
19776 " int big = 10000;\n"
19778 "unsigned oneTwoThree =123;\n"
19779 "int oneTwo = 12;\n"
19785 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19786 " int const i = 1;\n"
19787 " int ** j = 2, ***k;\n"
19789 " int && l = i + j;\n"
19790 " int big = 10000;\n"
19792 " unsigned oneTwoThree = 123;\n"
19793 " int oneTwo = 12;\n"
19796 " int ll = 10000;\n"
19798 "void SomeFunction(int parameter= 0) {\n"
19799 " int const i= 1;\n"
19800 " int **j=2,***k;\n"
19803 " int big = 10000;\n"
19805 "unsigned oneTwoThree =123;\n"
19806 "int oneTwo = 12;\n"
19812 // variables are aligned at their name, pointers are in the middle
19813 verifyFormat("int * a;\n"
19819 verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19823 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19824 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
19825 verifyFormat("#define A \\\n"
19826 " int aaaa = 12; \\\n"
19827 " float b = 23; \\\n"
19828 " const int ccc = 234; \\\n"
19829 " unsigned dddddddddd = 2345;",
19831 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
19832 verifyFormat("#define A \\\n"
19833 " int aaaa = 12; \\\n"
19834 " float b = 23; \\\n"
19835 " const int ccc = 234; \\\n"
19836 " unsigned dddddddddd = 2345;",
19838 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
19839 Alignment
.ColumnLimit
= 30;
19840 verifyFormat("#define A \\\n"
19841 " int aaaa = 12; \\\n"
19842 " float b = 23; \\\n"
19843 " const int ccc = 234; \\\n"
19844 " int dddddddddd = 2345;",
19846 Alignment
.ColumnLimit
= 80;
19847 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19848 "k = 4, int l = 5,\n"
19850 " const int j = 10;\n"
19851 " otherThing = 1;\n"
19854 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19855 " int const i = 1;\n"
19857 " int big = 10000;\n"
19860 verifyFormat("class C {\n"
19863 " virtual void f() = 0;\n"
19866 verifyFormat("float i = 1;\n"
19867 "if (SomeType t = getSomething()) {\n"
19869 "const unsigned j = 2;\n"
19870 "int big = 10000;",
19872 verifyFormat("float j = 7;\n"
19873 "for (int k = 0; k < N; ++k) {\n"
19875 "unsigned j = 2;\n"
19876 "int big = 10000;\n"
19879 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
19880 verifyFormat("float i = 1;\n"
19881 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19882 " = someLooooooooooooooooongFunction();\n"
19885 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
19886 verifyFormat("int i = 1;\n"
19887 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19888 " someLooooooooooooooooongFunction();\n"
19892 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19893 verifyFormat("auto lambda = []() {\n"
19906 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19910 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19911 " loooooooooooooooooooooongParameterB);\n"
19915 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19916 // We expect declarations and assignments to align, as long as it doesn't
19917 // exceed the column limit, starting a new alignment sequence whenever it
19919 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19920 Alignment
.ColumnLimit
= 30;
19921 verifyFormat("float ii = 1;\n"
19922 "unsigned j = 2;\n"
19923 "int someVerylongVariable = 1;\n"
19924 "AnotherLongType ll = 123456;\n"
19925 "VeryVeryLongType k = 2;\n"
19928 Alignment
.ColumnLimit
= 80;
19929 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19932 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19933 " typename LongType, typename B>\n"
19936 verifyFormat("float a, b = 1;\n"
19940 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19941 "float b[1][] = {{3.f}};",
19943 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19944 verifyFormat("float a, b = 1;\n"
19948 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19949 "float b[1][] = {{3.f}};",
19951 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19953 Alignment
.ColumnLimit
= 30;
19954 Alignment
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
19955 verifyFormat("void foo(float a,\n"
19958 " uint32_t *d) {\n"
19963 "void bar(ino_t a,\n"
19968 Alignment
.BinPackParameters
= FormatStyle::BPPS_BinPack
;
19969 Alignment
.ColumnLimit
= 80;
19972 Alignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
19974 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19975 " static const Version verVs2017;\n"
19979 Alignment
.PointerAlignment
= FormatStyle::PAS_Right
;
19981 // See llvm.org/PR35641
19982 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19983 verifyFormat("int func() { //\n"
19990 FormatStyle Style
= getMozillaStyle();
19991 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19992 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19994 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style
);
19996 Alignment
.PointerAlignment
= FormatStyle::PAS_Left
;
19997 verifyFormat("unsigned int* a;\n"
19999 "unsigned int Const* c;\n"
20000 "unsigned int const* d;\n"
20001 "unsigned int Const& e;\n"
20002 "unsigned int const& f;",
20004 verifyFormat("Const unsigned int* c;\n"
20005 "const unsigned int* d;\n"
20006 "Const unsigned int& e;\n"
20007 "const unsigned int& f;\n"
20008 "const unsigned g;\n"
20009 "Const unsigned h;",
20012 Alignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
20013 verifyFormat("unsigned int * a;\n"
20015 "unsigned int Const * c;\n"
20016 "unsigned int const * d;\n"
20017 "unsigned int Const & e;\n"
20018 "unsigned int const & f;",
20020 verifyFormat("Const unsigned int * c;\n"
20021 "const unsigned int * d;\n"
20022 "Const unsigned int & e;\n"
20023 "const unsigned int & f;\n"
20024 "const unsigned g;\n"
20025 "Const unsigned h;",
20029 FormatStyle BracedAlign
= getLLVMStyle();
20030 BracedAlign
.AlignConsecutiveDeclarations
.Enabled
= true;
20031 verifyFormat("const auto result{[]() {\n"
20032 " const auto something = 1;\n"
20036 verifyFormat("int foo{[]() {\n"
20041 BracedAlign
.Cpp11BracedListStyle
= false;
20042 verifyFormat("const auto result{ []() {\n"
20043 " const auto something = 1;\n"
20047 verifyFormat("int foo{ []() {\n"
20053 Alignment
.AlignConsecutiveDeclarations
.AlignFunctionDeclarations
= false;
20054 verifyFormat("unsigned int f1(void);\n"
20056 "size_t f3(void);",
20060 TEST_F(FormatTest
, AlignConsecutiveShortCaseStatements
) {
20061 FormatStyle Alignment
= getLLVMStyle();
20062 Alignment
.AllowShortCaseLabelsOnASingleLine
= true;
20063 Alignment
.AlignConsecutiveShortCaseStatements
.Enabled
= true;
20065 verifyFormat("switch (level) {\n"
20066 "case log::info: return \"info\";\n"
20067 "case log::warning: return \"warning\";\n"
20068 "default: return \"default\";\n"
20072 verifyFormat("switch (level) {\n"
20073 "case log::info: return \"info\";\n"
20074 "case log::warning: return \"warning\";\n"
20076 "switch (level) {\n"
20077 "case log::info: return \"info\";\n"
20078 "case log::warning:\n"
20079 " return \"warning\";\n"
20083 // Empty case statements push out the alignment, but non-short case labels
20085 verifyFormat("switch (level) {\n"
20086 "case log::info: return \"info\";\n"
20087 "case log::critical:\n"
20088 "case log::warning:\n"
20089 "case log::severe: return \"severe\";\n"
20090 "case log::extra_severe:\n"
20092 " return \"extra_severe\";\n"
20096 // Verify comments and empty lines break the alignment.
20097 verifyNoChange("switch (level) {\n"
20098 "case log::info: return \"info\";\n"
20099 "case log::warning: return \"warning\";\n"
20101 "case log::critical: return \"critical\";\n"
20102 "default: return \"default\";\n"
20104 "case log::severe: return \"severe\";\n"
20108 // Empty case statements don't break the alignment, and potentially push it
20110 verifyFormat("switch (level) {\n"
20111 "case log::info: return \"info\";\n"
20112 "case log::warning:\n"
20113 "case log::critical:\n"
20114 "default: return \"default\";\n"
20118 // Implicit fallthrough cases can be aligned with either a comment or
20120 verifyFormat("switch (level) {\n"
20121 "case log::info: return \"info\";\n"
20122 "case log::warning: // fallthrough\n"
20123 "case log::error: return \"error\";\n"
20124 "case log::critical: /*fallthrough*/\n"
20125 "case log::severe: return \"severe\";\n"
20126 "case log::diag: [[fallthrough]];\n"
20127 "default: return \"default\";\n"
20131 // Verify trailing comment that needs a reflow also gets aligned properly.
20132 verifyFormat("switch (level) {\n"
20133 "case log::info: return \"info\";\n"
20134 "case log::warning: // fallthrough\n"
20135 "case log::error: return \"error\";\n"
20137 "switch (level) {\n"
20138 "case log::info: return \"info\";\n"
20139 "case log::warning: //fallthrough\n"
20140 "case log::error: return \"error\";\n"
20144 // Verify adjacent non-short case statements don't change the alignment, and
20145 // properly break the set of consecutive statements.
20146 verifyFormat("switch (level) {\n"
20147 "case log::critical:\n"
20149 " return \"critical\";\n"
20150 "case log::info: return \"info\";\n"
20151 "case log::warning: return \"warning\";\n"
20155 "case log::error: return \"error\";\n"
20156 "case log::severe: return \"severe\";\n"
20157 "case log::extra_critical:\n"
20159 " return \"extra critical\";\n"
20163 Alignment
.SpaceBeforeCaseColon
= true;
20164 verifyFormat("switch (level) {\n"
20165 "case log::info : return \"info\";\n"
20166 "case log::warning : return \"warning\";\n"
20167 "default : return \"default\";\n"
20170 Alignment
.SpaceBeforeCaseColon
= false;
20172 // Make sure we don't incorrectly align correctly across nested switch cases.
20173 verifyFormat("switch (level) {\n"
20174 "case log::info: return \"info\";\n"
20175 "case log::warning: return \"warning\";\n"
20176 "case log::other:\n"
20177 " switch (sublevel) {\n"
20178 " case log::info: return \"info\";\n"
20179 " case log::warning: return \"warning\";\n"
20182 "case log::error: return \"error\";\n"
20183 "default: return \"default\";\n"
20185 "switch (level) {\n"
20186 "case log::info: return \"info\";\n"
20187 "case log::warning: return \"warning\";\n"
20188 "case log::other: switch (sublevel) {\n"
20189 " case log::info: return \"info\";\n"
20190 " case log::warning: return \"warning\";\n"
20193 "case log::error: return \"error\";\n"
20194 "default: return \"default\";\n"
20198 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossEmptyLines
= true;
20200 verifyFormat("switch (level) {\n"
20201 "case log::info: return \"info\";\n"
20203 "case log::warning: return \"warning\";\n"
20205 "switch (level) {\n"
20206 "case log::info: return \"info\";\n"
20208 "case log::warning: return \"warning\";\n"
20212 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossComments
= true;
20214 verifyNoChange("switch (level) {\n"
20215 "case log::info: return \"info\";\n"
20217 "/* block comment */\n"
20219 "// line comment\n"
20220 "case log::warning: return \"warning\";\n"
20224 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossEmptyLines
= false;
20226 verifyFormat("switch (level) {\n"
20227 "case log::info: return \"info\";\n"
20229 "case log::warning: return \"warning\";\n"
20233 Alignment
.AlignConsecutiveShortCaseStatements
.AlignCaseColons
= true;
20235 verifyFormat("switch (level) {\n"
20236 "case log::info : return \"info\";\n"
20237 "case log::warning: return \"warning\";\n"
20238 "default : return \"default\";\n"
20242 // With AlignCaseColons, empty case statements don't break alignment of
20243 // consecutive case statements (and are aligned).
20244 verifyFormat("switch (level) {\n"
20245 "case log::info : return \"info\";\n"
20246 "case log::warning :\n"
20247 "case log::critical:\n"
20248 "default : return \"default\";\n"
20252 // Final non-short case labels shouldn't have their colon aligned
20253 verifyFormat("switch (level) {\n"
20254 "case log::info : return \"info\";\n"
20255 "case log::warning :\n"
20256 "case log::critical:\n"
20257 "case log::severe : return \"severe\";\n"
20260 " return \"default\";\n"
20264 // Verify adjacent non-short case statements break the set of consecutive
20265 // alignments and aren't aligned with adjacent non-short case statements if
20266 // AlignCaseColons is set.
20267 verifyFormat("switch (level) {\n"
20268 "case log::critical:\n"
20270 " return \"critical\";\n"
20271 "case log::info : return \"info\";\n"
20272 "case log::warning: return \"warning\";\n"
20276 "case log::error : return \"error\";\n"
20277 "case log::severe: return \"severe\";\n"
20278 "case log::extra_critical:\n"
20280 " return \"extra critical\";\n"
20284 Alignment
.SpaceBeforeCaseColon
= true;
20285 verifyFormat("switch (level) {\n"
20286 "case log::info : return \"info\";\n"
20287 "case log::warning : return \"warning\";\n"
20288 "case log::error :\n"
20289 "default : return \"default\";\n"
20294 TEST_F(FormatTest
, AlignWithLineBreaks
) {
20295 auto Style
= getLLVMStyleWithColumns(120);
20297 EXPECT_EQ(Style
.AlignConsecutiveAssignments
,
20298 FormatStyle::AlignConsecutiveStyle(
20299 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20300 /*AcrossComments=*/false, /*AlignCompound=*/false,
20301 /*AlignFunctionDeclarations=*/false,
20302 /*AlignFunctionPointers=*/false,
20303 /*PadOperators=*/true}));
20304 EXPECT_EQ(Style
.AlignConsecutiveDeclarations
,
20305 FormatStyle::AlignConsecutiveStyle(
20306 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20307 /*AcrossComments=*/false, /*AlignCompound=*/false,
20308 /*AlignFunctionDeclarations=*/true,
20309 /*AlignFunctionPointers=*/false,
20310 /*PadOperators=*/false}));
20311 verifyFormat("void foo() {\n"
20312 " int myVar = 5;\n"
20313 " double x = 3.14;\n"
20314 " auto str = \"Hello \"\n"
20316 " auto s = \"Hello \"\n"
20321 // clang-format off
20322 verifyFormat("void foo() {\n"
20323 " const int capacityBefore = Entries.capacity();\n"
20324 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20325 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20326 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20327 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20332 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20333 verifyFormat("void foo() {\n"
20334 " int myVar = 5;\n"
20335 " double x = 3.14;\n"
20336 " auto str = \"Hello \"\n"
20338 " auto s = \"Hello \"\n"
20343 // clang-format off
20344 verifyFormat("void foo() {\n"
20345 " const int capacityBefore = Entries.capacity();\n"
20346 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20347 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20348 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20349 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20354 Style
.AlignConsecutiveAssignments
.Enabled
= false;
20355 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20356 verifyFormat("void foo() {\n"
20357 " int myVar = 5;\n"
20358 " double x = 3.14;\n"
20359 " auto str = \"Hello \"\n"
20361 " auto s = \"Hello \"\n"
20366 // clang-format off
20367 verifyFormat("void foo() {\n"
20368 " const int capacityBefore = Entries.capacity();\n"
20369 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20370 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20371 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20372 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20377 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20378 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20380 verifyFormat("void foo() {\n"
20381 " int myVar = 5;\n"
20382 " double x = 3.14;\n"
20383 " auto str = \"Hello \"\n"
20385 " auto s = \"Hello \"\n"
20390 // clang-format off
20391 verifyFormat("void foo() {\n"
20392 " const int capacityBefore = Entries.capacity();\n"
20393 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20394 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20395 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
20396 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
20401 Style
= getLLVMStyleWithColumns(20);
20402 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20403 Style
.IndentWidth
= 4;
20405 verifyFormat("void foo() {\n"
20414 verifyFormat("unsigned i = 0;\n"
20420 Style
.ColumnLimit
= 120;
20422 // clang-format off
20423 verifyFormat("void SomeFunc() {\n"
20424 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20425 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20426 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20427 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20428 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
20429 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20434 Style
.BinPackArguments
= false;
20436 // clang-format off
20437 verifyFormat("void SomeFunc() {\n"
20438 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
20439 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20440 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
20441 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20442 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
20443 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
20449 TEST_F(FormatTest
, AlignWithInitializerPeriods
) {
20450 auto Style
= getLLVMStyleWithColumns(60);
20452 verifyFormat("void foo1(void) {\n"
20453 " BYTE p[1] = 1;\n"
20454 " A B = {.one_foooooooooooooooo = 2,\n"
20455 " .two_fooooooooooooo = 3,\n"
20456 " .three_fooooooooooooo = 4};\n"
20457 " BYTE payload = 2;\n"
20461 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20462 Style
.AlignConsecutiveDeclarations
.Enabled
= false;
20463 verifyFormat("void foo2(void) {\n"
20464 " BYTE p[1] = 1;\n"
20465 " A B = {.one_foooooooooooooooo = 2,\n"
20466 " .two_fooooooooooooo = 3,\n"
20467 " .three_fooooooooooooo = 4};\n"
20468 " BYTE payload = 2;\n"
20472 Style
.AlignConsecutiveAssignments
.Enabled
= false;
20473 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20474 verifyFormat("void foo3(void) {\n"
20475 " BYTE p[1] = 1;\n"
20476 " A B = {.one_foooooooooooooooo = 2,\n"
20477 " .two_fooooooooooooo = 3,\n"
20478 " .three_fooooooooooooo = 4};\n"
20479 " BYTE payload = 2;\n"
20483 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20484 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20485 verifyFormat("void foo4(void) {\n"
20486 " BYTE p[1] = 1;\n"
20487 " A B = {.one_foooooooooooooooo = 2,\n"
20488 " .two_fooooooooooooo = 3,\n"
20489 " .three_fooooooooooooo = 4};\n"
20490 " BYTE payload = 2;\n"
20495 TEST_F(FormatTest
, LinuxBraceBreaking
) {
20496 FormatStyle LinuxBraceStyle
= getLLVMStyle();
20497 LinuxBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Linux
;
20498 verifyFormat("namespace a\n"
20511 " void g() { return; }\n"
20516 "} // namespace a",
20518 verifyFormat("enum X {\n"
20522 verifyFormat("struct S {\n"
20530 " MyFavoriteType Value;\n"
20536 TEST_F(FormatTest
, MozillaBraceBreaking
) {
20537 FormatStyle MozillaBraceStyle
= getLLVMStyle();
20538 MozillaBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Mozilla
;
20539 MozillaBraceStyle
.FixNamespaceComments
= false;
20540 verifyFormat("namespace a {\n"
20550 " void g() { return; }\n"
20564 MozillaBraceStyle
);
20565 verifyFormat("struct S\n"
20575 " MyFavoriteType Value;\n"
20578 MozillaBraceStyle
);
20581 TEST_F(FormatTest
, StroustrupBraceBreaking
) {
20582 FormatStyle StroustrupBraceStyle
= getLLVMStyle();
20583 StroustrupBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
20584 verifyFormat("namespace a {\n"
20593 " void g() { return; }\n"
20598 "} // namespace a",
20599 StroustrupBraceStyle
);
20601 verifyFormat("void foo()\n"
20610 StroustrupBraceStyle
);
20612 verifyFormat("#ifdef _DEBUG\n"
20613 "int foo(int i = 0)\n"
20615 "int foo(int i = 5)\n"
20620 StroustrupBraceStyle
);
20622 verifyFormat("void foo() {}\n"
20632 StroustrupBraceStyle
);
20634 verifyFormat("void foobar() { int i = 5; }\n"
20638 "void bar() { foobar(); }\n"
20640 StroustrupBraceStyle
);
20643 TEST_F(FormatTest
, AllmanBraceBreaking
) {
20644 FormatStyle AllmanBraceStyle
= getLLVMStyle();
20645 AllmanBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
20647 verifyFormat("namespace a\n"
20651 "} // namespace a",
20659 verifyFormat("namespace a\n"
20671 " void g() { return; }\n"
20680 "} // namespace a",
20683 verifyFormat("void f()\n"
20689 " else if (false)\n"
20700 verifyFormat("void f()\n"
20702 " for (int i = 0; i < 10; ++i)\n"
20713 " } while (false)\n"
20717 verifyFormat("void f(int a)\n"
20737 verifyFormat("enum X\n"
20742 verifyFormat("enum X\n"
20748 verifyFormat("@interface BSApplicationController ()\n"
20751 " id _extraIvar;\n"
20756 verifyFormat("#ifdef _DEBUG\n"
20757 "int foo(int i = 0)\n"
20759 "int foo(int i = 5)\n"
20766 verifyFormat("void foo() {}\n"
20778 verifyFormat("void foobar() { int i = 5; }\n"
20782 "void bar() { foobar(); }\n"
20786 EXPECT_EQ(AllmanBraceStyle
.AllowShortLambdasOnASingleLine
,
20787 FormatStyle::SLS_All
);
20789 verifyFormat("[](int i) { return i + 2; };\n"
20790 "[](int i, int j)\n"
20792 " auto x = i + j;\n"
20793 " auto y = i * j;\n"
20798 " auto shortLambda = [](int i) { return i + 2; };\n"
20799 " auto longLambda = [](int i, int j)\n"
20801 " auto x = i + j;\n"
20802 " auto y = i * j;\n"
20808 AllmanBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
20810 verifyFormat("[](int i)\n"
20814 "[](int i, int j)\n"
20816 " auto x = i + j;\n"
20817 " auto y = i * j;\n"
20822 " auto shortLambda = [](int i)\n"
20826 " auto longLambda = [](int i, int j)\n"
20828 " auto x = i + j;\n"
20829 " auto y = i * j;\n"
20836 AllmanBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_All
;
20838 // This shouldn't affect ObjC blocks..
20839 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20844 verifyFormat("void (^block)(void) = ^{\n"
20849 // .. or dict literals.
20850 verifyFormat("void f()\n"
20853 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20856 verifyFormat("void f()\n"
20859 " [object someMethod:@{a : @\"b\"}];\n"
20862 verifyFormat("int f()\n"
20868 AllmanBraceStyle
.ColumnLimit
= 19;
20869 verifyFormat("void f() { int i; }", AllmanBraceStyle
);
20870 AllmanBraceStyle
.ColumnLimit
= 18;
20871 verifyFormat("void f()\n"
20876 AllmanBraceStyle
.ColumnLimit
= 80;
20878 FormatStyle BreakBeforeBraceShortIfs
= AllmanBraceStyle
;
20879 BreakBeforeBraceShortIfs
.AllowShortIfStatementsOnASingleLine
=
20880 FormatStyle::SIS_WithoutElse
;
20881 BreakBeforeBraceShortIfs
.AllowShortLoopsOnASingleLine
= true;
20882 verifyFormat("void f(bool b)\n"
20889 BreakBeforeBraceShortIfs
);
20890 verifyFormat("void f(bool b)\n"
20892 " if constexpr (b)\n"
20897 BreakBeforeBraceShortIfs
);
20898 verifyFormat("void f(bool b)\n"
20900 " if CONSTEXPR (b)\n"
20905 BreakBeforeBraceShortIfs
);
20906 verifyFormat("void f(bool b)\n"
20908 " if (b) return;\n"
20910 BreakBeforeBraceShortIfs
);
20911 verifyFormat("void f(bool b)\n"
20913 " if constexpr (b) return;\n"
20915 BreakBeforeBraceShortIfs
);
20916 verifyFormat("void f(bool b)\n"
20918 " if CONSTEXPR (b) return;\n"
20920 BreakBeforeBraceShortIfs
);
20921 verifyFormat("void f(bool b)\n"
20928 BreakBeforeBraceShortIfs
);
20931 TEST_F(FormatTest
, WhitesmithsBraceBreaking
) {
20932 FormatStyle WhitesmithsBraceStyle
= getLLVMStyleWithColumns(0);
20933 WhitesmithsBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
20935 // Make a few changes to the style for testing purposes
20936 WhitesmithsBraceStyle
.AllowShortFunctionsOnASingleLine
=
20937 FormatStyle::SFS_Empty
;
20938 WhitesmithsBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
20940 // FIXME: this test case can't decide whether there should be a blank line
20941 // after the ~D() line or not. It adds one if one doesn't exist in the test
20942 // and it removes the line if one exists.
20944 verifyFormat("class A;\n"
20960 " } // namespace B",
20961 WhitesmithsBraceStyle);
20964 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_None
;
20965 verifyFormat("namespace a\n"
20986 " } // namespace a",
20987 WhitesmithsBraceStyle
);
20989 verifyFormat("namespace a\n"
21012 " } // namespace b\n"
21013 " } // namespace a",
21014 WhitesmithsBraceStyle
);
21016 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_Inner
;
21017 verifyFormat("namespace a\n"
21040 " } // namespace b\n"
21041 " } // namespace a",
21042 WhitesmithsBraceStyle
);
21044 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_All
;
21045 verifyFormat("namespace a\n"
21068 " } // namespace b\n"
21069 " } // namespace a",
21070 WhitesmithsBraceStyle
);
21072 verifyFormat("void f()\n"
21078 " else if (false)\n"
21087 WhitesmithsBraceStyle
);
21089 verifyFormat("void f()\n"
21091 " for (int i = 0; i < 10; ++i)\n"
21102 " } while (false)\n"
21104 WhitesmithsBraceStyle
);
21106 WhitesmithsBraceStyle
.IndentCaseLabels
= true;
21107 verifyFormat("void switchTest1(int a)\n"
21117 WhitesmithsBraceStyle
);
21119 verifyFormat("void switchTest2(int a)\n"
21137 WhitesmithsBraceStyle
);
21139 verifyFormat("void switchTest3(int a)\n"
21155 WhitesmithsBraceStyle
);
21157 WhitesmithsBraceStyle
.IndentCaseLabels
= false;
21159 verifyFormat("void switchTest4(int a)\n"
21169 WhitesmithsBraceStyle
);
21171 verifyFormat("void switchTest5(int a)\n"
21190 WhitesmithsBraceStyle
);
21192 verifyFormat("void switchTest6(int a)\n"
21208 WhitesmithsBraceStyle
);
21210 verifyFormat("enum X\n"
21212 " Y = 0, // testing\n"
21214 WhitesmithsBraceStyle
);
21216 verifyFormat("enum X\n"
21220 WhitesmithsBraceStyle
);
21221 verifyFormat("enum X\n"
21226 WhitesmithsBraceStyle
);
21228 verifyFormat("@interface BSApplicationController ()\n"
21231 " id _extraIvar;\n"
21234 WhitesmithsBraceStyle
);
21236 verifyFormat("#ifdef _DEBUG\n"
21237 "int foo(int i = 0)\n"
21239 "int foo(int i = 5)\n"
21244 WhitesmithsBraceStyle
);
21246 verifyFormat("void foo() {}\n"
21256 WhitesmithsBraceStyle
);
21258 verifyFormat("void foobar()\n"
21270 WhitesmithsBraceStyle
);
21272 // This shouldn't affect ObjC blocks..
21273 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
21277 WhitesmithsBraceStyle
);
21278 verifyFormat("void (^block)(void) = ^{\n"
21282 WhitesmithsBraceStyle
);
21283 // .. or dict literals.
21284 verifyFormat("void f()\n"
21286 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
21288 WhitesmithsBraceStyle
);
21290 verifyFormat("int f()\n"
21294 WhitesmithsBraceStyle
);
21296 FormatStyle BreakBeforeBraceShortIfs
= WhitesmithsBraceStyle
;
21297 BreakBeforeBraceShortIfs
.AllowShortIfStatementsOnASingleLine
=
21298 FormatStyle::SIS_OnlyFirstIf
;
21299 BreakBeforeBraceShortIfs
.AllowShortLoopsOnASingleLine
= true;
21300 verifyFormat("void f(bool b)\n"
21307 BreakBeforeBraceShortIfs
);
21308 verifyFormat("void f(bool b)\n"
21310 " if (b) return;\n"
21312 BreakBeforeBraceShortIfs
);
21313 verifyFormat("void f(bool b)\n"
21320 BreakBeforeBraceShortIfs
);
21323 TEST_F(FormatTest
, GNUBraceBreaking
) {
21324 FormatStyle GNUBraceStyle
= getLLVMStyle();
21325 GNUBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_GNU
;
21326 verifyFormat("namespace a\n"
21342 " void g() { return; }\n"
21344 "} // namespace a",
21347 verifyFormat("void f()\n"
21353 " else if (false)\n"
21364 verifyFormat("void f()\n"
21366 " for (int i = 0; i < 10; ++i)\n"
21378 " while (false);\n"
21382 verifyFormat("void f(int a)\n"
21402 verifyFormat("enum X\n"
21408 verifyFormat("@interface BSApplicationController ()\n"
21411 " id _extraIvar;\n"
21416 verifyFormat("#ifdef _DEBUG\n"
21417 "int foo(int i = 0)\n"
21419 "int foo(int i = 5)\n"
21426 verifyFormat("void foo() {}\n"
21438 verifyFormat("void foobar() { int i = 5; }\n"
21442 "void bar() { foobar(); }\n"
21447 TEST_F(FormatTest
, WebKitBraceBreaking
) {
21448 FormatStyle WebKitBraceStyle
= getLLVMStyle();
21449 WebKitBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_WebKit
;
21450 WebKitBraceStyle
.FixNamespaceComments
= false;
21451 verifyFormat("namespace a {\n"
21460 " void g() { return; }\n"
21473 verifyFormat("struct S {\n"
21480 " MyFavoriteType Value;\n"
21486 TEST_F(FormatTest
, CatchExceptionReferenceBinding
) {
21487 verifyFormat("void f() {\n"
21489 " } catch (const Exception &e) {\n"
21494 TEST_F(FormatTest
, CatchAlignArrayOfStructuresRightAlignment
) {
21495 auto Style
= getLLVMStyle();
21496 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
21497 verifyNoCrash("f({\n"
21498 "table({}, table({{\"\", false}}, {}))\n"
21502 Style
.AlignConsecutiveAssignments
.Enabled
= true;
21503 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
21504 verifyFormat("struct test demo[] = {\n"
21505 " {56, 23, \"hello\"},\n"
21506 " {-1, 93463, \"world\"},\n"
21507 " { 7, 5, \"!!\"}\n"
21511 verifyFormat("struct test demo[] = {\n"
21512 " {56, 23, \"hello\"}, // first line\n"
21513 " {-1, 93463, \"world\"}, // second line\n"
21514 " { 7, 5, \"!!\"} // third line\n"
21518 verifyFormat("struct test demo[4] = {\n"
21519 " { 56, 23, 21, \"oh\"}, // first line\n"
21520 " { -1, 93463, 22, \"my\"}, // second line\n"
21521 " { 7, 5, 1, \"goodness\"} // third line\n"
21522 " {234, 5, 1, \"gracious\"} // fourth line\n"
21526 verifyFormat("struct test demo[3] = {\n"
21527 " {56, 23, \"hello\"},\n"
21528 " {-1, 93463, \"world\"},\n"
21529 " { 7, 5, \"!!\"}\n"
21533 verifyFormat("struct test demo[3] = {\n"
21534 " {int{56}, 23, \"hello\"},\n"
21535 " {int{-1}, 93463, \"world\"},\n"
21536 " { int{7}, 5, \"!!\"}\n"
21540 verifyFormat("struct test demo[] = {\n"
21541 " {56, 23, \"hello\"},\n"
21542 " {-1, 93463, \"world\"},\n"
21543 " { 7, 5, \"!!\"},\n"
21547 verifyFormat("test demo[] = {\n"
21548 " {56, 23, \"hello\"},\n"
21549 " {-1, 93463, \"world\"},\n"
21550 " { 7, 5, \"!!\"},\n"
21554 verifyFormat("demo = std::array<struct test, 3>{\n"
21555 " test{56, 23, \"hello\"},\n"
21556 " test{-1, 93463, \"world\"},\n"
21557 " test{ 7, 5, \"!!\"},\n"
21561 verifyFormat("test demo[] = {\n"
21562 " {56, 23, \"hello\"},\n"
21564 " {-1, 93463, \"world\"},\n"
21566 " { 7, 5, \"!!\"}\n"
21571 "test demo[] = {\n"
21573 " \"hello world i am a very long line that really, in any\"\n"
21574 " \"just world, ought to be split over multiple lines\"},\n"
21575 " {-1, 93463, \"world\"},\n"
21576 " {56, 5, \"!!\"}\n"
21580 verifyNoCrash("Foo f[] = {\n"
21585 verifyNoCrash("Foo foo[] = {\n"
21587 " [1] { 1, 1, },\n"
21588 " [2] { 1, 1, },\n"
21591 verifyNoCrash("test arr[] = {\n"
21592 "#define FOO(i) {i, i},\n"
21593 "SOME_GENERATOR(FOO)\n"
21598 verifyFormat("return GradForUnaryCwise(g, {\n"
21599 " {{\"sign\"}, \"Sign\", "
21600 " {\"x\", \"dy\"}},\n"
21601 " { {\"dx\"}, \"Mul\", {\"dy\""
21606 Style
.Cpp11BracedListStyle
= false;
21607 verifyFormat("struct test demo[] = {\n"
21608 " { 56, 23, \"hello\" },\n"
21609 " { -1, 93463, \"world\" },\n"
21610 " { 7, 5, \"!!\" }\n"
21613 Style
.Cpp11BracedListStyle
= true;
21615 Style
.ColumnLimit
= 0;
21617 "test demo[] = {\n"
21618 " {56, 23, \"hello world i am a very long line that really, "
21619 "in any just world, ought to be split over multiple lines\"},\n"
21625 "test demo[] = {{56, 23, \"hello world i am a very long line "
21626 "that really, in any just world, ought to be split over multiple "
21627 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21630 Style
.ColumnLimit
= 80;
21631 verifyFormat("test demo[] = {\n"
21632 " {56, 23, /* a comment */ \"hello\"},\n"
21633 " {-1, 93463, \"world\"},\n"
21634 " { 7, 5, \"!!\"}\n"
21638 verifyFormat("test demo[] = {\n"
21639 " {56, 23, \"hello\"},\n"
21640 " {-1, 93463, \"world\" /* comment here */},\n"
21641 " { 7, 5, \"!!\"}\n"
21645 verifyFormat("test demo[] = {\n"
21646 " {56, /* a comment */ 23, \"hello\"},\n"
21647 " {-1, 93463, \"world\"},\n"
21648 " { 7, 5, \"!!\"}\n"
21652 Style
.ColumnLimit
= 20;
21653 verifyFormat("demo = std::array<\n"
21654 " struct test, 3>{\n"
21659 " \"am a very \"\n"
21660 " \"long line \"\n"
21671 " test{-1, 93463,\n"
21676 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21677 "i am a very long line that really, in any just world, ought "
21678 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21679 "test{7, 5, \"!!\"},};",
21681 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21682 Style
= getLLVMStyleWithColumns(50);
21683 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
21684 verifyFormat("static A x = {\n"
21685 " {{init1, init2, init3, init4},\n"
21686 " {init1, init2, init3, init4}}\n"
21689 // TODO: Fix the indentations below when this option is fully functional.
21691 verifyFormat("int a[][] = {\n"
21699 Style
.ColumnLimit
= 100;
21701 "test demo[] = {\n"
21703 " \"hello world i am a very long line that really, in any just world"
21704 ", ought to be split over \"\n"
21705 " \"multiple lines\" },\n"
21706 " {-1, 93463, \"world\"},\n"
21707 " { 7, 5, \"!!\"},\n"
21709 "test demo[] = {{56, 23, \"hello world i am a very long line "
21710 "that really, in any just world, ought to be split over multiple "
21711 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21714 Style
= getLLVMStyleWithColumns(50);
21715 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
21716 verifyFormat("struct test demo[] = {\n"
21717 " {56, 23, \"hello\"},\n"
21718 " {-1, 93463, \"world\"},\n"
21719 " { 7, 5, \"!!\"}\n"
21722 " {{init1, init2, init3, init4},\n"
21723 " {init1, init2, init3, init4}}\n"
21726 Style
.ColumnLimit
= 100;
21727 Style
.AlignConsecutiveAssignments
.AcrossComments
= true;
21728 Style
.AlignConsecutiveDeclarations
.AcrossComments
= true;
21729 verifyFormat("struct test demo[] = {\n"
21730 " {56, 23, \"hello\"},\n"
21731 " {-1, 93463, \"world\"},\n"
21732 " { 7, 5, \"!!\"}\n"
21734 "struct test demo[4] = {\n"
21735 " { 56, 23, 21, \"oh\"}, // first line\n"
21736 " { -1, 93463, 22, \"my\"}, // second line\n"
21737 " { 7, 5, 1, \"goodness\"} // third line\n"
21738 " {234, 5, 1, \"gracious\"} // fourth line\n"
21742 "test demo[] = {\n"
21744 " \"hello world i am a very long line that really, in any just world"
21745 ", ought to be split over \"\n"
21746 " \"multiple lines\", 23},\n"
21747 " {-1, \"world\", 93463},\n"
21748 " { 7, \"!!\", 5},\n"
21750 "test demo[] = {{56, \"hello world i am a very long line "
21751 "that really, in any just world, ought to be split over multiple "
21752 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
21756 TEST_F(FormatTest
, CatchAlignArrayOfStructuresLeftAlignment
) {
21757 auto Style
= getLLVMStyle();
21758 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
21759 /* FIXME: This case gets misformatted.
21760 verifyFormat("auto foo = Items{\n"
21761 " Section{0, bar(), },\n"
21762 " Section{1, boo() }\n"
21766 verifyFormat("auto foo = Items{\n"
21772 verifyFormat("struct test demo[] = {\n"
21773 " {56, 23, \"hello\"},\n"
21774 " {-1, 93463, \"world\"},\n"
21775 " {7, 5, \"!!\" }\n"
21778 verifyFormat("struct test demo[] = {\n"
21779 " {56, 23, \"hello\"}, // first line\n"
21780 " {-1, 93463, \"world\"}, // second line\n"
21781 " {7, 5, \"!!\" } // third line\n"
21784 verifyFormat("struct test demo[4] = {\n"
21785 " {56, 23, 21, \"oh\" }, // first line\n"
21786 " {-1, 93463, 22, \"my\" }, // second line\n"
21787 " {7, 5, 1, \"goodness\"} // third line\n"
21788 " {234, 5, 1, \"gracious\"} // fourth line\n"
21791 verifyFormat("struct test demo[3] = {\n"
21792 " {56, 23, \"hello\"},\n"
21793 " {-1, 93463, \"world\"},\n"
21794 " {7, 5, \"!!\" }\n"
21798 verifyFormat("struct test demo[3] = {\n"
21799 " {int{56}, 23, \"hello\"},\n"
21800 " {int{-1}, 93463, \"world\"},\n"
21801 " {int{7}, 5, \"!!\" }\n"
21804 verifyFormat("struct test demo[] = {\n"
21805 " {56, 23, \"hello\"},\n"
21806 " {-1, 93463, \"world\"},\n"
21807 " {7, 5, \"!!\" },\n"
21810 verifyFormat("test demo[] = {\n"
21811 " {56, 23, \"hello\"},\n"
21812 " {-1, 93463, \"world\"},\n"
21813 " {7, 5, \"!!\" },\n"
21816 verifyFormat("demo = std::array<struct test, 3>{\n"
21817 " test{56, 23, \"hello\"},\n"
21818 " test{-1, 93463, \"world\"},\n"
21819 " test{7, 5, \"!!\" },\n"
21822 verifyFormat("test demo[] = {\n"
21823 " {56, 23, \"hello\"},\n"
21825 " {-1, 93463, \"world\"},\n"
21827 " {7, 5, \"!!\" }\n"
21831 "test demo[] = {\n"
21833 " \"hello world i am a very long line that really, in any\"\n"
21834 " \"just world, ought to be split over multiple lines\"},\n"
21835 " {-1, 93463, \"world\" },\n"
21836 " {56, 5, \"!!\" }\n"
21840 verifyNoCrash("Foo f[] = {\n"
21845 verifyNoCrash("Foo foo[] = {\n"
21847 " [1] { 1, 1, },\n"
21848 " [2] { 1, 1, },\n"
21851 verifyNoCrash("test arr[] = {\n"
21852 "#define FOO(i) {i, i},\n"
21853 "SOME_GENERATOR(FOO)\n"
21858 verifyFormat("return GradForUnaryCwise(g, {\n"
21859 " {{\"sign\"}, \"Sign\", {\"x\", "
21861 " {{\"dx\"}, \"Mul\", "
21862 "{\"dy\", \"sign\"}},\n"
21866 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
21867 verifyFormat("#define FOO \\\n"
21868 " int foo[][2] = { \\\n"
21873 Style
.Cpp11BracedListStyle
= false;
21874 verifyFormat("struct test demo[] = {\n"
21875 " { 56, 23, \"hello\" },\n"
21876 " { -1, 93463, \"world\" },\n"
21877 " { 7, 5, \"!!\" }\n"
21880 Style
.Cpp11BracedListStyle
= true;
21882 Style
.ColumnLimit
= 0;
21884 "test demo[] = {\n"
21885 " {56, 23, \"hello world i am a very long line that really, in any "
21886 "just world, ought to be split over multiple lines\"},\n"
21887 " {-1, 93463, \"world\" "
21892 "test demo[] = {{56, 23, \"hello world i am a very long line "
21893 "that really, in any just world, ought to be split over multiple "
21894 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21897 Style
.ColumnLimit
= 80;
21898 verifyFormat("test demo[] = {\n"
21899 " {56, 23, /* a comment */ \"hello\"},\n"
21900 " {-1, 93463, \"world\" },\n"
21901 " {7, 5, \"!!\" }\n"
21905 verifyFormat("test demo[] = {\n"
21906 " {56, 23, \"hello\" },\n"
21907 " {-1, 93463, \"world\" /* comment here */},\n"
21908 " {7, 5, \"!!\" }\n"
21912 verifyFormat("test demo[] = {\n"
21913 " {56, /* a comment */ 23, \"hello\"},\n"
21914 " {-1, 93463, \"world\"},\n"
21915 " {7, 5, \"!!\" }\n"
21918 verifyFormat("Foo foo = {\n"
21924 Style
.ColumnLimit
= 20;
21925 // FIXME: unstable test case
21927 "demo = std::array<\n"
21928 " struct test, 3>{\n"
21933 " \"am a very \"\n"
21934 " \"long line \"\n"
21945 " test{-1, 93463,\n"
21950 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21951 "i am a very long line that really, in any just world, ought "
21952 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21953 "test{7, 5, \"!!\"},};",
21956 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21957 Style
= getLLVMStyleWithColumns(50);
21958 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
21959 verifyFormat("static A x = {\n"
21960 " {{init1, init2, init3, init4},\n"
21961 " {init1, init2, init3, init4}}\n"
21964 Style
.ColumnLimit
= 100;
21966 "test demo[] = {\n"
21968 " \"hello world i am a very long line that really, in any just world"
21969 ", ought to be split over \"\n"
21970 " \"multiple lines\" },\n"
21971 " {-1, 93463, \"world\"},\n"
21972 " {7, 5, \"!!\" },\n"
21974 "test demo[] = {{56, 23, \"hello world i am a very long line "
21975 "that really, in any just world, ought to be split over multiple "
21976 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21979 Style
.ColumnLimit
= 25;
21980 verifyNoCrash("Type foo{\n"
21989 verifyNoCrash("Type object[X][Y] = {\n"
21990 " {{val}, {val}, {val}},\n"
21991 " {{val}, {val}, // some comment\n"
21996 Style
.ColumnLimit
= 120;
21999 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, "
22000 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, "
22001 "\"00000000000000000000000000000000000000000000000000000000"
22002 "00000000000000000000000000000000000000000000000000000000\" },\n"
22006 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
22007 Style
.SpacesInParensOptions
.Other
= true;
22008 verifyFormat("Foo foo[] = {\n"
22015 TEST_F(FormatTest
, UnderstandsPragmas
) {
22016 verifyFormat("#pragma omp reduction(| : var)");
22017 verifyFormat("#pragma omp reduction(+ : var)");
22019 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22020 "(including parentheses).",
22021 "#pragma mark Any non-hyphenated or hyphenated string "
22022 "(including parentheses).");
22024 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
22025 "(including parentheses).",
22026 "#pragma mark Any non-hyphenated or hyphenated string "
22027 "(including parentheses).");
22029 verifyFormat("#pragma comment(linker, \\\n"
22030 " \"argument\" \\\n"
22032 "#pragma comment(linker, \\\n"
22033 " \"argument\" \\\n"
22035 getStyleWithColumns(
22036 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp
), 32));
22039 TEST_F(FormatTest
, UnderstandsPragmaOmpTarget
) {
22040 verifyFormat("#pragma omp target map(to : var)");
22041 verifyFormat("#pragma omp target map(to : var[ : N])");
22042 verifyFormat("#pragma omp target map(to : var[0 : N])");
22043 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
22046 "#pragma omp target \\\n"
22047 " reduction(+ : var) \\\n"
22048 " map(to : A[0 : N]) \\\n"
22049 " map(to : B[0 : N]) \\\n"
22050 " map(from : C[0 : N]) \\\n"
22051 " firstprivate(i) \\\n"
22052 " firstprivate(j) \\\n"
22053 " firstprivate(k)",
22054 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
22055 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
22056 getLLVMStyleWithColumns(26));
22059 TEST_F(FormatTest
, UnderstandPragmaOption
) {
22060 verifyFormat("#pragma option -C -A");
22062 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
22065 TEST_F(FormatTest
, UnderstandPragmaRegion
) {
22066 auto Style
= getLLVMStyleWithColumns(0);
22067 verifyFormat("#pragma region TEST(FOO : BAR)", Style
);
22068 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style
);
22071 TEST_F(FormatTest
, OptimizeBreakPenaltyVsExcess
) {
22072 FormatStyle Style
= getLLVMStyleWithColumns(20);
22075 verifyFormat("/*\n"
22080 " *\t9012345 /8901\n"
22083 verifyFormat("/*\n"
22088 " *345678\t/8901\n"
22092 verifyFormat("int a; // the\n"
22095 verifyNoChange("int a; /* first line\n"
22101 verifyFormat("int a; // first line\n"
22105 "int a; // first line\n"
22106 " // second line\n"
22110 Style
.PenaltyExcessCharacter
= 90;
22111 verifyFormat("int a; // the comment", Style
);
22112 verifyFormat("int a; // the comment\n"
22114 "int a; // the comment aaa", Style
);
22115 verifyNoChange("int a; /* first line\n"
22120 verifyFormat("int a; // first line\n"
22121 " // second line\n"
22124 // FIXME: Investigate why this is not getting the same layout as the test
22126 verifyFormat("int a; /* first line\n"
22130 "int a; /* first line second line third line"
22134 verifyFormat("// foo bar baz bazfoo\n"
22135 "// foo bar foo bar",
22136 "// foo bar baz bazfoo\n"
22137 "// foo bar foo bar",
22139 verifyFormat("// foo bar baz bazfoo\n"
22140 "// foo bar foo bar",
22141 "// foo bar baz bazfoo\n"
22142 "// foo bar foo bar",
22145 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
22147 verifyFormat("// foo bar baz bazfoo\n"
22149 "// foo bar baz bazfoo bar\n"
22153 // FIXME: unstable test case
22154 EXPECT_EQ("// foo bar baz bazfoo\n"
22155 "// foo bar baz bazfoo\n"
22157 format("// foo bar baz bazfoo\n"
22158 "// foo bar baz bazfoo bar\n"
22162 // FIXME: unstable test case
22163 EXPECT_EQ("// foo bar baz bazfoo\n"
22164 "// foo bar baz bazfoo\n"
22166 format("// foo bar baz bazfoo\n"
22167 "// foo bar baz bazfoo bar\n"
22171 // Make sure we do not keep protruding characters if strict mode reflow is
22172 // cheaper than keeping protruding characters.
22173 Style
.ColumnLimit
= 21;
22174 verifyFormat("// foo foo foo foo\n"
22175 "// foo foo foo foo\n"
22176 "// foo foo foo foo",
22177 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style
);
22179 verifyFormat("int a = /* long block\n"
22182 "int a = /* long block comment */ 42;", Style
);
22185 TEST_F(FormatTest
, BreakPenaltyAfterLParen
) {
22186 FormatStyle Style
= getLLVMStyle();
22187 Style
.ColumnLimit
= 8;
22188 Style
.PenaltyExcessCharacter
= 15;
22189 verifyFormat("int foo(\n"
22190 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22192 Style
.PenaltyBreakOpenParenthesis
= 200;
22193 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
22195 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
22199 TEST_F(FormatTest
, BreakPenaltyAfterCastLParen
) {
22200 FormatStyle Style
= getLLVMStyle();
22201 Style
.ColumnLimit
= 5;
22202 Style
.PenaltyExcessCharacter
= 150;
22203 verifyFormat("foo((\n"
22204 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22207 Style
.PenaltyBreakOpenParenthesis
= 100'000;
22208 verifyFormat("foo((int)\n"
22209 " aaaaaaaaaaaaaaaaaaaaaaaa);",
22211 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
22215 TEST_F(FormatTest
, BreakPenaltyAfterForLoopLParen
) {
22216 FormatStyle Style
= getLLVMStyle();
22217 Style
.ColumnLimit
= 4;
22218 Style
.PenaltyExcessCharacter
= 100;
22219 verifyFormat("for (\n"
22220 " int iiiiiiiiiiiiiiiii =\n"
22222 " iiiiiiiiiiiiiiiii <\n"
22224 " iiiiiiiiiiiiiiiii++) {\n"
22228 Style
.PenaltyBreakOpenParenthesis
= 1250;
22229 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
22231 " iiiiiiiiiiiiiiiii <\n"
22233 " iiiiiiiiiiiiiiiii++) {\n"
22236 " int iiiiiiiiiiiiiiiii =\n"
22238 " iiiiiiiiiiiiiiiii <\n"
22240 " iiiiiiiiiiiiiiiii++) {\n"
22245 TEST_F(FormatTest
, BreakPenaltyScopeResolution
) {
22246 FormatStyle Style
= getLLVMStyle();
22247 Style
.ColumnLimit
= 20;
22248 Style
.PenaltyExcessCharacter
= 100;
22249 verifyFormat("unsigned long\n"
22252 Style
.PenaltyBreakScopeResolution
= 10;
22253 verifyFormat("unsigned long foo::\n"
22258 TEST_F(FormatTest
, WorksFor8bitEncodings
) {
22259 // FIXME: unstable test case
22260 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
22261 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
22262 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
22263 "\"\xef\xee\xf0\xf3...\"",
22264 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
22265 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
22266 "\xef\xee\xf0\xf3...\"",
22267 getLLVMStyleWithColumns(12)));
22270 TEST_F(FormatTest
, HandlesUTF8BOM
) {
22271 verifyFormat("\xef\xbb\xbf");
22272 verifyFormat("\xef\xbb\xbf#include <iostream>");
22273 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
22275 auto Style
= getLLVMStyle();
22276 Style
.KeepEmptyLines
.AtStartOfFile
= false;
22277 verifyFormat("\xef\xbb\xbf#include <iostream>",
22278 "\xef\xbb\xbf\n#include <iostream>", Style
);
22281 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
22282 #if !defined(_MSC_VER)
22284 TEST_F(FormatTest
, CountsUTF8CharactersProperly
) {
22285 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
22286 getLLVMStyleWithColumns(35));
22287 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
22288 getLLVMStyleWithColumns(31));
22289 verifyFormat("// Однажды в студёную зимнюю пору...",
22290 getLLVMStyleWithColumns(36));
22291 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
22292 verifyFormat("/* Однажды в студёную зимнюю пору... */",
22293 getLLVMStyleWithColumns(39));
22294 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
22295 getLLVMStyleWithColumns(35));
22298 TEST_F(FormatTest
, SplitsUTF8Strings
) {
22299 // Non-printable characters' width is currently considered to be the length in
22300 // bytes in UTF8. The characters can be displayed in very different manner
22301 // (zero-width, single width with a substitution glyph, expanded to their code
22302 // (e.g. "<8d>"), so there's no single correct way to handle them.
22303 // FIXME: unstable test case
22304 EXPECT_EQ("\"aaaaÄ\"\n"
22306 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22307 // FIXME: unstable test case
22308 EXPECT_EQ("\"aaaaaaaÄ\"\n"
22310 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
22311 // FIXME: unstable test case
22312 EXPECT_EQ("\"Однажды, в \"\n"
22316 format("\"Однажды, в студёную зимнюю пору,\"",
22317 getLLVMStyleWithColumns(13)));
22318 // FIXME: unstable test case
22324 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
22325 // FIXME: unstable test case
22326 EXPECT_EQ("\"一\t\"\n"
22333 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
22334 getLLVMStyleWithColumns(11)));
22336 // UTF8 character in an escape sequence.
22337 // FIXME: unstable test case
22338 EXPECT_EQ("\"aaaaaa\"\n"
22340 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
22343 TEST_F(FormatTest
, HandlesDoubleWidthCharsInMultiLineStrings
) {
22344 verifyFormat("const char *sssss =\n"
22347 "const char *sssss = \"一二三四五六七八\\\n"
22349 getLLVMStyleWithColumns(30));
22352 TEST_F(FormatTest
, SplitsUTF8LineComments
) {
22353 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
22354 verifyFormat("// Я из лесу\n"
22358 "// Я из лесу вышел; был сильный мороз.",
22359 getLLVMStyleWithColumns(13));
22360 verifyFormat("// 一二三\n"
22364 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
22367 TEST_F(FormatTest
, SplitsUTF8BlockComments
) {
22368 verifyFormat("/* Гляжу,\n"
22376 "/* Гляжу, поднимается медленно в гору\n"
22377 " * Лошадка, везущая хворосту воз. */",
22378 getLLVMStyleWithColumns(13));
22379 verifyFormat("/* 一二三\n"
22383 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
22384 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
22387 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
22392 TEST_F(FormatTest
, ConstructorInitializerIndentWidth
) {
22393 FormatStyle Style
= getLLVMStyle();
22395 Style
.ConstructorInitializerIndentWidth
= 4;
22397 "SomeClass::Constructor()\n"
22398 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22399 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22402 Style
.ConstructorInitializerIndentWidth
= 2;
22404 "SomeClass::Constructor()\n"
22405 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22406 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22409 Style
.ConstructorInitializerIndentWidth
= 0;
22411 "SomeClass::Constructor()\n"
22412 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
22413 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
22415 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
22417 "SomeLongTemplateVariableName<\n"
22418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
22420 verifyFormat("bool smaller = 1 < "
22421 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
22423 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
22426 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
22427 verifyFormat("SomeClass::Constructor() :\n"
22428 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
22429 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
22433 TEST_F(FormatTest
, BreakConstructorInitializersBeforeComma
) {
22434 FormatStyle Style
= getLLVMStyle();
22435 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
22436 Style
.ConstructorInitializerIndentWidth
= 4;
22437 verifyFormat("SomeClass::Constructor()\n"
22442 verifyFormat("SomeClass::Constructor()\n"
22446 Style
.ColumnLimit
= 0;
22447 verifyFormat("SomeClass::Constructor()\n"
22450 verifyFormat("SomeClass::Constructor() noexcept\n"
22453 verifyFormat("SomeClass::Constructor()\n"
22458 verifyFormat("SomeClass::Constructor()\n"
22465 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
22466 verifyFormat("SomeClass::Constructor()\n"
22471 verifyFormat("SomeClass::Constructor()\n"
22475 Style
.ColumnLimit
= 80;
22476 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
22477 Style
.ConstructorInitializerIndentWidth
= 2;
22478 verifyFormat("SomeClass::Constructor()\n"
22484 Style
.ConstructorInitializerIndentWidth
= 0;
22485 verifyFormat("SomeClass::Constructor()\n"
22491 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
22492 Style
.ConstructorInitializerIndentWidth
= 4;
22493 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style
);
22495 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22498 "SomeClass::Constructor()\n"
22499 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22501 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
22502 verifyFormat("SomeClass::Constructor()\n"
22503 " : aaaaaaaa(aaaaaaaa) {}",
22505 verifyFormat("SomeClass::Constructor()\n"
22506 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
22509 "SomeClass::Constructor()\n"
22510 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
22513 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
22514 Style
.ConstructorInitializerIndentWidth
= 4;
22515 Style
.ColumnLimit
= 60;
22516 verifyFormat("SomeClass::Constructor()\n"
22517 " : aaaaaaaa(aaaaaaaa)\n"
22518 " , aaaaaaaa(aaaaaaaa)\n"
22519 " , aaaaaaaa(aaaaaaaa) {}",
22521 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
22522 verifyFormat("SomeClass::Constructor()\n"
22523 " : aaaaaaaa(aaaaaaaa)\n"
22524 " , aaaaaaaa(aaaaaaaa)\n"
22525 " , aaaaaaaa(aaaaaaaa) {}",
22529 TEST_F(FormatTest
, ConstructorInitializersWithPreprocessorDirective
) {
22530 FormatStyle Style
= getLLVMStyle();
22531 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
22532 Style
.ConstructorInitializerIndentWidth
= 4;
22533 verifyFormat("SomeClass::Constructor()\n"
22537 verifyFormat("SomeClass::Constructor()\n"
22544 Style
.ConstructorInitializerIndentWidth
= 2;
22545 verifyFormat("SomeClass::Constructor()\n"
22552 Style
.ConstructorInitializerIndentWidth
= 0;
22553 verifyFormat("SomeClass::Constructor()\n"
22555 "#ifdef CONDITION\n"
22562 Style
.ConstructorInitializerIndentWidth
= 4;
22563 verifyFormat("SomeClass::Constructor()\n"
22580 verifyFormat("SomeClass::Constructor()\n"
22601 TEST_F(FormatTest
, Destructors
) {
22602 verifyFormat("void F(int &i) { i.~int(); }");
22603 verifyFormat("void F(int &i) { i->~int(); }");
22606 TEST_F(FormatTest
, FormatsWithWebKitStyle
) {
22607 FormatStyle Style
= getWebKitStyle();
22609 // Don't indent in outer namespaces.
22610 verifyFormat("namespace outer {\n"
22612 "namespace inner {\n"
22614 "} // namespace inner\n"
22615 "} // namespace outer\n"
22616 "namespace other_outer {\n"
22621 // Don't indent case labels.
22622 verifyFormat("switch (variable) {\n"
22625 " doSomething();\n"
22632 // Wrap before binary operators.
22636 " if (aaaaaaaaaaaaaaaa\n"
22637 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
22638 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22642 "if (aaaaaaaaaaaaaaaa\n"
22643 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
22644 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
22649 // Allow functions on a single line.
22650 verifyFormat("void f() { return; }", Style
);
22652 // Allow empty blocks on a single line and insert a space in empty blocks.
22653 verifyFormat("void f() { }", "void f() {}", Style
);
22654 verifyFormat("while (true) { }", "while (true) {}", Style
);
22655 // However, don't merge non-empty short loops.
22656 verifyFormat("while (true) {\n"
22659 "while (true) { continue; }", Style
);
22661 // Constructor initializers are formatted one per line with the "," on the
22663 verifyFormat("Constructor()\n"
22664 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
22665 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
22666 " aaaaaaaaaaaaaa)\n"
22667 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
22671 verifyFormat("SomeClass::Constructor()\n"
22676 verifyFormat("SomeClass::Constructor()\n"
22680 "SomeClass::Constructor():a(a){}", Style
);
22681 verifyFormat("SomeClass::Constructor()\n"
22688 verifyFormat("SomeClass::Constructor()\n"
22696 // Access specifiers should be aligned left.
22697 verifyFormat("class C {\n"
22703 // Do not align comments.
22704 verifyFormat("int a; // Do not\n"
22705 "double b; // align comments.",
22708 // Do not align operands.
22709 verifyFormat("ASSERT(aaaa\n"
22711 "ASSERT ( aaaa\n||bbbb);", Style
);
22713 // Accept input's line breaks.
22714 verifyFormat("if (aaaaaaaaaaaaaaa\n"
22715 " || bbbbbbbbbbbbbbb) {\n"
22718 "if (aaaaaaaaaaaaaaa\n"
22719 "|| bbbbbbbbbbbbbbb) { i++; }",
22721 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
22724 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style
);
22726 // Don't automatically break all macro definitions (llvm.org/PR17842).
22727 verifyFormat("#define aNumber 10", Style
);
22728 // However, generally keep the line breaks that the user authored.
22729 verifyFormat("#define aNumber \\\n"
22731 "#define aNumber \\\n"
22735 // Keep empty and one-element array literals on a single line.
22736 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
22737 " copyItems:YES];",
22738 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
22741 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
22742 " copyItems:YES];",
22743 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
22744 " copyItems:YES];",
22746 // FIXME: This does not seem right, there should be more indentation before
22747 // the array literal's entries. Nested blocks have the same problem.
22748 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22752 " copyItems:YES];",
22753 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
22757 " copyItems:YES];",
22760 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22761 " copyItems:YES];",
22762 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
22763 " copyItems:YES];",
22766 verifyFormat("[self.a b:c c:d];", Style
);
22767 verifyFormat("[self.a b:c\n"
22774 TEST_F(FormatTest
, FormatsLambdas
) {
22775 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
22777 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
22778 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
22779 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
22780 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
22781 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
22782 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
22783 verifyFormat("auto c = [a = [b = 42] {}] {};");
22784 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
22785 verifyFormat("int x = f(*+[] {});");
22786 verifyFormat("void f() {\n"
22787 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
22789 verifyFormat("void f() {\n"
22790 " other(x.begin(), //\n"
22792 " [&](int, int) { return 1; });\n"
22794 verifyFormat("void f() {\n"
22795 " other.other.other.other.other(\n"
22796 " x.begin(), x.end(),\n"
22797 " [something, rather](int, int, int, int, int, int, int) { "
22802 " other.other.other.other.other(\n"
22803 " x.begin(), x.end(),\n"
22804 " [something, rather](int, int, int, int, int, int, int) {\n"
22808 verifyFormat("SomeFunction([]() { // A cool function...\n"
22811 verifyFormat("SomeFunction([]() {\n"
22815 "SomeFunction([](){\n"
22819 verifyFormat("void f() {\n"
22820 " SomeFunction([](decltype(x), A *a) {});\n"
22821 " SomeFunction([](typeof(x), A *a) {});\n"
22822 " SomeFunction([](_Atomic(x), A *a) {});\n"
22823 " SomeFunction([](__underlying_type(x), A *a) {});\n"
22825 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22826 " [](const aaaaaaaaaa &a) { return a; });");
22827 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
22828 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
22830 verifyFormat("Constructor()\n"
22831 " : Field([] { // comment\n"
22834 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
22835 " return some_parameter.size();\n"
22837 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
22838 " [](const string &s) { return s; };");
22839 verifyFormat("int i = aaaaaa ? 1 //\n"
22843 verifyFormat("llvm::errs() << \"number of twos is \"\n"
22844 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
22845 " return x == 2; // force break\n"
22847 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22848 " [=](int iiiiiiiiiiii) {\n"
22849 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
22850 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
22852 getLLVMStyleWithColumns(60));
22854 verifyFormat("SomeFunction({[&] {\n"
22860 verifyFormat("SomeFunction({[&] {\n"
22864 "virtual aaaaaaaaaaaaaaaa(\n"
22865 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
22866 " aaaaa aaaaaaaaa);");
22868 // Lambdas with return types.
22869 verifyFormat("int c = []() -> int { return 2; }();");
22870 verifyFormat("int c = []() -> int * { return 2; }();");
22871 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
22872 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
22873 verifyFormat("foo([]() noexcept -> int {});");
22874 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
22875 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
22876 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
22877 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
22878 verifyFormat("[a, a]() -> a<1> {};");
22879 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
22880 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
22881 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
22882 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
22883 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
22884 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
22885 verifyFormat("[]() -> foo<!5> { return {}; };");
22886 verifyFormat("[]() -> foo<~5> { return {}; };");
22887 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
22888 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
22889 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
22890 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
22891 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
22892 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
22893 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
22894 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
22895 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
22896 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
22897 verifyFormat("namespace bar {\n"
22899 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
22900 "} // namespace bar");
22901 verifyFormat("namespace bar {\n"
22903 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
22904 "} // namespace bar");
22905 verifyFormat("namespace bar {\n"
22907 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
22908 "} // namespace bar");
22909 verifyFormat("namespace bar {\n"
22911 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
22912 "} // namespace bar");
22913 verifyFormat("namespace bar {\n"
22915 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
22916 "} // namespace bar");
22917 verifyFormat("namespace bar {\n"
22919 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22920 "} // namespace bar");
22921 verifyFormat("namespace bar {\n"
22923 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22924 "} // namespace bar");
22925 verifyFormat("namespace bar {\n"
22927 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22928 "} // namespace bar");
22929 verifyFormat("namespace bar {\n"
22931 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22932 "} // namespace bar");
22933 verifyFormat("namespace bar {\n"
22935 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22936 "} // namespace bar");
22937 verifyFormat("namespace bar {\n"
22939 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22940 "} // namespace bar");
22941 verifyFormat("namespace bar {\n"
22943 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22944 "} // namespace bar");
22945 verifyFormat("namespace bar {\n"
22947 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22948 "} // namespace bar");
22949 verifyFormat("namespace bar {\n"
22951 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22952 "} // namespace bar");
22953 verifyFormat("namespace bar {\n"
22955 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22956 "} // namespace bar");
22957 verifyFormat("namespace bar {\n"
22959 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22960 "} // namespace bar");
22961 verifyFormat("namespace bar {\n"
22963 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22964 "} // namespace bar");
22965 verifyFormat("namespace bar {\n"
22967 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22968 "} // namespace bar");
22969 verifyFormat("[]() -> a<1> {};");
22970 verifyFormat("[]() -> a<1> { ; };");
22971 verifyFormat("[]() -> a<1> { ; }();");
22972 verifyFormat("[a, a]() -> a<true> {};");
22973 verifyFormat("[]() -> a<true> {};");
22974 verifyFormat("[]() -> a<true> { ; };");
22975 verifyFormat("[]() -> a<true> { ; }();");
22976 verifyFormat("[a, a]() -> a<false> {};");
22977 verifyFormat("[]() -> a<false> {};");
22978 verifyFormat("[]() -> a<false> { ; };");
22979 verifyFormat("[]() -> a<false> { ; }();");
22980 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22981 verifyFormat("namespace bar {\n"
22982 "auto foo{[]() -> foo<false> { ; }};\n"
22983 "} // namespace bar");
22984 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22985 " int j) -> int {\n"
22986 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22989 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22990 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22991 " return aaaaaaaaaaaaaaaaa;\n"
22993 getLLVMStyleWithColumns(70));
22994 verifyFormat("[]() //\n"
22998 verifyFormat("[]() -> Void<T...> {};");
22999 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
23000 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
23001 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
23002 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
23003 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
23004 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
23005 verifyFormat("return int{[x = x]() { return x; }()};");
23007 // Lambdas with explicit template argument lists.
23009 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
23010 verifyFormat("auto L = []<class T>(T) {\n"
23016 verifyFormat("auto L = []<class... T>(T...) {\n"
23022 verifyFormat("auto L = []<typename... T>(T...) {\n"
23028 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
23034 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
23040 verifyFormat("auto L = []<int... T>(T...) {\n"
23046 verifyFormat("auto L = []<Foo... T>(T...) {\n"
23053 // Lambdas that fit on a single line within an argument list are not forced
23055 verifyFormat("SomeFunction([] {});");
23056 verifyFormat("SomeFunction(0, [] {});");
23057 verifyFormat("SomeFunction([] {}, 0);");
23058 verifyFormat("SomeFunction(0, [] {}, 0);");
23059 verifyFormat("SomeFunction([] { return 0; }, 0);");
23060 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
23061 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
23062 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
23063 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
23064 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
23065 // Exceeded column limit. We need to break.
23066 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
23067 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
23068 "return 0; }, b);");
23070 // Multiple multi-line lambdas in the same parentheses change indentation
23071 // rules. These lambdas are always forced to start on new lines.
23072 verifyFormat("SomeFunction(\n"
23080 // A multi-line lambda passed as arg0 is always pushed to the next line.
23081 verifyFormat("SomeFunction(\n"
23087 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
23088 // the arg0 case above.
23089 auto Style
= getGoogleStyle();
23090 Style
.BinPackArguments
= false;
23091 verifyFormat("SomeFunction(\n"
23098 verifyFormat("SomeFunction(\n"
23105 // A lambda with a very long line forces arg0 to be pushed out irrespective of
23106 // the BinPackArguments value (as long as the code is wide enough).
23108 "something->SomeFunction(\n"
23112 "D0000000000000000000000000000000000000000000000000000000000001();\n"
23116 // A multi-line lambda is pulled up as long as the introducer fits on the
23117 // previous line and there are no further args.
23118 verifyFormat("function(1, [this, that] {\n"
23121 verifyFormat("function([this, that] {\n"
23124 // FIXME: this format is not ideal and we should consider forcing the first
23125 // arg onto its own line.
23126 verifyFormat("function(a, b, c, //\n"
23127 " d, [this, that] {\n"
23131 // Multiple lambdas are treated correctly even when there is a short arg0.
23132 verifyFormat("SomeFunction(\n"
23142 // More complex introducers.
23143 verifyFormat("return [i, args...] {};");
23146 verifyFormat("constexpr char hello[]{\"hello\"};");
23147 verifyFormat("double &operator[](int i) { return 0; }\n"
23149 verifyFormat("std::unique_ptr<int[]> foo() {}");
23150 verifyFormat("int i = a[a][a]->f();");
23151 verifyFormat("int i = (*b)[a]->f();");
23153 // Other corner cases.
23154 verifyFormat("void f() {\n"
23155 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
23158 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
23160 // Lambdas created through weird macros.
23161 verifyFormat("void f() {\n"
23162 " MACRO((const AA &a) { return 1; });\n"
23163 " MACRO((AA &a) { return 1; });\n"
23166 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
23171 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
23176 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
23181 verifyFormat("auto lambda = []() {\n"
23189 // Lambdas with complex multiline introducers.
23191 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
23192 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
23193 " -> ::std::unordered_set<\n"
23194 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
23198 FormatStyle LLVMStyle
= getLLVMStyleWithColumns(60);
23199 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23200 " [](auto n) noexcept [[back_attr]]\n"
23201 " -> std::unordered_map<very_long_type_name_A,\n"
23202 " very_long_type_name_B> {\n"
23203 " really_do_something();\n"
23206 verifyFormat("very_long_function_name_yes_it_is_really_long(\n"
23207 " [](auto n) constexpr\n"
23208 " -> std::unordered_map<very_long_type_name_A,\n"
23209 " very_long_type_name_B> {\n"
23210 " really_do_something();\n"
23214 FormatStyle DoNotMerge
= getLLVMStyle();
23215 DoNotMerge
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
23216 verifyFormat("auto c = []() {\n"
23219 "auto c = []() { return b; };", DoNotMerge
);
23220 verifyFormat("auto c = []() {\n"
23222 " auto c = []() {};", DoNotMerge
);
23224 FormatStyle MergeEmptyOnly
= getLLVMStyle();
23225 MergeEmptyOnly
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_Empty
;
23226 verifyFormat("auto c = []() {\n"
23229 "auto c = []() {\n"
23233 verifyFormat("auto c = []() {};",
23234 "auto c = []() {\n"
23238 FormatStyle MergeInline
= getLLVMStyle();
23239 MergeInline
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_Inline
;
23240 verifyFormat("auto c = []() {\n"
23243 "auto c = []() { return b; };", MergeInline
);
23244 verifyFormat("function([]() { return b; })", MergeInline
);
23245 verifyFormat("function([]() { return b; }, a)", MergeInline
);
23246 verifyFormat("function(a, []() { return b; })", MergeInline
);
23248 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
23249 // AllowShortLambdasOnASingleLine
23250 FormatStyle LLVMWithBeforeLambdaBody
= getLLVMStyle();
23251 LLVMWithBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
23252 LLVMWithBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
23253 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23254 FormatStyle::ShortLambdaStyle::SLS_None
;
23255 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
23260 LLVMWithBeforeLambdaBody
);
23261 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
23265 LLVMWithBeforeLambdaBody
);
23266 verifyFormat("auto fct_SLS_None = []()\n"
23270 LLVMWithBeforeLambdaBody
);
23271 verifyFormat("TwoNestedLambdas_SLS_None(\n"
23280 LLVMWithBeforeLambdaBody
);
23281 verifyFormat("void Fct() {\n"
23287 LLVMWithBeforeLambdaBody
);
23289 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23290 FormatStyle::ShortLambdaStyle::SLS_Empty
;
23291 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
23296 LLVMWithBeforeLambdaBody
);
23297 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
23298 LLVMWithBeforeLambdaBody
);
23299 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
23300 "ongFunctionName_SLS_Empty(\n"
23302 LLVMWithBeforeLambdaBody
);
23303 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
23308 LLVMWithBeforeLambdaBody
);
23309 verifyFormat("auto fct_SLS_Empty = []()\n"
23313 LLVMWithBeforeLambdaBody
);
23314 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
23317 " return Call([]() {});\n"
23319 LLVMWithBeforeLambdaBody
);
23320 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
23323 " return Call([]() {});\n"
23325 LLVMWithBeforeLambdaBody
);
23327 "FctWithLongLineInLambda_SLS_Empty(\n"
23330 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23331 " AndShouldNotBeConsiderAsInline,\n"
23332 " LambdaBodyMustBeBreak);\n"
23334 LLVMWithBeforeLambdaBody
);
23336 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23337 FormatStyle::ShortLambdaStyle::SLS_Inline
;
23338 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
23339 LLVMWithBeforeLambdaBody
);
23340 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
23341 LLVMWithBeforeLambdaBody
);
23342 verifyFormat("auto fct_SLS_Inline = []()\n"
23346 LLVMWithBeforeLambdaBody
);
23347 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
23349 LLVMWithBeforeLambdaBody
);
23351 "FctWithLongLineInLambda_SLS_Inline(\n"
23354 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23355 " AndShouldNotBeConsiderAsInline,\n"
23356 " LambdaBodyMustBeBreak);\n"
23358 LLVMWithBeforeLambdaBody
);
23359 verifyFormat("FctWithMultipleParams_SLS_Inline("
23360 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23361 " []() { return 17; });",
23362 LLVMWithBeforeLambdaBody
);
23364 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
23365 LLVMWithBeforeLambdaBody
);
23367 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23368 FormatStyle::ShortLambdaStyle::SLS_All
;
23369 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
23370 LLVMWithBeforeLambdaBody
);
23371 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
23372 LLVMWithBeforeLambdaBody
);
23373 verifyFormat("auto fct_SLS_All = []() { return 17; };",
23374 LLVMWithBeforeLambdaBody
);
23375 verifyFormat("FctWithOneParam_SLS_All(\n"
23378 " // A cool function...\n"
23381 LLVMWithBeforeLambdaBody
);
23382 verifyFormat("FctWithMultipleParams_SLS_All("
23383 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
23384 " []() { return 17; });",
23385 LLVMWithBeforeLambdaBody
);
23386 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
23387 LLVMWithBeforeLambdaBody
);
23388 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
23389 LLVMWithBeforeLambdaBody
);
23391 "FctWithLongLineInLambda_SLS_All(\n"
23394 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23395 " AndShouldNotBeConsiderAsInline,\n"
23396 " LambdaBodyMustBeBreak);\n"
23398 LLVMWithBeforeLambdaBody
);
23400 "auto fct_SLS_All = []()\n"
23402 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23403 " AndShouldNotBeConsiderAsInline,\n"
23404 " LambdaBodyMustBeBreak);\n"
23406 LLVMWithBeforeLambdaBody
);
23407 LLVMWithBeforeLambdaBody
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
23408 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
23409 LLVMWithBeforeLambdaBody
);
23411 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
23416 LLVMWithBeforeLambdaBody
);
23417 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23419 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
23424 LLVMWithBeforeLambdaBody
);
23426 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
23430 " []() { return SomeValueNotSoLong; });",
23431 LLVMWithBeforeLambdaBody
);
23432 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
23436 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
23437 "eConsiderAsInline;\n"
23439 LLVMWithBeforeLambdaBody
);
23441 "FctWithLongLineInLambda_SLS_All(\n"
23444 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
23445 " AndShouldNotBeConsiderAsInline,\n"
23446 " LambdaBodyMustBeBreak);\n"
23448 LLVMWithBeforeLambdaBody
);
23449 verifyFormat("FctWithTwoParams_SLS_All(\n"
23452 " // A cool function...\n"
23456 LLVMWithBeforeLambdaBody
);
23457 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
23458 LLVMWithBeforeLambdaBody
);
23460 "FctWithTwoParams_SLS_All(\n"
23461 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
23462 LLVMWithBeforeLambdaBody
);
23464 "FctWithTwoParams_SLS_All(\n"
23469 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
23471 LLVMWithBeforeLambdaBody
);
23472 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
23473 LLVMWithBeforeLambdaBody
);
23475 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
23476 LLVMWithBeforeLambdaBody
);
23477 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
23479 LLVMWithBeforeLambdaBody
);
23480 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23483 " // A cool function...\n"
23484 " return Call([]() { return 17; });\n"
23486 LLVMWithBeforeLambdaBody
);
23487 verifyFormat("TwoNestedLambdas_SLS_All(\n"
23493 " // A cool function...\n"
23497 LLVMWithBeforeLambdaBody
);
23499 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23500 FormatStyle::ShortLambdaStyle::SLS_None
;
23502 verifyFormat("auto select = [this]() -> const Library::Object *\n"
23504 " return MyAssignment::SelectFromList(this);\n"
23506 LLVMWithBeforeLambdaBody
);
23508 verifyFormat("auto select = [this]() -> const Library::Object &\n"
23510 " return MyAssignment::SelectFromList(this);\n"
23512 LLVMWithBeforeLambdaBody
);
23514 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
23516 " return MyAssignment::SelectFromList(this);\n"
23518 LLVMWithBeforeLambdaBody
);
23520 verifyFormat("namespace test {\n"
23523 " Test() = default;\n"
23525 "} // namespace test",
23526 LLVMWithBeforeLambdaBody
);
23528 // Lambdas with different indentation styles.
23529 Style
= getLLVMStyleWithColumns(60);
23530 verifyFormat("Result doSomething(Promise promise) {\n"
23531 " return promise.then(\n"
23532 " [this, obj = std::move(s)](int bar) mutable {\n"
23533 " return someObject.startAsyncAction().then(\n"
23534 " [this, &obj](Result result) mutable {\n"
23535 " result.processMore();\n"
23540 Style
.LambdaBodyIndentation
= FormatStyle::LBI_OuterScope
;
23541 verifyFormat("Result doSomething(Promise promise) {\n"
23542 " return promise.then(\n"
23543 " [this, obj = std::move(s)](int bar) mutable {\n"
23544 " return obj.startAsyncAction().then(\n"
23545 " [this, &obj](Result result) mutable {\n"
23546 " result.processMore();\n"
23551 verifyFormat("Result doSomething(Promise promise) {\n"
23552 " return promise.then([this, obj = std::move(s)] {\n"
23553 " return obj.startAsyncAction().then(\n"
23554 " [this, &obj](Result result) mutable {\n"
23555 " result.processMore();\n"
23560 verifyFormat("void test() {\n"
23561 " ([]() -> auto {\n"
23567 verifyFormat("void test() {\n"
23568 " []() -> auto {\n"
23574 verifyFormat("void test() {\n"
23575 " std::sort(v.begin(), v.end(),\n"
23576 " [](const auto &foo, const auto &bar) {\n"
23577 " return foo.baz < bar.baz;\n"
23581 verifyFormat("void test() {\n"
23583 " []() -> auto {\n"
23590 verifyFormat("void test() {\n"
23591 " ([]() -> auto {\n"
23599 verifyFormat("#define A \\\n"
23601 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
23602 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
23605 verifyFormat("#define SORT(v) \\\n"
23606 " std::sort(v.begin(), v.end(), \\\n"
23607 " [](const auto &foo, const auto &bar) { \\\n"
23608 " return foo.baz < bar.baz; \\\n"
23611 verifyFormat("void foo() {\n"
23612 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
23613 " auto f = e(d);\n"
23618 verifyFormat("void foo() {\n"
23619 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
23620 " auto f = e(foo, [&] {\n"
23623 " }, qux, [&] -> Bar {\n"
23631 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23632 " AnotherLongClassName baz)\n"
23633 " : baz{baz}, func{[&] {\n"
23634 " auto qux = bar;\n"
23635 " return aFunkyFunctionCall(qux);\n"
23638 verifyFormat("void foo() {\n"
23642 " : qux{[](int quux) {\n"
23643 " auto tmp = quux;\n"
23648 " std::function<void(int quux)> qux;\n"
23652 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
23653 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23654 " AnotherLongClassName baz) :\n"
23655 " baz{baz}, func{[&] {\n"
23656 " auto qux = bar;\n"
23657 " return aFunkyFunctionCall(qux);\n"
23660 Style
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
23661 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
23662 " AnotherLongClassName baz) :\n"
23665 " auto qux = bar;\n"
23666 " return aFunkyFunctionCall(qux);\n"
23669 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
23670 // FIXME: The following test should pass, but fails at the time of writing.
23672 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
23673 // doesn't force an initial line break, even if lambdas span multiple lines.
23674 verifyFormat("void foo() {\n"
23676 " [](d) -> Foo {\n"
23677 " auto f = e(d);\n"
23679 " }, foo, Bar{}, [] {\n"
23686 // A long non-lambda argument forces arguments to span multiple lines and thus
23687 // forces an initial line break when using AlwaysBreak.
23688 verifyFormat("void foo() {\n"
23691 " [](d) -> Foo {\n"
23692 " auto f = e(d);\n"
23694 " }, foo, Bar{},\n"
23699 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23702 Style
.BinPackArguments
= false;
23703 verifyFormat("void foo() {\n"
23706 " [](d) -> Foo {\n"
23707 " auto f = e(d);\n"
23717 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
23720 Style
.BinPackArguments
= true;
23721 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
23722 Style
.BraceWrapping
.BeforeLambdaBody
= true;
23723 verifyFormat("void foo() {\n"
23725 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
23732 " }, qux, [&] -> Bar\n"
23743 TEST_F(FormatTest
, LambdaWithLineComments
) {
23744 FormatStyle LLVMWithBeforeLambdaBody
= getLLVMStyle();
23745 LLVMWithBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
23746 LLVMWithBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
23747 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
23748 FormatStyle::ShortLambdaStyle::SLS_All
;
23750 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody
);
23751 verifyFormat("auto k = []() // comment\n"
23753 LLVMWithBeforeLambdaBody
);
23754 verifyFormat("auto k = []() /* comment */ { return; }",
23755 LLVMWithBeforeLambdaBody
);
23756 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
23757 LLVMWithBeforeLambdaBody
);
23758 verifyFormat("auto k = []() // X\n"
23760 LLVMWithBeforeLambdaBody
);
23762 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
23764 LLVMWithBeforeLambdaBody
);
23766 LLVMWithBeforeLambdaBody
.ColumnLimit
= 0;
23768 verifyFormat("foo([]()\n"
23771 " return 1; // comment\n"
23775 " return 1; // comment\n"
23777 LLVMWithBeforeLambdaBody
);
23778 verifyFormat("foo(\n"
23781 " bar(); // comment\n"
23785 " 1, MACRO { baz(); bar(); // comment\n"
23788 LLVMWithBeforeLambdaBody
);
23791 TEST_F(FormatTest
, EmptyLinesInLambdas
) {
23792 verifyFormat("auto lambda = []() {\n"
23795 "auto lambda = []() {\n"
23802 TEST_F(FormatTest
, FormatsBlocks
) {
23803 FormatStyle ShortBlocks
= getLLVMStyle();
23804 ShortBlocks
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
23805 verifyFormat("int (^Block)(int, int);", ShortBlocks
);
23806 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks
);
23807 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks
);
23808 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks
);
23809 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks
);
23810 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks
);
23812 verifyFormat("foo(^{ bar(); });", ShortBlocks
);
23813 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks
);
23814 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks
);
23816 verifyFormat("[operation setCompletionBlock:^{\n"
23817 " [self onOperationDone];\n"
23819 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
23820 " [self onOperationDone];\n"
23822 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
23825 verifyFormat("int a = [operation block:^int(int *i) {\n"
23828 verifyFormat("[myObject doSomethingWith:arg1\n"
23829 " aaa:^int(int *a) {\n"
23832 " bbb:f(a * bbbbbbbb)];");
23834 verifyFormat("[operation setCompletionBlock:^{\n"
23835 " [self.delegate newDataAvailable];\n"
23837 getLLVMStyleWithColumns(60));
23838 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
23839 " NSString *path = [self sessionFilePath];\n"
23844 verifyFormat("[[SessionService sharedService]\n"
23845 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23847 " [self windowDidLoad:window];\n"
23849 " [self errorLoadingWindow];\n"
23852 verifyFormat("void (^largeBlock)(void) = ^{\n"
23855 getLLVMStyleWithColumns(40));
23856 verifyFormat("[[SessionService sharedService]\n"
23857 " loadWindowWithCompletionBlock: //\n"
23858 " ^(SessionWindow *window) {\n"
23860 " [self windowDidLoad:window];\n"
23862 " [self errorLoadingWindow];\n"
23865 getLLVMStyleWithColumns(60));
23866 verifyFormat("[myObject doSomethingWith:arg1\n"
23867 " firstBlock:^(Foo *a) {\n"
23871 " secondBlock:^(Bar *b) {\n"
23875 " thirdBlock:^Foo(Bar *b) {\n"
23879 verifyFormat("[myObject doSomethingWith:arg1\n"
23881 " secondBlock:^(Bar *b) {\n"
23886 verifyFormat("f(^{\n"
23887 " @autoreleasepool {\n"
23893 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
23895 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
23898 FormatStyle FourIndent
= getLLVMStyle();
23899 FourIndent
.ObjCBlockIndentWidth
= 4;
23900 verifyFormat("[operation setCompletionBlock:^{\n"
23901 " [self onOperationDone];\n"
23906 TEST_F(FormatTest
, FormatsBlocksWithZeroColumnWidth
) {
23907 FormatStyle ZeroColumn
= getLLVMStyleWithColumns(0);
23909 verifyFormat("[[SessionService sharedService] "
23910 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23912 " [self windowDidLoad:window];\n"
23914 " [self errorLoadingWindow];\n"
23918 verifyFormat("[[SessionService sharedService]\n"
23919 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23921 " [self windowDidLoad:window];\n"
23923 " [self errorLoadingWindow];\n"
23926 "[[SessionService sharedService]\n"
23927 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
23929 " [self windowDidLoad:window];\n"
23931 " [self errorLoadingWindow];\n"
23935 verifyFormat("[myObject doSomethingWith:arg1\n"
23936 " firstBlock:^(Foo *a) {\n"
23940 " secondBlock:^(Bar *b) {\n"
23944 " thirdBlock:^Foo(Bar *b) {\n"
23949 verifyFormat("f(^{\n"
23950 " @autoreleasepool {\n"
23957 verifyFormat("void (^largeBlock)(void) = ^{\n"
23962 ZeroColumn
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
23963 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23964 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn
);
23965 ZeroColumn
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
23966 verifyFormat("void (^largeBlock)(void) = ^{\n"
23969 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn
);
23972 TEST_F(FormatTest
, SupportsCRLF
) {
23973 verifyFormat("int a;\r\n"
23979 verifyFormat("int a;\r\n"
23985 verifyFormat("int a;\n"
23991 // FIXME: unstable test case
23992 EXPECT_EQ("\"aaaaaaa \"\r\n"
23993 "\"bbbbbbb\";\r\n",
23994 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23995 verifyFormat("#define A \\\r\n"
24004 verifyNoChange("/*\r\n"
24005 "multi line block comments\r\n"
24006 "should not introduce\r\n"
24007 "an extra carriage return\r\n"
24009 verifyFormat("/*\r\n"
24016 FormatStyle style
= getLLVMStyle();
24018 EXPECT_EQ(style
.LineEnding
, FormatStyle::LE_DeriveLF
);
24019 verifyFormat("union FooBarBazQux {\n"
24024 "union FooBarBazQux {\r\n"
24030 style
.LineEnding
= FormatStyle::LE_DeriveCRLF
;
24031 verifyFormat("union FooBarBazQux {\r\n"
24036 "union FooBarBazQux {\r\n"
24043 style
.LineEnding
= FormatStyle::LE_LF
;
24044 verifyFormat("union FooBarBazQux {\n"
24050 "union FooBarBazQux {\r\n"
24057 style
.LineEnding
= FormatStyle::LE_CRLF
;
24058 verifyFormat("union FooBarBazQux {\r\n"
24064 "union FooBarBazQux {\r\n"
24072 style
.LineEnding
= FormatStyle::LE_DeriveLF
;
24073 verifyFormat("union FooBarBazQux {\r\n"
24079 "union FooBarBazQux {\r\n"
24086 style
.LineEnding
= FormatStyle::LE_DeriveCRLF
;
24087 verifyFormat("union FooBarBazQux {\n"
24093 "union FooBarBazQux {\r\n"
24102 TEST_F(FormatTest
, MunchSemicolonAfterBlocks
) {
24103 verifyFormat("MY_CLASS(C) {\n"
24109 TEST_F(FormatTest
, ConfigurableContinuationIndentWidth
) {
24110 FormatStyle TwoIndent
= getLLVMStyleWithColumns(15);
24111 TwoIndent
.ContinuationIndentWidth
= 2;
24113 verifyFormat("int i =\n"
24116 "int i = longFunction(arg);", TwoIndent
);
24118 FormatStyle SixIndent
= getLLVMStyleWithColumns(20);
24119 SixIndent
.ContinuationIndentWidth
= 6;
24121 verifyFormat("int i =\n"
24124 "int i = longFunction(arg);", SixIndent
);
24127 TEST_F(FormatTest
, WrappedClosingParenthesisIndent
) {
24128 FormatStyle Style
= getLLVMStyle();
24129 verifyFormat("int Foo::getter(\n"
24135 verifyFormat("void Foo::setter(\n"
24143 TEST_F(FormatTest
, SpacesInAngles
) {
24144 FormatStyle Spaces
= getLLVMStyle();
24145 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24147 verifyFormat("vector< ::std::string > x1;", Spaces
);
24148 verifyFormat("Foo< int, Bar > x2;", Spaces
);
24149 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces
);
24151 verifyFormat("static_cast< int >(arg);", Spaces
);
24152 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces
);
24153 verifyFormat("f< int, float >();", Spaces
);
24154 verifyFormat("template <> g() {}", Spaces
);
24155 verifyFormat("template < std::vector< int > > f() {}", Spaces
);
24156 verifyFormat("std::function< void(int, int) > fct;", Spaces
);
24157 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
24160 Spaces
.Standard
= FormatStyle::LS_Cpp03
;
24161 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24162 verifyFormat("A< A< int > >();", Spaces
);
24164 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Never
;
24165 verifyFormat("A<A<int> >();", Spaces
);
24167 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Leave
;
24168 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
24170 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
24173 verifyFormat("A<A<int> >();", Spaces
);
24174 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces
);
24175 verifyFormat("A< A< int > >();", Spaces
);
24177 Spaces
.Standard
= FormatStyle::LS_Cpp11
;
24178 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24179 verifyFormat("A< A< int > >();", Spaces
);
24181 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Never
;
24182 verifyFormat("vector<::std::string> x4;", Spaces
);
24183 verifyFormat("vector<int> x5;", Spaces
);
24184 verifyFormat("Foo<int, Bar> x6;", Spaces
);
24185 verifyFormat("Foo<::int, ::Bar> x7;", Spaces
);
24187 verifyFormat("A<A<int>>();", Spaces
);
24189 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Leave
;
24190 verifyFormat("vector<::std::string> x4;", Spaces
);
24191 verifyFormat("vector< ::std::string > x4;", Spaces
);
24192 verifyFormat("vector<int> x5;", Spaces
);
24193 verifyFormat("vector< int > x5;", Spaces
);
24194 verifyFormat("Foo<int, Bar> x6;", Spaces
);
24195 verifyFormat("Foo< int, Bar > x6;", Spaces
);
24196 verifyFormat("Foo<::int, ::Bar> x7;", Spaces
);
24197 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces
);
24199 verifyFormat("A<A<int>>();", Spaces
);
24200 verifyFormat("A< A< int > >();", Spaces
);
24201 verifyFormat("A<A<int > >();", Spaces
);
24202 verifyFormat("A< A< int>>();", Spaces
);
24204 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
24205 verifyFormat("// clang-format off\n"
24206 "foo<<<1, 1>>>();\n"
24207 "// clang-format on",
24209 verifyFormat("// clang-format off\n"
24210 "foo< < <1, 1> > >();\n"
24211 "// clang-format on",
24215 TEST_F(FormatTest
, SpaceAfterTemplateKeyword
) {
24216 FormatStyle Style
= getLLVMStyle();
24217 Style
.SpaceAfterTemplateKeyword
= false;
24218 verifyFormat("template<int> void foo();", Style
);
24221 TEST_F(FormatTest
, TripleAngleBrackets
) {
24222 verifyFormat("f<<<1, 1>>>();");
24223 verifyFormat("f<<<1, 1, 1, s>>>();");
24224 verifyFormat("f<<<a, b, c, d>>>();");
24225 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
24226 verifyFormat("f<param><<<1, 1>>>();");
24227 verifyFormat("f<1><<<1, 1>>>();");
24228 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
24229 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24230 "aaaaaaaaaaa<<<\n 1, 1>>>();");
24231 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
24232 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
24235 TEST_F(FormatTest
, MergeLessLessAtEnd
) {
24236 verifyFormat("<<");
24237 verifyFormat("< < <", "\\\n<<<");
24238 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24239 "aaallvm::outs() <<");
24240 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
24241 "aaaallvm::outs()\n <<");
24244 TEST_F(FormatTest
, HandleUnbalancedImplicitBracesAcrossPPBranches
) {
24245 std::string code
= "#if A\n"
24255 verifyFormat(code
);
24258 TEST_F(FormatTest
, HandleConflictMarkers
) {
24259 // Git/SVN conflict markers.
24260 verifyFormat("int a;\n"
24262 " callme(some(parameter1,\n"
24263 "<<<<<<< text by the vcs\n"
24265 "||||||| text by the vcs\n"
24268 "======= text by the vcs\n"
24269 " parameter2, parameter3),\n"
24270 ">>>>>>> text by the vcs\n"
24271 " otherparameter);",
24274 " callme(some(parameter1,\n"
24275 "<<<<<<< text by the vcs\n"
24277 "||||||| text by the vcs\n"
24280 "======= text by the vcs\n"
24283 ">>>>>>> text by the vcs\n"
24284 " otherparameter);");
24286 // Perforce markers.
24287 verifyFormat("void f() {\n"
24289 ">>>> text by the vcs\n"
24291 "==== text by the vcs\n"
24293 "==== text by the vcs\n"
24295 "<<<< text by the vcs\n"
24299 ">>>> text by the vcs\n"
24301 "==== text by the vcs\n"
24303 "==== text by the vcs\n"
24305 "<<<< text by the vcs\n"
24308 verifyNoChange("<<<<<<<\n"
24313 verifyNoChange("<<<<<<<\n"
24319 // FIXME: Handle parsing of macros around conflict markers correctly:
24320 verifyFormat("#define Macro \\\n"
24329 "#define Macro \\\n"
24340 verifyFormat(R
"(====
24349 TEST_F(FormatTest
, DisableRegions
) {
24350 verifyFormat("int i;\n"
24351 "// clang-format off\n"
24353 "// clang-format on\n"
24356 " // clang-format off\n"
24358 " // clang-format on\n"
24360 verifyFormat("int i;\n"
24361 "/* clang-format off */\n"
24363 "/* clang-format on */\n"
24366 " /* clang-format off */\n"
24368 " /* clang-format on */\n"
24371 // Don't reflow comments within disabled regions.
24372 verifyFormat("// clang-format off\n"
24373 "// long long long long long long line\n"
24374 "/* clang-format on */\n"
24375 "/* long long long\n"
24376 " * long long long\n"
24379 "/* clang-format off */\n"
24380 "/* long long long long long long line */",
24381 "// clang-format off\n"
24382 "// long long long long long long line\n"
24383 "/* clang-format on */\n"
24384 "/* long long long long long long line */\n"
24386 "/* clang-format off */\n"
24387 "/* long long long long long long line */",
24388 getLLVMStyleWithColumns(20));
24390 verifyFormat("int *i;\n"
24391 "// clang-format off:\n"
24393 "// clang-format on: 1\n"
24396 "// clang-format off:\n"
24398 "// clang-format on: 1\n"
24401 verifyFormat("int *i;\n"
24402 "// clang-format off:0\n"
24404 "// clang-format only\n"
24407 "// clang-format off:0\n"
24409 "// clang-format only\n"
24412 verifyNoChange("// clang-format off\n"
24414 " #if SHOULD_STAY_INDENTED\n"
24417 "// clang-format on");
24420 TEST_F(FormatTest
, DoNotCrashOnInvalidInput
) {
24422 verifyNoCrash("#define a\\\n /**/}");
24423 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n"
24424 "LY52: ! [internal]");
24427 TEST_F(FormatTest
, FormatsTableGenCode
) {
24428 FormatStyle Style
= getLLVMStyle();
24429 Style
.Language
= FormatStyle::LK_TableGen
;
24430 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style
);
24433 TEST_F(FormatTest
, ArrayOfTemplates
) {
24434 verifyFormat("auto a = new unique_ptr<int>[10];",
24435 "auto a = new unique_ptr<int > [ 10];");
24437 FormatStyle Spaces
= getLLVMStyle();
24438 Spaces
.SpacesInSquareBrackets
= true;
24439 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
24440 "auto a = new unique_ptr<int > [10];", Spaces
);
24443 TEST_F(FormatTest
, ArrayAsTemplateType
) {
24444 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
24445 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
24447 FormatStyle Spaces
= getLLVMStyle();
24448 Spaces
.SpacesInSquareBrackets
= true;
24449 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
24450 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces
);
24453 TEST_F(FormatTest
, NoSpaceAfterSuper
) { verifyFormat("__super::FooBar();"); }
24455 TEST_F(FormatTest
, FormatSortsUsingDeclarations
) {
24456 verifyFormat("using std::cin;\n"
24457 "using std::cout;",
24458 "using std::cout;\n"
24463 TEST_F(FormatTest
, UTF8CharacterLiteralCpp03
) {
24464 FormatStyle Style
= getLLVMStyle();
24465 Style
.Standard
= FormatStyle::LS_Cpp03
;
24466 // cpp03 recognize this string as identifier u8 and literal character 'a'
24467 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style
);
24470 TEST_F(FormatTest
, UTF8CharacterLiteralCpp11
) {
24471 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
24472 // all modes, including C++11, C++14 and C++17
24473 verifyFormat("auto c = u8'a';");
24476 TEST_F(FormatTest
, DoNotFormatLikelyXml
) {
24477 verifyGoogleFormat("<!-- ;> -->");
24478 verifyNoChange(" <!-- >; -->", getGoogleStyle());
24481 TEST_F(FormatTest
, StructuredBindings
) {
24482 // Structured bindings is a C++17 feature.
24483 // all modes, including C++11, C++14 and C++17
24484 verifyFormat("auto [a, b] = f();");
24485 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
24486 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
24487 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
24488 verifyFormat("auto const volatile [a, b] = f();",
24489 "auto const volatile[a, b] = f();");
24490 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
24491 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
24492 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
24493 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
24494 verifyFormat("auto const volatile &&[a, b] = f();",
24495 "auto const volatile &&[a, b] = f();");
24496 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
24497 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
24498 verifyFormat("const auto volatile &&[a, b] = f();",
24499 "const auto volatile &&[a, b] = f();");
24500 verifyFormat("volatile const auto &&[a, b] = f();",
24501 "volatile const auto &&[a, b] = f();");
24502 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
24504 // Make sure we don't mistake structured bindings for lambdas.
24505 FormatStyle PointerMiddle
= getLLVMStyle();
24506 PointerMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
24507 verifyGoogleFormat("auto [a1, b]{A * i};");
24508 verifyFormat("auto [a2, b]{A * i};");
24509 verifyFormat("auto [a3, b]{A * i};", PointerMiddle
);
24510 verifyGoogleFormat("auto const [a1, b]{A * i};");
24511 verifyFormat("auto const [a2, b]{A * i};");
24512 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle
);
24513 verifyGoogleFormat("auto const& [a1, b]{A * i};");
24514 verifyFormat("auto const &[a2, b]{A * i};");
24515 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle
);
24516 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
24517 verifyFormat("auto const &&[a2, b]{A * i};");
24518 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle
);
24520 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
24521 "for (const auto && [a, b] : some_range) {\n}");
24522 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
24523 "for (const auto & [a, b] : some_range) {\n}");
24524 verifyFormat("for (const auto [a, b] : some_range) {\n}",
24525 "for (const auto[a, b] : some_range) {\n}");
24526 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
24527 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
24528 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
24529 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
24530 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
24531 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
24532 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
24533 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
24535 FormatStyle Spaces
= getLLVMStyle();
24536 Spaces
.SpacesInSquareBrackets
= true;
24537 verifyFormat("auto [ a, b ] = f();", Spaces
);
24538 verifyFormat("auto &&[ a, b ] = f();", Spaces
);
24539 verifyFormat("auto &[ a, b ] = f();", Spaces
);
24540 verifyFormat("auto const &&[ a, b ] = f();", Spaces
);
24541 verifyFormat("auto const &[ a, b ] = f();", Spaces
);
24544 TEST_F(FormatTest
, FileAndCode
) {
24545 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.cc", ""));
24546 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.m", ""));
24547 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.mm", ""));
24548 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", ""));
24549 EXPECT_EQ(FormatStyle::LK_ObjC
,
24550 guessLanguage("foo.h", "@interface Foo\n@end"));
24552 FormatStyle::LK_ObjC
,
24553 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
24554 EXPECT_EQ(FormatStyle::LK_ObjC
,
24555 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
24556 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.h", "@class Foo;"));
24557 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo", ""));
24558 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo", "@interface Foo\n@end"));
24559 EXPECT_EQ(FormatStyle::LK_ObjC
,
24560 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
24561 EXPECT_EQ(FormatStyle::LK_ObjC
,
24563 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
24565 FormatStyle::LK_Cpp
,
24566 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
24567 // Only one of the two preprocessor regions has ObjC-like code.
24568 EXPECT_EQ(FormatStyle::LK_ObjC
,
24569 guessLanguage("foo.h", "#if A\n"
24572 "#define B() [NSString a:@\"\"]\n"
24576 TEST_F(FormatTest
, GuessLanguageWithCpp11AttributeSpecifiers
) {
24577 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "[[noreturn]];"));
24578 EXPECT_EQ(FormatStyle::LK_ObjC
,
24579 guessLanguage("foo.h", "array[[calculator getIndex]];"));
24580 EXPECT_EQ(FormatStyle::LK_Cpp
,
24581 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
24583 FormatStyle::LK_Cpp
,
24584 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
24585 EXPECT_EQ(FormatStyle::LK_ObjC
,
24586 guessLanguage("foo.h", "[[noreturn foo] bar];"));
24587 EXPECT_EQ(FormatStyle::LK_Cpp
,
24588 guessLanguage("foo.h", "[[clang::fallthrough]];"));
24589 EXPECT_EQ(FormatStyle::LK_ObjC
,
24590 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
24591 EXPECT_EQ(FormatStyle::LK_Cpp
,
24592 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
24593 EXPECT_EQ(FormatStyle::LK_Cpp
,
24594 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
24595 EXPECT_EQ(FormatStyle::LK_ObjC
,
24596 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
24597 EXPECT_EQ(FormatStyle::LK_Cpp
,
24598 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
24600 FormatStyle::LK_Cpp
,
24601 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
24603 FormatStyle::LK_Cpp
,
24604 guessLanguage("foo.h",
24605 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
24606 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "[[foo::bar, ...]]"));
24609 TEST_F(FormatTest
, GuessLanguageWithCaret
) {
24610 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "FOO(^);"));
24611 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "FOO(^, Bar);"));
24612 EXPECT_EQ(FormatStyle::LK_ObjC
,
24613 guessLanguage("foo.h", "int(^)(char, float);"));
24614 EXPECT_EQ(FormatStyle::LK_ObjC
,
24615 guessLanguage("foo.h", "int(^foo)(char, float);"));
24616 EXPECT_EQ(FormatStyle::LK_ObjC
,
24617 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
24618 EXPECT_EQ(FormatStyle::LK_ObjC
,
24619 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
24621 FormatStyle::LK_ObjC
,
24622 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
24625 TEST_F(FormatTest
, GuessLanguageWithPragmas
) {
24626 EXPECT_EQ(FormatStyle::LK_Cpp
,
24627 guessLanguage("foo.h", "__pragma(warning(disable:))"));
24628 EXPECT_EQ(FormatStyle::LK_Cpp
,
24629 guessLanguage("foo.h", "#pragma(warning(disable:))"));
24630 EXPECT_EQ(FormatStyle::LK_Cpp
,
24631 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
24634 TEST_F(FormatTest
, FormatsInlineAsmSymbolicNames
) {
24635 // ASM symbolic names are identifiers that must be surrounded by [] without
24636 // space in between:
24637 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
24639 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
24641 asm volatile("mrs
%x
[result
], FPCR
" : [result] "=r
"(result));
24644 // A list of several ASM symbolic names.
24645 verifyFormat(R
"(asm("mov
%[e
], %[d
]" : [d] "=rm
"(d), [e] "rm
"(*e));)");
24647 // ASM symbolic names in inline ASM with inputs and outputs.
24649 asm("cmoveq
%1, %2, %[result
]"
24650 : [result] "=r
"(result)
24651 : "r
"(test), "r
"(new), "[result
]"(old));
24654 // ASM symbolic names in inline ASM with no outputs.
24655 verifyFormat(R
"(asm("mov
%[e
], %[d
]" : : [d] "=rm
"(d), [e] "rm
"(*e));)");
24658 TEST_F(FormatTest
, GuessedLanguageWithInlineAsmClobbers
) {
24659 EXPECT_EQ(FormatStyle::LK_Cpp
,
24660 guessLanguage("foo.h", "void f() {\n"
24661 " asm (\"mov %[e], %[d]\"\n"
24662 " : [d] \"=rm\" (d)\n"
24663 " [e] \"rm\" (*e));\n"
24665 EXPECT_EQ(FormatStyle::LK_Cpp
,
24666 guessLanguage("foo.h", "void f() {\n"
24667 " _asm (\"mov %[e], %[d]\"\n"
24668 " : [d] \"=rm\" (d)\n"
24669 " [e] \"rm\" (*e));\n"
24671 EXPECT_EQ(FormatStyle::LK_Cpp
,
24672 guessLanguage("foo.h", "void f() {\n"
24673 " __asm (\"mov %[e], %[d]\"\n"
24674 " : [d] \"=rm\" (d)\n"
24675 " [e] \"rm\" (*e));\n"
24677 EXPECT_EQ(FormatStyle::LK_Cpp
,
24678 guessLanguage("foo.h", "void f() {\n"
24679 " __asm__ (\"mov %[e], %[d]\"\n"
24680 " : [d] \"=rm\" (d)\n"
24681 " [e] \"rm\" (*e));\n"
24683 EXPECT_EQ(FormatStyle::LK_Cpp
,
24684 guessLanguage("foo.h", "void f() {\n"
24685 " asm (\"mov %[e], %[d]\"\n"
24686 " : [d] \"=rm\" (d),\n"
24687 " [e] \"rm\" (*e));\n"
24689 EXPECT_EQ(FormatStyle::LK_Cpp
,
24690 guessLanguage("foo.h", "void f() {\n"
24691 " asm volatile (\"mov %[e], %[d]\"\n"
24692 " : [d] \"=rm\" (d)\n"
24693 " [e] \"rm\" (*e));\n"
24697 TEST_F(FormatTest
, GuessLanguageWithChildLines
) {
24698 EXPECT_EQ(FormatStyle::LK_Cpp
,
24699 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
24700 EXPECT_EQ(FormatStyle::LK_ObjC
,
24701 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
24703 FormatStyle::LK_Cpp
,
24704 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
24706 FormatStyle::LK_ObjC
,
24707 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
24710 TEST_F(FormatTest
, TypenameMacros
) {
24711 std::vector
<std::string
> TypenameMacros
= {"STACK_OF", "LIST", "TAILQ_ENTRY"};
24713 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
24714 FormatStyle Google
= getGoogleStyleWithColumns(0);
24715 Google
.TypenameMacros
= TypenameMacros
;
24716 verifyFormat("struct foo {\n"
24718 " TAILQ_ENTRY(a) bleh;\n"
24722 FormatStyle Macros
= getLLVMStyle();
24723 Macros
.TypenameMacros
= TypenameMacros
;
24725 verifyFormat("STACK_OF(int) a;", Macros
);
24726 verifyFormat("STACK_OF(int) *a;", Macros
);
24727 verifyFormat("STACK_OF(int const *) *a;", Macros
);
24728 verifyFormat("STACK_OF(int *const) *a;", Macros
);
24729 verifyFormat("STACK_OF(int, string) a;", Macros
);
24730 verifyFormat("STACK_OF(LIST(int)) a;", Macros
);
24731 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros
);
24732 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros
);
24733 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros
);
24734 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros
);
24735 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros
);
24737 Macros
.PointerAlignment
= FormatStyle::PAS_Left
;
24738 verifyFormat("STACK_OF(int)* a;", Macros
);
24739 verifyFormat("STACK_OF(int*)* a;", Macros
);
24740 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros
);
24741 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros
);
24742 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros
);
24745 TEST_F(FormatTest
, AtomicQualifier
) {
24746 // Check that we treate _Atomic as a type and not a function call
24747 FormatStyle Google
= getGoogleStyleWithColumns(0);
24748 verifyFormat("struct foo {\n"
24750 " _Atomic(a) a2;\n"
24751 " _Atomic(_Atomic(int) *const) a3;\n"
24754 verifyFormat("_Atomic(uint64_t) a;");
24755 verifyFormat("_Atomic(uint64_t) *a;");
24756 verifyFormat("_Atomic(uint64_t const *) *a;");
24757 verifyFormat("_Atomic(uint64_t *const) *a;");
24758 verifyFormat("_Atomic(const uint64_t *) *a;");
24759 verifyFormat("_Atomic(uint64_t) a;");
24760 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
24761 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
24762 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
24763 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
24765 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
24766 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
24767 FormatStyle Style
= getLLVMStyle();
24768 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
24769 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style
);
24770 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style
);
24771 verifyFormat("_Atomic(int)* a;", Style
);
24772 verifyFormat("_Atomic(int*)* a;", Style
);
24773 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style
);
24775 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
24776 Style
.SpacesInParensOptions
.InCStyleCasts
= true;
24777 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style
);
24778 Style
.SpacesInParensOptions
.InCStyleCasts
= false;
24779 Style
.SpacesInParensOptions
.Other
= true;
24780 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style
);
24781 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style
);
24784 TEST_F(FormatTest
, C11Generic
) {
24785 verifyFormat("_Generic(x, int: 1, default: 0)");
24786 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
24787 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
24788 verifyFormat("_Generic(x, int: f1, const int: f2)();");
24789 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
24791 verifyFormat("_Generic(x,\n"
24794 " long double: ld,\n"
24795 " float _Complex: fc,\n"
24796 " double _Complex: dc,\n"
24797 " long double _Complex: ldc)");
24799 verifyFormat("while (_Generic(x, //\n"
24800 " long: x)(x) > x) {\n"
24802 verifyFormat("while (_Generic(x, //\n"
24803 " long: x)(x)) {\n"
24805 verifyFormat("x(_Generic(x, //\n"
24808 FormatStyle Style
= getLLVMStyle();
24809 Style
.ColumnLimit
= 40;
24810 verifyFormat("#define LIMIT_MAX(T) \\\n"
24811 " _Generic(((T)0), \\\n"
24812 " unsigned int: UINT_MAX, \\\n"
24813 " unsigned long: ULONG_MAX, \\\n"
24814 " unsigned long long: ULLONG_MAX)",
24816 verifyFormat("_Generic(x,\n"
24818 " void (*)(void): 2);",
24821 Style
.ContinuationIndentWidth
= 2;
24822 verifyFormat("_Generic(x,\n"
24824 " void (*)(void): 2);",
24828 TEST_F(FormatTest
, AmbersandInLamda
) {
24829 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
24830 FormatStyle AlignStyle
= getLLVMStyle();
24831 AlignStyle
.PointerAlignment
= FormatStyle::PAS_Left
;
24832 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle
);
24833 AlignStyle
.PointerAlignment
= FormatStyle::PAS_Right
;
24834 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle
);
24837 TEST_F(FormatTest
, TrailingReturnTypeAuto
) {
24838 FormatStyle Style
= getLLVMStyle();
24839 verifyFormat("[]() -> auto { return Val; }", Style
);
24840 verifyFormat("[]() -> auto * { return Val; }", Style
);
24841 verifyFormat("[]() -> auto & { return Val; }", Style
);
24842 verifyFormat("auto foo() -> auto { return Val; }", Style
);
24843 verifyFormat("auto foo() -> auto * { return Val; }", Style
);
24844 verifyFormat("auto foo() -> auto & { return Val; }", Style
);
24847 TEST_F(FormatTest
, SpacesInConditionalStatement
) {
24848 FormatStyle Spaces
= getLLVMStyle();
24849 Spaces
.IfMacros
.clear();
24850 Spaces
.IfMacros
.push_back("MYIF");
24851 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
24852 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
24853 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces
);
24854 verifyFormat("if ( !a )\n return;", Spaces
);
24855 verifyFormat("if ( a )\n return;", Spaces
);
24856 verifyFormat("if constexpr ( a )\n return;", Spaces
);
24857 verifyFormat("MYIF ( a )\n return;", Spaces
);
24858 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces
);
24859 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces
);
24860 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces
);
24861 verifyFormat("while ( a )\n return;", Spaces
);
24862 verifyFormat("while ( (a && b) )\n return;", Spaces
);
24863 verifyFormat("do {\n} while ( 1 != 0 );", Spaces
);
24864 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces
);
24865 // Check that space on the left of "::" is inserted as expected at beginning
24867 verifyFormat("while ( ::func() )\n return;", Spaces
);
24869 // Check impact of ControlStatementsExceptControlMacros is honored.
24870 Spaces
.SpaceBeforeParens
=
24871 FormatStyle::SBPO_ControlStatementsExceptControlMacros
;
24872 verifyFormat("MYIF( a )\n return;", Spaces
);
24873 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces
);
24874 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces
);
24877 TEST_F(FormatTest
, AlternativeOperators
) {
24878 // Test case for ensuring alternate operators are not
24879 // combined with their right most neighbour.
24880 verifyFormat("int a and b;");
24881 verifyFormat("int a and_eq b;");
24882 verifyFormat("int a bitand b;");
24883 verifyFormat("int a bitor b;");
24884 verifyFormat("int a compl b;");
24885 verifyFormat("int a not b;");
24886 verifyFormat("int a not_eq b;");
24887 verifyFormat("int a or b;");
24888 verifyFormat("int a xor b;");
24889 verifyFormat("int a xor_eq b;");
24890 verifyFormat("return this not_eq bitand other;");
24891 verifyFormat("bool operator not_eq(const X bitand other)");
24893 verifyFormat("int a and 5;");
24894 verifyFormat("int a and_eq 5;");
24895 verifyFormat("int a bitand 5;");
24896 verifyFormat("int a bitor 5;");
24897 verifyFormat("int a compl 5;");
24898 verifyFormat("int a not 5;");
24899 verifyFormat("int a not_eq 5;");
24900 verifyFormat("int a or 5;");
24901 verifyFormat("int a xor 5;");
24902 verifyFormat("int a xor_eq 5;");
24904 verifyFormat("int a compl(5);");
24905 verifyFormat("int a not(5);");
24907 verifyFormat("compl foo();"); // ~foo();
24908 verifyFormat("foo() <%%>"); // foo() {}
24909 verifyFormat("void foo() <%%>"); // void foo() {}
24910 verifyFormat("int a<:1:>;"); // int a[1];
24911 verifyFormat("%:define ABC abc"); // #define ABC abc
24912 verifyFormat("%:%:"); // ##
24914 verifyFormat("a = v(not;);\n"
24918 "e = v(not 123.f);");
24920 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
24925 getLLVMStyleWithColumns(40));
24928 TEST_F(FormatTest
, STLWhileNotDefineChed
) {
24929 verifyFormat("#if defined(while)\n"
24930 "#define while EMIT WARNING C4005\n"
24931 "#endif // while");
24934 TEST_F(FormatTest
, OperatorSpacing
) {
24935 FormatStyle Style
= getLLVMStyle();
24936 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
24937 verifyFormat("Foo::operator*();", Style
);
24938 verifyFormat("Foo::operator void *();", Style
);
24939 verifyFormat("Foo::operator void **();", Style
);
24940 verifyFormat("Foo::operator void *&();", Style
);
24941 verifyFormat("Foo::operator void *&&();", Style
);
24942 verifyFormat("Foo::operator void const *();", Style
);
24943 verifyFormat("Foo::operator void const **();", Style
);
24944 verifyFormat("Foo::operator void const *&();", Style
);
24945 verifyFormat("Foo::operator void const *&&();", Style
);
24946 verifyFormat("Foo::operator()(void *);", Style
);
24947 verifyFormat("Foo::operator*(void *);", Style
);
24948 verifyFormat("Foo::operator*();", Style
);
24949 verifyFormat("Foo::operator**();", Style
);
24950 verifyFormat("Foo::operator&();", Style
);
24951 verifyFormat("Foo::operator<int> *();", Style
);
24952 verifyFormat("Foo::operator<Foo> *();", Style
);
24953 verifyFormat("Foo::operator<int> **();", Style
);
24954 verifyFormat("Foo::operator<Foo> **();", Style
);
24955 verifyFormat("Foo::operator<int> &();", Style
);
24956 verifyFormat("Foo::operator<Foo> &();", Style
);
24957 verifyFormat("Foo::operator<int> &&();", Style
);
24958 verifyFormat("Foo::operator<Foo> &&();", Style
);
24959 verifyFormat("Foo::operator<int> *&();", Style
);
24960 verifyFormat("Foo::operator<Foo> *&();", Style
);
24961 verifyFormat("Foo::operator<int> *&&();", Style
);
24962 verifyFormat("Foo::operator<Foo> *&&();", Style
);
24963 verifyFormat("operator*(int (*)(), class Foo);", Style
);
24965 verifyFormat("Foo::operator&();", Style
);
24966 verifyFormat("Foo::operator void &();", Style
);
24967 verifyFormat("Foo::operator void const &();", Style
);
24968 verifyFormat("Foo::operator()(void &);", Style
);
24969 verifyFormat("Foo::operator&(void &);", Style
);
24970 verifyFormat("Foo::operator&();", Style
);
24971 verifyFormat("operator&(int (&)(), class Foo);", Style
);
24972 verifyFormat("operator&&(int (&)(), class Foo);", Style
);
24974 verifyFormat("Foo::operator&&();", Style
);
24975 verifyFormat("Foo::operator**();", Style
);
24976 verifyFormat("Foo::operator void &&();", Style
);
24977 verifyFormat("Foo::operator void const &&();", Style
);
24978 verifyFormat("Foo::operator()(void &&);", Style
);
24979 verifyFormat("Foo::operator&&(void &&);", Style
);
24980 verifyFormat("Foo::operator&&();", Style
);
24981 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
24982 verifyFormat("operator const nsTArrayRight<E> &()", Style
);
24983 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24985 verifyFormat("operator void **()", Style
);
24986 verifyFormat("operator const FooRight<Object> &()", Style
);
24987 verifyFormat("operator const FooRight<Object> *()", Style
);
24988 verifyFormat("operator const FooRight<Object> **()", Style
);
24989 verifyFormat("operator const FooRight<Object> *&()", Style
);
24990 verifyFormat("operator const FooRight<Object> *&&()", Style
);
24992 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
24993 verifyFormat("Foo::operator*();", Style
);
24994 verifyFormat("Foo::operator**();", Style
);
24995 verifyFormat("Foo::operator void*();", Style
);
24996 verifyFormat("Foo::operator void**();", Style
);
24997 verifyFormat("Foo::operator void*&();", Style
);
24998 verifyFormat("Foo::operator void*&&();", Style
);
24999 verifyFormat("Foo::operator void const*();", Style
);
25000 verifyFormat("Foo::operator void const**();", Style
);
25001 verifyFormat("Foo::operator void const*&();", Style
);
25002 verifyFormat("Foo::operator void const*&&();", Style
);
25003 verifyFormat("Foo::operator/*comment*/ void*();", Style
);
25004 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style
);
25005 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style
);
25006 verifyFormat("Foo::operator()(void*);", Style
);
25007 verifyFormat("Foo::operator*(void*);", Style
);
25008 verifyFormat("Foo::operator*();", Style
);
25009 verifyFormat("Foo::operator<int>*();", Style
);
25010 verifyFormat("Foo::operator<Foo>*();", Style
);
25011 verifyFormat("Foo::operator<int>**();", Style
);
25012 verifyFormat("Foo::operator<Foo>**();", Style
);
25013 verifyFormat("Foo::operator<Foo>*&();", Style
);
25014 verifyFormat("Foo::operator<int>&();", Style
);
25015 verifyFormat("Foo::operator<Foo>&();", Style
);
25016 verifyFormat("Foo::operator<int>&&();", Style
);
25017 verifyFormat("Foo::operator<Foo>&&();", Style
);
25018 verifyFormat("Foo::operator<int>*&();", Style
);
25019 verifyFormat("Foo::operator<Foo>*&();", Style
);
25020 verifyFormat("operator*(int (*)(), class Foo);", Style
);
25022 verifyFormat("Foo::operator&();", Style
);
25023 verifyFormat("Foo::operator void&();", Style
);
25024 verifyFormat("Foo::operator void const&();", Style
);
25025 verifyFormat("Foo::operator/*comment*/ void&();", Style
);
25026 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style
);
25027 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style
);
25028 verifyFormat("Foo::operator()(void&);", Style
);
25029 verifyFormat("Foo::operator&(void&);", Style
);
25030 verifyFormat("Foo::operator&();", Style
);
25031 verifyFormat("operator&(int (&)(), class Foo);", Style
);
25032 verifyFormat("operator&(int (&&)(), class Foo);", Style
);
25033 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
25035 verifyFormat("Foo::operator&&();", Style
);
25036 verifyFormat("Foo::operator void&&();", Style
);
25037 verifyFormat("Foo::operator void const&&();", Style
);
25038 verifyFormat("Foo::operator/*comment*/ void&&();", Style
);
25039 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style
);
25040 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style
);
25041 verifyFormat("Foo::operator()(void&&);", Style
);
25042 verifyFormat("Foo::operator&&(void&&);", Style
);
25043 verifyFormat("Foo::operator&&();", Style
);
25044 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
25045 verifyFormat("operator const nsTArrayLeft<E>&()", Style
);
25046 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
25048 verifyFormat("operator void**()", Style
);
25049 verifyFormat("operator const FooLeft<Object>&()", Style
);
25050 verifyFormat("operator const FooLeft<Object>*()", Style
);
25051 verifyFormat("operator const FooLeft<Object>**()", Style
);
25052 verifyFormat("operator const FooLeft<Object>*&()", Style
);
25053 verifyFormat("operator const FooLeft<Object>*&&()", Style
);
25056 verifyFormat("operator Vector<String>&();", Style
);
25057 verifyFormat("operator const Vector<String>&();", Style
);
25058 verifyFormat("operator foo::Bar*();", Style
);
25059 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style
);
25060 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
25063 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
25064 verifyFormat("Foo::operator*();", Style
);
25065 verifyFormat("Foo::operator void *();", Style
);
25066 verifyFormat("Foo::operator()(void *);", Style
);
25067 verifyFormat("Foo::operator*(void *);", Style
);
25068 verifyFormat("Foo::operator*();", Style
);
25069 verifyFormat("operator*(int (*)(), class Foo);", Style
);
25071 verifyFormat("Foo::operator&();", Style
);
25072 verifyFormat("Foo::operator void &();", Style
);
25073 verifyFormat("Foo::operator void const &();", Style
);
25074 verifyFormat("Foo::operator()(void &);", Style
);
25075 verifyFormat("Foo::operator&(void &);", Style
);
25076 verifyFormat("Foo::operator&();", Style
);
25077 verifyFormat("operator&(int (&)(), class Foo);", Style
);
25079 verifyFormat("Foo::operator&&();", Style
);
25080 verifyFormat("Foo::operator void &&();", Style
);
25081 verifyFormat("Foo::operator void const &&();", Style
);
25082 verifyFormat("Foo::operator()(void &&);", Style
);
25083 verifyFormat("Foo::operator&&(void &&);", Style
);
25084 verifyFormat("Foo::operator&&();", Style
);
25085 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
25088 TEST_F(FormatTest
, OperatorPassedAsAFunctionPtr
) {
25089 FormatStyle Style
= getLLVMStyle();
25091 verifyFormat("foo(operator+, -42);", Style
);
25092 verifyFormat("foo(operator++, -42);", Style
);
25093 verifyFormat("foo(operator--, -42);", Style
);
25094 verifyFormat("foo(-42, operator--);", Style
);
25095 verifyFormat("foo(-42, operator, );", Style
);
25096 verifyFormat("foo(operator, , -42);", Style
);
25099 TEST_F(FormatTest
, WhitespaceSensitiveMacros
) {
25100 FormatStyle Style
= getLLVMStyle();
25101 Style
.WhitespaceSensitiveMacros
.push_back("FOO");
25103 // Newlines are important here.
25104 verifyNoChange("FOO(1+2 )\n", Style
);
25105 verifyNoChange("FOO(a:b:c)\n", Style
);
25107 // Don't use the helpers here, since 'mess up' will change the whitespace
25108 // and these are all whitespace sensitive by definition
25109 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style
);
25110 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style
);
25111 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style
);
25112 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
25113 " Still=Intentional);",
25115 Style
.AlignConsecutiveAssignments
.Enabled
= true;
25116 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
25117 " Still=Intentional);",
25120 Style
.ColumnLimit
= 21;
25121 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style
);
25124 TEST_F(FormatTest
, SkipMacroDefinitionBody
) {
25125 auto Style
= getLLVMStyle();
25126 Style
.SkipMacroDefinitionBody
= true;
25128 verifyFormat("#define A", "#define A", Style
);
25129 verifyFormat("#define A a aa", "#define A a aa", Style
);
25130 verifyNoChange("#define A b", Style
);
25131 verifyNoChange("#define A ( args )", Style
);
25132 verifyNoChange("#define A ( args ) = func ( args )", Style
);
25133 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style
);
25134 verifyNoChange("#define A ( args ) \\\n"
25140 verifyNoChange("#define A x:", Style
);
25141 verifyNoChange("#define A a. b", Style
);
25143 // Surrounded with formatted code.
25144 verifyFormat("int a;\n"
25152 // Columns are not broken when a limit is set.
25153 Style
.ColumnLimit
= 10;
25154 verifyFormat("#define A a a a a", " # define A a a a a ", Style
);
25155 verifyNoChange("#define A a a a a", Style
);
25157 Style
.ColumnLimit
= 15;
25158 verifyFormat("#define A // a\n"
25162 "#define A //a very long comment", Style
);
25163 Style
.ColumnLimit
= 0;
25165 // Multiline definition.
25166 verifyNoChange("#define A \\\n"
25167 "Line one with spaces . \\\n"
25170 verifyNoChange("#define A \\\n"
25175 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
25176 verifyNoChange("#define A \\\n"
25181 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
25182 verifyNoChange("#define A \\\n"
25188 // Adjust indendations but don't change the definition.
25189 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
25190 verifyNoChange("#if A\n"
25194 verifyFormat("#if A\n"
25201 Style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
25202 verifyNoChange("#if A\n"
25206 verifyFormat("#if A\n"
25213 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
25214 verifyNoChange("#if A\n"
25218 verifyFormat("#if A\n"
25226 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
25227 // SkipMacroDefinitionBody should not affect other PP directives
25228 verifyFormat("#if !defined(A)\n"
25231 "#if ! defined ( A )\n"
25237 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a",
25239 verifyNoChange("/* */ #define A a // a a", Style
);
25241 verifyFormat("int a; // a\n"
25250 "#define MACRO_WITH_COMMENTS() \\\n"
25252 " /* Documentation parsed by Doxygen for the following method. */ \\\n"
25253 " static MyType getClassTypeId(); \\\n"
25254 " /** Normal comment for the following method. */ \\\n"
25255 " virtual MyType getTypeId() const;",
25258 // multiline macro definitions
25259 verifyNoChange("#define A a\\\n"
25265 TEST_F(FormatTest
, VeryLongNamespaceCommentSplit
) {
25266 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
25267 // test its interaction with line wrapping
25268 FormatStyle Style
= getLLVMStyleWithColumns(80);
25269 verifyFormat("namespace {\n"
25275 verifyFormat("namespace AAA {\n"
25278 "} // namespace AAA",
25281 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
25284 "} // namespace Averyveryveryverylongnamespace",
25285 "namespace Averyveryveryverylongnamespace {\n"
25293 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25294 " went::mad::now {\n"
25299 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25302 "would::it::save::you::a::lot::of::time::if_::i::"
25303 "just::gave::up::and_::went::mad::now {\n"
25309 // This used to duplicate the comment again and again on subsequent runs
25312 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
25313 " went::mad::now {\n"
25318 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
25321 "would::it::save::you::a::lot::of::time::if_::i::"
25322 "just::gave::up::and_::went::mad::now {\n"
25327 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
25328 "and_::went::mad::now",
25332 TEST_F(FormatTest
, LikelyUnlikely
) {
25333 FormatStyle Style
= getLLVMStyle();
25335 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25340 verifyFormat("if (argc > 5) [[likely]] {\n"
25345 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25347 "} else [[likely]] {\n"
25352 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25354 "} else if (argc > 10) [[likely]] {\n"
25361 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
25366 verifyFormat("if (argc > 5) [[unlikely]]\n"
25369 verifyFormat("if (argc > 5) [[likely]]\n"
25373 verifyFormat("while (limit > 0) [[unlikely]] {\n"
25377 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
25382 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
25385 verifyFormat("while (limit > 0) [[likely]]\n"
25389 Style
.AttributeMacros
.push_back("UNLIKELY");
25390 Style
.AttributeMacros
.push_back("LIKELY");
25391 verifyFormat("if (argc > 5) UNLIKELY\n"
25395 verifyFormat("if (argc > 5) UNLIKELY {\n"
25399 verifyFormat("if (argc > 5) UNLIKELY {\n"
25401 "} else [[likely]] {\n"
25405 verifyFormat("if (argc > 5) UNLIKELY {\n"
25407 "} else LIKELY {\n"
25411 verifyFormat("if (argc > 5) [[unlikely]] {\n"
25413 "} else LIKELY {\n"
25418 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
25422 verifyFormat("while (limit > 0) LIKELY {\n"
25427 verifyFormat("while (limit > 0) UNLIKELY\n"
25430 verifyFormat("for (auto &limit : limits) LIKELY\n"
25435 TEST_F(FormatTest
, PenaltyIndentedWhitespace
) {
25436 verifyFormat("Constructor()\n"
25437 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25438 " aaaa(aaaaaaaaaaaaaaaaaa, "
25439 "aaaaaaaaaaaaaaaaaat))");
25440 verifyFormat("Constructor()\n"
25441 " : aaaaaaaaaaaaa(aaaaaa), "
25442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
25444 FormatStyle StyleWithWhitespacePenalty
= getLLVMStyle();
25445 StyleWithWhitespacePenalty
.PenaltyIndentedWhitespace
= 5;
25446 verifyFormat("Constructor()\n"
25447 " : aaaaaa(aaaaaa),\n"
25448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
25449 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
25450 StyleWithWhitespacePenalty
);
25451 verifyFormat("Constructor()\n"
25452 " : aaaaaaaaaaaaa(aaaaaa), "
25453 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
25454 StyleWithWhitespacePenalty
);
25457 TEST_F(FormatTest
, LLVMDefaultStyle
) {
25458 FormatStyle Style
= getLLVMStyle();
25459 verifyFormat("extern \"C\" {\n"
25464 TEST_F(FormatTest
, GNUDefaultStyle
) {
25465 FormatStyle Style
= getGNUStyle();
25466 verifyFormat("extern \"C\"\n"
25472 TEST_F(FormatTest
, MozillaDefaultStyle
) {
25473 FormatStyle Style
= getMozillaStyle();
25474 verifyFormat("extern \"C\"\n"
25480 TEST_F(FormatTest
, GoogleDefaultStyle
) {
25481 FormatStyle Style
= getGoogleStyle();
25482 verifyFormat("extern \"C\" {\n"
25487 TEST_F(FormatTest
, ChromiumDefaultStyle
) {
25488 FormatStyle Style
= getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp
);
25489 verifyFormat("extern \"C\" {\n"
25494 TEST_F(FormatTest
, MicrosoftDefaultStyle
) {
25495 FormatStyle Style
= getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp
);
25496 verifyFormat("extern \"C\"\n"
25502 TEST_F(FormatTest
, WebKitDefaultStyle
) {
25503 FormatStyle Style
= getWebKitStyle();
25504 verifyFormat("extern \"C\" {\n"
25510 TEST_F(FormatTest
, Concepts
) {
25511 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations
,
25512 FormatStyle::BBCDS_Always
);
25514 // The default in LLVM style is REI_OuterScope, but these tests were written
25515 // when the default was REI_Keyword.
25516 FormatStyle Style
= getLLVMStyle();
25517 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
25519 verifyFormat("template <typename T>\n"
25520 "concept True = true;");
25522 verifyFormat("template <typename T>\n"
25523 "concept C = ((false || foo()) && C2<T>) ||\n"
25524 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
25525 getLLVMStyleWithColumns(60));
25527 verifyFormat("template <typename T>\n"
25528 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
25529 "sizeof(T) <= 8;");
25531 verifyFormat("template <typename T>\n"
25532 "concept DelayedCheck = true && requires(T t) {\n"
25535 " } && sizeof(T) <= 8;",
25538 verifyFormat("template <typename T>\n"
25539 "concept DelayedCheck = true && requires(T t) { // Comment\n"
25542 " } && sizeof(T) <= 8;",
25545 verifyFormat("template <typename T>\n"
25546 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
25547 "sizeof(T) <= 8;");
25549 verifyFormat("template <typename T>\n"
25550 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
25552 verifyFormat("template <typename T>\n"
25553 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
25555 verifyFormat("template <typename T>\n"
25556 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
25558 verifyFormat("template <typename T>\n"
25559 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
25560 "&& sizeof(T) <= 8;");
25562 verifyFormat("template <typename T>\n"
25563 "concept DelayedCheck =\n"
25564 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
25565 "sizeof(T) <= 8;");
25567 verifyFormat("template <typename T>\n"
25568 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
25569 "&& sizeof(T) <= 8;");
25572 "template <typename T>\n"
25573 "concept DelayedCheck =\n"
25574 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25576 verifyFormat("template <typename T>\n"
25577 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
25578 "&& sizeof(T) <= 8;");
25580 verifyFormat("template <typename T>\n"
25581 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
25582 "sizeof(T) <= 8;");
25584 verifyFormat("template <typename T>\n"
25585 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
25586 " requires(T t) {\n"
25589 " } && sizeof(T) <= 8 && !(4 < 3);",
25590 getLLVMStyleWithColumns(60));
25592 verifyFormat("template <typename T>\n"
25593 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
25595 verifyFormat("template <typename T>\n"
25596 "concept C = foo();");
25598 verifyFormat("template <typename T>\n"
25599 "concept C = foo(T());");
25601 verifyFormat("template <typename T>\n"
25602 "concept C = foo(T{});");
25604 verifyFormat("template <typename T>\n"
25605 "concept Size = V<sizeof(T)>::Value > 5;");
25607 verifyFormat("template <typename T>\n"
25608 "concept True = S<T>::Value;");
25610 verifyFormat("template <S T>\n"
25611 "concept True = T.field;");
25614 "template <typename T>\n"
25615 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
25616 " sizeof(T) <= 8;");
25618 // FIXME: This is misformatted because the fake l paren starts at bool, not at
25619 // the lambda l square.
25620 verifyFormat("template <typename T>\n"
25621 "concept C = [] -> bool { return true; }() && requires(T t) { "
25623 " sizeof(T) <= 8;");
25626 "template <typename T>\n"
25627 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
25628 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25630 verifyFormat("template <typename T>\n"
25631 "concept C = decltype([]() { return std::true_type{}; "
25632 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25633 getLLVMStyleWithColumns(120));
25635 verifyFormat("template <typename T>\n"
25636 "concept C = decltype([]() -> std::true_type { return {}; "
25638 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
25640 verifyFormat("template <typename T>\n"
25641 "concept C = true;\n"
25644 verifyFormat("template <typename T>\n"
25645 "concept Hashable = requires(T a) {\n"
25646 " { std::hash<T>{}(a) } -> "
25647 "std::convertible_to<std::size_t>;\n"
25652 "template <typename T>\n"
25653 "concept EqualityComparable = requires(T a, T b) {\n"
25654 " { a == b } -> std::same_as<bool>;\n"
25659 "template <typename T>\n"
25660 "concept EqualityComparable = requires(T a, T b) {\n"
25661 " { a == b } -> std::same_as<bool>;\n"
25662 " { a != b } -> std::same_as<bool>;\n"
25666 verifyFormat("template <typename T>\n"
25667 "concept WeakEqualityComparable = requires(T a, T b) {\n"
25673 verifyFormat("template <typename T>\n"
25674 "concept HasSizeT = requires { typename T::size_t; };");
25676 verifyFormat("template <typename T>\n"
25677 "concept Semiregular =\n"
25678 " DefaultConstructible<T> && CopyConstructible<T> && "
25679 "CopyAssignable<T> &&\n"
25680 " 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 T[n])>;\n"
25685 " { delete new T; };\n"
25686 " { delete new T[n]; };\n"
25690 verifyFormat("template <typename T>\n"
25691 "concept Semiregular =\n"
25692 " requires(T a, std::size_t n) {\n"
25693 " requires Same<T *, decltype(&a)>;\n"
25694 " { a.~T() } noexcept;\n"
25695 " requires Same<T *, decltype(new T)>;\n"
25696 " requires Same<T *, decltype(new T[n])>;\n"
25697 " { delete new T; };\n"
25698 " { delete new T[n]; };\n"
25699 " { new T } -> std::same_as<T *>;\n"
25700 " } && DefaultConstructible<T> && CopyConstructible<T> && "
25701 "CopyAssignable<T>;",
25705 "template <typename T>\n"
25706 "concept Semiregular =\n"
25707 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
25708 " requires Same<T *, decltype(&a)>;\n"
25709 " { a.~T() } noexcept;\n"
25710 " requires Same<T *, decltype(new T)>;\n"
25711 " requires Same<T *, decltype(new "
25713 " { delete new T; };\n"
25714 " { delete new T[n]; };\n"
25715 " } && CopyConstructible<T> && "
25716 "CopyAssignable<T>;",
25719 verifyFormat("template <typename T>\n"
25720 "concept Two = requires(T t) {\n"
25721 " { t.foo() } -> std::same_as<Bar>;\n"
25722 " } && requires(T &&t) {\n"
25723 " { t.foo() } -> std::same_as<Bar &&>;\n"
25728 "template <typename T>\n"
25729 "concept C = requires(T x) {\n"
25730 " { *x } -> std::convertible_to<typename T::inner>;\n"
25731 " { x + 1 } noexcept -> std::same_as<int>;\n"
25732 " { x * 1 } -> std::convertible_to<T>;\n"
25736 verifyFormat("template <typename T>\n"
25737 "concept C = requires(T x) {\n"
25739 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25740 " } -> long_long_concept_name<T>;\n"
25742 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
25743 " } noexcept -> long_long_concept_name<T>;\n"
25748 "template <typename T, typename U = T>\n"
25749 "concept Swappable = requires(T &&t, U &&u) {\n"
25750 " swap(std::forward<T>(t), std::forward<U>(u));\n"
25751 " swap(std::forward<U>(u), std::forward<T>(t));\n"
25755 verifyFormat("template <typename T, typename U>\n"
25756 "concept Common = requires(T &&t, U &&u) {\n"
25757 " typename CommonType<T, U>;\n"
25758 " { CommonType<T, U>(std::forward<T>(t)) };\n"
25762 verifyFormat("template <typename T, typename U>\n"
25763 "concept Common = requires(T &&t, U &&u) {\n"
25764 " typename CommonType<T, U>;\n"
25765 " { CommonType<T, U>{std::forward<T>(t)} };\n"
25770 "template <typename T>\n"
25771 "concept C = requires(T t) {\n"
25772 " requires Bar<T> && Foo<T>;\n"
25773 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25777 verifyFormat("template <typename T>\n"
25778 "concept HasFoo = requires(T t) {\n"
25782 "template <typename T>\n"
25783 "concept HasBar = requires(T t) {\n"
25789 verifyFormat("template <typename T>\n"
25790 "concept Large = sizeof(T) > 10;");
25792 verifyFormat("template <typename T, typename U>\n"
25793 "concept FooableWith = requires(T t, U u) {\n"
25794 " typename T::foo_type;\n"
25795 " { t.foo(u) } -> typename T::foo_type;\n"
25798 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
25801 verifyFormat("template <typename T>\n"
25802 "concept Context = is_specialization_of_v<context, T>;");
25804 verifyFormat("template <typename T>\n"
25805 "concept Node = std::is_object_v<T>;");
25807 verifyFormat("template <class T>\n"
25808 "concept integral = __is_integral(T);");
25810 verifyFormat("template <class T>\n"
25811 "concept is2D = __array_extent(T, 1) == 2;");
25813 verifyFormat("template <class T>\n"
25814 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
25816 verifyFormat("template <class T, class T2>\n"
25817 "concept Same = __is_same_as<T, T2>;");
25820 "template <class _InIt, class _OutIt>\n"
25821 "concept _Can_reread_dest =\n"
25822 " std::forward_iterator<_OutIt> &&\n"
25823 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
25825 Style
.BreakBeforeConceptDeclarations
= FormatStyle::BBCDS_Allowed
;
25828 "template <typename T>\n"
25829 "concept C = requires(T t) {\n"
25830 " requires Bar<T> && Foo<T>;\n"
25831 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25835 verifyFormat("template <typename T>\n"
25836 "concept HasFoo = requires(T t) {\n"
25840 "template <typename T>\n"
25841 "concept HasBar = requires(T t) {\n"
25847 verifyFormat("template <typename T> concept True = true;", Style
);
25849 verifyFormat("template <typename T>\n"
25850 "concept C = decltype([]() -> std::true_type { return {}; "
25852 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25855 verifyFormat("template <typename T>\n"
25856 "concept Semiregular =\n"
25857 " DefaultConstructible<T> && CopyConstructible<T> && "
25858 "CopyAssignable<T> &&\n"
25859 " requires(T a, std::size_t n) {\n"
25860 " requires Same<T *, decltype(&a)>;\n"
25861 " { a.~T() } noexcept;\n"
25862 " requires Same<T *, decltype(new T)>;\n"
25863 " requires Same<T *, decltype(new T[n])>;\n"
25864 " { delete new T; };\n"
25865 " { delete new T[n]; };\n"
25869 Style
.BreakBeforeConceptDeclarations
= FormatStyle::BBCDS_Never
;
25871 verifyFormat("template <typename T> concept C =\n"
25872 " requires(T t) {\n"
25873 " requires Bar<T> && Foo<T>;\n"
25874 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
25878 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
25882 "template <typename T> concept HasBar = requires(T t) {\n"
25888 verifyFormat("template <typename T> concept True = true;", Style
);
25891 "template <typename T> concept C =\n"
25892 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
25893 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
25896 verifyFormat("template <typename T> concept Semiregular =\n"
25897 " DefaultConstructible<T> && CopyConstructible<T> && "
25898 "CopyAssignable<T> &&\n"
25899 " requires(T a, std::size_t n) {\n"
25900 " requires Same<T *, decltype(&a)>;\n"
25901 " { a.~T() } noexcept;\n"
25902 " requires Same<T *, decltype(new T)>;\n"
25903 " requires Same<T *, decltype(new T[n])>;\n"
25904 " { delete new T; };\n"
25905 " { delete new T[n]; };\n"
25909 // The following tests are invalid C++, we just want to make sure we don't
25911 verifyNoCrash("template <typename T>\n"
25912 "concept C = requires C2<T>;");
25914 verifyNoCrash("template <typename T>\n"
25915 "concept C = 5 + 4;");
25917 verifyNoCrash("template <typename T>\n"
25918 "concept C = class X;");
25920 verifyNoCrash("template <typename T>\n"
25921 "concept C = [] && true;");
25923 verifyNoCrash("template <typename T>\n"
25924 "concept C = [] && requires(T t) { typename T::size_type; };");
25927 TEST_F(FormatTest
, RequiresClausesPositions
) {
25928 auto Style
= getLLVMStyle();
25929 EXPECT_EQ(Style
.RequiresClausePosition
, FormatStyle::RCPS_OwnLine
);
25930 EXPECT_EQ(Style
.IndentRequiresClause
, true);
25932 // The default in LLVM style is REI_OuterScope, but these tests were written
25933 // when the default was REI_Keyword.
25934 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
25936 verifyFormat("template <typename T>\n"
25937 " requires(Foo<T> && std::trait<T>)\n"
25941 verifyFormat("template <typename T>\n"
25942 " requires(Foo<T> && std::trait<T>)\n"
25951 "template <typename T>\n"
25952 " requires requires(T &&t) {\n"
25953 " typename T::I;\n"
25954 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
25956 "Bar(T) -> Bar<typename T::I>;",
25959 verifyFormat("template <typename T>\n"
25960 " requires(Foo<T> && std::trait<T>)\n"
25961 "constexpr T MyGlobal;",
25964 verifyFormat("template <typename T>\n"
25965 " requires Foo<T> && requires(T t) {\n"
25966 " { t.baz() } -> std::same_as<bool>;\n"
25967 " requires std::same_as<T::Factor, int>;\n"
25969 "inline int bar(T t) {\n"
25970 " return t.baz() ? T::Factor : 5;\n"
25974 verifyFormat("template <typename T>\n"
25975 "inline int bar(T t)\n"
25976 " requires Foo<T> && requires(T t) {\n"
25977 " { t.baz() } -> std::same_as<bool>;\n"
25978 " requires std::same_as<T::Factor, int>;\n"
25981 " return t.baz() ? T::Factor : 5;\n"
25985 verifyFormat("template <typename T>\n"
25992 verifyFormat("template <typename T>\n"
26000 verifyFormat("template <typename T>\n"
26001 "int S::bar(T t) &&\n"
26008 verifyFormat("template <typename T>\n"
26013 Style
.IndentRequiresClause
= false;
26014 verifyFormat("template <typename T>\n"
26021 verifyFormat("template <typename T>\n"
26022 "int S::bar(T t) &&\n"
26029 verifyFormat("template <typename T>\n"
26037 Style
.RequiresClausePosition
= FormatStyle::RCPS_OwnLineWithBrace
;
26038 Style
.IndentRequiresClause
= true;
26040 verifyFormat("template <typename T>\n"
26041 " requires(Foo<T> && std::trait<T>)\n"
26045 verifyFormat("template <typename T>\n"
26046 " requires(Foo<T> && std::trait<T>)\n"
26055 "template <typename T>\n"
26056 " requires requires(T &&t) {\n"
26057 " typename T::I;\n"
26058 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
26060 "Bar(T) -> Bar<typename T::I>;",
26063 verifyFormat("template <typename T>\n"
26064 " requires(Foo<T> && std::trait<T>)\n"
26065 "constexpr T MyGlobal;",
26068 verifyFormat("template <typename T>\n"
26069 " requires Foo<T> && requires(T t) {\n"
26070 " { t.baz() } -> std::same_as<bool>;\n"
26071 " requires std::same_as<T::Factor, int>;\n"
26073 "inline int bar(T t) {\n"
26074 " return t.baz() ? T::Factor : 5;\n"
26078 verifyFormat("template <typename T>\n"
26079 "inline int bar(T t)\n"
26080 " requires Foo<T> && requires(T t) {\n"
26081 " { t.baz() } -> std::same_as<bool>;\n"
26082 " requires std::same_as<T::Factor, int>;\n"
26084 " return t.baz() ? T::Factor : 5;\n"
26088 verifyFormat("template <typename T>\n"
26095 verifyFormat("template <typename T>\n"
26097 " requires F<T> {\n"
26102 verifyFormat("template <typename T>\n"
26103 "int S::bar(T t) &&\n"
26104 " requires F<T> {\n"
26109 verifyFormat("template <typename T>\n"
26114 verifyFormat("template <typename T>\n"
26116 " requires F<T> {}",
26119 Style
.RequiresClausePosition
= FormatStyle::RCPS_SingleLine
;
26120 Style
.IndentRequiresClause
= false;
26121 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
26122 "template <typename T> requires Foo<T> void bar() {}\n"
26123 "template <typename T> void bar() requires Foo<T> {}\n"
26124 "template <typename T> void bar() requires Foo<T>;\n"
26125 "template <typename T> void S::bar() && requires Foo<T> {}\n"
26126 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
26129 auto ColumnStyle
= Style
;
26130 ColumnStyle
.ColumnLimit
= 40;
26131 verifyFormat("template <typename AAAAAAA>\n"
26132 "requires Foo<T> struct Bar {};\n"
26133 "template <typename AAAAAAA>\n"
26134 "requires Foo<T> void bar() {}\n"
26135 "template <typename AAAAAAA>\n"
26136 "void bar() requires Foo<T> {}\n"
26137 "template <typename T>\n"
26138 "void S::bar() && requires Foo<T> {}\n"
26139 "template <typename AAAAAAA>\n"
26140 "requires Foo<T> Baz(T) -> Baz<T>;",
26143 verifyFormat("template <typename T>\n"
26144 "requires Foo<AAAAAAA> struct Bar {};\n"
26145 "template <typename T>\n"
26146 "requires Foo<AAAAAAA> void bar() {}\n"
26147 "template <typename T>\n"
26148 "void bar() requires Foo<AAAAAAA> {}\n"
26149 "template <typename T>\n"
26150 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
26153 verifyFormat("template <typename AAAAAAA>\n"
26154 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26156 "template <typename AAAAAAA>\n"
26157 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26159 "template <typename AAAAAAA>\n"
26161 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26162 "template <typename AAAAAAA>\n"
26163 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26164 "template <typename AAAAAAA>\n"
26165 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26166 "Bar(T) -> Bar<T>;",
26169 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithFollowing
;
26170 ColumnStyle
.RequiresClausePosition
= FormatStyle::RCPS_WithFollowing
;
26172 verifyFormat("template <typename T>\n"
26173 "requires Foo<T> struct Bar {};\n"
26174 "template <typename T>\n"
26175 "requires Foo<T> void bar() {}\n"
26176 "template <typename T>\n"
26178 "requires Foo<T> {}\n"
26179 "template <typename T>\n"
26181 "requires Foo<T>;\n"
26182 "template <typename T>\n"
26183 "void S::bar() &&\n"
26184 "requires Foo<T> {}\n"
26185 "template <typename T>\n"
26186 "requires Foo<T> Bar(T) -> Bar<T>;",
26189 verifyFormat("template <typename AAAAAAA>\n"
26190 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26192 "template <typename AAAAAAA>\n"
26193 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26195 "template <typename AAAAAAA>\n"
26197 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26198 "template <typename AAAAAAA>\n"
26199 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
26200 "template <typename AAAAAAA>\n"
26201 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26202 "Bar(T) -> Bar<T>;",
26205 Style
.IndentRequiresClause
= true;
26206 ColumnStyle
.IndentRequiresClause
= true;
26208 verifyFormat("template <typename T>\n"
26209 " requires Foo<T> struct Bar {};\n"
26210 "template <typename T>\n"
26211 " requires Foo<T> void bar() {}\n"
26212 "template <typename T>\n"
26214 " requires Foo<T> {}\n"
26215 "template <typename T>\n"
26216 "void S::bar() &&\n"
26217 " requires Foo<T> {}\n"
26218 "template <typename T>\n"
26219 " requires Foo<T> Bar(T) -> Bar<T>;",
26222 verifyFormat("template <typename AAAAAAA>\n"
26223 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26225 "template <typename AAAAAAA>\n"
26226 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26228 "template <typename AAAAAAA>\n"
26230 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
26231 "template <typename AAAAAAA>\n"
26232 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
26233 "template <typename AAAAAAA>\n"
26234 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26235 "Bar(T) -> Bar<T>;",
26238 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
26239 ColumnStyle
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
26241 verifyFormat("template <typename T> requires Foo<T>\n"
26243 "template <typename T> requires Foo<T>\n"
26245 "template <typename T>\n"
26246 "void bar() requires Foo<T>\n"
26248 "template <typename T> void bar() requires Foo<T>;\n"
26249 "template <typename T>\n"
26250 "void S::bar() && requires Foo<T>\n"
26252 "template <typename T> requires Foo<T>\n"
26253 "Bar(T) -> Bar<T>;",
26256 verifyFormat("template <typename AAAAAAA>\n"
26257 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26259 "template <typename AAAAAAA>\n"
26260 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26262 "template <typename AAAAAAA>\n"
26264 " requires Foo<AAAAAAAAAAAAAAAA>\n"
26266 "template <typename AAAAAAA>\n"
26267 "requires Foo<AAAAAAAA>\n"
26268 "Bar(T) -> Bar<T>;\n"
26269 "template <typename AAAAAAA>\n"
26270 "requires Foo<AAAAAAAAAAAAAAAA>\n"
26271 "Bar(T) -> Bar<T>;",
26275 TEST_F(FormatTest
, RequiresClauses
) {
26276 verifyFormat("struct [[nodiscard]] zero_t {\n"
26277 " template <class T>\n"
26278 " requires requires { number_zero_v<T>; }\n"
26279 " [[nodiscard]] constexpr operator T() const {\n"
26280 " return number_zero_v<T>;\n"
26284 verifyFormat("template <class T>\n"
26285 " requires(std::same_as<int, T>)\n"
26286 "decltype(auto) fun() {}");
26288 auto Style
= getLLVMStyle();
26291 "template <typename T>\n"
26292 " requires is_default_constructible_v<hash<T>> and\n"
26293 " is_copy_constructible_v<hash<T>> and\n"
26294 " is_move_constructible_v<hash<T>> and\n"
26295 " is_copy_assignable_v<hash<T>> and "
26296 "is_move_assignable_v<hash<T>> and\n"
26297 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
26298 " is_callable_v<hash<T>(T)> and\n"
26299 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
26300 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
26301 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
26305 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
26307 "template <typename T>\n"
26308 " requires is_default_constructible_v<hash<T>>\n"
26309 " and is_copy_constructible_v<hash<T>>\n"
26310 " and is_move_constructible_v<hash<T>>\n"
26311 " and is_copy_assignable_v<hash<T>> and "
26312 "is_move_assignable_v<hash<T>>\n"
26313 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
26314 " and is_callable_v<hash<T>(T)>\n"
26315 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
26316 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
26317 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
26322 Style
= getLLVMStyle();
26323 Style
.ConstructorInitializerIndentWidth
= 4;
26324 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
26325 Style
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
26326 verifyFormat("constexpr Foo(Foo const &other)\n"
26327 " requires std::is_copy_constructible<T>\n"
26328 " : value{other.value} {\n"
26330 " do_more_magic();\n"
26334 // Not a clause, but we once hit an assert.
26335 verifyFormat("#if 0\n"
26342 TEST_F(FormatTest
, RequiresExpressionIndentation
) {
26343 auto Style
= getLLVMStyle();
26344 EXPECT_EQ(Style
.RequiresExpressionIndentation
, FormatStyle::REI_OuterScope
);
26346 verifyFormat("template <typename T>\n"
26347 "concept C = requires(T t) {\n"
26348 " typename T::value;\n"
26349 " requires requires(typename T::value v) {\n"
26350 " { t == v } -> std::same_as<bool>;\n"
26355 verifyFormat("template <typename T>\n"
26357 " requires Foo<T> && requires(T t) {\n"
26358 " { t.foo() } -> std::same_as<int>;\n"
26359 " } && requires(T t) {\n"
26360 " { t.bar() } -> std::same_as<bool>;\n"
26365 verifyFormat("template <typename T>\n"
26366 " requires Foo<T> &&\n"
26367 " requires(T t) {\n"
26368 " { t.foo() } -> std::same_as<int>;\n"
26369 " } && requires(T t) {\n"
26370 " { t.bar() } -> std::same_as<bool>;\n"
26376 verifyFormat("template <typename T> void f() {\n"
26377 " if constexpr (requires(T t) {\n"
26378 " { t.bar() } -> std::same_as<bool>;\n"
26384 verifyFormat("template <typename T> void f() {\n"
26385 " if constexpr (condition && requires(T t) {\n"
26386 " { t.bar() } -> std::same_as<bool>;\n"
26392 verifyFormat("template <typename T> struct C {\n"
26394 " requires requires(T t) {\n"
26395 " { t.bar() } -> std::same_as<bool>;\n"
26400 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
26402 verifyFormat("template <typename T>\n"
26403 "concept C = requires(T t) {\n"
26404 " typename T::value;\n"
26405 " requires requires(typename T::value v) {\n"
26406 " { t == v } -> std::same_as<bool>;\n"
26412 "template <typename T>\n"
26414 " requires Foo<T> && requires(T t) {\n"
26415 " { t.foo() } -> std::same_as<int>;\n"
26416 " } && requires(T t) {\n"
26417 " { t.bar() } -> std::same_as<bool>;\n"
26422 verifyFormat("template <typename T>\n"
26423 " requires Foo<T> &&\n"
26424 " requires(T t) {\n"
26425 " { t.foo() } -> std::same_as<int>;\n"
26426 " } && requires(T t) {\n"
26427 " { t.bar() } -> std::same_as<bool>;\n"
26433 verifyFormat("template <typename T> void f() {\n"
26434 " if constexpr (requires(T t) {\n"
26435 " { t.bar() } -> std::same_as<bool>;\n"
26442 "template <typename T> void f() {\n"
26443 " if constexpr (condition && requires(T t) {\n"
26444 " { t.bar() } -> std::same_as<bool>;\n"
26450 verifyFormat("template <typename T> struct C {\n"
26452 " requires requires(T t) {\n"
26453 " { t.bar() } -> std::same_as<bool>;\n"
26459 TEST_F(FormatTest
, StatementAttributeLikeMacros
) {
26460 FormatStyle Style
= getLLVMStyle();
26461 StringRef Source
= "void Foo::slot() {\n"
26462 " unsigned char MyChar = 'x';\n"
26463 " emit signal(MyChar);\n"
26464 " Q_EMIT signal(MyChar);\n"
26467 verifyFormat(Source
, Style
);
26469 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
26470 verifyFormat("void Foo::slot() {\n"
26471 " unsigned char MyChar = 'x';\n"
26472 " emit signal(MyChar);\n"
26473 " Q_EMIT signal(MyChar);\n"
26477 Style
.StatementAttributeLikeMacros
.push_back("emit");
26478 verifyFormat(Source
, Style
);
26480 Style
.StatementAttributeLikeMacros
= {};
26481 verifyFormat("void Foo::slot() {\n"
26482 " unsigned char MyChar = 'x';\n"
26483 " emit signal(MyChar);\n"
26484 " Q_EMIT signal(MyChar);\n"
26489 TEST_F(FormatTest
, IndentAccessModifiers
) {
26490 FormatStyle Style
= getLLVMStyle();
26491 Style
.IndentAccessModifiers
= true;
26492 // Members are *two* levels below the record;
26493 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
26494 verifyFormat("class C {\n"
26498 verifyFormat("union C {\n"
26503 // Access modifiers should be indented one level below the record.
26504 verifyFormat("class C {\n"
26509 verifyFormat("class C {\n"
26510 " public /* comment */:\n"
26514 verifyFormat("struct S {\n"
26527 // Enumerations are not records and should be unaffected.
26528 Style
.AllowShortEnumsOnASingleLine
= false;
26529 verifyFormat("enum class E {\n"
26534 // Test with a different indentation width;
26535 // also proves that the result is Style.AccessModifierOffset agnostic.
26536 Style
.IndentWidth
= 3;
26537 verifyFormat("class C {\n"
26542 verifyFormat("class C {\n"
26547 Style
.AttributeMacros
.push_back("FOO");
26548 verifyFormat("class C {\n"
26555 TEST_F(FormatTest
, LimitlessStringsAndComments
) {
26556 auto Style
= getLLVMStyleWithColumns(0);
26557 constexpr StringRef Code
=
26559 " * This is a multiline comment with quite some long lines, at least for "
26560 "the LLVM Style.\n"
26561 " * We will redo this with strings and line comments. Just to check if "
26562 "everything is working.\n"
26565 " /* Single line multi line comment. */\n"
26566 " const std::string String = \"This is a multiline string with quite "
26567 "some long lines, at least for the LLVM Style.\"\n"
26568 " \"We already did it with multi line "
26569 "comments, and we will do it with line comments. Just to check if "
26570 "everything is working.\";\n"
26571 " // This is a line comment (block) with quite some long lines, at "
26572 "least for the LLVM Style.\n"
26573 " // We already did this with multi line comments and strings. Just to "
26574 "check if everything is working.\n"
26575 " const std::string SmallString = \"Hello World\";\n"
26576 " // Small line comment\n"
26577 " return String.size() > SmallString.size();\n"
26579 verifyNoChange(Code
, Style
);
26582 TEST_F(FormatTest
, FormatDecayCopy
) {
26583 // error cases from unit tests
26584 verifyFormat("foo(auto())");
26585 verifyFormat("foo(auto{})");
26586 verifyFormat("foo(auto({}))");
26587 verifyFormat("foo(auto{{}})");
26589 verifyFormat("foo(auto(1))");
26590 verifyFormat("foo(auto{1})");
26591 verifyFormat("foo(new auto(1))");
26592 verifyFormat("foo(new auto{1})");
26593 verifyFormat("decltype(auto(1)) x;");
26594 verifyFormat("decltype(auto{1}) x;");
26595 verifyFormat("auto(x);");
26596 verifyFormat("auto{x};");
26597 verifyFormat("new auto{x};");
26598 verifyFormat("auto{x} = y;");
26599 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
26600 // the user's own fault
26601 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
26602 // clearly the user's own fault
26603 verifyFormat("auto (*p)() = f;");
26606 TEST_F(FormatTest
, Cpp20ModulesSupport
) {
26607 FormatStyle Style
= getLLVMStyle();
26608 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
26609 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
26611 verifyFormat("export import foo;", Style
);
26612 verifyFormat("export import foo:bar;", Style
);
26613 verifyFormat("export import foo.bar;", Style
);
26614 verifyFormat("export import foo.bar:baz;", Style
);
26615 verifyFormat("export import :bar;", Style
);
26616 verifyFormat("export module foo:bar;", Style
);
26617 verifyFormat("export module foo;", Style
);
26618 verifyFormat("export module foo.bar;", Style
);
26619 verifyFormat("export module foo.bar:baz;", Style
);
26620 verifyFormat("export import <string_view>;", Style
);
26621 verifyFormat("export import <Foo/Bar>;", Style
);
26623 verifyFormat("export type_name var;", Style
);
26624 verifyFormat("template <class T> export using A = B<T>;", Style
);
26625 verifyFormat("export using A = B;", Style
);
26626 verifyFormat("export int func() {\n"
26630 verifyFormat("export struct {\n"
26634 verifyFormat("export {\n"
26638 verifyFormat("export export char const *hello() { return \"hello\"; }");
26640 verifyFormat("import bar;", Style
);
26641 verifyFormat("import foo.bar;", Style
);
26642 verifyFormat("import foo:bar;", Style
);
26643 verifyFormat("import :bar;", Style
);
26644 verifyFormat("import /* module partition */ :bar;", Style
);
26645 verifyFormat("import <ctime>;", Style
);
26646 verifyFormat("import \"header\";", Style
);
26648 verifyFormat("module foo;", Style
);
26649 verifyFormat("module foo:bar;", Style
);
26650 verifyFormat("module foo.bar;", Style
);
26651 verifyFormat("module;", Style
);
26653 verifyFormat("export namespace hi {\n"
26654 "const char *sayhi();\n"
26658 verifyFormat("module :private;", Style
);
26659 verifyFormat("import <foo/bar.h>;", Style
);
26660 verifyFormat("import foo...bar;", Style
);
26661 verifyFormat("import ..........;", Style
);
26662 verifyFormat("module foo:private;", Style
);
26663 verifyFormat("import a", Style
);
26664 verifyFormat("module a", Style
);
26665 verifyFormat("export import a", Style
);
26666 verifyFormat("export module a", Style
);
26668 verifyFormat("import", Style
);
26669 verifyFormat("module", Style
);
26670 verifyFormat("export", Style
);
26672 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
26675 TEST_F(FormatTest
, CoroutineForCoawait
) {
26676 FormatStyle Style
= getLLVMStyle();
26677 verifyFormat("for co_await (auto x : range())\n ;");
26678 verifyFormat("for (auto i : arr) {\n"
26681 verifyFormat("for co_await (auto i : arr) {\n"
26684 verifyFormat("for co_await (auto i : foo(T{})) {\n"
26689 TEST_F(FormatTest
, CoroutineCoAwait
) {
26690 verifyFormat("int x = co_await foo();");
26691 verifyFormat("int x = (co_await foo());");
26692 verifyFormat("co_await (42);");
26693 verifyFormat("void operator co_await(int);");
26694 verifyFormat("void operator co_await(a);");
26695 verifyFormat("co_await a;");
26696 verifyFormat("co_await missing_await_resume{};");
26697 verifyFormat("co_await a; // comment");
26698 verifyFormat("void test0() { co_await a; }");
26699 verifyFormat("co_await co_await co_await foo();");
26700 verifyFormat("co_await foo().bar();");
26701 verifyFormat("co_await [this]() -> Task { co_return x; }");
26702 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
26703 "foo(); }(x, y);");
26705 FormatStyle Style
= getLLVMStyleWithColumns(40);
26706 verifyFormat("co_await [this](int a, int b) -> Task {\n"
26707 " co_return co_await foo();\n"
26710 verifyFormat("co_await;");
26713 TEST_F(FormatTest
, CoroutineCoYield
) {
26714 verifyFormat("int x = co_yield foo();");
26715 verifyFormat("int x = (co_yield foo());");
26716 verifyFormat("co_yield (42);");
26717 verifyFormat("co_yield {42};");
26718 verifyFormat("co_yield 42;");
26719 verifyFormat("co_yield n++;");
26720 verifyFormat("co_yield ++n;");
26721 verifyFormat("co_yield;");
26724 TEST_F(FormatTest
, CoroutineCoReturn
) {
26725 verifyFormat("co_return (42);");
26726 verifyFormat("co_return;");
26727 verifyFormat("co_return {};");
26728 verifyFormat("co_return x;");
26729 verifyFormat("co_return co_await foo();");
26730 verifyFormat("co_return co_yield foo();");
26733 TEST_F(FormatTest
, EmptyShortBlock
) {
26734 auto Style
= getLLVMStyle();
26735 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Empty
;
26737 verifyFormat("try {\n"
26739 "} catch (Exception &e) {\n"
26740 " e.printStackTrace();\n"
26744 verifyFormat("try {\n"
26746 "} catch (Exception &e) {}",
26750 TEST_F(FormatTest
, ShortTemplatedArgumentLists
) {
26751 auto Style
= getLLVMStyle();
26753 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style
);
26754 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style
);
26755 verifyFormat("struct Y : X<[] { return 0; }> {};", Style
);
26756 verifyFormat("struct Y<[] { return 0; }> {};", Style
);
26758 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style
);
26759 verifyFormat("template <int N> struct Foo<char[N]> {};", Style
);
26762 TEST_F(FormatTest
, MultilineLambdaInConditional
) {
26763 auto Style
= getLLVMStyleWithColumns(70);
26764 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
26771 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
26777 Style
= getLLVMStyleWithColumns(60);
26778 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
26785 verifyFormat("auto aLengthyIdentifier =\n"
26786 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
26792 Style
= getLLVMStyleWithColumns(40);
26793 verifyFormat("auto aLengthyIdentifier =\n"
26794 " oneExpressionSoThatWeBreak ? []() {\n"
26800 verifyFormat("auto aLengthyIdentifier =\n"
26801 " oneExpressionSoThatWeBreak\n"
26810 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndent
) {
26811 auto Style
= getLLVMStyle();
26813 StringRef Short
= "functionCall(paramA, paramB, paramC);\n"
26814 "void functionDecl(int a, int b, int c);";
26816 StringRef Medium
= "functionCall(paramA, paramB, paramC, paramD, paramE, "
26817 "paramF, paramG, paramH, paramI);\n"
26818 "void functionDecl(int argumentA, int argumentB, int "
26819 "argumentC, int argumentD, int argumentE);";
26821 verifyFormat(Short
, Style
);
26823 StringRef NoBreak
= "functionCall(paramA, paramB, paramC, paramD, paramE, "
26824 "paramF, paramG, paramH,\n"
26826 "void functionDecl(int argumentA, int argumentB, int "
26827 "argumentC, int argumentD,\n"
26828 " int argumentE);";
26830 verifyFormat(NoBreak
, Medium
, Style
);
26831 verifyFormat(NoBreak
,
26843 "void functionDecl(\n"
26844 " int argumentA,\n"
26845 " int argumentB,\n"
26846 " int argumentC,\n"
26847 " int argumentD,\n"
26852 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
26853 " nestedLongFunctionCall(argument1, "
26854 "argument2, argument3,\n"
26859 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
26861 verifyFormat(Short
, Style
);
26864 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26867 "void functionDecl(\n"
26868 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26873 Style
.AllowAllArgumentsOnNextLine
= false;
26874 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
26876 verifyFormat(Short
, Style
);
26879 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
26882 "void functionDecl(\n"
26883 " int argumentA, int argumentB, int argumentC, int argumentD, int "
26888 Style
.BinPackArguments
= false;
26889 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
26891 verifyFormat(Short
, Style
);
26893 verifyFormat("functionCall(\n"
26904 "void functionDecl(\n"
26905 " int argumentA,\n"
26906 " int argumentB,\n"
26907 " int argumentC,\n"
26908 " int argumentD,\n"
26913 verifyFormat("outerFunctionCall(\n"
26914 " nestedFunctionCall(argument1),\n"
26915 " nestedLongFunctionCall(\n"
26925 verifyFormat("int a = (int)b;", Style
);
26926 verifyFormat("int a = (int)b;",
26932 verifyFormat("return (true);", Style
);
26933 verifyFormat("return (true);",
26939 verifyFormat("void foo();", Style
);
26940 verifyFormat("void foo();",
26945 verifyFormat("void foo() {}", Style
);
26946 verifyFormat("void foo() {}",
26952 verifyFormat("auto string = std::string();", Style
);
26953 verifyFormat("auto string = std::string();",
26954 "auto string = std::string(\n"
26958 verifyFormat("void (*functionPointer)() = nullptr;", Style
);
26959 verifyFormat("void (*functionPointer)() = nullptr;",
26961 " *functionPointer\n"
26968 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentIfStatement
) {
26969 auto Style
= getLLVMStyle();
26971 verifyFormat("if (foo()) {\n"
26976 verifyFormat("if (quiteLongArg !=\n"
26977 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26983 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
26985 verifyFormat("if (foo()) {\n"
26990 verifyFormat("if (quiteLongArg !=\n"
26991 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
26997 verifyFormat("void foo() {\n"
26998 " if (camelCaseName < alsoLongName ||\n"
26999 " anotherEvenLongerName <=\n"
27000 " thisReallyReallyReallyReallyReallyReallyLongerName ||"
27002 " otherName < thisLastName) {\n"
27004 " } else if (quiteLongName < alsoLongName ||\n"
27005 " anotherEvenLongerName <=\n"
27006 " thisReallyReallyReallyReallyReallyReallyLonger"
27008 " otherName < thisLastName) {\n"
27014 Style
.ContinuationIndentWidth
= 2;
27015 verifyFormat("void foo() {\n"
27016 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
27017 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27020 " } else if (thisIsRatherALongIfClause && "
27021 "thatIExpectToBeBroken ||\n"
27022 " ontoMultipleLines && whenFormattedCorrectly) {\n"
27030 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentForStatement
) {
27031 auto Style
= getLLVMStyle();
27033 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27034 " doSomething();\n"
27038 verifyFormat("for (int myReallyLongCountVariable = 0; "
27039 "myReallyLongCountVariable < count;\n"
27040 " myReallyLongCountVariable++) {\n"
27041 " doSomething();\n"
27045 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
27047 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
27048 " doSomething();\n"
27052 verifyFormat("for (int myReallyLongCountVariable = 0; "
27053 "myReallyLongCountVariable < count;\n"
27054 " myReallyLongCountVariable++) {\n"
27055 " doSomething();\n"
27060 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentInitializers
) {
27061 auto Style
= getLLVMStyleWithColumns(60);
27062 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
27063 // Aggregate initialization.
27064 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27065 " 10000000, 20000000\n"
27068 verifyFormat("SomeStruct s{\n"
27069 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
27070 " \"zzzzzzzzzzzzzzzz\"\n"
27073 // Designated initializers.
27074 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
27075 " [0] = 10000000, [1] = 20000000\n"
27078 verifyFormat("SomeStruct s{\n"
27079 " .foo = \"xxxxxxxxxxxxx\",\n"
27080 " .bar = \"yyyyyyyyyyyyy\",\n"
27081 " .baz = \"zzzzzzzzzzzzz\"\n"
27084 // List initialization.
27085 verifyFormat("SomeStruct s{\n"
27086 " \"xxxxxxxxxxxxx\",\n"
27087 " \"yyyyyyyyyyyyy\",\n"
27088 " \"zzzzzzzzzzzzz\",\n"
27091 verifyFormat("SomeStruct{\n"
27092 " \"xxxxxxxxxxxxx\",\n"
27093 " \"yyyyyyyyyyyyy\",\n"
27094 " \"zzzzzzzzzzzzz\",\n"
27097 verifyFormat("new SomeStruct{\n"
27098 " \"xxxxxxxxxxxxx\",\n"
27099 " \"yyyyyyyyyyyyy\",\n"
27100 " \"zzzzzzzzzzzzz\",\n"
27103 // Member initializer.
27104 verifyFormat("class SomeClass {\n"
27106 " \"xxxxxxxxxxxxx\",\n"
27107 " \"yyyyyyyyyyyyy\",\n"
27108 " \"zzzzzzzzzzzzz\",\n"
27112 // Constructor member initializer.
27113 verifyFormat("SomeClass::SomeClass : strct{\n"
27114 " \"xxxxxxxxxxxxx\",\n"
27115 " \"yyyyyyyyyyyyy\",\n"
27116 " \"zzzzzzzzzzzzz\",\n"
27119 // Copy initialization.
27120 verifyFormat("SomeStruct s = SomeStruct{\n"
27121 " \"xxxxxxxxxxxxx\",\n"
27122 " \"yyyyyyyyyyyyy\",\n"
27123 " \"zzzzzzzzzzzzz\",\n"
27126 // Copy list initialization.
27127 verifyFormat("SomeStruct s = {\n"
27128 " \"xxxxxxxxxxxxx\",\n"
27129 " \"yyyyyyyyyyyyy\",\n"
27130 " \"zzzzzzzzzzzzz\",\n"
27133 // Assignment operand initialization.
27134 verifyFormat("s = {\n"
27135 " \"xxxxxxxxxxxxx\",\n"
27136 " \"yyyyyyyyyyyyy\",\n"
27137 " \"zzzzzzzzzzzzz\",\n"
27140 // Returned object initialization.
27141 verifyFormat("return {\n"
27142 " \"xxxxxxxxxxxxx\",\n"
27143 " \"yyyyyyyyyyyyy\",\n"
27144 " \"zzzzzzzzzzzzz\",\n"
27147 // Initializer list.
27148 verifyFormat("auto initializerList = {\n"
27149 " \"xxxxxxxxxxxxx\",\n"
27150 " \"yyyyyyyyyyyyy\",\n"
27151 " \"zzzzzzzzzzzzz\",\n"
27154 // Function parameter initialization.
27155 verifyFormat("func({\n"
27156 " \"xxxxxxxxxxxxx\",\n"
27157 " \"yyyyyyyyyyyyy\",\n"
27158 " \"zzzzzzzzzzzzz\",\n"
27161 // Nested init lists.
27162 verifyFormat("SomeStruct s = {\n"
27163 " {{init1, init2, init3, init4, init5},\n"
27164 " {init1, init2, init3, init4, init5}}\n"
27167 verifyFormat("SomeStruct s = {\n"
27175 " {init1, init2, init3, init4, init5}}\n"
27178 verifyFormat("SomeArrayT a[3] = {\n"
27190 verifyFormat("SomeArrayT a[3] = {\n"
27209 TEST_F(FormatTest
, UnderstandsDigraphs
) {
27210 verifyFormat("int arr<:5:> = {};");
27211 verifyFormat("int arr[5] = <%%>;");
27212 verifyFormat("int arr<:::qualified_variable:> = {};");
27213 verifyFormat("int arr[::qualified_variable] = <%%>;");
27214 verifyFormat("%:include <header>");
27215 verifyFormat("%:define A x##y");
27216 verifyFormat("#define A x%:%:y");
27219 TEST_F(FormatTest
, AlignArrayOfStructuresLeftAlignmentNonSquare
) {
27220 auto Style
= getLLVMStyle();
27221 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
27222 Style
.AlignConsecutiveAssignments
.Enabled
= true;
27223 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
27225 // The AlignArray code is incorrect for non square Arrays and can cause
27226 // crashes, these tests assert that the array is not changed but will
27227 // also act as regression tests for when it is properly fixed
27228 verifyFormat("struct test demo[] = {\n"
27234 verifyFormat("struct test demo[] = {\n"
27235 " {1, 2, 3, 4, 5},\n"
27240 verifyFormat("struct test demo[] = {\n"
27241 " {1, 2, 3, 4, 5},\n"
27243 " {6, 7, 8, 9, 10, 11, 12}\n"
27246 verifyFormat("struct test demo[] = {\n"
27249 " {6, 7, 8, 9, 10, 11, 12}\n"
27253 verifyFormat("S{\n"
27259 verifyFormat("S{\n"
27265 verifyFormat("void foo() {\n"
27266 " auto thing = test{\n"
27268 " {13}, {something}, // A\n"
27273 " auto thing = test{\n"
27276 " {something}, // A\n"
27283 TEST_F(FormatTest
, AlignArrayOfStructuresRightAlignmentNonSquare
) {
27284 auto Style
= getLLVMStyle();
27285 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
27286 Style
.AlignConsecutiveAssignments
.Enabled
= true;
27287 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
27289 // The AlignArray code is incorrect for non square Arrays and can cause
27290 // crashes, these tests assert that the array is not changed but will
27291 // also act as regression tests for when it is properly fixed
27292 verifyFormat("struct test demo[] = {\n"
27298 verifyFormat("struct test demo[] = {\n"
27299 " {1, 2, 3, 4, 5},\n"
27304 verifyFormat("struct test demo[] = {\n"
27305 " {1, 2, 3, 4, 5},\n"
27307 " {6, 7, 8, 9, 10, 11, 12}\n"
27310 verifyFormat("struct test demo[] = {\n"
27313 " {6, 7, 8, 9, 10, 11, 12}\n"
27317 verifyFormat("S{\n"
27323 verifyFormat("S{\n"
27329 verifyFormat("void foo() {\n"
27330 " auto thing = test{\n"
27332 " {13}, {something}, // A\n"
27337 " auto thing = test{\n"
27340 " {something}, // A\n"
27347 TEST_F(FormatTest
, FormatsVariableTemplates
) {
27348 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
27349 verifyFormat("template <typename T> "
27350 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
27353 TEST_F(FormatTest
, RemoveSemicolon
) {
27354 FormatStyle Style
= getLLVMStyle();
27355 Style
.RemoveSemicolon
= true;
27357 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27358 "int max(int a, int b) { return a > b ? a : b; };", Style
);
27360 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
27361 "int max(int a, int b) { return a > b ? a : b; };;", Style
);
27363 verifyFormat("class Foo {\n"
27364 " int getSomething() const { return something; }\n"
27367 " int getSomething() const { return something; };\n"
27371 verifyFormat("class Foo {\n"
27372 " int getSomething() const { return something; }\n"
27375 " int getSomething() const { return something; };;\n"
27379 verifyFormat("for (;;) {\n"
27383 verifyFormat("class [[deprecated(\"\")]] C {\n"
27388 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
27393 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style
);
27395 verifyFormat("void main() {}", "void main() {};", Style
);
27397 verifyFormat("struct Foo {\n"
27407 // We can't (and probably shouldn't) support the following.
27409 verifyFormat("void foo() {} //\n"
27411 "void foo() {}; //\n"
27416 verifyFormat("auto sgf = [] {\n"
27418 " a, b, c, d, e,\n"
27423 Style
.TypenameMacros
.push_back("STRUCT");
27424 verifyFormat("STRUCT(T, B) { int i; };", Style
);
27427 TEST_F(FormatTest
, BreakAfterAttributes
) {
27428 constexpr StringRef
Code("[[maybe_unused]] const int i;\n"
27429 "[[foo([[]])]] [[maybe_unused]]\n"
27431 "[[maybe_unused]]\n"
27433 "[[nodiscard]] inline int f(int &i);\n"
27434 "[[foo([[]])]] [[nodiscard]]\n"
27437 "inline int f(int &i) {\n"
27441 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27446 FormatStyle Style
= getLLVMStyle();
27447 EXPECT_EQ(Style
.BreakAfterAttributes
, FormatStyle::ABS_Leave
);
27448 verifyNoChange(Code
, Style
);
27450 Style
.BreakAfterAttributes
= FormatStyle::ABS_Never
;
27451 verifyFormat("[[maybe_unused]] const int i;\n"
27452 "[[foo([[]])]] [[maybe_unused]] int j;\n"
27453 "[[maybe_unused]] foo<int> k;\n"
27454 "[[nodiscard]] inline int f(int &i);\n"
27455 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
27456 "[[nodiscard]] inline int f(int &i) {\n"
27460 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
27466 Style
.BreakAfterAttributes
= FormatStyle::ABS_Always
;
27467 verifyFormat("[[maybe_unused]]\n"
27469 "[[foo([[]])]] [[maybe_unused]]\n"
27471 "[[maybe_unused]]\n"
27474 "inline int f(int &i);\n"
27475 "[[foo([[]])]] [[nodiscard]]\n"
27478 "inline int f(int &i) {\n"
27482 "[[foo([[]])]] [[nodiscard]]\n"
27483 "int g(int &i) {\n"
27489 constexpr StringRef
CtrlStmtCode("[[likely]] if (a)\n"
27495 "[[unlikely]] case 1:\n"
27502 "[[unlikely]] for (; c > 0; --c)\n"
27508 Style
.BreakAfterAttributes
= FormatStyle::ABS_Leave
;
27509 verifyNoChange(CtrlStmtCode
, Style
);
27511 Style
.BreakAfterAttributes
= FormatStyle::ABS_Never
;
27512 verifyFormat("[[likely]] if (a)\n"
27516 "[[foo([[]])]] switch (b) {\n"
27517 "[[unlikely]] case 1:\n"
27520 "[[likely]] default:\n"
27523 "[[unlikely]] for (; c > 0; --c)\n"
27525 "[[likely]] while (d > 0)\n"
27527 CtrlStmtCode
, Style
);
27529 Style
.BreakAfterAttributes
= FormatStyle::ABS_Always
;
27530 verifyFormat("[[likely]]\n"
27546 "for (; c > 0; --c)\n"
27551 CtrlStmtCode
, Style
);
27553 constexpr StringRef
CtorDtorCode("struct Foo {\n"
27554 " [[deprecated]] Foo();\n"
27555 " [[deprecated]] Foo() {}\n"
27556 " [[deprecated]] ~Foo();\n"
27557 " [[deprecated]] ~Foo() {}\n"
27558 " [[deprecated]] void f();\n"
27559 " [[deprecated]] void f() {}\n"
27561 "[[deprecated]] Bar::Bar() {}\n"
27562 "[[deprecated]] Bar::~Bar() {}\n"
27563 "[[deprecated]] void g() {}");
27564 verifyFormat("struct Foo {\n"
27565 " [[deprecated]]\n"
27567 " [[deprecated]]\n"
27569 " [[deprecated]]\n"
27571 " [[deprecated]]\n"
27573 " [[deprecated]]\n"
27575 " [[deprecated]]\n"
27584 CtorDtorCode
, Style
);
27586 Style
.BreakBeforeBraces
= FormatStyle::BS_Linux
;
27587 verifyFormat("struct Foo {\n"
27588 " [[deprecated]]\n"
27590 " [[deprecated]]\n"
27594 " [[deprecated]]\n"
27596 " [[deprecated]]\n"
27600 " [[deprecated]]\n"
27602 " [[deprecated]]\n"
27619 CtorDtorCode
, Style
);
27621 verifyFormat("struct Foo {\n"
27622 " [[maybe_unused]]\n"
27623 " void operator+();\n"
27626 "Foo &operator-(Foo &);",
27629 Style
.ReferenceAlignment
= FormatStyle::ReferenceAlignmentStyle::RAS_Left
;
27630 verifyFormat("[[nodiscard]]\n"
27631 "Foo& operator-(Foo&);",
27635 TEST_F(FormatTest
, InsertNewlineAtEOF
) {
27636 FormatStyle Style
= getLLVMStyle();
27637 Style
.InsertNewlineAtEOF
= true;
27639 verifyNoChange("int i;\n", Style
);
27640 verifyFormat("int i;\n", "int i;", Style
);
27642 constexpr StringRef Code
{"namespace {\n"
27645 verifyFormat(Code
.str() + '\n', Code
, Style
,
27646 {tooling::Range(19, 13)}); // line 3
27649 TEST_F(FormatTest
, KeepEmptyLinesAtEOF
) {
27650 FormatStyle Style
= getLLVMStyle();
27651 Style
.KeepEmptyLines
.AtEndOfFile
= true;
27653 const StringRef Code
{"int i;\n\n"};
27654 verifyNoChange(Code
, Style
);
27655 verifyFormat(Code
, "int i;\n\n\n", Style
);
27658 TEST_F(FormatTest
, SpaceAfterUDL
) {
27659 verifyFormat("auto c = (4s).count();");
27660 verifyFormat("auto x = 5s .count() == 5;");
27663 TEST_F(FormatTest
, InterfaceAsClassMemberName
) {
27664 verifyFormat("class Foo {\n"
27665 " int interface;\n"
27666 " Foo::Foo(int iface) : interface{iface} {}\n"
27670 TEST_F(FormatTest
, PreprocessorOverlappingRegions
) {
27671 verifyFormat("#ifdef\n\n"
27682 TEST_F(FormatTest
, RemoveParentheses
) {
27683 FormatStyle Style
= getLLVMStyle();
27684 EXPECT_EQ(Style
.RemoveParentheses
, FormatStyle::RPS_Leave
);
27686 Style
.RemoveParentheses
= FormatStyle::RPS_MultipleParentheses
;
27687 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style
);
27688 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style
);
27689 verifyFormat("decltype((foo->bar)) baz;", Style
);
27690 verifyFormat("class __declspec(dllimport) X {};",
27691 "class __declspec((dllimport)) X {};", Style
);
27692 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style
);
27693 verifyFormat("while (a)\n"
27698 verifyFormat("while ((a = b))\n"
27700 "while (((a = b)))\n"
27703 verifyFormat("if (a)\n"
27708 verifyFormat("if constexpr ((a = b))\n"
27710 "if constexpr (((a = b)))\n"
27713 verifyFormat("if (({ a; }))\n"
27715 "if ((({ a; })))\n"
27718 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));",
27719 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));",
27721 verifyFormat("foo((a, b));", "foo(((a, b)));", Style
);
27722 verifyFormat("foo((a, b));", "foo(((a), b));", Style
);
27723 verifyFormat("foo((a, b));", "foo((a, (b)));", Style
);
27724 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style
);
27725 verifyFormat("return (0);", "return (((0)));", Style
);
27726 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style
);
27727 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));",
27728 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27731 Style
.RemoveParentheses
= FormatStyle::RPS_ReturnStatement
;
27732 verifyFormat("#define Return0 return (0);", Style
);
27733 verifyFormat("return 0;", "return (0);", Style
);
27734 verifyFormat("co_return 0;", "co_return ((0));", Style
);
27735 verifyFormat("return 0;", "return (((0)));", Style
);
27736 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style
);
27737 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);",
27738 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));",
27740 verifyFormat("inline decltype(auto) f() {\n"
27746 "inline decltype(auto) f() {\n"
27753 verifyFormat("auto g() {\n"
27754 " decltype(auto) x = [] {\n"
27772 " decltype(auto) x = [] {\n"
27791 Style
.ColumnLimit
= 25;
27792 verifyFormat("return (a + b) - (c + d);",
27793 "return (((a + b)) -\n"
27798 TEST_F(FormatTest
, AllowBreakBeforeNoexceptSpecifier
) {
27799 auto Style
= getLLVMStyleWithColumns(35);
27801 EXPECT_EQ(Style
.AllowBreakBeforeNoexceptSpecifier
, FormatStyle::BBNSS_Never
);
27802 verifyFormat("void foo(int arg1,\n"
27803 " double arg2) noexcept;",
27806 // The following line does not fit within the 35 column limit, but that's what
27807 // happens with no break allowed.
27808 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
27809 " noexcept(baz(arg1)) &&\n"
27810 " noexcept(baz(arg2)));",
27813 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27816 Style
.AllowBreakBeforeNoexceptSpecifier
= FormatStyle::BBNSS_Always
;
27817 verifyFormat("void foo(int arg1,\n"
27818 " double arg2) noexcept;",
27821 verifyFormat("void bar(int arg1, double arg2)\n"
27822 " noexcept(noexcept(baz(arg1)) &&\n"
27823 " noexcept(baz(arg2)));",
27826 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
27830 Style
.AllowBreakBeforeNoexceptSpecifier
= FormatStyle::BBNSS_OnlyWithParen
;
27831 verifyFormat("void foo(int arg1,\n"
27832 " double arg2) noexcept;",
27835 verifyFormat("void bar(int arg1, double arg2)\n"
27836 " noexcept(noexcept(baz(arg1)) &&\n"
27837 " noexcept(baz(arg2)));",
27840 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
27844 TEST_F(FormatTest
, PPBranchesInBracedInit
) {
27845 verifyFormat("A a_{kFlag1,\n"
27863 TEST_F(FormatTest
, PPDirectivesAndCommentsInBracedInit
) {
27866 " /* abc */ \"abc\",\n"
27868 " /* xyz */ \"xyz\",\n"
27870 " /* last */ \"last\"};\n"
27872 getLLVMStyleWithColumns(30));
27875 TEST_F(FormatTest
, BreakAdjacentStringLiterals
) {
27876 constexpr StringRef Code
{
27877 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"};
27879 verifyFormat("return \"Code\"\n"
27880 " \"\\0\\52\\26\\55\\55\\0\"\n"
27885 auto Style
= getLLVMStyle();
27886 Style
.BreakAdjacentStringLiterals
= false;
27887 verifyFormat(Code
, Style
);
27890 TEST_F(FormatTest
, AlignUTFCommentsAndStringLiterals
) {
27892 "int rus; // А теперь комментарии, например, на русском, 2-байта\n"
27893 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
27894 " // уже отодвинут. Перенос, при этом, отрабатывает верно");
27896 auto Style
= getLLVMStyle();
27897 Style
.ColumnLimit
= 15;
27898 verifyNoChange("#define test \\\n"
27904 Style
.ColumnLimit
= 25;
27905 verifyFormat("struct foo {\n"
27906 " int iiiiii; ///< iiiiii\n"
27907 " int b; ///< ыыы\n"
27908 " int c; ///< ыыыы\n"
27912 Style
.ColumnLimit
= 35;
27913 verifyFormat("#define SENSOR_DESC_1 \\\n"
27915 " \"unit_of_measurement: \\\"°C\\\",\" \\\n"
27919 Style
.ColumnLimit
= 80;
27920 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
27921 verifyFormat("Languages languages = {\n"
27922 " Language{{'e', 'n'}, U\"Test English\" },\n"
27923 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
27924 " Language{{'r', 'u'}, U\"Test Русский\" },\n"
27929 TEST_F(FormatTest
, SpaceBetweenKeywordAndLiteral
) {
27930 verifyFormat("return .5;");
27931 verifyFormat("return not '5';");
27932 verifyFormat("return sizeof \"5\";");
27935 TEST_F(FormatTest
, BreakBinaryOperations
) {
27936 auto Style
= getLLVMStyleWithColumns(60);
27937 EXPECT_EQ(Style
.BreakBinaryOperations
, FormatStyle::BBO_Never
);
27939 // Logical operations
27940 verifyFormat("if (condition1 && condition2) {\n"
27944 verifyFormat("if (condition1 && condition2 &&\n"
27945 " (condition3 || condition4) && condition5 &&\n"
27950 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27951 " loooooooooooooooooooooongcondition2) {\n"
27956 verifyFormat("const int result = lhs + rhs;", Style
);
27958 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n"
27959 " loooooooooooooooooooooongop3;",
27962 verifyFormat("result = longOperand1 + longOperand2 -\n"
27963 " (longOperand3 + longOperand4) -\n"
27964 " longOperand5 * longOperand6;",
27967 verifyFormat("const int result =\n"
27968 " operand1 + operand2 - (operand3 + operand4);",
27971 Style
.BreakBinaryOperations
= FormatStyle::BBO_OnePerLine
;
27973 // Logical operations
27974 verifyFormat("if (condition1 && condition2) {\n"
27978 verifyFormat("if (condition1 && // comment\n"
27980 " (condition3 || condition4) && // comment\n"
27986 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n"
27987 " loooooooooooooooooooooongcondition2) {\n"
27992 verifyFormat("const int result = lhs + rhs;", Style
);
27994 verifyFormat("result = loooooooooooooooooooooongop1 +\n"
27995 " loooooooooooooooooooooongop2 +\n"
27996 " loooooooooooooooooooooongop3;",
27999 verifyFormat("const int result =\n"
28000 " operand1 + operand2 - (operand3 + operand4);",
28003 verifyFormat("result = longOperand1 +\n"
28004 " longOperand2 -\n"
28005 " (longOperand3 + longOperand4) -\n"
28006 " longOperand5 +\n"
28010 verifyFormat("result = operand1 +\n"
28018 // Ensure mixed precedence operations are handled properly
28019 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28021 verifyFormat("result = operand1 +\n"
28029 verifyFormat("result = operand1 *\n"
28037 verifyFormat("result = operand1 *\n"
28038 " (operand2 - operand3 * operand4) -\n"
28043 verifyFormat("result = operand1.member *\n"
28044 " (operand2.member() - operand3->mem * operand4) -\n"
28045 " operand5.member() +\n"
28046 " operand6->member;",
28049 Style
.BreakBinaryOperations
= FormatStyle::BBO_RespectPrecedence
;
28050 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28052 verifyFormat("result = operand1 +\n"
28053 " operand2 / operand3 +\n"
28054 " operand4 / operand5 * operand6;",
28057 verifyFormat("result = operand1 * operand2 -\n"
28058 " operand3 * operand4 -\n"
28063 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n"
28068 verifyFormat("std::uint32_t a = byte_buffer[0] |\n"
28069 " byte_buffer[1] << 8 |\n"
28070 " byte_buffer[2] << 16 |\n"
28071 " byte_buffer[3] << 24;",
28074 Style
.BreakBinaryOperations
= FormatStyle::BBO_OnePerLine
;
28075 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
28077 // Logical operations
28078 verifyFormat("if (condition1 && condition2) {\n"
28082 verifyFormat("if (loooooooooooooooooooooongcondition1\n"
28083 " && loooooooooooooooooooooongcondition2) {\n"
28088 verifyFormat("const int result = lhs + rhs;", Style
);
28090 verifyFormat("result = loooooooooooooooooooooongop1\n"
28091 " + loooooooooooooooooooooongop2\n"
28092 " + loooooooooooooooooooooongop3;",
28095 verifyFormat("const int result =\n"
28096 " operand1 + operand2 - (operand3 + operand4);",
28099 verifyFormat("result = longOperand1\n"
28100 " + longOperand2\n"
28101 " - (longOperand3 + longOperand4)\n"
28102 " - longOperand5\n"
28103 " + longOperand6;",
28106 verifyFormat("result = operand1\n"
28114 // Ensure mixed precedence operations are handled properly
28115 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28117 verifyFormat("result = operand1\n"
28125 verifyFormat("result = operand1\n"
28133 verifyFormat("result = operand1\n"
28134 " * (operand2 - operand3 * operand4)\n"
28139 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28140 " | byte_buffer[1]\n"
28142 " | byte_buffer[2]\n"
28144 " | byte_buffer[3]\n"
28148 Style
.BreakBinaryOperations
= FormatStyle::BBO_RespectPrecedence
;
28149 verifyFormat("result = op1 + op2 * op3 - op4;", Style
);
28151 verifyFormat("result = operand1\n"
28152 " + operand2 / operand3\n"
28153 " + operand4 / operand5 * operand6;",
28156 verifyFormat("result = operand1 * operand2\n"
28157 " - operand3 * operand4\n"
28162 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n"
28167 verifyFormat("std::uint32_t a = byte_buffer[0]\n"
28168 " | byte_buffer[1] << 8\n"
28169 " | byte_buffer[2] << 16\n"
28170 " | byte_buffer[3] << 24;",
28174 TEST_F(FormatTest
, RemoveEmptyLinesInUnwrappedLines
) {
28175 auto Style
= getLLVMStyle();
28176 Style
.RemoveEmptyLinesInUnwrappedLines
= true;
28178 verifyFormat("int c = a + b;",
28184 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
28185 "enum : unsigned\n"
28193 verifyFormat("class B : public E {\n"
28196 "class B : public E\n"
28204 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
28205 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
28207 " 23, \"hello\"},\n"
28208 " {7, 5, \"!!\"}};",
28211 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
28212 "int myFunction(\n"
28214 " int aaaaaaaaaaaaa,\n"
28216 " int ccccccccccccc, int d);",
28219 verifyFormat("switch (e) {\n"
28235 verifyFormat("while (true) {\n"
28243 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
28244 " std::map<int, std::string> *outputMap);",
28245 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
28247 " (std::map<int, std::string> *outputMap);",
28251 TEST_F(FormatTest
, KeepFormFeed
) {
28252 auto Style
= getLLVMStyle();
28253 Style
.KeepFormFeed
= true;
28255 constexpr StringRef NoFormFeed
{"int i;\n"
28258 verifyFormat(NoFormFeed
,
28263 verifyFormat(NoFormFeed
,
28268 verifyFormat(NoFormFeed
,
28273 verifyFormat(NoFormFeed
,
28279 constexpr StringRef FormFeed
{"int i;\n"
28282 verifyNoChange(FormFeed
, Style
);
28284 Style
.LineEnding
= FormatStyle::LE_LF
;
28285 verifyFormat(FormFeed
,
28291 constexpr StringRef FormFeedBeforeEmptyLine
{"int i;\n"
28295 Style
.MaxEmptyLinesToKeep
= 2;
28296 verifyFormat(FormFeedBeforeEmptyLine
,
28302 verifyFormat(FormFeedBeforeEmptyLine
,
28311 } // namespace test
28312 } // namespace format
28313 } // namespace clang